Committed patch #5636
[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
33 /* This callback occurs when the text in the label entry box
34    is changed */
35 static void
36 on_label_entry_change(GtkEntry *entry, gpointer data)
37 {
38   union value v;
39   const gchar *text ;
40   struct val_labs_dialog *dialog = data;
41   g_assert(dialog->labs);
42
43   text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
44
45   text_to_value(text, &v,
46                 *var_get_write_format (dialog->pv));
47
48
49   if ( val_labs_find (dialog->labs, v) )
50     {
51       gtk_widget_set_sensitive(dialog->change_button, TRUE);
52       gtk_widget_set_sensitive(dialog->add_button, FALSE);
53     }
54   else
55     {
56       gtk_widget_set_sensitive(dialog->change_button, FALSE);
57       gtk_widget_set_sensitive(dialog->add_button, TRUE);
58     }
59 }
60
61
62 /* Set the TREEVIEW list cursor to the item which has the value VAL */
63 static void
64 select_treeview_from_value(GtkTreeView *treeview, union value *val)
65 {
66   GtkTreePath *path ;
67
68   /*
69     We do this with a linear search through the model --- hardly
70     efficient, but the list is short ... */
71   GtkTreeIter iter;
72
73   GtkTreeModel * model  = gtk_tree_view_get_model(treeview);
74
75   gboolean success;
76   for (success = gtk_tree_model_get_iter_first(model, &iter);
77        success;
78        success = gtk_tree_model_iter_next(model, &iter))
79     {
80       union value v;
81       GValue gvalue = {0};
82
83       gtk_tree_model_get_value(model, &iter, 1, &gvalue);
84
85       v.f = g_value_get_double(&gvalue);
86
87       if ( 0 == memcmp(&v, val, sizeof (union value)))
88         {
89           break;
90         }
91     }
92
93   path = gtk_tree_model_get_path(model, &iter);
94   if ( path )
95     {
96       gtk_tree_view_set_cursor(treeview, path, 0, 0);
97       gtk_tree_path_free(path);
98     }
99
100 }
101
102
103 /* This callback occurs when the text in the value entry box is
104    changed */
105 static void
106 on_value_entry_change(GtkEntry *entry, gpointer data)
107 {
108   char *s;
109
110   struct val_labs_dialog *dialog = data;
111
112   const gchar *text = gtk_entry_get_text(GTK_ENTRY(dialog->value_entry));
113
114   union value v;
115   text_to_value(text, &v,
116                 *var_get_write_format (dialog->pv));
117
118
119   g_signal_handler_block(GTK_ENTRY(dialog->label_entry),
120                          dialog->change_handler_id);
121
122   gtk_entry_set_text(GTK_ENTRY(dialog->label_entry),"");
123
124
125   if ( (s = val_labs_find (dialog->labs, v)) )
126     {
127       gtk_entry_set_text(GTK_ENTRY(dialog->label_entry), s);
128       gtk_widget_set_sensitive(dialog->add_button, FALSE);
129       gtk_widget_set_sensitive(dialog->remove_button, TRUE);
130       select_treeview_from_value(GTK_TREE_VIEW(dialog->treeview), &v);
131     }
132   else
133     {
134       gtk_widget_set_sensitive(dialog->remove_button, FALSE);
135       gtk_widget_set_sensitive(dialog->add_button, TRUE);
136     }
137
138   g_signal_handler_unblock(GTK_ENTRY(dialog->label_entry),
139                          dialog->change_handler_id);
140 }
141
142
143 /* Callback for when the Value Labels dialog is closed using
144    the OK button.*/
145 static gint
146 val_labs_ok(GtkWidget *w, gpointer data)
147 {
148   struct val_labs_dialog *dialog = data;
149
150   var_set_value_labels (dialog->pv, dialog->labs);
151
152   val_labs_destroy (dialog->labs);
153
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                 *var_get_write_format (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                 *var_get_write_format (dialog->pv));
237
238
239   if ( ! val_labs_add (dialog->labs, v,
240                        gtk_entry_get_text
241                        ( GTK_ENTRY (dialog->label_entry)) ) )
242     return FALSE;
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                                     *var_get_write_format (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                       *var_get_write_format (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   const struct val_labs *value_labels;
455
456   g_assert(!dialog->labs);
457
458   value_labels = var_get_value_labels (dialog->pv);
459
460   if (value_labels)
461     dialog->labs = val_labs_copy ( value_labels );
462   else
463     dialog->labs = val_labs_create ( var_get_width (dialog->pv));
464   
465   gtk_widget_set_sensitive(dialog->remove_button, FALSE);
466   gtk_widget_set_sensitive(dialog->change_button, FALSE);
467   gtk_widget_set_sensitive(dialog->add_button, FALSE);
468
469   repopulate_dialog(dialog);
470   gtk_widget_show(dialog->window);
471 }
472