📄 coolbar.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 provide an area for dynamically * positioning the items they contain. * <p> * The item children that may be added to instances of this class * must be of type <code>CoolItem</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</dd> * <dt><b>Events:</b></dt> * <dd>(none)</dd> * </dl> * <p> * IMPORTANT: This class is <em>not</em> intended to be subclassed. * </p> */public class CoolBar extends Composite { CoolItem [] items; CoolItem [] originalItems; boolean locked; boolean ignoreResize; static final int ReBarProc; static final TCHAR ReBarClass = new TCHAR (0, OS.REBARCLASSNAME, true); static { INITCOMMONCONTROLSEX icex = new INITCOMMONCONTROLSEX (); icex.dwSize = INITCOMMONCONTROLSEX.sizeof; icex.dwICC = OS.ICC_COOL_CLASSES; OS.InitCommonControlsEx (icex); WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, ReBarClass, lpWndClass); ReBarProc = lpWndClass.lpfnWndProc; } static final int SEPARATOR_WIDTH = 2; static final int MAX_WIDTH = 0x7FFF;/** * 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 * @see Widget#checkSubclass * @see Widget#getStyle */public CoolBar (Composite parent, int style) { super (parent, checkStyle (style));}int callWindowProc (int msg, int wParam, int lParam) { if (handle == 0) return 0; return OS.CallWindowProc (ReBarProc, handle, msg, wParam, lParam);}static int checkStyle (int style) { style |= SWT.NO_FOCUS; /* * 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 (); int width = 0, height = 0; int border = getBorderWidth (); int newWidth = wHint == SWT.DEFAULT ? 0x3FFF : wHint + (border * 2); int newHeight = hHint == SWT.DEFAULT ? 0x3FFF : hHint + (border * 2); int count = OS.SendMessage (handle, OS.RB_GETBANDCOUNT, 0, 0); if (count != 0) { ignoreResize = true; boolean redraw = false; if (OS.IsWindowVisible (handle)) { if (OS.COMCTL32_MAJOR >= 6) { redraw = true; OS.UpdateWindow (handle); OS.DefWindowProc (handle, OS.WM_SETREDRAW, 0, 0); } else { redraw = drawCount == 0; if (redraw) { OS.UpdateWindow (handle); OS.SendMessage (handle, OS.WM_SETREDRAW, 0, 0); } } } RECT oldRect = new RECT (); OS.GetWindowRect (handle, oldRect); int oldWidth = oldRect.right - oldRect.left; int oldHeight = oldRect.bottom - oldRect.top; int flags = OS.SWP_NOACTIVATE | OS.SWP_NOMOVE | OS.SWP_NOREDRAW | OS.SWP_NOZORDER; SetWindowPos (handle, 0, 0, 0, newWidth, newHeight, flags); RECT rect = new RECT (); OS.SendMessage (handle, OS.RB_GETRECT, count - 1, rect); height = Math.max (height, rect.bottom); SetWindowPos (handle, 0, 0, 0, oldWidth, oldHeight, flags); REBARBANDINFO rbBand = new REBARBANDINFO (); rbBand.cbSize = REBARBANDINFO.sizeof; rbBand.fMask = OS.RBBIM_IDEALSIZE | OS.RBBIM_STYLE; int rowWidth = 0; int separator = (style & SWT.FLAT) == 0 ? SEPARATOR_WIDTH : 0; for (int i = 0; i < count; i++) { OS.SendMessage(handle, OS.RB_GETBANDINFO, i, rbBand); if ((rbBand.fStyle & OS.RBBS_BREAK) != 0) { width = Math.max(width, rowWidth - separator); rowWidth = 0; } rowWidth += rbBand.cxIdeal + getMargin (i) + separator; } width = Math.max(width, rowWidth - separator); if (redraw) { if (OS.COMCTL32_MAJOR >= 6) { OS.DefWindowProc (handle, OS.WM_SETREDRAW, 1, 0); } else { OS.SendMessage (handle, OS.WM_SETREDRAW, 1, 0); } } ignoreResize = false; } if (width == 0) width = DEFAULT_WIDTH; if (height == 0) height = DEFAULT_HEIGHT; if (wHint != SWT.DEFAULT) width = wHint; if (hHint != SWT.DEFAULT) height = hHint; height += border * 2; width += border * 2; return new Point (width, height);}void createHandle () { super.createHandle (); state &= ~CANVAS; /* * 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);}void createItem (CoolItem item, int index) { int count = OS.SendMessage (handle, OS.RB_GETBANDCOUNT, 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) { CoolItem [] newItems = new CoolItem [items.length + 4]; System.arraycopy (items, 0, newItems, 0, items.length); items = newItems; } int hHeap = OS.GetProcessHeap (); int lpText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, TCHAR.sizeof); REBARBANDINFO rbBand = new REBARBANDINFO (); rbBand.cbSize = REBARBANDINFO.sizeof; rbBand.fMask = OS.RBBIM_TEXT | OS.RBBIM_STYLE | OS.RBBIM_ID; rbBand.fStyle = OS.RBBS_VARIABLEHEIGHT | OS.RBBS_GRIPPERALWAYS; if ((item.style & SWT.DROP_DOWN) != 0) { rbBand.fStyle |= OS.RBBS_USECHEVRON; } rbBand.lpText = lpText; rbBand.wID = id; /* * Feature in Windows. When inserting an item at end of a row, * sometimes, Windows will begin to place the item on the right * side of the cool bar. The fix is to resize the new items to * the maximum size and then resize the next to last item to the * ideal size. */ int lastIndex = getLastIndexOfRow (index - 1); boolean fixLast = index == lastIndex + 1; if (fixLast) { rbBand.fMask |= OS.RBBIM_SIZE; rbBand.cx = MAX_WIDTH; } /* * Feature in Windows. Is possible that the item at index zero * has the RBBS_BREAK flag set. When a new item is inserted at * position zero, the previous item at position zero moves to * a new line. The fix is to detect this case and clear the * RBBS_BREAK flag on the previous item before inserting the * new item. */ if (index == 0 && count > 0) { getItem (0).setWrap (false); } /* Insert the item */ if (OS.SendMessage (handle, OS.RB_INSERTBAND, index, rbBand) == 0) { error (SWT.ERROR_ITEM_NOT_ADDED); } /* Resize the next to last item to the ideal size */ if (fixLast) { resizeToPreferredWidth (lastIndex); } OS.HeapFree (hHeap, 0, lpText); items [item.id = id] = item; int length = originalItems.length; CoolItem [] newOriginals = new CoolItem [length + 1]; System.arraycopy (originalItems, 0, newOriginals, 0, index); System.arraycopy (originalItems, index, newOriginals, index + 1, length - index); newOriginals [index] = item; originalItems = newOriginals;}void createWidget () { super.createWidget (); items = new CoolItem [4]; originalItems = new CoolItem [0];}void destroyItem (CoolItem item) { int index = OS.SendMessage (handle, OS.RB_IDTOINDEX, item.id, 0); int count = OS.SendMessage (handle, OS.RB_GETBANDCOUNT, 0, 0); if (count != 0) { int lastIndex = getLastIndexOfRow (index); if (index == lastIndex) { /* * Feature in Windows. If the last item in a row is * given its ideal size, it will be placed at the far * right hand edge of the coolbar. It is preferred * that the last item appear next to the second last * item. The fix is to size the last item of each row * so that it occupies all the available space to the * right in the row. */ resizeToMaximumWidth (lastIndex - 1); } } /* * Feature in Windows. When Windows removed a rebar * band, it makes the band child invisible. The fix * is to show the child. */ Control control = item.control; boolean wasVisible = control != null && !control.isDisposed() && control.getVisible (); /* * When a wrapped item is being deleted, make the next * item in the row wrapped in order to preserve the row. * In order to avoid an unnecessary layout, temporarily * ignore WM_SIZE. If the next item is wrapped then a * row will be deleted and the WM_SIZE is necessary. */ CoolItem nextItem = null; if (item.getWrap ()) { if (index + 1 < count) { nextItem = getItem (index + 1); ignoreResize = !nextItem.getWrap (); } } if (OS.SendMessage (handle, OS.RB_DELETEBAND, index, 0) == 0) { error (SWT.ERROR_ITEM_NOT_REMOVED); } items [item.id] = null; item.id = -1; if (ignoreResize) { nextItem.setWrap (true); ignoreResize = false; } /* Restore the visible state tof the control */ if (wasVisible) control.setVisible (true); index = 0; while (index < originalItems.length) { if (originalItems [index] == item) break; index++; } int length = originalItems.length - 1; CoolItem [] newOriginals = new CoolItem [length]; System.arraycopy (originalItems, 0, newOriginals, 0, index); System.arraycopy (originalItems, index + 1, newOriginals, index, length - index); originalItems = newOriginals;}int getMargin (int index) { int margin = 0; if (OS.COMCTL32_MAJOR >= 6) { MARGINS margins = new MARGINS (); OS.SendMessage (handle, OS.RB_GETBANDMARGINS, 0, margins); margin += margins.cxLeftWidth + margins.cxRightWidth; } RECT rect = new RECT (); OS.SendMessage (handle, OS.RB_GETBANDBORDERS, index, rect); if ((style & SWT.FLAT) != 0) { /* * Bug in Windows. When the style bit RBS_BANDBORDERS is not set * the rectangle returned by RBS_BANDBORDERS is four pixels too small. * The fix is to add four pixels to the result. */ margin += rect.left + 4; } else { margin += rect.left + rect.right; } return margin;}/** * Returns the item that is currently displayed at the given, * zero-relative index. Throws an exception if the index is * out of range. * * @param index the visual index of the item to return * @return the item at the given visual index
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -