Search in sources :

Example 61 with StyleRange

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

the class TextEditor method installListeners.

void installListeners() {
    styledText.addCaretListener(event -> {
        updateStatusBar();
        updateToolBar();
    });
    styledText.addListener(SWT.MouseUp, event -> handleMouseUp(event));
    styledText.addListener(SWT.KeyDown, event -> handleKeyDown(event));
    styledText.addVerifyListener(event -> handleVerifyText(event));
    styledText.addModifyListener(event -> handleModify(event));
    styledText.addPaintObjectListener(event -> handlePaintObject(event));
    styledText.addListener(SWT.Dispose, event -> {
        StyleRange[] styles = styledText.getStyleRanges(0, styledText.getCharCount(), false);
        for (StyleRange style : styles) {
            Object data = style.data;
            if (data != null) {
                if (data instanceof Image)
                    ((Image) data).dispose();
                if (data instanceof Control)
                    ((Control) data).dispose();
            }
        }
    });
    shell.addControlListener(ControlListener.controlResizedAdapter(event -> handleResize(event)));
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) CoolBar(org.eclipse.swt.widgets.CoolBar) ToolBar(org.eclipse.swt.widgets.ToolBar) GC(org.eclipse.swt.graphics.GC) Point(org.eclipse.swt.graphics.Point) ColorDialog(org.eclipse.swt.widgets.ColorDialog) MenuEvent(org.eclipse.swt.events.MenuEvent) Composite(org.eclipse.swt.widgets.Composite) Bullet(org.eclipse.swt.custom.Bullet) Text(org.eclipse.swt.widgets.Text) ModifyEvent(org.eclipse.swt.events.ModifyEvent) Button(org.eclipse.swt.widgets.Button) MissingResourceException(java.util.MissingResourceException) GlyphMetrics(org.eclipse.swt.graphics.GlyphMetrics) Display(org.eclipse.swt.widgets.Display) ToolItem(org.eclipse.swt.widgets.ToolItem) MenuListener.menuShownAdapter(org.eclipse.swt.events.MenuListener.menuShownAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) SWT(org.eclipse.swt.SWT) PaintObjectEvent(org.eclipse.swt.custom.PaintObjectEvent) Label(org.eclipse.swt.widgets.Label) SelectionListener(org.eclipse.swt.events.SelectionListener) Image(org.eclipse.swt.graphics.Image) Rectangle(org.eclipse.swt.graphics.Rectangle) Spinner(org.eclipse.swt.widgets.Spinner) ControlListener(org.eclipse.swt.events.ControlListener) MenuAdapter(org.eclipse.swt.events.MenuAdapter) ImageData(org.eclipse.swt.graphics.ImageData) Event(org.eclipse.swt.widgets.Event) ControlEvent(org.eclipse.swt.events.ControlEvent) SelectionListener.widgetSelectedAdapter(org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter) VerifyEvent(org.eclipse.swt.events.VerifyEvent) ResourceBundle(java.util.ResourceBundle) StringTokenizer(java.util.StringTokenizer) Listener(org.eclipse.swt.widgets.Listener) RGB(org.eclipse.swt.graphics.RGB) Font(org.eclipse.swt.graphics.Font) GridData(org.eclipse.swt.layout.GridData) FillLayout(org.eclipse.swt.layout.FillLayout) Resource(org.eclipse.swt.graphics.Resource) Browser(org.eclipse.swt.browser.Browser) Combo(org.eclipse.swt.widgets.Combo) Shell(org.eclipse.swt.widgets.Shell) FileDialog(org.eclipse.swt.widgets.FileDialog) FileWriter(java.io.FileWriter) StyleRange(org.eclipse.swt.custom.StyleRange) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FontDialog(org.eclipse.swt.widgets.FontDialog) InputStreamReader(java.io.InputStreamReader) CoolItem(org.eclipse.swt.widgets.CoolItem) Color(org.eclipse.swt.graphics.Color) ST(org.eclipse.swt.custom.ST) FontData(org.eclipse.swt.graphics.FontData) MessageBox(org.eclipse.swt.widgets.MessageBox) BufferedReader(java.io.BufferedReader) Menu(org.eclipse.swt.widgets.Menu) Control(org.eclipse.swt.widgets.Control) InputStream(java.io.InputStream) GridLayout(org.eclipse.swt.layout.GridLayout) Control(org.eclipse.swt.widgets.Control) StyleRange(org.eclipse.swt.custom.StyleRange) Image(org.eclipse.swt.graphics.Image)

Example 62 with StyleRange

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

the class TextEditor method setBullet.

void setBullet(int type) {
    Point selection = styledText.getSelection();
    int lineStart = styledText.getLineAtOffset(selection.x);
    int lineEnd = styledText.getLineAtOffset(selection.y);
    StyleRange styleRange = new StyleRange();
    styleRange.metrics = new GlyphMetrics(0, 0, BULLET_WIDTH);
    Bullet bullet = new Bullet(type, styleRange);
    bullet.text = ".";
    for (int lineIndex = lineStart; lineIndex <= lineEnd; lineIndex++) {
        Bullet oldBullet = styledText.getLineBullet(lineIndex);
        styledText.setLineBullet(lineIndex, 1, oldBullet != null ? null : bullet);
    }
}
Also used : Bullet(org.eclipse.swt.custom.Bullet) GlyphMetrics(org.eclipse.swt.graphics.GlyphMetrics) StyleRange(org.eclipse.swt.custom.StyleRange) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point)

Example 63 with StyleRange

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

the class TextEditor method updateToolBar.

void updateToolBar() {
    styleState = 0;
    link = null;
    boolean bold = false, italic = false;
    Font font = null;
    int offset = styledText.getCaretOffset();
    StyleRange range = offset > 0 ? styledText.getStyleRangeAtOffset(offset - 1) : null;
    if (range != null) {
        if (range.font != null) {
            font = range.font;
            FontData[] fds = font.getFontData();
            for (FontData fd : fds) {
                int fontStyle = fd.getStyle();
                if (!bold && (fontStyle & SWT.BOLD) != 0)
                    bold = true;
                if (!italic && (fontStyle & SWT.ITALIC) != 0)
                    italic = true;
            }
        } else {
            bold = (range.fontStyle & SWT.BOLD) != 0;
            italic = (range.fontStyle & SWT.ITALIC) != 0;
        }
        if (range.foreground != null) {
            styleState |= FOREGROUND;
            if (textForeground != range.foreground) {
                disposeResource(textForeground);
                textForeground = range.foreground;
            }
        }
        if (range.background != null) {
            styleState |= BACKGROUND;
            if (textBackground != range.background) {
                disposeResource(textBackground);
                textBackground = range.background;
            }
        }
        if (range.underline) {
            switch(range.underlineStyle) {
                case SWT.UNDERLINE_SINGLE:
                    styleState |= UNDERLINE_SINGLE;
                    break;
                case SWT.UNDERLINE_DOUBLE:
                    styleState |= UNDERLINE_DOUBLE;
                    break;
                case SWT.UNDERLINE_SQUIGGLE:
                    styleState |= UNDERLINE_SQUIGGLE;
                    break;
                case SWT.UNDERLINE_ERROR:
                    styleState |= UNDERLINE_ERROR;
                    break;
                case SWT.UNDERLINE_LINK:
                    styleState |= UNDERLINE_LINK;
                    link = (String) range.data;
                    break;
            }
            if (range.underlineStyle != SWT.UNDERLINE_LINK) {
                underlineSingleItem.setSelection((styleState & UNDERLINE_SINGLE) != 0);
                underlineDoubleItem.setSelection((styleState & UNDERLINE_DOUBLE) != 0);
                underlineErrorItem.setSelection((styleState & UNDERLINE_ERROR) != 0);
                underlineSquiggleItem.setSelection((styleState & UNDERLINE_SQUIGGLE) != 0);
                disposeResource(underlineColor);
                underlineColor = range.underlineColor;
            }
        }
        if (range.strikeout) {
            styleState |= STRIKEOUT;
            disposeResource(strikeoutColor);
            strikeoutColor = range.strikeoutColor;
        }
        if (range.borderStyle != SWT.NONE) {
            switch(range.borderStyle) {
                case SWT.BORDER_SOLID:
                    styleState |= BORDER_SOLID;
                    break;
                case SWT.BORDER_DASH:
                    styleState |= BORDER_DASH;
                    break;
                case SWT.BORDER_DOT:
                    styleState |= BORDER_DOT;
                    break;
            }
            borderSolidItem.setSelection((styleState & BORDER_SOLID) != 0);
            borderDashItem.setSelection((styleState & BORDER_DASH) != 0);
            borderDotItem.setSelection((styleState & BORDER_DOT) != 0);
            disposeResource(borderColor);
            borderColor = range.borderColor;
        }
    }
    boldControl.setSelection(bold);
    italicControl.setSelection(italic);
    FontData fontData = font != null ? font.getFontData()[0] : styledText.getFont().getFontData()[0];
    int index = 0;
    int count = fontNameControl.getItemCount();
    String fontName = fontData.getName();
    while (index < count) {
        if (fontNameControl.getItem(index).equals(fontName)) {
            fontNameControl.select(index);
            break;
        }
        index++;
    }
    index = 0;
    count = fontSizeControl.getItemCount();
    int fontSize = fontData.getHeight();
    while (index < count) {
        int size = Integer.parseInt(fontSizeControl.getItem(index));
        if (fontSize == size) {
            fontSizeControl.select(index);
            break;
        }
        if (size > fontSize) {
            fontSizeControl.add(String.valueOf(fontSize), index);
            fontSizeControl.select(index);
            break;
        }
        index++;
    }
    disposeResource(textFont);
    textFont = font;
    int lineIndex = styledText.getLineAtOffset(offset);
    int alignment = styledText.getLineAlignment(lineIndex);
    leftAlignmentItem.setSelection((alignment & SWT.LEFT) != 0);
    centerAlignmentItem.setSelection((alignment & SWT.CENTER) != 0);
    rightAlignmentItem.setSelection((alignment & SWT.RIGHT) != 0);
    boolean justify = styledText.getLineJustify(lineIndex);
    justifyAlignmentItem.setSelection(justify);
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange) FontData(org.eclipse.swt.graphics.FontData) Font(org.eclipse.swt.graphics.Font) Point(org.eclipse.swt.graphics.Point)

Example 64 with StyleRange

use of org.eclipse.swt.custom.StyleRange 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 65 with StyleRange

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

the class TextEditor method handleModify.

void handleModify(ModifyEvent event) {
    if (newCharCount > 0 && start >= 0) {
        StyleRange style = new StyleRange();
        if (textFont != null && !textFont.equals(styledText.getFont())) {
            style.font = textFont;
        } else {
            style.fontStyle = SWT.NONE;
            if (boldControl.getSelection())
                style.fontStyle |= SWT.BOLD;
            if (italicControl.getSelection())
                style.fontStyle |= SWT.ITALIC;
        }
        if ((styleState & FOREGROUND) != 0) {
            style.foreground = textForeground;
        }
        if ((styleState & BACKGROUND) != 0) {
            style.background = textBackground;
        }
        int underlineStyle = styleState & UNDERLINE;
        if (underlineStyle != 0) {
            style.underline = true;
            style.underlineColor = underlineColor;
            switch(underlineStyle) {
                case UNDERLINE_SINGLE:
                    style.underlineStyle = SWT.UNDERLINE_SINGLE;
                    break;
                case UNDERLINE_DOUBLE:
                    style.underlineStyle = SWT.UNDERLINE_DOUBLE;
                    break;
                case UNDERLINE_SQUIGGLE:
                    style.underlineStyle = SWT.UNDERLINE_SQUIGGLE;
                    break;
                case UNDERLINE_ERROR:
                    style.underlineStyle = SWT.UNDERLINE_ERROR;
                    break;
                case UNDERLINE_LINK:
                    {
                        style.underlineColor = null;
                        if (link != null && link.length() > 0) {
                            style.underlineStyle = SWT.UNDERLINE_LINK;
                            style.data = link;
                        } else {
                            style.underline = false;
                        }
                        break;
                    }
            }
        }
        if ((styleState & STRIKEOUT) != 0) {
            style.strikeout = true;
            style.strikeoutColor = strikeoutColor;
        }
        int borderStyle = styleState & BORDER;
        if (borderStyle != 0) {
            style.borderColor = borderColor;
            switch(borderStyle) {
                case BORDER_DASH:
                    style.borderStyle = SWT.BORDER_DASH;
                    break;
                case BORDER_DOT:
                    style.borderStyle = SWT.BORDER_DOT;
                    break;
                case BORDER_SOLID:
                    style.borderStyle = SWT.BORDER_SOLID;
                    break;
            }
        }
        int[] ranges = { start, newCharCount };
        StyleRange[] styles = { style };
        styledText.setStyleRanges(start, newCharCount, ranges, styles);
    }
    disposeRanges(selectedRanges);
}
Also used : StyleRange(org.eclipse.swt.custom.StyleRange) Point(org.eclipse.swt.graphics.Point)

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