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