Search
lxdream.org :: lxdream/src/gui/gdrom_menu.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gui/gdrom_menu.c
changeset 479:e030b18d2744
prev470:e09a16196693
next508:ccd2c10edfe6
author nkeynes
date Wed Oct 31 11:22:10 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix uninitialized value
view annotate diff log raw
     1 /**
     2  * $Id: gdrom_menu.c,v 1.4 2007-10-31 11:22:10 nkeynes Exp $
     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>
    22 #include "dream.h"
    23 #include "dreamcast.h"
    24 #include "config.h"
    25 #include "gdrom/gdrom.h"
    26 #include "gui/gtkui.h"
    28 #define MAX_RECENT_ITEMS 5
    30 static GList *gdrom_menu_list = NULL;
    31 static gboolean gdrom_menu_adjusting = FALSE;
    32 static GList *gdrom_device_list = NULL;
    33 static GList *gdrom_recent_list = NULL;
    35 void gdrom_menu_rebuild_all();
    38 gint gdrom_menu_find_item( const gchar *name )
    39 {
    40     gint posn = 0;
    41     GList *ptr;
    43     for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    44 	gchar *device = (gchar *)ptr->data;
    45 	posn++;
    46 	if( strcmp(device, name) == 0 ) {
    47 	    return posn;
    48 	}
    49     }
    50     posn++;
    51     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    52 	gchar *file = (gchar *)ptr->data;
    53 	posn++;
    54 	if( strcmp(file, name) == 0 ) {
    55 	    return posn;
    56 	}
    57     }
    58     return -1;
    59 }
    61 gint gdrom_menu_add_recent_item( const gchar *name )
    62 {
    63     gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
    64     if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
    65 	GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
    66 	g_free( ptr->data );
    67 	gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
    68     }
    70     GList *ptr;
    71     int size = 0;
    72     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    73 	size += strlen( (gchar *)ptr->data ) + 1;
    74     }
    75     char buf[size];
    76     strcpy( buf, (gchar *)gdrom_recent_list->data );
    77     ptr = g_list_next(gdrom_recent_list);
    78     while( ptr != NULL ) {
    79 	strcat( buf, ":" );
    80 	strcat( buf, (gchar *)ptr->data );
    81 	ptr = g_list_next(ptr);
    82     }
    83     lxdream_set_global_config_value( CONFIG_RECENT, buf );
    84     lxdream_save_config();
    86     return g_list_length( gdrom_device_list ) + 2; // menu posn of new item
    87 }
    89 void gdrom_menu_update_all()
    90 {
    91     gdrom_disc_t disc = gdrom_get_current_disc();
    92     gint posn = 0;
    93     GList *ptr;
    95     gdrom_menu_adjusting = TRUE;
    97     if( disc != NULL ) {
    98 	posn = gdrom_menu_find_item( disc->name );
    99 	if( posn == -1 ) {
   100 	    posn = gdrom_menu_add_recent_item( disc->name );
   101 	    gdrom_menu_rebuild_all();
   102 	}
   103     }
   105     for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   106 	GtkWidget *menu = GTK_WIDGET(ptr->data);
   107 	GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
   108 	GList *item = g_list_nth( children, posn );
   109 	assert( item != NULL );
   110 	gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM(item->data), TRUE );
   111 	g_list_free(children);
   112     }    
   114     gdrom_menu_adjusting = FALSE;
   115 }
   117 void gdrom_menu_empty_callback( GtkWidget *widget, gpointer user_data )
   118 {
   119     if( !gdrom_menu_adjusting ) {
   120 	gdrom_unmount_disc();
   121 	gdrom_menu_update_all();
   122 	lxdream_set_global_config_value( CONFIG_GDROM, NULL );
   123 	lxdream_save_config();
   124     }
   125 }
   127 gboolean gdrom_menu_open_file( const char *filename )
   128 {
   129     gboolean result = FALSE;
   130     if( filename != NULL ) {
   131 	result = gdrom_mount_image(filename);
   132     }
   133     if( result ) {
   134 	gdrom_menu_update_all();
   135 	lxdream_set_global_config_value( CONFIG_GDROM, filename );
   136 	lxdream_save_config();
   137     }
   138     return result;
   139 }
   141 void gdrom_menu_open_image_callback( GtkWidget *widget, gpointer user_data )
   142 {
   143     if( !gdrom_menu_adjusting ) {
   144 	const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
   145 	open_file_dialog( "Open...", gdrom_menu_open_file, NULL, NULL, dir );
   146     }
   147 }
   150 void gdrom_menu_open_specified_callback( GtkWidget *widget, gpointer user_data )
   151 {
   152     if( !gdrom_menu_adjusting ) {
   153 	gdrom_menu_open_file( (gchar *)user_data );
   154     }
   155 }
   157 void gdrom_menu_build( GtkWidget *menu ) 
   158 {
   159     GSList *group = NULL;
   160     GtkWidget *empty = gtk_radio_menu_item_new_with_label( group, "Empty" );
   161     group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(empty) );
   162     g_signal_connect_after( empty, "activate", G_CALLBACK(gdrom_menu_empty_callback), NULL );
   163     gtk_menu_shell_append( GTK_MENU_SHELL(menu), empty );
   165     GList *ptr;
   166     for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   167 	gchar *name = (gchar *)ptr->data;
   168 	GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name);
   169 	gtk_widget_set_name( item, name );
   170 	group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
   171 	g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
   172 			  name );
   173 	gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
   174     }
   176     if( gdrom_recent_list != NULL ) {
   177 	gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
   178 	for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   179 	    gchar *path = (gchar *)ptr->data;
   180 	    gchar *name = basename(path);
   181 	    GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name );
   182 	    gtk_widget_set_name( item, path );
   183 	    group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
   184 	    g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
   185 				    path );
   186 	    gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
   188 	}
   189     }
   190     gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
   191     GtkWidget *open = gtk_image_menu_item_new_with_label( "Open image file..." );
   192     g_signal_connect_after( open, "activate", G_CALLBACK(gdrom_menu_open_image_callback), NULL );
   193     gtk_menu_shell_append( GTK_MENU_SHELL(menu), open );
   194     gtk_widget_show_all(menu);
   195 }
   197 GtkWidget *gdrom_menu_new()
   198 {
   199     GtkWidget *menu = gtk_menu_new();
   200     gtk_menu_set_title( GTK_MENU(menu), "GD-Rom Settings" );
   202     gdrom_menu_build(menu);
   204     gdrom_menu_list = g_list_append(gdrom_menu_list, menu);
   205     gtk_widget_show_all(menu);
   206     gdrom_menu_update_all();
   207     return menu;
   208 }
   210 void gdrom_menu_rebuild_all()
   211 {
   212     GList *ptr;
   214     for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   215 	GtkWidget *menu = GTK_WIDGET(ptr->data);
   216 	GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
   217 	GList *listptr;
   218 	for( listptr = children; listptr != NULL; listptr = g_list_next(listptr) ) {
   219 	    gtk_widget_destroy( GTK_WIDGET(listptr->data) );
   220 	}
   221 	g_list_free(children);
   222 	gdrom_menu_build(menu);
   223     }
   224     gdrom_menu_update_all();
   225 }
   227 void gdrom_menu_init()
   228 {
   229     const gchar *recent = lxdream_get_config_value( CONFIG_RECENT );
   230     gdrom_device_list = gdrom_get_native_devices();
   231     if( recent != NULL ) {
   232 	gchar **list = g_strsplit(recent, ":", 5);
   233 	int i;
   234 	for( i=0; list[i] != NULL; i++ ) {
   235 	    gdrom_recent_list = g_list_append( gdrom_recent_list, g_strdup(list[i]) );
   236 	}
   237 	g_strfreev(list);
   238     }
   239 }
.