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