Build with GTK 3
[projects/chimara/chimara.git] / tests / test-close.c
1 #include <gtk/gtk.h>
2 #include <libchimara/chimara-if.h>
3
4 void
5 on_command(ChimaraIF *glk, gchar *input, gchar *response, GtkWindow *window)
6 {
7         GtkWidget *dialog = gtk_message_dialog_new(window, 
8                 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
9                 "%s", input);
10         gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", 
11                 response);
12         gtk_dialog_run(GTK_DIALOG(dialog));
13         gtk_widget_destroy(dialog);
14 }
15
16 void
17 on_stop(GtkButton *button, ChimaraIF *glk)
18 {
19         chimara_glk_stop(CHIMARA_GLK(glk));
20         chimara_glk_wait(CHIMARA_GLK(glk));
21 }
22
23 void
24 on_go(GtkButton *button, ChimaraIF *glk)
25 {
26         on_stop(button, glk);
27         g_assert(chimara_if_run_game(CHIMARA_IF(glk), "unicodetest.ulx", NULL));
28 }
29
30 int
31 main(int argc, char *argv[])
32 {
33     GtkWidget *window, *vbox, *hbox, *stop, *go, *glk;
34
35     /* Initialize threads and GTK */
36     if(!g_thread_supported())
37         g_thread_init(NULL);
38     gdk_threads_init();
39     gtk_init(&argc, &argv);
40     
41     /* Construct the window and its contents. We quit the GTK main loop
42      * when the window's close button is clicked. */
43     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
44     gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
45     g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
46     vbox = gtk_vbox_new(FALSE, 6);
47     glk = chimara_if_new();
48     //g_signal_connect(glk, "command", G_CALLBACK(on_command), window);
49     hbox = gtk_hbutton_box_new();
50     stop = gtk_button_new_with_label("Stop");
51     g_signal_connect(stop, "clicked", G_CALLBACK(on_stop), glk);
52     go = gtk_button_new_with_label("Go");
53     g_signal_connect(go, "clicked", G_CALLBACK(on_go), glk);
54
55     gtk_box_pack_start(GTK_BOX(hbox), stop, TRUE, TRUE, 0);
56     gtk_box_pack_start(GTK_BOX(hbox), go, TRUE, TRUE, 0);
57     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
58     gtk_box_pack_start(GTK_BOX(vbox), glk, TRUE, TRUE, 0);
59     gtk_container_add(GTK_CONTAINER(window), vbox);
60     gtk_widget_show_all(window);
61     
62     /* Add a reference to the ChimaraGlk widget, because we want to keep it
63     around after gtk_main() exits */
64     g_object_ref(glk);
65     
66     /* Start the plugin */
67     g_assert(chimara_if_run_game(CHIMARA_IF(glk), "unicodetest.ulx", NULL));
68     
69     /* Start the GTK main loop */
70     gdk_threads_enter();
71     gtk_main();
72     gdk_threads_leave();
73
74     /* After the GTK main loop exits, signal the Glk program to shut down if
75      * it is still running, and wait for it to exit. */
76     chimara_glk_stop(CHIMARA_GLK(glk));
77     chimara_glk_wait(CHIMARA_GLK(glk));
78     g_object_unref(glk);
79
80     return 0;
81 }