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/gtksignal.h>
20 #include <gtk/gtkwindow.h>
21 #include <gtk/gtkcheckmenuitem.h>
26 #define _(msgid) gettext (msgid)
27 #define N_(msgid) msgid
29 #include "psppire-window.h"
30 #include "psppire-window-register.h"
32 static void psppire_window_base_finalize (PsppireWindowClass *, gpointer);
33 static void psppire_window_base_init (PsppireWindowClass *class);
34 static void psppire_window_class_init (PsppireWindowClass *class);
35 static void psppire_window_init (PsppireWindow *window);
38 static PsppireWindowClass *the_class;
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, 0);
66 return psppire_window_type;
80 uniquify (const gchar *str, int *x)
82 return g_strdup_printf ("%s%d", str, (*x)++);
85 static gchar mdash[6] = {0,0,0,0,0,0};
88 psppire_window_set_title (PsppireWindow *window)
90 GString *title = g_string_sized_new (80);
92 g_string_printf (title, _("%s %s PSPPIRE %s"),
93 window->basename, mdash, window->description);
96 g_string_prepend_c (title, '*');
98 gtk_window_set_title (GTK_WINDOW (window), title->str);
100 g_string_free (title, TRUE);
104 psppire_window_set_property (GObject *object,
109 PsppireWindow *window = PSPPIRE_WINDOW (object);
113 case PROP_DESCRIPTION:
114 window->description = g_value_dup_string (value);
115 psppire_window_set_title (window);
119 PsppireWindowRegister *reg = psppire_window_register_new ();
121 gchar *candidate_name ;
124 const gchar *name = g_value_get_string (value);
127 g_value_init (&def, pspec->value_type);
131 g_param_value_set_default (pspec, &def);
132 name = g_value_get_string (&def);
135 candidate_name = strdup (name);
137 while ( psppire_window_register_lookup (reg, candidate_name))
139 free (candidate_name);
140 candidate_name = uniquify (name, &x);
143 window->basename = g_path_get_basename (candidate_name);
145 g_value_unset (&def);
148 psppire_window_set_title (window);
151 psppire_window_register_remove (reg, window->name);
154 window->name = candidate_name;
156 psppire_window_register_insert (reg, window, window->name);
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 psppire_window_get_property (GObject *object,
172 PsppireWindow *window = PSPPIRE_WINDOW (object);
177 g_value_set_string (value, window->name);
179 case PROP_DESCRIPTION:
180 g_value_set_string (value, window->description);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 psppire_window_finalize (GObject *object)
193 PsppireWindow *window = PSPPIRE_WINDOW (object);
195 PsppireWindowRegister *reg = psppire_window_register_new ();
197 psppire_window_register_remove (reg, window->name);
199 free (window->description);
201 g_signal_handler_disconnect (psppire_window_register_new (),
202 window->remove_handler);
204 g_signal_handler_disconnect (psppire_window_register_new (),
205 window->insert_handler);
207 g_hash_table_destroy (window->menuitem_table);
209 if (G_OBJECT_CLASS (parent_class)->finalize)
210 G_OBJECT_CLASS (parent_class)->finalize (object);
215 psppire_window_class_init (PsppireWindowClass *class)
217 GObjectClass *object_class = G_OBJECT_CLASS (class);
219 GParamSpec *description_spec =
220 g_param_spec_string ("description",
222 "A string describing the usage of the window",
223 "??????", /*Should be overridden by derived classes */
224 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
226 GParamSpec *filename_spec =
227 g_param_spec_string ("filename",
229 "The name of the file associated with this window, if any",
231 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
233 g_unichar_to_utf8 (0x2014, mdash);
235 object_class->set_property = psppire_window_set_property;
236 object_class->get_property = psppire_window_get_property;
238 g_object_class_install_property (object_class,
242 g_object_class_install_property (object_class,
247 parent_class = g_type_class_peek_parent (class);
252 psppire_window_base_init (PsppireWindowClass *class)
254 GObjectClass *object_class = G_OBJECT_CLASS (class);
256 object_class->finalize = psppire_window_finalize;
262 psppire_window_base_finalize (PsppireWindowClass *class,
268 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
270 /* Prohibit changes to the state */
271 mi->active = !mi->active;
275 /* Look up the window associated with this menuitem and present it to the user */
277 menu_activate (GtkMenuItem *mi, gpointer data)
279 const gchar *key = data;
281 PsppireWindowRegister *reg = psppire_window_register_new ();
283 PsppireWindow *window = psppire_window_register_lookup (reg, key);
285 gtk_window_present (GTK_WINDOW (window));
289 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
291 GtkWidget *item = gtk_check_menu_item_new_with_label (key);
293 g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
294 g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
296 gtk_widget_show (item);
298 gtk_menu_shell_append (window->menu, item);
300 /* Set the state without emitting a signal */
301 GTK_CHECK_MENU_ITEM (item)->active =
302 (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
304 g_hash_table_insert (window->menuitem_table, key, item);
308 insert_item (gpointer key, gpointer value, gpointer data)
310 PsppireWindow *window = PSPPIRE_WINDOW (data);
312 if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
315 insert_menuitem_into_menu (window, key);
318 /* Insert a new item into the window menu */
320 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
322 PsppireWindow *window = PSPPIRE_WINDOW (data);
324 insert_menuitem_into_menu (window, (gpointer) key);
329 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
331 PsppireWindow *window = PSPPIRE_WINDOW (data);
334 item = g_hash_table_lookup (window->menuitem_table, key);
336 g_hash_table_remove (window->menuitem_table, key);
338 if (GTK_IS_CONTAINER (window->menu))
339 gtk_container_remove (GTK_CONTAINER (window->menu), item);
343 insert_existing_items (PsppireWindow *window)
345 psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
349 psppire_window_init (PsppireWindow *window)
354 window->menuitem_table = g_hash_table_new (g_str_hash, g_str_equal);
357 g_signal_connect (window, "realize", G_CALLBACK (insert_existing_items), NULL);
359 window->insert_handler = g_signal_connect (psppire_window_register_new (),
361 G_CALLBACK (insert_menuitem),
364 window->remove_handler = g_signal_connect (psppire_window_register_new (),
366 G_CALLBACK (remove_menuitem),
369 window->unsaved = FALSE;
374 psppire_window_new (void)
376 return GTK_WIDGET (g_object_new (psppire_window_get_type (),
377 "type", GTK_WINDOW_TOPLEVEL,
383 psppire_window_get_filename (PsppireWindow *w)
385 const gchar *name = NULL;
386 g_object_get (w, "filename", &name, NULL);
392 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
394 g_object_set (w, "filename", filename, NULL);
398 psppire_window_set_unsaved (PsppireWindow *w, gboolean unsaved)
400 w->unsaved = unsaved;
402 psppire_window_set_title (w);
409 minimise_window (gpointer key, gpointer value, gpointer data)
411 gtk_window_iconify (GTK_WINDOW (value));
416 psppire_window_minimise_all (void)
418 PsppireWindowRegister *reg = psppire_window_register_new ();
420 g_hash_table_foreach (reg->name_table, minimise_window, NULL);