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, see <http://www.gnu.org/licenses/>.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #define GDK_DISABLE_DEPRECATION_WARNINGS
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 * SECTION:gtkhandlebox
44 * @Short_description: a widget for detachable window portions
45 * @Title: GtkHandleBox
47 * The #GtkHandleBox widget allows a portion of a window to be "torn
48 * off". It is a bin widget which displays its child and a handle that
49 * the user can drag to tear off a separate window (the <firstterm>float
50 * window</firstterm>) containing the child widget. A thin
51 * <firstterm>ghost</firstterm> is drawn in the original location of the
52 * handlebox. By dragging the separate window back to its original
53 * location, it can be reattached.
55 * When reattaching, the ghost and float window, must be aligned
56 * along one of the edges, the <firstterm>snap edge</firstterm>.
57 * This either can be specified by the application programmer
58 * explicitely, or GTK+ will pick a reasonable default based
59 * on the handle position.
61 * To make detaching and reattaching the handlebox as minimally confusing
62 * as possible to the user, it is important to set the snap edge so that
63 * the snap edge does not move when the handlebox is deattached. For
64 * instance, if the handlebox is packed at the bottom of a VBox, then
65 * when the handlebox is detached, the bottom edge of the handlebox's
66 * allocation will remain fixed as the height of the handlebox shrinks,
67 * so the snap edge should be set to %GTK_POS_BOTTOM.
70 * #GtkHandleBox has been deprecated. It is very specialized, lacks features
71 * to make it useful and most importantly does not fit well into modern
72 * application design. Do not use it. There is no replacement.
77 struct _GtkHandleBoxPrivate
80 GtkPositionType handle_position;
81 GtkPositionType snap_edge;
82 GtkShadowType shadow_type;
83 gboolean child_detached;
86 GtkAllocation attach_allocation;
87 GtkAllocation float_allocation;
89 GdkDevice *grab_device;
91 GdkWindow *bin_window; /* parent window for children */
92 GdkWindow *float_window;
94 /* Variables used during a drag
99 guint float_window_mapped : 1;
101 guint shrink_on_detach : 1;
107 PROP_HANDLE_POSITION,
113 #define DRAG_HANDLE_SIZE 10
114 #define CHILDLESS_SIZE 25
115 #define GHOST_HEIGHT 3
119 SIGNAL_CHILD_ATTACHED,
120 SIGNAL_CHILD_DETACHED,
124 /* The algorithm for docking and redocking implemented here
125 * has a couple of nice properties:
127 * 1) During a single drag, docking always occurs at the
128 * the same cursor position. This means that the users
129 * motions are reversible, and that you won't
130 * undock/dock oscillations.
132 * 2) Docking generally occurs at user-visible features.
133 * The user, once they figure out to redock, will
134 * have useful information about doing it again in
137 * Please try to preserve these properties if you
138 * change the algorithm. (And the current algorithm
139 * is far from ideal). Briefly, the current algorithm
140 * for deciding whether the handlebox is docked or not:
142 * 1) The decision is done by comparing two rectangles - the
143 * allocation if the widget at the start of the drag,
144 * and the boundary of hb->bin_window at the start of
145 * of the drag offset by the distance that the cursor
148 * 2) These rectangles must have one edge, the "snap_edge"
149 * of the handlebox, aligned within TOLERANCE.
151 * 3) On the other dimension, the extents of one rectangle
152 * must be contained in the extents of the other,
153 * extended by tolerance. That is, either we can have:
155 * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE->
156 * <--------float_window-------------------->
160 * <-TOLERANCE-|------float_window--------------|-TOLERANCE->
161 * <--------bin_window-------------------->
164 static void gtk_handle_box_set_property (GObject *object,
168 static void gtk_handle_box_get_property (GObject *object,
172 static void gtk_handle_box_map (GtkWidget *widget);
173 static void gtk_handle_box_unmap (GtkWidget *widget);
174 static void gtk_handle_box_realize (GtkWidget *widget);
175 static void gtk_handle_box_unrealize (GtkWidget *widget);
176 static void gtk_handle_box_style_updated (GtkWidget *widget);
177 static void gtk_handle_box_size_request (GtkWidget *widget,
178 GtkRequisition *requisition);
179 static void gtk_handle_box_get_preferred_width (GtkWidget *widget,
182 static void gtk_handle_box_get_preferred_height (GtkWidget *widget,
185 static void gtk_handle_box_size_allocate (GtkWidget *widget,
186 GtkAllocation *real_allocation);
187 static void gtk_handle_box_add (GtkContainer *container,
189 static void gtk_handle_box_remove (GtkContainer *container,
191 static gboolean gtk_handle_box_draw (GtkWidget *widget,
193 static gboolean gtk_handle_box_button_press (GtkWidget *widget,
194 GdkEventButton *event);
195 static gboolean gtk_handle_box_motion (GtkWidget *widget,
196 GdkEventMotion *event);
197 static gboolean gtk_handle_box_delete_event (GtkWidget *widget,
199 static void gtk_handle_box_reattach (GtkHandleBox *hb);
200 static void gtk_handle_box_end_drag (GtkHandleBox *hb,
203 static guint handle_box_signals[SIGNAL_LAST] = { 0 };
205 G_DEFINE_TYPE (GtkHandleBox, gtk_handle_box, GTK_TYPE_BIN)
208 gtk_handle_box_class_init (GtkHandleBoxClass *class)
210 GObjectClass *gobject_class;
211 GtkWidgetClass *widget_class;
212 GtkContainerClass *container_class;
214 gobject_class = (GObjectClass *) class;
215 widget_class = (GtkWidgetClass *) class;
216 container_class = (GtkContainerClass *) class;
218 gobject_class->set_property = gtk_handle_box_set_property;
219 gobject_class->get_property = gtk_handle_box_get_property;
221 g_object_class_install_property (gobject_class,
223 g_param_spec_enum ("shadow-type",
225 P_("Appearance of the shadow that surrounds the container"),
226 GTK_TYPE_SHADOW_TYPE,
228 GTK_PARAM_READWRITE));
230 g_object_class_install_property (gobject_class,
231 PROP_HANDLE_POSITION,
232 g_param_spec_enum ("handle-position",
233 P_("Handle position"),
234 P_("Position of the handle relative to the child widget"),
235 GTK_TYPE_POSITION_TYPE,
237 GTK_PARAM_READWRITE));
239 g_object_class_install_property (gobject_class,
241 g_param_spec_enum ("snap-edge",
243 P_("Side of the handlebox that's lined up with the docking point to dock the handlebox"),
244 GTK_TYPE_POSITION_TYPE,
246 GTK_PARAM_READWRITE));
248 g_object_class_install_property (gobject_class,
250 g_param_spec_boolean ("snap-edge-set",
252 P_("Whether to use the value from the snap_edge property or a value derived from handle_position"),
254 GTK_PARAM_READWRITE));
256 g_object_class_install_property (gobject_class,
258 g_param_spec_boolean ("child-detached",
259 P_("Child Detached"),
260 P_("A boolean value indicating whether the handlebox's child is attached or detached."),
262 GTK_PARAM_READABLE));
264 widget_class->map = gtk_handle_box_map;
265 widget_class->unmap = gtk_handle_box_unmap;
266 widget_class->realize = gtk_handle_box_realize;
267 widget_class->unrealize = gtk_handle_box_unrealize;
268 widget_class->style_updated = gtk_handle_box_style_updated;
269 widget_class->get_preferred_width = gtk_handle_box_get_preferred_width;
270 widget_class->get_preferred_height = gtk_handle_box_get_preferred_height;
271 widget_class->size_allocate = gtk_handle_box_size_allocate;
272 widget_class->draw = gtk_handle_box_draw;
273 widget_class->button_press_event = gtk_handle_box_button_press;
274 widget_class->delete_event = gtk_handle_box_delete_event;
276 container_class->add = gtk_handle_box_add;
277 container_class->remove = gtk_handle_box_remove;
279 class->child_attached = NULL;
280 class->child_detached = NULL;
283 * GtkHandleBox::child-attached:
284 * @handlebox: the object which received the signal.
285 * @widget: the child widget of the handlebox.
286 * (this argument provides no extra information
287 * and is here only for backwards-compatibility)
289 * This signal is emitted when the contents of the
290 * handlebox are reattached to the main window.
292 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
294 handle_box_signals[SIGNAL_CHILD_ATTACHED] =
295 g_signal_new (I_("child-attached"),
296 G_OBJECT_CLASS_TYPE (gobject_class),
298 G_STRUCT_OFFSET (GtkHandleBoxClass, child_attached),
300 _gtk_marshal_VOID__OBJECT,
305 * GtkHandleBox::child-detached:
306 * @handlebox: the object which received the signal.
307 * @widget: the child widget of the handlebox.
308 * (this argument provides no extra information
309 * and is here only for backwards-compatibility)
311 * This signal is emitted when the contents of the
312 * handlebox are detached from the main window.
314 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
316 handle_box_signals[SIGNAL_CHILD_DETACHED] =
317 g_signal_new (I_("child-detached"),
318 G_OBJECT_CLASS_TYPE (gobject_class),
320 G_STRUCT_OFFSET (GtkHandleBoxClass, child_detached),
322 _gtk_marshal_VOID__OBJECT,
326 g_type_class_add_private (gobject_class, sizeof (GtkHandleBoxPrivate));
330 gtk_handle_box_init (GtkHandleBox *handle_box)
332 GtkHandleBoxPrivate *priv;
333 GtkStyleContext *context;
335 handle_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (handle_box,
337 GtkHandleBoxPrivate);
338 priv = handle_box->priv;
340 gtk_widget_set_has_window (GTK_WIDGET (handle_box), TRUE);
342 priv->bin_window = NULL;
343 priv->float_window = NULL;
344 priv->shadow_type = GTK_SHADOW_OUT;
345 priv->handle_position = GTK_POS_LEFT;
346 priv->float_window_mapped = FALSE;
347 priv->child_detached = FALSE;
348 priv->in_drag = FALSE;
349 priv->shrink_on_detach = TRUE;
350 priv->snap_edge = -1;
352 context = gtk_widget_get_style_context (GTK_WIDGET (handle_box));
353 gtk_style_context_add_class (context, GTK_STYLE_CLASS_DOCK);
357 gtk_handle_box_set_property (GObject *object,
362 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
366 case PROP_SHADOW_TYPE:
367 gtk_handle_box_set_shadow_type (handle_box, g_value_get_enum (value));
369 case PROP_HANDLE_POSITION:
370 gtk_handle_box_set_handle_position (handle_box, g_value_get_enum (value));
373 gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value));
375 case PROP_SNAP_EDGE_SET:
376 if (!g_value_get_boolean (value))
377 gtk_handle_box_set_snap_edge (handle_box, (GtkPositionType)-1);
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
386 gtk_handle_box_get_property (GObject *object,
391 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
392 GtkHandleBoxPrivate *priv = handle_box->priv;
396 case PROP_SHADOW_TYPE:
397 g_value_set_enum (value, priv->shadow_type);
399 case PROP_HANDLE_POSITION:
400 g_value_set_enum (value, priv->handle_position);
403 g_value_set_enum (value,
404 (priv->snap_edge == -1 ?
405 GTK_POS_TOP : priv->snap_edge));
407 case PROP_SNAP_EDGE_SET:
408 g_value_set_boolean (value, priv->snap_edge != -1);
410 case PROP_CHILD_DETACHED:
411 g_value_set_boolean (value, priv->child_detached);
414 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
420 * gtk_handle_box_new:
422 * Create a new handle box.
424 * Returns: a new #GtkHandleBox.
426 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
429 gtk_handle_box_new (void)
431 return g_object_new (GTK_TYPE_HANDLE_BOX, NULL);
435 gtk_handle_box_map (GtkWidget *widget)
437 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
438 GtkHandleBoxPrivate *priv = hb->priv;
439 GtkBin *bin = GTK_BIN (widget);
442 gtk_widget_set_mapped (widget, TRUE);
444 child = gtk_bin_get_child (bin);
446 gtk_widget_get_visible (child) &&
447 !gtk_widget_get_mapped (child))
448 gtk_widget_map (child);
450 if (priv->child_detached && !priv->float_window_mapped)
452 gdk_window_show (priv->float_window);
453 priv->float_window_mapped = TRUE;
456 gdk_window_show (priv->bin_window);
457 gdk_window_show (gtk_widget_get_window (widget));
461 gtk_handle_box_unmap (GtkWidget *widget)
463 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
464 GtkHandleBoxPrivate *priv = hb->priv;
466 gtk_widget_set_mapped (widget, FALSE);
468 gdk_window_hide (gtk_widget_get_window (widget));
469 if (priv->float_window_mapped)
471 gdk_window_hide (priv->float_window);
472 priv->float_window_mapped = FALSE;
475 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unmap (widget);
479 gtk_handle_box_realize (GtkWidget *widget)
481 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
482 GtkHandleBoxPrivate *priv = hb->priv;
483 GtkAllocation allocation;
484 GtkRequisition requisition;
485 GtkStyleContext *context;
488 GdkWindowAttr attributes;
489 gint attributes_mask;
491 gtk_widget_set_realized (widget, TRUE);
493 gtk_widget_get_allocation (widget, &allocation);
495 attributes.x = allocation.x;
496 attributes.y = allocation.y;
497 attributes.width = allocation.width;
498 attributes.height = allocation.height;
499 attributes.window_type = GDK_WINDOW_CHILD;
500 attributes.wclass = GDK_INPUT_OUTPUT;
501 attributes.visual = gtk_widget_get_visual (widget);
502 attributes.event_mask = (gtk_widget_get_events (widget)
503 | GDK_EXPOSURE_MASK);
504 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
506 window = gdk_window_new (gtk_widget_get_parent_window (widget),
507 &attributes, attributes_mask);
508 gtk_widget_set_window (widget, window);
509 gdk_window_set_user_data (window, widget);
513 attributes.width = allocation.width;
514 attributes.height = allocation.height;
515 attributes.window_type = GDK_WINDOW_CHILD;
516 attributes.event_mask = (gtk_widget_get_events (widget)
518 | GDK_BUTTON1_MOTION_MASK
519 | GDK_POINTER_MOTION_HINT_MASK
520 | GDK_BUTTON_PRESS_MASK
521 | GDK_BUTTON_RELEASE_MASK);
522 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
524 priv->bin_window = gdk_window_new (window,
525 &attributes, attributes_mask);
526 gdk_window_set_user_data (priv->bin_window, widget);
528 child = gtk_bin_get_child (GTK_BIN (hb));
530 gtk_widget_set_parent_window (child, priv->bin_window);
532 gtk_widget_get_preferred_size (widget, &requisition, NULL);
536 attributes.width = requisition.width;
537 attributes.height = requisition.height;
538 attributes.window_type = GDK_WINDOW_TOPLEVEL;
539 attributes.wclass = GDK_INPUT_OUTPUT;
540 attributes.visual = gtk_widget_get_visual (widget);
541 attributes.event_mask = (gtk_widget_get_events (widget)
543 | GDK_ENTER_NOTIFY_MASK
544 | GDK_LEAVE_NOTIFY_MASK
545 | GDK_FOCUS_CHANGE_MASK
546 | GDK_STRUCTURE_MASK);
547 attributes.type_hint = GDK_WINDOW_TYPE_HINT_TOOLBAR;
548 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_TYPE_HINT;
549 priv->float_window = gdk_window_new (gtk_widget_get_root_window (widget),
550 &attributes, attributes_mask);
551 gdk_window_set_user_data (priv->float_window, widget);
552 gdk_window_set_decorations (priv->float_window, 0);
553 gdk_window_set_type_hint (priv->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
555 context = gtk_widget_get_style_context (widget);
556 gtk_style_context_set_background (context, window);
557 gtk_style_context_set_background (context, priv->bin_window);
558 gtk_style_context_set_background (context, priv->float_window);
562 gtk_handle_box_unrealize (GtkWidget *widget)
564 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
565 GtkHandleBoxPrivate *priv = hb->priv;
567 gdk_window_set_user_data (priv->bin_window, NULL);
568 gdk_window_destroy (priv->bin_window);
569 priv->bin_window = NULL;
570 gdk_window_set_user_data (priv->float_window, NULL);
571 gdk_window_destroy (priv->float_window);
572 priv->float_window = NULL;
574 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unrealize (widget);
578 gtk_handle_box_style_updated (GtkWidget *widget)
580 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
581 GtkHandleBoxPrivate *priv = hb->priv;
583 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->style_updated (widget);
585 if (gtk_widget_get_realized (widget) &&
586 gtk_widget_get_has_window (widget))
589 GtkStyleContext *context;
591 context = gtk_widget_get_style_context (widget);
592 state = gtk_widget_get_state_flags (widget);
594 gtk_style_context_save (context);
595 gtk_style_context_set_state (context, state);
597 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
598 gtk_style_context_set_background (context, priv->bin_window);
599 gtk_style_context_set_background (context, priv->float_window);
601 gtk_style_context_restore (context);
606 effective_handle_position (GtkHandleBox *hb)
608 GtkHandleBoxPrivate *priv = hb->priv;
611 if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
612 handle_position = priv->handle_position;
615 switch (priv->handle_position)
618 handle_position = GTK_POS_RIGHT;
621 handle_position = GTK_POS_LEFT;
624 handle_position = priv->handle_position;
629 return handle_position;
633 gtk_handle_box_size_request (GtkWidget *widget,
634 GtkRequisition *requisition)
636 GtkBin *bin = GTK_BIN (widget);
637 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
638 GtkHandleBoxPrivate *priv = hb->priv;
639 GtkRequisition child_requisition;
641 gint handle_position;
643 handle_position = effective_handle_position (hb);
645 if (handle_position == GTK_POS_LEFT ||
646 handle_position == GTK_POS_RIGHT)
648 requisition->width = DRAG_HANDLE_SIZE;
649 requisition->height = 0;
653 requisition->width = 0;
654 requisition->height = DRAG_HANDLE_SIZE;
657 child = gtk_bin_get_child (bin);
658 /* if our child is not visible, we still request its size, since we
659 * won't have any useful hint for our size otherwise.
663 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
667 child_requisition.width = 0;
668 child_requisition.height = 0;
671 if (priv->child_detached)
673 /* FIXME: This doesn't work currently */
674 if (!priv->shrink_on_detach)
676 if (handle_position == GTK_POS_LEFT ||
677 handle_position == GTK_POS_RIGHT)
678 requisition->height += child_requisition.height;
680 requisition->width += child_requisition.width;
684 GtkStyleContext *context;
688 context = gtk_widget_get_style_context (widget);
689 state = gtk_widget_get_state_flags (widget);
690 gtk_style_context_get_padding (context, state, &padding);
692 if (handle_position == GTK_POS_LEFT ||
693 handle_position == GTK_POS_RIGHT)
694 requisition->height += padding.top;
696 requisition->width += padding.left;
703 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
704 requisition->width += border_width * 2;
705 requisition->height += border_width * 2;
709 requisition->width += child_requisition.width;
710 requisition->height += child_requisition.height;
714 requisition->width += CHILDLESS_SIZE;
715 requisition->height += CHILDLESS_SIZE;
721 gtk_handle_box_get_preferred_width (GtkWidget *widget,
725 GtkRequisition requisition;
727 gtk_handle_box_size_request (widget, &requisition);
729 *minimum = *natural = requisition.width;
733 gtk_handle_box_get_preferred_height (GtkWidget *widget,
737 GtkRequisition requisition;
739 gtk_handle_box_size_request (widget, &requisition);
741 *minimum = *natural = requisition.height;
746 gtk_handle_box_size_allocate (GtkWidget *widget,
747 GtkAllocation *allocation)
749 GtkBin *bin = GTK_BIN (widget);
750 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
751 GtkHandleBoxPrivate *priv = hb->priv;
752 GtkRequisition child_requisition;
754 gint handle_position;
756 handle_position = effective_handle_position (hb);
758 child = gtk_bin_get_child (bin);
762 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
766 child_requisition.width = 0;
767 child_requisition.height = 0;
770 gtk_widget_set_allocation (widget, allocation);
772 if (gtk_widget_get_realized (widget))
773 gdk_window_move_resize (gtk_widget_get_window (widget),
774 allocation->x, allocation->y,
775 allocation->width, allocation->height);
777 if (child != NULL && gtk_widget_get_visible (child))
779 GtkAllocation child_allocation;
782 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
784 child_allocation.x = border_width;
785 child_allocation.y = border_width;
786 if (handle_position == GTK_POS_LEFT)
787 child_allocation.x += DRAG_HANDLE_SIZE;
788 else if (handle_position == GTK_POS_TOP)
789 child_allocation.y += DRAG_HANDLE_SIZE;
791 if (priv->child_detached)
796 child_allocation.width = child_requisition.width;
797 child_allocation.height = child_requisition.height;
799 float_width = child_allocation.width + 2 * border_width;
800 float_height = child_allocation.height + 2 * border_width;
802 if (handle_position == GTK_POS_LEFT ||
803 handle_position == GTK_POS_RIGHT)
804 float_width += DRAG_HANDLE_SIZE;
806 float_height += DRAG_HANDLE_SIZE;
808 if (gtk_widget_get_realized (widget))
810 gdk_window_resize (priv->float_window,
813 gdk_window_move_resize (priv->bin_window,
822 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
823 child_allocation.height = MAX (1, (gint) allocation->height - 2 * border_width);
825 if (handle_position == GTK_POS_LEFT ||
826 handle_position == GTK_POS_RIGHT)
827 child_allocation.width -= DRAG_HANDLE_SIZE;
829 child_allocation.height -= DRAG_HANDLE_SIZE;
831 if (gtk_widget_get_realized (widget))
832 gdk_window_move_resize (priv->bin_window,
839 gtk_widget_size_allocate (child, &child_allocation);
844 gtk_handle_box_draw_ghost (GtkHandleBox *hb,
847 GtkWidget *widget = GTK_WIDGET (hb);
849 GtkStyleContext *context;
854 gint allocation_width;
855 gint allocation_height;
856 gint handle_position;
858 handle_position = effective_handle_position (hb);
859 allocation_width = gtk_widget_get_allocated_width (widget);
860 allocation_height = gtk_widget_get_allocated_height (widget);
862 if (handle_position == GTK_POS_LEFT ||
863 handle_position == GTK_POS_RIGHT)
865 x = handle_position == GTK_POS_LEFT ? 0 : allocation_width - DRAG_HANDLE_SIZE;
867 width = DRAG_HANDLE_SIZE;
868 height = allocation_height;
873 y = handle_position == GTK_POS_TOP ? 0 : allocation_height - DRAG_HANDLE_SIZE;
874 width = allocation_width;
875 height = DRAG_HANDLE_SIZE;
878 context = gtk_widget_get_style_context (widget);
879 state = gtk_widget_get_state_flags (widget);
881 gtk_style_context_save (context);
882 gtk_style_context_set_state (context, state);
884 gtk_render_background (context, cr, x, y, width, height);
885 gtk_render_frame (context, cr, x, y, width, height);
887 if (handle_position == GTK_POS_LEFT ||
888 handle_position == GTK_POS_RIGHT)
889 gtk_render_line (context, cr,
890 handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
891 allocation_height / 2,
892 handle_position == GTK_POS_LEFT ? allocation_width : allocation_width - DRAG_HANDLE_SIZE,
893 allocation_height / 2);
895 gtk_render_line (context, cr,
896 allocation_width / 2,
897 handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
898 allocation_width / 2,
899 handle_position == GTK_POS_TOP ? allocation_height : allocation_height - DRAG_HANDLE_SIZE);
901 gtk_style_context_restore (context);
905 * gtk_handle_box_set_shadow_type:
906 * @handle_box: a #GtkHandleBox
907 * @type: the shadow type.
909 * Sets the type of shadow to be drawn around the border
912 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
915 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
918 GtkHandleBoxPrivate *priv;
920 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
922 priv = handle_box->priv;
924 if ((GtkShadowType) priv->shadow_type != type)
926 priv->shadow_type = type;
927 g_object_notify (G_OBJECT (handle_box), "shadow-type");
928 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
933 * gtk_handle_box_get_shadow_type:
934 * @handle_box: a #GtkHandleBox
936 * Gets the type of shadow drawn around the handle box. See
937 * gtk_handle_box_set_shadow_type().
939 * Return value: the type of shadow currently drawn around the handle box.
941 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
944 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
946 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
948 return handle_box->priv->shadow_type;
952 * gtk_handle_box_set_handle_position:
953 * @handle_box: a #GtkHandleBox
954 * @position: the side of the handlebox where the handle should be drawn.
956 * Sets the side of the handlebox where the handle is drawn.
958 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
961 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
962 GtkPositionType position)
964 GtkHandleBoxPrivate *priv;
966 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
968 priv = handle_box->priv;
970 if ((GtkPositionType) priv->handle_position != position)
972 priv->handle_position = position;
973 g_object_notify (G_OBJECT (handle_box), "handle-position");
974 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
979 * gtk_handle_box_get_handle_position:
980 * @handle_box: a #GtkHandleBox
982 * Gets the handle position of the handle box. See
983 * gtk_handle_box_set_handle_position().
985 * Return value: the current handle position.
987 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
990 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
992 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
994 return handle_box->priv->handle_position;
998 * gtk_handle_box_set_snap_edge:
999 * @handle_box: a #GtkHandleBox
1000 * @edge: the snap edge, or -1 to unset the value; in which
1001 * case GTK+ will try to guess an appropriate value
1004 * Sets the snap edge of a handlebox. The snap edge is
1005 * the edge of the detached child that must be aligned
1006 * with the corresponding edge of the "ghost" left
1007 * behind when the child was detached to reattach
1008 * the torn-off window. Usually, the snap edge should
1009 * be chosen so that it stays in the same place on
1010 * the screen when the handlebox is torn off.
1012 * If the snap edge is not set, then an appropriate value
1013 * will be guessed from the handle position. If the
1014 * handle position is %GTK_POS_RIGHT or %GTK_POS_LEFT,
1015 * then the snap edge will be %GTK_POS_TOP, otherwise
1016 * it will be %GTK_POS_LEFT.
1018 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
1021 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
1022 GtkPositionType edge)
1024 GtkHandleBoxPrivate *priv;
1026 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
1028 priv = handle_box->priv;
1030 if (priv->snap_edge != edge)
1032 priv->snap_edge = edge;
1034 g_object_freeze_notify (G_OBJECT (handle_box));
1035 g_object_notify (G_OBJECT (handle_box), "snap-edge");
1036 g_object_notify (G_OBJECT (handle_box), "snap-edge-set");
1037 g_object_thaw_notify (G_OBJECT (handle_box));
1042 * gtk_handle_box_get_snap_edge:
1043 * @handle_box: a #GtkHandleBox
1045 * Gets the edge used for determining reattachment of the handle box.
1046 * See gtk_handle_box_set_snap_edge().
1048 * Return value: the edge used for determining reattachment, or
1049 * (GtkPositionType)-1 if this is determined (as per default)
1050 * from the handle position.
1052 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
1055 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
1057 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
1059 return handle_box->priv->snap_edge;
1063 * gtk_handle_box_get_child_detached:
1064 * @handle_box: a #GtkHandleBox
1066 * Whether the handlebox's child is currently detached.
1068 * Return value: %TRUE if the child is currently detached, otherwise %FALSE
1072 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
1075 gtk_handle_box_get_child_detached (GtkHandleBox *handle_box)
1077 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), FALSE);
1079 return handle_box->priv->child_detached;
1083 gtk_handle_box_paint (GtkWidget *widget,
1086 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1087 GtkHandleBoxPrivate *priv = hb->priv;
1088 GtkBin *bin = GTK_BIN (widget);
1089 GtkStyleContext *context;
1090 GtkStateFlags state;
1094 gint handle_position;
1096 handle_position = effective_handle_position (hb);
1098 width = gdk_window_get_width (priv->bin_window);
1099 height = gdk_window_get_height (priv->bin_window);
1101 context = gtk_widget_get_style_context (widget);
1102 state = gtk_widget_get_state_flags (widget);
1104 gtk_style_context_save (context);
1105 gtk_style_context_set_state (context, state);
1107 gtk_render_background (context, cr, 0, 0, width, height);
1108 gtk_render_frame (context, cr, 0, 0, width, height);
1110 switch (handle_position)
1115 rect.width = DRAG_HANDLE_SIZE;
1116 rect.height = height;
1119 rect.x = width - DRAG_HANDLE_SIZE;
1121 rect.width = DRAG_HANDLE_SIZE;
1122 rect.height = height;
1128 rect.height = DRAG_HANDLE_SIZE;
1130 case GTK_POS_BOTTOM:
1132 rect.y = height - DRAG_HANDLE_SIZE;
1134 rect.height = DRAG_HANDLE_SIZE;
1137 g_assert_not_reached ();
1141 gtk_render_handle (context, cr,
1142 rect.x, rect.y, rect.width, rect.height);
1144 child = gtk_bin_get_child (bin);
1145 if (child != NULL && gtk_widget_get_visible (child))
1146 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->draw (widget, cr);
1148 gtk_style_context_restore (context);
1152 gtk_handle_box_draw (GtkWidget *widget,
1155 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1156 GtkHandleBoxPrivate *priv = hb->priv;
1158 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
1160 if (priv->child_detached)
1161 gtk_handle_box_draw_ghost (hb, cr);
1163 else if (gtk_cairo_should_draw_window (cr, priv->bin_window))
1164 gtk_handle_box_paint (widget, cr);
1170 gtk_handle_box_get_invisible (void)
1172 static GtkWidget *handle_box_invisible = NULL;
1174 if (!handle_box_invisible)
1176 handle_box_invisible = gtk_invisible_new ();
1177 gtk_widget_show (handle_box_invisible);
1180 return handle_box_invisible;
1184 gtk_handle_box_grab_event (GtkWidget *widget,
1188 GtkHandleBoxPrivate *priv = hb->priv;
1190 switch (event->type)
1192 case GDK_BUTTON_RELEASE:
1193 if (priv->in_drag) /* sanity check */
1195 gtk_handle_box_end_drag (hb, event->button.time);
1200 case GDK_MOTION_NOTIFY:
1201 return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event);
1212 gtk_handle_box_button_press (GtkWidget *widget,
1213 GdkEventButton *event)
1215 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1216 GtkHandleBoxPrivate *priv = hb->priv;
1217 gboolean event_handled;
1219 gint handle_position;
1221 handle_position = effective_handle_position (hb);
1223 event_handled = FALSE;
1224 if ((event->button == GDK_BUTTON_PRIMARY) &&
1225 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1230 if (event->window != priv->bin_window)
1233 child = gtk_bin_get_child (GTK_BIN (hb));
1237 GtkAllocation child_allocation;
1240 gtk_widget_get_allocation (child, &child_allocation);
1241 border_width = gtk_container_get_border_width (GTK_CONTAINER (hb));
1243 switch (handle_position)
1246 in_handle = event->x < DRAG_HANDLE_SIZE;
1249 in_handle = event->y < DRAG_HANDLE_SIZE;
1252 in_handle = event->x > 2 * border_width + child_allocation.width;
1254 case GTK_POS_BOTTOM:
1255 in_handle = event->y > 2 * border_width + child_allocation.height;
1265 event_handled = TRUE;
1270 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
1272 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1274 gint root_x, root_y;
1276 gtk_invisible_set_screen (GTK_INVISIBLE (invisible),
1277 gtk_widget_get_screen (GTK_WIDGET (hb)));
1278 gdk_window_get_origin (priv->bin_window, &root_x, &root_y);
1280 priv->orig_x = event->x_root;
1281 priv->orig_y = event->y_root;
1283 priv->float_allocation.x = root_x - event->x_root;
1284 priv->float_allocation.y = root_y - event->y_root;
1285 priv->float_allocation.width = gdk_window_get_width (priv->bin_window);
1286 priv->float_allocation.height = gdk_window_get_height (priv->bin_window);
1288 window = gtk_widget_get_window (widget);
1289 if (gdk_window_is_viewable (window))
1291 gdk_window_get_origin (window, &root_x, &root_y);
1293 priv->attach_allocation.x = root_x;
1294 priv->attach_allocation.y = root_y;
1295 priv->attach_allocation.width = gdk_window_get_width (window);
1296 priv->attach_allocation.height = gdk_window_get_height (window);
1300 priv->attach_allocation.x = -1;
1301 priv->attach_allocation.y = -1;
1302 priv->attach_allocation.width = 0;
1303 priv->attach_allocation.height = 0;
1305 priv->in_drag = TRUE;
1306 priv->grab_device = event->device;
1307 fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1309 if (gdk_device_grab (event->device,
1310 gtk_widget_get_window (invisible),
1311 GDK_OWNERSHIP_WINDOW,
1313 (GDK_BUTTON1_MOTION_MASK |
1314 GDK_POINTER_MOTION_HINT_MASK |
1315 GDK_BUTTON_RELEASE_MASK),
1317 event->time) != GDK_GRAB_SUCCESS)
1319 priv->in_drag = FALSE;
1320 priv->grab_device = NULL;
1324 gtk_device_grab_add (invisible, priv->grab_device, TRUE);
1325 g_signal_connect (invisible, "event",
1326 G_CALLBACK (gtk_handle_box_grab_event), hb);
1329 g_object_unref (fleur);
1330 event_handled = TRUE;
1332 else if (priv->child_detached) /* Double click */
1334 gtk_handle_box_reattach (hb);
1339 return event_handled;
1343 gtk_handle_box_motion (GtkWidget *widget,
1344 GdkEventMotion *event)
1346 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1347 GtkHandleBoxPrivate *priv = hb->priv;
1351 gboolean is_snapped = FALSE;
1352 gint handle_position;
1353 GdkGeometry geometry;
1354 GdkScreen *screen, *pointer_screen;
1358 handle_position = effective_handle_position (hb);
1360 /* Calculate the attachment point on the float, if the float
1365 screen = gtk_widget_get_screen (widget);
1366 gdk_device_get_position (event->device,
1369 if (pointer_screen != screen)
1371 new_x = priv->orig_x;
1372 new_y = priv->orig_y;
1375 new_x += priv->float_allocation.x;
1376 new_y += priv->float_allocation.y;
1378 snap_edge = priv->snap_edge;
1379 if (snap_edge == -1)
1380 snap_edge = (handle_position == GTK_POS_LEFT ||
1381 handle_position == GTK_POS_RIGHT) ?
1382 GTK_POS_TOP : GTK_POS_LEFT;
1384 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1388 snap_edge = GTK_POS_RIGHT;
1391 snap_edge = GTK_POS_LEFT;
1397 /* First, check if the snapped edge is aligned
1402 is_snapped = abs (priv->attach_allocation.y - new_y) < TOLERANCE;
1404 case GTK_POS_BOTTOM:
1405 is_snapped = abs (priv->attach_allocation.y + (gint)priv->attach_allocation.height -
1406 new_y - (gint)priv->float_allocation.height) < TOLERANCE;
1409 is_snapped = abs (priv->attach_allocation.x - new_x) < TOLERANCE;
1412 is_snapped = abs (priv->attach_allocation.x + (gint)priv->attach_allocation.width -
1413 new_x - (gint)priv->float_allocation.width) < TOLERANCE;
1417 /* Next, check if coordinates in the other direction are sufficiently
1422 gint float_pos1 = 0; /* Initialize to suppress warnings */
1423 gint float_pos2 = 0;
1424 gint attach_pos1 = 0;
1425 gint attach_pos2 = 0;
1430 case GTK_POS_BOTTOM:
1431 attach_pos1 = priv->attach_allocation.x;
1432 attach_pos2 = priv->attach_allocation.x + priv->attach_allocation.width;
1434 float_pos2 = new_x + priv->float_allocation.width;
1438 attach_pos1 = priv->attach_allocation.y;
1439 attach_pos2 = priv->attach_allocation.y + priv->attach_allocation.height;
1441 float_pos2 = new_y + priv->float_allocation.height;
1445 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1446 (attach_pos2 + TOLERANCE > float_pos2)) ||
1447 ((float_pos1 - TOLERANCE < attach_pos1) &&
1448 (float_pos2 + TOLERANCE > attach_pos2));
1451 child = gtk_bin_get_child (GTK_BIN (hb));
1455 if (priv->child_detached)
1457 priv->child_detached = FALSE;
1458 gdk_window_hide (priv->float_window);
1459 gdk_window_reparent (priv->bin_window, gtk_widget_get_window (widget), 0, 0);
1460 priv->float_window_mapped = FALSE;
1462 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1466 gtk_widget_queue_resize (widget);
1473 width = gdk_window_get_width (priv->float_window);
1474 height = gdk_window_get_height (priv->float_window);
1476 switch (handle_position)
1479 new_y += ((gint)priv->float_allocation.height - height) / 2;
1482 new_x += (gint)priv->float_allocation.width - width;
1483 new_y += ((gint)priv->float_allocation.height - height) / 2;
1486 new_x += ((gint)priv->float_allocation.width - width) / 2;
1488 case GTK_POS_BOTTOM:
1489 new_x += ((gint)priv->float_allocation.width - width) / 2;
1490 new_y += (gint)priv->float_allocation.height - height;
1494 if (priv->child_detached)
1496 gdk_window_move (priv->float_window, new_x, new_y);
1497 gdk_window_raise (priv->float_window);
1502 GtkRequisition child_requisition;
1504 priv->child_detached = TRUE;
1508 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
1512 child_requisition.width = 0;
1513 child_requisition.height = 0;
1516 border_width = gtk_container_get_border_width (GTK_CONTAINER (hb));
1517 width = child_requisition.width + 2 * border_width;
1518 height = child_requisition.height + 2 * border_width;
1520 if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT)
1521 width += DRAG_HANDLE_SIZE;
1523 height += DRAG_HANDLE_SIZE;
1525 gdk_window_move_resize (priv->float_window, new_x, new_y, width, height);
1526 gdk_window_reparent (priv->bin_window, priv->float_window, 0, 0);
1527 gdk_window_set_geometry_hints (priv->float_window, &geometry, GDK_HINT_POS);
1528 gdk_window_show (priv->float_window);
1529 priv->float_window_mapped = TRUE;
1531 /* this extra move is necessary if we use decorations, or our
1532 * window manager insists on decorations.
1534 gdk_display_sync (gtk_widget_get_display (widget));
1535 gdk_window_move (priv->float_window, new_x, new_y);
1536 gdk_display_sync (gtk_widget_get_display (widget));
1539 handle_box_signals[SIGNAL_CHILD_DETACHED],
1543 gtk_widget_queue_resize (widget);
1551 gtk_handle_box_add (GtkContainer *container,
1554 GtkHandleBoxPrivate *priv = GTK_HANDLE_BOX (container)->priv;
1556 gtk_widget_set_parent_window (widget, priv->bin_window);
1557 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->add (container, widget);
1561 gtk_handle_box_remove (GtkContainer *container,
1564 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->remove (container, widget);
1566 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1570 gtk_handle_box_delete_event (GtkWidget *widget,
1573 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1574 GtkHandleBoxPrivate *priv = hb->priv;
1576 if (event->window == priv->float_window)
1578 gtk_handle_box_reattach (hb);
1587 gtk_handle_box_reattach (GtkHandleBox *hb)
1589 GtkHandleBoxPrivate *priv = hb->priv;
1591 GtkWidget *widget = GTK_WIDGET (hb);
1593 if (priv->child_detached)
1595 priv->child_detached = FALSE;
1596 if (gtk_widget_get_realized (widget))
1598 gdk_window_hide (priv->float_window);
1599 gdk_window_reparent (priv->bin_window, gtk_widget_get_window (widget),
1602 child = gtk_bin_get_child (GTK_BIN (hb));
1605 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1610 priv->float_window_mapped = FALSE;
1613 gtk_handle_box_end_drag (hb, GDK_CURRENT_TIME);
1615 gtk_widget_queue_resize (GTK_WIDGET (hb));
1619 gtk_handle_box_end_drag (GtkHandleBox *hb,
1622 GtkHandleBoxPrivate *priv = hb->priv;
1623 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1625 priv->in_drag = FALSE;
1627 gtk_device_grab_remove (invisible, priv->grab_device);
1628 gdk_device_ungrab (priv->grab_device, time);
1629 g_signal_handlers_disconnect_by_func (invisible,
1630 G_CALLBACK (gtk_handle_box_grab_event),
1633 priv->grab_device = NULL;