1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department 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 |
|
---|
91 | QT_BEGIN_NAMESPACE
|
---|
92 | extern bool qt_mac_execute_apple_script(const QString &script, AEDesc *ret); //qapplication_mac.cpp
|
---|
93 | extern void qtsystray_sendActivated(QSystemTrayIcon *i, int r); //qsystemtrayicon.cpp
|
---|
94 | extern NSString *keySequenceToKeyEqivalent(const QKeySequence &accel); // qmenu_mac.mm
|
---|
95 | extern NSUInteger keySequenceModifierMask(const QKeySequence &accel); // qmenu_mac.mm
|
---|
96 | QT_END_NAMESPACE
|
---|
97 |
|
---|
98 | QT_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 | @interface QNSMenu : NSMenu {
|
---|
128 | QMenu *qmenu;
|
---|
129 | }
|
---|
130 | -(QMenu*)menu;
|
---|
131 | -(id)initWithQMenu:(QMenu*)qmenu;
|
---|
132 | -(void)menuNeedsUpdate:(QNSMenu*)menu;
|
---|
133 | -(void)selectedAction:(id)item;
|
---|
134 | @end
|
---|
135 |
|
---|
136 | QT_BEGIN_NAMESPACE
|
---|
137 | class QSystemTrayIconSys
|
---|
138 | {
|
---|
139 | public:
|
---|
140 | QSystemTrayIconSys(QSystemTrayIcon *icon, QSystemTrayIconPrivate *d) {
|
---|
141 | QMacCocoaAutoReleasePool pool;
|
---|
142 | item = [[QNSStatusItem alloc] initWithIcon:icon iconPrivate:d];
|
---|
143 | }
|
---|
144 | ~QSystemTrayIconSys() {
|
---|
145 | QMacCocoaAutoReleasePool pool;
|
---|
146 | [[[item item] view] setHidden: YES];
|
---|
147 | [item release];
|
---|
148 | }
|
---|
149 | QNSStatusItem *item;
|
---|
150 | };
|
---|
151 |
|
---|
152 | void QSystemTrayIconPrivate::install_sys()
|
---|
153 | {
|
---|
154 | Q_Q(QSystemTrayIcon);
|
---|
155 | if (!sys) {
|
---|
156 | sys = new QSystemTrayIconSys(q, this);
|
---|
157 | updateIcon_sys();
|
---|
158 | updateMenu_sys();
|
---|
159 | updateToolTip_sys();
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | QRect QSystemTrayIconPrivate::geometry_sys() const
|
---|
164 | {
|
---|
165 | if(sys) {
|
---|
166 | const QRectF geom = [sys->item geometry];
|
---|
167 | if(!geom.isNull())
|
---|
168 | return geom.toRect();
|
---|
169 | }
|
---|
170 | return QRect();
|
---|
171 | }
|
---|
172 |
|
---|
173 | void QSystemTrayIconPrivate::remove_sys()
|
---|
174 | {
|
---|
175 | delete sys;
|
---|
176 | sys = 0;
|
---|
177 | }
|
---|
178 |
|
---|
179 | void QSystemTrayIconPrivate::updateIcon_sys()
|
---|
180 | {
|
---|
181 | if(sys && !icon.isNull()) {
|
---|
182 | QMacCocoaAutoReleasePool pool;
|
---|
183 | #ifndef QT_MAC_USE_COCOA
|
---|
184 | const short scale = GetMBarHeight()-4;
|
---|
185 | #else
|
---|
186 | CGFloat hgt = [[[NSApplication sharedApplication] mainMenu] menuBarHeight];
|
---|
187 | const short scale = hgt - 4;
|
---|
188 | #endif
|
---|
189 | NSImage *nsimage = static_cast<NSImage *>(qt_mac_create_nsimage(icon.pixmap(QSize(scale, scale))));
|
---|
190 | [(NSImageView*)[[sys->item item] view] setImage: nsimage];
|
---|
191 | [nsimage release];
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | void QSystemTrayIconPrivate::updateMenu_sys()
|
---|
196 | {
|
---|
197 | if(sys) {
|
---|
198 | QMacCocoaAutoReleasePool pool;
|
---|
199 | if(menu && !menu->isEmpty()) {
|
---|
200 | [[sys->item item] setHighlightMode:YES];
|
---|
201 | } else {
|
---|
202 | [[sys->item item] setHighlightMode:NO];
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | void QSystemTrayIconPrivate::updateToolTip_sys()
|
---|
208 | {
|
---|
209 | if(sys) {
|
---|
210 | QMacCocoaAutoReleasePool pool;
|
---|
211 | QCFString string(toolTip);
|
---|
212 | [[[sys->item item] view] setToolTip:(NSString*)static_cast<CFStringRef>(string)];
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys()
|
---|
217 | {
|
---|
218 | return true;
|
---|
219 | }
|
---|
220 |
|
---|
221 | void QSystemTrayIconPrivate::showMessage_sys(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon, int)
|
---|
222 | {
|
---|
223 |
|
---|
224 | if(sys) {
|
---|
225 | #ifdef QT_MAC_SYSTEMTRAY_USE_GROWL
|
---|
226 | // Make sure that we have Growl installed on the machine we are running on.
|
---|
227 | QCFType<CFURLRef> cfurl;
|
---|
228 | OSStatus status = LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
|
---|
229 | CFSTR("growlTicket"), kLSRolesAll, 0, &cfurl);
|
---|
230 | if (status == kLSApplicationNotFoundErr)
|
---|
231 | return;
|
---|
232 | QCFType<CFBundleRef> bundle = CFBundleCreate(0, cfurl);
|
---|
233 |
|
---|
234 | if (CFStringCompare(CFBundleGetIdentifier(bundle), CFSTR("com.Growl.GrowlHelperApp"),
|
---|
235 | kCFCompareCaseInsensitive | kCFCompareBackwards) != kCFCompareEqualTo)
|
---|
236 | return;
|
---|
237 | QPixmap notificationIconPixmap;
|
---|
238 | if(icon == QSystemTrayIcon::Information)
|
---|
239 | notificationIconPixmap = QApplication::style()->standardPixmap(QStyle::SP_MessageBoxInformation);
|
---|
240 | else if(icon == QSystemTrayIcon::Warning)
|
---|
241 | notificationIconPixmap = QApplication::style()->standardPixmap(QStyle::SP_MessageBoxWarning);
|
---|
242 | else if(icon == QSystemTrayIcon::Critical)
|
---|
243 | notificationIconPixmap = QApplication::style()->standardPixmap(QStyle::SP_MessageBoxCritical);
|
---|
244 | QTemporaryFile notificationIconFile;
|
---|
245 | QString notificationType(QLatin1String("Notification")), notificationIcon, notificationApp(QApplication::applicationName());
|
---|
246 | if(notificationApp.isEmpty())
|
---|
247 | notificationApp = QLatin1String("Application");
|
---|
248 | if(!notificationIconPixmap.isNull() && notificationIconFile.open()) {
|
---|
249 | QImageWriter writer(¬ificationIconFile, "PNG");
|
---|
250 | if(writer.write(notificationIconPixmap.toImage()))
|
---|
251 | notificationIcon = QLatin1String("image from location \"file://") + notificationIconFile.fileName() + QLatin1String("\"");
|
---|
252 | }
|
---|
253 | const QString script(QLatin1String(
|
---|
254 | "tell application \"GrowlHelperApp\"\n"
|
---|
255 | "-- Make a list of all the notification types (all)\n"
|
---|
256 | "set the allNotificationsList to {\"") + notificationType + QLatin1String("\"}\n"
|
---|
257 |
|
---|
258 | "-- Make a list of the notifications (enabled)\n"
|
---|
259 | "set the enabledNotificationsList to {\"") + notificationType + QLatin1String("\"}\n"
|
---|
260 |
|
---|
261 | "-- Register our script with growl.\n"
|
---|
262 | "register as application \"") + notificationApp + QLatin1String("\" all notifications allNotificationsList default notifications enabledNotificationsList\n"
|
---|
263 |
|
---|
264 | "-- Send a Notification...\n") +
|
---|
265 | QLatin1String("notify with name \"") + notificationType +
|
---|
266 | QLatin1String("\" title \"") + title +
|
---|
267 | QLatin1String("\" description \"") + message +
|
---|
268 | QLatin1String("\" application name \"") + notificationApp +
|
---|
269 | QLatin1String("\" ") + notificationIcon +
|
---|
270 | QLatin1String("\nend tell"));
|
---|
271 | qt_mac_execute_apple_script(script, 0);
|
---|
272 | #elif 0
|
---|
273 | Q_Q(QSystemTrayIcon);
|
---|
274 | NSView *v = [[sys->item item] view];
|
---|
275 | NSWindow *w = [v window];
|
---|
276 | w = [[sys->item item] window];
|
---|
277 | qDebug() << w << v;
|
---|
278 | QPoint p(qRound([w frame].origin.x), qRound([w frame].origin.y));
|
---|
279 | qDebug() << p;
|
---|
280 | QBalloonTip::showBalloon(icon, message, title, q, QPoint(0, 0), msecs);
|
---|
281 | #else
|
---|
282 | Q_UNUSED(icon);
|
---|
283 | Q_UNUSED(title);
|
---|
284 | Q_UNUSED(message);
|
---|
285 | #endif
|
---|
286 | }
|
---|
287 | }
|
---|
288 | QT_END_NAMESPACE
|
---|
289 |
|
---|
290 | @implementation NSStatusItem (Qt)
|
---|
291 | @end
|
---|
292 |
|
---|
293 | @implementation QNSImageView
|
---|
294 | -(id)initWithParent:(QNSStatusItem*)myParent {
|
---|
295 | self = [super init];
|
---|
296 | parent = myParent;
|
---|
297 | down = NO;
|
---|
298 | return self;
|
---|
299 | }
|
---|
300 |
|
---|
301 | -(QSystemTrayIcon*)icon {
|
---|
302 | return [parent icon];
|
---|
303 | }
|
---|
304 |
|
---|
305 | -(void)menuTrackingDone:(NSNotification*)notification
|
---|
306 | {
|
---|
307 | Q_UNUSED(notification);
|
---|
308 | down = NO;
|
---|
309 | if([self icon]->contextMenu())
|
---|
310 | [self icon]->contextMenu()->hide();
|
---|
311 | [self setNeedsDisplay:YES];
|
---|
312 | }
|
---|
313 |
|
---|
314 | -(void)mousePressed:(NSEvent *)mouseEvent
|
---|
315 | {
|
---|
316 | int clickCount = [mouseEvent clickCount];
|
---|
317 | down = !down;
|
---|
318 | if(!down && [self icon]->contextMenu())
|
---|
319 | [self icon]->contextMenu()->hide();
|
---|
320 | [self setNeedsDisplay:YES];
|
---|
321 |
|
---|
322 | if (down)
|
---|
323 | [parent triggerSelector:self];
|
---|
324 | else if ((clickCount%2))
|
---|
325 | [parent doubleClickSelector:self];
|
---|
326 | while (down) {
|
---|
327 | mouseEvent = [[self window] nextEventMatchingMask:NSLeftMouseDownMask | NSLeftMouseUpMask
|
---|
328 | | NSLeftMouseDraggedMask | NSRightMouseDownMask | NSRightMouseUpMask
|
---|
329 | | NSRightMouseDraggedMask];
|
---|
330 | switch ([mouseEvent type]) {
|
---|
331 | case NSRightMouseDown:
|
---|
332 | case NSRightMouseUp:
|
---|
333 | case NSLeftMouseDown:
|
---|
334 | case NSLeftMouseUp:
|
---|
335 | [self menuTrackingDone:nil];
|
---|
336 | break;
|
---|
337 | case NSRightMouseDragged:
|
---|
338 | case NSLeftMouseDragged:
|
---|
339 | default:
|
---|
340 | /* Ignore any other kind of event. */
|
---|
341 | break;
|
---|
342 | }
|
---|
343 | };
|
---|
344 | }
|
---|
345 |
|
---|
346 | -(void)mouseDown:(NSEvent *)mouseEvent
|
---|
347 | {
|
---|
348 | [self mousePressed:mouseEvent];
|
---|
349 | }
|
---|
350 |
|
---|
351 | - (void)rightMouseDown:(NSEvent *)mouseEvent
|
---|
352 | {
|
---|
353 | [self mousePressed:mouseEvent];
|
---|
354 | }
|
---|
355 |
|
---|
356 |
|
---|
357 | -(void)drawRect:(NSRect)rect {
|
---|
358 | [[parent item] drawStatusBarBackgroundInRect:rect withHighlight:down];
|
---|
359 | [super drawRect:rect];
|
---|
360 | }
|
---|
361 | @end
|
---|
362 |
|
---|
363 | @implementation QNSStatusItem
|
---|
364 |
|
---|
365 | -(id)initWithIcon:(QSystemTrayIcon*)i iconPrivate:(QSystemTrayIconPrivate *)iPrivate
|
---|
366 | {
|
---|
367 | self = [super init];
|
---|
368 | if(self) {
|
---|
369 | icon = i;
|
---|
370 | iconPrivate = iPrivate;
|
---|
371 | item = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
|
---|
372 | imageCell = [[QNSImageView alloc] initWithParent:self];
|
---|
373 | [item setView: imageCell];
|
---|
374 | }
|
---|
375 | return self;
|
---|
376 | }
|
---|
377 | -(void)dealloc {
|
---|
378 | [[NSStatusBar systemStatusBar] removeStatusItem:item];
|
---|
379 | [imageCell release];
|
---|
380 | [item release];
|
---|
381 | [super dealloc];
|
---|
382 |
|
---|
383 | }
|
---|
384 |
|
---|
385 | -(QSystemTrayIcon*)icon {
|
---|
386 | return icon;
|
---|
387 | }
|
---|
388 |
|
---|
389 | -(NSStatusItem*)item {
|
---|
390 | return item;
|
---|
391 | }
|
---|
392 | -(QRectF)geometry {
|
---|
393 | if(NSWindow *window = [[item view] window]) {
|
---|
394 | NSRect screenRect = [[window screen] frame];
|
---|
395 | NSRect windowRect = [window frame];
|
---|
396 | return QRectF(windowRect.origin.x, screenRect.size.height-windowRect.origin.y-windowRect.size.height, windowRect.size.width, windowRect.size.height);
|
---|
397 | }
|
---|
398 | return QRectF();
|
---|
399 | }
|
---|
400 | - (void)triggerSelector:(id)sender {
|
---|
401 | Q_UNUSED(sender);
|
---|
402 | if(!icon)
|
---|
403 | return;
|
---|
404 | qtsystray_sendActivated(icon, QSystemTrayIcon::Trigger);
|
---|
405 | if (icon->contextMenu()) {
|
---|
406 | #if 0
|
---|
407 | const QRectF geom = [self geometry];
|
---|
408 | if(!geom.isNull()) {
|
---|
409 | [[NSNotificationCenter defaultCenter] addObserver:imageCell
|
---|
410 | selector:@selector(menuTrackingDone:)
|
---|
411 | name:nil
|
---|
412 | object:self];
|
---|
413 | icon->contextMenu()->exec(geom.topLeft().toPoint(), 0);
|
---|
414 | [imageCell menuTrackingDone:nil];
|
---|
415 | } else
|
---|
416 | #endif
|
---|
417 | {
|
---|
418 | #ifndef QT_MAC_USE_COCOA
|
---|
419 | [[[self item] view] removeAllToolTips];
|
---|
420 | iconPrivate->updateToolTip_sys();
|
---|
421 | #endif
|
---|
422 | NSMenu *m = [[QNSMenu alloc] initWithQMenu:icon->contextMenu()];
|
---|
423 | [m setAutoenablesItems: NO];
|
---|
424 | [[NSNotificationCenter defaultCenter] addObserver:imageCell
|
---|
425 | selector:@selector(menuTrackingDone:)
|
---|
426 | name:NSMenuDidEndTrackingNotification
|
---|
427 | object:m];
|
---|
428 | [item popUpStatusItemMenu: m];
|
---|
429 | [m release];
|
---|
430 | }
|
---|
431 | }
|
---|
432 | }
|
---|
433 | - (void)doubleClickSelector:(id)sender {
|
---|
434 | Q_UNUSED(sender);
|
---|
435 | if(!icon)
|
---|
436 | return;
|
---|
437 | qtsystray_sendActivated(icon, QSystemTrayIcon::DoubleClick);
|
---|
438 | }
|
---|
439 | @end
|
---|
440 |
|
---|
441 | class QSystemTrayIconQMenu : public QMenu
|
---|
442 | {
|
---|
443 | public:
|
---|
444 | void doAboutToShow() { emit aboutToShow(); }
|
---|
445 | private:
|
---|
446 | QSystemTrayIconQMenu();
|
---|
447 | };
|
---|
448 |
|
---|
449 | @implementation QNSMenu
|
---|
450 | -(id)initWithQMenu:(QMenu*)qm {
|
---|
451 | self = [super init];
|
---|
452 | if(self) {
|
---|
453 | self->qmenu = qm;
|
---|
454 | [self setDelegate:self];
|
---|
455 | }
|
---|
456 | return self;
|
---|
457 | }
|
---|
458 | -(QMenu*)menu {
|
---|
459 | return qmenu;
|
---|
460 | }
|
---|
461 | -(void)menuNeedsUpdate:(QNSMenu*)menu {
|
---|
462 | emit static_cast<QSystemTrayIconQMenu*>(menu->qmenu)->doAboutToShow();
|
---|
463 | for(int i = [menu numberOfItems]-1; i >= 0; --i)
|
---|
464 | [menu removeItemAtIndex:i];
|
---|
465 | QList<QAction*> actions = menu->qmenu->actions();;
|
---|
466 | for(int i = 0; i < actions.size(); ++i) {
|
---|
467 | const QAction *action = actions[i];
|
---|
468 | if(!action->isVisible())
|
---|
469 | continue;
|
---|
470 |
|
---|
471 | NSMenuItem *item = 0;
|
---|
472 | bool needRelease = false;
|
---|
473 | if(action->isSeparator()) {
|
---|
474 | item = [NSMenuItem separatorItem];
|
---|
475 | } else {
|
---|
476 | item = [[NSMenuItem alloc] init];
|
---|
477 | needRelease = true;
|
---|
478 | QString text = action->text();
|
---|
479 | QKeySequence accel = action->shortcut();
|
---|
480 | {
|
---|
481 | int st = text.lastIndexOf(QLatin1Char('\t'));
|
---|
482 | if(st != -1) {
|
---|
483 | accel = QKeySequence(text.right(text.length()-(st+1)));
|
---|
484 | text.remove(st, text.length()-st);
|
---|
485 | }
|
---|
486 | }
|
---|
487 | if(accel.count() > 1)
|
---|
488 | text += QLatin1String(" (****)"); //just to denote a multi stroke shortcut
|
---|
489 |
|
---|
490 | [item setTitle:(NSString*)QCFString::toCFStringRef(qt_mac_removeMnemonics(text))];
|
---|
491 | [item setEnabled:menu->qmenu->isEnabled() && action->isEnabled()];
|
---|
492 | [item setState:action->isChecked() ? NSOnState : NSOffState];
|
---|
493 | [item setToolTip:(NSString*)QCFString::toCFStringRef(action->toolTip())];
|
---|
494 | const QIcon icon = action->icon();
|
---|
495 | if(!icon.isNull()) {
|
---|
496 | const short scale = [[NSApp mainMenu] menuBarHeight];
|
---|
497 | NSImage *nsimage = static_cast<NSImage *>(qt_mac_create_nsimage(icon.pixmap(QSize(scale, scale))));
|
---|
498 | [item setImage: nsimage];
|
---|
499 | [nsimage release];
|
---|
500 | }
|
---|
501 | if(action->menu()) {
|
---|
502 | QNSMenu *sub = [[QNSMenu alloc] initWithQMenu:action->menu()];
|
---|
503 | [item setSubmenu:sub];
|
---|
504 | } else {
|
---|
505 | [item setAction:@selector(selectedAction:)];
|
---|
506 | [item setTarget:self];
|
---|
507 | }
|
---|
508 | if(!accel.isEmpty()) {
|
---|
509 | [item setKeyEquivalent:keySequenceToKeyEqivalent(accel)];
|
---|
510 | [item setKeyEquivalentModifierMask:keySequenceModifierMask(accel)];
|
---|
511 | }
|
---|
512 | }
|
---|
513 | if(item)
|
---|
514 | [menu addItem:item];
|
---|
515 | if (needRelease)
|
---|
516 | [item release];
|
---|
517 | }
|
---|
518 | }
|
---|
519 | -(void)selectedAction:(id)a {
|
---|
520 | const int activated = [self indexOfItem:a];
|
---|
521 | QAction *action = 0;
|
---|
522 | QList<QAction*> actions = qmenu->actions();
|
---|
523 | for(int i = 0, cnt = 0; i < actions.size(); ++i) {
|
---|
524 | if(actions.at(i)->isVisible() && (cnt++) == activated) {
|
---|
525 | action = actions.at(i);
|
---|
526 | break;
|
---|
527 | }
|
---|
528 | }
|
---|
529 | if(action) {
|
---|
530 | action->activate(QAction::Trigger);
|
---|
531 | }
|
---|
532 | }
|
---|
533 | @end
|
---|
534 |
|
---|
535 |
|
---|
536 | /* Done here because this is the only .mm for now! -Sam */
|
---|
537 | QMacCocoaAutoReleasePool::QMacCocoaAutoReleasePool()
|
---|
538 | {
|
---|
539 | NSApplicationLoad();
|
---|
540 | pool = (void*)[[NSAutoreleasePool alloc] init];
|
---|
541 | }
|
---|
542 |
|
---|
543 | QMacCocoaAutoReleasePool::~QMacCocoaAutoReleasePool()
|
---|
544 | {
|
---|
545 | [(NSAutoreleasePool*)pool release];
|
---|
546 | }
|
---|
547 |
|
---|