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/.
29 #include "gtkhandlebox.h"
31 #include "gtksignal.h"
32 #include "gtkwindow.h"
42 #define DRAG_HANDLE_SIZE 10
43 #define CHILDLESS_SIZE 25
44 #define GHOST_HEIGHT 3
48 SIGNAL_CHILD_ATTACHED,
49 SIGNAL_CHILD_DETACHED,
53 /* The algorithm for docking and redocking implemented here
54 * has a couple of nice properties:
56 * 1) During a single drag, docking always occurs at the
57 * the same cursor position. This means that the users
58 * motions are reversible, and that you won't
59 * undock/dock oscillations.
61 * 2) Docking generally occurs at user-visible features.
62 * The user, once they figure out to redock, will
63 * have useful information about doing it again in
66 * Please try to preserve these properties if you
67 * change the algorithm. (And the current algorithm
68 * is far from ideal). Briefly, the current algorithm
69 * for deciding whether the handlebox is docked or not:
71 * 1) The decision is done by comparing two rectangles - the
72 * allocation if the widget at the start of the drag,
73 * and the boundary of hb->bin_window at the start of
74 * of the drag offset by the distance that the cursor
77 * 2) These rectangles must have one edge, the "snap_edge"
78 * of the handlebox, aligned within TOLERANCE.
80 * 3) On the other dimension, the extents of one rectangle
81 * must be contained in the extents of the other,
82 * extended by tolerance. That is, either we can have:
84 * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE->
85 * <--------float_window-------------------->
89 * <-TOLERANCE-|------float_window--------------|-TOLERANCE->
90 * <--------bin_window-------------------->
93 static void gtk_handle_box_class_init (GtkHandleBoxClass *klass);
94 static void gtk_handle_box_init (GtkHandleBox *handle_box);
95 static void gtk_handle_box_set_property (GObject *object,
99 static void gtk_handle_box_get_property (GObject *object,
103 static void gtk_handle_box_destroy (GtkObject *object);
104 static void gtk_handle_box_map (GtkWidget *widget);
105 static void gtk_handle_box_unmap (GtkWidget *widget);
106 static void gtk_handle_box_realize (GtkWidget *widget);
107 static void gtk_handle_box_unrealize (GtkWidget *widget);
108 static void gtk_handle_box_style_set (GtkWidget *widget,
109 GtkStyle *previous_style);
110 static void gtk_handle_box_size_request (GtkWidget *widget,
111 GtkRequisition *requisition);
112 static void gtk_handle_box_size_allocate (GtkWidget *widget,
113 GtkAllocation *real_allocation);
114 static void gtk_handle_box_add (GtkContainer *container,
116 static void gtk_handle_box_remove (GtkContainer *container,
118 static void gtk_handle_box_draw_ghost (GtkHandleBox *hb);
119 static void gtk_handle_box_paint (GtkWidget *widget,
120 GdkEventExpose *event,
122 static gint gtk_handle_box_expose (GtkWidget *widget,
123 GdkEventExpose *event);
124 static gint gtk_handle_box_button_changed (GtkWidget *widget,
125 GdkEventButton *event);
126 static gint gtk_handle_box_motion (GtkWidget *widget,
127 GdkEventMotion *event);
128 static gint gtk_handle_box_delete_event (GtkWidget *widget,
130 static void gtk_handle_box_reattach (GtkHandleBox *hb);
133 static GtkBinClass *parent_class;
134 static guint handle_box_signals[SIGNAL_LAST] = { 0 };
138 gtk_handle_box_get_type (void)
140 static GtkType handle_box_type = 0;
142 if (!handle_box_type)
144 static const GtkTypeInfo handle_box_info =
147 sizeof (GtkHandleBox),
148 sizeof (GtkHandleBoxClass),
149 (GtkClassInitFunc) gtk_handle_box_class_init,
150 (GtkObjectInitFunc) gtk_handle_box_init,
151 /* reserved_1 */ NULL,
152 /* reserved_2 */ NULL,
153 (GtkClassInitFunc) NULL,
156 handle_box_type = gtk_type_unique (GTK_TYPE_BIN, &handle_box_info);
159 return handle_box_type;
163 gtk_handle_box_class_init (GtkHandleBoxClass *class)
165 GObjectClass *gobject_class;
166 GtkObjectClass *object_class;
167 GtkWidgetClass *widget_class;
168 GtkContainerClass *container_class;
170 gobject_class = (GObjectClass *) class;
171 object_class = (GtkObjectClass *) class;
172 widget_class = (GtkWidgetClass *) class;
173 container_class = (GtkContainerClass *) class;
175 parent_class = gtk_type_class (GTK_TYPE_BIN);
177 gobject_class->set_property = gtk_handle_box_set_property;
178 gobject_class->get_property = gtk_handle_box_get_property;
180 g_object_class_install_property (gobject_class,
182 g_param_spec_enum ("shadow",
184 _("Appearance of the shadow that surrounds the container."),
185 GTK_TYPE_SHADOW_TYPE,
186 GTK_SHADOW_ETCHED_OUT,
187 G_PARAM_READABLE | G_PARAM_WRITABLE));
189 g_object_class_install_property (gobject_class,
190 PROP_HANDLE_POSITION,
191 g_param_spec_enum ("handle_position",
192 _("Handle position"),
193 _("Position of the handle relative to the child widget."),
194 GTK_TYPE_POSITION_TYPE,
196 G_PARAM_READABLE | G_PARAM_WRITABLE));
198 g_object_class_install_property (gobject_class,
200 g_param_spec_enum ("snap_edge",
202 _("Side of the handlebox that's lined up with the docking point to dock the handlebox."),
203 GTK_TYPE_POSITION_TYPE,
205 G_PARAM_READABLE | G_PARAM_WRITABLE));
206 object_class->destroy = gtk_handle_box_destroy;
208 widget_class->map = gtk_handle_box_map;
209 widget_class->unmap = gtk_handle_box_unmap;
210 widget_class->realize = gtk_handle_box_realize;
211 widget_class->unrealize = gtk_handle_box_unrealize;
212 widget_class->style_set = gtk_handle_box_style_set;
213 widget_class->size_request = gtk_handle_box_size_request;
214 widget_class->size_allocate = gtk_handle_box_size_allocate;
215 widget_class->expose_event = gtk_handle_box_expose;
216 widget_class->button_press_event = gtk_handle_box_button_changed;
217 widget_class->button_release_event = gtk_handle_box_button_changed;
218 widget_class->motion_notify_event = gtk_handle_box_motion;
219 widget_class->delete_event = gtk_handle_box_delete_event;
221 container_class->add = gtk_handle_box_add;
222 container_class->remove = gtk_handle_box_remove;
224 class->child_attached = NULL;
225 class->child_detached = NULL;
227 handle_box_signals[SIGNAL_CHILD_ATTACHED] =
228 gtk_signal_new ("child_attached",
230 GTK_CLASS_TYPE (object_class),
231 GTK_SIGNAL_OFFSET (GtkHandleBoxClass, child_attached),
232 gtk_marshal_VOID__OBJECT,
235 handle_box_signals[SIGNAL_CHILD_DETACHED] =
236 gtk_signal_new ("child_detached",
238 GTK_CLASS_TYPE (object_class),
239 GTK_SIGNAL_OFFSET (GtkHandleBoxClass, child_detached),
240 gtk_marshal_VOID__OBJECT,
246 gtk_handle_box_init (GtkHandleBox *handle_box)
248 GTK_WIDGET_UNSET_FLAGS (handle_box, GTK_NO_WINDOW);
250 handle_box->bin_window = NULL;
251 handle_box->float_window = NULL;
252 handle_box->shadow_type = GTK_SHADOW_OUT;
253 handle_box->handle_position = GTK_POS_LEFT;
254 handle_box->float_window_mapped = FALSE;
255 handle_box->child_detached = FALSE;
256 handle_box->in_drag = FALSE;
257 handle_box->shrink_on_detach = TRUE;
258 handle_box->snap_edge = -1;
262 gtk_handle_box_set_property (GObject *object,
267 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
272 gtk_handle_box_set_shadow_type (handle_box, g_value_get_enum (value));
274 case PROP_HANDLE_POSITION:
275 gtk_handle_box_set_handle_position (handle_box, g_value_get_enum (value));
278 gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value));
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
287 gtk_handle_box_get_property (GObject *object,
292 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
297 g_value_set_enum (value, handle_box->shadow_type);
299 case PROP_HANDLE_POSITION:
300 g_value_set_enum (value, handle_box->handle_position);
303 g_value_set_enum (value, handle_box->snap_edge);
306 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
312 gtk_handle_box_new (void)
314 return GTK_WIDGET (gtk_type_new (gtk_handle_box_get_type ()));
318 gtk_handle_box_destroy (GtkObject *object)
322 g_return_if_fail (object != NULL);
323 g_return_if_fail (GTK_IS_HANDLE_BOX (object));
325 hb = GTK_HANDLE_BOX (object);
327 if (GTK_OBJECT_CLASS (parent_class)->destroy)
328 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
332 gtk_handle_box_map (GtkWidget *widget)
337 g_return_if_fail (widget != NULL);
338 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
340 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
342 bin = GTK_BIN (widget);
343 hb = GTK_HANDLE_BOX (widget);
346 GTK_WIDGET_VISIBLE (bin->child) &&
347 !GTK_WIDGET_MAPPED (bin->child))
348 gtk_widget_map (bin->child);
350 if (hb->child_detached && !hb->float_window_mapped)
352 gdk_window_show (hb->float_window);
353 hb->float_window_mapped = TRUE;
356 gdk_window_show (hb->bin_window);
357 gdk_window_show (widget->window);
361 gtk_handle_box_unmap (GtkWidget *widget)
365 g_return_if_fail (widget != NULL);
366 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
368 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
370 hb = GTK_HANDLE_BOX (widget);
372 gdk_window_hide (widget->window);
373 if (hb->float_window_mapped)
375 gdk_window_hide (hb->float_window);
376 hb->float_window_mapped = FALSE;
381 gtk_handle_box_realize (GtkWidget *widget)
383 GdkWindowAttr attributes;
384 gint attributes_mask;
387 g_return_if_fail (widget != NULL);
388 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
390 hb = GTK_HANDLE_BOX (widget);
392 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
394 attributes.x = widget->allocation.x;
395 attributes.y = widget->allocation.y;
396 attributes.width = widget->allocation.width;
397 attributes.height = widget->allocation.height;
398 attributes.window_type = GDK_WINDOW_CHILD;
399 attributes.wclass = GDK_INPUT_OUTPUT;
400 attributes.visual = gtk_widget_get_visual (widget);
401 attributes.colormap = gtk_widget_get_colormap (widget);
402 attributes.event_mask = (gtk_widget_get_events (widget)
403 | GDK_EXPOSURE_MASK);
404 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
405 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
406 gdk_window_set_user_data (widget->window, widget);
410 attributes.width = widget->allocation.width;
411 attributes.height = widget->allocation.height;
412 attributes.window_type = GDK_WINDOW_CHILD;
413 attributes.event_mask |= (gtk_widget_get_events (widget) |
415 GDK_BUTTON1_MOTION_MASK |
416 GDK_POINTER_MOTION_HINT_MASK |
417 GDK_BUTTON_PRESS_MASK |
418 GDK_BUTTON_RELEASE_MASK);
419 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
420 hb->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask);
421 gdk_window_set_user_data (hb->bin_window, widget);
422 if (GTK_BIN (hb)->child)
423 gtk_widget_set_parent_window (GTK_BIN (hb)->child, hb->bin_window);
427 attributes.width = widget->requisition.width;
428 attributes.height = widget->requisition.height;
429 attributes.window_type = GDK_WINDOW_TOPLEVEL;
430 attributes.wclass = GDK_INPUT_OUTPUT;
431 attributes.visual = gtk_widget_get_visual (widget);
432 attributes.colormap = gtk_widget_get_colormap (widget);
433 attributes.event_mask = (gtk_widget_get_events (widget) |
435 GDK_ENTER_NOTIFY_MASK |
436 GDK_LEAVE_NOTIFY_MASK |
437 GDK_FOCUS_CHANGE_MASK |
439 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
440 hb->float_window = gdk_window_new (NULL, &attributes, attributes_mask);
441 gdk_window_set_user_data (hb->float_window, widget);
442 gdk_window_set_decorations (hb->float_window, 0);
443 gdk_window_set_type_hint (hb->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
445 widget->style = gtk_style_attach (widget->style, widget->window);
446 gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (hb));
447 gtk_style_set_background (widget->style, hb->bin_window, GTK_WIDGET_STATE (hb));
448 gtk_style_set_background (widget->style, hb->float_window, GTK_WIDGET_STATE (hb));
449 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
453 gtk_handle_box_unrealize (GtkWidget *widget)
457 g_return_if_fail (widget != NULL);
458 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
460 hb = GTK_HANDLE_BOX (widget);
462 gdk_window_set_user_data (hb->bin_window, NULL);
463 gdk_window_destroy (hb->bin_window);
464 hb->bin_window = NULL;
465 gdk_window_set_user_data (hb->float_window, NULL);
466 gdk_window_destroy (hb->float_window);
467 hb->float_window = NULL;
469 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
470 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
474 gtk_handle_box_style_set (GtkWidget *widget,
475 GtkStyle *previous_style)
479 g_return_if_fail (widget != NULL);
480 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
482 hb = GTK_HANDLE_BOX (widget);
484 if (GTK_WIDGET_REALIZED (widget) &&
485 !GTK_WIDGET_NO_WINDOW (widget))
487 gtk_style_set_background (widget->style, widget->window,
489 gtk_style_set_background (widget->style, hb->bin_window, widget->state);
490 gtk_style_set_background (widget->style, hb->float_window, widget->state);
495 gtk_handle_box_size_request (GtkWidget *widget,
496 GtkRequisition *requisition)
500 GtkRequisition child_requisition;
502 g_return_if_fail (widget != NULL);
503 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
504 g_return_if_fail (requisition != NULL);
506 bin = GTK_BIN (widget);
507 hb = GTK_HANDLE_BOX (widget);
509 if (hb->handle_position == GTK_POS_LEFT ||
510 hb->handle_position == GTK_POS_RIGHT)
512 requisition->width = DRAG_HANDLE_SIZE;
513 requisition->height = 0;
517 requisition->width = 0;
518 requisition->height = DRAG_HANDLE_SIZE;
521 /* if our child is not visible, we still request its size, since we
522 * won't have any usefull hint for our size otherwise.
525 gtk_widget_size_request (bin->child, &child_requisition);
528 child_requisition.width = 0;
529 child_requisition.height = 0;
532 if (hb->child_detached)
534 /* FIXME: This doesn't work currently */
535 if (!hb->shrink_on_detach)
537 if (hb->handle_position == GTK_POS_LEFT ||
538 hb->handle_position == GTK_POS_RIGHT)
539 requisition->height += child_requisition.height;
541 requisition->width += child_requisition.width;
545 if (hb->handle_position == GTK_POS_LEFT ||
546 hb->handle_position == GTK_POS_RIGHT)
547 requisition->height += widget->style->ythickness;
549 requisition->width += widget->style->xthickness;
554 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
555 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
559 requisition->width += child_requisition.width;
560 requisition->height += child_requisition.height;
564 requisition->width += CHILDLESS_SIZE;
565 requisition->height += CHILDLESS_SIZE;
571 gtk_handle_box_size_allocate (GtkWidget *widget,
572 GtkAllocation *allocation)
576 GtkRequisition child_requisition;
578 g_return_if_fail (widget != NULL);
579 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
580 g_return_if_fail (allocation != NULL);
582 bin = GTK_BIN (widget);
583 hb = GTK_HANDLE_BOX (widget);
586 gtk_widget_get_child_requisition (bin->child, &child_requisition);
589 child_requisition.width = 0;
590 child_requisition.height = 0;
593 widget->allocation = *allocation;
595 if (GTK_WIDGET_REALIZED (hb))
596 gdk_window_move_resize (widget->window,
597 widget->allocation.x,
598 widget->allocation.y,
599 widget->allocation.width,
600 widget->allocation.height);
603 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
606 GtkAllocation child_allocation;
610 border_width = GTK_CONTAINER (widget)->border_width;
612 child_allocation.x = border_width;
613 child_allocation.y = border_width;
614 if (hb->handle_position == GTK_POS_LEFT)
615 child_allocation.x += DRAG_HANDLE_SIZE;
616 else if (hb->handle_position == GTK_POS_TOP)
617 child_allocation.y += DRAG_HANDLE_SIZE;
619 if (hb->child_detached)
624 child_allocation.width = child_requisition.width;
625 child_allocation.height = child_requisition.height;
627 float_width = child_allocation.width + 2 * border_width;
628 float_height = child_allocation.height + 2 * border_width;
630 if (hb->handle_position == GTK_POS_LEFT ||
631 hb->handle_position == GTK_POS_RIGHT)
632 float_width += DRAG_HANDLE_SIZE;
634 float_height += DRAG_HANDLE_SIZE;
636 if (GTK_WIDGET_REALIZED (hb))
638 gdk_window_resize (hb->float_window,
641 gdk_window_move_resize (hb->bin_window,
650 child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
651 child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
653 if (hb->handle_position == GTK_POS_LEFT ||
654 hb->handle_position == GTK_POS_RIGHT)
655 child_allocation.width -= DRAG_HANDLE_SIZE;
657 child_allocation.height -= DRAG_HANDLE_SIZE;
659 if (GTK_WIDGET_REALIZED (hb))
660 gdk_window_move_resize (hb->bin_window,
663 widget->allocation.width,
664 widget->allocation.height);
667 gtk_widget_size_allocate (bin->child, &child_allocation);
672 gtk_handle_box_draw_ghost (GtkHandleBox *hb)
680 widget = GTK_WIDGET (hb);
682 if (hb->handle_position == GTK_POS_LEFT ||
683 hb->handle_position == GTK_POS_RIGHT)
685 x = hb->handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
687 width = DRAG_HANDLE_SIZE;
688 height = widget->allocation.height;
693 y = hb->handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
694 width = widget->allocation.width;
695 height = DRAG_HANDLE_SIZE;
697 gtk_paint_shadow (widget->style,
699 GTK_WIDGET_STATE (widget),
700 GTK_SHADOW_ETCHED_IN,
701 NULL, widget, "handle",
706 if (hb->handle_position == GTK_POS_LEFT ||
707 hb->handle_position == GTK_POS_RIGHT)
708 gtk_paint_hline (widget->style,
710 GTK_WIDGET_STATE (widget),
711 NULL, widget, "handlebox",
712 hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
713 hb->handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
714 widget->allocation.height / 2);
716 gtk_paint_vline (widget->style,
718 GTK_WIDGET_STATE (widget),
719 NULL, widget, "handlebox",
720 hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
721 hb->handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
722 widget->allocation.width / 2);
726 draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
729 gtk_paint_handle(widget->style, window, GTK_STATE_NORMAL, shadow,
730 clip, widget, "handlebox",
731 rect->x, rect->y, rect->width, rect->height,
732 GTK_ORIENTATION_VERTICAL);
736 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
739 g_return_if_fail (handle_box != NULL);
740 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
742 if ((GtkShadowType) handle_box->shadow_type != type)
744 handle_box->shadow_type = type;
745 g_object_notify (G_OBJECT (handle_box), "shadow");
746 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
751 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
752 GtkPositionType position)
754 if ((GtkPositionType) handle_box->handle_position != position)
756 handle_box->handle_position = position;
757 g_object_notify (G_OBJECT (handle_box), "handle_position");
758 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
763 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
764 GtkPositionType edge)
766 g_return_if_fail (handle_box != NULL);
767 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
769 if (handle_box->snap_edge != edge)
771 handle_box->snap_edge = edge;
772 g_object_notify (G_OBJECT (handle_box), "snap_edge");
777 gtk_handle_box_paint (GtkWidget *widget,
778 GdkEventExpose *event,
788 bin = GTK_BIN (widget);
789 hb = GTK_HANDLE_BOX (widget);
791 gdk_window_get_size (hb->bin_window, &width, &height);
794 gtk_paint_box(widget->style,
796 GTK_WIDGET_STATE (widget),
798 area, widget, "handlebox_bin",
801 gtk_paint_box(widget->style,
803 GTK_WIDGET_STATE (widget),
805 &event->area, widget, "handlebox_bin",
808 /* We currently draw the handle _above_ the relief of the handlebox.
809 * it could also be drawn on the same level...
811 hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
812 hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
816 switch (hb->handle_position)
821 rect.width = DRAG_HANDLE_SIZE;
822 rect.height = height;
825 rect.x = width - DRAG_HANDLE_SIZE;
827 rect.width = DRAG_HANDLE_SIZE;
828 rect.height = height;
834 rect.height = DRAG_HANDLE_SIZE;
838 rect.y = height - DRAG_HANDLE_SIZE;
840 rect.height = DRAG_HANDLE_SIZE;
844 if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
845 draw_textured_frame (widget, hb->bin_window, &rect,
847 event ? &event->area : area);
849 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
851 GdkRectangle child_area;
853 if (!event) /* we were called from draw() */
855 if (gtk_widget_intersect (bin->child, area, &child_area))
856 gtk_widget_draw (bin->child, &child_area);
858 else /* we were called from expose() */
859 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
864 gtk_handle_box_expose (GtkWidget *widget,
865 GdkEventExpose *event)
869 g_return_val_if_fail (widget != NULL, FALSE);
870 g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
871 g_return_val_if_fail (event != NULL, FALSE);
873 if (GTK_WIDGET_DRAWABLE (widget))
875 hb = GTK_HANDLE_BOX (widget);
877 if (event->window == widget->window)
879 if (hb->child_detached)
880 gtk_handle_box_draw_ghost (hb);
883 gtk_handle_box_paint (widget, event, NULL);
890 gtk_handle_box_button_changed (GtkWidget *widget,
891 GdkEventButton *event)
894 gboolean event_handled;
897 g_return_val_if_fail (widget != NULL, FALSE);
898 g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
899 g_return_val_if_fail (event != NULL, FALSE);
901 hb = GTK_HANDLE_BOX (widget);
903 event_handled = FALSE;
904 if ((event->button == 1) &&
905 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
910 if (event->window != hb->bin_window)
913 child = GTK_BIN (hb)->child;
917 switch (hb->handle_position)
920 in_handle = event->x < DRAG_HANDLE_SIZE;
923 in_handle = event->y < DRAG_HANDLE_SIZE;
926 in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width;
929 in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height;
939 event_handled = TRUE;
944 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
950 gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y);
951 gdk_window_get_origin (hb->bin_window, &root_x, &root_y);
952 gdk_window_get_size (hb->bin_window, &width, &height);
954 hb->float_allocation.x = root_x - event->x_root;
955 hb->float_allocation.y = root_y - event->y_root;
956 hb->float_allocation.width = width;
957 hb->float_allocation.height = height;
959 hb->deskoff_x = desk_x - root_x;
960 hb->deskoff_y = desk_y - root_y;
962 gdk_window_get_origin (widget->window, &root_x, &root_y);
963 gdk_window_get_size (widget->window, &width, &height);
965 hb->attach_allocation.x = root_x;
966 hb->attach_allocation.y = root_y;
967 hb->attach_allocation.width = width;
968 hb->attach_allocation.height = height;
971 fleur = gdk_cursor_new (GDK_FLEUR);
972 if (gdk_pointer_grab (widget->window,
974 (GDK_BUTTON1_MOTION_MASK |
975 GDK_POINTER_MOTION_HINT_MASK |
976 GDK_BUTTON_RELEASE_MASK),
979 GDK_CURRENT_TIME) != 0)
984 gdk_cursor_destroy (fleur);
985 event_handled = TRUE;
987 else if (hb->child_detached) /* Double click */
989 gtk_handle_box_reattach (hb);
993 else if (event->type == GDK_BUTTON_RELEASE &&
996 if (event->window != widget->window)
999 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1000 hb->in_drag = FALSE;
1001 event_handled = TRUE;
1004 return event_handled;
1008 gtk_handle_box_motion (GtkWidget *widget,
1009 GdkEventMotion *event)
1014 gboolean is_snapped = FALSE;
1016 g_return_val_if_fail (widget != NULL, FALSE);
1017 g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
1018 g_return_val_if_fail (event != NULL, FALSE);
1020 hb = GTK_HANDLE_BOX (widget);
1024 if (!hb->in_drag || (event->window != widget->window))
1027 /* Calculate the attachment point on the float, if the float
1032 gdk_window_get_pointer (NULL, &new_x, &new_y, NULL);
1033 new_x += hb->float_allocation.x;
1034 new_y += hb->float_allocation.y;
1036 snap_edge = hb->snap_edge;
1037 if (snap_edge == -1)
1038 snap_edge = (hb->handle_position == GTK_POS_LEFT ||
1039 hb->handle_position == GTK_POS_RIGHT) ?
1040 GTK_POS_TOP : GTK_POS_LEFT;
1042 /* First, check if the snapped edge is aligned
1047 is_snapped = abs (hb->attach_allocation.y - new_y) < TOLERANCE;
1049 case GTK_POS_BOTTOM:
1050 is_snapped = abs (hb->attach_allocation.y + (gint)hb->attach_allocation.height -
1051 new_y - (gint)hb->float_allocation.height) < TOLERANCE;
1054 is_snapped = abs (hb->attach_allocation.x - new_x) < TOLERANCE;
1057 is_snapped = abs (hb->attach_allocation.x + (gint)hb->attach_allocation.width -
1058 new_x - (gint)hb->float_allocation.width) < TOLERANCE;
1062 /* Next, check if coordinates in the other direction are sufficiently
1067 gint float_pos1 = 0; /* Initialize to suppress warnings */
1068 gint float_pos2 = 0;
1069 gint attach_pos1 = 0;
1070 gint attach_pos2 = 0;
1075 case GTK_POS_BOTTOM:
1076 attach_pos1 = hb->attach_allocation.x;
1077 attach_pos2 = hb->attach_allocation.x + hb->attach_allocation.width;
1079 float_pos2 = new_x + hb->float_allocation.width;
1083 attach_pos1 = hb->attach_allocation.y;
1084 attach_pos2 = hb->attach_allocation.y + hb->attach_allocation.height;
1086 float_pos2 = new_y + hb->float_allocation.height;
1090 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1091 (attach_pos2 + TOLERANCE > float_pos2)) ||
1092 ((float_pos1 - TOLERANCE < attach_pos1) &&
1093 (float_pos2 + TOLERANCE > attach_pos2));
1098 if (hb->child_detached)
1100 hb->child_detached = FALSE;
1101 gdk_window_hide (hb->float_window);
1102 gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1103 hb->float_window_mapped = FALSE;
1104 gtk_signal_emit (GTK_OBJECT (hb),
1105 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1106 GTK_BIN (hb)->child);
1108 gtk_widget_queue_resize (widget);
1115 gdk_window_get_size (hb->float_window, &width, &height);
1116 new_x += hb->deskoff_x;
1117 new_y += hb->deskoff_y;
1119 switch (hb->handle_position)
1122 new_y += ((gint)hb->float_allocation.height - height) / 2;
1125 new_x += (gint)hb->float_allocation.width - width;
1126 new_y += ((gint)hb->float_allocation.height - height) / 2;
1129 new_x += ((gint)hb->float_allocation.width - width) / 2;
1131 case GTK_POS_BOTTOM:
1132 new_x += ((gint)hb->float_allocation.width - width) / 2;
1133 new_y += (gint)hb->float_allocation.height - height;
1137 if (hb->child_detached)
1139 gdk_window_move (hb->float_window, new_x, new_y);
1140 gdk_window_raise (hb->float_window);
1146 GtkRequisition child_requisition;
1148 hb->child_detached = TRUE;
1150 if (GTK_BIN (hb)->child)
1151 gtk_widget_get_child_requisition (GTK_BIN (hb)->child, &child_requisition);
1154 child_requisition.width = 0;
1155 child_requisition.height = 0;
1158 width = child_requisition.width + 2 * GTK_CONTAINER (hb)->border_width;
1159 height = child_requisition.height + 2 * GTK_CONTAINER (hb)->border_width;
1160 gdk_window_move_resize (hb->float_window, new_x, new_y, width, height);
1161 gdk_window_reparent (hb->bin_window, hb->float_window, 0, 0);
1162 gdk_window_set_hints (hb->float_window, new_x, new_y, 0, 0, 0, 0, GDK_HINT_POS);
1163 gdk_window_show (hb->float_window);
1164 hb->float_window_mapped = TRUE;
1166 /* this extra move is neccessary if we use decorations, or our
1167 * window manager insists on decorations.
1170 gdk_window_move (hb->float_window, new_x, new_y);
1173 gtk_signal_emit (GTK_OBJECT (hb),
1174 handle_box_signals[SIGNAL_CHILD_DETACHED],
1175 GTK_BIN (hb)->child);
1176 gtk_handle_box_draw_ghost (hb);
1178 gtk_widget_queue_resize (widget);
1186 gtk_handle_box_add (GtkContainer *container,
1189 g_return_if_fail (GTK_IS_HANDLE_BOX (container));
1190 g_return_if_fail (GTK_BIN (container)->child == NULL);
1191 g_return_if_fail (widget->parent == NULL);
1193 gtk_widget_set_parent_window (widget, GTK_HANDLE_BOX (container)->bin_window);
1194 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
1198 gtk_handle_box_remove (GtkContainer *container,
1201 g_return_if_fail (GTK_IS_HANDLE_BOX (container));
1202 g_return_if_fail (GTK_BIN (container)->child == widget);
1204 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1206 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1210 gtk_handle_box_delete_event (GtkWidget *widget,
1215 g_return_val_if_fail (widget != NULL, FALSE);
1216 g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
1217 g_return_val_if_fail (event != NULL, FALSE);
1219 hb = GTK_HANDLE_BOX (widget);
1221 if (event->window == hb->float_window)
1223 gtk_handle_box_reattach (hb);
1232 gtk_handle_box_reattach (GtkHandleBox *hb)
1234 if (hb->child_detached)
1236 hb->child_detached = FALSE;
1237 if (GTK_WIDGET_REALIZED (hb))
1239 gdk_window_hide (hb->float_window);
1240 gdk_window_reparent (hb->bin_window, GTK_WIDGET (hb)->window, 0, 0);
1242 if (GTK_BIN (hb)->child)
1243 gtk_signal_emit (GTK_OBJECT (hb),
1244 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1245 GTK_BIN (hb)->child);
1248 hb->float_window_mapped = FALSE;
1252 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1253 hb->in_drag = FALSE;
1256 gtk_widget_queue_resize (GTK_WIDGET (hb));