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