1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010, 2011, 2012, 2016,
3 2017 Free Software Foundation, Inc.
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.
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.
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/>. */
20 #include "ui/gui/psppire-data-editor.h"
23 #include <gtk-contrib/gtkxpaned.h>
25 #include "data/datasheet.h"
26 #include "data/value-labels.h"
27 #include "libpspp/range-set.h"
28 #include "libpspp/str.h"
30 #include "ui/gui/helper.h"
31 #include "ui/gui/var-display.h"
32 #include "ui/gui/val-labs-dialog.h"
33 #include "ui/gui/missing-val-dialog.h"
34 #include "ui/gui/var-type-dialog.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"
44 #include <ssw-sheet.h>
47 #define _(msgid) gettext (msgid)
49 static void psppire_data_editor_class_init (PsppireDataEditorClass *klass);
50 static void psppire_data_editor_init (PsppireDataEditor *de);
52 static void refresh_entry (PsppireDataEditor *);
55 psppire_data_editor_get_type (void)
57 static GType de_type = 0;
61 static const GTypeInfo de_info =
63 sizeof (PsppireDataEditorClass),
65 NULL, /* base_finalize */
66 (GClassInitFunc) psppire_data_editor_class_init,
67 NULL, /* class_finalize */
68 NULL, /* class_data */
69 sizeof (PsppireDataEditor),
71 (GInstanceInitFunc) psppire_data_editor_init,
74 de_type = g_type_register_static (GTK_TYPE_NOTEBOOK, "PsppireDataEditor",
81 static GObjectClass * parent_class = NULL;
84 psppire_data_editor_dispose (GObject *obj)
86 PsppireDataEditor *de = (PsppireDataEditor *) obj;
90 g_object_unref (de->data_store);
91 de->data_store = NULL;
96 g_object_unref (de->dict);
100 if (de->font != NULL)
102 pango_font_description_free (de->font);
106 /* Chain up to the parent class */
107 G_OBJECT_CLASS (parent_class)->dispose (obj);
120 psppire_data_editor_refresh_model (PsppireDataEditor *de)
126 psppire_data_editor_set_property (GObject *object,
131 PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (object);
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);
140 case PROP_DATA_STORE:
143 g_signal_handlers_disconnect_by_func (de->data_store,
144 G_CALLBACK (refresh_entry),
146 g_object_unref (de->data_store);
149 de->data_store = g_value_get_pointer (value);
150 g_object_ref (de->data_store);
152 g_object_set (de->data_sheet, "data-model", de->data_store, NULL);
153 psppire_data_editor_refresh_model (de);
155 g_signal_connect_swapped (de->data_sheet, "selection-changed",
156 G_CALLBACK (refresh_entry),
159 g_signal_connect_swapped (de->data_store, "case-changed",
160 G_CALLBACK (refresh_entry), de);
163 case PROP_DICTIONARY:
165 g_object_unref (de->dict);
166 de->dict = g_value_get_pointer (value);
167 g_object_ref (de->dict);
169 g_object_set (de->var_sheet, "data-model", de->dict, NULL);
171 case PROP_VALUE_LABELS:
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 psppire_data_editor_get_property (GObject *object,
186 PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (object);
190 case PROP_SPLIT_WINDOW:
191 g_value_set_boolean (value, de->split);
193 case PROP_DATA_STORE:
194 g_value_set_pointer (value, de->data_store);
196 case PROP_DICTIONARY:
197 g_value_set_pointer (value, de->dict);
199 case PROP_VALUE_LABELS:
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208 psppire_data_editor_switch_page (GtkNotebook *notebook,
212 GTK_NOTEBOOK_CLASS (parent_class)->switch_page (notebook, w, page_num);
217 psppire_data_editor_set_focus_child (GtkContainer *container,
220 GTK_CONTAINER_CLASS (parent_class)->set_focus_child (container, widget);
225 psppire_data_editor_class_init (PsppireDataEditorClass *klass)
227 GParamSpec *data_store_spec ;
228 GParamSpec *dict_spec ;
229 GParamSpec *value_labels_spec;
230 GParamSpec *split_window_spec;
232 GObjectClass *object_class = G_OBJECT_CLASS (klass);
233 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
234 GtkNotebookClass *notebook_class = GTK_NOTEBOOK_CLASS (klass);
236 parent_class = g_type_class_peek_parent (klass);
238 object_class->dispose = psppire_data_editor_dispose;
239 object_class->set_property = psppire_data_editor_set_property;
240 object_class->get_property = psppire_data_editor_get_property;
242 container_class->set_focus_child = psppire_data_editor_set_focus_child;
244 notebook_class->switch_page = psppire_data_editor_switch_page;
247 g_param_spec_pointer ("data-store",
249 "A pointer to the data store associated with this editor",
250 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE );
252 g_object_class_install_property (object_class,
257 g_param_spec_pointer ("dictionary",
259 "A pointer to the dictionary associated with this editor",
260 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE );
262 g_object_class_install_property (object_class,
267 g_param_spec_boolean ("value-labels",
269 "Whether or not the data sheet should display labels instead of values",
271 G_PARAM_WRITABLE | G_PARAM_READABLE);
273 g_object_class_install_property (object_class,
279 g_param_spec_boolean ("split",
281 "True iff the data sheet is split",
283 G_PARAM_READABLE | G_PARAM_WRITABLE);
285 g_object_class_install_property (object_class,
292 on_var_sheet_var_double_clicked (void *var_sheet, gint dict_index,
293 PsppireDataEditor *de)
295 gtk_notebook_set_current_page (GTK_NOTEBOOK (de),
296 PSPPIRE_DATA_EDITOR_DATA_VIEW);
298 ssw_sheet_scroll_to (SSW_SHEET (de->data_sheet), dict_index, -1);
303 on_data_sheet_var_double_clicked (SswSheet *data_sheet, gint dict_index,
304 PsppireDataEditor *de)
307 gtk_notebook_set_current_page (GTK_NOTEBOOK (de),
308 PSPPIRE_DATA_EDITOR_VARIABLE_VIEW);
310 ssw_sheet_scroll_to (SSW_SHEET (de->var_sheet), -1, dict_index);
315 /* Refreshes 'de->cell_ref_label' and 'de->datum_entry' from the currently
316 active cell or cells. */
318 refresh_entry (PsppireDataEditor *de)
321 if (ssw_sheet_get_active_cell (SSW_SHEET (de->data_sheet), &col, &row))
324 const struct variable *var = psppire_dict_get_variable (de->dict, col);
328 psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (de->datum_entry), var);
330 int width = var_get_width (var);
331 if (! psppire_data_store_get_value (PSPPIRE_DATA_STORE (de->data_store),
335 psppire_value_entry_set_value (PSPPIRE_VALUE_ENTRY (de->datum_entry),
337 value_destroy (&val, width);
342 on_datum_entry_activate (PsppireValueEntry *entry, PsppireDataEditor *de)
347 /* Called when the active cell or the selection in the data sheet changes */
349 on_data_selection_change (PsppireDataEditor *de, SswRange *sel)
351 gchar *ref_cell_text = NULL;
353 gint n_cases = abs (sel->end_y - sel->start_y) + 1;
354 gint n_vars = abs (sel->end_x - sel->start_x) + 1;
356 if (n_cases == 1 && n_vars == 1)
358 /* A single cell is selected */
359 const struct variable *var = psppire_dict_get_variable (de->dict, sel->start_x);
362 ref_cell_text = g_strdup_printf (_("%d : %s"),
363 sel->start_y + 1, var_get_name (var));
369 /* The glib string library does not understand the ' printf modifier
370 on all platforms, but the "struct string" library does (because
371 Gnulib fixes that problem), so use the latter. */
373 ds_put_format (&s, ngettext ("%'d case", "%'d cases", n_cases),
375 ds_put_byte (&s, ' ');
376 ds_put_unichar (&s, 0xd7); /* U+00D7 MULTIPLICATION SIGN */
377 ds_put_byte (&s, ' ');
378 ds_put_format (&s, ngettext ("%'d variable", "%'d variables",
381 ref_cell_text = ds_steal_cstr (&s);
384 gtk_label_set_label (GTK_LABEL (de->cell_ref_label),
385 ref_cell_text ? ref_cell_text : "");
387 g_free (ref_cell_text);
391 static void set_font_recursively (GtkWidget *w, gpointer data);
396 psppire_data_editor_data_delete_variables (PsppireDataEditor *de)
398 SswRange *range = SSW_SHEET(de->data_sheet)->selection;
400 psppire_dict_delete_variables (de->dict, range->start_x,
401 (range->end_x - range->start_x + 1));
403 gtk_widget_queue_draw (GTK_WIDGET (de->data_sheet));
407 psppire_data_editor_var_delete_variables (PsppireDataEditor *de)
409 SswRange *range = SSW_SHEET(de->var_sheet)->selection;
411 psppire_dict_delete_variables (de->dict, range->start_y,
412 (range->end_y - range->start_y + 1));
414 gtk_widget_queue_draw (GTK_WIDGET (de->var_sheet));
418 psppire_data_editor_insert_new_case_at_posn (PsppireDataEditor *de, gint posn)
420 psppire_data_store_insert_new_case (de->data_store, posn);
422 gtk_widget_queue_draw (GTK_WIDGET (de->data_sheet));
426 psppire_data_editor_insert_new_variable_at_posn (PsppireDataEditor *de, gint posn)
428 const struct variable *v = psppire_dict_insert_variable (de->dict, posn, NULL);
429 psppire_data_store_insert_value (de->data_store, var_get_width(v),
430 var_get_case_index (v));
432 gtk_widget_queue_draw (GTK_WIDGET (de));
436 psppire_data_editor_init (PsppireDataEditor *de)
439 gchar *fontname = NULL;
441 GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (de));
442 gtk_style_context_add_class (context, "psppire-data-editor");
446 g_object_set (de, "tab-pos", GTK_POS_BOTTOM, NULL);
448 de->cell_ref_label = gtk_label_new ("");
449 gtk_label_set_width_chars (GTK_LABEL (de->cell_ref_label), 25);
450 gtk_widget_set_valign (de->cell_ref_label, GTK_ALIGN_CENTER);
452 de->datum_entry = psppire_value_entry_new ();
453 g_signal_connect (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (de->datum_entry))),
454 "activate", G_CALLBACK (on_datum_entry_activate), de);
456 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
457 gtk_box_pack_start (GTK_BOX (hbox), de->cell_ref_label, FALSE, FALSE, 0);
458 gtk_box_pack_start (GTK_BOX (hbox), de->datum_entry, TRUE, TRUE, 0);
461 de->data_sheet = psppire_data_sheet_new ();
463 GtkWidget *data_button = ssw_sheet_get_button (SSW_SHEET (de->data_sheet));
464 gtk_button_set_label (GTK_BUTTON (data_button), _("Case"));
465 de->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
466 gtk_box_pack_start (GTK_BOX (de->vbox), hbox, FALSE, FALSE, 0);
467 gtk_box_pack_start (GTK_BOX (de->vbox), de->data_sheet, TRUE, TRUE, 0);
470 g_signal_connect_swapped (de->data_sheet, "selection-changed",
471 G_CALLBACK (on_data_selection_change), de);
473 gtk_notebook_append_page (GTK_NOTEBOOK (de), de->vbox,
474 gtk_label_new_with_mnemonic (_("Data View")));
476 gtk_widget_show_all (de->vbox);
478 de->var_sheet = psppire_variable_sheet_new ();
480 GtkWidget *var_button = ssw_sheet_get_button (SSW_SHEET (de->var_sheet));
481 gtk_button_set_label (GTK_BUTTON (var_button), _("Variable"));
483 gtk_notebook_append_page (GTK_NOTEBOOK (de), de->var_sheet,
484 gtk_label_new_with_mnemonic (_("Variable View")));
486 gtk_widget_show_all (de->var_sheet);
488 g_signal_connect (de->var_sheet, "row-header-double-clicked",
489 G_CALLBACK (on_var_sheet_var_double_clicked), de);
491 g_signal_connect (de->data_sheet, "column-header-double-clicked",
492 G_CALLBACK (on_data_sheet_var_double_clicked), de);
494 g_object_set (de, "can-focus", FALSE, NULL);
496 if (psppire_conf_get_string (psppire_conf_new (),
497 "Data Editor", "font",
500 de->font = pango_font_description_from_string (fontname);
502 set_font_recursively (GTK_WIDGET (de), de->font);
508 psppire_data_editor_new (PsppireDict *dict,
509 PsppireDataStore *data_store)
511 return g_object_new (PSPPIRE_DATA_EDITOR_TYPE,
513 "data-store", data_store,
517 /* Turns the visible grid on or off, according to GRID_VISIBLE, for DE's data
518 sheet(s) and variable sheet. */
520 psppire_data_editor_show_grid (PsppireDataEditor *de, gboolean grid_visible)
522 g_object_set (SSW_SHEET (de->var_sheet), "gridlines", grid_visible, NULL);
523 g_object_set (SSW_SHEET (de->data_sheet), "gridlines", grid_visible, NULL);
528 set_font_recursively (GtkWidget *w, gpointer data)
530 PangoFontDescription *font_desc = data;
532 GtkStyleContext *style = gtk_widget_get_style_context (w);
533 GtkCssProvider *cssp = gtk_css_provider_new ();
535 gchar *str = pango_font_description_to_string (font_desc);
537 g_strdup_printf ("* {font: %s}", str);
541 gtk_css_provider_load_from_data (cssp, css, -1, &err);
544 g_warning ("Failed to load font css \"%s\": %s", css, err->message);
549 gtk_style_context_add_provider (style,
550 GTK_STYLE_PROVIDER (cssp),
551 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
552 g_object_unref (cssp);
555 if ( GTK_IS_CONTAINER (w))
556 gtk_container_foreach (GTK_CONTAINER (w), set_font_recursively, font_desc);
559 /* Sets FONT_DESC as the font used by the data sheet(s) and variable sheet. */
561 psppire_data_editor_set_font (PsppireDataEditor *de, PangoFontDescription *font_desc)
564 set_font_recursively (GTK_WIDGET (de), font_desc);
567 pango_font_description_free (de->font);
568 de->font = pango_font_description_copy (font_desc);
569 font_name = pango_font_description_to_string (de->font);
571 psppire_conf_set_string (psppire_conf_new (),
572 "Data Editor", "font",
577 /* If SPLIT is TRUE, splits DE's data sheet into four panes.
578 If SPLIT is FALSE, un-splits it into a single pane. */
580 psppire_data_editor_split_window (PsppireDataEditor *de, gboolean split)
582 g_object_set (de, "split", split, NULL);
585 /* Makes the variable with dictionary index DICT_INDEX in DE's dictionary
586 visible and selected in the active view in DE. */
588 psppire_data_editor_goto_variable (PsppireDataEditor *de, gint dict_index)
590 gint page = gtk_notebook_get_current_page (GTK_NOTEBOOK (de));
594 case PSPPIRE_DATA_EDITOR_DATA_VIEW:
595 ssw_sheet_scroll_to (SSW_SHEET (de->data_sheet), dict_index, -1);
596 ssw_sheet_set_active_cell (SSW_SHEET (de->data_sheet), dict_index, -1, NULL);
598 case PSPPIRE_DATA_EDITOR_VARIABLE_VIEW:
599 ssw_sheet_scroll_to (SSW_SHEET (de->var_sheet), -1, dict_index);
600 ssw_sheet_set_active_cell (SSW_SHEET (de->var_sheet), -1, dict_index, NULL);
605 /* Set the datum at COL, ROW, to that contained in VALUE.
608 store_set_datum (GtkTreeModel *model, gint col, gint row,
611 PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
612 GVariant *v = g_value_get_variant (value);
614 value_variant_get (&uv, v);
615 const struct variable *var = psppire_dict_get_variable (store->dict, col);
616 psppire_data_store_set_value (store, row, var, &uv);
617 value_destroy_from_variant (&uv, v);
621 psppire_data_editor_paste (PsppireDataEditor *de)
623 SswSheet *sheet = SSW_SHEET (de->data_sheet);
625 gtk_clipboard_get_for_display (gtk_widget_get_display (GTK_WIDGET (sheet)),
626 GDK_SELECTION_CLIPBOARD);
628 ssw_sheet_paste (sheet, clip, store_set_datum);