📄 styledtext.java
字号:
* 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#FULL_SELECTION * @see SWT#MULTI * @see SWT#READ_ONLY * @see SWT#SINGLE * @see SWT#WRAP * @see #getStyle */public StyledText(Composite parent, int style) { super(parent, checkStyle(style | SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND)); // set the bg/fg in the OS to ensure that these are the same as StyledText, necessary // for ensuring that the bg/fg the IME box uses is the same as what StyledText uses super.setForeground(getForeground()); super.setBackground(getBackground()); Display display = getDisplay(); isMirrored = (super.getStyle() & SWT.MIRRORED) != 0; isBidi = BidiUtil.isBidiPlatform() || isMirrored; if ((style & SWT.READ_ONLY) != 0) { setEditable(false); } leftMargin = rightMargin = isBidi() ? BIDI_CARET_WIDTH - 1: 0; if ((style & SWT.SINGLE) != 0 && (style & SWT.BORDER) != 0) { leftMargin = topMargin = rightMargin = bottomMargin = 2; } clipboard = new Clipboard(display); installDefaultContent(); initializeRenderer(); if ((style & SWT.WRAP) != 0) { setWordWrap(true); } else { lineCache = new ContentWidthCache(this, content); } defaultCaret = new Caret(this, SWT.NULL); if (isBidi) { createCaretBitmaps(); Runnable runnable = new Runnable() { public void run() { int direction = BidiUtil.getKeyboardLanguage() == BidiUtil.KEYBOARD_BIDI ? SWT.RIGHT : SWT.LEFT; if (direction == caretDirection) return; if (getCaret() != defaultCaret) return; int lineIndex = getCaretLine(); String line = content.getLine(lineIndex); int lineOffset = content.getOffsetAtLine(lineIndex); int offsetInLine = caretOffset - lineOffset; int newCaretX = getXAtOffset(line, lineIndex, offsetInLine); setCaretLocation(newCaretX, getCaretLine(), direction); } }; BidiUtil.addLanguageListener(handle, runnable); } setCaret(defaultCaret); calculateScrollBars(); createKeyBindings(); ibeamCursor = new Cursor(display, SWT.CURSOR_IBEAM); setCursor(ibeamCursor); installListeners(); installDefaultLineStyler(); initializeAccessible();}/** * Adds an extended modify listener. An ExtendedModify event is sent by the * widget when the widget text has changed. * <p> * * @param listener the listener * @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 IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT when listener is null</li> * </ul> */public void addExtendedModifyListener(ExtendedModifyListener extendedModifyListener) { checkWidget(); if (extendedModifyListener == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); StyledTextListener typedListener = new StyledTextListener(extendedModifyListener); addListener(ExtendedModify, typedListener);}/** * Maps a key to an action. * One action can be associated with N keys. However, each key can only * have one action (key:action is N:1 relation). * <p> * * @param key a key code defined in SWT.java or a character. * Optionally ORd with a state mask. Preferred state masks are one or more of * SWT.MOD1, SWT.MOD2, SWT.MOD3, since these masks account for modifier platform * differences. However, there may be cases where using the specific state masks * (i.e., SWT.CTRL, SWT.SHIFT, SWT.ALT, SWT.COMMAND) makes sense. * @param action one of the predefined actions defined in ST.java. * Use SWT.NULL to remove a key binding. * @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 setKeyBinding(int key, int action) { checkWidget(); int keyValue = key & SWT.KEY_MASK; int modifierValue = key & SWT.MODIFIER_MASK; char keyChar = (char)keyValue; if (Compatibility.isLetter(keyChar)) { // make the keybinding case insensitive by adding it // in its upper and lower case form char ch = Character.toUpperCase(keyChar); int newKey = ch | modifierValue; if (action == SWT.NULL) { keyActionMap.remove(new Integer(newKey)); } else { keyActionMap.put(new Integer(newKey), new Integer(action)); } ch = Character.toLowerCase(keyChar); newKey = ch | modifierValue; if (action == SWT.NULL) { keyActionMap.remove(new Integer(newKey)); } else { keyActionMap.put(new Integer(newKey), new Integer(action)); } } else { if (action == SWT.NULL) { keyActionMap.remove(new Integer(key)); } else { keyActionMap.put(new Integer(key), new Integer(action)); } } }/** * Adds a bidirectional segment listener. A BidiSegmentEvent is sent * whenever a line of text is measured or rendered. The user can * specify text ranges in the line that should be treated as if they * had a different direction than the surrounding text. * This may be used when adjacent segments of right-to-left text should * not be reordered relative to each other. * E.g., Multiple Java string literals in a right-to-left language * should generally remain in logical order to each other, that is, the * way they are stored. * <p> * * @param listener the listener * @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 IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT when listener is null</li> * </ul> * @see BidiSegmentEvent * @since 2.0 */public void addBidiSegmentListener(BidiSegmentListener listener) { checkWidget(); if (listener == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } StyledTextListener typedListener = new StyledTextListener(listener); addListener(LineGetSegments, typedListener); }/** * Adds a line background listener. A LineGetBackground event is sent by the * widget to determine the background color for a line. * <p> * * @param listener the listener * @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 IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT when listener is null</li> * </ul> */public void addLineBackgroundListener(LineBackgroundListener listener) { checkWidget(); if (listener == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (userLineBackground == false) { removeLineBackgroundListener(defaultLineStyler); defaultLineStyler.setLineBackground(0, logicalContent.getLineCount(), null); userLineBackground = true; } StyledTextListener typedListener = new StyledTextListener(listener); addListener(LineGetBackground, typedListener); }/** * Adds a line style listener. A LineGetStyle event is sent by the widget to * determine the styles for a line. * <p> * * @param listener the listener * @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 IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT when listener is null</li> * </ul> */public void addLineStyleListener(LineStyleListener listener) { checkWidget(); if (listener == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } if (userLineStyle == false) { removeLineStyleListener(defaultLineStyler); defaultLineStyler.setStyleRange(null); userLineStyle = true; } StyledTextListener typedListener = new StyledTextListener(listener); addListener(LineGetStyle, typedListener); }/** * Adds a modify listener. A Modify event is sent by the widget when the widget text * has changed. * <p> * * @param listener the listener * @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 IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT when listener is null</li> * </ul> */public void addModifyListener(ModifyListener modifyListener) { checkWidget(); if (modifyListener == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } TypedListener typedListener = new TypedListener(modifyListener); addListener(SWT.Modify, typedListener);}/** * Adds a selection listener. A Selection event is sent by the widget when the * selection has changed. * <p> * When <code>widgetSelected</code> is called, the event x amd y fields contain * the start and end caret indices of the selection. * <code>widgetDefaultSelected</code> is not called for StyledTexts. * </p> * * @param listener the listener * @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 IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT when listener is null</li> * </ul> */public void addSelectionListener(SelectionListener listener) { checkWidget(); if (listener == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } TypedListener typedListener = new TypedListener(listener); addListener(SWT.Selection, typedListener); }/** * Adds a verify key listener. A VerifyKey event is sent by the widget when a key * is pressed. The widget ignores the key press if the listener sets the doit field * of the event to false. * <p> * * @param listener the listener * @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 IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT when listener is null</li> * </ul> */public void addVerifyKeyListener(VerifyKeyListener listener) { checkWidget(); if (listener == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } StyledTextListener typedListener = new StyledTextListener(listener); addListener(VerifyKey, typedListener); }/** * Adds a verify listener. A Verify event is sent by the widget when the widget text * is about to change. The listener can set the event text and the doit field to * change the text that is set in the widget or to force the widget to ignore the * text change. * <p> * * @param listener the listener * @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 IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT when listener is null</li> * </ul> */public void addVerifyListener(VerifyListener verifyListener) { checkWidget(); if (verifyListener == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } TypedListener typedListener = new TypedListener(verifyListener); addListener(SWT.Verify, typedListener);}/** * Appends a string to the text at the end of the widget. * <p> * * @param string the string to be appended * @see #replaceTextRange(int,int,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> * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT when listener is null</li> * </ul> */public void append(String string) { checkWidget(); if (string == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } int lastChar = Math.max(getCharCount(), 0); replaceTextRange(lastChar, 0, string);}/** * Calculates the width of the widest visible line. */void calculateContentWidth() { lineCache = getLineCache(content); lineCache.calculate(topIndex, getPartialBottomIndex() - topIndex + 1);}/** * Calculates the scroll bars */void calculateScrollBars() { ScrollBar horizontalBar = getHorizontalBar(); ScrollBar verticalBar = getVerticalBar(); setScrollBars(); if (verticalBar != null) { verticalBar.setIncrement(getVerticalIncrement()); } if (horizontalBar != null) { horizontalBar.setIncrement(getHorizontalIncrement()); }}/** * Calculates the top index based on the current vertical scroll offset. * The top index is the index of the topmost fully visible line or the * topmost partially visible line if no line is fully visible. * The top index starts at 0. */void calculateTopIndex() { int oldTopIndex = topIndex; int verticalIncrement = getVerticalIncrement(); int clientAreaHeight = getClientArea().height; if (verticalIncrement == 0) { return; } topIndex = Compatibility.ceil(verticalScrollOffset, verticalIncrement); // Set top index to partially visible top line if no line is fully
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -