winemac: Implement rudimentary support for system tray icons as Mac status items.
[wine] / dlls / winemac.drv / cocoa_status_item.m
1 /*
2  * MACDRV Cocoa status item class
3  *
4  * Copyright 2011, 2012, 2013 Ken Thomases for CodeWeavers Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #import <Cocoa/Cocoa.h>
22 #include "macdrv_cocoa.h"
23 #import "cocoa_app.h"
24 #import "cocoa_event.h"
25
26
27 @interface WineStatusItem : NSObject
28 {
29     NSStatusItem* item;
30     WineEventQueue* queue;
31 }
32
33 @property (retain, nonatomic) NSStatusItem* item;
34 @property (assign, nonatomic) WineEventQueue* queue;
35
36 @end
37
38
39 @implementation WineStatusItem
40
41 @synthesize item, queue;
42
43     - (id) initWithEventQueue:(WineEventQueue*)inQueue
44     {
45         self = [super init];
46         if (self)
47         {
48             NSStatusBar* statusBar = [NSStatusBar systemStatusBar];
49             item = [[statusBar statusItemWithLength:NSSquareStatusItemLength] retain];
50             [item setTarget:self];
51             [item setAction:@selector(clicked:)];
52             [item setDoubleAction:@selector(doubleClicked:)];
53
54             queue = inQueue;
55         }
56         return self;
57     }
58
59     - (void) dealloc
60     {
61         if (item)
62         {
63             NSStatusBar* statusBar = [NSStatusBar systemStatusBar];
64             [statusBar removeStatusItem:item];
65             [item release];
66         }
67         [super dealloc];
68     }
69
70     - (void) removeFromStatusBar
71     {
72         if (item)
73         {
74             NSStatusBar* statusBar = [NSStatusBar systemStatusBar];
75             [statusBar removeStatusItem:item];
76             self.item = nil;
77         }
78     }
79
80     - (void) postClickedEventWithCount:(int)count
81     {
82         macdrv_event event;
83         event.type = STATUS_ITEM_CLICKED;
84         event.window = NULL;
85         event.status_item_clicked.item = (macdrv_status_item)self;
86         event.status_item_clicked.count = count;
87         [queue postEvent:&event];
88     }
89
90     - (void) clicked:(id)sender
91     {
92         [self postClickedEventWithCount:1];
93     }
94
95     - (void) doubleClicked:(id)sender
96     {
97         [self postClickedEventWithCount:2];
98     }
99
100 @end
101
102
103 /***********************************************************************
104  *              macdrv_create_status_item
105  *
106  * Creates a new status item in the status bar.
107  */
108 macdrv_status_item macdrv_create_status_item(macdrv_event_queue q)
109 {
110     WineEventQueue* queue = (WineEventQueue*)q;
111     __block WineStatusItem* statusItem;
112
113     OnMainThread(^{
114         statusItem = [[WineStatusItem alloc] initWithEventQueue:queue];
115     });
116
117     return (macdrv_status_item)statusItem;
118 }
119
120 /***********************************************************************
121  *              macdrv_destroy_status_item
122  *
123  * Removes a status item previously returned by
124  * macdrv_create_status_item() from the status bar and destroys it.
125  */
126 void macdrv_destroy_status_item(macdrv_status_item s)
127 {
128     WineStatusItem* statusItem = (WineStatusItem*)s;
129
130     OnMainThreadAsync(^{
131         [statusItem removeFromStatusBar];
132         [statusItem release];
133     });
134 }
135
136 /***********************************************************************
137  *              macdrv_set_status_item_image
138  *
139  * Sets the image for a status item.  If cgimage is NULL, clears the
140  * image of the status item (leaving it a blank spot on the menu bar).
141  */
142 void macdrv_set_status_item_image(macdrv_status_item s, CGImageRef cgimage)
143 {
144     WineStatusItem* statusItem = (WineStatusItem*)s;
145
146     CGImageRetain(cgimage);
147
148     OnMainThreadAsync(^{
149         NSImage* image = nil;
150         if (cgimage)
151         {
152             image = [[NSImage alloc] initWithCGImage:cgimage size:NSZeroSize];
153             CGImageRelease(cgimage);
154         }
155         [statusItem.item setImage:image];
156         [image release];
157     });
158 }
159
160 /***********************************************************************
161  *              macdrv_set_status_item_tooltip
162  *
163  * Sets the tooltip string for a status item.  If cftip is NULL, clears
164  * the tooltip string for the status item.
165  */
166 void macdrv_set_status_item_tooltip(macdrv_status_item s, CFStringRef cftip)
167 {
168     WineStatusItem* statusItem = (WineStatusItem*)s;
169     NSString* tip = (NSString*)cftip;
170
171     if (![tip length]) tip = nil;
172     OnMainThreadAsync(^{
173         [statusItem.item setToolTip:tip];
174     });
175 }