cf610f6f640a6dd4a701d55bfaedaf3372efa01c
[pspp-builds.git] / 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;
308   GtkWidget *item;
309
310   /* Add a separator before adding the first real item.  If we add a separator
311      at any other time, sometimes GtkUIManager removes it. */
312   if (g_hash_table_size (window->menuitem_table) == 0)
313     {
314       GtkWidget *separator = gtk_separator_menu_item_new ();
315       gtk_widget_show (separator);
316       gtk_menu_shell_append (window->menu, separator);
317     }
318
319   filename = g_filename_display_name (key);
320   item = gtk_check_menu_item_new_with_label (filename);
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   const gchar *base = G_OBJECT_TYPE_NAME (w);
385
386   PsppireConf *conf = psppire_conf_new ();
387
388   psppire_conf_save_window_geometry (conf, base, GTK_WINDOW (w));
389
390
391   if ( w->dirty )
392     {
393       gint response = psppire_window_query_save (w);
394
395       switch (response)
396         {
397         default:
398         case GTK_RESPONSE_CANCEL:
399           return TRUE;
400           break;
401         case GTK_RESPONSE_APPLY:
402           psppire_window_save (w);
403           break;
404         case GTK_RESPONSE_REJECT:
405           break;
406         }
407     }
408
409   if ( 1 == psppire_window_register_n_items (reg))
410     gtk_main_quit ();
411
412   return FALSE;
413 }
414
415
416 static void
417 psppire_window_init (PsppireWindow *window)
418 {
419   window->name = NULL;
420   window->menu = NULL;
421   window->description = xstrdup ("");
422
423   window->menuitem_table  = g_hash_table_new (g_str_hash, g_str_equal);
424
425
426   g_signal_connect (window,  "realize", G_CALLBACK (insert_existing_items), NULL);
427
428   window->insert_handler = g_signal_connect (psppire_window_register_new (),
429                                              "inserted",
430                                              G_CALLBACK (insert_menuitem),
431                                              window);
432
433   window->remove_handler = g_signal_connect (psppire_window_register_new (),
434                                              "removed",
435                                              G_CALLBACK (remove_menuitem),
436                                              window);
437
438   window->dirty = FALSE;
439
440   g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
441
442   g_object_set (window, "icon-name", "psppicon", NULL);
443
444   g_signal_connect (window, "realize",
445                     G_CALLBACK (on_realize), window);
446
447 }
448
449 /*
450    Ask the user if the buffer should be saved.
451    Return the response.
452 */
453 gint
454 psppire_window_query_save (PsppireWindow *se)
455 {
456   gchar *fn;
457   gint response;
458   GtkWidget *dialog;
459   GtkWidget *cancel_button;
460
461   const gchar *description;
462   const gchar *filename = psppire_window_get_filename (se);
463
464   GTimeVal time;
465
466   g_get_current_time (&time);
467
468   g_object_get (se, "description", &description, NULL);
469
470   g_return_val_if_fail (filename != NULL, GTK_RESPONSE_NONE);
471
472
473   fn = g_filename_display_basename (filename);
474
475   dialog =
476     gtk_message_dialog_new (GTK_WINDOW (se),
477                             GTK_DIALOG_MODAL,
478                             GTK_MESSAGE_WARNING,
479                             GTK_BUTTONS_NONE,
480                             _("Save the changes to `%s' before closing?"),
481                             fn);
482   g_free (fn);
483
484   g_object_set (dialog, "icon-name", "psppicon", NULL);
485
486   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
487                                             _("If you don't save, changes from the last %ld seconds will be permanently lost."),
488                                             time.tv_sec - se->savetime.tv_sec);
489
490   gtk_dialog_add_button  (GTK_DIALOG (dialog),
491                           _("Close _without saving"),
492                           GTK_RESPONSE_REJECT);
493
494   cancel_button = gtk_dialog_add_button  (GTK_DIALOG (dialog),
495                                           GTK_STOCK_CANCEL,
496                                           GTK_RESPONSE_CANCEL);
497
498   gtk_dialog_add_button  (GTK_DIALOG (dialog),
499                           GTK_STOCK_SAVE,
500                           GTK_RESPONSE_APPLY);
501
502   gtk_widget_grab_focus (cancel_button);
503
504   response = gtk_dialog_run (GTK_DIALOG (dialog));
505
506   gtk_widget_destroy (dialog);
507
508   return response;
509 }
510
511
512
513 const gchar *
514 psppire_window_get_filename (PsppireWindow *w)
515 {
516   const gchar *name = NULL;
517   g_object_get (w, "filename", &name, NULL);
518   return name;
519 }
520
521
522 void
523 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
524 {
525   g_object_set (w, "filename", filename, NULL);
526 }
527
528 void
529 psppire_window_set_unsaved (PsppireWindow *w)
530 {
531   if ( w->dirty == FALSE)
532     g_get_current_time (&w->savetime);
533
534   w->dirty = TRUE;
535
536   psppire_window_set_title (w);
537 }
538
539 gboolean
540 psppire_window_get_unsaved (PsppireWindow *w)
541 {
542   return w->dirty;
543 }
544
545
546 \f
547
548
549 static void
550 minimise_window (gpointer key, gpointer value, gpointer data)
551 {
552   gtk_window_iconify (GTK_WINDOW (value));
553 }
554
555
556 void
557 psppire_window_minimise_all (void)
558 {
559   PsppireWindowRegister *reg = psppire_window_register_new ();
560
561   g_hash_table_foreach (reg->name_table, minimise_window, NULL);
562 }
563
564
565 \f
566
567 GType
568 psppire_window_model_get_type (void)
569 {
570   static GType window_model_type = 0;
571
572   if (! window_model_type)
573     {
574       static const GTypeInfo window_model_info =
575       {
576         sizeof (PsppireWindowIface), /* class_size */
577         NULL,           /* base_init */
578         NULL,           /* base_finalize */
579         NULL,
580         NULL,           /* class_finalize */
581         NULL,           /* class_data */
582         0,
583         0,              /* n_preallocs */
584         NULL
585       };
586
587       window_model_type =
588         g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
589                                 &window_model_info, 0);
590
591       g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
592     }
593
594   return window_model_type;
595 }
596
597
598 void
599 psppire_window_save (PsppireWindow *w)
600 {
601   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
602
603   g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
604
605   g_assert (i);
606
607   g_return_if_fail (i->save);
608
609   i->save (w);
610
611   w->dirty = FALSE;
612   psppire_window_set_title (w);
613 }
614
615 extern GtkRecentManager *the_recent_mgr;
616
617 static void add_most_recent (const char *file_name, GtkRecentManager *rm);
618 static void delete_recent (const char *file_name, GtkRecentManager *rm);
619
620 gboolean
621 psppire_window_load (PsppireWindow *w, const gchar *file)
622 {
623   gboolean ok;
624   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
625
626   g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
627
628   g_assert (i);
629
630   g_return_val_if_fail (i->load, FALSE);
631
632   ok = i->load (w, file);
633
634   if ( ok )
635     {
636       psppire_window_set_filename (w, file);
637       add_most_recent (file, the_recent_mgr);
638       w->dirty = FALSE;
639     }
640   else
641     delete_recent (file, the_recent_mgr);
642
643   psppire_window_set_title (w);
644
645   return ok;
646 }
647
648
649 /* Puts FILE_NAME into the recent list.
650    If it's already in the list, it moves it to the top
651 */
652 static void
653 add_most_recent (const char *file_name, GtkRecentManager *rm)
654 {
655   gchar *uri = g_filename_to_uri  (file_name, NULL, NULL);
656
657   if ( uri )
658     gtk_recent_manager_add_item (rm, uri);
659
660   g_free (uri);
661 }
662
663
664
665 /*
666    If FILE_NAME exists in the recent list, then  delete it.
667  */
668 static void
669 delete_recent (const char *file_name, GtkRecentManager *rm)
670 {
671   gchar *uri = g_filename_to_uri  (file_name, NULL, NULL);
672
673   if ( uri )
674     gtk_recent_manager_remove_item (rm, uri, NULL);
675
676   g_free (uri);
677 }
678