📄 text.java
字号:
* space (' ') character. The width of a single * tab stop is the pixel width of the spaces. * </p> * * @return the number of tab characters * * @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 int getTabs () { checkWidget (); return tabs;}int getTabWidth (int tabs) { int oldFont = 0; RECT rect = new RECT (); int hDC = OS.GetDC (handle); int newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0); if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE | OS.DT_NOPREFIX; TCHAR SPACE = new TCHAR (getCodePage (), " ", false); OS.DrawText (hDC, SPACE, SPACE.length (), rect, flags); if (newFont != 0) OS.SelectObject (hDC, oldFont); OS.ReleaseDC (handle, hDC); return (rect.right - rect.left) * tabs;}/** * Gets the widget text. * <p> * The text for a text widget is the characters in the widget. * </p> * * @return the widget text * * @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 String getText () { checkWidget (); int length = OS.GetWindowTextLength (handle); if (length == 0) return ""; TCHAR buffer = new TCHAR (getCodePage (), length + 1); OS.GetWindowText (handle, buffer, length + 1); return buffer.toString (0, length);}/** * Gets a range of text. Returns an empty string if the * start of the range is greater than the end. * <p> * Indexing is zero based. The range of * a selection is from 0..N-1 where N is * the number of characters in the widget. * </p> * * @param start the start of the range * @param end the end of the range * @return the range of text * * @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 String getText (int start, int end) { checkWidget (); if (!(start <= end && 0 <= end)) return ""; int length = OS.GetWindowTextLength (handle); if (OS.IsDBLocale) length = mbcsToWcsPos (length); start = Math.max (0, start); end = Math.min (end, length - 1); /* * NOTE: The current implementation uses substring () * which can reference a potentially large character * array. */ return getText ().substring (start, end + 1);}/** * Returns the maximum number of characters that the receiver is capable of holding. * <p> * If this has not been changed by <code>setTextLimit()</code>, * it will be the constant <code>Text.LIMIT</code>. * </p> * * @return the text limit * * @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 int getTextLimit () { checkWidget (); return OS.SendMessage (handle, OS.EM_GETLIMITTEXT, 0, 0);}/** * Returns the zero-relative index of the line which is currently * at the top of the receiver. * <p> * This index can change when lines are scrolled or new lines are added or removed. * </p> * * @return the index of the top line * * @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 int getTopIndex () { checkWidget (); if ((style & SWT.SINGLE) != 0) return 0; return OS.SendMessage (handle, OS.EM_GETFIRSTVISIBLELINE, 0, 0);}/** * Gets the top pixel. * <p> * The top pixel is the pixel position of the line * that is currently at the top of the widget. On * some platforms, a text widget can be scrolled by * pixels instead of lines so that a partial line * is displayed at the top of the widget. * </p><p> * The top pixel changes when the widget is scrolled. * The top pixel does not include the widget trimming. * </p> * * @return the pixel position of the top line * * @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 int getTopPixel () { checkWidget (); /* * Note, EM_GETSCROLLPOS is implemented in Rich Edit 3.0 * and greater. The plain text widget and previous versions * of Rich Edit return zero. */ int [] buffer = new int [2]; int code = OS.SendMessage (handle, OS.EM_GETSCROLLPOS, 0, buffer); if (code == 1) return buffer [1]; return getTopIndex () * getLineHeight ();}/** * Inserts a string. * <p> * The old selection is replaced with the new text. * </p> * * @param string the string * * @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 insert (String string) { checkWidget (); if (string == null) error (SWT.ERROR_NULL_ARGUMENT); string = Display.withCrLf (string); if (hooks (SWT.Verify) || filters (SWT.Verify)) { int [] start = new int [1], end = new int [1]; OS.SendMessage (handle, OS.EM_GETSEL, start, end); string = verifyText (string, start [0], end [0], null); if (string == null) return; } 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;}int mbcsToWcsPos (int mbcsPos) { if (mbcsPos == 0) return 0; if (OS.IsUnicode) return mbcsPos; int cp = getCodePage (); int wcsTotal = 0, mbcsTotal = 0; byte [] buffer = new byte [128]; String delimiter = getLineDelimiter(); int delimiterSize = delimiter.length (); int count = OS.SendMessageA (handle, OS.EM_GETLINECOUNT, 0, 0); for (int line=0; line<count; line++) { int wcsSize = 0; int linePos = OS.SendMessageA (handle, OS.EM_LINEINDEX, line, 0); int mbcsSize = OS.SendMessageA (handle, OS.EM_LINELENGTH, linePos, 0); if (mbcsSize != 0) { if (mbcsSize + delimiterSize > buffer.length) { buffer = new byte [mbcsSize + delimiterSize]; } buffer [0] = (byte) (mbcsSize & 0xFF); buffer [1] = (byte) (mbcsSize >> 8); mbcsSize = OS.SendMessageA (handle, OS.EM_GETLINE, line, buffer); wcsSize = OS.MultiByteToWideChar (cp, OS.MB_PRECOMPOSED, buffer, mbcsSize, null, 0); } if (line - 1 != count) { for (int i=0; i<delimiterSize; i++) { buffer [mbcsSize++] = (byte) delimiter.charAt (i); } wcsSize += delimiterSize; } if ((mbcsTotal + mbcsSize) >= mbcsPos) { int bufferSize = mbcsPos - mbcsTotal; wcsSize = OS.MultiByteToWideChar (cp, OS.MB_PRECOMPOSED, buffer, bufferSize, null, 0); return wcsTotal + wcsSize; } wcsTotal += wcsSize; mbcsTotal += mbcsSize; } return wcsTotal;}/** * Pastes text from clipboard. * <p> * The selected text is deleted from the widget * and new text inserted from 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 paste () { checkWidget (); OS.SendMessage (handle, OS.WM_PASTE, 0, 0);}/** * Removes the listener from the collection of listeners who will * be notified when the receiver's text is modified. * * @param listener the listener which should no longer 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 #addModifyListener */public void removeModifyListener (ModifyListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Modify, listener); }/** * Removes the listener from the collection of listeners who will * be notified when the control is selected. * * @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 #addSelectionListener */public void removeSelectionListener (SelectionListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Selection, listener); eventTable.unhook (SWT.DefaultSelection,listener); }/** * Removes the listener from the collection of listeners who will * be notified when the control is verified. * * @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 #addVerifyListener */public void removeVerifyListener (VerifyListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Verify, listener); }/** * Selects all the text in the receiver. * * @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 selectAll () { checkWidget (); OS.SendMessage (handle, OS.EM_SETSEL, 0, -1);}boolean sendKeyEvent (int type, int msg, int wParam, int lParam, Event event) { if (!super.sendKeyEvent (type, msg, wParam, lParam, event)) { return false; } if (ignoreVerify) return true; if (type != SWT.KeyDown) return true; if (msg != OS.WM_CHAR && msg != OS.WM_KEYDOWN && msg != OS.WM_IME_CHAR) { return true; } if (event.character == 0) return true; if (!hooks (SWT.Verify) && !filters (SWT.Verify)) return true; char key = event.character; int stateMask = event.stateMask; /* * Disable all magic keys that could modify the text * and don't send events when Alt, Shift or Ctrl is * pressed. */ switch (msg) { case OS.WM_CHAR: if (key != 0x08 && key != 0x7F && key != '\r' && key != '\t' && key != '\n') break; // FALL THROUGH case OS.WM_KEYDOWN: if ((stateMask & (SWT.ALT | SWT.SHIFT | SWT.CONTROL)) != 0) return false; break; } /* * If the left button is down, the text widget refuses the character. */ if (OS.GetKeyState (OS.VK_LBUTTON) < 0) { return true; } /* Verify the character */ String oldText = ""; int [] start = new int [1], end = new int [1]; OS.SendMessage (handle, OS.EM_GETSEL, start, end); switch (key) { case 0x08: /* Bs */ if (start [0] == end [0]) { if (start [0] == 0) return true; int lineStart = OS.SendMessage (handle, OS.EM_LINEINDEX, -1, 0); if (start [0] == lineStart) { start [0] = start [0] - DELIMITER.length (); } else { start [0] = start [0] - 1; if (OS.IsDBLocale) { int [] newStart = new int [1], newEnd = new int [1]; OS.SendMessage (handle, OS.EM_SETSEL, start [0], end [0]); OS.SendMessage (handle, OS.EM_GETSEL, newStart, newEnd); if (start [0] != newStart [0]) start [0] = start [0] - 1; } } start [0] = Math.max (start [0], 0); } break; case 0x7F: /* Del */ if (start [0] == end [0]) { int length = OS.GetWindowTextLength (handle); if (start [0] == length) return true; int line = OS.SendMessage (handle, OS.EM_LINEFROMCHAR, end [0], 0); int lineStart = OS.SendMessage (handle, OS.EM_LINEINDEX, line + 1, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -