2692e4880c3607948e9aed64d7b03c0a72469755
[pspp-builds.git] / src / ui / gui / univariate-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2011  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 <gtk/gtk.h>
20 #include <ui/gui/helper.h>
21 #include "psppire-dialog.h"
22 #include "dict-display.h"
23
24 #include "psppire-var-view.h"
25 #include "psppire-selector.h"
26 #include "psppire-dictview.h"
27 #include <ui/gui/dialog-common.h>
28
29 #include "executor.h"
30
31 #include "univariate-dialog.h"
32
33 struct uni_dialog
34 {
35   struct dictionary *dict;
36
37   /* Entry box for the dependent variable */
38   GtkWidget *dep_entry;
39
40   GtkWidget *factor_list;
41 };
42
43
44 /* Dialog is valid iff at least one variable has been selected in both
45    the dependent variable box and the factor list. */
46 static gboolean
47 dialog_state_valid (gpointer data)
48 {
49   struct uni_dialog *uv_d = data;
50   GtkTreeModel *vars;
51   GtkTreeIter notused;
52
53   if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (uv_d->dep_entry))))
54     return false;
55
56   vars =
57     gtk_tree_view_get_model (GTK_TREE_VIEW (uv_d->factor_list));
58
59   return gtk_tree_model_get_iter_first (vars, &notused);
60 }
61
62 /* Reset the dialog to its default state */
63 static void
64 refresh (PsppireDialog *dialog, struct uni_dialog *uv_d)
65 {
66   GtkTreeModel *liststore ;
67
68   gtk_entry_set_text (GTK_ENTRY (uv_d->dep_entry), "");
69
70   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (uv_d->factor_list));
71
72   gtk_list_store_clear (GTK_LIST_STORE (liststore));
73 }
74
75
76 static char *
77 generate_syntax (const struct uni_dialog *uvd)
78 {
79   gchar *text = NULL;
80   GString *str = g_string_new ("GLM ");
81
82
83   g_string_append (str, gtk_entry_get_text (GTK_ENTRY (uvd->dep_entry)));
84   
85
86   g_string_append (str, " BY ");
87
88   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (uvd->factor_list), 0, str);
89
90
91   g_string_append (str, ".");
92
93   text = str->str;
94
95   g_string_free (str, FALSE);
96
97   return text;
98 }
99
100 void 
101 univariate_dialog (PsppireDataWindow * de)
102 {
103   struct uni_dialog uv_d;
104
105   gint response;
106
107   GtkBuilder *xml = builder_new ("univariate.ui");
108
109   GtkWidget *dialog = get_widget_assert   (xml, "univariate-dialog");
110   GtkWidget *source = get_widget_assert   (xml, "dict-view");
111
112   GtkWidget *dep_selector = get_widget_assert (xml, "dep-selector");
113   GtkWidget *factor_selector = get_widget_assert (xml, "factor-selector");
114
115
116   PsppireVarStore *vs = NULL;
117
118   uv_d.dep_entry = get_widget_assert (xml, "dep-entry");
119   uv_d.factor_list = get_widget_assert (xml, "factors-view");
120
121   g_object_get (de->data_editor, "var-store", &vs, NULL);
122
123   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
124   g_signal_connect (dialog, "refresh", G_CALLBACK (refresh),  &uv_d);
125
126   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
127                                       dialog_state_valid, &uv_d);
128
129
130   g_object_get (vs, "dictionary", &uv_d.dict, NULL);
131   g_object_set (source, "model", uv_d.dict, NULL);
132
133   psppire_selector_set_allow (PSPPIRE_SELECTOR (dep_selector),
134                               numeric_only);
135
136   psppire_selector_set_filter_func (PSPPIRE_SELECTOR (dep_selector),
137                                     is_currently_in_entry);
138
139
140   psppire_selector_set_filter_func (PSPPIRE_SELECTOR (factor_selector),
141                                     is_currently_in_varview);
142
143   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
144
145
146   switch (response)
147     {
148     case GTK_RESPONSE_OK:
149       g_free (execute_syntax_string (de, generate_syntax (&uv_d)));
150       break;
151     case PSPPIRE_RESPONSE_PASTE:
152       g_free (paste_syntax_to_window (generate_syntax (&uv_d)));
153       break;
154     default:
155       break;
156     }
157
158   g_object_unref (xml);
159 }
160
161