77da88bd98826c9791151af09edccc4de1d9b186
[pspp] / src / ui / gui / dict-display.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17
18 #include <config.h>
19 #include <gettext.h>
20 #include <gtk/gtk.h>
21
22 #include "psppire-conf.h"
23 #include "dict-display.h"
24
25 #include "psppire-dict.h"
26 #include "psppire-dictview.h"
27 #include "psppire-var-ptr.h"
28 #include "psppire-var-view.h"
29 #include "psppire-select-dest.h"
30 #include <libpspp/i18n.h>
31 #include "helper.h"
32 #include <data/variable.h>
33 #include <data/format.h>
34
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38 static void
39 get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter,
40                 GtkTreeModel **model, GtkTreeIter *iter
41                 )
42 {
43   *model = top_model;
44   *iter = *top_iter;
45   while (GTK_IS_TREE_MODEL_FILTER (*model))
46     {
47       GtkTreeIter parent_iter = *iter;
48       GtkTreeModelFilter *parent_model = GTK_TREE_MODEL_FILTER (*model);
49
50       *model = gtk_tree_model_filter_get_model (parent_model);
51
52       gtk_tree_model_filter_convert_iter_to_child_iter (parent_model,
53                                                         iter,
54                                                         &parent_iter);
55     }
56
57   g_assert (PSPPIRE_IS_DICT (*model));
58 }
59
60
61 void
62 insert_source_row_into_entry (GtkTreeIter iter,
63                               GtkWidget *dest,
64                               GtkTreeModel *model,
65                               gpointer data
66                               )
67 {
68   GtkTreePath *path;
69   GtkTreeModel *dict;
70   gint *idx;
71   struct variable *var;
72   GtkTreeIter dict_iter;
73
74   g_return_if_fail (GTK_IS_ENTRY(dest));
75
76   get_base_model (model, &iter, &dict, &dict_iter);
77
78   path = gtk_tree_model_get_path (GTK_TREE_MODEL (dict), &dict_iter);
79
80   idx = gtk_tree_path_get_indices (path);
81
82   var =  psppire_dict_get_variable (PSPPIRE_DICT (dict), *idx);
83
84   gtk_tree_path_free (path);
85
86   gtk_entry_set_text (GTK_ENTRY (dest),  var_get_name (var));
87 }
88
89
90 void
91 insert_source_row_into_tree_view (GtkTreeIter iter,
92                                   GtkWidget *dest,
93                                   GtkTreeModel *model,
94                                   gpointer data
95                                   )
96 {
97   GtkTreePath *path;
98   GtkTreeIter dest_iter;
99   GtkTreeIter dict_iter;
100   gint *row ;
101   GtkTreeModel *destmodel = gtk_tree_view_get_model (GTK_TREE_VIEW (dest));
102
103   const struct variable *var;
104   GtkTreeModel *dict;
105
106   get_base_model (model, &iter, &dict, &dict_iter);
107
108   path = gtk_tree_model_get_path (dict, &dict_iter);
109
110   row = gtk_tree_path_get_indices (path);
111
112   var = psppire_dict_get_variable (PSPPIRE_DICT (dict), *row);
113
114   gtk_list_store_append (GTK_LIST_STORE (destmodel),  &dest_iter);
115
116   gtk_list_store_set (GTK_LIST_STORE (destmodel), &dest_iter, 0, var, -1);
117
118   gtk_tree_path_free (path);
119 }
120
121
122
123 gboolean
124 is_currently_in_entry (GtkTreeModel *model, GtkTreeIter *iter,
125                        PsppireSelector *selector)
126 {
127   gboolean result;
128   GtkTreeIter dict_iter;
129   GtkTreeModel *dict;
130   struct variable *var;
131   gint dict_index;
132   gint *indeces;
133   GtkTreePath *path;
134   GtkWidget *entry = NULL;
135   const gchar *text = NULL;
136
137   g_object_get (selector, "dest-widget", &entry, NULL);
138
139   text = gtk_entry_get_text (GTK_ENTRY (entry));
140
141   get_base_model (model, iter, &dict, &dict_iter);
142
143   path = gtk_tree_model_get_path (dict, &dict_iter);
144
145   indeces = gtk_tree_path_get_indices (path);
146
147   dict_index = indeces [0];
148
149   var = psppire_dict_get_variable (PSPPIRE_DICT (dict), dict_index);
150
151   gtk_tree_path_free (path);
152
153   result = ( 0 == strcmp (text, var_get_name (var) ));
154
155   return result;
156 }
157
158 gboolean
159 is_currently_in_varview (GtkTreeModel *model, GtkTreeIter *iter, PsppireSelector *sel)
160 {
161   gboolean ret = false;
162
163   /* First, fetch the variable from the source */
164
165   PsppireDictView *dv = PSPPIRE_DICT_VIEW (sel->source);
166
167   GtkTreePath *path = gtk_tree_model_get_path (model, iter);
168
169   gint *idx = gtk_tree_path_get_indices (path);
170
171   const struct variable *var =  psppire_dict_get_variable (dv->dict, *idx);
172
173
174   /* Now test if that variable exists in the destination */
175
176   GValue value = {0};
177
178   g_value_init (&value, PSPPIRE_VAR_PTR_TYPE);
179   g_value_set_boxed (&value, var);
180
181   ret = psppire_select_dest_widget_contains_var (PSPPIRE_SELECT_DEST_WIDGET (sel->dest), &value);
182
183   g_value_unset (&value);
184
185   return ret ;
186 }
187