Change "union value" to dynamically allocate long strings.
[pspp-builds.git] / src / ui / gui / val-labs-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2005, 2009  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 /*  This module describes the behaviour of the Value Labels dialog box,
19     used for input of the value labels in the variable sheet */
20
21 #include <config.h>
22
23 #include <string.h>
24
25 #include "helper.h"
26 #include "val-labs-dialog.h"
27 #include <data/value-labels.h>
28 #include <data/format.h>
29 #include "psppire-var-sheet.h"
30 #include "psppire-var-store.h"
31 #include <libpspp/i18n.h>
32
33 struct val_labs_dialog
34 {
35   GtkWidget *window;
36
37   PsppireSheet *vs;
38
39   /* The variable to be updated */
40   struct variable *pv;
41
42   /* Local copy of labels */
43   struct val_labs *labs;
44
45   /* Actions */
46   GtkWidget *add_button;
47   GtkWidget *remove_button;
48   GtkWidget *change_button;
49
50   /* Entry Boxes */
51   GtkWidget *value_entry;
52   GtkWidget *label_entry;
53
54   /* Signal handler ids */
55   gint change_handler_id;
56   gint value_handler_id;
57
58   GtkWidget *treeview;
59 };
60
61
62 /* This callback occurs when the text in the label entry box
63    is changed */
64 static void
65 on_label_entry_change (GtkEntry *entry, gpointer data)
66 {
67   union value v;
68   const gchar *text ;
69   struct val_labs_dialog *dialog = data;
70   g_assert (dialog->labs);
71
72   text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
73
74   text_to_value (text, &v,
75                 *var_get_write_format (dialog->pv));
76
77
78   if (val_labs_find (dialog->labs, &v))
79     {
80       gtk_widget_set_sensitive (dialog->change_button, TRUE);
81       gtk_widget_set_sensitive (dialog->add_button, FALSE);
82     }
83   else
84     {
85       gtk_widget_set_sensitive (dialog->change_button, FALSE);
86       gtk_widget_set_sensitive (dialog->add_button, TRUE);
87     }
88 }
89
90
91 /* Set the TREEVIEW list cursor to the item which has the value VAL */
92 static void
93 select_treeview_from_value (GtkTreeView *treeview, union value *val)
94 {
95   GtkTreePath *path ;
96
97   /*
98     We do this with a linear search through the model --- hardly
99     efficient, but the list is short ... */
100   GtkTreeIter iter;
101
102   GtkTreeModel * model  = gtk_tree_view_get_model (treeview);
103
104   gboolean success;
105   for (success = gtk_tree_model_get_iter_first (model, &iter);
106        success;
107        success = gtk_tree_model_iter_next (model, &iter))
108     {
109       union value v;
110       GValue gvalue = {0};
111
112       gtk_tree_model_get_value (model, &iter, 1, &gvalue);
113
114       v.f = g_value_get_double (&gvalue);
115
116       if ( 0 == memcmp (&v, val, sizeof (union value)))
117         {
118           break;
119         }
120     }
121
122   path = gtk_tree_model_get_path (model, &iter);
123   if ( path )
124     {
125       gtk_tree_view_set_cursor (treeview, path, 0, 0);
126       gtk_tree_path_free (path);
127     }
128
129 }
130
131
132 /* This callback occurs when the text in the value entry box is
133    changed */
134 static void
135 on_value_entry_change (GtkEntry *entry, gpointer data)
136 {
137   const char *s;
138
139   struct val_labs_dialog *dialog = data;
140
141   const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
142
143   union value v;
144   text_to_value (text, &v,
145                 *var_get_write_format (dialog->pv));
146
147
148   g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
149                          dialog->change_handler_id);
150
151   gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),"");
152
153
154   if ( (s = val_labs_find (dialog->labs, &v)) )
155     {
156       gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), s);
157       gtk_widget_set_sensitive (dialog->add_button, FALSE);
158       gtk_widget_set_sensitive (dialog->remove_button, TRUE);
159       select_treeview_from_value (GTK_TREE_VIEW (dialog->treeview), &v);
160     }
161   else
162     {
163       gtk_widget_set_sensitive (dialog->remove_button, FALSE);
164       gtk_widget_set_sensitive (dialog->add_button, TRUE);
165     }
166
167   g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
168                          dialog->change_handler_id);
169 }
170
171
172 /* Callback for when the Value Labels dialog is closed using
173    the OK button.*/
174 static gint
175 val_labs_ok (GtkWidget *w, gpointer data)
176 {
177   struct val_labs_dialog *dialog = data;
178
179   var_set_value_labels (dialog->pv, dialog->labs);
180
181   val_labs_destroy (dialog->labs);
182
183   dialog->labs = 0;
184
185   gtk_widget_hide (dialog->window);
186
187   return FALSE;
188 }
189
190 /* Callback for when the Value Labels dialog is closed using
191    the Cancel button.*/
192 static void
193 val_labs_cancel (struct val_labs_dialog *dialog)
194 {
195   val_labs_destroy (dialog->labs);
196
197   dialog->labs = 0;
198
199   gtk_widget_hide (dialog->window);
200 }
201
202
203 /* Callback for when the Value Labels dialog is closed using
204    the Cancel button.*/
205 static gint
206 on_cancel (GtkWidget *w, gpointer data)
207 {
208   struct val_labs_dialog *dialog = data;
209
210   val_labs_cancel (dialog);
211
212   return FALSE;
213 }
214
215
216 /* Callback for when the Value Labels dialog is closed using
217    the window delete button.*/
218 static gint
219 on_delete (GtkWidget *w, GdkEvent *e, gpointer data)
220 {
221   struct val_labs_dialog *dialog = data;
222
223   val_labs_cancel (dialog);
224
225   return TRUE;
226 }
227
228
229 /* Return the value-label pair currently selected in the dialog box  */
230 static void
231 get_selected_tuple (struct val_labs_dialog *dialog,
232                     union value *valuep, const char **label)
233 {
234   GtkTreeView *treeview = GTK_TREE_VIEW (dialog->treeview);
235
236   GtkTreeIter iter ;
237   GValue the_value = {0};
238   union value value;
239
240   GtkTreeSelection* sel =  gtk_tree_view_get_selection (treeview);
241
242   GtkTreeModel * model  = gtk_tree_view_get_model (treeview);
243
244   gtk_tree_selection_get_selected (sel, &model, &iter);
245
246   gtk_tree_model_get_value (model, &iter, 1, &the_value);
247
248   value.f = g_value_get_double (&the_value);
249   g_value_unset (&the_value);
250
251   if (valuep != NULL)
252     *valuep = value;
253   if (label != NULL)
254     *label = val_labs_find (dialog->labs, &value);
255 }
256
257
258 static void repopulate_dialog (struct val_labs_dialog *dialog);
259
260 /* Callback which occurs when the "Change" button is clicked */
261 static gint
262 on_change (GtkWidget *w, gpointer data)
263 {
264   struct val_labs_dialog *dialog = data;
265
266   const gchar *val_text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
267
268   union value v;
269
270   text_to_value (val_text, &v,
271                 *var_get_write_format (dialog->pv));
272
273   val_labs_replace (dialog->labs, &v,
274                     gtk_entry_get_text (GTK_ENTRY (dialog->label_entry)));
275
276   gtk_widget_set_sensitive (dialog->change_button, FALSE);
277
278   repopulate_dialog (dialog);
279   gtk_widget_grab_focus (dialog->value_entry);
280
281   return FALSE;
282 }
283
284 /* Callback which occurs when the "Add" button is clicked */
285 static gint
286 on_add (GtkWidget *w, gpointer data)
287 {
288   struct val_labs_dialog *dialog = data;
289
290   union value v;
291
292   const gchar *text = gtk_entry_get_text (GTK_ENTRY (dialog->value_entry));
293
294   text_to_value (text, &v,
295                 *var_get_write_format (dialog->pv));
296
297
298   if ( ! val_labs_add (dialog->labs, &v,
299                        gtk_entry_get_text
300                        ( GTK_ENTRY (dialog->label_entry)) ) )
301     return FALSE;
302
303   gtk_widget_set_sensitive (dialog->add_button, FALSE);
304
305   repopulate_dialog (dialog);
306   gtk_widget_grab_focus (dialog->value_entry);
307
308   return FALSE;
309 }
310
311 /* Callback which occurs when the "Remove" button is clicked */
312 static gint
313 on_remove (GtkWidget *w, gpointer data)
314 {
315   struct val_labs_dialog *dialog = data;
316
317   union value value;
318   const struct val_lab *vl;
319
320   get_selected_tuple (dialog, &value, NULL);
321   vl = val_labs_lookup (dialog->labs, &value);
322   if (vl != NULL)
323     val_labs_remove (dialog->labs, vl);
324
325   repopulate_dialog (dialog);
326   gtk_widget_grab_focus (dialog->value_entry);
327
328   gtk_widget_set_sensitive (dialog->remove_button, FALSE);
329
330   return FALSE;
331 }
332
333
334
335 /* Callback which occurs when a line item is selected in the list of
336    value--label pairs.*/
337 static void
338 on_select_row (GtkTreeView *treeview, gpointer data)
339 {
340   gchar *labeltext;
341   struct val_labs_dialog *dialog = data;
342
343   union value value;
344   const char *label;
345
346   gchar *text;
347
348   PsppireVarStore *var_store =
349     PSPPIRE_VAR_STORE (psppire_sheet_get_model (dialog->vs));
350
351   get_selected_tuple (dialog, &value, &label);
352   text = value_to_text (value, *var_get_write_format (dialog->pv));
353
354   g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
355                          dialog->value_handler_id);
356
357   gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), text);
358
359   g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
360                          dialog->value_handler_id);
361   g_free (text);
362
363   g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
364                          dialog->change_handler_id);
365
366
367   labeltext = recode_string (UTF8, psppire_dict_encoding (var_store->dict),
368                              label, -1);
369
370   gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),
371                      labeltext);
372   g_free (labeltext);
373
374   g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
375                          dialog->change_handler_id);
376
377   gtk_widget_set_sensitive (dialog->remove_button, TRUE);
378   gtk_widget_set_sensitive (dialog->change_button, FALSE);
379 }
380
381
382 /* Create a new dialog box
383    (there should  normally be only one)*/
384 struct val_labs_dialog *
385 val_labs_dialog_create (GtkWindow *toplevel, PsppireSheet *sheet)
386 {
387   GtkTreeViewColumn *column;
388
389   GtkCellRenderer *renderer ;
390
391   GtkBuilder *xml = builder_new ("var-sheet-dialogs.ui");
392
393   struct val_labs_dialog *dialog = g_malloc (sizeof (*dialog));
394
395   dialog->window = get_widget_assert (xml,"val_labs_dialog");
396   dialog->value_entry = get_widget_assert (xml,"value_entry");
397   dialog->label_entry = get_widget_assert (xml,"label_entry");
398   dialog->vs = sheet;
399
400   gtk_window_set_transient_for
401     (GTK_WINDOW (dialog->window), toplevel);
402
403   dialog->add_button = get_widget_assert (xml, "val_labs_add");
404   dialog->remove_button = get_widget_assert (xml, "val_labs_remove");
405   dialog->change_button = get_widget_assert (xml, "val_labs_change");
406
407   dialog->treeview = get_widget_assert (xml,"treeview1");
408
409   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->treeview), FALSE);
410
411   renderer = gtk_cell_renderer_text_new ();
412
413   column = gtk_tree_view_column_new_with_attributes ("Title",
414                                                      renderer,
415                                                      "text",
416                                                      0,
417                                                      NULL);
418
419   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
420
421   g_signal_connect (get_widget_assert (xml, "val_labs_cancel"),
422                    "clicked",
423                    GTK_SIGNAL_FUNC (on_cancel), dialog);
424
425   g_signal_connect (dialog->window, "delete-event",
426                     GTK_SIGNAL_FUNC (on_delete), dialog);
427
428   g_signal_connect (get_widget_assert (xml, "val_labs_ok"),
429                    "clicked",
430                    GTK_SIGNAL_FUNC (val_labs_ok), dialog);
431
432   dialog->change_handler_id =
433     g_signal_connect (dialog->label_entry,
434                      "changed",
435                      GTK_SIGNAL_FUNC (on_label_entry_change), dialog);
436
437   dialog->value_handler_id  =
438     g_signal_connect (dialog->value_entry,
439                      "changed",
440                      GTK_SIGNAL_FUNC (on_value_entry_change), dialog);
441
442   g_signal_connect (dialog->change_button,
443                    "clicked",
444                    GTK_SIGNAL_FUNC (on_change), dialog);
445
446
447   g_signal_connect (dialog->treeview, "cursor-changed",
448                    GTK_SIGNAL_FUNC (on_select_row), dialog);
449
450   g_signal_connect (dialog->remove_button, "clicked",
451                    GTK_SIGNAL_FUNC (on_remove), dialog);
452
453   g_signal_connect (dialog->add_button, "clicked",
454                    GTK_SIGNAL_FUNC (on_add), dialog);
455
456   dialog->labs = 0;
457
458   g_object_unref (xml);
459
460   return dialog;
461 }
462
463
464 void
465 val_labs_dialog_set_target_variable (struct val_labs_dialog *dialog,
466                                      struct variable *var)
467 {
468   dialog->pv = var;
469 }
470
471
472
473 /* Populate the components of the dialog box, from the 'labs' member
474    variable */
475 static void
476 repopulate_dialog (struct val_labs_dialog *dialog)
477 {
478   const struct val_lab **labels;
479   size_t n_labels;
480   size_t i;
481
482   GtkTreeIter iter;
483
484   PsppireVarStore *var_store =
485     PSPPIRE_VAR_STORE (psppire_sheet_get_model (dialog->vs));
486
487   GtkListStore *list_store = gtk_list_store_new (2,
488                                                  G_TYPE_STRING,
489                                                  G_TYPE_DOUBLE);
490
491   g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
492                          dialog->change_handler_id);
493   g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
494                          dialog->value_handler_id);
495
496   gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), "");
497   gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), "");
498
499   g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
500                          dialog->value_handler_id);
501   g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
502                            dialog->change_handler_id);
503
504   labels = val_labs_sorted (dialog->labs);
505   n_labels = val_labs_count (dialog->labs);
506   for (i = 0; i < n_labels; i++)
507     {
508       const struct val_lab *vl = labels[i];
509
510       gchar *const vstr  =
511         value_to_text (vl->value,
512                       *var_get_write_format (dialog->pv));
513
514       gchar *labeltext =
515         recode_string (UTF8,
516                        psppire_dict_encoding (var_store->dict),
517                        val_lab_get_label (vl), -1);
518
519       gchar *const text = g_strdup_printf ("%s = \"%s\"",
520                                            vstr, labeltext);
521
522       gtk_list_store_append (list_store, &iter);
523       gtk_list_store_set (list_store, &iter,
524                           0, text,
525                           1, vl->value.f,
526                           -1);
527
528       g_free (labeltext);
529       g_free (text);
530       g_free (vstr);
531     }
532   free (labels);
533
534   gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
535                           GTK_TREE_MODEL (list_store));
536
537   g_object_unref (list_store);
538
539 }
540
541 /* Initialise and display the dialog box */
542 void
543 val_labs_dialog_show (struct val_labs_dialog *dialog)
544 {
545   const struct val_labs *value_labels;
546
547   g_assert (!dialog->labs);
548
549   value_labels = var_get_value_labels (dialog->pv);
550
551   if (value_labels)
552     dialog->labs = val_labs_clone ( value_labels );
553   else
554     dialog->labs = val_labs_create ( var_get_width (dialog->pv));
555
556   gtk_widget_set_sensitive (dialog->remove_button, FALSE);
557   gtk_widget_set_sensitive (dialog->change_button, FALSE);
558   gtk_widget_set_sensitive (dialog->add_button, FALSE);
559
560   gtk_widget_grab_focus (dialog->value_entry);
561
562   repopulate_dialog (dialog);
563   gtk_widget_show (dialog->window);
564 }
565