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