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