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; }
/* * @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(); }
/** * 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); }
protected int getFontWidth(Button b) { GC gc = new GC(b); FontMetrics fm = gc.getFontMetrics(); gc.dispose(); return fm.getAverageCharWidth(); }