INPUT PROGRAM: Use a separate dataset for the input program.
[pspp] / src / ui / gui / psppire-data-window.c
index 3d9774579adead051376234fe0d4d5b63d426954..d9488b354990c6b4135a7db9cbe7d75a0de9402a 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2008, 2009, 2010, 2011, 2012  Free Software Foundation
+   Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #include "libpspp/str.h"
 #include "ui/gui/aggregate-dialog.h"
 #include "ui/gui/autorecode-dialog.h"
-#include "ui/gui/binomial-dialog.h"
 #include "ui/gui/builder-wrapper.h"
 #include "ui/gui/chi-square-dialog.h"
 #include "ui/gui/comments-dialog.h"
 #include "ui/gui/compute-dialog.h"
 #include "ui/gui/count-dialog.h"
-#include "ui/gui/crosstabs-dialog.h"
 #include "ui/gui/entry-dialog.h"
 #include "ui/gui/executor.h"
-#include "ui/gui/frequencies-dialog.h"
 #include "ui/gui/help-menu.h"
 #include "ui/gui/helper.h"
 #include "ui/gui/helper.h"
@@ -78,6 +75,7 @@ static void psppire_data_window_init          (PsppireDataWindow      *data_edit
 static void psppire_data_window_iface_init (PsppireWindowIface *iface);
 
 static void psppire_data_window_dispose (GObject *object);
+static void psppire_data_window_finalize (GObject *object);
 static void psppire_data_window_set_property (GObject         *object,
                                               guint            prop_id,
                                               const GValue    *value,
@@ -145,6 +143,7 @@ psppire_data_window_class_init (PsppireDataWindowClass *class)
   parent_class = g_type_class_peek_parent (class);
 
   object_class->dispose = psppire_data_window_dispose;
+  object_class->finalize = psppire_data_window_finalize;
   object_class->set_property = psppire_data_window_set_property;
   object_class->get_property = psppire_data_window_get_property;
 
@@ -337,34 +336,46 @@ name_has_suffix (const gchar *name)
 }
 
 static gboolean
-load_file (PsppireWindow *de, const gchar *file_name)
+load_file (PsppireWindow *de, const gchar *file_name, gpointer syn)
 {
-  struct string filename;
-  gchar *utf8_file_name;
-  const char *mime_type;
-  gchar *syntax;
+  const char *mime_type = NULL;
+  gchar *syntax = NULL;
   bool ok;
 
-  ds_init_empty (&filename);
-
-  utf8_file_name = g_filename_to_utf8 (file_name, -1, NULL, NULL, NULL);
-
-  syntax_gen_string (&filename, ss_cstr (utf8_file_name));
-
-  g_free (utf8_file_name);
+  if (syn == NULL)
+    {
+      gchar *utf8_file_name;
+      struct string filename;
+      ds_init_empty (&filename);
+      
+      utf8_file_name = g_filename_to_utf8 (file_name, -1, NULL, NULL, NULL);
+    
+      syntax_gen_string (&filename, ss_cstr (utf8_file_name));
+      
+      g_free (utf8_file_name);
+      
+      syntax = g_strdup_printf ("GET FILE=%s.", ds_cstr (&filename));
+      ds_destroy (&filename);
 
-  syntax = g_strdup_printf ("GET FILE=%s.", ds_cstr (&filename));
-  ds_destroy (&filename);
+    }
+  else
+    {
+      syntax = syn;
+    }
 
   ok = execute_syntax (PSPPIRE_DATA_WINDOW (de),
                        lex_reader_for_string (syntax));
   g_free (syntax);
 
-  mime_type = (name_has_por_suffix (file_name)
-               ? "application/x-spss-por"
-               : "application/x-spss-sav");
-
-  add_most_recent (file_name, mime_type);
+  if (ok && syn == NULL)
+    {
+      if (name_has_por_suffix (file_name))
+       mime_type = "application/x-spss-por";
+      else if (name_has_sav_suffix (file_name))
+       mime_type = "application/x-spss-sav";
+      
+      add_most_recent (file_name, mime_type);
+    }
 
   return ok;
 }
@@ -702,7 +713,7 @@ on_recent_data_select (GtkMenuShell *menushell,
 
   g_free (uri);
 
-  open_data_window (window, file);
+  open_data_window (window, file, NULL);
 
   g_free (file);
 }
@@ -765,7 +776,7 @@ on_recent_files_select (GtkMenuShell *menushell,   gpointer user_data)
 
   free (encoding);
 
-  if ( psppire_window_load (PSPPIRE_WINDOW (se), file) ) 
+  if ( psppire_window_load (PSPPIRE_WINDOW (se), file, NULL) ) 
     gtk_widget_show (se);
   else
     gtk_widget_destroy (se);
@@ -780,7 +791,7 @@ set_unsaved (gpointer w)
 }
 
 static void
-on_switch_page (PsppireDataEditor *de, GtkNotebookPage *p,
+on_switch_page (PsppireDataEditor *de, gpointer p,
                gint pagenum, PsppireDataWindow *dw)
 {
   GtkWidget *page_menu_item;
@@ -827,12 +838,24 @@ connect_action (PsppireDataWindow *dw, const char *action_name,
                                    GCallback handler)
 {
   GtkAction *action = get_action_assert (dw->builder, action_name);
+
   g_signal_connect_swapped (action, "activate", handler, dw);
 
   return action;
 }
 
+/* Only a data file with at least one variable can be saved. */
+static void
+enable_save (PsppireDataWindow *dw)
+{
+  gboolean enable = psppire_dict_get_var_cnt (dw->dict) > 0;
+
+  gtk_action_set_sensitive (get_action_assert (dw->builder, "file_save"),
+                            enable);
+  gtk_action_set_sensitive (get_action_assert (dw->builder, "file_save_as"),
+                            enable);
+}
+
 /* Initializes as much of a PsppireDataWindow as we can and must before the
    dataset has been set.
 
@@ -919,10 +942,17 @@ psppire_data_window_finish_init (PsppireDataWindow *de,
                    G_CALLBACK (on_split_change),
                    de);
 
+  g_signal_connect_swapped (de->dict, "backend-changed",
+                            G_CALLBACK (enable_save), de);
+  g_signal_connect_swapped (de->dict, "variable-inserted",
+                            G_CALLBACK (enable_save), de);
+  g_signal_connect_swapped (de->dict, "variable-deleted",
+                            G_CALLBACK (enable_save), de);
+  enable_save (de);
 
   connect_action (de, "file_new_data", G_CALLBACK (create_data_window));
 
-  connect_action (de, "file_import-text", G_CALLBACK (text_data_import_assistant));
+  connect_action (de, "file_import", G_CALLBACK (text_data_import_assistant));
 
   connect_action (de, "file_save", G_CALLBACK (psppire_window_save));
  
@@ -952,11 +982,8 @@ psppire_data_window_finish_init (PsppireDataWindow *de,
   connect_action (de, "transform_count", G_CALLBACK (count_dialog));
   connect_action (de, "transform_recode-same", G_CALLBACK (recode_same_dialog));
   connect_action (de, "transform_recode-different", G_CALLBACK (recode_different_dialog));
-  connect_action (de, "analyze_frequencies", G_CALLBACK (frequencies_dialog));
-  connect_action (de, "crosstabs", G_CALLBACK (crosstabs_dialog));
   connect_action (de, "univariate", G_CALLBACK (univariate_dialog));
   connect_action (de, "chi-square", G_CALLBACK (chisquare_dialog));
-  connect_action (de, "binomial", G_CALLBACK (binomial_dialog));
   connect_action (de, "runs", G_CALLBACK (runs_dialog));
   connect_action (de, "ks-one-sample", G_CALLBACK (ks_one_sample_dialog));
   connect_action (de, "k-related-samples", G_CALLBACK (k_related_dialog));
@@ -1053,6 +1080,13 @@ psppire_data_window_dispose (GObject *object)
 {
   PsppireDataWindow *dw = PSPPIRE_DATA_WINDOW (object);
 
+  if (dw->uim)
+    {
+      psppire_data_window_remove_ui (dw, dw->uim, dw->merge_id);
+      g_object_unref (dw->uim);
+      dw->uim = NULL;
+    }
+
   if (dw->builder != NULL)
     {
       g_object_unref (dw->builder);
@@ -1081,6 +1115,27 @@ psppire_data_window_dispose (GObject *object)
     G_OBJECT_CLASS (parent_class)->dispose (object);
 }
 
+static void
+psppire_data_window_finalize (GObject *object)
+{
+  PsppireDataWindow *dw = PSPPIRE_DATA_WINDOW (object);
+
+  if (dw->dataset)
+    {
+      struct dataset *dataset = dw->dataset;
+      struct session *session = dataset_session (dataset);
+
+      dw->dataset = NULL;
+
+      dataset_set_callbacks (dataset, NULL, NULL);
+      session_set_active_dataset (session, NULL);
+      dataset_destroy (dataset);
+    }
+
+  if (G_OBJECT_CLASS (parent_class)->finalize)
+    G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
 static void
 psppire_data_window_set_property (GObject         *object,
                                   guint            prop_id,
@@ -1176,12 +1231,6 @@ psppire_data_window_remove_ui (PsppireDataWindow *pdw,
 
   gtk_window_remove_accel_group (GTK_WINDOW (pdw),
                                  gtk_ui_manager_get_accel_group (uim));
-
-  /* Our caller unrefs 'uim', possibly causing 'uim' to be freed.  The
-     following call appears to be necessary to ensure that pdw->ui_manager
-     drops all references to 'uim'.  Otherwise, I get valgrind complaints about
-     access to freed memory (and segfaults) on e.g. Windows|Split View.  */
-  gtk_ui_manager_ensure_update (pdw->ui_manager);
 }
 
 GtkWidget*
@@ -1190,14 +1239,11 @@ psppire_data_window_new (struct dataset *ds)
   GtkWidget *dw;
 
   if (the_session == NULL)
-    the_session = session_create ();
+    the_session = session_create (NULL);
 
   if (ds == NULL)
     {
-      static int n_datasets;
-      char *dataset_name;
-
-      dataset_name = xasprintf ("DataSet%d", ++n_datasets);
+      char *dataset_name = session_generate_dataset_name (the_session);
       ds = dataset_create (the_session, dataset_name);
       free (dataset_name);
     }
@@ -1283,7 +1329,7 @@ create_data_window (void)
 }
 
 void
-open_data_window (PsppireWindow *victim, const char *file_name)
+open_data_window (PsppireWindow *victim, const char *file_name, gpointer hint)
 {
   GtkWidget *window;
 
@@ -1296,7 +1342,7 @@ open_data_window (PsppireWindow *victim, const char *file_name)
   else
     window = psppire_data_window_new (NULL);
 
-  psppire_window_load (PSPPIRE_WINDOW (window), file_name);
+  psppire_window_load (PSPPIRE_WINDOW (window), file_name, hint);
   gtk_widget_show_all (window);
 }