Implement preferences window resizing
[macgdbp.git] / Source / PreferencesController.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2009, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "PreferencesController.h"
18
19 NSSize generalSize;
20 NSSize pathsSize;
21
22 @interface PreferencesController (Private)
23 - (void)resizeWindowToSize:(NSSize)size;
24 @end
25
26
27 @implementation PreferencesController
28
29 /**
30 * Loads the NIB and shows the preferences
31 */
32 - (id)init
33 {
34 if (self = [super initWithWindowNibName:@"Preferences"])
35 {
36 blankView = [[NSView alloc] init];
37 }
38 return self;
39 }
40
41 /**
42 * Destructor
43 */
44 - (void)dealloc
45 {
46 [blankView release];
47 [super dealloc];
48 }
49
50 /**
51 * Awake from nib
52 */
53 - (void)awakeFromNib
54 {
55 generalSize = [generalPreferencesView frame].size;
56 pathsSize = [pathsPreferencesView frame].size;
57 }
58
59 /**
60 * Shows the preferences controller window
61 */
62 - (void)showPreferencesWindow
63 {
64 [self showGeneral:self];
65 [[self window] makeKeyAndOrderFront:self];
66 }
67
68 #pragma mark Panel Switching
69
70 /**
71 * Shows the general panel
72 */
73 - (IBAction)showGeneral:(id)sender
74 {
75 if ([[self window] contentView] == generalPreferencesView)
76 return;
77
78 [self resizeWindowToSize:generalSize];
79
80 [[self window] setContentView:generalPreferencesView];
81 [toolbar setSelectedItemIdentifier:[generalPreferencesItem itemIdentifier]];
82 }
83
84 /**
85 * Shows the path replacement panel
86 */
87 - (IBAction)showPaths:(id)sender
88 {
89 if ([[self window] contentView] == pathsPreferencesView)
90 return;
91
92 [self resizeWindowToSize:pathsSize];
93
94 [[self window] setContentView:pathsPreferencesView];
95 [toolbar setSelectedItemIdentifier:[pathsPreferencesItem itemIdentifier]];
96 }
97
98 #pragma mark NSToolbar Delegate
99
100 /**
101 * Returns the selection names
102 */
103 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
104 {
105 return [NSArray arrayWithObjects:
106 [generalPreferencesItem itemIdentifier],
107 [pathsPreferencesItem itemIdentifier],
108 nil
109 ];
110 }
111
112 #pragma mark Private
113
114 /**
115 * Resizes the preferences window to be the size of the given preferences panel
116 */
117 - (void)resizeWindowToSize:(NSSize)size
118 {
119 [[self window] setContentView:blankView]; // don't want weird redraw artifacts
120
121 NSRect newFrame;
122
123 newFrame = [NSWindow contentRectForFrameRect:[[self window] frame] styleMask:[[self window] styleMask]];
124
125 float height = size.height + 50;
126
127 newFrame.origin.y += newFrame.size.height;
128 newFrame.origin.y -= height;
129 newFrame.size.height = height;
130 newFrame.size.width = size.width;
131
132 newFrame = [NSWindow frameRectForContentRect:newFrame styleMask:[[self window] styleMask]];
133
134 [[self window] setFrame:newFrame display:YES animate:YES];
135 }
136
137 @end