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:53:35 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix miscellaneous warnings
file annotate diff log raw
nkeynes@464
     1
/**
nkeynes@479
     2
 * $Id: gdrom_menu.c,v 1.4 2007-10-31 11:22:10 nkeynes Exp $
nkeynes@464
     3
 *
nkeynes@464
     4
 * Creates and manages the GD-Rom attachment menu.
nkeynes@464
     5
 *
nkeynes@464
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@464
     7
 *
nkeynes@464
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@464
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@464
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@464
    11
 * (at your option) any later version.
nkeynes@464
    12
 *
nkeynes@464
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@464
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@464
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@464
    16
 * GNU General Public License for more details.
nkeynes@464
    17
 */
nkeynes@464
    18
nkeynes@464
    19
#include <assert.h>
nkeynes@464
    20
#include <libgen.h>
nkeynes@464
    21
nkeynes@464
    22
#include "dream.h"
nkeynes@464
    23
#include "dreamcast.h"
nkeynes@464
    24
#include "config.h"
nkeynes@464
    25
#include "gdrom/gdrom.h"
nkeynes@464
    26
#include "gui/gtkui.h"
nkeynes@464
    27
nkeynes@464
    28
#define MAX_RECENT_ITEMS 5
nkeynes@464
    29
nkeynes@464
    30
static GList *gdrom_menu_list = NULL;
nkeynes@464
    31
static gboolean gdrom_menu_adjusting = FALSE;
nkeynes@464
    32
static GList *gdrom_device_list = NULL;
nkeynes@464
    33
static GList *gdrom_recent_list = NULL;
nkeynes@464
    34
nkeynes@464
    35
void gdrom_menu_rebuild_all();
nkeynes@464
    36
nkeynes@464
    37
nkeynes@464
    38
gint gdrom_menu_find_item( const gchar *name )
nkeynes@464
    39
{
nkeynes@464
    40
    gint posn = 0;
nkeynes@464
    41
    GList *ptr;
nkeynes@464
    42
nkeynes@464
    43
    for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@464
    44
	gchar *device = (gchar *)ptr->data;
nkeynes@464
    45
	posn++;
nkeynes@464
    46
	if( strcmp(device, name) == 0 ) {
nkeynes@464
    47
	    return posn;
nkeynes@464
    48
	}
nkeynes@464
    49
    }
nkeynes@464
    50
    posn++;
nkeynes@464
    51
    for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@464
    52
	gchar *file = (gchar *)ptr->data;
nkeynes@464
    53
	posn++;
nkeynes@464
    54
	if( strcmp(file, name) == 0 ) {
nkeynes@464
    55
	    return posn;
nkeynes@464
    56
	}
nkeynes@464
    57
    }
nkeynes@464
    58
    return -1;
nkeynes@464
    59
}
nkeynes@464
    60
nkeynes@464
    61
gint gdrom_menu_add_recent_item( const gchar *name )
nkeynes@464
    62
{
nkeynes@464
    63
    gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
nkeynes@464
    64
    if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
nkeynes@464
    65
	GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
nkeynes@464
    66
	g_free( ptr->data );
nkeynes@464
    67
	gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
nkeynes@464
    68
    }
nkeynes@470
    69
nkeynes@470
    70
    GList *ptr;
nkeynes@479
    71
    int size = 0;
nkeynes@470
    72
    for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@470
    73
	size += strlen( (gchar *)ptr->data ) + 1;
nkeynes@470
    74
    }
nkeynes@470
    75
    char buf[size];
nkeynes@470
    76
    strcpy( buf, (gchar *)gdrom_recent_list->data );
nkeynes@470
    77
    ptr = g_list_next(gdrom_recent_list);
nkeynes@470
    78
    while( ptr != NULL ) {
nkeynes@470
    79
	strcat( buf, ":" );
nkeynes@470
    80
	strcat( buf, (gchar *)ptr->data );
nkeynes@470
    81
	ptr = g_list_next(ptr);
nkeynes@470
    82
    }
nkeynes@470
    83
    lxdream_set_global_config_value( CONFIG_RECENT, buf );
nkeynes@470
    84
    lxdream_save_config();
nkeynes@470
    85
nkeynes@464
    86
    return g_list_length( gdrom_device_list ) + 2; // menu posn of new item
nkeynes@464
    87
}
nkeynes@464
    88
nkeynes@464
    89
void gdrom_menu_update_all()
nkeynes@464
    90
{
nkeynes@464
    91
    gdrom_disc_t disc = gdrom_get_current_disc();
nkeynes@464
    92
    gint posn = 0;
nkeynes@464
    93
    GList *ptr;
nkeynes@464
    94
nkeynes@464
    95
    gdrom_menu_adjusting = TRUE;
nkeynes@464
    96
nkeynes@464
    97
    if( disc != NULL ) {
nkeynes@464
    98
	posn = gdrom_menu_find_item( disc->name );
nkeynes@464
    99
	if( posn == -1 ) {
nkeynes@464
   100
	    posn = gdrom_menu_add_recent_item( disc->name );
nkeynes@464
   101
	    gdrom_menu_rebuild_all();
nkeynes@464
   102
	}
nkeynes@464
   103
    }
nkeynes@464
   104
nkeynes@464
   105
    for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@464
   106
	GtkWidget *menu = GTK_WIDGET(ptr->data);
nkeynes@464
   107
	GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
nkeynes@464
   108
	GList *item = g_list_nth( children, posn );
nkeynes@464
   109
	assert( item != NULL );
nkeynes@464
   110
	gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM(item->data), TRUE );
nkeynes@464
   111
	g_list_free(children);
nkeynes@464
   112
    }    
nkeynes@464
   113
nkeynes@464
   114
    gdrom_menu_adjusting = FALSE;
nkeynes@464
   115
}
nkeynes@464
   116
nkeynes@464
   117
void gdrom_menu_empty_callback( GtkWidget *widget, gpointer user_data )
nkeynes@464
   118
{
nkeynes@464
   119
    if( !gdrom_menu_adjusting ) {
nkeynes@464
   120
	gdrom_unmount_disc();
nkeynes@464
   121
	gdrom_menu_update_all();
nkeynes@470
   122
	lxdream_set_global_config_value( CONFIG_GDROM, NULL );
nkeynes@470
   123
	lxdream_save_config();
nkeynes@464
   124
    }
nkeynes@464
   125
}
nkeynes@464
   126
nkeynes@469
   127
gboolean gdrom_menu_open_file( const char *filename )
nkeynes@464
   128
{
nkeynes@469
   129
    gboolean result = FALSE;
nkeynes@464
   130
    if( filename != NULL ) {
nkeynes@469
   131
	result = gdrom_mount_image(filename);
nkeynes@464
   132
    }
nkeynes@469
   133
    if( result ) {
nkeynes@469
   134
	gdrom_menu_update_all();
nkeynes@470
   135
	lxdream_set_global_config_value( CONFIG_GDROM, filename );
nkeynes@470
   136
	lxdream_save_config();
nkeynes@469
   137
    }
nkeynes@469
   138
    return result;
nkeynes@464
   139
}
nkeynes@464
   140
nkeynes@464
   141
void gdrom_menu_open_image_callback( GtkWidget *widget, gpointer user_data )
nkeynes@464
   142
{
nkeynes@464
   143
    if( !gdrom_menu_adjusting ) {
nkeynes@464
   144
	const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
nkeynes@464
   145
	open_file_dialog( "Open...", gdrom_menu_open_file, NULL, NULL, dir );
nkeynes@464
   146
    }
nkeynes@464
   147
}
nkeynes@464
   148
nkeynes@464
   149
nkeynes@464
   150
void gdrom_menu_open_specified_callback( GtkWidget *widget, gpointer user_data )
nkeynes@464
   151
{
nkeynes@464
   152
    if( !gdrom_menu_adjusting ) {
nkeynes@464
   153
	gdrom_menu_open_file( (gchar *)user_data );
nkeynes@464
   154
    }
nkeynes@464
   155
}
nkeynes@464
   156
nkeynes@464
   157
void gdrom_menu_build( GtkWidget *menu ) 
nkeynes@464
   158
{
nkeynes@464
   159
    GSList *group = NULL;
nkeynes@464
   160
    GtkWidget *empty = gtk_radio_menu_item_new_with_label( group, "Empty" );
nkeynes@464
   161
    group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(empty) );
nkeynes@464
   162
    g_signal_connect_after( empty, "activate", G_CALLBACK(gdrom_menu_empty_callback), NULL );
nkeynes@464
   163
    gtk_menu_shell_append( GTK_MENU_SHELL(menu), empty );
nkeynes@464
   164
    
nkeynes@464
   165
    GList *ptr;
nkeynes@464
   166
    for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@464
   167
	gchar *name = (gchar *)ptr->data;
nkeynes@464
   168
	GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name);
nkeynes@464
   169
	gtk_widget_set_name( item, name );
nkeynes@464
   170
	group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
nkeynes@464
   171
	g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
nkeynes@464
   172
			  name );
nkeynes@464
   173
	gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
nkeynes@464
   174
    }
nkeynes@464
   175
nkeynes@464
   176
    if( gdrom_recent_list != NULL ) {
nkeynes@464
   177
	gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
nkeynes@464
   178
	for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@464
   179
	    gchar *path = (gchar *)ptr->data;
nkeynes@464
   180
	    gchar *name = basename(path);
nkeynes@464
   181
	    GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name );
nkeynes@464
   182
	    gtk_widget_set_name( item, path );
nkeynes@464
   183
	    group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
nkeynes@464
   184
	    g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
nkeynes@464
   185
				    path );
nkeynes@464
   186
	    gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
nkeynes@464
   187
	    
nkeynes@464
   188
	}
nkeynes@464
   189
    }
nkeynes@464
   190
    gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
nkeynes@464
   191
    GtkWidget *open = gtk_image_menu_item_new_with_label( "Open image file..." );
nkeynes@464
   192
    g_signal_connect_after( open, "activate", G_CALLBACK(gdrom_menu_open_image_callback), NULL );
nkeynes@464
   193
    gtk_menu_shell_append( GTK_MENU_SHELL(menu), open );
nkeynes@464
   194
    gtk_widget_show_all(menu);
nkeynes@464
   195
}
nkeynes@464
   196
nkeynes@464
   197
GtkWidget *gdrom_menu_new()
nkeynes@464
   198
{
nkeynes@464
   199
    GtkWidget *menu = gtk_menu_new();
nkeynes@464
   200
    gtk_menu_set_title( GTK_MENU(menu), "GD-Rom Settings" );
nkeynes@464
   201
nkeynes@464
   202
    gdrom_menu_build(menu);
nkeynes@464
   203
nkeynes@464
   204
    gdrom_menu_list = g_list_append(gdrom_menu_list, menu);
nkeynes@464
   205
    gtk_widget_show_all(menu);
nkeynes@464
   206
    gdrom_menu_update_all();
nkeynes@464
   207
    return menu;
nkeynes@464
   208
}
nkeynes@464
   209
nkeynes@464
   210
void gdrom_menu_rebuild_all()
nkeynes@464
   211
{
nkeynes@464
   212
    GList *ptr;
nkeynes@464
   213
nkeynes@464
   214
    for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@464
   215
	GtkWidget *menu = GTK_WIDGET(ptr->data);
nkeynes@464
   216
	GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
nkeynes@464
   217
	GList *listptr;
nkeynes@464
   218
	for( listptr = children; listptr != NULL; listptr = g_list_next(listptr) ) {
nkeynes@464
   219
	    gtk_widget_destroy( GTK_WIDGET(listptr->data) );
nkeynes@464
   220
	}
nkeynes@464
   221
	g_list_free(children);
nkeynes@464
   222
	gdrom_menu_build(menu);
nkeynes@464
   223
    }
nkeynes@464
   224
    gdrom_menu_update_all();
nkeynes@464
   225
}
nkeynes@470
   226
nkeynes@470
   227
void gdrom_menu_init()
nkeynes@470
   228
{
nkeynes@470
   229
    const gchar *recent = lxdream_get_config_value( CONFIG_RECENT );
nkeynes@470
   230
    gdrom_device_list = gdrom_get_native_devices();
nkeynes@470
   231
    if( recent != NULL ) {
nkeynes@470
   232
	gchar **list = g_strsplit(recent, ":", 5);
nkeynes@470
   233
	int i;
nkeynes@470
   234
	for( i=0; list[i] != NULL; i++ ) {
nkeynes@470
   235
	    gdrom_recent_list = g_list_append( gdrom_recent_list, g_strdup(list[i]) );
nkeynes@470
   236
	}
nkeynes@470
   237
	g_strfreev(list);
nkeynes@470
   238
    }
nkeynes@470
   239
}
.