From 475cec615cda74aa34d9519239e2f627256ef71d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Sun, 2 Aug 2009 15:15:38 +0200 Subject: Created a generic CocoaAction. diff --git a/Slimber/Cocoa/CocoaAction.h b/Slimber/Cocoa/CocoaAction.h new file mode 100644 index 0000000..13be6b9 --- /dev/null +++ b/Slimber/Cocoa/CocoaAction.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +class CocoaMenulet; + +@interface CocoaAction : NSObject { + boost::function* function; +} + +- (id) initWithFunction: (boost::function*) f; +- (void) doAction: (id) sender; + +@end diff --git a/Slimber/Cocoa/CocoaAction.mm b/Slimber/Cocoa/CocoaAction.mm new file mode 100644 index 0000000..15498a1 --- /dev/null +++ b/Slimber/Cocoa/CocoaAction.mm @@ -0,0 +1,22 @@ +#include "Slimber/Cocoa/CocoaAction.h" + +@implementation CocoaAction + +- (id) initWithFunction: (boost::function*) f { + if ([super init]) { + function = f; + } + return self; +} + +- (void) dealloc { + delete function; + [super dealloc]; +} + +- (void) doAction: (id) sender { + (void) sender; + (*function)(); +} + +@end diff --git a/Slimber/Cocoa/CocoaMenulet.h b/Slimber/Cocoa/CocoaMenulet.h index 73f5c2b..913731f 100644 --- a/Slimber/Cocoa/CocoaMenulet.h +++ b/Slimber/Cocoa/CocoaMenulet.h @@ -3,7 +3,7 @@ #include #include "Slimber/Menulet.h" -#include "Slimber/Cocoa/CocoaMenuletDelegate.h" +#include "Slimber/Cocoa/CocoaAction.h" class CocoaMenulet : public Menulet { public: @@ -22,5 +22,5 @@ class CocoaMenulet : public Menulet { private: NSStatusItem* statusItem; NSMenu* menu; - CocoaMenuletDelegate* delegate; + CocoaAction* restartAction; }; diff --git a/Slimber/Cocoa/CocoaMenulet.mm b/Slimber/Cocoa/CocoaMenulet.mm index f9981e0..72ab000 100644 --- a/Slimber/Cocoa/CocoaMenulet.mm +++ b/Slimber/Cocoa/CocoaMenulet.mm @@ -1,9 +1,12 @@ #include "Slimber/Cocoa/CocoaMenulet.h" +#include + using namespace Swift; CocoaMenulet::CocoaMenulet() { - delegate = [[CocoaMenuletDelegate alloc] initWithMenulet: this]; + restartAction = [[CocoaAction alloc] initWithFunction: + new boost::function(boost::ref(onRestartClicked))]; menu = [[NSMenu alloc] init]; statusItem = [[[NSStatusBar systemStatusBar] @@ -15,10 +18,9 @@ CocoaMenulet::CocoaMenulet() { } CocoaMenulet::~CocoaMenulet() { - [delegate release]; [statusItem release]; [menu release]; - [delegate release]; + [restartAction release]; } void CocoaMenulet::setIcon(const String& icon) { @@ -58,8 +60,9 @@ void CocoaMenulet::addAboutItem() { } void CocoaMenulet::addRestartItem() { - NSMenuItem* item = [[NSMenuItem alloc] initWithTitle: @"Restart" action: @selector(handleRestartClicked:) keyEquivalent: @""]; - [item setTarget: delegate]; + NSMenuItem* item = [[NSMenuItem alloc] initWithTitle: + @"Restart" action: @selector(doAction:) keyEquivalent: @""]; + [item setTarget: restartAction]; [menu addItem: item]; [item release]; } diff --git a/Slimber/Cocoa/CocoaMenuletDelegate.h b/Slimber/Cocoa/CocoaMenuletDelegate.h deleted file mode 100644 index 5c28130..0000000 --- a/Slimber/Cocoa/CocoaMenuletDelegate.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include - -class CocoaMenulet; - -@interface CocoaMenuletDelegate : NSObject { - CocoaMenulet* menulet; -} - -- (id) initWithMenulet: (CocoaMenulet*) m; -- (void) handleRestartClicked: (id) sender; - -@end diff --git a/Slimber/Cocoa/CocoaMenuletDelegate.mm b/Slimber/Cocoa/CocoaMenuletDelegate.mm deleted file mode 100644 index f4a7d7b..0000000 --- a/Slimber/Cocoa/CocoaMenuletDelegate.mm +++ /dev/null @@ -1,17 +0,0 @@ -#include "Slimber/Cocoa/CocoaMenuletDelegate.h" -#include "Slimber/Cocoa/CocoaMenulet.h" - -@implementation CocoaMenuletDelegate - -- (id) initWithMenulet: (CocoaMenulet*) m { - if ([super init]) { - menulet = m; - } - return self; -} - -- (void) handleRestartClicked: (id) sender { - menulet->onRestartClicked(); -} - -@end diff --git a/Slimber/Cocoa/Makefile.inc b/Slimber/Cocoa/Makefile.inc index 5ea1154..2c364b6 100644 --- a/Slimber/Cocoa/Makefile.inc +++ b/Slimber/Cocoa/Makefile.inc @@ -5,7 +5,7 @@ SLIMBER_COCOA_SOURCES = \ Slimber/Cocoa/main.mm \ Slimber/Cocoa/CocoaController.mm \ Slimber/Cocoa/CocoaMenulet.mm \ - Slimber/Cocoa/CocoaMenuletDelegate.mm + Slimber/Cocoa/CocoaAction.mm SLIMBER_COCOA_XIBS = \ Slimber/Cocoa/MainMenu.xib SLIMBER_COCOA_RESOURCES = \ -- cgit v0.10.2-6-g49f6