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