5 // Created by Joseph Spiros on Wed Apr 30 2003.
6 // Copyright (c) 2003 iThink Software. All rights reserved.
9 #import "MenuController.h"
10 #import "NewMainController.h"
11 #import "ITMTRemote.h"
13 @implementation MenuController
17 if ( (self = [super init]) ) {
18 _menuLayout = [[NSMutableArray alloc] initWithCapacity:0];
25 NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
26 NSArray *menuArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"];
27 NSEnumerator *enumerator = [menuArray objectEnumerator];
29 ITMTRemote *currentRemote = [[MainController sharedController] currentRemote];
32 //Get the current playlist, track index, etc.
33 int playlistIndex = [currentRemote currentPlaylistIndex];
34 int trackIndex = [currentRemote currentSongIndex];
36 // dynamically create menu from supplied data and layout information.
37 while ( (nextObject = [enumerator nextObject]) ) {
38 if ([nextObject isEqualToString:@"Play/Pause"]) {
39 tempItem = [menu addItemWithTitle:@"Play"
40 action:@selector(performMainMenuAction:)
42 [tempItem setTag:MTMenuPlayPauseItem];
43 [tempItem setTarget:self];
45 switch ([currentRemote playerPlayingState]) {
46 case ITMTRemotePlayerPlaying:
47 [tempItem setTitle:@"Pause"];
49 case ITMTRemotePlayerRewinding:
50 case ITMTRemotePlayerForwarding:
51 [tempItem setTitle:@"Resume"];
56 } else if ([nextObject isEqualToString:@"Next Track"]) {
57 tempItem = [menu addItemWithTitle:@"Next Track"
58 action:@selector(performMainMenuAction:)
60 [tempItem setTag:MTMenuNextTrackItem];
61 [tempItem setTarget:self];
62 } else if ([nextObject isEqualToString:@"Previous Track"]) {
63 tempItem = [menu addItemWithTitle:@"Previous Track"
64 action:@selector(performMainMenuAction:)
66 [tempItem setTag:MTMenuPreviousTrackItem];
67 [tempItem setTarget:self];
68 } else if ([nextObject isEqualToString:@"Current Track Info"]) {
69 NSString *title = [currentRemote currentSongTitle];
70 [menu addItemWithTitle:@"Now Playing" action:NULL keyEquivalent:@""];
72 if ([title length] > 0) {
73 [menu addItemWithTitle:[NSString stringWithFormat:@" %@", title] action:nil keyEquivalent:@""];
76 //Do cool stuff here woo hehe gack yay! 0_o
79 [_currentMenu release];
84 - (void)performMainMenuAction:(id)sender
86 switch ( [sender tag] )
88 case MTMenuPlayPauseItem:
89 NSLog(@"MenuController: Play/Pause");
90 [[MainController sharedController] playPause];
91 //We're gonna have to change the Play menu item to Pause here too.
93 case MTMenuFastForwardItem:
94 NSLog(@"MenuController: Fast Forward");
95 [[MainController sharedController] fastForward];
96 //make sure play/pause item says sane through this
98 case MTMenuRewindItem:
99 NSLog(@"MenuController: Rewind");
100 [[MainController sharedController] rewind];
101 //make sure play/pause item says sane through this
103 case MTMenuPreviousTrackItem:
104 NSLog(@"MenuController: Previous Track");
105 [[MainController sharedController] prevSong];
107 case MTMenuNextTrackItem:
108 NSLog(@"MenuController: Next Track");
109 [[MainController sharedController] nextSong];
111 case MTMenuPreferencesItem:
112 NSLog(@"MenuController: Preferences...");
113 [[MainController sharedController] showPreferences];
116 NSLog(@"MenuController: Quit");
117 [[MainController sharedController] quitMenuTunes];
120 NSLog(@"MenuController: Unimplemented Menu Item OR Child-bearing Menu Item");
125 - (void)performRatingMenuAction:(id)sender
127 [[MainController sharedController] selectSongRating:[sender tag]];
130 - (void)performPlaylistMenuAction:(id)sender
132 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
135 - (void)performEqualizerMenuAction:(id)sender
137 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
140 - (void)performUpcomingSongsMenuAction:(id)sender
142 [[MainController sharedController] selectSongAtIndex:[sender tag]];
148 [_currentMenu update];
151 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
156 - (NSString *)systemUIColor
158 NSDictionary *tmpDict;
160 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
161 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
162 if ( ([tmpNumber intValue] == 1) ) {
175 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
176 onItem:(NSMenuItem *)item
178 unichar charcode = 'a';
180 long cocoaModifiers = 0;
181 static long carbonToCocoa[6][2] =
183 { cmdKey, NSCommandKeyMask },
184 { optionKey, NSAlternateKeyMask },
185 { controlKey, NSControlKeyMask },
186 { shiftKey, NSShiftKeyMask },
189 for (i = 0; i < 6; i++) {
190 if (modifiers & carbonToCocoa[i][0]) {
191 cocoaModifiers += carbonToCocoa[i][1];
194 [item setKeyEquivalentModifierMask:cocoaModifiers];
196 //Missing key combos for some keys. Must find them later.
210 // Haven't tested this, though it should work.
212 [[NSString stringWithString:@"Space"] getCharacters:&buffer];
214 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
215 NSLog(@"%@", menuRef);
216 SetMenuItemCommandKey(menuRef, 0, NO, 49);
217 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
218 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
225 charcode = NSDeleteFunctionKey;
241 charcode = NSF5FunctionKey;
245 charcode = NSF6FunctionKey;
249 charcode = NSF7FunctionKey;
253 charcode = NSF3FunctionKey;
257 charcode = NSF8FunctionKey;
261 charcode = NSF9FunctionKey;
265 charcode = NSF11FunctionKey;
269 charcode = NSF3FunctionKey;
273 charcode = NSF14FunctionKey;
277 charcode = NSF10FunctionKey;
281 charcode = NSF12FunctionKey;
285 charcode = NSF13FunctionKey;
289 charcode = NSInsertFunctionKey;
293 charcode = NSHomeFunctionKey;
297 charcode = NSPageUpFunctionKey;
301 charcode = NSDeleteFunctionKey;
305 charcode = NSF4FunctionKey;
309 charcode = NSEndFunctionKey;
313 charcode = NSF2FunctionKey;
317 charcode = NSPageDownFunctionKey;
321 charcode = NSF1FunctionKey;
325 charcode = NSLeftArrowFunctionKey;
329 charcode = NSRightArrowFunctionKey;
333 charcode = NSDownArrowFunctionKey;
337 charcode = NSUpArrowFunctionKey;
341 if (charcode == 'a') {
347 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
348 keyTrans = KeyTranslate(kchr, code, &state);
350 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
351 } else if (charcode != 'b') {
352 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];