Categoricals.c Create entries for all groups.
[pspp] / src / ui / gui / psppire-data-window.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <gtk/gtksignal.h>
20 #include <gtk/gtkbox.h>
21 #include <stdlib.h>
22
23 #include "data/any-reader.h"
24 #include "data/procedure.h"
25 #include "language/syntax-string-source.h"
26 #include "libpspp/message.h"
27 #include "ui/gui/help-menu.h"
28 #include "ui/gui/binomial-dialog.h"
29 #include "ui/gui/comments-dialog.h"
30 #include "ui/gui/compute-dialog.h"
31 #include "ui/gui/correlation-dialog.h"
32 #include "ui/gui/chi-square-dialog.h"
33 #include "ui/gui/crosstabs-dialog.h"
34 #include "ui/gui/descriptives-dialog.h"
35 #include "ui/gui/examine-dialog.h"
36 #include "ui/gui/executor.h"
37 #include "ui/gui/factor-dialog.h"
38 #include "ui/gui/find-dialog.h"
39 #include "ui/gui/frequencies-dialog.h"
40 #include "ui/gui/goto-case-dialog.h"
41 #include "ui/gui/helper.h"
42 #include "ui/gui/oneway-anova-dialog.h"
43 #include "ui/gui/psppire-data-window.h"
44 #include "ui/gui/psppire-syntax-window.h"
45 #include "ui/gui/psppire-window.h"
46 #include "ui/gui/psppire.h"
47 #include "ui/gui/rank-dialog.h"
48 #include "ui/gui/recode-dialog.h"
49 #include "ui/gui/regression-dialog.h"
50 #include "ui/gui/reliability-dialog.h"
51 #include "ui/gui/roc-dialog.h"
52 #include "ui/gui/select-cases-dialog.h"
53 #include "ui/gui/sort-cases-dialog.h"
54 #include "ui/gui/split-file-dialog.h"
55 #include "ui/gui/t-test-independent-samples-dialog.h"
56 #include "ui/gui/t-test-one-sample.h"
57 #include "ui/gui/t-test-paired-samples.h"
58 #include "ui/gui/text-data-import-dialog.h"
59 #include "ui/gui/transpose-dialog.h"
60 #include "ui/gui/aggregate-dialog.h"
61 #include "ui/gui/variable-info-dialog.h"
62 #include "ui/gui/weight-cases-dialog.h"
63 #include "ui/syntax-gen.h"
64
65 #include <gettext.h>
66 #define _(msgid) gettext (msgid)
67 #define N_(msgid) msgid
68
69
70
71 static void psppire_data_window_base_finalize (PsppireDataWindowClass *, gpointer);
72 static void psppire_data_window_base_init     (PsppireDataWindowClass *class);
73 static void psppire_data_window_class_init    (PsppireDataWindowClass *class);
74 static void psppire_data_window_init          (PsppireDataWindow      *data_editor);
75
76
77 static void psppire_data_window_iface_init (PsppireWindowIface *iface);
78
79
80 GType
81 psppire_data_window_get_type (void)
82 {
83   static GType psppire_data_window_type = 0;
84
85   if (!psppire_data_window_type)
86     {
87       static const GTypeInfo psppire_data_window_info =
88         {
89           sizeof (PsppireDataWindowClass),
90           (GBaseInitFunc) psppire_data_window_base_init,
91           (GBaseFinalizeFunc) psppire_data_window_base_finalize,
92           (GClassInitFunc)psppire_data_window_class_init,
93           (GClassFinalizeFunc) NULL,
94           NULL,
95           sizeof (PsppireDataWindow),
96           0,
97           (GInstanceInitFunc) psppire_data_window_init,
98         };
99
100       static const GInterfaceInfo window_interface_info =
101         {
102           (GInterfaceInitFunc) psppire_data_window_iface_init,
103           NULL,
104           NULL
105         };
106
107       psppire_data_window_type =
108         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireDataWindow",
109                                 &psppire_data_window_info, 0);
110
111
112       g_type_add_interface_static (psppire_data_window_type,
113                                    PSPPIRE_TYPE_WINDOW_MODEL,
114                                    &window_interface_info);
115     }
116
117   return psppire_data_window_type;
118 }
119
120 static GObjectClass *parent_class ;
121
122 static void
123 psppire_data_window_finalize (GObject *object)
124 {
125   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (object);
126
127   g_object_unref (de->builder);
128
129   if (G_OBJECT_CLASS (parent_class)->finalize)
130     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
131 }
132
133
134 static void
135 psppire_data_window_class_init (PsppireDataWindowClass *class)
136 {
137   parent_class = g_type_class_peek_parent (class);
138 }
139
140
141 static void
142 psppire_data_window_base_init (PsppireDataWindowClass *class)
143 {
144   GObjectClass *object_class = G_OBJECT_CLASS (class);
145
146   object_class->finalize = psppire_data_window_finalize;
147 }
148
149
150
151 static void
152 psppire_data_window_base_finalize (PsppireDataWindowClass *class,
153                                    gpointer class_data)
154 {
155 }
156
157
158 \f
159
160
161 extern PsppireVarStore *the_var_store;
162 extern struct dataset *the_dataset;
163 extern PsppireDataStore *the_data_store ;
164
165 extern GtkRecentManager *the_recent_mgr;
166
167 static void
168 set_paste_menuitem_sensitivity (PsppireDataWindow *de, gboolean x)
169 {
170   GtkAction *edit_paste = get_action_assert (de->builder, "edit_paste");
171
172   gtk_action_set_sensitive (edit_paste, x);
173 }
174
175 static void
176 set_cut_copy_menuitem_sensitivity (PsppireDataWindow *de, gboolean x)
177 {
178   GtkAction *edit_copy = get_action_assert (de->builder, "edit_copy");
179   GtkAction *edit_cut = get_action_assert (de->builder, "edit_cut");
180
181   gtk_action_set_sensitive (edit_copy, x);
182   gtk_action_set_sensitive (edit_cut, x);
183 }
184
185 /* Run the EXECUTE command. */
186 static void
187 execute (void)
188 {
189   struct getl_interface *sss = create_syntax_string_source ("EXECUTE.");
190
191   execute_syntax (sss);
192 }
193
194 static void
195 transformation_change_callback (bool transformations_pending,
196                                 gpointer data)
197 {
198   PsppireDataWindow  *de = PSPPIRE_DATA_WINDOW (data);
199
200   GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (de->builder, "uimanager1", GTK_TYPE_UI_MANAGER));
201
202   GtkWidget *menuitem =
203     gtk_ui_manager_get_widget (uim,"/ui/menubar/transform/transform_run-pending");
204
205   GtkWidget *status_label  =
206     get_widget_assert (de->builder, "case-counter-area");
207
208   gtk_widget_set_sensitive (menuitem, transformations_pending);
209
210
211   if ( transformations_pending)
212     gtk_label_set_text (GTK_LABEL (status_label),
213                         _("Transformations Pending"));
214   else
215     gtk_label_set_text (GTK_LABEL (status_label), "");
216 }
217
218 /* Callback for when the dictionary changes its filter variable */
219 static void
220 on_filter_change (GObject *o, gint filter_index, gpointer data)
221 {
222   PsppireDataWindow  *de = PSPPIRE_DATA_WINDOW (data);
223
224   GtkWidget *filter_status_area =
225     get_widget_assert (de->builder, "filter-use-status-area");
226
227   if ( filter_index == -1 )
228     {
229       gtk_label_set_text (GTK_LABEL (filter_status_area), _("Filter off"));
230     }
231   else
232     {
233       PsppireVarStore *vs = NULL;
234       PsppireDict *dict = NULL;
235       struct variable *var ;
236       gchar *text ;
237
238       g_object_get (de->data_editor, "var-store", &vs, NULL);
239       g_object_get (vs, "dictionary", &dict, NULL);
240
241       var = psppire_dict_get_variable (dict, filter_index);
242
243       text = g_strdup_printf (_("Filter by %s"), var_get_name (var));
244
245       gtk_label_set_text (GTK_LABEL (filter_status_area), text);
246
247       g_free (text);
248     }
249 }
250
251 /* Callback for when the dictionary changes its split variables */
252 static void
253 on_split_change (PsppireDict *dict, gpointer data)
254 {
255   PsppireDataWindow  *de = PSPPIRE_DATA_WINDOW (data);
256
257   size_t n_split_vars = dict_get_split_cnt (dict->dict);
258
259   GtkWidget *split_status_area =
260     get_widget_assert (de->builder, "split-file-status-area");
261
262   if ( n_split_vars == 0 )
263     {
264       gtk_label_set_text (GTK_LABEL (split_status_area), _("No Split"));
265     }
266   else
267     {
268       gint i;
269       GString *text;
270       const struct variable *const * split_vars =
271         dict_get_split_vars (dict->dict);
272
273       text = g_string_new (_("Split by "));
274
275       for (i = 0 ; i < n_split_vars - 1; ++i )
276         {
277           g_string_append_printf (text, "%s, ", var_get_name (split_vars[i]));
278         }
279       g_string_append (text, var_get_name (split_vars[i]));
280
281       gtk_label_set_text (GTK_LABEL (split_status_area), text->str);
282
283       g_string_free (text, TRUE);
284     }
285 }
286
287
288
289
290 /* Callback for when the dictionary changes its weights */
291 static void
292 on_weight_change (GObject *o, gint weight_index, gpointer data)
293 {
294   PsppireDataWindow  *de = PSPPIRE_DATA_WINDOW (data);
295
296   GtkWidget *weight_status_area =
297     get_widget_assert (de->builder, "weight-status-area");
298
299   if ( weight_index == -1 )
300     {
301       gtk_label_set_text (GTK_LABEL (weight_status_area), _("Weights off"));
302     }
303   else
304     {
305       struct variable *var ;
306       PsppireVarStore *vs = NULL;
307       PsppireDict *dict = NULL;
308       gchar *text;
309
310       g_object_get (de->data_editor, "var-store", &vs, NULL);
311       g_object_get (vs, "dictionary", &dict, NULL);
312
313       var = psppire_dict_get_variable (dict, weight_index);
314
315       text = g_strdup_printf (_("Weight by %s"), var_get_name (var));
316
317       gtk_label_set_text (GTK_LABEL (weight_status_area), text);
318
319       g_free (text);
320     }
321 }
322
323 #if 0
324 static void
325 dump_rm (GtkRecentManager *rm)
326 {
327   GList *items = gtk_recent_manager_get_items (rm);
328
329   GList *i;
330
331   g_print ("Recent Items:\n");
332   for (i = items; i; i = i->next)
333     {
334       GtkRecentInfo *ri = i->data;
335
336       g_print ("Item: %s (Mime: %s) (Desc: %s) (URI: %s)\n",
337                gtk_recent_info_get_short_name (ri),
338                gtk_recent_info_get_mime_type (ri),
339                gtk_recent_info_get_description (ri),
340                gtk_recent_info_get_uri (ri)
341                );
342
343
344       gtk_recent_info_unref (ri);
345     }
346
347   g_list_free (items);
348 }
349 #endif
350
351
352 static gboolean
353 load_file (PsppireWindow *de, const gchar *file_name)
354 {
355   gchar *native_file_name;
356   struct getl_interface *sss;
357   struct string filename;
358
359   ds_init_empty (&filename);
360
361   native_file_name =
362     convert_glib_filename_to_system_filename (file_name, NULL);
363
364   syntax_gen_string (&filename, ss_cstr (native_file_name));
365
366   g_free (native_file_name);
367
368   sss = create_syntax_string_source ("GET FILE=%s.",
369                                      ds_cstr (&filename));
370
371   ds_destroy (&filename);
372
373   if (execute_syntax (sss) )
374     return TRUE;
375
376   return FALSE;
377 }
378
379 static GtkWidget *
380 sysfile_chooser_dialog (PsppireWindow *toplevel)
381 {
382   GtkWidget *dialog =
383     gtk_file_chooser_dialog_new (_("Open"),
384                                  GTK_WINDOW (toplevel),
385                                  GTK_FILE_CHOOSER_ACTION_OPEN,
386                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
387                                  GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
388                                  NULL);
389
390   GtkFileFilter *filter;
391
392   filter = gtk_file_filter_new ();
393   gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
394   gtk_file_filter_add_pattern (filter, "*.sav");
395   gtk_file_filter_add_pattern (filter, "*.SAV");
396   gtk_file_filter_add_pattern (filter, "*.por");
397   gtk_file_filter_add_pattern (filter, "*.POR");
398   gtk_file_filter_add_pattern (filter, "*.sps");
399   gtk_file_filter_add_pattern (filter, "*.SPS");
400   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
401
402   filter = gtk_file_filter_new ();
403   gtk_file_filter_set_name (filter, _("System Files (*.sav)"));
404   gtk_file_filter_add_pattern (filter, "*.sav");
405   gtk_file_filter_add_pattern (filter, "*.SAV");
406   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
407
408   filter = gtk_file_filter_new ();
409   gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
410   gtk_file_filter_add_pattern (filter, "*.por");
411   gtk_file_filter_add_pattern (filter, "*.POR");
412   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
413
414   filter = gtk_file_filter_new ();
415   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
416   gtk_file_filter_add_pattern (filter, "*.sps");
417   gtk_file_filter_add_pattern (filter, "*.SPS");
418   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
419
420   filter = gtk_file_filter_new ();
421   gtk_file_filter_set_name (filter, _("All Files"));
422   gtk_file_filter_add_pattern (filter, "*");
423   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
424
425   {
426     gchar *dir_name;
427     gchar *filename = NULL;
428     g_object_get (toplevel, "filename", &filename, NULL);
429
430     if ( ! g_path_is_absolute (filename))
431       {
432         gchar *path =
433           g_build_filename (g_get_current_dir (), filename, NULL);
434         dir_name = g_path_get_dirname (path);
435         g_free (path);
436       }
437     else
438       {
439         dir_name = g_path_get_dirname (filename);
440       }
441     gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
442                                          dir_name);
443     free (dir_name);
444   }
445
446   return dialog;
447 }
448
449 /* Callback for the data_open action.
450    Prompts for a filename and opens it */
451 static void
452 open_window (PsppireWindow *de)
453 {
454   GtkWidget *dialog = sysfile_chooser_dialog (de);
455
456   switch (gtk_dialog_run (GTK_DIALOG (dialog)))
457     {
458     case GTK_RESPONSE_ACCEPT:
459       {
460         gchar *name =
461           gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
462
463         gchar *sysname = convert_glib_filename_to_system_filename (name, NULL);
464
465         if (any_reader_may_open (sysname))
466           psppire_window_load (de, name);
467         else
468           open_syntax_window (name);
469
470         g_free (sysname);
471         g_free (name);
472       }
473       break;
474     default:
475       break;
476     }
477
478   gtk_widget_destroy (dialog);
479 }
480
481 /* Returns true if NAME has a suffix which might denote a PSPP file */
482 static gboolean
483 name_has_suffix (const gchar *name)
484 {
485   if ( g_str_has_suffix (name, ".sav"))
486     return TRUE;
487   if ( g_str_has_suffix (name, ".SAV"))
488     return TRUE;
489   if ( g_str_has_suffix (name, ".por"))
490     return TRUE;
491   if ( g_str_has_suffix (name, ".POR"))
492     return TRUE;
493
494   return FALSE;
495 }
496
497
498 /* Save DE to file */
499 static void
500 save_file (PsppireWindow *w)
501 {
502   gchar *native_file_name = NULL;
503   gchar *file_name = NULL;
504   GString *fnx;
505   struct getl_interface *sss;
506   struct string filename ;
507   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (w);
508
509   g_object_get (w, "filename", &file_name, NULL);
510
511   fnx = g_string_new (file_name);
512
513   if ( ! name_has_suffix (fnx->str))
514     {
515       if ( de->save_as_portable)
516         g_string_append (fnx, ".por");
517       else
518         g_string_append (fnx, ".sav");
519     }
520
521   ds_init_empty (&filename);
522
523   native_file_name =
524     convert_glib_filename_to_system_filename (fnx->str, NULL);
525
526   g_string_free (fnx, TRUE);
527
528   syntax_gen_string (&filename, ss_cstr (native_file_name));
529   g_free (native_file_name);
530
531   if ( de->save_as_portable )
532     {
533       sss = create_syntax_string_source ("EXPORT OUTFILE=%s.",
534                                          ds_cstr (&filename));
535     }
536   else
537     {
538       sss = create_syntax_string_source ("SAVE OUTFILE=%s.",
539                                          ds_cstr (&filename));
540     }
541
542   ds_destroy (&filename);
543
544   execute_syntax (sss);
545 }
546
547
548 static void
549 insert_case (PsppireDataWindow *dw)
550 {
551   psppire_data_editor_insert_case (dw->data_editor);
552 }
553
554 static void
555 on_insert_variable (PsppireDataWindow *dw)
556 {
557   psppire_data_editor_insert_variable (dw->data_editor);
558 }
559
560
561 static void
562 display_dict (PsppireDataWindow *de)
563 {
564
565   struct getl_interface *sss =
566     create_syntax_string_source ("DISPLAY DICTIONARY.");
567
568   execute_syntax (sss);
569 }
570
571 static void
572 sysfile_info (PsppireDataWindow *de)
573 {
574   GtkWidget *dialog = sysfile_chooser_dialog (PSPPIRE_WINDOW (de));
575
576   if  ( GTK_RESPONSE_ACCEPT == gtk_dialog_run (GTK_DIALOG (dialog)))
577     {
578       struct string filename;
579       struct getl_interface *sss;
580       gchar *file_name =
581         gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
582
583       gchar *native_file_name =
584         convert_glib_filename_to_system_filename (file_name, NULL);
585
586       ds_init_empty (&filename);
587
588       syntax_gen_string (&filename, ss_cstr (native_file_name));
589
590       g_free (native_file_name);
591
592       sss = create_syntax_string_source ("SYSFILE INFO %s.",
593                                          ds_cstr (&filename));
594       execute_syntax (sss);
595     }
596
597   gtk_widget_destroy (dialog);
598 }
599
600
601 /* Callback for data_save_as action. Prompt for a filename and save */
602 static void
603 data_save_as_dialog (PsppireDataWindow *de)
604 {
605   GtkWidget *button_sys;
606   GtkWidget *dialog =
607     gtk_file_chooser_dialog_new (_("Save"),
608                                  GTK_WINDOW (de),
609                                  GTK_FILE_CHOOSER_ACTION_SAVE,
610                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
611                                  GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
612                                  NULL);
613
614   GtkFileFilter *filter = gtk_file_filter_new ();
615   gtk_file_filter_set_name (filter, _("System Files (*.sav)"));
616   gtk_file_filter_add_pattern (filter, "*.sav");
617   gtk_file_filter_add_pattern (filter, "*.SAV");
618   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
619
620   filter = gtk_file_filter_new ();
621   gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
622   gtk_file_filter_add_pattern (filter, "*.por");
623   gtk_file_filter_add_pattern (filter, "*.POR");
624   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
625
626   filter = gtk_file_filter_new ();
627   gtk_file_filter_set_name (filter, _("All Files"));
628   gtk_file_filter_add_pattern (filter, "*");
629   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
630
631   {
632     GtkWidget *button_por;
633     GtkWidget *vbox = gtk_vbox_new (TRUE, 5);
634     button_sys =
635       gtk_radio_button_new_with_label (NULL, _("System File"));
636
637     button_por =
638       gtk_radio_button_new_with_label
639       (gtk_radio_button_get_group (GTK_RADIO_BUTTON(button_sys)),
640        _("Portable File"));
641
642     psppire_box_pack_start_defaults (GTK_BOX (vbox), button_sys);
643     psppire_box_pack_start_defaults (GTK_BOX (vbox), button_por);
644
645     gtk_widget_show_all (vbox);
646
647     gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER(dialog), vbox);
648   }
649
650   switch (gtk_dialog_run (GTK_DIALOG (dialog)))
651     {
652     case GTK_RESPONSE_ACCEPT:
653       {
654         GString *filename =
655           g_string_new
656           (
657            gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog))
658            );
659
660         de->save_as_portable =
661           ! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_sys));
662
663         if ( ! name_has_suffix (filename->str))
664           {
665             if ( de->save_as_portable)
666               g_string_append (filename, ".por");
667             else
668               g_string_append (filename, ".sav");
669           }
670
671         psppire_window_set_filename (PSPPIRE_WINDOW (de), filename->str);
672
673         save_file (PSPPIRE_WINDOW (de));
674
675         g_string_free (filename, TRUE);
676       }
677       break;
678     default:
679       break;
680     }
681
682   gtk_widget_destroy (dialog);
683 }
684
685
686 /* Callback for data_save action.
687  */
688 static void
689 data_save (PsppireWindow *de)
690 {
691   const gchar *fn = psppire_window_get_filename (de);
692
693   if ( NULL != fn)
694     psppire_window_save (de);
695   else
696     data_save_as_dialog (PSPPIRE_DATA_WINDOW (de));
697 }
698
699
700 /* Callback for data_new action.
701    Performs the NEW FILE command */
702 static void
703 new_file (PsppireDataWindow *de)
704 {
705   struct getl_interface *sss =
706     create_syntax_string_source ("NEW FILE.");
707
708   execute_syntax (sss);
709
710   psppire_window_set_filename (PSPPIRE_WINDOW (de), NULL);
711 }
712
713
714
715 static void
716 on_edit_paste (PsppireDataWindow  *de)
717 {
718   psppire_data_editor_clip_paste (de->data_editor);
719 }
720
721 static void
722 on_edit_copy (PsppireDataWindow  *de)
723 {
724   psppire_data_editor_clip_copy (de->data_editor);
725 }
726
727
728
729 static void
730 on_edit_cut (PsppireDataWindow  *de)
731 {
732   psppire_data_editor_clip_cut (de->data_editor);
733 }
734
735
736 static void
737 status_bar_activate (PsppireDataWindow  *de, GtkToggleAction *action)
738 {
739   GtkWidget *statusbar = get_widget_assert (de->builder, "status-bar");
740
741   if ( gtk_toggle_action_get_active (action))
742     gtk_widget_show (statusbar);
743   else
744     gtk_widget_hide (statusbar);
745 }
746
747
748 static void
749 grid_lines_activate (PsppireDataWindow  *de, GtkToggleAction *action)
750 {
751   const gboolean grid_visible = gtk_toggle_action_get_active (action);
752
753   psppire_data_editor_show_grid (de->data_editor, grid_visible);
754 }
755
756 static void
757 data_view_activate (PsppireDataWindow  *de)
758 {
759   gtk_notebook_set_current_page (GTK_NOTEBOOK (de->data_editor), PSPPIRE_DATA_EDITOR_DATA_VIEW);
760 }
761
762
763 static void
764 variable_view_activate (PsppireDataWindow  *de)
765 {
766   gtk_notebook_set_current_page (GTK_NOTEBOOK (de->data_editor), PSPPIRE_DATA_EDITOR_VARIABLE_VIEW);
767 }
768
769
770 static void
771 fonts_activate (PsppireDataWindow  *de)
772 {
773   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (de));
774   PangoFontDescription *current_font;
775   gchar *font_name;
776   GtkWidget *dialog =
777     gtk_font_selection_dialog_new (_("Font Selection"));
778
779
780   current_font = GTK_WIDGET(de->data_editor)->style->font_desc;
781   font_name = pango_font_description_to_string (current_font);
782
783   gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (dialog), font_name);
784
785   g_free (font_name);
786
787   gtk_window_set_transient_for (GTK_WINDOW (dialog),
788                                 GTK_WINDOW (toplevel));
789
790   if ( GTK_RESPONSE_OK == gtk_dialog_run (GTK_DIALOG (dialog)) )
791     {
792       const gchar *font = gtk_font_selection_dialog_get_font_name
793         (GTK_FONT_SELECTION_DIALOG (dialog));
794
795       PangoFontDescription* font_desc =
796         pango_font_description_from_string (font);
797
798       psppire_data_editor_set_font (de->data_editor, font_desc);
799     }
800
801   gtk_widget_hide (dialog);
802 }
803
804
805
806 /* Callback for the value labels action */
807 static void
808 toggle_value_labels (PsppireDataWindow  *de, GtkToggleAction *ta)
809 {
810   g_object_set (de->data_editor, "value-labels", gtk_toggle_action_get_active (ta), NULL);
811 }
812
813 static void
814 toggle_split_window (PsppireDataWindow  *de, GtkToggleAction *ta)
815 {
816   psppire_data_editor_split_window (de->data_editor,
817                                     gtk_toggle_action_get_active (ta));
818 }
819
820
821 static void
822 file_quit (void)
823 {
824   /* FIXME: Need to be more intelligent here.
825      Give the user the opportunity to save any unsaved data.
826   */
827   g_object_unref (the_data_store);
828
829   psppire_quit ();
830 }
831
832
833 static void
834 on_recent_data_select (GtkMenuShell *menushell,
835                        PsppireWindow *window)
836 {
837   gchar *file;
838
839   gchar *uri =
840     gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (menushell));
841
842   file = g_filename_from_uri (uri, NULL, NULL);
843
844   g_free (uri);
845
846   psppire_window_load (window, file);
847
848   g_free (file);
849 }
850
851 static void
852 on_recent_files_select (GtkMenuShell *menushell,   gpointer user_data)
853 {
854   gchar *file;
855
856   GtkWidget *se ;
857
858   gchar *uri =
859     gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (menushell));
860
861   file = g_filename_from_uri (uri, NULL, NULL);
862
863   g_free (uri);
864
865   se = psppire_syntax_window_new ();
866
867   if ( psppire_window_load (PSPPIRE_WINDOW (se), file) ) 
868     gtk_widget_show (se);
869   else
870     gtk_widget_destroy (se);
871
872   g_free (file);
873 }
874
875
876 static void
877 enable_delete_cases (GtkWidget *w, gint case_num, gpointer data)
878 {
879   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
880
881   gtk_action_set_visible (de->delete_cases, case_num != -1);
882 }
883
884
885 static void
886 enable_delete_variables (GtkWidget *w, gint var, gpointer data)
887 {
888   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
889
890   gtk_action_set_visible (de->delete_variables, var != -1);
891 }
892
893 /* Callback for when the datasheet/varsheet is selected */
894 static void
895 on_switch_sheet (GtkNotebook *notebook,
896                  GtkNotebookPage *page,
897                  guint page_num,
898                  gpointer user_data)
899 {
900   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (user_data);
901
902   GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (de->builder, "uimanager1", GTK_TYPE_UI_MANAGER));
903
904   GtkWidget *view_data =
905     gtk_ui_manager_get_widget (uim,"/ui/menubar/view/view_data");
906
907   GtkWidget *view_variables =
908     gtk_ui_manager_get_widget (uim,"/ui/menubar/view/view_variables");
909
910   switch (page_num)
911     {
912     case PSPPIRE_DATA_EDITOR_VARIABLE_VIEW:
913       gtk_widget_hide (view_variables);
914       gtk_widget_show (view_data);
915       gtk_action_set_sensitive (de->insert_variable, TRUE);
916       gtk_action_set_sensitive (de->insert_case, FALSE);
917       gtk_action_set_sensitive (de->invoke_goto_dialog, FALSE);
918       break;
919     case PSPPIRE_DATA_EDITOR_DATA_VIEW:
920       gtk_widget_show (view_variables);
921       gtk_widget_hide (view_data);
922       gtk_action_set_sensitive (de->invoke_goto_dialog, TRUE);
923       gtk_action_set_sensitive (de->insert_case, TRUE);
924       break;
925     default:
926       g_assert_not_reached ();
927       break;
928     }
929
930 #if 0
931   update_paste_menuitem (de, page_num);
932 #endif
933 }
934
935
936
937 static void
938 set_unsaved (gpointer w)
939 {
940   psppire_window_set_unsaved (PSPPIRE_WINDOW (w));
941 }
942
943
944 /* Connects the action called ACTION_NAME to HANDLER passing DW as the auxilliary data.
945    Returns a pointer to the action
946 */
947 static GtkAction *
948 connect_action (PsppireDataWindow *dw, const char *action_name, 
949                                     GCallback handler)
950 {
951   GtkAction *action = get_action_assert (dw->builder, action_name);
952  
953   g_signal_connect_swapped (action, "activate", handler, dw);
954
955   return action;
956 }
957
958 static void
959 psppire_data_window_init (PsppireDataWindow *de)
960 {
961   PsppireVarStore *vs;
962   PsppireDict *dict = NULL;
963
964   GtkWidget *menubar;
965   GtkWidget *hb ;
966   GtkWidget *sb ;
967
968   GtkWidget *box = gtk_vbox_new (FALSE, 0);
969   de->builder = builder_new ("data-editor.ui");
970
971   menubar = get_widget_assert (de->builder, "menubar");
972   hb = get_widget_assert (de->builder, "handlebox1");
973   sb = get_widget_assert (de->builder, "status-bar");
974
975   de->data_editor =
976     PSPPIRE_DATA_EDITOR (psppire_data_editor_new (the_var_store, the_data_store));
977
978   g_signal_connect_swapped (the_data_store, "case-changed",
979                             G_CALLBACK (set_unsaved), de);
980
981   g_signal_connect_swapped (the_data_store, "case-inserted",
982                             G_CALLBACK (set_unsaved), de);
983
984   g_signal_connect_swapped (the_data_store, "cases-deleted",
985                             G_CALLBACK (set_unsaved), de);
986
987   dataset_set_callback (the_dataset, set_unsaved, de);
988
989   connect_help (de->builder);
990
991   gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
992   gtk_box_pack_start (GTK_BOX (box), hb, FALSE, TRUE, 0);
993   gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (de->data_editor), TRUE, TRUE, 0);
994   gtk_box_pack_start (GTK_BOX (box), sb, FALSE, TRUE, 0);
995
996   gtk_container_add (GTK_CONTAINER (de), box);
997
998   set_cut_copy_menuitem_sensitivity (de, FALSE);
999
1000   g_signal_connect_swapped (de->data_editor, "data-selection-changed",
1001                             G_CALLBACK (set_cut_copy_menuitem_sensitivity), de);
1002
1003
1004   set_paste_menuitem_sensitivity (de, FALSE);
1005
1006   g_signal_connect_swapped (de->data_editor, "data-available-changed",
1007                             G_CALLBACK (set_paste_menuitem_sensitivity), de);
1008
1009   dataset_add_transform_change_callback (the_dataset,
1010                                          transformation_change_callback,
1011                                          de);
1012
1013
1014   vs = the_var_store;
1015
1016   g_assert(vs); /* Traps a possible bug in w32 build */
1017
1018   g_object_get (vs, "dictionary", &dict, NULL);
1019
1020   g_signal_connect (dict, "weight-changed",
1021                     G_CALLBACK (on_weight_change),
1022                     de);
1023
1024   g_signal_connect (dict, "filter-changed",
1025                     G_CALLBACK (on_filter_change),
1026                     de);
1027
1028   g_signal_connect (dict, "split-changed",
1029                     G_CALLBACK (on_split_change),
1030                     de);
1031
1032
1033   connect_action (de, "edit_copy", G_CALLBACK (on_edit_copy));
1034
1035   connect_action (de, "edit_cut", G_CALLBACK (on_edit_cut));
1036
1037   connect_action (de, "file_new_data", G_CALLBACK (new_file));
1038
1039   connect_action (de, "file_import-text", G_CALLBACK (text_data_import_assistant));
1040
1041   connect_action (de, "file_save", G_CALLBACK (data_save));
1042  
1043   connect_action (de, "file_open", G_CALLBACK (open_window));
1044
1045   connect_action (de, "file_save_as", G_CALLBACK (data_save_as_dialog));
1046
1047   connect_action (de, "file_information_working-file", G_CALLBACK (display_dict));
1048
1049   connect_action (de, "file_information_external-file", G_CALLBACK (sysfile_info));
1050
1051   connect_action (de, "edit_paste", G_CALLBACK (on_edit_paste));
1052
1053   de->insert_case = connect_action (de, "edit_insert-case", G_CALLBACK (insert_case));
1054
1055   de->insert_variable = connect_action (de, "action_insert-variable", G_CALLBACK (on_insert_variable));
1056
1057   de->invoke_goto_dialog = connect_action (de, "edit_goto-case", G_CALLBACK (goto_case_dialog));
1058
1059   g_signal_connect_swapped (get_action_assert (de->builder, "view_value-labels"), "toggled", G_CALLBACK (toggle_value_labels), de);
1060
1061   {
1062     de->delete_cases = get_action_assert (de->builder, "edit_clear-cases");
1063
1064     g_signal_connect_swapped (de->delete_cases, "activate", G_CALLBACK (psppire_data_editor_delete_cases), de->data_editor);
1065
1066     gtk_action_set_visible (de->delete_cases, FALSE);
1067   }
1068
1069
1070   {
1071     de->delete_variables = get_action_assert (de->builder, "edit_clear-variables");
1072
1073     g_signal_connect_swapped (de->delete_variables, "activate", G_CALLBACK (psppire_data_editor_delete_variables), de->data_editor);
1074
1075     gtk_action_set_visible (de->delete_variables, FALSE);
1076   }
1077
1078
1079   connect_action (de, "data_transpose", G_CALLBACK (transpose_dialog));
1080
1081   connect_action (de, "data_select-cases", G_CALLBACK (select_cases_dialog));
1082  
1083   connect_action (de, "data_sort-cases", G_CALLBACK (sort_cases_dialog));
1084
1085   connect_action (de, "data_aggregate", G_CALLBACK (aggregate_dialog));
1086
1087   connect_action (de, "transform_compute", G_CALLBACK (compute_dialog));
1088
1089   connect_action (de, "edit_find", G_CALLBACK (find_dialog));
1090
1091   connect_action (de, "data_split-file", G_CALLBACK (split_file_dialog));
1092
1093   connect_action (de, "data_weight-cases", G_CALLBACK (weight_cases_dialog));
1094
1095
1096   connect_action (de, "utilities_variables", G_CALLBACK (variable_info_dialog));
1097  
1098   connect_action (de, "oneway-anova", G_CALLBACK (oneway_anova_dialog));
1099
1100   connect_action (de, "indep-t-test", G_CALLBACK (t_test_independent_samples_dialog));
1101
1102   connect_action (de, "paired-t-test", G_CALLBACK (t_test_paired_samples_dialog));
1103
1104   connect_action (de, "one-sample-t-test", G_CALLBACK (t_test_one_sample_dialog));
1105
1106   connect_action (de, "utilities_comments", G_CALLBACK (comments_dialog));
1107  
1108   connect_action (de, "transform_rank", G_CALLBACK (rank_dialog));
1109  
1110   connect_action (de, "transform_recode-same", G_CALLBACK (recode_same_dialog));
1111  
1112   connect_action (de, "transform_recode-different", G_CALLBACK (recode_different_dialog));
1113
1114   connect_action (de, "analyze_descriptives", G_CALLBACK (descriptives_dialog));
1115  
1116   connect_action (de, "analyze_frequencies", G_CALLBACK (frequencies_dialog));
1117  
1118   connect_action (de, "crosstabs", G_CALLBACK (crosstabs_dialog));
1119  
1120   connect_action (de, "analyze_explore", G_CALLBACK (examine_dialog));
1121  
1122   connect_action (de, "linear-regression", G_CALLBACK (regression_dialog));
1123  
1124   connect_action (de, "reliability", G_CALLBACK (reliability_dialog));
1125  
1126   connect_action (de, "roc-curve", G_CALLBACK (roc_dialog));
1127
1128   connect_action (de, "correlation", G_CALLBACK (correlation_dialog));
1129  
1130   connect_action (de, "factor-analysis", G_CALLBACK (factor_dialog));
1131
1132   connect_action (de, "chi-square", G_CALLBACK (chisquare_dialog));
1133
1134   connect_action (de, "binomial", G_CALLBACK (binomial_dialog));
1135  
1136
1137   {
1138     GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (de->builder, "uimanager1", GTK_TYPE_UI_MANAGER));
1139
1140     GtkWidget *recent_data =
1141       gtk_ui_manager_get_widget (uim,"/ui/menubar/file/file_recent-data");
1142
1143     GtkWidget *recent_files =
1144       gtk_ui_manager_get_widget (uim,"/ui/menubar/file/file_recent-files");
1145
1146
1147     GtkWidget *menu_data =
1148       gtk_recent_chooser_menu_new_for_manager (the_recent_mgr);
1149
1150     GtkWidget *menu_files =
1151       gtk_recent_chooser_menu_new_for_manager (the_recent_mgr);
1152
1153     {
1154       GtkRecentFilter *filter = gtk_recent_filter_new ();
1155
1156       gtk_recent_filter_add_pattern (filter, "*.sav");
1157       gtk_recent_filter_add_pattern (filter, "*.SAV");
1158       gtk_recent_filter_add_pattern (filter, "*.por");
1159       gtk_recent_filter_add_pattern (filter, "*.POR");
1160
1161       gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (menu_data), GTK_RECENT_SORT_MRU);
1162
1163       gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (menu_data), filter);
1164     }
1165
1166     gtk_menu_item_set_submenu (GTK_MENU_ITEM (recent_data), menu_data);
1167
1168
1169     g_signal_connect (menu_data, "selection-done", G_CALLBACK (on_recent_data_select), de);
1170
1171     {
1172       GtkRecentFilter *filter = gtk_recent_filter_new ();
1173
1174       gtk_recent_filter_add_pattern (filter, "*.sps");
1175       gtk_recent_filter_add_pattern (filter, "*.SPS");
1176
1177       gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (menu_files), GTK_RECENT_SORT_MRU);
1178
1179       gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (menu_files), filter);
1180     }
1181
1182     gtk_menu_item_set_submenu (GTK_MENU_ITEM (recent_files), menu_files);
1183
1184     g_signal_connect (menu_files, "selection-done", G_CALLBACK (on_recent_files_select), de);
1185
1186   }
1187
1188   connect_action (de, "file_new_syntax", G_CALLBACK (create_syntax_window));
1189
1190
1191   g_signal_connect (de->data_editor,
1192                     "cases-selected",
1193                     G_CALLBACK (enable_delete_cases),
1194                     de);
1195
1196   g_signal_connect (de->data_editor,
1197                     "variables-selected",
1198                     G_CALLBACK (enable_delete_variables),
1199                     de);
1200
1201
1202   g_signal_connect (de->data_editor,
1203                     "switch-page",
1204                     G_CALLBACK (on_switch_sheet), de);
1205
1206   gtk_notebook_set_current_page (GTK_NOTEBOOK (de->data_editor), PSPPIRE_DATA_EDITOR_VARIABLE_VIEW);
1207   gtk_notebook_set_current_page (GTK_NOTEBOOK (de->data_editor), PSPPIRE_DATA_EDITOR_DATA_VIEW);
1208
1209   connect_action (de, "view_statusbar", G_CALLBACK (status_bar_activate));
1210
1211   connect_action (de, "view_gridlines", G_CALLBACK (grid_lines_activate));
1212
1213   connect_action (de, "view_data", G_CALLBACK (data_view_activate));
1214
1215   connect_action (de, "view_variables", G_CALLBACK (variable_view_activate));
1216
1217   connect_action (de, "view_fonts", G_CALLBACK (fonts_activate));
1218
1219   connect_action (de, "file_quit", G_CALLBACK (file_quit));
1220
1221   connect_action (de, "transform_run-pending", G_CALLBACK (execute));
1222
1223   connect_action (de, "windows_minimise_all", G_CALLBACK (psppire_window_minimise_all));
1224
1225   g_signal_connect_swapped (get_action_assert (de->builder, "windows_split"), "toggled", G_CALLBACK (toggle_split_window), de);
1226
1227   {
1228     GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (de->builder, "uimanager1", GTK_TYPE_UI_MANAGER));
1229
1230     merge_help_menu (uim);
1231     
1232     PSPPIRE_WINDOW (de)->menu =
1233       GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
1234   }
1235
1236   {
1237     GtkWidget *data_sheet_cases_popup_menu = get_widget_assert (de->builder,
1238                                                                 "datasheet-cases-popup");
1239
1240     GtkWidget *var_sheet_variable_popup_menu = get_widget_assert (de->builder,
1241                                                                   "varsheet-variable-popup");
1242
1243     GtkWidget *data_sheet_variable_popup_menu = get_widget_assert (de->builder,
1244                                                                    "datasheet-variable-popup");
1245
1246     g_signal_connect_swapped (get_action_assert (de->builder, "sort-up"), "activate",
1247                               G_CALLBACK (psppire_data_editor_sort_ascending),
1248                               de->data_editor);
1249
1250     g_signal_connect_swapped (get_action_assert (de->builder, "sort-down"), "activate",
1251                               G_CALLBACK (psppire_data_editor_sort_descending),
1252                               de->data_editor);
1253
1254     g_object_set (de->data_editor,
1255                   "datasheet-column-menu", data_sheet_variable_popup_menu,
1256                   "datasheet-row-menu", data_sheet_cases_popup_menu,
1257                   "varsheet-row-menu", var_sheet_variable_popup_menu,
1258                   NULL);
1259   }
1260
1261   gtk_widget_show (GTK_WIDGET (de->data_editor));
1262   gtk_widget_show (box);
1263 }
1264
1265
1266 GtkWidget*
1267 psppire_data_window_new (void)
1268 {
1269   return GTK_WIDGET (g_object_new (psppire_data_window_get_type (),
1270                                    "description", _("Data Editor"),
1271                                    NULL));
1272 }
1273
1274
1275 static void
1276 psppire_data_window_iface_init (PsppireWindowIface *iface)
1277 {
1278   iface->save = save_file;
1279   iface->load = load_file;
1280 }
1281