BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
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 static MacDockIconHandler *s_instance = nullptr;
18 
19 bool dockClickHandler(id self,SEL _cmd,...) {
20  Q_UNUSED(self)
21  Q_UNUSED(_cmd)
22 
23  s_instance->handleDockIconClickEvent();
24 
25  // Return NO (false) to suppress the default OS X actions
26  return false;
27 }
28 
30  Class cls = objc_getClass("NSApplication");
31  id appInst = objc_msgSend((id)cls, sel_registerName("sharedApplication"));
32 
33  if (appInst != nullptr) {
34  id delegate = objc_msgSend(appInst, sel_registerName("delegate"));
35  Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
36  SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
37  if (class_getInstanceMethod(delClass, shouldHandle))
38  class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:");
39  else
40  class_addMethod(delClass, shouldHandle, (IMP)dockClickHandler,"B@:");
41  }
42 }
43 
44 
46 {
47  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
48 
50  this->m_dummyWidget = new QWidget();
51  this->m_dockMenu = new QMenu(this->m_dummyWidget);
52  this->setMainWindow(nullptr);
53 #if QT_VERSION >= 0x050200
54  this->m_dockMenu->setAsDockMenu();
55 #endif
56  [pool release];
57 }
58 
59 void MacDockIconHandler::setMainWindow(QMainWindow *window) {
60  this->mainWindow = window;
61 }
62 
64 {
65  delete this->m_dummyWidget;
66  this->setMainWindow(nullptr);
67 }
68 
70 {
71  return this->m_dockMenu;
72 }
73 
74 void MacDockIconHandler::setIcon(const QIcon &icon)
75 {
76  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
77  NSImage *image = nil;
78  if (icon.isNull())
79  image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
80  else {
81  // generate NSImage from QIcon and use this as dock icon.
82  QSize size = icon.actualSize(QSize(128, 128));
83  QPixmap pixmap = icon.pixmap(size);
84 
85  // Write image into a R/W buffer from raw pixmap, then save the image.
86  QBuffer notificationBuffer;
87  if (!pixmap.isNull() && notificationBuffer.open(QIODevice::ReadWrite)) {
88  QImageWriter writer(&notificationBuffer, "PNG");
89  if (writer.write(pixmap.toImage())) {
90  NSData* macImgData = [NSData dataWithBytes:notificationBuffer.buffer().data()
91  length:notificationBuffer.buffer().size()];
92  image = [[NSImage alloc] initWithData:macImgData];
93  }
94  }
95 
96  if(!image) {
97  // if testnet image could not be created, load std. app icon
98  image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
99  }
100  }
101 
102  [NSApp setApplicationIconImage:image];
103  [image release];
104  [pool release];
105 }
106 
108 {
109  if (!s_instance)
110  s_instance = new MacDockIconHandler();
111  return s_instance;
112 }
113 
115 {
116  delete s_instance;
117 }
118 
120 {
121  if (this->mainWindow)
122  {
123  this->mainWindow->activateWindow();
124  this->mainWindow->show();
125  }
126 
127  Q_EMIT this->dockIconClicked();
128 }
QMainWindow * mainWindow
void setupDockClickHandler()
Macintosh-specific dock icon handler.
void setIcon(const QIcon &icon)
static MacDockIconHandler * instance()
void setMainWindow(QMainWindow *window)
bool dockClickHandler(id self, SEL _cmd,...)