📄 menuitem.java
字号:
* * @return the receiver's parent * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public Menu getParent () { checkWidget (); return parent;}/** * Returns <code>true</code> if the receiver is selected, * and false otherwise. * <p> * When the receiver is of type <code>CHECK</code> or <code>RADIO</code>, * it is selected when it is checked. * * @return the selection state * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public boolean getSelection () { checkWidget (); if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return false; if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) return false; int hMenu = parent.handle; 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_GET_SELECTION); return (info.fState & OS.MFS_CHECKED) !=0;}/** * 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 control 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> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see #getEnabled */public boolean isEnabled () { return getEnabled () && parent.isEnabled ();}void releaseChild () { super.releaseChild (); if (menu != null) menu.dispose (); menu = null; parent.destroyItem (this);}void releaseMenu () { if (!OS.IsSP) setMenu (null); menu = null;}void releaseWidget () { if (menu != null) menu.releaseResources (); menu = null; super.releaseWidget (); if (accelerator != 0) { parent.destroyAccelerators (); } accelerator = 0; display.removeMenuItem (this); parent = null;}/** * Removes the listener from the collection of listeners who will * be notified when the arm events are generated for the control. * * @param listener the listener which should be notified * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see ArmListener * @see #addArmListener */public void removeArmListener (ArmListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Arm, listener);}/** * Removes the listener from the collection of listeners who will * be notified when the help events are generated for the control. * * @param listener the listener which should be notified * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see HelpListener * @see #addHelpListener */public void removeHelpListener (HelpListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Help, listener);}/** * Removes the listener from the collection of listeners who will * be notified when the control is selected. * * @param listener the listener which should be notified * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see SelectionListener * @see #addSelectionListener */public void removeSelectionListener (SelectionListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Selection, listener); eventTable.unhook (SWT.DefaultSelection,listener); }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 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>. * * @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> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public void setAccelerator (int accelerator) { checkWidget (); if (this.accelerator == accelerator) return; this.accelerator = accelerator; parent.destroyAccelerators ();}/** * Enables the receiver if the argument is <code>true</code>, * and disables it otherwise. A disabled control 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> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public void setEnabled (boolean enabled) { checkWidget (); if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { 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 { 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) error (SWT.ERROR_CANNOT_SET_ENABLED); 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) error (SWT.ERROR_CANNOT_SET_ENABLED); } } parent.destroyAccelerators (); parent.redraw ();}/** * Sets the image the receiver will display to the argument. * <p> * Note: This feature is not available on all window systems (for example, Window NT), * in which case, calling this method will silently do nothing. * * @param image the image to display * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </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) { 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_MAJOR << 16 | OS.WIN32_MINOR) < (4 << 16 | 10)) { return; } MENUITEMINFO info = new MENUITEMINFO (); info.cbSize = MENUITEMINFO.sizeof; info.fMask = OS.MIIM_BITMAP; if (image != null) info.hbmpItem = OS.HBMMENU_CALLBACK; int hMenu = parent.handle; OS.SetMenuItemInfo (hMenu, id, false, info); parent.redraw ();}/** * 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. * * @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> * <li>ERROR_MENUITEM_NOT_CASCADE - if the menu item is not a <code>CASCADE</code></li> * <li>ERROR_INVALID_ARGUMENT - if the menu has been disposed</li> * <li>ERROR_INVALID_PARENT - if the menu is not in the same widget tree</li> * </ul> * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </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); } } /* 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) { int hwndCB = parent.hwndCB; 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 { /* * 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 (). */ 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; boolean hasBitmap = false, success = false; /*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -