]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
a60718d84e5cde4b58e1663a0249d8acca2d3ceb
[~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_flags_changed   (GtkWidget        *widget,
186                                                  GtkStateFlags     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_flags_changed = gtk_paned_state_flags_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_flags_changed updates the cursor */
710       gtk_paned_state_flags_changed (GTK_WIDGET (paned), 0);
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     g_object_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       GtkStyleContext *context;
1201       GtkStateFlags state;
1202       GtkAllocation allocation;
1203
1204       gtk_widget_get_allocation (widget, &allocation);
1205       context = gtk_widget_get_style_context (widget);
1206       state = gtk_widget_get_state_flags (widget);
1207
1208       if (gtk_widget_is_focus (widget))
1209         state |= GTK_STATE_FLAG_SELECTED;
1210       if (priv->handle_prelit)
1211         state |= GTK_STATE_FLAG_PRELIGHT;
1212
1213       gtk_style_context_save (context);
1214       gtk_style_context_set_state (context, state);
1215       gtk_render_handle (context, cr,
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
1221       gtk_style_context_restore (context);
1222     }
1223
1224   /* Chain up to draw children */
1225   GTK_WIDGET_CLASS (gtk_paned_parent_class)->draw (widget, cr);
1226   
1227   return FALSE;
1228 }
1229
1230 static gboolean
1231 is_rtl (GtkPaned *paned)
1232 {
1233   GtkPanedPrivate *priv = paned->priv;
1234
1235   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1236       gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL)
1237     {
1238       return TRUE;
1239     }
1240
1241   return FALSE;
1242 }
1243
1244 static void
1245 update_drag (GtkPaned *paned)
1246 {
1247   GtkPanedPrivate *priv = paned->priv;
1248   GtkAllocation allocation;
1249   GtkWidget *widget = GTK_WIDGET (paned);
1250   gint pos;
1251   gint handle_size;
1252   gint size;
1253
1254   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1255     gtk_widget_get_pointer (widget, &pos, NULL);
1256   else
1257     gtk_widget_get_pointer (widget, NULL, &pos);
1258
1259   pos -= priv->drag_pos;
1260
1261   if (is_rtl (paned))
1262     {
1263       gtk_widget_style_get (widget,
1264                             "handle-size", &handle_size,
1265                             NULL);
1266
1267       gtk_widget_get_allocation (widget, &allocation);
1268       size = allocation.width - pos - handle_size;
1269     }
1270   else
1271     {
1272       size = pos;
1273     }
1274
1275   size = CLAMP (size, priv->min_position, priv->max_position);
1276
1277   if (size != priv->child1_size)
1278     gtk_paned_set_position (paned, size);
1279 }
1280
1281 static gboolean
1282 gtk_paned_enter (GtkWidget        *widget,
1283                  GdkEventCrossing *event)
1284 {
1285   GtkPaned *paned = GTK_PANED (widget);
1286   GtkPanedPrivate *priv = paned->priv;
1287
1288   if (priv->in_drag)
1289     update_drag (paned);
1290   else
1291     {
1292       priv->handle_prelit = TRUE;
1293       gtk_widget_queue_draw_area (widget,
1294                                   priv->handle_pos.x,
1295                                   priv->handle_pos.y,
1296                                   priv->handle_pos.width,
1297                                   priv->handle_pos.height);
1298     }
1299   
1300   return TRUE;
1301 }
1302
1303 static gboolean
1304 gtk_paned_leave (GtkWidget        *widget,
1305                  GdkEventCrossing *event)
1306 {
1307   GtkPaned *paned = GTK_PANED (widget);
1308   GtkPanedPrivate *priv = paned->priv;
1309
1310   if (priv->in_drag)
1311     update_drag (paned);
1312   else
1313     {
1314       priv->handle_prelit = FALSE;
1315       gtk_widget_queue_draw_area (widget,
1316                                   priv->handle_pos.x,
1317                                   priv->handle_pos.y,
1318                                   priv->handle_pos.width,
1319                                   priv->handle_pos.height);
1320     }
1321
1322   return TRUE;
1323 }
1324
1325 static gboolean
1326 gtk_paned_focus (GtkWidget        *widget,
1327                  GtkDirectionType  direction)
1328
1329 {
1330   gboolean retval;
1331   
1332   /* This is a hack, but how can this be done without
1333    * excessive cut-and-paste from gtkcontainer.c?
1334    */
1335
1336   gtk_widget_set_can_focus (widget, FALSE);
1337   retval = GTK_WIDGET_CLASS (gtk_paned_parent_class)->focus (widget, direction);
1338   gtk_widget_set_can_focus (widget, TRUE);
1339
1340   return retval;
1341 }
1342
1343 static gboolean
1344 gtk_paned_button_press (GtkWidget      *widget,
1345                         GdkEventButton *event)
1346 {
1347   GtkPaned *paned = GTK_PANED (widget);
1348   GtkPanedPrivate *priv = paned->priv;
1349
1350   if (!priv->in_drag &&
1351       (event->window == priv->handle) && (event->button == 1))
1352     {
1353       /* We need a server grab here, not gtk_grab_add(), since
1354        * we don't want to pass events on to the widget's children */
1355       if (gdk_device_grab (event->device,
1356                            priv->handle,
1357                            GDK_OWNERSHIP_WINDOW, FALSE,
1358                            GDK_POINTER_MOTION_HINT_MASK
1359                            | GDK_BUTTON1_MOTION_MASK
1360                            | GDK_BUTTON_RELEASE_MASK
1361                            | GDK_ENTER_NOTIFY_MASK
1362                            | GDK_LEAVE_NOTIFY_MASK,
1363                            NULL, event->time) != GDK_GRAB_SUCCESS)
1364         return FALSE;
1365
1366       priv->in_drag = TRUE;
1367       priv->grab_time = event->time;
1368       priv->grab_device = event->device;
1369
1370       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1371         priv->drag_pos = event->x;
1372       else
1373         priv->drag_pos = event->y;
1374
1375       return TRUE;
1376     }
1377
1378   return FALSE;
1379 }
1380
1381 static gboolean
1382 gtk_paned_grab_broken (GtkWidget          *widget,
1383                        GdkEventGrabBroken *event)
1384 {
1385   GtkPaned *paned = GTK_PANED (widget);
1386   GtkPanedPrivate *priv = paned->priv;
1387
1388   priv->in_drag = FALSE;
1389   priv->drag_pos = -1;
1390   priv->position_set = TRUE;
1391
1392   return TRUE;
1393 }
1394
1395 static void
1396 stop_drag (GtkPaned *paned)
1397 {
1398   GtkPanedPrivate *priv = paned->priv;
1399
1400   priv->in_drag = FALSE;
1401   priv->drag_pos = -1;
1402   priv->position_set = TRUE;
1403
1404   gdk_device_ungrab (priv->grab_device,
1405                      priv->grab_time);
1406   priv->grab_device = NULL;
1407 }
1408
1409 static void
1410 gtk_paned_grab_notify (GtkWidget *widget,
1411                        gboolean   was_grabbed)
1412 {
1413   GtkPaned *paned = GTK_PANED (widget);
1414   GtkPanedPrivate *priv = paned->priv;
1415   GdkDevice *grab_device;
1416
1417   grab_device = priv->grab_device;
1418
1419   if (priv->in_drag && grab_device &&
1420       gtk_widget_device_is_shadowed (widget, grab_device))
1421     stop_drag (paned);
1422 }
1423
1424 static void
1425 gtk_paned_state_flags_changed (GtkWidget     *widget,
1426                                GtkStateFlags  previous_state)
1427 {
1428   GtkPaned *paned = GTK_PANED (widget);
1429   GtkPanedPrivate *priv = paned->priv;
1430   GdkCursor *cursor;
1431
1432   if (gtk_widget_get_realized (widget))
1433     {
1434       if (gtk_widget_is_sensitive (widget))
1435         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1436                                              priv->cursor_type);
1437       else
1438         cursor = NULL;
1439
1440       gdk_window_set_cursor (priv->handle, cursor);
1441
1442       if (cursor)
1443         g_object_unref (cursor);
1444     }
1445 }
1446
1447 static gboolean
1448 gtk_paned_button_release (GtkWidget      *widget,
1449                           GdkEventButton *event)
1450 {
1451   GtkPaned *paned = GTK_PANED (widget);
1452   GtkPanedPrivate *priv = paned->priv;
1453
1454   if (priv->in_drag && (event->button == 1))
1455     {
1456       stop_drag (paned);
1457
1458       return TRUE;
1459     }
1460
1461   return FALSE;
1462 }
1463
1464 static gboolean
1465 gtk_paned_motion (GtkWidget      *widget,
1466                   GdkEventMotion *event)
1467 {
1468   GtkPaned *paned = GTK_PANED (widget);
1469   GtkPanedPrivate *priv = paned->priv;
1470
1471   if (priv->in_drag)
1472     {
1473       update_drag (paned);
1474       return TRUE;
1475     }
1476   
1477   return FALSE;
1478 }
1479
1480 /**
1481  * gtk_paned_new:
1482  * @orientation: the paned's orientation.
1483  *
1484  * Creates a new #GtkPaned widget.
1485  *
1486  * Return value: a new #GtkPaned.
1487  *
1488  * Since: 3.0
1489  **/
1490 GtkWidget *
1491 gtk_paned_new (GtkOrientation orientation)
1492 {
1493   return g_object_new (GTK_TYPE_PANED,
1494                        "orientation", orientation,
1495                        NULL);
1496 }
1497
1498 /**
1499  * gtk_paned_add1:
1500  * @paned: a paned widget
1501  * @child: the child to add
1502  *
1503  * Adds a child to the top or left pane with default parameters. This is
1504  * equivalent to
1505  * <literal>gtk_paned_pack1 (paned, child, FALSE, TRUE)</literal>.
1506  */
1507 void
1508 gtk_paned_add1 (GtkPaned  *paned,
1509                 GtkWidget *widget)
1510 {
1511   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
1512 }
1513
1514 /**
1515  * gtk_paned_add2:
1516  * @paned: a paned widget
1517  * @child: the child to add
1518  *
1519  * Adds a child to the bottom or right pane with default parameters. This
1520  * is equivalent to
1521  * <literal>gtk_paned_pack2 (paned, child, TRUE, TRUE)</literal>.
1522  */
1523 void
1524 gtk_paned_add2 (GtkPaned  *paned,
1525                 GtkWidget *widget)
1526 {
1527   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
1528 }
1529
1530 /**
1531  * gtk_paned_pack1:
1532  * @paned: a paned widget
1533  * @child: the child to add
1534  * @resize: should this child expand when the paned widget is resized.
1535  * @shrink: can this child be made smaller than its requisition.
1536  *
1537  * Adds a child to the top or left pane.
1538  */
1539 void
1540 gtk_paned_pack1 (GtkPaned  *paned,
1541                  GtkWidget *child,
1542                  gboolean   resize,
1543                  gboolean   shrink)
1544 {
1545   GtkPanedPrivate *priv;
1546
1547   g_return_if_fail (GTK_IS_PANED (paned));
1548   g_return_if_fail (GTK_IS_WIDGET (child));
1549
1550   priv = paned->priv;
1551
1552   if (!priv->child1)
1553     {
1554       priv->child1 = child;
1555       priv->child1_resize = resize;
1556       priv->child1_shrink = shrink;
1557
1558       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1559     }
1560 }
1561
1562 /**
1563  * gtk_paned_pack2:
1564  * @paned: a paned widget
1565  * @child: the child to add
1566  * @resize: should this child expand when the paned widget is resized.
1567  * @shrink: can this child be made smaller than its requisition.
1568  *
1569  * Adds a child to the bottom or right pane.
1570  */
1571 void
1572 gtk_paned_pack2 (GtkPaned  *paned,
1573                  GtkWidget *child,
1574                  gboolean   resize,
1575                  gboolean   shrink)
1576 {
1577   GtkPanedPrivate *priv;
1578
1579   g_return_if_fail (GTK_IS_PANED (paned));
1580   g_return_if_fail (GTK_IS_WIDGET (child));
1581
1582   priv = paned->priv;
1583
1584   if (!priv->child2)
1585     {
1586       priv->child2 = child;
1587       priv->child2_resize = resize;
1588       priv->child2_shrink = shrink;
1589
1590       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1591     }
1592 }
1593
1594
1595 static void
1596 gtk_paned_add (GtkContainer *container,
1597                GtkWidget    *widget)
1598 {
1599   GtkPanedPrivate *priv;
1600   GtkPaned *paned;
1601
1602   g_return_if_fail (GTK_IS_PANED (container));
1603
1604   paned = GTK_PANED (container);
1605   priv = paned->priv;
1606
1607   if (!priv->child1)
1608     gtk_paned_add1 (paned, widget);
1609   else if (!priv->child2)
1610     gtk_paned_add2 (paned, widget);
1611   else
1612     g_warning ("GtkPaned cannot have more than 2 children\n");
1613 }
1614
1615 static void
1616 gtk_paned_remove (GtkContainer *container,
1617                   GtkWidget    *widget)
1618 {
1619   GtkPaned *paned = GTK_PANED (container);
1620   GtkPanedPrivate *priv = paned->priv;
1621   gboolean was_visible;
1622
1623   was_visible = gtk_widget_get_visible (widget);
1624
1625   if (priv->child1 == widget)
1626     {
1627       gtk_widget_unparent (widget);
1628
1629       priv->child1 = NULL;
1630
1631       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1632         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1633     }
1634   else if (priv->child2 == widget)
1635     {
1636       gtk_widget_unparent (widget);
1637
1638       priv->child2 = NULL;
1639
1640       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1641         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1642     }
1643 }
1644
1645 static void
1646 gtk_paned_forall (GtkContainer *container,
1647                   gboolean      include_internals,
1648                   GtkCallback   callback,
1649                   gpointer      callback_data)
1650 {
1651   GtkPanedPrivate *priv;
1652   GtkPaned *paned;
1653
1654   g_return_if_fail (callback != NULL);
1655
1656   paned = GTK_PANED (container);
1657   priv = paned->priv;
1658
1659   if (priv->child1)
1660     (*callback) (priv->child1, callback_data);
1661   if (priv->child2)
1662     (*callback) (priv->child2, callback_data);
1663 }
1664
1665 /**
1666  * gtk_paned_get_position:
1667  * @paned: a #GtkPaned widget
1668  * 
1669  * Obtains the position of the divider between the two panes.
1670  * 
1671  * Return value: position of the divider
1672  **/
1673 gint
1674 gtk_paned_get_position (GtkPaned  *paned)
1675 {
1676   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1677
1678   return paned->priv->child1_size;
1679 }
1680
1681 /**
1682  * gtk_paned_set_position:
1683  * @paned: a #GtkPaned widget
1684  * @position: pixel position of divider, a negative value means that the position
1685  *            is unset.
1686  * 
1687  * Sets the position of the divider between the two panes.
1688  **/
1689 void
1690 gtk_paned_set_position (GtkPaned *paned,
1691                         gint      position)
1692 {
1693   GtkPanedPrivate *priv;
1694   GObject *object;
1695
1696   g_return_if_fail (GTK_IS_PANED (paned));
1697
1698   priv = paned->priv;
1699
1700   if (priv->child1_size == position)
1701     return;
1702
1703   object = G_OBJECT (paned);
1704   
1705   if (position >= 0)
1706     {
1707       /* We don't clamp here - the assumption is that
1708        * if the total allocation changes at the same time
1709        * as the position, the position set is with reference
1710        * to the new total size. If only the position changes,
1711        * then clamping will occur in gtk_paned_calc_position()
1712        */
1713
1714       priv->child1_size = position;
1715       priv->position_set = TRUE;
1716     }
1717   else
1718     {
1719       priv->position_set = FALSE;
1720     }
1721
1722   g_object_freeze_notify (object);
1723   g_object_notify (object, "position");
1724   g_object_notify (object, "position-set");
1725   g_object_thaw_notify (object);
1726
1727   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
1728
1729 #ifdef G_OS_WIN32
1730   /* Hacky work-around for bug #144269 */
1731   if (priv->child2 != NULL)
1732     {
1733       gtk_widget_queue_draw (priv->child2);
1734     }
1735 #endif
1736 }
1737
1738 /**
1739  * gtk_paned_get_child1:
1740  * @paned: a #GtkPaned widget
1741  * 
1742  * Obtains the first child of the paned widget.
1743  * 
1744  * Return value: (transfer none): first child, or %NULL if it is not set.
1745  *
1746  * Since: 2.4
1747  **/
1748 GtkWidget *
1749 gtk_paned_get_child1 (GtkPaned *paned)
1750 {
1751   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1752
1753   return paned->priv->child1;
1754 }
1755
1756 /**
1757  * gtk_paned_get_child2:
1758  * @paned: a #GtkPaned widget
1759  * 
1760  * Obtains the second child of the paned widget.
1761  * 
1762  * Return value: (transfer none): second child, or %NULL if it is not set.
1763  *
1764  * Since: 2.4
1765  **/
1766 GtkWidget *
1767 gtk_paned_get_child2 (GtkPaned *paned)
1768 {
1769   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1770
1771   return paned->priv->child2;
1772 }
1773
1774 static void
1775 gtk_paned_calc_position (GtkPaned *paned,
1776                          gint      allocation,
1777                          gint      child1_req,
1778                          gint      child2_req)
1779 {
1780   GtkPanedPrivate *priv = paned->priv;
1781   gint old_position;
1782   gint old_min_position;
1783   gint old_max_position;
1784
1785   old_position = priv->child1_size;
1786   old_min_position = priv->min_position;
1787   old_max_position = priv->max_position;
1788
1789   priv->min_position = priv->child1_shrink ? 0 : child1_req;
1790
1791   priv->max_position = allocation;
1792   if (!priv->child2_shrink)
1793     priv->max_position = MAX (1, priv->max_position - child2_req);
1794   priv->max_position = MAX (priv->min_position, priv->max_position);
1795
1796   if (!priv->position_set)
1797     {
1798       if (priv->child1_resize && !priv->child2_resize)
1799         priv->child1_size = MAX (0, allocation - child2_req);
1800       else if (!priv->child1_resize && priv->child2_resize)
1801         priv->child1_size = child1_req;
1802       else if (child1_req + child2_req != 0)
1803         priv->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
1804       else
1805         priv->child1_size = allocation * 0.5 + 0.5;
1806     }
1807   else
1808     {
1809       /* If the position was set before the initial allocation.
1810        * (priv->last_allocation <= 0) just clamp it and leave it.
1811        */
1812       if (priv->last_allocation > 0)
1813         {
1814           if (priv->child1_resize && !priv->child2_resize)
1815             priv->child1_size += allocation - priv->last_allocation;
1816           else if (!(!priv->child1_resize && priv->child2_resize))
1817             priv->child1_size = allocation * ((gdouble) priv->child1_size / (priv->last_allocation)) + 0.5;
1818         }
1819     }
1820
1821   priv->child1_size = CLAMP (priv->child1_size,
1822                               priv->min_position,
1823                               priv->max_position);
1824
1825   if (priv->child1)
1826     gtk_widget_set_child_visible (priv->child1, priv->child1_size != 0);
1827   
1828   if (priv->child2)
1829     gtk_widget_set_child_visible (priv->child2, priv->child1_size != allocation); 
1830
1831   g_object_freeze_notify (G_OBJECT (paned));
1832   if (priv->child1_size != old_position)
1833     g_object_notify (G_OBJECT (paned), "position");
1834   if (priv->min_position != old_min_position)
1835     g_object_notify (G_OBJECT (paned), "min-position");
1836   if (priv->max_position != old_max_position)
1837     g_object_notify (G_OBJECT (paned), "max-position");
1838   g_object_thaw_notify (G_OBJECT (paned));
1839
1840   priv->last_allocation = allocation;
1841 }
1842
1843 static void
1844 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1845 {
1846   GtkPanedPrivate *priv = paned->priv;
1847
1848   if (priv->saved_focus)
1849     g_object_remove_weak_pointer (G_OBJECT (priv->saved_focus),
1850                                   (gpointer *)&(priv->saved_focus));
1851
1852   priv->saved_focus = widget;
1853
1854   if (priv->saved_focus)
1855     g_object_add_weak_pointer (G_OBJECT (priv->saved_focus),
1856                                (gpointer *)&(priv->saved_focus));
1857 }
1858
1859 static void
1860 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1861 {
1862   GtkPanedPrivate *priv = paned->priv;
1863
1864   if (priv->first_paned)
1865     g_object_remove_weak_pointer (G_OBJECT (priv->first_paned),
1866                                   (gpointer *)&(priv->first_paned));
1867
1868   priv->first_paned = first_paned;
1869
1870   if (priv->first_paned)
1871     g_object_add_weak_pointer (G_OBJECT (priv->first_paned),
1872                                (gpointer *)&(priv->first_paned));
1873 }
1874
1875 static void
1876 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1877 {
1878   GtkPanedPrivate *priv = paned->priv;
1879
1880   if (priv->last_child1_focus)
1881     g_object_remove_weak_pointer (G_OBJECT (priv->last_child1_focus),
1882                                   (gpointer *)&(priv->last_child1_focus));
1883
1884   priv->last_child1_focus = widget;
1885
1886   if (priv->last_child1_focus)
1887     g_object_add_weak_pointer (G_OBJECT (priv->last_child1_focus),
1888                                (gpointer *)&(priv->last_child1_focus));
1889 }
1890
1891 static void
1892 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1893 {
1894   GtkPanedPrivate *priv = paned->priv;
1895
1896   if (priv->last_child2_focus)
1897     g_object_remove_weak_pointer (G_OBJECT (priv->last_child2_focus),
1898                                   (gpointer *)&(priv->last_child2_focus));
1899
1900   priv->last_child2_focus = widget;
1901
1902   if (priv->last_child2_focus)
1903     g_object_add_weak_pointer (G_OBJECT (priv->last_child2_focus),
1904                                (gpointer *)&(priv->last_child2_focus));
1905 }
1906
1907 static GtkWidget *
1908 paned_get_focus_widget (GtkPaned *paned)
1909 {
1910   GtkWidget *toplevel;
1911
1912   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1913   if (gtk_widget_is_toplevel (toplevel))
1914     return gtk_window_get_focus (GTK_WINDOW (toplevel));
1915
1916   return NULL;
1917 }
1918
1919 static void
1920 gtk_paned_set_focus_child (GtkContainer *container,
1921                            GtkWidget    *focus_child)
1922 {
1923   GtkPaned *paned;
1924   GtkPanedPrivate *priv;
1925   GtkWidget *container_focus_child;
1926
1927   g_return_if_fail (GTK_IS_PANED (container));
1928
1929   paned = GTK_PANED (container);
1930   priv = paned->priv;
1931
1932   if (focus_child == NULL)
1933     {
1934       GtkWidget *last_focus;
1935       GtkWidget *w;
1936       
1937       last_focus = paned_get_focus_widget (paned);
1938
1939       if (last_focus)
1940         {
1941           /* If there is one or more paned widgets between us and the
1942            * focus widget, we want the topmost of those as last_focus
1943            */
1944           for (w = last_focus; w != GTK_WIDGET (paned); w = gtk_widget_get_parent (w))
1945             if (GTK_IS_PANED (w))
1946               last_focus = w;
1947
1948           container_focus_child = gtk_container_get_focus_child (container);
1949           if (container_focus_child == priv->child1)
1950             gtk_paned_set_last_child1_focus (paned, last_focus);
1951           else if (container_focus_child == priv->child2)
1952             gtk_paned_set_last_child2_focus (paned, last_focus);
1953         }
1954     }
1955
1956   if (GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child)
1957     GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child (container, focus_child);
1958 }
1959
1960 static void
1961 gtk_paned_get_cycle_chain (GtkPaned          *paned,
1962                            GtkDirectionType   direction,
1963                            GList            **widgets)
1964 {
1965   GtkPanedPrivate *priv = paned->priv;
1966   GtkContainer *container = GTK_CONTAINER (paned);
1967   GtkWidget *ancestor = NULL;
1968   GtkWidget *focus_child;
1969   GtkWidget *parent;
1970   GtkWidget *widget = GTK_WIDGET (paned);
1971   GList *temp_list = NULL;
1972   GList *list;
1973
1974   if (priv->in_recursion)
1975     return;
1976
1977   g_assert (widgets != NULL);
1978
1979   if (priv->last_child1_focus &&
1980       !gtk_widget_is_ancestor (priv->last_child1_focus, widget))
1981     {
1982       gtk_paned_set_last_child1_focus (paned, NULL);
1983     }
1984
1985   if (priv->last_child2_focus &&
1986       !gtk_widget_is_ancestor (priv->last_child2_focus, widget))
1987     {
1988       gtk_paned_set_last_child2_focus (paned, NULL);
1989     }
1990
1991   parent = gtk_widget_get_parent (widget);
1992   if (parent)
1993     ancestor = gtk_widget_get_ancestor (parent, GTK_TYPE_PANED);
1994
1995   /* The idea here is that temp_list is a list of widgets we want to cycle
1996    * to. The list is prioritized so that the first element is our first
1997    * choice, the next our second, and so on.
1998    *
1999    * We can't just use g_list_reverse(), because we want to try
2000    * priv->last_child?_focus before priv->child?, both when we
2001    * are going forward and backward.
2002    */
2003   focus_child = gtk_container_get_focus_child (container);
2004   if (direction == GTK_DIR_TAB_FORWARD)
2005     {
2006       if (focus_child == priv->child1)
2007         {
2008           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2009           temp_list = g_list_append (temp_list, priv->child2);
2010           temp_list = g_list_append (temp_list, ancestor);
2011         }
2012       else if (focus_child == priv->child2)
2013         {
2014           temp_list = g_list_append (temp_list, ancestor);
2015           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2016           temp_list = g_list_append (temp_list, priv->child1);
2017         }
2018       else
2019         {
2020           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2021           temp_list = g_list_append (temp_list, priv->child1);
2022           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2023           temp_list = g_list_append (temp_list, priv->child2);
2024           temp_list = g_list_append (temp_list, ancestor);
2025         }
2026     }
2027   else
2028     {
2029       if (focus_child == priv->child1)
2030         {
2031           temp_list = g_list_append (temp_list, ancestor);
2032           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2033           temp_list = g_list_append (temp_list, priv->child2);
2034         }
2035       else if (focus_child == priv->child2)
2036         {
2037           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2038           temp_list = g_list_append (temp_list, priv->child1);
2039           temp_list = g_list_append (temp_list, ancestor);
2040         }
2041       else
2042         {
2043           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2044           temp_list = g_list_append (temp_list, priv->child2);
2045           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2046           temp_list = g_list_append (temp_list, priv->child1);
2047           temp_list = g_list_append (temp_list, ancestor);
2048         }
2049     }
2050
2051   /* Walk the list and expand all the paned widgets. */
2052   for (list = temp_list; list != NULL; list = list->next)
2053     {
2054       GtkWidget *widget = list->data;
2055
2056       if (widget)
2057         {
2058           if (GTK_IS_PANED (widget))
2059             {
2060               priv->in_recursion = TRUE;
2061               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
2062               priv->in_recursion = FALSE;
2063             }
2064           else
2065             {
2066               *widgets = g_list_append (*widgets, widget);
2067             }
2068         }
2069     }
2070
2071   g_list_free (temp_list);
2072 }
2073
2074 static gboolean
2075 gtk_paned_cycle_child_focus (GtkPaned *paned,
2076                              gboolean  reversed)
2077 {
2078   GList *cycle_chain = NULL;
2079   GList *list;
2080   
2081   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
2082
2083   /* ignore f6 if the handle is focused */
2084   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2085     return TRUE;
2086   
2087   /* we can't just let the event propagate up the hierarchy,
2088    * because the paned will want to cycle focus _unless_ an
2089    * ancestor paned handles the event
2090    */
2091   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
2092
2093   for (list = cycle_chain; list != NULL; list = list->next)
2094     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
2095       break;
2096
2097   g_list_free (cycle_chain);
2098   
2099   return TRUE;
2100 }
2101
2102 static void
2103 get_child_panes (GtkWidget  *widget,
2104                  GList     **panes)
2105 {
2106   if (!widget || !gtk_widget_get_realized (widget))
2107     return;
2108
2109   if (GTK_IS_PANED (widget))
2110     {
2111       GtkPaned *paned = GTK_PANED (widget);
2112       GtkPanedPrivate *priv = paned->priv;
2113
2114       get_child_panes (priv->child1, panes);
2115       *panes = g_list_prepend (*panes, widget);
2116       get_child_panes (priv->child2, panes);
2117     }
2118   else if (GTK_IS_CONTAINER (widget))
2119     {
2120       gtk_container_forall (GTK_CONTAINER (widget),
2121                             (GtkCallback)get_child_panes, panes);
2122     }
2123 }
2124
2125 static GList *
2126 get_all_panes (GtkPaned *paned)
2127 {
2128   GtkPaned *topmost = NULL;
2129   GList *result = NULL;
2130   GtkWidget *w;
2131
2132   for (w = GTK_WIDGET (paned); w != NULL; w = gtk_widget_get_parent (w))
2133     {
2134       if (GTK_IS_PANED (w))
2135         topmost = GTK_PANED (w);
2136     }
2137
2138   g_assert (topmost);
2139
2140   get_child_panes (GTK_WIDGET (topmost), &result);
2141
2142   return g_list_reverse (result);
2143 }
2144
2145 static void
2146 gtk_paned_find_neighbours (GtkPaned  *paned,
2147                            GtkPaned **next,
2148                            GtkPaned **prev)
2149 {
2150   GList *all_panes;
2151   GList *this_link;
2152
2153   all_panes = get_all_panes (paned);
2154   g_assert (all_panes);
2155
2156   this_link = g_list_find (all_panes, paned);
2157
2158   g_assert (this_link);
2159   
2160   if (this_link->next)
2161     *next = this_link->next->data;
2162   else
2163     *next = all_panes->data;
2164
2165   if (this_link->prev)
2166     *prev = this_link->prev->data;
2167   else
2168     *prev = g_list_last (all_panes)->data;
2169
2170   g_list_free (all_panes);
2171 }
2172
2173 static gboolean
2174 gtk_paned_move_handle (GtkPaned      *paned,
2175                        GtkScrollType  scroll)
2176 {
2177   GtkPanedPrivate *priv = paned->priv;
2178
2179   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2180     {
2181       gint old_position;
2182       gint new_position;
2183       gint increment;
2184       
2185       enum {
2186         SINGLE_STEP_SIZE = 1,
2187         PAGE_STEP_SIZE   = 75
2188       };
2189       
2190       new_position = old_position = gtk_paned_get_position (paned);
2191       increment = 0;
2192       
2193       switch (scroll)
2194         {
2195         case GTK_SCROLL_STEP_LEFT:
2196         case GTK_SCROLL_STEP_UP:
2197         case GTK_SCROLL_STEP_BACKWARD:
2198           increment = - SINGLE_STEP_SIZE;
2199           break;
2200           
2201         case GTK_SCROLL_STEP_RIGHT:
2202         case GTK_SCROLL_STEP_DOWN:
2203         case GTK_SCROLL_STEP_FORWARD:
2204           increment = SINGLE_STEP_SIZE;
2205           break;
2206           
2207         case GTK_SCROLL_PAGE_LEFT:
2208         case GTK_SCROLL_PAGE_UP:
2209         case GTK_SCROLL_PAGE_BACKWARD:
2210           increment = - PAGE_STEP_SIZE;
2211           break;
2212           
2213         case GTK_SCROLL_PAGE_RIGHT:
2214         case GTK_SCROLL_PAGE_DOWN:
2215         case GTK_SCROLL_PAGE_FORWARD:
2216           increment = PAGE_STEP_SIZE;
2217           break;
2218           
2219         case GTK_SCROLL_START:
2220           new_position = priv->min_position;
2221           break;
2222           
2223         case GTK_SCROLL_END:
2224           new_position = priv->max_position;
2225           break;
2226
2227         default:
2228           break;
2229         }
2230
2231       if (increment)
2232         {
2233           if (is_rtl (paned))
2234             increment = -increment;
2235           
2236           new_position = old_position + increment;
2237         }
2238       
2239       new_position = CLAMP (new_position, priv->min_position, priv->max_position);
2240       
2241       if (old_position != new_position)
2242         gtk_paned_set_position (paned, new_position);
2243
2244       return TRUE;
2245     }
2246
2247   return FALSE;
2248 }
2249
2250 static void
2251 gtk_paned_restore_focus (GtkPaned *paned)
2252 {
2253   GtkPanedPrivate *priv = paned->priv;
2254
2255   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2256     {
2257       if (priv->saved_focus &&
2258           gtk_widget_get_sensitive (priv->saved_focus))
2259         {
2260           gtk_widget_grab_focus (priv->saved_focus);
2261         }
2262       else
2263         {
2264           /* the saved focus is somehow not available for focusing,
2265            * try
2266            *   1) tabbing into the paned widget
2267            * if that didn't work,
2268            *   2) unset focus for the window if there is one
2269            */
2270           
2271           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
2272             {
2273               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2274               
2275               if (GTK_IS_WINDOW (toplevel))
2276                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
2277             }
2278         }
2279       
2280       gtk_paned_set_saved_focus (paned, NULL);
2281       gtk_paned_set_first_paned (paned, NULL);
2282     }
2283 }
2284
2285 static gboolean
2286 gtk_paned_accept_position (GtkPaned *paned)
2287 {
2288   GtkPanedPrivate *priv = paned->priv;
2289
2290   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2291     {
2292       priv->original_position = -1;
2293       gtk_paned_restore_focus (paned);
2294
2295       return TRUE;
2296     }
2297
2298   return FALSE;
2299 }
2300
2301
2302 static gboolean
2303 gtk_paned_cancel_position (GtkPaned *paned)
2304 {
2305   GtkPanedPrivate *priv = paned->priv;
2306
2307   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2308     {
2309       if (priv->original_position != -1)
2310         {
2311           gtk_paned_set_position (paned, priv->original_position);
2312           priv->original_position = -1;
2313         }
2314
2315       gtk_paned_restore_focus (paned);
2316       return TRUE;
2317     }
2318
2319   return FALSE;
2320 }
2321
2322 static gboolean
2323 gtk_paned_cycle_handle_focus (GtkPaned *paned,
2324                               gboolean  reversed)
2325 {
2326   GtkPanedPrivate *priv = paned->priv;
2327   GtkPaned *next, *prev;
2328
2329   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2330     {
2331       GtkPaned *focus = NULL;
2332
2333       if (!priv->first_paned)
2334         {
2335           /* The first_pane has disappeared. As an ad-hoc solution,
2336            * we make the currently focused paned the first_paned. To the
2337            * user this will seem like the paned cycling has been reset.
2338            */
2339           
2340           gtk_paned_set_first_paned (paned, paned);
2341         }
2342       
2343       gtk_paned_find_neighbours (paned, &next, &prev);
2344
2345       if (reversed && prev &&
2346           prev != paned && paned != priv->first_paned)
2347         {
2348           focus = prev;
2349         }
2350       else if (!reversed && next &&
2351                next != paned && next != priv->first_paned)
2352         {
2353           focus = next;
2354         }
2355       else
2356         {
2357           gtk_paned_accept_position (paned);
2358           return TRUE;
2359         }
2360
2361       g_assert (focus);
2362       
2363       gtk_paned_set_saved_focus (focus, priv->saved_focus);
2364       gtk_paned_set_first_paned (focus, priv->first_paned);
2365       
2366       gtk_paned_set_saved_focus (paned, NULL);
2367       gtk_paned_set_first_paned (paned, NULL);
2368       
2369       gtk_widget_grab_focus (GTK_WIDGET (focus));
2370       
2371       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
2372         {
2373           priv->original_position = -1;
2374           focus->priv->original_position = gtk_paned_get_position (focus);
2375         }
2376     }
2377   else
2378     {
2379       GtkContainer *container = GTK_CONTAINER (paned);
2380       GtkPaned *focus;
2381       GtkPaned *first;
2382       GtkPaned *prev, *next;
2383       GtkWidget *toplevel;
2384       GtkWidget *focus_child;
2385
2386       gtk_paned_find_neighbours (paned, &next, &prev);
2387       focus_child = gtk_container_get_focus_child (container);
2388
2389       if (focus_child == priv->child1)
2390         {
2391           if (reversed)
2392             {
2393               focus = prev;
2394               first = paned;
2395             }
2396           else
2397             {
2398               focus = paned;
2399               first = paned;
2400             }
2401         }
2402       else if (focus_child == priv->child2)
2403         {
2404           if (reversed)
2405             {
2406               focus = paned;
2407               first = next;
2408             }
2409           else
2410             {
2411               focus = next;
2412               first = next;
2413             }
2414         }
2415       else
2416         {
2417           /* Focus is not inside this paned, and we don't have focus.
2418            * Presumably this happened because the application wants us
2419            * to start keyboard navigating.
2420            */
2421           focus = paned;
2422
2423           if (reversed)
2424             first = paned;
2425           else
2426             first = next;
2427         }
2428
2429       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2430
2431       if (GTK_IS_WINDOW (toplevel))
2432         gtk_paned_set_saved_focus (focus, gtk_window_get_focus (GTK_WINDOW (toplevel)));
2433       gtk_paned_set_first_paned (focus, first);
2434       focus->priv->original_position = gtk_paned_get_position (focus);
2435
2436       gtk_widget_grab_focus (GTK_WIDGET (focus));
2437    }
2438
2439   return TRUE;
2440 }
2441
2442 static gboolean
2443 gtk_paned_toggle_handle_focus (GtkPaned *paned)
2444 {
2445   /* This function/signal has the wrong name. It is called when you
2446    * press Tab or Shift-Tab and what we do is act as if
2447    * the user pressed Return and then Tab or Shift-Tab
2448    */
2449   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2450     gtk_paned_accept_position (paned);
2451
2452   return FALSE;
2453 }
2454
2455 /**
2456  * gtk_paned_get_handle_window:
2457  * @paned: a #GtkPaned
2458  *
2459  * Returns the #GdkWindow of the handle. This function is
2460  * useful when handling button or motion events because it
2461  * enables the callback to distinguish between the window
2462  * of the paned, a child and the handle.
2463  *
2464  * Return value: (transfer none): the paned's handle window.
2465  *
2466  * Since: 2.20
2467  **/
2468 GdkWindow *
2469 gtk_paned_get_handle_window (GtkPaned *paned)
2470 {
2471   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2472
2473   return paned->priv->handle;
2474 }