| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 144 | QT_BEGIN_NAMESPACE
|
|---|
| 145 | class QSystemTrayIconSys
|
|---|
| 146 | {
|
|---|
| 147 | public:
|
|---|
| 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 |
|
|---|
| 160 | void 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 |
|
|---|
| 171 | QRect 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 |
|
|---|
| 181 | void QSystemTrayIconPrivate::remove_sys()
|
|---|
| 182 | {
|
|---|
| 183 | delete sys;
|
|---|
| 184 | sys = 0;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | void 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 |
|
|---|
| 203 | void 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 |
|
|---|
| 215 | void 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 |
|
|---|
| 224 | bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys()
|
|---|
| 225 | {
|
|---|
| 226 | return true;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | void 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(¬ificationIconFile, "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 | }
|
|---|
| 296 | QT_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
|
|---|
| 446 | selector:@selector(menuTrackingDone:)
|
|---|
| 447 | name:nil
|
|---|
| 448 | object:self];
|
|---|
| 449 | icon->contextMenu()->exec(geom.topLeft().toPoint(), 0);
|
|---|
| 450 | [imageCell menuTrackingDone:nil];
|
|---|
| 451 | } else
|
|---|
| 452 | #endif
|
|---|
| 453 | {
|
|---|
| 454 | #ifndef QT_MAC_USE_COCOA
|
|---|
| 455 | [[[self item] view] removeAllToolTips];
|
|---|
| 456 | iconPrivate->updateToolTip_sys();
|
|---|
| 457 | #endif
|
|---|
| 458 | NSMenu *m = [[QNSMenu alloc] initWithQMenu:icon->contextMenu()];
|
|---|
| 459 | [m setAutoenablesItems: NO];
|
|---|
| 460 | [[NSNotificationCenter defaultCenter] addObserver:imageCell
|
|---|
| 461 | selector:@selector(menuTrackingDone:)
|
|---|
| 462 | name:NSMenuDidEndTrackingNotification
|
|---|
| 463 | object:m];
|
|---|
| 464 | [item popUpStatusItemMenu: m];
|
|---|
| 465 | [m release];
|
|---|
| 466 | }
|
|---|
| 467 | }
|
|---|
| 468 | }
|
|---|
| 469 | - (void)doubleClickSelector:(id)sender {
|
|---|
| 470 | Q_UNUSED(sender);
|
|---|
| 471 | if(!icon)
|
|---|
| 472 | return;
|
|---|
| 473 | qtsystray_sendActivated(icon, QSystemTrayIcon::DoubleClick);
|
|---|
| 474 | }
|
|---|
| 475 | @end
|
|---|
| 476 |
|
|---|
| 477 | class QSystemTrayIconQMenu : public QMenu
|
|---|
| 478 | {
|
|---|
| 479 | public:
|
|---|
| 480 | void doAboutToShow() { emit aboutToShow(); }
|
|---|
| 481 | private:
|
|---|
| 482 | QSystemTrayIconQMenu();
|
|---|
| 483 | };
|
|---|
| 484 |
|
|---|
| 485 | @implementation QNSMenu
|
|---|
| 486 | -(id)initWithQMenu:(QMenu*)qm {
|
|---|
| 487 | self = [super init];
|
|---|
| 488 | if(self) {
|
|---|
| 489 | self->qmenu = qm;
|
|---|
| 490 | [self setDelegate:self];
|
|---|
| 491 | }
|
|---|
| 492 | return self;
|
|---|
| 493 | }
|
|---|
| 494 | -(QMenu*)menu {
|
|---|
| 495 | return qmenu;
|
|---|
| 496 | }
|
|---|
| 497 | -(void)menuNeedsUpdate:(NSMenu*)nsmenu {
|
|---|
| 498 | QNSMenu *menu = static_cast<QNSMenu *>(nsmenu);
|
|---|
| 499 | emit static_cast<QSystemTrayIconQMenu*>(menu->qmenu)->doAboutToShow();
|
|---|
| 500 | for(int i = [menu numberOfItems]-1; i >= 0; --i)
|
|---|
| 501 | [menu removeItemAtIndex:i];
|
|---|
| 502 | QList<QAction*> actions = menu->qmenu->actions();;
|
|---|
| 503 | for(int i = 0; i < actions.size(); ++i) {
|
|---|
| 504 | const QAction *action = actions[i];
|
|---|
| 505 | if(!action->isVisible())
|
|---|
| 506 | continue;
|
|---|
| 507 |
|
|---|
| 508 | NSMenuItem *item = 0;
|
|---|
| 509 | bool needRelease = false;
|
|---|
| 510 | if(action->isSeparator()) {
|
|---|
| 511 | item = [NSMenuItem separatorItem];
|
|---|
| 512 | } else {
|
|---|
| 513 | item = [[NSMenuItem alloc] init];
|
|---|
| 514 | needRelease = true;
|
|---|
| 515 | QString text = action->text();
|
|---|
| 516 | QKeySequence accel = action->shortcut();
|
|---|
| 517 | {
|
|---|
| 518 | int st = text.lastIndexOf(QLatin1Char('\t'));
|
|---|
| 519 | if(st != -1) {
|
|---|
| 520 | accel = QKeySequence(text.right(text.length()-(st+1)));
|
|---|
| 521 | text.remove(st, text.length()-st);
|
|---|
| 522 | }
|
|---|
| 523 | }
|
|---|
| 524 | if(accel.count() > 1)
|
|---|
| 525 | text += QLatin1String(" (****)"); //just to denote a multi stroke shortcut
|
|---|
| 526 |
|
|---|
| 527 | [item setTitle:(NSString*)QCFString::toCFStringRef(qt_mac_removeMnemonics(text))];
|
|---|
| 528 | [item setEnabled:menu->qmenu->isEnabled() && action->isEnabled()];
|
|---|
| 529 | [item setState:action->isChecked() ? NSOnState : NSOffState];
|
|---|
| 530 | [item setToolTip:(NSString*)QCFString::toCFStringRef(action->toolTip())];
|
|---|
| 531 | const QIcon icon = action->icon();
|
|---|
| 532 | if(!icon.isNull()) {
|
|---|
| 533 | const short scale = [[NSApp mainMenu] menuBarHeight];
|
|---|
| 534 | NSImage *nsimage = static_cast<NSImage *>(qt_mac_create_nsimage(icon.pixmap(QSize(scale, scale))));
|
|---|
| 535 | [item setImage: nsimage];
|
|---|
| 536 | [nsimage release];
|
|---|
| 537 | }
|
|---|
| 538 | if(action->menu()) {
|
|---|
| 539 | QNSMenu *sub = [[QNSMenu alloc] initWithQMenu:action->menu()];
|
|---|
| 540 | [item setSubmenu:sub];
|
|---|
| 541 | } else {
|
|---|
| 542 | [item setAction:@selector(selectedAction:)];
|
|---|
| 543 | [item setTarget:self];
|
|---|
| 544 | }
|
|---|
| 545 | if(!accel.isEmpty()) {
|
|---|
| 546 | [item setKeyEquivalent:keySequenceToKeyEqivalent(accel)];
|
|---|
| 547 | [item setKeyEquivalentModifierMask:keySequenceModifierMask(accel)];
|
|---|
| 548 | }
|
|---|
| 549 | }
|
|---|
| 550 | if(item)
|
|---|
| 551 | [menu addItem:item];
|
|---|
| 552 | if (needRelease)
|
|---|
| 553 | [item release];
|
|---|
| 554 | }
|
|---|
| 555 | }
|
|---|
| 556 | -(void)selectedAction:(id)a {
|
|---|
| 557 | const int activated = [self indexOfItem:a];
|
|---|
| 558 | QAction *action = 0;
|
|---|
| 559 | QList<QAction*> actions = qmenu->actions();
|
|---|
| 560 | for(int i = 0, cnt = 0; i < actions.size(); ++i) {
|
|---|
| 561 | if(actions.at(i)->isVisible() && (cnt++) == activated) {
|
|---|
| 562 | action = actions.at(i);
|
|---|
| 563 | break;
|
|---|
| 564 | }
|
|---|
| 565 | }
|
|---|
| 566 | if(action) {
|
|---|
| 567 | action->activate(QAction::Trigger);
|
|---|
| 568 | }
|
|---|
| 569 | }
|
|---|
| 570 | @end
|
|---|
| 571 |
|
|---|
| 572 |
|
|---|
| 573 | /* Done here because this is the only .mm for now! -Sam */
|
|---|
| 574 | QMacCocoaAutoReleasePool::QMacCocoaAutoReleasePool()
|
|---|
| 575 | {
|
|---|
| 576 | NSApplicationLoad();
|
|---|
| 577 | pool = (void*)[[NSAutoreleasePool alloc] init];
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | QMacCocoaAutoReleasePool::~QMacCocoaAutoReleasePool()
|
|---|
| 581 | {
|
|---|
| 582 | [(NSAutoreleasePool*)pool release];
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|