Showing off dynamic styles: started work on preferences dialog.
[rodin/chimara.git] / player / main.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * main.c
4  * Copyright (C) Philip en Marijn 2008 <>
5  * 
6  * main.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  * main.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 <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <stdio.h>
38
39 #include <glib.h>
40 #include <glib/gi18n.h>
41 #include <gtk/gtk.h>
42
43 #include "error.h"
44 #include <libchimara/chimara-glk.h>
45 #include <libchimara/chimara-if.h>
46
47 #include "preferences.h"
48
49 /* Static global pointers to widgets */
50 static GtkUIManager *uimanager = NULL;
51 static GtkWidget *window = NULL;
52 static GtkWidget *glk = NULL;
53
54 /* Global global pointers */
55 GtkBuilder *builder = NULL;
56 GtkWidget *aboutwindow = NULL;
57 GtkWidget *prefswindow = NULL;
58
59 GObject *
60 load_object(const gchar *name)
61 {
62         GObject *retval;
63         if( (retval = gtk_builder_get_object(builder, name)) == NULL) {
64                 error_dialog(NULL, NULL, "Error while getting object '%s'", name);
65                 g_error("Error while getting object '%s'", name);
66         }
67         return retval;
68 }
69
70 static void
71 change_window_title(ChimaraGlk *glk, GParamSpec *pspec, GtkWindow *window)
72 {
73         gchar *program_name, *story_name, *title;
74         g_object_get(glk, "program-name", &program_name, "story-name", &story_name, NULL);
75         if(!program_name) {
76                 gtk_window_set_title(window, "Chimara");
77                 return;
78         }
79         else if(!story_name)
80                 title = g_strdup_printf("%s - Chimara", program_name);
81         else
82                 title = g_strdup_printf("%s - %s - Chimara", program_name, story_name);
83                 
84         g_free(program_name);
85         g_free(story_name);
86         gtk_window_set_title(window, title);
87         g_free(title);
88 }
89
90 static void
91 create_window(void)
92 {
93         GError *error = NULL;
94
95         builder = gtk_builder_new();
96         if( !gtk_builder_add_from_file(builder, PACKAGE_DATA_DIR "/chimara.ui", &error) ) {
97 #ifdef DEBUG
98                 g_error_free(error);
99                 error = NULL;
100                 if( !gtk_builder_add_from_file(builder, PACKAGE_SRC_DIR "/chimara.ui", &error) ) {
101 #endif /* DEBUG */
102                         error_dialog(NULL, error, "Error while building interface: ");  
103                         return;
104 #ifdef DEBUG
105                 }
106 #endif /* DEBUG */
107         }
108
109         window = GTK_WIDGET(load_object("chimara"));
110         aboutwindow = GTK_WIDGET(load_object("aboutwindow"));
111         prefswindow = GTK_WIDGET(load_object("prefswindow"));
112         GtkActionGroup *actiongroup = GTK_ACTION_GROUP(load_object("actiongroup"));
113         GtkActionGroup *style_actiongroup = GTK_ACTION_GROUP(load_object("style-actiongroup"));
114
115         /* Add all the actions to the action group. This for-loop is a temporary fix
116         and can be removed once Glade supports adding actions and accelerators to an
117         action group. */
118         const gchar *actions[] = { 
119                 "game", "",
120                 "open", "<ctrl>O", 
121                 "recent", "",
122                 "stop", "",
123                 "quit_chimara", NULL, /* NULL means use stock accelerator */
124                 "command", "",
125                 "undo", "<ctrl>Z",
126                 "save", NULL, 
127                 "restore", "<ctrl>R", 
128                 "restart", "",
129                 "quit", "",
130                 "edit", "",
131                 "copy", NULL,
132                 "paste", NULL,
133                 "preferences", "",
134                 "help", "",
135                 "about", "",
136                 NULL
137         };
138         const gchar *style_actions[] = { 
139                 "align-left",
140                 "align-justify",
141                 "align-right",
142                 "bold",
143                 "italic",
144                 "underline",
145                 NULL
146         };
147         const gchar **ptr;
148         for(ptr = actions; *ptr; ptr += 2)
149                 gtk_action_group_add_action_with_accel(actiongroup, GTK_ACTION(load_object(ptr[0])), ptr[1]);
150         for(ptr = style_actions; *ptr; ptr ++)
151                 gtk_action_group_add_action(style_actiongroup, GTK_ACTION(load_object(*ptr)));
152         GtkRecentFilter *filter = gtk_recent_filter_new();
153         /* TODO: Use mimetypes and construct the filter dynamically depending on 
154         what plugins are installed */
155         const gchar *patterns[] = {
156                 "*.z[1-8]", "*.[zg]lb", "*.[zg]blorb", "*.ulx", "*.blb", "*.blorb", NULL
157         };
158         for(ptr = patterns; *ptr; ptr++)
159                 gtk_recent_filter_add_pattern(filter, *ptr);
160         GtkRecentChooser *recent = GTK_RECENT_CHOOSER(load_object("recent"));
161         gtk_recent_chooser_add_filter(recent, filter);
162
163         uimanager = gtk_ui_manager_new();
164         if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_DATA_DIR "/chimara.menus", &error) ) {
165 #ifdef DEBUG
166                 g_error_free(error);
167                 error = NULL;
168                 if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_SRC_DIR "/chimara.menus", &error) ) {
169 #endif /* DEBUG */
170                         error_dialog(NULL, error, "Error while building interface: ");
171                         return;
172 #ifdef DEBUG
173                 }
174 #endif /* DEBUG */
175         }
176         
177         glk = chimara_if_new();
178         g_object_set(glk, "ignore-errors", TRUE, NULL);
179         if( !chimara_glk_set_css_from_file(CHIMARA_GLK(glk), PACKAGE_DATA_DIR "/style.css", &error) ) {
180 #ifdef DEBUG
181                 g_error_free(error);
182                 error = NULL;
183                 if( !chimara_glk_set_css_from_file(CHIMARA_GLK(glk), PACKAGE_SRC_DIR "/macstyle.css", &error) ) {
184 #endif /* DEBUG */
185                         error_dialog(NULL, error, "Couldn't open CSS file: ");
186                         return;
187 #ifdef DEBUG
188                 }
189 #endif /* DEBUG */
190         }
191         
192         /* DON'T UNCOMMENT THIS your eyes will burn
193          but it is a good test of programmatically altering just one style
194         chimara_glk_set_css_from_string(CHIMARA_GLK(glk),
195             "buffer.normal { font-family: 'Comic Sans MS'; }");*/
196         
197         GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );                      
198         if(vbox == NULL)
199         {
200                 error_dialog(NULL, NULL, "Could not find vbox");
201                 return;
202         }
203
204         gtk_ui_manager_insert_action_group(uimanager, actiongroup, 0);
205         GtkWidget *menubar = gtk_ui_manager_get_widget(uimanager, "/menubar");
206         //GtkWidget *toolbar = gtk_ui_manager_get_widget(uimanager, "/toolbar");
207
208         gtk_box_pack_end(vbox, glk, TRUE, TRUE, 0);
209         gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
210         //gtk_box_pack_start(vbox, toolbar, FALSE, FALSE, 0);
211         
212         gtk_builder_connect_signals(builder, glk);
213         g_signal_connect(glk, "notify::program-name", G_CALLBACK(change_window_title), window);
214         g_signal_connect(glk, "notify::story-name", G_CALLBACK(change_window_title), window);
215         
216         /* Create preferences window */
217         preferences_create(CHIMARA_GLK(glk));
218 }
219
220 int
221 main(int argc, char *argv[])
222 {
223         GError *error = NULL;
224
225 #ifdef ENABLE_NLS
226         bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
227         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
228         textdomain(GETTEXT_PACKAGE);
229 #endif
230
231         if( !g_thread_supported() )
232                 g_thread_init(NULL);
233         gdk_threads_init();
234         gtk_init(&argc, &argv);
235
236         create_window();
237         gtk_widget_show_all(window);
238
239         g_object_unref( G_OBJECT(uimanager) );
240
241         if(argc >= 2) {
242                 if( !chimara_if_run_game(CHIMARA_IF(glk), argv[1], &error) ) {
243                         error_dialog(GTK_WINDOW(window), error, "Error starting Glk library: ");
244                         return 1;
245                 }
246         }
247
248     gdk_threads_enter();
249         gtk_main();
250         gdk_threads_leave();
251
252         chimara_glk_stop(CHIMARA_GLK(glk));
253         chimara_glk_wait(CHIMARA_GLK(glk));
254
255         g_object_unref( G_OBJECT(builder) );
256
257         return 0;
258 }