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