Improved Mac experience; QDoubleSpinBox for BitcoinAmountField
[novacoin.git] / src / qt / macdockiconhandler.mm
1
2 #include "macdockiconhandler.h"
3
4 #include <QtGui/QMenu>
5 #include <QtGui/QWidget>
6
7 extern void qt_mac_set_dock_menu(QMenu*);
8
9 #undef slots
10 #include <Cocoa/Cocoa.h>
11
12 @interface DockIconClickEventHandler : NSObject
13 {
14     MacDockIconHandler* dockIconHandler;
15 }
16
17 @end
18
19 @implementation DockIconClickEventHandler
20
21 - (id)initWithDockIconHandler:(MacDockIconHandler *)aDockIconHandler
22 {
23     self = [super init];
24     if (self) {
25         dockIconHandler = aDockIconHandler;
26
27         [[NSAppleEventManager sharedAppleEventManager]
28             setEventHandler:self
29                 andSelector:@selector(handleDockClickEvent:withReplyEvent:)
30               forEventClass:kCoreEventClass
31                  andEventID:kAEReopenApplication];
32     }
33     return self;
34 }
35
36 - (void)handleDockClickEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
37 {
38     Q_UNUSED(event)
39     Q_UNUSED(replyEvent)
40
41     if (dockIconHandler)
42         dockIconHandler->handleDockIconClickEvent();
43 }
44
45 @end
46
47 MacDockIconHandler::MacDockIconHandler() : QObject()
48 {
49     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
50     this->m_dockIconClickEventHandler = [[DockIconClickEventHandler alloc] initWithDockIconHandler:this];
51
52     this->m_dummyWidget = new QWidget();
53     this->m_dockMenu = new QMenu(this->m_dummyWidget);
54     qt_mac_set_dock_menu(this->m_dockMenu);
55     [pool release];
56 }
57
58 MacDockIconHandler::~MacDockIconHandler()
59 {
60     [this->m_dockIconClickEventHandler release];
61     delete this->m_dummyWidget;
62 }
63
64 QMenu *MacDockIconHandler::dockMenu()
65 {
66     return this->m_dockMenu;
67 }
68
69 void MacDockIconHandler::setIcon(const QIcon &icon)
70 {
71     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
72     NSImage *image;
73     if (icon.isNull())
74         image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
75     else {
76         QSize size = icon.actualSize(QSize(128, 128));
77         QPixmap pixmap = icon.pixmap(size);
78         CGImageRef cgImage = pixmap.toMacCGImageRef();
79         image = [[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize];
80         CFRelease(cgImage);
81     }
82
83     [NSApp setApplicationIconImage:image];
84     [image release];
85     [pool release];
86 }
87
88 MacDockIconHandler *MacDockIconHandler::instance()
89 {
90     static MacDockIconHandler *s_instance = NULL;
91     if (!s_instance)
92         s_instance = new MacDockIconHandler();
93     return s_instance;
94 }
95
96 void MacDockIconHandler::handleDockIconClickEvent()
97 {
98     emit this->dockIconClicked();
99 }