?? toolitem.java
字號:
/******************************************************************************* * 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 represents a button in a tool bar. * <dl> * <dt><b>Styles:</b></dt> * <dd>PUSH, CHECK, RADIO, SEPARATOR, DROP_DOWN</dd> * <dt><b>Events:</b></dt> * <dd>Selection</dd> * </dl> * <p> * Note: Only one of the styles CHECK, PUSH, RADIO, SEPARATOR and DROP_DOWN * may be specified. * </p><p> * IMPORTANT: This class is <em>not</em> intended to be subclassed. * </p> */public class ToolItem extends Item { ToolBar parent; Control control; String toolTipText; Image disabledImage, hotImage; Image disabledImage2; int id;/** * Constructs a new instance of this class given its parent * (which must be a <code>ToolBar</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 composite 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#PUSH * @see SWT#CHECK * @see SWT#RADIO * @see SWT#SEPARATOR * @see SWT#DROP_DOWN * @see Widget#checkSubclass * @see Widget#getStyle */public ToolItem (ToolBar 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>ToolBar</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 composite 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#PUSH * @see SWT#CHECK * @see SWT#RADIO * @see SWT#SEPARATOR * @see SWT#DROP_DOWN * @see Widget#checkSubclass * @see Widget#getStyle */public ToolItem (ToolBar parent, int style, int index) { super (parent, checkStyle (style)); this.parent = parent; parent.createItem (this, index);}/** * 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 when the mouse is over the arrow portion of a drop-down tool, * the event object detail field contains the value <code>SWT.ARROW</code>. * <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);}static int checkStyle (int style) { return checkBits (style, SWT.PUSH, SWT.CHECK, SWT.RADIO, SWT.SEPARATOR, SWT.DROP_DOWN, 0);}protected void checkSubclass () { if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);}void click (boolean dropDown) { /* * In order to emulate all the processing that * happens when a mnemonic key is pressed, fake * a mouse press and release. This will ensure * that radio and pull down items are handled * properly. */ int hwnd = parent.handle; if (OS.GetKeyState (OS.VK_LBUTTON) < 0) return; int index = OS.SendMessage (hwnd, OS.TB_COMMANDTOINDEX, id, 0); RECT rect = new RECT (); OS.SendMessage (hwnd, OS.TB_GETITEMRECT, index, rect); int y = rect.top + (rect.bottom - rect.top) / 2; int lParam = (dropDown ? rect.right - 1 : rect.left) | (y << 16); int hotIndex = OS.SendMessage (hwnd, OS.TB_GETHOTITEM, 0, 0); OS.SendMessage (hwnd, OS.WM_LBUTTONDOWN, 0, lParam); OS.SendMessage (hwnd, OS.WM_LBUTTONUP, 0, lParam); if (hotIndex != -1) { OS.SendMessage (hwnd, OS.TB_SETHOTITEM, hotIndex, 0); }}Image createDisabledImage (Image image, Color color) { /* * In order to be consistent with the way that disabled * images appear in other places in the user interface, * use the SWT Graphics to create a disabled image instead * of calling DrawState(). */ return new Image (display, image, SWT.IMAGE_DISABLE); /* * This code is intentionally commented. */// if (OS.IsWinCE) {// return new Image (display, image, SWT.IMAGE_DISABLE);// }// Rectangle rect = image.getBounds ();// Image disabled = new Image (display, rect);// GC gc = new GC (disabled);// gc.setBackground (color);// gc.fillRectangle (rect);// int hDC = gc.handle;// int hImage = image.handle;// int fuFlags = OS.DSS_DISABLED;// switch (image.type) {// case SWT.BITMAP: fuFlags |= OS.DST_BITMAP; break;// case SWT.ICON: fuFlags |= OS.DST_ICON; break;// }// OS.DrawState (hDC, 0, 0, hImage, 0, 0, 0, rect.width, rect.height, fuFlags);// gc.dispose ();// return disabled;}/** * Returns a rectangle describing the receiver's size and location * relative to its parent. * * @return the receiver's bounding rectangle * * @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 Rectangle getBounds () { checkWidget(); int hwnd = parent.handle; int index = OS.SendMessage (hwnd, OS.TB_COMMANDTOINDEX, id, 0); RECT rect = new RECT (); OS.SendMessage (hwnd, OS.TB_GETITEMRECT, index, rect); int width = rect.right - rect.left; int height = rect.bottom - rect.top; return new Rectangle (rect.left, rect.top, width, height);}/** * Returns the control that is used to fill the bounds of * the item when the items is a <code>SEPARATOR</code>. * * @return the control * * @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 Control getControl () { checkWidget(); return control;}/** * Returns the receiver's disabled image if it has one, or null * if it does not. * <p> * The disabled image is displayed when the receiver is disabled. * </p> * * @return the receiver's disabled image * * @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 Image getDisabledImage () { checkWidget(); return disabledImage;}/** * 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(); int hwnd = parent.handle; int fsState = OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0); return (fsState & OS.TBSTATE_ENABLED) != 0;}/** * Returns the receiver's hot image if it has one, or null * if it does not. * <p> * The hot image is displayed when the mouse enters the receiver. * </p> * * @return the receiver's hot image * * @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 Image getHotImage () { checkWidget(); return hotImage;}/** * * @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 ToolBar 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 (which some platforms draw as a * pushed in button). If the receiver is of any other type, this method * returns false. * </p> * * @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; int hwnd = parent.handle; int fsState = OS.SendMessage (hwnd, OS.TB_GETSTATE, id, 0); return (fsState & OS.TBSTATE_CHECKED) != 0;}/** * Returns the receiver's tool tip text, or null if it has not been set. * * @return the receiver's tool tip text * * @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 String getToolTipText () { checkWidget(); return toolTipText;}/** * Gets the width of the receiver. * * @return the width * * @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 getWidth () { checkWidget(); int hwnd = parent.handle; int index = OS.SendMessage (hwnd, OS.TB_COMMANDTOINDEX, id, 0); RECT rect = new RECT (); OS.SendMessage (hwnd, OS.TB_GETITEMRECT, index, rect); return rect.right - rect.left;}/** * 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 () { checkWidget(); return getEnabled () && parent.isEnabled ();}void releaseChild () { super.releaseChild (); parent.destroyItem (this);}void releaseWidget () { super.releaseWidget (); parent = null; control = null;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -