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)));
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations