Add #include <config.h>.
[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 <glib.h>
24 #include "syntax-editor.h"
25 #include "data-editor.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     default:
93       g_assert_not_reached ();
94     };
95
96   e->type = type;
97   e->name = NULL;
98
99   set_window_name (e, name);
100
101
102   gtk_window_set_icon_from_file (GTK_WINDOW (e->window),
103                                  PKGDATADIR "/psppicon.png", 0);
104
105   g_signal_connect (e->window, "destroy",
106                     G_CALLBACK (deregister_window), e);
107
108   register_window (e);
109
110   gtk_widget_show (GTK_WIDGET (e->window));
111
112   return e;
113 }
114
115 void
116 default_window_name (struct editor_window *w)
117 {
118   set_window_name (w, NULL);
119 }
120
121 static void
122 set_window_name (struct editor_window *e,
123                  const gchar *name )
124 {
125   gchar *title ;
126   g_free (e->name);
127
128   e->name = NULL;
129
130   if ( name )
131     {
132       e->name =  g_strdup (name);
133       return;
134     }
135
136   switch (e->type )
137     {
138     case WINDOW_SYNTAX:
139       e->name = g_strdup_printf (_("Syntax%d"), next_window_id () );
140       title = g_strdup_printf (_("%s --- PSPP Syntax Editor"), e->name);
141       break;
142     case WINDOW_DATA:
143       e->name = g_strdup_printf (_("Untitled%d"), next_window_id () );
144       title = g_strdup_printf (_("%s --- PSPP Data Editor"), e->name);
145       break;
146     default:
147       g_assert_not_reached ();
148     }
149
150   gtk_window_set_title (GTK_WINDOW (e->window), title);
151
152   g_free (title);
153 }
154
155
156 /* Set the name of this window based on FILENAME.
157    FILENAME is in "filename encoding" */
158 void
159 window_set_name_from_filename (struct editor_window *e,
160                                const gchar *filename)
161 {
162   gchar *title;
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 }
184
185 const gchar *
186 window_name (const struct editor_window *e)
187 {
188   return e->name;
189 }