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>
27 #include <data/format.h>
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
33 /* A GtkTreeModelFilterVisibleFunc to filter lines in the treeview */
35 filter_variables (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
37 var_predicate_func *predicate = data;
39 PsppireDict *dict = PSPPIRE_DICT (model);
41 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
43 gint *idx = gtk_tree_path_get_indices (path);
45 var = psppire_dict_get_variable (dict, *idx);
47 gtk_tree_path_free (path);
49 return predicate (var);
52 /* A GtkTreeCellDataFunc which sets the icon appropriate to the type
55 var_icon_cell_data_func (GtkTreeViewColumn *col,
56 GtkCellRenderer *cell,
62 gtk_tree_model_get (model, iter, DICT_TVM_COL_VAR, &var, -1);
64 if ( var_is_alpha (var))
66 g_object_set (cell, "stock-id", "var-string", NULL);
70 const struct fmt_spec *fs = var_get_write_format (var);
71 int cat = fmt_get_category (fs->type);
72 switch ( var_get_measure (var))
75 g_object_set (cell, "stock-id", "var-nominal", NULL);
78 g_object_set (cell, "stock-id", "var-ordinal", NULL);
81 if ( ( FMT_CAT_DATE | FMT_CAT_TIME ) & cat )
82 g_object_set (cell, "stock-id", "var-date-scale", NULL);
84 g_object_set (cell, "stock-id", "var-scale", NULL);
87 g_assert_not_reached ();
94 get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter,
95 GtkTreeModel **model, GtkTreeIter *iter
100 while (GTK_IS_TREE_MODEL_FILTER (*model))
102 GtkTreeIter parent_iter = *iter;
103 GtkTreeModelFilter *parent_model = GTK_TREE_MODEL_FILTER (*model);
105 *model = gtk_tree_model_filter_get_model (parent_model);
107 gtk_tree_model_filter_convert_iter_to_child_iter (parent_model,
112 g_assert (PSPPIRE_IS_DICT (*model));
115 /* A GtkTreeCellDataFunc which renders the name and/or label of the
118 var_description_cell_data_func (GtkTreeViewColumn *col,
119 GtkCellRenderer *cell,
120 GtkTreeModel *top_model,
121 GtkTreeIter *top_iter,
124 struct variable *var;
129 get_base_model (top_model, top_iter, &model, &iter);
131 g_assert (PSPPIRE_IS_DICT (model));
134 gtk_tree_model_get (model,
135 &iter, DICT_TVM_COL_VAR, &var, -1);
137 if ( var_has_label (var))
139 gchar *text = g_strdup_printf (
140 "<span stretch=\"condensed\">%s</span>",
141 var_get_label (var));
144 char *utf8 = pspp_locale_to_utf8 (text, -1, NULL);
147 g_object_set (cell, "markup", utf8, NULL);
152 g_object_set (cell, "text", var_get_name (var), NULL);
157 #if GTK_CHECK_VERSION (2, 12, 0)
158 /* Sets the tooltip to be the name of the variable under the cursor */
160 set_tooltip_for_variable (GtkTreeView *treeview,
163 gboolean keyboard_mode,
171 GtkTreeModel *tree_model;
172 struct variable *var = NULL;
176 gtk_tree_view_convert_widget_to_bin_window_coords (treeview,
179 if (!gtk_tree_view_get_path_at_pos (treeview, bx, by,
180 &path, NULL, NULL, NULL))
183 tree_model = gtk_tree_view_get_model (treeview);
186 gtk_tree_view_set_tooltip_row (treeview, tooltip, path);
188 ok = gtk_tree_model_get_iter (tree_model, &iter, path);
190 gtk_tree_path_free (path);
195 gtk_tree_model_get (tree_model, &iter, DICT_TVM_COL_VAR, &var, -1);
197 if ( ! var_has_label (var))
200 gtk_tooltip_set_text (tooltip, var_get_name (var));
206 /* Sets up TREEVIEW to display the variables of DICT.
207 MODE is the selection mode for TREEVIEW.
208 PREDICATE determines which variables should be visible, or NULL if
209 all are to be visible.
212 attach_dictionary_to_treeview (GtkTreeView *treeview, PsppireDict *dict,
213 GtkSelectionMode mode,
214 var_predicate_func *predicate
217 GtkTreeViewColumn *col;
219 GtkTreeSelection *selection =
220 gtk_tree_view_get_selection (treeview);
222 GtkCellRenderer *renderer;
224 GtkTreeModel *model ;
228 model = gtk_tree_model_filter_new (GTK_TREE_MODEL (dict),
231 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (model),
238 model = GTK_TREE_MODEL (dict);
241 gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), model);
244 col = gtk_tree_view_column_new ();
245 gtk_tree_view_column_set_title (col, _("Variable"));
247 renderer = gtk_cell_renderer_pixbuf_new ();
248 gtk_tree_view_column_pack_start (col, renderer, FALSE);
250 gtk_tree_view_column_set_cell_data_func (col, renderer,
251 var_icon_cell_data_func,
255 renderer = gtk_cell_renderer_text_new ();
256 gtk_tree_view_column_pack_start (col, renderer, TRUE);
257 gtk_tree_view_column_set_cell_data_func (col, renderer,
258 var_description_cell_data_func,
261 g_object_set (renderer, "ellipsize-set", TRUE, NULL);
262 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_MIDDLE, NULL);
264 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
266 /* FIXME: make this a value in terms of character widths */
267 gtk_tree_view_column_set_min_width (col, 150);
269 gtk_tree_view_append_column (treeview, col);
271 gtk_tree_selection_set_mode (selection, mode);
273 g_object_set (treeview, "has-tooltip", TRUE, NULL);
275 #if GTK_CHECK_VERSION (2, 12, 0)
276 g_signal_connect (treeview, "query-tooltip", G_CALLBACK (set_tooltip_for_variable), NULL);
282 insert_source_row_into_entry (GtkTreeIter iter,
291 struct variable *var;
292 GtkTreeIter dict_iter;
295 g_return_if_fail (GTK_IS_ENTRY(dest));
297 get_base_model (model, &iter, &dict, &dict_iter);
299 path = gtk_tree_model_get_path (GTK_TREE_MODEL (dict), &dict_iter);
301 idx = gtk_tree_path_get_indices (path);
303 var = psppire_dict_get_variable (PSPPIRE_DICT (dict), *idx);
305 gtk_tree_path_free (path);
307 name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL);
308 gtk_entry_set_text (GTK_ENTRY (dest), name);
315 insert_source_row_into_tree_view (GtkTreeIter iter,
322 GtkTreeIter dest_iter;
323 GtkTreeIter dict_iter;
325 GtkTreeModel *destmodel = gtk_tree_view_get_model ( GTK_TREE_VIEW (dest));
330 get_base_model (model, &iter, &dict, &dict_iter);
332 path = gtk_tree_model_get_path (dict, &dict_iter);
334 row = gtk_tree_path_get_indices (path);
336 gtk_list_store_append (GTK_LIST_STORE (destmodel), &dest_iter);
337 gtk_list_store_set (GTK_LIST_STORE (destmodel), &dest_iter, 0, *row, -1);
339 gtk_tree_path_free (path);
344 is_currently_in_entry (GtkTreeModel *model, GtkTreeIter *iter,
345 PsppireSelector *selector)
349 GtkTreeIter dict_iter;
351 struct variable *var;
355 const gchar *text = gtk_entry_get_text (GTK_ENTRY (selector->dest));
357 get_base_model (model, iter, &dict, &dict_iter);
359 path = gtk_tree_model_get_path (dict, &dict_iter);
361 indeces = gtk_tree_path_get_indices (path);
363 dict_index = indeces [0];
365 var = psppire_dict_get_variable (PSPPIRE_DICT (dict), dict_index);
367 gtk_tree_path_free (path);
369 name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL);
370 result = ( 0 == strcmp (text, name));