gtkhandlebox.c 34.3 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 31
#include "gtkmain.h"
#include "gtksignal.h"
32
#include "gtkwindow.h"
33

34 35
enum {
  ARG_0,
36 37 38
  ARG_SHADOW,
  ARG_HANDLE_POSITION,
  ARG_SNAP_EDGE
39
};
40

Elliot Lee's avatar
Elliot Lee committed
41
#define DRAG_HANDLE_SIZE 10
42
#define CHILDLESS_SIZE	25
43
#define GHOST_HEIGHT 3
44
#define TOLERANCE 5
45

46
enum {
47 48 49 50 51
  SIGNAL_CHILD_ATTACHED,
  SIGNAL_CHILD_DETACHED,
  SIGNAL_LAST
};

52 53 54 55 56 57 58 59 60 61 62 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
/* 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-------------------->
 */

92 93
static void gtk_handle_box_class_init     (GtkHandleBoxClass *klass);
static void gtk_handle_box_init           (GtkHandleBox      *handle_box);
94 95 96
static void gtk_handle_box_set_arg        (GtkObject         *object,
					   GtkArg            *arg,
					   guint              arg_id);
97 98 99
static void gtk_handle_box_get_arg        (GtkObject         *object,
					   GtkArg            *arg,
					   guint              arg_id);
100 101 102 103 104
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
105 106
static void gtk_handle_box_style_set      (GtkWidget         *widget,
					   GtkStyle          *previous_style);
107 108 109
static void gtk_handle_box_size_request   (GtkWidget         *widget,
					   GtkRequisition    *requisition);
static void gtk_handle_box_size_allocate  (GtkWidget         *widget,
110
					   GtkAllocation     *real_allocation);
111 112
static void gtk_handle_box_add            (GtkContainer      *container,
					   GtkWidget         *widget);
113 114
static void gtk_handle_box_remove         (GtkContainer      *container,
					   GtkWidget         *widget);
115
static void gtk_handle_box_draw_ghost     (GtkHandleBox      *hb);
116 117 118 119 120 121 122 123 124
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);
125 126
static gint gtk_handle_box_delete_event   (GtkWidget         *widget,
					   GdkEventAny       *event);
127
static void gtk_handle_box_reattach       (GtkHandleBox      *hb);
128 129 130


static GtkBinClass *parent_class;
131
static guint        handle_box_signals[SIGNAL_LAST] = { 0 };
132 133


Tor Lillqvist's avatar
Tor Lillqvist committed
134
GtkType
135
gtk_handle_box_get_type (void)
136
{
Tor Lillqvist's avatar
Tor Lillqvist committed
137
  static GtkType handle_box_type = 0;
138

Elliot Lee's avatar
Elliot Lee committed
139
  if (!handle_box_type)
140
    {
141
      static const GtkTypeInfo handle_box_info =
142 143 144 145 146 147
      {
	"GtkHandleBox",
	sizeof (GtkHandleBox),
	sizeof (GtkHandleBoxClass),
	(GtkClassInitFunc) gtk_handle_box_class_init,
	(GtkObjectInitFunc) gtk_handle_box_init,
148 149
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
150
        (GtkClassInitFunc) NULL,
151 152
      };

153
      handle_box_type = gtk_type_unique (GTK_TYPE_BIN, &handle_box_info);
154 155
    }

156
  return handle_box_type;
157 158 159 160 161
}

static void
gtk_handle_box_class_init (GtkHandleBoxClass *class)
{
162
  GtkObjectClass *object_class;
163 164
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;
165 166 167

  object_class = (GtkObjectClass *) class;
  widget_class = (GtkWidgetClass *) class;
168
  container_class = (GtkContainerClass *) class;
169

170 171
  parent_class = gtk_type_class (GTK_TYPE_BIN);
  
172
  gtk_object_add_arg_type ("GtkHandleBox::shadow", GTK_TYPE_SHADOW_TYPE, GTK_ARG_READWRITE, ARG_SHADOW);
173 174
  gtk_object_add_arg_type ("GtkHandleBox::handle_position", GTK_TYPE_POSITION_TYPE, GTK_ARG_READWRITE, ARG_HANDLE_POSITION);
  gtk_object_add_arg_type ("GtkHandleBox::snap_edge", GTK_TYPE_POSITION_TYPE, GTK_ARG_READWRITE, ARG_SNAP_EDGE);
175 176 177 178

  object_class->set_arg = gtk_handle_box_set_arg;
  object_class->get_arg = gtk_handle_box_get_arg;
  
179
  
180 181 182 183
  object_class->destroy = gtk_handle_box_destroy;

  widget_class->map = gtk_handle_box_map;
  widget_class->unmap = gtk_handle_box_unmap;
184
  widget_class->realize = gtk_handle_box_realize;
185
  widget_class->unrealize = gtk_handle_box_unrealize;
Owen Taylor's avatar
Owen Taylor committed
186
  widget_class->style_set = gtk_handle_box_style_set;
187 188 189
  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
190 191
  widget_class->button_press_event = gtk_handle_box_button_changed;
  widget_class->button_release_event = gtk_handle_box_button_changed;
192
  widget_class->motion_notify_event = gtk_handle_box_motion;
193
  widget_class->delete_event = gtk_handle_box_delete_event;
194

195
  container_class->add = gtk_handle_box_add;
196 197
  container_class->remove = gtk_handle_box_remove;

198 199
  class->child_attached = NULL;
  class->child_detached = NULL;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

  handle_box_signals[SIGNAL_CHILD_ATTACHED] =
    gtk_signal_new ("child_attached",
		    GTK_RUN_FIRST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkHandleBoxClass, child_attached),
		    gtk_marshal_VOID__POINTER,
		    GTK_TYPE_NONE, 1,
		    GTK_TYPE_WIDGET);
  handle_box_signals[SIGNAL_CHILD_DETACHED] =
    gtk_signal_new ("child_detached",
		    GTK_RUN_FIRST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkHandleBoxClass, child_detached),
		    gtk_marshal_VOID__POINTER,
		    GTK_TYPE_NONE, 1,
		    GTK_TYPE_WIDGET);
217 218 219
}

static void
220
gtk_handle_box_init (GtkHandleBox *handle_box)
221
{
222
  GTK_WIDGET_UNSET_FLAGS (handle_box, GTK_NO_WINDOW);
223

224
  handle_box->bin_window = NULL;
225
  handle_box->float_window = NULL;
226
  handle_box->shadow_type = GTK_SHADOW_OUT;
227 228 229 230
  handle_box->handle_position = GTK_POS_LEFT;
  handle_box->float_window_mapped = FALSE;
  handle_box->child_detached = FALSE;
  handle_box->in_drag = FALSE;
231
  handle_box->shrink_on_detach = TRUE;
232
  handle_box->snap_edge = -1;
233 234
}

235
static void
236 237 238
gtk_handle_box_set_arg (GtkObject *object,
			GtkArg    *arg,
			guint      arg_id)
239
{
240
  GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
241 242 243 244 245 246

  switch (arg_id)
    {
    case ARG_SHADOW:
      gtk_handle_box_set_shadow_type (handle_box, GTK_VALUE_ENUM (*arg));
      break;
247 248 249 250 251
    case ARG_HANDLE_POSITION:
      gtk_handle_box_set_handle_position (handle_box, GTK_VALUE_ENUM (*arg));
      break;
    case ARG_SNAP_EDGE:
      gtk_handle_box_set_snap_edge (handle_box, GTK_VALUE_ENUM (*arg));
252 253 254 255 256
      break;
    }
}

static void
257 258 259
gtk_handle_box_get_arg (GtkObject *object,
			GtkArg    *arg,
			guint      arg_id)
260
{
261 262
  GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
  
263 264 265 266 267
  switch (arg_id)
    {
    case ARG_SHADOW:
      GTK_VALUE_ENUM (*arg) = handle_box->shadow_type;
      break;
268 269 270 271 272 273
    case ARG_HANDLE_POSITION:
      GTK_VALUE_ENUM (*arg) = handle_box->handle_position;
      break;
    case ARG_SNAP_EDGE:
      GTK_VALUE_ENUM (*arg) = handle_box->snap_edge;
      break;
274 275 276 277 278 279
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}
 
280
GtkWidget*
281 282 283 284 285 286 287
gtk_handle_box_new (void)
{
  return GTK_WIDGET (gtk_type_new (gtk_handle_box_get_type ()));
}

static void
gtk_handle_box_destroy (GtkObject *object)
288
{
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
  GtkHandleBox *hb;

  g_return_if_fail (object != NULL);
  g_return_if_fail (GTK_IS_HANDLE_BOX (object));

  hb = GTK_HANDLE_BOX (object);

  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;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_HANDLE_BOX (widget));

  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);

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

314 315 316 317
  if (bin->child &&
      GTK_WIDGET_VISIBLE (bin->child) &&
      !GTK_WIDGET_MAPPED (bin->child))
    gtk_widget_map (bin->child);
318

319 320 321 322 323 324
  if (hb->child_detached && !hb->float_window_mapped)
    {
      gdk_window_show (hb->float_window);
      hb->float_window_mapped = TRUE;
    }

325 326
  gdk_window_show (hb->bin_window);
  gdk_window_show (widget->window);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
}

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

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_HANDLE_BOX (widget));

  GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);

  hb = GTK_HANDLE_BOX (widget);

  gdk_window_hide (widget->window);
342 343 344 345 346
  if (hb->float_window_mapped)
    {
      gdk_window_hide (hb->float_window);
      hb->float_window_mapped = FALSE;
    }
347 348 349 350 351 352 353
}

static void
gtk_handle_box_realize (GtkWidget *widget)
{
  GdkWindowAttr attributes;
  gint attributes_mask;
354
  GtkHandleBox *hb;
355 356 357 358

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_HANDLE_BOX (widget));

359 360
  hb = GTK_HANDLE_BOX (widget);

361 362 363 364 365 366 367 368 369 370
  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);
371 372
  attributes.event_mask = (gtk_widget_get_events (widget)
			   | GDK_EXPOSURE_MASK);
373
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
374 375
  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, widget);
376 377 378

  attributes.x = 0;
  attributes.y = 0;
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.event_mask |= (gtk_widget_get_events (widget) |
			    GDK_EXPOSURE_MASK |
			    GDK_BUTTON1_MOTION_MASK |
			    GDK_POINTER_MOTION_HINT_MASK |
			    GDK_BUTTON_PRESS_MASK |
			    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;
  hb->float_window = gdk_window_new (NULL, &attributes, attributes_mask);
  gdk_window_set_user_data (hb->float_window, widget);
  gdk_window_set_decorations (hb->float_window, 0);
  
413
  widget->style = gtk_style_attach (widget->style, widget->window);
414 415 416
  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));
417
  gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
418 419
}

420 421 422 423 424 425 426 427 428 429
static void
gtk_handle_box_unrealize (GtkWidget *widget)
{
  GtkHandleBox *hb;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_HANDLE_BOX (widget));

  hb = GTK_HANDLE_BOX (widget);

430 431 432 433 434
  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);
435
  hb->float_window = NULL;
436 437 438

  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
439 440
}

Owen Taylor's avatar
Owen Taylor committed
441 442 443 444 445
static void
gtk_handle_box_style_set (GtkWidget *widget,
			  GtkStyle  *previous_style)
{
  GtkHandleBox *hb;
446
  GdkRectangle new_area;
Owen Taylor's avatar
Owen Taylor committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_HANDLE_BOX (widget));

  hb = GTK_HANDLE_BOX (widget);

  if (GTK_WIDGET_REALIZED (widget) &&
      !GTK_WIDGET_NO_WINDOW (widget))
    {
      gtk_style_set_background (widget->style, widget->window,
widget->state);
      gtk_style_set_background (widget->style, hb->bin_window, widget->state);
      gtk_style_set_background (widget->style, hb->float_window, widget->state);
    }
}

463 464
static void
gtk_handle_box_size_request (GtkWidget      *widget,
Elliot Lee's avatar
Elliot Lee committed
465
			     GtkRequisition *requisition)
466 467
{
  GtkBin *bin;
468
  GtkHandleBox *hb;
469
  GtkRequisition child_requisition;
470 471 472 473 474 475

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
  g_return_if_fail (requisition != NULL);

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

478 479 480 481 482 483 484
  if (hb->handle_position == GTK_POS_LEFT ||
      hb->handle_position == GTK_POS_RIGHT)
    {
      requisition->width = DRAG_HANDLE_SIZE;
      requisition->height = 0;
    }
  else
485
    {
486 487 488
      requisition->width = 0;
      requisition->height = DRAG_HANDLE_SIZE;
    }
489

490 491 492 493
  /* 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)
494
    gtk_widget_size_request (bin->child, &child_requisition);
495 496 497 498 499
  else
    {
      child_requisition.width = 0;
      child_requisition.height = 0;
    }      
500 501 502

  if (hb->child_detached)
    {
503
      /* FIXME: This doesn't work currently */
504 505 506 507
      if (!hb->shrink_on_detach)
	{
	  if (hb->handle_position == GTK_POS_LEFT ||
	      hb->handle_position == GTK_POS_RIGHT)
508
	    requisition->height += child_requisition.height;
509
	  else
510
	    requisition->width += child_requisition.width;
511
	}
512
      else
513 514 515
	{
	  if (hb->handle_position == GTK_POS_LEFT ||
	      hb->handle_position == GTK_POS_RIGHT)
516
	    requisition->height += widget->style->ythickness;
517
	  else
518
	    requisition->width += widget->style->xthickness;
519
	}
520
    }
521 522
  else
    {
523 524 525 526 527
      requisition->width += GTK_CONTAINER (widget)->border_width * 2;
      requisition->height += GTK_CONTAINER (widget)->border_width * 2;
      
      if (bin->child)
	{
528 529
	  requisition->width += child_requisition.width;
	  requisition->height += child_requisition.height;
530 531 532 533 534 535
	}
      else
	{
	  requisition->width += CHILDLESS_SIZE;
	  requisition->height += CHILDLESS_SIZE;
	}
536
    }
537 538 539 540
}

static void
gtk_handle_box_size_allocate (GtkWidget     *widget,
541
			      GtkAllocation *allocation)
542 543
{
  GtkBin *bin;
544
  GtkHandleBox *hb;
545
  GtkRequisition child_requisition;
546
  
547 548
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_HANDLE_BOX (widget));
549 550
  g_return_if_fail (allocation != NULL);
  
551
  bin = GTK_BIN (widget);
552
  hb = GTK_HANDLE_BOX (widget);
553
  
554 555 556 557 558 559
  if (bin->child)
    gtk_widget_get_child_requisition (bin->child, &child_requisition);
  else
    {
      child_requisition.width = 0;
      child_requisition.height = 0;
560
    }      
561
      
562
  widget->allocation = *allocation;
563

564 565 566 567 568 569
  if (GTK_WIDGET_REALIZED (hb))
    gdk_window_move_resize (widget->window,
			    widget->allocation.x,
			    widget->allocation.y,
			    widget->allocation.width,
			    widget->allocation.height);
570

571 572 573

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    {
574
      GtkWidget *child;
575
      GtkAllocation child_allocation;
576 577 578 579
      guint border_width;

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

581 582 583 584 585 586
      child_allocation.x = border_width;
      child_allocation.y = border_width;
      if (hb->handle_position == GTK_POS_LEFT)
	child_allocation.x += DRAG_HANDLE_SIZE;
      else if (hb->handle_position == GTK_POS_TOP)
	child_allocation.y += DRAG_HANDLE_SIZE;
587

588
      if (hb->child_detached)
589
	{
590 591 592
	  guint float_width;
	  guint float_height;
	  
593 594
	  child_allocation.width = child_requisition.width;
	  child_allocation.height = child_requisition.height;
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	  
	  float_width = child_allocation.width + 2 * border_width;
	  float_height = child_allocation.height + 2 * border_width;
	  
	  if (hb->handle_position == GTK_POS_LEFT ||
	      hb->handle_position == GTK_POS_RIGHT)
	    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);
	    }
616 617 618
	}
      else
	{
619 620
	  child_allocation.width = MAX (1, (gint)widget->allocation.width - 2 * border_width);
	  child_allocation.height = MAX (1, (gint)widget->allocation.height - 2 * border_width);
621 622 623 624 625 626 627 628 629 630 631 632 633

	  if (hb->handle_position == GTK_POS_LEFT ||
	      hb->handle_position == GTK_POS_RIGHT)
	    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);
634 635
	}

636
      gtk_widget_size_allocate (bin->child, &child_allocation);
637
    }
638
}
639

640
static void
641
gtk_handle_box_draw_ghost (GtkHandleBox *hb)
642
{
643 644 645 646 647
  GtkWidget *widget;
  guint x;
  guint y;
  guint width;
  guint height;
648

649
  widget = GTK_WIDGET (hb);
650

651 652
  if (hb->handle_position == GTK_POS_LEFT ||
      hb->handle_position == GTK_POS_RIGHT)
653
    {
654
      x = hb->handle_position == GTK_POS_LEFT ? 0 : widget->allocation.width - DRAG_HANDLE_SIZE;
655 656 657
      y = 0;
      width = DRAG_HANDLE_SIZE;
      height = widget->allocation.height;
658
    }
659 660 661
  else
    {
      x = 0;
662
      y = hb->handle_position == GTK_POS_TOP ? 0 : widget->allocation.height - DRAG_HANDLE_SIZE;
663 664 665
      width = widget->allocation.width;
      height = DRAG_HANDLE_SIZE;
    }
666
  gtk_paint_shadow (widget->style,
667 668
		    widget->window,
		    GTK_WIDGET_STATE (widget),
669 670 671 672 673 674 675 676 677 678 679 680
		    GTK_SHADOW_ETCHED_IN,
		    NULL, widget, "handle",
		    x,
		    y,
		    width,
		    height);
   if (hb->handle_position == GTK_POS_LEFT ||
       hb->handle_position == GTK_POS_RIGHT)
     gtk_paint_hline (widget->style,
		      widget->window,
		      GTK_WIDGET_STATE (widget),
		      NULL, widget, "handlebox",
681 682
		      hb->handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
		      hb->handle_position == GTK_POS_LEFT ? widget->allocation.width : widget->allocation.width - DRAG_HANDLE_SIZE,
683 684 685 686 687 688
		      widget->allocation.height / 2);
   else
     gtk_paint_vline (widget->style,
		      widget->window,
		      GTK_WIDGET_STATE (widget),
		      NULL, widget, "handlebox",
689 690
		      hb->handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
		      hb->handle_position == GTK_POS_TOP ? widget->allocation.height : widget->allocation.height - DRAG_HANDLE_SIZE,
691
		      widget->allocation.width / 2);
692 693
}

694
static void
695 696
draw_textured_frame (GtkWidget *widget, GdkWindow *window, GdkRectangle *rect, GtkShadowType shadow,
		     GdkRectangle *clip)
697
{
698
   gtk_paint_handle(widget->style, window, GTK_STATE_NORMAL, shadow,
699
		    clip, widget, "handlebox",
700 701
		    rect->x, rect->y, rect->width, rect->height, 
		    GTK_ORIENTATION_VERTICAL);
702 703
}

704 705 706 707 708 709 710 711 712 713
void
gtk_handle_box_set_shadow_type (GtkHandleBox  *handle_box,
				GtkShadowType  type)
{
  g_return_if_fail (handle_box != NULL);
  g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));

  if ((GtkShadowType) handle_box->shadow_type != type)
    {
      handle_box->shadow_type = type;
714 715 716
      gtk_widget_queue_resize (GTK_WIDGET (handle_box));
    }
}
717

718 719 720 721 722 723 724
void        
gtk_handle_box_set_handle_position  (GtkHandleBox    *handle_box,
				     GtkPositionType  position)
{
  if ((GtkPositionType) handle_box->handle_position != position)
    {
      handle_box->handle_position = position;
725 726 727 728
      gtk_widget_queue_resize (GTK_WIDGET (handle_box));
    }
}

729 730 731 732 733 734 735 736 737 738
void        
gtk_handle_box_set_snap_edge        (GtkHandleBox    *handle_box,
				     GtkPositionType  edge)
{
  g_return_if_fail (handle_box != NULL);
  g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));

  handle_box->snap_edge = edge;
}

739
static void
740 741 742
gtk_handle_box_paint (GtkWidget      *widget,
		      GdkEventExpose *event,
		      GdkRectangle   *area)
Elliot Lee's avatar
Elliot Lee committed
743
{
744
  GtkBin *bin;
745
  GtkHandleBox *hb;
746 747
  guint width;
  guint height;
748
  GdkRectangle rect;
749
  GdkRectangle dest;
750

751 752
  bin = GTK_BIN (widget);
  hb = GTK_HANDLE_BOX (widget);
753

754 755
  gdk_window_get_size (hb->bin_window, &width, &height);
  
756
  if (!event)
757 758 759
   gtk_paint_box(widget->style,
		 hb->bin_window,
		 GTK_WIDGET_STATE (widget),
760
		 hb->shadow_type,
761 762 763 764 765 766
		 area, widget, "handlebox_bin",
		 0, 0, -1, -1);
  else
   gtk_paint_box(widget->style,
		 hb->bin_window,
		 GTK_WIDGET_STATE (widget),
767
		 hb->shadow_type,
768 769 770 771 772 773 774 775 776 777
		 &event->area, widget, "handlebox_bin",
		 0, 0, -1, -1);

/* 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);*/
778

779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
  switch (hb->handle_position)
    {
    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;
    }
806

807 808 809 810
  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);
811 812

  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
813
    {
814 815 816 817
      GdkRectangle child_area;
      GdkEventExpose child_event;

      if (!event) /* we were called from draw() */
818 819 820 821 822 823 824 825
	{
	  if (gtk_widget_intersect (bin->child, area, &child_area))
	    gtk_widget_draw (bin->child, &child_area);
	}
      else /* we were called from expose() */
	{
	  child_event = *event;
	  
826 827
	  if (GTK_WIDGET_NO_WINDOW (bin->child) &&
	      gtk_widget_intersect (bin->child, &event->area, &child_event.area))
828 829 830
	    gtk_widget_event (bin->child, (GdkEvent *) &child_event);
	}
    }
Elliot Lee's avatar
Elliot Lee committed
831 832
}

833 834
static gint
gtk_handle_box_expose (GtkWidget      *widget,
Elliot Lee's avatar
Elliot Lee committed
835
		       GdkEventExpose *event)
836
{
837
  GtkHandleBox *hb;
838 839 840 841 842 843 844

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  if (GTK_WIDGET_DRAWABLE (widget))
    {
845 846
      hb = GTK_HANDLE_BOX (widget);

847 848 849 850 851 852
      if (event->window == widget->window)
	{
	  if (hb->child_detached)
	    gtk_handle_box_draw_ghost (hb);
	}
      else
853
	gtk_handle_box_paint (widget, event, NULL);
854
    }
855
  
856 857 858
  return FALSE;
}

859
static gint
860 861
gtk_handle_box_button_changed (GtkWidget      *widget,
			       GdkEventButton *event)
862 863
{
  GtkHandleBox *hb;
864
  gboolean event_handled;
865
  GdkCursor *fleur;
866
  
867 868 869 870 871 872
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  hb = GTK_HANDLE_BOX (widget);

873
  event_handled = FALSE;
874 875
  if ((event->button == 1) && 
      (event->type == GDK_BUTTON_PRESS || event->type == GDK_2BUTTON_PRESS))
876
    {
877 878 879
      GtkWidget *child;
      gboolean in_handle;
      
880 881 882
      if (event->window != hb->bin_window)
	return FALSE;

883
      child = GTK_BIN (hb)->child;
884 885

      if (child)
886
	{
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
	  switch (hb->handle_position)
	    {
	    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;
	    }
905
	}
906
      else
907 908 909 910 911 912
	{
	  in_handle = FALSE;
	  event_handled = TRUE;
	}
      
      if (in_handle)
913
	{
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
	  if (event->type == GDK_BUTTON_PRESS) /* Start a drag */
	    {
	      gint desk_x, desk_y;
	      gint root_x, root_y;
	      gint width, height;
	      
	      gdk_window_get_deskrelative_origin (hb->bin_window, &desk_x, &desk_y);
	      gdk_window_get_origin (hb->bin_window, &root_x, &root_y);
	      gdk_window_get_size (hb->bin_window, &width, &height);
	      
	      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);
	      gdk_window_get_size (widget->window, &width, &height);
	      
	      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;
	      fleur = gdk_cursor_new (GDK_FLEUR);
	      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;
		}
	      
	      gdk_cursor_destroy (fleur);
	      event_handled = TRUE;
	    }
	  else if (hb->child_detached) /* Double click */
	    {
	      gtk_handle_box_reattach (hb);
	    }
961 962
	}
    }
963 964 965
  else if (event->type == GDK_BUTTON_RELEASE &&
	   hb->in_drag)
    {
966 967 968
      if (event->window != widget->window)
	return FALSE;
      
969 970 971 972 973 974
      gdk_pointer_ungrab (GDK_CURRENT_TIME);
      hb->in_drag = FALSE;
      event_handled = TRUE;
    }
  
  return event_handled;
975 976
}

977 978 979
static gint
gtk_handle_box_motion (GtkWidget      *widget,
		       GdkEventMotion *event)
980 981
{
  GtkHandleBox *hb;
982
  gint new_x, new_y;
983 984
  gint snap_edge;
  gboolean is_snapped = FALSE;
985 986 987 988

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_HANDLE_BOX (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
989

990
  hb = GTK_HANDLE_BOX (widget);
991 992 993
  if (!hb->in_drag)
    return FALSE;

994
  if (!hb->in_drag || (event->window != widget->window))
995
    return FALSE;
996 997 998 999
  
  /* Calculate the attachment point on the float, if the float
   * were detached
   */
1000 1001
  new_x = 0;
  new_y = 0;
1002
  gdk_window_get_pointer (NULL, &new_x, &new_y, NULL);
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
  new_x += hb->float_allocation.x;
  new_y += hb->float_allocation.y;

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

  /* First, check if the snapped edge is aligned
   */
  switch (snap_edge)
1015
    {