]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
argh, actually call the newly added private gtk_paned_calc_position()
[~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_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 | GTK_CAN_FOCUS);
563
564   /* We only need to redraw when the handle position moves, which is
565    * independent of the overall allocation of the GtkPaned
566    */
567   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (paned), FALSE);
568
569   paned->priv = G_TYPE_INSTANCE_GET_PRIVATE (paned, GTK_TYPE_PANED, GtkPanedPrivate);
570
571   paned->priv->orientation = GTK_ORIENTATION_HORIZONTAL;
572   paned->cursor_type = GDK_SB_H_DOUBLE_ARROW;
573
574   paned->child1 = NULL;
575   paned->child2 = NULL;
576   paned->handle = NULL;
577   paned->xor_gc = NULL;
578   paned->cursor_type = GDK_CROSS;
579   
580   paned->handle_pos.width = 5;
581   paned->handle_pos.height = 5;
582   paned->position_set = FALSE;
583   paned->last_allocation = -1;
584   paned->in_drag = FALSE;
585
586   paned->last_child1_focus = NULL;
587   paned->last_child2_focus = NULL;
588   paned->in_recursion = FALSE;
589   paned->handle_prelit = FALSE;
590   paned->original_position = -1;
591   
592   paned->handle_pos.x = -1;
593   paned->handle_pos.y = -1;
594
595   paned->drag_pos = -1;
596 }
597
598 static void
599 gtk_paned_set_property (GObject        *object,
600                         guint           prop_id,
601                         const GValue   *value,
602                         GParamSpec     *pspec)
603 {
604   GtkPaned *paned = GTK_PANED (object);
605
606   switch (prop_id)
607     {
608     case PROP_ORIENTATION:
609       paned->priv->orientation = g_value_get_enum (value);
610       paned->orientation = !paned->priv->orientation;
611
612       if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
613         paned->cursor_type = GDK_SB_H_DOUBLE_ARROW;
614       else
615         paned->cursor_type = GDK_SB_V_DOUBLE_ARROW;
616
617       /* state_changed updates the cursor */
618       gtk_paned_state_changed (GTK_WIDGET (paned), GTK_WIDGET (paned)->state);
619       gtk_widget_queue_resize (GTK_WIDGET (paned));
620       break;
621     case PROP_POSITION:
622       gtk_paned_set_position (paned, g_value_get_int (value));
623       break;
624     case PROP_POSITION_SET:
625       paned->position_set = g_value_get_boolean (value);
626       gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
627       break;
628     default:
629       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
630       break;
631     }
632 }
633
634 static void
635 gtk_paned_get_property (GObject        *object,
636                         guint           prop_id,
637                         GValue         *value,
638                         GParamSpec     *pspec)
639 {
640   GtkPaned *paned = GTK_PANED (object);
641
642   switch (prop_id)
643     {
644     case PROP_ORIENTATION:
645       g_value_set_enum (value, paned->priv->orientation);
646       break;
647     case PROP_POSITION:
648       g_value_set_int (value, paned->child1_size);
649       break;
650     case PROP_POSITION_SET:
651       g_value_set_boolean (value, paned->position_set);
652       break;
653     case PROP_MIN_POSITION:
654       g_value_set_int (value, paned->min_position);
655       break;
656     case PROP_MAX_POSITION:
657       g_value_set_int (value, paned->max_position);
658       break;
659     default:
660       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
661       break;
662     }
663 }
664
665 static void
666 gtk_paned_set_child_property (GtkContainer    *container,
667                               GtkWidget       *child,
668                               guint            property_id,
669                               const GValue    *value,
670                               GParamSpec      *pspec)
671 {
672   GtkPaned *paned = GTK_PANED (container);
673   gboolean old_value, new_value;
674
675   g_assert (child == paned->child1 || child == paned->child2);
676
677   new_value = g_value_get_boolean (value);
678   switch (property_id)
679     {
680     case CHILD_PROP_RESIZE:
681       if (child == paned->child1)
682         {
683           old_value = paned->child1_resize;
684           paned->child1_resize = new_value;
685         }
686       else
687         {
688           old_value = paned->child2_resize;
689           paned->child2_resize = new_value;
690         }
691       break;
692     case CHILD_PROP_SHRINK:
693       if (child == paned->child1)
694         {
695           old_value = paned->child1_shrink;
696           paned->child1_shrink = new_value;
697         }
698       else
699         {
700           old_value = paned->child2_shrink;
701           paned->child2_shrink = new_value;
702         }
703       break;
704     default:
705       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
706       old_value = -1; /* quiet gcc */
707       break;
708     }
709   if (old_value != new_value)
710     gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
711 }
712
713 static void
714 gtk_paned_get_child_property (GtkContainer *container,
715                               GtkWidget    *child,
716                               guint         property_id,
717                               GValue       *value,
718                               GParamSpec   *pspec)
719 {
720   GtkPaned *paned = GTK_PANED (container);
721
722   g_assert (child == paned->child1 || child == paned->child2);
723   
724   switch (property_id)
725     {
726     case CHILD_PROP_RESIZE:
727       if (child == paned->child1)
728         g_value_set_boolean (value, paned->child1_resize);
729       else
730         g_value_set_boolean (value, paned->child2_resize);
731       break;
732     case CHILD_PROP_SHRINK:
733       if (child == paned->child1)
734         g_value_set_boolean (value, paned->child1_shrink);
735       else
736         g_value_set_boolean (value, paned->child2_shrink);
737       break;
738     default:
739       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
740       break;
741     }
742 }
743
744 static void
745 gtk_paned_finalize (GObject *object)
746 {
747   GtkPaned *paned = GTK_PANED (object);
748   
749   gtk_paned_set_saved_focus (paned, NULL);
750   gtk_paned_set_first_paned (paned, NULL);
751
752   G_OBJECT_CLASS (gtk_paned_parent_class)->finalize (object);
753 }
754
755 static void
756 gtk_paned_size_request (GtkWidget      *widget,
757                         GtkRequisition *requisition)
758 {
759   GtkPaned *paned = GTK_PANED (widget);
760   GtkRequisition child_requisition;
761
762   requisition->width = 0;
763   requisition->height = 0;
764
765   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
766     {
767       gtk_widget_size_request (paned->child1, &child_requisition);
768
769       requisition->height = child_requisition.height;
770       requisition->width = child_requisition.width;
771     }
772
773   if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
774     {
775       gtk_widget_size_request (paned->child2, &child_requisition);
776
777       if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
778         {
779           requisition->height = MAX (requisition->height,
780                                      child_requisition.height);
781           requisition->width += child_requisition.width;
782         }
783       else
784         {
785           requisition->width = MAX (requisition->width,
786                                     child_requisition.width);
787           requisition->height += child_requisition.height;
788         }
789     }
790
791   requisition->width += GTK_CONTAINER (paned)->border_width * 2;
792   requisition->height += GTK_CONTAINER (paned)->border_width * 2;
793
794   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
795       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
796     {
797       gint handle_size;
798
799       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
800
801       if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
802         requisition->width += handle_size;
803       else
804         requisition->height += handle_size;
805     }
806 }
807
808 static void
809 flip_child (GtkWidget     *widget,
810             GtkAllocation *child_pos)
811 {
812   gint x     = widget->allocation.x;
813   gint width = widget->allocation.width;
814
815   child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
816 }
817
818 static void
819 gtk_paned_size_allocate (GtkWidget     *widget,
820                          GtkAllocation *allocation)
821 {
822   GtkPaned *paned = GTK_PANED (widget);
823   gint border_width = GTK_CONTAINER (paned)->border_width;
824
825   widget->allocation = *allocation;
826
827   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
828       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
829     {
830       GtkRequisition child1_requisition;
831       GtkRequisition child2_requisition;
832       GtkAllocation child1_allocation;
833       GtkAllocation child2_allocation;
834       GdkRectangle old_handle_pos;
835       gint handle_size;
836
837       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
838
839       gtk_widget_get_child_requisition (paned->child1, &child1_requisition);
840       gtk_widget_get_child_requisition (paned->child2, &child2_requisition);
841
842       old_handle_pos = paned->handle_pos;
843
844       if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
845         {
846           gtk_paned_calc_position (paned,
847                                    MAX (1, widget->allocation.width
848                                         - handle_size
849                                         - 2 * border_width),
850                                    child1_requisition.width,
851                                    child2_requisition.width);
852
853           paned->handle_pos.x = widget->allocation.x + paned->child1_size + border_width;
854           paned->handle_pos.y = widget->allocation.y + border_width;
855           paned->handle_pos.width = handle_size;
856           paned->handle_pos.height = MAX (1, widget->allocation.height - 2 * border_width);
857
858           child1_allocation.height = child2_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
859           child1_allocation.width = MAX (1, paned->child1_size);
860           child1_allocation.x = widget->allocation.x + border_width;
861           child1_allocation.y = child2_allocation.y = widget->allocation.y + border_width;
862
863           child2_allocation.x = child1_allocation.x + paned->child1_size + paned->handle_pos.width;
864           child2_allocation.width = MAX (1, widget->allocation.x + widget->allocation.width - child2_allocation.x - border_width);
865
866           if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
867             {
868               flip_child (widget, &(child2_allocation));
869               flip_child (widget, &(child1_allocation));
870               flip_child (widget, &(paned->handle_pos));
871             }
872         }
873       else
874         {
875           gtk_paned_calc_position (paned,
876                                    MAX (1, widget->allocation.height
877                                         - handle_size
878                                         - 2 * border_width),
879                                    child1_requisition.height,
880                                    child2_requisition.height);
881
882           paned->handle_pos.x = widget->allocation.x + border_width;
883           paned->handle_pos.y = widget->allocation.y + paned->child1_size + border_width;
884           paned->handle_pos.width = MAX (1, (gint) widget->allocation.width - 2 * border_width);
885           paned->handle_pos.height = handle_size;
886
887           child1_allocation.width = child2_allocation.width = MAX (1, (gint) allocation->width - border_width * 2);
888           child1_allocation.height = MAX (1, paned->child1_size);
889           child1_allocation.x = child2_allocation.x = widget->allocation.x + border_width;
890           child1_allocation.y = widget->allocation.y + border_width;
891
892           child2_allocation.y = child1_allocation.y + paned->child1_size + paned->handle_pos.height;
893           child2_allocation.height = MAX (1, widget->allocation.y + widget->allocation.height - child2_allocation.y - border_width);
894         }
895
896       if (GTK_WIDGET_MAPPED (widget) &&
897           (old_handle_pos.x != paned->handle_pos.x ||
898            old_handle_pos.y != paned->handle_pos.y ||
899            old_handle_pos.width != paned->handle_pos.width ||
900            old_handle_pos.height != paned->handle_pos.height))
901         {
902           gdk_window_invalidate_rect (widget->window, &old_handle_pos, FALSE);
903           gdk_window_invalidate_rect (widget->window, &paned->handle_pos, FALSE);
904         }
905
906       if (GTK_WIDGET_REALIZED (widget))
907         {
908           if (GTK_WIDGET_MAPPED (widget))
909             gdk_window_show (paned->handle);
910
911           if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
912             {
913               gdk_window_move_resize (paned->handle,
914                                       paned->handle_pos.x,
915                                       paned->handle_pos.y,
916                                       handle_size,
917                                       paned->handle_pos.height);
918             }
919           else
920             {
921               gdk_window_move_resize (paned->handle,
922                                       paned->handle_pos.x,
923                                       paned->handle_pos.y,
924                                       paned->handle_pos.width,
925                                       handle_size);
926             }
927         }
928
929       /* Now allocate the childen, making sure, when resizing not to
930        * overlap the windows
931        */
932       if (GTK_WIDGET_MAPPED (widget) &&
933
934           ((paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
935             paned->child1->allocation.width < child1_allocation.width) ||
936
937            (paned->priv->orientation == GTK_ORIENTATION_VERTICAL &&
938             paned->child1->allocation.height < child1_allocation.height)))
939         {
940           gtk_widget_size_allocate (paned->child2, &child2_allocation);
941           gtk_widget_size_allocate (paned->child1, &child1_allocation);
942         }
943       else
944         {
945           gtk_widget_size_allocate (paned->child1, &child1_allocation);
946           gtk_widget_size_allocate (paned->child2, &child2_allocation);
947         }
948     }
949   else
950     {
951       GtkAllocation child_allocation;
952
953       if (GTK_WIDGET_REALIZED (widget))
954         gdk_window_hide (paned->handle);
955
956       if (paned->child1)
957         gtk_widget_set_child_visible (paned->child1, TRUE);
958       if (paned->child2)
959         gtk_widget_set_child_visible (paned->child2, TRUE);
960
961       child_allocation.x = widget->allocation.x + border_width;
962       child_allocation.y = widget->allocation.y + border_width;
963       child_allocation.width = MAX (1, allocation->width - 2 * border_width);
964       child_allocation.height = MAX (1, allocation->height - 2 * border_width);
965
966       if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
967         gtk_widget_size_allocate (paned->child1, &child_allocation);
968       else if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
969         gtk_widget_size_allocate (paned->child2, &child_allocation);
970     }
971 }
972
973 static void
974 gtk_paned_realize (GtkWidget *widget)
975 {
976   GtkPaned *paned;
977   GdkWindowAttr attributes;
978   gint attributes_mask;
979
980   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
981   paned = GTK_PANED (widget);
982
983   widget->window = gtk_widget_get_parent_window (widget);
984   g_object_ref (widget->window);
985   
986   attributes.window_type = GDK_WINDOW_CHILD;
987   attributes.wclass = GDK_INPUT_ONLY;
988   attributes.x = paned->handle_pos.x;
989   attributes.y = paned->handle_pos.y;
990   attributes.width = paned->handle_pos.width;
991   attributes.height = paned->handle_pos.height;
992   attributes.event_mask = gtk_widget_get_events (widget);
993   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
994                             GDK_BUTTON_RELEASE_MASK |
995                             GDK_ENTER_NOTIFY_MASK |
996                             GDK_LEAVE_NOTIFY_MASK |
997                             GDK_POINTER_MOTION_MASK |
998                             GDK_POINTER_MOTION_HINT_MASK);
999   attributes_mask = GDK_WA_X | GDK_WA_Y;
1000   if (GTK_WIDGET_IS_SENSITIVE (widget))
1001     {
1002       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1003                                                       paned->cursor_type);
1004       attributes_mask |= GDK_WA_CURSOR;
1005     }
1006
1007   paned->handle = gdk_window_new (widget->window,
1008                                   &attributes, attributes_mask);
1009   gdk_window_set_user_data (paned->handle, paned);
1010   if (attributes_mask & GDK_WA_CURSOR)
1011     gdk_cursor_unref (attributes.cursor);
1012
1013   widget->style = gtk_style_attach (widget->style, widget->window);
1014
1015   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
1016       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
1017     gdk_window_show (paned->handle);
1018 }
1019
1020 static void
1021 gtk_paned_unrealize (GtkWidget *widget)
1022 {
1023   GtkPaned *paned = GTK_PANED (widget);
1024
1025   if (paned->xor_gc)
1026     {
1027       g_object_unref (paned->xor_gc);
1028       paned->xor_gc = NULL;
1029     }
1030
1031   if (paned->handle)
1032     {
1033       gdk_window_set_user_data (paned->handle, NULL);
1034       gdk_window_destroy (paned->handle);
1035       paned->handle = NULL;
1036     }
1037
1038   gtk_paned_set_last_child1_focus (paned, NULL);
1039   gtk_paned_set_last_child2_focus (paned, NULL);
1040   gtk_paned_set_saved_focus (paned, NULL);
1041   gtk_paned_set_first_paned (paned, NULL);
1042
1043   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unrealize (widget);
1044 }
1045
1046 static void
1047 gtk_paned_map (GtkWidget *widget)
1048 {
1049   GtkPaned *paned = GTK_PANED (widget);
1050
1051   gdk_window_show (paned->handle);
1052
1053   GTK_WIDGET_CLASS (gtk_paned_parent_class)->map (widget);
1054 }
1055
1056 static void
1057 gtk_paned_unmap (GtkWidget *widget)
1058 {
1059   GtkPaned *paned = GTK_PANED (widget);
1060     
1061   gdk_window_hide (paned->handle);
1062
1063   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unmap (widget);
1064 }
1065
1066 static gboolean
1067 gtk_paned_expose (GtkWidget      *widget,
1068                   GdkEventExpose *event)
1069 {
1070   GtkPaned *paned = GTK_PANED (widget);
1071
1072   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
1073       paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
1074       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
1075     {
1076       GtkStateType state;
1077       
1078       if (gtk_widget_is_focus (widget))
1079         state = GTK_STATE_SELECTED;
1080       else if (paned->handle_prelit)
1081         state = GTK_STATE_PRELIGHT;
1082       else
1083         state = GTK_WIDGET_STATE (widget);
1084       
1085       gtk_paint_handle (widget->style, widget->window,
1086                         state, GTK_SHADOW_NONE,
1087                         &paned->handle_pos, widget, "paned",
1088                         paned->handle_pos.x, paned->handle_pos.y,
1089                         paned->handle_pos.width, paned->handle_pos.height,
1090                         !paned->priv->orientation);
1091     }
1092
1093   /* Chain up to draw children */
1094   GTK_WIDGET_CLASS (gtk_paned_parent_class)->expose_event (widget, event);
1095   
1096   return FALSE;
1097 }
1098
1099 static gboolean
1100 is_rtl (GtkPaned *paned)
1101 {
1102   if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1103       gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL)
1104     {
1105       return TRUE;
1106     }
1107
1108   return FALSE;
1109 }
1110
1111 static void
1112 update_drag (GtkPaned *paned)
1113 {
1114   gint pos;
1115   gint handle_size;
1116   gint size;
1117
1118   if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1119     gtk_widget_get_pointer (GTK_WIDGET (paned), &pos, NULL);
1120   else
1121     gtk_widget_get_pointer (GTK_WIDGET (paned), NULL, &pos);
1122
1123   pos -= paned->drag_pos;
1124
1125   if (is_rtl (paned))
1126     {
1127       gtk_widget_style_get (GTK_WIDGET (paned),
1128                             "handle-size", &handle_size,
1129                             NULL);
1130       
1131       size = GTK_WIDGET (paned)->allocation.width - pos - handle_size;
1132     }
1133   else
1134     {
1135       size = pos;
1136     }
1137
1138   size -= GTK_CONTAINER (paned)->border_width;
1139   
1140   size = CLAMP (size, paned->min_position, paned->max_position);
1141
1142   if (size != paned->child1_size)
1143     gtk_paned_set_position (paned, size);
1144 }
1145
1146 static gboolean
1147 gtk_paned_enter (GtkWidget        *widget,
1148                  GdkEventCrossing *event)
1149 {
1150   GtkPaned *paned = GTK_PANED (widget);
1151   
1152   if (paned->in_drag)
1153     update_drag (paned);
1154   else
1155     {
1156       paned->handle_prelit = TRUE;
1157       gtk_widget_queue_draw_area (widget,
1158                                   paned->handle_pos.x,
1159                                   paned->handle_pos.y,
1160                                   paned->handle_pos.width,
1161                                   paned->handle_pos.height);
1162     }
1163   
1164   return TRUE;
1165 }
1166
1167 static gboolean
1168 gtk_paned_leave (GtkWidget        *widget,
1169                  GdkEventCrossing *event)
1170 {
1171   GtkPaned *paned = GTK_PANED (widget);
1172   
1173   if (paned->in_drag)
1174     update_drag (paned);
1175   else
1176     {
1177       paned->handle_prelit = FALSE;
1178       gtk_widget_queue_draw_area (widget,
1179                                   paned->handle_pos.x,
1180                                   paned->handle_pos.y,
1181                                   paned->handle_pos.width,
1182                                   paned->handle_pos.height);
1183     }
1184
1185   return TRUE;
1186 }
1187
1188 static gboolean
1189 gtk_paned_focus (GtkWidget        *widget,
1190                  GtkDirectionType  direction)
1191
1192 {
1193   gboolean retval;
1194   
1195   /* This is a hack, but how can this be done without
1196    * excessive cut-and-paste from gtkcontainer.c?
1197    */
1198
1199   GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
1200   retval = GTK_WIDGET_CLASS (gtk_paned_parent_class)->focus (widget, direction);
1201   GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
1202
1203   return retval;
1204 }
1205
1206 static gboolean
1207 gtk_paned_button_press (GtkWidget      *widget,
1208                         GdkEventButton *event)
1209 {
1210   GtkPaned *paned = GTK_PANED (widget);
1211
1212   if (!paned->in_drag &&
1213       (event->window == paned->handle) && (event->button == 1))
1214     {
1215       /* We need a server grab here, not gtk_grab_add(), since
1216        * we don't want to pass events on to the widget's children */
1217       if (gdk_pointer_grab (paned->handle, FALSE,
1218                             GDK_POINTER_MOTION_HINT_MASK
1219                             | GDK_BUTTON1_MOTION_MASK
1220                             | GDK_BUTTON_RELEASE_MASK
1221                             | GDK_ENTER_NOTIFY_MASK
1222                             | GDK_LEAVE_NOTIFY_MASK,
1223                             NULL, NULL,
1224                             event->time) != GDK_GRAB_SUCCESS)
1225         return FALSE;
1226
1227       paned->in_drag = TRUE;
1228       paned->priv->grab_time = event->time;
1229
1230       if (paned->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1231         paned->drag_pos = event->x;
1232       else
1233         paned->drag_pos = event->y;
1234
1235       return TRUE;
1236     }
1237
1238   return FALSE;
1239 }
1240
1241 static gboolean
1242 gtk_paned_grab_broken (GtkWidget          *widget,
1243                        GdkEventGrabBroken *event)
1244 {
1245   GtkPaned *paned = GTK_PANED (widget);
1246
1247   paned->in_drag = FALSE;
1248   paned->drag_pos = -1;
1249   paned->position_set = TRUE;
1250
1251   return TRUE;
1252 }
1253
1254 static void
1255 stop_drag (GtkPaned *paned)
1256 {
1257   paned->in_drag = FALSE;
1258   paned->drag_pos = -1;
1259   paned->position_set = TRUE;
1260   gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (paned)),
1261                               paned->priv->grab_time);
1262 }
1263
1264 static void
1265 gtk_paned_grab_notify (GtkWidget *widget,
1266                        gboolean   was_grabbed)
1267 {
1268   GtkPaned *paned = GTK_PANED (widget);
1269
1270   if (!was_grabbed && paned->in_drag)
1271     stop_drag (paned);
1272 }
1273
1274 static void
1275 gtk_paned_state_changed (GtkWidget    *widget,
1276                          GtkStateType  previous_state)
1277 {
1278   GtkPaned *paned = GTK_PANED (widget);
1279   GdkCursor *cursor;
1280
1281   if (GTK_WIDGET_REALIZED (paned))
1282     {
1283       if (GTK_WIDGET_IS_SENSITIVE (widget))
1284         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1285                                              paned->cursor_type); 
1286       else
1287         cursor = NULL;
1288
1289       gdk_window_set_cursor (paned->handle, cursor);
1290
1291       if (cursor)
1292         gdk_cursor_unref (cursor);
1293     }
1294 }
1295
1296 static gboolean
1297 gtk_paned_button_release (GtkWidget      *widget,
1298                           GdkEventButton *event)
1299 {
1300   GtkPaned *paned = GTK_PANED (widget);
1301
1302   if (paned->in_drag && (event->button == 1))
1303     {
1304       stop_drag (paned);
1305
1306       return TRUE;
1307     }
1308
1309   return FALSE;
1310 }
1311
1312 static gboolean
1313 gtk_paned_motion (GtkWidget      *widget,
1314                   GdkEventMotion *event)
1315 {
1316   GtkPaned *paned = GTK_PANED (widget);
1317   
1318   if (paned->in_drag)
1319     {
1320       update_drag (paned);
1321       return TRUE;
1322     }
1323   
1324   return FALSE;
1325 }
1326
1327 /**
1328  * gtk_paned_new:
1329  * @orientation: the paned's orientation.
1330  *
1331  * Creates a new #GtkPaned widget.
1332  *
1333  * Return value: a new #GtkPaned.
1334  *
1335  * Since: 2.16
1336  **/
1337 GtkWidget *
1338 gtk_paned_new (GtkOrientation orientation)
1339 {
1340   return g_object_new (GTK_TYPE_PANED,
1341                        "orientation", orientation,
1342                        NULL);
1343 }
1344
1345 void
1346 gtk_paned_add1 (GtkPaned  *paned,
1347                 GtkWidget *widget)
1348 {
1349   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
1350 }
1351
1352 void
1353 gtk_paned_add2 (GtkPaned  *paned,
1354                 GtkWidget *widget)
1355 {
1356   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
1357 }
1358
1359 void
1360 gtk_paned_pack1 (GtkPaned  *paned,
1361                  GtkWidget *child,
1362                  gboolean   resize,
1363                  gboolean   shrink)
1364 {
1365   g_return_if_fail (GTK_IS_PANED (paned));
1366   g_return_if_fail (GTK_IS_WIDGET (child));
1367
1368   if (!paned->child1)
1369     {
1370       paned->child1 = child;
1371       paned->child1_resize = resize;
1372       paned->child1_shrink = shrink;
1373
1374       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1375     }
1376 }
1377
1378 void
1379 gtk_paned_pack2 (GtkPaned  *paned,
1380                  GtkWidget *child,
1381                  gboolean   resize,
1382                  gboolean   shrink)
1383 {
1384   g_return_if_fail (GTK_IS_PANED (paned));
1385   g_return_if_fail (GTK_IS_WIDGET (child));
1386
1387   if (!paned->child2)
1388     {
1389       paned->child2 = child;
1390       paned->child2_resize = resize;
1391       paned->child2_shrink = shrink;
1392
1393       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1394     }
1395 }
1396
1397
1398 static void
1399 gtk_paned_add (GtkContainer *container,
1400                GtkWidget    *widget)
1401 {
1402   GtkPaned *paned;
1403
1404   g_return_if_fail (GTK_IS_PANED (container));
1405
1406   paned = GTK_PANED (container);
1407
1408   if (!paned->child1)
1409     gtk_paned_add1 (paned, widget);
1410   else if (!paned->child2)
1411     gtk_paned_add2 (paned, widget);
1412   else
1413     g_warning ("GtkPaned cannot have more than 2 children\n");
1414 }
1415
1416 static void
1417 gtk_paned_remove (GtkContainer *container,
1418                   GtkWidget    *widget)
1419 {
1420   GtkPaned *paned;
1421   gboolean was_visible;
1422
1423   paned = GTK_PANED (container);
1424   was_visible = GTK_WIDGET_VISIBLE (widget);
1425
1426   if (paned->child1 == widget)
1427     {
1428       gtk_widget_unparent (widget);
1429
1430       paned->child1 = NULL;
1431
1432       if (was_visible && GTK_WIDGET_VISIBLE (container))
1433         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1434     }
1435   else if (paned->child2 == widget)
1436     {
1437       gtk_widget_unparent (widget);
1438
1439       paned->child2 = NULL;
1440
1441       if (was_visible && GTK_WIDGET_VISIBLE (container))
1442         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1443     }
1444 }
1445
1446 static void
1447 gtk_paned_forall (GtkContainer *container,
1448                   gboolean      include_internals,
1449                   GtkCallback   callback,
1450                   gpointer      callback_data)
1451 {
1452   GtkPaned *paned;
1453
1454   g_return_if_fail (callback != NULL);
1455
1456   paned = GTK_PANED (container);
1457
1458   if (paned->child1)
1459     (*callback) (paned->child1, callback_data);
1460   if (paned->child2)
1461     (*callback) (paned->child2, callback_data);
1462 }
1463
1464 /**
1465  * gtk_paned_get_position:
1466  * @paned: a #GtkPaned widget
1467  * 
1468  * Obtains the position of the divider between the two panes.
1469  * 
1470  * Return value: position of the divider
1471  **/
1472 gint
1473 gtk_paned_get_position (GtkPaned  *paned)
1474 {
1475   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1476
1477   return paned->child1_size;
1478 }
1479
1480 /**
1481  * gtk_paned_set_position:
1482  * @paned: a #GtkPaned widget
1483  * @position: pixel position of divider, a negative value means that the position
1484  *            is unset.
1485  * 
1486  * Sets the position of the divider between the two panes.
1487  **/
1488 void
1489 gtk_paned_set_position (GtkPaned *paned,
1490                         gint      position)
1491 {
1492   GObject *object;
1493   
1494   g_return_if_fail (GTK_IS_PANED (paned));
1495
1496   object = G_OBJECT (paned);
1497   
1498   if (position >= 0)
1499     {
1500       /* We don't clamp here - the assumption is that
1501        * if the total allocation changes at the same time
1502        * as the position, the position set is with reference
1503        * to the new total size. If only the position changes,
1504        * then clamping will occur in gtk_paned_calc_position()
1505        */
1506
1507       paned->child1_size = position;
1508       paned->position_set = TRUE;
1509     }
1510   else
1511     {
1512       paned->position_set = FALSE;
1513     }
1514
1515   g_object_freeze_notify (object);
1516   g_object_notify (object, "position");
1517   g_object_notify (object, "position-set");
1518   g_object_thaw_notify (object);
1519
1520   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
1521
1522 #ifdef G_OS_WIN32
1523   /* Hacky work-around for bug #144269 */
1524   if (paned->child2 != NULL)
1525     {
1526       gtk_widget_queue_draw (paned->child2);
1527     }
1528 #endif
1529 }
1530
1531 /**
1532  * gtk_paned_get_child1:
1533  * @paned: a #GtkPaned widget
1534  * 
1535  * Obtains the first child of the paned widget.
1536  * 
1537  * Return value: first child, or %NULL if it is not set.
1538  *
1539  * Since: 2.4
1540  **/
1541 GtkWidget *
1542 gtk_paned_get_child1 (GtkPaned *paned)
1543 {
1544   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1545
1546   return paned->child1;
1547 }
1548
1549 /**
1550  * gtk_paned_get_child2:
1551  * @paned: a #GtkPaned widget
1552  * 
1553  * Obtains the second child of the paned widget.
1554  * 
1555  * Return value: second child, or %NULL if it is not set.
1556  *
1557  * Since: 2.4
1558  **/
1559 GtkWidget *
1560 gtk_paned_get_child2 (GtkPaned *paned)
1561 {
1562   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1563
1564   return paned->child2;
1565 }
1566
1567 void
1568 gtk_paned_compute_position (GtkPaned *paned,
1569                             gint      allocation,
1570                             gint      child1_req,
1571                             gint      child2_req)
1572 {
1573   g_return_if_fail (GTK_IS_PANED (paned));
1574
1575   gtk_paned_calc_position (paned, allocation, child1_req, child2_req);
1576 }
1577
1578 static void
1579 gtk_paned_calc_position (GtkPaned *paned,
1580                          gint      allocation,
1581                          gint      child1_req,
1582                          gint      child2_req)
1583 {
1584   gint old_position;
1585   gint old_min_position;
1586   gint old_max_position;
1587
1588   old_position = paned->child1_size;
1589   old_min_position = paned->min_position;
1590   old_max_position = paned->max_position;
1591
1592   paned->min_position = paned->child1_shrink ? 0 : child1_req;
1593
1594   paned->max_position = allocation;
1595   if (!paned->child2_shrink)
1596     paned->max_position = MAX (1, paned->max_position - child2_req);
1597   paned->max_position = MAX (paned->min_position, paned->max_position);
1598
1599   if (!paned->position_set)
1600     {
1601       if (paned->child1_resize && !paned->child2_resize)
1602         paned->child1_size = MAX (0, allocation - child2_req);
1603       else if (!paned->child1_resize && paned->child2_resize)
1604         paned->child1_size = child1_req;
1605       else if (child1_req + child2_req != 0)
1606         paned->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
1607       else
1608         paned->child1_size = allocation * 0.5 + 0.5;
1609     }
1610   else
1611     {
1612       /* If the position was set before the initial allocation.
1613        * (paned->last_allocation <= 0) just clamp it and leave it.
1614        */
1615       if (paned->last_allocation > 0)
1616         {
1617           if (paned->child1_resize && !paned->child2_resize)
1618             paned->child1_size += allocation - paned->last_allocation;
1619           else if (!(!paned->child1_resize && paned->child2_resize))
1620             paned->child1_size = allocation * ((gdouble) paned->child1_size / (paned->last_allocation)) + 0.5;
1621         }
1622     }
1623
1624   paned->child1_size = CLAMP (paned->child1_size,
1625                               paned->min_position,
1626                               paned->max_position);
1627
1628   if (paned->child1)
1629     gtk_widget_set_child_visible (paned->child1, paned->child1_size != 0);
1630   
1631   if (paned->child2)
1632     gtk_widget_set_child_visible (paned->child2, paned->child1_size != allocation); 
1633
1634   g_object_freeze_notify (G_OBJECT (paned));
1635   if (paned->child1_size != old_position)
1636     g_object_notify (G_OBJECT (paned), "position");
1637   if (paned->min_position != old_min_position)
1638     g_object_notify (G_OBJECT (paned), "min-position");
1639   if (paned->max_position != old_max_position)
1640     g_object_notify (G_OBJECT (paned), "max-position");
1641   g_object_thaw_notify (G_OBJECT (paned));
1642
1643   paned->last_allocation = allocation;
1644 }
1645
1646 static void
1647 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1648 {
1649   if (paned->priv->saved_focus)
1650     g_object_remove_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1651                                   (gpointer *)&(paned->priv->saved_focus));
1652
1653   paned->priv->saved_focus = widget;
1654
1655   if (paned->priv->saved_focus)
1656     g_object_add_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1657                                (gpointer *)&(paned->priv->saved_focus));
1658 }
1659
1660 static void
1661 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1662 {
1663   if (paned->priv->first_paned)
1664     g_object_remove_weak_pointer (G_OBJECT (paned->priv->first_paned),
1665                                   (gpointer *)&(paned->priv->first_paned));
1666
1667   paned->priv->first_paned = first_paned;
1668
1669   if (paned->priv->first_paned)
1670     g_object_add_weak_pointer (G_OBJECT (paned->priv->first_paned),
1671                                (gpointer *)&(paned->priv->first_paned));
1672 }
1673
1674 static void
1675 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1676 {
1677   if (paned->last_child1_focus)
1678     g_object_remove_weak_pointer (G_OBJECT (paned->last_child1_focus),
1679                                   (gpointer *)&(paned->last_child1_focus));
1680
1681   paned->last_child1_focus = widget;
1682
1683   if (paned->last_child1_focus)
1684     g_object_add_weak_pointer (G_OBJECT (paned->last_child1_focus),
1685                                (gpointer *)&(paned->last_child1_focus));
1686 }
1687
1688 static void
1689 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1690 {
1691   if (paned->last_child2_focus)
1692     g_object_remove_weak_pointer (G_OBJECT (paned->last_child2_focus),
1693                                   (gpointer *)&(paned->last_child2_focus));
1694
1695   paned->last_child2_focus = widget;
1696
1697   if (paned->last_child2_focus)
1698     g_object_add_weak_pointer (G_OBJECT (paned->last_child2_focus),
1699                                (gpointer *)&(paned->last_child2_focus));
1700 }
1701
1702 static GtkWidget *
1703 paned_get_focus_widget (GtkPaned *paned)
1704 {
1705   GtkWidget *toplevel;
1706
1707   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1708   if (GTK_WIDGET_TOPLEVEL (toplevel))
1709     return GTK_WINDOW (toplevel)->focus_widget;
1710
1711   return NULL;
1712 }
1713
1714 static void
1715 gtk_paned_set_focus_child (GtkContainer *container,
1716                            GtkWidget    *focus_child)
1717 {
1718   GtkPaned *paned;
1719   
1720   g_return_if_fail (GTK_IS_PANED (container));
1721
1722   paned = GTK_PANED (container);
1723  
1724   if (focus_child == NULL)
1725     {
1726       GtkWidget *last_focus;
1727       GtkWidget *w;
1728       
1729       last_focus = paned_get_focus_widget (paned);
1730
1731       if (last_focus)
1732         {
1733           /* If there is one or more paned widgets between us and the
1734            * focus widget, we want the topmost of those as last_focus
1735            */
1736           for (w = last_focus; w != GTK_WIDGET (paned); w = w->parent)
1737             if (GTK_IS_PANED (w))
1738               last_focus = w;
1739           
1740           if (container->focus_child == paned->child1)
1741             gtk_paned_set_last_child1_focus (paned, last_focus);
1742           else if (container->focus_child == paned->child2)
1743             gtk_paned_set_last_child2_focus (paned, last_focus);
1744         }
1745     }
1746
1747   if (GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child)
1748     GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child (container, focus_child);
1749 }
1750
1751 static void
1752 gtk_paned_get_cycle_chain (GtkPaned          *paned,
1753                            GtkDirectionType   direction,
1754                            GList            **widgets)
1755 {
1756   GtkContainer *container = GTK_CONTAINER (paned);
1757   GtkWidget *ancestor = NULL;
1758   GList *temp_list = NULL;
1759   GList *list;
1760
1761   if (paned->in_recursion)
1762     return;
1763
1764   g_assert (widgets != NULL);
1765
1766   if (paned->last_child1_focus &&
1767       !gtk_widget_is_ancestor (paned->last_child1_focus, GTK_WIDGET (paned)))
1768     {
1769       gtk_paned_set_last_child1_focus (paned, NULL);
1770     }
1771
1772   if (paned->last_child2_focus &&
1773       !gtk_widget_is_ancestor (paned->last_child2_focus, GTK_WIDGET (paned)))
1774     {
1775       gtk_paned_set_last_child2_focus (paned, NULL);
1776     }
1777
1778   if (GTK_WIDGET (paned)->parent)
1779     ancestor = gtk_widget_get_ancestor (GTK_WIDGET (paned)->parent, GTK_TYPE_PANED);
1780
1781   /* The idea here is that temp_list is a list of widgets we want to cycle
1782    * to. The list is prioritized so that the first element is our first
1783    * choice, the next our second, and so on.
1784    *
1785    * We can't just use g_list_reverse(), because we want to try
1786    * paned->last_child?_focus before paned->child?, both when we
1787    * are going forward and backward.
1788    */
1789   if (direction == GTK_DIR_TAB_FORWARD)
1790     {
1791       if (container->focus_child == paned->child1)
1792         {
1793           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1794           temp_list = g_list_append (temp_list, paned->child2);
1795           temp_list = g_list_append (temp_list, ancestor);
1796         }
1797       else if (container->focus_child == paned->child2)
1798         {
1799           temp_list = g_list_append (temp_list, ancestor);
1800           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1801           temp_list = g_list_append (temp_list, paned->child1);
1802         }
1803       else
1804         {
1805           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1806           temp_list = g_list_append (temp_list, paned->child1);
1807           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1808           temp_list = g_list_append (temp_list, paned->child2);
1809           temp_list = g_list_append (temp_list, ancestor);
1810         }
1811     }
1812   else
1813     {
1814       if (container->focus_child == paned->child1)
1815         {
1816           temp_list = g_list_append (temp_list, ancestor);
1817           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1818           temp_list = g_list_append (temp_list, paned->child2);
1819         }
1820       else if (container->focus_child == paned->child2)
1821         {
1822           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1823           temp_list = g_list_append (temp_list, paned->child1);
1824           temp_list = g_list_append (temp_list, ancestor);
1825         }
1826       else
1827         {
1828           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1829           temp_list = g_list_append (temp_list, paned->child2);
1830           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1831           temp_list = g_list_append (temp_list, paned->child1);
1832           temp_list = g_list_append (temp_list, ancestor);
1833         }
1834     }
1835
1836   /* Walk the list and expand all the paned widgets. */
1837   for (list = temp_list; list != NULL; list = list->next)
1838     {
1839       GtkWidget *widget = list->data;
1840
1841       if (widget)
1842         {
1843           if (GTK_IS_PANED (widget))
1844             {
1845               paned->in_recursion = TRUE;
1846               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
1847               paned->in_recursion = FALSE;
1848             }
1849           else
1850             {
1851               *widgets = g_list_append (*widgets, widget);
1852             }
1853         }
1854     }
1855
1856   g_list_free (temp_list);
1857 }
1858
1859 static gboolean
1860 gtk_paned_cycle_child_focus (GtkPaned *paned,
1861                              gboolean  reversed)
1862 {
1863   GList *cycle_chain = NULL;
1864   GList *list;
1865   
1866   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
1867
1868   /* ignore f6 if the handle is focused */
1869   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1870     return TRUE;
1871   
1872   /* we can't just let the event propagate up the hierarchy,
1873    * because the paned will want to cycle focus _unless_ an
1874    * ancestor paned handles the event
1875    */
1876   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
1877
1878   for (list = cycle_chain; list != NULL; list = list->next)
1879     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
1880       break;
1881
1882   g_list_free (cycle_chain);
1883   
1884   return TRUE;
1885 }
1886
1887 static void
1888 get_child_panes (GtkWidget  *widget,
1889                  GList     **panes)
1890 {
1891   if (GTK_IS_PANED (widget))
1892     {
1893       GtkPaned *paned = GTK_PANED (widget);
1894       
1895       get_child_panes (paned->child1, panes);
1896       *panes = g_list_prepend (*panes, widget);
1897       get_child_panes (paned->child2, panes);
1898     }
1899   else if (GTK_IS_CONTAINER (widget))
1900     {
1901       gtk_container_forall (GTK_CONTAINER (widget),
1902                              (GtkCallback)get_child_panes, panes);
1903     }
1904 }
1905
1906 static GList *
1907 get_all_panes (GtkPaned *paned)
1908 {
1909   GtkPaned *topmost = NULL;
1910   GList *result = NULL;
1911   GtkWidget *w;
1912   
1913   for (w = GTK_WIDGET (paned); w != NULL; w = w->parent)
1914     {
1915       if (GTK_IS_PANED (w))
1916         topmost = GTK_PANED (w);
1917     }
1918
1919   g_assert (topmost);
1920
1921   get_child_panes (GTK_WIDGET (topmost), &result);
1922
1923   return g_list_reverse (result);
1924 }
1925
1926 static void
1927 gtk_paned_find_neighbours (GtkPaned  *paned,
1928                            GtkPaned **next,
1929                            GtkPaned **prev)
1930 {
1931   GList *all_panes;
1932   GList *this_link;
1933
1934   all_panes = get_all_panes (paned);
1935   g_assert (all_panes);
1936
1937   this_link = g_list_find (all_panes, paned);
1938
1939   g_assert (this_link);
1940   
1941   if (this_link->next)
1942     *next = this_link->next->data;
1943   else
1944     *next = all_panes->data;
1945
1946   if (this_link->prev)
1947     *prev = this_link->prev->data;
1948   else
1949     *prev = g_list_last (all_panes)->data;
1950
1951   g_list_free (all_panes);
1952 }
1953
1954 static gboolean
1955 gtk_paned_move_handle (GtkPaned      *paned,
1956                        GtkScrollType  scroll)
1957 {
1958   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1959     {
1960       gint old_position;
1961       gint new_position;
1962       gint increment;
1963       
1964       enum {
1965         SINGLE_STEP_SIZE = 1,
1966         PAGE_STEP_SIZE   = 75
1967       };
1968       
1969       new_position = old_position = gtk_paned_get_position (paned);
1970       increment = 0;
1971       
1972       switch (scroll)
1973         {
1974         case GTK_SCROLL_STEP_LEFT:
1975         case GTK_SCROLL_STEP_UP:
1976         case GTK_SCROLL_STEP_BACKWARD:
1977           increment = - SINGLE_STEP_SIZE;
1978           break;
1979           
1980         case GTK_SCROLL_STEP_RIGHT:
1981         case GTK_SCROLL_STEP_DOWN:
1982         case GTK_SCROLL_STEP_FORWARD:
1983           increment = SINGLE_STEP_SIZE;
1984           break;
1985           
1986         case GTK_SCROLL_PAGE_LEFT:
1987         case GTK_SCROLL_PAGE_UP:
1988         case GTK_SCROLL_PAGE_BACKWARD:
1989           increment = - PAGE_STEP_SIZE;
1990           break;
1991           
1992         case GTK_SCROLL_PAGE_RIGHT:
1993         case GTK_SCROLL_PAGE_DOWN:
1994         case GTK_SCROLL_PAGE_FORWARD:
1995           increment = PAGE_STEP_SIZE;
1996           break;
1997           
1998         case GTK_SCROLL_START:
1999           new_position = paned->min_position;
2000           break;
2001           
2002         case GTK_SCROLL_END:
2003           new_position = paned->max_position;
2004           break;
2005
2006         default:
2007           break;
2008         }
2009
2010       if (increment)
2011         {
2012           if (is_rtl (paned))
2013             increment = -increment;
2014           
2015           new_position = old_position + increment;
2016         }
2017       
2018       new_position = CLAMP (new_position, paned->min_position, paned->max_position);
2019       
2020       if (old_position != new_position)
2021         gtk_paned_set_position (paned, new_position);
2022
2023       return TRUE;
2024     }
2025
2026   return FALSE;
2027 }
2028
2029 static void
2030 gtk_paned_restore_focus (GtkPaned *paned)
2031 {
2032   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2033     {
2034       if (paned->priv->saved_focus &&
2035           GTK_WIDGET_SENSITIVE (paned->priv->saved_focus))
2036         {
2037           gtk_widget_grab_focus (paned->priv->saved_focus);
2038         }
2039       else
2040         {
2041           /* the saved focus is somehow not available for focusing,
2042            * try
2043            *   1) tabbing into the paned widget
2044            * if that didn't work,
2045            *   2) unset focus for the window if there is one
2046            */
2047           
2048           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
2049             {
2050               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2051               
2052               if (GTK_IS_WINDOW (toplevel))
2053                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
2054             }
2055         }
2056       
2057       gtk_paned_set_saved_focus (paned, NULL);
2058       gtk_paned_set_first_paned (paned, NULL);
2059     }
2060 }
2061
2062 static gboolean
2063 gtk_paned_accept_position (GtkPaned *paned)
2064 {
2065   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2066     {
2067       paned->original_position = -1;
2068       gtk_paned_restore_focus (paned);
2069
2070       return TRUE;
2071     }
2072
2073   return FALSE;
2074 }
2075
2076
2077 static gboolean
2078 gtk_paned_cancel_position (GtkPaned *paned)
2079 {
2080   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2081     {
2082       if (paned->original_position != -1)
2083         {
2084           gtk_paned_set_position (paned, paned->original_position);
2085           paned->original_position = -1;
2086         }
2087
2088       gtk_paned_restore_focus (paned);
2089       return TRUE;
2090     }
2091
2092   return FALSE;
2093 }
2094
2095 static gboolean
2096 gtk_paned_cycle_handle_focus (GtkPaned *paned,
2097                               gboolean  reversed)
2098 {
2099   GtkPaned *next, *prev;
2100   
2101   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2102     {
2103       GtkPaned *focus = NULL;
2104
2105       if (!paned->priv->first_paned)
2106         {
2107           /* The first_pane has disappeared. As an ad-hoc solution,
2108            * we make the currently focused paned the first_paned. To the
2109            * user this will seem like the paned cycling has been reset.
2110            */
2111           
2112           gtk_paned_set_first_paned (paned, paned);
2113         }
2114       
2115       gtk_paned_find_neighbours (paned, &next, &prev);
2116
2117       if (reversed && prev &&
2118           prev != paned && paned != paned->priv->first_paned)
2119         {
2120           focus = prev;
2121         }
2122       else if (!reversed && next &&
2123                next != paned && next != paned->priv->first_paned)
2124         {
2125           focus = next;
2126         }
2127       else
2128         {
2129           gtk_paned_accept_position (paned);
2130           return TRUE;
2131         }
2132
2133       g_assert (focus);
2134       
2135       gtk_paned_set_saved_focus (focus, paned->priv->saved_focus);
2136       gtk_paned_set_first_paned (focus, paned->priv->first_paned);
2137       
2138       gtk_paned_set_saved_focus (paned, NULL);
2139       gtk_paned_set_first_paned (paned, NULL);
2140       
2141       gtk_widget_grab_focus (GTK_WIDGET (focus));
2142       
2143       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
2144         {
2145           paned->original_position = -1;
2146           focus->original_position = gtk_paned_get_position (focus);
2147         }
2148     }
2149   else
2150     {
2151       GtkContainer *container = GTK_CONTAINER (paned);
2152       GtkPaned *focus;
2153       GtkPaned *first;
2154       GtkPaned *prev, *next;
2155       GtkWidget *toplevel;
2156
2157       gtk_paned_find_neighbours (paned, &next, &prev);
2158
2159       if (container->focus_child == paned->child1)
2160         {
2161           if (reversed)
2162             {
2163               focus = prev;
2164               first = paned;
2165             }
2166           else
2167             {
2168               focus = paned;
2169               first = paned;
2170             }
2171         }
2172       else if (container->focus_child == paned->child2)
2173         {
2174           if (reversed)
2175             {
2176               focus = paned;
2177               first = next;
2178             }
2179           else
2180             {
2181               focus = next;
2182               first = next;
2183             }
2184         }
2185       else
2186         {
2187           /* Focus is not inside this paned, and we don't have focus.
2188            * Presumably this happened because the application wants us
2189            * to start keyboard navigating.
2190            */
2191           focus = paned;
2192
2193           if (reversed)
2194             first = paned;
2195           else
2196             first = next;
2197         }
2198
2199       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2200
2201       if (GTK_IS_WINDOW (toplevel))
2202         gtk_paned_set_saved_focus (focus, GTK_WINDOW (toplevel)->focus_widget);
2203       gtk_paned_set_first_paned (focus, first);
2204       focus->original_position = gtk_paned_get_position (focus); 
2205
2206       gtk_widget_grab_focus (GTK_WIDGET (focus));
2207    }
2208   
2209   return TRUE;
2210 }
2211
2212 static gboolean
2213 gtk_paned_toggle_handle_focus (GtkPaned *paned)
2214 {
2215   /* This function/signal has the wrong name. It is called when you
2216    * press Tab or Shift-Tab and what we do is act as if
2217    * the user pressed Return and then Tab or Shift-Tab
2218    */
2219   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2220     gtk_paned_accept_position (paned);
2221
2222   return FALSE;
2223 }
2224
2225 #define __GTK_PANED_C__
2226 #include "gtkaliasdef.c"