Added custom psppire-selector widget.
[pspp-builds.git] / src / ui / gui / dict-display.c
1 /*
2    PSPPIRE --- A Graphical User Interface for PSPP
3    Copyright (C) 2007  Free Software Foundation
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20
21 #include <config.h>
22 #include <gettext.h>
23 #include <gtk/gtk.h>
24
25 #include "dict-display.h"
26
27 #include "psppire-dict.h"
28 #include "helper.h"
29 #include <data/variable.h>
30
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
33
34
35 /* A GtkTreeModelFilterVisibleFunc to filter lines in the treeview */
36 static gboolean
37 filter_variables (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
38 {
39   var_predicate_func *predicate = data;
40   struct variable *var;
41   PsppireDict *dict = PSPPIRE_DICT (model);
42
43   GtkTreePath *path = gtk_tree_model_get_path (model, iter);
44
45   gint *idx = gtk_tree_path_get_indices (path);
46
47   var =  psppire_dict_get_variable (dict, *idx);
48
49   gtk_tree_path_free (path);
50
51   return predicate (var);
52 }
53
54 /* Sets up TREEVIEW to display the variables of DICT.
55    MODE is the selection mode for TREEVIEW.
56    PREDICATE determines which variables should be visible, or NULL if
57    all are to be visible.
58  */
59 void
60 attach_dictionary_to_treeview (GtkTreeView *treeview, PsppireDict *dict,
61                                GtkSelectionMode mode,
62                                var_predicate_func *predicate
63                                )
64 {
65   GtkTreeViewColumn *col;
66
67   GtkTreeSelection *selection =
68     gtk_tree_view_get_selection (treeview);
69
70   GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
71   GtkTreeModel *model ;
72
73   if ( predicate )
74     {
75       model = gtk_tree_model_filter_new (GTK_TREE_MODEL (dict),
76                                           NULL);
77
78       gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (model),
79                                               filter_variables,
80                                               predicate,
81                                               NULL);
82     }
83   else
84     {
85       model = GTK_TREE_MODEL (dict);
86     }
87
88   gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), model);
89
90
91
92   col = gtk_tree_view_column_new_with_attributes (_("Var"),
93                                                   renderer,
94                                                   "text",
95                                                   0,
96                                                   NULL);
97
98   /* FIXME: make this a value in terms of character widths */
99   g_object_set (col, "min-width",  100, NULL);
100
101   gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
102
103   gtk_tree_view_append_column (treeview, col);
104
105   gtk_tree_selection_set_mode (selection, mode);
106 }
107
108
109 void
110 insert_source_row_into_entry (GtkTreeIter iter,
111                               GtkWidget *dest,
112                               GtkTreeModel *model
113                               )
114 {
115   GtkTreePath *path;
116   PsppireDict *dict;
117   gint *idx;
118   struct variable *var;
119   GtkTreeIter dict_iter;
120   gchar *name;
121
122   g_return_if_fail (GTK_IS_ENTRY(dest));
123
124
125   if ( GTK_IS_TREE_MODEL_FILTER (model))
126     {
127       dict = PSPPIRE_DICT (gtk_tree_model_filter_get_model
128                            (GTK_TREE_MODEL_FILTER(model)));
129       gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER
130                                                         (model),
131                                                         &dict_iter, &iter);
132     }
133   else
134     {
135       dict = PSPPIRE_DICT (model);
136       dict_iter = iter;
137     }
138
139
140   path = gtk_tree_model_get_path (GTK_TREE_MODEL (dict), &dict_iter);
141
142   idx = gtk_tree_path_get_indices (path);
143
144   var =  psppire_dict_get_variable (dict, *idx);
145
146   gtk_tree_path_free (path);
147
148   name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL);
149   gtk_entry_set_text (GTK_ENTRY (dest),  name);
150   g_free (name);
151 }
152
153
154
155 void
156 insert_source_row_into_tree_view (GtkTreeIter iter,
157                                   GtkWidget *dest,
158                                   GtkTreeModel *model
159                                   )
160 {
161   GtkTreePath *path;
162   GtkTreeIter dest_iter;
163   GtkTreeIter dict_iter;
164   gint *row ;
165   GtkTreeModel *destmodel = gtk_tree_view_get_model ( GTK_TREE_VIEW (dest));
166
167   PsppireDict *dict;
168
169   if ( GTK_IS_TREE_MODEL_FILTER (model))
170     {
171       dict = PSPPIRE_DICT (gtk_tree_model_filter_get_model
172                            (GTK_TREE_MODEL_FILTER(model)));
173       gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER
174                                                         (model),
175                                                         &dict_iter, &iter);
176     }
177   else
178     {
179       dict = PSPPIRE_DICT (model);
180       dict_iter = iter;
181     }
182
183
184   path = gtk_tree_model_get_path (GTK_TREE_MODEL (dict), &dict_iter);
185
186   row = gtk_tree_path_get_indices (path);
187
188   gtk_list_store_append (GTK_LIST_STORE (destmodel),  &dest_iter);
189   gtk_list_store_set (GTK_LIST_STORE (destmodel), &dest_iter, 0, *row, -1);
190
191   gtk_tree_path_free (path);
192 }
193
194
195 gboolean
196 is_currently_in_entry (GtkTreeModel *model, GtkTreeIter *iter,
197                        PsppireSelector *selector)
198 {
199   gboolean result;
200   gchar *name;
201   GtkTreeIter dict_iter;
202   PsppireDict *dict;
203   struct variable *var;
204   gint dict_index;
205   gint *indeces;
206   GtkTreePath *path;
207   const gchar *text =   gtk_entry_get_text (GTK_ENTRY (selector->dest));
208
209
210   if ( GTK_IS_TREE_MODEL_FILTER (model))
211     {
212       dict = PSPPIRE_DICT (gtk_tree_model_filter_get_model
213                            (GTK_TREE_MODEL_FILTER(model)));
214       gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER
215                                                         (model),
216                                                         &dict_iter, iter);
217     }
218   else
219     {
220       dict = PSPPIRE_DICT (model);
221       dict_iter = *iter;
222     }
223
224
225   path = gtk_tree_model_get_path (GTK_TREE_MODEL(dict),
226                                   &dict_iter);
227
228   indeces = gtk_tree_path_get_indices (path);
229
230   dict_index = indeces [0];
231
232   var = psppire_dict_get_variable (dict, dict_index);
233
234   gtk_tree_path_free (path);
235
236   name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL);
237   result = ( 0 == strcmp (text, name));
238   g_free (name);
239
240   return result;
241 }
242
243