Convert to utf8 in data_out function.
[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   struct val_labs_dialog *dialog = data;
341
342   union value value;
343   const char *label = NULL;
344
345   gchar *text;
346
347   get_selected_tuple (dialog, &value, &label);
348   text = value_to_text (value, NULL, *var_get_write_format (dialog->pv));
349
350   g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
351                          dialog->value_handler_id);
352
353   gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), text);
354
355   g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
356                          dialog->value_handler_id);
357   g_free (text);
358
359   g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
360                          dialog->change_handler_id);
361
362
363   gtk_entry_set_text (GTK_ENTRY (dialog->label_entry),
364                       label);
365
366   g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
367                          dialog->change_handler_id);
368
369   gtk_widget_set_sensitive (dialog->remove_button, TRUE);
370   gtk_widget_set_sensitive (dialog->change_button, FALSE);
371 }
372
373
374 /* Create a new dialog box
375    (there should  normally be only one)*/
376 struct val_labs_dialog *
377 val_labs_dialog_create (GtkWindow *toplevel, PsppireSheet *sheet)
378 {
379   GtkTreeViewColumn *column;
380
381   GtkCellRenderer *renderer ;
382
383   GtkBuilder *xml = builder_new ("var-sheet-dialogs.ui");
384
385   struct val_labs_dialog *dialog = g_malloc (sizeof (*dialog));
386
387   dialog->window = get_widget_assert (xml,"val_labs_dialog");
388   dialog->value_entry = get_widget_assert (xml,"value_entry");
389   dialog->label_entry = get_widget_assert (xml,"label_entry");
390   dialog->vs = sheet;
391
392   gtk_window_set_transient_for
393     (GTK_WINDOW (dialog->window), toplevel);
394
395   dialog->add_button = get_widget_assert (xml, "val_labs_add");
396   dialog->remove_button = get_widget_assert (xml, "val_labs_remove");
397   dialog->change_button = get_widget_assert (xml, "val_labs_change");
398
399   dialog->treeview = get_widget_assert (xml,"treeview1");
400
401   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->treeview), FALSE);
402
403   renderer = gtk_cell_renderer_text_new ();
404
405   column = gtk_tree_view_column_new_with_attributes ("Title",
406                                                      renderer,
407                                                      "text",
408                                                      0,
409                                                      NULL);
410
411   gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->treeview), column);
412
413   g_signal_connect (get_widget_assert (xml, "val_labs_cancel"),
414                    "clicked",
415                    GTK_SIGNAL_FUNC (on_cancel), dialog);
416
417   g_signal_connect (dialog->window, "delete-event",
418                     GTK_SIGNAL_FUNC (on_delete), dialog);
419
420   g_signal_connect (get_widget_assert (xml, "val_labs_ok"),
421                    "clicked",
422                    GTK_SIGNAL_FUNC (val_labs_ok), dialog);
423
424   dialog->change_handler_id =
425     g_signal_connect (dialog->label_entry,
426                      "changed",
427                      GTK_SIGNAL_FUNC (on_label_entry_change), dialog);
428
429   dialog->value_handler_id  =
430     g_signal_connect (dialog->value_entry,
431                      "changed",
432                      GTK_SIGNAL_FUNC (on_value_entry_change), dialog);
433
434   g_signal_connect (dialog->change_button,
435                    "clicked",
436                    GTK_SIGNAL_FUNC (on_change), dialog);
437
438
439   g_signal_connect (dialog->treeview, "cursor-changed",
440                    GTK_SIGNAL_FUNC (on_select_row), dialog);
441
442   g_signal_connect (dialog->remove_button, "clicked",
443                    GTK_SIGNAL_FUNC (on_remove), dialog);
444
445   g_signal_connect (dialog->add_button, "clicked",
446                    GTK_SIGNAL_FUNC (on_add), dialog);
447
448   dialog->labs = 0;
449
450   g_object_unref (xml);
451
452   return dialog;
453 }
454
455
456 void
457 val_labs_dialog_set_target_variable (struct val_labs_dialog *dialog,
458                                      struct variable *var)
459 {
460   dialog->pv = var;
461 }
462
463
464
465 /* Populate the components of the dialog box, from the 'labs' member
466    variable */
467 static void
468 repopulate_dialog (struct val_labs_dialog *dialog)
469 {
470   const struct val_lab **labels;
471   size_t n_labels;
472   size_t i;
473
474   GtkTreeIter iter;
475
476   GtkListStore *list_store = gtk_list_store_new (2,
477                                                  G_TYPE_STRING,
478                                                  G_TYPE_DOUBLE);
479
480   g_signal_handler_block (GTK_ENTRY (dialog->label_entry),
481                          dialog->change_handler_id);
482   g_signal_handler_block (GTK_ENTRY (dialog->value_entry),
483                          dialog->value_handler_id);
484
485   gtk_entry_set_text (GTK_ENTRY (dialog->value_entry), "");
486   gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), "");
487
488   g_signal_handler_unblock (GTK_ENTRY (dialog->value_entry),
489                          dialog->value_handler_id);
490   g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry),
491                            dialog->change_handler_id);
492
493   labels = val_labs_sorted (dialog->labs);
494   n_labels = val_labs_count (dialog->labs);
495   for (i = 0; i < n_labels; i++)
496     {
497       const struct val_lab *vl = labels[i];
498
499       gchar *const vstr  =
500         value_to_text (vl->value, NULL,
501                       *var_get_write_format (dialog->pv));
502
503       gchar *const text = g_strdup_printf ("%s = \"%s\"",
504                                            vstr, val_lab_get_label (vl));
505
506       gtk_list_store_append (list_store, &iter);
507       gtk_list_store_set (list_store, &iter,
508                           0, text,
509                           1, vl->value.f,
510                           -1);
511
512       g_free (text);
513       g_free (vstr);
514     }
515   free (labels);
516
517   gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
518                           GTK_TREE_MODEL (list_store));
519
520   g_object_unref (list_store);
521
522 }
523
524 /* Initialise and display the dialog box */
525 void
526 val_labs_dialog_show (struct val_labs_dialog *dialog)
527 {
528   const struct val_labs *value_labels;
529
530   g_assert (!dialog->labs);
531
532   value_labels = var_get_value_labels (dialog->pv);
533
534   if (value_labels)
535     dialog->labs = val_labs_clone ( value_labels );
536   else
537     dialog->labs = val_labs_create ( var_get_width (dialog->pv));
538
539   gtk_widget_set_sensitive (dialog->remove_button, FALSE);
540   gtk_widget_set_sensitive (dialog->change_button, FALSE);
541   gtk_widget_set_sensitive (dialog->add_button, FALSE);
542
543   gtk_widget_grab_focus (dialog->value_entry);
544
545   repopulate_dialog (dialog);
546   gtk_widget_show (dialog->window);
547 }
548