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;
417 GtkStyleContext *context;
420 GdkWindowAttr attributes;
421 gint attributes_mask;
423 gtk_widget_set_realized (widget, TRUE);
425 gtk_widget_get_allocation (widget, &allocation);
427 attributes.x = allocation.x;
428 attributes.y = allocation.y;
429 attributes.width = allocation.width;
430 attributes.height = allocation.height;
431 attributes.window_type = GDK_WINDOW_CHILD;
432 attributes.wclass = GDK_INPUT_OUTPUT;
433 attributes.visual = gtk_widget_get_visual (widget);
434 attributes.event_mask = (gtk_widget_get_events (widget)
435 | GDK_EXPOSURE_MASK);
436 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
438 window = gdk_window_new (gtk_widget_get_parent_window (widget),
439 &attributes, attributes_mask);
440 gtk_widget_set_window (widget, window);
441 gdk_window_set_user_data (window, widget);
445 attributes.width = allocation.width;
446 attributes.height = allocation.height;
447 attributes.window_type = GDK_WINDOW_CHILD;
448 attributes.event_mask = (gtk_widget_get_events (widget)
450 | GDK_BUTTON1_MOTION_MASK
451 | GDK_POINTER_MOTION_HINT_MASK
452 | GDK_BUTTON_PRESS_MASK
453 | GDK_BUTTON_RELEASE_MASK);
454 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
456 priv->bin_window = gdk_window_new (window,
457 &attributes, attributes_mask);
458 gdk_window_set_user_data (priv->bin_window, widget);
460 child = gtk_bin_get_child (GTK_BIN (hb));
462 gtk_widget_set_parent_window (child, priv->bin_window);
464 gtk_widget_get_preferred_size (widget, &requisition, NULL);
468 attributes.width = requisition.width;
469 attributes.height = requisition.height;
470 attributes.window_type = GDK_WINDOW_TOPLEVEL;
471 attributes.wclass = GDK_INPUT_OUTPUT;
472 attributes.visual = gtk_widget_get_visual (widget);
473 attributes.event_mask = (gtk_widget_get_events (widget)
475 | GDK_ENTER_NOTIFY_MASK
476 | GDK_LEAVE_NOTIFY_MASK
477 | GDK_FOCUS_CHANGE_MASK
478 | GDK_STRUCTURE_MASK);
479 attributes.type_hint = GDK_WINDOW_TYPE_HINT_TOOLBAR;
480 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_TYPE_HINT;
481 priv->float_window = gdk_window_new (gtk_widget_get_root_window (widget),
482 &attributes, attributes_mask);
483 gdk_window_set_user_data (priv->float_window, widget);
484 gdk_window_set_decorations (priv->float_window, 0);
485 gdk_window_set_type_hint (priv->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
487 context = gtk_widget_get_style_context (widget);
488 gtk_style_context_set_background (context, window);
489 gtk_style_context_set_background (context, priv->bin_window);
490 gtk_style_context_set_background (context, priv->float_window);
494 gtk_handle_box_unrealize (GtkWidget *widget)
496 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
497 GtkHandleBoxPrivate *priv = hb->priv;
499 gdk_window_set_user_data (priv->bin_window, NULL);
500 gdk_window_destroy (priv->bin_window);
501 priv->bin_window = NULL;
502 gdk_window_set_user_data (priv->float_window, NULL);
503 gdk_window_destroy (priv->float_window);
504 priv->float_window = NULL;
506 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unrealize (widget);
510 gtk_handle_box_style_updated (GtkWidget *widget)
512 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
513 GtkHandleBoxPrivate *priv = hb->priv;
515 if (gtk_widget_get_realized (widget) &&
516 gtk_widget_get_has_window (widget))
519 GtkStyleContext *context;
521 context = gtk_widget_get_style_context (widget);
522 state = gtk_widget_get_state_flags (widget);
524 gtk_style_context_save (context);
525 gtk_style_context_set_state (context, state);
527 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
528 gtk_style_context_set_background (context, priv->bin_window);
529 gtk_style_context_set_background (context, priv->float_window);
531 gtk_style_context_restore (context);
536 effective_handle_position (GtkHandleBox *hb)
538 GtkHandleBoxPrivate *priv = hb->priv;
541 if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
542 handle_position = priv->handle_position;
545 switch (priv->handle_position)
548 handle_position = GTK_POS_RIGHT;
551 handle_position = GTK_POS_LEFT;
554 handle_position = priv->handle_position;
559 return handle_position;
563 gtk_handle_box_size_request (GtkWidget *widget,
564 GtkRequisition *requisition)
566 GtkBin *bin = GTK_BIN (widget);
567 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
568 GtkHandleBoxPrivate *priv = hb->priv;
569 GtkRequisition child_requisition;
571 gint handle_position;
573 handle_position = effective_handle_position (hb);
575 if (handle_position == GTK_POS_LEFT ||
576 handle_position == GTK_POS_RIGHT)
578 requisition->width = DRAG_HANDLE_SIZE;
579 requisition->height = 0;
583 requisition->width = 0;
584 requisition->height = DRAG_HANDLE_SIZE;
587 child = gtk_bin_get_child (bin);
588 /* if our child is not visible, we still request its size, since we
589 * won't have any useful hint for our size otherwise.
593 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
597 child_requisition.width = 0;
598 child_requisition.height = 0;
601 if (priv->child_detached)
603 /* FIXME: This doesn't work currently */
604 if (!priv->shrink_on_detach)
606 if (handle_position == GTK_POS_LEFT ||
607 handle_position == GTK_POS_RIGHT)
608 requisition->height += child_requisition.height;
610 requisition->width += child_requisition.width;
614 GtkStyleContext *context;
618 context = gtk_widget_get_style_context (widget);
619 state = gtk_widget_get_state_flags (widget);
620 gtk_style_context_get_padding (context, state, &padding);
622 if (handle_position == GTK_POS_LEFT ||
623 handle_position == GTK_POS_RIGHT)
624 requisition->height += padding.top;
626 requisition->width += padding.left;
633 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
634 requisition->width += border_width * 2;
635 requisition->height += border_width * 2;
639 requisition->width += child_requisition.width;
640 requisition->height += child_requisition.height;
644 requisition->width += CHILDLESS_SIZE;
645 requisition->height += CHILDLESS_SIZE;
651 gtk_handle_box_get_preferred_width (GtkWidget *widget,
655 GtkRequisition requisition;
657 gtk_handle_box_size_request (widget, &requisition);
659 *minimum = *natural = requisition.width;
663 gtk_handle_box_get_preferred_height (GtkWidget *widget,
667 GtkRequisition requisition;
669 gtk_handle_box_size_request (widget, &requisition);
671 *minimum = *natural = requisition.height;
676 gtk_handle_box_size_allocate (GtkWidget *widget,
677 GtkAllocation *allocation)
679 GtkBin *bin = GTK_BIN (widget);
680 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
681 GtkHandleBoxPrivate *priv = hb->priv;
682 GtkRequisition child_requisition;
684 gint handle_position;
686 handle_position = effective_handle_position (hb);
688 child = gtk_bin_get_child (bin);
692 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
696 child_requisition.width = 0;
697 child_requisition.height = 0;
700 gtk_widget_set_allocation (widget, allocation);
702 if (gtk_widget_get_realized (widget))
703 gdk_window_move_resize (gtk_widget_get_window (widget),
704 allocation->x, allocation->y,
705 allocation->width, allocation->height);
707 if (child != NULL && gtk_widget_get_visible (child))
709 GtkAllocation child_allocation;
712 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
714 child_allocation.x = border_width;
715 child_allocation.y = border_width;
716 if (handle_position == GTK_POS_LEFT)
717 child_allocation.x += DRAG_HANDLE_SIZE;
718 else if (handle_position == GTK_POS_TOP)
719 child_allocation.y += DRAG_HANDLE_SIZE;
721 if (priv->child_detached)
726 child_allocation.width = child_requisition.width;
727 child_allocation.height = child_requisition.height;
729 float_width = child_allocation.width + 2 * border_width;
730 float_height = child_allocation.height + 2 * border_width;
732 if (handle_position == GTK_POS_LEFT ||
733 handle_position == GTK_POS_RIGHT)
734 float_width += DRAG_HANDLE_SIZE;
736 float_height += DRAG_HANDLE_SIZE;
738 if (gtk_widget_get_realized (widget))
740 gdk_window_resize (priv->float_window,
743 gdk_window_move_resize (priv->bin_window,
752 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
753 child_allocation.height = MAX (1, (gint) allocation->height - 2 * border_width);
755 if (handle_position == GTK_POS_LEFT ||
756 handle_position == GTK_POS_RIGHT)
757 child_allocation.width -= DRAG_HANDLE_SIZE;
759 child_allocation.height -= DRAG_HANDLE_SIZE;
761 if (gtk_widget_get_realized (widget))
762 gdk_window_move_resize (priv->bin_window,
769 gtk_widget_size_allocate (child, &child_allocation);
774 gtk_handle_box_draw_ghost (GtkHandleBox *hb,
777 GtkWidget *widget = GTK_WIDGET (hb);
779 GtkStyleContext *context;
784 gint allocation_width;
785 gint allocation_height;
786 gint handle_position;
788 handle_position = effective_handle_position (hb);
789 allocation_width = gtk_widget_get_allocated_width (widget);
790 allocation_height = gtk_widget_get_allocated_height (widget);
792 if (handle_position == GTK_POS_LEFT ||
793 handle_position == GTK_POS_RIGHT)
795 x = handle_position == GTK_POS_LEFT ? 0 : allocation_width - DRAG_HANDLE_SIZE;
797 width = DRAG_HANDLE_SIZE;
798 height = allocation_height;
803 y = handle_position == GTK_POS_TOP ? 0 : allocation_height - DRAG_HANDLE_SIZE;
804 width = allocation_width;
805 height = DRAG_HANDLE_SIZE;
808 context = gtk_widget_get_style_context (widget);
809 state = gtk_widget_get_state_flags (widget);
811 gtk_style_context_save (context);
812 gtk_style_context_set_state (context, state);
814 gtk_render_background (context, cr, x, y, width, height);
815 gtk_render_frame (context, cr, x, y, width, height);
817 if (handle_position == GTK_POS_LEFT ||
818 handle_position == GTK_POS_RIGHT)
819 gtk_render_line (context, cr,
820 handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
821 allocation_height / 2,
822 handle_position == GTK_POS_LEFT ? allocation_width : allocation_width - DRAG_HANDLE_SIZE,
823 allocation_height / 2);
825 gtk_render_line (context, cr,
826 allocation_width / 2,
827 handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
828 allocation_width / 2,
829 handle_position == GTK_POS_TOP ? allocation_height : allocation_height - DRAG_HANDLE_SIZE);
831 gtk_style_context_restore (context);
835 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
838 GtkHandleBoxPrivate *priv;
840 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
842 priv = handle_box->priv;
844 if ((GtkShadowType) priv->shadow_type != type)
846 priv->shadow_type = type;
847 g_object_notify (G_OBJECT (handle_box), "shadow-type");
848 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
853 * gtk_handle_box_get_shadow_type:
854 * @handle_box: a #GtkHandleBox
856 * Gets the type of shadow drawn around the handle box. See
857 * gtk_handle_box_set_shadow_type().
859 * Return value: the type of shadow currently drawn around the handle box.
862 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
864 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
866 return handle_box->priv->shadow_type;
870 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
871 GtkPositionType position)
873 GtkHandleBoxPrivate *priv;
875 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
877 priv = handle_box->priv;
879 if ((GtkPositionType) priv->handle_position != position)
881 priv->handle_position = position;
882 g_object_notify (G_OBJECT (handle_box), "handle-position");
883 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
888 * gtk_handle_box_get_handle_position:
889 * @handle_box: a #GtkHandleBox
891 * Gets the handle position of the handle box. See
892 * gtk_handle_box_set_handle_position().
894 * Return value: the current handle position.
897 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
899 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
901 return handle_box->priv->handle_position;
905 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
906 GtkPositionType edge)
908 GtkHandleBoxPrivate *priv;
910 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
912 priv = handle_box->priv;
914 if (priv->snap_edge != edge)
916 priv->snap_edge = edge;
918 g_object_freeze_notify (G_OBJECT (handle_box));
919 g_object_notify (G_OBJECT (handle_box), "snap-edge");
920 g_object_notify (G_OBJECT (handle_box), "snap-edge-set");
921 g_object_thaw_notify (G_OBJECT (handle_box));
926 * gtk_handle_box_get_snap_edge:
927 * @handle_box: a #GtkHandleBox
929 * Gets the edge used for determining reattachment of the handle box. See
930 * gtk_handle_box_set_snap_edge().
932 * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
933 * is determined (as per default) from the handle position.
936 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
938 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
940 return handle_box->priv->snap_edge;
944 * gtk_handle_box_get_child_detached:
945 * @handle_box: a #GtkHandleBox
947 * Whether the handlebox's child is currently detached.
949 * Return value: %TRUE if the child is currently detached, otherwise %FALSE
954 gtk_handle_box_get_child_detached (GtkHandleBox *handle_box)
956 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), FALSE);
958 return handle_box->priv->child_detached;
962 gtk_handle_box_paint (GtkWidget *widget,
965 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
966 GtkHandleBoxPrivate *priv = hb->priv;
967 GtkBin *bin = GTK_BIN (widget);
968 GtkStyleContext *context;
973 gint handle_position;
975 handle_position = effective_handle_position (hb);
977 width = gdk_window_get_width (priv->bin_window);
978 height = gdk_window_get_height (priv->bin_window);
980 context = gtk_widget_get_style_context (widget);
981 state = gtk_widget_get_state_flags (widget);
983 gtk_style_context_save (context);
984 gtk_style_context_set_state (context, state);
986 gtk_render_background (context, cr, 0, 0, width, height);
987 gtk_render_frame (context, cr, 0, 0, width, height);
989 switch (handle_position)
994 rect.width = DRAG_HANDLE_SIZE;
995 rect.height = height;
998 rect.x = width - DRAG_HANDLE_SIZE;
1000 rect.width = DRAG_HANDLE_SIZE;
1001 rect.height = height;
1007 rect.height = DRAG_HANDLE_SIZE;
1009 case GTK_POS_BOTTOM:
1011 rect.y = height - DRAG_HANDLE_SIZE;
1013 rect.height = DRAG_HANDLE_SIZE;
1016 g_assert_not_reached ();
1020 gtk_render_handle (context, cr,
1021 rect.x, rect.y, rect.width, rect.height);
1023 child = gtk_bin_get_child (bin);
1024 if (child != NULL && gtk_widget_get_visible (child))
1025 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->draw (widget, cr);
1027 gtk_style_context_restore (context);
1031 gtk_handle_box_draw (GtkWidget *widget,
1034 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1035 GtkHandleBoxPrivate *priv = hb->priv;
1037 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
1039 if (priv->child_detached)
1040 gtk_handle_box_draw_ghost (hb, cr);
1042 else if (gtk_cairo_should_draw_window (cr, priv->bin_window))
1043 gtk_handle_box_paint (widget, cr);
1049 gtk_handle_box_get_invisible (void)
1051 static GtkWidget *handle_box_invisible = NULL;
1053 if (!handle_box_invisible)
1055 handle_box_invisible = gtk_invisible_new ();
1056 gtk_widget_show (handle_box_invisible);
1059 return handle_box_invisible;
1063 gtk_handle_box_grab_event (GtkWidget *widget,
1067 GtkHandleBoxPrivate *priv = hb->priv;
1069 switch (event->type)
1071 case GDK_BUTTON_RELEASE:
1072 if (priv->in_drag) /* sanity check */
1074 gtk_handle_box_end_drag (hb, event->button.time);
1079 case GDK_MOTION_NOTIFY:
1080 return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event);
1091 gtk_handle_box_button_press (GtkWidget *widget,
1092 GdkEventButton *event)
1094 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1095 GtkHandleBoxPrivate *priv = hb->priv;
1096 gboolean event_handled;
1098 gint handle_position;
1100 handle_position = effective_handle_position (hb);
1102 event_handled = FALSE;
1103 if ((event->button == 1) &&
1104 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1109 if (event->window != priv->bin_window)
1112 child = gtk_bin_get_child (GTK_BIN (hb));
1116 GtkAllocation child_allocation;
1119 gtk_widget_get_allocation (child, &child_allocation);
1120 border_width = gtk_container_get_border_width (GTK_CONTAINER (hb));
1122 switch (handle_position)
1125 in_handle = event->x < DRAG_HANDLE_SIZE;
1128 in_handle = event->y < DRAG_HANDLE_SIZE;
1131 in_handle = event->x > 2 * border_width + child_allocation.width;
1133 case GTK_POS_BOTTOM:
1134 in_handle = event->y > 2 * border_width + child_allocation.height;
1144 event_handled = TRUE;
1149 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
1151 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1153 gint root_x, root_y;
1155 gtk_invisible_set_screen (GTK_INVISIBLE (invisible),
1156 gtk_widget_get_screen (GTK_WIDGET (hb)));
1157 gdk_window_get_origin (priv->bin_window, &root_x, &root_y);
1159 priv->orig_x = event->x_root;
1160 priv->orig_y = event->y_root;
1162 priv->float_allocation.x = root_x - event->x_root;
1163 priv->float_allocation.y = root_y - event->y_root;
1164 priv->float_allocation.width = gdk_window_get_width (priv->bin_window);
1165 priv->float_allocation.height = gdk_window_get_height (priv->bin_window);
1167 window = gtk_widget_get_window (widget);
1168 if (gdk_window_is_viewable (window))
1170 gdk_window_get_origin (window, &root_x, &root_y);
1172 priv->attach_allocation.x = root_x;
1173 priv->attach_allocation.y = root_y;
1174 priv->attach_allocation.width = gdk_window_get_width (window);
1175 priv->attach_allocation.height = gdk_window_get_height (window);
1179 priv->attach_allocation.x = -1;
1180 priv->attach_allocation.y = -1;
1181 priv->attach_allocation.width = 0;
1182 priv->attach_allocation.height = 0;
1184 priv->in_drag = TRUE;
1185 priv->grab_device = event->device;
1186 fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1188 if (gdk_device_grab (event->device,
1189 gtk_widget_get_window (invisible),
1190 GDK_OWNERSHIP_WINDOW,
1192 (GDK_BUTTON1_MOTION_MASK |
1193 GDK_POINTER_MOTION_HINT_MASK |
1194 GDK_BUTTON_RELEASE_MASK),
1196 event->time) != GDK_GRAB_SUCCESS)
1198 priv->in_drag = FALSE;
1199 priv->grab_device = NULL;
1203 gtk_device_grab_add (invisible, priv->grab_device, TRUE);
1204 g_signal_connect (invisible, "event",
1205 G_CALLBACK (gtk_handle_box_grab_event), hb);
1208 g_object_unref (fleur);
1209 event_handled = TRUE;
1211 else if (priv->child_detached) /* Double click */
1213 gtk_handle_box_reattach (hb);
1218 return event_handled;
1222 gtk_handle_box_motion (GtkWidget *widget,
1223 GdkEventMotion *event)
1225 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1226 GtkHandleBoxPrivate *priv = hb->priv;
1230 gboolean is_snapped = FALSE;
1231 gint handle_position;
1232 GdkGeometry geometry;
1233 GdkScreen *screen, *pointer_screen;
1237 handle_position = effective_handle_position (hb);
1239 /* Calculate the attachment point on the float, if the float
1244 screen = gtk_widget_get_screen (widget);
1245 gdk_device_get_position (event->device,
1248 if (pointer_screen != screen)
1250 new_x = priv->orig_x;
1251 new_y = priv->orig_y;
1254 new_x += priv->float_allocation.x;
1255 new_y += priv->float_allocation.y;
1257 snap_edge = priv->snap_edge;
1258 if (snap_edge == -1)
1259 snap_edge = (handle_position == GTK_POS_LEFT ||
1260 handle_position == GTK_POS_RIGHT) ?
1261 GTK_POS_TOP : GTK_POS_LEFT;
1263 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1267 snap_edge = GTK_POS_RIGHT;
1270 snap_edge = GTK_POS_LEFT;
1276 /* First, check if the snapped edge is aligned
1281 is_snapped = abs (priv->attach_allocation.y - new_y) < TOLERANCE;
1283 case GTK_POS_BOTTOM:
1284 is_snapped = abs (priv->attach_allocation.y + (gint)priv->attach_allocation.height -
1285 new_y - (gint)priv->float_allocation.height) < TOLERANCE;
1288 is_snapped = abs (priv->attach_allocation.x - new_x) < TOLERANCE;
1291 is_snapped = abs (priv->attach_allocation.x + (gint)priv->attach_allocation.width -
1292 new_x - (gint)priv->float_allocation.width) < TOLERANCE;
1296 /* Next, check if coordinates in the other direction are sufficiently
1301 gint float_pos1 = 0; /* Initialize to suppress warnings */
1302 gint float_pos2 = 0;
1303 gint attach_pos1 = 0;
1304 gint attach_pos2 = 0;
1309 case GTK_POS_BOTTOM:
1310 attach_pos1 = priv->attach_allocation.x;
1311 attach_pos2 = priv->attach_allocation.x + priv->attach_allocation.width;
1313 float_pos2 = new_x + priv->float_allocation.width;
1317 attach_pos1 = priv->attach_allocation.y;
1318 attach_pos2 = priv->attach_allocation.y + priv->attach_allocation.height;
1320 float_pos2 = new_y + priv->float_allocation.height;
1324 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1325 (attach_pos2 + TOLERANCE > float_pos2)) ||
1326 ((float_pos1 - TOLERANCE < attach_pos1) &&
1327 (float_pos2 + TOLERANCE > attach_pos2));
1330 child = gtk_bin_get_child (GTK_BIN (hb));
1334 if (priv->child_detached)
1336 priv->child_detached = FALSE;
1337 gdk_window_hide (priv->float_window);
1338 gdk_window_reparent (priv->bin_window, gtk_widget_get_window (widget), 0, 0);
1339 priv->float_window_mapped = FALSE;
1341 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1345 gtk_widget_queue_resize (widget);
1352 width = gdk_window_get_width (priv->float_window);
1353 height = gdk_window_get_height (priv->float_window);
1355 switch (handle_position)
1358 new_y += ((gint)priv->float_allocation.height - height) / 2;
1361 new_x += (gint)priv->float_allocation.width - width;
1362 new_y += ((gint)priv->float_allocation.height - height) / 2;
1365 new_x += ((gint)priv->float_allocation.width - width) / 2;
1367 case GTK_POS_BOTTOM:
1368 new_x += ((gint)priv->float_allocation.width - width) / 2;
1369 new_y += (gint)priv->float_allocation.height - height;
1373 if (priv->child_detached)
1375 gdk_window_move (priv->float_window, new_x, new_y);
1376 gdk_window_raise (priv->float_window);
1381 GtkRequisition child_requisition;
1383 priv->child_detached = TRUE;
1387 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
1391 child_requisition.width = 0;
1392 child_requisition.height = 0;
1395 border_width = gtk_container_get_border_width (GTK_CONTAINER (hb));
1396 width = child_requisition.width + 2 * border_width;
1397 height = child_requisition.height + 2 * border_width;
1399 if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT)
1400 width += DRAG_HANDLE_SIZE;
1402 height += DRAG_HANDLE_SIZE;
1404 gdk_window_move_resize (priv->float_window, new_x, new_y, width, height);
1405 gdk_window_reparent (priv->bin_window, priv->float_window, 0, 0);
1406 gdk_window_set_geometry_hints (priv->float_window, &geometry, GDK_HINT_POS);
1407 gdk_window_show (priv->float_window);
1408 priv->float_window_mapped = TRUE;
1410 /* this extra move is necessary if we use decorations, or our
1411 * window manager insists on decorations.
1413 gdk_display_sync (gtk_widget_get_display (widget));
1414 gdk_window_move (priv->float_window, new_x, new_y);
1415 gdk_display_sync (gtk_widget_get_display (widget));
1418 handle_box_signals[SIGNAL_CHILD_DETACHED],
1422 gtk_widget_queue_resize (widget);
1430 gtk_handle_box_add (GtkContainer *container,
1433 GtkHandleBoxPrivate *priv = GTK_HANDLE_BOX (container)->priv;
1435 gtk_widget_set_parent_window (widget, priv->bin_window);
1436 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->add (container, widget);
1440 gtk_handle_box_remove (GtkContainer *container,
1443 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->remove (container, widget);
1445 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1449 gtk_handle_box_delete_event (GtkWidget *widget,
1452 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1453 GtkHandleBoxPrivate *priv = hb->priv;
1455 if (event->window == priv->float_window)
1457 gtk_handle_box_reattach (hb);
1466 gtk_handle_box_reattach (GtkHandleBox *hb)
1468 GtkHandleBoxPrivate *priv = hb->priv;
1470 GtkWidget *widget = GTK_WIDGET (hb);
1472 if (priv->child_detached)
1474 priv->child_detached = FALSE;
1475 if (gtk_widget_get_realized (widget))
1477 gdk_window_hide (priv->float_window);
1478 gdk_window_reparent (priv->bin_window, gtk_widget_get_window (widget),
1481 child = gtk_bin_get_child (GTK_BIN (hb));
1484 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1489 priv->float_window_mapped = FALSE;
1492 gtk_handle_box_end_drag (hb, GDK_CURRENT_TIME);
1494 gtk_widget_queue_resize (GTK_WIDGET (hb));
1498 gtk_handle_box_end_drag (GtkHandleBox *hb,
1501 GtkHandleBoxPrivate *priv = hb->priv;
1502 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1504 priv->in_drag = FALSE;
1506 gtk_device_grab_remove (invisible, priv->grab_device);
1507 gdk_device_ungrab (priv->grab_device, time);
1508 g_signal_handlers_disconnect_by_func (invisible,
1509 G_CALLBACK (gtk_handle_box_grab_event),
1512 priv->grab_device = NULL;