Enables Data Open/Save/New menuitems.
[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
127   if ( name )
128     {
129       e->name = g_strdup (name);
130       return ;
131     }
132
133   switch (e->type )
134     {
135     case WINDOW_SYNTAX:
136       e->name = g_strdup_printf (_("Syntax%d"), next_window_id () );
137       title = g_strdup_printf (_("%s --- PSPP Syntax Editor"), e->name);
138       break;
139     case WINDOW_DATA:
140       e->name = g_strdup_printf (_("Untitled%d"), next_window_id () );
141       title = g_strdup_printf (_("%s --- PSPP Data Editor"), e->name);
142       break;
143     default:
144       g_assert_not_reached ();
145     }
146
147   gtk_window_set_title (GTK_WINDOW (e->window), title);
148
149   g_free (title);
150 }
151
152
153 void
154 window_set_name_from_filename (struct editor_window *e,
155                                const gchar *filename)
156 {
157   gchar *title;
158   gchar *basename = g_path_get_basename (filename);
159
160   set_window_name (e, filename);
161
162   switch (e->type )
163     {
164     case WINDOW_SYNTAX:
165       title = g_strdup_printf (_("%s --- PSPP Syntax Editor"), basename);
166       break;
167     case WINDOW_DATA:
168       title = g_strdup_printf (_("%s --- PSPP Data Editor"), basename);
169       break;
170     default:
171       g_assert_not_reached ();
172     }
173   g_free (basename);
174
175   gtk_window_set_title (GTK_WINDOW (e->window), title);
176
177   g_free (title);
178 }
179
180 const gchar *
181 window_name (const struct editor_window *e)
182 {
183   return e->name;
184 }