X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=player%2Fplayer.c;h=10f45d53c4b724145163f66ea91c93e302d74553;hb=6fd0347f330cb24ef6145662023053a1c0f81a64;hp=a35423bd978665ebc547f670273f01a77d36aaa4;hpb=25a7dce7786ce3c8514bd67313048e4f6d19816a;p=projects%2Fchimara%2Fchimara.git diff --git a/player/player.c b/player/player.c index a35423b..10f45d5 100644 --- a/player/player.c +++ b/player/player.c @@ -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 #include +#include +#include #include #include #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) { @@ -45,15 +69,28 @@ change_window_title(ChimaraGlk *glk, GParamSpec *pspec, GtkWindow *window) g_free(title); } +static void +on_css_changed(GSettings *prefs_settings, char *key, ChimaraPlayer *self) +{ + char *user_css; + g_settings_get(prefs_settings, "css-file", "ms", &user_css); + if(user_css) { + if(!chimara_glk_set_css_from_file(CHIMARA_GLK(self->glk), user_css, NULL)) { + /* If the setting didn't point to a CSS file, fail silently and + null the setting */ + g_settings_set(prefs_settings, "css-file", "ms", NULL); + } + g_free(user_css); + } +} + static void chimara_player_dispose(GObject *object) { ChimaraPlayer *self = CHIMARA_PLAYER(object); if(chimara_glk_get_running(CHIMARA_GLK(self->glk))) { chimara_glk_stop(CHIMARA_GLK(self->glk)); - g_printerr("Stopping...\n"); chimara_glk_wait(CHIMARA_GLK(self->glk)); - g_printerr("Done Waiting\n"); } /* Chain up */ @@ -63,7 +100,6 @@ chimara_player_dispose(GObject *object) static void chimara_player_finalize(GObject *object) { - g_printerr("Unreffing\n"); g_object_unref(CHIMARA_PLAYER(object)->glk); /* Chain up */ @@ -75,14 +111,8 @@ chimara_player_class_init(ChimaraPlayerClass *klass) { /* Override methods of parent classes */ GObjectClass *object_class = G_OBJECT_CLASS(klass); - //object_class->set_property = chimara_if_set_property; - //object_class->get_property = chimara_if_get_property; object_class->dispose = chimara_player_dispose; object_class->finalize = chimara_player_finalize; - - /* Signals */ - - /* Properties */ /* Private data */ g_type_class_add_private(klass, sizeof(ChimaraPlayerPrivate)); @@ -92,77 +122,52 @@ 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 */ + + /* Set the CSS styles for the interpreter */ + 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: "); } + g_free(default_css); + on_css_changed(theapp->prefs_settings, "css-file", self); /* 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 +181,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 */ @@ -187,9 +193,10 @@ chimara_player_init(ChimaraPlayer *self) gtk_builder_connect_signals(builder, 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_signal_connect(theapp->prefs_settings, "changed::css-file", G_CALLBACK(on_css_changed), 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 +208,110 @@ chimara_player_new(void) "type", GTK_WINDOW_TOPLEVEL, NULL)); } + +void +chimara_player_set_user_css_file(ChimaraPlayer *self, const char *filename) +{ + chimara_glk_set_css_to_default(CHIMARA_GLK(self->glk)); + chimara_glk_set_css_from_file(CHIMARA_GLK(self->glk), filename, 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"); +} +