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