Removing the use of private CoreGraphics APIs to draw shadows, and replacing with...
[ITKit.git] / ITApplicationController.m
1 #import "ITApplicationController.h"
2 #import "ITCategory-NSApplication.h"
3
4 @implementation ITApplicationController
5
6 - (id)init {
7         if (self = [super init]) {
8                 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ITDebugMode"]) {
9                         SetITDebugMode(YES);
10                 }
11                 
12                 _plugins = nil;
13                 
14                 _dockMenu = [[NSMenu alloc] initWithTitle:[[NSApplication sharedApplication] applicationName]];
15                 
16                 _debugMenu = [[NSMenu alloc] initWithTitle:@"Debug"];
17                 _debugMenuItem = [[NSMenuItem alloc] initWithTitle:@"Debug" action:nil keyEquivalent:@""];
18                 [_debugMenuItem setSubmenu:_debugMenu];
19                 _dockDebugMenuItem = [[NSMenuItem alloc] initWithTitle:@"Debug" action:nil keyEquivalent:@""];
20                 [_dockDebugMenuItem setSubmenu:_debugMenu];
21         }
22         return self;
23 }
24
25 - (void)reloadPlugins {
26         if (_plugins) {
27                 [_plugins release];
28         }
29         
30         _plugins = [[NSMutableArray alloc] init];
31         
32         NSArray *pluginPaths = [NSBundle pathsForResourcesOfType:@"plugin" inDirectory:[[NSBundle mainBundle] builtInPlugInsPath]];
33         NSEnumerator *pluginPathEnumerator = [pluginPaths objectEnumerator];
34         id pluginPath;
35         
36         while (pluginPath = [pluginPathEnumerator nextObject]) {
37                 NSBundle *plugin = [NSBundle bundleWithPath:pluginPath];
38                 if ([plugin load]) {
39                         Class pluginClass = [plugin principalClass];
40                         id pluginInstance;
41                         if ([pluginClass instancesRespondToSelector:@selector(initWithApplicationController:)]) {
42                                 pluginInstance = [(id <ITApplicationControllerGenericPlugin>)[pluginClass alloc] initWithApplicationController:self];
43                         } else {
44                                 pluginInstance = [[pluginClass alloc] init];
45                         }
46                         if (pluginInstance) {
47                                 [_plugins addObject:[pluginInstance autorelease]]; // autoreleasing so that when we reload plugins, and the _plugins array is released, the accompanying previously-loaded plugins die with it.
48                         }
49                 }
50         }
51 }
52
53 - (NSArray *)plugins {
54         return _plugins;
55 }
56
57 - (NSMenu *)dockMenu {
58         return _dockMenu;
59 }
60
61 - (NSMenu *)debugMenu {
62         return _debugMenu;
63 }
64
65 - (void)enableDebugMenu {
66         NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
67         int helpIndex = [mainMenu indexOfItemWithTitle:@"Help"];
68         if (helpIndex != -1) {
69                 [mainMenu insertItem:_debugMenuItem atIndex:helpIndex];
70         } else {
71                 [mainMenu addItem:_debugMenuItem];
72         }
73         
74         [_dockMenu insertItem:_dockDebugMenuItem atIndex:0];
75         if ([_dockMenu numberOfItems] > 1) {
76                 [_dockMenu insertItem:[NSMenuItem separatorItem] atIndex:1];
77         }
78 }
79
80 - (void)disableDebugMenu {
81         [[[NSApplication sharedApplication] mainMenu] removeItem:_debugMenuItem];
82         [_dockMenu removeItem:_dockDebugMenuItem];
83         if ([_dockMenu numberOfItems] > 1) {
84                 NSMenuItem *sep = [_dockMenu itemAtIndex:0];
85                 if ([sep isSeparatorItem]) {
86                         [_dockMenu removeItem:sep];
87                 }
88         }
89 }
90
91 - (void)dealloc {
92         [_dockDebugMenuItem release];
93         [_debugMenuItem release];
94         [_debugMenu release];
95         [_dockMenu release];
96         [super dealloc];
97 }
98
99 - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
100         return _dockMenu;
101 }
102
103 @end