1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2009 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>
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
33 #include "psppire-window.h"
34 #include "psppire-window-register.h"
35 #include "psppire-conf.h"
37 static void psppire_window_base_finalize (PsppireWindowClass *, gpointer);
38 static void psppire_window_base_init (PsppireWindowClass *class);
39 static void psppire_window_class_init (PsppireWindowClass *class);
40 static void psppire_window_init (PsppireWindow *window);
43 static GObjectClass *parent_class;
46 psppire_window_get_type (void)
48 static GType psppire_window_type = 0;
50 if (!psppire_window_type)
52 static const GTypeInfo psppire_window_info =
54 sizeof (PsppireWindowClass),
55 (GBaseInitFunc) psppire_window_base_init,
56 (GBaseFinalizeFunc) psppire_window_base_finalize,
57 (GClassInitFunc) psppire_window_class_init,
58 (GClassFinalizeFunc) NULL,
60 sizeof (PsppireWindow),
62 (GInstanceInitFunc) psppire_window_init,
66 g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow",
67 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
70 return psppire_window_type;
84 uniquify (const gchar *str, int *x)
86 return g_strdup_printf ("%s%d", str, (*x)++);
89 static gchar mdash[6] = {0,0,0,0,0,0};
92 psppire_window_set_title (PsppireWindow *window)
94 GString *title = g_string_sized_new (80);
96 g_string_printf (title, _("%s %s PSPPIRE %s"),
97 window->basename ? window->basename : "",
98 mdash, window->description);
101 g_string_prepend_c (title, '*');
103 gtk_window_set_title (GTK_WINDOW (window), title->str);
105 g_string_free (title, TRUE);
109 psppire_window_set_property (GObject *object,
114 PsppireWindow *window = PSPPIRE_WINDOW (object);
118 case PROP_DESCRIPTION:
119 window->description = g_value_dup_string (value);
120 psppire_window_set_title (window);
124 PsppireWindowRegister *reg = psppire_window_register_new ();
126 gchar *candidate_name ;
129 const gchar *name = g_value_get_string (value);
132 g_value_init (&def, pspec->value_type);
136 g_param_value_set_default (pspec, &def);
137 name = g_value_get_string (&def);
140 candidate_name = xstrdup (name);
142 while ( psppire_window_register_lookup (reg, candidate_name))
144 free (candidate_name);
145 candidate_name = uniquify (name, &x);
148 window->basename = g_filename_display_basename (candidate_name);
150 g_value_unset (&def);
153 psppire_window_set_title (window);
156 psppire_window_register_remove (reg, window->name);
159 window->name = candidate_name;
161 psppire_window_register_insert (reg, window, window->name);
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172 psppire_window_get_property (GObject *object,
177 PsppireWindow *window = PSPPIRE_WINDOW (object);
182 g_value_set_string (value, window->name);
184 case PROP_DESCRIPTION:
185 g_value_set_string (value, window->description);
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195 on_realize (GtkWindow *window, gpointer data)
197 PsppireConf *conf = psppire_conf_new ();
199 const gchar *base = G_OBJECT_TYPE_NAME (window);
201 psppire_conf_set_window_geometry (conf, base, window);
206 save_geometry (GtkWidget *window, GdkEvent *event, gpointer data)
208 const gchar *base = G_OBJECT_TYPE_NAME (window);
210 PsppireConf *conf = psppire_conf_new ();
212 psppire_conf_save_window_geometry (conf, base, event);
219 psppire_window_finalize (GObject *object)
221 PsppireWindow *window = PSPPIRE_WINDOW (object);
223 PsppireWindowRegister *reg = psppire_window_register_new ();
225 psppire_window_register_remove (reg, window->name);
227 free (window->description);
229 g_signal_handler_disconnect (psppire_window_register_new (),
230 window->remove_handler);
232 g_signal_handler_disconnect (psppire_window_register_new (),
233 window->insert_handler);
235 g_hash_table_destroy (window->menuitem_table);
237 if (G_OBJECT_CLASS (parent_class)->finalize)
238 G_OBJECT_CLASS (parent_class)->finalize (object);
243 psppire_window_class_init (PsppireWindowClass *class)
245 GObjectClass *object_class = G_OBJECT_CLASS (class);
247 GParamSpec *description_spec =
248 g_param_spec_string ("description",
250 "A string describing the usage of the window",
251 "??????", /*Should be overridden by derived classes */
252 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
254 GParamSpec *filename_spec =
255 g_param_spec_string ("filename",
257 "The name of the file associated with this window, if any",
259 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
261 g_unichar_to_utf8 (0x2014, mdash);
263 object_class->set_property = psppire_window_set_property;
264 object_class->get_property = psppire_window_get_property;
266 g_object_class_install_property (object_class,
270 g_object_class_install_property (object_class,
274 parent_class = g_type_class_peek_parent (class);
279 psppire_window_base_init (PsppireWindowClass *class)
281 GObjectClass *object_class = G_OBJECT_CLASS (class);
283 object_class->finalize = psppire_window_finalize;
289 psppire_window_base_finalize (PsppireWindowClass *class,
295 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
297 /* Prohibit changes to the state */
298 mi->active = !mi->active;
302 /* Look up the window associated with this menuitem and present it to the user */
304 menu_activate (GtkMenuItem *mi, gpointer data)
306 const gchar *key = data;
308 PsppireWindowRegister *reg = psppire_window_register_new ();
310 PsppireWindow *window = psppire_window_register_lookup (reg, key);
312 gtk_window_present (GTK_WINDOW (window));
316 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
318 gchar *filename = g_filename_display_name (key);
319 GtkWidget *item = gtk_check_menu_item_new_with_label (filename);
323 g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
324 g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
326 gtk_widget_show (item);
328 gtk_menu_shell_append (window->menu, item);
330 /* Set the state without emitting a signal */
331 GTK_CHECK_MENU_ITEM (item)->active =
332 (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
334 g_hash_table_insert (window->menuitem_table, key, item);
338 insert_item (gpointer key, gpointer value, gpointer data)
340 PsppireWindow *window = PSPPIRE_WINDOW (data);
342 if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
345 insert_menuitem_into_menu (window, key);
348 /* Insert a new item into the window menu */
350 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
352 PsppireWindow *window = PSPPIRE_WINDOW (data);
354 insert_menuitem_into_menu (window, (gpointer) key);
359 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
361 PsppireWindow *window = PSPPIRE_WINDOW (data);
364 item = g_hash_table_lookup (window->menuitem_table, key);
366 g_hash_table_remove (window->menuitem_table, key);
368 if (GTK_IS_CONTAINER (window->menu))
369 gtk_container_remove (GTK_CONTAINER (window->menu), item);
373 insert_existing_items (PsppireWindow *window)
375 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
380 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
382 PsppireWindowRegister *reg = psppire_window_register_new ();
386 gint response = psppire_window_query_save (w);
391 case GTK_RESPONSE_CANCEL:
394 case GTK_RESPONSE_APPLY:
395 psppire_window_save (w);
397 case GTK_RESPONSE_REJECT:
402 if ( 1 == psppire_window_register_n_items (reg))
410 psppire_window_init (PsppireWindow *window)
414 window->description = xstrdup ("");
416 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
419 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
421 window->insert_handler = g_signal_connect (psppire_window_register_new (),
423 G_CALLBACK (insert_menuitem),
426 window->remove_handler = g_signal_connect (psppire_window_register_new (),
428 G_CALLBACK (remove_menuitem),
431 window->dirty = FALSE;
433 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
435 g_object_set (window, "icon-name", "psppicon", NULL);
437 g_signal_connect (window, "configure-event",
438 G_CALLBACK (save_geometry), window);
440 g_signal_connect (window, "window-state-event",
441 G_CALLBACK (save_geometry), window);
443 g_signal_connect (window, "realize",
444 G_CALLBACK (on_realize), window);
449 Ask the user if the buffer should be saved.
453 psppire_window_query_save (PsppireWindow *se)
458 GtkWidget *cancel_button;
460 const gchar *description;
461 const gchar *filename = psppire_window_get_filename (se);
465 g_get_current_time (&time);
467 g_object_get (se, "description", &description, NULL);
469 g_return_val_if_fail (filename != NULL, GTK_RESPONSE_NONE);
472 fn = g_filename_display_basename (filename);
475 gtk_message_dialog_new (GTK_WINDOW (se),
479 _("Save the changes to \"%s\" before closing?"),
483 g_object_set (dialog, "icon-name", "psppicon", NULL);
485 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
486 _("If you don't save, changes from the last %ld seconds will be permanently lost."),
487 time.tv_sec - se->savetime.tv_sec);
489 gtk_dialog_add_button (GTK_DIALOG (dialog),
490 _("Close _without saving"),
491 GTK_RESPONSE_REJECT);
493 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
495 GTK_RESPONSE_CANCEL);
497 gtk_dialog_add_button (GTK_DIALOG (dialog),
501 gtk_widget_grab_focus (cancel_button);
503 response = gtk_dialog_run (GTK_DIALOG (dialog));
505 gtk_widget_destroy (dialog);
513 psppire_window_get_filename (PsppireWindow *w)
515 const gchar *name = NULL;
516 g_object_get (w, "filename", &name, NULL);
522 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
524 g_object_set (w, "filename", filename, NULL);
528 psppire_window_set_unsaved (PsppireWindow *w)
530 if ( w->dirty == FALSE)
531 g_get_current_time (&w->savetime);
535 psppire_window_set_title (w);
539 psppire_window_get_unsaved (PsppireWindow *w)
549 minimise_window (gpointer key, gpointer value, gpointer data)
551 gtk_window_iconify (GTK_WINDOW (value));
556 psppire_window_minimise_all (void)
558 PsppireWindowRegister *reg = psppire_window_register_new ();
560 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
567 psppire_window_model_get_type (void)
569 static GType window_model_type = 0;
571 if (! window_model_type)
573 static const GTypeInfo window_model_info =
575 sizeof (PsppireWindowIface), /* class_size */
576 NULL, /* base_init */
577 NULL, /* base_finalize */
579 NULL, /* class_finalize */
580 NULL, /* class_data */
587 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
588 &window_model_info, 0);
590 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
593 return window_model_type;
598 psppire_window_save (PsppireWindow *w)
600 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
602 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
606 g_return_if_fail (i->save);
611 psppire_window_set_title (w);
614 extern GtkRecentManager *the_recent_mgr;
616 static void add_most_recent (const char *file_name, GtkRecentManager *rm);
617 static void delete_recent (const char *file_name, GtkRecentManager *rm);
620 psppire_window_load (PsppireWindow *w, const gchar *file)
623 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
625 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
629 g_return_val_if_fail (i->load, FALSE);
631 ok = i->load (w, file);
635 psppire_window_set_filename (w, file);
636 add_most_recent (file, the_recent_mgr);
640 delete_recent (file, the_recent_mgr);
642 psppire_window_set_title (w);
648 /* Puts FILE_NAME into the recent list.
649 If it's already in the list, it moves it to the top
652 add_most_recent (const char *file_name, GtkRecentManager *rm)
654 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
657 gtk_recent_manager_add_item (rm, uri);
665 If FILE_NAME exists in the recent list, then delete it.
668 delete_recent (const char *file_name, GtkRecentManager *rm)
670 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
673 gtk_recent_manager_remove_item (rm, uri, NULL);