Made ChimaraApp class
[projects/chimara/chimara.git] / player / callbacks.c
index 38629fe875da262df3d56369001eb85b2571103b..baa8ca561795342b146fda6fdcea7f89cee822da 100644 (file)
 #include <libchimara/chimara-if.h>
 #include <config.h>
 #include "error.h"
+#include "player.h"
+#include "app.h"
 
+#if 0
 /* If a game is running in @glk, warn the user that they will quit the currently
 running game if they open a new one. Returns TRUE if no game was running.
 Returns FALSE if the user cancelled. Returns TRUE and shuts down the running
@@ -69,31 +72,86 @@ confirm_open_new_game(ChimaraGlk *glk)
        }
        return TRUE;
 }
+#endif
 
-void 
-on_open_activate(GtkAction *action, ChimaraGlk *glk) 
+/* Internal function: See if there is a corresponding graphics file. If so,
+return its path. If not, return NULL. */
+static char *
+search_for_graphics_file(const char *filename)
 {
-       GtkWindow *window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
-       
-       if(!confirm_open_new_game(glk))
-               return;
+       ChimaraApp *theapp = chimara_app_get();
+
+       /* First get the name of the story file */
+       char *scratch = g_path_get_basename(filename);
+       *(strrchr(scratch, '.')) = '\0';
+
+       /* Check in the stored resource path, if set */
+       char *resource_path;
+       g_settings_get(theapp->prefs_settings, "resource-path", "ms", &resource_path);
+
+       /* Otherwise check in the current directory */
+       if(!resource_path)
+               resource_path = g_path_get_dirname(filename);
+
+       char *blorbfile = g_strconcat(resource_path, "/", scratch, ".blb", NULL);
+       g_free(scratch);
+       g_free(resource_path);
+
+       if(g_file_test(blorbfile, G_FILE_TEST_EXISTS))
+               return blorbfile;
+
+       g_free(blorbfile);
+       return NULL;
+}
+
+void
+on_open_activate(GtkAction *action, ChimaraApp *theapp)
+{
+       //if(!confirm_open_new_game(CHIMARA_GLK(player->glk)))
+       //      return;
 
        GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Open Game"),
-           window,
+           NULL, // FIXME
            GTK_FILE_CHOOSER_ACTION_OPEN,
            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
            GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
            NULL);
+
+       /* Get last opened path */
+       gchar *path;
+       g_settings_get(theapp->state_settings, "last-open-path", "ms", &path);
+       if(path) {
+               gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), path);
+               g_free(path);
+       }
+
        if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
                GError *error = NULL;
-               gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
-               if(!chimara_if_run_game(CHIMARA_IF(glk), filename, &error)) {
-                       error_dialog(window, error, _("Could not open game file '%s': "), filename);
+               char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
+
+               /* Open a new player window */
+               ChimaraPlayer *player = CHIMARA_PLAYER(chimara_player_new());
+               gtk_widget_show_all(GTK_WIDGET(player));
+               gtk_window_present(GTK_WINDOW(player));
+
+               gchar *blorbfile = search_for_graphics_file(filename);
+               if(blorbfile) {
+                       g_object_set(player->glk, "graphics-file", blorbfile, NULL);
+                       g_free(blorbfile);
+               }
+               if(!chimara_if_run_game(CHIMARA_IF(player->glk), filename, &error)) {
+                       error_dialog(GTK_WINDOW(player), error, _("Could not open game file '%s': "), filename);
                        g_free(filename);
                        gtk_widget_destroy(dialog);
                        return;
                }
                
+               path = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog));
+               if(path) {
+                       g_settings_set(theapp->state_settings, "last-open-path", "ms", path);
+                       g_free(path);
+               }
+
                /* Add file to recent files list */
                GtkRecentManager *manager = gtk_recent_manager_get_default();
                gchar *uri;
@@ -111,119 +169,129 @@ on_open_activate(GtkAction *action, ChimaraGlk *glk)
 }
 
 void
-on_recent_item_activated(GtkRecentChooser *chooser, ChimaraGlk *glk)
+on_recent_item_activated(GtkRecentChooser *chooser, ChimaraApp *theapp)
 {
        GError *error = NULL;
-       GtkWindow *window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
        gchar *uri = gtk_recent_chooser_get_current_uri(chooser);
        gchar *filename;
        if(!(filename = g_filename_from_uri(uri, NULL, &error))) {
-               error_dialog(window, error, _("Could not open game file '%s': "), uri);
-               g_free(uri);
-               return;
+               error_dialog(NULL /* FIXME */, error, _("Could not open game file '%s': "), uri);
+               goto finally;
        }
        
-       if(!confirm_open_new_game(glk)) {
-               g_free(filename);
-               g_free(uri);
-               return;
-       }
+       //if(!confirm_open_new_game(CHIMARA_GLK(player->glk)))
+       //      goto finally2;
+
+       /* Open a new player window */
+       ChimaraPlayer *player = CHIMARA_PLAYER(chimara_player_new());
+       gtk_widget_show_all(GTK_WIDGET(player));
+       gtk_window_present(GTK_WINDOW(player));
        
-       if(!chimara_if_run_game(CHIMARA_IF(glk), filename, &error)) {
-               error_dialog(window, error, _("Could not open game file '%s': "), filename);
-               g_free(filename);
-               g_free(uri);
-               return;
+       char *blorbfile = search_for_graphics_file(filename);
+       if(blorbfile) {
+               g_object_set(player->glk, "graphics-file", blorbfile, NULL);
+               g_free(blorbfile);
+       }
+       if(!chimara_if_run_game(CHIMARA_IF(player->glk), filename, &error)) {
+               error_dialog(GTK_WINDOW(player), error, _("Could not open game file '%s': "), filename);
+               goto finally2;
        }
-       g_free(filename);
        
        /* Add file to recent files list again, this updates it to most recently used */
        GtkRecentManager *manager = gtk_recent_manager_get_default();
        if(!gtk_recent_manager_add_item(manager, uri))
                g_warning(_("Could not add URI '%s' to recent files list."), uri);
-       
+
+finally2:
+       g_free(filename);
+finally:
        g_free(uri);
 }
 
 void
-on_stop_activate(GtkAction *action, ChimaraGlk *glk)
+on_stop_activate(GtkAction *action, ChimaraPlayer *player)
 {
-       chimara_glk_stop(glk);
+       chimara_glk_stop(CHIMARA_GLK(player->glk));
 }
 
 void 
-on_quit_chimara_activate(GtkAction *action, ChimaraGlk *glk) 
+on_quit_chimara_activate(GtkAction *action, ChimaraApp *theapp)
 {
        gtk_main_quit();
 }
 
 void
-on_copy_activate(GtkAction *action, ChimaraGlk *glk)
+on_copy_activate(GtkAction *action, ChimaraPlayer *player)
 {
-       GtkWindow *toplevel = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
-       GtkWidget *focus = gtk_window_get_focus(toplevel);
+       GtkWidget *focus = gtk_window_get_focus(GTK_WINDOW(player));
        /* Call "copy clipboard" on any widget that defines it */
        if(GTK_IS_LABEL(focus) || GTK_IS_ENTRY(focus) || GTK_IS_TEXT_VIEW(focus))
                g_signal_emit_by_name(focus, "copy-clipboard");
 }
 
 void
-on_paste_activate(GtkAction *action, ChimaraGlk *glk)
+on_paste_activate(GtkAction *action, ChimaraPlayer *player)
 {
-       GtkWindow *toplevel = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
-       GtkWidget *focus = gtk_window_get_focus(toplevel);
+       GtkWidget *focus = gtk_window_get_focus(GTK_WINDOW(player));
        /* Call "paste clipboard" on any widget that defines it */
        if(GTK_IS_ENTRY(focus) || GTK_IS_TEXT_VIEW(focus))
                g_signal_emit_by_name(focus, "paste-clipboard");
 }
 
 void
-on_preferences_activate(GtkAction *action, ChimaraGlk *glk)
+on_preferences_activate(GtkAction *action, ChimaraApp *theapp)
+{
+       gtk_window_present(GTK_WINDOW(theapp->prefswindow));
+}
+
+void
+on_toolbar_toggled(GtkToggleAction *action, ChimaraPlayer *player)
 {
-       extern GtkWidget *prefswindow;
-       gtk_window_present(GTK_WINDOW(prefswindow));
+       if(gtk_toggle_action_get_active(action))
+               gtk_widget_show(player->toolbar);
+       else
+               gtk_widget_hide(player->toolbar);
 }
 
 void
-on_undo_activate(GtkAction *action, ChimaraGlk *glk)
+on_undo_activate(GtkAction *action, ChimaraPlayer *player)
 {
-       chimara_glk_feed_line_input(glk, "undo");
+       chimara_glk_feed_line_input(CHIMARA_GLK(player->glk), "undo");
 }
 
 void 
-on_save_activate(GtkAction *action, ChimaraGlk *glk) 
+on_save_activate(GtkAction *action, ChimaraPlayer *player)
 {
-       chimara_glk_feed_line_input(glk, "save");
+       chimara_glk_feed_line_input(CHIMARA_GLK(player->glk), "save");
 }
 
 void 
-on_restore_activate(GtkAction *action, ChimaraGlk *glk) 
+on_restore_activate(GtkAction *action, ChimaraPlayer *player)
 {
-       chimara_glk_feed_line_input(glk, "restore");
+       chimara_glk_feed_line_input(CHIMARA_GLK(player->glk), "restore");
 }
 
 void 
-on_restart_activate(GtkAction *action, ChimaraGlk *glk) 
+on_restart_activate(GtkAction *action, ChimaraPlayer *player)
 {
-       chimara_glk_feed_line_input(glk, "restart");
+       chimara_glk_feed_line_input(CHIMARA_GLK(player->glk), "restart");
 }
 
 void 
-on_quit_activate(GtkAction *action, ChimaraGlk *glk) 
+on_quit_activate(GtkAction *action, ChimaraPlayer *player)
 {
-       chimara_glk_feed_line_input(glk, "quit");
+       chimara_glk_feed_line_input(CHIMARA_GLK(player->glk), "quit");
 }
 
 void
-on_about_activate(GtkAction *action, ChimaraGlk *glk)
+on_about_activate(GtkAction *action, ChimaraApp *theapp)
 {
-       extern GtkWidget *aboutwindow;
-       gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(aboutwindow), PACKAGE_VERSION);
-       gtk_window_present(GTK_WINDOW(aboutwindow));
+       gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(theapp->aboutwindow), PACKAGE_VERSION);
+       gtk_window_present(GTK_WINDOW(theapp->aboutwindow));
 }
 
 gboolean 
-on_window_delete_event(GtkWidget *widget, GdkEvent *event, ChimaraGlk *glk
+on_window_delete_event(GtkWidget *widget, GdkEvent *event, ChimaraPlayer *player
 {
        gtk_main_quit();
        return TRUE;