]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
GtkPanedPrivate: Improve struct packing
[~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 "gtkbindings.h"
32 #include "gtkmain.h"
33 #include "gtkmarshalers.h"
34 #include "gtkorientable.h"
35 #include "gtkwindow.h"
36 #include "gtktypebuiltins.h"
37 #include "gtkprivate.h"
38 #include "gtkintl.h"
39
40
41 /**
42  * SECTION:gtkpaned
43  * @Short_description: Base class for widgets with two adjustable panes
44  * @Title: GtkPaned
45  *
46  * #GtkPaned is the base class for widgets with two panes, arranged either
47  * horizontally (#GtkHPaned) or vertically (#GtkVPaned). Child widgets are
48  * added to the panes of the widget with gtk_paned_pack1() and
49  * gtk_paned_pack2(). The division between the two children is set by default
50  * from the size requests of the children, but it can be adjusted by the
51  * user.
52  *
53  * A paned widget draws a separator between the two child widgets and a
54  * small handle that the user can drag to adjust the division. It does not
55  * draw any relief around the children or around the separator. (The space
56  * in which the separator is called the gutter.) Often, it is useful to put
57  * each child inside a #GtkFrame with the shadow type set to %GTK_SHADOW_IN
58  * so that the gutter appears as a ridge. No separator is drawn if one of
59  * the children is missing.
60  *
61  * Each child has two options that can be set, @resize and @shrink. If
62  * @resize is true, then when the #GtkPaned is resized, that child will
63  * expand or shrink along with the paned widget. If @shrink is true, then
64  * that child can be made smaller than its requisition by the user.
65  * Setting @shrink to %FALSE allows the application to set a minimum size.
66  * If @resize is false for both children, then this is treated as if
67  * @resize is true for both children.
68  *
69  * The application can set the position of the slider as if it were set
70  * by the user, by calling gtk_paned_set_position().
71  *
72  * <example>
73  * <title>Creating a paned widget with minimum sizes.</title>
74  * <programlisting>
75  * GtkWidget *hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
76  * GtkWidget *frame1 = gtk_frame_new (NULL);
77  * GtkWidget *frame2 = gtk_frame_new (NULL);
78  * gtk_frame_set_shadow_type (GTK_FRAME (frame1), GTK_SHADOW_IN);
79  * gtk_frame_set_shadow_type (GTK_FRAME (frame2), GTK_SHADOW_IN);
80  *
81  * gtk_widget_set_size_request (hpaned, 200, -1);
82  *
83  * gtk_paned_pack1 (GTK_PANED (hpaned), frame1, TRUE, FALSE);
84  * gtk_widget_set_size_request (frame1, 50, -1);
85  *
86  * gtk_paned_pack2 (GTK_PANED (hpaned), frame2, FALSE, FALSE);
87  * gtk_widget_set_size_request (frame2, 50, -1);
88  * </programlisting>
89  * </example>
90  */
91
92 struct _GtkPanedPrivate
93 {
94   GtkPaned       *first_paned;
95   GtkWidget      *child1;
96   GtkWidget      *child2;
97   GtkWidget      *last_child1_focus;
98   GtkWidget      *last_child2_focus;
99   GtkWidget      *saved_focus;
100   GtkOrientation  orientation;
101
102   GdkCursorType  cursor_type;
103   GdkDevice     *grab_device;
104   GdkRectangle   handle_pos;
105   GdkWindow     *handle;
106
107   gint          child1_size;
108   gint          drag_pos;
109   gint          last_allocation;
110   gint          max_position;
111   gint          min_position;
112   gint          original_position;
113
114   guint32       grab_time;
115
116   guint         handle_prelit : 1;
117   guint         in_drag       : 1;
118   guint         in_recursion  : 1;
119   guint         child1_resize : 1;
120   guint         child1_shrink : 1;
121   guint         child2_resize : 1;
122   guint         child2_shrink : 1;
123   guint         position_set  : 1;
124 };
125
126 enum {
127   PROP_0,
128   PROP_ORIENTATION,
129   PROP_POSITION,
130   PROP_POSITION_SET,
131   PROP_MIN_POSITION,
132   PROP_MAX_POSITION
133 };
134
135 enum {
136   CHILD_PROP_0,
137   CHILD_PROP_RESIZE,
138   CHILD_PROP_SHRINK
139 };
140
141 enum {
142   CYCLE_CHILD_FOCUS,
143   TOGGLE_HANDLE_FOCUS,
144   MOVE_HANDLE,
145   CYCLE_HANDLE_FOCUS,
146   ACCEPT_POSITION,
147   CANCEL_POSITION,
148   LAST_SIGNAL
149 };
150
151 static void     gtk_paned_set_property          (GObject          *object,
152                                                  guint             prop_id,
153                                                  const GValue     *value,
154                                                  GParamSpec       *pspec);
155 static void     gtk_paned_get_property          (GObject          *object,
156                                                  guint             prop_id,
157                                                  GValue           *value,
158                                                  GParamSpec       *pspec);
159 static void     gtk_paned_set_child_property    (GtkContainer     *container,
160                                                  GtkWidget        *child,
161                                                  guint             property_id,
162                                                  const GValue     *value,
163                                                  GParamSpec       *pspec);
164 static void     gtk_paned_get_child_property    (GtkContainer     *container,
165                                                  GtkWidget        *child,
166                                                  guint             property_id,
167                                                  GValue           *value,
168                                                  GParamSpec       *pspec);
169 static void     gtk_paned_finalize              (GObject          *object);
170
171 static void     gtk_paned_get_preferred_width   (GtkWidget        *widget,
172                                                  gint             *minimum,
173                                                  gint             *natural);
174 static void     gtk_paned_get_preferred_height  (GtkWidget        *widget,
175                                                  gint             *minimum,
176                                                  gint             *natural);
177
178 static void     gtk_paned_size_allocate         (GtkWidget        *widget,
179                                                  GtkAllocation    *allocation);
180 static void     gtk_paned_realize               (GtkWidget        *widget);
181 static void     gtk_paned_unrealize             (GtkWidget        *widget);
182 static void     gtk_paned_map                   (GtkWidget        *widget);
183 static void     gtk_paned_unmap                 (GtkWidget        *widget);
184 static void     gtk_paned_state_flags_changed   (GtkWidget        *widget,
185                                                  GtkStateFlags     previous_state);
186 static gboolean gtk_paned_draw                  (GtkWidget        *widget,
187                                                  cairo_t          *cr);
188 static gboolean gtk_paned_enter                 (GtkWidget        *widget,
189                                                  GdkEventCrossing *event);
190 static gboolean gtk_paned_leave                 (GtkWidget        *widget,
191                                                  GdkEventCrossing *event);
192 static gboolean gtk_paned_button_press          (GtkWidget        *widget,
193                                                  GdkEventButton   *event);
194 static gboolean gtk_paned_button_release        (GtkWidget        *widget,
195                                                  GdkEventButton   *event);
196 static gboolean gtk_paned_motion                (GtkWidget        *widget,
197                                                  GdkEventMotion   *event);
198 static gboolean gtk_paned_focus                 (GtkWidget        *widget,
199                                                  GtkDirectionType  direction);
200 static gboolean gtk_paned_grab_broken           (GtkWidget          *widget,
201                                                  GdkEventGrabBroken *event);
202 static void     gtk_paned_add                   (GtkContainer     *container,
203                                                  GtkWidget        *widget);
204 static void     gtk_paned_remove                (GtkContainer     *container,
205                                                  GtkWidget        *widget);
206 static void     gtk_paned_forall                (GtkContainer     *container,
207                                                  gboolean          include_internals,
208                                                  GtkCallback       callback,
209                                                  gpointer          callback_data);
210 static void     gtk_paned_calc_position         (GtkPaned         *paned,
211                                                  gint              allocation,
212                                                  gint              child1_req,
213                                                  gint              child2_req);
214 static void     gtk_paned_set_focus_child       (GtkContainer     *container,
215                                                  GtkWidget        *child);
216 static void     gtk_paned_set_saved_focus       (GtkPaned         *paned,
217                                                  GtkWidget        *widget);
218 static void     gtk_paned_set_first_paned       (GtkPaned         *paned,
219                                                  GtkPaned         *first_paned);
220 static void     gtk_paned_set_last_child1_focus (GtkPaned         *paned,
221                                                  GtkWidget        *widget);
222 static void     gtk_paned_set_last_child2_focus (GtkPaned         *paned,
223                                                  GtkWidget        *widget);
224 static gboolean gtk_paned_cycle_child_focus     (GtkPaned         *paned,
225                                                  gboolean          reverse);
226 static gboolean gtk_paned_cycle_handle_focus    (GtkPaned         *paned,
227                                                  gboolean          reverse);
228 static gboolean gtk_paned_move_handle           (GtkPaned         *paned,
229                                                  GtkScrollType     scroll);
230 static gboolean gtk_paned_accept_position       (GtkPaned         *paned);
231 static gboolean gtk_paned_cancel_position       (GtkPaned         *paned);
232 static gboolean gtk_paned_toggle_handle_focus   (GtkPaned         *paned);
233
234 static GType    gtk_paned_child_type            (GtkContainer     *container);
235 static void     gtk_paned_grab_notify           (GtkWidget        *widget,
236                                                  gboolean          was_grabbed);
237
238
239 G_DEFINE_TYPE_WITH_CODE (GtkPaned, gtk_paned, GTK_TYPE_CONTAINER,
240                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
241                                                 NULL))
242
243 static guint signals[LAST_SIGNAL] = { 0 };
244
245
246 static void
247 add_tab_bindings (GtkBindingSet    *binding_set,
248                   GdkModifierType   modifiers)
249 {
250   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
251                                 "toggle-handle-focus", 0);
252   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
253                                 "toggle-handle-focus", 0);
254 }
255
256 static void
257 add_move_binding (GtkBindingSet   *binding_set,
258                   guint            keyval,
259                   GdkModifierType  mask,
260                   GtkScrollType    scroll)
261 {
262   gtk_binding_entry_add_signal (binding_set, keyval, mask,
263                                 "move-handle", 1,
264                                 GTK_TYPE_SCROLL_TYPE, scroll);
265 }
266
267 static void
268 gtk_paned_class_init (GtkPanedClass *class)
269 {
270   GObjectClass *object_class;
271   GtkWidgetClass *widget_class;
272   GtkContainerClass *container_class;
273   GtkPanedClass *paned_class;
274   GtkBindingSet *binding_set;
275
276   object_class = (GObjectClass *) class;
277   widget_class = (GtkWidgetClass *) class;
278   container_class = (GtkContainerClass *) class;
279   paned_class = (GtkPanedClass *) class;
280
281   object_class->set_property = gtk_paned_set_property;
282   object_class->get_property = gtk_paned_get_property;
283   object_class->finalize = gtk_paned_finalize;
284
285   widget_class->get_preferred_width = gtk_paned_get_preferred_width;
286   widget_class->get_preferred_height = gtk_paned_get_preferred_height;
287   widget_class->size_allocate = gtk_paned_size_allocate;
288   widget_class->realize = gtk_paned_realize;
289   widget_class->unrealize = gtk_paned_unrealize;
290   widget_class->map = gtk_paned_map;
291   widget_class->unmap = gtk_paned_unmap;
292   widget_class->draw = gtk_paned_draw;
293   widget_class->focus = gtk_paned_focus;
294   widget_class->enter_notify_event = gtk_paned_enter;
295   widget_class->leave_notify_event = gtk_paned_leave;
296   widget_class->button_press_event = gtk_paned_button_press;
297   widget_class->button_release_event = gtk_paned_button_release;
298   widget_class->motion_notify_event = gtk_paned_motion;
299   widget_class->grab_broken_event = gtk_paned_grab_broken;
300   widget_class->grab_notify = gtk_paned_grab_notify;
301   widget_class->state_flags_changed = gtk_paned_state_flags_changed;
302
303   container_class->add = gtk_paned_add;
304   container_class->remove = gtk_paned_remove;
305   container_class->forall = gtk_paned_forall;
306   container_class->child_type = gtk_paned_child_type;
307   container_class->set_focus_child = gtk_paned_set_focus_child;
308   container_class->set_child_property = gtk_paned_set_child_property;
309   container_class->get_child_property = gtk_paned_get_child_property;
310   gtk_container_class_handle_border_width (container_class);
311
312   paned_class->cycle_child_focus = gtk_paned_cycle_child_focus;
313   paned_class->toggle_handle_focus = gtk_paned_toggle_handle_focus;
314   paned_class->move_handle = gtk_paned_move_handle;
315   paned_class->cycle_handle_focus = gtk_paned_cycle_handle_focus;
316   paned_class->accept_position = gtk_paned_accept_position;
317   paned_class->cancel_position = gtk_paned_cancel_position;
318
319   g_object_class_override_property (object_class,
320                                     PROP_ORIENTATION,
321                                     "orientation");
322
323   g_object_class_install_property (object_class,
324                                    PROP_POSITION,
325                                    g_param_spec_int ("position",
326                                                      P_("Position"),
327                                                      P_("Position of paned separator in pixels (0 means all the way to the left/top)"),
328                                                      0,
329                                                      G_MAXINT,
330                                                      0,
331                                                      GTK_PARAM_READWRITE));
332
333   g_object_class_install_property (object_class,
334                                    PROP_POSITION_SET,
335                                    g_param_spec_boolean ("position-set",
336                                                          P_("Position Set"),
337                                                          P_("TRUE if the Position property should be used"),
338                                                          FALSE,
339                                                          GTK_PARAM_READWRITE));
340
341   gtk_widget_class_install_style_property (widget_class,
342                                            g_param_spec_int ("handle-size",
343                                                              P_("Handle Size"),
344                                                              P_("Width of handle"),
345                                                              0,
346                                                              G_MAXINT,
347                                                              5,
348                                                              GTK_PARAM_READABLE));
349   /**
350    * GtkPaned:min-position:
351    *
352    * The smallest possible value for the position property. This property is derived from the
353    * size and shrinkability of the widget's children.
354    *
355    * Since: 2.4
356    */
357   g_object_class_install_property (object_class,
358                                    PROP_MIN_POSITION,
359                                    g_param_spec_int ("min-position",
360                                                      P_("Minimal Position"),
361                                                      P_("Smallest possible value for the \"position\" property"),
362                                                      0,
363                                                      G_MAXINT,
364                                                      0,
365                                                      GTK_PARAM_READABLE));
366
367   /**
368    * GtkPaned:max-position:
369    *
370    * The largest possible value for the position property. This property is derived from the
371    * size and shrinkability of the widget's children.
372    *
373    * Since: 2.4
374    */
375   g_object_class_install_property (object_class,
376                                    PROP_MAX_POSITION,
377                                    g_param_spec_int ("max-position",
378                                                      P_("Maximal Position"),
379                                                      P_("Largest possible value for the \"position\" property"),
380                                                      0,
381                                                      G_MAXINT,
382                                                      G_MAXINT,
383                                                      GTK_PARAM_READABLE));
384
385   /**
386    * GtkPaned:resize:
387    *
388    * The "resize" child property determines whether the child expands and
389    * shrinks along with the paned widget.
390    *
391    * Since: 2.4
392    */
393   gtk_container_class_install_child_property (container_class,
394                                               CHILD_PROP_RESIZE,
395                                               g_param_spec_boolean ("resize", 
396                                                                     P_("Resize"),
397                                                                     P_("If TRUE, the child expands and shrinks along with the paned widget"),
398                                                                     TRUE,
399                                                                     GTK_PARAM_READWRITE));
400
401   /**
402    * GtkPaned:shrink:
403    *
404    * The "shrink" child property determines whether the child can be made
405    * smaller than its requisition.
406    *
407    * Since: 2.4
408    */
409   gtk_container_class_install_child_property (container_class,
410                                               CHILD_PROP_SHRINK,
411                                               g_param_spec_boolean ("shrink", 
412                                                                     P_("Shrink"),
413                                                                     P_("If TRUE, the child can be made smaller than its requisition"),
414                                                                     TRUE,
415                                                                     GTK_PARAM_READWRITE));
416
417   /**
418    * GtkPaned::cycle-child-focus:
419    * @widget: the object that received the signal
420    * @reversed: whether cycling backward or forward
421    *
422    * The ::cycle-child-focus signal is a 
423    * <link linkend="keybinding-signals">keybinding signal</link>
424    * which gets emitted to cycle the focus between the children of the paned.
425    *
426    * The default binding is f6.
427    *
428    * Since: 2.0
429    */
430   signals [CYCLE_CHILD_FOCUS] =
431     g_signal_new (I_("cycle-child-focus"),
432                   G_TYPE_FROM_CLASS (object_class),
433                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
434                   G_STRUCT_OFFSET (GtkPanedClass, cycle_child_focus),
435                   NULL, NULL,
436                   _gtk_marshal_BOOLEAN__BOOLEAN,
437                   G_TYPE_BOOLEAN, 1,
438                   G_TYPE_BOOLEAN);
439
440   /**
441    * GtkPaned::toggle-handle-focus:
442    * @widget: the object that received the signal
443    *
444    * The ::toggle-handle-focus is a 
445    * <link linkend="keybinding-signals">keybinding signal</link>
446    * which gets emitted to accept the current position of the handle and then 
447    * move focus to the next widget in the focus chain.
448    *
449    * The default binding is Tab.
450    *
451    * Since: 2.0
452    */
453   signals [TOGGLE_HANDLE_FOCUS] =
454     g_signal_new (I_("toggle-handle-focus"),
455                   G_TYPE_FROM_CLASS (object_class),
456                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
457                   G_STRUCT_OFFSET (GtkPanedClass, toggle_handle_focus),
458                   NULL, NULL,
459                   _gtk_marshal_BOOLEAN__VOID,
460                   G_TYPE_BOOLEAN, 0);
461
462   /**
463    * GtkPaned::move-handle:
464    * @widget: the object that received the signal
465    * @scroll_type: a #GtkScrollType
466    *
467    * The ::move-handle signal is a 
468    * <link linkend="keybinding-signals">keybinding signal</link>
469    * which gets emitted to move the handle when the user is using key bindings 
470    * to move it.
471    *
472    * Since: 2.0
473    */
474   signals[MOVE_HANDLE] =
475     g_signal_new (I_("move-handle"),
476                   G_TYPE_FROM_CLASS (object_class),
477                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
478                   G_STRUCT_OFFSET (GtkPanedClass, move_handle),
479                   NULL, NULL,
480                   _gtk_marshal_BOOLEAN__ENUM,
481                   G_TYPE_BOOLEAN, 1,
482                   GTK_TYPE_SCROLL_TYPE);
483
484   /**
485    * GtkPaned::cycle-handle-focus:
486    * @widget: the object that received the signal
487    * @reversed: whether cycling backward or forward
488    *
489    * The ::cycle-handle-focus signal is a 
490    * <link linkend="keybinding-signals">keybinding signal</link>
491    * which gets emitted to cycle whether the paned should grab focus to allow
492    * the user to change position of the handle by using key bindings.
493    *
494    * The default binding for this signal is f8.
495    *
496    * Since: 2.0
497    */
498   signals [CYCLE_HANDLE_FOCUS] =
499     g_signal_new (I_("cycle-handle-focus"),
500                   G_TYPE_FROM_CLASS (object_class),
501                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
502                   G_STRUCT_OFFSET (GtkPanedClass, cycle_handle_focus),
503                   NULL, NULL,
504                   _gtk_marshal_BOOLEAN__BOOLEAN,
505                   G_TYPE_BOOLEAN, 1,
506                   G_TYPE_BOOLEAN);
507
508   /**
509    * GtkPaned::accept-position:
510    * @widget: the object that received the signal
511    *
512    * The ::accept-position signal is a 
513    * <link linkend="keybinding-signals">keybinding signal</link>
514    * which gets emitted to accept the current position of the handle when 
515    * moving it using key bindings.
516    *
517    * The default binding for this signal is Return or Space.
518    *
519    * Since: 2.0
520    */
521   signals [ACCEPT_POSITION] =
522     g_signal_new (I_("accept-position"),
523                   G_TYPE_FROM_CLASS (object_class),
524                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
525                   G_STRUCT_OFFSET (GtkPanedClass, accept_position),
526                   NULL, NULL,
527                   _gtk_marshal_BOOLEAN__VOID,
528                   G_TYPE_BOOLEAN, 0);
529
530   /**
531    * GtkPaned::cancel-position:
532    * @widget: the object that received the signal
533    *
534    * The ::cancel-position signal is a 
535    * <link linkend="keybinding-signals">keybinding signal</link>
536    * which gets emitted to cancel moving the position of the handle using key 
537    * bindings. The position of the handle will be reset to the value prior to 
538    * moving it.
539    *
540    * The default binding for this signal is Escape.
541    *
542    * Since: 2.0
543    */
544   signals [CANCEL_POSITION] =
545     g_signal_new (I_("cancel-position"),
546                   G_TYPE_FROM_CLASS (object_class),
547                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
548                   G_STRUCT_OFFSET (GtkPanedClass, cancel_position),
549                   NULL, NULL,
550                   _gtk_marshal_BOOLEAN__VOID,
551                   G_TYPE_BOOLEAN, 0);
552
553   binding_set = gtk_binding_set_by_class (class);
554
555   /* F6 and friends */
556   gtk_binding_entry_add_signal (binding_set,
557                                 GDK_KEY_F6, 0,
558                                 "cycle-child-focus", 1, 
559                                 G_TYPE_BOOLEAN, FALSE);
560   gtk_binding_entry_add_signal (binding_set,
561                                 GDK_KEY_F6, GDK_SHIFT_MASK,
562                                 "cycle-child-focus", 1,
563                                 G_TYPE_BOOLEAN, TRUE);
564
565   /* F8 and friends */
566   gtk_binding_entry_add_signal (binding_set,
567                                 GDK_KEY_F8, 0,
568                                 "cycle-handle-focus", 1,
569                                 G_TYPE_BOOLEAN, FALSE);
570  
571   gtk_binding_entry_add_signal (binding_set,
572                                 GDK_KEY_F8, GDK_SHIFT_MASK,
573                                 "cycle-handle-focus", 1,
574                                 G_TYPE_BOOLEAN, TRUE);
575  
576   add_tab_bindings (binding_set, 0);
577   add_tab_bindings (binding_set, GDK_CONTROL_MASK);
578   add_tab_bindings (binding_set, GDK_SHIFT_MASK);
579   add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK);
580
581   /* accept and cancel positions */
582   gtk_binding_entry_add_signal (binding_set,
583                                 GDK_KEY_Escape, 0,
584                                 "cancel-position", 0);
585
586   gtk_binding_entry_add_signal (binding_set,
587                                 GDK_KEY_Return, 0,
588                                 "accept-position", 0);
589   gtk_binding_entry_add_signal (binding_set,
590                                 GDK_KEY_ISO_Enter, 0,
591                                 "accept-position", 0);
592   gtk_binding_entry_add_signal (binding_set,
593                                 GDK_KEY_KP_Enter, 0,
594                                 "accept-position", 0);
595   gtk_binding_entry_add_signal (binding_set,
596                                 GDK_KEY_space, 0,
597                                 "accept-position", 0);
598   gtk_binding_entry_add_signal (binding_set,
599                                 GDK_KEY_KP_Space, 0,
600                                 "accept-position", 0);
601
602   /* move handle */
603   add_move_binding (binding_set, GDK_KEY_Left, 0, GTK_SCROLL_STEP_LEFT);
604   add_move_binding (binding_set, GDK_KEY_KP_Left, 0, GTK_SCROLL_STEP_LEFT);
605   add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
606   add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
607
608   add_move_binding (binding_set, GDK_KEY_Right, 0, GTK_SCROLL_STEP_RIGHT);
609   add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
610   add_move_binding (binding_set, GDK_KEY_KP_Right, 0, GTK_SCROLL_STEP_RIGHT);
611   add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
612
613   add_move_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
614   add_move_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
615   add_move_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
616   add_move_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
617   add_move_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
618   add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0, GTK_SCROLL_PAGE_UP);
619
620   add_move_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
621   add_move_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
622   add_move_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
623   add_move_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
624   add_move_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
625   add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
626
627   add_move_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START);
628   add_move_binding (binding_set, GDK_KEY_KP_Home, 0, GTK_SCROLL_START);
629   add_move_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END);
630   add_move_binding (binding_set, GDK_KEY_KP_End, 0, GTK_SCROLL_END);
631
632   g_type_class_add_private (object_class, sizeof (GtkPanedPrivate));
633 }
634
635 static GType
636 gtk_paned_child_type (GtkContainer *container)
637 {
638   GtkPaned *paned = GTK_PANED (container);
639   GtkPanedPrivate *priv = paned->priv;
640
641   if (!priv->child1 || !priv->child2)
642     return GTK_TYPE_WIDGET;
643   else
644     return G_TYPE_NONE;
645 }
646
647 static void
648 gtk_paned_init (GtkPaned *paned)
649 {
650   GtkPanedPrivate *priv;
651
652   gtk_widget_set_has_window (GTK_WIDGET (paned), FALSE);
653   gtk_widget_set_can_focus (GTK_WIDGET (paned), TRUE);
654
655   /* We only need to redraw when the handle position moves, which is
656    * independent of the overall allocation of the GtkPaned
657    */
658   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (paned), FALSE);
659
660   paned->priv = G_TYPE_INSTANCE_GET_PRIVATE (paned, GTK_TYPE_PANED, GtkPanedPrivate);
661   priv = paned->priv;
662
663   priv->orientation = GTK_ORIENTATION_HORIZONTAL;
664   priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
665
666   priv->child1 = NULL;
667   priv->child2 = NULL;
668   priv->handle = NULL;
669   priv->cursor_type = GDK_CROSS;
670
671   priv->handle_pos.width = 5;
672   priv->handle_pos.height = 5;
673   priv->position_set = FALSE;
674   priv->last_allocation = -1;
675   priv->in_drag = FALSE;
676
677   priv->last_child1_focus = NULL;
678   priv->last_child2_focus = NULL;
679   priv->in_recursion = FALSE;
680   priv->handle_prelit = FALSE;
681   priv->original_position = -1;
682
683   priv->handle_pos.x = -1;
684   priv->handle_pos.y = -1;
685
686   priv->drag_pos = -1;
687 }
688
689 static void
690 gtk_paned_set_property (GObject        *object,
691                         guint           prop_id,
692                         const GValue   *value,
693                         GParamSpec     *pspec)
694 {
695   GtkPaned *paned = GTK_PANED (object);
696   GtkPanedPrivate *priv = paned->priv;
697
698   switch (prop_id)
699     {
700     case PROP_ORIENTATION:
701       priv->orientation = g_value_get_enum (value);
702
703       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
704         priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
705       else
706         priv->cursor_type = GDK_SB_V_DOUBLE_ARROW;
707
708       /* state_flags_changed updates the cursor */
709       gtk_paned_state_flags_changed (GTK_WIDGET (paned), 0);
710       gtk_widget_queue_resize (GTK_WIDGET (paned));
711       break;
712     case PROP_POSITION:
713       gtk_paned_set_position (paned, g_value_get_int (value));
714       break;
715     case PROP_POSITION_SET:
716       priv->position_set = g_value_get_boolean (value);
717       gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
718       break;
719     default:
720       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
721       break;
722     }
723 }
724
725 static void
726 gtk_paned_get_property (GObject        *object,
727                         guint           prop_id,
728                         GValue         *value,
729                         GParamSpec     *pspec)
730 {
731   GtkPaned *paned = GTK_PANED (object);
732   GtkPanedPrivate *priv = paned->priv;
733
734   switch (prop_id)
735     {
736     case PROP_ORIENTATION:
737       g_value_set_enum (value, priv->orientation);
738       break;
739     case PROP_POSITION:
740       g_value_set_int (value, priv->child1_size);
741       break;
742     case PROP_POSITION_SET:
743       g_value_set_boolean (value, priv->position_set);
744       break;
745     case PROP_MIN_POSITION:
746       g_value_set_int (value, priv->min_position);
747       break;
748     case PROP_MAX_POSITION:
749       g_value_set_int (value, priv->max_position);
750       break;
751     default:
752       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
753       break;
754     }
755 }
756
757 static void
758 gtk_paned_set_child_property (GtkContainer    *container,
759                               GtkWidget       *child,
760                               guint            property_id,
761                               const GValue    *value,
762                               GParamSpec      *pspec)
763 {
764   GtkPaned *paned = GTK_PANED (container);
765   GtkPanedPrivate *priv = paned->priv;
766   gboolean old_value, new_value;
767
768   g_assert (child == priv->child1 || child == priv->child2);
769
770   new_value = g_value_get_boolean (value);
771   switch (property_id)
772     {
773     case CHILD_PROP_RESIZE:
774       if (child == priv->child1)
775         {
776           old_value = priv->child1_resize;
777           priv->child1_resize = new_value;
778         }
779       else
780         {
781           old_value = priv->child2_resize;
782           priv->child2_resize = new_value;
783         }
784       break;
785     case CHILD_PROP_SHRINK:
786       if (child == priv->child1)
787         {
788           old_value = priv->child1_shrink;
789           priv->child1_shrink = new_value;
790         }
791       else
792         {
793           old_value = priv->child2_shrink;
794           priv->child2_shrink = new_value;
795         }
796       break;
797     default:
798       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
799       old_value = -1; /* quiet gcc */
800       break;
801     }
802   if (old_value != new_value)
803     gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
804 }
805
806 static void
807 gtk_paned_get_child_property (GtkContainer *container,
808                               GtkWidget    *child,
809                               guint         property_id,
810                               GValue       *value,
811                               GParamSpec   *pspec)
812 {
813   GtkPaned *paned = GTK_PANED (container);
814   GtkPanedPrivate *priv = paned->priv;
815
816   g_assert (child == priv->child1 || child == priv->child2);
817   
818   switch (property_id)
819     {
820     case CHILD_PROP_RESIZE:
821       if (child == priv->child1)
822         g_value_set_boolean (value, priv->child1_resize);
823       else
824         g_value_set_boolean (value, priv->child2_resize);
825       break;
826     case CHILD_PROP_SHRINK:
827       if (child == priv->child1)
828         g_value_set_boolean (value, priv->child1_shrink);
829       else
830         g_value_set_boolean (value, priv->child2_shrink);
831       break;
832     default:
833       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
834       break;
835     }
836 }
837
838 static void
839 gtk_paned_finalize (GObject *object)
840 {
841   GtkPaned *paned = GTK_PANED (object);
842   
843   gtk_paned_set_saved_focus (paned, NULL);
844   gtk_paned_set_first_paned (paned, NULL);
845
846   G_OBJECT_CLASS (gtk_paned_parent_class)->finalize (object);
847 }
848
849 static void
850 gtk_paned_get_preferred_size (GtkWidget      *widget,
851                               GtkOrientation  orientation,
852                               gint           *minimum,
853                               gint           *natural)
854 {
855   GtkPaned *paned = GTK_PANED (widget);
856   GtkPanedPrivate *priv = paned->priv;
857   gint child_min, child_nat;
858
859   *minimum = *natural = 0;
860
861   if (priv->child1 && gtk_widget_get_visible (priv->child1))
862     {
863       if (orientation == GTK_ORIENTATION_HORIZONTAL)
864         gtk_widget_get_preferred_width (priv->child1, &child_min, &child_nat);
865       else
866         gtk_widget_get_preferred_height (priv->child1, &child_min, &child_nat);
867
868       *minimum = child_min;
869       *natural = child_nat;
870     }
871
872   if (priv->child2 && gtk_widget_get_visible (priv->child2))
873     {
874       if (orientation == GTK_ORIENTATION_HORIZONTAL)
875         gtk_widget_get_preferred_width (priv->child2, &child_min, &child_nat);
876       else
877         gtk_widget_get_preferred_height (priv->child2, &child_min, &child_nat);
878
879       if (priv->orientation == orientation)
880         {
881           *minimum += child_min;
882           *natural += child_nat;
883         }
884       else
885         {
886           *minimum = MAX (*minimum, child_min);
887           *natural = MAX (*natural, child_nat);
888         }
889     }
890
891   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
892       priv->child2 && gtk_widget_get_visible (priv->child2))
893     {
894       gint handle_size;
895
896       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
897
898       if (priv->orientation == orientation)
899         {
900           *minimum += handle_size;
901           *natural += handle_size;
902         }
903     }
904 }
905
906 static void
907 gtk_paned_get_preferred_width (GtkWidget *widget,
908                                gint      *minimum,
909                                gint      *natural)
910 {
911   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
912 }
913
914 static void
915 gtk_paned_get_preferred_height (GtkWidget *widget,
916                                 gint      *minimum,
917                                 gint      *natural)
918 {
919   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum, natural);
920 }
921
922 static void
923 flip_child (GtkWidget     *widget,
924             GtkAllocation *child_pos)
925 {
926   GtkAllocation allocation;
927   gint x, width;
928
929   gtk_widget_get_allocation (widget, &allocation);
930   x = allocation.x;
931   width = allocation.width;
932
933   child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
934 }
935
936 static void
937 gtk_paned_size_allocate (GtkWidget     *widget,
938                          GtkAllocation *allocation)
939 {
940   GtkPaned *paned = GTK_PANED (widget);
941   GtkPanedPrivate *priv = paned->priv;
942   GtkAllocation widget_allocation;
943
944   gtk_widget_set_allocation (widget, allocation);
945
946   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
947       priv->child2 && gtk_widget_get_visible (priv->child2))
948     {
949       GtkRequisition child1_requisition;
950       GtkRequisition child2_requisition;
951       GtkAllocation child1_allocation;
952       GtkAllocation child2_allocation;
953       GtkAllocation priv_child1_allocation;
954       GdkRectangle old_handle_pos;
955       gint handle_size;
956
957       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
958
959       gtk_widget_get_preferred_size (priv->child1, &child1_requisition, NULL);
960       gtk_widget_get_preferred_size (priv->child2, &child2_requisition, NULL);
961
962       old_handle_pos = priv->handle_pos;
963
964       gtk_widget_get_allocation (widget, &widget_allocation);
965
966       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
967         {
968           gtk_paned_calc_position (paned,
969                                    MAX (1, widget_allocation.width - handle_size),
970                                    child1_requisition.width,
971                                    child2_requisition.width);
972
973           priv->handle_pos.x = widget_allocation.x + priv->child1_size;
974           priv->handle_pos.y = widget_allocation.y;
975           priv->handle_pos.width = handle_size;
976           priv->handle_pos.height = widget_allocation.height;
977
978           child1_allocation.height = child2_allocation.height = allocation->height;
979           child1_allocation.width = MAX (1, priv->child1_size);
980           child1_allocation.x = widget_allocation.x;
981           child1_allocation.y = child2_allocation.y = widget_allocation.y;
982
983           child2_allocation.x = child1_allocation.x + priv->child1_size + priv->handle_pos.width;
984           child2_allocation.width = MAX (1, widget_allocation.x + widget_allocation.width - child2_allocation.x);
985
986           if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
987             {
988               flip_child (widget, &(child2_allocation));
989               flip_child (widget, &(child1_allocation));
990               flip_child (widget, &(priv->handle_pos));
991             }
992         }
993       else
994         {
995           gtk_paned_calc_position (paned,
996                                    MAX (1, widget_allocation.height - handle_size),
997                                    child1_requisition.height,
998                                    child2_requisition.height);
999
1000           priv->handle_pos.x = widget_allocation.x;
1001           priv->handle_pos.y = widget_allocation.y + priv->child1_size;
1002           priv->handle_pos.width = widget_allocation.width;
1003           priv->handle_pos.height = handle_size;
1004
1005           child1_allocation.width = child2_allocation.width = allocation->width;
1006           child1_allocation.height = MAX (1, priv->child1_size);
1007           child1_allocation.x = child2_allocation.x = widget_allocation.x;
1008           child1_allocation.y = widget_allocation.y;
1009
1010           child2_allocation.y = child1_allocation.y + priv->child1_size + priv->handle_pos.height;
1011           child2_allocation.height = MAX (1, widget_allocation.y + widget_allocation.height - child2_allocation.y);
1012         }
1013
1014       if (gtk_widget_get_mapped (widget) &&
1015           (old_handle_pos.x != priv->handle_pos.x ||
1016            old_handle_pos.y != priv->handle_pos.y ||
1017            old_handle_pos.width != priv->handle_pos.width ||
1018            old_handle_pos.height != priv->handle_pos.height))
1019         {
1020           GdkWindow *window;
1021
1022           window = gtk_widget_get_window (widget);
1023           gdk_window_invalidate_rect (window, &old_handle_pos, FALSE);
1024           gdk_window_invalidate_rect (window, &priv->handle_pos, FALSE);
1025         }
1026
1027       if (gtk_widget_get_realized (widget))
1028         {
1029           if (gtk_widget_get_mapped (widget))
1030             gdk_window_show (priv->handle);
1031
1032           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1033             {
1034               gdk_window_move_resize (priv->handle,
1035                                       priv->handle_pos.x,
1036                                       priv->handle_pos.y,
1037                                       handle_size,
1038                                       priv->handle_pos.height);
1039             }
1040           else
1041             {
1042               gdk_window_move_resize (priv->handle,
1043                                       priv->handle_pos.x,
1044                                       priv->handle_pos.y,
1045                                       priv->handle_pos.width,
1046                                       handle_size);
1047             }
1048         }
1049
1050       /* Now allocate the childen, making sure, when resizing not to
1051        * overlap the windows
1052        */
1053       gtk_widget_get_allocation (priv->child1, &priv_child1_allocation);
1054       if (gtk_widget_get_mapped (widget) &&
1055           ((priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1056             priv_child1_allocation.width < child1_allocation.width) ||
1057
1058            (priv->orientation == GTK_ORIENTATION_VERTICAL &&
1059             priv_child1_allocation.height < child1_allocation.height)))
1060         {
1061           gtk_widget_size_allocate (priv->child2, &child2_allocation);
1062           gtk_widget_size_allocate (priv->child1, &child1_allocation);
1063         }
1064       else
1065         {
1066           gtk_widget_size_allocate (priv->child1, &child1_allocation);
1067           gtk_widget_size_allocate (priv->child2, &child2_allocation);
1068         }
1069     }
1070   else
1071     {
1072       GtkAllocation child_allocation;
1073
1074       if (gtk_widget_get_realized (widget))
1075         gdk_window_hide (priv->handle);
1076
1077       if (priv->child1)
1078         gtk_widget_set_child_visible (priv->child1, TRUE);
1079       if (priv->child2)
1080         gtk_widget_set_child_visible (priv->child2, TRUE);
1081
1082       gtk_widget_get_allocation (widget, &widget_allocation);
1083
1084       child_allocation.x = widget_allocation.x;
1085       child_allocation.y = widget_allocation.y;
1086       child_allocation.width = allocation->width;
1087       child_allocation.height = allocation->height;
1088
1089       if (priv->child1 && gtk_widget_get_visible (priv->child1))
1090         gtk_widget_size_allocate (priv->child1, &child_allocation);
1091       else if (priv->child2 && gtk_widget_get_visible (priv->child2))
1092         gtk_widget_size_allocate (priv->child2, &child_allocation);
1093     }
1094 }
1095
1096 static void
1097 gtk_paned_realize (GtkWidget *widget)
1098 {
1099   GtkPaned *paned = GTK_PANED (widget);
1100   GtkPanedPrivate *priv = paned->priv;
1101   GdkWindow *window;
1102   GdkWindowAttr attributes;
1103   gint attributes_mask;
1104
1105   gtk_widget_set_realized (widget, TRUE);
1106
1107   window = gtk_widget_get_parent_window (widget);
1108   gtk_widget_set_window (widget, window);
1109   g_object_ref (window);
1110
1111   attributes.window_type = GDK_WINDOW_CHILD;
1112   attributes.wclass = GDK_INPUT_ONLY;
1113   attributes.x = priv->handle_pos.x;
1114   attributes.y = priv->handle_pos.y;
1115   attributes.width = priv->handle_pos.width;
1116   attributes.height = priv->handle_pos.height;
1117   attributes.event_mask = gtk_widget_get_events (widget);
1118   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1119                             GDK_BUTTON_RELEASE_MASK |
1120                             GDK_ENTER_NOTIFY_MASK |
1121                             GDK_LEAVE_NOTIFY_MASK |
1122                             GDK_POINTER_MOTION_MASK |
1123                             GDK_POINTER_MOTION_HINT_MASK);
1124   attributes_mask = GDK_WA_X | GDK_WA_Y;
1125   if (gtk_widget_is_sensitive (widget))
1126     {
1127       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1128                                                       priv->cursor_type);
1129       attributes_mask |= GDK_WA_CURSOR;
1130     }
1131
1132   priv->handle = gdk_window_new (window,
1133                                  &attributes, attributes_mask);
1134   gdk_window_set_user_data (priv->handle, paned);
1135   if (attributes_mask & GDK_WA_CURSOR)
1136     g_object_unref (attributes.cursor);
1137
1138   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
1139       priv->child2 && gtk_widget_get_visible (priv->child2))
1140     gdk_window_show (priv->handle);
1141 }
1142
1143 static void
1144 gtk_paned_unrealize (GtkWidget *widget)
1145 {
1146   GtkPaned *paned = GTK_PANED (widget);
1147   GtkPanedPrivate *priv = paned->priv;
1148
1149   if (priv->handle)
1150     {
1151       gdk_window_set_user_data (priv->handle, NULL);
1152       gdk_window_destroy (priv->handle);
1153       priv->handle = NULL;
1154     }
1155
1156   gtk_paned_set_last_child1_focus (paned, NULL);
1157   gtk_paned_set_last_child2_focus (paned, NULL);
1158   gtk_paned_set_saved_focus (paned, NULL);
1159   gtk_paned_set_first_paned (paned, NULL);
1160
1161   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unrealize (widget);
1162 }
1163
1164 static void
1165 gtk_paned_map (GtkWidget *widget)
1166 {
1167   GtkPaned *paned = GTK_PANED (widget);
1168   GtkPanedPrivate *priv = paned->priv;
1169
1170   gdk_window_show (priv->handle);
1171
1172   GTK_WIDGET_CLASS (gtk_paned_parent_class)->map (widget);
1173 }
1174
1175 static void
1176 gtk_paned_unmap (GtkWidget *widget)
1177 {
1178   GtkPaned *paned = GTK_PANED (widget);
1179   GtkPanedPrivate *priv = paned->priv;
1180
1181   gdk_window_hide (priv->handle);
1182
1183   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unmap (widget);
1184 }
1185
1186 static gboolean
1187 gtk_paned_draw (GtkWidget *widget,
1188                 cairo_t   *cr)
1189 {
1190   GtkPaned *paned = GTK_PANED (widget);
1191   GtkPanedPrivate *priv = paned->priv;
1192
1193   if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
1194       priv->child1 && gtk_widget_get_visible (priv->child1) &&
1195       priv->child2 && gtk_widget_get_visible (priv->child2))
1196     {
1197       GtkStyleContext *context;
1198       GtkStateFlags state;
1199       GtkAllocation allocation;
1200
1201       gtk_widget_get_allocation (widget, &allocation);
1202       context = gtk_widget_get_style_context (widget);
1203       state = gtk_widget_get_state_flags (widget);
1204
1205       if (gtk_widget_is_focus (widget))
1206         state |= GTK_STATE_FLAG_SELECTED;
1207       if (priv->handle_prelit)
1208         state |= GTK_STATE_FLAG_PRELIGHT;
1209
1210       gtk_style_context_save (context);
1211       gtk_style_context_set_state (context, state);
1212       gtk_style_context_add_class (context, GTK_STYLE_CLASS_PANE_SEPARATOR);
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 }