Change license from GPLv2+ to GPLv3+.
[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
26 #include <gettext.h>
27 #define _(msgid) gettext (msgid)
28 #define N_(msgid) msgid
29
30
31 #include "window-manager.h"
32
33
34
35 /* A list of struct editor_windows */
36 static GSList *window_list = NULL;
37
38
39 static void
40 deregister_window (GtkWindow *w, gpointer data)
41 {
42   struct editor_window *e = data;
43
44   window_list = g_slist_remove (window_list, e);
45
46   if ( g_slist_length (window_list) == 0 )
47     gtk_main_quit ();
48 };
49
50
51 static void
52 register_window (struct editor_window *e)
53 {
54   window_list = g_slist_prepend (window_list, e);
55 }
56
57
58 static gint
59 next_window_id (void)
60 {
61   return g_slist_length (window_list);
62 }
63
64 void
65 minimise_all_windows (void)
66 {
67   const GSList *i = NULL;
68
69   for (i = window_list; i != NULL ; i = i->next)
70     {
71       struct editor_window *e = i->data;
72       gtk_window_iconify (e->window);
73     }
74 }
75
76 static void set_window_name (struct editor_window *e, const gchar *name );
77
78
79 struct editor_window *
80 window_create (enum window_type type, const gchar *name)
81 {
82   struct editor_window *e;
83   switch (type)
84     {
85     case WINDOW_SYNTAX:
86       e = (struct editor_window *) new_syntax_editor ();
87       break;
88     case WINDOW_DATA:
89       e = (struct editor_window *) new_data_editor ();
90       break;
91     default:
92       g_assert_not_reached ();
93     };
94
95   e->type = type;
96   e->name = NULL;
97
98   set_window_name (e, name);
99
100
101   gtk_window_set_icon_from_file (GTK_WINDOW (e->window),
102                                  relocate (PKGDATADIR "/psppicon.png"), 0);
103
104   g_signal_connect (e->window, "destroy",
105                     G_CALLBACK (deregister_window), e);
106
107   register_window (e);
108
109   gtk_widget_show (GTK_WIDGET (e->window));
110
111   return e;
112 }
113
114 void
115 default_window_name (struct editor_window *w)
116 {
117   set_window_name (w, NULL);
118 }
119
120 static void
121 set_window_name (struct editor_window *e,
122                  const gchar *name )
123 {
124   gchar *title ;
125   g_free (e->name);
126
127   e->name = NULL;
128
129   if ( name )
130     {
131       e->name =  g_strdup (name);
132       return;
133     }
134
135   switch (e->type )
136     {
137     case WINDOW_SYNTAX:
138       e->name = g_strdup_printf (_("Syntax%d"), next_window_id () );
139       title = g_strdup_printf (_("%s --- PSPP Syntax Editor"), e->name);
140       break;
141     case WINDOW_DATA:
142       e->name = g_strdup_printf (_("Untitled%d"), next_window_id () );
143       title = g_strdup_printf (_("%s --- PSPP Data Editor"), e->name);
144       break;
145     default:
146       g_assert_not_reached ();
147     }
148
149   gtk_window_set_title (GTK_WINDOW (e->window), title);
150
151   g_free (title);
152 }
153
154
155 /* Set the name of this window based on FILENAME.
156    FILENAME is in "filename encoding" */
157 void
158 window_set_name_from_filename (struct editor_window *e,
159                                const gchar *fn)
160 {
161   gchar *title;
162   gchar *filename = g_filename_to_utf8 (fn, -1, NULL, NULL, NULL);
163   gchar *basename = g_path_get_basename (filename);
164
165   set_window_name (e, filename);
166
167   switch (e->type)
168     {
169     case WINDOW_SYNTAX:
170       title = g_strdup_printf (_("%s --- PSPP Syntax Editor"), basename);
171       break;
172     case WINDOW_DATA:
173       title = g_strdup_printf (_("%s --- PSPP Data Editor"), basename);
174       break;
175     default:
176       g_assert_not_reached ();
177     }
178   g_free (basename);
179
180   gtk_window_set_title (GTK_WINDOW (e->window), title);
181
182   g_free (title);
183   g_free (filename);
184 }
185
186 const gchar *
187 window_name (const struct editor_window *e)
188 {
189   return e->name;
190 }