Yet more Gseal stuff
[pspp] / src / ui / gui / psppire-data-window.c
index 3d9774579adead051376234fe0d4d5b63d426954..90c1fb5f69b322585d68ed684cc470948ef1ab4f 100644 (file)
 #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;
 }
@@ -633,14 +644,10 @@ static void
 fonts_activate (PsppireDataWindow  *de)
 {
   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (de));
-  PangoFontDescription *current_font;
-  gchar *font_name;
-  GtkWidget *dialog =
-    gtk_font_selection_dialog_new (_("Font Selection"));
-
-
-  current_font = GTK_WIDGET(de->data_editor)->style->font_desc;
-  font_name = pango_font_description_to_string (current_font);
+  GtkWidget *dialog =  gtk_font_selection_dialog_new (_("Font Selection"));
+  GtkStyle *style = gtk_widget_get_style (GTK_WIDGET(de->data_editor));
+  PangoFontDescription *current_font = style->font_desc;
+  gchar *font_name = pango_font_description_to_string (current_font);
 
   gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (dialog), font_name);
 
@@ -702,7 +709,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 +772,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 +787,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 +834,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.
 
@@ -843,12 +862,14 @@ connect_action (PsppireDataWindow *dw, const char *action_name,
 static void
 psppire_data_window_init (PsppireDataWindow *de)
 {
+  GtkWidget *w ;
   de->builder = builder_new ("data-editor.ui");
 
   de->ui_manager = GTK_UI_MANAGER (get_object_assert (de->builder, "uimanager1", GTK_TYPE_UI_MANAGER));
 
-  PSPPIRE_WINDOW (de)->menu =
-    GTK_MENU_SHELL (gtk_ui_manager_get_widget (de->ui_manager, "/ui/menubar/windows/windows_minimise_all")->parent);
+  w = gtk_ui_manager_get_widget (de->ui_manager, "/ui/menubar/windows/windows_minimise_all");
+
+  PSPPIRE_WINDOW (de)->menu = GTK_MENU_SHELL (gtk_widget_get_parent (w));
 
   de->uim = NULL;
   de->merge_id = 0;
@@ -919,10 +940,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 +980,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 +1078,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 +1113,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 +1229,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*
@@ -1194,10 +1241,7 @@ psppire_data_window_new (struct dataset *ds)
 
   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 +1327,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 +1340,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);
 }