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