1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007 Free Software Foundation
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.
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.
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/>. */
19 #include "dialog-common.h"
21 #include "psppire-var-ptr.h"
26 /* Append the names of selected variables to STRING.
27 TREEVIEW is the treeview containing the variables.
28 COLUMN is the column in the treeview containing the variables.
29 DICT is the dictionary for those variables.
32 append_variable_names (GString *string,
33 PsppireDict *dict, GtkTreeView *treeview, gint column)
38 GtkTreeModel *list_store =
39 gtk_tree_view_get_model (treeview);
41 if ( gtk_tree_model_get_iter_first (list_store, &iter) )
46 struct variable *var = NULL;
47 GtkTreePath *path = gtk_tree_model_get_path (list_store, &iter);
49 gtk_tree_model_get_value (list_store, &iter, column, &value);
51 /* FIXME: G_TYPE_INT should be deprecated.
52 As well as being simpler, it'd be unecessary to pass dict */
53 if ( G_VALUE_TYPE (&value) == G_TYPE_INT )
54 var = psppire_dict_get_variable (dict, g_value_get_int (&value));
56 else if ( G_VALUE_TYPE (&value) == PSPPIRE_VAR_PTR_TYPE)
57 var = g_value_get_boxed (&value);
60 g_critical ("Unsupported type \"%s\", in variable name treeview.",
61 G_VALUE_TYPE_NAME (&value));
63 g_value_unset (&value);
65 g_string_append (string, " ");
66 g_string_append (string, var_get_name (var));
68 gtk_tree_path_free (path);
71 while (gtk_tree_model_iter_next (list_store, &iter));
80 get_selected_variable (GtkTreeModel *treemodel,
87 GtkTreePath *path = gtk_tree_model_get_path (treemodel, iter);
89 gtk_tree_model_get_value (treemodel, iter, 0, &value);
91 gtk_tree_path_free (path);
93 var = psppire_dict_get_variable (dict, g_value_get_int (&value));
95 g_value_unset (&value);
103 /* A (*GtkTreeCellDataFunc) function.
104 This function expects TREEMODEL to hold G_TYPE_INT. The ints it holds
105 are the indices of the variables in the dictionary, which DATA points to.
106 It renders the name of the variable into CELL.
109 cell_var_name (GtkTreeViewColumn *tree_column,
110 GtkCellRenderer *cell,
111 GtkTreeModel *tree_model,
115 PsppireDict *dict = data;
116 struct variable *var;
119 var = get_selected_variable (tree_model, iter, dict);
121 name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL);
122 g_object_set (cell, "text", name, NULL);
128 /* Set a model for DEST, which is an GtkListStore of g_int's
129 whose values are the indices into DICT */
131 set_dest_model (GtkTreeView *dest, PsppireDict *dict)
133 GtkTreeViewColumn *col;
134 GtkListStore *dest_list = gtk_list_store_new (1, G_TYPE_INT);
135 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
137 gtk_tree_view_set_model (GTK_TREE_VIEW (dest), GTK_TREE_MODEL (dest_list));
139 col = gtk_tree_view_column_new_with_attributes ("Var",
145 gtk_tree_view_column_set_cell_data_func (col, renderer,
149 /* FIXME: make this a value in terms of character widths */
150 g_object_set (col, "min-width", 100, NULL);
152 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
154 gtk_tree_view_append_column (GTK_TREE_VIEW (dest), col);
159 /* Returns FALSE if the variables represented by the union of the rows
160 currently selected by SOURCE widget, and contents of the DEST
161 widget, are of different types.
163 In other words, this function when passed as the argument to
164 psppire_selector_set_allow, ensures that the selector selects only
165 string variables, or only numeric variables, not a mixture.
168 homogeneous_types (GtkWidget *source, GtkWidget *dest)
172 gboolean retval = TRUE;
174 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (source));
177 GtkTreeSelection *selection;
178 enum val_type type = -1;
181 while (GTK_IS_TREE_MODEL_FILTER (model))
183 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
186 dict = PSPPIRE_DICT (model);
188 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (source));
190 list = gtk_tree_selection_get_selected_rows (selection, &model);
192 /* Iterate through the selection of the source treeview */
193 for (l = list; l ; l = l->next)
195 GtkTreePath *path = l->data;
199 const struct variable *v;
201 fpath = gtk_tree_model_filter_convert_path_to_child_path (GTK_TREE_MODEL_FILTER (model), path);
203 idx = gtk_tree_path_get_indices (fpath);
205 v = psppire_dict_get_variable (dict, idx[0]);
209 if ( var_get_type (v) != type )
216 type = var_get_type (v);
219 g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);
222 if ( retval == FALSE )
225 /* now deal with the dest widget */
226 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dest));
228 for (ok = gtk_tree_model_get_iter_first (model, &iter);
230 ok = gtk_tree_model_iter_next (model, &iter))
233 const struct variable *v;
234 gtk_tree_model_get (model, &iter, 0, &idx, -1);
236 v = psppire_dict_get_variable (dict, idx);
240 if ( var_get_type (v) != type )
247 type = var_get_type (v);
256 /* Returns true iff the variable selected by SOURCE is numeric */
258 numeric_only (GtkWidget *source, GtkWidget *dest)
260 gboolean retval = TRUE;
262 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (source));
265 GtkTreeSelection *selection;
268 while (GTK_IS_TREE_MODEL_FILTER (model))
270 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
273 dict = PSPPIRE_DICT (model);
275 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (source));
277 list = gtk_tree_selection_get_selected_rows (selection, &model);
279 /* Iterate through the selection of the source treeview */
280 for (l = list; l ; l = l->next)
282 GtkTreePath *path = l->data;
286 const struct variable *v;
288 fpath = gtk_tree_model_filter_convert_path_to_child_path
289 (GTK_TREE_MODEL_FILTER (model), path);
291 idx = gtk_tree_path_get_indices (fpath);
293 v = psppire_dict_get_variable (dict, idx[0]);
295 if ( var_is_alpha (v))
302 g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);