Search
lxdream.org :: lxdream/src/cocoaui/cocoa_ctrl.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoa_ctrl.c
changeset 765:4cd066048203
next770:429ff505c450
author nkeynes
date Wed Jul 23 11:11:30 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Cocoa preferences panel work-in-progress
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Construct and manage the controller configuration pane
     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.h"
    20 #include "config.h"
    21 #include "maple/maple.h"
    23 #define MAX_DEVICES 4
    25 @implementation LxdreamPrefsControllerPane
    26 + (LxdreamPrefsControllerPane *)new
    27 {
    28     return [[LxdreamPrefsControllerPane alloc] initWithFrame: NSMakeRect(0,0,600,400)];
    29 }
    30 - (id)initWithFrame: (NSRect)frameRect
    31 {
    32     if( [super initWithFrame: frameRect title: NS_("Controllers")] == nil ) {
    33         return nil;
    34     } else {
    35         const struct maple_device_class **devices = maple_get_device_classes();
    36         char buf[16];
    37         int i,j;
    38         int height = [self contentHeight] - TEXT_HEIGHT - TEXT_GAP;
    39         for( i=0; i<MAX_DEVICES; i++ ) {
    40             save_controller[i] = NULL;
    41             maple_device_t device = maple_get_device(i,0);
    42             NSRect frame = NSMakeRect( TEXT_GAP, height -((TEXT_HEIGHT+TEXT_GAP)*i - 2),
    43                                        50, LABEL_HEIGHT );
    44             snprintf( buf, sizeof(buf), _("Slot %d."), i );
    45             NSTextField *label = [self addLabel: [NSString stringWithUTF8String: buf]
    46                                        withFrame: frame ];
    47             [label setAlignment: NSRightTextAlignment];
    48             frame = NSMakeRect( 50 + (TEXT_GAP*2), height - ((TEXT_HEIGHT+TEXT_GAP)*i),
    49                                 150, TEXT_HEIGHT );
    50             NSPopUpButton *popup = [[NSPopUpButton alloc] initWithFrame: frame pullsDown: NO];
    51             [popup addItemWithTitle: NS_("<empty>")];
    52             [popup setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
    53             [[popup itemAtIndex: 0] setTag: 0];
    54             for( j=0; devices[j] != NULL; j++ ) {
    55                 [popup addItemWithTitle: [NSString stringWithUTF8String: devices[j]->name]];
    56                 if( device != NULL && device->device_class == devices[j] ) {
    57                     [popup selectItemAtIndex: (j+1)];
    58                 }
    59                 [[popup itemAtIndex: (j+1)] setTag: (j+1)];
    60             }
    61             [popup setTarget: self];
    62             [popup setAction: @selector(deviceChanged:)];
    63             [popup setTag: i];
    64             [self addSubview: popup];
    65         }
    66         return self;
    67     }
    68 }
    69 - (void)deviceChanged: (id)sender
    70 {
    71     int slot = [sender tag];
    72     int new_device_idx = [sender indexOfSelectedItem] - 1; 
    73     maple_device_class_t new_device_class = NULL;
    75     maple_device_t current = maple_get_device(slot,0);
    76     maple_device_t new_device = NULL;
    77     if( new_device_idx != -1 ) {
    78         new_device_class = maple_get_device_classes()[new_device_idx];
    79     }
    80     if( current == NULL ? new_device_class == NULL : current->device_class == new_device_class ) {
    81         // No change
    82         return;
    83     }
    84     if( current != NULL && current->device_class == &controller_class ) {
    85         save_controller[slot] = current->clone(current);
    86     }
    87     if( new_device_class == NULL ) {
    88         maple_detach_device(slot,0);
    89     } else {
    90         if( new_device_class == &controller_class && save_controller[slot] != NULL ) {
    91             new_device = save_controller[slot];
    92             save_controller[slot] = NULL;
    93         } else {
    94             new_device = maple_new_device( new_device_class->name );
    95         }
    96         maple_attach_device(new_device,slot,0);
    97     }
    99     lxdream_save_config();
   100 }
   101 @end
.