Search
lxdream.org :: lxdream/src/gtkui/gdrom_menu.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gdrom_menu.c
changeset 691:ad3356543392
prev678:35eb00945316
next736:a02d1475ccfd
author nkeynes
date Thu Jun 19 04:40:37 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Refactor the gd-rom list management out of the GUI (devices, recent files, etc).
Add gd-rom list to the cocoa UI.
file annotate diff log raw
1.1 --- a/src/gtkui/gdrom_menu.c Thu May 29 11:00:26 2008 +0000
1.2 +++ b/src/gtkui/gdrom_menu.c Thu Jun 19 04:40:37 2008 +0000
1.3 @@ -26,173 +26,41 @@
1.4 #include "gdrom/gdrom.h"
1.5 #include "gtkui/gtkui.h"
1.6
1.7 -#define MAX_RECENT_ITEMS 5
1.8 +static gboolean gdrom_menu_adjusting = FALSE;
1.9
1.10 -static GList *gdrom_menu_list = NULL;
1.11 -static gboolean gdrom_menu_adjusting = FALSE;
1.12 -static GList *gdrom_device_list = NULL;
1.13 -static GList *gdrom_recent_list = NULL;
1.14 -
1.15 -void gdrom_menu_rebuild_all();
1.16 -
1.17 -
1.18 -gint gdrom_menu_find_item( const gchar *name )
1.19 -{
1.20 - gint posn = 0;
1.21 - GList *ptr;
1.22 -
1.23 - for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.24 - gchar *device = (gchar *)ptr->data;
1.25 - posn++;
1.26 - if( strcmp(device, name) == 0 ) {
1.27 - return posn;
1.28 - }
1.29 - }
1.30 - posn++;
1.31 - for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.32 - gchar *file = (gchar *)ptr->data;
1.33 - posn++;
1.34 - if( strcmp(file, name) == 0 ) {
1.35 - return posn;
1.36 - }
1.37 - }
1.38 - return -1;
1.39 -}
1.40 -
1.41 -gint gdrom_menu_add_recent_item( const gchar *name )
1.42 -{
1.43 - gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
1.44 - if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
1.45 - GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
1.46 - g_free( ptr->data );
1.47 - gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
1.48 - }
1.49 -
1.50 - GList *ptr;
1.51 - int size = 0;
1.52 - for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.53 - size += strlen( (gchar *)ptr->data ) + 1;
1.54 - }
1.55 - char buf[size];
1.56 - strcpy( buf, (gchar *)gdrom_recent_list->data );
1.57 - ptr = g_list_next(gdrom_recent_list);
1.58 - while( ptr != NULL ) {
1.59 - strcat( buf, ":" );
1.60 - strcat( buf, (gchar *)ptr->data );
1.61 - ptr = g_list_next(ptr);
1.62 - }
1.63 - lxdream_set_global_config_value( CONFIG_RECENT, buf );
1.64 - lxdream_save_config();
1.65 -
1.66 - return g_list_length( gdrom_device_list ) + 2; // menu posn of new item
1.67 -}
1.68 -
1.69 -void gdrom_menu_update_all()
1.70 -{
1.71 - const gchar *disc_name = gdrom_get_current_disc_name();
1.72 - gint posn = 0;
1.73 - GList *ptr;
1.74 -
1.75 - gdrom_menu_adjusting = TRUE;
1.76 -
1.77 - if( disc_name != NULL ) {
1.78 - posn = gdrom_menu_find_item( disc_name );
1.79 - if( posn == -1 ) {
1.80 - posn = gdrom_menu_add_recent_item( disc_name );
1.81 - gdrom_menu_rebuild_all();
1.82 - }
1.83 - }
1.84 -
1.85 - for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.86 - GtkWidget *menu = GTK_WIDGET(ptr->data);
1.87 - GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
1.88 - GList *item = g_list_nth( children, posn );
1.89 - assert( item != NULL );
1.90 - gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM(item->data), TRUE );
1.91 - g_list_free(children);
1.92 - }
1.93 -
1.94 - gdrom_menu_adjusting = FALSE;
1.95 -}
1.96 -
1.97 -void gdrom_menu_empty_callback( GtkWidget *widget, gpointer user_data )
1.98 +static void gdrom_menu_open_image_callback( GtkWidget *widget, gpointer user_data )
1.99 {
1.100 if( !gdrom_menu_adjusting ) {
1.101 - gdrom_unmount_disc();
1.102 - lxdream_set_global_config_value( CONFIG_GDROM, NULL );
1.103 - lxdream_save_config();
1.104 + const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
1.105 + open_file_dialog( _("Open..."), gdrom_mount_image, NULL, NULL, dir );
1.106 }
1.107 }
1.108
1.109 -void gdrom_menu_disc_changed( gdrom_disc_t disc, const gchar *name, void *user_data )
1.110 +void gdrom_menu_item_callback( GtkWidget *widget, gpointer user_data )
1.111 {
1.112 if( !gdrom_menu_adjusting ) {
1.113 - gdrom_menu_update_all();
1.114 - }
1.115 -}
1.116 -
1.117 -gboolean gdrom_menu_open_file( const char *filename )
1.118 -{
1.119 - gboolean result = FALSE;
1.120 - if( filename != NULL ) {
1.121 - result = gdrom_mount_image(filename);
1.122 - }
1.123 - if( result ) {
1.124 - lxdream_set_global_config_value( CONFIG_GDROM, filename );
1.125 - lxdream_save_config();
1.126 - }
1.127 - return result;
1.128 -}
1.129 -
1.130 -void gdrom_menu_open_image_callback( GtkWidget *widget, gpointer user_data )
1.131 -{
1.132 - if( !gdrom_menu_adjusting ) {
1.133 - const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
1.134 - open_file_dialog( _("Open..."), gdrom_menu_open_file, NULL, NULL, dir );
1.135 - }
1.136 -}
1.137 -
1.138 -
1.139 -void gdrom_menu_open_specified_callback( GtkWidget *widget, gpointer user_data )
1.140 -{
1.141 - if( !gdrom_menu_adjusting ) {
1.142 - gdrom_menu_open_file( (gchar *)user_data );
1.143 + gdrom_list_set_selection( GPOINTER_TO_INT(user_data) );
1.144 }
1.145 }
1.146
1.147 void gdrom_menu_build( GtkWidget *menu )
1.148 {
1.149 + unsigned int i, len;
1.150 GSList *group = NULL;
1.151 - GtkWidget *empty = gtk_radio_menu_item_new_with_label( group, _("Empty") );
1.152 - group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(empty) );
1.153 - g_signal_connect_after( empty, "activate", G_CALLBACK(gdrom_menu_empty_callback), NULL );
1.154 - gtk_menu_shell_append( GTK_MENU_SHELL(menu), empty );
1.155
1.156 - GList *ptr;
1.157 - for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.158 - gchar *name = (gchar *)ptr->data;
1.159 - GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name);
1.160 - gtk_widget_set_name( item, name );
1.161 - group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
1.162 - g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
1.163 - name );
1.164 - gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
1.165 + len = gdrom_list_size();
1.166 + for( i=0; i < len; i++ ) {
1.167 + const gchar *entry = gdrom_list_get_display_name(i);
1.168 + if( entry[0] == '\0' ) { // Empty string = separator
1.169 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
1.170 + } else {
1.171 + GtkWidget *item = gtk_radio_menu_item_new_with_label( group, entry );
1.172 + group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
1.173 + g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_item_callback), GINT_TO_POINTER(i) );
1.174 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
1.175 + }
1.176 }
1.177
1.178 - if( gdrom_recent_list != NULL ) {
1.179 - gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
1.180 - for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.181 - gchar *path = (gchar *)ptr->data;
1.182 - gchar *name = basename(path);
1.183 - GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name );
1.184 - gtk_widget_set_name( item, path );
1.185 - group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
1.186 - g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
1.187 - path );
1.188 - gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
1.189 -
1.190 - }
1.191 - }
1.192 gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
1.193 GtkWidget *open = gtk_image_menu_item_new_with_label( _("Open image file...") );
1.194 g_signal_connect_after( open, "activate", G_CALLBACK(gdrom_menu_open_image_callback), NULL );
1.195 @@ -200,47 +68,45 @@
1.196 gtk_widget_show_all(menu);
1.197 }
1.198
1.199 +void gdrom_menu_rebuild( GtkWidget *menu )
1.200 +{
1.201 + GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
1.202 + GList *listptr;
1.203 + for( listptr = children; listptr != NULL; listptr = g_list_next(listptr) ) {
1.204 + gtk_widget_destroy( GTK_WIDGET(listptr->data) );
1.205 + }
1.206 + g_list_free(children);
1.207 + gdrom_menu_build(menu);
1.208 +}
1.209 +
1.210 +gboolean gdrom_menu_update( gboolean list_changed, int selection, void *user_data )
1.211 +{
1.212 + gdrom_menu_adjusting = TRUE;
1.213 + GtkWidget *menu = GTK_WIDGET(user_data);
1.214 +
1.215 + if( list_changed ) {
1.216 + gdrom_menu_rebuild(menu);
1.217 + }
1.218 +
1.219 + GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
1.220 + GList *item = g_list_nth( children, selection );
1.221 + assert( item != NULL );
1.222 + gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM(item->data), TRUE );
1.223 + g_list_free(children);
1.224 +
1.225 + gdrom_menu_adjusting = FALSE;
1.226 + return TRUE;
1.227 +}
1.228 +
1.229 GtkWidget *gdrom_menu_new()
1.230 {
1.231 GtkWidget *menu = gtk_menu_new();
1.232 gtk_menu_set_title( GTK_MENU(menu), _("GD-Rom Settings") );
1.233
1.234 gdrom_menu_build(menu);
1.235 -
1.236 - gdrom_menu_list = g_list_append(gdrom_menu_list, menu);
1.237 + register_gdrom_list_change_hook(gdrom_menu_update, menu);
1.238 + gdrom_menu_update( FALSE, gdrom_list_get_selection(), menu );
1.239 gtk_widget_show_all(menu);
1.240 - gdrom_menu_update_all();
1.241 +
1.242 return menu;
1.243 }
1.244 -
1.245 -void gdrom_menu_rebuild_all()
1.246 -{
1.247 - GList *ptr;
1.248 -
1.249 - for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.250 - GtkWidget *menu = GTK_WIDGET(ptr->data);
1.251 - GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
1.252 - GList *listptr;
1.253 - for( listptr = children; listptr != NULL; listptr = g_list_next(listptr) ) {
1.254 - gtk_widget_destroy( GTK_WIDGET(listptr->data) );
1.255 - }
1.256 - g_list_free(children);
1.257 - gdrom_menu_build(menu);
1.258 - }
1.259 - gdrom_menu_update_all();
1.260 -}
1.261 -
1.262 -void gdrom_menu_init()
1.263 -{
1.264 - const gchar *recent = lxdream_get_config_value( CONFIG_RECENT );
1.265 - register_gdrom_disc_change_hook( gdrom_menu_disc_changed, NULL );
1.266 - gdrom_device_list = gdrom_get_native_devices();
1.267 - if( recent != NULL ) {
1.268 - gchar **list = g_strsplit(recent, ":", 5);
1.269 - int i;
1.270 - for( i=0; list[i] != NULL; i++ ) {
1.271 - gdrom_recent_list = g_list_append( gdrom_recent_list, g_strdup(list[i]) );
1.272 - }
1.273 - g_strfreev(list);
1.274 - }
1.275 -}
.