]> Pileus Git - ~andy/gtk/blob - gtk/gtkstatusbar.c
statusbar: Port to draw vfunc
[~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 _GtkStatusbarPrivate
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_draw              (GtkWidget         *widget,
132                                                  cairo_t           *cr);
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->draw = gtk_statusbar_draw;
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 (GtkStatusbarPrivate));
254 }
255
256 static void
257 gtk_statusbar_init (GtkStatusbar *statusbar)
258 {
259   GtkStatusbarPrivate *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                                                  GtkStatusbarPrivate);
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   GtkStatusbarPrivate *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   GtkStatusbarPrivate *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   GtkStatusbarPrivate *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   GtkStatusbarPrivate *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   GtkStatusbarPrivate *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   GtkStatusbarPrivate *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   GtkStatusbarPrivate *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   GtkStatusbarPrivate *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: (transfer none): a #GtkBox
678  *
679  * Since: 2.20
680  */
681 GtkWidget*
682 gtk_statusbar_get_message_area (GtkStatusbar *statusbar)
683 {
684   GtkStatusbarPrivate *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   GtkStatusbarPrivate *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   GtkStatusbarPrivate *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                gboolean      include_allocation_offset,
772                GdkRectangle *rect)
773 {
774   GtkAllocation allocation;
775   GtkStyle *style;
776   GtkWidget *widget = GTK_WIDGET (statusbar);
777   gint w, h;
778
779   gtk_widget_get_allocation (widget, &allocation);
780   style = gtk_widget_get_style (widget);
781
782   /* These are in effect the max/default size of the grip. */
783   w = 18;
784   h = 18;
785
786   if (w > allocation.width)
787     w = allocation.width;
788
789   if (h > allocation.height - style->ythickness)
790     h = allocation.height - style->ythickness;
791
792   rect->width = w;
793   rect->height = h;
794   rect->y = allocation.height - h;
795
796   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) 
797     rect->x = allocation.width - w;
798   else
799     rect->x = style->xthickness;
800
801   if (include_allocation_offset)
802     {
803       rect->x += allocation.x;
804       rect->y += allocation.y;
805     }
806 }
807
808 static void
809 set_grip_cursor (GtkStatusbar *statusbar)
810 {
811   GtkStatusbarPrivate *priv = statusbar->priv;
812
813   if (priv->has_resize_grip && priv->grip_window != NULL)
814     {
815       GtkWidget *widget = GTK_WIDGET (statusbar);
816       GdkDisplay *display = gtk_widget_get_display (widget);
817       GdkCursorType cursor_type;
818       GdkCursor *cursor;
819       
820       if (gtk_widget_is_sensitive (widget))
821         {
822           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
823             cursor_type = GDK_BOTTOM_RIGHT_CORNER;
824           else
825             cursor_type = GDK_BOTTOM_LEFT_CORNER;
826
827           cursor = gdk_cursor_new_for_display (display, cursor_type);
828           gdk_window_set_cursor (priv->grip_window, cursor);
829           gdk_cursor_unref (cursor);
830         }
831       else
832         gdk_window_set_cursor (priv->grip_window, NULL);
833     }
834 }
835
836 static void
837 gtk_statusbar_create_window (GtkStatusbar *statusbar)
838 {
839   GtkWidget *widget;
840   GtkStatusbarPrivate *priv = statusbar->priv;
841   GdkWindowAttr attributes;
842   gint attributes_mask;
843   GdkRectangle rect;
844
845   widget = GTK_WIDGET (statusbar);
846
847   g_return_if_fail (gtk_widget_get_realized (widget));
848   g_return_if_fail (priv->has_resize_grip);
849
850   get_grip_rect (statusbar, TRUE, &rect);
851
852   attributes.x = rect.x;
853   attributes.y = rect.y;
854   attributes.width = rect.width;
855   attributes.height = rect.height;
856   attributes.window_type = GDK_WINDOW_CHILD;
857   attributes.wclass = GDK_INPUT_ONLY;
858   attributes.event_mask = gtk_widget_get_events (widget) |
859     GDK_BUTTON_PRESS_MASK;
860
861   attributes_mask = GDK_WA_X | GDK_WA_Y;
862
863   priv->grip_window = gdk_window_new (gtk_widget_get_window (widget),
864                                       &attributes, attributes_mask);
865   gdk_window_set_user_data (priv->grip_window, widget);
866
867   gdk_window_raise (priv->grip_window);
868
869   set_grip_cursor (statusbar);
870 }
871
872 static void
873 gtk_statusbar_direction_changed (GtkWidget        *widget,
874                                  GtkTextDirection  prev_dir)
875 {
876   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
877
878   set_grip_cursor (statusbar);
879 }
880
881 static void
882 gtk_statusbar_state_changed (GtkWidget    *widget,
883                              GtkStateType  previous_state)   
884 {
885   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
886
887   set_grip_cursor (statusbar);
888 }
889
890 static void
891 gtk_statusbar_destroy_window (GtkStatusbar *statusbar)
892 {
893   GtkStatusbarPrivate *priv = statusbar->priv;
894
895   gdk_window_set_user_data (priv->grip_window, NULL);
896   gdk_window_destroy (priv->grip_window);
897   priv->grip_window = NULL;
898 }
899
900 static void
901 gtk_statusbar_realize (GtkWidget *widget)
902 {
903   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
904   GtkStatusbarPrivate *priv = statusbar->priv;
905
906   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->realize (widget);
907
908   if (priv->has_resize_grip)
909     gtk_statusbar_create_window (statusbar);
910 }
911
912 static void
913 gtk_statusbar_unrealize (GtkWidget *widget)
914 {
915   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
916   GtkStatusbarPrivate *priv = statusbar->priv;
917
918   if (priv->grip_window)
919     gtk_statusbar_destroy_window (statusbar);
920
921   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->unrealize (widget);
922 }
923
924 static void
925 gtk_statusbar_map (GtkWidget *widget)
926 {
927   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
928   GtkStatusbarPrivate *priv = statusbar->priv;
929
930   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->map (widget);
931
932   if (priv->grip_window)
933     gdk_window_show (priv->grip_window);
934 }
935
936 static void
937 gtk_statusbar_unmap (GtkWidget *widget)
938 {
939   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
940   GtkStatusbarPrivate *priv = statusbar->priv;
941
942   if (priv->grip_window)
943     gdk_window_hide (priv->grip_window);
944
945   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->unmap (widget);
946 }
947
948 static gboolean
949 gtk_statusbar_button_press (GtkWidget      *widget,
950                             GdkEventButton *event)
951 {
952   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
953   GtkStatusbarPrivate *priv = statusbar->priv;
954   GtkWidget *ancestor;
955   GdkWindowEdge edge;
956
957   if (!priv->has_resize_grip ||
958       event->type != GDK_BUTTON_PRESS ||
959       event->window != priv->grip_window)
960     return FALSE;
961   
962   ancestor = gtk_widget_get_toplevel (widget);
963
964   if (!GTK_IS_WINDOW (ancestor))
965     return FALSE;
966
967   edge = get_grip_edge (statusbar);
968
969   if (event->button == 1)
970     gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
971                                   edge,
972                                   event->button,
973                                   event->x_root, event->y_root,
974                                   event->time);
975   else if (event->button == 2)
976     gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
977                                 event->button,
978                                 event->x_root, event->y_root,
979                                 event->time);
980   else
981     return FALSE;
982   
983   return TRUE;
984 }
985
986 static gboolean
987 gtk_statusbar_draw (GtkWidget *widget,
988                     cairo_t   *cr)
989 {
990   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
991   GtkStatusbarPrivate *priv = statusbar->priv;
992   GtkStyle *style;
993   GdkRectangle rect;
994
995   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->draw (widget, cr);
996
997   if (priv->has_resize_grip)
998     {
999       GdkWindowEdge edge;
1000       
1001       edge = get_grip_edge (statusbar);
1002
1003       get_grip_rect (statusbar, FALSE, &rect);
1004
1005       style = gtk_widget_get_style (widget);
1006       gtk_cairo_paint_resize_grip (style,
1007                              cr,
1008                              gtk_widget_get_state (widget),
1009                              widget,
1010                              "statusbar",
1011                              edge,
1012                              rect.x, rect.y,
1013                              /* don't draw grip over the frame, though you
1014                               * can click on the frame.
1015                               */
1016                              rect.width - style->xthickness,
1017                              rect.height - style->ythickness);
1018     }
1019
1020   return FALSE;
1021 }
1022
1023 static void
1024 gtk_statusbar_size_request (GtkWidget      *widget,
1025                             GtkRequisition *requisition)
1026 {
1027   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
1028   GtkStatusbarPrivate *priv = statusbar->priv;
1029   GtkShadowType shadow_type;
1030
1031   gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);  
1032   gtk_frame_set_shadow_type (GTK_FRAME (priv->frame), shadow_type);
1033   
1034   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_request (widget, requisition);
1035 }
1036
1037 /* look for extra children between the frame containing
1038  * the label and where we want to draw the resize grip 
1039  */
1040 static gboolean
1041 has_extra_children (GtkStatusbar *statusbar)
1042 {
1043   GtkStatusbarPrivate *priv = statusbar->priv;
1044   GtkPackType child_pack_type, frame_pack_type;
1045   GtkWidget *child, *frame;
1046   GList *l, *children;
1047   gboolean retval = FALSE;
1048
1049   /* If the internal frame has been modified assume we have extra children */
1050   if (gtk_bin_get_child (GTK_BIN (priv->frame)) != priv->label)
1051     return TRUE;
1052
1053   frame = NULL;
1054   children = _gtk_box_get_children (GTK_BOX (statusbar));
1055   for (l = children; l; l = l->next)
1056     {
1057       frame = l->data;
1058
1059       if (frame == priv->frame)
1060         break;
1061     }
1062
1063   gtk_box_query_child_packing (GTK_BOX (statusbar), frame,
1064                                NULL, NULL, NULL, &frame_pack_type);
1065
1066   for (l = l->next; l; l = l->next)
1067     {
1068       child = l->data;
1069
1070       if (!gtk_widget_get_visible (child))
1071         continue;
1072
1073       gtk_box_query_child_packing (GTK_BOX (statusbar), frame,
1074                                    NULL, NULL, NULL, &child_pack_type);
1075
1076       if (frame_pack_type == GTK_PACK_START || child_pack_type == GTK_PACK_END)
1077         {
1078           retval = TRUE;
1079           break;
1080         }
1081     }
1082
1083   g_list_free (children);
1084
1085   return retval;
1086 }
1087
1088 static void
1089 gtk_statusbar_size_allocate  (GtkWidget     *widget,
1090                               GtkAllocation *allocation)
1091 {
1092   GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
1093   GtkStatusbarPrivate *priv = statusbar->priv;
1094   gboolean extra_children = FALSE;
1095   GdkRectangle rect;
1096
1097   if (priv->has_resize_grip)
1098     {
1099       get_grip_rect (statusbar, TRUE, &rect);
1100
1101       extra_children = has_extra_children (statusbar);
1102
1103       /* If there are extra children, we don't want them to occupy
1104        * the space where we draw the resize grip, so we temporarily
1105        * shrink the allocation.
1106        * If there are no extra children, we want the frame to get
1107        * the full allocation, and we fix up the allocation of the
1108        * label afterwards to make room for the grip.
1109        */
1110       if (extra_children)
1111         {
1112           allocation->width -= rect.width;
1113           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 
1114             allocation->x += rect.width;
1115         }
1116     }
1117
1118   /* chain up normally */
1119   GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_allocate (widget, allocation);
1120
1121   if (priv->has_resize_grip)
1122     {
1123       if (extra_children) 
1124         {
1125           allocation->width += rect.width;
1126           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 
1127             allocation->x -= rect.width;
1128           
1129           gtk_widget_set_allocation (widget, allocation);
1130         }
1131       else
1132         {
1133           GtkAllocation child_allocation, frame_allocation;
1134           GtkWidget *child;
1135
1136           /* Use the frame's child instead of priv->label directly, in case
1137            * the label has been replaced by a container as the frame's child
1138            * (and the label reparented into that container).
1139            */
1140           child = gtk_bin_get_child (GTK_BIN (priv->frame));
1141
1142           gtk_widget_get_allocation (child, &child_allocation);
1143           gtk_widget_get_allocation (priv->frame, &frame_allocation);
1144           if (child_allocation.width + rect.width > frame_allocation.width)
1145             {
1146               /* shrink the label to make room for the grip */
1147               *allocation = child_allocation;
1148               allocation->width = MAX (1, allocation->width - rect.width);
1149               if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1150                 allocation->x += child_allocation.width - allocation->width;
1151
1152               gtk_widget_size_allocate (child, allocation);
1153             }
1154         }
1155
1156       if (priv->grip_window)
1157         {
1158           get_grip_rect (statusbar, TRUE, &rect);
1159
1160           gdk_window_raise (priv->grip_window);
1161           gdk_window_move_resize (priv->grip_window,
1162                                   rect.x, rect.y,
1163                                   rect.width, rect.height);
1164         }
1165
1166     }
1167 }
1168
1169 static void
1170 label_selectable_changed (GtkWidget  *label,
1171                           GParamSpec *pspec,
1172                           gpointer    data)
1173 {
1174   GtkStatusbar *statusbar = GTK_STATUSBAR (data);
1175   GtkStatusbarPrivate *priv = statusbar->priv;
1176
1177   if (statusbar &&
1178       priv->has_resize_grip && priv->grip_window)
1179     gdk_window_raise (priv->grip_window);
1180 }