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