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