psppire_data_editor: Don't crash when trying to insert when no variable or case is...
[pspp] / src / ui / gui / psppire-data-editor.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010, 2011, 2012, 2016,
3    2017 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 #include "ui/gui/psppire-data-editor.h"
21
22 #include <gtk/gtk.h>
23
24 #include "data/datasheet.h"
25 #include "data/value-labels.h"
26 #include "libpspp/range-set.h"
27 #include "libpspp/str.h"
28
29 #include "ui/gui/helper.h"
30 #include "ui/gui/var-display.h"
31 #include "ui/gui/val-labs-dialog.h"
32 #include "ui/gui/missing-val-dialog.h"
33 #include "ui/gui/var-type-dialog.h"
34 #include "ui/gui/value-variant.h"
35 #include "ui/gui/psppire-dict.h"
36 #include "ui/gui/psppire-data-store.h"
37 #include "ui/gui/psppire-data-window.h"
38 #include "ui/gui/psppire-value-entry.h"
39 #include "ui/gui/psppire-conf.h"
40 #include "ui/gui/psppire-variable-sheet.h"
41 #include "ui/gui/psppire-data-sheet.h"
42
43
44 #include <ssw-sheet.h>
45
46 #include <gettext.h>
47 #define _(msgid) gettext (msgid)
48
49 static void psppire_data_editor_class_init          (PsppireDataEditorClass *klass);
50 static void psppire_data_editor_init                (PsppireDataEditor      *de);
51
52 static void refresh_entry (PsppireDataEditor *);
53
54 GType
55 psppire_data_editor_get_type (void)
56 {
57   static GType de_type = 0;
58
59   if (!de_type)
60     {
61       static const GTypeInfo de_info =
62       {
63         sizeof (PsppireDataEditorClass),
64         NULL, /* base_init */
65         NULL, /* base_finalize */
66         (GClassInitFunc) psppire_data_editor_class_init,
67         NULL, /* class_finalize */
68         NULL, /* class_data */
69         sizeof (PsppireDataEditor),
70         0,
71         (GInstanceInitFunc) psppire_data_editor_init,
72       };
73
74       de_type = g_type_register_static (GTK_TYPE_NOTEBOOK, "PsppireDataEditor",
75                                         &de_info, 0);
76     }
77
78   return de_type;
79 }
80
81 static GObjectClass * parent_class = NULL;
82
83 static void
84 psppire_data_editor_dispose (GObject *obj)
85 {
86   PsppireDataEditor *de = (PsppireDataEditor *) obj;
87
88   if (de->data_store)
89     {
90       g_object_unref (de->data_store);
91       de->data_store = NULL;
92     }
93
94   if (de->dict)
95     {
96       g_object_unref (de->dict);
97       de->dict = NULL;
98     }
99
100   if (de->font != NULL)
101     {
102       pango_font_description_free (de->font);
103       de->font = NULL;
104     }
105
106   /* Chain up to the parent class */
107   G_OBJECT_CLASS (parent_class)->dispose (obj);
108 }
109
110 enum
111   {
112     PROP_0,
113     PROP_DATA_STORE,
114     PROP_DICTIONARY,
115     PROP_VALUE_LABELS,
116     PROP_SPLIT_WINDOW
117   };
118
119 static void
120 psppire_data_editor_refresh_model (PsppireDataEditor *de)
121 {
122 }
123
124
125 static void
126 psppire_data_editor_set_property (GObject         *object,
127                                   guint            prop_id,
128                                   const GValue    *value,
129                                   GParamSpec      *pspec)
130 {
131   PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (object);
132
133   switch (prop_id)
134     {
135     case PROP_SPLIT_WINDOW:
136       de->split = g_value_get_boolean (value);
137       g_object_set (de->data_sheet, "split", de->split, NULL);
138       g_object_set (de->var_sheet, "split", de->split, NULL);
139       break;
140     case PROP_DATA_STORE:
141       if ( de->data_store)
142         {
143           g_signal_handlers_disconnect_by_func (de->data_store,
144                                                 G_CALLBACK (refresh_entry),
145                                                 de);
146           g_object_unref (de->data_store);
147         }
148
149       de->data_store = g_value_get_pointer (value);
150       g_object_ref (de->data_store);
151
152       g_object_set (de->data_sheet, "data-model", de->data_store, NULL);
153       psppire_data_editor_refresh_model (de);
154
155       g_signal_connect_swapped (de->data_sheet, "selection-changed",
156                                 G_CALLBACK (refresh_entry),
157                                 de);
158
159       g_signal_connect_swapped (de->data_store, "case-changed",
160                                 G_CALLBACK (refresh_entry), de);
161
162       break;
163     case PROP_DICTIONARY:
164       if (de->dict)
165         g_object_unref (de->dict);
166       de->dict = g_value_get_pointer (value);
167       g_object_ref (de->dict);
168
169       g_object_set (de->var_sheet, "data-model", de->dict, NULL);
170       break;
171
172     case PROP_VALUE_LABELS:
173       {
174         gboolean l = g_value_get_boolean (value);
175         g_object_set (de->data_sheet, "forward-conversion",
176                       l ?
177                       psppire_data_store_value_to_string_with_labels :
178                       psppire_data_store_value_to_string,
179                       NULL);
180       }
181       break;
182
183     default:
184       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185       break;
186     };
187 }
188
189 static void
190 psppire_data_editor_get_property (GObject         *object,
191                                   guint            prop_id,
192                                   GValue          *value,
193                                   GParamSpec      *pspec)
194 {
195   PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (object);
196
197   switch (prop_id)
198     {
199     case PROP_SPLIT_WINDOW:
200       g_value_set_boolean (value, de->split);
201       break;
202     case PROP_DATA_STORE:
203       g_value_set_pointer (value, de->data_store);
204       break;
205     case PROP_DICTIONARY:
206       g_value_set_pointer (value, de->dict);
207       break;
208     case PROP_VALUE_LABELS:
209       break;
210     default:
211       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212       break;
213     }
214 }
215
216 static void
217 psppire_data_editor_switch_page (GtkNotebook     *notebook,
218                                  GtkWidget *w,
219                                  guint            page_num)
220 {
221   GTK_NOTEBOOK_CLASS (parent_class)->switch_page (notebook, w, page_num);
222 }
223
224 static void
225 psppire_data_editor_set_focus_child (GtkContainer *container,
226                                      GtkWidget    *widget)
227 {
228   GTK_CONTAINER_CLASS (parent_class)->set_focus_child (container, widget);
229 }
230
231
232 static gboolean
233 on_key_press (GtkWidget *w, GdkEventKey *e)
234 {
235   PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (w);
236   if (e->keyval == GDK_KEY_F2 &&
237       PSPPIRE_DATA_EDITOR_DATA_VIEW == gtk_notebook_get_current_page (GTK_NOTEBOOK (de)))
238     {
239       gtk_widget_grab_focus (de->datum_entry);
240     }
241
242   return FALSE;
243 }
244
245 static void
246 psppire_data_editor_class_init (PsppireDataEditorClass *klass)
247 {
248   GParamSpec *data_store_spec ;
249   GParamSpec *dict_spec ;
250   GParamSpec *value_labels_spec;
251   GParamSpec *split_window_spec;
252
253   GObjectClass *object_class = G_OBJECT_CLASS (klass);
254   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
255   GtkNotebookClass *notebook_class = GTK_NOTEBOOK_CLASS (klass);
256   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
257
258   parent_class = g_type_class_peek_parent (klass);
259
260   object_class->dispose = psppire_data_editor_dispose;
261   object_class->set_property = psppire_data_editor_set_property;
262   object_class->get_property = psppire_data_editor_get_property;
263
264   container_class->set_focus_child = psppire_data_editor_set_focus_child;
265   notebook_class->switch_page = psppire_data_editor_switch_page;
266   widget_class->key_press_event = on_key_press;
267
268   data_store_spec =
269     g_param_spec_pointer ("data-store",
270                           "Data Store",
271                           "A pointer to the data store associated with this editor",
272                           G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE );
273
274   g_object_class_install_property (object_class,
275                                    PROP_DATA_STORE,
276                                    data_store_spec);
277
278   dict_spec =
279     g_param_spec_pointer ("dictionary",
280                           "Dictionary",
281                           "A pointer to the dictionary associated with this editor",
282                           G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE );
283
284   g_object_class_install_property (object_class,
285                                    PROP_DICTIONARY,
286                                    dict_spec);
287
288   value_labels_spec =
289     g_param_spec_boolean ("value-labels",
290                          "Value Labels",
291                          "Whether or not the data sheet should display labels instead of values",
292                           FALSE,
293                          G_PARAM_WRITABLE | G_PARAM_READABLE);
294
295   g_object_class_install_property (object_class,
296                                    PROP_VALUE_LABELS,
297                                    value_labels_spec);
298
299
300   split_window_spec =
301     g_param_spec_boolean ("split",
302                           "Split Window",
303                           "True iff the data sheet is split",
304                           FALSE,
305                           G_PARAM_READABLE | G_PARAM_WRITABLE);
306
307   g_object_class_install_property (object_class,
308                                    PROP_SPLIT_WINDOW,
309                                    split_window_spec);
310 }
311
312
313 static void
314 on_var_sheet_var_double_clicked (void *var_sheet, gint dict_index,
315                                  PsppireDataEditor *de)
316 {
317   gtk_notebook_set_current_page (GTK_NOTEBOOK (de),
318                                  PSPPIRE_DATA_EDITOR_DATA_VIEW);
319
320   ssw_sheet_scroll_to (SSW_SHEET (de->data_sheet), dict_index, -1);
321 }
322
323
324 static void
325 on_data_sheet_var_double_clicked (SswSheet *data_sheet, gint dict_index,
326                                  PsppireDataEditor *de)
327 {
328
329   gtk_notebook_set_current_page (GTK_NOTEBOOK (de),
330                                  PSPPIRE_DATA_EDITOR_VARIABLE_VIEW);
331
332   ssw_sheet_scroll_to (SSW_SHEET (de->var_sheet), -1, dict_index);
333 }
334
335
336
337 /* Refreshes 'de->cell_ref_label' and 'de->datum_entry' from the currently
338    active cell or cells. */
339 static void
340 refresh_entry (PsppireDataEditor *de)
341 {
342   gint row, col;
343   if (ssw_sheet_get_active_cell (SSW_SHEET (de->data_sheet), &col, &row))
344     {
345       union value val;
346       const struct variable *var = psppire_dict_get_variable (de->dict, col);
347       if (var == NULL)
348         return;
349
350       psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (de->datum_entry), var);
351
352       int width = var_get_width (var);
353       if (! psppire_data_store_get_value (PSPPIRE_DATA_STORE (de->data_store),
354                                           row, var, &val))
355         return;
356
357       psppire_value_entry_set_value (PSPPIRE_VALUE_ENTRY (de->datum_entry),
358                                      &val, width);
359       value_destroy (&val, width);
360     }
361 }
362
363 static void
364 on_datum_entry_activate (GtkEntry *entry, PsppireDataEditor *de)
365 {
366   gint row, col;
367   if (ssw_sheet_get_active_cell (SSW_SHEET (de->data_sheet), &col, &row))
368     {
369       union value val;
370       const struct variable *var = psppire_dict_get_variable (de->dict, col);
371       if (var == NULL)
372         return;
373
374       int width = var_get_width (var);
375       value_init (&val, width);
376       if (psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (de->datum_entry),
377                                          &val, width))
378         {
379           psppire_data_store_set_value (de->data_store, row, var, &val);
380         }
381       value_destroy (&val, width);
382
383       gtk_widget_grab_focus (de->data_sheet);
384       ssw_sheet_set_active_cell (SSW_SHEET (de->data_sheet), col, row, NULL);
385     }
386 }
387
388
389 /* Called when the active cell or the selection in the data sheet changes */
390 static void
391 on_data_selection_change (PsppireDataEditor *de, SswRange *sel)
392 {
393   gchar *ref_cell_text = NULL;
394
395   gint n_cases = abs (sel->end_y - sel->start_y) + 1;
396   gint n_vars = abs (sel->end_x - sel->start_x) + 1;
397
398   if (n_cases == 1 && n_vars == 1)
399     {
400       /* A single cell is selected */
401       const struct variable *var = psppire_dict_get_variable (de->dict, sel->start_x);
402
403       if (var)
404         ref_cell_text = g_strdup_printf (_("%d : %s"),
405                                          sel->start_y + 1, var_get_name (var));
406     }
407   else
408     {
409       struct string s;
410
411       /* The glib string library does not understand the ' printf modifier
412          on all platforms, but the "struct string" library does (because
413          Gnulib fixes that problem), so use the latter.  */
414       ds_init_empty (&s);
415       ds_put_format (&s, ngettext ("%'d case", "%'d cases", n_cases),
416                      n_cases);
417       ds_put_byte (&s, ' ');
418       ds_put_unichar (&s, 0xd7); /* U+00D7 MULTIPLICATION SIGN */
419       ds_put_byte (&s, ' ');
420       ds_put_format (&s, ngettext ("%'d variable", "%'d variables",
421                                    n_vars),
422                      n_vars);
423       ref_cell_text = ds_steal_cstr (&s);
424     }
425
426   gtk_label_set_label (GTK_LABEL (de->cell_ref_label),
427                        ref_cell_text ? ref_cell_text : "");
428
429   g_free (ref_cell_text);
430 }
431
432
433 static void set_font_recursively (GtkWidget *w, gpointer data);
434
435 \f
436
437 void
438 psppire_data_editor_data_delete_variables (PsppireDataEditor *de)
439 {
440   SswRange *range = SSW_SHEET(de->data_sheet)->selection;
441
442   psppire_dict_delete_variables (de->dict, range->start_x,
443                                  (range->end_x - range->start_x + 1));
444
445   gtk_widget_queue_draw (GTK_WIDGET (de->data_sheet));
446 }
447
448 void
449 psppire_data_editor_var_delete_variables (PsppireDataEditor *de)
450 {
451   SswRange *range = SSW_SHEET(de->var_sheet)->selection;
452
453   psppire_dict_delete_variables (de->dict, range->start_y,
454                                  (range->end_y - range->start_y + 1));
455
456   gtk_widget_queue_draw (GTK_WIDGET (de->var_sheet));
457 }
458
459 void
460 psppire_data_editor_insert_new_case_at_posn  (PsppireDataEditor *de, gint posn)
461 {
462   g_return_if_fail (posn >= 0);
463
464   psppire_data_store_insert_new_case (de->data_store, posn);
465
466   gtk_widget_queue_draw (GTK_WIDGET (de->data_sheet));
467 }
468
469 void
470 psppire_data_editor_insert_new_variable_at_posn (PsppireDataEditor *de, gint posn)
471 {
472   g_return_if_fail (posn >= 0);
473   const struct variable *v = psppire_dict_insert_variable (de->dict, posn, NULL);
474   g_return_if_fail (v);
475   psppire_data_store_insert_value (de->data_store, var_get_width(v),
476                                    var_get_case_index (v));
477
478   gtk_widget_queue_draw (GTK_WIDGET (de));
479 }
480
481 static void
482 psppire_data_editor_init (PsppireDataEditor *de)
483 {
484   GtkWidget *hbox;
485   gchar *fontname = NULL;
486
487   GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (de));
488   gtk_style_context_add_class (context, "psppire-data-editor");
489
490   de->font = NULL;
491
492   g_object_set (de, "tab-pos", GTK_POS_BOTTOM, NULL);
493
494   de->cell_ref_label = gtk_label_new ("");
495   gtk_label_set_width_chars (GTK_LABEL (de->cell_ref_label), 25);
496   gtk_widget_set_valign (de->cell_ref_label, GTK_ALIGN_CENTER);
497
498   de->datum_entry = psppire_value_entry_new ();
499   g_signal_connect (de->datum_entry, "edit-done",
500                     G_CALLBACK (on_datum_entry_activate), de);
501
502   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
503   gtk_box_pack_start (GTK_BOX (hbox), de->cell_ref_label, FALSE, FALSE, 0);
504   gtk_box_pack_start (GTK_BOX (hbox), de->datum_entry, TRUE, TRUE, 0);
505
506   de->split = FALSE;
507   de->data_sheet = psppire_data_sheet_new ();
508
509   GtkWidget *data_button = ssw_sheet_get_button (SSW_SHEET (de->data_sheet));
510   gtk_button_set_label (GTK_BUTTON (data_button), _("Case"));
511   de->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
512   gtk_box_pack_start (GTK_BOX (de->vbox), hbox, FALSE, FALSE, 0);
513   gtk_box_pack_start (GTK_BOX (de->vbox), de->data_sheet, TRUE, TRUE, 0);
514
515
516   g_signal_connect_swapped (de->data_sheet, "selection-changed",
517                     G_CALLBACK (on_data_selection_change), de);
518
519   gtk_notebook_append_page (GTK_NOTEBOOK (de), de->vbox,
520                             gtk_label_new_with_mnemonic (_("Data View")));
521
522   gtk_widget_show_all (de->vbox);
523
524   de->var_sheet = psppire_variable_sheet_new ();
525
526   GtkWidget *var_button = ssw_sheet_get_button (SSW_SHEET (de->var_sheet));
527   gtk_button_set_label (GTK_BUTTON (var_button), _("Variable"));
528
529   gtk_notebook_append_page (GTK_NOTEBOOK (de), de->var_sheet,
530                             gtk_label_new_with_mnemonic (_("Variable View")));
531
532   gtk_widget_show_all (de->var_sheet);
533
534   g_signal_connect (de->var_sheet, "row-header-double-clicked",
535                     G_CALLBACK (on_var_sheet_var_double_clicked), de);
536
537   g_signal_connect (de->data_sheet, "column-header-double-clicked",
538                     G_CALLBACK (on_data_sheet_var_double_clicked), de);
539
540   g_object_set (de, "can-focus", FALSE, NULL);
541
542   if (psppire_conf_get_string (psppire_conf_new (),
543                            "Data Editor", "font",
544                                 &fontname) )
545     {
546       de->font = pango_font_description_from_string (fontname);
547       g_free (fontname);
548       set_font_recursively (GTK_WIDGET (de), de->font);
549     }
550
551   gtk_widget_add_events (GTK_WIDGET (de), GDK_KEY_PRESS_MASK);
552 }
553
554 GtkWidget*
555 psppire_data_editor_new (PsppireDict *dict,
556                          PsppireDataStore *data_store)
557 {
558   return  g_object_new (PSPPIRE_DATA_EDITOR_TYPE,
559                         "dictionary",  dict,
560                         "data-store",  data_store,
561                         NULL);
562 }
563 \f
564 /* Turns the visible grid on or off, according to GRID_VISIBLE, for DE's data
565    sheet(s) and variable sheet. */
566 void
567 psppire_data_editor_show_grid (PsppireDataEditor *de, gboolean grid_visible)
568 {
569   g_object_set (SSW_SHEET (de->var_sheet), "gridlines", grid_visible, NULL);
570   g_object_set (SSW_SHEET (de->data_sheet), "gridlines", grid_visible, NULL);
571 }
572
573
574 static void
575 set_font_recursively (GtkWidget *w, gpointer data)
576 {
577   PangoFontDescription *font_desc = data;
578
579   GtkStyleContext *style = gtk_widget_get_style_context (w);
580   GtkCssProvider *cssp = gtk_css_provider_new ();
581
582   gchar *str = pango_font_description_to_string (font_desc);
583   gchar *css =
584     g_strdup_printf ("* {font: %s}", str);
585   g_free (str);
586
587   GError *err = NULL;
588   gtk_css_provider_load_from_data (cssp, css, -1, &err);
589   if (err)
590     {
591       g_warning ("Failed to load font css \"%s\": %s", css, err->message);
592       g_error_free (err);
593     }
594   g_free (css);
595
596   gtk_style_context_add_provider (style,
597                                   GTK_STYLE_PROVIDER (cssp),
598                                   GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
599   g_object_unref (cssp);
600
601
602   if ( GTK_IS_CONTAINER (w))
603     gtk_container_foreach (GTK_CONTAINER (w), set_font_recursively, font_desc);
604 }
605
606 /* Sets FONT_DESC as the font used by the data sheet(s) and variable sheet. */
607 void
608 psppire_data_editor_set_font (PsppireDataEditor *de, PangoFontDescription *font_desc)
609 {
610   gchar *font_name;
611   set_font_recursively (GTK_WIDGET (de), font_desc);
612
613   if (de->font)
614     pango_font_description_free (de->font);
615   de->font = pango_font_description_copy (font_desc);
616   font_name = pango_font_description_to_string (de->font);
617
618   psppire_conf_set_string (psppire_conf_new (),
619                            "Data Editor", "font",
620                            font_name);
621   g_free (font_name);
622 }
623
624 /* If SPLIT is TRUE, splits DE's data sheet into four panes.
625    If SPLIT is FALSE, un-splits it into a single pane. */
626 void
627 psppire_data_editor_split_window (PsppireDataEditor *de, gboolean split)
628 {
629   g_object_set (de, "split", split, NULL);
630 }
631
632 /* Makes the variable with dictionary index DICT_INDEX in DE's dictionary
633    visible and selected in the active view in DE. */
634 void
635 psppire_data_editor_goto_variable (PsppireDataEditor *de, gint dict_index)
636 {
637   gint page = gtk_notebook_get_current_page (GTK_NOTEBOOK (de));
638
639   switch (page)
640     {
641       case PSPPIRE_DATA_EDITOR_DATA_VIEW:
642         ssw_sheet_scroll_to (SSW_SHEET (de->data_sheet), dict_index, -1);
643         ssw_sheet_set_active_cell (SSW_SHEET (de->data_sheet), dict_index, -1, NULL);
644         break;
645       case PSPPIRE_DATA_EDITOR_VARIABLE_VIEW:
646         ssw_sheet_scroll_to (SSW_SHEET (de->var_sheet), -1, dict_index);
647         ssw_sheet_set_active_cell (SSW_SHEET (de->var_sheet), -1, dict_index, NULL);
648         break;
649     }
650 }
651
652 /* Set the datum at COL, ROW, to that contained in VALUE.
653  */
654 static void
655 store_set_datum (GtkTreeModel *model, gint col, gint row,
656                          const GValue *value)
657 {
658   PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
659   GVariant *v = g_value_get_variant (value);
660   union value uv;
661   value_variant_get (&uv, v);
662   const struct variable *var = psppire_dict_get_variable (store->dict, col);
663   psppire_data_store_set_value (store, row, var, &uv);
664   value_destroy_from_variant (&uv, v);
665 }
666
667 void
668 psppire_data_editor_paste (PsppireDataEditor *de)
669 {
670   SswSheet *sheet = SSW_SHEET (de->data_sheet);
671   GtkClipboard *clip =
672     gtk_clipboard_get_for_display (gtk_widget_get_display (GTK_WIDGET (sheet)),
673                                    GDK_SELECTION_CLIPBOARD);
674
675   ssw_sheet_paste (sheet, clip, store_set_datum);
676 }