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