winemac: Don't accidentally unminimize a window when trying to change z-order.
[wine/multimedia.git] / dlls / winemac.drv / cocoa_status_item.m
blob5b4ee94758491f65774571c19745c9931a6e8b40
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  */
21 #import <Cocoa/Cocoa.h>
22 #include "macdrv_cocoa.h"
23 #import "cocoa_app.h"
24 #import "cocoa_event.h"
27 @interface WineStatusItem : NSObject
29     NSStatusItem* item;
30     WineEventQueue* queue;
33 @property (retain, nonatomic) NSStatusItem* item;
34 @property (assign, nonatomic) WineEventQueue* queue;
36 @end
39 @implementation WineStatusItem
41 @synthesize item, queue;
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:)];
54             queue = inQueue;
55         }
56         return self;
57     }
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     }
70     - (void) removeFromStatusBar
71     {
72         if (item)
73         {
74             NSStatusBar* statusBar = [NSStatusBar systemStatusBar];
75             [statusBar removeStatusItem:item];
76             self.item = nil;
77         }
78     }
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     }
90     - (void) clicked:(id)sender
91     {
92         [self postClickedEventWithCount:1];
93     }
95     - (void) doubleClicked:(id)sender
96     {
97         [self postClickedEventWithCount:2];
98     }
100 @end
103 /***********************************************************************
104  *              macdrv_create_status_item
106  * Creates a new status item in the status bar.
107  */
108 macdrv_status_item macdrv_create_status_item(macdrv_event_queue q)
110     WineEventQueue* queue = (WineEventQueue*)q;
111     __block WineStatusItem* statusItem;
113     OnMainThread(^{
114         statusItem = [[WineStatusItem alloc] initWithEventQueue:queue];
115     });
117     return (macdrv_status_item)statusItem;
120 /***********************************************************************
121  *              macdrv_destroy_status_item
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)
128     WineStatusItem* statusItem = (WineStatusItem*)s;
130     OnMainThreadAsync(^{
131         [statusItem removeFromStatusBar];
132         [statusItem release];
133     });
136 /***********************************************************************
137  *              macdrv_set_status_item_image
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)
144     WineStatusItem* statusItem = (WineStatusItem*)s;
146     CGImageRetain(cgimage);
148     OnMainThreadAsync(^{
149         NSImage* image = nil;
150         if (cgimage)
151         {
152             NSSize size;
153             CGFloat maxSize = [[NSStatusBar systemStatusBar] thickness];
154             BOOL changed = FALSE;
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     });
173 /***********************************************************************
174  *              macdrv_set_status_item_tooltip
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)
181     WineStatusItem* statusItem = (WineStatusItem*)s;
182     NSString* tip = (NSString*)cftip;
184     if (![tip length]) tip = nil;
185     OnMainThreadAsync(^{
186         [statusItem.item setToolTip:tip];
187     });