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