X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fui%2Fgui%2Fpsppire-window.c;h=2ae6b02fd98cad854507e63fce27b2d1bdda737e;hb=c742f5287ac3c40742091c4f37d368f6206e3d3f;hp=c1333769802671d1fcd612cb3194ece3a38bffff;hpb=1efacfa55381cfdbd7f01006f217f1d03a4c48af;p=pspp diff --git a/src/ui/gui/psppire-window.c b/src/ui/gui/psppire-window.c index c133376980..2ae6b02fd9 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 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 @@ -16,11 +16,16 @@ #include + +#include +#include #include #include #include +#include #include +#include #include #define _(msgid) gettext (msgid) @@ -28,6 +33,7 @@ #include "psppire-window.h" #include "psppire-window-register.h" +#include "psppire-conf.h" static void psppire_window_base_finalize (PsppireWindowClass *, gpointer); static void psppire_window_base_init (PsppireWindowClass *class); @@ -35,7 +41,6 @@ static void psppire_window_class_init (PsppireWindowClass *class); static void psppire_window_init (PsppireWindow *window); -static PsppireWindowClass *the_class; static GObjectClass *parent_class; GType @@ -60,7 +65,7 @@ psppire_window_get_type (void) psppire_window_type = g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow", - &psppire_window_info, 0); + &psppire_window_info, G_TYPE_FLAG_ABSTRACT); } return psppire_window_type; @@ -72,7 +77,7 @@ enum { PROP_0, PROP_FILENAME, - PROP_USAGE + PROP_DESCRIPTION }; @@ -82,7 +87,24 @@ uniquify (const gchar *str, int *x) return g_strdup_printf ("%s%d", str, (*x)++); } +static gchar mdash[6] = {0,0,0,0,0,0}; + +static void +psppire_window_set_title (PsppireWindow *window) +{ + GString *title = g_string_sized_new (80); + + g_string_printf (title, _("%s %s PSPPIRE %s"), + window->basename ? window->basename : "", + mdash, window->description); + if (window->dirty) + g_string_prepend_c (title, '*'); + + gtk_window_set_title (GTK_WINDOW (window), title->str); + + g_string_free (title, TRUE); +} static void psppire_window_set_property (GObject *object, @@ -94,47 +116,42 @@ psppire_window_set_property (GObject *object, switch (prop_id) { - case PROP_USAGE: - window->usage = g_value_get_enum (value); + case PROP_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); + gchar *candidate_name ; + + { + const gchar *name = g_value_get_string (value); + int x = 0; + GValue def = {0}; + g_value_init (&def, pspec->value_type); + + if ( NULL == name) + { + g_param_value_set_default (pspec, &def); + name = g_value_get_string (&def); + } + + candidate_name = xstrdup (name); + + while ( psppire_window_register_lookup (reg, candidate_name)) + { + free (candidate_name); + candidate_name = uniquify (name, &x); + } + + window->basename = g_filename_display_basename (candidate_name); + + g_value_unset (&def); + } + + psppire_window_set_title (window); if ( window->name) psppire_window_register_remove (reg, window->name); @@ -143,9 +160,6 @@ psppire_window_set_property (GObject *object, window->name = candidate_name; psppire_window_register_insert (reg, window, window->name); - - free (basename); - free (title); } break; default: @@ -165,12 +179,12 @@ 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); break; + case PROP_DESCRIPTION: + g_value_set_string (value, window->description); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -178,16 +192,27 @@ psppire_window_get_property (GObject *object, } +static void +on_realize (GtkWindow *window, gpointer data) +{ + PsppireConf *conf = psppire_conf_new (); + + const gchar *base = G_OBJECT_TYPE_NAME (window); + + psppire_conf_set_window_geometry (conf, base, window); +} + 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); + free (window->description); g_signal_handler_disconnect (psppire_window_register_new (), window->remove_handler); @@ -207,37 +232,34 @@ 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 = + g_param_spec_string ("description", + "Description", + "A string describing the usage of the window", + "??????", /*Should be overridden by derived classes */ + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); GParamSpec *filename_spec = g_param_spec_string ("filename", "File name", "The name of the file associated with this window, if any", - "Untitled", - G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT - ); + /* TRANSLATORS: This will form a filename. Please avoid whitespace. */ + _("Untitled"), + G_PARAM_CONSTRUCT | G_PARAM_READWRITE); + g_unichar_to_utf8 (0x2014, mdash); object_class->set_property = psppire_window_set_property; object_class->get_property = psppire_window_get_property; g_object_class_install_property (object_class, - PROP_FILENAME, - filename_spec); + PROP_DESCRIPTION, + description_spec); g_object_class_install_property (object_class, - PROP_USAGE, - use_class_spec); - + PROP_FILENAME, + filename_spec); - the_class = class; parent_class = g_type_class_peek_parent (class); } @@ -282,13 +304,16 @@ menu_activate (GtkMenuItem *mi, gpointer data) static void insert_menuitem_into_menu (PsppireWindow *window, gpointer key) { - GtkWidget *item = gtk_check_menu_item_new_with_label (key); + gchar *filename = g_filename_display_name (key); + GtkWidget *item = gtk_check_menu_item_new_with_label (filename); + + 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); /* Set the state without emitting a signal */ @@ -325,14 +350,12 @@ 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); + if (GTK_IS_CONTAINER (window->menu)) + gtk_container_remove (GTK_CONTAINER (window->menu), item); } static void @@ -341,11 +364,50 @@ 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 (); + + const gchar *base = G_OBJECT_TYPE_NAME (w); + + PsppireConf *conf = psppire_conf_new (); + + psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (w)); + + + if ( w->dirty ) + { + 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); + 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->description = xstrdup (""); window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal); @@ -361,19 +423,82 @@ psppire_window_init (PsppireWindow *window) "removed", G_CALLBACK (remove_menuitem), window); -} + window->dirty = FALSE; + + g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window); + + g_object_set (window, "icon-name", "psppicon", NULL); -GtkWidget* -psppire_window_new (PsppireWindowUsage usage) + g_signal_connect (window, "realize", + G_CALLBACK (on_realize), window); + +} + +/* + 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)); + gchar *fn; + gint response; + GtkWidget *dialog; + GtkWidget *cancel_button; + + const gchar *description; + const gchar *filename = psppire_window_get_filename (se); + + GTimeVal time; + + g_get_current_time (&time); + + g_object_get (se, "description", &description, NULL); + + g_return_val_if_fail (filename != NULL, GTK_RESPONSE_NONE); + + + fn = g_filename_display_basename (filename); + + dialog = + gtk_message_dialog_new (GTK_WINDOW (se), + GTK_DIALOG_MODAL, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_NONE, + _("Save the changes to \"%s\" before closing?"), + fn); + g_free (fn); + + g_object_set (dialog, "icon-name", "psppicon", 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."), + time.tv_sec - se->savetime.tv_sec); + + 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, + GTK_RESPONSE_CANCEL); + + gtk_dialog_add_button (GTK_DIALOG (dialog), + GTK_STOCK_SAVE, + GTK_RESPONSE_APPLY); + + gtk_widget_grab_focus (cancel_button); + + response = gtk_dialog_run (GTK_DIALOG (dialog)); + + gtk_widget_destroy (dialog); + + return response; } + const gchar * psppire_window_get_filename (PsppireWindow *w) { @@ -389,35 +514,26 @@ psppire_window_set_filename (PsppireWindow *w, const gchar *filename) g_object_set (w, "filename", filename, NULL); } - - -GType -psppire_window_usage_get_type (void) +void +psppire_window_set_unsaved (PsppireWindow *w) { - static GType etype = 0; - if (etype == 0) - { - static const GEnumValue values[] = { - { PSPPIRE_WINDOW_USAGE_SYNTAX, "PSPPIRE_WINDOW_USAGE_SYNTAX", - "Syntax" }, + if ( w->dirty == FALSE) + g_get_current_time (&w->savetime); - { PSPPIRE_WINDOW_USAGE_OUTPUT, "PSPPIRE_WINDOW_USAGE_OUTPUT", - "Output" }, + w->dirty = TRUE; - { PSPPIRE_WINDOW_USAGE_DATA, "PSPPIRE_WINDOW_USAGE_DATA", - "Data" }, - - { 0, NULL, NULL } - }; - - etype = g_enum_register_static (g_intern_static_string ("PsppireWindowUsage"), - values); - } + psppire_window_set_title (w); +} - return etype; +gboolean +psppire_window_get_unsaved (PsppireWindow *w) +{ + return w->dirty; } + + static void minimise_window (gpointer key, gpointer value, gpointer data) @@ -433,3 +549,119 @@ psppire_window_minimise_all (void) g_hash_table_foreach (reg->name_table, minimise_window, NULL); } + + + + +GType +psppire_window_model_get_type (void) +{ + static GType window_model_type = 0; + + if (! window_model_type) + { + 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 + }; + + window_model_type = + g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel", + &window_model_info, 0); + + g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT); + } + + return window_model_type; +} + + +void +psppire_window_save (PsppireWindow *w) +{ + PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w); + + g_assert (PSPPIRE_IS_WINDOW_MODEL (w)); + + g_assert (i); + + g_return_if_fail (i->save); + + i->save (w); + + w->dirty = FALSE; + psppire_window_set_title (w); +} + +extern GtkRecentManager *the_recent_mgr; + +static void add_most_recent (const char *file_name, GtkRecentManager *rm); +static void delete_recent (const char *file_name, GtkRecentManager *rm); + +gboolean +psppire_window_load (PsppireWindow *w, const gchar *file) +{ + 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); + + if ( ok ) + { + psppire_window_set_filename (w, file); + add_most_recent (file, the_recent_mgr); + w->dirty = FALSE; + } + else + delete_recent (file, the_recent_mgr); + + psppire_window_set_title (w); + + return ok; +} + + +/* Puts FILE_NAME into the recent list. + If it's already in the list, it moves it to the top +*/ +static void +add_most_recent (const char *file_name, GtkRecentManager *rm) +{ + gchar *uri = g_filename_to_uri (file_name, NULL, NULL); + + if ( uri ) + gtk_recent_manager_add_item (rm, uri); + + g_free (uri); +} + + + +/* + If FILE_NAME exists in the recent list, then delete it. + */ +static void +delete_recent (const char *file_name, GtkRecentManager *rm) +{ + gchar *uri = g_filename_to_uri (file_name, NULL, NULL); + + if ( uri ) + gtk_recent_manager_remove_item (rm, uri, NULL); + + g_free (uri); +} +