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