Search
lxdream.org :: lxdream/src/gui/gdrom_menu.c :: diff
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
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/gui/gdrom_menu.c Sat Oct 27 05:44:54 2007 +0000
1.3 @@ -0,0 +1,203 @@
1.4 +/**
1.5 + * $Id: gdrom_menu.c,v 1.1 2007-10-27 05:44:54 nkeynes Exp $
1.6 + *
1.7 + * Creates and manages the GD-Rom attachment menu.
1.8 + *
1.9 + * Copyright (c) 2005 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include <assert.h>
1.23 +#include <libgen.h>
1.24 +
1.25 +#include "dream.h"
1.26 +#include "dreamcast.h"
1.27 +#include "config.h"
1.28 +#include "gdrom/gdrom.h"
1.29 +#include "gui/gtkui.h"
1.30 +
1.31 +#define MAX_RECENT_ITEMS 5
1.32 +
1.33 +static GList *gdrom_menu_list = NULL;
1.34 +static gboolean gdrom_menu_adjusting = FALSE;
1.35 +static GList *gdrom_device_list = NULL;
1.36 +static GList *gdrom_recent_list = NULL;
1.37 +
1.38 +void gdrom_menu_rebuild_all();
1.39 +
1.40 +
1.41 +gint gdrom_menu_find_item( const gchar *name )
1.42 +{
1.43 + gint posn = 0;
1.44 + GList *ptr;
1.45 +
1.46 + for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.47 + gchar *device = (gchar *)ptr->data;
1.48 + posn++;
1.49 + if( strcmp(device, name) == 0 ) {
1.50 + return posn;
1.51 + }
1.52 + }
1.53 + posn++;
1.54 + for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.55 + gchar *file = (gchar *)ptr->data;
1.56 + posn++;
1.57 + if( strcmp(file, name) == 0 ) {
1.58 + return posn;
1.59 + }
1.60 + }
1.61 + return -1;
1.62 +}
1.63 +
1.64 +gint gdrom_menu_add_recent_item( const gchar *name )
1.65 +{
1.66 + gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
1.67 + if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
1.68 + GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
1.69 + g_free( ptr->data );
1.70 + gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
1.71 + }
1.72 + return g_list_length( gdrom_device_list ) + 2; // menu posn of new item
1.73 +}
1.74 +
1.75 +void gdrom_menu_update_all()
1.76 +{
1.77 + gdrom_disc_t disc = gdrom_get_current_disc();
1.78 + gint posn = 0;
1.79 + GList *ptr;
1.80 +
1.81 + gdrom_menu_adjusting = TRUE;
1.82 +
1.83 + if( disc != NULL ) {
1.84 + posn = gdrom_menu_find_item( disc->name );
1.85 + if( posn == -1 ) {
1.86 + posn = gdrom_menu_add_recent_item( disc->name );
1.87 + gdrom_menu_rebuild_all();
1.88 + }
1.89 + }
1.90 +
1.91 + for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.92 + GtkWidget *menu = GTK_WIDGET(ptr->data);
1.93 + GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
1.94 + GList *item = g_list_nth( children, posn );
1.95 + assert( item != NULL );
1.96 + gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM(item->data), TRUE );
1.97 + g_list_free(children);
1.98 + }
1.99 +
1.100 + gdrom_menu_adjusting = FALSE;
1.101 +}
1.102 +
1.103 +void gdrom_menu_empty_callback( GtkWidget *widget, gpointer user_data )
1.104 +{
1.105 + if( !gdrom_menu_adjusting ) {
1.106 + gdrom_unmount_disc();
1.107 + gdrom_menu_update_all();
1.108 + }
1.109 +}
1.110 +
1.111 +void gdrom_menu_open_file( const char *filename )
1.112 +{
1.113 + if( filename != NULL ) {
1.114 + gdrom_mount_image(filename);
1.115 + }
1.116 + gdrom_menu_update_all();
1.117 +}
1.118 +
1.119 +void gdrom_menu_open_image_callback( GtkWidget *widget, gpointer user_data )
1.120 +{
1.121 + if( !gdrom_menu_adjusting ) {
1.122 + const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
1.123 + open_file_dialog( "Open...", gdrom_menu_open_file, NULL, NULL, dir );
1.124 + }
1.125 +}
1.126 +
1.127 +
1.128 +void gdrom_menu_open_specified_callback( GtkWidget *widget, gpointer user_data )
1.129 +{
1.130 + if( !gdrom_menu_adjusting ) {
1.131 + gdrom_menu_open_file( (gchar *)user_data );
1.132 + }
1.133 +}
1.134 +
1.135 +void gdrom_menu_build( GtkWidget *menu )
1.136 +{
1.137 + GSList *group = NULL;
1.138 + GtkWidget *empty = gtk_radio_menu_item_new_with_label( group, "Empty" );
1.139 + group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(empty) );
1.140 + g_signal_connect_after( empty, "activate", G_CALLBACK(gdrom_menu_empty_callback), NULL );
1.141 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), empty );
1.142 +
1.143 + if( gdrom_device_list == NULL ) {
1.144 + gdrom_device_list = gdrom_get_native_devices();
1.145 + }
1.146 + GList *ptr;
1.147 + for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.148 + gchar *name = (gchar *)ptr->data;
1.149 + GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name);
1.150 + gtk_widget_set_name( item, name );
1.151 + group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
1.152 + g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
1.153 + name );
1.154 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
1.155 + }
1.156 +
1.157 + if( gdrom_recent_list != NULL ) {
1.158 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
1.159 + for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.160 + gchar *path = (gchar *)ptr->data;
1.161 + gchar *name = basename(path);
1.162 + GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name );
1.163 + gtk_widget_set_name( item, path );
1.164 + group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
1.165 + g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
1.166 + path );
1.167 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
1.168 +
1.169 + }
1.170 + }
1.171 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
1.172 + GtkWidget *open = gtk_image_menu_item_new_with_label( "Open image file..." );
1.173 + g_signal_connect_after( open, "activate", G_CALLBACK(gdrom_menu_open_image_callback), NULL );
1.174 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), open );
1.175 + gtk_widget_show_all(menu);
1.176 +}
1.177 +
1.178 +GtkWidget *gdrom_menu_new()
1.179 +{
1.180 + GtkWidget *menu = gtk_menu_new();
1.181 + gtk_menu_set_title( GTK_MENU(menu), "GD-Rom Settings" );
1.182 +
1.183 + gdrom_menu_build(menu);
1.184 +
1.185 + gdrom_menu_list = g_list_append(gdrom_menu_list, menu);
1.186 + gtk_widget_show_all(menu);
1.187 + gdrom_menu_update_all();
1.188 + return menu;
1.189 +}
1.190 +
1.191 +void gdrom_menu_rebuild_all()
1.192 +{
1.193 + GList *ptr;
1.194 +
1.195 + for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.196 + GtkWidget *menu = GTK_WIDGET(ptr->data);
1.197 + GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
1.198 + GList *listptr;
1.199 + for( listptr = children; listptr != NULL; listptr = g_list_next(listptr) ) {
1.200 + gtk_widget_destroy( GTK_WIDGET(listptr->data) );
1.201 + }
1.202 + g_list_free(children);
1.203 + gdrom_menu_build(menu);
1.204 + }
1.205 + gdrom_menu_update_all();
1.206 +}
.