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/>. */
19 #include <gtk/gtkstock.h>
20 #include <gtk/gtkmessagedialog.h>
21 #include <gtk/gtksignal.h>
22 #include <gtk/gtkwindow.h>
23 #include <gtk/gtkcheckmenuitem.h>
24 #include <gtk/gtkmain.h>
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
32 #include "psppire-window.h"
33 #include "psppire-window-register.h"
35 static void psppire_window_base_finalize (PsppireWindowClass *, gpointer);
36 static void psppire_window_base_init (PsppireWindowClass *class);
37 static void psppire_window_class_init (PsppireWindowClass *class);
38 static void psppire_window_init (PsppireWindow *window);
41 static PsppireWindowClass *the_class;
42 static GObjectClass *parent_class;
45 psppire_window_get_type (void)
47 static GType psppire_window_type = 0;
49 if (!psppire_window_type)
51 static const GTypeInfo psppire_window_info =
53 sizeof (PsppireWindowClass),
54 (GBaseInitFunc) psppire_window_base_init,
55 (GBaseFinalizeFunc) psppire_window_base_finalize,
56 (GClassInitFunc) psppire_window_class_init,
57 (GClassFinalizeFunc) NULL,
59 sizeof (PsppireWindow),
61 (GInstanceInitFunc) psppire_window_init,
65 g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow",
66 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
69 return psppire_window_type;
83 uniquify (const gchar *str, int *x)
85 return g_strdup_printf ("%s%d", str, (*x)++);
88 static gchar mdash[6] = {0,0,0,0,0,0};
91 psppire_window_set_title (PsppireWindow *window)
93 GString *title = g_string_sized_new (80);
95 g_string_printf (title, _("%s %s PSPPIRE %s"),
96 window->basename ? window->basename : "",
97 mdash, 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 = strdup (name);
141 while ( psppire_window_register_lookup (reg, candidate_name))
143 free (candidate_name);
144 candidate_name = uniquify (name, &x);
147 window->basename = g_path_get_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);
195 psppire_window_finalize (GObject *object)
197 PsppireWindow *window = PSPPIRE_WINDOW (object);
199 PsppireWindowRegister *reg = psppire_window_register_new ();
201 psppire_window_register_remove (reg, window->name);
203 free (window->description);
205 g_signal_handler_disconnect (psppire_window_register_new (),
206 window->remove_handler);
208 g_signal_handler_disconnect (psppire_window_register_new (),
209 window->insert_handler);
211 g_hash_table_destroy (window->menuitem_table);
213 if (G_OBJECT_CLASS (parent_class)->finalize)
214 G_OBJECT_CLASS (parent_class)->finalize (object);
219 psppire_window_class_init (PsppireWindowClass *class)
221 GObjectClass *object_class = G_OBJECT_CLASS (class);
223 GParamSpec *description_spec =
224 g_param_spec_string ("description",
226 "A string describing the usage of the window",
227 "??????", /*Should be overridden by derived classes */
228 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
230 GParamSpec *filename_spec =
231 g_param_spec_string ("filename",
233 "The name of the file associated with this window, if any",
235 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
237 g_unichar_to_utf8 (0x2014, mdash);
239 object_class->set_property = psppire_window_set_property;
240 object_class->get_property = psppire_window_get_property;
242 g_object_class_install_property (object_class,
246 g_object_class_install_property (object_class,
251 parent_class = g_type_class_peek_parent (class);
256 psppire_window_base_init (PsppireWindowClass *class)
258 GObjectClass *object_class = G_OBJECT_CLASS (class);
260 object_class->finalize = psppire_window_finalize;
266 psppire_window_base_finalize (PsppireWindowClass *class,
272 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
274 /* Prohibit changes to the state */
275 mi->active = !mi->active;
279 /* Look up the window associated with this menuitem and present it to the user */
281 menu_activate (GtkMenuItem *mi, gpointer data)
283 const gchar *key = data;
285 PsppireWindowRegister *reg = psppire_window_register_new ();
287 PsppireWindow *window = psppire_window_register_lookup (reg, key);
289 gtk_window_present (GTK_WINDOW (window));
293 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
295 GtkWidget *item = gtk_check_menu_item_new_with_label (key);
297 g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
298 g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
300 gtk_widget_show (item);
302 gtk_menu_shell_append (window->menu, item);
304 /* Set the state without emitting a signal */
305 GTK_CHECK_MENU_ITEM (item)->active =
306 (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
308 g_hash_table_insert (window->menuitem_table, key, item);
312 insert_item (gpointer key, gpointer value, gpointer data)
314 PsppireWindow *window = PSPPIRE_WINDOW (data);
316 if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
319 insert_menuitem_into_menu (window, key);
322 /* Insert a new item into the window menu */
324 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
326 PsppireWindow *window = PSPPIRE_WINDOW (data);
328 insert_menuitem_into_menu (window, (gpointer) key);
333 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
335 PsppireWindow *window = PSPPIRE_WINDOW (data);
338 item = g_hash_table_lookup (window->menuitem_table, key);
340 g_hash_table_remove (window->menuitem_table, key);
342 if (GTK_IS_CONTAINER (window->menu))
343 gtk_container_remove (GTK_CONTAINER (window->menu), item);
347 insert_existing_items (PsppireWindow *window)
349 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
354 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
356 PsppireWindowRegister *reg = psppire_window_register_new ();
360 gint response = psppire_window_query_save (w);
362 if ( response == GTK_RESPONSE_CANCEL)
365 if ( response == GTK_RESPONSE_ACCEPT)
367 psppire_window_save (w);
371 if ( 1 == psppire_window_register_n_items (reg))
379 psppire_window_init (PsppireWindow *window)
384 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
387 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
389 window->insert_handler = g_signal_connect (psppire_window_register_new (),
391 G_CALLBACK (insert_menuitem),
394 window->remove_handler = g_signal_connect (psppire_window_register_new (),
396 G_CALLBACK (remove_menuitem),
399 window->unsaved = FALSE;
401 g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
403 g_object_set (window, "icon-name", "psppicon", NULL);
409 Ask the user if the buffer should be saved.
413 psppire_window_query_save (PsppireWindow *se)
418 const gchar *description;
419 const gchar *filename = psppire_window_get_filename (se);
421 g_object_get (se, "description", &description, NULL);
423 g_return_val_if_fail (filename != NULL, GTK_RESPONSE_NONE);
426 gtk_message_dialog_new (GTK_WINDOW (se),
428 GTK_MESSAGE_QUESTION,
430 _("Save contents of %s to \"%s\"?"),
434 gtk_dialog_add_button (GTK_DIALOG (dialog),
436 GTK_RESPONSE_ACCEPT);
438 gtk_dialog_add_button (GTK_DIALOG (dialog),
440 GTK_RESPONSE_REJECT);
442 gtk_dialog_add_button (GTK_DIALOG (dialog),
444 GTK_RESPONSE_CANCEL);
446 response = gtk_dialog_run (GTK_DIALOG (dialog));
448 gtk_widget_destroy (dialog);
455 psppire_window_get_filename (PsppireWindow *w)
457 const gchar *name = NULL;
458 g_object_get (w, "filename", &name, NULL);
464 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
466 g_object_set (w, "filename", filename, NULL);
470 psppire_window_set_unsaved (PsppireWindow *w, gboolean unsaved)
472 w->unsaved = unsaved;
474 psppire_window_set_title (w);
478 psppire_window_get_unsaved (PsppireWindow *w)
488 minimise_window (gpointer key, gpointer value, gpointer data)
490 gtk_window_iconify (GTK_WINDOW (value));
495 psppire_window_minimise_all (void)
497 PsppireWindowRegister *reg = psppire_window_register_new ();
499 g_hash_table_foreach (reg->name_table, minimise_window, NULL);
506 psppire_window_model_get_type (void)
508 static GType window_model_type = 0;
510 if (! window_model_type)
512 static const GTypeInfo window_model_info =
514 sizeof (PsppireWindowIface), /* class_size */
515 NULL, /* base_init */
516 NULL, /* base_finalize */
518 NULL, /* class_finalize */
519 NULL, /* class_data */
526 g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
527 &window_model_info, 0);
529 g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
532 return window_model_type;
537 psppire_window_save (PsppireWindow *w)
539 PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
541 g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
545 g_return_if_fail (i->save);
551 /* Puts FILE_NAME into the recent list.
552 If it's already in the list, it moves it to the top
555 add_most_recent (const char *file_name, GtkRecentManager *rm)
557 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
560 gtk_recent_manager_add_item (rm, uri);