Search
lxdream.org :: lxdream/src/gtkui/gtkcb.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gtkcb.c
changeset 1124:aacaae9812ea
prev1109:700c5ab26a63
author nkeynes
date Sun Feb 12 16:30:26 2012 +1000 (10 years ago)
permissions -rw-r--r--
last change Add -Werror for mregparm check, so it actually fails if mregparm isn't
accepted
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Action callbacks from the main window
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <stdlib.h>
    21 #include "lxdream.h"
    22 #include "config.h"
    23 #include "lxpaths.h"
    24 #include "dreamcast.h"
    25 #include "gdrom/gdrom.h"
    26 #include "gtkui/gtkui.h"
    27 #include "pvr2/pvr2.h"
    28 #include "loader.h"
    31 static void add_file_pattern( GtkFileChooser *chooser, const char *pattern, const char *patname )
    32 {
    33     if( pattern != NULL ) {
    34         GtkFileFilter *filter = gtk_file_filter_new();
    35         gtk_file_filter_add_pattern( filter, pattern );
    36         gtk_file_filter_set_name( filter, patname );
    37         gtk_file_chooser_add_filter( chooser, filter );
    38         filter = gtk_file_filter_new();
    39         gtk_file_filter_set_name( filter, _("All files") );
    40         gtk_file_filter_add_pattern( filter, "*" );
    41         gtk_file_chooser_add_filter( chooser, filter );
    42     }
    43 }
    45 gchar *open_file_dialog( const char *title, const char *pattern, const char *patname,
    46                          int initial_dir_key )
    47 {
    48     GtkWidget *file;
    49     gchar *filename = NULL;
    51     file = gtk_file_chooser_dialog_new( title, NULL,
    52             GTK_FILE_CHOOSER_ACTION_OPEN,
    53             GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
    54             GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
    55             NULL );
    56     add_file_pattern( GTK_FILE_CHOOSER(file), pattern, patname );
    57     if( initial_dir_key != -1 ) {
    58         gchar *initial_path = get_absolute_path(get_gui_path(initial_dir_key));
    59         gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(file), initial_path );
    60         g_free(initial_path);
    61     }
    62     gtk_window_set_modal( GTK_WINDOW(file), TRUE );
    63     gtk_dialog_set_default_response( GTK_DIALOG(file), GTK_RESPONSE_ACCEPT );
    64     int result = gtk_dialog_run( GTK_DIALOG(file) );
    65     if( result == GTK_RESPONSE_ACCEPT ) {
    66         filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(file) );
    67         if( initial_dir_key != -1 ) {
    68             gchar *end_path = gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(file) );
    69             set_gui_path(initial_dir_key,end_path);
    70             g_free(end_path);
    71         }
    72     }
    73     gtk_widget_destroy(file);
    75     return filename;
    76 }
    78 gchar *save_file_dialog( const char *title, const char *pattern, const char *patname,
    79                          const char *mandatory_ext, int initial_dir_key )
    80 {
    81     GtkWidget *file;
    82     gchar *filename = NULL;
    84     file = gtk_file_chooser_dialog_new( title, NULL,
    85             GTK_FILE_CHOOSER_ACTION_SAVE,
    86             GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
    87             GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
    88             NULL );
    89     add_file_pattern( GTK_FILE_CHOOSER(file), pattern, patname );
    90     if( initial_dir_key != -1 ) {
    91         gchar *initial_path = get_absolute_path(get_gui_path(initial_dir_key));
    92         gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(file), initial_path );
    93         g_free(initial_path);
    94     }
    95     gtk_window_set_modal( GTK_WINDOW(file), TRUE );
    96     gtk_dialog_set_default_response( GTK_DIALOG(file), GTK_RESPONSE_ACCEPT );
    97     int result = gtk_dialog_run( GTK_DIALOG(file) );
    98     if( result == GTK_RESPONSE_ACCEPT ) {
    99         filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(file) );
   100         if( mandatory_ext ) {
   101             int extlen = strlen(mandatory_ext);
   102             int fnlen = strlen(filename);
   103             if( fnlen < extlen || strcmp(filename + (fnlen - extlen), mandatory_ext) != 0 ) {
   104                 gchar *f2 = g_strdup_printf("%s%s",filename, mandatory_ext);
   105                 g_free(filename);
   106                 filename = f2;
   107             }
   108         }
   109         if( initial_dir_key != -1 ) {
   110             gchar *end_path = gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(file) );
   111             set_gui_path(initial_dir_key,end_path);
   112             g_free(end_path);
   113         }
   114     }
   115     gtk_widget_destroy(file);
   116     return filename;
   117 }
   119 void open_file_dialog_cb( const char *title, file_callback_t action, const char *pattern, const char *patname,
   120                           int initial_dir_key )
   121 {
   122     gchar *filename = open_file_dialog( title, pattern, patname, initial_dir_key ); 
   123     if( filename != NULL ) {
   124         action( filename );
   125         g_free(filename);
   126     }
   127 }
   129 void save_file_dialog_cb( const char *title, file_callback_t action, const char *pattern, const char *patname,
   130                           const char *mandatory_ext, int initial_dir_key )
   131 {
   132     gchar *filename = save_file_dialog( title, pattern, patname, mandatory_ext, initial_dir_key );
   133     if( filename != NULL ) {
   134         action(filename);
   135         g_free(filename);
   136     }
   137 }
   139 void mount_action_callback( GtkAction *action, gpointer user_data)
   140 {
   141     open_file_dialog_cb( "Open...", gtk_gui_gdrom_mount_image, NULL, NULL, CONFIG_DEFAULT_PATH );
   142 }
   143 void reset_action_callback( GtkAction *action, gpointer user_data)
   144 {
   145     dreamcast_reset();
   146 }
   148 void pause_action_callback( GtkAction *action, gpointer user_data)
   149 {
   150     dreamcast_stop();
   151 }
   153 void resume_action_callback( GtkAction *action, gpointer user_data)
   154 {
   155     dreamcast_run();
   156 }
   158 gboolean gtk_gui_load_exec( const gchar *filename )
   159 {
   160     ERROR err;
   161     gboolean ok = file_load_exec(filename, &err);
   162     if( !ok ) {
   163         ERROR(err.msg);
   164     }
   165     return ok;
   166 }
   168 void load_binary_action_callback( GtkAction *action, gpointer user_data)
   169 {
   170     open_file_dialog_cb( "Open Binary...", gtk_gui_load_exec, NULL, NULL, CONFIG_DEFAULT_PATH );
   171 }
   173 void load_state_preview_callback( GtkFileChooser *chooser, gpointer user_data )
   174 {
   175     GtkWidget *preview = GTK_WIDGET(user_data);
   176     gchar *filename = gtk_file_chooser_get_preview_filename(chooser);
   178     frame_buffer_t data = dreamcast_load_preview(filename);
   179     if( data != NULL ) {
   180         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_frame_buffer(data);
   181         GdkPixbuf *scaled = gdk_pixbuf_scale_simple(pixbuf, 320, 240,
   182                 GDK_INTERP_BILINEAR);
   183         g_object_unref(pixbuf);
   184         gtk_image_set_from_pixbuf( GTK_IMAGE(preview), scaled );
   185         g_object_unref(scaled);
   186         gtk_widget_show(preview);
   187     } else {
   188         gtk_widget_hide(preview);
   189     }
   190 }
   192 void load_state_action_callback( GtkAction *action, gpointer user_data)
   193 {
   194     GtkWidget *file, *preview, *frame, *align;
   195     GtkRequisition size;
   196     const gchar *dir = get_gui_path(CONFIG_SAVE_PATH);
   197     gchar *path = get_absolute_path(dir);
   198     file = gtk_file_chooser_dialog_new( _("Load state..."), NULL,
   199             GTK_FILE_CHOOSER_ACTION_OPEN,
   200             GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
   201             GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
   202             NULL );
   203     add_file_pattern( GTK_FILE_CHOOSER(file), "*.dst", _("lxDream Save State (*.dst)") );
   204     gtk_object_set_data( GTK_OBJECT(file), "file_action", action );
   206     preview = gtk_image_new( );
   208     frame = gtk_frame_new(NULL);
   209     gtk_frame_set_shadow_type( GTK_FRAME(frame), GTK_SHADOW_IN );
   210     gtk_container_add( GTK_CONTAINER(frame), preview );
   211     gtk_widget_show(frame);
   212     gtk_widget_size_request(frame, &size);
   213     gtk_widget_set_size_request(frame, size.width + 320, size.height + 240);
   214     align = gtk_alignment_new(0.5, 0.5, 0, 0 );
   215     gtk_container_add( GTK_CONTAINER(align), frame );
   216     gtk_widget_show( align );
   217     gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(file), align);
   218     g_signal_connect( file, "update-preview", G_CALLBACK(load_state_preview_callback),
   219                       preview );
   220     gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(file), path );
   221     gtk_window_set_modal( GTK_WINDOW(file), TRUE );
   222     int result = gtk_dialog_run( GTK_DIALOG(file) );
   223     if( result == GTK_RESPONSE_ACCEPT ) {
   224         gchar *filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(file) );
   225         gchar *end_path = gtk_file_chooser_get_current_folder( GTK_FILE_CHOOSER(file) );
   226         set_gui_path(CONFIG_SAVE_PATH,end_path);
   227         g_free(end_path);
   228         dreamcast_load_state( filename );
   229     }
   230     gtk_widget_destroy(file);
   231     g_free(path);
   232 }
   234 void save_state_action_callback( GtkAction *action, gpointer user_data)
   235 {
   236     save_file_dialog_cb( "Save state...", dreamcast_save_state, "*.dst", _("lxDream Save State (*.dst)"), ".dst", CONFIG_SAVE_PATH );
   237 }
   239 void quick_state_action_callback( GtkRadioAction *action, GtkRadioAction *current, gpointer user_data)
   240 {
   241     gint val = gtk_radio_action_get_current_value(action);
   242     dreamcast_set_quick_state(val);
   243 }
   245 void quick_load_action_callback( GtkAction *action, gpointer user_data)
   246 {
   247     dreamcast_quick_load();
   248 }
   250 void quick_save_action_callback( GtkAction *action, gpointer user_data)
   251 {
   252     dreamcast_quick_save();
   253 }
   256 void about_action_callback( GtkAction *action, gpointer user_data)
   257 {
   259     GtkWidget *dialog = g_object_new (GTK_TYPE_ABOUT_DIALOG,
   260             "name", APP_NAME, 
   261             "version", lxdream_full_version,
   262             "copyright", lxdream_copyright,
   263             "logo-icon-name", "lxdream",
   264             NULL);
   265     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
   266     gtk_dialog_run(GTK_DIALOG(dialog));
   267     gtk_widget_destroy(dialog);
   269 }
   271 void exit_action_callback( GtkAction *action, gpointer user_data)
   272 {
   273     dreamcast_shutdown();
   274     exit(0);
   275 }
   277 void path_settings_callback( GtkAction *action, gpointer user_data)
   278 {
   279     gtk_configuration_panel_run( _("Path Settings"), lxdream_get_config_group(CONFIG_GROUP_GLOBAL) );
   280 }
   282 void audio_settings_callback( GtkAction *action, gpointer user_data)
   283 {
   284 }
   286 void maple_settings_callback( GtkAction *action, gpointer user_data)
   287 {
   288     maple_dialog_run( );
   289 }
   291 void network_settings_callback( GtkAction *action, gpointer user_data)
   292 {
   293 }
   295 void video_settings_callback( GtkAction *action, gpointer user_data)
   296 {
   297 }
   299 void hotkey_settings_callback( GtkAction *action, gpointer user_data)
   300 {
   301     gtk_configuration_panel_run( _("Hotkey Settings"), lxdream_get_config_group(CONFIG_GROUP_HOTKEYS) );
   302 }
   304 void fullscreen_toggle_callback( GtkToggleAction *action, gpointer user_data)
   305 {
   306     main_window_set_fullscreen(gtk_gui_get_main(), gtk_toggle_action_get_active(action));
   307 }
   309 void debugger_action_callback( GtkAction *action, gpointer user_data)
   310 {
   311     gtk_gui_show_debugger();
   312 }
   314 void debug_memory_action_callback( GtkAction *action, gpointer user_data)
   315 {
   316     gchar *title = g_strdup_printf( "%s :: %s", lxdream_package_name, _("Memory dump") );
   317     dump_window_new( title );
   318     g_free(title);
   319 }
   321 void debug_mmio_action_callback( GtkAction *action, gpointer user_data)
   322 {
   323     gtk_gui_show_mmio();
   324 }
   326 void save_scene_action_callback( GtkAction *action, gpointer user_data)
   327 {
   328     save_file_dialog_cb( _("Save next scene..."), pvr2_save_next_scene, "*.dsc", _("lxdream scene file (*.dsc)"), ".dsc", CONFIG_SAVE_PATH );
   329 }
   331 int debug_window_get_selected_row( debug_window_t data );
   333 void debug_step_action_callback( GtkAction *action, gpointer user_data)
   334 {
   335     debug_window_single_step(gtk_gui_get_debugger());
   336 }
   338 void debug_runto_action_callback( GtkAction *action, gpointer user_data)
   339 {
   340     debug_window_t debug = gtk_gui_get_debugger();
   341     int selected_row = debug_window_get_selected_row(debug);
   342     if( selected_row == -1 ) {
   343         WARN( _("No address selected, so can't run to it"), NULL );
   344     } else {
   345         debug_window_set_oneshot_breakpoint( debug, selected_row );
   346         dreamcast_run();
   347     }
   348 }
   350 void debug_breakpoint_action_callback( GtkAction *action, gpointer user_data)
   351 {
   352     debug_window_t debug = gtk_gui_get_debugger();
   353     int selected_row = debug_window_get_selected_row(debug);
   354     if( selected_row != -1 ) {
   355         debug_window_toggle_breakpoint( debug, selected_row );
   356     }
   357 }
.