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;
59 /**************************** Main preferences window ************************/
61 @interface LxdreamPrefsPanel (Private)
63 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
64 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action;
67 @implementation LxdreamPrefsPanel
69 - (NSView *)createControlsPane
71 NSView *pane = [[NSView alloc] initWithFrame: NSMakeRect(0,0,640,400)];
75 - (id)initWithContentRect:(NSRect)contentRect
77 if( [super initWithContentRect: contentRect
78 styleMask: ( NSTitledWindowMask | NSClosableWindowMask |
79 NSMiniaturizableWindowMask | NSResizableWindowMask |
80 NSUnifiedTitleAndToolbarWindowMask )
81 backing: NSBackingStoreBuffered defer: NO ] == nil ) {
84 [self setTitle: NS_("Preferences")];
85 [self setDelegate: self];
86 [self setMinSize: NSMakeSize(400,300)];
88 path_pane = cocoa_gui_create_prefs_path_pane();
89 ctrl_pane = cocoa_gui_create_prefs_controller_pane();
91 [self setContentView: path_pane];
97 if( binding_editor != nil ) {
98 [binding_editor release];
103 - (void)windowWillClose: (NSNotification *)notice
107 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)view
109 if( [view isKindOfClass: [KeyBindingField class]] ) {
110 if( binding_editor == nil ) {
111 binding_editor = [[[KeyBindingEditor alloc] init] retain];
113 return binding_editor;
119 NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamPrefsToolbar"];
121 NSToolbarItem *paths = [self createToolbarItem: @"Paths" label: @"Paths"
122 tooltip: @"Configure system paths" icon: @"tb-paths"
123 action: @selector(paths_action:)];
124 NSToolbarItem *ctrls = [self createToolbarItem: @"Controllers" label: @"Controllers"
125 tooltip: @"Configure controllers" icon: @"tb-ctrls"
126 action: @selector(controllers_action:)];
127 toolbar_ids = [NSArray arrayWithObjects: @"Paths", @"Controllers", nil ];
128 toolbar_defaults = [NSArray arrayWithObjects: @"Paths", @"Controllers", nil ];
129 NSArray *values = [NSArray arrayWithObjects: paths, ctrls, nil ];
130 toolbar_items = [NSDictionary dictionaryWithObjects: values forKeys: toolbar_ids];
132 [toolbar setDelegate: self];
133 [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
134 [toolbar setSizeMode: NSToolbarSizeModeSmall];
135 [toolbar setSelectedItemIdentifier: @"Paths"];
136 [self setToolbar: toolbar];
139 - (void)paths_action: (id)sender
141 [self setContentView: path_pane];
143 - (void)controllers_action: (id)sender
145 [self setContentView: ctrl_pane];
148 /***************************** Toolbar methods ***************************/
149 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label
150 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action
152 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
153 [item setLabel: label];
154 [item setToolTip: tooltip];
155 [item setTarget: self];
156 NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
157 NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
158 [item setImage: image];
159 [item setAction: action];
162 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar
167 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
169 return toolbar_defaults;
172 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
174 return [NSArray arrayWithObjects: @"Paths", @"Controllers", nil ];
177 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
178 willBeInsertedIntoToolbar:(BOOL)flag
180 return [toolbar_items objectForKey: itemIdentifier];
184 void cocoa_gui_show_preferences()
186 if( prefs_panel == NULL ) {
187 prefs_panel = [[LxdreamPrefsPanel alloc] initWithContentRect: NSMakeRect(0,0,640,540)];
189 [prefs_panel makeKeyAndOrderFront: prefs_panel];
.