/** * Returns <code>true</code> if the receiver is enabled, and <code>false</code> otherwise. A * disabled menu item is typically not selectable from the user interface and draws with an * inactive or "grayed" look. * * @return the receiver's enabled state * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> * * @see #isEnabled */ public boolean getEnabled() { checkWidget(); if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { long /*int*/ hwndCB = parent.hwndCB; TBBUTTONINFO info = new TBBUTTONINFO(); info.cbSize = TBBUTTONINFO.sizeof; info.dwMask = OS.TBIF_STATE; OS.SendMessage(hwndCB, OS.TB_GETBUTTONINFO, id, info); return (info.fsState & OS.TBSTATE_ENABLED) != 0; } /* * Feature in Windows. For some reason, when the menu item * is a separator, GetMenuItemInfo() always indicates that * the item is not enabled. The fix is to track the enabled * state for separators. */ if ((style & SWT.SEPARATOR) != 0) { return (state & DISABLED) == 0; } long /*int*/ hMenu = parent.handle; MENUITEMINFO info = new MENUITEMINFO(); info.cbSize = MENUITEMINFO.sizeof; info.fMask = OS.MIIM_STATE; boolean success; if (OS.IsWinCE) { int index = parent.indexOf(this); if (index == -1) error(SWT.ERROR_CANNOT_GET_ENABLED); success = OS.GetMenuItemInfo(hMenu, index, true, info); } else { success = OS.GetMenuItemInfo(hMenu, id, false, info); } if (!success) error(SWT.ERROR_CANNOT_GET_ENABLED); return (info.fState & (OS.MFS_DISABLED | OS.MFS_GRAYED)) == 0; }
/** * Returns a rectangle describing the receiver's size and location relative to its parent (or its * display if its parent is null). * * @return the receiver's bounding rectangle * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> * * @since 3.1 */ /*public*/ Rectangle getBounds() { checkWidget(); if (OS.IsWinCE) return new Rectangle(0, 0, 0, 0); int index = parent.indexOf(this); if (index == -1) return new Rectangle(0, 0, 0, 0); if ((parent.style & SWT.BAR) != 0) { Decorations shell = parent.parent; if (shell.menuBar != parent) { return new Rectangle(0, 0, 0, 0); } long /*int*/ hwndShell = shell.handle; MENUBARINFO info1 = new MENUBARINFO(); info1.cbSize = MENUBARINFO.sizeof; if (!OS.GetMenuBarInfo(hwndShell, OS.OBJID_MENU, 1, info1)) { return new Rectangle(0, 0, 0, 0); } MENUBARINFO info2 = new MENUBARINFO(); info2.cbSize = MENUBARINFO.sizeof; if (!OS.GetMenuBarInfo(hwndShell, OS.OBJID_MENU, index + 1, info2)) { return new Rectangle(0, 0, 0, 0); } int x = info2.left - info1.left; int y = info2.top - info1.top; int width = info2.right - info2.left; int height = info2.bottom - info2.top; return new Rectangle(x, y, width, height); } else { long /*int*/ hMenu = parent.handle; RECT rect1 = new RECT(); if (!OS.GetMenuItemRect(0, hMenu, 0, rect1)) { return new Rectangle(0, 0, 0, 0); } RECT rect2 = new RECT(); if (!OS.GetMenuItemRect(0, hMenu, index, rect2)) { return new Rectangle(0, 0, 0, 0); } int x = rect2.left - rect1.left + 2; int y = rect2.top - rect1.top + 2; int width = rect2.right - rect2.left; int height = rect2.bottom - rect2.top; return new Rectangle(x, y, width, height); } }
/** * Sets the selection state of the receiver. * * <p>When the receiver is of type <code>CHECK</code> or <code>RADIO</code>, it is selected when * it is checked. * * @param selected the new selection state * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public void setSelection(boolean selected) { checkWidget(); if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return; if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) return; long /*int*/ hMenu = parent.handle; if (OS.IsWinCE) { int index = parent.indexOf(this); if (index == -1) return; int uCheck = OS.MF_BYPOSITION | (selected ? OS.MF_CHECKED : OS.MF_UNCHECKED); OS.CheckMenuItem(hMenu, index, uCheck); } else { MENUITEMINFO info = new MENUITEMINFO(); info.cbSize = MENUITEMINFO.sizeof; info.fMask = OS.MIIM_STATE; boolean success = OS.GetMenuItemInfo(hMenu, id, false, info); if (!success) error(SWT.ERROR_CANNOT_SET_SELECTION); info.fState &= ~OS.MFS_CHECKED; if (selected) info.fState |= OS.MFS_CHECKED; success = OS.SetMenuItemInfo(hMenu, id, false, info); if (!success) { /* * Bug in Windows. For some reason SetMenuItemInfo(), * returns a fail code when setting the enabled or * selected state of a default item, but sets the * state anyway. The fix is to ignore the error. * * NOTE: This only happens on Vista. */ if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(6, 0)) { success = id == OS.GetMenuDefaultItem(hMenu, OS.MF_BYCOMMAND, OS.GMDI_USEDISABLED); } if (!success) { int error = OS.GetLastError(); SWT.error( SWT.ERROR_CANNOT_SET_SELECTION, null, " [GetLastError=0x" + Integer.toHexString(error) + "]"); // $NON-NLS-1$ $NON-NLS-2$ } } } parent.redraw(); }
/** * Enables the receiver if the argument is <code>true</code>, and disables it otherwise. A * disabled menu item is typically not selectable from the user interface and draws with an * inactive or "grayed" look. * * @param enabled the new enabled state * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public void setEnabled(boolean enabled) { checkWidget(); if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { long /*int*/ hwndCB = parent.hwndCB; TBBUTTONINFO info = new TBBUTTONINFO(); info.cbSize = TBBUTTONINFO.sizeof; info.dwMask = OS.TBIF_STATE; OS.SendMessage(hwndCB, OS.TB_GETBUTTONINFO, id, info); info.fsState &= ~OS.TBSTATE_ENABLED; if (enabled) info.fsState |= OS.TBSTATE_ENABLED; OS.SendMessage(hwndCB, OS.TB_SETBUTTONINFO, id, info); } else { /* * Feature in Windows. For some reason, when the menu item * is a separator, GetMenuItemInfo() always indicates that * the item is not enabled. The fix is to track the enabled * state for separators. */ if ((style & SWT.SEPARATOR) != 0) { if (enabled) { state &= ~DISABLED; } else { state |= DISABLED; } } long /*int*/ hMenu = parent.handle; if (OS.IsWinCE) { int index = parent.indexOf(this); if (index == -1) return; int uEnable = OS.MF_BYPOSITION | (enabled ? OS.MF_ENABLED : OS.MF_GRAYED); OS.EnableMenuItem(hMenu, index, uEnable); } else { MENUITEMINFO info = new MENUITEMINFO(); info.cbSize = MENUITEMINFO.sizeof; info.fMask = OS.MIIM_STATE; boolean success = OS.GetMenuItemInfo(hMenu, id, false, info); if (!success) { int error = OS.GetLastError(); SWT.error( SWT.ERROR_CANNOT_SET_ENABLED, null, " [GetLastError=0x" + Integer.toHexString(error) + "]"); // $NON-NLS-1$ $NON-NLS-2$ } int bits = OS.MFS_DISABLED | OS.MFS_GRAYED; if (enabled) { if ((info.fState & bits) == 0) return; info.fState &= ~bits; } else { if ((info.fState & bits) == bits) return; info.fState |= bits; } success = OS.SetMenuItemInfo(hMenu, id, false, info); if (!success) { /* * Bug in Windows. For some reason SetMenuItemInfo(), * returns a fail code when setting the enabled or * selected state of a default item, but sets the * state anyway. The fix is to ignore the error. * * NOTE: This only happens on Vista. */ if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(6, 0)) { success = id == OS.GetMenuDefaultItem(hMenu, OS.MF_BYCOMMAND, OS.GMDI_USEDISABLED); } if (!success) { int error = OS.GetLastError(); SWT.error( SWT.ERROR_CANNOT_SET_ENABLED, null, " [GetLastError=0x" + Integer.toHexString(error) + "]"); // $NON-NLS-1$ $NON-NLS-2$ } } } } parent.destroyAccelerators(); parent.redraw(); }