1 /* resultwin.c - window for displaying test results
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 "test.h"
22 #include "gtkui.h"
23 
24 static GtkWidget *answers_box;
25 static GtkWidget *close_box;
26 static GtkWidget *guesses_box;
27 static GtkWidget *main_box;
28 static GtkWidget *ncorr_box;
29 static GtkWidget *pval_box;
30 static GtkWidget *window;
31 static GtkWidget *close_button;
32 static GtkWidget *answers;
33 static GtkWidget *guesses;
34 
35 static gboolean
delete_event_handler(GtkWidget * widget,GdkEvent * event,gpointer data)36 delete_event_handler(GtkWidget *widget, GdkEvent *event, gpointer data)
37 {
38     window = NULL;
39     return FALSE;
40 }
41 
42 static void
close_clicked(GtkWidget * button,gpointer data)43 close_clicked(GtkWidget *button, gpointer data)
44 {
45     gtk_widget_destroy(window);
46     window = NULL;
47 }
48 
49 /*
50  * Create and show result window.
51  */
52 void
show_result_window(void)53 show_result_window(void)
54 {
55     GString *as;
56     GString *gs;
57     int i;
58     int ncorr;
59 
60     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
61     gtk_window_set_title(GTK_WINDOW(window), "Test results");
62     g_signal_connect(G_OBJECT(window), "delete_event",
63                      G_CALLBACK(delete_event_handler), NULL);
64     close_button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
65     g_signal_connect(close_button, "clicked",
66                      G_CALLBACK(close_clicked), NULL);
67 
68     /* build decision strings */
69     if (get_answer(0) == get_guess(0)) ncorr = 1;
70     else ncorr = 0;
71     as = g_string_new(get_answer(0) == SAMPLE_A ? "<tt>A" : "<tt>B");
72     gs = g_string_new(get_guess(0) == SAMPLE_A ? "<tt>A" : "<tt>B");
73     for (i = 1; i < num_test_trials(); i++) {
74         g_string_append(as, get_answer(i) == SAMPLE_A ? " A" : " B");
75         g_string_append(gs, get_guess(i) == SAMPLE_A ? " A" : " B");
76         if (get_answer(i) == get_guess(i)) ncorr++;
77     }
78     g_string_append(as, "</tt>");
79     g_string_append(gs, "</tt>");
80 
81     answers = gtk_label_new(NULL);
82     gtk_label_set_markup(GTK_LABEL (answers), as->str);
83     guesses = gtk_label_new(NULL);
84     gtk_label_set_markup(GTK_LABEL (guesses), gs->str);
85 
86     answers_box = gtk_hbox_new(FALSE, 5);
87     guesses_box = gtk_hbox_new(FALSE, 5);
88     ncorr_box = gtk_hbox_new(FALSE, 5);
89     pval_box = gtk_hbox_new(FALSE, 5);
90     close_box = gtk_hbox_new(FALSE, 5);
91 
92     gtk_box_pack_start(GTK_BOX(answers_box),
93                        gtk_label_new("Correct decisions:"),
94                        FALSE, FALSE, 0);
95     gtk_box_pack_end(GTK_BOX(answers_box), answers, FALSE, FALSE, 0);
96 
97     gtk_box_pack_start(GTK_BOX(guesses_box),
98                        gtk_label_new("Your decisions:"),
99                        FALSE, FALSE, 0);
100     gtk_box_pack_end(GTK_BOX(guesses_box), guesses, FALSE, FALSE, 0);
101 
102     gtk_box_pack_start(GTK_BOX(ncorr_box),
103                        gtk_label_new
104                        (g_strdup_printf
105                         ("Number of correct decisions: %d", ncorr)),
106                        FALSE, FALSE, 0);
107 
108     gtk_box_pack_start(GTK_BOX(pval_box),
109                        gtk_label_new
110                        (g_strdup_printf
111                         ("p-value: %f", calculate_p_value())),
112                        FALSE, FALSE, 0);
113 
114     gtk_box_pack_start(GTK_BOX(close_box),
115                        close_button, TRUE, FALSE, 0);
116 
117     main_box = gtk_vbox_new(FALSE, 5);
118     gtk_container_set_border_width(GTK_CONTAINER(main_box), 10);
119     gtk_box_pack_start(GTK_BOX(main_box), answers_box, TRUE, TRUE, 0);
120     gtk_box_pack_start(GTK_BOX(main_box), guesses_box, TRUE, TRUE, 0);
121     gtk_box_pack_start(GTK_BOX(main_box), ncorr_box, TRUE, TRUE, 0);
122     gtk_box_pack_start(GTK_BOX(main_box), pval_box, TRUE, TRUE, 0);
123     gtk_box_pack_start(GTK_BOX(main_box), close_box, TRUE, TRUE, 0);
124 
125     gtk_container_add(GTK_CONTAINER(window), main_box);
126     gtk_widget_show_all(window);
127 }
128 
129 /*
130  * Hide and destroy result window.
131  */
132 void
hide_result_window(void)133 hide_result_window(void)
134 {
135     if (window) {
136         gtk_widget_destroy(window);
137         window = NULL;
138     }
139 }
140