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"
24 #include "data/datasheet.h"
25 #include "data/value-labels.h"
26 #include "libpspp/range-set.h"
27 #include "libpspp/str.h"
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"
43 #include <ssw-sheet.h>
46 #define _(msgid) gettext (msgid)
48 static void psppire_data_editor_class_init (PsppireDataEditorClass *klass);
49 static void psppire_data_editor_init (PsppireDataEditor *de);
51 static void refresh_entry (PsppireDataEditor *);
54 psppire_data_editor_get_type (void)
56 static GType de_type = 0;
60 static const GTypeInfo de_info =
62 sizeof (PsppireDataEditorClass),
64 NULL, /* base_finalize */
65 (GClassInitFunc) psppire_data_editor_class_init,
66 NULL, /* class_finalize */
67 NULL, /* class_data */
68 sizeof (PsppireDataEditor),
70 (GInstanceInitFunc) psppire_data_editor_init,
73 de_type = g_type_register_static (GTK_TYPE_NOTEBOOK, "PsppireDataEditor",
80 static GObjectClass * parent_class = NULL;
83 psppire_data_editor_dispose (GObject *obj)
85 PsppireDataEditor *de = (PsppireDataEditor *) obj;
89 g_object_unref (de->data_store);
90 de->data_store = NULL;
95 g_object_unref (de->dict);
101 pango_font_description_free (de->font);
105 /* Chain up to the parent class */
106 G_OBJECT_CLASS (parent_class)->dispose (obj);
119 psppire_data_editor_refresh_model (PsppireDataEditor *de)
125 psppire_data_editor_set_property (GObject *object,
130 PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (object);
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);
139 case PROP_DATA_STORE:
142 g_signal_handlers_disconnect_by_func (de->data_store,
143 G_CALLBACK (refresh_entry),
145 g_object_unref (de->data_store);
148 de->data_store = g_value_get_pointer (value);
149 g_object_ref (de->data_store);
151 g_object_set (de->data_sheet, "data-model", de->data_store, NULL);
152 psppire_data_editor_refresh_model (de);
154 g_signal_connect_swapped (de->data_sheet, "selection-changed",
155 G_CALLBACK (refresh_entry),
158 g_signal_connect_swapped (de->data_store, "case-changed",
159 G_CALLBACK (refresh_entry), de);
162 case PROP_DICTIONARY:
164 g_object_unref (de->dict);
165 de->dict = g_value_get_pointer (value);
166 g_object_ref (de->dict);
168 g_object_set (de->var_sheet, "data-model", de->dict, NULL);
171 case PROP_VALUE_LABELS:
173 gboolean l = g_value_get_boolean (value);
174 g_object_set (de->data_sheet, "forward-conversion",
176 psppire_data_store_value_to_string_with_labels :
177 psppire_data_store_value_to_string,
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 psppire_data_editor_get_property (GObject *object,
194 PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (object);
198 case PROP_SPLIT_WINDOW:
199 g_value_set_boolean (value, de->split);
201 case PROP_DATA_STORE:
202 g_value_set_pointer (value, de->data_store);
204 case PROP_DICTIONARY:
205 g_value_set_pointer (value, de->dict);
207 case PROP_VALUE_LABELS:
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 psppire_data_editor_switch_page (GtkNotebook *notebook,
220 GTK_NOTEBOOK_CLASS (parent_class)->switch_page (notebook, w, page_num);
224 psppire_data_editor_set_focus_child (GtkContainer *container,
227 GTK_CONTAINER_CLASS (parent_class)->set_focus_child (container, widget);
232 on_key_press (GtkWidget *w, GdkEventKey *e)
234 PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (w);
235 if (e->keyval == GDK_KEY_F2 &&
236 PSPPIRE_DATA_EDITOR_DATA_VIEW == gtk_notebook_get_current_page (GTK_NOTEBOOK (de)))
238 gtk_widget_grab_focus (de->datum_entry);
245 psppire_data_editor_class_init (PsppireDataEditorClass *klass)
247 GParamSpec *data_store_spec ;
248 GParamSpec *dict_spec ;
249 GParamSpec *value_labels_spec;
250 GParamSpec *split_window_spec;
252 GObjectClass *object_class = G_OBJECT_CLASS (klass);
253 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
254 GtkNotebookClass *notebook_class = GTK_NOTEBOOK_CLASS (klass);
255 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
257 parent_class = g_type_class_peek_parent (klass);
259 object_class->dispose = psppire_data_editor_dispose;
260 object_class->set_property = psppire_data_editor_set_property;
261 object_class->get_property = psppire_data_editor_get_property;
263 container_class->set_focus_child = psppire_data_editor_set_focus_child;
264 notebook_class->switch_page = psppire_data_editor_switch_page;
265 widget_class->key_press_event = on_key_press;
268 g_param_spec_pointer ("data-store",
270 "A pointer to the data store associated with this editor",
271 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE );
273 g_object_class_install_property (object_class,
278 g_param_spec_pointer ("dictionary",
280 "A pointer to the dictionary associated with this editor",
281 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE );
283 g_object_class_install_property (object_class,
288 g_param_spec_boolean ("value-labels",
290 "Whether or not the data sheet should display labels instead of values",
292 G_PARAM_WRITABLE | G_PARAM_READABLE);
294 g_object_class_install_property (object_class,
300 g_param_spec_boolean ("split",
302 "True iff the data sheet is split",
304 G_PARAM_READABLE | G_PARAM_WRITABLE);
306 g_object_class_install_property (object_class,
313 on_var_sheet_var_double_clicked (void *var_sheet, gint dict_index,
314 PsppireDataEditor *de)
316 gtk_notebook_set_current_page (GTK_NOTEBOOK (de),
317 PSPPIRE_DATA_EDITOR_DATA_VIEW);
319 ssw_sheet_scroll_to (SSW_SHEET (de->data_sheet), dict_index, -1);
324 on_data_sheet_var_double_clicked (SswSheet *data_sheet, gint dict_index,
325 PsppireDataEditor *de)
328 gtk_notebook_set_current_page (GTK_NOTEBOOK (de),
329 PSPPIRE_DATA_EDITOR_VARIABLE_VIEW);
331 ssw_sheet_scroll_to (SSW_SHEET (de->var_sheet), -1, dict_index);
336 /* Refreshes 'de->cell_ref_label' and 'de->datum_entry' from the currently
337 active cell or cells. */
339 refresh_entry (PsppireDataEditor *de)
342 if (ssw_sheet_get_active_cell (SSW_SHEET (de->data_sheet), &col, &row))
345 const struct variable *var = psppire_dict_get_variable (de->dict, col);
349 psppire_value_entry_set_variable (PSPPIRE_VALUE_ENTRY (de->datum_entry), var);
351 int width = var_get_width (var);
352 if (! psppire_data_store_get_value (PSPPIRE_DATA_STORE (de->data_store),
356 psppire_value_entry_set_value (PSPPIRE_VALUE_ENTRY (de->datum_entry),
358 value_destroy (&val, width);
363 on_datum_entry_activate (GtkEntry *entry, PsppireDataEditor *de)
366 if (ssw_sheet_get_active_cell (SSW_SHEET (de->data_sheet), &col, &row))
369 const struct variable *var = psppire_dict_get_variable (de->dict, col);
373 int width = var_get_width (var);
374 value_init (&val, width);
375 if (psppire_value_entry_get_value (PSPPIRE_VALUE_ENTRY (de->datum_entry),
378 psppire_data_store_set_value (de->data_store, row, var, &val);
380 value_destroy (&val, width);
382 gtk_widget_grab_focus (de->data_sheet);
383 ssw_sheet_set_active_cell (de->data_sheet, col, row, NULL);
388 /* Called when the active cell or the selection in the data sheet changes */
390 on_data_selection_change (PsppireDataEditor *de, SswRange *sel)
392 gchar *ref_cell_text = NULL;
394 gint n_cases = abs (sel->end_y - sel->start_y) + 1;
395 gint n_vars = abs (sel->end_x - sel->start_x) + 1;
397 if (n_cases == 1 && n_vars == 1)
399 /* A single cell is selected */
400 const struct variable *var = psppire_dict_get_variable (de->dict, sel->start_x);
403 ref_cell_text = g_strdup_printf (_("%d : %s"),
404 sel->start_y + 1, var_get_name (var));
410 /* The glib string library does not understand the ' printf modifier
411 on all platforms, but the "struct string" library does (because
412 Gnulib fixes that problem), so use the latter. */
414 ds_put_format (&s, ngettext ("%'d case", "%'d cases", n_cases),
416 ds_put_byte (&s, ' ');
417 ds_put_unichar (&s, 0xd7); /* U+00D7 MULTIPLICATION SIGN */
418 ds_put_byte (&s, ' ');
419 ds_put_format (&s, ngettext ("%'d variable", "%'d variables",
422 ref_cell_text = ds_steal_cstr (&s);
425 gtk_label_set_label (GTK_LABEL (de->cell_ref_label),
426 ref_cell_text ? ref_cell_text : "");
428 g_free (ref_cell_text);
432 static void set_font_recursively (GtkWidget *w, gpointer data);
437 psppire_data_editor_data_delete_variables (PsppireDataEditor *de)
439 SswRange *range = SSW_SHEET(de->data_sheet)->selection;
441 psppire_dict_delete_variables (de->dict, range->start_x,
442 (range->end_x - range->start_x + 1));
444 gtk_widget_queue_draw (GTK_WIDGET (de->data_sheet));
448 psppire_data_editor_var_delete_variables (PsppireDataEditor *de)
450 SswRange *range = SSW_SHEET(de->var_sheet)->selection;
452 psppire_dict_delete_variables (de->dict, range->start_y,
453 (range->end_y - range->start_y + 1));
455 gtk_widget_queue_draw (GTK_WIDGET (de->var_sheet));
459 psppire_data_editor_insert_new_case_at_posn (PsppireDataEditor *de, gint posn)
461 psppire_data_store_insert_new_case (de->data_store, posn);
463 gtk_widget_queue_draw (GTK_WIDGET (de->data_sheet));
467 psppire_data_editor_insert_new_variable_at_posn (PsppireDataEditor *de, gint posn)
469 const struct variable *v = psppire_dict_insert_variable (de->dict, posn, NULL);
470 psppire_data_store_insert_value (de->data_store, var_get_width(v),
471 var_get_case_index (v));
473 gtk_widget_queue_draw (GTK_WIDGET (de));
477 psppire_data_editor_init (PsppireDataEditor *de)
480 gchar *fontname = NULL;
482 GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (de));
483 gtk_style_context_add_class (context, "psppire-data-editor");
487 g_object_set (de, "tab-pos", GTK_POS_BOTTOM, NULL);
489 de->cell_ref_label = gtk_label_new ("");
490 gtk_label_set_width_chars (GTK_LABEL (de->cell_ref_label), 25);
491 gtk_widget_set_valign (de->cell_ref_label, GTK_ALIGN_CENTER);
493 de->datum_entry = psppire_value_entry_new ();
494 g_signal_connect (de->datum_entry, "edit-done",
495 G_CALLBACK (on_datum_entry_activate), de);
497 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
498 gtk_box_pack_start (GTK_BOX (hbox), de->cell_ref_label, FALSE, FALSE, 0);
499 gtk_box_pack_start (GTK_BOX (hbox), de->datum_entry, TRUE, TRUE, 0);
502 de->data_sheet = psppire_data_sheet_new ();
504 GtkWidget *data_button = ssw_sheet_get_button (SSW_SHEET (de->data_sheet));
505 gtk_button_set_label (GTK_BUTTON (data_button), _("Case"));
506 de->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
507 gtk_box_pack_start (GTK_BOX (de->vbox), hbox, FALSE, FALSE, 0);
508 gtk_box_pack_start (GTK_BOX (de->vbox), de->data_sheet, TRUE, TRUE, 0);
511 g_signal_connect_swapped (de->data_sheet, "selection-changed",
512 G_CALLBACK (on_data_selection_change), de);
514 gtk_notebook_append_page (GTK_NOTEBOOK (de), de->vbox,
515 gtk_label_new_with_mnemonic (_("Data View")));
517 gtk_widget_show_all (de->vbox);
519 de->var_sheet = psppire_variable_sheet_new ();
521 GtkWidget *var_button = ssw_sheet_get_button (SSW_SHEET (de->var_sheet));
522 gtk_button_set_label (GTK_BUTTON (var_button), _("Variable"));
524 gtk_notebook_append_page (GTK_NOTEBOOK (de), de->var_sheet,
525 gtk_label_new_with_mnemonic (_("Variable View")));
527 gtk_widget_show_all (de->var_sheet);
529 g_signal_connect (de->var_sheet, "row-header-double-clicked",
530 G_CALLBACK (on_var_sheet_var_double_clicked), de);
532 g_signal_connect (de->data_sheet, "column-header-double-clicked",
533 G_CALLBACK (on_data_sheet_var_double_clicked), de);
535 g_object_set (de, "can-focus", FALSE, NULL);
537 if (psppire_conf_get_string (psppire_conf_new (),
538 "Data Editor", "font",
541 de->font = pango_font_description_from_string (fontname);
543 set_font_recursively (GTK_WIDGET (de), de->font);
546 gtk_widget_add_events (GTK_WIDGET (de), GDK_KEY_PRESS_MASK);
550 psppire_data_editor_new (PsppireDict *dict,
551 PsppireDataStore *data_store)
553 return g_object_new (PSPPIRE_DATA_EDITOR_TYPE,
555 "data-store", data_store,
559 /* Turns the visible grid on or off, according to GRID_VISIBLE, for DE's data
560 sheet(s) and variable sheet. */
562 psppire_data_editor_show_grid (PsppireDataEditor *de, gboolean grid_visible)
564 g_object_set (SSW_SHEET (de->var_sheet), "gridlines", grid_visible, NULL);
565 g_object_set (SSW_SHEET (de->data_sheet), "gridlines", grid_visible, NULL);
570 set_font_recursively (GtkWidget *w, gpointer data)
572 PangoFontDescription *font_desc = data;
574 GtkStyleContext *style = gtk_widget_get_style_context (w);
575 GtkCssProvider *cssp = gtk_css_provider_new ();
577 gchar *str = pango_font_description_to_string (font_desc);
579 g_strdup_printf ("* {font: %s}", str);
583 gtk_css_provider_load_from_data (cssp, css, -1, &err);
586 g_warning ("Failed to load font css \"%s\": %s", css, err->message);
591 gtk_style_context_add_provider (style,
592 GTK_STYLE_PROVIDER (cssp),
593 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
594 g_object_unref (cssp);
597 if ( GTK_IS_CONTAINER (w))
598 gtk_container_foreach (GTK_CONTAINER (w), set_font_recursively, font_desc);
601 /* Sets FONT_DESC as the font used by the data sheet(s) and variable sheet. */
603 psppire_data_editor_set_font (PsppireDataEditor *de, PangoFontDescription *font_desc)
606 set_font_recursively (GTK_WIDGET (de), font_desc);
609 pango_font_description_free (de->font);
610 de->font = pango_font_description_copy (font_desc);
611 font_name = pango_font_description_to_string (de->font);
613 psppire_conf_set_string (psppire_conf_new (),
614 "Data Editor", "font",
619 /* If SPLIT is TRUE, splits DE's data sheet into four panes.
620 If SPLIT is FALSE, un-splits it into a single pane. */
622 psppire_data_editor_split_window (PsppireDataEditor *de, gboolean split)
624 g_object_set (de, "split", split, NULL);
627 /* Makes the variable with dictionary index DICT_INDEX in DE's dictionary
628 visible and selected in the active view in DE. */
630 psppire_data_editor_goto_variable (PsppireDataEditor *de, gint dict_index)
632 gint page = gtk_notebook_get_current_page (GTK_NOTEBOOK (de));
636 case PSPPIRE_DATA_EDITOR_DATA_VIEW:
637 ssw_sheet_scroll_to (SSW_SHEET (de->data_sheet), dict_index, -1);
638 ssw_sheet_set_active_cell (SSW_SHEET (de->data_sheet), dict_index, -1, NULL);
640 case PSPPIRE_DATA_EDITOR_VARIABLE_VIEW:
641 ssw_sheet_scroll_to (SSW_SHEET (de->var_sheet), -1, dict_index);
642 ssw_sheet_set_active_cell (SSW_SHEET (de->var_sheet), -1, dict_index, NULL);
647 /* Set the datum at COL, ROW, to that contained in VALUE.
650 store_set_datum (GtkTreeModel *model, gint col, gint row,
653 PsppireDataStore *store = PSPPIRE_DATA_STORE (model);
654 GVariant *v = g_value_get_variant (value);
656 value_variant_get (&uv, v);
657 const struct variable *var = psppire_dict_get_variable (store->dict, col);
658 psppire_data_store_set_value (store, row, var, &uv);
659 value_destroy_from_variant (&uv, v);
663 psppire_data_editor_paste (PsppireDataEditor *de)
665 SswSheet *sheet = SSW_SHEET (de->data_sheet);
667 gtk_clipboard_get_for_display (gtk_widget_get_display (GTK_WIDGET (sheet)),
668 GDK_SELECTION_CLIPBOARD);
670 ssw_sheet_paste (sheet, clip, store_set_datum);