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 Library 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 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library 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-1999. 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"
32 #include "gtksignal.h"
33 #include "gtkwindow.h"
40 #define DRAG_HANDLE_SIZE 10
41 #define CHILDLESS_SIZE 25
42 #define GHOST_HEIGHT 3
47 SIGNAL_CHILD_ATTACHED,
48 SIGNAL_CHILD_DETACHED,
52 /* The algorithm for docking and redocking implemented here
53 * has a couple of nice properties:
55 * 1) During a single drag, docking always occurs at the
56 * the same cursor position. This means that the users
57 * motions are reversible, and that you won't
58 * undock/dock oscillations.
60 * 2) Docking generally occurs at user-visible features.
61 * The user, once they figure out to redock, will
62 * have useful information about doing it again in
65 * Please try to preserve these properties if you
66 * change the algorithm. (And the current algorithm
67 * is far from ideal). Briefly, the current algorithm
68 * for deciding whether the handlebox is docked or not:
70 * 1) The decision is done by comparing two rectangles - the
71 * allocation if the widget at the start of the drag,
72 * and the boundary of hb->bin_window at the start of
73 * of the drag offset by the distance that the cursor
76 * 2) These rectangles must have one edge, the "snap_edge"
77 * of the handlebox, aligned within TOLERANCE.
79 * 3) On the other dimension, the extents of one rectangle
80 * must be contained in the extents of the other,
81 * extended by tolerance. That is, either we can have:
83 * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE->
84 * <--------float_window-------------------->
88 * <-TOLERANCE-|------float_window--------------|-TOLERANCE->
89 * <--------bin_window-------------------->
92 static void gtk_handle_box_class_init (GtkHandleBoxClass *klass);
93 static void gtk_handle_box_init (GtkHandleBox *handle_box);
94 static void gtk_handle_box_set_arg (GtkObject *object,
97 static void gtk_handle_box_get_arg (GtkObject *object,
100 static void gtk_handle_box_destroy (GtkObject *object);
101 static void gtk_handle_box_map (GtkWidget *widget);
102 static void gtk_handle_box_unmap (GtkWidget *widget);
103 static void gtk_handle_box_realize (GtkWidget *widget);
104 static void gtk_handle_box_unrealize (GtkWidget *widget);
105 static void gtk_handle_box_style_set (GtkWidget *widget,
106 GtkStyle *previous_style);
107 static void gtk_handle_box_size_request (GtkWidget *widget,
108 GtkRequisition *requisition);
109 static void gtk_handle_box_size_allocate (GtkWidget *widget,
110 GtkAllocation *real_allocation);
111 static void gtk_handle_box_add (GtkContainer *container,
113 static void gtk_handle_box_remove (GtkContainer *container,
115 static void gtk_handle_box_draw_ghost (GtkHandleBox *hb);
116 static void gtk_handle_box_paint (GtkWidget *widget,
117 GdkEventExpose *event,
119 static void gtk_handle_box_draw (GtkWidget *widget,
121 static gint gtk_handle_box_expose (GtkWidget *widget,
122 GdkEventExpose *event);
123 static gint gtk_handle_box_button_changed (GtkWidget *widget,
124 GdkEventButton *event);
125 static gint gtk_handle_box_motion (GtkWidget *widget,
126 GdkEventMotion *event);
127 static gint gtk_handle_box_delete_event (GtkWidget *widget,
129 static void gtk_handle_box_reattach (GtkHandleBox *hb);
132 static GtkBinClass *parent_class;
133 static guint handle_box_signals[SIGNAL_LAST] = { 0 };
137 gtk_handle_box_get_type (void)
139 static guint handle_box_type = 0;
141 if (!handle_box_type)
143 static const GtkTypeInfo handle_box_info =
146 sizeof (GtkHandleBox),
147 sizeof (GtkHandleBoxClass),
148 (GtkClassInitFunc) gtk_handle_box_class_init,
149 (GtkObjectInitFunc) gtk_handle_box_init,
150 /* reserved_1 */ NULL,
151 /* reserved_2 */ NULL,
152 (GtkClassInitFunc) NULL,
155 handle_box_type = gtk_type_unique (gtk_bin_get_type (), &handle_box_info);
158 return handle_box_type;
162 gtk_handle_box_class_init (GtkHandleBoxClass *class)
164 GtkObjectClass *object_class;
165 GtkWidgetClass *widget_class;
166 GtkContainerClass *container_class;
168 object_class = (GtkObjectClass *) class;
169 widget_class = (GtkWidgetClass *) class;
170 container_class = (GtkContainerClass *) class;
172 parent_class = gtk_type_class (gtk_bin_get_type ());
174 gtk_object_add_arg_type ("GtkHandleBox::shadow", GTK_TYPE_SHADOW_TYPE, GTK_ARG_READWRITE, ARG_SHADOW);
176 object_class->set_arg = gtk_handle_box_set_arg;
177 object_class->get_arg = gtk_handle_box_get_arg;
179 handle_box_signals[SIGNAL_CHILD_ATTACHED] =
180 gtk_signal_new ("child_attached",
183 GTK_SIGNAL_OFFSET (GtkHandleBoxClass, child_attached),
184 gtk_marshal_NONE__POINTER,
187 handle_box_signals[SIGNAL_CHILD_DETACHED] =
188 gtk_signal_new ("child_detached",
191 GTK_SIGNAL_OFFSET (GtkHandleBoxClass, child_detached),
192 gtk_marshal_NONE__POINTER,
195 gtk_object_class_add_signals (object_class, handle_box_signals, SIGNAL_LAST);
197 object_class->destroy = gtk_handle_box_destroy;
199 widget_class->map = gtk_handle_box_map;
200 widget_class->unmap = gtk_handle_box_unmap;
201 widget_class->realize = gtk_handle_box_realize;
202 widget_class->unrealize = gtk_handle_box_unrealize;
203 widget_class->style_set = gtk_handle_box_style_set;
204 widget_class->size_request = gtk_handle_box_size_request;
205 widget_class->size_allocate = gtk_handle_box_size_allocate;
206 widget_class->draw = gtk_handle_box_draw;
207 widget_class->expose_event = gtk_handle_box_expose;
208 widget_class->button_press_event = gtk_handle_box_button_changed;
209 widget_class->button_release_event = gtk_handle_box_button_changed;
210 widget_class->motion_notify_event = gtk_handle_box_motion;
211 widget_class->delete_event = gtk_handle_box_delete_event;
213 container_class->add = gtk_handle_box_add;
214 container_class->remove = gtk_handle_box_remove;
216 class->child_attached = NULL;
217 class->child_detached = NULL;
221 gtk_handle_box_init (GtkHandleBox *handle_box)
223 GTK_WIDGET_UNSET_FLAGS (handle_box, GTK_NO_WINDOW);
225 handle_box->bin_window = NULL;
226 handle_box->float_window = NULL;
227 handle_box->shadow_type = GTK_SHADOW_OUT;
228 handle_box->handle_position = GTK_POS_LEFT;
229 handle_box->float_window_mapped = FALSE;
230 handle_box->child_detached = FALSE;
231 handle_box->in_drag = FALSE;
232 handle_box->shrink_on_detach = TRUE;
233 handle_box->snap_edge = -1;
237 gtk_handle_box_set_arg (GtkObject *object,
241 GtkHandleBox *handle_box;
243 handle_box = GTK_HANDLE_BOX (object);
248 gtk_handle_box_set_shadow_type (handle_box, GTK_VALUE_ENUM (*arg));
256 gtk_handle_box_get_arg (GtkObject *object,
260 GtkHandleBox *handle_box;
262 handle_box = GTK_HANDLE_BOX (object);
267 GTK_VALUE_ENUM (*arg) = handle_box->shadow_type;
270 arg->type = GTK_TYPE_INVALID;
276 gtk_handle_box_new (void)
278 return GTK_WIDGET (gtk_type_new (gtk_handle_box_get_type ()));
282 gtk_handle_box_destroy (GtkObject *object)
286 g_return_if_fail (object != NULL);
287 g_return_if_fail (GTK_IS_HANDLE_BOX (object));
289 hb = GTK_HANDLE_BOX (object);
291 if (GTK_OBJECT_CLASS (parent_class)->destroy)
292 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
296 gtk_handle_box_map (GtkWidget *widget)
301 g_return_if_fail (widget != NULL);
302 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
304 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
306 bin = GTK_BIN (widget);
307 hb = GTK_HANDLE_BOX (widget);
310 GTK_WIDGET_VISIBLE (bin->child) &&
311 !GTK_WIDGET_MAPPED (bin->child))
312 gtk_widget_map (bin->child);
314 if (hb->child_detached && !hb->float_window_mapped)
316 gdk_window_show (hb->float_window);
317 hb->float_window_mapped = TRUE;
320 gdk_window_show (hb->bin_window);
321 gdk_window_show (widget->window);
325 gtk_handle_box_unmap (GtkWidget *widget)
329 g_return_if_fail (widget != NULL);
330 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
332 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
334 hb = GTK_HANDLE_BOX (widget);
336 gdk_window_hide (widget->window);
337 if (hb->float_window_mapped)
339 gdk_window_hide (hb->float_window);
340 hb->float_window_mapped = FALSE;
345 gtk_handle_box_realize (GtkWidget *widget)
347 GdkWindowAttr attributes;
348 gint attributes_mask;
351 g_return_if_fail (widget != NULL);
352 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
354 hb = GTK_HANDLE_BOX (widget);
356 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
358 attributes.x = widget->allocation.x;
359 attributes.y = widget->allocation.y;
360 attributes.width = widget->allocation.width;
361 attributes.height = widget->allocation.height;
362 attributes.window_type = GDK_WINDOW_CHILD;
363 attributes.wclass = GDK_INPUT_OUTPUT;
364 attributes.visual = gtk_widget_get_visual (widget);
365 attributes.colormap = gtk_widget_get_colormap (widget);
366 attributes.event_mask = (gtk_widget_get_events (widget)
367 | GDK_EXPOSURE_MASK);
368 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
369 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
370 gdk_window_set_user_data (widget->window, widget);
374 attributes.width = widget->allocation.width;
375 attributes.height = widget->allocation.height;
376 attributes.window_type = GDK_WINDOW_CHILD;
377 attributes.event_mask |= (gtk_widget_get_events (widget) |
379 GDK_BUTTON1_MOTION_MASK |
380 GDK_POINTER_MOTION_HINT_MASK |
381 GDK_BUTTON_PRESS_MASK |
382 GDK_BUTTON_RELEASE_MASK);
383 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
384 hb->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask);
385 gdk_window_set_user_data (hb->bin_window, widget);
386 if (GTK_BIN (hb)->child)
387 gtk_widget_set_parent_window (GTK_BIN (hb)->child, hb->bin_window);
391 attributes.width = widget->requisition.width;
392 attributes.height = widget->requisition.height;
393 attributes.window_type = GDK_WINDOW_TOPLEVEL;
394 attributes.wclass = GDK_INPUT_OUTPUT;
395 attributes.visual = gtk_widget_get_visual (widget);
396 attributes.colormap = gtk_widget_get_colormap (widget);
397 attributes.event_mask = (gtk_widget_get_events (widget) |
399 GDK_ENTER_NOTIFY_MASK |
400 GDK_LEAVE_NOTIFY_MASK |
401 GDK_FOCUS_CHANGE_MASK |
403 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
404 hb->float_window = gdk_window_new (NULL, &attributes, attributes_mask);
405 gdk_window_set_user_data (hb->float_window, widget);
406 gdk_window_set_decorations (hb->float_window, 0);
408 widget->style = gtk_style_attach (widget->style, widget->window);
409 gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (hb));
410 gtk_style_set_background (widget->style, hb->bin_window, GTK_WIDGET_STATE (hb));
411 gtk_style_set_background (widget->style, hb->float_window, GTK_WIDGET_STATE (hb));
412 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
416 gtk_handle_box_unrealize (GtkWidget *widget)
420 g_return_if_fail (widget != NULL);
421 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
423 hb = GTK_HANDLE_BOX (widget);
425 gdk_window_set_user_data (hb->bin_window, NULL);
426 gdk_window_destroy (hb->bin_window);
427 hb->bin_window = NULL;
428 gdk_window_set_user_data (hb->float_window, NULL);
429 gdk_window_destroy (hb->float_window);
430 hb->float_window = NULL;
432 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
433 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
437 gtk_handle_box_style_set (GtkWidget *widget,
438 GtkStyle *previous_style)
442 g_return_if_fail (widget != NULL);
443 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
445 hb = GTK_HANDLE_BOX (widget);
447 if (GTK_WIDGET_REALIZED (widget) &&
448 !GTK_WIDGET_NO_WINDOW (widget))
450 gtk_style_set_background (widget->style, widget->window,
452 gtk_style_set_background (widget->style, hb->bin_window, widget->state);
453 gtk_style_set_background (widget->style, hb->float_window, widget->state);
458 gtk_handle_box_size_request (GtkWidget *widget,
459 GtkRequisition *requisition)
463 GtkRequisition child_requisition;
465 g_return_if_fail (widget != NULL);
466 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
467 g_return_if_fail (requisition != NULL);
469 bin = GTK_BIN (widget);
470 hb = GTK_HANDLE_BOX (widget);
472 if (hb->handle_position == GTK_POS_LEFT ||
473 hb->handle_position == GTK_POS_RIGHT)
475 requisition->width = DRAG_HANDLE_SIZE;
476 requisition->height = 0;
480 requisition->width = 0;
481 requisition->height = DRAG_HANDLE_SIZE;
484 /* if our child is not visible, we still request its size, since we
485 * won't have any usefull hint for our size otherwise.
488 gtk_widget_size_request (bin->child, &child_requisition);
491 child_requisition.width = 0;
492 child_requisition.height = 0;
495 if (hb->child_detached)
497 /* FIXME: This doesn't work currently */
498 if (!hb->shrink_on_detach)
500 if (hb->handle_position == GTK_POS_LEFT ||
501 hb->handle_position == GTK_POS_RIGHT)
502 requisition->height += child_requisition.height;
504 requisition->width += child_requisition.width;
508 if (hb->handle_position == GTK_POS_LEFT ||
509 hb->handle_position == GTK_POS_RIGHT)
510 requisition->height += widget->style->klass->ythickness;
512 requisition->width += widget->style->klass->xthickness;
517 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
518 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
522 requisition->width += child_requisition.width;
523 requisition->height += child_requisition.height;
527 requisition->width += CHILDLESS_SIZE;
528 requisition->height += CHILDLESS_SIZE;
534 gtk_handle_box_size_allocate (GtkWidget *widget,
535 GtkAllocation *allocation)
539 GtkRequisition child_requisition;
541 g_return_if_fail (widget != NULL);
542 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
543 g_return_if_fail (allocation != NULL);
545 bin = GTK_BIN (widget);
546 hb = GTK_HANDLE_BOX (widget);
549 gtk_widget_get_child_requisition (bin->child, &child_requisition);
552 child_requisition.width = 0;
553 child_requisition.height = 0;
556 widget->allocation = *allocation;
558 if (GTK_WIDGET_REALIZED (hb))
559 gdk_window_move_resize (widget->window,
560 widget->allocation.x,
561 widget->allocation.y,
562 widget->allocation.width,
563 widget->allocation.height);
566 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
569 GtkAllocation child_allocation;
573 border_width = GTK_CONTAINER (widget)->border_width;
575 child_allocation.x = border_width;
576 child_allocation.y = border_width;
577 if (hb->handle_position == GTK_POS_LEFT)
578 child_allocation.x += DRAG_HANDLE_SIZE;
579 else if (hb->handle_position == GTK_POS_TOP)
580 child_allocation.y += DRAG_HANDLE_SIZE;
582 if (hb->child_detached)
587 child_allocation.width = child_requisition.width;
588 child_allocation.height = child_requisition.height;
590 float_width = child_allocation.width + 2 * border_width;
591 float_height = child_allocation.height + 2 * border_width;
593 if (hb->handle_position == GTK_POS_LEFT ||
594 hb->handle_position == GTK_POS_RIGHT)
595 float_width += DRAG_HANDLE_SIZE;
597 float_height += DRAG_HANDLE_SIZE;
599 if (GTK_WIDGET_REALIZED (hb))
601 gdk_window_resize (hb->float_window,
604 gdk_window_move_resize (hb->bin_window,
613 child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
614 child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
616 if (hb->handle_position == GTK_POS_LEFT ||
617 hb->handle_position == GTK_POS_RIGHT)
618 child_allocation.width -= DRAG_HANDLE_SIZE;
620 child_allocation.height -= DRAG_HANDLE_SIZE;
622 if (GTK_WIDGET_REALIZED (hb))
623 gdk_window_move_resize (hb->bin_window,
626 widget->allocation.width,
627 widget->allocation.height);
630 gtk_widget_size_allocate (bin->child, &child_allocation);
635 gtk_handle_box_draw_ghost (GtkHandleBox *hb)
643 widget = GTK_WIDGET (hb);
645 if (hb->handle_position == GTK_POS_LEFT ||
646 hb->handle_position == GTK_POS_RIGHT)
648 x = hb->handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
650 width = DRAG_HANDLE_SIZE;
651 height = widget->allocation.height;
656 y = hb->handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
657 width = widget->allocation.width;
658 height = DRAG_HANDLE_SIZE;
660 gtk_paint_shadow (widget->style,
662 GTK_WIDGET_STATE (widget),
663 GTK_SHADOW_ETCHED_IN,
664 NULL, widget, "handle",
669 if (hb->handle_position == GTK_POS_LEFT ||
670 hb->handle_position == GTK_POS_RIGHT)
671 gtk_paint_hline (widget->style,
673 GTK_WIDGET_STATE (widget),
674 NULL, widget, "handlebox",
675 hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
676 hb->handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
677 widget->allocation.height / 2);
679 gtk_paint_vline (widget->style,
681 GTK_WIDGET_STATE (widget),
682 NULL, widget, "handlebox",
683 hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
684 hb->handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
685 widget->allocation.width / 2);
689 draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
692 gtk_paint_handle(widget->style, window, GTK_STATE_NORMAL, shadow,
693 clip, widget, "handlebox",
694 rect->x, rect->y, rect->width, rect->height,
695 GTK_ORIENTATION_VERTICAL);
699 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
702 g_return_if_fail (handle_box != NULL);
703 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
705 if ((GtkShadowType) handle_box->shadow_type != type)
707 handle_box->shadow_type = type;
708 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
713 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
714 GtkPositionType position)
716 if ((GtkPositionType) handle_box->handle_position != position)
718 handle_box->handle_position = position;
719 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
724 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
725 GtkPositionType edge)
727 g_return_if_fail (handle_box != NULL);
728 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
730 handle_box->snap_edge = edge;
734 gtk_handle_box_paint (GtkWidget *widget,
735 GdkEventExpose *event,
745 bin = GTK_BIN (widget);
746 hb = GTK_HANDLE_BOX (widget);
748 gdk_window_get_size (hb->bin_window, &width, &height);
751 gtk_paint_box(widget->style,
753 GTK_WIDGET_STATE (widget),
755 area, widget, "handlebox_bin",
758 gtk_paint_box(widget->style,
760 GTK_WIDGET_STATE (widget),
762 &event->area, widget, "handlebox_bin",
765 /* We currently draw the handle _above_ the relief of the handlebox.
766 * it could also be drawn on the same level...
768 hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
769 hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
773 switch (hb->handle_position)
778 rect.width = DRAG_HANDLE_SIZE;
779 rect.height = height;
782 rect.x = width - DRAG_HANDLE_SIZE;
784 rect.width = DRAG_HANDLE_SIZE;
785 rect.height = height;
791 rect.height = DRAG_HANDLE_SIZE;
795 rect.y = height - DRAG_HANDLE_SIZE;
797 rect.height = DRAG_HANDLE_SIZE;
801 if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
802 draw_textured_frame (widget, hb->bin_window, &rect,
804 event ? &event->area : area);
806 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
808 GdkRectangle child_area;
809 GdkEventExpose child_event;
811 if (!event) /* we were called from draw() */
813 if (gtk_widget_intersect (bin->child, area, &child_area))
814 gtk_widget_draw (bin->child, &child_area);
816 else /* we were called from expose() */
818 child_event = *event;
820 if (GTK_WIDGET_NO_WINDOW (bin->child) &&
821 gtk_widget_intersect (bin->child, &event->area, &child_event.area))
822 gtk_widget_event (bin->child, (GdkEvent *) &child_event);
828 gtk_handle_box_draw (GtkWidget *widget,
833 g_return_if_fail (widget != NULL);
834 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
835 g_return_if_fail (area != NULL);
837 hb = GTK_HANDLE_BOX (widget);
839 if (GTK_WIDGET_DRAWABLE (widget))
841 if (hb->child_detached)
843 /* The area parameter does not make sense in this case, so we
844 * repaint everything.
847 gtk_handle_box_draw_ghost (hb);
851 area->width = 2 * GTK_CONTAINER (hb)->border_width + DRAG_HANDLE_SIZE;
852 area->height = area->width + GTK_BIN (hb)->child->allocation.height;
853 area->width += GTK_BIN (hb)->child->allocation.width;
855 gtk_handle_box_paint (widget, NULL, area);
858 gtk_handle_box_paint (widget, NULL, area);
863 gtk_handle_box_expose (GtkWidget *widget,
864 GdkEventExpose *event)
868 g_return_val_if_fail (widget != NULL, FALSE);
869 g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
870 g_return_val_if_fail (event != NULL, FALSE);
872 if (GTK_WIDGET_DRAWABLE (widget))
874 hb = GTK_HANDLE_BOX (widget);
876 if (event->window == widget->window)
878 if (hb->child_detached)
879 gtk_handle_box_draw_ghost (hb);
882 gtk_handle_box_paint (widget, event, NULL);
889 gtk_handle_box_button_changed (GtkWidget *widget,
890 GdkEventButton *event)
893 gboolean event_handled;
896 g_return_val_if_fail (widget != NULL, FALSE);
897 g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
898 g_return_val_if_fail (event != NULL, FALSE);
900 hb = GTK_HANDLE_BOX (widget);
902 event_handled = FALSE;
903 if ((event->button == 1) &&
904 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
909 if (event->window != hb->bin_window)
912 child = GTK_BIN (hb)->child;
914 switch (hb->handle_position)
917 in_handle = event->x < DRAG_HANDLE_SIZE;
920 in_handle = event->y < DRAG_HANDLE_SIZE;
923 in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width;
926 in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height;
936 event_handled = TRUE;
941 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
947 gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y);
948 gdk_window_get_origin (hb->bin_window, &root_x, &root_y);
949 gdk_window_get_size (hb->bin_window, &width, &height);
951 hb->float_allocation.x = root_x - event->x_root;
952 hb->float_allocation.y = root_y - event->y_root;
953 hb->float_allocation.width = width;
954 hb->float_allocation.height = height;
956 hb->deskoff_x = desk_x - root_x;
957 hb->deskoff_y = desk_y - root_y;
959 gdk_window_get_origin (widget->window, &root_x, &root_y);
960 gdk_window_get_size (widget->window, &width, &height);
962 hb->attach_allocation.x = root_x;
963 hb->attach_allocation.y = root_y;
964 hb->attach_allocation.width = width;
965 hb->attach_allocation.height = height;
968 fleur = gdk_cursor_new (GDK_FLEUR);
969 if (gdk_pointer_grab (widget->window,
971 (GDK_BUTTON1_MOTION_MASK |
972 GDK_POINTER_MOTION_HINT_MASK |
973 GDK_BUTTON_RELEASE_MASK),
976 GDK_CURRENT_TIME) != 0)
981 gdk_cursor_destroy (fleur);
982 event_handled = TRUE;
984 else if (hb->child_detached) /* Double click */
986 gtk_handle_box_reattach (hb);
990 else if (event->type == GDK_BUTTON_RELEASE &&
993 if (event->window != widget->window)
996 gdk_pointer_ungrab (GDK_CURRENT_TIME);
998 event_handled = TRUE;
1001 return event_handled;
1005 gtk_handle_box_motion (GtkWidget *widget,
1006 GdkEventMotion *event)
1011 gboolean is_snapped = FALSE;
1013 g_return_val_if_fail (widget != NULL, FALSE);
1014 g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
1015 g_return_val_if_fail (event != NULL, FALSE);
1017 hb = GTK_HANDLE_BOX (widget);
1021 if (!hb->in_drag || (event->window != widget->window))
1024 /* Calculate the attachment point on the float, if the float
1029 gdk_window_get_pointer (GDK_ROOT_PARENT(), &new_x, &new_y, NULL);
1030 new_x += hb->float_allocation.x;
1031 new_y += hb->float_allocation.y;
1033 snap_edge = hb->snap_edge;
1034 if (snap_edge == -1)
1035 snap_edge = (hb->handle_position == GTK_POS_LEFT ||
1036 hb->handle_position == GTK_POS_RIGHT) ?
1037 GTK_POS_TOP : GTK_POS_LEFT;
1039 /* First, check if the snapped edge is aligned
1044 is_snapped = abs (hb->attach_allocation.y - new_y) < TOLERANCE;
1046 case GTK_POS_BOTTOM:
1047 is_snapped = abs (hb->attach_allocation.y + (gint)hb->attach_allocation.height -
1048 new_y - (gint)hb->float_allocation.height) < TOLERANCE;
1051 is_snapped = abs (hb->attach_allocation.x - new_x) < TOLERANCE;
1054 is_snapped = abs (hb->attach_allocation.x + (gint)hb->attach_allocation.width -
1055 new_x - (gint)hb->float_allocation.width) < TOLERANCE;
1059 /* Next, check if coordinates in the other direction are sufficiently
1064 gint float_pos1 = 0; /* Initialize to suppress warnings */
1065 gint float_pos2 = 0;
1066 gint attach_pos1 = 0;
1067 gint attach_pos2 = 0;
1072 case GTK_POS_BOTTOM:
1073 attach_pos1 = hb->attach_allocation.x;
1074 attach_pos2 = hb->attach_allocation.x + hb->attach_allocation.width;
1076 float_pos2 = new_x + hb->float_allocation.width;
1080 attach_pos1 = hb->attach_allocation.y;
1081 attach_pos2 = hb->attach_allocation.y + hb->attach_allocation.height;
1083 float_pos2 = new_y + hb->float_allocation.height;
1087 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1088 (attach_pos2 + TOLERANCE > float_pos2)) ||
1089 ((float_pos1 - TOLERANCE < attach_pos1) &&
1090 (float_pos2 + TOLERANCE > attach_pos2));
1095 if (hb->child_detached)
1097 hb->child_detached = FALSE;
1098 gdk_window_hide (hb->float_window);
1099 gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1100 hb->float_window_mapped = FALSE;
1101 gtk_signal_emit (GTK_OBJECT (hb),
1102 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1103 GTK_BIN (hb)->child);
1105 gtk_widget_queue_resize (widget);
1112 gdk_window_get_size (hb->float_window, &width, &height);
1113 new_x += hb->deskoff_x;
1114 new_y += hb->deskoff_y;
1116 switch (hb->handle_position)
1119 new_y += ((gint)hb->float_allocation.height - height) / 2;
1122 new_x += (gint)hb->float_allocation.width - width;
1123 new_y += ((gint)hb->float_allocation.height - height) / 2;
1126 new_x += ((gint)hb->float_allocation.width - width) / 2;
1128 case GTK_POS_BOTTOM:
1129 new_x += ((gint)hb->float_allocation.width - width) / 2;
1130 new_y += (gint)hb->float_allocation.height - height;
1134 if (hb->child_detached)
1136 gdk_window_move (hb->float_window, new_x, new_y);
1137 gdk_window_raise (hb->float_window);
1143 GtkRequisition child_requisition;
1145 hb->child_detached = TRUE;
1147 if (GTK_BIN (hb)->child)
1148 gtk_widget_get_child_requisition (GTK_BIN (hb)->child, &child_requisition);
1151 child_requisition.width = 0;
1152 child_requisition.height = 0;
1155 width = child_requisition.width + 2 * GTK_CONTAINER (hb)->border_width;
1156 height = child_requisition.height + 2 * GTK_CONTAINER (hb)->border_width;
1157 gdk_window_move_resize (hb->float_window, new_x, new_y, width, height);
1158 gdk_window_reparent (hb->bin_window, hb->float_window, 0, 0);
1159 gdk_window_set_hints (hb->float_window, new_x, new_y, 0, 0, 0, 0, GDK_HINT_POS);
1160 gdk_window_show (hb->float_window);
1161 hb->float_window_mapped = TRUE;
1163 /* this extra move is neccessary if we use decorations, or our
1164 * window manager insists on decorations.
1167 gdk_window_move (hb->float_window, new_x, new_y);
1170 gtk_signal_emit (GTK_OBJECT (hb),
1171 handle_box_signals[SIGNAL_CHILD_DETACHED],
1172 GTK_BIN (hb)->child);
1173 gtk_handle_box_draw_ghost (hb);
1175 gtk_widget_queue_resize (widget);
1183 gtk_handle_box_add (GtkContainer *container,
1186 g_return_if_fail (GTK_IS_HANDLE_BOX (container));
1187 g_return_if_fail (GTK_BIN (container)->child == NULL);
1188 g_return_if_fail (widget->parent == NULL);
1190 gtk_widget_set_parent_window (widget, GTK_HANDLE_BOX (container)->bin_window);
1191 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
1195 gtk_handle_box_remove (GtkContainer *container,
1198 g_return_if_fail (GTK_IS_HANDLE_BOX (container));
1199 g_return_if_fail (GTK_BIN (container)->child == widget);
1201 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1203 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1207 gtk_handle_box_delete_event (GtkWidget *widget,
1212 g_return_val_if_fail (widget != NULL, FALSE);
1213 g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
1214 g_return_val_if_fail (event != NULL, FALSE);
1216 hb = GTK_HANDLE_BOX (widget);
1218 if (event->window == hb->float_window)
1220 gtk_handle_box_reattach (hb);
1229 gtk_handle_box_reattach (GtkHandleBox *hb)
1231 if (hb->child_detached)
1233 hb->child_detached = FALSE;
1234 if (GTK_WIDGET_REALIZED (hb))
1236 gdk_window_hide (hb->float_window);
1237 gdk_window_reparent (hb->bin_window, GTK_WIDGET (hb)->window, 0, 0);
1239 if (GTK_BIN (hb)->child)
1240 gtk_signal_emit (GTK_OBJECT (hb),
1241 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1242 GTK_BIN (hb)->child);
1245 hb->float_window_mapped = FALSE;
1249 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1250 hb->in_drag = FALSE;
1253 gtk_widget_queue_resize (GTK_WIDGET (hb));