@Override public Point draw(GC gc, StyledText textWidget, Color color, int x, int y) { // increment x with 3 spaces width x += 3 * (int) gc.getFontMetrics().getAverageCharacterWidth(); Point p = super.draw(gc, textWidget, color, x, y); if (fRgb != null) { int width = drawSquare(fRgb, gc, textWidget, x + p.x, y); p.x += width; } return p; }
@Override public Point computeSizeConstraints(int widthInChars, int heightInChars) { GC gc= new GC(fText); gc.setFont(fTextFont); double width= gc.getFontMetrics().getAverageCharacterWidth(); int height= fText.getLineHeight(); gc.dispose(); return new Point((int) (widthInChars * width), heightInChars * height); } }
/** * Computes and remembers the x-offset of the margin column for the * current widget font. */ private void computeWidgetX() { GC gc= new GC(fTextWidget); fFontForCachedWidgetX= fTextWidget.getFont(); double pixels= gc.getFontMetrics().getAverageCharacterWidth(); gc.dispose(); fCachedWidgetX= (int) (pixels * fMarginWidth + 0.5); }
@Override public Point computeSizeConstraints(int widthInChars, int heightInChars) { if (fSymbolicFontName == null) return null; GC gc= new GC(fBrowser); Font font= JFaceResources.getFont(fSymbolicFontName); gc.setFont(font); double width= gc.getFontMetrics().getAverageCharacterWidth(); int height= gc.getFontMetrics().getHeight(); gc.dispose(); return new Point((int) (widthInChars * width), heightInChars * height); }
/** * Draw the inlined annotation. By default it draws the text of the annotation with gray color. * User can override this method to draw anything. * * @param gc the graphics context * @param textWidget the text widget to draw on * @param offset the offset of the line * @param length the length of the line * @param color the color of the line * @param x the x position of the annotation * @param y the y position of the annotation * @return the text width. */ protected int drawAndComputeWidth(GC gc, StyledText textWidget, int offset, int length, Color color, int x, int y) { // Draw the text annotation and returns the width super.draw(gc, textWidget, offset, length, color, x, y); return (int) (gc.stringExtent(getText()).x + 2 * gc.getFontMetrics().getAverageCharacterWidth()); }
/** * Returns the average character width of the given control's font. * * @param control the control to calculate the average char width for * @return the average character width of the controls font * * @since 3.4 */ public static int getAverageCharWidth(Control control) { GC gc= new GC(control); gc.setFont(control.getFont()); double increment= gc.getFontMetrics().getAverageCharacterWidth(); gc.dispose(); return (int) increment; }
/** * Computes the size constraints based on the * {@link JFaceResources#getDialogFont() dialog font}. Subclasses can * override or extend. * * @see org.eclipse.jface.text.IInformationControlExtension5#computeSizeConstraints(int, int) */ @Override public Point computeSizeConstraints(int widthInChars, int heightInChars) { GC gc= new GC(fContentComposite); gc.setFont(JFaceResources.getDialogFont()); double width= gc.getFontMetrics().getAverageCharacterWidth(); int height= gc.getFontMetrics().getHeight(); gc.dispose(); return new Point((int) (widthInChars * width), heightInChars * height); }
int index = (int) Math.min (availableWidth / gc.getFontMetrics ().getAverageCharacterWidth (), text.length ()); textWidth = gc.textExtent (text.substring (0, index), SWT.DRAW_MNEMONIC).x;
/** * Returns the width hint for this label. * * @param control the root control of this label * @return the width hint for this label * @since 2.1 */ private int getWidthHint(Composite control) { if (fFixedWidth < 0) { GC gc= new GC(control); gc.setFont(control.getFont()); fFixedWidth = (int) gc.getFontMetrics().getAverageCharacterWidth() * fWidthInChars; fFixedWidth += INDENT * 2; gc.dispose(); } return fFixedWidth; }
/** * Computes the size constraints of the information control in points based on the * default font of the given subject control as well as the size constraints in character * width. * * @param subjectControl the subject control * @param informationControl the information control whose size constraints are computed * @return the computed size constraints in points */ protected Point computeSizeConstraints(Control subjectControl, IInformationControl informationControl) { if (fSizeConstraints == null) { if (informationControl instanceof IInformationControlExtension5) { IInformationControlExtension5 iControl5= (IInformationControlExtension5) informationControl; fSizeConstraints= iControl5.computeSizeConstraints(fWidthConstraint, fHeightConstraint); if (fSizeConstraints != null) return Geometry.copy(fSizeConstraints); } if (subjectControl == null) return null; GC gc= new GC(subjectControl); gc.setFont(subjectControl.getFont()); double width= gc.getFontMetrics().getAverageCharacterWidth(); int height = gc.getFontMetrics().getHeight(); gc.dispose(); fSizeConstraints= new Point((int) (fWidthConstraint * width), fHeightConstraint * height); } return new Point(fSizeConstraints.x, fSizeConstraints.y); }
int index = (int) Math.min (availableWidth / gc.getFontMetrics ().getAverageCharacterWidth (), text.length ()); textWidth = gc.stringExtent (text.substring (0, index)).x;
rotateSpinner = new Spinner(comp, SWT.BORDER | SWT.WRAP); GC gc = new GC(rotateSpinner); int width = (int) (gc.getFontMetrics().getAverageCharacterWidth() * 5); gc.dispose(); rotateSpinner.setLayoutData(new GridData(width, SWT.DEFAULT));
/** * Returns <code>true</code> if the point specified by the arguments is inside the annotation * specified by the receiver, and <code>false</code> otherwise. * * @param x the x coordinate of the point to test for containment * @param y the y coordinate of the point to test for containment * @return <code>true</code> if the annotation contains the point and <code>false</code> * otherwise */ boolean contains(int x, int y) { StyledText styledText= getTextWidget(); GC gc= null; try { gc= new GC(styledText); return x >= fX && y >= fY && y <= fY + styledText.getLineHeight(position.getOffset()) && x <= fX + gc.stringExtent(getText()).x + 2 * gc.getFontMetrics().getAverageCharacterWidth(); } finally { if (gc != null) { gc.dispose(); } } } }
double width= metrics.getAverageCharacterWidth(); gc.drawRectangle(p.x, p.y + leading, (int) width, height); int third= (int) (width / 3);