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_destroy (GtkWidget *widget);
110 static void gtk_statusbar_size_allocate (GtkWidget *widget,
111 GtkAllocation *allocation);
112 static void gtk_statusbar_hierarchy_changed (GtkWidget *widget,
113 GtkWidget *previous_toplevel);
116 static guint statusbar_signals[SIGNAL_LAST] = { 0 };
118 G_DEFINE_TYPE_WITH_CODE (GtkStatusbar, gtk_statusbar, GTK_TYPE_HBOX,
119 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
120 gtk_statusbar_buildable_interface_init));
123 gtk_statusbar_class_init (GtkStatusbarClass *class)
125 GObjectClass *gobject_class;
126 GtkWidgetClass *widget_class;
128 gobject_class = (GObjectClass *) class;
129 widget_class = (GtkWidgetClass *) class;
131 widget_class->destroy = gtk_statusbar_destroy;
132 widget_class->size_allocate = gtk_statusbar_size_allocate;
133 widget_class->hierarchy_changed = gtk_statusbar_hierarchy_changed;
135 class->text_pushed = gtk_statusbar_update;
136 class->text_popped = gtk_statusbar_update;
139 * GtkStatusbar::text-pushed:
140 * @statusbar: the object which received the signal.
141 * @context_id: the context id of the relevant message/statusbar.
142 * @text: the message that was pushed.
144 * Is emitted whenever a new message gets pushed onto a statusbar's stack.
146 statusbar_signals[SIGNAL_TEXT_PUSHED] =
147 g_signal_new (I_("text-pushed"),
148 G_OBJECT_CLASS_TYPE (class),
150 G_STRUCT_OFFSET (GtkStatusbarClass, text_pushed),
152 _gtk_marshal_VOID__UINT_STRING,
158 * GtkStatusbar::text-popped:
159 * @statusbar: the object which received the signal.
160 * @context_id: the context id of the relevant message/statusbar.
161 * @text: the message that was just popped.
163 * Is emitted whenever a new message is popped off a statusbar's stack.
165 statusbar_signals[SIGNAL_TEXT_POPPED] =
166 g_signal_new (I_("text-popped"),
167 G_OBJECT_CLASS_TYPE (class),
169 G_STRUCT_OFFSET (GtkStatusbarClass, text_popped),
171 _gtk_marshal_VOID__UINT_STRING,
176 gtk_widget_class_install_style_property (widget_class,
177 g_param_spec_enum ("shadow-type",
179 P_("Style of bevel around the statusbar text"),
180 GTK_TYPE_SHADOW_TYPE,
182 GTK_PARAM_READABLE));
184 g_type_class_add_private (class, sizeof (GtkStatusbarPrivate));
188 gtk_statusbar_init (GtkStatusbar *statusbar)
190 GtkStatusbarPrivate *priv;
191 GtkBox *box = GTK_BOX (statusbar);
192 GtkWidget *message_area;
193 GtkShadowType shadow_type;
195 statusbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (statusbar,
197 GtkStatusbarPrivate);
198 priv = statusbar->priv;
200 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), TRUE);
202 gtk_box_set_spacing (box, 2);
203 gtk_box_set_homogeneous (box, FALSE);
205 gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);
207 priv->frame = gtk_frame_new (NULL);
208 gtk_frame_set_shadow_type (GTK_FRAME (priv->frame), shadow_type);
209 gtk_box_pack_start (box, priv->frame, TRUE, TRUE, 0);
210 gtk_widget_show (priv->frame);
212 message_area = gtk_hbox_new (FALSE, 4);
213 gtk_container_add (GTK_CONTAINER (priv->frame), message_area);
214 gtk_widget_show (message_area);
216 priv->label = gtk_label_new ("");
217 gtk_label_set_single_line_mode (GTK_LABEL (priv->label), TRUE);
218 gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
219 gtk_label_set_ellipsize (GTK_LABEL (priv->label), PANGO_ELLIPSIZE_END);
220 gtk_container_add (GTK_CONTAINER (message_area), priv->label);
221 gtk_widget_show (priv->label);
223 priv->seq_context_id = 1;
224 priv->seq_message_id = 1;
225 priv->messages = NULL;
229 static GtkBuildableIface *parent_buildable_iface;
232 gtk_statusbar_buildable_interface_init (GtkBuildableIface *iface)
234 parent_buildable_iface = g_type_interface_peek_parent (iface);
235 iface->get_internal_child = gtk_statusbar_buildable_get_internal_child;
239 gtk_statusbar_buildable_get_internal_child (GtkBuildable *buildable,
241 const gchar *childname)
243 GtkStatusbar *statusbar = GTK_STATUSBAR (buildable);
244 GtkStatusbarPrivate *priv = statusbar->priv;
246 if (strcmp (childname, "message_area") == 0)
247 return G_OBJECT (gtk_bin_get_child (GTK_BIN (priv->frame)));
249 return parent_buildable_iface->get_internal_child (buildable,
257 * Creates a new #GtkStatusbar ready for messages.
259 * Returns: the new #GtkStatusbar
262 gtk_statusbar_new (void)
264 return g_object_new (GTK_TYPE_STATUSBAR, NULL);
268 gtk_statusbar_update (GtkStatusbar *statusbar,
272 GtkStatusbarPrivate *priv;
274 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
276 priv = statusbar->priv;
281 gtk_label_set_text (GTK_LABEL (priv->label), text);
285 * gtk_statusbar_get_context_id:
286 * @statusbar: a #GtkStatusbar
287 * @context_description: textual description of what context
288 * the new message is being used in
290 * Returns a new context identifier, given a description
291 * of the actual context. Note that the description is
292 * <emphasis>not</emphasis> shown in the UI.
294 * Returns: an integer id
297 gtk_statusbar_get_context_id (GtkStatusbar *statusbar,
298 const gchar *context_description)
300 GtkStatusbarPrivate *priv;
304 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
305 g_return_val_if_fail (context_description != NULL, 0);
307 priv = statusbar->priv;
309 /* we need to preserve namespaces on object datas */
310 string = g_strconcat ("gtk-status-bar-context:", context_description, NULL);
312 id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (statusbar), string));
315 id = priv->seq_context_id++;
316 g_object_set_data_full (G_OBJECT (statusbar), string, GUINT_TO_POINTER (id), NULL);
317 priv->keys = g_slist_prepend (priv->keys, string);
326 * gtk_statusbar_push:
327 * @statusbar: a #GtkStatusbar
328 * @context_id: the message's context id, as returned by
329 * gtk_statusbar_get_context_id()
330 * @text: the message to add to the statusbar
332 * Pushes a new message onto a statusbar's stack.
334 * Returns: a message id that can be used with
335 * gtk_statusbar_remove().
338 gtk_statusbar_push (GtkStatusbar *statusbar,
342 GtkStatusbarPrivate *priv;
343 GtkStatusbarMsg *msg;
345 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
346 g_return_val_if_fail (text != NULL, 0);
348 priv = statusbar->priv;
350 msg = g_slice_new (GtkStatusbarMsg);
351 msg->text = g_strdup (text);
352 msg->context_id = context_id;
353 msg->message_id = priv->seq_message_id++;
355 priv->messages = g_slist_prepend (priv->messages, msg);
357 g_signal_emit (statusbar,
358 statusbar_signals[SIGNAL_TEXT_PUSHED],
363 return msg->message_id;
368 * @statusbar: a #GtkStatusBar
369 * @context_id: a context identifier
371 * Removes the first message in the #GtkStatusBar's stack
372 * with the given context id.
374 * Note that this may not change the displayed message, if
375 * the message at the top of the stack has a different
379 gtk_statusbar_pop (GtkStatusbar *statusbar,
382 GtkStatusbarPrivate *priv;
383 GtkStatusbarMsg *msg;
385 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
387 priv = statusbar->priv;
393 for (list = priv->messages; list; list = list->next)
397 if (msg->context_id == context_id)
399 priv->messages = g_slist_remove_link (priv->messages,
402 g_slice_free (GtkStatusbarMsg, msg);
403 g_slist_free_1 (list);
409 msg = priv->messages ? priv->messages->data : NULL;
411 g_signal_emit (statusbar,
412 statusbar_signals[SIGNAL_TEXT_POPPED],
414 (guint) (msg ? msg->context_id : 0),
415 msg ? msg->text : NULL);
419 * gtk_statusbar_remove:
420 * @statusbar: a #GtkStatusBar
421 * @context_id: a context identifier
422 * @message_id: a message identifier, as returned by gtk_statusbar_push()
424 * Forces the removal of a message from a statusbar's stack.
425 * The exact @context_id and @message_id must be specified.
428 gtk_statusbar_remove (GtkStatusbar *statusbar,
432 GtkStatusbarPrivate *priv;
433 GtkStatusbarMsg *msg;
435 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
436 g_return_if_fail (message_id > 0);
438 priv = statusbar->priv;
440 msg = priv->messages ? priv->messages->data : NULL;
445 /* care about signal emission if the topmost item is removed */
446 if (msg->context_id == context_id &&
447 msg->message_id == message_id)
449 gtk_statusbar_pop (statusbar, context_id);
453 for (list = priv->messages; list; list = list->next)
457 if (msg->context_id == context_id &&
458 msg->message_id == message_id)
460 priv->messages = g_slist_remove_link (priv->messages, list);
462 g_slice_free (GtkStatusbarMsg, msg);
463 g_slist_free_1 (list);
472 * gtk_statusbar_remove_all:
473 * @statusbar: a #GtkStatusBar
474 * @context_id: a context identifier
476 * Forces the removal of all messages from a statusbar's
477 * stack with the exact @context_id.
482 gtk_statusbar_remove_all (GtkStatusbar *statusbar,
485 GtkStatusbarPrivate *priv;
486 GtkStatusbarMsg *msg;
489 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
491 priv = statusbar->priv;
493 if (priv->messages == NULL)
496 msg = priv->messages->data;
498 /* care about signal emission if the topmost item is removed */
499 if (msg->context_id == context_id)
501 gtk_statusbar_pop (statusbar, context_id);
504 list = priv->messages;
508 prev = priv->messages;
516 if (msg->context_id == context_id)
519 priv->messages = list->next;
521 prev->next = list->next;
524 g_slice_free (GtkStatusbarMsg, msg);
525 g_slist_free_1 (list);
528 prev = priv->messages;
541 * gtk_statusbar_get_message_area:
542 * @statusbar: a #GtkStatusBar
544 * Retrieves the box containing the label widget.
546 * Returns: (transfer none): a #GtkBox
551 gtk_statusbar_get_message_area (GtkStatusbar *statusbar)
553 GtkStatusbarPrivate *priv;
555 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), NULL);
557 priv = statusbar->priv;
559 return gtk_bin_get_child (GTK_BIN (priv->frame));
563 gtk_statusbar_destroy (GtkWidget *widget)
565 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
566 GtkStatusbarPrivate *priv = statusbar->priv;
569 for (list = priv->messages; list; list = list->next)
571 GtkStatusbarMsg *msg;
575 g_slice_free (GtkStatusbarMsg, msg);
577 g_slist_free (priv->messages);
578 priv->messages = NULL;
580 for (list = priv->keys; list; list = list->next)
582 g_slist_free (priv->keys);
585 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->destroy (widget);
588 /* look for extra children between the frame containing
589 * the label and where we want to draw the resize grip
592 has_extra_children (GtkStatusbar *statusbar)
594 GtkStatusbarPrivate *priv = statusbar->priv;
595 GtkPackType child_pack_type, frame_pack_type;
596 GtkWidget *child, *frame;
598 gboolean retval = FALSE;
601 children = _gtk_box_get_children (GTK_BOX (statusbar));
602 for (l = children; l; l = l->next)
606 if (frame == priv->frame)
610 gtk_box_query_child_packing (GTK_BOX (statusbar), frame,
611 NULL, NULL, NULL, &frame_pack_type);
613 for (l = l->next; l; l = l->next)
617 if (!gtk_widget_get_visible (child))
620 gtk_box_query_child_packing (GTK_BOX (statusbar), child,
621 NULL, NULL, NULL, &child_pack_type);
623 if (frame_pack_type == GTK_PACK_START || child_pack_type == GTK_PACK_END)
630 g_list_free (children);
636 gtk_statusbar_size_allocate (GtkWidget *widget,
637 GtkAllocation *allocation)
639 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
640 GtkStatusbarPrivate *priv = statusbar->priv;
641 gboolean extra_children = FALSE;
642 gboolean has_resize_grip = FALSE;
646 GdkRectangle translated_rect;
648 window = gtk_widget_get_toplevel (widget);
649 if (GTK_IS_WINDOW (window) &&
650 gtk_window_resize_grip_is_visible (GTK_WINDOW (window)))
652 gtk_window_get_resize_grip_area (GTK_WINDOW (window), &rect);
654 if (gtk_widget_translate_coordinates (gtk_widget_get_parent (widget),
661 translated_rect.x = x;
662 translated_rect.y = y;
663 translated_rect.width = allocation->width;
664 translated_rect.height = allocation->height;
666 if (gdk_rectangle_intersect (&rect, &translated_rect, NULL))
668 has_resize_grip = TRUE;
669 extra_children = has_extra_children (statusbar);
671 /* If there are extra children, we don't want them to occupy
672 * the space where we draw the resize grip, so we temporarily
673 * shrink the allocation.
674 * If there are no extra children, we want the frame to get
675 * the full allocation, and we fix up the allocation of the
676 * label afterwards to make room for the grip.
680 allocation->width -= rect.width;
681 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
682 allocation->x += rect.width;
688 /* chain up normally */
689 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_allocate (widget, allocation);
695 allocation->width += rect.width;
696 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
697 allocation->x -= rect.width;
699 gtk_widget_set_allocation (widget, allocation);
703 GtkAllocation child_allocation, frame_allocation;
706 /* Use the frame's child instead of statusbar->label directly, in case
707 * the label has been replaced by a container as the frame's child
708 * (and the label reparented into that container).
710 child = gtk_bin_get_child (GTK_BIN (priv->frame));
712 gtk_widget_get_allocation (child, &child_allocation);
713 gtk_widget_get_allocation (priv->frame, &frame_allocation);
714 if (child_allocation.width + rect.width > frame_allocation.width)
716 /* shrink the label to make room for the grip */
717 *allocation = child_allocation;
718 allocation->width = MAX (1, allocation->width - rect.width);
719 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
720 allocation->x += child_allocation.width - allocation->width;
722 gtk_widget_size_allocate (child, allocation);
729 resize_grip_visible_changed (GObject *object,
733 GtkStatusbar *statusbar = GTK_STATUSBAR (user_data);
734 GtkStatusbarPrivate *priv = statusbar->priv;
736 gtk_widget_queue_resize (GTK_WIDGET (statusbar));
737 gtk_widget_queue_resize (priv->label);
738 gtk_widget_queue_resize (priv->frame);
742 gtk_statusbar_hierarchy_changed (GtkWidget *widget,
743 GtkWidget *previous_toplevel)
747 if (previous_toplevel)
748 g_signal_handlers_disconnect_by_func (previous_toplevel,
749 G_CALLBACK (resize_grip_visible_changed),
751 window = gtk_widget_get_toplevel (widget);
752 if (GTK_IS_WINDOW (window))
753 g_signal_connect (window, "notify::resize-grip-visible",
754 G_CALLBACK (resize_grip_visible_changed), widget);