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 464:8e099fad42a6
next469:3e2a0fde5cd8
author nkeynes
date Sat Oct 27 05:44:54 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Initial gdrom attachment menu
view annotate diff log raw
     1 /**
     2  * $Id: gdrom_menu.c,v 1.1 2007-10-27 05:44:54 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     }
    69     return g_list_length( gdrom_device_list ) + 2; // menu posn of new item
    70 }
    72 void gdrom_menu_update_all()
    73 {
    74     gdrom_disc_t disc = gdrom_get_current_disc();
    75     gint posn = 0;
    76     GList *ptr;
    78     gdrom_menu_adjusting = TRUE;
    80     if( disc != NULL ) {
    81 	posn = gdrom_menu_find_item( disc->name );
    82 	if( posn == -1 ) {
    83 	    posn = gdrom_menu_add_recent_item( disc->name );
    84 	    gdrom_menu_rebuild_all();
    85 	}
    86     }
    88     for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    89 	GtkWidget *menu = GTK_WIDGET(ptr->data);
    90 	GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
    91 	GList *item = g_list_nth( children, posn );
    92 	assert( item != NULL );
    93 	gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM(item->data), TRUE );
    94 	g_list_free(children);
    95     }    
    97     gdrom_menu_adjusting = FALSE;
    98 }
   100 void gdrom_menu_empty_callback( GtkWidget *widget, gpointer user_data )
   101 {
   102     if( !gdrom_menu_adjusting ) {
   103 	gdrom_unmount_disc();
   104 	gdrom_menu_update_all();
   105     }
   106 }
   108 void gdrom_menu_open_file( const char *filename )
   109 {
   110     if( filename != NULL ) {
   111 	gdrom_mount_image(filename);
   112     }
   113     gdrom_menu_update_all();
   114 }
   116 void gdrom_menu_open_image_callback( GtkWidget *widget, gpointer user_data )
   117 {
   118     if( !gdrom_menu_adjusting ) {
   119 	const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
   120 	open_file_dialog( "Open...", gdrom_menu_open_file, NULL, NULL, dir );
   121     }
   122 }
   125 void gdrom_menu_open_specified_callback( GtkWidget *widget, gpointer user_data )
   126 {
   127     if( !gdrom_menu_adjusting ) {
   128 	gdrom_menu_open_file( (gchar *)user_data );
   129     }
   130 }
   132 void gdrom_menu_build( GtkWidget *menu ) 
   133 {
   134     GSList *group = NULL;
   135     GtkWidget *empty = gtk_radio_menu_item_new_with_label( group, "Empty" );
   136     group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(empty) );
   137     g_signal_connect_after( empty, "activate", G_CALLBACK(gdrom_menu_empty_callback), NULL );
   138     gtk_menu_shell_append( GTK_MENU_SHELL(menu), empty );
   140     if( gdrom_device_list == NULL ) {
   141 	gdrom_device_list = gdrom_get_native_devices();
   142     }
   143     GList *ptr;
   144     for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   145 	gchar *name = (gchar *)ptr->data;
   146 	GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name);
   147 	gtk_widget_set_name( item, name );
   148 	group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
   149 	g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
   150 			  name );
   151 	gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
   152     }
   154     if( gdrom_recent_list != NULL ) {
   155 	gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
   156 	for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   157 	    gchar *path = (gchar *)ptr->data;
   158 	    gchar *name = basename(path);
   159 	    GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name );
   160 	    gtk_widget_set_name( item, path );
   161 	    group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
   162 	    g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
   163 				    path );
   164 	    gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
   166 	}
   167     }
   168     gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
   169     GtkWidget *open = gtk_image_menu_item_new_with_label( "Open image file..." );
   170     g_signal_connect_after( open, "activate", G_CALLBACK(gdrom_menu_open_image_callback), NULL );
   171     gtk_menu_shell_append( GTK_MENU_SHELL(menu), open );
   172     gtk_widget_show_all(menu);
   173 }
   175 GtkWidget *gdrom_menu_new()
   176 {
   177     GtkWidget *menu = gtk_menu_new();
   178     gtk_menu_set_title( GTK_MENU(menu), "GD-Rom Settings" );
   180     gdrom_menu_build(menu);
   182     gdrom_menu_list = g_list_append(gdrom_menu_list, menu);
   183     gtk_widget_show_all(menu);
   184     gdrom_menu_update_all();
   185     return menu;
   186 }
   188 void gdrom_menu_rebuild_all()
   189 {
   190     GList *ptr;
   192     for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   193 	GtkWidget *menu = GTK_WIDGET(ptr->data);
   194 	GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
   195 	GList *listptr;
   196 	for( listptr = children; listptr != NULL; listptr = g_list_next(listptr) ) {
   197 	    gtk_widget_destroy( GTK_WIDGET(listptr->data) );
   198 	}
   199 	g_list_free(children);
   200 	gdrom_menu_build(menu);
   201     }
   202     gdrom_menu_update_all();
   203 }
.