1 // Copyright (c) 2011-2013 The Bitcoin Core developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "macdockiconhandler.h"
7 #include <QImageWriter>
12 extern void qt_mac_set_dock_menu(QMenu*);
15 #include <Cocoa/Cocoa.h>
17 #if QT_VERSION < 0x050000
18 extern void qt_mac_set_dock_menu(QMenu *);
21 @interface DockIconClickEventHandler : NSObject
23 MacDockIconHandler* dockIconHandler;
28 @implementation DockIconClickEventHandler
30 - (id)initWithDockIconHandler:(MacDockIconHandler *)aDockIconHandler
34 dockIconHandler = aDockIconHandler;
36 [[NSAppleEventManager sharedAppleEventManager]
38 andSelector:@selector(handleDockClickEvent:withReplyEvent:)
39 forEventClass:kCoreEventClass
40 andEventID:kAEReopenApplication];
45 - (void)handleDockClickEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
50 if (dockIconHandler) {
51 dockIconHandler->handleDockIconClickEvent();
57 MacDockIconHandler::MacDockIconHandler() : QObject()
59 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
61 this->m_dockIconClickEventHandler = [[DockIconClickEventHandler alloc] initWithDockIconHandler:this];
62 this->m_dummyWidget = new QWidget();
63 this->m_dockMenu = new QMenu(this->m_dummyWidget);
64 this->setMainWindow(NULL);
65 #if QT_VERSION < 0x050000
66 qt_mac_set_dock_menu(this->m_dockMenu);
67 #elif QT_VERSION >= 0x050200
68 this->m_dockMenu->setAsDockMenu();
73 void MacDockIconHandler::setMainWindow(QMainWindow *window) {
74 this->mainWindow = window;
77 MacDockIconHandler::~MacDockIconHandler()
79 [this->m_dockIconClickEventHandler release];
80 delete this->m_dummyWidget;
81 this->setMainWindow(NULL);
84 QMenu *MacDockIconHandler::dockMenu()
86 return this->m_dockMenu;
89 void MacDockIconHandler::setIcon(const QIcon &icon)
91 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
94 image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
96 // generate NSImage from QIcon and use this as dock icon.
97 QSize size = icon.actualSize(QSize(128, 128));
98 QPixmap pixmap = icon.pixmap(size);
100 // Write image into a R/W buffer from raw pixmap, then save the image.
101 QBuffer notificationBuffer;
102 if (!pixmap.isNull() && notificationBuffer.open(QIODevice::ReadWrite)) {
103 QImageWriter writer(¬ificationBuffer, "PNG");
104 if (writer.write(pixmap.toImage())) {
105 NSData* macImgData = [NSData dataWithBytes:notificationBuffer.buffer().data()
106 length:notificationBuffer.buffer().size()];
107 image = [[NSImage alloc] initWithData:macImgData];
112 // if testnet image could not be created, load std. app icon
113 image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
117 [NSApp setApplicationIconImage:image];
122 MacDockIconHandler *MacDockIconHandler::instance()
124 static MacDockIconHandler *s_instance = NULL;
126 s_instance = new MacDockIconHandler();
130 void MacDockIconHandler::handleDockIconClickEvent()
132 if (this->mainWindow)
134 this->mainWindow->activateWindow();
135 this->mainWindow->show();
138 emit this->dockIconClicked();