X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=inline;f=src%2Fui%2Fgui%2Fpsppire-window.c;h=ca81692305052e2596ddff40bebf4029bb7008ae;hb=5b2ed095a8aaa98adaea4922855edcbfb619c728;hp=dcc44a8d5520a1c9bf3669034fc80caeb6c6fc0a;hpb=e2da62d735c597afeef2e0e9b36e5a4a83d7da94;p=pspp
diff --git a/src/ui/gui/psppire-window.c b/src/ui/gui/psppire-window.c
index dcc44a8d55..ca81692305 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) 2009, 2010, 2011, 2013 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,6 +15,7 @@
along with this program. If not, see . */
#include
+#include
#include "psppire-window.h"
#include "psppire-window-base.h"
@@ -22,22 +23,26 @@
#include
#include
-#include
#include
#define _(msgid) gettext (msgid)
#define N_(msgid) msgid
#include "data/any-reader.h"
-#include "data/file-name.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"
-#include "psppire.h"
static void psppire_window_base_init (PsppireWindowClass *class);
static void psppire_window_class_init (PsppireWindowClass *class);
@@ -56,14 +61,15 @@ psppire_window_get_type (void)
static const GTypeInfo psppire_window_info =
{
sizeof (PsppireWindowClass),
- (GBaseInitFunc) psppire_window_base_init,
+ (GBaseInitFunc) (void (*)(void)) psppire_window_base_init,
(GBaseFinalizeFunc) NULL,
- (GClassInitFunc) psppire_window_class_init,
+ (GClassInitFunc) (void (*)(void)) psppire_window_class_init,
(GClassFinalizeFunc) NULL,
NULL,
sizeof (PsppireWindow),
0,
- (GInstanceInitFunc) psppire_window_init,
+ (GInstanceInitFunc) (void (*)(void)) psppire_window_init,
+ NULL /* value_table */
};
psppire_window_type =
@@ -90,7 +96,7 @@ psppire_window_set_title (PsppireWindow *window)
{
GString *title = g_string_sized_new (80);
- if (window->dirty)
+ if (window->edited != NULL)
g_string_append_c (title, '*');
if (window->basename || window->id)
@@ -98,7 +104,7 @@ psppire_window_set_title (PsppireWindow *window)
if (window->basename)
g_string_append_printf (title, "%s ", window->basename);
- if (window->id != '\0')
+ if (window->id)
g_string_append_printf (title, "[%s] ", window->id);
g_string_append_unichar (title, 0x2014); /* em dash */
@@ -107,6 +113,11 @@ psppire_window_set_title (PsppireWindow *window)
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);
@@ -190,7 +201,6 @@ psppire_window_set_property (GObject *object,
: NULL);
psppire_window_name_changed (window);
break;
- break;
case PROP_ID:
g_free (window->id);
window->id = g_value_dup_string (value);
@@ -236,6 +246,9 @@ psppire_window_finalize (GObject *object)
PsppireWindowRegister *reg = psppire_window_register_new ();
+ if (window->edited)
+ g_date_time_unref (window->edited);
+
g_signal_handler_disconnect (reg, window->remove_handler);
g_signal_handler_disconnect (reg, window->insert_handler);
psppire_window_register_remove (reg, window->list_name);
@@ -307,62 +320,16 @@ psppire_window_base_init (PsppireWindowClass *class)
-static void
-menu_toggled (GtkCheckMenuItem *mi, gpointer data)
-{
-#if GTK3_TRANSITION
- /* Prohibit changes to the state */
- mi->active = !mi->active;
-#endif
-}
-
-
-/* Look up the window associated with this menuitem and present it to the user */
-static void
-menu_activate (GtkMenuItem *mi, gpointer data)
-{
- const gchar *key = data;
-
- PsppireWindowRegister *reg = psppire_window_register_new ();
-
- PsppireWindow *window = psppire_window_register_lookup (reg, key);
-
- gtk_window_present (GTK_WINDOW (window));
-}
-
static void
insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
{
gchar *filename;
GtkWidget *item;
-
- /* Add a separator before adding the first real item. If we add a separator
- at any other time, sometimes GtkUIManager removes it. */
- if (!window->added_separator)
- {
- GtkWidget *separator = gtk_separator_menu_item_new ();
- gtk_widget_show (separator);
- gtk_menu_shell_append (window->menu, separator);
- window->added_separator = TRUE;
- }
-
filename = g_filename_display_name (key);
item = gtk_check_menu_item_new_with_label (filename);
+ g_object_ref_sink (item);
g_free (filename);
- g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
- g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
-
- gtk_widget_show (item);
-
- gtk_menu_shell_append (window->menu, item);
-
-#if GTK3_TRANSITION
- /* Set the state without emitting a signal */
- GTK_CHECK_MENU_ITEM (item)->active =
- (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
-#endif
-
g_hash_table_insert (window->menuitem_table, key, item);
}
@@ -371,7 +338,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);
@@ -382,7 +349,7 @@ static void
insert_menuitem (GObject *reg, const gchar *key, gpointer data)
{
PsppireWindow *window = PSPPIRE_WINDOW (data);
-
+
insert_menuitem_into_menu (window, (gpointer) key);
}
@@ -391,14 +358,7 @@ static void
remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
{
PsppireWindow *window = PSPPIRE_WINDOW (data);
- GtkWidget *item ;
-
- item = g_hash_table_lookup (window->menuitem_table, key);
-
g_hash_table_remove (window->menuitem_table, key);
-
- if (GTK_IS_CONTAINER (window->menu))
- gtk_container_remove (GTK_CONTAINER (window->menu), item);
}
static void
@@ -413,7 +373,7 @@ on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
{
PsppireWindowRegister *reg = psppire_window_register_new ();
- if ( w->dirty )
+ if (w->edited != NULL)
{
gint response = psppire_window_query_save (w);
@@ -425,7 +385,7 @@ on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
break;
case GTK_RESPONSE_APPLY:
psppire_window_save (w);
- if (w->dirty)
+ if (w->edited != NULL)
{
/* Save failed, or user exited Save As dialog with Cancel. */
return TRUE;
@@ -436,7 +396,7 @@ on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
}
}
- if ( 1 == psppire_window_register_n_items (reg))
+ if (1 == psppire_window_register_n_items (reg))
gtk_main_quit ();
return FALSE;
@@ -446,30 +406,31 @@ on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
static void
psppire_window_init (PsppireWindow *window)
{
- 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);
+ window->menuitem_table = g_hash_table_new_full (g_str_hash, g_str_equal,
+ NULL, g_object_unref);
g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
- window->insert_handler = g_signal_connect (psppire_window_register_new (),
+ PsppireWindowRegister *reg = psppire_window_register_new ();
+ window->insert_handler = g_signal_connect (reg,
"inserted",
G_CALLBACK (insert_menuitem),
window);
- window->remove_handler = g_signal_connect (psppire_window_register_new (),
+ window->remove_handler = g_signal_connect (reg,
"removed",
G_CALLBACK (remove_menuitem),
window);
window->added_separator = FALSE;
- window->dirty = FALSE;
g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
@@ -489,9 +450,9 @@ psppire_window_query_save (PsppireWindow *se)
gchar *description;
- GTimeVal time;
-
- g_get_current_time (&time);
+ 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);
@@ -512,18 +473,18 @@ psppire_window_query_save (PsppireWindow *se)
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."),
- time.tv_sec - se->savetime.tv_sec);
+ 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),
- GTK_STOCK_CANCEL,
+ _("Cancel"),
GTK_RESPONSE_CANCEL);
gtk_dialog_add_button (GTK_DIALOG (dialog),
- GTK_STOCK_SAVE,
+ _("Save"),
GTK_RESPONSE_APPLY);
gtk_widget_grab_focus (cancel_button);
@@ -554,10 +515,8 @@ psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
void
psppire_window_set_unsaved (PsppireWindow *w)
{
- if ( w->dirty == FALSE)
- g_get_current_time (&w->savetime);
-
- w->dirty = TRUE;
+ if (w->edited == NULL)
+ w->edited = g_date_time_new_now_utc ();
psppire_window_set_title (w);
}
@@ -565,7 +524,7 @@ psppire_window_set_unsaved (PsppireWindow *w)
gboolean
psppire_window_get_unsaved (PsppireWindow *w)
{
- return w->dirty;
+ return w->edited != NULL;
}
@@ -607,7 +566,8 @@ psppire_window_model_get_type (void)
NULL, /* class_data */
0,
0, /* n_preallocs */
- NULL
+ NULL,
+ NULL /* value_table */
};
window_model_type =
@@ -634,7 +594,10 @@ psppire_window_save (PsppireWindow *w)
else
{
i->save (w);
- w->dirty = FALSE;
+ if (w->edited)
+ g_date_time_unref (w->edited);
+ w->edited = NULL;
+
psppire_window_set_title (w);
}
}
@@ -664,7 +627,8 @@ psppire_window_save_as (PsppireWindow *w)
static void delete_recent (const char *file_name);
gboolean
-psppire_window_load (PsppireWindow *w, const gchar *file, gpointer hint)
+psppire_window_load (PsppireWindow *w, const gchar *file,
+ const gchar *encoding, gpointer hint)
{
gboolean ok;
PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
@@ -675,12 +639,14 @@ psppire_window_load (PsppireWindow *w, const gchar *file, gpointer hint)
g_return_val_if_fail (i->load, FALSE);
- ok = i->load (w, file, hint);
+ ok = i->load (w, file, encoding, hint);
- if ( ok )
+ if (ok)
{
psppire_window_set_filename (w, file);
- w->dirty = FALSE;
+ if (w->edited)
+ g_date_time_unref (w->edited);
+ w->edited = NULL;
}
else
delete_recent (file);
@@ -689,27 +655,6 @@ psppire_window_load (PsppireWindow *w, const gchar *file, gpointer hint)
}
-static void
-on_selection_changed (GtkFileChooser *chooser, GtkWidget *encoding_selector)
-{
- const gchar *sysname;
-
- const gchar *name = gtk_file_chooser_get_filename (chooser);
-
- if ( NULL == name )
- return;
-
- sysname = convert_glib_filename_to_system_filename (name, NULL);
-
- if ( ! fn_exists (sysname))
- {
- gtk_widget_set_sensitive (encoding_selector, FALSE);
- return;
- }
-
- gtk_widget_set_sensitive (encoding_selector, ! any_reader_may_open (sysname));
-}
-
GtkWidget *
psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
{
@@ -718,8 +663,8 @@ psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
gtk_file_chooser_dialog_new (_("Open"),
GTK_WINDOW (toplevel),
GTK_FILE_CHOOSER_ACTION_OPEN,
- GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
- GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+ _("Cancel"), GTK_RESPONSE_CANCEL,
+ _("Open"), GTK_RESPONSE_ACCEPT,
NULL);
g_object_set (dialog, "local-only", FALSE, NULL);
@@ -727,13 +672,18 @@ psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
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)"));
+ 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 ();
@@ -747,6 +697,12 @@ psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
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, "*");
@@ -757,7 +713,7 @@ psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
const gchar *filename = toplevel->filename;
gchar *dir_name;
- if ( ! g_path_is_absolute (filename))
+ if (! g_path_is_absolute (filename))
{
gchar *path =
g_build_filename (g_get_current_dir (), filename, NULL);
@@ -773,19 +729,117 @@ psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
free (dir_name);
}
+ gtk_file_chooser_set_extra_widget (
+ GTK_FILE_CHOOSER (dialog),
+ psppire_encoding_selector_new ("Auto", true));
+
+ return dialog;
+}
+struct item_path
{
- GtkWidget *encoding_selector = psppire_encoding_selector_new ("Auto", true);
+ const struct spv_item **nodes;
+ size_t n;
- gtk_widget_set_sensitive (encoding_selector, FALSE);
+#define N_STUB 10
+ const struct spv_item *stub[N_STUB];
+ };
- g_signal_connect (dialog, "selection-changed", G_CALLBACK (on_selection_changed),
- encoding_selector);
+static void
+swap_nodes (const struct spv_item **a, const struct spv_item **b)
+{
+ const struct spv_item *tmp = *a;
+ *a = *b;
+ *b = tmp;
+}
- gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), encoding_selector);
- }
+static void
+get_path (const struct spv_item *item, struct item_path *path)
+{
+ size_t allocated = 10;
+ path->nodes = path->stub;
+ path->n = 0;
- return dialog;
+ 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
+read_spv_file (const char *filename)
+{
+ struct spv_reader *spv;
+ char *error = spv_open (filename, &spv);
+ if (error)
+ {
+ /* XXX */
+ fprintf (stderr, "%s\n", error);
+ return;
+ }
+
+ 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.
@@ -795,6 +849,8 @@ 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:
@@ -802,18 +858,31 @@ psppire_window_open (PsppireWindow *de)
gchar *name =
gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
- gchar *sysname = convert_glib_filename_to_system_filename (name, NULL);
+ 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)));
- if (any_reader_may_open (sysname))
- open_data_window (de, name, NULL);
- else
- open_syntax_window (name, encoding);
+ 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);
- g_free (sysname);
+ fh_unref (fh);
g_free (name);
}
break;
@@ -829,17 +898,24 @@ psppire_window_open (PsppireWindow *de)
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)
+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 )
+ 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 = CONST_CAST (gchar *, mime_type);
+ 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;
@@ -849,6 +925,7 @@ add_most_recent (const char *file_name, const char *mime_type)
uri, &recent_data);
g_free (recent_data.app_exec);
+ g_free (full_mime_type);
}
g_free (uri);
@@ -864,7 +941,7 @@ delete_recent (const char *file_name)
{
gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
- if ( uri )
+ if (uri)
gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);
g_free (uri);