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