1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 1998 Elliot Lee
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include "gtkhandlebox.h"
31 #include "gtkinvisible.h"
33 #include "gtkmarshalers.h"
34 #include "gtkwindow.h"
35 #include "gtkprivate.h"
39 typedef struct _GtkHandleBoxPrivate GtkHandleBoxPrivate;
41 struct _GtkHandleBoxPrivate
57 #define DRAG_HANDLE_SIZE 10
58 #define CHILDLESS_SIZE 25
59 #define GHOST_HEIGHT 3
63 SIGNAL_CHILD_ATTACHED,
64 SIGNAL_CHILD_DETACHED,
68 /* The algorithm for docking and redocking implemented here
69 * has a couple of nice properties:
71 * 1) During a single drag, docking always occurs at the
72 * the same cursor position. This means that the users
73 * motions are reversible, and that you won't
74 * undock/dock oscillations.
76 * 2) Docking generally occurs at user-visible features.
77 * The user, once they figure out to redock, will
78 * have useful information about doing it again in
81 * Please try to preserve these properties if you
82 * change the algorithm. (And the current algorithm
83 * is far from ideal). Briefly, the current algorithm
84 * for deciding whether the handlebox is docked or not:
86 * 1) The decision is done by comparing two rectangles - the
87 * allocation if the widget at the start of the drag,
88 * and the boundary of hb->bin_window at the start of
89 * of the drag offset by the distance that the cursor
92 * 2) These rectangles must have one edge, the "snap_edge"
93 * of the handlebox, aligned within TOLERANCE.
95 * 3) On the other dimension, the extents of one rectangle
96 * must be contained in the extents of the other,
97 * extended by tolerance. That is, either we can have:
99 * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE->
100 * <--------float_window-------------------->
104 * <-TOLERANCE-|------float_window--------------|-TOLERANCE->
105 * <--------bin_window-------------------->
108 static void gtk_handle_box_set_property (GObject *object,
112 static void gtk_handle_box_get_property (GObject *object,
116 static void gtk_handle_box_destroy (GtkObject *object);
117 static void gtk_handle_box_map (GtkWidget *widget);
118 static void gtk_handle_box_unmap (GtkWidget *widget);
119 static void gtk_handle_box_realize (GtkWidget *widget);
120 static void gtk_handle_box_unrealize (GtkWidget *widget);
121 static void gtk_handle_box_style_set (GtkWidget *widget,
122 GtkStyle *previous_style);
123 static void gtk_handle_box_size_request (GtkWidget *widget,
124 GtkRequisition *requisition);
125 static void gtk_handle_box_size_allocate (GtkWidget *widget,
126 GtkAllocation *real_allocation);
127 static void gtk_handle_box_add (GtkContainer *container,
129 static void gtk_handle_box_remove (GtkContainer *container,
131 static void gtk_handle_box_draw_ghost (GtkHandleBox *hb);
132 static void gtk_handle_box_paint (GtkWidget *widget,
133 GdkEventExpose *event,
135 static gint gtk_handle_box_expose (GtkWidget *widget,
136 GdkEventExpose *event);
137 static gint gtk_handle_box_button_changed (GtkWidget *widget,
138 GdkEventButton *event);
139 static gint gtk_handle_box_motion (GtkWidget *widget,
140 GdkEventMotion *event);
141 static gint gtk_handle_box_delete_event (GtkWidget *widget,
143 static void gtk_handle_box_reattach (GtkHandleBox *hb);
144 static void gtk_handle_box_end_drag (GtkHandleBox *hb,
147 static guint handle_box_signals[SIGNAL_LAST] = { 0 };
149 G_DEFINE_TYPE (GtkHandleBox, gtk_handle_box, GTK_TYPE_BIN)
152 gtk_handle_box_class_init (GtkHandleBoxClass *class)
154 GObjectClass *gobject_class;
155 GtkObjectClass *object_class;
156 GtkWidgetClass *widget_class;
157 GtkContainerClass *container_class;
159 gobject_class = (GObjectClass *) class;
160 object_class = (GtkObjectClass *) class;
161 widget_class = (GtkWidgetClass *) class;
162 container_class = (GtkContainerClass *) class;
164 gobject_class->set_property = gtk_handle_box_set_property;
165 gobject_class->get_property = gtk_handle_box_get_property;
167 g_object_class_install_property (gobject_class,
169 g_param_spec_enum ("shadow", NULL,
170 P_("Deprecated property, use shadow_type instead"),
171 GTK_TYPE_SHADOW_TYPE,
173 GTK_PARAM_READWRITE));
174 g_object_class_install_property (gobject_class,
176 g_param_spec_enum ("shadow-type",
178 P_("Appearance of the shadow that surrounds the container"),
179 GTK_TYPE_SHADOW_TYPE,
181 GTK_PARAM_READWRITE));
183 g_object_class_install_property (gobject_class,
184 PROP_HANDLE_POSITION,
185 g_param_spec_enum ("handle-position",
186 P_("Handle position"),
187 P_("Position of the handle relative to the child widget"),
188 GTK_TYPE_POSITION_TYPE,
190 GTK_PARAM_READWRITE));
192 g_object_class_install_property (gobject_class,
194 g_param_spec_enum ("snap-edge",
196 P_("Side of the handlebox that's lined up with the docking point to dock the handlebox"),
197 GTK_TYPE_POSITION_TYPE,
199 GTK_PARAM_READWRITE));
201 g_object_class_install_property (gobject_class,
203 g_param_spec_boolean ("snap-edge-set",
205 P_("Whether to use the value from the snap_edge property or a value derived from handle_position"),
207 GTK_PARAM_READWRITE));
209 g_object_class_install_property (gobject_class,
211 g_param_spec_boolean ("child-detached",
212 P_("Child Detached"),
213 P_("A boolean value indicating whether the handlebox's child is attached or detached."),
215 GTK_PARAM_READABLE));
217 object_class->destroy = gtk_handle_box_destroy;
219 widget_class->map = gtk_handle_box_map;
220 widget_class->unmap = gtk_handle_box_unmap;
221 widget_class->realize = gtk_handle_box_realize;
222 widget_class->unrealize = gtk_handle_box_unrealize;
223 widget_class->style_set = gtk_handle_box_style_set;
224 widget_class->size_request = gtk_handle_box_size_request;
225 widget_class->size_allocate = gtk_handle_box_size_allocate;
226 widget_class->expose_event = gtk_handle_box_expose;
227 widget_class->button_press_event = gtk_handle_box_button_changed;
228 widget_class->delete_event = gtk_handle_box_delete_event;
230 container_class->add = gtk_handle_box_add;
231 container_class->remove = gtk_handle_box_remove;
233 class->child_attached = NULL;
234 class->child_detached = NULL;
236 handle_box_signals[SIGNAL_CHILD_ATTACHED] =
237 g_signal_new (I_("child_attached"),
238 G_OBJECT_CLASS_TYPE (gobject_class),
240 G_STRUCT_OFFSET (GtkHandleBoxClass, child_attached),
242 _gtk_marshal_VOID__OBJECT,
245 handle_box_signals[SIGNAL_CHILD_DETACHED] =
246 g_signal_new (I_("child_detached"),
247 G_OBJECT_CLASS_TYPE (gobject_class),
249 G_STRUCT_OFFSET (GtkHandleBoxClass, child_detached),
251 _gtk_marshal_VOID__OBJECT,
255 g_type_class_add_private (gobject_class, sizeof (GtkHandleBoxPrivate));
258 static GtkHandleBoxPrivate *
259 gtk_handle_box_get_private (GtkHandleBox *hb)
261 return G_TYPE_INSTANCE_GET_PRIVATE (hb, GTK_TYPE_HANDLE_BOX, GtkHandleBoxPrivate);
265 gtk_handle_box_init (GtkHandleBox *handle_box)
267 GTK_WIDGET_UNSET_FLAGS (handle_box, GTK_NO_WINDOW);
269 handle_box->bin_window = NULL;
270 handle_box->float_window = NULL;
271 handle_box->shadow_type = GTK_SHADOW_OUT;
272 handle_box->handle_position = GTK_POS_LEFT;
273 handle_box->float_window_mapped = FALSE;
274 handle_box->child_detached = FALSE;
275 handle_box->in_drag = FALSE;
276 handle_box->shrink_on_detach = TRUE;
277 handle_box->snap_edge = -1;
281 gtk_handle_box_set_property (GObject *object,
286 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
291 case PROP_SHADOW_TYPE:
292 gtk_handle_box_set_shadow_type (handle_box, g_value_get_enum (value));
294 case PROP_HANDLE_POSITION:
295 gtk_handle_box_set_handle_position (handle_box, g_value_get_enum (value));
298 gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value));
300 case PROP_SNAP_EDGE_SET:
301 if (!g_value_get_boolean (value))
302 gtk_handle_box_set_snap_edge (handle_box, (GtkPositionType)-1);
305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
311 gtk_handle_box_get_property (GObject *object,
316 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
321 case PROP_SHADOW_TYPE:
322 g_value_set_enum (value, handle_box->shadow_type);
324 case PROP_HANDLE_POSITION:
325 g_value_set_enum (value, handle_box->handle_position);
328 g_value_set_enum (value,
329 (handle_box->snap_edge == -1 ?
330 GTK_POS_TOP : handle_box->snap_edge));
332 case PROP_SNAP_EDGE_SET:
333 g_value_set_boolean (value, handle_box->snap_edge != -1);
335 case PROP_CHILD_DETACHED:
336 g_value_set_boolean (value, handle_box->child_detached);
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
345 gtk_handle_box_new (void)
347 return g_object_new (GTK_TYPE_HANDLE_BOX, NULL);
351 gtk_handle_box_destroy (GtkObject *object)
353 if (GTK_OBJECT_CLASS (gtk_handle_box_parent_class)->destroy)
354 (* GTK_OBJECT_CLASS (gtk_handle_box_parent_class)->destroy) (object);
358 gtk_handle_box_map (GtkWidget *widget)
363 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
365 bin = GTK_BIN (widget);
366 hb = GTK_HANDLE_BOX (widget);
369 GTK_WIDGET_VISIBLE (bin->child) &&
370 !GTK_WIDGET_MAPPED (bin->child))
371 gtk_widget_map (bin->child);
373 if (hb->child_detached && !hb->float_window_mapped)
375 gdk_window_show (hb->float_window);
376 hb->float_window_mapped = TRUE;
379 gdk_window_show (hb->bin_window);
380 gdk_window_show (widget->window);
384 gtk_handle_box_unmap (GtkWidget *widget)
388 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
390 hb = GTK_HANDLE_BOX (widget);
392 gdk_window_hide (widget->window);
393 if (hb->float_window_mapped)
395 gdk_window_hide (hb->float_window);
396 hb->float_window_mapped = FALSE;
401 gtk_handle_box_realize (GtkWidget *widget)
403 GdkWindowAttr attributes;
404 gint attributes_mask;
407 hb = GTK_HANDLE_BOX (widget);
409 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
411 attributes.x = widget->allocation.x;
412 attributes.y = widget->allocation.y;
413 attributes.width = widget->allocation.width;
414 attributes.height = widget->allocation.height;
415 attributes.window_type = GDK_WINDOW_CHILD;
416 attributes.wclass = GDK_INPUT_OUTPUT;
417 attributes.visual = gtk_widget_get_visual (widget);
418 attributes.colormap = gtk_widget_get_colormap (widget);
419 attributes.event_mask = (gtk_widget_get_events (widget)
420 | GDK_EXPOSURE_MASK);
421 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
422 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
423 gdk_window_set_user_data (widget->window, widget);
427 attributes.width = widget->allocation.width;
428 attributes.height = widget->allocation.height;
429 attributes.window_type = GDK_WINDOW_CHILD;
430 attributes.event_mask = (gtk_widget_get_events (widget) |
432 GDK_BUTTON1_MOTION_MASK |
433 GDK_POINTER_MOTION_HINT_MASK |
434 GDK_BUTTON_PRESS_MASK |
435 GDK_BUTTON_RELEASE_MASK);
436 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
437 hb->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask);
438 gdk_window_set_user_data (hb->bin_window, widget);
439 if (GTK_BIN (hb)->child)
440 gtk_widget_set_parent_window (GTK_BIN (hb)->child, hb->bin_window);
444 attributes.width = widget->requisition.width;
445 attributes.height = widget->requisition.height;
446 attributes.window_type = GDK_WINDOW_TOPLEVEL;
447 attributes.wclass = GDK_INPUT_OUTPUT;
448 attributes.visual = gtk_widget_get_visual (widget);
449 attributes.colormap = gtk_widget_get_colormap (widget);
450 attributes.event_mask = (gtk_widget_get_events (widget) |
452 GDK_ENTER_NOTIFY_MASK |
453 GDK_LEAVE_NOTIFY_MASK |
454 GDK_FOCUS_CHANGE_MASK |
456 attributes.type_hint = GDK_WINDOW_TYPE_HINT_TOOLBAR;
457 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP | GDK_WA_TYPE_HINT;
458 hb->float_window = gdk_window_new (gtk_widget_get_root_window (widget),
459 &attributes, attributes_mask);
460 gdk_window_set_user_data (hb->float_window, widget);
461 gdk_window_set_decorations (hb->float_window, 0);
462 gdk_window_set_type_hint (hb->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
464 widget->style = gtk_style_attach (widget->style, widget->window);
465 gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (hb));
466 gtk_style_set_background (widget->style, hb->bin_window, GTK_WIDGET_STATE (hb));
467 gtk_style_set_background (widget->style, hb->float_window, GTK_WIDGET_STATE (hb));
468 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
472 gtk_handle_box_unrealize (GtkWidget *widget)
474 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
476 gdk_window_set_user_data (hb->bin_window, NULL);
477 gdk_window_destroy (hb->bin_window);
478 hb->bin_window = NULL;
479 gdk_window_set_user_data (hb->float_window, NULL);
480 gdk_window_destroy (hb->float_window);
481 hb->float_window = NULL;
483 if (GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unrealize)
484 (* GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unrealize) (widget);
488 gtk_handle_box_style_set (GtkWidget *widget,
489 GtkStyle *previous_style)
491 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
493 if (GTK_WIDGET_REALIZED (widget) &&
494 !GTK_WIDGET_NO_WINDOW (widget))
496 gtk_style_set_background (widget->style, widget->window,
498 gtk_style_set_background (widget->style, hb->bin_window, widget->state);
499 gtk_style_set_background (widget->style, hb->float_window, widget->state);
504 effective_handle_position (GtkHandleBox *hb)
508 if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
509 handle_position = hb->handle_position;
512 switch (hb->handle_position)
515 handle_position = GTK_POS_RIGHT;
518 handle_position = GTK_POS_LEFT;
521 handle_position = hb->handle_position;
526 return handle_position;
530 gtk_handle_box_size_request (GtkWidget *widget,
531 GtkRequisition *requisition)
535 GtkRequisition child_requisition;
536 gint handle_position;
538 bin = GTK_BIN (widget);
539 hb = GTK_HANDLE_BOX (widget);
541 handle_position = effective_handle_position (hb);
543 if (handle_position == GTK_POS_LEFT ||
544 handle_position == GTK_POS_RIGHT)
546 requisition->width = DRAG_HANDLE_SIZE;
547 requisition->height = 0;
551 requisition->width = 0;
552 requisition->height = DRAG_HANDLE_SIZE;
555 /* if our child is not visible, we still request its size, since we
556 * won't have any useful hint for our size otherwise.
559 gtk_widget_size_request (bin->child, &child_requisition);
562 child_requisition.width = 0;
563 child_requisition.height = 0;
566 if (hb->child_detached)
568 /* FIXME: This doesn't work currently */
569 if (!hb->shrink_on_detach)
571 if (handle_position == GTK_POS_LEFT ||
572 handle_position == GTK_POS_RIGHT)
573 requisition->height += child_requisition.height;
575 requisition->width += child_requisition.width;
579 if (handle_position == GTK_POS_LEFT ||
580 handle_position == GTK_POS_RIGHT)
581 requisition->height += widget->style->ythickness;
583 requisition->width += widget->style->xthickness;
588 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
589 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
593 requisition->width += child_requisition.width;
594 requisition->height += child_requisition.height;
598 requisition->width += CHILDLESS_SIZE;
599 requisition->height += CHILDLESS_SIZE;
605 gtk_handle_box_size_allocate (GtkWidget *widget,
606 GtkAllocation *allocation)
610 GtkRequisition child_requisition;
611 gint handle_position;
613 bin = GTK_BIN (widget);
614 hb = GTK_HANDLE_BOX (widget);
616 handle_position = effective_handle_position (hb);
619 gtk_widget_get_child_requisition (bin->child, &child_requisition);
622 child_requisition.width = 0;
623 child_requisition.height = 0;
626 widget->allocation = *allocation;
628 if (GTK_WIDGET_REALIZED (hb))
629 gdk_window_move_resize (widget->window,
630 widget->allocation.x,
631 widget->allocation.y,
632 widget->allocation.width,
633 widget->allocation.height);
636 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
638 GtkAllocation child_allocation;
641 border_width = GTK_CONTAINER (widget)->border_width;
643 child_allocation.x = border_width;
644 child_allocation.y = border_width;
645 if (handle_position == GTK_POS_LEFT)
646 child_allocation.x += DRAG_HANDLE_SIZE;
647 else if (handle_position == GTK_POS_TOP)
648 child_allocation.y += DRAG_HANDLE_SIZE;
650 if (hb->child_detached)
655 child_allocation.width = child_requisition.width;
656 child_allocation.height = child_requisition.height;
658 float_width = child_allocation.width + 2 * border_width;
659 float_height = child_allocation.height + 2 * border_width;
661 if (handle_position == GTK_POS_LEFT ||
662 handle_position == GTK_POS_RIGHT)
663 float_width += DRAG_HANDLE_SIZE;
665 float_height += DRAG_HANDLE_SIZE;
667 if (GTK_WIDGET_REALIZED (hb))
669 gdk_window_resize (hb->float_window,
672 gdk_window_move_resize (hb->bin_window,
681 child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
682 child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
684 if (handle_position == GTK_POS_LEFT ||
685 handle_position == GTK_POS_RIGHT)
686 child_allocation.width -= DRAG_HANDLE_SIZE;
688 child_allocation.height -= DRAG_HANDLE_SIZE;
690 if (GTK_WIDGET_REALIZED (hb))
691 gdk_window_move_resize (hb->bin_window,
694 widget->allocation.width,
695 widget->allocation.height);
698 gtk_widget_size_allocate (bin->child, &child_allocation);
703 gtk_handle_box_draw_ghost (GtkHandleBox *hb)
710 gint handle_position;
712 widget = GTK_WIDGET (hb);
714 handle_position = effective_handle_position (hb);
715 if (handle_position == GTK_POS_LEFT ||
716 handle_position == GTK_POS_RIGHT)
718 x = handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
720 width = DRAG_HANDLE_SIZE;
721 height = widget->allocation.height;
726 y = handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
727 width = widget->allocation.width;
728 height = DRAG_HANDLE_SIZE;
730 gtk_paint_shadow (widget->style,
732 GTK_WIDGET_STATE (widget),
733 GTK_SHADOW_ETCHED_IN,
734 NULL, widget, "handle",
739 if (handle_position == GTK_POS_LEFT ||
740 handle_position == GTK_POS_RIGHT)
741 gtk_paint_hline (widget->style,
743 GTK_WIDGET_STATE (widget),
744 NULL, widget, "handlebox",
745 handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
746 handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
747 widget->allocation.height / 2);
749 gtk_paint_vline (widget->style,
751 GTK_WIDGET_STATE (widget),
752 NULL, widget, "handlebox",
753 handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
754 handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
755 widget->allocation.width / 2);
759 draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
760 GdkRectangle *clip, GtkOrientation orientation)
762 gtk_paint_handle (widget->style, window, GTK_STATE_NORMAL, shadow,
763 clip, widget, "handlebox",
764 rect->x, rect->y, rect->width, rect->height,
769 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
772 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
774 if ((GtkShadowType) handle_box->shadow_type != type)
776 handle_box->shadow_type = type;
777 g_object_notify (G_OBJECT (handle_box), "shadow-type");
778 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
783 * gtk_handle_box_get_shadow_type:
784 * @handle_box: a #GtkHandleBox
786 * Gets the type of shadow drawn around the handle box. See
787 * gtk_handle_box_set_shadow_type().
789 * Return value: the type of shadow currently drawn around the handle box.
792 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
794 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
796 return handle_box->shadow_type;
800 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
801 GtkPositionType position)
803 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
805 if ((GtkPositionType) handle_box->handle_position != position)
807 handle_box->handle_position = position;
808 g_object_notify (G_OBJECT (handle_box), "handle-position");
809 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
814 * gtk_handle_box_get_handle_position:
815 * @handle_box: a #GtkHandleBox
817 * Gets the handle position of the handle box. See
818 * gtk_handle_box_set_handle_position().
820 * Return value: the current handle position.
823 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
825 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
827 return handle_box->handle_position;
831 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
832 GtkPositionType edge)
834 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
836 if (handle_box->snap_edge != edge)
838 handle_box->snap_edge = edge;
840 g_object_freeze_notify (G_OBJECT (handle_box));
841 g_object_notify (G_OBJECT (handle_box), "snap-edge");
842 g_object_notify (G_OBJECT (handle_box), "snap-edge-set");
843 g_object_thaw_notify (G_OBJECT (handle_box));
848 * gtk_handle_box_get_snap_edge:
849 * @handle_box: a #GtkHandleBox
851 * Gets the edge used for determining reattachment of the handle box. See
852 * gtk_handle_box_set_snap_edge().
854 * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
855 * is determined (as per default) from the handle position.
858 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
860 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
862 return handle_box->snap_edge;
866 * gtk_handle_box_get_child_detached:
867 * @handle_box: a #GtkHandleBox
869 * Whether the handlebox's child is currently detached.
871 * Return value: %TRUE if the child is currently detached, otherwise %FALSE
876 gtk_handle_box_get_child_detached (GtkHandleBox *handle_box)
878 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), FALSE);
880 return handle_box->child_detached;
884 gtk_handle_box_paint (GtkWidget *widget,
886 GdkEventExpose *event,
894 gint handle_position;
895 GtkOrientation handle_orientation;
897 bin = GTK_BIN (widget);
898 hb = GTK_HANDLE_BOX (widget);
900 handle_position = effective_handle_position (hb);
902 gdk_drawable_get_size (hb->bin_window, &width, &height);
905 gtk_paint_box (widget->style,
907 GTK_WIDGET_STATE (widget),
909 area, widget, "handlebox_bin",
912 gtk_paint_box (widget->style,
914 GTK_WIDGET_STATE (widget),
916 &event->area, widget, "handlebox_bin",
919 /* We currently draw the handle _above_ the relief of the handlebox.
920 * it could also be drawn on the same level...
922 hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
923 hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
927 switch (handle_position)
932 rect.width = DRAG_HANDLE_SIZE;
933 rect.height = height;
934 handle_orientation = GTK_ORIENTATION_VERTICAL;
937 rect.x = width - DRAG_HANDLE_SIZE;
939 rect.width = DRAG_HANDLE_SIZE;
940 rect.height = height;
941 handle_orientation = GTK_ORIENTATION_VERTICAL;
947 rect.height = DRAG_HANDLE_SIZE;
948 handle_orientation = GTK_ORIENTATION_HORIZONTAL;
952 rect.y = height - DRAG_HANDLE_SIZE;
954 rect.height = DRAG_HANDLE_SIZE;
955 handle_orientation = GTK_ORIENTATION_HORIZONTAL;
958 g_assert_not_reached ();
962 if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
963 draw_textured_frame (widget, hb->bin_window, &rect,
965 event ? &event->area : area,
968 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
969 (* GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->expose_event) (widget, event);
973 gtk_handle_box_expose (GtkWidget *widget,
974 GdkEventExpose *event)
978 if (GTK_WIDGET_DRAWABLE (widget))
980 hb = GTK_HANDLE_BOX (widget);
982 if (event->window == widget->window)
984 if (hb->child_detached)
985 gtk_handle_box_draw_ghost (hb);
988 gtk_handle_box_paint (widget, event, NULL);
995 gtk_handle_box_get_invisible (void)
997 static GtkWidget *handle_box_invisible = NULL;
999 if (!handle_box_invisible)
1001 handle_box_invisible = gtk_invisible_new ();
1002 gtk_widget_show (handle_box_invisible);
1005 return handle_box_invisible;
1009 gtk_handle_box_grab_event (GtkWidget *widget,
1013 switch (event->type)
1015 case GDK_BUTTON_RELEASE:
1016 if (hb->in_drag) /* sanity check */
1018 gtk_handle_box_end_drag (hb, event->button.time);
1023 case GDK_MOTION_NOTIFY:
1024 return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event);
1035 gtk_handle_box_button_changed (GtkWidget *widget,
1036 GdkEventButton *event)
1039 gboolean event_handled;
1041 gint handle_position;
1043 hb = GTK_HANDLE_BOX (widget);
1045 handle_position = effective_handle_position (hb);
1047 event_handled = FALSE;
1048 if ((event->button == 1) &&
1049 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1054 if (event->window != hb->bin_window)
1057 child = GTK_BIN (hb)->child;
1061 switch (handle_position)
1064 in_handle = event->x < DRAG_HANDLE_SIZE;
1067 in_handle = event->y < DRAG_HANDLE_SIZE;
1070 in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width;
1072 case GTK_POS_BOTTOM:
1073 in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height;
1083 event_handled = TRUE;
1088 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
1090 GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1091 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1092 gint desk_x, desk_y;
1093 gint root_x, root_y;
1096 gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y);
1097 gdk_window_get_origin (hb->bin_window, &root_x, &root_y);
1098 gdk_drawable_get_size (hb->bin_window, &width, &height);
1100 private->orig_x = event->x_root;
1101 private->orig_y = event->y_root;
1103 hb->float_allocation.x = root_x - event->x_root;
1104 hb->float_allocation.y = root_y - event->y_root;
1105 hb->float_allocation.width = width;
1106 hb->float_allocation.height = height;
1108 hb->deskoff_x = desk_x - root_x;
1109 hb->deskoff_y = desk_y - root_y;
1111 if (gdk_window_is_viewable (widget->window))
1113 gdk_window_get_origin (widget->window, &root_x, &root_y);
1114 gdk_drawable_get_size (widget->window, &width, &height);
1116 hb->attach_allocation.x = root_x;
1117 hb->attach_allocation.y = root_y;
1118 hb->attach_allocation.width = width;
1119 hb->attach_allocation.height = height;
1123 hb->attach_allocation.x = -1;
1124 hb->attach_allocation.y = -1;
1125 hb->attach_allocation.width = 0;
1126 hb->attach_allocation.height = 0;
1129 fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1131 if (gdk_pointer_grab (invisible->window,
1133 (GDK_BUTTON1_MOTION_MASK |
1134 GDK_POINTER_MOTION_HINT_MASK |
1135 GDK_BUTTON_RELEASE_MASK),
1140 hb->in_drag = FALSE;
1144 gtk_grab_add (invisible);
1145 g_signal_connect (invisible, "event",
1146 G_CALLBACK (gtk_handle_box_grab_event), hb);
1149 gdk_cursor_unref (fleur);
1150 event_handled = TRUE;
1152 else if (hb->child_detached) /* Double click */
1154 gtk_handle_box_reattach (hb);
1159 return event_handled;
1163 gtk_handle_box_motion (GtkWidget *widget,
1164 GdkEventMotion *event)
1166 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1169 gboolean is_snapped = FALSE;
1170 gint handle_position;
1171 GdkGeometry geometry;
1172 GdkScreen *screen, *pointer_screen;
1176 handle_position = effective_handle_position (hb);
1178 /* Calculate the attachment point on the float, if the float
1183 screen = gtk_widget_get_screen (widget);
1184 gdk_display_get_pointer (gdk_screen_get_display (screen),
1186 &new_x, &new_y, NULL);
1187 if (pointer_screen != screen)
1189 GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1191 new_x = private->orig_x;
1192 new_y = private->orig_y;
1195 new_x += hb->float_allocation.x;
1196 new_y += hb->float_allocation.y;
1198 snap_edge = hb->snap_edge;
1199 if (snap_edge == -1)
1200 snap_edge = (handle_position == GTK_POS_LEFT ||
1201 handle_position == GTK_POS_RIGHT) ?
1202 GTK_POS_TOP : GTK_POS_LEFT;
1204 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1208 snap_edge = GTK_POS_RIGHT;
1211 snap_edge = GTK_POS_LEFT;
1217 /* First, check if the snapped edge is aligned
1222 is_snapped = abs (hb->attach_allocation.y - new_y) < TOLERANCE;
1224 case GTK_POS_BOTTOM:
1225 is_snapped = abs (hb->attach_allocation.y + (gint)hb->attach_allocation.height -
1226 new_y - (gint)hb->float_allocation.height) < TOLERANCE;
1229 is_snapped = abs (hb->attach_allocation.x - new_x) < TOLERANCE;
1232 is_snapped = abs (hb->attach_allocation.x + (gint)hb->attach_allocation.width -
1233 new_x - (gint)hb->float_allocation.width) < TOLERANCE;
1237 /* Next, check if coordinates in the other direction are sufficiently
1242 gint float_pos1 = 0; /* Initialize to suppress warnings */
1243 gint float_pos2 = 0;
1244 gint attach_pos1 = 0;
1245 gint attach_pos2 = 0;
1250 case GTK_POS_BOTTOM:
1251 attach_pos1 = hb->attach_allocation.x;
1252 attach_pos2 = hb->attach_allocation.x + hb->attach_allocation.width;
1254 float_pos2 = new_x + hb->float_allocation.width;
1258 attach_pos1 = hb->attach_allocation.y;
1259 attach_pos2 = hb->attach_allocation.y + hb->attach_allocation.height;
1261 float_pos2 = new_y + hb->float_allocation.height;
1265 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1266 (attach_pos2 + TOLERANCE > float_pos2)) ||
1267 ((float_pos1 - TOLERANCE < attach_pos1) &&
1268 (float_pos2 + TOLERANCE > attach_pos2));
1273 if (hb->child_detached)
1275 hb->child_detached = FALSE;
1276 gdk_window_hide (hb->float_window);
1277 gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1278 hb->float_window_mapped = FALSE;
1280 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1282 GTK_BIN (hb)->child);
1284 gtk_widget_queue_resize (widget);
1291 gdk_drawable_get_size (hb->float_window, &width, &height);
1292 new_x += hb->deskoff_x;
1293 new_y += hb->deskoff_y;
1295 switch (handle_position)
1298 new_y += ((gint)hb->float_allocation.height - height) / 2;
1301 new_x += (gint)hb->float_allocation.width - width;
1302 new_y += ((gint)hb->float_allocation.height - height) / 2;
1305 new_x += ((gint)hb->float_allocation.width - width) / 2;
1307 case GTK_POS_BOTTOM:
1308 new_x += ((gint)hb->float_allocation.width - width) / 2;
1309 new_y += (gint)hb->float_allocation.height - height;
1313 if (hb->child_detached)
1315 gdk_window_move (hb->float_window, new_x, new_y);
1316 gdk_window_raise (hb->float_window);
1322 GtkRequisition child_requisition;
1324 hb->child_detached = TRUE;
1326 if (GTK_BIN (hb)->child)
1327 gtk_widget_get_child_requisition (GTK_BIN (hb)->child, &child_requisition);
1330 child_requisition.width = 0;
1331 child_requisition.height = 0;
1334 width = child_requisition.width + 2 * GTK_CONTAINER (hb)->border_width;
1335 height = child_requisition.height + 2 * GTK_CONTAINER (hb)->border_width;
1337 if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT)
1338 width += DRAG_HANDLE_SIZE;
1340 height += DRAG_HANDLE_SIZE;
1342 gdk_window_move_resize (hb->float_window, new_x, new_y, width, height);
1343 gdk_window_reparent (hb->bin_window, hb->float_window, 0, 0);
1344 gdk_window_set_geometry_hints (hb->float_window, &geometry, GDK_HINT_POS);
1345 gdk_window_show (hb->float_window);
1346 hb->float_window_mapped = TRUE;
1348 /* this extra move is necessary if we use decorations, or our
1349 * window manager insists on decorations.
1351 gdk_display_sync (gtk_widget_get_display (widget));
1352 gdk_window_move (hb->float_window, new_x, new_y);
1353 gdk_display_sync (gtk_widget_get_display (widget));
1356 handle_box_signals[SIGNAL_CHILD_DETACHED],
1358 GTK_BIN (hb)->child);
1359 gtk_handle_box_draw_ghost (hb);
1361 gtk_widget_queue_resize (widget);
1369 gtk_handle_box_add (GtkContainer *container,
1372 gtk_widget_set_parent_window (widget, GTK_HANDLE_BOX (container)->bin_window);
1373 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->add (container, widget);
1377 gtk_handle_box_remove (GtkContainer *container,
1380 GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->remove (container, widget);
1382 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1386 gtk_handle_box_delete_event (GtkWidget *widget,
1389 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1391 if (event->window == hb->float_window)
1393 gtk_handle_box_reattach (hb);
1402 gtk_handle_box_reattach (GtkHandleBox *hb)
1404 GtkWidget *widget = GTK_WIDGET (hb);
1406 if (hb->child_detached)
1408 hb->child_detached = FALSE;
1409 if (GTK_WIDGET_REALIZED (hb))
1411 gdk_window_hide (hb->float_window);
1412 gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1414 if (GTK_BIN (hb)->child)
1416 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1418 GTK_BIN (hb)->child);
1421 hb->float_window_mapped = FALSE;
1424 gtk_handle_box_end_drag (hb, GDK_CURRENT_TIME);
1426 gtk_widget_queue_resize (GTK_WIDGET (hb));
1430 gtk_handle_box_end_drag (GtkHandleBox *hb,
1433 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1435 hb->in_drag = FALSE;
1437 gtk_grab_remove (invisible);
1438 gdk_pointer_ungrab (time);
1439 g_signal_handlers_disconnect_by_func (invisible,
1440 G_CALLBACK (gtk_handle_box_grab_event),
1444 #define __GTK_HANDLE_BOX_C__
1445 #include "gtkaliasdef.c"