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/>. */
22 #include "dict-display.h"
24 #include "psppire-dict.h"
26 #include <data/variable.h>
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
32 /* A GtkTreeModelFilterVisibleFunc to filter lines in the treeview */
34 filter_variables (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
36 var_predicate_func *predicate = data;
38 PsppireDict *dict = PSPPIRE_DICT (model);
40 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
42 gint *idx = gtk_tree_path_get_indices (path);
44 var = psppire_dict_get_variable (dict, *idx);
46 gtk_tree_path_free (path);
48 return predicate (var);
51 /* A GtkTreeCellDataFunc which sets the icon appropriate to the type
54 var_icon_cell_data_func (GtkTreeViewColumn *col,
55 GtkCellRenderer *cell,
61 gtk_tree_model_get (model, iter, DICT_TVM_COL_VAR, &var, -1);
63 if ( var_is_alpha (var))
65 g_object_set (cell, "stock-id", "var-string", NULL);
69 const struct fmt_spec *fs = var_get_write_format (var);
70 int cat = fmt_get_category (fs->type);
71 switch ( var_get_measure (var))
74 g_object_set (cell, "stock-id", "var-nominal", NULL);
77 g_object_set (cell, "stock-id", "var-ordinal", NULL);
80 if ( ( FMT_CAT_DATE | FMT_CAT_TIME ) & cat )
81 g_object_set (cell, "stock-id", "var-date-scale", NULL);
83 g_object_set (cell, "stock-id", "var-scale", NULL);
86 g_assert_not_reached ();
93 get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter,
94 GtkTreeModel **model, GtkTreeIter *iter
99 while (GTK_IS_TREE_MODEL_FILTER (*model))
101 GtkTreeIter parent_iter = *iter;
102 GtkTreeModelFilter *parent_model = GTK_TREE_MODEL_FILTER (*model);
104 *model = gtk_tree_model_filter_get_model (parent_model);
106 gtk_tree_model_filter_convert_iter_to_child_iter (parent_model,
111 g_assert (PSPPIRE_IS_DICT (*model));
114 /* A GtkTreeCellDataFunc which renders the name and/or label of the
117 var_description_cell_data_func (GtkTreeViewColumn *col,
118 GtkCellRenderer *cell,
119 GtkTreeModel *top_model,
120 GtkTreeIter *top_iter,
123 struct variable *var;
128 get_base_model (top_model, top_iter, &model, &iter);
130 g_assert (PSPPIRE_IS_DICT (model));
133 gtk_tree_model_get (model,
134 &iter, DICT_TVM_COL_VAR, &var, -1);
136 if ( var_has_label (var))
138 gchar *text = g_strdup_printf (
139 "<span stretch=\"condensed\">%s</span>",
140 var_get_label (var));
143 char *utf8 = pspp_locale_to_utf8 (text, -1, NULL);
146 g_object_set (cell, "markup", utf8, NULL);
151 g_object_set (cell, "text", var_get_name (var), NULL);
156 /* Sets the tooltip to be the name of the variable under the cursor */
158 set_tooltip_for_variable (GtkTreeView *treeview,
161 gboolean keyboard_mode,
169 GtkTreeModel *tree_model;
170 struct variable *var = NULL;
173 gtk_tree_view_convert_widget_to_bin_window_coords (treeview,
176 if (!gtk_tree_view_get_path_at_pos (treeview, bx, by,
177 &path, NULL, NULL, NULL))
180 tree_model = gtk_tree_view_get_model (treeview);
183 gtk_tree_view_set_tooltip_row (treeview, tooltip, path);
185 ok = gtk_tree_model_get_iter (tree_model, &iter, path);
187 gtk_tree_path_free (path);
192 gtk_tree_model_get (tree_model, &iter, DICT_TVM_COL_VAR, &var, -1);
194 if ( ! var_has_label (var))
197 gtk_tooltip_set_text (tooltip, var_get_name (var));
202 /* Sets up TREEVIEW to display the variables of DICT.
203 MODE is the selection mode for TREEVIEW.
204 PREDICATE determines which variables should be visible, or NULL if
205 all are to be visible.
208 attach_dictionary_to_treeview (GtkTreeView *treeview, PsppireDict *dict,
209 GtkSelectionMode mode,
210 var_predicate_func *predicate
213 GtkTreeViewColumn *col;
215 GtkTreeSelection *selection =
216 gtk_tree_view_get_selection (treeview);
218 GtkCellRenderer *renderer;
220 GtkTreeModel *model ;
224 model = gtk_tree_model_filter_new (GTK_TREE_MODEL (dict),
227 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (model),
234 model = GTK_TREE_MODEL (dict);
237 gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), model);
240 col = gtk_tree_view_column_new ();
241 gtk_tree_view_column_set_title (col, _("Variable"));
243 renderer = gtk_cell_renderer_pixbuf_new ();
244 gtk_tree_view_column_pack_start (col, renderer, FALSE);
246 gtk_tree_view_column_set_cell_data_func (col, renderer,
247 var_icon_cell_data_func,
251 renderer = gtk_cell_renderer_text_new ();
252 gtk_tree_view_column_pack_start (col, renderer, TRUE);
253 gtk_tree_view_column_set_cell_data_func (col, renderer,
254 var_description_cell_data_func,
257 g_object_set (renderer, "ellipsize-set", TRUE, NULL);
258 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
260 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
262 /* FIXME: make this a value in terms of character widths */
263 gtk_tree_view_column_set_min_width (col, 150);
265 gtk_tree_view_append_column (treeview, col);
267 gtk_tree_selection_set_mode (selection, mode);
269 g_object_set (treeview, "has-tooltip", TRUE, NULL);
271 g_signal_connect (treeview, "query-tooltip", G_CALLBACK (set_tooltip_for_variable), NULL);
276 insert_source_row_into_entry (GtkTreeIter iter,
285 struct variable *var;
286 GtkTreeIter dict_iter;
289 g_return_if_fail (GTK_IS_ENTRY(dest));
291 get_base_model (model, &iter, &dict, &dict_iter);
293 path = gtk_tree_model_get_path (GTK_TREE_MODEL (dict), &dict_iter);
295 idx = gtk_tree_path_get_indices (path);
297 var = psppire_dict_get_variable (PSPPIRE_DICT (dict), *idx);
299 gtk_tree_path_free (path);
301 name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL);
302 gtk_entry_set_text (GTK_ENTRY (dest), name);
309 insert_source_row_into_tree_view (GtkTreeIter iter,
316 GtkTreeIter dest_iter;
317 GtkTreeIter dict_iter;
319 GtkTreeModel *destmodel = gtk_tree_view_get_model ( GTK_TREE_VIEW (dest));
324 get_base_model (model, &iter, &dict, &dict_iter);
326 path = gtk_tree_model_get_path (dict, &dict_iter);
328 row = gtk_tree_path_get_indices (path);
330 gtk_list_store_append (GTK_LIST_STORE (destmodel), &dest_iter);
331 gtk_list_store_set (GTK_LIST_STORE (destmodel), &dest_iter, 0, *row, -1);
333 gtk_tree_path_free (path);
338 is_currently_in_entry (GtkTreeModel *model, GtkTreeIter *iter,
339 PsppireSelector *selector)
343 GtkTreeIter dict_iter;
345 struct variable *var;
349 const gchar *text = gtk_entry_get_text (GTK_ENTRY (selector->dest));
351 get_base_model (model, iter, &dict, &dict_iter);
353 path = gtk_tree_model_get_path (dict, &dict_iter);
355 indeces = gtk_tree_path_get_indices (path);
357 dict_index = indeces [0];
359 var = psppire_dict_get_variable (PSPPIRE_DICT (dict), dict_index);
361 gtk_tree_path_free (path);
363 name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL);
364 result = ( 0 == strcmp (text, name));