1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010, 2011, 2013 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"
20 #include "psppire-window-base.h"
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
31 #include "data/any-reader.h"
32 #include "data/file-name.h"
33 #include "data/dataset.h"
36 #include "psppire-data-window.h"
37 #include "psppire-encoding-selector.h"
38 #include "psppire-syntax-window.h"
39 #include "psppire-window-register.h"
42 static void psppire_window_base_init (PsppireWindowClass *class);
43 static void psppire_window_class_init (PsppireWindowClass *class);
44 static void psppire_window_init (PsppireWindow *window);
47 static GObjectClass *parent_class;
50 psppire_window_get_type (void)
52 static GType psppire_window_type = 0;
54 if (!psppire_window_type)
56 static const GTypeInfo psppire_window_info =
58 sizeof (PsppireWindowClass),
59 (GBaseInitFunc) psppire_window_base_init,
60 (GBaseFinalizeFunc) NULL,
61 (GClassInitFunc) psppire_window_class_init,
62 (GClassFinalizeFunc) NULL,
64 sizeof (PsppireWindow),
66 (GInstanceInitFunc) psppire_window_init,
70 g_type_register_static (PSPPIRE_TYPE_WINDOW_BASE, "PsppireWindow",
71 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
74 return psppire_window_type;
89 psppire_window_set_title (PsppireWindow *window)
91 GString *title = g_string_sized_new (80);
94 g_string_append_c (title, '*');
96 if (window->basename || window->id)
99 g_string_append_printf (title, "%s ", window->basename);
101 if (window->id != '\0')
102 g_string_append_printf (title, "[%s] ", window->id);
104 g_string_append_unichar (title, 0x2014); /* em dash */
105 g_string_append_c (title, ' '); /* em dash */
108 g_string_append_printf (title, "PSPPIRE %s", window->description);
110 gtk_window_set_title (GTK_WINDOW (window), title->str);
112 g_string_free (title, TRUE);
116 psppire_window_update_list_name (PsppireWindow *window)
118 PsppireWindowRegister *reg = psppire_window_register_new ();
119 GString *candidate = g_string_sized_new (80);
125 /* Compose a name. */
126 g_string_truncate (candidate, 0);
127 if (window->filename)
129 gchar *display_filename = g_filename_display_name (window->filename);
130 g_string_append (candidate, display_filename);
131 g_free (display_filename);
134 g_string_append_printf (candidate, " [%s]", window->id);
137 g_string_append_printf (candidate, "[%s]", window->id);
139 g_string_append (candidate, window->description);
142 g_string_append_printf (candidate, " #%d", n);
144 if (window->list_name && !strcmp (candidate->str, window->list_name))
146 /* Keep the existing name. */
147 g_string_free (candidate, TRUE);
151 while (psppire_window_register_lookup (reg, candidate->str));
153 if (window->list_name)
154 psppire_window_register_remove (reg, window->list_name);
156 g_free (window->list_name);
157 window->list_name = g_string_free (candidate, FALSE);
159 psppire_window_register_insert (reg, window, window->list_name);
163 psppire_window_name_changed (PsppireWindow *window)
165 psppire_window_set_title (window);
166 psppire_window_update_list_name (window);
170 psppire_window_set_property (GObject *object,
175 PsppireWindow *window = PSPPIRE_WINDOW (object);
179 case PROP_DESCRIPTION:
180 g_free (window->description);
181 window->description = g_value_dup_string (value);
182 psppire_window_set_title (window);
185 g_free (window->filename);
186 window->filename = g_value_dup_string (value);
187 g_free (window->basename);
188 window->basename = (window->filename
189 ? g_filename_display_basename (window->filename)
191 psppire_window_name_changed (window);
196 window->id = g_value_dup_string (value);
197 psppire_window_name_changed (window);
200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 psppire_window_get_property (GObject *object,
212 PsppireWindow *window = PSPPIRE_WINDOW (object);
217 g_value_set_string (value, window->filename);
219 case PROP_DESCRIPTION:
220 g_value_set_string (value, window->description);
223 g_value_set_string (value, window->id);
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233 psppire_window_finalize (GObject *object)
235 PsppireWindow *window = PSPPIRE_WINDOW (object);
237 PsppireWindowRegister *reg = psppire_window_register_new ();
239 psppire_window_register_remove (reg, window->list_name);
240 g_free (window->filename);
241 g_free (window->basename);
243 g_free (window->description);
244 g_free (window->list_name);
246 g_signal_handler_disconnect (psppire_window_register_new (),
247 window->remove_handler);
249 g_signal_handler_disconnect (psppire_window_register_new (),
250 window->insert_handler);
252 g_hash_table_destroy (window->menuitem_table);
254 if (G_OBJECT_CLASS (parent_class)->finalize)
255 G_OBJECT_CLASS (parent_class)->finalize (object);
259 psppire_window_class_init (PsppireWindowClass *class)
261 GObjectClass *object_class = G_OBJECT_CLASS (class);
263 GParamSpec *description_spec =
264 null_if_empty_param ("description",
266 "A string describing the usage of the window",
267 NULL, /*Should be overridden by derived classes */
268 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
270 GParamSpec *filename_spec =
271 null_if_empty_param ("filename",
273 "The name of the file associated with this window, if any",
275 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
277 GParamSpec *id_spec =
278 null_if_empty_param ("id",
280 "The PSPP language identifier for the data associated "
281 "with this window (e.g. dataset name)",
283 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
285 object_class->set_property = psppire_window_set_property;
286 object_class->get_property = psppire_window_get_property;
288 g_object_class_install_property (object_class,
292 g_object_class_install_property (object_class,
296 g_object_class_install_property (object_class,
300 parent_class = g_type_class_peek_parent (class);
305 psppire_window_base_init (PsppireWindowClass *class)
307 GObjectClass *object_class = G_OBJECT_CLASS (class);
309 object_class->finalize = psppire_window_finalize;
315 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
317 /* Prohibit changes to the state */
318 mi->active = !mi->active;
322 /* Look up the window associated with this menuitem and present it to the user */
324 menu_activate (GtkMenuItem *mi, gpointer data)
326 const gchar *key = data;
328 PsppireWindowRegister *reg = psppire_window_register_new ();
330 PsppireWindow *window = psppire_window_register_lookup (reg, key);
332 gtk_window_present (GTK_WINDOW (window));
336 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
341 /* Add a separator before adding the first real item. If we add a separator
342 at any other time, sometimes GtkUIManager removes it. */
343 if (!window->added_separator)
345 GtkWidget *separator = gtk_separator_menu_item_new ();
346 gtk_widget_show (separator);
347 gtk_menu_shell_append (window->menu, separator);
348 window->added_separator = TRUE;
351 filename = g_filename_display_name (key);
352 item = gtk_check_menu_item_new_with_label (filename);
355 g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
356 g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
358 gtk_widget_show (item);
360 gtk_menu_shell_append (window->menu, item);
362 /* Set the state without emitting a signal */
363 GTK_CHECK_MENU_ITEM (item)->active =
364 (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
366 g_hash_table_insert (window->menuitem_table, key, item);
370 insert_item (gpointer key, gpointer value, gpointer data)
372 PsppireWindow *window = PSPPIRE_WINDOW (data);
374 if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
377 insert_menuitem_into_menu (window, key);
380 /* Insert a new item into the window menu */
382 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
384 PsppireWindow *window = PSPPIRE_WINDOW (data);
386 insert_menuitem_into_menu (window, (gpointer) key);
391 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
393 PsppireWindow *window = PSPPIRE_WINDOW (data);
396 item = g_hash_table_lookup (window->menuitem_table, key);
398 g_hash_table_remove (window->menuitem_table, key);
400 if (GTK_IS_CONTAINER (window->menu))
401 gtk_container_remove (GTK_CONTAINER (window->menu), item);
405 insert_existing_items (PsppireWindow *window)
407 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
412 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
414 PsppireWindowRegister *reg = psppire_window_register_new ();
418 gint response = psppire_window_query_save (w);
423 case GTK_RESPONSE_CANCEL:
426 case GTK_RESPONSE_APPLY:
427 psppire_window_save (w);
430 /* Save failed, or user exited Save As dialog with Cancel. */
434 case GTK_RESPONSE_REJECT:
439 if ( 1 == psppire_window_register_n_items (reg))
447 psppire_window_init (PsppireWindow *window)
450 window->filename = NULL;
451 window->basename = NULL;
453 window->description = NULL;
454 window->list_name = NULL;
456 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
459 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
461 window->insert_handler = g_signal_connect (psppire_window_register_new (),
463 G_CALLBACK (insert_menuitem),
466 window->remove_handler = g_signal_connect (psppire_window_register_new (),
468 G_CALLBACK (remove_menuitem),
471 window->added_separator = FALSE;
472 window->dirty = FALSE;
474 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
476 g_object_set (window, "icon-name", "pspp", NULL);
480 Ask the user if the buffer should be saved.
484 psppire_window_query_save (PsppireWindow *se)
488 GtkWidget *cancel_button;
494 g_get_current_time (&time);
497 description = g_filename_display_basename (se->filename);
499 description = g_strdup (se->id);
501 description = g_strdup (se->description);
503 gtk_message_dialog_new (GTK_WINDOW (se),
507 _("Save the changes to `%s' before closing?"),
509 g_free (description);
511 g_object_set (dialog, "icon-name", "pspp", NULL);
513 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
514 _("If you don't save, changes from the last %ld seconds will be permanently lost."),
515 time.tv_sec - se->savetime.tv_sec);
517 gtk_dialog_add_button (GTK_DIALOG (dialog),
518 _("Close _without saving"),
519 GTK_RESPONSE_REJECT);
521 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
523 GTK_RESPONSE_CANCEL);
525 gtk_dialog_add_button (GTK_DIALOG (dialog),
529 gtk_widget_grab_focus (cancel_button);
531 response = gtk_dialog_run (GTK_DIALOG (dialog));
533 gtk_widget_destroy (dialog);
539 /* The return value is encoded in the glib filename encoding. */
541 psppire_window_get_filename (PsppireWindow *w)
547 /* FILENAME must be encoded in the glib filename encoding. */
549 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
551 g_object_set (w, "filename", filename, NULL);
555 psppire_window_set_unsaved (PsppireWindow *w)
557 if ( w->dirty == FALSE)
558 g_get_current_time (&w->savetime);
562 psppire_window_set_title (w);
566 psppire_window_get_unsaved (PsppireWindow *w)
576 minimise_window (gpointer key, gpointer value, gpointer data)
578 gtk_window_iconify (GTK_WINDOW (value));
583 psppire_window_minimise_all (void)
585 PsppireWindowRegister *reg = psppire_window_register_new ();
587 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
594 psppire_window_model_get_type (void)
596 static GType window_model_type = 0;
598 if (! window_model_type)
600 static const GTypeInfo window_model_info =
602 sizeof (PsppireWindowIface), /* class_size */
603 NULL, /* base_init */
604 NULL, /* base_finalize */
606 NULL, /* class_finalize */
607 NULL, /* class_data */
614 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
615 &window_model_info, 0);
617 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
620 return window_model_type;
625 psppire_window_save (PsppireWindow *w)
627 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
630 g_return_if_fail (i->save);
632 if (w->filename == NULL)
633 psppire_window_save_as (w);
638 psppire_window_set_title (w);
643 psppire_window_save_as (PsppireWindow *w)
645 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
649 g_return_if_fail (i->pick_filename);
651 old_filename = w->filename;
654 i->pick_filename (w);
655 if (w->filename == NULL)
656 w->filename = old_filename;
659 g_free (old_filename);
660 psppire_window_save (w);
664 static void delete_recent (const char *file_name);
667 psppire_window_load (PsppireWindow *w, const gchar *file)
670 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
672 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
676 g_return_val_if_fail (i->load, FALSE);
678 ok = i->load (w, file);
682 psppire_window_set_filename (w, file);
686 delete_recent (file);
693 on_selection_changed (GtkFileChooser *chooser, GtkWidget *encoding_selector)
695 const gchar *sysname;
697 const gchar *name = gtk_file_chooser_get_filename (chooser);
702 sysname = convert_glib_filename_to_system_filename (name, NULL);
704 if ( ! fn_exists (sysname))
706 gtk_widget_set_sensitive (encoding_selector, FALSE);
710 gtk_widget_set_sensitive (encoding_selector, ! any_reader_may_open (sysname));
714 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
716 GtkFileFilter *filter = gtk_file_filter_new ();
718 gtk_file_chooser_dialog_new (_("Open"),
719 GTK_WINDOW (toplevel),
720 GTK_FILE_CHOOSER_ACTION_OPEN,
721 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
722 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
725 g_object_set (dialog, "local-only", FALSE, NULL);
727 gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
728 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
729 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
730 gtk_file_filter_add_pattern (filter, "*.sps");
731 gtk_file_filter_add_pattern (filter, "*.SPS");
732 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
734 filter = gtk_file_filter_new ();
735 gtk_file_filter_set_name (filter, _("System Files (*.sav)"));
736 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
737 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
739 filter = gtk_file_filter_new ();
740 gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
741 gtk_file_filter_add_mime_type (filter, "application/x-spss-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),
778 GtkWidget *encoding_selector = psppire_encoding_selector_new ("Auto", true);
780 gtk_widget_set_sensitive (encoding_selector, FALSE);
782 g_signal_connect (dialog, "selection-changed", G_CALLBACK (on_selection_changed),
785 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), encoding_selector);
791 /* Callback for the file_open action.
792 Prompts for a filename and opens it */
794 psppire_window_open (PsppireWindow *de)
796 GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
798 switch (gtk_dialog_run (GTK_DIALOG (dialog)))
800 case GTK_RESPONSE_ACCEPT:
803 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
805 gchar *sysname = convert_glib_filename_to_system_filename (name, NULL);
807 gchar *encoding = psppire_encoding_selector_get_encoding (
808 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
810 if (any_reader_may_open (sysname))
811 open_data_window (de, name);
813 open_syntax_window (name, encoding);
824 gtk_widget_destroy (dialog);
828 /* Puts FILE_NAME (encoded in the glib file name encoding) into the recent list
829 with associated MIME_TYPE. If it's already in the list, it moves it to the
832 add_most_recent (const char *file_name, const char *mime_type)
834 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
838 GtkRecentData recent_data;
840 recent_data.display_name = NULL;
841 recent_data.description = NULL;
842 recent_data.mime_type = CONST_CAST (gchar *, mime_type);
843 recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
844 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
845 recent_data.groups = NULL;
846 recent_data.is_private = FALSE;
848 gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
851 g_free (recent_data.app_exec);
860 If FILE_NAME exists in the recent list, then delete it.
863 delete_recent (const char *file_name)
865 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
868 gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);