]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
Practically everything changed.
[~andy/gtk] / gtk / gtkpaned.c
1 /* GTK - The GTK+ 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   if (GTK_WIDGET_CLASS (gtk_paned_parent_class)->unrealize)
777     (* GTK_WIDGET_CLASS (gtk_paned_parent_class)->unrealize) (widget);
778 }
779
780 static void
781 gtk_paned_map (GtkWidget *widget)
782 {
783   GtkPaned *paned = GTK_PANED (widget);
784
785   gdk_window_show (paned->handle);
786
787   GTK_WIDGET_CLASS (gtk_paned_parent_class)->map (widget);
788 }
789
790 static void
791 gtk_paned_unmap (GtkWidget *widget)
792 {
793   GtkPaned *paned = GTK_PANED (widget);
794     
795   gdk_window_hide (paned->handle);
796
797   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unmap (widget);
798 }
799
800 static gboolean
801 gtk_paned_expose (GtkWidget      *widget,
802                   GdkEventExpose *event)
803 {
804   GtkPaned *paned = GTK_PANED (widget);
805
806   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
807       paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
808       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
809     {
810       GtkStateType state;
811       
812       if (gtk_widget_is_focus (widget))
813         state = GTK_STATE_SELECTED;
814       else if (paned->handle_prelit)
815         state = GTK_STATE_PRELIGHT;
816       else
817         state = GTK_WIDGET_STATE (widget);
818       
819       gtk_paint_handle (widget->style, widget->window,
820                         state, GTK_SHADOW_NONE,
821                         &paned->handle_pos, widget, "paned",
822                         paned->handle_pos.x, paned->handle_pos.y,
823                         paned->handle_pos.width, paned->handle_pos.height,
824                         paned->orientation);
825     }
826
827   /* Chain up to draw children */
828   GTK_WIDGET_CLASS (gtk_paned_parent_class)->expose_event (widget, event);
829   
830   return FALSE;
831 }
832
833 static gboolean
834 is_rtl (GtkPaned *paned)
835 {
836   if (paned->orientation == GTK_ORIENTATION_VERTICAL &&
837       gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL)
838     {
839       return TRUE;
840     }
841
842   return FALSE;
843 }
844
845 static void
846 update_drag (GtkPaned *paned)
847 {
848   gint pos;
849   gint handle_size;
850   gint size;
851   
852   if (paned->orientation == GTK_ORIENTATION_HORIZONTAL)
853     gtk_widget_get_pointer (GTK_WIDGET (paned), NULL, &pos);
854   else
855     gtk_widget_get_pointer (GTK_WIDGET (paned), &pos, NULL);
856
857   pos -= paned->drag_pos;
858
859   if (is_rtl (paned))
860     {
861       gtk_widget_style_get (GTK_WIDGET (paned),
862                             "handle-size", &handle_size,
863                             NULL);
864       
865       size = GTK_WIDGET (paned)->allocation.width - pos - handle_size;
866     }
867   else
868     {
869       size = pos;
870     }
871
872   size -= GTK_CONTAINER (paned)->border_width;
873   
874   size = CLAMP (size, paned->min_position, paned->max_position);
875
876   if (size != paned->child1_size)
877     gtk_paned_set_position (paned, size);
878 }
879
880 static gboolean
881 gtk_paned_enter (GtkWidget        *widget,
882                  GdkEventCrossing *event)
883 {
884   GtkPaned *paned = GTK_PANED (widget);
885   
886   if (paned->in_drag)
887     update_drag (paned);
888   else
889     {
890       paned->handle_prelit = TRUE;
891       gtk_widget_queue_draw_area (widget,
892                                   paned->handle_pos.x,
893                                   paned->handle_pos.y,
894                                   paned->handle_pos.width,
895                                   paned->handle_pos.height);
896     }
897   
898   return TRUE;
899 }
900
901 static gboolean
902 gtk_paned_leave (GtkWidget        *widget,
903                  GdkEventCrossing *event)
904 {
905   GtkPaned *paned = GTK_PANED (widget);
906   
907   if (paned->in_drag)
908     update_drag (paned);
909   else
910     {
911       paned->handle_prelit = FALSE;
912       gtk_widget_queue_draw_area (widget,
913                                   paned->handle_pos.x,
914                                   paned->handle_pos.y,
915                                   paned->handle_pos.width,
916                                   paned->handle_pos.height);
917     }
918
919   return TRUE;
920 }
921
922 static gboolean
923 gtk_paned_focus (GtkWidget        *widget,
924                  GtkDirectionType  direction)
925
926 {
927   gboolean retval;
928   
929   /* This is a hack, but how can this be done without
930    * excessive cut-and-paste from gtkcontainer.c?
931    */
932
933   GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
934   retval = (* GTK_WIDGET_CLASS (gtk_paned_parent_class)->focus) (widget, direction);
935   GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
936
937   return retval;
938 }
939
940 static gboolean
941 gtk_paned_button_press (GtkWidget      *widget,
942                         GdkEventButton *event)
943 {
944   GtkPaned *paned = GTK_PANED (widget);
945
946   if (!paned->in_drag &&
947       (event->window == paned->handle) && (event->button == 1))
948     {
949       /* We need a server grab here, not gtk_grab_add(), since
950        * we don't want to pass events on to the widget's children */
951       if (gdk_pointer_grab (paned->handle, FALSE,
952                             GDK_POINTER_MOTION_HINT_MASK
953                             | GDK_BUTTON1_MOTION_MASK
954                             | GDK_BUTTON_RELEASE_MASK
955                             | GDK_ENTER_NOTIFY_MASK
956                             | GDK_LEAVE_NOTIFY_MASK,
957                             NULL, NULL,
958                             event->time) != GDK_GRAB_SUCCESS)
959         return FALSE;
960
961       paned->in_drag = TRUE;
962       paned->priv->grab_time = event->time;
963
964       if (paned->orientation == GTK_ORIENTATION_HORIZONTAL)
965         paned->drag_pos = event->y;
966       else
967         paned->drag_pos = event->x;
968       
969       return TRUE;
970     }
971
972   return FALSE;
973 }
974
975 static gboolean
976 gtk_paned_grab_broken (GtkWidget          *widget,
977                        GdkEventGrabBroken *event)
978 {
979   GtkPaned *paned = GTK_PANED (widget);
980
981   paned->in_drag = FALSE;
982   paned->drag_pos = -1;
983   paned->position_set = TRUE;
984
985   return TRUE;
986 }
987
988 static void
989 stop_drag (GtkPaned *paned)
990 {
991   paned->in_drag = FALSE;
992   paned->drag_pos = -1;
993   paned->position_set = TRUE;
994   gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (paned)),
995                               paned->priv->grab_time);
996 }
997
998 static void
999 gtk_paned_grab_notify (GtkWidget *widget,
1000                        gboolean   was_grabbed)
1001 {
1002   GtkPaned *paned = GTK_PANED (widget);
1003
1004   if (!was_grabbed && paned->in_drag)
1005     stop_drag (paned);
1006 }
1007
1008 static void
1009 gtk_paned_state_changed (GtkWidget    *widget,
1010                          GtkStateType  previous_state)
1011 {
1012   GtkPaned *paned = GTK_PANED (widget);
1013   GdkCursor *cursor;
1014
1015   if (GTK_WIDGET_REALIZED (paned))
1016     {
1017       if (GTK_WIDGET_IS_SENSITIVE (widget))
1018         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1019                                              paned->cursor_type); 
1020       else
1021         cursor = NULL;
1022
1023       gdk_window_set_cursor (paned->handle, cursor);
1024
1025       if (cursor)
1026         gdk_cursor_unref (cursor);
1027     }
1028 }
1029
1030 static gboolean
1031 gtk_paned_button_release (GtkWidget      *widget,
1032                           GdkEventButton *event)
1033 {
1034   GtkPaned *paned = GTK_PANED (widget);
1035
1036   if (paned->in_drag && (event->button == 1))
1037     {
1038       stop_drag (paned);
1039
1040       return TRUE;
1041     }
1042
1043   return FALSE;
1044 }
1045
1046 static gboolean
1047 gtk_paned_motion (GtkWidget      *widget,
1048                   GdkEventMotion *event)
1049 {
1050   GtkPaned *paned = GTK_PANED (widget);
1051   
1052   if (paned->in_drag)
1053     {
1054       update_drag (paned);
1055       return TRUE;
1056     }
1057   
1058   return FALSE;
1059 }
1060
1061 void
1062 gtk_paned_add1 (GtkPaned  *paned,
1063                 GtkWidget *widget)
1064 {
1065   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
1066 }
1067
1068 void
1069 gtk_paned_add2 (GtkPaned  *paned,
1070                 GtkWidget *widget)
1071 {
1072   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
1073 }
1074
1075 void
1076 gtk_paned_pack1 (GtkPaned  *paned,
1077                  GtkWidget *child,
1078                  gboolean   resize,
1079                  gboolean   shrink)
1080 {
1081   g_return_if_fail (GTK_IS_PANED (paned));
1082   g_return_if_fail (GTK_IS_WIDGET (child));
1083
1084   if (!paned->child1)
1085     {
1086       paned->child1 = child;
1087       paned->child1_resize = resize;
1088       paned->child1_shrink = shrink;
1089
1090       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1091     }
1092 }
1093
1094 void
1095 gtk_paned_pack2 (GtkPaned  *paned,
1096                  GtkWidget *child,
1097                  gboolean   resize,
1098                  gboolean   shrink)
1099 {
1100   g_return_if_fail (GTK_IS_PANED (paned));
1101   g_return_if_fail (GTK_IS_WIDGET (child));
1102
1103   if (!paned->child2)
1104     {
1105       paned->child2 = child;
1106       paned->child2_resize = resize;
1107       paned->child2_shrink = shrink;
1108
1109       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1110     }
1111 }
1112
1113
1114 static void
1115 gtk_paned_add (GtkContainer *container,
1116                GtkWidget    *widget)
1117 {
1118   GtkPaned *paned;
1119
1120   g_return_if_fail (GTK_IS_PANED (container));
1121
1122   paned = GTK_PANED (container);
1123
1124   if (!paned->child1)
1125     gtk_paned_add1 (paned, widget);
1126   else if (!paned->child2)
1127     gtk_paned_add2 (paned, widget);
1128   else
1129     g_warning ("GtkPaned cannot have more than 2 children\n");
1130 }
1131
1132 static void
1133 gtk_paned_remove (GtkContainer *container,
1134                   GtkWidget    *widget)
1135 {
1136   GtkPaned *paned;
1137   gboolean was_visible;
1138
1139   paned = GTK_PANED (container);
1140   was_visible = GTK_WIDGET_VISIBLE (widget);
1141
1142   if (paned->child1 == widget)
1143     {
1144       gtk_widget_unparent (widget);
1145
1146       paned->child1 = NULL;
1147
1148       if (was_visible && GTK_WIDGET_VISIBLE (container))
1149         gtk_widget_queue_resize (GTK_WIDGET (container));
1150     }
1151   else if (paned->child2 == widget)
1152     {
1153       gtk_widget_unparent (widget);
1154
1155       paned->child2 = NULL;
1156
1157       if (was_visible && GTK_WIDGET_VISIBLE (container))
1158         gtk_widget_queue_resize (GTK_WIDGET (container));
1159     }
1160 }
1161
1162 static void
1163 gtk_paned_forall (GtkContainer *container,
1164                   gboolean      include_internals,
1165                   GtkCallback   callback,
1166                   gpointer      callback_data)
1167 {
1168   GtkPaned *paned;
1169
1170   g_return_if_fail (callback != NULL);
1171
1172   paned = GTK_PANED (container);
1173
1174   if (paned->child1)
1175     (*callback) (paned->child1, callback_data);
1176   if (paned->child2)
1177     (*callback) (paned->child2, callback_data);
1178 }
1179
1180 /**
1181  * gtk_paned_get_position:
1182  * @paned: a #GtkPaned widget
1183  * 
1184  * Obtains the position of the divider between the two panes.
1185  * 
1186  * Return value: position of the divider
1187  **/
1188 gint
1189 gtk_paned_get_position (GtkPaned  *paned)
1190 {
1191   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1192
1193   return paned->child1_size;
1194 }
1195
1196 /**
1197  * gtk_paned_set_position:
1198  * @paned: a #GtkPaned widget
1199  * @position: pixel position of divider, a negative value means that the position
1200  *            is unset.
1201  * 
1202  * Sets the position of the divider between the two panes.
1203  **/
1204 void
1205 gtk_paned_set_position (GtkPaned *paned,
1206                         gint      position)
1207 {
1208   GObject *object;
1209   
1210   g_return_if_fail (GTK_IS_PANED (paned));
1211
1212   object = G_OBJECT (paned);
1213   
1214   if (position >= 0)
1215     {
1216       /* We don't clamp here - the assumption is that
1217        * if the total allocation changes at the same time
1218        * as the position, the position set is with reference
1219        * to the new total size. If only the position changes,
1220        * then clamping will occur in gtk_paned_compute_position()
1221        */
1222
1223       paned->child1_size = position;
1224       paned->position_set = TRUE;
1225     }
1226   else
1227     {
1228       paned->position_set = FALSE;
1229     }
1230
1231   g_object_freeze_notify (object);
1232   g_object_notify (object, "position");
1233   g_object_notify (object, "position-set");
1234   g_object_thaw_notify (object);
1235
1236   gtk_widget_queue_resize (GTK_WIDGET (paned));
1237
1238 #ifdef G_OS_WIN32
1239   /* Hacky work-around for bug #144269 */
1240   if (paned->child2 != NULL)
1241     {
1242       gtk_widget_queue_draw (paned->child2);
1243     }
1244 #endif
1245 }
1246
1247 /**
1248  * gtk_paned_get_child1:
1249  * @paned: a #GtkPaned widget
1250  * 
1251  * Obtains the first child of the paned widget.
1252  * 
1253  * Return value: first child, or %NULL if it is not set.
1254  *
1255  * Since: 2.4
1256  **/
1257 GtkWidget *
1258 gtk_paned_get_child1 (GtkPaned *paned)
1259 {
1260   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1261
1262   return paned->child1;
1263 }
1264
1265 /**
1266  * gtk_paned_get_child2:
1267  * @paned: a #GtkPaned widget
1268  * 
1269  * Obtains the second child of the paned widget.
1270  * 
1271  * Return value: second child, or %NULL if it is not set.
1272  *
1273  * Since: 2.4
1274  **/
1275 GtkWidget *
1276 gtk_paned_get_child2 (GtkPaned *paned)
1277 {
1278   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1279
1280   return paned->child2;
1281 }
1282
1283 void
1284 gtk_paned_compute_position (GtkPaned *paned,
1285                             gint      allocation,
1286                             gint      child1_req,
1287                             gint      child2_req)
1288 {
1289   gint old_position;
1290   gint old_min_position;
1291   gint old_max_position;
1292   
1293   g_return_if_fail (GTK_IS_PANED (paned));
1294
1295   old_position = paned->child1_size;
1296   old_min_position = paned->min_position;
1297   old_max_position = paned->max_position;
1298
1299   paned->min_position = paned->child1_shrink ? 0 : child1_req;
1300
1301   paned->max_position = allocation;
1302   if (!paned->child2_shrink)
1303     paned->max_position = MAX (1, paned->max_position - child2_req);
1304   paned->max_position = MAX (paned->min_position, paned->max_position);
1305
1306   if (!paned->position_set)
1307     {
1308       if (paned->child1_resize && !paned->child2_resize)
1309         paned->child1_size = MAX (0, allocation - child2_req);
1310       else if (!paned->child1_resize && paned->child2_resize)
1311         paned->child1_size = child1_req;
1312       else if (child1_req + child2_req != 0)
1313         paned->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
1314       else
1315         paned->child1_size = allocation * 0.5 + 0.5;
1316     }
1317   else
1318     {
1319       /* If the position was set before the initial allocation.
1320        * (paned->last_allocation <= 0) just clamp it and leave it.
1321        */
1322       if (paned->last_allocation > 0)
1323         {
1324           if (paned->child1_resize && !paned->child2_resize)
1325             paned->child1_size += allocation - paned->last_allocation;
1326           else if (!(!paned->child1_resize && paned->child2_resize))
1327             paned->child1_size = allocation * ((gdouble) paned->child1_size / (paned->last_allocation)) + 0.5;
1328         }
1329     }
1330
1331   paned->child1_size = CLAMP (paned->child1_size,
1332                               paned->min_position,
1333                               paned->max_position);
1334
1335   if (paned->child1)
1336     gtk_widget_set_child_visible (paned->child1, paned->child1_size != 0);
1337   
1338   if (paned->child2)
1339     gtk_widget_set_child_visible (paned->child2, paned->child1_size != allocation); 
1340
1341   g_object_freeze_notify (G_OBJECT (paned));
1342   if (paned->child1_size != old_position)
1343     g_object_notify (G_OBJECT (paned), "position");
1344   if (paned->min_position != old_min_position)
1345     g_object_notify (G_OBJECT (paned), "min-position");
1346   if (paned->max_position != old_max_position)
1347     g_object_notify (G_OBJECT (paned), "max-position");
1348   g_object_thaw_notify (G_OBJECT (paned));
1349
1350   paned->last_allocation = allocation;
1351 }
1352
1353 static void
1354 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1355 {
1356   if (paned->priv->saved_focus)
1357     g_object_remove_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1358                                   (gpointer *)&(paned->priv->saved_focus));
1359
1360   paned->priv->saved_focus = widget;
1361
1362   if (paned->priv->saved_focus)
1363     g_object_add_weak_pointer (G_OBJECT (paned->priv->saved_focus),
1364                                (gpointer *)&(paned->priv->saved_focus));
1365 }
1366
1367 static void
1368 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1369 {
1370   if (paned->priv->first_paned)
1371     g_object_remove_weak_pointer (G_OBJECT (paned->priv->first_paned),
1372                                   (gpointer *)&(paned->priv->first_paned));
1373
1374   paned->priv->first_paned = first_paned;
1375
1376   if (paned->priv->first_paned)
1377     g_object_add_weak_pointer (G_OBJECT (paned->priv->first_paned),
1378                                (gpointer *)&(paned->priv->first_paned));
1379 }
1380
1381 static void
1382 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1383 {
1384   if (paned->last_child1_focus)
1385     g_object_remove_weak_pointer (G_OBJECT (paned->last_child1_focus),
1386                                   (gpointer *)&(paned->last_child1_focus));
1387
1388   paned->last_child1_focus = widget;
1389
1390   if (paned->last_child1_focus)
1391     g_object_add_weak_pointer (G_OBJECT (paned->last_child1_focus),
1392                                (gpointer *)&(paned->last_child1_focus));
1393 }
1394
1395 static void
1396 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1397 {
1398   if (paned->last_child2_focus)
1399     g_object_remove_weak_pointer (G_OBJECT (paned->last_child2_focus),
1400                                   (gpointer *)&(paned->last_child2_focus));
1401
1402   paned->last_child2_focus = widget;
1403
1404   if (paned->last_child2_focus)
1405     g_object_add_weak_pointer (G_OBJECT (paned->last_child2_focus),
1406                                (gpointer *)&(paned->last_child2_focus));
1407 }
1408
1409 static GtkWidget *
1410 paned_get_focus_widget (GtkPaned *paned)
1411 {
1412   GtkWidget *toplevel;
1413
1414   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1415   if (GTK_WIDGET_TOPLEVEL (toplevel))
1416     return GTK_WINDOW (toplevel)->focus_widget;
1417
1418   return NULL;
1419 }
1420
1421 static void
1422 gtk_paned_set_focus_child (GtkContainer *container,
1423                            GtkWidget    *focus_child)
1424 {
1425   GtkPaned *paned;
1426   
1427   g_return_if_fail (GTK_IS_PANED (container));
1428
1429   paned = GTK_PANED (container);
1430  
1431   if (focus_child == NULL)
1432     {
1433       GtkWidget *last_focus;
1434       GtkWidget *w;
1435       
1436       last_focus = paned_get_focus_widget (paned);
1437
1438       if (last_focus)
1439         {
1440           /* If there is one or more paned widgets between us and the
1441            * focus widget, we want the topmost of those as last_focus
1442            */
1443           for (w = last_focus; w != GTK_WIDGET (paned); w = w->parent)
1444             if (GTK_IS_PANED (w))
1445               last_focus = w;
1446           
1447           if (container->focus_child == paned->child1)
1448             gtk_paned_set_last_child1_focus (paned, last_focus);
1449           else if (container->focus_child == paned->child2)
1450             gtk_paned_set_last_child2_focus (paned, last_focus);
1451         }
1452     }
1453
1454   if (GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child)
1455     (* GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child) (container, focus_child);
1456 }
1457
1458 static void
1459 gtk_paned_get_cycle_chain (GtkPaned          *paned,
1460                            GtkDirectionType   direction,
1461                            GList            **widgets)
1462 {
1463   GtkContainer *container = GTK_CONTAINER (paned);
1464   GtkWidget *ancestor = NULL;
1465   GList *temp_list = NULL;
1466   GList *list;
1467
1468   if (paned->in_recursion)
1469     return;
1470
1471   g_assert (widgets != NULL);
1472
1473   if (paned->last_child1_focus &&
1474       !gtk_widget_is_ancestor (paned->last_child1_focus, GTK_WIDGET (paned)))
1475     {
1476       gtk_paned_set_last_child1_focus (paned, NULL);
1477     }
1478
1479   if (paned->last_child2_focus &&
1480       !gtk_widget_is_ancestor (paned->last_child2_focus, GTK_WIDGET (paned)))
1481     {
1482       gtk_paned_set_last_child2_focus (paned, NULL);
1483     }
1484
1485   if (GTK_WIDGET (paned)->parent)
1486     ancestor = gtk_widget_get_ancestor (GTK_WIDGET (paned)->parent, GTK_TYPE_PANED);
1487
1488   /* The idea here is that temp_list is a list of widgets we want to cycle
1489    * to. The list is prioritized so that the first element is our first
1490    * choice, the next our second, and so on.
1491    *
1492    * We can't just use g_list_reverse(), because we want to try
1493    * paned->last_child?_focus before paned->child?, both when we
1494    * are going forward and backward.
1495    */
1496   if (direction == GTK_DIR_TAB_FORWARD)
1497     {
1498       if (container->focus_child == paned->child1)
1499         {
1500           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1501           temp_list = g_list_append (temp_list, paned->child2);
1502           temp_list = g_list_append (temp_list, ancestor);
1503         }
1504       else if (container->focus_child == paned->child2)
1505         {
1506           temp_list = g_list_append (temp_list, ancestor);
1507           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1508           temp_list = g_list_append (temp_list, paned->child1);
1509         }
1510       else
1511         {
1512           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1513           temp_list = g_list_append (temp_list, paned->child1);
1514           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1515           temp_list = g_list_append (temp_list, paned->child2);
1516           temp_list = g_list_append (temp_list, ancestor);
1517         }
1518     }
1519   else
1520     {
1521       if (container->focus_child == paned->child1)
1522         {
1523           temp_list = g_list_append (temp_list, ancestor);
1524           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1525           temp_list = g_list_append (temp_list, paned->child2);
1526         }
1527       else if (container->focus_child == paned->child2)
1528         {
1529           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1530           temp_list = g_list_append (temp_list, paned->child1);
1531           temp_list = g_list_append (temp_list, ancestor);
1532         }
1533       else
1534         {
1535           temp_list = g_list_append (temp_list, paned->last_child2_focus);
1536           temp_list = g_list_append (temp_list, paned->child2);
1537           temp_list = g_list_append (temp_list, paned->last_child1_focus);
1538           temp_list = g_list_append (temp_list, paned->child1);
1539           temp_list = g_list_append (temp_list, ancestor);
1540         }
1541     }
1542
1543   /* Walk the list and expand all the paned widgets. */
1544   for (list = temp_list; list != NULL; list = list->next)
1545     {
1546       GtkWidget *widget = list->data;
1547
1548       if (widget)
1549         {
1550           if (GTK_IS_PANED (widget))
1551             {
1552               paned->in_recursion = TRUE;
1553               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
1554               paned->in_recursion = FALSE;
1555             }
1556           else
1557             {
1558               *widgets = g_list_append (*widgets, widget);
1559             }
1560         }
1561     }
1562
1563   g_list_free (temp_list);
1564 }
1565
1566 static gboolean
1567 gtk_paned_cycle_child_focus (GtkPaned *paned,
1568                              gboolean  reversed)
1569 {
1570   GList *cycle_chain = NULL;
1571   GList *list;
1572   
1573   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
1574
1575   /* ignore f6 if the handle is focused */
1576   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1577     return TRUE;
1578   
1579   /* we can't just let the event propagate up the hierarchy,
1580    * because the paned will want to cycle focus _unless_ an
1581    * ancestor paned handles the event
1582    */
1583   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
1584
1585   for (list = cycle_chain; list != NULL; list = list->next)
1586     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
1587       break;
1588
1589   g_list_free (cycle_chain);
1590   
1591   return TRUE;
1592 }
1593
1594 static void
1595 get_child_panes (GtkWidget  *widget,
1596                  GList     **panes)
1597 {
1598   if (GTK_IS_PANED (widget))
1599     {
1600       GtkPaned *paned = GTK_PANED (widget);
1601       
1602       get_child_panes (paned->child1, panes);
1603       *panes = g_list_prepend (*panes, widget);
1604       get_child_panes (paned->child2, panes);
1605     }
1606   else if (GTK_IS_CONTAINER (widget))
1607     {
1608       gtk_container_forall (GTK_CONTAINER (widget),
1609                              (GtkCallback)get_child_panes, panes);
1610     }
1611 }
1612
1613 static GList *
1614 get_all_panes (GtkPaned *paned)
1615 {
1616   GtkPaned *topmost = NULL;
1617   GList *result = NULL;
1618   GtkWidget *w;
1619   
1620   for (w = GTK_WIDGET (paned); w != NULL; w = w->parent)
1621     {
1622       if (GTK_IS_PANED (w))
1623         topmost = GTK_PANED (w);
1624     }
1625
1626   g_assert (topmost);
1627
1628   get_child_panes (GTK_WIDGET (topmost), &result);
1629
1630   return g_list_reverse (result);
1631 }
1632
1633 static void
1634 gtk_paned_find_neighbours (GtkPaned  *paned,
1635                            GtkPaned **next,
1636                            GtkPaned **prev)
1637 {
1638   GList *all_panes;
1639   GList *this_link;
1640
1641   all_panes = get_all_panes (paned);
1642   g_assert (all_panes);
1643
1644   this_link = g_list_find (all_panes, paned);
1645
1646   g_assert (this_link);
1647   
1648   if (this_link->next)
1649     *next = this_link->next->data;
1650   else
1651     *next = all_panes->data;
1652
1653   if (this_link->prev)
1654     *prev = this_link->prev->data;
1655   else
1656     *prev = g_list_last (all_panes)->data;
1657
1658   g_list_free (all_panes);
1659 }
1660
1661 static gboolean
1662 gtk_paned_move_handle (GtkPaned      *paned,
1663                        GtkScrollType  scroll)
1664 {
1665   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1666     {
1667       gint old_position;
1668       gint new_position;
1669       gint increment;
1670       
1671       enum {
1672         SINGLE_STEP_SIZE = 1,
1673         PAGE_STEP_SIZE   = 75
1674       };
1675       
1676       new_position = old_position = gtk_paned_get_position (paned);
1677       increment = 0;
1678       
1679       switch (scroll)
1680         {
1681         case GTK_SCROLL_STEP_LEFT:
1682         case GTK_SCROLL_STEP_UP:
1683         case GTK_SCROLL_STEP_BACKWARD:
1684           increment = - SINGLE_STEP_SIZE;
1685           break;
1686           
1687         case GTK_SCROLL_STEP_RIGHT:
1688         case GTK_SCROLL_STEP_DOWN:
1689         case GTK_SCROLL_STEP_FORWARD:
1690           increment = SINGLE_STEP_SIZE;
1691           break;
1692           
1693         case GTK_SCROLL_PAGE_LEFT:
1694         case GTK_SCROLL_PAGE_UP:
1695         case GTK_SCROLL_PAGE_BACKWARD:
1696           increment = - PAGE_STEP_SIZE;
1697           break;
1698           
1699         case GTK_SCROLL_PAGE_RIGHT:
1700         case GTK_SCROLL_PAGE_DOWN:
1701         case GTK_SCROLL_PAGE_FORWARD:
1702           increment = PAGE_STEP_SIZE;
1703           break;
1704           
1705         case GTK_SCROLL_START:
1706           new_position = paned->min_position;
1707           break;
1708           
1709         case GTK_SCROLL_END:
1710           new_position = paned->max_position;
1711           break;
1712
1713         default:
1714           break;
1715         }
1716
1717       if (increment)
1718         {
1719           if (is_rtl (paned))
1720             increment = -increment;
1721           
1722           new_position = old_position + increment;
1723         }
1724       
1725       new_position = CLAMP (new_position, paned->min_position, paned->max_position);
1726       
1727       if (old_position != new_position)
1728         gtk_paned_set_position (paned, new_position);
1729
1730       return TRUE;
1731     }
1732
1733   return FALSE;
1734 }
1735
1736 static void
1737 gtk_paned_restore_focus (GtkPaned *paned)
1738 {
1739   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1740     {
1741       if (paned->priv->saved_focus &&
1742           GTK_WIDGET_SENSITIVE (paned->priv->saved_focus))
1743         {
1744           gtk_widget_grab_focus (paned->priv->saved_focus);
1745         }
1746       else
1747         {
1748           /* the saved focus is somehow not available for focusing,
1749            * try
1750            *   1) tabbing into the paned widget
1751            * if that didn't work,
1752            *   2) unset focus for the window if there is one
1753            */
1754           
1755           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
1756             {
1757               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1758               
1759               if (GTK_IS_WINDOW (toplevel))
1760                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
1761             }
1762         }
1763       
1764       gtk_paned_set_saved_focus (paned, NULL);
1765       gtk_paned_set_first_paned (paned, NULL);
1766     }
1767 }
1768
1769 static gboolean
1770 gtk_paned_accept_position (GtkPaned *paned)
1771 {
1772   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1773     {
1774       paned->original_position = -1;
1775       gtk_paned_restore_focus (paned);
1776
1777       return TRUE;
1778     }
1779
1780   return FALSE;
1781 }
1782
1783
1784 static gboolean
1785 gtk_paned_cancel_position (GtkPaned *paned)
1786 {
1787   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1788     {
1789       if (paned->original_position != -1)
1790         {
1791           gtk_paned_set_position (paned, paned->original_position);
1792           paned->original_position = -1;
1793         }
1794
1795       gtk_paned_restore_focus (paned);
1796       return TRUE;
1797     }
1798
1799   return FALSE;
1800 }
1801
1802 static gboolean
1803 gtk_paned_cycle_handle_focus (GtkPaned *paned,
1804                               gboolean  reversed)
1805 {
1806   GtkPaned *next, *prev;
1807   
1808   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1809     {
1810       GtkPaned *focus = NULL;
1811
1812       if (!paned->priv->first_paned)
1813         {
1814           /* The first_pane has disappeared. As an ad-hoc solution,
1815            * we make the currently focused paned the first_paned. To the
1816            * user this will seem like the paned cycling has been reset.
1817            */
1818           
1819           gtk_paned_set_first_paned (paned, paned);
1820         }
1821       
1822       gtk_paned_find_neighbours (paned, &next, &prev);
1823
1824       if (reversed && prev &&
1825           prev != paned && paned != paned->priv->first_paned)
1826         {
1827           focus = prev;
1828         }
1829       else if (!reversed && next &&
1830                next != paned && next != paned->priv->first_paned)
1831         {
1832           focus = next;
1833         }
1834       else
1835         {
1836           gtk_paned_accept_position (paned);
1837           return TRUE;
1838         }
1839
1840       g_assert (focus);
1841       
1842       gtk_paned_set_saved_focus (focus, paned->priv->saved_focus);
1843       gtk_paned_set_first_paned (focus, paned->priv->first_paned);
1844       
1845       gtk_paned_set_saved_focus (paned, NULL);
1846       gtk_paned_set_first_paned (paned, NULL);
1847       
1848       gtk_widget_grab_focus (GTK_WIDGET (focus));
1849       
1850       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
1851         {
1852           paned->original_position = -1;
1853           focus->original_position = gtk_paned_get_position (focus);
1854         }
1855     }
1856   else
1857     {
1858       GtkContainer *container = GTK_CONTAINER (paned);
1859       GtkPaned *focus;
1860       GtkPaned *first;
1861       GtkPaned *prev, *next;
1862       GtkWidget *toplevel;
1863
1864       gtk_paned_find_neighbours (paned, &next, &prev);
1865
1866       if (container->focus_child == paned->child1)
1867         {
1868           if (reversed)
1869             {
1870               focus = prev;
1871               first = paned;
1872             }
1873           else
1874             {
1875               focus = paned;
1876               first = paned;
1877             }
1878         }
1879       else if (container->focus_child == paned->child2)
1880         {
1881           if (reversed)
1882             {
1883               focus = paned;
1884               first = next;
1885             }
1886           else
1887             {
1888               focus = next;
1889               first = next;
1890             }
1891         }
1892       else
1893         {
1894           /* Focus is not inside this paned, and we don't have focus.
1895            * Presumably this happened because the application wants us
1896            * to start keyboard navigating.
1897            */
1898           focus = paned;
1899
1900           if (reversed)
1901             first = paned;
1902           else
1903             first = next;
1904         }
1905
1906       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1907
1908       if (GTK_IS_WINDOW (toplevel))
1909         gtk_paned_set_saved_focus (focus, GTK_WINDOW (toplevel)->focus_widget);
1910       gtk_paned_set_first_paned (focus, first);
1911       focus->original_position = gtk_paned_get_position (focus); 
1912
1913       gtk_widget_grab_focus (GTK_WIDGET (focus));
1914    }
1915   
1916   return TRUE;
1917 }
1918
1919 static gboolean
1920 gtk_paned_toggle_handle_focus (GtkPaned *paned)
1921 {
1922   /* This function/signal has the wrong name. It is called when you
1923    * press Tab or Shift-Tab and what we do is act as if
1924    * the user pressed Return and then Tab or Shift-Tab
1925    */
1926   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1927     gtk_paned_accept_position (paned);
1928
1929   return FALSE;
1930 }
1931
1932 #define __GTK_PANED_C__
1933 #include "gtkaliasdef.c"