]> Pileus Git - ~andy/gtk/blob - gtk/gtkpaned.c
Tons of transfer annotations
[~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
29 #include "gtkpaned.h"
30
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkbindings.h"
33 #include "gtkmain.h"
34 #include "gtkmarshalers.h"
35 #include "gtkorientable.h"
36 #include "gtksizerequest.h"
37 #include "gtkwindow.h"
38
39 #include "gtkprivate.h"
40 #include "gtkintl.h"
41
42
43
44 struct _GtkPanedPrivate
45 {
46   GtkOrientation  orientation;
47   GtkPaned       *first_paned;
48   GtkWidget      *child1;
49   GtkWidget      *child2;
50   GtkWidget      *last_child1_focus;
51   GtkWidget      *last_child2_focus;
52   GtkWidget      *saved_focus;
53
54   GdkCursorType  cursor_type;
55   GdkDevice     *grab_device;
56   GdkRectangle   handle_pos;
57   GdkWindow     *handle;
58
59   gint          child1_size;
60   gint          drag_pos;
61   gint          last_allocation;
62   gint          max_position;
63   gint          min_position;
64   gint          original_position;
65
66   guint         handle_prelit : 1;
67   guint         in_drag       : 1;
68   guint         in_recursion  : 1;
69   guint         child1_resize : 1;
70   guint         child1_shrink : 1;
71   guint         child2_resize : 1;
72   guint         child2_shrink : 1;
73   guint         position_set  : 1;
74
75   guint32       grab_time;
76 };
77
78 enum {
79   PROP_0,
80   PROP_ORIENTATION,
81   PROP_POSITION,
82   PROP_POSITION_SET,
83   PROP_MIN_POSITION,
84   PROP_MAX_POSITION
85 };
86
87 enum {
88   CHILD_PROP_0,
89   CHILD_PROP_RESIZE,
90   CHILD_PROP_SHRINK
91 };
92
93 enum {
94   CYCLE_CHILD_FOCUS,
95   TOGGLE_HANDLE_FOCUS,
96   MOVE_HANDLE,
97   CYCLE_HANDLE_FOCUS,
98   ACCEPT_POSITION,
99   CANCEL_POSITION,
100   LAST_SIGNAL
101 };
102
103 static void     gtk_paned_set_property          (GObject          *object,
104                                                  guint             prop_id,
105                                                  const GValue     *value,
106                                                  GParamSpec       *pspec);
107 static void     gtk_paned_get_property          (GObject          *object,
108                                                  guint             prop_id,
109                                                  GValue           *value,
110                                                  GParamSpec       *pspec);
111 static void     gtk_paned_set_child_property    (GtkContainer     *container,
112                                                  GtkWidget        *child,
113                                                  guint             property_id,
114                                                  const GValue     *value,
115                                                  GParamSpec       *pspec);
116 static void     gtk_paned_get_child_property    (GtkContainer     *container,
117                                                  GtkWidget        *child,
118                                                  guint             property_id,
119                                                  GValue           *value,
120                                                  GParamSpec       *pspec);
121 static void     gtk_paned_finalize              (GObject          *object);
122
123 static void     gtk_paned_size_request          (GtkWidget        *widget,
124                                                  GtkRequisition   *requisition);
125 static void     gtk_paned_size_allocate         (GtkWidget        *widget,
126                                                  GtkAllocation    *allocation);
127 static void     gtk_paned_realize               (GtkWidget        *widget);
128 static void     gtk_paned_unrealize             (GtkWidget        *widget);
129 static void     gtk_paned_map                   (GtkWidget        *widget);
130 static void     gtk_paned_unmap                 (GtkWidget        *widget);
131 static void     gtk_paned_state_changed         (GtkWidget        *widget,
132                                                  GtkStateType      previous_state);
133 static gboolean gtk_paned_expose                (GtkWidget        *widget,
134                                                  GdkEventExpose   *event);
135 static gboolean gtk_paned_enter                 (GtkWidget        *widget,
136                                                  GdkEventCrossing *event);
137 static gboolean gtk_paned_leave                 (GtkWidget        *widget,
138                                                  GdkEventCrossing *event);
139 static gboolean gtk_paned_button_press          (GtkWidget        *widget,
140                                                  GdkEventButton   *event);
141 static gboolean gtk_paned_button_release        (GtkWidget        *widget,
142                                                  GdkEventButton   *event);
143 static gboolean gtk_paned_motion                (GtkWidget        *widget,
144                                                  GdkEventMotion   *event);
145 static gboolean gtk_paned_focus                 (GtkWidget        *widget,
146                                                  GtkDirectionType  direction);
147 static gboolean gtk_paned_grab_broken           (GtkWidget          *widget,
148                                                  GdkEventGrabBroken *event);
149 static void     gtk_paned_add                   (GtkContainer     *container,
150                                                  GtkWidget        *widget);
151 static void     gtk_paned_remove                (GtkContainer     *container,
152                                                  GtkWidget        *widget);
153 static void     gtk_paned_forall                (GtkContainer     *container,
154                                                  gboolean          include_internals,
155                                                  GtkCallback       callback,
156                                                  gpointer          callback_data);
157 static void     gtk_paned_calc_position         (GtkPaned         *paned,
158                                                  gint              allocation,
159                                                  gint              child1_req,
160                                                  gint              child2_req);
161 static void     gtk_paned_set_focus_child       (GtkContainer     *container,
162                                                  GtkWidget        *child);
163 static void     gtk_paned_set_saved_focus       (GtkPaned         *paned,
164                                                  GtkWidget        *widget);
165 static void     gtk_paned_set_first_paned       (GtkPaned         *paned,
166                                                  GtkPaned         *first_paned);
167 static void     gtk_paned_set_last_child1_focus (GtkPaned         *paned,
168                                                  GtkWidget        *widget);
169 static void     gtk_paned_set_last_child2_focus (GtkPaned         *paned,
170                                                  GtkWidget        *widget);
171 static gboolean gtk_paned_cycle_child_focus     (GtkPaned         *paned,
172                                                  gboolean          reverse);
173 static gboolean gtk_paned_cycle_handle_focus    (GtkPaned         *paned,
174                                                  gboolean          reverse);
175 static gboolean gtk_paned_move_handle           (GtkPaned         *paned,
176                                                  GtkScrollType     scroll);
177 static gboolean gtk_paned_accept_position       (GtkPaned         *paned);
178 static gboolean gtk_paned_cancel_position       (GtkPaned         *paned);
179 static gboolean gtk_paned_toggle_handle_focus   (GtkPaned         *paned);
180
181 static GType    gtk_paned_child_type            (GtkContainer     *container);
182 static void     gtk_paned_grab_notify           (GtkWidget        *widget,
183                                                  gboolean          was_grabbed);
184
185
186 G_DEFINE_TYPE_WITH_CODE (GtkPaned, gtk_paned, GTK_TYPE_CONTAINER,
187                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
188                                                 NULL))
189
190 static guint signals[LAST_SIGNAL] = { 0 };
191
192
193 static void
194 add_tab_bindings (GtkBindingSet    *binding_set,
195                   GdkModifierType   modifiers)
196 {
197   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
198                                 "toggle-handle-focus", 0);
199   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
200                                 "toggle-handle-focus", 0);
201 }
202
203 static void
204 add_move_binding (GtkBindingSet   *binding_set,
205                   guint            keyval,
206                   GdkModifierType  mask,
207                   GtkScrollType    scroll)
208 {
209   gtk_binding_entry_add_signal (binding_set, keyval, mask,
210                                 "move-handle", 1,
211                                 GTK_TYPE_SCROLL_TYPE, scroll);
212 }
213
214 static void
215 gtk_paned_class_init (GtkPanedClass *class)
216 {
217   GObjectClass *object_class;
218   GtkWidgetClass *widget_class;
219   GtkContainerClass *container_class;
220   GtkPanedClass *paned_class;
221   GtkBindingSet *binding_set;
222
223   object_class = (GObjectClass *) class;
224   widget_class = (GtkWidgetClass *) class;
225   container_class = (GtkContainerClass *) class;
226   paned_class = (GtkPanedClass *) class;
227
228   object_class->set_property = gtk_paned_set_property;
229   object_class->get_property = gtk_paned_get_property;
230   object_class->finalize = gtk_paned_finalize;
231
232   widget_class->size_request = gtk_paned_size_request;
233   widget_class->size_allocate = gtk_paned_size_allocate;
234   widget_class->realize = gtk_paned_realize;
235   widget_class->unrealize = gtk_paned_unrealize;
236   widget_class->map = gtk_paned_map;
237   widget_class->unmap = gtk_paned_unmap;
238   widget_class->expose_event = gtk_paned_expose;
239   widget_class->focus = gtk_paned_focus;
240   widget_class->enter_notify_event = gtk_paned_enter;
241   widget_class->leave_notify_event = gtk_paned_leave;
242   widget_class->button_press_event = gtk_paned_button_press;
243   widget_class->button_release_event = gtk_paned_button_release;
244   widget_class->motion_notify_event = gtk_paned_motion;
245   widget_class->grab_broken_event = gtk_paned_grab_broken;
246   widget_class->grab_notify = gtk_paned_grab_notify;
247   widget_class->state_changed = gtk_paned_state_changed;
248
249   container_class->add = gtk_paned_add;
250   container_class->remove = gtk_paned_remove;
251   container_class->forall = gtk_paned_forall;
252   container_class->child_type = gtk_paned_child_type;
253   container_class->set_focus_child = gtk_paned_set_focus_child;
254   container_class->set_child_property = gtk_paned_set_child_property;
255   container_class->get_child_property = gtk_paned_get_child_property;
256
257   paned_class->cycle_child_focus = gtk_paned_cycle_child_focus;
258   paned_class->toggle_handle_focus = gtk_paned_toggle_handle_focus;
259   paned_class->move_handle = gtk_paned_move_handle;
260   paned_class->cycle_handle_focus = gtk_paned_cycle_handle_focus;
261   paned_class->accept_position = gtk_paned_accept_position;
262   paned_class->cancel_position = gtk_paned_cancel_position;
263
264   g_object_class_override_property (object_class,
265                                     PROP_ORIENTATION,
266                                     "orientation");
267
268   g_object_class_install_property (object_class,
269                                    PROP_POSITION,
270                                    g_param_spec_int ("position",
271                                                      P_("Position"),
272                                                      P_("Position of paned separator in pixels (0 means all the way to the left/top)"),
273                                                      0,
274                                                      G_MAXINT,
275                                                      0,
276                                                      GTK_PARAM_READWRITE));
277
278   g_object_class_install_property (object_class,
279                                    PROP_POSITION_SET,
280                                    g_param_spec_boolean ("position-set",
281                                                          P_("Position Set"),
282                                                          P_("TRUE if the Position property should be used"),
283                                                          FALSE,
284                                                          GTK_PARAM_READWRITE));
285
286   gtk_widget_class_install_style_property (widget_class,
287                                            g_param_spec_int ("handle-size",
288                                                              P_("Handle Size"),
289                                                              P_("Width of handle"),
290                                                              0,
291                                                              G_MAXINT,
292                                                              5,
293                                                              GTK_PARAM_READABLE));
294   /**
295    * GtkPaned:min-position:
296    *
297    * The smallest possible value for the position property. This property is derived from the
298    * size and shrinkability of the widget's children.
299    *
300    * Since: 2.4
301    */
302   g_object_class_install_property (object_class,
303                                    PROP_MIN_POSITION,
304                                    g_param_spec_int ("min-position",
305                                                      P_("Minimal Position"),
306                                                      P_("Smallest possible value for the \"position\" property"),
307                                                      0,
308                                                      G_MAXINT,
309                                                      0,
310                                                      GTK_PARAM_READABLE));
311
312   /**
313    * GtkPaned:max-position:
314    *
315    * The largest possible value for the position property. This property is derived from the
316    * size and shrinkability of the widget's children.
317    *
318    * Since: 2.4
319    */
320   g_object_class_install_property (object_class,
321                                    PROP_MAX_POSITION,
322                                    g_param_spec_int ("max-position",
323                                                      P_("Maximal Position"),
324                                                      P_("Largest possible value for the \"position\" property"),
325                                                      0,
326                                                      G_MAXINT,
327                                                      G_MAXINT,
328                                                      GTK_PARAM_READABLE));
329
330   /**
331    * GtkPaned:resize:
332    *
333    * The "resize" child property determines whether the child expands and
334    * shrinks along with the paned widget.
335    *
336    * Since: 2.4
337    */
338   gtk_container_class_install_child_property (container_class,
339                                               CHILD_PROP_RESIZE,
340                                               g_param_spec_boolean ("resize", 
341                                                                     P_("Resize"),
342                                                                     P_("If TRUE, the child expands and shrinks along with the paned widget"),
343                                                                     TRUE,
344                                                                     GTK_PARAM_READWRITE));
345
346   /**
347    * GtkPaned:shrink:
348    *
349    * The "shrink" child property determines whether the child can be made
350    * smaller than its requisition.
351    *
352    * Since: 2.4
353    */
354   gtk_container_class_install_child_property (container_class,
355                                               CHILD_PROP_SHRINK,
356                                               g_param_spec_boolean ("shrink", 
357                                                                     P_("Shrink"),
358                                                                     P_("If TRUE, the child can be made smaller than its requisition"),
359                                                                     TRUE,
360                                                                     GTK_PARAM_READWRITE));
361
362   /**
363    * GtkPaned::cycle-child-focus:
364    * @widget: the object that received the signal
365    * @reversed: whether cycling backward or forward
366    *
367    * The ::cycle-child-focus signal is a 
368    * <link linkend="keybinding-signals">keybinding signal</link>
369    * which gets emitted to cycle the focus between the children of the paned.
370    *
371    * The default binding is f6.
372    *
373    * Since: 2.0
374    */
375   signals [CYCLE_CHILD_FOCUS] =
376     g_signal_new (I_("cycle-child-focus"),
377                   G_TYPE_FROM_CLASS (object_class),
378                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
379                   G_STRUCT_OFFSET (GtkPanedClass, cycle_child_focus),
380                   NULL, NULL,
381                   _gtk_marshal_BOOLEAN__BOOLEAN,
382                   G_TYPE_BOOLEAN, 1,
383                   G_TYPE_BOOLEAN);
384
385   /**
386    * GtkPaned::toggle-handle-focus:
387    * @widget: the object that received the signal
388    *
389    * The ::toggle-handle-focus is a 
390    * <link linkend="keybinding-signals">keybinding signal</link>
391    * which gets emitted to accept the current position of the handle and then 
392    * move focus to the next widget in the focus chain.
393    *
394    * The default binding is Tab.
395    *
396    * Since: 2.0
397    */
398   signals [TOGGLE_HANDLE_FOCUS] =
399     g_signal_new (I_("toggle-handle-focus"),
400                   G_TYPE_FROM_CLASS (object_class),
401                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
402                   G_STRUCT_OFFSET (GtkPanedClass, toggle_handle_focus),
403                   NULL, NULL,
404                   _gtk_marshal_BOOLEAN__VOID,
405                   G_TYPE_BOOLEAN, 0);
406
407   /**
408    * GtkPaned::move-handle:
409    * @widget: the object that received the signal
410    * @scroll_type: a #GtkScrollType
411    *
412    * The ::move-handle signal is a 
413    * <link linkend="keybinding-signals">keybinding signal</link>
414    * which gets emitted to move the handle when the user is using key bindings 
415    * to move it.
416    *
417    * Since: 2.0
418    */
419   signals[MOVE_HANDLE] =
420     g_signal_new (I_("move-handle"),
421                   G_TYPE_FROM_CLASS (object_class),
422                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
423                   G_STRUCT_OFFSET (GtkPanedClass, move_handle),
424                   NULL, NULL,
425                   _gtk_marshal_BOOLEAN__ENUM,
426                   G_TYPE_BOOLEAN, 1,
427                   GTK_TYPE_SCROLL_TYPE);
428
429   /**
430    * GtkPaned::cycle-handle-focus:
431    * @widget: the object that received the signal
432    * @reversed: whether cycling backward or forward
433    *
434    * The ::cycle-handle-focus signal is a 
435    * <link linkend="keybinding-signals">keybinding signal</link>
436    * which gets emitted to cycle whether the paned should grab focus to allow
437    * the user to change position of the handle by using key bindings.
438    *
439    * The default binding for this signal is f8.
440    *
441    * Since: 2.0
442    */
443   signals [CYCLE_HANDLE_FOCUS] =
444     g_signal_new (I_("cycle-handle-focus"),
445                   G_TYPE_FROM_CLASS (object_class),
446                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
447                   G_STRUCT_OFFSET (GtkPanedClass, cycle_handle_focus),
448                   NULL, NULL,
449                   _gtk_marshal_BOOLEAN__BOOLEAN,
450                   G_TYPE_BOOLEAN, 1,
451                   G_TYPE_BOOLEAN);
452
453   /**
454    * GtkPaned::accept-position:
455    * @widget: the object that received the signal
456    *
457    * The ::accept-position signal is a 
458    * <link linkend="keybinding-signals">keybinding signal</link>
459    * which gets emitted to accept the current position of the handle when 
460    * moving it using key bindings.
461    *
462    * The default binding for this signal is Return or Space.
463    *
464    * Since: 2.0
465    */
466   signals [ACCEPT_POSITION] =
467     g_signal_new (I_("accept-position"),
468                   G_TYPE_FROM_CLASS (object_class),
469                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
470                   G_STRUCT_OFFSET (GtkPanedClass, accept_position),
471                   NULL, NULL,
472                   _gtk_marshal_BOOLEAN__VOID,
473                   G_TYPE_BOOLEAN, 0);
474
475   /**
476    * GtkPaned::cancel-position:
477    * @widget: the object that received the signal
478    *
479    * The ::cancel-position signal is a 
480    * <link linkend="keybinding-signals">keybinding signal</link>
481    * which gets emitted to cancel moving the position of the handle using key 
482    * bindings. The position of the handle will be reset to the value prior to 
483    * moving it.
484    *
485    * The default binding for this signal is Escape.
486    *
487    * Since: 2.0
488    */
489   signals [CANCEL_POSITION] =
490     g_signal_new (I_("cancel-position"),
491                   G_TYPE_FROM_CLASS (object_class),
492                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
493                   G_STRUCT_OFFSET (GtkPanedClass, cancel_position),
494                   NULL, NULL,
495                   _gtk_marshal_BOOLEAN__VOID,
496                   G_TYPE_BOOLEAN, 0);
497
498   binding_set = gtk_binding_set_by_class (class);
499
500   /* F6 and friends */
501   gtk_binding_entry_add_signal (binding_set,
502                                 GDK_KEY_F6, 0,
503                                 "cycle-child-focus", 1, 
504                                 G_TYPE_BOOLEAN, FALSE);
505   gtk_binding_entry_add_signal (binding_set,
506                                 GDK_KEY_F6, GDK_SHIFT_MASK,
507                                 "cycle-child-focus", 1,
508                                 G_TYPE_BOOLEAN, TRUE);
509
510   /* F8 and friends */
511   gtk_binding_entry_add_signal (binding_set,
512                                 GDK_KEY_F8, 0,
513                                 "cycle-handle-focus", 1,
514                                 G_TYPE_BOOLEAN, FALSE);
515  
516   gtk_binding_entry_add_signal (binding_set,
517                                 GDK_KEY_F8, GDK_SHIFT_MASK,
518                                 "cycle-handle-focus", 1,
519                                 G_TYPE_BOOLEAN, TRUE);
520  
521   add_tab_bindings (binding_set, 0);
522   add_tab_bindings (binding_set, GDK_CONTROL_MASK);
523   add_tab_bindings (binding_set, GDK_SHIFT_MASK);
524   add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK);
525
526   /* accept and cancel positions */
527   gtk_binding_entry_add_signal (binding_set,
528                                 GDK_KEY_Escape, 0,
529                                 "cancel-position", 0);
530
531   gtk_binding_entry_add_signal (binding_set,
532                                 GDK_KEY_Return, 0,
533                                 "accept-position", 0);
534   gtk_binding_entry_add_signal (binding_set,
535                                 GDK_KEY_ISO_Enter, 0,
536                                 "accept-position", 0);
537   gtk_binding_entry_add_signal (binding_set,
538                                 GDK_KEY_KP_Enter, 0,
539                                 "accept-position", 0);
540   gtk_binding_entry_add_signal (binding_set,
541                                 GDK_KEY_space, 0,
542                                 "accept-position", 0);
543   gtk_binding_entry_add_signal (binding_set,
544                                 GDK_KEY_KP_Space, 0,
545                                 "accept-position", 0);
546
547   /* move handle */
548   add_move_binding (binding_set, GDK_KEY_Left, 0, GTK_SCROLL_STEP_LEFT);
549   add_move_binding (binding_set, GDK_KEY_KP_Left, 0, GTK_SCROLL_STEP_LEFT);
550   add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
551   add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
552
553   add_move_binding (binding_set, GDK_KEY_Right, 0, GTK_SCROLL_STEP_RIGHT);
554   add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
555   add_move_binding (binding_set, GDK_KEY_KP_Right, 0, GTK_SCROLL_STEP_RIGHT);
556   add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
557
558   add_move_binding (binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_UP);
559   add_move_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
560   add_move_binding (binding_set, GDK_KEY_KP_Up, 0, GTK_SCROLL_STEP_UP);
561   add_move_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
562   add_move_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_UP);
563   add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0, GTK_SCROLL_PAGE_UP);
564
565   add_move_binding (binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_DOWN);
566   add_move_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
567   add_move_binding (binding_set, GDK_KEY_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
568   add_move_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
569   add_move_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
570   add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
571
572   add_move_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START);
573   add_move_binding (binding_set, GDK_KEY_KP_Home, 0, GTK_SCROLL_START);
574   add_move_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END);
575   add_move_binding (binding_set, GDK_KEY_KP_End, 0, GTK_SCROLL_END);
576
577   g_type_class_add_private (object_class, sizeof (GtkPanedPrivate));
578 }
579
580 static GType
581 gtk_paned_child_type (GtkContainer *container)
582 {
583   GtkPaned *paned = GTK_PANED (container);
584   GtkPanedPrivate *priv = paned->priv;
585
586   if (!priv->child1 || !priv->child2)
587     return GTK_TYPE_WIDGET;
588   else
589     return G_TYPE_NONE;
590 }
591
592 static void
593 gtk_paned_init (GtkPaned *paned)
594 {
595   GtkPanedPrivate *priv;
596
597   gtk_widget_set_has_window (GTK_WIDGET (paned), FALSE);
598   gtk_widget_set_can_focus (GTK_WIDGET (paned), TRUE);
599
600   /* We only need to redraw when the handle position moves, which is
601    * independent of the overall allocation of the GtkPaned
602    */
603   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (paned), FALSE);
604
605   paned->priv = G_TYPE_INSTANCE_GET_PRIVATE (paned, GTK_TYPE_PANED, GtkPanedPrivate);
606   priv = paned->priv;
607
608   priv->orientation = GTK_ORIENTATION_HORIZONTAL;
609   priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
610
611   priv->child1 = NULL;
612   priv->child2 = NULL;
613   priv->handle = NULL;
614   priv->cursor_type = GDK_CROSS;
615
616   priv->handle_pos.width = 5;
617   priv->handle_pos.height = 5;
618   priv->position_set = FALSE;
619   priv->last_allocation = -1;
620   priv->in_drag = FALSE;
621
622   priv->last_child1_focus = NULL;
623   priv->last_child2_focus = NULL;
624   priv->in_recursion = FALSE;
625   priv->handle_prelit = FALSE;
626   priv->original_position = -1;
627
628   priv->handle_pos.x = -1;
629   priv->handle_pos.y = -1;
630
631   priv->drag_pos = -1;
632 }
633
634 static void
635 gtk_paned_set_property (GObject        *object,
636                         guint           prop_id,
637                         const GValue   *value,
638                         GParamSpec     *pspec)
639 {
640   GtkPaned *paned = GTK_PANED (object);
641   GtkPanedPrivate *priv = paned->priv;
642
643   switch (prop_id)
644     {
645     case PROP_ORIENTATION:
646       priv->orientation = g_value_get_enum (value);
647
648       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
649         priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
650       else
651         priv->cursor_type = GDK_SB_V_DOUBLE_ARROW;
652
653       /* state_changed updates the cursor */
654       gtk_paned_state_changed (GTK_WIDGET (paned), gtk_widget_get_state (GTK_WIDGET (paned)));
655       gtk_widget_queue_resize (GTK_WIDGET (paned));
656       break;
657     case PROP_POSITION:
658       gtk_paned_set_position (paned, g_value_get_int (value));
659       break;
660     case PROP_POSITION_SET:
661       priv->position_set = g_value_get_boolean (value);
662       gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
663       break;
664     default:
665       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
666       break;
667     }
668 }
669
670 static void
671 gtk_paned_get_property (GObject        *object,
672                         guint           prop_id,
673                         GValue         *value,
674                         GParamSpec     *pspec)
675 {
676   GtkPaned *paned = GTK_PANED (object);
677   GtkPanedPrivate *priv = paned->priv;
678
679   switch (prop_id)
680     {
681     case PROP_ORIENTATION:
682       g_value_set_enum (value, priv->orientation);
683       break;
684     case PROP_POSITION:
685       g_value_set_int (value, priv->child1_size);
686       break;
687     case PROP_POSITION_SET:
688       g_value_set_boolean (value, priv->position_set);
689       break;
690     case PROP_MIN_POSITION:
691       g_value_set_int (value, priv->min_position);
692       break;
693     case PROP_MAX_POSITION:
694       g_value_set_int (value, priv->max_position);
695       break;
696     default:
697       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
698       break;
699     }
700 }
701
702 static void
703 gtk_paned_set_child_property (GtkContainer    *container,
704                               GtkWidget       *child,
705                               guint            property_id,
706                               const GValue    *value,
707                               GParamSpec      *pspec)
708 {
709   GtkPaned *paned = GTK_PANED (container);
710   GtkPanedPrivate *priv = paned->priv;
711   gboolean old_value, new_value;
712
713   g_assert (child == priv->child1 || child == priv->child2);
714
715   new_value = g_value_get_boolean (value);
716   switch (property_id)
717     {
718     case CHILD_PROP_RESIZE:
719       if (child == priv->child1)
720         {
721           old_value = priv->child1_resize;
722           priv->child1_resize = new_value;
723         }
724       else
725         {
726           old_value = priv->child2_resize;
727           priv->child2_resize = new_value;
728         }
729       break;
730     case CHILD_PROP_SHRINK:
731       if (child == priv->child1)
732         {
733           old_value = priv->child1_shrink;
734           priv->child1_shrink = new_value;
735         }
736       else
737         {
738           old_value = priv->child2_shrink;
739           priv->child2_shrink = new_value;
740         }
741       break;
742     default:
743       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
744       old_value = -1; /* quiet gcc */
745       break;
746     }
747   if (old_value != new_value)
748     gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
749 }
750
751 static void
752 gtk_paned_get_child_property (GtkContainer *container,
753                               GtkWidget    *child,
754                               guint         property_id,
755                               GValue       *value,
756                               GParamSpec   *pspec)
757 {
758   GtkPaned *paned = GTK_PANED (container);
759   GtkPanedPrivate *priv = paned->priv;
760
761   g_assert (child == priv->child1 || child == priv->child2);
762   
763   switch (property_id)
764     {
765     case CHILD_PROP_RESIZE:
766       if (child == priv->child1)
767         g_value_set_boolean (value, priv->child1_resize);
768       else
769         g_value_set_boolean (value, priv->child2_resize);
770       break;
771     case CHILD_PROP_SHRINK:
772       if (child == priv->child1)
773         g_value_set_boolean (value, priv->child1_shrink);
774       else
775         g_value_set_boolean (value, priv->child2_shrink);
776       break;
777     default:
778       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
779       break;
780     }
781 }
782
783 static void
784 gtk_paned_finalize (GObject *object)
785 {
786   GtkPaned *paned = GTK_PANED (object);
787   
788   gtk_paned_set_saved_focus (paned, NULL);
789   gtk_paned_set_first_paned (paned, NULL);
790
791   G_OBJECT_CLASS (gtk_paned_parent_class)->finalize (object);
792 }
793
794 static void
795 gtk_paned_size_request (GtkWidget      *widget,
796                         GtkRequisition *requisition)
797 {
798   GtkPaned *paned = GTK_PANED (widget);
799   GtkPanedPrivate *priv = paned->priv;
800   GtkRequisition child_requisition;
801   guint border_width;
802
803   requisition->width = 0;
804   requisition->height = 0;
805
806   if (priv->child1 && gtk_widget_get_visible (priv->child1))
807     {
808       gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->child1),
809                                  &child_requisition, NULL);
810
811       requisition->height = child_requisition.height;
812       requisition->width = child_requisition.width;
813     }
814
815   if (priv->child2 && gtk_widget_get_visible (priv->child2))
816     {
817       gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->child2),
818                                  &child_requisition, NULL);
819
820       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
821         {
822           requisition->height = MAX (requisition->height,
823                                      child_requisition.height);
824           requisition->width += child_requisition.width;
825         }
826       else
827         {
828           requisition->width = MAX (requisition->width,
829                                     child_requisition.width);
830           requisition->height += child_requisition.height;
831         }
832     }
833
834   border_width = gtk_container_get_border_width (GTK_CONTAINER (paned));
835   requisition->width += border_width * 2;
836   requisition->height += border_width * 2;
837
838   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
839       priv->child2 && gtk_widget_get_visible (priv->child2))
840     {
841       gint handle_size;
842
843       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
844
845       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
846         requisition->width += handle_size;
847       else
848         requisition->height += handle_size;
849     }
850 }
851
852 static void
853 flip_child (GtkWidget     *widget,
854             GtkAllocation *child_pos)
855 {
856   GtkAllocation allocation;
857   gint x, width;
858
859   gtk_widget_get_allocation (widget, &allocation);
860   x = allocation.x;
861   width = allocation.width;
862
863   child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
864 }
865
866 static void
867 gtk_paned_size_allocate (GtkWidget     *widget,
868                          GtkAllocation *allocation)
869 {
870   GtkPaned *paned = GTK_PANED (widget);
871   GtkPanedPrivate *priv = paned->priv;
872   GtkAllocation widget_allocation;
873   guint border_width;
874
875   border_width = gtk_container_get_border_width (GTK_CONTAINER (paned));
876
877   gtk_widget_set_allocation (widget, allocation);
878
879   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
880       priv->child2 && gtk_widget_get_visible (priv->child2))
881     {
882       GtkRequisition child1_requisition;
883       GtkRequisition child2_requisition;
884       GtkAllocation child1_allocation;
885       GtkAllocation child2_allocation;
886       GtkAllocation priv_child1_allocation;
887       GdkRectangle old_handle_pos;
888       gint handle_size;
889
890       gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
891
892       gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->child1),
893                                  &child1_requisition, NULL);
894       gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->child2),
895                                  &child2_requisition, NULL);
896
897       old_handle_pos = priv->handle_pos;
898
899       gtk_widget_get_allocation (widget, &widget_allocation);
900
901       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
902         {
903           gtk_paned_calc_position (paned,
904                                    MAX (1, widget_allocation.width
905                                         - handle_size
906                                         - 2 * border_width),
907                                    child1_requisition.width,
908                                    child2_requisition.width);
909
910           priv->handle_pos.x = widget_allocation.x + priv->child1_size + border_width;
911           priv->handle_pos.y = widget_allocation.y + border_width;
912           priv->handle_pos.width = handle_size;
913           priv->handle_pos.height = MAX (1, widget_allocation.height - 2 * border_width);
914
915           child1_allocation.height = child2_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
916           child1_allocation.width = MAX (1, priv->child1_size);
917           child1_allocation.x = widget_allocation.x + border_width;
918           child1_allocation.y = child2_allocation.y = widget_allocation.y + border_width;
919
920           child2_allocation.x = child1_allocation.x + priv->child1_size + priv->handle_pos.width;
921           child2_allocation.width = MAX (1, widget_allocation.x + widget_allocation.width - child2_allocation.x - border_width);
922
923           if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
924             {
925               flip_child (widget, &(child2_allocation));
926               flip_child (widget, &(child1_allocation));
927               flip_child (widget, &(priv->handle_pos));
928             }
929         }
930       else
931         {
932           gtk_paned_calc_position (paned,
933                                    MAX (1, widget_allocation.height
934                                         - handle_size
935                                         - 2 * border_width),
936                                    child1_requisition.height,
937                                    child2_requisition.height);
938
939           priv->handle_pos.x = widget_allocation.x + border_width;
940           priv->handle_pos.y = widget_allocation.y + priv->child1_size + border_width;
941           priv->handle_pos.width = MAX (1, (gint) widget_allocation.width - 2 * border_width);
942           priv->handle_pos.height = handle_size;
943
944           child1_allocation.width = child2_allocation.width = MAX (1, (gint) allocation->width - border_width * 2);
945           child1_allocation.height = MAX (1, priv->child1_size);
946           child1_allocation.x = child2_allocation.x = widget_allocation.x + border_width;
947           child1_allocation.y = widget_allocation.y + border_width;
948
949           child2_allocation.y = child1_allocation.y + priv->child1_size + priv->handle_pos.height;
950           child2_allocation.height = MAX (1, widget_allocation.y + widget_allocation.height - child2_allocation.y - border_width);
951         }
952
953       if (gtk_widget_get_mapped (widget) &&
954           (old_handle_pos.x != priv->handle_pos.x ||
955            old_handle_pos.y != priv->handle_pos.y ||
956            old_handle_pos.width != priv->handle_pos.width ||
957            old_handle_pos.height != priv->handle_pos.height))
958         {
959           GdkWindow *window;
960
961           window = gtk_widget_get_window (widget);
962           gdk_window_invalidate_rect (window, &old_handle_pos, FALSE);
963           gdk_window_invalidate_rect (window, &priv->handle_pos, FALSE);
964         }
965
966       if (gtk_widget_get_realized (widget))
967         {
968           if (gtk_widget_get_mapped (widget))
969             gdk_window_show (priv->handle);
970
971           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
972             {
973               gdk_window_move_resize (priv->handle,
974                                       priv->handle_pos.x,
975                                       priv->handle_pos.y,
976                                       handle_size,
977                                       priv->handle_pos.height);
978             }
979           else
980             {
981               gdk_window_move_resize (priv->handle,
982                                       priv->handle_pos.x,
983                                       priv->handle_pos.y,
984                                       priv->handle_pos.width,
985                                       handle_size);
986             }
987         }
988
989       /* Now allocate the childen, making sure, when resizing not to
990        * overlap the windows
991        */
992       gtk_widget_get_allocation (priv->child1, &priv_child1_allocation);
993       if (gtk_widget_get_mapped (widget) &&
994           ((priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
995             priv_child1_allocation.width < child1_allocation.width) ||
996
997            (priv->orientation == GTK_ORIENTATION_VERTICAL &&
998             priv_child1_allocation.height < child1_allocation.height)))
999         {
1000           gtk_widget_size_allocate (priv->child2, &child2_allocation);
1001           gtk_widget_size_allocate (priv->child1, &child1_allocation);
1002         }
1003       else
1004         {
1005           gtk_widget_size_allocate (priv->child1, &child1_allocation);
1006           gtk_widget_size_allocate (priv->child2, &child2_allocation);
1007         }
1008     }
1009   else
1010     {
1011       GtkAllocation child_allocation;
1012
1013       if (gtk_widget_get_realized (widget))
1014         gdk_window_hide (priv->handle);
1015
1016       if (priv->child1)
1017         gtk_widget_set_child_visible (priv->child1, TRUE);
1018       if (priv->child2)
1019         gtk_widget_set_child_visible (priv->child2, TRUE);
1020
1021       gtk_widget_get_allocation (widget, &widget_allocation);
1022
1023       child_allocation.x = widget_allocation.x + border_width;
1024       child_allocation.y = widget_allocation.y + border_width;
1025       child_allocation.width = MAX (1, allocation->width - 2 * border_width);
1026       child_allocation.height = MAX (1, allocation->height - 2 * border_width);
1027
1028       if (priv->child1 && gtk_widget_get_visible (priv->child1))
1029         gtk_widget_size_allocate (priv->child1, &child_allocation);
1030       else if (priv->child2 && gtk_widget_get_visible (priv->child2))
1031         gtk_widget_size_allocate (priv->child2, &child_allocation);
1032     }
1033 }
1034
1035 static void
1036 gtk_paned_realize (GtkWidget *widget)
1037 {
1038   GtkPaned *paned = GTK_PANED (widget);
1039   GtkPanedPrivate *priv = paned->priv;
1040   GdkWindow *window;
1041   GdkWindowAttr attributes;
1042   gint attributes_mask;
1043
1044   gtk_widget_set_realized (widget, TRUE);
1045
1046   window = gtk_widget_get_parent_window (widget);
1047   gtk_widget_set_window (widget, window);
1048   g_object_ref (window);
1049
1050   attributes.window_type = GDK_WINDOW_CHILD;
1051   attributes.wclass = GDK_INPUT_ONLY;
1052   attributes.x = priv->handle_pos.x;
1053   attributes.y = priv->handle_pos.y;
1054   attributes.width = priv->handle_pos.width;
1055   attributes.height = priv->handle_pos.height;
1056   attributes.event_mask = gtk_widget_get_events (widget);
1057   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1058                             GDK_BUTTON_RELEASE_MASK |
1059                             GDK_ENTER_NOTIFY_MASK |
1060                             GDK_LEAVE_NOTIFY_MASK |
1061                             GDK_POINTER_MOTION_MASK |
1062                             GDK_POINTER_MOTION_HINT_MASK);
1063   attributes_mask = GDK_WA_X | GDK_WA_Y;
1064   if (gtk_widget_is_sensitive (widget))
1065     {
1066       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1067                                                       priv->cursor_type);
1068       attributes_mask |= GDK_WA_CURSOR;
1069     }
1070
1071   priv->handle = gdk_window_new (window,
1072                                  &attributes, attributes_mask);
1073   gdk_window_set_user_data (priv->handle, paned);
1074   if (attributes_mask & GDK_WA_CURSOR)
1075     gdk_cursor_unref (attributes.cursor);
1076
1077   gtk_widget_style_attach (widget);
1078
1079   if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
1080       priv->child2 && gtk_widget_get_visible (priv->child2))
1081     gdk_window_show (priv->handle);
1082 }
1083
1084 static void
1085 gtk_paned_unrealize (GtkWidget *widget)
1086 {
1087   GtkPaned *paned = GTK_PANED (widget);
1088   GtkPanedPrivate *priv = paned->priv;
1089
1090   if (priv->handle)
1091     {
1092       gdk_window_set_user_data (priv->handle, NULL);
1093       gdk_window_destroy (priv->handle);
1094       priv->handle = NULL;
1095     }
1096
1097   gtk_paned_set_last_child1_focus (paned, NULL);
1098   gtk_paned_set_last_child2_focus (paned, NULL);
1099   gtk_paned_set_saved_focus (paned, NULL);
1100   gtk_paned_set_first_paned (paned, NULL);
1101
1102   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unrealize (widget);
1103 }
1104
1105 static void
1106 gtk_paned_map (GtkWidget *widget)
1107 {
1108   GtkPaned *paned = GTK_PANED (widget);
1109   GtkPanedPrivate *priv = paned->priv;
1110
1111   gdk_window_show (priv->handle);
1112
1113   GTK_WIDGET_CLASS (gtk_paned_parent_class)->map (widget);
1114 }
1115
1116 static void
1117 gtk_paned_unmap (GtkWidget *widget)
1118 {
1119   GtkPaned *paned = GTK_PANED (widget);
1120   GtkPanedPrivate *priv = paned->priv;
1121
1122   gdk_window_hide (priv->handle);
1123
1124   GTK_WIDGET_CLASS (gtk_paned_parent_class)->unmap (widget);
1125 }
1126
1127 static gboolean
1128 gtk_paned_expose (GtkWidget      *widget,
1129                   GdkEventExpose *event)
1130 {
1131   GtkPaned *paned = GTK_PANED (widget);
1132   GtkPanedPrivate *priv = paned->priv;
1133
1134   if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
1135       priv->child1 && gtk_widget_get_visible (priv->child1) &&
1136       priv->child2 && gtk_widget_get_visible (priv->child2))
1137     {
1138       GtkStateType state;
1139       
1140       if (gtk_widget_is_focus (widget))
1141         state = GTK_STATE_SELECTED;
1142       else if (priv->handle_prelit)
1143         state = GTK_STATE_PRELIGHT;
1144       else
1145         state = gtk_widget_get_state (widget);
1146
1147       gtk_paint_handle (gtk_widget_get_style (widget),
1148                         gtk_widget_get_window (widget),
1149                         state, GTK_SHADOW_NONE,
1150                         &priv->handle_pos, widget, "paned",
1151                         priv->handle_pos.x, priv->handle_pos.y,
1152                         priv->handle_pos.width, priv->handle_pos.height,
1153                         !priv->orientation);
1154     }
1155
1156   /* Chain up to draw children */
1157   GTK_WIDGET_CLASS (gtk_paned_parent_class)->expose_event (widget, event);
1158   
1159   return FALSE;
1160 }
1161
1162 static gboolean
1163 is_rtl (GtkPaned *paned)
1164 {
1165   GtkPanedPrivate *priv = paned->priv;
1166
1167   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1168       gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL)
1169     {
1170       return TRUE;
1171     }
1172
1173   return FALSE;
1174 }
1175
1176 static void
1177 update_drag (GtkPaned *paned)
1178 {
1179   GtkPanedPrivate *priv = paned->priv;
1180   GtkAllocation allocation;
1181   GtkWidget *widget = GTK_WIDGET (paned);
1182   gint pos;
1183   gint handle_size;
1184   gint size;
1185
1186   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1187     gtk_widget_get_pointer (widget, &pos, NULL);
1188   else
1189     gtk_widget_get_pointer (widget, NULL, &pos);
1190
1191   pos -= priv->drag_pos;
1192
1193   if (is_rtl (paned))
1194     {
1195       gtk_widget_style_get (widget,
1196                             "handle-size", &handle_size,
1197                             NULL);
1198
1199       gtk_widget_get_allocation (widget, &allocation);
1200       size = allocation.width - pos - handle_size;
1201     }
1202   else
1203     {
1204       size = pos;
1205     }
1206
1207   size -= gtk_container_get_border_width (GTK_CONTAINER (paned));
1208   
1209   size = CLAMP (size, priv->min_position, priv->max_position);
1210
1211   if (size != priv->child1_size)
1212     gtk_paned_set_position (paned, size);
1213 }
1214
1215 static gboolean
1216 gtk_paned_enter (GtkWidget        *widget,
1217                  GdkEventCrossing *event)
1218 {
1219   GtkPaned *paned = GTK_PANED (widget);
1220   GtkPanedPrivate *priv = paned->priv;
1221
1222   if (priv->in_drag)
1223     update_drag (paned);
1224   else
1225     {
1226       priv->handle_prelit = TRUE;
1227       gtk_widget_queue_draw_area (widget,
1228                                   priv->handle_pos.x,
1229                                   priv->handle_pos.y,
1230                                   priv->handle_pos.width,
1231                                   priv->handle_pos.height);
1232     }
1233   
1234   return TRUE;
1235 }
1236
1237 static gboolean
1238 gtk_paned_leave (GtkWidget        *widget,
1239                  GdkEventCrossing *event)
1240 {
1241   GtkPaned *paned = GTK_PANED (widget);
1242   GtkPanedPrivate *priv = paned->priv;
1243
1244   if (priv->in_drag)
1245     update_drag (paned);
1246   else
1247     {
1248       priv->handle_prelit = FALSE;
1249       gtk_widget_queue_draw_area (widget,
1250                                   priv->handle_pos.x,
1251                                   priv->handle_pos.y,
1252                                   priv->handle_pos.width,
1253                                   priv->handle_pos.height);
1254     }
1255
1256   return TRUE;
1257 }
1258
1259 static gboolean
1260 gtk_paned_focus (GtkWidget        *widget,
1261                  GtkDirectionType  direction)
1262
1263 {
1264   gboolean retval;
1265   
1266   /* This is a hack, but how can this be done without
1267    * excessive cut-and-paste from gtkcontainer.c?
1268    */
1269
1270   gtk_widget_set_can_focus (widget, FALSE);
1271   retval = GTK_WIDGET_CLASS (gtk_paned_parent_class)->focus (widget, direction);
1272   gtk_widget_set_can_focus (widget, TRUE);
1273
1274   return retval;
1275 }
1276
1277 static gboolean
1278 gtk_paned_button_press (GtkWidget      *widget,
1279                         GdkEventButton *event)
1280 {
1281   GtkPaned *paned = GTK_PANED (widget);
1282   GtkPanedPrivate *priv = paned->priv;
1283
1284   if (!priv->in_drag &&
1285       (event->window == priv->handle) && (event->button == 1))
1286     {
1287       /* We need a server grab here, not gtk_grab_add(), since
1288        * we don't want to pass events on to the widget's children */
1289       if (gdk_device_grab (event->device,
1290                            priv->handle,
1291                            GDK_OWNERSHIP_WINDOW, FALSE,
1292                            GDK_POINTER_MOTION_HINT_MASK
1293                            | GDK_BUTTON1_MOTION_MASK
1294                            | GDK_BUTTON_RELEASE_MASK
1295                            | GDK_ENTER_NOTIFY_MASK
1296                            | GDK_LEAVE_NOTIFY_MASK,
1297                            NULL, event->time) != GDK_GRAB_SUCCESS)
1298         return FALSE;
1299
1300       priv->in_drag = TRUE;
1301       priv->grab_time = event->time;
1302       priv->grab_device = event->device;
1303
1304       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1305         priv->drag_pos = event->x;
1306       else
1307         priv->drag_pos = event->y;
1308
1309       return TRUE;
1310     }
1311
1312   return FALSE;
1313 }
1314
1315 static gboolean
1316 gtk_paned_grab_broken (GtkWidget          *widget,
1317                        GdkEventGrabBroken *event)
1318 {
1319   GtkPaned *paned = GTK_PANED (widget);
1320   GtkPanedPrivate *priv = paned->priv;
1321
1322   priv->in_drag = FALSE;
1323   priv->drag_pos = -1;
1324   priv->position_set = TRUE;
1325
1326   return TRUE;
1327 }
1328
1329 static void
1330 stop_drag (GtkPaned *paned)
1331 {
1332   GtkPanedPrivate *priv = paned->priv;
1333
1334   priv->in_drag = FALSE;
1335   priv->drag_pos = -1;
1336   priv->position_set = TRUE;
1337
1338   gdk_device_ungrab (priv->grab_device,
1339                      priv->grab_time);
1340   priv->grab_device = NULL;
1341 }
1342
1343 static void
1344 gtk_paned_grab_notify (GtkWidget *widget,
1345                        gboolean   was_grabbed)
1346 {
1347   GtkPaned *paned = GTK_PANED (widget);
1348   GtkPanedPrivate *priv = paned->priv;
1349   GdkDevice *grab_device;
1350
1351   grab_device = priv->grab_device;
1352
1353   if (priv->in_drag && grab_device &&
1354       gtk_widget_device_is_shadowed (widget, grab_device))
1355     stop_drag (paned);
1356 }
1357
1358 static void
1359 gtk_paned_state_changed (GtkWidget    *widget,
1360                          GtkStateType  previous_state)
1361 {
1362   GtkPaned *paned = GTK_PANED (widget);
1363   GtkPanedPrivate *priv = paned->priv;
1364   GdkCursor *cursor;
1365
1366   if (gtk_widget_get_realized (widget))
1367     {
1368       if (gtk_widget_is_sensitive (widget))
1369         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1370                                              priv->cursor_type);
1371       else
1372         cursor = NULL;
1373
1374       gdk_window_set_cursor (priv->handle, cursor);
1375
1376       if (cursor)
1377         gdk_cursor_unref (cursor);
1378     }
1379 }
1380
1381 static gboolean
1382 gtk_paned_button_release (GtkWidget      *widget,
1383                           GdkEventButton *event)
1384 {
1385   GtkPaned *paned = GTK_PANED (widget);
1386   GtkPanedPrivate *priv = paned->priv;
1387
1388   if (priv->in_drag && (event->button == 1))
1389     {
1390       stop_drag (paned);
1391
1392       return TRUE;
1393     }
1394
1395   return FALSE;
1396 }
1397
1398 static gboolean
1399 gtk_paned_motion (GtkWidget      *widget,
1400                   GdkEventMotion *event)
1401 {
1402   GtkPaned *paned = GTK_PANED (widget);
1403   GtkPanedPrivate *priv = paned->priv;
1404
1405   if (priv->in_drag)
1406     {
1407       update_drag (paned);
1408       return TRUE;
1409     }
1410   
1411   return FALSE;
1412 }
1413
1414 /**
1415  * gtk_paned_new:
1416  * @orientation: the paned's orientation.
1417  *
1418  * Creates a new #GtkPaned widget.
1419  *
1420  * Return value: a new #GtkPaned.
1421  *
1422  * Since: 3.0
1423  **/
1424 GtkWidget *
1425 gtk_paned_new (GtkOrientation orientation)
1426 {
1427   return g_object_new (GTK_TYPE_PANED,
1428                        "orientation", orientation,
1429                        NULL);
1430 }
1431
1432 void
1433 gtk_paned_add1 (GtkPaned  *paned,
1434                 GtkWidget *widget)
1435 {
1436   gtk_paned_pack1 (paned, widget, FALSE, TRUE);
1437 }
1438
1439 void
1440 gtk_paned_add2 (GtkPaned  *paned,
1441                 GtkWidget *widget)
1442 {
1443   gtk_paned_pack2 (paned, widget, TRUE, TRUE);
1444 }
1445
1446 void
1447 gtk_paned_pack1 (GtkPaned  *paned,
1448                  GtkWidget *child,
1449                  gboolean   resize,
1450                  gboolean   shrink)
1451 {
1452   GtkPanedPrivate *priv;
1453
1454   g_return_if_fail (GTK_IS_PANED (paned));
1455   g_return_if_fail (GTK_IS_WIDGET (child));
1456
1457   priv = paned->priv;
1458
1459   if (!priv->child1)
1460     {
1461       priv->child1 = child;
1462       priv->child1_resize = resize;
1463       priv->child1_shrink = shrink;
1464
1465       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1466     }
1467 }
1468
1469 void
1470 gtk_paned_pack2 (GtkPaned  *paned,
1471                  GtkWidget *child,
1472                  gboolean   resize,
1473                  gboolean   shrink)
1474 {
1475   GtkPanedPrivate *priv;
1476
1477   g_return_if_fail (GTK_IS_PANED (paned));
1478   g_return_if_fail (GTK_IS_WIDGET (child));
1479
1480   priv = paned->priv;
1481
1482   if (!priv->child2)
1483     {
1484       priv->child2 = child;
1485       priv->child2_resize = resize;
1486       priv->child2_shrink = shrink;
1487
1488       gtk_widget_set_parent (child, GTK_WIDGET (paned));
1489     }
1490 }
1491
1492
1493 static void
1494 gtk_paned_add (GtkContainer *container,
1495                GtkWidget    *widget)
1496 {
1497   GtkPanedPrivate *priv;
1498   GtkPaned *paned;
1499
1500   g_return_if_fail (GTK_IS_PANED (container));
1501
1502   paned = GTK_PANED (container);
1503   priv = paned->priv;
1504
1505   if (!priv->child1)
1506     gtk_paned_add1 (paned, widget);
1507   else if (!priv->child2)
1508     gtk_paned_add2 (paned, widget);
1509   else
1510     g_warning ("GtkPaned cannot have more than 2 children\n");
1511 }
1512
1513 static void
1514 gtk_paned_remove (GtkContainer *container,
1515                   GtkWidget    *widget)
1516 {
1517   GtkPaned *paned = GTK_PANED (container);
1518   GtkPanedPrivate *priv = paned->priv;
1519   gboolean was_visible;
1520
1521   was_visible = gtk_widget_get_visible (widget);
1522
1523   if (priv->child1 == widget)
1524     {
1525       gtk_widget_unparent (widget);
1526
1527       priv->child1 = NULL;
1528
1529       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1530         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1531     }
1532   else if (priv->child2 == widget)
1533     {
1534       gtk_widget_unparent (widget);
1535
1536       priv->child2 = NULL;
1537
1538       if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1539         gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1540     }
1541 }
1542
1543 static void
1544 gtk_paned_forall (GtkContainer *container,
1545                   gboolean      include_internals,
1546                   GtkCallback   callback,
1547                   gpointer      callback_data)
1548 {
1549   GtkPanedPrivate *priv;
1550   GtkPaned *paned;
1551
1552   g_return_if_fail (callback != NULL);
1553
1554   paned = GTK_PANED (container);
1555   priv = paned->priv;
1556
1557   if (priv->child1)
1558     (*callback) (priv->child1, callback_data);
1559   if (priv->child2)
1560     (*callback) (priv->child2, callback_data);
1561 }
1562
1563 /**
1564  * gtk_paned_get_position:
1565  * @paned: a #GtkPaned widget
1566  * 
1567  * Obtains the position of the divider between the two panes.
1568  * 
1569  * Return value: position of the divider
1570  **/
1571 gint
1572 gtk_paned_get_position (GtkPaned  *paned)
1573 {
1574   g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1575
1576   return paned->priv->child1_size;
1577 }
1578
1579 /**
1580  * gtk_paned_set_position:
1581  * @paned: a #GtkPaned widget
1582  * @position: pixel position of divider, a negative value means that the position
1583  *            is unset.
1584  * 
1585  * Sets the position of the divider between the two panes.
1586  **/
1587 void
1588 gtk_paned_set_position (GtkPaned *paned,
1589                         gint      position)
1590 {
1591   GtkPanedPrivate *priv;
1592   GObject *object;
1593
1594   g_return_if_fail (GTK_IS_PANED (paned));
1595
1596   priv = paned->priv;
1597
1598   if (priv->child1_size == position)
1599     return;
1600
1601   object = G_OBJECT (paned);
1602   
1603   if (position >= 0)
1604     {
1605       /* We don't clamp here - the assumption is that
1606        * if the total allocation changes at the same time
1607        * as the position, the position set is with reference
1608        * to the new total size. If only the position changes,
1609        * then clamping will occur in gtk_paned_calc_position()
1610        */
1611
1612       priv->child1_size = position;
1613       priv->position_set = TRUE;
1614     }
1615   else
1616     {
1617       priv->position_set = FALSE;
1618     }
1619
1620   g_object_freeze_notify (object);
1621   g_object_notify (object, "position");
1622   g_object_notify (object, "position-set");
1623   g_object_thaw_notify (object);
1624
1625   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
1626
1627 #ifdef G_OS_WIN32
1628   /* Hacky work-around for bug #144269 */
1629   if (priv->child2 != NULL)
1630     {
1631       gtk_widget_queue_draw (priv->child2);
1632     }
1633 #endif
1634 }
1635
1636 /**
1637  * gtk_paned_get_child1:
1638  * @paned: a #GtkPaned widget
1639  * 
1640  * Obtains the first child of the paned widget.
1641  * 
1642  * Return value: (transfer none): first child, or %NULL if it is not set.
1643  *
1644  * Since: 2.4
1645  **/
1646 GtkWidget *
1647 gtk_paned_get_child1 (GtkPaned *paned)
1648 {
1649   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1650
1651   return paned->priv->child1;
1652 }
1653
1654 /**
1655  * gtk_paned_get_child2:
1656  * @paned: a #GtkPaned widget
1657  * 
1658  * Obtains the second child of the paned widget.
1659  * 
1660  * Return value: (transfer none): second child, or %NULL if it is not set.
1661  *
1662  * Since: 2.4
1663  **/
1664 GtkWidget *
1665 gtk_paned_get_child2 (GtkPaned *paned)
1666 {
1667   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1668
1669   return paned->priv->child2;
1670 }
1671
1672 static void
1673 gtk_paned_calc_position (GtkPaned *paned,
1674                          gint      allocation,
1675                          gint      child1_req,
1676                          gint      child2_req)
1677 {
1678   GtkPanedPrivate *priv = paned->priv;
1679   gint old_position;
1680   gint old_min_position;
1681   gint old_max_position;
1682
1683   old_position = priv->child1_size;
1684   old_min_position = priv->min_position;
1685   old_max_position = priv->max_position;
1686
1687   priv->min_position = priv->child1_shrink ? 0 : child1_req;
1688
1689   priv->max_position = allocation;
1690   if (!priv->child2_shrink)
1691     priv->max_position = MAX (1, priv->max_position - child2_req);
1692   priv->max_position = MAX (priv->min_position, priv->max_position);
1693
1694   if (!priv->position_set)
1695     {
1696       if (priv->child1_resize && !priv->child2_resize)
1697         priv->child1_size = MAX (0, allocation - child2_req);
1698       else if (!priv->child1_resize && priv->child2_resize)
1699         priv->child1_size = child1_req;
1700       else if (child1_req + child2_req != 0)
1701         priv->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
1702       else
1703         priv->child1_size = allocation * 0.5 + 0.5;
1704     }
1705   else
1706     {
1707       /* If the position was set before the initial allocation.
1708        * (priv->last_allocation <= 0) just clamp it and leave it.
1709        */
1710       if (priv->last_allocation > 0)
1711         {
1712           if (priv->child1_resize && !priv->child2_resize)
1713             priv->child1_size += allocation - priv->last_allocation;
1714           else if (!(!priv->child1_resize && priv->child2_resize))
1715             priv->child1_size = allocation * ((gdouble) priv->child1_size / (priv->last_allocation)) + 0.5;
1716         }
1717     }
1718
1719   priv->child1_size = CLAMP (priv->child1_size,
1720                               priv->min_position,
1721                               priv->max_position);
1722
1723   if (priv->child1)
1724     gtk_widget_set_child_visible (priv->child1, priv->child1_size != 0);
1725   
1726   if (priv->child2)
1727     gtk_widget_set_child_visible (priv->child2, priv->child1_size != allocation); 
1728
1729   g_object_freeze_notify (G_OBJECT (paned));
1730   if (priv->child1_size != old_position)
1731     g_object_notify (G_OBJECT (paned), "position");
1732   if (priv->min_position != old_min_position)
1733     g_object_notify (G_OBJECT (paned), "min-position");
1734   if (priv->max_position != old_max_position)
1735     g_object_notify (G_OBJECT (paned), "max-position");
1736   g_object_thaw_notify (G_OBJECT (paned));
1737
1738   priv->last_allocation = allocation;
1739 }
1740
1741 static void
1742 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1743 {
1744   GtkPanedPrivate *priv = paned->priv;
1745
1746   if (priv->saved_focus)
1747     g_object_remove_weak_pointer (G_OBJECT (priv->saved_focus),
1748                                   (gpointer *)&(priv->saved_focus));
1749
1750   priv->saved_focus = widget;
1751
1752   if (priv->saved_focus)
1753     g_object_add_weak_pointer (G_OBJECT (priv->saved_focus),
1754                                (gpointer *)&(priv->saved_focus));
1755 }
1756
1757 static void
1758 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1759 {
1760   GtkPanedPrivate *priv = paned->priv;
1761
1762   if (priv->first_paned)
1763     g_object_remove_weak_pointer (G_OBJECT (priv->first_paned),
1764                                   (gpointer *)&(priv->first_paned));
1765
1766   priv->first_paned = first_paned;
1767
1768   if (priv->first_paned)
1769     g_object_add_weak_pointer (G_OBJECT (priv->first_paned),
1770                                (gpointer *)&(priv->first_paned));
1771 }
1772
1773 static void
1774 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1775 {
1776   GtkPanedPrivate *priv = paned->priv;
1777
1778   if (priv->last_child1_focus)
1779     g_object_remove_weak_pointer (G_OBJECT (priv->last_child1_focus),
1780                                   (gpointer *)&(priv->last_child1_focus));
1781
1782   priv->last_child1_focus = widget;
1783
1784   if (priv->last_child1_focus)
1785     g_object_add_weak_pointer (G_OBJECT (priv->last_child1_focus),
1786                                (gpointer *)&(priv->last_child1_focus));
1787 }
1788
1789 static void
1790 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1791 {
1792   GtkPanedPrivate *priv = paned->priv;
1793
1794   if (priv->last_child2_focus)
1795     g_object_remove_weak_pointer (G_OBJECT (priv->last_child2_focus),
1796                                   (gpointer *)&(priv->last_child2_focus));
1797
1798   priv->last_child2_focus = widget;
1799
1800   if (priv->last_child2_focus)
1801     g_object_add_weak_pointer (G_OBJECT (priv->last_child2_focus),
1802                                (gpointer *)&(priv->last_child2_focus));
1803 }
1804
1805 static GtkWidget *
1806 paned_get_focus_widget (GtkPaned *paned)
1807 {
1808   GtkWidget *toplevel;
1809
1810   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1811   if (gtk_widget_is_toplevel (toplevel))
1812     return gtk_window_get_focus (GTK_WINDOW (toplevel));
1813
1814   return NULL;
1815 }
1816
1817 static void
1818 gtk_paned_set_focus_child (GtkContainer *container,
1819                            GtkWidget    *focus_child)
1820 {
1821   GtkPaned *paned;
1822   GtkPanedPrivate *priv;
1823   GtkWidget *container_focus_child;
1824
1825   g_return_if_fail (GTK_IS_PANED (container));
1826
1827   paned = GTK_PANED (container);
1828   priv = paned->priv;
1829
1830   if (focus_child == NULL)
1831     {
1832       GtkWidget *last_focus;
1833       GtkWidget *w;
1834       
1835       last_focus = paned_get_focus_widget (paned);
1836
1837       if (last_focus)
1838         {
1839           /* If there is one or more paned widgets between us and the
1840            * focus widget, we want the topmost of those as last_focus
1841            */
1842           for (w = last_focus; w != GTK_WIDGET (paned); w = gtk_widget_get_parent (w))
1843             if (GTK_IS_PANED (w))
1844               last_focus = w;
1845
1846           container_focus_child = gtk_container_get_focus_child (container);
1847           if (container_focus_child == priv->child1)
1848             gtk_paned_set_last_child1_focus (paned, last_focus);
1849           else if (container_focus_child == priv->child2)
1850             gtk_paned_set_last_child2_focus (paned, last_focus);
1851         }
1852     }
1853
1854   if (GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child)
1855     GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child (container, focus_child);
1856 }
1857
1858 static void
1859 gtk_paned_get_cycle_chain (GtkPaned          *paned,
1860                            GtkDirectionType   direction,
1861                            GList            **widgets)
1862 {
1863   GtkPanedPrivate *priv = paned->priv;
1864   GtkContainer *container = GTK_CONTAINER (paned);
1865   GtkWidget *ancestor = NULL;
1866   GtkWidget *focus_child;
1867   GtkWidget *parent;
1868   GtkWidget *widget = GTK_WIDGET (paned);
1869   GList *temp_list = NULL;
1870   GList *list;
1871
1872   if (priv->in_recursion)
1873     return;
1874
1875   g_assert (widgets != NULL);
1876
1877   if (priv->last_child1_focus &&
1878       !gtk_widget_is_ancestor (priv->last_child1_focus, widget))
1879     {
1880       gtk_paned_set_last_child1_focus (paned, NULL);
1881     }
1882
1883   if (priv->last_child2_focus &&
1884       !gtk_widget_is_ancestor (priv->last_child2_focus, widget))
1885     {
1886       gtk_paned_set_last_child2_focus (paned, NULL);
1887     }
1888
1889   parent = gtk_widget_get_parent (widget);
1890   if (parent)
1891     ancestor = gtk_widget_get_ancestor (parent, GTK_TYPE_PANED);
1892
1893   /* The idea here is that temp_list is a list of widgets we want to cycle
1894    * to. The list is prioritized so that the first element is our first
1895    * choice, the next our second, and so on.
1896    *
1897    * We can't just use g_list_reverse(), because we want to try
1898    * priv->last_child?_focus before priv->child?, both when we
1899    * are going forward and backward.
1900    */
1901   focus_child = gtk_container_get_focus_child (container);
1902   if (direction == GTK_DIR_TAB_FORWARD)
1903     {
1904       if (focus_child == priv->child1)
1905         {
1906           temp_list = g_list_append (temp_list, priv->last_child2_focus);
1907           temp_list = g_list_append (temp_list, priv->child2);
1908           temp_list = g_list_append (temp_list, ancestor);
1909         }
1910       else if (focus_child == priv->child2)
1911         {
1912           temp_list = g_list_append (temp_list, ancestor);
1913           temp_list = g_list_append (temp_list, priv->last_child1_focus);
1914           temp_list = g_list_append (temp_list, priv->child1);
1915         }
1916       else
1917         {
1918           temp_list = g_list_append (temp_list, priv->last_child1_focus);
1919           temp_list = g_list_append (temp_list, priv->child1);
1920           temp_list = g_list_append (temp_list, priv->last_child2_focus);
1921           temp_list = g_list_append (temp_list, priv->child2);
1922           temp_list = g_list_append (temp_list, ancestor);
1923         }
1924     }
1925   else
1926     {
1927       if (focus_child == priv->child1)
1928         {
1929           temp_list = g_list_append (temp_list, ancestor);
1930           temp_list = g_list_append (temp_list, priv->last_child2_focus);
1931           temp_list = g_list_append (temp_list, priv->child2);
1932         }
1933       else if (focus_child == priv->child2)
1934         {
1935           temp_list = g_list_append (temp_list, priv->last_child1_focus);
1936           temp_list = g_list_append (temp_list, priv->child1);
1937           temp_list = g_list_append (temp_list, ancestor);
1938         }
1939       else
1940         {
1941           temp_list = g_list_append (temp_list, priv->last_child2_focus);
1942           temp_list = g_list_append (temp_list, priv->child2);
1943           temp_list = g_list_append (temp_list, priv->last_child1_focus);
1944           temp_list = g_list_append (temp_list, priv->child1);
1945           temp_list = g_list_append (temp_list, ancestor);
1946         }
1947     }
1948
1949   /* Walk the list and expand all the paned widgets. */
1950   for (list = temp_list; list != NULL; list = list->next)
1951     {
1952       GtkWidget *widget = list->data;
1953
1954       if (widget)
1955         {
1956           if (GTK_IS_PANED (widget))
1957             {
1958               priv->in_recursion = TRUE;
1959               gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
1960               priv->in_recursion = FALSE;
1961             }
1962           else
1963             {
1964               *widgets = g_list_append (*widgets, widget);
1965             }
1966         }
1967     }
1968
1969   g_list_free (temp_list);
1970 }
1971
1972 static gboolean
1973 gtk_paned_cycle_child_focus (GtkPaned *paned,
1974                              gboolean  reversed)
1975 {
1976   GList *cycle_chain = NULL;
1977   GList *list;
1978   
1979   GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
1980
1981   /* ignore f6 if the handle is focused */
1982   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1983     return TRUE;
1984   
1985   /* we can't just let the event propagate up the hierarchy,
1986    * because the paned will want to cycle focus _unless_ an
1987    * ancestor paned handles the event
1988    */
1989   gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
1990
1991   for (list = cycle_chain; list != NULL; list = list->next)
1992     if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
1993       break;
1994
1995   g_list_free (cycle_chain);
1996   
1997   return TRUE;
1998 }
1999
2000 static void
2001 get_child_panes (GtkWidget  *widget,
2002                  GList     **panes)
2003 {
2004   if (!widget || !gtk_widget_get_realized (widget))
2005     return;
2006
2007   if (GTK_IS_PANED (widget))
2008     {
2009       GtkPaned *paned = GTK_PANED (widget);
2010       GtkPanedPrivate *priv = paned->priv;
2011
2012       get_child_panes (priv->child1, panes);
2013       *panes = g_list_prepend (*panes, widget);
2014       get_child_panes (priv->child2, panes);
2015     }
2016   else if (GTK_IS_CONTAINER (widget))
2017     {
2018       gtk_container_forall (GTK_CONTAINER (widget),
2019                             (GtkCallback)get_child_panes, panes);
2020     }
2021 }
2022
2023 static GList *
2024 get_all_panes (GtkPaned *paned)
2025 {
2026   GtkPaned *topmost = NULL;
2027   GList *result = NULL;
2028   GtkWidget *w;
2029
2030   for (w = GTK_WIDGET (paned); w != NULL; w = gtk_widget_get_parent (w))
2031     {
2032       if (GTK_IS_PANED (w))
2033         topmost = GTK_PANED (w);
2034     }
2035
2036   g_assert (topmost);
2037
2038   get_child_panes (GTK_WIDGET (topmost), &result);
2039
2040   return g_list_reverse (result);
2041 }
2042
2043 static void
2044 gtk_paned_find_neighbours (GtkPaned  *paned,
2045                            GtkPaned **next,
2046                            GtkPaned **prev)
2047 {
2048   GList *all_panes;
2049   GList *this_link;
2050
2051   all_panes = get_all_panes (paned);
2052   g_assert (all_panes);
2053
2054   this_link = g_list_find (all_panes, paned);
2055
2056   g_assert (this_link);
2057   
2058   if (this_link->next)
2059     *next = this_link->next->data;
2060   else
2061     *next = all_panes->data;
2062
2063   if (this_link->prev)
2064     *prev = this_link->prev->data;
2065   else
2066     *prev = g_list_last (all_panes)->data;
2067
2068   g_list_free (all_panes);
2069 }
2070
2071 static gboolean
2072 gtk_paned_move_handle (GtkPaned      *paned,
2073                        GtkScrollType  scroll)
2074 {
2075   GtkPanedPrivate *priv = paned->priv;
2076
2077   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2078     {
2079       gint old_position;
2080       gint new_position;
2081       gint increment;
2082       
2083       enum {
2084         SINGLE_STEP_SIZE = 1,
2085         PAGE_STEP_SIZE   = 75
2086       };
2087       
2088       new_position = old_position = gtk_paned_get_position (paned);
2089       increment = 0;
2090       
2091       switch (scroll)
2092         {
2093         case GTK_SCROLL_STEP_LEFT:
2094         case GTK_SCROLL_STEP_UP:
2095         case GTK_SCROLL_STEP_BACKWARD:
2096           increment = - SINGLE_STEP_SIZE;
2097           break;
2098           
2099         case GTK_SCROLL_STEP_RIGHT:
2100         case GTK_SCROLL_STEP_DOWN:
2101         case GTK_SCROLL_STEP_FORWARD:
2102           increment = SINGLE_STEP_SIZE;
2103           break;
2104           
2105         case GTK_SCROLL_PAGE_LEFT:
2106         case GTK_SCROLL_PAGE_UP:
2107         case GTK_SCROLL_PAGE_BACKWARD:
2108           increment = - PAGE_STEP_SIZE;
2109           break;
2110           
2111         case GTK_SCROLL_PAGE_RIGHT:
2112         case GTK_SCROLL_PAGE_DOWN:
2113         case GTK_SCROLL_PAGE_FORWARD:
2114           increment = PAGE_STEP_SIZE;
2115           break;
2116           
2117         case GTK_SCROLL_START:
2118           new_position = priv->min_position;
2119           break;
2120           
2121         case GTK_SCROLL_END:
2122           new_position = priv->max_position;
2123           break;
2124
2125         default:
2126           break;
2127         }
2128
2129       if (increment)
2130         {
2131           if (is_rtl (paned))
2132             increment = -increment;
2133           
2134           new_position = old_position + increment;
2135         }
2136       
2137       new_position = CLAMP (new_position, priv->min_position, priv->max_position);
2138       
2139       if (old_position != new_position)
2140         gtk_paned_set_position (paned, new_position);
2141
2142       return TRUE;
2143     }
2144
2145   return FALSE;
2146 }
2147
2148 static void
2149 gtk_paned_restore_focus (GtkPaned *paned)
2150 {
2151   GtkPanedPrivate *priv = paned->priv;
2152
2153   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2154     {
2155       if (priv->saved_focus &&
2156           gtk_widget_get_sensitive (priv->saved_focus))
2157         {
2158           gtk_widget_grab_focus (priv->saved_focus);
2159         }
2160       else
2161         {
2162           /* the saved focus is somehow not available for focusing,
2163            * try
2164            *   1) tabbing into the paned widget
2165            * if that didn't work,
2166            *   2) unset focus for the window if there is one
2167            */
2168           
2169           if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
2170             {
2171               GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2172               
2173               if (GTK_IS_WINDOW (toplevel))
2174                 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
2175             }
2176         }
2177       
2178       gtk_paned_set_saved_focus (paned, NULL);
2179       gtk_paned_set_first_paned (paned, NULL);
2180     }
2181 }
2182
2183 static gboolean
2184 gtk_paned_accept_position (GtkPaned *paned)
2185 {
2186   GtkPanedPrivate *priv = paned->priv;
2187
2188   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2189     {
2190       priv->original_position = -1;
2191       gtk_paned_restore_focus (paned);
2192
2193       return TRUE;
2194     }
2195
2196   return FALSE;
2197 }
2198
2199
2200 static gboolean
2201 gtk_paned_cancel_position (GtkPaned *paned)
2202 {
2203   GtkPanedPrivate *priv = paned->priv;
2204
2205   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2206     {
2207       if (priv->original_position != -1)
2208         {
2209           gtk_paned_set_position (paned, priv->original_position);
2210           priv->original_position = -1;
2211         }
2212
2213       gtk_paned_restore_focus (paned);
2214       return TRUE;
2215     }
2216
2217   return FALSE;
2218 }
2219
2220 static gboolean
2221 gtk_paned_cycle_handle_focus (GtkPaned *paned,
2222                               gboolean  reversed)
2223 {
2224   GtkPanedPrivate *priv = paned->priv;
2225   GtkPaned *next, *prev;
2226
2227   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2228     {
2229       GtkPaned *focus = NULL;
2230
2231       if (!priv->first_paned)
2232         {
2233           /* The first_pane has disappeared. As an ad-hoc solution,
2234            * we make the currently focused paned the first_paned. To the
2235            * user this will seem like the paned cycling has been reset.
2236            */
2237           
2238           gtk_paned_set_first_paned (paned, paned);
2239         }
2240       
2241       gtk_paned_find_neighbours (paned, &next, &prev);
2242
2243       if (reversed && prev &&
2244           prev != paned && paned != priv->first_paned)
2245         {
2246           focus = prev;
2247         }
2248       else if (!reversed && next &&
2249                next != paned && next != priv->first_paned)
2250         {
2251           focus = next;
2252         }
2253       else
2254         {
2255           gtk_paned_accept_position (paned);
2256           return TRUE;
2257         }
2258
2259       g_assert (focus);
2260       
2261       gtk_paned_set_saved_focus (focus, priv->saved_focus);
2262       gtk_paned_set_first_paned (focus, priv->first_paned);
2263       
2264       gtk_paned_set_saved_focus (paned, NULL);
2265       gtk_paned_set_first_paned (paned, NULL);
2266       
2267       gtk_widget_grab_focus (GTK_WIDGET (focus));
2268       
2269       if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
2270         {
2271           priv->original_position = -1;
2272           focus->priv->original_position = gtk_paned_get_position (focus);
2273         }
2274     }
2275   else
2276     {
2277       GtkContainer *container = GTK_CONTAINER (paned);
2278       GtkPaned *focus;
2279       GtkPaned *first;
2280       GtkPaned *prev, *next;
2281       GtkWidget *toplevel;
2282       GtkWidget *focus_child;
2283
2284       gtk_paned_find_neighbours (paned, &next, &prev);
2285       focus_child = gtk_container_get_focus_child (container);
2286
2287       if (focus_child == priv->child1)
2288         {
2289           if (reversed)
2290             {
2291               focus = prev;
2292               first = paned;
2293             }
2294           else
2295             {
2296               focus = paned;
2297               first = paned;
2298             }
2299         }
2300       else if (focus_child == priv->child2)
2301         {
2302           if (reversed)
2303             {
2304               focus = paned;
2305               first = next;
2306             }
2307           else
2308             {
2309               focus = next;
2310               first = next;
2311             }
2312         }
2313       else
2314         {
2315           /* Focus is not inside this paned, and we don't have focus.
2316            * Presumably this happened because the application wants us
2317            * to start keyboard navigating.
2318            */
2319           focus = paned;
2320
2321           if (reversed)
2322             first = paned;
2323           else
2324             first = next;
2325         }
2326
2327       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2328
2329       if (GTK_IS_WINDOW (toplevel))
2330         gtk_paned_set_saved_focus (focus, gtk_window_get_focus (GTK_WINDOW (toplevel)));
2331       gtk_paned_set_first_paned (focus, first);
2332       focus->priv->original_position = gtk_paned_get_position (focus);
2333
2334       gtk_widget_grab_focus (GTK_WIDGET (focus));
2335    }
2336
2337   return TRUE;
2338 }
2339
2340 static gboolean
2341 gtk_paned_toggle_handle_focus (GtkPaned *paned)
2342 {
2343   /* This function/signal has the wrong name. It is called when you
2344    * press Tab or Shift-Tab and what we do is act as if
2345    * the user pressed Return and then Tab or Shift-Tab
2346    */
2347   if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2348     gtk_paned_accept_position (paned);
2349
2350   return FALSE;
2351 }
2352
2353 /**
2354  * gtk_paned_get_handle_window:
2355  * @paned: a #GtkPaned
2356  *
2357  * Returns the #GdkWindow of the handle. This function is
2358  * useful when handling button or motion events because it
2359  * enables the callback to distinguish between the window
2360  * of the paned, a child and the handle.
2361  *
2362  * Return value: (transfer none): the paned's handle window.
2363  *
2364  * Since: 2.20
2365  **/
2366 GdkWindow *
2367 gtk_paned_get_handle_window (GtkPaned *paned)
2368 {
2369   g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2370
2371   return paned->priv->handle;
2372 }