]> Pileus Git - ~andy/gtk/blob - gtk/gtkhandlebox.c
consistently chain up using GTK_FOO_CLASS(parent_class)->bar(instance)
[~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 gint gtk_handle_box_expose         (GtkWidget         *widget,
135                                            GdkEventExpose    *event);
136 static gint gtk_handle_box_button_changed (GtkWidget         *widget,
137                                            GdkEventButton    *event);
138 static gint gtk_handle_box_motion         (GtkWidget         *widget,
139                                            GdkEventMotion    *event);
140 static gint gtk_handle_box_delete_event   (GtkWidget         *widget,
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_changed;
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_VISIBLE (bin->child) &&
358       !GTK_WIDGET_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   if (GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unrealize)
472     GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unrealize (widget);
473 }
474
475 static void
476 gtk_handle_box_style_set (GtkWidget *widget,
477                           GtkStyle  *previous_style)
478 {
479   GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
480
481   if (GTK_WIDGET_REALIZED (widget) &&
482       !GTK_WIDGET_NO_WINDOW (widget))
483     {
484       gtk_style_set_background (widget->style, widget->window,
485                                 widget->state);
486       gtk_style_set_background (widget->style, hb->bin_window, widget->state);
487       gtk_style_set_background (widget->style, hb->float_window, widget->state);
488     }
489 }
490
491 static int
492 effective_handle_position (GtkHandleBox *hb)
493 {
494   int handle_position;
495
496   if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
497     handle_position = hb->handle_position;
498   else
499     {
500       switch (hb->handle_position) 
501         {
502         case GTK_POS_LEFT:
503           handle_position = GTK_POS_RIGHT;
504           break;
505         case GTK_POS_RIGHT:
506           handle_position = GTK_POS_LEFT;
507           break;
508         default:
509           handle_position = hb->handle_position;
510           break;
511         }
512     }
513
514   return handle_position;
515 }
516
517 static void
518 gtk_handle_box_size_request (GtkWidget      *widget,
519                              GtkRequisition *requisition)
520 {
521   GtkBin *bin;
522   GtkHandleBox *hb;
523   GtkRequisition child_requisition;
524   gint handle_position;
525
526   bin = GTK_BIN (widget);
527   hb = GTK_HANDLE_BOX (widget);
528
529   handle_position = effective_handle_position (hb);
530
531   if (handle_position == GTK_POS_LEFT ||
532       handle_position == GTK_POS_RIGHT)
533     {
534       requisition->width = DRAG_HANDLE_SIZE;
535       requisition->height = 0;
536     }
537   else
538     {
539       requisition->width = 0;
540       requisition->height = DRAG_HANDLE_SIZE;
541     }
542
543   /* if our child is not visible, we still request its size, since we
544    * won't have any useful hint for our size otherwise.
545    */
546   if (bin->child)
547     gtk_widget_size_request (bin->child, &child_requisition);
548   else
549     {
550       child_requisition.width = 0;
551       child_requisition.height = 0;
552     }      
553
554   if (hb->child_detached)
555     {
556       /* FIXME: This doesn't work currently */
557       if (!hb->shrink_on_detach)
558         {
559           if (handle_position == GTK_POS_LEFT ||
560               handle_position == GTK_POS_RIGHT)
561             requisition->height += child_requisition.height;
562           else
563             requisition->width += child_requisition.width;
564         }
565       else
566         {
567           if (handle_position == GTK_POS_LEFT ||
568               handle_position == GTK_POS_RIGHT)
569             requisition->height += widget->style->ythickness;
570           else
571             requisition->width += widget->style->xthickness;
572         }
573     }
574   else
575     {
576       requisition->width += GTK_CONTAINER (widget)->border_width * 2;
577       requisition->height += GTK_CONTAINER (widget)->border_width * 2;
578       
579       if (bin->child)
580         {
581           requisition->width += child_requisition.width;
582           requisition->height += child_requisition.height;
583         }
584       else
585         {
586           requisition->width += CHILDLESS_SIZE;
587           requisition->height += CHILDLESS_SIZE;
588         }
589     }
590 }
591
592 static void
593 gtk_handle_box_size_allocate (GtkWidget     *widget,
594                               GtkAllocation *allocation)
595 {
596   GtkBin *bin;
597   GtkHandleBox *hb;
598   GtkRequisition child_requisition;
599   gint handle_position;
600   
601   bin = GTK_BIN (widget);
602   hb = GTK_HANDLE_BOX (widget);
603   
604   handle_position = effective_handle_position (hb);
605
606   if (bin->child)
607     gtk_widget_get_child_requisition (bin->child, &child_requisition);
608   else
609     {
610       child_requisition.width = 0;
611       child_requisition.height = 0;
612     }      
613       
614   widget->allocation = *allocation;
615
616   if (GTK_WIDGET_REALIZED (hb))
617     gdk_window_move_resize (widget->window,
618                             widget->allocation.x,
619                             widget->allocation.y,
620                             widget->allocation.width,
621                             widget->allocation.height);
622
623
624   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
625     {
626       GtkAllocation child_allocation;
627       guint border_width;
628
629       border_width = GTK_CONTAINER (widget)->border_width;
630
631       child_allocation.x = border_width;
632       child_allocation.y = border_width;
633       if (handle_position == GTK_POS_LEFT)
634         child_allocation.x += DRAG_HANDLE_SIZE;
635       else if (handle_position == GTK_POS_TOP)
636         child_allocation.y += DRAG_HANDLE_SIZE;
637
638       if (hb->child_detached)
639         {
640           guint float_width;
641           guint float_height;
642           
643           child_allocation.width = child_requisition.width;
644           child_allocation.height = child_requisition.height;
645           
646           float_width = child_allocation.width + 2 * border_width;
647           float_height = child_allocation.height + 2 * border_width;
648           
649           if (handle_position == GTK_POS_LEFT ||
650               handle_position == GTK_POS_RIGHT)
651             float_width += DRAG_HANDLE_SIZE;
652           else
653             float_height += DRAG_HANDLE_SIZE;
654
655           if (GTK_WIDGET_REALIZED (hb))
656             {
657               gdk_window_resize (hb->float_window,
658                                  float_width,
659                                  float_height);
660               gdk_window_move_resize (hb->bin_window,
661                                       0,
662                                       0,
663                                       float_width,
664                                       float_height);
665             }
666         }
667       else
668         {
669           child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
670           child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
671
672           if (handle_position == GTK_POS_LEFT ||
673               handle_position == GTK_POS_RIGHT)
674             child_allocation.width -= DRAG_HANDLE_SIZE;
675           else
676             child_allocation.height -= DRAG_HANDLE_SIZE;
677           
678           if (GTK_WIDGET_REALIZED (hb))
679             gdk_window_move_resize (hb->bin_window,
680                                     0,
681                                     0,
682                                     widget->allocation.width,
683                                     widget->allocation.height);
684         }
685
686       gtk_widget_size_allocate (bin->child, &child_allocation);
687     }
688 }
689
690 static void
691 gtk_handle_box_draw_ghost (GtkHandleBox *hb)
692 {
693   GtkWidget *widget;
694   guint x;
695   guint y;
696   guint width;
697   guint height;
698   gint handle_position;
699
700   widget = GTK_WIDGET (hb);
701   
702   handle_position = effective_handle_position (hb);
703   if (handle_position == GTK_POS_LEFT ||
704       handle_position == GTK_POS_RIGHT)
705     {
706       x = handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
707       y = 0;
708       width = DRAG_HANDLE_SIZE;
709       height = widget->allocation.height;
710     }
711   else
712     {
713       x = 0;
714       y = handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
715       width = widget->allocation.width;
716       height = DRAG_HANDLE_SIZE;
717     }
718   gtk_paint_shadow (widget->style,
719                     widget->window,
720                     GTK_WIDGET_STATE (widget),
721                     GTK_SHADOW_ETCHED_IN,
722                     NULL, widget, "handle",
723                     x,
724                     y,
725                     width,
726                     height);
727    if (handle_position == GTK_POS_LEFT ||
728        handle_position == GTK_POS_RIGHT)
729      gtk_paint_hline (widget->style,
730                       widget->window,
731                       GTK_WIDGET_STATE (widget),
732                       NULL, widget, "handlebox",
733                       handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
734                       handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
735                       widget->allocation.height / 2);
736    else
737      gtk_paint_vline (widget->style,
738                       widget->window,
739                       GTK_WIDGET_STATE (widget),
740                       NULL, widget, "handlebox",
741                       handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
742                       handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
743                       widget->allocation.width / 2);
744 }
745
746 static void
747 draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
748                      GdkRectangle *clip, GtkOrientation orientation)
749 {
750    gtk_paint_handle (widget->style, window, GTK_STATE_NORMAL, shadow,
751                      clip, widget, "handlebox",
752                      rect->x, rect->y, rect->width, rect->height, 
753                      orientation);
754 }
755
756 void
757 gtk_handle_box_set_shadow_type (GtkHandleBox  *handle_box,
758                                 GtkShadowType  type)
759 {
760   g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
761
762   if ((GtkShadowType) handle_box->shadow_type != type)
763     {
764       handle_box->shadow_type = type;
765       g_object_notify (G_OBJECT (handle_box), "shadow-type");
766       gtk_widget_queue_resize (GTK_WIDGET (handle_box));
767     }
768 }
769
770 /**
771  * gtk_handle_box_get_shadow_type:
772  * @handle_box: a #GtkHandleBox
773  * 
774  * Gets the type of shadow drawn around the handle box. See
775  * gtk_handle_box_set_shadow_type().
776  *
777  * Return value: the type of shadow currently drawn around the handle box.
778  **/
779 GtkShadowType
780 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
781 {
782   g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
783
784   return handle_box->shadow_type;
785 }
786
787 void        
788 gtk_handle_box_set_handle_position  (GtkHandleBox    *handle_box,
789                                      GtkPositionType  position)
790 {
791   g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
792
793   if ((GtkPositionType) handle_box->handle_position != position)
794     {
795       handle_box->handle_position = position;
796       g_object_notify (G_OBJECT (handle_box), "handle-position");
797       gtk_widget_queue_resize (GTK_WIDGET (handle_box));
798     }
799 }
800
801 /**
802  * gtk_handle_box_get_handle_position:
803  * @handle_box: a #GtkHandleBox
804  *
805  * Gets the handle position of the handle box. See
806  * gtk_handle_box_set_handle_position().
807  *
808  * Return value: the current handle position.
809  **/
810 GtkPositionType
811 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
812 {
813   g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
814
815   return handle_box->handle_position;
816 }
817
818 void        
819 gtk_handle_box_set_snap_edge        (GtkHandleBox    *handle_box,
820                                      GtkPositionType  edge)
821 {
822   g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
823
824   if (handle_box->snap_edge != edge)
825     {
826       handle_box->snap_edge = edge;
827       
828       g_object_freeze_notify (G_OBJECT (handle_box));
829       g_object_notify (G_OBJECT (handle_box), "snap-edge");
830       g_object_notify (G_OBJECT (handle_box), "snap-edge-set");
831       g_object_thaw_notify (G_OBJECT (handle_box));
832     }
833 }
834
835 /**
836  * gtk_handle_box_get_snap_edge:
837  * @handle_box: a #GtkHandleBox
838  * 
839  * Gets the edge used for determining reattachment of the handle box. See
840  * gtk_handle_box_set_snap_edge().
841  *
842  * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
843  *               is determined (as per default) from the handle position. 
844  **/
845 GtkPositionType
846 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
847 {
848   g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
849
850   return handle_box->snap_edge;
851 }
852
853 /**
854  * gtk_handle_box_get_child_detached:
855  * @handle_box: a #GtkHandleBox
856  *
857  * Whether the handlebox's child is currently detached.
858  *
859  * Return value: %TRUE if the child is currently detached, otherwise %FALSE
860  *
861  * Since: 2.14
862  **/
863 gboolean
864 gtk_handle_box_get_child_detached (GtkHandleBox *handle_box)
865 {
866   g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), FALSE);
867
868   return handle_box->child_detached;
869 }
870
871 static void
872 gtk_handle_box_paint (GtkWidget      *widget,
873                   
874     GdkEventExpose *event,
875                       GdkRectangle   *area)
876 {
877   GtkBin *bin;
878   GtkHandleBox *hb;
879   gint width, height;
880   GdkRectangle rect;
881   GdkRectangle dest;
882   gint handle_position;
883   GtkOrientation handle_orientation;
884
885   bin = GTK_BIN (widget);
886   hb = GTK_HANDLE_BOX (widget);
887
888   handle_position = effective_handle_position (hb);
889
890   gdk_drawable_get_size (hb->bin_window, &width, &height);
891   
892   if (!event)
893     gtk_paint_box (widget->style,
894                    hb->bin_window,
895                    GTK_WIDGET_STATE (widget),
896                    hb->shadow_type,
897                    area, widget, "handlebox_bin",
898                    0, 0, -1, -1);
899   else
900    gtk_paint_box (widget->style,
901                   hb->bin_window,
902                   GTK_WIDGET_STATE (widget),
903                   hb->shadow_type,
904                   &event->area, widget, "handlebox_bin",
905                   0, 0, -1, -1);
906
907 /* We currently draw the handle _above_ the relief of the handlebox.
908  * it could also be drawn on the same level...
909
910                  hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
911                  hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
912                  width,
913                  height);*/
914
915   switch (handle_position)
916     {
917     case GTK_POS_LEFT:
918       rect.x = 0;
919       rect.y = 0; 
920       rect.width = DRAG_HANDLE_SIZE;
921       rect.height = height;
922       handle_orientation = GTK_ORIENTATION_VERTICAL;
923       break;
924     case GTK_POS_RIGHT:
925       rect.x = width - DRAG_HANDLE_SIZE; 
926       rect.y = 0;
927       rect.width = DRAG_HANDLE_SIZE;
928       rect.height = height;
929       handle_orientation = GTK_ORIENTATION_VERTICAL;
930       break;
931     case GTK_POS_TOP:
932       rect.x = 0;
933       rect.y = 0; 
934       rect.width = width;
935       rect.height = DRAG_HANDLE_SIZE;
936       handle_orientation = GTK_ORIENTATION_HORIZONTAL;
937       break;
938     case GTK_POS_BOTTOM:
939       rect.x = 0;
940       rect.y = height - DRAG_HANDLE_SIZE;
941       rect.width = width;
942       rect.height = DRAG_HANDLE_SIZE;
943       handle_orientation = GTK_ORIENTATION_HORIZONTAL;
944       break;
945     default: 
946       g_assert_not_reached ();
947       break;
948     }
949
950   if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
951     draw_textured_frame (widget, hb->bin_window, &rect,
952                          GTK_SHADOW_OUT,
953                          event ? &event->area : area,
954                          handle_orientation);
955
956   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
957     GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->expose_event (widget, event);
958 }
959
960 static gint
961 gtk_handle_box_expose (GtkWidget      *widget,
962                        GdkEventExpose *event)
963 {
964   GtkHandleBox *hb;
965
966   if (GTK_WIDGET_DRAWABLE (widget))
967     {
968       hb = GTK_HANDLE_BOX (widget);
969
970       if (event->window == widget->window)
971         {
972           if (hb->child_detached)
973             gtk_handle_box_draw_ghost (hb);
974         }
975       else
976         gtk_handle_box_paint (widget, event, NULL);
977     }
978   
979   return FALSE;
980 }
981
982 static GtkWidget *
983 gtk_handle_box_get_invisible (void)
984 {
985   static GtkWidget *handle_box_invisible = NULL;
986
987   if (!handle_box_invisible)
988     {
989       handle_box_invisible = gtk_invisible_new ();
990       gtk_widget_show (handle_box_invisible);
991     }
992   
993   return handle_box_invisible;
994 }
995
996 static gboolean
997 gtk_handle_box_grab_event (GtkWidget    *widget,
998                            GdkEvent     *event,
999                            GtkHandleBox *hb)
1000 {
1001   switch (event->type)
1002     {
1003     case GDK_BUTTON_RELEASE:
1004       if (hb->in_drag)          /* sanity check */
1005         {
1006           gtk_handle_box_end_drag (hb, event->button.time);
1007           return TRUE;
1008         }
1009       break;
1010
1011     case GDK_MOTION_NOTIFY:
1012       return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event);
1013       break;
1014
1015     default:
1016       break;
1017     }
1018
1019   return FALSE;
1020 }
1021
1022 static gint
1023 gtk_handle_box_button_changed (GtkWidget      *widget,
1024                                GdkEventButton *event)
1025 {
1026   GtkHandleBox *hb;
1027   gboolean event_handled;
1028   GdkCursor *fleur;
1029   gint handle_position;
1030
1031   hb = GTK_HANDLE_BOX (widget);
1032
1033   handle_position = effective_handle_position (hb);
1034
1035   event_handled = FALSE;
1036   if ((event->button == 1) && 
1037       (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1038     {
1039       GtkWidget *child;
1040       gboolean in_handle;
1041       
1042       if (event->window != hb->bin_window)
1043         return FALSE;
1044
1045       child = GTK_BIN (hb)->child;
1046
1047       if (child)
1048         {
1049           switch (handle_position)
1050             {
1051             case GTK_POS_LEFT:
1052               in_handle = event->x < DRAG_HANDLE_SIZE;
1053               break;
1054             case GTK_POS_TOP:
1055               in_handle = event->y < DRAG_HANDLE_SIZE;
1056               break;
1057             case GTK_POS_RIGHT:
1058               in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width;
1059               break;
1060             case GTK_POS_BOTTOM:
1061               in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height;
1062               break;
1063             default:
1064               in_handle = FALSE;
1065               break;
1066             }
1067         }
1068       else
1069         {
1070           in_handle = FALSE;
1071           event_handled = TRUE;
1072         }
1073       
1074       if (in_handle)
1075         {
1076           if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
1077             {
1078               GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1079               GtkWidget *invisible = gtk_handle_box_get_invisible ();
1080               gint desk_x, desk_y;
1081               gint root_x, root_y;
1082               gint width, height;
1083
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 gint
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"