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>
43 #include "callbacks.h"
45 #include <libchimara/chimara-glk.h>
46 #include <libchimara/chimara-if.h>
48 /* Global pointers to widgets */
49 GtkBuilder *builder = NULL;
50 GtkUIManager *uimanager = NULL;
51 GtkWidget *window = NULL;
52 GtkWidget *glk = NULL;
55 on_started(ChimaraGlk *glk)
57 g_printerr("Started!\n");
61 on_stopped(ChimaraGlk *glk)
63 g_printerr("Stopped!\n");
67 on_command(ChimaraGlk *glk, gchar *input, gchar *response)
69 g_printerr("Command!\n");
73 load_object(const gchar *name)
76 if( (retval = gtk_builder_get_object(builder, name)) == NULL) {
77 error_dialog(NULL, NULL, "Error while getting object '%s'", name);
78 g_error("Error while getting object '%s'", name);
88 builder = gtk_builder_new();
89 if( !gtk_builder_add_from_file(builder, "chimara.ui", &error) ) {
90 error_dialog(NULL, error, "Error while building interface: ");
94 window = GTK_WIDGET(load_object("chimara"));
95 GtkActionGroup *actiongroup = GTK_ACTION_GROUP(load_object("actiongroup"));
97 /* Add all the actions to the action group. This for-loop is a temporary fix
98 and can be removed once Glade supports adding actions and accelerators to an
100 const gchar *actions[] = {
103 "save", NULL, /* NULL means use stock accelerator */
108 for(ptr = actions; *ptr; ptr += 2)
109 gtk_action_group_add_action_with_accel(actiongroup, GTK_ACTION(load_object(ptr[0])), ptr[1]);
111 uimanager = gtk_ui_manager_new();
112 if( !gtk_ui_manager_add_ui_from_file(uimanager, "chimara.menus", &error) ) {
113 error_dialog(NULL, error, "Error while building interface: ");
117 glk = chimara_if_new();
118 g_object_set(glk, "border-width", 6, "spacing", 6, NULL);
119 chimara_glk_set_default_font_string(CHIMARA_GLK(glk), "Serif 12");
120 chimara_glk_set_monospace_font_string(CHIMARA_GLK(glk), "Monospace 12");
121 g_signal_connect(glk, "started", G_CALLBACK(on_started), NULL);
122 g_signal_connect(glk, "stopped", G_CALLBACK(on_stopped), NULL);
123 g_signal_connect(glk, "command", G_CALLBACK(on_command), NULL);
125 GtkBox *vbox = GTK_BOX( gtk_builder_get_object(builder, "vbox") );
128 error_dialog(NULL, NULL, "Could not find vbox");
132 gtk_ui_manager_insert_action_group(uimanager, actiongroup, 0);
133 GtkWidget *menubar = gtk_ui_manager_get_widget(uimanager, "/menubar");
134 GtkWidget *toolbar = gtk_ui_manager_get_widget(uimanager, "/toolbar");
136 gtk_box_pack_end(vbox, glk, TRUE, TRUE, 0);
137 gtk_box_pack_start(vbox, menubar, FALSE, FALSE, 0);
138 gtk_box_pack_start(vbox, toolbar, FALSE, FALSE, 0);
140 gtk_builder_connect_signals(builder, NULL);
144 main(int argc, char *argv[])
146 GError *error = NULL;
149 bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
150 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
151 textdomain(GETTEXT_PACKAGE);
154 if( !g_thread_supported() )
157 gtk_init(&argc, &argv);
160 gtk_widget_show_all(window);
162 g_object_unref( G_OBJECT(builder) );
163 g_object_unref( G_OBJECT(uimanager) );
166 error_dialog(GTK_WINDOW(window), NULL, "Must provide a game file");
170 if( !chimara_if_run_game(CHIMARA_IF(glk), argv[1], &error) ) {
171 error_dialog(GTK_WINDOW(window), error, "Error starting Glk library: ");
179 chimara_glk_stop(CHIMARA_GLK(glk));
180 chimara_glk_wait(CHIMARA_GLK(glk));