/** * Returns the difference between the baseline of the widget and the baseline as specified by the * font for <code>gc</code>. When drawing line numbers, the returned bias should be added to * obtain text lined up on the correct base line of the text widget. * * @param gc the {@code GC} to get the font metrics from * @param widgetLine the widget line * @return the baseline bias to use when drawing text that is lined up with the text widget. */ private int getBaselineBias(GC gc, int widgetLine) { ITextViewer textViewer = getParentRuler().getTextViewer(); int offset = textViewer.getTextWidget().getOffsetAtLine(widgetLine); int widgetBaseline = textViewer.getTextWidget().getBaseline(offset); FontMetrics fm = gc.getFontMetrics(); int fontBaseline = fm.getAscent() + fm.getLeading(); int baselineBias = widgetBaseline - fontBaseline; return Math.max(0, baselineBias); }
public static void convertSizeInCharsToPixels(Control control, Point size) { FontMetrics fontMetrics = getFontMetrics(control); if (fontMetrics == null) { return; } size.x = fontMetrics.getAverageCharWidth() * size.x; size.y = fontMetrics.getHeight() * size.y; }
/** * Draw string at widget offset. * * @param gc * @param offset the widget offset * @param s the string to be drawn * @param fg the foreground color */ private void draw(GC gc, int offset, String s, Color fg) { // Compute baseline delta (see // https://bugs.eclipse.org/bugs/show_bug.cgi?id=165640) int baseline = fTextWidget.getBaseline(offset); FontMetrics fontMetrics = gc.getFontMetrics(); int fontBaseline = fontMetrics.getAscent() + fontMetrics.getLeading(); int baslineDelta = baseline - fontBaseline; Point pos = fTextWidget.getLocationAtOffset(offset); gc.setForeground(fg); gc.drawString(s, pos.x, pos.y + baslineDelta, true); }
/* * @see org.eclipse.jface.text.source.AnnotationPainter.IDrawingStrategy#draw(org.eclipse.swt.graphics.GC, org.eclipse.swt.custom.StyledText, int, int, org.eclipse.swt.graphics.Color) */ public void draw( Annotation annotation, GC gc, StyledText textWidget, int offset, int length, Color color) { if (annotation instanceof ProjectionAnnotation) { ProjectionAnnotation projectionAnnotation = (ProjectionAnnotation) annotation; if (projectionAnnotation.isCollapsed()) { if (gc != null) { StyledTextContent content = textWidget.getContent(); int line = content.getLineAtOffset(offset); int lineStart = content.getOffsetAtLine(line); String text = content.getLine(line); int lineLength = text == null ? 0 : text.length(); int lineEnd = lineStart + lineLength; Point p = textWidget.getLocationAtOffset(lineEnd); Color c = gc.getForeground(); gc.setForeground(color); FontMetrics metrics = gc.getFontMetrics(); // baseline: where the dots are drawn int baseline = textWidget.getBaseline(offset); // descent: number of pixels that the box extends over baseline int descent = Math.min(2, textWidget.getLineHeight(offset) - baseline); // ascent: so much does the box stand up from baseline int ascent = metrics.getAscent(); // leading: free space from line top to box upper line int leading = baseline - ascent; // height: height of the box int height = ascent + descent; int width = metrics.getAverageCharWidth(); gc.drawRectangle(p.x, p.y + leading, width, height); int third = width / 3; int dotsVertical = p.y + baseline - 1; gc.drawPoint(p.x + third, dotsVertical); gc.drawPoint(p.x + width - third, dotsVertical); gc.setForeground(c); } else { textWidget.redrawRange(offset, length, true); } } } }
/** This method initializes composite1 */ private void createComposite1() { GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 2; composite1 = new Composite(sShell, SWT.NONE); composite1.setLayout(gridLayout); createComposite(); text = new Text(composite1, SWT.BORDER | SWT.SINGLE); text.setTextLimit(30); int columns = 35; GC gc = new GC(text); FontMetrics fm = gc.getFontMetrics(); int width = columns * fm.getAverageCharWidth(); gc.dispose(); text.setLayoutData(new GridData(width, SWT.DEFAULT)); text.addModifyListener( new ModifyListener() { public void modifyText(ModifyEvent e) { String newText = text.getText(); int radix = 10; Matcher numberMatcher = null; if (hexRadioButton.getSelection()) { numberMatcher = patternHexDigits.matcher(newText); radix = 16; } else { numberMatcher = patternDecDigits.matcher(newText); } tempResult = -1; if (numberMatcher.matches()) tempResult = Long.parseLong(newText, radix); if (tempResult >= 0L && tempResult <= limit) { showButton.setEnabled(true); gotoButton.setEnabled(true); label2.setText(""); } else { showButton.setEnabled(false); gotoButton.setEnabled(false); if ("".equals(newText)) label2.setText(""); else if (tempResult < 0) label2.setText("Not a number"); else label2.setText("Location out of range"); } } }); FormData formData = new FormData(); formData.top = new FormAttachment(label); composite1.setLayoutData(formData); }
@Override public void createControl(Composite parent) { Composite topLevel = new Composite(parent, SWT.NONE); FormLayout layout = new FormLayout(); layout.marginWidth = layout.marginHeight = 5; topLevel.setLayout(layout); Label textLabel = new Label(topLevel, SWT.NONE); textLabel.setText("Wait Duration (ms): "); durationText = new Text(topLevel, SWT.SINGLE | SWT.BORDER); durationText.setText("" + xmlWait.getWaitForMilliseconds()); durationText.addListener( SWT.Verify, new Listener() { public void handleEvent(Event e) { String string = e.text; char[] chars = string.toCharArray(); for (char c : chars) { if (!('0' <= c && c <= '9')) { e.doit = false; return; } } } }); durationText.addModifyListener( new ModifyListener() { public void modifyText(ModifyEvent e) { validateInput(); } }); GC gc = new GC(durationText); FontMetrics fm = gc.getFontMetrics(); int charWidth = fm.getAverageCharWidth(); int width = durationText.computeSize(charWidth * 8, SWT.DEFAULT).x; gc.dispose(); FormData data = new FormData(width, SWT.DEFAULT); durationText.setLayoutData(data); data.left = new FormAttachment(textLabel, 5); data.top = new FormAttachment(textLabel, 0, SWT.CENTER); setControl(topLevel); validateInput(); }
protected void drawLabel(PaintEvent e) { Rectangle bounds = getBounds(); // label // drawn within this composite, ref point is composite Rectangle rlabel = new Rectangle(0, 0, bounds.width, bounds.height); // getItem().setLabeled(true); /*r.height>=fm.getAscent() && r.width>=SWITCH_ITEM_WIDTH*/ FontMetrics fm = e.gc.getFontMetrics(); if (getItem().isLabeled()) { String s = getCell() .getCalendar() .getModel() .getLabel(getItem().getItem(), getCell().getDate().getTime()); int centery = Math.max(0, (rlabel.height - fm.getAscent()) / 2 - 3); e.gc.drawString(s, rlabel.x + 3, rlabel.y + centery); } }
/** * Get the size hints for the receiver. These are used for layout data. * * @return Point - the preferred x and y coordinates */ public Point getSizeHints() { Display display = canvas.getDisplay(); GC gc = new GC(canvas); FontMetrics fm = gc.getFontMetrics(); int charWidth = fm.getAverageCharWidth(); int charHeight = fm.getHeight(); gc.dispose(); int maxWidth = display.getBounds().width / 2; int maxHeight = display.getBounds().height / 6; int fontWidth = charWidth * maxCharacterWidth; int fontHeight = charHeight * numShowItems; if (maxWidth < fontWidth) { fontWidth = maxWidth; } if (maxHeight < fontHeight) { fontHeight = maxHeight; } return new Point(fontWidth, fontHeight); }
@Override protected void createContent(final Composite parent) { fContentComposite = new Composite(parent, SWT.NONE) { @Override public Point computeSize(final int width, final int height, final boolean changed) { return super.computeSize(width, height, changed || width != getSize().x); } }; fContentComposite.setBackgroundMode(SWT.INHERIT_FORCE); final GridLayout gridLayout = LayoutUtil.applyCompositeDefaults(new GridLayout(), 2); gridLayout.horizontalSpacing = (int) ((gridLayout.horizontalSpacing) / 1.5); fContentComposite.setLayout(gridLayout); final int vIndent = Math.max(1, LayoutUtil.defaultVSpacing() / 4); final int hIndent = Math.max(2, LayoutUtil.defaultHSpacing() / 3); { // Title image fTitleImage = new Label(fContentComposite, SWT.NULL); final Image image = SharedUIResources.getImages().get(SharedUIResources.PLACEHOLDER_IMAGE_ID); fTitleImage.setImage(image); final GridData textGd = new GridData(SWT.FILL, SWT.TOP, false, false); fTitleText = new StyledText(fContentComposite, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP) { @Override public Point computeSize(int width, final int height, final boolean changed) { if (!fLayoutHint && width <= 0 && fContentComposite.getSize().x > 0) { width = fContentComposite.getSize().x - LayoutUtil.defaultHMargin() - fTitleImage.getSize().x - LayoutUtil.defaultHSpacing() - 10; } final Point size = super.computeSize(width, -1, true); // if (width >= 0) { // size.x = Math.min(size.x, width); // } return size; } }; fTitleText.setFont(JFaceResources.getDialogFont()); final GC gc = new GC(fTitleText); final FontMetrics fontMetrics = gc.getFontMetrics(); final GridData imageGd = new GridData(SWT.FILL, SWT.TOP, false, false); imageGd.horizontalIndent = hIndent; final int textHeight = fontMetrics.getAscent() + fontMetrics.getLeading(); final int imageHeight = image.getBounds().height; final int shift = Math.max(3, (int) ((fontMetrics.getDescent()) / 1.5)); if (textHeight + shift < imageHeight) { imageGd.verticalIndent = vIndent + shift; textGd.verticalIndent = vIndent + (imageHeight - textHeight); } else { imageGd.verticalIndent = vIndent + (textHeight - imageHeight) + shift; textGd.verticalIndent = vIndent; } fTitleImage.setLayoutData(imageGd); fTitleText.setLayoutData(textGd); fLayoutWorkaround = true; gc.dispose(); } fInfoText = new StyledText( fContentComposite, fMode == MODE_FOCUS ? (SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL) : (SWT.MULTI | SWT.READ_ONLY)); fInfoText.setIndent(hIndent); fInfoText.setFont(JFaceResources.getFont(PREF_DETAIL_PANE_FONT)); final GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1); // gd.widthHint = LayoutUtil.hintWidth(fInfoText, INFO_FONT, 50); fInfoText.setLayoutData(gd); setBackgroundColor(getShell().getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); setForegroundColor(getShell().getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND)); updateInput(); }
/** * Creates the progress monitor's UI parts and layouts them according to the given layout. If the * layout is <code>null</code> the part's default layout is used. * * @param layout The layout for the receiver. * @param progressIndicatorHeight The suggested height of the indicator */ protected void initialize(Layout layout, int progressIndicatorHeight) { if (layout == null) { GridLayout l = new GridLayout(); l.marginWidth = 0; l.marginHeight = 0; layout = l; } int numColumns = 1; if (fHasStopButton) numColumns++; setLayout(layout); if (layout instanceof GridLayout) ((GridLayout) layout).numColumns = numColumns; fLabel = new Label(this, SWT.LEFT); fLabel.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, numColumns, 1)); if (progressIndicatorHeight == SWT.DEFAULT) { GC gc = new GC(fLabel); FontMetrics fm = gc.getFontMetrics(); gc.dispose(); progressIndicatorHeight = fm.getHeight(); } fProgressIndicator = new ProgressIndicator(this); GridData gd = new GridData(); gd.horizontalAlignment = GridData.FILL; gd.grabExcessHorizontalSpace = true; gd.grabExcessVerticalSpace = false; gd.verticalAlignment = GridData.CENTER; gd.heightHint = progressIndicatorHeight; fProgressIndicator.setLayoutData(gd); if (fHasStopButton) { fToolBar = new ToolBar(this, SWT.FLAT); gd = new GridData(); gd.grabExcessHorizontalSpace = false; gd.grabExcessVerticalSpace = false; gd.verticalAlignment = GridData.CENTER; fToolBar.setLayoutData(gd); fStopButton = new ToolItem(fToolBar, SWT.PUSH); // It would have been nice to use the fCancelListener, but that // listener operates on the fCancelComponent which must be a control. fStopButton.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { setCanceled(true); if (fStopButton != null) { fStopButton.setEnabled(false); } } }); final Image stopImage = ImageDescriptor.createFromFile(ProgressMonitorPart.class, "images/stop.gif") .createImage(getDisplay()); // $NON-NLS-1$ final Cursor arrowCursor = new Cursor(this.getDisplay(), SWT.CURSOR_ARROW); fToolBar.setCursor(arrowCursor); fStopButton.setImage(stopImage); fStopButton.addDisposeListener( new DisposeListener() { public void widgetDisposed(DisposeEvent e) { // RAP [rh] images don't support dispose // stopImage.dispose(); arrowCursor.dispose(); } }); fStopButton.setToolTipText( JFaceResources.getString("ProgressMonitorPart.cancelToolTip")); // $NON-NLS-1$ } }
protected int getFontWidth(Button b) { GC gc = new GC(b); FontMetrics fm = gc.getFontMetrics(); gc.dispose(); return fm.getAverageCharWidth(); }
private static int getFontHeight(Font f) { FontMetrics fm = FigureUtilities.getFontMetrics(f); return fm.getHeight(); }