1// 2// Menu_Extensions.m 3// celestia 4// 5// Created by Da Woon Jung on 12/9/07. 6// Copyright (C) 2007, Celestia Development Team 7// 8 9#import "Menu_Extensions.h" 10#import "CelestiaSelection.h" 11#import "BrowserItem.h" 12#import "CelestiaSettings.h" 13 14 15@implementation NSMenu (CelestiaMenu) 16- (int) indexOfItemWithLocalizableTitle: (NSString *) aTitle 17{ 18 int index = [self indexOfItemWithTitle: NSLocalizedString(aTitle,@"")]; 19 if (index < 0) 20 index = [self indexOfItemWithTitle: aTitle]; 21 return index; 22} 23 24- (void) removeRefMarkItems 25{ 26 int index; 27 index = [self indexOfItemWithTitle: NSLocalizedStringFromTable(@"Reference Vectors",@"po",@"")]; 28 if (index >= 0) [self removeItemAtIndex: index]; 29} 30 31- (void) removePlanetarySystemItem 32{ 33 int satMenuIndex = [self indexOfItemWithTitle: 34 NSLocalizedStringFromTable(@"Satellites",@"po",@"")]; 35 if (satMenuIndex < 0) 36 satMenuIndex = [self indexOfItemWithTitle: 37 NSLocalizedStringFromTable(@"Orbiting Bodies",@"po",@"")]; 38 if (satMenuIndex < 0) 39 satMenuIndex = [self indexOfItemWithTitle: NSLocalizedStringFromTable(@"Planets",@"po",@"")]; 40 if (satMenuIndex >= 0) 41 [self removeItemAtIndex: satMenuIndex]; 42} 43 44- (void) removeAltSurfaceItem 45{ 46 int surfMenuIndex = [self indexOfItemWithLocalizableTitle: 47 @"Show Alternate Surface" ]; 48 if (surfMenuIndex >= 0) 49 [self removeItemAtIndex: surfMenuIndex]; 50} 51 52- (BOOL) insertRefMarkItemsForSelection: (CelestiaSelection *) aSelection 53 atIndex: (int) aIndex 54{ 55 BOOL result = NO; 56 NSMenuItem *mi = nil; 57 id target = nil; 58 CelestiaSettings *settings = [CelestiaSettings shared]; 59 60 if ([aSelection body]) 61 { 62 target = [aSelection body]; 63 mi = [[[NSMenuItem alloc] initWithTitle: NSLocalizedStringFromTable(@"Reference Vectors",@"po",@"") action: nil keyEquivalent: @""] autorelease]; 64 NSMenu *refMarksMenu = [[[NSMenu alloc ] initWithTitle: @"Reference Vectors" ] autorelease]; 65 [mi setSubmenu: refMarksMenu]; 66 if (mi) 67 { 68 [self insertItem: mi atIndex: aIndex]; 69 } 70 71 mi = [[[NSMenuItem alloc] initWithTitle: NSLocalizedStringFromTable(@"Show Body Axes",@"po",@"") action: nil keyEquivalent: @""] autorelease]; 72 if (mi) 73 { 74 [mi setTag: 1000]; 75 [refMarksMenu addItem: mi]; 76 [settings scanForKeys: mi]; 77 } 78 mi = [[[NSMenuItem alloc] initWithTitle: NSLocalizedStringFromTable(@"Show Frame Axes",@"po",@"") action: nil keyEquivalent: @""] autorelease]; 79 if (mi) 80 { 81 [mi setTag: 1001]; 82 [refMarksMenu addItem: mi]; 83 [settings scanForKeys: mi]; 84 } 85 mi = [[[NSMenuItem alloc] initWithTitle: NSLocalizedStringFromTable(@"Show Sun Direction",@"po",@"") action: nil keyEquivalent: @""] autorelease]; 86 if (mi) 87 { 88 [mi setTag: 1002]; 89 [refMarksMenu addItem: mi]; 90 [settings scanForKeys: mi]; 91 } 92 mi = [[[NSMenuItem alloc] initWithTitle: NSLocalizedStringFromTable(@"Show Velocity Vector",@"po",@"") action: nil keyEquivalent: @""] autorelease]; 93 if (mi) 94 { 95 [mi setTag: 1003]; 96 [refMarksMenu addItem: mi]; 97 [settings scanForKeys: mi]; 98 } 99 mi = [[[NSMenuItem alloc] initWithTitle: NSLocalizedStringFromTable(@"Show Planetographic Grid",@"po",@"") action: nil keyEquivalent: @""] autorelease]; 100 if (mi) 101 { 102 [mi setTag: 1004]; 103 [refMarksMenu addItem: mi]; 104 [settings scanForKeys: mi]; 105 } 106 mi = [[[NSMenuItem alloc] initWithTitle: NSLocalizedStringFromTable(@"Show Terminator",@"po",@"") action: nil keyEquivalent: @""] autorelease]; 107 if (mi) 108 { 109 [mi setTag: 1005]; 110 [refMarksMenu addItem: mi]; 111 [settings scanForKeys: mi]; 112 } 113 result = YES; 114 } 115 return result; 116} 117 118- (BOOL) insertPlanetarySystemItemForSelection: (CelestiaSelection *) aSelection 119 target: (id) aTarget 120 atIndex: (int) aIndex 121{ 122 BOOL result = NO; 123 NSMenuItem *mi = nil; 124 id browseItem; 125 if ([aSelection body]) 126 { 127 browseItem = [[[BrowserItem alloc] initWithCelestiaBody: [aSelection body]] autorelease]; 128 [BrowserItem addChildrenToBody: browseItem]; 129 NSArray *children = [browseItem allChildNames]; 130 if (children && [children count] > 0) 131 { 132 mi = [[[NSMenuItem alloc] initWithTitle: NSLocalizedStringFromTable(@"Satellites",@"po",@"") action: nil keyEquivalent: @""] autorelease]; 133 [mi addPlanetarySystemMenuForItem: browseItem 134 target: aTarget 135 action: @selector(selectSatellite:)]; 136 } 137 } 138 else if ([aSelection star]) 139 { 140 browseItem = [[[BrowserItem alloc] initWithCelestiaStar: [aSelection star]] autorelease]; 141 [BrowserItem addChildrenToStar: browseItem]; 142 NSArray *children = [browseItem allChildNames]; 143 if (children && [children count] > 0) 144 { 145 NSString *satMenuItemName = [[browseItem name] isEqualToString: @"Sol"] ? 146 @"Orbiting Bodies" : @"Planets"; 147 mi = [[[NSMenuItem alloc] initWithTitle: NSLocalizedStringFromTable(satMenuItemName,@"po",@"") action: nil keyEquivalent: @""] autorelease]; 148 [mi addPlanetarySystemMenuForItem: browseItem 149 target: aTarget 150 action: @selector(selectSatellite:)]; 151 } 152 } 153 if (mi && [[mi submenu] numberOfItems] > 0) 154 { 155 [self insertItem: mi atIndex: aIndex]; 156 result = YES; 157 } 158 return result; 159} 160 161- (BOOL) insertAltSurfaceItemForSelection: (CelestiaSelection *) aSelection 162 target: (id) aTarget 163 atIndex: (int) aIndex 164{ 165 BOOL result = NO; 166 NSMenuItem *mi = nil; 167 if ([aSelection body]) 168 { 169 NSArray *surfaces = [[aSelection body] alternateSurfaceNames]; 170 if (surfaces && [surfaces count] > 0) 171 { 172 mi = [[[NSMenuItem alloc] initWithTitle: 173 NSLocalizedString(@"Show Alternate Surface",@"") action: nil 174 keyEquivalent: @""] autorelease]; 175 [mi addAltSurfaceMenuWithNames: surfaces 176 target: aTarget 177 action: @selector(activateMenuItem:)]; 178 if (mi && [[mi submenu] numberOfItems] > 0) 179 { 180 [self insertItem: mi atIndex: aIndex]; 181 result = YES; 182 } 183 } 184 } 185 return result; 186} 187@end 188 189 190@implementation NSMenuItem (CelestiaMenu) 191- (void) addPlanetarySystemMenuForItem: (BrowserItem *) browseItem 192 target: (id) target 193 action: (SEL) action 194{ 195 NSMenu *satMenu; 196 id childName; 197 id child; 198 id childChildren; 199 unsigned childCount = 0; 200 BOOL loneChild = NO; 201 NSString *locationsName = NSLocalizedStringFromTable(@"Locations",@"po",@""); 202 NSArray *children = [browseItem allChildNames]; 203 NSEnumerator *childEnum = [children objectEnumerator]; 204 205 while ((childName = [childEnum nextObject])) 206 if (![childName isEqualToString: locationsName]) ++childCount; 207 loneChild = (childCount == 1); 208 209 satMenu = [[[NSMenu alloc ] initWithTitle: @"Satellites" ] autorelease]; 210 [self setSubmenu: satMenu]; 211 212 childEnum = [children objectEnumerator]; 213 while ((childName = [childEnum nextObject])) 214 { 215 if ([childName isEqualToString: locationsName]) 216 continue; 217 NSMenuItem *satMenuItem; 218 child = [browseItem childNamed: childName]; 219 if (child) 220 { 221 childChildren = [child allChildNames]; 222 // Don't create a submenu for a single item 223 if (loneChild) 224 { 225 satMenuItem = self; 226 } 227 else 228 { 229 satMenuItem = [[[NSMenuItem alloc] initWithTitle: childName action: nil keyEquivalent: @""] autorelease]; 230 [satMenuItem setRepresentedObject: [child body] ]; 231 [satMenuItem setTarget: target]; 232 [satMenu addItem: satMenuItem]; 233 } 234 235 if (childChildren && [childChildren count] > 0) 236 { 237 NSMenu *subMenu = [[[NSMenu alloc ] initWithTitle: @"children" ] autorelease]; 238 NSMenuItem *subMenuItem; 239 id subChildName; 240 id subChild; 241 NSEnumerator *subEnum = [childChildren objectEnumerator]; 242 while ((subChildName = [subEnum nextObject])) 243 { 244 subChild = [child childNamed: subChildName]; 245 if (subChild) 246 { 247 subMenuItem = [[[NSMenuItem alloc] initWithTitle: subChildName action: action keyEquivalent: @""] autorelease]; 248 [subMenuItem setRepresentedObject: [subChild body] ]; 249 [subMenuItem setTarget: target]; 250 [subMenu addItem: subMenuItem]; 251 } 252 } 253 [satMenuItem setSubmenu: subMenu]; 254 } 255 else 256 { 257 [satMenuItem setAction: action]; 258 } 259 } 260 } 261} 262 263- (void) addAltSurfaceMenuWithNames: (NSArray *) surfaces 264 target: (id) target 265 action: (SEL) action 266{ 267 NSMenu *surfaceMenu = [[NSMenu alloc ] initWithTitle: @"altsurf" ]; 268 [ self setEnabled: YES ]; 269 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle: NSLocalizedString(@"default",@"") action: action keyEquivalent: @""]; 270 [newItem setTag: 600 ]; 271 [newItem setTarget: target ]; 272 [ surfaceMenu addItem: newItem ]; 273 unsigned i; 274 for (i = 0; i < [surfaces count]; ++i) 275 { 276 newItem = [[NSMenuItem alloc] initWithTitle: NSLocalizedStringFromTable([surfaces objectAtIndex: i],@"po",@"") action: action keyEquivalent: @""]; 277 [newItem setTag: 601+i ]; 278 [newItem setTarget: target ]; 279 [ surfaceMenu addItem: newItem ]; 280 } 281 [ self setSubmenu: surfaceMenu ]; 282 [ surfaceMenu update ]; 283} 284@end 285