public void mouseMove(MouseEvent e) { if ((e.stateMask & SWT.BUTTON1) == 0) return; GC gc = new GC((Canvas) e.widget); gc.drawLine(p.x, p.y, e.x, e.y); gc.dispose(); updatePoint(e); }
LRESULT WM_PRINTCLIENT(int /*long*/ wParam, int /*long*/ lParam) { LRESULT result = super.WM_PRINTCLIENT(wParam, lParam); if (OS.COMCTL32_MAJOR < 6) { RECT rect = new RECT(); OS.GetClientRect(handle, rect); GCData data = new GCData(); data.device = display; data.foreground = getForegroundPixel(); GC gc = GC.win32_new(wParam, data); drawWidget(gc, rect); gc.dispose(); } return result; }
/** Sets or clears the caret in the "Example" widget. */ void setCaret() { Caret oldCaret = canvas.getCaret(); if (caretButton.getSelection()) { Caret newCaret = new Caret(canvas, SWT.NONE); Font font = canvas.getFont(); newCaret.setFont(font); GC gc = new GC(canvas); gc.setFont(font); newCaret.setBounds(1, 1, 1, gc.getFontMetrics().getHeight()); gc.dispose(); canvas.setCaret(newCaret); canvas.setFocus(); } else { canvas.setCaret(null); } if (oldCaret != null) oldCaret.dispose(); }
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Widget"); final Table table = new Table(shell, SWT.MULTI); table.setLinesVisible(true); table.setBounds(10, 10, 100, 100); for (int i = 0; i < 9; i++) { new TableItem(table, SWT.NONE).setText("item" + i); } Button button = new Button(shell, SWT.PUSH); button.setText("Capture"); button.pack(); button.setLocation(10, 140); button.addListener( SWT.Selection, event -> { Point tableSize = table.getSize(); GC gc = new GC(table); final Image image = new Image(display, tableSize.x, tableSize.y); gc.copyArea(image, 0, 0); gc.dispose(); Shell popup = new Shell(shell); popup.setText("Image"); popup.addListener(SWT.Close, e -> image.dispose()); Canvas canvas = new Canvas(popup, SWT.NONE); canvas.setBounds(10, 10, tableSize.x + 10, tableSize.y + 10); canvas.addPaintListener(e -> e.gc.drawImage(image, 0, 0)); popup.pack(); popup.open(); }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
LRESULT WM_PAINT(int /*long*/ wParam, int /*long*/ lParam) { if (OS.COMCTL32_MAJOR >= 6) { return super.WM_PAINT(wParam, lParam); } PAINTSTRUCT ps = new PAINTSTRUCT(); GCData data = new GCData(); data.ps = ps; data.hwnd = handle; GC gc = new_GC(data); if (gc != null) { int width = ps.right - ps.left; int height = ps.bottom - ps.top; if (width != 0 && height != 0) { RECT rect = new RECT(); OS.SetRect(rect, ps.left, ps.top, ps.right, ps.bottom); drawWidget(gc, rect); } gc.dispose(); } return LRESULT.ZERO; }
LRESULT wmDrawChild(long /*int*/ wParam, long /*int*/ lParam) { DRAWITEMSTRUCT struct = new DRAWITEMSTRUCT(); OS.MoveMemory(struct, lParam, DRAWITEMSTRUCT.sizeof); if (image != null) { GCData data = new GCData(); data.device = display; GC gc = GC.win32_new(struct.hDC, data); /* * Bug in Windows. When a bitmap is included in the * menu bar, the HDC seems to already include the left * coordinate. The fix is to ignore this value when * the item is in a menu bar. */ int x = (parent.style & SWT.BAR) != 0 ? MARGIN_WIDTH * 2 : struct.left; Image image = getEnabled() ? this.image : new Image(display, this.image, SWT.IMAGE_DISABLE); gc.drawImage(image, x, struct.top + MARGIN_HEIGHT); if (this.image != image) image.dispose(); gc.dispose(); } if (parent.foreground != -1) OS.SetTextColor(struct.hDC, parent.foreground); return null; }
/** Compute the minimum size. */ private Point getTotalSize(Image image, String text) { Point size = new Point(0, 0); if (image != null) { Rectangle r = image.getBounds(); size.x += r.width; size.y += r.height; } GC gc = new GC(this); if (text != null && text.length() > 0) { Point e = gc.textExtent(text, DRAW_FLAGS); size.x += e.x; size.y = Math.max(size.y, e.y); if (image != null) size.x += GAP; } else { size.y = Math.max(size.y, gc.getFontMetrics().getHeight()); } gc.dispose(); return size; }
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(); }
/** * Causes the receiver to be resized to its preferred size. For a composite, this involves * computing the preferred size from its layout, if there is one. * * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public void pack() { checkWidget(); int width = 0; /* compute header width */ NSTableHeaderCell headerCell = nsColumn.headerCell(); NSSize size = headerCell.cellSize(); width += Math.ceil(size.width); if (image != null) { NSSize imageSize = image.handle.size(); width += Math.ceil(imageSize.width) + MARGIN; } if (parent.sortColumn == this && parent.sortDirection != SWT.NONE) { NSRect sortRect = headerCell.sortIndicatorRectForBounds(new NSRect()); width += Math.ceil(sortRect.width + 2 * MARGIN); } /* compute item widths down column */ GC gc = new GC(parent); width = Math.max(width, parent.calculateWidth(parent.items, parent.indexOf(this), gc, true)); gc.dispose(); setWidth(width); }
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; }
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) { CTabFolder folder = (CTabFolder) composite; CTabItem[] items = folder.items; CTabFolderRenderer renderer = folder.renderer; // preferred width of tab area to show all tabs int tabW = 0; int selectedIndex = folder.selectedIndex; if (selectedIndex == -1) selectedIndex = 0; GC gc = new GC(folder); for (int i = 0; i < items.length; i++) { if (folder.single) { tabW = Math.max(tabW, renderer.computeSize(i, SWT.SELECTED, gc, SWT.DEFAULT, SWT.DEFAULT).x); } else { int state = 0; if (i == selectedIndex) state |= SWT.SELECTED; tabW += renderer.computeSize(i, state, gc, SWT.DEFAULT, SWT.DEFAULT).x; } } tabW += 3; if (folder.showMax) tabW += renderer.computeSize( CTabFolderRenderer.PART_MAX_BUTTON, SWT.NONE, gc, SWT.DEFAULT, SWT.DEFAULT) .x; if (folder.showMin) tabW += renderer.computeSize( CTabFolderRenderer.PART_MIN_BUTTON, SWT.NONE, gc, SWT.DEFAULT, SWT.DEFAULT) .x; if (folder.single) tabW += renderer.computeSize( CTabFolderRenderer.PART_CHEVRON_BUTTON, SWT.NONE, gc, SWT.DEFAULT, SWT.DEFAULT) .x; if (folder.topRight != null) { Point pt = folder.topRight.computeSize(SWT.DEFAULT, folder.tabHeight, flushCache); tabW += 3 + pt.x; } gc.dispose(); int controlW = 0; int controlH = 0; // preferred size of controls in tab items for (int i = 0; i < items.length; i++) { Control control = items[i].getControl(); if (control != null && !control.isDisposed()) { Point size = control.computeSize(wHint, hHint, flushCache); controlW = Math.max(controlW, size.x); controlH = Math.max(controlH, size.y); } } int minWidth = Math.max(tabW, controlW); int minHeight = (folder.minimized) ? 0 : controlH; if (minWidth == 0) minWidth = CTabFolder.DEFAULT_WIDTH; if (minHeight == 0) minHeight = CTabFolder.DEFAULT_HEIGHT; if (wHint != SWT.DEFAULT) minWidth = wHint; if (hHint != SWT.DEFAULT) minHeight = hHint; return new Point(minWidth, minHeight); }
public static void main(String[] args) { final ImageFileNameProvider filenameProvider = new ImageFileNameProvider() { @Override public String getImagePath(int zoom) { switch (zoom) { case 150: return IMAGE_PATH_150; case 200: return IMAGE_PATH_200; default: return IMAGE_PATH_100; } } }; final ImageDataProvider imageDataProvider = new ImageDataProvider() { @Override public ImageData getImageData(int zoom) { switch (zoom) { case 150: return new ImageData(IMAGE_PATH_150); case 200: return new ImageData(IMAGE_PATH_200); default: return new ImageData(IMAGE_PATH_100); } } }; final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new GridLayout(2, false)); new Label(shell, SWT.NONE).setText(IMAGE_200 + ":"); new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_200)); new Label(shell, SWT.NONE).setText(IMAGE_150 + ":"); new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_150)); new Label(shell, SWT.NONE).setText(IMAGE_100 + ":"); new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_100)); new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); new Label(shell, SWT.NONE).setText("ImageFileNameProvider:"); new Label(shell, SWT.NONE).setImage(new Image(display, filenameProvider)); new Label(shell, SWT.NONE).setText("ImageDataProvider:"); new Label(shell, SWT.NONE).setImage(new Image(display, imageDataProvider)); new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); new Label(shell, SWT.NONE).setText("Canvas\n(PaintListener)"); final Point size = new Point(550, 35); final Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener( new PaintListener() { @Override public void paintControl(PaintEvent e) { Point size = canvas.getSize(); paintImage(e.gc, size); } }); canvas.setLayoutData(new GridData(size.x, size.y)); new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); new Label(shell, SWT.NONE).setText("Painted image\n (default resolution)"); Image image = new Image(display, size.x, size.y); GC gc = new GC(image); try { paintImage(gc, size); } finally { gc.dispose(); } new Label(shell, SWT.NONE).setImage(image); new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); new Label(shell, SWT.NONE).setText("Painted image\n(multi-res, unzoomed paint)"); new Label(shell, SWT.NONE) .setImage( new Image( display, new ImageDataProvider() { @Override public ImageData getImageData(int zoom) { Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100); GC gc = new GC(temp); try { paintImage(gc, size); return temp.getImageData(); } finally { gc.dispose(); temp.dispose(); } } })); new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL) .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); new Label(shell, SWT.NONE).setText("Painted image\n(multi-res, zoomed paint)"); new Label(shell, SWT.NONE) .setImage( new Image( display, new ImageDataProvider() { @Override public ImageData getImageData(int zoom) { Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100); GC gc = new GC(temp); try { paintImage2( gc, new Point(size.x * zoom / 100, size.y * zoom / 100), zoom / 100); return temp.getImageData(); } finally { gc.dispose(); temp.dispose(); } } })); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }