]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
Add a GrabBroken event to GDK, and a grab-broken-event signal to
[~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, "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 ("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 ("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 ("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 ("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 ("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 ("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
926 static gboolean
927 gtk_paned_button_release (GtkWidget      *widget,
928                           GdkEventButton *event)
929 {
930   GtkPaned *paned = GTK_PANED (widget);
931
932   if (paned->in_drag && (event->button == 1))
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 (widget),
938                                   paned->priv->grab_time);
939       return TRUE;
940     }
941
942   return FALSE;
943 }
944
945 static gboolean
946 gtk_paned_motion (GtkWidget      *widget,
947                   GdkEventMotion *event)
948 {
949   GtkPaned *paned = GTK_PANED (widget);
950   
951   if (paned->in_drag)
952     {
953       update_drag (paned);
954       return TRUE;
955     }
956   
957   return FALSE;
958 }
959
960 void
961 gtk_paned_add1 (GtkPaned  *paned,
962                 GtkWidget *widget)
963 {
964   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
965 }
966
967 void
968 gtk_paned_add2 (GtkPaned  *paned,
969                 GtkWidget *widget)
970 {
971   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
972 }
973
974 void
975 gtk_paned_pack1 (GtkPaned  *paned,
976                  GtkWidget *child,
977                  gboolean   resize,
978                  gboolean   shrink)
979 {
980   g_return_if_fail (GTK_IS_PANED (paned));
981   g_return_if_fail (GTK_IS_WIDGET (child));
982
983   if (!paned->child1)
984     {
985       paned->child1 = child;
986       paned->child1_resize = resize;
987       paned->child1_shrink = shrink;
988
989       gtk_widget_set_parent (child, GTK_WIDGET (paned));
990     }
991 }
992
993 void
994 gtk_paned_pack2 (GtkPaned  *paned,
995                  GtkWidget *child,
996                  gboolean   resize,
997                  gboolean   shrink)
998 {
999   g_return_if_fail (GTK_IS_PANED (paned));
1000   g_return_if_fail (GTK_IS_WIDGET (child));
1001
1002   if (!paned->child2)
1003     {
1004       paned->child2 = child;
1005       paned->child2_resize = resize;
1006       paned->child2_shrink = shrink;
1007
1008       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1009     }
1010 }
1011
1012
1013 static void
1014 gtk_paned_add (GtkContainer *container,
1015                GtkWidget    *widget)
1016 {
1017   GtkPaned *paned;
1018
1019   g_return_if_fail (GTK_IS_PANED (container));
1020
1021   paned = GTK_PANED (container);
1022
1023   if (!paned->child1)
1024     gtk_paned_add1 (paned, widget);
1025   else if (!paned->child2)
1026     gtk_paned_add2 (paned, widget);
1027   else
1028     g_warning ("GtkPaned cannot have more than 2 children\n");
1029 }
1030
1031 static void
1032 gtk_paned_remove (GtkContainer *container,
1033                   GtkWidget    *widget)
1034 {
1035   GtkPaned *paned;
1036   gboolean was_visible;
1037
1038   paned = GTK_PANED (container);
1039   was_visible = GTK_WIDGET_VISIBLE (widget);
1040
1041   if (paned->child1 == widget)
1042     {
1043       gtk_widget_unparent (widget);
1044
1045       paned->child1 = NULL;
1046
1047       if (was_visible && GTK_WIDGET_VISIBLE (container))
1048         gtk_widget_queue_resize (GTK_WIDGET (container));
1049     }
1050   else if (paned->child2 == widget)
1051     {
1052       gtk_widget_unparent (widget);
1053
1054       paned->child2 = NULL;
1055
1056       if (was_visible && GTK_WIDGET_VISIBLE (container))
1057         gtk_widget_queue_resize (GTK_WIDGET (container));
1058     }
1059 }
1060
1061 static void
1062 gtk_paned_forall (GtkContainer *container,
1063                   gboolean      include_internals,
1064                   GtkCallback   callback,
1065                   gpointer      callback_data)
1066 {
1067   GtkPaned *paned;
1068
1069   g_return_if_fail (callback != NULL);
1070
1071   paned = GTK_PANED (container);
1072
1073   if (paned->child1)
1074     (*callback) (paned->child1, callback_data);
1075   if (paned->child2)
1076     (*callback) (paned->child2, callback_data);
1077 }
1078
1079 /**
1080  * gtk_paned_get_position:
1081  * @paned: a #GtkPaned widget
1082  * 
1083  * Obtains the position of the divider between the two panes.
1084  * 
1085  * Return value: position of the divider
1086  **/
1087 gint
1088 gtk_paned_get_position (GtkPaned  *paned)
1089 {
1090   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1091
1092   return paned->child1_size;
1093 }
1094
1095 /**
1096  * gtk_paned_set_position:
1097  * @paned: a #GtkPaned widget
1098  * @position: pixel position of divider, a negative value means that the position
1099  *            is unset.
1100  * 
1101  * Sets the position of the divider between the two panes.
1102  **/
1103 void
1104 gtk_paned_set_position (GtkPaned *paned,
1105                         gint      position)
1106 {
1107   GObject *object;
1108   
1109   g_return_if_fail (GTK_IS_PANED (paned));
1110
1111   object = G_OBJECT (paned);
1112   
1113   if (position >= 0)
1114     {
1115       /* We don't clamp here - the assumption is that
1116        * if the total allocation changes at the same time
1117        * as the position, the position set is with reference
1118        * to the new total size. If only the position changes,
1119        * then clamping will occur in gtk_paned_compute_position()
1120        */
1121
1122       paned->child1_size = position;
1123       paned->position_set = TRUE;
1124     }
1125   else
1126     {
1127       paned->position_set = FALSE;
1128     }
1129
1130   g_object_freeze_notify (object);
1131   g_object_notify (object, "position");
1132   g_object_notify (object, "position-set");
1133   g_object_thaw_notify (object);
1134
1135   gtk_widget_queue_resize (GTK_WIDGET (paned));
1136 }
1137
1138 /**
1139  * gtk_paned_get_child1:
1140  * @paned: a #GtkPaned widget
1141  * 
1142  * Obtains the first child of the paned widget.
1143  * 
1144  * Return value: first child, or %NULL if it is not set.
1145  *
1146  * Since: 2.4
1147  **/
1148 GtkWidget *
1149 gtk_paned_get_child1 (GtkPaned *paned)
1150 {
1151   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1152
1153   return paned->child1;
1154 }
1155
1156 /**
1157  * gtk_paned_get_child2:
1158  * @paned: a #GtkPaned widget
1159  * 
1160  * Obtains the second child of the paned widget.
1161  * 
1162  * Return value: second child, or %NULL if it is not set.
1163  *
1164  * Since: 2.4
1165  **/
1166 GtkWidget *
1167 gtk_paned_get_child2 (GtkPaned *paned)
1168 {
1169   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1170
1171   return paned->child2;
1172 }
1173
1174 void
1175 gtk_paned_compute_position (GtkPaned *paned,
1176                             gint      allocation,
1177                             gint      child1_req,
1178                             gint      child2_req)
1179 {
1180   gint old_position;
1181   gint old_min_position;
1182   gint old_max_position;
1183   
1184   g_return_if_fail (GTK_IS_PANED (paned));
1185
1186   old_position = paned->child1_size;
1187   old_min_position = paned->min_position;
1188   old_max_position = paned->max_position;
1189
1190   paned->min_position = paned->child1_shrink ? 0 : child1_req;
1191
1192   paned->max_position = allocation;
1193   if (!paned->child2_shrink)
1194     paned->max_position = MAX (1, paned->max_position - child2_req);
1195   paned->max_position = MAX (paned->min_position, paned->max_position);
1196
1197   if (!paned->position_set)
1198     {
1199       if (paned->child1_resize && !paned->child2_resize)
1200         paned->child1_size = MAX (0, allocation - child2_req);
1201       else if (!paned->child1_resize && paned->child2_resize)
1202         paned->child1_size = child1_req;
1203       else if (child1_req + child2_req != 0)
1204         paned->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
1205       else
1206         paned->child1_size = allocation * 0.5 + 0.5;
1207     }
1208   else
1209     {
1210       /* If the position was set before the initial allocation.
1211        * (paned->last_allocation <= 0) just clamp it and leave it.
1212        */
1213       if (paned->last_allocation > 0)
1214         {
1215           if (paned->child1_resize && !paned->child2_resize)
1216             paned->child1_size += allocation - paned->last_allocation;
1217           else if (!(!paned->child1_resize && paned->child2_resize))
1218             paned->child1_size = allocation * ((gdouble) paned->child1_size / (paned->last_allocation)) + 0.5;
1219         }
1220     }
1221
1222   paned->child1_size = CLAMP (paned->child1_size,
1223                               paned->min_position,
1224                               paned->max_position);
1225
1226   gtk_widget_set_child_visible (paned->child1, paned->child1_size != 0);
1227   gtk_widget_set_child_visible (paned->child2, paned->child1_size != allocation);
1228
1229   g_object_freeze_notify (G_OBJECT (paned));
1230   if (paned->child1_size != old_position)
1231     g_object_notify (G_OBJECT (paned), "position");
1232   if (paned->min_position != old_min_position)
1233     g_object_notify (G_OBJECT (paned), "min-position");
1234   if (paned->max_position != old_max_position)
1235     g_object_notify (G_OBJECT (paned), "max-position");
1236   g_object_thaw_notify (G_OBJECT (paned));
1237
1238   paned->last_allocation = allocation;
1239 }
1240
1241 static void
1242 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1243 {
1244   if (paned->priv->saved_focus)
1245     g_object_remove_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1246                                   (gpointer *)&(paned->priv->saved_focus));
1247
1248   paned->priv->saved_focus = widget;
1249
1250   if (paned->priv->saved_focus)
1251     g_object_add_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1252                                (gpointer *)&(paned->priv->saved_focus));
1253 }
1254
1255 static void
1256 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1257 {
1258   if (paned->priv->first_paned)
1259     g_object_remove_weak_pointer (G_OBJECT (paned->priv->first_paned),
1260                                   (gpointer *)&(paned->priv->first_paned));
1261
1262   paned->priv->first_paned = first_paned;
1263
1264   if (paned->priv->first_paned)
1265     g_object_add_weak_pointer (G_OBJECT (paned->priv->first_paned),
1266                                (gpointer *)&(paned->priv->first_paned));
1267 }
1268
1269 static void
1270 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1271 {
1272   if (paned->last_child1_focus)
1273     g_object_remove_weak_pointer (G_OBJECT (paned->last_child1_focus),
1274                                   (gpointer *)&(paned->last_child1_focus));
1275
1276   paned->last_child1_focus = widget;
1277
1278   if (paned->last_child1_focus)
1279     g_object_add_weak_pointer (G_OBJECT (paned->last_child1_focus),
1280                                (gpointer *)&(paned->last_child1_focus));
1281 }
1282
1283 static void
1284 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1285 {
1286   if (paned->last_child2_focus)
1287     g_object_remove_weak_pointer (G_OBJECT (paned->last_child2_focus),
1288                                   (gpointer *)&(paned->last_child2_focus));
1289
1290   paned->last_child2_focus = widget;
1291
1292   if (paned->last_child2_focus)
1293     g_object_add_weak_pointer (G_OBJECT (paned->last_child2_focus),
1294                                (gpointer *)&(paned->last_child2_focus));
1295 }
1296
1297 static GtkWidget *
1298 paned_get_focus_widget (GtkPaned *paned)
1299 {
1300   GtkWidget *toplevel;
1301
1302   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1303   if (GTK_WIDGET_TOPLEVEL (toplevel))
1304     return GTK_WINDOW (toplevel)->focus_widget;
1305
1306   return NULL;
1307 }
1308
1309 static void
1310 gtk_paned_set_focus_child (GtkContainer *container,
1311                            GtkWidget    *focus_child)
1312 {
1313   GtkPaned *paned;
1314   
1315   g_return_if_fail (GTK_IS_PANED (container));
1316
1317   paned = GTK_PANED (container);
1318  
1319   if (focus_child == NULL)
1320     {
1321       GtkWidget *last_focus;
1322       GtkWidget *w;
1323       
1324       last_focus = paned_get_focus_widget (paned);
1325
1326       if (last_focus)
1327         {
1328           /* If there is one or more paned widgets between us and the
1329            * focus widget, we want the topmost of those as last_focus
1330            */
1331           for (w = last_focus; w != GTK_WIDGET (paned); w = w->parent)
1332             if (GTK_IS_PANED (w))
1333               last_focus = w;
1334           
1335           if (container->focus_child == paned->child1)
1336             gtk_paned_set_last_child1_focus (paned, last_focus);
1337           else if (container->focus_child == paned->child2)
1338             gtk_paned_set_last_child2_focus (paned, last_focus);
1339         }
1340     }
1341
1342   if (parent_class->set_focus_child)
1343     (* parent_class->set_focus_child) (container, focus_child);
1344 }
1345
1346 static void
1347 gtk_paned_get_cycle_chain (GtkPaned          *paned,
1348                            GtkDirectionType   direction,
1349                            GList            **widgets)
1350 {
1351   GtkContainer *container = GTK_CONTAINER (paned);
1352   GtkWidget *ancestor = NULL;
1353   GList *temp_list = NULL;
1354   GList *list;
1355
1356   if (paned->in_recursion)
1357     return;
1358
1359   g_assert (widgets != NULL);
1360
1361   if (paned->last_child1_focus &&
1362       !gtk_widget_is_ancestor (paned->last_child1_focus, GTK_WIDGET (paned)))
1363     {
1364       gtk_paned_set_last_child1_focus (paned, NULL);
1365     }
1366
1367   if (paned->last_child2_focus &&
1368       !gtk_widget_is_ancestor (paned->last_child2_focus, GTK_WIDGET (paned)))
1369     {
1370       gtk_paned_set_last_child2_focus (paned, NULL);
1371     }
1372
1373   if (GTK_WIDGET (paned)->parent)
1374     ancestor = gtk_widget_get_ancestor (GTK_WIDGET (paned)->parent, GTK_TYPE_PANED);
1375
1376   /* The idea here is that temp_list is a list of widgets we want to cycle
1377    * to. The list is prioritized so that the first element is our first
1378    * choice, the next our second, and so on.
1379    *
1380    * We can't just use g_list_reverse(), because we want to try
1381    * paned->last_child?_focus before paned->child?, both when we
1382    * are going forward and backward.
1383    */
1384   if (direction == GTK_DIR_TAB_FORWARD)
1385     {
1386       if (container->focus_child == paned->child1)
1387         {
1388           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1389           temp_list = g_list_append (temp_list, paned->child2);
1390           temp_list = g_list_append (temp_list, ancestor);
1391         }
1392       else if (container->focus_child == paned->child2)
1393         {
1394           temp_list = g_list_append (temp_list, ancestor);
1395           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1396           temp_list = g_list_append (temp_list, paned->child1);
1397         }
1398       else
1399         {
1400           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1401           temp_list = g_list_append (temp_list, paned->child1);
1402           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1403           temp_list = g_list_append (temp_list, paned->child2);
1404           temp_list = g_list_append (temp_list, ancestor);
1405         }
1406     }
1407   else
1408     {
1409       if (container->focus_child == paned->child1)
1410         {
1411           temp_list = g_list_append (temp_list, ancestor);
1412           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1413           temp_list = g_list_append (temp_list, paned->child2);
1414         }
1415       else if (container->focus_child == paned->child2)
1416         {
1417           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1418           temp_list = g_list_append (temp_list, paned->child1);
1419           temp_list = g_list_append (temp_list, ancestor);
1420         }
1421       else
1422         {
1423           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1424           temp_list = g_list_append (temp_list, paned->child2);
1425           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1426           temp_list = g_list_append (temp_list, paned->child1);
1427           temp_list = g_list_append (temp_list, ancestor);
1428         }
1429     }
1430
1431   /* Walk the list and expand all the paned widgets. */
1432   for (list = temp_list; list != NULL; list = list->next)
1433     {
1434       GtkWidget *widget = list->data;
1435
1436       if (widget)
1437         {
1438           if (GTK_IS_PANED (widget))
1439             {
1440               paned->in_recursion = TRUE;
1441               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
1442               paned->in_recursion = FALSE;
1443             }
1444           else
1445             {
1446               *widgets = g_list_append (*widgets, widget);
1447             }
1448         }
1449     }
1450
1451   g_list_free (temp_list);
1452 }
1453
1454 static gboolean
1455 gtk_paned_cycle_child_focus (GtkPaned *paned,
1456                              gboolean  reversed)
1457 {
1458   GList *cycle_chain = NULL;
1459   GList *list;
1460   
1461   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
1462
1463   /* ignore f6 if the handle is focused */
1464   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1465     return TRUE;
1466   
1467   /* we can't just let the event propagate up the hierarchy,
1468    * because the paned will want to cycle focus _unless_ an
1469    * ancestor paned handles the event
1470    */
1471   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
1472
1473   for (list = cycle_chain; list != NULL; list = list->next)
1474     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
1475       break;
1476
1477   g_list_free (cycle_chain);
1478   
1479   return TRUE;
1480 }
1481
1482 static void
1483 get_child_panes (GtkWidget  *widget,
1484                  GList     **panes)
1485 {
1486   if (GTK_IS_PANED (widget))
1487     {
1488       GtkPaned *paned = GTK_PANED (widget);
1489       
1490       get_child_panes (paned->child1, panes);
1491       *panes = g_list_prepend (*panes, widget);
1492       get_child_panes (paned->child2, panes);
1493     }
1494   else if (GTK_IS_CONTAINER (widget))
1495     {
1496       gtk_container_foreach (GTK_CONTAINER (widget),
1497                              (GtkCallback)get_child_panes, panes);
1498     }
1499 }
1500
1501 static GList *
1502 get_all_panes (GtkPaned *paned)
1503 {
1504   GtkPaned *topmost = NULL;
1505   GList *result = NULL;
1506   GtkWidget *w;
1507   
1508   for (w = GTK_WIDGET (paned); w != NULL; w = w->parent)
1509     {
1510       if (GTK_IS_PANED (w))
1511         topmost = GTK_PANED (w);
1512     }
1513
1514   g_assert (topmost);
1515
1516   get_child_panes (GTK_WIDGET (topmost), &result);
1517
1518   return g_list_reverse (result);
1519 }
1520
1521 static void
1522 gtk_paned_find_neighbours (GtkPaned  *paned,
1523                            GtkPaned **next,
1524                            GtkPaned **prev)
1525 {
1526   GList *all_panes;
1527   GList *this_link;
1528
1529   all_panes = get_all_panes (paned);
1530   g_assert (all_panes);
1531
1532   this_link = g_list_find (all_panes, paned);
1533
1534   g_assert (this_link);
1535   
1536   if (this_link->next)
1537     *next = this_link->next->data;
1538   else
1539     *next = all_panes->data;
1540
1541   if (this_link->prev)
1542     *prev = this_link->prev->data;
1543   else
1544     *prev = g_list_last (all_panes)->data;
1545
1546   g_list_free (all_panes);
1547 }
1548
1549 static gboolean
1550 gtk_paned_move_handle (GtkPaned      *paned,
1551                        GtkScrollType  scroll)
1552 {
1553   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1554     {
1555       gint old_position;
1556       gint new_position;
1557       gint increment;
1558       
1559       enum {
1560         SINGLE_STEP_SIZE = 1,
1561         PAGE_STEP_SIZE   = 75
1562       };
1563       
1564       new_position = old_position = gtk_paned_get_position (paned);
1565       increment = 0;
1566       
1567       switch (scroll)
1568         {
1569         case GTK_SCROLL_STEP_LEFT:
1570         case GTK_SCROLL_STEP_UP:
1571         case GTK_SCROLL_STEP_BACKWARD:
1572           increment = - SINGLE_STEP_SIZE;
1573           break;
1574           
1575         case GTK_SCROLL_STEP_RIGHT:
1576         case GTK_SCROLL_STEP_DOWN:
1577         case GTK_SCROLL_STEP_FORWARD:
1578           increment = SINGLE_STEP_SIZE;
1579           break;
1580           
1581         case GTK_SCROLL_PAGE_LEFT:
1582         case GTK_SCROLL_PAGE_UP:
1583         case GTK_SCROLL_PAGE_BACKWARD:
1584           increment = - PAGE_STEP_SIZE;
1585           break;
1586           
1587         case GTK_SCROLL_PAGE_RIGHT:
1588         case GTK_SCROLL_PAGE_DOWN:
1589         case GTK_SCROLL_PAGE_FORWARD:
1590           increment = PAGE_STEP_SIZE;
1591           break;
1592           
1593         case GTK_SCROLL_START:
1594           new_position = paned->min_position;
1595           break;
1596           
1597         case GTK_SCROLL_END:
1598           new_position = paned->max_position;
1599           break;
1600
1601         default:
1602           break;
1603         }
1604
1605       if (increment)
1606         {
1607           if (is_rtl (paned))
1608             increment = -increment;
1609           
1610           new_position = old_position + increment;
1611         }
1612       
1613       new_position = CLAMP (new_position, paned->min_position, paned->max_position);
1614       
1615       if (old_position != new_position)
1616         gtk_paned_set_position (paned, new_position);
1617
1618       return TRUE;
1619     }
1620
1621   return FALSE;
1622 }
1623
1624 static void
1625 gtk_paned_restore_focus (GtkPaned *paned)
1626 {
1627   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1628     {
1629       if (paned->priv->saved_focus &&
1630           GTK_WIDGET_SENSITIVE (paned->priv->saved_focus))
1631         {
1632           gtk_widget_grab_focus (paned->priv->saved_focus);
1633         }
1634       else
1635         {
1636           /* the saved focus is somehow not available for focusing,
1637            * try
1638            *   1) tabbing into the paned widget
1639            * if that didn't work,
1640            *   2) unset focus for the window if there is one
1641            */
1642           
1643           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
1644             {
1645               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1646               
1647               if (GTK_IS_WINDOW (toplevel))
1648                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
1649             }
1650         }
1651       
1652       gtk_paned_set_saved_focus (paned, NULL);
1653       gtk_paned_set_first_paned (paned, NULL);
1654     }
1655 }
1656
1657 static gboolean
1658 gtk_paned_accept_position (GtkPaned *paned)
1659 {
1660   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1661     {
1662       paned->original_position = -1;
1663       gtk_paned_restore_focus (paned);
1664
1665       return TRUE;
1666     }
1667
1668   return FALSE;
1669 }
1670
1671
1672 static gboolean
1673 gtk_paned_cancel_position (GtkPaned *paned)
1674 {
1675   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1676     {
1677       if (paned->original_position != -1)
1678         {
1679           gtk_paned_set_position (paned, paned->original_position);
1680           paned->original_position = -1;
1681         }
1682
1683       gtk_paned_restore_focus (paned);
1684       return TRUE;
1685     }
1686
1687   return FALSE;
1688 }
1689
1690 static gboolean
1691 gtk_paned_cycle_handle_focus (GtkPaned *paned,
1692                               gboolean  reversed)
1693 {
1694   GtkPaned *next, *prev;
1695   
1696   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1697     {
1698       GtkPaned *focus = NULL;
1699
1700       if (!paned->priv->first_paned)
1701         {
1702           /* The first_pane has disappeared. As an ad-hoc solution,
1703            * we make the currently focused paned the first_paned. To the
1704            * user this will seem like the paned cycling has been reset.
1705            */
1706           
1707           gtk_paned_set_first_paned (paned, paned);
1708         }
1709       
1710       gtk_paned_find_neighbours (paned, &next, &prev);
1711
1712       if (reversed && prev &&
1713           prev != paned && paned != paned->priv->first_paned)
1714         {
1715           focus = prev;
1716         }
1717       else if (!reversed && next &&
1718                next != paned && next != paned->priv->first_paned)
1719         {
1720           focus = next;
1721         }
1722       else
1723         {
1724           gtk_paned_accept_position (paned);
1725           return TRUE;
1726         }
1727
1728       g_assert (focus);
1729       
1730       gtk_paned_set_saved_focus (focus, paned->priv->saved_focus);
1731       gtk_paned_set_first_paned (focus, paned->priv->first_paned);
1732       
1733       gtk_paned_set_saved_focus (paned, NULL);
1734       gtk_paned_set_first_paned (paned, NULL);
1735       
1736       gtk_widget_grab_focus (GTK_WIDGET (focus));
1737       
1738       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
1739         {
1740           paned->original_position = -1;
1741           focus->original_position = gtk_paned_get_position (focus);
1742         }
1743     }
1744   else
1745     {
1746       GtkContainer *container = GTK_CONTAINER (paned);
1747       GtkPaned *focus;
1748       GtkPaned *first;
1749       GtkPaned *prev, *next;
1750       GtkWidget *toplevel;
1751
1752       gtk_paned_find_neighbours (paned, &next, &prev);
1753
1754       if (container->focus_child == paned->child1)
1755         {
1756           if (reversed)
1757             {
1758               focus = prev;
1759               first = paned;
1760             }
1761           else
1762             {
1763               focus = paned;
1764               first = paned;
1765             }
1766         }
1767       else if (container->focus_child == paned->child2)
1768         {
1769           if (reversed)
1770             {
1771               focus = paned;
1772               first = next;
1773             }
1774           else
1775             {
1776               focus = next;
1777               first = next;
1778             }
1779         }
1780       else
1781         {
1782           /* Focus is not inside this paned, and we don't have focus.
1783            * Presumably this happened because the application wants us
1784            * to start keyboard navigating.
1785            */
1786           focus = paned;
1787
1788           if (reversed)
1789             first = paned;
1790           else
1791             first = next;
1792         }
1793
1794       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1795
1796       if (GTK_IS_WINDOW (toplevel))
1797         gtk_paned_set_saved_focus (focus, GTK_WINDOW (toplevel)->focus_widget);
1798       gtk_paned_set_first_paned (focus, first);
1799       focus->original_position = gtk_paned_get_position (focus); 
1800
1801       gtk_widget_grab_focus (GTK_WIDGET (focus));
1802    }
1803   
1804   return TRUE;
1805 }
1806
1807 static gboolean
1808 gtk_paned_toggle_handle_focus (GtkPaned *paned)
1809 {
1810   /* This function/signal has the wrong name. It is called when you
1811    * press Tab or Shift-Tab and what we do is act as if
1812    * the user pressed Return and then Tab or Shift-Tab
1813    */
1814   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1815     gtk_paned_accept_position (paned);
1816
1817   return FALSE;
1818 }
1819
1820 #define __GTK_PANED_C__
1821 #include "gtkaliasdef.c"