Java Code Examples for org.eclipse.swt.graphics.GC#stringExtent()

The following examples show how to use org.eclipse.swt.graphics.GC#stringExtent() . 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: NatTableCustomCellPainter.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
private int drawTextPart(GC gc, int yStartPos, int xStartPos, TextPart text){
	Point textExtent = new Point(0, 0);
	if (text.getStyle() == TextPart.PartStyle.NORMAL) {
		textExtent = gc.stringExtent(text.getText());
		gc.drawText(text.getText(), xStartPos, yStartPos + spacing,
			SWT.DRAW_TRANSPARENT | SWT.DRAW_DELIMITER);
	} else if (text.getStyle() == TextPart.PartStyle.BOLD) {
		Font origFont = gc.getFont();
		FontDescriptor boldDescriptor =
			FontDescriptor.createFrom(gc.getFont()).setStyle(SWT.BOLD);
		Font boldFont = boldDescriptor.createFont(Display.getDefault());
		gc.setFont(boldFont);
		textExtent = gc.stringExtent(text.getText());
		gc.drawText(text.getText(), xStartPos, yStartPos + spacing,
			SWT.DRAW_TRANSPARENT | SWT.DRAW_DELIMITER);
		gc.setFont(origFont);
		boldFont.dispose();
	}
	return xStartPos + textExtent.x;
}
 
Example 2
Source File: BreadcrumbItem.java    From SWET with MIT License 6 votes vote down vote up
private Point computeSizeOfTextAndImages() {
	int width = 0, height = 0;
	final boolean textISNotEmpty = getText() != null && !getText().equals("");

	if (textISNotEmpty) {
		final GC gc = new GC(this.parentBreadcrumb);
		gc.setFont(this.parentBreadcrumb.getFont());
		final Point extent = gc.stringExtent(getText());
		gc.dispose();
		width += extent.x;
		height = extent.y;
	}

	final Point imageSize = computeMaxWidthAndHeightForImages(getImage(),
			this.selectionImage, this.disabledImage);

	if (imageSize.x != -1) {
		width += imageSize.x;
		height = Math.max(imageSize.y, height);
		if (textISNotEmpty) {
			width += MARGIN * 2;
		}
	}
	width += MARGIN;
	return new Point(width, height);
}
 
Example 3
Source File: CalculatorCombo.java    From nebula with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @see org.eclipse.swt.widgets.Composite#computeSize(int, int, boolean)
 */
@Override
public Point computeSize(final int wHint, final int hHint, final boolean changed) {
	checkWidget();
	int width = 0, height = 0;

	final GC gc = new GC(label);
	final int spacer = gc.stringExtent("                    ").x;
	final int textWidth = gc.stringExtent(label.getText()).x;
	gc.dispose();
	final Point textSize = label.computeSize(SWT.DEFAULT, SWT.DEFAULT, changed);
	final Point arrowSize = arrow.computeSize(SWT.DEFAULT, SWT.DEFAULT, changed);
	final int borderWidth = getBorderWidth();

	height = Math.max(textSize.y, arrowSize.y);
	width = textWidth + 2 * spacer + arrowSize.x + 2 * borderWidth;
	if (wHint != SWT.DEFAULT) {
		width = wHint;
	}
	if (hHint != SWT.DEFAULT) {
		height = hHint;
	}
	return new Point(width + 2 * borderWidth, height + 2 * borderWidth);
}
 
Example 4
Source File: TypeColunmCellRenderer.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Rectangle getTextBounds(GridItem item, boolean preferred) {
	int x = leftMargin;

	Image image = item.getImage(getColumn());
	if (image != null) {
		x += image.getBounds().width + 3;
	}

	Rectangle bounds = new Rectangle(x, topMargin + textTopMargin, 0, 0);

	GC gc = new GC(item.getParent());
	gc.setFont(item.getFont(getColumn()));
	Point size = gc.stringExtent(item.getText(getColumn()));

	bounds.height = size.y;

	if (preferred) {
		bounds.width = size.x - 1;
	} else {
		bounds.width = getBounds().width - x - rightMargin;
	}

	gc.dispose();

	return bounds;
}
 
Example 5
Source File: BadgedLabel.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
private Point getTextSizeInPixels() {
	if (text == null || text.equals("")) {
		return new Point(0, 0);
	}

	if (textSizeCache != null) {
		return textSizeCache;
	}

	final GC gc = new GC(this);
	gc.setFont(boldFont);
	textSizeCache = gc.stringExtent(text);
	gc.dispose();
	return textSizeCache;
}
 
Example 6
Source File: DefaultRowHeaderRenderer.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
/**
    * {@inheritDoc}
    */
   @Override
public Point computeSize(GC gc, int wHint, int hHint, Object value)
   {
       GridItem item = (GridItem) value;

       String text = getHeaderText(item);
       Image image = getHeaderImage(item);

       Font previousFont = gc.getFont();
	if (getHeaderFont(item) == null) {
		gc.setFont(item.getParent().getFont());
	} else {
		gc.setFont(getHeaderFont(item));
	}

       int x = leftMargin;

       if( image != null ) {
       	x += image.getBounds().width + 5;
       }

       x += gc.stringExtent(text).x + rightMargin;

       int y = topMargin;

       if( image != null ) {
       	y += Math.max(gc.getFontMetrics().getHeight(),image.getBounds().height);
       } else {
       	y += gc.getFontMetrics().getHeight();
       }


       y += bottomMargin;
       gc.setFont(previousFont);

       return new Point(x, y);
   }
 
Example 7
Source File: CCombo.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public Point computeSize (int wHint, int hHint, boolean changed) {
		checkWidget( );
	int width = 0, height = 0;
		String[] items = list.getItems( );
		GC gc = new GC( text );
		int spacer = gc.stringExtent( " " ).x; //$NON-NLS-1$
		int textWidth = gc.stringExtent( text.getText( ) ).x;
		for ( int i = 0; i < items.length; i++ )
		{
			textWidth = Math.max( gc.stringExtent( items[i] ).x, textWidth );
		}
		gc.dispose( );
		Point textSize = text.computeSize( SWT.DEFAULT, SWT.DEFAULT, changed );
		Point arrowSize = arrow.computeSize( SWT.DEFAULT, SWT.DEFAULT, changed );
		Point listSize = list.computeSize( SWT.DEFAULT, SWT.DEFAULT, changed );
		int borderWidth = getBorderWidth( );
	
	height = Math.max( textSize.y, arrowSize.y );
		width = Math.max( textWidth
				+ 2
				* spacer
				+ arrowSize.x
				+ 2
				* borderWidth, listSize.x );
		if ( wHint != SWT.DEFAULT )
			width = wHint;
		if ( hHint != SWT.DEFAULT )
			height = hHint;
		return new Point( width + 2 * borderWidth, height + 2 * borderWidth );
}
 
Example 8
Source File: CalendarComposite.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
private void drawTitleDays(GC gc) {
	Calendar temp = Calendar.getInstance(mSettings.getLocale());
	// fetch the first day of the week, and draw starting on that day
	int fdow = temp.getFirstDayOfWeek();

	Rectangle bounds = super.getBounds();
	int xStart = mSettings.getDatesLeftMargin() + 5;
	int yStart = bounds.y + mSettings.getHeaderTopMargin() + mSettings.getHeaderHeight() + 1;

	int spacer = 0;
	int letterHeight = 0;

	for (int i = 0; i < 7; i++) {
		Point strWidth = gc.stringExtent(mDayTitles[fdow]);

		int x = xStart + mSettings.getOneDateBoxSize() + spacer - strWidth.x;
		// don't add the string width, as our string width later when
		// drawing days will differ
		mDayXs[i] = xStart + mSettings.getOneDateBoxSize() + spacer;

		gc.drawString(mDayTitles[fdow], x, yStart, true);

		letterHeight = strWidth.y;
		spacer += mSettings.getOneDateBoxSize() + mSettings.getBoxSpacer();

		fdow++;
		if (fdow > 7) {
			fdow = 1;
		}
	}

	int lineStart = yStart + 1 + letterHeight;
	gc.setForeground(mColorManager.getLineColor());
	gc.drawLine(mSettings.getDatesLeftMargin() + 1, lineStart, bounds.width - mSettings.getDatesRightMargin() - 3, lineStart);

	mDatesTopY = lineStart + 3;
}
 
Example 9
Source File: AbstractPaintManager.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
public void drawDaysOnChart(final GanttComposite ganttComposite, final ISettings settings, final IColorManager colorManager, final GanttEvent event, final GC gc, final boolean threeDee, final int x, final int y, final int eventWidth, final int daysNumber, final Rectangle bounds) {
    if (event.isImage()) {
        return;
    }

    final int top = y - 2;
    final int xE = x + eventWidth;
    final int middle = x + ((xE - x) / 2);
    int yMiddle = event.getY() + (event.getHeight() / 2);

    final StringBuffer buf = new StringBuffer();
    buf.append(daysNumber);
    final String dayString = buf.toString();

    final Point extent = gc.stringExtent(dayString);
    final Point unmodified = new Point(extent.x, extent.y);
    extent.x = extent.x + (2 * 2) + 2; // 2 pixel spacing on 2 sides, for clarity's sake

    Color gradient = event.getGradientStatusColor();

    if (gradient == null) {
        gradient = settings.getDefaultGradientEventColor();
    }

    if ((middle - extent.x) > x) {
        gc.setBackground(gradient);
        gc.fillRectangle(middle - extent.x / 2, top, extent.x, settings.getEventHeight() + 4);
        gc.setForeground(colorManager.getTextColor());
        gc.drawRectangle(middle - extent.x / 2, top, extent.x, settings.getEventHeight() + 4);

        yMiddle -= unmodified.y / 2;
        gc.drawString(dayString, middle - unmodified.x + (unmodified.x / 2) + 1, yMiddle, true);
    }
}
 
Example 10
Source File: Chips.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @see org.eclipse.swt.widgets.Control#computeSize(int, int, boolean)
 */
@Override
public Point computeSize(final int wHint, final int hHint, final boolean changed) {
	checkWidget();
	int width = 0; // Border
	int height = 20;
	if (image != null) {
		final Rectangle imageSize = image.getBounds();
		width += 4 + imageSize.width;
		height = Math.max(height, imageSize.height + 4);

	}

	if (text != null) {
		final GC gc = new GC(this);
		final Point textSize = gc.stringExtent(text);
		width += 4 + textSize.x;
		height = Math.max(height, textSize.y);
		gc.dispose();
	}

	if (isCheck && selection || isClose) {
		width += 20;
	}

	width += Math.max(height, hHint); // Size for left & right half-circle
	return new Point(Math.max(width, wHint), Math.max(height, hHint));
}
 
Example 11
Source File: TypeColunmCellRenderer.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Rectangle getTextBounds(GridItem item, boolean preferred) {
	int x = leftMargin;

	Image image = item.getImage(getColumn());
	if (image != null) {
		x += image.getBounds().width + 3;
	}

	Rectangle bounds = new Rectangle(x, topMargin + textTopMargin, 0, 0);

	GC gc = new GC(item.getParent());
	gc.setFont(item.getFont(getColumn()));
	Point size = gc.stringExtent(item.getText(getColumn()));

	bounds.height = size.y;

	if (preferred) {
		bounds.width = size.x - 1;
	} else {
		bounds.width = getBounds().width - x - rightMargin;
	}

	gc.dispose();

	return bounds;
}
 
Example 12
Source File: XBookmarksDialog.java    From xds-ide with Eclipse Public License 1.0 4 votes vote down vote up
@Override
 protected Control createDialogArea(Composite parent) {
     Composite composite = (Composite) super.createDialogArea(parent);
     GridLayoutFactory.fillDefaults().extendedMargins(Util.isWindows() ? 0 : 3, 3, 2, 2).applyTo(composite);

     Composite tableComposite = new Composite(composite, SWT.NONE);
     tableComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
     tableComposite.setLayout(new GridLayout(1, false));
     
     tableViewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
             | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
     
     table = tableViewer.getTable();
     table.setLayoutData(new GridData(GridData.FILL_BOTH));
     tableViewer.setContentProvider(new MenuTableContentProvider());
     tableViewer.setLabelProvider(new MenuTableLabelProvider());

     { // Columns:
         GC gc= new GC(table);
         try {
         	int maxW = gc.stringExtent("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW").x; //$NON-NLS-1$
         	int gap  = gc.stringExtent("WW").x; //$NON-NLS-1$
         	int widths[] = new int[model.columns];
         	for (int i=0; i<model.columns; ++i) {
         		widths[i] = 0;
         	}
         	for (Model.Row r : model.getRows()) {
         		for (int i=0; i<model.columns; ++i) {
         			if (!r.getField(i).isEmpty()) {
         				int w     = Math.min(gc.stringExtent(r.getField(i)).x + gap, maxW);
         				widths[i] = Math.max(widths[i], w);
         			}
         		}
         	}
         	
         	for (int i=0; i<model.columns; ++i) {
         		TableColumn tc = new TableColumn(table, SWT.LEFT);
         		tc.setWidth(widths[i]);
         	}
         	table.setHeaderVisible(false);
         	table.setLinesVisible(false);
} finally {
	gc.dispose();
}
     }
     
     Listener eventListener = new Listener() {
         @Override
         public void handleEvent(Event event) {
             if (event.type == SWT.MouseDoubleClick || 
                (event.type == SWT.KeyDown && event.character == SWT.CR)) 
             {
                 doSelect();
             }
         }
         
     };
     
     addListener (SWT.KeyDown, eventListener);
     addListener (SWT.MouseDoubleClick, eventListener);
         
     tableViewer.setInput(model);
     table.select(0);

     return composite;
 }
 
Example 13
Source File: TextUtils.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Shortens a supplied string so that it fits within the area specified by
 * the width argument. Strings that have been shorted have an "..." attached
 * to the end of the string. The width is computed using the
 * {@link GC#stringExtent(String)}.
 * 
 * @param gc GC used to perform calculation.
 * @param t text to modify.
 * @param width Pixels to display.
 * @return shortened string that fits in area specified.
 */
public static String getShortString(GC gc, String t, int width)
{

    if (t == null)
    {
        return null;
    }

    if (t.equals(""))
    {
        return "";
    }

    if (width >= gc.stringExtent(t).x)
    {
        return t;
    }

    int w = gc.stringExtent("...").x;
    String text = t;
    int l = text.length();
    int pivot = l / 2;
    int s = pivot;
    int e = pivot + 1;
    while (s >= 0 && e < l)
    {
        String s1 = text.substring(0, s);
        String s2 = text.substring(e, l);
        int l1 = gc.stringExtent(s1).x;
        int l2 = gc.stringExtent(s2).x;
        if (l1 + w + l2 < width)
        {
            text = s1 + "..." + s2;
            break;
        }
        s--;
        e++;
    }

    if (s == 0 || e == l)
    {
        text = text.substring(0, 1) + "..." + text.substring(l - 1, l);
    }

    return text;
}
 
Example 14
Source File: TextPiece.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
public void paint(final GC gc, final int x, final int y) {
	Font oldFont = gc.getFont();
	Color oldForeground = gc.getForeground();
	Color oldBackground = gc.getBackground();

	final int width = getSize().x;
	final int align = style.getAlignment();

	try {
		boolean transparent = initGC(gc);

		FontMetrics fm = gc.getFontMetrics();
		int lineHeight = fm.getHeight();

		boolean strikeout = style.getStrikeout();
		boolean underline = style.getUnderline();
		int lineThickness = Math.max(1, fm.getDescent() / 3);
		int strikeoutOffset = fm.getLeading() + fm.getAscent() / 2;
		int underlineOffset = ascent + lineThickness;

		for (int i = 0; i < lines.length; i++) {
			String line = lines[i];
			int lineWidth = gc.stringExtent(line).x;
			int offset = getHorzAlignmentOffset(align, lineWidth, width);

			gc.drawString(lines[i], x + offset, y + lineHeight * i,
					transparent);
			if (strikeout || underline) {
				Color saveBackground = gc.getBackground();
				gc.setBackground(gc.getForeground());
				if (strikeout)
					gc.fillRectangle(x + offset, y + lineHeight * i
							+ strikeoutOffset, lineWidth, lineThickness);
				if (underline)
					gc.fillRectangle(x + offset, y + lineHeight * i
							+ underlineOffset, lineWidth, lineThickness);
				gc.setBackground(saveBackground);
			}
		}
	} finally {
		restoreGC(gc, oldFont, oldForeground, oldBackground);
	}
}
 
Example 15
Source File: CharacterPairsTableWidget.java    From tm4e with Eclipse Public License 1.0 4 votes vote down vote up
protected int computeMinimumColumnWidth(GC gc, String string) {
	return gc.stringExtent(string).x + 10;
}
 
Example 16
Source File: LanguageConfigurationPreferencePage.java    From tm4e with Eclipse Public License 1.0 4 votes vote down vote up
private int computeMinimumColumnWidth(GC gc, String string) {
	return gc.stringExtent(string).x + 10; // pad 10 to accommodate table header trimmings
}
 
Example 17
Source File: GrammarPreferencePage.java    From tm4e with Eclipse Public License 1.0 4 votes vote down vote up
private int computeMinimumColumnWidth(GC gc, String string) {
	return gc.stringExtent(string).x + 10; // pad 10 to accommodate table
											// header trimmings
}
 
Example 18
Source File: TableCombo.java    From tmxeditor8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Point computeSize(int wHint, int hHint, boolean changed) {
	checkWidget();

	int overallWidth = 0;
	int overallHeight = 0;
	int borderWidth = getBorderWidth();

	// use user defined values if they are specified.
	if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) {
		overallWidth = wHint;
		overallHeight = hHint;
	} else {
		TableItem[] tableItems = table.getItems();

		GC gc = new GC(text);
		int spacer = gc.stringExtent(" ").x; //$NON-NLS-1$
		int maxTextWidth = gc.stringExtent(text.getText()).x;
		int colIndex = getDisplayColumnIndex();
		int maxImageHeight = 0;
		int currTextWidth = 0;

		// calculate the maximum text width and image height.
		for (int i = 0; i < tableItems.length; i++) {
			currTextWidth = gc.stringExtent(tableItems[i].getText(colIndex)).x;

			// take image into account if there is one for the tableitem.
			if (tableItems[i].getImage() != null) {
				currTextWidth += tableItems[i].getImage().getBounds().width;
				maxImageHeight = Math.max(tableItems[i].getImage().getBounds().height, maxImageHeight);
			}

			maxTextWidth = Math.max(currTextWidth, maxTextWidth);
		}

		gc.dispose();
		Point textSize = text.computeSize(SWT.DEFAULT, SWT.DEFAULT, changed);
		Point arrowSize = arrow.computeSize(SWT.DEFAULT, SWT.DEFAULT, changed);
		Point tableSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT, changed);

		overallHeight = Math.max(textSize.y, arrowSize.y);
		overallHeight = Math.max(maxImageHeight, overallHeight);
		overallWidth = Math.max(maxTextWidth + 2 * spacer + arrowSize.x + 2 * borderWidth, tableSize.x);

		// use user specified if they were entered.
		if (wHint != SWT.DEFAULT)
			overallWidth = wHint;
		if (hHint != SWT.DEFAULT)
			overallHeight = hHint;
	}

	return new Point(overallWidth + 2 * borderWidth, overallHeight + 2 * borderWidth);
}
 
Example 19
Source File: DefaultRowHeaderRenderer.java    From tmxeditor8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Point computeSize(GC gc, int wHint, int hHint, Object value)
{
    GridItem item = (GridItem) value;

    String text = getHeaderText(item);
    Image image = getHeaderImage(item);

    int x = 0;

    x += leftMargin;

    if( image != null ) {
    	x += image.getBounds().width + 5;
    }

    x += gc.stringExtent(text).x + rightMargin;

    int y = 0;

    y += topMargin;

    if( image != null ) {
    	y += Math.max(gc.getFontMetrics().getHeight(),image.getBounds().height);
    } else {
    	y += gc.getFontMetrics().getHeight();
    }


    y += bottomMargin;

    return new Point(x, y);
}
 
Example 20
Source File: Utils.java    From tracecompass with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Draw text in a rectangle.
 *
 * @param gc
 *            The SWT GC object
 * @param text
 *            The text to draw
 * @param rect
 *            The rectangle object which is being drawn
 * @param transp
 *            If true the background will be transparent
 * @return The width of the written text
 */
public static int drawText(GC gc, String text, Rectangle rect, boolean transp) {
    Point size = gc.stringExtent(text);
    gc.drawText(text, rect.x, rect.y, transp);
    return size.x;
}