private AutoCloseable configGC(final GC gc) { final int lineStyle = gc.getLineStyle(); final int alpha = gc.getAlpha(); final int[] lineDash = gc.getLineDash(); final Color foreground = gc.getForeground(); final Color background = gc.getBackground(); gc.setForeground(this.indentGuide.getColor(styledText)); gc.setBackground(styledText.getBackground()); gc.setAlpha(this.indentGuide.getTransparency()); gc.setLineStyle(SWT.LINE_CUSTOM); gc.setLineDash(new int[] {1, 2}); return new AutoCloseable() { @Override public void close() throws Exception { gc.setForeground(foreground); gc.setBackground(background); gc.setAlpha(alpha); gc.setLineStyle(lineStyle); gc.setLineDash(lineDash); } }; }
public void paint( GC gc, boolean hover, Hashtable resourceTable, boolean selected, SelectionData selData, Rectangle repaintRegion) { Image image = getImage(resourceTable); int iwidth = 0; int iheight = 0; if (image != null) { Rectangle rect = image.getBounds(); iwidth = rect.width + (isSelectable() ? 2 : 0); iheight = rect.height + (isSelectable() ? 2 : 0); } else return; Rectangle bounds = getBounds(); int ix = bounds.x + (isSelectable() ? 1 : 0); int iy = bounds.y + (isSelectable() ? 1 : 0); if (selData != null) { int leftOffset = selData.getLeftOffset(bounds.height); int rightOffset = selData.getRightOffset(bounds.height); boolean firstRow = selData.isFirstSelectionRow(bounds.y, bounds.height); boolean lastRow = selData.isLastSelectionRow(bounds.y, bounds.height); boolean selectedRow = selData.isSelectedRow(bounds.y, bounds.height); if (selectedRow) { if ((firstRow && leftOffset > ix) || (lastRow && rightOffset < ix + iwidth / 2)) { drawClipImage(gc, image, ix, iy, repaintRegion); } else { Color savedBg = gc.getBackground(); gc.setBackground(selData.bg); int sx = ix; int sy = iy; if (repaintRegion != null) { sx -= repaintRegion.x; sy -= repaintRegion.y; } gc.fillRectangle(sx, sy, iwidth, iheight); Image selImage = getSelectedImage(resourceTable, selData); gc.drawImage(selImage, sx, sy); gc.setBackground(savedBg); } } else drawClipImage(gc, image, ix, iy, repaintRegion); } else drawClipImage(gc, image, ix, iy, repaintRegion); if (selected) { int fx = bounds.x; int fy = bounds.y; if (repaintRegion != null) { fx -= repaintRegion.x; fy -= repaintRegion.y; } Color fg = gc.getForeground(); gc.setForeground(gc.getBackground()); // Clean up to avoid canceling out XOR if it is already // selected. gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1); gc.setForeground(fg); gc.drawFocus(fx, fy, bounds.width, bounds.height); } }
/** @param gc */ private void paintSpellError(GC gc) { if (ranges.isEmpty()) return; int lineStyle = gc.getLineStyle(); int lineWidth = gc.getLineWidth(); Color lineColor = gc.getForeground(); gc.setLineWidth(2); gc.setLineStyle(SWT.LINE_DOT); gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_RED)); int charCount = contentAdapter.getControlContents(control).length(); Rectangle clipping = gc.getClipping(); lineCache.clear(); for (Object obj : ranges.values().toArray()) { SpellCheckEvent range = (SpellCheckEvent) obj; int start = range.getWordContextPosition(); if (start < 0 || start >= charCount) continue; int length = Math.min(range.getInvalidWord().length(), charCount - start); if (length <= 0) continue; drawLines(gc, start, start + length - 1, clipping); } gc.setLineWidth(lineWidth); gc.setLineStyle(lineStyle); gc.setForeground(lineColor); }
@Override public void paintLayer( ILayer natLayer, GC gc, int xOffset, int yOffset, Rectangle rectangle, IConfigRegistry configRegistry) { super.paintLayer(natLayer, gc, xOffset, yOffset, rectangle, configRegistry); Color separatorColor = configRegistry.getConfigAttribute( IFreezeConfigAttributes.SEPARATOR_COLOR, DisplayMode.NORMAL); if (separatorColor == null) { separatorColor = GUIHelper.COLOR_BLUE; } gc.setClipping(rectangle); Color oldFg = gc.getForeground(); gc.setForeground(separatorColor); final int freezeWidth = freezeLayer.getWidth() - 1; if (freezeWidth > 0) { gc.drawLine( xOffset + freezeWidth, yOffset, xOffset + freezeWidth, yOffset + getHeight() - 1); } final int freezeHeight = freezeLayer.getHeight() - 1; if (freezeHeight > 0) { gc.drawLine( xOffset, yOffset + freezeHeight, xOffset + getWidth() - 1, yOffset + freezeHeight); } gc.setForeground(oldFg); }
@Test public void testRestoresState() { dispatcher.dispatch(); assertEquals(0, gc.getAlpha()); assertEquals(0, gc.getLineWidth()); assertNull(gc.getForeground()); }
/* * @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); } } } }
@Override public void paintCell(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) { Color foregroundColor = getForeGroundColour(cell, configRegistry); Color backgroundColor = getBackgroundColour(cell, configRegistry); if (backgroundColor != null && foregroundColor != null) { Color originalForeground = gc.getForeground(); Color originalBackground = gc.getBackground(); gc.setForeground(foregroundColor); gc.setBackground(backgroundColor); gc.fillGradientRectangle(bounds.x, bounds.y, bounds.width, bounds.height, vertical); gc.setForeground(originalForeground); gc.setBackground(originalBackground); } super.paintCell(cell, gc, bounds, configRegistry); }
@Override public void paint(Property property, GC gc, int x, int y, int width, int height) throws Exception { String text = getText(property); if (text != null) { // draw color sample { RGB rgb = ColorDialog.getRGB(text); if (rgb != null) { Color swtColor = new Color(null, rgb); // Color oldBackground = gc.getBackground(); Color oldForeground = gc.getForeground(); try { int width_c = SAMPLE_SIZE; int height_c = SAMPLE_SIZE; int x_c = x; int y_c = y + (height - height_c) / 2; // update rest bounds { int delta = SAMPLE_SIZE + SAMPLE_MARGIN; x += delta; width -= delta; } // fill { gc.setBackground(swtColor); gc.fillRectangle(x_c, y_c, width_c, height_c); } // draw line gc.setForeground(IColorConstants.gray); gc.drawRectangle(x_c, y_c, width_c, height_c); } finally { gc.setBackground(oldBackground); gc.setForeground(oldForeground); swtColor.dispose(); } } } // draw color text DrawUtils.drawStringCV(gc, text, x, y, width, height); } }
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); }
public void drawImage(FSImage image, int x, int y) { Image img = ((SWTFSImage) image).getImage(); if (img == null) { int width = image.getWidth(); int height = image.getHeight(); Color oldBG = _gc.getBackground(); Color oldFG = _gc.getForeground(); _gc.setBackground(_gc.getDevice().getSystemColor(SWT.COLOR_WHITE)); _gc.setForeground(_gc.getDevice().getSystemColor(SWT.COLOR_BLACK)); _gc.fillRectangle(x, y, width, height); _gc.drawRectangle(x, y, width, height); _gc.drawLine(x, y, x + width - 1, y + height - 1); _gc.drawLine(x, y + height - 1, x + width - 1, y); _gc.setBackground(oldBG); _gc.setForeground(oldFG); } else { Rectangle bounds = img.getBounds(); _gc.drawImage( img, 0, 0, bounds.width, bounds.height, x, y, image.getWidth(), image.getHeight()); } }
/** {@inheritDoc} */ @Override public void paint( final GC gc, final SWTMediaPool media, final Font label_font, final Font scale_font, final Rectangle plot_bounds) { super.paint(gc, media); final Rectangle region = getBounds(); final Color old_fg = gc.getForeground(); gc.setForeground(media.get(getColor())); gc.setFont(scale_font); // Axis and Tick marks gc.drawLine(region.x, region.y, region.x + region.width - 1, region.y); computeTicks(gc); final double high_value = range.getHigh(); final int minor_ticks = ticks.getMinorTicks(); double tick = ticks.getStart(); double prev = ticks.getPrevious(tick); for ( /**/ ; tick <= high_value && Double.isFinite(tick); tick = ticks.getNext(tick)) { // Minor ticks? for (int i = 1; i < minor_ticks; ++i) { final double minor = prev + ((tick - prev) * i) / minor_ticks; final int x = getScreenCoord(minor); if (x < region.x) continue; gc.drawLine(x, region.y, x, region.y + MINOR_TICK_LENGTH); } drawTickLabel(gc, media, tick, false); if (show_grid) { final int x = getScreenCoord(tick); // gc.setLineStyle(SWT.LINE_DOT); gc.drawLine(x, plot_bounds.y, x, plot_bounds.y + plot_bounds.height - 1); // gc.setLineStyle(SWT.LINE_SOLID); } prev = tick; } // Minor ticks after last major tick? if (Double.isFinite(tick)) for (int i = 1; i < minor_ticks; ++i) { final double minor = prev + ((tick - prev) * i) / minor_ticks; if (minor > high_value) break; final int x = getScreenCoord(minor); gc.drawLine(x, region.y, x, region.y + MINOR_TICK_LENGTH); } // Label: centered at bottom of region gc.setFont(label_font); final Point label_size = gc.textExtent(getName()); gc.drawString( getName(), region.x + (region.width - label_size.x) / 2, region.y + region.height - label_size.y - 1, false); gc.setForeground(old_fg); }
@SuppressWarnings("deprecation") public void handleEvent(Event event) { curPaintArea = getVisibleRect(); // System.out.println("event: " + event.type); if (event.type == SWT.MouseDown) { startX = event.x; startY = event.y; // start mouse activity mouseDown = true; } else if (event.type == SWT.MouseUp) { endX = event.x; endY = event.y; boolean mouseWasMoved = startX != endX || startY != endY; if (toolCanMove && mouseWasMoved) { // if the tool is able to move draw the moved image afterImageMove(); } // stop mouse activity mouseDown = false; isDragging = false; } else if (event.type == SWT.Paint) { // System.out.println("PAINT CALLED (DOESN'T MEAN I'M DRAWING)"); if (acceptRepaintRequests) { gc = event.gc; // System.out.println(toolCanDraw + "/" + toolCanMove + "/" + isDragging + "/" + // redrawBaseImage); /* * if the mouse is dragging and the current tool can * move the map we just draw what we already have * on white background. At the end of the moving * we will take care of adding the missing pieces. */ if (toolCanMove && isDragging) { // System.out.println("toolCanMove && isDragging"); if (gc != null && !gc.isDisposed() && swtImage != null) { /* * double buffer necessary, since the SWT.NO_BACKGROUND * needed by the canvas to properly draw background, doesn't * clean the parts outside the bounds of the moving panned image, * giving a spilling image effect. */ Image tmpImage = new Image(getDisplay(), curPaintArea.width, curPaintArea.height); GC tmpGc = new GC(tmpImage); tmpGc.setBackground(white); tmpGc.fillRectangle(0, 0, curPaintArea.width, curPaintArea.height); tmpGc.drawImage(swtImage, imageOrigin.x, imageOrigin.y); gc.drawImage(tmpImage, 0, 0); tmpImage.dispose(); } return; } /* * if the mouse is dragging and the current tool can * draw a boundingbox while dragging, we draw the box * keeping the current drawn image */ if (toolCanDraw && toolManager.getCursorTool().isDrawing() && isDragging) { // System.out.println("draw box: " + startX + "/" + startY + "/" + endX + // "/" + endY); if (swtImage != null) { drawFinalImage(swtImage); } gc.setXORMode(true); org.eclipse.swt.graphics.Color fC = gc.getForeground(); gc.setLineStyle(cursorToolLineStyle); gc.setLineWidth(cursorToolLineWidth); gc.setForeground(cursorToolColor); gc.drawRectangle(startX, startY, endX - startX, endY - startY); gc.setForeground(fC); gc.setXORMode(false); return; } if (!toolCanDraw && !toolCanMove && isDragging) { return; } if (curPaintArea == null || content == null || renderer == null) { return; } if (content.layers().size() == 0) { // if no layers available, return only if there are also no overlays gc.setForeground(yellow); gc.fillRectangle(0, 0, curPaintArea.width + 1, curPaintArea.height + 1); if (overlayImage == null) return; } final ReferencedEnvelope mapAOI = content.getViewport().getBounds(); if (mapAOI == null) { return; } if (redrawBaseImage) { MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.RENDERING_STARTED); publishEvent(ev); baseImage = new BufferedImage( curPaintArea.width + 1, curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = baseImage.createGraphics(); g2d.fillRect(0, 0, curPaintArea.width + 1, curPaintArea.height + 1); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // renderer.setContext(context); java.awt.Rectangle awtRectangle = Utils.toAwtRectangle(curPaintArea); renderer.paint(g2d, awtRectangle, mapAOI, getWorldToScreenTransform()); // swtImage.dispose(); if (swtImage != null && !swtImage.isDisposed()) { swtImage.dispose(); swtImage = null; } // System.out.println("READRAWBASEIMAGE"); swtImage = new Image( getDisplay(), awtToSwt(baseImage, curPaintArea.width + 1, curPaintArea.height + 1)); } if (swtImage != null) { drawFinalImage(swtImage); } MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.RENDERING_STOPPED); publishEvent(ev); clearLabelCache = true; onRenderingCompleted(); redrawBaseImage = false; } } }
/* (non-Javadoc) * @see org.eclipse.vtp.desktop.editors.themes.mantis.MantisComponentFrame#renderFrame(org.eclipse.swt.graphics.GC, int, int, java.util.Map) */ public void renderFrame(GC gc, int renderingPhase, int options, Map<String, Object> resourceMap) { Font originalFont = gc.getFont(); Color foreground = gc.getForeground(); Color background = gc.getBackground(); Color selectedColor = getColor(gc, resourceMap, "rust.selection.color", 82, 140, 55); Color elementBlue = getColor(gc, resourceMap, "rust.element.color", 207, 234, 195); Color elementGradBlue = getColor(gc, resourceMap, "rust.element.color.gradient", 161, 211, 137); Font nameFont = new Font(gc.getDevice(), "Arial", 10, SWT.NORMAL); gc.setFont(nameFont); if (upperLeft == null) { initializeGraphics(gc, resourceMap); } int width = lowerRight.x - upperLeft.x; int height = lowerRight.y - upperLeft.y; int mainBodyHeight = height - (uiElement.hasConnectors() ? 12 : 0); if (uiElement.hasConnectors()) { // draw inspector tray gc.setBackground(elementBlue); gc.fillRoundRectangle(upperLeft.x, upperLeft.y + 3, width - 1, height - 3, 20, 20); // draw inspector tray border if (selected && (options & Theme.RENDER_FLAG_NO_SELECTION) == 0) { gc.setForeground(selectedColor); } gc.drawRoundRectangle(upperLeft.x, upperLeft.y + 3, width - 1, height - 3, 20, 20); } // draw main body gc.setBackground(elementBlue); gc.fillRoundRectangle(upperLeft.x, upperLeft.y, width - 1, mainBodyHeight - 1, 20, 20); gc.setBackground(elementGradBlue); gc.fillRoundRectangle( upperLeft.x + 1, upperLeft.y + (mainBodyHeight / 2) - 1, lowerRight.x - upperLeft.x - 2, (mainBodyHeight / 2) + (mainBodyHeight % 2), 20, 20); // gc.setForeground(elementBlue); // gc.fillGradientRectangle(upperLeft.x + 2, // upperLeft.y // + (mainBodyHeight / 3), // lowerRight.x - upperLeft.x - 4, // ((mainBodyHeight / 3) * 2) - 5, // true); gc.setForeground(foreground); gc.setBackground(background); // draw main body border if (selected && (options & Theme.RENDER_FLAG_NO_SELECTION) == 0) { gc.setForeground(selectedColor); } gc.drawRoundRectangle(upperLeft.x, upperLeft.y, width - 1, mainBodyHeight - 1, 20, 20); gc.setForeground(foreground); // draw connector hot spot if (uiElement.hasConnectors()) { gc.drawLine(lowerRight.x - 19, lowerRight.y - 8, lowerRight.x - 5, lowerRight.y - 8); // gc.drawLine(lowerRight.x - 8, lowerRight.y - 15, lowerRight.x - 3, lowerRight.y - 10); gc.drawLine(lowerRight.x - 10, lowerRight.y - 3, lowerRight.x - 5, lowerRight.y - 8); } // draw icon if ((options & Theme.RENDER_FLAG_NO_ICONS) == 0) { gc.drawImage( icon, upperLeft.x + 5, upperLeft.y + (((mainBodyHeight - 16) / 2) + ((mainBodyHeight - 16) % 2))); } // draw element name int curX = upperLeft.x + 25; int curY = upperLeft.y + 5; String[] parts = this.getDesignElement().getName().split(" "); Point stringExtent = gc.stringExtent(parts[0]); int ew = stringExtent.x; gc.drawString(parts[0], curX, curY, true); curX += stringExtent.x; for (int i = 1; i < parts.length; i++) { stringExtent = gc.stringExtent(" " + parts[i]); boolean wrapped = false; if (ew + stringExtent.x > 110) // wrap it { stringExtent = gc.stringExtent(parts[i]); ew = stringExtent.x; curY += 2 + stringExtent.y; curX = upperLeft.x + 25; wrapped = true; } else ew += stringExtent.x; gc.drawString((wrapped ? "" : " ") + parts[i], curX, curY, true); curX += stringExtent.x; } // draw decorator icons if ((options & Theme.RENDER_FLAG_NO_MARKERS) == 0) { if (uiElement.hasErrors()) gc.drawImage( org.eclipse.vtp.desktop.core.Activator.getDefault() .getImageRegistry() .get("ICON_ERROR"), lowerRight.x - 17, upperLeft.y); else if (uiElement.hasWarnings()) gc.drawImage( org.eclipse.vtp.desktop.core.Activator.getDefault() .getImageRegistry() .get("ICON_WARNING"), lowerRight.x - 16, upperLeft.y); else if (uiElement.hasTodo()) gc.drawImage( org.eclipse.vtp.desktop.core.Activator.getDefault().getImageRegistry().get("ICON_TASK"), lowerRight.x - 18, upperLeft.y + 2); } // resource cleanup if (selected) { gc.setForeground(foreground); } gc.setFont(originalFont); nameFont.dispose(); }
/* (non-Javadoc) * @see org.eclipse.vtp.desktop.editors.themes.attraction.AttractionComponentFrame#renderFrame(org.eclipse.swt.graphics.GC, int, int, java.util.Map) */ public void renderFrame(GC gc, int renderingPhase, int options, Map<String, Object> resourceMap) { Font originalFont = gc.getFont(); Color foreground = gc.getForeground(); Color background = gc.getBackground(); Color selectedColor = getColor(gc, resourceMap, "attraction.selection.color", 0, 0, 255); Color elementBlue = getColor(gc, resourceMap, "attraction.element.color", 201, 229, 255); Color elementGradBlue = getColor(gc, resourceMap, "attraction.element.color.gradient", 153, 206, 255); Font nameFont = getFont(gc, resourceMap, "attraction.element.font", "Arial", 10, SWT.NORMAL); gc.setFont(nameFont); if (upperLeft == null) { initializeGraphics(gc, resourceMap); } int width = lowerRight.x - upperLeft.x; int height = lowerRight.y - upperLeft.y; gc.setBackground(elementBlue); gc.fillRoundRectangle(upperLeft.x, upperLeft.y, width - 1, height - 1, 12, 12); if ((options & Theme.RENDER_FLAG_PRINTING) == 0) { gc.setBackground(elementGradBlue); gc.fillRoundRectangle( upperLeft.x, upperLeft.y + ((lowerRight.y - upperLeft.y) / 2), lowerRight.x - upperLeft.x, ((lowerRight.y - upperLeft.y) / 2), 12, 12); gc.setForeground(elementBlue); gc.fillGradientRectangle( upperLeft.x, upperLeft.y + ((lowerRight.y - upperLeft.y) / 3), lowerRight.x - upperLeft.x, (((lowerRight.y - upperLeft.y) / 3) * 2) - 5, true); } gc.setForeground(foreground); gc.setBackground(background); // draw connector hot spot if (uiElement.hasConnectors()) { gc.drawLine(lowerRight.x - 17, lowerRight.y - 10, lowerRight.x - 3, lowerRight.y - 10); gc.drawLine(lowerRight.x - 8, lowerRight.y - 15, lowerRight.x - 3, lowerRight.y - 10); gc.drawLine(lowerRight.x - 8, lowerRight.y - 5, lowerRight.x - 3, lowerRight.y - 10); } if (selected && (options & Theme.RENDER_FLAG_NO_SELECTION) == 0) { gc.setForeground(selectedColor); } gc.drawRoundRectangle(upperLeft.x, upperLeft.y, width - 1, height - 1, 12, 12); if ((options & Theme.RENDER_FLAG_NO_ICONS) == 0) { gc.drawImage(icon, upperLeft.x + 10, upperLeft.y + 12); } int curX = upperLeft.x + 30; int curY = upperLeft.y + 15; String[] parts = this.getDesignElement().getName().split(" "); Point stringExtent = gc.stringExtent(parts[0]); int ew = stringExtent.x; gc.drawString(parts[0], curX, curY, true); curX += stringExtent.x; for (int i = 1; i < parts.length; i++) { stringExtent = gc.stringExtent(" " + parts[i]); boolean wrapped = false; if (ew + stringExtent.x > 110) // wrap it { stringExtent = gc.stringExtent(parts[i]); ew = stringExtent.x; curY += 3 + stringExtent.y; curX = upperLeft.x + 30; wrapped = true; } else ew += stringExtent.x; gc.drawString((wrapped ? "" : " ") + parts[i], curX, curY, true); curX += stringExtent.x; } if (selected) { gc.setForeground(foreground); } gc.setFont(originalFont); if ((options & Theme.RENDER_FLAG_NO_MARKERS) == 0) { if (uiElement.hasErrors()) gc.drawImage( org.eclipse.vtp.desktop.core.Activator.getDefault() .getImageRegistry() .get("ICON_ERROR"), lowerRight.x - 17, upperLeft.y); else if (uiElement.hasWarnings()) gc.drawImage( org.eclipse.vtp.desktop.core.Activator.getDefault() .getImageRegistry() .get("ICON_WARNING"), lowerRight.x - 16, upperLeft.y); else if (uiElement.hasTodo()) gc.drawImage( org.eclipse.vtp.desktop.core.Activator.getDefault().getImageRegistry().get("ICON_TASK"), lowerRight.x - 18, upperLeft.y + 2); } }
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; } } }