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_finalize        (GObject   *object);
22 static void psppire_window_register_dispose        (GObject   *object);
23 
24 static GObjectClass *parent_class = NULL;
25 
26 
27 enum  {
28   INSERTED,
29   REMOVED,
30   n_SIGNALS
31 };
32 
33 static guint signals [n_SIGNALS];
34 
G_DEFINE_TYPE(PsppireWindowRegister,psppire_window_register,G_TYPE_OBJECT)35 G_DEFINE_TYPE (PsppireWindowRegister, psppire_window_register, G_TYPE_OBJECT)
36 
37 static void
38 psppire_window_register_finalize (GObject *object)
39 {
40 }
41 
42 static void
psppire_window_register_dispose(GObject * object)43 psppire_window_register_dispose  (GObject *object)
44 {
45 }
46 
47 static PsppireWindowRegister *the_instance = NULL;
48 
49 static GObject*
psppire_window_register_construct(GType type,guint n_construct_params,GObjectConstructParam * construct_params)50 psppire_window_register_construct   (GType                  type,
51 				     guint                  n_construct_params,
52 				     GObjectConstructParam *construct_params)
53 {
54   GObject *object;
55 
56   if (!the_instance)
57     {
58       object = G_OBJECT_CLASS (parent_class)->constructor (type,
59                                                            n_construct_params,
60                                                            construct_params);
61       the_instance = PSPPIRE_WINDOW_REGISTER (object);
62     }
63   else
64     object = g_object_ref (G_OBJECT (the_instance));
65 
66   return object;
67 }
68 
69 static void
psppire_window_register_class_init(PsppireWindowRegisterClass * class)70 psppire_window_register_class_init (PsppireWindowRegisterClass *class)
71 {
72   GObjectClass *object_class;
73 
74   parent_class = g_type_class_peek_parent (class);
75   object_class = G_OBJECT_CLASS (class);
76 
77   object_class->finalize = psppire_window_register_finalize;
78   object_class->dispose = psppire_window_register_dispose;
79   object_class->constructor = psppire_window_register_construct;
80 
81   signals [INSERTED] =
82     g_signal_new ("inserted",
83 		  G_TYPE_FROM_CLASS (class),
84 		  G_SIGNAL_RUN_FIRST,
85 		  0,
86 		  NULL, NULL,
87 		  g_cclosure_marshal_VOID__POINTER,
88 		  G_TYPE_NONE,
89 		  1,
90 		  G_TYPE_POINTER);
91 
92   signals [REMOVED] =
93     g_signal_new ("removed",
94 		  G_TYPE_FROM_CLASS (class),
95 		  G_SIGNAL_RUN_FIRST,
96 		  0,
97 		  NULL, NULL,
98 		  g_cclosure_marshal_VOID__POINTER,
99 		  G_TYPE_NONE,
100 		  1,
101 		  G_TYPE_POINTER);
102 }
103 
104 static void
psppire_window_register_init(PsppireWindowRegister * window_register)105 psppire_window_register_init (PsppireWindowRegister *window_register)
106 {
107   window_register->name_table = g_hash_table_new (g_str_hash, g_str_equal);
108 }
109 
110 void
psppire_window_register_insert(PsppireWindowRegister * wr,PsppireWindow * window,const gchar * name)111 psppire_window_register_insert (PsppireWindowRegister *wr, PsppireWindow *window, const gchar *name)
112 {
113   g_hash_table_insert (wr->name_table, (gpointer) name, window);
114 
115   g_signal_emit (wr, signals[INSERTED], 0, name);
116 }
117 
118 void
psppire_window_register_remove(PsppireWindowRegister * wr,const gchar * name)119 psppire_window_register_remove (PsppireWindowRegister *wr, const gchar *name)
120 {
121   g_hash_table_remove (wr->name_table, (gpointer) name);
122   g_signal_emit (wr, signals[REMOVED], 0, name);
123 }
124 
125 PsppireWindow *
psppire_window_register_lookup(PsppireWindowRegister * wr,const gchar * name)126 psppire_window_register_lookup (PsppireWindowRegister *wr, const gchar *name)
127 {
128   return g_hash_table_lookup (wr->name_table, name);
129 }
130 
131 void
psppire_window_register_foreach(PsppireWindowRegister * wr,GHFunc func,gpointer data)132 psppire_window_register_foreach (PsppireWindowRegister *wr,
133 				 GHFunc func, gpointer data)
134 {
135   g_hash_table_foreach (wr->name_table, func, data);
136 }
137 
138 PsppireWindowRegister *
psppire_window_register_new(void)139 psppire_window_register_new (void)
140 {
141   return g_object_new (psppire_window_register_get_type (), NULL);
142 }
143 
144 
145 gint
psppire_window_register_n_items(PsppireWindowRegister * wr)146 psppire_window_register_n_items (PsppireWindowRegister *wr)
147 {
148   return g_hash_table_size (wr->name_table);
149 }
150