Search in sources :

Example 11 with TextLayout

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

the class GridCopyEnable method handleVerticalScroll.

void handleVerticalScroll(Event event) {
    GridVisibleRange visibleR = gridTable.getVisibleRange();
    GridItem[] items = visibleR.getItems();
    boolean itemFlg = false;
    for (GridItem item : items) {
        if (focusItem == item) {
            itemFlg = true;
        }
    }
    boolean columnFlg = false;
    GridColumn[] columns = visibleR.getColumns();
    if (columns.length - 1 >= focusColIndex) {
        columnFlg = true;
    }
    if (!itemFlg || !columnFlg) {
        defaultCaret.setVisible(false);
        return;
    }
    defaultCaret.setVisible(true);
    GridColumn col = gridTable.getColumn(focusColIndex);
    GridCellRenderer gcr = col.getCellRenderer();
    int colIndex = gcr.getColumn();
    if (gcr == null || !(gcr instanceof XGridCellRenderer) || !copyAbleColumnIndexs.contains(colIndex)) {
        return;
    }
    XGridCellRenderer cellRender = (XGridCellRenderer) gcr;
    Rectangle cellBounds = focusItem.getBounds(colIndex);
    GC gc = new GC(Display.getDefault());
    TextLayout layout = null;
    try {
        layout = cellRender.getTextLayout(gc, focusItem, colIndex, true, false);
        if (layout == null) {
            gc.dispose();
            return;
        }
        Point point = layout.getLocation(caretOffset, false);
        coordinateOffsetX = cellBounds.x + cellRender.leftMargin;
        coordinateOffsetY = cellBounds.y + cellRender.topMargin + cellRender.textTopMargin;
        defaultCaret.setLocation(point.x + coordinateOffsetX, point.y + coordinateOffsetY);
    } finally {
        if (layout != null) {
            layout.dispose();
        }
        if (gc != null) {
            gc.dispose();
        }
    }
}
Also used : GridVisibleRange(org.eclipse.nebula.widgets.grid.Grid.GridVisibleRange) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout) GridItem(org.eclipse.nebula.widgets.grid.GridItem) GridCellRenderer(org.eclipse.nebula.widgets.grid.GridCellRenderer) GridColumn(org.eclipse.nebula.widgets.grid.GridColumn) GC(org.eclipse.swt.graphics.GC)

Example 12 with TextLayout

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

the class AttributeTextCellRenderer method draw.

public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row, IColumn column, boolean drawFocus, boolean selected, boolean printing) {
    super.draw(gc, jaretTable, cellStyle, drawingArea, row, column, drawFocus, selected, printing);
    // Color bg = gc.getBackground();
    // Color fg = gc.getForeground();
    Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
    Rectangle rect = applyInsets(drect);
    String s = convertValue(row, column);
    if (s != null && mapStyle != null && mapStyle.containsKey(row)) {
        if (selected && !printing) {
            Color color = ColorManager.getColorManager(Display.getCurrent()).getColor(new RGB(218, 218, 218));
            gc.setBackground(color);
        } else {
            gc.setBackground(getBackgroundColor(cellStyle, printing));
        }
        if (textLayout == null) {
            textLayout = new TextLayout(gc.getDevice());
            jaretTable.getParent().addDisposeListener(new DisposeListener() {

                public void widgetDisposed(DisposeEvent e) {
                    textLayout.dispose();
                }
            });
        }
        textLayout.setOrientation(jaretTable.getOrientation());
        textLayout.setText(s);
        textLayout.setFont(gc.getFont());
        textLayout.setWidth(rect.width);
        ArrayList<int[]> lstIndex = mapStyle.get(row);
        if (lstIndex != null) {
            for (int[] arrIndex : lstIndex) {
                if (arrIndex != null && arrIndex.length == 2) {
                    textLayout.setStyle(style, arrIndex[0], arrIndex[1] - 1);
                }
            }
            gc.fillRectangle(rect);
            textLayout.draw(gc, rect.x, rect.y);
            gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
        }
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) RGB(org.eclipse.swt.graphics.RGB) DisposeEvent(org.eclipse.swt.events.DisposeEvent) TextLayout(org.eclipse.swt.graphics.TextLayout)

Example 13 with TextLayout

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

the class TextPainterWithPadding method getCellTextLayout.

private TextLayout getCellTextLayout(LayerCell cell) {
    int orientation = editor.getTable().getStyle() & (SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT);
    TextLayout layout = new TextLayout(editor.getTable().getDisplay());
    layout.setOrientation(orientation);
    layout.setSpacing(Constants.SEGMENT_LINE_SPACING);
    layout.setFont(font);
    layout.setAscent(ascent);
    // 和 StyledTextEditor 同步
    layout.setDescent(descent);
    layout.setTabs(new int[] { tabWidth });
    Rectangle rectangle = cell.getBounds();
    int width = rectangle.width - leftPadding - rightPadding;
    width -= 1;
    if (wrapText && width > 0) {
        layout.setWidth(width);
    }
    String displayText = InnerTagUtil.resolveTag(innerTagFactory.parseInnerTag((String) cell.getDataValue()));
    if (XliffEditorParameter.getInstance().isShowNonpirnttingCharacter()) {
        displayText = displayText.replaceAll("\\n", Constants.LINE_SEPARATOR_CHARACTER + "\n");
        displayText = displayText.replaceAll("\\t", Constants.TAB_CHARACTER + "​");
        displayText = displayText.replaceAll(" ", Constants.SPACE_CHARACTER + "​");
    }
    layout.setText(displayText);
    List<InnerTagBean> innerTagBeans = innerTagFactory.getInnerTagBeans();
    for (InnerTagBean innerTagBean : innerTagBeans) {
        String placeHolder = placeHolderBuilder.getPlaceHolder(innerTagBeans, innerTagBeans.indexOf(innerTagBean));
        int start = displayText.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);
    }
    return layout;
}
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) TextLayout(org.eclipse.swt.graphics.TextLayout)

Example 14 with TextLayout

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

the class TextPainterWithPadding method setFont.

public void setFont(Font font) {
    TextLayout layout = new TextLayout(Display.getDefault());
    try {
        if (font != null) {
            this.font = font;
            Font boldFont = getFont(SWT.BOLD), italicFont = getFont(SWT.ITALIC), boldItalicFont = getFont(SWT.BOLD | SWT.ITALIC);
            layout.setText("    ");
            layout.setFont(font);
            layout.setStyle(new TextStyle(font, null, null), 0, 0);
            layout.setStyle(new TextStyle(boldFont, null, null), 1, 1);
            layout.setStyle(new TextStyle(italicFont, null, null), 2, 2);
            layout.setStyle(new TextStyle(boldItalicFont, null, null), 3, 3);
            FontMetrics metrics = layout.getLineMetrics(0);
            ascent = metrics.getAscent() + metrics.getLeading();
            descent = metrics.getDescent();
            boldFont.dispose();
            italicFont.dispose();
            boldItalicFont.dispose();
            boldFont = italicFont = boldItalicFont = null;
        }
        layout.dispose();
        layout = new TextLayout(Display.getDefault());
        layout.setFont(this.font);
        StringBuffer tabBuffer = new StringBuffer(tabSize);
        for (int i = 0; i < tabSize; i++) {
            tabBuffer.append(' ');
        }
        layout.setText(tabBuffer.toString());
        tabWidth = layout.getBounds().width;
        layout.dispose();
    } finally {
        if (layout != null && !layout.isDisposed()) {
            layout.dispose();
        }
    }
}
Also used : TextStyle(org.eclipse.swt.graphics.TextStyle) FontMetrics(org.eclipse.swt.graphics.FontMetrics) Font(org.eclipse.swt.graphics.Font) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout)

Example 15 with TextLayout

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

the class TextPainterWithPadding method getPreferredHeight.

public int getPreferredHeight(LayerCell cell, GC gc, IConfigRegistry configRegistry) {
    if (innerTagFactory == null) {
        innerTagFactory = new XliffInnerTagFactory(placeHolderBuilder);
    }
    innerTagFactory.reset();
    TextLayout layout = getCellTextLayout(cell);
    int counts = layout.getLineCount();
    int contentHeight = 0;
    for (int i = 0; i < counts; i++) {
        contentHeight += layout.getLineBounds(i).height;
    }
    layout.dispose();
    contentHeight += Math.max(counts - 1, 0) * SEGMENT_LINE_SPACING;
    // 加上编辑模式下,StyledTextCellEditor的边框
    contentHeight += 4;
    contentHeight += topPadding;
    contentHeight += bottomPadding;
    return contentHeight;
}
Also used : XliffInnerTagFactory(net.heartsome.cat.common.innertag.factory.XliffInnerTagFactory) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout)

Aggregations

TextLayout (org.eclipse.swt.graphics.TextLayout)26 Point (org.eclipse.swt.graphics.Point)19 GridItem (org.eclipse.nebula.widgets.grid.GridItem)13 Color (org.eclipse.swt.graphics.Color)10 Image (org.eclipse.swt.graphics.Image)10 Rectangle (org.eclipse.swt.graphics.Rectangle)10 DisposeEvent (org.eclipse.swt.events.DisposeEvent)9 DisposeListener (org.eclipse.swt.events.DisposeListener)9 TextStyle (org.eclipse.swt.graphics.TextStyle)7 Font (org.eclipse.swt.graphics.Font)4 Matcher (java.util.regex.Matcher)3 ArrayList (java.util.ArrayList)2 Pattern (java.util.regex.Pattern)2 InnerTagBean (net.heartsome.cat.common.innertag.InnerTagBean)2 XliffInnerTagFactory (net.heartsome.cat.common.innertag.factory.XliffInnerTagFactory)2 GridColumn (org.eclipse.nebula.widgets.grid.GridColumn)2 StyleRange (org.eclipse.swt.custom.StyleRange)2 RGB (org.eclipse.swt.graphics.RGB)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1