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