]> Pileus Git - ~andy/gtk/blob - gtk/gtkhandlebox.c
remove unused variable.
[~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   if (GTK_OBJECT_CLASS (parent_class)->destroy)
332     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
333 }
334
335 static void
336 gtk_handle_box_map (GtkWidget *widget)
337 {
338   GtkBin *bin;
339   GtkHandleBox *hb;
340
341   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
342
343   bin = GTK_BIN (widget);
344   hb = GTK_HANDLE_BOX (widget);
345
346   if (bin->child &&
347       GTK_WIDGET_VISIBLE (bin->child) &&
348       !GTK_WIDGET_MAPPED (bin->child))
349     gtk_widget_map (bin->child);
350
351   if (hb->child_detached && !hb->float_window_mapped)
352     {
353       gdk_window_show (hb->float_window);
354       hb->float_window_mapped = TRUE;
355     }
356
357   gdk_window_show (hb->bin_window);
358   gdk_window_show (widget->window);
359 }
360
361 static void
362 gtk_handle_box_unmap (GtkWidget *widget)
363 {
364   GtkHandleBox *hb;
365
366   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
367
368   hb = GTK_HANDLE_BOX (widget);
369
370   gdk_window_hide (widget->window);
371   if (hb->float_window_mapped)
372     {
373       gdk_window_hide (hb->float_window);
374       hb->float_window_mapped = FALSE;
375     }
376 }
377
378 static void
379 gtk_handle_box_realize (GtkWidget *widget)
380 {
381   GdkWindowAttr attributes;
382   gint attributes_mask;
383   GtkHandleBox *hb;
384
385   hb = GTK_HANDLE_BOX (widget);
386
387   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
388
389   attributes.x = widget->allocation.x;
390   attributes.y = widget->allocation.y;
391   attributes.width = widget->allocation.width;
392   attributes.height = widget->allocation.height;
393   attributes.window_type = GDK_WINDOW_CHILD;
394   attributes.wclass = GDK_INPUT_OUTPUT;
395   attributes.visual = gtk_widget_get_visual (widget);
396   attributes.colormap = gtk_widget_get_colormap (widget);
397   attributes.event_mask = (gtk_widget_get_events (widget)
398                            | GDK_EXPOSURE_MASK);
399   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
400   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
401   gdk_window_set_user_data (widget->window, widget);
402
403   attributes.x = 0;
404   attributes.y = 0;
405   attributes.width = widget->allocation.width;
406   attributes.height = widget->allocation.height;
407   attributes.window_type = GDK_WINDOW_CHILD;
408   attributes.event_mask |= (gtk_widget_get_events (widget) |
409                             GDK_EXPOSURE_MASK |
410                             GDK_BUTTON1_MOTION_MASK |
411                             GDK_POINTER_MOTION_HINT_MASK |
412                             GDK_BUTTON_PRESS_MASK |
413                             GDK_BUTTON_RELEASE_MASK);
414   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
415   hb->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask);
416   gdk_window_set_user_data (hb->bin_window, widget);
417   if (GTK_BIN (hb)->child)
418     gtk_widget_set_parent_window (GTK_BIN (hb)->child, hb->bin_window);
419   
420   attributes.x = 0;
421   attributes.y = 0;
422   attributes.width = widget->requisition.width;
423   attributes.height = widget->requisition.height;
424   attributes.window_type = GDK_WINDOW_TOPLEVEL;
425   attributes.wclass = GDK_INPUT_OUTPUT;
426   attributes.visual = gtk_widget_get_visual (widget);
427   attributes.colormap = gtk_widget_get_colormap (widget);
428   attributes.event_mask = (gtk_widget_get_events (widget) |
429                            GDK_KEY_PRESS_MASK |
430                            GDK_ENTER_NOTIFY_MASK |
431                            GDK_LEAVE_NOTIFY_MASK |
432                            GDK_FOCUS_CHANGE_MASK |
433                            GDK_STRUCTURE_MASK);
434   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
435   hb->float_window = gdk_window_new (NULL, &attributes, attributes_mask);
436   gdk_window_set_user_data (hb->float_window, widget);
437   gdk_window_set_decorations (hb->float_window, 0);
438   gdk_window_set_type_hint (hb->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
439   
440   widget->style = gtk_style_attach (widget->style, widget->window);
441   gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (hb));
442   gtk_style_set_background (widget->style, hb->bin_window, GTK_WIDGET_STATE (hb));
443   gtk_style_set_background (widget->style, hb->float_window, GTK_WIDGET_STATE (hb));
444   gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
445 }
446
447 static void
448 gtk_handle_box_unrealize (GtkWidget *widget)
449 {
450   GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
451
452   gdk_window_set_user_data (hb->bin_window, NULL);
453   gdk_window_destroy (hb->bin_window);
454   hb->bin_window = NULL;
455   gdk_window_set_user_data (hb->float_window, NULL);
456   gdk_window_destroy (hb->float_window);
457   hb->float_window = NULL;
458
459   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
460     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
461 }
462
463 static void
464 gtk_handle_box_style_set (GtkWidget *widget,
465                           GtkStyle  *previous_style)
466 {
467   GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
468
469   if (GTK_WIDGET_REALIZED (widget) &&
470       !GTK_WIDGET_NO_WINDOW (widget))
471     {
472       gtk_style_set_background (widget->style, widget->window,
473 widget->state);
474       gtk_style_set_background (widget->style, hb->bin_window, widget->state);
475       gtk_style_set_background (widget->style, hb->float_window, widget->state);
476     }
477 }
478
479 static void
480 gtk_handle_box_size_request (GtkWidget      *widget,
481                              GtkRequisition *requisition)
482 {
483   GtkBin *bin;
484   GtkHandleBox *hb;
485   GtkRequisition child_requisition;
486
487   bin = GTK_BIN (widget);
488   hb = GTK_HANDLE_BOX (widget);
489
490   if (hb->handle_position == GTK_POS_LEFT ||
491       hb->handle_position == GTK_POS_RIGHT)
492     {
493       requisition->width = DRAG_HANDLE_SIZE;
494       requisition->height = 0;
495     }
496   else
497     {
498       requisition->width = 0;
499       requisition->height = DRAG_HANDLE_SIZE;
500     }
501
502   /* if our child is not visible, we still request its size, since we
503    * won't have any usefull hint for our size otherwise.
504    */
505   if (bin->child)
506     gtk_widget_size_request (bin->child, &child_requisition);
507   else
508     {
509       child_requisition.width = 0;
510       child_requisition.height = 0;
511     }      
512
513   if (hb->child_detached)
514     {
515       /* FIXME: This doesn't work currently */
516       if (!hb->shrink_on_detach)
517         {
518           if (hb->handle_position == GTK_POS_LEFT ||
519               hb->handle_position == GTK_POS_RIGHT)
520             requisition->height += child_requisition.height;
521           else
522             requisition->width += child_requisition.width;
523         }
524       else
525         {
526           if (hb->handle_position == GTK_POS_LEFT ||
527               hb->handle_position == GTK_POS_RIGHT)
528             requisition->height += widget->style->ythickness;
529           else
530             requisition->width += widget->style->xthickness;
531         }
532     }
533   else
534     {
535       requisition->width += GTK_CONTAINER (widget)->border_width * 2;
536       requisition->height += GTK_CONTAINER (widget)->border_width * 2;
537       
538       if (bin->child)
539         {
540           requisition->width += child_requisition.width;
541           requisition->height += child_requisition.height;
542         }
543       else
544         {
545           requisition->width += CHILDLESS_SIZE;
546           requisition->height += CHILDLESS_SIZE;
547         }
548     }
549 }
550
551 static void
552 gtk_handle_box_size_allocate (GtkWidget     *widget,
553                               GtkAllocation *allocation)
554 {
555   GtkBin *bin;
556   GtkHandleBox *hb;
557   GtkRequisition child_requisition;
558   
559   bin = GTK_BIN (widget);
560   hb = GTK_HANDLE_BOX (widget);
561   
562   if (bin->child)
563     gtk_widget_get_child_requisition (bin->child, &child_requisition);
564   else
565     {
566       child_requisition.width = 0;
567       child_requisition.height = 0;
568     }      
569       
570   widget->allocation = *allocation;
571
572   if (GTK_WIDGET_REALIZED (hb))
573     gdk_window_move_resize (widget->window,
574                             widget->allocation.x,
575                             widget->allocation.y,
576                             widget->allocation.width,
577                             widget->allocation.height);
578
579
580   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
581     {
582       GtkWidget *child;
583       GtkAllocation child_allocation;
584       guint border_width;
585
586       child = bin->child;
587       border_width = GTK_CONTAINER (widget)->border_width;
588
589       child_allocation.x = border_width;
590       child_allocation.y = border_width;
591       if (hb->handle_position == GTK_POS_LEFT)
592         child_allocation.x += DRAG_HANDLE_SIZE;
593       else if (hb->handle_position == GTK_POS_TOP)
594         child_allocation.y += DRAG_HANDLE_SIZE;
595
596       if (hb->child_detached)
597         {
598           guint float_width;
599           guint float_height;
600           
601           child_allocation.width = child_requisition.width;
602           child_allocation.height = child_requisition.height;
603           
604           float_width = child_allocation.width + 2 * border_width;
605           float_height = child_allocation.height + 2 * border_width;
606           
607           if (hb->handle_position == GTK_POS_LEFT ||
608               hb->handle_position == GTK_POS_RIGHT)
609             float_width += DRAG_HANDLE_SIZE;
610           else
611             float_height += DRAG_HANDLE_SIZE;
612
613           if (GTK_WIDGET_REALIZED (hb))
614             {
615               gdk_window_resize (hb->float_window,
616                                  float_width,
617                                  float_height);
618               gdk_window_move_resize (hb->bin_window,
619                                       0,
620                                       0,
621                                       float_width,
622                                       float_height);
623             }
624         }
625       else
626         {
627           child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
628           child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
629
630           if (hb->handle_position == GTK_POS_LEFT ||
631               hb->handle_position == GTK_POS_RIGHT)
632             child_allocation.width -= DRAG_HANDLE_SIZE;
633           else
634             child_allocation.height -= DRAG_HANDLE_SIZE;
635           
636           if (GTK_WIDGET_REALIZED (hb))
637             gdk_window_move_resize (hb->bin_window,
638                                     0,
639                                     0,
640                                     widget->allocation.width,
641                                     widget->allocation.height);
642         }
643
644       gtk_widget_size_allocate (bin->child, &child_allocation);
645     }
646 }
647
648 static void
649 gtk_handle_box_draw_ghost (GtkHandleBox *hb)
650 {
651   GtkWidget *widget;
652   guint x;
653   guint y;
654   guint width;
655   guint height;
656
657   widget = GTK_WIDGET (hb);
658
659   if (hb->handle_position == GTK_POS_LEFT ||
660       hb->handle_position == GTK_POS_RIGHT)
661     {
662       x = hb->handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
663       y = 0;
664       width = DRAG_HANDLE_SIZE;
665       height = widget->allocation.height;
666     }
667   else
668     {
669       x = 0;
670       y = hb->handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
671       width = widget->allocation.width;
672       height = DRAG_HANDLE_SIZE;
673     }
674   gtk_paint_shadow (widget->style,
675                     widget->window,
676                     GTK_WIDGET_STATE (widget),
677                     GTK_SHADOW_ETCHED_IN,
678                     NULL, widget, "handle",
679                     x,
680                     y,
681                     width,
682                     height);
683    if (hb->handle_position == GTK_POS_LEFT ||
684        hb->handle_position == GTK_POS_RIGHT)
685      gtk_paint_hline (widget->style,
686                       widget->window,
687                       GTK_WIDGET_STATE (widget),
688                       NULL, widget, "handlebox",
689                       hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
690                       hb->handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
691                       widget->allocation.height / 2);
692    else
693      gtk_paint_vline (widget->style,
694                       widget->window,
695                       GTK_WIDGET_STATE (widget),
696                       NULL, widget, "handlebox",
697                       hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
698                       hb->handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
699                       widget->allocation.width / 2);
700 }
701
702 static void
703 draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
704                      GdkRectangle *clip)
705 {
706    gtk_paint_handle (widget->style, window, GTK_STATE_NORMAL, shadow,
707                      clip, widget, "handlebox",
708                      rect->x, rect->y, rect->width, rect->height, 
709                      GTK_ORIENTATION_VERTICAL);
710 }
711
712 void
713 gtk_handle_box_set_shadow_type (GtkHandleBox  *handle_box,
714                                 GtkShadowType  type)
715 {
716   g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
717
718   if ((GtkShadowType) handle_box->shadow_type != type)
719     {
720       handle_box->shadow_type = type;
721       g_object_notify (G_OBJECT (handle_box), "shadow_type");
722       gtk_widget_queue_resize (GTK_WIDGET (handle_box));
723     }
724 }
725
726 /**
727  * gtk_handle_box_get_shadow_type:
728  * @handle_box: a #GtkHandleBox
729  * 
730  * Gets the type of shadow drawn around the handle box. See
731  * gtk_handle_box_set_shadow_type().
732  *
733  * Return value: the type of shadow currently drawn around the handle box.
734  **/
735 GtkShadowType
736 gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
737 {
738   g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);
739
740   return handle_box->shadow_type;
741 }
742
743 void        
744 gtk_handle_box_set_handle_position  (GtkHandleBox    *handle_box,
745                                      GtkPositionType  position)
746 {
747   g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
748
749   if ((GtkPositionType) handle_box->handle_position != position)
750     {
751       handle_box->handle_position = position;
752       g_object_notify (G_OBJECT (handle_box), "handle_position");
753       gtk_widget_queue_resize (GTK_WIDGET (handle_box));
754     }
755 }
756
757 /**
758  * gtk_handle_box_get_handle_position:
759  * @handle_box: a #GtkHandleBox
760  *
761  * Gets the handle position of the handle box. See
762  * gtk_handle_box_set_handle_position().
763  *
764  * Return value: the current handle position.
765  **/
766 GtkPositionType
767 gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
768 {
769   g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);
770
771   return handle_box->handle_position;
772 }
773
774 void        
775 gtk_handle_box_set_snap_edge        (GtkHandleBox    *handle_box,
776                                      GtkPositionType  edge)
777 {
778   g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));
779
780   if (handle_box->snap_edge != edge)
781     {
782       handle_box->snap_edge = edge;
783       g_object_notify (G_OBJECT (handle_box), "snap_edge");
784     }
785 }
786
787 /**
788  * gtk_handle_box_get_snap_edge:
789  * @handle_box: a #GtkHandleBox
790  * 
791  * Gets the edge used for determining reattachment of the handle box. See
792  * gtk_handle_box_set_snap_edge().
793  *
794  * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
795  *               is determined (as per default) from the handle position. 
796  **/
797 GtkPositionType
798 gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
799 {
800   g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);
801
802   return handle_box->snap_edge;
803 }
804
805 static void
806 gtk_handle_box_paint (GtkWidget      *widget,
807                       GdkEventExpose *event,
808                       GdkRectangle   *area)
809 {
810   GtkBin *bin;
811   GtkHandleBox *hb;
812   guint width;
813   guint height;
814   GdkRectangle rect;
815   GdkRectangle dest;
816
817   bin = GTK_BIN (widget);
818   hb = GTK_HANDLE_BOX (widget);
819
820   gdk_window_get_size (hb->bin_window, &width, &height);
821   
822   if (!event)
823     gtk_paint_box (widget->style,
824                    hb->bin_window,
825                    GTK_WIDGET_STATE (widget),
826                    hb->shadow_type,
827                    area, widget, "handlebox_bin",
828                    0, 0, -1, -1);
829   else
830    gtk_paint_box (widget->style,
831                   hb->bin_window,
832                   GTK_WIDGET_STATE (widget),
833                   hb->shadow_type,
834                   &event->area, widget, "handlebox_bin",
835                   0, 0, -1, -1);
836
837 /* We currently draw the handle _above_ the relief of the handlebox.
838  * it could also be drawn on the same level...
839
840                  hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
841                  hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
842                  width,
843                  height);*/
844
845   switch (hb->handle_position)
846     {
847     case GTK_POS_LEFT:
848       rect.x = 0;
849       rect.y = 0; 
850       rect.width = DRAG_HANDLE_SIZE;
851       rect.height = height;
852       break;
853     case GTK_POS_RIGHT:
854       rect.x = width - DRAG_HANDLE_SIZE; 
855       rect.y = 0;
856       rect.width = DRAG_HANDLE_SIZE;
857       rect.height = height;
858       break;
859     case GTK_POS_TOP:
860       rect.x = 0;
861       rect.y = 0; 
862       rect.width = width;
863       rect.height = DRAG_HANDLE_SIZE;
864       break;
865     case GTK_POS_BOTTOM:
866       rect.x = 0;
867       rect.y = height - DRAG_HANDLE_SIZE;
868       rect.width = width;
869       rect.height = DRAG_HANDLE_SIZE;
870       break;
871     }
872
873   if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
874     draw_textured_frame (widget, hb->bin_window, &rect,
875                          GTK_SHADOW_OUT,
876                          event ? &event->area : area);
877
878   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
879     {
880       GdkRectangle child_area;
881
882       if (!event) /* we were called from draw() */
883         {
884           if (gtk_widget_intersect (bin->child, area, &child_area))
885             gtk_widget_draw (bin->child, &child_area);
886         }
887       else /* we were called from expose() */
888         (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
889     }
890 }
891
892 static gint
893 gtk_handle_box_expose (GtkWidget      *widget,
894                        GdkEventExpose *event)
895 {
896   GtkHandleBox *hb;
897
898   if (GTK_WIDGET_DRAWABLE (widget))
899     {
900       hb = GTK_HANDLE_BOX (widget);
901
902       if (event->window == widget->window)
903         {
904           if (hb->child_detached)
905             gtk_handle_box_draw_ghost (hb);
906         }
907       else
908         gtk_handle_box_paint (widget, event, NULL);
909     }
910   
911   return FALSE;
912 }
913
914 static gint
915 gtk_handle_box_button_changed (GtkWidget      *widget,
916                                GdkEventButton *event)
917 {
918   GtkHandleBox *hb;
919   gboolean event_handled;
920   GdkCursor *fleur;
921
922   hb = GTK_HANDLE_BOX (widget);
923
924   event_handled = FALSE;
925   if ((event->button == 1) && 
926       (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
927     {
928       GtkWidget *child;
929       gboolean in_handle;
930       
931       if (event->window != hb->bin_window)
932         return FALSE;
933
934       child = GTK_BIN (hb)->child;
935
936       if (child)
937         {
938           switch (hb->handle_position)
939             {
940             case GTK_POS_LEFT:
941               in_handle = event->x < DRAG_HANDLE_SIZE;
942               break;
943             case GTK_POS_TOP:
944               in_handle = event->y < DRAG_HANDLE_SIZE;
945               break;
946             case GTK_POS_RIGHT:
947               in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width;
948               break;
949             case GTK_POS_BOTTOM:
950               in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height;
951               break;
952             default:
953               in_handle = FALSE;
954               break;
955             }
956         }
957       else
958         {
959           in_handle = FALSE;
960           event_handled = TRUE;
961         }
962       
963       if (in_handle)
964         {
965           if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
966             {
967               gint desk_x, desk_y;
968               gint root_x, root_y;
969               gint width, height;
970               
971               gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y);
972               gdk_window_get_origin (hb->bin_window, &root_x, &root_y);
973               gdk_window_get_size (hb->bin_window, &width, &height);
974               
975               hb->float_allocation.x = root_x - event->x_root;
976               hb->float_allocation.y = root_y - event->y_root;
977               hb->float_allocation.width = width;
978               hb->float_allocation.height = height;
979               
980               hb->deskoff_x = desk_x - root_x;
981               hb->deskoff_y = desk_y - root_y;
982               
983               gdk_window_get_origin (widget->window, &root_x, &root_y);
984               gdk_window_get_size (widget->window, &width, &height);
985               
986               hb->attach_allocation.x = root_x;
987               hb->attach_allocation.y = root_y;
988               hb->attach_allocation.width = width;
989               hb->attach_allocation.height = height;
990
991               hb->in_drag = TRUE;
992               fleur = gdk_cursor_new (GDK_FLEUR);
993               if (gdk_pointer_grab (widget->window,
994                                     FALSE,
995                                     (GDK_BUTTON1_MOTION_MASK |
996                                      GDK_POINTER_MOTION_HINT_MASK |
997                                      GDK_BUTTON_RELEASE_MASK),
998                                     NULL,
999                                     fleur,
1000                                     GDK_CURRENT_TIME) != 0)
1001                 {
1002                   hb->in_drag = FALSE;
1003                 }
1004               
1005               gdk_cursor_destroy (fleur);
1006               event_handled = TRUE;
1007             }
1008           else if (hb->child_detached) /* Double click */
1009             {
1010               gtk_handle_box_reattach (hb);
1011             }
1012         }
1013     }
1014   else if (event->type == GDK_BUTTON_RELEASE &&
1015            hb->in_drag)
1016     {
1017       if (event->window != widget->window)
1018         return FALSE;
1019       
1020       gdk_pointer_ungrab (GDK_CURRENT_TIME);
1021       hb->in_drag = FALSE;
1022       event_handled = TRUE;
1023     }
1024   
1025   return event_handled;
1026 }
1027
1028 static gint
1029 gtk_handle_box_motion (GtkWidget      *widget,
1030                        GdkEventMotion *event)
1031 {
1032   GtkHandleBox *hb;
1033   gint new_x, new_y;
1034   gint snap_edge;
1035   gboolean is_snapped = FALSE;
1036
1037   hb = GTK_HANDLE_BOX (widget);
1038   if (!hb->in_drag)
1039     return FALSE;
1040
1041   if (!hb->in_drag || (event->window != widget->window))
1042     return FALSE;
1043   
1044   /* Calculate the attachment point on the float, if the float
1045    * were detached
1046    */
1047   new_x = 0;
1048   new_y = 0;
1049   gdk_window_get_pointer (NULL, &new_x, &new_y, NULL);
1050   new_x += hb->float_allocation.x;
1051   new_y += hb->float_allocation.y;
1052
1053   snap_edge = hb->snap_edge;
1054   if (snap_edge == -1)
1055     snap_edge = (hb->handle_position == GTK_POS_LEFT ||
1056                   hb->handle_position == GTK_POS_RIGHT) ?
1057       GTK_POS_TOP : GTK_POS_LEFT;
1058
1059   /* First, check if the snapped edge is aligned
1060    */
1061   switch (snap_edge)
1062     {
1063     case GTK_POS_TOP:
1064       is_snapped = abs (hb->attach_allocation.y - new_y) < TOLERANCE;
1065       break;
1066     case GTK_POS_BOTTOM:
1067       is_snapped = abs (hb->attach_allocation.y + (gint)hb->attach_allocation.height -
1068                         new_y - (gint)hb->float_allocation.height) < TOLERANCE;
1069       break;
1070     case GTK_POS_LEFT:
1071       is_snapped = abs (hb->attach_allocation.x - new_x) < TOLERANCE;
1072       break;
1073     case GTK_POS_RIGHT:
1074       is_snapped = abs (hb->attach_allocation.x + (gint)hb->attach_allocation.width -
1075                         new_x - (gint)hb->float_allocation.width) < TOLERANCE;
1076       break;
1077     }
1078
1079   /* Next, check if coordinates in the other direction are sufficiently
1080    * aligned
1081    */
1082   if (is_snapped)
1083     {
1084       gint float_pos1 = 0;      /* Initialize to suppress warnings */
1085       gint float_pos2 = 0;
1086       gint attach_pos1 = 0;
1087       gint attach_pos2 = 0;
1088       
1089       switch (snap_edge)
1090         {
1091         case GTK_POS_TOP:
1092         case GTK_POS_BOTTOM:
1093           attach_pos1 = hb->attach_allocation.x;
1094           attach_pos2 = hb->attach_allocation.x + hb->attach_allocation.width;
1095           float_pos1 = new_x;
1096           float_pos2 = new_x + hb->float_allocation.width;
1097           break;
1098         case GTK_POS_LEFT:
1099         case GTK_POS_RIGHT:
1100           attach_pos1 = hb->attach_allocation.y;
1101           attach_pos2 = hb->attach_allocation.y + hb->attach_allocation.height;
1102           float_pos1 = new_y;
1103           float_pos2 = new_y + hb->float_allocation.height;
1104           break;
1105         }
1106
1107       is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) && 
1108                     (attach_pos2 + TOLERANCE > float_pos2)) ||
1109                    ((float_pos1 - TOLERANCE < attach_pos1) &&
1110                     (float_pos2 + TOLERANCE > attach_pos2));
1111     }
1112
1113   if (is_snapped)
1114     {
1115       if (hb->child_detached)
1116         {
1117           hb->child_detached = FALSE;
1118           gdk_window_hide (hb->float_window);
1119           gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
1120           hb->float_window_mapped = FALSE;
1121           gtk_signal_emit (GTK_OBJECT (hb),
1122                            handle_box_signals[SIGNAL_CHILD_ATTACHED],
1123                            GTK_BIN (hb)->child);
1124           
1125           gtk_widget_queue_resize (widget);
1126         }
1127     }
1128   else
1129     {
1130       gint width, height;
1131
1132       gdk_window_get_size (hb->float_window, &width, &height);
1133       new_x += hb->deskoff_x;
1134       new_y += hb->deskoff_y;
1135
1136       switch (hb->handle_position)
1137         {
1138         case GTK_POS_LEFT:
1139           new_y += ((gint)hb->float_allocation.height - height) / 2;
1140           break;
1141         case GTK_POS_RIGHT:
1142           new_x += (gint)hb->float_allocation.width - width;
1143           new_y += ((gint)hb->float_allocation.height - height) / 2;
1144           break;
1145         case GTK_POS_TOP:
1146           new_x += ((gint)hb->float_allocation.width - width) / 2;
1147           break;
1148         case GTK_POS_BOTTOM:
1149           new_x += ((gint)hb->float_allocation.width - width) / 2;
1150           new_y += (gint)hb->float_allocation.height - height;
1151           break;
1152         }
1153
1154       if (hb->child_detached)
1155         {
1156           gdk_window_move (hb->float_window, new_x, new_y);
1157           gdk_window_raise (hb->float_window);
1158         }
1159       else
1160         {
1161           gint width;
1162           gint height;
1163           GtkRequisition child_requisition;
1164
1165           hb->child_detached = TRUE;
1166
1167           if (GTK_BIN (hb)->child)
1168             gtk_widget_get_child_requisition (GTK_BIN (hb)->child, &child_requisition);
1169           else
1170             {
1171               child_requisition.width = 0;
1172               child_requisition.height = 0;
1173             }      
1174
1175           width = child_requisition.width + 2 * GTK_CONTAINER (hb)->border_width;
1176           height = child_requisition.height + 2 * GTK_CONTAINER (hb)->border_width;
1177
1178           if (hb->handle_position == GTK_POS_LEFT || hb->handle_position == GTK_POS_RIGHT)
1179             width += DRAG_HANDLE_SIZE;
1180           else
1181             height += DRAG_HANDLE_SIZE;
1182           
1183           gdk_window_move_resize (hb->float_window, new_x, new_y, width, height);
1184           gdk_window_reparent (hb->bin_window, hb->float_window, 0, 0);
1185           gdk_window_set_hints (hb->float_window, new_x, new_y, 0, 0, 0, 0, GDK_HINT_POS);
1186           gdk_window_show (hb->float_window);
1187           hb->float_window_mapped = TRUE;
1188 #if     0
1189           /* this extra move is neccessary if we use decorations, or our
1190            * window manager insists on decorations.
1191            */
1192           gdk_flush ();
1193           gdk_window_move (hb->float_window, new_x, new_y);
1194           gdk_flush ();
1195 #endif  /* 0 */
1196           gtk_signal_emit (GTK_OBJECT (hb),
1197                            handle_box_signals[SIGNAL_CHILD_DETACHED],
1198                            GTK_BIN (hb)->child);
1199           gtk_handle_box_draw_ghost (hb);
1200           
1201           gtk_widget_queue_resize (widget);
1202         }
1203     }
1204
1205   return TRUE;
1206 }
1207
1208 static void
1209 gtk_handle_box_add (GtkContainer *container,
1210                     GtkWidget    *widget)
1211 {
1212   gtk_widget_set_parent_window (widget, GTK_HANDLE_BOX (container)->bin_window);
1213   GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
1214 }
1215
1216 static void
1217 gtk_handle_box_remove (GtkContainer *container,
1218                        GtkWidget    *widget)
1219 {
1220   GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1221
1222   gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1223 }
1224
1225 static gint
1226 gtk_handle_box_delete_event (GtkWidget *widget,
1227                              GdkEventAny  *event)
1228 {
1229   GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1230
1231   if (event->window == hb->float_window)
1232     {
1233       gtk_handle_box_reattach (hb);
1234       
1235       return TRUE;
1236     }
1237
1238   return FALSE;
1239 }
1240
1241 static void
1242 gtk_handle_box_reattach (GtkHandleBox *hb)
1243 {
1244   if (hb->child_detached)
1245     {
1246       hb->child_detached = FALSE;
1247       if (GTK_WIDGET_REALIZED (hb))
1248         {
1249           gdk_window_hide (hb->float_window);
1250           gdk_window_reparent (hb->bin_window, GTK_WIDGET (hb)->window, 0, 0);
1251
1252           if (GTK_BIN (hb)->child)
1253             gtk_signal_emit (GTK_OBJECT (hb),
1254                              handle_box_signals[SIGNAL_CHILD_ATTACHED],
1255                              GTK_BIN (hb)->child);
1256
1257         }
1258       hb->float_window_mapped = FALSE;
1259     }
1260   if (hb->in_drag)
1261     {
1262       gdk_pointer_ungrab (GDK_CURRENT_TIME);
1263       hb->in_drag = FALSE;
1264     }
1265
1266   gtk_widget_queue_resize (GTK_WIDGET (hb));
1267 }