]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
Reinstate abstract types
[~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   gtk_widget_set_child_visible (paned->child1, paned->child1_size != 0);
1217   gtk_widget_set_child_visible (paned->child2, paned->child1_size != allocation);
1218
1219   g_object_freeze_notify (G_OBJECT (paned));
1220   if (paned->child1_size != old_position)
1221     g_object_notify (G_OBJECT (paned), "position");
1222   if (paned->min_position != old_min_position)
1223     g_object_notify (G_OBJECT (paned), "min-position");
1224   if (paned->max_position != old_max_position)
1225     g_object_notify (G_OBJECT (paned), "max-position");
1226   g_object_thaw_notify (G_OBJECT (paned));
1227
1228   paned->last_allocation = allocation;
1229 }
1230
1231 static void
1232 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1233 {
1234   if (paned->priv->saved_focus)
1235     g_object_remove_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1236                                   (gpointer *)&(paned->priv->saved_focus));
1237
1238   paned->priv->saved_focus = widget;
1239
1240   if (paned->priv->saved_focus)
1241     g_object_add_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1242                                (gpointer *)&(paned->priv->saved_focus));
1243 }
1244
1245 static void
1246 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1247 {
1248   if (paned->priv->first_paned)
1249     g_object_remove_weak_pointer (G_OBJECT (paned->priv->first_paned),
1250                                   (gpointer *)&(paned->priv->first_paned));
1251
1252   paned->priv->first_paned = first_paned;
1253
1254   if (paned->priv->first_paned)
1255     g_object_add_weak_pointer (G_OBJECT (paned->priv->first_paned),
1256                                (gpointer *)&(paned->priv->first_paned));
1257 }
1258
1259 static void
1260 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1261 {
1262   if (paned->last_child1_focus)
1263     g_object_remove_weak_pointer (G_OBJECT (paned->last_child1_focus),
1264                                   (gpointer *)&(paned->last_child1_focus));
1265
1266   paned->last_child1_focus = widget;
1267
1268   if (paned->last_child1_focus)
1269     g_object_add_weak_pointer (G_OBJECT (paned->last_child1_focus),
1270                                (gpointer *)&(paned->last_child1_focus));
1271 }
1272
1273 static void
1274 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1275 {
1276   if (paned->last_child2_focus)
1277     g_object_remove_weak_pointer (G_OBJECT (paned->last_child2_focus),
1278                                   (gpointer *)&(paned->last_child2_focus));
1279
1280   paned->last_child2_focus = widget;
1281
1282   if (paned->last_child2_focus)
1283     g_object_add_weak_pointer (G_OBJECT (paned->last_child2_focus),
1284                                (gpointer *)&(paned->last_child2_focus));
1285 }
1286
1287 static GtkWidget *
1288 paned_get_focus_widget (GtkPaned *paned)
1289 {
1290   GtkWidget *toplevel;
1291
1292   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1293   if (GTK_WIDGET_TOPLEVEL (toplevel))
1294     return GTK_WINDOW (toplevel)->focus_widget;
1295
1296   return NULL;
1297 }
1298
1299 static void
1300 gtk_paned_set_focus_child (GtkContainer *container,
1301                            GtkWidget    *focus_child)
1302 {
1303   GtkPaned *paned;
1304   
1305   g_return_if_fail (GTK_IS_PANED (container));
1306
1307   paned = GTK_PANED (container);
1308  
1309   if (focus_child == NULL)
1310     {
1311       GtkWidget *last_focus;
1312       GtkWidget *w;
1313       
1314       last_focus = paned_get_focus_widget (paned);
1315
1316       if (last_focus)
1317         {
1318           /* If there is one or more paned widgets between us and the
1319            * focus widget, we want the topmost of those as last_focus
1320            */
1321           for (w = last_focus; w != GTK_WIDGET (paned); w = w->parent)
1322             if (GTK_IS_PANED (w))
1323               last_focus = w;
1324           
1325           if (container->focus_child == paned->child1)
1326             gtk_paned_set_last_child1_focus (paned, last_focus);
1327           else if (container->focus_child == paned->child2)
1328             gtk_paned_set_last_child2_focus (paned, last_focus);
1329         }
1330     }
1331
1332   if (GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child)
1333     (* GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child) (container, focus_child);
1334 }
1335
1336 static void
1337 gtk_paned_get_cycle_chain (GtkPaned          *paned,
1338                            GtkDirectionType   direction,
1339                            GList            **widgets)
1340 {
1341   GtkContainer *container = GTK_CONTAINER (paned);
1342   GtkWidget *ancestor = NULL;
1343   GList *temp_list = NULL;
1344   GList *list;
1345
1346   if (paned->in_recursion)
1347     return;
1348
1349   g_assert (widgets != NULL);
1350
1351   if (paned->last_child1_focus &&
1352       !gtk_widget_is_ancestor (paned->last_child1_focus, GTK_WIDGET (paned)))
1353     {
1354       gtk_paned_set_last_child1_focus (paned, NULL);
1355     }
1356
1357   if (paned->last_child2_focus &&
1358       !gtk_widget_is_ancestor (paned->last_child2_focus, GTK_WIDGET (paned)))
1359     {
1360       gtk_paned_set_last_child2_focus (paned, NULL);
1361     }
1362
1363   if (GTK_WIDGET (paned)->parent)
1364     ancestor = gtk_widget_get_ancestor (GTK_WIDGET (paned)->parent, GTK_TYPE_PANED);
1365
1366   /* The idea here is that temp_list is a list of widgets we want to cycle
1367    * to. The list is prioritized so that the first element is our first
1368    * choice, the next our second, and so on.
1369    *
1370    * We can't just use g_list_reverse(), because we want to try
1371    * paned->last_child?_focus before paned->child?, both when we
1372    * are going forward and backward.
1373    */
1374   if (direction == GTK_DIR_TAB_FORWARD)
1375     {
1376       if (container->focus_child == paned->child1)
1377         {
1378           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1379           temp_list = g_list_append (temp_list, paned->child2);
1380           temp_list = g_list_append (temp_list, ancestor);
1381         }
1382       else if (container->focus_child == paned->child2)
1383         {
1384           temp_list = g_list_append (temp_list, ancestor);
1385           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1386           temp_list = g_list_append (temp_list, paned->child1);
1387         }
1388       else
1389         {
1390           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1391           temp_list = g_list_append (temp_list, paned->child1);
1392           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1393           temp_list = g_list_append (temp_list, paned->child2);
1394           temp_list = g_list_append (temp_list, ancestor);
1395         }
1396     }
1397   else
1398     {
1399       if (container->focus_child == paned->child1)
1400         {
1401           temp_list = g_list_append (temp_list, ancestor);
1402           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1403           temp_list = g_list_append (temp_list, paned->child2);
1404         }
1405       else if (container->focus_child == paned->child2)
1406         {
1407           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1408           temp_list = g_list_append (temp_list, paned->child1);
1409           temp_list = g_list_append (temp_list, ancestor);
1410         }
1411       else
1412         {
1413           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1414           temp_list = g_list_append (temp_list, paned->child2);
1415           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1416           temp_list = g_list_append (temp_list, paned->child1);
1417           temp_list = g_list_append (temp_list, ancestor);
1418         }
1419     }
1420
1421   /* Walk the list and expand all the paned widgets. */
1422   for (list = temp_list; list != NULL; list = list->next)
1423     {
1424       GtkWidget *widget = list->data;
1425
1426       if (widget)
1427         {
1428           if (GTK_IS_PANED (widget))
1429             {
1430               paned->in_recursion = TRUE;
1431               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
1432               paned->in_recursion = FALSE;
1433             }
1434           else
1435             {
1436               *widgets = g_list_append (*widgets, widget);
1437             }
1438         }
1439     }
1440
1441   g_list_free (temp_list);
1442 }
1443
1444 static gboolean
1445 gtk_paned_cycle_child_focus (GtkPaned *paned,
1446                              gboolean  reversed)
1447 {
1448   GList *cycle_chain = NULL;
1449   GList *list;
1450   
1451   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
1452
1453   /* ignore f6 if the handle is focused */
1454   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1455     return TRUE;
1456   
1457   /* we can't just let the event propagate up the hierarchy,
1458    * because the paned will want to cycle focus _unless_ an
1459    * ancestor paned handles the event
1460    */
1461   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
1462
1463   for (list = cycle_chain; list != NULL; list = list->next)
1464     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
1465       break;
1466
1467   g_list_free (cycle_chain);
1468   
1469   return TRUE;
1470 }
1471
1472 static void
1473 get_child_panes (GtkWidget  *widget,
1474                  GList     **panes)
1475 {
1476   if (GTK_IS_PANED (widget))
1477     {
1478       GtkPaned *paned = GTK_PANED (widget);
1479       
1480       get_child_panes (paned->child1, panes);
1481       *panes = g_list_prepend (*panes, widget);
1482       get_child_panes (paned->child2, panes);
1483     }
1484   else if (GTK_IS_CONTAINER (widget))
1485     {
1486       gtk_container_foreach (GTK_CONTAINER (widget),
1487                              (GtkCallback)get_child_panes, panes);
1488     }
1489 }
1490
1491 static GList *
1492 get_all_panes (GtkPaned *paned)
1493 {
1494   GtkPaned *topmost = NULL;
1495   GList *result = NULL;
1496   GtkWidget *w;
1497   
1498   for (w = GTK_WIDGET (paned); w != NULL; w = w->parent)
1499     {
1500       if (GTK_IS_PANED (w))
1501         topmost = GTK_PANED (w);
1502     }
1503
1504   g_assert (topmost);
1505
1506   get_child_panes (GTK_WIDGET (topmost), &result);
1507
1508   return g_list_reverse (result);
1509 }
1510
1511 static void
1512 gtk_paned_find_neighbours (GtkPaned  *paned,
1513                            GtkPaned **next,
1514                            GtkPaned **prev)
1515 {
1516   GList *all_panes;
1517   GList *this_link;
1518
1519   all_panes = get_all_panes (paned);
1520   g_assert (all_panes);
1521
1522   this_link = g_list_find (all_panes, paned);
1523
1524   g_assert (this_link);
1525   
1526   if (this_link->next)
1527     *next = this_link->next->data;
1528   else
1529     *next = all_panes->data;
1530
1531   if (this_link->prev)
1532     *prev = this_link->prev->data;
1533   else
1534     *prev = g_list_last (all_panes)->data;
1535
1536   g_list_free (all_panes);
1537 }
1538
1539 static gboolean
1540 gtk_paned_move_handle (GtkPaned      *paned,
1541                        GtkScrollType  scroll)
1542 {
1543   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1544     {
1545       gint old_position;
1546       gint new_position;
1547       gint increment;
1548       
1549       enum {
1550         SINGLE_STEP_SIZE = 1,
1551         PAGE_STEP_SIZE   = 75
1552       };
1553       
1554       new_position = old_position = gtk_paned_get_position (paned);
1555       increment = 0;
1556       
1557       switch (scroll)
1558         {
1559         case GTK_SCROLL_STEP_LEFT:
1560         case GTK_SCROLL_STEP_UP:
1561         case GTK_SCROLL_STEP_BACKWARD:
1562           increment = - SINGLE_STEP_SIZE;
1563           break;
1564           
1565         case GTK_SCROLL_STEP_RIGHT:
1566         case GTK_SCROLL_STEP_DOWN:
1567         case GTK_SCROLL_STEP_FORWARD:
1568           increment = SINGLE_STEP_SIZE;
1569           break;
1570           
1571         case GTK_SCROLL_PAGE_LEFT:
1572         case GTK_SCROLL_PAGE_UP:
1573         case GTK_SCROLL_PAGE_BACKWARD:
1574           increment = - PAGE_STEP_SIZE;
1575           break;
1576           
1577         case GTK_SCROLL_PAGE_RIGHT:
1578         case GTK_SCROLL_PAGE_DOWN:
1579         case GTK_SCROLL_PAGE_FORWARD:
1580           increment = PAGE_STEP_SIZE;
1581           break;
1582           
1583         case GTK_SCROLL_START:
1584           new_position = paned->min_position;
1585           break;
1586           
1587         case GTK_SCROLL_END:
1588           new_position = paned->max_position;
1589           break;
1590
1591         default:
1592           break;
1593         }
1594
1595       if (increment)
1596         {
1597           if (is_rtl (paned))
1598             increment = -increment;
1599           
1600           new_position = old_position + increment;
1601         }
1602       
1603       new_position = CLAMP (new_position, paned->min_position, paned->max_position);
1604       
1605       if (old_position != new_position)
1606         gtk_paned_set_position (paned, new_position);
1607
1608       return TRUE;
1609     }
1610
1611   return FALSE;
1612 }
1613
1614 static void
1615 gtk_paned_restore_focus (GtkPaned *paned)
1616 {
1617   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1618     {
1619       if (paned->priv->saved_focus &&
1620           GTK_WIDGET_SENSITIVE (paned->priv->saved_focus))
1621         {
1622           gtk_widget_grab_focus (paned->priv->saved_focus);
1623         }
1624       else
1625         {
1626           /* the saved focus is somehow not available for focusing,
1627            * try
1628            *   1) tabbing into the paned widget
1629            * if that didn't work,
1630            *   2) unset focus for the window if there is one
1631            */
1632           
1633           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
1634             {
1635               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1636               
1637               if (GTK_IS_WINDOW (toplevel))
1638                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
1639             }
1640         }
1641       
1642       gtk_paned_set_saved_focus (paned, NULL);
1643       gtk_paned_set_first_paned (paned, NULL);
1644     }
1645 }
1646
1647 static gboolean
1648 gtk_paned_accept_position (GtkPaned *paned)
1649 {
1650   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1651     {
1652       paned->original_position = -1;
1653       gtk_paned_restore_focus (paned);
1654
1655       return TRUE;
1656     }
1657
1658   return FALSE;
1659 }
1660
1661
1662 static gboolean
1663 gtk_paned_cancel_position (GtkPaned *paned)
1664 {
1665   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1666     {
1667       if (paned->original_position != -1)
1668         {
1669           gtk_paned_set_position (paned, paned->original_position);
1670           paned->original_position = -1;
1671         }
1672
1673       gtk_paned_restore_focus (paned);
1674       return TRUE;
1675     }
1676
1677   return FALSE;
1678 }
1679
1680 static gboolean
1681 gtk_paned_cycle_handle_focus (GtkPaned *paned,
1682                               gboolean  reversed)
1683 {
1684   GtkPaned *next, *prev;
1685   
1686   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1687     {
1688       GtkPaned *focus = NULL;
1689
1690       if (!paned->priv->first_paned)
1691         {
1692           /* The first_pane has disappeared. As an ad-hoc solution,
1693            * we make the currently focused paned the first_paned. To the
1694            * user this will seem like the paned cycling has been reset.
1695            */
1696           
1697           gtk_paned_set_first_paned (paned, paned);
1698         }
1699       
1700       gtk_paned_find_neighbours (paned, &next, &prev);
1701
1702       if (reversed && prev &&
1703           prev != paned && paned != paned->priv->first_paned)
1704         {
1705           focus = prev;
1706         }
1707       else if (!reversed && next &&
1708                next != paned && next != paned->priv->first_paned)
1709         {
1710           focus = next;
1711         }
1712       else
1713         {
1714           gtk_paned_accept_position (paned);
1715           return TRUE;
1716         }
1717
1718       g_assert (focus);
1719       
1720       gtk_paned_set_saved_focus (focus, paned->priv->saved_focus);
1721       gtk_paned_set_first_paned (focus, paned->priv->first_paned);
1722       
1723       gtk_paned_set_saved_focus (paned, NULL);
1724       gtk_paned_set_first_paned (paned, NULL);
1725       
1726       gtk_widget_grab_focus (GTK_WIDGET (focus));
1727       
1728       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
1729         {
1730           paned->original_position = -1;
1731           focus->original_position = gtk_paned_get_position (focus);
1732         }
1733     }
1734   else
1735     {
1736       GtkContainer *container = GTK_CONTAINER (paned);
1737       GtkPaned *focus;
1738       GtkPaned *first;
1739       GtkPaned *prev, *next;
1740       GtkWidget *toplevel;
1741
1742       gtk_paned_find_neighbours (paned, &next, &prev);
1743
1744       if (container->focus_child == paned->child1)
1745         {
1746           if (reversed)
1747             {
1748               focus = prev;
1749               first = paned;
1750             }
1751           else
1752             {
1753               focus = paned;
1754               first = paned;
1755             }
1756         }
1757       else if (container->focus_child == paned->child2)
1758         {
1759           if (reversed)
1760             {
1761               focus = paned;
1762               first = next;
1763             }
1764           else
1765             {
1766               focus = next;
1767               first = next;
1768             }
1769         }
1770       else
1771         {
1772           /* Focus is not inside this paned, and we don't have focus.
1773            * Presumably this happened because the application wants us
1774            * to start keyboard navigating.
1775            */
1776           focus = paned;
1777
1778           if (reversed)
1779             first = paned;
1780           else
1781             first = next;
1782         }
1783
1784       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1785
1786       if (GTK_IS_WINDOW (toplevel))
1787         gtk_paned_set_saved_focus (focus, GTK_WINDOW (toplevel)->focus_widget);
1788       gtk_paned_set_first_paned (focus, first);
1789       focus->original_position = gtk_paned_get_position (focus); 
1790
1791       gtk_widget_grab_focus (GTK_WIDGET (focus));
1792    }
1793   
1794   return TRUE;
1795 }
1796
1797 static gboolean
1798 gtk_paned_toggle_handle_focus (GtkPaned *paned)
1799 {
1800   /* This function/signal has the wrong name. It is called when you
1801    * press Tab or Shift-Tab and what we do is act as if
1802    * the user pressed Return and then Tab or Shift-Tab
1803    */
1804   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1805     gtk_paned_accept_position (paned);
1806
1807   return FALSE;
1808 }
1809
1810 #define __GTK_PANED_C__
1811 #include "gtkaliasdef.c"