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