Search
lxdream.org :: lxdream/src/gtkui/gdrom_menu.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gdrom_menu.c
changeset 537:d924be49e192
prev508:ccd2c10edfe6
next561:533f6b478071
author nkeynes
date Tue Nov 20 10:27:58 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Move gtk UI into gtkui subdir (prep for non-gtk builds), and protect with
an automake conditional
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/gtkui/gdrom_menu.c Tue Nov 20 10:27:58 2007 +0000
1.3 @@ -0,0 +1,240 @@
1.4 +/**
1.5 + * $Id: gdrom_menu.c,v 1.5 2007-11-10 04:45:29 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 +#include <glib/gi18n.h>
1.25 +
1.26 +#include "dream.h"
1.27 +#include "dreamcast.h"
1.28 +#include "config.h"
1.29 +#include "gdrom/gdrom.h"
1.30 +#include "gtkui/gtkui.h"
1.31 +
1.32 +#define MAX_RECENT_ITEMS 5
1.33 +
1.34 +static GList *gdrom_menu_list = NULL;
1.35 +static gboolean gdrom_menu_adjusting = FALSE;
1.36 +static GList *gdrom_device_list = NULL;
1.37 +static GList *gdrom_recent_list = NULL;
1.38 +
1.39 +void gdrom_menu_rebuild_all();
1.40 +
1.41 +
1.42 +gint gdrom_menu_find_item( const gchar *name )
1.43 +{
1.44 + gint posn = 0;
1.45 + GList *ptr;
1.46 +
1.47 + for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.48 + gchar *device = (gchar *)ptr->data;
1.49 + posn++;
1.50 + if( strcmp(device, name) == 0 ) {
1.51 + return posn;
1.52 + }
1.53 + }
1.54 + posn++;
1.55 + for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.56 + gchar *file = (gchar *)ptr->data;
1.57 + posn++;
1.58 + if( strcmp(file, name) == 0 ) {
1.59 + return posn;
1.60 + }
1.61 + }
1.62 + return -1;
1.63 +}
1.64 +
1.65 +gint gdrom_menu_add_recent_item( const gchar *name )
1.66 +{
1.67 + gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
1.68 + if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
1.69 + GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
1.70 + g_free( ptr->data );
1.71 + gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
1.72 + }
1.73 +
1.74 + GList *ptr;
1.75 + int size = 0;
1.76 + for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.77 + size += strlen( (gchar *)ptr->data ) + 1;
1.78 + }
1.79 + char buf[size];
1.80 + strcpy( buf, (gchar *)gdrom_recent_list->data );
1.81 + ptr = g_list_next(gdrom_recent_list);
1.82 + while( ptr != NULL ) {
1.83 + strcat( buf, ":" );
1.84 + strcat( buf, (gchar *)ptr->data );
1.85 + ptr = g_list_next(ptr);
1.86 + }
1.87 + lxdream_set_global_config_value( CONFIG_RECENT, buf );
1.88 + lxdream_save_config();
1.89 +
1.90 + return g_list_length( gdrom_device_list ) + 2; // menu posn of new item
1.91 +}
1.92 +
1.93 +void gdrom_menu_update_all()
1.94 +{
1.95 + gdrom_disc_t disc = gdrom_get_current_disc();
1.96 + gint posn = 0;
1.97 + GList *ptr;
1.98 +
1.99 + gdrom_menu_adjusting = TRUE;
1.100 +
1.101 + if( disc != NULL ) {
1.102 + posn = gdrom_menu_find_item( disc->name );
1.103 + if( posn == -1 ) {
1.104 + posn = gdrom_menu_add_recent_item( disc->name );
1.105 + gdrom_menu_rebuild_all();
1.106 + }
1.107 + }
1.108 +
1.109 + for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.110 + GtkWidget *menu = GTK_WIDGET(ptr->data);
1.111 + GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
1.112 + GList *item = g_list_nth( children, posn );
1.113 + assert( item != NULL );
1.114 + gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM(item->data), TRUE );
1.115 + g_list_free(children);
1.116 + }
1.117 +
1.118 + gdrom_menu_adjusting = FALSE;
1.119 +}
1.120 +
1.121 +void gdrom_menu_empty_callback( GtkWidget *widget, gpointer user_data )
1.122 +{
1.123 + if( !gdrom_menu_adjusting ) {
1.124 + gdrom_unmount_disc();
1.125 + gdrom_menu_update_all();
1.126 + lxdream_set_global_config_value( CONFIG_GDROM, NULL );
1.127 + lxdream_save_config();
1.128 + }
1.129 +}
1.130 +
1.131 +gboolean gdrom_menu_open_file( const char *filename )
1.132 +{
1.133 + gboolean result = FALSE;
1.134 + if( filename != NULL ) {
1.135 + result = gdrom_mount_image(filename);
1.136 + }
1.137 + if( result ) {
1.138 + gdrom_menu_update_all();
1.139 + lxdream_set_global_config_value( CONFIG_GDROM, filename );
1.140 + lxdream_save_config();
1.141 + }
1.142 + return result;
1.143 +}
1.144 +
1.145 +void gdrom_menu_open_image_callback( GtkWidget *widget, gpointer user_data )
1.146 +{
1.147 + if( !gdrom_menu_adjusting ) {
1.148 + const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
1.149 + open_file_dialog( _("Open..."), gdrom_menu_open_file, NULL, NULL, dir );
1.150 + }
1.151 +}
1.152 +
1.153 +
1.154 +void gdrom_menu_open_specified_callback( GtkWidget *widget, gpointer user_data )
1.155 +{
1.156 + if( !gdrom_menu_adjusting ) {
1.157 + gdrom_menu_open_file( (gchar *)user_data );
1.158 + }
1.159 +}
1.160 +
1.161 +void gdrom_menu_build( GtkWidget *menu )
1.162 +{
1.163 + GSList *group = NULL;
1.164 + GtkWidget *empty = gtk_radio_menu_item_new_with_label( group, _("Empty") );
1.165 + group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(empty) );
1.166 + g_signal_connect_after( empty, "activate", G_CALLBACK(gdrom_menu_empty_callback), NULL );
1.167 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), empty );
1.168 +
1.169 + GList *ptr;
1.170 + for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.171 + gchar *name = (gchar *)ptr->data;
1.172 + GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name);
1.173 + gtk_widget_set_name( item, name );
1.174 + group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
1.175 + g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
1.176 + name );
1.177 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
1.178 + }
1.179 +
1.180 + if( gdrom_recent_list != NULL ) {
1.181 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
1.182 + for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.183 + gchar *path = (gchar *)ptr->data;
1.184 + gchar *name = basename(path);
1.185 + GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name );
1.186 + gtk_widget_set_name( item, path );
1.187 + group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
1.188 + g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
1.189 + path );
1.190 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
1.191 +
1.192 + }
1.193 + }
1.194 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
1.195 + GtkWidget *open = gtk_image_menu_item_new_with_label( _("Open image file...") );
1.196 + g_signal_connect_after( open, "activate", G_CALLBACK(gdrom_menu_open_image_callback), NULL );
1.197 + gtk_menu_shell_append( GTK_MENU_SHELL(menu), open );
1.198 + gtk_widget_show_all(menu);
1.199 +}
1.200 +
1.201 +GtkWidget *gdrom_menu_new()
1.202 +{
1.203 + GtkWidget *menu = gtk_menu_new();
1.204 + gtk_menu_set_title( GTK_MENU(menu), _("GD-Rom Settings") );
1.205 +
1.206 + gdrom_menu_build(menu);
1.207 +
1.208 + gdrom_menu_list = g_list_append(gdrom_menu_list, menu);
1.209 + gtk_widget_show_all(menu);
1.210 + gdrom_menu_update_all();
1.211 + return menu;
1.212 +}
1.213 +
1.214 +void gdrom_menu_rebuild_all()
1.215 +{
1.216 + GList *ptr;
1.217 +
1.218 + for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.219 + GtkWidget *menu = GTK_WIDGET(ptr->data);
1.220 + GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
1.221 + GList *listptr;
1.222 + for( listptr = children; listptr != NULL; listptr = g_list_next(listptr) ) {
1.223 + gtk_widget_destroy( GTK_WIDGET(listptr->data) );
1.224 + }
1.225 + g_list_free(children);
1.226 + gdrom_menu_build(menu);
1.227 + }
1.228 + gdrom_menu_update_all();
1.229 +}
1.230 +
1.231 +void gdrom_menu_init()
1.232 +{
1.233 + const gchar *recent = lxdream_get_config_value( CONFIG_RECENT );
1.234 + gdrom_device_list = gdrom_get_native_devices();
1.235 + if( recent != NULL ) {
1.236 + gchar **list = g_strsplit(recent, ":", 5);
1.237 + int i;
1.238 + for( i=0; list[i] != NULL; i++ ) {
1.239 + gdrom_recent_list = g_list_append( gdrom_recent_list, g_strdup(list[i]) );
1.240 + }
1.241 + g_strfreev(list);
1.242 + }
1.243 +}
.