Prevent crash under certain pathological results.
[pspp-builds.git] / src / ui / gui / window-manager.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2006, 2007  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
18 #include <config.h>
19
20 #include "relocatable.h"
21
22 #include <glib.h>
23 #include "syntax-editor.h"
24 #include "data-editor.h"
25 #include "output-viewer.h"
26
27 #include <gettext.h>
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) msgid
30
31
32 #include "window-manager.h"
33
34
35
36 /* A list of struct editor_windows */
37 static GSList *window_list = NULL;
38
39
40 static void
41 deregister_window (GtkWindow *w, gpointer data)
42 {
43   struct editor_window *e = data;
44
45   window_list = g_slist_remove (window_list, e);
46
47   if ( g_slist_length (window_list) == 0 )
48     gtk_main_quit ();
49 };
50
51
52 static void
53 register_window (struct editor_window *e)
54 {
55   window_list = g_slist_prepend (window_list, e);
56 }
57
58
59 static gint
60 next_window_id (void)
61 {
62   return g_slist_length (window_list);
63 }
64
65 void
66 minimise_all_windows (void)
67 {
68   const GSList *i = NULL;
69
70   for (i = window_list; i != NULL ; i = i->next)
71     {
72       struct editor_window *e = i->data;
73       gtk_window_iconify (e->window);
74     }
75 }
76
77 static void set_window_name (struct editor_window *e, const gchar *name );
78
79
80 struct editor_window *
81 window_create (enum window_type type, const gchar *name)
82 {
83   struct editor_window *e;
84   switch (type)
85     {
86     case WINDOW_SYNTAX:
87       e = (struct editor_window *) new_syntax_editor ();
88       break;
89     case WINDOW_DATA:
90       e = (struct editor_window *) new_data_editor ();
91       break;
92     case WINDOW_OUTPUT:
93       e = (struct editor_window *) new_output_viewer ();
94       break;
95     default:
96       g_assert_not_reached ();
97     };
98
99   e->type = type;
100   e->name = NULL;
101
102   set_window_name (e, name);
103
104
105   gtk_window_set_icon_from_file (GTK_WINDOW (e->window),
106                                  relocate (PKGDATADIR "/psppicon.png"), 0);
107
108   g_signal_connect (e->window, "destroy",
109                     G_CALLBACK (deregister_window), e);
110
111   register_window (e);
112
113   gtk_widget_show (GTK_WIDGET (e->window));
114
115   return e;
116 }
117
118 void
119 default_window_name (struct editor_window *w)
120 {
121   set_window_name (w, NULL);
122 }
123
124 static void
125 set_window_name (struct editor_window *e,
126                  const gchar *name )
127 {
128   gchar mdash[6] = { 0, 0, 0, 0, 0, 0};
129
130   gchar *title ;
131   g_free (e->name);
132
133   e->name = NULL;
134
135   if ( name )
136     {
137       e->name =  g_strdup (name);
138       return;
139     }
140
141   g_unichar_to_utf8 (2014, mdash);
142
143   switch (e->type )
144     {
145     case WINDOW_SYNTAX:
146       e->name = g_strdup_printf (_("Syntax%d"), next_window_id () );
147       title = g_strdup_printf (_("%s %s PSPP Syntax Editor"), e->name, mdash);
148       break;
149     case WINDOW_DATA:
150       e->name = g_strdup_printf (_("Untitled%d"), next_window_id () );
151       title = g_strdup_printf (_("%s %s PSPP Data Editor"), e->name, mdash);
152       break;
153     case WINDOW_OUTPUT:
154       e->name = g_strdup_printf (_("Output%d"), next_window_id () );
155       title = g_strdup_printf (_("%s %s PSPP Output"), e->name, mdash);
156       break;
157     default:
158       g_assert_not_reached ();
159     }
160
161   gchar* x = g_locale_from_utf8  (title, -1, NULL, NULL, NULL);
162
163
164   gtk_window_set_title (GTK_WINDOW (e->window), x);
165
166   g_free (x);
167   g_free (title);
168 }
169
170
171 /* Set the name of this window based on FILENAME.
172    FILENAME is in "filename encoding" */
173 void
174 window_set_name_from_filename (struct editor_window *e,
175                                const gchar *fn)
176 {
177   gchar *title;
178   gchar *filename = g_filename_to_utf8 (fn, -1, NULL, NULL, NULL);
179   gchar *basename = g_path_get_basename (filename);
180
181   set_window_name (e, filename);
182
183   switch (e->type)
184     {
185     case WINDOW_SYNTAX:
186       title = g_strdup_printf (_("%s --- PSPP Syntax Editor"), basename);
187       break;
188     case WINDOW_DATA:
189       title = g_strdup_printf (_("%s --- PSPP Data Editor"), basename);
190       break;
191     default:
192       g_assert_not_reached ();
193     }
194   g_free (basename);
195
196   gchar* x = g_locale_from_utf8  (title, -1, NULL, NULL, NULL);
197
198
199   gtk_window_set_title (GTK_WINDOW (e->window), x);
200
201   g_free (x);
202   g_free (title);
203   g_free (filename);
204 }
205
206 const gchar *
207 window_name (const struct editor_window *e)
208 {
209   return e->name;
210 }