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