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