General code cleanup
[projects/chimara/chimara.git] / player / player.c
index a35423bd978665ebc547f670273f01a77d36aaa4..511449ba0519fdbe4319e241bdbf9fe72ef0fa5e 100644 (file)
@@ -1,9 +1,44 @@
+/*
+ * Copyright (C) 2008, 2009, 2010, 2011 Philip Chimento and Marijn van Vliet.
+ * All rights reserved.
+ *
+ * Chimara is free software copyrighted by Philip Chimento and Marijn van Vliet.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ * 3. Neither of the names Philip Chimento or Marijn van Vliet, nor the name of
+ *    any other contributor may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <glib.h>
 #include <glib-object.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
 #include <libchimara/chimara-glk.h>
 #include <libchimara/chimara-if.h>
 #include "player.h"
-#include "error.h"
 #include "app.h"
+#include "error.h"
+#include "util.h"
 
 typedef struct _ChimaraPlayerPrivate {
        int dummy;
@@ -14,17 +49,6 @@ typedef struct _ChimaraPlayerPrivate {
 
 G_DEFINE_TYPE(ChimaraPlayer, chimara_player, GTK_TYPE_WINDOW);
 
-static GObject *
-load_object(GtkBuilder *builder, const gchar *name)
-{
-       GObject *retval;
-       if( (retval = gtk_builder_get_object(builder, name)) == NULL) {
-               error_dialog(NULL, NULL, "Error while getting object '%s'", name);
-               g_error("Error while getting object '%s'", name);
-       }
-       return retval;
-}
-
 static void
 change_window_title(ChimaraGlk *glk, GParamSpec *pspec, GtkWindow *window)
 {
@@ -92,77 +116,48 @@ static void
 chimara_player_init(ChimaraPlayer *self)
 {      
        GError *error = NULL;
-       
-       GtkBuilder *builder = gtk_builder_new();
+       ChimaraApp *theapp = chimara_app_get();
+
+       /* Set parent properties */
+       g_object_set(self,
+               "title", _("Chimara"),
+               "default-width", 600,
+               "default-height", 800,
+               NULL);
+
+       /* Construct user interface */
        char *object_ids[] = {
                "actiongroup",
-               "vbox",
+               "player-vbox",
                NULL
        };
-       
-       if( !gtk_builder_add_objects_from_file(builder, PACKAGE_DATA_DIR "/chimara.ui", object_ids, &error) ) {
-#ifdef DEBUG
-               g_error_free(error);
-               error = NULL;
-               if( !gtk_builder_add_objects_from_file(builder, PACKAGE_SRC_DIR "/chimara.ui", object_ids, &error) ) {
-#endif /* DEBUG */
-                       error_dialog(NULL, error, "Error while building interface: ");  
-                       return;
-#ifdef DEBUG
-               }
-#endif /* DEBUG */
-       }
-       
+       GtkBuilder *builder = new_builder_with_objects(object_ids);
+
        GtkActionGroup *actiongroup = GTK_ACTION_GROUP(load_object(builder, "actiongroup"));
-       
+
        /* Set the default value of the "View/Toolbar" menu item upon creation of a
         new window to the "show-toolbar-default" setting, but bind the setting
         one-way only - we don't want toolbars to disappear suddenly */
        GtkToggleAction *toolbar_action = GTK_TOGGLE_ACTION(load_object(builder, "toolbar"));
-       //gtk_toggle_action_set_active(toolbar_action, g_settings_get_boolean(state_settings, "show-toolbar-default"));
-       //g_settings_bind(state_settings, "show-toolbar-default", toolbar_action, "active", G_SETTINGS_BIND_SET);
-               
-       GtkUIManager *uimanager = gtk_ui_manager_new();
-       if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_DATA_DIR "/chimara.menus", &error) ) {
-#ifdef DEBUG
-               g_error_free(error);
-               error = NULL;
-               if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_SRC_DIR "/chimara.menus", &error) ) {
-#endif /* DEBUG */
-                       error_dialog(NULL, error, "Error while building interface: ");
-                       return;
-#ifdef DEBUG
-               }
-#endif /* DEBUG */
-       }
-       
+       gtk_toggle_action_set_active(toolbar_action, g_settings_get_boolean(theapp->state_settings, "show-toolbar-default"));
+       g_settings_bind(theapp->state_settings, "show-toolbar-default", toolbar_action, "active", G_SETTINGS_BIND_SET);
+
        self->glk = chimara_if_new();
        g_object_set(self->glk,
                                 "ignore-errors", TRUE,
                                 /*"interpreter-number", CHIMARA_IF_ZMACHINE_TANDY_COLOR,*/
                                 NULL);
-       if( !chimara_glk_set_css_from_file(CHIMARA_GLK(self->glk), PACKAGE_DATA_DIR "/style.css", &error) ) {
-#ifdef DEBUG
-               g_error_free(error);
-               error = NULL;
-               if( !chimara_glk_set_css_from_file(CHIMARA_GLK(self->glk), PACKAGE_SRC_DIR "/style.css", &error) ) {
-#endif /* DEBUG */
-                       error_dialog(NULL, error, "Couldn't open CSS file: ");
-                       return;
-#ifdef DEBUG
-               }
-#endif /* DEBUG */
+       char *default_css = get_data_file_path("style.css");
+       if( !chimara_glk_set_css_from_file(CHIMARA_GLK(self->glk), default_css, &error) ) {
+               error_dialog(GTK_WINDOW(self), error, "Couldn't open default CSS file: ");
        }
        
        /* DON'T UNCOMMENT THIS your eyes will burn
         but it is a good test of programmatically altering just one style
         chimara_glk_set_css_from_string(CHIMARA_GLK(glk),
         "buffer.normal { font-family: 'Comic Sans MS'; }");*/
-       
-       GtkBox *vbox = GTK_BOX(load_object(builder, "vbox"));                   
-
-       ChimaraApp *theapp = chimara_app_get();
 
+       GtkUIManager *uimanager = new_ui_manager("player.menus");
        gtk_ui_manager_insert_action_group(uimanager, actiongroup, 0);
        gtk_ui_manager_insert_action_group(uimanager, chimara_app_get_action_group(theapp), 1);
        GtkWidget *menubar = gtk_ui_manager_get_widget(uimanager, "/player_menu");
@@ -176,7 +171,8 @@ chimara_player_init(ChimaraPlayer *self)
        /* Connect the accelerators */
        GtkAccelGroup *accels = gtk_ui_manager_get_accel_group(uimanager);
        gtk_window_add_accel_group(GTK_WINDOW(self), accels);
-       
+
+       GtkBox *vbox = GTK_BOX(load_object(builder, "player-vbox"));
        gtk_box_pack_end(vbox, self->glk, TRUE, TRUE, 0);
        g_object_ref(self->glk); /* add an extra reference to keep it alive while
                                                          the Glk program shuts down */
@@ -188,8 +184,8 @@ chimara_player_init(ChimaraPlayer *self)
        g_signal_connect(self->glk, "notify::program-name", G_CALLBACK(change_window_title), self);
        g_signal_connect(self->glk, "notify::story-name", G_CALLBACK(change_window_title), self);
 
-       g_object_unref( G_OBJECT(builder) );
-       g_object_unref( G_OBJECT(uimanager) );
+       g_object_unref(builder);
+       g_object_unref(uimanager);
 }
 
 /* PUBLIC FUNCTIONS */
@@ -201,3 +197,103 @@ chimara_player_new(void)
                "type", GTK_WINDOW_TOPLEVEL,
                NULL));
 }
+
+/* GLADE CALLBACKS */
+
+#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
+game if the user wishes to continue. */
+static gboolean
+confirm_open_new_game(ChimaraGlk *glk)
+{
+       g_return_val_if_fail(glk && CHIMARA_IS_GLK(glk), FALSE);
+       
+       GtkWindow *window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
+       
+       if(chimara_glk_get_running(glk)) {
+               GtkWidget *dialog = gtk_message_dialog_new(window,
+                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+                   GTK_MESSAGE_WARNING,
+                   GTK_BUTTONS_CANCEL,
+                   _("Are you sure you want to open a new game?"));
+               gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
+                   _("If you open a new game, you will quit the one you are currently playing."));
+               gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_OPEN, GTK_RESPONSE_OK);
+               gint response = gtk_dialog_run(GTK_DIALOG(dialog));
+               gtk_widget_destroy(dialog);
+               
+               if(response != GTK_RESPONSE_OK)
+                       return FALSE;
+
+               chimara_glk_stop(glk);
+               chimara_glk_wait(glk);
+       }
+       return TRUE;
+}
+#endif
+
+void
+on_stop_activate(GtkAction *action, ChimaraPlayer *player)
+{
+       chimara_glk_stop(CHIMARA_GLK(player->glk));
+}
+
+void
+on_copy_activate(GtkAction *action, ChimaraPlayer *player)
+{
+       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, ChimaraPlayer *player)
+{
+       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_toolbar_toggled(GtkToggleAction *action, ChimaraPlayer *player)
+{
+       if(gtk_toggle_action_get_active(action))
+               gtk_widget_show(player->toolbar);
+       else
+               gtk_widget_hide(player->toolbar);
+}
+
+void
+on_undo_activate(GtkAction *action, ChimaraPlayer *player)
+{
+       chimara_glk_feed_line_input(CHIMARA_GLK(player->glk), "undo");
+}
+
+void 
+on_save_activate(GtkAction *action, ChimaraPlayer *player)
+{
+       chimara_glk_feed_line_input(CHIMARA_GLK(player->glk), "save");
+}
+
+void 
+on_restore_activate(GtkAction *action, ChimaraPlayer *player)
+{
+       chimara_glk_feed_line_input(CHIMARA_GLK(player->glk), "restore");
+}
+
+void 
+on_restart_activate(GtkAction *action, ChimaraPlayer *player)
+{
+       chimara_glk_feed_line_input(CHIMARA_GLK(player->glk), "restart");
+}
+
+void 
+on_quit_activate(GtkAction *action, ChimaraPlayer *player)
+{
+       chimara_glk_feed_line_input(CHIMARA_GLK(player->glk), "quit");
+}
+