X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Fui%2Fgui%2Fpsppire-window.c;h=7d478a642bfa38d87d807c5a2e3aac490f07f9e6;hb=6b6718a372daeaa66d69e126f9ee4a9bf46bf059;hp=3da046abdb98c8e54bda7edd3866e4c0af27d136;hpb=f130490a5cf37625ee3b4d16356ba59725ca40b7;p=pspp
diff --git a/src/ui/gui/psppire-window.c b/src/ui/gui/psppire-window.c
index 3da046abdb..7d478a642b 100644
--- a/src/ui/gui/psppire-window.c
+++ b/src/ui/gui/psppire-window.c
@@ -1,5 +1,5 @@
/* PSPPIRE - a graphical user interface for PSPP.
- Copyright (C) 2008 Free Software Foundation
+ Copyright (C) 2009, 2010, 2011, 2013, 2014, 2020 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
@@ -15,10 +15,12 @@
along with this program. If not, see . */
#include
+#include
-#include
-#include
-#include
+#include "psppire-window.h"
+#include "psppire-window-base.h"
+
+#include
#include
@@ -26,16 +28,27 @@
#define _(msgid) gettext (msgid)
#define N_(msgid) msgid
-#include "psppire-window.h"
+#include "data/any-reader.h"
+#include "data/file-handle-def.h"
+#include "data/dataset.h"
+#include "libpspp/version.h"
+#include "output/group-item.h"
+#include "output/pivot-table.h"
+#include "output/spv/spv.h"
+#include "output/spv/spv-output.h"
+#include "output/spv/spv-select.h"
+
+#include "helper.h"
+#include "psppire-data-window.h"
+#include "psppire-encoding-selector.h"
+#include "psppire-syntax-window.h"
#include "psppire-window-register.h"
-static void psppire_window_base_finalize (PsppireWindowClass *, gpointer);
static void psppire_window_base_init (PsppireWindowClass *class);
static void psppire_window_class_init (PsppireWindowClass *class);
static void psppire_window_init (PsppireWindow *window);
-static PsppireWindowClass *the_class;
static GObjectClass *parent_class;
GType
@@ -49,7 +62,7 @@ psppire_window_get_type (void)
{
sizeof (PsppireWindowClass),
(GBaseInitFunc) psppire_window_base_init,
- (GBaseFinalizeFunc) psppire_window_base_finalize,
+ (GBaseFinalizeFunc) NULL,
(GClassInitFunc) psppire_window_class_init,
(GClassFinalizeFunc) NULL,
NULL,
@@ -59,8 +72,8 @@ psppire_window_get_type (void)
};
psppire_window_type =
- g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow",
- &psppire_window_info, 0);
+ g_type_register_static (PSPPIRE_TYPE_WINDOW_BASE, "PsppireWindow",
+ &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
}
return psppire_window_type;
@@ -72,17 +85,96 @@ enum
{
PROP_0,
PROP_FILENAME,
- PROP_USAGE
+ PROP_DESCRIPTION,
+ PROP_ID
};
-gchar *
-uniquify (const gchar *str, int *x)
+static void
+psppire_window_set_title (PsppireWindow *window)
{
- return g_strdup_printf ("%s%d", str, (*x)++);
+ GString *title = g_string_sized_new (80);
+
+ if (window->edited != NULL)
+ g_string_append_c (title, '*');
+
+ if (window->basename || window->id)
+ {
+ if (window->basename)
+ g_string_append_printf (title, "%s ", window->basename);
+
+ if (window->id)
+ g_string_append_printf (title, "[%s] ", window->id);
+
+ g_string_append_unichar (title, 0x2014); /* em dash */
+ g_string_append_c (title, ' '); /* em dash */
+ }
+
+ g_string_append_printf (title, "PSPPIRE %s", window->description);
+
+ int minor = 1;
+ sscanf (bare_version, "%*d.%d.%*d", &minor);
+ if (minor % 2)
+ g_string_append_printf (title, " - Test version! Please report bugs to %s", PACKAGE_BUGREPORT);
+
+ gtk_window_set_title (GTK_WINDOW (window), title->str);
+
+ g_string_free (title, TRUE);
}
+static void
+psppire_window_update_list_name (PsppireWindow *window)
+{
+ PsppireWindowRegister *reg = psppire_window_register_new ();
+ GString *candidate = g_string_sized_new (80);
+ int n;
+
+ n = 1;
+ do
+ {
+ /* Compose a name. */
+ g_string_truncate (candidate, 0);
+ if (window->filename)
+ {
+ gchar *display_filename = g_filename_display_name (window->filename);
+ g_string_append (candidate, display_filename);
+ g_free (display_filename);
+
+ if (window->id)
+ g_string_append_printf (candidate, " [%s]", window->id);
+ }
+ else if (window->id)
+ g_string_append_printf (candidate, "[%s]", window->id);
+ else
+ g_string_append (candidate, window->description);
+
+ if (n++ > 1)
+ g_string_append_printf (candidate, " #%d", n);
+
+ if (window->list_name && !strcmp (candidate->str, window->list_name))
+ {
+ /* Keep the existing name. */
+ g_string_free (candidate, TRUE);
+ return;
+ }
+ }
+ while (psppire_window_register_lookup (reg, candidate->str));
+
+ if (window->list_name)
+ psppire_window_register_remove (reg, window->list_name);
+ g_free (window->list_name);
+ window->list_name = g_string_free (candidate, FALSE);
+
+ psppire_window_register_insert (reg, window, window->list_name);
+}
+
+static void
+psppire_window_name_changed (PsppireWindow *window)
+{
+ psppire_window_set_title (window);
+ psppire_window_update_list_name (window);
+}
static void
psppire_window_set_property (GObject *object,
@@ -94,59 +186,24 @@ psppire_window_set_property (GObject *object,
switch (prop_id)
{
- case PROP_USAGE:
- window->usage = g_value_get_enum (value);
+ case PROP_DESCRIPTION:
+ g_free (window->description);
+ window->description = g_value_dup_string (value);
+ psppire_window_set_title (window);
break;
case PROP_FILENAME:
- {
- gchar mdash[6] = {0,0,0,0,0,0};
- gchar *basename, *title;
- const gchar *name = g_value_get_string (value);
- gchar *candidate_name = strdup (name);
- int x = 0;
-
- PsppireWindowRegister *reg = psppire_window_register_new ();
-
- while ( psppire_window_register_lookup (reg, candidate_name))
- {
- free (candidate_name);
- candidate_name = uniquify (name, &x);
- }
-
- basename = g_path_get_basename (candidate_name);
- g_unichar_to_utf8 (0x2014, mdash);
-
- switch (window->usage)
- {
- case PSPPIRE_WINDOW_USAGE_SYNTAX:
- title = g_strdup_printf ( _("%s %s PSPPIRE Syntax Editor"),
- basename, mdash);
- break;
- case PSPPIRE_WINDOW_USAGE_OUTPUT:
- title = g_strdup_printf ( _("%s %s PSPPIRE Output"),
- basename, mdash);
- case PSPPIRE_WINDOW_USAGE_DATA:
- title = g_strdup_printf ( _("%s %s PSPPIRE Data Editor"),
- basename, mdash);
- break;
- default:
- g_assert_not_reached ();
- break;
- }
-
- gtk_window_set_title (GTK_WINDOW (window), title);
-
- if ( window->name)
- psppire_window_register_remove (reg, window->name);
-
- free (window->name);
- window->name = candidate_name;
-
- psppire_window_register_insert (reg, window, window->name);
-
- free (basename);
- free (title);
- }
+ g_free (window->filename);
+ window->filename = g_value_dup_string (value);
+ g_free (window->basename);
+ window->basename = (window->filename
+ ? g_filename_display_basename (window->filename)
+ : NULL);
+ psppire_window_name_changed (window);
+ break;
+ case PROP_ID:
+ g_free (window->id);
+ window->id = g_value_dup_string (value);
+ psppire_window_name_changed (window);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -165,11 +222,14 @@ psppire_window_get_property (GObject *object,
switch (prop_id)
{
- case PROP_USAGE:
- g_value_set_enum (value, window->usage);
- break;
case PROP_FILENAME:
- g_value_set_string (value, window->name);
+ g_value_set_string (value, window->filename);
+ break;
+ case PROP_DESCRIPTION:
+ g_value_set_string (value, window->description);
+ break;
+ case PROP_ID:
+ g_value_set_string (value, window->id);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -178,22 +238,24 @@ psppire_window_get_property (GObject *object,
}
-
static void
psppire_window_finalize (GObject *object)
{
PsppireWindow *window = PSPPIRE_WINDOW (object);
-
- PsppireWindowRegister *reg = psppire_window_register_new ();
- psppire_window_register_remove (reg, window->name);
- free (window->name);
+ PsppireWindowRegister *reg = psppire_window_register_new ();
- g_signal_handler_disconnect (psppire_window_register_new (),
- window->remove_handler);
+ if (window->edited)
+ g_date_time_unref (window->edited);
- g_signal_handler_disconnect (psppire_window_register_new (),
- window->insert_handler);
+ g_signal_handler_disconnect (reg, window->remove_handler);
+ g_signal_handler_disconnect (reg, window->insert_handler);
+ psppire_window_register_remove (reg, window->list_name);
+ g_free (window->filename);
+ g_free (window->basename);
+ g_free (window->id);
+ g_free (window->description);
+ g_free (window->list_name);
g_hash_table_destroy (window->menuitem_table);
@@ -201,43 +263,48 @@ psppire_window_finalize (GObject *object)
G_OBJECT_CLASS (parent_class)->finalize (object);
}
-
static void
psppire_window_class_init (PsppireWindowClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
- GParamSpec *use_class_spec =
- g_param_spec_enum ("usage",
- "Usage",
- "What the window is used for",
- G_TYPE_PSPPIRE_WINDOW_USAGE,
- PSPPIRE_WINDOW_USAGE_SYNTAX /* default value */,
- G_PARAM_CONSTRUCT_ONLY |G_PARAM_READABLE | G_PARAM_WRITABLE);
-
+ GParamSpec *description_spec =
+ null_if_empty_param ("description",
+ "Description",
+ "A string describing the usage of the window",
+ NULL, /*Should be overridden by derived classes */
+ G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
GParamSpec *filename_spec =
- g_param_spec_string ("filename",
+ null_if_empty_param ("filename",
"File name",
"The name of the file associated with this window, if any",
- "Untitled",
- G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT
- );
+ NULL,
+ G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
+ GParamSpec *id_spec =
+ null_if_empty_param ("id",
+ "Identifier",
+ "The PSPP language identifier for the data associated "
+ "with this window (e.g. dataset name)",
+ NULL,
+ G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
object_class->set_property = psppire_window_set_property;
object_class->get_property = psppire_window_get_property;
+ g_object_class_install_property (object_class,
+ PROP_DESCRIPTION,
+ description_spec);
+
g_object_class_install_property (object_class,
PROP_FILENAME,
filename_spec);
g_object_class_install_property (object_class,
- PROP_USAGE,
- use_class_spec);
+ PROP_ID,
+ id_spec);
-
- the_class = class;
parent_class = g_type_class_peek_parent (class);
}
@@ -252,23 +319,14 @@ psppire_window_base_init (PsppireWindowClass *class)
-static void
-psppire_window_base_finalize (PsppireWindowClass *class,
- gpointer class_data)
-{
-}
-
static void
insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
{
- GtkWidget *item = gtk_check_menu_item_new_with_label (key);
-
- gtk_widget_show (item);
-
- gtk_menu_shell_append (window->menu, item);
-
- gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
- (psppire_window_register_lookup (psppire_window_register_new (), key) == window));
+ gchar *filename;
+ GtkWidget *item;
+ filename = g_filename_display_name (key);
+ item = gtk_check_menu_item_new_with_label (filename);
+ g_free (filename);
g_hash_table_insert (window->menuitem_table, key, item);
}
@@ -278,7 +336,7 @@ insert_item (gpointer key, gpointer value, gpointer data)
{
PsppireWindow *window = PSPPIRE_WINDOW (data);
- if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
+ if (NULL != g_hash_table_lookup (window->menuitem_table, key))
return;
insert_menuitem_into_menu (window, key);
@@ -289,7 +347,7 @@ static void
insert_menuitem (GObject *reg, const gchar *key, gpointer data)
{
PsppireWindow *window = PSPPIRE_WINDOW (data);
-
+
insert_menuitem_into_menu (window, (gpointer) key);
}
@@ -298,16 +356,7 @@ static void
remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
{
PsppireWindow *window = PSPPIRE_WINDOW (data);
- GtkWidget *item ;
-
- if ( !GTK_WIDGET_REALIZED (window))
- return;
-
- item = g_hash_table_lookup (window->menuitem_table, key);
-
g_hash_table_remove (window->menuitem_table, key);
-
- gtk_container_remove (GTK_CONTAINER (window->menu), item);
}
static void
@@ -316,11 +365,51 @@ insert_existing_items (PsppireWindow *window)
psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
}
+
+static gboolean
+on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
+{
+ PsppireWindowRegister *reg = psppire_window_register_new ();
+
+ if (w->edited != NULL)
+ {
+ gint response = psppire_window_query_save (w);
+
+ switch (response)
+ {
+ default:
+ case GTK_RESPONSE_CANCEL:
+ return TRUE;
+ break;
+ case GTK_RESPONSE_APPLY:
+ psppire_window_save (w);
+ if (w->edited != NULL)
+ {
+ /* Save failed, or user exited Save As dialog with Cancel. */
+ return TRUE;
+ }
+ break;
+ case GTK_RESPONSE_REJECT:
+ break;
+ }
+ }
+
+ if (1 == psppire_window_register_n_items (reg))
+ gtk_main_quit ();
+
+ return FALSE;
+}
+
+
static void
psppire_window_init (PsppireWindow *window)
{
- window->name = NULL;
- window->menu = NULL;
+ window->filename = NULL;
+ window->basename = NULL;
+ window->id = NULL;
+ window->description = NULL;
+ window->list_name = NULL;
+ window->edited = NULL;
window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
@@ -336,75 +425,520 @@ psppire_window_init (PsppireWindow *window)
"removed",
G_CALLBACK (remove_menuitem),
window);
-}
+ window->added_separator = FALSE;
+
+ g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
-GtkWidget*
-psppire_window_new (PsppireWindowUsage usage)
+ g_object_set (window, "icon-name", "pspp", NULL);
+}
+
+/*
+ Ask the user if the buffer should be saved.
+ Return the response.
+*/
+gint
+psppire_window_query_save (PsppireWindow *se)
{
- return GTK_WIDGET (g_object_new (psppire_window_get_type (),
- "type", GTK_WINDOW_TOPLEVEL,
- "usage", usage,
- NULL));
+ gint response;
+ GtkWidget *dialog;
+ GtkWidget *cancel_button;
+
+ gchar *description;
+
+ GDateTime *now = g_date_time_new_now_utc ();
+ GTimeSpan timespan = g_date_time_difference (now, se->edited);
+ g_date_time_unref (now);
+
+ if (se->filename)
+ description = g_filename_display_basename (se->filename);
+ else if (se->id)
+ description = g_strdup (se->id);
+ else
+ description = g_strdup (se->description);
+ dialog =
+ gtk_message_dialog_new (GTK_WINDOW (se),
+ GTK_DIALOG_MODAL,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_NONE,
+ _("Save the changes to `%s' before closing?"),
+ description);
+ g_free (description);
+
+ g_object_set (dialog, "icon-name", "pspp", NULL);
+
+ gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+ _("If you don't save, changes from the last %ld seconds will be permanently lost."),
+ timespan / G_TIME_SPAN_SECOND);
+
+ gtk_dialog_add_button (GTK_DIALOG (dialog),
+ _("Close _without saving"),
+ GTK_RESPONSE_REJECT);
+
+ cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
+ _("Cancel"),
+ GTK_RESPONSE_CANCEL);
+
+ gtk_dialog_add_button (GTK_DIALOG (dialog),
+ _("Save"),
+ GTK_RESPONSE_APPLY);
+
+ gtk_widget_grab_focus (cancel_button);
+
+ response = gtk_dialog_run (GTK_DIALOG (dialog));
+
+ gtk_widget_destroy (dialog);
+
+ return response;
}
+/* The return value is encoded in the glib filename encoding. */
const gchar *
psppire_window_get_filename (PsppireWindow *w)
{
- const gchar *name = NULL;
- g_object_get (w, "filename", name, NULL);
- return name;
+ return w->filename;
}
+/* FILENAME must be encoded in the glib filename encoding. */
void
psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
{
g_object_set (w, "filename", filename, NULL);
}
+void
+psppire_window_set_unsaved (PsppireWindow *w)
+{
+ if (w->edited == NULL)
+ w->edited = g_date_time_new_now_utc ();
+
+ psppire_window_set_title (w);
+}
+
+gboolean
+psppire_window_get_unsaved (PsppireWindow *w)
+{
+ return w->edited != NULL;
+}
+
+
+
+
+
+static void
+minimise_window (gpointer key, gpointer value, gpointer data)
+{
+ gtk_window_iconify (GTK_WINDOW (value));
+}
+
+
+void
+psppire_window_minimise_all (void)
+{
+ PsppireWindowRegister *reg = psppire_window_register_new ();
+
+ g_hash_table_foreach (reg->name_table, minimise_window, NULL);
+}
+
+
GType
-psppire_window_usage_get_type (void)
+psppire_window_model_get_type (void)
{
- static GType etype = 0;
- if (etype == 0)
+ static GType window_model_type = 0;
+
+ if (! window_model_type)
{
- static const GEnumValue values[] = {
- { PSPPIRE_WINDOW_USAGE_SYNTAX, "PSPPIRE_WINDOW_USAGE_SYNTAX",
- "Syntax" },
+ static const GTypeInfo window_model_info =
+ {
+ sizeof (PsppireWindowIface), /* class_size */
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ NULL,
+ NULL, /* class_finalize */
+ NULL, /* class_data */
+ 0,
+ 0, /* n_preallocs */
+ NULL
+ };
- { PSPPIRE_WINDOW_USAGE_OUTPUT, "PSPPIRE_WINDOW_USAGE_OUTPUT",
- "Output" },
+ window_model_type =
+ g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
+ &window_model_info, 0);
- { PSPPIRE_WINDOW_USAGE_DATA, "PSPPIRE_WINDOW_USAGE_DATA",
- "Data" },
+ g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
+ }
- { 0, NULL, NULL }
- };
+ return window_model_type;
+}
+
+
+void
+psppire_window_save (PsppireWindow *w)
+{
+ PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
- etype = g_enum_register_static (g_intern_static_string ("PsppireWindowUsage"),
- values);
+ g_assert (i);
+ g_return_if_fail (i->save);
+
+ if (w->filename == NULL)
+ psppire_window_save_as (w);
+ else
+ {
+ i->save (w);
+ if (w->edited)
+ g_date_time_unref (w->edited);
+ w->edited = NULL;
+
+ psppire_window_set_title (w);
}
+}
+
+void
+psppire_window_save_as (PsppireWindow *w)
+{
+ PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
+ gchar *old_filename;
- return etype;
+ g_assert (i);
+ g_return_if_fail (i->pick_filename);
+
+ old_filename = w->filename;
+ w->filename = NULL;
+
+ i->pick_filename (w);
+ if (w->filename == NULL)
+ w->filename = old_filename;
+ else
+ {
+ g_free (old_filename);
+ psppire_window_save (w);
+ }
}
+static void delete_recent (const char *file_name);
+
+gboolean
+psppire_window_load (PsppireWindow *w, const gchar *file,
+ const gchar *encoding, gpointer hint)
+{
+ gboolean ok;
+ PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
+ g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
+
+ g_assert (i);
+
+ g_return_val_if_fail (i->load, FALSE);
+
+ ok = i->load (w, file, encoding, hint);
+
+ if (ok)
+ {
+ psppire_window_set_filename (w, file);
+ if (w->edited)
+ g_date_time_unref (w->edited);
+ w->edited = NULL;
+ }
+ else
+ delete_recent (file);
+
+ return ok;
+}
+
+
+GtkWidget *
+psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
+{
+ GtkFileFilter *filter = gtk_file_filter_new ();
+ GtkWidget *dialog =
+ gtk_file_chooser_dialog_new (_("Open"),
+ GTK_WINDOW (toplevel),
+ GTK_FILE_CHOOSER_ACTION_OPEN,
+ _("Cancel"), GTK_RESPONSE_CANCEL,
+ _("Open"), GTK_RESPONSE_ACCEPT,
+ NULL);
+
+ g_object_set (dialog, "local-only", FALSE, NULL);
+
+ gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
+ gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
+ gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
+ gtk_file_filter_add_mime_type (filter, "application/x-spss-spv");
+ gtk_file_filter_add_pattern (filter, "*.zsav");
+ gtk_file_filter_add_pattern (filter, "*.sps");
+ gtk_file_filter_add_pattern (filter, "*.SPS");
+ gtk_file_filter_add_pattern (filter, "*.spv");
+ gtk_file_filter_add_pattern (filter, "*.SPV");
+ gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
+
+ filter = gtk_file_filter_new ();
+ gtk_file_filter_set_name (filter, _("System Files (*.sav, *.zsav)"));
+ gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
+ gtk_file_filter_add_pattern (filter, "*.zsav");
+ gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
+
+ filter = gtk_file_filter_new ();
+ gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
+ gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
+ gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
+
+ filter = gtk_file_filter_new ();
+ gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
+ gtk_file_filter_add_pattern (filter, "*.sps");
+ gtk_file_filter_add_pattern (filter, "*.SPS");
+ gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
+
+ filter = gtk_file_filter_new ();
+ gtk_file_filter_set_name (filter, _("Output Files (*.spv) "));
+ gtk_file_filter_add_pattern (filter, "*.spv");
+ gtk_file_filter_add_pattern (filter, "*.SPV");
+ gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
+
+ filter = gtk_file_filter_new ();
+ gtk_file_filter_set_name (filter, _("All Files"));
+ gtk_file_filter_add_pattern (filter, "*");
+ gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
+
+ if (toplevel->filename)
+ {
+ const gchar *filename = toplevel->filename;
+ gchar *dir_name;
+
+ if (! g_path_is_absolute (filename))
+ {
+ gchar *path =
+ g_build_filename (g_get_current_dir (), filename, NULL);
+ dir_name = g_path_get_dirname (path);
+ g_free (path);
+ }
+ else
+ {
+ dir_name = g_path_get_dirname (filename);
+ }
+ gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
+ dir_name);
+ free (dir_name);
+ }
+
+ gtk_file_chooser_set_extra_widget (
+ GTK_FILE_CHOOSER (dialog),
+ psppire_encoding_selector_new ("Auto", true));
+
+ return dialog;
+}
+
+struct item_path
+ {
+ const struct spv_item **nodes;
+ size_t n;
+
+#define N_STUB 10
+ const struct spv_item *stub[N_STUB];
+ };
static void
-minimise_window (gpointer key, gpointer value, gpointer data)
+swap_nodes (const struct spv_item **a, const struct spv_item **b)
{
- gtk_window_iconify (GTK_WINDOW (value));
+ const struct spv_item *tmp = *a;
+ *a = *b;
+ *b = tmp;
+}
+
+static void
+get_path (const struct spv_item *item, struct item_path *path)
+{
+ size_t allocated = 10;
+ path->nodes = path->stub;
+ path->n = 0;
+
+ while (item)
+ {
+ if (path->n >= allocated)
+ {
+ if (path->nodes == path->stub)
+ path->nodes = xmemdup (path->stub, sizeof path->stub);
+ path->nodes = x2nrealloc (path->nodes, &allocated,
+ sizeof *path->nodes);
+ }
+ path->nodes[path->n++] = item;
+ item = item->parent;
+ }
+
+ for (size_t i = 0; i < path->n / 2; i++)
+ swap_nodes (&path->nodes[i], &path->nodes[path->n - i - 1]);
}
+static void
+free_path (struct item_path *path)
+{
+ if (path && path->nodes != path->stub)
+ free (path->nodes);
+}
+
+static void
+dump_heading_transition (const struct spv_item *old,
+ const struct spv_item *new)
+{
+ if (old == new)
+ return;
+
+ struct item_path old_path, new_path;
+ get_path (old, &old_path);
+ get_path (new, &new_path);
+
+ size_t common = 0;
+ for (; common < old_path.n && common < new_path.n; common++)
+ if (old_path.nodes[common] != new_path.nodes[common])
+ break;
+
+ for (size_t i = common; i < old_path.n; i++)
+ group_close_item_submit (group_close_item_create ());
+ for (size_t i = common; i < new_path.n; i++)
+ group_open_item_submit (group_open_item_create (
+ new_path.nodes[i]->command_id));
+
+ free_path (&old_path);
+ free_path (&new_path);
+}
void
-psppire_window_minimise_all (void)
+read_spv_file (const char *filename)
{
- PsppireWindowRegister *reg = psppire_window_register_new ();
+ struct spv_reader *spv;
+ char *error = spv_open (filename, &spv);
+ if (error)
+ {
+ /* XXX */
+ fprintf (stderr, "%s\n", error);
+ return;
+ }
- g_hash_table_foreach (reg->name_table, minimise_window, NULL);
+ struct spv_item **items;
+ size_t n_items;
+ spv_select (spv, NULL, 0, &items, &n_items);
+ struct spv_item *prev_heading = spv_get_root (spv);
+ for (size_t i = 0; i < n_items; i++)
+ {
+ struct spv_item *heading
+ = items[i]->type == SPV_ITEM_HEADING ? items[i] : items[i]->parent;
+ dump_heading_transition (prev_heading, heading);
+ if (items[i]->type == SPV_ITEM_TEXT)
+ spv_text_submit (items[i]);
+ else if (items[i]->type == SPV_ITEM_TABLE)
+ pivot_table_submit (spv_item_get_table (items[i]));
+ prev_heading = heading;
+ }
+ dump_heading_transition (prev_heading, spv_get_root (spv));
+ free (items);
+ spv_close (spv);
}
+
+/* Callback for the file_open action.
+ Prompts for a filename and opens it */
+void
+psppire_window_open (PsppireWindow *de)
+{
+ GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
+
+ gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), relocate (examples_dir), NULL);
+
+ switch (gtk_dialog_run (GTK_DIALOG (dialog)))
+ {
+ case GTK_RESPONSE_ACCEPT:
+ {
+ gchar *name =
+ gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+
+ const gchar **cs = NULL;
+ g_get_filename_charsets (&cs);
+
+ gchar *encoding = psppire_encoding_selector_get_encoding (
+ gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
+
+ struct file_handle *fh = fh_create_file (NULL, name, cs[0], fh_default_properties ());
+
+ int retval = any_reader_detect (fh, NULL);
+ if (retval == 1)
+ open_data_window (de, name, encoding, NULL);
+ else if (retval == 0)
+ {
+ char *error = spv_detect (name);
+ if (!error)
+ read_spv_file (name);
+ else
+ {
+ free (error);
+ open_syntax_window (name, encoding);
+ }
+ }
+
+ g_free (encoding);
+ fh_unref (fh);
+ g_free (name);
+ }
+ break;
+ default:
+ break;
+ }
+
+ gtk_widget_destroy (dialog);
+}
+
+
+/* Puts FILE_NAME (encoded in the glib file name encoding) into the recent list
+ with associated MIME_TYPE. If it's already in the list, it moves it to the
+ top. */
+void
+add_most_recent (const char *file_name,
+ const char *mime_type, const char *encoding)
+{
+ gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
+ if (uri)
+ {
+ GtkRecentData recent_data;
+ gchar *full_mime_type;
+
+ if (encoding && encoding[0])
+ full_mime_type = g_strdup_printf ("%s; charset=%s",
+ mime_type, encoding);
+ else
+ full_mime_type = g_strdup (mime_type);
+
+ recent_data.display_name = NULL;
+ recent_data.description = NULL;
+ recent_data.mime_type = full_mime_type;
+ recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
+ recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
+ recent_data.groups = NULL;
+ recent_data.is_private = FALSE;
+
+ gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
+ uri, &recent_data);
+
+ g_free (recent_data.app_exec);
+ g_free (full_mime_type);
+ }
+
+ g_free (uri);
+}
+
+
+
+/*
+ If FILE_NAME exists in the recent list, then delete it.
+ */
+static void
+delete_recent (const char *file_name)
+{
+ gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
+
+ if (uri)
+ gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);
+
+ g_free (uri);
+}
+