Finish converting struct variable to an opaque type. In this
[pspp-builds.git] / src / ui / gui / val-labs-dialog.c
1 /* 
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2005  Free Software Foundation
4     Written by John Darrington
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19     02110-1301, USA. */
20
21
22 /*  This module describes the behaviour of the Value Labels dialog box,
23     used for input of the value labels in the variable sheet */
24
25 #include <config.h>
26
27 #include <string.h>
28
29 #include "helper.h"
30 #include "val-labs-dialog.h"
31 #include <data/value-labels.h>
32 #include "psppire-variable.h"
33
34 /* This callback occurs when the text in the label entry box 
35    is changed */
36 static void
37 on_label_entry_change(GtkEntry *entry, gpointer data)
38 {
39   union value v;
40   const gchar *text ;
41   struct val_labs_dialog *dialog = data;
42   g_assert(dialog->labs);
43
44   text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
45
46   text_to_value(text, &v, 
47                 *psppire_variable_get_write_spec(dialog->pv));
48
49
50   if ( val_labs_find (dialog->labs, v) ) 
51     {
52       gtk_widget_set_sensitive(dialog->change_button, TRUE);      
53       gtk_widget_set_sensitive(dialog->add_button, FALSE);      
54     }
55   else
56     {
57       gtk_widget_set_sensitive(dialog->change_button, FALSE);     
58       gtk_widget_set_sensitive(dialog->add_button, TRUE);       
59     }
60 }
61
62
63 /* Set the TREEVIEW list cursor to the item which has the value VAL */
64 static void
65 select_treeview_from_value(GtkTreeView *treeview, union value *val)
66 {
67   GtkTreePath *path ;
68   
69   /*
70     We do this with a linear search through the model --- hardly 
71     efficient, but the list is short ... */
72   GtkTreeIter iter;
73
74   GtkTreeModel * model  = gtk_tree_view_get_model(treeview);
75
76   gboolean success;
77   for (success = gtk_tree_model_get_iter_first(model, &iter);
78        success;
79        success = gtk_tree_model_iter_next(model, &iter))
80     {
81       union value v;
82       GValue gvalue = {0};
83
84       gtk_tree_model_get_value(model, &iter, 1, &gvalue);
85           
86       v.f = g_value_get_double(&gvalue);
87
88       if ( 0 == memcmp(&v, val, sizeof (union value)))
89         {
90           break;
91         }
92     }
93         
94   path = gtk_tree_model_get_path(model, &iter);
95   if ( path ) 
96     {
97       gtk_tree_view_set_cursor(treeview, path, 0, 0);
98       gtk_tree_path_free(path);
99     }
100
101 }
102
103
104 /* This callback occurs when the text in the value entry box is
105    changed */
106 static void
107 on_value_entry_change(GtkEntry *entry, gpointer data)
108 {
109   char *s;
110   
111   struct val_labs_dialog *dialog = data;
112
113   const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
114
115   union value v;
116   text_to_value(text, &v, 
117                 *psppire_variable_get_write_spec(dialog->pv));
118
119
120   g_signal_handler_block(GTK_ENTRY(dialog->label_entry), 
121                          dialog->change_handler_id);
122
123   gtk_entry_set_text(GTK_ENTRY(dialog->label_entry),"");
124
125
126   if ( (s = val_labs_find (dialog->labs, v)) ) 
127     {
128       gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), s);
129       gtk_widget_set_sensitive(dialog->add_button, FALSE);
130       gtk_widget_set_sensitive(dialog->remove_button, TRUE);
131       select_treeview_from_value(GTK_TREE_VIEW(dialog->treeview), &v);
132     }
133   else
134     {
135       gtk_widget_set_sensitive(dialog->remove_button, FALSE);
136       gtk_widget_set_sensitive(dialog->add_button, TRUE);
137     }
138   
139   g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry), 
140                          dialog->change_handler_id);
141 }
142
143
144 /* Callback for when the Value Labels dialog is closed using 
145    the OK button.*/
146 static gint
147 val_labs_ok(GtkWidget *w, gpointer data)
148 {
149   struct val_labs_dialog *dialog = data;
150
151   psppire_variable_set_value_labels(dialog->pv, dialog->labs);
152
153   val_labs_destroy (dialog->labs);
154   dialog->labs = 0;
155
156   return FALSE;
157 }
158
159 /* Callback for when the Value Labels dialog is closed using 
160    the Cancel button.*/
161 static gint
162 val_labs_cancel(GtkWidget *w, gpointer data)
163 {
164   struct val_labs_dialog *dialog = data;
165
166   val_labs_destroy (dialog->labs);
167   dialog->labs = 0;
168   
169   return FALSE;
170 }
171
172
173 /* Return the value-label pair currently selected in the dialog box  */
174 static struct val_lab *
175 get_selected_tuple(struct val_labs_dialog *dialog)
176 {
177   GtkTreeView *treeview = GTK_TREE_VIEW(dialog->treeview);
178   static struct val_lab vl;
179   
180   GtkTreeIter iter ;
181   GValue the_value = {0}; 
182
183   GtkTreeSelection* sel =  gtk_tree_view_get_selection(treeview);
184
185   GtkTreeModel * model  = gtk_tree_view_get_model(treeview);
186
187   gtk_tree_selection_get_selected (sel, &model, &iter);
188
189   gtk_tree_model_get_value(model, &iter, 1, &the_value);
190
191   vl.value.f = g_value_get_double(&the_value);
192   g_value_unset(&the_value);
193
194   vl.label = val_labs_find (dialog->labs, vl.value);
195   
196   return &vl;
197 }
198
199
200 static void repopulate_dialog(struct val_labs_dialog *dialog);
201
202 /* Callback which occurs when the "Change" button is clicked */
203 static gint
204 on_change(GtkWidget *w, gpointer data)
205 {
206   struct val_labs_dialog *dialog = data;
207   
208   const gchar *val_text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
209   
210   union value v;
211   
212   text_to_value(val_text, &v, 
213                 *psppire_variable_get_write_spec(dialog->pv));
214
215   val_labs_replace (dialog->labs, v,
216                     gtk_entry_get_text(GTK_ENTRY(dialog->label_entry)));
217
218   gtk_widget_set_sensitive(dialog->change_button, FALSE);
219
220   repopulate_dialog(dialog);
221
222   return FALSE;
223 }
224
225 /* Callback which occurs when the "Add" button is clicked */
226 static gint
227 on_add(GtkWidget *w, gpointer data)
228 {
229   struct val_labs_dialog *dialog = data;
230
231   union value v;
232
233   const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
234
235   text_to_value(text, &v, 
236                 *psppire_variable_get_write_spec(dialog->pv));
237
238
239   if ( ! val_labs_add (dialog->labs, v,
240                        gtk_entry_get_text(GTK_ENTRY(dialog->label_entry)) ) )
241     return FALSE;
242
243
244   gtk_widget_set_sensitive(dialog->add_button, FALSE);
245
246   repopulate_dialog(dialog);
247
248   return FALSE;
249 }
250
251 /* Callback which occurs when the "Remove" button is clicked */
252 static gint
253 on_remove(GtkWidget *w, gpointer data)
254 {
255   struct val_labs_dialog *dialog = data;
256
257   struct val_lab *vl = get_selected_tuple(dialog);
258
259   val_labs_remove (dialog->labs, vl->value);
260   
261   repopulate_dialog(dialog);
262
263   gtk_widget_set_sensitive(dialog->remove_button, FALSE);
264
265   return FALSE;
266 }
267
268
269
270 /* Callback which occurs when a line item is selected in the list of 
271    value--label pairs.*/
272 static void        
273 on_select_row                  (GtkTreeView *treeview,
274                                 gpointer data)
275 {
276   gchar *labeltext;
277   struct val_labs_dialog *dialog = data;
278
279   struct val_lab * vl  = get_selected_tuple(dialog);
280
281   gchar *const text = value_to_text(vl->value, 
282                                     *psppire_variable_get_write_spec(dialog->pv));
283
284   g_signal_handler_block(GTK_ENTRY(dialog->value_entry), 
285                          dialog->value_handler_id);
286
287   gtk_entry_set_text(GTK_ENTRY(dialog->value_entry), text);
288
289   g_signal_handler_unblock(GTK_ENTRY(dialog->value_entry), 
290                          dialog->value_handler_id);
291   g_free(text);
292
293   g_signal_handler_block(GTK_ENTRY(dialog->label_entry), 
294                          dialog->change_handler_id);
295
296   labeltext = pspp_locale_to_utf8(vl->label, -1, 0);
297   gtk_entry_set_text(GTK_ENTRY(dialog->label_entry),
298                      labeltext);
299   g_free(labeltext);
300
301   g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry), 
302                          dialog->change_handler_id);
303
304   gtk_widget_set_sensitive(dialog->remove_button, TRUE);
305   gtk_widget_set_sensitive(dialog->change_button, FALSE);
306 }
307
308
309 /* Create a new dialog box 
310    (there should  normally be only one)*/
311 struct val_labs_dialog *
312 val_labs_dialog_create(GladeXML *xml)
313 {
314   GtkTreeViewColumn *column;
315
316   GtkCellRenderer *renderer ;
317
318   struct val_labs_dialog *dialog = g_malloc(sizeof(*dialog));
319
320   dialog->window = get_widget_assert(xml,"val_labs_dialog");
321   dialog->value_entry = get_widget_assert(xml,"value_entry");
322   dialog->label_entry = get_widget_assert(xml,"label_entry");
323
324   gtk_window_set_transient_for
325     (GTK_WINDOW(dialog->window), 
326      GTK_WINDOW(get_widget_assert(xml, "data_editor")));
327
328   dialog->ok = get_widget_assert(xml, "val_labs_ok");
329   dialog->add_button = get_widget_assert(xml, "val_labs_add");
330   dialog->remove_button = get_widget_assert(xml, "val_labs_remove");
331   dialog->change_button = get_widget_assert(xml, "val_labs_change");
332
333   dialog->treeview = get_widget_assert(xml,"treeview1");
334
335   gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(dialog->treeview), FALSE);
336
337   renderer = gtk_cell_renderer_text_new();
338   
339   column = gtk_tree_view_column_new_with_attributes ("Title",
340                                                      renderer,
341                                                      "text",
342                                                      0,
343                                                      NULL);
344
345   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
346
347   g_signal_connect(GTK_OBJECT(get_widget_assert(xml, "val_labs_cancel")),
348                    "clicked", 
349                    GTK_SIGNAL_FUNC(val_labs_cancel), dialog);
350
351   dialog->change_handler_id = 
352     g_signal_connect(GTK_OBJECT(dialog->label_entry), 
353                      "changed",
354                      GTK_SIGNAL_FUNC(on_label_entry_change), dialog);
355
356   dialog->value_handler_id  = 
357     g_signal_connect(GTK_OBJECT(dialog->value_entry), 
358                      "changed",
359                      GTK_SIGNAL_FUNC(on_value_entry_change), dialog);
360
361   g_signal_connect(GTK_OBJECT(dialog->change_button), 
362                    "clicked",
363                    GTK_SIGNAL_FUNC(on_change), dialog);
364
365
366   g_signal_connect(GTK_OBJECT(get_widget_assert(xml, "val_labs_ok")),
367                    "clicked", 
368                    GTK_SIGNAL_FUNC(val_labs_ok), dialog);
369
370
371   g_signal_connect(GTK_OBJECT(dialog->treeview), "cursor-changed",
372                    GTK_SIGNAL_FUNC(on_select_row), dialog);
373
374
375   g_signal_connect(GTK_OBJECT(dialog->remove_button), "clicked",
376                    GTK_SIGNAL_FUNC(on_remove), dialog);
377
378
379   g_signal_connect(GTK_OBJECT(dialog->add_button), "clicked",
380                    GTK_SIGNAL_FUNC(on_add), dialog);
381
382   dialog->labs = 0;
383
384   return dialog;
385 }
386
387
388 /* Populate the components of the dialog box, from the 'labs' member
389    variable */
390 static void 
391 repopulate_dialog(struct val_labs_dialog *dialog)
392 {
393   struct val_labs_iterator *vli = 0;
394   struct val_lab *vl; 
395
396   GtkTreeIter iter;
397
398   GtkListStore *list_store = gtk_list_store_new (2, 
399                                                  G_TYPE_STRING, 
400                                                  G_TYPE_DOUBLE);
401
402   g_signal_handler_block(GTK_ENTRY(dialog->label_entry), 
403                          dialog->change_handler_id);
404   g_signal_handler_block(GTK_ENTRY(dialog->value_entry), 
405                          dialog->value_handler_id);
406
407   gtk_entry_set_text(GTK_ENTRY(dialog->value_entry), "");
408   gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), "");
409
410   g_signal_handler_unblock(GTK_ENTRY(dialog->value_entry), 
411                          dialog->value_handler_id);
412   g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry), 
413                            dialog->change_handler_id);
414
415
416   for(vl = val_labs_first_sorted (dialog->labs, &vli);
417       vl;
418       vl = val_labs_next(dialog->labs, &vli))
419     {
420
421       gchar *const vstr  = 
422         value_to_text(vl->value, 
423                       *psppire_variable_get_write_spec(dialog->pv));
424
425       gchar *labeltext = 
426         pspp_locale_to_utf8(vl->label, -1, 0);  
427                                            
428       gchar *const text = g_strdup_printf("%s = \"%s\"",
429                                           vstr, labeltext);
430
431       
432       gtk_list_store_append (list_store, &iter);
433       gtk_list_store_set (list_store, &iter,
434                           0, text, 
435                           1, vl->value.f,
436                           -1);
437
438       g_free(labeltext);
439       g_free(text); 
440       g_free(vstr);
441     }
442
443   gtk_tree_view_set_model(GTK_TREE_VIEW(dialog->treeview), 
444                           GTK_TREE_MODEL(list_store));
445
446   g_object_unref(list_store);
447
448 }
449
450 /* Initialise and display the dialog box */
451 void 
452 val_labs_dialog_show(struct val_labs_dialog *dialog)
453 {
454   g_assert(!dialog->labs);
455   dialog->labs = val_labs_copy(
456                                psppire_variable_get_value_labels(dialog->pv)
457                                );
458
459
460   gtk_widget_set_sensitive(dialog->remove_button, FALSE);
461   gtk_widget_set_sensitive(dialog->change_button, FALSE);
462   gtk_widget_set_sensitive(dialog->add_button, FALSE);
463
464   repopulate_dialog(dialog);
465   gtk_widget_show(dialog->window);
466 }
467