1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010, 2011 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "psppire-window.h"
27 #define _(msgid) gettext (msgid)
28 #define N_(msgid) msgid
30 #include "data/any-reader.h"
31 #include "data/dataset.h"
34 #include "psppire-conf.h"
35 #include "psppire-data-window.h"
36 #include "psppire-encoding-selector.h"
37 #include "psppire-syntax-window.h"
38 #include "psppire-window-register.h"
41 static void psppire_window_base_init (PsppireWindowClass *class);
42 static void psppire_window_class_init (PsppireWindowClass *class);
43 static void psppire_window_init (PsppireWindow *window);
46 static GObjectClass *parent_class;
49 psppire_window_get_type (void)
51 static GType psppire_window_type = 0;
53 if (!psppire_window_type)
55 static const GTypeInfo psppire_window_info =
57 sizeof (PsppireWindowClass),
58 (GBaseInitFunc) psppire_window_base_init,
59 (GBaseFinalizeFunc) NULL,
60 (GClassInitFunc) psppire_window_class_init,
61 (GClassFinalizeFunc) NULL,
63 sizeof (PsppireWindow),
65 (GInstanceInitFunc) psppire_window_init,
69 g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow",
70 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
73 return psppire_window_type;
88 psppire_window_set_title (PsppireWindow *window)
90 GString *title = g_string_sized_new (80);
93 g_string_append_c (title, '*');
95 if (window->basename || window->id)
98 g_string_append_printf (title, "%s ", window->basename);
100 if (window->id != '\0')
101 g_string_append_printf (title, "[%s] ", window->id);
103 g_string_append_unichar (title, 0x2014); /* em dash */
104 g_string_append_c (title, ' '); /* em dash */
107 g_string_append_printf (title, "PSPPIRE %s", window->description);
109 gtk_window_set_title (GTK_WINDOW (window), title->str);
111 g_string_free (title, TRUE);
115 psppire_window_update_list_name (PsppireWindow *window)
117 PsppireWindowRegister *reg = psppire_window_register_new ();
118 GString *candidate = g_string_sized_new (80);
124 /* Compose a name. */
125 g_string_truncate (candidate, 0);
126 if (window->filename)
128 gchar *display_filename = g_filename_display_name (window->filename);
129 g_string_append (candidate, display_filename);
130 g_free (display_filename);
133 g_string_append_printf (candidate, " [%s]", window->id);
136 g_string_append_printf (candidate, "[%s]", window->id);
138 g_string_append (candidate, window->description);
141 g_string_append_printf (candidate, " #%d", n);
143 if (window->list_name && !strcmp (candidate->str, window->list_name))
145 /* Keep the existing name. */
146 g_string_free (candidate, TRUE);
150 while (psppire_window_register_lookup (reg, candidate->str));
152 if (window->list_name)
153 psppire_window_register_remove (reg, window->list_name);
155 g_free (window->list_name);
156 window->list_name = g_string_free (candidate, FALSE);
158 psppire_window_register_insert (reg, window, window->list_name);
162 psppire_window_name_changed (PsppireWindow *window)
164 psppire_window_set_title (window);
165 psppire_window_update_list_name (window);
169 psppire_window_set_property (GObject *object,
174 PsppireWindow *window = PSPPIRE_WINDOW (object);
178 case PROP_DESCRIPTION:
179 g_free (window->description);
180 window->description = g_value_dup_string (value);
181 psppire_window_set_title (window);
184 g_free (window->filename);
185 window->filename = g_value_dup_string (value);
186 g_free (window->basename);
187 window->basename = (window->filename
188 ? g_filename_display_basename (window->filename)
190 psppire_window_name_changed (window);
195 window->id = g_value_dup_string (value);
196 psppire_window_name_changed (window);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
206 psppire_window_get_property (GObject *object,
211 PsppireWindow *window = PSPPIRE_WINDOW (object);
216 g_value_set_string (value, window->filename);
218 case PROP_DESCRIPTION:
219 g_value_set_string (value, window->description);
222 g_value_set_string (value, window->id);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232 on_realize (GtkWindow *window, gpointer data)
234 PsppireConf *conf = psppire_conf_new ();
236 const gchar *base = G_OBJECT_TYPE_NAME (window);
238 psppire_conf_set_window_geometry (conf, base, window);
243 psppire_window_finalize (GObject *object)
245 PsppireWindow *window = PSPPIRE_WINDOW (object);
247 PsppireWindowRegister *reg = psppire_window_register_new ();
249 psppire_window_register_remove (reg, window->list_name);
250 g_free (window->filename);
251 g_free (window->basename);
253 g_free (window->description);
254 g_free (window->list_name);
256 g_signal_handler_disconnect (psppire_window_register_new (),
257 window->remove_handler);
259 g_signal_handler_disconnect (psppire_window_register_new (),
260 window->insert_handler);
262 g_hash_table_destroy (window->menuitem_table);
264 if (G_OBJECT_CLASS (parent_class)->finalize)
265 G_OBJECT_CLASS (parent_class)->finalize (object);
269 psppire_window_class_init (PsppireWindowClass *class)
271 GObjectClass *object_class = G_OBJECT_CLASS (class);
273 GParamSpec *description_spec =
274 null_if_empty_param ("description",
276 "A string describing the usage of the window",
277 NULL, /*Should be overridden by derived classes */
278 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
280 GParamSpec *filename_spec =
281 null_if_empty_param ("filename",
283 "The name of the file associated with this window, if any",
285 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
287 GParamSpec *id_spec =
288 null_if_empty_param ("id",
290 "The PSPP language identifier for the data associated "
291 "with this window (e.g. dataset name)",
293 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
295 object_class->set_property = psppire_window_set_property;
296 object_class->get_property = psppire_window_get_property;
298 g_object_class_install_property (object_class,
302 g_object_class_install_property (object_class,
306 g_object_class_install_property (object_class,
310 parent_class = g_type_class_peek_parent (class);
315 psppire_window_base_init (PsppireWindowClass *class)
317 GObjectClass *object_class = G_OBJECT_CLASS (class);
319 object_class->finalize = psppire_window_finalize;
325 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
327 /* Prohibit changes to the state */
328 mi->active = !mi->active;
332 /* Look up the window associated with this menuitem and present it to the user */
334 menu_activate (GtkMenuItem *mi, gpointer data)
336 const gchar *key = data;
338 PsppireWindowRegister *reg = psppire_window_register_new ();
340 PsppireWindow *window = psppire_window_register_lookup (reg, key);
342 gtk_window_present (GTK_WINDOW (window));
346 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
351 /* Add a separator before adding the first real item. If we add a separator
352 at any other time, sometimes GtkUIManager removes it. */
353 if (g_hash_table_size (window->menuitem_table) == 0)
355 GtkWidget *separator = gtk_separator_menu_item_new ();
356 gtk_widget_show (separator);
357 gtk_menu_shell_append (window->menu, separator);
360 filename = g_filename_display_name (key);
361 item = gtk_check_menu_item_new_with_label (filename);
364 g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
365 g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
367 gtk_widget_show (item);
369 gtk_menu_shell_append (window->menu, item);
371 /* Set the state without emitting a signal */
372 GTK_CHECK_MENU_ITEM (item)->active =
373 (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
375 g_hash_table_insert (window->menuitem_table, key, item);
379 insert_item (gpointer key, gpointer value, gpointer data)
381 PsppireWindow *window = PSPPIRE_WINDOW (data);
383 if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
386 insert_menuitem_into_menu (window, key);
389 /* Insert a new item into the window menu */
391 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
393 PsppireWindow *window = PSPPIRE_WINDOW (data);
395 insert_menuitem_into_menu (window, (gpointer) key);
400 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
402 PsppireWindow *window = PSPPIRE_WINDOW (data);
405 item = g_hash_table_lookup (window->menuitem_table, key);
407 g_hash_table_remove (window->menuitem_table, key);
409 if (GTK_IS_CONTAINER (window->menu))
410 gtk_container_remove (GTK_CONTAINER (window->menu), item);
414 insert_existing_items (PsppireWindow *window)
416 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
421 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
423 PsppireWindowRegister *reg = psppire_window_register_new ();
425 const gchar *base = G_OBJECT_TYPE_NAME (w);
427 PsppireConf *conf = psppire_conf_new ();
429 psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (w));
434 gint response = psppire_window_query_save (w);
439 case GTK_RESPONSE_CANCEL:
442 case GTK_RESPONSE_APPLY:
443 psppire_window_save (w);
446 /* Save failed, or user exited Save As dialog with Cancel. */
450 case GTK_RESPONSE_REJECT:
455 if ( 1 == psppire_window_register_n_items (reg))
463 psppire_window_init (PsppireWindow *window)
466 window->filename = NULL;
467 window->basename = NULL;
469 window->description = NULL;
470 window->list_name = NULL;
472 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
475 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
477 window->insert_handler = g_signal_connect (psppire_window_register_new (),
479 G_CALLBACK (insert_menuitem),
482 window->remove_handler = g_signal_connect (psppire_window_register_new (),
484 G_CALLBACK (remove_menuitem),
487 window->dirty = FALSE;
489 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
491 g_object_set (window, "icon-name", "psppicon", NULL);
493 g_signal_connect (window, "realize",
494 G_CALLBACK (on_realize), window);
498 Ask the user if the buffer should be saved.
502 psppire_window_query_save (PsppireWindow *se)
506 GtkWidget *cancel_button;
512 g_get_current_time (&time);
515 description = g_filename_display_basename (se->filename);
517 description = g_strdup (se->id);
519 description = g_strdup (se->description);
521 gtk_message_dialog_new (GTK_WINDOW (se),
525 _("Save the changes to `%s' before closing?"),
527 g_free (description);
529 g_object_set (dialog, "icon-name", "psppicon", NULL);
531 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
532 _("If you don't save, changes from the last %ld seconds will be permanently lost."),
533 time.tv_sec - se->savetime.tv_sec);
535 gtk_dialog_add_button (GTK_DIALOG (dialog),
536 _("Close _without saving"),
537 GTK_RESPONSE_REJECT);
539 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
541 GTK_RESPONSE_CANCEL);
543 gtk_dialog_add_button (GTK_DIALOG (dialog),
547 gtk_widget_grab_focus (cancel_button);
549 response = gtk_dialog_run (GTK_DIALOG (dialog));
551 gtk_widget_destroy (dialog);
557 /* The return value is encoded in the glib filename encoding. */
559 psppire_window_get_filename (PsppireWindow *w)
565 /* FILENAME must be encoded in the glib filename encoding. */
567 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
569 g_object_set (w, "filename", filename, NULL);
573 psppire_window_set_unsaved (PsppireWindow *w)
575 if ( w->dirty == FALSE)
576 g_get_current_time (&w->savetime);
580 psppire_window_set_title (w);
584 psppire_window_get_unsaved (PsppireWindow *w)
594 minimise_window (gpointer key, gpointer value, gpointer data)
596 gtk_window_iconify (GTK_WINDOW (value));
601 psppire_window_minimise_all (void)
603 PsppireWindowRegister *reg = psppire_window_register_new ();
605 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
612 psppire_window_model_get_type (void)
614 static GType window_model_type = 0;
616 if (! window_model_type)
618 static const GTypeInfo window_model_info =
620 sizeof (PsppireWindowIface), /* class_size */
621 NULL, /* base_init */
622 NULL, /* base_finalize */
624 NULL, /* class_finalize */
625 NULL, /* class_data */
632 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
633 &window_model_info, 0);
635 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
638 return window_model_type;
643 psppire_window_save (PsppireWindow *w)
645 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
648 g_return_if_fail (i->save);
650 if (w->filename == NULL)
651 psppire_window_save_as (w);
656 psppire_window_set_title (w);
661 psppire_window_save_as (PsppireWindow *w)
663 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
667 g_return_if_fail (i->pick_filename);
669 old_filename = w->filename;
672 i->pick_filename (w);
673 if (w->filename == NULL)
674 w->filename = old_filename;
677 g_free (old_filename);
678 psppire_window_save (w);
682 static void delete_recent (const char *file_name);
685 psppire_window_load (PsppireWindow *w, const gchar *file)
688 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
690 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
694 g_return_val_if_fail (i->load, FALSE);
696 ok = i->load (w, file);
700 psppire_window_set_filename (w, file);
704 delete_recent (file);
710 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
712 GtkFileFilter *filter = gtk_file_filter_new ();
714 gtk_file_chooser_dialog_new (_("Open"),
715 GTK_WINDOW (toplevel),
716 GTK_FILE_CHOOSER_ACTION_OPEN,
717 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
718 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
721 g_object_set (dialog, "local-only", FALSE, NULL);
723 gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
724 gtk_file_filter_add_pattern (filter, "*.sav");
725 gtk_file_filter_add_pattern (filter, "*.SAV");
726 gtk_file_filter_add_pattern (filter, "*.por");
727 gtk_file_filter_add_pattern (filter, "*.POR");
728 gtk_file_filter_add_pattern (filter, "*.sps");
729 gtk_file_filter_add_pattern (filter, "*.SPS");
730 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
732 filter = gtk_file_filter_new ();
733 gtk_file_filter_set_name (filter, _("System Files (*.sav)"));
734 gtk_file_filter_add_pattern (filter, "*.sav");
735 gtk_file_filter_add_pattern (filter, "*.SAV");
736 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
738 filter = gtk_file_filter_new ();
739 gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
740 gtk_file_filter_add_pattern (filter, "*.por");
741 gtk_file_filter_add_pattern (filter, "*.POR");
742 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
744 filter = gtk_file_filter_new ();
745 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
746 gtk_file_filter_add_pattern (filter, "*.sps");
747 gtk_file_filter_add_pattern (filter, "*.SPS");
748 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
750 filter = gtk_file_filter_new ();
751 gtk_file_filter_set_name (filter, _("All Files"));
752 gtk_file_filter_add_pattern (filter, "*");
753 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
755 if (toplevel->filename)
757 const gchar *filename = toplevel->filename;
760 if ( ! g_path_is_absolute (filename))
763 g_build_filename (g_get_current_dir (), filename, NULL);
764 dir_name = g_path_get_dirname (path);
769 dir_name = g_path_get_dirname (filename);
771 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
776 gtk_file_chooser_set_extra_widget (
777 GTK_FILE_CHOOSER (dialog), psppire_encoding_selector_new ("Auto", true));
782 /* Callback for the file_open action.
783 Prompts for a filename and opens it */
785 psppire_window_open (PsppireWindow *de)
787 GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
789 switch (gtk_dialog_run (GTK_DIALOG (dialog)))
791 case GTK_RESPONSE_ACCEPT:
794 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
796 gchar *sysname = convert_glib_filename_to_system_filename (name, NULL);
798 gchar *encoding = psppire_encoding_selector_get_encoding (
799 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
801 if (any_reader_may_open (sysname))
802 open_data_window (de, name);
804 open_syntax_window (name, encoding);
815 gtk_widget_destroy (dialog);
819 /* Puts FILE_NAME (encoded in the glib file name encoding) into the recent list
820 with associated MIME_TYPE. If it's already in the list, it moves it to the
823 add_most_recent (const char *file_name, const char *mime_type)
825 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
829 GtkRecentData recent_data;
831 recent_data.display_name = NULL;
832 recent_data.description = NULL;
833 recent_data.mime_type = CONST_CAST (gchar *, mime_type);
834 recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
835 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
836 recent_data.groups = NULL;
837 recent_data.is_private = FALSE;
839 gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
842 g_free (recent_data.app_exec);
851 If FILE_NAME exists in the recent list, then delete it.
854 delete_recent (const char *file_name)
856 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
859 gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);