]> Pileus Git - ~andy/gtk/blob - gtk/gtkhandlebox.c
Deprecate widget flag: GTK_WIDGET_MAPPED
[~andy/gtk] / gtk / gtkhandlebox.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  * Copyright (C) 1998 Elliot Lee
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /*
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/. 
26  */
27
28 #include "config.h"
29 #include <stdlib.h>
30 #include "gtkhandlebox.h"
31 #include "gtkinvisible.h"
32 #include "gtkmain.h"
33 #include "gtkmarshalers.h"
34 #include "gtkwindow.h"
35 #include "gtkprivate.h"
36 #include "gtkintl.h"
37 #include "gtkalias.h"
38
39 typedef struct _GtkHandleBoxPrivate GtkHandleBoxPrivate;
40
41 struct _GtkHandleBoxPrivate
42 {
43   gint orig_x;
44   gint orig_y;
45 };
46
47 enum {
48   PROP_0,
49   PROP_SHADOW,
50   PROP_SHADOW_TYPE,
51   PROP_HANDLE_POSITION,
52   PROP_SNAP_EDGE,
53   PROP_SNAP_EDGE_SET,
54   PROP_CHILD_DETACHED
55 };
56
57 #define DRAG_HANDLE_SIZE 10
58 #define CHILDLESS_SIZE  25
59 #define GHOST_HEIGHT 3
60 #define TOLERANCE 5
61
62 enum {
63   SIGNAL_CHILD_ATTACHED,
64   SIGNAL_CHILD_DETACHED,
65   SIGNAL_LAST
66 };
67
68 /* The algorithm for docking and redocking implemented here
69  * has a couple of nice properties:
70  *
71  * 1) During a single drag, docking always occurs at the
72  *    the same cursor position. This means that the users
73  *    motions are reversible, and that you won't
74  *    undock/dock oscillations.
75  *
76  * 2) Docking generally occurs at user-visible features.
77  *    The user, once they figure out to redock, will
78  *    have useful information about doing it again in
79  *    the future.
80  *
81  * Please try to preserve these properties if you
82  * change the algorithm. (And the current algorithm
83  * is far from ideal). Briefly, the current algorithm
84  * for deciding whether the handlebox is docked or not:
85  *
86  * 1) The decision is done by comparing two rectangles - the
87  *    allocation if the widget at the start of the drag,
88  *    and the boundary of hb->bin_window at the start of
89  *    of the drag offset by the distance that the cursor
90  *    has moved.
91  *
92  * 2) These rectangles must have one edge, the "snap_edge"
93  *    of the handlebox, aligned within TOLERANCE.
94  * 
95  * 3) On the other dimension, the extents of one rectangle
96  *    must be contained in the extents of the other,
97  *    extended by tolerance. That is, either we can have:
98  *
99  * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE->
100  *         <--------float_window-------------------->
101  *
102  * or we can have:
103  *
104  * <-TOLERANCE-|------float_window--------------|-TOLERANCE->
105  *          <--------bin_window-------------------->
106  */
107
108 static void     gtk_handle_box_set_property  (GObject        *object,
109                                               guint           param_id,
110                                               const GValue   *value,
111                                               GParamSpec     *pspec);
112 static void     gtk_handle_box_get_property  (GObject        *object,
113                                               guint           param_id,
114                                               GValue         *value,
115                                               GParamSpec     *pspec);
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,
127                                               GtkWidget      *widget);
128 static void     gtk_handle_box_remove        (GtkContainer   *container,
129                                               GtkWidget      *widget);
130 static void     gtk_handle_box_draw_ghost    (GtkHandleBox   *hb);
131 static void     gtk_handle_box_paint         (GtkWidget      *widget,
132                                               GdkEventExpose *event,
133                                               GdkRectangle   *area);
134 static gboolean gtk_handle_box_expose        (GtkWidget      *widget,
135                                               GdkEventExpose *event);
136 static gboolean gtk_handle_box_button_press  (GtkWidget      *widget,
137                                               GdkEventButton *event);
138 static gboolean gtk_handle_box_motion        (GtkWidget      *widget,
139                                               GdkEventMotion *event);
140 static gboolean gtk_handle_box_delete_event  (GtkWidget      *widget,
141                                               GdkEventAny    *event);
142 static void     gtk_handle_box_reattach      (GtkHandleBox   *hb);
143 static void     gtk_handle_box_end_drag      (GtkHandleBox   *hb,
144                                               guint32         time);
145
146 static guint handle_box_signals[SIGNAL_LAST] = { 0 };
147
148 G_DEFINE_TYPE (GtkHandleBox, gtk_handle_box, GTK_TYPE_BIN)
149
150 static void
151 gtk_handle_box_class_init (GtkHandleBoxClass *class)
152 {
153   GObjectClass *gobject_class;
154   GtkWidgetClass *widget_class;
155   GtkContainerClass *container_class;
156
157   gobject_class = (GObjectClass *) class;
158   widget_class = (GtkWidgetClass *) class;
159   container_class = (GtkContainerClass *) class;
160
161   gobject_class->set_property = gtk_handle_box_set_property;
162   gobject_class->get_property = gtk_handle_box_get_property;
163   
164   g_object_class_install_property (gobject_class,
165                                    PROP_SHADOW,
166                                    g_param_spec_enum ("shadow", NULL,
167                                                       P_("Deprecated property, use shadow_type instead"),
168                                                       GTK_TYPE_SHADOW_TYPE,
169                                                       GTK_SHADOW_OUT,
170                                                       GTK_PARAM_READWRITE));
171   g_object_class_install_property (gobject_class,
172                                    PROP_SHADOW_TYPE,
173                                    g_param_spec_enum ("shadow-type",
174                                                       P_("Shadow type"),
175                                                       P_("Appearance of the shadow that surrounds the container"),
176                                                       GTK_TYPE_SHADOW_TYPE,
177                                                       GTK_SHADOW_OUT,
178                                                       GTK_PARAM_READWRITE));
179   
180   g_object_class_install_property (gobject_class,
181                                    PROP_HANDLE_POSITION,
182                                    g_param_spec_enum ("handle-position",
183                                                       P_("Handle position"),
184                                                       P_("Position of the handle relative to the child widget"),
185                                                       GTK_TYPE_POSITION_TYPE,
186                                                       GTK_POS_LEFT,
187                                                       GTK_PARAM_READWRITE));
188   
189   g_object_class_install_property (gobject_class,
190                                    PROP_SNAP_EDGE,
191                                    g_param_spec_enum ("snap-edge",
192                                                       P_("Snap edge"),
193                                                       P_("Side of the handlebox that's lined up with the docking point to dock the handlebox"),
194                                                       GTK_TYPE_POSITION_TYPE,
195                                                       GTK_POS_TOP,
196                                                       GTK_PARAM_READWRITE));
197
198   g_object_class_install_property (gobject_class,
199                                    PROP_SNAP_EDGE_SET,
200                                    g_param_spec_boolean ("snap-edge-set",
201                                                          P_("Snap edge set"),
202                                                          P_("Whether to use the value from the snap_edge property or a value derived from handle_position"),
203                                                          FALSE,
204                                                          GTK_PARAM_READWRITE));
205
206   g_object_class_install_property (gobject_class,
207                                    PROP_CHILD_DETACHED,
208                                    g_param_spec_boolean ("child-detached",
209                                                          P_("Child Detached"),
210                                                          P_("A boolean value indicating whether the handlebox's child is attached or detached."),
211                                                          FALSE,
212                                                          GTK_PARAM_READABLE));
213
214   widget_class->map = gtk_handle_box_map;
215   widget_class->unmap = gtk_handle_box_unmap;
216   widget_class->realize = gtk_handle_box_realize;
217   widget_class->unrealize = gtk_handle_box_unrealize;
218   widget_class->style_set = gtk_handle_box_style_set;
219   widget_class->size_request = gtk_handle_box_size_request;
220   widget_class->size_allocate = gtk_handle_box_size_allocate;
221   widget_class->expose_event = gtk_handle_box_expose;
222   widget_class->button_press_event = gtk_handle_box_button_press;
223   widget_class->delete_event = gtk_handle_box_delete_event;
224
225   container_class->add = gtk_handle_box_add;
226   container_class->remove = gtk_handle_box_remove;
227
228   class->child_attached = NULL;
229   class->child_detached = NULL;
230
231   handle_box_signals[SIGNAL_CHILD_ATTACHED] =
232     g_signal_new (I_("child-attached"),
233                   G_OBJECT_CLASS_TYPE (gobject_class),
234                   G_SIGNAL_RUN_FIRST,
235                   G_STRUCT_OFFSET (GtkHandleBoxClass, child_attached),
236                   NULL, NULL,
237                   _gtk_marshal_VOID__OBJECT,
238                   G_TYPE_NONE, 1,
239                   GTK_TYPE_WIDGET);
240   handle_box_signals[SIGNAL_CHILD_DETACHED] =
241     g_signal_new (I_("child-detached"),
242                   G_OBJECT_CLASS_TYPE (gobject_class),
243                   G_SIGNAL_RUN_FIRST,
244                   G_STRUCT_OFFSET (GtkHandleBoxClass, child_detached),
245                   NULL, NULL,
246                   _gtk_marshal_VOID__OBJECT,
247                   G_TYPE_NONE, 1,
248                   GTK_TYPE_WIDGET);
249
250   g_type_class_add_private (gobject_class, sizeof (GtkHandleBoxPrivate));    
251 }
252
253 static GtkHandleBoxPrivate *
254 gtk_handle_box_get_private (GtkHandleBox *hb)
255 {
256   return G_TYPE_INSTANCE_GET_PRIVATE (hb, GTK_TYPE_HANDLE_BOX, GtkHandleBoxPrivate);
257 }
258
259 static void
260 gtk_handle_box_init (GtkHandleBox *handle_box)
261 {
262   GTK_WIDGET_UNSET_FLAGS (handle_box, GTK_NO_WINDOW);
263
264   handle_box->bin_window = NULL;
265   handle_box->float_window = NULL;
266   handle_box->shadow_type = GTK_SHADOW_OUT;
267   handle_box->handle_position = GTK_POS_LEFT;
268   handle_box->float_window_mapped = FALSE;
269   handle_box->child_detached = FALSE;
270   handle_box->in_drag = FALSE;
271   handle_box->shrink_on_detach = TRUE;
272   handle_box->snap_edge = -1;
273 }
274
275 static void 
276 gtk_handle_box_set_property (GObject         *object,
277                              guint            prop_id,
278                              const GValue    *value,
279                              GParamSpec      *pspec)
280 {
281   GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
282
283   switch (prop_id)
284     {
285     case PROP_SHADOW:
286     case PROP_SHADOW_TYPE:
287       gtk_handle_box_set_shadow_type (handle_box, g_value_get_enum (value));
288       break;
289     case PROP_HANDLE_POSITION:
290       gtk_handle_box_set_handle_position (handle_box, g_value_get_enum (value));
291       break;
292     case PROP_SNAP_EDGE:
293       gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value));
294       break;
295     case PROP_SNAP_EDGE_SET:
296       if (!g_value_get_boolean (value))
297         gtk_handle_box_set_snap_edge (handle_box, (GtkPositionType)-1);
298       break;
299     default:
300       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301       break;
302     }
303 }
304
305 static void 
306 gtk_handle_box_get_property (GObject         *object,
307                              guint            prop_id,
308                              GValue          *value,
309                              GParamSpec      *pspec)
310 {
311   GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
312   
313   switch (prop_id)
314     {
315     case PROP_SHADOW:
316     case PROP_SHADOW_TYPE:
317       g_value_set_enum (value, handle_box->shadow_type);
318       break;
319     case PROP_HANDLE_POSITION:
320       g_value_set_enum (value, handle_box->handle_position);
321       break;
322     case PROP_SNAP_EDGE:
323       g_value_set_enum (value,
324                         (handle_box->snap_edge == -1 ?
325                          GTK_POS_TOP : handle_box->snap_edge));
326       break;
327     case PROP_SNAP_EDGE_SET:
328       g_value_set_boolean (value, handle_box->snap_edge != -1);
329       break;
330     case PROP_CHILD_DETACHED:
331       g_value_set_boolean (value, handle_box->child_detached);
332       break;
333     default:
334       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
335       break;
336     }
337 }
338  
339 GtkWidget*
340 gtk_handle_box_new (void)
341 {
342   return g_object_new (GTK_TYPE_HANDLE_BOX, NULL);
343 }
344
345 static void
346 gtk_handle_box_map (GtkWidget *widget)
347 {
348   GtkBin *bin;
349   GtkHandleBox *hb;
350
351   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
352
353   bin = GTK_BIN (widget);
354   hb = GTK_HANDLE_BOX (widget);
355
356   if (bin->child &&
357       gtk_widget_get_visible (bin->child) &&
358       !gtk_widget_get_mapped (bin->child))
359     gtk_widget_map (bin->child);
360
361   if (hb->child_detached && !hb->float_window_mapped)
362     {
363       gdk_window_show (hb->float_window);
364       hb->float_window_mapped = TRUE;
365     }
366
367   gdk_window_show (hb->bin_window);
368   gdk_window_show (widget->window);
369 }
370
371 static void
372 gtk_handle_box_unmap (GtkWidget *widget)
373 {
374   GtkHandleBox *hb;
375
376   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
377
378   hb = GTK_HANDLE_BOX (widget);
379
380   gdk_window_hide (widget->window);
381   if (hb->float_window_mapped)
382     {
383       gdk_window_hide (hb->float_window);
384       hb->float_window_mapped = FALSE;
385     }
386 }
387
388 static void
389 gtk_handle_box_realize (GtkWidget *widget)
390 {
391   GdkWindowAttr attributes;
392   gint attributes_mask;
393   GtkHandleBox *hb;
394
395   hb = GTK_HANDLE_BOX (widget);
396
397   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
398
399   attributes.x = widget->allocation.x;
400   attributes.y = widget->allocation.y;
401   attributes.width = widget->allocation.width;
402   attributes.height = widget->allocation.height;
403   attributes.window_type = GDK_WINDOW_CHILD;
404   attributes.wclass = GDK_INPUT_OUTPUT;
405   attributes.visual = gtk_widget_get_visual (widget);
406   attributes.colormap = gtk_widget_get_colormap (widget);
407   attributes.event_mask = (gtk_widget_get_events (widget)
408                            | GDK_EXPOSURE_MASK);
409   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
410   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
411   gdk_window_set_user_data (widget->window, widget);
412
413   attributes.x = 0;
414   attributes.y = 0;
415   attributes.width = widget->allocation.width;
416   attributes.height = widget->allocation.height;
417   attributes.window_type = GDK_WINDOW_CHILD;
418   attributes.event_mask = (gtk_widget_get_events (widget) |
419                            GDK_EXPOSURE_MASK |
420                            GDK_BUTTON1_MOTION_MASK |
421                            GDK_POINTER_MOTION_HINT_MASK |
422                            GDK_BUTTON_PRESS_MASK |
423                             GDK_BUTTON_RELEASE_MASK);
424   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
425   hb->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask);
426   gdk_window_set_user_data (hb->bin_window, widget);
427   if (GTK_BIN (hb)->child)
428     gtk_widget_set_parent_window (GTK_BIN (hb)->child, hb->bin_window);
429   
430   attributes.x = 0;
431   attributes.y = 0;
432   attributes.width = widget->requisition.width;
433   attributes.height = widget->requisition.height;
434   attributes.window_type = GDK_WINDOW_TOPLEVEL;
435   attributes.wclass = GDK_INPUT_OUTPUT;
436   attributes.visual = gtk_widget_get_visual (widget);
437   attributes.colormap = gtk_widget_get_colormap (widget);
438   attributes.event_mask = (gtk_widget_get_events (widget) |
439                            GDK_KEY_PRESS_MASK |
440                            GDK_ENTER_NOTIFY_MASK |
441                            GDK_LEAVE_NOTIFY_MASK |
442                            GDK_FOCUS_CHANGE_MASK |
443                            GDK_STRUCTURE_MASK);
444   attributes.type_hint = GDK_WINDOW_TYPE_HINT_TOOLBAR;
445   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP | GDK_WA_TYPE_HINT;
446   hb->float_window = gdk_window_new (gtk_widget_get_root_window (widget),
447                                      &attributes, attributes_mask);
448   gdk_window_set_user_data (hb->float_window, widget);
449   gdk_window_set_decorations (hb->float_window, 0);
450   gdk_window_set_type_hint (hb->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
451   
452   widget->style = gtk_style_attach (widget->style, widget->window);
453   gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (hb));
454   gtk_style_set_background (widget->style, hb->bin_window, GTK_WIDGET_STATE (hb));
455   gtk_style_set_background (widget->style, hb->float_window, GTK_WIDGET_STATE (hb));
456   gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
457 }
458
459 static void
460 gtk_handle_box_unrealize (GtkWidget *widget)
461 {
462   GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
463
464   gdk_window_set_user_data (hb->bin_window, NULL);
465   gdk_window_destroy (hb->bin_window);
466   hb->bin_window = NULL;
467   gdk_window_set_user_data (hb->float_window, NULL);
468   gdk_window_destroy (hb->float_window);
469   hb->float_window = NULL;
470
471   GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unrealize (widget);
472 }
473
474 static void
475 gtk_handle_box_style_set (GtkWidget *widget,
476                           GtkStyle  *previous_style)
477 {
478   GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
479
480   if (GTK_WIDGET_REALIZED (widget) &&
481       gtk_widget_get_has_window (widget))
482     {
483       gtk_style_set_background (widget->style, widget->window,
484                                 widget->state);
485       gtk_style_set_background (widget->style, hb->bin_window, widget->state);
486       gtk_style_set_background (widget->style, hb->float_window, widget->state);
487     }
488 }
489
490 static int
491 effective_handle_position (GtkHandleBox *hb)
492 {
493   int handle_position;
494
495   if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
496     handle_position = hb->handle_position;
497   else
498     {
499       switch (hb->handle_position) 
500         {
501         case GTK_POS_LEFT:
502           handle_position = GTK_POS_RIGHT;
503           break;
504         case GTK_POS_RIGHT:
505           handle_position = GTK_POS_LEFT;
506           break;
507         default:
508           handle_position = hb->handle_position;
509           break;
510         }
511     }
512
513   return handle_position;
514 }
515
516 static void
517 gtk_handle_box_size_request (GtkWidget      *widget,
518                              GtkRequisition *requisition)
519 {
520   GtkBin *bin;
521   GtkHandleBox *hb;
522   GtkRequisition child_requisition;
523   gint handle_position;
524
525   bin = GTK_BIN (widget);
526   hb = GTK_HANDLE_BOX (widget);
527
528   handle_position = effective_handle_position (hb);
529
530   if (handle_position == GTK_POS_LEFT ||
531       handle_position == GTK_POS_RIGHT)
532     {
533       requisition->width = DRAG_HANDLE_SIZE;
534       requisition->height = 0;
535     }
536   else
537     {
538       requisition->width = 0;
539       requisition->height = DRAG_HANDLE_SIZE;
540     }
541
542   /* if our child is not visible, we still request its size, since we
543    * won't have any useful hint for our size otherwise.
544    */
545   if (bin->child)
546     gtk_widget_size_request (bin->child, &child_requisition);
547   else
548     {
549       child_requisition.width = 0;
550       child_requisition.height = 0;
551     }      
552
553   if (hb->child_detached)
554     {
555       /* FIXME: This doesn't work currently */
556       if (!hb->shrink_on_detach)
557         {
558           if (handle_position == GTK_POS_LEFT ||
559               handle_position == GTK_POS_RIGHT)
560             requisition->height += child_requisition.height;
561           else
562             requisition->width += child_requisition.width;
563         }
564       else
565         {
566           if (handle_position == GTK_POS_LEFT ||
567               handle_position == GTK_POS_RIGHT)
568             requisition->height += widget->style->ythickness;
569           else
570             requisition->width += widget->style->xthickness;
571         }
572     }
573   else
574     {
575       requisition->width += GTK_CONTAINER (widget)->border_width * 2;
576       requisition->height += GTK_CONTAINER (widget)->border_width * 2;
577       
578       if (bin->child)
579         {
580           requisition->width += child_requisition.width;
581           requisition->height += child_requisition.height;
582         }
583       else
584         {
585           requisition->width += CHILDLESS_SIZE;
586           requisition->height += CHILDLESS_SIZE;
587         }
588     }
589 }
590
591 static void
592 gtk_handle_box_size_allocate (GtkWidget     *widget,
593                               GtkAllocation *allocation)
594 {
595   GtkBin *bin;
596   GtkHandleBox *hb;
597   GtkRequisition child_requisition;
598   gint handle_position;
599   
600   bin = GTK_BIN (widget);
601   hb = GTK_HANDLE_BOX (widget);
602   
603   handle_position = effective_handle_position (hb);
604
605   if (bin->child)
606     gtk_widget_get_child_requisition (bin->child, &child_requisition);
607   else
608     {
609       child_requisition.width = 0;
610       child_requisition.height = 0;
611     }      
612       
613   widget->allocation = *allocation;
614
615   if (GTK_WIDGET_REALIZED (hb))
616     gdk_window_move_resize (widget->window,
617                             widget->allocation.x,
618                             widget->allocation.y,
619                             widget->allocation.width,
620                             widget->allocation.height);
621
622
623   if (bin->child && gtk_widget_get_visible (bin->child))
624     {
625       GtkAllocation child_allocation;
626       guint border_width;
627
628       border_width = GTK_CONTAINER (widget)->border_width;
629
630       child_allocation.x = border_width;
631       child_allocation.y = border_width;
632       if (handle_position == GTK_POS_LEFT)
633         child_allocation.x += DRAG_HANDLE_SIZE;
634       else if (handle_position == GTK_POS_TOP)
635         child_allocation.y += DRAG_HANDLE_SIZE;
636
637       if (hb->child_detached)
638         {
639           guint float_width;
640           guint float_height;
641           
642           child_allocation.width = child_requisition.width;
643           child_allocation.height = child_requisition.height;
644           
645           float_width = child_allocation.width + 2 * border_width;
646           float_height = child_allocation.height + 2 * border_width;
647           
648           if (handle_position == GTK_POS_LEFT ||
649               handle_position == GTK_POS_RIGHT)
650             float_width += DRAG_HANDLE_SIZE;
651           else
652             float_height += DRAG_HANDLE_SIZE;
653
654           if (GTK_WIDGET_REALIZED (hb))
655             {
656               gdk_window_resize (hb->float_window,
657                                  float_width,
658                                  float_height);
659               gdk_window_move_resize (hb->bin_window,
660                                       0,
661                                       0,
662                                       float_width,
663                                       float_height);
664             }
665         }
666       else
667         {
668           child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
669           child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
670
671           if (handle_position == GTK_POS_LEFT ||
672               handle_position == GTK_POS_RIGHT)
673             child_allocation.width -= DRAG_HANDLE_SIZE;
674           else
675             child_allocation.height -= DRAG_HANDLE_SIZE;
676           
677           if (GTK_WIDGET_REALIZED (hb))
678             gdk_window_move_resize (hb->bin_window,
679                                     0,
680                                     0,
681                                     widget->allocation.width,
682                                     widget->allocation.height);
683         }
684
685       gtk_widget_size_allocate (bin->child, &child_allocation);
686     }
687 }
688
689 static void
690 gtk_handle_box_draw_ghost (GtkHandleBox *hb)
691 {
692   GtkWidget *widget;
693   guint x;
694   guint y;
695   guint width;
696   guint height;
697   gint handle_position;
698
699   widget = GTK_WIDGET (hb);
700   
701   handle_position = effective_handle_position (hb);
702   if (handle_position == GTK_POS_LEFT ||
703       handle_position == GTK_POS_RIGHT)
704     {
705       x = handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
706       y = 0;
707       width = DRAG_HANDLE_SIZE;
708       height = widget->allocation.height;
709     }
710   else
711     {
712       x = 0;
713       y = handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
714       width = widget->allocation.width;
715       height = DRAG_HANDLE_SIZE;
716     }
717   gtk_paint_shadow (widget->style,
718                     widget->window,
719                     GTK_WIDGET_STATE (widget),
720                     GTK_SHADOW_ETCHED_IN,
721                     NULL, widget, "handle",
722                     x,
723                     y,
724                     width,
725                     height);
726    if (handle_position == GTK_POS_LEFT ||
727        handle_position == GTK_POS_RIGHT)
728      gtk_paint_hline (widget->style,
729                       widget->window,
730                       GTK_WIDGET_STATE (widget),
731                       NULL, widget, "handlebox",
732                       handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
733                       handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
734                       widget->allocation.height / 2);
735    else
736      gtk_paint_vline (widget->style,
737                       widget->window,
738                       GTK_WIDGET_STATE (widget),
739                       NULL, widget, "handlebox",
740                       handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
741                       handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
742                       widget->allocation.width / 2);
743 }
744
745 static void
746 draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
747                      GdkRectangle *clip, GtkOrientation orientation)
748 {
749    gtk_paint_handle (widget->style, window, GTK_STATE_NORMAL, shadow,
750                      clip, widget, "handlebox",
751                      rect->x, rect->y, rect->width, rect->height, 
752                      orientation);
753 }
754
755 void
756 gtk_handle_box_set_shadow_type (GtkHandleBox  *handle_box,
757                                 GtkShadowType  type)
758 {
759   g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
760
761   if ((GtkShadowType) handle_box->shadow_type != type)
762     {
763       handle_box->shadow_type = type;
764       g_object_notify (G_OBJECT (handle_box), "shadow-type");
765       gtk_widget_queue_resize (GTK_WIDGET (handle_box));
766     }
767 }
768
769 /**
770  * gtk_handle_box_get_shadow_type:
771  * @handle_box: a #GtkHandleBox
772  * 
773  * Gets the type of shadow drawn around the handle box. See
774  * gtk_handle_box_set_shadow_type().
775  *
776  * Return value: the type of shadow currently drawn around the handle box.
777  **/
778 GtkShadowType
779 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
780 {
781   g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
782
783   return handle_box->shadow_type;
784 }
785
786 void        
787 gtk_handle_box_set_handle_position  (GtkHandleBox    *handle_box,
788                                      GtkPositionType  position)
789 {
790   g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
791
792   if ((GtkPositionType) handle_box->handle_position != position)
793     {
794       handle_box->handle_position = position;
795       g_object_notify (G_OBJECT (handle_box), "handle-position");
796       gtk_widget_queue_resize (GTK_WIDGET (handle_box));
797     }
798 }
799
800 /**
801  * gtk_handle_box_get_handle_position:
802  * @handle_box: a #GtkHandleBox
803  *
804  * Gets the handle position of the handle box. See
805  * gtk_handle_box_set_handle_position().
806  *
807  * Return value: the current handle position.
808  **/
809 GtkPositionType
810 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
811 {
812   g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
813
814   return handle_box->handle_position;
815 }
816
817 void        
818 gtk_handle_box_set_snap_edge        (GtkHandleBox    *handle_box,
819                                      GtkPositionType  edge)
820 {
821   g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
822
823   if (handle_box->snap_edge != edge)
824     {
825       handle_box->snap_edge = edge;
826       
827       g_object_freeze_notify (G_OBJECT (handle_box));
828       g_object_notify (G_OBJECT (handle_box), "snap-edge");
829       g_object_notify (G_OBJECT (handle_box), "snap-edge-set");
830       g_object_thaw_notify (G_OBJECT (handle_box));
831     }
832 }
833
834 /**
835  * gtk_handle_box_get_snap_edge:
836  * @handle_box: a #GtkHandleBox
837  * 
838  * Gets the edge used for determining reattachment of the handle box. See
839  * gtk_handle_box_set_snap_edge().
840  *
841  * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
842  *               is determined (as per default) from the handle position. 
843  **/
844 GtkPositionType
845 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
846 {
847   g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
848
849   return handle_box->snap_edge;
850 }
851
852 /**
853  * gtk_handle_box_get_child_detached:
854  * @handle_box: a #GtkHandleBox
855  *
856  * Whether the handlebox's child is currently detached.
857  *
858  * Return value: %TRUE if the child is currently detached, otherwise %FALSE
859  *
860  * Since: 2.14
861  **/
862 gboolean
863 gtk_handle_box_get_child_detached (GtkHandleBox *handle_box)
864 {
865   g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), FALSE);
866
867   return handle_box->child_detached;
868 }
869
870 static void
871 gtk_handle_box_paint (GtkWidget      *widget,
872                       GdkEventExpose *event,
873                       GdkRectangle   *area)
874 {
875   GtkBin *bin;
876   GtkHandleBox *hb;
877   gint width, height;
878   GdkRectangle rect;
879   GdkRectangle dest;
880   gint handle_position;
881   GtkOrientation handle_orientation;
882
883   bin = GTK_BIN (widget);
884   hb = GTK_HANDLE_BOX (widget);
885
886   handle_position = effective_handle_position (hb);
887
888   gdk_drawable_get_size (hb->bin_window, &width, &height);
889   
890   if (!event)
891     gtk_paint_box (widget->style,
892                    hb->bin_window,
893                    GTK_WIDGET_STATE (widget),
894                    hb->shadow_type,
895                    area, widget, "handlebox_bin",
896                    0, 0, -1, -1);
897   else
898    gtk_paint_box (widget->style,
899                   hb->bin_window,
900                   GTK_WIDGET_STATE (widget),
901                   hb->shadow_type,
902                   &event->area, widget, "handlebox_bin",
903                   0, 0, -1, -1);
904
905 /* We currently draw the handle _above_ the relief of the handlebox.
906  * it could also be drawn on the same level...
907
908                  hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
909                  hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
910                  width,
911                  height);*/
912
913   switch (handle_position)
914     {
915     case GTK_POS_LEFT:
916       rect.x = 0;
917       rect.y = 0; 
918       rect.width = DRAG_HANDLE_SIZE;
919       rect.height = height;
920       handle_orientation = GTK_ORIENTATION_VERTICAL;
921       break;
922     case GTK_POS_RIGHT:
923       rect.x = width - DRAG_HANDLE_SIZE; 
924       rect.y = 0;
925       rect.width = DRAG_HANDLE_SIZE;
926       rect.height = height;
927       handle_orientation = GTK_ORIENTATION_VERTICAL;
928       break;
929     case GTK_POS_TOP:
930       rect.x = 0;
931       rect.y = 0; 
932       rect.width = width;
933       rect.height = DRAG_HANDLE_SIZE;
934       handle_orientation = GTK_ORIENTATION_HORIZONTAL;
935       break;
936     case GTK_POS_BOTTOM:
937       rect.x = 0;
938       rect.y = height - DRAG_HANDLE_SIZE;
939       rect.width = width;
940       rect.height = DRAG_HANDLE_SIZE;
941       handle_orientation = GTK_ORIENTATION_HORIZONTAL;
942       break;
943     default: 
944       g_assert_not_reached ();
945       break;
946     }
947
948   if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
949     draw_textured_frame (widget, hb->bin_window, &rect,
950                          GTK_SHADOW_OUT,
951                          event ? &event->area : area,
952                          handle_orientation);
953
954   if (bin->child && gtk_widget_get_visible (bin->child))
955     GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->expose_event (widget, event);
956 }
957
958 static gboolean
959 gtk_handle_box_expose (GtkWidget      *widget,
960                        GdkEventExpose *event)
961 {
962   GtkHandleBox *hb;
963
964   if (gtk_widget_is_drawable (widget))
965     {
966       hb = GTK_HANDLE_BOX (widget);
967
968       if (event->window == widget->window)
969         {
970           if (hb->child_detached)
971             gtk_handle_box_draw_ghost (hb);
972         }
973       else
974         gtk_handle_box_paint (widget, event, NULL);
975     }
976   
977   return FALSE;
978 }
979
980 static GtkWidget *
981 gtk_handle_box_get_invisible (void)
982 {
983   static GtkWidget *handle_box_invisible = NULL;
984
985   if (!handle_box_invisible)
986     {
987       handle_box_invisible = gtk_invisible_new ();
988       gtk_widget_show (handle_box_invisible);
989     }
990   
991   return handle_box_invisible;
992 }
993
994 static gboolean
995 gtk_handle_box_grab_event (GtkWidget    *widget,
996                            GdkEvent     *event,
997                            GtkHandleBox *hb)
998 {
999   switch (event->type)
1000     {
1001     case GDK_BUTTON_RELEASE:
1002       if (hb->in_drag)          /* sanity check */
1003         {
1004           gtk_handle_box_end_drag (hb, event->button.time);
1005           return TRUE;
1006         }
1007       break;
1008
1009     case GDK_MOTION_NOTIFY:
1010       return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event);
1011       break;
1012
1013     default:
1014       break;
1015     }
1016
1017   return FALSE;
1018 }
1019
1020 static gboolean
1021 gtk_handle_box_button_press (GtkWidget      *widget,
1022                              GdkEventButton *event)
1023 {
1024   GtkHandleBox *hb;
1025   gboolean event_handled;
1026   GdkCursor *fleur;
1027   gint handle_position;
1028
1029   hb = GTK_HANDLE_BOX (widget);
1030
1031   handle_position = effective_handle_position (hb);
1032
1033   event_handled = FALSE;
1034   if ((event->button == 1) && 
1035       (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1036     {
1037       GtkWidget *child;
1038       gboolean in_handle;
1039       
1040       if (event->window != hb->bin_window)
1041         return FALSE;
1042
1043       child = GTK_BIN (hb)->child;
1044
1045       if (child)
1046         {
1047           switch (handle_position)
1048             {
1049             case GTK_POS_LEFT:
1050               in_handle = event->x < DRAG_HANDLE_SIZE;
1051               break;
1052             case GTK_POS_TOP:
1053               in_handle = event->y < DRAG_HANDLE_SIZE;
1054               break;
1055             case GTK_POS_RIGHT:
1056               in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width;
1057               break;
1058             case GTK_POS_BOTTOM:
1059               in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height;
1060               break;
1061             default:
1062               in_handle = FALSE;
1063               break;
1064             }
1065         }
1066       else
1067         {
1068           in_handle = FALSE;
1069           event_handled = TRUE;
1070         }
1071       
1072       if (in_handle)
1073         {
1074           if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
1075             {
1076               GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1077               GtkWidget *invisible = gtk_handle_box_get_invisible ();
1078               gint desk_x, desk_y;
1079               gint root_x, root_y;
1080               gint width, height;
1081
1082               gtk_invisible_set_screen (GTK_INVISIBLE (invisible),
1083                                         gtk_widget_get_screen (GTK_WIDGET (hb)));
1084               gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y);
1085               gdk_window_get_origin (hb->bin_window, &root_x, &root_y);
1086               gdk_drawable_get_size (hb->bin_window, &width, &height);
1087                   
1088               private->orig_x = event->x_root;
1089               private->orig_y = event->y_root;
1090                   
1091               hb->float_allocation.x = root_x - event->x_root;
1092               hb->float_allocation.y = root_y - event->y_root;
1093               hb->float_allocation.width = width;
1094               hb->float_allocation.height = height;
1095               
1096               hb->deskoff_x = desk_x - root_x;
1097               hb->deskoff_y = desk_y - root_y;
1098               
1099               if (gdk_window_is_viewable (widget->window))
1100                 {
1101                   gdk_window_get_origin (widget->window, &root_x, &root_y);
1102                   gdk_drawable_get_size (widget->window, &width, &height);
1103               
1104                   hb->attach_allocation.x = root_x;
1105                   hb->attach_allocation.y = root_y;
1106                   hb->attach_allocation.width = width;
1107                   hb->attach_allocation.height = height;
1108                 }
1109               else
1110                 {
1111                   hb->attach_allocation.x = -1;
1112                   hb->attach_allocation.y = -1;
1113                   hb->attach_allocation.width = 0;
1114                   hb->attach_allocation.height = 0;
1115                 }
1116               hb->in_drag = TRUE;
1117               fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1118                                                   GDK_FLEUR);
1119               if (gdk_pointer_grab (invisible->window,
1120                                     FALSE,
1121                                     (GDK_BUTTON1_MOTION_MASK |
1122                                      GDK_POINTER_MOTION_HINT_MASK |
1123                                      GDK_BUTTON_RELEASE_MASK),
1124                                     NULL,
1125                                     fleur,
1126                                     event->time) != 0)
1127                 {
1128                   hb->in_drag = FALSE;
1129                 }
1130               else
1131                 {
1132                   gtk_grab_add (invisible);
1133                   g_signal_connect (invisible, "event",
1134                                     G_CALLBACK (gtk_handle_box_grab_event), hb);
1135                 }
1136               
1137               gdk_cursor_unref (fleur);
1138               event_handled = TRUE;
1139             }
1140           else if (hb->child_detached) /* Double click */
1141             {
1142               gtk_handle_box_reattach (hb);
1143             }
1144         }
1145     }
1146   
1147   return event_handled;
1148 }
1149
1150 static gboolean
1151 gtk_handle_box_motion (GtkWidget      *widget,
1152                        GdkEventMotion *event)
1153 {
1154   GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1155   gint new_x, new_y;
1156   gint snap_edge;
1157   gboolean is_snapped = FALSE;
1158   gint handle_position;
1159   GdkGeometry geometry;
1160   GdkScreen *screen, *pointer_screen;
1161
1162   if (!hb->in_drag)
1163     return FALSE;
1164   handle_position = effective_handle_position (hb);
1165
1166   /* Calculate the attachment point on the float, if the float
1167    * were detached
1168    */
1169   new_x = 0;
1170   new_y = 0;
1171   screen = gtk_widget_get_screen (widget);
1172   gdk_display_get_pointer (gdk_screen_get_display (screen),
1173                            &pointer_screen, 
1174                            &new_x, &new_y, NULL);
1175   if (pointer_screen != screen)
1176     {
1177       GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1178
1179       new_x = private->orig_x;
1180       new_y = private->orig_y;
1181     }
1182   
1183   new_x += hb->float_allocation.x;
1184   new_y += hb->float_allocation.y;
1185
1186   snap_edge = hb->snap_edge;
1187   if (snap_edge == -1)
1188     snap_edge = (handle_position == GTK_POS_LEFT ||
1189                  handle_position == GTK_POS_RIGHT) ?
1190       GTK_POS_TOP : GTK_POS_LEFT;
1191
1192   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 
1193     switch (snap_edge) 
1194       {
1195       case GTK_POS_LEFT:
1196         snap_edge = GTK_POS_RIGHT;
1197         break;
1198       case GTK_POS_RIGHT:
1199         snap_edge = GTK_POS_LEFT;
1200         break;
1201       default:
1202         break;
1203       }
1204
1205   /* First, check if the snapped edge is aligned
1206    */
1207   switch (snap_edge)
1208     {
1209     case GTK_POS_TOP:
1210       is_snapped = abs (hb->attach_allocation.y - new_y) < TOLERANCE;
1211       break;
1212     case GTK_POS_BOTTOM:
1213       is_snapped = abs (hb->attach_allocation.y + (gint)hb->attach_allocation.height -
1214                         new_y - (gint)hb->float_allocation.height) < TOLERANCE;
1215       break;
1216     case GTK_POS_LEFT:
1217       is_snapped = abs (hb->attach_allocation.x - new_x) < TOLERANCE;
1218       break;
1219     case GTK_POS_RIGHT:
1220       is_snapped = abs (hb->attach_allocation.x + (gint)hb->attach_allocation.width -
1221                         new_x - (gint)hb->float_allocation.width) < TOLERANCE;
1222       break;
1223     }
1224
1225   /* Next, check if coordinates in the other direction are sufficiently
1226    * aligned
1227    */
1228   if (is_snapped)
1229     {
1230       gint float_pos1 = 0;      /* Initialize to suppress warnings */
1231       gint float_pos2 = 0;
1232       gint attach_pos1 = 0;
1233       gint attach_pos2 = 0;
1234       
1235       switch (snap_edge)
1236         {
1237         case GTK_POS_TOP:
1238         case GTK_POS_BOTTOM:
1239           attach_pos1 = hb->attach_allocation.x;
1240           attach_pos2 = hb->attach_allocation.x + hb->attach_allocation.width;
1241           float_pos1 = new_x;
1242           float_pos2 = new_x + hb->float_allocation.width;
1243           break;
1244         case GTK_POS_LEFT:
1245         case GTK_POS_RIGHT:
1246           attach_pos1 = hb->attach_allocation.y;
1247           attach_pos2 = hb->attach_allocation.y + hb->attach_allocation.height;
1248           float_pos1 = new_y;
1249           float_pos2 = new_y + hb->float_allocation.height;
1250           break;
1251         }
1252
1253       is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) && 
1254                     (attach_pos2 + TOLERANCE > float_pos2)) ||
1255                    ((float_pos1 - TOLERANCE < attach_pos1) &&
1256                     (float_pos2 + TOLERANCE > attach_pos2));
1257     }
1258
1259   if (is_snapped)
1260     {
1261       if (hb->child_detached)
1262         {
1263           hb->child_detached = FALSE;
1264           gdk_window_hide (hb->float_window);
1265           gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1266           hb->float_window_mapped = FALSE;
1267           g_signal_emit (hb,
1268                          handle_box_signals[SIGNAL_CHILD_ATTACHED],
1269                          0,
1270                          GTK_BIN (hb)->child);
1271           
1272           gtk_widget_queue_resize (widget);
1273         }
1274     }
1275   else
1276     {
1277       gint width, height;
1278
1279       gdk_drawable_get_size (hb->float_window, &width, &height);
1280       new_x += hb->deskoff_x;
1281       new_y += hb->deskoff_y;
1282
1283       switch (handle_position)
1284         {
1285         case GTK_POS_LEFT:
1286           new_y += ((gint)hb->float_allocation.height - height) / 2;
1287           break;
1288         case GTK_POS_RIGHT:
1289           new_x += (gint)hb->float_allocation.width - width;
1290           new_y += ((gint)hb->float_allocation.height - height) / 2;
1291           break;
1292         case GTK_POS_TOP:
1293           new_x += ((gint)hb->float_allocation.width - width) / 2;
1294           break;
1295         case GTK_POS_BOTTOM:
1296           new_x += ((gint)hb->float_allocation.width - width) / 2;
1297           new_y += (gint)hb->float_allocation.height - height;
1298           break;
1299         }
1300
1301       if (hb->child_detached)
1302         {
1303           gdk_window_move (hb->float_window, new_x, new_y);
1304           gdk_window_raise (hb->float_window);
1305         }
1306       else
1307         {
1308           gint width;
1309           gint height;
1310           GtkRequisition child_requisition;
1311
1312           hb->child_detached = TRUE;
1313
1314           if (GTK_BIN (hb)->child)
1315             gtk_widget_get_child_requisition (GTK_BIN (hb)->child, &child_requisition);
1316           else
1317             {
1318               child_requisition.width = 0;
1319               child_requisition.height = 0;
1320             }      
1321
1322           width = child_requisition.width + 2 * GTK_CONTAINER (hb)->border_width;
1323           height = child_requisition.height + 2 * GTK_CONTAINER (hb)->border_width;
1324
1325           if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT)
1326             width += DRAG_HANDLE_SIZE;
1327           else
1328             height += DRAG_HANDLE_SIZE;
1329           
1330           gdk_window_move_resize (hb->float_window, new_x, new_y, width, height);
1331           gdk_window_reparent (hb->bin_window, hb->float_window, 0, 0);
1332           gdk_window_set_geometry_hints (hb->float_window, &geometry, GDK_HINT_POS);
1333           gdk_window_show (hb->float_window);
1334           hb->float_window_mapped = TRUE;
1335 #if     0
1336           /* this extra move is necessary if we use decorations, or our
1337            * window manager insists on decorations.
1338            */
1339           gdk_display_sync (gtk_widget_get_display (widget));
1340           gdk_window_move (hb->float_window, new_x, new_y);
1341           gdk_display_sync (gtk_widget_get_display (widget));
1342 #endif  /* 0 */
1343           g_signal_emit (hb,
1344                          handle_box_signals[SIGNAL_CHILD_DETACHED],
1345                          0,
1346                          GTK_BIN (hb)->child);
1347           gtk_handle_box_draw_ghost (hb);
1348           
1349           gtk_widget_queue_resize (widget);
1350         }
1351     }
1352
1353   return TRUE;
1354 }
1355
1356 static void
1357 gtk_handle_box_add (GtkContainer *container,
1358                     GtkWidget    *widget)
1359 {
1360   gtk_widget_set_parent_window (widget, GTK_HANDLE_BOX (container)->bin_window);
1361   GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->add (container, widget);
1362 }
1363
1364 static void
1365 gtk_handle_box_remove (GtkContainer *container,
1366                        GtkWidget    *widget)
1367 {
1368   GTK_CONTAINER_CLASS (gtk_handle_box_parent_class)->remove (container, widget);
1369
1370   gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1371 }
1372
1373 static gint
1374 gtk_handle_box_delete_event (GtkWidget *widget,
1375                              GdkEventAny  *event)
1376 {
1377   GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1378
1379   if (event->window == hb->float_window)
1380     {
1381       gtk_handle_box_reattach (hb);
1382       
1383       return TRUE;
1384     }
1385
1386   return FALSE;
1387 }
1388
1389 static void
1390 gtk_handle_box_reattach (GtkHandleBox *hb)
1391 {
1392   GtkWidget *widget = GTK_WIDGET (hb);
1393   
1394   if (hb->child_detached)
1395     {
1396       hb->child_detached = FALSE;
1397       if (GTK_WIDGET_REALIZED (hb))
1398         {
1399           gdk_window_hide (hb->float_window);
1400           gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1401
1402           if (GTK_BIN (hb)->child)
1403             g_signal_emit (hb,
1404                            handle_box_signals[SIGNAL_CHILD_ATTACHED],
1405                            0,
1406                            GTK_BIN (hb)->child);
1407
1408         }
1409       hb->float_window_mapped = FALSE;
1410     }
1411   if (hb->in_drag)
1412     gtk_handle_box_end_drag (hb, GDK_CURRENT_TIME);
1413
1414   gtk_widget_queue_resize (GTK_WIDGET (hb));
1415 }
1416
1417 static void
1418 gtk_handle_box_end_drag (GtkHandleBox *hb,
1419                          guint32       time)
1420 {
1421   GtkWidget *invisible = gtk_handle_box_get_invisible ();
1422                 
1423   hb->in_drag = FALSE;
1424
1425   gtk_grab_remove (invisible);
1426   gdk_pointer_ungrab (time);
1427   g_signal_handlers_disconnect_by_func (invisible,
1428                                         G_CALLBACK (gtk_handle_box_grab_event),
1429                                         hb);
1430 }
1431
1432 #define __GTK_HANDLE_BOX_C__
1433 #include "gtkaliasdef.c"