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 849:bbe26d798fc2
prev770:429ff505c450
author nkeynes
date Mon Sep 08 05:13:51 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Refactor mouse event management - button events are now usable for controllers
file annotate diff log raw
nkeynes@725
     1
/**
nkeynes@729
     2
 * $Id$
nkeynes@725
     3
 *
nkeynes@725
     4
 * Construct and manage the preferences panel under cocoa.
nkeynes@725
     5
 *
nkeynes@725
     6
 * Copyright (c) 2008 Nathan Keynes.
nkeynes@725
     7
 *
nkeynes@725
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@725
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@725
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@725
    11
 * (at your option) any later version.
nkeynes@725
    12
 *
nkeynes@725
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@725
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@725
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@725
    16
 * GNU General Public License for more details.
nkeynes@725
    17
 */
nkeynes@725
    18
nkeynes@725
    19
#include "cocoaui/cocoaui.h"
nkeynes@725
    20
#include "lxdream.h"
nkeynes@725
    21
#include "config.h"
nkeynes@725
    22
nkeynes@765
    23
static LxdreamPrefsPanel *prefs_panel = NULL;
nkeynes@765
    24
nkeynes@765
    25
@implementation LxdreamPrefsPane
nkeynes@765
    26
- (int)contentHeight
nkeynes@765
    27
{
nkeynes@765
    28
    return [self frame].size.height - headerHeight;
nkeynes@765
    29
}
nkeynes@765
    30
nkeynes@765
    31
- (id)initWithFrame: (NSRect)frameRect title:(NSString *)title
nkeynes@765
    32
{
nkeynes@765
    33
    if( [super initWithFrame: frameRect ] == nil ) {
nkeynes@765
    34
        return nil;
nkeynes@765
    35
    } else {
nkeynes@765
    36
        int height = frameRect.size.height - TEXT_GAP;
nkeynes@765
    37
        
nkeynes@765
    38
        NSFont *titleFont = [NSFont fontWithName: @"Helvetica-Bold" size: 16.0];
nkeynes@765
    39
        NSRect fontRect = [titleFont boundingRectForFont];
nkeynes@765
    40
        int titleHeight = fontRect.size.height + [titleFont descender];
nkeynes@770
    41
        NSTextField *label = cocoa_gui_add_label(self, title, 
nkeynes@765
    42
            NSMakeRect( TEXT_GAP, height-titleHeight, 
nkeynes@770
    43
                        frameRect.size.width - (TEXT_GAP*2), titleHeight ));
nkeynes@765
    44
        [label setFont: titleFont];
nkeynes@765
    45
        height -= (titleHeight + TEXT_GAP);
nkeynes@765
    46
        
nkeynes@765
    47
        NSBox *rule = [[NSBox alloc] initWithFrame: NSMakeRect(1, height, frameRect.size.width-2, 1)];
nkeynes@765
    48
        [rule setAutoresizingMask: (NSViewMinYMargin|NSViewWidthSizable)];
nkeynes@765
    49
        [rule setBoxType: NSBoxSeparator];
nkeynes@765
    50
        [self addSubview: rule];
nkeynes@765
    51
        height -= TEXT_GAP;
nkeynes@765
    52
      
nkeynes@765
    53
        headerHeight = frameRect.size.height - height;
nkeynes@765
    54
        return self;
nkeynes@765
    55
    }
nkeynes@765
    56
}
nkeynes@725
    57
@end
nkeynes@725
    58
nkeynes@770
    59
/**************************** Main preferences window ************************/
nkeynes@765
    60
nkeynes@765
    61
@interface LxdreamPrefsPanel (Private)
nkeynes@765
    62
- (void) initToolbar;
nkeynes@765
    63
- (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
nkeynes@765
    64
tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action;
nkeynes@765
    65
@end
nkeynes@765
    66
nkeynes@765
    67
@implementation LxdreamPrefsPanel
nkeynes@765
    68
nkeynes@765
    69
- (NSView *)createControlsPane
nkeynes@725
    70
{
nkeynes@849
    71
    NSView *pane = [[NSView alloc] initWithFrame: NSMakeRect(0,0,640,400)];
nkeynes@765
    72
    return pane;
nkeynes@765
    73
}
nkeynes@765
    74
nkeynes@765
    75
- (id)initWithContentRect:(NSRect)contentRect 
nkeynes@765
    76
{
nkeynes@765
    77
    if( [super initWithContentRect: contentRect
nkeynes@765
    78
         styleMask: ( NSTitledWindowMask | NSClosableWindowMask | 
nkeynes@765
    79
                 NSMiniaturizableWindowMask | NSResizableWindowMask |
nkeynes@765
    80
                 NSUnifiedTitleAndToolbarWindowMask )
nkeynes@765
    81
                 backing: NSBackingStoreBuffered defer: NO ] == nil ) {
nkeynes@765
    82
        return nil;
nkeynes@765
    83
    } else {
nkeynes@765
    84
        [self setTitle: NS_("Preferences")];
nkeynes@765
    85
        [self setDelegate: self];
nkeynes@770
    86
        [self setMinSize: NSMakeSize(400,300)];
nkeynes@765
    87
        [self initToolbar];
nkeynes@770
    88
        path_pane = cocoa_gui_create_prefs_path_pane();
nkeynes@770
    89
        ctrl_pane = cocoa_gui_create_prefs_controller_pane();
nkeynes@770
    90
        binding_editor = nil;
nkeynes@765
    91
        [self setContentView: path_pane];
nkeynes@765
    92
        return self;
nkeynes@765
    93
    }
nkeynes@765
    94
}
nkeynes@770
    95
- (void)dealloc
nkeynes@770
    96
{
nkeynes@770
    97
    if( binding_editor != nil ) {
nkeynes@770
    98
        [binding_editor release];
nkeynes@770
    99
        binding_editor = nil;
nkeynes@770
   100
    }
nkeynes@770
   101
    [super dealloc];
nkeynes@770
   102
}
nkeynes@765
   103
- (void)windowWillClose: (NSNotification *)notice
nkeynes@765
   104
{
nkeynes@765
   105
    prefs_panel = NULL;
nkeynes@765
   106
}
nkeynes@770
   107
- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)view
nkeynes@770
   108
{
nkeynes@770
   109
    if( [view isKindOfClass: [KeyBindingField class]] ) {
nkeynes@770
   110
        if( binding_editor == nil ) {
nkeynes@770
   111
            binding_editor = [[[KeyBindingEditor alloc] init] retain];
nkeynes@770
   112
        }
nkeynes@770
   113
        return binding_editor;
nkeynes@770
   114
    }
nkeynes@770
   115
    return nil;
nkeynes@770
   116
}
nkeynes@765
   117
- (void) initToolbar
nkeynes@765
   118
{
nkeynes@765
   119
    NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"LxdreamPrefsToolbar"];
nkeynes@765
   120
nkeynes@725
   121
    NSToolbarItem *paths = [self createToolbarItem: @"Paths" label: @"Paths" 
nkeynes@725
   122
                            tooltip: @"Configure system paths" icon: @"tb-paths" 
nkeynes@725
   123
                            action: @selector(paths_action:)];
nkeynes@725
   124
    NSToolbarItem *ctrls = [self createToolbarItem: @"Controllers" label: @"Controllers"
nkeynes@725
   125
                            tooltip: @"Configure controllers" icon: @"tb-ctrls"
nkeynes@725
   126
                            action: @selector(controllers_action:)];
nkeynes@765
   127
    toolbar_ids = [NSArray arrayWithObjects: @"Paths", @"Controllers", nil ];
nkeynes@765
   128
    toolbar_defaults = [NSArray arrayWithObjects: @"Paths", @"Controllers", nil ]; 
nkeynes@725
   129
    NSArray *values = [NSArray arrayWithObjects: paths, ctrls, nil ];
nkeynes@765
   130
    toolbar_items = [NSDictionary dictionaryWithObjects: values forKeys: toolbar_ids];
nkeynes@765
   131
nkeynes@765
   132
    [toolbar setDelegate: self];
nkeynes@765
   133
    [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
nkeynes@765
   134
    [toolbar setSizeMode: NSToolbarSizeModeSmall];
nkeynes@765
   135
    [toolbar setSelectedItemIdentifier: @"Paths"];
nkeynes@765
   136
    [self setToolbar: toolbar];
nkeynes@725
   137
}
nkeynes@725
   138
nkeynes@765
   139
- (void)paths_action: (id)sender
nkeynes@765
   140
{
nkeynes@765
   141
    [self setContentView: path_pane];
nkeynes@765
   142
}
nkeynes@765
   143
- (void)controllers_action: (id)sender
nkeynes@765
   144
{
nkeynes@765
   145
    [self setContentView: ctrl_pane];
nkeynes@765
   146
}
nkeynes@765
   147
nkeynes@765
   148
/***************************** Toolbar methods ***************************/
nkeynes@725
   149
- (NSToolbarItem *) createToolbarItem: (NSString *)id label: (NSString *) label 
nkeynes@736
   150
tooltip: (NSString *)tooltip icon: (NSString *)icon action: (SEL) action 
nkeynes@725
   151
{
nkeynes@725
   152
    NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier: id];
nkeynes@725
   153
    [item setLabel: label];
nkeynes@725
   154
    [item setToolTip: tooltip];
nkeynes@725
   155
    [item setTarget: self];
nkeynes@725
   156
    NSString *iconFile = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"];
nkeynes@725
   157
    NSImage *image = [[NSImage alloc] initWithContentsOfFile: iconFile];
nkeynes@725
   158
    [item setImage: image];
nkeynes@725
   159
    [item setAction: action];
nkeynes@725
   160
    return item;
nkeynes@725
   161
}
nkeynes@725
   162
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *)toolbar 
nkeynes@725
   163
{
nkeynes@765
   164
    return toolbar_ids;
nkeynes@725
   165
}
nkeynes@725
   166
nkeynes@725
   167
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *)toolbar
nkeynes@725
   168
{
nkeynes@765
   169
    return toolbar_defaults;
nkeynes@725
   170
}
nkeynes@725
   171
nkeynes@725
   172
- (NSArray *)toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar
nkeynes@725
   173
{
nkeynes@765
   174
    return [NSArray arrayWithObjects: @"Paths", @"Controllers", nil ]; 
nkeynes@725
   175
}
nkeynes@725
   176
nkeynes@725
   177
- (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier
nkeynes@736
   178
willBeInsertedIntoToolbar:(BOOL)flag 
nkeynes@725
   179
{
nkeynes@765
   180
    return [toolbar_items objectForKey: itemIdentifier];
nkeynes@725
   181
}
nkeynes@725
   182
@end
nkeynes@725
   183
nkeynes@765
   184
void cocoa_gui_show_preferences() 
nkeynes@725
   185
{
nkeynes@765
   186
    if( prefs_panel == NULL ) {
nkeynes@849
   187
        prefs_panel = [[LxdreamPrefsPanel alloc] initWithContentRect: NSMakeRect(0,0,640,400)];
nkeynes@725
   188
    }
nkeynes@765
   189
    [prefs_panel makeKeyAndOrderFront: prefs_panel];
nkeynes@765
   190
}
.