diff options
author | Felipe Heidrich | 2011-10-19 18:49:49 +0000 |
---|---|---|
committer | Felipe Heidrich | 2011-10-19 18:49:49 +0000 |
commit | 83784a0bd43bf551a0a4fb21e5409eeadecd384d (patch) | |
tree | 00a166c3c25407fa8b24266bbb821542554f4757 | |
parent | 1842fb6618b874714b89bdeb1ef17cab740bfb5f (diff) | |
download | eclipse.platform.swt-83784a0bd43bf551a0a4fb21e5409eeadecd384d.tar.gz eclipse.platform.swt-83784a0bd43bf551a0a4fb21e5409eeadecd384d.tar.xz eclipse.platform.swt-83784a0bd43bf551a0a4fb21e5409eeadecd384d.zip |
Bug 304001 - [mac] No SWT API to set highlighted image for status item
4 files changed, 116 insertions, 30 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/TrayItem.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/TrayItem.java index f2c639f663..e09b7514d6 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/TrayItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/TrayItem.java @@ -43,6 +43,7 @@ public class TrayItem extends Item { boolean visible = true, highlight; NSStatusItem item; NSImageView view; + Image highlightImage; /** * Constructs a new instance of this class given its parent @@ -168,6 +169,11 @@ void destroyWidget () { releaseHandle (); } +public Image getHighlightImage () { + checkWidget(); + return highlightImage; +} + Point getLocation () { NSRect rect = view.frame(); NSRect windowRect = view.window().frame(); @@ -264,6 +270,10 @@ void releaseWidget () { super.releaseWidget (); NSStatusBar statusBar = NSStatusBar.systemStatusBar(); statusBar.removeStatusItem(item); + if (toolTip != null) toolTip.item = null; + toolTip = null; + toolTipText = null; + highlightImage = null; } /** @@ -335,31 +345,14 @@ public void setImage (Image image) { checkWidget (); if (image != null && image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); super.setImage (image); - float /*double*/ width = 0; - if (image == null) { - view.setImage (null); - } else { - /* - * Feature in Cocoa. If the NSImage object being set into the view is - * the same NSImage object that is already there then the new image is - * not taken. This results in the view's image not changing even if the - * NSImage object's content has changed since it was last set into the - * view. The workaround is to temporarily set the view's image to null - * so that the new image will then be taken. - */ - NSImage current = view.image (); - if (current != null && current.id == image.handle.id) { - view.setImage (null); - item.setLength (0); - } - view.setImage (image.handle); - if (visible) { - NSSize size = image.handle.size (); - view.setFrameSize (size); - width = OS.NSSquareStatusItemLength; - } - } - item.setLength (width); + updateImage (); +} + +public void setHighlightImage (Image image) { + checkWidget (); + if (image != null && image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); + highlightImage = image; + updateImage (); } /** @@ -440,8 +433,7 @@ public void setVisible (boolean visible) { if (isDisposed ()) return; } this.visible = visible; - float /*double*/ width = image != null && visible ? OS.NSSquareStatusItemLength : 0; - item.setLength(width); + updateImage (); if (!visible) sendEvent (SWT.Hide); } @@ -496,6 +488,7 @@ boolean shouldShowMenu (NSEvent event) { void mouseDown(int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { NSEvent nsEvent = new NSEvent(theEvent); highlight = true; + updateImage(); view.setNeedsDisplay(true); if (shouldShowMenu(nsEvent)) displayMenu(); } @@ -503,7 +496,11 @@ void mouseDown(int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { void mouseDragged(int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { NSEvent nsEvent = new NSEvent(theEvent); NSRect frame = view.frame(); + boolean oldHighlight = highlight; highlight = OS.NSPointInRect(nsEvent.locationInWindow(), frame); + if (oldHighlight != highlight) { + updateImage (); + } view.setNeedsDisplay(true); if (shouldShowMenu(nsEvent)) displayMenu(); } @@ -514,8 +511,9 @@ void mouseUp(int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { if (nsEvent.type() == OS.NSLeftMouseUp) { sendSelectionEvent(nsEvent.clickCount() == 2 ? SWT.DefaultSelection : SWT.Selection); } + highlight = false; + updateImage (); } - highlight = false; view.setNeedsDisplay(true); } @@ -535,4 +533,32 @@ void drawRect(int /*long*/ id, int /*long*/ sel, NSRect rect) { item.drawStatusBarBackgroundInRect(rect, highlight); super.drawRect(id, sel, rect); } + +void updateImage () { + float /*double*/ width = 0; + Image image = this.image; + if (highlight && highlightImage != null) image = highlightImage; + if (image == null) { + view.setImage (null); + } else { + /* + * Feature in Cocoa. If the NSImage object being set into the view is + * the same NSImage object that is already there then the new image is + * not taken. This results in the view's image not changing even if the + * NSImage object's content has changed since it was last set into the + * view. The workaround is to temporarily set the view's image to null + * so that the new image will then be taken. + */ + NSImage current = view.image (); + if (current != null && current.id == image.handle.id) { + view.setImage (null); + item.setLength (0); + } + view.setImage (image.handle); + if (visible) { + width = OS.NSSquareStatusItemLength; + } + } + item.setLength (width); +} } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TrayItem.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TrayItem.java index b9558a4b83..44e4b82380 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TrayItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TrayItem.java @@ -43,6 +43,7 @@ public class TrayItem extends Item { int /*long*/ imageHandle; int /*long*/ tooltipsHandle; ImageList imageList; + Image highlightImage; /** * Constructs a new instance of this class given its parent @@ -220,6 +221,11 @@ public Tray getParent () { return parent; } +public Image getHighlightImage () { + checkWidget (); + return highlightImage; +} + /** * Returns the receiver's tool tip, or null if it has * not been set. @@ -397,6 +403,7 @@ void releaseWidget () { if (imageList != null) imageList.dispose (); imageList = null; toolTipText = null; + highlightImage = null; } /** @@ -451,6 +458,12 @@ public void removeSelectionListener (SelectionListener listener) { eventTable.unhook (SWT.DefaultSelection, listener); } +public void setHighlightImage (Image image) { + checkWidget (); + if (image != null && image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); + highlightImage = image; +} + /** * Sets the receiver's image. * diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TrayItem.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TrayItem.java index c9e45d6ac7..a4ba1a0a61 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TrayItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TrayItem.java @@ -39,7 +39,7 @@ import org.eclipse.swt.internal.win32.*; public class TrayItem extends Item { Tray parent; int id; - Image image2; + Image image2, highlightImage; ToolTip toolTip; String toolTipText; boolean visible = true; @@ -161,6 +161,24 @@ void destroyWidget () { } /** + * Returns the receiver's highlight image if it has one, or null + * if it does not. + * + * @return the receiver's highlight image + * + * @exception SWTException <ul> + * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> + * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> + * </ul> + * + * @since 3.8 + */ +public Image getHighlightImage () { + checkWidget (); + return highlightImage; +} + +/** * Returns the receiver's parent, which must be a <code>Tray</code>. * * @return the receiver's parent @@ -320,6 +338,7 @@ void releaseWidget () { toolTip = null; if (image2 != null) image2.dispose (); image2 = null; + highlightImage = null; toolTipText = null; NOTIFYICONDATA iconData = OS.IsUnicode ? (NOTIFYICONDATA) new NOTIFYICONDATAW () : new NOTIFYICONDATAA (); iconData.cbSize = NOTIFYICONDATA.sizeof; @@ -381,6 +400,27 @@ public void removeMenuDetectListener (MenuDetectListener listener) { } /** + * Sets the receiver's highlight image. + * + * @param image the new highlight image + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li> + * </ul> + * @exception SWTException <ul> + * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> + * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> + * </ul> + * + * @since 3.8 + */ +public void setHighlightImage (Image image) { + checkWidget (); + if (image != null && image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); + highlightImage = image; +} + +/** * Sets the receiver's image. * * @param image the new image diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java index 314a925b06..b52865ac8b 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java @@ -28,6 +28,11 @@ public static void main(String[] args) { Display display = new Display (); Shell shell = new Shell (display); Image image = new Image (display, 16, 16); + Image image2 = new Image (display, 16, 16); + GC gc = new GC(image2); + gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); + gc.fillRectangle(image2.getBounds()); + gc.dispose(); final Tray tray = display.getSystemTray (); if (tray == null) { System.out.println ("The system tray is not available"); @@ -70,7 +75,8 @@ public static void main(String[] args) { menu.setVisible (true); } }); - item.setImage (image); + item.setImage (image2); + item.setHighlightImage (image); } shell.setBounds(50, 50, 300, 200); shell.open (); @@ -78,6 +84,7 @@ public static void main(String[] args) { if (!display.readAndDispatch ()) display.sleep (); } image.dispose (); + image2.dispose (); display.dispose (); } } |