1 /* newtestwin.c - window for creating a new test
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 *window;
25 static GtkWidget *main_box;
26 static GtkWidget *first_file_selector;
27 static GtkWidget *second_file_selector;
28 static GtkWidget *first_file_box;
29 static GtkWidget *second_file_box;
30 static GtkWidget *file_box;
31 static GtkObject *adjustment;
32 static GtkWidget *spinner;
33 static GtkWidget *spinner_box;
34 static GtkWidget *ok;
35 static GtkWidget *cancel;
36 static GtkWidget *button_box;
37 
38 /*
39  * Try to initialize a new test.
40  *
41  * Destroy window if test setup was successful.  Otherwise display
42  * error dialog.
43  */
44 static void
init_new_test(void)45 init_new_test(void)
46 {
47     char *a;
48     char *b;
49     char *s;
50     int testsucc;
51 
52     a = gtk_file_chooser_get_filename
53         (GTK_FILE_CHOOSER(first_file_selector));
54     b = gtk_file_chooser_get_filename
55         (GTK_FILE_CHOOSER(second_file_selector));
56 
57     if (a == NULL) {
58         error_dialog(window, "You have not chosen sample file A.");
59         return;
60     }
61 
62     if (b == NULL) {
63         error_dialog(window, "You have not chosen sample file B.");
64         return;
65     }
66 
67     testsucc = init_test(a, b, gtk_spin_button_get_value_as_int
68                          (GTK_SPIN_BUTTON(spinner)), -1);
69 
70     switch (testsucc) {
71     case 0:
72         gtk_widget_destroy(window);
73         window = NULL;
74         reset_test();
75         break;
76     case 1:
77         b = a;                  /* allow uniform error handling */
78     case 2:
79         s = g_strdup_printf("An error occurred while setting up "
80                             "playback for sample '%s'.", b);
81         error_dialog(window, s);
82         g_free(s);
83         break;
84     case 3:
85         error_dialog(window,
86                      "The chosen samples have different duration.  "
87                      "Both samples must have exactly the same duration.");
88         break;
89     case 4:
90         error_dialog(window, "Invalid number of trials.");
91         break;
92     }
93 }
94 
95 static gboolean
delete_event_handler(GtkWidget * widget,GdkEvent * event,gpointer data)96 delete_event_handler(GtkWidget *widget, GdkEvent *event, gpointer data)
97 {
98     window = NULL;
99     return FALSE;
100 }
101 
102 static void
button_clicked(GtkWidget * button,gpointer data)103 button_clicked(GtkWidget *button, gpointer data)
104 {
105     if (button == cancel) {
106         gtk_widget_destroy(window);
107         window = NULL;
108     } else if (button == ok) {
109         init_new_test();
110     }
111 }
112 
113 static void
create_new_test_window(void)114 create_new_test_window(void)
115 {
116     GtkWidget *sample_a_label = gtk_label_new_with_mnemonic("Sample _A:");
117     GtkWidget *sample_b_label = gtk_label_new_with_mnemonic("Sample _B:");
118     GtkWidget *trials_label = (gtk_label_new_with_mnemonic
119                                ("Number of _trials:"));
120 
121     /* create filename boxes and selector */
122     first_file_selector = (gtk_file_chooser_button_new
123                            ("Choose audio sample A",
124                             GTK_FILE_CHOOSER_ACTION_OPEN));
125     second_file_selector = (gtk_file_chooser_button_new
126                             ("Choose audio sample B",
127                              GTK_FILE_CHOOSER_ACTION_OPEN));
128     gtk_label_set_mnemonic_widget(GTK_LABEL(sample_a_label),
129                                   first_file_selector);
130     gtk_label_set_mnemonic_widget(GTK_LABEL(sample_b_label),
131                                   second_file_selector);
132 
133     first_file_box = gtk_hbox_new(FALSE, 5);
134     gtk_box_pack_start(GTK_BOX(first_file_box), sample_a_label,
135                        FALSE, FALSE, 0);
136     gtk_box_pack_start(GTK_BOX(first_file_box),
137                        GTK_WIDGET(first_file_selector), TRUE, TRUE, 0);
138     second_file_box = gtk_hbox_new(FALSE, 5);
139     gtk_box_pack_start(GTK_BOX(second_file_box), sample_b_label,
140                        FALSE, FALSE, 0);
141     gtk_box_pack_start(GTK_BOX(second_file_box),
142                        second_file_selector, TRUE, TRUE, 0);
143     file_box = gtk_vbox_new(FALSE, 5);
144     gtk_box_pack_start(GTK_BOX(file_box),
145                        first_file_box, TRUE, TRUE, 0);
146     gtk_box_pack_start(GTK_BOX(file_box),
147                        second_file_box, TRUE, TRUE, 0);
148 
149     /* trials */
150     adjustment = gtk_adjustment_new(1, 1, 50, 1.0, 10.0, 0);
151     spinner = gtk_spin_button_new(GTK_ADJUSTMENT(adjustment), 1, 0);
152     gtk_label_set_mnemonic_widget(GTK_LABEL(trials_label),
153                                   spinner);
154     spinner_box = gtk_hbox_new(FALSE, 5);
155     gtk_box_pack_start(GTK_BOX(spinner_box), trials_label,
156                        TRUE, TRUE, 0);
157     gtk_box_pack_start(GTK_BOX(spinner_box), spinner, TRUE, TRUE, 0);
158 
159     /* buttons */
160     ok = gtk_button_new_from_stock(GTK_STOCK_OK);
161     cancel = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
162     button_box = gtk_hbox_new(TRUE, 5);
163     gtk_box_pack_start(GTK_BOX(button_box), ok, TRUE, TRUE, 0);
164     gtk_box_pack_start(GTK_BOX(button_box), cancel, TRUE, TRUE, 0);
165     g_signal_connect(ok, "clicked", G_CALLBACK(button_clicked), NULL);
166     g_signal_connect(cancel, "clicked", G_CALLBACK(button_clicked),
167                      NULL);
168 
169     /* main window */
170     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
171     gtk_window_set_title(GTK_WINDOW(window), "New test");
172     main_box = gtk_vbox_new(FALSE, 5);
173     gtk_container_set_border_width(GTK_CONTAINER(main_box), 10);
174     gtk_box_pack_start(GTK_BOX(main_box), file_box, TRUE, TRUE, 0);
175     gtk_box_pack_start(GTK_BOX(main_box), spinner_box, TRUE, TRUE, 5);
176     gtk_box_pack_start(GTK_BOX(main_box), button_box, TRUE, TRUE, 0);
177     g_signal_connect(G_OBJECT(window), "delete_event",
178                      G_CALLBACK(delete_event_handler), NULL);
179     gtk_container_add(GTK_CONTAINER(window), main_box);
180 }
181 
182 void
show_new_test_window(void)183 show_new_test_window(void)
184 {
185     if (!window) {
186         create_new_test_window();
187         gtk_widget_show_all(window);
188     } else {
189         gtk_window_present(GTK_WINDOW(window));
190     }
191 }
192