-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyMainWindow.m
119 lines (87 loc) · 3.48 KB
/
MyMainWindow.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// MyMainWindow.m
// Gallery
//
// Created by David Kapp on 2/27/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "MyMainWindow.h"
// static stuff
static const NSInteger BrowserViewIndex = 0;
static const NSInteger EditorViewIndex = 1;
static const NSInteger ListViewIndex = 2;
// names for the views
static NSString* const MyBrowserViewName = @"MyBrowserView";
static NSString* const MyEditorViewName = @"MyEditorView";
static NSString* const MyListViewName = @"MyListView";
@implementation MyMainWindow
// synthesizers (live in concert!)
@synthesize viewSelectionControl;
@synthesize viewControllers;
@synthesize currentViewController;
@synthesize controllerNamesByIndex;
// method implementations
- (void) loadWindow {
[super loadWindow];
self.viewControllers = [NSMutableDictionary dictionary];
// match indexes to their names
NSMutableArray *names = [NSMutableArray array];
[names insertObject:MyBrowserViewName atIndex:BrowserViewIndex];
[names insertObject:MyEditorViewName atIndex:EditorViewIndex];
[names insertObject:MyListViewName atIndex:ListViewIndex];
self.controllerNamesByIndex = names;
// start in browser mode
NSViewController *initial = [self viewControllerForName:MyBrowserViewName];
[self activateViewController:initial];
}
- (IBAction) viewSelectionDidChange: (id)sender {
// find the requested view controller
NSInteger selection = [sender selectedSegment];
NSArray *names = self.controllerNamesByIndex;
NSString *controllerName = [names objectAtIndex:selection];
// load the controller
NSViewController *controller = [self viewControllerForName:controllerName];
[self activateViewController:controller];
}
- (void) activateViewController: (NSViewController *)controller {
NSArray *names = self.controllerNamesByIndex;
NSInteger segment = self.viewSelectionControl.selectedSegment;
NSString *targetName = [controller className];
NSInteger targetIndex = [names indexOfObject:targetName];
// update segmented control
if (segment != targetIndex) {
[self.viewSelectionControl setSelectedSegment:targetIndex];
}
// remove the current view
[self.currentViewController.view removeFromSuperview];
// now set the new view controller up
self.currentViewController = controller;
[[self.window contentView] addSubview:controller.view];
// adjust for window margins
NSWindow *window = self.window;
CGFloat padding = [window contentBorderThicknessForEdge:NSMinYEdge];
NSRect frame = [window.contentView frame];
frame.size.height -= padding;
frame.origin.y += padding;
controller.view.frame = frame;
}
- (NSViewController*) viewControllerForName:(NSString *)name {
// see if the view already exists
NSMutableDictionary *allControllers = self.viewControllers;
NSViewController *controller = [allControllers objectForKey:name];
if (controller) return controller;
// create a new instance of the view
Class controllerClass = NSClassFromString(name);
controller = [[controllerClass alloc] initWithNibName:name bundle:nil];
[allControllers setObject:controller forKey:name];
// use key-value coding to avoid compiler warnings
[controller setValue:self forKey:@"mainWindowController"];
return [controller autorelease];
}
- (void) dealloc {
self.viewSelectionControl = nil;
self.viewControllers = nil;
self.controllerNamesByIndex = nil;
[super dealloc];
}
@end