void redrawFrom(int lno) { if (lno < 0 || lno >= text.getLineCount()) return; int y = text.getLocationAtOffset(text.getOffsetAtLine(lno)).y; int height = text.getClientArea().height - y; int width = text.getClientArea().width + text.getHorizontalPixel(); text.redraw(0, y, width, height, false); }
/* * @see AbstractInfoView#setInput(Object) */ protected void setInput(Object input) { String javadocHtml = (String) input; if (fIsUsingBrowserWidget) { if (javadocHtml != null && javadocHtml.length() > 0) { boolean RTL = (getSite().getShell().getStyle() & SWT.RIGHT_TO_LEFT) != 0; if (RTL) { StringBuffer buffer = new StringBuffer(javadocHtml); HTMLPrinter.insertStyles(buffer, new String[] {"direction:rtl"}); // $NON-NLS-1$ javadocHtml = buffer.toString(); } } fBrowser.setText(javadocHtml); } else { fPresentation.clear(); Rectangle size = fText.getClientArea(); try { javadocHtml = ((DefaultInformationControl.IInformationPresenterExtension) fPresenter) .updatePresentation( getSite().getShell(), javadocHtml, fPresentation, size.width, size.height); } catch (IllegalArgumentException ex) { // the javadoc might no longer be valid return; } fText.setText(javadocHtml); TextPresentation.applyTextPresentation(fPresentation, fText); } }
/** * Hook to compute the menu location if the focus widget is a styled text widget. * * @param text the styled text widget that has the focus * @return a widget relative position of the menu to pop up or <code>null</code> if now position * inside the widget can be computed */ protected Point computeMenuLocation(StyledText text) { int offset = text.getCaretOffset(); Point result = text.getLocationAtOffset(offset); result.y += text.getLineHeight(offset); if (!text.getClientArea().contains(result)) return null; return result; }
private Point computeMenuLocation(Control focus, Menu menu) { Point cursorLocation = focus.getDisplay().getCursorLocation(); Rectangle clientArea = null; Point result = null; if (focus instanceof StyledText) { StyledText styledText = (StyledText) focus; clientArea = styledText.getClientArea(); result = computeMenuLocation(styledText); } else if (focus instanceof Tree) { Tree tree = (Tree) focus; clientArea = tree.getClientArea(); result = computeMenuLocation(tree); } else if (focus instanceof Table) { Table table = (Table) focus; clientArea = table.getClientArea(); result = computeMenuLocation(table); } if (result == null) { result = focus.toControl(cursorLocation); } if (clientArea != null && !clientArea.contains(result)) { result = new Point(clientArea.x + clientArea.width / 2, clientArea.y + clientArea.height / 2); } Rectangle shellArea = focus.getShell().getClientArea(); if (!shellArea.contains(focus.getShell().toControl(focus.toDisplay(result)))) { result = new Point(shellArea.x + shellArea.width / 2, shellArea.y + shellArea.height / 2); } return focus.toDisplay(result); }
/** * Draw the given line range. * * @param gc * @param startLine first line number * @param endLine last line number (inclusive) * @param x the X-coordinate of the drawing range * @param w the width of the drawing range */ private void drawLineRange(GC gc, int startLine, int endLine, int x, int w) { final int viewPortWidth = fTextWidget.getClientArea().width; for (int line = startLine; line <= endLine; line++) { int lineOffset = fTextWidget.getOffsetAtLine(line); // line end offset including line delimiter int lineEndOffset; if (line < fTextWidget.getLineCount() - 1) { lineEndOffset = fTextWidget.getOffsetAtLine(line + 1); } else { lineEndOffset = fTextWidget.getCharCount(); } // line length excluding line delimiter int lineLength = lineEndOffset - lineOffset; while (lineLength > 0) { char c = fTextWidget.getTextRange(lineOffset + lineLength - 1, 1).charAt(0); if (c != '\r' && c != '\n') { break; } --lineLength; } // compute coordinates of last character on line Point endOfLine = fTextWidget.getLocationAtOffset(lineOffset + lineLength); if (x - endOfLine.x > viewPortWidth) { // line is not visible continue; } // Y-coordinate of line int y = fTextWidget.getLinePixel(line); // compute first visible char offset int startOffset; try { startOffset = fTextWidget.getOffsetAtLocation(new Point(x, y)) - 1; if (startOffset - 2 <= lineOffset) { startOffset = lineOffset; } } catch (IllegalArgumentException iae) { startOffset = lineOffset; } // compute last visible char offset int endOffset; if (x + w >= endOfLine.x) { // line end is visible endOffset = lineEndOffset; } else { try { endOffset = fTextWidget.getOffsetAtLocation(new Point(x + w - 1, y)) + 1; if (endOffset + 2 >= lineEndOffset) { endOffset = lineEndOffset; } } catch (IllegalArgumentException iae) { endOffset = lineEndOffset; } } // draw character range if (endOffset > startOffset) { drawCharRange(gc, startOffset, endOffset); } } }
boolean getClientAreaChangedAndStoreNew() { Rectangle clientArea = styledText.getClientArea(); if (currClientArea == null || !currClientArea.equals(clientArea)) { currClientArea = clientArea; return true; } return false; }
void drawLine(int lno) { if (lno < 0 || lno >= text.getLineCount()) return; int y = text.getLocationAtOffset(text.getOffsetAtLine(lno)).y; int height = 0; if (text.getLineCount() > lno + 1) height = text.getLocationAtOffset(text.getOffsetAtLine(lno + 1)).y - y; else height = text.getLocationAtOffset(text.getCharCount()).y + text.getLineHeight(); int width = text.getClientArea().width + text.getHorizontalPixel(); text.redraw(0, y, width, height, false); }
@Override public void paintControl(PaintEvent event) { /* Using event.gc.getFontMetrics().getAverageCharWidth()) was not enough on low resolutions, as the rounding to an int seemed enough to screw up the right margin, even when using a monospaced font (this is the current theory). */ if (charsPerLine != getCharsPerLine()) { charsPerLine = getCharsPerLine(); char buffer[] = new char[charsPerLine]; for (int i = 0; i < charsPerLine; i++) buffer[i] = 'm'; rightMargin = event.gc.stringExtent(new String(buffer)).x; } int lineHeight = source.getLineHeight(); int drawHeight = source.getClientArea().height; int drawWidth = source.getClientArea().width; event.gc.setForeground(color); event.gc.setBackground(color); // draw right margin event.gc.drawLine(rightMargin, 0, rightMargin, drawHeight); for (int i = source.getTopIndex(); i < source.getLineCount(); i++) { // draw page lines int at = source.getLinePixel(i); if (isFirstLineOnPage(i)) event.gc.drawLine(0, at, drawWidth, at); // draw paragraph end markers String line = source.getLine(i); if (line.length() > 0 && line.charAt(line.length() - 1) == PARAGRAPH_END) { Point point = event.gc.stringExtent(line); int span = point.y / 2; event.gc.fillOval(point.x + span / 2, at + span / 2, span, span); } // check if line still visible if (at + lineHeight > drawHeight) break; } adjustOtherThread.notifyPainted(source); }
/* * (non-Javadoc) * * @see * org.eclipse.jface.fieldassist.IControlContentAdapter#getInsertionBounds(org.eclipse * .swt.widgets.Control) */ @Override public Rectangle getInsertionBounds(Control control) { StyledText text = (StyledText) control; Point caretOrigin = text.getLocationAtOffset(text.getCaretOffset()); return new Rectangle( caretOrigin.x + text.getClientArea().x, caretOrigin.y + text.getClientArea().y + 3, 1, text.getLineHeight()); }
void updateViewport() { baseEditor.lineCountEvent(text.getLineCount()); int start = 0; try { start = text.getTopIndex() - 1; } catch (Exception e) { e.printStackTrace(System.out); } if (start < 0) start = 0; int end = start + text.getClientArea().height / text.getLineHeight(); baseEditor.visibleTextEvent(start, end - start + 2); }
private synchronized void adjustOther(StyledText source, StyledText other) { if (source == null) source = this.source; if (other == null) other = this.other; if (source == null) { logWriter.println("WARNING: attempting to adjust source but no source StyledText"); return; } if (other == null) { logWriter.println("WARNING: attempting to adjust other but no other StyledText"); return; } int caretOffset = source.getCaretOffset(); int lineIndex = source.getLineAtOffset(caretOffset); int otherLineHeight = other.getLineHeight(); int otherLineRealPixel = lineIndex * otherLineHeight; int sourceLinePixel = source.getLinePixel(lineIndex); int otherTopPixel = otherLineRealPixel - sourceLinePixel; int otherHeight = other.getClientArea().height; int otherLineBelow = otherHeight - (sourceLinePixel + otherLineHeight); int otherLineCount = other.getLineCount(); int otherLinesBelow = otherLineCount - lineIndex; int otherLinesBelowHeight = otherLinesBelow * otherLineHeight; int otherBottomGap = otherHeight - (sourceLinePixel + otherLinesBelowHeight); // other would have to scroll before first line if (otherTopPixel < 0) { int sourceTopPixel = source.getTopPixel() - otherTopPixel; otherTopPixel = 0; source.setTopPixel(sourceTopPixel); } // other line would be partially past the bottom of view else if (otherLineBelow < 0) { int sourceTopPixel = source.getTopPixel() - otherLineBelow; otherTopPixel -= otherLineBelow; source.setTopPixel(sourceTopPixel); } // other would have to scroll past last line else if (otherBottomGap > 0) { int sourceTopPixel = source.getTopPixel() - otherBottomGap; otherTopPixel -= otherBottomGap; source.setTopPixel(sourceTopPixel); } other.setTopPixel(otherTopPixel); redraw(); }
private Point computeWordStart() { ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection(); IRegion textRegion = CWordFinder.findWord(fEditor.getViewer().getDocument(), selection.getOffset()); if (textRegion == null) return null; IRegion widgetRegion = modelRange2WidgetRange(textRegion); if (widgetRegion == null) return null; int start = widgetRegion.getOffset(); StyledText styledText = fEditor.getViewer().getTextWidget(); Point result = styledText.getLocationAtOffset(start); result.y += styledText.getLineHeight(start); if (!styledText.getClientArea().contains(result)) return null; return result; }
@Override public void caretMoved(CaretEvent ignored) { int caretOffset = source.getCaretOffset(); int lineIndex = source.getLineAtOffset(caretOffset); int lineOffset = source.getOffsetAtLine(lineIndex); // play line margin bell if (lineMarginClip != null && lineMarginBell > 0 && caretOffset == prevCaretOffset + 1) { if (caretOffset - lineOffset == lineMarginBell) if (!lineMarginClip.isActive()) { lineMarginClip.setFramePosition(0); lineMarginClip.start(); } } // play line end bell if (lineEndClip != null && charsPerLine > 0 && caretOffset == prevCaretOffset + 1) { if (caretOffset - lineOffset == charsPerLine) if (!lineEndClip.isActive()) { lineEndClip.setFramePosition(0); lineEndClip.start(); } } prevCaretOffset = caretOffset; // scroll other text to match current if (lineIndex == prevLineIndex) return; prevLineIndex = lineIndex; if (source != currentText) return; int sourceLinePixel = source.getLinePixel(lineIndex); int sourceHeight = source.getClientArea().height; int sourceLineHeight = source.getLineHeight(); // check if have to wait until after paint event if (sourceLinePixel < 0 || sourceLinePixel + sourceLineHeight > sourceHeight) adjustOtherThread.waitPainted(source, other); else adjustOtherThread.adjustOther(source, other); }
private Rectangle getLineRectangle(Position position) { if (position == null) { return null; } // if the position that is about to be drawn was deleted then we can't if (position.isDeleted()) { return null; } int widgetOffset = 0; if (fViewer instanceof ITextViewerExtension5) { ITextViewerExtension5 extension = (ITextViewerExtension5) fViewer; widgetOffset = extension.modelOffset2WidgetOffset(position.getOffset()); if (widgetOffset == -1) { return null; } } else { IRegion visible = fViewer.getVisibleRegion(); widgetOffset = position.getOffset() - visible.getOffset(); if (widgetOffset < 0 || visible.getLength() < widgetOffset) { return null; } } StyledText textWidget = fViewer.getTextWidget(); // check for https://bugs.eclipse.org/bugs/show_bug.cgi?id=64898 // this is a guard against the symptoms but not the actual solution if (0 <= widgetOffset && widgetOffset <= textWidget.getCharCount()) { Point upperLeft = textWidget.getLocationAtOffset(widgetOffset); int width = textWidget.getClientArea().width + textWidget.getHorizontalPixel(); int height = textWidget.getLineHeight(widgetOffset); return new Rectangle(0, upperLeft.y, width, height); } return null; }
public int getViewportHeight() { StyledText te = getSourceViewer().getTextWidget(); Rectangle clArea = te.getClientArea(); if (!clArea.isEmpty()) return clArea.height; return 0; }