Merged [18171]: An exception like [http://www.visualdistortion.org/crash/view.jsp...
[adiumx.git] / Other / XtrasCreator / AXCStartingPointsController.m
blob7aac333e4b35300a5517b0aae93cc10576c4c39c
1 //
2 //  AXCStartingPointsController.m
3 //  XtrasCreator
4 //
5 //  Created by Mac-arena the Bored Zo on 2005-10-31.
6 //  Copyright 2005 Adium Team. All rights reserved.
7 //
9 #import "AXCStartingPointsController.h"
11 #import "AXCDocumentController.h"
12 #import "NSMutableArrayAdditions.h"
13 #import "AXCPreferenceController.h"
15 @implementation AXCStartingPointsController
17 - (void) awakeFromNib
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];
23                 else {
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];
34                 }
35         }
38 - (void) dealloc
40         [documentTypes release];
41         [usableDocTypes release];
42         [startingPointsWindow release];
44         [super dealloc];
47 #pragma mark -
49 - (NSArray *) documentTypes
51         if (!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];
66                 }
68                 [temp sortUsingSelector:@selector(caseInsensitiveCompare:)];
70                 documentTypes = [temp copy];
71                 [temp release];
72         }
74         return documentTypes;
77 - (void) setStartingPointsVisible:(BOOL)flag
79         if (flag)
80                 [startingPointsWindow makeKeyAndOrderFront:nil];
81         else
82                 [startingPointsWindow orderOut:nil];
84 - (BOOL) isStartingPointsVisible
86         return [startingPointsWindow isVisible];
89 #pragma mark -
90 #pragma mark Actions
92 - (IBAction) makeNewDocumentOfSelectedType:(id)sender
94         int selection = [sender selectedRow];
95         if (selection >= 0)
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];
108 #pragma mark -
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];
125 #pragma mark -
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;
142 #pragma mark -
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.
148         if ([item tag])
149                 return ![self isStartingPointsVisible];
150         else
151                 return ([[AXCDocumentController sharedDocumentController] documentClassForType:[item title]] != Nil);
154 @end