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