]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
GtkThemingEngine: Fix handles rendering
[~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   GtkOrientation  orientation;
95   GtkPaned       *first_paned;
96   GtkWidget      *child1;
97   GtkWidget      *child2;
98   GtkWidget      *last_child1_focus;
99   GtkWidget      *last_child2_focus;
100   GtkWidget      *saved_focus;
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   guint         handle_prelit : 1;
115   guint         in_drag       : 1;
116   guint         in_recursion  : 1;
117   guint         child1_resize : 1;
118   guint         child1_shrink : 1;
119   guint         child2_resize : 1;
120   guint         child2_shrink : 1;
121   guint         position_set  : 1;
122
123   guint32       grab_time;
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   GtkStyleContext *context;
652
653   gtk_widget_set_has_window (GTK_WIDGET (paned), FALSE);
654   gtk_widget_set_can_focus (GTK_WIDGET (paned), TRUE);
655
656   /* We only need to redraw when the handle position moves, which is
657    * independent of the overall allocation of the GtkPaned
658    */
659   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (paned), FALSE);
660
661   paned->priv = G_TYPE_INSTANCE_GET_PRIVATE (paned, GTK_TYPE_PANED, GtkPanedPrivate);
662   priv = paned->priv;
663
664   priv->orientation = GTK_ORIENTATION_HORIZONTAL;
665   priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
666
667   priv->child1 = NULL;
668   priv->child2 = NULL;
669   priv->handle = NULL;
670   priv->cursor_type = GDK_CROSS;
671
672   priv->handle_pos.width = 5;
673   priv->handle_pos.height = 5;
674   priv->position_set = FALSE;
675   priv->last_allocation = -1;
676   priv->in_drag = FALSE;
677
678   priv->last_child1_focus = NULL;
679   priv->last_child2_focus = NULL;
680   priv->in_recursion = FALSE;
681   priv->handle_prelit = FALSE;
682   priv->original_position = -1;
683
684   priv->handle_pos.x = -1;
685   priv->handle_pos.y = -1;
686
687   priv->drag_pos = -1;
688
689   context = gtk_widget_get_style_context (GTK_WIDGET (paned));
690   gtk_style_context_add_class (context, GTK_STYLE_CLASS_PANE_SEPARATOR);
691 }
692
693 static void
694 gtk_paned_set_property (GObject        *object,
695                         guint           prop_id,
696                         const GValue   *value,
697                         GParamSpec     *pspec)
698 {
699   GtkPaned *paned = GTK_PANED (object);
700   GtkPanedPrivate *priv = paned->priv;
701
702   switch (prop_id)
703     {
704     case PROP_ORIENTATION:
705       priv->orientation = g_value_get_enum (value);
706
707       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
708         priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
709       else
710         priv->cursor_type = GDK_SB_V_DOUBLE_ARROW;
711
712       /* state_flags_changed updates the cursor */
713       gtk_paned_state_flags_changed (GTK_WIDGET (paned), 0);
714       gtk_widget_queue_resize (GTK_WIDGET (paned));
715       break;
716     case PROP_POSITION:
717       gtk_paned_set_position (paned, g_value_get_int (value));
718       break;
719     case PROP_POSITION_SET:
720       priv->position_set = g_value_get_boolean (value);
721       gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
722       break;
723     default:
724       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
725       break;
726     }
727 }
728
729 static void
730 gtk_paned_get_property (GObject        *object,
731                         guint           prop_id,
732                         GValue         *value,
733                         GParamSpec     *pspec)
734 {
735   GtkPaned *paned = GTK_PANED (object);
736   GtkPanedPrivate *priv = paned->priv;
737
738   switch (prop_id)
739     {
740     case PROP_ORIENTATION:
741       g_value_set_enum (value, priv->orientation);
742       break;
743     case PROP_POSITION:
744       g_value_set_int (value, priv->child1_size);
745       break;
746     case PROP_POSITION_SET:
747       g_value_set_boolean (value, priv->position_set);
748       break;
749     case PROP_MIN_POSITION:
750       g_value_set_int (value, priv->min_position);
751       break;
752     case PROP_MAX_POSITION:
753       g_value_set_int (value, priv->max_position);
754       break;
755     default:
756       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
757       break;
758     }
759 }
760
761 static void
762 gtk_paned_set_child_property (GtkContainer    *container,
763                               GtkWidget       *child,
764                               guint            property_id,
765                               const GValue    *value,
766                               GParamSpec      *pspec)
767 {
768   GtkPaned *paned = GTK_PANED (container);
769   GtkPanedPrivate *priv = paned->priv;
770   gboolean old_value, new_value;
771
772   g_assert (child == priv->child1 || child == priv->child2);
773
774   new_value = g_value_get_boolean (value);
775   switch (property_id)
776     {
777     case CHILD_PROP_RESIZE:
778       if (child == priv->child1)
779         {
780           old_value = priv->child1_resize;
781           priv->child1_resize = new_value;
782         }
783       else
784         {
785           old_value = priv->child2_resize;
786           priv->child2_resize = new_value;
787         }
788       break;
789     case CHILD_PROP_SHRINK:
790       if (child == priv->child1)
791         {
792           old_value = priv->child1_shrink;
793           priv->child1_shrink = new_value;
794         }
795       else
796         {
797           old_value = priv->child2_shrink;
798           priv->child2_shrink = new_value;
799         }
800       break;
801     default:
802       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
803       old_value = -1; /* quiet gcc */
804       break;
805     }
806   if (old_value != new_value)
807     gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
808 }
809
810 static void
811 gtk_paned_get_child_property (GtkContainer *container,
812                               GtkWidget    *child,
813                               guint         property_id,
814                               GValue       *value,
815                               GParamSpec   *pspec)
816 {
817   GtkPaned *paned = GTK_PANED (container);
818   GtkPanedPrivate *priv = paned->priv;
819
820   g_assert (child == priv->child1 || child == priv->child2);
821   
822   switch (property_id)
823     {
824     case CHILD_PROP_RESIZE:
825       if (child == priv->child1)
826         g_value_set_boolean (value, priv->child1_resize);
827       else
828         g_value_set_boolean (value, priv->child2_resize);
829       break;
830     case CHILD_PROP_SHRINK:
831       if (child == priv->child1)
832         g_value_set_boolean (value, priv->child1_shrink);
833       else
834         g_value_set_boolean (value, priv->child2_shrink);
835       break;
836     default:
837       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
838       break;
839     }
840 }
841
842 static void
843 gtk_paned_finalize (GObject *object)
844 {
845   GtkPaned *paned = GTK_PANED (object);
846   
847   gtk_paned_set_saved_focus (paned, NULL);
848   gtk_paned_set_first_paned (paned, NULL);
849
850   G_OBJECT_CLASS (gtk_paned_parent_class)->finalize (object);
851 }
852
853 static void
854 gtk_paned_get_preferred_size (GtkWidget      *widget,
855                               GtkOrientation  orientation,
856                               gint           *minimum,
857                               gint           *natural)
858 {
859   GtkPaned *paned = GTK_PANED (widget);
860   GtkPanedPrivate *priv = paned->priv;
861   gint child_min, child_nat;
862
863   *minimum = *natural = 0;
864
865   if (priv->child1 && gtk_widget_get_visible (priv->child1))
866     {
867       if (orientation == GTK_ORIENTATION_HORIZONTAL)
868         gtk_widget_get_preferred_width (priv->child1, &child_min, &child_nat);
869       else
870         gtk_widget_get_preferred_height (priv->child1, &child_min, &child_nat);
871
872       *minimum = child_min;
873       *natural = child_nat;
874     }
875
876   if (priv->child2 && gtk_widget_get_visible (priv->child2))
877     {
878       if (orientation == GTK_ORIENTATION_HORIZONTAL)
879         gtk_widget_get_preferred_width (priv->child2, &child_min, &child_nat);
880       else
881         gtk_widget_get_preferred_height (priv->child2, &child_min, &child_nat);
882
883       if (priv->orientation == orientation)
884         {
885           *minimum += child_min;
886           *natural += child_nat;
887         }
888       else
889         {
890           *minimum = MAX (*minimum, child_min);
891           *natural = MAX (*natural, child_nat);
892         }
893     }
894
895   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
896       priv->child2 && gtk_widget_get_visible (priv->child2))
897     {
898       gint handle_size;
899
900       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
901
902       if (priv->orientation == orientation)
903         {
904           *minimum += handle_size;
905           *natural += handle_size;
906         }
907     }
908 }
909
910 static void
911 gtk_paned_get_preferred_width (GtkWidget *widget,
912                                gint      *minimum,
913                                gint      *natural)
914 {
915   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
916 }
917
918 static void
919 gtk_paned_get_preferred_height (GtkWidget *widget,
920                                 gint      *minimum,
921                                 gint      *natural)
922 {
923   gtk_paned_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum, natural);
924 }
925
926 static void
927 flip_child (GtkWidget     *widget,
928             GtkAllocation *child_pos)
929 {
930   GtkAllocation allocation;
931   gint x, width;
932
933   gtk_widget_get_allocation (widget, &allocation);
934   x = allocation.x;
935   width = allocation.width;
936
937   child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
938 }
939
940 static void
941 gtk_paned_size_allocate (GtkWidget     *widget,
942                          GtkAllocation *allocation)
943 {
944   GtkPaned *paned = GTK_PANED (widget);
945   GtkPanedPrivate *priv = paned->priv;
946   GtkAllocation widget_allocation;
947
948   gtk_widget_set_allocation (widget, allocation);
949
950   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
951       priv->child2 && gtk_widget_get_visible (priv->child2))
952     {
953       GtkRequisition child1_requisition;
954       GtkRequisition child2_requisition;
955       GtkAllocation child1_allocation;
956       GtkAllocation child2_allocation;
957       GtkAllocation priv_child1_allocation;
958       GdkRectangle old_handle_pos;
959       gint handle_size;
960
961       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
962
963       gtk_widget_get_preferred_size (priv->child1, &child1_requisition, NULL);
964       gtk_widget_get_preferred_size (priv->child2, &child2_requisition, NULL);
965
966       old_handle_pos = priv->handle_pos;
967
968       gtk_widget_get_allocation (widget, &widget_allocation);
969
970       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
971         {
972           gtk_paned_calc_position (paned,
973                                    MAX (1, widget_allocation.width - handle_size),
974                                    child1_requisition.width,
975                                    child2_requisition.width);
976
977           priv->handle_pos.x = widget_allocation.x + priv->child1_size;
978           priv->handle_pos.y = widget_allocation.y;
979           priv->handle_pos.width = handle_size;
980           priv->handle_pos.height = widget_allocation.height;
981
982           child1_allocation.height = child2_allocation.height = allocation->height;
983           child1_allocation.width = MAX (1, priv->child1_size);
984           child1_allocation.x = widget_allocation.x;
985           child1_allocation.y = child2_allocation.y = widget_allocation.y;
986
987           child2_allocation.x = child1_allocation.x + priv->child1_size + priv->handle_pos.width;
988           child2_allocation.width = MAX (1, widget_allocation.x + widget_allocation.width - child2_allocation.x);
989
990           if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
991             {
992               flip_child (widget, &(child2_allocation));
993               flip_child (widget, &(child1_allocation));
994               flip_child (widget, &(priv->handle_pos));
995             }
996         }
997       else
998         {
999           gtk_paned_calc_position (paned,
1000                                    MAX (1, widget_allocation.height - handle_size),
1001                                    child1_requisition.height,
1002                                    child2_requisition.height);
1003
1004           priv->handle_pos.x = widget_allocation.x;
1005           priv->handle_pos.y = widget_allocation.y + priv->child1_size;
1006           priv->handle_pos.width = widget_allocation.width;
1007           priv->handle_pos.height = handle_size;
1008
1009           child1_allocation.width = child2_allocation.width = allocation->width;
1010           child1_allocation.height = MAX (1, priv->child1_size);
1011           child1_allocation.x = child2_allocation.x = widget_allocation.x;
1012           child1_allocation.y = widget_allocation.y;
1013
1014           child2_allocation.y = child1_allocation.y + priv->child1_size + priv->handle_pos.height;
1015           child2_allocation.height = MAX (1, widget_allocation.y + widget_allocation.height - child2_allocation.y);
1016         }
1017
1018       if (gtk_widget_get_mapped (widget) &&
1019           (old_handle_pos.x != priv->handle_pos.x ||
1020            old_handle_pos.y != priv->handle_pos.y ||
1021            old_handle_pos.width != priv->handle_pos.width ||
1022            old_handle_pos.height != priv->handle_pos.height))
1023         {
1024           GdkWindow *window;
1025
1026           window = gtk_widget_get_window (widget);
1027           gdk_window_invalidate_rect (window, &old_handle_pos, FALSE);
1028           gdk_window_invalidate_rect (window, &priv->handle_pos, FALSE);
1029         }
1030
1031       if (gtk_widget_get_realized (widget))
1032         {
1033           if (gtk_widget_get_mapped (widget))
1034             gdk_window_show (priv->handle);
1035
1036           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1037             {
1038               gdk_window_move_resize (priv->handle,
1039                                       priv->handle_pos.x,
1040                                       priv->handle_pos.y,
1041                                       handle_size,
1042                                       priv->handle_pos.height);
1043             }
1044           else
1045             {
1046               gdk_window_move_resize (priv->handle,
1047                                       priv->handle_pos.x,
1048                                       priv->handle_pos.y,
1049                                       priv->handle_pos.width,
1050                                       handle_size);
1051             }
1052         }
1053
1054       /* Now allocate the childen, making sure, when resizing not to
1055        * overlap the windows
1056        */
1057       gtk_widget_get_allocation (priv->child1, &priv_child1_allocation);
1058       if (gtk_widget_get_mapped (widget) &&
1059           ((priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1060             priv_child1_allocation.width < child1_allocation.width) ||
1061
1062            (priv->orientation == GTK_ORIENTATION_VERTICAL &&
1063             priv_child1_allocation.height < child1_allocation.height)))
1064         {
1065           gtk_widget_size_allocate (priv->child2, &child2_allocation);
1066           gtk_widget_size_allocate (priv->child1, &child1_allocation);
1067         }
1068       else
1069         {
1070           gtk_widget_size_allocate (priv->child1, &child1_allocation);
1071           gtk_widget_size_allocate (priv->child2, &child2_allocation);
1072         }
1073     }
1074   else
1075     {
1076       GtkAllocation child_allocation;
1077
1078       if (gtk_widget_get_realized (widget))
1079         gdk_window_hide (priv->handle);
1080
1081       if (priv->child1)
1082         gtk_widget_set_child_visible (priv->child1, TRUE);
1083       if (priv->child2)
1084         gtk_widget_set_child_visible (priv->child2, TRUE);
1085
1086       gtk_widget_get_allocation (widget, &widget_allocation);
1087
1088       child_allocation.x = widget_allocation.x;
1089       child_allocation.y = widget_allocation.y;
1090       child_allocation.width = allocation->width;
1091       child_allocation.height = allocation->height;
1092
1093       if (priv->child1 && gtk_widget_get_visible (priv->child1))
1094         gtk_widget_size_allocate (priv->child1, &child_allocation);
1095       else if (priv->child2 && gtk_widget_get_visible (priv->child2))
1096         gtk_widget_size_allocate (priv->child2, &child_allocation);
1097     }
1098 }
1099
1100 static void
1101 gtk_paned_realize (GtkWidget *widget)
1102 {
1103   GtkPaned *paned = GTK_PANED (widget);
1104   GtkPanedPrivate *priv = paned->priv;
1105   GdkWindow *window;
1106   GdkWindowAttr attributes;
1107   gint attributes_mask;
1108
1109   gtk_widget_set_realized (widget, TRUE);
1110
1111   window = gtk_widget_get_parent_window (widget);
1112   gtk_widget_set_window (widget, window);
1113   g_object_ref (window);
1114
1115   attributes.window_type = GDK_WINDOW_CHILD;
1116   attributes.wclass = GDK_INPUT_ONLY;
1117   attributes.x = priv->handle_pos.x;
1118   attributes.y = priv->handle_pos.y;
1119   attributes.width = priv->handle_pos.width;
1120   attributes.height = priv->handle_pos.height;
1121   attributes.event_mask = gtk_widget_get_events (widget);
1122   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1123                             GDK_BUTTON_RELEASE_MASK |
1124                             GDK_ENTER_NOTIFY_MASK |
1125                             GDK_LEAVE_NOTIFY_MASK |
1126                             GDK_POINTER_MOTION_MASK |
1127                             GDK_POINTER_MOTION_HINT_MASK);
1128   attributes_mask = GDK_WA_X | GDK_WA_Y;
1129   if (gtk_widget_is_sensitive (widget))
1130     {
1131       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1132                                                       priv->cursor_type);
1133       attributes_mask |= GDK_WA_CURSOR;
1134     }
1135
1136   priv->handle = gdk_window_new (window,
1137                                  &attributes, attributes_mask);
1138   gdk_window_set_user_data (priv->handle, paned);
1139   if (attributes_mask & GDK_WA_CURSOR)
1140     g_object_unref (attributes.cursor);
1141
1142   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
1143       priv->child2 && gtk_widget_get_visible (priv->child2))
1144     gdk_window_show (priv->handle);
1145 }
1146
1147 static void
1148 gtk_paned_unrealize (GtkWidget *widget)
1149 {
1150   GtkPaned *paned = GTK_PANED (widget);
1151   GtkPanedPrivate *priv = paned->priv;
1152
1153   if (priv->handle)
1154     {
1155       gdk_window_set_user_data (priv->handle, NULL);
1156       gdk_window_destroy (priv->handle);
1157       priv->handle = NULL;
1158     }
1159
1160   gtk_paned_set_last_child1_focus (paned, NULL);
1161   gtk_paned_set_last_child2_focus (paned, NULL);
1162   gtk_paned_set_saved_focus (paned, NULL);
1163   gtk_paned_set_first_paned (paned, NULL);
1164
1165   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unrealize (widget);
1166 }
1167
1168 static void
1169 gtk_paned_map (GtkWidget *widget)
1170 {
1171   GtkPaned *paned = GTK_PANED (widget);
1172   GtkPanedPrivate *priv = paned->priv;
1173
1174   gdk_window_show (priv->handle);
1175
1176   GTK_WIDGET_CLASS (gtk_paned_parent_class)->map (widget);
1177 }
1178
1179 static void
1180 gtk_paned_unmap (GtkWidget *widget)
1181 {
1182   GtkPaned *paned = GTK_PANED (widget);
1183   GtkPanedPrivate *priv = paned->priv;
1184
1185   gdk_window_hide (priv->handle);
1186
1187   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unmap (widget);
1188 }
1189
1190 static gboolean
1191 gtk_paned_draw (GtkWidget *widget,
1192                 cairo_t   *cr)
1193 {
1194   GtkPaned *paned = GTK_PANED (widget);
1195   GtkPanedPrivate *priv = paned->priv;
1196
1197   if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
1198       priv->child1 && gtk_widget_get_visible (priv->child1) &&
1199       priv->child2 && gtk_widget_get_visible (priv->child2))
1200     {
1201       GtkStyleContext *context;
1202       GtkStateFlags state;
1203       GtkAllocation allocation;
1204
1205       gtk_widget_get_allocation (widget, &allocation);
1206       context = gtk_widget_get_style_context (widget);
1207       state = gtk_widget_get_state_flags (widget);
1208
1209       if (gtk_widget_is_focus (widget))
1210         state |= GTK_STATE_FLAG_SELECTED;
1211       if (priv->handle_prelit)
1212         state |= GTK_STATE_FLAG_PRELIGHT;
1213
1214       gtk_style_context_save (context);
1215       gtk_style_context_set_state (context, state);
1216       gtk_render_handle (context, cr,
1217                          priv->handle_pos.x - allocation.x,
1218                          priv->handle_pos.y - allocation.y,
1219                          priv->handle_pos.width,
1220                          priv->handle_pos.height);
1221
1222       gtk_style_context_restore (context);
1223     }
1224
1225   /* Chain up to draw children */
1226   GTK_WIDGET_CLASS (gtk_paned_parent_class)->draw (widget, cr);
1227   
1228   return FALSE;
1229 }
1230
1231 static gboolean
1232 is_rtl (GtkPaned *paned)
1233 {
1234   GtkPanedPrivate *priv = paned->priv;
1235
1236   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1237       gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL)
1238     {
1239       return TRUE;
1240     }
1241
1242   return FALSE;
1243 }
1244
1245 static void
1246 update_drag (GtkPaned *paned)
1247 {
1248   GtkPanedPrivate *priv = paned->priv;
1249   GtkAllocation allocation;
1250   GtkWidget *widget = GTK_WIDGET (paned);
1251   gint pos;
1252   gint handle_size;
1253   gint size;
1254
1255   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1256     gtk_widget_get_pointer (widget, &pos, NULL);
1257   else
1258     gtk_widget_get_pointer (widget, NULL, &pos);
1259
1260   pos -= priv->drag_pos;
1261
1262   if (is_rtl (paned))
1263     {
1264       gtk_widget_style_get (widget,
1265                             "handle-size", &handle_size,
1266                             NULL);
1267
1268       gtk_widget_get_allocation (widget, &allocation);
1269       size = allocation.width - pos - handle_size;
1270     }
1271   else
1272     {
1273       size = pos;
1274     }
1275
1276   size = CLAMP (size, priv->min_position, priv->max_position);
1277
1278   if (size != priv->child1_size)
1279     gtk_paned_set_position (paned, size);
1280 }
1281
1282 static gboolean
1283 gtk_paned_enter (GtkWidget        *widget,
1284                  GdkEventCrossing *event)
1285 {
1286   GtkPaned *paned = GTK_PANED (widget);
1287   GtkPanedPrivate *priv = paned->priv;
1288
1289   if (priv->in_drag)
1290     update_drag (paned);
1291   else
1292     {
1293       priv->handle_prelit = TRUE;
1294       gtk_widget_queue_draw_area (widget,
1295                                   priv->handle_pos.x,
1296                                   priv->handle_pos.y,
1297                                   priv->handle_pos.width,
1298                                   priv->handle_pos.height);
1299     }
1300   
1301   return TRUE;
1302 }
1303
1304 static gboolean
1305 gtk_paned_leave (GtkWidget        *widget,
1306                  GdkEventCrossing *event)
1307 {
1308   GtkPaned *paned = GTK_PANED (widget);
1309   GtkPanedPrivate *priv = paned->priv;
1310
1311   if (priv->in_drag)
1312     update_drag (paned);
1313   else
1314     {
1315       priv->handle_prelit = FALSE;
1316       gtk_widget_queue_draw_area (widget,
1317                                   priv->handle_pos.x,
1318                                   priv->handle_pos.y,
1319                                   priv->handle_pos.width,
1320                                   priv->handle_pos.height);
1321     }
1322
1323   return TRUE;
1324 }
1325
1326 static gboolean
1327 gtk_paned_focus (GtkWidget        *widget,
1328                  GtkDirectionType  direction)
1329
1330 {
1331   gboolean retval;
1332   
1333   /* This is a hack, but how can this be done without
1334    * excessive cut-and-paste from gtkcontainer.c?
1335    */
1336
1337   gtk_widget_set_can_focus (widget, FALSE);
1338   retval = GTK_WIDGET_CLASS (gtk_paned_parent_class)->focus (widget, direction);
1339   gtk_widget_set_can_focus (widget, TRUE);
1340
1341   return retval;
1342 }
1343
1344 static gboolean
1345 gtk_paned_button_press (GtkWidget      *widget,
1346                         GdkEventButton *event)
1347 {
1348   GtkPaned *paned = GTK_PANED (widget);
1349   GtkPanedPrivate *priv = paned->priv;
1350
1351   if (!priv->in_drag &&
1352       (event->window == priv->handle) && (event->button == 1))
1353     {
1354       /* We need a server grab here, not gtk_grab_add(), since
1355        * we don't want to pass events on to the widget's children */
1356       if (gdk_device_grab (event->device,
1357                            priv->handle,
1358                            GDK_OWNERSHIP_WINDOW, FALSE,
1359                            GDK_POINTER_MOTION_HINT_MASK
1360                            | GDK_BUTTON1_MOTION_MASK
1361                            | GDK_BUTTON_RELEASE_MASK
1362                            | GDK_ENTER_NOTIFY_MASK
1363                            | GDK_LEAVE_NOTIFY_MASK,
1364                            NULL, event->time) != GDK_GRAB_SUCCESS)
1365         return FALSE;
1366
1367       priv->in_drag = TRUE;
1368       priv->grab_time = event->time;
1369       priv->grab_device = event->device;
1370
1371       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1372         priv->drag_pos = event->x;
1373       else
1374         priv->drag_pos = event->y;
1375
1376       return TRUE;
1377     }
1378
1379   return FALSE;
1380 }
1381
1382 static gboolean
1383 gtk_paned_grab_broken (GtkWidget          *widget,
1384                        GdkEventGrabBroken *event)
1385 {
1386   GtkPaned *paned = GTK_PANED (widget);
1387   GtkPanedPrivate *priv = paned->priv;
1388
1389   priv->in_drag = FALSE;
1390   priv->drag_pos = -1;
1391   priv->position_set = TRUE;
1392
1393   return TRUE;
1394 }
1395
1396 static void
1397 stop_drag (GtkPaned *paned)
1398 {
1399   GtkPanedPrivate *priv = paned->priv;
1400
1401   priv->in_drag = FALSE;
1402   priv->drag_pos = -1;
1403   priv->position_set = TRUE;
1404
1405   gdk_device_ungrab (priv->grab_device,
1406                      priv->grab_time);
1407   priv->grab_device = NULL;
1408 }
1409
1410 static void
1411 gtk_paned_grab_notify (GtkWidget *widget,
1412                        gboolean   was_grabbed)
1413 {
1414   GtkPaned *paned = GTK_PANED (widget);
1415   GtkPanedPrivate *priv = paned->priv;
1416   GdkDevice *grab_device;
1417
1418   grab_device = priv->grab_device;
1419
1420   if (priv->in_drag && grab_device &&
1421       gtk_widget_device_is_shadowed (widget, grab_device))
1422     stop_drag (paned);
1423 }
1424
1425 static void
1426 gtk_paned_state_flags_changed (GtkWidget     *widget,
1427                                GtkStateFlags  previous_state)
1428 {
1429   GtkPaned *paned = GTK_PANED (widget);
1430   GtkPanedPrivate *priv = paned->priv;
1431   GdkCursor *cursor;
1432
1433   if (gtk_widget_get_realized (widget))
1434     {
1435       if (gtk_widget_is_sensitive (widget))
1436         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1437                                              priv->cursor_type);
1438       else
1439         cursor = NULL;
1440
1441       gdk_window_set_cursor (priv->handle, cursor);
1442
1443       if (cursor)
1444         g_object_unref (cursor);
1445     }
1446 }
1447
1448 static gboolean
1449 gtk_paned_button_release (GtkWidget      *widget,
1450                           GdkEventButton *event)
1451 {
1452   GtkPaned *paned = GTK_PANED (widget);
1453   GtkPanedPrivate *priv = paned->priv;
1454
1455   if (priv->in_drag && (event->button == 1))
1456     {
1457       stop_drag (paned);
1458
1459       return TRUE;
1460     }
1461
1462   return FALSE;
1463 }
1464
1465 static gboolean
1466 gtk_paned_motion (GtkWidget      *widget,
1467                   GdkEventMotion *event)
1468 {
1469   GtkPaned *paned = GTK_PANED (widget);
1470   GtkPanedPrivate *priv = paned->priv;
1471
1472   if (priv->in_drag)
1473     {
1474       update_drag (paned);
1475       return TRUE;
1476     }
1477   
1478   return FALSE;
1479 }
1480
1481 /**
1482  * gtk_paned_new:
1483  * @orientation: the paned's orientation.
1484  *
1485  * Creates a new #GtkPaned widget.
1486  *
1487  * Return value: a new #GtkPaned.
1488  *
1489  * Since: 3.0
1490  **/
1491 GtkWidget *
1492 gtk_paned_new (GtkOrientation orientation)
1493 {
1494   return g_object_new (GTK_TYPE_PANED,
1495                        "orientation", orientation,
1496                        NULL);
1497 }
1498
1499 /**
1500  * gtk_paned_add1:
1501  * @paned: a paned widget
1502  * @child: the child to add
1503  *
1504  * Adds a child to the top or left pane with default parameters. This is
1505  * equivalent to
1506  * <literal>gtk_paned_pack1 (paned, child, FALSE, TRUE)</literal>.
1507  */
1508 void
1509 gtk_paned_add1 (GtkPaned  *paned,
1510                 GtkWidget *widget)
1511 {
1512   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
1513 }
1514
1515 /**
1516  * gtk_paned_add2:
1517  * @paned: a paned widget
1518  * @child: the child to add
1519  *
1520  * Adds a child to the bottom or right pane with default parameters. This
1521  * is equivalent to
1522  * <literal>gtk_paned_pack2 (paned, child, TRUE, TRUE)</literal>.
1523  */
1524 void
1525 gtk_paned_add2 (GtkPaned  *paned,
1526                 GtkWidget *widget)
1527 {
1528   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
1529 }
1530
1531 /**
1532  * gtk_paned_pack1:
1533  * @paned: a paned widget
1534  * @child: the child to add
1535  * @resize: should this child expand when the paned widget is resized.
1536  * @shrink: can this child be made smaller than its requisition.
1537  *
1538  * Adds a child to the top or left pane.
1539  */
1540 void
1541 gtk_paned_pack1 (GtkPaned  *paned,
1542                  GtkWidget *child,
1543                  gboolean   resize,
1544                  gboolean   shrink)
1545 {
1546   GtkPanedPrivate *priv;
1547
1548   g_return_if_fail (GTK_IS_PANED (paned));
1549   g_return_if_fail (GTK_IS_WIDGET (child));
1550
1551   priv = paned->priv;
1552
1553   if (!priv->child1)
1554     {
1555       priv->child1 = child;
1556       priv->child1_resize = resize;
1557       priv->child1_shrink = shrink;
1558
1559       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1560     }
1561 }
1562
1563 /**
1564  * gtk_paned_pack2:
1565  * @paned: a paned widget
1566  * @child: the child to add
1567  * @resize: should this child expand when the paned widget is resized.
1568  * @shrink: can this child be made smaller than its requisition.
1569  *
1570  * Adds a child to the bottom or right pane.
1571  */
1572 void
1573 gtk_paned_pack2 (GtkPaned  *paned,
1574                  GtkWidget *child,
1575                  gboolean   resize,
1576                  gboolean   shrink)
1577 {
1578   GtkPanedPrivate *priv;
1579
1580   g_return_if_fail (GTK_IS_PANED (paned));
1581   g_return_if_fail (GTK_IS_WIDGET (child));
1582
1583   priv = paned->priv;
1584
1585   if (!priv->child2)
1586     {
1587       priv->child2 = child;
1588       priv->child2_resize = resize;
1589       priv->child2_shrink = shrink;
1590
1591       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1592     }
1593 }
1594
1595
1596 static void
1597 gtk_paned_add (GtkContainer *container,
1598                GtkWidget    *widget)
1599 {
1600   GtkPanedPrivate *priv;
1601   GtkPaned *paned;
1602
1603   g_return_if_fail (GTK_IS_PANED (container));
1604
1605   paned = GTK_PANED (container);
1606   priv = paned->priv;
1607
1608   if (!priv->child1)
1609     gtk_paned_add1 (paned, widget);
1610   else if (!priv->child2)
1611     gtk_paned_add2 (paned, widget);
1612   else
1613     g_warning ("GtkPaned cannot have more than 2 children\n");
1614 }
1615
1616 static void
1617 gtk_paned_remove (GtkContainer *container,
1618                   GtkWidget    *widget)
1619 {
1620   GtkPaned *paned = GTK_PANED (container);
1621   GtkPanedPrivate *priv = paned->priv;
1622   gboolean was_visible;
1623
1624   was_visible = gtk_widget_get_visible (widget);
1625
1626   if (priv->child1 == widget)
1627     {
1628       gtk_widget_unparent (widget);
1629
1630       priv->child1 = NULL;
1631
1632       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1633         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1634     }
1635   else if (priv->child2 == widget)
1636     {
1637       gtk_widget_unparent (widget);
1638
1639       priv->child2 = NULL;
1640
1641       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1642         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1643     }
1644 }
1645
1646 static void
1647 gtk_paned_forall (GtkContainer *container,
1648                   gboolean      include_internals,
1649                   GtkCallback   callback,
1650                   gpointer      callback_data)
1651 {
1652   GtkPanedPrivate *priv;
1653   GtkPaned *paned;
1654
1655   g_return_if_fail (callback != NULL);
1656
1657   paned = GTK_PANED (container);
1658   priv = paned->priv;
1659
1660   if (priv->child1)
1661     (*callback) (priv->child1, callback_data);
1662   if (priv->child2)
1663     (*callback) (priv->child2, callback_data);
1664 }
1665
1666 /**
1667  * gtk_paned_get_position:
1668  * @paned: a #GtkPaned widget
1669  * 
1670  * Obtains the position of the divider between the two panes.
1671  * 
1672  * Return value: position of the divider
1673  **/
1674 gint
1675 gtk_paned_get_position (GtkPaned  *paned)
1676 {
1677   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1678
1679   return paned->priv->child1_size;
1680 }
1681
1682 /**
1683  * gtk_paned_set_position:
1684  * @paned: a #GtkPaned widget
1685  * @position: pixel position of divider, a negative value means that the position
1686  *            is unset.
1687  * 
1688  * Sets the position of the divider between the two panes.
1689  **/
1690 void
1691 gtk_paned_set_position (GtkPaned *paned,
1692                         gint      position)
1693 {
1694   GtkPanedPrivate *priv;
1695   GObject *object;
1696
1697   g_return_if_fail (GTK_IS_PANED (paned));
1698
1699   priv = paned->priv;
1700
1701   if (priv->child1_size == position)
1702     return;
1703
1704   object = G_OBJECT (paned);
1705   
1706   if (position >= 0)
1707     {
1708       /* We don't clamp here - the assumption is that
1709        * if the total allocation changes at the same time
1710        * as the position, the position set is with reference
1711        * to the new total size. If only the position changes,
1712        * then clamping will occur in gtk_paned_calc_position()
1713        */
1714
1715       priv->child1_size = position;
1716       priv->position_set = TRUE;
1717     }
1718   else
1719     {
1720       priv->position_set = FALSE;
1721     }
1722
1723   g_object_freeze_notify (object);
1724   g_object_notify (object, "position");
1725   g_object_notify (object, "position-set");
1726   g_object_thaw_notify (object);
1727
1728   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
1729
1730 #ifdef G_OS_WIN32
1731   /* Hacky work-around for bug #144269 */
1732   if (priv->child2 != NULL)
1733     {
1734       gtk_widget_queue_draw (priv->child2);
1735     }
1736 #endif
1737 }
1738
1739 /**
1740  * gtk_paned_get_child1:
1741  * @paned: a #GtkPaned widget
1742  * 
1743  * Obtains the first child of the paned widget.
1744  * 
1745  * Return value: (transfer none): first child, or %NULL if it is not set.
1746  *
1747  * Since: 2.4
1748  **/
1749 GtkWidget *
1750 gtk_paned_get_child1 (GtkPaned *paned)
1751 {
1752   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1753
1754   return paned->priv->child1;
1755 }
1756
1757 /**
1758  * gtk_paned_get_child2:
1759  * @paned: a #GtkPaned widget
1760  * 
1761  * Obtains the second child of the paned widget.
1762  * 
1763  * Return value: (transfer none): second child, or %NULL if it is not set.
1764  *
1765  * Since: 2.4
1766  **/
1767 GtkWidget *
1768 gtk_paned_get_child2 (GtkPaned *paned)
1769 {
1770   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1771
1772   return paned->priv->child2;
1773 }
1774
1775 static void
1776 gtk_paned_calc_position (GtkPaned *paned,
1777                          gint      allocation,
1778                          gint      child1_req,
1779                          gint      child2_req)
1780 {
1781   GtkPanedPrivate *priv = paned->priv;
1782   gint old_position;
1783   gint old_min_position;
1784   gint old_max_position;
1785
1786   old_position = priv->child1_size;
1787   old_min_position = priv->min_position;
1788   old_max_position = priv->max_position;
1789
1790   priv->min_position = priv->child1_shrink ? 0 : child1_req;
1791
1792   priv->max_position = allocation;
1793   if (!priv->child2_shrink)
1794     priv->max_position = MAX (1, priv->max_position - child2_req);
1795   priv->max_position = MAX (priv->min_position, priv->max_position);
1796
1797   if (!priv->position_set)
1798     {
1799       if (priv->child1_resize && !priv->child2_resize)
1800         priv->child1_size = MAX (0, allocation - child2_req);
1801       else if (!priv->child1_resize && priv->child2_resize)
1802         priv->child1_size = child1_req;
1803       else if (child1_req + child2_req != 0)
1804         priv->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
1805       else
1806         priv->child1_size = allocation * 0.5 + 0.5;
1807     }
1808   else
1809     {
1810       /* If the position was set before the initial allocation.
1811        * (priv->last_allocation <= 0) just clamp it and leave it.
1812        */
1813       if (priv->last_allocation > 0)
1814         {
1815           if (priv->child1_resize && !priv->child2_resize)
1816             priv->child1_size += allocation - priv->last_allocation;
1817           else if (!(!priv->child1_resize && priv->child2_resize))
1818             priv->child1_size = allocation * ((gdouble) priv->child1_size / (priv->last_allocation)) + 0.5;
1819         }
1820     }
1821
1822   priv->child1_size = CLAMP (priv->child1_size,
1823                               priv->min_position,
1824                               priv->max_position);
1825
1826   if (priv->child1)
1827     gtk_widget_set_child_visible (priv->child1, priv->child1_size != 0);
1828   
1829   if (priv->child2)
1830     gtk_widget_set_child_visible (priv->child2, priv->child1_size != allocation); 
1831
1832   g_object_freeze_notify (G_OBJECT (paned));
1833   if (priv->child1_size != old_position)
1834     g_object_notify (G_OBJECT (paned), "position");
1835   if (priv->min_position != old_min_position)
1836     g_object_notify (G_OBJECT (paned), "min-position");
1837   if (priv->max_position != old_max_position)
1838     g_object_notify (G_OBJECT (paned), "max-position");
1839   g_object_thaw_notify (G_OBJECT (paned));
1840
1841   priv->last_allocation = allocation;
1842 }
1843
1844 static void
1845 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1846 {
1847   GtkPanedPrivate *priv = paned->priv;
1848
1849   if (priv->saved_focus)
1850     g_object_remove_weak_pointer (G_OBJECT (priv->saved_focus),
1851                                   (gpointer *)&(priv->saved_focus));
1852
1853   priv->saved_focus = widget;
1854
1855   if (priv->saved_focus)
1856     g_object_add_weak_pointer (G_OBJECT (priv->saved_focus),
1857                                (gpointer *)&(priv->saved_focus));
1858 }
1859
1860 static void
1861 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1862 {
1863   GtkPanedPrivate *priv = paned->priv;
1864
1865   if (priv->first_paned)
1866     g_object_remove_weak_pointer (G_OBJECT (priv->first_paned),
1867                                   (gpointer *)&(priv->first_paned));
1868
1869   priv->first_paned = first_paned;
1870
1871   if (priv->first_paned)
1872     g_object_add_weak_pointer (G_OBJECT (priv->first_paned),
1873                                (gpointer *)&(priv->first_paned));
1874 }
1875
1876 static void
1877 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1878 {
1879   GtkPanedPrivate *priv = paned->priv;
1880
1881   if (priv->last_child1_focus)
1882     g_object_remove_weak_pointer (G_OBJECT (priv->last_child1_focus),
1883                                   (gpointer *)&(priv->last_child1_focus));
1884
1885   priv->last_child1_focus = widget;
1886
1887   if (priv->last_child1_focus)
1888     g_object_add_weak_pointer (G_OBJECT (priv->last_child1_focus),
1889                                (gpointer *)&(priv->last_child1_focus));
1890 }
1891
1892 static void
1893 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1894 {
1895   GtkPanedPrivate *priv = paned->priv;
1896
1897   if (priv->last_child2_focus)
1898     g_object_remove_weak_pointer (G_OBJECT (priv->last_child2_focus),
1899                                   (gpointer *)&(priv->last_child2_focus));
1900
1901   priv->last_child2_focus = widget;
1902
1903   if (priv->last_child2_focus)
1904     g_object_add_weak_pointer (G_OBJECT (priv->last_child2_focus),
1905                                (gpointer *)&(priv->last_child2_focus));
1906 }
1907
1908 static GtkWidget *
1909 paned_get_focus_widget (GtkPaned *paned)
1910 {
1911   GtkWidget *toplevel;
1912
1913   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1914   if (gtk_widget_is_toplevel (toplevel))
1915     return gtk_window_get_focus (GTK_WINDOW (toplevel));
1916
1917   return NULL;
1918 }
1919
1920 static void
1921 gtk_paned_set_focus_child (GtkContainer *container,
1922                            GtkWidget    *focus_child)
1923 {
1924   GtkPaned *paned;
1925   GtkPanedPrivate *priv;
1926   GtkWidget *container_focus_child;
1927
1928   g_return_if_fail (GTK_IS_PANED (container));
1929
1930   paned = GTK_PANED (container);
1931   priv = paned->priv;
1932
1933   if (focus_child == NULL)
1934     {
1935       GtkWidget *last_focus;
1936       GtkWidget *w;
1937       
1938       last_focus = paned_get_focus_widget (paned);
1939
1940       if (last_focus)
1941         {
1942           /* If there is one or more paned widgets between us and the
1943            * focus widget, we want the topmost of those as last_focus
1944            */
1945           for (w = last_focus; w != GTK_WIDGET (paned); w = gtk_widget_get_parent (w))
1946             if (GTK_IS_PANED (w))
1947               last_focus = w;
1948
1949           container_focus_child = gtk_container_get_focus_child (container);
1950           if (container_focus_child == priv->child1)
1951             gtk_paned_set_last_child1_focus (paned, last_focus);
1952           else if (container_focus_child == priv->child2)
1953             gtk_paned_set_last_child2_focus (paned, last_focus);
1954         }
1955     }
1956
1957   if (GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child)
1958     GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child (container, focus_child);
1959 }
1960
1961 static void
1962 gtk_paned_get_cycle_chain (GtkPaned          *paned,
1963                            GtkDirectionType   direction,
1964                            GList            **widgets)
1965 {
1966   GtkPanedPrivate *priv = paned->priv;
1967   GtkContainer *container = GTK_CONTAINER (paned);
1968   GtkWidget *ancestor = NULL;
1969   GtkWidget *focus_child;
1970   GtkWidget *parent;
1971   GtkWidget *widget = GTK_WIDGET (paned);
1972   GList *temp_list = NULL;
1973   GList *list;
1974
1975   if (priv->in_recursion)
1976     return;
1977
1978   g_assert (widgets != NULL);
1979
1980   if (priv->last_child1_focus &&
1981       !gtk_widget_is_ancestor (priv->last_child1_focus, widget))
1982     {
1983       gtk_paned_set_last_child1_focus (paned, NULL);
1984     }
1985
1986   if (priv->last_child2_focus &&
1987       !gtk_widget_is_ancestor (priv->last_child2_focus, widget))
1988     {
1989       gtk_paned_set_last_child2_focus (paned, NULL);
1990     }
1991
1992   parent = gtk_widget_get_parent (widget);
1993   if (parent)
1994     ancestor = gtk_widget_get_ancestor (parent, GTK_TYPE_PANED);
1995
1996   /* The idea here is that temp_list is a list of widgets we want to cycle
1997    * to. The list is prioritized so that the first element is our first
1998    * choice, the next our second, and so on.
1999    *
2000    * We can't just use g_list_reverse(), because we want to try
2001    * priv->last_child?_focus before priv->child?, both when we
2002    * are going forward and backward.
2003    */
2004   focus_child = gtk_container_get_focus_child (container);
2005   if (direction == GTK_DIR_TAB_FORWARD)
2006     {
2007       if (focus_child == priv->child1)
2008         {
2009           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2010           temp_list = g_list_append (temp_list, priv->child2);
2011           temp_list = g_list_append (temp_list, ancestor);
2012         }
2013       else if (focus_child == priv->child2)
2014         {
2015           temp_list = g_list_append (temp_list, ancestor);
2016           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2017           temp_list = g_list_append (temp_list, priv->child1);
2018         }
2019       else
2020         {
2021           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2022           temp_list = g_list_append (temp_list, priv->child1);
2023           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2024           temp_list = g_list_append (temp_list, priv->child2);
2025           temp_list = g_list_append (temp_list, ancestor);
2026         }
2027     }
2028   else
2029     {
2030       if (focus_child == priv->child1)
2031         {
2032           temp_list = g_list_append (temp_list, ancestor);
2033           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2034           temp_list = g_list_append (temp_list, priv->child2);
2035         }
2036       else if (focus_child == priv->child2)
2037         {
2038           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2039           temp_list = g_list_append (temp_list, priv->child1);
2040           temp_list = g_list_append (temp_list, ancestor);
2041         }
2042       else
2043         {
2044           temp_list = g_list_append (temp_list, priv->last_child2_focus);
2045           temp_list = g_list_append (temp_list, priv->child2);
2046           temp_list = g_list_append (temp_list, priv->last_child1_focus);
2047           temp_list = g_list_append (temp_list, priv->child1);
2048           temp_list = g_list_append (temp_list, ancestor);
2049         }
2050     }
2051
2052   /* Walk the list and expand all the paned widgets. */
2053   for (list = temp_list; list != NULL; list = list->next)
2054     {
2055       GtkWidget *widget = list->data;
2056
2057       if (widget)
2058         {
2059           if (GTK_IS_PANED (widget))
2060             {
2061               priv->in_recursion = TRUE;
2062               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
2063               priv->in_recursion = FALSE;
2064             }
2065           else
2066             {
2067               *widgets = g_list_append (*widgets, widget);
2068             }
2069         }
2070     }
2071
2072   g_list_free (temp_list);
2073 }
2074
2075 static gboolean
2076 gtk_paned_cycle_child_focus (GtkPaned *paned,
2077                              gboolean  reversed)
2078 {
2079   GList *cycle_chain = NULL;
2080   GList *list;
2081   
2082   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
2083
2084   /* ignore f6 if the handle is focused */
2085   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2086     return TRUE;
2087   
2088   /* we can't just let the event propagate up the hierarchy,
2089    * because the paned will want to cycle focus _unless_ an
2090    * ancestor paned handles the event
2091    */
2092   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
2093
2094   for (list = cycle_chain; list != NULL; list = list->next)
2095     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
2096       break;
2097
2098   g_list_free (cycle_chain);
2099   
2100   return TRUE;
2101 }
2102
2103 static void
2104 get_child_panes (GtkWidget  *widget,
2105                  GList     **panes)
2106 {
2107   if (!widget || !gtk_widget_get_realized (widget))
2108     return;
2109
2110   if (GTK_IS_PANED (widget))
2111     {
2112       GtkPaned *paned = GTK_PANED (widget);
2113       GtkPanedPrivate *priv = paned->priv;
2114
2115       get_child_panes (priv->child1, panes);
2116       *panes = g_list_prepend (*panes, widget);
2117       get_child_panes (priv->child2, panes);
2118     }
2119   else if (GTK_IS_CONTAINER (widget))
2120     {
2121       gtk_container_forall (GTK_CONTAINER (widget),
2122                             (GtkCallback)get_child_panes, panes);
2123     }
2124 }
2125
2126 static GList *
2127 get_all_panes (GtkPaned *paned)
2128 {
2129   GtkPaned *topmost = NULL;
2130   GList *result = NULL;
2131   GtkWidget *w;
2132
2133   for (w = GTK_WIDGET (paned); w != NULL; w = gtk_widget_get_parent (w))
2134     {
2135       if (GTK_IS_PANED (w))
2136         topmost = GTK_PANED (w);
2137     }
2138
2139   g_assert (topmost);
2140
2141   get_child_panes (GTK_WIDGET (topmost), &result);
2142
2143   return g_list_reverse (result);
2144 }
2145
2146 static void
2147 gtk_paned_find_neighbours (GtkPaned  *paned,
2148                            GtkPaned **next,
2149                            GtkPaned **prev)
2150 {
2151   GList *all_panes;
2152   GList *this_link;
2153
2154   all_panes = get_all_panes (paned);
2155   g_assert (all_panes);
2156
2157   this_link = g_list_find (all_panes, paned);
2158
2159   g_assert (this_link);
2160   
2161   if (this_link->next)
2162     *next = this_link->next->data;
2163   else
2164     *next = all_panes->data;
2165
2166   if (this_link->prev)
2167     *prev = this_link->prev->data;
2168   else
2169     *prev = g_list_last (all_panes)->data;
2170
2171   g_list_free (all_panes);
2172 }
2173
2174 static gboolean
2175 gtk_paned_move_handle (GtkPaned      *paned,
2176                        GtkScrollType  scroll)
2177 {
2178   GtkPanedPrivate *priv = paned->priv;
2179
2180   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2181     {
2182       gint old_position;
2183       gint new_position;
2184       gint increment;
2185       
2186       enum {
2187         SINGLE_STEP_SIZE = 1,
2188         PAGE_STEP_SIZE   = 75
2189       };
2190       
2191       new_position = old_position = gtk_paned_get_position (paned);
2192       increment = 0;
2193       
2194       switch (scroll)
2195         {
2196         case GTK_SCROLL_STEP_LEFT:
2197         case GTK_SCROLL_STEP_UP:
2198         case GTK_SCROLL_STEP_BACKWARD:
2199           increment = - SINGLE_STEP_SIZE;
2200           break;
2201           
2202         case GTK_SCROLL_STEP_RIGHT:
2203         case GTK_SCROLL_STEP_DOWN:
2204         case GTK_SCROLL_STEP_FORWARD:
2205           increment = SINGLE_STEP_SIZE;
2206           break;
2207           
2208         case GTK_SCROLL_PAGE_LEFT:
2209         case GTK_SCROLL_PAGE_UP:
2210         case GTK_SCROLL_PAGE_BACKWARD:
2211           increment = - PAGE_STEP_SIZE;
2212           break;
2213           
2214         case GTK_SCROLL_PAGE_RIGHT:
2215         case GTK_SCROLL_PAGE_DOWN:
2216         case GTK_SCROLL_PAGE_FORWARD:
2217           increment = PAGE_STEP_SIZE;
2218           break;
2219           
2220         case GTK_SCROLL_START:
2221           new_position = priv->min_position;
2222           break;
2223           
2224         case GTK_SCROLL_END:
2225           new_position = priv->max_position;
2226           break;
2227
2228         default:
2229           break;
2230         }
2231
2232       if (increment)
2233         {
2234           if (is_rtl (paned))
2235             increment = -increment;
2236           
2237           new_position = old_position + increment;
2238         }
2239       
2240       new_position = CLAMP (new_position, priv->min_position, priv->max_position);
2241       
2242       if (old_position != new_position)
2243         gtk_paned_set_position (paned, new_position);
2244
2245       return TRUE;
2246     }
2247
2248   return FALSE;
2249 }
2250
2251 static void
2252 gtk_paned_restore_focus (GtkPaned *paned)
2253 {
2254   GtkPanedPrivate *priv = paned->priv;
2255
2256   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2257     {
2258       if (priv->saved_focus &&
2259           gtk_widget_get_sensitive (priv->saved_focus))
2260         {
2261           gtk_widget_grab_focus (priv->saved_focus);
2262         }
2263       else
2264         {
2265           /* the saved focus is somehow not available for focusing,
2266            * try
2267            *   1) tabbing into the paned widget
2268            * if that didn't work,
2269            *   2) unset focus for the window if there is one
2270            */
2271           
2272           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
2273             {
2274               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2275               
2276               if (GTK_IS_WINDOW (toplevel))
2277                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
2278             }
2279         }
2280       
2281       gtk_paned_set_saved_focus (paned, NULL);
2282       gtk_paned_set_first_paned (paned, NULL);
2283     }
2284 }
2285
2286 static gboolean
2287 gtk_paned_accept_position (GtkPaned *paned)
2288 {
2289   GtkPanedPrivate *priv = paned->priv;
2290
2291   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2292     {
2293       priv->original_position = -1;
2294       gtk_paned_restore_focus (paned);
2295
2296       return TRUE;
2297     }
2298
2299   return FALSE;
2300 }
2301
2302
2303 static gboolean
2304 gtk_paned_cancel_position (GtkPaned *paned)
2305 {
2306   GtkPanedPrivate *priv = paned->priv;
2307
2308   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2309     {
2310       if (priv->original_position != -1)
2311         {
2312           gtk_paned_set_position (paned, priv->original_position);
2313           priv->original_position = -1;
2314         }
2315
2316       gtk_paned_restore_focus (paned);
2317       return TRUE;
2318     }
2319
2320   return FALSE;
2321 }
2322
2323 static gboolean
2324 gtk_paned_cycle_handle_focus (GtkPaned *paned,
2325                               gboolean  reversed)
2326 {
2327   GtkPanedPrivate *priv = paned->priv;
2328   GtkPaned *next, *prev;
2329
2330   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2331     {
2332       GtkPaned *focus = NULL;
2333
2334       if (!priv->first_paned)
2335         {
2336           /* The first_pane has disappeared. As an ad-hoc solution,
2337            * we make the currently focused paned the first_paned. To the
2338            * user this will seem like the paned cycling has been reset.
2339            */
2340           
2341           gtk_paned_set_first_paned (paned, paned);
2342         }
2343       
2344       gtk_paned_find_neighbours (paned, &next, &prev);
2345
2346       if (reversed && prev &&
2347           prev != paned && paned != priv->first_paned)
2348         {
2349           focus = prev;
2350         }
2351       else if (!reversed && next &&
2352                next != paned && next != priv->first_paned)
2353         {
2354           focus = next;
2355         }
2356       else
2357         {
2358           gtk_paned_accept_position (paned);
2359           return TRUE;
2360         }
2361
2362       g_assert (focus);
2363       
2364       gtk_paned_set_saved_focus (focus, priv->saved_focus);
2365       gtk_paned_set_first_paned (focus, priv->first_paned);
2366       
2367       gtk_paned_set_saved_focus (paned, NULL);
2368       gtk_paned_set_first_paned (paned, NULL);
2369       
2370       gtk_widget_grab_focus (GTK_WIDGET (focus));
2371       
2372       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
2373         {
2374           priv->original_position = -1;
2375           focus->priv->original_position = gtk_paned_get_position (focus);
2376         }
2377     }
2378   else
2379     {
2380       GtkContainer *container = GTK_CONTAINER (paned);
2381       GtkPaned *focus;
2382       GtkPaned *first;
2383       GtkPaned *prev, *next;
2384       GtkWidget *toplevel;
2385       GtkWidget *focus_child;
2386
2387       gtk_paned_find_neighbours (paned, &next, &prev);
2388       focus_child = gtk_container_get_focus_child (container);
2389
2390       if (focus_child == priv->child1)
2391         {
2392           if (reversed)
2393             {
2394               focus = prev;
2395               first = paned;
2396             }
2397           else
2398             {
2399               focus = paned;
2400               first = paned;
2401             }
2402         }
2403       else if (focus_child == priv->child2)
2404         {
2405           if (reversed)
2406             {
2407               focus = paned;
2408               first = next;
2409             }
2410           else
2411             {
2412               focus = next;
2413               first = next;
2414             }
2415         }
2416       else
2417         {
2418           /* Focus is not inside this paned, and we don't have focus.
2419            * Presumably this happened because the application wants us
2420            * to start keyboard navigating.
2421            */
2422           focus = paned;
2423
2424           if (reversed)
2425             first = paned;
2426           else
2427             first = next;
2428         }
2429
2430       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2431
2432       if (GTK_IS_WINDOW (toplevel))
2433         gtk_paned_set_saved_focus (focus, gtk_window_get_focus (GTK_WINDOW (toplevel)));
2434       gtk_paned_set_first_paned (focus, first);
2435       focus->priv->original_position = gtk_paned_get_position (focus);
2436
2437       gtk_widget_grab_focus (GTK_WIDGET (focus));
2438    }
2439
2440   return TRUE;
2441 }
2442
2443 static gboolean
2444 gtk_paned_toggle_handle_focus (GtkPaned *paned)
2445 {
2446   /* This function/signal has the wrong name. It is called when you
2447    * press Tab or Shift-Tab and what we do is act as if
2448    * the user pressed Return and then Tab or Shift-Tab
2449    */
2450   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2451     gtk_paned_accept_position (paned);
2452
2453   return FALSE;
2454 }
2455
2456 /**
2457  * gtk_paned_get_handle_window:
2458  * @paned: a #GtkPaned
2459  *
2460  * Returns the #GdkWindow of the handle. This function is
2461  * useful when handling button or motion events because it
2462  * enables the callback to distinguish between the window
2463  * of the paned, a child and the handle.
2464  *
2465  * Return value: (transfer none): the paned's handle window.
2466  *
2467  * Since: 2.20
2468  **/
2469 GdkWindow *
2470 gtk_paned_get_handle_window (GtkPaned *paned)
2471 {
2472   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2473
2474   return paned->priv->handle;
2475 }