?? text.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 are selectable user interface * objects that allow the user to enter and modify text. * <p> * <dl> * <dt><b>Styles:</b></dt> * <dd>CENTER, LEFT, MULTI, PASSWORD, SINGLE, RIGHT, READ_ONLY, WRAP</dd> * <dt><b>Events:</b></dt> * <dd>DefaultSelection, Modify, Verify</dd> * </dl> * <p> * Note: Only one of the styles MULTI and SINGLE may be specified. * </p><p> * IMPORTANT: This class is <em>not</em> intended to be subclassed. * </p> */public class Text extends Scrollable { int tabs, oldStart, oldEnd; boolean doubleClick, ignoreModify, ignoreVerify, ignoreCharacter; /** * The maximum number of characters that can be entered * into a text widget. */ public static final int LIMIT; /** * The delimiter used by multi-line text widgets. When text * is queried and from the widget, it will be delimited using * this delimiter. */ public static final String DELIMITER; /* * This code is intentionally commented. */// static final char PASSWORD; /* * 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; DELIMITER = "\r\n"; } static final int EditProc; static final TCHAR EditClass = new TCHAR (0, "EDIT", true); static { WNDCLASS lpWndClass = new WNDCLASS (); OS.GetClassInfo (0, EditClass, lpWndClass); EditProc = lpWndClass.lpfnWndProc; /* * This code is intentionally commented. */// int hwndText = OS.CreateWindowEx (0,// EditClass,// null,// OS.WS_OVERLAPPED | OS.ES_PASSWORD,// 0, 0, 0, 0,// 0,// 0,// OS.GetModuleHandle (null),// null);// char echo = (char) OS.SendMessage (hwndText, OS.EM_GETPASSWORDCHAR, 0, 0);// OS.DestroyWindow (hwndText);// PASSWORD = echo != 0 ? echo : '*'; }/** * 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#SINGLE * @see SWT#MULTI * @see SWT#READ_ONLY * @see SWT#WRAP * @see Widget#checkSubclass * @see Widget#getStyle */public Text (Composite parent, int style) { super (parent, checkStyle (style));}int callWindowProc (int msg, int wParam, int lParam) { if (handle == 0) return 0; return OS.CallWindowProc (EditProc, handle, msg, wParam, lParam);}void createHandle () { super.createHandle (); OS.SendMessage (handle, OS.EM_LIMITTEXT, 0, 0);}/** * 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 control is selected, by sending * it one of the messages defined in the <code>SelectionListener</code> * interface. * <p> * <code>widgetSelected</code> is not called for texts. * <code>widgetDefaultSelected</code> is typically called when ENTER is pressed in a single-line text. * </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);}/** * Adds the listener to the collection of listeners who will * be notified when the receiver's text is verified, by sending * it one of the messages defined in the <code>VerifyListener</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 VerifyListener * @see #removeVerifyListener */public void addVerifyListener (VerifyListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener (listener); addListener (SWT.Verify, typedListener);}/** * Appends a string. * <p> * The new text is appended to the text at * the end of the widget. * </p> * * @param string the string to be appended * * @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> */public void append (String string) { checkWidget (); if (string == null) error (SWT.ERROR_NULL_ARGUMENT); string = Display.withCrLf (string); int length = OS.GetWindowTextLength (handle); if (hooks (SWT.Verify) || filters (SWT.Verify)) { string = verifyText (string, length, length, null); if (string == null) return; } OS.SendMessage (handle, OS.EM_SETSEL, length, length); TCHAR buffer = new TCHAR (getCodePage (), string, true); /* * Feature in Windows. When an edit control with ES_MULTILINE * style that does not have the WS_VSCROLL style is full (i.e. * there is no space at the end to draw any more characters), * EM_REPLACESEL sends a WM_CHAR with a backspace character * to remove any further text that is added. This is an * implementation detail of the edit control that is unexpected * and can cause endless recursion when EM_REPLACESEL is sent * from a WM_CHAR handler. The fix is to ignore calling the * handler from WM_CHAR. */ ignoreCharacter = true; OS.SendMessage (handle, OS.EM_REPLACESEL, 0, buffer); ignoreCharacter = false; OS.SendMessage (handle, OS.EM_SCROLLCARET, 0, 0);}static int checkStyle (int style) { style = checkBits (style, SWT.LEFT, SWT.CENTER, SWT.RIGHT, 0, 0, 0); if ((style & SWT.SINGLE) != 0) style &= ~(SWT.H_SCROLL | SWT.V_SCROLL | SWT.WRAP); if ((style & SWT.WRAP) != 0) style |= SWT.MULTI; if ((style & SWT.MULTI) != 0) style &= ~SWT.PASSWORD; if ((style & (SWT.SINGLE | SWT.MULTI)) != 0) return style; if ((style & (SWT.H_SCROLL | SWT.V_SCROLL)) != 0) return style | SWT.MULTI; return style | SWT.SINGLE;}/** * Clears the selection. * * @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 clearSelection () { checkWidget (); if (OS.IsWinCE) { /* * Bug in WinCE. Calling EM_SETSEL with -1 and 0 is equivalent * to calling EM_SETSEL with 0 and -1. It causes the entire * text to be selected instead of clearing the selection. The * fix is to set the start of the selection to the end of the * current selection. */ int [] end = new int [1]; OS.SendMessage (handle, OS.EM_GETSEL, (int []) null, end); OS.SendMessage (handle, OS.EM_SETSEL, end [0], end [0]); } else { OS.SendMessage (handle, OS.EM_SETSEL, -1, 0); }}public Point computeSize (int wHint, int hHint, boolean changed) { checkWidget (); int height = 0, width = 0; if (wHint == SWT.DEFAULT || hHint == 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); TEXTMETRIC tm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW () : new TEXTMETRICA (); OS.GetTextMetrics (hDC, tm); int count = OS.SendMessage (handle, OS.EM_GETLINECOUNT, 0, 0); height = count * tm.tmHeight; RECT rect = new RECT (); int flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_NOPREFIX; boolean wrap = (style & SWT.MULTI) != 0 && (style & SWT.WRAP) != 0; if (wrap && wHint != SWT.DEFAULT) { flags |= OS.DT_WORDBREAK; rect.right = wHint; } String text = getText (); TCHAR buffer = new TCHAR (getCodePage (), text, false); int length = buffer.length (); if (length != 0) { OS.DrawText (hDC, buffer, length, rect, flags); width = rect.right - rect.left; } if (wrap && hHint == SWT.DEFAULT) { int newHeight = rect.bottom - rect.top; if (newHeight != 0) height = newHeight; } if (newFont != 0) OS.SelectObject (hDC, oldFont); OS.ReleaseDC (handle, hDC); } if (width == 0) width = DEFAULT_WIDTH; if (height == 0) height = DEFAULT_HEIGHT; if (wHint != SWT.DEFAULT) width = wHint; if (hHint != SWT.DEFAULT) height = hHint; /* Calculate the margin width */ int margins = OS.SendMessage(handle, OS.EM_GETMARGINS, 0, 0); int marginWidth = (margins & 0xFFFF) + ((margins >> 16) & 0xFFFF); width += marginWidth; /* * The preferred height of a single-line text widget * has been hand-crafted to be the same height as * the single-line text widget in an editable combo * box. */ if ((style & SWT.V_SCROLL) != 0) { width += OS.GetSystemMetrics (OS.SM_CXVSCROLL); } if ((style & SWT.H_SCROLL) != 0) { height += OS.GetSystemMetrics (OS.SM_CYHSCROLL); if ((style & SWT.BORDER) == 0) width++; } if ((style & SWT.BORDER) != 0) { int border = getBorderWidth (); width += (border * 2) + 3; height += (border * 2) + 3; } 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> */public void copy () { checkWidget (); OS.SendMessage (handle, OS.WM_COPY, 0, 0);}void createWidget () { super.createWidget (); doubleClick = true; setTabStops (tabs = 8); fixAlignment ();}/** * Cuts the selected text. * <p> * The current selection is first copied to the * clipboard and then deleted from the widget. * </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 cut () { checkWidget (); OS.SendMessage (handle, OS.WM_CUT, 0, 0);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -