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/>. */
20 #include <gtk/gtkstock.h>
21 #include <gtk/gtkmessagedialog.h>
22 #include <gtk/gtksignal.h>
23 #include <gtk/gtkwindow.h>
24 #include <gtk/gtkcheckmenuitem.h>
25 #include <gtk/gtkmain.h>
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
34 #include "psppire-window.h"
35 #include "psppire-window-register.h"
36 #include "psppire-conf.h"
38 static void psppire_window_base_finalize (PsppireWindowClass *, gpointer);
39 static void psppire_window_base_init (PsppireWindowClass *class);
40 static void psppire_window_class_init (PsppireWindowClass *class);
41 static void psppire_window_init (PsppireWindow *window);
44 static GObjectClass *parent_class;
47 psppire_window_get_type (void)
49 static GType psppire_window_type = 0;
51 if (!psppire_window_type)
53 static const GTypeInfo psppire_window_info =
55 sizeof (PsppireWindowClass),
56 (GBaseInitFunc) psppire_window_base_init,
57 (GBaseFinalizeFunc) psppire_window_base_finalize,
58 (GClassInitFunc) psppire_window_class_init,
59 (GClassFinalizeFunc) NULL,
61 sizeof (PsppireWindow),
63 (GInstanceInitFunc) psppire_window_init,
67 g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow",
68 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
71 return psppire_window_type;
85 uniquify (const gchar *str, int *x)
87 return g_strdup_printf ("%s%d", str, (*x)++);
91 psppire_window_set_title (PsppireWindow *window)
93 GString *title = g_string_sized_new (80);
95 g_string_printf (title, "%s ", window->basename ? window->basename : "");
96 g_string_append_unichar (title, 0x2014); /* em dash */
97 g_string_printf (title, " PSPPIRE %s", window->description);
100 g_string_prepend_c (title, '*');
102 gtk_window_set_title (GTK_WINDOW (window), title->str);
104 g_string_free (title, TRUE);
108 psppire_window_set_property (GObject *object,
113 PsppireWindow *window = PSPPIRE_WINDOW (object);
117 case PROP_DESCRIPTION:
118 window->description = g_value_dup_string (value);
119 psppire_window_set_title (window);
123 PsppireWindowRegister *reg = psppire_window_register_new ();
125 gchar *candidate_name ;
128 const gchar *name = g_value_get_string (value);
131 g_value_init (&def, pspec->value_type);
135 g_param_value_set_default (pspec, &def);
136 name = g_value_get_string (&def);
139 candidate_name = xstrdup (name);
141 while ( psppire_window_register_lookup (reg, candidate_name))
143 free (candidate_name);
144 candidate_name = uniquify (name, &x);
147 window->basename = g_filename_display_basename (candidate_name);
149 g_value_unset (&def);
152 psppire_window_set_title (window);
155 psppire_window_register_remove (reg, window->name);
158 window->name = candidate_name;
160 psppire_window_register_insert (reg, window, window->name);
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171 psppire_window_get_property (GObject *object,
176 PsppireWindow *window = PSPPIRE_WINDOW (object);
181 g_value_set_string (value, window->name);
183 case PROP_DESCRIPTION:
184 g_value_set_string (value, window->description);
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 on_realize (GtkWindow *window, gpointer data)
196 PsppireConf *conf = psppire_conf_new ();
198 const gchar *base = G_OBJECT_TYPE_NAME (window);
200 psppire_conf_set_window_geometry (conf, base, window);
205 psppire_window_finalize (GObject *object)
207 PsppireWindow *window = PSPPIRE_WINDOW (object);
209 PsppireWindowRegister *reg = psppire_window_register_new ();
211 psppire_window_register_remove (reg, window->name);
213 free (window->description);
215 g_signal_handler_disconnect (psppire_window_register_new (),
216 window->remove_handler);
218 g_signal_handler_disconnect (psppire_window_register_new (),
219 window->insert_handler);
221 g_hash_table_destroy (window->menuitem_table);
223 if (G_OBJECT_CLASS (parent_class)->finalize)
224 G_OBJECT_CLASS (parent_class)->finalize (object);
229 psppire_window_class_init (PsppireWindowClass *class)
231 GObjectClass *object_class = G_OBJECT_CLASS (class);
233 GParamSpec *description_spec =
234 g_param_spec_string ("description",
236 "A string describing the usage of the window",
237 "??????", /*Should be overridden by derived classes */
238 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
240 GParamSpec *filename_spec =
241 g_param_spec_string ("filename",
243 "The name of the file associated with this window, if any",
244 /* TRANSLATORS: This will form a filename. Please avoid whitespace. */
246 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
248 object_class->set_property = psppire_window_set_property;
249 object_class->get_property = psppire_window_get_property;
251 g_object_class_install_property (object_class,
255 g_object_class_install_property (object_class,
259 parent_class = g_type_class_peek_parent (class);
264 psppire_window_base_init (PsppireWindowClass *class)
266 GObjectClass *object_class = G_OBJECT_CLASS (class);
268 object_class->finalize = psppire_window_finalize;
274 psppire_window_base_finalize (PsppireWindowClass *class,
280 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
282 /* Prohibit changes to the state */
283 mi->active = !mi->active;
287 /* Look up the window associated with this menuitem and present it to the user */
289 menu_activate (GtkMenuItem *mi, gpointer data)
291 const gchar *key = data;
293 PsppireWindowRegister *reg = psppire_window_register_new ();
295 PsppireWindow *window = psppire_window_register_lookup (reg, key);
297 gtk_window_present (GTK_WINDOW (window));
301 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
306 /* Add a separator before adding the first real item. If we add a separator
307 at any other time, sometimes GtkUIManager removes it. */
308 if (g_hash_table_size (window->menuitem_table) == 0)
310 GtkWidget *separator = gtk_separator_menu_item_new ();
311 gtk_widget_show (separator);
312 gtk_menu_shell_append (window->menu, separator);
315 filename = g_filename_display_name (key);
316 item = gtk_check_menu_item_new_with_label (filename);
319 g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
320 g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
322 gtk_widget_show (item);
324 gtk_menu_shell_append (window->menu, item);
326 /* Set the state without emitting a signal */
327 GTK_CHECK_MENU_ITEM (item)->active =
328 (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
330 g_hash_table_insert (window->menuitem_table, key, item);
334 insert_item (gpointer key, gpointer value, gpointer data)
336 PsppireWindow *window = PSPPIRE_WINDOW (data);
338 if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
341 insert_menuitem_into_menu (window, key);
344 /* Insert a new item into the window menu */
346 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
348 PsppireWindow *window = PSPPIRE_WINDOW (data);
350 insert_menuitem_into_menu (window, (gpointer) key);
355 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
357 PsppireWindow *window = PSPPIRE_WINDOW (data);
360 item = g_hash_table_lookup (window->menuitem_table, key);
362 g_hash_table_remove (window->menuitem_table, key);
364 if (GTK_IS_CONTAINER (window->menu))
365 gtk_container_remove (GTK_CONTAINER (window->menu), item);
369 insert_existing_items (PsppireWindow *window)
371 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
376 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
378 PsppireWindowRegister *reg = psppire_window_register_new ();
380 const gchar *base = G_OBJECT_TYPE_NAME (w);
382 PsppireConf *conf = psppire_conf_new ();
384 psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (w));
389 gint response = psppire_window_query_save (w);
394 case GTK_RESPONSE_CANCEL:
397 case GTK_RESPONSE_APPLY:
398 psppire_window_save (w);
400 case GTK_RESPONSE_REJECT:
405 if ( 1 == psppire_window_register_n_items (reg))
413 psppire_window_init (PsppireWindow *window)
417 window->description = xstrdup ("");
419 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
422 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
424 window->insert_handler = g_signal_connect (psppire_window_register_new (),
426 G_CALLBACK (insert_menuitem),
429 window->remove_handler = g_signal_connect (psppire_window_register_new (),
431 G_CALLBACK (remove_menuitem),
434 window->dirty = FALSE;
436 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
438 g_object_set (window, "icon-name", "psppicon", NULL);
440 g_signal_connect (window, "realize",
441 G_CALLBACK (on_realize), window);
446 Ask the user if the buffer should be saved.
450 psppire_window_query_save (PsppireWindow *se)
455 GtkWidget *cancel_button;
457 const gchar *description;
458 const gchar *filename = psppire_window_get_filename (se);
462 g_get_current_time (&time);
464 g_object_get (se, "description", &description, NULL);
466 g_return_val_if_fail (filename != NULL, GTK_RESPONSE_NONE);
469 fn = g_filename_display_basename (filename);
472 gtk_message_dialog_new (GTK_WINDOW (se),
476 _("Save the changes to `%s' before closing?"),
480 g_object_set (dialog, "icon-name", "psppicon", NULL);
482 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
483 _("If you don't save, changes from the last %ld seconds will be permanently lost."),
484 time.tv_sec - se->savetime.tv_sec);
486 gtk_dialog_add_button (GTK_DIALOG (dialog),
487 _("Close _without saving"),
488 GTK_RESPONSE_REJECT);
490 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
492 GTK_RESPONSE_CANCEL);
494 gtk_dialog_add_button (GTK_DIALOG (dialog),
498 gtk_widget_grab_focus (cancel_button);
500 response = gtk_dialog_run (GTK_DIALOG (dialog));
502 gtk_widget_destroy (dialog);
510 psppire_window_get_filename (PsppireWindow *w)
512 const gchar *name = NULL;
513 g_object_get (w, "filename", &name, NULL);
519 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
521 g_object_set (w, "filename", filename, NULL);
525 psppire_window_set_unsaved (PsppireWindow *w)
527 if ( w->dirty == FALSE)
528 g_get_current_time (&w->savetime);
532 psppire_window_set_title (w);
536 psppire_window_get_unsaved (PsppireWindow *w)
546 minimise_window (gpointer key, gpointer value, gpointer data)
548 gtk_window_iconify (GTK_WINDOW (value));
553 psppire_window_minimise_all (void)
555 PsppireWindowRegister *reg = psppire_window_register_new ();
557 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
564 psppire_window_model_get_type (void)
566 static GType window_model_type = 0;
568 if (! window_model_type)
570 static const GTypeInfo window_model_info =
572 sizeof (PsppireWindowIface), /* class_size */
573 NULL, /* base_init */
574 NULL, /* base_finalize */
576 NULL, /* class_finalize */
577 NULL, /* class_data */
584 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
585 &window_model_info, 0);
587 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
590 return window_model_type;
595 psppire_window_save (PsppireWindow *w)
597 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
599 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
603 g_return_if_fail (i->save);
608 psppire_window_set_title (w);
611 extern GtkRecentManager *the_recent_mgr;
613 static void add_most_recent (const char *file_name, GtkRecentManager *rm);
614 static void delete_recent (const char *file_name, GtkRecentManager *rm);
617 psppire_window_load (PsppireWindow *w, const gchar *file)
620 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
622 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
626 g_return_val_if_fail (i->load, FALSE);
628 ok = i->load (w, file);
632 psppire_window_set_filename (w, file);
633 add_most_recent (file, the_recent_mgr);
637 delete_recent (file, the_recent_mgr);
639 psppire_window_set_title (w);
645 /* Puts FILE_NAME into the recent list.
646 If it's already in the list, it moves it to the top
649 add_most_recent (const char *file_name, GtkRecentManager *rm)
651 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
654 gtk_recent_manager_add_item (rm, uri);
662 If FILE_NAME exists in the recent list, then delete it.
665 delete_recent (const char *file_name, GtkRecentManager *rm)
667 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
670 gtk_recent_manager_remove_item (rm, uri, NULL);