Files
thehub/hub-qt/macdockiconhandler.mm
T

149 lines
4.3 KiB
Plaintext
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2011-2013 The Bitcoin Core developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "macdockiconhandler.h"
2013-05-31 14:02:24 +02:00
#include <QImageWriter>
#include <QMenu>
#include <QBuffer>
#include <QWidget>
#undef slots
#include <Cocoa/Cocoa.h>
2015-03-12 00:08:22 +01:00
#include <objc/objc.h>
#include <objc/message.h>
2013-11-26 19:10:41 -05:00
#if QT_VERSION < 0x050000
extern void qt_mac_set_dock_menu(QMenu *);
#endif
2015-03-12 00:08:22 +01:00
static MacDockIconHandler *s_instance = NULL;
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;
}
2015-03-12 00:08:22 +01:00
void setupDockClickHandler() {
Class cls = objc_getClass("NSApplication");
id appInst = objc_msgSend((id)cls, sel_registerName("sharedApplication"));
if (appInst != NULL) {
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
}
}
MacDockIconHandler::MacDockIconHandler() : QObject()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2015-03-12 00:08:22 +01:00
setupDockClickHandler();
this->m_dummyWidget = new QWidget();
this->m_dockMenu = new QMenu(this->m_dummyWidget);
this->setMainWindow(NULL);
2013-11-26 19:10:41 -05:00
#if QT_VERSION < 0x050000
qt_mac_set_dock_menu(this->m_dockMenu);
2014-05-29 15:30:46 -04:00
#elif QT_VERSION >= 0x050200
this->m_dockMenu->setAsDockMenu();
2013-11-26 19:10:41 -05:00
#endif
[pool release];
}
2013-04-14 22:11:55 +02:00
void MacDockIconHandler::setMainWindow(QMainWindow *window) {
this->mainWindow = window;
}
MacDockIconHandler::~MacDockIconHandler()
{
delete this->m_dummyWidget;
2013-04-14 22:11:55 +02:00
this->setMainWindow(NULL);
}
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;
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.
QSize size = icon.actualSize(QSize(128, 128));
QPixmap pixmap = icon.pixmap(size);
2013-05-31 14:02:24 +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(&notificationBuffer, "PNG");
2013-05-31 14:02:24 +02:00
if (writer.write(pixmap.toImage())) {
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];
}
}
[NSApp setApplicationIconImage:image];
[image release];
[pool release];
}
MacDockIconHandler *MacDockIconHandler::instance()
{
if (!s_instance)
s_instance = new MacDockIconHandler();
return s_instance;
}
void MacDockIconHandler::cleanup()
{
delete s_instance;
}
void MacDockIconHandler::handleDockIconClickEvent()
{
if (this->mainWindow)
{
this->mainWindow->activateWindow();
this->mainWindow->show();
}
2013-04-14 22:11:55 +02:00
Q_EMIT this->dockIconClicked();
}