Save window contents on closing.
[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/gtkstock.h>
20 #include <gtk/gtkmessagedialog.h>
21 #include <gtk/gtksignal.h>
22 #include <gtk/gtkwindow.h>
23 #include <gtk/gtkcheckmenuitem.h>
24
25 #include <stdlib.h>
26
27 #include <gettext.h>
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
30
31 #include "psppire-window.h"
32 #include "psppire-window-register.h"
33
34 static void psppire_window_base_finalize (PsppireWindowClass *, gpointer);
35 static void psppire_window_base_init     (PsppireWindowClass *class);
36 static void psppire_window_class_init    (PsppireWindowClass *class);
37 static void psppire_window_init          (PsppireWindow      *window);
38
39
40 static PsppireWindowClass *the_class;
41 static GObjectClass *parent_class;
42
43 GType
44 psppire_window_get_type (void)
45 {
46   static GType psppire_window_type = 0;
47
48   if (!psppire_window_type)
49     {
50       static const GTypeInfo psppire_window_info =
51       {
52         sizeof (PsppireWindowClass),
53         (GBaseInitFunc) psppire_window_base_init,
54         (GBaseFinalizeFunc) psppire_window_base_finalize,
55         (GClassInitFunc) psppire_window_class_init,
56         (GClassFinalizeFunc) NULL,
57         NULL,
58         sizeof (PsppireWindow),
59         0,
60         (GInstanceInitFunc) psppire_window_init,
61       };
62
63       psppire_window_type =
64         g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow",
65                                 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
66     }
67
68   return psppire_window_type;
69 }
70
71
72 /* Properties */
73 enum
74 {
75   PROP_0,
76   PROP_FILENAME,
77   PROP_DESCRIPTION
78 };
79
80
81 gchar *
82 uniquify (const gchar *str, int *x)
83 {
84   return g_strdup_printf ("%s%d", str, (*x)++);
85 }
86
87 static gchar mdash[6] = {0,0,0,0,0,0};
88
89 static void
90 psppire_window_set_title (PsppireWindow *window)
91 {
92   GString *title = g_string_sized_new (80);
93
94   g_string_printf (title, _("%s %s PSPPIRE %s"),
95                    window->basename ? window->basename : "",
96                    mdash, window->description);
97
98   if ( window->unsaved)
99     g_string_prepend_c (title, '*');
100
101   gtk_window_set_title (GTK_WINDOW (window), title->str);
102
103   g_string_free (title, TRUE);
104 }
105
106 static void
107 psppire_window_set_property (GObject         *object,
108                              guint            prop_id,
109                              const GValue    *value,
110                              GParamSpec      *pspec)
111 {
112   PsppireWindow *window = PSPPIRE_WINDOW (object);
113
114   switch (prop_id)
115     {
116     case PROP_DESCRIPTION:
117       window->description = g_value_dup_string (value);
118       psppire_window_set_title (window);
119       break;
120     case PROP_FILENAME:
121       {
122         PsppireWindowRegister *reg = psppire_window_register_new ();
123
124         gchar *candidate_name ;
125
126         {
127           const gchar *name = g_value_get_string (value);
128           int x = 0;
129           GValue def = {0};
130           g_value_init (&def, pspec->value_type);
131
132           if ( NULL == name)
133             {
134               g_param_value_set_default (pspec, &def);
135               name = g_value_get_string (&def);
136             }
137
138           candidate_name = strdup (name);
139
140           while ( psppire_window_register_lookup (reg, candidate_name))
141             {
142               free (candidate_name);
143               candidate_name = uniquify (name, &x);
144             }
145
146           window->basename = g_path_get_basename (candidate_name);
147
148           g_value_unset (&def);
149         }
150
151         psppire_window_set_title (window);
152
153         if ( window->name)
154           psppire_window_register_remove (reg, window->name);
155
156         free (window->name);
157         window->name = candidate_name;
158
159         psppire_window_register_insert (reg, window, window->name);
160       }
161       break;
162     default:
163       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164       break;
165     };
166 }
167
168
169 static void
170 psppire_window_get_property (GObject         *object,
171                              guint            prop_id,
172                              GValue          *value,
173                              GParamSpec      *pspec)
174 {
175   PsppireWindow *window = PSPPIRE_WINDOW (object);
176
177   switch (prop_id)
178     {
179     case PROP_FILENAME:
180       g_value_set_string (value, window->name);
181       break;
182     case PROP_DESCRIPTION:
183       g_value_set_string (value, window->description);
184       break;
185     default:
186       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187       break;
188     };
189 }
190
191
192
193 static void
194 psppire_window_finalize (GObject *object)
195 {
196   PsppireWindow *window = PSPPIRE_WINDOW (object);
197
198   PsppireWindowRegister *reg = psppire_window_register_new ();
199
200   psppire_window_register_remove (reg, window->name);
201   free (window->name);
202   free (window->description);
203
204   g_signal_handler_disconnect (psppire_window_register_new (),
205                                window->remove_handler);
206
207   g_signal_handler_disconnect (psppire_window_register_new (),
208                                window->insert_handler);
209
210   g_hash_table_destroy (window->menuitem_table);
211
212   if (G_OBJECT_CLASS (parent_class)->finalize)
213     G_OBJECT_CLASS (parent_class)->finalize (object);
214 }
215
216
217 static void
218 psppire_window_class_init (PsppireWindowClass *class)
219 {
220   GObjectClass *object_class = G_OBJECT_CLASS (class);
221
222   GParamSpec *description_spec =
223     g_param_spec_string ("description",
224                        "Description",
225                        "A string describing the usage of the window",
226                          "??????", /*Should be overridden by derived classes */
227                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
228
229   GParamSpec *filename_spec =
230     g_param_spec_string ("filename",
231                        "File name",
232                        "The name of the file associated with this window, if any",
233                          "Untitled",
234                          G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
235
236   g_unichar_to_utf8 (0x2014, mdash);
237
238   object_class->set_property = psppire_window_set_property;
239   object_class->get_property = psppire_window_get_property;
240
241   g_object_class_install_property (object_class,
242                                    PROP_DESCRIPTION,
243                                    description_spec);
244
245   g_object_class_install_property (object_class,
246                                    PROP_FILENAME,
247                                    filename_spec);
248
249   the_class = class;
250   parent_class = g_type_class_peek_parent (class);
251 }
252
253
254 static void
255 psppire_window_base_init (PsppireWindowClass *class)
256 {
257   GObjectClass *object_class = G_OBJECT_CLASS (class);
258
259   object_class->finalize = psppire_window_finalize;
260 }
261
262
263
264 static void
265 psppire_window_base_finalize (PsppireWindowClass *class,
266                                 gpointer class_data)
267 {
268 }
269
270 static void
271 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
272 {
273   /* Prohibit changes to the state */
274   mi->active = !mi->active;
275 }
276
277
278 /* Look up the window associated with this menuitem and present it to the user */
279 static void
280 menu_activate (GtkMenuItem *mi, gpointer data)
281 {
282   const gchar *key = data;
283
284   PsppireWindowRegister *reg = psppire_window_register_new ();
285
286   PsppireWindow *window = psppire_window_register_lookup (reg, key);
287
288   gtk_window_present (GTK_WINDOW (window));
289 }
290
291 static void
292 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
293 {
294   GtkWidget *item = gtk_check_menu_item_new_with_label (key);
295
296   g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
297   g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
298
299   gtk_widget_show (item);
300
301   gtk_menu_shell_append (window->menu, item);
302
303   /* Set the state without emitting a signal */
304   GTK_CHECK_MENU_ITEM (item)->active =
305    (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
306
307   g_hash_table_insert (window->menuitem_table, key, item);
308 }
309
310 static void
311 insert_item (gpointer key, gpointer value, gpointer data)
312 {
313   PsppireWindow *window = PSPPIRE_WINDOW (data);
314
315   if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
316     return;
317
318   insert_menuitem_into_menu (window, key);
319 }
320
321 /* Insert a new item into the window menu */
322 static void
323 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
324 {
325   PsppireWindow *window = PSPPIRE_WINDOW (data);
326   
327   insert_menuitem_into_menu (window, (gpointer) key);
328 }
329
330
331 static void
332 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
333 {
334   PsppireWindow *window = PSPPIRE_WINDOW (data);
335   GtkWidget *item ;
336
337   item = g_hash_table_lookup (window->menuitem_table, key);
338
339   g_hash_table_remove (window->menuitem_table, key);
340
341   if (GTK_IS_CONTAINER (window->menu))
342     gtk_container_remove (GTK_CONTAINER (window->menu), item);
343 }
344
345 static void
346 insert_existing_items (PsppireWindow *window)
347 {
348   psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
349 }
350
351
352 static gboolean
353 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
354 {
355   PsppireWindowRegister *reg = psppire_window_register_new ();
356
357   if ( w->unsaved )
358     {
359       gint response = psppire_window_query_save (w);
360
361       if ( response == GTK_RESPONSE_CANCEL)
362         return TRUE;
363
364       if ( response == GTK_RESPONSE_ACCEPT)
365         {
366           psppire_window_save (w);
367         }
368     }
369
370   if ( 1 == psppire_window_register_n_items (reg))
371     gtk_main_quit ();
372
373   return FALSE;
374 }
375
376
377 static void
378 psppire_window_init (PsppireWindow *window)
379 {
380   window->name = NULL;
381   window->menu = NULL;
382
383   window->menuitem_table  = g_hash_table_new (g_str_hash, g_str_equal);
384
385
386   g_signal_connect (window,  "realize", G_CALLBACK (insert_existing_items), NULL);
387
388   window->insert_handler = g_signal_connect (psppire_window_register_new (),
389                                              "inserted",
390                                              G_CALLBACK (insert_menuitem),
391                                              window);
392
393   window->remove_handler = g_signal_connect (psppire_window_register_new (),
394                                              "removed",
395                                              G_CALLBACK (remove_menuitem),
396                                              window);
397
398   window->unsaved = FALSE;
399
400   g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
401
402   g_object_set (window, "icon-name", "psppicon", NULL);
403
404 }
405
406
407 /* 
408    Ask the user if the buffer should be saved.
409    Return the response.
410 */
411 gint
412 psppire_window_query_save (PsppireWindow *se)
413 {
414   gint response;
415   GtkWidget *dialog;
416
417   const gchar *description;
418   const gchar *filename = psppire_window_get_filename (se);
419
420   g_object_get (se, "description", &description, NULL);
421
422   g_return_val_if_fail (filename != NULL, GTK_RESPONSE_NONE);
423
424   dialog =
425     gtk_message_dialog_new (GTK_WINDOW (se),
426                             GTK_DIALOG_MODAL,
427                             GTK_MESSAGE_QUESTION,
428                             GTK_BUTTONS_NONE,
429                             _("Save contents of %s to \"%s\"?"),
430                             description,
431                             filename);
432
433   gtk_dialog_add_button  (GTK_DIALOG (dialog),
434                           GTK_STOCK_YES,
435                           GTK_RESPONSE_ACCEPT);
436
437   gtk_dialog_add_button  (GTK_DIALOG (dialog),
438                           GTK_STOCK_NO,
439                           GTK_RESPONSE_REJECT);
440
441   gtk_dialog_add_button  (GTK_DIALOG (dialog),
442                           GTK_STOCK_CANCEL,
443                           GTK_RESPONSE_CANCEL);
444
445   response = gtk_dialog_run (GTK_DIALOG (dialog));
446
447   gtk_widget_destroy (dialog);
448
449   return response;
450 }
451
452
453 const gchar *
454 psppire_window_get_filename (PsppireWindow *w)
455 {
456   const gchar *name = NULL;
457   g_object_get (w, "filename", &name, NULL);
458   return name;
459 }
460
461
462 void
463 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
464 {
465   g_object_set (w, "filename", filename, NULL);
466 }
467
468 void
469 psppire_window_set_unsaved (PsppireWindow *w, gboolean unsaved)
470 {
471   w->unsaved = unsaved;
472
473   psppire_window_set_title (w);
474 }
475
476 gboolean
477 psppire_window_get_unsaved (PsppireWindow *w)
478 {
479   return w->unsaved;
480 }
481
482
483 \f
484
485
486 static void
487 minimise_window (gpointer key, gpointer value, gpointer data)
488 {
489   gtk_window_iconify (GTK_WINDOW (value));
490 }
491
492
493 void
494 psppire_window_minimise_all (void)
495 {
496   PsppireWindowRegister *reg = psppire_window_register_new ();
497
498   g_hash_table_foreach (reg->name_table, minimise_window, NULL);
499 }
500
501
502 \f
503
504 GType
505 psppire_window_model_get_type (void)
506 {
507   static GType window_model_type = 0;
508
509   if (! window_model_type)
510     {
511       static const GTypeInfo window_model_info =
512       {
513         sizeof (PsppireWindowIface), /* class_size */
514         NULL,           /* base_init */
515         NULL,           /* base_finalize */
516         NULL,
517         NULL,           /* class_finalize */
518         NULL,           /* class_data */
519         0,
520         0,              /* n_preallocs */
521         NULL
522       };
523
524       window_model_type =
525         g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
526                                 &window_model_info, 0);
527
528       g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
529     }
530
531   return window_model_type;
532 }
533
534
535 void
536 psppire_window_save (PsppireWindow *w)
537 {
538   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
539
540   g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
541
542   g_assert (i);
543
544   g_return_if_fail (i->save);
545
546   i->save (w);
547 }