]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
paned: Properly hide panes that were set to invisible
[~andy/gtk] / gtk / gtkpaned.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #include "config.h"
28
29 #include "gtkpaned.h"
30
31 #include "gtkbindings.h"
32 #include "gtkmain.h"
33 #include "gtkmarshalers.h"
34 #include "gtkorientable.h"
35 #include "gtkwindow.h"
36 #include "gtktypebuiltins.h"
37 #include "gtkprivate.h"
38 #include "gtkintl.h"
39
40
41 /**
42  * SECTION:gtkpaned
43  * @Short_description: Base class for widgets with two adjustable panes
44  * @Title: GtkPaned
45  *
46  * #GtkPaned is the base class for widgets with two panes, arranged either
47  * horizontally (#GtkHPaned) or vertically (#GtkVPaned). Child widgets are
48  * added to the panes of the widget with gtk_paned_pack1() and
49  * gtk_paned_pack2(). The division between the two children is set by default
50  * from the size requests of the children, but it can be adjusted by the
51  * user.
52  *
53  * A paned widget draws a separator between the two child widgets and a
54  * small handle that the user can drag to adjust the division. It does not
55  * draw any relief around the children or around the separator. (The space
56  * in which the separator is called the gutter.) Often, it is useful to put
57  * each child inside a #GtkFrame with the shadow type set to %GTK_SHADOW_IN
58  * so that the gutter appears as a ridge. No separator is drawn if one of
59  * the children is missing.
60  *
61  * Each child has two options that can be set, @resize and @shrink. If
62  * @resize is true, then when the #GtkPaned is resized, that child will
63  * expand or shrink along with the paned widget. If @shrink is true, then
64  * that child can be made smaller than its requisition by the user.
65  * Setting @shrink to %FALSE allows the application to set a minimum size.
66  * If @resize is false for both children, then this is treated as if
67  * @resize is true for both children.
68  *
69  * The application can set the position of the slider as if it were set
70  * by the user, by calling gtk_paned_set_position().
71  *
72  * <example>
73  * <title>Creating a paned widget with minimum sizes.</title>
74  * <programlisting>
75  * GtkWidget *hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
76  * GtkWidget *frame1 = gtk_frame_new (NULL);
77  * GtkWidget *frame2 = gtk_frame_new (NULL);
78  * gtk_frame_set_shadow_type (GTK_FRAME (frame1), GTK_SHADOW_IN);
79  * gtk_frame_set_shadow_type (GTK_FRAME (frame2), GTK_SHADOW_IN);
80  *
81  * gtk_widget_set_size_request (hpaned, 200, -1);
82  *
83  * gtk_paned_pack1 (GTK_PANED (hpaned), frame1, TRUE, FALSE);
84  * gtk_widget_set_size_request (frame1, 50, -1);
85  *
86  * gtk_paned_pack2 (GTK_PANED (hpaned), frame2, FALSE, FALSE);
87  * gtk_widget_set_size_request (frame2, 50, -1);
88  * </programlisting>
89  * </example>
90  */
91
92 enum {
93   CHILD1,
94   CHILD2
95 };
96
97 struct _GtkPanedPrivate
98 {
99   GtkPaned       *first_paned;
100   GtkWidget      *child1;
101   GtkWidget      *child2;
102   GdkWindow      *child1_window;
103   GdkWindow      *child2_window;
104   GtkWidget      *last_child1_focus;
105   GtkWidget      *last_child2_focus;
106   GtkWidget      *saved_focus;
107   GtkOrientation  orientation;
108
109   GdkCursorType  cursor_type;
110   GdkDevice     *grab_device;
111   GdkRectangle   handle_pos;
112   GdkWindow     *handle;
113
114   gint          child1_size;
115   gint          drag_pos;
116   gint          last_allocation;
117   gint          max_position;
118   gint          min_position;
119   gint          original_position;
120
121   guint32       grab_time;
122
123   guint         handle_prelit : 1;
124   guint         in_drag       : 1;
125   guint         in_recursion  : 1;
126   guint         child1_resize : 1;
127   guint         child1_shrink : 1;
128   guint         child2_resize : 1;
129   guint         child2_shrink : 1;
130   guint         position_set  : 1;
131 };
132
133 enum {
134   PROP_0,
135   PROP_ORIENTATION,
136   PROP_POSITION,
137   PROP_POSITION_SET,
138   PROP_MIN_POSITION,
139   PROP_MAX_POSITION
140 };
141
142 enum {
143   CHILD_PROP_0,
144   CHILD_PROP_RESIZE,
145   CHILD_PROP_SHRINK
146 };
147
148 enum {
149   CYCLE_CHILD_FOCUS,
150   TOGGLE_HANDLE_FOCUS,
151   MOVE_HANDLE,
152   CYCLE_HANDLE_FOCUS,
153   ACCEPT_POSITION,
154   CANCEL_POSITION,
155   LAST_SIGNAL
156 };
157
158 static void     gtk_paned_set_property          (GObject          *object,
159                                                  guint             prop_id,
160                                                  const GValue     *value,
161                                                  GParamSpec       *pspec);
162 static void     gtk_paned_get_property          (GObject          *object,
163                                                  guint             prop_id,
164                                                  GValue           *value,
165                                                  GParamSpec       *pspec);
166 static void     gtk_paned_set_child_property    (GtkContainer     *container,
167                                                  GtkWidget        *child,
168                                                  guint             property_id,
169                                                  const GValue     *value,
170                                                  GParamSpec       *pspec);
171 static void     gtk_paned_get_child_property    (GtkContainer     *container,
172                                                  GtkWidget        *child,
173                                                  guint             property_id,
174                                                  GValue           *value,
175                                                  GParamSpec       *pspec);
176 static void     gtk_paned_finalize              (GObject          *object);
177
178 static void     gtk_paned_get_preferred_width   (GtkWidget        *widget,
179                                                  gint             *minimum,
180                                                  gint             *natural);
181 static void     gtk_paned_get_preferred_height  (GtkWidget        *widget,
182                                                  gint             *minimum,
183                                                  gint             *natural);
184 static void     gtk_paned_get_preferred_width_for_height
185                                                 (GtkWidget        *widget,
186                                                  gint              height,
187                                                  gint             *minimum,
188                                                  gint             *natural);
189 static void     gtk_paned_get_preferred_height_for_width
190                                                 (GtkWidget        *widget,
191                                                  gint              width,
192                                                  gint             *minimum,
193                                                  gint              *natural);
194
195 static void     gtk_paned_size_allocate         (GtkWidget        *widget,
196                                                  GtkAllocation    *allocation);
197 static void     gtk_paned_realize               (GtkWidget        *widget);
198 static void     gtk_paned_unrealize             (GtkWidget        *widget);
199 static void     gtk_paned_map                   (GtkWidget        *widget);
200 static void     gtk_paned_unmap                 (GtkWidget        *widget);
201 static void     gtk_paned_state_flags_changed   (GtkWidget        *widget,
202                                                  GtkStateFlags     previous_state);
203 static gboolean gtk_paned_draw                  (GtkWidget        *widget,
204                                                  cairo_t          *cr);
205 static gboolean gtk_paned_enter                 (GtkWidget        *widget,
206                                                  GdkEventCrossing *event);
207 static gboolean gtk_paned_leave                 (GtkWidget        *widget,
208                                                  GdkEventCrossing *event);
209 static gboolean gtk_paned_button_press          (GtkWidget        *widget,
210                                                  GdkEventButton   *event);
211 static gboolean gtk_paned_button_release        (GtkWidget        *widget,
212                                                  GdkEventButton   *event);
213 static gboolean gtk_paned_motion                (GtkWidget        *widget,
214                                                  GdkEventMotion   *event);
215 static gboolean gtk_paned_focus                 (GtkWidget        *widget,
216                                                  GtkDirectionType  direction);
217 static gboolean gtk_paned_grab_broken           (GtkWidget          *widget,
218                                                  GdkEventGrabBroken *event);
219 static void     gtk_paned_add                   (GtkContainer     *container,
220                                                  GtkWidget        *widget);
221 static void     gtk_paned_remove                (GtkContainer     *container,
222                                                  GtkWidget        *widget);
223 static void     gtk_paned_forall                (GtkContainer     *container,
224                                                  gboolean          include_internals,
225                                                  GtkCallback       callback,
226                                                  gpointer          callback_data);
227 static void     gtk_paned_calc_position         (GtkPaned         *paned,
228                                                  gint              allocation,
229                                                  gint              child1_req,
230                                                  gint              child2_req);
231 static void     gtk_paned_set_focus_child       (GtkContainer     *container,
232                                                  GtkWidget        *child);
233 static void     gtk_paned_set_saved_focus       (GtkPaned         *paned,
234                                                  GtkWidget        *widget);
235 static void     gtk_paned_set_first_paned       (GtkPaned         *paned,
236                                                  GtkPaned         *first_paned);
237 static void     gtk_paned_set_last_child1_focus (GtkPaned         *paned,
238                                                  GtkWidget        *widget);
239 static void     gtk_paned_set_last_child2_focus (GtkPaned         *paned,
240                                                  GtkWidget        *widget);
241 static gboolean gtk_paned_cycle_child_focus     (GtkPaned         *paned,
242                                                  gboolean          reverse);
243 static gboolean gtk_paned_cycle_handle_focus    (GtkPaned         *paned,
244                                                  gboolean          reverse);
245 static gboolean gtk_paned_move_handle           (GtkPaned         *paned,
246                                                  GtkScrollType     scroll);
247 static gboolean gtk_paned_accept_position       (GtkPaned         *paned);
248 static gboolean gtk_paned_cancel_position       (GtkPaned         *paned);
249 static gboolean gtk_paned_toggle_handle_focus   (GtkPaned         *paned);
250
251 static GType    gtk_paned_child_type            (GtkContainer     *container);
252 static void     gtk_paned_grab_notify           (GtkWidget        *widget,
253                                                  gboolean          was_grabbed);
254
255
256 G_DEFINE_TYPE_WITH_CODE (GtkPaned, gtk_paned, GTK_TYPE_CONTAINER,
257                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
258                                                 NULL))
259
260 static guint signals[LAST_SIGNAL] = { 0 };
261
262
263 static void
264 add_tab_bindings (GtkBindingSet    *binding_set,
265                   GdkModifierType   modifiers)
266 {
267   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
268                                 "toggle-handle-focus", 0);
269   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
270                                 "toggle-handle-focus", 0);
271 }
272
273 static void
274 add_move_binding (GtkBindingSet   *binding_set,
275                   guint            keyval,
276                   GdkModifierType  mask,
277                   GtkScrollType    scroll)
278 {
279   gtk_binding_entry_add_signal (binding_set, keyval, mask,
280                                 "move-handle", 1,
281                                 GTK_TYPE_SCROLL_TYPE, scroll);
282 }
283
284 static void
285 gtk_paned_class_init (GtkPanedClass *class)
286 {
287   GObjectClass *object_class;
288   GtkWidgetClass *widget_class;
289   GtkContainerClass *container_class;
290   GtkPanedClass *paned_class;
291   GtkBindingSet *binding_set;
292
293   object_class = (GObjectClass *) class;
294   widget_class = (GtkWidgetClass *) class;
295   container_class = (GtkContainerClass *) class;
296   paned_class = (GtkPanedClass *) class;
297
298   object_class->set_property = gtk_paned_set_property;
299   object_class->get_property = gtk_paned_get_property;
300   object_class->finalize = gtk_paned_finalize;
301
302   widget_class->get_preferred_width = gtk_paned_get_preferred_width;
303   widget_class->get_preferred_height = gtk_paned_get_preferred_height;
304   widget_class->get_preferred_width_for_height = gtk_paned_get_preferred_width_for_height;
305   widget_class->get_preferred_height_for_width = gtk_paned_get_preferred_height_for_width;
306   widget_class->size_allocate = gtk_paned_size_allocate;
307   widget_class->realize = gtk_paned_realize;
308   widget_class->unrealize = gtk_paned_unrealize;
309   widget_class->map = gtk_paned_map;
310   widget_class->unmap = gtk_paned_unmap;
311   widget_class->draw = gtk_paned_draw;
312   widget_class->focus = gtk_paned_focus;
313   widget_class->enter_notify_event = gtk_paned_enter;
314   widget_class->leave_notify_event = gtk_paned_leave;
315   widget_class->button_press_event = gtk_paned_button_press;
316   widget_class->button_release_event = gtk_paned_button_release;
317   widget_class->motion_notify_event = gtk_paned_motion;
318   widget_class->grab_broken_event = gtk_paned_grab_broken;
319   widget_class->grab_notify = gtk_paned_grab_notify;
320   widget_class->state_flags_changed = gtk_paned_state_flags_changed;
321
322   container_class->add = gtk_paned_add;
323   container_class->remove = gtk_paned_remove;
324   container_class->forall = gtk_paned_forall;
325   container_class->child_type = gtk_paned_child_type;
326   container_class->set_focus_child = gtk_paned_set_focus_child;
327   container_class->set_child_property = gtk_paned_set_child_property;
328   container_class->get_child_property = gtk_paned_get_child_property;
329   gtk_container_class_handle_border_width (container_class);
330
331   paned_class->cycle_child_focus = gtk_paned_cycle_child_focus;
332   paned_class->toggle_handle_focus = gtk_paned_toggle_handle_focus;
333   paned_class->move_handle = gtk_paned_move_handle;
334   paned_class->cycle_handle_focus = gtk_paned_cycle_handle_focus;
335   paned_class->accept_position = gtk_paned_accept_position;
336   paned_class->cancel_position = gtk_paned_cancel_position;
337
338   g_object_class_override_property (object_class,
339                                     PROP_ORIENTATION,
340                                     "orientation");
341
342   g_object_class_install_property (object_class,
343                                    PROP_POSITION,
344                                    g_param_spec_int ("position",
345                                                      P_("Position"),
346                                                      P_("Position of paned separator in pixels (0 means all the way to the left/top)"),
347                                                      0,
348                                                      G_MAXINT,
349                                                      0,
350                                                      GTK_PARAM_READWRITE));
351
352   g_object_class_install_property (object_class,
353                                    PROP_POSITION_SET,
354                                    g_param_spec_boolean ("position-set",
355                                                          P_("Position Set"),
356                                                          P_("TRUE if the Position property should be used"),
357                                                          FALSE,
358                                                          GTK_PARAM_READWRITE));
359
360   gtk_widget_class_install_style_property (widget_class,
361                                            g_param_spec_int ("handle-size",
362                                                              P_("Handle Size"),
363                                                              P_("Width of handle"),
364                                                              0,
365                                                              G_MAXINT,
366                                                              5,
367                                                              GTK_PARAM_READABLE));
368   /**
369    * GtkPaned:min-position:
370    *
371    * The smallest possible value for the position property. This property is derived from the
372    * size and shrinkability of the widget's children.
373    *
374    * Since: 2.4
375    */
376   g_object_class_install_property (object_class,
377                                    PROP_MIN_POSITION,
378                                    g_param_spec_int ("min-position",
379                                                      P_("Minimal Position"),
380                                                      P_("Smallest possible value for the \"position\" property"),
381                                                      0,
382                                                      G_MAXINT,
383                                                      0,
384                                                      GTK_PARAM_READABLE));
385
386   /**
387    * GtkPaned:max-position:
388    *
389    * The largest possible value for the position property. This property is derived from the
390    * size and shrinkability of the widget's children.
391    *
392    * Since: 2.4
393    */
394   g_object_class_install_property (object_class,
395                                    PROP_MAX_POSITION,
396                                    g_param_spec_int ("max-position",
397                                                      P_("Maximal Position"),
398                                                      P_("Largest possible value for the \"position\" property"),
399                                                      0,
400                                                      G_MAXINT,
401                                                      G_MAXINT,
402                                                      GTK_PARAM_READABLE));
403
404   /**
405    * GtkPaned:resize:
406    *
407    * The "resize" child property determines whether the child expands and
408    * shrinks along with the paned widget.
409    *
410    * Since: 2.4
411    */
412   gtk_container_class_install_child_property (container_class,
413                                               CHILD_PROP_RESIZE,
414                                               g_param_spec_boolean ("resize", 
415                                                                     P_("Resize"),
416                                                                     P_("If TRUE, the child expands and shrinks along with the paned widget"),
417                                                                     TRUE,
418                                                                     GTK_PARAM_READWRITE));
419
420   /**
421    * GtkPaned:shrink:
422    *
423    * The "shrink" child property determines whether the child can be made
424    * smaller than its requisition.
425    *
426    * Since: 2.4
427    */
428   gtk_container_class_install_child_property (container_class,
429                                               CHILD_PROP_SHRINK,
430                                               g_param_spec_boolean ("shrink", 
431                                                                     P_("Shrink"),
432                                                                     P_("If TRUE, the child can be made smaller than its requisition"),
433                                                                     TRUE,
434                                                                     GTK_PARAM_READWRITE));
435
436   /**
437    * GtkPaned::cycle-child-focus:
438    * @widget: the object that received the signal
439    * @reversed: whether cycling backward or forward
440    *
441    * The ::cycle-child-focus signal is a 
442    * <link linkend="keybinding-signals">keybinding signal</link>
443    * which gets emitted to cycle the focus between the children of the paned.
444    *
445    * The default binding is f6.
446    *
447    * Since: 2.0
448    */
449   signals [CYCLE_CHILD_FOCUS] =
450     g_signal_new (I_("cycle-child-focus"),
451                   G_TYPE_FROM_CLASS (object_class),
452                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
453                   G_STRUCT_OFFSET (GtkPanedClass, cycle_child_focus),
454                   NULL, NULL,
455                   _gtk_marshal_BOOLEAN__BOOLEAN,
456                   G_TYPE_BOOLEAN, 1,
457                   G_TYPE_BOOLEAN);
458
459   /**
460    * GtkPaned::toggle-handle-focus:
461    * @widget: the object that received the signal
462    *
463    * The ::toggle-handle-focus is a 
464    * <link linkend="keybinding-signals">keybinding signal</link>
465    * which gets emitted to accept the current position of the handle and then 
466    * move focus to the next widget in the focus chain.
467    *
468    * The default binding is Tab.
469    *
470    * Since: 2.0
471    */
472   signals [TOGGLE_HANDLE_FOCUS] =
473     g_signal_new (I_("toggle-handle-focus"),
474                   G_TYPE_FROM_CLASS (object_class),
475                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
476                   G_STRUCT_OFFSET (GtkPanedClass, toggle_handle_focus),
477                   NULL, NULL,
478                   _gtk_marshal_BOOLEAN__VOID,
479                   G_TYPE_BOOLEAN, 0);
480
481   /**
482    * GtkPaned::move-handle:
483    * @widget: the object that received the signal
484    * @scroll_type: a #GtkScrollType
485    *
486    * The ::move-handle signal is a 
487    * <link linkend="keybinding-signals">keybinding signal</link>
488    * which gets emitted to move the handle when the user is using key bindings 
489    * to move it.
490    *
491    * Since: 2.0
492    */
493   signals[MOVE_HANDLE] =
494     g_signal_new (I_("move-handle"),
495                   G_TYPE_FROM_CLASS (object_class),
496                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
497                   G_STRUCT_OFFSET (GtkPanedClass, move_handle),
498                   NULL, NULL,
499                   _gtk_marshal_BOOLEAN__ENUM,
500                   G_TYPE_BOOLEAN, 1,
501                   GTK_TYPE_SCROLL_TYPE);
502
503   /**
504    * GtkPaned::cycle-handle-focus:
505    * @widget: the object that received the signal
506    * @reversed: whether cycling backward or forward
507    *
508    * The ::cycle-handle-focus signal is a 
509    * <link linkend="keybinding-signals">keybinding signal</link>
510    * which gets emitted to cycle whether the paned should grab focus to allow
511    * the user to change position of the handle by using key bindings.
512    *
513    * The default binding for this signal is f8.
514    *
515    * Since: 2.0
516    */
517   signals [CYCLE_HANDLE_FOCUS] =
518     g_signal_new (I_("cycle-handle-focus"),
519                   G_TYPE_FROM_CLASS (object_class),
520                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
521                   G_STRUCT_OFFSET (GtkPanedClass, cycle_handle_focus),
522                   NULL, NULL,
523                   _gtk_marshal_BOOLEAN__BOOLEAN,
524                   G_TYPE_BOOLEAN, 1,
525                   G_TYPE_BOOLEAN);
526
527   /**
528    * GtkPaned::accept-position:
529    * @widget: the object that received the signal
530    *
531    * The ::accept-position signal is a 
532    * <link linkend="keybinding-signals">keybinding signal</link>
533    * which gets emitted to accept the current position of the handle when 
534    * moving it using key bindings.
535    *
536    * The default binding for this signal is Return or Space.
537    *
538    * Since: 2.0
539    */
540   signals [ACCEPT_POSITION] =
541     g_signal_new (I_("accept-position"),
542                   G_TYPE_FROM_CLASS (object_class),
543                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
544                   G_STRUCT_OFFSET (GtkPanedClass, accept_position),
545                   NULL, NULL,
546                   _gtk_marshal_BOOLEAN__VOID,
547                   G_TYPE_BOOLEAN, 0);
548
549   /**
550    * GtkPaned::cancel-position:
551    * @widget: the object that received the signal
552    *
553    * The ::cancel-position signal is a 
554    * <link linkend="keybinding-signals">keybinding signal</link>
555    * which gets emitted to cancel moving the position of the handle using key 
556    * bindings. The position of the handle will be reset to the value prior to 
557    * moving it.
558    *
559    * The default binding for this signal is Escape.
560    *
561    * Since: 2.0
562    */
563   signals [CANCEL_POSITION] =
564     g_signal_new (I_("cancel-position"),
565                   G_TYPE_FROM_CLASS (object_class),
566                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
567                   G_STRUCT_OFFSET (GtkPanedClass, cancel_position),
568                   NULL, NULL,
569                   _gtk_marshal_BOOLEAN__VOID,
570                   G_TYPE_BOOLEAN, 0);
571
572   binding_set = gtk_binding_set_by_class (class);
573
574   /* F6 and friends */
575   gtk_binding_entry_add_signal (binding_set,
576                                 GDK_KEY_F6, 0,
577                                 "cycle-child-focus", 1, 
578                                 G_TYPE_BOOLEAN, FALSE);
579   gtk_binding_entry_add_signal (binding_set,
580                                 GDK_KEY_F6, GDK_SHIFT_MASK,
581                                 "cycle-child-focus", 1,
582                                 G_TYPE_BOOLEAN, TRUE);
583
584   /* F8 and friends */
585   gtk_binding_entry_add_signal (binding_set,
586                                 GDK_KEY_F8, 0,
587                                 "cycle-handle-focus", 1,
588                                 G_TYPE_BOOLEAN, FALSE);
589  
590   gtk_binding_entry_add_signal (binding_set,
591                                 GDK_KEY_F8, GDK_SHIFT_MASK,
592                                 "cycle-handle-focus", 1,
593                                 G_TYPE_BOOLEAN, TRUE);
594  
595   add_tab_bindings (binding_set, 0);
596   add_tab_bindings (binding_set, GDK_CONTROL_MASK);
597   add_tab_bindings (binding_set, GDK_SHIFT_MASK);
598   add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK);
599
600   /* accept and cancel positions */
601   gtk_binding_entry_add_signal (binding_set,
602                                 GDK_KEY_Escape, 0,
603                                 "cancel-position", 0);
604
605   gtk_binding_entry_add_signal (binding_set,
606                                 GDK_KEY_Return, 0,
607                                 "accept-position", 0);
608   gtk_binding_entry_add_signal (binding_set,
609                                 GDK_KEY_ISO_Enter, 0,
610                                 "accept-position", 0);
611   gtk_binding_entry_add_signal (binding_set,
612                                 GDK_KEY_KP_Enter, 0,
613                                 "accept-position", 0);
614   gtk_binding_entry_add_signal (binding_set,
615                                 GDK_KEY_space, 0,
616                                 "accept-position", 0);
617   gtk_binding_entry_add_signal (binding_set,
618                                 GDK_KEY_KP_Space, 0,
619                                 "accept-position", 0);
620
621   /* move handle */
622   add_move_binding (binding_set, GDK_KEY_Left, 0, GTK_SCROLL_STEP_LEFT);
623   add_move_binding (binding_set, GDK_KEY_KP_Left, 0, GTK_SCROLL_STEP_LEFT);
624   add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
625   add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
626
627   add_move_binding (binding_set, GDK_KEY_Right, 0, GTK_SCROLL_STEP_RIGHT);
628   add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
629   add_move_binding (binding_set, GDK_KEY_KP_Right, 0, GTK_SCROLL_STEP_RIGHT);
630   add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
631
632   add_move_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
633   add_move_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
634   add_move_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
635   add_move_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
636   add_move_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
637   add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0, GTK_SCROLL_PAGE_UP);
638
639   add_move_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
640   add_move_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
641   add_move_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
642   add_move_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
643   add_move_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
644   add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
645
646   add_move_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START);
647   add_move_binding (binding_set, GDK_KEY_KP_Home, 0, GTK_SCROLL_START);
648   add_move_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END);
649   add_move_binding (binding_set, GDK_KEY_KP_End, 0, GTK_SCROLL_END);
650
651   g_type_class_add_private (object_class, sizeof (GtkPanedPrivate));
652 }
653
654 static GType
655 gtk_paned_child_type (GtkContainer *container)
656 {
657   GtkPaned *paned = GTK_PANED (container);
658   GtkPanedPrivate *priv = paned->priv;
659
660   if (!priv->child1 || !priv->child2)
661     return GTK_TYPE_WIDGET;
662   else
663     return G_TYPE_NONE;
664 }
665
666 static void
667 gtk_paned_init (GtkPaned *paned)
668 {
669   GtkPanedPrivate *priv;
670
671   gtk_widget_set_has_window (GTK_WIDGET (paned), FALSE);
672   gtk_widget_set_can_focus (GTK_WIDGET (paned), TRUE);
673
674   /* We only need to redraw when the handle position moves, which is
675    * independent of the overall allocation of the GtkPaned
676    */
677   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (paned), FALSE);
678
679   paned->priv = G_TYPE_INSTANCE_GET_PRIVATE (paned, GTK_TYPE_PANED, GtkPanedPrivate);
680   priv = paned->priv;
681
682   priv->orientation = GTK_ORIENTATION_HORIZONTAL;
683   priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
684
685   priv->child1 = NULL;
686   priv->child2 = NULL;
687   priv->handle = NULL;
688   priv->cursor_type = GDK_CROSS;
689
690   priv->handle_pos.width = 5;
691   priv->handle_pos.height = 5;
692   priv->position_set = FALSE;
693   priv->last_allocation = -1;
694   priv->in_drag = FALSE;
695
696   priv->last_child1_focus = NULL;
697   priv->last_child2_focus = NULL;
698   priv->in_recursion = FALSE;
699   priv->handle_prelit = FALSE;
700   priv->original_position = -1;
701
702   priv->handle_pos.x = -1;
703   priv->handle_pos.y = -1;
704
705   priv->drag_pos = -1;
706 }
707
708 static void
709 gtk_paned_set_property (GObject        *object,
710                         guint           prop_id,
711                         const GValue   *value,
712                         GParamSpec     *pspec)
713 {
714   GtkPaned *paned = GTK_PANED (object);
715   GtkPanedPrivate *priv = paned->priv;
716
717   switch (prop_id)
718     {
719     case PROP_ORIENTATION:
720       priv->orientation = g_value_get_enum (value);
721
722       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
723         priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
724       else
725         priv->cursor_type = GDK_SB_V_DOUBLE_ARROW;
726
727       /* state_flags_changed updates the cursor */
728       gtk_paned_state_flags_changed (GTK_WIDGET (paned), 0);
729       gtk_widget_queue_resize (GTK_WIDGET (paned));
730       break;
731     case PROP_POSITION:
732       gtk_paned_set_position (paned, g_value_get_int (value));
733       break;
734     case PROP_POSITION_SET:
735       priv->position_set = g_value_get_boolean (value);
736       gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
737       break;
738     default:
739       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
740       break;
741     }
742 }
743
744 static void
745 gtk_paned_get_property (GObject        *object,
746                         guint           prop_id,
747                         GValue         *value,
748                         GParamSpec     *pspec)
749 {
750   GtkPaned *paned = GTK_PANED (object);
751   GtkPanedPrivate *priv = paned->priv;
752
753   switch (prop_id)
754     {
755     case PROP_ORIENTATION:
756       g_value_set_enum (value, priv->orientation);
757       break;
758     case PROP_POSITION:
759       g_value_set_int (value, priv->child1_size);
760       break;
761     case PROP_POSITION_SET:
762       g_value_set_boolean (value, priv->position_set);
763       break;
764     case PROP_MIN_POSITION:
765       g_value_set_int (value, priv->min_position);
766       break;
767     case PROP_MAX_POSITION:
768       g_value_set_int (value, priv->max_position);
769       break;
770     default:
771       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
772       break;
773     }
774 }
775
776 static void
777 gtk_paned_set_child_property (GtkContainer    *container,
778                               GtkWidget       *child,
779                               guint            property_id,
780                               const GValue    *value,
781                               GParamSpec      *pspec)
782 {
783   GtkPaned *paned = GTK_PANED (container);
784   GtkPanedPrivate *priv = paned->priv;
785   gboolean old_value, new_value;
786
787   g_assert (child == priv->child1 || child == priv->child2);
788
789   new_value = g_value_get_boolean (value);
790   switch (property_id)
791     {
792     case CHILD_PROP_RESIZE:
793       if (child == priv->child1)
794         {
795           old_value = priv->child1_resize;
796           priv->child1_resize = new_value;
797         }
798       else
799         {
800           old_value = priv->child2_resize;
801           priv->child2_resize = new_value;
802         }
803       break;
804     case CHILD_PROP_SHRINK:
805       if (child == priv->child1)
806         {
807           old_value = priv->child1_shrink;
808           priv->child1_shrink = new_value;
809         }
810       else
811         {
812           old_value = priv->child2_shrink;
813           priv->child2_shrink = new_value;
814         }
815       break;
816     default:
817       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
818       old_value = -1; /* quiet gcc */
819       break;
820     }
821   if (old_value != new_value)
822     gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
823 }
824
825 static void
826 gtk_paned_get_child_property (GtkContainer *container,
827                               GtkWidget    *child,
828                               guint         property_id,
829                               GValue       *value,
830                               GParamSpec   *pspec)
831 {
832   GtkPaned *paned = GTK_PANED (container);
833   GtkPanedPrivate *priv = paned->priv;
834
835   g_assert (child == priv->child1 || child == priv->child2);
836   
837   switch (property_id)
838     {
839     case CHILD_PROP_RESIZE:
840       if (child == priv->child1)
841         g_value_set_boolean (value, priv->child1_resize);
842       else
843         g_value_set_boolean (value, priv->child2_resize);
844       break;
845     case CHILD_PROP_SHRINK:
846       if (child == priv->child1)
847         g_value_set_boolean (value, priv->child1_shrink);
848       else
849         g_value_set_boolean (value, priv->child2_shrink);
850       break;
851     default:
852       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
853       break;
854     }
855 }
856
857 static void
858 gtk_paned_finalize (GObject *object)
859 {
860   GtkPaned *paned = GTK_PANED (object);
861   
862   gtk_paned_set_saved_focus (paned, NULL);
863   gtk_paned_set_first_paned (paned, NULL);
864
865   G_OBJECT_CLASS (gtk_paned_parent_class)->finalize (object);
866 }
867
868 static void
869 get_preferred_size_for_size (GtkWidget      *widget,
870                              GtkOrientation  orientation,
871                              gint            size,
872                              gint           *minimum,
873                              gint           *natural)
874 {
875   if (orientation == GTK_ORIENTATION_HORIZONTAL)
876     if (size < 0)
877       gtk_widget_get_preferred_width (widget, minimum, natural);
878     else
879       gtk_widget_get_preferred_width_for_height (widget, size, minimum, natural);
880   else
881     if (size < 0)
882       gtk_widget_get_preferred_height (widget, minimum, natural);
883     else
884       gtk_widget_get_preferred_height_for_width (widget, size, minimum, natural);
885 }
886
887 static void
888 gtk_paned_get_preferred_size (GtkWidget      *widget,
889                               GtkOrientation  orientation,
890                               gint            size,
891                               gint           *minimum,
892                               gint           *natural)
893 {
894   GtkPaned *paned = GTK_PANED (widget);
895   GtkPanedPrivate *priv = paned->priv;
896   gint child_min, child_nat;
897
898   *minimum = *natural = 0;
899
900   if (priv->child1 && gtk_widget_get_visible (priv->child1))
901     {
902       get_preferred_size_for_size (priv->child1, orientation, size, &child_min, &child_nat);
903       *minimum = child_min;
904       *natural = child_nat;
905     }
906
907   if (priv->child2 && gtk_widget_get_visible (priv->child2))
908     {
909       get_preferred_size_for_size (priv->child2, orientation, size, &child_min, &child_nat);
910
911       if (priv->orientation == orientation)
912         {
913           *minimum += child_min;
914           *natural += child_nat;
915         }
916       else
917         {
918           *minimum = MAX (*minimum, child_min);
919           *natural = MAX (*natural, child_nat);
920         }
921     }
922
923   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
924       priv->child2 && gtk_widget_get_visible (priv->child2))
925     {
926       gint handle_size;
927
928       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
929
930       if (priv->orientation == orientation)
931         {
932           *minimum += handle_size;
933           *natural += handle_size;
934         }
935     }
936 }
937
938 static void
939 gtk_paned_get_preferred_width (GtkWidget *widget,
940                                gint      *minimum,
941                                gint      *natural)
942 {
943   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, -1, minimum, natural);
944 }
945
946 static void
947 gtk_paned_get_preferred_height (GtkWidget *widget,
948                                 gint      *minimum,
949                                 gint      *natural)
950 {
951   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, -1, minimum, natural);
952 }
953
954 static void
955 gtk_paned_get_preferred_width_for_height (GtkWidget *widget,
956                                           gint       height,
957                                           gint      *minimum,
958                                           gint      *natural)
959 {
960   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
961 }
962
963 static void
964 gtk_paned_get_preferred_height_for_width (GtkWidget *widget,
965                                           gint       width,
966                                           gint      *minimum,
967                                           gint      *natural)
968 {
969   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
970 }
971
972 static void
973 flip_child (GtkWidget     *widget,
974             GtkAllocation *child_pos)
975 {
976   GtkAllocation allocation;
977   gint x, width;
978
979   gtk_widget_get_allocation (widget, &allocation);
980   x = allocation.x;
981   width = allocation.width;
982
983   child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
984 }
985
986 static void
987 gtk_paned_set_child_visible (GtkPaned  *paned,
988                              guint      id,
989                              gboolean   visible)
990 {
991   GtkPanedPrivate *priv = paned->priv;
992   GtkWidget *child;
993
994   child = id == CHILD1 ? priv->child1 : priv->child2;
995
996   if (child == NULL)
997     return;
998
999   gtk_widget_set_child_visible (child, visible);
1000
1001   if (gtk_widget_get_mapped (GTK_WIDGET (paned)))
1002     {
1003       GdkWindow *window = id == CHILD1 ? priv->child1_window : priv->child2_window;
1004
1005       if (visible != gdk_window_is_visible (window))
1006         {
1007           if (visible)
1008             gdk_window_show (window);
1009           else
1010             gdk_window_hide (window);
1011         }
1012     }
1013 }
1014
1015 static void
1016 gtk_paned_child_allocate (GtkWidget           *child,
1017                           GdkWindow           *child_window, /* can be NULL */
1018                           const GtkAllocation *window_allocation,
1019                           GtkAllocation       *child_allocation)
1020 {
1021   if (child_window)
1022     gdk_window_move_resize (child_window,
1023                             window_allocation->x, window_allocation->y,
1024                             window_allocation->width, window_allocation->height);
1025
1026   gtk_widget_size_allocate (child, child_allocation);
1027 }
1028
1029 static void
1030 gtk_paned_size_allocate (GtkWidget     *widget,
1031                          GtkAllocation *allocation)
1032 {
1033   GtkPaned *paned = GTK_PANED (widget);
1034   GtkPanedPrivate *priv = paned->priv;
1035
1036   gtk_widget_set_allocation (widget, allocation);
1037
1038   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
1039       priv->child2 && gtk_widget_get_visible (priv->child2))
1040     {
1041       GtkAllocation child1_allocation, window1_allocation;
1042       GtkAllocation child2_allocation, window2_allocation;
1043       GtkAllocation priv_child1_allocation;
1044       GdkRectangle old_handle_pos;
1045       gint handle_size;
1046
1047       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
1048
1049       old_handle_pos = priv->handle_pos;
1050
1051       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1052         {
1053           gint child1_width, child2_width;
1054
1055           gtk_widget_get_preferred_width_for_height (priv->child1,
1056                                                      allocation->height,
1057                                                      &child1_width, NULL);
1058           gtk_widget_get_preferred_width_for_height (priv->child2,
1059                                                      allocation->height,
1060                                                      &child2_width, NULL);
1061
1062           gtk_paned_calc_position (paned,
1063                                    MAX (1, allocation->width - handle_size),
1064                                    child1_width,
1065                                    child2_width);
1066
1067           priv->handle_pos.x = allocation->x + priv->child1_size;
1068           priv->handle_pos.y = allocation->y;
1069           priv->handle_pos.width = handle_size;
1070           priv->handle_pos.height = allocation->height;
1071
1072           window1_allocation.height = window2_allocation.height = allocation->height;
1073           window1_allocation.width = MAX (1, priv->child1_size);
1074           window1_allocation.x = allocation->x;
1075           window1_allocation.y = window2_allocation.y = allocation->y;
1076
1077           window2_allocation.x = window1_allocation.x + priv->child1_size + priv->handle_pos.width;
1078           window2_allocation.width = MAX (1, allocation->x + allocation->width - window2_allocation.x);
1079
1080           if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
1081             {
1082               flip_child (widget, &(window2_allocation));
1083               flip_child (widget, &(window1_allocation));
1084               flip_child (widget, &(priv->handle_pos));
1085             }
1086
1087           child1_allocation.x = child1_allocation.y = 0;
1088           child1_allocation.width = window1_allocation.width;
1089           child1_allocation.height = window1_allocation.height;
1090           if (child1_width > child1_allocation.width)
1091             {
1092               if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_LTR)
1093                 child1_allocation.x -= child1_width - child1_allocation.width;
1094               child1_allocation.width = child1_width;
1095             }
1096
1097           child2_allocation.x = child2_allocation.y = 0;
1098           child2_allocation.width = window2_allocation.width;
1099           child2_allocation.height = window2_allocation.height;
1100           if (child2_width > child2_allocation.width)
1101             {
1102               if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
1103                 child2_allocation.x -= child2_width - child2_allocation.width;
1104               child2_allocation.width = child2_width;
1105             }
1106         }
1107       else
1108         {
1109           gint child1_height, child2_height;
1110
1111           gtk_widget_get_preferred_height_for_width (priv->child1,
1112                                                      allocation->width,
1113                                                      &child1_height, NULL);
1114           gtk_widget_get_preferred_height_for_width (priv->child2,
1115                                                      allocation->width,
1116                                                      &child2_height, NULL);
1117
1118           gtk_paned_calc_position (paned,
1119                                    MAX (1, allocation->height - handle_size),
1120                                    child1_height,
1121                                    child2_height);
1122
1123           priv->handle_pos.x = allocation->x;
1124           priv->handle_pos.y = allocation->y + priv->child1_size;
1125           priv->handle_pos.width = allocation->width;
1126           priv->handle_pos.height = handle_size;
1127
1128           window1_allocation.width = window2_allocation.width = allocation->width;
1129           window1_allocation.height = MAX (1, priv->child1_size);
1130           window1_allocation.x = window2_allocation.x = allocation->x;
1131           window1_allocation.y = allocation->y;
1132
1133           window2_allocation.y = window1_allocation.y + priv->child1_size + priv->handle_pos.height;
1134           window2_allocation.height = MAX (1, allocation->y + allocation->height - window2_allocation.y);
1135
1136           child1_allocation.x = child1_allocation.y = 0;
1137           child1_allocation.width = window1_allocation.width;
1138           child1_allocation.height = window1_allocation.height;
1139           if (child1_height > child1_allocation.height)
1140             {
1141               child1_allocation.y -= child1_height - child1_allocation.height;
1142               child1_allocation.height = child1_height;
1143             }
1144
1145           child2_allocation.x = child2_allocation.y = 0;
1146           child2_allocation.width = window2_allocation.width;
1147           child2_allocation.height = window2_allocation.height;
1148           if (child2_height > child2_allocation.height)
1149             child2_allocation.height = child2_height;
1150         }
1151
1152       if (gtk_widget_get_mapped (widget) &&
1153           (old_handle_pos.x != priv->handle_pos.x ||
1154            old_handle_pos.y != priv->handle_pos.y ||
1155            old_handle_pos.width != priv->handle_pos.width ||
1156            old_handle_pos.height != priv->handle_pos.height))
1157         {
1158           GdkWindow *window;
1159
1160           window = gtk_widget_get_window (widget);
1161           gdk_window_invalidate_rect (window, &old_handle_pos, FALSE);
1162           gdk_window_invalidate_rect (window, &priv->handle_pos, FALSE);
1163         }
1164
1165       if (gtk_widget_get_realized (widget))
1166         {
1167           if (gtk_widget_get_mapped (widget))
1168             gdk_window_show (priv->handle);
1169
1170           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1171             {
1172               gdk_window_move_resize (priv->handle,
1173                                       priv->handle_pos.x,
1174                                       priv->handle_pos.y,
1175                                       handle_size,
1176                                       priv->handle_pos.height);
1177             }
1178           else
1179             {
1180               gdk_window_move_resize (priv->handle,
1181                                       priv->handle_pos.x,
1182                                       priv->handle_pos.y,
1183                                       priv->handle_pos.width,
1184                                       handle_size);
1185             }
1186         }
1187
1188       /* Now allocate the childen, making sure, when resizing not to
1189        * overlap the windows
1190        */
1191       gtk_widget_get_allocation (priv->child1, &priv_child1_allocation);
1192       if (gtk_widget_get_mapped (widget) &&
1193           ((priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1194             priv_child1_allocation.width < child1_allocation.width) ||
1195
1196            (priv->orientation == GTK_ORIENTATION_VERTICAL &&
1197             priv_child1_allocation.height < child1_allocation.height)))
1198         {
1199           gtk_paned_child_allocate (priv->child2,
1200                                     priv->child2_window,
1201                                     &window2_allocation,
1202                                     &child2_allocation);
1203           gtk_paned_child_allocate (priv->child1,
1204                                     priv->child1_window,
1205                                     &window1_allocation,
1206                                     &child1_allocation);
1207         }
1208       else
1209         {
1210           gtk_paned_child_allocate (priv->child1,
1211                                     priv->child1_window,
1212                                     &window1_allocation,
1213                                     &child1_allocation);
1214           gtk_paned_child_allocate (priv->child2,
1215                                     priv->child2_window,
1216                                     &window2_allocation,
1217                                     &child2_allocation);
1218         }
1219     }
1220   else
1221     {
1222       GtkAllocation window_allocation, child_allocation;
1223
1224       if (gtk_widget_get_realized (widget))
1225         gdk_window_hide (priv->handle);
1226
1227       window_allocation.x = allocation->x;
1228       window_allocation.y = allocation->y;
1229       window_allocation.width = allocation->width;
1230       window_allocation.height = allocation->height;
1231       child_allocation.x = child_allocation.y = 0;
1232       child_allocation.width = allocation->width;
1233       child_allocation.height = allocation->height;
1234
1235       if (priv->child1 && gtk_widget_get_visible (priv->child1))
1236         {
1237           gtk_paned_set_child_visible (paned, 0, TRUE);
1238           if (priv->child2)
1239             gtk_paned_set_child_visible (paned, 1, FALSE);
1240
1241           gtk_paned_child_allocate (priv->child1,
1242                                     priv->child1_window,
1243                                     &window_allocation,
1244                                     &child_allocation);
1245         }
1246       else if (priv->child2 && gtk_widget_get_visible (priv->child2))
1247         {
1248           gtk_paned_set_child_visible (paned, 1, TRUE);
1249           if (priv->child1)
1250             gtk_paned_set_child_visible (paned, 0, FALSE);
1251
1252           gtk_paned_child_allocate (priv->child2,
1253                                     priv->child2_window,
1254                                     &window_allocation,
1255                                     &child_allocation);
1256         }
1257       else
1258         {
1259           if (priv->child1)
1260             gtk_paned_set_child_visible (paned, 0, FALSE);
1261           if (priv->child2)
1262             gtk_paned_set_child_visible (paned, 1, FALSE);
1263         }
1264     }
1265 }
1266
1267 static GdkWindow *
1268 gtk_paned_create_child_window (GtkPaned  *paned,
1269                                GtkWidget *child) /* may be NULL */
1270 {
1271   GtkWidget *widget = GTK_WIDGET (paned);
1272   GtkPanedPrivate *priv = paned->priv;
1273   GdkWindow *window;
1274   GdkWindowAttr attributes;
1275   gint attributes_mask;
1276
1277   attributes.window_type = GDK_WINDOW_CHILD;
1278   attributes.wclass = GDK_INPUT_OUTPUT;
1279   attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
1280   if (child)
1281     {
1282       GtkAllocation allocation;
1283       int handle_size;
1284
1285       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
1286
1287       gtk_widget_get_allocation (widget, &allocation);
1288       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1289           child == priv->child2)
1290         attributes.x = priv->handle_pos.x + handle_size;
1291       else
1292         attributes.x = allocation.x;
1293       if (priv->orientation == GTK_ORIENTATION_VERTICAL &&
1294           child == priv->child2)
1295         attributes.y = priv->handle_pos.y + handle_size;
1296       else
1297         attributes.y = allocation.y;
1298
1299       gtk_widget_get_allocation (child, &allocation);
1300       attributes.width = allocation.width;
1301       attributes.height = allocation.height;
1302       attributes_mask = GDK_WA_X | GDK_WA_Y;
1303     }
1304   else
1305     {
1306       attributes.width = 1;
1307       attributes.height = 1;
1308       attributes_mask = 0;
1309     }
1310
1311   window = gdk_window_new (gtk_widget_get_window (widget),
1312                            &attributes, attributes_mask);
1313   gdk_window_set_user_data (window, paned);
1314   gtk_style_context_set_background (gtk_widget_get_style_context (widget), window);
1315
1316   if (child)
1317     gtk_widget_set_parent_window (child, window);
1318
1319   return window;
1320 }
1321
1322 static void
1323 gtk_paned_realize (GtkWidget *widget)
1324 {
1325   GtkPaned *paned = GTK_PANED (widget);
1326   GtkPanedPrivate *priv = paned->priv;
1327   GdkWindow *window;
1328   GdkWindowAttr attributes;
1329   gint attributes_mask;
1330
1331   gtk_widget_set_realized (widget, TRUE);
1332
1333   window = gtk_widget_get_parent_window (widget);
1334   gtk_widget_set_window (widget, window);
1335   g_object_ref (window);
1336
1337   attributes.window_type = GDK_WINDOW_CHILD;
1338   attributes.wclass = GDK_INPUT_ONLY;
1339   attributes.x = priv->handle_pos.x;
1340   attributes.y = priv->handle_pos.y;
1341   attributes.width = priv->handle_pos.width;
1342   attributes.height = priv->handle_pos.height;
1343   attributes.event_mask = gtk_widget_get_events (widget);
1344   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1345                             GDK_BUTTON_RELEASE_MASK |
1346                             GDK_ENTER_NOTIFY_MASK |
1347                             GDK_LEAVE_NOTIFY_MASK |
1348                             GDK_POINTER_MOTION_MASK |
1349                             GDK_POINTER_MOTION_HINT_MASK);
1350   attributes_mask = GDK_WA_X | GDK_WA_Y;
1351   if (gtk_widget_is_sensitive (widget))
1352     {
1353       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1354                                                       priv->cursor_type);
1355       attributes_mask |= GDK_WA_CURSOR;
1356     }
1357
1358   priv->handle = gdk_window_new (window,
1359                                  &attributes, attributes_mask);
1360   gdk_window_set_user_data (priv->handle, paned);
1361   if (attributes_mask & GDK_WA_CURSOR)
1362     g_object_unref (attributes.cursor);
1363
1364   priv->child1_window = gtk_paned_create_child_window (paned, priv->child1);
1365   priv->child2_window = gtk_paned_create_child_window (paned, priv->child2);
1366 }
1367
1368 static void
1369 gtk_paned_unrealize (GtkWidget *widget)
1370 {
1371   GtkPaned *paned = GTK_PANED (widget);
1372   GtkPanedPrivate *priv = paned->priv;
1373
1374   if (priv->child2)
1375     gtk_widget_set_parent_window (priv->child2, NULL);
1376   gdk_window_set_user_data (priv->child2_window, NULL);
1377   gdk_window_destroy (priv->child2_window);
1378   priv->child2_window = NULL;
1379
1380   if (priv->child1)
1381     gtk_widget_set_parent_window (priv->child1, NULL);
1382   gdk_window_set_user_data (priv->child1_window, NULL);
1383   gdk_window_destroy (priv->child1_window);
1384   priv->child1_window = NULL;
1385
1386   if (priv->handle)
1387     {
1388       gdk_window_set_user_data (priv->handle, NULL);
1389       gdk_window_destroy (priv->handle);
1390       priv->handle = NULL;
1391     }
1392
1393   gtk_paned_set_last_child1_focus (paned, NULL);
1394   gtk_paned_set_last_child2_focus (paned, NULL);
1395   gtk_paned_set_saved_focus (paned, NULL);
1396   gtk_paned_set_first_paned (paned, NULL);
1397
1398   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unrealize (widget);
1399 }
1400
1401 static void
1402 gtk_paned_map (GtkWidget *widget)
1403 {
1404   GtkPaned *paned = GTK_PANED (widget);
1405   GtkPanedPrivate *priv = paned->priv;
1406
1407   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
1408       priv->child2 && gtk_widget_get_visible (priv->child2))
1409     gdk_window_show (priv->handle);
1410
1411   if (priv->child1 && gtk_widget_get_visible (priv->child1) && gtk_widget_get_child_visible (priv->child1))
1412     gdk_window_show (priv->child1_window);
1413   if (priv->child2 && gtk_widget_get_visible (priv->child2) && gtk_widget_get_child_visible (priv->child2))
1414     gdk_window_show (priv->child2_window);
1415
1416   GTK_WIDGET_CLASS (gtk_paned_parent_class)->map (widget);
1417 }
1418
1419 static void
1420 gtk_paned_unmap (GtkWidget *widget)
1421 {
1422   GtkPaned *paned = GTK_PANED (widget);
1423   GtkPanedPrivate *priv = paned->priv;
1424
1425   gdk_window_hide (priv->handle);
1426   
1427   if (gdk_window_is_visible (priv->child1_window))
1428     gdk_window_hide (priv->child1_window);
1429   if (gdk_window_is_visible (priv->child2_window))
1430     gdk_window_hide (priv->child2_window);
1431
1432   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unmap (widget);
1433 }
1434
1435 static gboolean
1436 gtk_paned_draw (GtkWidget *widget,
1437                 cairo_t   *cr)
1438 {
1439   GtkPaned *paned = GTK_PANED (widget);
1440   GtkPanedPrivate *priv = paned->priv;
1441
1442   if (gtk_cairo_should_draw_window (cr, priv->child1_window))
1443     {
1444       cairo_save (cr);
1445       gtk_cairo_transform_to_window (cr, widget, priv->child1_window);
1446       gtk_render_background (gtk_widget_get_style_context (widget),
1447                              cr,
1448                              0, 0,
1449                              gdk_window_get_width (priv->child1_window),
1450                              gdk_window_get_height (priv->child1_window));
1451       cairo_restore (cr);
1452     }
1453
1454   if (gtk_cairo_should_draw_window (cr, priv->child2_window))
1455     {
1456       cairo_save (cr);
1457       gtk_cairo_transform_to_window (cr, widget, priv->child2_window);
1458       gtk_render_background (gtk_widget_get_style_context (widget),
1459                              cr,
1460                              0, 0,
1461                              gdk_window_get_width (priv->child2_window),
1462                              gdk_window_get_height (priv->child2_window));
1463       cairo_restore (cr);
1464     }
1465
1466   if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)) &&
1467       priv->child1 && gtk_widget_get_visible (priv->child1) &&
1468       priv->child2 && gtk_widget_get_visible (priv->child2))
1469     {
1470       GtkStyleContext *context;
1471       GtkStateFlags state;
1472       GtkAllocation allocation;
1473
1474       gtk_widget_get_allocation (widget, &allocation);
1475       context = gtk_widget_get_style_context (widget);
1476       state = gtk_widget_get_state_flags (widget);
1477
1478       if (gtk_widget_is_focus (widget))
1479         state |= GTK_STATE_FLAG_SELECTED;
1480       if (priv->handle_prelit)
1481         state |= GTK_STATE_FLAG_PRELIGHT;
1482
1483       gtk_style_context_save (context);
1484       gtk_style_context_set_state (context, state);
1485       gtk_style_context_add_class (context, GTK_STYLE_CLASS_PANE_SEPARATOR);
1486       gtk_render_handle (context, cr,
1487                          priv->handle_pos.x - allocation.x,
1488                          priv->handle_pos.y - allocation.y,
1489                          priv->handle_pos.width,
1490                          priv->handle_pos.height);
1491
1492       gtk_style_context_restore (context);
1493     }
1494
1495   /* Chain up to draw children */
1496   GTK_WIDGET_CLASS (gtk_paned_parent_class)->draw (widget, cr);
1497   
1498   return FALSE;
1499 }
1500
1501 static gboolean
1502 is_rtl (GtkPaned *paned)
1503 {
1504   GtkPanedPrivate *priv = paned->priv;
1505
1506   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1507       gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL)
1508     {
1509       return TRUE;
1510     }
1511
1512   return FALSE;
1513 }
1514
1515 static void
1516 update_drag (GtkPaned *paned)
1517 {
1518   GtkPanedPrivate *priv = paned->priv;
1519   GtkAllocation allocation;
1520   GtkWidget *widget = GTK_WIDGET (paned);
1521   gint pos;
1522   gint handle_size;
1523   gint size;
1524
1525   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1526     gtk_widget_get_pointer (widget, &pos, NULL);
1527   else
1528     gtk_widget_get_pointer (widget, NULL, &pos);
1529
1530   pos -= priv->drag_pos;
1531
1532   if (is_rtl (paned))
1533     {
1534       gtk_widget_style_get (widget,
1535                             "handle-size", &handle_size,
1536                             NULL);
1537
1538       gtk_widget_get_allocation (widget, &allocation);
1539       size = allocation.width - pos - handle_size;
1540     }
1541   else
1542     {
1543       size = pos;
1544     }
1545
1546   size = CLAMP (size, priv->min_position, priv->max_position);
1547
1548   if (size != priv->child1_size)
1549     gtk_paned_set_position (paned, size);
1550 }
1551
1552 static gboolean
1553 gtk_paned_enter (GtkWidget        *widget,
1554                  GdkEventCrossing *event)
1555 {
1556   GtkPaned *paned = GTK_PANED (widget);
1557   GtkPanedPrivate *priv = paned->priv;
1558
1559   if (priv->in_drag)
1560     update_drag (paned);
1561   else
1562     {
1563       priv->handle_prelit = TRUE;
1564       gtk_widget_queue_draw_area (widget,
1565                                   priv->handle_pos.x,
1566                                   priv->handle_pos.y,
1567                                   priv->handle_pos.width,
1568                                   priv->handle_pos.height);
1569     }
1570   
1571   return TRUE;
1572 }
1573
1574 static gboolean
1575 gtk_paned_leave (GtkWidget        *widget,
1576                  GdkEventCrossing *event)
1577 {
1578   GtkPaned *paned = GTK_PANED (widget);
1579   GtkPanedPrivate *priv = paned->priv;
1580
1581   if (priv->in_drag)
1582     update_drag (paned);
1583   else
1584     {
1585       priv->handle_prelit = FALSE;
1586       gtk_widget_queue_draw_area (widget,
1587                                   priv->handle_pos.x,
1588                                   priv->handle_pos.y,
1589                                   priv->handle_pos.width,
1590                                   priv->handle_pos.height);
1591     }
1592
1593   return TRUE;
1594 }
1595
1596 static gboolean
1597 gtk_paned_focus (GtkWidget        *widget,
1598                  GtkDirectionType  direction)
1599
1600 {
1601   gboolean retval;
1602   
1603   /* This is a hack, but how can this be done without
1604    * excessive cut-and-paste from gtkcontainer.c?
1605    */
1606
1607   gtk_widget_set_can_focus (widget, FALSE);
1608   retval = GTK_WIDGET_CLASS (gtk_paned_parent_class)->focus (widget, direction);
1609   gtk_widget_set_can_focus (widget, TRUE);
1610
1611   return retval;
1612 }
1613
1614 static gboolean
1615 gtk_paned_button_press (GtkWidget      *widget,
1616                         GdkEventButton *event)
1617 {
1618   GtkPaned *paned = GTK_PANED (widget);
1619   GtkPanedPrivate *priv = paned->priv;
1620
1621   if (!priv->in_drag &&
1622       (event->window == priv->handle) && (event->button == 1))
1623     {
1624       /* We need a server grab here, not gtk_grab_add(), since
1625        * we don't want to pass events on to the widget's children */
1626       if (gdk_device_grab (event->device,
1627                            priv->handle,
1628                            GDK_OWNERSHIP_WINDOW, FALSE,
1629                            GDK_POINTER_MOTION_HINT_MASK
1630                            | GDK_BUTTON1_MOTION_MASK
1631                            | GDK_BUTTON_RELEASE_MASK
1632                            | GDK_ENTER_NOTIFY_MASK
1633                            | GDK_LEAVE_NOTIFY_MASK,
1634                            NULL, event->time) != GDK_GRAB_SUCCESS)
1635         return FALSE;
1636
1637       priv->in_drag = TRUE;
1638       priv->grab_time = event->time;
1639       priv->grab_device = event->device;
1640
1641       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1642         priv->drag_pos = event->x;
1643       else
1644         priv->drag_pos = event->y;
1645
1646       return TRUE;
1647     }
1648
1649   return FALSE;
1650 }
1651
1652 static gboolean
1653 gtk_paned_grab_broken (GtkWidget          *widget,
1654                        GdkEventGrabBroken *event)
1655 {
1656   GtkPaned *paned = GTK_PANED (widget);
1657   GtkPanedPrivate *priv = paned->priv;
1658
1659   priv->in_drag = FALSE;
1660   priv->drag_pos = -1;
1661   priv->position_set = TRUE;
1662
1663   return TRUE;
1664 }
1665
1666 static void
1667 stop_drag (GtkPaned *paned)
1668 {
1669   GtkPanedPrivate *priv = paned->priv;
1670
1671   priv->in_drag = FALSE;
1672   priv->drag_pos = -1;
1673   priv->position_set = TRUE;
1674
1675   gdk_device_ungrab (priv->grab_device,
1676                      priv->grab_time);
1677   priv->grab_device = NULL;
1678 }
1679
1680 static void
1681 gtk_paned_grab_notify (GtkWidget *widget,
1682                        gboolean   was_grabbed)
1683 {
1684   GtkPaned *paned = GTK_PANED (widget);
1685   GtkPanedPrivate *priv = paned->priv;
1686   GdkDevice *grab_device;
1687
1688   grab_device = priv->grab_device;
1689
1690   if (priv->in_drag && grab_device &&
1691       gtk_widget_device_is_shadowed (widget, grab_device))
1692     stop_drag (paned);
1693 }
1694
1695 static void
1696 gtk_paned_state_flags_changed (GtkWidget     *widget,
1697                                GtkStateFlags  previous_state)
1698 {
1699   GtkPaned *paned = GTK_PANED (widget);
1700   GtkPanedPrivate *priv = paned->priv;
1701   GdkCursor *cursor;
1702
1703   if (gtk_widget_get_realized (widget))
1704     {
1705       if (gtk_widget_is_sensitive (widget))
1706         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1707                                              priv->cursor_type);
1708       else
1709         cursor = NULL;
1710
1711       gdk_window_set_cursor (priv->handle, cursor);
1712
1713       if (cursor)
1714         g_object_unref (cursor);
1715     }
1716 }
1717
1718 static gboolean
1719 gtk_paned_button_release (GtkWidget      *widget,
1720                           GdkEventButton *event)
1721 {
1722   GtkPaned *paned = GTK_PANED (widget);
1723   GtkPanedPrivate *priv = paned->priv;
1724
1725   if (priv->in_drag && (event->button == 1))
1726     {
1727       stop_drag (paned);
1728
1729       return TRUE;
1730     }
1731
1732   return FALSE;
1733 }
1734
1735 static gboolean
1736 gtk_paned_motion (GtkWidget      *widget,
1737                   GdkEventMotion *event)
1738 {
1739   GtkPaned *paned = GTK_PANED (widget);
1740   GtkPanedPrivate *priv = paned->priv;
1741
1742   if (priv->in_drag)
1743     {
1744       update_drag (paned);
1745       return TRUE;
1746     }
1747   
1748   return FALSE;
1749 }
1750
1751 /**
1752  * gtk_paned_new:
1753  * @orientation: the paned's orientation.
1754  *
1755  * Creates a new #GtkPaned widget.
1756  *
1757  * Return value: a new #GtkPaned.
1758  *
1759  * Since: 3.0
1760  **/
1761 GtkWidget *
1762 gtk_paned_new (GtkOrientation orientation)
1763 {
1764   return g_object_new (GTK_TYPE_PANED,
1765                        "orientation", orientation,
1766                        NULL);
1767 }
1768
1769 /**
1770  * gtk_paned_add1:
1771  * @paned: a paned widget
1772  * @child: the child to add
1773  *
1774  * Adds a child to the top or left pane with default parameters. This is
1775  * equivalent to
1776  * <literal>gtk_paned_pack1 (paned, child, FALSE, TRUE)</literal>.
1777  */
1778 void
1779 gtk_paned_add1 (GtkPaned  *paned,
1780                 GtkWidget *widget)
1781 {
1782   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
1783 }
1784
1785 /**
1786  * gtk_paned_add2:
1787  * @paned: a paned widget
1788  * @child: the child to add
1789  *
1790  * Adds a child to the bottom or right pane with default parameters. This
1791  * is equivalent to
1792  * <literal>gtk_paned_pack2 (paned, child, TRUE, TRUE)</literal>.
1793  */
1794 void
1795 gtk_paned_add2 (GtkPaned  *paned,
1796                 GtkWidget *widget)
1797 {
1798   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
1799 }
1800
1801 /**
1802  * gtk_paned_pack1:
1803  * @paned: a paned widget
1804  * @child: the child to add
1805  * @resize: should this child expand when the paned widget is resized.
1806  * @shrink: can this child be made smaller than its requisition.
1807  *
1808  * Adds a child to the top or left pane.
1809  */
1810 void
1811 gtk_paned_pack1 (GtkPaned  *paned,
1812                  GtkWidget *child,
1813                  gboolean   resize,
1814                  gboolean   shrink)
1815 {
1816   GtkPanedPrivate *priv;
1817
1818   g_return_if_fail (GTK_IS_PANED (paned));
1819   g_return_if_fail (GTK_IS_WIDGET (child));
1820
1821   priv = paned->priv;
1822
1823   if (!priv->child1)
1824     {
1825       priv->child1 = child;
1826       priv->child1_resize = resize;
1827       priv->child1_shrink = shrink;
1828
1829       gtk_widget_set_parent_window (child, priv->child1_window);
1830       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1831     }
1832 }
1833
1834 /**
1835  * gtk_paned_pack2:
1836  * @paned: a paned widget
1837  * @child: the child to add
1838  * @resize: should this child expand when the paned widget is resized.
1839  * @shrink: can this child be made smaller than its requisition.
1840  *
1841  * Adds a child to the bottom or right pane.
1842  */
1843 void
1844 gtk_paned_pack2 (GtkPaned  *paned,
1845                  GtkWidget *child,
1846                  gboolean   resize,
1847                  gboolean   shrink)
1848 {
1849   GtkPanedPrivate *priv;
1850
1851   g_return_if_fail (GTK_IS_PANED (paned));
1852   g_return_if_fail (GTK_IS_WIDGET (child));
1853
1854   priv = paned->priv;
1855
1856   if (!priv->child2)
1857     {
1858       priv->child2 = child;
1859       priv->child2_resize = resize;
1860       priv->child2_shrink = shrink;
1861
1862       gtk_widget_set_parent_window (child, priv->child2_window);
1863       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1864     }
1865 }
1866
1867
1868 static void
1869 gtk_paned_add (GtkContainer *container,
1870                GtkWidget    *widget)
1871 {
1872   GtkPanedPrivate *priv;
1873   GtkPaned *paned;
1874
1875   g_return_if_fail (GTK_IS_PANED (container));
1876
1877   paned = GTK_PANED (container);
1878   priv = paned->priv;
1879
1880   if (!priv->child1)
1881     gtk_paned_add1 (paned, widget);
1882   else if (!priv->child2)
1883     gtk_paned_add2 (paned, widget);
1884   else
1885     g_warning ("GtkPaned cannot have more than 2 children\n");
1886 }
1887
1888 static void
1889 gtk_paned_remove (GtkContainer *container,
1890                   GtkWidget    *widget)
1891 {
1892   GtkPaned *paned = GTK_PANED (container);
1893   GtkPanedPrivate *priv = paned->priv;
1894   gboolean was_visible;
1895
1896   was_visible = gtk_widget_get_visible (widget);
1897
1898   if (priv->child1 == widget)
1899     {
1900       if (priv->child1_window && gdk_window_is_visible (priv->child1_window))
1901         gdk_window_hide (priv->child1_window);
1902
1903       gtk_widget_unparent (widget);
1904
1905       priv->child1 = NULL;
1906
1907       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1908         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1909     }
1910   else if (priv->child2 == widget)
1911     {
1912       if (priv->child2_window && gdk_window_is_visible (priv->child2_window))
1913         gdk_window_hide (priv->child2_window);
1914
1915       gtk_widget_unparent (widget);
1916
1917       priv->child2 = NULL;
1918
1919       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1920         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1921     }
1922 }
1923
1924 static void
1925 gtk_paned_forall (GtkContainer *container,
1926                   gboolean      include_internals,
1927                   GtkCallback   callback,
1928                   gpointer      callback_data)
1929 {
1930   GtkPanedPrivate *priv;
1931   GtkPaned *paned;
1932
1933   g_return_if_fail (callback != NULL);
1934
1935   paned = GTK_PANED (container);
1936   priv = paned->priv;
1937
1938   if (priv->child1)
1939     (*callback) (priv->child1, callback_data);
1940   if (priv->child2)
1941     (*callback) (priv->child2, callback_data);
1942 }
1943
1944 /**
1945  * gtk_paned_get_position:
1946  * @paned: a #GtkPaned widget
1947  * 
1948  * Obtains the position of the divider between the two panes.
1949  * 
1950  * Return value: position of the divider
1951  **/
1952 gint
1953 gtk_paned_get_position (GtkPaned  *paned)
1954 {
1955   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1956
1957   return paned->priv->child1_size;
1958 }
1959
1960 /**
1961  * gtk_paned_set_position:
1962  * @paned: a #GtkPaned widget
1963  * @position: pixel position of divider, a negative value means that the position
1964  *            is unset.
1965  * 
1966  * Sets the position of the divider between the two panes.
1967  **/
1968 void
1969 gtk_paned_set_position (GtkPaned *paned,
1970                         gint      position)
1971 {
1972   GtkPanedPrivate *priv;
1973   GObject *object;
1974
1975   g_return_if_fail (GTK_IS_PANED (paned));
1976
1977   priv = paned->priv;
1978
1979   if (priv->child1_size == position)
1980     return;
1981
1982   object = G_OBJECT (paned);
1983   
1984   if (position >= 0)
1985     {
1986       /* We don't clamp here - the assumption is that
1987        * if the total allocation changes at the same time
1988        * as the position, the position set is with reference
1989        * to the new total size. If only the position changes,
1990        * then clamping will occur in gtk_paned_calc_position()
1991        */
1992
1993       priv->child1_size = position;
1994       priv->position_set = TRUE;
1995     }
1996   else
1997     {
1998       priv->position_set = FALSE;
1999     }
2000
2001   g_object_freeze_notify (object);
2002   g_object_notify (object, "position");
2003   g_object_notify (object, "position-set");
2004   g_object_thaw_notify (object);
2005
2006   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
2007
2008 #ifdef G_OS_WIN32
2009   /* Hacky work-around for bug #144269 */
2010   if (priv->child2 != NULL)
2011     {
2012       gtk_widget_queue_draw (priv->child2);
2013     }
2014 #endif
2015 }
2016
2017 /**
2018  * gtk_paned_get_child1:
2019  * @paned: a #GtkPaned widget
2020  * 
2021  * Obtains the first child of the paned widget.
2022  * 
2023  * Return value: (transfer none): first child, or %NULL if it is not set.
2024  *
2025  * Since: 2.4
2026  **/
2027 GtkWidget *
2028 gtk_paned_get_child1 (GtkPaned *paned)
2029 {
2030   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2031
2032   return paned->priv->child1;
2033 }
2034
2035 /**
2036  * gtk_paned_get_child2:
2037  * @paned: a #GtkPaned widget
2038  * 
2039  * Obtains the second child of the paned widget.
2040  * 
2041  * Return value: (transfer none): second child, or %NULL if it is not set.
2042  *
2043  * Since: 2.4
2044  **/
2045 GtkWidget *
2046 gtk_paned_get_child2 (GtkPaned *paned)
2047 {
2048   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2049
2050   return paned->priv->child2;
2051 }
2052
2053 static void
2054 gtk_paned_calc_position (GtkPaned *paned,
2055                          gint      allocation,
2056                          gint      child1_req,
2057                          gint      child2_req)
2058 {
2059   GtkPanedPrivate *priv = paned->priv;
2060   gint old_position;
2061   gint old_min_position;
2062   gint old_max_position;
2063
2064   old_position = priv->child1_size;
2065   old_min_position = priv->min_position;
2066   old_max_position = priv->max_position;
2067
2068   priv->min_position = priv->child1_shrink ? 0 : child1_req;
2069
2070   priv->max_position = allocation;
2071   if (!priv->child2_shrink)
2072     priv->max_position = MAX (1, priv->max_position - child2_req);
2073   priv->max_position = MAX (priv->min_position, priv->max_position);
2074
2075   if (!priv->position_set)
2076     {
2077       if (priv->child1_resize && !priv->child2_resize)
2078         priv->child1_size = MAX (0, allocation - child2_req);
2079       else if (!priv->child1_resize && priv->child2_resize)
2080         priv->child1_size = child1_req;
2081       else if (child1_req + child2_req != 0)
2082         priv->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
2083       else
2084         priv->child1_size = allocation * 0.5 + 0.5;
2085     }
2086   else
2087     {
2088       /* If the position was set before the initial allocation.
2089        * (priv->last_allocation <= 0) just clamp it and leave it.
2090        */
2091       if (priv->last_allocation > 0)
2092         {
2093           if (priv->child1_resize && !priv->child2_resize)
2094             priv->child1_size += allocation - priv->last_allocation;
2095           else if (!(!priv->child1_resize && priv->child2_resize))
2096             priv->child1_size = allocation * ((gdouble) priv->child1_size / (priv->last_allocation)) + 0.5;
2097         }
2098     }
2099
2100   priv->child1_size = CLAMP (priv->child1_size,
2101                               priv->min_position,
2102                               priv->max_position);
2103
2104   if (priv->child1)
2105     gtk_paned_set_child_visible (paned, 0, priv->child1_size != 0);
2106   
2107   if (priv->child2)
2108     gtk_paned_set_child_visible (paned, 1, priv->child1_size != allocation); 
2109
2110   g_object_freeze_notify (G_OBJECT (paned));
2111   if (priv->child1_size != old_position)
2112     g_object_notify (G_OBJECT (paned), "position");
2113   if (priv->min_position != old_min_position)
2114     g_object_notify (G_OBJECT (paned), "min-position");
2115   if (priv->max_position != old_max_position)
2116     g_object_notify (G_OBJECT (paned), "max-position");
2117   g_object_thaw_notify (G_OBJECT (paned));
2118
2119   priv->last_allocation = allocation;
2120 }
2121
2122 static void
2123 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
2124 {
2125   GtkPanedPrivate *priv = paned->priv;
2126
2127   if (priv->saved_focus)
2128     g_object_remove_weak_pointer (G_OBJECT (priv->saved_focus),
2129                                   (gpointer *)&(priv->saved_focus));
2130
2131   priv->saved_focus = widget;
2132
2133   if (priv->saved_focus)
2134     g_object_add_weak_pointer (G_OBJECT (priv->saved_focus),
2135                                (gpointer *)&(priv->saved_focus));
2136 }
2137
2138 static void
2139 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
2140 {
2141   GtkPanedPrivate *priv = paned->priv;
2142
2143   if (priv->first_paned)
2144     g_object_remove_weak_pointer (G_OBJECT (priv->first_paned),
2145                                   (gpointer *)&(priv->first_paned));
2146
2147   priv->first_paned = first_paned;
2148
2149   if (priv->first_paned)
2150     g_object_add_weak_pointer (G_OBJECT (priv->first_paned),
2151                                (gpointer *)&(priv->first_paned));
2152 }
2153
2154 static void
2155 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
2156 {
2157   GtkPanedPrivate *priv = paned->priv;
2158
2159   if (priv->last_child1_focus)
2160     g_object_remove_weak_pointer (G_OBJECT (priv->last_child1_focus),
2161                                   (gpointer *)&(priv->last_child1_focus));
2162
2163   priv->last_child1_focus = widget;
2164
2165   if (priv->last_child1_focus)
2166     g_object_add_weak_pointer (G_OBJECT (priv->last_child1_focus),
2167                                (gpointer *)&(priv->last_child1_focus));
2168 }
2169
2170 static void
2171 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
2172 {
2173   GtkPanedPrivate *priv = paned->priv;
2174
2175   if (priv->last_child2_focus)
2176     g_object_remove_weak_pointer (G_OBJECT (priv->last_child2_focus),
2177                                   (gpointer *)&(priv->last_child2_focus));
2178
2179   priv->last_child2_focus = widget;
2180
2181   if (priv->last_child2_focus)
2182     g_object_add_weak_pointer (G_OBJECT (priv->last_child2_focus),
2183                                (gpointer *)&(priv->last_child2_focus));
2184 }
2185
2186 static GtkWidget *
2187 paned_get_focus_widget (GtkPaned *paned)
2188 {
2189   GtkWidget *toplevel;
2190
2191   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2192   if (gtk_widget_is_toplevel (toplevel))
2193     return gtk_window_get_focus (GTK_WINDOW (toplevel));
2194
2195   return NULL;
2196 }
2197
2198 static void
2199 gtk_paned_set_focus_child (GtkContainer *container,
2200                            GtkWidget    *focus_child)
2201 {
2202   GtkPaned *paned;
2203   GtkPanedPrivate *priv;
2204   GtkWidget *container_focus_child;
2205
2206   g_return_if_fail (GTK_IS_PANED (container));
2207
2208   paned = GTK_PANED (container);
2209   priv = paned->priv;
2210
2211   if (focus_child == NULL)
2212     {
2213       GtkWidget *last_focus;
2214       GtkWidget *w;
2215       
2216       last_focus = paned_get_focus_widget (paned);
2217
2218       if (last_focus)
2219         {
2220           /* If there is one or more paned widgets between us and the
2221            * focus widget, we want the topmost of those as last_focus
2222            */
2223           for (w = last_focus; w != GTK_WIDGET (paned); w = gtk_widget_get_parent (w))
2224             if (GTK_IS_PANED (w))
2225               last_focus = w;
2226
2227           container_focus_child = gtk_container_get_focus_child (container);
2228           if (container_focus_child == priv->child1)
2229             gtk_paned_set_last_child1_focus (paned, last_focus);
2230           else if (container_focus_child == priv->child2)
2231             gtk_paned_set_last_child2_focus (paned, last_focus);
2232         }
2233     }
2234
2235   if (GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child)
2236     GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child (container, focus_child);
2237 }
2238
2239 static void
2240 gtk_paned_get_cycle_chain (GtkPaned          *paned,
2241                            GtkDirectionType   direction,
2242                            GList            **widgets)
2243 {
2244   GtkPanedPrivate *priv = paned->priv;
2245   GtkContainer *container = GTK_CONTAINER (paned);
2246   GtkWidget *ancestor = NULL;
2247   GtkWidget *focus_child;
2248   GtkWidget *parent;
2249   GtkWidget *widget = GTK_WIDGET (paned);
2250   GList *temp_list = NULL;
2251   GList *list;
2252
2253   if (priv->in_recursion)
2254     return;
2255
2256   g_assert (widgets != NULL);
2257
2258   if (priv->last_child1_focus &&
2259       !gtk_widget_is_ancestor (priv->last_child1_focus, widget))
2260     {
2261       gtk_paned_set_last_child1_focus (paned, NULL);
2262     }
2263
2264   if (priv->last_child2_focus &&
2265       !gtk_widget_is_ancestor (priv->last_child2_focus, widget))
2266     {
2267       gtk_paned_set_last_child2_focus (paned, NULL);
2268     }
2269
2270   parent = gtk_widget_get_parent (widget);
2271   if (parent)
2272     ancestor = gtk_widget_get_ancestor (parent, GTK_TYPE_PANED);
2273
2274   /* The idea here is that temp_list is a list of widgets we want to cycle
2275    * to. The list is prioritized so that the first element is our first
2276    * choice, the next our second, and so on.
2277    *
2278    * We can't just use g_list_reverse(), because we want to try
2279    * priv->last_child?_focus before priv->child?, both when we
2280    * are going forward and backward.
2281    */
2282   focus_child = gtk_container_get_focus_child (container);
2283   if (direction == GTK_DIR_TAB_FORWARD)
2284     {
2285       if (focus_child == priv->child1)
2286         {
2287           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2288           temp_list = g_list_append (temp_list, priv->child2);
2289           temp_list = g_list_append (temp_list, ancestor);
2290         }
2291       else if (focus_child == priv->child2)
2292         {
2293           temp_list = g_list_append (temp_list, ancestor);
2294           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2295           temp_list = g_list_append (temp_list, priv->child1);
2296         }
2297       else
2298         {
2299           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2300           temp_list = g_list_append (temp_list, priv->child1);
2301           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2302           temp_list = g_list_append (temp_list, priv->child2);
2303           temp_list = g_list_append (temp_list, ancestor);
2304         }
2305     }
2306   else
2307     {
2308       if (focus_child == priv->child1)
2309         {
2310           temp_list = g_list_append (temp_list, ancestor);
2311           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2312           temp_list = g_list_append (temp_list, priv->child2);
2313         }
2314       else if (focus_child == priv->child2)
2315         {
2316           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2317           temp_list = g_list_append (temp_list, priv->child1);
2318           temp_list = g_list_append (temp_list, ancestor);
2319         }
2320       else
2321         {
2322           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2323           temp_list = g_list_append (temp_list, priv->child2);
2324           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2325           temp_list = g_list_append (temp_list, priv->child1);
2326           temp_list = g_list_append (temp_list, ancestor);
2327         }
2328     }
2329
2330   /* Walk the list and expand all the paned widgets. */
2331   for (list = temp_list; list != NULL; list = list->next)
2332     {
2333       GtkWidget *widget = list->data;
2334
2335       if (widget)
2336         {
2337           if (GTK_IS_PANED (widget))
2338             {
2339               priv->in_recursion = TRUE;
2340               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
2341               priv->in_recursion = FALSE;
2342             }
2343           else
2344             {
2345               *widgets = g_list_append (*widgets, widget);
2346             }
2347         }
2348     }
2349
2350   g_list_free (temp_list);
2351 }
2352
2353 static gboolean
2354 gtk_paned_cycle_child_focus (GtkPaned *paned,
2355                              gboolean  reversed)
2356 {
2357   GList *cycle_chain = NULL;
2358   GList *list;
2359   
2360   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
2361
2362   /* ignore f6 if the handle is focused */
2363   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2364     return TRUE;
2365   
2366   /* we can't just let the event propagate up the hierarchy,
2367    * because the paned will want to cycle focus _unless_ an
2368    * ancestor paned handles the event
2369    */
2370   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
2371
2372   for (list = cycle_chain; list != NULL; list = list->next)
2373     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
2374       break;
2375
2376   g_list_free (cycle_chain);
2377   
2378   return TRUE;
2379 }
2380
2381 static void
2382 get_child_panes (GtkWidget  *widget,
2383                  GList     **panes)
2384 {
2385   if (!widget || !gtk_widget_get_realized (widget))
2386     return;
2387
2388   if (GTK_IS_PANED (widget))
2389     {
2390       GtkPaned *paned = GTK_PANED (widget);
2391       GtkPanedPrivate *priv = paned->priv;
2392
2393       get_child_panes (priv->child1, panes);
2394       *panes = g_list_prepend (*panes, widget);
2395       get_child_panes (priv->child2, panes);
2396     }
2397   else if (GTK_IS_CONTAINER (widget))
2398     {
2399       gtk_container_forall (GTK_CONTAINER (widget),
2400                             (GtkCallback)get_child_panes, panes);
2401     }
2402 }
2403
2404 static GList *
2405 get_all_panes (GtkPaned *paned)
2406 {
2407   GtkPaned *topmost = NULL;
2408   GList *result = NULL;
2409   GtkWidget *w;
2410
2411   for (w = GTK_WIDGET (paned); w != NULL; w = gtk_widget_get_parent (w))
2412     {
2413       if (GTK_IS_PANED (w))
2414         topmost = GTK_PANED (w);
2415     }
2416
2417   g_assert (topmost);
2418
2419   get_child_panes (GTK_WIDGET (topmost), &result);
2420
2421   return g_list_reverse (result);
2422 }
2423
2424 static void
2425 gtk_paned_find_neighbours (GtkPaned  *paned,
2426                            GtkPaned **next,
2427                            GtkPaned **prev)
2428 {
2429   GList *all_panes;
2430   GList *this_link;
2431
2432   all_panes = get_all_panes (paned);
2433   g_assert (all_panes);
2434
2435   this_link = g_list_find (all_panes, paned);
2436
2437   g_assert (this_link);
2438   
2439   if (this_link->next)
2440     *next = this_link->next->data;
2441   else
2442     *next = all_panes->data;
2443
2444   if (this_link->prev)
2445     *prev = this_link->prev->data;
2446   else
2447     *prev = g_list_last (all_panes)->data;
2448
2449   g_list_free (all_panes);
2450 }
2451
2452 static gboolean
2453 gtk_paned_move_handle (GtkPaned      *paned,
2454                        GtkScrollType  scroll)
2455 {
2456   GtkPanedPrivate *priv = paned->priv;
2457
2458   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2459     {
2460       gint old_position;
2461       gint new_position;
2462       gint increment;
2463       
2464       enum {
2465         SINGLE_STEP_SIZE = 1,
2466         PAGE_STEP_SIZE   = 75
2467       };
2468       
2469       new_position = old_position = gtk_paned_get_position (paned);
2470       increment = 0;
2471       
2472       switch (scroll)
2473         {
2474         case GTK_SCROLL_STEP_LEFT:
2475         case GTK_SCROLL_STEP_UP:
2476         case GTK_SCROLL_STEP_BACKWARD:
2477           increment = - SINGLE_STEP_SIZE;
2478           break;
2479           
2480         case GTK_SCROLL_STEP_RIGHT:
2481         case GTK_SCROLL_STEP_DOWN:
2482         case GTK_SCROLL_STEP_FORWARD:
2483           increment = SINGLE_STEP_SIZE;
2484           break;
2485           
2486         case GTK_SCROLL_PAGE_LEFT:
2487         case GTK_SCROLL_PAGE_UP:
2488         case GTK_SCROLL_PAGE_BACKWARD:
2489           increment = - PAGE_STEP_SIZE;
2490           break;
2491           
2492         case GTK_SCROLL_PAGE_RIGHT:
2493         case GTK_SCROLL_PAGE_DOWN:
2494         case GTK_SCROLL_PAGE_FORWARD:
2495           increment = PAGE_STEP_SIZE;
2496           break;
2497           
2498         case GTK_SCROLL_START:
2499           new_position = priv->min_position;
2500           break;
2501           
2502         case GTK_SCROLL_END:
2503           new_position = priv->max_position;
2504           break;
2505
2506         default:
2507           break;
2508         }
2509
2510       if (increment)
2511         {
2512           if (is_rtl (paned))
2513             increment = -increment;
2514           
2515           new_position = old_position + increment;
2516         }
2517       
2518       new_position = CLAMP (new_position, priv->min_position, priv->max_position);
2519       
2520       if (old_position != new_position)
2521         gtk_paned_set_position (paned, new_position);
2522
2523       return TRUE;
2524     }
2525
2526   return FALSE;
2527 }
2528
2529 static void
2530 gtk_paned_restore_focus (GtkPaned *paned)
2531 {
2532   GtkPanedPrivate *priv = paned->priv;
2533
2534   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2535     {
2536       if (priv->saved_focus &&
2537           gtk_widget_get_sensitive (priv->saved_focus))
2538         {
2539           gtk_widget_grab_focus (priv->saved_focus);
2540         }
2541       else
2542         {
2543           /* the saved focus is somehow not available for focusing,
2544            * try
2545            *   1) tabbing into the paned widget
2546            * if that didn't work,
2547            *   2) unset focus for the window if there is one
2548            */
2549           
2550           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
2551             {
2552               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2553               
2554               if (GTK_IS_WINDOW (toplevel))
2555                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
2556             }
2557         }
2558       
2559       gtk_paned_set_saved_focus (paned, NULL);
2560       gtk_paned_set_first_paned (paned, NULL);
2561     }
2562 }
2563
2564 static gboolean
2565 gtk_paned_accept_position (GtkPaned *paned)
2566 {
2567   GtkPanedPrivate *priv = paned->priv;
2568
2569   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2570     {
2571       priv->original_position = -1;
2572       gtk_paned_restore_focus (paned);
2573
2574       return TRUE;
2575     }
2576
2577   return FALSE;
2578 }
2579
2580
2581 static gboolean
2582 gtk_paned_cancel_position (GtkPaned *paned)
2583 {
2584   GtkPanedPrivate *priv = paned->priv;
2585
2586   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2587     {
2588       if (priv->original_position != -1)
2589         {
2590           gtk_paned_set_position (paned, priv->original_position);
2591           priv->original_position = -1;
2592         }
2593
2594       gtk_paned_restore_focus (paned);
2595       return TRUE;
2596     }
2597
2598   return FALSE;
2599 }
2600
2601 static gboolean
2602 gtk_paned_cycle_handle_focus (GtkPaned *paned,
2603                               gboolean  reversed)
2604 {
2605   GtkPanedPrivate *priv = paned->priv;
2606   GtkPaned *next, *prev;
2607
2608   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2609     {
2610       GtkPaned *focus = NULL;
2611
2612       if (!priv->first_paned)
2613         {
2614           /* The first_pane has disappeared. As an ad-hoc solution,
2615            * we make the currently focused paned the first_paned. To the
2616            * user this will seem like the paned cycling has been reset.
2617            */
2618           
2619           gtk_paned_set_first_paned (paned, paned);
2620         }
2621       
2622       gtk_paned_find_neighbours (paned, &next, &prev);
2623
2624       if (reversed && prev &&
2625           prev != paned && paned != priv->first_paned)
2626         {
2627           focus = prev;
2628         }
2629       else if (!reversed && next &&
2630                next != paned && next != priv->first_paned)
2631         {
2632           focus = next;
2633         }
2634       else
2635         {
2636           gtk_paned_accept_position (paned);
2637           return TRUE;
2638         }
2639
2640       g_assert (focus);
2641       
2642       gtk_paned_set_saved_focus (focus, priv->saved_focus);
2643       gtk_paned_set_first_paned (focus, priv->first_paned);
2644       
2645       gtk_paned_set_saved_focus (paned, NULL);
2646       gtk_paned_set_first_paned (paned, NULL);
2647       
2648       gtk_widget_grab_focus (GTK_WIDGET (focus));
2649       
2650       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
2651         {
2652           priv->original_position = -1;
2653           focus->priv->original_position = gtk_paned_get_position (focus);
2654         }
2655     }
2656   else
2657     {
2658       GtkContainer *container = GTK_CONTAINER (paned);
2659       GtkPaned *focus;
2660       GtkPaned *first;
2661       GtkPaned *prev, *next;
2662       GtkWidget *toplevel;
2663       GtkWidget *focus_child;
2664
2665       gtk_paned_find_neighbours (paned, &next, &prev);
2666       focus_child = gtk_container_get_focus_child (container);
2667
2668       if (focus_child == priv->child1)
2669         {
2670           if (reversed)
2671             {
2672               focus = prev;
2673               first = paned;
2674             }
2675           else
2676             {
2677               focus = paned;
2678               first = paned;
2679             }
2680         }
2681       else if (focus_child == priv->child2)
2682         {
2683           if (reversed)
2684             {
2685               focus = paned;
2686               first = next;
2687             }
2688           else
2689             {
2690               focus = next;
2691               first = next;
2692             }
2693         }
2694       else
2695         {
2696           /* Focus is not inside this paned, and we don't have focus.
2697            * Presumably this happened because the application wants us
2698            * to start keyboard navigating.
2699            */
2700           focus = paned;
2701
2702           if (reversed)
2703             first = paned;
2704           else
2705             first = next;
2706         }
2707
2708       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2709
2710       if (GTK_IS_WINDOW (toplevel))
2711         gtk_paned_set_saved_focus (focus, gtk_window_get_focus (GTK_WINDOW (toplevel)));
2712       gtk_paned_set_first_paned (focus, first);
2713       focus->priv->original_position = gtk_paned_get_position (focus);
2714
2715       gtk_widget_grab_focus (GTK_WIDGET (focus));
2716    }
2717
2718   return TRUE;
2719 }
2720
2721 static gboolean
2722 gtk_paned_toggle_handle_focus (GtkPaned *paned)
2723 {
2724   /* This function/signal has the wrong name. It is called when you
2725    * press Tab or Shift-Tab and what we do is act as if
2726    * the user pressed Return and then Tab or Shift-Tab
2727    */
2728   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2729     gtk_paned_accept_position (paned);
2730
2731   return FALSE;
2732 }
2733
2734 /**
2735  * gtk_paned_get_handle_window:
2736  * @paned: a #GtkPaned
2737  *
2738  * Returns the #GdkWindow of the handle. This function is
2739  * useful when handling button or motion events because it
2740  * enables the callback to distinguish between the window
2741  * of the paned, a child and the handle.
2742  *
2743  * Return value: (transfer none): the paned's handle window.
2744  *
2745  * Since: 2.20
2746  **/
2747 GdkWindow *
2748 gtk_paned_get_handle_window (GtkPaned *paned)
2749 {
2750   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2751
2752   return paned->priv->handle;
2753 }