gtkhandlebox.c 47.7 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2
 * 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
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
/*
20
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
21 22 23 24
 * 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/. 
 */
25

26
#include "config.h"
27

28
#include <stdlib.h>
29

30
#define GDK_DISABLE_DEPRECATION_WARNINGS
Benjamin Otte's avatar
Benjamin Otte committed
31

32
#include "gtkhandlebox.h"
33
#include "gtkinvisible.h"
34
#include "gtkmain.h"
35
#include "gtkmarshalers.h"
36
#include "gtkrender.h"
37
#include "gtkwindow.h"
38
#include "gtktypebuiltins.h"
39
#include "gtkprivate.h"
40
#include "gtkintl.h"
41

42

43 44 45 46 47 48 49
/**
 * SECTION:gtkhandlebox
 * @Short_description: a widget for detachable window portions
 * @Title: GtkHandleBox
 *
 * The #GtkHandleBox widget allows a portion of a window to be "torn
 * off". It is a bin widget which displays its child and a handle that
50 51 52
 * the user can drag to tear off a separate window (the “float
 * window”) containing the child widget. A thin
 * “ghost” is drawn in the original location of the
53 54 55 56
 * handlebox. By dragging the separate window back to its original
 * location, it can be reattached.
 *
 * When reattaching, the ghost and float window, must be aligned
57
 * along one of the edges, the “snap edge”.
58
 * This either can be specified by the application programmer
59
 * explicitly, or GTK+ will pick a reasonable default based
60 61 62 63 64 65 66 67 68
 * on the handle position.
 *
 * To make detaching and reattaching the handlebox as minimally confusing
 * as possible to the user, it is important to set the snap edge so that
 * the snap edge does not move when the handlebox is deattached. For
 * instance, if the handlebox is packed at the bottom of a VBox, then
 * when the handlebox is detached, the bottom edge of the handlebox's
 * allocation will remain fixed as the height of the handlebox shrinks,
 * so the snap edge should be set to %GTK_POS_BOTTOM.
Benjamin Otte's avatar
Benjamin Otte committed
69
 *
70 71 72
 * > #GtkHandleBox has been deprecated. It is very specialized, lacks features
 * > to make it useful and most importantly does not fit well into modern
 * > application design. Do not use it. There is no replacement.
73 74
 */

75

76
struct _GtkHandleBoxPrivate
77
{
78 79
  /* Properties */
  GtkPositionType handle_position;
80
  gint snap_edge;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  GtkShadowType   shadow_type;
  gboolean        child_detached;
  /* Properties */

  GtkAllocation   attach_allocation;
  GtkAllocation   float_allocation;

  GdkDevice      *grab_device;

  GdkWindow      *bin_window;     /* parent window for children */
  GdkWindow      *float_window;

  /* Variables used during a drag
   */
  gint            orig_x;
  gint            orig_y;

  guint           float_window_mapped : 1;
  guint           in_drag : 1;
  guint           shrink_on_detach : 1;
101 102
};

103
enum {
104
  PROP_0,
105
  PROP_SHADOW_TYPE,
106
  PROP_HANDLE_POSITION,
107
  PROP_SNAP_EDGE,
Tim Janik's avatar
Tim Janik committed
108 109
  PROP_SNAP_EDGE_SET,
  PROP_CHILD_DETACHED
110
};
111

Elliot Lee's avatar
Elliot Lee committed
112
#define DRAG_HANDLE_SIZE 10
113
#define CHILDLESS_SIZE	25
114
#define GHOST_HEIGHT 3
115
#define TOLERANCE 5
116

117
enum {
118 119 120 121 122
  SIGNAL_CHILD_ATTACHED,
  SIGNAL_CHILD_DETACHED,
  SIGNAL_LAST
};

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/* 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.
 *
147
 * 2) These rectangles must have one edge, the “snap_edge”
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
 *    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-------------------->
 */

163 164 165 166 167 168 169 170 171 172 173 174
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);
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);
175
static void     gtk_handle_box_style_updated (GtkWidget      *widget);
176 177
static void     gtk_handle_box_size_request  (GtkWidget      *widget,
                                              GtkRequisition *requisition);
178 179 180 181 182 183
static void     gtk_handle_box_get_preferred_width (GtkWidget *widget,
						    gint      *minimum,
						    gint      *natural);
static void     gtk_handle_box_get_preferred_height (GtkWidget *widget,
						    gint      *minimum,
						    gint      *natural);
184 185 186 187 188 189
static void     gtk_handle_box_size_allocate (GtkWidget      *widget,
                                              GtkAllocation  *real_allocation);
static void     gtk_handle_box_add           (GtkContainer   *container,
                                              GtkWidget      *widget);
static void     gtk_handle_box_remove        (GtkContainer   *container,
                                              GtkWidget      *widget);
190 191
static gboolean gtk_handle_box_draw          (GtkWidget      *widget,
                                              cairo_t        *cr);
192 193 194 195 196 197 198 199 200 201 202
static gboolean gtk_handle_box_button_press  (GtkWidget      *widget,
                                              GdkEventButton *event);
static gboolean gtk_handle_box_motion        (GtkWidget      *widget,
                                              GdkEventMotion *event);
static gboolean gtk_handle_box_delete_event  (GtkWidget      *widget,
                                              GdkEventAny    *event);
static void     gtk_handle_box_reattach      (GtkHandleBox   *hb);
static void     gtk_handle_box_end_drag      (GtkHandleBox   *hb,
                                              guint32         time);

static guint handle_box_signals[SIGNAL_LAST] = { 0 };
203

204
G_DEFINE_TYPE_WITH_PRIVATE (GtkHandleBox, gtk_handle_box, GTK_TYPE_BIN)
205 206 207 208

static void
gtk_handle_box_class_init (GtkHandleBoxClass *class)
{
209
  GObjectClass *gobject_class;
210 211
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;
212

213
  gobject_class = (GObjectClass *) class;
214
  widget_class = (GtkWidgetClass *) class;
215
  container_class = (GtkContainerClass *) class;
216

217 218 219
  gobject_class->set_property = gtk_handle_box_set_property;
  gobject_class->get_property = gtk_handle_box_get_property;
  
220 221
  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW_TYPE,
222
                                   g_param_spec_enum ("shadow-type",
223 224
                                                      P_("Shadow type"),
                                                      P_("Appearance of the shadow that surrounds the container"),
225
						      GTK_TYPE_SHADOW_TYPE,
226
						      GTK_SHADOW_OUT,
227
                                                      GTK_PARAM_READWRITE));
228
  
229 230
  g_object_class_install_property (gobject_class,
                                   PROP_HANDLE_POSITION,
231
                                   g_param_spec_enum ("handle-position",
232 233
                                                      P_("Handle position"),
                                                      P_("Position of the handle relative to the child widget"),
234 235
						      GTK_TYPE_POSITION_TYPE,
						      GTK_POS_LEFT,
236
                                                      GTK_PARAM_READWRITE));
237
  
238 239
  g_object_class_install_property (gobject_class,
                                   PROP_SNAP_EDGE,
240
                                   g_param_spec_enum ("snap-edge",
241 242
                                                      P_("Snap edge"),
                                                      P_("Side of the handlebox that's lined up with the docking point to dock the handlebox"),
243
						      GTK_TYPE_POSITION_TYPE,
244
						      GTK_POS_TOP,
245
                                                      GTK_PARAM_READWRITE));
Manish Singh's avatar
Manish Singh committed
246

247 248
  g_object_class_install_property (gobject_class,
                                   PROP_SNAP_EDGE_SET,
249
                                   g_param_spec_boolean ("snap-edge-set",
250 251
							 P_("Snap edge set"),
							 P_("Whether to use the value from the snap_edge property or a value derived from handle_position"),
252
							 FALSE,
253
							 GTK_PARAM_READWRITE));
254

Tim Janik's avatar
Tim Janik committed
255 256 257 258 259 260 261 262
  g_object_class_install_property (gobject_class,
                                   PROP_CHILD_DETACHED,
                                   g_param_spec_boolean ("child-detached",
							 P_("Child Detached"),
							 P_("A boolean value indicating whether the handlebox's child is attached or detached."),
							 FALSE,
							 GTK_PARAM_READABLE));

263 264
  widget_class->map = gtk_handle_box_map;
  widget_class->unmap = gtk_handle_box_unmap;
265
  widget_class->realize = gtk_handle_box_realize;
266
  widget_class->unrealize = gtk_handle_box_unrealize;
267
  widget_class->style_updated = gtk_handle_box_style_updated;
268 269
  widget_class->get_preferred_width = gtk_handle_box_get_preferred_width;
  widget_class->get_preferred_height = gtk_handle_box_get_preferred_height;
270
  widget_class->size_allocate = gtk_handle_box_size_allocate;
271
  widget_class->draw = gtk_handle_box_draw;
272
  widget_class->button_press_event = gtk_handle_box_button_press;
273
  widget_class->delete_event = gtk_handle_box_delete_event;
274

275
  container_class->add = gtk_handle_box_add;
276 277
  container_class->remove = gtk_handle_box_remove;

278 279
  class->child_attached = NULL;
  class->child_detached = NULL;
280

281 282 283 284 285 286 287 288 289
  /**
   * GtkHandleBox::child-attached:
   * @handlebox: the object which received the signal.
   * @widget: the child widget of the handlebox.
   *   (this argument provides no extra information
   *   and is here only for backwards-compatibility)
   *
   * This signal is emitted when the contents of the
   * handlebox are reattached to the main window.
Benjamin Otte's avatar
Benjamin Otte committed
290 291
   *
   * Deprecated: 3.4: #GtkHandleBox has been deprecated.
292
   */
293
  handle_box_signals[SIGNAL_CHILD_ATTACHED] =
294
    g_signal_new (I_("child-attached"),
Manish Singh's avatar
Manish Singh committed
295 296 297 298 299 300 301
		  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);
302 303 304 305 306 307 308 309 310 311

  /**
   * GtkHandleBox::child-detached:
   * @handlebox: the object which received the signal.
   * @widget: the child widget of the handlebox.
   *   (this argument provides no extra information
   *   and is here only for backwards-compatibility)
   *
   * This signal is emitted when the contents of the
   * handlebox are detached from the main window.
Benjamin Otte's avatar
Benjamin Otte committed
312 313
   *
   * Deprecated: 3.4: #GtkHandleBox has been deprecated.
314
   */
315
  handle_box_signals[SIGNAL_CHILD_DETACHED] =
316
    g_signal_new (I_("child-detached"),
Manish Singh's avatar
Manish Singh committed
317 318 319 320 321 322 323
		  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);
324 325
}

326
static void
327
gtk_handle_box_init (GtkHandleBox *handle_box)
328
{
329
  GtkHandleBoxPrivate *priv;
330
  GtkStyleContext *context;
331

332
  handle_box->priv = gtk_handle_box_get_instance_private (handle_box);
333 334
  priv = handle_box->priv;

335
  gtk_widget_set_has_window (GTK_WIDGET (handle_box), TRUE);
336

337 338 339 340 341 342 343 344 345
  priv->bin_window = NULL;
  priv->float_window = NULL;
  priv->shadow_type = GTK_SHADOW_OUT;
  priv->handle_position = GTK_POS_LEFT;
  priv->float_window_mapped = FALSE;
  priv->child_detached = FALSE;
  priv->in_drag = FALSE;
  priv->shrink_on_detach = TRUE;
  priv->snap_edge = -1;
346 347 348

  context = gtk_widget_get_style_context (GTK_WIDGET (handle_box));
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_DOCK);
349 350
}

351 352 353 354 355
static void 
gtk_handle_box_set_property (GObject         *object,
			     guint            prop_id,
			     const GValue    *value,
			     GParamSpec      *pspec)
356
{
357
  GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
358

359
  switch (prop_id)
360
    {
361
    case PROP_SHADOW_TYPE:
362 363 364 365
      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));
366
      break;
367 368
    case PROP_SNAP_EDGE:
      gtk_handle_box_set_snap_edge (handle_box, g_value_get_enum (value));
369
      break;
370 371 372 373
    case PROP_SNAP_EDGE_SET:
      if (!g_value_get_boolean (value))
	gtk_handle_box_set_snap_edge (handle_box, (GtkPositionType)-1);
      break;
374 375
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
376 377 378 379
      break;
    }
}

380 381 382 383 384
static void 
gtk_handle_box_get_property (GObject         *object,
			     guint            prop_id,
			     GValue          *value,
			     GParamSpec      *pspec)
385
{
386
  GtkHandleBox *handle_box = GTK_HANDLE_BOX (object);
387
  GtkHandleBoxPrivate *priv = handle_box->priv;
388

389
  switch (prop_id)
390
    {
391
    case PROP_SHADOW_TYPE:
392
      g_value_set_enum (value, priv->shadow_type);
393
      break;
394
    case PROP_HANDLE_POSITION:
395
      g_value_set_enum (value, priv->handle_position);
396
      break;
397
    case PROP_SNAP_EDGE:
398
      g_value_set_enum (value,
399 400
			(priv->snap_edge == -1 ?
			 GTK_POS_TOP : priv->snap_edge));
401 402
      break;
    case PROP_SNAP_EDGE_SET:
403
      g_value_set_boolean (value, priv->snap_edge != -1);
404
      break;
Tim Janik's avatar
Tim Janik committed
405
    case PROP_CHILD_DETACHED:
406
      g_value_set_boolean (value, priv->child_detached);
Tim Janik's avatar
Tim Janik committed
407
      break;
408
    default:
409
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
410 411 412
      break;
    }
}
413 414 415 416 417 418 419

/**
 * gtk_handle_box_new:
 *
 * Create a new handle box.
 *
 * Returns: a new #GtkHandleBox.
Benjamin Otte's avatar
Benjamin Otte committed
420 421
 *
 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
422
 */
423
GtkWidget*
424 425
gtk_handle_box_new (void)
{
Manish Singh's avatar
Manish Singh committed
426
  return g_object_new (GTK_TYPE_HANDLE_BOX, NULL);
427 428 429 430 431
}

static void
gtk_handle_box_map (GtkWidget *widget)
{
432
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
433
  GtkHandleBoxPrivate *priv = hb->priv;
434
  GtkBin *bin = GTK_BIN (widget);
Javier Jardón's avatar
Javier Jardón committed
435
  GtkWidget *child;
436

437
  gtk_widget_set_mapped (widget, TRUE);
438

Javier Jardón's avatar
Javier Jardón committed
439
  child = gtk_bin_get_child (bin);
440 441
  if (child != NULL &&
      gtk_widget_get_visible (child) &&
Javier Jardón's avatar
Javier Jardón committed
442 443
      !gtk_widget_get_mapped (child))
    gtk_widget_map (child);
444

445
  if (priv->child_detached && !priv->float_window_mapped)
446
    {
447 448
      gdk_window_show (priv->float_window);
      priv->float_window_mapped = TRUE;
449 450
    }

451
  gdk_window_show (priv->bin_window);
452
  gdk_window_show (gtk_widget_get_window (widget));
453 454 455 456 457
}

static void
gtk_handle_box_unmap (GtkWidget *widget)
{
458
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
459
  GtkHandleBoxPrivate *priv = hb->priv;
460

461
  gtk_widget_set_mapped (widget, FALSE);
462

463
  gdk_window_hide (gtk_widget_get_window (widget));
464
  if (priv->float_window_mapped)
465
    {
466 467
      gdk_window_hide (priv->float_window);
      priv->float_window_mapped = FALSE;
468
    }
469 470

  GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unmap (widget);
471 472 473 474 475
}

static void
gtk_handle_box_realize (GtkWidget *widget)
{
476
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
477
  GtkHandleBoxPrivate *priv = hb->priv;
478 479
  GtkAllocation allocation;
  GtkRequisition requisition;
480
  GtkStyleContext *context;
Javier Jardón's avatar
Javier Jardón committed
481
  GtkWidget *child;
482
  GdkWindow *window;
483 484 485
  GdkWindowAttr attributes;
  gint attributes_mask;

486
  gtk_widget_set_realized (widget, TRUE);
487

488 489 490 491 492 493
  gtk_widget_get_allocation (widget, &allocation);

  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
494 495 496
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
497
  attributes.event_mask = gtk_widget_get_events (widget);
498
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
499 500 501 502 503

  window = gdk_window_new (gtk_widget_get_parent_window (widget),
                           &attributes, attributes_mask);
  gtk_widget_set_window (widget, window);
  gdk_window_set_user_data (window, widget);
504 505 506

  attributes.x = 0;
  attributes.y = 0;
507 508
  attributes.width = allocation.width;
  attributes.height = allocation.height;
509
  attributes.window_type = GDK_WINDOW_CHILD;
510 511 512 513 514
  attributes.event_mask = (gtk_widget_get_events (widget)
                           | GDK_BUTTON1_MOTION_MASK
                           | GDK_POINTER_MOTION_HINT_MASK
                           | GDK_BUTTON_PRESS_MASK
                           | GDK_BUTTON_RELEASE_MASK);
515
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
516 517 518

  priv->bin_window = gdk_window_new (window,
                                     &attributes, attributes_mask);
519
  gdk_window_set_user_data (priv->bin_window, widget);
Javier Jardón's avatar
Javier Jardón committed
520 521 522

  child = gtk_bin_get_child (GTK_BIN (hb));
  if (child)
523
    gtk_widget_set_parent_window (child, priv->bin_window);
524

525
  gtk_widget_get_preferred_size (widget, &requisition, NULL);
526

527 528
  attributes.x = 0;
  attributes.y = 0;
529 530
  attributes.width = requisition.width;
  attributes.height = requisition.height;
531 532 533
  attributes.window_type = GDK_WINDOW_TOPLEVEL;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
534 535 536 537 538 539
  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);
540
  attributes.type_hint = GDK_WINDOW_TYPE_HINT_TOOLBAR;
541
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_TYPE_HINT;
542
  priv->float_window = gdk_window_new (gdk_screen_get_root_window (gtk_widget_get_screen (widget)),
543
                                       &attributes, attributes_mask);
544 545 546
  gdk_window_set_user_data (priv->float_window, widget);
  gdk_window_set_decorations (priv->float_window, 0);
  gdk_window_set_type_hint (priv->float_window, GDK_WINDOW_TYPE_HINT_TOOLBAR);
547

548 549 550 551
  context = gtk_widget_get_style_context (widget);
  gtk_style_context_set_background (context, window);
  gtk_style_context_set_background (context, priv->bin_window);
  gtk_style_context_set_background (context, priv->float_window);
552 553
}

554 555 556
static void
gtk_handle_box_unrealize (GtkWidget *widget)
{
557
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
558
  GtkHandleBoxPrivate *priv = hb->priv;
559

560 561 562 563 564 565
  gdk_window_set_user_data (priv->bin_window, NULL);
  gdk_window_destroy (priv->bin_window);
  priv->bin_window = NULL;
  gdk_window_set_user_data (priv->float_window, NULL);
  gdk_window_destroy (priv->float_window);
  priv->float_window = NULL;
566

567
  GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->unrealize (widget);
568 569
}

Owen Taylor's avatar
Owen Taylor committed
570
static void
571
gtk_handle_box_style_updated (GtkWidget *widget)
Owen Taylor's avatar
Owen Taylor committed
572
{
573
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
574
  GtkHandleBoxPrivate *priv = hb->priv;
Owen Taylor's avatar
Owen Taylor committed
575

576 577
  GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->style_updated (widget);

578
  if (gtk_widget_get_realized (widget) &&
579
      gtk_widget_get_has_window (widget))
Owen Taylor's avatar
Owen Taylor committed
580
    {
581 582 583 584 585 586 587 588
      GtkStateFlags state;
      GtkStyleContext *context;

      context = gtk_widget_get_style_context (widget);
      state = gtk_widget_get_state_flags (widget);

      gtk_style_context_save (context);
      gtk_style_context_set_state (context, state);
589

590 591 592
      gtk_style_context_set_background (context, gtk_widget_get_window (widget));
      gtk_style_context_set_background (context, priv->bin_window);
      gtk_style_context_set_background (context, priv->float_window);
593

594
      gtk_style_context_restore (context);
Owen Taylor's avatar
Owen Taylor committed
595 596 597
    }
}

598 599 600
static int
effective_handle_position (GtkHandleBox *hb)
{
601
  GtkHandleBoxPrivate *priv = hb->priv;
602 603 604
  int handle_position;

  if (gtk_widget_get_direction (GTK_WIDGET (hb)) == GTK_TEXT_DIR_LTR)
605
    handle_position = priv->handle_position;
606 607
  else
    {
608
      switch (priv->handle_position)
609 610 611 612 613 614 615 616
	{
	case GTK_POS_LEFT:
	  handle_position = GTK_POS_RIGHT;
	  break;
	case GTK_POS_RIGHT:
	  handle_position = GTK_POS_LEFT;
	  break;
	default:
617
	  handle_position = priv->handle_position;
618 619 620 621 622 623 624
	  break;
	}
    }

  return handle_position;
}

625 626
static void
gtk_handle_box_size_request (GtkWidget      *widget,
Elliot Lee's avatar
Elliot Lee committed
627
			     GtkRequisition *requisition)
628
{
629 630
  GtkBin *bin = GTK_BIN (widget);
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
631
  GtkHandleBoxPrivate *priv = hb->priv;
632
  GtkRequisition child_requisition;
Javier Jardón's avatar
Javier Jardón committed
633
  GtkWidget *child;
634
  gint handle_position;
635

636 637 638 639
  handle_position = effective_handle_position (hb);

  if (handle_position == GTK_POS_LEFT ||
      handle_position == GTK_POS_RIGHT)
640 641 642 643 644
    {
      requisition->width = DRAG_HANDLE_SIZE;
      requisition->height = 0;
    }
  else
645
    {
646 647 648
      requisition->width = 0;
      requisition->height = DRAG_HANDLE_SIZE;
    }
649

Javier Jardón's avatar
Javier Jardón committed
650
  child = gtk_bin_get_child (bin);
651
  /* if our child is not visible, we still request its size, since we
652
   * won't have any useful hint for our size otherwise.
653
   */
Javier Jardón's avatar
Javier Jardón committed
654
  if (child)
655
    {
656
      gtk_widget_get_preferred_size (child, &child_requisition, NULL);
657
    }
658 659 660 661 662
  else
    {
      child_requisition.width = 0;
      child_requisition.height = 0;
    }      
663

664
  if (priv->child_detached)
665
    {
666
      /* FIXME: This doesn't work currently */
667
      if (!priv->shrink_on_detach)
668
	{
669 670
	  if (handle_position == GTK_POS_LEFT ||
	      handle_position == GTK_POS_RIGHT)
671
	    requisition->height += child_requisition.height;
672
	  else
673
	    requisition->width += child_requisition.width;
674
	}
675
      else
676
	{
677 678 679 680 681 682 683 684
          GtkStyleContext *context;
          GtkStateFlags state;
          GtkBorder padding;

          context = gtk_widget_get_style_context (widget);
          state = gtk_widget_get_state_flags (widget);
          gtk_style_context_get_padding (context, state, &padding);

685 686
	  if (handle_position == GTK_POS_LEFT ||
	      handle_position == GTK_POS_RIGHT)
687
	    requisition->height += padding.top;
688
	  else
689
	    requisition->width += padding.left;
690
	}
691
    }
692 693
  else
    {
694 695 696 697 698
      guint border_width;

      border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
      requisition->width += border_width * 2;
      requisition->height += border_width * 2;
699
      
Javier Jardón's avatar
Javier Jardón committed
700
      if (child)
701
	{
702 703
	  requisition->width += child_requisition.width;
	  requisition->height += child_requisition.height;
704 705 706 707 708 709
	}
      else
	{
	  requisition->width += CHILDLESS_SIZE;
	  requisition->height += CHILDLESS_SIZE;
	}
710
    }
711 712
}

713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
static void
gtk_handle_box_get_preferred_width (GtkWidget *widget,
				     gint      *minimum,
				     gint      *natural)
{
  GtkRequisition requisition;

  gtk_handle_box_size_request (widget, &requisition);

  *minimum = *natural = requisition.width;
}

static void
gtk_handle_box_get_preferred_height (GtkWidget *widget,
				     gint      *minimum,
				     gint      *natural)
{
  GtkRequisition requisition;

  gtk_handle_box_size_request (widget, &requisition);

  *minimum = *natural = requisition.height;
}


738 739
static void
gtk_handle_box_size_allocate (GtkWidget     *widget,
740
			      GtkAllocation *allocation)
741
{
742 743
  GtkBin *bin = GTK_BIN (widget);
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
744
  GtkHandleBoxPrivate *priv = hb->priv;
745
  GtkRequisition child_requisition;
Javier Jardón's avatar
Javier Jardón committed
746
  GtkWidget *child;
747
  gint handle_position;
748

749 750
  handle_position = effective_handle_position (hb);

Javier Jardón's avatar
Javier Jardón committed
751 752 753
  child = gtk_bin_get_child (bin);

  if (child)
754
    {
755
      gtk_widget_get_preferred_size (child, &child_requisition, NULL);
756
    }
757 758 759 760
  else
    {
      child_requisition.width = 0;
      child_requisition.height = 0;
761
    }
762

763
  gtk_widget_set_allocation (widget, allocation);
764

765 766 767 768
  if (gtk_widget_get_realized (widget))
    gdk_window_move_resize (gtk_widget_get_window (widget),
                            allocation->x, allocation->y,
                            allocation->width, allocation->height);
769

770
  if (child != NULL && gtk_widget_get_visible (child))
771
    {
772
      GtkAllocation child_allocation;
773 774
      guint border_width;

775
      border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
776

777 778
      child_allocation.x = border_width;
      child_allocation.y = border_width;
779
      if (handle_position == GTK_POS_LEFT)
780
	child_allocation.x += DRAG_HANDLE_SIZE;
781
      else if (handle_position == GTK_POS_TOP)
782
	child_allocation.y += DRAG_HANDLE_SIZE;
783

784
      if (priv->child_detached)
785
	{
786 787 788
	  guint float_width;
	  guint float_height;
	  
789 790
	  child_allocation.width = child_requisition.width;
	  child_allocation.height = child_requisition.height;
791 792 793 794
	  
	  float_width = child_allocation.width + 2 * border_width;
	  float_height = child_allocation.height + 2 * border_width;
	  
795 796
	  if (handle_position == GTK_POS_LEFT ||
	      handle_position == GTK_POS_RIGHT)
797 798 799 800
	    float_width += DRAG_HANDLE_SIZE;
	  else
	    float_height += DRAG_HANDLE_SIZE;

801
	  if (gtk_widget_get_realized (widget))
802
	    {
803
	      gdk_window_resize (priv->float_window,
804 805
				 float_width,
				 float_height);
806
	      gdk_window_move_resize (priv->bin_window,
807 808 809 810 811
				      0,
				      0,
				      float_width,
				      float_height);
	    }
812 813 814
	}
      else
	{
815 816
	  child_allocation.width = MAX (1, (gint) allocation->width - 2 * border_width);
	  child_allocation.height = MAX (1, (gint) allocation->height - 2 * border_width);
817

818 819
	  if (handle_position == GTK_POS_LEFT ||
	      handle_position == GTK_POS_RIGHT)
820 821 822 823
	    child_allocation.width -= DRAG_HANDLE_SIZE;
	  else
	    child_allocation.height -= DRAG_HANDLE_SIZE;
	  
824
	  if (gtk_widget_get_realized (widget))
825
	    gdk_window_move_resize (priv->bin_window,
826 827
				    0,
				    0,
828 829
				    allocation->width,
				    allocation->height);
830 831
	}

Javier Jardón's avatar
Javier Jardón committed
832
      gtk_widget_size_allocate (child, &child_allocation);
833
    }
834
}
835

836
static void
837 838
gtk_handle_box_draw_ghost (GtkHandleBox *hb,
                           cairo_t      *cr)
839
{
840
  GtkWidget *widget = GTK_WIDGET (hb);
841 842
  GtkStateFlags state;
  GtkStyleContext *context;
843 844 845 846
  guint x;
  guint y;
  guint width;
  guint height;
847 848
  gint allocation_width;
  gint allocation_height;
849
  gint handle_position;
850

851
  handle_position = effective_handle_position (hb);
852 853 854
  allocation_width = gtk_widget_get_allocated_width (widget);
  allocation_height = gtk_widget_get_allocated_height (widget);

855 856
  if (handle_position == GTK_POS_LEFT ||
      handle_position == GTK_POS_RIGHT)
857
    {
858
      x = handle_position == GTK_POS_LEFT ? 0 : allocation_width - DRAG_HANDLE_SIZE;
859 860
      y = 0;
      width = DRAG_HANDLE_SIZE;
861
      height = allocation_height;
862
    }
863 864 865
  else
    {
      x = 0;
866 867
      y = handle_position == GTK_POS_TOP ? 0 : allocation_height - DRAG_HANDLE_SIZE;
      width = allocation_width;
868 869
      height = DRAG_HANDLE_SIZE;
    }
870

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
  context = gtk_widget_get_style_context (widget);
  state = gtk_widget_get_state_flags (widget);

  gtk_style_context_save (context);
  gtk_style_context_set_state (context, state);

  gtk_render_background (context, cr, x, y, width, height);
  gtk_render_frame (context, cr, x, y, width, height);

  if (handle_position == GTK_POS_LEFT ||
      handle_position == GTK_POS_RIGHT)
    gtk_render_line (context, cr,
                     handle_position == GTK_POS_LEFT ? DRAG_HANDLE_SIZE : 0,
                     allocation_height / 2,
                     handle_position == GTK_POS_LEFT ? allocation_width : allocation_width - DRAG_HANDLE_SIZE,
                     allocation_height / 2);
  else
    gtk_render_line (context, cr,
                     allocation_width / 2,
                     handle_position == GTK_POS_TOP ? DRAG_HANDLE_SIZE : 0,
                     allocation_width / 2,
                     handle_position == GTK_POS_TOP ? allocation_height : allocation_height - DRAG_HANDLE_SIZE);

  gtk_style_context_restore (context);
895 896
}

897 898 899 900 901 902 903
/**
 * gtk_handle_box_set_shadow_type:
 * @handle_box: a #GtkHandleBox
 * @type: the shadow type.
 *
 * Sets the type of shadow to be drawn around the border
 * of the handle box.
Benjamin Otte's avatar
Benjamin Otte committed
904 905
 *
 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
906
 */
907 908 909 910
void
gtk_handle_box_set_shadow_type (GtkHandleBox  *handle_box,
				GtkShadowType  type)
{
911
  GtkHandleBoxPrivate *priv;
912

913 914
  g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));

915 916 917
  priv = handle_box->priv;

  if ((GtkShadowType) priv->shadow_type != type)
918
    {
919
      priv->shadow_type = type;
920
      g_object_notify (G_OBJECT (handle_box), "shadow-type");
921 922 923
      gtk_widget_queue_resize (GTK_WIDGET (handle_box));
    }
}
924

925 926 927
/**
 * gtk_handle_box_get_shadow_type:
 * @handle_box: a #GtkHandleBox
928
 *
929 930 931
 * Gets the type of shadow drawn around the handle box. See
 * gtk_handle_box_set_shadow_type().
 *
932
 * Returns: the type of shadow currently drawn around the handle box.
Benjamin Otte's avatar
Benjamin Otte committed
933 934
 *
 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
935 936 937 938 939 940
 **/
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);

941
  return handle_box->priv->shadow_type;
942 943
}

944 945 946 947 948 949
/**
 * gtk_handle_box_set_handle_position:
 * @handle_box: a #GtkHandleBox
 * @position: the side of the handlebox where the handle should be drawn.
 *
 * Sets the side of the handlebox where the handle is drawn.
Benjamin Otte's avatar
Benjamin Otte committed
950 951
 *
 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
952
 */
953 954 955 956
void        
gtk_handle_box_set_handle_position  (GtkHandleBox    *handle_box,
				     GtkPositionType  position)
{
957
  GtkHandleBoxPrivate *priv;
958

959 960
  g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));

961 962 963
  priv = handle_box->priv;

  if ((GtkPositionType) priv->handle_position != position)
964
    {
965
      priv->handle_position = position;
966
      g_object_notify (G_OBJECT (handle_box), "handle-position");
967 968 969 970
      gtk_widget_queue_resize (GTK_WIDGET (handle_box));
    }
}

971 972 973 974 975 976 977
/**
 * 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().
 *
978
 * Returns: the current handle position.
Benjamin Otte's avatar
Benjamin Otte committed
979 980
 *
 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
981 982 983 984 985 986
 **/
GtkPositionType
gtk_handle_box_get_handle_position (GtkHandleBox *handle_box)
{
  g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), GTK_POS_LEFT);

987
  return handle_box->priv->handle_position;
988 989
}

990 991 992 993 994 995 996 997 998
/**
 * gtk_handle_box_set_snap_edge:
 * @handle_box: a #GtkHandleBox
 * @edge: the snap edge, or -1 to unset the value; in which
 *   case GTK+ will try to guess an appropriate value
 *   in the future.
 *
 * Sets the snap edge of a handlebox. The snap edge is
 * the edge of the detached child that must be aligned
999
 * with the corresponding edge of the “ghost” left
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
 * behind when the child was detached to reattach
 * the torn-off window. Usually, the snap edge should
 * be chosen so that it stays in the same place on
 * the screen when the handlebox is torn off.
 *
 * If the snap edge is not set, then an appropriate value
 * will be guessed from the handle position. If the
 * handle position is %GTK_POS_RIGHT or %GTK_POS_LEFT,
 * then the snap edge will be %GTK_POS_TOP, otherwise
 * it will be %GTK_POS_LEFT.
Benjamin Otte's avatar
Benjamin Otte committed
1010 1011
 *
 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
1012 1013
 */
void
1014 1015 1016
gtk_handle_box_set_snap_edge        (GtkHandleBox    *handle_box,
				     GtkPositionType  edge)
{
1017
  GtkHandleBoxPrivate *priv;
1018

1019 1020
  g_return_if_fail (GTK_IS_HANDLE_BOX (handle_box));

1021 1022 1023
  priv = handle_box->priv;

  if (priv->snap_edge != edge)
1024
    {
1025 1026
      priv->snap_edge = edge;

1027
      g_object_freeze_notify (G_OBJECT (handle_box));
1028 1029
      g_object_notify (G_OBJECT (handle_box), "snap-edge");
      g_object_notify (G_OBJECT (handle_box), "snap-edge-set");
1030
      g_object_thaw_notify (G_OBJECT (handle_box));
1031
    }
1032 1033
}

1034 1035 1036 1037
/**
 * gtk_handle_box_get_snap_edge:
 * @handle_box: a #GtkHandleBox
 *
1038 1039 1040
 * Gets the edge used for determining reattachment of the handle box.
 * See gtk_handle_box_set_snap_edge().
 *
1041
 * Returns: the edge used for determining reattachment, or
1042 1043
 *   (GtkPositionType)-1 if this is determined (as per default)
 *   from the handle position.
Benjamin Otte's avatar
Benjamin Otte committed
1044 1045
 *
 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
1046 1047 1048 1049 1050 1051
 **/
GtkPositionType
gtk_handle_box_get_snap_edge (GtkHandleBox *handle_box)
{
  g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), (GtkPositionType)-1);

1052
  return (GtkPositionType)handle_box->priv->snap_edge;
1053 1054
}

Tim Janik's avatar
Tim Janik committed
1055 1056 1057 1058
/**
 * gtk_handle_box_get_child_detached:
 * @handle_box: a #GtkHandleBox
 *
1059
 * Whether the handlebox’s child is currently detached.
Tim Janik's avatar
Tim Janik committed
1060
 *
1061
 * Returns: %TRUE if the child is currently detached, otherwise %FALSE
Tim Janik's avatar
Tim Janik committed
1062
 *
1063
 * Since: 2.14
Benjamin Otte's avatar
Benjamin Otte committed
1064 1065
 *
 * Deprecated: 3.4: #GtkHandleBox has been deprecated.
Tim Janik's avatar
Tim Janik committed
1066 1067 1068 1069 1070 1071
 **/
gboolean
gtk_handle_box_get_child_detached (GtkHandleBox *handle_box)
{
  g_return_val_if_fail (GTK_IS_HANDLE_BOX (handle_box), FALSE);

1072
  return handle_box->priv->child_detached;
Tim Janik's avatar
Tim Janik committed
1073 1074
}

1075
static void
1076 1077
gtk_handle_box_paint (GtkWidget *widget,
                      cairo_t   *cr)
Elliot Lee's avatar
Elliot Lee committed
1078
{
1079
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1080
  GtkHandleBoxPrivate *priv = hb->priv;
1081
  GtkBin *bin = GTK_BIN (widget);
1082 1083
  GtkStyleContext *context;
  GtkStateFlags state;
1084
  GtkWidget *child;
1085
  gint width, height;
1086
  GdkRectangle rect;
1087
  gint handle_position;
1088

1089 1090
  handle_position = effective_handle_position (hb);

1091 1092
  width = gdk_window_get_width (priv->bin_window);
  height = gdk_window_get_height (priv->bin_window);
1093

1094 1095 1096 1097 1098 1099 1100 1101
  context = gtk_widget_get_style_context (widget);
  state = gtk_widget_get_state_flags (widget);

  gtk_style_context_save (context);
  gtk_style_context_set_state (context, state);

  gtk_render_background (context, cr, 0, 0, width, height);
  gtk_render_frame (context, cr, 0, 0, width, height);
1102

1103
  switch (handle_position)
1104 1105 1106
    {
    case GTK_POS_LEFT:
      rect.x = 0;
1107
      rect.y = 0;
1108 1109 1110 1111
      rect.width = DRAG_HANDLE_SIZE;
      rect.height = height;
      break;
    case GTK_POS_RIGHT:
1112
      rect.x = width - DRAG_HANDLE_SIZE;
1113 1114 1115 1116 1117 1118
      rect.y = 0;
      rect.width = DRAG_HANDLE_SIZE;
      rect.height = height;
      break;
    case GTK_POS_TOP:
      rect.x = 0;
1119
      rect.y = 0;
1120 1121 1122 1123 1124 1125 1126 1127 1128
      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;
1129
    default:
1130 1131
      g_assert_not_reached ();
      break;
1132
    }
1133

1134 1135
  gtk_render_handle (context, cr,
                     rect.x, rect.y, rect.width, rect.height);
1136

1137 1138
  child = gtk_bin_get_child (bin);
  if (child != NULL && gtk_widget_get_visible (child))
1139
    GTK_WIDGET_CLASS (gtk_handle_box_parent_class)->draw (widget, cr);
1140 1141

  gtk_style_context_restore (context);
Elliot Lee's avatar
Elliot Lee committed
1142 1143
}

1144
static gboolean
1145 1146
gtk_handle_box_draw (GtkWidget *widget,
		     cairo_t   *cr)
1147
{
1148 1149
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
  GtkHandleBoxPrivate *priv = hb->priv;
1150

1151
  if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
1152
    {
1153 1154
      if (priv->child_detached)
        gtk_handle_box_draw_ghost (hb, cr);
1155
    }
1156
  else if (gtk_cairo_should_draw_window (cr, priv->bin_window))
1157
    gtk_handle_box_paint (widget, cr);
1158
  
1159 1160 1161
  return FALSE;
}

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
static GtkWidget *
gtk_handle_box_get_invisible (void)
{
  static GtkWidget *handle_box_invisible = NULL;

  if (!handle_box_invisible)
    {
      handle_box_invisible = gtk_invisible_new ();
      gtk_widget_show (handle_box_invisible);
    }
  
  return handle_box_invisible;
}

static gboolean
gtk_handle_box_grab_event (GtkWidget    *widget,
			   GdkEvent     *event,
			   GtkHandleBox *hb)
{
1181
  GtkHandleBoxPrivate *priv = hb->priv;
1182

1183 1184 1185
  switch (event->type)
    {
    case GDK_BUTTON_RELEASE:
1186
      if (priv->in_drag)		/* sanity check */
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
	{
	  gtk_handle_box_end_drag (hb, event->button.time);
	  return TRUE;
	}
      break;

    case GDK_MOTION_NOTIFY:
      return gtk_handle_box_motion (GTK_WIDGET (hb), (GdkEventMotion *)event);
      break;

    default:
      break;
    }

  return FALSE;
}

1204 1205 1206
static gboolean
gtk_handle_box_button_press (GtkWidget      *widget,
                             GdkEventButton *event)
1207
{
1208
  GtkHandleBox *hb = GTK_HANDLE_BOX (widget);
1209
  GtkHandleBoxPrivate *priv = hb->priv;