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