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