private void markPoint(double x, double y, String label, Color fg, Color bg, GC gc) { gc.setForeground(black); gc.setBackground(bg); gc.fillOval((int) x - 3, (int) y - 3, 7, 7); gc.drawOval((int) x - 3, (int) y - 3, 6, 6); gc.setForeground(fg); gc.setBackground(white); if (y > 0) { gc.drawText(label, (int) x + 8, (int) y + 4, true); } else { gc.drawText(label, (int) x + 10, (int) y - 10, true); } }
/** * Draws the rotated text. * * @param gc the graphics context * @param text the text * @param x the x coordinate * @param y the y coordinate * @param angle the angle */ private void drawRotatedText(GC gc, String text, float x, float y, int angle) { int textWidth = gc.textExtent(text).x; int textHeight = gc.textExtent(text).y; // create image to draw text Image image = new Image(Display.getCurrent(), textWidth, textHeight); GC tmpGc = new GC(image); tmpGc.setForeground(getForeground()); tmpGc.setBackground(gc.getBackground()); tmpGc.setFont(getFont()); tmpGc.drawText(text, 0, 0); // set transform to rotate Transform transform = new Transform(gc.getDevice()); transform.translate(x, y); transform.rotate(360 - angle); gc.setTransform(transform); // draw the image on the rotated graphics context gc.drawImage(image, 0, 0); // dispose resources tmpGc.dispose(); transform.dispose(); image.dispose(); gc.setTransform(null); }
public void writeValue(int ex) { double x = xyGraph.primaryXAxis.getPositionValue(ex, false); int index = (int) x; if (index < 0) { return; } Sample sample = (Sample) trace.getDataProvider().getSample(index); if (sample != null) { double y = sample.getYValue(); int height = xyGraph.primaryYAxis.getValuePosition(y, false); int startX = xyGraph.primaryXAxis.getValuePosition((int) x, false); GC gc = new GC(canvas); Font font = new Font(null, "Verdana", 10, SWT.BOLD); gc.setFont(font); String value = FormatUtil.print(y, "#,###"); Point textSize = gc.textExtent(value); gc.drawText(value, startX + (xAxisUnitWidth - textSize.x) / 2, height - 20, true); int ground = xyGraph.primaryYAxis.getValuePosition(0, false); gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK)); gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_MAGENTA)); gc.drawRectangle( startX + (xAxisUnitWidth - lineWidth) / 2, height, lineWidth, ground - height); gc.fillRectangle( startX + (xAxisUnitWidth - lineWidth) / 2, height, lineWidth, ground - height); gc.dispose(); writedValueMode = true; lastWritedX = ex; } }
protected void drawInformation(GC gc) { super.drawInformation(gc); String info2 = "SHFIT+×ó - Ç°ÒÆ SHIFT+ÓÒ - ºóÒÆ"; Point ts = gc.textExtent(info2); Point size = getSize(); gc.drawText(info2, size.x / 2 - ts.x / 2, size.y - ts.y * 2 - 5); }
private static void paintImage(GC gc, Point size) { gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); gc.fillRectangle(0, 0, size.x, size.y); gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION)); gc.fillRoundRectangle(0, 0, size.x - 1, size.y - 1, 10, 10); gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); gc.drawRoundRectangle(0, 0, size.x - 1, size.y - 1, 10, 10); gc.drawText(gc.getFont().getFontData()[0].toString(), 10, 10, true); }
@Override public GC postCalculation(GC toDraw) { textScale = (float) canvas.imageDimension.width / (float) canvas.sourceImage.getWidth(); String text = (int) (textScale * 100) + "%"; Point textDim = toDraw.textExtent(text); toDraw.drawText( text, canvas.canvasDimension.width - textDim.x - 20, canvas.canvasDimension.height - textDim.y - 20); return toDraw; }
private void drawVersionWarning(GC gc, Display display) { gc.setBackground(versionWarningBackgroundColor); gc.setForeground(versionWarningForegroundColor); gc.fillRectangle(290, 231, 367, 49); gc.drawRectangle(290, 231, 367, 49); gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK)); gc.drawImage(exclamation_image, 304, 243); gc.setFont(devWarningFont); gc.drawText( BaseMessages.getString(PKG, "SplashDialog.DevelopmentWarning"), 335, 241); // $NON-NLS-1$ }
protected void drawButtons(GC gc) { Point size = getSize(); gc.setBackground(getBackground()); gc.setForeground(AbstractImageViewer.invert(getBackground())); int bx = 1; int i; for (i = 0; i < buttonTexts.length; i++) { Point ts = gc.textExtent(buttonTexts[i]); int by = size.y - ts.y - 8; int bw = ts.x + 7; int bh = ts.y + 6; buttonBounds[i] = new Rectangle(bx, by, bw, bh); gc.setForeground(AbstractImageViewer.invert(getBackground())); gc.drawRectangle(buttonBounds[i]); gc.drawText(buttonTexts[i], buttonBounds[i].x + 4, buttonBounds[i].y + 4); bx += bw + 2; } }
public void paintControl(PaintEvent e) { if (text == null || foreground == null) return; GC gc = e.gc; Color oldForeground = gc.getForeground(), oldBackground = gc.getBackground(); Font oldFont = gc.getFont(); int oldAntiAlias = gc.getTextAntialias(); gc.setTextAntialias(SWT.ON); if (background != null) gc.setBackground(background); if (font != null) gc.setFont(font); int finalX = x, finalY = y; if (useRelativeXY) { Point size = shell.getSize(); Point textExtent = gc.textExtent(text); if (x >= 0) finalX = x; else { finalX = size.x + x; finalX -= textExtent.x; } if (y >= 0) finalY = y; else { finalY = size.y + y; finalY -= textExtent.y; } } gc.setForeground(foreground); gc.drawText(text, finalX, finalY, background == null); gc.setForeground(oldForeground); gc.setBackground(oldBackground); gc.setFont(oldFont); gc.setTextAntialias(oldAntiAlias); }
/** * Draw the X tick. * * @param gc the graphics context */ private void drawXTick(GC gc) { int offset = axis.getTick().getAxisTickMarks().getBounds().x; // draw tick labels gc.setFont(axis.getTick().getFont()); int angle = axis.getTick().getTickLabelAngle(); for (int i = 0; i < tickLabelPositions.size(); i++) { if (axis.isValidCategoryAxis() || tickVisibilities.get(i) == true) { String text = tickLabels.get(i); int textWidth = gc.textExtent(text).x; int textHeight = gc.textExtent(text).y; if (angle == 0) { int x = (int) (tickLabelPositions.get(i) - textWidth / 2d + offset); gc.drawText(text, bounds.x + x, bounds.y); continue; } float x, y; if (axis.getPosition() == Position.Primary) { x = (float) (offset + bounds.x + tickLabelPositions.get(i) - textWidth * Math.cos(Math.toRadians(angle)) - textHeight / 2d * Math.sin(Math.toRadians(angle))); y = (float) (bounds.y + textWidth * Math.sin(Math.toRadians(angle))); } else { x = (float) (offset + bounds.x + tickLabelPositions.get(i) - textHeight / 2d * Math.sin(Math.toRadians(angle))); y = (float) (bounds.y + tickLabelMaxLength * Math.sin(Math.toRadians(angle))); } drawRotatedText(gc, text, x, y, angle); } } }
/** * Draw the Y tick. * * @param gc the graphics context */ private void drawYTick(GC gc) { int margin = Axis.MARGIN + AxisTickMarks.TICK_LENGTH; // draw tick labels gc.setFont(axis.getTick().getFont()); int figureHeight = gc.textExtent("dummy").y; for (int i = 0; i < tickLabelPositions.size(); i++) { if (tickVisibilities.size() == 0 || tickLabels.size() == 0) { break; } if (tickVisibilities.get(i) == true) { String text = tickLabels.get(i); int x = 0; if (tickLabels.get(0).startsWith("-") && !text.startsWith("-")) { x += gc.textExtent("-").x; } int y = (int) (bounds.height - 1 - tickLabelPositions.get(i) - figureHeight / 2.0 - margin); gc.drawText(text, bounds.x + x, bounds.y + y); } } }
private static void paintImage2(GC gc, Point size, int f) { gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); gc.fillRectangle(0, 0, size.x, size.y); // Scale line width, corner roundness, and font size. // Caveat: line width expands in all directions, so the origin also has to move. gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION)); gc.fillRoundRectangle(f / 2, f / 2, size.x - f, size.y - f, 10 * f, 10 * f); gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); gc.setLineWidth(f); gc.drawRoundRectangle(f / 2, f / 2, size.x - f, size.y - f, 10 * f, 10 * f); FontData fontData = gc.getFont().getFontData()[0]; fontData.setHeight(fontData.getHeight() * f); Font font = new Font(gc.getDevice(), fontData); try { gc.setFont(font); gc.drawText(fontData.toString(), 10 * f, 10 * f, true); } finally { font.dispose(); } }
/* * (non-Javadoc) * * @see * org.eclipse.nebula.widgets.gallery.AbstractGalleryItemRenderer#draw(org * .eclipse.swt.graphics.GC, org.eclipse.nebula.widgets.gallery.GalleryItem, * int, int, int, int, int) */ public void draw(GC gc, GalleryItem item, int index, int x, int y, int width, int height) { Image itemImage = item.getImage(); Color itemBackgroundColor = item.getBackground(); Color itemForegroundColor = item.getForeground(); int useableHeight = height; int imageWidth = 0; int imageHeight = 0; int xShift = 0; int yShift = 0; Point size = null; if (itemImage != null) { Rectangle itemImageBounds = itemImage.getBounds(); imageWidth = itemImageBounds.width; imageHeight = itemImageBounds.height; size = RendererHelper.getBestSize( imageWidth, imageHeight, useableHeight - 4 - this.dropShadowsSize, useableHeight - 4 - this.dropShadowsSize); xShift = ((useableHeight - size.x) >> 1) + 2; yShift = (useableHeight - size.y) >> 1; if (dropShadows) { Color c = null; for (int i = this.dropShadowsSize - 1; i >= 0; i--) { c = dropShadowsColors.get(i); gc.setForeground(c); gc.drawLine( x + useableHeight + i - xShift - 1, y + dropShadowsSize + yShift, x + useableHeight + i - xShift - 1, y + useableHeight + i - yShift); gc.drawLine( x + xShift + dropShadowsSize, y + useableHeight + i - yShift - 1, x + useableHeight + i - xShift, y - 1 + useableHeight + i - yShift); } } } // Draw selection background (rounded rectangles) if (selected || RendererHelper.isColorsEquals(itemBackgroundColor, gallery.getBackground())) { if (selected) { gc.setBackground(selectionBackgroundColor); gc.setForeground(selectionBackgroundColor); } else if (itemBackgroundColor != null) { gc.setBackground(itemBackgroundColor); } if (showRoundedSelectionCorners) gc.fillRoundRectangle(x, y, width, useableHeight, selectionRadius, selectionRadius); else gc.fillRectangle(x, y, width, useableHeight); } if (itemImage != null && size != null) { if (size.x > 0 && size.y > 0) { gc.drawImage( itemImage, 0, 0, imageWidth, imageHeight, x + xShift, y + yShift, size.x, size.y); drawAllOverlays(gc, item, x, y, size, xShift, yShift); } } if (item.getText() != null && !EMPTY_STRING.equals(item.getText()) && showLabels) { // Calculate font height (text and description) gc.setFont(textFont); String text = RendererHelper.createLabel(item.getText(), gc, width - useableHeight - 10); int textFontHeight = gc.getFontMetrics().getHeight(); String description = null; int descriptionFontHeight = 0; if (item.getText(1) != null && !EMPTY_STRING.equals(item.getText(1))) { gc.setFont(descriptionFont); description = RendererHelper.createLabel(item.getText(1), gc, width - useableHeight - 10); descriptionFontHeight = gc.getFontMetrics().getHeight(); } boolean displayText = false; boolean displayDescription = false; int remainingHeight = height - 2 - textFontHeight; if (remainingHeight > 0) displayText = true; remainingHeight -= descriptionFontHeight; if (remainingHeight > 0) displayDescription = true; // Background color gc.setBackground(selected ? selectionBackgroundColor : backgroundColor); // Draw text if (displayText) { int transY = (height - textFontHeight - 2); if (displayDescription) transY -= descriptionFontHeight; transY = transY >> 1; if (selected) { gc.setForeground(this.selectionForegroundColor); } else if (itemForegroundColor != null) { gc.setForeground(itemForegroundColor); } else { gc.setForeground(this.foregroundColor); } gc.setFont(textFont); gc.drawText(text, x + useableHeight + 5, y + transY, true); } // Draw description if (description != null && displayDescription) { gc.setForeground(this.descriptionColor); gc.setFont(descriptionFont); gc.drawText( description, x + useableHeight + 5, y + ((height - descriptionFontHeight - textFontHeight - 2) >> 1) + textFontHeight + 1, true); } } }
/** * Paints the preview title at the given position (and returns the required height) * * @param gc the graphics context to paint into * @param x the left edge of the preview rectangle * @param y the top edge of the preview rectangle * @param displayName the title string to be used */ int paintTitle(GC gc, int x, int y, boolean showFile, String displayName) { int titleHeight = 0; if (showFile && mIncludedWithin != null) { if (mManager.getMode() != INCLUDES) { displayName = "<include>"; } else { // Skip: just paint footer instead displayName = null; } } int width = getWidth(); int labelTop = y + 1; gc.setClipping(x, labelTop, width, 100); // Use font height rather than extent height since we want two adjacent // previews (which may have different display names and therefore end // up with slightly different extent heights) to have identical title // heights such that they are aligned identically int fontHeight = gc.getFontMetrics().getHeight(); if (displayName != null && displayName.length() > 0) { gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE)); Point extent = gc.textExtent(displayName); int labelLeft = Math.max(x, x + (width - extent.x) / 2); Image icon = null; Locale locale = mConfiguration.getLocale(); if (locale != null && (locale.hasLanguage() || locale.hasRegion()) && (!(mConfiguration instanceof NestedConfiguration) || ((NestedConfiguration) mConfiguration).isOverridingLocale())) { icon = locale.getFlagImage(); } if (icon != null) { int flagWidth = icon.getImageData().width; int flagHeight = icon.getImageData().height; labelLeft = Math.max(x + flagWidth / 2, labelLeft); gc.drawImage(icon, labelLeft - flagWidth / 2 - 1, labelTop); labelLeft += flagWidth / 2 + 1; gc.drawText(displayName, labelLeft, labelTop - (extent.y - flagHeight) / 2, true); } else { gc.drawText(displayName, labelLeft, labelTop, true); } labelTop += extent.y; titleHeight += fontHeight; } if (showFile && (mAlternateInput != null || mIncludedWithin != null)) { // Draw file flag, and parent folder name IFile file = mAlternateInput != null ? mAlternateInput : mIncludedWithin.getFile(); String fileName = file.getParent().getName() + File.separator + file.getName(); Point extent = gc.textExtent(fileName); Image icon = IconFactory.getInstance().getIcon("android_file"); // $NON-NLS-1$ int flagWidth = icon.getImageData().width; int flagHeight = icon.getImageData().height; int labelLeft = Math.max(x, x + (width - extent.x - flagWidth - 1) / 2); gc.drawImage(icon, labelLeft, labelTop); gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_GRAY)); labelLeft += flagWidth + 1; labelTop -= (extent.y - flagHeight) / 2; gc.drawText(fileName, labelLeft, labelTop, true); titleHeight += Math.max(titleHeight, icon.getImageData().height); } gc.setClipping((Region) null); return titleHeight; }
public static void main(String[] args) { final Display display = new Display(); final int[] count = new int[] {4}; final Image image = new Image(display, 300, 300); GC gc = new GC(image); gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); gc.fillRectangle(image.getBounds()); gc.drawText("Splash Screen", 10, 10); gc.dispose(); final Shell splash = new Shell(SWT.ON_TOP); final ProgressBar bar = new ProgressBar(splash, SWT.NONE); bar.setMaximum(count[0]); Label label = new Label(splash, SWT.NONE); label.setImage(SWTResourceManager.getImage(SplashScreen.class, "/resources/splashep.png")); FormLayout layout = new FormLayout(); splash.setLayout(layout); FormData labelData = new FormData(); labelData.right = new FormAttachment(100, 0); labelData.bottom = new FormAttachment(100, 0); label.setLayoutData(labelData); FormData progressData = new FormData(); progressData.left = new FormAttachment(0, 5); progressData.right = new FormAttachment(100, -5); progressData.bottom = new FormAttachment(100, -5); bar.setLayoutData(progressData); splash.pack(); Rectangle splashRect = splash.getBounds(); Rectangle displayRect = display.getBounds(); int x = (displayRect.width - splashRect.width) / 2; int y = (displayRect.height - splashRect.height) / 2; splash.setLocation(x, y); splash.open(); display.asyncExec( new Runnable() { @Override public void run() { Shell[] shells = new Shell[count[0]]; for (int i = 0; i < count[0]; i++) { shells[i] = new Shell(display); shells[i].setSize(300, 300); shells[i].addListener( SWT.Close, new Listener() { @Override public void handleEvent(Event e) { --count[0]; } }); bar.setSelection(i + 1); try { Thread.sleep(1000); } catch (Throwable e) { } } splash.close(); image.dispose(); // Start Main Window MainWindow.main(null); } }); while (count[0] != 0) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
void onPaint(PaintEvent event) { Display disp = getDisplay(); Rectangle rect = getClientArea(); if (rect.width == 0 || rect.height == 0) return; boolean shortenText = false; String t = text; Image img = image; int availableWidth = Math.max(0, rect.width - 2 * hIndent); Point extent = getTotalSize(img, t); if (extent.x > availableWidth) { shortenText = true; } GC gc = event.gc; String[] lines = text == null ? null : splitString(text); // shorten the text if (shortenText) { extent.x = 0; if (image != null) { availableWidth -= image.getBounds().width + GAP; } for (int i = 0; i < lines.length; i++) { Point e = gc.textExtent(lines[i], DRAW_FLAGS); if (e.x > availableWidth) { lines[i] = shortenText(gc, lines[i], availableWidth); extent.x = Math.max(extent.x, getTotalSize(img, lines[i]).x); } else { extent.x = Math.max(extent.x, e.x); } } setToolTipText(text); } // determine horizontal position int x = rect.x + hIndent; // draw the background behind the text Color background = getBackground(); if (selected) { background = Activator.getDefault().getFormColors(disp).getColor("labelex_selection_backgound"); } gc.setBackground(background); gc.fillRectangle(rect); // draw the image if (img != null) { Rectangle imageRect = img.getBounds(); gc.drawImage( img, 0, 0, imageRect.width, imageRect.height, x, (rect.height - imageRect.height) / 2, imageRect.width, imageRect.height); x += imageRect.width + GAP; extent.x -= imageRect.width + GAP; } // draw the text if (lines != null) { int lineHeight = gc.getFontMetrics().getHeight(); int textHeight = lines.length * lineHeight; int lineY = Math.max(vIndent, rect.y + (rect.height - textHeight) / 2); gc.setForeground(getForeground()); for (int i = 0; i < lines.length; i++) { int lineX = x; gc.drawText(lines[i], lineX, lineY, DRAW_FLAGS); lineY += lineHeight; } } }
int XmNexposureCallback(int w, int client_data, int call_data) { if ((style & SWT.SEPARATOR) != 0) return 0; int xDisplay = OS.XtDisplay(handle); if (xDisplay == 0) return 0; int xWindow = OS.XtWindow(handle); if (xWindow == 0) return 0; int[] argList = { OS.XmNcolormap, 0, OS.XmNwidth, 0, OS.XmNheight, 0, }; OS.XtGetValues(handle, argList, argList.length / 2); int width = argList[3], height = argList[5]; Image currentImage = image; boolean enabled = getEnabled(); if ((parent.style & SWT.FLAT) != 0) { boolean hasCursor = hasCursor(); /* Set the shadow thickness */ int thickness = 0; if (set || (hasCursor && enabled)) { thickness = Math.min(2, display.buttonShadowThickness); } argList = new int[] {OS.XmNshadowThickness, thickness}; OS.XtSetValues(handle, argList, argList.length / 2); /* Determine if hot image should be used */ if (enabled && hasCursor && hotImage != null) { currentImage = hotImage; } } GCData data = new GCData(); data.device = display; data.display = xDisplay; data.drawable = xWindow; data.font = parent.font; data.colormap = argList[1]; int xGC = OS.XCreateGC(xDisplay, xWindow, 0, null); if (xGC == 0) SWT.error(SWT.ERROR_NO_HANDLES); GC gc = GC.motif_new(xGC, data); XmAnyCallbackStruct cb = new XmAnyCallbackStruct(); OS.memmove(cb, call_data, XmAnyCallbackStruct.sizeof); if (cb.event != 0) { XExposeEvent xEvent = new XExposeEvent(); OS.memmove(xEvent, cb.event, XExposeEvent.sizeof); Rectangle rect = new Rectangle(xEvent.x, xEvent.y, xEvent.width, xEvent.height); gc.setClipping(rect); } if (!enabled) { currentImage = disabledImage; if (currentImage == null && image != null) { currentImage = new Image(display, image, SWT.IMAGE_DISABLE); } Color disabledColor = display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW); gc.setForeground(disabledColor); } else { gc.setForeground(parent.getForeground()); } gc.setBackground(parent.getBackground()); int textX = 0, textY = 0, textWidth = 0, textHeight = 0; if (text.length() != 0) { int flags = SWT.DRAW_DELIMITER | SWT.DRAW_TAB | SWT.DRAW_MNEMONIC; Point textExtent = gc.textExtent(text, flags); textWidth = textExtent.x; textHeight = textExtent.y; } int imageX = 0, imageY = 0, imageWidth = 0, imageHeight = 0; if (currentImage != null) { Rectangle imageBounds = currentImage.getBounds(); imageWidth = imageBounds.width; imageHeight = imageBounds.height; } int spacing = 0; if (textWidth != 0 && imageWidth != 0) spacing = 2; if ((parent.style & SWT.RIGHT) != 0) { imageX = (width - imageWidth - textWidth - spacing) / 2; imageY = (height - imageHeight) / 2; textX = spacing + imageX + imageWidth; textY = (height - textHeight) / 2; } else { imageX = (width - imageWidth) / 2; imageY = (height - imageHeight - textHeight - spacing) / 2; textX = (width - textWidth) / 2; textY = spacing + imageY + imageHeight; } if ((style & SWT.DROP_DOWN) != 0) { textX -= 6; imageX -= 6; } if (textWidth > 0) { int flags = SWT.DRAW_DELIMITER | SWT.DRAW_TAB | SWT.DRAW_MNEMONIC | SWT.DRAW_TRANSPARENT; gc.drawText(text, textX, textY, flags); } if (imageWidth > 0) gc.drawImage(currentImage, imageX, imageY); if ((style & SWT.DROP_DOWN) != 0) { int startX = width - 12, startY = (height - 2) / 2; int[] arrow = {startX, startY, startX + 3, startY + 3, startX + 6, startY}; gc.setBackground(parent.getForeground()); gc.fillPolygon(arrow); gc.drawPolygon(arrow); } gc.dispose(); OS.XFreeGC(xDisplay, xGC); if (!enabled && disabledImage == null) { if (currentImage != null) currentImage.dispose(); } return 0; }
void onPaint(PaintEvent event) { Rectangle rect = getClientArea(); if (rect.width == 0 || rect.height == 0) return; boolean shortenText = false; String t = text; Image img = image; int availableWidth = Math.max(0, rect.width - (leftMargin + rightMargin)); Point extent = getTotalSize(img, t); if (extent.x > availableWidth) { img = null; extent = getTotalSize(img, t); if (extent.x > availableWidth) { shortenText = true; } } GC gc = event.gc; String[] lines = text == null ? null : splitString(text); // shorten the text if (shortenText) { extent.x = 0; for (int i = 0; i < lines.length; i++) { Point e = gc.textExtent(lines[i], DRAW_FLAGS); if (e.x > availableWidth) { lines[i] = shortenText(gc, lines[i], availableWidth); extent.x = Math.max(extent.x, getTotalSize(null, lines[i]).x); } else { extent.x = Math.max(extent.x, e.x); } } if (appToolTipText == null) { super.setToolTipText(text); } } else { super.setToolTipText(appToolTipText); } // determine horizontal position int x = rect.x + leftMargin; if (align == SWT.CENTER) { x = (rect.width - extent.x) / 2; } if (align == SWT.RIGHT) { x = rect.width - rightMargin - extent.x; } // draw a background image behind the text try { if (backgroundImage != null) { // draw a background image behind the text Rectangle imageRect = backgroundImage.getBounds(); // tile image to fill space gc.setBackground(getBackground()); gc.fillRectangle(rect); int xPos = 0; while (xPos < rect.width) { int yPos = 0; while (yPos < rect.height) { gc.drawImage(backgroundImage, xPos, yPos); yPos += imageRect.height; } xPos += imageRect.width; } } else if (gradientColors != null) { // draw a gradient behind the text final Color oldBackground = gc.getBackground(); if (gradientColors.length == 1) { if (gradientColors[0] != null) gc.setBackground(gradientColors[0]); gc.fillRectangle(0, 0, rect.width, rect.height); } else { final Color oldForeground = gc.getForeground(); Color lastColor = gradientColors[0]; if (lastColor == null) lastColor = oldBackground; int pos = 0; for (int i = 0; i < gradientPercents.length; ++i) { gc.setForeground(lastColor); lastColor = gradientColors[i + 1]; if (lastColor == null) lastColor = oldBackground; gc.setBackground(lastColor); if (gradientVertical) { final int gradientHeight = (gradientPercents[i] * rect.height / 100) - pos; gc.fillGradientRectangle(0, pos, rect.width, gradientHeight, true); pos += gradientHeight; } else { final int gradientWidth = (gradientPercents[i] * rect.width / 100) - pos; gc.fillGradientRectangle(pos, 0, gradientWidth, rect.height, false); pos += gradientWidth; } } if (gradientVertical && pos < rect.height) { gc.setBackground(getBackground()); gc.fillRectangle(0, pos, rect.width, rect.height - pos); } if (!gradientVertical && pos < rect.width) { gc.setBackground(getBackground()); gc.fillRectangle(pos, 0, rect.width - pos, rect.height); } gc.setForeground(oldForeground); } gc.setBackground(oldBackground); } else { if (background != null || (getStyle() & SWT.DOUBLE_BUFFERED) == 0) { gc.setBackground(getBackground()); gc.fillRectangle(rect); } } } catch (SWTException e) { if ((getStyle() & SWT.DOUBLE_BUFFERED) == 0) { gc.setBackground(getBackground()); gc.fillRectangle(rect); } } // draw border int style = getStyle(); if ((style & SWT.SHADOW_IN) != 0 || (style & SWT.SHADOW_OUT) != 0) { paintBorder(gc, rect); } /* * Compute text height and image height. If image height is more than * the text height, draw image starting from top margin. Else draw text * starting from top margin. */ Rectangle imageRect = null; int lineHeight = 0, textHeight = 0, imageHeight = 0; if (img != null) { imageRect = img.getBounds(); imageHeight = imageRect.height; } if (lines != null) { lineHeight = gc.getFontMetrics().getHeight(); textHeight = lines.length * lineHeight; } int imageY = 0, midPoint = 0, lineY = 0; if (imageHeight > textHeight) { if (topMargin == DEFAULT_MARGIN && bottomMargin == DEFAULT_MARGIN) imageY = rect.y + (rect.height - imageHeight) / 2; else imageY = topMargin; midPoint = imageY + imageHeight / 2; lineY = midPoint - textHeight / 2; } else { if (topMargin == DEFAULT_MARGIN && bottomMargin == DEFAULT_MARGIN) lineY = rect.y + (rect.height - textHeight) / 2; else lineY = topMargin; midPoint = lineY + textHeight / 2; imageY = midPoint - imageHeight / 2; } // draw the image if (img != null) { gc.drawImage( img, 0, 0, imageRect.width, imageHeight, x, imageY, imageRect.width, imageHeight); x += imageRect.width + GAP; extent.x -= imageRect.width + GAP; } // draw the text if (lines != null) { gc.setForeground(getForeground()); for (int i = 0; i < lines.length; i++) { int lineX = x; if (lines.length > 1) { if (align == SWT.CENTER) { int lineWidth = gc.textExtent(lines[i], DRAW_FLAGS).x; lineX = x + Math.max(0, (extent.x - lineWidth) / 2); } if (align == SWT.RIGHT) { int lineWidth = gc.textExtent(lines[i], DRAW_FLAGS).x; lineX = Math.max(x, rect.x + rect.width - rightMargin - lineWidth); } } gc.drawText(lines[i], lineX, lineY, DRAW_FLAGS); lineY += lineHeight; } } }
private void drawAttitudeControls(GC gc, int width, int height) { int size = Math.min(width / 2, height); int verticalOffset = (height - size) / 2; gc.setLineWidth(2); gc.setBackground(background1); gc.fillArc(width / 2 - size, verticalOffset, size, size, 0, 360); gc.fillArc(width / 2, verticalOffset, size, size, 0, 360); gc.setBackground(background2); gc.fillArc(width / 2 - size, verticalOffset, size, size, (int) pitch, -180); gc.fillArc(width / 2, verticalOffset, size, size, (int) roll, -180); gc.setForeground(foregroundWhite); gc.drawArc(width / 2 - size, verticalOffset, size, size, 0, 360); gc.drawArc(width / 2, verticalOffset, size, size, 0, 360); String value = ""; gc.setForeground(foregroundWhite); for (int i = 0; i < 2; i++) { int centerX = width / 2 - i * size; int tickSize = 20; int step = 20; for (int theta = 10; theta <= 360; theta += step) { int x = (int) (size / 2 + size / 2 * Math.cos(theta * Math.PI / 180.0)) + centerX; int y = (int) (size / 2 + size / 2 * Math.sin(theta * Math.PI / 180.0)); int xx = (int) ((size) / 2 + (size - tickSize) / 2 * Math.cos(theta * Math.PI / 180.0)) + centerX; int yy = (int) ((size) / 2 + (size - tickSize) / 2 * Math.sin(theta * Math.PI / 180.0)); gc.drawLine(x, verticalOffset + y, xx, verticalOffset + yy); if (theta < 180) { if (theta > 90 && theta < 180) { yy = yy - 15; xx = xx + 2; value = String.valueOf(theta); } else if (theta > 45 && theta <= 90) { yy = yy - 15; xx = xx - 10; value = String.valueOf(theta); } else if (theta < 45) { yy = yy - 15; xx = xx - 15; value = String.valueOf(theta); } gc.drawText(value, xx, verticalOffset + yy, true); } else { if (theta > 270) { xx = xx - 9; value = String.valueOf(theta - 360); } else { xx = xx - 3; value = String.valueOf(theta - 360); } if (theta == 180) { yy = yy - 7; xx = xx + 10; } else if (theta == 360) { yy = yy - 7; xx = xx - 3; } gc.drawText(value, xx, verticalOffset + yy, true); } } } }
/** * Paints the preview at the given x/y position * * @param gc the graphics context to paint it into * @param x the x coordinate to paint the preview at * @param y the y coordinate to paint the preview at */ void paint(GC gc, int x, int y) { mTitleHeight = paintTitle(gc, x, y, true /*showFile*/); y += mTitleHeight; y += 2; int width = getWidth(); int height = getHeight(); if (mThumbnail != null && mError == null) { gc.drawImage(mThumbnail, x, y); if (mActive) { int oldWidth = gc.getLineWidth(); gc.setLineWidth(3); gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION)); gc.drawRectangle(x - 1, y - 1, width + 2, height + 2); gc.setLineWidth(oldWidth); } } else if (mError != null) { if (mThumbnail != null) { gc.drawImage(mThumbnail, x, y); } else { gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BORDER)); gc.drawRectangle(x, y, width, height); } gc.setClipping(x, y, width, height); Image icon = IconFactory.getInstance().getIcon("renderError"); // $NON-NLS-1$ ImageData data = icon.getImageData(); int prevAlpha = gc.getAlpha(); int alpha = 96; if (mThumbnail != null) { alpha -= 32; } gc.setAlpha(alpha); gc.drawImage(icon, x + (width - data.width) / 2, y + (height - data.height) / 2); String msg = mError; Density density = mConfiguration.getDensity(); if (density == Density.TV || density == Density.LOW) { msg = "Broken rendering library; unsupported DPI. Try using the SDK manager " + "to get updated layout libraries."; } int charWidth = gc.getFontMetrics().getAverageCharWidth(); int charsPerLine = (width - 10) / charWidth; msg = SdkUtils.wrap(msg, charsPerLine, null); gc.setAlpha(255); gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_BLACK)); gc.drawText(msg, x + 5, y + HEADER_HEIGHT, true); gc.setAlpha(prevAlpha); gc.setClipping((Region) null); } else { gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BORDER)); gc.drawRectangle(x, y, width, height); Image icon = IconFactory.getInstance().getIcon("refreshPreview"); // $NON-NLS-1$ ImageData data = icon.getImageData(); int prevAlpha = gc.getAlpha(); gc.setAlpha(96); gc.drawImage(icon, x + (width - data.width) / 2, y + (height - data.height) / 2); gc.setAlpha(prevAlpha); } if (mActive) { int left = x; int prevAlpha = gc.getAlpha(); gc.setAlpha(208); Color bg = mCanvas.getDisplay().getSystemColor(SWT.COLOR_WHITE); gc.setBackground(bg); gc.fillRectangle(left, y, x + width - left, HEADER_HEIGHT); gc.setAlpha(prevAlpha); y += 2; // Paint icons gc.drawImage(CLOSE_ICON, left, y); left += CLOSE_ICON_WIDTH; gc.drawImage(ZOOM_IN_ICON, left, y); left += ZOOM_IN_ICON_WIDTH; gc.drawImage(ZOOM_OUT_ICON, left, y); left += ZOOM_OUT_ICON_WIDTH; gc.drawImage(EDIT_ICON, left, y); left += EDIT_ICON_WIDTH; } }
private void drawGraph(PaintEvent e) { GC gc = e.gc; Point size = canvasCurve.getSize(); int gridSize = 50 - sliderZoom.getSelection(); Color black = new Color(canvasCurve.getDisplay(), 0, 0, 0); Color grey = new Color(canvasCurve.getDisplay(), 235, 235, 235); // first, draw the grid gc.setForeground(grey); for (int i = 0; i < size.x / 2; i += gridSize) { gc.drawLine(size.x / 2 - i, 0, size.x / 2 - i, size.y); gc.drawLine(size.x / 2 + i, 0, size.x / 2 + i, size.y); } for (int i = 0; i < size.y / 2; i += gridSize) { gc.drawLine(0, size.y / 2 - i, size.x, size.y / 2 - i); gc.drawLine(0, size.y / 2 + i, size.x, size.y / 2 + i); } // Draw the axis gc.setForeground(black); // Black gc.drawLine(size.x / 2, 0, size.x / 2, size.y); gc.drawLine(0, size.y / 2, size.x, size.y / 2); int labeljumps = 5; int scale = (size.x / 2 / gridSize) - ((size.x / 2 / gridSize) % labeljumps); if (scale > 50) labeljumps = 10; for (int i = 0; i < size.x / 2; i += gridSize) { if ((i / gridSize) % labeljumps == 0) { gc.drawLine(size.x / 2 + i, size.y / 2 - 8, size.x / 2 + i, size.y / 2 + 8); gc.drawLine(size.x / 2 - i, size.y / 2 - 8, size.x / 2 - i, size.y / 2 + 8); gc.drawLine(size.x / 2 - 8, size.y / 2 + i, size.x / 2 + 8, size.y / 2 + i); gc.drawLine(size.x / 2 - 8, size.y / 2 - i, size.x / 2 + 8, size.y / 2 - i); int label = i / gridSize; if (label < 10) { if (label != 0) { gc.drawText(label + "", size.x / 2 + i - 2, size.y / 2 + 10, true); // $NON-NLS-1$ gc.drawText(-label + "", size.x / 2 - i - 5, size.y / 2 + 10, true); // $NON-NLS-1$ gc.drawText(label + "", size.x / 2 + 13, size.y / 2 - i - 7, true); // $NON-NLS-1$ gc.drawText(-label + "", size.x / 2 + 13, size.y / 2 + i - 7, true); // $NON-NLS-1$ } else { gc.drawText(label + "", size.x / 2 + i + 13, size.y / 2 + 10, true); // $NON-NLS-1$ } } else { gc.drawText(label + "", size.x / 2 + i - 6, size.y / 2 + 10, true); // $NON-NLS-1$ gc.drawText(-label + "", size.x / 2 - i - 10, size.y / 2 + 10, true); // $NON-NLS-1$ gc.drawText(label + "", size.x / 2 + 13, size.y / 2 - i - 7, true); // $NON-NLS-1$ gc.drawText(-label + "", size.x / 2 + 13, size.y / 2 + i - 7, true); // $NON-NLS-1$ } } else { gc.drawLine(size.x / 2 + i, size.y / 2 - 2, size.x / 2 + i, size.y / 2 + 2); gc.drawLine(size.x / 2 - i, size.y / 2 - 2, size.x / 2 - i, size.y / 2 + 2); gc.drawLine(size.x / 2 - 2, size.y / 2 + i, size.x / 2 + 2, size.y / 2 + i); gc.drawLine(size.x / 2 - 2, size.y / 2 - i, size.x / 2 + 2, size.y / 2 - i); } } if (points != null) { gc.setForeground(blue); double step = Math.pow((double) gridSize, -1); double x1, y1, x2, y2; for (int i = 2; i < points.length; i++) { if (points[i - 2].y == 0) { if (points[i - 1] != null && points[i - 1].y != 0) { x1 = (double) points[i - 2].x / 100 / step + size.x / 2; y1 = -(double) points[i - 2].y / 100 / step + size.y / 2; x2 = (double) points[i - 1].x / 100 / step + size.x / 2; y2 = -(double) points[i - 1].y / 100 / step + size.y / 2; gc.drawLine((int) x1, (int) y1, (int) x2, (int) y2); gc.drawLine((int) x1, (int) y1 + 1, (int) x2, (int) y2 + 1); x1 = (double) points[i - 2].x / 100 / step + size.x / 2; y1 = -(double) points[i - 2].y / 100 / step + size.y / 2; x2 = (double) points[i].x / 100 / step + size.x / 2; y2 = -(double) points[i].y / 100 / step + size.y / 2; gc.drawLine((int) x1, (int) y1, (int) x2, (int) y2); gc.drawLine((int) x1, (int) y1 + 1, (int) x2, (int) y2 + 1); } else { x1 = (double) points[i - 2].x / 100 / step + size.x / 2; y1 = -(double) points[i - 2].y / 100 / step + size.y / 2; x2 = (double) points[i - 3].x / 100 / step + size.x / 2; y2 = -(double) points[i - 3].y / 100 / step + size.y / 2; gc.drawLine((int) x1, (int) y1, (int) x2, (int) y2); gc.drawLine((int) x1, (int) y1 + 1, (int) x2, (int) y2 + 1); x1 = (double) points[i - 2].x / 100 / step + size.x / 2; y1 = -(double) points[i - 2].y / 100 / step + size.y / 2; x2 = (double) points[i - 4].x / 100 / step + size.x / 2; y2 = -(double) points[i - 4].y / 100 / step + size.y / 2; gc.drawLine((int) x1, (int) y1, (int) x2, (int) y2); gc.drawLine((int) x1, (int) y1 + 1, (int) x2, (int) y2 + 1); } } else { x1 = (double) points[i - 2].x / 100 / step + size.x / 2; y1 = -(double) points[i - 2].y / 100 / step + size.y / 2; x2 = (double) points[i].x / 100 / step + size.x / 2; y2 = -(double) points[i].y / 100 / step + size.y / 2; if ((int) Math.signum(points[i - 2].y + 0.0) == (int) Math.signum(points[i].y + 0.0)) { gc.drawLine((int) x1, (int) y1, (int) x2, (int) y2); gc.drawLine((int) x1, (int) y1 + 1, (int) x2, (int) y2 + 1); } } if (points[points.length - 1].y == 0) { x1 = (double) points[points.length - 1].x / 100 / step + size.x / 2; y1 = -(double) points[points.length - 1].y / 100 / step + size.y / 2; x2 = (double) points[points.length - 2].x / 100 / step + size.x / 2; y2 = -(double) points[points.length - 2].y / 100 / step + size.y / 2; gc.drawLine((int) x1, (int) y1, (int) x2, (int) y2); gc.drawLine((int) x1, (int) y1 + 1, (int) x2, (int) y2 + 1); x1 = (double) points[points.length - 1].x / 100 / step + size.x / 2; y1 = -(double) points[points.length - 1].y / 100 / step + size.y / 2; x2 = (double) points[points.length - 3].x / 100 / step + size.x / 2; y2 = -(double) points[points.length - 3].y / 100 / step + size.y / 2; gc.drawLine((int) x1, (int) y1, (int) x2, (int) y2); gc.drawLine((int) x1, (int) y1 + 1, (int) x2, (int) y2 + 1); } } if (pointR != null && !pointR.isInfinite()) { int rX; int rY; int lX; int lY; FpPoint p = pointP; FpPoint q = pointQ; FpPoint r = pointR; if (q != null) { if (p.x < q.x && p.x < r.x) { // if pointP is the most left point lX = (int) ((double) p.x / 100 / step + size.x / 2); lY = (int) ((double) -p.y / 100 / step + size.y / 2); } else if (q.x < r.x) { // if pointQ is the most left point lX = (int) ((double) q.x / 100 / step + size.x / 2); lY = (int) ((double) -q.y / 100 / step + size.y / 2); } else { // if pointR is the most left point lX = (int) ((double) r.x / 100 / step + size.x / 2); lY = (int) ((double) r.y / 100 / step + size.y / 2); } if (p.x > q.x && p.x > r.x) { // if pointP is the most right point rX = (int) ((double) p.x / 100 / step + size.x / 2); rY = (int) ((double) -p.y / 100 / step + size.y / 2); } else if (q.x > r.x) { // if pointQ is the most right point rX = (int) ((double) q.x / 100 / step + size.x / 2); rY = (int) ((double) -q.y / 100 / step + size.y / 2); } else { // if pointR is the most left point rX = (int) ((double) r.x / 100 / step + size.x / 2); rY = (int) ((double) r.y / 100 / step + size.y / 2); } } else { if (p.x < r.x) { // if pointP is the most left point lX = (int) ((double) p.x / 100 / step + size.x / 2); lY = (int) ((double) -p.y / 100 / step + size.y / 2); rX = (int) ((double) r.x / 100 / step + size.x / 2); rY = (int) ((double) r.y / 100 / step + size.y / 2); } else { // if pointR is the most left point lX = (int) ((double) r.x / 100 / step + size.x / 2); lY = (int) ((double) r.y / 100 / step + size.y / 2); rX = (int) ((double) p.x / 100 / step + size.x / 2); rY = (int) ((double) -p.y / 100 / step + size.y / 2); } } int startX; int endX; int startY; int endY; if ((rX > lX ? rX - lX : lX - rX) > (rY > lY ? rY - lY : lY - rY)) { double alfa = (double) (rY - lY) / (double) (rX - lX); startX = lX - 30; endX = rX + 30; startY = lY - (int) (alfa * 30); endY = rY + (int) (alfa * 30); } else { double alfa = (double) (rX - lX) / (double) (rY - lY); startX = lX + (lY > rY ? (int) (alfa * 30) : -(int) (alfa * 30)); endX = rX + (lY > rY ? -(int) (alfa * 30) : (int) (alfa * 30)); ; startY = lY + (lY > rY ? 30 : -30); endY = rY + (lY > rY ? -30 : 30); } if (startY < 0) { double alfa = (double) (rX - lX) / (double) (rY - lY); startX += (double) (-startY) * alfa; startY = 0; } else if (startY > size.y) { double alfa = (double) (rX - lX) / (double) (rY - lY); startX += -(double) (startY - size.y) * alfa; startY = size.y; } if (endY < 0) { double alfa = (double) (rX - lX) / (double) (rY - lY); endX += (double) (-endY - 1) * alfa; endY = -1; } else if (endY > size.y) { double alfa = (double) (rX - lX) / (double) (rY - lY); endX -= (double) (endY - size.y) * alfa + 0.5; endY = size.y; } gc.setForeground(darkPurple); gc.setLineWidth(2); gc.drawLine(startX, startY, endX, endY); startX = (int) ((double) pointR.x / 100 / step + size.x / 2); endX = startX; if (pointR.y > 0) { startY = (int) ((double) pointR.y / 100 / step + size.y / 2) + 30; endY = (int) ((double) -pointR.y / 100 / step + size.y / 2) - 30; } else { startY = (int) ((double) pointR.y / 100 / step + size.y / 2) - 30; endY = (int) ((double) -pointR.y / 100 / step + size.y / 2) + 30; } gc.setForeground(purple); gc.drawLine(startX, startY, endX, endY); gc.setLineWidth(1); double x = (double) pointR.x / 100 / step + size.x / 2; double y = (double) pointR.y / 100 / step + size.y / 2; gc.setForeground(black); gc.setBackground(darkPurple); gc.fillOval((int) x - 3, (int) y - 3, 7, 7); gc.drawOval((int) x - 3, (int) y - 3, 6, 6); gc.setForeground(darkPurple); gc.setBackground(white); if (y < size.x / 2) { gc.drawText("-R", (int) x + 8, (int) y + 4, true); // $NON-NLS-1$ } else { gc.drawText("-R", (int) x + 8, (int) y - 10, true); // $NON-NLS-1$ } } else if (pointP != null) { gc.setForeground(darkPurple); gc.setLineWidth(2); int x = (int) ((double) pointP.x / 100 / step + size.x / 2); gc.drawLine(x, 0, x, size.y); gc.setLineWidth(1); } if (pointSelect != null) { double x = (double) pointSelect.x / 100 / step + size.x / 2; double y = -(double) pointSelect.y / 100 / step + size.y / 2; String caption = "Q"; // $NON-NLS-1$ if (pointP == null) caption = "P"; // $NON-NLS-1$ markPoint(x, y, caption, black, lightBlue, gc); } if (pointP != null) { double x = (double) pointP.x / 100 / step + size.x / 2; double y = -(double) pointP.y / 100 / step + size.y / 2; String caption = "P"; // $NON-NLS-1$ if (pointP.equals(pointQ)) caption = "P=Q"; // $NON-NLS-1$ if (pointP.equals(pointR)) caption = "P=R"; // $NON-NLS-1$ markPoint(x, y, caption, red, red, gc); } if (pointQ != null && !pointQ.equals(pointP)) { double x = (double) pointQ.x / 100 / step + size.x / 2; double y = -(double) pointQ.y / 100 / step + size.y / 2; if (!pointQ.equals(pointP)) markPoint(x, y, "Q", red, red, gc); // $NON-NLS-1$ } if (pointR != null && !pointR.isInfinite()) { double x = (double) pointR.x / 100 / step + size.x / 2; double y = -(double) pointR.y / 100 / step + size.y / 2; markPoint(x, y, "R", purple, purple, gc); // $NON-NLS-1$ } } }