New module psppire-var-view
[pspp-builds.git] / 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 <libpspp/i18n.h>
27 #include "helper.h"
28 #include <data/variable.h>
29 #include <data/format.h>
30
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
33
34 static void
35 get_base_model (GtkTreeModel *top_model, GtkTreeIter *top_iter,
36                 GtkTreeModel **model, GtkTreeIter *iter
37                 )
38 {
39   *model = top_model;
40   *iter = *top_iter;
41   while (GTK_IS_TREE_MODEL_FILTER (*model))
42     {
43       GtkTreeIter parent_iter = *iter;
44       GtkTreeModelFilter *parent_model = GTK_TREE_MODEL_FILTER (*model);
45
46       *model = gtk_tree_model_filter_get_model (parent_model);
47
48       gtk_tree_model_filter_convert_iter_to_child_iter (parent_model,
49                                                         iter,
50                                                         &parent_iter);
51     }
52
53   g_assert (PSPPIRE_IS_DICT (*model));
54 }
55
56
57 void
58 insert_source_row_into_entry (GtkTreeIter iter,
59                               GtkWidget *dest,
60                               GtkTreeModel *model,
61                               gpointer data
62                               )
63 {
64   GtkTreePath *path;
65   GtkTreeModel *dict;
66   gint *idx;
67   struct variable *var;
68   GtkTreeIter dict_iter;
69
70   g_return_if_fail (GTK_IS_ENTRY(dest));
71
72   get_base_model (model, &iter, &dict, &dict_iter);
73
74   path = gtk_tree_model_get_path (GTK_TREE_MODEL (dict), &dict_iter);
75
76   idx = gtk_tree_path_get_indices (path);
77
78   var =  psppire_dict_get_variable (PSPPIRE_DICT (dict), *idx);
79
80   gtk_tree_path_free (path);
81
82   gtk_entry_set_text (GTK_ENTRY (dest),  var_get_name (var));
83 }
84
85
86 void
87 insert_source_row_into_tree_view (GtkTreeIter iter,
88                                   GtkWidget *dest,
89                                   GtkTreeModel *model,
90                                   gpointer data
91                                   )
92 {
93   GtkTreePath *path;
94   GtkTreeIter dest_iter;
95   GtkTreeIter dict_iter;
96   gint *row ;
97   GtkTreeModel *destmodel = gtk_tree_view_get_model (GTK_TREE_VIEW (dest));
98
99   const struct variable *var;
100   GtkTreeModel *dict;
101
102   get_base_model (model, &iter, &dict, &dict_iter);
103
104   path = gtk_tree_model_get_path (dict, &dict_iter);
105
106   row = gtk_tree_path_get_indices (path);
107
108   var = psppire_dict_get_variable (PSPPIRE_DICT (dict), *row);
109
110   gtk_list_store_append (GTK_LIST_STORE (destmodel),  &dest_iter);
111
112   gtk_list_store_set (GTK_LIST_STORE (destmodel), &dest_iter, 0, var, -1);
113
114   gtk_tree_path_free (path);
115 }
116
117
118
119 gboolean
120 is_currently_in_entry (GtkTreeModel *model, GtkTreeIter *iter,
121                        PsppireSelector *selector)
122 {
123   gboolean result;
124   GtkTreeIter dict_iter;
125   GtkTreeModel *dict;
126   struct variable *var;
127   gint dict_index;
128   gint *indeces;
129   GtkTreePath *path;
130   GtkWidget *entry = NULL;
131   const gchar *text = NULL;
132
133   g_object_get (selector, "dest-widget", &entry, NULL);
134
135   text = gtk_entry_get_text (GTK_ENTRY (entry));
136
137   get_base_model (model, iter, &dict, &dict_iter);
138
139   path = gtk_tree_model_get_path (dict, &dict_iter);
140
141   indeces = gtk_tree_path_get_indices (path);
142
143   dict_index = indeces [0];
144
145   var = psppire_dict_get_variable (PSPPIRE_DICT (dict), dict_index);
146
147   gtk_tree_path_free (path);
148
149   result = ( 0 == strcmp (text, var_get_name (var) ));
150
151   return result;
152 }
153
154
155