]> Pileus Git - ~andy/gtk/blob - gtk/gtkstatusbar.c
gtk/gtkstatusbar.c: use accessor functions to access GtkWidget
[~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 "config.h"
29 #include "gtkframe.h"
30 #include "gtklabel.h"
31 #include "gtkmarshalers.h"
32 #include "gtkstatusbar.h"
33 #include "gtkwindow.h"
34 #include "gtkprivate.h"
35 #include "gtkintl.h"
36 #include "gtkbuildable.h"
37
38 /**
39  * SECTION:gtkstatusbar
40  * @title: GtkStatusbar
41  * @short_description: Report messages of minor importance to the user
42  *
43  * A #GtkStatusbar is usually placed along the bottom of an application's
44  * main #GtkWindow. It may provide a regular commentary of the application's
45  * status (as is usually the case in a web browser, for example), or may be
46  * used to simply output a message when the status changes, (when an upload
47  * is complete in an FTP client, for example).
48  * It may also have a resize grip (a triangular area in the lower right
49  * corner) which can be clicked on to resize the window containing the
50  * statusbar.
51  *
52  * Status bars in GTK+ maintain a stack of messages. The message at
53  * the top of the each bar's stack is the one that will currently be displayed.
54  *
55  * Any messages added to a statusbar's stack must specify a
56  * <emphasis>context id</emphasis> that is used to uniquely identify
57  * the source of a message. This context id can be generated by
58  * gtk_statusbar_get_context_id(), given a message and the statusbar that
59  * it will be added to. Note that messages are stored in a stack, and when
60  * choosing which message to display, the stack structure is adhered to,
61  * regardless of the context identifier of a message.
62  *
63  * One could say that a statusbar maintains one stack of messages for
64  * display purposes, but allows multiple message producers to maintain
65  * sub-stacks of the messages they produced (via context ids).
66  *
67  * Status bars are created using gtk_statusbar_new().
68  *
69  * Messages are added to the bar's stack with gtk_statusbar_push().
70  *
71  * The message at the top of the stack can be removed using
72  * gtk_statusbar_pop(). A message can be removed from anywhere in the
73  * stack if its message id was recorded at the time it was added. This
74  * is done using gtk_statusbar_remove().
75  */
76 typedef struct _GtkStatusbarMsg GtkStatusbarMsg;
77
78 struct _GtkStatusbarPriv
79 {
80   GtkWidget     *frame;
81   GtkWidget     *label;
82
83   GdkWindow     *grip_window;
84
85   GSList        *messages;
86   GSList        *keys;
87
88   guint          seq_context_id;
89   guint          seq_message_id;
90
91   guint          has_resize_grip : 1;
92 };
93
94
95 struct _GtkStatusbarMsg
96 {
97   gchar *text;
98   guint context_id;
99   guint message_id;
100 };
101
102 enum
103 {
104   SIGNAL_TEXT_PUSHED,
105   SIGNAL_TEXT_POPPED,
106   SIGNAL_LAST
107 };
108
109 enum
110 {
111   PROP_0,
112   PROP_HAS_RESIZE_GRIP
113 };
114
115 static void     gtk_statusbar_buildable_interface_init    (GtkBuildableIface *iface);
116 static GObject *gtk_statusbar_buildable_get_internal_child (GtkBuildable *buildable,
117                                                             GtkBuilder   *builder,
118                                                             const gchar  *childname);
119 static void     gtk_statusbar_destroy           (GtkObject         *object);
120 static void     gtk_statusbar_update            (GtkStatusbar      *statusbar,
121                                                  guint              context_id,
122                                                  const gchar       *text);
123 static void     gtk_statusbar_size_allocate     (GtkWidget         *widget,
124                                                  GtkAllocation     *allocation);
125 static void     gtk_statusbar_realize           (GtkWidget         *widget);
126 static void     gtk_statusbar_unrealize         (GtkWidget         *widget);
127 static void     gtk_statusbar_map               (GtkWidget         *widget);
128 static void     gtk_statusbar_unmap             (GtkWidget         *widget);
129 static gboolean gtk_statusbar_button_press      (GtkWidget         *widget,
130                                                  GdkEventButton    *event);
131 static gboolean gtk_statusbar_expose_event      (GtkWidget         *widget,
132                                                  GdkEventExpose    *event);
133 static void     gtk_statusbar_size_request      (GtkWidget         *widget,
134                                                  GtkRequisition    *requisition);
135 static void     gtk_statusbar_size_allocate     (GtkWidget         *widget,
136                                                  GtkAllocation     *allocation);
137 static void     gtk_statusbar_direction_changed (GtkWidget         *widget,
138                                                  GtkTextDirection   prev_dir);
139 static void     gtk_statusbar_state_changed     (GtkWidget        *widget,
140                                                  GtkStateType      previous_state);
141 static void     gtk_statusbar_create_window     (GtkStatusbar      *statusbar);
142 static void     gtk_statusbar_destroy_window    (GtkStatusbar      *statusbar);
143 static void     gtk_statusbar_get_property      (GObject           *object,
144                                                  guint              prop_id,
145                                                  GValue            *value,
146                                                  GParamSpec        *pspec);
147 static void     gtk_statusbar_set_property      (GObject           *object,
148                                                  guint              prop_id,
149                                                  const GValue      *value,
150                                                  GParamSpec        *pspec);
151 static void     label_selectable_changed        (GtkWidget         *label,
152                                                  GParamSpec        *pspec,
153                                                  gpointer           data);
154
155
156 static guint              statusbar_signals[SIGNAL_LAST] = { 0 };
157
158 G_DEFINE_TYPE_WITH_CODE (GtkStatusbar, gtk_statusbar, GTK_TYPE_HBOX,
159                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
160                                                 gtk_statusbar_buildable_interface_init));
161
162 static void
163 gtk_statusbar_class_init (GtkStatusbarClass *class)
164 {
165   GObjectClass *gobject_class;
166   GtkObjectClass *object_class;
167   GtkWidgetClass *widget_class;
168
169   gobject_class = (GObjectClass *) class;
170   object_class = (GtkObjectClass *) class;
171   widget_class = (GtkWidgetClass *) class;
172
173   gobject_class->set_property = gtk_statusbar_set_property;
174   gobject_class->get_property = gtk_statusbar_get_property;
175
176   object_class->destroy = gtk_statusbar_destroy;
177
178   widget_class->realize = gtk_statusbar_realize;
179   widget_class->unrealize = gtk_statusbar_unrealize;
180   widget_class->map = gtk_statusbar_map;
181   widget_class->unmap = gtk_statusbar_unmap;
182   widget_class->button_press_event = gtk_statusbar_button_press;
183   widget_class->expose_event = gtk_statusbar_expose_event;
184   widget_class->size_request = gtk_statusbar_size_request;
185   widget_class->size_allocate = gtk_statusbar_size_allocate;
186   widget_class->direction_changed = gtk_statusbar_direction_changed;
187   widget_class->state_changed = gtk_statusbar_state_changed;
188   
189   class->text_pushed = gtk_statusbar_update;
190   class->text_popped = gtk_statusbar_update;
191   
192   /**
193    * GtkStatusbar:has-resize-grip:
194    *
195    * Whether the statusbar has a grip for resizing the toplevel window.
196    *
197    * Since: 2.4
198    */
199   g_object_class_install_property (gobject_class,
200                                    PROP_HAS_RESIZE_GRIP,
201                                    g_param_spec_boolean ("has-resize-grip",
202                                                          P_("Has Resize Grip"),
203                                                          P_("Whether the statusbar has a grip for resizing the toplevel"),
204                                                          TRUE,
205                                                          GTK_PARAM_READWRITE));
206
207   /** 
208    * GtkStatusbar::text-pushed:
209    * @statusbar: the object which received the signal.
210    * @context_id: the context id of the relevant message/statusbar.
211    * @text: the message that was pushed.
212    * 
213    * Is emitted whenever a new message gets pushed onto a statusbar's stack.
214    */
215   statusbar_signals[SIGNAL_TEXT_PUSHED] =
216     g_signal_new (I_("text-pushed"),
217                   G_OBJECT_CLASS_TYPE (class),
218                   G_SIGNAL_RUN_LAST,
219                   G_STRUCT_OFFSET (GtkStatusbarClass, text_pushed),
220                   NULL, NULL,
221                   _gtk_marshal_VOID__UINT_STRING,
222                   G_TYPE_NONE, 2,
223                   G_TYPE_UINT,
224                   G_TYPE_STRING);
225
226   /**
227    * GtkStatusbar::text-popped:
228    * @statusbar: the object which received the signal.
229    * @context_id: the context id of the relevant message/statusbar.
230    * @text: the message that was just popped.
231    *
232    * Is emitted whenever a new message is popped off a statusbar's stack.
233    */
234   statusbar_signals[SIGNAL_TEXT_POPPED] =
235     g_signal_new (I_("text-popped"),
236                   G_OBJECT_CLASS_TYPE (class),
237                   G_SIGNAL_RUN_LAST,
238                   G_STRUCT_OFFSET (GtkStatusbarClass, text_popped),
239                   NULL, NULL,
240                   _gtk_marshal_VOID__UINT_STRING,
241                   G_TYPE_NONE, 2,
242                   G_TYPE_UINT,
243                   G_TYPE_STRING);
244
245   gtk_widget_class_install_style_property (widget_class,
246                                            g_param_spec_enum ("shadow-type",
247                                                               P_("Shadow type"),
248                                                               P_("Style of bevel around the statusbar text"),
249                                                               GTK_TYPE_SHADOW_TYPE,
250                                                               GTK_SHADOW_IN,
251                                                               GTK_PARAM_READABLE));
252
253    g_type_class_add_private (class, sizeof (GtkStatusbarPriv));
254 }
255
256 static void
257 gtk_statusbar_init (GtkStatusbar *statusbar)
258 {
259   GtkStatusbarPriv *priv;
260   GtkBox *box = GTK_BOX (statusbar);
261   GtkWidget *message_area;
262   GtkShadowType shadow_type;
263
264   statusbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (statusbar,
265                                                  GTK_TYPE_STATUSBAR,
266                                                  GtkStatusbarPriv);
267   priv = statusbar->priv;
268
269   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), TRUE);
270
271   gtk_box_set_spacing (box, 2);
272   gtk_box_set_homogeneous (box, FALSE);
273
274   priv->has_resize_grip = TRUE;
275
276   gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);
277
278   priv->frame = gtk_frame_new (NULL);
279   gtk_frame_set_shadow_type (GTK_FRAME (priv->frame), shadow_type);
280   gtk_box_pack_start (box, priv->frame, TRUE, TRUE, 0);
281   gtk_widget_show (priv->frame);
282
283   message_area = gtk_hbox_new (FALSE, 4);
284   gtk_container_add (GTK_CONTAINER (priv->frame), message_area);
285   gtk_widget_show (message_area);
286
287   priv->label = gtk_label_new ("");
288   gtk_label_set_single_line_mode (GTK_LABEL (priv->label), TRUE);
289   gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
290   g_signal_connect (priv->label, "notify::selectable",
291                     G_CALLBACK (label_selectable_changed), statusbar);
292   gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
293   gtk_container_add (GTK_CONTAINER (message_area), priv->label);
294   gtk_widget_show (priv->label);
295
296   priv->seq_context_id = 1;
297   priv->seq_message_id = 1;
298   priv->messages = NULL;
299   priv->keys = NULL;
300 }
301
302 static GtkBuildableIface *parent_buildable_iface;
303
304 static void
305 gtk_statusbar_buildable_interface_init (GtkBuildableIface *iface)
306 {
307   parent_buildable_iface = g_type_interface_peek_parent (iface);
308   iface->get_internal_child = gtk_statusbar_buildable_get_internal_child;
309 }
310
311 static GObject *
312 gtk_statusbar_buildable_get_internal_child (GtkBuildable *buildable,
313                                             GtkBuilder   *builder,
314                                             const gchar  *childname)
315 {
316   GtkStatusbar *statusbar = GTK_STATUSBAR (buildable);
317   GtkStatusbarPriv *priv = statusbar->priv;
318
319     if (strcmp (childname, "message_area") == 0)
320       return G_OBJECT (gtk_bin_get_child (GTK_BIN (priv->frame)));
321
322     return parent_buildable_iface->get_internal_child (buildable,
323                                                        builder,
324                                                        childname);
325 }
326
327 /**
328  * gtk_statusbar_new:
329  *
330  * Creates a new #GtkStatusbar ready for messages.
331  *
332  * Returns: the new #GtkStatusbar
333  */
334 GtkWidget* 
335 gtk_statusbar_new (void)
336 {
337   return g_object_new (GTK_TYPE_STATUSBAR, NULL);
338 }
339
340 static void
341 gtk_statusbar_update (GtkStatusbar *statusbar,
342                       guint         context_id,
343                       const gchar  *text)
344 {
345   GtkStatusbarPriv *priv;
346
347   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
348
349   priv = statusbar->priv;
350
351   if (!text)
352     text = "";
353
354   gtk_label_set_text (GTK_LABEL (priv->label), text);
355 }
356
357 /**
358  * gtk_statusbar_get_context_id:
359  * @statusbar: a #GtkStatusbar
360  * @context_description: textual description of what context 
361  *                       the new message is being used in
362  *
363  * Returns a new context identifier, given a description 
364  * of the actual context. Note that the description is 
365  * <emphasis>not</emphasis> shown in the UI.
366  *
367  * Returns: an integer id
368  */
369 guint
370 gtk_statusbar_get_context_id (GtkStatusbar *statusbar,
371                               const gchar  *context_description)
372 {
373   GtkStatusbarPriv *priv;
374   gchar *string;
375   guint id;
376   
377   g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
378   g_return_val_if_fail (context_description != NULL, 0);
379
380   priv = statusbar->priv;
381
382   /* we need to preserve namespaces on object datas */
383   string = g_strconcat ("gtk-status-bar-context:", context_description, NULL);
384
385   id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (statusbar), string));
386   if (id == 0)
387     {
388       id = priv->seq_context_id++;
389       g_object_set_data_full (G_OBJECT (statusbar), string, GUINT_TO_POINTER (id), NULL);
390       priv->keys = g_slist_prepend (priv->keys, string);
391     }
392   else
393     g_free (string);
394
395   return id;
396 }
397
398 /**
399  * gtk_statusbar_push:
400  * @statusbar: a #GtkStatusbar
401  * @context_id: the message's context id, as returned by
402  *              gtk_statusbar_get_context_id()
403  * @text: the message to add to the statusbar
404  * 
405  * Pushes a new message onto a statusbar's stack.
406  *
407  * Returns: a message id that can be used with 
408  *          gtk_statusbar_remove().
409  */
410 guint
411 gtk_statusbar_push (GtkStatusbar *statusbar,
412                     guint         context_id,
413                     const gchar  *text)
414 {
415   GtkStatusbarPriv *priv;
416   GtkStatusbarMsg *msg;
417
418   g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
419   g_return_val_if_fail (text != NULL, 0);
420
421   priv = statusbar->priv;
422
423   msg = g_slice_new (GtkStatusbarMsg);
424   msg->text = g_strdup (text);
425   msg->context_id = context_id;
426   msg->message_id = priv->seq_message_id++;
427
428   priv->messages = g_slist_prepend (priv->messages, msg);
429
430   g_signal_emit (statusbar,
431                  statusbar_signals[SIGNAL_TEXT_PUSHED],
432                  0,
433                  msg->context_id,
434                  msg->text);
435
436   return msg->message_id;
437 }
438
439 /**
440  * gtk_statusbar_pop:
441  * @statusbar: a #GtkStatusBar
442  * @context_id: a context identifier
443  * 
444  * Removes the first message in the #GtkStatusBar's stack
445  * with the given context id. 
446  *
447  * Note that this may not change the displayed message, if 
448  * the message at the top of the stack has a different 
449  * context id.
450  */
451 void
452 gtk_statusbar_pop (GtkStatusbar *statusbar,
453                    guint         context_id)
454 {
455   GtkStatusbarPriv *priv;
456   GtkStatusbarMsg *msg;
457
458   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
459
460   priv = statusbar->priv;
461
462   if (priv->messages)
463     {
464       GSList *list;
465
466       for (list = priv->messages; list; list = list->next)
467         {
468           msg = list->data;
469
470           if (msg->context_id == context_id)
471             {
472               priv->messages = g_slist_remove_link (priv->messages,
473                                                          list);
474               g_free (msg->text);
475               g_slice_free (GtkStatusbarMsg, msg);
476               g_slist_free_1 (list);
477               break;
478             }
479         }
480     }
481
482   msg = priv->messages ? priv->messages->data : NULL;
483
484   g_signal_emit (statusbar,
485                  statusbar_signals[SIGNAL_TEXT_POPPED],
486                  0,
487                  (guint) (msg ? msg->context_id : 0),
488                  msg ? msg->text : NULL);
489 }
490
491 /**
492  * gtk_statusbar_remove:
493  * @statusbar: a #GtkStatusBar
494  * @context_id: a context identifier
495  * @message_id: a message identifier, as returned by gtk_statusbar_push()
496  *
497  * Forces the removal of a message from a statusbar's stack. 
498  * The exact @context_id and @message_id must be specified.
499  */
500 void
501 gtk_statusbar_remove (GtkStatusbar *statusbar,
502                       guint        context_id,
503                       guint        message_id)
504 {
505   GtkStatusbarPriv *priv;
506   GtkStatusbarMsg *msg;
507
508   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
509   g_return_if_fail (message_id > 0);
510
511   priv = statusbar->priv;
512
513   msg = priv->messages ? priv->messages->data : NULL;
514   if (msg)
515     {
516       GSList *list;
517
518       /* care about signal emission if the topmost item is removed */
519       if (msg->context_id == context_id &&
520           msg->message_id == message_id)
521         {
522           gtk_statusbar_pop (statusbar, context_id);
523           return;
524         }
525       
526       for (list = priv->messages; list; list = list->next)
527         {
528           msg = list->data;
529           
530           if (msg->context_id == context_id &&
531               msg->message_id == message_id)
532             {
533               priv->messages = g_slist_remove_link (priv->messages, list);
534               g_free (msg->text);
535               g_slice_free (GtkStatusbarMsg, msg);
536               g_slist_free_1 (list);
537               
538               break;
539             }
540         }
541     }
542 }
543
544 /**
545  * gtk_statusbar_remove_all:
546  * @statusbar: a #GtkStatusBar
547  * @context_id: a context identifier
548  *
549  * Forces the removal of all messages from a statusbar's
550  * stack with the exact @context_id.
551  *
552  * Since: 2.22
553  */
554 void
555 gtk_statusbar_remove_all (GtkStatusbar *statusbar,
556                           guint         context_id)
557 {
558   GtkStatusbarPriv *priv;
559   GtkStatusbarMsg *msg;
560   GSList *prev, *list;
561
562   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
563
564   priv = statusbar->priv;
565
566   if (priv->messages == NULL)
567     return;
568
569   msg = priv->messages->data;
570
571   /* care about signal emission if the topmost item is removed */
572   if (msg->context_id == context_id)
573     {
574       gtk_statusbar_pop (statusbar, context_id);
575
576       prev = NULL;
577       list = priv->messages;
578     }
579   else
580     {
581       prev = priv->messages;
582       list = prev->next;
583     }
584
585   while (list != NULL)
586     {
587       msg = list->data;
588
589       if (msg->context_id == context_id)
590         {
591           if (prev == NULL)
592             priv->messages = list->next;
593           else
594             prev->next = list->next;
595
596           g_free (msg->text);
597           g_slice_free (GtkStatusbarMsg, msg);
598           g_slist_free_1 (list);
599
600           if (prev == NULL)
601             prev = priv->messages;
602
603           list = prev->next;
604         }
605       else
606         {
607           prev = list;
608           list = prev->next;
609         }
610     }
611 }
612
613 /**
614  * gtk_statusbar_set_has_resize_grip:
615  * @statusbar: a #GtkStatusBar
616  * @setting: %TRUE to have a resize grip
617  *
618  * Sets whether the statusbar has a resize grip. 
619  * %TRUE by default.
620  */
621 void
622 gtk_statusbar_set_has_resize_grip (GtkStatusbar *statusbar,
623                                    gboolean      setting)
624 {
625   GtkStatusbarPriv *priv;
626
627   g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
628
629   priv = statusbar->priv;
630
631   setting = setting != FALSE;
632
633   if (setting != priv->has_resize_grip)
634     {
635       priv->has_resize_grip = setting;
636       gtk_widget_queue_resize (priv->label);
637       gtk_widget_queue_draw (GTK_WIDGET (statusbar));
638
639       if (gtk_widget_get_realized (GTK_WIDGET (statusbar)))
640         {
641           if (priv->has_resize_grip && priv->grip_window == NULL)
642             {
643               gtk_statusbar_create_window (statusbar);
644               if (gtk_widget_get_mapped (GTK_WIDGET (statusbar)))
645                 gdk_window_show (priv->grip_window);
646             }
647           else if (!priv->has_resize_grip && priv->grip_window != NULL)
648             gtk_statusbar_destroy_window (statusbar);
649         }
650       
651       g_object_notify (G_OBJECT (statusbar), "has-resize-grip");
652     }
653 }
654
655 /**
656  * gtk_statusbar_get_has_resize_grip:
657  * @statusbar: a #GtkStatusBar
658  * 
659  * Returns whether the statusbar has a resize grip.
660  *
661  * Returns: %TRUE if the statusbar has a resize grip.
662  */
663 gboolean
664 gtk_statusbar_get_has_resize_grip (GtkStatusbar *statusbar)
665 {
666   g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), FALSE);
667
668   return statusbar->priv->has_resize_grip;
669 }
670
671 /**
672  * gtk_statusbar_get_message_area:
673  * @statusbar: a #GtkStatusBar
674  *
675  * Retrieves the box containing the label widget.
676  *
677  * Returns: a #GtkBox
678  *
679  * Since: 2.20
680  */
681 GtkWidget*
682 gtk_statusbar_get_message_area (GtkStatusbar *statusbar)
683 {
684   GtkStatusbarPriv *priv;
685
686   g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), NULL);
687
688   priv = statusbar->priv;
689
690   return gtk_bin_get_child (GTK_BIN (priv->frame));
691 }
692
693 static void
694 gtk_statusbar_destroy (GtkObject *object)
695 {
696   GtkStatusbar *statusbar = GTK_STATUSBAR (object);
697   GtkStatusbarPriv *priv = statusbar->priv;
698   GSList *list;
699
700   for (list = priv->messages; list; list = list->next)
701     {
702       GtkStatusbarMsg *msg;
703
704       msg = list->data;
705       g_free (msg->text);
706       g_slice_free (GtkStatusbarMsg, msg);
707     }
708   g_slist_free (priv->messages);
709   priv->messages = NULL;
710
711   for (list = priv->keys; list; list = list->next)
712     g_free (list->data);
713   g_slist_free (priv->keys);
714   priv->keys = NULL;
715
716   GTK_OBJECT_CLASS (gtk_statusbar_parent_class)->destroy (object);
717 }
718
719 static void
720 gtk_statusbar_set_property (GObject      *object, 
721                             guint         prop_id, 
722                             const GValue *value, 
723                             GParamSpec   *pspec)
724 {
725   GtkStatusbar *statusbar = GTK_STATUSBAR (object);
726
727   switch (prop_id) 
728     {
729     case PROP_HAS_RESIZE_GRIP:
730       gtk_statusbar_set_has_resize_grip (statusbar, g_value_get_boolean (value));
731       break;
732     default:
733       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
734       break;
735     }
736 }
737
738 static void
739 gtk_statusbar_get_property (GObject    *object, 
740                             guint       prop_id, 
741                             GValue     *value, 
742                             GParamSpec *pspec)
743 {
744   GtkStatusbar *statusbar = GTK_STATUSBAR (object);
745   GtkStatusbarPriv *priv = statusbar->priv;
746
747   switch (prop_id) 
748     {
749     case PROP_HAS_RESIZE_GRIP:
750       g_value_set_boolean (value, priv->has_resize_grip);
751       break;
752     default:
753       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
754       break;
755     }
756 }
757
758 static GdkWindowEdge
759 get_grip_edge (GtkStatusbar *statusbar)
760 {
761   GtkWidget *widget = GTK_WIDGET (statusbar);
762
763   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) 
764     return GDK_WINDOW_EDGE_SOUTH_EAST; 
765   else
766     return GDK_WINDOW_EDGE_SOUTH_WEST; 
767 }
768
769 static void
770 get_grip_rect (GtkStatusbar *statusbar,
771                GdkRectangle *rect)
772 {
773   GtkAllocation allocation;
774   GtkStyle *style;
775   GtkWidget *widget = GTK_WIDGET (statusbar);
776   gint w, h;
777
778   gtk_widget_get_allocation (widget, &allocation);
779   style = gtk_widget_get_style (widget);
780
781   /* These are in effect the max/default size of the grip. */
782   w = 18;
783   h = 18;
784
785   if (w > allocation.width)
786     w = allocation.width;
787
788   if (h > allocation.height - style->ythickness)
789     h = allocation.height - style->ythickness;
790
791   rect->width = w;
792   rect->height = h;
793   rect->y = allocation.y + allocation.height - h;
794
795   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) 
796     rect->x = allocation.x + allocation.width - w;
797   else
798     rect->x = allocation.x + style->xthickness;
799 }
800
801 static void
802 set_grip_cursor (GtkStatusbar *statusbar)
803 {
804   GtkStatusbarPriv *priv = statusbar->priv;
805
806   if (priv->has_resize_grip && priv->grip_window != NULL)
807     {
808       GtkWidget *widget = GTK_WIDGET (statusbar);
809       GdkDisplay *display = gtk_widget_get_display (widget);
810       GdkCursorType cursor_type;
811       GdkCursor *cursor;
812       
813       if (gtk_widget_is_sensitive (widget))
814         {
815           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
816             cursor_type = GDK_BOTTOM_RIGHT_CORNER;
817           else
818             cursor_type = GDK_BOTTOM_LEFT_CORNER;
819
820           cursor = gdk_cursor_new_for_display (display, cursor_type);
821           gdk_window_set_cursor (priv->grip_window, cursor);
822           gdk_cursor_unref (cursor);
823         }
824       else
825         gdk_window_set_cursor (priv->grip_window, NULL);
826     }
827 }
828
829 static void
830 gtk_statusbar_create_window (GtkStatusbar *statusbar)
831 {
832   GtkWidget *widget;
833   GtkStatusbarPriv *priv = statusbar->priv;
834   GdkWindowAttr attributes;
835   gint attributes_mask;
836   GdkRectangle rect;
837
838   widget = GTK_WIDGET (statusbar);
839
840   g_return_if_fail (gtk_widget_get_realized (widget));
841   g_return_if_fail (priv->has_resize_grip);
842
843   get_grip_rect (statusbar, &rect);
844
845   attributes.x = rect.x;
846   attributes.y = rect.y;
847   attributes.width = rect.width;
848   attributes.height = rect.height;
849   attributes.window_type = GDK_WINDOW_CHILD;
850   attributes.wclass = GDK_INPUT_ONLY;
851   attributes.event_mask = gtk_widget_get_events (widget) |
852     GDK_BUTTON_PRESS_MASK;
853
854   attributes_mask = GDK_WA_X | GDK_WA_Y;
855
856   priv->grip_window = gdk_window_new (gtk_widget_get_window (widget),
857                                       &attributes, attributes_mask);
858   gdk_window_set_user_data (priv->grip_window, widget);
859
860   gdk_window_raise (priv->grip_window);
861
862   set_grip_cursor (statusbar);
863 }
864
865 static void
866 gtk_statusbar_direction_changed (GtkWidget        *widget,
867                                  GtkTextDirection  prev_dir)
868 {
869   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
870
871   set_grip_cursor (statusbar);
872 }
873
874 static void
875 gtk_statusbar_state_changed (GtkWidget    *widget,
876                              GtkStateType  previous_state)   
877 {
878   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
879
880   set_grip_cursor (statusbar);
881 }
882
883 static void
884 gtk_statusbar_destroy_window (GtkStatusbar *statusbar)
885 {
886   GtkStatusbarPriv *priv = statusbar->priv;
887
888   gdk_window_set_user_data (priv->grip_window, NULL);
889   gdk_window_destroy (priv->grip_window);
890   priv->grip_window = NULL;
891 }
892
893 static void
894 gtk_statusbar_realize (GtkWidget *widget)
895 {
896   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
897   GtkStatusbarPriv *priv = statusbar->priv;
898
899   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->realize (widget);
900
901   if (priv->has_resize_grip)
902     gtk_statusbar_create_window (statusbar);
903 }
904
905 static void
906 gtk_statusbar_unrealize (GtkWidget *widget)
907 {
908   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
909   GtkStatusbarPriv *priv = statusbar->priv;
910
911   if (priv->grip_window)
912     gtk_statusbar_destroy_window (statusbar);
913
914   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->unrealize (widget);
915 }
916
917 static void
918 gtk_statusbar_map (GtkWidget *widget)
919 {
920   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
921   GtkStatusbarPriv *priv = statusbar->priv;
922
923   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->map (widget);
924
925   if (priv->grip_window)
926     gdk_window_show (priv->grip_window);
927 }
928
929 static void
930 gtk_statusbar_unmap (GtkWidget *widget)
931 {
932   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
933   GtkStatusbarPriv *priv = statusbar->priv;
934
935   if (priv->grip_window)
936     gdk_window_hide (priv->grip_window);
937
938   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->unmap (widget);
939 }
940
941 static gboolean
942 gtk_statusbar_button_press (GtkWidget      *widget,
943                             GdkEventButton *event)
944 {
945   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
946   GtkStatusbarPriv *priv = statusbar->priv;
947   GtkWidget *ancestor;
948   GdkWindowEdge edge;
949
950   if (!priv->has_resize_grip ||
951       event->type != GDK_BUTTON_PRESS ||
952       event->window != priv->grip_window)
953     return FALSE;
954   
955   ancestor = gtk_widget_get_toplevel (widget);
956
957   if (!GTK_IS_WINDOW (ancestor))
958     return FALSE;
959
960   edge = get_grip_edge (statusbar);
961
962   if (event->button == 1)
963     gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
964                                   edge,
965                                   event->button,
966                                   event->x_root, event->y_root,
967                                   event->time);
968   else if (event->button == 2)
969     gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
970                                 event->button,
971                                 event->x_root, event->y_root,
972                                 event->time);
973   else
974     return FALSE;
975   
976   return TRUE;
977 }
978
979 static gboolean
980 gtk_statusbar_expose_event (GtkWidget      *widget,
981                             GdkEventExpose *event)
982 {
983   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
984   GtkStatusbarPriv *priv = statusbar->priv;
985   GtkStyle *style;
986   GdkRectangle rect;
987
988   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->expose_event (widget, event);
989
990   if (priv->has_resize_grip)
991     {
992       GdkWindowEdge edge;
993       
994       edge = get_grip_edge (statusbar);
995
996       get_grip_rect (statusbar, &rect);
997
998       style = gtk_widget_get_style (widget);
999       gtk_paint_resize_grip (style,
1000                              gtk_widget_get_window (widget),
1001                              gtk_widget_get_state (widget),
1002                              &event->area,
1003                              widget,
1004                              "statusbar",
1005                              edge,
1006                              rect.x, rect.y,
1007                              /* don't draw grip over the frame, though you
1008                               * can click on the frame.
1009                               */
1010                              rect.width - style->xthickness,
1011                              rect.height - style->ythickness);
1012     }
1013
1014   return FALSE;
1015 }
1016
1017 static void
1018 gtk_statusbar_size_request (GtkWidget      *widget,
1019                             GtkRequisition *requisition)
1020 {
1021   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
1022   GtkStatusbarPriv *priv = statusbar->priv;
1023   GtkShadowType shadow_type;
1024
1025   gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);  
1026   gtk_frame_set_shadow_type (GTK_FRAME (priv->frame), shadow_type);
1027   
1028   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_request (widget, requisition);
1029 }
1030
1031 /* look for extra children between the frame containing
1032  * the label and where we want to draw the resize grip 
1033  */
1034 static gboolean
1035 has_extra_children (GtkStatusbar *statusbar)
1036 {
1037   GtkStatusbarPriv *priv = statusbar->priv;
1038   GtkPackType child_pack_type, frame_pack_type;
1039   GtkWidget *child, *frame;
1040   GList *l, *children;
1041   gboolean retval = FALSE;
1042
1043   /* If the internal frame has been modified assume we have extra children */
1044   if (gtk_bin_get_child (GTK_BIN (priv->frame)) != priv->label)
1045     return TRUE;
1046
1047   frame = NULL;
1048   children = _gtk_box_get_children (GTK_BOX (statusbar));
1049   for (l = children; l; l = l->next)
1050     {
1051       frame = l->data;
1052
1053       if (frame == priv->frame)
1054         break;
1055     }
1056
1057   gtk_box_query_child_packing (GTK_BOX (statusbar), frame,
1058                                NULL, NULL, NULL, &frame_pack_type);
1059
1060   for (l = l->next; l; l = l->next)
1061     {
1062       child = l->data;
1063
1064       if (!gtk_widget_get_visible (child))
1065         continue;
1066
1067       gtk_box_query_child_packing (GTK_BOX (statusbar), frame,
1068                                    NULL, NULL, NULL, &child_pack_type);
1069
1070       if (frame_pack_type == GTK_PACK_START || child_pack_type == GTK_PACK_END)
1071         {
1072           retval = TRUE;
1073           break;
1074         }
1075     }
1076
1077   g_list_free (children);
1078
1079   return retval;
1080 }
1081
1082 static void
1083 gtk_statusbar_size_allocate  (GtkWidget     *widget,
1084                               GtkAllocation *allocation)
1085 {
1086   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
1087   GtkStatusbarPriv *priv = statusbar->priv;
1088   gboolean extra_children = FALSE;
1089   GdkRectangle rect;
1090
1091   if (priv->has_resize_grip)
1092     {
1093       get_grip_rect (statusbar, &rect);
1094
1095       extra_children = has_extra_children (statusbar);
1096
1097       /* If there are extra children, we don't want them to occupy
1098        * the space where we draw the resize grip, so we temporarily
1099        * shrink the allocation.
1100        * If there are no extra children, we want the frame to get
1101        * the full allocation, and we fix up the allocation of the
1102        * label afterwards to make room for the grip.
1103        */
1104       if (extra_children)
1105         {
1106           allocation->width -= rect.width;
1107           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 
1108             allocation->x += rect.width;
1109         }
1110     }
1111
1112   /* chain up normally */
1113   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_allocate (widget, allocation);
1114
1115   if (priv->has_resize_grip)
1116     {
1117       if (extra_children) 
1118         {
1119           allocation->width += rect.width;
1120           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 
1121             allocation->x -= rect.width;
1122           
1123           gtk_widget_set_allocation (widget, allocation);
1124         }
1125       else
1126         {
1127           GtkAllocation child_allocation, frame_allocation;
1128           GtkWidget *child;
1129
1130           /* Use the frame's child instead of priv->label directly, in case
1131            * the label has been replaced by a container as the frame's child
1132            * (and the label reparented into that container).
1133            */
1134           child = gtk_bin_get_child (GTK_BIN (priv->frame));
1135
1136           gtk_widget_get_allocation (child, &child_allocation);
1137           gtk_widget_get_allocation (priv->frame, &frame_allocation);
1138           if (child_allocation.width + rect.width > frame_allocation.width)
1139             {
1140               /* shrink the label to make room for the grip */
1141               *allocation = child_allocation;
1142               allocation->width = MAX (1, allocation->width - rect.width);
1143               if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1144                 allocation->x += child_allocation.width - allocation->width;
1145
1146               gtk_widget_size_allocate (child, allocation);
1147             }
1148         }
1149
1150       if (priv->grip_window)
1151         {
1152           get_grip_rect (statusbar, &rect);
1153
1154           gdk_window_raise (priv->grip_window);
1155           gdk_window_move_resize (priv->grip_window,
1156                                   rect.x, rect.y,
1157                                   rect.width, rect.height);
1158         }
1159
1160     }
1161 }
1162
1163 static void
1164 label_selectable_changed (GtkWidget  *label,
1165                           GParamSpec *pspec,
1166                           gpointer    data)
1167 {
1168   GtkStatusbar *statusbar = GTK_STATUSBAR (data);
1169   GtkStatusbarPriv *priv = statusbar->priv;
1170
1171   if (statusbar &&
1172       priv->has_resize_grip && priv->grip_window)
1173     gdk_window_raise (priv->grip_window);
1174 }