Search
lxdream.org :: lxdream/src/cocoaui/cocoa_prefs.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoa_prefs.c
changeset 770:429ff505c450
prev765:4cd066048203
next849:bbe26d798fc2
author nkeynes
date Mon Jul 28 03:41:25 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Implement key-binding configuration pane for Cocoa UI
Minor tweaks for consistency and static-correctness
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 }
    57 @end
    59 /**************************** Main preferences window ************************/
    61 @interface LxdreamPrefsPanel (Private)
    62 - (void) initToolbar;
    63 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
    64 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action;
    65 @end
    67 @implementation LxdreamPrefsPanel
    69 - (NSView *)createControlsPane
    70 {
    71     NSView *pane = [[NSView alloc] initWithFrame: NSMakeRect(0,0,600,400)];
    72     return pane;
    73 }
    75 - (id)initWithContentRect:(NSRect)contentRect 
    76 {
    77     if( [super initWithContentRect: contentRect
    78          styleMask: ( NSTitledWindowMask | NSClosableWindowMask | 
    79                  NSMiniaturizableWindowMask | NSResizableWindowMask |
    80                  NSUnifiedTitleAndToolbarWindowMask )
    81                  backing: NSBackingStoreBuffered defer: NO ] == nil ) {
    82         return nil;
    83     } else {
    84         [self setTitle: NS_("Preferences")];
    85         [self setDelegate: self];
    86         [self setMinSize: NSMakeSize(400,300)];
    87         [self initToolbar];
    88         path_pane = cocoa_gui_create_prefs_path_pane();
    89         ctrl_pane = cocoa_gui_create_prefs_controller_pane();
    90         binding_editor = nil;
    91         [self setContentView: path_pane];
    92         return self;
    93     }
    94 }
    95 - (void)dealloc
    96 {
    97     if( binding_editor != nil ) {
    98         [binding_editor release];
    99         binding_editor = nil;
   100     }
   101     [super dealloc];
   102 }
   103 - (void)windowWillClose: (NSNotification *)notice
   104 {
   105     prefs_panel = NULL;
   106 }
   107 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)view
   108 {
   109     if( [view isKindOfClass: [KeyBindingField class]] ) {
   110         if( binding_editor == nil ) {
   111             binding_editor = [[[KeyBindingEditor alloc] init] retain];
   112         }
   113         return binding_editor;
   114     }
   115     return nil;
   116 }
   117 - (void) initToolbar
   118 {
   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];
   137 }
   139 - (void)paths_action: (id)sender
   140 {
   141     [self setContentView: path_pane];
   142 }
   143 - (void)controllers_action: (id)sender
   144 {
   145     [self setContentView: ctrl_pane];
   146 }
   148 /***************************** Toolbar methods ***************************/
   149 - (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
   150 tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action 
   151 {
   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];
   160     return item;
   161 }
   162 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar 
   163 {
   164     return toolbar_ids;
   165 }
   167 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
   168 {
   169     return toolbar_defaults;
   170 }
   172 - (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
   173 {
   174     return [NSArray arrayWithObjects: @"Paths", @"Controllers", nil ]; 
   175 }
   177 - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
   178 willBeInsertedIntoToolbar:(BOOL)flag 
   179 {
   180     return [toolbar_items objectForKey: itemIdentifier];
   181 }
   182 @end
   184 void cocoa_gui_show_preferences() 
   185 {
   186     if( prefs_panel == NULL ) {
   187         prefs_panel = [[LxdreamPrefsPanel alloc] initWithContentRect: NSMakeRect(0,0,600,400)];
   188     }
   189     [prefs_panel makeKeyAndOrderFront: prefs_panel];
.