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 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->style_updated (widget);
517 if (gtk_widget_get_realized (widget) &&
518 gtk_widget_get_has_window (widget))
521 GtkStyleContext *context;
523 context = gtk_widget_get_style_context (widget);
524 state = gtk_widget_get_state_flags (widget);
526 gtk_style_context_save (context);
527 gtk_style_context_set_state (context, state);
529 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
530 gtk_style_context_set_background (context, priv->bin_window);
531 gtk_style_context_set_background (context, priv->float_window);
533 gtk_style_context_restore (context);
538 effective_handle_position (GtkHandleBox *hb)
540 GtkHandleBoxPrivate *priv = hb->priv;
543 if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
544 handle_position = priv->handle_position;
547 switch (priv->handle_position)
550 handle_position = GTK_POS_RIGHT;
553 handle_position = GTK_POS_LEFT;
556 handle_position = priv->handle_position;
561 return handle_position;
565 gtk_handle_box_size_request (GtkWidget *widget,
566 GtkRequisition *requisition)
568 GtkBin *bin = GTK_BIN (widget);
569 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
570 GtkHandleBoxPrivate *priv = hb->priv;
571 GtkRequisition child_requisition;
573 gint handle_position;
575 handle_position = effective_handle_position (hb);
577 if (handle_position == GTK_POS_LEFT ||
578 handle_position == GTK_POS_RIGHT)
580 requisition->width = DRAG_HANDLE_SIZE;
581 requisition->height = 0;
585 requisition->width = 0;
586 requisition->height = DRAG_HANDLE_SIZE;
589 child = gtk_bin_get_child (bin);
590 /* if our child is not visible, we still request its size, since we
591 * won't have any useful hint for our size otherwise.
595 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
599 child_requisition.width = 0;
600 child_requisition.height = 0;
603 if (priv->child_detached)
605 /* FIXME: This doesn't work currently */
606 if (!priv->shrink_on_detach)
608 if (handle_position == GTK_POS_LEFT ||
609 handle_position == GTK_POS_RIGHT)
610 requisition->height += child_requisition.height;
612 requisition->width += child_requisition.width;
616 GtkStyleContext *context;
620 context = gtk_widget_get_style_context (widget);
621 state = gtk_widget_get_state_flags (widget);
622 gtk_style_context_get_padding (context, state, &padding);
624 if (handle_position == GTK_POS_LEFT ||
625 handle_position == GTK_POS_RIGHT)
626 requisition->height += padding.top;
628 requisition->width += padding.left;
635 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
636 requisition->width += border_width * 2;
637 requisition->height += border_width * 2;
641 requisition->width += child_requisition.width;
642 requisition->height += child_requisition.height;
646 requisition->width += CHILDLESS_SIZE;
647 requisition->height += CHILDLESS_SIZE;
653 gtk_handle_box_get_preferred_width (GtkWidget *widget,
657 GtkRequisition requisition;
659 gtk_handle_box_size_request (widget, &requisition);
661 *minimum = *natural = requisition.width;
665 gtk_handle_box_get_preferred_height (GtkWidget *widget,
669 GtkRequisition requisition;
671 gtk_handle_box_size_request (widget, &requisition);
673 *minimum = *natural = requisition.height;
678 gtk_handle_box_size_allocate (GtkWidget *widget,
679 GtkAllocation *allocation)
681 GtkBin *bin = GTK_BIN (widget);
682 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
683 GtkHandleBoxPrivate *priv = hb->priv;
684 GtkRequisition child_requisition;
686 gint handle_position;
688 handle_position = effective_handle_position (hb);
690 child = gtk_bin_get_child (bin);
694 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
698 child_requisition.width = 0;
699 child_requisition.height = 0;
702 gtk_widget_set_allocation (widget, allocation);
704 if (gtk_widget_get_realized (widget))
705 gdk_window_move_resize (gtk_widget_get_window (widget),
706 allocation->x, allocation->y,
707 allocation->width, allocation->height);
709 if (child != NULL && gtk_widget_get_visible (child))
711 GtkAllocation child_allocation;
714 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
716 child_allocation.x = border_width;
717 child_allocation.y = border_width;
718 if (handle_position == GTK_POS_LEFT)
719 child_allocation.x += DRAG_HANDLE_SIZE;
720 else if (handle_position == GTK_POS_TOP)
721 child_allocation.y += DRAG_HANDLE_SIZE;
723 if (priv->child_detached)
728 child_allocation.width = child_requisition.width;
729 child_allocation.height = child_requisition.height;
731 float_width = child_allocation.width + 2 * border_width;
732 float_height = child_allocation.height + 2 * border_width;
734 if (handle_position == GTK_POS_LEFT ||
735 handle_position == GTK_POS_RIGHT)
736 float_width += DRAG_HANDLE_SIZE;
738 float_height += DRAG_HANDLE_SIZE;
740 if (gtk_widget_get_realized (widget))
742 gdk_window_resize (priv->float_window,
745 gdk_window_move_resize (priv->bin_window,
754 child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
755 child_allocation.height = MAX (1, (gint) allocation->height - 2 * border_width);
757 if (handle_position == GTK_POS_LEFT ||
758 handle_position == GTK_POS_RIGHT)
759 child_allocation.width -= DRAG_HANDLE_SIZE;
761 child_allocation.height -= DRAG_HANDLE_SIZE;
763 if (gtk_widget_get_realized (widget))
764 gdk_window_move_resize (priv->bin_window,
771 gtk_widget_size_allocate (child, &child_allocation);
776 gtk_handle_box_draw_ghost (GtkHandleBox *hb,
779 GtkWidget *widget = GTK_WIDGET (hb);
781 GtkStyleContext *context;
786 gint allocation_width;
787 gint allocation_height;
788 gint handle_position;
790 handle_position = effective_handle_position (hb);
791 allocation_width = gtk_widget_get_allocated_width (widget);
792 allocation_height = gtk_widget_get_allocated_height (widget);
794 if (handle_position == GTK_POS_LEFT ||
795 handle_position == GTK_POS_RIGHT)
797 x = handle_position == GTK_POS_LEFT ? 0 : allocation_width - DRAG_HANDLE_SIZE;
799 width = DRAG_HANDLE_SIZE;
800 height = allocation_height;
805 y = handle_position == GTK_POS_TOP ? 0 : allocation_height - DRAG_HANDLE_SIZE;
806 width = allocation_width;
807 height = DRAG_HANDLE_SIZE;
810 context = gtk_widget_get_style_context (widget);
811 state = gtk_widget_get_state_flags (widget);
813 gtk_style_context_save (context);
814 gtk_style_context_set_state (context, state);
816 gtk_render_background (context, cr, x, y, width, height);
817 gtk_render_frame (context, cr, x, y, width, height);
819 if (handle_position == GTK_POS_LEFT ||
820 handle_position == GTK_POS_RIGHT)
821 gtk_render_line (context, cr,
822 handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
823 allocation_height / 2,
824 handle_position == GTK_POS_LEFT ? allocation_width : allocation_width - DRAG_HANDLE_SIZE,
825 allocation_height / 2);
827 gtk_render_line (context, cr,
828 allocation_width / 2,
829 handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
830 allocation_width / 2,
831 handle_position == GTK_POS_TOP ? allocation_height : allocation_height - DRAG_HANDLE_SIZE);
833 gtk_style_context_restore (context);
837 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
840 GtkHandleBoxPrivate *priv;
842 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
844 priv = handle_box->priv;
846 if ((GtkShadowType) priv->shadow_type != type)
848 priv->shadow_type = type;
849 g_object_notify (G_OBJECT (handle_box), "shadow-type");
850 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
855 * gtk_handle_box_get_shadow_type:
856 * @handle_box: a #GtkHandleBox
858 * Gets the type of shadow drawn around the handle box. See
859 * gtk_handle_box_set_shadow_type().
861 * Return value: the type of shadow currently drawn around the handle box.
864 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
866 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
868 return handle_box->priv->shadow_type;
872 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
873 GtkPositionType position)
875 GtkHandleBoxPrivate *priv;
877 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
879 priv = handle_box->priv;
881 if ((GtkPositionType) priv->handle_position != position)
883 priv->handle_position = position;
884 g_object_notify (G_OBJECT (handle_box), "handle-position");
885 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
890 * gtk_handle_box_get_handle_position:
891 * @handle_box: a #GtkHandleBox
893 * Gets the handle position of the handle box. See
894 * gtk_handle_box_set_handle_position().
896 * Return value: the current handle position.
899 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
901 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
903 return handle_box->priv->handle_position;
907 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
908 GtkPositionType edge)
910 GtkHandleBoxPrivate *priv;
912 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
914 priv = handle_box->priv;
916 if (priv->snap_edge != edge)
918 priv->snap_edge = edge;
920 g_object_freeze_notify (G_OBJECT (handle_box));
921 g_object_notify (G_OBJECT (handle_box), "snap-edge");
922 g_object_notify (G_OBJECT (handle_box), "snap-edge-set");
923 g_object_thaw_notify (G_OBJECT (handle_box));
928 * gtk_handle_box_get_snap_edge:
929 * @handle_box: a #GtkHandleBox
931 * Gets the edge used for determining reattachment of the handle box. See
932 * gtk_handle_box_set_snap_edge().
934 * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
935 * is determined (as per default) from the handle position.
938 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
940 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
942 return handle_box->priv->snap_edge;
946 * gtk_handle_box_get_child_detached:
947 * @handle_box: a #GtkHandleBox
949 * Whether the handlebox's child is currently detached.
951 * Return value: %TRUE if the child is currently detached, otherwise %FALSE
956 gtk_handle_box_get_child_detached (GtkHandleBox *handle_box)
958 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), FALSE);
960 return handle_box->priv->child_detached;
964 gtk_handle_box_paint (GtkWidget *widget,
967 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
968 GtkHandleBoxPrivate *priv = hb->priv;
969 GtkBin *bin = GTK_BIN (widget);
970 GtkStyleContext *context;
975 gint handle_position;
977 handle_position = effective_handle_position (hb);
979 width = gdk_window_get_width (priv->bin_window);
980 height = gdk_window_get_height (priv->bin_window);
982 context = gtk_widget_get_style_context (widget);
983 state = gtk_widget_get_state_flags (widget);
985 gtk_style_context_save (context);
986 gtk_style_context_set_state (context, state);
988 gtk_render_background (context, cr, 0, 0, width, height);
989 gtk_render_frame (context, cr, 0, 0, width, height);
991 switch (handle_position)
996 rect.width = DRAG_HANDLE_SIZE;
997 rect.height = height;
1000 rect.x = width - DRAG_HANDLE_SIZE;
1002 rect.width = DRAG_HANDLE_SIZE;
1003 rect.height = height;
1009 rect.height = DRAG_HANDLE_SIZE;
1011 case GTK_POS_BOTTOM:
1013 rect.y = height - DRAG_HANDLE_SIZE;
1015 rect.height = DRAG_HANDLE_SIZE;
1018 g_assert_not_reached ();
1022 gtk_render_handle (context, cr,
1023 rect.x, rect.y, rect.width, rect.height);
1025 child = gtk_bin_get_child (bin);
1026 if (child != NULL && gtk_widget_get_visible (child))
1027 GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->draw (widget, cr);
1029 gtk_style_context_restore (context);
1033 gtk_handle_box_draw (GtkWidget *widget,
1036 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1037 GtkHandleBoxPrivate *priv = hb->priv;
1039 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
1041 if (priv->child_detached)
1042 gtk_handle_box_draw_ghost (hb, cr);
1044 else if (gtk_cairo_should_draw_window (cr, priv->bin_window))
1045 gtk_handle_box_paint (widget, cr);
1051 gtk_handle_box_get_invisible (void)
1053 static GtkWidget *handle_box_invisible = NULL;
1055 if (!handle_box_invisible)
1057 handle_box_invisible = gtk_invisible_new ();
1058 gtk_widget_show (handle_box_invisible);
1061 return handle_box_invisible;
1065 gtk_handle_box_grab_event (GtkWidget *widget,
1069 GtkHandleBoxPrivate *priv = hb->priv;
1071 switch (event->type)
1073 case GDK_BUTTON_RELEASE:
1074 if (priv->in_drag) /* sanity check */
1076 gtk_handle_box_end_drag (hb, event->button.time);
1081 case GDK_MOTION_NOTIFY:
1082 return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event);
1093 gtk_handle_box_button_press (GtkWidget *widget,
1094 GdkEventButton *event)
1096 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1097 GtkHandleBoxPrivate *priv = hb->priv;
1098 gboolean event_handled;
1100 gint handle_position;
1102 handle_position = effective_handle_position (hb);
1104 event_handled = FALSE;
1105 if ((event->button == 1) &&
1106 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1111 if (event->window != priv->bin_window)
1114 child = gtk_bin_get_child (GTK_BIN (hb));
1118 GtkAllocation child_allocation;
1121 gtk_widget_get_allocation (child, &child_allocation);
1122 border_width = gtk_container_get_border_width (GTK_CONTAINER (hb));
1124 switch (handle_position)
1127 in_handle = event->x < DRAG_HANDLE_SIZE;
1130 in_handle = event->y < DRAG_HANDLE_SIZE;
1133 in_handle = event->x > 2 * border_width + child_allocation.width;
1135 case GTK_POS_BOTTOM:
1136 in_handle = event->y > 2 * border_width + child_allocation.height;
1146 event_handled = TRUE;
1151 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
1153 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1155 gint root_x, root_y;
1157 gtk_invisible_set_screen (GTK_INVISIBLE (invisible),
1158 gtk_widget_get_screen (GTK_WIDGET (hb)));
1159 gdk_window_get_origin (priv->bin_window, &root_x, &root_y);
1161 priv->orig_x = event->x_root;
1162 priv->orig_y = event->y_root;
1164 priv->float_allocation.x = root_x - event->x_root;
1165 priv->float_allocation.y = root_y - event->y_root;
1166 priv->float_allocation.width = gdk_window_get_width (priv->bin_window);
1167 priv->float_allocation.height = gdk_window_get_height (priv->bin_window);
1169 window = gtk_widget_get_window (widget);
1170 if (gdk_window_is_viewable (window))
1172 gdk_window_get_origin (window, &root_x, &root_y);
1174 priv->attach_allocation.x = root_x;
1175 priv->attach_allocation.y = root_y;
1176 priv->attach_allocation.width = gdk_window_get_width (window);
1177 priv->attach_allocation.height = gdk_window_get_height (window);
1181 priv->attach_allocation.x = -1;
1182 priv->attach_allocation.y = -1;
1183 priv->attach_allocation.width = 0;
1184 priv->attach_allocation.height = 0;
1186 priv->in_drag = TRUE;
1187 priv->grab_device = event->device;
1188 fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1190 if (gdk_device_grab (event->device,
1191 gtk_widget_get_window (invisible),
1192 GDK_OWNERSHIP_WINDOW,
1194 (GDK_BUTTON1_MOTION_MASK |
1195 GDK_POINTER_MOTION_HINT_MASK |
1196 GDK_BUTTON_RELEASE_MASK),
1198 event->time) != GDK_GRAB_SUCCESS)
1200 priv->in_drag = FALSE;
1201 priv->grab_device = NULL;
1205 gtk_device_grab_add (invisible, priv->grab_device, TRUE);
1206 g_signal_connect (invisible, "event",
1207 G_CALLBACK (gtk_handle_box_grab_event), hb);
1210 g_object_unref (fleur);
1211 event_handled = TRUE;
1213 else if (priv->child_detached) /* Double click */
1215 gtk_handle_box_reattach (hb);
1220 return event_handled;
1224 gtk_handle_box_motion (GtkWidget *widget,
1225 GdkEventMotion *event)
1227 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1228 GtkHandleBoxPrivate *priv = hb->priv;
1232 gboolean is_snapped = FALSE;
1233 gint handle_position;
1234 GdkGeometry geometry;
1235 GdkScreen *screen, *pointer_screen;
1239 handle_position = effective_handle_position (hb);
1241 /* Calculate the attachment point on the float, if the float
1246 screen = gtk_widget_get_screen (widget);
1247 gdk_device_get_position (event->device,
1250 if (pointer_screen != screen)
1252 new_x = priv->orig_x;
1253 new_y = priv->orig_y;
1256 new_x += priv->float_allocation.x;
1257 new_y += priv->float_allocation.y;
1259 snap_edge = priv->snap_edge;
1260 if (snap_edge == -1)
1261 snap_edge = (handle_position == GTK_POS_LEFT ||
1262 handle_position == GTK_POS_RIGHT) ?
1263 GTK_POS_TOP : GTK_POS_LEFT;
1265 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1269 snap_edge = GTK_POS_RIGHT;
1272 snap_edge = GTK_POS_LEFT;
1278 /* First, check if the snapped edge is aligned
1283 is_snapped = abs (priv->attach_allocation.y - new_y) < TOLERANCE;
1285 case GTK_POS_BOTTOM:
1286 is_snapped = abs (priv->attach_allocation.y + (gint)priv->attach_allocation.height -
1287 new_y - (gint)priv->float_allocation.height) < TOLERANCE;
1290 is_snapped = abs (priv->attach_allocation.x - new_x) < TOLERANCE;
1293 is_snapped = abs (priv->attach_allocation.x + (gint)priv->attach_allocation.width -
1294 new_x - (gint)priv->float_allocation.width) < TOLERANCE;
1298 /* Next, check if coordinates in the other direction are sufficiently
1303 gint float_pos1 = 0; /* Initialize to suppress warnings */
1304 gint float_pos2 = 0;
1305 gint attach_pos1 = 0;
1306 gint attach_pos2 = 0;
1311 case GTK_POS_BOTTOM:
1312 attach_pos1 = priv->attach_allocation.x;
1313 attach_pos2 = priv->attach_allocation.x + priv->attach_allocation.width;
1315 float_pos2 = new_x + priv->float_allocation.width;
1319 attach_pos1 = priv->attach_allocation.y;
1320 attach_pos2 = priv->attach_allocation.y + priv->attach_allocation.height;
1322 float_pos2 = new_y + priv->float_allocation.height;
1326 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1327 (attach_pos2 + TOLERANCE > float_pos2)) ||
1328 ((float_pos1 - TOLERANCE < attach_pos1) &&
1329 (float_pos2 + TOLERANCE > attach_pos2));
1332 child = gtk_bin_get_child (GTK_BIN (hb));
1336 if (priv->child_detached)
1338 priv->child_detached = FALSE;
1339 gdk_window_hide (priv->float_window);
1340 gdk_window_reparent (priv->bin_window, gtk_widget_get_window (widget), 0, 0);
1341 priv->float_window_mapped = FALSE;
1343 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1347 gtk_widget_queue_resize (widget);
1354 width = gdk_window_get_width (priv->float_window);
1355 height = gdk_window_get_height (priv->float_window);
1357 switch (handle_position)
1360 new_y += ((gint)priv->float_allocation.height - height) / 2;
1363 new_x += (gint)priv->float_allocation.width - width;
1364 new_y += ((gint)priv->float_allocation.height - height) / 2;
1367 new_x += ((gint)priv->float_allocation.width - width) / 2;
1369 case GTK_POS_BOTTOM:
1370 new_x += ((gint)priv->float_allocation.width - width) / 2;
1371 new_y += (gint)priv->float_allocation.height - height;
1375 if (priv->child_detached)
1377 gdk_window_move (priv->float_window, new_x, new_y);
1378 gdk_window_raise (priv->float_window);
1383 GtkRequisition child_requisition;
1385 priv->child_detached = TRUE;
1389 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
1393 child_requisition.width = 0;
1394 child_requisition.height = 0;
1397 border_width = gtk_container_get_border_width (GTK_CONTAINER (hb));
1398 width = child_requisition.width + 2 * border_width;
1399 height = child_requisition.height + 2 * border_width;
1401 if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT)
1402 width += DRAG_HANDLE_SIZE;
1404 height += DRAG_HANDLE_SIZE;
1406 gdk_window_move_resize (priv->float_window, new_x, new_y, width, height);
1407 gdk_window_reparent (priv->bin_window, priv->float_window, 0, 0);
1408 gdk_window_set_geometry_hints (priv->float_window, &geometry, GDK_HINT_POS);
1409 gdk_window_show (priv->float_window);
1410 priv->float_window_mapped = TRUE;
1412 /* this extra move is necessary if we use decorations, or our
1413 * window manager insists on decorations.
1415 gdk_display_sync (gtk_widget_get_display (widget));
1416 gdk_window_move (priv->float_window, new_x, new_y);
1417 gdk_display_sync (gtk_widget_get_display (widget));
1420 handle_box_signals[SIGNAL_CHILD_DETACHED],
1424 gtk_widget_queue_resize (widget);
1432 gtk_handle_box_add (GtkContainer *container,
1435 GtkHandleBoxPrivate *priv = GTK_HANDLE_BOX (container)->priv;
1437 gtk_widget_set_parent_window (widget, priv->bin_window);
1438 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->add (container, widget);
1442 gtk_handle_box_remove (GtkContainer *container,
1445 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->remove (container, widget);
1447 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1451 gtk_handle_box_delete_event (GtkWidget *widget,
1454 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1455 GtkHandleBoxPrivate *priv = hb->priv;
1457 if (event->window == priv->float_window)
1459 gtk_handle_box_reattach (hb);
1468 gtk_handle_box_reattach (GtkHandleBox *hb)
1470 GtkHandleBoxPrivate *priv = hb->priv;
1472 GtkWidget *widget = GTK_WIDGET (hb);
1474 if (priv->child_detached)
1476 priv->child_detached = FALSE;
1477 if (gtk_widget_get_realized (widget))
1479 gdk_window_hide (priv->float_window);
1480 gdk_window_reparent (priv->bin_window, gtk_widget_get_window (widget),
1483 child = gtk_bin_get_child (GTK_BIN (hb));
1486 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1491 priv->float_window_mapped = FALSE;
1494 gtk_handle_box_end_drag (hb, GDK_CURRENT_TIME);
1496 gtk_widget_queue_resize (GTK_WIDGET (hb));
1500 gtk_handle_box_end_drag (GtkHandleBox *hb,
1503 GtkHandleBoxPrivate *priv = hb->priv;
1504 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1506 priv->in_drag = FALSE;
1508 gtk_device_grab_remove (invisible, priv->grab_device);
1509 gdk_device_ungrab (priv->grab_device, time);
1510 g_signal_handlers_disconnect_by_func (invisible,
1511 G_CALLBACK (gtk_handle_box_grab_event),
1514 priv->grab_device = NULL;