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;
263 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), 0);
264 g_return_val_if_fail (text != NULL, 0);
266 msg = g_slice_new (GtkStatusbarMsg);
267 msg->text = g_strdup (text);
268 msg->context_id = context_id;
269 msg->message_id = statusbar->seq_message_id++;
271 statusbar->messages = g_slist_prepend (statusbar->messages, msg);
273 g_signal_emit (statusbar,
274 statusbar_signals[SIGNAL_TEXT_PUSHED],
279 return msg->message_id;
283 gtk_statusbar_pop (GtkStatusbar *statusbar,
286 GtkStatusbarMsg *msg;
288 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
290 if (statusbar->messages)
294 for (list = statusbar->messages; list; list = list->next)
298 if (msg->context_id == context_id)
300 statusbar->messages = g_slist_remove_link (statusbar->messages,
303 g_slice_free (GtkStatusbarMsg, msg);
304 g_slist_free_1 (list);
310 msg = statusbar->messages ? statusbar->messages->data : NULL;
312 g_signal_emit (statusbar,
313 statusbar_signals[SIGNAL_TEXT_POPPED],
315 (guint) (msg ? msg->context_id : 0),
316 msg ? msg->text : NULL);
320 gtk_statusbar_remove (GtkStatusbar *statusbar,
324 GtkStatusbarMsg *msg;
326 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
327 g_return_if_fail (message_id > 0);
329 msg = statusbar->messages ? statusbar->messages->data : NULL;
334 /* care about signal emission if the topmost item is removed */
335 if (msg->context_id == context_id &&
336 msg->message_id == message_id)
338 gtk_statusbar_pop (statusbar, context_id);
342 for (list = statusbar->messages; list; list = list->next)
346 if (msg->context_id == context_id &&
347 msg->message_id == message_id)
349 statusbar->messages = g_slist_remove_link (statusbar->messages, list);
351 g_slice_free (GtkStatusbarMsg, msg);
352 g_slist_free_1 (list);
361 gtk_statusbar_set_has_resize_grip (GtkStatusbar *statusbar,
364 g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
366 setting = setting != FALSE;
368 if (setting != statusbar->has_resize_grip)
370 statusbar->has_resize_grip = setting;
371 gtk_widget_queue_resize (statusbar->label);
372 gtk_widget_queue_draw (GTK_WIDGET (statusbar));
374 if (GTK_WIDGET_REALIZED (statusbar))
376 if (statusbar->has_resize_grip && statusbar->grip_window == NULL)
378 gtk_statusbar_create_window (statusbar);
379 if (GTK_WIDGET_MAPPED (statusbar))
380 gdk_window_show (statusbar->grip_window);
382 else if (!statusbar->has_resize_grip && statusbar->grip_window != NULL)
383 gtk_statusbar_destroy_window (statusbar);
386 g_object_notify (G_OBJECT (statusbar), "has-resize-grip");
391 gtk_statusbar_get_has_resize_grip (GtkStatusbar *statusbar)
393 g_return_val_if_fail (GTK_IS_STATUSBAR (statusbar), FALSE);
395 return statusbar->has_resize_grip;
399 gtk_statusbar_destroy (GtkObject *object)
401 GtkStatusbar *statusbar;
404 g_return_if_fail (GTK_IS_STATUSBAR (object));
406 statusbar = GTK_STATUSBAR (object);
408 for (list = statusbar->messages; list; list = list->next)
410 GtkStatusbarMsg *msg;
414 g_slice_free (GtkStatusbarMsg, msg);
416 g_slist_free (statusbar->messages);
417 statusbar->messages = NULL;
419 for (list = statusbar->keys; list; list = list->next)
421 g_slist_free (statusbar->keys);
422 statusbar->keys = NULL;
424 GTK_OBJECT_CLASS (gtk_statusbar_parent_class)->destroy (object);
428 gtk_statusbar_set_property (GObject *object,
433 GtkStatusbar *statusbar = GTK_STATUSBAR (object);
437 case PROP_HAS_RESIZE_GRIP:
438 gtk_statusbar_set_has_resize_grip (statusbar, g_value_get_boolean (value));
441 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
447 gtk_statusbar_get_property (GObject *object,
452 GtkStatusbar *statusbar = GTK_STATUSBAR (object);
456 case PROP_HAS_RESIZE_GRIP:
457 g_value_set_boolean (value, statusbar->has_resize_grip);
460 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
466 get_grip_edge (GtkStatusbar *statusbar)
468 GtkWidget *widget = GTK_WIDGET (statusbar);
470 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
471 return GDK_WINDOW_EDGE_SOUTH_EAST;
473 return GDK_WINDOW_EDGE_SOUTH_WEST;
477 get_grip_rect (GtkStatusbar *statusbar,
483 widget = GTK_WIDGET (statusbar);
485 /* These are in effect the max/default size of the grip. */
489 if (w > widget->allocation.width)
490 w = widget->allocation.width;
492 if (h > widget->allocation.height - widget->style->ythickness)
493 h = widget->allocation.height - widget->style->ythickness;
497 rect->y = widget->allocation.y + widget->allocation.height - h;
499 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
500 rect->x = widget->allocation.x + widget->allocation.width - w;
502 rect->x = widget->allocation.x + widget->style->xthickness;
506 set_grip_cursor (GtkStatusbar *statusbar)
508 if (statusbar->has_resize_grip)
510 GtkWidget *widget = GTK_WIDGET (statusbar);
511 GdkDisplay *display = gtk_widget_get_display (widget);
512 GdkCursorType cursor_type;
515 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
516 cursor_type = GDK_BOTTOM_RIGHT_CORNER;
518 cursor_type = GDK_BOTTOM_LEFT_CORNER;
520 cursor = gdk_cursor_new_for_display (display, cursor_type);
521 gdk_window_set_cursor (statusbar->grip_window, cursor);
522 gdk_cursor_unref (cursor);
527 gtk_statusbar_create_window (GtkStatusbar *statusbar)
530 GdkWindowAttr attributes;
531 gint attributes_mask;
534 g_return_if_fail (GTK_WIDGET_REALIZED (statusbar));
535 g_return_if_fail (statusbar->has_resize_grip);
537 widget = GTK_WIDGET (statusbar);
539 get_grip_rect (statusbar, &rect);
541 attributes.x = rect.x;
542 attributes.y = rect.y;
543 attributes.width = rect.width;
544 attributes.height = rect.height;
545 attributes.window_type = GDK_WINDOW_CHILD;
546 attributes.wclass = GDK_INPUT_ONLY;
547 attributes.event_mask = gtk_widget_get_events (widget) |
548 GDK_BUTTON_PRESS_MASK;
550 attributes_mask = GDK_WA_X | GDK_WA_Y;
552 statusbar->grip_window = gdk_window_new (widget->window,
553 &attributes, attributes_mask);
555 gdk_window_set_user_data (statusbar->grip_window, widget);
557 set_grip_cursor (statusbar);
561 gtk_statusbar_direction_changed (GtkWidget *widget,
562 GtkTextDirection prev_dir)
564 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
566 set_grip_cursor (statusbar);
570 gtk_statusbar_destroy_window (GtkStatusbar *statusbar)
572 gdk_window_set_user_data (statusbar->grip_window, NULL);
573 gdk_window_destroy (statusbar->grip_window);
574 statusbar->grip_window = NULL;
578 gtk_statusbar_realize (GtkWidget *widget)
580 GtkStatusbar *statusbar;
582 statusbar = GTK_STATUSBAR (widget);
584 (* GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->realize) (widget);
586 if (statusbar->has_resize_grip)
587 gtk_statusbar_create_window (statusbar);
591 gtk_statusbar_unrealize (GtkWidget *widget)
593 GtkStatusbar *statusbar;
595 statusbar = GTK_STATUSBAR (widget);
597 if (statusbar->grip_window)
598 gtk_statusbar_destroy_window (statusbar);
600 (* GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->unrealize) (widget);
604 gtk_statusbar_map (GtkWidget *widget)
606 GtkStatusbar *statusbar;
608 statusbar = GTK_STATUSBAR (widget);
610 (* GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->map) (widget);
612 if (statusbar->grip_window)
613 gdk_window_show (statusbar->grip_window);
617 gtk_statusbar_unmap (GtkWidget *widget)
619 GtkStatusbar *statusbar;
621 statusbar = GTK_STATUSBAR (widget);
623 if (statusbar->grip_window)
624 gdk_window_hide (statusbar->grip_window);
626 (* GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->unmap) (widget);
630 gtk_statusbar_button_press (GtkWidget *widget,
631 GdkEventButton *event)
633 GtkStatusbar *statusbar;
637 statusbar = GTK_STATUSBAR (widget);
639 if (!statusbar->has_resize_grip ||
640 event->type != GDK_BUTTON_PRESS ||
641 event->window != statusbar->grip_window)
644 ancestor = gtk_widget_get_toplevel (widget);
646 if (!GTK_IS_WINDOW (ancestor))
649 edge = get_grip_edge (statusbar);
651 if (event->button == 1)
652 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
655 event->x_root, event->y_root,
657 else if (event->button == 2)
658 gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
660 event->x_root, event->y_root,
669 gtk_statusbar_expose_event (GtkWidget *widget,
670 GdkEventExpose *event)
672 GtkStatusbar *statusbar;
675 statusbar = GTK_STATUSBAR (widget);
677 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->expose_event (widget, event);
679 if (statusbar->has_resize_grip)
683 edge = get_grip_edge (statusbar);
685 get_grip_rect (statusbar, &rect);
687 gtk_paint_resize_grip (widget->style,
689 GTK_WIDGET_STATE (widget),
695 /* don't draw grip over the frame, though you
696 * can click on the frame.
698 rect.width - widget->style->xthickness,
699 rect.height - widget->style->ythickness);
706 gtk_statusbar_size_request (GtkWidget *widget,
707 GtkRequisition *requisition)
709 GtkStatusbar *statusbar;
710 GtkShadowType shadow_type;
712 statusbar = GTK_STATUSBAR (widget);
714 gtk_widget_style_get (GTK_WIDGET (statusbar), "shadow-type", &shadow_type, NULL);
715 gtk_frame_set_shadow_type (GTK_FRAME (statusbar->frame), shadow_type);
717 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_request (widget, requisition);
720 /* look for extra children between the frame containing
721 * the label and where we want to draw the resize grip
724 has_extra_children (GtkStatusbar *statusbar)
727 GtkBoxChild *child, *frame;
730 for (l = GTK_BOX (statusbar)->children; l; l = l->next)
734 if (frame->widget == statusbar->frame)
738 for (l = l->next; l; l = l->next)
742 if (!GTK_WIDGET_VISIBLE (child->widget))
745 if (frame->pack == GTK_PACK_START || child->pack == GTK_PACK_END)
753 gtk_statusbar_size_allocate (GtkWidget *widget,
754 GtkAllocation *allocation)
756 GtkStatusbar *statusbar = GTK_STATUSBAR (widget);
757 gboolean extra_children = FALSE;
760 if (statusbar->has_resize_grip)
762 widget->allocation = *allocation;
763 get_grip_rect (statusbar, &rect);
765 extra_children = has_extra_children (statusbar);
767 /* If there are extra children, we don't want them to occupy
768 * the space where we draw the resize grip, so we temporarily
769 * shrink the allocation.
770 * If there are no extra children, we want the frame to get
771 * the full allocation, and we fix up the allocation of the
772 * label afterwards to make room for the grip.
776 allocation->width -= rect.width;
777 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
778 allocation->x += rect.width;
782 /* chain up normally */
783 GTK_WIDGET_CLASS (gtk_statusbar_parent_class)->size_allocate (widget, allocation);
785 if (statusbar->has_resize_grip)
787 if (statusbar->grip_window)
789 gdk_window_raise (statusbar->grip_window);
790 gdk_window_move_resize (statusbar->grip_window,
792 rect.width, rect.height);
797 allocation->width += rect.width;
798 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
799 allocation->x -= rect.width;
801 widget->allocation = *allocation;
805 if (statusbar->label->allocation.width + rect.width > statusbar->frame->allocation.width)
807 /* shrink the label to make room for the grip */
808 *allocation = statusbar->label->allocation;
809 allocation->width = MAX (1, allocation->width - rect.width);
810 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
811 allocation->x += statusbar->label->allocation.width - allocation->width;
813 gtk_widget_size_allocate (statusbar->label, allocation);
820 label_selectable_changed (GtkWidget *label,
824 GtkStatusbar *statusbar = GTK_STATUSBAR (data);
827 statusbar->has_resize_grip && statusbar->grip_window)
828 gdk_window_raise (statusbar->grip_window);
831 #define __GTK_STATUSBAR_C__
832 #include "gtkaliasdef.c"