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"
37 typedef struct _GtkHandleBoxPrivate GtkHandleBoxPrivate;
39 struct _GtkHandleBoxPrivate
54 #define DRAG_HANDLE_SIZE 10
55 #define CHILDLESS_SIZE 25
56 #define GHOST_HEIGHT 3
60 SIGNAL_CHILD_ATTACHED,
61 SIGNAL_CHILD_DETACHED,
65 /* The algorithm for docking and redocking implemented here
66 * has a couple of nice properties:
68 * 1) During a single drag, docking always occurs at the
69 * the same cursor position. This means that the users
70 * motions are reversible, and that you won't
71 * undock/dock oscillations.
73 * 2) Docking generally occurs at user-visible features.
74 * The user, once they figure out to redock, will
75 * have useful information about doing it again in
78 * Please try to preserve these properties if you
79 * change the algorithm. (And the current algorithm
80 * is far from ideal). Briefly, the current algorithm
81 * for deciding whether the handlebox is docked or not:
83 * 1) The decision is done by comparing two rectangles - the
84 * allocation if the widget at the start of the drag,
85 * and the boundary of hb->bin_window at the start of
86 * of the drag offset by the distance that the cursor
89 * 2) These rectangles must have one edge, the "snap_edge"
90 * of the handlebox, aligned within TOLERANCE.
92 * 3) On the other dimension, the extents of one rectangle
93 * must be contained in the extents of the other,
94 * extended by tolerance. That is, either we can have:
96 * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE->
97 * <--------float_window-------------------->
101 * <-TOLERANCE-|------float_window--------------|-TOLERANCE->
102 * <--------bin_window-------------------->
105 static void gtk_handle_box_class_init (GtkHandleBoxClass *klass);
106 static void gtk_handle_box_init (GtkHandleBox *handle_box);
107 static void gtk_handle_box_set_property (GObject *object,
111 static void gtk_handle_box_get_property (GObject *object,
115 static void gtk_handle_box_destroy (GtkObject *object);
116 static void gtk_handle_box_map (GtkWidget *widget);
117 static void gtk_handle_box_unmap (GtkWidget *widget);
118 static void gtk_handle_box_realize (GtkWidget *widget);
119 static void gtk_handle_box_unrealize (GtkWidget *widget);
120 static void gtk_handle_box_style_set (GtkWidget *widget,
121 GtkStyle *previous_style);
122 static void gtk_handle_box_size_request (GtkWidget *widget,
123 GtkRequisition *requisition);
124 static void gtk_handle_box_size_allocate (GtkWidget *widget,
125 GtkAllocation *real_allocation);
126 static void gtk_handle_box_add (GtkContainer *container,
128 static void gtk_handle_box_remove (GtkContainer *container,
130 static void gtk_handle_box_draw_ghost (GtkHandleBox *hb);
131 static void gtk_handle_box_paint (GtkWidget *widget,
132 GdkEventExpose *event,
134 static gint gtk_handle_box_expose (GtkWidget *widget,
135 GdkEventExpose *event);
136 static gint gtk_handle_box_button_changed (GtkWidget *widget,
137 GdkEventButton *event);
138 static gint gtk_handle_box_motion (GtkWidget *widget,
139 GdkEventMotion *event);
140 static gint gtk_handle_box_delete_event (GtkWidget *widget,
142 static void gtk_handle_box_reattach (GtkHandleBox *hb);
143 static void gtk_handle_box_end_drag (GtkHandleBox *hb,
147 static GtkBinClass *parent_class;
148 static guint handle_box_signals[SIGNAL_LAST] = { 0 };
152 gtk_handle_box_get_type (void)
154 static GType handle_box_type = 0;
156 if (!handle_box_type)
158 static const GTypeInfo handle_box_info =
160 sizeof (GtkHandleBoxClass),
161 NULL, /* base_init */
162 NULL, /* base_finalize */
163 (GClassInitFunc) gtk_handle_box_class_init,
164 NULL, /* class_finalize */
165 NULL, /* class_data */
166 sizeof (GtkHandleBox),
168 (GInstanceInitFunc) gtk_handle_box_init,
171 handle_box_type = g_type_register_static (GTK_TYPE_BIN, "GtkHandleBox",
172 &handle_box_info, 0);
175 return handle_box_type;
179 gtk_handle_box_class_init (GtkHandleBoxClass *class)
181 GObjectClass *gobject_class;
182 GtkObjectClass *object_class;
183 GtkWidgetClass *widget_class;
184 GtkContainerClass *container_class;
186 gobject_class = (GObjectClass *) class;
187 object_class = (GtkObjectClass *) class;
188 widget_class = (GtkWidgetClass *) class;
189 container_class = (GtkContainerClass *) class;
191 parent_class = g_type_class_peek_parent (class);
193 gobject_class->set_property = gtk_handle_box_set_property;
194 gobject_class->get_property = gtk_handle_box_get_property;
196 g_object_class_install_property (gobject_class,
198 g_param_spec_enum ("shadow", NULL,
199 P_("Deprecated property, use shadow_type instead"),
200 GTK_TYPE_SHADOW_TYPE,
201 GTK_SHADOW_ETCHED_OUT,
202 G_PARAM_READABLE | G_PARAM_WRITABLE));
203 g_object_class_install_property (gobject_class,
205 g_param_spec_enum ("shadow_type",
207 P_("Appearance of the shadow that surrounds the container"),
208 GTK_TYPE_SHADOW_TYPE,
209 GTK_SHADOW_ETCHED_OUT,
210 G_PARAM_READABLE | G_PARAM_WRITABLE));
212 g_object_class_install_property (gobject_class,
213 PROP_HANDLE_POSITION,
214 g_param_spec_enum ("handle_position",
215 P_("Handle position"),
216 P_("Position of the handle relative to the child widget"),
217 GTK_TYPE_POSITION_TYPE,
219 G_PARAM_READABLE | G_PARAM_WRITABLE));
221 g_object_class_install_property (gobject_class,
223 g_param_spec_enum ("snap_edge",
225 P_("Side of the handlebox that's lined up with the docking point to dock the handlebox"),
226 GTK_TYPE_POSITION_TYPE,
228 G_PARAM_READABLE | G_PARAM_WRITABLE));
230 g_object_class_install_property (gobject_class,
232 g_param_spec_boolean ("snap_edge_set",
234 P_("Whether to use the value from the snap_edge property or a value derived from handle_position"),
236 G_PARAM_READABLE | G_PARAM_WRITABLE));
238 object_class->destroy = gtk_handle_box_destroy;
240 widget_class->map = gtk_handle_box_map;
241 widget_class->unmap = gtk_handle_box_unmap;
242 widget_class->realize = gtk_handle_box_realize;
243 widget_class->unrealize = gtk_handle_box_unrealize;
244 widget_class->style_set = gtk_handle_box_style_set;
245 widget_class->size_request = gtk_handle_box_size_request;
246 widget_class->size_allocate = gtk_handle_box_size_allocate;
247 widget_class->expose_event = gtk_handle_box_expose;
248 widget_class->button_press_event = gtk_handle_box_button_changed;
249 widget_class->delete_event = gtk_handle_box_delete_event;
251 container_class->add = gtk_handle_box_add;
252 container_class->remove = gtk_handle_box_remove;
254 class->child_attached = NULL;
255 class->child_detached = NULL;
257 handle_box_signals[SIGNAL_CHILD_ATTACHED] =
258 g_signal_new ("child_attached",
259 G_OBJECT_CLASS_TYPE (gobject_class),
261 G_STRUCT_OFFSET (GtkHandleBoxClass, child_attached),
263 _gtk_marshal_VOID__OBJECT,
266 handle_box_signals[SIGNAL_CHILD_DETACHED] =
267 g_signal_new ("child_detached",
268 G_OBJECT_CLASS_TYPE (gobject_class),
270 G_STRUCT_OFFSET (GtkHandleBoxClass, child_detached),
272 _gtk_marshal_VOID__OBJECT,
277 static GtkHandleBoxPrivate *
278 gtk_handle_box_get_private (GtkHandleBox *hb)
280 GtkHandleBoxPrivate *private;
281 static GQuark private_quark = 0;
284 private_quark = g_quark_from_static_string ("gtk-handle-box-private");
286 private = g_object_get_qdata (G_OBJECT (hb), private_quark);
290 private = g_new0 (GtkHandleBoxPrivate, 1);
291 g_object_set_qdata_full (G_OBJECT (hb), private_quark,
299 gtk_handle_box_init (GtkHandleBox *handle_box)
301 GTK_WIDGET_UNSET_FLAGS (handle_box, GTK_NO_WINDOW);
303 handle_box->bin_window = NULL;
304 handle_box->float_window = NULL;
305 handle_box->shadow_type = GTK_SHADOW_OUT;
306 handle_box->handle_position = GTK_POS_LEFT;
307 handle_box->float_window_mapped = FALSE;
308 handle_box->child_detached = FALSE;
309 handle_box->in_drag = FALSE;
310 handle_box->shrink_on_detach = TRUE;
311 handle_box->snap_edge = -1;
315 gtk_handle_box_set_property (GObject *object,
320 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
325 case PROP_SHADOW_TYPE:
326 gtk_handle_box_set_shadow_type (handle_box, g_value_get_enum (value));
328 case PROP_HANDLE_POSITION:
329 gtk_handle_box_set_handle_position (handle_box, g_value_get_enum (value));
332 gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value));
334 case PROP_SNAP_EDGE_SET:
335 if (!g_value_get_boolean (value))
336 gtk_handle_box_set_snap_edge (handle_box, (GtkPositionType)-1);
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
345 gtk_handle_box_get_property (GObject *object,
350 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
355 case PROP_SHADOW_TYPE:
356 g_value_set_enum (value, handle_box->shadow_type);
358 case PROP_HANDLE_POSITION:
359 g_value_set_enum (value, handle_box->handle_position);
362 g_value_set_enum (value,
363 (handle_box->snap_edge == -1 ?
364 GTK_POS_TOP : handle_box->snap_edge));
366 case PROP_SNAP_EDGE_SET:
367 g_value_set_boolean (value, handle_box->snap_edge != -1);
370 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
376 gtk_handle_box_new (void)
378 return g_object_new (GTK_TYPE_HANDLE_BOX, NULL);
382 gtk_handle_box_destroy (GtkObject *object)
384 if (GTK_OBJECT_CLASS (parent_class)->destroy)
385 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
389 gtk_handle_box_map (GtkWidget *widget)
394 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
396 bin = GTK_BIN (widget);
397 hb = GTK_HANDLE_BOX (widget);
400 GTK_WIDGET_VISIBLE (bin->child) &&
401 !GTK_WIDGET_MAPPED (bin->child))
402 gtk_widget_map (bin->child);
404 if (hb->child_detached && !hb->float_window_mapped)
406 gdk_window_show (hb->float_window);
407 hb->float_window_mapped = TRUE;
410 gdk_window_show (hb->bin_window);
411 gdk_window_show (widget->window);
415 gtk_handle_box_unmap (GtkWidget *widget)
419 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
421 hb = GTK_HANDLE_BOX (widget);
423 gdk_window_hide (widget->window);
424 if (hb->float_window_mapped)
426 gdk_window_hide (hb->float_window);
427 hb->float_window_mapped = FALSE;
432 gtk_handle_box_realize (GtkWidget *widget)
434 GdkWindowAttr attributes;
435 gint attributes_mask;
438 hb = GTK_HANDLE_BOX (widget);
440 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
442 attributes.x = widget->allocation.x;
443 attributes.y = widget->allocation.y;
444 attributes.width = widget->allocation.width;
445 attributes.height = widget->allocation.height;
446 attributes.window_type = GDK_WINDOW_CHILD;
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)
451 | GDK_EXPOSURE_MASK);
452 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
453 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
454 gdk_window_set_user_data (widget->window, widget);
458 attributes.width = widget->allocation.width;
459 attributes.height = widget->allocation.height;
460 attributes.window_type = GDK_WINDOW_CHILD;
461 attributes.event_mask = (gtk_widget_get_events (widget) |
463 GDK_BUTTON1_MOTION_MASK |
464 GDK_POINTER_MOTION_HINT_MASK |
465 GDK_BUTTON_PRESS_MASK |
466 GDK_BUTTON_RELEASE_MASK);
467 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
468 hb->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask);
469 gdk_window_set_user_data (hb->bin_window, widget);
470 if (GTK_BIN (hb)->child)
471 gtk_widget_set_parent_window (GTK_BIN (hb)->child, hb->bin_window);
475 attributes.width = widget->requisition.width;
476 attributes.height = widget->requisition.height;
477 attributes.window_type = GDK_WINDOW_TOPLEVEL;
478 attributes.wclass = GDK_INPUT_OUTPUT;
479 attributes.visual = gtk_widget_get_visual (widget);
480 attributes.colormap = gtk_widget_get_colormap (widget);
481 attributes.event_mask = (gtk_widget_get_events (widget) |
483 GDK_ENTER_NOTIFY_MASK |
484 GDK_LEAVE_NOTIFY_MASK |
485 GDK_FOCUS_CHANGE_MASK |
487 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
488 hb->float_window = gdk_window_new (gtk_widget_get_root_window (widget),
489 &attributes, attributes_mask);
490 gdk_window_set_user_data (hb->float_window, widget);
491 gdk_window_set_decorations (hb->float_window, 0);
492 gdk_window_set_type_hint (hb->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
494 widget->style = gtk_style_attach (widget->style, widget->window);
495 gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (hb));
496 gtk_style_set_background (widget->style, hb->bin_window, GTK_WIDGET_STATE (hb));
497 gtk_style_set_background (widget->style, hb->float_window, GTK_WIDGET_STATE (hb));
498 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
502 gtk_handle_box_unrealize (GtkWidget *widget)
504 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
506 gdk_window_set_user_data (hb->bin_window, NULL);
507 gdk_window_destroy (hb->bin_window);
508 hb->bin_window = NULL;
509 gdk_window_set_user_data (hb->float_window, NULL);
510 gdk_window_destroy (hb->float_window);
511 hb->float_window = NULL;
513 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
514 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
518 gtk_handle_box_style_set (GtkWidget *widget,
519 GtkStyle *previous_style)
521 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
523 if (GTK_WIDGET_REALIZED (widget) &&
524 !GTK_WIDGET_NO_WINDOW (widget))
526 gtk_style_set_background (widget->style, widget->window,
528 gtk_style_set_background (widget->style, hb->bin_window, widget->state);
529 gtk_style_set_background (widget->style, hb->float_window, widget->state);
534 effective_handle_position (GtkHandleBox *hb)
538 if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
539 handle_position = hb->handle_position;
542 switch (hb->handle_position)
545 handle_position = GTK_POS_RIGHT;
548 handle_position = GTK_POS_LEFT;
551 handle_position = hb->handle_position;
556 return handle_position;
560 gtk_handle_box_size_request (GtkWidget *widget,
561 GtkRequisition *requisition)
565 GtkRequisition child_requisition;
566 gint handle_position;
568 bin = GTK_BIN (widget);
569 hb = GTK_HANDLE_BOX (widget);
571 handle_position = effective_handle_position (hb);
573 if (handle_position == GTK_POS_LEFT ||
574 handle_position == GTK_POS_RIGHT)
576 requisition->width = DRAG_HANDLE_SIZE;
577 requisition->height = 0;
581 requisition->width = 0;
582 requisition->height = DRAG_HANDLE_SIZE;
585 /* if our child is not visible, we still request its size, since we
586 * won't have any usefull hint for our size otherwise.
589 gtk_widget_size_request (bin->child, &child_requisition);
592 child_requisition.width = 0;
593 child_requisition.height = 0;
596 if (hb->child_detached)
598 /* FIXME: This doesn't work currently */
599 if (!hb->shrink_on_detach)
601 if (handle_position == GTK_POS_LEFT ||
602 handle_position == GTK_POS_RIGHT)
603 requisition->height += child_requisition.height;
605 requisition->width += child_requisition.width;
609 if (handle_position == GTK_POS_LEFT ||
610 handle_position == GTK_POS_RIGHT)
611 requisition->height += widget->style->ythickness;
613 requisition->width += widget->style->xthickness;
618 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
619 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
623 requisition->width += child_requisition.width;
624 requisition->height += child_requisition.height;
628 requisition->width += CHILDLESS_SIZE;
629 requisition->height += CHILDLESS_SIZE;
635 gtk_handle_box_size_allocate (GtkWidget *widget,
636 GtkAllocation *allocation)
640 GtkRequisition child_requisition;
641 gint handle_position;
643 bin = GTK_BIN (widget);
644 hb = GTK_HANDLE_BOX (widget);
646 handle_position = effective_handle_position (hb);
649 gtk_widget_get_child_requisition (bin->child, &child_requisition);
652 child_requisition.width = 0;
653 child_requisition.height = 0;
656 widget->allocation = *allocation;
658 if (GTK_WIDGET_REALIZED (hb))
659 gdk_window_move_resize (widget->window,
660 widget->allocation.x,
661 widget->allocation.y,
662 widget->allocation.width,
663 widget->allocation.height);
666 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
669 GtkAllocation child_allocation;
673 border_width = GTK_CONTAINER (widget)->border_width;
675 child_allocation.x = border_width;
676 child_allocation.y = border_width;
677 if (handle_position == GTK_POS_LEFT)
678 child_allocation.x += DRAG_HANDLE_SIZE;
679 else if (handle_position == GTK_POS_TOP)
680 child_allocation.y += DRAG_HANDLE_SIZE;
682 if (hb->child_detached)
687 child_allocation.width = child_requisition.width;
688 child_allocation.height = child_requisition.height;
690 float_width = child_allocation.width + 2 * border_width;
691 float_height = child_allocation.height + 2 * border_width;
693 if (handle_position == GTK_POS_LEFT ||
694 handle_position == GTK_POS_RIGHT)
695 float_width += DRAG_HANDLE_SIZE;
697 float_height += DRAG_HANDLE_SIZE;
699 if (GTK_WIDGET_REALIZED (hb))
701 gdk_window_resize (hb->float_window,
704 gdk_window_move_resize (hb->bin_window,
713 child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
714 child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
716 if (handle_position == GTK_POS_LEFT ||
717 handle_position == GTK_POS_RIGHT)
718 child_allocation.width -= DRAG_HANDLE_SIZE;
720 child_allocation.height -= DRAG_HANDLE_SIZE;
722 if (GTK_WIDGET_REALIZED (hb))
723 gdk_window_move_resize (hb->bin_window,
726 widget->allocation.width,
727 widget->allocation.height);
730 gtk_widget_size_allocate (bin->child, &child_allocation);
735 gtk_handle_box_draw_ghost (GtkHandleBox *hb)
742 gint handle_position;
744 widget = GTK_WIDGET (hb);
746 handle_position = effective_handle_position (hb);
747 if (handle_position == GTK_POS_LEFT ||
748 handle_position == GTK_POS_RIGHT)
750 x = handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
752 width = DRAG_HANDLE_SIZE;
753 height = widget->allocation.height;
758 y = handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
759 width = widget->allocation.width;
760 height = DRAG_HANDLE_SIZE;
762 gtk_paint_shadow (widget->style,
764 GTK_WIDGET_STATE (widget),
765 GTK_SHADOW_ETCHED_IN,
766 NULL, widget, "handle",
771 if (handle_position == GTK_POS_LEFT ||
772 handle_position == GTK_POS_RIGHT)
773 gtk_paint_hline (widget->style,
775 GTK_WIDGET_STATE (widget),
776 NULL, widget, "handlebox",
777 handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
778 handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
779 widget->allocation.height / 2);
781 gtk_paint_vline (widget->style,
783 GTK_WIDGET_STATE (widget),
784 NULL, widget, "handlebox",
785 handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
786 handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
787 widget->allocation.width / 2);
791 draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
794 gtk_paint_handle (widget->style, window, GTK_STATE_NORMAL, shadow,
795 clip, widget, "handlebox",
796 rect->x, rect->y, rect->width, rect->height,
797 GTK_ORIENTATION_VERTICAL);
801 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
804 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
806 if ((GtkShadowType) handle_box->shadow_type != type)
808 handle_box->shadow_type = type;
809 g_object_notify (G_OBJECT (handle_box), "shadow_type");
810 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
815 * gtk_handle_box_get_shadow_type:
816 * @handle_box: a #GtkHandleBox
818 * Gets the type of shadow drawn around the handle box. See
819 * gtk_handle_box_set_shadow_type().
821 * Return value: the type of shadow currently drawn around the handle box.
824 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
826 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
828 return handle_box->shadow_type;
832 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
833 GtkPositionType position)
835 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
837 if ((GtkPositionType) handle_box->handle_position != position)
839 handle_box->handle_position = position;
840 g_object_notify (G_OBJECT (handle_box), "handle_position");
841 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
846 * gtk_handle_box_get_handle_position:
847 * @handle_box: a #GtkHandleBox
849 * Gets the handle position of the handle box. See
850 * gtk_handle_box_set_handle_position().
852 * Return value: the current handle position.
855 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
857 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
859 return handle_box->handle_position;
863 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
864 GtkPositionType edge)
866 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
868 if (handle_box->snap_edge != edge)
870 handle_box->snap_edge = edge;
872 g_object_freeze_notify (G_OBJECT (handle_box));
873 g_object_notify (G_OBJECT (handle_box), "snap_edge");
874 g_object_notify (G_OBJECT (handle_box), "snap_edge_set");
875 g_object_thaw_notify (G_OBJECT (handle_box));
880 * gtk_handle_box_get_snap_edge:
881 * @handle_box: a #GtkHandleBox
883 * Gets the edge used for determining reattachment of the handle box. See
884 * gtk_handle_box_set_snap_edge().
886 * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
887 * is determined (as per default) from the handle position.
890 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
892 g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
894 return handle_box->snap_edge;
898 gtk_handle_box_paint (GtkWidget *widget,
900 GdkEventExpose *event,
909 gint handle_position;
911 bin = GTK_BIN (widget);
912 hb = GTK_HANDLE_BOX (widget);
914 handle_position = effective_handle_position (hb);
916 gdk_drawable_get_size (hb->bin_window, &width, &height);
919 gtk_paint_box (widget->style,
921 GTK_WIDGET_STATE (widget),
923 area, widget, "handlebox_bin",
926 gtk_paint_box (widget->style,
928 GTK_WIDGET_STATE (widget),
930 &event->area, widget, "handlebox_bin",
933 /* We currently draw the handle _above_ the relief of the handlebox.
934 * it could also be drawn on the same level...
936 hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
937 hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
941 switch (handle_position)
946 rect.width = DRAG_HANDLE_SIZE;
947 rect.height = height;
950 rect.x = width - DRAG_HANDLE_SIZE;
952 rect.width = DRAG_HANDLE_SIZE;
953 rect.height = height;
959 rect.height = DRAG_HANDLE_SIZE;
963 rect.y = height - DRAG_HANDLE_SIZE;
965 rect.height = DRAG_HANDLE_SIZE;
969 if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
970 draw_textured_frame (widget, hb->bin_window, &rect,
972 event ? &event->area : area);
974 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
975 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
979 gtk_handle_box_expose (GtkWidget *widget,
980 GdkEventExpose *event)
984 if (GTK_WIDGET_DRAWABLE (widget))
986 hb = GTK_HANDLE_BOX (widget);
988 if (event->window == widget->window)
990 if (hb->child_detached)
991 gtk_handle_box_draw_ghost (hb);
994 gtk_handle_box_paint (widget, event, NULL);
1001 gtk_handle_box_get_invisible (void)
1003 static GtkWidget *handle_box_invisible = NULL;
1005 if (!handle_box_invisible)
1007 handle_box_invisible = gtk_invisible_new ();
1008 gtk_widget_show (handle_box_invisible);
1011 return handle_box_invisible;
1015 gtk_handle_box_grab_event (GtkWidget *widget,
1019 switch (event->type)
1021 case GDK_BUTTON_RELEASE:
1022 if (hb->in_drag) /* sanity check */
1024 gtk_handle_box_end_drag (hb, event->button.time);
1029 case GDK_MOTION_NOTIFY:
1030 return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event);
1041 gtk_handle_box_button_changed (GtkWidget *widget,
1042 GdkEventButton *event)
1045 gboolean event_handled;
1047 gint handle_position;
1049 hb = GTK_HANDLE_BOX (widget);
1051 handle_position = effective_handle_position (hb);
1053 event_handled = FALSE;
1054 if ((event->button == 1) &&
1055 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1060 if (event->window != hb->bin_window)
1063 child = GTK_BIN (hb)->child;
1067 switch (handle_position)
1070 in_handle = event->x < DRAG_HANDLE_SIZE;
1073 in_handle = event->y < DRAG_HANDLE_SIZE;
1076 in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width;
1078 case GTK_POS_BOTTOM:
1079 in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height;
1089 event_handled = TRUE;
1094 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
1096 GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1097 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1098 gint desk_x, desk_y;
1099 gint root_x, root_y;
1102 gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y);
1103 gdk_window_get_origin (hb->bin_window, &root_x, &root_y);
1104 gdk_drawable_get_size (hb->bin_window, &width, &height);
1106 private->orig_x = event->x_root;
1107 private->orig_y = event->y_root;
1109 hb->float_allocation.x = root_x - event->x_root;
1110 hb->float_allocation.y = root_y - event->y_root;
1111 hb->float_allocation.width = width;
1112 hb->float_allocation.height = height;
1114 hb->deskoff_x = desk_x - root_x;
1115 hb->deskoff_y = desk_y - root_y;
1117 if (gdk_window_is_viewable (widget->window))
1119 gdk_window_get_origin (widget->window, &root_x, &root_y);
1120 gdk_drawable_get_size (widget->window, &width, &height);
1122 hb->attach_allocation.x = root_x;
1123 hb->attach_allocation.y = root_y;
1124 hb->attach_allocation.width = width;
1125 hb->attach_allocation.height = height;
1129 hb->attach_allocation.x = -1;
1130 hb->attach_allocation.y = -1;
1131 hb->attach_allocation.width = 0;
1132 hb->attach_allocation.height = 0;
1135 fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1137 if (gdk_pointer_grab (invisible->window,
1139 (GDK_BUTTON1_MOTION_MASK |
1140 GDK_POINTER_MOTION_HINT_MASK |
1141 GDK_BUTTON_RELEASE_MASK),
1146 hb->in_drag = FALSE;
1150 gtk_grab_add (invisible);
1151 g_signal_connect (invisible, "event",
1152 G_CALLBACK (gtk_handle_box_grab_event), hb);
1155 gdk_cursor_unref (fleur);
1156 event_handled = TRUE;
1158 else if (hb->child_detached) /* Double click */
1160 gtk_handle_box_reattach (hb);
1165 return event_handled;
1169 gtk_handle_box_motion (GtkWidget *widget,
1170 GdkEventMotion *event)
1172 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1175 gboolean is_snapped = FALSE;
1176 gint handle_position;
1177 GdkGeometry geometry;
1178 GdkScreen *screen, *pointer_screen;
1182 handle_position = effective_handle_position (hb);
1184 /* Calculate the attachment point on the float, if the float
1189 screen = gtk_widget_get_screen (widget);
1190 gdk_display_get_pointer (gdk_screen_get_display (screen),
1192 &new_x, &new_y, NULL);
1193 if (pointer_screen != screen)
1195 GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1197 new_x = private->orig_x;
1198 new_y = private->orig_y;
1201 new_x += hb->float_allocation.x;
1202 new_y += hb->float_allocation.y;
1204 snap_edge = hb->snap_edge;
1205 if (snap_edge == -1)
1206 snap_edge = (handle_position == GTK_POS_LEFT ||
1207 handle_position == GTK_POS_RIGHT) ?
1208 GTK_POS_TOP : GTK_POS_LEFT;
1210 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1214 snap_edge = GTK_POS_RIGHT;
1217 snap_edge = GTK_POS_LEFT;
1223 /* First, check if the snapped edge is aligned
1228 is_snapped = abs (hb->attach_allocation.y - new_y) < TOLERANCE;
1230 case GTK_POS_BOTTOM:
1231 is_snapped = abs (hb->attach_allocation.y + (gint)hb->attach_allocation.height -
1232 new_y - (gint)hb->float_allocation.height) < TOLERANCE;
1235 is_snapped = abs (hb->attach_allocation.x - new_x) < TOLERANCE;
1238 is_snapped = abs (hb->attach_allocation.x + (gint)hb->attach_allocation.width -
1239 new_x - (gint)hb->float_allocation.width) < TOLERANCE;
1243 /* Next, check if coordinates in the other direction are sufficiently
1248 gint float_pos1 = 0; /* Initialize to suppress warnings */
1249 gint float_pos2 = 0;
1250 gint attach_pos1 = 0;
1251 gint attach_pos2 = 0;
1256 case GTK_POS_BOTTOM:
1257 attach_pos1 = hb->attach_allocation.x;
1258 attach_pos2 = hb->attach_allocation.x + hb->attach_allocation.width;
1260 float_pos2 = new_x + hb->float_allocation.width;
1264 attach_pos1 = hb->attach_allocation.y;
1265 attach_pos2 = hb->attach_allocation.y + hb->attach_allocation.height;
1267 float_pos2 = new_y + hb->float_allocation.height;
1271 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1272 (attach_pos2 + TOLERANCE > float_pos2)) ||
1273 ((float_pos1 - TOLERANCE < attach_pos1) &&
1274 (float_pos2 + TOLERANCE > attach_pos2));
1279 if (hb->child_detached)
1281 hb->child_detached = FALSE;
1282 gdk_window_hide (hb->float_window);
1283 gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1284 hb->float_window_mapped = FALSE;
1286 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1288 GTK_BIN (hb)->child);
1290 gtk_widget_queue_resize (widget);
1297 gdk_drawable_get_size (hb->float_window, &width, &height);
1298 new_x += hb->deskoff_x;
1299 new_y += hb->deskoff_y;
1301 switch (handle_position)
1304 new_y += ((gint)hb->float_allocation.height - height) / 2;
1307 new_x += (gint)hb->float_allocation.width - width;
1308 new_y += ((gint)hb->float_allocation.height - height) / 2;
1311 new_x += ((gint)hb->float_allocation.width - width) / 2;
1313 case GTK_POS_BOTTOM:
1314 new_x += ((gint)hb->float_allocation.width - width) / 2;
1315 new_y += (gint)hb->float_allocation.height - height;
1319 if (hb->child_detached)
1321 gdk_window_move (hb->float_window, new_x, new_y);
1322 gdk_window_raise (hb->float_window);
1328 GtkRequisition child_requisition;
1330 hb->child_detached = TRUE;
1332 if (GTK_BIN (hb)->child)
1333 gtk_widget_get_child_requisition (GTK_BIN (hb)->child, &child_requisition);
1336 child_requisition.width = 0;
1337 child_requisition.height = 0;
1340 width = child_requisition.width + 2 * GTK_CONTAINER (hb)->border_width;
1341 height = child_requisition.height + 2 * GTK_CONTAINER (hb)->border_width;
1343 if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT)
1344 width += DRAG_HANDLE_SIZE;
1346 height += DRAG_HANDLE_SIZE;
1348 gdk_window_move_resize (hb->float_window, new_x, new_y, width, height);
1349 gdk_window_reparent (hb->bin_window, hb->float_window, 0, 0);
1350 gdk_window_set_geometry_hints (hb->float_window, &geometry, GDK_HINT_POS);
1351 gdk_window_show (hb->float_window);
1352 hb->float_window_mapped = TRUE;
1354 /* this extra move is necessary if we use decorations, or our
1355 * window manager insists on decorations.
1357 gdk_display_sync (gtk_widget_get_display (widget));
1358 gdk_window_move (hb->float_window, new_x, new_y);
1359 gdk_display_sync (gtk_widget_get_display (widget));
1362 handle_box_signals[SIGNAL_CHILD_DETACHED],
1364 GTK_BIN (hb)->child);
1365 gtk_handle_box_draw_ghost (hb);
1367 gtk_widget_queue_resize (widget);
1375 gtk_handle_box_add (GtkContainer *container,
1378 gtk_widget_set_parent_window (widget, GTK_HANDLE_BOX (container)->bin_window);
1379 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
1383 gtk_handle_box_remove (GtkContainer *container,
1386 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1388 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1392 gtk_handle_box_delete_event (GtkWidget *widget,
1395 GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1397 if (event->window == hb->float_window)
1399 gtk_handle_box_reattach (hb);
1408 gtk_handle_box_reattach (GtkHandleBox *hb)
1410 GtkWidget *widget = GTK_WIDGET (hb);
1412 if (hb->child_detached)
1414 hb->child_detached = FALSE;
1415 if (GTK_WIDGET_REALIZED (hb))
1417 gdk_window_hide (hb->float_window);
1418 gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1420 if (GTK_BIN (hb)->child)
1422 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1424 GTK_BIN (hb)->child);
1427 hb->float_window_mapped = FALSE;
1430 gtk_handle_box_end_drag (hb, GDK_CURRENT_TIME);
1432 gtk_widget_queue_resize (GTK_WIDGET (hb));
1436 gtk_handle_box_end_drag (GtkHandleBox *hb,
1439 GtkWidget *invisible = gtk_handle_box_get_invisible ();
1441 hb->in_drag = FALSE;
1443 gtk_grab_remove (invisible);
1444 gdk_pointer_ungrab (time);
1445 g_signal_handlers_disconnect_by_func (invisible,
1446 G_CALLBACK (gtk_handle_box_grab_event),