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