Add "dictionary" property to PsppireVarStore and use it.
[pspp-builds.git] / src / ui / gui / reliability-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009  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 #include <config.h>
18
19 #include "dialog-common.h"
20 #include <language/syntax-string-source.h>
21 #include "reliability-dialog.h"
22 #include "psppire-selector.h"
23 #include "psppire-dictview.h"
24 #include "psppire-dialog.h"
25
26 #include "psppire-data-window.h"
27
28 #include "executor.h"
29 #include "helper.h"
30
31 #include <gtk/gtk.h>
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35 #define N_(msgid) msgid
36
37
38 struct reliability
39 {
40   PsppireDict *dict;
41   GtkWidget *model_combo;
42   GtkWidget *variables;
43   GtkWidget *split_point_hbox;
44   GtkWidget *split_spinbutton;
45 };
46
47
48 static char * generate_syntax (const struct reliability *rd);
49
50
51 static void
52 on_vars_changed (struct reliability *rd)
53 {
54   GtkTreeModel *tm =
55     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
56
57   gint n_vars = gtk_tree_model_iter_n_children (tm, NULL);
58
59   gint current_value =
60     gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (rd->split_spinbutton));
61
62   gint new_value = current_value;
63
64   gtk_spin_button_set_range (GTK_SPIN_BUTTON (rd->split_spinbutton),
65                              0, n_vars - 1);
66
67   if ( current_value > n_vars - 1)
68     new_value = n_vars - 1;
69
70   gtk_spin_button_set_value (GTK_SPIN_BUTTON (rd->split_spinbutton),
71                              new_value);
72 }
73
74 static void
75 on_method_change (struct reliability *rd)
76 {
77   gtk_widget_set_sensitive (rd->split_point_hbox,
78                             ( 1 == gtk_combo_box_get_active (GTK_COMBO_BOX (rd->model_combo))));
79
80 }
81
82 static void
83 refresh (PsppireDialog *dialog, struct reliability *rd)
84 {
85   GtkTreeModel *liststore =
86     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
87   gtk_list_store_clear (GTK_LIST_STORE (liststore));
88
89   gtk_combo_box_set_active (GTK_COMBO_BOX (rd->model_combo), 0);
90
91   gtk_spin_button_set_value (GTK_SPIN_BUTTON (rd->split_spinbutton), 0);
92
93   gtk_spin_button_set_range (GTK_SPIN_BUTTON (rd->split_spinbutton),
94                              0, 0);
95 }
96
97
98 static gboolean
99 dialog_state_valid (gpointer data)
100 {
101   struct reliability *rd = data;
102
103   GtkTreeModel *liststore =
104     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
105
106   return (2 <= gtk_tree_model_iter_n_children (liststore, NULL));
107 }
108
109
110 /* Pops up the Reliability dialog box */
111 void
112 reliability_dialog (GObject *o, gpointer data)
113 {
114   struct reliability rd;
115   gint response;
116
117   GtkBuilder *xml = builder_new ("reliability.ui");
118   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
119   PsppireVarStore *vs;
120
121   GtkWidget *dialog = get_widget_assert   (xml, "reliability-dialog");
122   GtkWidget *source = get_widget_assert   (xml, "dict-view");
123
124   GtkWidget *selector = get_widget_assert (xml, "psppire-selector1");
125
126   rd.split_point_hbox = get_widget_assert (xml, "split-point-hbox");
127
128   rd.variables = get_widget_assert   (xml, "treeview2");
129
130   rd.model_combo = get_widget_assert   (xml, "combobox1");
131   rd.split_spinbutton = get_widget_assert (xml, "spinbutton1");
132
133   g_signal_connect_swapped (rd.model_combo, "changed",
134                             G_CALLBACK (on_method_change), &rd);
135
136   g_object_get (de->data_editor, "var-store", &vs, NULL);
137
138   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
139
140   g_object_get (vs, "dictionary", &rd.dict, NULL);
141   g_object_set (source, "dictionary", rd.dict, NULL);
142
143   set_dest_model (GTK_TREE_VIEW (rd.variables), rd.dict);
144
145   psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
146                                  source,
147                                  rd.variables,
148                                  insert_source_row_into_tree_view,
149                                  NULL,
150                                  NULL);
151
152   {
153     GtkTreeModel *tm =
154       gtk_tree_view_get_model (GTK_TREE_VIEW (rd.variables));
155
156
157     g_signal_connect_swapped (tm, "row-inserted",
158                       G_CALLBACK (on_vars_changed), &rd);
159
160     g_signal_connect_swapped (tm, "row-deleted",
161                       G_CALLBACK (on_vars_changed), &rd);
162   }
163
164   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &rd);
165
166   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
167                                       dialog_state_valid, &rd);
168
169   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
170
171
172   switch (response)
173     {
174     case GTK_RESPONSE_OK:
175       {
176         gchar *syntax = generate_syntax (&rd);
177
178         struct getl_interface *sss = create_syntax_string_source (syntax);
179         execute_syntax (sss);
180
181         g_free (syntax);
182       }
183       break;
184     case PSPPIRE_RESPONSE_PASTE:
185       {
186         gchar *syntax = generate_syntax (&rd);
187         paste_syntax_in_new_window (syntax);
188
189         g_free (syntax);
190       }
191       break;
192     default:
193       break;
194     }
195
196   g_object_unref (xml);
197 }
198
199
200 \f
201
202 static char *
203 generate_syntax (const struct reliability *rd)
204 {
205   gchar *text;
206   GString *string = g_string_new ("RELIABILITY");
207
208   g_string_append (string, "\n\t/VARIABLES=");
209   append_variable_names (string, rd->dict, GTK_TREE_VIEW (rd->variables), 0);
210
211
212   g_string_append (string, "\n\t/MODEL=");
213
214   if ( 0 == gtk_combo_box_get_active (GTK_COMBO_BOX (rd->model_combo)))
215     g_string_append (string, "ALPHA");
216   else
217     g_string_append_printf (string, "SPLIT (%d)",
218                             gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (rd->split_spinbutton))
219                             );
220
221   g_string_append (string, ".\n");
222
223   text = string->str;
224
225   g_string_free (string, FALSE);
226
227   return text;
228 }