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