1 /* decidewin.c - decision dialog
2 *
3 * Copyright 2010 Petteri Hintsanen <petterih@iki.fi>
4 *
5 * This file is part of abx.
6 *
7 * abx is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * abx is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with abx. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "gtkui.h"
22 #include "test.h"
23
24 static GtkWidget *dialog;
25
26 /*
27 * Handle button responses. Close test and display result window, if
28 * the current trial is the last one.
29 */
30 static void
response_handler(GtkWidget * widget,gint response,gpointer data)31 response_handler(GtkWidget *widget, gint response, gpointer data)
32 {
33 switch (response) {
34 case 0:
35 case 1:
36 set_guess(current_trial, response);
37 gtk_widget_hide_all(dialog);
38 if (current_trial == num_test_trials() - 1) {
39 /* test over */
40 show_result_window();
41 close_test();
42 current_trial = -1;
43 update_main_window();
44 } else {
45 /* proceed to the next trial */
46 current_trial++;
47 update_main_window();
48 }
49 break;
50 case GTK_RESPONSE_DELETE_EVENT:
51 case GTK_RESPONSE_CANCEL:
52 gtk_widget_hide_all(dialog);
53 break;
54 }
55 }
56
57 /*
58 * Build and show decide dialog with the given parent window.
59 */
60 void
show_decide_dialog(GtkWindow * parent)61 show_decide_dialog(GtkWindow *parent)
62 {
63 if (dialog) {
64 gtk_widget_show_all(dialog);
65 } else {
66 dialog = (gtk_message_dialog_new
67 (parent, GTK_DIALOG_DESTROY_WITH_PARENT,
68 GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "Decide X"));
69 gtk_window_set_title(GTK_WINDOW(dialog), "Decision");
70 gtk_message_dialog_format_secondary_text
71 (GTK_MESSAGE_DIALOG(dialog), "Which one is X, A or B?");
72
73 gtk_dialog_add_button(GTK_DIALOG(dialog), "_A", 0);
74 gtk_dialog_add_button(GTK_DIALOG(dialog), "_B", 1);
75 gtk_dialog_add_button(GTK_DIALOG(dialog),
76 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
77 g_signal_connect(dialog, "response",
78 G_CALLBACK(response_handler), NULL);
79 gtk_widget_show_all(dialog);
80 }
81 }
82
83 void
hide_decide_dialog(void)84 hide_decide_dialog(void)
85 {
86 if (dialog) {
87 gtk_widget_hide_all(dialog);
88 }
89 }
90