Search
lxdream.org :: lxdream/src/gtkui/gdrom_menu.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gdrom_menu.c
changeset 561:533f6b478071
prev537:d924be49e192
next678:35eb00945316
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Creates and manages the GD-Rom attachment menu.
     5  *
     6  * Copyright (c) 2005 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 <assert.h>
    20 #include <libgen.h>
    21 #include <glib/gi18n.h>
    23 #include "dream.h"
    24 #include "dreamcast.h"
    25 #include "config.h"
    26 #include "gdrom/gdrom.h"
    27 #include "gtkui/gtkui.h"
    29 #define MAX_RECENT_ITEMS 5
    31 static GList *gdrom_menu_list = NULL;
    32 static gboolean gdrom_menu_adjusting = FALSE;
    33 static GList *gdrom_device_list = NULL;
    34 static GList *gdrom_recent_list = NULL;
    36 void gdrom_menu_rebuild_all();
    39 gint gdrom_menu_find_item( const gchar *name )
    40 {
    41     gint posn = 0;
    42     GList *ptr;
    44     for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    45 	gchar *device = (gchar *)ptr->data;
    46 	posn++;
    47 	if( strcmp(device, name) == 0 ) {
    48 	    return posn;
    49 	}
    50     }
    51     posn++;
    52     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    53 	gchar *file = (gchar *)ptr->data;
    54 	posn++;
    55 	if( strcmp(file, name) == 0 ) {
    56 	    return posn;
    57 	}
    58     }
    59     return -1;
    60 }
    62 gint gdrom_menu_add_recent_item( const gchar *name )
    63 {
    64     gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
    65     if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
    66 	GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
    67 	g_free( ptr->data );
    68 	gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
    69     }
    71     GList *ptr;
    72     int size = 0;
    73     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    74 	size += strlen( (gchar *)ptr->data ) + 1;
    75     }
    76     char buf[size];
    77     strcpy( buf, (gchar *)gdrom_recent_list->data );
    78     ptr = g_list_next(gdrom_recent_list);
    79     while( ptr != NULL ) {
    80 	strcat( buf, ":" );
    81 	strcat( buf, (gchar *)ptr->data );
    82 	ptr = g_list_next(ptr);
    83     }
    84     lxdream_set_global_config_value( CONFIG_RECENT, buf );
    85     lxdream_save_config();
    87     return g_list_length( gdrom_device_list ) + 2; // menu posn of new item
    88 }
    90 void gdrom_menu_update_all()
    91 {
    92     gdrom_disc_t disc = gdrom_get_current_disc();
    93     gint posn = 0;
    94     GList *ptr;
    96     gdrom_menu_adjusting = TRUE;
    98     if( disc != NULL ) {
    99 	posn = gdrom_menu_find_item( disc->name );
   100 	if( posn == -1 ) {
   101 	    posn = gdrom_menu_add_recent_item( disc->name );
   102 	    gdrom_menu_rebuild_all();
   103 	}
   104     }
   106     for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   107 	GtkWidget *menu = GTK_WIDGET(ptr->data);
   108 	GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
   109 	GList *item = g_list_nth( children, posn );
   110 	assert( item != NULL );
   111 	gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM(item->data), TRUE );
   112 	g_list_free(children);
   113     }    
   115     gdrom_menu_adjusting = FALSE;
   116 }
   118 void gdrom_menu_empty_callback( GtkWidget *widget, gpointer user_data )
   119 {
   120     if( !gdrom_menu_adjusting ) {
   121 	gdrom_unmount_disc();
   122 	gdrom_menu_update_all();
   123 	lxdream_set_global_config_value( CONFIG_GDROM, NULL );
   124 	lxdream_save_config();
   125     }
   126 }
   128 gboolean gdrom_menu_open_file( const char *filename )
   129 {
   130     gboolean result = FALSE;
   131     if( filename != NULL ) {
   132 	result = gdrom_mount_image(filename);
   133     }
   134     if( result ) {
   135 	gdrom_menu_update_all();
   136 	lxdream_set_global_config_value( CONFIG_GDROM, filename );
   137 	lxdream_save_config();
   138     }
   139     return result;
   140 }
   142 void gdrom_menu_open_image_callback( GtkWidget *widget, gpointer user_data )
   143 {
   144     if( !gdrom_menu_adjusting ) {
   145 	const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
   146 	open_file_dialog( _("Open..."), gdrom_menu_open_file, NULL, NULL, dir );
   147     }
   148 }
   151 void gdrom_menu_open_specified_callback( GtkWidget *widget, gpointer user_data )
   152 {
   153     if( !gdrom_menu_adjusting ) {
   154 	gdrom_menu_open_file( (gchar *)user_data );
   155     }
   156 }
   158 void gdrom_menu_build( GtkWidget *menu ) 
   159 {
   160     GSList *group = NULL;
   161     GtkWidget *empty = gtk_radio_menu_item_new_with_label( group, _("Empty") );
   162     group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(empty) );
   163     g_signal_connect_after( empty, "activate", G_CALLBACK(gdrom_menu_empty_callback), NULL );
   164     gtk_menu_shell_append( GTK_MENU_SHELL(menu), empty );
   166     GList *ptr;
   167     for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   168 	gchar *name = (gchar *)ptr->data;
   169 	GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name);
   170 	gtk_widget_set_name( item, name );
   171 	group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
   172 	g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
   173 			  name );
   174 	gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
   175     }
   177     if( gdrom_recent_list != NULL ) {
   178 	gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
   179 	for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   180 	    gchar *path = (gchar *)ptr->data;
   181 	    gchar *name = basename(path);
   182 	    GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name );
   183 	    gtk_widget_set_name( item, path );
   184 	    group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
   185 	    g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
   186 				    path );
   187 	    gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
   189 	}
   190     }
   191     gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
   192     GtkWidget *open = gtk_image_menu_item_new_with_label( _("Open image file...") );
   193     g_signal_connect_after( open, "activate", G_CALLBACK(gdrom_menu_open_image_callback), NULL );
   194     gtk_menu_shell_append( GTK_MENU_SHELL(menu), open );
   195     gtk_widget_show_all(menu);
   196 }
   198 GtkWidget *gdrom_menu_new()
   199 {
   200     GtkWidget *menu = gtk_menu_new();
   201     gtk_menu_set_title( GTK_MENU(menu), _("GD-Rom Settings") );
   203     gdrom_menu_build(menu);
   205     gdrom_menu_list = g_list_append(gdrom_menu_list, menu);
   206     gtk_widget_show_all(menu);
   207     gdrom_menu_update_all();
   208     return menu;
   209 }
   211 void gdrom_menu_rebuild_all()
   212 {
   213     GList *ptr;
   215     for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   216 	GtkWidget *menu = GTK_WIDGET(ptr->data);
   217 	GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
   218 	GList *listptr;
   219 	for( listptr = children; listptr != NULL; listptr = g_list_next(listptr) ) {
   220 	    gtk_widget_destroy( GTK_WIDGET(listptr->data) );
   221 	}
   222 	g_list_free(children);
   223 	gdrom_menu_build(menu);
   224     }
   225     gdrom_menu_update_all();
   226 }
   228 void gdrom_menu_init()
   229 {
   230     const gchar *recent = lxdream_get_config_value( CONFIG_RECENT );
   231     gdrom_device_list = gdrom_get_native_devices();
   232     if( recent != NULL ) {
   233 	gchar **list = g_strsplit(recent, ":", 5);
   234 	int i;
   235 	for( i=0; list[i] != NULL; i++ ) {
   236 	    gdrom_recent_list = g_list_append( gdrom_recent_list, g_strdup(list[i]) );
   237 	}
   238 	g_strfreev(list);
   239     }
   240 }
.