]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
c03d6e83461dded7b4ec832fbb47cd3a60da0343
[~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 (paned)->state);
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   gint x     = widget->allocation.x;
852   gint width = widget->allocation.width;
853
854   child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
855 }
856
857 static void
858 gtk_paned_size_allocate (GtkWidget     *widget,
859                          GtkAllocation *allocation)
860 {
861   GtkPaned *paned = GTK_PANED (widget);
862   GtkPanedPrivate *priv = paned->priv;
863   guint border_width;
864
865   border_width = gtk_container_get_border_width (GTK_CONTAINER (paned));
866
867   widget->allocation = *allocation;
868
869   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
870       priv->child2 && gtk_widget_get_visible (priv->child2))
871     {
872       GtkRequisition child1_requisition;
873       GtkRequisition child2_requisition;
874       GtkAllocation child1_allocation;
875       GtkAllocation child2_allocation;
876       GdkRectangle old_handle_pos;
877       gint handle_size;
878
879       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
880
881       gtk_widget_get_child_requisition (priv->child1, &child1_requisition);
882       gtk_widget_get_child_requisition (priv->child2, &child2_requisition);
883
884       old_handle_pos = priv->handle_pos;
885
886       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
887         {
888           gtk_paned_calc_position (paned,
889                                    MAX (1, widget->allocation.width
890                                         - handle_size
891                                         - 2 * border_width),
892                                    child1_requisition.width,
893                                    child2_requisition.width);
894
895           priv->handle_pos.x = widget->allocation.x + priv->child1_size + border_width;
896           priv->handle_pos.y = widget->allocation.y + border_width;
897           priv->handle_pos.width = handle_size;
898           priv->handle_pos.height = MAX (1, widget->allocation.height - 2 * border_width);
899
900           child1_allocation.height = child2_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
901           child1_allocation.width = MAX (1, priv->child1_size);
902           child1_allocation.x = widget->allocation.x + border_width;
903           child1_allocation.y = child2_allocation.y = widget->allocation.y + border_width;
904
905           child2_allocation.x = child1_allocation.x + priv->child1_size + priv->handle_pos.width;
906           child2_allocation.width = MAX (1, widget->allocation.x + widget->allocation.width - child2_allocation.x - border_width);
907
908           if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
909             {
910               flip_child (widget, &(child2_allocation));
911               flip_child (widget, &(child1_allocation));
912               flip_child (widget, &(priv->handle_pos));
913             }
914         }
915       else
916         {
917           gtk_paned_calc_position (paned,
918                                    MAX (1, widget->allocation.height
919                                         - handle_size
920                                         - 2 * border_width),
921                                    child1_requisition.height,
922                                    child2_requisition.height);
923
924           priv->handle_pos.x = widget->allocation.x + border_width;
925           priv->handle_pos.y = widget->allocation.y + priv->child1_size + border_width;
926           priv->handle_pos.width = MAX (1, (gint) widget->allocation.width - 2 * border_width);
927           priv->handle_pos.height = handle_size;
928
929           child1_allocation.width = child2_allocation.width = MAX (1, (gint) allocation->width - border_width * 2);
930           child1_allocation.height = MAX (1, priv->child1_size);
931           child1_allocation.x = child2_allocation.x = widget->allocation.x + border_width;
932           child1_allocation.y = widget->allocation.y + border_width;
933
934           child2_allocation.y = child1_allocation.y + priv->child1_size + priv->handle_pos.height;
935           child2_allocation.height = MAX (1, widget->allocation.y + widget->allocation.height - child2_allocation.y - border_width);
936         }
937
938       if (gtk_widget_get_mapped (widget) &&
939           (old_handle_pos.x != priv->handle_pos.x ||
940            old_handle_pos.y != priv->handle_pos.y ||
941            old_handle_pos.width != priv->handle_pos.width ||
942            old_handle_pos.height != priv->handle_pos.height))
943         {
944           gdk_window_invalidate_rect (widget->window, &old_handle_pos, FALSE);
945           gdk_window_invalidate_rect (widget->window, &priv->handle_pos, FALSE);
946         }
947
948       if (gtk_widget_get_realized (widget))
949         {
950           if (gtk_widget_get_mapped (widget))
951             gdk_window_show (priv->handle);
952
953           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
954             {
955               gdk_window_move_resize (priv->handle,
956                                       priv->handle_pos.x,
957                                       priv->handle_pos.y,
958                                       handle_size,
959                                       priv->handle_pos.height);
960             }
961           else
962             {
963               gdk_window_move_resize (priv->handle,
964                                       priv->handle_pos.x,
965                                       priv->handle_pos.y,
966                                       priv->handle_pos.width,
967                                       handle_size);
968             }
969         }
970
971       /* Now allocate the childen, making sure, when resizing not to
972        * overlap the windows
973        */
974       if (gtk_widget_get_mapped (widget) &&
975
976           ((priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
977             priv->child1->allocation.width < child1_allocation.width) ||
978
979            (priv->orientation == GTK_ORIENTATION_VERTICAL &&
980             priv->child1->allocation.height < child1_allocation.height)))
981         {
982           gtk_widget_size_allocate (priv->child2, &child2_allocation);
983           gtk_widget_size_allocate (priv->child1, &child1_allocation);
984         }
985       else
986         {
987           gtk_widget_size_allocate (priv->child1, &child1_allocation);
988           gtk_widget_size_allocate (priv->child2, &child2_allocation);
989         }
990     }
991   else
992     {
993       GtkAllocation child_allocation;
994
995       if (gtk_widget_get_realized (widget))
996         gdk_window_hide (priv->handle);
997
998       if (priv->child1)
999         gtk_widget_set_child_visible (priv->child1, TRUE);
1000       if (priv->child2)
1001         gtk_widget_set_child_visible (priv->child2, TRUE);
1002
1003       child_allocation.x = widget->allocation.x + border_width;
1004       child_allocation.y = widget->allocation.y + border_width;
1005       child_allocation.width = MAX (1, allocation->width - 2 * border_width);
1006       child_allocation.height = MAX (1, allocation->height - 2 * border_width);
1007
1008       if (priv->child1 && gtk_widget_get_visible (priv->child1))
1009         gtk_widget_size_allocate (priv->child1, &child_allocation);
1010       else if (priv->child2 && gtk_widget_get_visible (priv->child2))
1011         gtk_widget_size_allocate (priv->child2, &child_allocation);
1012     }
1013 }
1014
1015 static void
1016 gtk_paned_realize (GtkWidget *widget)
1017 {
1018   GtkPaned *paned = GTK_PANED (widget);
1019   GtkPanedPrivate *priv = paned->priv;
1020   GdkWindowAttr attributes;
1021   gint attributes_mask;
1022
1023   gtk_widget_set_realized (widget, TRUE);
1024
1025   widget->window = gtk_widget_get_parent_window (widget);
1026   g_object_ref (widget->window);
1027   
1028   attributes.window_type = GDK_WINDOW_CHILD;
1029   attributes.wclass = GDK_INPUT_ONLY;
1030   attributes.x = priv->handle_pos.x;
1031   attributes.y = priv->handle_pos.y;
1032   attributes.width = priv->handle_pos.width;
1033   attributes.height = priv->handle_pos.height;
1034   attributes.event_mask = gtk_widget_get_events (widget);
1035   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1036                             GDK_BUTTON_RELEASE_MASK |
1037                             GDK_ENTER_NOTIFY_MASK |
1038                             GDK_LEAVE_NOTIFY_MASK |
1039                             GDK_POINTER_MOTION_MASK |
1040                             GDK_POINTER_MOTION_HINT_MASK);
1041   attributes_mask = GDK_WA_X | GDK_WA_Y;
1042   if (gtk_widget_is_sensitive (widget))
1043     {
1044       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1045                                                       priv->cursor_type);
1046       attributes_mask |= GDK_WA_CURSOR;
1047     }
1048
1049   priv->handle = gdk_window_new (widget->window,
1050                                   &attributes, attributes_mask);
1051   gdk_window_set_user_data (priv->handle, paned);
1052   if (attributes_mask & GDK_WA_CURSOR)
1053     gdk_cursor_unref (attributes.cursor);
1054
1055   widget->style = gtk_style_attach (widget->style, widget->window);
1056
1057   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
1058       priv->child2 && gtk_widget_get_visible (priv->child2))
1059     gdk_window_show (priv->handle);
1060 }
1061
1062 static void
1063 gtk_paned_unrealize (GtkWidget *widget)
1064 {
1065   GtkPaned *paned = GTK_PANED (widget);
1066   GtkPanedPrivate *priv = paned->priv;
1067
1068   if (priv->handle)
1069     {
1070       gdk_window_set_user_data (priv->handle, NULL);
1071       gdk_window_destroy (priv->handle);
1072       priv->handle = NULL;
1073     }
1074
1075   gtk_paned_set_last_child1_focus (paned, NULL);
1076   gtk_paned_set_last_child2_focus (paned, NULL);
1077   gtk_paned_set_saved_focus (paned, NULL);
1078   gtk_paned_set_first_paned (paned, NULL);
1079
1080   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unrealize (widget);
1081 }
1082
1083 static void
1084 gtk_paned_map (GtkWidget *widget)
1085 {
1086   GtkPaned *paned = GTK_PANED (widget);
1087   GtkPanedPrivate *priv = paned->priv;
1088
1089   gdk_window_show (priv->handle);
1090
1091   GTK_WIDGET_CLASS (gtk_paned_parent_class)->map (widget);
1092 }
1093
1094 static void
1095 gtk_paned_unmap (GtkWidget *widget)
1096 {
1097   GtkPaned *paned = GTK_PANED (widget);
1098   GtkPanedPrivate *priv = paned->priv;
1099
1100   gdk_window_hide (priv->handle);
1101
1102   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unmap (widget);
1103 }
1104
1105 static gboolean
1106 gtk_paned_expose (GtkWidget      *widget,
1107                   GdkEventExpose *event)
1108 {
1109   GtkPaned *paned = GTK_PANED (widget);
1110   GtkPanedPrivate *priv = paned->priv;
1111
1112   if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
1113       priv->child1 && gtk_widget_get_visible (priv->child1) &&
1114       priv->child2 && gtk_widget_get_visible (priv->child2))
1115     {
1116       GtkStateType state;
1117       
1118       if (gtk_widget_is_focus (widget))
1119         state = GTK_STATE_SELECTED;
1120       else if (priv->handle_prelit)
1121         state = GTK_STATE_PRELIGHT;
1122       else
1123         state = gtk_widget_get_state (widget);
1124       
1125       gtk_paint_handle (widget->style, widget->window,
1126                         state, GTK_SHADOW_NONE,
1127                         &priv->handle_pos, widget, "paned",
1128                         priv->handle_pos.x, priv->handle_pos.y,
1129                         priv->handle_pos.width, priv->handle_pos.height,
1130                         !priv->orientation);
1131     }
1132
1133   /* Chain up to draw children */
1134   GTK_WIDGET_CLASS (gtk_paned_parent_class)->expose_event (widget, event);
1135   
1136   return FALSE;
1137 }
1138
1139 static gboolean
1140 is_rtl (GtkPaned *paned)
1141 {
1142   GtkPanedPrivate *priv = paned->priv;
1143
1144   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1145       gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL)
1146     {
1147       return TRUE;
1148     }
1149
1150   return FALSE;
1151 }
1152
1153 static void
1154 update_drag (GtkPaned *paned)
1155 {
1156   GtkPanedPrivate *priv = paned->priv;
1157   gint pos;
1158   gint handle_size;
1159   gint size;
1160
1161   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1162     gtk_widget_get_pointer (GTK_WIDGET (paned), &pos, NULL);
1163   else
1164     gtk_widget_get_pointer (GTK_WIDGET (paned), NULL, &pos);
1165
1166   pos -= priv->drag_pos;
1167
1168   if (is_rtl (paned))
1169     {
1170       gtk_widget_style_get (GTK_WIDGET (paned),
1171                             "handle-size", &handle_size,
1172                             NULL);
1173       
1174       size = GTK_WIDGET (paned)->allocation.width - pos - handle_size;
1175     }
1176   else
1177     {
1178       size = pos;
1179     }
1180
1181   size -= gtk_container_get_border_width (GTK_CONTAINER (paned));
1182   
1183   size = CLAMP (size, priv->min_position, priv->max_position);
1184
1185   if (size != priv->child1_size)
1186     gtk_paned_set_position (paned, size);
1187 }
1188
1189 static gboolean
1190 gtk_paned_enter (GtkWidget        *widget,
1191                  GdkEventCrossing *event)
1192 {
1193   GtkPaned *paned = GTK_PANED (widget);
1194   GtkPanedPrivate *priv = paned->priv;
1195
1196   if (priv->in_drag)
1197     update_drag (paned);
1198   else
1199     {
1200       priv->handle_prelit = TRUE;
1201       gtk_widget_queue_draw_area (widget,
1202                                   priv->handle_pos.x,
1203                                   priv->handle_pos.y,
1204                                   priv->handle_pos.width,
1205                                   priv->handle_pos.height);
1206     }
1207   
1208   return TRUE;
1209 }
1210
1211 static gboolean
1212 gtk_paned_leave (GtkWidget        *widget,
1213                  GdkEventCrossing *event)
1214 {
1215   GtkPaned *paned = GTK_PANED (widget);
1216   GtkPanedPrivate *priv = paned->priv;
1217
1218   if (priv->in_drag)
1219     update_drag (paned);
1220   else
1221     {
1222       priv->handle_prelit = FALSE;
1223       gtk_widget_queue_draw_area (widget,
1224                                   priv->handle_pos.x,
1225                                   priv->handle_pos.y,
1226                                   priv->handle_pos.width,
1227                                   priv->handle_pos.height);
1228     }
1229
1230   return TRUE;
1231 }
1232
1233 static gboolean
1234 gtk_paned_focus (GtkWidget        *widget,
1235                  GtkDirectionType  direction)
1236
1237 {
1238   gboolean retval;
1239   
1240   /* This is a hack, but how can this be done without
1241    * excessive cut-and-paste from gtkcontainer.c?
1242    */
1243
1244   gtk_widget_set_can_focus (widget, FALSE);
1245   retval = GTK_WIDGET_CLASS (gtk_paned_parent_class)->focus (widget, direction);
1246   gtk_widget_set_can_focus (widget, TRUE);
1247
1248   return retval;
1249 }
1250
1251 static gboolean
1252 gtk_paned_button_press (GtkWidget      *widget,
1253                         GdkEventButton *event)
1254 {
1255   GtkPaned *paned = GTK_PANED (widget);
1256   GtkPanedPrivate *priv = paned->priv;
1257
1258   if (!priv->in_drag &&
1259       (event->window == priv->handle) && (event->button == 1))
1260     {
1261       /* We need a server grab here, not gtk_grab_add(), since
1262        * we don't want to pass events on to the widget's children */
1263       if (gdk_device_grab (event->device,
1264                            priv->handle,
1265                            GDK_OWNERSHIP_WINDOW, FALSE,
1266                            GDK_POINTER_MOTION_HINT_MASK
1267                            | GDK_BUTTON1_MOTION_MASK
1268                            | GDK_BUTTON_RELEASE_MASK
1269                            | GDK_ENTER_NOTIFY_MASK
1270                            | GDK_LEAVE_NOTIFY_MASK,
1271                            NULL, event->time) != GDK_GRAB_SUCCESS)
1272         return FALSE;
1273
1274       priv->in_drag = TRUE;
1275       priv->grab_time = event->time;
1276       priv->grab_device = event->device;
1277
1278       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1279         priv->drag_pos = event->x;
1280       else
1281         priv->drag_pos = event->y;
1282
1283       return TRUE;
1284     }
1285
1286   return FALSE;
1287 }
1288
1289 static gboolean
1290 gtk_paned_grab_broken (GtkWidget          *widget,
1291                        GdkEventGrabBroken *event)
1292 {
1293   GtkPaned *paned = GTK_PANED (widget);
1294   GtkPanedPrivate *priv = paned->priv;
1295
1296   priv->in_drag = FALSE;
1297   priv->drag_pos = -1;
1298   priv->position_set = TRUE;
1299
1300   return TRUE;
1301 }
1302
1303 static void
1304 stop_drag (GtkPaned *paned)
1305 {
1306   GtkPanedPrivate *priv = paned->priv;
1307
1308   priv->in_drag = FALSE;
1309   priv->drag_pos = -1;
1310   priv->position_set = TRUE;
1311
1312   gdk_device_ungrab (priv->grab_device,
1313                      priv->grab_time);
1314   priv->grab_device = NULL;
1315 }
1316
1317 static void
1318 gtk_paned_grab_notify (GtkWidget *widget,
1319                        gboolean   was_grabbed)
1320 {
1321   GtkPaned *paned = GTK_PANED (widget);
1322   GtkPanedPrivate *priv = paned->priv;
1323   GdkDevice *grab_device;
1324
1325   grab_device = priv->grab_device;
1326
1327   if (priv->in_drag && grab_device &&
1328       gtk_widget_device_is_shadowed (widget, grab_device))
1329     stop_drag (paned);
1330 }
1331
1332 static void
1333 gtk_paned_state_changed (GtkWidget    *widget,
1334                          GtkStateType  previous_state)
1335 {
1336   GtkPaned *paned = GTK_PANED (widget);
1337   GtkPanedPrivate *priv = paned->priv;
1338   GdkCursor *cursor;
1339
1340   if (gtk_widget_get_realized (widget))
1341     {
1342       if (gtk_widget_is_sensitive (widget))
1343         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1344                                              priv->cursor_type);
1345       else
1346         cursor = NULL;
1347
1348       gdk_window_set_cursor (priv->handle, cursor);
1349
1350       if (cursor)
1351         gdk_cursor_unref (cursor);
1352     }
1353 }
1354
1355 static gboolean
1356 gtk_paned_button_release (GtkWidget      *widget,
1357                           GdkEventButton *event)
1358 {
1359   GtkPaned *paned = GTK_PANED (widget);
1360   GtkPanedPrivate *priv = paned->priv;
1361
1362   if (priv->in_drag && (event->button == 1))
1363     {
1364       stop_drag (paned);
1365
1366       return TRUE;
1367     }
1368
1369   return FALSE;
1370 }
1371
1372 static gboolean
1373 gtk_paned_motion (GtkWidget      *widget,
1374                   GdkEventMotion *event)
1375 {
1376   GtkPaned *paned = GTK_PANED (widget);
1377   GtkPanedPrivate *priv = paned->priv;
1378
1379   if (priv->in_drag)
1380     {
1381       update_drag (paned);
1382       return TRUE;
1383     }
1384   
1385   return FALSE;
1386 }
1387
1388 /**
1389  * gtk_paned_new:
1390  * @orientation: the paned's orientation.
1391  *
1392  * Creates a new #GtkPaned widget.
1393  *
1394  * Return value: a new #GtkPaned.
1395  *
1396  * Since: 3.0
1397  **/
1398 GtkWidget *
1399 gtk_paned_new (GtkOrientation orientation)
1400 {
1401   return g_object_new (GTK_TYPE_PANED,
1402                        "orientation", orientation,
1403                        NULL);
1404 }
1405
1406 void
1407 gtk_paned_add1 (GtkPaned  *paned,
1408                 GtkWidget *widget)
1409 {
1410   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
1411 }
1412
1413 void
1414 gtk_paned_add2 (GtkPaned  *paned,
1415                 GtkWidget *widget)
1416 {
1417   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
1418 }
1419
1420 void
1421 gtk_paned_pack1 (GtkPaned  *paned,
1422                  GtkWidget *child,
1423                  gboolean   resize,
1424                  gboolean   shrink)
1425 {
1426   GtkPanedPrivate *priv;
1427
1428   g_return_if_fail (GTK_IS_PANED (paned));
1429   g_return_if_fail (GTK_IS_WIDGET (child));
1430
1431   priv = paned->priv;
1432
1433   if (!priv->child1)
1434     {
1435       priv->child1 = child;
1436       priv->child1_resize = resize;
1437       priv->child1_shrink = shrink;
1438
1439       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1440     }
1441 }
1442
1443 void
1444 gtk_paned_pack2 (GtkPaned  *paned,
1445                  GtkWidget *child,
1446                  gboolean   resize,
1447                  gboolean   shrink)
1448 {
1449   GtkPanedPrivate *priv;
1450
1451   g_return_if_fail (GTK_IS_PANED (paned));
1452   g_return_if_fail (GTK_IS_WIDGET (child));
1453
1454   priv = paned->priv;
1455
1456   if (!priv->child2)
1457     {
1458       priv->child2 = child;
1459       priv->child2_resize = resize;
1460       priv->child2_shrink = shrink;
1461
1462       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1463     }
1464 }
1465
1466
1467 static void
1468 gtk_paned_add (GtkContainer *container,
1469                GtkWidget    *widget)
1470 {
1471   GtkPanedPrivate *priv;
1472   GtkPaned *paned;
1473
1474   g_return_if_fail (GTK_IS_PANED (container));
1475
1476   paned = GTK_PANED (container);
1477   priv = paned->priv;
1478
1479   if (!priv->child1)
1480     gtk_paned_add1 (paned, widget);
1481   else if (!priv->child2)
1482     gtk_paned_add2 (paned, widget);
1483   else
1484     g_warning ("GtkPaned cannot have more than 2 children\n");
1485 }
1486
1487 static void
1488 gtk_paned_remove (GtkContainer *container,
1489                   GtkWidget    *widget)
1490 {
1491   GtkPaned *paned = GTK_PANED (container);
1492   GtkPanedPrivate *priv = paned->priv;
1493   gboolean was_visible;
1494
1495   was_visible = gtk_widget_get_visible (widget);
1496
1497   if (priv->child1 == widget)
1498     {
1499       gtk_widget_unparent (widget);
1500
1501       priv->child1 = NULL;
1502
1503       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1504         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1505     }
1506   else if (priv->child2 == widget)
1507     {
1508       gtk_widget_unparent (widget);
1509
1510       priv->child2 = NULL;
1511
1512       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1513         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1514     }
1515 }
1516
1517 static void
1518 gtk_paned_forall (GtkContainer *container,
1519                   gboolean      include_internals,
1520                   GtkCallback   callback,
1521                   gpointer      callback_data)
1522 {
1523   GtkPanedPrivate *priv;
1524   GtkPaned *paned;
1525
1526   g_return_if_fail (callback != NULL);
1527
1528   paned = GTK_PANED (container);
1529   priv = paned->priv;
1530
1531   if (priv->child1)
1532     (*callback) (priv->child1, callback_data);
1533   if (priv->child2)
1534     (*callback) (priv->child2, callback_data);
1535 }
1536
1537 /**
1538  * gtk_paned_get_position:
1539  * @paned: a #GtkPaned widget
1540  * 
1541  * Obtains the position of the divider between the two panes.
1542  * 
1543  * Return value: position of the divider
1544  **/
1545 gint
1546 gtk_paned_get_position (GtkPaned  *paned)
1547 {
1548   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1549
1550   return paned->priv->child1_size;
1551 }
1552
1553 /**
1554  * gtk_paned_set_position:
1555  * @paned: a #GtkPaned widget
1556  * @position: pixel position of divider, a negative value means that the position
1557  *            is unset.
1558  * 
1559  * Sets the position of the divider between the two panes.
1560  **/
1561 void
1562 gtk_paned_set_position (GtkPaned *paned,
1563                         gint      position)
1564 {
1565   GtkPanedPrivate *priv;
1566   GObject *object;
1567
1568   g_return_if_fail (GTK_IS_PANED (paned));
1569
1570   priv = paned->priv;
1571
1572   if (priv->child1_size == position)
1573     return;
1574
1575   object = G_OBJECT (paned);
1576   
1577   if (position >= 0)
1578     {
1579       /* We don't clamp here - the assumption is that
1580        * if the total allocation changes at the same time
1581        * as the position, the position set is with reference
1582        * to the new total size. If only the position changes,
1583        * then clamping will occur in gtk_paned_calc_position()
1584        */
1585
1586       priv->child1_size = position;
1587       priv->position_set = TRUE;
1588     }
1589   else
1590     {
1591       priv->position_set = FALSE;
1592     }
1593
1594   g_object_freeze_notify (object);
1595   g_object_notify (object, "position");
1596   g_object_notify (object, "position-set");
1597   g_object_thaw_notify (object);
1598
1599   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
1600
1601 #ifdef G_OS_WIN32
1602   /* Hacky work-around for bug #144269 */
1603   if (priv->child2 != NULL)
1604     {
1605       gtk_widget_queue_draw (priv->child2);
1606     }
1607 #endif
1608 }
1609
1610 /**
1611  * gtk_paned_get_child1:
1612  * @paned: a #GtkPaned widget
1613  * 
1614  * Obtains the first child of the paned widget.
1615  * 
1616  * Return value: first child, or %NULL if it is not set.
1617  *
1618  * Since: 2.4
1619  **/
1620 GtkWidget *
1621 gtk_paned_get_child1 (GtkPaned *paned)
1622 {
1623   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1624
1625   return paned->priv->child1;
1626 }
1627
1628 /**
1629  * gtk_paned_get_child2:
1630  * @paned: a #GtkPaned widget
1631  * 
1632  * Obtains the second child of the paned widget.
1633  * 
1634  * Return value: second child, or %NULL if it is not set.
1635  *
1636  * Since: 2.4
1637  **/
1638 GtkWidget *
1639 gtk_paned_get_child2 (GtkPaned *paned)
1640 {
1641   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1642
1643   return paned->priv->child2;
1644 }
1645
1646 static void
1647 gtk_paned_calc_position (GtkPaned *paned,
1648                          gint      allocation,
1649                          gint      child1_req,
1650                          gint      child2_req)
1651 {
1652   GtkPanedPrivate *priv = paned->priv;
1653   gint old_position;
1654   gint old_min_position;
1655   gint old_max_position;
1656
1657   old_position = priv->child1_size;
1658   old_min_position = priv->min_position;
1659   old_max_position = priv->max_position;
1660
1661   priv->min_position = priv->child1_shrink ? 0 : child1_req;
1662
1663   priv->max_position = allocation;
1664   if (!priv->child2_shrink)
1665     priv->max_position = MAX (1, priv->max_position - child2_req);
1666   priv->max_position = MAX (priv->min_position, priv->max_position);
1667
1668   if (!priv->position_set)
1669     {
1670       if (priv->child1_resize && !priv->child2_resize)
1671         priv->child1_size = MAX (0, allocation - child2_req);
1672       else if (!priv->child1_resize && priv->child2_resize)
1673         priv->child1_size = child1_req;
1674       else if (child1_req + child2_req != 0)
1675         priv->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
1676       else
1677         priv->child1_size = allocation * 0.5 + 0.5;
1678     }
1679   else
1680     {
1681       /* If the position was set before the initial allocation.
1682        * (priv->last_allocation <= 0) just clamp it and leave it.
1683        */
1684       if (priv->last_allocation > 0)
1685         {
1686           if (priv->child1_resize && !priv->child2_resize)
1687             priv->child1_size += allocation - priv->last_allocation;
1688           else if (!(!priv->child1_resize && priv->child2_resize))
1689             priv->child1_size = allocation * ((gdouble) priv->child1_size / (priv->last_allocation)) + 0.5;
1690         }
1691     }
1692
1693   priv->child1_size = CLAMP (priv->child1_size,
1694                               priv->min_position,
1695                               priv->max_position);
1696
1697   if (priv->child1)
1698     gtk_widget_set_child_visible (priv->child1, priv->child1_size != 0);
1699   
1700   if (priv->child2)
1701     gtk_widget_set_child_visible (priv->child2, priv->child1_size != allocation); 
1702
1703   g_object_freeze_notify (G_OBJECT (paned));
1704   if (priv->child1_size != old_position)
1705     g_object_notify (G_OBJECT (paned), "position");
1706   if (priv->min_position != old_min_position)
1707     g_object_notify (G_OBJECT (paned), "min-position");
1708   if (priv->max_position != old_max_position)
1709     g_object_notify (G_OBJECT (paned), "max-position");
1710   g_object_thaw_notify (G_OBJECT (paned));
1711
1712   priv->last_allocation = allocation;
1713 }
1714
1715 static void
1716 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1717 {
1718   GtkPanedPrivate *priv = paned->priv;
1719
1720   if (priv->saved_focus)
1721     g_object_remove_weak_pointer (G_OBJECT (priv->saved_focus),
1722                                   (gpointer *)&(priv->saved_focus));
1723
1724   priv->saved_focus = widget;
1725
1726   if (priv->saved_focus)
1727     g_object_add_weak_pointer (G_OBJECT (priv->saved_focus),
1728                                (gpointer *)&(priv->saved_focus));
1729 }
1730
1731 static void
1732 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1733 {
1734   GtkPanedPrivate *priv = paned->priv;
1735
1736   if (priv->first_paned)
1737     g_object_remove_weak_pointer (G_OBJECT (priv->first_paned),
1738                                   (gpointer *)&(priv->first_paned));
1739
1740   priv->first_paned = first_paned;
1741
1742   if (priv->first_paned)
1743     g_object_add_weak_pointer (G_OBJECT (priv->first_paned),
1744                                (gpointer *)&(priv->first_paned));
1745 }
1746
1747 static void
1748 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1749 {
1750   GtkPanedPrivate *priv = paned->priv;
1751
1752   if (priv->last_child1_focus)
1753     g_object_remove_weak_pointer (G_OBJECT (priv->last_child1_focus),
1754                                   (gpointer *)&(priv->last_child1_focus));
1755
1756   priv->last_child1_focus = widget;
1757
1758   if (priv->last_child1_focus)
1759     g_object_add_weak_pointer (G_OBJECT (priv->last_child1_focus),
1760                                (gpointer *)&(priv->last_child1_focus));
1761 }
1762
1763 static void
1764 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1765 {
1766   GtkPanedPrivate *priv = paned->priv;
1767
1768   if (priv->last_child2_focus)
1769     g_object_remove_weak_pointer (G_OBJECT (priv->last_child2_focus),
1770                                   (gpointer *)&(priv->last_child2_focus));
1771
1772   priv->last_child2_focus = widget;
1773
1774   if (priv->last_child2_focus)
1775     g_object_add_weak_pointer (G_OBJECT (priv->last_child2_focus),
1776                                (gpointer *)&(priv->last_child2_focus));
1777 }
1778
1779 static GtkWidget *
1780 paned_get_focus_widget (GtkPaned *paned)
1781 {
1782   GtkWidget *toplevel;
1783
1784   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1785   if (gtk_widget_is_toplevel (toplevel))
1786     return GTK_WINDOW (toplevel)->focus_widget;
1787
1788   return NULL;
1789 }
1790
1791 static void
1792 gtk_paned_set_focus_child (GtkContainer *container,
1793                            GtkWidget    *focus_child)
1794 {
1795   GtkPaned *paned;
1796   GtkPanedPrivate *priv;
1797   GtkWidget *container_focus_child;
1798
1799   g_return_if_fail (GTK_IS_PANED (container));
1800
1801   paned = GTK_PANED (container);
1802   priv = paned->priv;
1803
1804   if (focus_child == NULL)
1805     {
1806       GtkWidget *last_focus;
1807       GtkWidget *w;
1808       
1809       last_focus = paned_get_focus_widget (paned);
1810
1811       if (last_focus)
1812         {
1813           /* If there is one or more paned widgets between us and the
1814            * focus widget, we want the topmost of those as last_focus
1815            */
1816           for (w = last_focus; w != GTK_WIDGET (paned); w = w->parent)
1817             if (GTK_IS_PANED (w))
1818               last_focus = w;
1819
1820           container_focus_child = gtk_container_get_focus_child (container);
1821           if (container_focus_child == priv->child1)
1822             gtk_paned_set_last_child1_focus (paned, last_focus);
1823           else if (container_focus_child == priv->child2)
1824             gtk_paned_set_last_child2_focus (paned, last_focus);
1825         }
1826     }
1827
1828   if (GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child)
1829     GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child (container, focus_child);
1830 }
1831
1832 static void
1833 gtk_paned_get_cycle_chain (GtkPaned          *paned,
1834                            GtkDirectionType   direction,
1835                            GList            **widgets)
1836 {
1837   GtkPanedPrivate *priv = paned->priv;
1838   GtkContainer *container = GTK_CONTAINER (paned);
1839   GtkWidget *ancestor = NULL;
1840   GtkWidget *focus_child;
1841   GList *temp_list = NULL;
1842   GList *list;
1843
1844   if (priv->in_recursion)
1845     return;
1846
1847   g_assert (widgets != NULL);
1848
1849   if (priv->last_child1_focus &&
1850       !gtk_widget_is_ancestor (priv->last_child1_focus, GTK_WIDGET (paned)))
1851     {
1852       gtk_paned_set_last_child1_focus (paned, NULL);
1853     }
1854
1855   if (priv->last_child2_focus &&
1856       !gtk_widget_is_ancestor (priv->last_child2_focus, GTK_WIDGET (paned)))
1857     {
1858       gtk_paned_set_last_child2_focus (paned, NULL);
1859     }
1860
1861   if (GTK_WIDGET (paned)->parent)
1862     ancestor = gtk_widget_get_ancestor (GTK_WIDGET (paned)->parent, GTK_TYPE_PANED);
1863
1864   /* The idea here is that temp_list is a list of widgets we want to cycle
1865    * to. The list is prioritized so that the first element is our first
1866    * choice, the next our second, and so on.
1867    *
1868    * We can't just use g_list_reverse(), because we want to try
1869    * priv->last_child?_focus before priv->child?, both when we
1870    * are going forward and backward.
1871    */
1872   focus_child = gtk_container_get_focus_child (container);
1873   if (direction == GTK_DIR_TAB_FORWARD)
1874     {
1875       if (focus_child == priv->child1)
1876         {
1877           temp_list = g_list_append (temp_list, priv->last_child2_focus);
1878           temp_list = g_list_append (temp_list, priv->child2);
1879           temp_list = g_list_append (temp_list, ancestor);
1880         }
1881       else if (focus_child == priv->child2)
1882         {
1883           temp_list = g_list_append (temp_list, ancestor);
1884           temp_list = g_list_append (temp_list, priv->last_child1_focus);
1885           temp_list = g_list_append (temp_list, priv->child1);
1886         }
1887       else
1888         {
1889           temp_list = g_list_append (temp_list, priv->last_child1_focus);
1890           temp_list = g_list_append (temp_list, priv->child1);
1891           temp_list = g_list_append (temp_list, priv->last_child2_focus);
1892           temp_list = g_list_append (temp_list, priv->child2);
1893           temp_list = g_list_append (temp_list, ancestor);
1894         }
1895     }
1896   else
1897     {
1898       if (focus_child == priv->child1)
1899         {
1900           temp_list = g_list_append (temp_list, ancestor);
1901           temp_list = g_list_append (temp_list, priv->last_child2_focus);
1902           temp_list = g_list_append (temp_list, priv->child2);
1903         }
1904       else if (focus_child == priv->child2)
1905         {
1906           temp_list = g_list_append (temp_list, priv->last_child1_focus);
1907           temp_list = g_list_append (temp_list, priv->child1);
1908           temp_list = g_list_append (temp_list, ancestor);
1909         }
1910       else
1911         {
1912           temp_list = g_list_append (temp_list, priv->last_child2_focus);
1913           temp_list = g_list_append (temp_list, priv->child2);
1914           temp_list = g_list_append (temp_list, priv->last_child1_focus);
1915           temp_list = g_list_append (temp_list, priv->child1);
1916           temp_list = g_list_append (temp_list, ancestor);
1917         }
1918     }
1919
1920   /* Walk the list and expand all the paned widgets. */
1921   for (list = temp_list; list != NULL; list = list->next)
1922     {
1923       GtkWidget *widget = list->data;
1924
1925       if (widget)
1926         {
1927           if (GTK_IS_PANED (widget))
1928             {
1929               priv->in_recursion = TRUE;
1930               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
1931               priv->in_recursion = FALSE;
1932             }
1933           else
1934             {
1935               *widgets = g_list_append (*widgets, widget);
1936             }
1937         }
1938     }
1939
1940   g_list_free (temp_list);
1941 }
1942
1943 static gboolean
1944 gtk_paned_cycle_child_focus (GtkPaned *paned,
1945                              gboolean  reversed)
1946 {
1947   GList *cycle_chain = NULL;
1948   GList *list;
1949   
1950   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
1951
1952   /* ignore f6 if the handle is focused */
1953   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1954     return TRUE;
1955   
1956   /* we can't just let the event propagate up the hierarchy,
1957    * because the paned will want to cycle focus _unless_ an
1958    * ancestor paned handles the event
1959    */
1960   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
1961
1962   for (list = cycle_chain; list != NULL; list = list->next)
1963     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
1964       break;
1965
1966   g_list_free (cycle_chain);
1967   
1968   return TRUE;
1969 }
1970
1971 static void
1972 get_child_panes (GtkWidget  *widget,
1973                  GList     **panes)
1974 {
1975   if (!widget || !gtk_widget_get_realized (widget))
1976     return;
1977
1978   if (GTK_IS_PANED (widget))
1979     {
1980       GtkPaned *paned = GTK_PANED (widget);
1981       GtkPanedPrivate *priv = paned->priv;
1982
1983       get_child_panes (priv->child1, panes);
1984       *panes = g_list_prepend (*panes, widget);
1985       get_child_panes (priv->child2, panes);
1986     }
1987   else if (GTK_IS_CONTAINER (widget))
1988     {
1989       gtk_container_forall (GTK_CONTAINER (widget),
1990                             (GtkCallback)get_child_panes, panes);
1991     }
1992 }
1993
1994 static GList *
1995 get_all_panes (GtkPaned *paned)
1996 {
1997   GtkPaned *topmost = NULL;
1998   GList *result = NULL;
1999   GtkWidget *w;
2000   
2001   for (w = GTK_WIDGET (paned); w != NULL; w = w->parent)
2002     {
2003       if (GTK_IS_PANED (w))
2004         topmost = GTK_PANED (w);
2005     }
2006
2007   g_assert (topmost);
2008
2009   get_child_panes (GTK_WIDGET (topmost), &result);
2010
2011   return g_list_reverse (result);
2012 }
2013
2014 static void
2015 gtk_paned_find_neighbours (GtkPaned  *paned,
2016                            GtkPaned **next,
2017                            GtkPaned **prev)
2018 {
2019   GList *all_panes;
2020   GList *this_link;
2021
2022   all_panes = get_all_panes (paned);
2023   g_assert (all_panes);
2024
2025   this_link = g_list_find (all_panes, paned);
2026
2027   g_assert (this_link);
2028   
2029   if (this_link->next)
2030     *next = this_link->next->data;
2031   else
2032     *next = all_panes->data;
2033
2034   if (this_link->prev)
2035     *prev = this_link->prev->data;
2036   else
2037     *prev = g_list_last (all_panes)->data;
2038
2039   g_list_free (all_panes);
2040 }
2041
2042 static gboolean
2043 gtk_paned_move_handle (GtkPaned      *paned,
2044                        GtkScrollType  scroll)
2045 {
2046   GtkPanedPrivate *priv = paned->priv;
2047
2048   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2049     {
2050       gint old_position;
2051       gint new_position;
2052       gint increment;
2053       
2054       enum {
2055         SINGLE_STEP_SIZE = 1,
2056         PAGE_STEP_SIZE   = 75
2057       };
2058       
2059       new_position = old_position = gtk_paned_get_position (paned);
2060       increment = 0;
2061       
2062       switch (scroll)
2063         {
2064         case GTK_SCROLL_STEP_LEFT:
2065         case GTK_SCROLL_STEP_UP:
2066         case GTK_SCROLL_STEP_BACKWARD:
2067           increment = - SINGLE_STEP_SIZE;
2068           break;
2069           
2070         case GTK_SCROLL_STEP_RIGHT:
2071         case GTK_SCROLL_STEP_DOWN:
2072         case GTK_SCROLL_STEP_FORWARD:
2073           increment = SINGLE_STEP_SIZE;
2074           break;
2075           
2076         case GTK_SCROLL_PAGE_LEFT:
2077         case GTK_SCROLL_PAGE_UP:
2078         case GTK_SCROLL_PAGE_BACKWARD:
2079           increment = - PAGE_STEP_SIZE;
2080           break;
2081           
2082         case GTK_SCROLL_PAGE_RIGHT:
2083         case GTK_SCROLL_PAGE_DOWN:
2084         case GTK_SCROLL_PAGE_FORWARD:
2085           increment = PAGE_STEP_SIZE;
2086           break;
2087           
2088         case GTK_SCROLL_START:
2089           new_position = priv->min_position;
2090           break;
2091           
2092         case GTK_SCROLL_END:
2093           new_position = priv->max_position;
2094           break;
2095
2096         default:
2097           break;
2098         }
2099
2100       if (increment)
2101         {
2102           if (is_rtl (paned))
2103             increment = -increment;
2104           
2105           new_position = old_position + increment;
2106         }
2107       
2108       new_position = CLAMP (new_position, priv->min_position, priv->max_position);
2109       
2110       if (old_position != new_position)
2111         gtk_paned_set_position (paned, new_position);
2112
2113       return TRUE;
2114     }
2115
2116   return FALSE;
2117 }
2118
2119 static void
2120 gtk_paned_restore_focus (GtkPaned *paned)
2121 {
2122   GtkPanedPrivate *priv = paned->priv;
2123
2124   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2125     {
2126       if (priv->saved_focus &&
2127           gtk_widget_get_sensitive (priv->saved_focus))
2128         {
2129           gtk_widget_grab_focus (priv->saved_focus);
2130         }
2131       else
2132         {
2133           /* the saved focus is somehow not available for focusing,
2134            * try
2135            *   1) tabbing into the paned widget
2136            * if that didn't work,
2137            *   2) unset focus for the window if there is one
2138            */
2139           
2140           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
2141             {
2142               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2143               
2144               if (GTK_IS_WINDOW (toplevel))
2145                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
2146             }
2147         }
2148       
2149       gtk_paned_set_saved_focus (paned, NULL);
2150       gtk_paned_set_first_paned (paned, NULL);
2151     }
2152 }
2153
2154 static gboolean
2155 gtk_paned_accept_position (GtkPaned *paned)
2156 {
2157   GtkPanedPrivate *priv = paned->priv;
2158
2159   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2160     {
2161       priv->original_position = -1;
2162       gtk_paned_restore_focus (paned);
2163
2164       return TRUE;
2165     }
2166
2167   return FALSE;
2168 }
2169
2170
2171 static gboolean
2172 gtk_paned_cancel_position (GtkPaned *paned)
2173 {
2174   GtkPanedPrivate *priv = paned->priv;
2175
2176   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2177     {
2178       if (priv->original_position != -1)
2179         {
2180           gtk_paned_set_position (paned, priv->original_position);
2181           priv->original_position = -1;
2182         }
2183
2184       gtk_paned_restore_focus (paned);
2185       return TRUE;
2186     }
2187
2188   return FALSE;
2189 }
2190
2191 static gboolean
2192 gtk_paned_cycle_handle_focus (GtkPaned *paned,
2193                               gboolean  reversed)
2194 {
2195   GtkPanedPrivate *priv = paned->priv;
2196   GtkPaned *next, *prev;
2197
2198   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2199     {
2200       GtkPaned *focus = NULL;
2201
2202       if (!priv->first_paned)
2203         {
2204           /* The first_pane has disappeared. As an ad-hoc solution,
2205            * we make the currently focused paned the first_paned. To the
2206            * user this will seem like the paned cycling has been reset.
2207            */
2208           
2209           gtk_paned_set_first_paned (paned, paned);
2210         }
2211       
2212       gtk_paned_find_neighbours (paned, &next, &prev);
2213
2214       if (reversed && prev &&
2215           prev != paned && paned != priv->first_paned)
2216         {
2217           focus = prev;
2218         }
2219       else if (!reversed && next &&
2220                next != paned && next != priv->first_paned)
2221         {
2222           focus = next;
2223         }
2224       else
2225         {
2226           gtk_paned_accept_position (paned);
2227           return TRUE;
2228         }
2229
2230       g_assert (focus);
2231       
2232       gtk_paned_set_saved_focus (focus, priv->saved_focus);
2233       gtk_paned_set_first_paned (focus, priv->first_paned);
2234       
2235       gtk_paned_set_saved_focus (paned, NULL);
2236       gtk_paned_set_first_paned (paned, NULL);
2237       
2238       gtk_widget_grab_focus (GTK_WIDGET (focus));
2239       
2240       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
2241         {
2242           priv->original_position = -1;
2243           focus->priv->original_position = gtk_paned_get_position (focus);
2244         }
2245     }
2246   else
2247     {
2248       GtkContainer *container = GTK_CONTAINER (paned);
2249       GtkPaned *focus;
2250       GtkPaned *first;
2251       GtkPaned *prev, *next;
2252       GtkWidget *toplevel;
2253       GtkWidget *focus_child;
2254
2255       gtk_paned_find_neighbours (paned, &next, &prev);
2256       focus_child = gtk_container_get_focus_child (container);
2257
2258       if (focus_child == priv->child1)
2259         {
2260           if (reversed)
2261             {
2262               focus = prev;
2263               first = paned;
2264             }
2265           else
2266             {
2267               focus = paned;
2268               first = paned;
2269             }
2270         }
2271       else if (focus_child == priv->child2)
2272         {
2273           if (reversed)
2274             {
2275               focus = paned;
2276               first = next;
2277             }
2278           else
2279             {
2280               focus = next;
2281               first = next;
2282             }
2283         }
2284       else
2285         {
2286           /* Focus is not inside this paned, and we don't have focus.
2287            * Presumably this happened because the application wants us
2288            * to start keyboard navigating.
2289            */
2290           focus = paned;
2291
2292           if (reversed)
2293             first = paned;
2294           else
2295             first = next;
2296         }
2297
2298       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2299
2300       if (GTK_IS_WINDOW (toplevel))
2301         gtk_paned_set_saved_focus (focus, GTK_WINDOW (toplevel)->focus_widget);
2302       gtk_paned_set_first_paned (focus, first);
2303       focus->priv->original_position = gtk_paned_get_position (focus);
2304
2305       gtk_widget_grab_focus (GTK_WIDGET (focus));
2306    }
2307
2308   return TRUE;
2309 }
2310
2311 static gboolean
2312 gtk_paned_toggle_handle_focus (GtkPaned *paned)
2313 {
2314   /* This function/signal has the wrong name. It is called when you
2315    * press Tab or Shift-Tab and what we do is act as if
2316    * the user pressed Return and then Tab or Shift-Tab
2317    */
2318   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2319     gtk_paned_accept_position (paned);
2320
2321   return FALSE;
2322 }
2323
2324 /**
2325  * gtk_paned_get_handle_window:
2326  * @paned: a #GtkPaned
2327  *
2328  * Returns the #GdkWindow of the handle. This function is
2329  * useful when handling button or motion events because it
2330  * enables the callback to distinguish between the window
2331  * of the paned, a child and the handle.
2332  *
2333  * Return value: the paned's handle window.
2334  *
2335  * Since: 2.20
2336  **/
2337 GdkWindow *
2338 gtk_paned_get_handle_window (GtkPaned *paned)
2339 {
2340   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2341
2342   return paned->priv->handle;
2343 }