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