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