Change some instances of GtkAction to PsppireDialogAction
[pspp] / src / ui / gui / psppire-dialog-action-examine.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2012  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 #include <config.h>
19
20 #include "psppire-dialog-action-examine.h"
21
22 #include "psppire-var-view.h"
23 #include "dialog-common.h"
24 #include "psppire-selector.h"
25 #include "psppire-dict.h"
26 #include "psppire-dialog.h"
27 #include "builder-wrapper.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33 static void psppire_dialog_action_examine_class_init      (PsppireDialogActionExamineClass *class);
34
35 G_DEFINE_TYPE (PsppireDialogActionExamine, psppire_dialog_action_examine, PSPPIRE_TYPE_DIALOG_ACTION);
36
37
38 #define     STAT_DESCRIPTIVES  0x01
39 #define     STAT_EXTREMES      0x02
40 #define     STAT_PERCENTILES   0x04
41
42 static void
43 run_stats_dialog (PsppireDialogActionExamine *ed)
44 {
45   gint response;
46
47   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->descriptives_button),
48                                 ed->stats & STAT_DESCRIPTIVES);
49
50   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->extremes_button),
51                                 ed->stats & STAT_EXTREMES);
52
53   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->percentiles_button),
54                                 ed->stats & STAT_PERCENTILES);
55
56   response = psppire_dialog_run (PSPPIRE_DIALOG (ed->stats_dialog));
57
58   if ( response == PSPPIRE_RESPONSE_CONTINUE )
59     {
60       ed->stats = 0;
61       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->descriptives_button) ))
62         ed->stats |= STAT_DESCRIPTIVES;
63
64       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->extremes_button) ))
65         ed->stats |= STAT_EXTREMES;
66
67       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->percentiles_button) ))
68         ed->stats |= STAT_PERCENTILES;
69     }
70 }
71
72 static void
73 run_opts_dialog (PsppireDialogActionExamine *ed)
74 {
75   gint response;
76
77   switch (ed->opts)
78     {
79     case OPT_LISTWISE:
80       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->listwise), TRUE);
81       break;
82     case OPT_PAIRWISE:
83       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->pairwise), TRUE);
84       break;
85     case OPT_REPORT:
86       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ed->report), TRUE);
87       break;
88     default:
89       g_assert_not_reached ();
90       break;
91     };
92
93   response = psppire_dialog_run (PSPPIRE_DIALOG (ed->opts_dialog));
94
95   if ( response == PSPPIRE_RESPONSE_CONTINUE )
96     {
97       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->listwise)))
98         ed->opts = OPT_LISTWISE;
99       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->pairwise)))
100         ed->opts = OPT_PAIRWISE;
101       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (ed->report)))
102         ed->opts = OPT_REPORT;
103   }
104 }
105
106
107
108
109 static char *
110 generate_syntax (PsppireDialogAction *act)
111 {
112   PsppireDialogActionExamine *ed  = PSPPIRE_DIALOG_ACTION_EXAMINE (act);
113
114   const char *label;
115   gchar *text = NULL;
116   GString *str = g_string_new ("EXAMINE ");
117
118   g_string_append (str, "\n\t/VARIABLES=");
119   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (ed->variables), 0, str);
120
121   if ( 0  < gtk_tree_model_iter_n_children
122        (gtk_tree_view_get_model (GTK_TREE_VIEW (ed->factors)), NULL))
123     {
124       g_string_append (str, "\n\tBY ");
125       psppire_var_view_append_names (PSPPIRE_VAR_VIEW (ed->factors), 0, str);
126     }
127
128   label = gtk_entry_get_text (GTK_ENTRY (ed->id_var));
129   if ( 0 != strcmp (label, "") )
130     {
131       g_string_append (str, "\n\t/ID = ");
132       g_string_append (str, label);
133     }
134
135   if ( ed->stats & (STAT_DESCRIPTIVES | STAT_EXTREMES))
136     {
137       g_string_append (str, "\n\t/STATISTICS =");
138
139       if ( ed->stats & STAT_DESCRIPTIVES)
140         g_string_append (str, " DESCRIPTIVES");
141
142       if ( ed->stats & STAT_EXTREMES)
143         g_string_append (str, " EXTREME");
144     }
145
146   if ( ed->stats & STAT_PERCENTILES)
147     g_string_append (str, "\n\t/PERCENTILES");
148
149
150   g_string_append (str, "\n\t/MISSING=");
151   switch (ed->opts)
152     {
153     case OPT_REPORT:
154       g_string_append (str, "REPORT");
155       break;
156     case OPT_PAIRWISE:
157       g_string_append (str, "PAIRWISE");
158       break;
159     default:
160       g_string_append (str, "LISTWISE");
161       break;
162     };
163
164   g_string_append (str, ".");
165   text = str->str;
166
167   g_string_free (str, FALSE);
168
169   return text;
170 }
171
172 static gboolean
173 dialog_state_valid (PsppireDialogAction *da)
174 {
175   PsppireDialogActionExamine *pae  = PSPPIRE_DIALOG_ACTION_EXAMINE (da);
176   GtkTreeIter notused;
177   GtkTreeModel *vars =
178     gtk_tree_view_get_model (GTK_TREE_VIEW (pae->variables));
179
180   return gtk_tree_model_get_iter_first (vars, &notused);
181 }
182
183 static void
184 dialog_refresh (PsppireDialogAction *da)
185 {
186   PsppireDialogActionExamine *dae  = PSPPIRE_DIALOG_ACTION_EXAMINE (da);
187   GtkTreeModel *liststore = NULL;
188
189   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (dae->variables));
190   gtk_list_store_clear (GTK_LIST_STORE (liststore));
191
192   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (dae->factors));
193   gtk_list_store_clear (GTK_LIST_STORE (liststore));
194
195   gtk_entry_set_text (GTK_ENTRY (dae->id_var), "");
196   dae->stats = 0x00;
197   dae->opts = OPT_LISTWISE;
198 }
199
200 static void
201 psppire_dialog_action_examine_activate (PsppireDialogAction *a)
202 {
203   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
204   PsppireDialogActionExamine *act = PSPPIRE_DIALOG_ACTION_EXAMINE (a);
205
206   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
207   GtkBuilder *xml = g_hash_table_lookup (thing, a);
208   if (!xml)
209     {
210       xml = builder_new ("examine.ui");
211       g_hash_table_insert (thing, a, xml);
212     }
213
214   GtkWidget *stats_button = get_widget_assert (xml, "stats-button");
215   GtkWidget *opts_button = get_widget_assert (xml, "opts-button");
216
217   GtkWidget *dep_sel = get_widget_assert (xml, "psppire-selector1");
218   GtkWidget *dep_sel2 = get_widget_assert (xml, "psppire-selector2");
219   GtkWidget *dep_sel3 = get_widget_assert (xml, "psppire-selector3");
220   GtkWidget *table = get_widget_assert (xml, "table1");
221
222   pda->dialog    = get_widget_assert   (xml, "examine-dialog");
223   pda->source    = get_widget_assert   (xml, "treeview1");
224   act->variables = get_widget_assert   (xml, "treeview2");
225   act->factors   = get_widget_assert   (xml, "treeview3");
226   act->id_var    = get_widget_assert   (xml, "entry1");
227
228   /* Setting the focus chain like this is a pain.
229      But the default focus order seems to be somewhat odd. */
230   GList *list = NULL;
231   list = g_list_append (list, get_widget_assert (xml, "scrolledwindow1"));
232   list = g_list_append (list, dep_sel);
233   list = g_list_append (list, get_widget_assert (xml, "frame1"));
234   list = g_list_append (list, dep_sel2);
235   list = g_list_append (list, get_widget_assert (xml, "frame2"));
236   list = g_list_append (list, dep_sel3);
237   list = g_list_append (list, get_widget_assert (xml, "frame3"));
238   gtk_container_set_focus_chain (GTK_CONTAINER (table), list);
239
240
241   act->stats_dialog        = get_widget_assert (xml, "statistics-dialog");
242   act->descriptives_button = get_widget_assert (xml, "descriptives-button");
243   act->extremes_button     = get_widget_assert (xml, "extremes-button"); 
244   act->percentiles_button  = get_widget_assert (xml, "percentiles-button");
245
246   act->opts_dialog = get_widget_assert (xml, "options-dialog");
247   act->listwise    = get_widget_assert (xml, "radiobutton1");
248   act->pairwise    = get_widget_assert (xml, "radiobutton2");
249   act->report      = get_widget_assert (xml, "radiobutton3");
250
251   psppire_selector_set_allow (PSPPIRE_SELECTOR (dep_sel), numeric_only);
252
253   psppire_dialog_action_set_valid_predicate (pda, (void *) dialog_state_valid);
254   psppire_dialog_action_set_refresh (pda, dialog_refresh);
255
256   g_signal_connect_swapped (stats_button, "clicked",
257                     G_CALLBACK (run_stats_dialog), act);
258
259   g_signal_connect_swapped (opts_button, "clicked",
260                             G_CALLBACK (run_opts_dialog), act);
261  
262   PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_examine_parent_class)->activate (pda);
263
264   g_list_free (list);
265 }
266
267 static void
268 psppire_dialog_action_examine_class_init (PsppireDialogActionExamineClass *class)
269 {
270   psppire_dialog_action_set_activation (class, psppire_dialog_action_examine_activate);
271
272   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
273 }
274
275 static void
276 psppire_dialog_action_examine_init (PsppireDialogActionExamine *act)
277 {
278 }