Java Code Examples for org.eclipse.swt.graphics.GC#textExtent()
The following examples show how to use
org.eclipse.swt.graphics.GC#textExtent() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GuiToolbarWidgets.java From hop with Apache License 2.0 | 6 votes |
private int calculateComboWidth( Combo combo ) { Image image = new Image( HopGui.getInstance().getDisplay(), 10, 10 ); GC gc = new GC( image ); int maxWidth = combo.getSize().x; for ( String item : combo.getItems() ) { int width = gc.textExtent( item ).x; if ( width > maxWidth ) { maxWidth = width; } } gc.dispose(); image.dispose(); return maxWidth; }
Example 2
Source File: XFindPreferencePage.java From xds-ide with Eclipse Public License 1.0 | 6 votes |
public static void createVerticalSpacer(Composite parent, double xLines) { Label lbl = new Label(parent, SWT.NONE); GC gc = new GC(parent); int cyLine; try{ cyLine = gc.textExtent("Wq").y; //$NON-NLS-1$ } finally{ gc.dispose(); } int cy = (int)((double)cyLine * xLines); GridData gd = new GridData(GridData.FILL_HORIZONTAL); Layout layout = parent.getLayout(); if(layout instanceof GridLayout) { gd.horizontalSpan = ((GridLayout)parent.getLayout()).numColumns; } gd.heightHint = cy; lbl.setLayoutData(gd); }
Example 3
Source File: MonthCalendarableItemControl.java From nebula with Eclipse Public License 2.0 | 6 votes |
/** * Compute the minimum size. */ private Point getTotalSize(Image image, String text) { Point size = new Point(0, 0); if (image != null) { Rectangle r = image.getBounds(); size.x += r.width; size.y += r.height; } GC gc = new GC(this); if (text != null && text.length() > 0) { Point e = gc.textExtent(text, DRAW_FLAGS); size.x += e.x; size.y = Math.max(size.y, e.y); if (image != null) size.x += GAP; } else { size.y = Math.max(size.y, gc.getFontMetrics().getHeight()); } gc.dispose(); return size; }
Example 4
Source File: MainSplash.java From arx with Apache License 2.0 | 5 votes |
/** * Paint. * * @param gc */ private void paint(GC gc) { Point size = shell.getSize(); Point offsets = gc.textExtent(version); gc.setAdvanced(true); gc.setAntialias(SWT.ON); gc.setInterpolation(SWT.ON); gc.drawImage(splash, 0, 0, splash.getBounds().width, splash.getBounds().height, 0, 0, size.x, size.y); gc.setForeground(GUIHelper.COLOR_BLACK); gc.drawString(version, size.x - (int)offsets.x - 10, size.y - (int)offsets.y - 10, true); }
Example 5
Source File: SquareButton.java From swt-bling with MIT License | 5 votes |
public Point computeTextSize() { Point size = null; if (text != null) { GC gc = new GC(this); gc.setFont(font); size = gc.textExtent(text, SWT.DRAW_DELIMITER); gc.dispose(); } return size; }
Example 6
Source File: CTree.java From nebula with Eclipse Public License 2.0 | 5 votes |
protected void paintItems(GC gc, Rectangle ebounds) { if(isEmpty()) { if (emptyMessage.length() > 0) { Point bSize = getSize(); Point tSize = gc.textExtent(emptyMessage); gc.setForeground(colors.getItemForegroundNormal()); gc.drawText(emptyMessage, (bSize.x - tSize.x) / 2 - ebounds.x, 4 - ebounds.y); } } else { Image image = new Image(gc.getDevice(), ebounds); GC gc2 = new GC(image); for (Iterator iter = paintedItems.iterator(); iter.hasNext();) { CTreeItem item = (CTreeItem) iter.next(); for(int i = 0; i < item.cells.length; i++) { CTreeCell cell = item.cells[i]; cell.paint(gc, ebounds); Rectangle cb = cell.getClientArea(); gc2.setBackground(getDisplay().getSystemColor(SWT.COLOR_CYAN)); gc2.fillRectangle(ebounds); if(!cb.isEmpty() && cell.paintClientArea(gc2, new Rectangle(0,0,cb.width,cb.height))) { gc.drawImage(image, 0,0,Math.min(ebounds.width, cb.width),Math.min(ebounds.height, cb.height), cb.x-ebounds.x,cb.y-ebounds.y,cb.width,cb.height ); } } } gc2.dispose(); image.dispose(); } }
Example 7
Source File: SwtUtils.java From xds-ide with Eclipse Public License 1.0 | 5 votes |
public static int getTextWidth(Drawable control, String text) { GC gc = new GC(control); try { return gc.textExtent(text).x; } finally{ gc.dispose(); } }
Example 8
Source File: CleanupPreferencePage.java From eclipse-extras with Eclipse Public License 1.0 | 5 votes |
private int getTextWidth( String text ) { GC gc = new GC( cleanupTypesViewer.getControl() ); try { return gc.textExtent( text ).x; } finally { gc.dispose(); } }
Example 9
Source File: XAxisDynamicRenderer.java From neoscada with Eclipse Public License 1.0 | 5 votes |
private Point getExtent ( final GC gc, final String string ) { if ( string == null || string.isEmpty () ) { return EMPTY_POINT; } else { return gc.textExtent ( string ); } }
Example 10
Source File: CurveFittingViewer.java From birt with Eclipse Public License 1.0 | 4 votes |
private final void showException( GC g2d, Exception ex ) { String sWrappedException = ex.getClass( ).getName( ); Throwable th = ex; while ( ex.getCause( ) != null ) { ex = (Exception) ex.getCause( ); } String sException = ex.getClass( ).getName( ); if ( sWrappedException.equals( sException ) ) { sWrappedException = null; } String sMessage = null; if ( th instanceof BirtException ) { sMessage = ( (BirtException) th ).getLocalizedMessage( ); } else { sMessage = ex.getMessage( ); } if ( sMessage == null ) { sMessage = "<null>";//$NON-NLS-1$ } StackTraceElement[] stea = ex.getStackTrace( ); Point d = this.getSize( ); Device dv = Display.getCurrent( ); Font fo = new Font( dv, "Courier", SWT.BOLD, 16 );//$NON-NLS-1$ g2d.setFont( fo ); FontMetrics fm = g2d.getFontMetrics( ); g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) ); g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 ); g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 ); g2d.setClipping( 20, 20, d.x - 40, d.y - 40 ); int x = 25, y = 20 + fm.getHeight( ); g2d.drawString( "Exception:", x, y );//$NON-NLS-1$ x += g2d.textExtent( "Exception:" ).x + 5;//$NON-NLS-1$ g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); g2d.drawString( sException, x, y ); x = 25; y += fm.getHeight( ); if ( sWrappedException != null ) { g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); g2d.drawString( "Wrapped In:", x, y );//$NON-NLS-1$ x += g2d.textExtent( "Wrapped In:" ).x + 5;//$NON-NLS-1$ g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); g2d.drawString( sWrappedException, x, y ); x = 25; y += fm.getHeight( ); } g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); y += 10; g2d.drawString( "Message:", x, y );//$NON-NLS-1$ x += g2d.textExtent( "Message:" ).x + 5;//$NON-NLS-1$ g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) ); g2d.drawString( sMessage, x, y ); x = 25; y += fm.getHeight( ); g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); y += 10; g2d.drawString( "Trace:", x, y );//$NON-NLS-1$ x = 40; y += fm.getHeight( ); g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) ); for ( int i = 0; i < stea.length; i++ ) { g2d.drawString( stea[i].getClassName( ) + ":"//$NON-NLS-1$ + stea[i].getMethodName( ) + "(...):"//$NON-NLS-1$ + stea[i].getLineNumber( ), x, y ); x = 40; y += fm.getHeight( ); } fo.dispose( ); }
Example 11
Source File: StatusComposite.java From eclipse-extras with Eclipse Public License 1.0 | 4 votes |
private Point measureText( String string ) { GC gc = new GC( this ); Point result = gc.textExtent( string ); gc.dispose(); return result; }
Example 12
Source File: TextPainter.java From swt-bling with MIT License | 4 votes |
DrawData(GC gc, TextToken token) { configureForStyle(gc, token); this.token = token; this.extent = gc.textExtent(token.getText()); }
Example 13
Source File: DefaultCellRenderer.java From tmxeditor8 with GNU General Public License v2.0 | 4 votes |
/** * {@inheritDoc} */ public Point computeSize(GC gc, int wHint, int hHint, Object value) { GridItem item = (GridItem)value; gc.setFont(item.getFont(getColumn())); int x = 0; x += leftMargin; if (isTree()) { x += getToggleIndent(item); x += toggleRenderer.getBounds().width + insideMargin; } if (isCheck()) { x += checkRenderer.getBounds().width + insideMargin; } int y = 0; Image image = item.getImage(getColumn()); if (image != null) { y = topMargin + image.getBounds().height + bottomMargin; x += image.getBounds().width + insideMargin; } // MOPR-DND // MOPR: replaced this code (to get correct preferred height for cells in word-wrap columns) // // x += gc.stringExtent(item.getText(column)).x + rightMargin; // // y = Math.max(y,topMargin + gc.getFontMetrics().getHeight() + bottomMargin); // // with this code: int textHeight = 0; if(!isWordWrap()) { x += gc.textExtent(item.getText(getColumn())).x + rightMargin; textHeight = topMargin + textTopMargin + gc.getFontMetrics().getHeight() + textBottomMargin + bottomMargin; } else { int plainTextWidth; if (wHint == SWT.DEFAULT) plainTextWidth = getBounds().width - x - rightMargin; else plainTextWidth = wHint - x - rightMargin; TextLayout currTextLayout = new TextLayout(gc.getDevice()); currTextLayout.setFont(gc.getFont()); currTextLayout.setText(item.getText(getColumn())); currTextLayout.setAlignment(getAlignment()); currTextLayout.setWidth(plainTextWidth < 1 ? 1 : plainTextWidth); x += plainTextWidth + rightMargin; textHeight += topMargin + textTopMargin; for(int cnt=0;cnt<currTextLayout.getLineCount();cnt++) textHeight += currTextLayout.getLineBounds(cnt).height; textHeight += textBottomMargin + bottomMargin; currTextLayout.dispose(); } y = Math.max(y, textHeight); return new Point(x, y); }
Example 14
Source File: Chart3DViewer.java From birt with Eclipse Public License 1.0 | 4 votes |
private final void showException( GC g2d, Exception ex ) { String sWrappedException = ex.getClass( ).getName( ); Throwable th = ex; while ( ex.getCause( ) != null ) { ex = (Exception) ex.getCause( ); } String sException = ex.getClass( ).getName( ); if ( sWrappedException.equals( sException ) ) { sWrappedException = null; } String sMessage = null; if ( th instanceof BirtException ) { sMessage = ( (BirtException) th ).getLocalizedMessage( ); } else { sMessage = ex.getMessage( ); } if ( sMessage == null ) { sMessage = "<null>";//$NON-NLS-1$ } StackTraceElement[] stea = ex.getStackTrace( ); Point d = this.getSize( ); Device dv = Display.getCurrent( ); Font fo = new Font( dv, "Courier", SWT.BOLD, 16 );//$NON-NLS-1$ g2d.setFont( fo ); FontMetrics fm = g2d.getFontMetrics( ); g2d.setBackground( dv.getSystemColor( SWT.COLOR_WHITE ) ); g2d.fillRectangle( 20, 20, d.x - 40, d.y - 40 ); g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); g2d.drawRectangle( 20, 20, d.x - 40, d.y - 40 ); g2d.setClipping( 20, 20, d.x - 40, d.y - 40 ); int x = 25, y = 20 + fm.getHeight( ); g2d.drawString( "Exception:", x, y );//$NON-NLS-1$ x += g2d.textExtent( "Exception:" ).x + 5;//$NON-NLS-1$ g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); g2d.drawString( sException, x, y ); x = 25; y += fm.getHeight( ); if ( sWrappedException != null ) { g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); g2d.drawString( "Wrapped In:", x, y );//$NON-NLS-1$ x += g2d.textExtent( "Wrapped In:" ).x + 5;//$NON-NLS-1$ g2d.setForeground( dv.getSystemColor( SWT.COLOR_RED ) ); g2d.drawString( sWrappedException, x, y ); x = 25; y += fm.getHeight( ); } g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); y += 10; g2d.drawString( "Message:", x, y );//$NON-NLS-1$ x += g2d.textExtent( "Message:" ).x + 5;//$NON-NLS-1$ g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLUE ) ); g2d.drawString( sMessage, x, y ); x = 25; y += fm.getHeight( ); g2d.setForeground( dv.getSystemColor( SWT.COLOR_BLACK ) ); y += 10; g2d.drawString( "Trace:", x, y );//$NON-NLS-1$ x = 40; y += fm.getHeight( ); g2d.setForeground( dv.getSystemColor( SWT.COLOR_DARK_GREEN ) ); for ( int i = 0; i < stea.length; i++ ) { g2d.drawString( stea[i].getClassName( ) + ":"//$NON-NLS-1$ + stea[i].getMethodName( ) + "(...):"//$NON-NLS-1$ + stea[i].getLineNumber( ), x, y ); x = 40; y += fm.getHeight( ); } fo.dispose( ); }
Example 15
Source File: GraphicsHelper.java From gama with GNU General Public License v3.0 | 4 votes |
/** * Create an awt font by converting as much information as possible from the provided swt <code>FontData</code>. * <p> * Generally speaking, given a font size, an swt font will display differently on the screen than the corresponding * awt one. Because the SWT toolkit use native graphical ressources whenever it is possible, this fact is platform * dependent. To address this issue, it is possible to enforce the method to return an awt font with the same height * as the swt one. * * @param device * The swt device being drawn on (display or gc device). * @param fontData * The swt font to convert. * @param ensureSameSize * A boolean used to enforce the same size (in pixels) between the swt font and the newly created awt * font. * @return An awt font converted from the provided swt font. */ public static java.awt.Font toAwtFont(final Device device, final FontData fontData, final boolean ensureSameSize) { int style; switch (fontData.getStyle()) { case SWT.NORMAL: style = java.awt.Font.PLAIN; break; case SWT.ITALIC: style = java.awt.Font.ITALIC; break; case SWT.BOLD: style = java.awt.Font.BOLD; break; default: style = java.awt.Font.PLAIN; break; } int height = (int) Math.round(fontData.getHeight() * device.getDPI().y / 72.0); // hack to ensure the newly created awt fonts will be rendered with the // same height as the swt one if (ensureSameSize) { final GC tmpGC = new GC(device); final Font tmpFont = new Font(device, fontData); tmpGC.setFont(tmpFont); final JPanel DUMMY_PANEL = new JPanel(); java.awt.Font tmpAwtFont = new java.awt.Font(fontData.getName(), style, height); if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) { while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) > tmpGC.textExtent(Az).x) { height--; tmpAwtFont = new java.awt.Font(fontData.getName(), style, height); } } else if (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) { while (DUMMY_PANEL.getFontMetrics(tmpAwtFont).stringWidth(Az) < tmpGC.textExtent(Az).x) { height++; tmpAwtFont = new java.awt.Font(fontData.getName(), style, height); } } tmpFont.dispose(); tmpGC.dispose(); } return new java.awt.Font(fontData.getName(), style, height); }
Example 16
Source File: RendererHelper.java From nebula with Eclipse Public License 2.0 | 4 votes |
/** * Shorten the given text <code>text</code> so that its length doesn't * exceed the given width. The default implementation replaces characters in * the center of the original string with an ellipsis ("..."). Override if * you need a different strategy. * * Note: Code originally from org.eclipse.cwt.CLabel * * @param gc * the gc to use for text measurement * @param t * the text to shorten * @param width * the width to shorten the text to, in pixels * @return the shortened text */ public static String createLabel(String text, GC gc, int width) { if (text == null) return null; final int extent = gc.textExtent(text).x; if (extent > width) { final int w = gc.textExtent(ELLIPSIS).x; if (width <= w) { return text; } final int l = text.length(); int max = l / 2; int min = 0; int mid = (max + min) / 2 - 1; if (mid <= 0) { return text; } while (min < mid && mid < max) { final String s1 = text.substring(0, mid); final String s2 = text.substring(l - mid, l); final int l1 = gc.textExtent(s1).x; final int l2 = gc.textExtent(s2).x; if (l1 + w + l2 > width) { max = mid; mid = (max + min) / 2; } else if (l1 + w + l2 < width) { min = mid; mid = (max + min) / 2; } else { min = max; } } if (mid == 0) { return text; } String result = text.substring(0, mid) + ELLIPSIS + text.substring(l - mid, l); return result; } return text; }
Example 17
Source File: NButton.java From ldparteditor with MIT License | 4 votes |
private void paint(PaintEvent event) { final GC gc = event.gc; final Image img = this.img; final boolean focused = this.isFocusControl(); final boolean enabled = this.isEnabled(); final boolean hasImage = img != null; final int img_width = hasImage ? img.getImageData().width : 0; final int img_height = hasImage ? img.getImageData().height : 0; final int this_width = getBounds().width - 1; gc.setFont(Font.SYSTEM); // setFont before using textExtent, so that the size of the text // can be calculated correctly final Point textExtent = getText().isEmpty() ? new Point(0,0) : gc.textExtent(getText()); // TODO 1. Calculate sizes // TODO 2. Draw Content if (selected && (canToggle || isRadio)) { gc.setBackground(SWTResourceManager.getColor(160, 160, 200)); gc.fillRoundRectangle(0, 0, Math.max(img_width + 9 + textExtent.x, this_width), Math.max(textExtent.y, img_height) + 9, 5, 5); gc.setBackground(getBackground()); } gc.setForeground(SWTResourceManager.getColor(255, 255, 255)); if (hovered || focused) { gc.setForeground(SWTResourceManager.getColor(0, 0, 0)); } if (pressed) { gc.setForeground(SWTResourceManager.getColor(220, 220, 220)); } if (!canCheck && !hasBorder) { if (!enabled) { gc.setForeground(SWTResourceManager.getColor(200, 200, 200)); } gc.drawRoundRectangle(0, 0, Math.max(img_width + 9 + textExtent.x, this_width), Math.max(textExtent.y, img_height) + 9, 5, 5); gc.setForeground(SWTResourceManager.getColor(60, 60, 60)); if (hovered || focused) { gc.setBackground(SWTResourceManager.getColor(160, 160, 200)); gc.fillRoundRectangle(1, 1, Math.max(img_width + 9 + textExtent.x, this_width) - 1, Math.max(textExtent.y, img_height) + 9 - 1, 5, 5); if (selected && (canToggle || isRadio)) { gc.setBackground(SWTResourceManager.getColor(160, 160, 200)); } else { gc.setBackground(getBackground()); } gc.fillRoundRectangle(2, 2, Math.max(img_width + 9 + textExtent.x, this_width) - 3, Math.max(textExtent.y, img_height) + 9 - 3, 5, 5); } if (pressed) { gc.setForeground(SWTResourceManager.getColor(30, 30, 30)); } if (!enabled) { gc.setForeground(SWTResourceManager.getColor(200, 200, 200)); } gc.drawRoundRectangle(1, 1, Math.max(img_width + 9 + textExtent.x, this_width) - 1, Math.max(textExtent.y, img_height) + 9 - 1, 5, 5); } if (hasImage) { gc.drawImage(img, 5, 5); } gc.setForeground(getForeground()); gc.drawString(getText(), img_width + 5, 5, true); // 3. Paint custom forms for (PaintListener pl : painters) { pl.paintControl(event); } }
Example 18
Source File: ConditionEditor.java From hop with Apache License 2.0 | 4 votes |
private Point drawCondition( GC gc, int x, int y, int nr, Condition condition ) { int opx, opy, opw, oph; int cx, cy, cw, ch; opx = x; opy = y; opw = size_and_not.width + 6; oph = size_and_not.height + 2; /* * First draw the operator ... */ if ( nr > 0 ) { String operator = condition.getOperatorDesc(); // Remember the size of the rectangle! size_oper[ nr ] = new Rectangle( opx, opy, opw, oph ); if ( nr == hover_operator ) { gc.setBackground( gray ); gc.fillRectangle( Real2Screen( size_oper[ nr ] ) ); gc.drawRectangle( Real2Screen( size_oper[ nr ] ) ); gc.setBackground( bg ); } gc.drawText( operator, size_oper[ nr ].x + 2 + offsetx, size_oper[ nr ].y + 2 + offsety, SWT.DRAW_TRANSPARENT ); } /* * Then draw the condition below, possibly negated! */ String str = condition.toString( 0, true, false ); // don't show the operator! Point p = gc.textExtent( str ); cx = opx + 23; cy = opy + oph + 10; cw = p.x + 5; ch = p.y + 5; // Remember the size of the rectangle! size_cond[ nr ] = new Rectangle( cx, cy, cw, ch ); if ( nr == hover_condition ) { gc.setBackground( gray ); gc.fillRectangle( Real2Screen( size_cond[ nr ] ) ); gc.drawRectangle( Real2Screen( size_cond[ nr ] ) ); gc.setBackground( bg ); } gc.drawText( str, size_cond[ nr ].x + 2 + offsetx, size_cond[ nr ].y + 5 + offsety, SWT.DRAW_DELIMITER | SWT.DRAW_TRANSPARENT | SWT.DRAW_TAB | SWT.DRAW_MNEMONIC ); p.x += 0; p.y += 5; return p; }
Example 19
Source File: ConditionEditor.java From hop with Apache License 2.0 | 4 votes |
private Rectangle getUpSize( GC gc ) { Point p = gc.textExtent( STRING_UP ); return new Rectangle( size_not.x + size_not.width + 40, size_not.y, p.x + 20, size_not.height ); }
Example 20
Source File: ConditionEditor.java From hop with Apache License 2.0 | 4 votes |
private Rectangle getNotSize( GC gc ) { Point p = gc.textExtent( STRING_NOT ); return new Rectangle( 0, 0, p.x + 10, p.y + 4 ); }