]> Pileus Git - ~andy/gtk/blob - gtk/gtkstatusbar.c
urg, removed implementation of gtk_marshal_VOID__INT_INT_INT_INT. if
[~andy/gtk] / gtk / gtkstatusbar.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  * GtkStatusbar Copyright (C) 1998 Shawn T. Amundson
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "gtkframe.h"
29 #include "gtklabel.h"
30 #include "gtksignal.h"
31 #include "gtkstatusbar.h"
32
33
34 enum
35 {
36   SIGNAL_TEXT_PUSHED,
37   SIGNAL_TEXT_POPPED,
38   SIGNAL_LAST
39 };
40
41 static void gtk_statusbar_class_init               (GtkStatusbarClass *class);
42 static void gtk_statusbar_init                     (GtkStatusbar      *statusbar);
43 static void gtk_statusbar_destroy                  (GtkObject         *object);
44 static void gtk_statusbar_update                   (GtkStatusbar      *statusbar,
45                                                     guint              context_id,
46                                                     const gchar       *text);
47      
48 static GtkContainerClass *parent_class;
49 static guint              statusbar_signals[SIGNAL_LAST] = { 0 };
50
51 GtkType      
52 gtk_statusbar_get_type (void)
53 {
54   static GtkType statusbar_type = 0;
55
56   if (!statusbar_type)
57     {
58       static const GtkTypeInfo statusbar_info =
59       {
60         "GtkStatusbar",
61         sizeof (GtkStatusbar),
62         sizeof (GtkStatusbarClass),
63         (GtkClassInitFunc) gtk_statusbar_class_init,
64         (GtkObjectInitFunc) gtk_statusbar_init,
65         /* reserved_1 */ NULL,
66         /* reserved_2 */ NULL,
67         (GtkClassInitFunc) NULL,
68       };
69
70       statusbar_type = gtk_type_unique (GTK_TYPE_HBOX, &statusbar_info);
71     }
72
73   return statusbar_type;
74 }
75
76 static void
77 gtk_statusbar_class_init (GtkStatusbarClass *class)
78 {
79   GtkObjectClass *object_class;
80   GtkWidgetClass *widget_class;
81   GtkContainerClass *container_class;
82
83   object_class = (GtkObjectClass *) class;
84   widget_class = (GtkWidgetClass *) class;
85   container_class = (GtkContainerClass *) class;
86
87   parent_class = gtk_type_class (GTK_TYPE_HBOX);
88   
89   object_class->destroy = gtk_statusbar_destroy;
90
91   class->messages_mem_chunk = g_mem_chunk_new ("GtkStatusBar messages mem chunk",
92                                                sizeof (GtkStatusbarMsg),
93                                                sizeof (GtkStatusbarMsg) * 64,
94                                                G_ALLOC_AND_FREE);
95
96   class->text_pushed = gtk_statusbar_update;
97   class->text_popped = gtk_statusbar_update;
98
99   statusbar_signals[SIGNAL_TEXT_PUSHED] =
100     gtk_signal_new ("text_pushed",
101                     GTK_RUN_LAST,
102                     GTK_CLASS_TYPE (object_class),
103                     GTK_SIGNAL_OFFSET (GtkStatusbarClass, text_pushed),
104                     gtk_marshal_VOID__UINT_STRING,
105                     GTK_TYPE_NONE, 2,
106                     GTK_TYPE_UINT,
107                     GTK_TYPE_STRING);
108   statusbar_signals[SIGNAL_TEXT_POPPED] =
109     gtk_signal_new ("text_popped",
110                     GTK_RUN_LAST,
111                     GTK_CLASS_TYPE (object_class),
112                     GTK_SIGNAL_OFFSET (GtkStatusbarClass, text_popped),
113                     gtk_marshal_VOID__UINT_STRING,
114                     GTK_TYPE_NONE, 2,
115                     GTK_TYPE_UINT,
116                     GTK_TYPE_STRING);
117   gtk_object_class_add_signals (object_class, statusbar_signals, SIGNAL_LAST);
118 }
119
120 static void
121 gtk_statusbar_init (GtkStatusbar *statusbar)
122 {
123   GtkBox *box;
124
125   box = GTK_BOX (statusbar);
126
127   box->spacing = 2;
128   box->homogeneous = FALSE;
129
130   statusbar->frame = gtk_frame_new (NULL);
131   gtk_frame_set_shadow_type (GTK_FRAME (statusbar->frame), GTK_SHADOW_IN);
132   gtk_box_pack_start (box, statusbar->frame, TRUE, TRUE, 0);
133   gtk_widget_show (statusbar->frame);
134
135   statusbar->label = gtk_label_new ("");
136   gtk_misc_set_alignment (GTK_MISC (statusbar->label), 0.0, 0.0);
137   gtk_container_add (GTK_CONTAINER (statusbar->frame), statusbar->label);
138   gtk_widget_show (statusbar->label);
139
140   statusbar->seq_context_id = 1;
141   statusbar->seq_message_id = 1;
142   statusbar->messages = NULL;
143   statusbar->keys = NULL;
144 }
145
146 GtkWidget* 
147 gtk_statusbar_new (void)
148 {
149   return gtk_type_new (GTK_TYPE_STATUSBAR);
150 }
151
152 static void
153 gtk_statusbar_update (GtkStatusbar *statusbar,
154                       guint         context_id,
155                       const gchar  *text)
156 {
157   g_return_if_fail (statusbar != NULL);
158   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
159
160   if (!text)
161     text = "";
162
163   gtk_label_set_text (GTK_LABEL (statusbar->label), text);
164 }
165
166 guint
167 gtk_statusbar_get_context_id (GtkStatusbar *statusbar,
168                               const gchar  *context_description)
169 {
170   gchar *string;
171   guint *id;
172   
173   g_return_val_if_fail (statusbar != NULL, 0);
174   g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
175   g_return_val_if_fail (context_description != NULL, 0);
176
177   /* we need to preserve namespaces on object datas */
178   string = g_strconcat ("gtk-status-bar-context:", context_description, NULL);
179
180   id = gtk_object_get_data (GTK_OBJECT (statusbar), string);
181   if (!id)
182     {
183       id = g_new (guint, 1);
184       *id = statusbar->seq_context_id++;
185       gtk_object_set_data_full (GTK_OBJECT (statusbar), string, id, (GtkDestroyNotify) g_free);
186       statusbar->keys = g_slist_prepend (statusbar->keys, string);
187     }
188   else
189     g_free (string);
190
191   return *id;
192 }
193
194 guint
195 gtk_statusbar_push (GtkStatusbar *statusbar,
196                     guint         context_id,
197                     const gchar  *text)
198 {
199   GtkStatusbarMsg *msg;
200   GtkStatusbarClass *class;
201
202   g_return_val_if_fail (statusbar != NULL, 0);
203   g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
204   g_return_val_if_fail (text != NULL, 0);
205
206   class = GTK_STATUSBAR_GET_CLASS (statusbar);
207   msg = g_chunk_new (GtkStatusbarMsg, class->messages_mem_chunk);
208   msg->text = g_strdup (text);
209   msg->context_id = context_id;
210   msg->message_id = statusbar->seq_message_id++;
211
212   statusbar->messages = g_slist_prepend (statusbar->messages, msg);
213
214   gtk_signal_emit (GTK_OBJECT (statusbar),
215                    statusbar_signals[SIGNAL_TEXT_PUSHED],
216                    msg->context_id,
217                    msg->text);
218
219   return msg->message_id;
220 }
221
222 void
223 gtk_statusbar_pop (GtkStatusbar *statusbar,
224                    guint         context_id)
225 {
226   GtkStatusbarMsg *msg;
227
228   g_return_if_fail (statusbar != NULL);
229   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
230
231   if (statusbar->messages)
232     {
233       GSList *list;
234
235       for (list = statusbar->messages; list; list = list->next)
236         {
237           msg = list->data;
238
239           if (msg->context_id == context_id)
240             {
241               GtkStatusbarClass *class;
242
243               class = GTK_STATUSBAR_GET_CLASS (statusbar);
244
245               statusbar->messages = g_slist_remove_link (statusbar->messages,
246                                                          list);
247               g_free (msg->text);
248               g_mem_chunk_free (class->messages_mem_chunk, msg);
249               g_slist_free_1 (list);
250               break;
251             }
252         }
253     }
254
255   msg = statusbar->messages ? statusbar->messages->data : NULL;
256
257   gtk_signal_emit (GTK_OBJECT (statusbar),
258                    statusbar_signals[SIGNAL_TEXT_POPPED],
259                    (guint) (msg ? msg->context_id : 0),
260                    msg ? msg->text : NULL);
261 }
262
263 void
264 gtk_statusbar_remove (GtkStatusbar *statusbar,
265                       guint        context_id,
266                       guint        message_id)
267 {
268   GtkStatusbarMsg *msg;
269
270   g_return_if_fail (statusbar != NULL);
271   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
272   g_return_if_fail (message_id > 0);
273
274   msg = statusbar->messages ? statusbar->messages->data : NULL;
275   if (msg)
276     {
277       GSList *list;
278
279       /* care about signal emission if the topmost item is removed */
280       if (msg->context_id == context_id &&
281           msg->message_id == message_id)
282         {
283           gtk_statusbar_pop (statusbar, context_id);
284           return;
285         }
286       
287       for (list = statusbar->messages; list; list = list->next)
288         {
289           msg = list->data;
290           
291           if (msg->context_id == context_id &&
292               msg->message_id == message_id)
293             {
294               GtkStatusbarClass *class;
295               
296               class = GTK_STATUSBAR_GET_CLASS (statusbar);
297               statusbar->messages = g_slist_remove_link (statusbar->messages, list);
298               g_free (msg->text);
299               g_mem_chunk_free (class->messages_mem_chunk, msg);
300               g_slist_free_1 (list);
301               
302               break;
303             }
304         }
305     }
306 }
307
308 static void
309 gtk_statusbar_destroy (GtkObject *object)
310 {
311   GtkStatusbar *statusbar;
312   GtkStatusbarClass *class;
313   GSList *list;
314
315   g_return_if_fail (object != NULL);
316   g_return_if_fail (GTK_IS_STATUSBAR (object));
317
318   statusbar = GTK_STATUSBAR (object);
319   class = GTK_STATUSBAR_GET_CLASS (statusbar);
320
321   for (list = statusbar->messages; list; list = list->next)
322     {
323       GtkStatusbarMsg *msg;
324
325       msg = list->data;
326       g_free (msg->text);
327       g_mem_chunk_free (class->messages_mem_chunk, msg);
328     }
329   g_slist_free (statusbar->messages);
330   statusbar->messages = NULL;
331
332   for (list = statusbar->keys; list; list = list->next)
333     g_free (list->data);
334   g_slist_free (statusbar->keys);
335   statusbar->keys = NULL;
336
337   GTK_OBJECT_CLASS (parent_class)->destroy (object);
338 }