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