4 * Construct and manage the controller configuration pane
6 * Copyright (c) 2008 Nathan Keynes.
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.
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.
21 #include "maple/maple.h"
25 @implementation LxdreamPrefsControllerPane
26 + (LxdreamPrefsControllerPane *)new
28 return [[LxdreamPrefsControllerPane alloc] initWithFrame: NSMakeRect(0,0,600,400)];
30 - (id)initWithFrame: (NSRect)frameRect
32 if( [super initWithFrame: frameRect title: NS_("Controllers")] == nil ) {
35 const struct maple_device_class **devices = maple_get_device_classes();
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),
44 snprintf( buf, sizeof(buf), _("Slot %d."), i );
45 NSTextField *label = [self addLabel: [NSString stringWithUTF8String: buf]
47 [label setAlignment: NSRightTextAlignment];
48 frame = NSMakeRect( 50 + (TEXT_GAP*2), height - ((TEXT_HEIGHT+TEXT_GAP)*i),
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)];
59 [[popup itemAtIndex: (j+1)] setTag: (j+1)];
61 [popup setTarget: self];
62 [popup setAction: @selector(deviceChanged:)];
64 [self addSubview: popup];
69 - (void)deviceChanged: (id)sender
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];
80 if( current == NULL ? new_device_class == NULL : current->device_class == new_device_class ) {
84 if( current != NULL && current->device_class == &controller_class ) {
85 save_controller[slot] = current->clone(current);
87 if( new_device_class == NULL ) {
88 maple_detach_device(slot,0);
90 if( new_device_class == &controller_class && save_controller[slot] != NULL ) {
91 new_device = save_controller[slot];
92 save_controller[slot] = NULL;
94 new_device = maple_new_device( new_device_class->name );
96 maple_attach_device(new_device,slot,0);
99 lxdream_save_config();
.