⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menuitem.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html *  * Contributors: *     IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.widgets; import org.eclipse.swt.internal.win32.*;import org.eclipse.swt.*;import org.eclipse.swt.graphics.*;import org.eclipse.swt.events.*;/** * Instances of this class represent a selectable user interface object * that issues notification when pressed and released.  * <dl> * <dt><b>Styles:</b></dt> * <dd>CHECK, CASCADE, PUSH, RADIO, SEPARATOR</dd> * <dt><b>Events:</b></dt> * <dd>Arm, Help, Selection</dd> * </dl> * <p> * Note: Only one of the styles CHECK, CASCADE, PUSH, RADIO and SEPARATOR * may be specified. * </p><p> * IMPORTANT: This class is <em>not</em> intended to be subclassed. * </p> */public class MenuItem extends Item {	Menu parent, menu;	int id, accelerator;/** * 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. * </p> * * @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</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> * </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, parent.getItemCount ());}/** * 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. * </p> * * @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 index to store the receiver in its parent * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> * </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, index);}MenuItem (Menu parent, Menu menu, int style, int index) {	super (parent, checkStyle (style));	this.parent = parent;	this.menu = menu;		if (menu != null) menu.cascade = this;	display.addMenuItem (this);}/** * Adds the listener to the collection of listeners who will * be notified when the arm events are generated for the control, by sending * it one of the messages defined in the <code>ArmListener</code> * interface. * * @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 #removeArmListener */public void addArmListener (ArmListener listener) {	checkWidget ();	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);	TypedListener typedListener = new TypedListener (listener);	addListener (SWT.Arm, typedListener);}/** * Adds the listener to the collection of listeners who will * be notified when the help events are generated for the control, by sending * it one of the messages defined in the <code>HelpListener</code> * interface. * * @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 #removeHelpListener */public void addHelpListener (HelpListener listener) {	checkWidget ();	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);	TypedListener typedListener = new TypedListener (listener);	addListener (SWT.Help, typedListener);}/** * Adds the listener to the collection of listeners who will * be notified when the control is selected, by sending * it one of the messages defined in the <code>SelectionListener</code> * interface. * <p> * When <code>widgetSelected</code> is called, the stateMask field of the event object is valid. * <code>widgetDefaultSelected</code> is not called. * </p> * * @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 #removeSelectionListener * @see SelectionEvent */public void addSelectionListener (SelectionListener listener) {	checkWidget ();	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);	TypedListener typedListener = new TypedListener(listener);	addListener (SWT.Selection,typedListener);	addListener (SWT.DefaultSelection,typedListener);}protected void checkSubclass () {	if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);}static int checkStyle (int style) {	return checkBits (style, SWT.PUSH, SWT.CHECK, SWT.RADIO, SWT.SEPARATOR, SWT.CASCADE, 0);}void fillAccel (ACCEL accel) {	accel.fVirt = 0;	accel.cmd = accel.key = 0;	if (accelerator == 0 || !getEnabled ()) return;	int fVirt = OS.FVIRTKEY;	int key = accelerator & SWT.KEY_MASK;	int vKey = Display.untranslateKey (key);	if (vKey != 0) {		key = vKey;		} else {		switch (key) {			/*			* Bug in Windows.  For some reason, VkKeyScan			* fails to map ESC to VK_ESCAPE and DEL to			* VK_DELETE.  The fix is to map these keys			* as a special case.			*/			case 27: key = OS.VK_ESCAPE; break;			case 127: key = OS.VK_DELETE; break;			default: {				key = Display.wcsToMbcs ((char) key);				if (key == 0) return;				if (OS.IsWinCE) {					key = OS.CharUpper ((short) key);				} else {					vKey = OS.VkKeyScan ((short) key) & 0xFF;					if (vKey == -1) {						fVirt = 0;					} else {						key = vKey;					}				}			}		}	}	accel.key = (short) key;	accel.cmd = (short) id;	accel.fVirt = (byte) fVirt;	if ((accelerator & SWT.ALT) != 0) accel.fVirt |= OS.FALT;	if ((accelerator & SWT.SHIFT) != 0) accel.fVirt |= OS.FSHIFT;	if ((accelerator & SWT.CONTROL) != 0) accel.fVirt |= OS.FCONTROL;}void fixMenus (Decorations newParent) {	if (menu != null) menu.fixMenus (newParent);}/** * Return the widget accelerator.  An accelerator is the bit-wise * OR of zero or more modifier masks and a key. Examples: * <code>SWT.CONTROL | SWT.SHIFT | 'T', SWT.ALT | SWT.F2</code>. * * @return the accelerator * * </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 int getAccelerator () {	checkWidget ();	return accelerator;}/** * Returns <code>true</code> if the receiver is 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 #isEnabled */public boolean getEnabled () {	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);		return (info.fsState & OS.TBSTATE_ENABLED) != 0;	}	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 the receiver's cascade menu if it has one or null * if it does not. 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. * * @return the receiver's menu * * @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 getMenu () {	checkWidget ();	return menu;}String getNameText () {	if ((style & SWT.SEPARATOR) != 0) return "|";	return super.getNameText ();}/** * Returns the receiver's parent, which must be a <code>Menu</code>.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -