8bd5253deae0f8c75db34c273b51d87b61931fcb
[pspp] / lib / gtk-contrib / gtkxpaned.c
1 /*******************************************************************************
2  **3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 
3  **      10        20        30        40        50        60        70        80
4  **
5  **  library for GtkXPaned-widget, a 2x2 grid-like variation of GtkPaned of gtk+
6  **  Copyright (C) 2012, 2013 Free Software Foundation, Inc.
7  **  Copyright (C) 2005-2006 Mirco "MacSlow" Müller <macslow@bangang.de>
8  **
9  **  This library is free software; you can redistribute it and/or
10  **  modify it under the terms of the GNU Lesser General Public
11  **  License as published by the Free Software Foundation; either
12  **  version 2.1 of the License, or (at your option) any later version.
13  **
14  **  This library is distributed in the hope that it will be useful,
15  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  **  Lesser General Public License for more details.
18  **
19  **  You should have received a copy of the GNU Lesser General Public
20  **  License along with this library; if not, write to the Free Software
21  **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  **
23  **  GtkXPaned is based on GtkPaned which was done by...
24  **
25  **  "Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald"
26  **
27  **  and later modified by...
28  **
29  **  "the GTK+ Team and others 1997-2000"
30  **
31  *******************************************************************************/
32
33 #include <config.h>
34 #include "gtkxpaned.h"
35
36 #include <gtk/gtk.h>
37 #include <ui/gui/psppire-marshal.h>
38 #include <gdk/gdkkeysyms.h>
39 #include <gdk/gdkkeysyms-compat.h>
40
41 enum WidgetProperties
42   {
43     PROP_0,
44     PROP_X_POSITION,
45     PROP_Y_POSITION,
46     PROP_POSITION_SET,
47     PROP_MIN_X_POSITION,
48     PROP_MIN_Y_POSITION,
49     PROP_MAX_X_POSITION,
50     PROP_MAX_Y_POSITION
51   };
52
53 enum ChildProperties
54   {
55     CHILD_PROP_0,
56     CHILD_PROP_RESIZE,
57     CHILD_PROP_SHRINK
58   };
59
60 enum WidgetSignals
61   {
62     CYCLE_CHILD_FOCUS,
63     TOGGLE_HANDLE_FOCUS,
64     MOVE_HANDLE,
65     CYCLE_HANDLE_FOCUS,
66     ACCEPT_POSITION,
67     CANCEL_POSITION,
68     LAST_SIGNAL
69   };
70
71 static void gtk_xpaned_class_init (GtkXPanedClass * klass);
72
73 static void gtk_xpaned_init (GtkXPaned * xpaned);
74
75 static void
76 gtk_xpaned_get_preferred_width (GtkWidget *widget,
77                                 gint      *minimal_width,
78                                 gint      *natural_width)
79 {
80   GtkXPaned *xpaned = GTK_XPANED (widget);
81   gint tl[2], tr[2], bl[2], br[2];
82   gint overhead;
83   gint w[2];
84   int i;
85
86   if (xpaned->top_left_child
87       && gtk_widget_get_visible (xpaned->top_left_child))
88     gtk_widget_get_preferred_width (xpaned->top_left_child, &tl[0], &tl[1]);
89   else
90     tl[0] = tl[1] = 0;
91
92   if (xpaned->top_right_child
93       && gtk_widget_get_visible (xpaned->top_right_child))
94     gtk_widget_get_preferred_width (xpaned->top_right_child, &tr[0], &tr[1]);
95   else
96     tr[0] = tr[1] = 0;
97
98   if (xpaned->bottom_left_child
99       && gtk_widget_get_visible (xpaned->bottom_left_child))
100     gtk_widget_get_preferred_width (xpaned->bottom_left_child, &bl[0], &bl[1]);
101   else
102     bl[0] = bl[1] = 0;
103
104   if (xpaned->bottom_right_child
105       && gtk_widget_get_visible (xpaned->bottom_right_child))
106     gtk_widget_get_preferred_width (xpaned->bottom_right_child,
107                                     &br[0], &br[1]);
108   else
109     br[0] = br[1] = 0;
110
111   /* add 2 times the set border-width to the GtkXPaneds requisition */
112   overhead = gtk_container_get_border_width (GTK_CONTAINER (xpaned)) * 2;
113
114   /* also add the handle "thickness" to GtkXPaned's width requisition */
115   if (xpaned->top_left_child
116       && gtk_widget_get_visible (xpaned->top_left_child)
117       && xpaned->top_right_child
118       && gtk_widget_get_visible (xpaned->top_right_child)
119       && xpaned->bottom_left_child
120       && gtk_widget_get_visible (xpaned->bottom_left_child)
121       && xpaned->bottom_right_child
122       && gtk_widget_get_visible (xpaned->bottom_right_child))
123     {
124       gint handle_size;
125
126       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
127       overhead += handle_size;
128     }
129
130   for (i = 0; i < 2; i++)
131     w[i] = (br[i] ? br[i] : MAX (tl[i] + tr[i], bl[i])) + overhead;
132
133   *minimal_width = w[0];
134   *natural_width = w[1];
135 }
136
137 static void
138 gtk_xpaned_get_preferred_height (GtkWidget *widget,
139                                 gint      *minimal_height,
140                                 gint      *natural_height)
141 {
142   GtkXPaned *xpaned = GTK_XPANED (widget);
143   gint tl[2], tr[2], bl[2], br[2];
144   gint overhead;
145   gint h[2];
146   int i;
147
148   if (xpaned->top_left_child
149       && gtk_widget_get_visible (xpaned->top_left_child))
150     gtk_widget_get_preferred_height (xpaned->top_left_child, &tl[0], &tl[1]);
151   else
152     tl[0] = tl[1] = 0;
153
154   if (xpaned->top_right_child
155       && gtk_widget_get_visible (xpaned->top_right_child))
156     gtk_widget_get_preferred_height (xpaned->top_right_child, &tr[0], &tr[1]);
157   else
158     tr[0] = tr[1] = 0;
159
160   if (xpaned->bottom_left_child
161       && gtk_widget_get_visible (xpaned->bottom_left_child))
162     gtk_widget_get_preferred_height (xpaned->bottom_left_child,
163                                      &bl[0], &bl[1]);
164   else
165     bl[0] = bl[1] = 0;
166
167   if (xpaned->bottom_right_child
168       && gtk_widget_get_visible (xpaned->bottom_right_child))
169     gtk_widget_get_preferred_height (xpaned->bottom_right_child,
170                                     &br[0], &br[1]);
171   else
172     br[0] = br[1] = 0;
173
174   /* add 2 times the set border-width to the GtkXPaneds requisition */
175   overhead = gtk_container_get_border_width (GTK_CONTAINER (xpaned)) * 2;
176
177   /* also add the handle "thickness" to GtkXPaned's height-requisition */
178   if (xpaned->top_left_child
179       && gtk_widget_get_visible (xpaned->top_left_child)
180       && xpaned->top_right_child
181       && gtk_widget_get_visible (xpaned->top_right_child)
182       && xpaned->bottom_left_child
183       && gtk_widget_get_visible (xpaned->bottom_left_child)
184       && xpaned->bottom_right_child
185       && gtk_widget_get_visible (xpaned->bottom_right_child))
186     {
187       gint handle_size;
188
189       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
190       overhead += handle_size;
191     }
192
193   for (i = 0; i < 2; i++)
194     h[i] = (br[i] ? br[i] : bl[i] + MAX (tl[i], tr[i])) + overhead;
195
196   *minimal_height = h[0];
197   *natural_height = h[1];
198 }
199
200 static void gtk_xpaned_size_allocate (GtkWidget * widget,
201                                       GtkAllocation * allocation);
202
203 static void gtk_xpaned_set_property (GObject * object,
204                                      guint prop_id,
205                                      const GValue * value,
206                                      GParamSpec * pspec);
207
208 static void gtk_xpaned_get_property (GObject * object,
209                                      guint prop_id,
210                                      GValue * value, GParamSpec * pspec);
211
212 static void gtk_xpaned_set_child_property (GtkContainer * container,
213                                            GtkWidget * child,
214                                            guint property_id,
215                                            const GValue * value,
216                                            GParamSpec * pspec);
217
218 static void gtk_xpaned_get_child_property (GtkContainer * container,
219                                            GtkWidget * child,
220                                            guint property_id,
221                                            GValue * value,
222                                            GParamSpec * pspec);
223
224 static void gtk_xpaned_finalize (GObject * object);
225
226 static void gtk_xpaned_realize (GtkWidget * widget);
227
228 static void gtk_xpaned_unrealize (GtkWidget * widget);
229
230 static void gtk_xpaned_map (GtkWidget * widget);
231
232 static void gtk_xpaned_unmap (GtkWidget * widget);
233
234 static gboolean gtk_xpaned_draw (GtkWidget * widget,
235                                    cairo_t *ct);
236
237 static gboolean gtk_xpaned_enter (GtkWidget * widget,
238                                   GdkEventCrossing * event);
239
240 static gboolean gtk_xpaned_leave (GtkWidget * widget,
241                                   GdkEventCrossing * event);
242
243 static gboolean gtk_xpaned_button_press (GtkWidget * widget,
244                                          GdkEventButton * event);
245
246 static gboolean gtk_xpaned_button_release (GtkWidget * widget,
247                                            GdkEventButton * event);
248
249 static gboolean gtk_xpaned_motion (GtkWidget * widget,
250                                    GdkEventMotion * event);
251
252 static gboolean gtk_xpaned_focus (GtkWidget * widget,
253                                   GtkDirectionType direction);
254
255 static void gtk_xpaned_add (GtkContainer * container, GtkWidget * widget);
256
257 static void gtk_xpaned_remove (GtkContainer * container, GtkWidget * widget);
258
259 static void gtk_xpaned_forall (GtkContainer * container,
260                                gboolean include_internals,
261                                GtkCallback callback, gpointer callback_data);
262
263 static void gtk_xpaned_set_focus_child (GtkContainer * container,
264                                         GtkWidget * child);
265
266 static void gtk_xpaned_set_saved_focus (GtkXPaned * xpaned,
267                                         GtkWidget * widget);
268
269 static void gtk_xpaned_set_first_xpaned (GtkXPaned * xpaned,
270                                          GtkXPaned * first_xpaned);
271
272 static void gtk_xpaned_set_last_top_left_child_focus (GtkXPaned * xpaned,
273                                                       GtkWidget * widget);
274
275 static void gtk_xpaned_set_last_top_right_child_focus (GtkXPaned * xpaned,
276                                                        GtkWidget * widget);
277
278 static void gtk_xpaned_set_last_bottom_left_child_focus (GtkXPaned * xpaned,
279                                                          GtkWidget * widget);
280
281 static void gtk_xpaned_set_last_bottom_right_child_focus (GtkXPaned * xpaned,
282                                                           GtkWidget * widget);
283
284 static gboolean gtk_xpaned_cycle_child_focus (GtkXPaned * xpaned,
285                                               gboolean reverse);
286
287 static gboolean gtk_xpaned_cycle_handle_focus (GtkXPaned * xpaned,
288                                                gboolean reverse);
289
290 static gboolean gtk_xpaned_move_handle (GtkXPaned * xpaned,
291                                         GtkScrollType scroll);
292
293 static gboolean gtk_xpaned_accept_position (GtkXPaned * xpaned);
294
295 static gboolean gtk_xpaned_cancel_position (GtkXPaned * xpaned);
296
297 static gboolean gtk_xpaned_toggle_handle_focus (GtkXPaned * xpaned);
298
299 static GType gtk_xpaned_child_type (GtkContainer * container);
300
301 static GtkContainerClass *parent_class = NULL;
302
303 struct _GtkXPanedPrivate
304 {
305   GtkWidget *saved_focus;
306   GtkXPaned *first_xpaned;
307 };
308
309 GType
310 gtk_xpaned_get_type (void)
311 {
312   static GType xpaned_type = 0;
313
314   if (!xpaned_type)
315     {
316       static const GTypeInfo xpaned_info = {
317         sizeof (GtkXPanedClass),
318         NULL,                   /* base_init */
319         NULL,                   /* base_finalize */
320         (GClassInitFunc) gtk_xpaned_class_init,
321         NULL,                   /* class_finalize */
322         NULL,                   /* class_data */
323         sizeof (GtkXPaned),
324         0,                      /* n_preallocs */
325         (GInstanceInitFunc) gtk_xpaned_init
326       };
327
328       xpaned_type = g_type_register_static (GTK_TYPE_CONTAINER,
329                                             "GtkXPaned", &xpaned_info, 0);
330     }
331
332   return xpaned_type;
333 }
334
335 GtkWidget *
336 gtk_xpaned_new (void)
337 {
338   GtkXPaned *xpaned;
339
340   xpaned = g_object_new (GTK_TYPE_XPANED, NULL);
341
342   return GTK_WIDGET (xpaned);
343 }
344
345 static guint signals[LAST_SIGNAL] = { 0 };
346
347 static void
348 add_tab_bindings (GtkBindingSet * binding_set, GdkModifierType modifiers)
349 {
350   gtk_binding_entry_add_signal (binding_set,
351                                 GDK_Tab, modifiers, "toggle_handle_focus", 0);
352
353   gtk_binding_entry_add_signal (binding_set,
354                                 GDK_KP_Tab,
355                                 modifiers, "toggle_handle_focus", 0);
356 }
357
358 static void
359 add_move_binding (GtkBindingSet * binding_set,
360                   guint keyval, GdkModifierType mask, GtkScrollType scroll)
361 {
362   gtk_binding_entry_add_signal (binding_set,
363                                 keyval,
364                                 mask,
365                                 "move_handle",
366                                 1, GTK_TYPE_SCROLL_TYPE, scroll);
367 }
368
369 static void
370 gtk_xpaned_class_init (GtkXPanedClass * class)
371 {
372   GObjectClass *object_class;
373   GtkWidgetClass *widget_class;
374   GtkContainerClass *container_class;
375   GtkXPanedClass *xpaned_class;
376   GtkBindingSet *binding_set;
377
378   object_class = (GObjectClass *) class;
379   widget_class = (GtkWidgetClass *) class;
380   container_class = (GtkContainerClass *) class;
381   xpaned_class = (GtkXPanedClass *) class;
382
383   parent_class = g_type_class_peek_parent (class);
384
385   object_class->set_property = gtk_xpaned_set_property;
386   object_class->get_property = gtk_xpaned_get_property;
387   object_class->finalize = gtk_xpaned_finalize;
388
389   widget_class->realize = gtk_xpaned_realize;
390   widget_class->unrealize = gtk_xpaned_unrealize;
391   widget_class->map = gtk_xpaned_map;
392   widget_class->unmap = gtk_xpaned_unmap;
393   widget_class->draw = gtk_xpaned_draw;
394   widget_class->focus = gtk_xpaned_focus;
395   widget_class->enter_notify_event = gtk_xpaned_enter;
396   widget_class->leave_notify_event = gtk_xpaned_leave;
397   widget_class->button_press_event = gtk_xpaned_button_press;
398   widget_class->button_release_event = gtk_xpaned_button_release;
399   widget_class->motion_notify_event = gtk_xpaned_motion;
400   widget_class->get_preferred_width  = gtk_xpaned_get_preferred_width;
401   widget_class->get_preferred_height = gtk_xpaned_get_preferred_height;
402
403   widget_class->size_allocate = gtk_xpaned_size_allocate;
404
405   container_class->add = gtk_xpaned_add;
406   container_class->remove = gtk_xpaned_remove;
407   container_class->forall = gtk_xpaned_forall;
408   container_class->child_type = gtk_xpaned_child_type;
409   container_class->set_focus_child = gtk_xpaned_set_focus_child;
410   container_class->set_child_property = gtk_xpaned_set_child_property;
411   container_class->get_child_property = gtk_xpaned_get_child_property;
412
413   xpaned_class->cycle_child_focus = gtk_xpaned_cycle_child_focus;
414   xpaned_class->toggle_handle_focus = gtk_xpaned_toggle_handle_focus;
415   xpaned_class->move_handle = gtk_xpaned_move_handle;
416   xpaned_class->cycle_handle_focus = gtk_xpaned_cycle_handle_focus;
417   xpaned_class->accept_position = gtk_xpaned_accept_position;
418   xpaned_class->cancel_position = gtk_xpaned_cancel_position;
419
420   g_object_class_install_property (object_class,
421                                    PROP_X_POSITION,
422                                    g_param_spec_int ("x-position",
423                                                      ("x-Position"),
424                                                      ("x-Position of paned separator in pixels (0 means all the way to the left)"),
425                                                      0,
426                                                      G_MAXINT,
427                                                      0,
428                                                      G_PARAM_READABLE |
429                                                      G_PARAM_WRITABLE));
430
431   g_object_class_install_property (object_class,
432                                    PROP_Y_POSITION,
433                                    g_param_spec_int ("y-position",
434                                                      "y-Position",
435                                                      "y-Position of paned separator in pixels (0 means all the way to the top)",
436                                                      0,
437                                                      G_MAXINT,
438                                                      0,
439                                                      G_PARAM_READABLE |
440                                                      G_PARAM_WRITABLE));
441
442   g_object_class_install_property (object_class,
443                                    PROP_POSITION_SET,
444                                    g_param_spec_boolean ("position-set",
445                                                          "Position Set",
446                                                          "TRUE if the Position property should be used",
447                                                          FALSE,
448                                                          G_PARAM_READABLE |
449                                                          G_PARAM_WRITABLE));
450
451   gtk_widget_class_install_style_property (widget_class,
452                                            g_param_spec_int ("handle-size",
453                                                              "Handle Size",
454                                                              "Width of handle",
455                                                              0,
456                                                              G_MAXINT,
457                                                              3,
458                                                              G_PARAM_READABLE));
459   /**
460    * GtkXPaned:min-x-position:
461    *
462    * The smallest possible value for the x-position property. This property is derived from the
463    * size and shrinkability of the widget's children.
464    *
465    * Since: 2.4
466    */
467   g_object_class_install_property (object_class,
468                                    PROP_MIN_X_POSITION,
469                                    g_param_spec_int ("min-x-position",
470                                                      "Minimal x-Position",
471                                                      "Smallest possible value for the \"x-position\" property",
472                                                      0,
473                                                      G_MAXINT,
474                                                      0, G_PARAM_READABLE));
475
476   /**
477    * GtkXPaned:min-y-position:
478    *
479    * The smallest possible value for the y-position property. This property is derived from the
480    * size and shrinkability of the widget's children.
481    *
482    * Since: 2.4
483    */
484   g_object_class_install_property (object_class,
485                                    PROP_MIN_Y_POSITION,
486                                    g_param_spec_int ("min-y-position",
487                                                      "Minimal y-Position",
488                                                      "Smallest possible value for the \"y-position\" property",
489                                                      0,
490                                                      G_MAXINT,
491                                                      0, G_PARAM_READABLE));
492
493   /**
494    * GtkPaned:max-x-position:
495    *
496    * The largest possible value for the x-position property. This property is derived from the
497    * size and shrinkability of the widget's children.
498    *
499    * Since: 2.4
500    */
501   g_object_class_install_property (object_class,
502                                    PROP_MAX_X_POSITION,
503                                    g_param_spec_int ("max-x-position",
504                                                      "Maximal x-Position",
505                                                      "Largest possible value for the \"x-position\" property",
506                                                      0,
507                                                      G_MAXINT,
508                                                      G_MAXINT,
509                                                      G_PARAM_READABLE));
510
511   /**
512    * GtkPaned:max-y-position:
513    *
514    * The largest possible value for the y-position property. This property is derived from the
515    * size and shrinkability of the widget's children.
516    *
517    * Since: 2.4
518    */
519   g_object_class_install_property (object_class,
520                                    PROP_MAX_Y_POSITION,
521                                    g_param_spec_int ("max-y-position",
522                                                      "Maximal y-Position",
523                                                      "Largest possible value for the \"y-position\" property",
524                                                      0,
525                                                      G_MAXINT,
526                                                      G_MAXINT,
527                                                      G_PARAM_READABLE));
528
529   /**
530    * GtkPaned:resize:
531    *
532    * The "resize" child property determines whether the child expands and 
533    * shrinks along with the paned widget.
534    * 
535    * Since: 2.4 
536    */
537   gtk_container_class_install_child_property (container_class,
538                                               CHILD_PROP_RESIZE,
539                                               g_param_spec_boolean ("resize",
540                                                                     "Resize",
541                                                                     "If TRUE, the child expands and shrinks along with the paned widget",
542                                                                     TRUE,
543                                                                     G_PARAM_READWRITE));
544
545   /**
546    * GtkPaned:shrink:
547    *
548    * The "shrink" child property determines whether the child can be made 
549    * smaller than its requisition.
550    * 
551    * Since: 2.4 
552    */
553   gtk_container_class_install_child_property (container_class,
554                                               CHILD_PROP_SHRINK,
555                                               g_param_spec_boolean ("shrink",
556                                                                     "Shrink",
557                                                                     "If TRUE, the child can be made smaller than its requisition",
558                                                                     TRUE,
559                                                                     G_PARAM_READWRITE));
560
561   signals[CYCLE_CHILD_FOCUS] = g_signal_new ("cycle-child-focus",
562                                              G_TYPE_FROM_CLASS (object_class),
563                                              G_SIGNAL_RUN_LAST |
564                                              G_SIGNAL_ACTION,
565                                              G_STRUCT_OFFSET (GtkXPanedClass,
566                                                               cycle_child_focus),
567                                              NULL, NULL,
568                                              psppire_marshal_BOOLEAN__BOOLEAN,
569                                              G_TYPE_BOOLEAN, 1,
570                                              G_TYPE_BOOLEAN);
571
572   signals[TOGGLE_HANDLE_FOCUS] = g_signal_new ("toggle-handle-focus",
573                                                G_TYPE_FROM_CLASS
574                                                (object_class),
575                                                G_SIGNAL_RUN_LAST |
576                                                G_SIGNAL_ACTION,
577                                                G_STRUCT_OFFSET
578                                                (GtkXPanedClass,
579                                                 toggle_handle_focus), NULL,
580                                                NULL,
581                                                psppire_marshal_BOOLEAN__VOID,
582                                                G_TYPE_BOOLEAN, 0);
583
584   signals[MOVE_HANDLE] = g_signal_new ("move-handle",
585                                        G_TYPE_FROM_CLASS (object_class),
586                                        G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
587                                        G_STRUCT_OFFSET (GtkXPanedClass,
588                                                         move_handle), NULL,
589                                        NULL, psppire_marshal_BOOLEAN__ENUM,
590                                        G_TYPE_BOOLEAN, 1,
591                                        GTK_TYPE_SCROLL_TYPE);
592
593   signals[CYCLE_HANDLE_FOCUS] = g_signal_new ("cycle-handle-focus",
594                                               G_TYPE_FROM_CLASS
595                                               (object_class),
596                                               G_SIGNAL_RUN_LAST |
597                                               G_SIGNAL_ACTION,
598                                               G_STRUCT_OFFSET (GtkXPanedClass,
599                                                                cycle_handle_focus),
600                                               NULL, NULL,
601                                               psppire_marshal_BOOLEAN__BOOLEAN,
602                                               G_TYPE_BOOLEAN, 1,
603                                               G_TYPE_BOOLEAN);
604
605   signals[ACCEPT_POSITION] = g_signal_new ("accept-position",
606                                            G_TYPE_FROM_CLASS (object_class),
607                                            G_SIGNAL_RUN_LAST |
608                                            G_SIGNAL_ACTION,
609                                            G_STRUCT_OFFSET (GtkXPanedClass,
610                                                             accept_position),
611                                            NULL, NULL,
612                                            psppire_marshal_BOOLEAN__VOID,
613                                            G_TYPE_BOOLEAN, 0);
614
615   signals[CANCEL_POSITION] = g_signal_new ("cancel-position",
616                                            G_TYPE_FROM_CLASS (object_class),
617                                            G_SIGNAL_RUN_LAST |
618                                            G_SIGNAL_ACTION,
619                                            G_STRUCT_OFFSET (GtkXPanedClass,
620                                                             cancel_position),
621                                            NULL, NULL,
622                                            psppire_marshal_BOOLEAN__VOID,
623                                            G_TYPE_BOOLEAN, 0);
624
625   binding_set = gtk_binding_set_by_class (class);
626
627   /* F6 and friends */
628   gtk_binding_entry_add_signal (binding_set,
629                                 GDK_F6, 0,
630                                 "cycle-child-focus", 1,
631                                 G_TYPE_BOOLEAN, FALSE);
632
633   gtk_binding_entry_add_signal (binding_set,
634                                 GDK_F6, GDK_SHIFT_MASK,
635                                 "cycle-child-focus", 1, G_TYPE_BOOLEAN, TRUE);
636
637   /* F8 and friends */
638   gtk_binding_entry_add_signal (binding_set,
639                                 GDK_F8, 0,
640                                 "cycle-handle-focus", 1,
641                                 G_TYPE_BOOLEAN, FALSE);
642
643   gtk_binding_entry_add_signal (binding_set,
644                                 GDK_F8, GDK_SHIFT_MASK,
645                                 "cycle-handle-focus", 1,
646                                 G_TYPE_BOOLEAN, TRUE);
647
648   add_tab_bindings (binding_set, 0);
649   add_tab_bindings (binding_set, GDK_CONTROL_MASK);
650   add_tab_bindings (binding_set, GDK_SHIFT_MASK);
651   add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK);
652
653   /* accept and cancel positions */
654   gtk_binding_entry_add_signal (binding_set,
655                                 GDK_Escape, 0, "cancel-position", 0);
656
657   gtk_binding_entry_add_signal (binding_set,
658                                 GDK_Return, 0, "accept-position", 0);
659
660   gtk_binding_entry_add_signal (binding_set,
661                                 GDK_KP_Enter, 0, "accept-position", 0);
662
663   gtk_binding_entry_add_signal (binding_set,
664                                 GDK_space, 0, "accept-position", 0);
665
666   gtk_binding_entry_add_signal (binding_set,
667                                 GDK_KP_Space, 0, "accept-position", 0);
668
669   /* move handle */
670   add_move_binding (binding_set, GDK_Left, 0, GTK_SCROLL_STEP_LEFT);
671   add_move_binding (binding_set, GDK_KP_Left, 0, GTK_SCROLL_STEP_LEFT);
672   add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
673                     GTK_SCROLL_PAGE_LEFT);
674   add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
675                     GTK_SCROLL_PAGE_LEFT);
676
677   add_move_binding (binding_set, GDK_Right, 0, GTK_SCROLL_STEP_RIGHT);
678   add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
679                     GTK_SCROLL_PAGE_RIGHT);
680   add_move_binding (binding_set, GDK_KP_Right, 0, GTK_SCROLL_STEP_RIGHT);
681   add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
682                     GTK_SCROLL_PAGE_RIGHT);
683
684   add_move_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
685   add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
686                     GTK_SCROLL_PAGE_UP);
687   add_move_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
688   add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
689                     GTK_SCROLL_PAGE_UP);
690   add_move_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
691   add_move_binding (binding_set, GDK_KP_Page_Up, 0, GTK_SCROLL_PAGE_UP);
692
693   add_move_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
694   add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
695                     GTK_SCROLL_PAGE_DOWN);
696   add_move_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
697   add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
698                     GTK_SCROLL_PAGE_DOWN);
699   add_move_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
700   add_move_binding (binding_set, GDK_KP_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
701
702   add_move_binding (binding_set, GDK_Home, 0, GTK_SCROLL_START);
703   add_move_binding (binding_set, GDK_KP_Home, 0, GTK_SCROLL_START);
704   add_move_binding (binding_set, GDK_End, 0, GTK_SCROLL_END);
705   add_move_binding (binding_set, GDK_KP_End, 0, GTK_SCROLL_END);
706 }
707
708 static GType
709 gtk_xpaned_child_type (GtkContainer * container)
710 {
711   if (!GTK_XPANED (container)->top_left_child ||
712       !GTK_XPANED (container)->top_right_child ||
713       !GTK_XPANED (container)->bottom_left_child ||
714       !GTK_XPANED (container)->bottom_right_child)
715     return GTK_TYPE_WIDGET;
716   else
717     return G_TYPE_NONE;
718 }
719
720 static void
721 gtk_xpaned_init (GtkXPaned * xpaned)
722 {
723   gtk_widget_set_can_focus (GTK_WIDGET (xpaned), TRUE);
724   gtk_widget_set_has_window (GTK_WIDGET (xpaned), FALSE);
725
726   xpaned->top_left_child = NULL;
727   xpaned->top_right_child = NULL;
728   xpaned->bottom_left_child = NULL;
729   xpaned->bottom_right_child = NULL;
730   xpaned->handle_east = NULL;
731   xpaned->handle_west = NULL;
732   xpaned->handle_north = NULL;
733   xpaned->handle_south = NULL;
734   xpaned->handle_middle = NULL;
735   xpaned->cursor_type_east = GDK_SB_V_DOUBLE_ARROW;
736   xpaned->cursor_type_west = GDK_SB_V_DOUBLE_ARROW;
737   xpaned->cursor_type_north = GDK_SB_H_DOUBLE_ARROW;
738   xpaned->cursor_type_south = GDK_SB_H_DOUBLE_ARROW;
739   xpaned->cursor_type_middle = GDK_FLEUR;
740
741   xpaned->handle_pos_east.width = 5;
742   xpaned->handle_pos_east.height = 5;
743   xpaned->handle_pos_west.width = 5;
744   xpaned->handle_pos_west.height = 5;
745   xpaned->handle_pos_north.width = 5;
746   xpaned->handle_pos_north.height = 5;
747   xpaned->handle_pos_south.width = 5;
748   xpaned->handle_pos_south.height = 5;
749   xpaned->handle_pos_middle.width = 5;
750   xpaned->handle_pos_middle.height = 5;
751
752   xpaned->position_set = FALSE;
753   xpaned->last_allocation.width = -1;
754   xpaned->last_allocation.height = -1;
755   xpaned->in_drag_vert = FALSE;
756   xpaned->in_drag_horiz = FALSE;
757   xpaned->in_drag_vert_and_horiz = FALSE;
758
759   xpaned->maximized[GTK_XPANED_TOP_LEFT] = FALSE;
760   xpaned->maximized[GTK_XPANED_TOP_RIGHT] = FALSE;
761   xpaned->maximized[GTK_XPANED_BOTTOM_LEFT] = FALSE;
762   xpaned->maximized[GTK_XPANED_BOTTOM_RIGHT] = FALSE;
763
764   xpaned->priv = g_new0 (GtkXPanedPrivate, 1);
765   xpaned->last_top_left_child_focus = NULL;
766   xpaned->last_top_right_child_focus = NULL;
767   xpaned->last_bottom_left_child_focus = NULL;
768   xpaned->last_bottom_right_child_focus = NULL;
769   xpaned->in_recursion = FALSE;
770   xpaned->handle_prelit = FALSE;
771   xpaned->original_position.x = -1;
772   xpaned->original_position.y = -1;
773   xpaned->unmaximized_position.x = -1;
774   xpaned->unmaximized_position.y = -1;
775
776   xpaned->handle_pos_east.x = -1;
777   xpaned->handle_pos_east.y = -1;
778   xpaned->handle_pos_west.x = -1;
779   xpaned->handle_pos_west.y = -1;
780   xpaned->handle_pos_north.x = -1;
781   xpaned->handle_pos_north.y = -1;
782   xpaned->handle_pos_south.x = -1;
783   xpaned->handle_pos_south.y = -1;
784   xpaned->handle_pos_middle.x = -1;
785   xpaned->handle_pos_middle.y = -1;
786
787   xpaned->drag_pos.x = -1;
788   xpaned->drag_pos.y = -1;
789 }
790
791 void
792 gtk_xpaned_compute_position (GtkXPaned * xpaned,
793                              const GtkAllocation * allocation,
794                              GtkRequisition * top_left_child_req,
795                              GtkRequisition * top_right_child_req,
796                              GtkRequisition * bottom_left_child_req,
797                              GtkRequisition * bottom_right_child_req);
798
799
800 static void
801 gtk_xpaned_size_allocate (GtkWidget * widget, GtkAllocation * allocation)
802 {
803   GtkXPaned *xpaned = GTK_XPANED (widget);
804   gint border_width = gtk_container_get_border_width (GTK_CONTAINER (xpaned));
805   GtkAllocation top_left_child_allocation;
806   GtkAllocation top_right_child_allocation;
807   GtkAllocation bottom_left_child_allocation;
808   GtkAllocation bottom_right_child_allocation;
809   GtkRequisition top_left_child_requisition;
810   GtkRequisition top_right_child_requisition;
811   GtkRequisition bottom_left_child_requisition;
812   GtkRequisition bottom_right_child_requisition;
813   gint handle_size;
814
815   /* determine size of handle(s) */
816   gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
817
818   gtk_widget_set_allocation (widget, allocation);
819
820   if (xpaned->top_left_child
821       && gtk_widget_get_visible (xpaned->top_left_child)
822       && xpaned->top_right_child
823       && gtk_widget_get_visible (xpaned->top_right_child)
824       && xpaned->bottom_left_child
825       && gtk_widget_get_visible (xpaned->bottom_left_child)
826       && xpaned->bottom_right_child
827       && gtk_widget_get_visible (xpaned->bottom_right_child))
828     {
829       /* what sizes do the children want to be at least at */
830       gtk_widget_get_preferred_size (xpaned->top_left_child,
831                                      &top_left_child_requisition, NULL);
832       gtk_widget_get_preferred_size (xpaned->top_right_child,
833                                      &top_right_child_requisition, NULL);
834       gtk_widget_get_preferred_size (xpaned->bottom_left_child,
835                                      &bottom_left_child_requisition, NULL);
836       gtk_widget_get_preferred_size (xpaned->bottom_right_child,
837                                      &bottom_right_child_requisition, NULL);
838
839       /* determine the total requisition-sum of all requisitions of borders,
840        * handles, children etc. */
841       gtk_xpaned_compute_position (xpaned,
842                                    allocation,
843                                    &top_left_child_requisition,
844                                    &top_right_child_requisition,
845                                    &bottom_left_child_requisition,
846                                    &bottom_right_child_requisition);
847
848       /* calculate the current positions and sizes of the handles */
849       xpaned->handle_pos_east.x =
850         allocation->x + border_width +
851         xpaned->top_left_child_size.width + handle_size;
852       xpaned->handle_pos_east.y =
853         allocation->y + border_width +
854         xpaned->top_left_child_size.height;
855       xpaned->handle_pos_east.width =
856         allocation->width - xpaned->top_left_child_size.width -
857         2 * border_width - handle_size;
858       xpaned->handle_pos_east.height = handle_size;
859
860       xpaned->handle_pos_west.x = allocation->x + border_width;
861       xpaned->handle_pos_west.y = xpaned->handle_pos_east.y;
862       xpaned->handle_pos_west.width =
863         allocation->width - xpaned->handle_pos_east.width -
864         2 * border_width - handle_size;
865       xpaned->handle_pos_west.height = handle_size;
866
867       xpaned->handle_pos_north.x = xpaned->handle_pos_east.x - handle_size;
868       xpaned->handle_pos_north.y = allocation->y + border_width;
869       xpaned->handle_pos_north.width = handle_size;
870       xpaned->handle_pos_north.height =
871         xpaned->handle_pos_east.y - allocation->y - border_width;
872
873       xpaned->handle_pos_south.x = xpaned->handle_pos_north.x;
874       xpaned->handle_pos_south.y = xpaned->handle_pos_east.y + handle_size;
875       xpaned->handle_pos_south.width = handle_size;
876       xpaned->handle_pos_south.height =
877         allocation->height - xpaned->handle_pos_north.height -
878         2 * border_width - handle_size;
879
880
881 #define CENTRUM 20
882       xpaned->handle_pos_middle.x = xpaned->handle_pos_north.x;
883       xpaned->handle_pos_middle.y = xpaned->handle_pos_east.y;
884       xpaned->handle_pos_middle.width = handle_size + CENTRUM;
885       xpaned->handle_pos_middle.height = handle_size + CENTRUM;
886
887       /* set allocation for top-left child */
888       top_left_child_allocation.x = allocation->x + border_width;
889       top_left_child_allocation.y = allocation->y + border_width;
890       top_left_child_allocation.width = xpaned->handle_pos_west.width;
891       top_left_child_allocation.height = xpaned->handle_pos_north.height;
892
893       /* set allocation for top-right child */
894       top_right_child_allocation.x =
895         allocation->x + border_width + handle_size +
896         top_left_child_allocation.width;
897       top_right_child_allocation.y = allocation->y + border_width;
898       top_right_child_allocation.width = xpaned->handle_pos_east.width;
899       top_right_child_allocation.height = xpaned->handle_pos_north.height;
900
901       /* set allocation for bottom-left child */
902       bottom_left_child_allocation.x = xpaned->handle_pos_west.x;
903       bottom_left_child_allocation.y = xpaned->handle_pos_south.y;
904       bottom_left_child_allocation.width = xpaned->handle_pos_west.width;
905       bottom_left_child_allocation.height = xpaned->handle_pos_south.height;
906
907       /* set allocation for bottom-right child */
908       bottom_right_child_allocation.x = top_right_child_allocation.x;
909       bottom_right_child_allocation.y = bottom_left_child_allocation.y;
910       bottom_right_child_allocation.width = xpaned->handle_pos_east.width;
911       bottom_right_child_allocation.height = xpaned->handle_pos_south.height;
912
913       if (gtk_widget_get_realized (widget))
914         {
915           if (gtk_widget_get_mapped (widget))
916             {
917               gdk_window_show (xpaned->handle_east);
918               gdk_window_show (xpaned->handle_west);
919               gdk_window_show (xpaned->handle_north);
920               gdk_window_show (xpaned->handle_south);
921               gdk_window_show (xpaned->handle_middle);
922             }
923
924           gdk_window_move_resize (xpaned->handle_east,
925                                   xpaned->handle_pos_east.x,
926                                   xpaned->handle_pos_east.y,
927                                   xpaned->handle_pos_east.width,
928                                   xpaned->handle_pos_east.height);
929
930           gdk_window_move_resize (xpaned->handle_west,
931                                   xpaned->handle_pos_west.x,
932                                   xpaned->handle_pos_west.y,
933                                   xpaned->handle_pos_west.width,
934                                   xpaned->handle_pos_west.height);
935
936           gdk_window_move_resize (xpaned->handle_north,
937                                   xpaned->handle_pos_north.x,
938                                   xpaned->handle_pos_north.y,
939                                   xpaned->handle_pos_north.width,
940                                   xpaned->handle_pos_north.height);
941
942           gdk_window_move_resize (xpaned->handle_south,
943                                   xpaned->handle_pos_south.x,
944                                   xpaned->handle_pos_south.y,
945                                   xpaned->handle_pos_south.width,
946                                   xpaned->handle_pos_south.height);
947
948           gdk_window_move_resize (xpaned->handle_middle,
949                                   xpaned->handle_pos_middle.x,
950                                   xpaned->handle_pos_middle.y,
951                                   xpaned->handle_pos_middle.width,
952                                   xpaned->handle_pos_middle.height);
953         }
954
955       /* Now allocate the childen, making sure, when resizing not to
956        * overlap the windows
957        */
958       if (gtk_widget_get_mapped (widget))
959         {
960           gtk_widget_size_allocate (xpaned->top_right_child,
961                                     &top_right_child_allocation);
962           gtk_widget_size_allocate (xpaned->top_left_child,
963                                     &top_left_child_allocation);
964           gtk_widget_size_allocate (xpaned->bottom_left_child,
965                                     &bottom_left_child_allocation);
966           gtk_widget_size_allocate (xpaned->bottom_right_child,
967                                     &bottom_right_child_allocation);
968         }
969     }
970 }
971
972 static void
973 gtk_xpaned_set_property (GObject * object,
974                          guint prop_id,
975                          const GValue * value, GParamSpec * pspec)
976 {
977   GtkXPaned *xpaned = GTK_XPANED (object);
978
979   switch (prop_id)
980     {
981     case PROP_X_POSITION:
982       gtk_xpaned_set_position_x (xpaned, g_value_get_int (value));
983       break;
984
985     case PROP_Y_POSITION:
986       gtk_xpaned_set_position_y (xpaned, g_value_get_int (value));
987       break;
988
989     case PROP_POSITION_SET:
990       xpaned->position_set = g_value_get_boolean (value);
991       gtk_widget_queue_resize (GTK_WIDGET (xpaned));
992       break;
993
994     default:
995       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
996       break;
997     }
998 }
999
1000 static void
1001 gtk_xpaned_get_property (GObject * object,
1002                          guint prop_id, GValue * value, GParamSpec * pspec)
1003 {
1004   GtkXPaned *xpaned = GTK_XPANED (object);
1005
1006   switch (prop_id)
1007     {
1008     case PROP_X_POSITION:
1009       g_value_set_int (value, xpaned->top_left_child_size.width);
1010       break;
1011
1012     case PROP_Y_POSITION:
1013       g_value_set_int (value, xpaned->top_left_child_size.height);
1014       break;
1015
1016     case PROP_POSITION_SET:
1017       g_value_set_boolean (value, xpaned->position_set);
1018       break;
1019
1020     case PROP_MIN_X_POSITION:
1021       g_value_set_int (value, xpaned->min_position.x);
1022       break;
1023
1024     case PROP_MIN_Y_POSITION:
1025       g_value_set_int (value, xpaned->min_position.y);
1026       break;
1027
1028     case PROP_MAX_X_POSITION:
1029       g_value_set_int (value, xpaned->max_position.x);
1030       break;
1031
1032     case PROP_MAX_Y_POSITION:
1033       g_value_set_int (value, xpaned->max_position.y);
1034       break;
1035
1036     default:
1037       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1038       break;
1039     }
1040 }
1041
1042 static void
1043 gtk_xpaned_set_child_property (GtkContainer * container,
1044                                GtkWidget * child,
1045                                guint property_id,
1046                                const GValue * value, GParamSpec * pspec)
1047 {
1048   GtkXPaned *xpaned = GTK_XPANED (container);
1049   gboolean old_value = FALSE;
1050   gboolean new_value = FALSE;
1051
1052   g_assert (child == xpaned->top_left_child ||
1053             child == xpaned->top_right_child ||
1054             child == xpaned->bottom_left_child ||
1055             child == xpaned->bottom_right_child);
1056
1057   new_value = g_value_get_boolean (value);
1058
1059   switch (property_id)
1060     {
1061     case CHILD_PROP_RESIZE:
1062       if (child == xpaned->top_left_child)
1063         {
1064           old_value = xpaned->top_left_child_resize;
1065           xpaned->top_left_child_resize = new_value;
1066         }
1067       else if (child == xpaned->top_right_child)
1068         {
1069           old_value = xpaned->top_right_child_resize;
1070           xpaned->top_right_child_resize = new_value;
1071         }
1072       else if (child == xpaned->bottom_left_child)
1073         {
1074           old_value = xpaned->bottom_left_child_resize;
1075           xpaned->bottom_left_child_resize = new_value;
1076         }
1077       else if (child == xpaned->bottom_right_child)
1078         {
1079           old_value = xpaned->bottom_right_child_resize;
1080           xpaned->bottom_right_child_resize = new_value;
1081         }
1082       break;
1083
1084     case CHILD_PROP_SHRINK:
1085       if (child == xpaned->top_left_child)
1086         {
1087           old_value = xpaned->top_left_child_shrink;
1088           xpaned->top_left_child_shrink = new_value;
1089         }
1090       else if (child == xpaned->top_right_child)
1091         {
1092           old_value = xpaned->top_right_child_shrink;
1093           xpaned->top_right_child_shrink = new_value;
1094         }
1095       else if (child == xpaned->bottom_left_child)
1096         {
1097           old_value = xpaned->bottom_left_child_shrink;
1098           xpaned->bottom_left_child_shrink = new_value;
1099         }
1100       else if (child == xpaned->bottom_right_child)
1101         {
1102           old_value = xpaned->bottom_right_child_shrink;
1103           xpaned->bottom_right_child_shrink = new_value;
1104         }
1105       break;
1106
1107     default:
1108       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container,
1109                                                     property_id, pspec);
1110       old_value = -1;           /* quiet gcc */
1111       break;
1112     }
1113
1114   if (old_value != new_value)
1115     gtk_widget_queue_resize (GTK_WIDGET (container));
1116 }
1117
1118 static void
1119 gtk_xpaned_get_child_property (GtkContainer * container,
1120                                GtkWidget * child,
1121                                guint property_id,
1122                                GValue * value, GParamSpec * pspec)
1123 {
1124   GtkXPaned *xpaned = GTK_XPANED (container);
1125
1126   g_assert (child == xpaned->top_left_child ||
1127             child == xpaned->top_right_child ||
1128             child == xpaned->bottom_left_child ||
1129             child == xpaned->bottom_right_child);
1130
1131   switch (property_id)
1132     {
1133     case CHILD_PROP_RESIZE:
1134       if (child == xpaned->top_left_child)
1135         g_value_set_boolean (value, xpaned->top_left_child_resize);
1136       else if (child == xpaned->top_right_child)
1137         g_value_set_boolean (value, xpaned->top_right_child_resize);
1138       else if (child == xpaned->bottom_left_child)
1139         g_value_set_boolean (value, xpaned->bottom_left_child_resize);
1140       else if (child == xpaned->bottom_right_child)
1141         g_value_set_boolean (value, xpaned->bottom_right_child_resize);
1142       break;
1143
1144     case CHILD_PROP_SHRINK:
1145       if (child == xpaned->top_left_child)
1146         g_value_set_boolean (value, xpaned->top_left_child_shrink);
1147       else if (child == xpaned->top_right_child)
1148         g_value_set_boolean (value, xpaned->top_right_child_shrink);
1149       else if (child == xpaned->bottom_left_child)
1150         g_value_set_boolean (value, xpaned->bottom_left_child_shrink);
1151       else if (child == xpaned->bottom_right_child)
1152         g_value_set_boolean (value, xpaned->bottom_right_child_shrink);
1153       break;
1154
1155     default:
1156       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container,
1157                                                     property_id, pspec);
1158       break;
1159     }
1160 }
1161
1162 static void
1163 gtk_xpaned_finalize (GObject * object)
1164 {
1165   GtkXPaned *xpaned = GTK_XPANED (object);
1166
1167   gtk_xpaned_set_saved_focus (xpaned, NULL);
1168   gtk_xpaned_set_first_xpaned (xpaned, NULL);
1169
1170   g_free (xpaned->priv);
1171
1172   G_OBJECT_CLASS (parent_class)->finalize (object);
1173 }
1174
1175 static void
1176 gtk_xpaned_realize (GtkWidget * widget)
1177 {
1178   GtkXPaned *xpaned;
1179   GdkWindowAttr attributes_east;
1180   GdkWindowAttr attributes_west;
1181   GdkWindowAttr attributes_north;
1182   GdkWindowAttr attributes_south;
1183   GdkWindowAttr attributes_middle;
1184   gint attributes_mask_east;
1185   gint attributes_mask_west;
1186   gint attributes_mask_north;
1187   gint attributes_mask_south;
1188   gint attributes_mask_middle;
1189
1190   gtk_widget_set_realized (widget, TRUE);
1191   xpaned = GTK_XPANED (widget);
1192
1193   gtk_widget_set_window (widget, gtk_widget_get_parent_window (widget));
1194   //  g_object_ref (widget->window);
1195
1196   attributes_east.window_type = GDK_WINDOW_CHILD;
1197   attributes_west.window_type = GDK_WINDOW_CHILD;
1198   attributes_north.window_type = GDK_WINDOW_CHILD;
1199   attributes_south.window_type = GDK_WINDOW_CHILD;
1200   attributes_middle.window_type = GDK_WINDOW_CHILD;
1201
1202   attributes_east.wclass = GDK_INPUT_ONLY;
1203   attributes_west.wclass = GDK_INPUT_ONLY;
1204   attributes_north.wclass = GDK_INPUT_ONLY;
1205   attributes_south.wclass = GDK_INPUT_ONLY;
1206   attributes_middle.wclass = GDK_INPUT_ONLY;
1207
1208   attributes_east.x = xpaned->handle_pos_east.x;
1209   attributes_east.y = xpaned->handle_pos_east.y;
1210   attributes_east.width = xpaned->handle_pos_east.width;
1211   attributes_east.height = xpaned->handle_pos_east.height;
1212
1213   attributes_west.x = xpaned->handle_pos_west.x;
1214   attributes_west.y = xpaned->handle_pos_west.y;
1215   attributes_west.width = xpaned->handle_pos_west.width;
1216   attributes_west.height = xpaned->handle_pos_west.height;
1217
1218   attributes_north.x = xpaned->handle_pos_north.x;
1219   attributes_north.y = xpaned->handle_pos_north.y;
1220   attributes_north.width = xpaned->handle_pos_north.width;
1221   attributes_north.height = xpaned->handle_pos_north.height;
1222
1223   attributes_south.x = xpaned->handle_pos_south.x;
1224   attributes_south.y = xpaned->handle_pos_south.y;
1225   attributes_south.width = xpaned->handle_pos_south.width;
1226   attributes_south.height = xpaned->handle_pos_south.height;
1227
1228   attributes_middle.x = xpaned->handle_pos_middle.x;
1229   attributes_middle.y = xpaned->handle_pos_middle.y;
1230   attributes_middle.width = xpaned->handle_pos_middle.width;
1231   attributes_middle.height = xpaned->handle_pos_middle.height;
1232
1233   attributes_east.cursor =
1234     gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1235                                 xpaned->cursor_type_east);
1236   attributes_west.cursor =
1237     gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1238                                 xpaned->cursor_type_west);
1239   attributes_north.cursor =
1240     gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1241                                 xpaned->cursor_type_north);
1242   attributes_south.cursor =
1243     gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1244                                 xpaned->cursor_type_south);
1245   attributes_middle.cursor =
1246     gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1247                                 xpaned->cursor_type_middle);
1248
1249   attributes_east.event_mask = gtk_widget_get_events (widget);
1250   attributes_west.event_mask = gtk_widget_get_events (widget);
1251   attributes_north.event_mask = gtk_widget_get_events (widget);
1252   attributes_south.event_mask = gtk_widget_get_events (widget);
1253   attributes_middle.event_mask = gtk_widget_get_events (widget);
1254
1255   attributes_east.event_mask |= (GDK_BUTTON_PRESS_MASK |
1256                                  GDK_BUTTON_RELEASE_MASK |
1257                                  GDK_ENTER_NOTIFY_MASK |
1258                                  GDK_LEAVE_NOTIFY_MASK |
1259                                  GDK_POINTER_MOTION_MASK |
1260                                  GDK_POINTER_MOTION_HINT_MASK);
1261   attributes_west.event_mask |= (GDK_BUTTON_PRESS_MASK |
1262                                  GDK_BUTTON_RELEASE_MASK |
1263                                  GDK_ENTER_NOTIFY_MASK |
1264                                  GDK_LEAVE_NOTIFY_MASK |
1265                                  GDK_POINTER_MOTION_MASK |
1266                                  GDK_POINTER_MOTION_HINT_MASK);
1267   attributes_north.event_mask |= (GDK_BUTTON_PRESS_MASK |
1268                                   GDK_BUTTON_RELEASE_MASK |
1269                                   GDK_ENTER_NOTIFY_MASK |
1270                                   GDK_LEAVE_NOTIFY_MASK |
1271                                   GDK_POINTER_MOTION_MASK |
1272                                   GDK_POINTER_MOTION_HINT_MASK);
1273   attributes_south.event_mask |= (GDK_BUTTON_PRESS_MASK |
1274                                   GDK_BUTTON_RELEASE_MASK |
1275                                   GDK_ENTER_NOTIFY_MASK |
1276                                   GDK_LEAVE_NOTIFY_MASK |
1277                                   GDK_POINTER_MOTION_MASK |
1278                                   GDK_POINTER_MOTION_HINT_MASK);
1279   attributes_middle.event_mask |= (GDK_BUTTON_PRESS_MASK |
1280                                    GDK_BUTTON_RELEASE_MASK |
1281                                    GDK_ENTER_NOTIFY_MASK |
1282                                    GDK_LEAVE_NOTIFY_MASK |
1283                                    GDK_POINTER_MOTION_MASK |
1284                                    GDK_POINTER_MOTION_HINT_MASK);
1285
1286   attributes_mask_east = GDK_WA_X | GDK_WA_Y | GDK_WA_CURSOR;
1287   attributes_mask_west = GDK_WA_X | GDK_WA_Y | GDK_WA_CURSOR;
1288   attributes_mask_north = GDK_WA_X | GDK_WA_Y | GDK_WA_CURSOR;
1289   attributes_mask_south = GDK_WA_X | GDK_WA_Y | GDK_WA_CURSOR;
1290   attributes_mask_middle = GDK_WA_X | GDK_WA_Y | GDK_WA_CURSOR;
1291
1292   xpaned->handle_east = gdk_window_new (gtk_widget_get_window (widget),
1293                                         &attributes_east,
1294                                         attributes_mask_east);
1295   xpaned->handle_west = gdk_window_new (gtk_widget_get_window (widget),
1296                                         &attributes_west,
1297                                         attributes_mask_west);
1298   xpaned->handle_north = gdk_window_new (gtk_widget_get_window (widget),
1299                                          &attributes_north,
1300                                          attributes_mask_north);
1301   xpaned->handle_south = gdk_window_new (gtk_widget_get_window (widget),
1302                                          &attributes_south,
1303                                          attributes_mask_south);
1304   xpaned->handle_middle = gdk_window_new (gtk_widget_get_window (widget),
1305                                           &attributes_middle,
1306                                           attributes_mask_middle);
1307
1308   gdk_window_set_user_data (xpaned->handle_east, xpaned);
1309   gdk_window_set_user_data (xpaned->handle_west, xpaned);
1310   gdk_window_set_user_data (xpaned->handle_north, xpaned);
1311   gdk_window_set_user_data (xpaned->handle_south, xpaned);
1312   gdk_window_set_user_data (xpaned->handle_middle, xpaned);
1313
1314   g_object_unref (attributes_east.cursor);
1315   g_object_unref (attributes_west.cursor);
1316   g_object_unref (attributes_north.cursor);
1317   g_object_unref (attributes_south.cursor);
1318   g_object_unref (attributes_middle.cursor);
1319
1320   if (xpaned->top_left_child
1321       && gtk_widget_get_visible (xpaned->top_left_child)
1322       && xpaned->top_right_child
1323       && gtk_widget_get_visible (xpaned->top_right_child)
1324       && xpaned->bottom_left_child
1325       && gtk_widget_get_visible (xpaned->bottom_left_child)
1326       && xpaned->bottom_right_child
1327       && gtk_widget_get_visible (xpaned->bottom_right_child))
1328     {
1329       gdk_window_show (xpaned->handle_east);
1330       gdk_window_show (xpaned->handle_west);
1331       gdk_window_show (xpaned->handle_north);
1332       gdk_window_show (xpaned->handle_south);
1333       gdk_window_show (xpaned->handle_middle);
1334     }
1335 }
1336
1337 static void
1338 gtk_xpaned_unrealize (GtkWidget * widget)
1339 {
1340   GtkXPaned *xpaned = GTK_XPANED (widget);
1341
1342   if (xpaned->handle_east)
1343     {
1344       gdk_window_set_user_data (xpaned->handle_east, NULL);
1345       gdk_window_destroy (xpaned->handle_east);
1346       xpaned->handle_east = NULL;
1347     }
1348
1349   if (xpaned->handle_west)
1350     {
1351       gdk_window_set_user_data (xpaned->handle_west, NULL);
1352       gdk_window_destroy (xpaned->handle_west);
1353       xpaned->handle_west = NULL;
1354     }
1355
1356   if (xpaned->handle_north)
1357     {
1358       gdk_window_set_user_data (xpaned->handle_north, NULL);
1359       gdk_window_destroy (xpaned->handle_north);
1360       xpaned->handle_north = NULL;
1361     }
1362
1363   if (xpaned->handle_south)
1364     {
1365       gdk_window_set_user_data (xpaned->handle_south, NULL);
1366       gdk_window_destroy (xpaned->handle_south);
1367       xpaned->handle_south = NULL;
1368     }
1369
1370   if (xpaned->handle_middle)
1371     {
1372       gdk_window_set_user_data (xpaned->handle_middle, NULL);
1373       gdk_window_destroy (xpaned->handle_middle);
1374       xpaned->handle_middle = NULL;
1375     }
1376
1377   gtk_xpaned_set_last_top_left_child_focus (xpaned, NULL);
1378   gtk_xpaned_set_last_top_right_child_focus (xpaned, NULL);
1379   gtk_xpaned_set_last_bottom_left_child_focus (xpaned, NULL);
1380   gtk_xpaned_set_last_bottom_right_child_focus (xpaned, NULL);
1381   gtk_xpaned_set_saved_focus (xpaned, NULL);
1382   gtk_xpaned_set_first_xpaned (xpaned, NULL);
1383
1384   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1385     (*GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1386 }
1387
1388 static void
1389 gtk_xpaned_map (GtkWidget * widget)
1390 {
1391   GtkXPaned *xpaned = GTK_XPANED (widget);
1392
1393   gdk_window_show (xpaned->handle_east);
1394   gdk_window_show (xpaned->handle_west);
1395   gdk_window_show (xpaned->handle_north);
1396   gdk_window_show (xpaned->handle_south);
1397   gdk_window_show (xpaned->handle_middle);
1398
1399   GTK_WIDGET_CLASS (parent_class)->map (widget);
1400 }
1401
1402 static void
1403 gtk_xpaned_unmap (GtkWidget * widget)
1404 {
1405   GtkXPaned *xpaned = GTK_XPANED (widget);
1406
1407   gdk_window_hide (xpaned->handle_east);
1408   gdk_window_hide (xpaned->handle_west);
1409   gdk_window_hide (xpaned->handle_north);
1410   gdk_window_hide (xpaned->handle_south);
1411   gdk_window_hide (xpaned->handle_middle);
1412
1413   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
1414 }
1415
1416 static gboolean
1417 gtk_xpaned_draw (GtkWidget * widget, cairo_t *cr)
1418 {
1419   GtkXPaned *xpaned = GTK_XPANED (widget);
1420   gint handle_size;
1421
1422   /* determine size of handle(s) */
1423   gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
1424
1425   /* I want the handle-"thickness" to be at least 3 */
1426   g_assert (handle_size >= 3);
1427
1428   if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
1429       xpaned->top_left_child
1430       && gtk_widget_get_visible (xpaned->top_left_child)
1431       && xpaned->top_right_child
1432       && gtk_widget_get_visible (xpaned->top_right_child)
1433       && xpaned->bottom_left_child
1434       && gtk_widget_get_visible (xpaned->bottom_left_child)
1435       && xpaned->bottom_right_child
1436       && gtk_widget_get_visible (xpaned->bottom_right_child))
1437     {
1438       GtkStyleContext *context;
1439
1440       context = gtk_widget_get_style_context (widget);
1441       gtk_render_handle (context, cr,
1442                          xpaned->handle_pos_east.x - handle_size - 256 / 2,
1443                          xpaned->handle_pos_west.y + 1,
1444                          256 + handle_size, handle_size - 2);
1445
1446       gtk_render_handle (context, cr,
1447                          xpaned->handle_pos_north.x + 1,
1448                          xpaned->handle_pos_south.y - handle_size - 256 / 2,
1449                          handle_size - 2, 256 + handle_size);
1450     }
1451
1452   /* Chain up to draw children */
1453   GTK_WIDGET_CLASS (parent_class)->draw (widget, cr);
1454
1455   return FALSE;
1456 }
1457
1458 static gboolean
1459 is_rtl (GtkXPaned * xpaned)
1460 {
1461   if (gtk_widget_get_direction (GTK_WIDGET (xpaned)) == GTK_TEXT_DIR_RTL)
1462     return TRUE;
1463
1464   return FALSE;
1465 }
1466
1467 static void
1468 update_drag (GtkXPaned * xpaned)
1469 {
1470   GdkPoint pos;
1471   GtkWidget *widget = GTK_WIDGET (xpaned);
1472   gint handle_size;
1473   GtkRequisition size;
1474   GtkAllocation allocation;
1475
1476   gtk_widget_get_allocation (widget, &allocation);
1477
1478   gdk_window_get_device_position (gtk_widget_get_window (widget),
1479                                   gdk_device_manager_get_client_pointer (
1480                                     gdk_display_get_device_manager (
1481                                       gtk_widget_get_display (widget))),
1482                                   &pos.x, &pos.y, NULL);
1483
1484   if (xpaned->in_drag_vert)
1485     {
1486       pos.y -= xpaned->drag_pos.y;
1487
1488       if (is_rtl (xpaned))
1489         {
1490           gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
1491
1492           size.height = allocation.height - pos.y - handle_size;
1493         }
1494       else
1495         {
1496           size.height = pos.y;
1497         }
1498
1499       size.height -= gtk_container_get_border_width (GTK_CONTAINER (xpaned));
1500
1501       size.height =
1502         CLAMP (size.height, xpaned->min_position.y, xpaned->max_position.y);
1503
1504       if (size.height != xpaned->top_left_child_size.height)
1505         gtk_xpaned_set_position_y (xpaned, size.height);
1506     }
1507
1508   if (xpaned->in_drag_horiz)
1509     {
1510       pos.x -= xpaned->drag_pos.x;
1511
1512       if (is_rtl (xpaned))
1513         {
1514           gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
1515
1516           size.width = allocation.width - pos.x - handle_size;
1517         }
1518       else
1519         {
1520           size.width = pos.x;
1521         }
1522
1523       size.width -= gtk_container_get_border_width (GTK_CONTAINER (xpaned));
1524
1525       size.width =
1526         CLAMP (size.width, xpaned->min_position.x, xpaned->max_position.x);
1527
1528       if (size.width != xpaned->top_left_child_size.width)
1529         gtk_xpaned_set_position_x (xpaned, size.width);
1530     }
1531
1532   if (xpaned->in_drag_vert_and_horiz)
1533     {
1534       pos.x -= xpaned->drag_pos.x;
1535       pos.y -= xpaned->drag_pos.y;
1536
1537       if (is_rtl (xpaned))
1538         {
1539           gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
1540
1541           size.width = allocation.width - pos.x - handle_size;
1542           size.height = allocation.height - pos.y - handle_size;
1543         }
1544       else
1545         {
1546           size.width = pos.x;
1547           size.height = pos.y;
1548         }
1549
1550       size.width -= gtk_container_get_border_width (GTK_CONTAINER (xpaned));
1551       size.height -= gtk_container_get_border_width (GTK_CONTAINER (xpaned));
1552
1553       size.width =
1554         CLAMP (size.width, xpaned->min_position.x, xpaned->max_position.x);
1555       size.height =
1556         CLAMP (size.height, xpaned->min_position.y, xpaned->max_position.y);
1557
1558       if (size.width != xpaned->top_left_child_size.width)
1559         gtk_xpaned_set_position_x (xpaned, size.width);
1560
1561       if (size.height != xpaned->top_left_child_size.height)
1562         gtk_xpaned_set_position_y (xpaned, size.height);
1563     }
1564 }
1565
1566 static gboolean
1567 gtk_xpaned_enter (GtkWidget * widget, GdkEventCrossing * event)
1568 {
1569   GtkXPaned *xpaned = GTK_XPANED (widget);
1570
1571   if (xpaned->in_drag_vert ||
1572       xpaned->in_drag_horiz || xpaned->in_drag_vert_and_horiz)
1573     update_drag (xpaned);
1574   else
1575     {
1576       xpaned->handle_prelit = TRUE;
1577
1578       gtk_widget_queue_draw_area (widget,
1579                                   xpaned->handle_pos_east.x,
1580                                   xpaned->handle_pos_east.y,
1581                                   xpaned->handle_pos_east.width,
1582                                   xpaned->handle_pos_east.height);
1583
1584       gtk_widget_queue_draw_area (widget,
1585                                   xpaned->handle_pos_west.x,
1586                                   xpaned->handle_pos_west.y,
1587                                   xpaned->handle_pos_west.width,
1588                                   xpaned->handle_pos_west.height);
1589
1590       gtk_widget_queue_draw_area (widget,
1591                                   xpaned->handle_pos_north.x,
1592                                   xpaned->handle_pos_north.y,
1593                                   xpaned->handle_pos_north.width,
1594                                   xpaned->handle_pos_north.height);
1595
1596       gtk_widget_queue_draw_area (widget,
1597                                   xpaned->handle_pos_south.x,
1598                                   xpaned->handle_pos_south.y,
1599                                   xpaned->handle_pos_south.width,
1600                                   xpaned->handle_pos_south.height);
1601
1602       gtk_widget_queue_draw_area (widget,
1603                                   xpaned->handle_pos_middle.x,
1604                                   xpaned->handle_pos_middle.y,
1605                                   xpaned->handle_pos_middle.width,
1606                                   xpaned->handle_pos_middle.height);
1607     }
1608
1609   return TRUE;
1610 }
1611
1612 static gboolean
1613 gtk_xpaned_leave (GtkWidget * widget, GdkEventCrossing * event)
1614 {
1615   GtkXPaned *xpaned = GTK_XPANED (widget);
1616
1617   if (xpaned->in_drag_vert ||
1618       xpaned->in_drag_horiz || xpaned->in_drag_vert_and_horiz)
1619     update_drag (xpaned);
1620   else
1621     {
1622       xpaned->handle_prelit = FALSE;
1623
1624       gtk_widget_queue_draw_area (widget,
1625                                   xpaned->handle_pos_east.x,
1626                                   xpaned->handle_pos_east.y,
1627                                   xpaned->handle_pos_east.width,
1628                                   xpaned->handle_pos_east.height);
1629
1630       gtk_widget_queue_draw_area (widget,
1631                                   xpaned->handle_pos_west.x,
1632                                   xpaned->handle_pos_west.y,
1633                                   xpaned->handle_pos_west.width,
1634                                   xpaned->handle_pos_west.height);
1635
1636       gtk_widget_queue_draw_area (widget,
1637                                   xpaned->handle_pos_north.x,
1638                                   xpaned->handle_pos_north.y,
1639                                   xpaned->handle_pos_north.width,
1640                                   xpaned->handle_pos_north.height);
1641
1642       gtk_widget_queue_draw_area (widget,
1643                                   xpaned->handle_pos_south.x,
1644                                   xpaned->handle_pos_south.y,
1645                                   xpaned->handle_pos_south.width,
1646                                   xpaned->handle_pos_south.height);
1647
1648       gtk_widget_queue_draw_area (widget,
1649                                   xpaned->handle_pos_middle.x,
1650                                   xpaned->handle_pos_middle.y,
1651                                   xpaned->handle_pos_middle.width,
1652                                   xpaned->handle_pos_middle.height);
1653     }
1654
1655   return TRUE;
1656 }
1657
1658 static gboolean
1659 gtk_xpaned_focus (GtkWidget * widget, GtkDirectionType direction)
1660 {
1661   gboolean retval;
1662
1663   /* This is a hack, but how can this be done without
1664    * excessive cut-and-paste from gtkcontainer.c?
1665    */
1666
1667   gtk_widget_set_can_focus (GTK_WIDGET (widget), FALSE);
1668   retval = (*GTK_WIDGET_CLASS (parent_class)->focus) (widget, direction);
1669   gtk_widget_set_can_focus (GTK_WIDGET (widget), TRUE);
1670
1671   return retval;
1672 }
1673
1674 static gboolean
1675 gtk_xpaned_button_press (GtkWidget * widget, GdkEventButton * event)
1676 {
1677   GtkXPaned *xpaned = GTK_XPANED (widget);
1678
1679   /* if any child is currently maximized, jump right back */
1680   if (xpaned->maximized[GTK_XPANED_TOP_LEFT] ||
1681       xpaned->maximized[GTK_XPANED_TOP_RIGHT] ||
1682       xpaned->maximized[GTK_XPANED_BOTTOM_LEFT] ||
1683       xpaned->maximized[GTK_XPANED_BOTTOM_RIGHT])
1684     return FALSE;
1685
1686   /* if user is dragging the handles around */
1687   if (!xpaned->in_drag_vert_and_horiz &&
1688       event->window != xpaned->handle_east &&
1689       event->window != xpaned->handle_west &&
1690       event->window != xpaned->handle_north &&
1691       event->window != xpaned->handle_south &&
1692       event->window == xpaned->handle_middle && event->button == 1)
1693     {
1694       xpaned->in_drag_vert_and_horiz = TRUE;
1695
1696       /* We need a server grab here, not gtk_grab_add(), since
1697        * we don't want to pass events on to the widget's children */
1698       if (gdk_pointer_grab (xpaned->handle_middle,
1699                             FALSE,
1700                             GDK_POINTER_MOTION_HINT_MASK
1701                             | GDK_BUTTON1_MOTION_MASK
1702                             | GDK_BUTTON_RELEASE_MASK
1703                             | GDK_ENTER_NOTIFY_MASK
1704                             | GDK_LEAVE_NOTIFY_MASK,
1705                             NULL, NULL, event->time) == GDK_GRAB_SUCCESS)
1706         {
1707         }
1708
1709       xpaned->drag_pos.x = event->x;
1710       xpaned->drag_pos.y = event->y;
1711
1712       return TRUE;
1713     }
1714   else if (!xpaned->in_drag_vert &&
1715            event->window == xpaned->handle_east &&
1716            event->window != xpaned->handle_west &&
1717            event->window != xpaned->handle_north &&
1718            event->window != xpaned->handle_south &&
1719            event->window != xpaned->handle_middle && event->button == 1)
1720     {
1721       xpaned->in_drag_vert = TRUE;
1722
1723       /* We need a server grab here, not gtk_grab_add(), since
1724        * we don't want to pass events on to the widget's children */
1725       if (gdk_pointer_grab (xpaned->handle_east,
1726                             FALSE,
1727                             GDK_POINTER_MOTION_HINT_MASK
1728                             | GDK_BUTTON1_MOTION_MASK
1729                             | GDK_BUTTON_RELEASE_MASK
1730                             | GDK_ENTER_NOTIFY_MASK
1731                             | GDK_LEAVE_NOTIFY_MASK,
1732                             NULL, NULL, event->time) == GDK_GRAB_SUCCESS)
1733         {
1734         }
1735
1736       xpaned->drag_pos.y = event->y;
1737
1738       return TRUE;
1739     }
1740   else if (!xpaned->in_drag_vert &&
1741            event->window != xpaned->handle_east &&
1742            event->window == xpaned->handle_west &&
1743            event->window != xpaned->handle_north &&
1744            event->window != xpaned->handle_south &&
1745            event->window != xpaned->handle_middle && event->button == 1)
1746     {
1747       xpaned->in_drag_vert = TRUE;
1748
1749       /* We need a server grab here, not gtk_grab_add(), since
1750        * we don't want to pass events on to the widget's children */
1751       if (gdk_pointer_grab (xpaned->handle_west,
1752                             FALSE,
1753                             GDK_POINTER_MOTION_HINT_MASK
1754                             | GDK_BUTTON1_MOTION_MASK
1755                             | GDK_BUTTON_RELEASE_MASK
1756                             | GDK_ENTER_NOTIFY_MASK
1757                             | GDK_LEAVE_NOTIFY_MASK,
1758                             NULL, NULL, event->time) == GDK_GRAB_SUCCESS)
1759         {
1760         }
1761
1762       xpaned->drag_pos.y = event->y;
1763
1764       return TRUE;
1765     }
1766   else if (!xpaned->in_drag_horiz &&
1767            event->window != xpaned->handle_east &&
1768            event->window != xpaned->handle_west &&
1769            event->window == xpaned->handle_north &&
1770            event->window != xpaned->handle_south &&
1771            event->window != xpaned->handle_middle && event->button == 1)
1772     {
1773       xpaned->in_drag_horiz = TRUE;
1774
1775       /* We need a server grab here, not gtk_grab_add(), since
1776        * we don't want to pass events on to the widget's children */
1777       if (gdk_pointer_grab (xpaned->handle_north,
1778                             FALSE,
1779                             GDK_POINTER_MOTION_HINT_MASK
1780                             | GDK_BUTTON1_MOTION_MASK
1781                             | GDK_BUTTON_RELEASE_MASK
1782                             | GDK_ENTER_NOTIFY_MASK
1783                             | GDK_LEAVE_NOTIFY_MASK,
1784                             NULL, NULL, event->time) == GDK_GRAB_SUCCESS)
1785         {
1786         }
1787
1788       xpaned->drag_pos.x = event->x;
1789
1790       return TRUE;
1791     }
1792   else if (!xpaned->in_drag_horiz &&
1793            event->window != xpaned->handle_east &&
1794            event->window != xpaned->handle_west &&
1795            event->window != xpaned->handle_north &&
1796            event->window == xpaned->handle_south &&
1797            event->window != xpaned->handle_middle && event->button == 1)
1798     {
1799       xpaned->in_drag_horiz = TRUE;
1800
1801       /* We need a server grab here, not gtk_grab_add(), since
1802        * we don't want to pass events on to the widget's children */
1803       if (gdk_pointer_grab (xpaned->handle_south,
1804                             FALSE,
1805                             GDK_POINTER_MOTION_HINT_MASK
1806                             | GDK_BUTTON1_MOTION_MASK
1807                             | GDK_BUTTON_RELEASE_MASK
1808                             | GDK_ENTER_NOTIFY_MASK
1809                             | GDK_LEAVE_NOTIFY_MASK,
1810                             NULL, NULL, event->time) == GDK_GRAB_SUCCESS)
1811         {
1812         }
1813
1814       xpaned->drag_pos.x = event->x;
1815
1816       return TRUE;
1817     }
1818   return FALSE;
1819 }
1820
1821 static gboolean
1822 gtk_xpaned_button_release (GtkWidget * widget, GdkEventButton * event)
1823 {
1824   GtkXPaned *xpaned = GTK_XPANED (widget);
1825
1826   if (xpaned->in_drag_vert && (event->button == 1))
1827     {
1828       xpaned->in_drag_vert = FALSE;
1829       xpaned->drag_pos.y = -1;
1830       xpaned->position_set = TRUE;
1831       gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
1832                                   event->time);
1833       return TRUE;
1834     }
1835   else if (xpaned->in_drag_horiz && (event->button == 1))
1836     {
1837       xpaned->in_drag_horiz = FALSE;
1838       xpaned->drag_pos.x = -1;
1839       xpaned->position_set = TRUE;
1840       gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
1841                                   event->time);
1842       return TRUE;
1843     }
1844   else if (xpaned->in_drag_vert_and_horiz && (event->button == 1))
1845     {
1846       xpaned->in_drag_vert_and_horiz = FALSE;
1847       xpaned->drag_pos.x = -1;
1848       xpaned->drag_pos.y = -1;
1849       xpaned->position_set = TRUE;
1850       gdk_display_pointer_ungrab (gtk_widget_get_display (widget),
1851                                   event->time);
1852       return TRUE;
1853     }
1854
1855   return FALSE;
1856 }
1857
1858 static gboolean
1859 gtk_xpaned_motion (GtkWidget * widget, GdkEventMotion * event)
1860 {
1861   GtkXPaned *xpaned = GTK_XPANED (widget);
1862
1863   if (xpaned->in_drag_vert ||
1864       xpaned->in_drag_horiz || xpaned->in_drag_vert_and_horiz)
1865
1866     {
1867       update_drag (xpaned);
1868       return TRUE;
1869     }
1870
1871   return FALSE;
1872 }
1873
1874 void
1875 gtk_xpaned_add_top_left (GtkXPaned * xpaned, GtkWidget * widget)
1876 {
1877   gtk_xpaned_pack_top_left (xpaned, widget, FALSE, TRUE);
1878 }
1879
1880 void
1881 gtk_xpaned_add_top_right (GtkXPaned * xpaned, GtkWidget * widget)
1882 {
1883   gtk_xpaned_pack_top_right (xpaned, widget, FALSE, TRUE);
1884 }
1885
1886 void
1887 gtk_xpaned_add_bottom_left (GtkXPaned * xpaned, GtkWidget * widget)
1888 {
1889   gtk_xpaned_pack_bottom_left (xpaned, widget, FALSE, TRUE);
1890 }
1891
1892 void
1893 gtk_xpaned_add_bottom_right (GtkXPaned * xpaned, GtkWidget * widget)
1894 {
1895   gtk_xpaned_pack_bottom_right (xpaned, widget, FALSE, TRUE);
1896 }
1897
1898 void
1899 gtk_xpaned_pack_top_left (GtkXPaned * xpaned,
1900                           GtkWidget * child, gboolean resize, gboolean shrink)
1901 {
1902   g_return_if_fail (GTK_IS_XPANED (xpaned));
1903   g_return_if_fail (GTK_IS_WIDGET (child));
1904
1905   if (!xpaned->top_left_child)
1906     {
1907       xpaned->top_left_child = child;
1908       xpaned->top_left_child_resize = resize;
1909       xpaned->top_left_child_shrink = shrink;
1910
1911       gtk_widget_set_parent (child, GTK_WIDGET (xpaned));
1912     }
1913 }
1914
1915 void
1916 gtk_xpaned_pack_top_right (GtkXPaned * xpaned,
1917                            GtkWidget * child,
1918                            gboolean resize, gboolean shrink)
1919 {
1920   g_return_if_fail (GTK_IS_XPANED (xpaned));
1921   g_return_if_fail (GTK_IS_WIDGET (child));
1922
1923   if (!xpaned->top_right_child)
1924     {
1925       xpaned->top_right_child = child;
1926       xpaned->top_right_child_resize = resize;
1927       xpaned->top_right_child_shrink = shrink;
1928
1929       gtk_widget_set_parent (child, GTK_WIDGET (xpaned));
1930     }
1931 }
1932
1933 void
1934 gtk_xpaned_pack_bottom_left (GtkXPaned * xpaned,
1935                              GtkWidget * child,
1936                              gboolean resize, gboolean shrink)
1937 {
1938   g_return_if_fail (GTK_IS_XPANED (xpaned));
1939   g_return_if_fail (GTK_IS_WIDGET (child));
1940
1941   if (!xpaned->bottom_left_child)
1942     {
1943       xpaned->bottom_left_child = child;
1944       xpaned->bottom_left_child_resize = resize;
1945       xpaned->bottom_left_child_shrink = shrink;
1946
1947       gtk_widget_set_parent (child, GTK_WIDGET (xpaned));
1948     }
1949 }
1950
1951 void
1952 gtk_xpaned_pack_bottom_right (GtkXPaned * xpaned,
1953                               GtkWidget * child,
1954                               gboolean resize, gboolean shrink)
1955 {
1956   g_return_if_fail (GTK_IS_XPANED (xpaned));
1957   g_return_if_fail (GTK_IS_WIDGET (child));
1958
1959   if (!xpaned->bottom_right_child)
1960     {
1961       xpaned->bottom_right_child = child;
1962       xpaned->bottom_right_child_resize = resize;
1963       xpaned->bottom_right_child_shrink = shrink;
1964
1965       gtk_widget_set_parent (child, GTK_WIDGET (xpaned));
1966     }
1967 }
1968
1969 static void
1970 gtk_xpaned_add (GtkContainer * container, GtkWidget * widget)
1971 {
1972   GtkXPaned *xpaned;
1973
1974   g_return_if_fail (GTK_IS_XPANED (container));
1975
1976   xpaned = GTK_XPANED (container);
1977
1978   if (!xpaned->top_left_child)
1979     gtk_xpaned_add_top_left (xpaned, widget);
1980   else if (!xpaned->top_right_child)
1981     gtk_xpaned_add_top_right (xpaned, widget);
1982   else if (!xpaned->bottom_left_child)
1983     gtk_xpaned_add_bottom_left (xpaned, widget);
1984   else if (!xpaned->bottom_right_child)
1985     gtk_xpaned_add_bottom_right (xpaned, widget);
1986   else
1987     g_warning ("GtkXPaned cannot have more than 4 children\n");
1988 }
1989
1990 static void
1991 gtk_xpaned_remove (GtkContainer * container, GtkWidget * widget)
1992 {
1993   GtkXPaned *xpaned;
1994   gboolean was_visible;
1995
1996   xpaned = GTK_XPANED (container);
1997   was_visible = gtk_widget_get_visible (widget);
1998
1999   if (xpaned->top_left_child == widget)
2000     {
2001       gtk_widget_unparent (widget);
2002
2003       xpaned->top_left_child = NULL;
2004
2005       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
2006         gtk_widget_queue_resize (GTK_WIDGET (container));
2007     }
2008   else if (xpaned->top_right_child == widget)
2009     {
2010       gtk_widget_unparent (widget);
2011
2012       xpaned->top_right_child = NULL;
2013
2014       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
2015         gtk_widget_queue_resize (GTK_WIDGET (container));
2016     }
2017   else if (xpaned->bottom_left_child == widget)
2018     {
2019       gtk_widget_unparent (widget);
2020
2021       xpaned->bottom_left_child = NULL;
2022
2023       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
2024         gtk_widget_queue_resize (GTK_WIDGET (container));
2025     }
2026   else if (xpaned->bottom_right_child == widget)
2027     {
2028       gtk_widget_unparent (widget);
2029
2030       xpaned->bottom_right_child = NULL;
2031
2032       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
2033         gtk_widget_queue_resize (GTK_WIDGET (container));
2034     }
2035   else
2036     g_warning ("GtkXPaned has no more children attached\n");
2037
2038 }
2039
2040 static void
2041 gtk_xpaned_forall (GtkContainer * container,
2042                    gboolean include_internals,
2043                    GtkCallback callback, gpointer callback_data)
2044 {
2045   GtkXPaned *xpaned;
2046
2047   g_return_if_fail (callback != NULL);
2048
2049   xpaned = GTK_XPANED (container);
2050
2051   if (xpaned->top_left_child)
2052     (*callback) (xpaned->top_left_child, callback_data);
2053   if (xpaned->top_right_child)
2054     (*callback) (xpaned->top_right_child, callback_data);
2055   if (xpaned->bottom_left_child)
2056     (*callback) (xpaned->bottom_left_child, callback_data);
2057   if (xpaned->bottom_right_child)
2058     (*callback) (xpaned->bottom_right_child, callback_data);
2059 }
2060
2061 /**
2062  * gtk_xpaned_get_position_x:
2063  * @paned: a #GtkXPaned widget
2064  * 
2065  * Obtains the x-position of the divider.
2066  * 
2067  * Return value: x-position of the divider
2068  **/
2069 gint
2070 gtk_xpaned_get_position_x (GtkXPaned * xpaned)
2071 {
2072   g_return_val_if_fail (GTK_IS_XPANED (xpaned), 0);
2073
2074   return xpaned->top_left_child_size.width;
2075 }
2076
2077 /**
2078  * gtk_xpaned_get_position_y:
2079  * @paned: a #GtkXPaned widget
2080  * 
2081  * Obtains the y-position of the divider.
2082  * 
2083  * Return value: y-position of the divider
2084  **/
2085 gint
2086 gtk_xpaned_get_position_y (GtkXPaned * xpaned)
2087 {
2088   g_return_val_if_fail (GTK_IS_XPANED (xpaned), 0);
2089
2090   return xpaned->top_left_child_size.height;
2091 }
2092
2093 /**
2094  * gtk_xpaned_set_position_x:
2095  * @paned: a #GtkXPaned widget
2096  * @xposition: pixel x-position of divider, a negative values
2097  *                         of a component mean that the position is unset.
2098  * 
2099  * Sets the x-position of the divider between the four panes.
2100  **/
2101 void
2102 gtk_xpaned_set_position_x (GtkXPaned * xpaned, gint xposition)
2103 {
2104   GObject *object;
2105
2106   g_return_if_fail (GTK_IS_XPANED (xpaned));
2107
2108   /* if any child is currently maximized, jump right back */
2109   if (xpaned->maximized[GTK_XPANED_TOP_LEFT] ||
2110       xpaned->maximized[GTK_XPANED_TOP_RIGHT] ||
2111       xpaned->maximized[GTK_XPANED_BOTTOM_LEFT] ||
2112       xpaned->maximized[GTK_XPANED_BOTTOM_RIGHT])
2113     return;
2114
2115   object = G_OBJECT (xpaned);
2116
2117   if (xposition >= 0)
2118     {
2119       /* We don't clamp here - the assumption is that
2120        * if the total allocation changes at the same time
2121        * as the position, the position set is with reference
2122        * to the new total size. If only the position changes,
2123        * then clamping will occur in gtk_paned_compute_position()
2124        */
2125
2126       xpaned->top_left_child_size.width = xposition;
2127       xpaned->position_set = TRUE;
2128     }
2129   else
2130     {
2131       xpaned->position_set = FALSE;
2132     }
2133
2134   g_object_freeze_notify (object);
2135   g_object_notify (object, "x-position");
2136   g_object_notify (object, "position-set");
2137   g_object_thaw_notify (object);
2138
2139   gtk_widget_queue_resize (GTK_WIDGET (xpaned));
2140 }
2141
2142 /**
2143  * gtk_xpaned_set_position_y:
2144  * @paned: a #GtkXPaned widget
2145  * @yposition: pixel y-position of divider, a negative values
2146  *                         of a component mean that the position is unset.
2147  * 
2148  * Sets the y-position of the divider between the four panes.
2149  **/
2150 void
2151 gtk_xpaned_set_position_y (GtkXPaned * xpaned, gint yposition)
2152 {
2153   GObject *object;
2154
2155   g_return_if_fail (GTK_IS_XPANED (xpaned));
2156
2157   /* if any child is currently maximized, jump right back */
2158   if (xpaned->maximized[GTK_XPANED_TOP_LEFT] ||
2159       xpaned->maximized[GTK_XPANED_TOP_RIGHT] ||
2160       xpaned->maximized[GTK_XPANED_BOTTOM_LEFT] ||
2161       xpaned->maximized[GTK_XPANED_BOTTOM_RIGHT])
2162     return;
2163
2164   object = G_OBJECT (xpaned);
2165
2166   if (yposition >= 0)
2167     {
2168       /* We don't clamp here - the assumption is that
2169        * if the total allocation changes at the same time
2170        * as the position, the position set is with reference
2171        * to the new total size. If only the position changes,
2172        * then clamping will occur in gtk_paned_compute_position()
2173        */
2174
2175       xpaned->top_left_child_size.height = yposition;
2176       xpaned->position_set = TRUE;
2177     }
2178   else
2179     {
2180       xpaned->position_set = FALSE;
2181     }
2182
2183   g_object_freeze_notify (object);
2184   g_object_notify (object, "y-position");
2185   g_object_notify (object, "position-set");
2186   g_object_thaw_notify (object);
2187
2188   gtk_widget_queue_resize (GTK_WIDGET (xpaned));
2189 }
2190
2191 /* this call is private and only intended for internal use! */
2192 void
2193 gtk_xpaned_save_unmaximized_x (GtkXPaned * xpaned)
2194 {
2195   xpaned->unmaximized_position.x = gtk_xpaned_get_position_x (xpaned);
2196 }
2197
2198 /* this call is private and only intended for internal use! */
2199 void
2200 gtk_xpaned_save_unmaximized_y (GtkXPaned * xpaned)
2201 {
2202   xpaned->unmaximized_position.y = gtk_xpaned_get_position_y (xpaned);
2203 }
2204
2205 /* this call is private and only intended for internal use! */
2206 gint
2207 gtk_xpaned_fetch_unmaximized_x (GtkXPaned * xpaned)
2208 {
2209   return xpaned->unmaximized_position.x;
2210 }
2211
2212 /* this call is private and only intended for internal use! */
2213 gint
2214 gtk_xpaned_fetch_unmaximized_y (GtkXPaned * xpaned)
2215 {
2216   return xpaned->unmaximized_position.y;
2217 }
2218
2219 /**
2220  * gtk_xpaned_get_top_left_child:
2221  * @xpaned: a #GtkXPaned widget
2222  * 
2223  * Obtains the top-left child of the xpaned widget.
2224  * 
2225  * Return value: top-left child, or %NULL if it is not set.
2226  *
2227  * Since: 2.4
2228  **/
2229 GtkWidget *
2230 gtk_xpaned_get_top_left_child (GtkXPaned * xpaned)
2231 {
2232   g_return_val_if_fail (GTK_IS_XPANED (xpaned), NULL);
2233
2234   return xpaned->top_left_child;
2235 }
2236
2237 /**
2238  * gtk_xpaned_get_top_right_child:
2239  * @xpaned: a #GtkXPaned widget
2240  * 
2241  * Obtains the top-right child of the xpaned widget.
2242  * 
2243  * Return value: top-right child, or %NULL if it is not set.
2244  *
2245  * Since: 2.4
2246  **/
2247 GtkWidget *
2248 gtk_xpaned_get_top_right_child (GtkXPaned * xpaned)
2249 {
2250   g_return_val_if_fail (GTK_IS_XPANED (xpaned), NULL);
2251
2252   return xpaned->top_right_child;
2253 }
2254
2255 /**
2256  * gtk_xpaned_get_bottom_left_child:
2257  * @xpaned: a #GtkXPaned widget
2258  * 
2259  * Obtains the bottom-left child of the xpaned widget.
2260  * 
2261  * Return value: bottom-left child, or %NULL if it is not set.
2262  *
2263  * Since: 2.4
2264  **/
2265 GtkWidget *
2266 gtk_xpaned_get_bottom_left_child (GtkXPaned * xpaned)
2267 {
2268   g_return_val_if_fail (GTK_IS_XPANED (xpaned), NULL);
2269
2270   return xpaned->bottom_left_child;
2271 }
2272
2273 /**
2274  * gtk_xpaned_get_bottom_right_child:
2275  * @xpaned: a #GtkXPaned widget
2276  * 
2277  * Obtains the bottom-right child of the xpaned widget.
2278  * 
2279  * Return value: bottom-right child, or %NULL if it is not set.
2280  *
2281  * Since: 2.4
2282  **/
2283 GtkWidget *
2284 gtk_xpaned_get_bottom_right_child (GtkXPaned * xpaned)
2285 {
2286   g_return_val_if_fail (GTK_IS_XPANED (xpaned), NULL);
2287
2288   return xpaned->bottom_right_child;
2289 }
2290
2291 gboolean
2292 gtk_xpaned_maximize_top_left (GtkXPaned * xpaned, gboolean maximize)
2293 {
2294   if (maximize)
2295     {
2296       /* see if any child is already maximized */
2297       if (!xpaned->maximized[GTK_XPANED_TOP_LEFT] &&
2298           !xpaned->maximized[GTK_XPANED_TOP_RIGHT] &&
2299           !xpaned->maximized[GTK_XPANED_BOTTOM_LEFT] &&
2300           !xpaned->maximized[GTK_XPANED_BOTTOM_RIGHT])
2301         {
2302           /* save current position */
2303           gtk_xpaned_save_unmaximized_x (xpaned);
2304           gtk_xpaned_save_unmaximized_y (xpaned);
2305
2306           /* set new maximized position */
2307           gtk_xpaned_set_position_x (xpaned, xpaned->max_position.x);
2308           gtk_xpaned_set_position_y (xpaned, xpaned->max_position.y);
2309
2310           /* mark maximized flag for top-left child */
2311           xpaned->maximized[GTK_XPANED_TOP_LEFT] = TRUE;
2312
2313           return TRUE;
2314         }
2315       /* already one child maximized, report error */
2316       else
2317         return FALSE;
2318     }
2319   else
2320     {
2321       /* verify that top-left child is really currently maximized */
2322       if (xpaned->maximized[GTK_XPANED_TOP_LEFT])
2323         {
2324           /* clear maximized flat for top-left child */
2325           xpaned->maximized[GTK_XPANED_TOP_LEFT] = FALSE;
2326
2327           /* restore unmaximized position */
2328           gtk_xpaned_set_position_x (xpaned,
2329                                      gtk_xpaned_fetch_unmaximized_x (xpaned));
2330           gtk_xpaned_set_position_y (xpaned,
2331                                      gtk_xpaned_fetch_unmaximized_y (xpaned));
2332
2333           return TRUE;
2334         }
2335       /* top-left child is currently not maximized, report error */
2336       else
2337         return FALSE;
2338     }
2339 }
2340
2341 gboolean
2342 gtk_xpaned_maximize_top_right (GtkXPaned * xpaned, gboolean maximize)
2343 {
2344   if (maximize)
2345     {
2346       /* see if any child is already maximized */
2347       if (!xpaned->maximized[GTK_XPANED_TOP_LEFT] &&
2348           !xpaned->maximized[GTK_XPANED_TOP_RIGHT] &&
2349           !xpaned->maximized[GTK_XPANED_BOTTOM_LEFT] &&
2350           !xpaned->maximized[GTK_XPANED_BOTTOM_RIGHT])
2351         {
2352           /* save current position */
2353           gtk_xpaned_save_unmaximized_x (xpaned);
2354           gtk_xpaned_save_unmaximized_y (xpaned);
2355
2356           /* set new maximized position */
2357           gtk_xpaned_set_position_x (xpaned, xpaned->min_position.x);
2358           gtk_xpaned_set_position_y (xpaned, xpaned->max_position.y);
2359
2360           /* mark maximized flag for top-right child */
2361           xpaned->maximized[GTK_XPANED_TOP_RIGHT] = TRUE;
2362
2363           return TRUE;
2364         }
2365       /* already one child maximized, report error */
2366       else
2367         return FALSE;
2368     }
2369   else
2370     {
2371       /* verify that top-right child is really currently maximized */
2372       if (xpaned->maximized[GTK_XPANED_TOP_RIGHT])
2373         {
2374           /* clear maximized flat for top-right child */
2375           xpaned->maximized[GTK_XPANED_TOP_RIGHT] = FALSE;
2376
2377           /* restore unmaximized position */
2378           gtk_xpaned_set_position_x (xpaned,
2379                                      gtk_xpaned_fetch_unmaximized_x (xpaned));
2380           gtk_xpaned_set_position_y (xpaned,
2381                                      gtk_xpaned_fetch_unmaximized_y (xpaned));
2382
2383           return TRUE;
2384         }
2385       /* top-right child is currently not maximized, report error */
2386       else
2387         return FALSE;
2388     }
2389 }
2390
2391 gboolean
2392 gtk_xpaned_maximize_bottom_left (GtkXPaned * xpaned, gboolean maximize)
2393 {
2394   if (maximize)
2395     {
2396       /* see if any child is already maximized */
2397       if (!xpaned->maximized[GTK_XPANED_TOP_LEFT] &&
2398           !xpaned->maximized[GTK_XPANED_TOP_RIGHT] &&
2399           !xpaned->maximized[GTK_XPANED_BOTTOM_LEFT] &&
2400           !xpaned->maximized[GTK_XPANED_BOTTOM_RIGHT])
2401         {
2402           /* save current position */
2403           gtk_xpaned_save_unmaximized_x (xpaned);
2404           gtk_xpaned_save_unmaximized_y (xpaned);
2405
2406           /* set new maximized position */
2407           gtk_xpaned_set_position_x (xpaned, xpaned->max_position.x);
2408           gtk_xpaned_set_position_y (xpaned, xpaned->min_position.y);
2409
2410           /* mark maximized flag for bottom-left child */
2411           xpaned->maximized[GTK_XPANED_BOTTOM_LEFT] = TRUE;
2412
2413           return TRUE;
2414         }
2415       /* already one child maximized, report error */
2416       else
2417         return FALSE;
2418     }
2419   else
2420     {
2421       /* verify that bottom-left child is really currently maximized */
2422       if (xpaned->maximized[GTK_XPANED_BOTTOM_LEFT])
2423         {
2424           /* clear maximized flat for bottom-left child */
2425           xpaned->maximized[GTK_XPANED_BOTTOM_LEFT] = FALSE;
2426
2427           /* restore unmaximized position */
2428           gtk_xpaned_set_position_x (xpaned,
2429                                      gtk_xpaned_fetch_unmaximized_x (xpaned));
2430           gtk_xpaned_set_position_y (xpaned,
2431                                      gtk_xpaned_fetch_unmaximized_y (xpaned));
2432
2433           return TRUE;
2434         }
2435       /* bottom-left child is currently not maximized, report error */
2436       else
2437         return FALSE;
2438     }
2439 }
2440
2441 gboolean
2442 gtk_xpaned_maximize_bottom_right (GtkXPaned * xpaned, gboolean maximize)
2443 {
2444   if (maximize)
2445     {
2446       /* see if any child is already maximized */
2447       if (!xpaned->maximized[GTK_XPANED_TOP_LEFT] &&
2448           !xpaned->maximized[GTK_XPANED_TOP_RIGHT] &&
2449           !xpaned->maximized[GTK_XPANED_BOTTOM_LEFT] &&
2450           !xpaned->maximized[GTK_XPANED_BOTTOM_RIGHT])
2451         {
2452           /* save current position */
2453           gtk_xpaned_save_unmaximized_x (xpaned);
2454           gtk_xpaned_save_unmaximized_y (xpaned);
2455
2456           /* set new maximized position */
2457           gtk_xpaned_set_position_x (xpaned, xpaned->min_position.x);
2458           gtk_xpaned_set_position_y (xpaned, xpaned->min_position.y);
2459
2460           /* mark maximized flag for bottom-right child */
2461           xpaned->maximized[GTK_XPANED_BOTTOM_RIGHT] = TRUE;
2462
2463           return TRUE;
2464         }
2465       /* already one child maximized, report error */
2466       else
2467         return FALSE;
2468     }
2469   else
2470     {
2471       /* verify that bottom-right child is really currently maximized */
2472       if (xpaned->maximized[GTK_XPANED_BOTTOM_RIGHT])
2473         {
2474           /* clear maximized flat for bottom-right child */
2475           xpaned->maximized[GTK_XPANED_BOTTOM_RIGHT] = FALSE;
2476
2477           /* restore unmaximized position */
2478           gtk_xpaned_set_position_x (xpaned,
2479                                      gtk_xpaned_fetch_unmaximized_x (xpaned));
2480           gtk_xpaned_set_position_y (xpaned,
2481                                      gtk_xpaned_fetch_unmaximized_y (xpaned));
2482
2483           return TRUE;
2484         }
2485       /* bottom-right child is currently not maximized, report error */
2486       else
2487         return FALSE;
2488     }
2489 }
2490
2491 void
2492 gtk_xpaned_compute_position (GtkXPaned * xpaned,
2493                              const GtkAllocation * allocation,
2494                              GtkRequisition * top_left_child_req,
2495                              GtkRequisition * top_right_child_req,
2496                              GtkRequisition * bottom_left_child_req,
2497                              GtkRequisition * bottom_right_child_req)
2498 {
2499   GdkPoint old_position;
2500   GdkPoint old_min_position;
2501   GdkPoint old_max_position;
2502   gint handle_size;
2503   gint border_width = gtk_container_get_border_width (GTK_CONTAINER (xpaned));
2504
2505   g_return_if_fail (GTK_IS_XPANED (xpaned));
2506
2507   old_position.x = xpaned->top_left_child_size.width;
2508   old_position.y = xpaned->top_left_child_size.height;
2509   old_min_position.x = xpaned->min_position.x;
2510   old_min_position.y = xpaned->min_position.y;
2511   old_max_position.x = xpaned->max_position.x;
2512   old_max_position.y = xpaned->max_position.y;
2513
2514   xpaned->min_position.x =
2515     xpaned->top_left_child_shrink ? 0 : top_left_child_req->width;
2516   xpaned->min_position.y =
2517     xpaned->top_left_child_shrink ? 0 : top_left_child_req->height;
2518
2519   gtk_widget_style_get (GTK_WIDGET (xpaned), "handle-size", &handle_size,
2520                         NULL);
2521
2522   xpaned->max_position.x = allocation->width - 2 * border_width - handle_size;
2523   xpaned->max_position.y =
2524     allocation->height - 2 * border_width - handle_size;
2525   if (!xpaned->top_left_child_shrink)
2526     xpaned->max_position.x =
2527       MAX (1, xpaned->max_position.x - top_left_child_req->width);
2528   xpaned->max_position.x =
2529     MAX (xpaned->min_position.x, xpaned->max_position.x);
2530
2531   if (!xpaned->position_set)
2532     {
2533       if (xpaned->top_left_child_resize && !xpaned->top_right_child_resize)
2534         {
2535           xpaned->top_left_child_size.width =
2536             MAX (0, allocation->width - top_right_child_req->width);
2537           xpaned->top_left_child_size.height =
2538             MAX (0, allocation->height - top_right_child_req->height);
2539         }
2540       else if (!xpaned->top_left_child_resize
2541                && xpaned->top_right_child_resize)
2542         {
2543           xpaned->top_left_child_size.width = top_left_child_req->width;
2544           xpaned->top_left_child_size.height = top_left_child_req->height;
2545         }
2546       else
2547         {
2548           xpaned->top_left_child_size.width = allocation->width * 0.5 + 0.5;
2549           xpaned->top_left_child_size.height = allocation->height * 0.5 + 0.5;
2550         }
2551     }
2552   else
2553     {
2554       /* If the position was set before the initial allocation.
2555       ** (paned->last_allocation <= 0) just clamp it and leave it. */
2556       if (xpaned->last_allocation.width > 0)
2557         {
2558           if (xpaned->top_left_child_resize
2559               && !xpaned->top_right_child_resize)
2560             {
2561               xpaned->top_left_child_size.width += allocation->width
2562                 - xpaned->last_allocation.width;
2563
2564               xpaned->top_left_child_size.height += allocation->height
2565                 - xpaned->last_allocation.height;
2566             }
2567           else
2568             if (!
2569                 (!xpaned->top_left_child_resize
2570                  && xpaned->top_right_child_resize))
2571               {
2572                 xpaned->top_left_child_size.width = allocation->width
2573                   * ((gdouble) xpaned->top_left_child_size.width /
2574                      (xpaned->last_allocation.width)) + 0.5;
2575
2576                 xpaned->top_left_child_size.height = allocation->height
2577                   * ((gdouble) xpaned->top_left_child_size.height /
2578                      (xpaned->last_allocation.height)) + 0.5;
2579               }
2580         }
2581       if (xpaned->last_allocation.height > 0)
2582         {
2583           if (xpaned->top_left_child_resize
2584               && !xpaned->top_right_child_resize)
2585             {
2586               xpaned->top_left_child_size.width +=
2587                 allocation->width - xpaned->last_allocation.width;
2588               xpaned->top_left_child_size.height +=
2589                 allocation->height - xpaned->last_allocation.height;
2590             }
2591           else
2592             if (!
2593                 (!xpaned->top_left_child_resize
2594                  && xpaned->top_right_child_resize))
2595               {
2596                 xpaned->top_left_child_size.width =
2597                   allocation->width *
2598                   ((gdouble) xpaned->top_left_child_size.width /
2599                    (xpaned->last_allocation.width)) + 0.5;
2600                 xpaned->top_left_child_size.height =
2601                   allocation->height *
2602                   ((gdouble) xpaned->top_left_child_size.height /
2603                    (xpaned->last_allocation.height)) + 0.5;
2604               }
2605         }
2606
2607     }
2608
2609   xpaned->top_left_child_size.width =
2610     CLAMP (xpaned->top_left_child_size.width, xpaned->min_position.x,
2611            xpaned->max_position.x);
2612   xpaned->top_left_child_size.height =
2613     CLAMP (xpaned->top_left_child_size.height, xpaned->min_position.y,
2614            xpaned->max_position.y);
2615
2616   xpaned->top_right_child_size.width =
2617     CLAMP (xpaned->top_right_child_size.width, xpaned->min_position.x,
2618            xpaned->max_position.x);
2619   xpaned->top_right_child_size.height =
2620     CLAMP (xpaned->top_right_child_size.height, xpaned->min_position.y,
2621            xpaned->max_position.y);
2622
2623   xpaned->bottom_left_child_size.width =
2624     CLAMP (xpaned->bottom_left_child_size.width, xpaned->min_position.x,
2625            xpaned->max_position.x);
2626   xpaned->bottom_left_child_size.height =
2627     CLAMP (xpaned->bottom_left_child_size.height, xpaned->min_position.y,
2628            xpaned->max_position.y);
2629
2630   xpaned->bottom_right_child_size.width =
2631     CLAMP (xpaned->bottom_right_child_size.width, xpaned->min_position.x,
2632            xpaned->max_position.x);
2633   xpaned->bottom_right_child_size.height =
2634     CLAMP (xpaned->bottom_right_child_size.height, xpaned->min_position.y,
2635            xpaned->max_position.y);
2636
2637   gtk_widget_set_child_visible (xpaned->top_left_child, TRUE);
2638   gtk_widget_set_child_visible (xpaned->top_right_child, TRUE);
2639   gtk_widget_set_child_visible (xpaned->bottom_left_child, TRUE);
2640   gtk_widget_set_child_visible (xpaned->bottom_right_child, TRUE);
2641
2642   g_object_freeze_notify (G_OBJECT (xpaned));
2643
2644   if (xpaned->top_left_child_size.width != old_position.x)
2645     g_object_notify (G_OBJECT (xpaned), "x-position");
2646   if (xpaned->top_left_child_size.height != old_position.y)
2647     g_object_notify (G_OBJECT (xpaned), "y-position");
2648
2649   if (xpaned->top_right_child_size.width != old_position.x)
2650     g_object_notify (G_OBJECT (xpaned), "x-position");
2651   if (xpaned->top_right_child_size.height != old_position.y)
2652     g_object_notify (G_OBJECT (xpaned), "y-position");
2653
2654   if (xpaned->bottom_left_child_size.width != old_position.x)
2655     g_object_notify (G_OBJECT (xpaned), "x-position");
2656   if (xpaned->bottom_left_child_size.height != old_position.y)
2657     g_object_notify (G_OBJECT (xpaned), "y-position");
2658
2659   if (xpaned->bottom_right_child_size.width != old_position.x)
2660     g_object_notify (G_OBJECT (xpaned), "x-position");
2661   if (xpaned->bottom_right_child_size.height != old_position.y)
2662     g_object_notify (G_OBJECT (xpaned), "y-position");
2663
2664   if (xpaned->min_position.x != old_min_position.x)
2665     g_object_notify (G_OBJECT (xpaned), "min-x-position");
2666   if (xpaned->min_position.y != old_min_position.y)
2667     g_object_notify (G_OBJECT (xpaned), "min-y-position");
2668
2669   if (xpaned->max_position.x != old_max_position.x)
2670     g_object_notify (G_OBJECT (xpaned), "max-y-position");
2671   if (xpaned->max_position.y != old_max_position.y)
2672     g_object_notify (G_OBJECT (xpaned), "max-y-position");
2673
2674   g_object_thaw_notify (G_OBJECT (xpaned));
2675
2676   xpaned->last_allocation.width = allocation->width;
2677   xpaned->last_allocation.height = allocation->height;
2678 }
2679
2680 static void
2681 gtk_xpaned_set_saved_focus (GtkXPaned * xpaned, GtkWidget * widget)
2682 {
2683   if (xpaned->priv->saved_focus)
2684     g_object_remove_weak_pointer (G_OBJECT (xpaned->priv->saved_focus),
2685                                   (gpointer *) & (xpaned->priv->saved_focus));
2686
2687   xpaned->priv->saved_focus = widget;
2688
2689   if (xpaned->priv->saved_focus)
2690     g_object_add_weak_pointer (G_OBJECT (xpaned->priv->saved_focus),
2691                                (gpointer *) & (xpaned->priv->saved_focus));
2692 }
2693
2694 static void
2695 gtk_xpaned_set_first_xpaned (GtkXPaned * xpaned, GtkXPaned * first_xpaned)
2696 {
2697   if (xpaned->priv->first_xpaned)
2698     g_object_remove_weak_pointer (G_OBJECT (xpaned->priv->first_xpaned),
2699                                   (gpointer *) & (xpaned->priv->
2700                                                   first_xpaned));
2701
2702   xpaned->priv->first_xpaned = first_xpaned;
2703
2704   if (xpaned->priv->first_xpaned)
2705     g_object_add_weak_pointer (G_OBJECT (xpaned->priv->first_xpaned),
2706                                (gpointer *) & (xpaned->priv->first_xpaned));
2707 }
2708
2709 static void
2710 gtk_xpaned_set_last_top_left_child_focus (GtkXPaned * xpaned,
2711                                           GtkWidget * widget)
2712 {
2713   if (xpaned->last_top_left_child_focus)
2714     g_object_remove_weak_pointer (G_OBJECT
2715                                   (xpaned->last_top_left_child_focus),
2716                                   (gpointer *) & (xpaned->
2717                                                   last_top_left_child_focus));
2718
2719   xpaned->last_top_left_child_focus = widget;
2720
2721   if (xpaned->last_top_left_child_focus)
2722     g_object_add_weak_pointer (G_OBJECT (xpaned->last_top_left_child_focus),
2723                                (gpointer *) & (xpaned->
2724                                                last_top_left_child_focus));
2725 }
2726
2727 static void
2728 gtk_xpaned_set_last_top_right_child_focus (GtkXPaned * xpaned,
2729                                            GtkWidget * widget)
2730 {
2731   if (xpaned->last_top_right_child_focus)
2732     g_object_remove_weak_pointer (G_OBJECT
2733                                   (xpaned->last_top_right_child_focus),
2734                                   (gpointer *) & (xpaned->
2735                                                   last_top_right_child_focus));
2736
2737   xpaned->last_top_right_child_focus = widget;
2738
2739   if (xpaned->last_top_right_child_focus)
2740     g_object_add_weak_pointer (G_OBJECT (xpaned->last_top_right_child_focus),
2741                                (gpointer *) & (xpaned->
2742                                                last_top_right_child_focus));
2743 }
2744
2745 static void
2746 gtk_xpaned_set_last_bottom_left_child_focus (GtkXPaned * xpaned,
2747                                              GtkWidget * widget)
2748 {
2749   if (xpaned->last_bottom_left_child_focus)
2750     g_object_remove_weak_pointer (G_OBJECT
2751                                   (xpaned->last_bottom_left_child_focus),
2752                                   (gpointer *) & (xpaned->
2753                                                   last_bottom_left_child_focus));
2754
2755   xpaned->last_bottom_left_child_focus = widget;
2756
2757   if (xpaned->last_bottom_left_child_focus)
2758     g_object_add_weak_pointer (G_OBJECT
2759                                (xpaned->last_bottom_left_child_focus),
2760                                (gpointer *) & (xpaned->
2761                                                last_bottom_left_child_focus));
2762 }
2763
2764 static void
2765 gtk_xpaned_set_last_bottom_right_child_focus (GtkXPaned * xpaned,
2766                                               GtkWidget * widget)
2767 {
2768   if (xpaned->last_bottom_right_child_focus)
2769     g_object_remove_weak_pointer (G_OBJECT
2770                                   (xpaned->last_bottom_right_child_focus),
2771                                   (gpointer *) & (xpaned->
2772                                                   last_bottom_right_child_focus));
2773
2774   xpaned->last_bottom_right_child_focus = widget;
2775
2776   if (xpaned->last_bottom_right_child_focus)
2777     g_object_add_weak_pointer (G_OBJECT
2778                                (xpaned->last_bottom_right_child_focus),
2779                                (gpointer *) & (xpaned->
2780                                                last_bottom_right_child_focus));
2781 }
2782
2783 static GtkWidget *
2784 xpaned_get_focus_widget (GtkXPaned * xpaned)
2785 {
2786   GtkWidget *toplevel;
2787
2788   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (xpaned));
2789   if (gtk_widget_is_toplevel (toplevel))
2790     return gtk_window_get_focus (GTK_WINDOW (toplevel));
2791
2792   return NULL;
2793 }
2794
2795 static void
2796 gtk_xpaned_set_focus_child (GtkContainer * container, GtkWidget * focus_child)
2797 {
2798   GtkXPaned *xpaned;
2799
2800   g_return_if_fail (GTK_IS_XPANED (container));
2801
2802   xpaned = GTK_XPANED (container);
2803
2804   if (focus_child == NULL)
2805     {
2806       GtkWidget *last_focus;
2807       GtkWidget *w;
2808
2809       last_focus = xpaned_get_focus_widget (xpaned);
2810
2811       if (last_focus)
2812         {
2813           /* If there is one or more paned widgets between us and the
2814            * focus widget, we want the topmost of those as last_focus
2815            */
2816           for (w = last_focus; w != GTK_WIDGET (xpaned); w = gtk_widget_get_parent (w))
2817             if (GTK_IS_XPANED (w))
2818               last_focus = w;
2819
2820           if (gtk_container_get_focus_child (container) == xpaned->top_left_child)
2821             gtk_xpaned_set_last_top_left_child_focus (xpaned, last_focus);
2822           else if (gtk_container_get_focus_child (container) == xpaned->top_right_child)
2823             gtk_xpaned_set_last_top_right_child_focus (xpaned, last_focus);
2824           else if (gtk_container_get_focus_child (container) == xpaned->bottom_left_child)
2825             gtk_xpaned_set_last_bottom_left_child_focus (xpaned, last_focus);
2826           else if (gtk_container_get_focus_child (container) == xpaned->bottom_right_child)
2827             gtk_xpaned_set_last_bottom_right_child_focus (xpaned, last_focus);
2828         }
2829     }
2830
2831   if (parent_class->set_focus_child)
2832     (*parent_class->set_focus_child) (container, focus_child);
2833 }
2834
2835 static void
2836 gtk_xpaned_get_cycle_chain (GtkXPaned * xpaned,
2837                             GtkDirectionType direction, GList ** widgets)
2838 {
2839   GtkContainer *container = GTK_CONTAINER (xpaned);
2840   GtkWidget *ancestor = NULL;
2841   GList *temp_list = NULL;
2842   GList *list;
2843
2844   if (xpaned->in_recursion)
2845     return;
2846
2847   g_assert (widgets != NULL);
2848
2849   if (xpaned->last_top_left_child_focus &&
2850       !gtk_widget_is_ancestor (xpaned->last_top_left_child_focus,
2851                                GTK_WIDGET (xpaned)))
2852     {
2853       gtk_xpaned_set_last_top_left_child_focus (xpaned, NULL);
2854     }
2855
2856   if (xpaned->last_top_right_child_focus &&
2857       !gtk_widget_is_ancestor (xpaned->last_top_right_child_focus,
2858                                GTK_WIDGET (xpaned)))
2859     {
2860       gtk_xpaned_set_last_top_right_child_focus (xpaned, NULL);
2861     }
2862
2863   if (xpaned->last_bottom_left_child_focus &&
2864       !gtk_widget_is_ancestor (xpaned->last_bottom_left_child_focus,
2865                                GTK_WIDGET (xpaned)))
2866     {
2867       gtk_xpaned_set_last_bottom_left_child_focus (xpaned, NULL);
2868     }
2869
2870   if (xpaned->last_bottom_right_child_focus &&
2871       !gtk_widget_is_ancestor (xpaned->last_bottom_right_child_focus,
2872                                GTK_WIDGET (xpaned)))
2873     {
2874       gtk_xpaned_set_last_bottom_right_child_focus (xpaned, NULL);
2875     }
2876
2877   if (gtk_widget_get_parent (GTK_WIDGET (xpaned)))
2878     ancestor = gtk_widget_get_ancestor (gtk_widget_get_parent (GTK_WIDGET (xpaned)),
2879                                         GTK_TYPE_XPANED);
2880
2881   /* The idea here is that temp_list is a list of widgets we want to cycle
2882    * to. The list is prioritized so that the first element is our first
2883    * choice, the next our second, and so on.
2884    *
2885    * We can't just use g_list_reverse(), because we want to try
2886    * paned->last_child?_focus before paned->child?, both when we
2887    * are going forward and backward.
2888    */
2889   if (direction == GTK_DIR_TAB_FORWARD)
2890     {
2891       if (gtk_container_get_focus_child (container) == xpaned->top_left_child)
2892         {
2893           temp_list =
2894             g_list_append (temp_list, xpaned->last_top_right_child_focus);
2895           temp_list = g_list_append (temp_list, xpaned->top_right_child);
2896           temp_list = g_list_append (temp_list, ancestor);
2897         }
2898       else if (gtk_container_get_focus_child (container) == xpaned->top_right_child)
2899         {
2900           temp_list = g_list_append (temp_list, ancestor);
2901           temp_list =
2902             g_list_append (temp_list, xpaned->last_bottom_left_child_focus);
2903           temp_list = g_list_append (temp_list, xpaned->bottom_left_child);
2904         }
2905       else if (gtk_container_get_focus_child (container) == xpaned->bottom_left_child)
2906         {
2907           temp_list = g_list_append (temp_list, ancestor);
2908           temp_list =
2909             g_list_append (temp_list, xpaned->last_bottom_right_child_focus);
2910           temp_list = g_list_append (temp_list, xpaned->bottom_right_child);
2911         }
2912       else if (gtk_container_get_focus_child (container) == xpaned->bottom_right_child)
2913         {
2914           temp_list = g_list_append (temp_list, ancestor);
2915           temp_list =
2916             g_list_append (temp_list, xpaned->last_top_left_child_focus);
2917           temp_list = g_list_append (temp_list, xpaned->top_left_child);
2918         }
2919       else
2920         {
2921           temp_list =
2922             g_list_append (temp_list, xpaned->last_top_left_child_focus);
2923           temp_list = g_list_append (temp_list, xpaned->top_left_child);
2924           temp_list =
2925             g_list_append (temp_list, xpaned->last_top_right_child_focus);
2926           temp_list = g_list_append (temp_list, xpaned->top_right_child);
2927           temp_list =
2928             g_list_append (temp_list, xpaned->last_bottom_left_child_focus);
2929           temp_list = g_list_append (temp_list, xpaned->bottom_left_child);
2930           temp_list =
2931             g_list_append (temp_list, xpaned->last_bottom_right_child_focus);
2932           temp_list = g_list_append (temp_list, xpaned->bottom_right_child);
2933           temp_list = g_list_append (temp_list, ancestor);
2934         }
2935     }
2936   else
2937     {
2938       if (gtk_container_get_focus_child (container) == xpaned->top_left_child)
2939         {
2940           temp_list = g_list_append (temp_list, ancestor);
2941           temp_list =
2942             g_list_append (temp_list, xpaned->last_top_right_child_focus);
2943           temp_list = g_list_append (temp_list, xpaned->top_right_child);
2944         }
2945       else if (gtk_container_get_focus_child (container) == xpaned->top_right_child)
2946         {
2947           temp_list =
2948             g_list_append (temp_list, xpaned->last_bottom_left_child_focus);
2949           temp_list = g_list_append (temp_list, xpaned->bottom_left_child);
2950           temp_list = g_list_append (temp_list, ancestor);
2951         }
2952       else if (gtk_container_get_focus_child (container) == xpaned->bottom_right_child)
2953         {
2954           temp_list =
2955             g_list_append (temp_list, xpaned->last_bottom_left_child_focus);
2956           temp_list = g_list_append (temp_list, xpaned->bottom_left_child);
2957           temp_list = g_list_append (temp_list, ancestor);
2958         }
2959       else if (gtk_container_get_focus_child (container) == xpaned->top_right_child)
2960         {
2961           temp_list =
2962             g_list_append (temp_list, xpaned->last_bottom_left_child_focus);
2963           temp_list = g_list_append (temp_list, xpaned->bottom_left_child);
2964           temp_list = g_list_append (temp_list, ancestor);
2965         }
2966       else
2967         {
2968           temp_list =
2969             g_list_append (temp_list, xpaned->last_bottom_right_child_focus);
2970           temp_list = g_list_append (temp_list, xpaned->bottom_right_child);
2971           temp_list =
2972             g_list_append (temp_list, xpaned->last_bottom_left_child_focus);
2973           temp_list = g_list_append (temp_list, xpaned->bottom_left_child);
2974           temp_list =
2975             g_list_append (temp_list, xpaned->last_top_right_child_focus);
2976           temp_list = g_list_append (temp_list, xpaned->top_right_child);
2977           temp_list =
2978             g_list_append (temp_list, xpaned->last_top_left_child_focus);
2979           temp_list = g_list_append (temp_list, xpaned->top_left_child);
2980           temp_list = g_list_append (temp_list, ancestor);
2981         }
2982     }
2983
2984   /* Walk the list and expand all the paned widgets. */
2985   for (list = temp_list; list != NULL; list = list->next)
2986     {
2987       GtkWidget *widget = list->data;
2988
2989       if (widget)
2990         {
2991           if (GTK_IS_XPANED (widget))
2992             {
2993               xpaned->in_recursion = TRUE;
2994               gtk_xpaned_get_cycle_chain (GTK_XPANED (widget),
2995                                           direction, widgets);
2996               xpaned->in_recursion = FALSE;
2997             }
2998           else
2999             {
3000               *widgets = g_list_append (*widgets, widget);
3001             }
3002         }
3003     }
3004
3005   g_list_free (temp_list);
3006 }
3007
3008 static gboolean
3009 gtk_xpaned_cycle_child_focus (GtkXPaned * xpaned, gboolean reversed)
3010 {
3011   GList *cycle_chain = NULL;
3012   GList *list;
3013
3014   GtkDirectionType direction =
3015     reversed ? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
3016
3017   /* ignore f6 if the handle is focused */
3018   if (gtk_widget_is_focus (GTK_WIDGET (xpaned)))
3019     return TRUE;
3020
3021   /* we can't just let the event propagate up the hierarchy,
3022    * because the paned will want to cycle focus _unless_ an
3023    * ancestor paned handles the event
3024    */
3025   gtk_xpaned_get_cycle_chain (xpaned, direction, &cycle_chain);
3026
3027   for (list = cycle_chain; list != NULL; list = list->next)
3028     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
3029       break;
3030
3031   g_list_free (cycle_chain);
3032
3033   return TRUE;
3034 }
3035
3036 static void
3037 get_child_xpanes (GtkWidget * widget, GList ** xpanes)
3038 {
3039   if (GTK_IS_XPANED (widget))
3040     {
3041       GtkXPaned *xpaned = GTK_XPANED (widget);
3042
3043       get_child_xpanes (xpaned->top_left_child, xpanes);
3044       *xpanes = g_list_prepend (*xpanes, widget);
3045       get_child_xpanes (xpaned->top_right_child, xpanes);
3046       *xpanes = g_list_prepend (*xpanes, widget);
3047       get_child_xpanes (xpaned->bottom_left_child, xpanes);
3048       *xpanes = g_list_prepend (*xpanes, widget);
3049       get_child_xpanes (xpaned->bottom_right_child, xpanes);
3050     }
3051   else if (GTK_IS_CONTAINER (widget))
3052     {
3053       gtk_container_foreach (GTK_CONTAINER (widget),
3054                              (GtkCallback) get_child_xpanes, xpanes);
3055     }
3056 }
3057
3058 static GList *
3059 get_all_xpanes (GtkXPaned * xpaned)
3060 {
3061   GtkXPaned *topmost = NULL;
3062   GList *result = NULL;
3063   GtkWidget *w;
3064
3065   for (w = GTK_WIDGET (xpaned); w != NULL; w = gtk_widget_get_parent (w))
3066     {
3067       if (GTK_IS_XPANED (w))
3068         topmost = GTK_XPANED (w);
3069     }
3070
3071   g_assert (topmost);
3072
3073   get_child_xpanes (GTK_WIDGET (topmost), &result);
3074
3075   return g_list_reverse (result);
3076 }
3077
3078 static void
3079 gtk_xpaned_find_neighbours (GtkXPaned * xpaned,
3080                             GtkXPaned ** next, GtkXPaned ** prev)
3081 {
3082   GList *all_xpanes;
3083   GList *this_link;
3084
3085   all_xpanes = get_all_xpanes (xpaned);
3086   g_assert (all_xpanes);
3087
3088   this_link = g_list_find (all_xpanes, xpaned);
3089
3090   g_assert (this_link);
3091
3092   if (this_link->next)
3093     *next = this_link->next->data;
3094   else
3095     *next = all_xpanes->data;
3096
3097   if (this_link->prev)
3098     *prev = this_link->prev->data;
3099   else
3100     *prev = g_list_last (all_xpanes)->data;
3101
3102   g_list_free (all_xpanes);
3103 }
3104
3105 static gboolean
3106 gtk_xpaned_move_handle (GtkXPaned * xpaned, GtkScrollType scroll)
3107 {
3108   if (gtk_widget_is_focus (GTK_WIDGET (xpaned)))
3109     {
3110       GdkPoint old_position;
3111       GdkPoint new_position;
3112       gint increment;
3113
3114       enum
3115       {
3116         SINGLE_STEP_SIZE = 1,
3117         PAGE_STEP_SIZE = 75
3118       };
3119
3120       new_position.x = old_position.x = gtk_xpaned_get_position_x (xpaned);
3121       new_position.y = old_position.y = gtk_xpaned_get_position_y (xpaned);
3122       increment = 0;
3123
3124       switch (scroll)
3125         {
3126         case GTK_SCROLL_STEP_LEFT:
3127         case GTK_SCROLL_STEP_UP:
3128         case GTK_SCROLL_STEP_BACKWARD:
3129           increment = -SINGLE_STEP_SIZE;
3130           break;
3131
3132         case GTK_SCROLL_STEP_RIGHT:
3133         case GTK_SCROLL_STEP_DOWN:
3134         case GTK_SCROLL_STEP_FORWARD:
3135           increment = SINGLE_STEP_SIZE;
3136           break;
3137
3138         case GTK_SCROLL_PAGE_LEFT:
3139         case GTK_SCROLL_PAGE_UP:
3140         case GTK_SCROLL_PAGE_BACKWARD:
3141           increment = -PAGE_STEP_SIZE;
3142           break;
3143
3144         case GTK_SCROLL_PAGE_RIGHT:
3145         case GTK_SCROLL_PAGE_DOWN:
3146         case GTK_SCROLL_PAGE_FORWARD:
3147           increment = PAGE_STEP_SIZE;
3148           break;
3149
3150         case GTK_SCROLL_START:
3151           new_position.x = xpaned->min_position.x;
3152           new_position.y = xpaned->min_position.y;
3153           break;
3154
3155         case GTK_SCROLL_END:
3156           new_position.x = xpaned->max_position.x;
3157           new_position.y = xpaned->max_position.y;
3158           break;
3159
3160         default:
3161           break;
3162         }
3163
3164       if (increment)
3165         {
3166           if (is_rtl (xpaned))
3167             increment = -increment;
3168
3169           new_position.x = old_position.x + increment;
3170           new_position.y = old_position.y + increment;
3171         }
3172
3173       new_position.x = CLAMP (new_position.x,
3174                               xpaned->min_position.x, xpaned->max_position.x);
3175
3176       new_position.y = CLAMP (new_position.y,
3177                               xpaned->min_position.y, xpaned->max_position.y);
3178
3179       if (old_position.x != new_position.x)
3180         gtk_xpaned_set_position_x (xpaned, new_position.x);
3181
3182       if (old_position.y != new_position.y)
3183         gtk_xpaned_set_position_y (xpaned, new_position.y);
3184
3185       return TRUE;
3186     }
3187
3188   return FALSE;
3189 }
3190
3191 static void
3192 gtk_xpaned_restore_focus (GtkXPaned * xpaned)
3193 {
3194   if (gtk_widget_is_focus (GTK_WIDGET (xpaned)))
3195     {
3196       if (xpaned->priv->saved_focus &&
3197           gtk_widget_get_sensitive (xpaned->priv->saved_focus))
3198         {
3199           gtk_widget_grab_focus (xpaned->priv->saved_focus);
3200         }
3201       else
3202         {
3203           /* the saved focus is somehow not available for focusing,
3204            * try
3205            *   1) tabbing into the paned widget
3206            * if that didn't work,
3207            *   2) unset focus for the window if there is one
3208            */
3209
3210           if (!gtk_widget_child_focus
3211               (GTK_WIDGET (xpaned), GTK_DIR_TAB_FORWARD))
3212             {
3213               GtkWidget *toplevel =
3214                 gtk_widget_get_toplevel (GTK_WIDGET (xpaned));
3215
3216               if (GTK_IS_WINDOW (toplevel))
3217                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
3218             }
3219         }
3220
3221       gtk_xpaned_set_saved_focus (xpaned, NULL);
3222       gtk_xpaned_set_first_xpaned (xpaned, NULL);
3223     }
3224 }
3225
3226 static gboolean
3227 gtk_xpaned_accept_position (GtkXPaned * xpaned)
3228 {
3229   if (gtk_widget_is_focus (GTK_WIDGET (xpaned)))
3230     {
3231       xpaned->original_position.x = -1;
3232       xpaned->original_position.y = -1;
3233       gtk_xpaned_restore_focus (xpaned);
3234
3235       return TRUE;
3236     }
3237
3238   return FALSE;
3239 }
3240
3241 static gboolean
3242 gtk_xpaned_cancel_position (GtkXPaned * xpaned)
3243 {
3244   if (gtk_widget_is_focus (GTK_WIDGET (xpaned)))
3245     {
3246       if (xpaned->original_position.x != -1)
3247         {
3248           gtk_xpaned_set_position_x (xpaned, xpaned->original_position.x);
3249           xpaned->original_position.x = -1;
3250         }
3251
3252       if (xpaned->original_position.y != -1)
3253         {
3254           gtk_xpaned_set_position_y (xpaned, xpaned->original_position.y);
3255           xpaned->original_position.y = -1;
3256         }
3257
3258       gtk_xpaned_restore_focus (xpaned);
3259       return TRUE;
3260     }
3261
3262   return FALSE;
3263 }
3264
3265 static gboolean
3266 gtk_xpaned_cycle_handle_focus (GtkXPaned * xpaned, gboolean reversed)
3267 {
3268   GtkXPaned *next;
3269   GtkXPaned *prev;
3270
3271   if (gtk_widget_is_focus (GTK_WIDGET (xpaned)))
3272     {
3273       GtkXPaned *focus = NULL;
3274
3275       if (!xpaned->priv->first_xpaned)
3276         {
3277           /* The first_pane has disappeared. As an ad-hoc solution,
3278            * we make the currently focused paned the first_paned. To the
3279            * user this will seem like the paned cycling has been reset.
3280            */
3281           gtk_xpaned_set_first_xpaned (xpaned, xpaned);
3282         }
3283
3284       gtk_xpaned_find_neighbours (xpaned, &next, &prev);
3285
3286       if (reversed && prev &&
3287           prev != xpaned && xpaned != xpaned->priv->first_xpaned)
3288         {
3289           focus = prev;
3290         }
3291       else if (!reversed &&
3292                next && next != xpaned && next != xpaned->priv->first_xpaned)
3293         {
3294           focus = next;
3295         }
3296       else
3297         {
3298           gtk_xpaned_accept_position (xpaned);
3299           return TRUE;
3300         }
3301
3302       g_assert (focus);
3303
3304       gtk_xpaned_set_saved_focus (focus, xpaned->priv->saved_focus);
3305       gtk_xpaned_set_first_xpaned (focus, xpaned->priv->first_xpaned);
3306
3307       gtk_xpaned_set_saved_focus (xpaned, NULL);
3308       gtk_xpaned_set_first_xpaned (xpaned, NULL);
3309
3310       gtk_widget_grab_focus (GTK_WIDGET (focus));
3311
3312       if (!gtk_widget_is_focus (GTK_WIDGET (xpaned)))
3313         {
3314           xpaned->original_position.x = -1;
3315           xpaned->original_position.y = -1;
3316           focus->original_position.x = gtk_xpaned_get_position_x (focus);
3317           focus->original_position.y = gtk_xpaned_get_position_y (focus);
3318         }
3319     }
3320   else
3321     {
3322       GtkContainer *container = GTK_CONTAINER (xpaned);
3323       GtkXPaned *focus;
3324       GtkXPaned *first;
3325       GtkXPaned *prev;
3326       GtkXPaned *next;
3327       GtkWidget *toplevel;
3328
3329       gtk_xpaned_find_neighbours (xpaned, &next, &prev);
3330
3331       if (gtk_container_get_focus_child (container) == xpaned->top_left_child)
3332         {
3333           if (reversed)
3334             {
3335               focus = prev;
3336               first = xpaned;
3337             }
3338           else
3339             {
3340               focus = xpaned;
3341               first = xpaned;
3342             }
3343         }
3344       else if (gtk_container_get_focus_child (container) == xpaned->top_right_child)
3345         {
3346           if (reversed)
3347             {
3348               focus = xpaned;
3349               first = next;
3350             }
3351           else
3352             {
3353               focus = next;
3354               first = next;
3355             }
3356         }
3357       else
3358         {
3359           /* Focus is not inside this xpaned, and we don't have focus.
3360            * Presumably this happened because the application wants us
3361            * to start keyboard navigating.
3362            */
3363           focus = xpaned;
3364
3365           if (reversed)
3366             first = xpaned;
3367           else
3368             first = next;
3369         }
3370
3371       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (xpaned));
3372
3373       if (GTK_IS_WINDOW (toplevel))
3374         gtk_xpaned_set_saved_focus (focus,
3375                                     gtk_window_get_focus (GTK_WINDOW (toplevel)));
3376       gtk_xpaned_set_first_xpaned (focus, first);
3377       focus->original_position.x = gtk_xpaned_get_position_x (focus);
3378       focus->original_position.y = gtk_xpaned_get_position_y (focus);
3379
3380       gtk_widget_grab_focus (GTK_WIDGET (focus));
3381     }
3382
3383   return TRUE;
3384 }
3385
3386 static gboolean
3387 gtk_xpaned_toggle_handle_focus (GtkXPaned * xpaned)
3388 {
3389   /* This function/signal has the wrong name. It is called when you
3390    * press Tab or Shift-Tab and what we do is act as if
3391    * the user pressed Return and then Tab or Shift-Tab
3392    */
3393   if (gtk_widget_is_focus (GTK_WIDGET (xpaned)))
3394     gtk_xpaned_accept_position (xpaned);
3395
3396   return FALSE;
3397 }
3398
3399 /*#define __GTK_XPANED_C__*/
3400 /*#include "gtkaliasdef.c"*/