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