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