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