4 * Construct and manage the preferences panel under cocoa.
6 * Copyright (c) 2008 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include "cocoaui/cocoaui.h"
23 static LxdreamPrefsPanel *prefs_panel = NULL;
25 @implementation LxdreamPrefsPane
28 return [self frame].size.height - headerHeight;
31 - (id)initWithFrame: (NSRect)frameRect title:(NSString *)title
33 if( [super initWithFrame: frameRect ] == nil ) {
36 int height = frameRect.size.height - TEXT_GAP;
38 NSFont *titleFont = [NSFont fontWithName: @"Helvetica-Bold" size: 16.0];
39 NSRect fontRect = [titleFont boundingRectForFont];
40 int titleHeight = fontRect.size.height + [titleFont descender];
41 NSTextField *label = cocoa_gui_add_label(self, title,
42 NSMakeRect( TEXT_GAP, height-titleHeight,
43 frameRect.size.width - (TEXT_GAP*2), titleHeight ));
44 [label setFont: titleFont];
45 height -= (titleHeight + TEXT_GAP);
47 NSBox *rule = [[NSBox alloc] initWithFrame: NSMakeRect(1, height, frameRect.size.width-2, 1)];
48 [rule setAutoresizingMask: (NSViewMinYMargin|NSViewWidthSizable)];
49 [rule setBoxType: NSBoxSeparator];
50 [self addSubview: rule];
53 headerHeight = frameRect.size.height - height;
58 - (id)initWithFrame: (NSRect)frameRect title:(NSString *)title configGroup: (lxdream_config_group_t)group scrollable: (BOOL)scroll
60 if( [self initWithFrame: frameRect title: title] == nil ) {
63 ConfigurationView *view = [[ConfigurationView alloc] initWithFrame: frameRect
65 [view setAutoresizingMask: (NSViewWidthSizable|NSViewMinYMargin)];
67 NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame: NSMakeRect(0,0,frameRect.size.width,[self contentHeight]+TEXT_GAP)];
68 [scrollView setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
69 [scrollView setDocumentView: view];
70 [scrollView setDrawsBackground: NO];
71 [scrollView setHasVerticalScroller: YES];
72 [scrollView setAutohidesScrollers: YES];
73 [self addSubview: scrollView];
74 // [view scrollRectToVisible: NSMakeRect(0,0,1,1)];
76 [self addSubview: view];
82 /**************************** Main preferences window ************************/
84 @interface LxdreamPrefsPanel (Private)
86 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
87 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action;
90 @implementation LxdreamPrefsPanel
92 - (NSView *)createControlsPane
94 NSView *pane = [[NSView alloc] initWithFrame: NSMakeRect(0,0,640,400)];
98 - (id)initWithContentRect:(NSRect)contentRect
100 if( [super initWithContentRect: contentRect
101 styleMask: ( NSTitledWindowMask | NSClosableWindowMask |
102 NSMiniaturizableWindowMask | NSResizableWindowMask |
103 NSUnifiedTitleAndToolbarWindowMask )
104 backing: NSBackingStoreBuffered defer: NO ] == nil ) {
107 [self setTitle: NS_("Preferences")];
108 [self setDelegate: (id)self];
109 [self setMinSize: NSMakeSize(400,300)];
111 config_panes[0] = [[LxdreamPrefsPane alloc] initWithFrame: NSMakeRect(0,0,600,400)
113 configGroup: lxdream_get_config_group(CONFIG_GROUP_GLOBAL)
115 config_panes[1] = cocoa_gui_create_prefs_controller_pane();
116 config_panes[2] = [[LxdreamPrefsPane alloc] initWithFrame: NSMakeRect(0,0,600,400)
117 title: NS_("Hotkeys")
118 configGroup: lxdream_get_config_group(CONFIG_GROUP_HOTKEYS)
121 binding_editor = nil;
122 [self setContentView: config_panes[0]];
128 if( binding_editor != nil ) {
129 [binding_editor release];
130 binding_editor = nil;
134 - (void)windowWillClose: (NSNotification *)notice
138 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)view
140 if( [view isKindOfClass: [KeyBindingField class]] ) {
141 if( binding_editor == nil ) {
142 binding_editor = [[[KeyBindingEditor alloc] init] retain];
144 return binding_editor;
150 NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamPrefsToolbar"];
152 NSToolbarItem *paths = [self createToolbarItem: @"Paths" label: @"Paths"
153 tooltip: @"Configure system paths" icon: @"tb-paths"
154 action: @selector(paths_action:)];
155 NSToolbarItem *ctrls = [self createToolbarItem: @"Controllers" label: @"Controllers"
156 tooltip: @"Configure controllers" icon: @"tb-ctrls"
157 action: @selector(controllers_action:)];
158 NSToolbarItem *hotkeys=[self createToolbarItem: @"Hotkeys" label: @"Hotkeys"
159 tooltip: @"Configure hotkeys" icon: @"tb-ctrls"
160 action: @selector(hotkeys_action:)];
161 toolbar_ids = [NSArray arrayWithObjects: @"Paths", @"Controllers", @"Hotkeys", nil ];
162 toolbar_defaults = [NSArray arrayWithObjects: @"Paths", @"Controllers", @"Hotkeys", nil ];
163 NSArray *values = [NSArray arrayWithObjects: paths, ctrls, hotkeys, nil ];
164 toolbar_items = [NSDictionary dictionaryWithObjects: values forKeys: toolbar_ids];
166 [toolbar setDelegate: (id)self];
167 [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
168 [toolbar setSizeMode: NSToolbarSizeModeSmall];
169 [toolbar setSelectedItemIdentifier: @"Paths"];
170 [self setToolbar: toolbar];
173 - (void)paths_action: (id)sender
175 [self setContentView: config_panes[0]];
177 - (void)controllers_action: (id)sender
179 [self setContentView: config_panes[1]];
181 - (void)hotkeys_action: (id)sender
183 [self setContentView: config_panes[2]];
186 /***************************** Toolbar methods ***************************/
187 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
188 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action
190 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
191 [item setLabel: label];
192 [item setToolTip: tooltip];
193 [item setTarget: self];
194 NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
195 NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
196 [item setImage: image];
197 [item setAction: action];
200 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
205 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
207 return toolbar_defaults;
210 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
212 return [NSArray arrayWithObjects: @"Paths", @"Controllers", @"Hotkeys", nil ];
215 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
216 willBeInsertedIntoToolbar:(BOOL)flag
218 return [toolbar_items objectForKey: itemIdentifier];
222 void cocoa_gui_show_preferences()
224 if( prefs_panel == NULL ) {
225 prefs_panel = [[LxdreamPrefsPanel alloc] initWithContentRect: NSMakeRect(0,0,640,540)];
227 [prefs_panel makeKeyAndOrderFront: prefs_panel];
230 /**************************** Simple config panels ***************************/
.