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"
38 typedef struct _GtkStatusbarMsg GtkStatusbarMsg;
40 struct _GtkStatusbarMsg
60 static void gtk_statusbar_class_init (GtkStatusbarClass *class);
61 static void gtk_statusbar_init (GtkStatusbar *statusbar);
62 static void gtk_statusbar_destroy (GtkObject *object);
63 static void gtk_statusbar_update (GtkStatusbar *statusbar,
66 static void gtk_statusbar_size_allocate (GtkWidget *widget,
67 GtkAllocation *allocation);
68 static void gtk_statusbar_realize (GtkWidget *widget);
69 static void gtk_statusbar_unrealize (GtkWidget *widget);
70 static void gtk_statusbar_map (GtkWidget *widget);
71 static void gtk_statusbar_unmap (GtkWidget *widget);
72 static gboolean gtk_statusbar_button_press (GtkWidget *widget,
73 GdkEventButton *event);
74 static gboolean gtk_statusbar_expose_event (GtkWidget *widget,
75 GdkEventExpose *event);
76 static void gtk_statusbar_size_request (GtkWidget *widget,
77 GtkRequisition *requisition);
78 static void gtk_statusbar_size_allocate (GtkWidget *widget,
79 GtkAllocation *allocation);
80 static void gtk_statusbar_direction_changed (GtkWidget *widget,
81 GtkTextDirection prev_dir);
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,
88 static void gtk_statusbar_set_property (GObject *object,
92 static void label_selectable_changed (GtkWidget *label,
97 static GtkContainerClass *parent_class;
98 static guint statusbar_signals[SIGNAL_LAST] = { 0 };
101 gtk_statusbar_get_type (void)
103 static GType statusbar_type = 0;
107 static const GTypeInfo statusbar_info =
109 sizeof (GtkStatusbarClass),
110 NULL, /* base_init */
111 NULL, /* base_finalize */
112 (GClassInitFunc) gtk_statusbar_class_init,
113 NULL, /* class_finalize */
114 NULL, /* class_data */
115 sizeof (GtkStatusbar),
117 (GInstanceInitFunc) gtk_statusbar_init,
120 statusbar_type = g_type_register_static (GTK_TYPE_HBOX, I_("GtkStatusbar"),
124 return statusbar_type;
128 gtk_statusbar_class_init (GtkStatusbarClass *class)
130 GObjectClass *gobject_class;
131 GtkObjectClass *object_class;
132 GtkWidgetClass *widget_class;
134 gobject_class = (GObjectClass *) class;
135 object_class = (GtkObjectClass *) class;
136 widget_class = (GtkWidgetClass *) class;
138 parent_class = g_type_class_peek_parent (class);
140 gobject_class->set_property = gtk_statusbar_set_property;
141 gobject_class->get_property = gtk_statusbar_get_property;
143 object_class->destroy = gtk_statusbar_destroy;
145 widget_class->realize = gtk_statusbar_realize;
146 widget_class->unrealize = gtk_statusbar_unrealize;
147 widget_class->map = gtk_statusbar_map;
148 widget_class->unmap = gtk_statusbar_unmap;
150 widget_class->button_press_event = gtk_statusbar_button_press;
151 widget_class->expose_event = gtk_statusbar_expose_event;
153 widget_class->size_request = gtk_statusbar_size_request;
154 widget_class->size_allocate = gtk_statusbar_size_allocate;
156 widget_class->direction_changed = gtk_statusbar_direction_changed;
158 class->text_pushed = gtk_statusbar_update;
159 class->text_popped = gtk_statusbar_update;
162 * GtkStatusbar:has-resize-grip:
164 * Whether the statusbar has a grip for resizing the toplevel window.
168 g_object_class_install_property (gobject_class,
169 PROP_HAS_RESIZE_GRIP,
170 g_param_spec_boolean ("has-resize-grip",
171 P_("Has Resize Grip"),
172 P_("Whether the statusbar has a grip for resizing the toplevel"),
174 GTK_PARAM_READWRITE));
175 statusbar_signals[SIGNAL_TEXT_PUSHED] =
176 g_signal_new (I_("text_pushed"),
177 G_OBJECT_CLASS_TYPE (class),
179 G_STRUCT_OFFSET (GtkStatusbarClass, text_pushed),
181 _gtk_marshal_VOID__UINT_STRING,
185 statusbar_signals[SIGNAL_TEXT_POPPED] =
186 g_signal_new (I_("text_popped"),
187 G_OBJECT_CLASS_TYPE (class),
189 G_STRUCT_OFFSET (GtkStatusbarClass, text_popped),
191 _gtk_marshal_VOID__UINT_STRING,
196 gtk_widget_class_install_style_property (widget_class,
197 g_param_spec_enum ("shadow-type",
199 P_("Style of bevel around the statusbar text"),
200 GTK_TYPE_SHADOW_TYPE,
202 GTK_PARAM_READABLE));
206 gtk_statusbar_init (GtkStatusbar *statusbar)
209 GtkShadowType shadow_type;
211 box = GTK_BOX (statusbar);
214 box->homogeneous = FALSE;
216 statusbar->has_resize_grip = TRUE;
218 gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);
220 statusbar->frame = gtk_frame_new (NULL);
221 gtk_frame_set_shadow_type (GTK_FRAME (statusbar->frame), shadow_type);
222 gtk_box_pack_start (box, statusbar->frame, TRUE, TRUE, 0);
223 gtk_widget_show (statusbar->frame);
225 statusbar->label = gtk_label_new ("");
226 gtk_label_set_single_line_mode (GTK_LABEL (statusbar->label), TRUE);
227 gtk_misc_set_alignment (GTK_MISC (statusbar->label), 0.0, 0.5);
228 g_signal_connect (statusbar->label, "notify::selectable",
229 G_CALLBACK (label_selectable_changed), statusbar);
230 gtk_label_set_ellipsize (GTK_LABEL (statusbar->label), PANGO_ELLIPSIZE_END);
231 gtk_container_add (GTK_CONTAINER (statusbar->frame), statusbar->label);
232 gtk_widget_show (statusbar->label);
234 statusbar->seq_context_id = 1;
235 statusbar->seq_message_id = 1;
236 statusbar->messages = NULL;
237 statusbar->keys = NULL;
241 gtk_statusbar_new (void)
243 return g_object_new (GTK_TYPE_STATUSBAR, NULL);
247 gtk_statusbar_update (GtkStatusbar *statusbar,
251 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
256 gtk_label_set_text (GTK_LABEL (statusbar->label), text);
260 gtk_statusbar_get_context_id (GtkStatusbar *statusbar,
261 const gchar *context_description)
266 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
267 g_return_val_if_fail (context_description != NULL, 0);
269 /* we need to preserve namespaces on object datas */
270 string = g_strconcat ("gtk-status-bar-context:", context_description, NULL);
272 id = g_object_get_data (G_OBJECT (statusbar), string);
275 id = g_new (guint, 1);
276 *id = statusbar->seq_context_id++;
277 g_object_set_data_full (G_OBJECT (statusbar), string, id, g_free);
278 statusbar->keys = g_slist_prepend (statusbar->keys, string);
287 gtk_statusbar_push (GtkStatusbar *statusbar,
291 GtkStatusbarMsg *msg;
292 GtkStatusbarClass *class;
294 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
295 g_return_val_if_fail (text != NULL, 0);
297 class = GTK_STATUSBAR_GET_CLASS (statusbar);
298 msg = g_slice_new (GtkStatusbarMsg);
299 msg->text = g_strdup (text);
300 msg->context_id = context_id;
301 msg->message_id = statusbar->seq_message_id++;
303 statusbar->messages = g_slist_prepend (statusbar->messages, msg);
305 g_signal_emit (statusbar,
306 statusbar_signals[SIGNAL_TEXT_PUSHED],
311 return msg->message_id;
315 gtk_statusbar_pop (GtkStatusbar *statusbar,
318 GtkStatusbarMsg *msg;
320 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
322 if (statusbar->messages)
326 for (list = statusbar->messages; list; list = list->next)
330 if (msg->context_id == context_id)
332 GtkStatusbarClass *class;
334 class = GTK_STATUSBAR_GET_CLASS (statusbar);
336 statusbar->messages = g_slist_remove_link (statusbar->messages,
339 g_slice_free (GtkStatusbarMsg, msg);
340 g_slist_free_1 (list);
346 msg = statusbar->messages ? statusbar->messages->data : NULL;
348 g_signal_emit (statusbar,
349 statusbar_signals[SIGNAL_TEXT_POPPED],
351 (guint) (msg ? msg->context_id : 0),
352 msg ? msg->text : NULL);
356 gtk_statusbar_remove (GtkStatusbar *statusbar,
360 GtkStatusbarMsg *msg;
362 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
363 g_return_if_fail (message_id > 0);
365 msg = statusbar->messages ? statusbar->messages->data : NULL;
370 /* care about signal emission if the topmost item is removed */
371 if (msg->context_id == context_id &&
372 msg->message_id == message_id)
374 gtk_statusbar_pop (statusbar, context_id);
378 for (list = statusbar->messages; list; list = list->next)
382 if (msg->context_id == context_id &&
383 msg->message_id == message_id)
385 GtkStatusbarClass *class;
387 class = GTK_STATUSBAR_GET_CLASS (statusbar);
388 statusbar->messages = g_slist_remove_link (statusbar->messages, list);
390 g_slice_free (GtkStatusbarMsg, msg);
391 g_slist_free_1 (list);
400 gtk_statusbar_set_has_resize_grip (GtkStatusbar *statusbar,
403 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
405 setting = setting != FALSE;
407 if (setting != statusbar->has_resize_grip)
409 statusbar->has_resize_grip = setting;
410 gtk_widget_queue_resize (statusbar->label);
411 gtk_widget_queue_draw (GTK_WIDGET (statusbar));
413 if (GTK_WIDGET_REALIZED (statusbar))
415 if (statusbar->has_resize_grip && statusbar->grip_window == NULL)
417 gtk_statusbar_create_window (statusbar);
418 if (GTK_WIDGET_MAPPED (statusbar))
419 gdk_window_show (statusbar->grip_window);
421 else if (!statusbar->has_resize_grip && statusbar->grip_window != NULL)
422 gtk_statusbar_destroy_window (statusbar);
425 g_object_notify (G_OBJECT (statusbar), "has-resize-grip");
430 gtk_statusbar_get_has_resize_grip (GtkStatusbar *statusbar)
432 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), FALSE);
434 return statusbar->has_resize_grip;
438 gtk_statusbar_destroy (GtkObject *object)
440 GtkStatusbar *statusbar;
441 GtkStatusbarClass *class;
444 g_return_if_fail (GTK_IS_STATUSBAR (object));
446 statusbar = GTK_STATUSBAR (object);
447 class = GTK_STATUSBAR_GET_CLASS (statusbar);
449 for (list = statusbar->messages; list; list = list->next)
451 GtkStatusbarMsg *msg;
455 g_slice_free (GtkStatusbarMsg, msg);
457 g_slist_free (statusbar->messages);
458 statusbar->messages = NULL;
460 for (list = statusbar->keys; list; list = list->next)
462 g_slist_free (statusbar->keys);
463 statusbar->keys = NULL;
465 GTK_OBJECT_CLASS (parent_class)->destroy (object);
469 gtk_statusbar_set_property (GObject *object,
474 GtkStatusbar *statusbar = GTK_STATUSBAR (object);
478 case PROP_HAS_RESIZE_GRIP:
479 gtk_statusbar_set_has_resize_grip (statusbar, g_value_get_boolean (value));
482 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
488 gtk_statusbar_get_property (GObject *object,
493 GtkStatusbar *statusbar = GTK_STATUSBAR (object);
497 case PROP_HAS_RESIZE_GRIP:
498 g_value_set_boolean (value, statusbar->has_resize_grip);
501 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
507 get_grip_edge (GtkStatusbar *statusbar)
509 GtkWidget *widget = GTK_WIDGET (statusbar);
511 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
512 return GDK_WINDOW_EDGE_SOUTH_EAST;
514 return GDK_WINDOW_EDGE_SOUTH_WEST;
518 get_grip_rect (GtkStatusbar *statusbar,
524 widget = GTK_WIDGET (statusbar);
526 /* These are in effect the max/default size of the grip. */
530 if (w > widget->allocation.width)
531 w = widget->allocation.width;
533 if (h > widget->allocation.height - widget->style->ythickness)
534 h = widget->allocation.height - widget->style->ythickness;
538 rect->y = widget->allocation.y + widget->allocation.height - h;
540 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
541 rect->x = widget->allocation.x + widget->allocation.width - w;
543 rect->x = widget->allocation.x + widget->style->xthickness;
547 set_grip_cursor (GtkStatusbar *statusbar)
549 if (statusbar->has_resize_grip)
551 GtkWidget *widget = GTK_WIDGET (statusbar);
552 GdkDisplay *display = gtk_widget_get_display (widget);
553 GdkCursorType cursor_type;
556 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
557 cursor_type = GDK_BOTTOM_RIGHT_CORNER;
559 cursor_type = GDK_BOTTOM_LEFT_CORNER;
561 cursor = gdk_cursor_new_for_display (display, cursor_type);
562 gdk_window_set_cursor (statusbar->grip_window, cursor);
563 gdk_cursor_unref (cursor);
568 gtk_statusbar_create_window (GtkStatusbar *statusbar)
571 GdkWindowAttr attributes;
572 gint attributes_mask;
575 g_return_if_fail (GTK_WIDGET_REALIZED (statusbar));
576 g_return_if_fail (statusbar->has_resize_grip);
578 widget = GTK_WIDGET (statusbar);
580 get_grip_rect (statusbar, &rect);
582 attributes.x = rect.x;
583 attributes.y = rect.y;
584 attributes.width = rect.width;
585 attributes.height = rect.height;
586 attributes.window_type = GDK_WINDOW_CHILD;
587 attributes.wclass = GDK_INPUT_ONLY;
588 attributes.event_mask = gtk_widget_get_events (widget) |
589 GDK_BUTTON_PRESS_MASK;
591 attributes_mask = GDK_WA_X | GDK_WA_Y;
593 statusbar->grip_window = gdk_window_new (widget->window,
594 &attributes, attributes_mask);
596 gdk_window_set_user_data (statusbar->grip_window, widget);
598 set_grip_cursor (statusbar);
602 gtk_statusbar_direction_changed (GtkWidget *widget,
603 GtkTextDirection prev_dir)
605 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
607 set_grip_cursor (statusbar);
611 gtk_statusbar_destroy_window (GtkStatusbar *statusbar)
613 gdk_window_set_user_data (statusbar->grip_window, NULL);
614 gdk_window_destroy (statusbar->grip_window);
615 statusbar->grip_window = NULL;
619 gtk_statusbar_realize (GtkWidget *widget)
621 GtkStatusbar *statusbar;
623 statusbar = GTK_STATUSBAR (widget);
625 (* GTK_WIDGET_CLASS (parent_class)->realize) (widget);
627 if (statusbar->has_resize_grip)
628 gtk_statusbar_create_window (statusbar);
632 gtk_statusbar_unrealize (GtkWidget *widget)
634 GtkStatusbar *statusbar;
636 statusbar = GTK_STATUSBAR (widget);
638 if (statusbar->grip_window)
639 gtk_statusbar_destroy_window (statusbar);
641 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
645 gtk_statusbar_map (GtkWidget *widget)
647 GtkStatusbar *statusbar;
649 statusbar = GTK_STATUSBAR (widget);
651 (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
653 if (statusbar->grip_window)
654 gdk_window_show (statusbar->grip_window);
658 gtk_statusbar_unmap (GtkWidget *widget)
660 GtkStatusbar *statusbar;
662 statusbar = GTK_STATUSBAR (widget);
664 if (statusbar->grip_window)
665 gdk_window_hide (statusbar->grip_window);
667 (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
671 gtk_statusbar_button_press (GtkWidget *widget,
672 GdkEventButton *event)
674 GtkStatusbar *statusbar;
678 statusbar = GTK_STATUSBAR (widget);
680 if (!statusbar->has_resize_grip ||
681 event->type != GDK_BUTTON_PRESS ||
682 event->window != statusbar->grip_window)
685 ancestor = gtk_widget_get_toplevel (widget);
687 if (!GTK_IS_WINDOW (ancestor))
690 edge = get_grip_edge (statusbar);
692 if (event->button == 1)
693 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
696 event->x_root, event->y_root,
698 else if (event->button == 2)
699 gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
701 event->x_root, event->y_root,
710 gtk_statusbar_expose_event (GtkWidget *widget,
711 GdkEventExpose *event)
713 GtkStatusbar *statusbar;
716 statusbar = GTK_STATUSBAR (widget);
718 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
720 if (statusbar->has_resize_grip)
724 edge = get_grip_edge (statusbar);
726 get_grip_rect (statusbar, &rect);
728 gtk_paint_resize_grip (widget->style,
730 GTK_WIDGET_STATE (widget),
736 /* don't draw grip over the frame, though you
737 * can click on the frame.
739 rect.width - widget->style->xthickness,
740 rect.height - widget->style->ythickness);
747 gtk_statusbar_size_request (GtkWidget *widget,
748 GtkRequisition *requisition)
750 GtkStatusbar *statusbar;
751 GtkShadowType shadow_type;
753 statusbar = GTK_STATUSBAR (widget);
755 gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);
756 gtk_frame_set_shadow_type (GTK_FRAME (statusbar->frame), shadow_type);
758 GTK_WIDGET_CLASS (parent_class)->size_request (widget, requisition);
761 /* look for extra children between the frame containing
762 * the label and where we want to draw the resize grip
765 has_extra_children (GtkStatusbar *statusbar)
768 GtkBoxChild *child, *frame;
771 for (l = GTK_BOX (statusbar)->children; l; l = l->next)
775 if (frame->widget == statusbar->frame)
779 for (l = l->next; l; l = l->next)
783 if (!GTK_WIDGET_VISIBLE (child->widget))
786 if (frame->pack == GTK_PACK_START || child->pack == GTK_PACK_END)
794 gtk_statusbar_size_allocate (GtkWidget *widget,
795 GtkAllocation *allocation)
797 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
798 gboolean extra_children = FALSE;
801 if (statusbar->has_resize_grip)
803 widget->allocation = *allocation;
804 get_grip_rect (statusbar, &rect);
806 extra_children = has_extra_children (statusbar);
808 /* If there are extra children, we don't want them to occupy
809 * the space where we draw the resize grip, so we temporarily
810 * shrink the allocation.
811 * If there are no extra children, we want the frame to get
812 * the full allocation, and we fix up the allocation of the
813 * label afterwards to make room for the grip.
817 allocation->width -= rect.width;
818 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
819 allocation->x += rect.width;
823 /* chain up normally */
824 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
826 if (statusbar->has_resize_grip)
828 if (statusbar->grip_window)
830 gdk_window_raise (statusbar->grip_window);
831 gdk_window_move_resize (statusbar->grip_window,
833 rect.width, rect.height);
838 allocation->width += rect.width;
839 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
840 allocation->x -= rect.width;
842 widget->allocation = *allocation;
846 if (statusbar->label->allocation.width + rect.width > statusbar->frame->allocation.width)
848 /* shrink the label to make room for the grip */
849 *allocation = statusbar->label->allocation;
850 allocation->width = MAX (1, allocation->width - rect.width);
851 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
852 allocation->x += statusbar->label->allocation.width - allocation->width;
854 gtk_widget_size_allocate (statusbar->label, allocation);
861 label_selectable_changed (GtkWidget *label,
865 GtkStatusbar *statusbar = GTK_STATUSBAR (data);
868 statusbar->has_resize_grip && statusbar->grip_window)
869 gdk_window_raise (statusbar->grip_window);
872 #define __GTK_STATUSBAR_C__
873 #include "gtkaliasdef.c"