?? toolbar.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.*;/** * Instances of this class support the layout of selectable * tool bar items. * <p> * The item children that may be added to instances of this class * must be of type <code>ToolItem</code>. * </p><p> * Note that although this class is a subclass of <code>Composite</code>, * it does not make sense to add <code>Control</code> children to it, * or set a layout on it. * </p><p> * <dl> * <dt><b>Styles:</b></dt> * <dd>FLAT, WRAP, RIGHT, HORIZONTAL, VERTICAL, SHADOW_OUT</dd> * <dt><b>Events:</b></dt> * <dd>(none)</dd> * </dl> * <p> * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified. * </p><p> * IMPORTANT: This class is <em>not</em> intended to be subclassed. * </p> */public class ToolBar extends Composite { int lastFocusId; ToolItem [] items; boolean ignoreResize; ImageList imageList, disabledImageList, hotImageList; static final int ToolBarProc; static final TCHAR ToolBarClass = new TCHAR (0, OS.TOOLBARCLASSNAME, true); static { WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, ToolBarClass, lpWndClass); ToolBarProc = lpWndClass.lpfnWndProc; } /* * From the Windows SDK for TB_SETBUTTONSIZE: * * "If an application does not explicitly * set the button size, the size defaults * to 24 by 22 pixels". */ static final int DEFAULT_WIDTH = 24; static final int DEFAULT_HEIGHT = 22;/** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. * <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#FLAT * @see SWT#WRAP * @see SWT#RIGHT * @see SWT#HORIZONTAL * @see SWT#SHADOW_OUT * @see SWT#VERTICAL * @see Widget#checkSubclass() * @see Widget#getStyle() */public ToolBar (Composite parent, int style) { super (parent, checkStyle (style)); /* * Ensure that either of HORIZONTAL or VERTICAL is set. * NOTE: HORIZONTAL and VERTICAL have the same values * as H_SCROLL and V_SCROLL so it is necessary to first * clear these bits to avoid scroll bars and then reset * the bits using the original style supplied by the * programmer. */ if ((style & SWT.VERTICAL) != 0) { this.style |= SWT.VERTICAL; } else { this.style |= SWT.HORIZONTAL; }}int callWindowProc (int msg, int wParam, int lParam) { if (handle == 0) return 0; /* * Bug in Windows. For some reason, during the processing * of WM_SYSCHAR, the tool bar window proc does not call the * default window proc causing mnemonics for the menu bar * to be ignored. The fix is to always call the default * window proc for WM_SYSCHAR. */ if (msg == OS.WM_SYSCHAR) { return OS.DefWindowProc (handle, msg, wParam, lParam); } return OS.CallWindowProc (ToolBarProc, handle, msg, wParam, lParam);}static int checkStyle (int style) { /* * On Windows, only flat tool bars can be traversed. */ if ((style & SWT.FLAT) == 0) style |= SWT.NO_FOCUS; /* * A vertical tool bar cannot wrap because TB_SETROWS * fails when the toobar has TBSTYLE_WRAPABLE. */ if ((style & SWT.VERTICAL) != 0) style &= ~SWT.WRAP; /* * Even though it is legal to create this widget * with scroll bars, they serve no useful purpose * because they do not automatically scroll the * widget's client area. The fix is to clear * the SWT style. */ return style & ~(SWT.H_SCROLL | SWT.V_SCROLL);}protected void checkSubclass () { if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);}public Point computeSize (int wHint, int hHint, boolean changed) { checkWidget (); if (layout != null) { return super.computeSize (wHint, hHint, changed); } int width = 0, height = 0; if ((style & SWT.VERTICAL) != 0) { RECT rect = new RECT (); TBBUTTON lpButton = new TBBUTTON (); int count = OS.SendMessage (handle, OS.TB_BUTTONCOUNT, 0, 0); for (int i=0; i<count; i++) { OS.SendMessage (handle, OS.TB_GETITEMRECT, i, rect); height = Math.max (height, rect.bottom); OS.SendMessage (handle, OS.TB_GETBUTTON, i, lpButton); if ((lpButton.fsStyle & OS.BTNS_SEP) == 0) { width = Math.max (width, rect.right); } } } else { RECT oldRect = new RECT (); OS.GetWindowRect (handle, oldRect); int oldWidth = oldRect.right - oldRect.left; int oldHeight = oldRect.bottom - oldRect.top; int border = getBorderWidth (); int newWidth = wHint == SWT.DEFAULT ? 0x3FFF : wHint + border * 2; int newHeight = hHint == SWT.DEFAULT ? 0x3FFF : hHint + border * 2; boolean redraw = drawCount == 0 && OS.IsWindowVisible (handle); ignoreResize = true; if (redraw) OS.UpdateWindow (handle); int flags = OS.SWP_NOACTIVATE | OS.SWP_NOMOVE | OS.SWP_NOREDRAW | OS.SWP_NOZORDER; SetWindowPos (handle, 0, 0, 0, newWidth, newHeight, flags); int count = OS.SendMessage (handle, OS.TB_BUTTONCOUNT, 0, 0); if (count != 0) { RECT rect = new RECT (); OS.SendMessage (handle, OS.TB_GETITEMRECT, count - 1, rect); width = Math.max (width, rect.right); height = Math.max (height, rect.bottom); } SetWindowPos (handle, 0, 0, 0, oldWidth, oldHeight, flags); if (redraw) OS.ValidateRect (handle, null); ignoreResize = false; } /* * From the Windows SDK for TB_SETBUTTONSIZE: * * "If an application does not explicitly * set the button size, the size defaults * to 24 by 22 pixels". */ if (width == 0) width = DEFAULT_WIDTH; if (height == 0) height = DEFAULT_HEIGHT; if (wHint != SWT.DEFAULT) width = wHint; if (hHint != SWT.DEFAULT) height = hHint; Rectangle trim = computeTrim (0, 0, width, height); width = trim.width; height = trim.height; return new Point (width, height);}public Rectangle computeTrim (int x, int y, int width, int height) { checkWidget (); Rectangle trim = super.computeTrim (x, y, width, height); int bits = OS.GetWindowLong (handle, OS.GWL_STYLE); if ((bits & OS.CCS_NODIVIDER) == 0) trim.height += 2; return trim;}void createHandle () { super.createHandle (); state &= ~CANVAS; /* * Feature in Windows. Despite the fact that the * tool tip text contains \r\n, the tooltip will * not honour the new line unless TTM_SETMAXTIPWIDTH * is set. The fix is to set TTM_SETMAXTIPWIDTH to * a large value. */ /* * These lines are intentionally commented. The tool * bar currently sets this value to 300 so it is not * necessary to set TTM_SETMAXTIPWIDTH. */// int hwndToolTip = OS.SendMessage (handle, OS.TB_GETTOOLTIPS, 0, 0);// OS.SendMessage (hwndToolTip, OS.TTM_SETMAXTIPWIDTH, 0, 0x7FFF); /* * Feature in Windows. When the control is created, * it does not use the default system font. A new HFONT * is created and destroyed when the control is destroyed. * This means that a program that queries the font from * this control, uses the font in another control and then * destroys this control will have the font unexpectedly * destroyed in the other control. The fix is to assign * the font ourselves each time the control is created. * The control will not destroy a font that it did not * create. */ int hFont = OS.GetStockObject (OS.SYSTEM_FONT); OS.SendMessage (handle, OS.WM_SETFONT, hFont, 0); /* Set the button struct, bitmap and button sizes */ OS.SendMessage (handle, OS.TB_BUTTONSTRUCTSIZE, TBBUTTON.sizeof, 0); OS.SendMessage (handle, OS.TB_SETBITMAPSIZE, 0, 0); OS.SendMessage (handle, OS.TB_SETBUTTONSIZE, 0, 0); /* Set the extended style bits */ int bits = OS.TBSTYLE_EX_DRAWDDARROWS | OS.TBSTYLE_EX_MIXEDBUTTONS; OS.SendMessage (handle, OS.TB_SETEXTENDEDSTYLE, 0, bits);}void createItem (ToolItem item, int index) { int count = OS.SendMessage (handle, OS.TB_BUTTONCOUNT, 0, 0); if (!(0 <= index && index <= count)) error (SWT.ERROR_INVALID_RANGE); int id = 0; while (id < items.length && items [id] != null) id++; if (id == items.length) { ToolItem [] newItems = new ToolItem [items.length + 4]; System.arraycopy (items, 0, newItems, 0, items.length); items = newItems; } int bits = item.widgetStyle (); TBBUTTON lpButton = new TBBUTTON (); lpButton.idCommand = id; lpButton.fsStyle = (byte) bits; lpButton.fsState = (byte) OS.TBSTATE_ENABLED; /* * Bug in Windows. Despite the fact that the image list * index has never been set for the item, Windows always * assumes that the image index for the item is valid. * When an item is inserted, the image index is zero. * Therefore, when the first image is inserted and is * assigned image index zero, every item draws with this * image. The fix is to set the image index to none * when the item is created. This is not necessary in * the case when the item has the BTNS_SEP style because * separators cannot show images. */ if ((bits & OS.BTNS_SEP) == 0) lpButton.iBitmap = OS.I_IMAGENONE; if (OS.SendMessage (handle, OS.TB_INSERTBUTTON, index, lpButton) == 0) { error (SWT.ERROR_ITEM_NOT_ADDED); } items [item.id = id] = item; if ((style & SWT.VERTICAL) != 0) setRows (count + 1); layoutItems ();}void createWidget () { super.createWidget (); items = new ToolItem [4]; lastFocusId = -1;}int defaultBackground () { if (OS.IsWinCE) return OS.GetSysColor (OS.COLOR_BTNFACE); return super.defaultBackground ();}void destroyItem (ToolItem item) { TBBUTTONINFO info = new TBBUTTONINFO (); info.cbSize = TBBUTTONINFO.sizeof; info.dwMask = OS.TBIF_IMAGE | OS.TBIF_STYLE; int index = OS.SendMessage (handle, OS.TB_GETBUTTONINFO, item.id, info); /* * Feature in Windows. For some reason, a tool item that has * the style BTNS_SEP does not return I_IMAGENONE when queried * for an image index, despite the fact that no attempt has been * made to assign an image to the item. As a result, operations * on an image list that use the wrong index cause random results. * The fix is to ensure that the tool item is not a separator * before using the image index. Since separators cannot have * an image and one is never assigned, this is not a problem. */ if ((info.fsStyle & OS.BTNS_SEP) == 0 && info.iImage != OS.I_IMAGENONE) { if (imageList != null) imageList.put (info.iImage, null); if (hotImageList != null) hotImageList.put (info.iImage, null); if (disabledImageList != null) disabledImageList.put (info.iImage, null); } OS.SendMessage (handle, OS.TB_DELETEBUTTON, index, 0); if (item.id == lastFocusId) lastFocusId = -1; items [item.id] = null; item.id = -1; int count = OS.SendMessage (handle, OS.TB_BUTTONCOUNT, 0, 0); if (count == 0) { if (imageList != null) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -