1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009, 2010, 2011, 2013, 2014 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/>. */
18 #include <gl/relocatable.h>
20 #include "psppire-window.h"
21 #include "psppire-window-base.h"
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
32 #include "data/any-reader.h"
33 #include "data/file-handle-def.h"
34 #include "data/dataset.h"
35 #include "libpspp/version.h"
38 #include "psppire-data-window.h"
39 #include "psppire-encoding-selector.h"
40 #include "psppire-syntax-window.h"
41 #include "psppire-window-register.h"
43 static void psppire_window_base_init (PsppireWindowClass *class);
44 static void psppire_window_class_init (PsppireWindowClass *class);
45 static void psppire_window_init (PsppireWindow *window);
48 static GObjectClass *parent_class;
51 psppire_window_get_type (void)
53 static GType psppire_window_type = 0;
55 if (!psppire_window_type)
57 static const GTypeInfo psppire_window_info =
59 sizeof (PsppireWindowClass),
60 (GBaseInitFunc) psppire_window_base_init,
61 (GBaseFinalizeFunc) NULL,
62 (GClassInitFunc) psppire_window_class_init,
63 (GClassFinalizeFunc) NULL,
65 sizeof (PsppireWindow),
67 (GInstanceInitFunc) psppire_window_init,
71 g_type_register_static (PSPPIRE_TYPE_WINDOW_BASE, "PsppireWindow",
72 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
75 return psppire_window_type;
90 psppire_window_set_title (PsppireWindow *window)
92 GString *title = g_string_sized_new (80);
95 g_string_append_c (title, '*');
97 if (window->basename || window->id)
100 g_string_append_printf (title, "%s ", window->basename);
102 if (window->id != '\0')
103 g_string_append_printf (title, "[%s] ", window->id);
105 g_string_append_unichar (title, 0x2014); /* em dash */
106 g_string_append_c (title, ' '); /* em dash */
109 g_string_append_printf (title, "PSPPIRE %s", window->description);
111 gtk_window_set_title (GTK_WINDOW (window), title->str);
113 g_string_free (title, TRUE);
117 psppire_window_update_list_name (PsppireWindow *window)
119 PsppireWindowRegister *reg = psppire_window_register_new ();
120 GString *candidate = g_string_sized_new (80);
126 /* Compose a name. */
127 g_string_truncate (candidate, 0);
128 if (window->filename)
130 gchar *display_filename = g_filename_display_name (window->filename);
131 g_string_append (candidate, display_filename);
132 g_free (display_filename);
135 g_string_append_printf (candidate, " [%s]", window->id);
138 g_string_append_printf (candidate, "[%s]", window->id);
140 g_string_append (candidate, window->description);
143 g_string_append_printf (candidate, " #%d", n);
145 if (window->list_name && !strcmp (candidate->str, window->list_name))
147 /* Keep the existing name. */
148 g_string_free (candidate, TRUE);
152 while (psppire_window_register_lookup (reg, candidate->str));
154 if (window->list_name)
155 psppire_window_register_remove (reg, window->list_name);
157 g_free (window->list_name);
158 window->list_name = g_string_free (candidate, FALSE);
160 psppire_window_register_insert (reg, window, window->list_name);
164 psppire_window_name_changed (PsppireWindow *window)
166 psppire_window_set_title (window);
167 psppire_window_update_list_name (window);
171 psppire_window_set_property (GObject *object,
176 PsppireWindow *window = PSPPIRE_WINDOW (object);
180 case PROP_DESCRIPTION:
181 g_free (window->description);
182 window->description = g_value_dup_string (value);
183 psppire_window_set_title (window);
186 g_free (window->filename);
187 window->filename = g_value_dup_string (value);
188 g_free (window->basename);
189 window->basename = (window->filename
190 ? g_filename_display_basename (window->filename)
192 psppire_window_name_changed (window);
197 window->id = g_value_dup_string (value);
198 psppire_window_name_changed (window);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208 psppire_window_get_property (GObject *object,
213 PsppireWindow *window = PSPPIRE_WINDOW (object);
218 g_value_set_string (value, window->filename);
220 case PROP_DESCRIPTION:
221 g_value_set_string (value, window->description);
224 g_value_set_string (value, window->id);
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 psppire_window_finalize (GObject *object)
236 PsppireWindow *window = PSPPIRE_WINDOW (object);
238 PsppireWindowRegister *reg = psppire_window_register_new ();
240 g_signal_handler_disconnect (reg, window->remove_handler);
241 g_signal_handler_disconnect (reg, window->insert_handler);
242 psppire_window_register_remove (reg, window->list_name);
243 g_free (window->filename);
244 g_free (window->basename);
246 g_free (window->description);
247 g_free (window->list_name);
249 g_hash_table_destroy (window->menuitem_table);
251 if (G_OBJECT_CLASS (parent_class)->finalize)
252 G_OBJECT_CLASS (parent_class)->finalize (object);
256 psppire_window_class_init (PsppireWindowClass *class)
258 GObjectClass *object_class = G_OBJECT_CLASS (class);
260 GParamSpec *description_spec =
261 null_if_empty_param ("description",
263 "A string describing the usage of the window",
264 NULL, /*Should be overridden by derived classes */
265 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
267 GParamSpec *filename_spec =
268 null_if_empty_param ("filename",
270 "The name of the file associated with this window, if any",
272 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
274 GParamSpec *id_spec =
275 null_if_empty_param ("id",
277 "The PSPP language identifier for the data associated "
278 "with this window (e.g. dataset name)",
280 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
282 object_class->set_property = psppire_window_set_property;
283 object_class->get_property = psppire_window_get_property;
285 g_object_class_install_property (object_class,
289 g_object_class_install_property (object_class,
293 g_object_class_install_property (object_class,
297 parent_class = g_type_class_peek_parent (class);
302 psppire_window_base_init (PsppireWindowClass *class)
304 GObjectClass *object_class = G_OBJECT_CLASS (class);
306 object_class->finalize = psppire_window_finalize;
312 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
315 /* Prohibit changes to the state */
316 mi->active = !mi->active;
321 /* Look up the window associated with this menuitem and present it to the user */
323 menu_activate (GtkMenuItem *mi, gpointer data)
325 const gchar *key = data;
327 PsppireWindowRegister *reg = psppire_window_register_new ();
329 PsppireWindow *window = psppire_window_register_lookup (reg, key);
331 gtk_window_present (GTK_WINDOW (window));
335 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
340 /* Add a separator before adding the first real item. If we add a separator
341 at any other time, sometimes GtkUIManager removes it. */
342 if (!window->added_separator)
344 GtkWidget *separator = gtk_separator_menu_item_new ();
345 gtk_widget_show (separator);
346 gtk_menu_shell_append (window->menu, separator);
347 window->added_separator = TRUE;
350 filename = g_filename_display_name (key);
351 item = gtk_check_menu_item_new_with_label (filename);
354 g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
355 g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
357 gtk_widget_show (item);
359 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);
367 g_hash_table_insert (window->menuitem_table, key, item);
371 insert_item (gpointer key, gpointer value, gpointer data)
373 PsppireWindow *window = PSPPIRE_WINDOW (data);
375 if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
378 insert_menuitem_into_menu (window, key);
381 /* Insert a new item into the window menu */
383 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
385 PsppireWindow *window = PSPPIRE_WINDOW (data);
387 insert_menuitem_into_menu (window, (gpointer) key);
392 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
394 PsppireWindow *window = PSPPIRE_WINDOW (data);
397 item = g_hash_table_lookup (window->menuitem_table, key);
399 g_hash_table_remove (window->menuitem_table, key);
401 if (GTK_IS_CONTAINER (window->menu))
402 gtk_container_remove (GTK_CONTAINER (window->menu), item);
406 insert_existing_items (PsppireWindow *window)
408 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
413 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
415 PsppireWindowRegister *reg = psppire_window_register_new ();
419 gint response = psppire_window_query_save (w);
424 case GTK_RESPONSE_CANCEL:
427 case GTK_RESPONSE_APPLY:
428 psppire_window_save (w);
431 /* Save failed, or user exited Save As dialog with Cancel. */
435 case GTK_RESPONSE_REJECT:
440 if ( 1 == psppire_window_register_n_items (reg))
448 psppire_window_init (PsppireWindow *window)
451 window->filename = NULL;
452 window->basename = NULL;
454 window->description = NULL;
455 window->list_name = NULL;
457 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
460 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
462 window->insert_handler = g_signal_connect (psppire_window_register_new (),
464 G_CALLBACK (insert_menuitem),
467 window->remove_handler = g_signal_connect (psppire_window_register_new (),
469 G_CALLBACK (remove_menuitem),
472 window->added_separator = FALSE;
473 window->dirty = FALSE;
475 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
477 g_object_set (window, "icon-name", "pspp", NULL);
481 Ask the user if the buffer should be saved.
485 psppire_window_query_save (PsppireWindow *se)
489 GtkWidget *cancel_button;
495 g_get_current_time (&time);
498 description = g_filename_display_basename (se->filename);
500 description = g_strdup (se->id);
502 description = g_strdup (se->description);
504 gtk_message_dialog_new (GTK_WINDOW (se),
508 _("Save the changes to `%s' before closing?"),
510 g_free (description);
512 g_object_set (dialog, "icon-name", "pspp", NULL);
514 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
515 _("If you don't save, changes from the last %ld seconds will be permanently lost."),
516 time.tv_sec - se->savetime.tv_sec);
518 gtk_dialog_add_button (GTK_DIALOG (dialog),
519 _("Close _without saving"),
520 GTK_RESPONSE_REJECT);
522 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
524 GTK_RESPONSE_CANCEL);
526 gtk_dialog_add_button (GTK_DIALOG (dialog),
530 gtk_widget_grab_focus (cancel_button);
532 response = gtk_dialog_run (GTK_DIALOG (dialog));
534 gtk_widget_destroy (dialog);
540 /* The return value is encoded in the glib filename encoding. */
542 psppire_window_get_filename (PsppireWindow *w)
548 /* FILENAME must be encoded in the glib filename encoding. */
550 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
552 g_object_set (w, "filename", filename, NULL);
556 psppire_window_set_unsaved (PsppireWindow *w)
558 if ( w->dirty == FALSE)
559 g_get_current_time (&w->savetime);
563 psppire_window_set_title (w);
567 psppire_window_get_unsaved (PsppireWindow *w)
577 minimise_window (gpointer key, gpointer value, gpointer data)
579 gtk_window_iconify (GTK_WINDOW (value));
584 psppire_window_minimise_all (void)
586 PsppireWindowRegister *reg = psppire_window_register_new ();
588 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
595 psppire_window_model_get_type (void)
597 static GType window_model_type = 0;
599 if (! window_model_type)
601 static const GTypeInfo window_model_info =
603 sizeof (PsppireWindowIface), /* class_size */
604 NULL, /* base_init */
605 NULL, /* base_finalize */
607 NULL, /* class_finalize */
608 NULL, /* class_data */
615 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
616 &window_model_info, 0);
618 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
621 return window_model_type;
626 psppire_window_save (PsppireWindow *w)
628 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
631 g_return_if_fail (i->save);
633 if (w->filename == NULL)
634 psppire_window_save_as (w);
639 psppire_window_set_title (w);
644 psppire_window_save_as (PsppireWindow *w)
646 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
650 g_return_if_fail (i->pick_filename);
652 old_filename = w->filename;
655 i->pick_filename (w);
656 if (w->filename == NULL)
657 w->filename = old_filename;
660 g_free (old_filename);
661 psppire_window_save (w);
665 static void delete_recent (const char *file_name);
668 psppire_window_load (PsppireWindow *w, const gchar *file,
669 const gchar *encoding, gpointer hint)
672 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
674 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
678 g_return_val_if_fail (i->load, FALSE);
680 ok = i->load (w, file, encoding, hint);
684 psppire_window_set_filename (w, file);
688 delete_recent (file);
695 psppire_window_file_chooser_dialog (PsppireWindow *toplevel)
697 GtkFileFilter *filter = gtk_file_filter_new ();
699 gtk_file_chooser_dialog_new (_("Open"),
700 GTK_WINDOW (toplevel),
701 GTK_FILE_CHOOSER_ACTION_OPEN,
702 _("Cancel"), GTK_RESPONSE_CANCEL,
703 _("Open"), GTK_RESPONSE_ACCEPT,
706 g_object_set (dialog, "local-only", FALSE, NULL);
708 gtk_file_filter_set_name (filter, _("Data and Syntax Files"));
709 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
710 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
711 gtk_file_filter_add_pattern (filter, "*.zsav");
712 gtk_file_filter_add_pattern (filter, "*.sps");
713 gtk_file_filter_add_pattern (filter, "*.SPS");
714 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
716 filter = gtk_file_filter_new ();
717 gtk_file_filter_set_name (filter, _("System Files (*.sav, *.zsav)"));
718 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
719 gtk_file_filter_add_pattern (filter, "*.zsav");
720 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
722 filter = gtk_file_filter_new ();
723 gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
724 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
725 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
727 filter = gtk_file_filter_new ();
728 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
729 gtk_file_filter_add_pattern (filter, "*.sps");
730 gtk_file_filter_add_pattern (filter, "*.SPS");
731 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
733 filter = gtk_file_filter_new ();
734 gtk_file_filter_set_name (filter, _("All Files"));
735 gtk_file_filter_add_pattern (filter, "*");
736 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
738 if (toplevel->filename)
740 const gchar *filename = toplevel->filename;
743 if ( ! g_path_is_absolute (filename))
746 g_build_filename (g_get_current_dir (), filename, NULL);
747 dir_name = g_path_get_dirname (path);
752 dir_name = g_path_get_dirname (filename);
754 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog),
759 gtk_file_chooser_set_extra_widget (
760 GTK_FILE_CHOOSER (dialog),
761 psppire_encoding_selector_new ("Auto", true));
766 /* Callback for the file_open action.
767 Prompts for a filename and opens it */
769 psppire_window_open (PsppireWindow *de)
771 GtkWidget *dialog = psppire_window_file_chooser_dialog (de);
773 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), relocate (examples_dir), NULL);
775 switch (gtk_dialog_run (GTK_DIALOG (dialog)))
777 case GTK_RESPONSE_ACCEPT:
780 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
782 const gchar **cs = NULL;
783 g_get_filename_charsets (&cs);
785 gchar *encoding = psppire_encoding_selector_get_encoding (
786 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
788 struct file_handle *fh = fh_create_file (NULL, name, cs[0], fh_default_properties ());
790 int retval = any_reader_detect (fh, NULL);
792 open_data_window (de, name, encoding, NULL);
793 else if (retval == 0)
794 open_syntax_window (name, encoding);
805 gtk_widget_destroy (dialog);
809 /* Puts FILE_NAME (encoded in the glib file name encoding) into the recent list
810 with associated MIME_TYPE. If it's already in the list, it moves it to the
813 add_most_recent (const char *file_name,
814 const char *mime_type, const char *encoding)
816 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
819 GtkRecentData recent_data;
820 gchar *full_mime_type;
822 if (encoding && encoding[0])
823 full_mime_type = g_strdup_printf ("%s; charset=%s",
824 mime_type, encoding);
826 full_mime_type = g_strdup (mime_type);
828 recent_data.display_name = NULL;
829 recent_data.description = NULL;
830 recent_data.mime_type = full_mime_type;
831 recent_data.app_name = CONST_CAST (gchar *, g_get_application_name ());
832 recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
833 recent_data.groups = NULL;
834 recent_data.is_private = FALSE;
836 gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
839 g_free (recent_data.app_exec);
840 g_free (full_mime_type);
849 If FILE_NAME exists in the recent list, then delete it.
852 delete_recent (const char *file_name)
854 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
857 gtk_recent_manager_remove_item (gtk_recent_manager_get_default (), uri, NULL);