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;
198 gtk_tree_model_filter_convert_path_to_child_path (GTK_TREE_MODEL_FILTER (model), path);
200 gint *idx = gtk_tree_path_get_indices (fpath);
202 const struct variable *v = psppire_dict_get_variable (dict, idx[0]);
204 gtk_tree_path_free (fpath);
208 if ( var_get_type (v) != type )
215 type = var_get_type (v);
218 g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);
221 if ( retval == FALSE )
224 /* now deal with the dest widget */
225 model = gtk_tree_view_get_model (GTK_TREE_VIEW (dest));
227 for (ok = gtk_tree_model_get_iter_first (model, &iter);
229 ok = gtk_tree_model_iter_next (model, &iter))
232 const struct variable *v;
233 gtk_tree_model_get (model, &iter, 0, &idx, -1);
235 v = psppire_dict_get_variable (dict, idx);
239 if ( var_get_type (v) != type )
246 type = var_get_type (v);
255 /* Returns true iff the variable selected by SOURCE is numeric */
257 numeric_only (GtkWidget *source, GtkWidget *dest)
259 gboolean retval = TRUE;
261 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (source));
264 GtkTreeSelection *selection;
267 while (GTK_IS_TREE_MODEL_FILTER (model))
269 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
272 dict = PSPPIRE_DICT (model);
274 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (source));
276 list = gtk_tree_selection_get_selected_rows (selection, &model);
278 /* Iterate through the selection of the source treeview */
279 for (l = list; l ; l = l->next)
281 GtkTreePath *path = l->data;
282 GtkTreePath *fpath = gtk_tree_model_filter_convert_path_to_child_path
283 (GTK_TREE_MODEL_FILTER (model), path);
285 gint *idx = gtk_tree_path_get_indices (fpath);
287 const struct variable *v = psppire_dict_get_variable (dict, idx[0]);
289 gtk_tree_path_free (fpath);
291 if ( var_is_alpha (v))
298 g_list_foreach (list, (GFunc) gtk_tree_path_free, NULL);