Update CMakeLists.txt - play with openssl
[novacoin.git] / src / qt / macdockiconhandler.mm
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.
4
5 #include "macdockiconhandler.h"
6
7 #include <QImageWriter>
8 #include <QMenu>
9 #include <QBuffer>
10 #include <QWidget>
11
12 extern void qt_mac_set_dock_menu(QMenu*);
13
14 #undef slots
15 #include <Cocoa/Cocoa.h>
16
17 #if QT_VERSION < 0x050000
18 extern void qt_mac_set_dock_menu(QMenu *);
19 #endif
20
21 @interface DockIconClickEventHandler : NSObject
22 {
23     MacDockIconHandler* dockIconHandler;
24 }
25
26 @end
27
28 @implementation DockIconClickEventHandler
29
30 - (id)initWithDockIconHandler:(MacDockIconHandler *)aDockIconHandler
31 {
32     self = [super init];
33     if (self) {
34         dockIconHandler = aDockIconHandler;
35
36         [[NSAppleEventManager sharedAppleEventManager]
37             setEventHandler:self
38                 andSelector:@selector(handleDockClickEvent:withReplyEvent:)
39               forEventClass:kCoreEventClass
40                  andEventID:kAEReopenApplication];
41     }
42     return self;
43 }
44
45 - (void)handleDockClickEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
46 {
47     Q_UNUSED(event)
48     Q_UNUSED(replyEvent)
49
50     if (dockIconHandler) {
51         dockIconHandler->handleDockIconClickEvent();
52     }
53 }
54
55 @end
56
57 MacDockIconHandler::MacDockIconHandler() : QObject()
58 {
59     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
60
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();
69 #endif
70     [pool release];
71 }
72
73 void MacDockIconHandler::setMainWindow(QMainWindow *window) {
74     this->mainWindow = window;
75 }
76
77 MacDockIconHandler::~MacDockIconHandler()
78 {
79     [this->m_dockIconClickEventHandler release];
80     delete this->m_dummyWidget;
81     this->setMainWindow(NULL);
82 }
83
84 QMenu *MacDockIconHandler::dockMenu()
85 {
86     return this->m_dockMenu;
87 }
88
89 void MacDockIconHandler::setIcon(const QIcon &icon)
90 {
91     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
92     NSImage *image = nil;
93     if (icon.isNull())
94         image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
95     else {
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);
99
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(&notificationBuffer, "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];
108             }
109         }
110
111         if(!image) {
112             // if testnet image could not be created, load std. app icon
113             image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
114         }
115     }
116
117     [NSApp setApplicationIconImage:image];
118     [image release];
119     [pool release];
120 }
121
122 MacDockIconHandler *MacDockIconHandler::instance()
123 {
124     static MacDockIconHandler *s_instance = NULL;
125     if (!s_instance)
126         s_instance = new MacDockIconHandler();
127     return s_instance;
128 }
129
130 void MacDockIconHandler::handleDockIconClickEvent()
131 {
132     if (this->mainWindow)
133     {
134         this->mainWindow->activateWindow();
135         this->mainWindow->show();
136     }
137
138     emit this->dockIconClicked();
139 }