1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Philip en Marijn 2008 <>
6 * main.c is free software copyrighted by Philip en Marijn.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/types.h>
40 #include <glib/gi18n.h>
44 #include <libchimara/chimara-glk.h>
45 #include <libchimara/chimara-if.h>
47 #include "preferences.h"
49 /* Static global pointers to widgets */
50 static GtkUIManager *uimanager = NULL;
51 static GtkWidget *window = NULL;
52 static GtkWidget *glk = NULL;
54 /* Global global pointers */
55 GtkBuilder *builder = NULL;
56 GtkWidget *aboutwindow = NULL;
57 GtkWidget *prefswindow = NULL;
60 load_object(const gchar *name)
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);
71 change_window_title(ChimaraGlk *glk, GParamSpec *pspec, GtkWindow *window)
73 gchar *program_name, *story_name, *title;
74 g_object_get(glk, "program-name", &program_name, "story-name", &story_name, NULL);
76 gtk_window_set_title(window, "Chimara");
80 title = g_strdup_printf("%s - Chimara", program_name);
82 title = g_strdup_printf("%s - %s - Chimara", program_name, story_name);
86 gtk_window_set_title(window, title);
95 builder = gtk_builder_new();
96 if( !gtk_builder_add_from_file(builder, PACKAGE_DATA_DIR "/chimara.ui", &error) ) {
100 if( !gtk_builder_add_from_file(builder, PACKAGE_SRC_DIR "/chimara.ui", &error) ) {
102 error_dialog(NULL, error, "Error while building interface: ");
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"));
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
118 const gchar *actions[] = {
123 "quit_chimara", NULL, /* NULL means use stock accelerator */
127 "restore", "<ctrl>R",
138 const gchar *style_actions[] = {
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
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);
163 uimanager = gtk_ui_manager_new();
164 if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_DATA_DIR "/chimara.menus", &error) ) {
168 if( !gtk_ui_manager_add_ui_from_file(uimanager, PACKAGE_SRC_DIR "/chimara.menus", &error) ) {
170 error_dialog(NULL, error, "Error while building interface: ");
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) ) {
183 if( !chimara_glk_set_css_from_file(CHIMARA_GLK(glk), PACKAGE_SRC_DIR "/style.css", &error) ) {
185 error_dialog(NULL, error, "Couldn't open CSS file: ");
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'; }");*/
197 GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );
200 error_dialog(NULL, NULL, "Could not find vbox");
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");
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);
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);
216 /* Create preferences window */
217 preferences_create(CHIMARA_GLK(glk));
221 main(int argc, char *argv[])
223 GError *error = NULL;
226 bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
227 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
228 textdomain(GETTEXT_PACKAGE);
231 if( !g_thread_supported() )
234 gtk_init(&argc, &argv);
237 gtk_widget_show_all(window);
239 g_object_unref( G_OBJECT(uimanager) );
242 if( !chimara_if_run_game(CHIMARA_IF(glk), argv[1], &error) ) {
243 error_dialog(GTK_WINDOW(window), error, "Error starting Glk library: ");
252 chimara_glk_stop(CHIMARA_GLK(glk));
253 chimara_glk_wait(CHIMARA_GLK(glk));
255 g_object_unref( G_OBJECT(builder) );