Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
macdockiconhandler.mm
Go to the documentation of this file.
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 <QTemporaryFile>
10 #include <QWidget>
11 
12 #undef slots
13 #include <Cocoa/Cocoa.h>
14 
15 #if QT_VERSION < 0x050000
16 extern void qt_mac_set_dock_menu(QMenu *);
17 #endif
18 
20 {
22 }
23 
24 @end
25 
26 @implementation DockIconClickEventHandler
27 
28 - (id)initWithDockIconHandler:(MacDockIconHandler *)aDockIconHandler
29 {
30  self = [super init];
31  if (self) {
32  dockIconHandler = aDockIconHandler;
33 
34  [[NSAppleEventManager sharedAppleEventManager]
35  setEventHandler:self
36  andSelector:@selector(handleDockClickEvent:withReplyEvent:)
37  forEventClass:kCoreEventClass
38  andEventID:kAEReopenApplication];
39  }
40  return self;
41 }
42 
43 - (void)handleDockClickEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
44 {
45  Q_UNUSED(event)
46  Q_UNUSED(replyEvent)
47 
48  if (dockIconHandler) {
50  }
51 }
52 
53 @end
54 
56 {
57  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
58 
59  this->m_dockIconClickEventHandler = [[DockIconClickEventHandler alloc] initWithDockIconHandler:this];
60  this->m_dummyWidget = new QWidget();
61  this->m_dockMenu = new QMenu(this->m_dummyWidget);
62  this->setMainWindow(NULL);
63 #if QT_VERSION < 0x050000
65 #elif QT_VERSION >= 0x050200
66  this->m_dockMenu->setAsDockMenu();
67 #endif
68  [pool release];
69 }
70 
72  this->mainWindow = window;
73 }
74 
76 {
77  [this->m_dockIconClickEventHandler release];
78  delete this->m_dummyWidget;
79  this->setMainWindow(NULL);
80 }
81 
83 {
84  return this->m_dockMenu;
85 }
86 
87 void MacDockIconHandler::setIcon(const QIcon &icon)
88 {
89  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
90  NSImage *image = nil;
91  if (icon.isNull())
92  image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
93  else {
94  // generate NSImage from QIcon and use this as dock icon.
95  QSize size = icon.actualSize(QSize(128, 128));
96  QPixmap pixmap = icon.pixmap(size);
97 
98  // write temp file hack (could also be done through QIODevice [memory])
99  QTemporaryFile notificationIconFile;
100  if (!pixmap.isNull() && notificationIconFile.open()) {
101  QImageWriter writer(&notificationIconFile, "PNG");
102  if (writer.write(pixmap.toImage())) {
103  const char *cString = notificationIconFile.fileName().toUtf8().data();
104  NSString *macString = [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
105  image = [[NSImage alloc] initWithContentsOfFile:macString];
106  }
107  }
108 
109  if(!image) {
110  // if testnet image could not be created, load std. app icon
111  image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
112  }
113  }
114 
115  [NSApp setApplicationIconImage:image];
116  [image release];
117  [pool release];
118 }
119 
121 {
122  static MacDockIconHandler *s_instance = NULL;
123  if (!s_instance)
124  s_instance = new MacDockIconHandler();
125  return s_instance;
126 }
127 
129 {
130  if (this->mainWindow)
131  {
132  this->mainWindow->activateWindow();
133  this->mainWindow->show();
134  }
135 
136  emit this->dockIconClicked();
137 }
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