1 /* roms.c: ROM selector dialog box
2    Copyright (c) 2003-2004 Philip Kendall
3    Copyright (c) 2015 Stuart Brady
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation, Inc.,
17    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 
19    Author contact information:
20 
21    E-mail: philip-fuse@shadowmagic.org.uk
22 
23 */
24 
25 #include <config.h>
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #include <gdk/gdkkeysyms.h>
31 #include <gtk/gtk.h>
32 
33 #include "compat.h"
34 #include "fuse.h"
35 #include "gtkcompat.h"
36 #include "gtkinternals.h"
37 #include "menu.h"
38 #include "settings.h"
39 #include "ui/ui.h"
40 
41 static void add_rom( GtkBox *parent, size_t start, gint row,
42 		     int is_peripheral );
43 static void select_new_rom( GtkWidget *widget, gpointer data );
44 static void roms_done( GtkButton *button, gpointer data );
45 
46 /* The labels used to display the current ROMs */
47 static GtkWidget *rom[ SETTINGS_ROM_COUNT ];
48 
49 struct callback_info {
50 
51   size_t start, n;
52   int is_peripheral;
53 
54 };
55 
56 int
menu_select_roms_with_title(const char * title,size_t start,size_t n,int is_peripheral)57 menu_select_roms_with_title( const char *title, size_t start, size_t n,
58 			     int is_peripheral )
59 {
60   GtkWidget *dialog;
61   GtkBox *vbox;
62 
63   struct callback_info info;
64 
65   char buffer[ 256 ];
66   size_t i;
67 
68   /* Firstly, stop emulation */
69   fuse_emulation_pause();
70 
71   /* Give me a new dialog box */
72   snprintf( buffer, 256, "Fuse - Select ROMs - %s", title );
73   dialog = gtkstock_dialog_new( buffer, NULL );
74 
75   info.start = start;
76   info.n = n;
77   info.is_peripheral = is_peripheral;
78 
79   /* Create the OK and Cancel buttons */
80   gtkstock_create_ok_cancel( dialog, NULL, G_CALLBACK( roms_done ), &info,
81                              DEFAULT_DESTROY, DEFAULT_DESTROY );
82 
83   /* And the current values of each of the ROMs */
84   vbox = GTK_BOX( gtk_dialog_get_content_area( GTK_DIALOG( dialog ) ) );
85 
86   gtk_container_set_border_width( GTK_CONTAINER( vbox ), 5 );
87   for( i = 0; i < n; i++ ) add_rom( vbox, start, i, is_peripheral );
88 
89   /* Users shouldn't be able to resize this window */
90   gtk_window_set_resizable( GTK_WINDOW( dialog ), FALSE );
91 
92   /* Display the window */
93   gtk_widget_show_all( dialog );
94 
95   /* Process events until the window is done with */
96   gtk_main();
97 
98   /* And then carry on with emulation again */
99   fuse_emulation_unpause();
100 
101   return 0;
102 }
103 
104 static void
add_rom(GtkBox * parent,size_t start,gint row,int is_peripheral)105 add_rom( GtkBox *parent, size_t start, gint row, int is_peripheral )
106 {
107   GtkWidget *frame, *hbox, *change_button;
108   char buffer[ 80 ], **setting;
109 
110   snprintf( buffer, 80, "ROM %d", row );
111   frame = gtk_frame_new( buffer );
112   gtk_box_pack_start( parent, frame, FALSE, FALSE, 2 );
113 
114   hbox = gtk_box_new( GTK_ORIENTATION_HORIZONTAL, 4 );
115   gtk_container_set_border_width( GTK_CONTAINER( hbox ), 4 );
116   gtk_container_add( GTK_CONTAINER( frame ), hbox );
117 
118   setting = settings_get_rom_setting( &settings_current, start + row,
119 				      is_peripheral );
120   rom[ row ] = gtk_entry_new();
121   gtk_entry_set_text( GTK_ENTRY( rom[ row ] ), *setting );
122   gtk_box_pack_start( GTK_BOX( hbox ), rom[ row ], FALSE, FALSE, 2 );
123 
124   change_button = gtk_button_new_with_label( "Select..." );
125   g_signal_connect( G_OBJECT( change_button ), "clicked",
126 		    G_CALLBACK( select_new_rom ),
127 		    rom[ row ] );
128   gtk_box_pack_start( GTK_BOX( hbox ), change_button, FALSE, FALSE, 2 );
129 }
130 
131 static void
select_new_rom(GtkWidget * widget GCC_UNUSED,gpointer data)132 select_new_rom( GtkWidget *widget GCC_UNUSED, gpointer data )
133 {
134   char *filename;
135 
136   GtkWidget *entry = data;
137 
138   filename = ui_get_open_filename( "Fuse - Select ROM" );
139   if( !filename ) return;
140 
141   gtk_entry_set_text( GTK_ENTRY( entry ), filename );
142 }
143 
144 static void
roms_done(GtkButton * button GCC_UNUSED,gpointer data)145 roms_done( GtkButton *button GCC_UNUSED, gpointer data )
146 {
147   size_t i;
148 
149   char **setting; const char *string;
150 
151   struct callback_info *info = data;
152 
153   for( i = 0; i < info->n; i++ ) {
154 
155     setting = settings_get_rom_setting( &settings_current, info->start + i,
156 				        info->is_peripheral );
157     string = gtk_entry_get_text( GTK_ENTRY( rom[i] ) );
158 
159     settings_set_string( setting, string );
160   }
161 }
162