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/.
32 #include "gtkhandlebox.h"
33 #include "gtkinvisible.h"
35 #include "gtkmarshalers.h"
36 #include "gtkwindow.h"
37 #include "gtktypebuiltins.h"
38 #include "gtkprivate.h"
43 struct _GtkHandleBoxPrivate
46 GtkPositionType handle_position;
47 GtkPositionType snap_edge;
48 GtkShadowType shadow_type;
49 gboolean child_detached;
52 GtkAllocation attach_allocation;
53 GtkAllocation float_allocation;
55 GdkDevice *grab_device;
57 GdkWindow *bin_window; /* parent window for children */
58 GdkWindow *float_window;
60 /* Variables used during a drag
65 guint float_window_mapped : 1;
67 guint shrink_on_detach : 1;
79 #define DRAG_HANDLE_SIZE 10
80 #define CHILDLESS_SIZE 25
81 #define GHOST_HEIGHT 3
85 SIGNAL_CHILD_ATTACHED,
86 SIGNAL_CHILD_DETACHED,
90 /* The algorithm for docking and redocking implemented here
91 * has a couple of nice properties:
93 * 1) During a single drag, docking always occurs at the
94 * the same cursor position. This means that the users
95 * motions are reversible, and that you won't
96 * undock/dock oscillations.
98 * 2) Docking generally occurs at user-visible features.
99 * The user, once they figure out to redock, will
100 * have useful information about doing it again in
103 * Please try to preserve these properties if you
104 * change the algorithm. (And the current algorithm
105 * is far from ideal). Briefly, the current algorithm
106 * for deciding whether the handlebox is docked or not:
108 * 1) The decision is done by comparing two rectangles - the
109 * allocation if the widget at the start of the drag,
110 * and the boundary of hb->bin_window at the start of
111 * of the drag offset by the distance that the cursor
114 * 2) These rectangles must have one edge, the "snap_edge"
115 * of the handlebox, aligned within TOLERANCE.
117 * 3) On the other dimension, the extents of one rectangle
118 * must be contained in the extents of the other,
119 * extended by tolerance. That is, either we can have:
121 * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE->
122 * <--------float_window-------------------->
126 * <-TOLERANCE-|------float_window--------------|-TOLERANCE->
127 * <--------bin_window-------------------->
130 static void gtk_handle_box_set_property (GObject *object,
134 static void gtk_handle_box_get_property (GObject *object,
138 static void gtk_handle_box_map (GtkWidget *widget);
139 static void gtk_handle_box_unmap (GtkWidget *widget);
140 static void gtk_handle_box_realize (GtkWidget *widget);
141 static void gtk_handle_box_unrealize (GtkWidget *widget);
142 static void gtk_handle_box_style_updated (GtkWidget *widget);
143 static void gtk_handle_box_size_request (GtkWidget *widget,
144 GtkRequisition *requisition);
145 static void gtk_handle_box_get_preferred_width (GtkWidget *widget,
148 static void gtk_handle_box_get_preferred_height (GtkWidget *widget,
151 static void gtk_handle_box_size_allocate (GtkWidget *widget,
152 GtkAllocation *real_allocation);
153 static void gtk_handle_box_add (GtkContainer *container,
155 static void gtk_handle_box_remove (GtkContainer *container,
157 static gboolean gtk_handle_box_draw (GtkWidget *widget,
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-type",
191 P_("Appearance of the shadow that surrounds the container"),
192 GTK_TYPE_SHADOW_TYPE,
194 GTK_PARAM_READWRITE));
196 g_object_class_install_property (gobject_class,
197 PROP_HANDLE_POSITION,
198 g_param_spec_enum ("handle-position",
199 P_("Handle position"),
200 P_("Position of the handle relative to the child widget"),
201 GTK_TYPE_POSITION_TYPE,
203 GTK_PARAM_READWRITE));
205 g_object_class_install_property (gobject_class,
207 g_param_spec_enum ("snap-edge",
209 P_("Side of the handlebox that's lined up with the docking point to dock the handlebox"),
210 GTK_TYPE_POSITION_TYPE,
212 GTK_PARAM_READWRITE));
214 g_object_class_install_property (gobject_class,
216 g_param_spec_boolean ("snap-edge-set",
218 P_("Whether to use the value from the snap_edge property or a value derived from handle_position"),
220 GTK_PARAM_READWRITE));
222 g_object_class_install_property (gobject_class,
224 g_param_spec_boolean ("child-detached",
225 P_("Child Detached"),
226 P_("A boolean value indicating whether the handlebox's child is attached or detached."),
228 GTK_PARAM_READABLE));
230 widget_class->map = gtk_handle_box_map;
231 widget_class->unmap = gtk_handle_box_unmap;
232 widget_class->realize = gtk_handle_box_realize;
233 widget_class->unrealize = gtk_handle_box_unrealize;
234 widget_class->style_updated = gtk_handle_box_style_updated;
235 widget_class->get_preferred_width = gtk_handle_box_get_preferred_width;
236 widget_class->get_preferred_height = gtk_handle_box_get_preferred_height;
237 widget_class->size_allocate = gtk_handle_box_size_allocate;
238 widget_class->draw = gtk_handle_box_draw;
239 widget_class->button_press_event = gtk_handle_box_button_press;
240 widget_class->delete_event = gtk_handle_box_delete_event;
242 container_class->add = gtk_handle_box_add;
243 container_class->remove = gtk_handle_box_remove;
245 class->child_attached = NULL;
246 class->child_detached = NULL;
248 handle_box_signals[SIGNAL_CHILD_ATTACHED] =
249 g_signal_new (I_("child-attached"),
250 G_OBJECT_CLASS_TYPE (gobject_class),
252 G_STRUCT_OFFSET (GtkHandleBoxClass, child_attached),
254 _gtk_marshal_VOID__OBJECT,
257 handle_box_signals[SIGNAL_CHILD_DETACHED] =
258 g_signal_new (I_("child-detached"),
259 G_OBJECT_CLASS_TYPE (gobject_class),
261 G_STRUCT_OFFSET (GtkHandleBoxClass, child_detached),
263 _gtk_marshal_VOID__OBJECT,
267 g_type_class_add_private (gobject_class, sizeof (GtkHandleBoxPrivate));
271 gtk_handle_box_init (GtkHandleBox *handle_box)
273 GtkHandleBoxPrivate *priv;
274 GtkStyleContext *context;
276 handle_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (handle_box,
278 GtkHandleBoxPrivate);
279 priv = handle_box->priv;
281 gtk_widget_set_has_window (GTK_WIDGET (handle_box), TRUE);
283 priv->bin_window = NULL;
284 priv->float_window = NULL;
285 priv->shadow_type = GTK_SHADOW_OUT;
286 priv->handle_position = GTK_POS_LEFT;
287 priv->float_window_mapped = FALSE;
288 priv->child_detached = FALSE;
289 priv->in_drag = FALSE;
290 priv->shrink_on_detach = TRUE;
291 priv->snap_edge = -1;
293 context = gtk_widget_get_style_context (GTK_WIDGET (handle_box));
294 gtk_style_context_add_class (context, GTK_STYLE_CLASS_DOCK);
298 gtk_handle_box_set_property (GObject *object,
303 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
307 case PROP_SHADOW_TYPE:
308 gtk_handle_box_set_shadow_type (handle_box, g_value_get_enum (value));
310 case PROP_HANDLE_POSITION:
311 gtk_handle_box_set_handle_position (handle_box, g_value_get_enum (value));
314 gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value));
316 case PROP_SNAP_EDGE_SET:
317 if (!g_value_get_boolean (value))
318 gtk_handle_box_set_snap_edge (handle_box, (GtkPositionType)-1);
321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327 gtk_handle_box_get_property (GObject *object,
332 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
333 GtkHandleBoxPrivate *priv = handle_box->priv;
337 case PROP_SHADOW_TYPE:
338 g_value_set_enum (value, priv->shadow_type);
340 case PROP_HANDLE_POSITION:
341 g_value_set_enum (value, priv->handle_position);
344 g_value_set_enum (value,
345 (priv->snap_edge == -1 ?
346 GTK_POS_TOP : priv->snap_edge));
348 case PROP_SNAP_EDGE_SET:
349 g_value_set_boolean (value, priv->snap_edge != -1);
351 case PROP_CHILD_DETACHED:
352 g_value_set_boolean (value, priv->child_detached);
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361 gtk_handle_box_new (void)
363 return g_object_new (GTK_TYPE_HANDLE_BOX, NULL);
367 gtk_handle_box_map (GtkWidget *widget)
369 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
370 GtkHandleBoxPrivate *priv = hb->priv;
371 GtkBin *bin = GTK_BIN (widget);
374 gtk_widget_set_mapped (widget, TRUE);
376 child = gtk_bin_get_child (bin);
378 gtk_widget_get_visible (child) &&
379 !gtk_widget_get_mapped (child))
380 gtk_widget_map (child);
382 if (priv->child_detached && !priv->float_window_mapped)
384 gdk_window_show (priv->float_window);
385 priv->float_window_mapped = TRUE;
388 gdk_window_show (priv->bin_window);
389 gdk_window_show (gtk_widget_get_window (widget));
393 gtk_handle_box_unmap (GtkWidget *widget)
395 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
396 GtkHandleBoxPrivate *priv = hb->priv;
398 gtk_widget_set_mapped (widget, FALSE);
400 gdk_window_hide (gtk_widget_get_window (widget));
401 if (priv->float_window_mapped)
403 gdk_window_hide (priv->float_window);
404 priv->float_window_mapped = FALSE;
407 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unmap (widget);
411 gtk_handle_box_realize (GtkWidget *widget)
413 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
414 GtkHandleBoxPrivate *priv = hb->priv;
415 GtkAllocation allocation;
416 GtkRequisition requisition;
418 GtkStyleContext *context;
421 GdkWindowAttr attributes;
422 gint attributes_mask;
424 gtk_widget_set_realized (widget, TRUE);
426 gtk_widget_get_allocation (widget, &allocation);
428 attributes.x = allocation.x;
429 attributes.y = allocation.y;
430 attributes.width = allocation.width;
431 attributes.height = allocation.height;
432 attributes.window_type = GDK_WINDOW_CHILD;
433 attributes.wclass = GDK_INPUT_OUTPUT;
434 attributes.visual = gtk_widget_get_visual (widget);
435 attributes.event_mask = (gtk_widget_get_events (widget)
436 | GDK_EXPOSURE_MASK);
437 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
439 window = gdk_window_new (gtk_widget_get_parent_window (widget),
440 &attributes, attributes_mask);
441 gtk_widget_set_window (widget, window);
442 gdk_window_set_user_data (window, widget);
446 attributes.width = allocation.width;
447 attributes.height = allocation.height;
448 attributes.window_type = GDK_WINDOW_CHILD;
449 attributes.event_mask = (gtk_widget_get_events (widget) |
451 GDK_BUTTON1_MOTION_MASK |
452 GDK_POINTER_MOTION_HINT_MASK |
453 GDK_BUTTON_PRESS_MASK |
454 GDK_BUTTON_RELEASE_MASK);
455 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
457 priv->bin_window = gdk_window_new (window,
458 &attributes, attributes_mask);
459 gdk_window_set_user_data (priv->bin_window, widget);
461 child = gtk_bin_get_child (GTK_BIN (hb));
463 gtk_widget_set_parent_window (child, priv->bin_window);
465 gtk_widget_get_preferred_size (widget, &requisition, NULL);
469 attributes.width = requisition.width;
470 attributes.height = requisition.height;
471 attributes.window_type = GDK_WINDOW_TOPLEVEL;
472 attributes.wclass = GDK_INPUT_OUTPUT;
473 attributes.visual = gtk_widget_get_visual (widget);
474 attributes.event_mask = (gtk_widget_get_events (widget) |
476 GDK_ENTER_NOTIFY_MASK |
477 GDK_LEAVE_NOTIFY_MASK |
478 GDK_FOCUS_CHANGE_MASK |
480 attributes.type_hint = GDK_WINDOW_TYPE_HINT_TOOLBAR;
481 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_TYPE_HINT;
482 priv->float_window = gdk_window_new (gtk_widget_get_root_window (widget),
483 &attributes, attributes_mask);
484 gdk_window_set_user_data (priv->float_window, widget);
485 gdk_window_set_decorations (priv->float_window, 0);
486 gdk_window_set_type_hint (priv->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
488 context = gtk_widget_get_style_context (widget);
489 state = gtk_widget_get_state_flags (widget);
491 gtk_style_context_set_background (context, window);
492 gtk_style_context_set_background (context, priv->bin_window);
493 gtk_style_context_set_background (context, priv->float_window);
497 gtk_handle_box_unrealize (GtkWidget *widget)
499 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
500 GtkHandleBoxPrivate *priv = hb->priv;
502 gdk_window_set_user_data (priv->bin_window, NULL);
503 gdk_window_destroy (priv->bin_window);
504 priv->bin_window = NULL;
505 gdk_window_set_user_data (priv->float_window, NULL);
506 gdk_window_destroy (priv->float_window);
507 priv->float_window = NULL;
509 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unrealize (widget);
513 gtk_handle_box_style_updated (GtkWidget *widget)
515 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
516 GtkHandleBoxPrivate *priv = hb->priv;
518 if (gtk_widget_get_realized (widget) &&
519 gtk_widget_get_has_window (widget))
522 GtkStyleContext *context;
524 context = gtk_widget_get_style_context (widget);
525 state = gtk_widget_get_state_flags (widget);
527 gtk_style_context_save (context);
528 gtk_style_context_set_state (context, state);
530 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
531 gtk_style_context_set_background (context, priv->bin_window);
532 gtk_style_context_set_background (context, priv->float_window);
534 gtk_style_context_restore (context);
539 effective_handle_position (GtkHandleBox *hb)
541 GtkHandleBoxPrivate *priv = hb->priv;
544 if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
545 handle_position = priv->handle_position;
548 switch (priv->handle_position)
551 handle_position = GTK_POS_RIGHT;
554 handle_position = GTK_POS_LEFT;
557 handle_position = priv->handle_position;
562 return handle_position;
566 gtk_handle_box_size_request (GtkWidget *widget,
567 GtkRequisition *requisition)
569 GtkBin *bin = GTK_BIN (widget);
570 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
571 GtkHandleBoxPrivate *priv = hb->priv;
572 GtkRequisition child_requisition;
574 gint handle_position;
576 handle_position = effective_handle_position (hb);
578 if (handle_position == GTK_POS_LEFT ||
579 handle_position == GTK_POS_RIGHT)
581 requisition->width = DRAG_HANDLE_SIZE;
582 requisition->height = 0;
586 requisition->width = 0;
587 requisition->height = DRAG_HANDLE_SIZE;
590 child = gtk_bin_get_child (bin);
591 /* if our child is not visible, we still request its size, since we
592 * won't have any useful hint for our size otherwise.
596 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
600 child_requisition.width = 0;
601 child_requisition.height = 0;
604 if (priv->child_detached)
606 /* FIXME: This doesn't work currently */
607 if (!priv->shrink_on_detach)
609 if (handle_position == GTK_POS_LEFT ||
610 handle_position == GTK_POS_RIGHT)
611 requisition->height += child_requisition.height;
613 requisition->width += child_requisition.width;
617 GtkStyleContext *context;
621 context = gtk_widget_get_style_context (widget);
622 state = gtk_widget_get_state_flags (widget);
623 gtk_style_context_get_padding (context, state, &padding);
625 if (handle_position == GTK_POS_LEFT ||
626 handle_position == GTK_POS_RIGHT)
627 requisition->height += padding.top;
629 requisition->width += padding.left;
636 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
637 requisition->width += border_width * 2;
638 requisition->height += border_width * 2;
642 requisition->width += child_requisition.width;
643 requisition->height += child_requisition.height;
647 requisition->width += CHILDLESS_SIZE;
648 requisition->height += CHILDLESS_SIZE;
654 gtk_handle_box_get_preferred_width (GtkWidget *widget,
658 GtkRequisition requisition;
660 gtk_handle_box_size_request (widget, &requisition);
662 *minimum = *natural = requisition.width;
666 gtk_handle_box_get_preferred_height (GtkWidget *widget,
670 GtkRequisition requisition;
672 gtk_handle_box_size_request (widget, &requisition);
674 *minimum = *natural = requisition.height;
679 gtk_handle_box_size_allocate (GtkWidget *widget,
680 GtkAllocation *allocation)
682 GtkBin *bin = GTK_BIN (widget);
683 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
684 GtkHandleBoxPrivate *priv = hb->priv;
685 GtkRequisition child_requisition;
687 gint handle_position;
689 handle_position = effective_handle_position (hb);
691 child = gtk_bin_get_child (bin);
695 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
699 child_requisition.width = 0;
700 child_requisition.height = 0;
703 gtk_widget_set_allocation (widget, allocation);
705 if (gtk_widget_get_realized (widget))
706 gdk_window_move_resize (gtk_widget_get_window (widget),
707 allocation->x, allocation->y,
708 allocation->width, allocation->height);
710 if (child != NULL && gtk_widget_get_visible (child))
712 GtkAllocation child_allocation;
715 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
717 child_allocation.x = border_width;
718 child_allocation.y = border_width;
719 if (handle_position == GTK_POS_LEFT)
720 child_allocation.x += DRAG_HANDLE_SIZE;
721 else if (handle_position == GTK_POS_TOP)
722 child_allocation.y += DRAG_HANDLE_SIZE;
724 if (priv->child_detached)
729 child_allocation.width = child_requisition.width;
730 child_allocation.height = child_requisition.height;
732 float_width = child_allocation.width + 2 * border_width;
733 float_height = child_allocation.height + 2 * border_width;
735 if (handle_position == GTK_POS_LEFT ||
736 handle_position == GTK_POS_RIGHT)
737 float_width += DRAG_HANDLE_SIZE;
739 float_height += DRAG_HANDLE_SIZE;
741 if (gtk_widget_get_realized (widget))
743 gdk_window_resize (priv->float_window,
746 gdk_window_move_resize (priv->bin_window,
755 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
756 child_allocation.height = MAX (1, (gint) allocation->height - 2 * border_width);
758 if (handle_position == GTK_POS_LEFT ||
759 handle_position == GTK_POS_RIGHT)
760 child_allocation.width -= DRAG_HANDLE_SIZE;
762 child_allocation.height -= DRAG_HANDLE_SIZE;
764 if (gtk_widget_get_realized (widget))
765 gdk_window_move_resize (priv->bin_window,
772 gtk_widget_size_allocate (child, &child_allocation);
777 gtk_handle_box_draw_ghost (GtkHandleBox *hb,
780 GtkWidget *widget = GTK_WIDGET (hb);
782 GtkStyleContext *context;
788 gint allocation_width;
789 gint allocation_height;
790 gint handle_position;
792 handle_position = effective_handle_position (hb);
793 allocation_width = gtk_widget_get_allocated_width (widget);
794 allocation_height = gtk_widget_get_allocated_height (widget);
796 if (handle_position == GTK_POS_LEFT ||
797 handle_position == GTK_POS_RIGHT)
799 x = handle_position == GTK_POS_LEFT ? 0 : allocation_width - DRAG_HANDLE_SIZE;
801 width = DRAG_HANDLE_SIZE;
802 height = allocation_height;
807 y = handle_position == GTK_POS_TOP ? 0 : allocation_height - DRAG_HANDLE_SIZE;
808 width = allocation_width;
809 height = DRAG_HANDLE_SIZE;
812 context = gtk_widget_get_style_context (widget);
813 window = gtk_widget_get_window (widget);
814 state = gtk_widget_get_state_flags (widget);
816 gtk_style_context_save (context);
817 gtk_style_context_set_state (context, state);
819 gtk_render_background (context, cr, x, y, width, height);
820 gtk_render_frame (context, cr, x, y, width, height);
822 if (handle_position == GTK_POS_LEFT ||
823 handle_position == GTK_POS_RIGHT)
824 gtk_render_line (context, cr,
825 handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
826 allocation_height / 2,
827 handle_position == GTK_POS_LEFT ? allocation_width : allocation_width - DRAG_HANDLE_SIZE,
828 allocation_height / 2);
830 gtk_render_line (context, cr,
831 allocation_width / 2,
832 handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
833 allocation_width / 2,
834 handle_position == GTK_POS_TOP ? allocation_height : allocation_height - DRAG_HANDLE_SIZE);
836 gtk_style_context_restore (context);
840 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
843 GtkHandleBoxPrivate *priv;
845 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
847 priv = handle_box->priv;
849 if ((GtkShadowType) priv->shadow_type != type)
851 priv->shadow_type = type;
852 g_object_notify (G_OBJECT (handle_box), "shadow-type");
853 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
858 * gtk_handle_box_get_shadow_type:
859 * @handle_box: a #GtkHandleBox
861 * Gets the type of shadow drawn around the handle box. See
862 * gtk_handle_box_set_shadow_type().
864 * Return value: the type of shadow currently drawn around the handle box.
867 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
869 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
871 return handle_box->priv->shadow_type;
875 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
876 GtkPositionType position)
878 GtkHandleBoxPrivate *priv;
880 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
882 priv = handle_box->priv;
884 if ((GtkPositionType) priv->handle_position != position)
886 priv->handle_position = position;
887 g_object_notify (G_OBJECT (handle_box), "handle-position");
888 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
893 * gtk_handle_box_get_handle_position:
894 * @handle_box: a #GtkHandleBox
896 * Gets the handle position of the handle box. See
897 * gtk_handle_box_set_handle_position().
899 * Return value: the current handle position.
902 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
904 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
906 return handle_box->priv->handle_position;
910 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
911 GtkPositionType edge)
913 GtkHandleBoxPrivate *priv;
915 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
917 priv = handle_box->priv;
919 if (priv->snap_edge != edge)
921 priv->snap_edge = edge;
923 g_object_freeze_notify (G_OBJECT (handle_box));
924 g_object_notify (G_OBJECT (handle_box), "snap-edge");
925 g_object_notify (G_OBJECT (handle_box), "snap-edge-set");
926 g_object_thaw_notify (G_OBJECT (handle_box));
931 * gtk_handle_box_get_snap_edge:
932 * @handle_box: a #GtkHandleBox
934 * Gets the edge used for determining reattachment of the handle box. See
935 * gtk_handle_box_set_snap_edge().
937 * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
938 * is determined (as per default) from the handle position.
941 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
943 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
945 return handle_box->priv->snap_edge;
949 * gtk_handle_box_get_child_detached:
950 * @handle_box: a #GtkHandleBox
952 * Whether the handlebox's child is currently detached.
954 * Return value: %TRUE if the child is currently detached, otherwise %FALSE
959 gtk_handle_box_get_child_detached (GtkHandleBox *handle_box)
961 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), FALSE);
963 return handle_box->priv->child_detached;
967 gtk_handle_box_paint (GtkWidget *widget,
970 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
971 GtkHandleBoxPrivate *priv = hb->priv;
972 GtkBin *bin = GTK_BIN (widget);
973 GtkStyleContext *context;
978 gint handle_position;
979 GtkOrientation handle_orientation;
981 handle_position = effective_handle_position (hb);
983 width = gdk_window_get_width (priv->bin_window);
984 height = gdk_window_get_height (priv->bin_window);
986 context = gtk_widget_get_style_context (widget);
987 state = gtk_widget_get_state_flags (widget);
989 gtk_style_context_save (context);
990 gtk_style_context_set_state (context, state);
992 gtk_render_background (context, cr, 0, 0, width, height);
993 gtk_render_frame (context, cr, 0, 0, width, height);
995 switch (handle_position)
1000 rect.width = DRAG_HANDLE_SIZE;
1001 rect.height = height;
1002 handle_orientation = GTK_ORIENTATION_VERTICAL;
1005 rect.x = width - DRAG_HANDLE_SIZE;
1007 rect.width = DRAG_HANDLE_SIZE;
1008 rect.height = height;
1009 handle_orientation = GTK_ORIENTATION_VERTICAL;
1015 rect.height = DRAG_HANDLE_SIZE;
1016 handle_orientation = GTK_ORIENTATION_HORIZONTAL;
1018 case GTK_POS_BOTTOM:
1020 rect.y = height - DRAG_HANDLE_SIZE;
1022 rect.height = DRAG_HANDLE_SIZE;
1023 handle_orientation = GTK_ORIENTATION_HORIZONTAL;
1026 g_assert_not_reached ();
1030 gtk_render_handle (context, cr,
1031 rect.x, rect.y, rect.width, rect.height);
1033 child = gtk_bin_get_child (bin);
1034 if (child != NULL && gtk_widget_get_visible (child))
1035 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->draw (widget, cr);
1037 gtk_style_context_restore (context);
1041 gtk_handle_box_draw (GtkWidget *widget,
1044 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1045 GtkHandleBoxPrivate *priv = hb->priv;
1047 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
1049 if (priv->child_detached)
1050 gtk_handle_box_draw_ghost (hb, cr);
1052 else if (gtk_cairo_should_draw_window (cr, priv->bin_window))
1053 gtk_handle_box_paint (widget, cr);
1059 gtk_handle_box_get_invisible (void)
1061 static GtkWidget *handle_box_invisible = NULL;
1063 if (!handle_box_invisible)
1065 handle_box_invisible = gtk_invisible_new ();
1066 gtk_widget_show (handle_box_invisible);
1069 return handle_box_invisible;
1073 gtk_handle_box_grab_event (GtkWidget *widget,
1077 GtkHandleBoxPrivate *priv = hb->priv;
1079 switch (event->type)
1081 case GDK_BUTTON_RELEASE:
1082 if (priv->in_drag) /* sanity check */
1084 gtk_handle_box_end_drag (hb, event->button.time);
1089 case GDK_MOTION_NOTIFY:
1090 return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event);
1101 gtk_handle_box_button_press (GtkWidget *widget,
1102 GdkEventButton *event)
1104 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1105 GtkHandleBoxPrivate *priv = hb->priv;
1106 gboolean event_handled;
1108 gint handle_position;
1110 handle_position = effective_handle_position (hb);
1112 event_handled = FALSE;
1113 if ((event->button == 1) &&
1114 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1119 if (event->window != priv->bin_window)
1122 child = gtk_bin_get_child (GTK_BIN (hb));
1126 GtkAllocation child_allocation;
1129 gtk_widget_get_allocation (child, &child_allocation);
1130 border_width = gtk_container_get_border_width (GTK_CONTAINER (hb));
1132 switch (handle_position)
1135 in_handle = event->x < DRAG_HANDLE_SIZE;
1138 in_handle = event->y < DRAG_HANDLE_SIZE;
1141 in_handle = event->x > 2 * border_width + child_allocation.width;
1143 case GTK_POS_BOTTOM:
1144 in_handle = event->y > 2 * border_width + child_allocation.height;
1154 event_handled = TRUE;
1159 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
1161 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1163 gint root_x, root_y;
1165 gtk_invisible_set_screen (GTK_INVISIBLE (invisible),
1166 gtk_widget_get_screen (GTK_WIDGET (hb)));
1167 gdk_window_get_origin (priv->bin_window, &root_x, &root_y);
1169 priv->orig_x = event->x_root;
1170 priv->orig_y = event->y_root;
1172 priv->float_allocation.x = root_x - event->x_root;
1173 priv->float_allocation.y = root_y - event->y_root;
1174 priv->float_allocation.width = gdk_window_get_width (priv->bin_window);
1175 priv->float_allocation.height = gdk_window_get_height (priv->bin_window);
1177 window = gtk_widget_get_window (widget);
1178 if (gdk_window_is_viewable (window))
1180 gdk_window_get_origin (window, &root_x, &root_y);
1182 priv->attach_allocation.x = root_x;
1183 priv->attach_allocation.y = root_y;
1184 priv->attach_allocation.width = gdk_window_get_width (window);
1185 priv->attach_allocation.height = gdk_window_get_height (window);
1189 priv->attach_allocation.x = -1;
1190 priv->attach_allocation.y = -1;
1191 priv->attach_allocation.width = 0;
1192 priv->attach_allocation.height = 0;
1194 priv->in_drag = TRUE;
1195 priv->grab_device = event->device;
1196 fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1198 if (gdk_device_grab (event->device,
1199 gtk_widget_get_window (invisible),
1200 GDK_OWNERSHIP_WINDOW,
1202 (GDK_BUTTON1_MOTION_MASK |
1203 GDK_POINTER_MOTION_HINT_MASK |
1204 GDK_BUTTON_RELEASE_MASK),
1206 event->time) != GDK_GRAB_SUCCESS)
1208 priv->in_drag = FALSE;
1209 priv->grab_device = NULL;
1213 gtk_device_grab_add (invisible, priv->grab_device, TRUE);
1214 g_signal_connect (invisible, "event",
1215 G_CALLBACK (gtk_handle_box_grab_event), hb);
1218 g_object_unref (fleur);
1219 event_handled = TRUE;
1221 else if (priv->child_detached) /* Double click */
1223 gtk_handle_box_reattach (hb);
1228 return event_handled;
1232 gtk_handle_box_motion (GtkWidget *widget,
1233 GdkEventMotion *event)
1235 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1236 GtkHandleBoxPrivate *priv = hb->priv;
1240 gboolean is_snapped = FALSE;
1241 gint handle_position;
1242 GdkGeometry geometry;
1243 GdkScreen *screen, *pointer_screen;
1247 handle_position = effective_handle_position (hb);
1249 /* Calculate the attachment point on the float, if the float
1254 screen = gtk_widget_get_screen (widget);
1255 gdk_device_get_position (event->device,
1258 if (pointer_screen != screen)
1260 new_x = priv->orig_x;
1261 new_y = priv->orig_y;
1264 new_x += priv->float_allocation.x;
1265 new_y += priv->float_allocation.y;
1267 snap_edge = priv->snap_edge;
1268 if (snap_edge == -1)
1269 snap_edge = (handle_position == GTK_POS_LEFT ||
1270 handle_position == GTK_POS_RIGHT) ?
1271 GTK_POS_TOP : GTK_POS_LEFT;
1273 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1277 snap_edge = GTK_POS_RIGHT;
1280 snap_edge = GTK_POS_LEFT;
1286 /* First, check if the snapped edge is aligned
1291 is_snapped = abs (priv->attach_allocation.y - new_y) < TOLERANCE;
1293 case GTK_POS_BOTTOM:
1294 is_snapped = abs (priv->attach_allocation.y + (gint)priv->attach_allocation.height -
1295 new_y - (gint)priv->float_allocation.height) < TOLERANCE;
1298 is_snapped = abs (priv->attach_allocation.x - new_x) < TOLERANCE;
1301 is_snapped = abs (priv->attach_allocation.x + (gint)priv->attach_allocation.width -
1302 new_x - (gint)priv->float_allocation.width) < TOLERANCE;
1306 /* Next, check if coordinates in the other direction are sufficiently
1311 gint float_pos1 = 0; /* Initialize to suppress warnings */
1312 gint float_pos2 = 0;
1313 gint attach_pos1 = 0;
1314 gint attach_pos2 = 0;
1319 case GTK_POS_BOTTOM:
1320 attach_pos1 = priv->attach_allocation.x;
1321 attach_pos2 = priv->attach_allocation.x + priv->attach_allocation.width;
1323 float_pos2 = new_x + priv->float_allocation.width;
1327 attach_pos1 = priv->attach_allocation.y;
1328 attach_pos2 = priv->attach_allocation.y + priv->attach_allocation.height;
1330 float_pos2 = new_y + priv->float_allocation.height;
1334 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1335 (attach_pos2 + TOLERANCE > float_pos2)) ||
1336 ((float_pos1 - TOLERANCE < attach_pos1) &&
1337 (float_pos2 + TOLERANCE > attach_pos2));
1340 child = gtk_bin_get_child (GTK_BIN (hb));
1344 if (priv->child_detached)
1346 priv->child_detached = FALSE;
1347 gdk_window_hide (priv->float_window);
1348 gdk_window_reparent (priv->bin_window, gtk_widget_get_window (widget), 0, 0);
1349 priv->float_window_mapped = FALSE;
1351 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1355 gtk_widget_queue_resize (widget);
1362 width = gdk_window_get_width (priv->float_window);
1363 height = gdk_window_get_height (priv->float_window);
1365 switch (handle_position)
1368 new_y += ((gint)priv->float_allocation.height - height) / 2;
1371 new_x += (gint)priv->float_allocation.width - width;
1372 new_y += ((gint)priv->float_allocation.height - height) / 2;
1375 new_x += ((gint)priv->float_allocation.width - width) / 2;
1377 case GTK_POS_BOTTOM:
1378 new_x += ((gint)priv->float_allocation.width - width) / 2;
1379 new_y += (gint)priv->float_allocation.height - height;
1383 if (priv->child_detached)
1385 gdk_window_move (priv->float_window, new_x, new_y);
1386 gdk_window_raise (priv->float_window);
1391 GtkRequisition child_requisition;
1393 priv->child_detached = TRUE;
1397 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
1401 child_requisition.width = 0;
1402 child_requisition.height = 0;
1405 border_width = gtk_container_get_border_width (GTK_CONTAINER (hb));
1406 width = child_requisition.width + 2 * border_width;
1407 height = child_requisition.height + 2 * border_width;
1409 if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT)
1410 width += DRAG_HANDLE_SIZE;
1412 height += DRAG_HANDLE_SIZE;
1414 gdk_window_move_resize (priv->float_window, new_x, new_y, width, height);
1415 gdk_window_reparent (priv->bin_window, priv->float_window, 0, 0);
1416 gdk_window_set_geometry_hints (priv->float_window, &geometry, GDK_HINT_POS);
1417 gdk_window_show (priv->float_window);
1418 priv->float_window_mapped = TRUE;
1420 /* this extra move is necessary if we use decorations, or our
1421 * window manager insists on decorations.
1423 gdk_display_sync (gtk_widget_get_display (widget));
1424 gdk_window_move (priv->float_window, new_x, new_y);
1425 gdk_display_sync (gtk_widget_get_display (widget));
1428 handle_box_signals[SIGNAL_CHILD_DETACHED],
1432 gtk_widget_queue_resize (widget);
1440 gtk_handle_box_add (GtkContainer *container,
1443 GtkHandleBoxPrivate *priv = GTK_HANDLE_BOX (container)->priv;
1445 gtk_widget_set_parent_window (widget, priv->bin_window);
1446 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->add (container, widget);
1450 gtk_handle_box_remove (GtkContainer *container,
1453 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->remove (container, widget);
1455 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1459 gtk_handle_box_delete_event (GtkWidget *widget,
1462 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1463 GtkHandleBoxPrivate *priv = hb->priv;
1465 if (event->window == priv->float_window)
1467 gtk_handle_box_reattach (hb);
1476 gtk_handle_box_reattach (GtkHandleBox *hb)
1478 GtkHandleBoxPrivate *priv = hb->priv;
1480 GtkWidget *widget = GTK_WIDGET (hb);
1482 if (priv->child_detached)
1484 priv->child_detached = FALSE;
1485 if (gtk_widget_get_realized (widget))
1487 gdk_window_hide (priv->float_window);
1488 gdk_window_reparent (priv->bin_window, gtk_widget_get_window (widget),
1491 child = gtk_bin_get_child (GTK_BIN (hb));
1494 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1499 priv->float_window_mapped = FALSE;
1502 gtk_handle_box_end_drag (hb, GDK_CURRENT_TIME);
1504 gtk_widget_queue_resize (GTK_WIDGET (hb));
1508 gtk_handle_box_end_drag (GtkHandleBox *hb,
1511 GtkHandleBoxPrivate *priv = hb->priv;
1512 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1514 priv->in_drag = FALSE;
1516 gtk_device_grab_remove (invisible, priv->grab_device);
1517 gdk_device_ungrab (priv->grab_device, time);
1518 g_signal_handlers_disconnect_by_func (invisible,
1519 G_CALLBACK (gtk_handle_box_grab_event),
1522 priv->grab_device = NULL;