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