Rewrote the recent file manager stuff
[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 #include <gtk/gtkstock.h>
20 #include <gtk/gtkmessagedialog.h>
21 #include <gtk/gtksignal.h>
22 #include <gtk/gtkwindow.h>
23 #include <gtk/gtkcheckmenuitem.h>
24 #include <gtk/gtkmain.h>
25
26 #include <stdlib.h>
27
28 #include <gettext.h>
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) msgid
31
32 #include "psppire-window.h"
33 #include "psppire-window-register.h"
34
35 static void psppire_window_base_finalize (PsppireWindowClass *, gpointer);
36 static void psppire_window_base_init     (PsppireWindowClass *class);
37 static void psppire_window_class_init    (PsppireWindowClass *class);
38 static void psppire_window_init          (PsppireWindow      *window);
39
40
41 static PsppireWindowClass *the_class;
42 static GObjectClass *parent_class;
43
44 GType
45 psppire_window_get_type (void)
46 {
47   static GType psppire_window_type = 0;
48
49   if (!psppire_window_type)
50     {
51       static const GTypeInfo psppire_window_info =
52       {
53         sizeof (PsppireWindowClass),
54         (GBaseInitFunc) psppire_window_base_init,
55         (GBaseFinalizeFunc) psppire_window_base_finalize,
56         (GClassInitFunc) psppire_window_class_init,
57         (GClassFinalizeFunc) NULL,
58         NULL,
59         sizeof (PsppireWindow),
60         0,
61         (GInstanceInitFunc) psppire_window_init,
62       };
63
64       psppire_window_type =
65         g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow",
66                                 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
67     }
68
69   return psppire_window_type;
70 }
71
72
73 /* Properties */
74 enum
75 {
76   PROP_0,
77   PROP_FILENAME,
78   PROP_DESCRIPTION
79 };
80
81
82 gchar *
83 uniquify (const gchar *str, int *x)
84 {
85   return g_strdup_printf ("%s%d", str, (*x)++);
86 }
87
88 static gchar mdash[6] = {0,0,0,0,0,0};
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 %s PSPPIRE %s"),
96                    window->basename ? window->basename : "",
97                    mdash, window->description);
98
99   if ( window->unsaved)
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 = strdup (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_path_get_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
194 static void
195 psppire_window_finalize (GObject *object)
196 {
197   PsppireWindow *window = PSPPIRE_WINDOW (object);
198
199   PsppireWindowRegister *reg = psppire_window_register_new ();
200
201   psppire_window_register_remove (reg, window->name);
202   free (window->name);
203   free (window->description);
204
205   g_signal_handler_disconnect (psppire_window_register_new (),
206                                window->remove_handler);
207
208   g_signal_handler_disconnect (psppire_window_register_new (),
209                                window->insert_handler);
210
211   g_hash_table_destroy (window->menuitem_table);
212
213   if (G_OBJECT_CLASS (parent_class)->finalize)
214     G_OBJECT_CLASS (parent_class)->finalize (object);
215 }
216
217
218 static void
219 psppire_window_class_init (PsppireWindowClass *class)
220 {
221   GObjectClass *object_class = G_OBJECT_CLASS (class);
222
223   GParamSpec *description_spec =
224     g_param_spec_string ("description",
225                        "Description",
226                        "A string describing the usage of the window",
227                          "??????", /*Should be overridden by derived classes */
228                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
229
230   GParamSpec *filename_spec =
231     g_param_spec_string ("filename",
232                        "File name",
233                        "The name of the file associated with this window, if any",
234                          "Untitled",
235                          G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
236
237   g_unichar_to_utf8 (0x2014, mdash);
238
239   object_class->set_property = psppire_window_set_property;
240   object_class->get_property = psppire_window_get_property;
241
242   g_object_class_install_property (object_class,
243                                    PROP_DESCRIPTION,
244                                    description_spec);
245
246   g_object_class_install_property (object_class,
247                                    PROP_FILENAME,
248                                    filename_spec);
249
250   the_class = class;
251   parent_class = g_type_class_peek_parent (class);
252 }
253
254
255 static void
256 psppire_window_base_init (PsppireWindowClass *class)
257 {
258   GObjectClass *object_class = G_OBJECT_CLASS (class);
259
260   object_class->finalize = psppire_window_finalize;
261 }
262
263
264
265 static void
266 psppire_window_base_finalize (PsppireWindowClass *class,
267                                 gpointer class_data)
268 {
269 }
270
271 static void
272 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
273 {
274   /* Prohibit changes to the state */
275   mi->active = !mi->active;
276 }
277
278
279 /* Look up the window associated with this menuitem and present it to the user */
280 static void
281 menu_activate (GtkMenuItem *mi, gpointer data)
282 {
283   const gchar *key = data;
284
285   PsppireWindowRegister *reg = psppire_window_register_new ();
286
287   PsppireWindow *window = psppire_window_register_lookup (reg, key);
288
289   gtk_window_present (GTK_WINDOW (window));
290 }
291
292 static void
293 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
294 {
295   GtkWidget *item = gtk_check_menu_item_new_with_label (key);
296
297   g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
298   g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
299
300   gtk_widget_show (item);
301
302   gtk_menu_shell_append (window->menu, item);
303
304   /* Set the state without emitting a signal */
305   GTK_CHECK_MENU_ITEM (item)->active =
306    (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
307
308   g_hash_table_insert (window->menuitem_table, key, item);
309 }
310
311 static void
312 insert_item (gpointer key, gpointer value, gpointer data)
313 {
314   PsppireWindow *window = PSPPIRE_WINDOW (data);
315
316   if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
317     return;
318
319   insert_menuitem_into_menu (window, key);
320 }
321
322 /* Insert a new item into the window menu */
323 static void
324 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
325 {
326   PsppireWindow *window = PSPPIRE_WINDOW (data);
327   
328   insert_menuitem_into_menu (window, (gpointer) key);
329 }
330
331
332 static void
333 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
334 {
335   PsppireWindow *window = PSPPIRE_WINDOW (data);
336   GtkWidget *item ;
337
338   item = g_hash_table_lookup (window->menuitem_table, key);
339
340   g_hash_table_remove (window->menuitem_table, key);
341
342   if (GTK_IS_CONTAINER (window->menu))
343     gtk_container_remove (GTK_CONTAINER (window->menu), item);
344 }
345
346 static void
347 insert_existing_items (PsppireWindow *window)
348 {
349   psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
350 }
351
352
353 static gboolean
354 on_delete (PsppireWindow *w, GdkEvent *event, gpointer user_data)
355 {
356   PsppireWindowRegister *reg = psppire_window_register_new ();
357
358   if ( w->unsaved )
359     {
360       gint response = psppire_window_query_save (w);
361
362       if ( response == GTK_RESPONSE_CANCEL)
363         return TRUE;
364
365       if ( response == GTK_RESPONSE_ACCEPT)
366         {
367           psppire_window_save (w);
368         }
369     }
370
371   if ( 1 == psppire_window_register_n_items (reg))
372     gtk_main_quit ();
373
374   return FALSE;
375 }
376
377
378 static void
379 psppire_window_init (PsppireWindow *window)
380 {
381   window->name = NULL;
382   window->menu = NULL;
383
384   window->menuitem_table  = g_hash_table_new (g_str_hash, g_str_equal);
385
386
387   g_signal_connect (window,  "realize", G_CALLBACK (insert_existing_items), NULL);
388
389   window->insert_handler = g_signal_connect (psppire_window_register_new (),
390                                              "inserted",
391                                              G_CALLBACK (insert_menuitem),
392                                              window);
393
394   window->remove_handler = g_signal_connect (psppire_window_register_new (),
395                                              "removed",
396                                              G_CALLBACK (remove_menuitem),
397                                              window);
398
399   window->unsaved = FALSE;
400
401   g_signal_connect_swapped (window, "delete-event", G_CALLBACK (on_delete), window);
402
403   g_object_set (window, "icon-name", "psppicon", NULL);
404
405 }
406
407
408 /* 
409    Ask the user if the buffer should be saved.
410    Return the response.
411 */
412 gint
413 psppire_window_query_save (PsppireWindow *se)
414 {
415   gint response;
416   GtkWidget *dialog;
417
418   const gchar *description;
419   const gchar *filename = psppire_window_get_filename (se);
420
421   g_object_get (se, "description", &description, NULL);
422
423   g_return_val_if_fail (filename != NULL, GTK_RESPONSE_NONE);
424
425   dialog =
426     gtk_message_dialog_new (GTK_WINDOW (se),
427                             GTK_DIALOG_MODAL,
428                             GTK_MESSAGE_QUESTION,
429                             GTK_BUTTONS_NONE,
430                             _("Save contents of %s to \"%s\"?"),
431                             description,
432                             filename);
433
434   gtk_dialog_add_button  (GTK_DIALOG (dialog),
435                           GTK_STOCK_YES,
436                           GTK_RESPONSE_ACCEPT);
437
438   gtk_dialog_add_button  (GTK_DIALOG (dialog),
439                           GTK_STOCK_NO,
440                           GTK_RESPONSE_REJECT);
441
442   gtk_dialog_add_button  (GTK_DIALOG (dialog),
443                           GTK_STOCK_CANCEL,
444                           GTK_RESPONSE_CANCEL);
445
446   response = gtk_dialog_run (GTK_DIALOG (dialog));
447
448   gtk_widget_destroy (dialog);
449
450   return response;
451 }
452
453
454 const gchar *
455 psppire_window_get_filename (PsppireWindow *w)
456 {
457   const gchar *name = NULL;
458   g_object_get (w, "filename", &name, NULL);
459   return name;
460 }
461
462
463 void
464 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
465 {
466   g_object_set (w, "filename", filename, NULL);
467 }
468
469 void
470 psppire_window_set_unsaved (PsppireWindow *w, gboolean unsaved)
471 {
472   w->unsaved = unsaved;
473
474   psppire_window_set_title (w);
475 }
476
477 gboolean
478 psppire_window_get_unsaved (PsppireWindow *w)
479 {
480   return w->unsaved;
481 }
482
483
484 \f
485
486
487 static void
488 minimise_window (gpointer key, gpointer value, gpointer data)
489 {
490   gtk_window_iconify (GTK_WINDOW (value));
491 }
492
493
494 void
495 psppire_window_minimise_all (void)
496 {
497   PsppireWindowRegister *reg = psppire_window_register_new ();
498
499   g_hash_table_foreach (reg->name_table, minimise_window, NULL);
500 }
501
502
503 \f
504
505 GType
506 psppire_window_model_get_type (void)
507 {
508   static GType window_model_type = 0;
509
510   if (! window_model_type)
511     {
512       static const GTypeInfo window_model_info =
513       {
514         sizeof (PsppireWindowIface), /* class_size */
515         NULL,           /* base_init */
516         NULL,           /* base_finalize */
517         NULL,
518         NULL,           /* class_finalize */
519         NULL,           /* class_data */
520         0,
521         0,              /* n_preallocs */
522         NULL
523       };
524
525       window_model_type =
526         g_type_register_static (G_TYPE_INTERFACE, "PsppireWindowModel",
527                                 &window_model_info, 0);
528
529       g_type_interface_add_prerequisite (window_model_type, G_TYPE_OBJECT);
530     }
531
532   return window_model_type;
533 }
534
535
536 void
537 psppire_window_save (PsppireWindow *w)
538 {
539   PsppireWindowIface *i = PSPPIRE_WINDOW_MODEL_GET_IFACE (w);
540
541   g_assert (PSPPIRE_IS_WINDOW_MODEL (w));
542
543   g_assert (i);
544
545   g_return_if_fail (i->save);
546
547   i->save (w);
548 }
549
550
551 /* Puts FILE_NAME into the recent list.
552    If it's already in the list, it moves it to the top
553 */
554 void
555 add_most_recent (const char *file_name, GtkRecentManager *rm)
556 {
557   gchar *uri = g_filename_to_uri  (file_name, NULL, NULL);
558
559   if ( uri )
560     gtk_recent_manager_add_item (rm, uri);
561
562   g_free (uri);
563 }
564