Anoncoin  0.9.4
P2P Digital Currency
macdockiconhandler.mm
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin Core developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "macdockiconhandler.h"
7 
8 #include <QImageWriter>
9 #include <QMenu>
10 #include <QTemporaryFile>
11 #include <QWidget>
12 
13 #undef slots
14 #include <Cocoa/Cocoa.h>
15 
16 #if QT_VERSION < 0x050000
17 extern void qt_mac_set_dock_menu(QMenu *);
18 #endif
19 
20 @interface DockIconClickEventHandler : NSObject
21 {
23 }
24 
25 @end
26 
27 @implementation DockIconClickEventHandler
28 
29 - (id)initWithDockIconHandler:(MacDockIconHandler *)aDockIconHandler
30 {
31  self = [super init];
32  if (self) {
33  dockIconHandler = aDockIconHandler;
34 
35  [[NSAppleEventManager sharedAppleEventManager]
36  setEventHandler:self
37  andSelector:@selector(handleDockClickEvent:withReplyEvent:)
38  forEventClass:kCoreEventClass
39  andEventID:kAEReopenApplication];
40  }
41  return self;
42 }
43 
44 - (void)handleDockClickEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
45 {
46  Q_UNUSED(event)
47  Q_UNUSED(replyEvent)
48 
49  if (dockIconHandler) {
50  dockIconHandler->handleDockIconClickEvent();
51  }
52 }
53 
54 @end
55 
57 {
58  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
59 
60  this->m_dockIconClickEventHandler = [[DockIconClickEventHandler alloc] initWithDockIconHandler:this];
61  this->m_dummyWidget = new QWidget();
62  this->m_dockMenu = new QMenu(this->m_dummyWidget);
63  this->setMainWindow(NULL);
64 #if QT_VERSION < 0x050000
66 #elif QT_VERSION >= 0x050200
67  this->m_dockMenu->setAsDockMenu();
68 #endif
69  [pool release];
70 }
71 
72 void MacDockIconHandler::setMainWindow(QMainWindow *window) {
73  this->mainWindow = window;
74 }
75 
77 {
78  [this->m_dockIconClickEventHandler release];
79  delete this->m_dummyWidget;
80  this->setMainWindow(NULL);
81 }
82 
84 {
85  return this->m_dockMenu;
86 }
87 
88 void MacDockIconHandler::setIcon(const QIcon &icon)
89 {
90  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
91  NSImage *image = nil;
92  if (icon.isNull())
93  image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
94  else {
95  // generate NSImage from QIcon and use this as dock icon.
96  QSize size = icon.actualSize(QSize(128, 128));
97  QPixmap pixmap = icon.pixmap(size);
98 
99  // write temp file hack (could also be done through QIODevice [memory])
100  QTemporaryFile notificationIconFile;
101  if (!pixmap.isNull() && notificationIconFile.open()) {
102  QImageWriter writer(&notificationIconFile, "PNG");
103  if (writer.write(pixmap.toImage())) {
104  const char *cString = notificationIconFile.fileName().toUtf8().data();
105  NSString *macString = [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
106  image = [[NSImage alloc] initWithContentsOfFile:macString];
107  }
108  }
109 
110  if(!image) {
111  // if testnet image could not be created, load std. app icon
112  image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
113  }
114  }
115 
116  [NSApp setApplicationIconImage:image];
117  [image release];
118  [pool release];
119 }
120 
122 {
123  static MacDockIconHandler *s_instance = NULL;
124  if (!s_instance)
125  s_instance = new MacDockIconHandler();
126  return s_instance;
127 }
128 
130 {
131  if (this->mainWindow)
132  {
133  this->mainWindow->activateWindow();
134  this->mainWindow->show();
135  }
136 
137  emit this->dockIconClicked();
138 }
QMainWindow * mainWindow
void qt_mac_set_dock_menu(QMenu *)
Macintosh-specific dock icon handler.
void setIcon(const QIcon &icon)
static MacDockIconHandler * instance()
void setMainWindow(QMainWindow *window)
MacDockIconHandler * dockIconHandler
DockIconClickEventHandler * m_dockIconClickEventHandler