2013-12-16 22:54:02 +01:00
|
|
|
// Copyright (c) 2011-2013 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-12-16 22:54:02 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2011-10-07 13:21:45 +02:00
|
|
|
#include "macdockiconhandler.h"
|
|
|
|
|
2013-12-16 22:54:02 +01:00
|
|
|
#include <QImageWriter>
|
2012-09-22 11:29:55 +02:00
|
|
|
#include <QMenu>
|
2014-07-30 22:04:40 +02:00
|
|
|
#include <QBuffer>
|
2013-12-16 22:54:02 +01:00
|
|
|
#include <QWidget>
|
2011-10-07 13:21:45 +02:00
|
|
|
|
|
|
|
#undef slots
|
|
|
|
#include <Cocoa/Cocoa.h>
|
2015-03-12 00:08:22 +01:00
|
|
|
#include <objc/objc.h>
|
|
|
|
#include <objc/message.h>
|
2011-10-07 13:21:45 +02:00
|
|
|
|
2013-11-27 01:10:41 +01:00
|
|
|
#if QT_VERSION < 0x050000
|
|
|
|
extern void qt_mac_set_dock_menu(QMenu *);
|
|
|
|
#endif
|
|
|
|
|
2017-08-16 17:26:07 +02:00
|
|
|
static MacDockIconHandler *s_instance = nullptr;
|
2011-10-07 13:21:45 +02:00
|
|
|
|
2015-03-12 00:08:22 +01:00
|
|
|
bool dockClickHandler(id self,SEL _cmd,...) {
|
|
|
|
Q_UNUSED(self)
|
|
|
|
Q_UNUSED(_cmd)
|
|
|
|
|
|
|
|
s_instance->handleDockIconClickEvent();
|
|
|
|
|
|
|
|
// Return NO (false) to suppress the default OS X actions
|
|
|
|
return false;
|
2011-10-07 13:21:45 +02:00
|
|
|
}
|
|
|
|
|
2015-03-12 00:08:22 +01:00
|
|
|
void setupDockClickHandler() {
|
|
|
|
Class cls = objc_getClass("NSApplication");
|
|
|
|
id appInst = objc_msgSend((id)cls, sel_registerName("sharedApplication"));
|
|
|
|
|
2017-08-16 17:26:07 +02:00
|
|
|
if (appInst != nullptr) {
|
2015-03-12 00:08:22 +01:00
|
|
|
id delegate = objc_msgSend(appInst, sel_registerName("delegate"));
|
|
|
|
Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
|
|
|
|
SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
|
|
|
|
if (class_getInstanceMethod(delClass, shouldHandle))
|
|
|
|
class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:");
|
|
|
|
else
|
|
|
|
class_addMethod(delClass, shouldHandle, (IMP)dockClickHandler,"B@:");
|
2013-04-14 22:11:55 +02:00
|
|
|
}
|
2011-10-07 13:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MacDockIconHandler::MacDockIconHandler() : QObject()
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2015-03-12 00:08:22 +01:00
|
|
|
setupDockClickHandler();
|
2011-10-07 13:21:45 +02:00
|
|
|
this->m_dummyWidget = new QWidget();
|
|
|
|
this->m_dockMenu = new QMenu(this->m_dummyWidget);
|
2017-08-16 17:26:07 +02:00
|
|
|
this->setMainWindow(nullptr);
|
2013-11-27 01:10:41 +01:00
|
|
|
#if QT_VERSION < 0x050000
|
|
|
|
qt_mac_set_dock_menu(this->m_dockMenu);
|
2014-05-29 21:30:46 +02:00
|
|
|
#elif QT_VERSION >= 0x050200
|
|
|
|
this->m_dockMenu->setAsDockMenu();
|
2013-11-27 01:10:41 +01:00
|
|
|
#endif
|
2011-10-07 13:21:45 +02:00
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
2013-04-14 22:11:55 +02:00
|
|
|
void MacDockIconHandler::setMainWindow(QMainWindow *window) {
|
|
|
|
this->mainWindow = window;
|
|
|
|
}
|
|
|
|
|
2011-10-07 13:21:45 +02:00
|
|
|
MacDockIconHandler::~MacDockIconHandler()
|
|
|
|
{
|
|
|
|
delete this->m_dummyWidget;
|
2017-08-16 17:26:07 +02:00
|
|
|
this->setMainWindow(nullptr);
|
2011-10-07 13:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QMenu *MacDockIconHandler::dockMenu()
|
|
|
|
{
|
|
|
|
return this->m_dockMenu;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacDockIconHandler::setIcon(const QIcon &icon)
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2013-05-31 14:02:24 +02:00
|
|
|
NSImage *image = nil;
|
2011-10-07 13:21:45 +02:00
|
|
|
if (icon.isNull())
|
|
|
|
image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
|
|
|
|
else {
|
2013-05-31 14:02:24 +02:00
|
|
|
// generate NSImage from QIcon and use this as dock icon.
|
2011-10-07 13:21:45 +02:00
|
|
|
QSize size = icon.actualSize(QSize(128, 128));
|
|
|
|
QPixmap pixmap = icon.pixmap(size);
|
2013-05-31 14:02:24 +02:00
|
|
|
|
2014-07-30 22:04:40 +02:00
|
|
|
// Write image into a R/W buffer from raw pixmap, then save the image.
|
|
|
|
QBuffer notificationBuffer;
|
|
|
|
if (!pixmap.isNull() && notificationBuffer.open(QIODevice::ReadWrite)) {
|
|
|
|
QImageWriter writer(¬ificationBuffer, "PNG");
|
2013-05-31 14:02:24 +02:00
|
|
|
if (writer.write(pixmap.toImage())) {
|
2014-07-30 22:04:40 +02:00
|
|
|
NSData* macImgData = [NSData dataWithBytes:notificationBuffer.buffer().data()
|
|
|
|
length:notificationBuffer.buffer().size()];
|
|
|
|
image = [[NSImage alloc] initWithData:macImgData];
|
2013-05-31 14:02:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!image) {
|
|
|
|
// if testnet image could not be created, load std. app icon
|
|
|
|
image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
|
|
|
|
}
|
2011-10-07 13:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[NSApp setApplicationIconImage:image];
|
|
|
|
[image release];
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
|
|
|
MacDockIconHandler *MacDockIconHandler::instance()
|
|
|
|
{
|
|
|
|
if (!s_instance)
|
|
|
|
s_instance = new MacDockIconHandler();
|
|
|
|
return s_instance;
|
|
|
|
}
|
|
|
|
|
2015-03-13 15:40:53 +01:00
|
|
|
void MacDockIconHandler::cleanup()
|
|
|
|
{
|
|
|
|
delete s_instance;
|
|
|
|
}
|
|
|
|
|
2011-10-07 13:21:45 +02:00
|
|
|
void MacDockIconHandler::handleDockIconClickEvent()
|
|
|
|
{
|
2013-06-05 05:44:53 +02:00
|
|
|
if (this->mainWindow)
|
|
|
|
{
|
|
|
|
this->mainWindow->activateWindow();
|
|
|
|
this->mainWindow->show();
|
|
|
|
}
|
2013-04-14 22:11:55 +02:00
|
|
|
|
2015-07-15 15:09:31 +02:00
|
|
|
Q_EMIT this->dockIconClicked();
|
2011-10-07 13:21:45 +02:00
|
|
|
}
|