2013-12-16 22:54:02 +01:00
|
|
|
// Copyright (c) 2011-2013 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// 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>
|
2013-05-31 14:02:24 +02:00
|
|
|
#include <QTemporaryFile>
|
2013-12-16 22:54:02 +01:00
|
|
|
#include <QWidget>
|
2011-10-07 13:21:45 +02:00
|
|
|
|
|
|
|
#undef slots
|
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
|
2013-11-27 01:10:41 +01:00
|
|
|
#if QT_VERSION < 0x050000
|
|
|
|
extern void qt_mac_set_dock_menu(QMenu *);
|
|
|
|
#endif
|
|
|
|
|
2011-10-07 13:21:45 +02:00
|
|
|
@interface DockIconClickEventHandler : NSObject
|
|
|
|
{
|
|
|
|
MacDockIconHandler* dockIconHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DockIconClickEventHandler
|
|
|
|
|
|
|
|
- (id)initWithDockIconHandler:(MacDockIconHandler *)aDockIconHandler
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
dockIconHandler = aDockIconHandler;
|
|
|
|
|
|
|
|
[[NSAppleEventManager sharedAppleEventManager]
|
|
|
|
setEventHandler:self
|
|
|
|
andSelector:@selector(handleDockClickEvent:withReplyEvent:)
|
|
|
|
forEventClass:kCoreEventClass
|
|
|
|
andEventID:kAEReopenApplication];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleDockClickEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
|
|
|
Q_UNUSED(replyEvent)
|
|
|
|
|
2013-04-14 22:11:55 +02:00
|
|
|
if (dockIconHandler) {
|
2011-10-07 13:21:45 +02:00
|
|
|
dockIconHandler->handleDockIconClickEvent();
|
2013-04-14 22:11:55 +02:00
|
|
|
}
|
2011-10-07 13:21:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
MacDockIconHandler::MacDockIconHandler() : QObject()
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2013-05-31 14:02:24 +02:00
|
|
|
this->m_dockIconClickEventHandler = [[DockIconClickEventHandler alloc] initWithDockIconHandler:this];
|
2011-10-07 13:21:45 +02:00
|
|
|
this->m_dummyWidget = new QWidget();
|
|
|
|
this->m_dockMenu = new QMenu(this->m_dummyWidget);
|
2013-06-05 05:44:53 +02:00
|
|
|
this->setMainWindow(NULL);
|
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()
|
|
|
|
{
|
|
|
|
[this->m_dockIconClickEventHandler release];
|
|
|
|
delete this->m_dummyWidget;
|
2013-04-14 22:11:55 +02:00
|
|
|
this->setMainWindow(NULL);
|
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
|
|
|
|
|
|
|
// write temp file hack (could also be done through QIODevice [memory])
|
|
|
|
QTemporaryFile notificationIconFile;
|
|
|
|
if (!pixmap.isNull() && notificationIconFile.open()) {
|
|
|
|
QImageWriter writer(¬ificationIconFile, "PNG");
|
|
|
|
if (writer.write(pixmap.toImage())) {
|
|
|
|
const char *cString = notificationIconFile.fileName().toUtf8().data();
|
|
|
|
NSString *macString = [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
|
|
|
|
image = [[NSImage alloc] initWithContentsOfFile:macString];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
static MacDockIconHandler *s_instance = NULL;
|
|
|
|
if (!s_instance)
|
|
|
|
s_instance = new MacDockIconHandler();
|
|
|
|
return s_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-10-07 13:21:45 +02:00
|
|
|
emit this->dockIconClicked();
|
|
|
|
}
|