1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkbindings.h"
32 #include "gtkmarshalers.h"
33 #include "gtkorientable.h"
35 #include "gtkwindow.h"
36 #include "gtkprivate.h"
41 struct _GtkPanedPrivate
43 GtkOrientation orientation;
44 GtkPaned *first_paned;
47 GtkWidget *last_child1_focus;
48 GtkWidget *last_child2_focus;
49 GtkWidget *saved_focus;
51 GdkCursorType cursor_type;
52 GdkDevice *grab_device;
53 GdkRectangle handle_pos;
61 gint original_position;
63 guint handle_prelit : 1;
65 guint in_recursion : 1;
66 guint child1_resize : 1;
67 guint child1_shrink : 1;
68 guint child2_resize : 1;
69 guint child2_shrink : 1;
70 guint position_set : 1;
100 static void gtk_paned_set_property (GObject *object,
104 static void gtk_paned_get_property (GObject *object,
108 static void gtk_paned_set_child_property (GtkContainer *container,
113 static void gtk_paned_get_child_property (GtkContainer *container,
118 static void gtk_paned_finalize (GObject *object);
120 static void gtk_paned_size_request (GtkWidget *widget,
121 GtkRequisition *requisition);
122 static void gtk_paned_size_allocate (GtkWidget *widget,
123 GtkAllocation *allocation);
124 static void gtk_paned_realize (GtkWidget *widget);
125 static void gtk_paned_unrealize (GtkWidget *widget);
126 static void gtk_paned_map (GtkWidget *widget);
127 static void gtk_paned_unmap (GtkWidget *widget);
128 static void gtk_paned_state_changed (GtkWidget *widget,
129 GtkStateType previous_state);
130 static gboolean gtk_paned_expose (GtkWidget *widget,
131 GdkEventExpose *event);
132 static gboolean gtk_paned_enter (GtkWidget *widget,
133 GdkEventCrossing *event);
134 static gboolean gtk_paned_leave (GtkWidget *widget,
135 GdkEventCrossing *event);
136 static gboolean gtk_paned_button_press (GtkWidget *widget,
137 GdkEventButton *event);
138 static gboolean gtk_paned_button_release (GtkWidget *widget,
139 GdkEventButton *event);
140 static gboolean gtk_paned_motion (GtkWidget *widget,
141 GdkEventMotion *event);
142 static gboolean gtk_paned_focus (GtkWidget *widget,
143 GtkDirectionType direction);
144 static gboolean gtk_paned_grab_broken (GtkWidget *widget,
145 GdkEventGrabBroken *event);
146 static void gtk_paned_add (GtkContainer *container,
148 static void gtk_paned_remove (GtkContainer *container,
150 static void gtk_paned_forall (GtkContainer *container,
151 gboolean include_internals,
152 GtkCallback callback,
153 gpointer callback_data);
154 static void gtk_paned_calc_position (GtkPaned *paned,
158 static void gtk_paned_set_focus_child (GtkContainer *container,
160 static void gtk_paned_set_saved_focus (GtkPaned *paned,
162 static void gtk_paned_set_first_paned (GtkPaned *paned,
163 GtkPaned *first_paned);
164 static void gtk_paned_set_last_child1_focus (GtkPaned *paned,
166 static void gtk_paned_set_last_child2_focus (GtkPaned *paned,
168 static gboolean gtk_paned_cycle_child_focus (GtkPaned *paned,
170 static gboolean gtk_paned_cycle_handle_focus (GtkPaned *paned,
172 static gboolean gtk_paned_move_handle (GtkPaned *paned,
173 GtkScrollType scroll);
174 static gboolean gtk_paned_accept_position (GtkPaned *paned);
175 static gboolean gtk_paned_cancel_position (GtkPaned *paned);
176 static gboolean gtk_paned_toggle_handle_focus (GtkPaned *paned);
178 static GType gtk_paned_child_type (GtkContainer *container);
179 static void gtk_paned_grab_notify (GtkWidget *widget,
180 gboolean was_grabbed);
183 G_DEFINE_TYPE_WITH_CODE (GtkPaned, gtk_paned, GTK_TYPE_CONTAINER,
184 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
187 static guint signals[LAST_SIGNAL] = { 0 };
191 add_tab_bindings (GtkBindingSet *binding_set,
192 GdkModifierType modifiers)
194 gtk_binding_entry_add_signal (binding_set, GDK_Tab, modifiers,
195 "toggle-handle-focus", 0);
196 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, modifiers,
197 "toggle-handle-focus", 0);
201 add_move_binding (GtkBindingSet *binding_set,
203 GdkModifierType mask,
204 GtkScrollType scroll)
206 gtk_binding_entry_add_signal (binding_set, keyval, mask,
208 GTK_TYPE_SCROLL_TYPE, scroll);
212 gtk_paned_class_init (GtkPanedClass *class)
214 GObjectClass *object_class;
215 GtkWidgetClass *widget_class;
216 GtkContainerClass *container_class;
217 GtkPanedClass *paned_class;
218 GtkBindingSet *binding_set;
220 object_class = (GObjectClass *) class;
221 widget_class = (GtkWidgetClass *) class;
222 container_class = (GtkContainerClass *) class;
223 paned_class = (GtkPanedClass *) class;
225 object_class->set_property = gtk_paned_set_property;
226 object_class->get_property = gtk_paned_get_property;
227 object_class->finalize = gtk_paned_finalize;
229 widget_class->size_request = gtk_paned_size_request;
230 widget_class->size_allocate = gtk_paned_size_allocate;
231 widget_class->realize = gtk_paned_realize;
232 widget_class->unrealize = gtk_paned_unrealize;
233 widget_class->map = gtk_paned_map;
234 widget_class->unmap = gtk_paned_unmap;
235 widget_class->expose_event = gtk_paned_expose;
236 widget_class->focus = gtk_paned_focus;
237 widget_class->enter_notify_event = gtk_paned_enter;
238 widget_class->leave_notify_event = gtk_paned_leave;
239 widget_class->button_press_event = gtk_paned_button_press;
240 widget_class->button_release_event = gtk_paned_button_release;
241 widget_class->motion_notify_event = gtk_paned_motion;
242 widget_class->grab_broken_event = gtk_paned_grab_broken;
243 widget_class->grab_notify = gtk_paned_grab_notify;
244 widget_class->state_changed = gtk_paned_state_changed;
246 container_class->add = gtk_paned_add;
247 container_class->remove = gtk_paned_remove;
248 container_class->forall = gtk_paned_forall;
249 container_class->child_type = gtk_paned_child_type;
250 container_class->set_focus_child = gtk_paned_set_focus_child;
251 container_class->set_child_property = gtk_paned_set_child_property;
252 container_class->get_child_property = gtk_paned_get_child_property;
254 paned_class->cycle_child_focus = gtk_paned_cycle_child_focus;
255 paned_class->toggle_handle_focus = gtk_paned_toggle_handle_focus;
256 paned_class->move_handle = gtk_paned_move_handle;
257 paned_class->cycle_handle_focus = gtk_paned_cycle_handle_focus;
258 paned_class->accept_position = gtk_paned_accept_position;
259 paned_class->cancel_position = gtk_paned_cancel_position;
261 g_object_class_override_property (object_class,
265 g_object_class_install_property (object_class,
267 g_param_spec_int ("position",
269 P_("Position of paned separator in pixels (0 means all the way to the left/top)"),
273 GTK_PARAM_READWRITE));
275 g_object_class_install_property (object_class,
277 g_param_spec_boolean ("position-set",
279 P_("TRUE if the Position property should be used"),
281 GTK_PARAM_READWRITE));
283 gtk_widget_class_install_style_property (widget_class,
284 g_param_spec_int ("handle-size",
286 P_("Width of handle"),
290 GTK_PARAM_READABLE));
292 * GtkPaned:min-position:
294 * The smallest possible value for the position property. This property is derived from the
295 * size and shrinkability of the widget's children.
299 g_object_class_install_property (object_class,
301 g_param_spec_int ("min-position",
302 P_("Minimal Position"),
303 P_("Smallest possible value for the \"position\" property"),
307 GTK_PARAM_READABLE));
310 * GtkPaned:max-position:
312 * The largest possible value for the position property. This property is derived from the
313 * size and shrinkability of the widget's children.
317 g_object_class_install_property (object_class,
319 g_param_spec_int ("max-position",
320 P_("Maximal Position"),
321 P_("Largest possible value for the \"position\" property"),
325 GTK_PARAM_READABLE));
330 * The "resize" child property determines whether the child expands and
331 * shrinks along with the paned widget.
335 gtk_container_class_install_child_property (container_class,
337 g_param_spec_boolean ("resize",
339 P_("If TRUE, the child expands and shrinks along with the paned widget"),
341 GTK_PARAM_READWRITE));
346 * The "shrink" child property determines whether the child can be made
347 * smaller than its requisition.
351 gtk_container_class_install_child_property (container_class,
353 g_param_spec_boolean ("shrink",
355 P_("If TRUE, the child can be made smaller than its requisition"),
357 GTK_PARAM_READWRITE));
360 * GtkPaned::cycle-child-focus:
361 * @widget: the object that received the signal
362 * @reversed: whether cycling backward or forward
364 * The ::cycle-child-focus signal is a
365 * <link linkend="keybinding-signals">keybinding signal</link>
366 * which gets emitted to cycle the focus between the children of the paned.
368 * The default binding is f6.
372 signals [CYCLE_CHILD_FOCUS] =
373 g_signal_new (I_("cycle-child-focus"),
374 G_TYPE_FROM_CLASS (object_class),
375 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
376 G_STRUCT_OFFSET (GtkPanedClass, cycle_child_focus),
378 _gtk_marshal_BOOLEAN__BOOLEAN,
383 * GtkPaned::toggle-handle-focus:
384 * @widget: the object that received the signal
386 * The ::toggle-handle-focus is a
387 * <link linkend="keybinding-signals">keybinding signal</link>
388 * which gets emitted to accept the current position of the handle and then
389 * move focus to the next widget in the focus chain.
391 * The default binding is Tab.
395 signals [TOGGLE_HANDLE_FOCUS] =
396 g_signal_new (I_("toggle-handle-focus"),
397 G_TYPE_FROM_CLASS (object_class),
398 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
399 G_STRUCT_OFFSET (GtkPanedClass, toggle_handle_focus),
401 _gtk_marshal_BOOLEAN__VOID,
405 * GtkPaned::move-handle:
406 * @widget: the object that received the signal
407 * @scroll_type: a #GtkScrollType
409 * The ::move-handle signal is a
410 * <link linkend="keybinding-signals">keybinding signal</link>
411 * which gets emitted to move the handle when the user is using key bindings
416 signals[MOVE_HANDLE] =
417 g_signal_new (I_("move-handle"),
418 G_TYPE_FROM_CLASS (object_class),
419 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
420 G_STRUCT_OFFSET (GtkPanedClass, move_handle),
422 _gtk_marshal_BOOLEAN__ENUM,
424 GTK_TYPE_SCROLL_TYPE);
427 * GtkPaned::cycle-handle-focus:
428 * @widget: the object that received the signal
429 * @reversed: whether cycling backward or forward
431 * The ::cycle-handle-focus signal is a
432 * <link linkend="keybinding-signals">keybinding signal</link>
433 * which gets emitted to cycle whether the paned should grab focus to allow
434 * the user to change position of the handle by using key bindings.
436 * The default binding for this signal is f8.
440 signals [CYCLE_HANDLE_FOCUS] =
441 g_signal_new (I_("cycle-handle-focus"),
442 G_TYPE_FROM_CLASS (object_class),
443 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
444 G_STRUCT_OFFSET (GtkPanedClass, cycle_handle_focus),
446 _gtk_marshal_BOOLEAN__BOOLEAN,
451 * GtkPaned::accept-position:
452 * @widget: the object that received the signal
454 * The ::accept-position signal is a
455 * <link linkend="keybinding-signals">keybinding signal</link>
456 * which gets emitted to accept the current position of the handle when
457 * moving it using key bindings.
459 * The default binding for this signal is Return or Space.
463 signals [ACCEPT_POSITION] =
464 g_signal_new (I_("accept-position"),
465 G_TYPE_FROM_CLASS (object_class),
466 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
467 G_STRUCT_OFFSET (GtkPanedClass, accept_position),
469 _gtk_marshal_BOOLEAN__VOID,
473 * GtkPaned::cancel-position:
474 * @widget: the object that received the signal
476 * The ::cancel-position signal is a
477 * <link linkend="keybinding-signals">keybinding signal</link>
478 * which gets emitted to cancel moving the position of the handle using key
479 * bindings. The position of the handle will be reset to the value prior to
482 * The default binding for this signal is Escape.
486 signals [CANCEL_POSITION] =
487 g_signal_new (I_("cancel-position"),
488 G_TYPE_FROM_CLASS (object_class),
489 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
490 G_STRUCT_OFFSET (GtkPanedClass, cancel_position),
492 _gtk_marshal_BOOLEAN__VOID,
495 binding_set = gtk_binding_set_by_class (class);
498 gtk_binding_entry_add_signal (binding_set,
500 "cycle-child-focus", 1,
501 G_TYPE_BOOLEAN, FALSE);
502 gtk_binding_entry_add_signal (binding_set,
503 GDK_F6, GDK_SHIFT_MASK,
504 "cycle-child-focus", 1,
505 G_TYPE_BOOLEAN, TRUE);
508 gtk_binding_entry_add_signal (binding_set,
510 "cycle-handle-focus", 1,
511 G_TYPE_BOOLEAN, FALSE);
513 gtk_binding_entry_add_signal (binding_set,
514 GDK_F8, GDK_SHIFT_MASK,
515 "cycle-handle-focus", 1,
516 G_TYPE_BOOLEAN, TRUE);
518 add_tab_bindings (binding_set, 0);
519 add_tab_bindings (binding_set, GDK_CONTROL_MASK);
520 add_tab_bindings (binding_set, GDK_SHIFT_MASK);
521 add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK);
523 /* accept and cancel positions */
524 gtk_binding_entry_add_signal (binding_set,
526 "cancel-position", 0);
528 gtk_binding_entry_add_signal (binding_set,
530 "accept-position", 0);
531 gtk_binding_entry_add_signal (binding_set,
533 "accept-position", 0);
534 gtk_binding_entry_add_signal (binding_set,
536 "accept-position", 0);
537 gtk_binding_entry_add_signal (binding_set,
539 "accept-position", 0);
540 gtk_binding_entry_add_signal (binding_set,
542 "accept-position", 0);
545 add_move_binding (binding_set, GDK_Left, 0, GTK_SCROLL_STEP_LEFT);
546 add_move_binding (binding_set, GDK_KP_Left, 0, GTK_SCROLL_STEP_LEFT);
547 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
548 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_LEFT);
550 add_move_binding (binding_set, GDK_Right, 0, GTK_SCROLL_STEP_RIGHT);
551 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
552 add_move_binding (binding_set, GDK_KP_Right, 0, GTK_SCROLL_STEP_RIGHT);
553 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_RIGHT);
555 add_move_binding (binding_set, GDK_Up, 0, GTK_SCROLL_STEP_UP);
556 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
557 add_move_binding (binding_set, GDK_KP_Up, 0, GTK_SCROLL_STEP_UP);
558 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_UP);
559 add_move_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_UP);
560 add_move_binding (binding_set, GDK_KP_Page_Up, 0, GTK_SCROLL_PAGE_UP);
562 add_move_binding (binding_set, GDK_Down, 0, GTK_SCROLL_STEP_DOWN);
563 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
564 add_move_binding (binding_set, GDK_KP_Down, 0, GTK_SCROLL_STEP_DOWN);
565 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_DOWN);
566 add_move_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
567 add_move_binding (binding_set, GDK_KP_Page_Down, 0, GTK_SCROLL_PAGE_RIGHT);
569 add_move_binding (binding_set, GDK_Home, 0, GTK_SCROLL_START);
570 add_move_binding (binding_set, GDK_KP_Home, 0, GTK_SCROLL_START);
571 add_move_binding (binding_set, GDK_End, 0, GTK_SCROLL_END);
572 add_move_binding (binding_set, GDK_KP_End, 0, GTK_SCROLL_END);
574 g_type_class_add_private (object_class, sizeof (GtkPanedPrivate));
578 gtk_paned_child_type (GtkContainer *container)
580 GtkPaned *paned = GTK_PANED (container);
581 GtkPanedPrivate *priv = paned->priv;
583 if (!priv->child1 || !priv->child2)
584 return GTK_TYPE_WIDGET;
590 gtk_paned_init (GtkPaned *paned)
592 GtkPanedPrivate *priv;
594 gtk_widget_set_has_window (GTK_WIDGET (paned), FALSE);
595 gtk_widget_set_can_focus (GTK_WIDGET (paned), TRUE);
597 /* We only need to redraw when the handle position moves, which is
598 * independent of the overall allocation of the GtkPaned
600 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (paned), FALSE);
602 paned->priv = G_TYPE_INSTANCE_GET_PRIVATE (paned, GTK_TYPE_PANED, GtkPanedPrivate);
605 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
606 priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
611 priv->cursor_type = GDK_CROSS;
613 priv->handle_pos.width = 5;
614 priv->handle_pos.height = 5;
615 priv->position_set = FALSE;
616 priv->last_allocation = -1;
617 priv->in_drag = FALSE;
619 priv->last_child1_focus = NULL;
620 priv->last_child2_focus = NULL;
621 priv->in_recursion = FALSE;
622 priv->handle_prelit = FALSE;
623 priv->original_position = -1;
625 priv->handle_pos.x = -1;
626 priv->handle_pos.y = -1;
632 gtk_paned_set_property (GObject *object,
637 GtkPaned *paned = GTK_PANED (object);
638 GtkPanedPrivate *priv = paned->priv;
642 case PROP_ORIENTATION:
643 priv->orientation = g_value_get_enum (value);
645 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
646 priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
648 priv->cursor_type = GDK_SB_V_DOUBLE_ARROW;
650 /* state_changed updates the cursor */
651 gtk_paned_state_changed (GTK_WIDGET (paned), GTK_WIDGET (paned)->state);
652 gtk_widget_queue_resize (GTK_WIDGET (paned));
655 gtk_paned_set_position (paned, g_value_get_int (value));
657 case PROP_POSITION_SET:
658 priv->position_set = g_value_get_boolean (value);
659 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
662 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
668 gtk_paned_get_property (GObject *object,
673 GtkPaned *paned = GTK_PANED (object);
674 GtkPanedPrivate *priv = paned->priv;
678 case PROP_ORIENTATION:
679 g_value_set_enum (value, priv->orientation);
682 g_value_set_int (value, priv->child1_size);
684 case PROP_POSITION_SET:
685 g_value_set_boolean (value, priv->position_set);
687 case PROP_MIN_POSITION:
688 g_value_set_int (value, priv->min_position);
690 case PROP_MAX_POSITION:
691 g_value_set_int (value, priv->max_position);
694 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
700 gtk_paned_set_child_property (GtkContainer *container,
706 GtkPaned *paned = GTK_PANED (container);
707 GtkPanedPrivate *priv = paned->priv;
708 gboolean old_value, new_value;
710 g_assert (child == priv->child1 || child == priv->child2);
712 new_value = g_value_get_boolean (value);
715 case CHILD_PROP_RESIZE:
716 if (child == priv->child1)
718 old_value = priv->child1_resize;
719 priv->child1_resize = new_value;
723 old_value = priv->child2_resize;
724 priv->child2_resize = new_value;
727 case CHILD_PROP_SHRINK:
728 if (child == priv->child1)
730 old_value = priv->child1_shrink;
731 priv->child1_shrink = new_value;
735 old_value = priv->child2_shrink;
736 priv->child2_shrink = new_value;
740 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
741 old_value = -1; /* quiet gcc */
744 if (old_value != new_value)
745 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
749 gtk_paned_get_child_property (GtkContainer *container,
755 GtkPaned *paned = GTK_PANED (container);
756 GtkPanedPrivate *priv = paned->priv;
758 g_assert (child == priv->child1 || child == priv->child2);
762 case CHILD_PROP_RESIZE:
763 if (child == priv->child1)
764 g_value_set_boolean (value, priv->child1_resize);
766 g_value_set_boolean (value, priv->child2_resize);
768 case CHILD_PROP_SHRINK:
769 if (child == priv->child1)
770 g_value_set_boolean (value, priv->child1_shrink);
772 g_value_set_boolean (value, priv->child2_shrink);
775 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
781 gtk_paned_finalize (GObject *object)
783 GtkPaned *paned = GTK_PANED (object);
785 gtk_paned_set_saved_focus (paned, NULL);
786 gtk_paned_set_first_paned (paned, NULL);
788 G_OBJECT_CLASS (gtk_paned_parent_class)->finalize (object);
792 gtk_paned_size_request (GtkWidget *widget,
793 GtkRequisition *requisition)
795 GtkPaned *paned = GTK_PANED (widget);
796 GtkPanedPrivate *priv = paned->priv;
797 GtkRequisition child_requisition;
800 requisition->width = 0;
801 requisition->height = 0;
803 if (priv->child1 && gtk_widget_get_visible (priv->child1))
805 gtk_widget_size_request (priv->child1, &child_requisition);
807 requisition->height = child_requisition.height;
808 requisition->width = child_requisition.width;
811 if (priv->child2 && gtk_widget_get_visible (priv->child2))
813 gtk_widget_size_request (priv->child2, &child_requisition);
815 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
817 requisition->height = MAX (requisition->height,
818 child_requisition.height);
819 requisition->width += child_requisition.width;
823 requisition->width = MAX (requisition->width,
824 child_requisition.width);
825 requisition->height += child_requisition.height;
829 border_width = gtk_container_get_border_width (GTK_CONTAINER (paned));
830 requisition->width += border_width * 2;
831 requisition->height += border_width * 2;
833 if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
834 priv->child2 && gtk_widget_get_visible (priv->child2))
838 gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
840 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
841 requisition->width += handle_size;
843 requisition->height += handle_size;
848 flip_child (GtkWidget *widget,
849 GtkAllocation *child_pos)
851 gint x = widget->allocation.x;
852 gint width = widget->allocation.width;
854 child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
858 gtk_paned_size_allocate (GtkWidget *widget,
859 GtkAllocation *allocation)
861 GtkPaned *paned = GTK_PANED (widget);
862 GtkPanedPrivate *priv = paned->priv;
865 border_width = gtk_container_get_border_width (GTK_CONTAINER (paned));
867 widget->allocation = *allocation;
869 if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
870 priv->child2 && gtk_widget_get_visible (priv->child2))
872 GtkRequisition child1_requisition;
873 GtkRequisition child2_requisition;
874 GtkAllocation child1_allocation;
875 GtkAllocation child2_allocation;
876 GdkRectangle old_handle_pos;
879 gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
881 gtk_widget_get_child_requisition (priv->child1, &child1_requisition);
882 gtk_widget_get_child_requisition (priv->child2, &child2_requisition);
884 old_handle_pos = priv->handle_pos;
886 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
888 gtk_paned_calc_position (paned,
889 MAX (1, widget->allocation.width
892 child1_requisition.width,
893 child2_requisition.width);
895 priv->handle_pos.x = widget->allocation.x + priv->child1_size + border_width;
896 priv->handle_pos.y = widget->allocation.y + border_width;
897 priv->handle_pos.width = handle_size;
898 priv->handle_pos.height = MAX (1, widget->allocation.height - 2 * border_width);
900 child1_allocation.height = child2_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
901 child1_allocation.width = MAX (1, priv->child1_size);
902 child1_allocation.x = widget->allocation.x + border_width;
903 child1_allocation.y = child2_allocation.y = widget->allocation.y + border_width;
905 child2_allocation.x = child1_allocation.x + priv->child1_size + priv->handle_pos.width;
906 child2_allocation.width = MAX (1, widget->allocation.x + widget->allocation.width - child2_allocation.x - border_width);
908 if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
910 flip_child (widget, &(child2_allocation));
911 flip_child (widget, &(child1_allocation));
912 flip_child (widget, &(priv->handle_pos));
917 gtk_paned_calc_position (paned,
918 MAX (1, widget->allocation.height
921 child1_requisition.height,
922 child2_requisition.height);
924 priv->handle_pos.x = widget->allocation.x + border_width;
925 priv->handle_pos.y = widget->allocation.y + priv->child1_size + border_width;
926 priv->handle_pos.width = MAX (1, (gint) widget->allocation.width - 2 * border_width);
927 priv->handle_pos.height = handle_size;
929 child1_allocation.width = child2_allocation.width = MAX (1, (gint) allocation->width - border_width * 2);
930 child1_allocation.height = MAX (1, priv->child1_size);
931 child1_allocation.x = child2_allocation.x = widget->allocation.x + border_width;
932 child1_allocation.y = widget->allocation.y + border_width;
934 child2_allocation.y = child1_allocation.y + priv->child1_size + priv->handle_pos.height;
935 child2_allocation.height = MAX (1, widget->allocation.y + widget->allocation.height - child2_allocation.y - border_width);
938 if (gtk_widget_get_mapped (widget) &&
939 (old_handle_pos.x != priv->handle_pos.x ||
940 old_handle_pos.y != priv->handle_pos.y ||
941 old_handle_pos.width != priv->handle_pos.width ||
942 old_handle_pos.height != priv->handle_pos.height))
944 gdk_window_invalidate_rect (widget->window, &old_handle_pos, FALSE);
945 gdk_window_invalidate_rect (widget->window, &priv->handle_pos, FALSE);
948 if (gtk_widget_get_realized (widget))
950 if (gtk_widget_get_mapped (widget))
951 gdk_window_show (priv->handle);
953 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
955 gdk_window_move_resize (priv->handle,
959 priv->handle_pos.height);
963 gdk_window_move_resize (priv->handle,
966 priv->handle_pos.width,
971 /* Now allocate the childen, making sure, when resizing not to
972 * overlap the windows
974 if (gtk_widget_get_mapped (widget) &&
976 ((priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
977 priv->child1->allocation.width < child1_allocation.width) ||
979 (priv->orientation == GTK_ORIENTATION_VERTICAL &&
980 priv->child1->allocation.height < child1_allocation.height)))
982 gtk_widget_size_allocate (priv->child2, &child2_allocation);
983 gtk_widget_size_allocate (priv->child1, &child1_allocation);
987 gtk_widget_size_allocate (priv->child1, &child1_allocation);
988 gtk_widget_size_allocate (priv->child2, &child2_allocation);
993 GtkAllocation child_allocation;
995 if (gtk_widget_get_realized (widget))
996 gdk_window_hide (priv->handle);
999 gtk_widget_set_child_visible (priv->child1, TRUE);
1001 gtk_widget_set_child_visible (priv->child2, TRUE);
1003 child_allocation.x = widget->allocation.x + border_width;
1004 child_allocation.y = widget->allocation.y + border_width;
1005 child_allocation.width = MAX (1, allocation->width - 2 * border_width);
1006 child_allocation.height = MAX (1, allocation->height - 2 * border_width);
1008 if (priv->child1 && gtk_widget_get_visible (priv->child1))
1009 gtk_widget_size_allocate (priv->child1, &child_allocation);
1010 else if (priv->child2 && gtk_widget_get_visible (priv->child2))
1011 gtk_widget_size_allocate (priv->child2, &child_allocation);
1016 gtk_paned_realize (GtkWidget *widget)
1018 GtkPaned *paned = GTK_PANED (widget);
1019 GtkPanedPrivate *priv = paned->priv;
1020 GdkWindowAttr attributes;
1021 gint attributes_mask;
1023 gtk_widget_set_realized (widget, TRUE);
1025 widget->window = gtk_widget_get_parent_window (widget);
1026 g_object_ref (widget->window);
1028 attributes.window_type = GDK_WINDOW_CHILD;
1029 attributes.wclass = GDK_INPUT_ONLY;
1030 attributes.x = priv->handle_pos.x;
1031 attributes.y = priv->handle_pos.y;
1032 attributes.width = priv->handle_pos.width;
1033 attributes.height = priv->handle_pos.height;
1034 attributes.event_mask = gtk_widget_get_events (widget);
1035 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1036 GDK_BUTTON_RELEASE_MASK |
1037 GDK_ENTER_NOTIFY_MASK |
1038 GDK_LEAVE_NOTIFY_MASK |
1039 GDK_POINTER_MOTION_MASK |
1040 GDK_POINTER_MOTION_HINT_MASK);
1041 attributes_mask = GDK_WA_X | GDK_WA_Y;
1042 if (gtk_widget_is_sensitive (widget))
1044 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1046 attributes_mask |= GDK_WA_CURSOR;
1049 priv->handle = gdk_window_new (widget->window,
1050 &attributes, attributes_mask);
1051 gdk_window_set_user_data (priv->handle, paned);
1052 if (attributes_mask & GDK_WA_CURSOR)
1053 gdk_cursor_unref (attributes.cursor);
1055 widget->style = gtk_style_attach (widget->style, widget->window);
1057 if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
1058 priv->child2 && gtk_widget_get_visible (priv->child2))
1059 gdk_window_show (priv->handle);
1063 gtk_paned_unrealize (GtkWidget *widget)
1065 GtkPaned *paned = GTK_PANED (widget);
1066 GtkPanedPrivate *priv = paned->priv;
1070 gdk_window_set_user_data (priv->handle, NULL);
1071 gdk_window_destroy (priv->handle);
1072 priv->handle = NULL;
1075 gtk_paned_set_last_child1_focus (paned, NULL);
1076 gtk_paned_set_last_child2_focus (paned, NULL);
1077 gtk_paned_set_saved_focus (paned, NULL);
1078 gtk_paned_set_first_paned (paned, NULL);
1080 GTK_WIDGET_CLASS (gtk_paned_parent_class)->unrealize (widget);
1084 gtk_paned_map (GtkWidget *widget)
1086 GtkPaned *paned = GTK_PANED (widget);
1087 GtkPanedPrivate *priv = paned->priv;
1089 gdk_window_show (priv->handle);
1091 GTK_WIDGET_CLASS (gtk_paned_parent_class)->map (widget);
1095 gtk_paned_unmap (GtkWidget *widget)
1097 GtkPaned *paned = GTK_PANED (widget);
1098 GtkPanedPrivate *priv = paned->priv;
1100 gdk_window_hide (priv->handle);
1102 GTK_WIDGET_CLASS (gtk_paned_parent_class)->unmap (widget);
1106 gtk_paned_expose (GtkWidget *widget,
1107 GdkEventExpose *event)
1109 GtkPaned *paned = GTK_PANED (widget);
1110 GtkPanedPrivate *priv = paned->priv;
1112 if (gtk_widget_get_visible (widget) && gtk_widget_get_mapped (widget) &&
1113 priv->child1 && gtk_widget_get_visible (priv->child1) &&
1114 priv->child2 && gtk_widget_get_visible (priv->child2))
1118 if (gtk_widget_is_focus (widget))
1119 state = GTK_STATE_SELECTED;
1120 else if (priv->handle_prelit)
1121 state = GTK_STATE_PRELIGHT;
1123 state = gtk_widget_get_state (widget);
1125 gtk_paint_handle (widget->style, widget->window,
1126 state, GTK_SHADOW_NONE,
1127 &priv->handle_pos, widget, "paned",
1128 priv->handle_pos.x, priv->handle_pos.y,
1129 priv->handle_pos.width, priv->handle_pos.height,
1130 !priv->orientation);
1133 /* Chain up to draw children */
1134 GTK_WIDGET_CLASS (gtk_paned_parent_class)->expose_event (widget, event);
1140 is_rtl (GtkPaned *paned)
1142 GtkPanedPrivate *priv = paned->priv;
1144 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1145 gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL)
1154 update_drag (GtkPaned *paned)
1156 GtkPanedPrivate *priv = paned->priv;
1161 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1162 gtk_widget_get_pointer (GTK_WIDGET (paned), &pos, NULL);
1164 gtk_widget_get_pointer (GTK_WIDGET (paned), NULL, &pos);
1166 pos -= priv->drag_pos;
1170 gtk_widget_style_get (GTK_WIDGET (paned),
1171 "handle-size", &handle_size,
1174 size = GTK_WIDGET (paned)->allocation.width - pos - handle_size;
1181 size -= gtk_container_get_border_width (GTK_CONTAINER (paned));
1183 size = CLAMP (size, priv->min_position, priv->max_position);
1185 if (size != priv->child1_size)
1186 gtk_paned_set_position (paned, size);
1190 gtk_paned_enter (GtkWidget *widget,
1191 GdkEventCrossing *event)
1193 GtkPaned *paned = GTK_PANED (widget);
1194 GtkPanedPrivate *priv = paned->priv;
1197 update_drag (paned);
1200 priv->handle_prelit = TRUE;
1201 gtk_widget_queue_draw_area (widget,
1204 priv->handle_pos.width,
1205 priv->handle_pos.height);
1212 gtk_paned_leave (GtkWidget *widget,
1213 GdkEventCrossing *event)
1215 GtkPaned *paned = GTK_PANED (widget);
1216 GtkPanedPrivate *priv = paned->priv;
1219 update_drag (paned);
1222 priv->handle_prelit = FALSE;
1223 gtk_widget_queue_draw_area (widget,
1226 priv->handle_pos.width,
1227 priv->handle_pos.height);
1234 gtk_paned_focus (GtkWidget *widget,
1235 GtkDirectionType direction)
1240 /* This is a hack, but how can this be done without
1241 * excessive cut-and-paste from gtkcontainer.c?
1244 gtk_widget_set_can_focus (widget, FALSE);
1245 retval = GTK_WIDGET_CLASS (gtk_paned_parent_class)->focus (widget, direction);
1246 gtk_widget_set_can_focus (widget, TRUE);
1252 gtk_paned_button_press (GtkWidget *widget,
1253 GdkEventButton *event)
1255 GtkPaned *paned = GTK_PANED (widget);
1256 GtkPanedPrivate *priv = paned->priv;
1258 if (!priv->in_drag &&
1259 (event->window == priv->handle) && (event->button == 1))
1261 /* We need a server grab here, not gtk_grab_add(), since
1262 * we don't want to pass events on to the widget's children */
1263 if (gdk_device_grab (event->device,
1265 GDK_OWNERSHIP_WINDOW, FALSE,
1266 GDK_POINTER_MOTION_HINT_MASK
1267 | GDK_BUTTON1_MOTION_MASK
1268 | GDK_BUTTON_RELEASE_MASK
1269 | GDK_ENTER_NOTIFY_MASK
1270 | GDK_LEAVE_NOTIFY_MASK,
1271 NULL, event->time) != GDK_GRAB_SUCCESS)
1274 priv->in_drag = TRUE;
1275 priv->grab_time = event->time;
1276 priv->grab_device = event->device;
1278 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1279 priv->drag_pos = event->x;
1281 priv->drag_pos = event->y;
1290 gtk_paned_grab_broken (GtkWidget *widget,
1291 GdkEventGrabBroken *event)
1293 GtkPaned *paned = GTK_PANED (widget);
1294 GtkPanedPrivate *priv = paned->priv;
1296 priv->in_drag = FALSE;
1297 priv->drag_pos = -1;
1298 priv->position_set = TRUE;
1304 stop_drag (GtkPaned *paned)
1306 GtkPanedPrivate *priv = paned->priv;
1308 priv->in_drag = FALSE;
1309 priv->drag_pos = -1;
1310 priv->position_set = TRUE;
1312 gdk_device_ungrab (priv->grab_device,
1314 priv->grab_device = NULL;
1318 gtk_paned_grab_notify (GtkWidget *widget,
1319 gboolean was_grabbed)
1321 GtkPaned *paned = GTK_PANED (widget);
1322 GtkPanedPrivate *priv = paned->priv;
1323 GdkDevice *grab_device;
1325 grab_device = priv->grab_device;
1327 if (priv->in_drag && grab_device &&
1328 gtk_widget_device_is_shadowed (widget, grab_device))
1333 gtk_paned_state_changed (GtkWidget *widget,
1334 GtkStateType previous_state)
1336 GtkPaned *paned = GTK_PANED (widget);
1337 GtkPanedPrivate *priv = paned->priv;
1340 if (gtk_widget_get_realized (widget))
1342 if (gtk_widget_is_sensitive (widget))
1343 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1348 gdk_window_set_cursor (priv->handle, cursor);
1351 gdk_cursor_unref (cursor);
1356 gtk_paned_button_release (GtkWidget *widget,
1357 GdkEventButton *event)
1359 GtkPaned *paned = GTK_PANED (widget);
1360 GtkPanedPrivate *priv = paned->priv;
1362 if (priv->in_drag && (event->button == 1))
1373 gtk_paned_motion (GtkWidget *widget,
1374 GdkEventMotion *event)
1376 GtkPaned *paned = GTK_PANED (widget);
1377 GtkPanedPrivate *priv = paned->priv;
1381 update_drag (paned);
1390 * @orientation: the paned's orientation.
1392 * Creates a new #GtkPaned widget.
1394 * Return value: a new #GtkPaned.
1399 gtk_paned_new (GtkOrientation orientation)
1401 return g_object_new (GTK_TYPE_PANED,
1402 "orientation", orientation,
1407 gtk_paned_add1 (GtkPaned *paned,
1410 gtk_paned_pack1 (paned, widget, FALSE, TRUE);
1414 gtk_paned_add2 (GtkPaned *paned,
1417 gtk_paned_pack2 (paned, widget, TRUE, TRUE);
1421 gtk_paned_pack1 (GtkPaned *paned,
1426 GtkPanedPrivate *priv;
1428 g_return_if_fail (GTK_IS_PANED (paned));
1429 g_return_if_fail (GTK_IS_WIDGET (child));
1435 priv->child1 = child;
1436 priv->child1_resize = resize;
1437 priv->child1_shrink = shrink;
1439 gtk_widget_set_parent (child, GTK_WIDGET (paned));
1444 gtk_paned_pack2 (GtkPaned *paned,
1449 GtkPanedPrivate *priv;
1451 g_return_if_fail (GTK_IS_PANED (paned));
1452 g_return_if_fail (GTK_IS_WIDGET (child));
1458 priv->child2 = child;
1459 priv->child2_resize = resize;
1460 priv->child2_shrink = shrink;
1462 gtk_widget_set_parent (child, GTK_WIDGET (paned));
1468 gtk_paned_add (GtkContainer *container,
1471 GtkPanedPrivate *priv;
1474 g_return_if_fail (GTK_IS_PANED (container));
1476 paned = GTK_PANED (container);
1480 gtk_paned_add1 (paned, widget);
1481 else if (!priv->child2)
1482 gtk_paned_add2 (paned, widget);
1484 g_warning ("GtkPaned cannot have more than 2 children\n");
1488 gtk_paned_remove (GtkContainer *container,
1491 GtkPaned *paned = GTK_PANED (container);
1492 GtkPanedPrivate *priv = paned->priv;
1493 gboolean was_visible;
1495 was_visible = gtk_widget_get_visible (widget);
1497 if (priv->child1 == widget)
1499 gtk_widget_unparent (widget);
1501 priv->child1 = NULL;
1503 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1504 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1506 else if (priv->child2 == widget)
1508 gtk_widget_unparent (widget);
1510 priv->child2 = NULL;
1512 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1513 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1518 gtk_paned_forall (GtkContainer *container,
1519 gboolean include_internals,
1520 GtkCallback callback,
1521 gpointer callback_data)
1523 GtkPanedPrivate *priv;
1526 g_return_if_fail (callback != NULL);
1528 paned = GTK_PANED (container);
1532 (*callback) (priv->child1, callback_data);
1534 (*callback) (priv->child2, callback_data);
1538 * gtk_paned_get_position:
1539 * @paned: a #GtkPaned widget
1541 * Obtains the position of the divider between the two panes.
1543 * Return value: position of the divider
1546 gtk_paned_get_position (GtkPaned *paned)
1548 g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1550 return paned->priv->child1_size;
1554 * gtk_paned_set_position:
1555 * @paned: a #GtkPaned widget
1556 * @position: pixel position of divider, a negative value means that the position
1559 * Sets the position of the divider between the two panes.
1562 gtk_paned_set_position (GtkPaned *paned,
1565 GtkPanedPrivate *priv;
1568 g_return_if_fail (GTK_IS_PANED (paned));
1572 if (priv->child1_size == position)
1575 object = G_OBJECT (paned);
1579 /* We don't clamp here - the assumption is that
1580 * if the total allocation changes at the same time
1581 * as the position, the position set is with reference
1582 * to the new total size. If only the position changes,
1583 * then clamping will occur in gtk_paned_calc_position()
1586 priv->child1_size = position;
1587 priv->position_set = TRUE;
1591 priv->position_set = FALSE;
1594 g_object_freeze_notify (object);
1595 g_object_notify (object, "position");
1596 g_object_notify (object, "position-set");
1597 g_object_thaw_notify (object);
1599 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
1602 /* Hacky work-around for bug #144269 */
1603 if (priv->child2 != NULL)
1605 gtk_widget_queue_draw (priv->child2);
1611 * gtk_paned_get_child1:
1612 * @paned: a #GtkPaned widget
1614 * Obtains the first child of the paned widget.
1616 * Return value: first child, or %NULL if it is not set.
1621 gtk_paned_get_child1 (GtkPaned *paned)
1623 g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1625 return paned->priv->child1;
1629 * gtk_paned_get_child2:
1630 * @paned: a #GtkPaned widget
1632 * Obtains the second child of the paned widget.
1634 * Return value: second child, or %NULL if it is not set.
1639 gtk_paned_get_child2 (GtkPaned *paned)
1641 g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1643 return paned->priv->child2;
1647 gtk_paned_calc_position (GtkPaned *paned,
1652 GtkPanedPrivate *priv = paned->priv;
1654 gint old_min_position;
1655 gint old_max_position;
1657 old_position = priv->child1_size;
1658 old_min_position = priv->min_position;
1659 old_max_position = priv->max_position;
1661 priv->min_position = priv->child1_shrink ? 0 : child1_req;
1663 priv->max_position = allocation;
1664 if (!priv->child2_shrink)
1665 priv->max_position = MAX (1, priv->max_position - child2_req);
1666 priv->max_position = MAX (priv->min_position, priv->max_position);
1668 if (!priv->position_set)
1670 if (priv->child1_resize && !priv->child2_resize)
1671 priv->child1_size = MAX (0, allocation - child2_req);
1672 else if (!priv->child1_resize && priv->child2_resize)
1673 priv->child1_size = child1_req;
1674 else if (child1_req + child2_req != 0)
1675 priv->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req)) + 0.5;
1677 priv->child1_size = allocation * 0.5 + 0.5;
1681 /* If the position was set before the initial allocation.
1682 * (priv->last_allocation <= 0) just clamp it and leave it.
1684 if (priv->last_allocation > 0)
1686 if (priv->child1_resize && !priv->child2_resize)
1687 priv->child1_size += allocation - priv->last_allocation;
1688 else if (!(!priv->child1_resize && priv->child2_resize))
1689 priv->child1_size = allocation * ((gdouble) priv->child1_size / (priv->last_allocation)) + 0.5;
1693 priv->child1_size = CLAMP (priv->child1_size,
1695 priv->max_position);
1698 gtk_widget_set_child_visible (priv->child1, priv->child1_size != 0);
1701 gtk_widget_set_child_visible (priv->child2, priv->child1_size != allocation);
1703 g_object_freeze_notify (G_OBJECT (paned));
1704 if (priv->child1_size != old_position)
1705 g_object_notify (G_OBJECT (paned), "position");
1706 if (priv->min_position != old_min_position)
1707 g_object_notify (G_OBJECT (paned), "min-position");
1708 if (priv->max_position != old_max_position)
1709 g_object_notify (G_OBJECT (paned), "max-position");
1710 g_object_thaw_notify (G_OBJECT (paned));
1712 priv->last_allocation = allocation;
1716 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1718 GtkPanedPrivate *priv = paned->priv;
1720 if (priv->saved_focus)
1721 g_object_remove_weak_pointer (G_OBJECT (priv->saved_focus),
1722 (gpointer *)&(priv->saved_focus));
1724 priv->saved_focus = widget;
1726 if (priv->saved_focus)
1727 g_object_add_weak_pointer (G_OBJECT (priv->saved_focus),
1728 (gpointer *)&(priv->saved_focus));
1732 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1734 GtkPanedPrivate *priv = paned->priv;
1736 if (priv->first_paned)
1737 g_object_remove_weak_pointer (G_OBJECT (priv->first_paned),
1738 (gpointer *)&(priv->first_paned));
1740 priv->first_paned = first_paned;
1742 if (priv->first_paned)
1743 g_object_add_weak_pointer (G_OBJECT (priv->first_paned),
1744 (gpointer *)&(priv->first_paned));
1748 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1750 GtkPanedPrivate *priv = paned->priv;
1752 if (priv->last_child1_focus)
1753 g_object_remove_weak_pointer (G_OBJECT (priv->last_child1_focus),
1754 (gpointer *)&(priv->last_child1_focus));
1756 priv->last_child1_focus = widget;
1758 if (priv->last_child1_focus)
1759 g_object_add_weak_pointer (G_OBJECT (priv->last_child1_focus),
1760 (gpointer *)&(priv->last_child1_focus));
1764 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1766 GtkPanedPrivate *priv = paned->priv;
1768 if (priv->last_child2_focus)
1769 g_object_remove_weak_pointer (G_OBJECT (priv->last_child2_focus),
1770 (gpointer *)&(priv->last_child2_focus));
1772 priv->last_child2_focus = widget;
1774 if (priv->last_child2_focus)
1775 g_object_add_weak_pointer (G_OBJECT (priv->last_child2_focus),
1776 (gpointer *)&(priv->last_child2_focus));
1780 paned_get_focus_widget (GtkPaned *paned)
1782 GtkWidget *toplevel;
1784 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
1785 if (gtk_widget_is_toplevel (toplevel))
1786 return GTK_WINDOW (toplevel)->focus_widget;
1792 gtk_paned_set_focus_child (GtkContainer *container,
1793 GtkWidget *focus_child)
1796 GtkPanedPrivate *priv;
1797 GtkWidget *container_focus_child;
1799 g_return_if_fail (GTK_IS_PANED (container));
1801 paned = GTK_PANED (container);
1804 if (focus_child == NULL)
1806 GtkWidget *last_focus;
1809 last_focus = paned_get_focus_widget (paned);
1813 /* If there is one or more paned widgets between us and the
1814 * focus widget, we want the topmost of those as last_focus
1816 for (w = last_focus; w != GTK_WIDGET (paned); w = w->parent)
1817 if (GTK_IS_PANED (w))
1820 container_focus_child = gtk_container_get_focus_child (container);
1821 if (container_focus_child == priv->child1)
1822 gtk_paned_set_last_child1_focus (paned, last_focus);
1823 else if (container_focus_child == priv->child2)
1824 gtk_paned_set_last_child2_focus (paned, last_focus);
1828 if (GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child)
1829 GTK_CONTAINER_CLASS (gtk_paned_parent_class)->set_focus_child (container, focus_child);
1833 gtk_paned_get_cycle_chain (GtkPaned *paned,
1834 GtkDirectionType direction,
1837 GtkPanedPrivate *priv = paned->priv;
1838 GtkContainer *container = GTK_CONTAINER (paned);
1839 GtkWidget *ancestor = NULL;
1840 GtkWidget *focus_child;
1841 GList *temp_list = NULL;
1844 if (priv->in_recursion)
1847 g_assert (widgets != NULL);
1849 if (priv->last_child1_focus &&
1850 !gtk_widget_is_ancestor (priv->last_child1_focus, GTK_WIDGET (paned)))
1852 gtk_paned_set_last_child1_focus (paned, NULL);
1855 if (priv->last_child2_focus &&
1856 !gtk_widget_is_ancestor (priv->last_child2_focus, GTK_WIDGET (paned)))
1858 gtk_paned_set_last_child2_focus (paned, NULL);
1861 if (GTK_WIDGET (paned)->parent)
1862 ancestor = gtk_widget_get_ancestor (GTK_WIDGET (paned)->parent, GTK_TYPE_PANED);
1864 /* The idea here is that temp_list is a list of widgets we want to cycle
1865 * to. The list is prioritized so that the first element is our first
1866 * choice, the next our second, and so on.
1868 * We can't just use g_list_reverse(), because we want to try
1869 * priv->last_child?_focus before priv->child?, both when we
1870 * are going forward and backward.
1872 focus_child = gtk_container_get_focus_child (container);
1873 if (direction == GTK_DIR_TAB_FORWARD)
1875 if (focus_child == priv->child1)
1877 temp_list = g_list_append (temp_list, priv->last_child2_focus);
1878 temp_list = g_list_append (temp_list, priv->child2);
1879 temp_list = g_list_append (temp_list, ancestor);
1881 else if (focus_child == priv->child2)
1883 temp_list = g_list_append (temp_list, ancestor);
1884 temp_list = g_list_append (temp_list, priv->last_child1_focus);
1885 temp_list = g_list_append (temp_list, priv->child1);
1889 temp_list = g_list_append (temp_list, priv->last_child1_focus);
1890 temp_list = g_list_append (temp_list, priv->child1);
1891 temp_list = g_list_append (temp_list, priv->last_child2_focus);
1892 temp_list = g_list_append (temp_list, priv->child2);
1893 temp_list = g_list_append (temp_list, ancestor);
1898 if (focus_child == priv->child1)
1900 temp_list = g_list_append (temp_list, ancestor);
1901 temp_list = g_list_append (temp_list, priv->last_child2_focus);
1902 temp_list = g_list_append (temp_list, priv->child2);
1904 else if (focus_child == priv->child2)
1906 temp_list = g_list_append (temp_list, priv->last_child1_focus);
1907 temp_list = g_list_append (temp_list, priv->child1);
1908 temp_list = g_list_append (temp_list, ancestor);
1912 temp_list = g_list_append (temp_list, priv->last_child2_focus);
1913 temp_list = g_list_append (temp_list, priv->child2);
1914 temp_list = g_list_append (temp_list, priv->last_child1_focus);
1915 temp_list = g_list_append (temp_list, priv->child1);
1916 temp_list = g_list_append (temp_list, ancestor);
1920 /* Walk the list and expand all the paned widgets. */
1921 for (list = temp_list; list != NULL; list = list->next)
1923 GtkWidget *widget = list->data;
1927 if (GTK_IS_PANED (widget))
1929 priv->in_recursion = TRUE;
1930 gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
1931 priv->in_recursion = FALSE;
1935 *widgets = g_list_append (*widgets, widget);
1940 g_list_free (temp_list);
1944 gtk_paned_cycle_child_focus (GtkPaned *paned,
1947 GList *cycle_chain = NULL;
1950 GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
1952 /* ignore f6 if the handle is focused */
1953 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
1956 /* we can't just let the event propagate up the hierarchy,
1957 * because the paned will want to cycle focus _unless_ an
1958 * ancestor paned handles the event
1960 gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
1962 for (list = cycle_chain; list != NULL; list = list->next)
1963 if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
1966 g_list_free (cycle_chain);
1972 get_child_panes (GtkWidget *widget,
1975 if (!widget || !gtk_widget_get_realized (widget))
1978 if (GTK_IS_PANED (widget))
1980 GtkPaned *paned = GTK_PANED (widget);
1981 GtkPanedPrivate *priv = paned->priv;
1983 get_child_panes (priv->child1, panes);
1984 *panes = g_list_prepend (*panes, widget);
1985 get_child_panes (priv->child2, panes);
1987 else if (GTK_IS_CONTAINER (widget))
1989 gtk_container_forall (GTK_CONTAINER (widget),
1990 (GtkCallback)get_child_panes, panes);
1995 get_all_panes (GtkPaned *paned)
1997 GtkPaned *topmost = NULL;
1998 GList *result = NULL;
2001 for (w = GTK_WIDGET (paned); w != NULL; w = w->parent)
2003 if (GTK_IS_PANED (w))
2004 topmost = GTK_PANED (w);
2009 get_child_panes (GTK_WIDGET (topmost), &result);
2011 return g_list_reverse (result);
2015 gtk_paned_find_neighbours (GtkPaned *paned,
2022 all_panes = get_all_panes (paned);
2023 g_assert (all_panes);
2025 this_link = g_list_find (all_panes, paned);
2027 g_assert (this_link);
2029 if (this_link->next)
2030 *next = this_link->next->data;
2032 *next = all_panes->data;
2034 if (this_link->prev)
2035 *prev = this_link->prev->data;
2037 *prev = g_list_last (all_panes)->data;
2039 g_list_free (all_panes);
2043 gtk_paned_move_handle (GtkPaned *paned,
2044 GtkScrollType scroll)
2046 GtkPanedPrivate *priv = paned->priv;
2048 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2055 SINGLE_STEP_SIZE = 1,
2059 new_position = old_position = gtk_paned_get_position (paned);
2064 case GTK_SCROLL_STEP_LEFT:
2065 case GTK_SCROLL_STEP_UP:
2066 case GTK_SCROLL_STEP_BACKWARD:
2067 increment = - SINGLE_STEP_SIZE;
2070 case GTK_SCROLL_STEP_RIGHT:
2071 case GTK_SCROLL_STEP_DOWN:
2072 case GTK_SCROLL_STEP_FORWARD:
2073 increment = SINGLE_STEP_SIZE;
2076 case GTK_SCROLL_PAGE_LEFT:
2077 case GTK_SCROLL_PAGE_UP:
2078 case GTK_SCROLL_PAGE_BACKWARD:
2079 increment = - PAGE_STEP_SIZE;
2082 case GTK_SCROLL_PAGE_RIGHT:
2083 case GTK_SCROLL_PAGE_DOWN:
2084 case GTK_SCROLL_PAGE_FORWARD:
2085 increment = PAGE_STEP_SIZE;
2088 case GTK_SCROLL_START:
2089 new_position = priv->min_position;
2092 case GTK_SCROLL_END:
2093 new_position = priv->max_position;
2103 increment = -increment;
2105 new_position = old_position + increment;
2108 new_position = CLAMP (new_position, priv->min_position, priv->max_position);
2110 if (old_position != new_position)
2111 gtk_paned_set_position (paned, new_position);
2120 gtk_paned_restore_focus (GtkPaned *paned)
2122 GtkPanedPrivate *priv = paned->priv;
2124 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2126 if (priv->saved_focus &&
2127 gtk_widget_get_sensitive (priv->saved_focus))
2129 gtk_widget_grab_focus (priv->saved_focus);
2133 /* the saved focus is somehow not available for focusing,
2135 * 1) tabbing into the paned widget
2136 * if that didn't work,
2137 * 2) unset focus for the window if there is one
2140 if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
2142 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2144 if (GTK_IS_WINDOW (toplevel))
2145 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
2149 gtk_paned_set_saved_focus (paned, NULL);
2150 gtk_paned_set_first_paned (paned, NULL);
2155 gtk_paned_accept_position (GtkPaned *paned)
2157 GtkPanedPrivate *priv = paned->priv;
2159 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2161 priv->original_position = -1;
2162 gtk_paned_restore_focus (paned);
2172 gtk_paned_cancel_position (GtkPaned *paned)
2174 GtkPanedPrivate *priv = paned->priv;
2176 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2178 if (priv->original_position != -1)
2180 gtk_paned_set_position (paned, priv->original_position);
2181 priv->original_position = -1;
2184 gtk_paned_restore_focus (paned);
2192 gtk_paned_cycle_handle_focus (GtkPaned *paned,
2195 GtkPanedPrivate *priv = paned->priv;
2196 GtkPaned *next, *prev;
2198 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2200 GtkPaned *focus = NULL;
2202 if (!priv->first_paned)
2204 /* The first_pane has disappeared. As an ad-hoc solution,
2205 * we make the currently focused paned the first_paned. To the
2206 * user this will seem like the paned cycling has been reset.
2209 gtk_paned_set_first_paned (paned, paned);
2212 gtk_paned_find_neighbours (paned, &next, &prev);
2214 if (reversed && prev &&
2215 prev != paned && paned != priv->first_paned)
2219 else if (!reversed && next &&
2220 next != paned && next != priv->first_paned)
2226 gtk_paned_accept_position (paned);
2232 gtk_paned_set_saved_focus (focus, priv->saved_focus);
2233 gtk_paned_set_first_paned (focus, priv->first_paned);
2235 gtk_paned_set_saved_focus (paned, NULL);
2236 gtk_paned_set_first_paned (paned, NULL);
2238 gtk_widget_grab_focus (GTK_WIDGET (focus));
2240 if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
2242 priv->original_position = -1;
2243 focus->priv->original_position = gtk_paned_get_position (focus);
2248 GtkContainer *container = GTK_CONTAINER (paned);
2251 GtkPaned *prev, *next;
2252 GtkWidget *toplevel;
2253 GtkWidget *focus_child;
2255 gtk_paned_find_neighbours (paned, &next, &prev);
2256 focus_child = gtk_container_get_focus_child (container);
2258 if (focus_child == priv->child1)
2271 else if (focus_child == priv->child2)
2286 /* Focus is not inside this paned, and we don't have focus.
2287 * Presumably this happened because the application wants us
2288 * to start keyboard navigating.
2298 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2300 if (GTK_IS_WINDOW (toplevel))
2301 gtk_paned_set_saved_focus (focus, GTK_WINDOW (toplevel)->focus_widget);
2302 gtk_paned_set_first_paned (focus, first);
2303 focus->priv->original_position = gtk_paned_get_position (focus);
2305 gtk_widget_grab_focus (GTK_WIDGET (focus));
2312 gtk_paned_toggle_handle_focus (GtkPaned *paned)
2314 /* This function/signal has the wrong name. It is called when you
2315 * press Tab or Shift-Tab and what we do is act as if
2316 * the user pressed Return and then Tab or Shift-Tab
2318 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2319 gtk_paned_accept_position (paned);
2325 * gtk_paned_get_handle_window:
2326 * @paned: a #GtkPaned
2328 * Returns the #GdkWindow of the handle. This function is
2329 * useful when handling button or motion events because it
2330 * enables the callback to distinguish between the window
2331 * of the paned, a child and the handle.
2333 * Return value: the paned's handle window.
2338 gtk_paned_get_handle_window (GtkPaned *paned)
2340 g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2342 return paned->priv->handle;