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 if ([currentRemote playerPlayingState] == ITMTRemotePlayerPlaying) {
40 tempItem = [menu addItemWithTitle:@"Pause"
41 action:@selector(performMainMenuAction:)
43 [tempItem setTag:MTMenuPlayPauseItem];
44 [tempItem setTarget:self];
46 tempItem = [menu addItemWithTitle:@"Play"
47 action:@selector(performMainMenuAction:)
49 [tempItem setTag:MTMenuPlayPauseItem];
50 [tempItem setTarget:self];
54 if ([nextObject isEqualToString:@"Current Track Info"]) {
55 NSString *title = [currentRemote currentSongTitle];
56 [menu addItemWithTitle:@"Now Playing" action:NULL keyEquivalent:@""];
58 if ([title length] > 0) {
59 [menu addItemWithTitle:[NSString stringWithFormat:@" %@", title] action:nil keyEquivalent:@""];
62 //Do cool stuff here woo hehe gack yay! 0_o
65 [_currentMenu release];
70 - (void)performMainMenuAction:(id)sender
72 switch ( [sender tag] )
74 case MTMenuPlayPauseItem:
75 NSLog(@"MenuController: Play/Pause");
76 [[MainController sharedController] playPause];
77 //We're gonna have to change the Play menu item to Pause here too.
79 case MTMenuFastForwardItem:
80 NSLog(@"MenuController: Fast Forward");
81 [[MainController sharedController] fastForward];
82 //make sure play/pause item says sane through this
84 case MTMenuRewindItem:
85 NSLog(@"MenuController: Rewind");
86 [[MainController sharedController] rewind];
87 //make sure play/pause item says sane through this
89 case MTMenuPreviousTrackItem:
90 NSLog(@"MenuController: Previous Track");
91 [[MainController sharedController] prevSong];
93 case MTMenuNextTrackItem:
94 NSLog(@"MenuController: Next Track");
95 [[MainController sharedController] nextSong];
97 case MTMenuPreferencesItem:
98 NSLog(@"MenuController: Preferences...");
99 [[MainController sharedController] showPreferences];
102 NSLog(@"MenuController: Quit");
103 [[MainController sharedController] quitMenuTunes];
106 NSLog(@"MenuController: Unimplemented Menu Item OR Child-bearing Menu Item");
111 - (void)performRatingMenuAction:(id)sender
113 [[MainController sharedController] selectSongRating:[sender tag]];
116 - (void)performPlaylistMenuAction:(id)sender
118 [[MainController sharedController] selectPlaylistAtIndex:[sender tag]];
121 - (void)performEqualizerMenuAction:(id)sender
123 [[MainController sharedController] selectEQPresetAtIndex:[sender tag]];
126 - (void)performUpcomingSongsMenuAction:(id)sender
128 [[MainController sharedController] selectSongAtIndex:[sender tag]];
134 [_currentMenu update];
137 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
142 - (NSString *)systemUIColor
144 NSDictionary *tmpDict;
146 if ( (tmpDict = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/.GlobalPreferences.plist" stringByExpandingTildeInPath]]) ) {
147 if ( (tmpNumber = [tmpDict objectForKey:@"AppleAquaColorVariant"]) ) {
148 if ( ([tmpNumber intValue] == 1) ) {
161 - (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers
162 onItem:(NSMenuItem *)item
164 unichar charcode = 'a';
166 long cocoaModifiers = 0;
167 static long carbonToCocoa[6][2] =
169 { cmdKey, NSCommandKeyMask },
170 { optionKey, NSAlternateKeyMask },
171 { controlKey, NSControlKeyMask },
172 { shiftKey, NSShiftKeyMask },
175 for (i = 0; i < 6; i++) {
176 if (modifiers & carbonToCocoa[i][0]) {
177 cocoaModifiers += carbonToCocoa[i][1];
180 [item setKeyEquivalentModifierMask:cocoaModifiers];
182 //Missing key combos for some keys. Must find them later.
196 // Haven't tested this, though it should work.
198 [[NSString stringWithString:@"Space"] getCharacters:&buffer];
200 /*MenuRef menuRef = _NSGetCarbonMenu([item menu]);
201 NSLog(@"%@", menuRef);
202 SetMenuItemCommandKey(menuRef, 0, NO, 49);
203 SetMenuItemModifiers(menuRef, 0, kMenuNoCommandModifier);
204 SetMenuItemKeyGlyph(menuRef, 0, kMenuBlankGlyph);
211 charcode = NSDeleteFunctionKey;
227 charcode = NSF5FunctionKey;
231 charcode = NSF6FunctionKey;
235 charcode = NSF7FunctionKey;
239 charcode = NSF3FunctionKey;
243 charcode = NSF8FunctionKey;
247 charcode = NSF9FunctionKey;
251 charcode = NSF11FunctionKey;
255 charcode = NSF3FunctionKey;
259 charcode = NSF14FunctionKey;
263 charcode = NSF10FunctionKey;
267 charcode = NSF12FunctionKey;
271 charcode = NSF13FunctionKey;
275 charcode = NSInsertFunctionKey;
279 charcode = NSHomeFunctionKey;
283 charcode = NSPageUpFunctionKey;
287 charcode = NSDeleteFunctionKey;
291 charcode = NSF4FunctionKey;
295 charcode = NSEndFunctionKey;
299 charcode = NSF2FunctionKey;
303 charcode = NSPageDownFunctionKey;
307 charcode = NSF1FunctionKey;
311 charcode = NSLeftArrowFunctionKey;
315 charcode = NSRightArrowFunctionKey;
319 charcode = NSDownArrowFunctionKey;
323 charcode = NSUpArrowFunctionKey;
327 if (charcode == 'a') {
333 kchr = (Ptr) GetScriptVariable(smCurrentScript, smKCHRCache);
334 keyTrans = KeyTranslate(kchr, code, &state);
336 [item setKeyEquivalent:[NSString stringWithCString:&charCode length:1]];
337 } else if (charcode != 'b') {
338 [item setKeyEquivalent:[NSString stringWithCharacters:&charcode length:1]];