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