2 // ApplicationController.m
5 // Created by Sam McCall on 4/05/09.
7 #include <Carbon/Carbon.h>
9 @implementation ApplicationController
11 @synthesize jsController, targetController, configsController;
15 pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData);
17 -(void) applicationDidFinishLaunching: (NSNotification*) notification {
20 [targetController setEnabled: false];
22 [configsController load];
24 et.eventClass = kEventClassApplication;
25 et.eventKind = kEventAppFrontSwitched;
26 EventHandlerUPP handler = NewEventHandlerUPP(appSwitch);
27 InstallApplicationEventHandler(handler, 1, &et, self, NULL);
30 -(void) applicationWillTerminate: (NSNotification *)aNotification {
31 [configsController save];
34 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
35 hasVisibleWindows:(BOOL)flag
37 [mainWindow makeKeyAndOrderFront:self];
41 pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData) {
42 ApplicationController* self = (ApplicationController*)userData;
43 NSDictionary* currentApp = [[NSWorkspace sharedWorkspace] activeApplication];
44 ProcessSerialNumber psn;
45 psn.lowLongOfPSN = [[currentApp objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
46 psn.highLongOfPSN = [[currentApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
47 [self->configsController applicationSwitchedTo: [currentApp objectForKey:@"NSApplicationName"] withPsn: psn];
55 -(void) setActive: (BOOL) newActive {
56 [activeButton setLabel: (newActive ? @"Stop" : @"Start")];
57 [activeButton setImage: [NSImage imageNamed: (newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate" )]];
58 [activeMenuItem setState: (newActive ? 1 : 0)];
62 -(IBAction) toggleActivity: (id)sender {
63 [self setActive: ![self active]];
66 -(void) configsChanged {
67 while([dockMenuBase numberOfItems] > 2)
68 [dockMenuBase removeItemAtIndex: ([dockMenuBase numberOfItems] - 1)];
70 for(Config* config in [configsController configs]) {
71 [dockMenuBase addItemWithTitle:[config name] action:@selector(chooseConfig:) keyEquivalent:@""];
75 -(void) configChanged {
76 Config* current = [configsController currentConfig];
77 NSArray* configs = [configsController configs];
78 for(int i=0; i<[configs count]; i++)
79 [[dockMenuBase itemAtIndex: (2+i)] setState: (([configs objectAtIndex:i] == current) ? YES : NO)];
82 -(void) chooseConfig: (id) sender {
83 [configsController activateConfig: [[configsController configs] objectAtIndex: ([dockMenuBase indexOfItem: sender]-2)] forApplication: NULL];