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