2 // AXCStartingPointsController.m
5 // Created by Mac-arena the Bored Zo on 2005-10-31.
6 // Copyright 2005 Adium Team. All rights reserved.
9 #import "AXCStartingPointsController.h"
11 #import "AXCDocumentController.h"
12 #import "NSMutableArrayAdditions.h"
13 #import "AXCPreferenceController.h"
15 @implementation AXCStartingPointsController
19 NSString * startupAction = [[NSUserDefaults standardUserDefaults] objectForKey:STARTUP_ACTION_KEY];
20 if ([startupAction isEqualToString:STARTING_POINTS_STARTUP_ACTION]) {
21 if (!startingPointsWindow)
22 [NSBundle loadNibNamed:@"StartingPoints" owner:self];
24 [startingPointsTableView setDoubleAction:@selector(makeNewDocumentOfSelectedType:)];
25 [startingPointsTableView setTarget:self];
27 //question for the ages: would it be possible to extend the selection to an empty selection?
28 [startingPointsTableView selectRowIndexes:[NSIndexSet indexSet] byExtendingSelection:NO];
30 //set the window's frame appropriately, then show it.
31 if(![startingPointsWindow setFrameUsingName:[startingPointsWindow frameAutosaveName]])
32 [startingPointsWindow center];
33 [startingPointsWindow makeKeyAndOrderFront:nil];
40 [documentTypes release];
41 [usableDocTypes release];
42 [startingPointsWindow release];
49 - (NSArray *) documentTypes
52 NSDictionary *typeDicts = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDocumentTypes"];
53 unsigned numTypes = [typeDicts count];
54 NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity:numTypes];
55 usableDocTypes = [[NSMutableSet alloc] initWithCapacity:numTypes];
57 NSEnumerator *typeDictsEnum = [typeDicts objectEnumerator];
58 NSDictionary *typeDict;
59 while ((typeDict = [typeDictsEnum nextObject])) {
60 NSString *name = [typeDict objectForKey:@"CFBundleTypeName"];
61 unsigned newIdx = [temp indexForInsortingObject:name usingSelector:@selector(caseInsensitiveCompare:)];
62 [temp insertObject:name atIndex:newIdx];
64 if (NSClassFromString([typeDict objectForKey:@"NSDocumentClass"]))
65 [usableDocTypes addObject:name];
68 [temp sortUsingSelector:@selector(caseInsensitiveCompare:)];
70 documentTypes = [temp copy];
77 - (void) setStartingPointsVisible:(BOOL)flag
80 [startingPointsWindow makeKeyAndOrderFront:nil];
82 [startingPointsWindow orderOut:nil];
84 - (BOOL) isStartingPointsVisible
86 return [startingPointsWindow isVisible];
92 - (IBAction) makeNewDocumentOfSelectedType:(id)sender
94 int selection = [sender selectedRow];
96 [[AXCDocumentController sharedDocumentController] openUntitledDocumentOfType:[documentTypes objectAtIndex:selection] display:YES];
98 - (IBAction) makeNewDocumentWithTypeBeingTitleOfMenuItem:(NSMenuItem *)sender
100 [[AXCDocumentController sharedDocumentController] openUntitledDocumentOfType:[sender title] display:YES];
103 - (IBAction) displayStartingPoints:(id)sender
105 [self setStartingPointsVisible:YES];
109 #pragma mark NSTableView delegate conformance
111 - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row
113 return ([[AXCDocumentController sharedDocumentController] documentClassForType:[[self documentTypes] objectAtIndex:row]] != Nil);
116 - (void) tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)col row:(int)row
118 //if this is a valid type (has a class we can instantiate), enable it. else, disable it.
119 BOOL isValidType = [usableDocTypes containsObject:[documentTypes objectAtIndex:row]];
120 NSColor *textColor = isValidType ? [NSColor controlTextColor] : [NSColor disabledControlTextColor];
121 //B&R: assumes that the cell is an NSTextFieldCell
122 [(NSTextFieldCell *)cell setTextColor:textColor];
126 #pragma mark NSMenu delegate conformance
128 - (int) numberOfItemsInMenu:(NSMenu *)menu
130 return [[self documentTypes] count];
132 - (BOOL) menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(int)index shouldCancel:(BOOL)shouldCancel
134 [item setTitle:[[self documentTypes] objectAtIndex:index]];
136 [item setAction:@selector(makeNewDocumentWithTypeBeingTitleOfMenuItem:)];
137 [item setTarget:self];
139 return !shouldCancel;
143 #pragma mark NSMenuItem validation
145 - (BOOL)validateMenuItem:(id <NSMenuItem>)item
147 //the Starting Points command has a tag of 1. all the menu items in the New submenu have a tag of 0.
149 return ![self isStartingPointsVisible];
151 return ([[AXCDocumentController sharedDocumentController] documentClassForType:[item title]] != Nil);