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>
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)++);
90 static gchar mdash[6] = {0,0,0,0,0,0};
93 psppire_window_set_title (PsppireWindow *window)
95 GString *title = g_string_sized_new (80);
97 g_string_printf (title, _("%s %s PSPPIRE %s"),
98 window->basename ? window->basename : "",
99 mdash, window->description);
102 g_string_prepend_c (title, '*');
104 gtk_window_set_title (GTK_WINDOW (window), title->str);
106 g_string_free (title, TRUE);
110 psppire_window_set_property (GObject *object,
115 PsppireWindow *window = PSPPIRE_WINDOW (object);
119 case PROP_DESCRIPTION:
120 window->description = g_value_dup_string (value);
121 psppire_window_set_title (window);
125 PsppireWindowRegister *reg = psppire_window_register_new ();
127 gchar *candidate_name ;
130 const gchar *name = g_value_get_string (value);
133 g_value_init (&def, pspec->value_type);
137 g_param_value_set_default (pspec, &def);
138 name = g_value_get_string (&def);
141 candidate_name = xstrdup (name);
143 while ( psppire_window_register_lookup (reg, candidate_name))
145 free (candidate_name);
146 candidate_name = uniquify (name, &x);
149 window->basename = g_filename_display_basename (candidate_name);
151 g_value_unset (&def);
154 psppire_window_set_title (window);
157 psppire_window_register_remove (reg, window->name);
160 window->name = candidate_name;
162 psppire_window_register_insert (reg, window, window->name);
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173 psppire_window_get_property (GObject *object,
178 PsppireWindow *window = PSPPIRE_WINDOW (object);
183 g_value_set_string (value, window->name);
185 case PROP_DESCRIPTION:
186 g_value_set_string (value, window->description);
189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196 on_realize (GtkWindow *window, gpointer data)
198 PsppireConf *conf = psppire_conf_new ();
200 const gchar *base = G_OBJECT_TYPE_NAME (window);
202 psppire_conf_set_window_geometry (conf, base, window);
207 save_geometry (GtkWidget *window, GdkEvent *event, gpointer data)
209 const gchar *base = G_OBJECT_TYPE_NAME (window);
211 PsppireConf *conf = psppire_conf_new ();
213 psppire_conf_save_window_geometry (conf, base, event);
220 psppire_window_finalize (GObject *object)
222 PsppireWindow *window = PSPPIRE_WINDOW (object);
224 PsppireWindowRegister *reg = psppire_window_register_new ();
226 psppire_window_register_remove (reg, window->name);
228 free (window->description);
230 g_signal_handler_disconnect (psppire_window_register_new (),
231 window->remove_handler);
233 g_signal_handler_disconnect (psppire_window_register_new (),
234 window->insert_handler);
236 g_hash_table_destroy (window->menuitem_table);
238 if (G_OBJECT_CLASS (parent_class)->finalize)
239 G_OBJECT_CLASS (parent_class)->finalize (object);
244 psppire_window_class_init (PsppireWindowClass *class)
246 GObjectClass *object_class = G_OBJECT_CLASS (class);
248 GParamSpec *description_spec =
249 g_param_spec_string ("description",
251 "A string describing the usage of the window",
252 "??????", /*Should be overridden by derived classes */
253 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
255 GParamSpec *filename_spec =
256 g_param_spec_string ("filename",
258 "The name of the file associated with this window, if any",
260 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
262 g_unichar_to_utf8 (0x2014, mdash);
264 object_class->set_property = psppire_window_set_property;
265 object_class->get_property = psppire_window_get_property;
267 g_object_class_install_property (object_class,
271 g_object_class_install_property (object_class,
275 parent_class = g_type_class_peek_parent (class);
280 psppire_window_base_init (PsppireWindowClass *class)
282 GObjectClass *object_class = G_OBJECT_CLASS (class);
284 object_class->finalize = psppire_window_finalize;
290 psppire_window_base_finalize (PsppireWindowClass *class,
296 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
298 /* Prohibit changes to the state */
299 mi->active = !mi->active;
303 /* Look up the window associated with this menuitem and present it to the user */
305 menu_activate (GtkMenuItem *mi, gpointer data)
307 const gchar *key = data;
309 PsppireWindowRegister *reg = psppire_window_register_new ();
311 PsppireWindow *window = psppire_window_register_lookup (reg, key);
313 gtk_window_present (GTK_WINDOW (window));
317 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
319 gchar *filename = g_filename_display_name (key);
320 GtkWidget *item = gtk_check_menu_item_new_with_label (filename);
324 g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
325 g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
327 gtk_widget_show (item);
329 gtk_menu_shell_append (window->menu, item);
331 /* Set the state without emitting a signal */
332 GTK_CHECK_MENU_ITEM (item)->active =
333 (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
335 g_hash_table_insert (window->menuitem_table, key, item);
339 insert_item (gpointer key, gpointer value, gpointer data)
341 PsppireWindow *window = PSPPIRE_WINDOW (data);
343 if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
346 insert_menuitem_into_menu (window, key);
349 /* Insert a new item into the window menu */
351 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
353 PsppireWindow *window = PSPPIRE_WINDOW (data);
355 insert_menuitem_into_menu (window, (gpointer) key);
360 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
362 PsppireWindow *window = PSPPIRE_WINDOW (data);
365 item = g_hash_table_lookup (window->menuitem_table, key);
367 g_hash_table_remove (window->menuitem_table, key);
369 if (GTK_IS_CONTAINER (window->menu))
370 gtk_container_remove (GTK_CONTAINER (window->menu), item);
374 insert_existing_items (PsppireWindow *window)
376 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
381 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
383 PsppireWindowRegister *reg = psppire_window_register_new ();
387 gint response = psppire_window_query_save (w);
392 case GTK_RESPONSE_CANCEL:
395 case GTK_RESPONSE_APPLY:
396 psppire_window_save (w);
398 case GTK_RESPONSE_REJECT:
403 if ( 1 == psppire_window_register_n_items (reg))
411 psppire_window_init (PsppireWindow *window)
415 window->description = xstrdup ("");
417 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
420 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
422 window->insert_handler = g_signal_connect (psppire_window_register_new (),
424 G_CALLBACK (insert_menuitem),
427 window->remove_handler = g_signal_connect (psppire_window_register_new (),
429 G_CALLBACK (remove_menuitem),
432 window->dirty = FALSE;
434 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
436 g_object_set (window, "icon-name", "psppicon", NULL);
438 g_signal_connect (window, "configure-event",
439 G_CALLBACK (save_geometry), window);
441 g_signal_connect (window, "window-state-event",
442 G_CALLBACK (save_geometry), window);
444 g_signal_connect (window, "realize",
445 G_CALLBACK (on_realize), window);
450 Ask the user if the buffer should be saved.
454 psppire_window_query_save (PsppireWindow *se)
459 GtkWidget *cancel_button;
461 const gchar *description;
462 const gchar *filename = psppire_window_get_filename (se);
466 g_get_current_time (&time);
468 g_object_get (se, "description", &description, NULL);
470 g_return_val_if_fail (filename != NULL, GTK_RESPONSE_NONE);
473 fn = g_filename_display_basename (filename);
476 gtk_message_dialog_new (GTK_WINDOW (se),
480 _("Save the changes to \"%s\" before closing?"),
484 g_object_set (dialog, "icon-name", "psppicon", NULL);
486 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
487 _("If you don't save, changes from the last %ld seconds will be permanently lost."),
488 time.tv_sec - se->savetime.tv_sec);
490 gtk_dialog_add_button (GTK_DIALOG (dialog),
491 _("Close _without saving"),
492 GTK_RESPONSE_REJECT);
494 cancel_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
496 GTK_RESPONSE_CANCEL);
498 gtk_dialog_add_button (GTK_DIALOG (dialog),
502 gtk_widget_grab_focus (cancel_button);
504 response = gtk_dialog_run (GTK_DIALOG (dialog));
506 gtk_widget_destroy (dialog);
514 psppire_window_get_filename (PsppireWindow *w)
516 const gchar *name = NULL;
517 g_object_get (w, "filename", &name, NULL);
523 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
525 g_object_set (w, "filename", filename, NULL);
529 psppire_window_set_unsaved (PsppireWindow *w)
531 if ( w->dirty == FALSE)
532 g_get_current_time (&w->savetime);
536 psppire_window_set_title (w);
540 psppire_window_get_unsaved (PsppireWindow *w)
550 minimise_window (gpointer key, gpointer value, gpointer data)
552 gtk_window_iconify (GTK_WINDOW (value));
557 psppire_window_minimise_all (void)
559 PsppireWindowRegister *reg = psppire_window_register_new ();
561 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
568 psppire_window_model_get_type (void)
570 static GType window_model_type = 0;
572 if (! window_model_type)
574 static const GTypeInfo window_model_info =
576 sizeof (PsppireWindowIface), /* class_size */
577 NULL, /* base_init */
578 NULL, /* base_finalize */
580 NULL, /* class_finalize */
581 NULL, /* class_data */
588 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
589 &window_model_info, 0);
591 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
594 return window_model_type;
599 psppire_window_save (PsppireWindow *w)
601 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
603 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
607 g_return_if_fail (i->save);
612 psppire_window_set_title (w);
615 extern GtkRecentManager *the_recent_mgr;
617 static void add_most_recent (const char *file_name, GtkRecentManager *rm);
618 static void delete_recent (const char *file_name, GtkRecentManager *rm);
621 psppire_window_load (PsppireWindow *w, const gchar *file)
624 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
626 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
630 g_return_val_if_fail (i->load, FALSE);
632 ok = i->load (w, file);
636 psppire_window_set_filename (w, file);
637 add_most_recent (file, the_recent_mgr);
641 delete_recent (file, the_recent_mgr);
643 psppire_window_set_title (w);
649 /* Puts FILE_NAME into the recent list.
650 If it's already in the list, it moves it to the top
653 add_most_recent (const char *file_name, GtkRecentManager *rm)
655 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
658 gtk_recent_manager_add_item (rm, uri);
666 If FILE_NAME exists in the recent list, then delete it.
669 delete_recent (const char *file_name, GtkRecentManager *rm)
671 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
674 gtk_recent_manager_remove_item (rm, uri, NULL);