]> Pileus Git - ~andy/gtk/blob - gtk/gtkstatusbar.c
doh, this was broken beyond believe.
[~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 }
118
119 static void
120 gtk_statusbar_init (GtkStatusbar *statusbar)
121 {
122   GtkBox *box;
123
124   box = GTK_BOX (statusbar);
125
126   box->spacing = 2;
127   box->homogeneous = FALSE;
128
129   statusbar->frame = gtk_frame_new (NULL);
130   gtk_frame_set_shadow_type (GTK_FRAME (statusbar->frame), GTK_SHADOW_IN);
131   gtk_box_pack_start (box, statusbar->frame, TRUE, TRUE, 0);
132   gtk_widget_show (statusbar->frame);
133
134   statusbar->label = gtk_label_new ("");
135   gtk_misc_set_alignment (GTK_MISC (statusbar->label), 0.0, 0.0);
136   gtk_container_add (GTK_CONTAINER (statusbar->frame), statusbar->label);
137   gtk_widget_show (statusbar->label);
138
139   statusbar->seq_context_id = 1;
140   statusbar->seq_message_id = 1;
141   statusbar->messages = NULL;
142   statusbar->keys = NULL;
143 }
144
145 GtkWidget* 
146 gtk_statusbar_new (void)
147 {
148   return gtk_type_new (GTK_TYPE_STATUSBAR);
149 }
150
151 static void
152 gtk_statusbar_update (GtkStatusbar *statusbar,
153                       guint         context_id,
154                       const gchar  *text)
155 {
156   g_return_if_fail (statusbar != NULL);
157   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
158
159   if (!text)
160     text = "";
161
162   gtk_label_set_text (GTK_LABEL (statusbar->label), text);
163 }
164
165 guint
166 gtk_statusbar_get_context_id (GtkStatusbar *statusbar,
167                               const gchar  *context_description)
168 {
169   gchar *string;
170   guint *id;
171   
172   g_return_val_if_fail (statusbar != NULL, 0);
173   g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
174   g_return_val_if_fail (context_description != NULL, 0);
175
176   /* we need to preserve namespaces on object datas */
177   string = g_strconcat ("gtk-status-bar-context:", context_description, NULL);
178
179   id = gtk_object_get_data (GTK_OBJECT (statusbar), string);
180   if (!id)
181     {
182       id = g_new (guint, 1);
183       *id = statusbar->seq_context_id++;
184       gtk_object_set_data_full (GTK_OBJECT (statusbar), string, id, (GtkDestroyNotify) g_free);
185       statusbar->keys = g_slist_prepend (statusbar->keys, string);
186     }
187   else
188     g_free (string);
189
190   return *id;
191 }
192
193 guint
194 gtk_statusbar_push (GtkStatusbar *statusbar,
195                     guint         context_id,
196                     const gchar  *text)
197 {
198   GtkStatusbarMsg *msg;
199   GtkStatusbarClass *class;
200
201   g_return_val_if_fail (statusbar != NULL, 0);
202   g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
203   g_return_val_if_fail (text != NULL, 0);
204
205   class = GTK_STATUSBAR_GET_CLASS (statusbar);
206   msg = g_chunk_new (GtkStatusbarMsg, class->messages_mem_chunk);
207   msg->text = g_strdup (text);
208   msg->context_id = context_id;
209   msg->message_id = statusbar->seq_message_id++;
210
211   statusbar->messages = g_slist_prepend (statusbar->messages, msg);
212
213   gtk_signal_emit (GTK_OBJECT (statusbar),
214                    statusbar_signals[SIGNAL_TEXT_PUSHED],
215                    msg->context_id,
216                    msg->text);
217
218   return msg->message_id;
219 }
220
221 void
222 gtk_statusbar_pop (GtkStatusbar *statusbar,
223                    guint         context_id)
224 {
225   GtkStatusbarMsg *msg;
226
227   g_return_if_fail (statusbar != NULL);
228   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
229
230   if (statusbar->messages)
231     {
232       GSList *list;
233
234       for (list = statusbar->messages; list; list = list->next)
235         {
236           msg = list->data;
237
238           if (msg->context_id == context_id)
239             {
240               GtkStatusbarClass *class;
241
242               class = GTK_STATUSBAR_GET_CLASS (statusbar);
243
244               statusbar->messages = g_slist_remove_link (statusbar->messages,
245                                                          list);
246               g_free (msg->text);
247               g_mem_chunk_free (class->messages_mem_chunk, msg);
248               g_slist_free_1 (list);
249               break;
250             }
251         }
252     }
253
254   msg = statusbar->messages ? statusbar->messages->data : NULL;
255
256   gtk_signal_emit (GTK_OBJECT (statusbar),
257                    statusbar_signals[SIGNAL_TEXT_POPPED],
258                    (guint) (msg ? msg->context_id : 0),
259                    msg ? msg->text : NULL);
260 }
261
262 void
263 gtk_statusbar_remove (GtkStatusbar *statusbar,
264                       guint        context_id,
265                       guint        message_id)
266 {
267   GtkStatusbarMsg *msg;
268
269   g_return_if_fail (statusbar != NULL);
270   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
271   g_return_if_fail (message_id > 0);
272
273   msg = statusbar->messages ? statusbar->messages->data : NULL;
274   if (msg)
275     {
276       GSList *list;
277
278       /* care about signal emission if the topmost item is removed */
279       if (msg->context_id == context_id &&
280           msg->message_id == message_id)
281         {
282           gtk_statusbar_pop (statusbar, context_id);
283           return;
284         }
285       
286       for (list = statusbar->messages; list; list = list->next)
287         {
288           msg = list->data;
289           
290           if (msg->context_id == context_id &&
291               msg->message_id == message_id)
292             {
293               GtkStatusbarClass *class;
294               
295               class = GTK_STATUSBAR_GET_CLASS (statusbar);
296               statusbar->messages = g_slist_remove_link (statusbar->messages, list);
297               g_free (msg->text);
298               g_mem_chunk_free (class->messages_mem_chunk, msg);
299               g_slist_free_1 (list);
300               
301               break;
302             }
303         }
304     }
305 }
306
307 static void
308 gtk_statusbar_destroy (GtkObject *object)
309 {
310   GtkStatusbar *statusbar;
311   GtkStatusbarClass *class;
312   GSList *list;
313
314   g_return_if_fail (object != NULL);
315   g_return_if_fail (GTK_IS_STATUSBAR (object));
316
317   statusbar = GTK_STATUSBAR (object);
318   class = GTK_STATUSBAR_GET_CLASS (statusbar);
319
320   for (list = statusbar->messages; list; list = list->next)
321     {
322       GtkStatusbarMsg *msg;
323
324       msg = list->data;
325       g_free (msg->text);
326       g_mem_chunk_free (class->messages_mem_chunk, msg);
327     }
328   g_slist_free (statusbar->messages);
329   statusbar->messages = NULL;
330
331   for (list = statusbar->keys; list; list = list->next)
332     g_free (list->data);
333   g_slist_free (statusbar->keys);
334   statusbar->keys = NULL;
335
336   GTK_OBJECT_CLASS (parent_class)->destroy (object);
337 }