]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
s/CYCLE_HANDLE_FOCUS/CYCLE_CHILD_FOCUS/
[~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 #include "gtkalias.h"
29 #include "gtkintl.h"
30 #include "gtkpaned.h"
31 #include "gtkbindings.h"
32 #include "gtksignal.h"
33 #include "gdk/gdkkeysyms.h"
34 #include "gtkwindow.h"
35 #include "gtkmain.h"
36 #include "gtkmarshalers.h"
37
38 enum {
39   PROP_0,
40   PROP_POSITION,
41   PROP_POSITION_SET,
42   PROP_MIN_POSITION,
43   PROP_MAX_POSITION
44 };
45
46 enum {
47   CHILD_PROP_0,
48   CHILD_PROP_RESIZE,
49   CHILD_PROP_SHRINK
50 };
51
52 enum {
53   CYCLE_CHILD_FOCUS,
54   TOGGLE_HANDLE_FOCUS,
55   MOVE_HANDLE,
56   CYCLE_HANDLE_FOCUS,
57   ACCEPT_POSITION,
58   CANCEL_POSITION,
59   LAST_SIGNAL
60 };
61
62 static void     gtk_paned_class_init            (GtkPanedClass    *klass);
63 static void     gtk_paned_init                  (GtkPaned         *paned);
64 static void     gtk_paned_set_property          (GObject          *object,
65                                                  guint             prop_id,
66                                                  const GValue     *value,
67                                                  GParamSpec       *pspec);
68 static void     gtk_paned_get_property          (GObject          *object,
69                                                  guint             prop_id,
70                                                  GValue           *value,
71                                                  GParamSpec       *pspec);
72 static void gtk_paned_set_child_property        (GtkContainer      *container,
73                                                  GtkWidget         *child,
74                                                  guint              property_id,
75                                                  const GValue      *value,
76                                                  GParamSpec        *pspec);
77 static void gtk_paned_get_child_property        (GtkContainer      *container,
78                                                  GtkWidget         *child,
79                                                  guint              property_id,
80                                                  GValue            *value,
81                                                  GParamSpec        *pspec);
82 static void     gtk_paned_finalize              (GObject          *object);
83 static void     gtk_paned_realize               (GtkWidget        *widget);
84 static void     gtk_paned_unrealize             (GtkWidget        *widget);
85 static void     gtk_paned_map                   (GtkWidget        *widget);
86 static void     gtk_paned_unmap                 (GtkWidget        *widget);
87 static gboolean gtk_paned_expose                (GtkWidget        *widget,
88                                                  GdkEventExpose   *event);
89 static gboolean gtk_paned_enter                 (GtkWidget        *widget,
90                                                  GdkEventCrossing *event);
91 static gboolean gtk_paned_leave                 (GtkWidget        *widget,
92                                                  GdkEventCrossing *event);
93 static gboolean gtk_paned_button_press          (GtkWidget        *widget,
94                                                  GdkEventButton   *event);
95 static gboolean gtk_paned_button_release        (GtkWidget        *widget,
96                                                  GdkEventButton   *event);
97 static gboolean gtk_paned_motion                (GtkWidget        *widget,
98                                                  GdkEventMotion   *event);
99 static gboolean gtk_paned_focus                 (GtkWidget        *widget,
100                                                  GtkDirectionType  direction);
101 static void     gtk_paned_add                   (GtkContainer     *container,
102                                                  GtkWidget        *widget);
103 static void     gtk_paned_remove                (GtkContainer     *container,
104                                                  GtkWidget        *widget);
105 static void     gtk_paned_forall                (GtkContainer     *container,
106                                                  gboolean          include_internals,
107                                                  GtkCallback       callback,
108                                                  gpointer          callback_data);
109 static void     gtk_paned_set_focus_child       (GtkContainer     *container,
110                                                  GtkWidget        *child);
111 static void     gtk_paned_set_saved_focus       (GtkPaned         *paned,
112                                                  GtkWidget        *widget);
113 static void     gtk_paned_set_first_paned       (GtkPaned         *paned,
114                                                  GtkPaned         *first_paned);
115 static void     gtk_paned_set_last_child1_focus (GtkPaned         *paned,
116                                                  GtkWidget        *widget);
117 static void     gtk_paned_set_last_child2_focus (GtkPaned         *paned,
118                                                  GtkWidget        *widget);
119 static gboolean gtk_paned_cycle_child_focus     (GtkPaned         *paned,
120                                                  gboolean          reverse);
121 static gboolean gtk_paned_cycle_handle_focus    (GtkPaned         *paned,
122                                                  gboolean          reverse);
123 static gboolean gtk_paned_move_handle           (GtkPaned         *paned,
124                                                  GtkScrollType     scroll);
125 static gboolean gtk_paned_accept_position       (GtkPaned         *paned);
126 static gboolean gtk_paned_cancel_position       (GtkPaned         *paned);
127 static gboolean gtk_paned_toggle_handle_focus   (GtkPaned         *paned);
128
129 static GType    gtk_paned_child_type             (GtkContainer     *container);
130
131 static GtkContainerClass *parent_class = NULL;
132
133 struct _GtkPanedPrivate
134 {
135   GtkWidget *saved_focus;
136   GtkPaned *first_paned;
137 };
138
139 GType
140 gtk_paned_get_type (void)
141 {
142   static GType paned_type = 0;
143   
144   if (!paned_type)
145     {
146       static const GTypeInfo paned_info =
147       {
148         sizeof (GtkPanedClass),
149         NULL,           /* base_init */
150         NULL,           /* base_finalize */
151         (GClassInitFunc) gtk_paned_class_init,
152         NULL,           /* class_finalize */
153         NULL,           /* class_data */
154         sizeof (GtkPaned),
155         0,              /* n_preallocs */
156         (GInstanceInitFunc) gtk_paned_init,
157         NULL,           /* value_table */
158       };
159
160       paned_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkPaned",
161                                            &paned_info, G_TYPE_FLAG_ABSTRACT);
162     }
163   
164   return paned_type;
165 }
166
167 static guint signals[LAST_SIGNAL] = { 0 };
168
169 static void
170 add_tab_bindings (GtkBindingSet    *binding_set,
171                   GdkModifierType   modifiers)
172 {
173   gtk_binding_entry_add_signal (binding_set, GDK_Tab, modifiers,
174                                 "toggle_handle_focus", 0);
175   gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, modifiers,
176                                 "toggle_handle_focus", 0);
177 }
178
179 static void
180 add_move_binding (GtkBindingSet   *binding_set,
181                   guint            keyval,
182                   GdkModifierType  mask,
183                   GtkScrollType    scroll)
184 {
185   gtk_binding_entry_add_signal (binding_set, keyval, mask,
186                                 "move_handle", 1,
187                                 GTK_TYPE_SCROLL_TYPE, scroll);
188 }
189
190 static void
191 gtk_paned_class_init (GtkPanedClass *class)
192 {
193   GObjectClass *object_class;
194   GtkWidgetClass *widget_class;
195   GtkContainerClass *container_class;
196   GtkPanedClass *paned_class;
197   GtkBindingSet *binding_set;
198
199   object_class = (GObjectClass *) class;
200   widget_class = (GtkWidgetClass *) class;
201   container_class = (GtkContainerClass *) class;
202   paned_class = (GtkPanedClass *) class;
203
204   parent_class = g_type_class_peek_parent (class);
205
206   object_class->set_property = gtk_paned_set_property;
207   object_class->get_property = gtk_paned_get_property;
208   object_class->finalize = gtk_paned_finalize;
209
210   widget_class->realize = gtk_paned_realize;
211   widget_class->unrealize = gtk_paned_unrealize;
212   widget_class->map = gtk_paned_map;
213   widget_class->unmap = gtk_paned_unmap;
214   widget_class->expose_event = gtk_paned_expose;
215   widget_class->focus = gtk_paned_focus;
216   widget_class->enter_notify_event = gtk_paned_enter;
217   widget_class->leave_notify_event = gtk_paned_leave;
218   widget_class->button_press_event = gtk_paned_button_press;
219   widget_class->button_release_event = gtk_paned_button_release;
220   widget_class->motion_notify_event = gtk_paned_motion;
221   
222   container_class->add = gtk_paned_add;
223   container_class->remove = gtk_paned_remove;
224   container_class->forall = gtk_paned_forall;
225   container_class->child_type = gtk_paned_child_type;
226   container_class->set_focus_child = gtk_paned_set_focus_child;
227   container_class->set_child_property = gtk_paned_set_child_property;
228   container_class->get_child_property = gtk_paned_get_child_property;
229
230   paned_class->cycle_child_focus = gtk_paned_cycle_child_focus;
231   paned_class->toggle_handle_focus = gtk_paned_toggle_handle_focus;
232   paned_class->move_handle = gtk_paned_move_handle;
233   paned_class->cycle_handle_focus = gtk_paned_cycle_handle_focus;
234   paned_class->accept_position = gtk_paned_accept_position;
235   paned_class->cancel_position = gtk_paned_cancel_position;
236
237   g_object_class_install_property (object_class,
238                                    PROP_POSITION,
239                                    g_param_spec_int ("position",
240                                                      P_("Position"),
241                                                      P_("Position of paned separator in pixels (0 means all the way to the left/top)"),
242                                                      0,
243                                                      G_MAXINT,
244                                                      0,
245                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
246   g_object_class_install_property (object_class,
247                                    PROP_POSITION_SET,
248                                    g_param_spec_boolean ("position_set",
249                                                          P_("Position Set"),
250                                                          P_("TRUE if the Position property should be used"),
251                                                          FALSE,
252                                                          G_PARAM_READABLE | G_PARAM_WRITABLE));
253                                    
254   gtk_widget_class_install_style_property (widget_class,
255                                            g_param_spec_int ("handle_size",
256                                                              P_("Handle Size"),
257                                                              P_("Width of handle"),
258                                                              0,
259                                                              G_MAXINT,
260                                                              5,
261                                                              G_PARAM_READABLE));
262   /**
263    * GtkPaned:min-position:
264    *
265    * The smallest possible value for the position property. This property is derived from the
266    * size and shrinkability of the widget's children.
267    *
268    * Since: 2.4
269    */
270   g_object_class_install_property (object_class,
271                                    PROP_MAX_POSITION,
272                                    g_param_spec_int ("min_position",
273                                                      P_("Minimal Position"),
274                                                      P_("Smallest possible value for the \"position\" property"),
275                                                      0,
276                                                      G_MAXINT,
277                                                      0,
278                                                      G_PARAM_READABLE));
279
280   /**
281    * GtkPaned:max-position:
282    *
283    * The largest possible value for the position property. This property is derived from the
284    * size and shrinkability of the widget's children.
285    *
286    * Since: 2.4
287    */
288   g_object_class_install_property (object_class,
289                                    PROP_MIN_POSITION,
290                                    g_param_spec_int ("max_position",
291                                                      P_("Maximal Position"),
292                                                      P_("Largest possible value for the \"position\" property"),
293                                                      0,
294                                                      G_MAXINT,
295                                                      G_MAXINT,
296                                                      G_PARAM_READABLE));
297
298 /**
299  * GtkPaned:resize:
300  *
301  * The "resize" child property determines whether the child expands and 
302  * shrinks along with the paned widget.
303  * 
304  * Since: 2.4 
305  */
306   gtk_container_class_install_child_property (container_class,
307                                               CHILD_PROP_RESIZE,
308                                               g_param_spec_boolean ("resize", 
309                                                                     P_("Resize"),
310                                                                     P_("If TRUE, the child expands and shrinks along with the paned widget"),
311                                                                     TRUE,
312                                                                     G_PARAM_READWRITE));
313
314 /**
315  * GtkPaned:shrink:
316  *
317  * The "shrink" child property determines whether the child can be made 
318  * smaller than its requisition.
319  * 
320  * Since: 2.4 
321  */
322   gtk_container_class_install_child_property (container_class,
323                                               CHILD_PROP_SHRINK,
324                                               g_param_spec_boolean ("shrink", 
325                                                                     P_("Shrink"),
326                                                                     P_("If TRUE, the child can be made smaller than its requisition"),
327                                                                     TRUE,
328                                                                     G_PARAM_READWRITE));
329
330   signals [CYCLE_CHILD_FOCUS] =
331     g_signal_new ("cycle_child_focus",
332                   G_TYPE_FROM_CLASS (object_class),
333                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
334                   G_STRUCT_OFFSET (GtkPanedClass, cycle_child_focus),
335                   NULL, NULL,
336                   _gtk_marshal_BOOLEAN__BOOLEAN,
337                   G_TYPE_BOOLEAN, 1,
338                   G_TYPE_BOOLEAN);
339
340   signals [TOGGLE_HANDLE_FOCUS] =
341     g_signal_new ("toggle_handle_focus",
342                   G_TYPE_FROM_CLASS (object_class),
343                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
344                   G_STRUCT_OFFSET (GtkPanedClass, toggle_handle_focus),
345                   NULL, NULL,
346                   _gtk_marshal_BOOLEAN__VOID,
347                   G_TYPE_BOOLEAN, 0);
348
349   signals[MOVE_HANDLE] =
350     g_signal_new ("move_handle",
351                   G_TYPE_FROM_CLASS (object_class),
352                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
353                   G_STRUCT_OFFSET (GtkPanedClass, move_handle),
354                   NULL, NULL,
355                   _gtk_marshal_BOOLEAN__ENUM,
356                   G_TYPE_BOOLEAN, 1,
357                   GTK_TYPE_SCROLL_TYPE);
358
359   signals [CYCLE_HANDLE_FOCUS] =
360     g_signal_new ("cycle_handle_focus",
361                   G_TYPE_FROM_CLASS (object_class),
362                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
363                   G_STRUCT_OFFSET (GtkPanedClass, cycle_handle_focus),
364                   NULL, NULL,
365                   _gtk_marshal_BOOLEAN__BOOLEAN,
366                   G_TYPE_BOOLEAN, 1,
367                   G_TYPE_BOOLEAN);
368
369   signals [ACCEPT_POSITION] =
370     g_signal_new ("accept_position",
371                   G_TYPE_FROM_CLASS (object_class),
372                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
373                   G_STRUCT_OFFSET (GtkPanedClass, accept_position),
374                   NULL, NULL,
375                   _gtk_marshal_BOOLEAN__VOID,
376                   G_TYPE_BOOLEAN, 0);
377
378   signals [CANCEL_POSITION] =
379     g_signal_new ("cancel_position",
380                   G_TYPE_FROM_CLASS (object_class),
381                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
382                   G_STRUCT_OFFSET (GtkPanedClass, cancel_position),
383                   NULL, NULL,
384                   _gtk_marshal_BOOLEAN__VOID,
385                   G_TYPE_BOOLEAN, 0);
386
387   binding_set = gtk_binding_set_by_class (class);
388
389   /* F6 and friends */
390   gtk_binding_entry_add_signal (binding_set,                            
391                                 GDK_F6, 0,
392                                 "cycle_child_focus", 1, 
393                                 G_TYPE_BOOLEAN, FALSE);
394   gtk_binding_entry_add_signal (binding_set,
395                                 GDK_F6, GDK_SHIFT_MASK,
396                                 "cycle_child_focus", 1,
397                                 G_TYPE_BOOLEAN, TRUE);
398
399   /* F8 and friends */
400   gtk_binding_entry_add_signal (binding_set,
401                                 GDK_F8, 0,
402                                 "cycle_handle_focus", 1,
403                                 G_TYPE_BOOLEAN, FALSE);
404  
405   gtk_binding_entry_add_signal (binding_set,
406                                 GDK_F8, GDK_SHIFT_MASK,
407                                 "cycle_handle_focus", 1,
408                                 G_TYPE_BOOLEAN, TRUE);
409  
410   add_tab_bindings (binding_set, 0);
411   add_tab_bindings (binding_set, GDK_CONTROL_MASK);
412   add_tab_bindings (binding_set, GDK_SHIFT_MASK);
413   add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK);
414
415   /* accept and cancel positions */
416   gtk_binding_entry_add_signal (binding_set,
417                                 GDK_Escape, 0,
418                                 "cancel_position", 0);
419
420   gtk_binding_entry_add_signal (binding_set,
421                                 GDK_Return, 0,
422                                 "accept_position", 0);
423   gtk_binding_entry_add_signal (binding_set,
424                                 GDK_KP_Enter, 0,
425                                 "accept_position", 0);
426   gtk_binding_entry_add_signal (binding_set,
427                                 GDK_space, 0,
428                                 "accept_position", 0);
429   gtk_binding_entry_add_signal (binding_set,
430                                 GDK_KP_Space, 0,
431                                 "accept_position", 0);
432
433   /* move handle */
434   add_move_binding (binding_set, GDK_Left, 0, GTK_SCROLL_STEP_LEFT);
435   add_move_binding (binding_set, GDK_KP_Left, 0, GTK_SCROLL_STEP_LEFT);
436   add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
437   add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
438
439   add_move_binding (binding_set, GDK_Right, 0, GTK_SCROLL_STEP_RIGHT);
440   add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
441   add_move_binding (binding_set, GDK_KP_Right, 0, GTK_SCROLL_STEP_RIGHT);
442   add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
443
444   add_move_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
445   add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
446   add_move_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
447   add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
448   add_move_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
449   add_move_binding (binding_set, GDK_KP_Page_Up, 0, GTK_SCROLL_PAGE_UP);
450
451   add_move_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
452   add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
453   add_move_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
454   add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
455   add_move_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
456   add_move_binding (binding_set, GDK_KP_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
457
458   add_move_binding (binding_set, GDK_Home, 0, GTK_SCROLL_START);
459   add_move_binding (binding_set, GDK_KP_Home, 0, GTK_SCROLL_START);
460   add_move_binding (binding_set, GDK_End, 0, GTK_SCROLL_END);
461   add_move_binding (binding_set, GDK_KP_End, 0, GTK_SCROLL_END);
462 }
463
464 static GType
465 gtk_paned_child_type (GtkContainer *container)
466 {
467   if (!GTK_PANED (container)->child1 || !GTK_PANED (container)->child2)
468     return GTK_TYPE_WIDGET;
469   else
470     return G_TYPE_NONE;
471 }
472
473 static void
474 gtk_paned_init (GtkPaned *paned)
475 {
476   GTK_WIDGET_SET_FLAGS (paned, GTK_NO_WINDOW | GTK_CAN_FOCUS);
477   
478   paned->child1 = NULL;
479   paned->child2 = NULL;
480   paned->handle = NULL;
481   paned->xor_gc = NULL;
482   paned->cursor_type = GDK_CROSS;
483   
484   paned->handle_pos.width = 5;
485   paned->handle_pos.height = 5;
486   paned->position_set = FALSE;
487   paned->last_allocation = -1;
488   paned->in_drag = FALSE;
489
490   paned->priv = g_new0 (GtkPanedPrivate, 1);
491   paned->last_child1_focus = NULL;
492   paned->last_child2_focus = NULL;
493   paned->in_recursion = FALSE;
494   paned->handle_prelit = FALSE;
495   paned->original_position = -1;
496   
497   paned->handle_pos.x = -1;
498   paned->handle_pos.y = -1;
499
500   paned->drag_pos = -1;
501 }
502
503 static void
504 gtk_paned_set_property (GObject        *object,
505                         guint           prop_id,
506                         const GValue   *value,
507                         GParamSpec     *pspec)
508 {
509   GtkPaned *paned = GTK_PANED (object);
510   
511   switch (prop_id)
512     {
513     case PROP_POSITION:
514       gtk_paned_set_position (paned, g_value_get_int (value));
515       break;
516     case PROP_POSITION_SET:
517       paned->position_set = g_value_get_boolean (value);
518       gtk_widget_queue_resize (GTK_WIDGET (paned));
519       break;
520     default:
521       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
522       break;
523     }
524 }
525
526 static void
527 gtk_paned_get_property (GObject        *object,
528                         guint           prop_id,
529                         GValue         *value,
530                         GParamSpec     *pspec)
531 {
532   GtkPaned *paned = GTK_PANED (object);
533   
534   switch (prop_id)
535     {
536     case PROP_POSITION:
537       g_value_set_int (value, paned->child1_size);
538       break;
539     case PROP_POSITION_SET:
540       g_value_set_boolean (value, paned->position_set);
541       break;
542     case PROP_MIN_POSITION:
543       g_value_set_int (value, paned->min_position);
544       break;
545     case PROP_MAX_POSITION:
546       g_value_set_int (value, paned->max_position);
547       break;
548     default:
549       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
550       break;
551     }
552 }
553
554 static void
555 gtk_paned_set_child_property (GtkContainer    *container,
556                               GtkWidget       *child,
557                               guint            property_id,
558                               const GValue    *value,
559                               GParamSpec      *pspec)
560 {
561   GtkPaned *paned = GTK_PANED (container);
562   gboolean old_value, new_value;
563
564   g_assert (child == paned->child1 || child == paned->child2);
565
566   new_value = g_value_get_boolean (value);
567   switch (property_id)
568     {
569     case CHILD_PROP_RESIZE:
570       if (child == paned->child1)
571         {
572           old_value = paned->child1_resize;
573           paned->child1_resize = new_value;
574         }
575       else
576         {
577           old_value = paned->child2_resize;
578           paned->child2_resize = new_value;
579         }
580       break;
581     case CHILD_PROP_SHRINK:
582       if (child == paned->child1)
583         {
584           old_value = paned->child1_shrink;
585           paned->child1_shrink = new_value;
586         }
587       else
588         {
589           old_value = paned->child2_shrink;
590           paned->child2_shrink = new_value;
591         }
592       break;
593     default:
594       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
595       old_value = -1; /* quiet gcc */
596       break;
597     }
598   if (old_value != new_value)
599     gtk_widget_queue_resize (GTK_WIDGET (container));
600 }
601
602 static void
603 gtk_paned_get_child_property (GtkContainer *container,
604                               GtkWidget    *child,
605                               guint         property_id,
606                               GValue       *value,
607                               GParamSpec   *pspec)
608 {
609   GtkPaned *paned = GTK_PANED (container);
610
611   g_assert (child == paned->child1 || child == paned->child2);
612   
613   switch (property_id)
614     {
615     case CHILD_PROP_RESIZE:
616       if (child == paned->child1)
617         g_value_set_boolean (value, paned->child1_resize);
618       else
619         g_value_set_boolean (value, paned->child2_resize);
620       break;
621     case CHILD_PROP_SHRINK:
622       if (child == paned->child1)
623         g_value_set_boolean (value, paned->child1_shrink);
624       else
625         g_value_set_boolean (value, paned->child2_shrink);
626       break;
627     default:
628       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
629       break;
630     }
631 }
632
633 static void
634 gtk_paned_finalize (GObject *object)
635 {
636   GtkPaned *paned = GTK_PANED (object);
637   
638   gtk_paned_set_saved_focus (paned, NULL);
639   gtk_paned_set_first_paned (paned, NULL);
640
641   g_free (paned->priv);
642
643   G_OBJECT_CLASS (parent_class)->finalize (object);
644 }
645
646 static void
647 gtk_paned_realize (GtkWidget *widget)
648 {
649   GtkPaned *paned;
650   GdkWindowAttr attributes;
651   gint attributes_mask;
652
653   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
654   paned = GTK_PANED (widget);
655
656   widget->window = gtk_widget_get_parent_window (widget);
657   g_object_ref (widget->window);
658   
659   attributes.window_type = GDK_WINDOW_CHILD;
660   attributes.wclass = GDK_INPUT_ONLY;
661   attributes.x = paned->handle_pos.x;
662   attributes.y = paned->handle_pos.y;
663   attributes.width = paned->handle_pos.width;
664   attributes.height = paned->handle_pos.height;
665   attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
666                                                   paned->cursor_type);
667   attributes.event_mask = gtk_widget_get_events (widget);
668   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
669                             GDK_BUTTON_RELEASE_MASK |
670                             GDK_ENTER_NOTIFY_MASK |
671                             GDK_LEAVE_NOTIFY_MASK |
672                             GDK_POINTER_MOTION_MASK |
673                             GDK_POINTER_MOTION_HINT_MASK);
674   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_CURSOR;
675
676   paned->handle = gdk_window_new (widget->window,
677                                   &attributes, attributes_mask);
678   gdk_window_set_user_data (paned->handle, paned);
679   gdk_cursor_unref (attributes.cursor);
680
681   widget->style = gtk_style_attach (widget->style, widget->window);
682
683   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
684       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
685     gdk_window_show (paned->handle);
686 }
687
688 static void
689 gtk_paned_unrealize (GtkWidget *widget)
690 {
691   GtkPaned *paned = GTK_PANED (widget);
692
693   if (paned->xor_gc)
694     {
695       g_object_unref (paned->xor_gc);
696       paned->xor_gc = NULL;
697     }
698
699   if (paned->handle)
700     {
701       gdk_window_set_user_data (paned->handle, NULL);
702       gdk_window_destroy (paned->handle);
703       paned->handle = NULL;
704     }
705
706   gtk_paned_set_last_child1_focus (paned, NULL);
707   gtk_paned_set_last_child2_focus (paned, NULL);
708   gtk_paned_set_saved_focus (paned, NULL);
709   gtk_paned_set_first_paned (paned, NULL);
710   
711   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
712     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
713 }
714
715 static void
716 gtk_paned_map (GtkWidget *widget)
717 {
718   GtkPaned *paned = GTK_PANED (widget);
719
720   gdk_window_show (paned->handle);
721
722   GTK_WIDGET_CLASS (parent_class)->map (widget);
723 }
724
725 static void
726 gtk_paned_unmap (GtkWidget *widget)
727 {
728   GtkPaned *paned = GTK_PANED (widget);
729     
730   gdk_window_hide (paned->handle);
731
732   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
733 }
734
735 static gboolean
736 gtk_paned_expose (GtkWidget      *widget,
737                   GdkEventExpose *event)
738 {
739   GtkPaned *paned = GTK_PANED (widget);
740
741   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
742       paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
743       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
744     {
745       GtkStateType state;
746       
747       if (gtk_widget_is_focus (widget))
748         state = GTK_STATE_SELECTED;
749       else if (paned->handle_prelit)
750         state = GTK_STATE_PRELIGHT;
751       else
752         state = GTK_WIDGET_STATE (widget);
753       
754       gtk_paint_handle (widget->style, widget->window,
755                         state, GTK_SHADOW_NONE,
756                         &paned->handle_pos, widget, "paned",
757                         paned->handle_pos.x, paned->handle_pos.y,
758                         paned->handle_pos.width, paned->handle_pos.height,
759                         paned->orientation);
760     }
761
762   /* Chain up to draw children */
763   GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
764   
765   return FALSE;
766 }
767
768 static gboolean
769 is_rtl (GtkPaned *paned)
770 {
771   if (paned->orientation == GTK_ORIENTATION_VERTICAL &&
772       gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL)
773     {
774       return TRUE;
775     }
776
777   return FALSE;
778 }
779
780 static void
781 update_drag (GtkPaned *paned)
782 {
783   gint pos;
784   gint handle_size;
785   gint size;
786   
787   if (paned->orientation == GTK_ORIENTATION_HORIZONTAL)
788     gtk_widget_get_pointer (GTK_WIDGET (paned), NULL, &pos);
789   else
790     gtk_widget_get_pointer (GTK_WIDGET (paned), &pos, NULL);
791
792   pos -= paned->drag_pos;
793
794   if (is_rtl (paned))
795     {
796       gtk_widget_style_get (GTK_WIDGET (paned),
797                             "handle_size", &handle_size,
798                             NULL);
799       
800       size = GTK_WIDGET (paned)->allocation.width - pos - handle_size;
801     }
802   else
803     {
804       size = pos;
805     }
806
807   size -= GTK_CONTAINER (paned)->border_width;
808   
809   size = CLAMP (size, paned->min_position, paned->max_position);
810
811   if (size != paned->child1_size)
812     gtk_paned_set_position (paned, size);
813 }
814
815 static gboolean
816 gtk_paned_enter (GtkWidget        *widget,
817                  GdkEventCrossing *event)
818 {
819   GtkPaned *paned = GTK_PANED (widget);
820   
821   if (paned->in_drag)
822     update_drag (paned);
823   else
824     {
825       paned->handle_prelit = TRUE;
826       gtk_widget_queue_draw_area (widget,
827                                   paned->handle_pos.x,
828                                   paned->handle_pos.y,
829                                   paned->handle_pos.width,
830                                   paned->handle_pos.height);
831     }
832   
833   return TRUE;
834 }
835
836 static gboolean
837 gtk_paned_leave (GtkWidget        *widget,
838                  GdkEventCrossing *event)
839 {
840   GtkPaned *paned = GTK_PANED (widget);
841   
842   if (paned->in_drag)
843     update_drag (paned);
844   else
845     {
846       paned->handle_prelit = FALSE;
847       gtk_widget_queue_draw_area (widget,
848                                   paned->handle_pos.x,
849                                   paned->handle_pos.y,
850                                   paned->handle_pos.width,
851                                   paned->handle_pos.height);
852     }
853
854   return TRUE;
855 }
856
857 static gboolean
858 gtk_paned_focus (GtkWidget        *widget,
859                  GtkDirectionType  direction)
860
861 {
862   gboolean retval;
863   
864   /* This is a hack, but how can this be done without
865    * excessive cut-and-paste from gtkcontainer.c?
866    */
867
868   GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
869   retval = (* GTK_WIDGET_CLASS (parent_class)->focus) (widget, direction);
870   GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
871
872   return retval;
873 }
874
875 static gboolean
876 gtk_paned_button_press (GtkWidget      *widget,
877                         GdkEventButton *event)
878 {
879   GtkPaned *paned = GTK_PANED (widget);
880
881   if (!paned->in_drag &&
882       (event->window == paned->handle) && (event->button == 1))
883     {
884       paned->in_drag = TRUE;
885
886       /* We need a server grab here, not gtk_grab_add(), since
887        * we don't want to pass events on to the widget's children */
888       gdk_pointer_grab (paned->handle, FALSE,
889                         GDK_POINTER_MOTION_HINT_MASK
890                         | GDK_BUTTON1_MOTION_MASK
891                         | GDK_BUTTON_RELEASE_MASK
892                         | GDK_ENTER_NOTIFY_MASK
893                         | GDK_LEAVE_NOTIFY_MASK,
894                         NULL, NULL,
895                         event->time);
896
897       if (paned->orientation == GTK_ORIENTATION_HORIZONTAL)
898         paned->drag_pos = event->y;
899       else
900         paned->drag_pos = event->x;
901       
902       return TRUE;
903     }
904
905   return FALSE;
906 }
907
908 static gboolean
909 gtk_paned_button_release (GtkWidget      *widget,
910                           GdkEventButton *event)
911 {
912   GtkPaned *paned = GTK_PANED (widget);
913
914   if (paned->in_drag && (event->button == 1))
915     {
916       paned->in_drag = FALSE;
917       paned->drag_pos = -1;
918       paned->position_set = TRUE;
919       gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
920                                   event->time);
921       return TRUE;
922     }
923
924   return FALSE;
925 }
926
927 static gboolean
928 gtk_paned_motion (GtkWidget      *widget,
929                   GdkEventMotion *event)
930 {
931   GtkPaned *paned = GTK_PANED (widget);
932   
933   if (paned->in_drag)
934     {
935       update_drag (paned);
936       return TRUE;
937     }
938   
939   return FALSE;
940 }
941
942 void
943 gtk_paned_add1 (GtkPaned  *paned,
944                 GtkWidget *widget)
945 {
946   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
947 }
948
949 void
950 gtk_paned_add2 (GtkPaned  *paned,
951                 GtkWidget *widget)
952 {
953   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
954 }
955
956 void
957 gtk_paned_pack1 (GtkPaned  *paned,
958                  GtkWidget *child,
959                  gboolean   resize,
960                  gboolean   shrink)
961 {
962   g_return_if_fail (GTK_IS_PANED (paned));
963   g_return_if_fail (GTK_IS_WIDGET (child));
964
965   if (!paned->child1)
966     {
967       paned->child1 = child;
968       paned->child1_resize = resize;
969       paned->child1_shrink = shrink;
970
971       gtk_widget_set_parent (child, GTK_WIDGET (paned));
972     }
973 }
974
975 void
976 gtk_paned_pack2 (GtkPaned  *paned,
977                  GtkWidget *child,
978                  gboolean   resize,
979                  gboolean   shrink)
980 {
981   g_return_if_fail (GTK_IS_PANED (paned));
982   g_return_if_fail (GTK_IS_WIDGET (child));
983
984   if (!paned->child2)
985     {
986       paned->child2 = child;
987       paned->child2_resize = resize;
988       paned->child2_shrink = shrink;
989
990       gtk_widget_set_parent (child, GTK_WIDGET (paned));
991     }
992 }
993
994
995 static void
996 gtk_paned_add (GtkContainer *container,
997                GtkWidget    *widget)
998 {
999   GtkPaned *paned;
1000
1001   g_return_if_fail (GTK_IS_PANED (container));
1002
1003   paned = GTK_PANED (container);
1004
1005   if (!paned->child1)
1006     gtk_paned_add1 (paned, widget);
1007   else if (!paned->child2)
1008     gtk_paned_add2 (paned, widget);
1009 }
1010
1011 static void
1012 gtk_paned_remove (GtkContainer *container,
1013                   GtkWidget    *widget)
1014 {
1015   GtkPaned *paned;
1016   gboolean was_visible;
1017
1018   paned = GTK_PANED (container);
1019   was_visible = GTK_WIDGET_VISIBLE (widget);
1020
1021   if (paned->child1 == widget)
1022     {
1023       gtk_widget_unparent (widget);
1024
1025       paned->child1 = NULL;
1026
1027       if (was_visible && GTK_WIDGET_VISIBLE (container))
1028         gtk_widget_queue_resize (GTK_WIDGET (container));
1029     }
1030   else if (paned->child2 == widget)
1031     {
1032       gtk_widget_unparent (widget);
1033
1034       paned->child2 = NULL;
1035
1036       if (was_visible && GTK_WIDGET_VISIBLE (container))
1037         gtk_widget_queue_resize (GTK_WIDGET (container));
1038     }
1039 }
1040
1041 static void
1042 gtk_paned_forall (GtkContainer *container,
1043                   gboolean      include_internals,
1044                   GtkCallback   callback,
1045                   gpointer      callback_data)
1046 {
1047   GtkPaned *paned;
1048
1049   g_return_if_fail (callback != NULL);
1050
1051   paned = GTK_PANED (container);
1052
1053   if (paned->child1)
1054     (*callback) (paned->child1, callback_data);
1055   if (paned->child2)
1056     (*callback) (paned->child2, callback_data);
1057 }
1058
1059 /**
1060  * gtk_paned_get_position:
1061  * @paned: a #GtkPaned widget
1062  * 
1063  * Obtains the position of the divider between the two panes.
1064  * 
1065  * Return value: position of the divider
1066  **/
1067 gint
1068 gtk_paned_get_position (GtkPaned  *paned)
1069 {
1070   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1071
1072   return paned->child1_size;
1073 }
1074
1075 /**
1076  * gtk_paned_set_position:
1077  * @paned: a #GtkPaned widget
1078  * @position: pixel position of divider, a negative value means that the position
1079  *            is unset.
1080  * 
1081  * Sets the position of the divider between the two panes.
1082  **/
1083 void
1084 gtk_paned_set_position (GtkPaned *paned,
1085                         gint      position)
1086 {
1087   GObject *object;
1088   
1089   g_return_if_fail (GTK_IS_PANED (paned));
1090
1091   object = G_OBJECT (paned);
1092   
1093   if (position >= 0)
1094     {
1095       /* We don't clamp here - the assumption is that
1096        * if the total allocation changes at the same time
1097        * as the position, the position set is with reference
1098        * to the new total size. If only the position changes,
1099        * then clamping will occur in gtk_paned_compute_position()
1100        */
1101
1102       paned->child1_size = position;
1103       paned->position_set = TRUE;
1104     }
1105   else
1106     {
1107       paned->position_set = FALSE;
1108     }
1109
1110   g_object_freeze_notify (object);
1111   g_object_notify (object, "position");
1112   g_object_notify (object, "position_set");
1113   g_object_thaw_notify (object);
1114
1115   gtk_widget_queue_resize (GTK_WIDGET (paned));
1116 }
1117
1118 /**
1119  * gtk_paned_get_child1:
1120  * @paned: a #GtkPaned widget
1121  * 
1122  * Obtains the first child of the paned widget.
1123  * 
1124  * Return value: first child, or %NULL if it is not set.
1125  *
1126  * Since: 2.4
1127  **/
1128 GtkWidget *
1129 gtk_paned_get_child1 (GtkPaned *paned)
1130 {
1131   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1132
1133   return paned->child1;
1134 }
1135
1136 /**
1137  * gtk_paned_get_child2:
1138  * @paned: a #GtkPaned widget
1139  * 
1140  * Obtains the second child of the paned widget.
1141  * 
1142  * Return value: second child, or %NULL if it is not set.
1143  *
1144  * Since: 2.4
1145  **/
1146 GtkWidget *
1147 gtk_paned_get_child2 (GtkPaned *paned)
1148 {
1149   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1150
1151   return paned->child2;
1152 }
1153
1154 void
1155 gtk_paned_compute_position (GtkPaned *paned,
1156                             gint      allocation,
1157                             gint      child1_req,
1158                             gint      child2_req)
1159 {
1160   gint old_position;
1161   gint old_min_position;
1162   gint old_max_position;
1163   
1164   g_return_if_fail (GTK_IS_PANED (paned));
1165
1166   old_position = paned->child1_size;
1167   old_min_position = paned->min_position;
1168   old_max_position = paned->max_position;
1169
1170   paned->min_position = paned->child1_shrink ? 0 : child1_req;
1171
1172   paned->max_position = allocation;
1173   if (!paned->child2_shrink)
1174     paned->max_position = MAX (1, paned->max_position - child2_req);
1175
1176   if (!paned->position_set)
1177     {
1178       if (paned->child1_resize && !paned->child2_resize)
1179         paned->child1_size = MAX (0, allocation - child2_req);
1180       else if (!paned->child1_resize && paned->child2_resize)
1181         paned->child1_size = child1_req;
1182       else if (child1_req + child2_req != 0)
1183         paned->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req));
1184       else
1185         paned->child1_size = allocation * 0.5;
1186     }
1187   else
1188     {
1189       /* If the position was set before the initial allocation.
1190        * (paned->last_allocation <= 0) just clamp it and leave it.
1191        */
1192       if (paned->last_allocation > 0)
1193         {
1194           if (paned->child1_resize && !paned->child2_resize)
1195             paned->child1_size += allocation - paned->last_allocation;
1196           else if (!(!paned->child1_resize && paned->child2_resize))
1197             paned->child1_size = allocation * ((gdouble) paned->child1_size / (paned->last_allocation));
1198         }
1199     }
1200
1201   paned->child1_size = CLAMP (paned->child1_size,
1202                               paned->min_position,
1203                               paned->max_position);
1204
1205   gtk_widget_set_child_visible (paned->child1, paned->child1_size != 0);
1206   gtk_widget_set_child_visible (paned->child2, paned->child1_size != allocation);
1207
1208   g_object_freeze_notify (G_OBJECT (paned));
1209   if (paned->child1_size != old_position)
1210     g_object_notify (G_OBJECT (paned), "position");
1211   if (paned->min_position != old_min_position)
1212     g_object_notify (G_OBJECT (paned), "min_position");
1213   if (paned->max_position != old_max_position)
1214     g_object_notify (G_OBJECT (paned), "max_position");
1215   g_object_thaw_notify (G_OBJECT (paned));
1216
1217   paned->last_allocation = allocation;
1218 }
1219
1220 static void
1221 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1222 {
1223   if (paned->priv->saved_focus)
1224     g_object_remove_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1225                                   (gpointer *)&(paned->priv->saved_focus));
1226
1227   paned->priv->saved_focus = widget;
1228
1229   if (paned->priv->saved_focus)
1230     g_object_add_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1231                                (gpointer *)&(paned->priv->saved_focus));
1232 }
1233
1234 static void
1235 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1236 {
1237   if (paned->priv->first_paned)
1238     g_object_remove_weak_pointer (G_OBJECT (paned->priv->first_paned),
1239                                   (gpointer *)&(paned->priv->first_paned));
1240
1241   paned->priv->first_paned = first_paned;
1242
1243   if (paned->priv->first_paned)
1244     g_object_add_weak_pointer (G_OBJECT (paned->priv->first_paned),
1245                                (gpointer *)&(paned->priv->first_paned));
1246 }
1247
1248 static void
1249 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1250 {
1251   if (paned->last_child1_focus)
1252     g_object_remove_weak_pointer (G_OBJECT (paned->last_child1_focus),
1253                                   (gpointer *)&(paned->last_child1_focus));
1254
1255   paned->last_child1_focus = widget;
1256
1257   if (paned->last_child1_focus)
1258     g_object_add_weak_pointer (G_OBJECT (paned->last_child1_focus),
1259                                (gpointer *)&(paned->last_child1_focus));
1260 }
1261
1262 static void
1263 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1264 {
1265   if (paned->last_child2_focus)
1266     g_object_remove_weak_pointer (G_OBJECT (paned->last_child2_focus),
1267                                   (gpointer *)&(paned->last_child2_focus));
1268
1269   paned->last_child2_focus = widget;
1270
1271   if (paned->last_child2_focus)
1272     g_object_add_weak_pointer (G_OBJECT (paned->last_child2_focus),
1273                                (gpointer *)&(paned->last_child2_focus));
1274 }
1275
1276 static GtkWidget *
1277 paned_get_focus_widget (GtkPaned *paned)
1278 {
1279   GtkWidget *toplevel;
1280
1281   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1282   if (GTK_WIDGET_TOPLEVEL (toplevel))
1283     return GTK_WINDOW (toplevel)->focus_widget;
1284
1285   return NULL;
1286 }
1287
1288 static void
1289 gtk_paned_set_focus_child (GtkContainer *container,
1290                            GtkWidget    *focus_child)
1291 {
1292   GtkPaned *paned;
1293   
1294   g_return_if_fail (GTK_IS_PANED (container));
1295
1296   paned = GTK_PANED (container);
1297  
1298   if (focus_child == NULL)
1299     {
1300       GtkWidget *last_focus;
1301       GtkWidget *w;
1302       
1303       last_focus = paned_get_focus_widget (paned);
1304
1305       if (last_focus)
1306         {
1307           /* If there is one or more paned widgets between us and the
1308            * focus widget, we want the topmost of those as last_focus
1309            */
1310           for (w = last_focus; w != GTK_WIDGET (paned); w = w->parent)
1311             if (GTK_IS_PANED (w))
1312               last_focus = w;
1313           
1314           if (container->focus_child == paned->child1)
1315             gtk_paned_set_last_child1_focus (paned, last_focus);
1316           else if (container->focus_child == paned->child2)
1317             gtk_paned_set_last_child2_focus (paned, last_focus);
1318         }
1319     }
1320
1321   if (parent_class->set_focus_child)
1322     (* parent_class->set_focus_child) (container, focus_child);
1323 }
1324
1325 static void
1326 gtk_paned_get_cycle_chain (GtkPaned          *paned,
1327                            GtkDirectionType   direction,
1328                            GList            **widgets)
1329 {
1330   GtkContainer *container = GTK_CONTAINER (paned);
1331   GtkWidget *ancestor = NULL;
1332   GList *temp_list = NULL;
1333   GList *list;
1334
1335   if (paned->in_recursion)
1336     return;
1337
1338   g_assert (widgets != NULL);
1339
1340   if (paned->last_child1_focus &&
1341       !gtk_widget_is_ancestor (paned->last_child1_focus, GTK_WIDGET (paned)))
1342     {
1343       gtk_paned_set_last_child1_focus (paned, NULL);
1344     }
1345
1346   if (paned->last_child2_focus &&
1347       !gtk_widget_is_ancestor (paned->last_child2_focus, GTK_WIDGET (paned)))
1348     {
1349       gtk_paned_set_last_child2_focus (paned, NULL);
1350     }
1351
1352   if (GTK_WIDGET (paned)->parent)
1353     ancestor = gtk_widget_get_ancestor (GTK_WIDGET (paned)->parent, GTK_TYPE_PANED);
1354
1355   /* The idea here is that temp_list is a list of widgets we want to cycle
1356    * to. The list is prioritized so that the first element is our first
1357    * choice, the next our second, and so on.
1358    *
1359    * We can't just use g_list_reverse(), because we want to try
1360    * paned->last_child?_focus before paned->child?, both when we
1361    * are going forward and backward.
1362    */
1363   if (direction == GTK_DIR_TAB_FORWARD)
1364     {
1365       if (container->focus_child == paned->child1)
1366         {
1367           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1368           temp_list = g_list_append (temp_list, paned->child2);
1369           temp_list = g_list_append (temp_list, ancestor);
1370         }
1371       else if (container->focus_child == paned->child2)
1372         {
1373           temp_list = g_list_append (temp_list, ancestor);
1374           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1375           temp_list = g_list_append (temp_list, paned->child1);
1376         }
1377       else
1378         {
1379           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1380           temp_list = g_list_append (temp_list, paned->child1);
1381           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1382           temp_list = g_list_append (temp_list, paned->child2);
1383           temp_list = g_list_append (temp_list, ancestor);
1384         }
1385     }
1386   else
1387     {
1388       if (container->focus_child == paned->child1)
1389         {
1390           temp_list = g_list_append (temp_list, ancestor);
1391           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1392           temp_list = g_list_append (temp_list, paned->child2);
1393         }
1394       else if (container->focus_child == paned->child2)
1395         {
1396           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1397           temp_list = g_list_append (temp_list, paned->child1);
1398           temp_list = g_list_append (temp_list, ancestor);
1399         }
1400       else
1401         {
1402           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1403           temp_list = g_list_append (temp_list, paned->child2);
1404           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1405           temp_list = g_list_append (temp_list, paned->child1);
1406           temp_list = g_list_append (temp_list, ancestor);
1407         }
1408     }
1409
1410   /* Walk the list and expand all the paned widgets. */
1411   for (list = temp_list; list != NULL; list = list->next)
1412     {
1413       GtkWidget *widget = list->data;
1414
1415       if (widget)
1416         {
1417           if (GTK_IS_PANED (widget))
1418             {
1419               paned->in_recursion = TRUE;
1420               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
1421               paned->in_recursion = FALSE;
1422             }
1423           else
1424             {
1425               *widgets = g_list_append (*widgets, widget);
1426             }
1427         }
1428     }
1429
1430   g_list_free (temp_list);
1431 }
1432
1433 static gboolean
1434 gtk_paned_cycle_child_focus (GtkPaned *paned,
1435                              gboolean  reversed)
1436 {
1437   GList *cycle_chain = NULL;
1438   GList *list;
1439   
1440   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
1441
1442   /* ignore f6 if the handle is focused */
1443   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1444     return TRUE;
1445   
1446   /* we can't just let the event propagate up the hierarchy,
1447    * because the paned will want to cycle focus _unless_ an
1448    * ancestor paned handles the event
1449    */
1450   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
1451
1452   for (list = cycle_chain; list != NULL; list = list->next)
1453     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
1454       break;
1455
1456   g_list_free (cycle_chain);
1457   
1458   return TRUE;
1459 }
1460
1461 static void
1462 get_child_panes (GtkWidget  *widget,
1463                  GList     **panes)
1464 {
1465   if (GTK_IS_PANED (widget))
1466     {
1467       GtkPaned *paned = GTK_PANED (widget);
1468       
1469       get_child_panes (paned->child1, panes);
1470       *panes = g_list_prepend (*panes, widget);
1471       get_child_panes (paned->child2, panes);
1472     }
1473   else if (GTK_IS_CONTAINER (widget))
1474     {
1475       gtk_container_foreach (GTK_CONTAINER (widget),
1476                              (GtkCallback)get_child_panes, panes);
1477     }
1478 }
1479
1480 static GList *
1481 get_all_panes (GtkPaned *paned)
1482 {
1483   GtkPaned *topmost = NULL;
1484   GList *result = NULL;
1485   GtkWidget *w;
1486   
1487   for (w = GTK_WIDGET (paned); w != NULL; w = w->parent)
1488     {
1489       if (GTK_IS_PANED (w))
1490         topmost = GTK_PANED (w);
1491     }
1492
1493   g_assert (topmost);
1494
1495   get_child_panes (GTK_WIDGET (topmost), &result);
1496
1497   return g_list_reverse (result);
1498 }
1499
1500 static void
1501 gtk_paned_find_neighbours (GtkPaned  *paned,
1502                            GtkPaned **next,
1503                            GtkPaned **prev)
1504 {
1505   GList *all_panes;
1506   GList *this_link;
1507
1508   all_panes = get_all_panes (paned);
1509   g_assert (all_panes);
1510
1511   this_link = g_list_find (all_panes, paned);
1512
1513   g_assert (this_link);
1514   
1515   if (this_link->next)
1516     *next = this_link->next->data;
1517   else
1518     *next = all_panes->data;
1519
1520   if (this_link->prev)
1521     *prev = this_link->prev->data;
1522   else
1523     *prev = g_list_last (all_panes)->data;
1524
1525   g_list_free (all_panes);
1526 }
1527
1528 static gboolean
1529 gtk_paned_move_handle (GtkPaned      *paned,
1530                        GtkScrollType  scroll)
1531 {
1532   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1533     {
1534       gint old_position;
1535       gint new_position;
1536       gint increment;
1537       
1538       enum {
1539         SINGLE_STEP_SIZE = 1,
1540         PAGE_STEP_SIZE   = 75
1541       };
1542       
1543       new_position = old_position = gtk_paned_get_position (paned);
1544       increment = 0;
1545       
1546       switch (scroll)
1547         {
1548         case GTK_SCROLL_STEP_LEFT:
1549         case GTK_SCROLL_STEP_UP:
1550         case GTK_SCROLL_STEP_BACKWARD:
1551           increment = - SINGLE_STEP_SIZE;
1552           break;
1553           
1554         case GTK_SCROLL_STEP_RIGHT:
1555         case GTK_SCROLL_STEP_DOWN:
1556         case GTK_SCROLL_STEP_FORWARD:
1557           increment = SINGLE_STEP_SIZE;
1558           break;
1559           
1560         case GTK_SCROLL_PAGE_LEFT:
1561         case GTK_SCROLL_PAGE_UP:
1562         case GTK_SCROLL_PAGE_BACKWARD:
1563           increment = - PAGE_STEP_SIZE;
1564           break;
1565           
1566         case GTK_SCROLL_PAGE_RIGHT:
1567         case GTK_SCROLL_PAGE_DOWN:
1568         case GTK_SCROLL_PAGE_FORWARD:
1569           increment = PAGE_STEP_SIZE;
1570           break;
1571           
1572         case GTK_SCROLL_START:
1573           new_position = paned->min_position;
1574           break;
1575           
1576         case GTK_SCROLL_END:
1577           new_position = paned->max_position;
1578           break;
1579
1580         default:
1581           break;
1582         }
1583
1584       if (increment)
1585         {
1586           if (is_rtl (paned))
1587             increment = -increment;
1588           
1589           new_position = old_position + increment;
1590         }
1591       
1592       new_position = CLAMP (new_position, paned->min_position, paned->max_position);
1593       
1594       if (old_position != new_position)
1595         gtk_paned_set_position (paned, new_position);
1596
1597       return TRUE;
1598     }
1599
1600   return FALSE;
1601 }
1602
1603 static void
1604 gtk_paned_restore_focus (GtkPaned *paned)
1605 {
1606   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1607     {
1608       if (paned->priv->saved_focus &&
1609           GTK_WIDGET_SENSITIVE (paned->priv->saved_focus))
1610         {
1611           gtk_widget_grab_focus (paned->priv->saved_focus);
1612         }
1613       else
1614         {
1615           /* the saved focus is somehow not available for focusing,
1616            * try
1617            *   1) tabbing into the paned widget
1618            * if that didn't work,
1619            *   2) unset focus for the window if there is one
1620            */
1621           
1622           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
1623             {
1624               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1625               
1626               if (GTK_IS_WINDOW (toplevel))
1627                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
1628             }
1629         }
1630       
1631       gtk_paned_set_saved_focus (paned, NULL);
1632       gtk_paned_set_first_paned (paned, NULL);
1633     }
1634 }
1635
1636 static gboolean
1637 gtk_paned_accept_position (GtkPaned *paned)
1638 {
1639   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1640     {
1641       paned->original_position = -1;
1642       gtk_paned_restore_focus (paned);
1643
1644       return TRUE;
1645     }
1646
1647   return FALSE;
1648 }
1649
1650
1651 static gboolean
1652 gtk_paned_cancel_position (GtkPaned *paned)
1653 {
1654   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1655     {
1656       if (paned->original_position != -1)
1657         {
1658           gtk_paned_set_position (paned, paned->original_position);
1659           paned->original_position = -1;
1660         }
1661
1662       gtk_paned_restore_focus (paned);
1663       return TRUE;
1664     }
1665
1666   return FALSE;
1667 }
1668
1669 static gboolean
1670 gtk_paned_cycle_handle_focus (GtkPaned *paned,
1671                               gboolean  reversed)
1672 {
1673   GtkPaned *next, *prev;
1674   
1675   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1676     {
1677       GtkPaned *focus = NULL;
1678
1679       if (!paned->priv->first_paned)
1680         {
1681           /* The first_pane has disappeared. As an ad-hoc solution,
1682            * we make the currently focused paned the first_paned. To the
1683            * user this will seem like the paned cycling has been reset.
1684            */
1685           
1686           gtk_paned_set_first_paned (paned, paned);
1687         }
1688       
1689       gtk_paned_find_neighbours (paned, &next, &prev);
1690
1691       if (reversed && prev &&
1692           prev != paned && paned != paned->priv->first_paned)
1693         {
1694           focus = prev;
1695         }
1696       else if (!reversed && next &&
1697                next != paned && next != paned->priv->first_paned)
1698         {
1699           focus = next;
1700         }
1701       else
1702         {
1703           gtk_paned_accept_position (paned);
1704           return TRUE;
1705         }
1706
1707       g_assert (focus);
1708       
1709       gtk_paned_set_saved_focus (focus, paned->priv->saved_focus);
1710       gtk_paned_set_first_paned (focus, paned->priv->first_paned);
1711       
1712       gtk_paned_set_saved_focus (paned, NULL);
1713       gtk_paned_set_first_paned (paned, NULL);
1714       
1715       gtk_widget_grab_focus (GTK_WIDGET (focus));
1716       
1717       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
1718         {
1719           paned->original_position = -1;
1720           focus->original_position = gtk_paned_get_position (focus);
1721         }
1722     }
1723   else
1724     {
1725       GtkContainer *container = GTK_CONTAINER (paned);
1726       GtkPaned *focus;
1727       GtkPaned *first;
1728       GtkPaned *prev, *next;
1729       GtkWidget *toplevel;
1730
1731       gtk_paned_find_neighbours (paned, &next, &prev);
1732
1733       if (container->focus_child == paned->child1)
1734         {
1735           if (reversed)
1736             {
1737               focus = prev;
1738               first = paned;
1739             }
1740           else
1741             {
1742               focus = paned;
1743               first = paned;
1744             }
1745         }
1746       else if (container->focus_child == paned->child2)
1747         {
1748           if (reversed)
1749             {
1750               focus = paned;
1751               first = next;
1752             }
1753           else
1754             {
1755               focus = next;
1756               first = next;
1757             }
1758         }
1759       else
1760         {
1761           /* Focus is not inside this paned, and we don't have focus.
1762            * Presumably this happened because the application wants us
1763            * to start keyboard navigating.
1764            */
1765           focus = paned;
1766
1767           if (reversed)
1768             first = paned;
1769           else
1770             first = next;
1771         }
1772
1773       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1774
1775       if (GTK_IS_WINDOW (toplevel))
1776         gtk_paned_set_saved_focus (focus, GTK_WINDOW (toplevel)->focus_widget);
1777       gtk_paned_set_first_paned (focus, first);
1778       focus->original_position = gtk_paned_get_position (focus); 
1779
1780       gtk_widget_grab_focus (GTK_WIDGET (focus));
1781    }
1782   
1783   return TRUE;
1784 }
1785
1786 static gboolean
1787 gtk_paned_toggle_handle_focus (GtkPaned *paned)
1788 {
1789   /* This function/signal has the wrong name. It is called when you
1790    * press Tab or Shift-Tab and what we do is act as if
1791    * the user pressed Return and then Tab or Shift-Tab
1792    */
1793   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1794     gtk_paned_accept_position (paned);
1795
1796   return FALSE;
1797 }