7edbbe504920a594e22bba139dba576f9e45b213
[pspp-builds.git] / src / ui / gui / dialog-common.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007  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 <libpspp/i18n.h>
20 #include "dialog-common.h"
21
22 #include "psppire-var-ptr.h"
23
24 #include "helper.h"
25
26 /* A (*GtkTreeCellDataFunc) function.
27    This function expects TREEMODEL to hold G_TYPE_BOXED, which is a pointer to a variable.
28    It renders the name of the variable into CELL.
29 */
30 void
31 XXX_cell_var_name (GtkTreeViewColumn *tree_column,
32                    GtkCellRenderer *cell,
33                    GtkTreeModel *treemodel,
34                    GtkTreeIter *iter,
35                    gpointer data)
36 {
37   const struct variable *var;
38   GValue value = {0};
39
40   GtkTreePath *path = gtk_tree_model_get_path (treemodel, iter);
41
42   gtk_tree_model_get_value (treemodel, iter, 0, &value);
43
44   gtk_tree_path_free (path);
45
46   var = g_value_get_boxed (&value);
47
48   g_value_unset (&value);
49
50   g_object_set (cell, "text", var_get_name (var), NULL);
51 }
52
53
54
55 /* Returns FALSE if the variables represented by the union of the rows
56    currently selected by SOURCE widget, and contents of the DEST
57    widget, are of different types.
58
59    In other words, this function when passed as the argument to
60    psppire_selector_set_allow, ensures that the selector selects only
61    string  variables, or only numeric variables, not a mixture.
62 */
63 gboolean
64 homogeneous_types (GtkWidget *source, GtkWidget *dest)
65 {
66   gboolean ok;
67   GtkTreeIter iter;
68   gboolean retval = TRUE;
69
70   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (source));
71
72   PsppireDict *dict;
73   GtkTreeSelection *selection;
74   enum val_type type = -1;
75   GList *list, *l;
76
77   while (GTK_IS_TREE_MODEL_FILTER (model))
78     {
79       model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
80     }
81
82   dict = PSPPIRE_DICT (model);
83
84   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (source));
85
86   list = gtk_tree_selection_get_selected_rows (selection, &model);
87
88   /* Iterate through the selection of the source treeview */
89   for (l = list; l ; l = l->next)
90     {
91       GtkTreePath *path = l->data;
92
93       GtkTreePath *fpath =
94         gtk_tree_model_filter_convert_path_to_child_path (GTK_TREE_MODEL_FILTER (model), path);
95
96       gint *idx = gtk_tree_path_get_indices (fpath);
97
98       const struct variable *v = psppire_dict_get_variable (dict, idx[0]);
99
100       gtk_tree_path_free (fpath);
101
102       if ( type != -1 )
103         {
104           if ( var_get_type (v) != type )
105             {
106               retval = FALSE;
107               break;
108             }
109         }
110
111       type = var_get_type (v);
112     }
113
114   g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);
115   g_list_free (list);
116
117   if ( retval == FALSE )
118     return FALSE;
119
120   /* now deal with the dest widget */
121   model = gtk_tree_view_get_model (GTK_TREE_VIEW (dest));
122
123   for (ok = gtk_tree_model_get_iter_first (model, &iter);
124        ok;
125        ok = gtk_tree_model_iter_next (model, &iter))
126     {
127       const struct variable *v;
128       gtk_tree_model_get (model, &iter, 0, &v, -1);
129
130       if ( type != -1 )
131         {
132           if ( var_get_type (v) != type )
133             {
134               retval = FALSE;
135               break;
136             }
137         }
138
139       type = var_get_type (v);
140     }
141
142   return retval;
143 }
144
145
146
147 /* Returns true iff the variable selected by SOURCE is numeric */
148 gboolean
149 numeric_only (GtkWidget *source, GtkWidget *dest)
150 {
151   gboolean retval = TRUE;
152
153   GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (source));
154
155   PsppireDict *dict;
156   GtkTreeSelection *selection;
157   GList *list, *l;
158
159   while (GTK_IS_TREE_MODEL_FILTER (model))
160     {
161       model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
162     }
163
164   dict = PSPPIRE_DICT (model);
165
166   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (source));
167
168   list = gtk_tree_selection_get_selected_rows (selection, &model);
169
170   /* Iterate through the selection of the source treeview */
171   for (l = list; l ; l = l->next)
172     {
173       GtkTreePath *path = l->data;
174       GtkTreePath *fpath = gtk_tree_model_filter_convert_path_to_child_path
175         (GTK_TREE_MODEL_FILTER (model), path);
176
177       gint *idx = gtk_tree_path_get_indices (fpath);
178
179       const struct variable *v = psppire_dict_get_variable (dict, idx[0]);
180
181       gtk_tree_path_free (fpath);
182
183       if ( var_is_alpha (v))
184         {
185           retval = FALSE;
186           break;
187         }
188     }
189
190   g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);
191   g_list_free (list);
192
193   return retval;
194 }
195