]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
paned: Pass x/y position to update_drag()
[~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 #include "a11y/gtkpanedaccessible.h"
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   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PANED_ACCESSIBLE);
657 }
658
659 static GType
660 gtk_paned_child_type (GtkContainer *container)
661 {
662   GtkPaned *paned = GTK_PANED (container);
663   GtkPanedPrivate *priv = paned->priv;
664
665   if (!priv->child1 || !priv->child2)
666     return GTK_TYPE_WIDGET;
667   else
668     return G_TYPE_NONE;
669 }
670
671 static void
672 gtk_paned_init (GtkPaned *paned)
673 {
674   GtkPanedPrivate *priv;
675
676   gtk_widget_set_has_window (GTK_WIDGET (paned), FALSE);
677   gtk_widget_set_can_focus (GTK_WIDGET (paned), TRUE);
678
679   /* We only need to redraw when the handle position moves, which is
680    * independent of the overall allocation of the GtkPaned
681    */
682   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (paned), FALSE);
683
684   paned->priv = G_TYPE_INSTANCE_GET_PRIVATE (paned, GTK_TYPE_PANED, GtkPanedPrivate);
685   priv = paned->priv;
686
687   priv->orientation = GTK_ORIENTATION_HORIZONTAL;
688   priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
689
690   priv->child1 = NULL;
691   priv->child2 = NULL;
692   priv->handle = NULL;
693   priv->cursor_type = GDK_CROSS;
694
695   priv->handle_pos.width = 5;
696   priv->handle_pos.height = 5;
697   priv->position_set = FALSE;
698   priv->last_allocation = -1;
699   priv->in_drag = FALSE;
700
701   priv->last_child1_focus = NULL;
702   priv->last_child2_focus = NULL;
703   priv->in_recursion = FALSE;
704   priv->handle_prelit = FALSE;
705   priv->original_position = -1;
706
707   priv->handle_pos.x = -1;
708   priv->handle_pos.y = -1;
709
710   priv->drag_pos = -1;
711 }
712
713 static void
714 gtk_paned_set_property (GObject        *object,
715                         guint           prop_id,
716                         const GValue   *value,
717                         GParamSpec     *pspec)
718 {
719   GtkPaned *paned = GTK_PANED (object);
720   GtkPanedPrivate *priv = paned->priv;
721
722   switch (prop_id)
723     {
724     case PROP_ORIENTATION:
725       priv->orientation = g_value_get_enum (value);
726
727       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
728         priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
729       else
730         priv->cursor_type = GDK_SB_V_DOUBLE_ARROW;
731
732       /* state_flags_changed updates the cursor */
733       gtk_paned_state_flags_changed (GTK_WIDGET (paned), 0);
734       gtk_widget_queue_resize (GTK_WIDGET (paned));
735       break;
736     case PROP_POSITION:
737       gtk_paned_set_position (paned, g_value_get_int (value));
738       break;
739     case PROP_POSITION_SET:
740       priv->position_set = g_value_get_boolean (value);
741       gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
742       break;
743     default:
744       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
745       break;
746     }
747 }
748
749 static void
750 gtk_paned_get_property (GObject        *object,
751                         guint           prop_id,
752                         GValue         *value,
753                         GParamSpec     *pspec)
754 {
755   GtkPaned *paned = GTK_PANED (object);
756   GtkPanedPrivate *priv = paned->priv;
757
758   switch (prop_id)
759     {
760     case PROP_ORIENTATION:
761       g_value_set_enum (value, priv->orientation);
762       break;
763     case PROP_POSITION:
764       g_value_set_int (value, priv->child1_size);
765       break;
766     case PROP_POSITION_SET:
767       g_value_set_boolean (value, priv->position_set);
768       break;
769     case PROP_MIN_POSITION:
770       g_value_set_int (value, priv->min_position);
771       break;
772     case PROP_MAX_POSITION:
773       g_value_set_int (value, priv->max_position);
774       break;
775     default:
776       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
777       break;
778     }
779 }
780
781 static void
782 gtk_paned_set_child_property (GtkContainer    *container,
783                               GtkWidget       *child,
784                               guint            property_id,
785                               const GValue    *value,
786                               GParamSpec      *pspec)
787 {
788   GtkPaned *paned = GTK_PANED (container);
789   GtkPanedPrivate *priv = paned->priv;
790   gboolean old_value, new_value;
791
792   g_assert (child == priv->child1 || child == priv->child2);
793
794   new_value = g_value_get_boolean (value);
795   switch (property_id)
796     {
797     case CHILD_PROP_RESIZE:
798       if (child == priv->child1)
799         {
800           old_value = priv->child1_resize;
801           priv->child1_resize = new_value;
802         }
803       else
804         {
805           old_value = priv->child2_resize;
806           priv->child2_resize = new_value;
807         }
808       break;
809     case CHILD_PROP_SHRINK:
810       if (child == priv->child1)
811         {
812           old_value = priv->child1_shrink;
813           priv->child1_shrink = new_value;
814         }
815       else
816         {
817           old_value = priv->child2_shrink;
818           priv->child2_shrink = new_value;
819         }
820       break;
821     default:
822       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
823       old_value = -1; /* quiet gcc */
824       break;
825     }
826   if (old_value != new_value)
827     gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
828 }
829
830 static void
831 gtk_paned_get_child_property (GtkContainer *container,
832                               GtkWidget    *child,
833                               guint         property_id,
834                               GValue       *value,
835                               GParamSpec   *pspec)
836 {
837   GtkPaned *paned = GTK_PANED (container);
838   GtkPanedPrivate *priv = paned->priv;
839
840   g_assert (child == priv->child1 || child == priv->child2);
841   
842   switch (property_id)
843     {
844     case CHILD_PROP_RESIZE:
845       if (child == priv->child1)
846         g_value_set_boolean (value, priv->child1_resize);
847       else
848         g_value_set_boolean (value, priv->child2_resize);
849       break;
850     case CHILD_PROP_SHRINK:
851       if (child == priv->child1)
852         g_value_set_boolean (value, priv->child1_shrink);
853       else
854         g_value_set_boolean (value, priv->child2_shrink);
855       break;
856     default:
857       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
858       break;
859     }
860 }
861
862 static void
863 gtk_paned_finalize (GObject *object)
864 {
865   GtkPaned *paned = GTK_PANED (object);
866   
867   gtk_paned_set_saved_focus (paned, NULL);
868   gtk_paned_set_first_paned (paned, NULL);
869
870   G_OBJECT_CLASS (gtk_paned_parent_class)->finalize (object);
871 }
872
873 static void
874 get_preferred_size_for_size (GtkWidget      *widget,
875                              GtkOrientation  orientation,
876                              gint            size,
877                              gint           *minimum,
878                              gint           *natural)
879 {
880   if (orientation == GTK_ORIENTATION_HORIZONTAL)
881     if (size < 0)
882       gtk_widget_get_preferred_width (widget, minimum, natural);
883     else
884       gtk_widget_get_preferred_width_for_height (widget, size, minimum, natural);
885   else
886     if (size < 0)
887       gtk_widget_get_preferred_height (widget, minimum, natural);
888     else
889       gtk_widget_get_preferred_height_for_width (widget, size, minimum, natural);
890 }
891
892 static void
893 gtk_paned_get_preferred_size (GtkWidget      *widget,
894                               GtkOrientation  orientation,
895                               gint            size,
896                               gint           *minimum,
897                               gint           *natural)
898 {
899   GtkPaned *paned = GTK_PANED (widget);
900   GtkPanedPrivate *priv = paned->priv;
901   gint child_min, child_nat;
902
903   *minimum = *natural = 0;
904
905   if (priv->child1 && gtk_widget_get_visible (priv->child1))
906     {
907       get_preferred_size_for_size (priv->child1, orientation, size, &child_min, &child_nat);
908       *minimum = child_min;
909       *natural = child_nat;
910     }
911
912   if (priv->child2 && gtk_widget_get_visible (priv->child2))
913     {
914       get_preferred_size_for_size (priv->child2, orientation, size, &child_min, &child_nat);
915
916       if (priv->orientation == orientation)
917         {
918           *minimum += child_min;
919           *natural += child_nat;
920         }
921       else
922         {
923           *minimum = MAX (*minimum, child_min);
924           *natural = MAX (*natural, child_nat);
925         }
926     }
927
928   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
929       priv->child2 && gtk_widget_get_visible (priv->child2))
930     {
931       gint handle_size;
932
933       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
934
935       if (priv->orientation == orientation)
936         {
937           *minimum += handle_size;
938           *natural += handle_size;
939         }
940     }
941 }
942
943 static void
944 gtk_paned_get_preferred_width (GtkWidget *widget,
945                                gint      *minimum,
946                                gint      *natural)
947 {
948   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, -1, minimum, natural);
949 }
950
951 static void
952 gtk_paned_get_preferred_height (GtkWidget *widget,
953                                 gint      *minimum,
954                                 gint      *natural)
955 {
956   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, -1, minimum, natural);
957 }
958
959 static void
960 gtk_paned_get_preferred_width_for_height (GtkWidget *widget,
961                                           gint       height,
962                                           gint      *minimum,
963                                           gint      *natural)
964 {
965   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, height, minimum, natural);
966 }
967
968 static void
969 gtk_paned_get_preferred_height_for_width (GtkWidget *widget,
970                                           gint       width,
971                                           gint      *minimum,
972                                           gint      *natural)
973 {
974   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, width, minimum, natural);
975 }
976
977 static void
978 flip_child (GtkWidget     *widget,
979             GtkAllocation *child_pos)
980 {
981   GtkAllocation allocation;
982   gint x, width;
983
984   gtk_widget_get_allocation (widget, &allocation);
985   x = allocation.x;
986   width = allocation.width;
987
988   child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
989 }
990
991 static void
992 gtk_paned_set_child_visible (GtkPaned  *paned,
993                              guint      id,
994                              gboolean   visible)
995 {
996   GtkPanedPrivate *priv = paned->priv;
997   GtkWidget *child;
998
999   child = id == CHILD1 ? priv->child1 : priv->child2;
1000
1001   if (child == NULL)
1002     return;
1003
1004   gtk_widget_set_child_visible (child, visible);
1005
1006   if (gtk_widget_get_mapped (GTK_WIDGET (paned)))
1007     {
1008       GdkWindow *window = id == CHILD1 ? priv->child1_window : priv->child2_window;
1009
1010       if (visible != gdk_window_is_visible (window))
1011         {
1012           if (visible)
1013             gdk_window_show (window);
1014           else
1015             gdk_window_hide (window);
1016         }
1017     }
1018 }
1019
1020 static void
1021 gtk_paned_child_allocate (GtkWidget           *child,
1022                           GdkWindow           *child_window, /* can be NULL */
1023                           const GtkAllocation *window_allocation,
1024                           GtkAllocation       *child_allocation)
1025 {
1026   if (child_window)
1027     gdk_window_move_resize (child_window,
1028                             window_allocation->x, window_allocation->y,
1029                             window_allocation->width, window_allocation->height);
1030
1031   gtk_widget_size_allocate (child, child_allocation);
1032 }
1033
1034 static void
1035 gtk_paned_size_allocate (GtkWidget     *widget,
1036                          GtkAllocation *allocation)
1037 {
1038   GtkPaned *paned = GTK_PANED (widget);
1039   GtkPanedPrivate *priv = paned->priv;
1040
1041   gtk_widget_set_allocation (widget, allocation);
1042
1043   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
1044       priv->child2 && gtk_widget_get_visible (priv->child2))
1045     {
1046       GtkAllocation child1_allocation, window1_allocation;
1047       GtkAllocation child2_allocation, window2_allocation;
1048       GtkAllocation priv_child1_allocation;
1049       GdkRectangle old_handle_pos;
1050       gint handle_size;
1051
1052       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
1053
1054       old_handle_pos = priv->handle_pos;
1055
1056       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1057         {
1058           gint child1_width, child2_width;
1059
1060           gtk_widget_get_preferred_width_for_height (priv->child1,
1061                                                      allocation->height,
1062                                                      &child1_width, NULL);
1063           gtk_widget_get_preferred_width_for_height (priv->child2,
1064                                                      allocation->height,
1065                                                      &child2_width, NULL);
1066
1067           gtk_paned_calc_position (paned,
1068                                    MAX (1, allocation->width - handle_size),
1069                                    child1_width,
1070                                    child2_width);
1071
1072           priv->handle_pos.x = allocation->x + priv->child1_size;
1073           priv->handle_pos.y = allocation->y;
1074           priv->handle_pos.width = handle_size;
1075           priv->handle_pos.height = allocation->height;
1076
1077           window1_allocation.height = window2_allocation.height = allocation->height;
1078           window1_allocation.width = MAX (1, priv->child1_size);
1079           window1_allocation.x = allocation->x;
1080           window1_allocation.y = window2_allocation.y = allocation->y;
1081
1082           window2_allocation.x = window1_allocation.x + priv->child1_size + priv->handle_pos.width;
1083           window2_allocation.width = MAX (1, allocation->x + allocation->width - window2_allocation.x);
1084
1085           if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
1086             {
1087               flip_child (widget, &(window2_allocation));
1088               flip_child (widget, &(window1_allocation));
1089               flip_child (widget, &(priv->handle_pos));
1090             }
1091
1092           child1_allocation.x = child1_allocation.y = 0;
1093           child1_allocation.width = window1_allocation.width;
1094           child1_allocation.height = window1_allocation.height;
1095           if (child1_width > child1_allocation.width)
1096             {
1097               if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_LTR)
1098                 child1_allocation.x -= child1_width - child1_allocation.width;
1099               child1_allocation.width = child1_width;
1100             }
1101
1102           child2_allocation.x = child2_allocation.y = 0;
1103           child2_allocation.width = window2_allocation.width;
1104           child2_allocation.height = window2_allocation.height;
1105           if (child2_width > child2_allocation.width)
1106             {
1107               if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
1108                 child2_allocation.x -= child2_width - child2_allocation.width;
1109               child2_allocation.width = child2_width;
1110             }
1111         }
1112       else
1113         {
1114           gint child1_height, child2_height;
1115
1116           gtk_widget_get_preferred_height_for_width (priv->child1,
1117                                                      allocation->width,
1118                                                      &child1_height, NULL);
1119           gtk_widget_get_preferred_height_for_width (priv->child2,
1120                                                      allocation->width,
1121                                                      &child2_height, NULL);
1122
1123           gtk_paned_calc_position (paned,
1124                                    MAX (1, allocation->height - handle_size),
1125                                    child1_height,
1126                                    child2_height);
1127
1128           priv->handle_pos.x = allocation->x;
1129           priv->handle_pos.y = allocation->y + priv->child1_size;
1130           priv->handle_pos.width = allocation->width;
1131           priv->handle_pos.height = handle_size;
1132
1133           window1_allocation.width = window2_allocation.width = allocation->width;
1134           window1_allocation.height = MAX (1, priv->child1_size);
1135           window1_allocation.x = window2_allocation.x = allocation->x;
1136           window1_allocation.y = allocation->y;
1137
1138           window2_allocation.y = window1_allocation.y + priv->child1_size + priv->handle_pos.height;
1139           window2_allocation.height = MAX (1, allocation->y + allocation->height - window2_allocation.y);
1140
1141           child1_allocation.x = child1_allocation.y = 0;
1142           child1_allocation.width = window1_allocation.width;
1143           child1_allocation.height = window1_allocation.height;
1144           if (child1_height > child1_allocation.height)
1145             {
1146               child1_allocation.y -= child1_height - child1_allocation.height;
1147               child1_allocation.height = child1_height;
1148             }
1149
1150           child2_allocation.x = child2_allocation.y = 0;
1151           child2_allocation.width = window2_allocation.width;
1152           child2_allocation.height = window2_allocation.height;
1153           if (child2_height > child2_allocation.height)
1154             child2_allocation.height = child2_height;
1155         }
1156
1157       if (gtk_widget_get_mapped (widget) &&
1158           (old_handle_pos.x != priv->handle_pos.x ||
1159            old_handle_pos.y != priv->handle_pos.y ||
1160            old_handle_pos.width != priv->handle_pos.width ||
1161            old_handle_pos.height != priv->handle_pos.height))
1162         {
1163           GdkWindow *window;
1164
1165           window = gtk_widget_get_window (widget);
1166           gdk_window_invalidate_rect (window, &old_handle_pos, FALSE);
1167           gdk_window_invalidate_rect (window, &priv->handle_pos, FALSE);
1168         }
1169
1170       if (gtk_widget_get_realized (widget))
1171         {
1172           if (gtk_widget_get_mapped (widget))
1173             gdk_window_show (priv->handle);
1174
1175           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1176             {
1177               gdk_window_move_resize (priv->handle,
1178                                       priv->handle_pos.x,
1179                                       priv->handle_pos.y,
1180                                       handle_size,
1181                                       priv->handle_pos.height);
1182             }
1183           else
1184             {
1185               gdk_window_move_resize (priv->handle,
1186                                       priv->handle_pos.x,
1187                                       priv->handle_pos.y,
1188                                       priv->handle_pos.width,
1189                                       handle_size);
1190             }
1191         }
1192
1193       /* Now allocate the childen, making sure, when resizing not to
1194        * overlap the windows
1195        */
1196       gtk_widget_get_allocation (priv->child1, &priv_child1_allocation);
1197       if (gtk_widget_get_mapped (widget) &&
1198           ((priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1199             priv_child1_allocation.width < child1_allocation.width) ||
1200
1201            (priv->orientation == GTK_ORIENTATION_VERTICAL &&
1202             priv_child1_allocation.height < child1_allocation.height)))
1203         {
1204           gtk_paned_child_allocate (priv->child2,
1205                                     priv->child2_window,
1206                                     &window2_allocation,
1207                                     &child2_allocation);
1208           gtk_paned_child_allocate (priv->child1,
1209                                     priv->child1_window,
1210                                     &window1_allocation,
1211                                     &child1_allocation);
1212         }
1213       else
1214         {
1215           gtk_paned_child_allocate (priv->child1,
1216                                     priv->child1_window,
1217                                     &window1_allocation,
1218                                     &child1_allocation);
1219           gtk_paned_child_allocate (priv->child2,
1220                                     priv->child2_window,
1221                                     &window2_allocation,
1222                                     &child2_allocation);
1223         }
1224     }
1225   else
1226     {
1227       GtkAllocation window_allocation, child_allocation;
1228
1229       if (gtk_widget_get_realized (widget))
1230         gdk_window_hide (priv->handle);
1231
1232       window_allocation.x = allocation->x;
1233       window_allocation.y = allocation->y;
1234       window_allocation.width = allocation->width;
1235       window_allocation.height = allocation->height;
1236       child_allocation.x = child_allocation.y = 0;
1237       child_allocation.width = allocation->width;
1238       child_allocation.height = allocation->height;
1239
1240       if (priv->child1 && gtk_widget_get_visible (priv->child1))
1241         {
1242           gtk_paned_set_child_visible (paned, 0, TRUE);
1243           if (priv->child2)
1244             gtk_paned_set_child_visible (paned, 1, FALSE);
1245
1246           gtk_paned_child_allocate (priv->child1,
1247                                     priv->child1_window,
1248                                     &window_allocation,
1249                                     &child_allocation);
1250         }
1251       else if (priv->child2 && gtk_widget_get_visible (priv->child2))
1252         {
1253           gtk_paned_set_child_visible (paned, 1, TRUE);
1254           if (priv->child1)
1255             gtk_paned_set_child_visible (paned, 0, FALSE);
1256
1257           gtk_paned_child_allocate (priv->child2,
1258                                     priv->child2_window,
1259                                     &window_allocation,
1260                                     &child_allocation);
1261         }
1262       else
1263         {
1264           if (priv->child1)
1265             gtk_paned_set_child_visible (paned, 0, FALSE);
1266           if (priv->child2)
1267             gtk_paned_set_child_visible (paned, 1, FALSE);
1268         }
1269     }
1270 }
1271
1272 static GdkWindow *
1273 gtk_paned_create_child_window (GtkPaned  *paned,
1274                                GtkWidget *child) /* may be NULL */
1275 {
1276   GtkWidget *widget = GTK_WIDGET (paned);
1277   GtkPanedPrivate *priv = paned->priv;
1278   GdkWindow *window;
1279   GdkWindowAttr attributes;
1280   gint attributes_mask;
1281
1282   attributes.window_type = GDK_WINDOW_CHILD;
1283   attributes.wclass = GDK_INPUT_OUTPUT;
1284   attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
1285   if (child)
1286     {
1287       GtkAllocation allocation;
1288       int handle_size;
1289
1290       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
1291
1292       gtk_widget_get_allocation (widget, &allocation);
1293       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1294           child == priv->child2 && priv->child1 &&
1295           gtk_widget_get_visible (priv->child1))
1296         attributes.x = priv->handle_pos.x + handle_size;
1297       else
1298         attributes.x = allocation.x;
1299       if (priv->orientation == GTK_ORIENTATION_VERTICAL &&
1300           child == priv->child2 && priv->child1 &&
1301           gtk_widget_get_visible (priv->child1))
1302         attributes.y = priv->handle_pos.y + handle_size;
1303       else
1304         attributes.y = allocation.y;
1305
1306       gtk_widget_get_allocation (child, &allocation);
1307       attributes.width = allocation.width;
1308       attributes.height = allocation.height;
1309       attributes_mask = GDK_WA_X | GDK_WA_Y;
1310     }
1311   else
1312     {
1313       attributes.width = 1;
1314       attributes.height = 1;
1315       attributes_mask = 0;
1316     }
1317
1318   window = gdk_window_new (gtk_widget_get_window (widget),
1319                            &attributes, attributes_mask);
1320   gdk_window_set_user_data (window, paned);
1321   gtk_style_context_set_background (gtk_widget_get_style_context (widget), window);
1322
1323   if (child)
1324     gtk_widget_set_parent_window (child, window);
1325
1326   return window;
1327 }
1328
1329 static void
1330 gtk_paned_realize (GtkWidget *widget)
1331 {
1332   GtkPaned *paned = GTK_PANED (widget);
1333   GtkPanedPrivate *priv = paned->priv;
1334   GdkWindow *window;
1335   GdkWindowAttr attributes;
1336   gint attributes_mask;
1337
1338   gtk_widget_set_realized (widget, TRUE);
1339
1340   window = gtk_widget_get_parent_window (widget);
1341   gtk_widget_set_window (widget, window);
1342   g_object_ref (window);
1343
1344   attributes.window_type = GDK_WINDOW_CHILD;
1345   attributes.wclass = GDK_INPUT_ONLY;
1346   attributes.x = priv->handle_pos.x;
1347   attributes.y = priv->handle_pos.y;
1348   attributes.width = priv->handle_pos.width;
1349   attributes.height = priv->handle_pos.height;
1350   attributes.event_mask = gtk_widget_get_events (widget);
1351   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1352                             GDK_BUTTON_RELEASE_MASK |
1353                             GDK_ENTER_NOTIFY_MASK |
1354                             GDK_LEAVE_NOTIFY_MASK |
1355                             GDK_POINTER_MOTION_MASK);
1356   attributes_mask = GDK_WA_X | GDK_WA_Y;
1357   if (gtk_widget_is_sensitive (widget))
1358     {
1359       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1360                                                       priv->cursor_type);
1361       attributes_mask |= GDK_WA_CURSOR;
1362     }
1363
1364   priv->handle = gdk_window_new (window,
1365                                  &attributes, attributes_mask);
1366   gdk_window_set_user_data (priv->handle, paned);
1367   if (attributes_mask & GDK_WA_CURSOR)
1368     g_object_unref (attributes.cursor);
1369
1370   priv->child1_window = gtk_paned_create_child_window (paned, priv->child1);
1371   priv->child2_window = gtk_paned_create_child_window (paned, priv->child2);
1372 }
1373
1374 static void
1375 gtk_paned_unrealize (GtkWidget *widget)
1376 {
1377   GtkPaned *paned = GTK_PANED (widget);
1378   GtkPanedPrivate *priv = paned->priv;
1379
1380   if (priv->child2)
1381     gtk_widget_set_parent_window (priv->child2, NULL);
1382   gdk_window_set_user_data (priv->child2_window, NULL);
1383   gdk_window_destroy (priv->child2_window);
1384   priv->child2_window = NULL;
1385
1386   if (priv->child1)
1387     gtk_widget_set_parent_window (priv->child1, NULL);
1388   gdk_window_set_user_data (priv->child1_window, NULL);
1389   gdk_window_destroy (priv->child1_window);
1390   priv->child1_window = NULL;
1391
1392   if (priv->handle)
1393     {
1394       gdk_window_set_user_data (priv->handle, NULL);
1395       gdk_window_destroy (priv->handle);
1396       priv->handle = NULL;
1397     }
1398
1399   gtk_paned_set_last_child1_focus (paned, NULL);
1400   gtk_paned_set_last_child2_focus (paned, NULL);
1401   gtk_paned_set_saved_focus (paned, NULL);
1402   gtk_paned_set_first_paned (paned, NULL);
1403
1404   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unrealize (widget);
1405 }
1406
1407 static void
1408 gtk_paned_map (GtkWidget *widget)
1409 {
1410   GtkPaned *paned = GTK_PANED (widget);
1411   GtkPanedPrivate *priv = paned->priv;
1412
1413   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
1414       priv->child2 && gtk_widget_get_visible (priv->child2))
1415     gdk_window_show (priv->handle);
1416
1417   if (priv->child1 && gtk_widget_get_visible (priv->child1) && gtk_widget_get_child_visible (priv->child1))
1418     gdk_window_show (priv->child1_window);
1419   if (priv->child2 && gtk_widget_get_visible (priv->child2) && gtk_widget_get_child_visible (priv->child2))
1420     gdk_window_show (priv->child2_window);
1421
1422   GTK_WIDGET_CLASS (gtk_paned_parent_class)->map (widget);
1423 }
1424
1425 static void
1426 gtk_paned_unmap (GtkWidget *widget)
1427 {
1428   GtkPaned *paned = GTK_PANED (widget);
1429   GtkPanedPrivate *priv = paned->priv;
1430
1431   gdk_window_hide (priv->handle);
1432   
1433   if (gdk_window_is_visible (priv->child1_window))
1434     gdk_window_hide (priv->child1_window);
1435   if (gdk_window_is_visible (priv->child2_window))
1436     gdk_window_hide (priv->child2_window);
1437
1438   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unmap (widget);
1439 }
1440
1441 static gboolean
1442 gtk_paned_draw (GtkWidget *widget,
1443                 cairo_t   *cr)
1444 {
1445   GtkPaned *paned = GTK_PANED (widget);
1446   GtkPanedPrivate *priv = paned->priv;
1447
1448   if (gtk_cairo_should_draw_window (cr, priv->child1_window))
1449     {
1450       cairo_save (cr);
1451       gtk_cairo_transform_to_window (cr, widget, priv->child1_window);
1452       gtk_render_background (gtk_widget_get_style_context (widget),
1453                              cr,
1454                              0, 0,
1455                              gdk_window_get_width (priv->child1_window),
1456                              gdk_window_get_height (priv->child1_window));
1457       cairo_restore (cr);
1458     }
1459
1460   if (gtk_cairo_should_draw_window (cr, priv->child2_window))
1461     {
1462       cairo_save (cr);
1463       gtk_cairo_transform_to_window (cr, widget, priv->child2_window);
1464       gtk_render_background (gtk_widget_get_style_context (widget),
1465                              cr,
1466                              0, 0,
1467                              gdk_window_get_width (priv->child2_window),
1468                              gdk_window_get_height (priv->child2_window));
1469       cairo_restore (cr);
1470     }
1471
1472   if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)) &&
1473       priv->child1 && gtk_widget_get_visible (priv->child1) &&
1474       priv->child2 && gtk_widget_get_visible (priv->child2))
1475     {
1476       GtkStyleContext *context;
1477       GtkStateFlags state;
1478       GtkAllocation allocation;
1479
1480       gtk_widget_get_allocation (widget, &allocation);
1481       context = gtk_widget_get_style_context (widget);
1482       state = gtk_widget_get_state_flags (widget);
1483
1484       if (gtk_widget_is_focus (widget))
1485         state |= GTK_STATE_FLAG_SELECTED;
1486       if (priv->handle_prelit)
1487         state |= GTK_STATE_FLAG_PRELIGHT;
1488
1489       gtk_style_context_save (context);
1490       gtk_style_context_set_state (context, state);
1491       gtk_style_context_add_class (context, GTK_STYLE_CLASS_PANE_SEPARATOR);
1492       gtk_render_handle (context, cr,
1493                          priv->handle_pos.x - allocation.x,
1494                          priv->handle_pos.y - allocation.y,
1495                          priv->handle_pos.width,
1496                          priv->handle_pos.height);
1497
1498       gtk_style_context_restore (context);
1499     }
1500
1501   /* Chain up to draw children */
1502   GTK_WIDGET_CLASS (gtk_paned_parent_class)->draw (widget, cr);
1503   
1504   return FALSE;
1505 }
1506
1507 static gboolean
1508 is_rtl (GtkPaned *paned)
1509 {
1510   GtkPanedPrivate *priv = paned->priv;
1511
1512   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1513       gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL)
1514     {
1515       return TRUE;
1516     }
1517
1518   return FALSE;
1519 }
1520
1521 static void
1522 update_drag (GtkPaned         *paned,
1523              /* relative to priv->handle */
1524              int               xpos,
1525              int               ypos)
1526 {
1527   GtkPanedPrivate *priv = paned->priv;
1528   GtkAllocation allocation;
1529   GtkWidget *widget = GTK_WIDGET (paned);
1530   gint pos;
1531   gint handle_size;
1532   gint size;
1533   gint x, y;
1534
1535   gdk_window_get_position (priv->handle, &x, &y);
1536   gtk_widget_get_allocation (widget, &allocation);
1537   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1538     {
1539       pos = xpos + x - allocation.x;
1540     }
1541   else
1542     {
1543       pos = ypos + y - allocation.y;
1544     }
1545
1546   pos -= priv->drag_pos;
1547
1548   if (is_rtl (paned))
1549     {
1550       gtk_widget_style_get (widget,
1551                             "handle-size", &handle_size,
1552                             NULL);
1553
1554       size = allocation.width - pos - handle_size;
1555     }
1556   else
1557     {
1558       size = pos;
1559     }
1560
1561   size = CLAMP (size, priv->min_position, priv->max_position);
1562
1563   if (size != priv->child1_size)
1564     gtk_paned_set_position (paned, size);
1565 }
1566
1567 static gboolean
1568 gtk_paned_enter (GtkWidget        *widget,
1569                  GdkEventCrossing *event)
1570 {
1571   GtkPaned *paned = GTK_PANED (widget);
1572   GtkPanedPrivate *priv = paned->priv;
1573
1574   if (priv->in_drag)
1575     update_drag (paned, event->x, event->y);
1576   else
1577     {
1578       priv->handle_prelit = TRUE;
1579       gtk_widget_queue_draw_area (widget,
1580                                   priv->handle_pos.x,
1581                                   priv->handle_pos.y,
1582                                   priv->handle_pos.width,
1583                                   priv->handle_pos.height);
1584     }
1585   
1586   return TRUE;
1587 }
1588
1589 static gboolean
1590 gtk_paned_leave (GtkWidget        *widget,
1591                  GdkEventCrossing *event)
1592 {
1593   GtkPaned *paned = GTK_PANED (widget);
1594   GtkPanedPrivate *priv = paned->priv;
1595
1596   if (priv->in_drag)
1597     update_drag (paned, event->x, event->y);
1598   else
1599     {
1600       priv->handle_prelit = FALSE;
1601       gtk_widget_queue_draw_area (widget,
1602                                   priv->handle_pos.x,
1603                                   priv->handle_pos.y,
1604                                   priv->handle_pos.width,
1605                                   priv->handle_pos.height);
1606     }
1607
1608   return TRUE;
1609 }
1610
1611 static gboolean
1612 gtk_paned_focus (GtkWidget        *widget,
1613                  GtkDirectionType  direction)
1614
1615 {
1616   gboolean retval;
1617   
1618   /* This is a hack, but how can this be done without
1619    * excessive cut-and-paste from gtkcontainer.c?
1620    */
1621
1622   gtk_widget_set_can_focus (widget, FALSE);
1623   retval = GTK_WIDGET_CLASS (gtk_paned_parent_class)->focus (widget, direction);
1624   gtk_widget_set_can_focus (widget, TRUE);
1625
1626   return retval;
1627 }
1628
1629 static gboolean
1630 gtk_paned_button_press (GtkWidget      *widget,
1631                         GdkEventButton *event)
1632 {
1633   GtkPaned *paned = GTK_PANED (widget);
1634   GtkPanedPrivate *priv = paned->priv;
1635
1636   if (!priv->in_drag &&
1637       (event->window == priv->handle) && (event->button == 1))
1638     {
1639       /* We need a server grab here, not gtk_grab_add(), since
1640        * we don't want to pass events on to the widget's children */
1641       if (gdk_device_grab (event->device,
1642                            priv->handle,
1643                            GDK_OWNERSHIP_WINDOW, FALSE,
1644                            GDK_BUTTON1_MOTION_MASK
1645                            | GDK_BUTTON_RELEASE_MASK
1646                            | GDK_ENTER_NOTIFY_MASK
1647                            | GDK_LEAVE_NOTIFY_MASK,
1648                            NULL, event->time) != GDK_GRAB_SUCCESS)
1649         return FALSE;
1650
1651       priv->in_drag = TRUE;
1652       priv->grab_time = event->time;
1653       priv->grab_device = event->device;
1654
1655       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1656         priv->drag_pos = event->x;
1657       else
1658         priv->drag_pos = event->y;
1659
1660       return TRUE;
1661     }
1662
1663   return FALSE;
1664 }
1665
1666 static gboolean
1667 gtk_paned_grab_broken (GtkWidget          *widget,
1668                        GdkEventGrabBroken *event)
1669 {
1670   GtkPaned *paned = GTK_PANED (widget);
1671   GtkPanedPrivate *priv = paned->priv;
1672
1673   priv->in_drag = FALSE;
1674   priv->drag_pos = -1;
1675   priv->position_set = TRUE;
1676
1677   return TRUE;
1678 }
1679
1680 static void
1681 stop_drag (GtkPaned *paned)
1682 {
1683   GtkPanedPrivate *priv = paned->priv;
1684
1685   priv->in_drag = FALSE;
1686   priv->drag_pos = -1;
1687   priv->position_set = TRUE;
1688
1689   gdk_device_ungrab (priv->grab_device,
1690                      priv->grab_time);
1691   priv->grab_device = NULL;
1692 }
1693
1694 static void
1695 gtk_paned_grab_notify (GtkWidget *widget,
1696                        gboolean   was_grabbed)
1697 {
1698   GtkPaned *paned = GTK_PANED (widget);
1699   GtkPanedPrivate *priv = paned->priv;
1700   GdkDevice *grab_device;
1701
1702   grab_device = priv->grab_device;
1703
1704   if (priv->in_drag && grab_device &&
1705       gtk_widget_device_is_shadowed (widget, grab_device))
1706     stop_drag (paned);
1707 }
1708
1709 static void
1710 gtk_paned_state_flags_changed (GtkWidget     *widget,
1711                                GtkStateFlags  previous_state)
1712 {
1713   GtkPaned *paned = GTK_PANED (widget);
1714   GtkPanedPrivate *priv = paned->priv;
1715   GdkCursor *cursor;
1716
1717   if (gtk_widget_get_realized (widget))
1718     {
1719       if (gtk_widget_is_sensitive (widget))
1720         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1721                                              priv->cursor_type);
1722       else
1723         cursor = NULL;
1724
1725       gdk_window_set_cursor (priv->handle, cursor);
1726
1727       if (cursor)
1728         g_object_unref (cursor);
1729     }
1730 }
1731
1732 static gboolean
1733 gtk_paned_button_release (GtkWidget      *widget,
1734                           GdkEventButton *event)
1735 {
1736   GtkPaned *paned = GTK_PANED (widget);
1737   GtkPanedPrivate *priv = paned->priv;
1738
1739   if (priv->in_drag && (event->button == 1))
1740     {
1741       stop_drag (paned);
1742
1743       return TRUE;
1744     }
1745
1746   return FALSE;
1747 }
1748
1749 static gboolean
1750 gtk_paned_motion (GtkWidget      *widget,
1751                   GdkEventMotion *event)
1752 {
1753   GtkPaned *paned = GTK_PANED (widget);
1754   GtkPanedPrivate *priv = paned->priv;
1755
1756   if (priv->in_drag)
1757     {
1758       update_drag (paned, event->x, event->y);
1759       return TRUE;
1760     }
1761   
1762   return FALSE;
1763 }
1764
1765 /**
1766  * gtk_paned_new:
1767  * @orientation: the paned's orientation.
1768  *
1769  * Creates a new #GtkPaned widget.
1770  *
1771  * Return value: a new #GtkPaned.
1772  *
1773  * Since: 3.0
1774  **/
1775 GtkWidget *
1776 gtk_paned_new (GtkOrientation orientation)
1777 {
1778   return g_object_new (GTK_TYPE_PANED,
1779                        "orientation", orientation,
1780                        NULL);
1781 }
1782
1783 /**
1784  * gtk_paned_add1:
1785  * @paned: a paned widget
1786  * @child: the child to add
1787  *
1788  * Adds a child to the top or left pane with default parameters. This is
1789  * equivalent to
1790  * <literal>gtk_paned_pack1 (paned, child, FALSE, TRUE)</literal>.
1791  */
1792 void
1793 gtk_paned_add1 (GtkPaned  *paned,
1794                 GtkWidget *widget)
1795 {
1796   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
1797 }
1798
1799 /**
1800  * gtk_paned_add2:
1801  * @paned: a paned widget
1802  * @child: the child to add
1803  *
1804  * Adds a child to the bottom or right pane with default parameters. This
1805  * is equivalent to
1806  * <literal>gtk_paned_pack2 (paned, child, TRUE, TRUE)</literal>.
1807  */
1808 void
1809 gtk_paned_add2 (GtkPaned  *paned,
1810                 GtkWidget *widget)
1811 {
1812   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
1813 }
1814
1815 /**
1816  * gtk_paned_pack1:
1817  * @paned: a paned widget
1818  * @child: the child to add
1819  * @resize: should this child expand when the paned widget is resized.
1820  * @shrink: can this child be made smaller than its requisition.
1821  *
1822  * Adds a child to the top or left pane.
1823  */
1824 void
1825 gtk_paned_pack1 (GtkPaned  *paned,
1826                  GtkWidget *child,
1827                  gboolean   resize,
1828                  gboolean   shrink)
1829 {
1830   GtkPanedPrivate *priv;
1831
1832   g_return_if_fail (GTK_IS_PANED (paned));
1833   g_return_if_fail (GTK_IS_WIDGET (child));
1834
1835   priv = paned->priv;
1836
1837   if (!priv->child1)
1838     {
1839       priv->child1 = child;
1840       priv->child1_resize = resize;
1841       priv->child1_shrink = shrink;
1842
1843       gtk_widget_set_parent_window (child, priv->child1_window);
1844       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1845     }
1846 }
1847
1848 /**
1849  * gtk_paned_pack2:
1850  * @paned: a paned widget
1851  * @child: the child to add
1852  * @resize: should this child expand when the paned widget is resized.
1853  * @shrink: can this child be made smaller than its requisition.
1854  *
1855  * Adds a child to the bottom or right pane.
1856  */
1857 void
1858 gtk_paned_pack2 (GtkPaned  *paned,
1859                  GtkWidget *child,
1860                  gboolean   resize,
1861                  gboolean   shrink)
1862 {
1863   GtkPanedPrivate *priv;
1864
1865   g_return_if_fail (GTK_IS_PANED (paned));
1866   g_return_if_fail (GTK_IS_WIDGET (child));
1867
1868   priv = paned->priv;
1869
1870   if (!priv->child2)
1871     {
1872       priv->child2 = child;
1873       priv->child2_resize = resize;
1874       priv->child2_shrink = shrink;
1875
1876       gtk_widget_set_parent_window (child, priv->child2_window);
1877       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1878     }
1879 }
1880
1881
1882 static void
1883 gtk_paned_add (GtkContainer *container,
1884                GtkWidget    *widget)
1885 {
1886   GtkPanedPrivate *priv;
1887   GtkPaned *paned;
1888
1889   g_return_if_fail (GTK_IS_PANED (container));
1890
1891   paned = GTK_PANED (container);
1892   priv = paned->priv;
1893
1894   if (!priv->child1)
1895     gtk_paned_add1 (paned, widget);
1896   else if (!priv->child2)
1897     gtk_paned_add2 (paned, widget);
1898   else
1899     g_warning ("GtkPaned cannot have more than 2 children\n");
1900 }
1901
1902 static void
1903 gtk_paned_remove (GtkContainer *container,
1904                   GtkWidget    *widget)
1905 {
1906   GtkPaned *paned = GTK_PANED (container);
1907   GtkPanedPrivate *priv = paned->priv;
1908   gboolean was_visible;
1909
1910   was_visible = gtk_widget_get_visible (widget);
1911
1912   if (priv->child1 == widget)
1913     {
1914       if (priv->child1_window && gdk_window_is_visible (priv->child1_window))
1915         gdk_window_hide (priv->child1_window);
1916
1917       gtk_widget_unparent (widget);
1918
1919       priv->child1 = NULL;
1920
1921       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1922         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1923     }
1924   else if (priv->child2 == widget)
1925     {
1926       if (priv->child2_window && gdk_window_is_visible (priv->child2_window))
1927         gdk_window_hide (priv->child2_window);
1928
1929       gtk_widget_unparent (widget);
1930
1931       priv->child2 = NULL;
1932
1933       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1934         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1935     }
1936 }
1937
1938 static void
1939 gtk_paned_forall (GtkContainer *container,
1940                   gboolean      include_internals,
1941                   GtkCallback   callback,
1942                   gpointer      callback_data)
1943 {
1944   GtkPanedPrivate *priv;
1945   GtkPaned *paned;
1946
1947   g_return_if_fail (callback != NULL);
1948
1949   paned = GTK_PANED (container);
1950   priv = paned->priv;
1951
1952   if (priv->child1)
1953     (*callback) (priv->child1, callback_data);
1954   if (priv->child2)
1955     (*callback) (priv->child2, callback_data);
1956 }
1957
1958 /**
1959  * gtk_paned_get_position:
1960  * @paned: a #GtkPaned widget
1961  * 
1962  * Obtains the position of the divider between the two panes.
1963  * 
1964  * Return value: position of the divider
1965  **/
1966 gint
1967 gtk_paned_get_position (GtkPaned  *paned)
1968 {
1969   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1970
1971   return paned->priv->child1_size;
1972 }
1973
1974 /**
1975  * gtk_paned_set_position:
1976  * @paned: a #GtkPaned widget
1977  * @position: pixel position of divider, a negative value means that the position
1978  *            is unset.
1979  * 
1980  * Sets the position of the divider between the two panes.
1981  **/
1982 void
1983 gtk_paned_set_position (GtkPaned *paned,
1984                         gint      position)
1985 {
1986   GtkPanedPrivate *priv;
1987   GObject *object;
1988
1989   g_return_if_fail (GTK_IS_PANED (paned));
1990
1991   priv = paned->priv;
1992
1993   if (priv->child1_size == position)
1994     return;
1995
1996   object = G_OBJECT (paned);
1997   
1998   if (position >= 0)
1999     {
2000       /* We don't clamp here - the assumption is that
2001        * if the total allocation changes at the same time
2002        * as the position, the position set is with reference
2003        * to the new total size. If only the position changes,
2004        * then clamping will occur in gtk_paned_calc_position()
2005        */
2006
2007       priv->child1_size = position;
2008       priv->position_set = TRUE;
2009     }
2010   else
2011     {
2012       priv->position_set = FALSE;
2013     }
2014
2015   g_object_freeze_notify (object);
2016   g_object_notify (object, "position");
2017   g_object_notify (object, "position-set");
2018   g_object_thaw_notify (object);
2019
2020   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
2021
2022 #ifdef G_OS_WIN32
2023   /* Hacky work-around for bug #144269 */
2024   if (priv->child2 != NULL)
2025     {
2026       gtk_widget_queue_draw (priv->child2);
2027     }
2028 #endif
2029 }
2030
2031 /**
2032  * gtk_paned_get_child1:
2033  * @paned: a #GtkPaned widget
2034  * 
2035  * Obtains the first child of the paned widget.
2036  * 
2037  * Return value: (transfer none): first child, or %NULL if it is not set.
2038  *
2039  * Since: 2.4
2040  **/
2041 GtkWidget *
2042 gtk_paned_get_child1 (GtkPaned *paned)
2043 {
2044   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2045
2046   return paned->priv->child1;
2047 }
2048
2049 /**
2050  * gtk_paned_get_child2:
2051  * @paned: a #GtkPaned widget
2052  * 
2053  * Obtains the second child of the paned widget.
2054  * 
2055  * Return value: (transfer none): second child, or %NULL if it is not set.
2056  *
2057  * Since: 2.4
2058  **/
2059 GtkWidget *
2060 gtk_paned_get_child2 (GtkPaned *paned)
2061 {
2062   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2063
2064   return paned->priv->child2;
2065 }
2066
2067 static void
2068 gtk_paned_calc_position (GtkPaned *paned,
2069                          gint      allocation,
2070                          gint      child1_req,
2071                          gint      child2_req)
2072 {
2073   GtkPanedPrivate *priv = paned->priv;
2074   gint old_position;
2075   gint old_min_position;
2076   gint old_max_position;
2077
2078   old_position = priv->child1_size;
2079   old_min_position = priv->min_position;
2080   old_max_position = priv->max_position;
2081
2082   priv->min_position = priv->child1_shrink ? 0 : child1_req;
2083
2084   priv->max_position = allocation;
2085   if (!priv->child2_shrink)
2086     priv->max_position = MAX (1, priv->max_position - child2_req);
2087   priv->max_position = MAX (priv->min_position, priv->max_position);
2088
2089   if (!priv->position_set)
2090     {
2091       if (priv->child1_resize && !priv->child2_resize)
2092         priv->child1_size = MAX (0, allocation - child2_req);
2093       else if (!priv->child1_resize && priv->child2_resize)
2094         priv->child1_size = child1_req;
2095       else if (child1_req + child2_req != 0)
2096         priv->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
2097       else
2098         priv->child1_size = allocation * 0.5 + 0.5;
2099     }
2100   else
2101     {
2102       /* If the position was set before the initial allocation.
2103        * (priv->last_allocation <= 0) just clamp it and leave it.
2104        */
2105       if (priv->last_allocation > 0)
2106         {
2107           if (priv->child1_resize && !priv->child2_resize)
2108             priv->child1_size += allocation - priv->last_allocation;
2109           else if (!(!priv->child1_resize && priv->child2_resize))
2110             priv->child1_size = allocation * ((gdouble) priv->child1_size / (priv->last_allocation)) + 0.5;
2111         }
2112     }
2113
2114   priv->child1_size = CLAMP (priv->child1_size,
2115                               priv->min_position,
2116                               priv->max_position);
2117
2118   if (priv->child1)
2119     gtk_paned_set_child_visible (paned, 0, priv->child1_size != 0);
2120   
2121   if (priv->child2)
2122     gtk_paned_set_child_visible (paned, 1, priv->child1_size != allocation); 
2123
2124   g_object_freeze_notify (G_OBJECT (paned));
2125   if (priv->child1_size != old_position)
2126     g_object_notify (G_OBJECT (paned), "position");
2127   if (priv->min_position != old_min_position)
2128     g_object_notify (G_OBJECT (paned), "min-position");
2129   if (priv->max_position != old_max_position)
2130     g_object_notify (G_OBJECT (paned), "max-position");
2131   g_object_thaw_notify (G_OBJECT (paned));
2132
2133   priv->last_allocation = allocation;
2134 }
2135
2136 static void
2137 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
2138 {
2139   GtkPanedPrivate *priv = paned->priv;
2140
2141   if (priv->saved_focus)
2142     g_object_remove_weak_pointer (G_OBJECT (priv->saved_focus),
2143                                   (gpointer *)&(priv->saved_focus));
2144
2145   priv->saved_focus = widget;
2146
2147   if (priv->saved_focus)
2148     g_object_add_weak_pointer (G_OBJECT (priv->saved_focus),
2149                                (gpointer *)&(priv->saved_focus));
2150 }
2151
2152 static void
2153 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
2154 {
2155   GtkPanedPrivate *priv = paned->priv;
2156
2157   if (priv->first_paned)
2158     g_object_remove_weak_pointer (G_OBJECT (priv->first_paned),
2159                                   (gpointer *)&(priv->first_paned));
2160
2161   priv->first_paned = first_paned;
2162
2163   if (priv->first_paned)
2164     g_object_add_weak_pointer (G_OBJECT (priv->first_paned),
2165                                (gpointer *)&(priv->first_paned));
2166 }
2167
2168 static void
2169 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
2170 {
2171   GtkPanedPrivate *priv = paned->priv;
2172
2173   if (priv->last_child1_focus)
2174     g_object_remove_weak_pointer (G_OBJECT (priv->last_child1_focus),
2175                                   (gpointer *)&(priv->last_child1_focus));
2176
2177   priv->last_child1_focus = widget;
2178
2179   if (priv->last_child1_focus)
2180     g_object_add_weak_pointer (G_OBJECT (priv->last_child1_focus),
2181                                (gpointer *)&(priv->last_child1_focus));
2182 }
2183
2184 static void
2185 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
2186 {
2187   GtkPanedPrivate *priv = paned->priv;
2188
2189   if (priv->last_child2_focus)
2190     g_object_remove_weak_pointer (G_OBJECT (priv->last_child2_focus),
2191                                   (gpointer *)&(priv->last_child2_focus));
2192
2193   priv->last_child2_focus = widget;
2194
2195   if (priv->last_child2_focus)
2196     g_object_add_weak_pointer (G_OBJECT (priv->last_child2_focus),
2197                                (gpointer *)&(priv->last_child2_focus));
2198 }
2199
2200 static GtkWidget *
2201 paned_get_focus_widget (GtkPaned *paned)
2202 {
2203   GtkWidget *toplevel;
2204
2205   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2206   if (gtk_widget_is_toplevel (toplevel))
2207     return gtk_window_get_focus (GTK_WINDOW (toplevel));
2208
2209   return NULL;
2210 }
2211
2212 static void
2213 gtk_paned_set_focus_child (GtkContainer *container,
2214                            GtkWidget    *focus_child)
2215 {
2216   GtkPaned *paned;
2217   GtkPanedPrivate *priv;
2218   GtkWidget *container_focus_child;
2219
2220   g_return_if_fail (GTK_IS_PANED (container));
2221
2222   paned = GTK_PANED (container);
2223   priv = paned->priv;
2224
2225   if (focus_child == NULL)
2226     {
2227       GtkWidget *last_focus;
2228       GtkWidget *w;
2229       
2230       last_focus = paned_get_focus_widget (paned);
2231
2232       if (last_focus)
2233         {
2234           /* If there is one or more paned widgets between us and the
2235            * focus widget, we want the topmost of those as last_focus
2236            */
2237           for (w = last_focus; w != GTK_WIDGET (paned); w = gtk_widget_get_parent (w))
2238             if (GTK_IS_PANED (w))
2239               last_focus = w;
2240
2241           container_focus_child = gtk_container_get_focus_child (container);
2242           if (container_focus_child == priv->child1)
2243             gtk_paned_set_last_child1_focus (paned, last_focus);
2244           else if (container_focus_child == priv->child2)
2245             gtk_paned_set_last_child2_focus (paned, last_focus);
2246         }
2247     }
2248
2249   if (GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child)
2250     GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child (container, focus_child);
2251 }
2252
2253 static void
2254 gtk_paned_get_cycle_chain (GtkPaned          *paned,
2255                            GtkDirectionType   direction,
2256                            GList            **widgets)
2257 {
2258   GtkPanedPrivate *priv = paned->priv;
2259   GtkContainer *container = GTK_CONTAINER (paned);
2260   GtkWidget *ancestor = NULL;
2261   GtkWidget *focus_child;
2262   GtkWidget *parent;
2263   GtkWidget *widget = GTK_WIDGET (paned);
2264   GList *temp_list = NULL;
2265   GList *list;
2266
2267   if (priv->in_recursion)
2268     return;
2269
2270   g_assert (widgets != NULL);
2271
2272   if (priv->last_child1_focus &&
2273       !gtk_widget_is_ancestor (priv->last_child1_focus, widget))
2274     {
2275       gtk_paned_set_last_child1_focus (paned, NULL);
2276     }
2277
2278   if (priv->last_child2_focus &&
2279       !gtk_widget_is_ancestor (priv->last_child2_focus, widget))
2280     {
2281       gtk_paned_set_last_child2_focus (paned, NULL);
2282     }
2283
2284   parent = gtk_widget_get_parent (widget);
2285   if (parent)
2286     ancestor = gtk_widget_get_ancestor (parent, GTK_TYPE_PANED);
2287
2288   /* The idea here is that temp_list is a list of widgets we want to cycle
2289    * to. The list is prioritized so that the first element is our first
2290    * choice, the next our second, and so on.
2291    *
2292    * We can't just use g_list_reverse(), because we want to try
2293    * priv->last_child?_focus before priv->child?, both when we
2294    * are going forward and backward.
2295    */
2296   focus_child = gtk_container_get_focus_child (container);
2297   if (direction == GTK_DIR_TAB_FORWARD)
2298     {
2299       if (focus_child == priv->child1)
2300         {
2301           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2302           temp_list = g_list_append (temp_list, priv->child2);
2303           temp_list = g_list_append (temp_list, ancestor);
2304         }
2305       else if (focus_child == priv->child2)
2306         {
2307           temp_list = g_list_append (temp_list, ancestor);
2308           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2309           temp_list = g_list_append (temp_list, priv->child1);
2310         }
2311       else
2312         {
2313           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2314           temp_list = g_list_append (temp_list, priv->child1);
2315           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2316           temp_list = g_list_append (temp_list, priv->child2);
2317           temp_list = g_list_append (temp_list, ancestor);
2318         }
2319     }
2320   else
2321     {
2322       if (focus_child == priv->child1)
2323         {
2324           temp_list = g_list_append (temp_list, ancestor);
2325           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2326           temp_list = g_list_append (temp_list, priv->child2);
2327         }
2328       else if (focus_child == priv->child2)
2329         {
2330           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2331           temp_list = g_list_append (temp_list, priv->child1);
2332           temp_list = g_list_append (temp_list, ancestor);
2333         }
2334       else
2335         {
2336           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2337           temp_list = g_list_append (temp_list, priv->child2);
2338           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2339           temp_list = g_list_append (temp_list, priv->child1);
2340           temp_list = g_list_append (temp_list, ancestor);
2341         }
2342     }
2343
2344   /* Walk the list and expand all the paned widgets. */
2345   for (list = temp_list; list != NULL; list = list->next)
2346     {
2347       GtkWidget *widget = list->data;
2348
2349       if (widget)
2350         {
2351           if (GTK_IS_PANED (widget))
2352             {
2353               priv->in_recursion = TRUE;
2354               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
2355               priv->in_recursion = FALSE;
2356             }
2357           else
2358             {
2359               *widgets = g_list_append (*widgets, widget);
2360             }
2361         }
2362     }
2363
2364   g_list_free (temp_list);
2365 }
2366
2367 static gboolean
2368 gtk_paned_cycle_child_focus (GtkPaned *paned,
2369                              gboolean  reversed)
2370 {
2371   GList *cycle_chain = NULL;
2372   GList *list;
2373   
2374   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
2375
2376   /* ignore f6 if the handle is focused */
2377   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2378     return TRUE;
2379   
2380   /* we can't just let the event propagate up the hierarchy,
2381    * because the paned will want to cycle focus _unless_ an
2382    * ancestor paned handles the event
2383    */
2384   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
2385
2386   for (list = cycle_chain; list != NULL; list = list->next)
2387     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
2388       break;
2389
2390   g_list_free (cycle_chain);
2391   
2392   return TRUE;
2393 }
2394
2395 static void
2396 get_child_panes (GtkWidget  *widget,
2397                  GList     **panes)
2398 {
2399   if (!widget || !gtk_widget_get_realized (widget))
2400     return;
2401
2402   if (GTK_IS_PANED (widget))
2403     {
2404       GtkPaned *paned = GTK_PANED (widget);
2405       GtkPanedPrivate *priv = paned->priv;
2406
2407       get_child_panes (priv->child1, panes);
2408       *panes = g_list_prepend (*panes, widget);
2409       get_child_panes (priv->child2, panes);
2410     }
2411   else if (GTK_IS_CONTAINER (widget))
2412     {
2413       gtk_container_forall (GTK_CONTAINER (widget),
2414                             (GtkCallback)get_child_panes, panes);
2415     }
2416 }
2417
2418 static GList *
2419 get_all_panes (GtkPaned *paned)
2420 {
2421   GtkPaned *topmost = NULL;
2422   GList *result = NULL;
2423   GtkWidget *w;
2424
2425   for (w = GTK_WIDGET (paned); w != NULL; w = gtk_widget_get_parent (w))
2426     {
2427       if (GTK_IS_PANED (w))
2428         topmost = GTK_PANED (w);
2429     }
2430
2431   g_assert (topmost);
2432
2433   get_child_panes (GTK_WIDGET (topmost), &result);
2434
2435   return g_list_reverse (result);
2436 }
2437
2438 static void
2439 gtk_paned_find_neighbours (GtkPaned  *paned,
2440                            GtkPaned **next,
2441                            GtkPaned **prev)
2442 {
2443   GList *all_panes;
2444   GList *this_link;
2445
2446   all_panes = get_all_panes (paned);
2447   g_assert (all_panes);
2448
2449   this_link = g_list_find (all_panes, paned);
2450
2451   g_assert (this_link);
2452   
2453   if (this_link->next)
2454     *next = this_link->next->data;
2455   else
2456     *next = all_panes->data;
2457
2458   if (this_link->prev)
2459     *prev = this_link->prev->data;
2460   else
2461     *prev = g_list_last (all_panes)->data;
2462
2463   g_list_free (all_panes);
2464 }
2465
2466 static gboolean
2467 gtk_paned_move_handle (GtkPaned      *paned,
2468                        GtkScrollType  scroll)
2469 {
2470   GtkPanedPrivate *priv = paned->priv;
2471
2472   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2473     {
2474       gint old_position;
2475       gint new_position;
2476       gint increment;
2477       
2478       enum {
2479         SINGLE_STEP_SIZE = 1,
2480         PAGE_STEP_SIZE   = 75
2481       };
2482       
2483       new_position = old_position = gtk_paned_get_position (paned);
2484       increment = 0;
2485       
2486       switch (scroll)
2487         {
2488         case GTK_SCROLL_STEP_LEFT:
2489         case GTK_SCROLL_STEP_UP:
2490         case GTK_SCROLL_STEP_BACKWARD:
2491           increment = - SINGLE_STEP_SIZE;
2492           break;
2493           
2494         case GTK_SCROLL_STEP_RIGHT:
2495         case GTK_SCROLL_STEP_DOWN:
2496         case GTK_SCROLL_STEP_FORWARD:
2497           increment = SINGLE_STEP_SIZE;
2498           break;
2499           
2500         case GTK_SCROLL_PAGE_LEFT:
2501         case GTK_SCROLL_PAGE_UP:
2502         case GTK_SCROLL_PAGE_BACKWARD:
2503           increment = - PAGE_STEP_SIZE;
2504           break;
2505           
2506         case GTK_SCROLL_PAGE_RIGHT:
2507         case GTK_SCROLL_PAGE_DOWN:
2508         case GTK_SCROLL_PAGE_FORWARD:
2509           increment = PAGE_STEP_SIZE;
2510           break;
2511           
2512         case GTK_SCROLL_START:
2513           new_position = priv->min_position;
2514           break;
2515           
2516         case GTK_SCROLL_END:
2517           new_position = priv->max_position;
2518           break;
2519
2520         default:
2521           break;
2522         }
2523
2524       if (increment)
2525         {
2526           if (is_rtl (paned))
2527             increment = -increment;
2528           
2529           new_position = old_position + increment;
2530         }
2531       
2532       new_position = CLAMP (new_position, priv->min_position, priv->max_position);
2533       
2534       if (old_position != new_position)
2535         gtk_paned_set_position (paned, new_position);
2536
2537       return TRUE;
2538     }
2539
2540   return FALSE;
2541 }
2542
2543 static void
2544 gtk_paned_restore_focus (GtkPaned *paned)
2545 {
2546   GtkPanedPrivate *priv = paned->priv;
2547
2548   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2549     {
2550       if (priv->saved_focus &&
2551           gtk_widget_get_sensitive (priv->saved_focus))
2552         {
2553           gtk_widget_grab_focus (priv->saved_focus);
2554         }
2555       else
2556         {
2557           /* the saved focus is somehow not available for focusing,
2558            * try
2559            *   1) tabbing into the paned widget
2560            * if that didn't work,
2561            *   2) unset focus for the window if there is one
2562            */
2563           
2564           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
2565             {
2566               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2567               
2568               if (GTK_IS_WINDOW (toplevel))
2569                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
2570             }
2571         }
2572       
2573       gtk_paned_set_saved_focus (paned, NULL);
2574       gtk_paned_set_first_paned (paned, NULL);
2575     }
2576 }
2577
2578 static gboolean
2579 gtk_paned_accept_position (GtkPaned *paned)
2580 {
2581   GtkPanedPrivate *priv = paned->priv;
2582
2583   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2584     {
2585       priv->original_position = -1;
2586       gtk_paned_restore_focus (paned);
2587
2588       return TRUE;
2589     }
2590
2591   return FALSE;
2592 }
2593
2594
2595 static gboolean
2596 gtk_paned_cancel_position (GtkPaned *paned)
2597 {
2598   GtkPanedPrivate *priv = paned->priv;
2599
2600   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2601     {
2602       if (priv->original_position != -1)
2603         {
2604           gtk_paned_set_position (paned, priv->original_position);
2605           priv->original_position = -1;
2606         }
2607
2608       gtk_paned_restore_focus (paned);
2609       return TRUE;
2610     }
2611
2612   return FALSE;
2613 }
2614
2615 static gboolean
2616 gtk_paned_cycle_handle_focus (GtkPaned *paned,
2617                               gboolean  reversed)
2618 {
2619   GtkPanedPrivate *priv = paned->priv;
2620   GtkPaned *next, *prev;
2621
2622   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2623     {
2624       GtkPaned *focus = NULL;
2625
2626       if (!priv->first_paned)
2627         {
2628           /* The first_pane has disappeared. As an ad-hoc solution,
2629            * we make the currently focused paned the first_paned. To the
2630            * user this will seem like the paned cycling has been reset.
2631            */
2632           
2633           gtk_paned_set_first_paned (paned, paned);
2634         }
2635       
2636       gtk_paned_find_neighbours (paned, &next, &prev);
2637
2638       if (reversed && prev &&
2639           prev != paned && paned != priv->first_paned)
2640         {
2641           focus = prev;
2642         }
2643       else if (!reversed && next &&
2644                next != paned && next != priv->first_paned)
2645         {
2646           focus = next;
2647         }
2648       else
2649         {
2650           gtk_paned_accept_position (paned);
2651           return TRUE;
2652         }
2653
2654       g_assert (focus);
2655       
2656       gtk_paned_set_saved_focus (focus, priv->saved_focus);
2657       gtk_paned_set_first_paned (focus, priv->first_paned);
2658       
2659       gtk_paned_set_saved_focus (paned, NULL);
2660       gtk_paned_set_first_paned (paned, NULL);
2661       
2662       gtk_widget_grab_focus (GTK_WIDGET (focus));
2663       
2664       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
2665         {
2666           priv->original_position = -1;
2667           focus->priv->original_position = gtk_paned_get_position (focus);
2668         }
2669     }
2670   else
2671     {
2672       GtkContainer *container = GTK_CONTAINER (paned);
2673       GtkPaned *focus;
2674       GtkPaned *first;
2675       GtkPaned *prev, *next;
2676       GtkWidget *toplevel;
2677       GtkWidget *focus_child;
2678
2679       gtk_paned_find_neighbours (paned, &next, &prev);
2680       focus_child = gtk_container_get_focus_child (container);
2681
2682       if (focus_child == priv->child1)
2683         {
2684           if (reversed)
2685             {
2686               focus = prev;
2687               first = paned;
2688             }
2689           else
2690             {
2691               focus = paned;
2692               first = paned;
2693             }
2694         }
2695       else if (focus_child == priv->child2)
2696         {
2697           if (reversed)
2698             {
2699               focus = paned;
2700               first = next;
2701             }
2702           else
2703             {
2704               focus = next;
2705               first = next;
2706             }
2707         }
2708       else
2709         {
2710           /* Focus is not inside this paned, and we don't have focus.
2711            * Presumably this happened because the application wants us
2712            * to start keyboard navigating.
2713            */
2714           focus = paned;
2715
2716           if (reversed)
2717             first = paned;
2718           else
2719             first = next;
2720         }
2721
2722       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2723
2724       if (GTK_IS_WINDOW (toplevel))
2725         gtk_paned_set_saved_focus (focus, gtk_window_get_focus (GTK_WINDOW (toplevel)));
2726       gtk_paned_set_first_paned (focus, first);
2727       focus->priv->original_position = gtk_paned_get_position (focus);
2728
2729       gtk_widget_grab_focus (GTK_WIDGET (focus));
2730    }
2731
2732   return TRUE;
2733 }
2734
2735 static gboolean
2736 gtk_paned_toggle_handle_focus (GtkPaned *paned)
2737 {
2738   /* This function/signal has the wrong name. It is called when you
2739    * press Tab or Shift-Tab and what we do is act as if
2740    * the user pressed Return and then Tab or Shift-Tab
2741    */
2742   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2743     gtk_paned_accept_position (paned);
2744
2745   return FALSE;
2746 }
2747
2748 /**
2749  * gtk_paned_get_handle_window:
2750  * @paned: a #GtkPaned
2751  *
2752  * Returns the #GdkWindow of the handle. This function is
2753  * useful when handling button or motion events because it
2754  * enables the callback to distinguish between the window
2755  * of the paned, a child and the handle.
2756  *
2757  * Return value: (transfer none): the paned's handle window.
2758  *
2759  * Since: 2.20
2760  **/
2761 GdkWindow *
2762 gtk_paned_get_handle_window (GtkPaned *paned)
2763 {
2764   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2765
2766   return paned->priv->handle;
2767 }