?? composite.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.*;import org.eclipse.swt.internal.win32.*;import org.eclipse.swt.*;import org.eclipse.swt.graphics.*;/** * Instances of this class are controls which are capable * of containing other controls. * <dl> * <dt><b>Styles:</b></dt> * <dd>NO_BACKGROUND, NO_FOCUS, NO_MERGE_PAINTS, NO_REDRAW_RESIZE, NO_RADIO_GROUP, EMBEDDED</dd> * <dt><b>Events:</b></dt> * <dd>(none)</dd> * </dl> * <p> * Note: The <code>NO_BACKGROUND</code>, <code>NO_FOCUS</code>, <code>NO_MERGE_PAINTS</code>, * and <code>NO_REDRAW_RESIZE</code> styles are intended for use with <code>Canvas</code>. * They can be used with <code>Composite</code> if you are drawing your own, but their * behavior is undefined if they are used with subclasses of <code>Composite</code> other * than <code>Canvas</code>. * </p><p> * This class may be subclassed by custom control implementors * who are building controls that are constructed from aggregates * of other controls. * </p> * * @see Canvas */public class Composite extends Scrollable { Layout layout; int font; WINDOWPOS [] lpwp; Control [] tabList; /** * Prevents uninitialized instances from being created outside the package. */Composite () {}/** * 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 widget which will be the parent of the new instance (cannot be null) * @param style the style of widget 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> * </ul> * * @see SWT#NO_BACKGROUND * @see SWT#NO_FOCUS * @see SWT#NO_MERGE_PAINTS * @see SWT#NO_REDRAW_RESIZE * @see SWT#NO_RADIO_GROUP * @see Widget#getStyle */public Composite (Composite parent, int style) { super (parent, style);}Control [] _getChildren () { int count = 0; int hwndChild = OS.GetWindow (handle, OS.GW_CHILD); if (hwndChild == 0) return new Control [0]; while (hwndChild != 0) { count++; hwndChild = OS.GetWindow (hwndChild, OS.GW_HWNDNEXT); } Control [] children = new Control [count]; int index = 0; hwndChild = OS.GetWindow (handle, OS.GW_CHILD); while (hwndChild != 0) { Control control = display.getControl (hwndChild); if (control != null && control != this) { children [index++] = control; } hwndChild = OS.GetWindow (hwndChild, OS.GW_HWNDNEXT); } if (count == index) return children; Control [] newChildren = new Control [index]; System.arraycopy (children, 0, newChildren, 0, index); return newChildren;}Control [] _getTabList () { if (tabList == null) return tabList; int count = 0; for (int i=0; i<tabList.length; i++) { if (!tabList [i].isDisposed ()) count++; } if (count == tabList.length) return tabList; Control [] newList = new Control [count]; int index = 0; for (int i=0; i<tabList.length; i++) { if (!tabList [i].isDisposed ()) { newList [index++] = tabList [i]; } } tabList = newList; return tabList;}protected void checkSubclass () { /* Do nothing - Subclassing is allowed */}Control [] computeTabList () { Control result [] = super.computeTabList (); if (result.length == 0) return result; Control [] list = tabList != null ? _getTabList () : _getChildren (); for (int i=0; i<list.length; i++) { Control child = list [i]; Control [] childList = child.computeTabList (); if (childList.length != 0) { Control [] newResult = new Control [result.length + childList.length]; System.arraycopy (result, 0, newResult, 0, result.length); System.arraycopy (childList, 0, newResult, result.length, childList.length); result = newResult; } } return result;}public Point computeSize (int wHint, int hHint, boolean changed) { checkWidget (); Point size; if (layout != null) { if (wHint == SWT.DEFAULT || hHint == SWT.DEFAULT) { size = layout.computeSize (this, wHint, hHint, changed); } else { size = new Point (wHint, hHint); } } else { size = minimumSize (); } if (size.x == 0) size.x = DEFAULT_WIDTH; if (size.y == 0) size.y = DEFAULT_HEIGHT; if (wHint != SWT.DEFAULT) size.x = wHint; if (hHint != SWT.DEFAULT) size.y = hHint; Rectangle trim = computeTrim (0, 0, size.x, size.y); return new Point (trim.width, trim.height);}void createHandle () { super.createHandle (); state |= CANVAS;}Menu [] findMenus (Control control) { if (control == this) return new Menu [0]; Menu result [] = super.findMenus (control); Control [] children = _getChildren (); for (int i=0; i<children.length; i++) { Control child = children [i]; Menu [] menuList = child.findMenus (control); if (menuList.length != 0) { Menu [] newResult = new Menu [result.length + menuList.length]; System.arraycopy (result, 0, newResult, 0, result.length); System.arraycopy (menuList, 0, newResult, result.length, menuList.length); result = newResult; } } return result;}void fixChildren (Shell newShell, Shell oldShell, Decorations newDecorations, Decorations oldDecorations, Menu [] menus) { super.fixChildren (newShell, oldShell, newDecorations, oldDecorations, menus); Control [] children = _getChildren (); for (int i=0; i<children.length; i++) { children [i].fixChildren (newShell, oldShell, newDecorations, oldDecorations, menus); }}void fixTabList (Control control) { if (tabList == null) return; int count = 0; for (int i=0; i<tabList.length; i++) { if (tabList [i] == control) count++; } if (count == 0) return; Control [] newList = null; int length = tabList.length - count; if (length != 0) { newList = new Control [length]; int index = 0; for (int i=0; i<tabList.length; i++) { if (tabList [i] != control) { newList [index++] = tabList [i]; } } } tabList = newList;}/** * Returns an array containing the receiver's children. * <p> * Note: This is not the actual structure used by the receiver * to maintain its list of children, so modifying the array will * not affect the receiver. * </p> * * @return an array of children * * @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 [] getChildren () { checkWidget (); return _getChildren ();}int getChildrenCount () { /* * NOTE: The current implementation will count * non-registered children. */ int count = 0; int hwndChild = OS.GetWindow (handle, OS.GW_CHILD); while (hwndChild != 0) { count++; hwndChild = OS.GetWindow (hwndChild, OS.GW_HWNDNEXT); } return count;}/** * Returns layout which is associated with the receiver, or * null if one has not been set. * * @return the receiver's layout or null * * @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 Layout getLayout () { checkWidget (); return layout;}/** * Gets the last specified tabbing order for the control. * * @return tabList the ordered list of controls representing the tab order * * @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 #setTabList */public Control [] getTabList () { checkWidget (); Control [] tabList = _getTabList (); if (tabList == null) { int count = 0; Control [] list =_getChildren (); for (int i=0; i<list.length; i++) { if (list [i].isTabGroup ()) count++; } tabList = new Control [count]; int index = 0; for (int i=0; i<list.length; i++) { if (list [i].isTabGroup ()) { tabList [index++] = list [i]; } } } return tabList;}boolean hooksKeys () { return hooks (SWT.KeyDown) || hooks (SWT.KeyUp);}/** * If the receiver has a layout, asks the layout to <em>lay out</em> * (that is, set the size and location of) the receiver's children. * If the receiver does not have a layout, do nothing. * <p> * This is equivalent to calling <code>layout(true)</code>. * </p> * * @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 layout () { checkWidget (); layout (true);}/** * If the receiver has a layout, asks the layout to <em>lay out</em> * (that is, set the size and location of) the receiver's children. * If the the argument is <code>true</code> the layout must not rely * on any cached information it is keeping about the children. If it * is <code>false</code> the layout may (potentially) simplify the * work it is doing by assuming that the state of the none of the * receiver's children has changed since the last layout. * If the receiver does not have a layout, do nothing. * * @param changed <code>true</code> if the layout must flush its caches, and <code>false</code> otherwise * * @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 layout (boolean changed) { checkWidget (); if (layout == null) return; setResizeChildren (false); layout.layout (this, changed); setResizeChildren (true);}Point minimumSize () { Control [] children = _getChildren (); int width = 0, height = 0; for (int i=0; i<children.length; i++) { Rectangle rect = children [i].getBounds (); width = Math.max (width, rect.x + rect.width); height = Math.max (height, rect.y + rect.height); } return new Point (width, height);}void releaseChildren () { Control [] children = _getChildren (); for (int i=0; i<children.length; i++) { Control child = children [i]; if (!child.isDisposed ()) child.releaseResources (); }}void releaseWidget () { releaseChildren (); super.releaseWidget (); if ((state & CANVAS) != 0 && (style & SWT.EMBEDDED) != 0) { int hwndChild = OS.GetWindow (handle, OS.GW_CHILD); if (hwndChild != 0) { int threadId = OS.GetWindowThreadProcessId (hwndChild, null); if (threadId != OS.GetCurrentThreadId ()) { OS.SetParent (hwndChild, 0); } } } layout = null; tabList = null; lpwp = null;}void removeControl (Control control) { fixTabList (control); resizeChildren ();}void resizeChildren () { if (lpwp == null) return; do { WINDOWPOS [] currentLpwp = lpwp; lpwp = null; if (!resizeChildren (true, currentLpwp)) { resizeChildren (false, currentLpwp); } } while (lpwp != null);}boolean resizeChildren (boolean defer, WINDOWPOS [] pwp) { if (pwp == null) return true; int hdwp = 0; if (defer) { hdwp = OS.BeginDeferWindowPos (pwp.length); if (hdwp == 0) return false; } for (int i=0; i<pwp.length; i++) { WINDOWPOS wp = pwp [i]; if (wp != null) { /* * This code is intentionally commented. All widgets that * are created by SWT have WS_CLIPSIBLINGS to ensure that * application code does not draw outside of the control. */// int count = parent.getChildrenCount ();// if (count > 1) {// int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);// if ((bits & OS.WS_CLIPSIBLINGS) == 0) wp.flags |= OS.SWP_NOCOPYBITS;// } if (defer) { hdwp = DeferWindowPos (hdwp, wp.hwnd, 0, wp.x, wp.y, wp.cx, wp.cy, wp.flags); if (hdwp == 0) return false; } else { SetWindowPos (wp.hwnd, 0, wp.x, wp.y, wp.cx, wp.cy, wp.flags); } } } if (defer) return OS.EndDeferWindowPos (hdwp); return true;}public boolean setFocus () { checkWidget (); Control [] children = _getChildren (); for (int i=0; i<children.length; i++) { Control child = children [i]; if (child.setRadioFocus ()) return true; } for (int i=0; i<children.length; i++) { Control child = children [i]; if (child.setFocus ()) return true; } return super.setFocus ();}/** * Sets the layout which is associated with the receiver to be * the argument which may be null. * * @param layout the receiver's new layout or null * * @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 setLayout (Layout layout) { checkWidget (); this.layout = layout;}/** * Sets the tabbing order for the specified controls to * match the order that they occur in the argument list. * * @param tabList the ordered list of controls representing the tab order or null * * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_ARGUMENT - if a widget in the tabList is null or has been disposed</li> * <li>ERROR_INVALID_PARENT - if widget in the tabList 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 setTabList (Control [] tabList) { checkWidget (); if (tabList != null) { for (int i=0; i<tabList.length; i++) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -