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"
41 #define DRAG_HANDLE_SIZE 10
42 #define CHILDLESS_SIZE 25
43 #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 gint gtk_handle_box_expose (GtkWidget *widget,
120 GdkEventExpose *event);
121 static gint gtk_handle_box_button_changed (GtkWidget *widget,
122 GdkEventButton *event);
123 static gint gtk_handle_box_motion (GtkWidget *widget,
124 GdkEventMotion *event);
125 static gint gtk_handle_box_delete_event (GtkWidget *widget,
127 static void gtk_handle_box_reattach (GtkHandleBox *hb);
130 static GtkBinClass *parent_class;
131 static guint handle_box_signals[SIGNAL_LAST] = { 0 };
135 gtk_handle_box_get_type (void)
137 static GtkType handle_box_type = 0;
139 if (!handle_box_type)
141 static const GtkTypeInfo handle_box_info =
144 sizeof (GtkHandleBox),
145 sizeof (GtkHandleBoxClass),
146 (GtkClassInitFunc) gtk_handle_box_class_init,
147 (GtkObjectInitFunc) gtk_handle_box_init,
148 /* reserved_1 */ NULL,
149 /* reserved_2 */ NULL,
150 (GtkClassInitFunc) NULL,
153 handle_box_type = gtk_type_unique (GTK_TYPE_BIN, &handle_box_info);
156 return handle_box_type;
160 gtk_handle_box_class_init (GtkHandleBoxClass *class)
162 GtkObjectClass *object_class;
163 GtkWidgetClass *widget_class;
164 GtkContainerClass *container_class;
166 object_class = (GtkObjectClass *) class;
167 widget_class = (GtkWidgetClass *) class;
168 container_class = (GtkContainerClass *) class;
170 parent_class = gtk_type_class (GTK_TYPE_BIN);
172 gtk_object_add_arg_type ("GtkHandleBox::shadow", GTK_TYPE_SHADOW_TYPE, GTK_ARG_READWRITE, ARG_SHADOW);
173 gtk_object_add_arg_type ("GtkHandleBox::handle_position", GTK_TYPE_POSITION_TYPE, GTK_ARG_READWRITE, ARG_HANDLE_POSITION);
174 gtk_object_add_arg_type ("GtkHandleBox::snap_edge", GTK_TYPE_POSITION_TYPE, GTK_ARG_READWRITE, ARG_SNAP_EDGE);
176 object_class->set_arg = gtk_handle_box_set_arg;
177 object_class->get_arg = gtk_handle_box_get_arg;
180 object_class->destroy = gtk_handle_box_destroy;
182 widget_class->map = gtk_handle_box_map;
183 widget_class->unmap = gtk_handle_box_unmap;
184 widget_class->realize = gtk_handle_box_realize;
185 widget_class->unrealize = gtk_handle_box_unrealize;
186 widget_class->style_set = gtk_handle_box_style_set;
187 widget_class->size_request = gtk_handle_box_size_request;
188 widget_class->size_allocate = gtk_handle_box_size_allocate;
189 widget_class->expose_event = gtk_handle_box_expose;
190 widget_class->button_press_event = gtk_handle_box_button_changed;
191 widget_class->button_release_event = gtk_handle_box_button_changed;
192 widget_class->motion_notify_event = gtk_handle_box_motion;
193 widget_class->delete_event = gtk_handle_box_delete_event;
195 container_class->add = gtk_handle_box_add;
196 container_class->remove = gtk_handle_box_remove;
198 class->child_attached = NULL;
199 class->child_detached = NULL;
201 handle_box_signals[SIGNAL_CHILD_ATTACHED] =
202 gtk_signal_new ("child_attached",
204 GTK_CLASS_TYPE (object_class),
205 GTK_SIGNAL_OFFSET (GtkHandleBoxClass, child_attached),
206 gtk_marshal_VOID__POINTER,
209 handle_box_signals[SIGNAL_CHILD_DETACHED] =
210 gtk_signal_new ("child_detached",
212 GTK_CLASS_TYPE (object_class),
213 GTK_SIGNAL_OFFSET (GtkHandleBoxClass, child_detached),
214 gtk_marshal_VOID__POINTER,
220 gtk_handle_box_init (GtkHandleBox *handle_box)
222 GTK_WIDGET_UNSET_FLAGS (handle_box, GTK_NO_WINDOW);
224 handle_box->bin_window = NULL;
225 handle_box->float_window = NULL;
226 handle_box->shadow_type = GTK_SHADOW_OUT;
227 handle_box->handle_position = GTK_POS_LEFT;
228 handle_box->float_window_mapped = FALSE;
229 handle_box->child_detached = FALSE;
230 handle_box->in_drag = FALSE;
231 handle_box->shrink_on_detach = TRUE;
232 handle_box->snap_edge = -1;
236 gtk_handle_box_set_arg (GtkObject *object,
240 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
245 gtk_handle_box_set_shadow_type (handle_box, GTK_VALUE_ENUM (*arg));
247 case ARG_HANDLE_POSITION:
248 gtk_handle_box_set_handle_position (handle_box, GTK_VALUE_ENUM (*arg));
251 gtk_handle_box_set_snap_edge (handle_box, GTK_VALUE_ENUM (*arg));
257 gtk_handle_box_get_arg (GtkObject *object,
261 GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
266 GTK_VALUE_ENUM (*arg) = handle_box->shadow_type;
268 case ARG_HANDLE_POSITION:
269 GTK_VALUE_ENUM (*arg) = handle_box->handle_position;
272 GTK_VALUE_ENUM (*arg) = handle_box->snap_edge;
275 arg->type = GTK_TYPE_INVALID;
281 gtk_handle_box_new (void)
283 return GTK_WIDGET (gtk_type_new (gtk_handle_box_get_type ()));
287 gtk_handle_box_destroy (GtkObject *object)
291 g_return_if_fail (object != NULL);
292 g_return_if_fail (GTK_IS_HANDLE_BOX (object));
294 hb = GTK_HANDLE_BOX (object);
296 if (GTK_OBJECT_CLASS (parent_class)->destroy)
297 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
301 gtk_handle_box_map (GtkWidget *widget)
306 g_return_if_fail (widget != NULL);
307 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
309 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
311 bin = GTK_BIN (widget);
312 hb = GTK_HANDLE_BOX (widget);
315 GTK_WIDGET_VISIBLE (bin->child) &&
316 !GTK_WIDGET_MAPPED (bin->child))
317 gtk_widget_map (bin->child);
319 if (hb->child_detached && !hb->float_window_mapped)
321 gdk_window_show (hb->float_window);
322 hb->float_window_mapped = TRUE;
325 gdk_window_show (hb->bin_window);
326 gdk_window_show (widget->window);
330 gtk_handle_box_unmap (GtkWidget *widget)
334 g_return_if_fail (widget != NULL);
335 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
337 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
339 hb = GTK_HANDLE_BOX (widget);
341 gdk_window_hide (widget->window);
342 if (hb->float_window_mapped)
344 gdk_window_hide (hb->float_window);
345 hb->float_window_mapped = FALSE;
350 gtk_handle_box_realize (GtkWidget *widget)
352 GdkWindowAttr attributes;
353 gint attributes_mask;
356 g_return_if_fail (widget != NULL);
357 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
359 hb = GTK_HANDLE_BOX (widget);
361 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
363 attributes.x = widget->allocation.x;
364 attributes.y = widget->allocation.y;
365 attributes.width = widget->allocation.width;
366 attributes.height = widget->allocation.height;
367 attributes.window_type = GDK_WINDOW_CHILD;
368 attributes.wclass = GDK_INPUT_OUTPUT;
369 attributes.visual = gtk_widget_get_visual (widget);
370 attributes.colormap = gtk_widget_get_colormap (widget);
371 attributes.event_mask = (gtk_widget_get_events (widget)
372 | GDK_EXPOSURE_MASK);
373 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
374 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
375 gdk_window_set_user_data (widget->window, widget);
379 attributes.width = widget->allocation.width;
380 attributes.height = widget->allocation.height;
381 attributes.window_type = GDK_WINDOW_CHILD;
382 attributes.event_mask |= (gtk_widget_get_events (widget) |
384 GDK_BUTTON1_MOTION_MASK |
385 GDK_POINTER_MOTION_HINT_MASK |
386 GDK_BUTTON_PRESS_MASK |
387 GDK_BUTTON_RELEASE_MASK);
388 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
389 hb->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask);
390 gdk_window_set_user_data (hb->bin_window, widget);
391 if (GTK_BIN (hb)->child)
392 gtk_widget_set_parent_window (GTK_BIN (hb)->child, hb->bin_window);
396 attributes.width = widget->requisition.width;
397 attributes.height = widget->requisition.height;
398 attributes.window_type = GDK_WINDOW_TOPLEVEL;
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) |
404 GDK_ENTER_NOTIFY_MASK |
405 GDK_LEAVE_NOTIFY_MASK |
406 GDK_FOCUS_CHANGE_MASK |
408 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
409 hb->float_window = gdk_window_new (NULL, &attributes, attributes_mask);
410 gdk_window_set_user_data (hb->float_window, widget);
411 gdk_window_set_decorations (hb->float_window, 0);
412 gdk_window_set_type_hint (hb->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
414 widget->style = gtk_style_attach (widget->style, widget->window);
415 gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (hb));
416 gtk_style_set_background (widget->style, hb->bin_window, GTK_WIDGET_STATE (hb));
417 gtk_style_set_background (widget->style, hb->float_window, GTK_WIDGET_STATE (hb));
418 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
422 gtk_handle_box_unrealize (GtkWidget *widget)
426 g_return_if_fail (widget != NULL);
427 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
429 hb = GTK_HANDLE_BOX (widget);
431 gdk_window_set_user_data (hb->bin_window, NULL);
432 gdk_window_destroy (hb->bin_window);
433 hb->bin_window = NULL;
434 gdk_window_set_user_data (hb->float_window, NULL);
435 gdk_window_destroy (hb->float_window);
436 hb->float_window = NULL;
438 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
439 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
443 gtk_handle_box_style_set (GtkWidget *widget,
444 GtkStyle *previous_style)
447 GdkRectangle new_area;
449 g_return_if_fail (widget != NULL);
450 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
452 hb = GTK_HANDLE_BOX (widget);
454 if (GTK_WIDGET_REALIZED (widget) &&
455 !GTK_WIDGET_NO_WINDOW (widget))
457 gtk_style_set_background (widget->style, widget->window,
459 gtk_style_set_background (widget->style, hb->bin_window, widget->state);
460 gtk_style_set_background (widget->style, hb->float_window, widget->state);
465 gtk_handle_box_size_request (GtkWidget *widget,
466 GtkRequisition *requisition)
470 GtkRequisition child_requisition;
472 g_return_if_fail (widget != NULL);
473 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
474 g_return_if_fail (requisition != NULL);
476 bin = GTK_BIN (widget);
477 hb = GTK_HANDLE_BOX (widget);
479 if (hb->handle_position == GTK_POS_LEFT ||
480 hb->handle_position == GTK_POS_RIGHT)
482 requisition->width = DRAG_HANDLE_SIZE;
483 requisition->height = 0;
487 requisition->width = 0;
488 requisition->height = DRAG_HANDLE_SIZE;
491 /* if our child is not visible, we still request its size, since we
492 * won't have any usefull hint for our size otherwise.
495 gtk_widget_size_request (bin->child, &child_requisition);
498 child_requisition.width = 0;
499 child_requisition.height = 0;
502 if (hb->child_detached)
504 /* FIXME: This doesn't work currently */
505 if (!hb->shrink_on_detach)
507 if (hb->handle_position == GTK_POS_LEFT ||
508 hb->handle_position == GTK_POS_RIGHT)
509 requisition->height += child_requisition.height;
511 requisition->width += child_requisition.width;
515 if (hb->handle_position == GTK_POS_LEFT ||
516 hb->handle_position == GTK_POS_RIGHT)
517 requisition->height += widget->style->ythickness;
519 requisition->width += widget->style->xthickness;
524 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
525 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
529 requisition->width += child_requisition.width;
530 requisition->height += child_requisition.height;
534 requisition->width += CHILDLESS_SIZE;
535 requisition->height += CHILDLESS_SIZE;
541 gtk_handle_box_size_allocate (GtkWidget *widget,
542 GtkAllocation *allocation)
546 GtkRequisition child_requisition;
548 g_return_if_fail (widget != NULL);
549 g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
550 g_return_if_fail (allocation != NULL);
552 bin = GTK_BIN (widget);
553 hb = GTK_HANDLE_BOX (widget);
556 gtk_widget_get_child_requisition (bin->child, &child_requisition);
559 child_requisition.width = 0;
560 child_requisition.height = 0;
563 widget->allocation = *allocation;
565 if (GTK_WIDGET_REALIZED (hb))
566 gdk_window_move_resize (widget->window,
567 widget->allocation.x,
568 widget->allocation.y,
569 widget->allocation.width,
570 widget->allocation.height);
573 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
576 GtkAllocation child_allocation;
580 border_width = GTK_CONTAINER (widget)->border_width;
582 child_allocation.x = border_width;
583 child_allocation.y = border_width;
584 if (hb->handle_position == GTK_POS_LEFT)
585 child_allocation.x += DRAG_HANDLE_SIZE;
586 else if (hb->handle_position == GTK_POS_TOP)
587 child_allocation.y += DRAG_HANDLE_SIZE;
589 if (hb->child_detached)
594 child_allocation.width = child_requisition.width;
595 child_allocation.height = child_requisition.height;
597 float_width = child_allocation.width + 2 * border_width;
598 float_height = child_allocation.height + 2 * border_width;
600 if (hb->handle_position == GTK_POS_LEFT ||
601 hb->handle_position == GTK_POS_RIGHT)
602 float_width += DRAG_HANDLE_SIZE;
604 float_height += DRAG_HANDLE_SIZE;
606 if (GTK_WIDGET_REALIZED (hb))
608 gdk_window_resize (hb->float_window,
611 gdk_window_move_resize (hb->bin_window,
620 child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
621 child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
623 if (hb->handle_position == GTK_POS_LEFT ||
624 hb->handle_position == GTK_POS_RIGHT)
625 child_allocation.width -= DRAG_HANDLE_SIZE;
627 child_allocation.height -= DRAG_HANDLE_SIZE;
629 if (GTK_WIDGET_REALIZED (hb))
630 gdk_window_move_resize (hb->bin_window,
633 widget->allocation.width,
634 widget->allocation.height);
637 gtk_widget_size_allocate (bin->child, &child_allocation);
642 gtk_handle_box_draw_ghost (GtkHandleBox *hb)
650 widget = GTK_WIDGET (hb);
652 if (hb->handle_position == GTK_POS_LEFT ||
653 hb->handle_position == GTK_POS_RIGHT)
655 x = hb->handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
657 width = DRAG_HANDLE_SIZE;
658 height = widget->allocation.height;
663 y = hb->handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
664 width = widget->allocation.width;
665 height = DRAG_HANDLE_SIZE;
667 gtk_paint_shadow (widget->style,
669 GTK_WIDGET_STATE (widget),
670 GTK_SHADOW_ETCHED_IN,
671 NULL, widget, "handle",
676 if (hb->handle_position == GTK_POS_LEFT ||
677 hb->handle_position == GTK_POS_RIGHT)
678 gtk_paint_hline (widget->style,
680 GTK_WIDGET_STATE (widget),
681 NULL, widget, "handlebox",
682 hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
683 hb->handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
684 widget->allocation.height / 2);
686 gtk_paint_vline (widget->style,
688 GTK_WIDGET_STATE (widget),
689 NULL, widget, "handlebox",
690 hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
691 hb->handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
692 widget->allocation.width / 2);
696 draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
699 gtk_paint_handle(widget->style, window, GTK_STATE_NORMAL, shadow,
700 clip, widget, "handlebox",
701 rect->x, rect->y, rect->width, rect->height,
702 GTK_ORIENTATION_VERTICAL);
706 gtk_handle_box_set_shadow_type (GtkHandleBox *handle_box,
709 g_return_if_fail (handle_box != NULL);
710 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
712 if ((GtkShadowType) handle_box->shadow_type != type)
714 handle_box->shadow_type = type;
715 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
720 gtk_handle_box_set_handle_position (GtkHandleBox *handle_box,
721 GtkPositionType position)
723 if ((GtkPositionType) handle_box->handle_position != position)
725 handle_box->handle_position = position;
726 gtk_widget_queue_resize (GTK_WIDGET (handle_box));
731 gtk_handle_box_set_snap_edge (GtkHandleBox *handle_box,
732 GtkPositionType edge)
734 g_return_if_fail (handle_box != NULL);
735 g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
737 handle_box->snap_edge = edge;
741 gtk_handle_box_paint (GtkWidget *widget,
742 GdkEventExpose *event,
752 bin = GTK_BIN (widget);
753 hb = GTK_HANDLE_BOX (widget);
755 gdk_window_get_size (hb->bin_window, &width, &height);
758 gtk_paint_box(widget->style,
760 GTK_WIDGET_STATE (widget),
762 area, widget, "handlebox_bin",
765 gtk_paint_box(widget->style,
767 GTK_WIDGET_STATE (widget),
769 &event->area, widget, "handlebox_bin",
772 /* We currently draw the handle _above_ the relief of the handlebox.
773 * it could also be drawn on the same level...
775 hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
776 hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
780 switch (hb->handle_position)
785 rect.width = DRAG_HANDLE_SIZE;
786 rect.height = height;
789 rect.x = width - DRAG_HANDLE_SIZE;
791 rect.width = DRAG_HANDLE_SIZE;
792 rect.height = height;
798 rect.height = DRAG_HANDLE_SIZE;
802 rect.y = height - DRAG_HANDLE_SIZE;
804 rect.height = DRAG_HANDLE_SIZE;
808 if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
809 draw_textured_frame (widget, hb->bin_window, &rect,
811 event ? &event->area : area);
813 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
815 GdkRectangle child_area;
816 GdkEventExpose child_event;
818 if (!event) /* we were called from draw() */
820 if (gtk_widget_intersect (bin->child, area, &child_area))
821 gtk_widget_draw (bin->child, &child_area);
823 else /* we were called from expose() */
825 child_event = *event;
827 if (GTK_WIDGET_NO_WINDOW (bin->child) &&
828 gtk_widget_intersect (bin->child, &event->area, &child_event.area))
829 gtk_widget_event (bin->child, (GdkEvent *) &child_event);
835 gtk_handle_box_expose (GtkWidget *widget,
836 GdkEventExpose *event)
840 g_return_val_if_fail (widget != NULL, FALSE);
841 g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
842 g_return_val_if_fail (event != NULL, FALSE);
844 if (GTK_WIDGET_DRAWABLE (widget))
846 hb = GTK_HANDLE_BOX (widget);
848 if (event->window == widget->window)
850 if (hb->child_detached)
851 gtk_handle_box_draw_ghost (hb);
854 gtk_handle_box_paint (widget, event, NULL);
861 gtk_handle_box_button_changed (GtkWidget *widget,
862 GdkEventButton *event)
865 gboolean event_handled;
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 hb = GTK_HANDLE_BOX (widget);
874 event_handled = FALSE;
875 if ((event->button == 1) &&
876 (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
881 if (event->window != hb->bin_window)
884 child = GTK_BIN (hb)->child;
888 switch (hb->handle_position)
891 in_handle = event->x < DRAG_HANDLE_SIZE;
894 in_handle = event->y < DRAG_HANDLE_SIZE;
897 in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width;
900 in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height;
910 event_handled = TRUE;
915 if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
921 gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y);
922 gdk_window_get_origin (hb->bin_window, &root_x, &root_y);
923 gdk_window_get_size (hb->bin_window, &width, &height);
925 hb->float_allocation.x = root_x - event->x_root;
926 hb->float_allocation.y = root_y - event->y_root;
927 hb->float_allocation.width = width;
928 hb->float_allocation.height = height;
930 hb->deskoff_x = desk_x - root_x;
931 hb->deskoff_y = desk_y - root_y;
933 gdk_window_get_origin (widget->window, &root_x, &root_y);
934 gdk_window_get_size (widget->window, &width, &height);
936 hb->attach_allocation.x = root_x;
937 hb->attach_allocation.y = root_y;
938 hb->attach_allocation.width = width;
939 hb->attach_allocation.height = height;
942 fleur = gdk_cursor_new (GDK_FLEUR);
943 if (gdk_pointer_grab (widget->window,
945 (GDK_BUTTON1_MOTION_MASK |
946 GDK_POINTER_MOTION_HINT_MASK |
947 GDK_BUTTON_RELEASE_MASK),
950 GDK_CURRENT_TIME) != 0)
955 gdk_cursor_destroy (fleur);
956 event_handled = TRUE;
958 else if (hb->child_detached) /* Double click */
960 gtk_handle_box_reattach (hb);
964 else if (event->type == GDK_BUTTON_RELEASE &&
967 if (event->window != widget->window)
970 gdk_pointer_ungrab (GDK_CURRENT_TIME);
972 event_handled = TRUE;
975 return event_handled;
979 gtk_handle_box_motion (GtkWidget *widget,
980 GdkEventMotion *event)
985 gboolean is_snapped = FALSE;
987 g_return_val_if_fail (widget != NULL, FALSE);
988 g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
989 g_return_val_if_fail (event != NULL, FALSE);
991 hb = GTK_HANDLE_BOX (widget);
995 if (!hb->in_drag || (event->window != widget->window))
998 /* Calculate the attachment point on the float, if the float
1003 gdk_window_get_pointer (NULL, &new_x, &new_y, NULL);
1004 new_x += hb->float_allocation.x;
1005 new_y += hb->float_allocation.y;
1007 snap_edge = hb->snap_edge;
1008 if (snap_edge == -1)
1009 snap_edge = (hb->handle_position == GTK_POS_LEFT ||
1010 hb->handle_position == GTK_POS_RIGHT) ?
1011 GTK_POS_TOP : GTK_POS_LEFT;
1013 /* First, check if the snapped edge is aligned
1018 is_snapped = abs (hb->attach_allocation.y - new_y) < TOLERANCE;
1020 case GTK_POS_BOTTOM:
1021 is_snapped = abs (hb->attach_allocation.y + (gint)hb->attach_allocation.height -
1022 new_y - (gint)hb->float_allocation.height) < TOLERANCE;
1025 is_snapped = abs (hb->attach_allocation.x - new_x) < TOLERANCE;
1028 is_snapped = abs (hb->attach_allocation.x + (gint)hb->attach_allocation.width -
1029 new_x - (gint)hb->float_allocation.width) < TOLERANCE;
1033 /* Next, check if coordinates in the other direction are sufficiently
1038 gint float_pos1 = 0; /* Initialize to suppress warnings */
1039 gint float_pos2 = 0;
1040 gint attach_pos1 = 0;
1041 gint attach_pos2 = 0;
1046 case GTK_POS_BOTTOM:
1047 attach_pos1 = hb->attach_allocation.x;
1048 attach_pos2 = hb->attach_allocation.x + hb->attach_allocation.width;
1050 float_pos2 = new_x + hb->float_allocation.width;
1054 attach_pos1 = hb->attach_allocation.y;
1055 attach_pos2 = hb->attach_allocation.y + hb->attach_allocation.height;
1057 float_pos2 = new_y + hb->float_allocation.height;
1061 is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) &&
1062 (attach_pos2 + TOLERANCE > float_pos2)) ||
1063 ((float_pos1 - TOLERANCE < attach_pos1) &&
1064 (float_pos2 + TOLERANCE > attach_pos2));
1069 if (hb->child_detached)
1071 hb->child_detached = FALSE;
1072 gdk_window_hide (hb->float_window);
1073 gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1074 hb->float_window_mapped = FALSE;
1075 gtk_signal_emit (GTK_OBJECT (hb),
1076 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1077 GTK_BIN (hb)->child);
1079 gtk_widget_queue_resize (widget);
1086 gdk_window_get_size (hb->float_window, &width, &height);
1087 new_x += hb->deskoff_x;
1088 new_y += hb->deskoff_y;
1090 switch (hb->handle_position)
1093 new_y += ((gint)hb->float_allocation.height - height) / 2;
1096 new_x += (gint)hb->float_allocation.width - width;
1097 new_y += ((gint)hb->float_allocation.height - height) / 2;
1100 new_x += ((gint)hb->float_allocation.width - width) / 2;
1102 case GTK_POS_BOTTOM:
1103 new_x += ((gint)hb->float_allocation.width - width) / 2;
1104 new_y += (gint)hb->float_allocation.height - height;
1108 if (hb->child_detached)
1110 gdk_window_move (hb->float_window, new_x, new_y);
1111 gdk_window_raise (hb->float_window);
1117 GtkRequisition child_requisition;
1119 hb->child_detached = TRUE;
1121 if (GTK_BIN (hb)->child)
1122 gtk_widget_get_child_requisition (GTK_BIN (hb)->child, &child_requisition);
1125 child_requisition.width = 0;
1126 child_requisition.height = 0;
1129 width = child_requisition.width + 2 * GTK_CONTAINER (hb)->border_width;
1130 height = child_requisition.height + 2 * GTK_CONTAINER (hb)->border_width;
1131 gdk_window_move_resize (hb->float_window, new_x, new_y, width, height);
1132 gdk_window_reparent (hb->bin_window, hb->float_window, 0, 0);
1133 gdk_window_set_hints (hb->float_window, new_x, new_y, 0, 0, 0, 0, GDK_HINT_POS);
1134 gdk_window_show (hb->float_window);
1135 hb->float_window_mapped = TRUE;
1137 /* this extra move is neccessary if we use decorations, or our
1138 * window manager insists on decorations.
1141 gdk_window_move (hb->float_window, new_x, new_y);
1144 gtk_signal_emit (GTK_OBJECT (hb),
1145 handle_box_signals[SIGNAL_CHILD_DETACHED],
1146 GTK_BIN (hb)->child);
1147 gtk_handle_box_draw_ghost (hb);
1149 gtk_widget_queue_resize (widget);
1157 gtk_handle_box_add (GtkContainer *container,
1160 g_return_if_fail (GTK_IS_HANDLE_BOX (container));
1161 g_return_if_fail (GTK_BIN (container)->child == NULL);
1162 g_return_if_fail (widget->parent == NULL);
1164 gtk_widget_set_parent_window (widget, GTK_HANDLE_BOX (container)->bin_window);
1165 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
1169 gtk_handle_box_remove (GtkContainer *container,
1172 g_return_if_fail (GTK_IS_HANDLE_BOX (container));
1173 g_return_if_fail (GTK_BIN (container)->child == widget);
1175 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1177 gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1181 gtk_handle_box_delete_event (GtkWidget *widget,
1186 g_return_val_if_fail (widget != NULL, FALSE);
1187 g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
1188 g_return_val_if_fail (event != NULL, FALSE);
1190 hb = GTK_HANDLE_BOX (widget);
1192 if (event->window == hb->float_window)
1194 gtk_handle_box_reattach (hb);
1203 gtk_handle_box_reattach (GtkHandleBox *hb)
1205 if (hb->child_detached)
1207 hb->child_detached = FALSE;
1208 if (GTK_WIDGET_REALIZED (hb))
1210 gdk_window_hide (hb->float_window);
1211 gdk_window_reparent (hb->bin_window, GTK_WIDGET (hb)->window, 0, 0);
1213 if (GTK_BIN (hb)->child)
1214 gtk_signal_emit (GTK_OBJECT (hb),
1215 handle_box_signals[SIGNAL_CHILD_ATTACHED],
1216 GTK_BIN (hb)->child);
1219 hb->float_window_mapped = FALSE;
1223 gdk_pointer_ungrab (GDK_CURRENT_TIME);
1224 hb->in_drag = FALSE;
1227 gtk_widget_queue_resize (GTK_WIDGET (hb));