Search
lxdream.org :: lxdream/src/cocoaui/cocoa_prefs.m
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoa_prefs.m
changeset 1072:d82e04e6d497
prev1034:7044e01148f0
next1298:d0eb2307b847
author nkeynes
date Tue Jul 21 20:33:21 2009 +1000 (14 years ago)
permissions -rw-r--r--
last change Heavy configuration management refactor
- Configuration groups now take both an on_change event handler and a
default keybinding handler, making most keybinding tasks quite simple
- GUI configuration all merged into a unified model, drastically reducing
the amount of GUI config code.

Bonuses
- OSX now has a hotkey preference pane
- GTK keybinding editor is much more usable
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Construct and manage the preferences panel under cocoa.
     5  *
     6  * Copyright (c) 2008 Nathan Keynes.
     7  *
     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.
    12  *
    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.
    17  */
    19 #include "cocoaui/cocoaui.h"
    20 #include "lxdream.h"
    21 #include "config.h"
    23 static LxdreamPrefsPanel *prefs_panel = NULL;
    25 @implementation LxdreamPrefsPane
    26 - (int)contentHeight
    27 {
    28     return [self frame].size.height - headerHeight;
    29 }
    31 - (id)initWithFrame: (NSRect)frameRect title:(NSString *)title
    32 {
    33     if( [super initWithFrame: frameRect ] == nil ) {
    34         return nil;
    35     } else {
    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];
    51         height -= TEXT_GAP;
    53         headerHeight = frameRect.size.height - height;
    54         return self;
    55     }
    56 }
    58 - (id)initWithFrame: (NSRect)frameRect title:(NSString *)title configGroup: (lxdream_config_group_t)group scrollable: (BOOL)scroll
    59 {
    60     if( [self initWithFrame: frameRect title: title] == nil ) {
    61         return nil;
    62     }
    63     ConfigurationView *view = [[ConfigurationView alloc] initWithFrame: frameRect
    64                                                          configGroup: group ];
    65     [view setAutoresizingMask: (NSViewWidthSizable|NSViewMinYMargin)];
    66     if( scroll ) {
    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)];
    75     } else {
    76         [self addSubview: view];
    77     }
    78     return self;
    79 }
    80 @end
    82 /**************************** Main preferences window ************************/
    84 @interface LxdreamPrefsPanel (Private)
    85 - (void) initToolbar;
    86 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
    87 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action;
    88 @end
    90 @implementation LxdreamPrefsPanel
    92 - (NSView *)createControlsPane
    93 {
    94     NSView *pane = [[NSView alloc] initWithFrame: NSMakeRect(0,0,640,400)];
    95     return pane;
    96 }
    98 - (id)initWithContentRect:(NSRect)contentRect 
    99 {
   100     if( [super initWithContentRect: contentRect
   101          styleMask: ( NSTitledWindowMask | NSClosableWindowMask | 
   102                  NSMiniaturizableWindowMask | NSResizableWindowMask |
   103                  NSUnifiedTitleAndToolbarWindowMask )
   104                  backing: NSBackingStoreBuffered defer: NO ] == nil ) {
   105         return nil;
   106     } else {
   107         [self setTitle: NS_("Preferences")];
   108         [self setDelegate: self];
   109         [self setMinSize: NSMakeSize(400,300)];
   110         [self initToolbar];
   111         config_panes[0] = [[LxdreamPrefsPane alloc] initWithFrame: NSMakeRect(0,0,600,400)
   112                              title: NS_("Paths")
   113                              configGroup: lxdream_get_config_group(CONFIG_GROUP_GLOBAL)
   114                              scrollable: YES];
   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)
   119                              scrollable: YES];
   121         binding_editor = nil;
   122         [self setContentView: config_panes[0]];
   123         return self;
   124     }
   125 }
   126 - (void)dealloc
   127 {
   128     if( binding_editor != nil ) {
   129         [binding_editor release];
   130         binding_editor = nil;
   131     }
   132     [super dealloc];
   133 }
   134 - (void)windowWillClose: (NSNotification *)notice
   135 {
   136     prefs_panel = NULL;
   137 }
   138 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)view
   139 {
   140     if( [view isKindOfClass: [KeyBindingField class]] ) {
   141         if( binding_editor == nil ) {
   142             binding_editor = [[[KeyBindingEditor alloc] init] retain];
   143         }
   144         return binding_editor;
   145     }
   146     return nil;
   147 }
   148 - (void) initToolbar
   149 {
   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: self];
   167     [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
   168     [toolbar setSizeMode: NSToolbarSizeModeSmall];
   169     [toolbar setSelectedItemIdentifier: @"Paths"];
   170     [self setToolbar: toolbar];
   171 }
   173 - (void)paths_action: (id)sender
   174 {
   175     [self setContentView: config_panes[0]];
   176 }
   177 - (void)controllers_action: (id)sender
   178 {
   179     [self setContentView: config_panes[1]];
   180 }
   181 - (void)hotkeys_action: (id)sender
   182 {
   183     [self setContentView: config_panes[2]];
   184 }
   186 /***************************** Toolbar methods ***************************/
   187 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
   188 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action 
   189 {
   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];
   198     return item;
   199 }
   200 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar 
   201 {
   202     return toolbar_ids;
   203 }
   205 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
   206 {
   207     return toolbar_defaults;
   208 }
   210 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
   211 {
   212     return [NSArray arrayWithObjects: @"Paths", @"Controllers", @"Hotkeys", nil ];
   213 }
   215 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
   216 willBeInsertedIntoToolbar:(BOOL)flag 
   217 {
   218     return [toolbar_items objectForKey: itemIdentifier];
   219 }
   220 @end
   222 void cocoa_gui_show_preferences() 
   223 {
   224     if( prefs_panel == NULL ) {
   225         prefs_panel = [[LxdreamPrefsPanel alloc] initWithContentRect: NSMakeRect(0,0,640,540)];
   226     }
   227     [prefs_panel makeKeyAndOrderFront: prefs_panel];
   228 }
   230 /**************************** Simple config panels ***************************/
.