Fabcoin Core  0.16.2
P2P Digital Currency
macdockiconhandler.mm
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin Core developers
2 // Distributed under the MIT 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 #undef slots
13 #include <Cocoa/Cocoa.h>
14 #include <objc/objc.h>
15 #include <objc/message.h>
16 
17 #if QT_VERSION < 0x050000
18 extern void qt_mac_set_dock_menu(QMenu *);
19 #endif
20 
21 static MacDockIconHandler *s_instance = NULL;
22 
23 bool dockClickHandler(id self,SEL _cmd,...) {
24  Q_UNUSED(self)
25  Q_UNUSED(_cmd)
26 
27  s_instance->handleDockIconClickEvent();
28 
29  // Return NO (false) to suppress the default OS X actions
30  return false;
31 }
32 
34  Class cls = objc_getClass("NSApplication");
35  id appInst = objc_msgSend((id)cls, sel_registerName("sharedApplication"));
36 
37  if (appInst != NULL) {
38  id delegate = objc_msgSend(appInst, sel_registerName("delegate"));
39  Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
40  SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
41  if (class_getInstanceMethod(delClass, shouldHandle))
42  class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:");
43  else
44  class_addMethod(delClass, shouldHandle, (IMP)dockClickHandler,"B@:");
45  }
46 }
47 
48 
50 {
51  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
52 
54  this->m_dummyWidget = new QWidget();
55  this->m_dockMenu = new QMenu(this->m_dummyWidget);
56  this->setMainWindow(NULL);
57 #if QT_VERSION < 0x050000
59 #elif QT_VERSION >= 0x050200
60  this->m_dockMenu->setAsDockMenu();
61 #endif
62  [pool release];
63 }
64 
65 void MacDockIconHandler::setMainWindow(QMainWindow *window) {
66  this->mainWindow = window;
67 }
68 
70 {
71  delete this->m_dummyWidget;
72  this->setMainWindow(NULL);
73 }
74 
76 {
77  return this->m_dockMenu;
78 }
79 
80 void MacDockIconHandler::setIcon(const QIcon &icon)
81 {
82  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
83  NSImage *image = nil;
84  if (icon.isNull())
85  image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
86  else {
87  // generate NSImage from QIcon and use this as dock icon.
88  QSize size = icon.actualSize(QSize(128, 128));
89  QPixmap pixmap = icon.pixmap(size);
90 
91  // Write image into a R/W buffer from raw pixmap, then save the image.
92  QBuffer notificationBuffer;
93  if (!pixmap.isNull() && notificationBuffer.open(QIODevice::ReadWrite)) {
94  QImageWriter writer(&notificationBuffer, "PNG");
95  if (writer.write(pixmap.toImage())) {
96  NSData* macImgData = [NSData dataWithBytes:notificationBuffer.buffer().data()
97  length:notificationBuffer.buffer().size()];
98  image = [[NSImage alloc] initWithData:macImgData];
99  }
100  }
101 
102  if(!image) {
103  // if testnet image could not be created, load std. app icon
104  image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
105  }
106  }
107 
108  [NSApp setApplicationIconImage:image];
109  [image release];
110  [pool release];
111 }
112 
114 {
115  if (!s_instance)
116  s_instance = new MacDockIconHandler();
117  return s_instance;
118 }
119 
121 {
122  delete s_instance;
123 }
124 
126 {
127  if (this->mainWindow)
128  {
129  this->mainWindow->activateWindow();
130  this->mainWindow->show();
131  }
132 
133  Q_EMIT this->dockIconClicked();
134 }
QMainWindow * mainWindow
void setupDockClickHandler()
void qt_mac_set_dock_menu(QMenu *)
Macintosh-specific dock icon handler.
void setIcon(const QIcon &icon)
static MacDockIconHandler * instance()
void setMainWindow(QMainWindow *window)
bool dockClickHandler(id self, SEL _cmd,...)
uint8_t const size_t const size
Definition: sha3.h:20