b78a341a0336d723ac4145e4ece3db2fcd39cc28
[pspp-builds.git] / src / ui / gui / window-manager.c
1 /*
2     PSPPIRE --- A Graphical User Interface for PSPP
3     Copyright (C) 2006, 2007  Free Software Foundation
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18     02110-1301, USA. */
19
20
21 #include <config.h>
22
23 #include "relocatable.h"
24
25 #include <glib.h>
26 #include "syntax-editor.h"
27 #include "data-editor.h"
28
29 #include <gettext.h>
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33
34 #include "window-manager.h"
35
36
37
38 /* A list of struct editor_windows */
39 static GSList *window_list = NULL;
40
41
42 static void
43 deregister_window (GtkWindow *w, gpointer data)
44 {
45   struct editor_window *e = data;
46
47   window_list = g_slist_remove (window_list, e);
48
49   if ( g_slist_length (window_list) == 0 )
50     gtk_main_quit ();
51 };
52
53
54 static void
55 register_window (struct editor_window *e)
56 {
57   window_list = g_slist_prepend (window_list, e);
58 }
59
60
61 static gint
62 next_window_id (void)
63 {
64   return g_slist_length (window_list);
65 }
66
67 void
68 minimise_all_windows (void)
69 {
70   const GSList *i = NULL;
71
72   for (i = window_list; i != NULL ; i = i->next)
73     {
74       struct editor_window *e = i->data;
75       gtk_window_iconify (e->window);
76     }
77 }
78
79 static void set_window_name (struct editor_window *e, const gchar *name );
80
81
82 struct editor_window *
83 window_create (enum window_type type, const gchar *name)
84 {
85   struct editor_window *e;
86   switch (type)
87     {
88     case WINDOW_SYNTAX:
89       e = (struct editor_window *) new_syntax_editor ();
90       break;
91     case WINDOW_DATA:
92       e = (struct editor_window *) new_data_editor ();
93       break;
94     default:
95       g_assert_not_reached ();
96     };
97
98   e->type = type;
99   e->name = NULL;
100
101   set_window_name (e, name);
102
103
104   gtk_window_set_icon_from_file (GTK_WINDOW (e->window),
105                                  relocate (PKGDATADIR "/psppicon.png"), 0);
106
107   g_signal_connect (e->window, "destroy",
108                     G_CALLBACK (deregister_window), e);
109
110   register_window (e);
111
112   gtk_widget_show (GTK_WIDGET (e->window));
113
114   return e;
115 }
116
117 void
118 default_window_name (struct editor_window *w)
119 {
120   set_window_name (w, NULL);
121 }
122
123 static void
124 set_window_name (struct editor_window *e,
125                  const gchar *name )
126 {
127   gchar *title ;
128   g_free (e->name);
129
130   e->name = NULL;
131
132   if ( name )
133     {
134       e->name =  g_strdup (name);
135       return;
136     }
137
138   switch (e->type )
139     {
140     case WINDOW_SYNTAX:
141       e->name = g_strdup_printf (_("Syntax%d"), next_window_id () );
142       title = g_strdup_printf (_("%s --- PSPP Syntax Editor"), e->name);
143       break;
144     case WINDOW_DATA:
145       e->name = g_strdup_printf (_("Untitled%d"), next_window_id () );
146       title = g_strdup_printf (_("%s --- PSPP Data Editor"), e->name);
147       break;
148     default:
149       g_assert_not_reached ();
150     }
151
152   gtk_window_set_title (GTK_WINDOW (e->window), title);
153
154   g_free (title);
155 }
156
157
158 /* Set the name of this window based on FILENAME.
159    FILENAME is in "filename encoding" */
160 void
161 window_set_name_from_filename (struct editor_window *e,
162                                const gchar *fn)
163 {
164   gchar *title;
165   gchar *filename = g_filename_to_utf8 (fn, -1, NULL, NULL, NULL);
166   gchar *basename = g_path_get_basename (filename);
167
168   set_window_name (e, filename);
169
170   switch (e->type)
171     {
172     case WINDOW_SYNTAX:
173       title = g_strdup_printf (_("%s --- PSPP Syntax Editor"), basename);
174       break;
175     case WINDOW_DATA:
176       title = g_strdup_printf (_("%s --- PSPP Data Editor"), basename);
177       break;
178     default:
179       g_assert_not_reached ();
180     }
181   g_free (basename);
182
183   gtk_window_set_title (GTK_WINDOW (e->window), title);
184
185   g_free (title);
186   g_free (filename);
187 }
188
189 const gchar *
190 window_name (const struct editor_window *e)
191 {
192   return e->name;
193 }