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