]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
Deprecate widget flag: GTK_WIDGET_VISIBLE
[~andy/gtk] / gtk / gtkpaned.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #include "config.h"
28
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkbindings.h"
31 #include "gtkmain.h"
32 #include "gtkmarshalers.h"
33 #include "gtkorientable.h"
34 #include "gtkpaned.h"
35 #include "gtkwindow.h"
36 #include "gtkprivate.h"
37 #include "gtkintl.h"
38 #include "gtkalias.h"
39
40 enum {
41   PROP_0,
42   PROP_ORIENTATION,
43   PROP_POSITION,
44   PROP_POSITION_SET,
45   PROP_MIN_POSITION,
46   PROP_MAX_POSITION
47 };
48
49 enum {
50   CHILD_PROP_0,
51   CHILD_PROP_RESIZE,
52   CHILD_PROP_SHRINK
53 };
54
55 enum {
56   CYCLE_CHILD_FOCUS,
57   TOGGLE_HANDLE_FOCUS,
58   MOVE_HANDLE,
59   CYCLE_HANDLE_FOCUS,
60   ACCEPT_POSITION,
61   CANCEL_POSITION,
62   LAST_SIGNAL
63 };
64
65 static void     gtk_paned_set_property          (GObject          *object,
66                                                  guint             prop_id,
67                                                  const GValue     *value,
68                                                  GParamSpec       *pspec);
69 static void     gtk_paned_get_property          (GObject          *object,
70                                                  guint             prop_id,
71                                                  GValue           *value,
72                                                  GParamSpec       *pspec);
73 static void     gtk_paned_set_child_property    (GtkContainer     *container,
74                                                  GtkWidget        *child,
75                                                  guint             property_id,
76                                                  const GValue     *value,
77                                                  GParamSpec       *pspec);
78 static void     gtk_paned_get_child_property    (GtkContainer     *container,
79                                                  GtkWidget        *child,
80                                                  guint             property_id,
81                                                  GValue           *value,
82                                                  GParamSpec       *pspec);
83 static void     gtk_paned_finalize              (GObject          *object);
84
85 static void     gtk_paned_size_request          (GtkWidget        *widget,
86                                                  GtkRequisition   *requisition);
87 static void     gtk_paned_size_allocate         (GtkWidget        *widget,
88                                                  GtkAllocation    *allocation);
89 static void     gtk_paned_realize               (GtkWidget        *widget);
90 static void     gtk_paned_unrealize             (GtkWidget        *widget);
91 static void     gtk_paned_map                   (GtkWidget        *widget);
92 static void     gtk_paned_unmap                 (GtkWidget        *widget);
93 static void     gtk_paned_state_changed         (GtkWidget        *widget,
94                                                  GtkStateType      previous_state);
95 static gboolean gtk_paned_expose                (GtkWidget        *widget,
96                                                  GdkEventExpose   *event);
97 static gboolean gtk_paned_enter                 (GtkWidget        *widget,
98                                                  GdkEventCrossing *event);
99 static gboolean gtk_paned_leave                 (GtkWidget        *widget,
100                                                  GdkEventCrossing *event);
101 static gboolean gtk_paned_button_press          (GtkWidget        *widget,
102                                                  GdkEventButton   *event);
103 static gboolean gtk_paned_button_release        (GtkWidget        *widget,
104                                                  GdkEventButton   *event);
105 static gboolean gtk_paned_motion                (GtkWidget        *widget,
106                                                  GdkEventMotion   *event);
107 static gboolean gtk_paned_focus                 (GtkWidget        *widget,
108                                                  GtkDirectionType  direction);
109 static gboolean gtk_paned_grab_broken           (GtkWidget          *widget,
110                                                  GdkEventGrabBroken *event);
111 static void     gtk_paned_add                   (GtkContainer     *container,
112                                                  GtkWidget        *widget);
113 static void     gtk_paned_remove                (GtkContainer     *container,
114                                                  GtkWidget        *widget);
115 static void     gtk_paned_forall                (GtkContainer     *container,
116                                                  gboolean          include_internals,
117                                                  GtkCallback       callback,
118                                                  gpointer          callback_data);
119 static void     gtk_paned_calc_position         (GtkPaned         *paned,
120                                                  gint              allocation,
121                                                  gint              child1_req,
122                                                  gint              child2_req);
123 static void     gtk_paned_set_focus_child       (GtkContainer     *container,
124                                                  GtkWidget        *child);
125 static void     gtk_paned_set_saved_focus       (GtkPaned         *paned,
126                                                  GtkWidget        *widget);
127 static void     gtk_paned_set_first_paned       (GtkPaned         *paned,
128                                                  GtkPaned         *first_paned);
129 static void     gtk_paned_set_last_child1_focus (GtkPaned         *paned,
130                                                  GtkWidget        *widget);
131 static void     gtk_paned_set_last_child2_focus (GtkPaned         *paned,
132                                                  GtkWidget        *widget);
133 static gboolean gtk_paned_cycle_child_focus     (GtkPaned         *paned,
134                                                  gboolean          reverse);
135 static gboolean gtk_paned_cycle_handle_focus    (GtkPaned         *paned,
136                                                  gboolean          reverse);
137 static gboolean gtk_paned_move_handle           (GtkPaned         *paned,
138                                                  GtkScrollType     scroll);
139 static gboolean gtk_paned_accept_position       (GtkPaned         *paned);
140 static gboolean gtk_paned_cancel_position       (GtkPaned         *paned);
141 static gboolean gtk_paned_toggle_handle_focus   (GtkPaned         *paned);
142
143 static GType    gtk_paned_child_type            (GtkContainer     *container);
144 static void     gtk_paned_grab_notify           (GtkWidget        *widget,
145                                                  gboolean          was_grabbed);
146
147 struct _GtkPanedPrivate
148 {
149   GtkOrientation  orientation;
150   GtkWidget      *saved_focus;
151   GtkPaned       *first_paned;
152   guint32         grab_time;
153 };
154
155
156 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GtkPaned, gtk_paned, GTK_TYPE_CONTAINER,
157                                   G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
158                                                          NULL))
159
160 static guint signals[LAST_SIGNAL] = { 0 };
161
162
163 static void
164 add_tab_bindings (GtkBindingSet    *binding_set,
165                   GdkModifierType   modifiers)
166 {
167   gtk_binding_entry_add_signal (binding_set, GDK_Tab, modifiers,
168                                 "toggle-handle-focus", 0);
169   gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, modifiers,
170                                 "toggle-handle-focus", 0);
171 }
172
173 static void
174 add_move_binding (GtkBindingSet   *binding_set,
175                   guint            keyval,
176                   GdkModifierType  mask,
177                   GtkScrollType    scroll)
178 {
179   gtk_binding_entry_add_signal (binding_set, keyval, mask,
180                                 "move-handle", 1,
181                                 GTK_TYPE_SCROLL_TYPE, scroll);
182 }
183
184 static void
185 gtk_paned_class_init (GtkPanedClass *class)
186 {
187   GObjectClass *object_class;
188   GtkWidgetClass *widget_class;
189   GtkContainerClass *container_class;
190   GtkPanedClass *paned_class;
191   GtkBindingSet *binding_set;
192
193   object_class = (GObjectClass *) class;
194   widget_class = (GtkWidgetClass *) class;
195   container_class = (GtkContainerClass *) class;
196   paned_class = (GtkPanedClass *) class;
197
198   object_class->set_property = gtk_paned_set_property;
199   object_class->get_property = gtk_paned_get_property;
200   object_class->finalize = gtk_paned_finalize;
201
202   widget_class->size_request = gtk_paned_size_request;
203   widget_class->size_allocate = gtk_paned_size_allocate;
204   widget_class->realize = gtk_paned_realize;
205   widget_class->unrealize = gtk_paned_unrealize;
206   widget_class->map = gtk_paned_map;
207   widget_class->unmap = gtk_paned_unmap;
208   widget_class->expose_event = gtk_paned_expose;
209   widget_class->focus = gtk_paned_focus;
210   widget_class->enter_notify_event = gtk_paned_enter;
211   widget_class->leave_notify_event = gtk_paned_leave;
212   widget_class->button_press_event = gtk_paned_button_press;
213   widget_class->button_release_event = gtk_paned_button_release;
214   widget_class->motion_notify_event = gtk_paned_motion;
215   widget_class->grab_broken_event = gtk_paned_grab_broken;
216   widget_class->grab_notify = gtk_paned_grab_notify;
217   widget_class->state_changed = gtk_paned_state_changed;
218
219   container_class->add = gtk_paned_add;
220   container_class->remove = gtk_paned_remove;
221   container_class->forall = gtk_paned_forall;
222   container_class->child_type = gtk_paned_child_type;
223   container_class->set_focus_child = gtk_paned_set_focus_child;
224   container_class->set_child_property = gtk_paned_set_child_property;
225   container_class->get_child_property = gtk_paned_get_child_property;
226
227   paned_class->cycle_child_focus = gtk_paned_cycle_child_focus;
228   paned_class->toggle_handle_focus = gtk_paned_toggle_handle_focus;
229   paned_class->move_handle = gtk_paned_move_handle;
230   paned_class->cycle_handle_focus = gtk_paned_cycle_handle_focus;
231   paned_class->accept_position = gtk_paned_accept_position;
232   paned_class->cancel_position = gtk_paned_cancel_position;
233
234   g_object_class_override_property (object_class,
235                                     PROP_ORIENTATION,
236                                     "orientation");
237
238   g_object_class_install_property (object_class,
239                                    PROP_POSITION,
240                                    g_param_spec_int ("position",
241                                                      P_("Position"),
242                                                      P_("Position of paned separator in pixels (0 means all the way to the left/top)"),
243                                                      0,
244                                                      G_MAXINT,
245                                                      0,
246                                                      GTK_PARAM_READWRITE));
247
248   g_object_class_install_property (object_class,
249                                    PROP_POSITION_SET,
250                                    g_param_spec_boolean ("position-set",
251                                                          P_("Position Set"),
252                                                          P_("TRUE if the Position property should be used"),
253                                                          FALSE,
254                                                          GTK_PARAM_READWRITE));
255
256   gtk_widget_class_install_style_property (widget_class,
257                                            g_param_spec_int ("handle-size",
258                                                              P_("Handle Size"),
259                                                              P_("Width of handle"),
260                                                              0,
261                                                              G_MAXINT,
262                                                              5,
263                                                              GTK_PARAM_READABLE));
264   /**
265    * GtkPaned:min-position:
266    *
267    * The smallest possible value for the position property. This property is derived from the
268    * size and shrinkability of the widget's children.
269    *
270    * Since: 2.4
271    */
272   g_object_class_install_property (object_class,
273                                    PROP_MIN_POSITION,
274                                    g_param_spec_int ("min-position",
275                                                      P_("Minimal Position"),
276                                                      P_("Smallest possible value for the \"position\" property"),
277                                                      0,
278                                                      G_MAXINT,
279                                                      0,
280                                                      GTK_PARAM_READABLE));
281
282   /**
283    * GtkPaned:max-position:
284    *
285    * The largest possible value for the position property. This property is derived from the
286    * size and shrinkability of the widget's children.
287    *
288    * Since: 2.4
289    */
290   g_object_class_install_property (object_class,
291                                    PROP_MAX_POSITION,
292                                    g_param_spec_int ("max-position",
293                                                      P_("Maximal Position"),
294                                                      P_("Largest possible value for the \"position\" property"),
295                                                      0,
296                                                      G_MAXINT,
297                                                      G_MAXINT,
298                                                      GTK_PARAM_READABLE));
299
300   /**
301    * GtkPaned:resize:
302    *
303    * The "resize" child property determines whether the child expands and
304    * shrinks along with the paned widget.
305    *
306    * Since: 2.4
307    */
308   gtk_container_class_install_child_property (container_class,
309                                               CHILD_PROP_RESIZE,
310                                               g_param_spec_boolean ("resize", 
311                                                                     P_("Resize"),
312                                                                     P_("If TRUE, the child expands and shrinks along with the paned widget"),
313                                                                     TRUE,
314                                                                     GTK_PARAM_READWRITE));
315
316   /**
317    * GtkPaned:shrink:
318    *
319    * The "shrink" child property determines whether the child can be made
320    * smaller than its requisition.
321    *
322    * Since: 2.4
323    */
324   gtk_container_class_install_child_property (container_class,
325                                               CHILD_PROP_SHRINK,
326                                               g_param_spec_boolean ("shrink", 
327                                                                     P_("Shrink"),
328                                                                     P_("If TRUE, the child can be made smaller than its requisition"),
329                                                                     TRUE,
330                                                                     GTK_PARAM_READWRITE));
331
332   /**
333    * GtkPaned::cycle-child-focus:
334    * @widget: the object that received the signal
335    * @reversed: whether cycling backward or forward
336    *
337    * The ::cycle-child-focus signal is a 
338    * <link linkend="keybinding-signals">keybinding signal</link>
339    * which gets emitted to cycle the focus between the children of the paned.
340    *
341    * The default binding is f6.
342    *
343    * Since: 2.0
344    */
345   signals [CYCLE_CHILD_FOCUS] =
346     g_signal_new (I_("cycle-child-focus"),
347                   G_TYPE_FROM_CLASS (object_class),
348                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
349                   G_STRUCT_OFFSET (GtkPanedClass, cycle_child_focus),
350                   NULL, NULL,
351                   _gtk_marshal_BOOLEAN__BOOLEAN,
352                   G_TYPE_BOOLEAN, 1,
353                   G_TYPE_BOOLEAN);
354
355   /**
356    * GtkPaned::toggle-handle-focus:
357    * @widget: the object that received the signal
358    *
359    * The ::toggle-handle-focus is a 
360    * <link linkend="keybinding-signals">keybinding signal</link>
361    * which gets emitted to accept the current position of the handle and then 
362    * move focus to the next widget in the focus chain.
363    *
364    * The default binding is Tab.
365    *
366    * Since: 2.0
367    */
368   signals [TOGGLE_HANDLE_FOCUS] =
369     g_signal_new (I_("toggle-handle-focus"),
370                   G_TYPE_FROM_CLASS (object_class),
371                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
372                   G_STRUCT_OFFSET (GtkPanedClass, toggle_handle_focus),
373                   NULL, NULL,
374                   _gtk_marshal_BOOLEAN__VOID,
375                   G_TYPE_BOOLEAN, 0);
376
377   /**
378    * GtkPaned::move-handle:
379    * @widget: the object that received the signal
380    * @scroll_type: a #GtkScrollType
381    *
382    * The ::move-handle signal is a 
383    * <link linkend="keybinding-signals">keybinding signal</link>
384    * which gets emitted to move the handle when the user is using key bindings 
385    * to move it.
386    *
387    * Since: 2.0
388    */
389   signals[MOVE_HANDLE] =
390     g_signal_new (I_("move-handle"),
391                   G_TYPE_FROM_CLASS (object_class),
392                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
393                   G_STRUCT_OFFSET (GtkPanedClass, move_handle),
394                   NULL, NULL,
395                   _gtk_marshal_BOOLEAN__ENUM,
396                   G_TYPE_BOOLEAN, 1,
397                   GTK_TYPE_SCROLL_TYPE);
398
399   /**
400    * GtkPaned::cycle-handle-focus:
401    * @widget: the object that received the signal
402    * @reversed: whether cycling backward or forward
403    *
404    * The ::cycle-handle-focus signal is a 
405    * <link linkend="keybinding-signals">keybinding signal</link>
406    * which gets emitted to cycle whether the paned should grab focus to allow
407    * the user to change position of the handle by using key bindings.
408    *
409    * The default binding for this signal is f8.
410    *
411    * Since: 2.0
412    */
413   signals [CYCLE_HANDLE_FOCUS] =
414     g_signal_new (I_("cycle-handle-focus"),
415                   G_TYPE_FROM_CLASS (object_class),
416                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
417                   G_STRUCT_OFFSET (GtkPanedClass, cycle_handle_focus),
418                   NULL, NULL,
419                   _gtk_marshal_BOOLEAN__BOOLEAN,
420                   G_TYPE_BOOLEAN, 1,
421                   G_TYPE_BOOLEAN);
422
423   /**
424    * GtkPaned::accept-position:
425    * @widget: the object that received the signal
426    *
427    * The ::accept-position signal is a 
428    * <link linkend="keybinding-signals">keybinding signal</link>
429    * which gets emitted to accept the current position of the handle when 
430    * moving it using key bindings.
431    *
432    * The default binding for this signal is Return or Space.
433    *
434    * Since: 2.0
435    */
436   signals [ACCEPT_POSITION] =
437     g_signal_new (I_("accept-position"),
438                   G_TYPE_FROM_CLASS (object_class),
439                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
440                   G_STRUCT_OFFSET (GtkPanedClass, accept_position),
441                   NULL, NULL,
442                   _gtk_marshal_BOOLEAN__VOID,
443                   G_TYPE_BOOLEAN, 0);
444
445   /**
446    * GtkPaned::cancel-position:
447    * @widget: the object that received the signal
448    *
449    * The ::cancel-position signal is a 
450    * <link linkend="keybinding-signals">keybinding signal</link>
451    * which gets emitted to cancel moving the position of the handle using key 
452    * bindings. The position of the handle will be reset to the value prior to 
453    * moving it.
454    *
455    * The default binding for this signal is Escape.
456    *
457    * Since: 2.0
458    */
459   signals [CANCEL_POSITION] =
460     g_signal_new (I_("cancel-position"),
461                   G_TYPE_FROM_CLASS (object_class),
462                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
463                   G_STRUCT_OFFSET (GtkPanedClass, cancel_position),
464                   NULL, NULL,
465                   _gtk_marshal_BOOLEAN__VOID,
466                   G_TYPE_BOOLEAN, 0);
467
468   binding_set = gtk_binding_set_by_class (class);
469
470   /* F6 and friends */
471   gtk_binding_entry_add_signal (binding_set,
472                                 GDK_F6, 0,
473                                 "cycle-child-focus", 1, 
474                                 G_TYPE_BOOLEAN, FALSE);
475   gtk_binding_entry_add_signal (binding_set,
476                                 GDK_F6, GDK_SHIFT_MASK,
477                                 "cycle-child-focus", 1,
478                                 G_TYPE_BOOLEAN, TRUE);
479
480   /* F8 and friends */
481   gtk_binding_entry_add_signal (binding_set,
482                                 GDK_F8, 0,
483                                 "cycle-handle-focus", 1,
484                                 G_TYPE_BOOLEAN, FALSE);
485  
486   gtk_binding_entry_add_signal (binding_set,
487                                 GDK_F8, GDK_SHIFT_MASK,
488                                 "cycle-handle-focus", 1,
489                                 G_TYPE_BOOLEAN, TRUE);
490  
491   add_tab_bindings (binding_set, 0);
492   add_tab_bindings (binding_set, GDK_CONTROL_MASK);
493   add_tab_bindings (binding_set, GDK_SHIFT_MASK);
494   add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK);
495
496   /* accept and cancel positions */
497   gtk_binding_entry_add_signal (binding_set,
498                                 GDK_Escape, 0,
499                                 "cancel-position", 0);
500
501   gtk_binding_entry_add_signal (binding_set,
502                                 GDK_Return, 0,
503                                 "accept-position", 0);
504   gtk_binding_entry_add_signal (binding_set,
505                                 GDK_ISO_Enter, 0,
506                                 "accept-position", 0);
507   gtk_binding_entry_add_signal (binding_set,
508                                 GDK_KP_Enter, 0,
509                                 "accept-position", 0);
510   gtk_binding_entry_add_signal (binding_set,
511                                 GDK_space, 0,
512                                 "accept-position", 0);
513   gtk_binding_entry_add_signal (binding_set,
514                                 GDK_KP_Space, 0,
515                                 "accept-position", 0);
516
517   /* move handle */
518   add_move_binding (binding_set, GDK_Left, 0, GTK_SCROLL_STEP_LEFT);
519   add_move_binding (binding_set, GDK_KP_Left, 0, GTK_SCROLL_STEP_LEFT);
520   add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
521   add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
522
523   add_move_binding (binding_set, GDK_Right, 0, GTK_SCROLL_STEP_RIGHT);
524   add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
525   add_move_binding (binding_set, GDK_KP_Right, 0, GTK_SCROLL_STEP_RIGHT);
526   add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
527
528   add_move_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
529   add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
530   add_move_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
531   add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
532   add_move_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
533   add_move_binding (binding_set, GDK_KP_Page_Up, 0, GTK_SCROLL_PAGE_UP);
534
535   add_move_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
536   add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
537   add_move_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
538   add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
539   add_move_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
540   add_move_binding (binding_set, GDK_KP_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
541
542   add_move_binding (binding_set, GDK_Home, 0, GTK_SCROLL_START);
543   add_move_binding (binding_set, GDK_KP_Home, 0, GTK_SCROLL_START);
544   add_move_binding (binding_set, GDK_End, 0, GTK_SCROLL_END);
545   add_move_binding (binding_set, GDK_KP_End, 0, GTK_SCROLL_END);
546
547   g_type_class_add_private (object_class, sizeof (GtkPanedPrivate));
548 }
549
550 static GType
551 gtk_paned_child_type (GtkContainer *container)
552 {
553   if (!GTK_PANED (container)->child1 || !GTK_PANED (container)->child2)
554     return GTK_TYPE_WIDGET;
555   else
556     return G_TYPE_NONE;
557 }
558
559 static void
560 gtk_paned_init (GtkPaned *paned)
561 {
562   GTK_WIDGET_SET_FLAGS (paned, GTK_NO_WINDOW | 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_get_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_get_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_get_visible (paned->child1) &&
795       paned->child2 && gtk_widget_get_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_get_visible (paned->child1) &&
828       paned->child2 && gtk_widget_get_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_get_visible (paned->child1))
967         gtk_widget_size_allocate (paned->child1, &child_allocation);
968       else if (paned->child2 && gtk_widget_get_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_get_visible (paned->child1) &&
1016       paned->child2 && gtk_widget_get_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_get_visible (widget) && GTK_WIDGET_MAPPED (widget) &&
1073       paned->child1 && gtk_widget_get_visible (paned->child1) &&
1074       paned->child2 && gtk_widget_get_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 #if 0
1328 /**
1329  * gtk_paned_new:
1330  * @orientation: the paned's orientation.
1331  *
1332  * Creates a new #GtkPaned widget.
1333  *
1334  * Return value: a new #GtkPaned.
1335  *
1336  * Since: 2.16
1337  **/
1338 GtkWidget *
1339 gtk_paned_new (GtkOrientation orientation)
1340 {
1341   return g_object_new (GTK_TYPE_PANED,
1342                        "orientation", orientation,
1343                        NULL);
1344 }
1345 #endif
1346
1347 void
1348 gtk_paned_add1 (GtkPaned  *paned,
1349                 GtkWidget *widget)
1350 {
1351   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
1352 }
1353
1354 void
1355 gtk_paned_add2 (GtkPaned  *paned,
1356                 GtkWidget *widget)
1357 {
1358   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
1359 }
1360
1361 void
1362 gtk_paned_pack1 (GtkPaned  *paned,
1363                  GtkWidget *child,
1364                  gboolean   resize,
1365                  gboolean   shrink)
1366 {
1367   g_return_if_fail (GTK_IS_PANED (paned));
1368   g_return_if_fail (GTK_IS_WIDGET (child));
1369
1370   if (!paned->child1)
1371     {
1372       paned->child1 = child;
1373       paned->child1_resize = resize;
1374       paned->child1_shrink = shrink;
1375
1376       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1377     }
1378 }
1379
1380 void
1381 gtk_paned_pack2 (GtkPaned  *paned,
1382                  GtkWidget *child,
1383                  gboolean   resize,
1384                  gboolean   shrink)
1385 {
1386   g_return_if_fail (GTK_IS_PANED (paned));
1387   g_return_if_fail (GTK_IS_WIDGET (child));
1388
1389   if (!paned->child2)
1390     {
1391       paned->child2 = child;
1392       paned->child2_resize = resize;
1393       paned->child2_shrink = shrink;
1394
1395       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1396     }
1397 }
1398
1399
1400 static void
1401 gtk_paned_add (GtkContainer *container,
1402                GtkWidget    *widget)
1403 {
1404   GtkPaned *paned;
1405
1406   g_return_if_fail (GTK_IS_PANED (container));
1407
1408   paned = GTK_PANED (container);
1409
1410   if (!paned->child1)
1411     gtk_paned_add1 (paned, widget);
1412   else if (!paned->child2)
1413     gtk_paned_add2 (paned, widget);
1414   else
1415     g_warning ("GtkPaned cannot have more than 2 children\n");
1416 }
1417
1418 static void
1419 gtk_paned_remove (GtkContainer *container,
1420                   GtkWidget    *widget)
1421 {
1422   GtkPaned *paned;
1423   gboolean was_visible;
1424
1425   paned = GTK_PANED (container);
1426   was_visible = gtk_widget_get_visible (widget);
1427
1428   if (paned->child1 == widget)
1429     {
1430       gtk_widget_unparent (widget);
1431
1432       paned->child1 = NULL;
1433
1434       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1435         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1436     }
1437   else if (paned->child2 == widget)
1438     {
1439       gtk_widget_unparent (widget);
1440
1441       paned->child2 = NULL;
1442
1443       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1444         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1445     }
1446 }
1447
1448 static void
1449 gtk_paned_forall (GtkContainer *container,
1450                   gboolean      include_internals,
1451                   GtkCallback   callback,
1452                   gpointer      callback_data)
1453 {
1454   GtkPaned *paned;
1455
1456   g_return_if_fail (callback != NULL);
1457
1458   paned = GTK_PANED (container);
1459
1460   if (paned->child1)
1461     (*callback) (paned->child1, callback_data);
1462   if (paned->child2)
1463     (*callback) (paned->child2, callback_data);
1464 }
1465
1466 /**
1467  * gtk_paned_get_position:
1468  * @paned: a #GtkPaned widget
1469  * 
1470  * Obtains the position of the divider between the two panes.
1471  * 
1472  * Return value: position of the divider
1473  **/
1474 gint
1475 gtk_paned_get_position (GtkPaned  *paned)
1476 {
1477   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1478
1479   return paned->child1_size;
1480 }
1481
1482 /**
1483  * gtk_paned_set_position:
1484  * @paned: a #GtkPaned widget
1485  * @position: pixel position of divider, a negative value means that the position
1486  *            is unset.
1487  * 
1488  * Sets the position of the divider between the two panes.
1489  **/
1490 void
1491 gtk_paned_set_position (GtkPaned *paned,
1492                         gint      position)
1493 {
1494   GObject *object;
1495   
1496   g_return_if_fail (GTK_IS_PANED (paned));
1497
1498   if (paned->child1_size == position)
1499     return;
1500
1501   object = G_OBJECT (paned);
1502   
1503   if (position >= 0)
1504     {
1505       /* We don't clamp here - the assumption is that
1506        * if the total allocation changes at the same time
1507        * as the position, the position set is with reference
1508        * to the new total size. If only the position changes,
1509        * then clamping will occur in gtk_paned_calc_position()
1510        */
1511
1512       paned->child1_size = position;
1513       paned->position_set = TRUE;
1514     }
1515   else
1516     {
1517       paned->position_set = FALSE;
1518     }
1519
1520   g_object_freeze_notify (object);
1521   g_object_notify (object, "position");
1522   g_object_notify (object, "position-set");
1523   g_object_thaw_notify (object);
1524
1525   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
1526
1527 #ifdef G_OS_WIN32
1528   /* Hacky work-around for bug #144269 */
1529   if (paned->child2 != NULL)
1530     {
1531       gtk_widget_queue_draw (paned->child2);
1532     }
1533 #endif
1534 }
1535
1536 /**
1537  * gtk_paned_get_child1:
1538  * @paned: a #GtkPaned widget
1539  * 
1540  * Obtains the first child of the paned widget.
1541  * 
1542  * Return value: first child, or %NULL if it is not set.
1543  *
1544  * Since: 2.4
1545  **/
1546 GtkWidget *
1547 gtk_paned_get_child1 (GtkPaned *paned)
1548 {
1549   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1550
1551   return paned->child1;
1552 }
1553
1554 /**
1555  * gtk_paned_get_child2:
1556  * @paned: a #GtkPaned widget
1557  * 
1558  * Obtains the second child of the paned widget.
1559  * 
1560  * Return value: second child, or %NULL if it is not set.
1561  *
1562  * Since: 2.4
1563  **/
1564 GtkWidget *
1565 gtk_paned_get_child2 (GtkPaned *paned)
1566 {
1567   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1568
1569   return paned->child2;
1570 }
1571
1572 void
1573 gtk_paned_compute_position (GtkPaned *paned,
1574                             gint      allocation,
1575                             gint      child1_req,
1576                             gint      child2_req)
1577 {
1578   g_return_if_fail (GTK_IS_PANED (paned));
1579
1580   gtk_paned_calc_position (paned, allocation, child1_req, child2_req);
1581 }
1582
1583 static void
1584 gtk_paned_calc_position (GtkPaned *paned,
1585                          gint      allocation,
1586                          gint      child1_req,
1587                          gint      child2_req)
1588 {
1589   gint old_position;
1590   gint old_min_position;
1591   gint old_max_position;
1592
1593   old_position = paned->child1_size;
1594   old_min_position = paned->min_position;
1595   old_max_position = paned->max_position;
1596
1597   paned->min_position = paned->child1_shrink ? 0 : child1_req;
1598
1599   paned->max_position = allocation;
1600   if (!paned->child2_shrink)
1601     paned->max_position = MAX (1, paned->max_position - child2_req);
1602   paned->max_position = MAX (paned->min_position, paned->max_position);
1603
1604   if (!paned->position_set)
1605     {
1606       if (paned->child1_resize && !paned->child2_resize)
1607         paned->child1_size = MAX (0, allocation - child2_req);
1608       else if (!paned->child1_resize && paned->child2_resize)
1609         paned->child1_size = child1_req;
1610       else if (child1_req + child2_req != 0)
1611         paned->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
1612       else
1613         paned->child1_size = allocation * 0.5 + 0.5;
1614     }
1615   else
1616     {
1617       /* If the position was set before the initial allocation.
1618        * (paned->last_allocation <= 0) just clamp it and leave it.
1619        */
1620       if (paned->last_allocation > 0)
1621         {
1622           if (paned->child1_resize && !paned->child2_resize)
1623             paned->child1_size += allocation - paned->last_allocation;
1624           else if (!(!paned->child1_resize && paned->child2_resize))
1625             paned->child1_size = allocation * ((gdouble) paned->child1_size / (paned->last_allocation)) + 0.5;
1626         }
1627     }
1628
1629   paned->child1_size = CLAMP (paned->child1_size,
1630                               paned->min_position,
1631                               paned->max_position);
1632
1633   if (paned->child1)
1634     gtk_widget_set_child_visible (paned->child1, paned->child1_size != 0);
1635   
1636   if (paned->child2)
1637     gtk_widget_set_child_visible (paned->child2, paned->child1_size != allocation); 
1638
1639   g_object_freeze_notify (G_OBJECT (paned));
1640   if (paned->child1_size != old_position)
1641     g_object_notify (G_OBJECT (paned), "position");
1642   if (paned->min_position != old_min_position)
1643     g_object_notify (G_OBJECT (paned), "min-position");
1644   if (paned->max_position != old_max_position)
1645     g_object_notify (G_OBJECT (paned), "max-position");
1646   g_object_thaw_notify (G_OBJECT (paned));
1647
1648   paned->last_allocation = allocation;
1649 }
1650
1651 static void
1652 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1653 {
1654   if (paned->priv->saved_focus)
1655     g_object_remove_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1656                                   (gpointer *)&(paned->priv->saved_focus));
1657
1658   paned->priv->saved_focus = widget;
1659
1660   if (paned->priv->saved_focus)
1661     g_object_add_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1662                                (gpointer *)&(paned->priv->saved_focus));
1663 }
1664
1665 static void
1666 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1667 {
1668   if (paned->priv->first_paned)
1669     g_object_remove_weak_pointer (G_OBJECT (paned->priv->first_paned),
1670                                   (gpointer *)&(paned->priv->first_paned));
1671
1672   paned->priv->first_paned = first_paned;
1673
1674   if (paned->priv->first_paned)
1675     g_object_add_weak_pointer (G_OBJECT (paned->priv->first_paned),
1676                                (gpointer *)&(paned->priv->first_paned));
1677 }
1678
1679 static void
1680 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1681 {
1682   if (paned->last_child1_focus)
1683     g_object_remove_weak_pointer (G_OBJECT (paned->last_child1_focus),
1684                                   (gpointer *)&(paned->last_child1_focus));
1685
1686   paned->last_child1_focus = widget;
1687
1688   if (paned->last_child1_focus)
1689     g_object_add_weak_pointer (G_OBJECT (paned->last_child1_focus),
1690                                (gpointer *)&(paned->last_child1_focus));
1691 }
1692
1693 static void
1694 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1695 {
1696   if (paned->last_child2_focus)
1697     g_object_remove_weak_pointer (G_OBJECT (paned->last_child2_focus),
1698                                   (gpointer *)&(paned->last_child2_focus));
1699
1700   paned->last_child2_focus = widget;
1701
1702   if (paned->last_child2_focus)
1703     g_object_add_weak_pointer (G_OBJECT (paned->last_child2_focus),
1704                                (gpointer *)&(paned->last_child2_focus));
1705 }
1706
1707 static GtkWidget *
1708 paned_get_focus_widget (GtkPaned *paned)
1709 {
1710   GtkWidget *toplevel;
1711
1712   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1713   if (gtk_widget_is_toplevel (toplevel))
1714     return GTK_WINDOW (toplevel)->focus_widget;
1715
1716   return NULL;
1717 }
1718
1719 static void
1720 gtk_paned_set_focus_child (GtkContainer *container,
1721                            GtkWidget    *focus_child)
1722 {
1723   GtkPaned *paned;
1724   
1725   g_return_if_fail (GTK_IS_PANED (container));
1726
1727   paned = GTK_PANED (container);
1728  
1729   if (focus_child == NULL)
1730     {
1731       GtkWidget *last_focus;
1732       GtkWidget *w;
1733       
1734       last_focus = paned_get_focus_widget (paned);
1735
1736       if (last_focus)
1737         {
1738           /* If there is one or more paned widgets between us and the
1739            * focus widget, we want the topmost of those as last_focus
1740            */
1741           for (w = last_focus; w != GTK_WIDGET (paned); w = w->parent)
1742             if (GTK_IS_PANED (w))
1743               last_focus = w;
1744           
1745           if (container->focus_child == paned->child1)
1746             gtk_paned_set_last_child1_focus (paned, last_focus);
1747           else if (container->focus_child == paned->child2)
1748             gtk_paned_set_last_child2_focus (paned, last_focus);
1749         }
1750     }
1751
1752   if (GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child)
1753     GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child (container, focus_child);
1754 }
1755
1756 static void
1757 gtk_paned_get_cycle_chain (GtkPaned          *paned,
1758                            GtkDirectionType   direction,
1759                            GList            **widgets)
1760 {
1761   GtkContainer *container = GTK_CONTAINER (paned);
1762   GtkWidget *ancestor = NULL;
1763   GList *temp_list = NULL;
1764   GList *list;
1765
1766   if (paned->in_recursion)
1767     return;
1768
1769   g_assert (widgets != NULL);
1770
1771   if (paned->last_child1_focus &&
1772       !gtk_widget_is_ancestor (paned->last_child1_focus, GTK_WIDGET (paned)))
1773     {
1774       gtk_paned_set_last_child1_focus (paned, NULL);
1775     }
1776
1777   if (paned->last_child2_focus &&
1778       !gtk_widget_is_ancestor (paned->last_child2_focus, GTK_WIDGET (paned)))
1779     {
1780       gtk_paned_set_last_child2_focus (paned, NULL);
1781     }
1782
1783   if (GTK_WIDGET (paned)->parent)
1784     ancestor = gtk_widget_get_ancestor (GTK_WIDGET (paned)->parent, GTK_TYPE_PANED);
1785
1786   /* The idea here is that temp_list is a list of widgets we want to cycle
1787    * to. The list is prioritized so that the first element is our first
1788    * choice, the next our second, and so on.
1789    *
1790    * We can't just use g_list_reverse(), because we want to try
1791    * paned->last_child?_focus before paned->child?, both when we
1792    * are going forward and backward.
1793    */
1794   if (direction == GTK_DIR_TAB_FORWARD)
1795     {
1796       if (container->focus_child == paned->child1)
1797         {
1798           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1799           temp_list = g_list_append (temp_list, paned->child2);
1800           temp_list = g_list_append (temp_list, ancestor);
1801         }
1802       else if (container->focus_child == paned->child2)
1803         {
1804           temp_list = g_list_append (temp_list, ancestor);
1805           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1806           temp_list = g_list_append (temp_list, paned->child1);
1807         }
1808       else
1809         {
1810           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1811           temp_list = g_list_append (temp_list, paned->child1);
1812           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1813           temp_list = g_list_append (temp_list, paned->child2);
1814           temp_list = g_list_append (temp_list, ancestor);
1815         }
1816     }
1817   else
1818     {
1819       if (container->focus_child == paned->child1)
1820         {
1821           temp_list = g_list_append (temp_list, ancestor);
1822           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1823           temp_list = g_list_append (temp_list, paned->child2);
1824         }
1825       else if (container->focus_child == paned->child2)
1826         {
1827           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1828           temp_list = g_list_append (temp_list, paned->child1);
1829           temp_list = g_list_append (temp_list, ancestor);
1830         }
1831       else
1832         {
1833           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1834           temp_list = g_list_append (temp_list, paned->child2);
1835           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1836           temp_list = g_list_append (temp_list, paned->child1);
1837           temp_list = g_list_append (temp_list, ancestor);
1838         }
1839     }
1840
1841   /* Walk the list and expand all the paned widgets. */
1842   for (list = temp_list; list != NULL; list = list->next)
1843     {
1844       GtkWidget *widget = list->data;
1845
1846       if (widget)
1847         {
1848           if (GTK_IS_PANED (widget))
1849             {
1850               paned->in_recursion = TRUE;
1851               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
1852               paned->in_recursion = FALSE;
1853             }
1854           else
1855             {
1856               *widgets = g_list_append (*widgets, widget);
1857             }
1858         }
1859     }
1860
1861   g_list_free (temp_list);
1862 }
1863
1864 static gboolean
1865 gtk_paned_cycle_child_focus (GtkPaned *paned,
1866                              gboolean  reversed)
1867 {
1868   GList *cycle_chain = NULL;
1869   GList *list;
1870   
1871   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
1872
1873   /* ignore f6 if the handle is focused */
1874   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1875     return TRUE;
1876   
1877   /* we can't just let the event propagate up the hierarchy,
1878    * because the paned will want to cycle focus _unless_ an
1879    * ancestor paned handles the event
1880    */
1881   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
1882
1883   for (list = cycle_chain; list != NULL; list = list->next)
1884     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
1885       break;
1886
1887   g_list_free (cycle_chain);
1888   
1889   return TRUE;
1890 }
1891
1892 static void
1893 get_child_panes (GtkWidget  *widget,
1894                  GList     **panes)
1895 {
1896   if (!widget || !GTK_WIDGET_REALIZED (widget))
1897     return;
1898
1899   if (GTK_IS_PANED (widget))
1900     {
1901       GtkPaned *paned = GTK_PANED (widget);
1902       
1903       get_child_panes (paned->child1, panes);
1904       *panes = g_list_prepend (*panes, widget);
1905       get_child_panes (paned->child2, panes);
1906     }
1907   else if (GTK_IS_CONTAINER (widget))
1908     {
1909       gtk_container_forall (GTK_CONTAINER (widget),
1910                             (GtkCallback)get_child_panes, panes);
1911     }
1912 }
1913
1914 static GList *
1915 get_all_panes (GtkPaned *paned)
1916 {
1917   GtkPaned *topmost = NULL;
1918   GList *result = NULL;
1919   GtkWidget *w;
1920   
1921   for (w = GTK_WIDGET (paned); w != NULL; w = w->parent)
1922     {
1923       if (GTK_IS_PANED (w))
1924         topmost = GTK_PANED (w);
1925     }
1926
1927   g_assert (topmost);
1928
1929   get_child_panes (GTK_WIDGET (topmost), &result);
1930
1931   return g_list_reverse (result);
1932 }
1933
1934 static void
1935 gtk_paned_find_neighbours (GtkPaned  *paned,
1936                            GtkPaned **next,
1937                            GtkPaned **prev)
1938 {
1939   GList *all_panes;
1940   GList *this_link;
1941
1942   all_panes = get_all_panes (paned);
1943   g_assert (all_panes);
1944
1945   this_link = g_list_find (all_panes, paned);
1946
1947   g_assert (this_link);
1948   
1949   if (this_link->next)
1950     *next = this_link->next->data;
1951   else
1952     *next = all_panes->data;
1953
1954   if (this_link->prev)
1955     *prev = this_link->prev->data;
1956   else
1957     *prev = g_list_last (all_panes)->data;
1958
1959   g_list_free (all_panes);
1960 }
1961
1962 static gboolean
1963 gtk_paned_move_handle (GtkPaned      *paned,
1964                        GtkScrollType  scroll)
1965 {
1966   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1967     {
1968       gint old_position;
1969       gint new_position;
1970       gint increment;
1971       
1972       enum {
1973         SINGLE_STEP_SIZE = 1,
1974         PAGE_STEP_SIZE   = 75
1975       };
1976       
1977       new_position = old_position = gtk_paned_get_position (paned);
1978       increment = 0;
1979       
1980       switch (scroll)
1981         {
1982         case GTK_SCROLL_STEP_LEFT:
1983         case GTK_SCROLL_STEP_UP:
1984         case GTK_SCROLL_STEP_BACKWARD:
1985           increment = - SINGLE_STEP_SIZE;
1986           break;
1987           
1988         case GTK_SCROLL_STEP_RIGHT:
1989         case GTK_SCROLL_STEP_DOWN:
1990         case GTK_SCROLL_STEP_FORWARD:
1991           increment = SINGLE_STEP_SIZE;
1992           break;
1993           
1994         case GTK_SCROLL_PAGE_LEFT:
1995         case GTK_SCROLL_PAGE_UP:
1996         case GTK_SCROLL_PAGE_BACKWARD:
1997           increment = - PAGE_STEP_SIZE;
1998           break;
1999           
2000         case GTK_SCROLL_PAGE_RIGHT:
2001         case GTK_SCROLL_PAGE_DOWN:
2002         case GTK_SCROLL_PAGE_FORWARD:
2003           increment = PAGE_STEP_SIZE;
2004           break;
2005           
2006         case GTK_SCROLL_START:
2007           new_position = paned->min_position;
2008           break;
2009           
2010         case GTK_SCROLL_END:
2011           new_position = paned->max_position;
2012           break;
2013
2014         default:
2015           break;
2016         }
2017
2018       if (increment)
2019         {
2020           if (is_rtl (paned))
2021             increment = -increment;
2022           
2023           new_position = old_position + increment;
2024         }
2025       
2026       new_position = CLAMP (new_position, paned->min_position, paned->max_position);
2027       
2028       if (old_position != new_position)
2029         gtk_paned_set_position (paned, new_position);
2030
2031       return TRUE;
2032     }
2033
2034   return FALSE;
2035 }
2036
2037 static void
2038 gtk_paned_restore_focus (GtkPaned *paned)
2039 {
2040   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2041     {
2042       if (paned->priv->saved_focus &&
2043           gtk_widget_get_sensitive (paned->priv->saved_focus))
2044         {
2045           gtk_widget_grab_focus (paned->priv->saved_focus);
2046         }
2047       else
2048         {
2049           /* the saved focus is somehow not available for focusing,
2050            * try
2051            *   1) tabbing into the paned widget
2052            * if that didn't work,
2053            *   2) unset focus for the window if there is one
2054            */
2055           
2056           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
2057             {
2058               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2059               
2060               if (GTK_IS_WINDOW (toplevel))
2061                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
2062             }
2063         }
2064       
2065       gtk_paned_set_saved_focus (paned, NULL);
2066       gtk_paned_set_first_paned (paned, NULL);
2067     }
2068 }
2069
2070 static gboolean
2071 gtk_paned_accept_position (GtkPaned *paned)
2072 {
2073   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2074     {
2075       paned->original_position = -1;
2076       gtk_paned_restore_focus (paned);
2077
2078       return TRUE;
2079     }
2080
2081   return FALSE;
2082 }
2083
2084
2085 static gboolean
2086 gtk_paned_cancel_position (GtkPaned *paned)
2087 {
2088   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2089     {
2090       if (paned->original_position != -1)
2091         {
2092           gtk_paned_set_position (paned, paned->original_position);
2093           paned->original_position = -1;
2094         }
2095
2096       gtk_paned_restore_focus (paned);
2097       return TRUE;
2098     }
2099
2100   return FALSE;
2101 }
2102
2103 static gboolean
2104 gtk_paned_cycle_handle_focus (GtkPaned *paned,
2105                               gboolean  reversed)
2106 {
2107   GtkPaned *next, *prev;
2108   
2109   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2110     {
2111       GtkPaned *focus = NULL;
2112
2113       if (!paned->priv->first_paned)
2114         {
2115           /* The first_pane has disappeared. As an ad-hoc solution,
2116            * we make the currently focused paned the first_paned. To the
2117            * user this will seem like the paned cycling has been reset.
2118            */
2119           
2120           gtk_paned_set_first_paned (paned, paned);
2121         }
2122       
2123       gtk_paned_find_neighbours (paned, &next, &prev);
2124
2125       if (reversed && prev &&
2126           prev != paned && paned != paned->priv->first_paned)
2127         {
2128           focus = prev;
2129         }
2130       else if (!reversed && next &&
2131                next != paned && next != paned->priv->first_paned)
2132         {
2133           focus = next;
2134         }
2135       else
2136         {
2137           gtk_paned_accept_position (paned);
2138           return TRUE;
2139         }
2140
2141       g_assert (focus);
2142       
2143       gtk_paned_set_saved_focus (focus, paned->priv->saved_focus);
2144       gtk_paned_set_first_paned (focus, paned->priv->first_paned);
2145       
2146       gtk_paned_set_saved_focus (paned, NULL);
2147       gtk_paned_set_first_paned (paned, NULL);
2148       
2149       gtk_widget_grab_focus (GTK_WIDGET (focus));
2150       
2151       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
2152         {
2153           paned->original_position = -1;
2154           focus->original_position = gtk_paned_get_position (focus);
2155         }
2156     }
2157   else
2158     {
2159       GtkContainer *container = GTK_CONTAINER (paned);
2160       GtkPaned *focus;
2161       GtkPaned *first;
2162       GtkPaned *prev, *next;
2163       GtkWidget *toplevel;
2164
2165       gtk_paned_find_neighbours (paned, &next, &prev);
2166
2167       if (container->focus_child == paned->child1)
2168         {
2169           if (reversed)
2170             {
2171               focus = prev;
2172               first = paned;
2173             }
2174           else
2175             {
2176               focus = paned;
2177               first = paned;
2178             }
2179         }
2180       else if (container->focus_child == paned->child2)
2181         {
2182           if (reversed)
2183             {
2184               focus = paned;
2185               first = next;
2186             }
2187           else
2188             {
2189               focus = next;
2190               first = next;
2191             }
2192         }
2193       else
2194         {
2195           /* Focus is not inside this paned, and we don't have focus.
2196            * Presumably this happened because the application wants us
2197            * to start keyboard navigating.
2198            */
2199           focus = paned;
2200
2201           if (reversed)
2202             first = paned;
2203           else
2204             first = next;
2205         }
2206
2207       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2208
2209       if (GTK_IS_WINDOW (toplevel))
2210         gtk_paned_set_saved_focus (focus, GTK_WINDOW (toplevel)->focus_widget);
2211       gtk_paned_set_first_paned (focus, first);
2212       focus->original_position = gtk_paned_get_position (focus); 
2213
2214       gtk_widget_grab_focus (GTK_WIDGET (focus));
2215    }
2216   
2217   return TRUE;
2218 }
2219
2220 static gboolean
2221 gtk_paned_toggle_handle_focus (GtkPaned *paned)
2222 {
2223   /* This function/signal has the wrong name. It is called when you
2224    * press Tab or Shift-Tab and what we do is act as if
2225    * the user pressed Return and then Tab or Shift-Tab
2226    */
2227   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2228     gtk_paned_accept_position (paned);
2229
2230   return FALSE;
2231 }
2232
2233 /**
2234  * gtk_paned_get_handle_window:
2235  * @panede: a #GtkPaned
2236  *
2237  * Returns the #GdkWindow of the handle. This function is
2238  * useful when handling button or motion events because it
2239  * enables the callback to distinguish between the window
2240  * of the paned, a child and the handle.
2241  *
2242  * Return value: the paned's handle window.
2243  *
2244  * Since: 2.20
2245  **/
2246 GdkWindow *
2247 gtk_paned_get_handle_window (GtkPaned *paned)
2248 {
2249   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2250
2251   return paned->handle;
2252 }
2253
2254 #define __GTK_PANED_C__
2255 #include "gtkaliasdef.c"