/** * default implementation for {@link DefaultTextEditActions#DELETE_WORD_NEXT} */ protected void defaultDeleteWordNext() { int offset = getControl().getCaretOffset(); int following = TextUtil.findWordEndOffset( getControl().getContent(), offset, true); if (following != BreakIterator.DONE) { getControl().getContent().replaceTextRange(getControl().getCaretOffset(), following - offset, ""); //$NON-NLS-1$ } }
void handleTextChanging(TextChangingEvent event) { if (event.replaceCharCount < 0) { event.offset += event.replaceCharCount; event.replaceCharCount *= -1; } this.lastTextChangeStart = event.offset; // this.lastTextChangeNewLineCount = event.newLineCount; this.lastTextChangeNewCharCount = event.newCharCount; // this.lastTextChangeReplaceLineCount = event.replaceLineCount; this.lastTextChangeReplaceCharCount = event.replaceCharCount; this.renderer.textChanging(event); // Update the caret offset if it is greater than the length of the // content. // This is necessary since style range API may be called between the // handleTextChanging and handleTextChanged events and this API sets the // caretOffset. int newEndOfText = getContent().getCharCount() - event.replaceCharCount + event.newCharCount; if (getCaretOffset() > newEndOfText) setCaretOffset(newEndOfText/* , SWT.DEFAULT */); }
@Override public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { if (newValue.booleanValue()) { getSkinnable().requestFocus(); } } });
/** * Reveal the current caret location * @since 2.4.0 */ public void revealCaret() { if (getSkin() != null) { ((StyledTextSkin)getSkin()).scrollOffsetIntoView(getCaretOffset(), 2, 2); } }
void updateSelection(int startOffset, int replacedLength, int newLength) { if (getSelection().offset + getSelection().length > startOffset && getSelection().offset < startOffset + replacedLength) { // selection intersects replaced text. set caret behind text change setSelection(new TextSelection(startOffset + newLength, 0)/*, true, false*/); } else { // move selection to keep same text selected setSelection(new TextSelection(getSelection().offset + newLength - replacedLength, getSelection().length)/*, true, false*/); if( getSelection().length > 0 ) { int delta = this.lastTextChangeNewCharCount - this.lastTextChangeReplaceCharCount; this.caretOffsetProperty.set(Math.max(0,Math.min(getCharCount()-1,getCaretOffset() + delta))); } } }
/** * Set the current caret offset * * @param offset * the new offset */ public void setCaretOffset(int offset) { this.anchor = offset; caretOffsetProperty().set(offset); clearSelection(); }
styledText.caretOffsetProperty().addListener((obs, ol, ne) -> { int lineIdx = styledText.getContent().getLineAtOffset(ne.intValue()); int colIdx = ne.intValue() - styledText.getContent().getOffsetAtLine(lineIdx); String line = styledText.getContent().getLine(lineIdx).substring(0, colIdx); int tabCount = (int)line.chars().filter(c -> c == '\t').count(); scrollColumnIntoView(colIdx + tabCount * (styledText.tabAvanceProperty().get() - 1), 12); scrollLineIntoView(lineIdx); }); this.content.lineHeightProperty().bind(styledText.fixedLineHeightProperty()); bind(getSkinnable().fontProperty()); final DoubleBinding zoomedFontSize = origFontSize.multiply(getSkinnable().fontZoomFactorProperty()); final StringExpression zoomedFontStyle = Bindings.concat("-fx-font-size: ", zoomedFontSize, ";"); //$NON-NLS-1$//$NON-NLS-2$ getSkinnable().getAnnotationProvider().addListener((SetChangeListener<? super AnnotationProvider>) (c) -> { if (c.wasAdded()) { Subscription s = c.getElementAdded().registerChangeListener(onAnnotationChange); for (AnnotationProvider p : getSkinnable().getAnnotationProvider()) { if (!subscriptions.containsKey(p)) { Subscription s = p.registerChangeListener(onAnnotationChange); getSkinnable().getContextMenu().show(this.content, e.getScreenX(), e.getScreenY()); }); getSkinnable().requestFocus();
String allContent = getControl().getContent().getTextRange(0, getControl().getCharCount()); StringBuffer dataBuffer = new StringBuffer(allContent); final int caret = getControl().getCaretOffset(); final int selectionOffset = getControl().getSelection().offset; final int selectionLength = getControl().getSelection().length; final int firstLine = getControl().getLineAtOffset(selectionOffset); int lastLine = getControl().getLineAtOffset(selectionOffset + selectionLength); if (getControl().getOffsetAtLine(lastLine) < selectionOffset + selectionLength) { int indentLength = getControl().isInsertSpacesForTab() ? getControl().getTabAdvance() : 1; if( getControl().isInsertSpacesForTab() ) { StringBuilder b = new StringBuilder(); for( int i = 0; i < getControl().getTabAdvance(); i++ ) { b.append(" "); //$NON-NLS-1$ int lineStart = getControl().getOffsetAtLine(lineNumber) + added; dataBuffer.replace(lineStart, lineStart + 0, insertString); added += insertString.length(); String replaced = dataBuffer.substring(start,start+length); getControl().getContent().replaceTextRange(start, length-added, replaced); getControl().setCaretOffset(selectionOffset == caret ? caret + indentLength : caret + added); getControl().setSelectionRange(selectionOffset + indentLength, selectionLength + added - indentLength);
/** * default implementation for {@link DefaultTextEditActions#DELETE_PREVIOUS} */ protected void defaultDeletePrevious() { int offset = getControl().getCaretOffset(); TextSelection selection = getControl().getSelection(); if (selection.length > 0) { getControl().getContent().replaceTextRange(selection.offset, selection.length, ""); //$NON-NLS-1$ getControl().setCaretOffset(selection.offset); } else { int start = getControl().getCaretOffset() - 1; int del = 1; if (start - 1 >= 0) { if ("\r\n".equals(getControl().getContent().getTextRange(getControl().getCaretOffset()-2, 2))) { //$NON-NLS-1$ start = start - 1; del = 2; } } if( start >= 0 ) { getControl().getContent().replaceTextRange(start, del, ""); //$NON-NLS-1$ getControl().setCaretOffset(offset - del); } } }
int currentRowIndex = getControl().getContent().getLineAtOffset(getControl().getCaretOffset()); final int offset = getControl().getCaretOffset(); int currentLine = getControl().getContent().getLineAtOffset(offset); @SuppressWarnings("unused") int newLine = getControl().getContent().getLineAtOffset(newOffset); getControl().impl_setCaretOffset(newOffset, event.isShiftDown()); event.consume(); invokeAction(ActionType.WORD_NEXT); } else if (event.isMetaDown()) { int currentLine = getControl().getContent().getLineAtOffset(offset); int lineOffset = getControl().getContent().getOffsetAtLine(currentLine); String lineContent = getControl().getContent().getLine(currentLine); getControl().impl_setCaretOffset(lineOffset + lineContent.length(), event.isShiftDown()); event.consume(); } else { if (offset + 1 > getControl().getContent().getCharCount()) { break; getControl().impl_setCaretOffset(newOffset, event.isShiftDown()); event.consume(); int colIdx = offset - getControl().getContent().getOffsetAtLine(rowIndex); rowIndex -= 1; int lineOffset = getControl().getContent().getOffsetAtLine(rowIndex);
@Focus void focused() { if( activeInput != null ) { activeInput.setValue(input); } viewer.getTextWidget().requestFocus(); //TODO We should remember the caret offset if( viewer.getTextWidget().getCaretOffset() == -1 && viewer.getTextWidget().getContent().getCharCount() > 0 ) { viewer.getTextWidget().setCaretOffset(0); } }
/** * inserts text at the caret location or replaces a given selection * * @param text */ @SuppressWarnings("null") public void insert(CharSequence text) { if (text == null) throw new NullPointerException(); int start, replaceLength; if (!isSelectionEmpty()) { // replace selection start = this.getSelection().offset; replaceLength = this.getSelection().length; } else { // insert at caret start = this.getCaretOffset(); replaceLength = 0; } String content = text.toString(); if( isInsertSpacesForTab() ) { content = content.replaceAll("\t", Util.createRepeatedString(' ', this.tabAdvance.get())); //$NON-NLS-1$ } this.getContent().replaceTextRange(start, replaceLength, content); this.setCaretOffset(start + content.length()); }
private void _keyTyped(KeyEvent event) { if (getControl().getEditable()) { String character = event.getCharacter(); if (character.length() == 0) { return; } // check the modifiers // - OS-X: ALT+L ==> @ // - win32/linux: ALTGR+Q ==> @ if (event.isControlDown() || event.isAltDown() || (Util.isMacOS() && event.isMetaDown())) { if (!((event.isControlDown() || Util.isMacOS()) && event.isAltDown())) return; } if (character.charAt(0) > 31 // No ascii control chars && character.charAt(0) != 127 // no delete key && !event.isMetaDown()) { final int offset = getControl().getCaretOffset(); getControl().getContent().replaceTextRange(getControl().getCaretOffset(), 0, character); getControl().setCaretOffset(offset + 1); } } }
private int calculatePageUpOffset() { final int linesPerPage = ((StyledTextSkin)getControl().getSkin()).getVisibleLineCount(); final int globalOffset = getControl().getCaretOffset(); final int lineIndex = getControl().getContent().getLineAtOffset(globalOffset); final int lineLocalOffset = globalOffset - getControl().getContent().getOffsetAtLine(lineIndex); final int newLineIndex = Math.max(0, lineIndex - linesPerPage); final int newLineLocalOffset = Math.min(getControl().getContent().getLine(newLineIndex).length(), lineLocalOffset); return getControl().getOffsetAtLine(newLineIndex) + newLineLocalOffset; }
@SuppressWarnings("deprecation") private void moveCaretAbsolute(int absoluteOffset, boolean select) { int offset = Math.max(0, absoluteOffset); offset = Math.min(getControl().getCharCount(), offset); // we need to jump 2 chars if windows line endings are in effect if( offset > 0 ) { String textRange = getControl().getContent().getTextRange(offset-1, 1); // check the navigation direction if( getControl().getCaretOffset() > offset ) { if( textRange.equals("\r") ) { //$NON-NLS-1$ offset -= 1; } } else { if( textRange.equals("\r") ) { //$NON-NLS-1$ offset += 1; } } } getControl().impl_setCaretOffset(Math.max(0,offset), select); }
} else if (this.dragMoveTextMode) { ((StyledTextSkin)getControl().getSkin()).updateInsertionMarkerIndex(-1); getControl().pseudoClassStateChanged(DRAG_TEXT_MOVE_ACTIVE_PSEUDOCLASS_STATE, false); getControl().pseudoClassStateChanged(DRAG_TEXT_COPY_ACTIVE_PSEUDOCLASS_STATE, false); if( getControl().getSelection().contains(targetOffset) ) { this.dragMoveTextMode = false; event.consume(); String text = getControl().getContent().getTextRange(this.dragMoveTextOffset, this.dragMoveTextLength); getControl().fireEvent(UndoHintEvent.createBeginCompoundChangeEvent()); try { if( ! org.eclipse.fx.ui.controls.Util.isCopyEvent(event) ) { getControl().getContent().replaceTextRange(this.dragMoveTextOffset, this.dragMoveTextLength, ""); //$NON-NLS-1$ getControl().getContent().replaceTextRange(targetOffset, 0, text); getControl().setSelection(new TextSelection(targetOffset, text.length())); getControl().fireEvent(UndoHintEvent.createEndCompoundChangeEvent());
/** * @return the current char count */ public int getCharCount() { return getContent().getCharCount(); }
/** * Cut the current selection into the clipboard */ public void cut() { TextSelection selection = getSelection(); if (selection.length > 0) { final Clipboard clipboard = Clipboard.getSystemClipboard(); String content = getContent().getTextRange(selection.offset, selection.length); setCaretOffset(selection.offset); getContent().replaceTextRange(selection.offset, content.length(), ""); //$NON-NLS-1$ clipboard.setContent(Collections.singletonMap(DataFormat.PLAIN_TEXT, content)); } }
/** * navigates to specified line * * @param lineIndex */ public void navigateToLine(int lineIndex) { if (lineIndex >= 0 && lineIndex <= getContent().getLineCount()) { int offset = getContent().getOffsetAtLine(lineIndex); setCaretOffset(offset); } }
/** * Setting the caret offset and updateing the selection if requested * * @param offset * the offset * @param selection * <code>true</code> to update the current selection * @deprecated internal API for now */ @Deprecated public void impl_setCaretOffset(int offset, boolean selection) { if (selection) { caretOffsetProperty().set(offset); if (offset > this.anchor) { setSelectionRange(this.anchor, offset - this.anchor); } else { setSelectionRange(offset, this.anchor - offset); } } else { setCaretOffset(offset); } }