Quit the application after closing the last window
[pspp-builds.git] / src / ui / gui / psppire-window.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <gtk/gtksignal.h>
20 #include <gtk/gtkwindow.h>
21 #include <gtk/gtkcheckmenuitem.h>
22
23 #include <stdlib.h>
24
25 #include <gettext.h>
26 #define _(msgid) gettext (msgid)
27 #define N_(msgid) msgid
28
29 #include "psppire-window.h"
30 #include "psppire-window-register.h"
31
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);
36
37
38 static PsppireWindowClass *the_class;
39 static GObjectClass *parent_class;
40
41 GType
42 psppire_window_get_type (void)
43 {
44   static GType psppire_window_type = 0;
45
46   if (!psppire_window_type)
47     {
48       static const GTypeInfo psppire_window_info =
49       {
50         sizeof (PsppireWindowClass),
51         (GBaseInitFunc) psppire_window_base_init,
52         (GBaseFinalizeFunc) psppire_window_base_finalize,
53         (GClassInitFunc) psppire_window_class_init,
54         (GClassFinalizeFunc) NULL,
55         NULL,
56         sizeof (PsppireWindow),
57         0,
58         (GInstanceInitFunc) psppire_window_init,
59       };
60
61       psppire_window_type =
62         g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow",
63                                 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
64     }
65
66   return psppire_window_type;
67 }
68
69
70 /* Properties */
71 enum
72 {
73   PROP_0,
74   PROP_FILENAME,
75   PROP_DESCRIPTION
76 };
77
78
79 gchar *
80 uniquify (const gchar *str, int *x)
81 {
82   return g_strdup_printf ("%s%d", str, (*x)++);
83 }
84
85 static gchar mdash[6] = {0,0,0,0,0,0};
86
87 static void
88 psppire_window_set_title (PsppireWindow *window)
89 {
90   GString *title = g_string_sized_new (80);
91
92   g_string_printf (title, _("%s %s PSPPIRE %s"),
93                     window->basename, mdash, window->description);
94
95   if ( window->unsaved)
96     g_string_prepend_c (title, '*');
97
98   gtk_window_set_title (GTK_WINDOW (window), title->str);
99
100   g_string_free (title, TRUE);
101 }
102
103 static void
104 psppire_window_set_property (GObject         *object,
105                              guint            prop_id,
106                              const GValue    *value,
107                              GParamSpec      *pspec)
108 {
109   PsppireWindow *window = PSPPIRE_WINDOW (object);
110
111   switch (prop_id)
112     {
113     case PROP_DESCRIPTION:
114       window->description = g_value_dup_string (value);
115       psppire_window_set_title (window);
116       break;
117     case PROP_FILENAME:
118       {
119         PsppireWindowRegister *reg = psppire_window_register_new ();
120
121         gchar *candidate_name ;
122
123         {
124           const gchar *name = g_value_get_string (value);
125           int x = 0;
126           GValue def = {0};
127           g_value_init (&def, pspec->value_type);
128
129           if ( NULL == name)
130             {
131               g_param_value_set_default (pspec, &def);
132               name = g_value_get_string (&def);
133             }
134
135           candidate_name = strdup (name);
136
137           while ( psppire_window_register_lookup (reg, candidate_name))
138             {
139               free (candidate_name);
140               candidate_name = uniquify (name, &x);
141             }
142
143           window->basename = g_path_get_basename (candidate_name);
144
145           g_value_unset (&def);
146         }
147
148         psppire_window_set_title (window);
149
150         if ( window->name)
151           psppire_window_register_remove (reg, window->name);
152
153         free (window->name);
154         window->name = candidate_name;
155
156         psppire_window_register_insert (reg, window, window->name);
157       }
158       break;
159     default:
160       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161       break;
162     };
163 }
164
165
166 static void
167 psppire_window_get_property (GObject         *object,
168                              guint            prop_id,
169                              GValue          *value,
170                              GParamSpec      *pspec)
171 {
172   PsppireWindow *window = PSPPIRE_WINDOW (object);
173
174   switch (prop_id)
175     {
176     case PROP_FILENAME:
177       g_value_set_string (value, window->name);
178       break;
179     case PROP_DESCRIPTION:
180       g_value_set_string (value, window->description);
181       break;
182     default:
183       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184       break;
185     };
186 }
187
188
189
190 static void
191 psppire_window_finalize (GObject *object)
192 {
193   PsppireWindow *window = PSPPIRE_WINDOW (object);
194
195   PsppireWindowRegister *reg = psppire_window_register_new ();
196
197   psppire_window_register_remove (reg, window->name);
198   free (window->name);
199   free (window->description);
200
201   g_signal_handler_disconnect (psppire_window_register_new (),
202                                window->remove_handler);
203
204   g_signal_handler_disconnect (psppire_window_register_new (),
205                                window->insert_handler);
206
207   g_hash_table_destroy (window->menuitem_table);
208
209   if (G_OBJECT_CLASS (parent_class)->finalize)
210     G_OBJECT_CLASS (parent_class)->finalize (object);
211 }
212
213
214 static void
215 psppire_window_class_init (PsppireWindowClass *class)
216 {
217   GObjectClass *object_class = G_OBJECT_CLASS (class);
218
219   GParamSpec *description_spec =
220     g_param_spec_string ("description",
221                        "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);
225
226   GParamSpec *filename_spec =
227     g_param_spec_string ("filename",
228                        "File name",
229                        "The name of the file associated with this window, if any",
230                          "Untitled",
231                          G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
232
233   g_unichar_to_utf8 (0x2014, mdash);
234
235   object_class->set_property = psppire_window_set_property;
236   object_class->get_property = psppire_window_get_property;
237
238   g_object_class_install_property (object_class,
239                                    PROP_DESCRIPTION,
240                                    description_spec);
241
242   g_object_class_install_property (object_class,
243                                    PROP_FILENAME,
244                                    filename_spec);
245
246   the_class = class;
247   parent_class = g_type_class_peek_parent (class);
248 }
249
250
251 static void
252 psppire_window_base_init (PsppireWindowClass *class)
253 {
254   GObjectClass *object_class = G_OBJECT_CLASS (class);
255
256   object_class->finalize = psppire_window_finalize;
257 }
258
259
260
261 static void
262 psppire_window_base_finalize (PsppireWindowClass *class,
263                                 gpointer class_data)
264 {
265 }
266
267 static void
268 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
269 {
270   /* Prohibit changes to the state */
271   mi->active = !mi->active;
272 }
273
274
275 /* Look up the window associated with this menuitem and present it to the user */
276 static void
277 menu_activate (GtkMenuItem *mi, gpointer data)
278 {
279   const gchar *key = data;
280
281   PsppireWindowRegister *reg = psppire_window_register_new ();
282
283   PsppireWindow *window = psppire_window_register_lookup (reg, key);
284
285   gtk_window_present (GTK_WINDOW (window));
286 }
287
288 static void
289 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
290 {
291   GtkWidget *item = gtk_check_menu_item_new_with_label (key);
292
293   g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
294   g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
295
296   gtk_widget_show (item);
297
298   gtk_menu_shell_append (window->menu, item);
299
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);
303
304   g_hash_table_insert (window->menuitem_table, key, item);
305 }
306
307 static void
308 insert_item (gpointer key, gpointer value, gpointer data)
309 {
310   PsppireWindow *window = PSPPIRE_WINDOW (data);
311
312   if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
313     return;
314
315   insert_menuitem_into_menu (window, key);
316 }
317
318 /* Insert a new item into the window menu */
319 static void
320 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
321 {
322   PsppireWindow *window = PSPPIRE_WINDOW (data);
323   
324   insert_menuitem_into_menu (window, (gpointer) key);
325 }
326
327
328 static void
329 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
330 {
331   PsppireWindow *window = PSPPIRE_WINDOW (data);
332   GtkWidget *item ;
333
334   item = g_hash_table_lookup (window->menuitem_table, key);
335
336   g_hash_table_remove (window->menuitem_table, key);
337
338   if (GTK_IS_CONTAINER (window->menu))
339     gtk_container_remove (GTK_CONTAINER (window->menu), item);
340 }
341
342 static void
343 insert_existing_items (PsppireWindow *window)
344 {
345   psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
346 }
347
348
349 static gboolean
350 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
351 {
352   PsppireWindow *dw = PSPPIRE_WINDOW (user_data);
353
354   PsppireWindowRegister *reg = psppire_window_register_new ();
355
356
357   if ( 1 == psppire_window_register_n_items (reg))
358     gtk_main_quit ();
359
360   return FALSE;
361 }
362
363
364 static void
365 psppire_window_init (PsppireWindow *window)
366 {
367   window->name = NULL;
368   window->menu = NULL;
369
370   window->menuitem_table  = g_hash_table_new (g_str_hash, g_str_equal);
371
372
373   g_signal_connect (window,  "realize", G_CALLBACK (insert_existing_items), NULL);
374
375   window->insert_handler = g_signal_connect (psppire_window_register_new (),
376                                              "inserted",
377                                              G_CALLBACK (insert_menuitem),
378                                              window);
379
380   window->remove_handler = g_signal_connect (psppire_window_register_new (),
381                                              "removed",
382                                              G_CALLBACK (remove_menuitem),
383                                              window);
384
385   window->unsaved = FALSE;
386
387   g_signal_connect (window, "delete-event", G_CALLBACK (on_delete), window);
388 }
389
390
391 const gchar *
392 psppire_window_get_filename (PsppireWindow *w)
393 {
394   const gchar *name = NULL;
395   g_object_get (w, "filename", &name, NULL);
396   return name;
397 }
398
399
400 void
401 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
402 {
403   g_object_set (w, "filename", filename, NULL);
404 }
405
406 void
407 psppire_window_set_unsaved (PsppireWindow *w, gboolean unsaved)
408 {
409   w->unsaved = unsaved;
410
411   psppire_window_set_title (w);
412 }
413
414 gboolean
415 psppire_window_get_unsaved (PsppireWindow *w)
416 {
417   return w->unsaved;
418 }
419
420
421 \f
422
423
424 static void
425 minimise_window (gpointer key, gpointer value, gpointer data)
426 {
427   gtk_window_iconify (GTK_WINDOW (value));
428 }
429
430
431 void
432 psppire_window_minimise_all (void)
433 {
434   PsppireWindowRegister *reg = psppire_window_register_new ();
435
436   g_hash_table_foreach (reg->name_table, minimise_window, NULL);
437 }