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"
40 struct _GtkHandleBoxPrivate
43 GtkPositionType handle_position;
44 GtkPositionType snap_edge;
45 GtkShadowType shadow_type;
46 gboolean child_detached;
49 GtkAllocation attach_allocation;
50 GtkAllocation float_allocation;
52 GdkDevice *grab_device;
54 GdkWindow *bin_window; /* parent window for children */
55 GdkWindow *float_window;
57 /* Variables used during a drag
59 gint deskoff_x; /* Offset between root relative coords */
60 gint deskoff_y; /* and deskrelative coords */
65 guint float_window_mapped : 1;
67 guint shrink_on_detach : 1;
80 #define DRAG_HANDLE_SIZE 10
81 #define CHILDLESS_SIZE 25
82 #define GHOST_HEIGHT 3
86 SIGNAL_CHILD_ATTACHED,
87 SIGNAL_CHILD_DETACHED,
91 /* The algorithm for docking and redocking implemented here
92 * has a couple of nice properties:
94 * 1) During a single drag, docking always occurs at the
95 * the same cursor position. This means that the users
96 * motions are reversible, and that you won't
97 * undock/dock oscillations.
99 * 2) Docking generally occurs at user-visible features.
100 * The user, once they figure out to redock, will
101 * have useful information about doing it again in
104 * Please try to preserve these properties if you
105 * change the algorithm. (And the current algorithm
106 * is far from ideal). Briefly, the current algorithm
107 * for deciding whether the handlebox is docked or not:
109 * 1) The decision is done by comparing two rectangles - the
110 * allocation if the widget at the start of the drag,
111 * and the boundary of hb->bin_window at the start of
112 * of the drag offset by the distance that the cursor
115 * 2) These rectangles must have one edge, the "snap_edge"
116 * of the handlebox, aligned within TOLERANCE.
118 * 3) On the other dimension, the extents of one rectangle
119 * must be contained in the extents of the other,
120 * extended by tolerance. That is, either we can have:
122 * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE->
123 * <--------float_window-------------------->
127 * <-TOLERANCE-|------float_window--------------|-TOLERANCE->
128 * <--------bin_window-------------------->
131 static void gtk_handle_box_set_property (GObject *object,
135 static void gtk_handle_box_get_property (GObject *object,
139 static void gtk_handle_box_map (GtkWidget *widget);
140 static void gtk_handle_box_unmap (GtkWidget *widget);
141 static void gtk_handle_box_realize (GtkWidget *widget);
142 static void gtk_handle_box_unrealize (GtkWidget *widget);
143 static void gtk_handle_box_style_set (GtkWidget *widget,
144 GtkStyle *previous_style);
145 static void gtk_handle_box_size_request (GtkWidget *widget,
146 GtkRequisition *requisition);
147 static void gtk_handle_box_size_allocate (GtkWidget *widget,
148 GtkAllocation *real_allocation);
149 static void gtk_handle_box_add (GtkContainer *container,
151 static void gtk_handle_box_remove (GtkContainer *container,
153 static void gtk_handle_box_draw_ghost (GtkHandleBox *hb);
154 static void gtk_handle_box_paint (GtkWidget *widget,
155 GdkEventExpose *event,
157 static gboolean gtk_handle_box_expose (GtkWidget *widget,
158 GdkEventExpose *event);
159 static gboolean gtk_handle_box_button_press (GtkWidget *widget,
160 GdkEventButton *event);
161 static gboolean gtk_handle_box_motion (GtkWidget *widget,
162 GdkEventMotion *event);
163 static gboolean gtk_handle_box_delete_event (GtkWidget *widget,
165 static void gtk_handle_box_reattach (GtkHandleBox *hb);
166 static void gtk_handle_box_end_drag (GtkHandleBox *hb,
169 static guint handle_box_signals[SIGNAL_LAST] = { 0 };
171 G_DEFINE_TYPE (GtkHandleBox, gtk_handle_box, GTK_TYPE_BIN)
174 gtk_handle_box_class_init (GtkHandleBoxClass *class)
176 GObjectClass *gobject_class;
177 GtkWidgetClass *widget_class;
178 GtkContainerClass *container_class;
180 gobject_class = (GObjectClass *) class;
181 widget_class = (GtkWidgetClass *) class;
182 container_class = (GtkContainerClass *) class;
184 gobject_class->set_property = gtk_handle_box_set_property;
185 gobject_class->get_property = gtk_handle_box_get_property;
187 g_object_class_install_property (gobject_class,
189 g_param_spec_enum ("shadow", NULL,
190 P_("Deprecated property, use shadow_type instead"),
191 GTK_TYPE_SHADOW_TYPE,
193 GTK_PARAM_READWRITE));
194 g_object_class_install_property (gobject_class,
196 g_param_spec_enum ("shadow-type",
198 P_("Appearance of the shadow that surrounds the container"),
199 GTK_TYPE_SHADOW_TYPE,
201 GTK_PARAM_READWRITE));
203 g_object_class_install_property (gobject_class,
204 PROP_HANDLE_POSITION,
205 g_param_spec_enum ("handle-position",
206 P_("Handle position"),
207 P_("Position of the handle relative to the child widget"),
208 GTK_TYPE_POSITION_TYPE,
210 GTK_PARAM_READWRITE));
212 g_object_class_install_property (gobject_class,
214 g_param_spec_enum ("snap-edge",
216 P_("Side of the handlebox that's lined up with the docking point to dock the handlebox"),
217 GTK_TYPE_POSITION_TYPE,
219 GTK_PARAM_READWRITE));
221 g_object_class_install_property (gobject_class,
223 g_param_spec_boolean ("snap-edge-set",
225 P_("Whether to use the value from the snap_edge property or a value derived from handle_position"),
227 GTK_PARAM_READWRITE));
229 g_object_class_install_property (gobject_class,
231 g_param_spec_boolean ("child-detached",
232 P_("Child Detached"),
233 P_("A boolean value indicating whether the handlebox's child is attached or detached."),
235 GTK_PARAM_READABLE));
237 widget_class->map = gtk_handle_box_map;
238 widget_class->unmap = gtk_handle_box_unmap;
239 widget_class->realize = gtk_handle_box_realize;
240 widget_class->unrealize = gtk_handle_box_unrealize;
241 widget_class->style_set = gtk_handle_box_style_set;
242 widget_class->size_request = gtk_handle_box_size_request;
243 widget_class->size_allocate = gtk_handle_box_size_allocate;
244 widget_class->expose_event = gtk_handle_box_expose;
245 widget_class->button_press_event = gtk_handle_box_button_press;
246 widget_class->delete_event = gtk_handle_box_delete_event;
248 container_class->add = gtk_handle_box_add;
249 container_class->remove = gtk_handle_box_remove;
251 class->child_attached = NULL;
252 class->child_detached = NULL;
254 handle_box_signals[SIGNAL_CHILD_ATTACHED] =
255 g_signal_new (I_("child-attached"),
256 G_OBJECT_CLASS_TYPE (gobject_class),
258 G_STRUCT_OFFSET (GtkHandleBoxClass, child_attached),
260 _gtk_marshal_VOID__OBJECT,
263 handle_box_signals[SIGNAL_CHILD_DETACHED] =
264 g_signal_new (I_("child-detached"),
265 G_OBJECT_CLASS_TYPE (gobject_class),
267 G_STRUCT_OFFSET (GtkHandleBoxClass, child_detached),
269 _gtk_marshal_VOID__OBJECT,
273 g_type_class_add_private (gobject_class, sizeof (GtkHandleBoxPrivate));
277 gtk_handle_box_init (GtkHandleBox *handle_box)
279 GtkHandleBoxPrivate *priv;
281 handle_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (handle_box,
283 GtkHandleBoxPrivate);
284 priv = handle_box->priv;
286 gtk_widget_set_has_window (GTK_WIDGET (handle_box), TRUE);
288 priv->bin_window = NULL;
289 priv->float_window = NULL;
290 priv->shadow_type = GTK_SHADOW_OUT;
291 priv->handle_position = GTK_POS_LEFT;
292 priv->float_window_mapped = FALSE;
293 priv->child_detached = FALSE;
294 priv->in_drag = FALSE;
295 priv->shrink_on_detach = TRUE;
296 priv->snap_edge = -1;
300 gtk_handle_box_set_property (GObject *object,
305 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
310 case PROP_SHADOW_TYPE:
311 gtk_handle_box_set_shadow_type (handle_box, g_value_get_enum (value));
313 case PROP_HANDLE_POSITION:
314 gtk_handle_box_set_handle_position (handle_box, g_value_get_enum (value));
317 gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value));
319 case PROP_SNAP_EDGE_SET:
320 if (!g_value_get_boolean (value))
321 gtk_handle_box_set_snap_edge (handle_box, (GtkPositionType)-1);
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330 gtk_handle_box_get_property (GObject *object,
335 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
336 GtkHandleBoxPrivate *priv = handle_box->priv;
341 case PROP_SHADOW_TYPE:
342 g_value_set_enum (value, priv->shadow_type);
344 case PROP_HANDLE_POSITION:
345 g_value_set_enum (value, priv->handle_position);
348 g_value_set_enum (value,
349 (priv->snap_edge == -1 ?
350 GTK_POS_TOP : priv->snap_edge));
352 case PROP_SNAP_EDGE_SET:
353 g_value_set_boolean (value, priv->snap_edge != -1);
355 case PROP_CHILD_DETACHED:
356 g_value_set_boolean (value, priv->child_detached);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365 gtk_handle_box_new (void)
367 return g_object_new (GTK_TYPE_HANDLE_BOX, NULL);
371 gtk_handle_box_map (GtkWidget *widget)
373 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
374 GtkHandleBoxPrivate *priv = hb->priv;
375 GtkBin *bin = GTK_BIN (widget);
378 gtk_widget_set_mapped (widget, TRUE);
380 child = gtk_bin_get_child (bin);
382 gtk_widget_get_visible (child) &&
383 !gtk_widget_get_mapped (child))
384 gtk_widget_map (child);
386 if (priv->child_detached && !priv->float_window_mapped)
388 gdk_window_show (priv->float_window);
389 priv->float_window_mapped = TRUE;
392 gdk_window_show (priv->bin_window);
393 gdk_window_show (widget->window);
397 gtk_handle_box_unmap (GtkWidget *widget)
399 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
400 GtkHandleBoxPrivate *priv = hb->priv;
402 gtk_widget_set_mapped (widget, FALSE);
404 gdk_window_hide (widget->window);
405 if (priv->float_window_mapped)
407 gdk_window_hide (priv->float_window);
408 priv->float_window_mapped = FALSE;
413 gtk_handle_box_realize (GtkWidget *widget)
415 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
416 GtkHandleBoxPrivate *priv = hb->priv;
418 GdkWindowAttr attributes;
419 gint attributes_mask;
421 gtk_widget_set_realized (widget, TRUE);
423 attributes.x = widget->allocation.x;
424 attributes.y = widget->allocation.y;
425 attributes.width = widget->allocation.width;
426 attributes.height = widget->allocation.height;
427 attributes.window_type = GDK_WINDOW_CHILD;
428 attributes.wclass = GDK_INPUT_OUTPUT;
429 attributes.visual = gtk_widget_get_visual (widget);
430 attributes.colormap = gtk_widget_get_colormap (widget);
431 attributes.event_mask = (gtk_widget_get_events (widget)
432 | GDK_EXPOSURE_MASK);
433 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
434 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
435 gdk_window_set_user_data (widget->window, widget);
439 attributes.width = widget->allocation.width;
440 attributes.height = widget->allocation.height;
441 attributes.window_type = GDK_WINDOW_CHILD;
442 attributes.event_mask = (gtk_widget_get_events (widget) |
444 GDK_BUTTON1_MOTION_MASK |
445 GDK_POINTER_MOTION_HINT_MASK |
446 GDK_BUTTON_PRESS_MASK |
447 GDK_BUTTON_RELEASE_MASK);
448 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
449 priv->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask);
450 gdk_window_set_user_data (priv->bin_window, widget);
452 child = gtk_bin_get_child (GTK_BIN (hb));
454 gtk_widget_set_parent_window (child, priv->bin_window);
458 attributes.width = widget->requisition.width;
459 attributes.height = widget->requisition.height;
460 attributes.window_type = GDK_WINDOW_TOPLEVEL;
461 attributes.wclass = GDK_INPUT_OUTPUT;
462 attributes.visual = gtk_widget_get_visual (widget);
463 attributes.colormap = gtk_widget_get_colormap (widget);
464 attributes.event_mask = (gtk_widget_get_events (widget) |
466 GDK_ENTER_NOTIFY_MASK |
467 GDK_LEAVE_NOTIFY_MASK |
468 GDK_FOCUS_CHANGE_MASK |
470 attributes.type_hint = GDK_WINDOW_TYPE_HINT_TOOLBAR;
471 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP | GDK_WA_TYPE_HINT;
472 priv->float_window = gdk_window_new (gtk_widget_get_root_window (widget),
473 &attributes, attributes_mask);
474 gdk_window_set_user_data (priv->float_window, widget);
475 gdk_window_set_decorations (priv->float_window, 0);
476 gdk_window_set_type_hint (priv->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
478 widget->style = gtk_style_attach (widget->style, widget->window);
479 gtk_style_set_background (widget->style, widget->window, gtk_widget_get_state (widget));
480 gtk_style_set_background (widget->style, priv->bin_window, gtk_widget_get_state (widget));
481 gtk_style_set_background (widget->style, priv->float_window, gtk_widget_get_state (widget));
482 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
486 gtk_handle_box_unrealize (GtkWidget *widget)
488 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
489 GtkHandleBoxPrivate *priv = hb->priv;
491 gdk_window_set_user_data (priv->bin_window, NULL);
492 gdk_window_destroy (priv->bin_window);
493 priv->bin_window = NULL;
494 gdk_window_set_user_data (priv->float_window, NULL);
495 gdk_window_destroy (priv->float_window);
496 priv->float_window = NULL;
498 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unrealize (widget);
502 gtk_handle_box_style_set (GtkWidget *widget,
503 GtkStyle *previous_style)
505 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
506 GtkHandleBoxPrivate *priv = hb->priv;
508 if (gtk_widget_get_realized (widget) &&
509 gtk_widget_get_has_window (widget))
511 gtk_style_set_background (widget->style, widget->window,
513 gtk_style_set_background (widget->style, priv->bin_window, widget->state);
514 gtk_style_set_background (widget->style, priv->float_window, widget->state);
519 effective_handle_position (GtkHandleBox *hb)
521 GtkHandleBoxPrivate *priv = hb->priv;
524 if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
525 handle_position = priv->handle_position;
528 switch (priv->handle_position)
531 handle_position = GTK_POS_RIGHT;
534 handle_position = GTK_POS_LEFT;
537 handle_position = priv->handle_position;
542 return handle_position;
546 gtk_handle_box_size_request (GtkWidget *widget,
547 GtkRequisition *requisition)
549 GtkBin *bin = GTK_BIN (widget);
550 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
551 GtkHandleBoxPrivate *priv = hb->priv;
552 GtkRequisition child_requisition;
554 gint handle_position;
556 handle_position = effective_handle_position (hb);
558 if (handle_position == GTK_POS_LEFT ||
559 handle_position == GTK_POS_RIGHT)
561 requisition->width = DRAG_HANDLE_SIZE;
562 requisition->height = 0;
566 requisition->width = 0;
567 requisition->height = DRAG_HANDLE_SIZE;
570 child = gtk_bin_get_child (bin);
571 /* if our child is not visible, we still request its size, since we
572 * won't have any useful hint for our size otherwise.
575 gtk_widget_size_request (child, &child_requisition);
578 child_requisition.width = 0;
579 child_requisition.height = 0;
582 if (priv->child_detached)
584 /* FIXME: This doesn't work currently */
585 if (!priv->shrink_on_detach)
587 if (handle_position == GTK_POS_LEFT ||
588 handle_position == GTK_POS_RIGHT)
589 requisition->height += child_requisition.height;
591 requisition->width += child_requisition.width;
595 if (handle_position == GTK_POS_LEFT ||
596 handle_position == GTK_POS_RIGHT)
597 requisition->height += widget->style->ythickness;
599 requisition->width += widget->style->xthickness;
606 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
607 requisition->width += border_width * 2;
608 requisition->height += border_width * 2;
612 requisition->width += child_requisition.width;
613 requisition->height += child_requisition.height;
617 requisition->width += CHILDLESS_SIZE;
618 requisition->height += CHILDLESS_SIZE;
624 gtk_handle_box_size_allocate (GtkWidget *widget,
625 GtkAllocation *allocation)
627 GtkBin *bin = GTK_BIN (widget);
628 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
629 GtkHandleBoxPrivate *priv = hb->priv;
630 GtkRequisition child_requisition;
632 gint handle_position;
634 handle_position = effective_handle_position (hb);
636 child = gtk_bin_get_child (bin);
639 gtk_widget_get_child_requisition (child, &child_requisition);
642 child_requisition.width = 0;
643 child_requisition.height = 0;
646 widget->allocation = *allocation;
648 if (gtk_widget_get_realized (widget))
649 gdk_window_move_resize (widget->window,
650 widget->allocation.x,
651 widget->allocation.y,
652 widget->allocation.width,
653 widget->allocation.height);
656 if (child != NULL && gtk_widget_get_visible (child))
658 GtkAllocation child_allocation;
661 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
663 child_allocation.x = border_width;
664 child_allocation.y = border_width;
665 if (handle_position == GTK_POS_LEFT)
666 child_allocation.x += DRAG_HANDLE_SIZE;
667 else if (handle_position == GTK_POS_TOP)
668 child_allocation.y += DRAG_HANDLE_SIZE;
670 if (priv->child_detached)
675 child_allocation.width = child_requisition.width;
676 child_allocation.height = child_requisition.height;
678 float_width = child_allocation.width + 2 * border_width;
679 float_height = child_allocation.height + 2 * border_width;
681 if (handle_position == GTK_POS_LEFT ||
682 handle_position == GTK_POS_RIGHT)
683 float_width += DRAG_HANDLE_SIZE;
685 float_height += DRAG_HANDLE_SIZE;
687 if (gtk_widget_get_realized (widget))
689 gdk_window_resize (priv->float_window,
692 gdk_window_move_resize (priv->bin_window,
701 child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
702 child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
704 if (handle_position == GTK_POS_LEFT ||
705 handle_position == GTK_POS_RIGHT)
706 child_allocation.width -= DRAG_HANDLE_SIZE;
708 child_allocation.height -= DRAG_HANDLE_SIZE;
710 if (gtk_widget_get_realized (widget))
711 gdk_window_move_resize (priv->bin_window,
714 widget->allocation.width,
715 widget->allocation.height);
718 gtk_widget_size_allocate (child, &child_allocation);
723 gtk_handle_box_draw_ghost (GtkHandleBox *hb)
730 gint handle_position;
732 widget = GTK_WIDGET (hb);
734 handle_position = effective_handle_position (hb);
735 if (handle_position == GTK_POS_LEFT ||
736 handle_position == GTK_POS_RIGHT)
738 x = handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
740 width = DRAG_HANDLE_SIZE;
741 height = widget->allocation.height;
746 y = handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
747 width = widget->allocation.width;
748 height = DRAG_HANDLE_SIZE;
750 gtk_paint_shadow (widget->style,
752 gtk_widget_get_state (widget),
753 GTK_SHADOW_ETCHED_IN,
754 NULL, widget, "handle",
759 if (handle_position == GTK_POS_LEFT ||
760 handle_position == GTK_POS_RIGHT)
761 gtk_paint_hline (widget->style,
763 gtk_widget_get_state (widget),
764 NULL, widget, "handlebox",
765 handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
766 handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
767 widget->allocation.height / 2);
769 gtk_paint_vline (widget->style,
771 gtk_widget_get_state (widget),
772 NULL, widget, "handlebox",
773 handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
774 handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
775 widget->allocation.width / 2);
779 draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
780 GdkRectangle *clip, GtkOrientation orientation)
782 gtk_paint_handle (widget->style, window, GTK_STATE_NORMAL, shadow,
783 clip, widget, "handlebox",
784 rect->x, rect->y, rect->width, rect->height,
789 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
792 GtkHandleBoxPrivate *priv;
794 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
796 priv = handle_box->priv;
798 if ((GtkShadowType) priv->shadow_type != type)
800 priv->shadow_type = type;
801 g_object_notify (G_OBJECT (handle_box), "shadow-type");
802 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
807 * gtk_handle_box_get_shadow_type:
808 * @handle_box: a #GtkHandleBox
810 * Gets the type of shadow drawn around the handle box. See
811 * gtk_handle_box_set_shadow_type().
813 * Return value: the type of shadow currently drawn around the handle box.
816 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
818 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
820 return handle_box->priv->shadow_type;
824 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
825 GtkPositionType position)
827 GtkHandleBoxPrivate *priv;
829 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
831 priv = handle_box->priv;
833 if ((GtkPositionType) priv->handle_position != position)
835 priv->handle_position = position;
836 g_object_notify (G_OBJECT (handle_box), "handle-position");
837 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
842 * gtk_handle_box_get_handle_position:
843 * @handle_box: a #GtkHandleBox
845 * Gets the handle position of the handle box. See
846 * gtk_handle_box_set_handle_position().
848 * Return value: the current handle position.
851 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
853 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
855 return handle_box->priv->handle_position;
859 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
860 GtkPositionType edge)
862 GtkHandleBoxPrivate *priv;
864 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
866 priv = handle_box->priv;
868 if (priv->snap_edge != edge)
870 priv->snap_edge = edge;
872 g_object_freeze_notify (G_OBJECT (handle_box));
873 g_object_notify (G_OBJECT (handle_box), "snap-edge");
874 g_object_notify (G_OBJECT (handle_box), "snap-edge-set");
875 g_object_thaw_notify (G_OBJECT (handle_box));
880 * gtk_handle_box_get_snap_edge:
881 * @handle_box: a #GtkHandleBox
883 * Gets the edge used for determining reattachment of the handle box. See
884 * gtk_handle_box_set_snap_edge().
886 * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
887 * is determined (as per default) from the handle position.
890 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
892 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
894 return handle_box->priv->snap_edge;
898 * gtk_handle_box_get_child_detached:
899 * @handle_box: a #GtkHandleBox
901 * Whether the handlebox's child is currently detached.
903 * Return value: %TRUE if the child is currently detached, otherwise %FALSE
908 gtk_handle_box_get_child_detached (GtkHandleBox *handle_box)
910 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), FALSE);
912 return handle_box->priv->child_detached;
916 gtk_handle_box_paint (GtkWidget *widget,
917 GdkEventExpose *event,
920 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
921 GtkHandleBoxPrivate *priv = hb->priv;
922 GtkBin *bin = GTK_BIN (widget);
927 gint handle_position;
928 GtkOrientation handle_orientation;
930 handle_position = effective_handle_position (hb);
932 gdk_drawable_get_size (priv->bin_window, &width, &height);
935 gtk_paint_box (widget->style,
937 gtk_widget_get_state (widget),
939 area, widget, "handlebox_bin",
942 gtk_paint_box (widget->style,
944 gtk_widget_get_state (widget),
946 &event->area, widget, "handlebox_bin",
949 /* We currently draw the handle _above_ the relief of the handlebox.
950 * it could also be drawn on the same level...
952 priv->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
953 priv->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
957 switch (handle_position)
962 rect.width = DRAG_HANDLE_SIZE;
963 rect.height = height;
964 handle_orientation = GTK_ORIENTATION_VERTICAL;
967 rect.x = width - DRAG_HANDLE_SIZE;
969 rect.width = DRAG_HANDLE_SIZE;
970 rect.height = height;
971 handle_orientation = GTK_ORIENTATION_VERTICAL;
977 rect.height = DRAG_HANDLE_SIZE;
978 handle_orientation = GTK_ORIENTATION_HORIZONTAL;
982 rect.y = height - DRAG_HANDLE_SIZE;
984 rect.height = DRAG_HANDLE_SIZE;
985 handle_orientation = GTK_ORIENTATION_HORIZONTAL;
988 g_assert_not_reached ();
992 if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
993 draw_textured_frame (widget, priv->bin_window, &rect,
995 event ? &event->area : area,
998 child = gtk_bin_get_child (bin);
999 if (child != NULL && gtk_widget_get_visible (child))
1000 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->expose_event (widget, event);
1004 gtk_handle_box_expose (GtkWidget *widget,
1005 GdkEventExpose *event)
1008 GtkHandleBoxPrivate *priv;
1010 if (gtk_widget_is_drawable (widget))
1012 hb = GTK_HANDLE_BOX (widget);
1015 if (event->window == widget->window)
1017 if (priv->child_detached)
1018 gtk_handle_box_draw_ghost (hb);
1021 gtk_handle_box_paint (widget, event, NULL);
1028 gtk_handle_box_get_invisible (void)
1030 static GtkWidget *handle_box_invisible = NULL;
1032 if (!handle_box_invisible)
1034 handle_box_invisible = gtk_invisible_new ();
1035 gtk_widget_show (handle_box_invisible);
1038 return handle_box_invisible;
1042 gtk_handle_box_grab_event (GtkWidget *widget,
1046 GtkHandleBoxPrivate *priv = hb->priv;
1048 switch (event->type)
1050 case GDK_BUTTON_RELEASE:
1051 if (priv->in_drag) /* sanity check */
1053 gtk_handle_box_end_drag (hb, event->button.time);
1058 case GDK_MOTION_NOTIFY:
1059 return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event);
1070 gtk_handle_box_button_press (GtkWidget *widget,
1071 GdkEventButton *event)
1073 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1074 GtkHandleBoxPrivate *priv = hb->priv;
1075 gboolean event_handled;
1077 gint handle_position;
1079 handle_position = effective_handle_position (hb);
1081 event_handled = FALSE;
1082 if ((event->button == 1) &&
1083 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1088 if (event->window != priv->bin_window)
1091 child = gtk_bin_get_child (GTK_BIN (hb));
1097 border_width = gtk_container_get_border_width (GTK_CONTAINER (hb));
1099 switch (handle_position)
1102 in_handle = event->x < DRAG_HANDLE_SIZE;
1105 in_handle = event->y < DRAG_HANDLE_SIZE;
1108 in_handle = event->x > 2 * border_width + child->allocation.width;
1110 case GTK_POS_BOTTOM:
1111 in_handle = event->y > 2 * border_width + child->allocation.height;
1121 event_handled = TRUE;
1126 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
1128 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1129 gint desk_x, desk_y;
1130 gint root_x, root_y;
1133 gtk_invisible_set_screen (GTK_INVISIBLE (invisible),
1134 gtk_widget_get_screen (GTK_WIDGET (hb)));
1135 gdk_window_get_deskrelative_origin (priv->bin_window, &desk_x, &desk_y);
1136 gdk_window_get_origin (priv->bin_window, &root_x, &root_y);
1137 gdk_drawable_get_size (priv->bin_window, &width, &height);
1139 priv->orig_x = event->x_root;
1140 priv->orig_y = event->y_root;
1142 priv->float_allocation.x = root_x - event->x_root;
1143 priv->float_allocation.y = root_y - event->y_root;
1144 priv->float_allocation.width = width;
1145 priv->float_allocation.height = height;
1147 priv->deskoff_x = desk_x - root_x;
1148 priv->deskoff_y = desk_y - root_y;
1150 if (gdk_window_is_viewable (widget->window))
1152 gdk_window_get_origin (widget->window, &root_x, &root_y);
1153 gdk_drawable_get_size (widget->window, &width, &height);
1155 priv->attach_allocation.x = root_x;
1156 priv->attach_allocation.y = root_y;
1157 priv->attach_allocation.width = width;
1158 priv->attach_allocation.height = height;
1162 priv->attach_allocation.x = -1;
1163 priv->attach_allocation.y = -1;
1164 priv->attach_allocation.width = 0;
1165 priv->attach_allocation.height = 0;
1167 priv->in_drag = TRUE;
1168 priv->grab_device = event->device;
1169 fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1171 if (gdk_device_grab (event->device,
1173 GDK_OWNERSHIP_WINDOW,
1175 (GDK_BUTTON1_MOTION_MASK |
1176 GDK_POINTER_MOTION_HINT_MASK |
1177 GDK_BUTTON_RELEASE_MASK),
1179 event->time) != GDK_GRAB_SUCCESS)
1181 priv->in_drag = FALSE;
1182 priv->grab_device = NULL;
1186 gtk_device_grab_add (invisible, priv->grab_device, TRUE);
1187 g_signal_connect (invisible, "event",
1188 G_CALLBACK (gtk_handle_box_grab_event), hb);
1191 gdk_cursor_unref (fleur);
1192 event_handled = TRUE;
1194 else if (priv->child_detached) /* Double click */
1196 gtk_handle_box_reattach (hb);
1201 return event_handled;
1205 gtk_handle_box_motion (GtkWidget *widget,
1206 GdkEventMotion *event)
1208 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1209 GtkHandleBoxPrivate *priv = hb->priv;
1213 gboolean is_snapped = FALSE;
1214 gint handle_position;
1215 GdkGeometry geometry;
1216 GdkScreen *screen, *pointer_screen;
1220 handle_position = effective_handle_position (hb);
1222 /* Calculate the attachment point on the float, if the float
1227 screen = gtk_widget_get_screen (widget);
1228 gdk_display_get_device_state (gdk_screen_get_display (screen),
1231 &new_x, &new_y, NULL);
1232 if (pointer_screen != screen)
1234 new_x = priv->orig_x;
1235 new_y = priv->orig_y;
1238 new_x += priv->float_allocation.x;
1239 new_y += priv->float_allocation.y;
1241 snap_edge = priv->snap_edge;
1242 if (snap_edge == -1)
1243 snap_edge = (handle_position == GTK_POS_LEFT ||
1244 handle_position == GTK_POS_RIGHT) ?
1245 GTK_POS_TOP : GTK_POS_LEFT;
1247 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1251 snap_edge = GTK_POS_RIGHT;
1254 snap_edge = GTK_POS_LEFT;
1260 /* First, check if the snapped edge is aligned
1265 is_snapped = abs (priv->attach_allocation.y - new_y) < TOLERANCE;
1267 case GTK_POS_BOTTOM:
1268 is_snapped = abs (priv->attach_allocation.y + (gint)priv->attach_allocation.height -
1269 new_y - (gint)priv->float_allocation.height) < TOLERANCE;
1272 is_snapped = abs (priv->attach_allocation.x - new_x) < TOLERANCE;
1275 is_snapped = abs (priv->attach_allocation.x + (gint)priv->attach_allocation.width -
1276 new_x - (gint)priv->float_allocation.width) < TOLERANCE;
1280 /* Next, check if coordinates in the other direction are sufficiently
1285 gint float_pos1 = 0; /* Initialize to suppress warnings */
1286 gint float_pos2 = 0;
1287 gint attach_pos1 = 0;
1288 gint attach_pos2 = 0;
1293 case GTK_POS_BOTTOM:
1294 attach_pos1 = priv->attach_allocation.x;
1295 attach_pos2 = priv->attach_allocation.x + priv->attach_allocation.width;
1297 float_pos2 = new_x + priv->float_allocation.width;
1301 attach_pos1 = priv->attach_allocation.y;
1302 attach_pos2 = priv->attach_allocation.y + priv->attach_allocation.height;
1304 float_pos2 = new_y + priv->float_allocation.height;
1308 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1309 (attach_pos2 + TOLERANCE > float_pos2)) ||
1310 ((float_pos1 - TOLERANCE < attach_pos1) &&
1311 (float_pos2 + TOLERANCE > attach_pos2));
1314 child = gtk_bin_get_child (GTK_BIN (hb));
1318 if (priv->child_detached)
1320 priv->child_detached = FALSE;
1321 gdk_window_hide (priv->float_window);
1322 gdk_window_reparent (priv->bin_window, widget->window, 0, 0);
1323 priv->float_window_mapped = FALSE;
1325 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1329 gtk_widget_queue_resize (widget);
1336 gdk_drawable_get_size (priv->float_window, &width, &height);
1337 new_x += priv->deskoff_x;
1338 new_y += priv->deskoff_y;
1340 switch (handle_position)
1343 new_y += ((gint)priv->float_allocation.height - height) / 2;
1346 new_x += (gint)priv->float_allocation.width - width;
1347 new_y += ((gint)priv->float_allocation.height - height) / 2;
1350 new_x += ((gint)priv->float_allocation.width - width) / 2;
1352 case GTK_POS_BOTTOM:
1353 new_x += ((gint)priv->float_allocation.width - width) / 2;
1354 new_y += (gint)priv->float_allocation.height - height;
1358 if (priv->child_detached)
1360 gdk_window_move (priv->float_window, new_x, new_y);
1361 gdk_window_raise (priv->float_window);
1368 GtkRequisition child_requisition;
1370 priv->child_detached = TRUE;
1373 gtk_widget_get_child_requisition (child, &child_requisition);
1376 child_requisition.width = 0;
1377 child_requisition.height = 0;
1380 border_width = gtk_container_get_border_width (GTK_CONTAINER (hb));
1381 width = child_requisition.width + 2 * border_width;
1382 height = child_requisition.height + 2 * border_width;
1384 if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT)
1385 width += DRAG_HANDLE_SIZE;
1387 height += DRAG_HANDLE_SIZE;
1389 gdk_window_move_resize (priv->float_window, new_x, new_y, width, height);
1390 gdk_window_reparent (priv->bin_window, priv->float_window, 0, 0);
1391 gdk_window_set_geometry_hints (priv->float_window, &geometry, GDK_HINT_POS);
1392 gdk_window_show (priv->float_window);
1393 priv->float_window_mapped = TRUE;
1395 /* this extra move is necessary if we use decorations, or our
1396 * window manager insists on decorations.
1398 gdk_display_sync (gtk_widget_get_display (widget));
1399 gdk_window_move (priv->float_window, new_x, new_y);
1400 gdk_display_sync (gtk_widget_get_display (widget));
1403 handle_box_signals[SIGNAL_CHILD_DETACHED],
1406 gtk_handle_box_draw_ghost (hb);
1408 gtk_widget_queue_resize (widget);
1416 gtk_handle_box_add (GtkContainer *container,
1419 GtkHandleBoxPrivate *priv = GTK_HANDLE_BOX (container)->priv;
1421 gtk_widget_set_parent_window (widget, priv->bin_window);
1422 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->add (container, widget);
1426 gtk_handle_box_remove (GtkContainer *container,
1429 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->remove (container, widget);
1431 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1435 gtk_handle_box_delete_event (GtkWidget *widget,
1438 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1439 GtkHandleBoxPrivate *priv = hb->priv;
1441 if (event->window == priv->float_window)
1443 gtk_handle_box_reattach (hb);
1452 gtk_handle_box_reattach (GtkHandleBox *hb)
1454 GtkHandleBoxPrivate *priv = hb->priv;
1456 GtkWidget *widget = GTK_WIDGET (hb);
1458 if (priv->child_detached)
1460 priv->child_detached = FALSE;
1461 if (gtk_widget_get_realized (widget))
1463 gdk_window_hide (priv->float_window);
1464 gdk_window_reparent (priv->bin_window, widget->window, 0, 0);
1466 child = gtk_bin_get_child (GTK_BIN (hb));
1469 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1474 priv->float_window_mapped = FALSE;
1477 gtk_handle_box_end_drag (hb, GDK_CURRENT_TIME);
1479 gtk_widget_queue_resize (GTK_WIDGET (hb));
1483 gtk_handle_box_end_drag (GtkHandleBox *hb,
1486 GtkHandleBoxPrivate *priv = hb->priv;
1487 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1489 priv->in_drag = FALSE;
1491 gtk_device_grab_remove (invisible, priv->grab_device);
1492 gdk_device_ungrab (priv->grab_device, time);
1493 g_signal_handlers_disconnect_by_func (invisible,
1494 G_CALLBACK (gtk_handle_box_grab_event),
1497 priv->grab_device = NULL;