1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 1998 Elliot Lee
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/.
29 #include "gtkhandlebox.h"
31 #include "gtkmarshalers.h"
32 #include "gtkwindow.h"
35 typedef struct _GtkHandleBoxPrivate GtkHandleBoxPrivate;
37 struct _GtkHandleBoxPrivate
52 #define DRAG_HANDLE_SIZE 10
53 #define CHILDLESS_SIZE 25
54 #define GHOST_HEIGHT 3
58 SIGNAL_CHILD_ATTACHED,
59 SIGNAL_CHILD_DETACHED,
63 /* The algorithm for docking and redocking implemented here
64 * has a couple of nice properties:
66 * 1) During a single drag, docking always occurs at the
67 * the same cursor position. This means that the users
68 * motions are reversible, and that you won't
69 * undock/dock oscillations.
71 * 2) Docking generally occurs at user-visible features.
72 * The user, once they figure out to redock, will
73 * have useful information about doing it again in
76 * Please try to preserve these properties if you
77 * change the algorithm. (And the current algorithm
78 * is far from ideal). Briefly, the current algorithm
79 * for deciding whether the handlebox is docked or not:
81 * 1) The decision is done by comparing two rectangles - the
82 * allocation if the widget at the start of the drag,
83 * and the boundary of hb->bin_window at the start of
84 * of the drag offset by the distance that the cursor
87 * 2) These rectangles must have one edge, the "snap_edge"
88 * of the handlebox, aligned within TOLERANCE.
90 * 3) On the other dimension, the extents of one rectangle
91 * must be contained in the extents of the other,
92 * extended by tolerance. That is, either we can have:
94 * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE->
95 * <--------float_window-------------------->
99 * <-TOLERANCE-|------float_window--------------|-TOLERANCE->
100 * <--------bin_window-------------------->
103 static void gtk_handle_box_class_init (GtkHandleBoxClass *klass);
104 static void gtk_handle_box_init (GtkHandleBox *handle_box);
105 static void gtk_handle_box_set_property (GObject *object,
109 static void gtk_handle_box_get_property (GObject *object,
113 static void gtk_handle_box_destroy (GtkObject *object);
114 static void gtk_handle_box_map (GtkWidget *widget);
115 static void gtk_handle_box_unmap (GtkWidget *widget);
116 static void gtk_handle_box_realize (GtkWidget *widget);
117 static void gtk_handle_box_unrealize (GtkWidget *widget);
118 static void gtk_handle_box_style_set (GtkWidget *widget,
119 GtkStyle *previous_style);
120 static void gtk_handle_box_size_request (GtkWidget *widget,
121 GtkRequisition *requisition);
122 static void gtk_handle_box_size_allocate (GtkWidget *widget,
123 GtkAllocation *real_allocation);
124 static void gtk_handle_box_add (GtkContainer *container,
126 static void gtk_handle_box_remove (GtkContainer *container,
128 static void gtk_handle_box_draw_ghost (GtkHandleBox *hb);
129 static void gtk_handle_box_paint (GtkWidget *widget,
130 GdkEventExpose *event,
132 static gint gtk_handle_box_expose (GtkWidget *widget,
133 GdkEventExpose *event);
134 static gint gtk_handle_box_button_changed (GtkWidget *widget,
135 GdkEventButton *event);
136 static gint gtk_handle_box_motion (GtkWidget *widget,
137 GdkEventMotion *event);
138 static gint gtk_handle_box_delete_event (GtkWidget *widget,
140 static void gtk_handle_box_reattach (GtkHandleBox *hb);
143 static GtkBinClass *parent_class;
144 static guint handle_box_signals[SIGNAL_LAST] = { 0 };
148 gtk_handle_box_get_type (void)
150 static GType handle_box_type = 0;
152 if (!handle_box_type)
154 static const GTypeInfo handle_box_info =
156 sizeof (GtkHandleBoxClass),
157 NULL, /* base_init */
158 NULL, /* base_finalize */
159 (GClassInitFunc) gtk_handle_box_class_init,
160 NULL, /* class_finalize */
161 NULL, /* class_data */
162 sizeof (GtkHandleBox),
164 (GInstanceInitFunc) gtk_handle_box_init,
167 handle_box_type = g_type_register_static (GTK_TYPE_BIN, "GtkHandleBox",
168 &handle_box_info, 0);
171 return handle_box_type;
175 gtk_handle_box_class_init (GtkHandleBoxClass *class)
177 GObjectClass *gobject_class;
178 GtkObjectClass *object_class;
179 GtkWidgetClass *widget_class;
180 GtkContainerClass *container_class;
182 gobject_class = (GObjectClass *) class;
183 object_class = (GtkObjectClass *) class;
184 widget_class = (GtkWidgetClass *) class;
185 container_class = (GtkContainerClass *) class;
187 parent_class = g_type_class_peek_parent (class);
189 gobject_class->set_property = gtk_handle_box_set_property;
190 gobject_class->get_property = gtk_handle_box_get_property;
192 g_object_class_install_property (gobject_class,
194 g_param_spec_enum ("shadow", NULL,
195 _("Deprecated property, use shadow_type instead"),
196 GTK_TYPE_SHADOW_TYPE,
197 GTK_SHADOW_ETCHED_OUT,
198 G_PARAM_READABLE | G_PARAM_WRITABLE));
199 g_object_class_install_property (gobject_class,
201 g_param_spec_enum ("shadow_type",
203 _("Appearance of the shadow that surrounds the container"),
204 GTK_TYPE_SHADOW_TYPE,
205 GTK_SHADOW_ETCHED_OUT,
206 G_PARAM_READABLE | G_PARAM_WRITABLE));
208 g_object_class_install_property (gobject_class,
209 PROP_HANDLE_POSITION,
210 g_param_spec_enum ("handle_position",
211 _("Handle position"),
212 _("Position of the handle relative to the child widget"),
213 GTK_TYPE_POSITION_TYPE,
215 G_PARAM_READABLE | G_PARAM_WRITABLE));
217 g_object_class_install_property (gobject_class,
219 g_param_spec_enum ("snap_edge",
221 _("Side of the handlebox that's lined up with the docking point to dock the handlebox"),
222 GTK_TYPE_POSITION_TYPE,
224 G_PARAM_READABLE | G_PARAM_WRITABLE));
226 g_object_class_install_property (gobject_class,
228 g_param_spec_boolean ("snap_edge_set",
230 _("Whether to use the value from the snap_edge property or a value derived from handle_position"),
232 G_PARAM_READABLE | G_PARAM_WRITABLE));
234 object_class->destroy = gtk_handle_box_destroy;
236 widget_class->map = gtk_handle_box_map;
237 widget_class->unmap = gtk_handle_box_unmap;
238 widget_class->realize = gtk_handle_box_realize;
239 widget_class->unrealize = gtk_handle_box_unrealize;
240 widget_class->style_set = gtk_handle_box_style_set;
241 widget_class->size_request = gtk_handle_box_size_request;
242 widget_class->size_allocate = gtk_handle_box_size_allocate;
243 widget_class->expose_event = gtk_handle_box_expose;
244 widget_class->button_press_event = gtk_handle_box_button_changed;
245 widget_class->button_release_event = gtk_handle_box_button_changed;
246 widget_class->motion_notify_event = gtk_handle_box_motion;
247 widget_class->delete_event = gtk_handle_box_delete_event;
249 container_class->add = gtk_handle_box_add;
250 container_class->remove = gtk_handle_box_remove;
252 class->child_attached = NULL;
253 class->child_detached = NULL;
255 handle_box_signals[SIGNAL_CHILD_ATTACHED] =
256 g_signal_new ("child_attached",
257 G_OBJECT_CLASS_TYPE (gobject_class),
259 G_STRUCT_OFFSET (GtkHandleBoxClass, child_attached),
261 _gtk_marshal_VOID__OBJECT,
264 handle_box_signals[SIGNAL_CHILD_DETACHED] =
265 g_signal_new ("child_detached",
266 G_OBJECT_CLASS_TYPE (gobject_class),
268 G_STRUCT_OFFSET (GtkHandleBoxClass, child_detached),
270 _gtk_marshal_VOID__OBJECT,
275 GtkHandleBoxPrivate *
276 gtk_handle_box_get_private (GtkHandleBox *hb)
278 GtkHandleBoxPrivate *private;
279 static GQuark private_quark = 0;
282 private_quark = g_quark_from_static_string ("gtk-handle-box-private");
284 private = g_object_get_qdata (G_OBJECT (hb), private_quark);
288 private = g_new0 (GtkHandleBoxPrivate, 1);
289 g_object_set_qdata_full (G_OBJECT (hb), private_quark,
297 gtk_handle_box_init (GtkHandleBox *handle_box)
299 GTK_WIDGET_UNSET_FLAGS (handle_box, GTK_NO_WINDOW);
301 handle_box->bin_window = NULL;
302 handle_box->float_window = NULL;
303 handle_box->shadow_type = GTK_SHADOW_OUT;
304 handle_box->handle_position = GTK_POS_LEFT;
305 handle_box->float_window_mapped = FALSE;
306 handle_box->child_detached = FALSE;
307 handle_box->in_drag = FALSE;
308 handle_box->shrink_on_detach = TRUE;
309 handle_box->snap_edge = -1;
313 gtk_handle_box_set_property (GObject *object,
318 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
323 case PROP_SHADOW_TYPE:
324 gtk_handle_box_set_shadow_type (handle_box, g_value_get_enum (value));
326 case PROP_HANDLE_POSITION:
327 gtk_handle_box_set_handle_position (handle_box, g_value_get_enum (value));
330 gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value));
332 case PROP_SNAP_EDGE_SET:
333 if (!g_value_get_boolean (value))
334 gtk_handle_box_set_snap_edge (handle_box, (GtkPositionType)-1);
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
343 gtk_handle_box_get_property (GObject *object,
348 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
353 case PROP_SHADOW_TYPE:
354 g_value_set_enum (value, handle_box->shadow_type);
356 case PROP_HANDLE_POSITION:
357 g_value_set_enum (value, handle_box->handle_position);
360 g_value_set_enum (value,
361 (handle_box->snap_edge == -1 ?
362 GTK_POS_TOP : handle_box->snap_edge));
364 case PROP_SNAP_EDGE_SET:
365 g_value_set_boolean (value, handle_box->snap_edge != -1);
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374 gtk_handle_box_new (void)
376 return g_object_new (GTK_TYPE_HANDLE_BOX, NULL);
380 gtk_handle_box_destroy (GtkObject *object)
382 if (GTK_OBJECT_CLASS (parent_class)->destroy)
383 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
387 gtk_handle_box_map (GtkWidget *widget)
392 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
394 bin = GTK_BIN (widget);
395 hb = GTK_HANDLE_BOX (widget);
398 GTK_WIDGET_VISIBLE (bin->child) &&
399 !GTK_WIDGET_MAPPED (bin->child))
400 gtk_widget_map (bin->child);
402 if (hb->child_detached && !hb->float_window_mapped)
404 gdk_window_show (hb->float_window);
405 hb->float_window_mapped = TRUE;
408 gdk_window_show (hb->bin_window);
409 gdk_window_show (widget->window);
413 gtk_handle_box_unmap (GtkWidget *widget)
417 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
419 hb = GTK_HANDLE_BOX (widget);
421 gdk_window_hide (widget->window);
422 if (hb->float_window_mapped)
424 gdk_window_hide (hb->float_window);
425 hb->float_window_mapped = FALSE;
430 gtk_handle_box_realize (GtkWidget *widget)
432 GdkWindowAttr attributes;
433 gint attributes_mask;
436 hb = GTK_HANDLE_BOX (widget);
438 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
440 attributes.x = widget->allocation.x;
441 attributes.y = widget->allocation.y;
442 attributes.width = widget->allocation.width;
443 attributes.height = widget->allocation.height;
444 attributes.window_type = GDK_WINDOW_CHILD;
445 attributes.wclass = GDK_INPUT_OUTPUT;
446 attributes.visual = gtk_widget_get_visual (widget);
447 attributes.colormap = gtk_widget_get_colormap (widget);
448 attributes.event_mask = (gtk_widget_get_events (widget)
449 | GDK_EXPOSURE_MASK);
450 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
451 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
452 gdk_window_set_user_data (widget->window, widget);
456 attributes.width = widget->allocation.width;
457 attributes.height = widget->allocation.height;
458 attributes.window_type = GDK_WINDOW_CHILD;
459 attributes.event_mask = (gtk_widget_get_events (widget) |
461 GDK_BUTTON1_MOTION_MASK |
462 GDK_POINTER_MOTION_HINT_MASK |
463 GDK_BUTTON_PRESS_MASK |
464 GDK_BUTTON_RELEASE_MASK);
465 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
466 hb->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask);
467 gdk_window_set_user_data (hb->bin_window, widget);
468 if (GTK_BIN (hb)->child)
469 gtk_widget_set_parent_window (GTK_BIN (hb)->child, hb->bin_window);
473 attributes.width = widget->requisition.width;
474 attributes.height = widget->requisition.height;
475 attributes.window_type = GDK_WINDOW_TOPLEVEL;
476 attributes.wclass = GDK_INPUT_OUTPUT;
477 attributes.visual = gtk_widget_get_visual (widget);
478 attributes.colormap = gtk_widget_get_colormap (widget);
479 attributes.event_mask = (gtk_widget_get_events (widget) |
481 GDK_ENTER_NOTIFY_MASK |
482 GDK_LEAVE_NOTIFY_MASK |
483 GDK_FOCUS_CHANGE_MASK |
485 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
486 hb->float_window = gdk_window_new (gtk_widget_get_root_window (widget),
487 &attributes, attributes_mask);
488 gdk_window_set_user_data (hb->float_window, widget);
489 gdk_window_set_decorations (hb->float_window, 0);
490 gdk_window_set_type_hint (hb->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
492 widget->style = gtk_style_attach (widget->style, widget->window);
493 gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (hb));
494 gtk_style_set_background (widget->style, hb->bin_window, GTK_WIDGET_STATE (hb));
495 gtk_style_set_background (widget->style, hb->float_window, GTK_WIDGET_STATE (hb));
496 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
500 gtk_handle_box_unrealize (GtkWidget *widget)
502 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
504 gdk_window_set_user_data (hb->bin_window, NULL);
505 gdk_window_destroy (hb->bin_window);
506 hb->bin_window = NULL;
507 gdk_window_set_user_data (hb->float_window, NULL);
508 gdk_window_destroy (hb->float_window);
509 hb->float_window = NULL;
511 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
512 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
516 gtk_handle_box_style_set (GtkWidget *widget,
517 GtkStyle *previous_style)
519 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
521 if (GTK_WIDGET_REALIZED (widget) &&
522 !GTK_WIDGET_NO_WINDOW (widget))
524 gtk_style_set_background (widget->style, widget->window,
526 gtk_style_set_background (widget->style, hb->bin_window, widget->state);
527 gtk_style_set_background (widget->style, hb->float_window, widget->state);
532 effective_handle_position (GtkHandleBox *hb)
536 if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
537 handle_position = hb->handle_position;
540 switch (hb->handle_position)
543 handle_position = GTK_POS_RIGHT;
546 handle_position = GTK_POS_LEFT;
549 handle_position = hb->handle_position;
554 return handle_position;
558 gtk_handle_box_size_request (GtkWidget *widget,
559 GtkRequisition *requisition)
563 GtkRequisition child_requisition;
564 gint handle_position;
566 bin = GTK_BIN (widget);
567 hb = GTK_HANDLE_BOX (widget);
569 handle_position = effective_handle_position (hb);
571 if (handle_position == GTK_POS_LEFT ||
572 handle_position == GTK_POS_RIGHT)
574 requisition->width = DRAG_HANDLE_SIZE;
575 requisition->height = 0;
579 requisition->width = 0;
580 requisition->height = DRAG_HANDLE_SIZE;
583 /* if our child is not visible, we still request its size, since we
584 * won't have any usefull hint for our size otherwise.
587 gtk_widget_size_request (bin->child, &child_requisition);
590 child_requisition.width = 0;
591 child_requisition.height = 0;
594 if (hb->child_detached)
596 /* FIXME: This doesn't work currently */
597 if (!hb->shrink_on_detach)
599 if (handle_position == GTK_POS_LEFT ||
600 handle_position == GTK_POS_RIGHT)
601 requisition->height += child_requisition.height;
603 requisition->width += child_requisition.width;
607 if (handle_position == GTK_POS_LEFT ||
608 handle_position == GTK_POS_RIGHT)
609 requisition->height += widget->style->ythickness;
611 requisition->width += widget->style->xthickness;
616 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
617 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
621 requisition->width += child_requisition.width;
622 requisition->height += child_requisition.height;
626 requisition->width += CHILDLESS_SIZE;
627 requisition->height += CHILDLESS_SIZE;
633 gtk_handle_box_size_allocate (GtkWidget *widget,
634 GtkAllocation *allocation)
638 GtkRequisition child_requisition;
639 gint handle_position;
641 bin = GTK_BIN (widget);
642 hb = GTK_HANDLE_BOX (widget);
644 handle_position = effective_handle_position (hb);
647 gtk_widget_get_child_requisition (bin->child, &child_requisition);
650 child_requisition.width = 0;
651 child_requisition.height = 0;
654 widget->allocation = *allocation;
656 if (GTK_WIDGET_REALIZED (hb))
657 gdk_window_move_resize (widget->window,
658 widget->allocation.x,
659 widget->allocation.y,
660 widget->allocation.width,
661 widget->allocation.height);
664 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
667 GtkAllocation child_allocation;
671 border_width = GTK_CONTAINER (widget)->border_width;
673 child_allocation.x = border_width;
674 child_allocation.y = border_width;
675 if (handle_position == GTK_POS_LEFT)
676 child_allocation.x += DRAG_HANDLE_SIZE;
677 else if (handle_position == GTK_POS_TOP)
678 child_allocation.y += DRAG_HANDLE_SIZE;
680 if (hb->child_detached)
685 child_allocation.width = child_requisition.width;
686 child_allocation.height = child_requisition.height;
688 float_width = child_allocation.width + 2 * border_width;
689 float_height = child_allocation.height + 2 * border_width;
691 if (handle_position == GTK_POS_LEFT ||
692 handle_position == GTK_POS_RIGHT)
693 float_width += DRAG_HANDLE_SIZE;
695 float_height += DRAG_HANDLE_SIZE;
697 if (GTK_WIDGET_REALIZED (hb))
699 gdk_window_resize (hb->float_window,
702 gdk_window_move_resize (hb->bin_window,
711 child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
712 child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
714 if (handle_position == GTK_POS_LEFT ||
715 handle_position == GTK_POS_RIGHT)
716 child_allocation.width -= DRAG_HANDLE_SIZE;
718 child_allocation.height -= DRAG_HANDLE_SIZE;
720 if (GTK_WIDGET_REALIZED (hb))
721 gdk_window_move_resize (hb->bin_window,
724 widget->allocation.width,
725 widget->allocation.height);
728 gtk_widget_size_allocate (bin->child, &child_allocation);
733 gtk_handle_box_draw_ghost (GtkHandleBox *hb)
740 gint handle_position;
742 widget = GTK_WIDGET (hb);
744 handle_position = effective_handle_position (hb);
745 if (handle_position == GTK_POS_LEFT ||
746 handle_position == GTK_POS_RIGHT)
748 x = handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
750 width = DRAG_HANDLE_SIZE;
751 height = widget->allocation.height;
756 y = handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
757 width = widget->allocation.width;
758 height = DRAG_HANDLE_SIZE;
760 gtk_paint_shadow (widget->style,
762 GTK_WIDGET_STATE (widget),
763 GTK_SHADOW_ETCHED_IN,
764 NULL, widget, "handle",
769 if (handle_position == GTK_POS_LEFT ||
770 handle_position == GTK_POS_RIGHT)
771 gtk_paint_hline (widget->style,
773 GTK_WIDGET_STATE (widget),
774 NULL, widget, "handlebox",
775 handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
776 handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
777 widget->allocation.height / 2);
779 gtk_paint_vline (widget->style,
781 GTK_WIDGET_STATE (widget),
782 NULL, widget, "handlebox",
783 handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
784 handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
785 widget->allocation.width / 2);
789 draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
792 gtk_paint_handle (widget->style, window, GTK_STATE_NORMAL, shadow,
793 clip, widget, "handlebox",
794 rect->x, rect->y, rect->width, rect->height,
795 GTK_ORIENTATION_VERTICAL);
799 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
802 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
804 if ((GtkShadowType) handle_box->shadow_type != type)
806 handle_box->shadow_type = type;
807 g_object_notify (G_OBJECT (handle_box), "shadow_type");
808 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
813 * gtk_handle_box_get_shadow_type:
814 * @handle_box: a #GtkHandleBox
816 * Gets the type of shadow drawn around the handle box. See
817 * gtk_handle_box_set_shadow_type().
819 * Return value: the type of shadow currently drawn around the handle box.
822 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
824 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
826 return handle_box->shadow_type;
830 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
831 GtkPositionType position)
833 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
835 if ((GtkPositionType) handle_box->handle_position != position)
837 handle_box->handle_position = position;
838 g_object_notify (G_OBJECT (handle_box), "handle_position");
839 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
844 * gtk_handle_box_get_handle_position:
845 * @handle_box: a #GtkHandleBox
847 * Gets the handle position of the handle box. See
848 * gtk_handle_box_set_handle_position().
850 * Return value: the current handle position.
853 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
855 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
857 return handle_box->handle_position;
861 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
862 GtkPositionType edge)
864 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
866 if (handle_box->snap_edge != edge)
868 handle_box->snap_edge = edge;
870 g_object_freeze_notify (G_OBJECT (handle_box));
871 g_object_notify (G_OBJECT (handle_box), "snap_edge");
872 g_object_notify (G_OBJECT (handle_box), "snap_edge_set");
873 g_object_thaw_notify (G_OBJECT (handle_box));
878 * gtk_handle_box_get_snap_edge:
879 * @handle_box: a #GtkHandleBox
881 * Gets the edge used for determining reattachment of the handle box. See
882 * gtk_handle_box_set_snap_edge().
884 * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
885 * is determined (as per default) from the handle position.
888 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
890 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
892 return handle_box->snap_edge;
896 gtk_handle_box_paint (GtkWidget *widget,
898 GdkEventExpose *event,
907 gint handle_position;
909 bin = GTK_BIN (widget);
910 hb = GTK_HANDLE_BOX (widget);
912 handle_position = effective_handle_position (hb);
914 gdk_drawable_get_size (hb->bin_window, &width, &height);
917 gtk_paint_box (widget->style,
919 GTK_WIDGET_STATE (widget),
921 area, widget, "handlebox_bin",
924 gtk_paint_box (widget->style,
926 GTK_WIDGET_STATE (widget),
928 &event->area, widget, "handlebox_bin",
931 /* We currently draw the handle _above_ the relief of the handlebox.
932 * it could also be drawn on the same level...
934 hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
935 hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
939 switch (handle_position)
944 rect.width = DRAG_HANDLE_SIZE;
945 rect.height = height;
948 rect.x = width - DRAG_HANDLE_SIZE;
950 rect.width = DRAG_HANDLE_SIZE;
951 rect.height = height;
957 rect.height = DRAG_HANDLE_SIZE;
961 rect.y = height - DRAG_HANDLE_SIZE;
963 rect.height = DRAG_HANDLE_SIZE;
967 if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
968 draw_textured_frame (widget, hb->bin_window, &rect,
970 event ? &event->area : area);
972 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
973 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
977 gtk_handle_box_expose (GtkWidget *widget,
978 GdkEventExpose *event)
982 if (GTK_WIDGET_DRAWABLE (widget))
984 hb = GTK_HANDLE_BOX (widget);
986 if (event->window == widget->window)
988 if (hb->child_detached)
989 gtk_handle_box_draw_ghost (hb);
992 gtk_handle_box_paint (widget, event, NULL);
999 gtk_handle_box_button_changed (GtkWidget *widget,
1000 GdkEventButton *event)
1003 gboolean event_handled;
1005 gint handle_position;
1007 hb = GTK_HANDLE_BOX (widget);
1009 handle_position = effective_handle_position (hb);
1011 event_handled = FALSE;
1012 if ((event->button == 1) &&
1013 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1018 if (event->window != hb->bin_window)
1021 child = GTK_BIN (hb)->child;
1025 switch (handle_position)
1028 in_handle = event->x < DRAG_HANDLE_SIZE;
1031 in_handle = event->y < DRAG_HANDLE_SIZE;
1034 in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width;
1036 case GTK_POS_BOTTOM:
1037 in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height;
1047 event_handled = TRUE;
1052 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
1054 GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1055 gint desk_x, desk_y;
1056 gint root_x, root_y;
1059 gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y);
1060 gdk_window_get_origin (hb->bin_window, &root_x, &root_y);
1061 gdk_drawable_get_size (hb->bin_window, &width, &height);
1063 private->orig_x = event->x_root;
1064 private->orig_y = event->y_root;
1066 hb->float_allocation.x = root_x - event->x_root;
1067 hb->float_allocation.y = root_y - event->y_root;
1068 hb->float_allocation.width = width;
1069 hb->float_allocation.height = height;
1071 hb->deskoff_x = desk_x - root_x;
1072 hb->deskoff_y = desk_y - root_y;
1074 gdk_window_get_origin (widget->window, &root_x, &root_y);
1075 gdk_drawable_get_size (widget->window, &width, &height);
1077 hb->attach_allocation.x = root_x;
1078 hb->attach_allocation.y = root_y;
1079 hb->attach_allocation.width = width;
1080 hb->attach_allocation.height = height;
1083 fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1085 if (gdk_pointer_grab (widget->window,
1087 (GDK_BUTTON1_MOTION_MASK |
1088 GDK_POINTER_MOTION_HINT_MASK |
1089 GDK_BUTTON_RELEASE_MASK),
1092 GDK_CURRENT_TIME) != 0)
1094 hb->in_drag = FALSE;
1097 gdk_cursor_unref (fleur);
1098 event_handled = TRUE;
1100 else if (hb->child_detached) /* Double click */
1102 gtk_handle_box_reattach (hb);
1106 else if (event->type == GDK_BUTTON_RELEASE &&
1109 if (event->window != widget->window)
1112 gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
1114 hb->in_drag = FALSE;
1115 event_handled = TRUE;
1118 return event_handled;
1122 gtk_handle_box_motion (GtkWidget *widget,
1123 GdkEventMotion *event)
1128 gboolean is_snapped = FALSE;
1129 gint handle_position;
1130 GdkGeometry geometry;
1131 GdkScreen *screen, *pointer_screen;
1133 hb = GTK_HANDLE_BOX (widget);
1137 if (!hb->in_drag || (event->window != widget->window))
1140 handle_position = effective_handle_position (hb);
1142 /* Calculate the attachment point on the float, if the float
1147 screen = gtk_widget_get_screen (widget);
1148 gdk_display_get_pointer (gdk_screen_get_display (screen),
1150 &new_x, &new_y, NULL);
1151 if (pointer_screen != screen)
1153 GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1155 new_x = private->orig_x;
1156 new_y = private->orig_y;
1159 new_x += hb->float_allocation.x;
1160 new_y += hb->float_allocation.y;
1162 snap_edge = hb->snap_edge;
1163 if (snap_edge == -1)
1164 snap_edge = (handle_position == GTK_POS_LEFT ||
1165 handle_position == GTK_POS_RIGHT) ?
1166 GTK_POS_TOP : GTK_POS_LEFT;
1168 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1172 snap_edge = GTK_POS_RIGHT;
1175 snap_edge = GTK_POS_LEFT;
1181 /* First, check if the snapped edge is aligned
1186 is_snapped = abs (hb->attach_allocation.y - new_y) < TOLERANCE;
1188 case GTK_POS_BOTTOM:
1189 is_snapped = abs (hb->attach_allocation.y + (gint)hb->attach_allocation.height -
1190 new_y - (gint)hb->float_allocation.height) < TOLERANCE;
1193 is_snapped = abs (hb->attach_allocation.x - new_x) < TOLERANCE;
1196 is_snapped = abs (hb->attach_allocation.x + (gint)hb->attach_allocation.width -
1197 new_x - (gint)hb->float_allocation.width) < TOLERANCE;
1201 /* Next, check if coordinates in the other direction are sufficiently
1206 gint float_pos1 = 0; /* Initialize to suppress warnings */
1207 gint float_pos2 = 0;
1208 gint attach_pos1 = 0;
1209 gint attach_pos2 = 0;
1214 case GTK_POS_BOTTOM:
1215 attach_pos1 = hb->attach_allocation.x;
1216 attach_pos2 = hb->attach_allocation.x + hb->attach_allocation.width;
1218 float_pos2 = new_x + hb->float_allocation.width;
1222 attach_pos1 = hb->attach_allocation.y;
1223 attach_pos2 = hb->attach_allocation.y + hb->attach_allocation.height;
1225 float_pos2 = new_y + hb->float_allocation.height;
1229 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1230 (attach_pos2 + TOLERANCE > float_pos2)) ||
1231 ((float_pos1 - TOLERANCE < attach_pos1) &&
1232 (float_pos2 + TOLERANCE > attach_pos2));
1237 if (hb->child_detached)
1239 hb->child_detached = FALSE;
1240 gdk_window_hide (hb->float_window);
1241 gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1242 hb->float_window_mapped = FALSE;
1244 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1246 GTK_BIN (hb)->child);
1248 gtk_widget_queue_resize (widget);
1255 gdk_drawable_get_size (hb->float_window, &width, &height);
1256 new_x += hb->deskoff_x;
1257 new_y += hb->deskoff_y;
1259 switch (handle_position)
1262 new_y += ((gint)hb->float_allocation.height - height) / 2;
1265 new_x += (gint)hb->float_allocation.width - width;
1266 new_y += ((gint)hb->float_allocation.height - height) / 2;
1269 new_x += ((gint)hb->float_allocation.width - width) / 2;
1271 case GTK_POS_BOTTOM:
1272 new_x += ((gint)hb->float_allocation.width - width) / 2;
1273 new_y += (gint)hb->float_allocation.height - height;
1277 if (hb->child_detached)
1279 gdk_window_move (hb->float_window, new_x, new_y);
1280 gdk_window_raise (hb->float_window);
1286 GtkRequisition child_requisition;
1288 hb->child_detached = TRUE;
1290 if (GTK_BIN (hb)->child)
1291 gtk_widget_get_child_requisition (GTK_BIN (hb)->child, &child_requisition);
1294 child_requisition.width = 0;
1295 child_requisition.height = 0;
1298 width = child_requisition.width + 2 * GTK_CONTAINER (hb)->border_width;
1299 height = child_requisition.height + 2 * GTK_CONTAINER (hb)->border_width;
1301 if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT)
1302 width += DRAG_HANDLE_SIZE;
1304 height += DRAG_HANDLE_SIZE;
1306 gdk_window_move_resize (hb->float_window, new_x, new_y, width, height);
1307 gdk_window_reparent (hb->bin_window, hb->float_window, 0, 0);
1308 gdk_window_set_geometry_hints (hb->float_window, &geometry, GDK_HINT_POS);
1309 gdk_window_show (hb->float_window);
1310 hb->float_window_mapped = TRUE;
1312 /* this extra move is neccessary if we use decorations, or our
1313 * window manager insists on decorations.
1315 gdk_display_sync (gtk_widget_get_display (widget));
1316 gdk_window_move (hb->float_window, new_x, new_y);
1317 gdk_display_sync (gtk_widget_get_display (widget));
1320 handle_box_signals[SIGNAL_CHILD_DETACHED],
1322 GTK_BIN (hb)->child);
1323 gtk_handle_box_draw_ghost (hb);
1325 gtk_widget_queue_resize (widget);
1333 gtk_handle_box_add (GtkContainer *container,
1336 gtk_widget_set_parent_window (widget, GTK_HANDLE_BOX (container)->bin_window);
1337 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
1341 gtk_handle_box_remove (GtkContainer *container,
1344 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1346 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1350 gtk_handle_box_delete_event (GtkWidget *widget,
1353 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1355 if (event->window == hb->float_window)
1357 gtk_handle_box_reattach (hb);
1366 gtk_handle_box_reattach (GtkHandleBox *hb)
1368 GtkWidget *widget = GTK_WIDGET (hb);
1370 if (hb->child_detached)
1372 hb->child_detached = FALSE;
1373 if (GTK_WIDGET_REALIZED (hb))
1375 gdk_window_hide (hb->float_window);
1376 gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1378 if (GTK_BIN (hb)->child)
1380 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1382 GTK_BIN (hb)->child);
1385 hb->float_window_mapped = FALSE;
1389 gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (hb)),
1391 hb->in_drag = FALSE;
1394 gtk_widget_queue_resize (GTK_WIDGET (hb));