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/.
30 #include "gtkhandlebox.h"
31 #include "gtkinvisible.h"
33 #include "gtkmarshalers.h"
34 #include "gtkwindow.h"
35 #include "gtkprivate.h"
39 typedef struct _GtkHandleBoxPrivate GtkHandleBoxPrivate;
41 struct _GtkHandleBoxPrivate
57 #define DRAG_HANDLE_SIZE 10
58 #define CHILDLESS_SIZE 25
59 #define GHOST_HEIGHT 3
63 SIGNAL_CHILD_ATTACHED,
64 SIGNAL_CHILD_DETACHED,
68 /* The algorithm for docking and redocking implemented here
69 * has a couple of nice properties:
71 * 1) During a single drag, docking always occurs at the
72 * the same cursor position. This means that the users
73 * motions are reversible, and that you won't
74 * undock/dock oscillations.
76 * 2) Docking generally occurs at user-visible features.
77 * The user, once they figure out to redock, will
78 * have useful information about doing it again in
81 * Please try to preserve these properties if you
82 * change the algorithm. (And the current algorithm
83 * is far from ideal). Briefly, the current algorithm
84 * for deciding whether the handlebox is docked or not:
86 * 1) The decision is done by comparing two rectangles - the
87 * allocation if the widget at the start of the drag,
88 * and the boundary of hb->bin_window at the start of
89 * of the drag offset by the distance that the cursor
92 * 2) These rectangles must have one edge, the "snap_edge"
93 * of the handlebox, aligned within TOLERANCE.
95 * 3) On the other dimension, the extents of one rectangle
96 * must be contained in the extents of the other,
97 * extended by tolerance. That is, either we can have:
99 * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE->
100 * <--------float_window-------------------->
104 * <-TOLERANCE-|------float_window--------------|-TOLERANCE->
105 * <--------bin_window-------------------->
108 static void gtk_handle_box_set_property (GObject *object,
112 static void gtk_handle_box_get_property (GObject *object,
116 static void gtk_handle_box_map (GtkWidget *widget);
117 static void gtk_handle_box_unmap (GtkWidget *widget);
118 static void gtk_handle_box_realize (GtkWidget *widget);
119 static void gtk_handle_box_unrealize (GtkWidget *widget);
120 static void gtk_handle_box_style_set (GtkWidget *widget,
121 GtkStyle *previous_style);
122 static void gtk_handle_box_size_request (GtkWidget *widget,
123 GtkRequisition *requisition);
124 static void gtk_handle_box_size_allocate (GtkWidget *widget,
125 GtkAllocation *real_allocation);
126 static void gtk_handle_box_add (GtkContainer *container,
128 static void gtk_handle_box_remove (GtkContainer *container,
130 static void gtk_handle_box_draw_ghost (GtkHandleBox *hb);
131 static void gtk_handle_box_paint (GtkWidget *widget,
132 GdkEventExpose *event,
134 static gboolean gtk_handle_box_expose (GtkWidget *widget,
135 GdkEventExpose *event);
136 static gboolean gtk_handle_box_button_press (GtkWidget *widget,
137 GdkEventButton *event);
138 static gboolean gtk_handle_box_motion (GtkWidget *widget,
139 GdkEventMotion *event);
140 static gboolean gtk_handle_box_delete_event (GtkWidget *widget,
142 static void gtk_handle_box_reattach (GtkHandleBox *hb);
143 static void gtk_handle_box_end_drag (GtkHandleBox *hb,
146 static guint handle_box_signals[SIGNAL_LAST] = { 0 };
148 G_DEFINE_TYPE (GtkHandleBox, gtk_handle_box, GTK_TYPE_BIN)
151 gtk_handle_box_class_init (GtkHandleBoxClass *class)
153 GObjectClass *gobject_class;
154 GtkWidgetClass *widget_class;
155 GtkContainerClass *container_class;
157 gobject_class = (GObjectClass *) class;
158 widget_class = (GtkWidgetClass *) class;
159 container_class = (GtkContainerClass *) class;
161 gobject_class->set_property = gtk_handle_box_set_property;
162 gobject_class->get_property = gtk_handle_box_get_property;
164 g_object_class_install_property (gobject_class,
166 g_param_spec_enum ("shadow", NULL,
167 P_("Deprecated property, use shadow_type instead"),
168 GTK_TYPE_SHADOW_TYPE,
170 GTK_PARAM_READWRITE));
171 g_object_class_install_property (gobject_class,
173 g_param_spec_enum ("shadow-type",
175 P_("Appearance of the shadow that surrounds the container"),
176 GTK_TYPE_SHADOW_TYPE,
178 GTK_PARAM_READWRITE));
180 g_object_class_install_property (gobject_class,
181 PROP_HANDLE_POSITION,
182 g_param_spec_enum ("handle-position",
183 P_("Handle position"),
184 P_("Position of the handle relative to the child widget"),
185 GTK_TYPE_POSITION_TYPE,
187 GTK_PARAM_READWRITE));
189 g_object_class_install_property (gobject_class,
191 g_param_spec_enum ("snap-edge",
193 P_("Side of the handlebox that's lined up with the docking point to dock the handlebox"),
194 GTK_TYPE_POSITION_TYPE,
196 GTK_PARAM_READWRITE));
198 g_object_class_install_property (gobject_class,
200 g_param_spec_boolean ("snap-edge-set",
202 P_("Whether to use the value from the snap_edge property or a value derived from handle_position"),
204 GTK_PARAM_READWRITE));
206 g_object_class_install_property (gobject_class,
208 g_param_spec_boolean ("child-detached",
209 P_("Child Detached"),
210 P_("A boolean value indicating whether the handlebox's child is attached or detached."),
212 GTK_PARAM_READABLE));
214 widget_class->map = gtk_handle_box_map;
215 widget_class->unmap = gtk_handle_box_unmap;
216 widget_class->realize = gtk_handle_box_realize;
217 widget_class->unrealize = gtk_handle_box_unrealize;
218 widget_class->style_set = gtk_handle_box_style_set;
219 widget_class->size_request = gtk_handle_box_size_request;
220 widget_class->size_allocate = gtk_handle_box_size_allocate;
221 widget_class->expose_event = gtk_handle_box_expose;
222 widget_class->button_press_event = gtk_handle_box_button_press;
223 widget_class->delete_event = gtk_handle_box_delete_event;
225 container_class->add = gtk_handle_box_add;
226 container_class->remove = gtk_handle_box_remove;
228 class->child_attached = NULL;
229 class->child_detached = NULL;
231 handle_box_signals[SIGNAL_CHILD_ATTACHED] =
232 g_signal_new (I_("child-attached"),
233 G_OBJECT_CLASS_TYPE (gobject_class),
235 G_STRUCT_OFFSET (GtkHandleBoxClass, child_attached),
237 _gtk_marshal_VOID__OBJECT,
240 handle_box_signals[SIGNAL_CHILD_DETACHED] =
241 g_signal_new (I_("child-detached"),
242 G_OBJECT_CLASS_TYPE (gobject_class),
244 G_STRUCT_OFFSET (GtkHandleBoxClass, child_detached),
246 _gtk_marshal_VOID__OBJECT,
250 g_type_class_add_private (gobject_class, sizeof (GtkHandleBoxPrivate));
253 static GtkHandleBoxPrivate *
254 gtk_handle_box_get_private (GtkHandleBox *hb)
256 return G_TYPE_INSTANCE_GET_PRIVATE (hb, GTK_TYPE_HANDLE_BOX, GtkHandleBoxPrivate);
260 gtk_handle_box_init (GtkHandleBox *handle_box)
262 GTK_WIDGET_UNSET_FLAGS (handle_box, GTK_NO_WINDOW);
264 handle_box->bin_window = NULL;
265 handle_box->float_window = NULL;
266 handle_box->shadow_type = GTK_SHADOW_OUT;
267 handle_box->handle_position = GTK_POS_LEFT;
268 handle_box->float_window_mapped = FALSE;
269 handle_box->child_detached = FALSE;
270 handle_box->in_drag = FALSE;
271 handle_box->shrink_on_detach = TRUE;
272 handle_box->snap_edge = -1;
276 gtk_handle_box_set_property (GObject *object,
281 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
286 case PROP_SHADOW_TYPE:
287 gtk_handle_box_set_shadow_type (handle_box, g_value_get_enum (value));
289 case PROP_HANDLE_POSITION:
290 gtk_handle_box_set_handle_position (handle_box, g_value_get_enum (value));
293 gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value));
295 case PROP_SNAP_EDGE_SET:
296 if (!g_value_get_boolean (value))
297 gtk_handle_box_set_snap_edge (handle_box, (GtkPositionType)-1);
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306 gtk_handle_box_get_property (GObject *object,
311 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
316 case PROP_SHADOW_TYPE:
317 g_value_set_enum (value, handle_box->shadow_type);
319 case PROP_HANDLE_POSITION:
320 g_value_set_enum (value, handle_box->handle_position);
323 g_value_set_enum (value,
324 (handle_box->snap_edge == -1 ?
325 GTK_POS_TOP : handle_box->snap_edge));
327 case PROP_SNAP_EDGE_SET:
328 g_value_set_boolean (value, handle_box->snap_edge != -1);
330 case PROP_CHILD_DETACHED:
331 g_value_set_boolean (value, handle_box->child_detached);
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
340 gtk_handle_box_new (void)
342 return g_object_new (GTK_TYPE_HANDLE_BOX, NULL);
346 gtk_handle_box_map (GtkWidget *widget)
351 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
353 bin = GTK_BIN (widget);
354 hb = GTK_HANDLE_BOX (widget);
357 GTK_WIDGET_VISIBLE (bin->child) &&
358 !GTK_WIDGET_MAPPED (bin->child))
359 gtk_widget_map (bin->child);
361 if (hb->child_detached && !hb->float_window_mapped)
363 gdk_window_show (hb->float_window);
364 hb->float_window_mapped = TRUE;
367 gdk_window_show (hb->bin_window);
368 gdk_window_show (widget->window);
372 gtk_handle_box_unmap (GtkWidget *widget)
376 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
378 hb = GTK_HANDLE_BOX (widget);
380 gdk_window_hide (widget->window);
381 if (hb->float_window_mapped)
383 gdk_window_hide (hb->float_window);
384 hb->float_window_mapped = FALSE;
389 gtk_handle_box_realize (GtkWidget *widget)
391 GdkWindowAttr attributes;
392 gint attributes_mask;
395 hb = GTK_HANDLE_BOX (widget);
397 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
399 attributes.x = widget->allocation.x;
400 attributes.y = widget->allocation.y;
401 attributes.width = widget->allocation.width;
402 attributes.height = widget->allocation.height;
403 attributes.window_type = GDK_WINDOW_CHILD;
404 attributes.wclass = GDK_INPUT_OUTPUT;
405 attributes.visual = gtk_widget_get_visual (widget);
406 attributes.colormap = gtk_widget_get_colormap (widget);
407 attributes.event_mask = (gtk_widget_get_events (widget)
408 | GDK_EXPOSURE_MASK);
409 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
410 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
411 gdk_window_set_user_data (widget->window, widget);
415 attributes.width = widget->allocation.width;
416 attributes.height = widget->allocation.height;
417 attributes.window_type = GDK_WINDOW_CHILD;
418 attributes.event_mask = (gtk_widget_get_events (widget) |
420 GDK_BUTTON1_MOTION_MASK |
421 GDK_POINTER_MOTION_HINT_MASK |
422 GDK_BUTTON_PRESS_MASK |
423 GDK_BUTTON_RELEASE_MASK);
424 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
425 hb->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask);
426 gdk_window_set_user_data (hb->bin_window, widget);
427 if (GTK_BIN (hb)->child)
428 gtk_widget_set_parent_window (GTK_BIN (hb)->child, hb->bin_window);
432 attributes.width = widget->requisition.width;
433 attributes.height = widget->requisition.height;
434 attributes.window_type = GDK_WINDOW_TOPLEVEL;
435 attributes.wclass = GDK_INPUT_OUTPUT;
436 attributes.visual = gtk_widget_get_visual (widget);
437 attributes.colormap = gtk_widget_get_colormap (widget);
438 attributes.event_mask = (gtk_widget_get_events (widget) |
440 GDK_ENTER_NOTIFY_MASK |
441 GDK_LEAVE_NOTIFY_MASK |
442 GDK_FOCUS_CHANGE_MASK |
444 attributes.type_hint = GDK_WINDOW_TYPE_HINT_TOOLBAR;
445 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP | GDK_WA_TYPE_HINT;
446 hb->float_window = gdk_window_new (gtk_widget_get_root_window (widget),
447 &attributes, attributes_mask);
448 gdk_window_set_user_data (hb->float_window, widget);
449 gdk_window_set_decorations (hb->float_window, 0);
450 gdk_window_set_type_hint (hb->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
452 widget->style = gtk_style_attach (widget->style, widget->window);
453 gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (hb));
454 gtk_style_set_background (widget->style, hb->bin_window, GTK_WIDGET_STATE (hb));
455 gtk_style_set_background (widget->style, hb->float_window, GTK_WIDGET_STATE (hb));
456 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
460 gtk_handle_box_unrealize (GtkWidget *widget)
462 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
464 gdk_window_set_user_data (hb->bin_window, NULL);
465 gdk_window_destroy (hb->bin_window);
466 hb->bin_window = NULL;
467 gdk_window_set_user_data (hb->float_window, NULL);
468 gdk_window_destroy (hb->float_window);
469 hb->float_window = NULL;
471 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unrealize (widget);
475 gtk_handle_box_style_set (GtkWidget *widget,
476 GtkStyle *previous_style)
478 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
480 if (GTK_WIDGET_REALIZED (widget) &&
481 !GTK_WIDGET_NO_WINDOW (widget))
483 gtk_style_set_background (widget->style, widget->window,
485 gtk_style_set_background (widget->style, hb->bin_window, widget->state);
486 gtk_style_set_background (widget->style, hb->float_window, widget->state);
491 effective_handle_position (GtkHandleBox *hb)
495 if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
496 handle_position = hb->handle_position;
499 switch (hb->handle_position)
502 handle_position = GTK_POS_RIGHT;
505 handle_position = GTK_POS_LEFT;
508 handle_position = hb->handle_position;
513 return handle_position;
517 gtk_handle_box_size_request (GtkWidget *widget,
518 GtkRequisition *requisition)
522 GtkRequisition child_requisition;
523 gint handle_position;
525 bin = GTK_BIN (widget);
526 hb = GTK_HANDLE_BOX (widget);
528 handle_position = effective_handle_position (hb);
530 if (handle_position == GTK_POS_LEFT ||
531 handle_position == GTK_POS_RIGHT)
533 requisition->width = DRAG_HANDLE_SIZE;
534 requisition->height = 0;
538 requisition->width = 0;
539 requisition->height = DRAG_HANDLE_SIZE;
542 /* if our child is not visible, we still request its size, since we
543 * won't have any useful hint for our size otherwise.
546 gtk_widget_size_request (bin->child, &child_requisition);
549 child_requisition.width = 0;
550 child_requisition.height = 0;
553 if (hb->child_detached)
555 /* FIXME: This doesn't work currently */
556 if (!hb->shrink_on_detach)
558 if (handle_position == GTK_POS_LEFT ||
559 handle_position == GTK_POS_RIGHT)
560 requisition->height += child_requisition.height;
562 requisition->width += child_requisition.width;
566 if (handle_position == GTK_POS_LEFT ||
567 handle_position == GTK_POS_RIGHT)
568 requisition->height += widget->style->ythickness;
570 requisition->width += widget->style->xthickness;
575 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
576 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
580 requisition->width += child_requisition.width;
581 requisition->height += child_requisition.height;
585 requisition->width += CHILDLESS_SIZE;
586 requisition->height += CHILDLESS_SIZE;
592 gtk_handle_box_size_allocate (GtkWidget *widget,
593 GtkAllocation *allocation)
597 GtkRequisition child_requisition;
598 gint handle_position;
600 bin = GTK_BIN (widget);
601 hb = GTK_HANDLE_BOX (widget);
603 handle_position = effective_handle_position (hb);
606 gtk_widget_get_child_requisition (bin->child, &child_requisition);
609 child_requisition.width = 0;
610 child_requisition.height = 0;
613 widget->allocation = *allocation;
615 if (GTK_WIDGET_REALIZED (hb))
616 gdk_window_move_resize (widget->window,
617 widget->allocation.x,
618 widget->allocation.y,
619 widget->allocation.width,
620 widget->allocation.height);
623 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
625 GtkAllocation child_allocation;
628 border_width = GTK_CONTAINER (widget)->border_width;
630 child_allocation.x = border_width;
631 child_allocation.y = border_width;
632 if (handle_position == GTK_POS_LEFT)
633 child_allocation.x += DRAG_HANDLE_SIZE;
634 else if (handle_position == GTK_POS_TOP)
635 child_allocation.y += DRAG_HANDLE_SIZE;
637 if (hb->child_detached)
642 child_allocation.width = child_requisition.width;
643 child_allocation.height = child_requisition.height;
645 float_width = child_allocation.width + 2 * border_width;
646 float_height = child_allocation.height + 2 * border_width;
648 if (handle_position == GTK_POS_LEFT ||
649 handle_position == GTK_POS_RIGHT)
650 float_width += DRAG_HANDLE_SIZE;
652 float_height += DRAG_HANDLE_SIZE;
654 if (GTK_WIDGET_REALIZED (hb))
656 gdk_window_resize (hb->float_window,
659 gdk_window_move_resize (hb->bin_window,
668 child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
669 child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
671 if (handle_position == GTK_POS_LEFT ||
672 handle_position == GTK_POS_RIGHT)
673 child_allocation.width -= DRAG_HANDLE_SIZE;
675 child_allocation.height -= DRAG_HANDLE_SIZE;
677 if (GTK_WIDGET_REALIZED (hb))
678 gdk_window_move_resize (hb->bin_window,
681 widget->allocation.width,
682 widget->allocation.height);
685 gtk_widget_size_allocate (bin->child, &child_allocation);
690 gtk_handle_box_draw_ghost (GtkHandleBox *hb)
697 gint handle_position;
699 widget = GTK_WIDGET (hb);
701 handle_position = effective_handle_position (hb);
702 if (handle_position == GTK_POS_LEFT ||
703 handle_position == GTK_POS_RIGHT)
705 x = handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
707 width = DRAG_HANDLE_SIZE;
708 height = widget->allocation.height;
713 y = handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
714 width = widget->allocation.width;
715 height = DRAG_HANDLE_SIZE;
717 gtk_paint_shadow (widget->style,
719 GTK_WIDGET_STATE (widget),
720 GTK_SHADOW_ETCHED_IN,
721 NULL, widget, "handle",
726 if (handle_position == GTK_POS_LEFT ||
727 handle_position == GTK_POS_RIGHT)
728 gtk_paint_hline (widget->style,
730 GTK_WIDGET_STATE (widget),
731 NULL, widget, "handlebox",
732 handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
733 handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
734 widget->allocation.height / 2);
736 gtk_paint_vline (widget->style,
738 GTK_WIDGET_STATE (widget),
739 NULL, widget, "handlebox",
740 handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
741 handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
742 widget->allocation.width / 2);
746 draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
747 GdkRectangle *clip, GtkOrientation orientation)
749 gtk_paint_handle (widget->style, window, GTK_STATE_NORMAL, shadow,
750 clip, widget, "handlebox",
751 rect->x, rect->y, rect->width, rect->height,
756 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
759 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
761 if ((GtkShadowType) handle_box->shadow_type != type)
763 handle_box->shadow_type = type;
764 g_object_notify (G_OBJECT (handle_box), "shadow-type");
765 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
770 * gtk_handle_box_get_shadow_type:
771 * @handle_box: a #GtkHandleBox
773 * Gets the type of shadow drawn around the handle box. See
774 * gtk_handle_box_set_shadow_type().
776 * Return value: the type of shadow currently drawn around the handle box.
779 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
781 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
783 return handle_box->shadow_type;
787 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
788 GtkPositionType position)
790 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
792 if ((GtkPositionType) handle_box->handle_position != position)
794 handle_box->handle_position = position;
795 g_object_notify (G_OBJECT (handle_box), "handle-position");
796 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
801 * gtk_handle_box_get_handle_position:
802 * @handle_box: a #GtkHandleBox
804 * Gets the handle position of the handle box. See
805 * gtk_handle_box_set_handle_position().
807 * Return value: the current handle position.
810 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
812 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
814 return handle_box->handle_position;
818 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
819 GtkPositionType edge)
821 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
823 if (handle_box->snap_edge != edge)
825 handle_box->snap_edge = edge;
827 g_object_freeze_notify (G_OBJECT (handle_box));
828 g_object_notify (G_OBJECT (handle_box), "snap-edge");
829 g_object_notify (G_OBJECT (handle_box), "snap-edge-set");
830 g_object_thaw_notify (G_OBJECT (handle_box));
835 * gtk_handle_box_get_snap_edge:
836 * @handle_box: a #GtkHandleBox
838 * Gets the edge used for determining reattachment of the handle box. See
839 * gtk_handle_box_set_snap_edge().
841 * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
842 * is determined (as per default) from the handle position.
845 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
847 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
849 return handle_box->snap_edge;
853 * gtk_handle_box_get_child_detached:
854 * @handle_box: a #GtkHandleBox
856 * Whether the handlebox's child is currently detached.
858 * Return value: %TRUE if the child is currently detached, otherwise %FALSE
863 gtk_handle_box_get_child_detached (GtkHandleBox *handle_box)
865 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), FALSE);
867 return handle_box->child_detached;
871 gtk_handle_box_paint (GtkWidget *widget,
872 GdkEventExpose *event,
880 gint handle_position;
881 GtkOrientation handle_orientation;
883 bin = GTK_BIN (widget);
884 hb = GTK_HANDLE_BOX (widget);
886 handle_position = effective_handle_position (hb);
888 gdk_drawable_get_size (hb->bin_window, &width, &height);
891 gtk_paint_box (widget->style,
893 GTK_WIDGET_STATE (widget),
895 area, widget, "handlebox_bin",
898 gtk_paint_box (widget->style,
900 GTK_WIDGET_STATE (widget),
902 &event->area, widget, "handlebox_bin",
905 /* We currently draw the handle _above_ the relief of the handlebox.
906 * it could also be drawn on the same level...
908 hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
909 hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
913 switch (handle_position)
918 rect.width = DRAG_HANDLE_SIZE;
919 rect.height = height;
920 handle_orientation = GTK_ORIENTATION_VERTICAL;
923 rect.x = width - DRAG_HANDLE_SIZE;
925 rect.width = DRAG_HANDLE_SIZE;
926 rect.height = height;
927 handle_orientation = GTK_ORIENTATION_VERTICAL;
933 rect.height = DRAG_HANDLE_SIZE;
934 handle_orientation = GTK_ORIENTATION_HORIZONTAL;
938 rect.y = height - DRAG_HANDLE_SIZE;
940 rect.height = DRAG_HANDLE_SIZE;
941 handle_orientation = GTK_ORIENTATION_HORIZONTAL;
944 g_assert_not_reached ();
948 if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
949 draw_textured_frame (widget, hb->bin_window, &rect,
951 event ? &event->area : area,
954 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
955 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->expose_event (widget, event);
959 gtk_handle_box_expose (GtkWidget *widget,
960 GdkEventExpose *event)
964 if (GTK_WIDGET_DRAWABLE (widget))
966 hb = GTK_HANDLE_BOX (widget);
968 if (event->window == widget->window)
970 if (hb->child_detached)
971 gtk_handle_box_draw_ghost (hb);
974 gtk_handle_box_paint (widget, event, NULL);
981 gtk_handle_box_get_invisible (void)
983 static GtkWidget *handle_box_invisible = NULL;
985 if (!handle_box_invisible)
987 handle_box_invisible = gtk_invisible_new ();
988 gtk_widget_show (handle_box_invisible);
991 return handle_box_invisible;
995 gtk_handle_box_grab_event (GtkWidget *widget,
1001 case GDK_BUTTON_RELEASE:
1002 if (hb->in_drag) /* sanity check */
1004 gtk_handle_box_end_drag (hb, event->button.time);
1009 case GDK_MOTION_NOTIFY:
1010 return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event);
1021 gtk_handle_box_button_press (GtkWidget *widget,
1022 GdkEventButton *event)
1025 gboolean event_handled;
1027 gint handle_position;
1029 hb = GTK_HANDLE_BOX (widget);
1031 handle_position = effective_handle_position (hb);
1033 event_handled = FALSE;
1034 if ((event->button == 1) &&
1035 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1040 if (event->window != hb->bin_window)
1043 child = GTK_BIN (hb)->child;
1047 switch (handle_position)
1050 in_handle = event->x < DRAG_HANDLE_SIZE;
1053 in_handle = event->y < DRAG_HANDLE_SIZE;
1056 in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width;
1058 case GTK_POS_BOTTOM:
1059 in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height;
1069 event_handled = TRUE;
1074 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
1076 GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1077 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1078 gint desk_x, desk_y;
1079 gint root_x, root_y;
1082 gtk_invisible_set_screen (GTK_INVISIBLE (invisible),
1083 gtk_widget_get_screen (GTK_WIDGET (hb)));
1084 gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y);
1085 gdk_window_get_origin (hb->bin_window, &root_x, &root_y);
1086 gdk_drawable_get_size (hb->bin_window, &width, &height);
1088 private->orig_x = event->x_root;
1089 private->orig_y = event->y_root;
1091 hb->float_allocation.x = root_x - event->x_root;
1092 hb->float_allocation.y = root_y - event->y_root;
1093 hb->float_allocation.width = width;
1094 hb->float_allocation.height = height;
1096 hb->deskoff_x = desk_x - root_x;
1097 hb->deskoff_y = desk_y - root_y;
1099 if (gdk_window_is_viewable (widget->window))
1101 gdk_window_get_origin (widget->window, &root_x, &root_y);
1102 gdk_drawable_get_size (widget->window, &width, &height);
1104 hb->attach_allocation.x = root_x;
1105 hb->attach_allocation.y = root_y;
1106 hb->attach_allocation.width = width;
1107 hb->attach_allocation.height = height;
1111 hb->attach_allocation.x = -1;
1112 hb->attach_allocation.y = -1;
1113 hb->attach_allocation.width = 0;
1114 hb->attach_allocation.height = 0;
1117 fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1119 if (gdk_pointer_grab (invisible->window,
1121 (GDK_BUTTON1_MOTION_MASK |
1122 GDK_POINTER_MOTION_HINT_MASK |
1123 GDK_BUTTON_RELEASE_MASK),
1128 hb->in_drag = FALSE;
1132 gtk_grab_add (invisible);
1133 g_signal_connect (invisible, "event",
1134 G_CALLBACK (gtk_handle_box_grab_event), hb);
1137 gdk_cursor_unref (fleur);
1138 event_handled = TRUE;
1140 else if (hb->child_detached) /* Double click */
1142 gtk_handle_box_reattach (hb);
1147 return event_handled;
1151 gtk_handle_box_motion (GtkWidget *widget,
1152 GdkEventMotion *event)
1154 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1157 gboolean is_snapped = FALSE;
1158 gint handle_position;
1159 GdkGeometry geometry;
1160 GdkScreen *screen, *pointer_screen;
1164 handle_position = effective_handle_position (hb);
1166 /* Calculate the attachment point on the float, if the float
1171 screen = gtk_widget_get_screen (widget);
1172 gdk_display_get_pointer (gdk_screen_get_display (screen),
1174 &new_x, &new_y, NULL);
1175 if (pointer_screen != screen)
1177 GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1179 new_x = private->orig_x;
1180 new_y = private->orig_y;
1183 new_x += hb->float_allocation.x;
1184 new_y += hb->float_allocation.y;
1186 snap_edge = hb->snap_edge;
1187 if (snap_edge == -1)
1188 snap_edge = (handle_position == GTK_POS_LEFT ||
1189 handle_position == GTK_POS_RIGHT) ?
1190 GTK_POS_TOP : GTK_POS_LEFT;
1192 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1196 snap_edge = GTK_POS_RIGHT;
1199 snap_edge = GTK_POS_LEFT;
1205 /* First, check if the snapped edge is aligned
1210 is_snapped = abs (hb->attach_allocation.y - new_y) < TOLERANCE;
1212 case GTK_POS_BOTTOM:
1213 is_snapped = abs (hb->attach_allocation.y + (gint)hb->attach_allocation.height -
1214 new_y - (gint)hb->float_allocation.height) < TOLERANCE;
1217 is_snapped = abs (hb->attach_allocation.x - new_x) < TOLERANCE;
1220 is_snapped = abs (hb->attach_allocation.x + (gint)hb->attach_allocation.width -
1221 new_x - (gint)hb->float_allocation.width) < TOLERANCE;
1225 /* Next, check if coordinates in the other direction are sufficiently
1230 gint float_pos1 = 0; /* Initialize to suppress warnings */
1231 gint float_pos2 = 0;
1232 gint attach_pos1 = 0;
1233 gint attach_pos2 = 0;
1238 case GTK_POS_BOTTOM:
1239 attach_pos1 = hb->attach_allocation.x;
1240 attach_pos2 = hb->attach_allocation.x + hb->attach_allocation.width;
1242 float_pos2 = new_x + hb->float_allocation.width;
1246 attach_pos1 = hb->attach_allocation.y;
1247 attach_pos2 = hb->attach_allocation.y + hb->attach_allocation.height;
1249 float_pos2 = new_y + hb->float_allocation.height;
1253 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1254 (attach_pos2 + TOLERANCE > float_pos2)) ||
1255 ((float_pos1 - TOLERANCE < attach_pos1) &&
1256 (float_pos2 + TOLERANCE > attach_pos2));
1261 if (hb->child_detached)
1263 hb->child_detached = FALSE;
1264 gdk_window_hide (hb->float_window);
1265 gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1266 hb->float_window_mapped = FALSE;
1268 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1270 GTK_BIN (hb)->child);
1272 gtk_widget_queue_resize (widget);
1279 gdk_drawable_get_size (hb->float_window, &width, &height);
1280 new_x += hb->deskoff_x;
1281 new_y += hb->deskoff_y;
1283 switch (handle_position)
1286 new_y += ((gint)hb->float_allocation.height - height) / 2;
1289 new_x += (gint)hb->float_allocation.width - width;
1290 new_y += ((gint)hb->float_allocation.height - height) / 2;
1293 new_x += ((gint)hb->float_allocation.width - width) / 2;
1295 case GTK_POS_BOTTOM:
1296 new_x += ((gint)hb->float_allocation.width - width) / 2;
1297 new_y += (gint)hb->float_allocation.height - height;
1301 if (hb->child_detached)
1303 gdk_window_move (hb->float_window, new_x, new_y);
1304 gdk_window_raise (hb->float_window);
1310 GtkRequisition child_requisition;
1312 hb->child_detached = TRUE;
1314 if (GTK_BIN (hb)->child)
1315 gtk_widget_get_child_requisition (GTK_BIN (hb)->child, &child_requisition);
1318 child_requisition.width = 0;
1319 child_requisition.height = 0;
1322 width = child_requisition.width + 2 * GTK_CONTAINER (hb)->border_width;
1323 height = child_requisition.height + 2 * GTK_CONTAINER (hb)->border_width;
1325 if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT)
1326 width += DRAG_HANDLE_SIZE;
1328 height += DRAG_HANDLE_SIZE;
1330 gdk_window_move_resize (hb->float_window, new_x, new_y, width, height);
1331 gdk_window_reparent (hb->bin_window, hb->float_window, 0, 0);
1332 gdk_window_set_geometry_hints (hb->float_window, &geometry, GDK_HINT_POS);
1333 gdk_window_show (hb->float_window);
1334 hb->float_window_mapped = TRUE;
1336 /* this extra move is necessary if we use decorations, or our
1337 * window manager insists on decorations.
1339 gdk_display_sync (gtk_widget_get_display (widget));
1340 gdk_window_move (hb->float_window, new_x, new_y);
1341 gdk_display_sync (gtk_widget_get_display (widget));
1344 handle_box_signals[SIGNAL_CHILD_DETACHED],
1346 GTK_BIN (hb)->child);
1347 gtk_handle_box_draw_ghost (hb);
1349 gtk_widget_queue_resize (widget);
1357 gtk_handle_box_add (GtkContainer *container,
1360 gtk_widget_set_parent_window (widget, GTK_HANDLE_BOX (container)->bin_window);
1361 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->add (container, widget);
1365 gtk_handle_box_remove (GtkContainer *container,
1368 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->remove (container, widget);
1370 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1374 gtk_handle_box_delete_event (GtkWidget *widget,
1377 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1379 if (event->window == hb->float_window)
1381 gtk_handle_box_reattach (hb);
1390 gtk_handle_box_reattach (GtkHandleBox *hb)
1392 GtkWidget *widget = GTK_WIDGET (hb);
1394 if (hb->child_detached)
1396 hb->child_detached = FALSE;
1397 if (GTK_WIDGET_REALIZED (hb))
1399 gdk_window_hide (hb->float_window);
1400 gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1402 if (GTK_BIN (hb)->child)
1404 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1406 GTK_BIN (hb)->child);
1409 hb->float_window_mapped = FALSE;
1412 gtk_handle_box_end_drag (hb, GDK_CURRENT_TIME);
1414 gtk_widget_queue_resize (GTK_WIDGET (hb));
1418 gtk_handle_box_end_drag (GtkHandleBox *hb,
1421 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1423 hb->in_drag = FALSE;
1425 gtk_grab_remove (invisible);
1426 gdk_pointer_ungrab (time);
1427 g_signal_handlers_disconnect_by_func (invisible,
1428 G_CALLBACK (gtk_handle_box_grab_event),
1432 #define __GTK_HANDLE_BOX_C__
1433 #include "gtkaliasdef.c"