gtkpaned.c 86.3 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 141
  PROP_POSITION_SET,
  PROP_MIN_POSITION,
  PROP_MAX_POSITION
142
};
Elliot Lee's avatar
Elliot Lee committed
143

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

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

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

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

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

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

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

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

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

258

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

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

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

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

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

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

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

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

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

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

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

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

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

  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));
391

392 393 394 395 396 397 398 399
  /**
   * GtkPaned:resize:
   *
   * The "resize" child property determines whether the child expands and
   * shrinks along with the paned widget.
   *
   * Since: 2.4
   */
400 401 402
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_RESIZE,
					      g_param_spec_boolean ("resize", 
403 404
								    P_("Resize"),
								    P_("If TRUE, the child expands and shrinks along with the paned widget"),
405
								    TRUE,
406
								    GTK_PARAM_READWRITE));
407

408 409 410 411 412 413 414 415
  /**
   * GtkPaned:shrink:
   *
   * The "shrink" child property determines whether the child can be made
   * smaller than its requisition.
   *
   * Since: 2.4
   */
416 417 418
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_SHRINK,
					      g_param_spec_boolean ("shrink", 
419 420
								    P_("Shrink"),
								    P_("If TRUE, the child can be made smaller than its requisition"),
421
								    TRUE,
422
								    GTK_PARAM_READWRITE));
423

424 425 426 427 428 429
  /**
   * GtkPaned::cycle-child-focus:
   * @widget: the object that received the signal
   * @reversed: whether cycling backward or forward
   *
   * The ::cycle-child-focus signal is a 
430
   * [keybinding signal][GtkBindingSignal]
431 432 433 434 435 436
   * which gets emitted to cycle the focus between the children of the paned.
   *
   * The default binding is f6.
   *
   * Since: 2.0
   */
437
  signals [CYCLE_CHILD_FOCUS] =
438
    g_signal_new (I_("cycle-child-focus"),
439 440 441 442 443 444 445 446
		  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);

447 448 449 450 451
  /**
   * GtkPaned::toggle-handle-focus:
   * @widget: the object that received the signal
   *
   * The ::toggle-handle-focus is a 
452
   * [keybinding signal][GtkBindingSignal]
453 454 455 456 457 458 459
   * 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
   */
460
  signals [TOGGLE_HANDLE_FOCUS] =
461
    g_signal_new (I_("toggle-handle-focus"),
462 463 464 465 466 467 468
		  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);

469 470 471 472 473 474
  /**
   * GtkPaned::move-handle:
   * @widget: the object that received the signal
   * @scroll_type: a #GtkScrollType
   *
   * The ::move-handle signal is a 
475
   * [keybinding signal][GtkBindingSignal]
476 477 478 479 480
   * which gets emitted to move the handle when the user is using key bindings 
   * to move it.
   *
   * Since: 2.0
   */
481
  signals[MOVE_HANDLE] =
482
    g_signal_new (I_("move-handle"),
483 484 485 486 487 488 489 490
		  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);

491 492 493 494 495 496
  /**
   * GtkPaned::cycle-handle-focus:
   * @widget: the object that received the signal
   * @reversed: whether cycling backward or forward
   *
   * The ::cycle-handle-focus signal is a 
497
   * [keybinding signal][GtkBindingSignal]
498 499 500 501 502 503 504
   * 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
   */
505
  signals [CYCLE_HANDLE_FOCUS] =
506
    g_signal_new (I_("cycle-handle-focus"),
507 508 509 510 511 512 513 514
		  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);

515 516 517 518 519
  /**
   * GtkPaned::accept-position:
   * @widget: the object that received the signal
   *
   * The ::accept-position signal is a 
520
   * [keybinding signal][GtkBindingSignal]
521 522 523 524 525 526 527
   * 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
   */
528
  signals [ACCEPT_POSITION] =
529
    g_signal_new (I_("accept-position"),
530 531 532 533 534 535 536
		  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);

537 538 539 540 541
  /**
   * GtkPaned::cancel-position:
   * @widget: the object that received the signal
   *
   * The ::cancel-position signal is a 
542
   * [keybinding signal][GtkBindingSignal]
543 544 545 546 547 548 549 550
   * 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
   */
551
  signals [CANCEL_POSITION] =
552
    g_signal_new (I_("cancel-position"),
553 554 555 556 557 558 559
		  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
560
  binding_set = gtk_binding_set_by_class (class);
561 562

  /* F6 and friends */
563
  gtk_binding_entry_add_signal (binding_set,
564
                                GDK_KEY_F6, 0,
565
                                "cycle-child-focus", 1, 
566 567
                                G_TYPE_BOOLEAN, FALSE);
  gtk_binding_entry_add_signal (binding_set,
568
				GDK_KEY_F6, GDK_SHIFT_MASK,
569
				"cycle-child-focus", 1,
570 571 572 573
				G_TYPE_BOOLEAN, TRUE);

  /* F8 and friends */
  gtk_binding_entry_add_signal (binding_set,
574
				GDK_KEY_F8, 0,
575
				"cycle-handle-focus", 1,
576 577 578
				G_TYPE_BOOLEAN, FALSE);
 
  gtk_binding_entry_add_signal (binding_set,
579
				GDK_KEY_F8, GDK_SHIFT_MASK,
580
				"cycle-handle-focus", 1,
581
				G_TYPE_BOOLEAN, TRUE);
582
 
583 584 585 586
  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);
587 588 589

  /* accept and cancel positions */
  gtk_binding_entry_add_signal (binding_set,
590
				GDK_KEY_Escape, 0,
591
				"cancel-position", 0);
592 593

  gtk_binding_entry_add_signal (binding_set,
594
				GDK_KEY_Return, 0,
595
				"accept-position", 0);
596
  gtk_binding_entry_add_signal (binding_set,
597
				GDK_KEY_ISO_Enter, 0,
598
				"accept-position", 0);
599
  gtk_binding_entry_add_signal (binding_set,
600
				GDK_KEY_KP_Enter, 0,
601
				"accept-position", 0);
602
  gtk_binding_entry_add_signal (binding_set,
603
				GDK_KEY_space, 0,
604
				"accept-position", 0);
605
  gtk_binding_entry_add_signal (binding_set,
606
				GDK_KEY_KP_Space, 0,
607
				"accept-position", 0);
608 609

  /* move handle */
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
  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);
638

639
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PANED_ACCESSIBLE);
640 641
}

Manish Singh's avatar
Manish Singh committed
642
static GType
643 644
gtk_paned_child_type (GtkContainer *container)
{
645 646 647 648
  GtkPaned *paned = GTK_PANED (container);
  GtkPanedPrivate *priv = paned->priv;

  if (!priv->child1 || !priv->child2)
649 650
    return GTK_TYPE_WIDGET;
  else
Manish Singh's avatar
Manish Singh committed
651
    return G_TYPE_NONE;
Elliot Lee's avatar
Elliot Lee committed
652 653
}

654 655 656 657 658 659 660 661 662 663 664 665 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 694 695 696
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;
697 698
  GdkDevice *device;
  gboolean is_touch;
699 700 701

  sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
  event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
702
  device = gdk_event_get_source_device (event);
703 704 705
  gtk_widget_get_allocation (GTK_WIDGET (paned), &allocation);
  paned->priv->panning = FALSE;

706 707 708
  is_touch = (event->type == GDK_TOUCH_BEGIN ||
              gdk_device_get_source (device) == GDK_SOURCE_TOUCHSCREEN);

709
  if (event->any.window == priv->handle ||
710
      (is_touch && initiates_touch_drag (paned, start_x, start_y)))
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
    {
      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
754 755 756
static void
gtk_paned_init (GtkPaned *paned)
{
757
  GtkPanedPrivate *priv;
758
  GtkGesture *gesture;
759

760
  gtk_widget_set_has_window (GTK_WIDGET (paned), FALSE);
761
  gtk_widget_set_can_focus (GTK_WIDGET (paned), TRUE);
762 763

  /* We only need to redraw when the handle position moves, which is
764 765
   * independent of the overall allocation of the GtkPaned
   */
766 767
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (paned), FALSE);

768
  paned->priv = gtk_paned_get_instance_private (paned);
769
  priv = paned->priv;
770

771 772
  priv->orientation = GTK_ORIENTATION_HORIZONTAL;
  priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
773

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
  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;
791

792 793
  _gtk_orientable_set_style_classes (GTK_ORIENTABLE (paned));

794
  gesture = gtk_gesture_pan_new (GTK_WIDGET (paned),
795
                                 GTK_ORIENTATION_HORIZONTAL);
796 797 798 799 800 801 802
  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);
803 804
  gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
                                              GTK_PHASE_CAPTURE);
805
  priv->pan_gesture = gesture;
Elliot Lee's avatar
Elliot Lee committed
806 807
}

808
static void
809 810 811 812
gtk_paned_set_property (GObject        *object,
			guint           prop_id,
			const GValue   *value,
			GParamSpec     *pspec)
813
{
814
  GtkPaned *paned = GTK_PANED (object);
815
  GtkPanedPrivate *priv = paned->priv;
816

817
  switch (prop_id)
818
    {
819
    case PROP_ORIENTATION:
820
      if (priv->orientation != g_value_get_enum (value))
821
        {
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
          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);
842
        }
843
      break;
844 845 846 847
    case PROP_POSITION:
      gtk_paned_set_position (paned, g_value_get_int (value));
      break;
    case PROP_POSITION_SET:
848 849 850 851 852 853
      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);
        }
854
      break;
855
    default:
856
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
857 858 859 860 861
      break;
    }
}

static void
862 863 864 865
gtk_paned_get_property (GObject        *object,
			guint           prop_id,
			GValue         *value,
			GParamSpec     *pspec)
866
{
867
  GtkPaned *paned = GTK_PANED (object);
868
  GtkPanedPrivate *priv = paned->priv;
869

870
  switch (prop_id)
871
    {
872
    case PROP_ORIENTATION:
873
      g_value_set_enum (value, priv->orientation);
874
      break;
875
    case PROP_POSITION:
876
      g_value_set_int (value, priv->child1_size);
877 878
      break;
    case PROP_POSITION_SET:
879
      g_value_set_boolean (value, priv->position_set);
880
      break;
881
    case PROP_MIN_POSITION:
882
      g_value_set_int (value, priv->min_position);
883 884
      break;
    case PROP_MAX_POSITION:
885
      g_value_set_int (value, priv->max_position);
886
      break;
887
    default:
888
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
889 890 891
      break;
    }
}
Elliot Lee's avatar
Elliot Lee committed
892

893 894 895 896 897 898 899 900
static void
gtk_paned_set_child_property (GtkContainer    *container,
			      GtkWidget       *child,
			      guint            property_id,
			      const GValue    *value,
			      GParamSpec      *pspec)
{
  GtkPaned *paned = GTK_PANED (container);
901
  GtkPanedPrivate *priv = paned->priv;
902 903
  gboolean old_value, new_value;

904
  g_assert (child == priv->child1 || child == priv->child2);
905 906 907 908 909

  new_value = g_value_get_boolean (value);
  switch (property_id)
    {
    case CHILD_PROP_RESIZE:
910
      if (child == priv->child1)
911
	{
912 913
	  old_value = priv->child1_resize;
	  priv->child1_resize = new_value;
914 915 916
	}
      else
	{
917 918
	  old_value = priv->child2_resize;
	  priv->child2_resize = new_value;
919 920 921
	}
      break;
    case CHILD_PROP_SHRINK:
922
      if (child == priv->child1)
923
	{
924 925
	  old_value = priv->child1_shrink;
	  priv->child1_shrink = new_value;
926 927 928
	}
      else
	{
929 930
	  old_value = priv->child2_shrink;
	  priv->child2_shrink = new_value;
931 932 933 934
	}
      break;
    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
935
      old_value = -1; /* quiet gcc */
936 937 938
      break;
    }
  if (old_value != new_value)
939
    gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
940 941 942 943 944 945 946 947 948 949
}

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

952
  g_assert (child == priv->child1 || child == priv->child2);
953 954 955 956
  
  switch (property_id)
    {
    case CHILD_PROP_RESIZE:
957 958
      if (child == priv->child1)
	g_value_set_boolean (value, priv->child1_resize);
959
      else
960
	g_value_set_boolean (value, priv->child2_resize);
961 962
      break;
    case CHILD_PROP_SHRINK:
963 964
      if (child == priv->child1)
	g_value_set_boolean (value, priv->child1_shrink);
965
      else
966
	g_value_set_boolean (value, priv->child2_shrink);
967 968 969 970 971 972 973
      break;
    default:
      GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
      break;
    }
}

974 975 976 977 978 979 980 981
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);

982 983
  g_clear_object (&paned->priv->pan_gesture);

Matthias Clasen's avatar
Matthias Clasen committed
984
  G_OBJECT_CLASS (gtk_paned_parent_class)->finalize (object);
985 986
}

987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 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
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;
1028 1029
          else
            pos = priv->child1_size;
1030 1031
	}
      else
1032
        pos = priv->child1_size;
1033 1034 1035 1036 1037
    }

  pos = CLAMP (pos, min, max);
  
  if (min_pos)
1038
    *min_pos = min;
1039
  if (max_pos)
1040
    *max_pos = max;
1041 1042 1043 1044
  if (out_pos)
    *out_pos = pos;
}

1045
static void
1046 1047 1048 1049
gtk_paned_get_preferred_size_for_orientation (GtkWidget      *widget,
                                              gint            size,
                                              gint           *minimum,
                                              gint           *natural)
1050
{
1051
  GtkPaned *paned = GTK_PANED (widget);
1052
  GtkPanedPrivate *priv = paned->priv;
1053
  gint child_min, child_nat;
1054

1055
  *minimum = *natural = 0;
1056

1057
  if (priv->child1 && gtk_widget_get_visible (priv->child1))
1058
    {
1059 1060 1061
      _gtk_widget_get_preferred_size_for_size (priv->child1, priv->orientation, size, &child_min, &child_nat, NULL, NULL);
      if (priv->child1_shrink)
        *minimum = 0;
1062
      else
1063 1064
        *minimum = child_min;
      *natural = child_nat;
1065
    }
1066 1067

  if (priv->child2 && gtk_widget_get_visible (priv->child2))
1068
    {
1069 1070 1071 1072 1073
      _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;
1074 1075
    }

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
  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;
    }
1086 1087
}

1088
static void
1089 1090 1091 1092
gtk_paned_get_preferred_size_for_opposite_orientation (GtkWidget      *widget,
                                                       gint            size,
                                                       gint           *minimum,
                                                       gint           *natural)
1093 1094
{
  GtkPaned *paned = GTK_PANED (widget);
1095
  GtkPanedPrivate *priv = paned->priv;
1096
  gint for_child1, for_child2;
1097
  gint child_min, child_nat;
1098

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
  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,
1112
                                  size - handle_size, child1_req, child2_req,
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
                                  NULL, NULL, &for_child1);

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

1123
  *minimum = *natural = 0;
1124

1125
  if (priv->child1 && gtk_widget_get_visible (priv->child1))
1126
    {
1127 1128
      _gtk_widget_get_preferred_size_for_size (priv->child1,
                                               OPPOSITE_ORIENTATION (priv->orientation),
1129
                                               for_child1,
1130 1131
                                               &child_min, &child_nat,
                                               NULL, NULL);
1132 1133
      
      *minimum = child_min;
1134
      *natural = child_nat;
1135 1136
    }

1137
  if (priv->child2 && gtk_widget_get_visible (priv->child2))
1138
    {
1139 1140
      _gtk_widget_get_preferred_size_for_size (priv->child2,
                                               OPPOSITE_ORIENTATION (priv->orientation),
1141
                                               for_child2,
1142 1143
                                               &child_min, &child_nat,
                                               NULL, NULL);
1144

1145 1146 1147 1148
      *minimum = MAX (*minimum, child_min);
      *natural = MAX (*natural, child_nat);
    }
}
1149

1150 1151 1152 1153 1154 1155 1156 1157 1158
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;
1159

1160 1161 1162 1163
  if (orientation == priv->orientation)
    return gtk_paned_get_preferred_size_for_orientation (widget, size, minimum, natural);
  else
    return gtk_paned_get_preferred_size_for_opposite_orientation (widget, size, minimum, natural);
1164 1165
}

1166 1167 1168 1169 1170
static void
gtk_paned_get_preferred_width (GtkWidget *widget,
                               gint      *minimum,
                               gint      *natural)
{
Benjamin Otte's avatar
Benjamin Otte committed
1171
  gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, -1, minimum, natural);
1172 1173 1174 1175 1176 1177 1178
}

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

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
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);
}

1200 1201 1202 1203
static void
flip_child (GtkWidget     *widget,
            GtkAllocation *child_pos)
{
1204 1205 1206 1207 1208 1209
  GtkAllocation allocation;
  gint x, width;

  gtk_widget_get_allocation (widget, &allocation);
  x = allocation.x;
  width = allocation.width;
1210 1211 1212 1213

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

1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
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;

1233 1234 1235 1236 1237 1238 1239
      if (visible != gdk_window_is_visible (window))
        {
          if (visible)
            gdk_window_show (window);
          else
            gdk_window_hide (window);
        }
1240 1241 1242
    }
}

1243 1244 1245
static void
gtk_paned_child_allocate (GtkWidget           *child,
                          GdkWindow           *child_window, /* can be NULL */
1246 1247
                          const GtkAllocation *window_allocation,
                          GtkAllocation       *child_allocation)
1248 1249 1250
{
  if (child_window)
    gdk_window_move_resize (child_window,
1251 1252
                            window_allocation->x, window_allocation->y,
                            window_allocation->width, window_allocation->height);
1253

1254
  gtk_widget_size_allocate (child, child_allocation);
1255
}
1256

1257 1258 1259 1260 1261
static void
gtk_paned_size_allocate (GtkWidget     *widget,
                         GtkAllocation *allocation)
{
  GtkPaned *paned = GTK_PANED (widget);
1262
  GtkPanedPrivate *priv = paned->priv;
1263

1264
  gtk_widget_set_allocation (widget, allocation);
1265

1266 1267
  if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
      priv->child2 && gtk_widget_get_visible (priv->child2))
1268
    {
1269 1270
      GtkAllocation child1_allocation, window1_allocation;
      GtkAllocation child2_allocation, window2_allocation;
1271
      GtkAllocation priv_child1_allocation;
1272 1273 1274 1275 1276
      GdkRectangle old_handle_pos;
      gint handle_size;

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

1277
      old_handle_pos = priv->handle_pos;
1278

1279
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1280
        {
1281 1282 1283 1284 1285 1286 1287 1288 1289
          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);

1290
          gtk_paned_calc_position (paned,
1291
                                   MAX (1, allocation->width - handle_size),
1292 1293
                                   child1_width,
                                   child2_width);
1294

1295 1296
          priv->handle_pos.x = allocation->x + priv->child1_size;
          priv->handle_pos.y = allocation->y;
1297
          priv->handle_pos.width = handle_size;
1298
          priv->handle_pos.height = allocation->height;
1299

1300 1301 1302 1303
          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;
1304

1305 1306
          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);
1307 1308 1309

          if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
            {
1310 1311
              flip_child (widget, &(window2_allocation));
              flip_child (widget, &(window1_allocation));
1312
              flip_child (widget, &(priv->handle_pos));
1313
            }
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333

          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;
            }
1334 1335 1336
        }
      else
        {
1337 1338 1339 1340 1341 1342 1343 1344 1345
          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);

1346
          gtk_paned_calc_position (paned,
1347
                                   MAX (1, allocation->height - handle_size),
1348 1349
                                   child1_height,
                                   child2_height);
1350

1351 1352 1353
          priv->handle_pos.x = allocation->x;
          priv->handle_pos.y = allocation->y + priv->child1_size;
          priv->handle_pos.width = allocation->width;
1354
          priv->handle_pos.height = handle_size;
1355

1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
          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;
            }
1372

1373 1374 1375 1376 1377
          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;
1378 1379
        }

1380
      if (gtk_widget_get_mapped (widget) &&
1381 1382 1383 1384
          (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))
1385
        {
1386 1387 1388 1389 1390
          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);
1391 1392
        }

1393
      if (gtk_widget_get_realized (widget))
1394
	{
1395 1396 1397 1398 1399 1400
          GtkBorder margin;

          gtk_style_context_get_margin (gtk_widget_get_style_context (widget),
                                        gtk_widget_get_state_flags (widget),
                                        &margin);

1401
	  if (gtk_widget_get_mapped (widget))
1402
	    gdk_window_show (priv->handle);
1403

1404
          if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1405
            {
1406
              gdk_window_move_resize (priv->handle,
1407
                                      priv->handle_pos.x - margin.left,
1408
                                      priv->handle_pos.y,
1409
                                      handle_size + margin.left + margin.right,
1410
                                      priv->handle_pos.height);
1411 1412 1413
            }
          else
            {
1414