Search in sources :

Example 6 with GlyphMetrics

use of org.eclipse.swt.graphics.GlyphMetrics in project eclipse.platform.swt by eclipse.

the class TextEditor method addImage.

void addImage(Image image) {
    int offset = styledText.getCaretOffset();
    // $NON-NLS-1$
    styledText.replaceTextRange(offset, 0, "\uFFFC");
    StyleRange style = new StyleRange();
    Rectangle rect = image.getBounds();
    style.metrics = new GlyphMetrics(rect.height, 0, rect.width);
    style.data = image;
    int[] ranges = { offset, 1 };
    StyleRange[] styles = { style };
    styledText.setStyleRanges(0, 0, ranges, styles);
}
Also used : GlyphMetrics(org.eclipse.swt.graphics.GlyphMetrics) StyleRange(org.eclipse.swt.custom.StyleRange) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point)

Example 7 with GlyphMetrics

use of org.eclipse.swt.graphics.GlyphMetrics in project translationstudio8 by heartsome.

the class PresentationRepairer method addRange.

/**
	 * Adds style information to the given text presentation.
	 * @param presentation
	 *            the text presentation to be extended
	 * @param offset
	 *            the offset of the range to be styled
	 * @param length
	 *            the length of the range to be styled
	 * @param textStyle
	 *            the style of the range to be styled
	 */
protected void addRange(TextPresentation presentation, int offset, int length, TextStyle textStyle) {
    if (textStyle != null) {
        if (textStyle.metrics != null && length >= 1) {
            for (int i = offset; i < offset + length; i++) {
                try {
                    StyleRange styleRange = new StyleRange(textStyle);
                    String placeHolder = fDocument.get(i, 1);
                    InnerTag innerTag = InnerTagUtil.getInnerTag(fViewer.getInnerTagCacheList(), placeHolder);
                    if (innerTag != null) {
                        Point rect = innerTag.computeSize(SWT.DEFAULT, SWT.DEFAULT);
                        // int ascent = 4 * rect.height / 5 + SEGMENT_LINE_SPACING / 2;
                        // int descent = rect.height - ascent + SEGMENT_LINE_SPACING;
                        styleRange.metrics = new GlyphMetrics(rect.y, 0, rect.x + SEGMENT_LINE_SPACING * 2);
                    }
                    styleRange.start = i;
                    styleRange.length = 1;
                    presentation.addStyleRange(styleRange);
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
        }
    /*
			 * else { StyleRange styleRange = new StyleRange(textStyle); styleRange.start = offset; styleRange.length =
			 * length; presentation.addStyleRange(styleRange); }
			 */
    }
}
Also used : GlyphMetrics(org.eclipse.swt.graphics.GlyphMetrics) StyleRange(org.eclipse.swt.custom.StyleRange) InnerTag(net.heartsome.cat.common.ui.innertag.InnerTag) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 8 with GlyphMetrics

use of org.eclipse.swt.graphics.GlyphMetrics in project translationstudio8 by heartsome.

the class XGridCellRenderer method attachInnertTagStyle.

protected void attachInnertTagStyle(GC gc, TextLayout layout, boolean drawInnerTag) {
    String displayStr = layout.getText();
    List<InnerTagBean> innerTagBeans = innerTagFactory.getInnerTagBeans();
    Rectangle bounds = getBounds();
    for (InnerTagBean innerTagBean : innerTagBeans) {
        String placeHolder = placeHolderBuilder.getPlaceHolder(innerTagBeans, innerTagBeans.indexOf(innerTagBean));
        int start = displayStr.indexOf(placeHolder);
        if (start == -1) {
            continue;
        }
        TextStyle style = new TextStyle();
        Point rect = tagRender.calculateTagSize(innerTagBean);
        style.metrics = new GlyphMetrics(rect.y, 0, rect.x + SEGMENT_LINE_SPACING * 2);
        layout.setStyle(style, start, start + placeHolder.length() - 1);
        if (drawInnerTag && gc != null) {
            Point p = layout.getLocation(start, false);
            int x = bounds.x + p.x + leftMargin;
            x += SEGMENT_LINE_SPACING;
            Point tagSize = tagRender.calculateTagSize(innerTagBean);
            int lineIdx = layout.getLineIndex(start);
            Rectangle r = layout.getLineBounds(lineIdx);
            int y = bounds.y + p.y + topMargin + r.height / 2 - tagSize.y / 2;
            tagRender.draw(gc, innerTagBean, x, y - layout.getAscent());
        }
    }
}
Also used : TextStyle(org.eclipse.swt.graphics.TextStyle) GlyphMetrics(org.eclipse.swt.graphics.GlyphMetrics) Rectangle(org.eclipse.swt.graphics.Rectangle) InnerTagBean(net.heartsome.cat.common.innertag.InnerTagBean) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point)

Example 9 with GlyphMetrics

use of org.eclipse.swt.graphics.GlyphMetrics in project eclipse.platform.text by eclipse.

the class InlinedAnnotationDrawingStrategy method updateStyle.

/**
 * Returns the style to apply with GlyphMetrics width only if needed.
 *
 * @param annotation the line content annotation
 * @param style the current style and null otherwise.
 * @return the style to apply with GlyphMetrics width only if needed.
 */
static StyleRange updateStyle(LineContentAnnotation annotation, StyleRange style) {
    int width = annotation.getWidth();
    if (width == 0) {
        return null;
    }
    int fullWidth = width + annotation.getRedrawnCharacterWidth();
    if (style == null) {
        style = new StyleRange();
        Position position = annotation.getPosition();
        style.start = position.getOffset();
        style.length = 1;
    }
    GlyphMetrics metrics = style.metrics;
    if (!annotation.isMarkedDeleted()) {
        if (metrics == null) {
            metrics = new GlyphMetrics(0, 0, fullWidth);
        } else {
            if (metrics.width == fullWidth) {
                return null;
            }
            /**
             * We must create a new GlyphMetrics instance because comparison with similarTo used
             * later in StyledText#setStyleRange will compare the same (modified) and won't
             * realize an update happened.
             */
            metrics = new GlyphMetrics(0, 0, fullWidth);
        }
    } else {
        metrics = null;
    }
    style.metrics = metrics;
    return style;
}
Also used : Position(org.eclipse.jface.text.Position) GlyphMetrics(org.eclipse.swt.graphics.GlyphMetrics) StyleRange(org.eclipse.swt.custom.StyleRange) Point(org.eclipse.swt.graphics.Point)

Example 10 with GlyphMetrics

use of org.eclipse.swt.graphics.GlyphMetrics in project eclipse.platform.swt by eclipse.

the class TextEditor method addControl.

void addControl(Control control) {
    int offset = styledText.getCaretOffset();
    // $NON-NLS-1$
    styledText.replaceTextRange(offset, 0, "\uFFFC");
    StyleRange style = new StyleRange();
    Point size = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    int ascent = 2 * size.y / 3;
    int descent = size.y - ascent;
    style.metrics = new GlyphMetrics(ascent + MARGIN, descent + MARGIN, size.x + 2 * MARGIN);
    style.data = control;
    int[] ranges = { offset, 1 };
    StyleRange[] styles = { style };
    styledText.setStyleRanges(0, 0, ranges, styles);
    control.setSize(size);
}
Also used : GlyphMetrics(org.eclipse.swt.graphics.GlyphMetrics) StyleRange(org.eclipse.swt.custom.StyleRange) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point)

Aggregations

GlyphMetrics (org.eclipse.swt.graphics.GlyphMetrics)13 StyleRange (org.eclipse.swt.custom.StyleRange)10 Point (org.eclipse.swt.graphics.Point)8 Rectangle (org.eclipse.swt.graphics.Rectangle)3 TextStyle (org.eclipse.swt.graphics.TextStyle)3 InnerTagBean (net.heartsome.cat.common.innertag.InnerTagBean)2 Position (org.eclipse.jface.text.Position)2 Bullet (org.eclipse.swt.custom.Bullet)2 InnerTag (net.heartsome.cat.common.ui.innertag.InnerTag)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 TextLayout (org.eclipse.swt.graphics.TextLayout)1 Test (org.junit.Test)1