Release 1.5.29.
[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 = macdrv_create_event(STATUS_ITEM_CLICKED, nil);
84         event->status_item_clicked.item = (macdrv_status_item)self;
85         event->status_item_clicked.count = count;
86         [queue postEvent:event];
87         macdrv_release_event(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             NSSize size;
153             CGFloat maxSize = [[NSStatusBar systemStatusBar] thickness];
154             BOOL changed = FALSE;
155
156             image = [[NSImage alloc] initWithCGImage:cgimage size:NSZeroSize];
157             CGImageRelease(cgimage);
158             size = [image size];
159             while (size.width > maxSize || size.height > maxSize)
160             {
161                 size.width /= 2.0;
162                 size.height /= 2.0;
163                 changed = TRUE;
164             }
165             if (changed)
166                 [image setSize:size];
167         }
168         [statusItem.item setImage:image];
169         [image release];
170     });
171 }
172
173 /***********************************************************************
174  *              macdrv_set_status_item_tooltip
175  *
176  * Sets the tooltip string for a status item.  If cftip is NULL, clears
177  * the tooltip string for the status item.
178  */
179 void macdrv_set_status_item_tooltip(macdrv_status_item s, CFStringRef cftip)
180 {
181     WineStatusItem* statusItem = (WineStatusItem*)s;
182     NSString* tip = (NSString*)cftip;
183
184     if (![tip length]) tip = nil;
185     OnMainThreadAsync(^{
186         [statusItem.item setToolTip:tip];
187     });
188 }