gtkhandlebox.c 39.4 KB
Newer Older
1 2
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
Elliot Lee's avatar
Elliot Lee committed
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 8 9 10 11 12
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
19
 */
20

21
/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */
27

28
#include <stdlib.h>
29
#include "gtkhandlebox.h"
30
#include "gtkmain.h"
31
#include "gtkmarshalers.h"
32
#include "gtkwindow.h"
33
#include "gtkintl.h"
34

35 36 37 38 39 40 41 42
typedef struct _GtkHandleBoxPrivate GtkHandleBoxPrivate;

struct _GtkHandleBoxPrivate
{
  gint orig_x;
  gint orig_y;
};

43
enum {
44 45
  PROP_0,
  PROP_SHADOW,
46
  PROP_SHADOW_TYPE,
47
  PROP_HANDLE_POSITION,
48 49
  PROP_SNAP_EDGE,
  PROP_SNAP_EDGE_SET
50
};
51

Elliot Lee's avatar
Elliot Lee committed
52
#define DRAG_HANDLE_SIZE 10
53
#define CHILDLESS_SIZE	25
54
#define GHOST_HEIGHT 3
55
#define TOLERANCE 5
56

57
enum {
58 59 60 61 62
  SIGNAL_CHILD_ATTACHED,
  SIGNAL_CHILD_DETACHED,
  SIGNAL_LAST
};

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
/* The algorithm for docking and redocking implemented here
 * has a couple of nice properties:
 *
 * 1) During a single drag, docking always occurs at the
 *    the same cursor position. This means that the users
 *    motions are reversible, and that you won't
 *    undock/dock oscillations.
 *
 * 2) Docking generally occurs at user-visible features.
 *    The user, once they figure out to redock, will
 *    have useful information about doing it again in
 *    the future.
 *
 * Please try to preserve these properties if you
 * change the algorithm. (And the current algorithm
 * is far from ideal). Briefly, the current algorithm
 * for deciding whether the handlebox is docked or not:
 *
 * 1) The decision is done by comparing two rectangles - the
 *    allocation if the widget at the start of the drag,
 *    and the boundary of hb->bin_window at the start of
 *    of the drag offset by the distance that the cursor
 *    has moved.
 *
 * 2) These rectangles must have one edge, the "snap_edge"
 *    of the handlebox, aligned within TOLERANCE.
 * 
 * 3) On the other dimension, the extents of one rectangle
 *    must be contained in the extents of the other,
 *    extended by tolerance. That is, either we can have:
 *
 * <-TOLERANCE-|--------bin_window--------------|-TOLERANCE->
 *         <--------float_window-------------------->
 *
 * or we can have:
 *
 * <-TOLERANCE-|------float_window--------------|-TOLERANCE->
 *          <--------bin_window-------------------->
 */

103 104
static void gtk_handle_box_class_init     (GtkHandleBoxClass *klass);
static void gtk_handle_box_init           (GtkHandleBox      *handle_box);
105 106 107 108 109 110 111 112
static void gtk_handle_box_set_property   (GObject      *object,
					   guint         param_id,
					   const GValue *value,
					   GParamSpec   *pspec);
static void gtk_handle_box_get_property   (GObject     *object,
					   guint        param_id,
					   GValue      *value,
					   GParamSpec  *pspec);
113 114 115 116 117
static void gtk_handle_box_destroy        (GtkObject         *object);
static void gtk_handle_box_map            (GtkWidget         *widget);
static void gtk_handle_box_unmap          (GtkWidget         *widget);
static void gtk_handle_box_realize        (GtkWidget         *widget);
static void gtk_handle_box_unrealize      (GtkWidget         *widget);
Owen Taylor's avatar
Owen Taylor committed
118 119
static void gtk_handle_box_style_set      (GtkWidget         *widget,
					   GtkStyle          *previous_style);
120 121 122
static void gtk_handle_box_size_request   (GtkWidget         *widget,
					   GtkRequisition    *requisition);
static void gtk_handle_box_size_allocate  (GtkWidget         *widget,
123
					   GtkAllocation     *real_allocation);
124 125
static void gtk_handle_box_add            (GtkContainer      *container,
					   GtkWidget         *widget);
126 127
static void gtk_handle_box_remove         (GtkContainer      *container,
					   GtkWidget         *widget);
128
static void gtk_handle_box_draw_ghost     (GtkHandleBox      *hb);
129 130 131 132 133 134 135 136 137
static void gtk_handle_box_paint          (GtkWidget         *widget,
					   GdkEventExpose    *event,
					   GdkRectangle      *area);
static gint gtk_handle_box_expose         (GtkWidget         *widget,
					   GdkEventExpose    *event);
static gint gtk_handle_box_button_changed (GtkWidget         *widget,
					   GdkEventButton    *event);
static gint gtk_handle_box_motion         (GtkWidget         *widget,
					   GdkEventMotion    *event);
138 139
static gint gtk_handle_box_delete_event   (GtkWidget         *widget,
					   GdkEventAny       *event);
140
static void gtk_handle_box_reattach       (GtkHandleBox      *hb);
141 142 143


static GtkBinClass *parent_class;
144
static guint        handle_box_signals[SIGNAL_LAST] = { 0 };
145 146


Manish Singh's avatar
Manish Singh committed
147
GType
148
gtk_handle_box_get_type (void)
149
{
Manish Singh's avatar
Manish Singh committed
150
  static GType handle_box_type = 0;
151

Elliot Lee's avatar
Elliot Lee committed
152
  if (!handle_box_type)
153
    {
Manish Singh's avatar
Manish Singh committed
154
      static const GTypeInfo handle_box_info =
155 156
      {
	sizeof (GtkHandleBoxClass),
Manish Singh's avatar
Manish Singh committed
157 158 159 160 161 162 163 164
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_handle_box_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkHandleBox),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_handle_box_init,
165 166
      };

Manish Singh's avatar
Manish Singh committed
167 168
      handle_box_type = g_type_register_static (GTK_TYPE_BIN, "GtkHandleBox",
						&handle_box_info, 0);
169 170
    }

171
  return handle_box_type;
172 173 174 175 176
}

static void
gtk_handle_box_class_init (GtkHandleBoxClass *class)
{
177
  GObjectClass *gobject_class;
178
  GtkObjectClass *object_class;
179 180
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;
181

182
  gobject_class = (GObjectClass *) class;
183 184
  object_class = (GtkObjectClass *) class;
  widget_class = (GtkWidgetClass *) class;
185
  container_class = (GtkContainerClass *) class;
186

Manish Singh's avatar
Manish Singh committed
187
  parent_class = g_type_class_peek_parent (class);
188

189 190 191 192 193
  gobject_class->set_property = gtk_handle_box_set_property;
  gobject_class->get_property = gtk_handle_box_get_property;
  
  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW,
194
                                   g_param_spec_enum ("shadow", NULL,
195
                                                      _("Deprecated property, use shadow_type instead"),
196 197 198 199 200 201
						      GTK_TYPE_SHADOW_TYPE,
						      GTK_SHADOW_ETCHED_OUT,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW_TYPE,
                                   g_param_spec_enum ("shadow_type",
202
                                                      _("Shadow type"),
203
                                                      _("Appearance of the shadow that surrounds the container"),
204 205 206
						      GTK_TYPE_SHADOW_TYPE,
						      GTK_SHADOW_ETCHED_OUT,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
207
  
208 209 210 211
  g_object_class_install_property (gobject_class,
                                   PROP_HANDLE_POSITION,
                                   g_param_spec_enum ("handle_position",
                                                      _("Handle position"),
212
                                                      _("Position of the handle relative to the child widget"),
213 214 215
						      GTK_TYPE_POSITION_TYPE,
						      GTK_POS_LEFT,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
216
  
217 218 219 220
  g_object_class_install_property (gobject_class,
                                   PROP_SNAP_EDGE,
                                   g_param_spec_enum ("snap_edge",
                                                      _("Snap edge"),
221
                                                      _("Side of the handlebox that's lined up with the docking point to dock the handlebox"),
222
						      GTK_TYPE_POSITION_TYPE,
223
						      GTK_POS_TOP,
224
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
Manish Singh's avatar
Manish Singh committed
225

226 227 228 229 230 231 232 233
  g_object_class_install_property (gobject_class,
                                   PROP_SNAP_EDGE_SET,
                                   g_param_spec_boolean ("snap_edge_set",
							 _("Snap edge set"),
							 _("Whether to use the value from the snap_edge property or a value derived from handle_position"),
							 FALSE,
							 G_PARAM_READABLE | G_PARAM_WRITABLE));

234 235 236 237
  object_class->destroy = gtk_handle_box_destroy;

  widget_class->map = gtk_handle_box_map;
  widget_class->unmap = gtk_handle_box_unmap;
238
  widget_class->realize = gtk_handle_box_realize;
239
  widget_class->unrealize = gtk_handle_box_unrealize;
Owen Taylor's avatar
Owen Taylor committed
240
  widget_class->style_set = gtk_handle_box_style_set;
241 242 243
  widget_class->size_request = gtk_handle_box_size_request;
  widget_class->size_allocate = gtk_handle_box_size_allocate;
  widget_class->expose_event = gtk_handle_box_expose;
Elliot Lee's avatar
Elliot Lee committed
244 245
  widget_class->button_press_event = gtk_handle_box_button_changed;
  widget_class->button_release_event = gtk_handle_box_button_changed;
246
  widget_class->motion_notify_event = gtk_handle_box_motion;
247
  widget_class->delete_event = gtk_handle_box_delete_event;
248

249
  container_class->add = gtk_handle_box_add;
250 251
  container_class->remove = gtk_handle_box_remove;

252 253
  class->child_attached = NULL;
  class->child_detached = NULL;
254 255

  handle_box_signals[SIGNAL_CHILD_ATTACHED] =
Manish Singh's avatar
Manish Singh committed
256 257 258 259 260 261 262 263
    g_signal_new ("child_attached",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkHandleBoxClass, child_attached),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1,
		  GTK_TYPE_WIDGET);
264
  handle_box_signals[SIGNAL_CHILD_DETACHED] =
Manish Singh's avatar
Manish Singh committed
265 266 267 268 269 270 271 272
    g_signal_new ("child_detached",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkHandleBoxClass, child_detached),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1,
		  GTK_TYPE_WIDGET);
273 274
}

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
GtkHandleBoxPrivate *
gtk_handle_box_get_private (GtkHandleBox *hb)
{
  GtkHandleBoxPrivate *private;
  static GQuark private_quark = 0;

  if (!private_quark)
    private_quark = g_quark_from_static_string ("gtk-handle-box-private");

  private = g_object_get_qdata (G_OBJECT (hb), private_quark);

  if (!private)
    {
      private = g_new0 (GtkHandleBoxPrivate, 1);
      g_object_set_qdata_full (G_OBJECT (hb), private_quark,
			       private, g_free);
    }

  return private;
}

296
static void
297
gtk_handle_box_init (GtkHandleBox *handle_box)
298
{
299
  GTK_WIDGET_UNSET_FLAGS (handle_box, GTK_NO_WINDOW);
300

301
  handle_box->bin_window = NULL;
302
  handle_box->float_window = NULL;
303
  handle_box->shadow_type = GTK_SHADOW_OUT;
304 305 306 307
  handle_box->handle_position = GTK_POS_LEFT;
  handle_box->float_window_mapped = FALSE;
  handle_box->child_detached = FALSE;
  handle_box->in_drag = FALSE;
308
  handle_box->shrink_on_detach = TRUE;
309
  handle_box->snap_edge = -1;
310 311
}

312 313 314 315 316
static void 
gtk_handle_box_set_property (GObject         *object,
			     guint            prop_id,
			     const GValue    *value,
			     GParamSpec      *pspec)
317
{
318
  GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
319

320
  switch (prop_id)
321
    {
322
    case PROP_SHADOW:
323
    case PROP_SHADOW_TYPE:
324 325 326 327
      gtk_handle_box_set_shadow_type (handle_box, g_value_get_enum (value));
      break;
    case PROP_HANDLE_POSITION:
      gtk_handle_box_set_handle_position (handle_box, g_value_get_enum (value));
328
      break;
329 330
    case PROP_SNAP_EDGE:
      gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value));
331
      break;
332 333 334 335
    case PROP_SNAP_EDGE_SET:
      if (!g_value_get_boolean (value))
	gtk_handle_box_set_snap_edge (handle_box, (GtkPositionType)-1);
      break;
336 337
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
338 339 340 341
      break;
    }
}

342 343 344 345 346
static void 
gtk_handle_box_get_property (GObject         *object,
			     guint            prop_id,
			     GValue          *value,
			     GParamSpec      *pspec)
347
{
348 349
  GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
  
350
  switch (prop_id)
351
    {
352
    case PROP_SHADOW:
353
    case PROP_SHADOW_TYPE:
354
      g_value_set_enum (value, handle_box->shadow_type);
355
      break;
356 357
    case PROP_HANDLE_POSITION:
      g_value_set_enum (value, handle_box->handle_position);
358
      break;
359
    case PROP_SNAP_EDGE:
360 361 362 363 364 365
      g_value_set_enum (value,
			(handle_box->snap_edge == -1 ?
			 GTK_POS_TOP : handle_box->snap_edge));
      break;
    case PROP_SNAP_EDGE_SET:
      g_value_set_boolean (value, handle_box->snap_edge != -1);
366
      break;
367
    default:
368
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
369 370 371 372
      break;
    }
}
 
373
GtkWidget*
374 375
gtk_handle_box_new (void)
{
Manish Singh's avatar
Manish Singh committed
376
  return g_object_new (GTK_TYPE_HANDLE_BOX, NULL);
377 378 379 380
}

static void
gtk_handle_box_destroy (GtkObject *object)
381
{
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  if (GTK_OBJECT_CLASS (parent_class)->destroy)
    (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}

static void
gtk_handle_box_map (GtkWidget *widget)
{
  GtkBin *bin;
  GtkHandleBox *hb;

  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);

  bin = GTK_BIN (widget);
  hb = GTK_HANDLE_BOX (widget);

397 398 399 400
  if (bin->child &&
      GTK_WIDGET_VISIBLE (bin->child) &&
      !GTK_WIDGET_MAPPED (bin->child))
    gtk_widget_map (bin->child);
401

402 403 404 405 406 407
  if (hb->child_detached && !hb->float_window_mapped)
    {
      gdk_window_show (hb->float_window);
      hb->float_window_mapped = TRUE;
    }

408 409
  gdk_window_show (hb->bin_window);
  gdk_window_show (widget->window);
410 411 412 413 414 415 416 417 418 419 420 421
}

static void
gtk_handle_box_unmap (GtkWidget *widget)
{
  GtkHandleBox *hb;

  GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);

  hb = GTK_HANDLE_BOX (widget);

  gdk_window_hide (widget->window);
422 423 424 425 426
  if (hb->float_window_mapped)
    {
      gdk_window_hide (hb->float_window);
      hb->float_window_mapped = FALSE;
    }
427 428 429 430 431 432 433
}

static void
gtk_handle_box_realize (GtkWidget *widget)
{
  GdkWindowAttr attributes;
  gint attributes_mask;
434
  GtkHandleBox *hb;
435

436 437
  hb = GTK_HANDLE_BOX (widget);

438 439 440 441 442 443 444 445 446 447
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
448 449
  attributes.event_mask = (gtk_widget_get_events (widget)
			   | GDK_EXPOSURE_MASK);
450
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
451 452
  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, widget);
453 454 455

  attributes.x = 0;
  attributes.y = 0;
456 457 458
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.window_type = GDK_WINDOW_CHILD;
Soeren Sandmann's avatar
Soeren Sandmann committed
459 460 461 462 463
  attributes.event_mask = (gtk_widget_get_events (widget) |
			   GDK_EXPOSURE_MASK |
			   GDK_BUTTON1_MOTION_MASK |
			   GDK_POINTER_MOTION_HINT_MASK |
			   GDK_BUTTON_PRESS_MASK |
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
			    GDK_BUTTON_RELEASE_MASK);
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
  hb->bin_window = gdk_window_new (widget->window, &attributes, attributes_mask);
  gdk_window_set_user_data (hb->bin_window, widget);
  if (GTK_BIN (hb)->child)
    gtk_widget_set_parent_window (GTK_BIN (hb)->child, hb->bin_window);
  
  attributes.x = 0;
  attributes.y = 0;
  attributes.width = widget->requisition.width;
  attributes.height = widget->requisition.height;
  attributes.window_type = GDK_WINDOW_TOPLEVEL;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = (gtk_widget_get_events (widget) |
			   GDK_KEY_PRESS_MASK |
			   GDK_ENTER_NOTIFY_MASK |
			   GDK_LEAVE_NOTIFY_MASK |
			   GDK_FOCUS_CHANGE_MASK |
			   GDK_STRUCTURE_MASK);
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
486 487
  hb->float_window = gdk_window_new (gtk_widget_get_root_window (widget),
				     &attributes, attributes_mask);
488 489
  gdk_window_set_user_data (hb->float_window, widget);
  gdk_window_set_decorations (hb->float_window, 0);
490
  gdk_window_set_type_hint (hb->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
491
  
492
  widget->style = gtk_style_attach (widget->style, widget->window);
493 494 495
  gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (hb));
  gtk_style_set_background (widget->style, hb->bin_window, GTK_WIDGET_STATE (hb));
  gtk_style_set_background (widget->style, hb->float_window, GTK_WIDGET_STATE (hb));
496
  gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
497 498
}

499 500 501
static void
gtk_handle_box_unrealize (GtkWidget *widget)
{
502
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
503

504 505 506 507 508
  gdk_window_set_user_data (hb->bin_window, NULL);
  gdk_window_destroy (hb->bin_window);
  hb->bin_window = NULL;
  gdk_window_set_user_data (hb->float_window, NULL);
  gdk_window_destroy (hb->float_window);
509
  hb->float_window = NULL;
510 511 512

  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
513 514
}

Owen Taylor's avatar
Owen Taylor committed
515 516 517 518
static void
gtk_handle_box_style_set (GtkWidget *widget,
			  GtkStyle  *previous_style)
{
519
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
Owen Taylor's avatar
Owen Taylor committed
520 521 522 523 524

  if (GTK_WIDGET_REALIZED (widget) &&
      !GTK_WIDGET_NO_WINDOW (widget))
    {
      gtk_style_set_background (widget->style, widget->window,
Soeren Sandmann's avatar
Soeren Sandmann committed
525
				widget->state);
Owen Taylor's avatar
Owen Taylor committed
526 527 528 529 530
      gtk_style_set_background (widget->style, hb->bin_window, widget->state);
      gtk_style_set_background (widget->style, hb->float_window, widget->state);
    }
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
static int
effective_handle_position (GtkHandleBox *hb)
{
  int handle_position;

  if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
    handle_position = hb->handle_position;
  else
    {
      switch (hb->handle_position) 
	{
	case GTK_POS_LEFT:
	  handle_position = GTK_POS_RIGHT;
	  break;
	case GTK_POS_RIGHT:
	  handle_position = GTK_POS_LEFT;
	  break;
	default:
	  handle_position = hb->handle_position;
	  break;
	}
    }

  return handle_position;
}

557 558
static void
gtk_handle_box_size_request (GtkWidget      *widget,
Elliot Lee's avatar
Elliot Lee committed
559
			     GtkRequisition *requisition)
560 561
{
  GtkBin *bin;
562
  GtkHandleBox *hb;
563
  GtkRequisition child_requisition;
564
  gint handle_position;
565 566

  bin = GTK_BIN (widget);
567
  hb = GTK_HANDLE_BOX (widget);
568

569 570 571 572
  handle_position = effective_handle_position (hb);

  if (handle_position == GTK_POS_LEFT ||
      handle_position == GTK_POS_RIGHT)
573 574 575 576 577
    {
      requisition->width = DRAG_HANDLE_SIZE;
      requisition->height = 0;
    }
  else
578
    {
579 580 581
      requisition->width = 0;
      requisition->height = DRAG_HANDLE_SIZE;
    }
582

583 584 585 586
  /* if our child is not visible, we still request its size, since we
   * won't have any usefull hint for our size otherwise.
   */
  if (bin->child)
587
    gtk_widget_size_request (bin->child, &child_requisition);
588 589 590 591 592
  else
    {
      child_requisition.width = 0;
      child_requisition.height = 0;
    }      
593 594 595

  if (hb->child_detached)
    {
596
      /* FIXME: This doesn't work currently */
597 598
      if (!hb->shrink_on_detach)
	{
599 600
	  if (handle_position == GTK_POS_LEFT ||
	      handle_position == GTK_POS_RIGHT)
601
	    requisition->height += child_requisition.height;
602
	  else
603
	    requisition->width += child_requisition.width;
604
	}
605
      else
606
	{
607 608
	  if (handle_position == GTK_POS_LEFT ||
	      handle_position == GTK_POS_RIGHT)
609
	    requisition->height += widget->style->ythickness;
610
	  else
611
	    requisition->width += widget->style->xthickness;
612
	}
613
    }
614 615
  else
    {
616 617 618 619 620
      requisition->width += GTK_CONTAINER (widget)->border_width * 2;
      requisition->height += GTK_CONTAINER (widget)->border_width * 2;
      
      if (bin->child)
	{
621 622
	  requisition->width += child_requisition.width;
	  requisition->height += child_requisition.height;
623 624 625 626 627 628
	}
      else
	{
	  requisition->width += CHILDLESS_SIZE;
	  requisition->height += CHILDLESS_SIZE;
	}
629
    }
630 631 632 633
}

static void
gtk_handle_box_size_allocate (GtkWidget     *widget,
634
			      GtkAllocation *allocation)
635 636
{
  GtkBin *bin;
637
  GtkHandleBox *hb;
638
  GtkRequisition child_requisition;
639
  gint handle_position;
640
  
641
  bin = GTK_BIN (widget);
642
  hb = GTK_HANDLE_BOX (widget);
643
  
644 645
  handle_position = effective_handle_position (hb);

646 647 648 649 650 651
  if (bin->child)
    gtk_widget_get_child_requisition (bin->child, &child_requisition);
  else
    {
      child_requisition.width = 0;
      child_requisition.height = 0;
652
    }      
653
      
654
  widget->allocation = *allocation;
655

656 657 658 659 660 661
  if (GTK_WIDGET_REALIZED (hb))
    gdk_window_move_resize (widget->window,
			    widget->allocation.x,
			    widget->allocation.y,
			    widget->allocation.width,
			    widget->allocation.height);
662

663 664 665

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    {
666
      GtkWidget *child;
667
      GtkAllocation child_allocation;
668 669 670 671
      guint border_width;

      child = bin->child;
      border_width = GTK_CONTAINER (widget)->border_width;
672

673 674
      child_allocation.x = border_width;
      child_allocation.y = border_width;
675
      if (handle_position == GTK_POS_LEFT)
676
	child_allocation.x += DRAG_HANDLE_SIZE;
677
      else if (handle_position == GTK_POS_TOP)
678
	child_allocation.y += DRAG_HANDLE_SIZE;
679

680
      if (hb->child_detached)
681
	{
682 683 684
	  guint float_width;
	  guint float_height;
	  
685 686
	  child_allocation.width = child_requisition.width;
	  child_allocation.height = child_requisition.height;
687 688 689 690
	  
	  float_width = child_allocation.width + 2 * border_width;
	  float_height = child_allocation.height + 2 * border_width;
	  
691 692
	  if (handle_position == GTK_POS_LEFT ||
	      handle_position == GTK_POS_RIGHT)
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	    float_width += DRAG_HANDLE_SIZE;
	  else
	    float_height += DRAG_HANDLE_SIZE;

	  if (GTK_WIDGET_REALIZED (hb))
	    {
	      gdk_window_resize (hb->float_window,
				 float_width,
				 float_height);
	      gdk_window_move_resize (hb->bin_window,
				      0,
				      0,
				      float_width,
				      float_height);
	    }
708 709 710
	}
      else
	{
711 712
	  child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
	  child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
713

714 715
	  if (handle_position == GTK_POS_LEFT ||
	      handle_position == GTK_POS_RIGHT)
716 717 718 719 720 721 722 723 724 725
	    child_allocation.width -= DRAG_HANDLE_SIZE;
	  else
	    child_allocation.height -= DRAG_HANDLE_SIZE;
	  
	  if (GTK_WIDGET_REALIZED (hb))
	    gdk_window_move_resize (hb->bin_window,
				    0,
				    0,
				    widget->allocation.width,
				    widget->allocation.height);
726 727
	}

728
      gtk_widget_size_allocate (bin->child, &child_allocation);
729
    }
730
}
731

732
static void
733
gtk_handle_box_draw_ghost (GtkHandleBox *hb)
734
{
735 736 737 738 739
  GtkWidget *widget;
  guint x;
  guint y;
  guint width;
  guint height;
740
  gint handle_position;
741

742
  widget = GTK_WIDGET (hb);
743 744 745 746
  
  handle_position = effective_handle_position (hb);
  if (handle_position == GTK_POS_LEFT ||
      handle_position == GTK_POS_RIGHT)
747
    {
748
      x = handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
749 750 751
      y = 0;
      width = DRAG_HANDLE_SIZE;
      height = widget->allocation.height;
752
    }
753 754 755
  else
    {
      x = 0;
756
      y = handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
757 758 759
      width = widget->allocation.width;
      height = DRAG_HANDLE_SIZE;
    }
760
  gtk_paint_shadow (widget->style,
761 762
		    widget->window,
		    GTK_WIDGET_STATE (widget),
763 764 765 766 767 768
		    GTK_SHADOW_ETCHED_IN,
		    NULL, widget, "handle",
		    x,
		    y,
		    width,
		    height);
769 770
   if (handle_position == GTK_POS_LEFT ||
       handle_position == GTK_POS_RIGHT)
771 772 773 774
     gtk_paint_hline (widget->style,
		      widget->window,
		      GTK_WIDGET_STATE (widget),
		      NULL, widget, "handlebox",
775 776
		      handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
		      handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
777 778 779 780 781 782
		      widget->allocation.height / 2);
   else
     gtk_paint_vline (widget->style,
		      widget->window,
		      GTK_WIDGET_STATE (widget),
		      NULL, widget, "handlebox",
783 784
		      handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
		      handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
785
		      widget->allocation.width / 2);
786 787
}

788
static void
789 790
draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
		     GdkRectangle *clip)
791
{
792 793 794 795
   gtk_paint_handle (widget->style, window, GTK_STATE_NORMAL, shadow,
		     clip, widget, "handlebox",
		     rect->x, rect->y, rect->width, rect->height, 
		     GTK_ORIENTATION_VERTICAL);
796 797
}

798 799 800 801 802 803 804 805 806
void
gtk_handle_box_set_shadow_type (GtkHandleBox  *handle_box,
				GtkShadowType  type)
{
  g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));

  if ((GtkShadowType) handle_box->shadow_type != type)
    {
      handle_box->shadow_type = type;
807
      g_object_notify (G_OBJECT (handle_box), "shadow_type");
808 809 810
      gtk_widget_queue_resize (GTK_WIDGET (handle_box));
    }
}
811

812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
/**
 * gtk_handle_box_get_shadow_type:
 * @handle_box: a #GtkHandleBox
 * 
 * Gets the type of shadow drawn around the handle box. See
 * gtk_handle_box_set_shadow_type().
 *
 * Return value: the type of shadow currently drawn around the handle box.
 **/
GtkShadowType
gtk_handle_box_get_shadow_type (GtkHandleBox *handle_box)
{
  g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_SHADOW_ETCHED_OUT);

  return handle_box->shadow_type;
}

829 830 831 832
void        
gtk_handle_box_set_handle_position  (GtkHandleBox    *handle_box,
				     GtkPositionType  position)
{
833 834
  g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));

835 836 837
  if ((GtkPositionType) handle_box->handle_position != position)
    {
      handle_box->handle_position = position;
838
      g_object_notify (G_OBJECT (handle_box), "handle_position");
839 840 841 842
      gtk_widget_queue_resize (GTK_WIDGET (handle_box));
    }
}

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
/**
 * gtk_handle_box_get_handle_position:
 * @handle_box: a #GtkHandleBox
 *
 * Gets the handle position of the handle box. See
 * gtk_handle_box_set_handle_position().
 *
 * Return value: the current handle position.
 **/
GtkPositionType
gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
{
  g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);

  return handle_box->handle_position;
}

860 861 862 863 864 865
void        
gtk_handle_box_set_snap_edge        (GtkHandleBox    *handle_box,
				     GtkPositionType  edge)
{
  g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));

866 867 868
  if (handle_box->snap_edge != edge)
    {
      handle_box->snap_edge = edge;
869 870
      
      g_object_freeze_notify (G_OBJECT (handle_box));
871
      g_object_notify (G_OBJECT (handle_box), "snap_edge");
872 873
      g_object_notify (G_OBJECT (handle_box), "snap_edge_set");
      g_object_thaw_notify (G_OBJECT (handle_box));
874
    }
875 876
}

877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
/**
 * gtk_handle_box_get_snap_edge:
 * @handle_box: a #GtkHandleBox
 * 
 * Gets the edge used for determining reattachment of the handle box. See
 * gtk_handle_box_set_snap_edge().
 *
 * Return value: the edge used for determining reattachment, or (GtkPositionType)-1 if this
 *               is determined (as per default) from the handle position. 
 **/
GtkPositionType
gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
{
  g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);

  return handle_box->snap_edge;
}

895
static void
896
gtk_handle_box_paint (GtkWidget      *widget,
897 898
		  
    GdkEventExpose *event,
899
		      GdkRectangle   *area)
Elliot Lee's avatar
Elliot Lee committed
900
{
901
  GtkBin *bin;
902
  GtkHandleBox *hb;
903 904
  guint width;
  guint height;
905
  GdkRectangle rect;
906
  GdkRectangle dest;
907
  gint handle_position;
908

909 910
  bin = GTK_BIN (widget);
  hb = GTK_HANDLE_BOX (widget);
911

912 913
  handle_position = effective_handle_position (hb);

Manish Singh's avatar
Manish Singh committed
914
  gdk_drawable_get_size (hb->bin_window, &width, &height);
915
  
916
  if (!event)
917 918 919 920 921 922
    gtk_paint_box (widget->style,
		   hb->bin_window,
		   GTK_WIDGET_STATE (widget),
		   hb->shadow_type,
		   area, widget, "handlebox_bin",
		   0, 0, -1, -1);
923
  else
924 925 926 927 928 929
   gtk_paint_box (widget->style,
		  hb->bin_window,
		  GTK_WIDGET_STATE (widget),
		  hb->shadow_type,
		  &event->area, widget, "handlebox_bin",
		  0, 0, -1, -1);
930 931 932 933 934 935 936 937

/* We currently draw the handle _above_ the relief of the handlebox.
 * it could also be drawn on the same level...

		 hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
		 hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
		 width,
		 height);*/
938

939
  switch (handle_position)
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
    {
    case GTK_POS_LEFT:
      rect.x = 0;
      rect.y = 0; 
      rect.width = DRAG_HANDLE_SIZE;
      rect.height = height;
      break;
    case GTK_POS_RIGHT:
      rect.x = width - DRAG_HANDLE_SIZE; 
      rect.y = 0;
      rect.width = DRAG_HANDLE_SIZE;
      rect.height = height;
      break;
    case GTK_POS_TOP:
      rect.x = 0;
      rect.y = 0; 
      rect.width = width;
      rect.height = DRAG_HANDLE_SIZE;
      break;
    case GTK_POS_BOTTOM:
      rect.x = 0;
      rect.y = height - DRAG_HANDLE_SIZE;
      rect.width = width;
      rect.height = DRAG_HANDLE_SIZE;
      break;
    }
966

967 968 969 970
  if (gdk_rectangle_intersect (event ? &event->area : area, &rect, &dest))
    draw_textured_frame (widget, hb->bin_window, &rect,
			 GTK_SHADOW_OUT,
			 event ? &event->area : area);
971 972

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
973
    (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
Elliot Lee's avatar
Elliot Lee committed
974 975
}

976 977
static gint
gtk_handle_box_expose (GtkWidget      *widget,
Elliot Lee's avatar
Elliot Lee committed
978
		       GdkEventExpose *event)
979
{
980
  GtkHandleBox *hb;
981 982 983

  if (GTK_WIDGET_DRAWABLE (widget))
    {
984 985
      hb = GTK_HANDLE_BOX (widget);

986 987 988 989 990 991
      if (event->window == widget->window)
	{
	  if (hb->child_detached)
	    gtk_handle_box_draw_ghost (hb);
	}
      else
992
	gtk_handle_box_paint (widget, event, NULL);
993
    }
994
  
995 996 997
  return FALSE;
}

998
static gint
999 1000
gtk_handle_box_button_changed (GtkWidget      *widget,
			       GdkEventButton *event)
1001 1002
{
  GtkHandleBox *hb;
1003
  gboolean event_handled;
1004
  GdkCursor *fleur;
1005
  gint handle_position;
1006

1007 1008
  hb = GTK_HANDLE_BOX (widget);

1009 1010
  handle_position = effective_handle_position (hb);

1011
  event_handled = FALSE;
1012 1013
  if ((event->button == 1) && 
      (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
1014
    {
1015 1016 1017
      GtkWidget *child;
      gboolean in_handle;
      
1018 1019 1020
      if (event->window != hb->bin_window)
	return FALSE;

1021
      child = GTK_BIN (hb)->child;
1022 1023

      if (child)
1024
	{
1025
	  switch (handle_position)
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
	    {
	    case GTK_POS_LEFT:
	      in_handle = event->x < DRAG_HANDLE_SIZE;
	      break;
	    case GTK_POS_TOP:
	      in_handle = event->y < DRAG_HANDLE_SIZE;
	      break;
	    case GTK_POS_RIGHT:
	      in_handle = event->x > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.width;
	      break;
	    case GTK_POS_BOTTOM:
	      in_handle = event->y > 2 * GTK_CONTAINER (hb)->border_width + child->allocation.height;
	      break;
	    default:
	      in_handle = FALSE;
	      break;
	    }
1043
	}
1044
      else
1045 1046 1047 1048 1049 1050
	{
	  in_handle = FALSE;
	  event_handled = TRUE;
	}
      
      if (in_handle)
1051
	{
1052 1053
	  if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
	    {
1054
	      GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);
1055 1056 1057
	      gint desk_x, desk_y;
	      gint root_x, root_y;
	      gint width, height;
1058

1059 1060
	      gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y);
	      gdk_window_get_origin (hb->bin_window, &root_x, &root_y);
Manish Singh's avatar
Manish Singh committed
1061
	      gdk_drawable_get_size (hb->bin_window, &width, &height);
1062 1063 1064

	      private->orig_x = event->x_root;
	      private->orig_y = event->y_root;
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	      
	      hb->float_allocation.x = root_x - event->x_root;
	      hb->float_allocation.y = root_y - event->y_root;
	      hb->float_allocation.width = width;
	      hb->float_allocation.height = height;
	      
	      hb->deskoff_x = desk_x - root_x;
	      hb->deskoff_y = desk_y - root_y;
	      
	      gdk_window_get_origin (widget->window, &root_x, &root_y);
Manish Singh's avatar
Manish Singh committed
1075
	      gdk_drawable_get_size (widget->window, &width, &height);
1076 1077 1078 1079 1080 1081 1082
	      
	      hb->attach_allocation.x = root_x;
	      hb->attach_allocation.y = root_y;
	      hb->attach_allocation.width = width;
	      hb->attach_allocation.height = height;

	      hb->in_drag = TRUE;
1083 1084
	      fleur = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
						  GDK_FLEUR);
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	      if (gdk_pointer_grab (widget->window,
				    FALSE,
				    (GDK_BUTTON1_MOTION_MASK |
				     GDK_POINTER_MOTION_HINT_MASK |
				     GDK_BUTTON_RELEASE_MASK),
				    NULL,
				    fleur,
				    GDK_CURRENT_TIME) != 0)
		{
		  hb->in_drag = FALSE;
		}
	      
Manish Singh's avatar
Manish Singh committed
1097
	      gdk_cursor_unref (fleur);
1098 1099 1100 1101 1102 1103
	      event_handled = TRUE;
	    }
	  else if (hb->child_detached) /* Double click */
	    {
	      gtk_handle_box_reattach (hb);
	    }
1104 1105
	}
    }
1106 1107 1108
  else if (event->type == GDK_BUTTON_RELEASE &&
	   hb->in_drag)
    {
1109 1110 1111
      if (event->window != widget->window)
	return FALSE;
      
1112 1113
      gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
				  GDK_CURRENT_TIME);
1114 1115 1116 1117 1118
      hb->in_drag = FALSE;
      event_handled = TRUE;
    }
  
  return event_handled;
1119 1120
}

1121 1122 1123
static gint
gtk_handle_box_motion (GtkWidget      *widget,
		       GdkEventMotion *event)
1124 1125
{
  GtkHandleBox *hb;
1126
  gint new_x, new_y;
1127 1128
  gint snap_edge;
  gboolean is_snapped = FALSE;
1129
  gint handle_position;
1130
  GdkGeometry geometry;
1131
  GdkScreen *screen, *pointer_screen;
1132 1133

  hb = GTK_HANDLE_BOX (widget);
1134 1135 1136
  if (!hb->in_drag)
    return FALSE;

1137
  if (!hb->in_drag || (event->window != widget->window))
1138
    return FALSE;
1139
  
1140 1141
  handle_position = effective_handle_position (hb);

1142 1143 1144
  /* Calculate the attachment point on the float, if the float
   * were detached
   */
1145 1146
  new_x = 0;
  new_y = 0;
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
  screen = gtk_widget_get_screen (widget);
  gdk_display_get_pointer (gdk_screen_get_display (screen),
			   &pointer_screen, 
			   &new_x, &new_y, NULL);
  if (pointer_screen != screen)
    {
      GtkHandleBoxPrivate *private = gtk_handle_box_get_private (hb);

      new_x = private->orig_x;
      new_y = private->orig_y;
    }
  
1159 1160 1161 1162 1163
  new_x += hb->float_allocation.x;
  new_y += hb->float_allocation.y;

  snap_edge = hb->snap_edge;
  if (snap_edge == -1)
1164 1165
    snap_edge = (handle_position == GTK_POS_LEFT ||
		 handle_position == GTK_POS_RIGHT) ?
1166 1167
      GTK_POS_TOP : GTK_POS_LEFT;

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) 
    switch (snap_edge) 
      {
      case GTK_POS_LEFT:
	snap_edge = GTK_POS_RIGHT;
	break;
      case GTK_POS_RIGHT:
	snap_edge = GTK_POS_LEFT;
	break;
      default:
	break;
      }

1181 1182 1183
  /* First, check if the snapped edge is aligned
   */
  switch (snap_edge)
1184
    {
1185
    case GTK_POS_TOP:
1186
      is_snapped = abs (hb->attach_allocation.y - new_y) < TOLERANCE;
1187 1188
      break;
    case GTK_POS_BOTTOM:
1189 1190
      is_snapped = abs (hb->attach_allocation.y + (gint)hb->attach_allocation.height -
			new_y - (gint)hb->float_allocation.height) < TOLERANCE;
1191
      break;
1192 1193 1194 1195 1196 1197
    case GTK_POS_LEFT:
      is_snapped = abs (hb->attach_allocation.x - new_x) < TOLERANCE;
      break;
    case GTK_POS_RIGHT:
      is_snapped = abs (hb->attach_allocation.x + (gint)hb->attach_allocation.width -
			new_x - (gint)hb->float_allocation.width) < TOLERANCE;
1198
      break;
1199
    }
1200

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
  /* Next, check if coordinates in the other direction are sufficiently
   * aligned
   */
  if (is_snapped)
    {
      gint float_pos1 = 0;	/* Initialize to suppress warnings */
      gint float_pos2 = 0;
      gint attach_pos1 = 0;
      gint attach_pos2 = 0;
      
      switch (snap_edge)
	{
	case GTK_POS_TOP:
	case GTK_POS_BOTTOM:
	  attach_pos1 = hb->attach_allocation.x;
	  attach_pos2 = hb->attach_allocation.x + hb->attach_allocation.width;
	  float_pos1 = new_x;
	  float_pos2 = new_x + hb->float_allocation.width;
	  break;
	case GTK_POS_LEFT:
	case GTK_POS_RIGHT:
	  attach_pos1 = hb->attach_allocation.y;
	  attach_pos2 = hb->attach_allocation.y + hb->attach_allocation.height;
	  float_pos1 = new_y;
	  float_pos2 = new_y + hb->float_allocation.height;
	  break;
	}

      is_snapped = ((attach_pos1 - TOLERANCE < float_pos1) && 
		    (attach_pos2 + TOLERANCE > float_pos2)) ||
	           ((float_pos1 - TOLERANCE < attach_pos1) &&
		    (float_pos2 + TOLERANCE > attach_pos2));
    }

  if (is_snapped)
1236
    {
1237 1238 1239 1240 1241 1242
      if (hb->child_detached)
	{
	  hb->child_detached = FALSE;
	  gdk_window_hide (hb->float_window);
	  gdk_window_reparent (hb->bin_window, widget->window, 0, 0);
	  hb->float_window_mapped = FALSE;
Manish Singh's avatar
Manish Singh committed
1243 1244 1245 1246
	  g_signal_emit (hb,
			 handle_box_signals[SIGNAL_CHILD_ATTACHED],
			 0,
			 GTK_BIN (hb)->child);
1247 1248 1249
	  
	  gtk_widget_queue_resize (widget);
	}
1250
    }
1251
  else
1252
    {
1253 1254
      gint width, height;

Manish Singh's avatar
Manish Singh committed
1255
      gdk_drawable_get_size (hb->float_window, &width, &height);
1256 1257 1258
      new_x += hb->deskoff_x;
      new_y += hb->deskoff_y;

1259
      switch (handle_position)
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
	{
	case GTK_POS_LEFT:
	  new_y += ((gint)hb->float_allocation.height - height) / 2;
	  break;
	case GTK_POS_RIGHT:
	  new_x += (gint)hb->float_allocation.width - width;
	  new_y += ((gint)hb->float_allocation.height - height) / 2;
	  break;
	case GTK_POS_TOP:
	  new_x += ((gint)hb->float_allocation.width - width) / 2;
	  break;
	case GTK_POS_BOTTOM:
	  new_x += ((gint)hb->float_allocation.width - width) / 2;
	  new_y += (gint)hb->float_allocation.height - height;
	  break;
	}

1277
      if (hb->child_detached)
1278
	{
1279 1280
	  gdk_window_move (hb->float_window, new_x, new_y);
	  gdk_window_raise (hb->float_window);
1281 1282 1283
	}
      else
	{
1284 1285
	  gint width;
	  gint height;
1286
	  GtkRequisition child_requisition;
1287

1288
	  hb->child_detached = TRUE;
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299

	  if (GTK_BIN (hb)->child)
	    gtk_widget_get_child_requisition (GTK_BIN (hb)->child, &child_requisition);
	  else
	    {
	      child_requisition.width = 0;
	      child_requisition.height = 0;
	    }      

	  width = child_requisition.width + 2 * GTK_CONTAINER (hb)->border_width;
	  height = child_requisition.height + 2 * GTK_CONTAINER (hb)->border_width;
1300

1301
	  if (handle_position == GTK_POS_LEFT || handle_position == GTK_POS_RIGHT)
1302 1303 1304 1305
	    width += DRAG_HANDLE_SIZE;
	  else
	    height += DRAG_HANDLE_SIZE;
	  
1306 1307
	  gdk_window_move_resize (hb->float_window, new_x, new_y, width, height);
	  gdk_window_reparent (hb->bin_window, hb->float_window, 0, 0);
1308
	  gdk_window_set_geometry_hints (hb->float_window, &geometry, GDK_HINT_POS);
1309 1310 1311 1312 1313 1314
	  gdk_window_show (hb->float_window);
	  hb->float_window_mapped = TRUE;
#if	0
	  /* this extra move is neccessary if we use decorations, or our
	   * window manager insists on decorations.
	   */
1315
	  gdk_display_sync (gtk_widget_get_display (widget));
1316
	  gdk_window_move (hb->float_window, new_x, new_y);
1317
	  gdk_display_sync (gtk_widget_get_display (widget));
1318
#endif	/* 0 */
Manish Singh's avatar
Manish Singh committed
1319 1320 1321 1322
	  g_signal_emit (hb,
			 handle_box_signals[SIGNAL_CHILD_DETACHED],
			 0,
			 GTK_BIN (hb)->child);
1323 1324 1325 1326 1327
	  gtk_handle_box_draw_ghost (hb);
	  
	  gtk_widget_queue_resize (widget);
	}
    }
1328

1329 1330
  return TRUE;
}
1331

1332 1333 1334 1335 1336 1337 1338
static void
gtk_handle_box_add (GtkContainer *container,
		    GtkWidget    *widget)
{
  gtk_widget_set_parent_window (widget, GTK_HANDLE_BOX (container)->bin_window);
  GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
}
1339

1340 1341 1342 1343 1344
static void
gtk_handle_box_remove (GtkContainer *container,
		       GtkWidget    *widget)
{
  GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1345 1346

  gtk_handle_box_reattach (GTK_HANDLE_BOX (container));
1347
}
1348 1349

static gint
1350 1351
gtk_handle_box_delete_event (GtkWidget *widget,
			     GdkEventAny  *event)
1352
{
1353
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367

  if (event->window == hb->float_window)
    {
      gtk_handle_box_reattach (hb);
      
      return TRUE;
    }

  return FALSE;
}

static void
gtk_handle_box_reattach (GtkHandleBox *hb)
{
1368 1369
  GtkWidget *widget = GTK_WIDGET (hb);
  
1370 1371 1372 1373 1374 1375
  if (hb->child_detached)
    {
      hb->child_detached = FALSE;
      if (GTK_WIDGET_REALIZED (hb))
	{
	  gdk_window_hide (hb->float_window);
1376
	  gdk_window_reparent (hb->bin_window, widget->window, 0, 0);