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_destroy (GtkObject *object);
61 static void gtk_statusbar_update (GtkStatusbar *statusbar,
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_create_window (GtkStatusbar *statusbar);
81 static void gtk_statusbar_destroy_window (GtkStatusbar *statusbar);
82 static void gtk_statusbar_get_property (GObject *object,
86 static void gtk_statusbar_set_property (GObject *object,
90 static void label_selectable_changed (GtkWidget *label,
95 static guint statusbar_signals[SIGNAL_LAST] = { 0 };
97 G_DEFINE_TYPE (GtkStatusbar, gtk_statusbar, GTK_TYPE_HBOX)
100 gtk_statusbar_class_init (GtkStatusbarClass *class)
102 GObjectClass *gobject_class;
103 GtkObjectClass *object_class;
104 GtkWidgetClass *widget_class;
106 gobject_class = (GObjectClass *) class;
107 object_class = (GtkObjectClass *) class;
108 widget_class = (GtkWidgetClass *) class;
110 gobject_class->set_property = gtk_statusbar_set_property;
111 gobject_class->get_property = gtk_statusbar_get_property;
113 object_class->destroy = gtk_statusbar_destroy;
115 widget_class->realize = gtk_statusbar_realize;
116 widget_class->unrealize = gtk_statusbar_unrealize;
117 widget_class->map = gtk_statusbar_map;
118 widget_class->unmap = gtk_statusbar_unmap;
120 widget_class->button_press_event = gtk_statusbar_button_press;
121 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;
126 widget_class->direction_changed = gtk_statusbar_direction_changed;
128 class->text_pushed = gtk_statusbar_update;
129 class->text_popped = gtk_statusbar_update;
132 * GtkStatusbar:has-resize-grip:
134 * Whether the statusbar has a grip for resizing the toplevel window.
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"),
144 GTK_PARAM_READWRITE));
145 statusbar_signals[SIGNAL_TEXT_PUSHED] =
146 g_signal_new (I_("text_pushed"),
147 G_OBJECT_CLASS_TYPE (class),
149 G_STRUCT_OFFSET (GtkStatusbarClass, text_pushed),
151 _gtk_marshal_VOID__UINT_STRING,
155 statusbar_signals[SIGNAL_TEXT_POPPED] =
156 g_signal_new (I_("text_popped"),
157 G_OBJECT_CLASS_TYPE (class),
159 G_STRUCT_OFFSET (GtkStatusbarClass, text_popped),
161 _gtk_marshal_VOID__UINT_STRING,
166 gtk_widget_class_install_style_property (widget_class,
167 g_param_spec_enum ("shadow-type",
169 P_("Style of bevel around the statusbar text"),
170 GTK_TYPE_SHADOW_TYPE,
172 GTK_PARAM_READABLE));
176 gtk_statusbar_init (GtkStatusbar *statusbar)
179 GtkShadowType shadow_type;
181 box = GTK_BOX (statusbar);
184 box->homogeneous = FALSE;
186 statusbar->has_resize_grip = TRUE;
188 gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);
190 statusbar->frame = gtk_frame_new (NULL);
191 gtk_frame_set_shadow_type (GTK_FRAME (statusbar->frame), shadow_type);
192 gtk_box_pack_start (box, statusbar->frame, TRUE, TRUE, 0);
193 gtk_widget_show (statusbar->frame);
195 statusbar->label = gtk_label_new ("");
196 gtk_label_set_single_line_mode (GTK_LABEL (statusbar->label), TRUE);
197 gtk_misc_set_alignment (GTK_MISC (statusbar->label), 0.0, 0.5);
198 g_signal_connect (statusbar->label, "notify::selectable",
199 G_CALLBACK (label_selectable_changed), statusbar);
200 gtk_label_set_ellipsize (GTK_LABEL (statusbar->label), PANGO_ELLIPSIZE_END);
201 gtk_container_add (GTK_CONTAINER (statusbar->frame), statusbar->label);
202 gtk_widget_show (statusbar->label);
204 statusbar->seq_context_id = 1;
205 statusbar->seq_message_id = 1;
206 statusbar->messages = NULL;
207 statusbar->keys = NULL;
211 gtk_statusbar_new (void)
213 return g_object_new (GTK_TYPE_STATUSBAR, NULL);
217 gtk_statusbar_update (GtkStatusbar *statusbar,
221 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
226 gtk_label_set_text (GTK_LABEL (statusbar->label), text);
230 gtk_statusbar_get_context_id (GtkStatusbar *statusbar,
231 const gchar *context_description)
236 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
237 g_return_val_if_fail (context_description != NULL, 0);
239 /* we need to preserve namespaces on object datas */
240 string = g_strconcat ("gtk-status-bar-context:", context_description, NULL);
242 id = g_object_get_data (G_OBJECT (statusbar), string);
245 id = g_new (guint, 1);
246 *id = statusbar->seq_context_id++;
247 g_object_set_data_full (G_OBJECT (statusbar), string, id, g_free);
248 statusbar->keys = g_slist_prepend (statusbar->keys, string);
257 gtk_statusbar_push (GtkStatusbar *statusbar,
261 GtkStatusbarMsg *msg;
262 GtkStatusbarClass *class;
264 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
265 g_return_val_if_fail (text != NULL, 0);
267 class = GTK_STATUSBAR_GET_CLASS (statusbar);
268 msg = g_slice_new (GtkStatusbarMsg);
269 msg->text = g_strdup (text);
270 msg->context_id = context_id;
271 msg->message_id = statusbar->seq_message_id++;
273 statusbar->messages = g_slist_prepend (statusbar->messages, msg);
275 g_signal_emit (statusbar,
276 statusbar_signals[SIGNAL_TEXT_PUSHED],
281 return msg->message_id;
285 gtk_statusbar_pop (GtkStatusbar *statusbar,
288 GtkStatusbarMsg *msg;
290 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
292 if (statusbar->messages)
296 for (list = statusbar->messages; list; list = list->next)
300 if (msg->context_id == context_id)
302 GtkStatusbarClass *class;
304 class = GTK_STATUSBAR_GET_CLASS (statusbar);
306 statusbar->messages = g_slist_remove_link (statusbar->messages,
309 g_slice_free (GtkStatusbarMsg, msg);
310 g_slist_free_1 (list);
316 msg = statusbar->messages ? statusbar->messages->data : NULL;
318 g_signal_emit (statusbar,
319 statusbar_signals[SIGNAL_TEXT_POPPED],
321 (guint) (msg ? msg->context_id : 0),
322 msg ? msg->text : NULL);
326 gtk_statusbar_remove (GtkStatusbar *statusbar,
330 GtkStatusbarMsg *msg;
332 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
333 g_return_if_fail (message_id > 0);
335 msg = statusbar->messages ? statusbar->messages->data : NULL;
340 /* care about signal emission if the topmost item is removed */
341 if (msg->context_id == context_id &&
342 msg->message_id == message_id)
344 gtk_statusbar_pop (statusbar, context_id);
348 for (list = statusbar->messages; list; list = list->next)
352 if (msg->context_id == context_id &&
353 msg->message_id == message_id)
355 GtkStatusbarClass *class;
357 class = GTK_STATUSBAR_GET_CLASS (statusbar);
358 statusbar->messages = g_slist_remove_link (statusbar->messages, list);
360 g_slice_free (GtkStatusbarMsg, msg);
361 g_slist_free_1 (list);
370 gtk_statusbar_set_has_resize_grip (GtkStatusbar *statusbar,
373 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
375 setting = setting != FALSE;
377 if (setting != statusbar->has_resize_grip)
379 statusbar->has_resize_grip = setting;
380 gtk_widget_queue_resize (statusbar->label);
381 gtk_widget_queue_draw (GTK_WIDGET (statusbar));
383 if (GTK_WIDGET_REALIZED (statusbar))
385 if (statusbar->has_resize_grip && statusbar->grip_window == NULL)
387 gtk_statusbar_create_window (statusbar);
388 if (GTK_WIDGET_MAPPED (statusbar))
389 gdk_window_show (statusbar->grip_window);
391 else if (!statusbar->has_resize_grip && statusbar->grip_window != NULL)
392 gtk_statusbar_destroy_window (statusbar);
395 g_object_notify (G_OBJECT (statusbar), "has-resize-grip");
400 gtk_statusbar_get_has_resize_grip (GtkStatusbar *statusbar)
402 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), FALSE);
404 return statusbar->has_resize_grip;
408 gtk_statusbar_destroy (GtkObject *object)
410 GtkStatusbar *statusbar;
411 GtkStatusbarClass *class;
414 g_return_if_fail (GTK_IS_STATUSBAR (object));
416 statusbar = GTK_STATUSBAR (object);
417 class = GTK_STATUSBAR_GET_CLASS (statusbar);
419 for (list = statusbar->messages; list; list = list->next)
421 GtkStatusbarMsg *msg;
425 g_slice_free (GtkStatusbarMsg, msg);
427 g_slist_free (statusbar->messages);
428 statusbar->messages = NULL;
430 for (list = statusbar->keys; list; list = list->next)
432 g_slist_free (statusbar->keys);
433 statusbar->keys = NULL;
435 GTK_OBJECT_CLASS (gtk_statusbar_parent_class)->destroy (object);
439 gtk_statusbar_set_property (GObject *object,
444 GtkStatusbar *statusbar = GTK_STATUSBAR (object);
448 case PROP_HAS_RESIZE_GRIP:
449 gtk_statusbar_set_has_resize_grip (statusbar, g_value_get_boolean (value));
452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458 gtk_statusbar_get_property (GObject *object,
463 GtkStatusbar *statusbar = GTK_STATUSBAR (object);
467 case PROP_HAS_RESIZE_GRIP:
468 g_value_set_boolean (value, statusbar->has_resize_grip);
471 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
477 get_grip_edge (GtkStatusbar *statusbar)
479 GtkWidget *widget = GTK_WIDGET (statusbar);
481 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
482 return GDK_WINDOW_EDGE_SOUTH_EAST;
484 return GDK_WINDOW_EDGE_SOUTH_WEST;
488 get_grip_rect (GtkStatusbar *statusbar,
494 widget = GTK_WIDGET (statusbar);
496 /* These are in effect the max/default size of the grip. */
500 if (w > widget->allocation.width)
501 w = widget->allocation.width;
503 if (h > widget->allocation.height - widget->style->ythickness)
504 h = widget->allocation.height - widget->style->ythickness;
508 rect->y = widget->allocation.y + widget->allocation.height - h;
510 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
511 rect->x = widget->allocation.x + widget->allocation.width - w;
513 rect->x = widget->allocation.x + widget->style->xthickness;
517 set_grip_cursor (GtkStatusbar *statusbar)
519 if (statusbar->has_resize_grip)
521 GtkWidget *widget = GTK_WIDGET (statusbar);
522 GdkDisplay *display = gtk_widget_get_display (widget);
523 GdkCursorType cursor_type;
526 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
527 cursor_type = GDK_BOTTOM_RIGHT_CORNER;
529 cursor_type = GDK_BOTTOM_LEFT_CORNER;
531 cursor = gdk_cursor_new_for_display (display, cursor_type);
532 gdk_window_set_cursor (statusbar->grip_window, cursor);
533 gdk_cursor_unref (cursor);
538 gtk_statusbar_create_window (GtkStatusbar *statusbar)
541 GdkWindowAttr attributes;
542 gint attributes_mask;
545 g_return_if_fail (GTK_WIDGET_REALIZED (statusbar));
546 g_return_if_fail (statusbar->has_resize_grip);
548 widget = GTK_WIDGET (statusbar);
550 get_grip_rect (statusbar, &rect);
552 attributes.x = rect.x;
553 attributes.y = rect.y;
554 attributes.width = rect.width;
555 attributes.height = rect.height;
556 attributes.window_type = GDK_WINDOW_CHILD;
557 attributes.wclass = GDK_INPUT_ONLY;
558 attributes.event_mask = gtk_widget_get_events (widget) |
559 GDK_BUTTON_PRESS_MASK;
561 attributes_mask = GDK_WA_X | GDK_WA_Y;
563 statusbar->grip_window = gdk_window_new (widget->window,
564 &attributes, attributes_mask);
566 gdk_window_set_user_data (statusbar->grip_window, widget);
568 set_grip_cursor (statusbar);
572 gtk_statusbar_direction_changed (GtkWidget *widget,
573 GtkTextDirection prev_dir)
575 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
577 set_grip_cursor (statusbar);
581 gtk_statusbar_destroy_window (GtkStatusbar *statusbar)
583 gdk_window_set_user_data (statusbar->grip_window, NULL);
584 gdk_window_destroy (statusbar->grip_window);
585 statusbar->grip_window = NULL;
589 gtk_statusbar_realize (GtkWidget *widget)
591 GtkStatusbar *statusbar;
593 statusbar = GTK_STATUSBAR (widget);
595 (* GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->realize) (widget);
597 if (statusbar->has_resize_grip)
598 gtk_statusbar_create_window (statusbar);
602 gtk_statusbar_unrealize (GtkWidget *widget)
604 GtkStatusbar *statusbar;
606 statusbar = GTK_STATUSBAR (widget);
608 if (statusbar->grip_window)
609 gtk_statusbar_destroy_window (statusbar);
611 (* GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->unrealize) (widget);
615 gtk_statusbar_map (GtkWidget *widget)
617 GtkStatusbar *statusbar;
619 statusbar = GTK_STATUSBAR (widget);
621 (* GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->map) (widget);
623 if (statusbar->grip_window)
624 gdk_window_show (statusbar->grip_window);
628 gtk_statusbar_unmap (GtkWidget *widget)
630 GtkStatusbar *statusbar;
632 statusbar = GTK_STATUSBAR (widget);
634 if (statusbar->grip_window)
635 gdk_window_hide (statusbar->grip_window);
637 (* GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->unmap) (widget);
641 gtk_statusbar_button_press (GtkWidget *widget,
642 GdkEventButton *event)
644 GtkStatusbar *statusbar;
648 statusbar = GTK_STATUSBAR (widget);
650 if (!statusbar->has_resize_grip ||
651 event->type != GDK_BUTTON_PRESS ||
652 event->window != statusbar->grip_window)
655 ancestor = gtk_widget_get_toplevel (widget);
657 if (!GTK_IS_WINDOW (ancestor))
660 edge = get_grip_edge (statusbar);
662 if (event->button == 1)
663 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
666 event->x_root, event->y_root,
668 else if (event->button == 2)
669 gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
671 event->x_root, event->y_root,
680 gtk_statusbar_expose_event (GtkWidget *widget,
681 GdkEventExpose *event)
683 GtkStatusbar *statusbar;
686 statusbar = GTK_STATUSBAR (widget);
688 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->expose_event (widget, event);
690 if (statusbar->has_resize_grip)
694 edge = get_grip_edge (statusbar);
696 get_grip_rect (statusbar, &rect);
698 gtk_paint_resize_grip (widget->style,
700 GTK_WIDGET_STATE (widget),
706 /* don't draw grip over the frame, though you
707 * can click on the frame.
709 rect.width - widget->style->xthickness,
710 rect.height - widget->style->ythickness);
717 gtk_statusbar_size_request (GtkWidget *widget,
718 GtkRequisition *requisition)
720 GtkStatusbar *statusbar;
721 GtkShadowType shadow_type;
723 statusbar = GTK_STATUSBAR (widget);
725 gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);
726 gtk_frame_set_shadow_type (GTK_FRAME (statusbar->frame), shadow_type);
728 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_request (widget, requisition);
731 /* look for extra children between the frame containing
732 * the label and where we want to draw the resize grip
735 has_extra_children (GtkStatusbar *statusbar)
738 GtkBoxChild *child, *frame;
741 for (l = GTK_BOX (statusbar)->children; l; l = l->next)
745 if (frame->widget == statusbar->frame)
749 for (l = l->next; l; l = l->next)
753 if (!GTK_WIDGET_VISIBLE (child->widget))
756 if (frame->pack == GTK_PACK_START || child->pack == GTK_PACK_END)
764 gtk_statusbar_size_allocate (GtkWidget *widget,
765 GtkAllocation *allocation)
767 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
768 gboolean extra_children = FALSE;
771 if (statusbar->has_resize_grip)
773 widget->allocation = *allocation;
774 get_grip_rect (statusbar, &rect);
776 extra_children = has_extra_children (statusbar);
778 /* If there are extra children, we don't want them to occupy
779 * the space where we draw the resize grip, so we temporarily
780 * shrink the allocation.
781 * If there are no extra children, we want the frame to get
782 * the full allocation, and we fix up the allocation of the
783 * label afterwards to make room for the grip.
787 allocation->width -= rect.width;
788 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
789 allocation->x += rect.width;
793 /* chain up normally */
794 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_allocate (widget, allocation);
796 if (statusbar->has_resize_grip)
798 if (statusbar->grip_window)
800 gdk_window_raise (statusbar->grip_window);
801 gdk_window_move_resize (statusbar->grip_window,
803 rect.width, rect.height);
808 allocation->width += rect.width;
809 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
810 allocation->x -= rect.width;
812 widget->allocation = *allocation;
816 if (statusbar->label->allocation.width + rect.width > statusbar->frame->allocation.width)
818 /* shrink the label to make room for the grip */
819 *allocation = statusbar->label->allocation;
820 allocation->width = MAX (1, allocation->width - rect.width);
821 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
822 allocation->x += statusbar->label->allocation.width - allocation->width;
824 gtk_widget_size_allocate (statusbar->label, allocation);
831 label_selectable_changed (GtkWidget *label,
835 GtkStatusbar *statusbar = GTK_STATUSBAR (data);
838 statusbar->has_resize_grip && statusbar->grip_window)
839 gdk_window_raise (statusbar->grip_window);
842 #define __GTK_STATUSBAR_C__
843 #include "gtkaliasdef.c"