source: trunk/src/gui/util/qsystemtrayicon_mac.mm@ 632

Last change on this file since 632 was 561, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 19.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/****************************************************************************
43**
44** Copyright (c) 2007-2008, Apple, Inc.
45**
46** All rights reserved.
47**
48** Redistribution and use in source and binary forms, with or without
49** modification, are permitted provided that the following conditions are met:
50**
51** * Redistributions of source code must retain the above copyright notice,
52** this list of conditions and the following disclaimer.
53**
54** * Redistributions in binary form must reproduce the above copyright notice,
55** this list of conditions and the following disclaimer in the documentation
56** and/or other materials provided with the distribution.
57**
58** * Neither the name of Apple, Inc. nor the names of its contributors
59** may be used to endorse or promote products derived from this software
60** without specific prior written permission.
61**
62** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
63** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
64** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
65** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
66** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
67** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
68** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
69** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
70** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
71** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73**
74****************************************************************************/
75
76#define QT_MAC_SYSTEMTRAY_USE_GROWL
77
78@class QNSMenu;
79
80#include <private/qt_cocoa_helpers_mac_p.h>
81#include <private/qsystemtrayicon_p.h>
82#include <qtemporaryfile.h>
83#include <qimagewriter.h>
84#include <qapplication.h>
85#include <qdebug.h>
86#include <qstyle.h>
87
88#include <private/qt_mac_p.h>
89#import <AppKit/AppKit.h>
90
91QT_BEGIN_NAMESPACE
92extern bool qt_mac_execute_apple_script(const QString &script, AEDesc *ret); //qapplication_mac.cpp
93extern void qtsystray_sendActivated(QSystemTrayIcon *i, int r); //qsystemtrayicon.cpp
94extern NSString *keySequenceToKeyEqivalent(const QKeySequence &accel); // qmenu_mac.mm
95extern NSUInteger keySequenceModifierMask(const QKeySequence &accel); // qmenu_mac.mm
96QT_END_NAMESPACE
97
98QT_USE_NAMESPACE
99
100@class QNSImageView;
101
102@interface QNSStatusItem : NSObject {
103 NSStatusItem *item;
104 QSystemTrayIcon *icon;
105 QSystemTrayIconPrivate *iconPrivate;
106 QNSImageView *imageCell;
107}
108-(id)initWithIcon:(QSystemTrayIcon*)icon iconPrivate:(QSystemTrayIconPrivate *)iprivate;
109-(void)dealloc;
110-(QSystemTrayIcon*)icon;
111-(NSStatusItem*)item;
112-(QRectF)geometry;
113- (void)triggerSelector:(id)sender;
114- (void)doubleClickSelector:(id)sender;
115@end
116
117@interface QNSImageView : NSImageView {
118 BOOL down;
119 QNSStatusItem *parent;
120}
121-(id)initWithParent:(QNSStatusItem*)myParent;
122-(QSystemTrayIcon*)icon;
123-(void)menuTrackingDone:(NSNotification*)notification;
124-(void)mousePressed:(NSEvent *)mouseEvent;
125@end
126
127
128#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
129
130@protocol NSMenuDelegate <NSObject>
131-(void)menuNeedsUpdate:(NSMenu*)menu;
132@end
133#endif
134
135
136@interface QNSMenu : NSMenu <NSMenuDelegate> {
137 QMenu *qmenu;
138}
139-(QMenu*)menu;
140-(id)initWithQMenu:(QMenu*)qmenu;
141-(void)selectedAction:(id)item;
142@end
143
144QT_BEGIN_NAMESPACE
145class QSystemTrayIconSys
146{
147public:
148 QSystemTrayIconSys(QSystemTrayIcon *icon, QSystemTrayIconPrivate *d) {
149 QMacCocoaAutoReleasePool pool;
150 item = [[QNSStatusItem alloc] initWithIcon:icon iconPrivate:d];
151 }
152 ~QSystemTrayIconSys() {
153 QMacCocoaAutoReleasePool pool;
154 [[[item item] view] setHidden: YES];
155 [item release];
156 }
157 QNSStatusItem *item;
158};
159
160void QSystemTrayIconPrivate::install_sys()
161{
162 Q_Q(QSystemTrayIcon);
163 if (!sys) {
164 sys = new QSystemTrayIconSys(q, this);
165 updateIcon_sys();
166 updateMenu_sys();
167 updateToolTip_sys();
168 }
169}
170
171QRect QSystemTrayIconPrivate::geometry_sys() const
172{
173 if(sys) {
174 const QRectF geom = [sys->item geometry];
175 if(!geom.isNull())
176 return geom.toRect();
177 }
178 return QRect();
179}
180
181void QSystemTrayIconPrivate::remove_sys()
182{
183 delete sys;
184 sys = 0;
185}
186
187void QSystemTrayIconPrivate::updateIcon_sys()
188{
189 if(sys && !icon.isNull()) {
190 QMacCocoaAutoReleasePool pool;
191#ifndef QT_MAC_USE_COCOA
192 const short scale = GetMBarHeight()-4;
193#else
194 CGFloat hgt = [[[NSApplication sharedApplication] mainMenu] menuBarHeight];
195 const short scale = hgt - 4;
196#endif
197 NSImage *nsimage = static_cast<NSImage *>(qt_mac_create_nsimage(icon.pixmap(QSize(scale, scale))));
198 [(NSImageView*)[[sys->item item] view] setImage: nsimage];
199 [nsimage release];
200 }
201}
202
203void QSystemTrayIconPrivate::updateMenu_sys()
204{
205 if(sys) {
206 QMacCocoaAutoReleasePool pool;
207 if(menu && !menu->isEmpty()) {
208 [[sys->item item] setHighlightMode:YES];
209 } else {
210 [[sys->item item] setHighlightMode:NO];
211 }
212 }
213}
214
215void QSystemTrayIconPrivate::updateToolTip_sys()
216{
217 if(sys) {
218 QMacCocoaAutoReleasePool pool;
219 QCFString string(toolTip);
220 [[[sys->item item] view] setToolTip:(NSString*)static_cast<CFStringRef>(string)];
221 }
222}
223
224bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys()
225{
226 return true;
227}
228
229void QSystemTrayIconPrivate::showMessage_sys(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon, int)
230{
231
232 if(sys) {
233#ifdef QT_MAC_SYSTEMTRAY_USE_GROWL
234 // Make sure that we have Growl installed on the machine we are running on.
235 QCFType<CFURLRef> cfurl;
236 OSStatus status = LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
237 CFSTR("growlTicket"), kLSRolesAll, 0, &cfurl);
238 if (status == kLSApplicationNotFoundErr)
239 return;
240 QCFType<CFBundleRef> bundle = CFBundleCreate(0, cfurl);
241
242 if (CFStringCompare(CFBundleGetIdentifier(bundle), CFSTR("com.Growl.GrowlHelperApp"),
243 kCFCompareCaseInsensitive | kCFCompareBackwards) != kCFCompareEqualTo)
244 return;
245 QPixmap notificationIconPixmap;
246 if(icon == QSystemTrayIcon::Information)
247 notificationIconPixmap = QApplication::style()->standardPixmap(QStyle::SP_MessageBoxInformation);
248 else if(icon == QSystemTrayIcon::Warning)
249 notificationIconPixmap = QApplication::style()->standardPixmap(QStyle::SP_MessageBoxWarning);
250 else if(icon == QSystemTrayIcon::Critical)
251 notificationIconPixmap = QApplication::style()->standardPixmap(QStyle::SP_MessageBoxCritical);
252 QTemporaryFile notificationIconFile;
253 QString notificationType(QLatin1String("Notification")), notificationIcon, notificationApp(QApplication::applicationName());
254 if(notificationApp.isEmpty())
255 notificationApp = QLatin1String("Application");
256 if(!notificationIconPixmap.isNull() && notificationIconFile.open()) {
257 QImageWriter writer(&notificationIconFile, "PNG");
258 if(writer.write(notificationIconPixmap.toImage()))
259 notificationIcon = QLatin1String("image from location \"file://") + notificationIconFile.fileName() + QLatin1String("\"");
260 }
261 const QString script(QLatin1String(
262 "tell application \"GrowlHelperApp\"\n"
263 "-- Make a list of all the notification types (all)\n"
264 "set the allNotificationsList to {\"") + notificationType + QLatin1String("\"}\n"
265
266 "-- Make a list of the notifications (enabled)\n"
267 "set the enabledNotificationsList to {\"") + notificationType + QLatin1String("\"}\n"
268
269 "-- Register our script with growl.\n"
270 "register as application \"") + notificationApp + QLatin1String("\" all notifications allNotificationsList default notifications enabledNotificationsList\n"
271
272 "-- Send a Notification...\n") +
273 QLatin1String("notify with name \"") + notificationType +
274 QLatin1String("\" title \"") + title +
275 QLatin1String("\" description \"") + message +
276 QLatin1String("\" application name \"") + notificationApp +
277 QLatin1String("\" ") + notificationIcon +
278 QLatin1String("\nend tell"));
279 qt_mac_execute_apple_script(script, 0);
280#elif 0
281 Q_Q(QSystemTrayIcon);
282 NSView *v = [[sys->item item] view];
283 NSWindow *w = [v window];
284 w = [[sys->item item] window];
285 qDebug() << w << v;
286 QPoint p(qRound([w frame].origin.x), qRound([w frame].origin.y));
287 qDebug() << p;
288 QBalloonTip::showBalloon(icon, message, title, q, QPoint(0, 0), msecs);
289#else
290 Q_UNUSED(icon);
291 Q_UNUSED(title);
292 Q_UNUSED(message);
293#endif
294 }
295}
296QT_END_NAMESPACE
297
298@implementation NSStatusItem (Qt)
299@end
300
301@implementation QNSImageView
302-(id)initWithParent:(QNSStatusItem*)myParent {
303 self = [super init];
304 parent = myParent;
305 down = NO;
306 return self;
307}
308
309-(QSystemTrayIcon*)icon {
310 return [parent icon];
311}
312
313-(void)menuTrackingDone:(NSNotification*)notification
314{
315 Q_UNUSED(notification);
316 down = NO;
317
318 if( ![self icon]->icon().isNull() ) {
319#ifndef QT_MAC_USE_COCOA
320 const short scale = GetMBarHeight()-4;
321#else
322 CGFloat hgt = [[[NSApplication sharedApplication] mainMenu] menuBarHeight];
323 const short scale = hgt - 4;
324#endif
325 NSImage *nsimage = static_cast<NSImage *>(qt_mac_create_nsimage([self icon]->icon().pixmap(QSize(scale, scale))));
326 [self setImage: nsimage];
327 [nsimage release];
328 }
329
330 if([self icon]->contextMenu())
331 [self icon]->contextMenu()->hide();
332
333 [self setNeedsDisplay:YES];
334}
335
336-(void)mousePressed:(NSEvent *)mouseEvent
337{
338 int clickCount = [mouseEvent clickCount];
339 down = !down;
340 if(!down && [self icon]->contextMenu())
341 [self icon]->contextMenu()->hide();
342 [self setNeedsDisplay:YES];
343
344#ifndef QT_MAC_USE_COCOA
345 const short scale = GetMBarHeight()-4;
346#else
347 CGFloat hgt = [[[NSApplication sharedApplication] mainMenu] menuBarHeight];
348 const short scale = hgt - 4;
349#endif
350
351 if( down && ![self icon]->icon().isNull() ) {
352 NSImage *nsaltimage = static_cast<NSImage *>(qt_mac_create_nsimage([self icon]->icon().pixmap(QSize(scale, scale), QIcon::Selected)));
353 [self setImage: nsaltimage];
354 [nsaltimage release];
355 }
356
357
358 if (down)
359 [parent triggerSelector:self];
360 else if ((clickCount%2))
361 [parent doubleClickSelector:self];
362 while (down) {
363 mouseEvent = [[self window] nextEventMatchingMask:NSLeftMouseDownMask | NSLeftMouseUpMask
364 | NSLeftMouseDraggedMask | NSRightMouseDownMask | NSRightMouseUpMask
365 | NSRightMouseDraggedMask];
366 switch ([mouseEvent type]) {
367 case NSRightMouseDown:
368 case NSRightMouseUp:
369 case NSLeftMouseDown:
370 case NSLeftMouseUp:
371 [self menuTrackingDone:nil];
372 break;
373 case NSRightMouseDragged:
374 case NSLeftMouseDragged:
375 default:
376 /* Ignore any other kind of event. */
377 break;
378 }
379 };
380}
381
382-(void)mouseDown:(NSEvent *)mouseEvent
383{
384 [self mousePressed:mouseEvent];
385}
386
387- (void)rightMouseDown:(NSEvent *)mouseEvent
388{
389 [self mousePressed:mouseEvent];
390}
391
392
393-(void)drawRect:(NSRect)rect {
394 [[parent item] drawStatusBarBackgroundInRect:rect withHighlight:down];
395 [super drawRect:rect];
396}
397@end
398
399@implementation QNSStatusItem
400
401-(id)initWithIcon:(QSystemTrayIcon*)i iconPrivate:(QSystemTrayIconPrivate *)iPrivate
402{
403 self = [super init];
404 if(self) {
405 icon = i;
406 iconPrivate = iPrivate;
407 item = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
408 imageCell = [[QNSImageView alloc] initWithParent:self];
409 [item setView: imageCell];
410 }
411 return self;
412}
413-(void)dealloc {
414 [[NSStatusBar systemStatusBar] removeStatusItem:item];
415 [imageCell release];
416 [item release];
417 [super dealloc];
418
419}
420
421-(QSystemTrayIcon*)icon {
422 return icon;
423}
424
425-(NSStatusItem*)item {
426 return item;
427}
428-(QRectF)geometry {
429 if(NSWindow *window = [[item view] window]) {
430 NSRect screenRect = [[window screen] frame];
431 NSRect windowRect = [window frame];
432 return QRectF(windowRect.origin.x, screenRect.size.height-windowRect.origin.y-windowRect.size.height, windowRect.size.width, windowRect.size.height);
433 }
434 return QRectF();
435}
436- (void)triggerSelector:(id)sender {
437 Q_UNUSED(sender);
438 if(!icon)
439 return;
440 qtsystray_sendActivated(icon, QSystemTrayIcon::Trigger);
441 if (icon->contextMenu()) {
442#if 0
443 const QRectF geom = [self geometry];
444 if(!geom.isNull()) {
445 [[NSNotificationCenter defaultCenter] addObserver:imageCell