Search
lxdream.org :: lxdream/src/gui/ctrl_dlg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gui/ctrl_dlg.c
changeset 447:3e095bfcb476
next450:207461e79f21
author nkeynes
date Tue Oct 16 12:36:29 2007 +0000 (15 years ago)
permissions -rw-r--r--
last change Add gui error reporting
Add initial controller settings dialog
view annotate diff log raw
     1 /**
     2  * $Id: ctrl_dlg.c,v 1.1 2007-10-16 12:36:29 nkeynes Exp $
     3  *
     4  * Define the main (emu) GTK window, along with its menubars,
     5  * toolbars, etc.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <assert.h>
    21 #include <gtk/gtk.h>
    23 #include "dream.h"
    24 #include "gui/gtkui.h"
    25 #include "maple/maple.h"
    27 #define MAX_DEVICES 4
    29 typedef struct maple_slot_data {
    30     maple_device_t old_device;
    31     maple_device_t new_device;
    32     GtkWidget *button;
    33     GtkWidget *combo;
    34 } *maple_slot_data_t;
    36 static struct maple_slot_data maple_data[MAX_DEVICES];
    39 gboolean controller_properties_activated( GtkButton *button, gpointer user_data )
    40 {
    41     maple_slot_data_t data = (maple_slot_data_t)user_data;
    42 }
    44 gboolean controller_device_changed( GtkComboBox *combo, gpointer user_data )
    45 {
    46     maple_slot_data_t data = (maple_slot_data_t)user_data;
    47     int active = gtk_combo_box_get_active(combo);
    48     gtk_widget_set_sensitive(data->button, active != 0);
    49     if( active != 0 ) {
    50 	gchar *devname = gtk_combo_box_get_active_text(combo);
    51 	const maple_device_class_t devclz = maple_get_device_class(devname);
    52 	assert(devclz != NULL);
    53 	if( data->new_device != NULL ) {
    54 	    if( data->new_device->device_class != devclz ) {
    55 		data->new_device->destroy(data->new_device);
    56 		data->new_device = maple_new_device(devname);
    57 	    }
    58 	} else {
    59 	    data->new_device = maple_new_device(devname);
    60 	}
    61     } else {
    62 	if( data->new_device != NULL && data->new_device != data->old_device ) {
    63 	    data->new_device->destroy(data->new_device);
    64 	}
    65 	data->new_device = NULL;
    66     }
    67 }
    69 void controller_commit_changes( )
    70 {
    71     int i;
    72     for( i=0; i<MAX_DEVICES; i++ ) {
    73 	if( maple_data[i].new_device != maple_data[i].old_device ) {
    74 	    if( maple_data[i].old_device != NULL ) {
    75 		maple_detach_device(i,0);
    76 	    }
    77 	    if( maple_data[i].new_device != NULL ) {
    78 		maple_attach_device(maple_data[i].new_device, i, 0 );
    79 	    }
    80 	}
    81     }
    82     dreamcast_save_config("testrc");
    83 }
    85 void controller_cancel_changes( )
    86 {
    87     int i;
    88     for( i=0; i<MAX_DEVICES; i++ ) {
    89 	if( maple_data[i].new_device != NULL && 
    90 	    maple_data[i].new_device != maple_data[i].old_device ) {
    91 	    maple_data[i].new_device->destroy(maple_data[i].new_device);
    92 	}
    93     }
    94 }
    96 GtkWidget *controller_pane_new()
    97 {
    98     GtkWidget *table = gtk_table_new(4, 3, TRUE);
    99     GtkTreeIter iter;
   100     int i,j;
   101     const struct maple_device_class **devices = maple_get_device_classes();
   103     for( i=0; i< MAX_DEVICES; i++ ) {
   104 	char buf[12];
   105 	GtkWidget *combo, *button;
   106 	int active = 0;
   107 	maple_device_t device = maple_get_device(i,0);
   108 	sprintf( buf, "Slot %d.", i );
   109 	gtk_table_attach_defaults( GTK_TABLE(table), gtk_label_new(buf), 0, 1, i, i+1 );
   110 	combo = gtk_combo_box_new_text();
   111 	gtk_combo_box_append_text( GTK_COMBO_BOX(combo), "<empty>" );
   112 	for( j=0; devices[j] != NULL; j++ ) {
   113 	    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), devices[j]->name);
   114 	    if( device != NULL && device->device_class == devices[j] ) {
   115 		active = j+1;
   116 	    }
   117 	}
   118 	gtk_combo_box_set_active(GTK_COMBO_BOX(combo), active);
   119 	gtk_table_attach_defaults( GTK_TABLE(table), combo, 1, 2, i, i+1 );
   120 	button = gtk_button_new_from_stock( GTK_STOCK_PROPERTIES );
   121 	gtk_widget_set_sensitive(button, active != 0);
   122 	gtk_table_attach_defaults( GTK_TABLE(table), button, 2, 3, i, i+1 );
   124 	maple_data[i].old_device = device;
   125 	maple_data[i].new_device = device;
   126 	maple_data[i].combo = combo;
   127 	maple_data[i].button = button;
   128 	g_signal_connect( button, "activate", 
   129 			  G_CALLBACK( controller_properties_activated ), &maple_data[i] );
   130 	g_signal_connect( combo, "changed", 
   131 			  G_CALLBACK( controller_device_changed ), &maple_data[i] );
   133     }
   134     return table;
   135 }
   137 void controller_dialog_run( GtkWindow *parent )
   138 {
   139     GtkWidget *dialog =
   140 	gtk_dialog_new_with_buttons("Controller Settings", parent, 
   141 				    GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
   142 				    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
   143 				    GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
   144 				    NULL);
   145     GtkWidget *panel = controller_pane_new();
   146     gint result;
   147     gtk_widget_show_all(panel);
   148     gtk_container_add( GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), panel );
   149     result = gtk_dialog_run( GTK_DIALOG(dialog) );
   150     gtk_widget_destroy( dialog );
   151     if( result == GTK_RESPONSE_ACCEPT ) {
   152 	controller_commit_changes();
   153     } else {
   154 	controller_cancel_changes();
   155     }
   156 }
.