Improve player
[rodin/chimara.git] / player / callbacks.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * callbacks.c
4  * Copyright (C) Philip en Marijn 2008 <>
5  * 
6  * callbacks.c is free software copyrighted by Philip en Marijn.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name ``Philip en Marijn'' nor the name of any other
17  *    contributor may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  * 
20  * callbacks.c IS PROVIDED BY Philip en Marijn ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Philip en Marijn OR ANY OTHER CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <glib.h>
34 #include <glib/gi18n.h>
35 #include <gtk/gtk.h>
36 #include <libchimara/chimara-glk.h>
37 #include <libchimara/chimara-if.h>
38 #include <config.h>
39 #include "error.h"
40
41 /* If a game is running in @glk, warn the user that they will quit the currently
42 running game if they open a new one. Returns TRUE if no game was running.
43 Returns FALSE if the user cancelled. Returns TRUE and shuts down the running
44 game if the user wishes to continue. */
45 static gboolean
46 confirm_open_new_game(ChimaraGlk *glk)
47 {
48         g_return_val_if_fail(glk && CHIMARA_IS_GLK(glk), FALSE);
49         
50         GtkWindow *window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
51         
52         if(chimara_glk_get_running(glk)) {
53                 GtkWidget *dialog = gtk_message_dialog_new(window,
54                     GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
55                     GTK_MESSAGE_WARNING,
56                     GTK_BUTTONS_CANCEL,
57                     _("Are you sure you want to open a new game?"));
58                 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
59                     _("If you open a new game, you will quit the one you are currently playing."));
60                 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_OPEN, GTK_RESPONSE_OK);
61                 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
62                 gtk_widget_destroy(dialog);
63                 
64                 if(response != GTK_RESPONSE_OK)
65                         return FALSE;
66
67                 chimara_glk_stop(glk);
68                 chimara_glk_wait(glk);
69         }
70         return TRUE;
71 }
72
73 void 
74 on_open_activate(GtkAction *action, ChimaraGlk *glk) 
75 {
76         GtkWindow *window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
77         
78         if(!confirm_open_new_game(glk))
79                 return;
80
81         GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Open Game"),
82             window,
83             GTK_FILE_CHOOSER_ACTION_OPEN,
84             GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
85             GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
86             NULL);
87         if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
88                 GError *error = NULL;
89                 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
90                 if(!chimara_if_run_game(CHIMARA_IF(glk), filename, &error)) {
91                         error_dialog(window, error, _("Could not open game file '%s': "), filename);
92                         g_free(filename);
93                         gtk_widget_destroy(dialog);
94                         return;
95                 }
96                 
97                 /* Add file to recent files list */
98                 GtkRecentManager *manager = gtk_recent_manager_get_default();
99                 gchar *uri;
100                 
101                 if(!(uri = g_filename_to_uri(filename, NULL, &error)))
102                         g_warning(_("Could not convert filename '%s' to URI: %s"), filename, error->message);
103                 else {
104                         if(!gtk_recent_manager_add_item(manager, uri))
105                                 g_warning(_("Could not add URI '%s' to recent files list."), uri);
106                         g_free(uri);
107                 }
108                 g_free(filename);
109         }
110         gtk_widget_destroy(dialog);
111 }
112
113 void
114 on_recent_item_activated(GtkRecentChooser *chooser, ChimaraGlk *glk)
115 {
116         GError *error = NULL;
117         GtkWindow *window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
118         gchar *uri = gtk_recent_chooser_get_current_uri(chooser);
119         gchar *filename;
120         if(!(filename = g_filename_from_uri(uri, NULL, &error))) {
121                 error_dialog(window, error, _("Could not open game file '%s': "), uri);
122                 g_free(uri);
123                 return;
124         }
125         
126         if(!confirm_open_new_game(glk)) {
127                 g_free(filename);
128                 g_free(uri);
129                 return;
130         }
131         
132         if(!chimara_if_run_game(CHIMARA_IF(glk), filename, &error)) {
133                 error_dialog(window, error, _("Could not open game file '%s': "), filename);
134                 g_free(filename);
135                 g_free(uri);
136                 return;
137         }
138         g_free(filename);
139         
140         /* Add file to recent files list again, this updates it to most recently used */
141         GtkRecentManager *manager = gtk_recent_manager_get_default();
142         if(!gtk_recent_manager_add_item(manager, uri))
143                 g_warning(_("Could not add URI '%s' to recent files list."), uri);
144         
145         g_free(uri);
146 }
147
148 void
149 on_stop_activate(GtkAction *action, ChimaraGlk *glk)
150 {
151         chimara_glk_stop(glk);
152 }
153
154 void 
155 on_quit_chimara_activate(GtkAction *action, ChimaraGlk *glk) 
156 {
157         gtk_main_quit();
158 }
159
160 void
161 on_copy_activate(GtkAction *action, ChimaraGlk *glk)
162 {
163         GtkWindow *toplevel = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
164         GtkWidget *focus = gtk_window_get_focus(toplevel);
165         /* Call "copy clipboard" on any widget that defines it */
166         if(GTK_IS_LABEL(focus) || GTK_IS_ENTRY(focus) || GTK_IS_TEXT_VIEW(focus))
167                 g_signal_emit_by_name(focus, "copy-clipboard");
168 }
169
170 void
171 on_paste_activate(GtkAction *action, ChimaraGlk *glk)
172 {
173         GtkWindow *toplevel = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(glk)));
174         GtkWidget *focus = gtk_window_get_focus(toplevel);
175         /* Call "paste clipboard" on any widget that defines it */
176         if(GTK_IS_ENTRY(focus) || GTK_IS_TEXT_VIEW(focus))
177                 g_signal_emit_by_name(focus, "paste-clipboard");
178 }
179
180 void
181 on_preferences_activate(GtkAction *action, ChimaraGlk *glk)
182 {
183         extern GtkWidget *prefswindow;
184         gtk_window_present(GTK_WINDOW(prefswindow));
185 }
186
187 void
188 on_undo_activate(GtkAction *action, ChimaraGlk *glk)
189 {
190         chimara_glk_feed_line_input(glk, "undo");
191 }
192
193 void 
194 on_save_activate(GtkAction *action, ChimaraGlk *glk) 
195 {
196         chimara_glk_feed_line_input(glk, "save");
197 }
198
199 void 
200 on_restore_activate(GtkAction *action, ChimaraGlk *glk) 
201 {
202         chimara_glk_feed_line_input(glk, "restore");
203 }
204
205 void 
206 on_restart_activate(GtkAction *action, ChimaraGlk *glk) 
207 {
208         chimara_glk_feed_line_input(glk, "restart");
209 }
210
211 void 
212 on_quit_activate(GtkAction *action, ChimaraGlk *glk) 
213 {
214         chimara_glk_feed_line_input(glk, "quit");
215 }
216
217 void
218 on_about_activate(GtkAction *action, ChimaraGlk *glk)
219 {
220         extern GtkWidget *aboutwindow;
221         gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(aboutwindow), PACKAGE_VERSION);
222         gtk_window_present(GTK_WINDOW(aboutwindow));
223 }
224
225 gboolean 
226 on_window_delete_event(GtkWidget *widget, GdkEvent *event, ChimaraGlk *glk) 
227 {
228         gtk_main_quit();
229         return TRUE;
230 }