gtkpaned.c 97.5 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

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

25
#include "config.h"
26

27 28
#include "gtkpaned.h"

29
#include "gtkbindings.h"
30 31
#include "gtkmain.h"
#include "gtkmarshalers.h"
32 33
#include "gtkorientable.h"
#include "gtkwindow.h"
34
#include "gtktypebuiltins.h"
35
#include "gtkorientableprivate.h"
36
#include "gtkprivate.h"
37
#include "gtkintl.h"
38
#include "gtkwidgetprivate.h"
39
#include "gtkcontainerprivate.h"
40
#include "a11y/gtkpanedaccessible.h"
41
#include "gtkcsscustomgadgetprivate.h"
42 43
#include "gtkcssnodeprivate.h"
#include "gtkstylecontextprivate.h"
44 45 46 47
#include "gtkcssstylepropertyprivate.h"
#include "gtkcssnumbervalueprivate.h"

#include <math.h>
Elliot Lee's avatar
Elliot Lee committed
48

49 50
/**
 * SECTION:gtkpaned
51
 * @Short_description: A widget with two adjustable panes
52 53
 * @Title: GtkPaned
 *
54 55 56
 * #GtkPaned has two panes, arranged either
 * horizontally or vertically. The division between
 * the two panes is adjustable by the user by dragging
57
 * a handle.
58 59
 *
 * Child widgets are
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
 * added to the panes of the widget with gtk_paned_pack1() and
 * gtk_paned_pack2(). The division between the two children is set by default
 * from the size requests of the children, but it can be adjusted by the
 * user.
 *
 * A paned widget draws a separator between the two child widgets and a
 * small handle that the user can drag to adjust the division. It does not
 * draw any relief around the children or around the separator. (The space
 * in which the separator is called the gutter.) Often, it is useful to put
 * each child inside a #GtkFrame with the shadow type set to %GTK_SHADOW_IN
 * so that the gutter appears as a ridge. No separator is drawn if one of
 * the children is missing.
 *
 * Each child has two options that can be set, @resize and @shrink. If
 * @resize is true, then when the #GtkPaned is resized, that child will
 * expand or shrink along with the paned widget. If @shrink is true, then
 * that child can be made smaller than its requisition by the user.
 * Setting @shrink to %FALSE allows the application to set a minimum size.
 * If @resize is false for both children, then this is treated as if
 * @resize is true for both children.
 *
 * The application can set the position of the slider as if it were set
 * by the user, by calling gtk_paned_set_position().
 *
84 85
 * # CSS nodes
 *
86 87
 * |[<!-- language="plain" -->
 * paned
88 89 90
 * ├── <child>
 * ├── separator[.wide]
 * ╰── <child>
91 92
 * ]|
 *
93
 * GtkPaned has a main CSS node with name paned, and a subnode for
Timm Bäder's avatar
Timm Bäder committed
94
 * the separator with name separator. The subnode gets a .wide style
95
 * class when the paned is supposed to be wide.
96
 *
97 98 99 100
 * In horizontal orientation, the nodes of the children are always arranged
 * from left to right. So :first-child will always select the leftmost child,
 * regardless of text direction.
 *
101 102
 * ## Creating a paned widget with minimum sizes.
 *
103
 * |[<!-- language="C" -->
104 105 106 107 108 109 110 111 112 113 114 115 116
 * GtkWidget *hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
 * GtkWidget *frame1 = gtk_frame_new (NULL);
 * GtkWidget *frame2 = gtk_frame_new (NULL);
 * gtk_frame_set_shadow_type (GTK_FRAME (frame1), GTK_SHADOW_IN);
 * gtk_frame_set_shadow_type (GTK_FRAME (frame2), GTK_SHADOW_IN);
 *
 * gtk_widget_set_size_request (hpaned, 200, -1);
 *
 * gtk_paned_pack1 (GTK_PANED (hpaned), frame1, TRUE, FALSE);
 * gtk_widget_set_size_request (frame1, 50, -1);
 *
 * gtk_paned_pack2 (GTK_PANED (hpaned), frame2, FALSE, FALSE);
 * gtk_widget_set_size_request (frame2, 50, -1);
117
 * ]|
118
 */
119

120 121 122 123 124
enum {
  CHILD1,
  CHILD2
};

125 126 127 128 129
struct _GtkPanedPrivate
{
  GtkPaned       *first_paned;
  GtkWidget      *child1;
  GtkWidget      *child2;
130 131
  GdkWindow      *child1_window;
  GdkWindow      *child2_window;
132 133 134
  GtkWidget      *last_child1_focus;
  GtkWidget      *last_child2_focus;
  GtkWidget      *saved_focus;
135
  GtkOrientation  orientation;
136 137 138

  GdkRectangle   handle_pos;
  GdkWindow     *handle;
139 140 141

  GtkCssGadget  *gadget;
  GtkCssGadget  *handle_gadget;
142

143 144
  GtkGesture    *pan_gesture;  /* Used for touch */
  GtkGesture    *drag_gesture; /* Used for mice */
145

146 147 148 149 150 151 152 153 154 155 156 157 158 159
  gint          child1_size;
  gint          drag_pos;
  gint          last_allocation;
  gint          max_position;
  gint          min_position;
  gint          original_position;

  guint         handle_prelit : 1;
  guint         in_recursion  : 1;
  guint         child1_resize : 1;
  guint         child1_shrink : 1;
  guint         child2_resize : 1;
  guint         child2_shrink : 1;
  guint         position_set  : 1;
160
  guint         panning       : 1;
161 162
};

163
enum {
164
  PROP_0,
165
  PROP_ORIENTATION,
166
  PROP_POSITION,
167 168
  PROP_POSITION_SET,
  PROP_MIN_POSITION,
169 170
  PROP_MAX_POSITION,
  PROP_WIDE_HANDLE
171
};
Elliot Lee's avatar
Elliot Lee committed
172

173 174 175 176 177 178
enum {
  CHILD_PROP_0,
  CHILD_PROP_RESIZE,
  CHILD_PROP_SHRINK
};

179 180 181 182 183 184
enum {
  CYCLE_CHILD_FOCUS,
  TOGGLE_HANDLE_FOCUS,
  MOVE_HANDLE,
  CYCLE_HANDLE_FOCUS,
  ACCEPT_POSITION,
185 186
  CANCEL_POSITION,
  LAST_SIGNAL
187 188 189 190 191 192 193 194 195 196
};

static void     gtk_paned_set_property          (GObject          *object,
						 guint             prop_id,
						 const GValue     *value,
						 GParamSpec       *pspec);
static void     gtk_paned_get_property          (GObject          *object,
						 guint             prop_id,
						 GValue           *value,
						 GParamSpec       *pspec);
197 198 199 200 201 202 203 204 205 206
static void     gtk_paned_set_child_property    (GtkContainer     *container,
                                                 GtkWidget        *child,
                                                 guint             property_id,
                                                 const GValue     *value,
                                                 GParamSpec       *pspec);
static void     gtk_paned_get_child_property    (GtkContainer     *container,
                                                 GtkWidget        *child,
                                                 guint             property_id,
                                                 GValue           *value,
                                                 GParamSpec       *pspec);
207
static void     gtk_paned_finalize              (GObject          *object);
208

209 210 211 212 213 214
static void     gtk_paned_get_preferred_width   (GtkWidget        *widget,
                                                 gint             *minimum,
                                                 gint             *natural);
static void     gtk_paned_get_preferred_height  (GtkWidget        *widget,
                                                 gint             *minimum,
                                                 gint             *natural);
215 216 217 218 219 220 221 222 223 224
static void     gtk_paned_get_preferred_width_for_height
                                                (GtkWidget        *widget,
                                                 gint              height,
                                                 gint             *minimum,
                                                 gint             *natural);
static void     gtk_paned_get_preferred_height_for_width
                                                (GtkWidget        *widget,
                                                 gint              width,
                                                 gint             *minimum,
                                                 gint              *natural);
225

226 227
static void     gtk_paned_size_allocate         (GtkWidget        *widget,
                                                 GtkAllocation    *allocation);
228 229 230 231
static void     gtk_paned_realize               (GtkWidget        *widget);
static void     gtk_paned_unrealize             (GtkWidget        *widget);
static void     gtk_paned_map                   (GtkWidget        *widget);
static void     gtk_paned_unmap                 (GtkWidget        *widget);
232 233
static void     gtk_paned_state_flags_changed   (GtkWidget        *widget,
                                                 GtkStateFlags     previous_state);
234 235
static void     gtk_paned_direction_changed     (GtkWidget        *widget,
                                                 GtkTextDirection  previous_direction);
236 237
static gboolean gtk_paned_draw                  (GtkWidget        *widget,
						 cairo_t          *cr);
238 239 240 241
static gboolean gtk_paned_enter                 (GtkWidget        *widget,
						 GdkEventCrossing *event);
static gboolean gtk_paned_leave                 (GtkWidget        *widget,
						 GdkEventCrossing *event);
242 243 244 245 246 247 248 249 250 251
static gboolean gtk_paned_focus                 (GtkWidget        *widget,
						 GtkDirectionType  direction);
static void     gtk_paned_add                   (GtkContainer     *container,
						 GtkWidget        *widget);
static void     gtk_paned_remove                (GtkContainer     *container,
						 GtkWidget        *widget);
static void     gtk_paned_forall                (GtkContainer     *container,
						 gboolean          include_internals,
						 GtkCallback       callback,
						 gpointer          callback_data);
252 253 254 255
static void     gtk_paned_calc_position         (GtkPaned         *paned,
                                                 gint              allocation,
                                                 gint              child1_req,
                                                 gint              child2_req);
256 257 258 259
static void     gtk_paned_set_focus_child       (GtkContainer     *container,
						 GtkWidget        *child);
static void     gtk_paned_set_saved_focus       (GtkPaned         *paned,
						 GtkWidget        *widget);
260 261
static void     gtk_paned_set_first_paned       (GtkPaned         *paned,
						 GtkPaned         *first_paned);
262 263 264 265 266 267 268 269 270 271 272 273 274
static void     gtk_paned_set_last_child1_focus (GtkPaned         *paned,
						 GtkWidget        *widget);
static void     gtk_paned_set_last_child2_focus (GtkPaned         *paned,
						 GtkWidget        *widget);
static gboolean gtk_paned_cycle_child_focus     (GtkPaned         *paned,
						 gboolean          reverse);
static gboolean gtk_paned_cycle_handle_focus    (GtkPaned         *paned,
						 gboolean          reverse);
static gboolean gtk_paned_move_handle           (GtkPaned         *paned,
						 GtkScrollType     scroll);
static gboolean gtk_paned_accept_position       (GtkPaned         *paned);
static gboolean gtk_paned_cancel_position       (GtkPaned         *paned);
static gboolean gtk_paned_toggle_handle_focus   (GtkPaned         *paned);
275

276
static GType    gtk_paned_child_type            (GtkContainer     *container);
Elliot Lee's avatar
Elliot Lee committed
277

278 279 280
static void     update_drag                     (GtkPaned         *paned,
                                                 int               xpos,
                                                 int               ypos);
281

282
G_DEFINE_TYPE_WITH_CODE (GtkPaned, gtk_paned, GTK_TYPE_CONTAINER,
283
                         G_ADD_PRIVATE (GtkPaned)
284 285
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
                                                NULL))
Elliot Lee's avatar
Elliot Lee committed
286

287 288
static guint signals[LAST_SIGNAL] = { 0 };

289

290 291
static void
add_tab_bindings (GtkBindingSet    *binding_set,
292
		  GdkModifierType   modifiers)
293
{
294
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
295
                                "toggle-handle-focus", 0);
296
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
297
				"toggle-handle-focus", 0);
298 299 300 301 302 303 304 305 306
}

static void
add_move_binding (GtkBindingSet   *binding_set,
		  guint            keyval,
		  GdkModifierType  mask,
		  GtkScrollType    scroll)
{
  gtk_binding_entry_add_signal (binding_set, keyval, mask,
307
				"move-handle", 1,
308 309 310
				GTK_TYPE_SCROLL_TYPE, scroll);
}

Elliot Lee's avatar
Elliot Lee committed
311 312 313
static void
gtk_paned_class_init (GtkPanedClass *class)
{
314
  GObjectClass *object_class;
Elliot Lee's avatar
Elliot Lee committed
315 316
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;
317 318
  GtkPanedClass *paned_class;
  GtkBindingSet *binding_set;
319

320
  object_class = (GObjectClass *) class;
321 322
  widget_class = (GtkWidgetClass *) class;
  container_class = (GtkContainerClass *) class;
323
  paned_class = (GtkPanedClass *) class;
324

325 326
  object_class->set_property = gtk_paned_set_property;
  object_class->get_property = gtk_paned_get_property;
327
  object_class->finalize = gtk_paned_finalize;
328

329 330
  widget_class->get_preferred_width = gtk_paned_get_preferred_width;
  widget_class->get_preferred_height = gtk_paned_get_preferred_height;
331 332
  widget_class->get_preferred_width_for_height = gtk_paned_get_preferred_width_for_height;
  widget_class->get_preferred_height_for_width = gtk_paned_get_preferred_height_for_width;
333
  widget_class->size_allocate = gtk_paned_size_allocate;
Elliot Lee's avatar
Elliot Lee committed
334 335
  widget_class->realize = gtk_paned_realize;
  widget_class->unrealize = gtk_paned_unrealize;
336 337
  widget_class->map = gtk_paned_map;
  widget_class->unmap = gtk_paned_unmap;
338
  widget_class->draw = gtk_paned_draw;
339
  widget_class->focus = gtk_paned_focus;
340 341
  widget_class->enter_notify_event = gtk_paned_enter;
  widget_class->leave_notify_event = gtk_paned_leave;
342
  widget_class->state_flags_changed = gtk_paned_state_flags_changed;
343
  widget_class->direction_changed = gtk_paned_direction_changed;
344

Elliot Lee's avatar
Elliot Lee committed
345 346
  container_class->add = gtk_paned_add;
  container_class->remove = gtk_paned_remove;
347
  container_class->forall = gtk_paned_forall;
348
  container_class->child_type = gtk_paned_child_type;
349
  container_class->set_focus_child = gtk_paned_set_focus_child;
350 351
  container_class->set_child_property = gtk_paned_set_child_property;
  container_class->get_child_property = gtk_paned_get_child_property;
352
  gtk_container_class_handle_border_width (container_class);
353 354 355 356 357 358 359

  paned_class->cycle_child_focus = gtk_paned_cycle_child_focus;
  paned_class->toggle_handle_focus = gtk_paned_toggle_handle_focus;
  paned_class->move_handle = gtk_paned_move_handle;
  paned_class->cycle_handle_focus = gtk_paned_cycle_handle_focus;
  paned_class->accept_position = gtk_paned_accept_position;
  paned_class->cancel_position = gtk_paned_cancel_position;
360

361 362 363 364
  g_object_class_override_property (object_class,
                                    PROP_ORIENTATION,
                                    "orientation");

365
  g_object_class_install_property (object_class,
366 367 368 369 370 371
                                   PROP_POSITION,
                                   g_param_spec_int ("position",
                                                     P_("Position"),
                                                     P_("Position of paned separator in pixels (0 means all the way to the left/top)"),
                                                     0, G_MAXINT, 0,
                                                     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
372

373
  g_object_class_install_property (object_class,
374 375 376 377 378 379
                                   PROP_POSITION_SET,
                                   g_param_spec_boolean ("position-set",
                                                         P_("Position Set"),
                                                         P_("TRUE if the Position property should be used"),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
380

381 382 383
  /**
   * GtkPaned:min-position:
   *
384 385 386
   * The smallest possible value for the position property.
   * This property is derived from the size and shrinkability
   * of the widget's children.
387 388 389 390
   *
   * Since: 2.4
   */
  g_object_class_install_property (object_class,
391 392 393 394 395 396
                                   PROP_MIN_POSITION,
                                   g_param_spec_int ("min-position",
                                                     P_("Minimal Position"),
                                                     P_("Smallest possible value for the \"position\" property"),
                                                     0, G_MAXINT, 0,
                                                     GTK_PARAM_READABLE|G_PARAM_EXPLICIT_NOTIFY));
397 398 399 400

  /**
   * GtkPaned:max-position:
   *
401 402 403
   * The largest possible value for the position property.
   * This property is derived from the size and shrinkability
   * of the widget's children.
404 405 406 407
   *
   * Since: 2.4
   */
  g_object_class_install_property (object_class,
408 409 410 411 412 413 414
                                   PROP_MAX_POSITION,
                                   g_param_spec_int ("max-position",
                                                     P_("Maximal Position"),
                                                     P_("Largest possible value for the \"position\" property"),
                                                     0, G_MAXINT, G_MAXINT,
                                                     GTK_PARAM_READABLE|G_PARAM_EXPLICIT_NOTIFY));

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
  /**
   * GtkPaned:wide-handle:
   *
   * Setting this property to %TRUE indicates that the paned needs
   * to provide stronger visual separation (e.g. because it separates
   * between two notebooks, whose tab rows would otherwise merge visually).
   *
   * Since: 3.16 
   */
  g_object_class_install_property (object_class,
                                   PROP_WIDE_HANDLE,
                                   g_param_spec_boolean ("wide-handle",
                                                         P_("Wide Handle"),
                                                         P_("Whether the paned should have a prominent handle"),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));

432 433 434 435 436 437 438
  /**
   * GtkPaned::handle-size:
   *
   * The width of the handle.
   *
   * Deprecated: 3.20: Use CSS min-width and min-height instead.
   */
439 440 441 442 443 444 445
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("handle-size",
							     P_("Handle Size"),
							     P_("Width of handle"),
							     0,
							     G_MAXINT,
							     5,
446
							     GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
447

448 449 450 451 452 453 454 455
  /**
   * GtkPaned:resize:
   *
   * The "resize" child property determines whether the child expands and
   * shrinks along with the paned widget.
   *
   * Since: 2.4
   */
456 457 458
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_RESIZE,
					      g_param_spec_boolean ("resize", 
459 460
								    P_("Resize"),
								    P_("If TRUE, the child expands and shrinks along with the paned widget"),
461
								    TRUE,
462
								    GTK_PARAM_READWRITE));
463

464 465 466 467 468 469 470 471
  /**
   * GtkPaned:shrink:
   *
   * The "shrink" child property determines whether the child can be made
   * smaller than its requisition.
   *
   * Since: 2.4
   */
472 473 474
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_SHRINK,
					      g_param_spec_boolean ("shrink", 
475 476
								    P_("Shrink"),
								    P_("If TRUE, the child can be made smaller than its requisition"),
477
								    TRUE,
478
								    GTK_PARAM_READWRITE));
479

480 481 482 483 484 485
  /**
   * GtkPaned::cycle-child-focus:
   * @widget: the object that received the signal
   * @reversed: whether cycling backward or forward
   *
   * The ::cycle-child-focus signal is a 
486
   * [keybinding signal][GtkBindingSignal]
487 488 489 490 491 492
   * which gets emitted to cycle the focus between the children of the paned.
   *
   * The default binding is f6.
   *
   * Since: 2.0
   */
493
  signals [CYCLE_CHILD_FOCUS] =
494
    g_signal_new (I_("cycle-child-focus"),
495 496 497 498 499 500 501 502
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkPanedClass, cycle_child_focus),
		  NULL, NULL,
		  _gtk_marshal_BOOLEAN__BOOLEAN,
		  G_TYPE_BOOLEAN, 1,
		  G_TYPE_BOOLEAN);

503 504 505 506 507
  /**
   * GtkPaned::toggle-handle-focus:
   * @widget: the object that received the signal
   *
   * The ::toggle-handle-focus is a 
508
   * [keybinding signal][GtkBindingSignal]
509 510 511 512 513 514 515
   * which gets emitted to accept the current position of the handle and then 
   * move focus to the next widget in the focus chain.
   *
   * The default binding is Tab.
   *
   * Since: 2.0
   */
516
  signals [TOGGLE_HANDLE_FOCUS] =
517
    g_signal_new (I_("toggle-handle-focus"),
518 519 520 521 522 523 524
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkPanedClass, toggle_handle_focus),
		  NULL, NULL,
		  _gtk_marshal_BOOLEAN__VOID,
		  G_TYPE_BOOLEAN, 0);

525 526 527 528 529 530
  /**
   * GtkPaned::move-handle:
   * @widget: the object that received the signal
   * @scroll_type: a #GtkScrollType
   *
   * The ::move-handle signal is a 
531
   * [keybinding signal][GtkBindingSignal]
532 533 534 535 536
   * which gets emitted to move the handle when the user is using key bindings 
   * to move it.
   *
   * Since: 2.0
   */
537
  signals[MOVE_HANDLE] =
538
    g_signal_new (I_("move-handle"),
539 540 541 542 543 544 545 546
		  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkPanedClass, move_handle),
                  NULL, NULL,
                  _gtk_marshal_BOOLEAN__ENUM,
                  G_TYPE_BOOLEAN, 1,
                  GTK_TYPE_SCROLL_TYPE);

547 548 549 550 551 552
  /**
   * GtkPaned::cycle-handle-focus:
   * @widget: the object that received the signal
   * @reversed: whether cycling backward or forward
   *
   * The ::cycle-handle-focus signal is a 
553
   * [keybinding signal][GtkBindingSignal]
554 555 556 557 558 559 560
   * which gets emitted to cycle whether the paned should grab focus to allow
   * the user to change position of the handle by using key bindings.
   *
   * The default binding for this signal is f8.
   *
   * Since: 2.0
   */
561
  signals [CYCLE_HANDLE_FOCUS] =
562
    g_signal_new (I_("cycle-handle-focus"),
563 564 565 566 567 568 569 570
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkPanedClass, cycle_handle_focus),
		  NULL, NULL,
		  _gtk_marshal_BOOLEAN__BOOLEAN,
		  G_TYPE_BOOLEAN, 1,
		  G_TYPE_BOOLEAN);

571 572 573 574 575
  /**
   * GtkPaned::accept-position:
   * @widget: the object that received the signal
   *
   * The ::accept-position signal is a 
576
   * [keybinding signal][GtkBindingSignal]
577 578 579 580 581 582 583
   * which gets emitted to accept the current position of the handle when 
   * moving it using key bindings.
   *
   * The default binding for this signal is Return or Space.
   *
   * Since: 2.0
   */
584
  signals [ACCEPT_POSITION] =
585
    g_signal_new (I_("accept-position"),
586 587 588 589 590 591 592
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkPanedClass, accept_position),
		  NULL, NULL,
		  _gtk_marshal_BOOLEAN__VOID,
		  G_TYPE_BOOLEAN, 0);

593 594 595 596 597
  /**
   * GtkPaned::cancel-position:
   * @widget: the object that received the signal
   *
   * The ::cancel-position signal is a 
598
   * [keybinding signal][GtkBindingSignal]
599 600 601 602 603 604 605 606
   * which gets emitted to cancel moving the position of the handle using key 
   * bindings. The position of the handle will be reset to the value prior to 
   * moving it.
   *
   * The default binding for this signal is Escape.
   *
   * Since: 2.0
   */
607
  signals [CANCEL_POSITION] =
608
    g_signal_new (I_("cancel-position"),
609 610 611 612 613 614 615
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkPanedClass, cancel_position),
		  NULL, NULL,
		  _gtk_marshal_BOOLEAN__VOID,
		  G_TYPE_BOOLEAN, 0);

Manish Singh's avatar
Manish Singh committed
616
  binding_set = gtk_binding_set_by_class (class);
617 618

  /* F6 and friends */
619
  gtk_binding_entry_add_signal (binding_set,
620
                                GDK_KEY_F6, 0,
621
                                "cycle-child-focus", 1, 
622 623
                                G_TYPE_BOOLEAN, FALSE);
  gtk_binding_entry_add_signal (binding_set,
624
				GDK_KEY_F6, GDK_SHIFT_MASK,
625
				"cycle-child-focus", 1,
626 627 628 629
				G_TYPE_BOOLEAN, TRUE);

  /* F8 and friends */
  gtk_binding_entry_add_signal (binding_set,
630
				GDK_KEY_F8, 0,
631
				"cycle-handle-focus", 1,
632 633 634
				G_TYPE_BOOLEAN, FALSE);
 
  gtk_binding_entry_add_signal (binding_set,
635
				GDK_KEY_F8, GDK_SHIFT_MASK,
636
				"cycle-handle-focus", 1,
637
				G_TYPE_BOOLEAN, TRUE);
638
 
639 640 641 642
  add_tab_bindings (binding_set, 0);
  add_tab_bindings (binding_set, GDK_CONTROL_MASK);
  add_tab_bindings (binding_set, GDK_SHIFT_MASK);
  add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK);
643 644 645

  /* accept and cancel positions */
  gtk_binding_entry_add_signal (binding_set,
646
				GDK_KEY_Escape, 0,
647
				"cancel-position", 0);
648 649

  gtk_binding_entry_add_signal (binding_set,
650
				GDK_KEY_Return, 0,
651
				"accept-position", 0);
652
  gtk_binding_entry_add_signal (binding_set,
653
				GDK_KEY_ISO_Enter, 0,
654
				"accept-position", 0);
655
  gtk_binding_entry_add_signal (binding_set,
656
				GDK_KEY_KP_Enter, 0,
657
				"accept-position", 0);
658
  gtk_binding_entry_add_signal (binding_set,
659
				GDK_KEY_space, 0,
660
				"accept-position", 0);
661
  gtk_binding_entry_add_signal (binding_set,
662
				GDK_KEY_KP_Space, 0,
663
				"accept-position", 0);
664 665

  /* move handle */
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
  add_move_binding (binding_set, GDK_KEY_Left, 0, GTK_SCROLL_STEP_LEFT);
  add_move_binding (binding_set, GDK_KEY_KP_Left, 0, GTK_SCROLL_STEP_LEFT);
  add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
  add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);

  add_move_binding (binding_set, GDK_KEY_Right, 0, GTK_SCROLL_STEP_RIGHT);
  add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
  add_move_binding (binding_set, GDK_KEY_KP_Right, 0, GTK_SCROLL_STEP_RIGHT);
  add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);

  add_move_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
  add_move_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
  add_move_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
  add_move_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
  add_move_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
  add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0, GTK_SCROLL_PAGE_UP);

  add_move_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
  add_move_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
  add_move_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
  add_move_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
  add_move_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
  add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);

  add_move_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START);
  add_move_binding (binding_set, GDK_KEY_KP_Home, 0, GTK_SCROLL_START);
  add_move_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END);
  add_move_binding (binding_set, GDK_KEY_KP_End, 0, GTK_SCROLL_END);
694

695
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PANED_ACCESSIBLE);
696
  gtk_widget_class_set_css_name (widget_class, "paned");
697 698
}

Manish Singh's avatar
Manish Singh committed
699
static GType
700 701
gtk_paned_child_type (GtkContainer *container)
{
702 703 704 705
  GtkPaned *paned = GTK_PANED (container);
  GtkPanedPrivate *priv = paned->priv;

  if (!priv->child1 || !priv->child2)
706 707
    return GTK_TYPE_WIDGET;
  else
Manish Singh's avatar
Manish Singh committed
708
    return G_TYPE_NONE;
Elliot Lee's avatar
Elliot Lee committed
709 710
}

711 712 713 714 715 716 717 718 719 720
static gboolean
initiates_touch_drag (GtkPaned *paned,
                      gdouble   start_x,
                      gdouble   start_y)
{
  gint handle_size, handle_pos, drag_pos;
  GtkPanedPrivate *priv = paned->priv;
  GtkAllocation allocation;

#define TOUCH_EXTRA_AREA_WIDTH 50
721
  gtk_css_gadget_get_content_allocation (priv->gadget, &allocation, NULL);
722 723 724 725 726
  gtk_css_gadget_get_preferred_size (priv->handle_gadget,
                                     priv->orientation,
                                     -1,
                                     NULL, &handle_size,
                                     NULL, NULL);
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748

  if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      handle_pos = priv->handle_pos.x - allocation.x;
      drag_pos = start_x;
    }
  else
    {
      handle_pos = priv->handle_pos.y - allocation.y;
      drag_pos = start_y;
    }

  if (drag_pos < handle_pos - TOUCH_EXTRA_AREA_WIDTH ||
      drag_pos > handle_pos + handle_size + TOUCH_EXTRA_AREA_WIDTH)
    return FALSE;

#undef TOUCH_EXTRA_AREA_WIDTH

  return TRUE;
}

static void
749 750 751 752
gesture_drag_begin_cb (GtkGestureDrag *gesture,
                       gdouble         start_x,
                       gdouble         start_y,
                       GtkPaned       *paned)
753 754 755 756 757
{
  GtkPanedPrivate *priv = paned->priv;
  GdkEventSequence *sequence;
  GtkAllocation allocation;
  const GdkEvent *event;
758 759
  GdkDevice *device;
  gboolean is_touch;
760 761 762

  sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
  event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
763
  device = gdk_event_get_source_device (event);
764
  gtk_css_gadget_get_content_allocation (priv->gadget, &allocation, NULL);
765 766
  paned->priv->panning = FALSE;

767 768 769
  is_touch = (event->type == GDK_TOUCH_BEGIN ||
              gdk_device_get_source (device) == GDK_SOURCE_TOUCHSCREEN);

770 771 772 773 774 775 776 777
  if ((is_touch && GTK_GESTURE (gesture) == priv->drag_gesture) ||
      (!is_touch && GTK_GESTURE (gesture) == priv->pan_gesture))
    {
      gtk_gesture_set_state (GTK_GESTURE (gesture),
                             GTK_EVENT_SEQUENCE_DENIED);
      return;
    }

778
  if (event->any.window == priv->handle ||
779
      (is_touch && initiates_touch_drag (paned, start_x, start_y)))
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
    {
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        priv->drag_pos = start_x - (priv->handle_pos.x - allocation.x);
      else
        priv->drag_pos = start_y - (priv->handle_pos.y - allocation.y);

      gtk_gesture_set_state (GTK_GESTURE (gesture),
                             GTK_EVENT_SEQUENCE_CLAIMED);
    }
  else
    {
      gtk_gesture_set_state (GTK_GESTURE (gesture),
                             GTK_EVENT_SEQUENCE_DENIED);
    }
}

static void
797 798 799 800
gesture_drag_update_cb (GtkGestureDrag   *gesture,
                        gdouble           offset_x,
                        gdouble           offset_y,
                        GtkPaned         *paned)
801
{
802
  gdouble start_x, start_y;
803 804 805 806 807 808 809 810 811

  paned->priv->panning = TRUE;

  gtk_gesture_drag_get_start_point (GTK_GESTURE_DRAG (gesture),
                               &start_x, &start_y);
  update_drag (paned, start_x + offset_x, start_y + offset_y);
}

static void
812 813 814 815
gesture_drag_end_cb (GtkGestureDrag *gesture,
                     gdouble         offset_x,
                     gdouble         offset_y,
                     GtkPaned       *paned)
816 817 818 819 820
{
  if (!paned->priv->panning)
    gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_DENIED);
}

821
static void
822 823 824 825
gtk_paned_set_property (GObject        *object,
			guint           prop_id,
			const GValue   *value,
			GParamSpec     *pspec)
826
{
827
  GtkPaned *paned = GTK_PANED (object);
828
  GtkPanedPrivate *priv = paned->priv;
829

830
  switch (prop_id)
831
    {
832
    case PROP_ORIENTATION:
833
      if (priv->orientation != g_value_get_enum (value))
834
        {
835 836 837 838
          priv->orientation = g_value_get_enum (value);
          _gtk_orientable_set_style_classes (GTK_ORIENTABLE (paned));

          if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
839 840
            gtk_gesture_pan_set_orientation (GTK_GESTURE_PAN (priv->pan_gesture),
                                             GTK_ORIENTATION_HORIZONTAL);
841
          else
842 843
            gtk_gesture_pan_set_orientation (GTK_GESTURE_PAN (priv->pan_gesture),
                                             GTK_ORIENTATION_VERTICAL);
844 845 846 847 848

          /* state_flags_changed updates the cursor */
          gtk_paned_state_flags_changed (GTK_WIDGET (paned), 0);
          gtk_widget_queue_resize (GTK_WIDGET (paned));
          g_object_notify_by_pspec (object, pspec);
849
        }
850
      break;
851 852 853 854
    case PROP_POSITION:
      gtk_paned_set_position (paned, g_value_get_int (value));
      break;
    case PROP_POSITION_SET:
855 856 857 858 859 860
      if (priv->position_set != g_value_get_boolean (value))
        {
          priv->position_set = g_value_get_boolean (value);
          gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
          g_object_notify_by_pspec (object, pspec);
        }
861
      break;
862 863 864
    case PROP_WIDE_HANDLE:
      gtk_paned_set_wide_handle (paned, g_value_get_boolean (value));
      break;
865
    default:
866
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
867 868 869 870 871
      break;
    }
}

static void
872 873 874 875
gtk_paned_get_property (GObject        *object,
			guint           prop_id,
			GValue         *value,
			GParamSpec     *pspec)
876
{
877
  GtkPaned *paned = GTK_PANED (object);
878
  GtkPanedPrivate *priv = paned->priv;
879

880
  switch (prop_id)
881
    {
882
    case PROP_ORIENTATION:
883
      g_value_set_enum (value, priv->orientation);
884
      break;
885
    case PROP_POSITION:
886
      g_value_set_int (value, priv->child1_size);
887 888
      break;
    case PROP_POSITION_SET:
889
      g_value_set_boolean (value, priv->position_set);
890
      break;
891
    case PROP_MIN_POSITION:
892
      g_value_set_int (value, priv->min_position);
893 894
      break;
    case PROP_MAX_POSITION:
895
      g_value_set_int (value, priv->max_position);
896
      break;
897 898 899
    case PROP_WIDE_HANDLE:
      g_value_set_boolean (value, gtk_paned_get_wide_handle (paned));
      break;
900
    default:
901
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
902 903 904
      break;
    }
}
Elliot Lee's avatar
Elliot Lee committed
905

906 907 908 909 910 911 912 913
static void
gtk_paned_set_child_property (GtkContainer    *container,
			      GtkWidget       *child,
			      guint            property_id,
			      const GValue    *value,
			      GParamSpec      *pspec)
{
  GtkPaned *paned = GTK_PANED (container);
914
  GtkPanedPrivate *priv = paned->priv;
915 916
  gboolean old_value, new_value;

917
  g_assert (child == priv->child1 || child == priv->child2);
918 919 920 921 922

  new_value = g_value_get_boolean (value);
  switch (property_id)
    {
    case CHILD_PROP_RESIZE:
923
      if (child == priv->child1)
924
	{
925 926
	  old_value = priv->child1_resize;
	  priv->child1_resize = new_value;
927 928 929
	}
      else
	{
930 931
	  old_value = priv->child2_resize;
	  priv->child2_resize = new_value;
932 933 934
	}
      break;
    case CHILD_PROP_SHRINK:
935
      if (child == priv->child1)
936
	{
937 938
	  old_value = priv->child1_shrink;
	  priv->child1_shrink = new_value;
939 940 941
	}
      else
	{
942 943
	  old_value = priv->child2_shrink;
	  priv->child2_shrink = new_value;
944 945 946 947
	}
      break;
    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
948
      old_value = -1; /* quiet gcc */
949 950 951
      break;
    }
  if (old_value != new_value)
952
    gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
953 954 955 956 957 958 959 960 961 962
}

static void
gtk_paned_get_child_property (GtkContainer *container,
			      GtkWidget    *child,
			      guint         property_id,
			      GValue       *value,
			      GParamSpec   *pspec)
{
  GtkPaned *paned = GTK_PANED (container);
963
  GtkPanedPrivate *priv = paned->priv;
964

965
  g_assert (child == priv->child1 || child == priv->child2);
966 967 968 969
  
  switch (property_id)
    {
    case CHILD_PROP_RESIZE:
970 971
      if (child == priv->child1)
	g_value_set_boolean (value, priv->child1_resize);
972
      else
973
	g_value_set_boolean (value, priv->child2_resize);
974 975
      break;
    case CHILD_PROP_SHRINK:
976 977
      if (child == priv->child1)
	g_value_set_boolean (value, priv->child1_shrink);
978
      else
979
	g_value_set_boolean (value, priv->child2_shrink);
980 981 982 983 984 985 986
      break;
    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
      break;
    }
}

987 988 989 990 991 992 993 994
static void
gtk_paned_finalize (GObject *object)
{
  GtkPaned *paned = GTK_PANED (object);
  
  gtk_paned_set_saved_focus (paned, NULL);
  gtk_paned_set_first_paned (paned, NULL);

995
  g_clear_object (&paned->priv->pan_gesture);
996
  g_clear_object (&paned->priv->drag_gesture);
997

998 999 1000
  g_clear_object (&paned->priv->handle_gadget);
  g_clear_object (&paned->priv->gadget);

Matthias Clasen's avatar
Matthias Clasen committed
1001
  G_OBJECT_CLASS (gtk_paned_parent_class)->finalize (object);
1002 1003
}

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
static void
gtk_paned_compute_position (GtkPaned *paned,
                            gint      allocation,
                            gint      child1_req,
                            gint      child2_req,
                            gint     *min_pos,
                            gint     *max_pos,
                            gint     *out_pos)
{
  GtkPanedPrivate *priv = paned->priv;
  gint min, max, pos;

  min = priv->child1_shrink ? 0 : child1_req;

  max = allocation;
  if (!priv->child2_shrink)
    max = MAX (1, max - child2_req);
  max = MAX (min, max);

  if (!priv->position_set)
    {
      if (priv->child1_resize && !priv->child2_resize)
	pos = MAX (0, allocation - child2_req);
      else if (!priv->child1_resize && priv->child2_resize)
	pos = child1_req;
      else if (child1_req + child2_req != 0)
	pos = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
      else
	pos = allocation * 0.5 + 0.5;
    }
  else
    {
      /* If the position was set before the initial allocation.
       * (priv->last_allocation <= 0) just clamp it and leave it.
       */
      if (priv->last_allocation > 0)
	{
	  if (priv->child1_resize && !priv->child2_resize)
	    pos = priv->child1_size + allocation - priv->last_allocation;
	  else if (!(!priv->child1_resize && priv->child2_resize))
	    pos = allocation * ((gdouble) priv->child1_size / (priv->last_allocation)) + 0.5;
1045 1046
          else
            pos = priv->child1_size;
1047 1048
	}
      else
1049
        pos = priv->child1_size;
1050 1051 1052 1053 1054
    }

  pos = CLAMP (pos, min, max);
  
  if (min_pos)
1055
    *min_pos = min;
1056
  if (max_pos)
1057
    *max_pos = max;
1058 1059 1060 1061
  if (out_pos)
    *out_pos = pos;
}

1062
static void
1063 1064 1065 1066
gtk_paned_get_preferred_size_for_orientation (GtkWidget      *widget,
                                              gint            size,
                                              gint           *minimum,
                                              gint           *natural)
1067
{
1068
  GtkPaned *paned = GTK_PANED (widget);
1069
  GtkPanedPrivate *priv = paned->priv;
1070
  gint child_min, child_nat;
1071

1072
  *minimum = *natural = 0;
1073

1074
  if (priv->child1 && gtk_widget_get_visible (priv->child1))
1075
    {
1076 1077 1078
      _gtk_widget_get_preferred_size_for_size (priv->child1, priv->orientation, size, &child_min, &child_nat, NULL, NULL);
      if (priv->child1_shrink)
        *minimum = 0;
1079
      else
1080 1081
        *minimum = child_min;
      *natural = child_nat;
1082
    }
1083 1084

  if (priv->child2 && gtk_widget_get_visible (priv->child2))
1085
    {
1086 1087 1088 1089 1090
      _gtk_widget_get_preferred_size_for_size (priv->child2, priv->orientation, size, &child_min, &child_nat, NULL, NULL);

      if (!priv->child2_shrink)
        *minimum += child_min;
      *natural += child_nat;
1091 1092
    }

1093 1094 1095 1096 1097
  if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
      priv->child2 && gtk_widget_get_visible (priv->child2))
    {
      gint handle_size;

1098 1099 1100 1101 1102
      gtk_css_gadget_get_preferred_size (priv->handle_gadget,
                                         priv->orientation,
                                         -1,
                                         NULL, &handle_size,
                                         NULL, NULL);
1103 1104 1105 1106

      *minimum += handle_size;
      *natural += handle_size;
    }
1107 1108
}

1109
static void
1110 1111 1112 1113
gtk_paned_get_preferred_size_for_opposite_orientation (GtkWidget      *widget,
                                                       gint            size,
                                                       gint           *minimum,
                                                       gint           *natural)
1114 1115
{
  GtkPaned *paned = GTK_PANED (widget);
1116
  GtkPanedPrivate *priv = paned->priv;
1117
  gint for_child1, for_child2;
1118
  gint child_min, child_nat;
1119

1120 1121 1122 1123 1124 1125 1126
  if (size > -1 &&
      priv->child1 && gtk_widget_get_visible (priv->child1) &&
      priv->child2 && gtk_widget_get_visible (priv->child2))
    {
      gint child1_req, child2_req;
      gint handle_size;

1127 1128 1129 1130 1131
      gtk_css_gadget_get_preferred_size (priv->handle_gadget,
                                         OPPOSITE_ORIENTATION (priv->orientation),
                                         -1,
                                         NULL, &handle_size,
                                         NULL, NULL);
1132 1133 1134 1135 1136

      _gtk_widget_get_preferred_size_for_size (priv->child1, priv->orientation, -1, &child1_req, NULL, NULL, NULL);
      _gtk_widget_get_preferred_size_for_size (priv->child2, priv->orientation, -1, &child2_req, NULL, NULL, NULL);

      gtk_paned_compute_position (paned,
1137
                                  size - handle_size, child1_req, child2_req,
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
                                  NULL, NULL, &for_child1);

      for_child2 = size - for_child1 - handle_size;
    }
  else
    {
      for_child1 = size;
      for_child2 = size;
    }

1148
  *minimum = *natural = 0;
1149

1150
  if (priv->child1 && gtk_widget_get_visible (priv->child1))
1151
    {
1152 1153
      _gtk_widget_get_preferred_size_for_size (priv->child1,
                                               OPPOSITE_ORIENTATION (priv->orientation),
1154
                                               for_child1,
1155 1156
                                               &child_min, &child_nat,
                                               NULL, NULL);
1157 1158
      
      *minimum = child_min;
1159
      *natural = child_nat;
1160 1161
    }

1162
  if (priv->child2 && gtk_widget_get_visible (priv->child2))
1163
    {
1164 1165
      _gtk_widget_get_preferred_size_for_size (priv->child2,
                                               OPPOSITE_ORIENTATION (priv->orientation),
1166
                                               for_child2,
1167 1168
                                               &child_min, &child_nat,
                                               NULL, NULL);
1169

1170 1171 1172 1173
      *minimum = MAX (*minimum, child_min);
      *natural = MAX (*natural, child_nat);
    }
}
1174

1175 1176 1177
static gint
get_number (GtkCssStyle *style,
            guint        property)
1178
{
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
  double d = _gtk_css_number_value_get (gtk_css_style_get_value (style, property), 100.0);

  if (d < 1)
    return ceil (d);
  else
    return floor (d);
}

static void
gtk_paned_measure_handle (GtkCssGadget   *gadget,
                          GtkOrientation  orientation,
                          int             size,
                          int            *minimum,
                          int            *natural,
                          int            *minimum_baseline,
                          int            *natural_baseline,
                          gpointer        data)
{
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
  GtkCssStyle *style;
  gint min_size;

  style = gtk_css_gadget_get_style (gadget);
1202 1203 1204 1205 1206 1207 1208 1209
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    min_size = get_number (style, GTK_CSS_PROPERTY_MIN_WIDTH);
  else
    min_size = get_number (style, GTK_CSS_PROPERTY_MIN_HEIGHT);

  if (min_size != 0)
    *minimum = *natural = min_size;
  else
1210
    {
1211
      GtkStyleContext *context;
1212

1213 1214 1215 1216 1217 1218
      context = gtk_widget_get_style_context (widget);
      gtk_style_context_save_to_node (context, gtk_css_gadget_get_node (gadget));
      gtk_widget_style_get (widget, "handle-size", &min_size, NULL);
      gtk_style_context_restore (context);

      *minimum = *natural = min_size;
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
    }
}

static void
gtk_paned_measure (GtkCssGadget   *gadget,
                   GtkOrientation  orientation,
                   int             size,
                   int            *minimum,
                   int            *natural,
                   int            *minimum_baseline,
                   int            *natural_baseline,
                   gpointer        data)
{
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
1233 1234
  GtkPaned *paned = GTK_PANED (widget);
  GtkPanedPrivate *priv = paned->priv;
1235

1236
  if (orientation == priv->orientation)
1237
    gtk_paned_get_preferred_size_for_orientation (widget, size, minimum, natural);
1238
  else
1239
    gtk_paned_get_preferred_size_for_opposite_orientation (widget, size, minimum, natural);
1240 1241
}

1242 1243 1244 1245 1246
static void
gtk_paned_get_preferred_width (GtkWidget *widget,
                               gint      *minimum,
                               gint      *natural)
{
1247 1248 1249 1250 1251
  gtk_css_gadget_get_preferred_size (GTK_PANED (widget)->priv->gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     minimum, natural,
                                     NULL, NULL);
1252 1253 1254 1255 1256 1257 1258
}

static void
gtk_paned_get_preferred_height (GtkWidget *widget,
                                gint      *minimum,
                                gint      *natural)
{
1259 1260 1261 1262 1263
  gtk_css_gadget_get_preferred_size (GTK_PANED (widget)->priv->gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     -1,
                                     minimum, natural,
                                     NULL, NULL);
1264 1265
}

1266 1267 1268 1269 1270 1271
static void
gtk_paned_get_preferred_width_for_height (GtkWidget *widget,
                                          gint       height,
                                          gint      *minimum,
                                          gint      *natural)
{
1272 1273 1274 1275 1276
  gtk_css_gadget_get_preferred_size (GTK_PANED (widget)->priv->gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     height,
                                     minimum, natural,
                                     NULL, NULL);
1277 1278 1279 1280 1281 1282 1283 1284
}

static void
gtk_paned_get_preferred_height_for_width (GtkWidget *widget,
                                          gint       width,
                                          gint      *minimum,
                                          gint      *natural)
{
1285 1286 1287 1288 1289
  gtk_css_gadget_get_preferred_size (GTK_PANED (widget)->priv->gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     width,
                                     minimum, natural,
                                     NULL, NULL);
1290 1291
}

1292
static void
1293 1294
flip_child (const GtkAllocation *allocation,
            GtkAllocation       *child_pos)
1295
{
1296 1297
  gint x, width;

1298 1299
  x = allocation->x;
  width = allocation->width;
1300 1301 1302 1303

  child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
}

1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
static void
gtk_paned_set_child_visible (GtkPaned  *paned,
                             guint      id,
                             gboolean   visible)
{
  GtkPanedPrivate *priv = paned->priv;
  GtkWidget *child;

  child = id == CHILD1 ? priv->child1 : priv->child2;

  if (child == NULL)
    return;

  gtk_widget_set_child_visible (child, visible);

  if (gtk_widget_get_mapped (GTK_WIDGET (paned)))
    {
      GdkWindow *window = id == CHILD1 ? priv->child1_window : priv->child2_window;

1323 1324 1325 1326 1327 1328 1329
      if (visible != gdk_window_is_visible (window))
        {
          if (visible)
            gdk_window_show (window);
          else
            gdk_window_hide (window);
        }
1330 1331 1332
    }
}

1333 1334 1335
static void
gtk_paned_child_allocate (GtkWidget           *child,
                          GdkWindow           *child_window, /* can be NULL */
1336 1337
                          const GtkAllocation *window_allocation,
                          GtkAllocation       *child_allocation)
1338 1339 1340
{
  if (child_window)
    gdk_window_move_resize (child_window,
1341 1342
                            window_allocation->x, window_allocation->y,
                            window_allocation->width, window_allocation->height);
1343

1344
  gtk_widget_size_allocate (child, child_allocation);
1345
}
1346

1347 1348 1349 1350 1351
static void
gtk_paned_size_allocate (GtkWidget     *widget,
                         GtkAllocation *allocation)
{
  GtkPaned *paned = GTK_PANED (widget);
1352
  GtkPanedPrivate *priv = paned->priv;
1353
  GtkAllocation clip;
1354

1355
  gtk_widget_set_allocation (widget, allocation);
1356

1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
  gtk_css_gadget_allocate (priv->gadget,
                           allocation,
                           gtk_widget_get_allocated_baseline (widget),
                           &clip);

  gtk_widget_set_clip (widget, &clip);
}

static void
gtk_paned_allocate (GtkCssGadget        *gadget,
                    const GtkAllocation *allocation,
                    int                  baseline,
                    GtkAllocation       *out_clip,
                    gpointer             data)
{
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
  GtkPaned *paned = GTK_PANED (widget);
  GtkPanedPrivate *priv = paned->priv;
1375
  GtkAllocation clip = { 0 };
1376

1377 1378
  if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
      priv->child2 && gtk_widget_get_visible (priv->child2))
1379
    {
1380 1381
      GtkAllocation child1_allocation, window1_allocation;
      GtkAllocation child2_allocation, window2_allocation;
1382
      GtkAllocation priv_child1_allocation;
1383 1384 1385
      GdkRectangle old_handle_pos;
      gint handle_size;

1386 1387 1388 1389 1390
      gtk_css_gadget_get_preferred_size (priv->handle_gadget,
                                         priv->orientation,
                                         -1,
                                         NULL, &handle_size,
                                         NULL, NULL);
1391

1392
      old_handle_pos = priv->handle_pos;
1393

1394
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1395
        {
1396 1397 1398 1399 1400 1401 1402 1403 1404
          gint child1_width, child2_width;

          gtk_widget_get_preferred_width_for_height (priv->child1,
                                                     allocation->height,
                                                     &child1_width, NULL);
          gtk_widget_get_preferred_width_for_height (priv->child2,
                                                     allocation->height,
                                                     &child2_width, NULL);

1405
          gtk_paned_calc_position (paned,
1406
                                   MAX (1, allocation->width - handle_size),
1407 1408
                                   child1_width,
                                   child2_width);
1409

1410 1411
          priv->handle_pos.x = allocation->x + priv->child1_size;
          priv->handle_pos.y = allocation->y;
1412
          priv->handle_pos.width = handle_size;
1413
          priv->handle_pos.height = allocation->height;
1414

1415 1416 1417 1418
          window1_allocation.height = window2_allocation.height = allocation->height;
          window1_allocation.width = MAX (1, priv->child1_size);
          window1_allocation.x = allocation->x;
          window1_allocation.y = window2_allocation.y = allocation->y;
1419