@Override public Point computeSizeConstraints(final int widthInChars, final int heightInChars) { final GC gc = new GC(fContentComposite); gc.setFont(JFaceResources.getDialogFont()); final int titleWidth = gc.getFontMetrics().getAverageCharWidth() * widthInChars; final int titleHeight = fTitleText.getLineHeight(); gc.dispose(); final int infoWidth = LayoutUtil.hintWidth(fInfoText, PREF_DETAIL_PANE_FONT, widthInChars); final int infoHeight = fInfoText.getLineHeight() * (heightInChars); return new Point( Math.max(titleWidth, infoWidth), titleHeight + LayoutUtil.defaultVSpacing() + infoHeight); }
/** * Convert mouse screen coordinates to a <code>StyledText</code> offset. * * @param x screen X-coordinate * @param y screen Y-coordinate * @param absolute if <code>true</code>, coordinates are expected to be absolute screen * coordinates * @return text offset * @see StyledText#getOffsetAtLocation() */ private int getOffsetAtLocation(int x, int y, boolean absolute) { StyledText textWidget = fViewer.getTextWidget(); StyledTextContent content = textWidget.getContent(); Point location; if (absolute) { location = textWidget.toControl(x, y); } else { location = new Point(x, y); } int line = (textWidget.getTopPixel() + location.y) / textWidget.getLineHeight(); if (line >= content.getLineCount()) { return content.getCharCount(); } int lineOffset = content.getOffsetAtLine(line); String lineText = content.getLine(line); Point endOfLine = textWidget.getLocationAtOffset(lineOffset + lineText.length()); if (location.x >= endOfLine.x) { return lineOffset + lineText.length(); } try { return textWidget.getOffsetAtLocation(location); } catch (IllegalArgumentException iae) { // we are expecting this return -1; } }
/** * 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; }
protected void refreshCaret(StyledText text, int newOffset) { if (newOffset != -1) { Point newPos = text.getLocationAtOffset(newOffset); int newHeight = text.getLineHeight(newOffset); text.redraw(newPos.x, newPos.y, CARET_WIDTH, newHeight, false); } }
private void scrollToCaret() { int caretOffset = currentText.getCaretOffset(); int lineIndex = currentText.getLineAtOffset(caretOffset); int lineHeight = currentText.getLineHeight(); int linesVisible = currentText.getSize().y / lineHeight; int lineMiddle = linesVisible / 2; int lineTop = lineIndex - lineMiddle; if (lineTop < 0) lineTop = 0; currentText.setTopIndex(lineTop); }
/* * (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 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); }
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(); }
@Override public Point computeSizeHint() { updateInput(); final Point sizeConstraints = getSizeConstraints(); final Rectangle trim = computeTrim(); // int charWidth = 20; // if (fInput.detailInfo != null) { // final int count = Math.min(6, fInfoText.getLineCount()); // for (int i = 0; i < count; i++) { // charWidth = Math.max(charWidth, fInfoText.getLine(i).length()); // } // } int widthHint = fInfoText.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x + LayoutUtil.defaultHSpacing(); final int widthMax2 = LayoutUtil.hintWidth(fInfoText, PREF_DETAIL_PANE_FONT, 80); final int widthMax = ((sizeConstraints != null && sizeConstraints.x != SWT.DEFAULT) ? sizeConstraints.x : widthMax2) - trim.width; fLayoutHint = true; final int titleHint = LayoutUtil.defaultHMargin() + fTitleImage.getSize().x + LayoutUtil.defaultHSpacing() + fTitleText.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; fLayoutHint = false; if (titleHint > widthHint && widthMax2 > widthHint) { widthHint = Math.min(widthMax2, titleHint); } if (widthMax < widthHint) { widthHint = widthMax; } // avoid change of wrapping caused by scrollbar if (widthHint < titleHint && widthHint + fInfoText.computeTrim(0, 0, 0, 0).width >= titleHint) { widthHint = titleHint; } final int heightMax = ((sizeConstraints != null && sizeConstraints.y != SWT.DEFAULT) ? sizeConstraints.y : fInfoText.getLineHeight() * 12) - trim.height; final Point size = fContentComposite.computeSize(widthHint, SWT.DEFAULT, true); size.y += LayoutUtil.defaultVSpacing(); size.x = Math.max(Math.min(size.x, widthMax), 200) + trim.width; size.y = Math.max(Math.min(size.y, heightMax), 100) + trim.height; return size; }
/* * @see org.eclipse.jface.text.source.AnnotationPainter.IDrawingStrategy#draw(org.eclipse.swt.graphics.GC, org.eclipse.swt.custom.StyledText, int, int, org.eclipse.swt.graphics.Color) */ public void draw( Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) { if (annotation instanceof ProjectionAnnotation) { ProjectionAnnotation projectionAnnotation = (ProjectionAnnotation) annotation; if (projectionAnnotation.isCollapsed()) { if (gc != null) { StyledTextContent content = textWidget.getContent(); int line = content.getLineAtOffset(offset); int lineStart = content.getOffsetAtLine(line); String text = content.getLine(line); int lineLength = text == null ? 0 : text.length(); int lineEnd = lineStart + lineLength; Point p = textWidget.getLocationAtOffset(lineEnd); Color c = gc.getForeground(); gc.setForeground(color); FontMetrics metrics = gc.getFontMetrics(); // baseline: where the dots are drawn int baseline = textWidget.getBaseline(offset); // descent: number of pixels that the box extends over baseline int descent = Math.min(2, textWidget.getLineHeight(offset) - baseline); // ascent: so much does the box stand up from baseline int ascent = metrics.getAscent(); // leading: free space from line top to box upper line int leading = baseline - ascent; // height: height of the box int height = ascent + descent; int width = metrics.getAverageCharWidth(); gc.drawRectangle(p.x, p.y + leading, width, height); int third = width / 3; int dotsVertical = p.y + baseline - 1; gc.drawPoint(p.x + third, dotsVertical); gc.drawPoint(p.x + width - third, dotsVertical); gc.setForeground(c); } else { textWidget.redrawRange(offset, length, true); } } } }
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 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); }
@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 getVerticalScrollOffset() { StyledText st = getSourceViewer().getTextWidget(); int lineHeight = st.getLineHeight(); return getSourceViewer().getTopInset() - ((getDocumentRegionOffset() * lineHeight) + st.getTopPixel()); }
public int getViewportLines() { StyledText te = getSourceViewer().getTextWidget(); Rectangle clArea = te.getClientArea(); if (!clArea.isEmpty()) return clArea.height / te.getLineHeight(); return 0; }
@Override protected Control createDialogArea(Composite parent) { setTitle((templatePersistenceData == null ? "Create a new" : "Modify the") + " Code Clip"); Composite parentComposite = (Composite) super.createDialogArea(parent); GridLayout gridLayout = (GridLayout) parentComposite.getLayout(); gridLayout.numColumns = 2; // 3; gridLayout.makeColumnsEqualWidth = false; GridData layoutData = (GridData) parentComposite.getChildren()[0].getLayoutData(); layoutData.horizontalSpan = 2; // 3; gridLayout.marginWidth = 10; gridLayout.marginHeight = 10; gridLayout.horizontalSpacing = 5; gridLayout.verticalSpacing = 5; Label abbrevLabel = new Label(parentComposite, SWT.NONE); abbrevLabel.setText("Abbreviation: "); GridData abbrevLabelGridData = new GridData(SWT.LEFT, SWT.CENTER, false, false); abbrevLabel.setLayoutData(abbrevLabelGridData); Label descriptionLabel = new Label(parentComposite, SWT.NONE); descriptionLabel.setText("Description:"); GridData descriptionLabelGridData = new GridData(SWT.FILL, SWT.CENTER, true, false); descriptionLabel.setLayoutData(descriptionLabelGridData); // Label contentTypeLabel = new Label(parentComposite, SWT.NONE); // contentTypeLabel.setText("Content Type:"); // GridData contentTypeLabelGridData = new GridData(SWT.LEFT, SWT.CENTER, false, false); // contentTypeLabel.setLayoutData(contentTypeLabelGridData); abbrevText = new Text(parentComposite, SWT.SINGLE | SWT.BORDER); abbrevText.setText(abbrev); abbrevText.setEditable(templatePersistenceData == null); GridData abbrevTextGridData = new GridData(SWT.FILL, SWT.CENTER, false, false); abbrevText.setLayoutData(abbrevTextGridData); abbrevText.addModifyListener( new ModifyListener() { public void modifyText(ModifyEvent e) { validateAbbrev(); } }); descriptionText = new Text(parentComposite, SWT.SINGLE | SWT.BORDER); descriptionText.setText(description); GridData descriptionTextGridData = new GridData(SWT.FILL, SWT.CENTER, true, false); descriptionText.setLayoutData(descriptionTextGridData); // contentTypeText = new Text(parentComposite, SWT.SINGLE | SWT.BORDER); // contentTypeText.setText(contentType); // contentTypeText.setEditable(false); // GridData contentTypeTextGridData = new GridData(SWT.FILL, SWT.CENTER, false, false); // contentTypeText.setLayoutData(contentTypeTextGridData); Label expansionLabel = new Label(parentComposite, SWT.NONE); expansionLabel.setText("Expansion:"); GridData expansionLabelGridData = new GridData(SWT.LEFT, SWT.CENTER, false, false); expansionLabelGridData.horizontalSpan = 2; // 3; expansionLabel.setLayoutData(expansionLabelGridData); expansionText = new StyledText(parentComposite, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); expansionText.setText(expansion); expansionText.setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT)); GridData styledTextGridData = new GridData(SWT.FILL, SWT.FILL, true, true); styledTextGridData.heightHint = expansionText.getLineHeight() * 8; styledTextGridData.horizontalSpan = 2; // 3; expansionText.setLayoutData(styledTextGridData); if (templatePersistenceData == null) { abbrevText.setFocus(); } else { descriptionText.setFocus(); } return parentComposite; }