Fixed the refresh button on the dialogs.
[pspp-builds.git] / src / ui / gui / dialog-common.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 "dialog-common.h"
23
24 #include "helper.h"
25
26
27 /* Append the names of selected variables to STRING.
28    TREEVIEW is the treeview containing the variables.
29    DICT is the dictionary for those variables.
30 */
31 gint
32 append_variable_names (GString *string,
33                        PsppireDict *dict, GtkTreeView *treeview)
34 {
35   gint n_vars = 0;
36   GtkTreeIter iter;
37
38   GtkTreeModel *list_store =
39     gtk_tree_view_get_model (treeview);
40
41   if ( gtk_tree_model_get_iter_first (list_store, &iter) )
42     {
43       do
44         {
45           GValue value = {0};
46           struct variable *var;
47           GtkTreePath *path = gtk_tree_model_get_path (list_store, &iter);
48
49           gtk_tree_model_get_value (list_store, &iter, 0, &value);
50
51           var = psppire_dict_get_variable (dict, g_value_get_int (&value));
52           g_value_unset (&value);
53
54           g_string_append (string, " ");
55           g_string_append (string, var_get_name (var));
56
57           gtk_tree_path_free (path);
58           n_vars++;
59         }
60       while (gtk_tree_model_iter_next (list_store, &iter));
61     }
62
63   return n_vars;
64 }
65
66
67
68 struct variable *
69 get_selected_variable (GtkTreeModel *treemodel,
70                        GtkTreeIter *iter,
71                        PsppireDict *dict)
72 {
73   struct variable *var;
74   GValue value = {0};
75
76   GtkTreePath *path = gtk_tree_model_get_path (treemodel, iter);
77
78   gtk_tree_model_get_value (treemodel, iter, 0, &value);
79
80   gtk_tree_path_free (path);
81
82   var =  psppire_dict_get_variable (dict, g_value_get_int (&value));
83
84   g_value_unset (&value);
85
86   return var;
87 }
88
89
90
91
92 /* A (*GtkTreeCellDataFunc) function.
93    This function expects TREEMODEL to hold G_TYPE_INT.  The ints it holds
94    are the indices of the variables in the dictionary, which DATA points to.
95    It renders the name of the variable into CELL.
96 */
97 void
98 cell_var_name (GtkTreeViewColumn *tree_column,
99                GtkCellRenderer *cell,
100                GtkTreeModel *tree_model,
101                GtkTreeIter *iter,
102                gpointer data)
103 {
104   PsppireDict *dict = data;
105   struct variable *var;
106   gchar *name;
107
108   var = get_selected_variable (tree_model, iter, dict);
109
110   name = pspp_locale_to_utf8 (var_get_name (var), -1, NULL);
111   g_object_set (cell, "text", name, NULL);
112   g_free (name);
113 }
114
115
116
117 /* Set a model for DEST, which is an GtkListStore of g_int's
118    whose values are the indices into DICT */
119 void
120 set_dest_model (GtkTreeView *dest, PsppireDict *dict)
121 {
122   GtkTreeViewColumn *col;
123   GtkListStore *dest_list = gtk_list_store_new (1, G_TYPE_INT);
124   GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
125
126   gtk_tree_view_set_model (GTK_TREE_VIEW (dest), GTK_TREE_MODEL (dest_list));
127
128   col = gtk_tree_view_column_new_with_attributes ("Var",
129                                                   renderer,
130                                                   "text",
131                                                   0,
132                                                   NULL);
133
134   gtk_tree_view_column_set_cell_data_func (col, renderer,
135                                            cell_var_name,
136                                            dict, 0);
137
138   /* FIXME: make this a value in terms of character widths */
139   g_object_set (col, "min-width",  100, NULL);
140
141   gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
142
143   gtk_tree_view_append_column (GTK_TREE_VIEW(dest), col);
144 }
145