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/>. */
26 #define _(msgid) gettext (msgid)
27 #define N_(msgid) msgid
29 #include "psppire-window.h"
30 #include "psppire-window-register.h"
31 #include "psppire-conf.h"
33 static void psppire_window_base_finalize (PsppireWindowClass *, gpointer);
34 static void psppire_window_base_init (PsppireWindowClass *class);
35 static void psppire_window_class_init (PsppireWindowClass *class);
36 static void psppire_window_init (PsppireWindow *window);
39 static GObjectClass *parent_class;
42 psppire_window_get_type (void)
44 static GType psppire_window_type = 0;
46 if (!psppire_window_type)
48 static const GTypeInfo psppire_window_info =
50 sizeof (PsppireWindowClass),
51 (GBaseInitFunc) psppire_window_base_init,
52 (GBaseFinalizeFunc) psppire_window_base_finalize,
53 (GClassInitFunc) psppire_window_class_init,
54 (GClassFinalizeFunc) NULL,
56 sizeof (PsppireWindow),
58 (GInstanceInitFunc) psppire_window_init,
62 g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow",
63 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
66 return psppire_window_type;
80 uniquify (const gchar *str, int *x)
82 return g_strdup_printf ("%s%d", str, (*x)++);
86 psppire_window_set_title (PsppireWindow *window)
88 GString *title = g_string_sized_new (80);
90 g_string_printf (title, "%s ", window->basename ? window->basename : "");
91 g_string_append_unichar (title, 0x2014); /* em dash */
92 g_string_printf (title, " PSPPIRE %s", window->description);
95 g_string_prepend_c (title, '*');
97 gtk_window_set_title (GTK_WINDOW (window), title->str);
99 g_string_free (title, TRUE);
103 psppire_window_set_property (GObject *object,
108 PsppireWindow *window = PSPPIRE_WINDOW (object);
112 case PROP_DESCRIPTION:
113 window->description = g_value_dup_string (value);
114 psppire_window_set_title (window);
118 PsppireWindowRegister *reg = psppire_window_register_new ();
120 gchar *candidate_name ;
123 const gchar *name = g_value_get_string (value);
126 g_value_init (&def, pspec->value_type);
130 g_param_value_set_default (pspec, &def);
131 name = g_value_get_string (&def);
134 candidate_name = xstrdup (name);
136 while ( psppire_window_register_lookup (reg, candidate_name))
138 free (candidate_name);
139 candidate_name = uniquify (name, &x);
142 window->basename = g_filename_display_basename (candidate_name);
144 g_value_unset (&def);
147 psppire_window_set_title (window);
150 psppire_window_register_remove (reg, window->name);
153 window->name = candidate_name;
155 psppire_window_register_insert (reg, window, window->name);
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 psppire_window_get_property (GObject *object,
171 PsppireWindow *window = PSPPIRE_WINDOW (object);
176 g_value_set_string (value, window->name);
178 case PROP_DESCRIPTION:
179 g_value_set_string (value, window->description);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 on_realize (GtkWindow *window, gpointer data)
191 PsppireConf *conf = psppire_conf_new ();
193 const gchar *base = G_OBJECT_TYPE_NAME (window);
195 psppire_conf_set_window_geometry (conf, base, window);
200 psppire_window_finalize (GObject *object)
202 PsppireWindow *window = PSPPIRE_WINDOW (object);
204 PsppireWindowRegister *reg = psppire_window_register_new ();
206 psppire_window_register_remove (reg, window->name);
208 free (window->description);
210 g_signal_handler_disconnect (psppire_window_register_new (),
211 window->remove_handler);
213 g_signal_handler_disconnect (psppire_window_register_new (),
214 window->insert_handler);
216 g_hash_table_destroy (window->menuitem_table);
218 if (G_OBJECT_CLASS (parent_class)->finalize)
219 G_OBJECT_CLASS (parent_class)->finalize (object);
224 psppire_window_class_init (PsppireWindowClass *class)
226 GObjectClass *object_class = G_OBJECT_CLASS (class);
228 GParamSpec *description_spec =
229 g_param_spec_string ("description",
231 "A string describing the usage of the window",
232 "??????", /*Should be overridden by derived classes */
233 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
235 GParamSpec *filename_spec =
236 g_param_spec_string ("filename",
238 "The name of the file associated with this window, if any",
239 /* TRANSLATORS: This will form a filename. Please avoid whitespace. */
241 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
243 object_class->set_property = psppire_window_set_property;
244 object_class->get_property = psppire_window_get_property;
246 g_object_class_install_property (object_class,
250 g_object_class_install_property (object_class,
254 parent_class = g_type_class_peek_parent (class);
259 psppire_window_base_init (PsppireWindowClass *class)
261 GObjectClass *object_class = G_OBJECT_CLASS (class);
263 object_class->finalize = psppire_window_finalize;
269 psppire_window_base_finalize (PsppireWindowClass *class,
275 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
277 /* Prohibit changes to the state */
278 mi->active = !mi->active;
282 /* Look up the window associated with this menuitem and present it to the user */
284 menu_activate (GtkMenuItem *mi, gpointer data)
286 const gchar *key = data;
288 PsppireWindowRegister *reg = psppire_window_register_new ();
290 PsppireWindow *window = psppire_window_register_lookup (reg, key);
292 gtk_window_present (GTK_WINDOW (window));
296 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
301 /* Add a separator before adding the first real item. If we add a separator
302 at any other time, sometimes GtkUIManager removes it. */
303 if (g_hash_table_size (window->menuitem_table) == 0)
305 GtkWidget *separator = gtk_separator_menu_item_new ();
306 gtk_widget_show (separator);
307 gtk_menu_shell_append (window->menu, separator);
310 filename = g_filename_display_name (key);
311 item = gtk_check_menu_item_new_with_label (filename);
314 g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
315 g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
317 gtk_widget_show (item);
319 gtk_menu_shell_append (window->menu, item);
321 /* Set the state without emitting a signal */
322 GTK_CHECK_MENU_ITEM (item)->active =
323 (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
325 g_hash_table_insert (window->menuitem_table, key, item);
329 insert_item (gpointer key, gpointer value, gpointer data)
331 PsppireWindow *window = PSPPIRE_WINDOW (data);
333 if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
336 insert_menuitem_into_menu (window, key);
339 /* Insert a new item into the window menu */
341 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
343 PsppireWindow *window = PSPPIRE_WINDOW (data);
345 insert_menuitem_into_menu (window, (gpointer) key);
350 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
352 PsppireWindow *window = PSPPIRE_WINDOW (data);
355 item = g_hash_table_lookup (window->menuitem_table, key);
357 g_hash_table_remove (window->menuitem_table, key);
359 if (GTK_IS_CONTAINER (window->menu))
360 gtk_container_remove (GTK_CONTAINER (window->menu), item);
364 insert_existing_items (PsppireWindow *window)
366 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
371 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
373 PsppireWindowRegister *reg = psppire_window_register_new ();
375 const gchar *base = G_OBJECT_TYPE_NAME (w);
377 PsppireConf *conf = psppire_conf_new ();
379 psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (w));
384 gint response = psppire_window_query_save (w);
389 case GTK_RESPONSE_CANCEL:
392 case GTK_RESPONSE_APPLY:
393 psppire_window_save (w);
395 case GTK_RESPONSE_REJECT:
400 if ( 1 == psppire_window_register_n_items (reg))
408 psppire_window_init (PsppireWindow *window)
412 window->description = xstrdup ("");
414 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
417 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
419 window->insert_handler = g_signal_connect (psppire_window_register_new (),
421 G_CALLBACK (insert_menuitem),
424 window->remove_handler = g_signal_connect (psppire_window_register_new (),
426 G_CALLBACK (remove_menuitem),
429 window->dirty = FALSE;
431 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
433 g_object_set (window, "icon-name", "psppicon", NULL);
435 g_signal_connect (window, "realize",
436 G_CALLBACK (on_realize), window);
441 Ask the user if the buffer should be saved.
445 psppire_window_query_save (PsppireWindow *se)
450 GtkWidget *cancel_button;
452 const gchar *description;
453 const gchar *filename = psppire_window_get_filename (se);
457 g_get_current_time (&time);
459 g_object_get (se, "description", &description, NULL);
461 g_return_val_if_fail (filename != NULL, GTK_RESPONSE_NONE);
464 fn = g_filename_display_basename (filename);
467 gtk_message_dialog_new (GTK_WINDOW (se),
471 _("Save the changes to `%s' before closing?"),
475 g_object_set (dialog, "icon-name", "psppicon", NULL);
477 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
478 _("If you don't save, changes from the last %ld seconds will be permanently lost."),
479 time.tv_sec - se->savetime.tv_sec);
481 gtk_dialog_add_button (GTK_DIALOG (dialog),
482 _("Close _without saving"),
483 GTK_RESPONSE_REJECT);
485 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
487 GTK_RESPONSE_CANCEL);
489 gtk_dialog_add_button (GTK_DIALOG (dialog),
493 gtk_widget_grab_focus (cancel_button);
495 response = gtk_dialog_run (GTK_DIALOG (dialog));
497 gtk_widget_destroy (dialog);
505 psppire_window_get_filename (PsppireWindow *w)
507 const gchar *name = NULL;
508 g_object_get (w, "filename", &name, NULL);
514 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
516 g_object_set (w, "filename", filename, NULL);
520 psppire_window_set_unsaved (PsppireWindow *w)
522 if ( w->dirty == FALSE)
523 g_get_current_time (&w->savetime);
527 psppire_window_set_title (w);
531 psppire_window_get_unsaved (PsppireWindow *w)
541 minimise_window (gpointer key, gpointer value, gpointer data)
543 gtk_window_iconify (GTK_WINDOW (value));
548 psppire_window_minimise_all (void)
550 PsppireWindowRegister *reg = psppire_window_register_new ();
552 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
559 psppire_window_model_get_type (void)
561 static GType window_model_type = 0;
563 if (! window_model_type)
565 static const GTypeInfo window_model_info =
567 sizeof (PsppireWindowIface), /* class_size */
568 NULL, /* base_init */
569 NULL, /* base_finalize */
571 NULL, /* class_finalize */
572 NULL, /* class_data */
579 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
580 &window_model_info, 0);
582 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
585 return window_model_type;
590 psppire_window_save (PsppireWindow *w)
592 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
594 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
598 g_return_if_fail (i->save);
603 psppire_window_set_title (w);
606 extern GtkRecentManager *the_recent_mgr;
608 static void add_most_recent (const char *file_name, GtkRecentManager *rm);
609 static void delete_recent (const char *file_name, GtkRecentManager *rm);
612 psppire_window_load (PsppireWindow *w, const gchar *file)
615 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
617 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
621 g_return_val_if_fail (i->load, FALSE);
623 ok = i->load (w, file);
627 psppire_window_set_filename (w, file);
628 add_most_recent (file, the_recent_mgr);
632 delete_recent (file, the_recent_mgr);
634 psppire_window_set_title (w);
640 /* Puts FILE_NAME into the recent list.
641 If it's already in the list, it moves it to the top
644 add_most_recent (const char *file_name, GtkRecentManager *rm)
646 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
649 gtk_recent_manager_add_item (rm, uri);
657 If FILE_NAME exists in the recent list, then delete it.
660 delete_recent (const char *file_name, GtkRecentManager *rm)
662 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
665 gtk_recent_manager_remove_item (rm, uri, NULL);