Set CSS file from preferences dialog
[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         if( !g_thread_supported() )
80                 g_thread_init(NULL);
81         gdk_threads_init();
82         gtk_init(&argc, &argv);
83
84         GtkBuilder *builder = gtk_builder_new();
85         if(!gtk_builder_add_from_file(builder, PACKAGE_SRC_DIR "/glulxercise.ui", &error))
86                 g_error("Failed to build interface: %s", error->message);
87
88         Widgets *w = g_slice_new0(Widgets);
89         w->window = LOAD_WIDGET("window");
90         GtkWidget *vbox = LOAD_WIDGET("vbox");
91         w->test_picker = LOAD_WIDGET("test_picker");
92         gtk_combo_box_set_active(GTK_COMBO_BOX(w->test_picker), 0);
93         w->go = LOAD_WIDGET("go");
94         w->stop = LOAD_WIDGET("stop");
95         w->interp = chimara_if_new();
96         gtk_widget_set_size_request(w->interp, 500, 600);
97         gtk_box_pack_end_defaults(GTK_BOX(vbox), w->interp);
98         gtk_builder_connect_signals(builder, w);
99         g_signal_connect(w->interp, "started", G_CALLBACK(on_interp_started), w);
100         g_signal_connect(w->interp, "stopped", G_CALLBACK(on_interp_stopped), w);
101         gtk_widget_show_all(w->window);
102
103         gdk_threads_enter();
104         gtk_main();
105         gdk_threads_leave();
106
107         chimara_glk_stop( CHIMARA_GLK(w->interp) );
108         chimara_glk_wait( CHIMARA_GLK(w->interp) );
109
110         g_slice_free(Widgets, w);
111
112         return 0;
113 }