1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyrigght (C) 2008 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include <gtk/gtksignal.h>
20 #include <gtksheet/gtksheet.h>
21 #include "psppire-data-editor.h"
22 #include "psppire-var-sheet.h"
24 #include <gtksheet/gsheet-hetero-column.h>
25 #include <language/syntax-string-source.h>
26 #include "psppire-data-store.h"
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
35 DATA_SELECTION_CHANGED,
36 DATA_AVAILABLE_CHANGED,
43 static guint data_editor_signals [n_SIGNALS] = { 0 };
46 static gboolean data_is_selected (PsppireDataEditor *de);
48 static void psppire_data_editor_class_init (PsppireDataEditorClass *klass);
49 static void psppire_data_editor_init (PsppireDataEditor *de);
52 psppire_data_editor_get_type (void)
54 static GType de_type = 0;
58 static const GTypeInfo de_info =
60 sizeof (PsppireDataEditorClass),
62 NULL, /* base_finalize */
63 (GClassInitFunc) psppire_data_editor_class_init,
64 NULL, /* class_finalize */
65 NULL, /* class_data */
66 sizeof (PsppireDataEditor),
68 (GInstanceInitFunc) psppire_data_editor_init,
71 de_type = g_type_register_static (GTK_TYPE_NOTEBOOK, "PsppireDataEditor",
78 static GObjectClass * parent_class = NULL;
81 psppire_data_editor_dispose (GObject *obj)
83 PsppireDataEditor *de = (PsppireDataEditor *) obj;
85 if (de->dispose_has_run)
88 g_object_unref (de->data_store);
89 g_object_unref (de->var_store);
91 /* Make sure dispose does not run twice. */
92 de->dispose_has_run = TRUE;
94 /* Chain up to the parent class */
95 G_OBJECT_CLASS (parent_class)->dispose (obj);
99 psppire_data_editor_finalize (GObject *obj)
101 /* Chain up to the parent class */
102 G_OBJECT_CLASS (parent_class)->finalize (obj);
107 static void popup_variable_menu (GtkSheet *sheet, gint column,
108 GdkEventButton *event, gpointer data);
110 static void popup_cases_menu (GtkSheet *sheet, gint row,
111 GdkEventButton *event, gpointer data);
116 /* Callback which occurs when the data sheet's column title
119 on_data_column_clicked (PsppireDataEditor *de, gint col, gpointer data)
122 gint current_row, current_column;
124 gtk_notebook_set_current_page (GTK_NOTEBOOK (de), PSPPIRE_DATA_EDITOR_VARIABLE_VIEW);
126 gtk_sheet_get_active_cell (GTK_SHEET (de->var_sheet),
127 ¤t_row, ¤t_column);
129 gtk_sheet_set_active_cell (GTK_SHEET (de->var_sheet), col, current_column);
135 /* Callback which occurs when the var sheet's row title
136 button is double clicked */
138 on_var_row_clicked (PsppireDataEditor *de, gint row, gpointer data)
140 GtkSheetRange visible_range;
142 gint current_row, current_column;
144 gtk_notebook_set_current_page (GTK_NOTEBOOK(de), PSPPIRE_DATA_EDITOR_DATA_VIEW);
146 gtk_sheet_get_active_cell (GTK_SHEET (de->data_sheet),
147 ¤t_row, ¤t_column);
149 gtk_sheet_set_active_cell (GTK_SHEET (de->data_sheet), current_row, row);
151 gtk_sheet_get_visible_range (GTK_SHEET (de->data_sheet), &visible_range);
153 if ( row < visible_range.col0 || row > visible_range.coli)
155 gtk_sheet_moveto (GTK_SHEET (de->data_sheet),
156 current_row, row, 0, 0);
178 psppire_data_editor_set_property (GObject *object,
183 PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (object);
187 case PROP_DATA_STORE:
188 if ( de->data_store) g_object_unref (de->data_store);
189 de->data_store = g_value_get_pointer (value);
190 g_object_ref (de->data_store);
192 g_object_set (de->data_sheet,
193 "row-geometry", de->data_store,
194 "column-geometry", de->data_store,
195 "model", de->data_store,
199 if ( de->var_store) g_object_unref (de->var_store);
200 de->var_store = g_value_get_pointer (value);
201 g_object_ref (de->var_store);
203 g_object_set (de->var_sheet,
204 "row-geometry", de->var_store,
205 "model", de->var_store,
208 case PROP_COLUMN_MENU:
210 GObject *menu = g_value_get_object (value);
212 g_signal_connect (de->data_sheet, "button-event-column",
213 G_CALLBACK (popup_variable_menu), menu);
218 GObject *menu = g_value_get_object (value);
220 g_signal_connect (de->data_sheet, "button-event-row",
221 G_CALLBACK (popup_cases_menu), menu);
224 case PROP_CURRENT_VAR:
227 gint var = g_value_get_long (value);
228 switch (gtk_notebook_get_current_page (GTK_NOTEBOOK (object)))
230 case PSPPIRE_DATA_EDITOR_DATA_VIEW:
231 gtk_sheet_get_active_cell (GTK_SHEET (de->data_sheet), &row, &col);
232 gtk_sheet_set_active_cell (GTK_SHEET (de->data_sheet), row, var);
233 gtk_sheet_moveto (GTK_SHEET (de->data_sheet), row, var, 0.5, 0.5);
235 case PSPPIRE_DATA_EDITOR_VARIABLE_VIEW:
236 gtk_sheet_get_active_cell (GTK_SHEET (de->var_sheet), &row, &col);
237 gtk_sheet_set_active_cell (GTK_SHEET (de->var_sheet), var, col);
238 gtk_sheet_moveto (GTK_SHEET (de->var_sheet), var, col, 0.5, 0.5);
241 g_assert_not_reached ();
246 case PROP_CURRENT_CASE:
249 gint case_num = g_value_get_long (value);
250 gtk_sheet_get_active_cell (GTK_SHEET (de->data_sheet), &row, &col);
251 gtk_sheet_set_active_cell (GTK_SHEET (de->data_sheet), case_num, col);
252 gtk_sheet_moveto (GTK_SHEET (de->data_sheet), case_num, col, 0.5, 0.5);
255 case PROP_VALUE_LABELS:
257 psppire_data_store_show_labels (de->data_store,
258 g_value_get_boolean (value));
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268 psppire_data_editor_get_property (GObject *object,
273 PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (object);
277 case PROP_DATA_STORE:
278 g_value_set_pointer (value, de->data_store);
281 g_value_set_pointer (value, de->var_store);
283 case PROP_CURRENT_CASE:
286 gtk_sheet_get_active_cell (GTK_SHEET (de->data_sheet), &row, &column);
287 g_value_set_long (value, row);
290 case PROP_CURRENT_VAR:
293 gtk_sheet_get_active_cell (GTK_SHEET (de->data_sheet), &row, &column);
294 g_value_set_long (value, column);
297 case PROP_DATA_SELECTED:
298 g_value_set_boolean (value, data_is_selected (de));
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308 psppire_data_editor_class_init (PsppireDataEditorClass *klass)
310 GParamSpec *data_store_spec ;
311 GParamSpec *var_store_spec ;
312 GParamSpec *column_menu_spec;
313 GParamSpec *row_menu_spec;
314 GParamSpec *value_labels_spec;
315 GParamSpec *current_case_spec;
316 GParamSpec *current_var_spec;
317 GParamSpec *data_selected_spec;
318 GObjectClass *object_class = G_OBJECT_CLASS (klass);
320 parent_class = g_type_class_peek_parent (klass);
322 object_class->dispose = psppire_data_editor_dispose;
323 object_class->finalize = psppire_data_editor_finalize;
325 object_class->set_property = psppire_data_editor_set_property;
326 object_class->get_property = psppire_data_editor_get_property;
329 g_param_spec_pointer ("data-store",
331 "A pointer to the data store associated with this editor",
332 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE );
334 g_object_class_install_property (object_class,
339 g_param_spec_pointer ("var-store",
341 "A pointer to the variable store associated with this editor",
342 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE );
344 g_object_class_install_property (object_class,
349 g_param_spec_object ("column-menu",
351 "A menu to be displayed when button 3 is pressed in the column title buttons",
355 g_object_class_install_property (object_class,
361 g_param_spec_object ("row-menu",
363 "A menu to be displayed when button 3 is pressed in the row title buttons",
367 g_object_class_install_property (object_class,
372 g_param_spec_boolean ("value-labels",
374 "Whether or not the data sheet should display labels instead of values",
376 G_PARAM_WRITABLE | G_PARAM_READABLE);
378 g_object_class_install_property (object_class,
384 g_param_spec_long ("current-case",
386 "Zero based number of the selected case",
389 G_PARAM_WRITABLE | G_PARAM_READABLE);
391 g_object_class_install_property (object_class,
397 g_param_spec_long ("current-variable",
399 "Zero based number of the selected variable",
402 G_PARAM_WRITABLE | G_PARAM_READABLE);
404 g_object_class_install_property (object_class,
410 g_param_spec_boolean ("data-selected",
412 "True iff the data view is active and one or more cells of data have been selected.",
416 g_object_class_install_property (object_class,
422 data_editor_signals [DATA_SELECTION_CHANGED] =
423 g_signal_new ("data-selection-changed",
424 G_TYPE_FROM_CLASS (klass),
428 g_cclosure_marshal_VOID__BOOLEAN,
433 data_editor_signals [CASES_SELECTED] =
434 g_signal_new ("cases-selected",
435 G_TYPE_FROM_CLASS (klass),
439 g_cclosure_marshal_VOID__INT,
445 data_editor_signals [VARIABLES_SELECTED] =
446 g_signal_new ("variables-selected",
447 G_TYPE_FROM_CLASS (klass),
451 g_cclosure_marshal_VOID__INT,
457 data_editor_signals [DATA_AVAILABLE_CHANGED] =
458 g_signal_new ("data-available-changed",
459 G_TYPE_FROM_CLASS (klass),
463 g_cclosure_marshal_VOID__BOOLEAN,
469 /* Update the data_ref_entry with the reference of the active cell */
471 update_data_ref_entry (const GtkSheet *sheet, gint row, gint col, gpointer data)
473 PsppireDataEditor *de = data;
475 PsppireDataStore *data_store =
476 PSPPIRE_DATA_STORE (gtk_sheet_get_model (sheet));
480 const struct variable *var =
481 psppire_dict_get_variable (data_store->dict, col);
485 gchar *text = g_strdup_printf ("%d: %s", row + FIRST_CASE_NUMBER,
488 gchar *s = pspp_locale_to_utf8 (text, -1, 0);
492 gtk_entry_set_text (GTK_ENTRY (de->cell_ref_entry), s);
502 psppire_data_store_get_string (data_store, row,
503 var_get_dict_index(var));
510 gtk_entry_set_text (GTK_ENTRY (de->datum_entry), text);
522 gtk_entry_set_text (GTK_ENTRY (de->datum_entry), "");
529 datum_entry_activate (GtkEntry *entry, gpointer data)
532 PsppireDataEditor *de = data;
534 const gchar *text = gtk_entry_get_text (entry);
536 gtk_sheet_get_active_cell (GTK_SHEET (de->data_sheet), &row, &column);
538 if ( row == -1 || column == -1)
541 psppire_data_store_set_string (de->data_store, text, row, column);
545 static void on_activate (PsppireDataEditor *de);
546 static void on_deactivate (PsppireDataEditor *de);
547 static gboolean on_switch_page (PsppireDataEditor *de, GtkNotebookPage *p, gint pagenum, gpointer data);
548 static void on_select_range (PsppireDataEditor *de);
550 static void on_select_row (GtkSheet *, gint, PsppireDataEditor *);
551 static void on_select_variable (GtkSheet *, gint, PsppireDataEditor *);
554 static void on_owner_change (GtkClipboard *,
555 GdkEventOwnerChange *, gpointer);
558 on_map (GtkWidget *w)
560 GtkClipboard *clip = gtk_widget_get_clipboard (w, GDK_SELECTION_CLIPBOARD);
562 g_signal_connect (clip, "owner-change", G_CALLBACK (on_owner_change), w);
566 traverse_cell_callback (GtkSheet *sheet,
567 gint row, gint column,
568 gint *new_row, gint *new_column,
571 PsppireDataStore *data_store = PSPPIRE_DATA_STORE (data);
573 if ( *new_column >= psppire_dict_get_var_cnt (data_store->dict))
580 psppire_data_editor_init (PsppireDataEditor *de)
582 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
583 GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
584 GtkWidget *sw_ds = gtk_scrolled_window_new (NULL, NULL);
585 GtkWidget *sw_vs = gtk_scrolled_window_new (NULL, NULL);
588 de->data_sheet = gtk_sheet_new (NULL, NULL, NULL);
589 de->var_sheet = psppire_var_sheet_new ();
591 g_object_set (de, "tab-pos", GTK_POS_BOTTOM, NULL);
593 de->datum_entry = gtk_entry_new ();
594 de->cell_ref_entry = gtk_entry_new ();
596 g_object_set (de->cell_ref_entry,
602 gtk_box_pack_start (GTK_BOX (hbox), de->cell_ref_entry, FALSE, FALSE, 0);
603 gtk_box_pack_start (GTK_BOX (hbox), de->datum_entry, TRUE, TRUE, 0);
606 gtk_container_add (GTK_CONTAINER (sw_vs), de->var_sheet);
607 gtk_widget_show_all (sw_vs);
610 gtk_container_add (GTK_CONTAINER (sw_ds), de->data_sheet);
611 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
612 gtk_box_pack_start (GTK_BOX (vbox), sw_ds, TRUE, TRUE, 0);
614 gtk_widget_show_all (vbox);
616 gtk_notebook_append_page (GTK_NOTEBOOK (de), vbox,
617 gtk_label_new_with_mnemonic (_("Data View")));
619 gtk_notebook_append_page (GTK_NOTEBOOK (de), sw_vs,
620 gtk_label_new_with_mnemonic (_("Variable View")));
622 g_signal_connect (de->data_sheet, "activate",
623 G_CALLBACK (update_data_ref_entry),
626 g_signal_connect (de->datum_entry, "activate",
627 G_CALLBACK (datum_entry_activate),
631 g_signal_connect_swapped (de->data_sheet,
632 "double-click-column",
633 G_CALLBACK (on_data_column_clicked),
636 g_signal_connect_swapped (de->var_sheet,
638 G_CALLBACK (on_var_row_clicked),
641 g_signal_connect_swapped (de->data_sheet, "activate",
642 G_CALLBACK (on_activate),
645 g_signal_connect_swapped (de->data_sheet, "deactivate",
646 G_CALLBACK (on_deactivate),
649 g_signal_connect_swapped (de->data_sheet, "select-range",
650 G_CALLBACK (on_select_range),
653 g_signal_connect (de->data_sheet, "select-row",
654 G_CALLBACK (on_select_row), de);
656 g_signal_connect (de->data_sheet, "select-column",
657 G_CALLBACK (on_select_variable), de);
660 g_signal_connect (de->var_sheet, "select-row",
661 G_CALLBACK (on_select_variable), de);
664 g_signal_connect_after (de, "switch-page",
665 G_CALLBACK (on_switch_page),
669 g_signal_connect (de, "map", G_CALLBACK (on_map), NULL);
671 de->dispose_has_run = FALSE;
676 psppire_data_editor_new (PsppireVarStore *var_store,
677 PsppireDataStore *data_store)
681 widget = g_object_new (PSPPIRE_DATA_EDITOR_TYPE,
682 "var-store", var_store,
683 "data-store", data_store,
686 g_signal_connect (PSPPIRE_DATA_EDITOR(widget)->data_sheet, "traverse",
687 G_CALLBACK (traverse_cell_callback), data_store);
692 static void data_sheet_set_clip (GtkSheet *sheet);
693 static void data_sheet_contents_received_callback (GtkClipboard *clipboard,
694 GtkSelectionData *sd,
699 psppire_data_editor_clip_copy (PsppireDataEditor *de)
701 data_sheet_set_clip (GTK_SHEET (de->data_sheet));
705 psppire_data_editor_clip_paste (PsppireDataEditor *de)
707 GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
709 gtk_clipboard_request_contents (clipboard,
710 gdk_atom_intern ("UTF8_STRING", TRUE),
711 data_sheet_contents_received_callback,
718 psppire_data_editor_clip_cut (PsppireDataEditor *de)
720 gint max_rows, max_columns;
723 PsppireDataStore *ds = de->data_store;
725 data_sheet_set_clip (GTK_SHEET (de->data_sheet));
727 /* Now blank all the cells */
728 gtk_sheet_get_selected_range (GTK_SHEET (de->data_sheet), &range);
730 /* If nothing selected, then use active cell */
731 if ( range.row0 < 0 || range.col0 < 0 )
734 gtk_sheet_get_active_cell (GTK_SHEET (de->data_sheet), &row, &col);
736 range.row0 = range.rowi = row;
737 range.col0 = range.coli = col;
740 /* The sheet range can include cells that do not include data.
741 Exclude them from the range. */
742 max_rows = psppire_data_store_get_case_count (ds);
743 if (range.rowi >= max_rows)
747 range.rowi = max_rows - 1;
750 max_columns = dict_get_var_cnt (ds->dict->dict);
751 if (range.coli >= max_columns)
753 if (max_columns == 0)
755 range.coli = max_columns - 1;
758 g_return_if_fail (range.rowi >= range.row0);
759 g_return_if_fail (range.row0 >= 0);
760 g_return_if_fail (range.coli >= range.col0);
761 g_return_if_fail (range.col0 >= 0);
764 for (r = range.row0; r <= range.rowi ; ++r )
768 for (c = range.col0 ; c <= range.coli; ++c)
770 psppire_data_store_set_string (ds, "", r, c);
774 /* and remove the selection */
775 gtk_sheet_unselect_range (GTK_SHEET (de->data_sheet));
781 /* Popup menu related stuff */
784 popup_variable_menu (GtkSheet *sheet, gint column,
785 GdkEventButton *event, gpointer data)
787 GtkMenu *menu = GTK_MENU (data);
789 PsppireDataStore *data_store =
790 PSPPIRE_DATA_STORE (gtk_sheet_get_model (sheet));
792 const struct variable *v =
793 psppire_dict_get_variable (data_store->dict, column);
795 if ( v && event->button == 3)
797 gtk_sheet_select_column (sheet, column);
799 gtk_menu_popup (menu,
800 NULL, NULL, NULL, NULL,
801 event->button, event->time);
807 popup_cases_menu (GtkSheet *sheet, gint row,
808 GdkEventButton *event, gpointer data)
810 GtkMenu *menu = GTK_MENU (data);
812 PsppireDataStore *data_store =
813 PSPPIRE_DATA_STORE (gtk_sheet_get_model (sheet));
815 if ( row <= psppire_data_store_get_case_count (data_store) &&
818 gtk_sheet_select_row (sheet, row);
820 gtk_menu_popup (menu,
821 NULL, NULL, NULL, NULL,
822 event->button, event->time);
831 do_sort (PsppireDataStore *ds, int var, gboolean descend)
833 GString *string = g_string_new ("SORT CASES BY ");
835 const struct variable *v =
836 psppire_dict_get_variable (ds->dict, var);
838 g_string_append_printf (string, "%s", var_get_name (v));
841 g_string_append (string, " (D)");
843 g_string_append (string, ".");
845 execute_syntax (create_syntax_string_source (string->str));
847 g_string_free (string, TRUE);
851 /* Sort the data by the the variable which the editor has currently
854 psppire_data_editor_sort_ascending (PsppireDataEditor *de)
857 gtk_sheet_get_selected_range (GTK_SHEET(de->data_sheet), &range);
859 do_sort (de->data_store, range.col0, FALSE);
863 /* Sort the data by the the variable which the editor has currently
866 psppire_data_editor_sort_descending (PsppireDataEditor *de)
869 gtk_sheet_get_selected_range (GTK_SHEET(de->data_sheet), &range);
871 do_sort (de->data_store, range.col0, TRUE);
878 /* Insert a new variable before the currently selected position */
880 psppire_data_editor_insert_variable (PsppireDataEditor *de)
884 if ( de->data_sheet->state == GTK_SHEET_COLUMN_SELECTED )
885 posn = GTK_SHEET (de->data_sheet)->range.col0;
887 posn = GTK_SHEET (de->data_sheet)->active_cell.col;
889 if ( posn == -1 ) posn = 0;
891 psppire_dict_insert_variable (de->data_store->dict, posn, NULL);
894 /* Insert a new case before the currently selected position */
896 psppire_data_editor_insert_case (PsppireDataEditor *de)
900 if ( de->data_sheet->state == GTK_SHEET_ROW_SELECTED )
901 posn = GTK_SHEET (de->data_sheet)->range.row0;
903 posn = GTK_SHEET (de->data_sheet)->active_cell.row;
905 if ( posn == -1 ) posn = 0;
907 psppire_data_store_insert_new_case (de->data_store, posn);
910 /* Delete the cases currently selected in the data sheet */
912 psppire_data_editor_delete_cases (PsppireDataEditor *de)
914 gint first = GTK_SHEET (de->data_sheet)->range.row0;
915 gint n = GTK_SHEET (de->data_sheet)->range.rowi - first + 1;
917 psppire_data_store_delete_cases (de->data_store, first, n);
919 gtk_sheet_unselect_range (GTK_SHEET (de->data_sheet));
922 /* Delete the variables currently selected in the
923 datasheet or variable sheet */
925 psppire_data_editor_delete_variables (PsppireDataEditor *de)
929 switch (gtk_notebook_get_current_page (GTK_NOTEBOOK (de)))
931 case PSPPIRE_DATA_EDITOR_DATA_VIEW:
932 first = GTK_SHEET (de->data_sheet)->range.col0;
933 n = GTK_SHEET (de->data_sheet)->range.coli - first + 1;
935 case PSPPIRE_DATA_EDITOR_VARIABLE_VIEW:
936 first = GTK_SHEET (de->var_sheet)->range.row0;
937 n = GTK_SHEET (de->var_sheet)->range.rowi - first + 1;
940 g_assert_not_reached ();
944 psppire_dict_delete_variables (de->var_store->dict, first, n);
946 gtk_sheet_unselect_range (GTK_SHEET (de->data_sheet));
947 gtk_sheet_unselect_range (GTK_SHEET (de->var_sheet));
952 psppire_data_editor_show_grid (PsppireDataEditor *de, gboolean grid_visible)
954 gtk_sheet_show_grid (GTK_SHEET (de->var_sheet), grid_visible);
955 gtk_sheet_show_grid (GTK_SHEET (de->data_sheet), grid_visible);
959 psppire_data_editor_set_font (PsppireDataEditor *de, PangoFontDescription *font_desc)
961 psppire_data_store_set_font (de->data_store, font_desc);
962 psppire_var_store_set_font (de->var_store, font_desc);
970 emit_selected_signal (PsppireDataEditor *de)
972 gboolean data_selected = data_is_selected (de);
974 g_signal_emit (de, data_editor_signals[DATA_SELECTION_CHANGED], 0, data_selected);
978 on_activate (PsppireDataEditor *de)
981 gtk_sheet_get_active_cell (GTK_SHEET (de->data_sheet), &row, &col);
983 if ( row < psppire_data_store_get_case_count (de->data_store)
985 col < psppire_var_store_get_var_cnt (de->var_store))
987 emit_selected_signal (de);
991 emit_selected_signal (de);
996 on_deactivate (PsppireDataEditor *de)
998 emit_selected_signal (de);
1002 on_select_range (PsppireDataEditor *de)
1004 GtkSheetRange range;
1006 gtk_sheet_get_selected_range (GTK_SHEET (de->data_sheet), &range);
1008 if ( range.rowi < psppire_data_store_get_case_count (de->data_store)
1010 range.coli < psppire_var_store_get_var_cnt (de->var_store))
1012 emit_selected_signal (de);
1016 emit_selected_signal (de);
1021 on_switch_page (PsppireDataEditor *de, GtkNotebookPage *p, gint pagenum, gpointer data)
1023 if ( pagenum != PSPPIRE_DATA_EDITOR_DATA_VIEW )
1025 emit_selected_signal (de);
1029 on_select_range (de);
1037 data_is_selected (PsppireDataEditor *de)
1039 GtkSheetRange range;
1042 if ( gtk_notebook_get_current_page (GTK_NOTEBOOK (de)) != PSPPIRE_DATA_EDITOR_DATA_VIEW)
1045 gtk_sheet_get_active_cell (GTK_SHEET (de->data_sheet), &row, &col);
1047 if ( row >= psppire_data_store_get_case_count (de->data_store)
1049 col >= psppire_var_store_get_var_cnt (de->var_store))
1054 gtk_sheet_get_selected_range (GTK_SHEET (de->data_sheet), &range);
1056 if ( range.rowi >= psppire_data_store_get_case_count (de->data_store)
1058 range.coli >= psppire_var_store_get_var_cnt (de->var_store))
1068 on_select_row (GtkSheet *sheet, gint row, PsppireDataEditor *de)
1070 g_signal_emit (de, data_editor_signals[CASES_SELECTED], 0, row);
1075 on_select_variable (GtkSheet *sheet, gint var, PsppireDataEditor *de)
1077 g_signal_emit (de, data_editor_signals[VARIABLES_SELECTED], 0, var);
1083 /* Clipboard stuff */
1086 #include <data/casereader.h>
1087 #include <data/case-map.h>
1088 #include <data/casewriter.h>
1090 #include <data/data-out.h>
1093 /* A casereader and dictionary holding the data currently in the clip */
1094 static struct casereader *clip_datasheet = NULL;
1095 static struct dictionary *clip_dict = NULL;
1098 static void data_sheet_update_clipboard (GtkSheet *);
1100 /* Set the clip according to the currently
1101 selected range in the data sheet */
1103 data_sheet_set_clip (GtkSheet *sheet)
1106 struct casewriter *writer ;
1107 GtkSheetRange range;
1108 PsppireDataStore *ds;
1109 struct case_map *map = NULL;
1110 casenumber max_rows;
1113 ds = PSPPIRE_DATA_STORE (gtk_sheet_get_model (sheet));
1115 gtk_sheet_get_selected_range (sheet, &range);
1117 /* If nothing selected, then use active cell */
1118 if ( range.row0 < 0 || range.col0 < 0 )
1121 gtk_sheet_get_active_cell (sheet, &row, &col);
1123 range.row0 = range.rowi = row;
1124 range.col0 = range.coli = col;
1127 /* The sheet range can include cells that do not include data.
1128 Exclude them from the range. */
1129 max_rows = psppire_data_store_get_case_count (ds);
1130 if (range.rowi >= max_rows)
1134 range.rowi = max_rows - 1;
1136 max_columns = dict_get_var_cnt (ds->dict->dict);
1137 if (range.coli >= max_columns)
1139 if (max_columns == 0)
1141 range.coli = max_columns - 1;
1144 g_return_if_fail (range.rowi >= range.row0);
1145 g_return_if_fail (range.row0 >= 0);
1146 g_return_if_fail (range.coli >= range.col0);
1147 g_return_if_fail (range.col0 >= 0);
1149 /* Destroy any existing clip */
1150 if ( clip_datasheet )
1152 casereader_destroy (clip_datasheet);
1153 clip_datasheet = NULL;
1158 dict_destroy (clip_dict);
1162 /* Construct clip dictionary. */
1163 clip_dict = dict_create ();
1164 for (i = range.col0; i <= range.coli; i++)
1166 const struct variable *old = dict_get_var (ds->dict->dict, i);
1167 dict_clone_var_assert (clip_dict, old, var_get_name (old));
1170 /* Construct clip data. */
1171 map = case_map_by_name (ds->dict->dict, clip_dict);
1172 writer = autopaging_writer_create (dict_get_next_value_idx (clip_dict));
1173 for (i = range.row0; i <= range.rowi ; ++i )
1177 if (psppire_case_file_get_case (ds->case_file, i, &old))
1181 case_map_execute (map, &old, &new);
1182 case_destroy (&old);
1183 casewriter_write (writer, &new);
1186 casewriter_force_error (writer);
1188 case_map_destroy (map);
1190 clip_datasheet = casewriter_make_reader (writer);
1192 data_sheet_update_clipboard (sheet);
1202 /* Perform data_out for case CC, variable V, appending to STRING */
1204 data_out_g_string (GString *string, const struct variable *v,
1205 const struct ccase *cc)
1209 const struct fmt_spec *fs = var_get_print_format (v);
1210 const union value *val = case_data (cc, v);
1211 buf = xzalloc (fs->w);
1213 data_out (val, fs, buf);
1215 g_string_append_len (string, buf, fs->w);
1226 const size_t val_cnt = casereader_get_value_cnt (clip_datasheet);
1227 const casenumber case_cnt = casereader_get_case_cnt (clip_datasheet);
1228 const size_t var_cnt = dict_get_var_cnt (clip_dict);
1230 string = g_string_sized_new (10 * val_cnt * case_cnt);
1232 for (r = 0 ; r < case_cnt ; ++r )
1236 if ( ! casereader_peek (clip_datasheet, r, &cc))
1238 g_warning ("Clipboard seems to have inexplicably shrunk");
1242 for (c = 0 ; c < var_cnt ; ++c)
1244 const struct variable *v = dict_get_var (clip_dict, c);
1245 data_out_g_string (string, v, &cc);
1246 if ( c < val_cnt - 1 )
1247 g_string_append (string, "\t");
1251 g_string_append (string, "\n");
1266 const size_t val_cnt = casereader_get_value_cnt (clip_datasheet);
1267 const casenumber case_cnt = casereader_get_case_cnt (clip_datasheet);
1268 const size_t var_cnt = dict_get_var_cnt (clip_dict);
1271 /* Guestimate the size needed */
1272 string = g_string_sized_new (20 * val_cnt * case_cnt);
1274 g_string_append (string, "<table>\n");
1275 for (r = 0 ; r < case_cnt ; ++r )
1279 if ( ! casereader_peek (clip_datasheet, r, &cc))
1281 g_warning ("Clipboard seems to have inexplicably shrunk");
1284 g_string_append (string, "<tr>\n");
1286 for (c = 0 ; c < var_cnt ; ++c)
1288 const struct variable *v = dict_get_var (clip_dict, c);
1289 g_string_append (string, "<td>");
1290 data_out_g_string (string, v, &cc);
1291 g_string_append (string, "</td>\n");
1294 g_string_append (string, "</tr>\n");
1298 g_string_append (string, "</table>\n");
1306 clipboard_get_cb (GtkClipboard *clipboard,
1307 GtkSelectionData *selection_data,
1311 GString *string = NULL;
1315 case SELECT_FMT_TEXT:
1316 string = clip_to_text ();
1318 case SELECT_FMT_HTML:
1319 string = clip_to_html ();
1322 g_assert_not_reached ();
1325 gtk_selection_data_set (selection_data, selection_data->target,
1327 (const guchar *) string->str, string->len);
1329 g_string_free (string, TRUE);
1333 clipboard_clear_cb (GtkClipboard *clipboard,
1336 dict_destroy (clip_dict);
1339 casereader_destroy (clip_datasheet);
1340 clip_datasheet = NULL;
1344 static const GtkTargetEntry targets[] = {
1345 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
1346 { "STRING", 0, SELECT_FMT_TEXT },
1347 { "TEXT", 0, SELECT_FMT_TEXT },
1348 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
1349 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
1350 { "text/plain", 0, SELECT_FMT_TEXT },
1351 { "text/html", 0, SELECT_FMT_HTML }
1357 data_sheet_update_clipboard (GtkSheet *sheet)
1359 GtkClipboard *clipboard =
1360 gtk_widget_get_clipboard (GTK_WIDGET (sheet),
1361 GDK_SELECTION_CLIPBOARD);
1363 if (!gtk_clipboard_set_with_owner (clipboard, targets,
1364 G_N_ELEMENTS (targets),
1365 clipboard_get_cb, clipboard_clear_cb,
1367 clipboard_clear_cb (clipboard, sheet);
1372 /* A callback for when the clipboard contents have been received */
1374 data_sheet_contents_received_callback (GtkClipboard *clipboard,
1375 GtkSelectionData *sd,
1380 gint next_row, next_column;
1383 PsppireDataEditor *data_editor = data;
1385 if ( sd->length < 0 )
1388 if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
1391 c = (char *) sd->data;
1393 /* Paste text to selected position */
1394 gtk_sheet_get_active_cell (GTK_SHEET (data_editor->data_sheet),
1397 g_return_if_fail (row >= 0);
1398 g_return_if_fail (column >= 0);
1400 first_column = column;
1402 next_column = column;
1403 while (count < sd->length)
1408 column = next_column;
1409 while (*c != '\t' && *c != '\n' && count < sd->length)
1417 next_column = column + 1;
1419 else if ( *c == '\n')
1422 next_column = first_column;
1428 /* Append some new cases if pasting beyond the last row */
1429 if ( row >= psppire_data_store_get_case_count (data_editor->data_store))
1430 psppire_data_store_insert_new_case (data_editor->data_store, row);
1432 psppire_data_store_set_string (data_editor->data_store, s, row, column);
1438 on_owner_change (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
1441 gboolean compatible_target = FALSE;
1442 PsppireDataEditor *de = PSPPIRE_DATA_EDITOR (data);
1444 for (i = 0 ; i < sizeof (targets) / sizeof(targets[0]) ; ++i )
1446 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
1447 if ( gtk_clipboard_wait_is_target_available (clip, atom))
1449 compatible_target = TRUE;
1454 g_signal_emit (de, data_editor_signals[DATA_AVAILABLE_CHANGED], 0,