Convert filename encodings when opening files.
[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
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
29 #include <gettext.h>
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33 #include "psppire-window.h"
34 #include "psppire-window-register.h"
35 #include "psppire-conf.h"
36
37 static void psppire_window_base_finalize (PsppireWindowClass *, gpointer);
38 static void psppire_window_base_init     (PsppireWindowClass *class);
39 static void psppire_window_class_init    (PsppireWindowClass *class);
40 static void psppire_window_init          (PsppireWindow      *window);
41
42
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->dirty)
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_filename_display_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   PsppireConf *conf = psppire_conf_new ();
198
199   const gchar *base = G_OBJECT_TYPE_NAME (window);
200
201   psppire_conf_set_window_geometry (conf, base, window);
202 }
203
204
205 static gboolean
206 save_geometry (GtkWidget *window, GdkEvent *event, gpointer data)
207 {
208   const gchar *base = G_OBJECT_TYPE_NAME (window);
209
210   PsppireConf *conf = psppire_conf_new ();
211
212   psppire_conf_save_window_geometry (conf, base, event);
213
214   return FALSE;
215 }
216
217
218 static void
219 psppire_window_finalize (GObject *object)
220 {
221   PsppireWindow *window = PSPPIRE_WINDOW (object);
222
223   PsppireWindowRegister *reg = psppire_window_register_new ();
224
225   psppire_window_register_remove (reg, window->name);
226   free (window->name);
227   free (window->description);
228
229   g_signal_handler_disconnect (psppire_window_register_new (),
230                                window->remove_handler);
231
232   g_signal_handler_disconnect (psppire_window_register_new (),
233                                window->insert_handler);
234
235   g_hash_table_destroy (window->menuitem_table);
236
237   if (G_OBJECT_CLASS (parent_class)->finalize)
238     G_OBJECT_CLASS (parent_class)->finalize (object);
239 }
240
241
242 static void
243 psppire_window_class_init (PsppireWindowClass *class)
244 {
245   GObjectClass *object_class = G_OBJECT_CLASS (class);
246
247   GParamSpec *description_spec =
248     g_param_spec_string ("description",
249                        "Description",
250                        "A string describing the usage of the window",
251                          "??????", /*Should be overridden by derived classes */
252                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
253
254   GParamSpec *filename_spec =
255     g_param_spec_string ("filename",
256                        "File name",
257                        "The name of the file associated with this window, if any",
258                          "Untitled",
259                          G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
260
261   g_unichar_to_utf8 (0x2014, mdash);
262
263   object_class->set_property = psppire_window_set_property;
264   object_class->get_property = psppire_window_get_property;
265
266   g_object_class_install_property (object_class,
267                                    PROP_DESCRIPTION,
268                                    description_spec);
269
270   g_object_class_install_property (object_class,
271                                    PROP_FILENAME,
272                                    filename_spec);
273
274   parent_class = g_type_class_peek_parent (class);
275 }
276
277
278 static void
279 psppire_window_base_init (PsppireWindowClass *class)
280 {
281   GObjectClass *object_class = G_OBJECT_CLASS (class);
282
283   object_class->finalize = psppire_window_finalize;
284 }
285
286
287
288 static void
289 psppire_window_base_finalize (PsppireWindowClass *class,
290                                 gpointer class_data)
291 {
292 }
293
294 static void
295 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
296 {
297   /* Prohibit changes to the state */
298   mi->active = !mi->active;
299 }
300
301
302 /* Look up the window associated with this menuitem and present it to the user */
303 static void
304 menu_activate (GtkMenuItem *mi, gpointer data)
305 {
306   const gchar *key = data;
307
308   PsppireWindowRegister *reg = psppire_window_register_new ();
309
310   PsppireWindow *window = psppire_window_register_lookup (reg, key);
311
312   gtk_window_present (GTK_WINDOW (window));
313 }
314
315 static void
316 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
317 {
318   gchar *filename = g_filename_display_name (key);
319   GtkWidget *item = gtk_check_menu_item_new_with_label (filename);
320
321   g_free (filename);
322
323   g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
324   g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
325
326   gtk_widget_show (item);
327
328   gtk_menu_shell_append (window->menu, item);
329
330   /* Set the state without emitting a signal */
331   GTK_CHECK_MENU_ITEM (item)->active =
332    (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
333
334   g_hash_table_insert (window->menuitem_table, key, item);
335 }
336
337 static void
338 insert_item (gpointer key, gpointer value, gpointer data)
339 {
340   PsppireWindow *window = PSPPIRE_WINDOW (data);
341
342   if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
343     return;
344
345   insert_menuitem_into_menu (window, key);
346 }
347
348 /* Insert a new item into the window menu */
349 static void
350 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
351 {
352   PsppireWindow *window = PSPPIRE_WINDOW (data);
353   
354   insert_menuitem_into_menu (window, (gpointer) key);
355 }
356
357
358 static void
359 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
360 {
361   PsppireWindow *window = PSPPIRE_WINDOW (data);
362   GtkWidget *item ;
363
364   item = g_hash_table_lookup (window->menuitem_table, key);
365
366   g_hash_table_remove (window->menuitem_table, key);
367
368   if (GTK_IS_CONTAINER (window->menu))
369     gtk_container_remove (GTK_CONTAINER (window->menu), item);
370 }
371
372 static void
373 insert_existing_items (PsppireWindow *window)
374 {
375   psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
376 }
377
378
379 static gboolean
380 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
381 {
382   PsppireWindowRegister *reg = psppire_window_register_new ();
383
384   if ( w->dirty )
385     {
386       gint response = psppire_window_query_save (w);
387
388       switch (response)
389         {
390         default:
391         case GTK_RESPONSE_CANCEL:
392           return TRUE;
393           break;
394         case GTK_RESPONSE_APPLY:
395           psppire_window_save (w);
396           break;
397         case GTK_RESPONSE_REJECT:
398           break;
399         }
400     }
401
402   if ( 1 == psppire_window_register_n_items (reg))
403     gtk_main_quit ();
404
405   return FALSE;
406 }
407
408
409 static void
410 psppire_window_init (PsppireWindow *window)
411 {
412   window->name = NULL;
413   window->menu = NULL;
414   window->description = strdup ("");
415
416   window->menuitem_table  = g_hash_table_new (g_str_hash, g_str_equal);
417
418
419   g_signal_connect (window,  "realize", G_CALLBACK (insert_existing_items), NULL);
420
421   window->insert_handler = g_signal_connect (psppire_window_register_new (),
422                                              "inserted",
423                                              G_CALLBACK (insert_menuitem),
424                                              window);
425
426   window->remove_handler = g_signal_connect (psppire_window_register_new (),
427                                              "removed",
428                                              G_CALLBACK (remove_menuitem),
429                                              window);
430
431   window->dirty = FALSE;
432
433   g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
434
435   g_object_set (window, "icon-name", "psppicon", NULL);
436
437   g_signal_connect (window, "configure-event",
438                     G_CALLBACK (save_geometry), window);
439
440   g_signal_connect (window, "window-state-event",
441                     G_CALLBACK (save_geometry), window);
442
443   g_signal_connect (window, "realize",
444                     G_CALLBACK (on_realize), window);
445
446 }
447
448 /*
449    Ask the user if the buffer should be saved.
450    Return the response.
451 */
452 gint
453 psppire_window_query_save (PsppireWindow *se)
454 {
455   gint response;
456   GtkWidget *dialog;
457   GtkWidget *cancel_button;
458
459   const gchar *description;
460   const gchar *filename = psppire_window_get_filename (se);
461
462   GTimeVal time;
463
464   g_get_current_time (&time);
465
466   g_object_get (se, "description", &description, NULL);
467
468   g_return_val_if_fail (filename != NULL, GTK_RESPONSE_NONE);
469
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                             filename);
478
479   g_object_set (dialog, "icon-name", "psppicon", NULL);
480
481   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
482                                             _("If you don't save, changes from the last %ld seconds will be permanently lost."),
483                                             time.tv_sec - se->savetime.tv_sec);
484
485   gtk_dialog_add_button  (GTK_DIALOG (dialog),
486                           _("Close _without saving"),
487                           GTK_RESPONSE_REJECT);
488
489   cancel_button = gtk_dialog_add_button  (GTK_DIALOG (dialog),
490                                           GTK_STOCK_CANCEL,
491                                           GTK_RESPONSE_CANCEL);
492
493   gtk_dialog_add_button  (GTK_DIALOG (dialog),
494                           GTK_STOCK_SAVE,
495                           GTK_RESPONSE_APPLY);
496
497   gtk_widget_grab_focus (cancel_button);
498
499   response = gtk_dialog_run (GTK_DIALOG (dialog));
500
501   gtk_widget_destroy (dialog);
502
503   return response;
504 }
505
506
507
508 const gchar *
509 psppire_window_get_filename (PsppireWindow *w)
510 {
511   const gchar *name = NULL;
512   g_object_get (w, "filename", &name, NULL);
513   return name;
514 }
515
516
517 void
518 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
519 {
520   g_object_set (w, "filename", filename, NULL);
521 }
522
523 void
524 psppire_window_set_unsaved (PsppireWindow *w)
525 {
526   if ( w->dirty == FALSE)
527     g_get_current_time (&w->savetime);
528
529   w->dirty = TRUE;
530
531   psppire_window_set_title (w);
532 }
533
534 gboolean
535 psppire_window_get_unsaved (PsppireWindow *w)
536 {
537   return w->dirty;
538 }
539
540
541 \f
542
543
544 static void
545 minimise_window (gpointer key, gpointer value, gpointer data)
546 {
547   gtk_window_iconify (GTK_WINDOW (value));
548 }
549
550
551 void
552 psppire_window_minimise_all (void)
553 {
554   PsppireWindowRegister *reg = psppire_window_register_new ();
555
556   g_hash_table_foreach (reg->name_table, minimise_window, NULL);
557 }
558
559
560 \f
561
562 GType
563 psppire_window_model_get_type (void)
564 {
565   static GType window_model_type = 0;
566
567   if (! window_model_type)
568     {
569       static const GTypeInfo window_model_info =
570       {
571         sizeof (PsppireWindowIface), /* class_size */
572         NULL,           /* base_init */
573         NULL,           /* base_finalize */
574         NULL,
575         NULL,           /* class_finalize */
576         NULL,           /* class_data */
577         0,
578         0,              /* n_preallocs */
579         NULL
580       };
581
582       window_model_type =
583         g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
584                                 &window_model_info, 0);
585
586       g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
587     }
588
589   return window_model_type;
590 }
591
592
593 void
594 psppire_window_save (PsppireWindow *w)
595 {
596   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
597
598   g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
599
600   g_assert (i);
601
602   g_return_if_fail (i->save);
603
604   i->save (w);
605
606   w->dirty = FALSE;
607   psppire_window_set_title (w);
608 }
609
610 extern GtkRecentManager *the_recent_mgr;
611
612 static void add_most_recent (const char *file_name, GtkRecentManager *rm);
613 static void delete_recent (const char *file_name, GtkRecentManager *rm);
614
615 gboolean
616 psppire_window_load (PsppireWindow *w, const gchar *file)
617 {
618   gboolean ok;
619   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
620
621   g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
622
623   g_assert (i);
624
625   g_return_val_if_fail (i->load, FALSE);
626
627   ok = i->load (w, file);
628
629   if ( ok )
630     {
631       psppire_window_set_filename (w, file);
632       add_most_recent (file, the_recent_mgr);
633       w->dirty = FALSE;
634     }
635   else
636     delete_recent (file, the_recent_mgr);
637
638   psppire_window_set_title (w);
639
640   return ok;
641 }
642
643
644 /* Puts FILE_NAME into the recent list.
645    If it's already in the list, it moves it to the top
646 */
647 static void
648 add_most_recent (const char *file_name, GtkRecentManager *rm)
649 {
650   gchar *uri = g_filename_to_uri  (file_name, NULL, NULL);
651
652   if ( uri )
653     gtk_recent_manager_add_item (rm, uri);
654
655   g_free (uri);
656 }
657
658
659
660 /*
661    If FILE_NAME exists in the recent list, then  delete it.
662  */
663 static void
664 delete_recent (const char *file_name, GtkRecentManager *rm)
665 {
666   gchar *uri = g_filename_to_uri  (file_name, NULL, NULL);
667
668   if ( uri )
669     gtk_recent_manager_remove_item (rm, uri, NULL);
670
671   g_free (uri);
672 }
673