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

📄 combo.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/******************************************************************************* * 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 are controls that allow the user * to choose an item from a list of items, or optionally  * enter a new value by typing it into an editable text * field. Often, <code>Combo</code>s are used in the same place * where a single selection <code>List</code> widget could * be used but space is limited. A <code>Combo</code> takes * less space than a <code>List</code> widget and shows * similar information. * <p> * Note: Since <code>Combo</code>s can contain both a list * and an editable text field, it is possible to confuse methods * which access one versus the other (compare for example, * <code>clearSelection()</code> and <code>deselectAll()</code>). * The API documentation is careful to indicate either "the * receiver's list" or the "the receiver's text field" to  * distinguish between the two cases. * </p><p> * Note that although this class is a subclass of <code>Composite</code>, * it does not make sense to add children to it, or set a layout on it. * </p> * <dl> * <dt><b>Styles:</b></dt> * <dd>DROP_DOWN, READ_ONLY, SIMPLE</dd> * <dt><b>Events:</b></dt> * <dd>DefaultSelection, Modify, Selection</dd> * </dl> * <p> * Note: Only one of the styles DROP_DOWN and SIMPLE  * may be specified. * </p><p> * IMPORTANT: This class is <em>not</em> intended to be subclassed. * </p> * * @see List */public class Combo extends Composite {	boolean noSelection, ignoreCharacter;	int visibleCount = 5;		/**	 * the operating system limit for the number of characters	 * that the text field in an instance of this class can hold	 */	public static final int LIMIT;		/*	 * These values can be different on different platforms.	 * Therefore they are not initialized in the declaration	 * to stop the compiler from inlining.	 */	static {		LIMIT = OS.IsWinNT ? 0x7FFFFFFF : 0x7FFF;		}		/*	 * These are the undocumented control id's for the children of	 * a combo box.  Since there are no constants for these values,	 * they may change with different versions of Windows (but have	 * been the same since Windows 3.0).	 */	static final int CBID_LIST = 1000;	static final int CBID_EDIT = 1001;	static int EditProc, ListProc;		static final int ComboProc;	static final TCHAR ComboClass = new TCHAR (0, "COMBOBOX", true);	static {		WNDCLASS lpWndClass = new WNDCLASS ();		OS.GetClassInfo (0, ComboClass, lpWndClass);		ComboProc = lpWndClass.lpfnWndProc;	}	/** * 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#DROP_DOWN * @see SWT#READ_ONLY * @see SWT#SIMPLE * @see Widget#checkSubclass * @see Widget#getStyle */public Combo (Composite parent, int style) {	super (parent, checkStyle (style));}/** * Adds the argument to the end of the receiver's list. * * @param string the new item * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the string 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> * @exception SWTError <ul> *    <li>ERROR_ITEM_NOT_ADDED - if the operation fails because of an operating system failure</li> * </ul> * * @see #add(String,int) */public void add (String string) {	checkWidget ();	if (string == null) error (SWT.ERROR_NULL_ARGUMENT);	TCHAR buffer = new TCHAR (getCodePage (), string, true);	int result = OS.SendMessage (handle, OS.CB_ADDSTRING, 0, buffer);	if (result == OS.CB_ERR) error (SWT.ERROR_ITEM_NOT_ADDED);	if (result == OS.CB_ERRSPACE) error (SWT.ERROR_ITEM_NOT_ADDED);}/** * Adds the argument to the receiver's list at the given * zero-relative index. * <p> * Note: To add an item at the end of the list, use the * result of calling <code>getItemCount()</code> as the * index or use <code>add(String)</code>. * </p> * * @param string the new item * @param index the index for the item * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the string is null</li> *    <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list (inclusive)</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> * @exception SWTError <ul> *    <li>ERROR_ITEM_NOT_ADDED - if the operation fails because of an operating system failure</li> * </ul> * * @see #add(String) */public void add (String string, int index) {	checkWidget ();	if (string == null) error (SWT.ERROR_NULL_ARGUMENT);	int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0);	if (!(0 <= index && index <= count)) {		error (SWT.ERROR_INVALID_RANGE);	}	TCHAR buffer = new TCHAR (getCodePage (), string, true);	int result = OS.SendMessage (handle, OS.CB_INSERTSTRING, index, buffer);	if (result == OS.CB_ERRSPACE || result == OS.CB_ERR) {		error (SWT.ERROR_ITEM_NOT_ADDED);	}}/** * Adds the listener to the collection of listeners who will * be notified when the receiver's text is modified, by sending * it one of the messages defined in the <code>ModifyListener</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 ModifyListener * @see #removeModifyListener */public void addModifyListener (ModifyListener listener) {	checkWidget ();	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);	TypedListener typedListener = new TypedListener (listener);	addListener (SWT.Modify, typedListener);}/** * Adds the listener to the collection of listeners who will * be notified when the receiver's selection changes, by sending * it one of the messages defined in the <code>SelectionListener</code> * interface. * <p> * <code>widgetSelected</code> is called when the combo's list selection changes. * <code>widgetDefaultSelected</code> is typically called when ENTER is pressed the combo's text area. * </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);}int callWindowProc (int msg, int wParam, int lParam) {	if (handle == 0) return 0;	return OS.CallWindowProc (ComboProc, handle, msg, wParam, lParam);}protected void checkSubclass () {	if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);}static int checkStyle (int style) {	/*	 * Feature in Windows.  It is not possible to create	 * a combo box that has a border using Windows style	 * bits.  All combo boxes draw their own border and	 * do not use the standard Windows border styles.	 * Therefore, no matter what style bits are specified,	 * clear the BORDER bits so that the SWT style will	 * match the Windows widget.	 *	 * The Windows behavior is currently implemented on	 * all platforms.	 */	style &= ~SWT.BORDER;		/*	 * 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.	 */	style &= ~(SWT.H_SCROLL | SWT.V_SCROLL);	style = checkBits (style, SWT.DROP_DOWN, SWT.SIMPLE, 0, 0, 0, 0);	if ((style & SWT.SIMPLE) != 0) return style & ~SWT.READ_ONLY;	return style;}/** * Sets the selection in the receiver's text field to an empty * selection starting just before the first character. If the * text field is editable, this has the effect of placing the * i-beam at the start of the text. * <p> * Note: To clear the selected items in the receiver's list,  * use <code>deselectAll()</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> * * @see #deselectAll */public void clearSelection () {	checkWidget ();	OS.SendMessage (handle, OS.CB_SETEDITSEL, 0, -1);}public Point computeSize (int wHint, int hHint, boolean changed) {	checkWidget ();	int width = 0, height = 0, tmInternalLeading = 0;	if (wHint == SWT.DEFAULT) {		int newFont, oldFont = 0;		int hDC = OS.GetDC (handle);		newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);		if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);		int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0);		RECT rect = new RECT ();		int flags = OS.DT_CALCRECT | OS.DT_NOPREFIX;		if ((style & SWT.READ_ONLY) == 0) flags |= OS.DT_EDITCONTROL;		int length = OS.GetWindowTextLength (handle);		int cp = getCodePage ();		TCHAR buffer = new TCHAR (cp, length + 1);		OS.GetWindowText (handle, buffer, length + 1);		OS.DrawText (hDC, buffer, length, rect, flags);		width = Math.max (width, rect.right - rect.left);		for (int i=0; i<count; i++) {			length = OS.SendMessage (handle, OS.CB_GETLBTEXTLEN, i, 0);			if (length != OS.CB_ERR) {				if (length + 1 > buffer.length ()) buffer = new TCHAR (cp, length + 1);				int result = OS.SendMessage (handle, OS.CB_GETLBTEXT, i, buffer);				if (result != OS.CB_ERR) {					OS.DrawText (hDC, buffer, length, rect, flags);					width = Math.max (width, rect.right - rect.left);				}			}		}		if ((style & SWT.READ_ONLY) != 0) {			TEXTMETRIC tm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW () : new TEXTMETRICA ();			OS.GetTextMetrics (hDC, tm);			tmInternalLeading = tm.tmInternalLeading;		}		if (newFont != 0) OS.SelectObject (hDC, oldFont);		OS.ReleaseDC (handle, hDC);	}	if (hHint == SWT.DEFAULT) {		if ((style & SWT.SIMPLE) != 0) {			int count = OS.SendMessage (handle, OS.CB_GETCOUNT, 0, 0);			int itemHeight = OS.SendMessage (handle, OS.CB_GETITEMHEIGHT, 0, 0);			height = count * itemHeight;		}	}		if (width == 0) width = DEFAULT_WIDTH;	if (height == 0) height = DEFAULT_HEIGHT;	if (wHint != SWT.DEFAULT) width = wHint;	if (hHint != SWT.DEFAULT) height = hHint;	if ((style & SWT.READ_ONLY) != 0) {		width += tmInternalLeading * 2;	} else {		int hwndText = OS.GetDlgItem (handle, CBID_EDIT);		if (hwndText != 0) {			int margins = OS.SendMessage (hwndText, OS.EM_GETMARGINS, 0, 0);			int marginWidth = (margins & 0xFFFF) + ((margins >> 16) & 0xFFFF);			width += marginWidth + 3;		}	}	int border = OS.GetSystemMetrics (OS.SM_CXEDGE);	width += OS.GetSystemMetrics (OS.SM_CXVSCROLL) + border * 2;	int textHeight = OS.SendMessage (handle, OS.CB_GETITEMHEIGHT, -1, 0);	if ((style & SWT.DROP_DOWN) != 0) {		height = textHeight + 6;	} else {		height += textHeight + 10;	}	return new Point (width, height);}/** * Copies the selected text. * <p> * The current selection is copied to the clipboard. * </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> *  * @since 2.1 */public void copy () {	checkWidget ();	OS.SendMessage (handle, OS.WM_COPY, 0, 0);}void createHandle () {	super.createHandle ();	state &= ~CANVAS;	/* Get the text and list window procs */	int hwndText = OS.GetDlgItem (handle, CBID_EDIT);	if (hwndText != 0 && EditProc == 0) {		EditProc = OS.GetWindowLong (hwndText, OS.GWL_WNDPROC);	}	int hwndList = OS.GetDlgItem (handle, CBID_LIST);	if (hwndList != 0 && ListProc == 0) {		ListProc = OS.GetWindowLong (hwndList, OS.GWL_WNDPROC);	}	/*	* Bug in Windows.  If the combo box has the CBS_SIMPLE style,	* the list portion of the combo box is not drawn correctly the	* first time, causing pixel corruption.  The fix is to ensure	* that the combo box has been resized more than once.	*/	if ((style & SWT.SIMPLE) != 0) {		int flags = OS.SWP_NOZORDER | OS.SWP_DRAWFRAME | OS.SWP_NOACTIVATE;		SetWindowPos (handle, 0, 0, 0, 0x3FFF, 0x3FFF, flags);		SetWindowPos (handle, 0, 0, 0, 0, 0, flags);	}}/**

⌨️ 快捷键说明

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