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