Merge commit 'origin/stable'
[pspp-builds.git] / src / ui / gui / psppire-window-register.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008  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 #include <config.h>
18
19 #include "psppire-window-register.h"
20
21 static void psppire_window_register_init            (PsppireWindowRegister      *window_register);
22 static void psppire_window_register_class_init      (PsppireWindowRegisterClass *class);
23
24 static void psppire_window_register_finalize        (GObject   *object);
25 static void psppire_window_register_dispose        (GObject   *object);
26
27 static GObjectClass *parent_class = NULL;
28
29
30 enum  {
31   INSERTED,
32   REMOVED,
33   n_SIGNALS
34 };
35
36 static guint signals [n_SIGNALS];
37
38 GType
39 psppire_window_register_get_type (void)
40 {
41   static GType window_register_type = 0;
42
43   if (!window_register_type)
44     {
45       static const GTypeInfo window_register_info =
46       {
47         sizeof (PsppireWindowRegisterClass),
48         NULL,           /* base_init */
49         NULL,           /* base_finalize */
50         (GClassInitFunc) psppire_window_register_class_init,
51         NULL,           /* class_finalize */
52         NULL,           /* class_data */
53         sizeof (PsppireWindowRegister),
54         0,
55         (GInstanceInitFunc) psppire_window_register_init,
56       };
57
58       window_register_type = g_type_register_static (G_TYPE_OBJECT,
59                                                 "PsppireWindowRegister",
60                                                 &window_register_info, 0);
61     }
62
63   return window_register_type;
64 }
65
66
67 static void
68 psppire_window_register_finalize (GObject *object)
69 {
70 }
71
72 static void
73 psppire_window_register_dispose  (GObject *object)
74 {
75 }
76
77 static PsppireWindowRegister *the_instance = NULL;
78
79 static GObject*
80 psppire_window_register_construct   (GType                  type,
81                                      guint                  n_construct_params,
82                                      GObjectConstructParam *construct_params)
83 {
84   GObject *object;
85   
86   if (!the_instance)
87     {
88       object = G_OBJECT_CLASS (parent_class)->constructor (type,
89                                                            n_construct_params,
90                                                            construct_params);
91       the_instance = PSPPIRE_WINDOW_REGISTER (object);
92     }
93   else
94     object = g_object_ref (G_OBJECT (the_instance));
95
96   return object;
97 }
98
99 static void
100 psppire_window_register_class_init (PsppireWindowRegisterClass *class)
101 {
102   GObjectClass *object_class;
103
104   parent_class = g_type_class_peek_parent (class);
105   object_class = (GObjectClass*) class;
106
107   object_class->finalize = psppire_window_register_finalize;
108   object_class->dispose = psppire_window_register_dispose;
109   object_class->constructor = psppire_window_register_construct;
110
111   signals [INSERTED] =
112     g_signal_new ("inserted",
113                   G_TYPE_FROM_CLASS (class),
114                   G_SIGNAL_RUN_FIRST,
115                   0,
116                   NULL, NULL,
117                   g_cclosure_marshal_VOID__POINTER,
118                   G_TYPE_NONE,
119                   1,
120                   G_TYPE_POINTER);
121
122   signals [REMOVED] =
123     g_signal_new ("removed",
124                   G_TYPE_FROM_CLASS (class),
125                   G_SIGNAL_RUN_FIRST,
126                   0,
127                   NULL, NULL,
128                   g_cclosure_marshal_VOID__POINTER,
129                   G_TYPE_NONE,
130                   1,
131                   G_TYPE_POINTER);
132 }
133
134 static void
135 psppire_window_register_init (PsppireWindowRegister *window_register)
136 {
137   window_register->dispose_has_run = FALSE;
138   window_register->name_table = g_hash_table_new (g_str_hash, g_str_equal);
139 }
140
141 void
142 psppire_window_register_insert (PsppireWindowRegister *wr, PsppireWindow *window, const gchar *name)
143 {
144   g_hash_table_insert (wr->name_table, (gpointer) name, window);
145
146   g_signal_emit (wr, signals[INSERTED], 0, name);
147 }
148
149
150 void
151 psppire_window_register_remove (PsppireWindowRegister *wr, const gchar *name)
152 {
153   g_signal_emit (wr, signals[REMOVED], 0, name);
154
155   g_hash_table_remove (wr->name_table, (gpointer) name);
156 }
157
158 PsppireWindow *
159 psppire_window_register_lookup (PsppireWindowRegister *wr, const gchar *name)
160 {
161   return g_hash_table_lookup (wr->name_table, name);
162 }
163
164 void
165 psppire_window_register_foreach (PsppireWindowRegister *wr,
166                                  GHFunc func, gpointer data)
167 {
168   g_hash_table_foreach (wr->name_table, func, data);
169 }
170
171 static void
172 minimise_window (gpointer key, gpointer value, gpointer data)
173 {
174   gtk_window_iconify (GTK_WINDOW (value));
175 }
176
177
178 void
179 psppire_window_register_minimise_all (PsppireWindowRegister *wr)
180 {
181   g_hash_table_foreach (wr->name_table, minimise_window, wr);
182 }
183
184
185
186 PsppireWindowRegister *
187 psppire_window_register_new (void)
188 {
189   return g_object_new (psppire_window_register_get_type (), NULL);
190 }
191
192
193 gint
194 psppire_window_register_n_items (PsppireWindowRegister *wr)
195 {
196   return g_hash_table_size (wr->name_table);
197 }