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