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