No need to include empty default signal handlers
[projects/chimara/chimara.git] / tests / glulxercise.c
1 #include <glib.h>
2 #include <libchimara/chimara-glk.h>
3 #include <libchimara/chimara-if.h>
4
5 #define LOAD_WIDGET(name) GTK_WIDGET(gtk_builder_get_object(builder, name))
6
7 typedef struct {
8         GtkWidget *window, *test_picker, *go, *stop, *interp;
9 } Widgets;
10
11 gboolean
12 on_window_delete_event()
13 {
14         gtk_main_quit();
15         return TRUE;
16 }
17
18 void
19 on_go_clicked(GtkButton *go, Widgets *w)
20 {
21         GError *error = NULL;
22
23         gchar *filename = NULL;
24         GtkTreeIter iter;
25         GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(w->test_picker));
26         gtk_combo_box_get_active_iter(GTK_COMBO_BOX(w->test_picker), &iter);
27         gtk_tree_model_get(model, &iter, 1, &filename, -1);
28         g_object_unref(model);
29
30         gchar *fullpath = g_build_filename(PACKAGE_SRC_DIR, filename, NULL);
31
32         if( !chimara_if_run_game(CHIMARA_IF(w->interp), fullpath, &error) )
33                 g_error("Error starting Glk library: %s", error->message);
34         g_free(fullpath);
35 }
36
37 void
38 on_interp_started(ChimaraGlk *glk, Widgets *w)
39 {
40         gtk_widget_set_sensitive(w->go, FALSE);
41         gtk_widget_set_sensitive(w->stop, TRUE);
42         gtk_widget_set_sensitive(w->test_picker, FALSE);
43 }
44
45 void
46 on_stop_clicked(GtkButton *stop, Widgets *w)
47 {
48         chimara_glk_stop( CHIMARA_GLK(w->interp) );
49         chimara_glk_wait( CHIMARA_GLK(w->interp) );
50 }
51
52 void
53 on_interp_stopped(ChimaraGlk *glk, Widgets *w)
54 {
55         gtk_widget_set_sensitive(w->stop, FALSE);
56         gtk_widget_set_sensitive(w->go, TRUE);
57         gtk_widget_set_sensitive(w->test_picker, TRUE);
58 }
59
60 void
61 on_glulxe_toggled(GtkToggleButton *glulxe, Widgets *w)
62 {
63         if(gtk_toggle_button_get_active(glulxe))
64                 chimara_if_set_preferred_interpreter(CHIMARA_IF(w->interp), CHIMARA_IF_FORMAT_GLULX, CHIMARA_IF_INTERPRETER_GLULXE);
65 }
66
67 void
68 on_git_toggled(GtkToggleButton *git, Widgets *w)
69 {
70         if(gtk_toggle_button_get_active(git))
71                 chimara_if_set_preferred_interpreter(CHIMARA_IF(w->interp), CHIMARA_IF_FORMAT_GLULX, CHIMARA_IF_INTERPRETER_GIT);
72 }
73
74 int
75 main(int argc, char *argv[])
76 {
77         GError *error = NULL;
78
79         gdk_threads_init();
80         gtk_init(&argc, &argv);
81
82         GtkBuilder *builder = gtk_builder_new();
83         if(!gtk_builder_add_from_file(builder, PACKAGE_SRC_DIR "/glulxercise.ui", &error))
84                 g_error("Failed to build interface: %s", error->message);
85
86         Widgets *w = g_slice_new0(Widgets);
87         w->window = LOAD_WIDGET("window");
88         GtkWidget *vbox = LOAD_WIDGET("vbox");
89         w->test_picker = LOAD_WIDGET("test_picker");
90         gtk_combo_box_set_active(GTK_COMBO_BOX(w->test_picker), 0);
91         w->go = LOAD_WIDGET("go");
92         w->stop = LOAD_WIDGET("stop");
93         w->interp = chimara_if_new();
94         gtk_widget_set_size_request(w->interp, 500, 600);
95         gtk_box_pack_end(GTK_BOX(vbox), w->interp, TRUE, TRUE, 0);
96         chimara_glk_set_css_from_string(CHIMARA_GLK(w->interp),
97                 "buffer { font-size: 12; } buffer.input { color: #00a; font-style: italic; }");
98         chimara_glk_set_spacing(CHIMARA_GLK(w->interp), 1);
99         gtk_builder_connect_signals(builder, w);
100         g_signal_connect(w->interp, "started", G_CALLBACK(on_interp_started), w);
101         g_signal_connect(w->interp, "stopped", G_CALLBACK(on_interp_stopped), w);
102         gtk_widget_show_all(w->window);
103
104         gdk_threads_enter();
105         gtk_main();
106         gdk_threads_leave();
107
108         chimara_glk_stop( CHIMARA_GLK(w->interp) );
109         chimara_glk_wait( CHIMARA_GLK(w->interp) );
110
111         g_slice_free(Widgets, w);
112
113         return 0;
114 }