Point getSize (int maxWidth) { int textWidth = 0, messageWidth = 0; if (layoutText != null) { layoutText.setWidth (-1); textWidth = layoutText.getBounds ().width; } if (layoutMessage != null) { layoutMessage.setWidth (-1); messageWidth = layoutMessage.getBounds ().width; } int messageTrim = 2 * INSET + 2 * BORDER + 2 * PADDING; boolean hasImage = layoutText != null && (style & SWT.BALLOON) != 0 && (style & (SWT.ICON_ERROR | SWT.ICON_INFORMATION | SWT.ICON_WARNING)) != 0; int textTrim = messageTrim + (hasImage ? IMAGE_SIZE : 0); int width = Math.min (maxWidth, Math.max (textWidth + textTrim, messageWidth + messageTrim)); int textHeight = 0, messageHeight = 0; if (layoutText != null) { layoutText.setWidth (maxWidth - textTrim); textHeight = layoutText.getBounds ().height; } if (layoutMessage != null) { layoutMessage.setWidth (maxWidth - messageTrim); messageHeight = layoutMessage.getBounds ().height; } int height = 2 * BORDER + 2 * PADDING + messageHeight; if (layoutText != null) height += Math.max (IMAGE_SIZE, textHeight) + 2 * PADDING; return new Point (width, height); }
Rectangle [] getRectanglesInPixels (int linkIndex) { int lineCount = layout.getLineCount (); Rectangle [] rects = new Rectangle [lineCount]; int [] lineOffsets = layout.getLineOffsets (); Point point = offsets [linkIndex]; int lineStart = 1; while (point.x > lineOffsets [lineStart]) lineStart++; int lineEnd = 1; while (point.y > lineOffsets [lineEnd]) lineEnd++; int index = 0; if (lineStart == lineEnd) { rects [index++] = DPIUtil.autoScaleUp(layout.getBounds (point.x, point.y)); } else { rects [index++] = DPIUtil.autoScaleUp(layout.getBounds (point.x, lineOffsets [lineStart]-1)); rects [index++] = DPIUtil.autoScaleUp(layout.getBounds (lineOffsets [lineEnd-1], point.y)); if (lineEnd - lineStart > 1) { for (int i = lineStart; i < lineEnd - 1; i++) { rects [index++] = DPIUtil.autoScaleUp(layout.getLineBounds (i)); } } } if (rects.length != index) { Rectangle [] tmp = new Rectangle [index]; System.arraycopy (rects, 0, tmp, 0, index); rects = tmp; } return rects; }
void calculate(int startLine, int lineCount) { int endLine = startLine + lineCount; if (startLine < 0 || endLine > lineWidth.length) { return; } int hTrim = styledText.leftMargin + styledText.rightMargin + styledText.getCaretWidth(); for (int i = startLine; i < endLine; i++) { if (lineWidth[i] == -1 || lineHeight[i] == -1) { TextLayout layout = getTextLayout(i); Rectangle rect = layout.getBounds(); lineWidth[i] = rect.width + hTrim; lineHeight[i] = rect.height; disposeTextLayout(layout); } if (lineWidth[i] > maxWidth) { maxWidth = lineWidth[i]; maxWidthLineIndex = i; } } } void calculateClientArea () {
void calculate(int startLine, int lineCount) { int endLine = startLine + lineCount; if (startLine < 0 || endLine > lineWidth.length) { return; } int hTrim = styledText.leftMargin + styledText.rightMargin + styledText.getCaretWidth(); for (int i = startLine; i < endLine; i++) { if (lineWidth[i] == -1 || lineHeight[i] == -1) { TextLayout layout = getTextLayout(i); Rectangle rect = layout.getBounds(); lineWidth[i] = rect.width + hTrim; lineHeight[i] = rect.height; disposeTextLayout(layout); } if (lineWidth[i] > maxWidth) { maxWidth = lineWidth[i]; maxWidthLineIndex = i; } } } void calculateClientArea () {
void calculate(int startLine, int lineCount) { int endLine = startLine + lineCount; if (startLine < 0 || endLine > lineWidth.length) { return; } int hTrim = styledText.leftMargin + styledText.rightMargin + styledText.getCaretWidth(); for (int i = startLine; i < endLine; i++) { if (lineWidth[i] == -1 || lineHeight[i] == -1) { TextLayout layout = getTextLayout(i); Rectangle rect = layout.getBounds(); lineWidth[i] = rect.width + hTrim; lineHeight[i] = rect.height; disposeTextLayout(layout); } if (lineWidth[i] > maxWidth) { maxWidth = lineWidth[i]; maxWidthLineIndex = i; } } } void calculateClientArea () {
void calculate(int startLine, int lineCount) { int endLine = startLine + lineCount; if (startLine < 0 || endLine > lineWidth.length) { return; } int hTrim = styledText.leftMargin + styledText.rightMargin + styledText.getCaretWidth(); for (int i = startLine; i < endLine; i++) { if (lineWidth[i] == -1 || lineHeight[i] == -1) { TextLayout layout = getTextLayout(i); Rectangle rect = layout.getBounds(); lineWidth[i] = rect.width + hTrim; lineHeight[i] = rect.height; disposeTextLayout(layout); } if (lineWidth[i] > maxWidth) { maxWidth = lineWidth[i]; maxWidthLineIndex = i; } } } void calculateClientArea () {
void calculate(int startLine, int lineCount) { int endLine = startLine + lineCount; if (startLine < 0 || endLine > lineWidth.length) { return; } int hTrim = styledText.leftMargin + styledText.rightMargin + styledText.getCaretWidth(); for (int i = startLine; i < endLine; i++) { if (lineWidth[i] == -1 || lineHeight[i] == -1) { TextLayout layout = getTextLayout(i); Rectangle rect = layout.getBounds(); lineWidth[i] = rect.width + hTrim; lineHeight[i] = rect.height; disposeTextLayout(layout); } if (lineWidth[i] > maxWidth) { maxWidth = lineWidth[i]; maxWidthLineIndex = i; } } } void calculateClientArea () {
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 fSharedLayout.setText(""); //$NON-NLS-1$ fSharedLayout.setText(item.getText(index)); int originalTextWidth= fSharedLayout.getBounds().width; // text width without any styles StyleRange[] ranges= getStyledRanges(item, index); if (ranges != null) { for (int i= 0; i < ranges.length; i++) { StyleRange curr= ranges[i]; 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; }
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 fSharedLayout.setText(""); //$NON-NLS-1$ fSharedLayout.setText(item.getText(index)); int originalTextWidth= fSharedLayout.getBounds().width; // text width without any styles 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; }
@Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) { checkWidget (); if (wHint != SWT.DEFAULT && wHint < 0) wHint = 0; if (hHint != SWT.DEFAULT && hHint < 0) hHint = 0; int width, height; int layoutWidth = layout.getWidth (); //TEMPORARY CODE if (wHint == 0) { layout.setWidth (1); Rectangle rect = DPIUtil.autoScaleUp(layout.getBounds ()); width = 0; height = rect.height; } else { layout.setWidth (DPIUtil.autoScaleDown(wHint)); Rectangle rect = DPIUtil.autoScaleUp(layout.getBounds ()); width = rect.width; height = rect.height; } layout.setWidth (layoutWidth); if (wHint != SWT.DEFAULT) width = wHint; if (hHint != SWT.DEFAULT) height = hHint; int border = getBorderWidthInPixels (); width += border * 2; height += border * 2; return new Point (width, height); }
@Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) { checkWidget (); if (wHint != SWT.DEFAULT && wHint < 0) wHint = 0; if (hHint != SWT.DEFAULT && hHint < 0) hHint = 0; int width, height; int layoutWidth = layout.getWidth (); //TEMPORARY CODE if (wHint == 0) { layout.setWidth (1); Rectangle rect = DPIUtil.autoScaleUp(layout.getBounds ()); width = 0; height = rect.height; } else { layout.setWidth (DPIUtil.autoScaleDown(wHint)); Rectangle rect = DPIUtil.autoScaleUp(layout.getBounds ()); width = rect.width; height = rect.height; } layout.setWidth (layoutWidth); if (wHint != SWT.DEFAULT) width = wHint; if (hHint != SWT.DEFAULT) height = hHint; int border = getBorderWidthInPixels (); width += border * 2; height += border * 2; return new Point (width, height); }
@Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) { checkWidget (); if (wHint != SWT.DEFAULT && wHint < 0) wHint = 0; if (hHint != SWT.DEFAULT && hHint < 0) hHint = 0; int width, height; int layoutWidth = layout.getWidth (); //TEMPORARY CODE if (wHint == 0) { layout.setWidth (1); Rectangle rect = DPIUtil.autoScaleUp(layout.getBounds ()); width = 0; height = rect.height; } else { layout.setWidth (DPIUtil.autoScaleDown(wHint)); Rectangle rect = DPIUtil.autoScaleUp(layout.getBounds ()); width = rect.width; height = rect.height; } layout.setWidth (layoutWidth); if (wHint != SWT.DEFAULT) width = wHint; if (hHint != SWT.DEFAULT) height = hHint; int border = getBorderWidthInPixels (); width += border * 2; height += border * 2; return new Point (width, height); }
void drawBullet(Bullet bullet, GC gc, int paintX, int paintY, int index, int lineAscent, int lineDescent) { StyleRange style = bullet.style; GlyphMetrics metrics = style.metrics; Color color = style.foreground; if (color != null) gc.setForeground(color); Font font = style.font; if (font != null) gc.setFont(font); String string = ""; int type = bullet.type & (ST.BULLET_DOT|ST.BULLET_NUMBER|ST.BULLET_LETTER_LOWER|ST.BULLET_LETTER_UPPER); switch (type) { case ST.BULLET_DOT: string = "\u2022"; break; case ST.BULLET_NUMBER: string = String.valueOf(index + 1); break; case ST.BULLET_LETTER_LOWER: string = String.valueOf((char) (index % 26 + 97)); break; case ST.BULLET_LETTER_UPPER: string = String.valueOf((char) (index % 26 + 65)); break; } if ((bullet.type & ST.BULLET_TEXT) != 0) string += bullet.text; Display display = styledText.getDisplay(); TextLayout layout = new TextLayout(display); layout.setText(string); layout.setAscent(lineAscent); layout.setDescent(lineDescent); style = (StyleRange)style.clone(); style.metrics = null; if (style.font == null) style.font = getFont(style.fontStyle); layout.setStyle(style, 0, string.length()); int x = paintX + Math.max(0, metrics.width - layout.getBounds().width - BULLET_MARGIN); layout.draw(gc, x, paintY); layout.dispose(); } int drawLine(int lineIndex, int paintX, int paintY, GC gc, Color widgetBackground, Color widgetForeground) {
void drawBullet(Bullet bullet, GC gc, int paintX, int paintY, int index, int lineAscent, int lineDescent) { StyleRange style = bullet.style; GlyphMetrics metrics = style.metrics; Color color = style.foreground; if (color != null) gc.setForeground(color); Font font = style.font; if (font != null) gc.setFont(font); String string = ""; int type = bullet.type & (ST.BULLET_DOT|ST.BULLET_NUMBER|ST.BULLET_LETTER_LOWER|ST.BULLET_LETTER_UPPER); switch (type) { case ST.BULLET_DOT: string = "\u2022"; break; case ST.BULLET_NUMBER: string = String.valueOf(index + 1); break; case ST.BULLET_LETTER_LOWER: string = String.valueOf((char) (index % 26 + 97)); break; case ST.BULLET_LETTER_UPPER: string = String.valueOf((char) (index % 26 + 65)); break; } if ((bullet.type & ST.BULLET_TEXT) != 0) string += bullet.text; Display display = styledText.getDisplay(); TextLayout layout = new TextLayout(display); layout.setText(string); layout.setAscent(lineAscent); layout.setDescent(lineDescent); style = (StyleRange)style.clone(); style.metrics = null; if (style.font == null) style.font = getFont(style.fontStyle); layout.setStyle(style, 0, string.length()); int x = paintX + Math.max(0, metrics.width - layout.getBounds().width - BULLET_MARGIN); layout.draw(gc, x, paintY); layout.dispose(); } int drawLine(int lineIndex, int paintX, int paintY, GC gc, Color widgetBackground, Color widgetForeground) {
void drawBullet(Bullet bullet, GC gc, int paintX, int paintY, int index, int lineAscent, int lineDescent) { StyleRange style = bullet.style; GlyphMetrics metrics = style.metrics; Color color = style.foreground; if (color != null) gc.setForeground(color); Font font = style.font; if (font != null) gc.setFont(font); String string = ""; int type = bullet.type & (ST.BULLET_DOT|ST.BULLET_NUMBER|ST.BULLET_LETTER_LOWER|ST.BULLET_LETTER_UPPER); switch (type) { case ST.BULLET_DOT: string = "\u2022"; break; case ST.BULLET_NUMBER: string = String.valueOf(index + 1); break; case ST.BULLET_LETTER_LOWER: string = String.valueOf((char) (index % 26 + 97)); break; case ST.BULLET_LETTER_UPPER: string = String.valueOf((char) (index % 26 + 65)); break; } if ((bullet.type & ST.BULLET_TEXT) != 0) string += bullet.text; Display display = styledText.getDisplay(); TextLayout layout = new TextLayout(display); layout.setText(string); layout.setAscent(lineAscent); layout.setDescent(lineDescent); style = (StyleRange)style.clone(); style.metrics = null; if (style.font == null) style.font = getFont(style.fontStyle); layout.setStyle(style, 0, string.length()); int x = paintX + Math.max(0, metrics.width - layout.getBounds().width - BULLET_MARGIN); layout.draw(gc, x, paintY); layout.dispose(); } int drawLine(int lineIndex, int paintX, int paintY, GC gc, Color widgetBackground, Color widgetForeground) {
private void processColoredLabel(ColoredString richLabel, GC gc, Rectangle textBounds, boolean isSelected, Font font) { String text= richLabel.getString(); fTextLayout.setText(text); fTextLayout.setFont(font); if (!isSelected) { // apply the styled ranges only when element is not selected Display display= (Display) gc.getDevice(); Iterator ranges= richLabel.getRanges(); while (ranges.hasNext()) { ColoredString.StyleRange curr= (ColoredString.StyleRange) ranges.next(); ColoredString.Style style= curr.style; if (style != null) { Color foreground= getColor(style.getForegroundColorName(), display); TextStyle textStyle= new TextStyle(null, foreground, null); fTextLayout.setStyle(textStyle, curr.offset, curr.offset + curr.length - 1); } } } Rectangle bounds= fTextLayout.getBounds(); int x= textBounds.x; int y = textBounds.y + Math.max(0, (textBounds.height - bounds.height) / 2); fTextLayout.draw(gc, x, y); fTextLayout.setText(""); // clear all ranges //$NON-NLS-1$ }
Rectangle getBoundsAtOffset(int offset) { int lineIndex = content.getLineAtOffset(offset); int lineOffset = content.getOffsetAtLine(lineIndex); String line = content.getLine(lineIndex); Rectangle bounds; if (line.length() != 0) { int offsetInLine = offset - lineOffset; TextLayout layout = renderer.getTextLayout(lineIndex); bounds = layout.getBounds(offsetInLine, offsetInLine); renderer.disposeTextLayout(layout); } else { bounds = new Rectangle (0, 0, 0, renderer.getLineHeight()); } if (offset == caretOffset && !wordWrap) { int lineEnd = lineOffset + line.length(); if (offset == lineEnd) { bounds.width += getCaretWidth(); } } bounds.x += leftMargin - horizontalScrollOffset; bounds.y += getLinePixel(lineIndex); return bounds; } /**
void handleCompositionChanged(Event event) { String text = event.text; int start = event.start; int end = event.end; int charCount = content.getCharCount(); start = Math.min(start, charCount); end = Math.min(end, charCount); int length = text.length(); if (length == ime.getCommitCount()) { content.replaceTextRange(start, end - start, ""); setCaretOffset(ime.getCompositionOffset(), SWT.DEFAULT); caretWidth = 0; caretDirection = SWT.NULL; } else { content.replaceTextRange(start, end - start, text); int alignment = SWT.DEFAULT; if (ime.getWideCaret()) { start = ime.getCompositionOffset(); int lineIndex = getCaretLine(); int lineOffset = content.getOffsetAtLine(lineIndex); TextLayout layout = renderer.getTextLayout(lineIndex); caretWidth = layout.getBounds(start - lineOffset, start + length - 1 - lineOffset).width; renderer.disposeTextLayout(layout); alignment = OFFSET_LEADING; } setCaretOffset(ime.getCaretOffset(), alignment); } showCaret(); } /**
/** * Creates and initializes the text layout used * to compute the size hint. * * @since 3.2 */ private void createTextLayout() { fTextLayout= new TextLayout(fBrowser.getDisplay()); // Initialize fonts String symbolicFontName= fSymbolicFontName == null ? JFaceResources.DIALOG_FONT : fSymbolicFontName; Font font= JFaceResources.getFont(symbolicFontName); fTextLayout.setFont(font); fTextLayout.setWidth(-1); font= JFaceResources.getFontRegistry().getBold(symbolicFontName); fBoldStyle= new TextStyle(font, null, null); // Compute and set tab width fTextLayout.setText(" "); //$NON-NLS-1$ int tabWidth= fTextLayout.getBounds().width; fTextLayout.setTabs(new int[] { tabWidth }); fTextLayout.setText(""); //$NON-NLS-1$ }
/** * Creates and initializes the text layout used * to compute the size hint. * * @since 3.2 */ private void createTextLayout() { fTextLayout= new TextLayout(fBrowser.getDisplay()); // Initialize fonts String symbolicFontName= fSymbolicFontName == null ? JFaceResources.DIALOG_FONT : fSymbolicFontName; Font font= JFaceResources.getFont(symbolicFontName); fTextLayout.setFont(font); fTextLayout.setWidth(-1); font= JFaceResources.getFontRegistry().getBold(symbolicFontName); fBoldStyle= new TextStyle(font, null, null); // Compute and set tab width fTextLayout.setText(" "); //$NON-NLS-1$ int tabWidth= fTextLayout.getBounds().width; fTextLayout.setTabs(new int[] { tabWidth }); fTextLayout.setText(""); //$NON-NLS-1$ }