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