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