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