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
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.
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.
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.
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/.
31 #include "gtkmarshalers.h"
32 #include "gtkstatusbar.h"
33 #include "gtkwindow.h"
34 #include "gtkprivate.h"
36 #include "gtkbuildable.h"
39 * SECTION:gtkstatusbar
40 * @title: GtkStatusbar
41 * @short_description: Report messages of minor importance to the user
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).
49 * Status bars in GTK+ maintain a stack of messages. The message at
50 * the top of the each bar's stack is the one that will currently be displayed.
52 * Any messages added to a statusbar's stack must specify a
53 * <emphasis>context id</emphasis> that is used to uniquely identify
54 * the source of a message. This context id can be generated by
55 * gtk_statusbar_get_context_id(), given a message and the statusbar that
56 * it will be added to. Note that messages are stored in a stack, and when
57 * choosing which message to display, the stack structure is adhered to,
58 * regardless of the context identifier of a message.
60 * One could say that a statusbar maintains one stack of messages for
61 * display purposes, but allows multiple message producers to maintain
62 * sub-stacks of the messages they produced (via context ids).
64 * Status bars are created using gtk_statusbar_new().
66 * Messages are added to the bar's stack with gtk_statusbar_push().
68 * The message at the top of the stack can be removed using
69 * gtk_statusbar_pop(). A message can be removed from anywhere in the
70 * stack if its message id was recorded at the time it was added. This
71 * is done using gtk_statusbar_remove().
73 typedef struct _GtkStatusbarMsg GtkStatusbarMsg;
75 struct _GtkStatusbarPrivate
88 struct _GtkStatusbarMsg
102 static void gtk_statusbar_buildable_interface_init (GtkBuildableIface *iface);
103 static GObject *gtk_statusbar_buildable_get_internal_child (GtkBuildable *buildable,
105 const gchar *childname);
106 static void gtk_statusbar_update (GtkStatusbar *statusbar,
109 static void gtk_statusbar_realize (GtkWidget *widget);
110 static void gtk_statusbar_destroy (GtkWidget *widget);
111 static void gtk_statusbar_size_allocate (GtkWidget *widget,
112 GtkAllocation *allocation);
113 static void gtk_statusbar_hierarchy_changed (GtkWidget *widget,
114 GtkWidget *previous_toplevel);
117 static guint statusbar_signals[SIGNAL_LAST] = { 0 };
119 G_DEFINE_TYPE_WITH_CODE (GtkStatusbar, gtk_statusbar, GTK_TYPE_HBOX,
120 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
121 gtk_statusbar_buildable_interface_init));
124 gtk_statusbar_class_init (GtkStatusbarClass *class)
126 GObjectClass *gobject_class;
127 GtkWidgetClass *widget_class;
129 gobject_class = (GObjectClass *) class;
130 widget_class = (GtkWidgetClass *) class;
132 widget_class->realize = gtk_statusbar_realize;
133 widget_class->destroy = gtk_statusbar_destroy;
134 widget_class->size_allocate = gtk_statusbar_size_allocate;
135 widget_class->hierarchy_changed = gtk_statusbar_hierarchy_changed;
137 class->text_pushed = gtk_statusbar_update;
138 class->text_popped = gtk_statusbar_update;
141 * GtkStatusbar::text-pushed:
142 * @statusbar: the object which received the signal.
143 * @context_id: the context id of the relevant message/statusbar.
144 * @text: the message that was pushed.
146 * Is emitted whenever a new message gets pushed onto a statusbar's stack.
148 statusbar_signals[SIGNAL_TEXT_PUSHED] =
149 g_signal_new (I_("text-pushed"),
150 G_OBJECT_CLASS_TYPE (class),
152 G_STRUCT_OFFSET (GtkStatusbarClass, text_pushed),
154 _gtk_marshal_VOID__UINT_STRING,
160 * GtkStatusbar::text-popped:
161 * @statusbar: the object which received the signal.
162 * @context_id: the context id of the relevant message/statusbar.
163 * @text: the message that was just popped.
165 * Is emitted whenever a new message is popped off a statusbar's stack.
167 statusbar_signals[SIGNAL_TEXT_POPPED] =
168 g_signal_new (I_("text-popped"),
169 G_OBJECT_CLASS_TYPE (class),
171 G_STRUCT_OFFSET (GtkStatusbarClass, text_popped),
173 _gtk_marshal_VOID__UINT_STRING,
178 gtk_widget_class_install_style_property (widget_class,
179 g_param_spec_enum ("shadow-type",
181 P_("Style of bevel around the statusbar text"),
182 GTK_TYPE_SHADOW_TYPE,
184 GTK_PARAM_READABLE));
186 g_type_class_add_private (class, sizeof (GtkStatusbarPrivate));
190 gtk_statusbar_init (GtkStatusbar *statusbar)
192 GtkStatusbarPrivate *priv;
193 GtkBox *box = GTK_BOX (statusbar);
194 GtkWidget *message_area;
195 GtkShadowType shadow_type;
197 statusbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (statusbar,
199 GtkStatusbarPrivate);
200 priv = statusbar->priv;
202 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), TRUE);
204 gtk_box_set_spacing (box, 2);
205 gtk_box_set_homogeneous (box, FALSE);
207 gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);
209 priv->frame = gtk_frame_new (NULL);
210 gtk_frame_set_shadow_type (GTK_FRAME (priv->frame), shadow_type);
211 gtk_box_pack_start (box, priv->frame, TRUE, TRUE, 0);
212 gtk_widget_show (priv->frame);
214 message_area = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
215 gtk_container_add (GTK_CONTAINER (priv->frame), message_area);
216 gtk_widget_show (message_area);
218 priv->label = gtk_label_new ("");
219 gtk_label_set_single_line_mode (GTK_LABEL (priv->label), TRUE);
220 gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
221 gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
222 gtk_container_add (GTK_CONTAINER (message_area), priv->label);
223 gtk_widget_show (priv->label);
225 priv->seq_context_id = 1;
226 priv->seq_message_id = 1;
227 priv->messages = NULL;
231 static GtkBuildableIface *parent_buildable_iface;
234 gtk_statusbar_buildable_interface_init (GtkBuildableIface *iface)
236 parent_buildable_iface = g_type_interface_peek_parent (iface);
237 iface->get_internal_child = gtk_statusbar_buildable_get_internal_child;
241 gtk_statusbar_buildable_get_internal_child (GtkBuildable *buildable,
243 const gchar *childname)
245 GtkStatusbar *statusbar = GTK_STATUSBAR (buildable);
246 GtkStatusbarPrivate *priv = statusbar->priv;
248 if (strcmp (childname, "message_area") == 0)
249 return G_OBJECT (gtk_bin_get_child (GTK_BIN (priv->frame)));
251 return parent_buildable_iface->get_internal_child (buildable,
259 * Creates a new #GtkStatusbar ready for messages.
261 * Returns: the new #GtkStatusbar
264 gtk_statusbar_new (void)
266 return g_object_new (GTK_TYPE_STATUSBAR, NULL);
270 gtk_statusbar_update (GtkStatusbar *statusbar,
274 GtkStatusbarPrivate *priv;
276 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
278 priv = statusbar->priv;
283 gtk_label_set_text (GTK_LABEL (priv->label), text);
287 * gtk_statusbar_get_context_id:
288 * @statusbar: a #GtkStatusbar
289 * @context_description: textual description of what context
290 * the new message is being used in
292 * Returns a new context identifier, given a description
293 * of the actual context. Note that the description is
294 * <emphasis>not</emphasis> shown in the UI.
296 * Returns: an integer id
299 gtk_statusbar_get_context_id (GtkStatusbar *statusbar,
300 const gchar *context_description)
302 GtkStatusbarPrivate *priv;
306 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
307 g_return_val_if_fail (context_description != NULL, 0);
309 priv = statusbar->priv;
311 /* we need to preserve namespaces on object datas */
312 string = g_strconcat ("gtk-status-bar-context:", context_description, NULL);
314 id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (statusbar), string));
317 id = priv->seq_context_id++;
318 g_object_set_data_full (G_OBJECT (statusbar), string, GUINT_TO_POINTER (id), NULL);
319 priv->keys = g_slist_prepend (priv->keys, string);
328 * gtk_statusbar_push:
329 * @statusbar: a #GtkStatusbar
330 * @context_id: the message's context id, as returned by
331 * gtk_statusbar_get_context_id()
332 * @text: the message to add to the statusbar
334 * Pushes a new message onto a statusbar's stack.
336 * Returns: a message id that can be used with
337 * gtk_statusbar_remove().
340 gtk_statusbar_push (GtkStatusbar *statusbar,
344 GtkStatusbarPrivate *priv;
345 GtkStatusbarMsg *msg;
347 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
348 g_return_val_if_fail (text != NULL, 0);
350 priv = statusbar->priv;
352 msg = g_slice_new (GtkStatusbarMsg);
353 msg->text = g_strdup (text);
354 msg->context_id = context_id;
355 msg->message_id = priv->seq_message_id++;
357 priv->messages = g_slist_prepend (priv->messages, msg);
359 g_signal_emit (statusbar,
360 statusbar_signals[SIGNAL_TEXT_PUSHED],
365 return msg->message_id;
370 * @statusbar: a #GtkStatusBar
371 * @context_id: a context identifier
373 * Removes the first message in the #GtkStatusBar's stack
374 * with the given context id.
376 * Note that this may not change the displayed message, if
377 * the message at the top of the stack has a different
381 gtk_statusbar_pop (GtkStatusbar *statusbar,
384 GtkStatusbarPrivate *priv;
385 GtkStatusbarMsg *msg;
387 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
389 priv = statusbar->priv;
395 for (list = priv->messages; list; list = list->next)
399 if (msg->context_id == context_id)
401 priv->messages = g_slist_remove_link (priv->messages,
404 g_slice_free (GtkStatusbarMsg, msg);
405 g_slist_free_1 (list);
411 msg = priv->messages ? priv->messages->data : NULL;
413 g_signal_emit (statusbar,
414 statusbar_signals[SIGNAL_TEXT_POPPED],
416 (guint) (msg ? msg->context_id : 0),
417 msg ? msg->text : NULL);
421 * gtk_statusbar_remove:
422 * @statusbar: a #GtkStatusBar
423 * @context_id: a context identifier
424 * @message_id: a message identifier, as returned by gtk_statusbar_push()
426 * Forces the removal of a message from a statusbar's stack.
427 * The exact @context_id and @message_id must be specified.
430 gtk_statusbar_remove (GtkStatusbar *statusbar,
434 GtkStatusbarPrivate *priv;
435 GtkStatusbarMsg *msg;
437 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
438 g_return_if_fail (message_id > 0);
440 priv = statusbar->priv;
442 msg = priv->messages ? priv->messages->data : NULL;
447 /* care about signal emission if the topmost item is removed */
448 if (msg->context_id == context_id &&
449 msg->message_id == message_id)
451 gtk_statusbar_pop (statusbar, context_id);
455 for (list = priv->messages; list; list = list->next)
459 if (msg->context_id == context_id &&
460 msg->message_id == message_id)
462 priv->messages = g_slist_remove_link (priv->messages, list);
464 g_slice_free (GtkStatusbarMsg, msg);
465 g_slist_free_1 (list);
474 * gtk_statusbar_remove_all:
475 * @statusbar: a #GtkStatusBar
476 * @context_id: a context identifier
478 * Forces the removal of all messages from a statusbar's
479 * stack with the exact @context_id.
484 gtk_statusbar_remove_all (GtkStatusbar *statusbar,
487 GtkStatusbarPrivate *priv;
488 GtkStatusbarMsg *msg;
491 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
493 priv = statusbar->priv;
495 if (priv->messages == NULL)
498 msg = priv->messages->data;
500 /* care about signal emission if the topmost item is removed */
501 if (msg->context_id == context_id)
503 gtk_statusbar_pop (statusbar, context_id);
506 list = priv->messages;
510 prev = priv->messages;
518 if (msg->context_id == context_id)
521 priv->messages = list->next;
523 prev->next = list->next;
526 g_slice_free (GtkStatusbarMsg, msg);
527 g_slist_free_1 (list);
530 prev = priv->messages;
543 * gtk_statusbar_get_message_area:
544 * @statusbar: a #GtkStatusBar
546 * Retrieves the box containing the label widget.
548 * Returns: (transfer none): a #GtkBox
553 gtk_statusbar_get_message_area (GtkStatusbar *statusbar)
555 GtkStatusbarPrivate *priv;
557 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), NULL);
559 priv = statusbar->priv;
561 return gtk_bin_get_child (GTK_BIN (priv->frame));
565 gtk_statusbar_destroy (GtkWidget *widget)
567 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
568 GtkStatusbarPrivate *priv = statusbar->priv;
571 for (list = priv->messages; list; list = list->next)
573 GtkStatusbarMsg *msg;
577 g_slice_free (GtkStatusbarMsg, msg);
579 g_slist_free (priv->messages);
580 priv->messages = NULL;
582 for (list = priv->keys; list; list = list->next)
584 g_slist_free (priv->keys);
587 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->destroy (widget);
590 /* look for extra children between the frame containing
591 * the label and where we want to draw the resize grip
594 has_extra_children (GtkStatusbar *statusbar)
596 GtkStatusbarPrivate *priv = statusbar->priv;
597 GtkPackType child_pack_type, frame_pack_type;
598 GtkWidget *child, *frame;
600 gboolean retval = FALSE;
603 children = _gtk_box_get_children (GTK_BOX (statusbar));
604 for (l = children; l; l = l->next)
608 if (frame == priv->frame)
612 gtk_box_query_child_packing (GTK_BOX (statusbar), frame,
613 NULL, NULL, NULL, &frame_pack_type);
615 for (l = l->next; l; l = l->next)
619 if (!gtk_widget_get_visible (child))
622 gtk_box_query_child_packing (GTK_BOX (statusbar), child,
623 NULL, NULL, NULL, &child_pack_type);
625 if (frame_pack_type == GTK_PACK_START || child_pack_type == GTK_PACK_END)
632 g_list_free (children);
638 gtk_statusbar_size_allocate (GtkWidget *widget,
639 GtkAllocation *allocation)
641 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
642 GtkStatusbarPrivate *priv = statusbar->priv;
643 gboolean extra_children = FALSE;
644 gboolean has_resize_grip = FALSE;
648 GdkRectangle translated_rect;
650 window = gtk_widget_get_toplevel (widget);
652 if (GTK_IS_WINDOW (window) &&
653 gtk_window_resize_grip_is_visible (GTK_WINDOW (window)))
655 gtk_window_get_resize_grip_area (GTK_WINDOW (window), &rect);
656 if (gtk_widget_translate_coordinates (gtk_widget_get_parent (widget),
663 translated_rect.x = x;
664 translated_rect.y = y;
665 translated_rect.width = allocation->width;
666 translated_rect.height = allocation->height;
668 if (gdk_rectangle_intersect (&rect, &translated_rect, NULL))
670 has_resize_grip = TRUE;
671 extra_children = has_extra_children (statusbar);
673 /* If there are extra children, we don't want them to occupy
674 * the space where we draw the resize grip, so we temporarily
675 * shrink the allocation.
676 * If there are no extra children, we want the frame to get
677 * the full allocation, and we fix up the allocation of the
678 * label afterwards to make room for the grip.
682 allocation->width -= rect.width;
683 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
684 allocation->x += rect.width;
690 /* chain up normally */
691 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_allocate (widget, allocation);
697 allocation->width += rect.width;
698 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
699 allocation->x -= rect.width;
701 gtk_widget_set_allocation (widget, allocation);
705 GtkAllocation child_allocation, frame_allocation;
708 /* Use the frame's child instead of statusbar->label directly, in case
709 * the label has been replaced by a container as the frame's child
710 * (and the label reparented into that container).
712 child = gtk_bin_get_child (GTK_BIN (priv->frame));
714 gtk_widget_get_allocation (child, &child_allocation);
715 gtk_widget_get_allocation (priv->frame, &frame_allocation);
716 if (child_allocation.width + rect.width > frame_allocation.width)
718 /* shrink the label to make room for the grip */
719 *allocation = child_allocation;
720 allocation->width = MAX (1, allocation->width - rect.width);
721 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
722 allocation->x += child_allocation.width - allocation->width;
724 gtk_widget_size_allocate (child, allocation);
731 resize_grip_visible_changed (GObject *object,
735 GtkStatusbar *statusbar = GTK_STATUSBAR (user_data);
736 GtkStatusbarPrivate *priv = statusbar->priv;
738 gtk_widget_queue_resize (priv->label);
739 gtk_widget_queue_resize (priv->frame);
740 gtk_widget_queue_resize (GTK_WIDGET (statusbar));
744 gtk_statusbar_hierarchy_changed (GtkWidget *widget,
745 GtkWidget *previous_toplevel)
749 if (previous_toplevel)
750 g_signal_handlers_disconnect_by_func (previous_toplevel,
751 G_CALLBACK (resize_grip_visible_changed),
753 window = gtk_widget_get_toplevel (widget);
754 if (GTK_IS_WINDOW (window))
755 g_signal_connect (window, "notify::resize-grip-visible",
756 G_CALLBACK (resize_grip_visible_changed), widget);
758 resize_grip_visible_changed (NULL, NULL, widget);
762 gtk_statusbar_realize (GtkWidget *widget)
764 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->realize (widget);
766 resize_grip_visible_changed (NULL, NULL, widget);