Search in sources :

Example 31 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.text by eclipse.

the class WhitespaceCharacterPainter method drawCharRange.

/**
 * Draw characters of content range.
 *
 * @param gc the GC
 * @param startOffset inclusive start index of the drawing range
 * @param endOffset exclusive end index of the drawing range
 * @param lineOffset inclusive start index of the line
 * @param lineEndOffset exclusive end index of the line
 * @param spaceCharsAreSameWidth whether or not all space chars are same width, if <code>true</code>
 *            rendering can be optimized
 */
private void drawCharRange(GC gc, int startOffset, int endOffset, int lineOffset, int lineEndOffset, boolean spaceCharsAreSameWidth) {
    StyledTextContent content = fTextWidget.getContent();
    String lineText = content.getTextRange(lineOffset, lineEndOffset - lineOffset);
    int startOffsetInLine = startOffset - lineOffset;
    int endOffsetInLine = endOffset - lineOffset;
    int textBegin = -1;
    for (int i = 0; i < lineText.length(); ++i) {
        if (!isWhitespaceCharacter(lineText.charAt(i))) {
            textBegin = i;
            break;
        }
    }
    boolean isEmptyLine = textBegin == -1;
    int textEnd = lineText.length() - 1;
    if (!isEmptyLine) {
        for (int i = lineText.length() - 1; i >= 0; --i) {
            if (!isWhitespaceCharacter(lineText.charAt(i))) {
                textEnd = i;
                break;
            }
        }
    }
    StyleRange styleRange = null;
    Color fg = null;
    StringBuilder visibleChar = new StringBuilder(10);
    int delta = 0;
    for (int textOffset = startOffsetInLine; textOffset <= endOffsetInLine; ++textOffset) {
        boolean eol = false;
        delta++;
        if (textOffset < endOffsetInLine) {
            char c = lineText.charAt(textOffset);
            switch(c) {
                case ' ':
                    if (isEmptyLine) {
                        if (fShowLeadingSpaces || fShowEnclosedSpace || fShowTrailingSpaces) {
                            visibleChar.append(SPACE_SIGN);
                        }
                    } else if (textOffset < textBegin) {
                        if (fShowLeadingSpaces) {
                            visibleChar.append(SPACE_SIGN);
                        }
                    } else if (textOffset < textEnd) {
                        if (fShowEnclosedSpace) {
                            visibleChar.append(SPACE_SIGN);
                        }
                    } else {
                        if (fShowTrailingSpaces) {
                            visibleChar.append(SPACE_SIGN);
                        }
                    }
                    // it can be used only for monospace fonts
                    if (spaceCharsAreSameWidth) {
                        continue;
                    }
                    break;
                case // ideographic whitespace
                '\u3000':
                    if (isEmptyLine) {
                        if (fShowLeadingIdeographicSpaces || fShowEnclosedIdeographicSpaces || fShowTrailingIdeographicSpaces) {
                            visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
                        }
                    } else if (textOffset < textBegin) {
                        if (fShowLeadingIdeographicSpaces) {
                            visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
                        }
                    } else if (textOffset < textEnd) {
                        if (fShowEnclosedIdeographicSpaces) {
                            visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
                        }
                    } else {
                        if (fShowTrailingIdeographicSpaces) {
                            visibleChar.append(IDEOGRAPHIC_SPACE_SIGN);
                        }
                    }
                    // it can be used only for monospace fonts
                    if (spaceCharsAreSameWidth) {
                        continue;
                    }
                    break;
                case '\t':
                    if (isEmptyLine) {
                        if (fShowLeadingTabs || fShowEnclosedTabs || fShowTrailingTabs) {
                            visibleChar.append(TAB_SIGN);
                        }
                    } else if (textOffset < textBegin) {
                        if (fShowLeadingTabs) {
                            visibleChar.append(TAB_SIGN);
                        }
                    } else if (textOffset < textEnd) {
                        if (fShowEnclosedTabs) {
                            visibleChar.append(TAB_SIGN);
                        }
                    } else {
                        if (fShowTrailingTabs) {
                            visibleChar.append(TAB_SIGN);
                        }
                    }
                    break;
                case '\r':
                    if (fShowCarriageReturn) {
                        visibleChar.append(CARRIAGE_RETURN_SIGN);
                    }
                    if (textOffset >= endOffsetInLine - 1 || lineText.charAt(textOffset + 1) != '\n') {
                        eol = true;
                        break;
                    }
                    continue;
                case '\n':
                    if (fShowLineFeed) {
                        visibleChar.append(LINE_FEED_SIGN);
                    }
                    eol = true;
                    break;
                default:
                    break;
            }
        }
        if (visibleChar.length() > 0) {
            int widgetOffset = startOffset + textOffset - startOffsetInLine - delta + 1;
            if (!eol || !isFoldedLine(content.getLineAtOffset(widgetOffset))) {
                /*
					 * Block selection is drawn using alpha and no selection-inverting
					 * takes place, we always draw as 'unselected' in block selection mode.
					 */
                if (!fTextWidget.getBlockSelection() && fIsFullSelectionStyle && isOffsetSelected(fTextWidget, widgetOffset)) {
                    fg = fTextWidget.getSelectionForeground();
                } else if (styleRange == null || styleRange.start + styleRange.length <= widgetOffset) {
                    styleRange = fTextWidget.getStyleRangeAtOffset(widgetOffset);
                    if (styleRange == null || styleRange.foreground == null) {
                        fg = fTextWidget.getForeground();
                    } else {
                        fg = styleRange.foreground;
                    }
                }
                draw(gc, widgetOffset, visibleChar.toString(), fg);
            }
            visibleChar.delete(0, visibleChar.length());
        }
        delta = 0;
    }
}
Also used : StyledTextContent(org.eclipse.swt.custom.StyledTextContent) StyleRange(org.eclipse.swt.custom.StyleRange) Color(org.eclipse.swt.graphics.Color) Point(org.eclipse.swt.graphics.Point)

Example 32 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.text by eclipse.

the class TableOwnerDrawSupport method updateTextLayout.

private int updateTextLayout(TableItem item, int index, boolean isSelected) {
    fSharedLayout.setFont(item.getFont(index));
    // XXX: needed to clear the style info, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=226090
    // $NON-NLS-1$
    fSharedLayout.setText("");
    fSharedLayout.setText(item.getText(index));
    // text width without any styles
    int originalTextWidth = fSharedLayout.getBounds().width;
    StyleRange[] ranges = getStyledRanges(item, index);
    if (ranges != null) {
        for (StyleRange range : ranges) {
            StyleRange curr = range;
            if (isSelected) {
                curr = (StyleRange) curr.clone();
                curr.foreground = null;
                curr.background = null;
            }
            fSharedLayout.setStyle(curr, curr.start, curr.start + curr.length - 1);
        }
    }
    return fSharedLayout.getBounds().width - originalTextWidth;
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange)

Example 33 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.text by eclipse.

the class BrowserInformationControl method computeSizeHint.

@Override
public Point computeSizeHint() {
    Point sizeConstraints = getSizeConstraints();
    Rectangle trim = computeTrim();
    int height = trim.height;
    // FIXME: The HTML2TextReader does not render <p> like a browser.
    // Instead of inserting an empty line, it just adds a single line break.
    // Furthermore, the indentation of <dl><dd> elements is too small (e.g with a long @see line)
    TextPresentation presentation = new TextPresentation();
    String text;
    try (HTML2TextReader reader = new HTML2TextReader(new StringReader(fInput.getHtml()), presentation)) {
        text = reader.getString();
    } catch (IOException e) {
        // $NON-NLS-1$
        text = "";
    }
    fTextLayout.setText(text);
    fTextLayout.setWidth(sizeConstraints == null ? SWT.DEFAULT : sizeConstraints.x - trim.width);
    Iterator<StyleRange> iter = presentation.getAllStyleRangeIterator();
    while (iter.hasNext()) {
        StyleRange sr = iter.next();
        if (sr.fontStyle == SWT.BOLD)
            fTextLayout.setStyle(fBoldStyle, sr.start, sr.start + sr.length - 1);
    }
    // does not return minimum width, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=217446
    Rectangle bounds = fTextLayout.getBounds();
    int lineCount = fTextLayout.getLineCount();
    int textWidth = 0;
    for (int i = 0; i < lineCount; i++) {
        Rectangle rect = fTextLayout.getLineBounds(i);
        int lineWidth = rect.x + rect.width;
        if (i == 0)
            lineWidth += fInput.getLeadingImageWidth();
        textWidth = Math.max(textWidth, lineWidth);
    }
    bounds.width = textWidth;
    // $NON-NLS-1$
    fTextLayout.setText("");
    int minWidth = bounds.width;
    height = height + bounds.height;
    // Add some air to accommodate for different browser renderings
    minWidth += 15;
    height += 15;
    // Apply max size constraints
    if (sizeConstraints != null) {
        if (sizeConstraints.x != SWT.DEFAULT)
            minWidth = Math.min(sizeConstraints.x, minWidth + trim.width);
        if (sizeConstraints.y != SWT.DEFAULT)
            height = Math.min(sizeConstraints.y, height);
    }
    // Ensure minimal size
    int width = Math.max(MIN_WIDTH, minWidth);
    height = Math.max(MIN_HEIGHT, height);
    return new Point(width, height);
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange) Rectangle(org.eclipse.swt.graphics.Rectangle) StringReader(java.io.StringReader) Point(org.eclipse.swt.graphics.Point) IOException(java.io.IOException) Point(org.eclipse.swt.graphics.Point) TextPresentation(org.eclipse.jface.text.TextPresentation)

Example 34 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.text by eclipse.

the class HTMLTextPresenter method adaptTextPresentation.

protected void adaptTextPresentation(TextPresentation presentation, int offset, int insertLength) {
    int yoursStart = offset;
    int yoursEnd = offset + insertLength - 1;
    yoursEnd = Math.max(yoursStart, yoursEnd);
    Iterator<StyleRange> e = presentation.getAllStyleRangeIterator();
    while (e.hasNext()) {
        StyleRange range = e.next();
        int myStart = range.start;
        int myEnd = range.start + range.length - 1;
        myEnd = Math.max(myStart, myEnd);
        if (myEnd < yoursStart)
            continue;
        if (myStart < yoursStart)
            range.length += insertLength;
        else
            range.start += insertLength;
    }
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange)

Example 35 with StyleRange

use of org.eclipse.swt.custom.StyleRange in project eclipse.platform.text by eclipse.

the class CompletionProposalPopup2 method setProposals.

/**
 * Initializes the proposal selector with these given proposals.
 *
 * @param proposals the proposals
 */
private void setProposals(ICompletionProposal[] proposals) {
    if (Helper2.okToUse(fProposalTable)) {
        ICompletionProposal oldProposal = getSelectedProposal();
        if (oldProposal instanceof ICompletionProposalExtension2)
            ((ICompletionProposalExtension2) oldProposal).unselected(fViewer);
        fFilteredProposals = proposals;
        int selectionIndex = 0;
        fProposalTable.setRedraw(false);
        try {
            fProposalTable.removeAll();
            Point selection = fViewer.getSelectedRange();
            int endOffset;
            endOffset = selection.x + selection.y;
            IDocument document = fViewer.getDocument();
            boolean validate = false;
            if (selection.y != 0 && document != null)
                validate = true;
            TableItem item;
            ICompletionProposal p;
            for (int i = 0; i < proposals.length; i++) {
                p = proposals[i];
                item = new TableItem(fProposalTable, SWT.NULL);
                if (p.getImage() != null)
                    item.setImage(p.getImage());
                String displayString;
                StyleRange[] styleRanges = null;
                if (fIsColoredLabelsSupportEnabled && p instanceof ICompletionProposalExtension6) {
                    StyledString styledString = ((ICompletionProposalExtension6) p).getStyledDisplayString();
                    displayString = styledString.getString();
                    styleRanges = styledString.getStyleRanges();
                } else
                    displayString = p.getDisplayString();
                item.setText(displayString);
                if (fIsColoredLabelsSupportEnabled)
                    TableOwnerDrawSupport.storeStyleRanges(item, 0, styleRanges);
                item.setData(p);
                if (validate && validateProposal(document, p, endOffset, null)) {
                    selectionIndex = i;
                    validate = false;
                }
            }
        } finally {
            fProposalTable.setRedraw(true);
        }
        resizeProposalSelector(false);
        selectProposal(selectionIndex, false);
    }
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) TableItem(org.eclipse.swt.widgets.TableItem) StyleRange(org.eclipse.swt.custom.StyleRange) ICompletionProposalExtension2(org.eclipse.jface.text.contentassist.ICompletionProposalExtension2) Point(org.eclipse.swt.graphics.Point) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) Point(org.eclipse.swt.graphics.Point) IDocument(org.eclipse.jface.text.IDocument) ICompletionProposalExtension6(org.eclipse.jface.text.contentassist.ICompletionProposalExtension6)

Aggregations

StyleRange (org.eclipse.swt.custom.StyleRange)145 Point (org.eclipse.swt.graphics.Point)52 Test (org.junit.Test)39 ArrayList (java.util.ArrayList)25 Color (org.eclipse.swt.graphics.Color)22 StyledText (org.eclipse.swt.custom.StyledText)13 Font (org.eclipse.swt.graphics.Font)10 GlyphMetrics (org.eclipse.swt.graphics.GlyphMetrics)10 Matcher (java.util.regex.Matcher)9 FontData (org.eclipse.swt.graphics.FontData)8 RGB (org.eclipse.swt.graphics.RGB)8 Rectangle (org.eclipse.swt.graphics.Rectangle)7 IOException (java.io.IOException)5 TextPresentation (org.eclipse.jface.text.TextPresentation)5 Image (org.eclipse.swt.graphics.Image)5 Control (org.eclipse.swt.widgets.Control)5 Position (org.eclipse.jface.text.Position)4 TextStyle (org.eclipse.swt.graphics.TextStyle)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3