gtkpaned.c 88.6 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 "a11y/gtkpanedaccessible.h"
Elliot Lee's avatar
Elliot Lee committed
40

41 42
/**
 * SECTION:gtkpaned
43
 * @Short_description: A widget with two adjustable panes
44 45
 * @Title: GtkPaned
 *
46 47 48
 * #GtkPaned has two panes, arranged either
 * horizontally or vertically. The division between
 * the two panes is adjustable by the user by dragging
49
 * a handle.
50 51
 *
 * Child widgets are
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
 * 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().
 *
76 77
 * ## Creating a paned widget with minimum sizes.
 *
78
 * |[<!-- language="C" -->
79 80 81 82 83 84 85 86 87 88 89 90 91
 * 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);
92
 * ]|
93
 */
94

95 96 97 98 99
enum {
  CHILD1,
  CHILD2
};

100 101 102 103 104
struct _GtkPanedPrivate
{
  GtkPaned       *first_paned;
  GtkWidget      *child1;
  GtkWidget      *child2;
105 106
  GdkWindow      *child1_window;
  GdkWindow      *child2_window;
107 108 109
  GtkWidget      *last_child1_focus;
  GtkWidget      *last_child2_focus;
  GtkWidget      *saved_focus;
110
  GtkOrientation  orientation;
111 112 113 114 115

  GdkCursorType  cursor_type;
  GdkRectangle   handle_pos;
  GdkWindow     *handle;

116 117
  GtkGesture    *pan_gesture;

118 119 120 121 122 123 124 125 126 127 128 129 130 131
  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;
132
  guint         panning       : 1;
133 134
};

135
enum {
136
  PROP_0,
137
  PROP_ORIENTATION,
138
  PROP_POSITION,
139 140
  PROP_POSITION_SET,
  PROP_MIN_POSITION,
141 142
  PROP_MAX_POSITION,
  PROP_WIDE_HANDLE
143
};
Elliot Lee's avatar
Elliot Lee committed
144

145 146 147 148 149 150
enum {
  CHILD_PROP_0,
  CHILD_PROP_RESIZE,
  CHILD_PROP_SHRINK
};

151 152 153 154 155 156
enum {
  CYCLE_CHILD_FOCUS,
  TOGGLE_HANDLE_FOCUS,
  MOVE_HANDLE,
  CYCLE_HANDLE_FOCUS,
  ACCEPT_POSITION,
157 158
  CANCEL_POSITION,
  LAST_SIGNAL
159 160 161 162 163 164 165 166 167 168
};

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);
169 170 171 172 173 174 175 176 177 178
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);
179
static void     gtk_paned_finalize              (GObject          *object);
180

181 182 183 184 185 186
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);
187 188 189 190 191 192 193 194 195 196
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);
197

198 199
static void     gtk_paned_size_allocate         (GtkWidget        *widget,
                                                 GtkAllocation    *allocation);
200 201 202 203
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);
204 205
static void     gtk_paned_state_flags_changed   (GtkWidget        *widget,
                                                 GtkStateFlags     previous_state);
206 207
static gboolean gtk_paned_draw                  (GtkWidget        *widget,
						 cairo_t          *cr);
208 209 210 211
static gboolean gtk_paned_enter                 (GtkWidget        *widget,
						 GdkEventCrossing *event);
static gboolean gtk_paned_leave                 (GtkWidget        *widget,
						 GdkEventCrossing *event);
212 213 214 215 216 217 218 219 220 221
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);
222 223 224 225
static void     gtk_paned_calc_position         (GtkPaned         *paned,
                                                 gint              allocation,
                                                 gint              child1_req,
                                                 gint              child2_req);
226 227 228 229
static void     gtk_paned_set_focus_child       (GtkContainer     *container,
						 GtkWidget        *child);
static void     gtk_paned_set_saved_focus       (GtkPaned         *paned,
						 GtkWidget        *widget);
230 231
static void     gtk_paned_set_first_paned       (GtkPaned         *paned,
						 GtkPaned         *first_paned);
232 233 234 235 236 237 238 239 240 241 242 243 244
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);
245

246
static GType    gtk_paned_child_type            (GtkContainer     *container);
Elliot Lee's avatar
Elliot Lee committed
247

248 249 250
static void     update_drag                     (GtkPaned         *paned,
                                                 int               xpos,
                                                 int               ypos);
251

252
G_DEFINE_TYPE_WITH_CODE (GtkPaned, gtk_paned, GTK_TYPE_CONTAINER,
253
                         G_ADD_PRIVATE (GtkPaned)
254 255
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
                                                NULL))
Elliot Lee's avatar
Elliot Lee committed
256

257 258
static guint signals[LAST_SIGNAL] = { 0 };

259

260 261
static void
add_tab_bindings (GtkBindingSet    *binding_set,
262
		  GdkModifierType   modifiers)
263
{
264
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
265
                                "toggle-handle-focus", 0);
266
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
267
				"toggle-handle-focus", 0);
268 269 270 271 272 273 274 275 276
}

static void
add_move_binding (GtkBindingSet   *binding_set,
		  guint            keyval,
		  GdkModifierType  mask,
		  GtkScrollType    scroll)
{
  gtk_binding_entry_add_signal (binding_set, keyval, mask,
277
				"move-handle", 1,
278 279 280
				GTK_TYPE_SCROLL_TYPE, scroll);
}

Elliot Lee's avatar
Elliot Lee committed
281 282 283
static void
gtk_paned_class_init (GtkPanedClass *class)
{
284
  GObjectClass *object_class;
Elliot Lee's avatar
Elliot Lee committed
285 286
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;
287 288
  GtkPanedClass *paned_class;
  GtkBindingSet *binding_set;
289

290
  object_class = (GObjectClass *) class;
291 292
  widget_class = (GtkWidgetClass *) class;
  container_class = (GtkContainerClass *) class;
293
  paned_class = (GtkPanedClass *) class;
294

295 296
  object_class->set_property = gtk_paned_set_property;
  object_class->get_property = gtk_paned_get_property;
297
  object_class->finalize = gtk_paned_finalize;
298

299 300
  widget_class->get_preferred_width = gtk_paned_get_preferred_width;
  widget_class->get_preferred_height = gtk_paned_get_preferred_height;
301 302
  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;
303
  widget_class->size_allocate = gtk_paned_size_allocate;
Elliot Lee's avatar
Elliot Lee committed
304 305
  widget_class->realize = gtk_paned_realize;
  widget_class->unrealize = gtk_paned_unrealize;
306 307
  widget_class->map = gtk_paned_map;
  widget_class->unmap = gtk_paned_unmap;
308
  widget_class->draw = gtk_paned_draw;
309
  widget_class->focus = gtk_paned_focus;
310 311
  widget_class->enter_notify_event = gtk_paned_enter;
  widget_class->leave_notify_event = gtk_paned_leave;
312
  widget_class->state_flags_changed = gtk_paned_state_flags_changed;
313

Elliot Lee's avatar
Elliot Lee committed
314 315
  container_class->add = gtk_paned_add;
  container_class->remove = gtk_paned_remove;
316
  container_class->forall = gtk_paned_forall;
317
  container_class->child_type = gtk_paned_child_type;
318
  container_class->set_focus_child = gtk_paned_set_focus_child;
319 320
  container_class->set_child_property = gtk_paned_set_child_property;
  container_class->get_child_property = gtk_paned_get_child_property;
321
  gtk_container_class_handle_border_width (container_class);
322 323 324 325 326 327 328

  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;
329

330 331 332 333
  g_object_class_override_property (object_class,
                                    PROP_ORIENTATION,
                                    "orientation");

334
  g_object_class_install_property (object_class,
335 336 337 338 339 340
                                   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));
341

342
  g_object_class_install_property (object_class,
343 344 345 346 347 348
                                   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));
349

350 351 352
  /**
   * GtkPaned:min-position:
   *
353 354 355
   * The smallest possible value for the position property.
   * This property is derived from the size and shrinkability
   * of the widget's children.
356 357 358 359
   *
   * Since: 2.4
   */
  g_object_class_install_property (object_class,
360 361 362 363 364 365
                                   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));
366 367 368 369

  /**
   * GtkPaned:max-position:
   *
370 371 372
   * The largest possible value for the position property.
   * This property is derived from the size and shrinkability
   * of the widget's children.
373 374 375 376
   *
   * Since: 2.4
   */
  g_object_class_install_property (object_class,
377 378 379 380 381 382 383
                                   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));

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  /**
   * 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));

401 402 403 404 405 406 407 408
  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,
							     GTK_PARAM_READABLE));
409

410 411 412 413 414 415 416 417
  /**
   * GtkPaned:resize:
   *
   * The "resize" child property determines whether the child expands and
   * shrinks along with the paned widget.
   *
   * Since: 2.4
   */
418 419 420
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_RESIZE,
					      g_param_spec_boolean ("resize", 
421 422
								    P_("Resize"),
								    P_("If TRUE, the child expands and shrinks along with the paned widget"),
423
								    TRUE,
424
								    GTK_PARAM_READWRITE));
425

426 427 428 429 430 431 432 433
  /**
   * GtkPaned:shrink:
   *
   * The "shrink" child property determines whether the child can be made
   * smaller than its requisition.
   *
   * Since: 2.4
   */
434 435 436
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_SHRINK,
					      g_param_spec_boolean ("shrink", 
437 438
								    P_("Shrink"),
								    P_("If TRUE, the child can be made smaller than its requisition"),
439
								    TRUE,
440
								    GTK_PARAM_READWRITE));
441

442 443 444 445 446 447
  /**
   * GtkPaned::cycle-child-focus:
   * @widget: the object that received the signal
   * @reversed: whether cycling backward or forward
   *
   * The ::cycle-child-focus signal is a 
448
   * [keybinding signal][GtkBindingSignal]
449 450 451 452 453 454
   * which gets emitted to cycle the focus between the children of the paned.
   *
   * The default binding is f6.
   *
   * Since: 2.0
   */
455
  signals [CYCLE_CHILD_FOCUS] =
456
    g_signal_new (I_("cycle-child-focus"),
457 458 459 460 461 462 463 464
		  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);

465 466 467 468 469
  /**
   * GtkPaned::toggle-handle-focus:
   * @widget: the object that received the signal
   *
   * The ::toggle-handle-focus is a 
470
   * [keybinding signal][GtkBindingSignal]
471 472 473 474 475 476 477
   * 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
   */
478
  signals [TOGGLE_HANDLE_FOCUS] =
479
    g_signal_new (I_("toggle-handle-focus"),
480 481 482 483 484 485 486
		  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);

487 488 489 490 491 492
  /**
   * GtkPaned::move-handle:
   * @widget: the object that received the signal
   * @scroll_type: a #GtkScrollType
   *
   * The ::move-handle signal is a 
493
   * [keybinding signal][GtkBindingSignal]
494 495 496 497 498
   * which gets emitted to move the handle when the user is using key bindings 
   * to move it.
   *
   * Since: 2.0
   */
499
  signals[MOVE_HANDLE] =
500
    g_signal_new (I_("move-handle"),
501 502 503 504 505 506 507 508
		  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);

509 510 511 512 513 514
  /**
   * GtkPaned::cycle-handle-focus:
   * @widget: the object that received the signal
   * @reversed: whether cycling backward or forward
   *
   * The ::cycle-handle-focus signal is a 
515
   * [keybinding signal][GtkBindingSignal]
516 517 518 519 520 521 522
   * 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
   */
523
  signals [CYCLE_HANDLE_FOCUS] =
524
    g_signal_new (I_("cycle-handle-focus"),
525 526 527 528 529 530 531 532
		  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);

533 534 535 536 537
  /**
   * GtkPaned::accept-position:
   * @widget: the object that received the signal
   *
   * The ::accept-position signal is a 
538
   * [keybinding signal][GtkBindingSignal]
539 540 541 542 543 544 545
   * 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
   */
546
  signals [ACCEPT_POSITION] =
547
    g_signal_new (I_("accept-position"),
548 549 550 551 552 553 554
		  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);

555 556 557 558 559
  /**
   * GtkPaned::cancel-position:
   * @widget: the object that received the signal
   *
   * The ::cancel-position signal is a 
560
   * [keybinding signal][GtkBindingSignal]
561 562 563 564 565 566 567 568
   * 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
   */
569
  signals [CANCEL_POSITION] =
570
    g_signal_new (I_("cancel-position"),
571 572 573 574 575 576 577
		  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
578
  binding_set = gtk_binding_set_by_class (class);
579 580

  /* F6 and friends */
581
  gtk_binding_entry_add_signal (binding_set,
582
                                GDK_KEY_F6, 0,
583
                                "cycle-child-focus", 1, 
584 585
                                G_TYPE_BOOLEAN, FALSE);
  gtk_binding_entry_add_signal (binding_set,
586
				GDK_KEY_F6, GDK_SHIFT_MASK,
587
				"cycle-child-focus", 1,
588 589 590 591
				G_TYPE_BOOLEAN, TRUE);

  /* F8 and friends */
  gtk_binding_entry_add_signal (binding_set,
592
				GDK_KEY_F8, 0,
593
				"cycle-handle-focus", 1,
594 595 596
				G_TYPE_BOOLEAN, FALSE);
 
  gtk_binding_entry_add_signal (binding_set,
597
				GDK_KEY_F8, GDK_SHIFT_MASK,
598
				"cycle-handle-focus", 1,
599
				G_TYPE_BOOLEAN, TRUE);
600
 
601 602 603 604
  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);
605 606 607

  /* accept and cancel positions */
  gtk_binding_entry_add_signal (binding_set,
608
				GDK_KEY_Escape, 0,
609
				"cancel-position", 0);
610 611

  gtk_binding_entry_add_signal (binding_set,
612
				GDK_KEY_Return, 0,
613
				"accept-position", 0);
614
  gtk_binding_entry_add_signal (binding_set,
615
				GDK_KEY_ISO_Enter, 0,
616
				"accept-position", 0);
617
  gtk_binding_entry_add_signal (binding_set,
618
				GDK_KEY_KP_Enter, 0,
619
				"accept-position", 0);
620
  gtk_binding_entry_add_signal (binding_set,
621
				GDK_KEY_space, 0,
622
				"accept-position", 0);
623
  gtk_binding_entry_add_signal (binding_set,
624
				GDK_KEY_KP_Space, 0,
625
				"accept-position", 0);
626 627

  /* move handle */
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
  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);
656

657
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PANED_ACCESSIBLE);
658 659
}

Manish Singh's avatar
Manish Singh committed
660
static GType
661 662
gtk_paned_child_type (GtkContainer *container)
{
663 664 665 666
  GtkPaned *paned = GTK_PANED (container);
  GtkPanedPrivate *priv = paned->priv;

  if (!priv->child1 || !priv->child2)
667 668
    return GTK_TYPE_WIDGET;
  else
Manish Singh's avatar
Manish Singh committed
669
    return G_TYPE_NONE;
Elliot Lee's avatar
Elliot Lee committed
670 671
}

672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
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
  gtk_widget_get_allocation (GTK_WIDGET (paned), &allocation);
  gtk_widget_style_get (GTK_WIDGET (paned), "handle-size", &handle_size, NULL);

  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
pan_gesture_drag_begin_cb (GtkGestureDrag *gesture,
                           gdouble         start_x,
                           gdouble         start_y,
                           GtkPaned       *paned)
{
  GtkPanedPrivate *priv = paned->priv;
  GdkEventSequence *sequence;
  GtkAllocation allocation;
  const GdkEvent *event;
715 716
  GdkDevice *device;
  gboolean is_touch;
717 718 719

  sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
  event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
720
  device = gdk_event_get_source_device (event);
721 722 723
  gtk_widget_get_allocation (GTK_WIDGET (paned), &allocation);
  paned->priv->panning = FALSE;

724 725 726
  is_touch = (event->type == GDK_TOUCH_BEGIN ||
              gdk_device_get_source (device) == GDK_SOURCE_TOUCHSCREEN);

727
  if (event->any.window == priv->handle ||
728
      (is_touch && initiates_touch_drag (paned, start_x, start_y)))
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
    {
      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
pan_gesture_pan_cb (GtkGesturePan   *gesture,
                    GtkPanDirection  direction,
                    gdouble          offset,
                    GtkPaned        *paned)
{
  gdouble start_x, start_y, offset_x, offset_y;

  paned->priv->panning = TRUE;

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

static void
pan_gesture_drag_end_cb (GtkGestureDrag *gesture,
                         gdouble         offset_x,
                         gdouble         offset_y,
                         GtkPaned       *paned)
{
  if (!paned->priv->panning)
    gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_DENIED);
}

Elliot Lee's avatar
Elliot Lee committed
772 773 774
static void
gtk_paned_init (GtkPaned *paned)
{
775
  GtkPanedPrivate *priv;
776
  GtkGesture *gesture;
777

778
  gtk_widget_set_has_window (GTK_WIDGET (paned), FALSE);
779
  gtk_widget_set_can_focus (GTK_WIDGET (paned), TRUE);
780 781

  /* We only need to redraw when the handle position moves, which is
782 783
   * independent of the overall allocation of the GtkPaned
   */
784 785
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (paned), FALSE);

786
  paned->priv = gtk_paned_get_instance_private (paned);
787
  priv = paned->priv;
788

789 790
  priv->orientation = GTK_ORIENTATION_HORIZONTAL;
  priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
791

792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
  priv->child1 = NULL;
  priv->child2 = NULL;
  priv->handle = NULL;

  priv->handle_pos.width = 5;
  priv->handle_pos.height = 5;
  priv->position_set = FALSE;
  priv->last_allocation = -1;

  priv->last_child1_focus = NULL;
  priv->last_child2_focus = NULL;
  priv->in_recursion = FALSE;
  priv->handle_prelit = FALSE;
  priv->original_position = -1;

  priv->handle_pos.x = -1;
  priv->handle_pos.y = -1;
809

810 811
  _gtk_orientable_set_style_classes (GTK_ORIENTABLE (paned));

812
  gesture = gtk_gesture_pan_new (GTK_WIDGET (paned),
813
                                 GTK_ORIENTATION_HORIZONTAL);
814 815 816 817 818 819 820
  gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (gesture), FALSE);
  g_signal_connect (gesture, "drag-begin",
                    G_CALLBACK (pan_gesture_drag_begin_cb), paned);
  g_signal_connect (gesture, "pan",
                    G_CALLBACK (pan_gesture_pan_cb), paned);
  g_signal_connect (gesture, "drag-end",
                    G_CALLBACK (pan_gesture_drag_end_cb), paned);
821 822
  gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
                                              GTK_PHASE_CAPTURE);
823
  priv->pan_gesture = gesture;
Elliot Lee's avatar
Elliot Lee committed
824 825
}

826
static void
827 828 829 830
gtk_paned_set_property (GObject        *object,
			guint           prop_id,
			const GValue   *value,
			GParamSpec     *pspec)
831
{
832
  GtkPaned *paned = GTK_PANED (object);
833
  GtkPanedPrivate *priv = paned->priv;
834

835
  switch (prop_id)
836
    {
837
    case PROP_ORIENTATION:
838
      if (priv->orientation != g_value_get_enum (value))
839
        {
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
          priv->orientation = g_value_get_enum (value);
          _gtk_orientable_set_style_classes (GTK_ORIENTABLE (paned));

          if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
            {
              priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
              gtk_gesture_pan_set_orientation (GTK_GESTURE_PAN (priv->pan_gesture),
                                               GTK_ORIENTATION_HORIZONTAL);
            }
          else
            {
              priv->cursor_type = GDK_SB_V_DOUBLE_ARROW;
              gtk_gesture_pan_set_orientation (GTK_GESTURE_PAN (priv->pan_gesture),
                                               GTK_ORIENTATION_VERTICAL);
            }

          /* 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);
860
        }
861
      break;
862 863 864 865
    case PROP_POSITION:
      gtk_paned_set_position (paned, g_value_get_int (value));
      break;
    case PROP_POSITION_SET:
866 867 868 869 870 871
      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);
        }
872
      break;
873 874 875
    case PROP_WIDE_HANDLE:
      gtk_paned_set_wide_handle (paned, g_value_get_boolean (value));
      break;
876
    default:
877
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
878 879 880 881 882
      break;
    }
}

static void
883 884 885 886
gtk_paned_get_property (GObject        *object,
			guint           prop_id,
			GValue         *value,
			GParamSpec     *pspec)
887
{
888
  GtkPaned *paned = GTK_PANED (object);
889
  GtkPanedPrivate *priv = paned->priv;
890

891
  switch (prop_id)
892
    {
893
    case PROP_ORIENTATION:
894
      g_value_set_enum (value, priv->orientation);
895
      break;
896
    case PROP_POSITION:
897
      g_value_set_int (value, priv->child1_size);
898 899
      break;
    case PROP_POSITION_SET:
900
      g_value_set_boolean (value, priv->position_set);
901
      break;
902
    case PROP_MIN_POSITION:
903
      g_value_set_int (value, priv->min_position);
904 905
      break;
    case PROP_MAX_POSITION:
906
      g_value_set_int (value, priv->max_position);
907
      break;
908 909 910
    case PROP_WIDE_HANDLE:
      g_value_set_boolean (value, gtk_paned_get_wide_handle (paned));
      break;
911
    default:
912
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
913 914 915
      break;
    }
}
Elliot Lee's avatar
Elliot Lee committed
916

917 918 919 920 921 922 923 924
static void
gtk_paned_set_child_property (GtkContainer    *container,
			      GtkWidget       *child,
			      guint            property_id,
			      const GValue    *value,
			      GParamSpec      *pspec)
{
  GtkPaned *paned = GTK_PANED (container);
925
  GtkPanedPrivate *priv = paned->priv;
926 927
  gboolean old_value, new_value;

928
  g_assert (child == priv->child1 || child == priv->child2);
929 930 931 932 933

  new_value = g_value_get_boolean (value);
  switch (property_id)
    {
    case CHILD_PROP_RESIZE:
934
      if (child == priv->child1)
935
	{
936 937
	  old_value = priv->child1_resize;
	  priv->child1_resize = new_value;
938 939 940
	}
      else
	{
941 942
	  old_value = priv->child2_resize;
	  priv->child2_resize = new_value;
943 944 945
	}
      break;
    case CHILD_PROP_SHRINK:
946
      if (child == priv->child1)
947
	{
948 949
	  old_value = priv->child1_shrink;
	  priv->child1_shrink = new_value;
950 951 952
	}
      else
	{
953 954
	  old_value = priv->child2_shrink;
	  priv->child2_shrink = new_value;
955 956 957 958
	}
      break;
    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
959
      old_value = -1; /* quiet gcc */
960 961 962
      break;
    }
  if (old_value != new_value)
963
    gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
964 965 966 967 968 969 970 971 972 973
}

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

976
  g_assert (child == priv->child1 || child == priv->child2);
977 978 979 980
  
  switch (property_id)
    {
    case CHILD_PROP_RESIZE:
981 982
      if (child == priv->child1)
	g_value_set_boolean (value, priv->child1_resize);
983
      else
984
	g_value_set_boolean (value, priv->child2_resize);
985 986
      break;
    case CHILD_PROP_SHRINK:
987 988
      if (child == priv->child1)
	g_value_set_boolean (value, priv->child1_shrink);
989
      else
990
	g_value_set_boolean (value, priv->child2_shrink);
991 992 993 994 995 996 997
      break;
    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
      break;
    }
}

998 999 1000 1001 1002 1003 1004 1005
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);

1006 1007
  g_clear_object (&paned->priv->pan_gesture);

Matthias Clasen's avatar
Matthias Clasen committed
1008
  G_OBJECT_CLASS (gtk_paned_parent_class)->finalize (object);
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 1045 1046 1047 1048 1049 1050 1051
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;
1052 1053
          else
            pos = priv->child1_size;
1054 1055
	}
      else
1056
        pos = priv->child1_size;
1057 1058 1059 1060 1061
    }

  pos = CLAMP (pos, min, max);
  
  if (min_pos)
1062
    *min_pos = min;
1063
  if (max_pos)
1064
    *max_pos = max;
1065 1066 1067 1068
  if (out_pos)
    *out_pos = pos;
}

1069
static void
1070 1071 1072 1073
gtk_paned_get_preferred_size_for_orientation (GtkWidget      *widget,
                                              gint            size,
                                              gint           *minimum,
                                              gint           *natural)
1074
{
1075
  GtkPaned *paned = GTK_PANED (widget);
1076
  GtkPanedPrivate *priv = paned->priv;
1077
  gint child_min, child_nat;
1078

1079
  *minimum = *natural = 0;
1080

1081
  if (priv->child1 && gtk_widget_get_visible (priv->child1))
1082
    {
1083 1084 1085
      _gtk_widget_get_preferred_size_for_size (priv->child1, priv->orientation, size, &child_min, &child_nat, NULL, NULL);
      if (priv->child1_shrink)
        *minimum = 0;
1086
      else
1087 1088
        *minimum = child_min;
      *natural = child_nat;
1089
    }
1090 1091

  if (priv->child2 && gtk_widget_get_visible (priv->child2))
1092
    {
1093 1094 1095 1096 1097
      _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;
1098 1099
    }

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
  if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
      priv->child2 && gtk_widget_get_visible (priv->child2))
    {
      gint handle_size;

      gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);

      *minimum += handle_size;
      *natural += handle_size;
    }
1110 1111
}

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

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
  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;

      gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);

      _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,
1136
                                  size - handle_size, child1_req, child2_req,
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
                                  NULL, NULL, &for_child1);

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

1147
  *minimum = *natural = 0;
1148

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

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

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

1174 1175 1176 1177 1178 1179 1180 1181 1182
static void
gtk_paned_get_preferred_size (GtkWidget      *widget,
                              GtkOrientation  orientation,
                              gint            size,
                              gint           *minimum,
                              gint           *natural)
{
  GtkPaned *paned = GTK_PANED (widget);
  GtkPanedPrivate *priv = paned->priv;
1183

1184
  if (orientation == priv->orientation)
1185
    gtk_paned_get_preferred_size_for_orientation (widget, size, minimum, natural);
1186
  else
1187
    gtk_paned_get_preferred_size_for_opposite_orientation (widget, size, minimum, natural);
1188 1189
}

1190 1191 1192 1193 1194
static void
gtk_paned_get_preferred_width (GtkWidget *widget,
                               gint      *minimum,
                               gint      *natural)
{
Benjamin Otte's avatar
Benjamin Otte committed
1195
  gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, -1, minimum, natural);
1196 1197 1198 1199 1200 1201 1202
}

static void
gtk_paned_get_preferred_height (GtkWidget *widget,
                                gint      *minimum,
                                gint      *natural)
{
Benjamin Otte's avatar
Benjamin Otte committed
1203
  gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, -1, minimum, natural);
1204 1205
}

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
static void
gtk_paned_get_preferred_width_for_height (GtkWidget *widget,
                                          gint       height,
                                          gint      *minimum,
                                          gint      *natural)
{
  gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
}

static void
gtk_paned_get_preferred_height_for_width (GtkWidget *widget,
                                          gint       width,
                                          gint      *minimum,
                                          gint      *natural)
{
  gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
}

1224 1225 1226 1227
static void
flip_child (GtkWidget     *widget,
            GtkAllocation *child_pos)
{
1228 1229 1230 1231 1232 1233
  GtkAllocation allocation;
  gint x, width;

  gtk_widget_get_allocation (widget, &allocation);
  x = allocation.x;
  width = allocation.width;
1234 1235 1236 1237

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

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
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;

1257 1258 1259 1260 1261 1262 1263
      if (visible != gdk_window_is_visible (window))
        {
          if (visible)
            gdk_window_show (window);
          else
            gdk_window_hide (window);
        }
1264 1265 1266
    }
}

1267 1268 1269
static void
gtk_paned_child_allocate (GtkWidget           *child,
                          GdkWindow           *child_window, /* can be NULL */
1270 1271
                          const GtkAllocation *window_allocation,
                          GtkAllocation       *child_allocation)
1272 1273 1274
{
  if (child_window)
    gdk_window_move_resize (child_window,
1275 1276
                            window_allocation->x, window_allocation->y,
                            window_allocation->width, window_allocation->height);
1277

1278
  gtk_widget_size_allocate (child, child_allocation);
1279
}
1280

1281 1282 1283 1284 1285
static void
gtk_paned_size_allocate (GtkWidget     *widget,
                         GtkAllocation *allocation)
{
  GtkPaned *paned = GTK_PANED (widget);
1286
  GtkPanedPrivate *priv = paned->priv;
1287

1288
  gtk_widget_set_allocation (widget, allocation);
1289

1290 1291
  if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
      priv->child2 && gtk_widget_get_visible (priv->child2))
1292
    {
1293 1294
      GtkAllocation child1_allocation, window1_allocation;
      GtkAllocation child2_allocation, window2_allocation;
1295
      GtkAllocation priv_child1_allocation;
1296 1297 1298 1299 1300
      GdkRectangle old_handle_pos;
      gint handle_size;

      gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);

1301
      old_handle_pos = priv->handle_pos;
1302

1303
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1304
        {
1305 1306 1307 1308 1309 1310 1311 1312 1313
          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);

1314
          gtk_paned_calc_position (paned,
1315
                                   MAX (1, allocation->width - handle_size),
1316 1317
                                   child1_width,
                                   child2_width);
1318

1319 1320
          priv->handle_pos.x = allocation->x + priv->child1_size;
          priv->handle_pos.y = allocation->y;
1321
          priv->handle_pos.width = handle_size;
1322
          priv->handle_pos.height = allocation->height;
1323

1324 1325 1326 1327
          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;
1328

1329 1330
          window2_allocation.x = window1_allocation.x + priv->child1_size + priv->handle_pos.width;
          window2_allocation.width = MAX (1, allocation->x + allocation->width - window2_allocation.x);
1331 1332 1333

          if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
            {
1334 1335
              flip_child (widget, &(window2_allocation));
              flip_child (widget, &(window1_allocation));
1336
              flip_child (widget, &(priv->handle_pos));
1337
            }
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357

          child1_allocation.x = child1_allocation.y = 0;
          child1_allocation.width = window1_allocation.width;
          child1_allocation.height = window1_allocation.height;
          if (child1_width > child1_allocation.width)
            {
              if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_LTR)
                child1_allocation.x -= child1_width - child1_allocation.width;
              child1_allocation.width = child1_width;
            }

          child2_allocation.x = child2_allocation.y = 0;
          child2_allocation.width = window2_allocation.width;
          child2_allocation.height = window2_allocation.height;
          if (child2_width > child2_allocation.width)
            {
              if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
                child2_allocation.x -= child2_width - child2_allocation.width;
              child2_allocation.width = child2_width;
            }
1358 1359 1360
        }
      else
        {
1361 1362 1363 1364 1365 1366 1367 1368 1369
          gint child1_height, child2_height;

          gtk_widget_get_preferred_height_for_width (priv->child1,
                                                     allocation->width,
                                                     &child1_height, NULL);
          gtk_widget_get_preferred_height_for_width (priv->child2,
                                                     allocation->width,
                                                     &child2_height, NULL);

1370
          gtk_paned_calc_position (paned,
1371
                                   MAX (1, allocation->height - handle_size),
1372 1373
                                   child1_height,
                                   child2_height);
1374

1375 1376 1377
          priv->handle_pos.x = allocation->x;
          priv->handle_pos.y = allocation->y + priv->child1_size;
          priv->handle_pos.width = allocation->width;
1378
          priv->handle_pos.height = handle_size;
1379

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
          window1_allocation.width = window2_allocation.width = allocation->width;
          window1_allocation.height = MAX (1, priv->child1_size);
          window1_allocation.x = window2_allocation.x = allocation->x;
          window1_allocation.y = allocation->y;

          window2_allocation.y = window1_allocation.y + priv->child1_size + priv->handle_pos.height;
          window2_allocation.height = MAX (1, allocation->y + allocation->height - window2_allocation.y);

          child1_allocation.x = child1_allocation.y = 0;
          child1_allocation.width = window1_allocation.width;
          child1_allocation.height = window1_allocation.height;
          if (child1_height > child1_allocation.height)
            {
              child1_allocation.y -= child1_height - child1_allocation.height;
              child1_allocation.height = child1_height;
            }
1396

1397 1398 1399 1400 1401
          child2_allocation.x = child2_allocation.y = 0;
          child2_allocation.width = window2_allocation.width;
          child2_allocation.height = window2_allocation.height;
          if (child2_height > child2_allocation.height)
            child2_allocation.height = child2_height;
1402 1403
        }

1404
      if (gtk_widget_get_mapped (widget) &&
1405 1406 1407 1408
          (old_handle_pos.x != priv->handle_pos.x ||
           old_handle_pos.y != priv->handle_pos.y ||
           old_handle_pos.width != priv->handle_pos.width ||
           old_handle_pos.height != priv->handle_pos.height))
1409
        {
1410 1411 1412 1413 1414
          GdkWindow *window;

          window = gtk_widget_get_window (widget);
          gdk_window_invalidate_rect (window, &old_handle_pos, FALSE);
          gdk_window_invalidate_rect (window, &priv->handle_pos, FALSE);
1415 1416
        }

1417
      if (gtk_widget_get_realized (widget))
Michael Natterer's avatar