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