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); }
void pairDraw(GC gc, StyledRegion sr, int start, int end) { if (start > text.getCharCount() || end > text.getCharCount()) return; if (gc != null) { Point left = text.getLocationAtOffset(start); Point right = text.getLocationAtOffset(end); if (sr != null) { if (highlightStyle == HLS_XOR) { int resultColor = sr.fore ^ cm.getColor(text.getBackground()); if (text.getLineAtOffset(text.getCaretOffset()) == text.getLineAtOffset(start) && horzCross && horzCrossColor != null && ((StyledRegion) horzCrossColor).bback) resultColor = sr.fore ^ ((StyledRegion) horzCrossColor).back; Color color = cm.getColor(sr.bfore, resultColor); gc.setBackground(color); gc.setXORMode(true); gc.fillRectangle(left.x, left.y, right.x - left.x, gc.getFontMetrics().getHeight()); } else if (highlightStyle == HLS_OUTLINE) { Color color = cm.getColor(sr.bfore, sr.fore); gc.setForeground(color); gc.drawRectangle( left.x, left.y, right.x - left.x - 1, gc.getFontMetrics().getHeight() - 1); } else if (highlightStyle == HLS_OUTLINE2) { Color color = cm.getColor(sr.bfore, sr.fore); gc.setForeground(color); gc.setLineWidth(2); gc.drawRectangle( left.x + 1, left.y + 1, right.x - left.x - 2, gc.getFontMetrics().getHeight() - 2); } } } else { text.redrawRange(start, end - start, true); } }
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(); } }
void paintStripes(GC gc) { if (!showStripes) return; Rectangle rect = getClientArea(); // Subtracted border painted by paint. rect = new Rectangle(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4); gc.setLineWidth(2); gc.setClipping(rect); Color color = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION); gc.setBackground(color); gc.fillRectangle(rect); gc.setForeground(this.getBackground()); int step = 12; int foregroundValue = value == 0 ? step - 2 : value - 2; if (orientation == SWT.HORIZONTAL) { int y = rect.y - 1; int w = rect.width; int h = rect.height + 2; for (int i = 0; i < w; i += step) { int x = i + foregroundValue; gc.drawLine(x, y, x, h); } } else { int x = rect.x - 1; int w = rect.width + 2; int h = rect.height; for (int i = 0; i < h; i += step) { int y = i + foregroundValue; gc.drawLine(x, y, w, y); } } if (active) { value = (value + 2) % step; } }
public static void main(String[] args) { final Display display = new Display(); final Image image = new Image(display, 16, 16); GC gc = new GC(image); gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); gc.fillRectangle(image.getBounds()); gc.dispose(); final Shell shell = new Shell(display); shell.setText("Lazy Table"); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.BORDER | SWT.MULTI); table.setSize(200, 200); Thread thread = new Thread() { @Override public void run() { for (int i = 0; i < 20000; i++) { if (table.isDisposed()) return; final int[] index = new int[] {i}; display.syncExec( () -> { if (table.isDisposed()) return; TableItem item = new TableItem(table, SWT.NONE); item.setText("Table Item " + index[0]); item.setImage(image); }); } } }; thread.start(); shell.setSize(200, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } image.dispose(); display.dispose(); }
void paint(Event event) { if (row == null) return; int columnIndex = column == null ? 0 : table.indexOf(column); GC gc = event.gc; gc.setBackground(getBackground()); gc.setForeground(getForeground()); gc.fillRectangle(event.x, event.y, event.width, event.height); int x = 0; Point size = getSize(); Image image = row.getImage(columnIndex); if (image != null) { Rectangle imageSize = image.getBounds(); int imageY = (size.y - imageSize.height) / 2; gc.drawImage(image, x, imageY); x += imageSize.width; } String text = row.getText(columnIndex); if (text.length() > 0) { Rectangle bounds = row.getBounds(columnIndex); Point extent = gc.stringExtent(text); // Temporary code - need a better way to determine table trim String platform = SWT.getPlatform(); if ("win32".equals(platform)) { // $NON-NLS-1$ if (table.getColumnCount() == 0 || columnIndex == 0) { x += 2; } else { int alignmnent = column.getAlignment(); switch (alignmnent) { case SWT.LEFT: x += 6; break; case SWT.RIGHT: x = bounds.width - extent.x - 6; break; case SWT.CENTER: x += (bounds.width - x - extent.x) / 2; break; } } } else { if (table.getColumnCount() == 0) { x += 5; } else { int alignmnent = column.getAlignment(); switch (alignmnent) { case SWT.LEFT: x += 5; break; case SWT.RIGHT: x = bounds.width - extent.x - 2; break; case SWT.CENTER: x += (bounds.width - x - extent.x) / 2 + 2; break; } } } int textY = (size.y - extent.y) / 2; gc.drawString(text, x, textY); } if (isFocusControl()) { Display display = getDisplay(); gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); gc.drawFocus(0, 0, size.x, size.y); } }
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; } } }