/** * Returns the receiver's image data. This is the icon that is associated with the receiver in the * operating system. * * @return the image data for the program, may be null */ public ImageData getImageData() { NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { NSWorkspace workspace = NSWorkspace.sharedWorkspace(); NSString fullPath; if (this.fullPath != null) { fullPath = NSString.stringWith(this.fullPath); } else { fullPath = workspace.fullPathForApplication(NSString.stringWith(name)); } if (fullPath != null) { NSImage nsImage = workspace.iconForFile(fullPath); if (nsImage != null) { NSSize size = new NSSize(); size.width = size.height = 16; nsImage.setSize(size); nsImage.retain(); Image image = Image.cocoa_new(Display.getCurrent(), SWT.BITMAP, nsImage); ImageData imageData = image.getImageData(); image.dispose(); return imageData; } } return null; } finally { pool.release(); } }
/** Frees the resources */ void freeResources() { if (images != null) { for (Image image : images) { if (image != null) image.dispose(); } images = null; } }
/** * Sets the receiver's image at a column. * * @param index the column index * @param image the new image * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed * </ul> * * @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 setImage(int index, Image image) { checkWidget(); if (image != null && image.isDisposed()) { error(SWT.ERROR_INVALID_ARGUMENT); } if (image != null && image.type == SWT.ICON) { if (image.equals(_getImage(index))) return; } int count = Math.max(1, parent.getColumnCount()); if (0 > index || index > count - 1) return; int /*long*/ pixbuf = 0; if (image != null) { ImageList imageList = parent.imageList; if (imageList == null) imageList = parent.imageList = new ImageList(); int imageIndex = imageList.indexOf(image); if (imageIndex == -1) imageIndex = imageList.add(image); pixbuf = imageList.getPixbuf(imageIndex); } int modelIndex = parent.columnCount == 0 ? Table.FIRST_COLUMN : parent.columns[index].modelIndex; OS.gtk_list_store_set(parent.modelHandle, handle, modelIndex + Table.CELL_PIXBUF, pixbuf, -1); /* * Bug in GTK. When using fixed-height-mode, * row changes do not cause the row to be repainted. The fix is to * invalidate the row when it is cleared. */ if ((parent.style & SWT.VIRTUAL) != 0) { if (OS.GTK_VERSION >= OS.VERSION(2, 3, 2) && OS.GTK_VERSION < OS.VERSION(2, 6, 3)) { redraw(); } } /* * Bug in GTK. When in fixed height mode, GTK does not recalculate the cell renderer width * when the image is changed in the model. The fix is to force it to recalculate the width if * more space is required. */ if ((parent.style & SWT.VIRTUAL) != 0 && parent.currentItem == null) { if (OS.GTK_VERSION >= OS.VERSION(2, 3, 2)) { if (image != null) { int /*long*/ parentHandle = parent.handle; int /*long*/ column = OS.gtk_tree_view_get_column(parentHandle, index); int[] w = new int[1]; int /*long*/ pixbufRenderer = parent.getPixbufRenderer(column); OS.gtk_tree_view_column_cell_get_position(column, pixbufRenderer, null, w); if (w[0] < image.getBounds().width) { /* * There is no direct way to clear the cell renderer width so we * are relying on the fact that it is done as part of modifying * the style. */ int /*long*/ style = OS.gtk_widget_get_modifier_style(parentHandle); parent.modifyStyle(parentHandle, style); } } } } cached = true; }
/** * Constructs a new Pattern given an image. Drawing with the resulting pattern will cause the * image to be tiled over the resulting area. * * <p>This operation requires the operating system's advanced graphics subsystem which may not be * available on some platforms. * * @param device the device on which to allocate the pattern * @param image the image that the pattern will draw * @exception IllegalArgumentException * <ul> * <li>ERROR_NULL_ARGUMENT - if the device is null and there is no current device, or the * image is null * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed * </ul> * * @exception SWTException * <ul> * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available * </ul> * * @exception SWTError * <ul> * <li>ERROR_NO_HANDLES if a handle for the pattern could not be obtained * </ul> * * @see #dispose() */ public Pattern(Device device, Image image) { super(device); if (image == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); this.device.checkCairo(); image.createSurface(); handle = Cairo.cairo_pattern_create_for_surface(image.surface); if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); Cairo.cairo_pattern_set_extend(handle, Cairo.CAIRO_EXTEND_REPEAT); surface = image.surface; init(); }
public void setImage(Image image) { checkWidget(); if ((style & SWT.SEPARATOR) != 0) return; if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); super.setImage(image); updateImages(getEnabled() && parent.getEnabled()); }
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER); styledText.setText(text); FontData data = display.getSystemFont().getFontData()[0]; Font font = new Font(display, data.getName(), 16, SWT.BOLD); styledText.setFont(font); styledText.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); styledText.addListener( SWT.Resize, new Listener() { public void handleEvent(Event event) { Rectangle rect = styledText.getClientArea(); Image newImage = new Image(display, 1, Math.max(1, rect.height)); GC gc = new GC(newImage); gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); gc.fillGradientRectangle(rect.x, rect.y, 1, rect.height, true); gc.dispose(); styledText.setBackgroundImage(newImage); if (oldImage != null) oldImage.dispose(); oldImage = newImage; } }); shell.setSize(700, 400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } if (oldImage != null) oldImage.dispose(); font.dispose(); display.dispose(); }
/** * Sets the receiver's disabled image to the argument, which may be null indicating that no * disabled image should be displayed. * * <p>The disabled image is displayed when the receiver is disabled. * * @param image the disabled image to display on the receiver (may be null) * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed * </ul> * * @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 setDisabledImage(Image image) { checkWidget(); if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); if ((style & SWT.SEPARATOR) != 0) return; disabledImage = image; if (!getEnabled()) redraw(); }
/** * Sets the receiver's hot image to the argument, which may be null indicating that no hot image * should be displayed. * * <p>The hot image is displayed when the mouse enters the receiver. * * @param image the hot image to display on the receiver (may be null) * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed * </ul> * * @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 setHotImage(Image image) { checkWidget(); if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); if ((style & SWT.SEPARATOR) != 0) return; hotImage = image; if ((parent.style & SWT.FLAT) != 0) redraw(); }
/** * Returns a rectangle describing the receiver's size and location relative to its parent (or its * display if its parent is null). * * @return the receiver's bounding rectangle * @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 Rectangle getBounds() { checkWidget(); if (image != null) { Rectangle rect = image.getBounds(); return new Rectangle(x, y, rect.width, rect.height); } return new Rectangle(x, y, width, height); }
/** * Returns a point describing the receiver's size. * * @return the receiver's size * @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 Point getSize() { checkWidget(); if (image != null) { Rectangle rect = image.getBounds(); return new Point(rect.width, rect.height); } return new Point(width, height); }
public void setImage(Image image) { checkWidget(); if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); if ((style & SWT.SEPARATOR) != 0) return; super.setImage(image); parent.relayout(); redraw(); }
void releaseWidget() { super.releaseWidget(); releaseImages(); control = null; toolTipText = null; disabledImage = hotImage = null; if (disabledImage2 != null) disabledImage2.dispose(); disabledImage2 = null; }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(2, false)); final Text text = new Text(shell, SWT.SEARCH | SWT.ICON_CANCEL); Image image = null; if ((text.getStyle() & SWT.ICON_CANCEL) == 0) { image = display.getSystemImage(SWT.ICON_ERROR); ToolBar toolBar = new ToolBar(shell, SWT.FLAT); ToolItem item = new ToolItem(toolBar, SWT.PUSH); item.setImage(image); item.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(""); System.out.println("Search cancelled"); } }); } text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); text.setText("Search text"); text.addSelectionListener( new SelectionAdapter() { @Override public void widgetDefaultSelected(SelectionEvent e) { if (e.detail == SWT.CANCEL) { System.out.println("Search cancelled"); } else { System.out.println("Searching for: " + text.getText() + "..."); } } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } if (image != null) image.dispose(); display.dispose(); }
void resize(int width, int height) { if (image != null) { Rectangle rect = image.getBounds(); width = rect.width; height = rect.height; } if (width == 0) width = 1; OS.FrameworkElement_Width(handle, width); OS.FrameworkElement_Height(handle, height); }
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(); }
public void setImage(Image image) { checkWidget(); if (image != null && image.isDisposed()) { error(SWT.ERROR_INVALID_ARGUMENT); } super.setImage(image); NSTableHeaderView headerView = ((NSOutlineView) parent.view).headerView(); if (headerView == null) return; int index = parent.indexOf(nsColumn); NSRect rect = headerView.headerRectOfColumn(index); headerView.setNeedsDisplayInRect(rect); }
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; }
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(); }
/** * Sets the image that the receiver will use to paint the caret to the image specified by the * argument, or to the default which is a filled rectangle if the argument is null * * @param image the new image (or null) * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed * </ul> * * @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 setImage(Image image) { checkWidget(); if (image != null && image.isDisposed()) { error(SWT.ERROR_INVALID_ARGUMENT); } this.image = image; if (image != null) { OS.Image_Source(imageHandle, image.handle); OS.UIElement_Visibility(imageHandle, OS.Visibility_Visible); } else { OS.Image_Source(imageHandle, 0); OS.UIElement_Visibility(imageHandle, OS.Visibility_Collapsed); } resize(width, height); }
/** * Sets the receiver's image to the argument, which may be <code>null</code> indicating that no * image should be displayed. * * <p>Note that a Button can display an image and text simultaneously on Windows (starting with * XP), GTK+ and OSX. On other platforms, a Button that has an image and text set into it will * display the image or text that was set most recently. * * @param image the image to display on the receiver (may be <code>null</code>) * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed * </ul> * * @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 setImage(Image image) { checkWidget(); if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); if ((style & SWT.ARROW) != 0) return; this.image = image; OS.Image_Source(imageHandle, image != null ? image.handle : 0); OS.UIElement_Visibility( imageHandle, image != null ? OS.Visibility_Visible : OS.Visibility_Collapsed); OS.UIElement_Visibility( textHandle, image != null && text.length() == 0 ? OS.Visibility_Collapsed : OS.Visibility_Visible); int spacing = image != null && text.length() != 0 ? 3 : 0; int margin = OS.gcnew_Thickness(0, 0, spacing, 0); if (margin == 0) error(SWT.ERROR_NO_HANDLES); OS.FrameworkElement_Margin(imageHandle, margin); OS.GCHandle_Free(margin); }
public void setImage(Image image) { super.setImage(image); if (imageList != null) imageList.dispose(); imageList = null; if (image != null) { if (image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); imageList = new ImageList(); int imageIndex = imageList.add(image); long /*int*/ pixbuf = imageList.getPixbuf(imageIndex); OS.gtk_image_set_from_pixbuf(imageHandle, pixbuf); if (text.length() == 0) OS.gtk_widget_hide(labelHandle); OS.gtk_widget_show(imageHandle); } else { OS.gtk_image_set_from_pixbuf(imageHandle, 0); OS.gtk_widget_show(labelHandle); OS.gtk_widget_hide(imageHandle); } }
/** 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; }
/** * This implementation of <code>dragFinished</code> disposes the image that was created in <code> * TableDragSourceEffect.dragStart</code>. * * <p>Subclasses that override this method should call <code>super.dragFinished(event)</code> to * dispose the image in the default implementation. * * @param event the information associated with the drag finished event */ public void dragFinished(DragSourceEvent event) { if (dragSourceImage != null) dragSourceImage.dispose(); dragSourceImage = null; }
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); } }
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; }
public static void main(String[] args) { Shell shell = new Shell(); Display display = shell.getDisplay(); shell.setLayout(new FillLayout()); shell.setText("ExpandBar Example"); Menu menubar = new Menu(shell, SWT.BAR); shell.setMenuBar(menubar); MenuItem fileItem = new MenuItem(menubar, SWT.CASCADE); fileItem.setText("&File"); Menu submenu = new Menu(shell, SWT.DROP_DOWN); fileItem.setMenu(submenu); item = new MenuItem(submenu, SWT.PUSH); item.setText("New ExpandItem"); bar = new ExpandBar(shell, SWT.V_SCROLL); image = display.getSystemImage(SWT.ICON_QUESTION); // First item Composite composite = new Composite(bar, SWT.NONE); Menu popupmenu = new Menu(shell, SWT.POP_UP); MenuItem popupItem = new MenuItem(popupmenu, SWT.PUSH); popupItem.setText("Popup"); composite.setMenu(popupmenu); GridLayout layout = new GridLayout(2, false); layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10; layout.verticalSpacing = 10; composite.setLayout(layout); Label label = new Label(composite, SWT.NONE); label.setImage(display.getSystemImage(SWT.ICON_ERROR)); label = new Label(composite, SWT.NONE); label.setText("SWT.ICON_ERROR"); label = new Label(composite, SWT.NONE); label.setImage(display.getSystemImage(SWT.ICON_INFORMATION)); label = new Label(composite, SWT.NONE); label.setText("SWT.ICON_INFORMATION"); label = new Label(composite, SWT.NONE); label.setImage(display.getSystemImage(SWT.ICON_WARNING)); label = new Label(composite, SWT.NONE); label.setText("SWT.ICON_WARNING"); label = new Label(composite, SWT.NONE); label.setImage(display.getSystemImage(SWT.ICON_QUESTION)); label = new Label(composite, SWT.NONE); label.setText("SWT.ICON_QUESTION"); ExpandItem item1 = new ExpandItem(bar, SWT.NONE); item1.setText("What is your favorite icon"); item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); item1.setControl(composite); item1.setImage(image); // Second item composite = new Composite(bar, SWT.NONE); layout = new GridLayout(2, true); layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10; layout.verticalSpacing = 10; composite.setLayout(layout); Button button1 = new Button(composite, SWT.PUSH); button1.setText("Button 1"); Button button2 = new Button(composite, SWT.PUSH); button2.setText("Button 2"); ExpandItem item0 = new ExpandItem(bar, SWT.NONE); item0.setText("What is your favorite button"); item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); item0.setControl(composite); item0.setImage(image); item0.setExpanded(false); item.addListener( SWT.Selection, new Listener() { @Override public void handleEvent(Event e) { ExpandItem item2 = new ExpandItem(bar, SWT.NONE); Composite composite = new Composite(bar, SWT.NONE); GridLayout layout = new GridLayout(2, false); composite.setLayout(layout); Label label = new Label(composite, SWT.NONE); label.setText("What is your name?"); Composite pcomposite = new Composite(composite, SWT.NONE); Text text = new Text(pcomposite, SWT.NONE); item2.setText("New Question"); text.pack(); composite.pack(); Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT); item2.setHeight(size.y); item2.setControl(composite); item2.setImage(image); item2.setExpanded(true); } }); bar.setSpacing(8); shell.setSize(400, 550); shell.open(); System.out.println("------------------------------"); System.out.println("getSize: " + button1.getSize()); System.out.println("getBounds: " + button1.getBounds()); System.out.println("computeSize: " + button1.computeSize(SWT.DEFAULT, SWT.DEFAULT)); System.out.println("getLocation: " + button1.getLocation()); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } image.dispose(); display.dispose(); }
void updateImages(boolean enabled) { if ((style & SWT.SEPARATOR) != 0) return; long /*int*/ hwnd = parent.handle; TBBUTTONINFO info = new TBBUTTONINFO(); info.cbSize = TBBUTTONINFO.sizeof; info.dwMask = OS.TBIF_IMAGE; OS.SendMessage(hwnd, OS.TB_GETBUTTONINFO, id, info); if (info.iImage == OS.I_IMAGENONE && image == null) return; ImageList imageList = parent.getImageList(); ImageList hotImageList = parent.getHotImageList(); ImageList disabledImageList = parent.getDisabledImageList(); if (info.iImage == OS.I_IMAGENONE) { Rectangle bounds = image.getBounds(); int listStyle = parent.style & SWT.RIGHT_TO_LEFT; if (imageList == null) { imageList = display.getImageListToolBar(listStyle, bounds.width, bounds.height); } if (disabledImageList == null) { disabledImageList = display.getImageListToolBarDisabled(listStyle, bounds.width, bounds.height); } if (hotImageList == null) { hotImageList = display.getImageListToolBarHot(listStyle, bounds.width, bounds.height); } Image disabled = disabledImage; if (disabledImage == null) { if (disabledImage2 != null) disabledImage2.dispose(); disabledImage2 = null; disabled = image; if (!enabled) { disabled = disabledImage2 = new Image(display, image, SWT.IMAGE_DISABLE); } } /* * Bug in Windows. When a tool item with the style * BTNS_CHECK or BTNS_CHECKGROUP is selected and then * disabled, the item does not draw using the disabled * image. The fix is to assign the disabled image in * all image lists. */ Image image2 = image, hot = hotImage; if ((style & (SWT.CHECK | SWT.RADIO)) != 0) { if (!enabled) image2 = hot = disabled; } info.iImage = imageList.add(image2); disabledImageList.add(disabled); hotImageList.add(hot != null ? hot : image2); parent.setImageList(imageList); parent.setDisabledImageList(disabledImageList); parent.setHotImageList(hotImageList); } else { Image disabled = null; if (disabledImageList != null) { if (image != null) { if (disabledImage2 != null) disabledImage2.dispose(); disabledImage2 = null; disabled = disabledImage; if (disabledImage == null) { disabled = image; if (!enabled) { disabled = disabledImage2 = new Image(display, image, SWT.IMAGE_DISABLE); } } } disabledImageList.put(info.iImage, disabled); } /* * Bug in Windows. When a tool item with the style * BTNS_CHECK or BTNS_CHECKGROUP is selected and then * disabled, the item does not draw using the disabled * image. The fix is to use the disabled image in all * image lists. */ Image image2 = image, hot = hotImage; if ((style & (SWT.CHECK | SWT.RADIO)) != 0) { if (!enabled) image2 = hot = disabled; } if (imageList != null) imageList.put(info.iImage, image2); if (hotImageList != null) { hotImageList.put(info.iImage, hot != null ? hot : image2); } if (image == null) info.iImage = OS.I_IMAGENONE; } /* * Bug in Windows. If the width of an item has already been * calculated, the tool bar control will not recalculate it to * include the space for the image. The fix is to set the width * to zero, forcing the control recalculate the width for the item. */ info.dwMask |= OS.TBIF_SIZE; info.cx = 0; OS.SendMessage(hwnd, OS.TB_SETBUTTONINFO, id, info); long /*int*/ hFont = OS.SendMessage(hwnd, OS.WM_GETFONT, 0, 0); OS.SendMessage(hwnd, OS.WM_SETFONT, hFont, 0); parent.layoutItems(); }
public static void main(String[] args) { final Display display = new Display(); final Image image = display.getSystemImage(SWT.ICON_INFORMATION); final Shell shell = new Shell(display, SWT.NO_TRIM); Region region = new Region(); final ImageData imageData = image.getImageData(); if (imageData.alphaData != null) { Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < imageData.height; y++) { for (int x = 0; x < imageData.width; x++) { if (imageData.getAlpha(x, y) == 255) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } else { ImageData mask = imageData.getTransparencyMask(); Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < mask.height; y++) { for (int x = 0; x < mask.width; x++) { if (mask.getPixel(x, y) != 0) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } shell.setRegion(region); Listener l = new Listener() { /** The x/y of the MouseDown, relative to top-left of the shell. */ int startX, startY; @Override public void handleEvent(Event e) { if (e.type == SWT.KeyDown && e.character == SWT.ESC) { shell.dispose(); } if (e.type == SWT.MouseDown && e.button == 1) { Point p = shell.toDisplay(e.x, e.y); Point loc = shell.getLocation(); startX = p.x - loc.x; startY = p.y - loc.y; } if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) { Point p = shell.toDisplay(e.x, e.y); p.x -= startX; p.y -= startY; shell.setLocation(p); } if (e.type == SWT.Paint) { e.gc.drawImage(image, imageData.x, imageData.y); } } }; shell.addListener(SWT.KeyDown, l); shell.addListener(SWT.MouseDown, l); shell.addListener(SWT.MouseMove, l); shell.addListener(SWT.Paint, l); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } region.dispose(); image.dispose(); display.dispose(); }
Image getDragSourceImage(DragSourceEvent event) { if (dragSourceImage != null) dragSourceImage.dispose(); dragSourceImage = null; Table table = (Table) control; if (OS.GTK_VERSION < OS.VERSION(2, 2, 0)) return null; // TEMPORARY CODE if (table.isListening(SWT.EraseItem) || table.isListening(SWT.PaintItem)) return null; /* * Bug in GTK. gtk_tree_selection_get_selected_rows() segmentation faults * in versions smaller than 2.2.4 if the model is NULL. The fix is * to give a valid pointer instead. */ long /*int*/ handle = table.handle; long /*int*/ selection = OS.gtk_tree_view_get_selection(handle); long /*int*/[] model = OS.GTK_VERSION < OS.VERSION(2, 2, 4) ? new long /*int*/[1] : null; long /*int*/ list = OS.gtk_tree_selection_get_selected_rows(selection, model); if (list == 0) return null; int count = Math.min(10, OS.g_list_length(list)); Display display = table.getDisplay(); if (count == 1) { long /*int*/ path = OS.g_list_nth_data(list, 0); long /*int*/ pixmap = OS.gtk_tree_view_create_row_drag_icon(handle, path); dragSourceImage = Image.gtk_new(display, SWT.ICON, pixmap, 0); } else { int width = 0, height = 0; int[] w = new int[1], h = new int[1]; int[] yy = new int[count], hh = new int[count]; long /*int*/[] pixmaps = new long /*int*/[count]; GdkRectangle rect = new GdkRectangle(); for (int i = 0; i < count; i++) { long /*int*/ path = OS.g_list_nth_data(list, i); OS.gtk_tree_view_get_cell_area(handle, path, 0, rect); pixmaps[i] = OS.gtk_tree_view_create_row_drag_icon(handle, path); OS.gdk_drawable_get_size(pixmaps[i], w, h); width = Math.max(width, w[0]); height = rect.y + h[0] - yy[0]; yy[i] = rect.y; hh[i] = h[0]; } long /*int*/ source = OS.gdk_pixmap_new(OS.GDK_ROOT_PARENT(), width, height, -1); long /*int*/ gcSource = OS.gdk_gc_new(source); long /*int*/ mask = OS.gdk_pixmap_new(OS.GDK_ROOT_PARENT(), width, height, 1); long /*int*/ gcMask = OS.gdk_gc_new(mask); GdkColor color = new GdkColor(); color.pixel = 0; OS.gdk_gc_set_foreground(gcMask, color); OS.gdk_draw_rectangle(mask, gcMask, 1, 0, 0, width, height); color.pixel = 1; OS.gdk_gc_set_foreground(gcMask, color); for (int i = 0; i < count; i++) { OS.gdk_draw_drawable(source, gcSource, pixmaps[i], 0, 0, 0, yy[i] - yy[0], -1, -1); OS.gdk_draw_rectangle(mask, gcMask, 1, 0, yy[i] - yy[0], width, hh[i]); OS.g_object_unref(pixmaps[i]); } OS.g_object_unref(gcSource); OS.g_object_unref(gcMask); dragSourceImage = Image.gtk_new(display, SWT.ICON, source, mask); } OS.g_list_free(list); return dragSourceImage; }
private void drag(Event dragEvent) { DNDEvent event = new DNDEvent(); event.widget = this; event.x = dragEvent.x; event.y = dragEvent.y; event.time = OS.GetMessageTime(); event.doit = true; notifyListeners(DND.DragStart, event); if (!event.doit || transferAgents == null || transferAgents.length == 0) return; int[] pdwEffect = new int[1]; int operations = opToOs(getStyle()); Display display = control.getDisplay(); String key = "org.eclipse.swt.internal.win32.runMessagesInIdle"; // $NON-NLS-1$ Object oldValue = display.getData(key); display.setData(key, Boolean.TRUE); ImageList imagelist = null; Image image = event.image; hwndDrag = 0; topControl = null; if (image != null) { imagelist = new ImageList(SWT.NONE); imagelist.add(image); topControl = control.getShell(); /* * Bug in Windows. The image is inverted if the shell is RIGHT_TO_LEFT. * The fix is to create a transparent window that covers the shell client * area and use it during the drag to prevent the image from being inverted. * On XP if the shell is RTL, the image is not displayed. */ int offsetX = event.offsetX; hwndDrag = topControl.handle; if ((topControl.getStyle() & SWT.RIGHT_TO_LEFT) != 0) { offsetX = image.getBounds().width - offsetX; RECT rect = new RECT(); OS.GetClientRect(topControl.handle, rect); hwndDrag = OS.CreateWindowEx( OS.WS_EX_TRANSPARENT | OS.WS_EX_NOINHERITLAYOUT, WindowClass, null, OS.WS_CHILD | OS.WS_CLIPSIBLINGS, 0, 0, rect.right - rect.left, rect.bottom - rect.top, topControl.handle, 0, OS.GetModuleHandle(null), null); OS.ShowWindow(hwndDrag, OS.SW_SHOW); } OS.ImageList_BeginDrag(imagelist.getHandle(), 0, offsetX, event.offsetY); /* * Feature in Windows. When ImageList_DragEnter() is called, * it takes a snapshot of the screen If a drag is started * when another window is in front, then the snapshot will * contain part of the other window, causing pixel corruption. * The fix is to force all paints to be delivered before * calling ImageList_DragEnter(). */ if (OS.IsWinCE) { OS.UpdateWindow(topControl.handle); } else { int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN; OS.RedrawWindow(topControl.handle, null, 0, flags); } POINT pt = new POINT(); pt.x = dragEvent.x; pt.y = dragEvent.y; OS.MapWindowPoints(control.handle, 0, pt, 1); RECT rect = new RECT(); OS.GetWindowRect(hwndDrag, rect); OS.ImageList_DragEnter(hwndDrag, pt.x - rect.left, pt.y - rect.top); } int result = COM.DRAGDROP_S_CANCEL; try { result = COM.DoDragDrop(iDataObject.getAddress(), iDropSource.getAddress(), operations, pdwEffect); } finally { // ensure that we don't leave transparent window around if (hwndDrag != 0) { OS.ImageList_DragLeave(hwndDrag); OS.ImageList_EndDrag(); imagelist.dispose(); if (hwndDrag != topControl.handle) OS.DestroyWindow(hwndDrag); hwndDrag = 0; topControl = null; } display.setData(key, oldValue); } int operation = osToOp(pdwEffect[0]); if (dataEffect == DND.DROP_MOVE) { operation = (operation == DND.DROP_NONE || operation == DND.DROP_COPY) ? DND.DROP_TARGET_MOVE : DND.DROP_MOVE; } else { if (dataEffect != DND.DROP_NONE) { operation = dataEffect; } } event = new DNDEvent(); event.widget = this; event.time = OS.GetMessageTime(); event.doit = (result == COM.DRAGDROP_S_DROP); event.detail = operation; notifyListeners(DND.DragEnd, event); dataEffect = DND.DROP_NONE; }