/** * Sets the image the receiver will display to the argument. * * <p>Note: This operation is a hint and is not supported on platforms that do not have this * concept (for example, Windows NT). Furthermore, some platforms (such as GTK), cannot display * both a check box and an image at the same time. Instead, they hide the image and display the * check box. * * @param image the image to display * @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 setImage(Image image) { checkWidget(); if ((style & SWT.SEPARATOR) != 0) return; super.setImage(image); if (OS.IsWinCE) { 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_IMAGE; info.iImage = parent.imageIndex(image); OS.SendMessage(hwndCB, OS.TB_SETBUTTONINFO, id, info); } return; } if (OS.WIN32_VERSION < OS.VERSION(4, 10)) return; MENUITEMINFO info = new MENUITEMINFO(); info.cbSize = MENUITEMINFO.sizeof; info.fMask = OS.MIIM_BITMAP; if (parent.foreground != -1) { info.hbmpItem = OS.HBMMENU_CALLBACK; } else { if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(6, 0) && OS.IsAppThemed()) { if (hBitmap != 0) OS.DeleteObject(hBitmap); info.hbmpItem = hBitmap = image != null ? Display.create32bitDIB(image) : 0; } else { info.hbmpItem = image != null ? OS.HBMMENU_CALLBACK : 0; } } long /*int*/ hMenu = parent.handle; OS.SetMenuItemInfo(hMenu, id, false, info); parent.redraw(); }
/** * Returns <code>true</code> if the receiver is enabled and all of the receiver's ancestors are * enabled, and <code>false</code> otherwise. A disabled menu 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 #getEnabled */ public boolean isEnabled() { checkWidget(); Menu parentMenu = getParentMenu(); if (parentMenu == null) { return getEnabled() && parent.isEnabled(); } return getEnabled() && parentMenu.isEnabled(); }
protected void doDropDown() { Menu menu = new Menu(getShell(), SWT.POP_UP); createDropDownMenu(menu); if (menu.getItemCount() == 0) return; Point loc = UIUtils.calcPopupMenuLocation(this); menu.setLocation(loc); menu.setVisible(true); }
/** * 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; }
void releaseChildren(boolean destroy) { if (menu != null) { menu.release(false); menu = null; } super.releaseChildren(destroy); }
MenuItem(Menu parent, Menu menu, int style, int index) { super(parent, checkStyle(style)); this.parent = parent; this.menu = menu; this.index = index; if (menu != null) menu.cascade = this; display.addMenuItem(this); }
void setOrientation(int orientation) { long /*int*/ hMenu = parent.handle; MENUITEMINFO info = new MENUITEMINFO(); info.cbSize = MENUITEMINFO.sizeof; info.fMask = OS.MIIM_FTYPE; info.fType = widgetStyle(); OS.SetMenuItemInfo(hMenu, id, false, info); if (menu != null) menu._setOrientation(orientation); }
void releaseWidget() { super.releaseWidget(); if (hBitmap != 0) OS.DeleteObject(hBitmap); hBitmap = 0; if (accelerator != 0) { parent.destroyAccelerators(); } accelerator = 0; display.removeMenuItem(this); }
void selectRadio() { int index = 0; MenuItem[] items = parent.getItems(); while (index < items.length && items[index] != this) index++; int i = index - 1; while (i >= 0 && items[i].setRadioSelection(false)) --i; int j = index + 1; while (j < items.length && items[j].setRadioSelection(false)) j++; setSelection(true); }
/** * 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(); }
LRESULT wmCommandChild(long /*int*/ wParam, long /*int*/ lParam) { if ((style & SWT.CHECK) != 0) { setSelection(!getSelection()); } else { if ((style & SWT.RADIO) != 0) { if ((parent.getStyle() & SWT.NO_RADIO_GROUP) != 0) { setSelection(!getSelection()); } else { selectRadio(); } } } sendSelectionEvent(SWT.Selection); return null; }
/** * Sets the receiver's pull down menu to the argument. Only <code>CASCADE</code> menu items can * have a pull down menu. The sequence of key strokes, button presses and/or button releases that * are used to request a pull down menu is platform specific. * * <p>Note: Disposing of a menu item that has a pull down menu will dispose of the menu. To avoid * this behavior, set the menu to null before the menu item is disposed. * * @param menu the new pull down menu * @exception IllegalArgumentException * <ul> * <li>ERROR_MENU_NOT_DROP_DOWN - if the menu is not a drop down menu * <li>ERROR_MENUITEM_NOT_CASCADE - if the menu item is not a <code>CASCADE</code> * <li>ERROR_INVALID_ARGUMENT - if the menu has been disposed * <li>ERROR_INVALID_PARENT - if the menu is not in the same widget tree * </ul> * * @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 setMenu(Menu menu) { checkWidget(); /* Check to make sure the new menu is valid */ if ((style & SWT.CASCADE) == 0) { error(SWT.ERROR_MENUITEM_NOT_CASCADE); } if (menu != null) { if (menu.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); if ((menu.style & SWT.DROP_DOWN) == 0) { error(SWT.ERROR_MENU_NOT_DROP_DOWN); } if (menu.parent != parent.parent) { error(SWT.ERROR_INVALID_PARENT); } } setMenu(menu, false); }
/** * 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); } }
LRESULT wmMeasureChild(long /*int*/ wParam, long /*int*/ lParam) { MEASUREITEMSTRUCT struct = new MEASUREITEMSTRUCT(); OS.MoveMemory(struct, lParam, MEASUREITEMSTRUCT.sizeof); int width = 0, height = 0; if (image != null) { Rectangle rect = image.getBounds(); width = rect.width; height = rect.height; } else { /* * Bug in Windows. If a menu contains items that have * images and can be checked, Windows does not include * the width of the image and the width of the check when * computing the width of the menu. When the longest item * does not have an image, the label and the accelerator * text can overlap. The fix is to use SetMenuItemInfo() * to indicate that all items have a bitmap and then include * the width of the widest bitmap in WM_MEASURECHILD. */ MENUINFO lpcmi = new MENUINFO(); lpcmi.cbSize = MENUINFO.sizeof; lpcmi.fMask = OS.MIM_STYLE; long /*int*/ hMenu = parent.handle; OS.GetMenuInfo(hMenu, lpcmi); if ((lpcmi.dwStyle & OS.MNS_CHECKORBMP) == 0) { MenuItem[] items = parent.getItems(); for (int i = 0; i < items.length; i++) { MenuItem item = items[i]; if (item.image != null) { Rectangle rect = item.image.getBounds(); width = Math.max(width, rect.width); } } } } if (width != 0 || height != 0) { struct.itemWidth = width + MARGIN_WIDTH * 2; struct.itemHeight = height + MARGIN_HEIGHT * 2; OS.MoveMemory(lParam, struct, MEASUREITEMSTRUCT.sizeof); } return null; }
void setMenu(Menu menu, boolean dispose) { /* Assign the new menu */ Menu oldMenu = this.menu; if (oldMenu == menu) return; if (oldMenu != null) oldMenu.cascade = null; this.menu = menu; /* Assign the new menu in the OS */ if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { if (OS.IsPPC) { long /*int*/ hwndCB = parent.hwndCB; long /*int*/ hMenu = menu == null ? 0 : menu.handle; OS.SendMessage(hwndCB, OS.SHCMBM_SETSUBMENU, id, hMenu); } if (OS.IsSP) error(SWT.ERROR_CANNOT_SET_MENU); } else { long /*int*/ hMenu = parent.handle; MENUITEMINFO info = new MENUITEMINFO(); info.cbSize = MENUITEMINFO.sizeof; info.fMask = OS.MIIM_DATA; int index = 0; while (OS.GetMenuItemInfo(hMenu, index, true, info)) { if (info.dwItemData == id) break; index++; } if (info.dwItemData != id) return; int cch = 128; long /*int*/ hHeap = OS.GetProcessHeap(); int byteCount = cch * TCHAR.sizeof; long /*int*/ pszText = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount); info.fMask = OS.MIIM_STATE | OS.MIIM_ID | OS.MIIM_DATA; /* * Bug in Windows. When GetMenuItemInfo() is used to get the text, * for an item that has a bitmap set using MIIM_BITMAP, the text is * not returned. This means that when SetMenuItemInfo() is used to * set the submenu and the current menu state, the text is lost. * The fix is use MIIM_BITMAP and MIIM_STRING. */ if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(4, 10)) { info.fMask |= OS.MIIM_BITMAP | OS.MIIM_STRING; } else { info.fMask |= OS.MIIM_TYPE; } info.dwTypeData = pszText; info.cch = cch; boolean success = OS.GetMenuItemInfo(hMenu, index, true, info); if (menu != null) { menu.cascade = this; info.fMask |= OS.MIIM_SUBMENU; info.hSubMenu = menu.handle; } if (OS.IsWinCE) { OS.RemoveMenu(hMenu, index, OS.MF_BYPOSITION); /* * On WinCE, InsertMenuItem() is not available. The fix is to * use SetMenuItemInfo() but this call does not set the menu item * state and submenu. The fix is to use InsertMenu() to insert * the item, SetMenuItemInfo() to set the string and EnableMenuItem() * and CheckMenuItem() to set the state. */ long /*int*/ uIDNewItem = id; int uFlags = OS.MF_BYPOSITION; if (menu != null) { uFlags |= OS.MF_POPUP; uIDNewItem = menu.handle; } TCHAR lpNewItem = new TCHAR(0, " ", true); success = OS.InsertMenu(hMenu, index, uFlags, uIDNewItem, lpNewItem); if (success) { info.fMask = OS.MIIM_DATA | OS.MIIM_TYPE; success = OS.SetMenuItemInfo(hMenu, index, true, info); if ((info.fState & (OS.MFS_DISABLED | OS.MFS_GRAYED)) != 0) { OS.EnableMenuItem(hMenu, index, OS.MF_BYPOSITION | OS.MF_GRAYED); } if ((info.fState & OS.MFS_CHECKED) != 0) { OS.CheckMenuItem(hMenu, index, OS.MF_BYPOSITION | OS.MF_CHECKED); } } } else { if (dispose || oldMenu == null) { success = OS.SetMenuItemInfo(hMenu, index, true, info); } else { /* * Feature in Windows. When SetMenuItemInfo () is used to * set a submenu and the menu item already has a submenu, * Windows destroys the previous menu. This is undocumented * and unexpected but not necessarily wrong. The fix is to * remove the item with RemoveMenu () which does not destroy * the submenu and then insert the item with InsertMenuItem (). */ OS.RemoveMenu(hMenu, index, OS.MF_BYPOSITION); success = OS.InsertMenuItem(hMenu, index, true, info); } } if (pszText != 0) OS.HeapFree(hHeap, 0, pszText); if (!success) { int error = OS.GetLastError(); SWT.error( SWT.ERROR_CANNOT_SET_MENU, null, " [GetLastError=0x" + Integer.toHexString(error) + "]"); // $NON-NLS-1$ $NON-NLS-2$ } } parent.destroyAccelerators(); }
/** * Constructs a new instance of this class given its parent (which must be a <code>Menu</code>) * and a style value describing its behavior and appearance. The item is added to the end of the * items maintained by its parent. * * <p>The style value is either one of the style constants defined in class <code>SWT</code> which * is applicable to instances of this class, or must be built by <em>bitwise OR</em>'ing together * (that is, using the <code>int</code> "|" operator) two or more of those <code>SWT</code> style * constants. The class description lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. * * @param parent a menu control which will be the parent of the new instance (cannot be null) * @param style the style of control to construct * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the parent is null * </ul> * * @exception SWTException * <ul> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass * </ul> * * @see SWT#CHECK * @see SWT#CASCADE * @see SWT#PUSH * @see SWT#RADIO * @see SWT#SEPARATOR * @see Widget#checkSubclass * @see Widget#getStyle */ public MenuItem(Menu parent, int style) { super(parent, checkStyle(style)); this.parent = parent; parent.createItem(this, (index = parent.getItemCount())); }
/** * 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(); }
/** * Sets the widget accelerator. An accelerator is the bit-wise OR of zero or more modifier masks * and a key. Examples: <code>SWT.MOD1 | SWT.MOD2 | 'T', SWT.MOD3 | SWT.F2</code>. <code> * SWT.CONTROL | SWT.SHIFT | 'T', SWT.ALT | SWT.F2</code>. The default value is zero, indicating * that the menu item does not have an accelerator. * * @param accelerator an integer that is the bit-wise OR of masks and a key * </ul> * * @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 setAccelerator(int accelerator) { checkWidget(); if (this.accelerator == accelerator) return; this.accelerator = accelerator; parent.destroyAccelerators(); }
void fixMenus(Decorations newParent) { if (menu != null) menu.fixMenus(newParent); }
void reskinChildren(int flags) { if (menu != null) { menu.reskin(flags); } super.reskinChildren(flags); }
/** * Constructs a new instance of this class given its parent (which must be a <code>Menu</code>), a * style value describing its behavior and appearance, and the index at which to place it in the * items maintained by its parent. * * <p>The style value is either one of the style constants defined in class <code>SWT</code> which * is applicable to instances of this class, or must be built by <em>bitwise OR</em>'ing together * (that is, using the <code>int</code> "|" operator) two or more of those <code>SWT</code> style * constants. The class description lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. * * @param parent a menu control which will be the parent of the new instance (cannot be null) * @param style the style of control to construct * @param index the zero-relative index to store the receiver in its parent * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the parent is null * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the * parent (inclusive) * </ul> * * @exception SWTException * <ul> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass * </ul> * * @see SWT#CHECK * @see SWT#CASCADE * @see SWT#PUSH * @see SWT#RADIO * @see SWT#SEPARATOR * @see Widget#checkSubclass * @see Widget#getStyle */ public MenuItem(Menu parent, int style, int index) { super(parent, checkStyle(style)); this.parent = parent; parent.createItem(this, (this.index = index)); }
/** * Creates all items located in the popup menu and associates all the menu items with their * appropriate functions. * * @return Menu The created popup menu. */ private Menu createPopUpMenu() { Menu popUpMenu = new Menu(shell, SWT.POP_UP); /** Adds a listener to handle enabling and disabling some items in the Edit submenu. */ popUpMenu.addMenuListener( new MenuAdapter() { @Override public void menuShown(MenuEvent e) { Menu menu = (Menu) e.widget; MenuItem[] items = menu.getItems(); int count = table.getSelectionCount(); items[2].setEnabled(count != 0); // edit items[3].setEnabled(count != 0); // copy items[4].setEnabled(copyBuffer != null); // paste items[5].setEnabled(count != 0); // delete items[7].setEnabled(table.getItemCount() != 0); // find } }); // New MenuItem item = new MenuItem(popUpMenu, SWT.PUSH); item.setText(resAddressBook.getString("Pop_up_new")); item.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { newEntry(); } }); new MenuItem(popUpMenu, SWT.SEPARATOR); // Edit item = new MenuItem(popUpMenu, SWT.PUSH); item.setText(resAddressBook.getString("Pop_up_edit")); item.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length == 0) return; editEntry(items[0]); } }); // Copy item = new MenuItem(popUpMenu, SWT.PUSH); item.setText(resAddressBook.getString("Pop_up_copy")); item.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length == 0) return; copyBuffer = new String[table.getColumnCount()]; for (int i = 0; i < copyBuffer.length; i++) { copyBuffer[i] = items[0].getText(i); } } }); // Paste item = new MenuItem(popUpMenu, SWT.PUSH); item.setText(resAddressBook.getString("Pop_up_paste")); item.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (copyBuffer == null) return; TableItem item = new TableItem(table, SWT.NONE); item.setText(copyBuffer); isModified = true; } }); // Delete item = new MenuItem(popUpMenu, SWT.PUSH); item.setText(resAddressBook.getString("Pop_up_delete")); item.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length == 0) return; items[0].dispose(); isModified = true; } }); new MenuItem(popUpMenu, SWT.SEPARATOR); // Find... item = new MenuItem(popUpMenu, SWT.PUSH); item.setText(resAddressBook.getString("Pop_up_find")); item.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { searchDialog.open(); } }); return popUpMenu; }
/** * Returns <code>true</code> if the receiver is enabled and all of the receiver's ancestors are * 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 #getEnabled */ public boolean isEnabled() { return getEnabled() && parent.isEnabled(); }
/** * Creates all the items located in the File submenu and associate all the menu items with their * appropriate functions. * * @param menuBar Menu the <code>Menu</code> that file contain the File submenu. */ private void createFileMenu(Menu menuBar) { // File menu. MenuItem item = new MenuItem(menuBar, SWT.CASCADE); item.setText(resAddressBook.getString("File_menu_title")); Menu menu = new Menu(shell, SWT.DROP_DOWN); item.setMenu(menu); /** Adds a listener to handle enabling and disabling some items in the Edit submenu. */ menu.addMenuListener( new MenuAdapter() { @Override public void menuShown(MenuEvent e) { Menu menu = (Menu) e.widget; MenuItem[] items = menu.getItems(); items[1].setEnabled(table.getSelectionCount() != 0); // edit contact items[5].setEnabled((file != null) && isModified); // save items[6].setEnabled(table.getItemCount() != 0); // save as } }); // File -> New Contact MenuItem subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("New_contact")); subItem.setAccelerator(SWT.MOD1 + 'N'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { newEntry(); } }); subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Edit_contact")); subItem.setAccelerator(SWT.MOD1 + 'E'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length == 0) return; editEntry(items[0]); } }); new MenuItem(menu, SWT.SEPARATOR); // File -> New Address Book subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("New_address_book")); subItem.setAccelerator(SWT.MOD1 + 'B'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (closeAddressBook()) { newAddressBook(); } } }); // File -> Open subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Open_address_book")); subItem.setAccelerator(SWT.MOD1 + 'O'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (closeAddressBook()) { openAddressBook(); } } }); // File -> Save. subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Save_address_book")); subItem.setAccelerator(SWT.MOD1 + 'S'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { save(); } }); // File -> Save As. subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Save_book_as")); subItem.setAccelerator(SWT.MOD1 + 'A'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { saveAs(); } }); new MenuItem(menu, SWT.SEPARATOR); // File -> Exit. subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Exit")); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { shell.close(); } }); }
/** * Creates all the items located in the Edit submenu and associate all the menu items with their * appropriate functions. * * @param menuBar Menu the <code>Menu</code> that file contain the Edit submenu. * @see #createSortMenu() */ private MenuItem createEditMenu(Menu menuBar) { // Edit menu. MenuItem item = new MenuItem(menuBar, SWT.CASCADE); item.setText(resAddressBook.getString("Edit_menu_title")); Menu menu = new Menu(shell, SWT.DROP_DOWN); item.setMenu(menu); /** Add a listener to handle enabling and disabling some items in the Edit submenu. */ menu.addMenuListener( new MenuAdapter() { @Override public void menuShown(MenuEvent e) { Menu menu = (Menu) e.widget; MenuItem[] items = menu.getItems(); int count = table.getSelectionCount(); items[0].setEnabled(count != 0); // edit items[1].setEnabled(count != 0); // copy items[2].setEnabled(copyBuffer != null); // paste items[3].setEnabled(count != 0); // delete items[5].setEnabled(table.getItemCount() != 0); // sort } }); // Edit -> Edit MenuItem subItem = new MenuItem(menu, SWT.PUSH); subItem.setText(resAddressBook.getString("Edit")); subItem.setAccelerator(SWT.MOD1 + 'E'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length == 0) return; editEntry(items[0]); } }); // Edit -> Copy subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Copy")); subItem.setAccelerator(SWT.MOD1 + 'C'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length == 0) return; copyBuffer = new String[table.getColumnCount()]; for (int i = 0; i < copyBuffer.length; i++) { copyBuffer[i] = items[0].getText(i); } } }); // Edit -> Paste subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Paste")); subItem.setAccelerator(SWT.MOD1 + 'V'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (copyBuffer == null) return; TableItem item = new TableItem(table, SWT.NONE); item.setText(copyBuffer); isModified = true; } }); // Edit -> Delete subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Delete")); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length == 0) return; items[0].dispose(); isModified = true; } }); new MenuItem(menu, SWT.SEPARATOR); // Edit -> Sort(Cascade) subItem = new MenuItem(menu, SWT.CASCADE); subItem.setText(resAddressBook.getString("Sort")); Menu submenu = createSortMenu(); subItem.setMenu(submenu); return item; }
void destroyWidget() { parent.destroyItem(this); releaseHandle(); }
void releaseParent() { super.releaseParent(); if (menu != null) menu.dispose(); menu = null; }
/** * Sets the receiver's text. The string may include the mnemonic character and accelerator text. * * <p>Mnemonics are indicated by an '&' that causes the next character to be the mnemonic. * When the user presses a key sequence that matches the mnemonic, a selection event occurs. On * most platforms, the mnemonic appears underlined but may be emphasised in a platform specific * manner. The mnemonic indicator character '&' can be escaped by doubling it in the string, * causing a single '&' to be displayed. * * <p>Accelerator text is indicated by the '\t' character. On platforms that support accelerator * text, the text that follows the '\t' character is displayed to the user, typically indicating * the key stroke that will cause the item to become selected. On most platforms, the accelerator * text appears right aligned in the menu. Setting the accelerator text does not install the * accelerator key sequence. The accelerator key sequence is installed using #setAccelerator. * * @param string the new text * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the text is null * </ul> * * @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 #setAccelerator */ public void setText(String string) { checkWidget(); if (string == null) error(SWT.ERROR_NULL_ARGUMENT); if ((style & SWT.SEPARATOR) != 0) return; if (text.equals(string)) return; super.setText(string); long /*int*/ hHeap = OS.GetProcessHeap(); long /*int*/ pszText = 0; boolean success = false; if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { /* * Bug in WinCE PPC. Tool items on the menubar don't resize * correctly when the character '&' is used (even when it * is a sequence '&&'). The fix is to remove all '&' from * the string. */ if (string.indexOf('&') != -1) { int length = string.length(); char[] text = new char[length]; string.getChars(0, length, text, 0); int i = 0, j = 0; for (i = 0; i < length; i++) { if (text[i] != '&') text[j++] = text[i]; } if (j < i) string = new String(text, 0, j); } /* Use the character encoding for the default locale */ TCHAR buffer = new TCHAR(0, string, true); int byteCount = buffer.length() * TCHAR.sizeof; pszText = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount); OS.MoveMemory(pszText, buffer, byteCount); long /*int*/ hwndCB = parent.hwndCB; TBBUTTONINFO info2 = new TBBUTTONINFO(); info2.cbSize = TBBUTTONINFO.sizeof; info2.dwMask = OS.TBIF_TEXT; info2.pszText = pszText; success = OS.SendMessage(hwndCB, OS.TB_SETBUTTONINFO, id, info2) != 0; } else { MENUITEMINFO info = new MENUITEMINFO(); info.cbSize = MENUITEMINFO.sizeof; long /*int*/ hMenu = parent.handle; /* Use the character encoding for the default locale */ TCHAR buffer = new TCHAR(0, string, true); int byteCount = buffer.length() * TCHAR.sizeof; pszText = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, byteCount); OS.MoveMemory(pszText, buffer, byteCount); /* * Bug in Windows 2000. For some reason, when MIIM_TYPE is set * on a menu item that also has MIIM_BITMAP, the MIIM_TYPE clears * the MIIM_BITMAP style. The fix is to use MIIM_STRING. */ if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(4, 10)) { info.fMask = OS.MIIM_STRING; } else { info.fMask = OS.MIIM_TYPE; info.fType = widgetStyle(); } info.dwTypeData = pszText; success = OS.SetMenuItemInfo(hMenu, id, false, info); } if (pszText != 0) OS.HeapFree(hHeap, 0, pszText); if (!success) { int error = OS.GetLastError(); SWT.error( SWT.ERROR_CANNOT_SET_TEXT, null, " [GetLastError=0x" + Integer.toHexString(error) + "]"); // $NON-NLS-1$ $NON-NLS-2$ } parent.redraw(); }