Fix display of window icon
[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
25 #include <stdlib.h>
26
27 #include <gettext.h>
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
30
31 #include "psppire-window.h"
32 #include "psppire-window-register.h"
33
34 static void psppire_window_base_finalize (PsppireWindowClass *, gpointer);
35 static void psppire_window_base_init     (PsppireWindowClass *class);
36 static void psppire_window_class_init    (PsppireWindowClass *class);
37 static void psppire_window_init          (PsppireWindow      *window);
38
39
40 static PsppireWindowClass *the_class;
41 static GObjectClass *parent_class;
42
43 GType
44 psppire_window_get_type (void)
45 {
46   static GType psppire_window_type = 0;
47
48   if (!psppire_window_type)
49     {
50       static const GTypeInfo psppire_window_info =
51       {
52         sizeof (PsppireWindowClass),
53         (GBaseInitFunc) psppire_window_base_init,
54         (GBaseFinalizeFunc) psppire_window_base_finalize,
55         (GClassInitFunc) psppire_window_class_init,
56         (GClassFinalizeFunc) NULL,
57         NULL,
58         sizeof (PsppireWindow),
59         0,
60         (GInstanceInitFunc) psppire_window_init,
61       };
62
63       psppire_window_type =
64         g_type_register_static (GTK_TYPE_WINDOW, "PsppireWindow",
65                                 &psppire_window_info, G_TYPE_FLAG_ABSTRACT);
66     }
67
68   return psppire_window_type;
69 }
70
71
72 /* Properties */
73 enum
74 {
75   PROP_0,
76   PROP_FILENAME,
77   PROP_DESCRIPTION
78 };
79
80
81 gchar *
82 uniquify (const gchar *str, int *x)
83 {
84   return g_strdup_printf ("%s%d", str, (*x)++);
85 }
86
87 static gchar mdash[6] = {0,0,0,0,0,0};
88
89 static void
90 psppire_window_set_title (PsppireWindow *window)
91 {
92   GString *title = g_string_sized_new (80);
93
94   g_string_printf (title, _("%s %s PSPPIRE %s"),
95                     window->basename, mdash, window->description);
96
97   if ( window->unsaved)
98     g_string_prepend_c (title, '*');
99
100   gtk_window_set_title (GTK_WINDOW (window), title->str);
101
102   g_string_free (title, TRUE);
103 }
104
105 static void
106 psppire_window_set_property (GObject         *object,
107                              guint            prop_id,
108                              const GValue    *value,
109                              GParamSpec      *pspec)
110 {
111   PsppireWindow *window = PSPPIRE_WINDOW (object);
112
113   switch (prop_id)
114     {
115     case PROP_DESCRIPTION:
116       window->description = g_value_dup_string (value);
117       psppire_window_set_title (window);
118       break;
119     case PROP_FILENAME:
120       {
121         PsppireWindowRegister *reg = psppire_window_register_new ();
122
123         gchar *candidate_name ;
124
125         {
126           const gchar *name = g_value_get_string (value);
127           int x = 0;
128           GValue def = {0};
129           g_value_init (&def, pspec->value_type);
130
131           if ( NULL == name)
132             {
133               g_param_value_set_default (pspec, &def);
134               name = g_value_get_string (&def);
135             }
136
137           candidate_name = strdup (name);
138
139           while ( psppire_window_register_lookup (reg, candidate_name))
140             {
141               free (candidate_name);
142               candidate_name = uniquify (name, &x);
143             }
144
145           window->basename = g_path_get_basename (candidate_name);
146
147           g_value_unset (&def);
148         }
149
150         psppire_window_set_title (window);
151
152         if ( window->name)
153           psppire_window_register_remove (reg, window->name);
154
155         free (window->name);
156         window->name = candidate_name;
157
158         psppire_window_register_insert (reg, window, window->name);
159       }
160       break;
161     default:
162       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163       break;
164     };
165 }
166
167
168 static void
169 psppire_window_get_property (GObject         *object,
170                              guint            prop_id,
171                              GValue          *value,
172                              GParamSpec      *pspec)
173 {
174   PsppireWindow *window = PSPPIRE_WINDOW (object);
175
176   switch (prop_id)
177     {
178     case PROP_FILENAME:
179       g_value_set_string (value, window->name);
180       break;
181     case PROP_DESCRIPTION:
182       g_value_set_string (value, window->description);
183       break;
184     default:
185       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186       break;
187     };
188 }
189
190
191
192 static void
193 psppire_window_finalize (GObject *object)
194 {
195   PsppireWindow *window = PSPPIRE_WINDOW (object);
196
197   PsppireWindowRegister *reg = psppire_window_register_new ();
198
199   psppire_window_register_remove (reg, window->name);
200   free (window->name);
201   free (window->description);
202
203   g_signal_handler_disconnect (psppire_window_register_new (),
204                                window->remove_handler);
205
206   g_signal_handler_disconnect (psppire_window_register_new (),
207                                window->insert_handler);
208
209   g_hash_table_destroy (window->menuitem_table);
210
211   if (G_OBJECT_CLASS (parent_class)->finalize)
212     G_OBJECT_CLASS (parent_class)->finalize (object);
213 }
214
215
216 static void
217 psppire_window_class_init (PsppireWindowClass *class)
218 {
219   GObjectClass *object_class = G_OBJECT_CLASS (class);
220
221   GParamSpec *description_spec =
222     g_param_spec_string ("description",
223                        "Description",
224                        "A string describing the usage of the window",
225                          "??????", /*Should be overridden by derived classes */
226                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
227
228   GParamSpec *filename_spec =
229     g_param_spec_string ("filename",
230                        "File name",
231                        "The name of the file associated with this window, if any",
232                          "Untitled",
233                          G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
234
235   g_unichar_to_utf8 (0x2014, mdash);
236
237   object_class->set_property = psppire_window_set_property;
238   object_class->get_property = psppire_window_get_property;
239
240   g_object_class_install_property (object_class,
241                                    PROP_DESCRIPTION,
242                                    description_spec);
243
244   g_object_class_install_property (object_class,
245                                    PROP_FILENAME,
246                                    filename_spec);
247
248   the_class = class;
249   parent_class = g_type_class_peek_parent (class);
250 }
251
252
253 static void
254 psppire_window_base_init (PsppireWindowClass *class)
255 {
256   GObjectClass *object_class = G_OBJECT_CLASS (class);
257
258   object_class->finalize = psppire_window_finalize;
259 }
260
261
262
263 static void
264 psppire_window_base_finalize (PsppireWindowClass *class,
265                                 gpointer class_data)
266 {
267 }
268
269 static void
270 menu_toggled (GtkCheckMenuItem *mi, gpointer data)
271 {
272   /* Prohibit changes to the state */
273   mi->active = !mi->active;
274 }
275
276
277 /* Look up the window associated with this menuitem and present it to the user */
278 static void
279 menu_activate (GtkMenuItem *mi, gpointer data)
280 {
281   const gchar *key = data;
282
283   PsppireWindowRegister *reg = psppire_window_register_new ();
284
285   PsppireWindow *window = psppire_window_register_lookup (reg, key);
286
287   gtk_window_present (GTK_WINDOW (window));
288 }
289
290 static void
291 insert_menuitem_into_menu (PsppireWindow *window, gpointer key)
292 {
293   GtkWidget *item = gtk_check_menu_item_new_with_label (key);
294
295   g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL);
296   g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key);
297
298   gtk_widget_show (item);
299
300   gtk_menu_shell_append (window->menu, item);
301
302   /* Set the state without emitting a signal */
303   GTK_CHECK_MENU_ITEM (item)->active =
304    (psppire_window_register_lookup (psppire_window_register_new (), key) == window);
305
306   g_hash_table_insert (window->menuitem_table, key, item);
307 }
308
309 static void
310 insert_item (gpointer key, gpointer value, gpointer data)
311 {
312   PsppireWindow *window = PSPPIRE_WINDOW (data);
313
314   if ( NULL != g_hash_table_lookup (window->menuitem_table, key))
315     return;
316
317   insert_menuitem_into_menu (window, key);
318 }
319
320 /* Insert a new item into the window menu */
321 static void
322 insert_menuitem (GObject *reg, const gchar *key, gpointer data)
323 {
324   PsppireWindow *window = PSPPIRE_WINDOW (data);
325   
326   insert_menuitem_into_menu (window, (gpointer) key);
327 }
328
329
330 static void
331 remove_menuitem (PsppireWindowRegister *reg, const gchar *key, gpointer data)
332 {
333   PsppireWindow *window = PSPPIRE_WINDOW (data);
334   GtkWidget *item ;
335
336   item = g_hash_table_lookup (window->menuitem_table, key);
337
338   g_hash_table_remove (window->menuitem_table, key);
339
340   if (GTK_IS_CONTAINER (window->menu))
341     gtk_container_remove (GTK_CONTAINER (window->menu), item);
342 }
343
344 static void
345 insert_existing_items (PsppireWindow *window)
346 {
347   psppire_window_register_foreach (psppire_window_register_new (), insert_item, window);
348 }
349
350
351 static gboolean
352 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
353 {
354   PsppireWindow *dw = PSPPIRE_WINDOW (user_data);
355
356   PsppireWindowRegister *reg = psppire_window_register_new ();
357
358
359   if ( 1 == psppire_window_register_n_items (reg))
360     gtk_main_quit ();
361
362   return FALSE;
363 }
364
365
366 static void
367 psppire_window_init (PsppireWindow *window)
368 {
369   window->name = NULL;
370   window->menu = NULL;
371
372   window->menuitem_table  = g_hash_table_new (g_str_hash, g_str_equal);
373
374
375   g_signal_connect (window,  "realize", G_CALLBACK (insert_existing_items), NULL);
376
377   window->insert_handler = g_signal_connect (psppire_window_register_new (),
378                                              "inserted",
379                                              G_CALLBACK (insert_menuitem),
380                                              window);
381
382   window->remove_handler = g_signal_connect (psppire_window_register_new (),
383                                              "removed",
384                                              G_CALLBACK (remove_menuitem),
385                                              window);
386
387   window->unsaved = FALSE;
388
389   g_signal_connect (window, "delete-event", G_CALLBACK (on_delete), window);
390
391   g_object_set (window, "icon-name", "psppicon", NULL);
392 }
393
394
395 /* If the buffer's modified flag is set,
396    ask the user if the buffer should be saved.
397    Return TRUE if is should.
398 */
399 gboolean
400 psppire_window_query_save (PsppireWindow *se)
401 {
402   gint response;
403   GtkWidget *dialog;
404
405   const gchar *description;
406   const gchar *filename = psppire_window_get_filename (se);
407
408   if ( ! psppire_window_get_unsaved (se))
409     return FALSE;
410
411   g_object_get (se, "description", &description, NULL);
412
413   g_return_val_if_fail (filename != NULL, FALSE);
414
415   dialog =
416     gtk_message_dialog_new (GTK_WINDOW (se),
417                             GTK_DIALOG_MODAL,
418                             GTK_MESSAGE_QUESTION,
419                             GTK_BUTTONS_NONE,
420                             _("Save contents of %s to \"%s\"?"),
421                             description,
422                             filename);
423
424   gtk_dialog_add_button  (GTK_DIALOG (dialog),
425                           GTK_STOCK_YES,
426                           GTK_RESPONSE_ACCEPT);
427
428   gtk_dialog_add_button  (GTK_DIALOG (dialog),
429                           GTK_STOCK_NO,
430                           GTK_RESPONSE_REJECT);
431
432   gtk_dialog_add_button  (GTK_DIALOG (dialog),
433                           GTK_STOCK_CANCEL,
434                           GTK_RESPONSE_CANCEL);
435
436   response = gtk_dialog_run (GTK_DIALOG (dialog));
437
438   gtk_widget_destroy (dialog);
439
440   if ( response == GTK_RESPONSE_ACCEPT )
441     {
442       return TRUE;
443     }
444
445   return FALSE;
446 }
447
448
449 const gchar *
450 psppire_window_get_filename (PsppireWindow *w)
451 {
452   const gchar *name = NULL;
453   g_object_get (w, "filename", &name, NULL);
454   return name;
455 }
456
457
458 void
459 psppire_window_set_filename (PsppireWindow *w, const gchar *filename)
460 {
461   g_object_set (w, "filename", filename, NULL);
462 }
463
464 void
465 psppire_window_set_unsaved (PsppireWindow *w, gboolean unsaved)
466 {
467   w->unsaved = unsaved;
468
469   psppire_window_set_title (w);
470 }
471
472 gboolean
473 psppire_window_get_unsaved (PsppireWindow *w)
474 {
475   return w->unsaved;
476 }
477
478
479 \f
480
481
482 static void
483 minimise_window (gpointer key, gpointer value, gpointer data)
484 {
485   gtk_window_iconify (GTK_WINDOW (value));
486 }
487
488
489 void
490 psppire_window_minimise_all (void)
491 {
492   PsppireWindowRegister *reg = psppire_window_register_new ();
493
494   g_hash_table_foreach (reg->name_table, minimise_window, NULL);
495 }