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/.
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkbindings.h"
34 #include "gtkmarshalers.h"
35 #include "gtkorientable.h"
36 #include "gtksizerequest.h"
37 #include "gtkwindow.h"
39 #include "gtkprivate.h"
44 struct _GtkPanedPrivate
46 GtkOrientation orientation;
47 GtkPaned *first_paned;
50 GtkWidget *last_child1_focus;
51 GtkWidget *last_child2_focus;
52 GtkWidget *saved_focus;
54 GdkCursorType cursor_type;
55 GdkDevice *grab_device;
56 GdkRectangle handle_pos;
64 gint original_position;
66 guint handle_prelit : 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;
103 static void gtk_paned_set_property (GObject *object,
107 static void gtk_paned_get_property (GObject *object,
111 static void gtk_paned_set_child_property (GtkContainer *container,
116 static void gtk_paned_get_child_property (GtkContainer *container,
121 static void gtk_paned_finalize (GObject *object);
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,
151 static void gtk_paned_remove (GtkContainer *container,
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,
161 static void gtk_paned_set_focus_child (GtkContainer *container,
163 static void gtk_paned_set_saved_focus (GtkPaned *paned,
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,
169 static void gtk_paned_set_last_child2_focus (GtkPaned *paned,
171 static gboolean gtk_paned_cycle_child_focus (GtkPaned *paned,
173 static gboolean gtk_paned_cycle_handle_focus (GtkPaned *paned,
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);
181 static GType gtk_paned_child_type (GtkContainer *container);
182 static void gtk_paned_grab_notify (GtkWidget *widget,
183 gboolean was_grabbed);
186 G_DEFINE_TYPE_WITH_CODE (GtkPaned, gtk_paned, GTK_TYPE_CONTAINER,
187 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
190 static guint signals[LAST_SIGNAL] = { 0 };
194 add_tab_bindings (GtkBindingSet *binding_set,
195 GdkModifierType modifiers)
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);
204 add_move_binding (GtkBindingSet *binding_set,
206 GdkModifierType mask,
207 GtkScrollType scroll)
209 gtk_binding_entry_add_signal (binding_set, keyval, mask,
211 GTK_TYPE_SCROLL_TYPE, scroll);
215 gtk_paned_class_init (GtkPanedClass *class)
217 GObjectClass *object_class;
218 GtkWidgetClass *widget_class;
219 GtkContainerClass *container_class;
220 GtkPanedClass *paned_class;
221 GtkBindingSet *binding_set;
223 object_class = (GObjectClass *) class;
224 widget_class = (GtkWidgetClass *) class;
225 container_class = (GtkContainerClass *) class;
226 paned_class = (GtkPanedClass *) class;
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;
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;
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;
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;
264 g_object_class_override_property (object_class,
268 g_object_class_install_property (object_class,
270 g_param_spec_int ("position",
272 P_("Position of paned separator in pixels (0 means all the way to the left/top)"),
276 GTK_PARAM_READWRITE));
278 g_object_class_install_property (object_class,
280 g_param_spec_boolean ("position-set",
282 P_("TRUE if the Position property should be used"),
284 GTK_PARAM_READWRITE));
286 gtk_widget_class_install_style_property (widget_class,
287 g_param_spec_int ("handle-size",
289 P_("Width of handle"),
293 GTK_PARAM_READABLE));
295 * GtkPaned:min-position:
297 * The smallest possible value for the position property. This property is derived from the
298 * size and shrinkability of the widget's children.
302 g_object_class_install_property (object_class,
304 g_param_spec_int ("min-position",
305 P_("Minimal Position"),
306 P_("Smallest possible value for the \"position\" property"),
310 GTK_PARAM_READABLE));
313 * GtkPaned:max-position:
315 * The largest possible value for the position property. This property is derived from the
316 * size and shrinkability of the widget's children.
320 g_object_class_install_property (object_class,
322 g_param_spec_int ("max-position",
323 P_("Maximal Position"),
324 P_("Largest possible value for the \"position\" property"),
328 GTK_PARAM_READABLE));
333 * The "resize" child property determines whether the child expands and
334 * shrinks along with the paned widget.
338 gtk_container_class_install_child_property (container_class,
340 g_param_spec_boolean ("resize",
342 P_("If TRUE, the child expands and shrinks along with the paned widget"),
344 GTK_PARAM_READWRITE));
349 * The "shrink" child property determines whether the child can be made
350 * smaller than its requisition.
354 gtk_container_class_install_child_property (container_class,
356 g_param_spec_boolean ("shrink",
358 P_("If TRUE, the child can be made smaller than its requisition"),
360 GTK_PARAM_READWRITE));
363 * GtkPaned::cycle-child-focus:
364 * @widget: the object that received the signal
365 * @reversed: whether cycling backward or forward
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.
371 * The default binding is f6.
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),
381 _gtk_marshal_BOOLEAN__BOOLEAN,
386 * GtkPaned::toggle-handle-focus:
387 * @widget: the object that received the signal
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.
394 * The default binding is Tab.
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),
404 _gtk_marshal_BOOLEAN__VOID,
408 * GtkPaned::move-handle:
409 * @widget: the object that received the signal
410 * @scroll_type: a #GtkScrollType
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
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),
425 _gtk_marshal_BOOLEAN__ENUM,
427 GTK_TYPE_SCROLL_TYPE);
430 * GtkPaned::cycle-handle-focus:
431 * @widget: the object that received the signal
432 * @reversed: whether cycling backward or forward
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.
439 * The default binding for this signal is f8.
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),
449 _gtk_marshal_BOOLEAN__BOOLEAN,
454 * GtkPaned::accept-position:
455 * @widget: the object that received the signal
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.
462 * The default binding for this signal is Return or Space.
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),
472 _gtk_marshal_BOOLEAN__VOID,
476 * GtkPaned::cancel-position:
477 * @widget: the object that received the signal
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
485 * The default binding for this signal is Escape.
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),
495 _gtk_marshal_BOOLEAN__VOID,
498 binding_set = gtk_binding_set_by_class (class);
501 gtk_binding_entry_add_signal (binding_set,
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);
511 gtk_binding_entry_add_signal (binding_set,
513 "cycle-handle-focus", 1,
514 G_TYPE_BOOLEAN, FALSE);
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);
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);
526 /* accept and cancel positions */
527 gtk_binding_entry_add_signal (binding_set,
529 "cancel-position", 0);
531 gtk_binding_entry_add_signal (binding_set,
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,
539 "accept-position", 0);
540 gtk_binding_entry_add_signal (binding_set,
542 "accept-position", 0);
543 gtk_binding_entry_add_signal (binding_set,
545 "accept-position", 0);
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);
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);
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);
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);
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);
577 g_type_class_add_private (object_class, sizeof (GtkPanedPrivate));
581 gtk_paned_child_type (GtkContainer *container)
583 GtkPaned *paned = GTK_PANED (container);
584 GtkPanedPrivate *priv = paned->priv;
586 if (!priv->child1 || !priv->child2)
587 return GTK_TYPE_WIDGET;
593 gtk_paned_init (GtkPaned *paned)
595 GtkPanedPrivate *priv;
597 gtk_widget_set_has_window (GTK_WIDGET (paned), FALSE);
598 gtk_widget_set_can_focus (GTK_WIDGET (paned), TRUE);
600 /* We only need to redraw when the handle position moves, which is
601 * independent of the overall allocation of the GtkPaned
603 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (paned), FALSE);
605 paned->priv = G_TYPE_INSTANCE_GET_PRIVATE (paned, GTK_TYPE_PANED, GtkPanedPrivate);
608 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
609 priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
614 priv->cursor_type = GDK_CROSS;
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;
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;
628 priv->handle_pos.x = -1;
629 priv->handle_pos.y = -1;
635 gtk_paned_set_property (GObject *object,
640 GtkPaned *paned = GTK_PANED (object);
641 GtkPanedPrivate *priv = paned->priv;
645 case PROP_ORIENTATION:
646 priv->orientation = g_value_get_enum (value);
648 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
649 priv->cursor_type = GDK_SB_H_DOUBLE_ARROW;
651 priv->cursor_type = GDK_SB_V_DOUBLE_ARROW;
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));
658 gtk_paned_set_position (paned, g_value_get_int (value));
660 case PROP_POSITION_SET:
661 priv->position_set = g_value_get_boolean (value);
662 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
665 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
671 gtk_paned_get_property (GObject *object,
676 GtkPaned *paned = GTK_PANED (object);
677 GtkPanedPrivate *priv = paned->priv;
681 case PROP_ORIENTATION:
682 g_value_set_enum (value, priv->orientation);
685 g_value_set_int (value, priv->child1_size);
687 case PROP_POSITION_SET:
688 g_value_set_boolean (value, priv->position_set);
690 case PROP_MIN_POSITION:
691 g_value_set_int (value, priv->min_position);
693 case PROP_MAX_POSITION:
694 g_value_set_int (value, priv->max_position);
697 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
703 gtk_paned_set_child_property (GtkContainer *container,
709 GtkPaned *paned = GTK_PANED (container);
710 GtkPanedPrivate *priv = paned->priv;
711 gboolean old_value, new_value;
713 g_assert (child == priv->child1 || child == priv->child2);
715 new_value = g_value_get_boolean (value);
718 case CHILD_PROP_RESIZE:
719 if (child == priv->child1)
721 old_value = priv->child1_resize;
722 priv->child1_resize = new_value;
726 old_value = priv->child2_resize;
727 priv->child2_resize = new_value;
730 case CHILD_PROP_SHRINK:
731 if (child == priv->child1)
733 old_value = priv->child1_shrink;
734 priv->child1_shrink = new_value;
738 old_value = priv->child2_shrink;
739 priv->child2_shrink = new_value;
743 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
744 old_value = -1; /* quiet gcc */
747 if (old_value != new_value)
748 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
752 gtk_paned_get_child_property (GtkContainer *container,
758 GtkPaned *paned = GTK_PANED (container);
759 GtkPanedPrivate *priv = paned->priv;
761 g_assert (child == priv->child1 || child == priv->child2);
765 case CHILD_PROP_RESIZE:
766 if (child == priv->child1)
767 g_value_set_boolean (value, priv->child1_resize);
769 g_value_set_boolean (value, priv->child2_resize);
771 case CHILD_PROP_SHRINK:
772 if (child == priv->child1)
773 g_value_set_boolean (value, priv->child1_shrink);
775 g_value_set_boolean (value, priv->child2_shrink);
778 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
784 gtk_paned_finalize (GObject *object)
786 GtkPaned *paned = GTK_PANED (object);
788 gtk_paned_set_saved_focus (paned, NULL);
789 gtk_paned_set_first_paned (paned, NULL);
791 G_OBJECT_CLASS (gtk_paned_parent_class)->finalize (object);
795 gtk_paned_size_request (GtkWidget *widget,
796 GtkRequisition *requisition)
798 GtkPaned *paned = GTK_PANED (widget);
799 GtkPanedPrivate *priv = paned->priv;
800 GtkRequisition child_requisition;
803 requisition->width = 0;
804 requisition->height = 0;
806 if (priv->child1 && gtk_widget_get_visible (priv->child1))
808 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->child1),
809 &child_requisition, NULL);
811 requisition->height = child_requisition.height;
812 requisition->width = child_requisition.width;
815 if (priv->child2 && gtk_widget_get_visible (priv->child2))
817 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->child2),
818 &child_requisition, NULL);
820 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
822 requisition->height = MAX (requisition->height,
823 child_requisition.height);
824 requisition->width += child_requisition.width;
828 requisition->width = MAX (requisition->width,
829 child_requisition.width);
830 requisition->height += child_requisition.height;
834 border_width = gtk_container_get_border_width (GTK_CONTAINER (paned));
835 requisition->width += border_width * 2;
836 requisition->height += border_width * 2;
838 if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
839 priv->child2 && gtk_widget_get_visible (priv->child2))
843 gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
845 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
846 requisition->width += handle_size;
848 requisition->height += handle_size;
853 flip_child (GtkWidget *widget,
854 GtkAllocation *child_pos)
856 GtkAllocation allocation;
859 gtk_widget_get_allocation (widget, &allocation);
861 width = allocation.width;
863 child_pos->x = 2 * x + width - child_pos->x - child_pos->width;
867 gtk_paned_size_allocate (GtkWidget *widget,
868 GtkAllocation *allocation)
870 GtkPaned *paned = GTK_PANED (widget);
871 GtkPanedPrivate *priv = paned->priv;
872 GtkAllocation widget_allocation;
875 border_width = gtk_container_get_border_width (GTK_CONTAINER (paned));
877 gtk_widget_set_allocation (widget, allocation);
879 if (priv->child1 && gtk_widget_get_visible (priv->child1) &&
880 priv->child2 && gtk_widget_get_visible (priv->child2))
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;
890 gtk_widget_style_get (widget, "handle-size", &handle_size, NULL);
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);
897 old_handle_pos = priv->handle_pos;
899 gtk_widget_get_allocation (widget, &widget_allocation);
901 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
903 gtk_paned_calc_position (paned,
904 MAX (1, widget_allocation.width
907 child1_requisition.width,
908 child2_requisition.width);
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);
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;
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);
923 if (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL)
925 flip_child (widget, &(child2_allocation));
926 flip_child (widget, &(child1_allocation));
927 flip_child (widget, &(priv->handle_pos));
932 gtk_paned_calc_position (paned,
933 MAX (1, widget_allocation.height
936 child1_requisition.height,
937 child2_requisition.height);
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;
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;
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);
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))
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);
966 if (gtk_widget_get_realized (widget))
968 if (gtk_widget_get_mapped (widget))
969 gdk_window_show (priv->handle);
971 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
973 gdk_window_move_resize (priv->handle,
977 priv->handle_pos.height);
981 gdk_window_move_resize (priv->handle,
984 priv->handle_pos.width,
989 /* Now allocate the childen, making sure, when resizing not to
990 * overlap the windows
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) ||
997 (priv->orientation == GTK_ORIENTATION_VERTICAL &&
998 priv_child1_allocation.height < child1_allocation.height)))
1000 gtk_widget_size_allocate (priv->child2, &child2_allocation);
1001 gtk_widget_size_allocate (priv->child1, &child1_allocation);
1005 gtk_widget_size_allocate (priv->child1, &child1_allocation);
1006 gtk_widget_size_allocate (priv->child2, &child2_allocation);
1011 GtkAllocation child_allocation;
1013 if (gtk_widget_get_realized (widget))
1014 gdk_window_hide (priv->handle);
1017 gtk_widget_set_child_visible (priv->child1, TRUE);
1019 gtk_widget_set_child_visible (priv->child2, TRUE);
1021 gtk_widget_get_allocation (widget, &widget_allocation);
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);
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);
1036 gtk_paned_realize (GtkWidget *widget)
1038 GtkPaned *paned = GTK_PANED (widget);
1039 GtkPanedPrivate *priv = paned->priv;
1041 GdkWindowAttr attributes;
1042 gint attributes_mask;
1044 gtk_widget_set_realized (widget, TRUE);
1046 window = gtk_widget_get_parent_window (widget);
1047 gtk_widget_set_window (widget, window);
1048 g_object_ref (window);
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))
1066 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1068 attributes_mask |= GDK_WA_CURSOR;
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);
1077 gtk_widget_style_attach (widget);
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);
1085 gtk_paned_unrealize (GtkWidget *widget)
1087 GtkPaned *paned = GTK_PANED (widget);
1088 GtkPanedPrivate *priv = paned->priv;
1092 gdk_window_set_user_data (priv->handle, NULL);
1093 gdk_window_destroy (priv->handle);
1094 priv->handle = NULL;
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);
1102 GTK_WIDGET_CLASS (gtk_paned_parent_class)->unrealize (widget);
1106 gtk_paned_map (GtkWidget *widget)
1108 GtkPaned *paned = GTK_PANED (widget);
1109 GtkPanedPrivate *priv = paned->priv;
1111 gdk_window_show (priv->handle);
1113 GTK_WIDGET_CLASS (gtk_paned_parent_class)->map (widget);
1117 gtk_paned_unmap (GtkWidget *widget)
1119 GtkPaned *paned = GTK_PANED (widget);
1120 GtkPanedPrivate *priv = paned->priv;
1122 gdk_window_hide (priv->handle);
1124 GTK_WIDGET_CLASS (gtk_paned_parent_class)->unmap (widget);
1128 gtk_paned_expose (GtkWidget *widget,
1129 GdkEventExpose *event)
1131 GtkPaned *paned = GTK_PANED (widget);
1132 GtkPanedPrivate *priv = paned->priv;
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))
1140 if (gtk_widget_is_focus (widget))
1141 state = GTK_STATE_SELECTED;
1142 else if (priv->handle_prelit)
1143 state = GTK_STATE_PRELIGHT;
1145 state = gtk_widget_get_state (widget);
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);
1156 /* Chain up to draw children */
1157 GTK_WIDGET_CLASS (gtk_paned_parent_class)->expose_event (widget, event);
1163 is_rtl (GtkPaned *paned)
1165 GtkPanedPrivate *priv = paned->priv;
1167 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
1168 gtk_widget_get_direction (GTK_WIDGET (paned)) == GTK_TEXT_DIR_RTL)
1177 update_drag (GtkPaned *paned)
1179 GtkPanedPrivate *priv = paned->priv;
1180 GtkAllocation allocation;
1181 GtkWidget *widget = GTK_WIDGET (paned);
1186 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1187 gtk_widget_get_pointer (widget, &pos, NULL);
1189 gtk_widget_get_pointer (widget, NULL, &pos);
1191 pos -= priv->drag_pos;
1195 gtk_widget_style_get (widget,
1196 "handle-size", &handle_size,
1199 gtk_widget_get_allocation (widget, &allocation);
1200 size = allocation.width - pos - handle_size;
1207 size -= gtk_container_get_border_width (GTK_CONTAINER (paned));
1209 size = CLAMP (size, priv->min_position, priv->max_position);
1211 if (size != priv->child1_size)
1212 gtk_paned_set_position (paned, size);
1216 gtk_paned_enter (GtkWidget *widget,
1217 GdkEventCrossing *event)
1219 GtkPaned *paned = GTK_PANED (widget);
1220 GtkPanedPrivate *priv = paned->priv;
1223 update_drag (paned);
1226 priv->handle_prelit = TRUE;
1227 gtk_widget_queue_draw_area (widget,
1230 priv->handle_pos.width,
1231 priv->handle_pos.height);
1238 gtk_paned_leave (GtkWidget *widget,
1239 GdkEventCrossing *event)
1241 GtkPaned *paned = GTK_PANED (widget);
1242 GtkPanedPrivate *priv = paned->priv;
1245 update_drag (paned);
1248 priv->handle_prelit = FALSE;
1249 gtk_widget_queue_draw_area (widget,
1252 priv->handle_pos.width,
1253 priv->handle_pos.height);
1260 gtk_paned_focus (GtkWidget *widget,
1261 GtkDirectionType direction)
1266 /* This is a hack, but how can this be done without
1267 * excessive cut-and-paste from gtkcontainer.c?
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);
1278 gtk_paned_button_press (GtkWidget *widget,
1279 GdkEventButton *event)
1281 GtkPaned *paned = GTK_PANED (widget);
1282 GtkPanedPrivate *priv = paned->priv;
1284 if (!priv->in_drag &&
1285 (event->window == priv->handle) && (event->button == 1))
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,
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)
1300 priv->in_drag = TRUE;
1301 priv->grab_time = event->time;
1302 priv->grab_device = event->device;
1304 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1305 priv->drag_pos = event->x;
1307 priv->drag_pos = event->y;
1316 gtk_paned_grab_broken (GtkWidget *widget,
1317 GdkEventGrabBroken *event)
1319 GtkPaned *paned = GTK_PANED (widget);
1320 GtkPanedPrivate *priv = paned->priv;
1322 priv->in_drag = FALSE;
1323 priv->drag_pos = -1;
1324 priv->position_set = TRUE;
1330 stop_drag (GtkPaned *paned)
1332 GtkPanedPrivate *priv = paned->priv;
1334 priv->in_drag = FALSE;
1335 priv->drag_pos = -1;
1336 priv->position_set = TRUE;
1338 gdk_device_ungrab (priv->grab_device,
1340 priv->grab_device = NULL;
1344 gtk_paned_grab_notify (GtkWidget *widget,
1345 gboolean was_grabbed)
1347 GtkPaned *paned = GTK_PANED (widget);
1348 GtkPanedPrivate *priv = paned->priv;
1349 GdkDevice *grab_device;
1351 grab_device = priv->grab_device;
1353 if (priv->in_drag && grab_device &&
1354 gtk_widget_device_is_shadowed (widget, grab_device))
1359 gtk_paned_state_changed (GtkWidget *widget,
1360 GtkStateType previous_state)
1362 GtkPaned *paned = GTK_PANED (widget);
1363 GtkPanedPrivate *priv = paned->priv;
1366 if (gtk_widget_get_realized (widget))
1368 if (gtk_widget_is_sensitive (widget))
1369 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
1374 gdk_window_set_cursor (priv->handle, cursor);
1377 gdk_cursor_unref (cursor);
1382 gtk_paned_button_release (GtkWidget *widget,
1383 GdkEventButton *event)
1385 GtkPaned *paned = GTK_PANED (widget);
1386 GtkPanedPrivate *priv = paned->priv;
1388 if (priv->in_drag && (event->button == 1))
1399 gtk_paned_motion (GtkWidget *widget,
1400 GdkEventMotion *event)
1402 GtkPaned *paned = GTK_PANED (widget);
1403 GtkPanedPrivate *priv = paned->priv;
1407 update_drag (paned);
1416 * @orientation: the paned's orientation.
1418 * Creates a new #GtkPaned widget.
1420 * Return value: a new #GtkPaned.
1425 gtk_paned_new (GtkOrientation orientation)
1427 return g_object_new (GTK_TYPE_PANED,
1428 "orientation", orientation,
1433 gtk_paned_add1 (GtkPaned *paned,
1436 gtk_paned_pack1 (paned, widget, FALSE, TRUE);
1440 gtk_paned_add2 (GtkPaned *paned,
1443 gtk_paned_pack2 (paned, widget, TRUE, TRUE);
1447 gtk_paned_pack1 (GtkPaned *paned,
1452 GtkPanedPrivate *priv;
1454 g_return_if_fail (GTK_IS_PANED (paned));
1455 g_return_if_fail (GTK_IS_WIDGET (child));
1461 priv->child1 = child;
1462 priv->child1_resize = resize;
1463 priv->child1_shrink = shrink;
1465 gtk_widget_set_parent (child, GTK_WIDGET (paned));
1470 gtk_paned_pack2 (GtkPaned *paned,
1475 GtkPanedPrivate *priv;
1477 g_return_if_fail (GTK_IS_PANED (paned));
1478 g_return_if_fail (GTK_IS_WIDGET (child));
1484 priv->child2 = child;
1485 priv->child2_resize = resize;
1486 priv->child2_shrink = shrink;
1488 gtk_widget_set_parent (child, GTK_WIDGET (paned));
1494 gtk_paned_add (GtkContainer *container,
1497 GtkPanedPrivate *priv;
1500 g_return_if_fail (GTK_IS_PANED (container));
1502 paned = GTK_PANED (container);
1506 gtk_paned_add1 (paned, widget);
1507 else if (!priv->child2)
1508 gtk_paned_add2 (paned, widget);
1510 g_warning ("GtkPaned cannot have more than 2 children\n");
1514 gtk_paned_remove (GtkContainer *container,
1517 GtkPaned *paned = GTK_PANED (container);
1518 GtkPanedPrivate *priv = paned->priv;
1519 gboolean was_visible;
1521 was_visible = gtk_widget_get_visible (widget);
1523 if (priv->child1 == widget)
1525 gtk_widget_unparent (widget);
1527 priv->child1 = NULL;
1529 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1530 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1532 else if (priv->child2 == widget)
1534 gtk_widget_unparent (widget);
1536 priv->child2 = NULL;
1538 if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
1539 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (container));
1544 gtk_paned_forall (GtkContainer *container,
1545 gboolean include_internals,
1546 GtkCallback callback,
1547 gpointer callback_data)
1549 GtkPanedPrivate *priv;
1552 g_return_if_fail (callback != NULL);
1554 paned = GTK_PANED (container);
1558 (*callback) (priv->child1, callback_data);
1560 (*callback) (priv->child2, callback_data);
1564 * gtk_paned_get_position:
1565 * @paned: a #GtkPaned widget
1567 * Obtains the position of the divider between the two panes.
1569 * Return value: position of the divider
1572 gtk_paned_get_position (GtkPaned *paned)
1574 g_return_val_if_fail (GTK_IS_PANED (paned), 0);
1576 return paned->priv->child1_size;
1580 * gtk_paned_set_position:
1581 * @paned: a #GtkPaned widget
1582 * @position: pixel position of divider, a negative value means that the position
1585 * Sets the position of the divider between the two panes.
1588 gtk_paned_set_position (GtkPaned *paned,
1591 GtkPanedPrivate *priv;
1594 g_return_if_fail (GTK_IS_PANED (paned));
1598 if (priv->child1_size == position)
1601 object = G_OBJECT (paned);
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()
1612 priv->child1_size = position;
1613 priv->position_set = TRUE;
1617 priv->position_set = FALSE;
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);
1625 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (paned));
1628 /* Hacky work-around for bug #144269 */
1629 if (priv->child2 != NULL)
1631 gtk_widget_queue_draw (priv->child2);
1637 * gtk_paned_get_child1:
1638 * @paned: a #GtkPaned widget
1640 * Obtains the first child of the paned widget.
1642 * Return value: (transfer none): first child, or %NULL if it is not set.
1647 gtk_paned_get_child1 (GtkPaned *paned)
1649 g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1651 return paned->priv->child1;
1655 * gtk_paned_get_child2:
1656 * @paned: a #GtkPaned widget
1658 * Obtains the second child of the paned widget.
1660 * Return value: (transfer none): second child, or %NULL if it is not set.
1665 gtk_paned_get_child2 (GtkPaned *paned)
1667 g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
1669 return paned->priv->child2;
1673 gtk_paned_calc_position (GtkPaned *paned,
1678 GtkPanedPrivate *priv = paned->priv;
1680 gint old_min_position;
1681 gint old_max_position;
1683 old_position = priv->child1_size;
1684 old_min_position = priv->min_position;
1685 old_max_position = priv->max_position;
1687 priv->min_position = priv->child1_shrink ? 0 : child1_req;
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);
1694 if (!priv->position_set)
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;
1703 priv->child1_size = allocation * 0.5 + 0.5;
1707 /* If the position was set before the initial allocation.
1708 * (priv->last_allocation <= 0) just clamp it and leave it.
1710 if (priv->last_allocation > 0)
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;
1719 priv->child1_size = CLAMP (priv->child1_size,
1721 priv->max_position);
1724 gtk_widget_set_child_visible (priv->child1, priv->child1_size != 0);
1727 gtk_widget_set_child_visible (priv->child2, priv->child1_size != allocation);
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));
1738 priv->last_allocation = allocation;
1742 gtk_paned_set_saved_focus (GtkPaned *paned, GtkWidget *widget)
1744 GtkPanedPrivate *priv = paned->priv;
1746 if (priv->saved_focus)
1747 g_object_remove_weak_pointer (G_OBJECT (priv->saved_focus),
1748 (gpointer *)&(priv->saved_focus));
1750 priv->saved_focus = widget;
1752 if (priv->saved_focus)
1753 g_object_add_weak_pointer (G_OBJECT (priv->saved_focus),
1754 (gpointer *)&(priv->saved_focus));
1758 gtk_paned_set_first_paned (GtkPaned *paned, GtkPaned *first_paned)
1760 GtkPanedPrivate *priv = paned->priv;
1762 if (priv->first_paned)
1763 g_object_remove_weak_pointer (G_OBJECT (priv->first_paned),
1764 (gpointer *)&(priv->first_paned));
1766 priv->first_paned = first_paned;
1768 if (priv->first_paned)
1769 g_object_add_weak_pointer (G_OBJECT (priv->first_paned),
1770 (gpointer *)&(priv->first_paned));
1774 gtk_paned_set_last_child1_focus (GtkPaned *paned, GtkWidget *widget)
1776 GtkPanedPrivate *priv = paned->priv;
1778 if (priv->last_child1_focus)
1779 g_object_remove_weak_pointer (G_OBJECT (priv->last_child1_focus),
1780 (gpointer *)&(priv->last_child1_focus));
1782 priv->last_child1_focus = widget;
1784 if (priv->last_child1_focus)
1785 g_object_add_weak_pointer (G_OBJECT (priv->last_child1_focus),
1786 (gpointer *)&(priv->last_child1_focus));
1790 gtk_paned_set_last_child2_focus (GtkPaned *paned, GtkWidget *widget)
1792 GtkPanedPrivate *priv = paned->priv;
1794 if (priv->last_child2_focus)
1795 g_object_remove_weak_pointer (G_OBJECT (priv->last_child2_focus),
1796 (gpointer *)&(priv->last_child2_focus));
1798 priv->last_child2_focus = widget;
1800 if (priv->last_child2_focus)
1801 g_object_add_weak_pointer (G_OBJECT (priv->last_child2_focus),
1802 (gpointer *)&(priv->last_child2_focus));
1806 paned_get_focus_widget (GtkPaned *paned)
1808 GtkWidget *toplevel;
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));
1818 gtk_paned_set_focus_child (GtkContainer *container,
1819 GtkWidget *focus_child)
1822 GtkPanedPrivate *priv;
1823 GtkWidget *container_focus_child;
1825 g_return_if_fail (GTK_IS_PANED (container));
1827 paned = GTK_PANED (container);
1830 if (focus_child == NULL)
1832 GtkWidget *last_focus;
1835 last_focus = paned_get_focus_widget (paned);
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
1842 for (w = last_focus; w != GTK_WIDGET (paned); w = gtk_widget_get_parent (w))
1843 if (GTK_IS_PANED (w))
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);
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);
1859 gtk_paned_get_cycle_chain (GtkPaned *paned,
1860 GtkDirectionType direction,
1863 GtkPanedPrivate *priv = paned->priv;
1864 GtkContainer *container = GTK_CONTAINER (paned);
1865 GtkWidget *ancestor = NULL;
1866 GtkWidget *focus_child;
1868 GtkWidget *widget = GTK_WIDGET (paned);
1869 GList *temp_list = NULL;
1872 if (priv->in_recursion)
1875 g_assert (widgets != NULL);
1877 if (priv->last_child1_focus &&
1878 !gtk_widget_is_ancestor (priv->last_child1_focus, widget))
1880 gtk_paned_set_last_child1_focus (paned, NULL);
1883 if (priv->last_child2_focus &&
1884 !gtk_widget_is_ancestor (priv->last_child2_focus, widget))
1886 gtk_paned_set_last_child2_focus (paned, NULL);
1889 parent = gtk_widget_get_parent (widget);
1891 ancestor = gtk_widget_get_ancestor (parent, GTK_TYPE_PANED);
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.
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.
1901 focus_child = gtk_container_get_focus_child (container);
1902 if (direction == GTK_DIR_TAB_FORWARD)
1904 if (focus_child == priv->child1)
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);
1910 else if (focus_child == priv->child2)
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);
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);
1927 if (focus_child == priv->child1)
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);
1933 else if (focus_child == priv->child2)
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);
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);
1949 /* Walk the list and expand all the paned widgets. */
1950 for (list = temp_list; list != NULL; list = list->next)
1952 GtkWidget *widget = list->data;
1956 if (GTK_IS_PANED (widget))
1958 priv->in_recursion = TRUE;
1959 gtk_paned_get_cycle_chain (GTK_PANED (widget), direction, widgets);
1960 priv->in_recursion = FALSE;
1964 *widgets = g_list_append (*widgets, widget);
1969 g_list_free (temp_list);
1973 gtk_paned_cycle_child_focus (GtkPaned *paned,
1976 GList *cycle_chain = NULL;
1979 GtkDirectionType direction = reversed? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
1981 /* ignore f6 if the handle is focused */
1982 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
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
1989 gtk_paned_get_cycle_chain (paned, direction, &cycle_chain);
1991 for (list = cycle_chain; list != NULL; list = list->next)
1992 if (gtk_widget_child_focus (GTK_WIDGET (list->data), direction))
1995 g_list_free (cycle_chain);
2001 get_child_panes (GtkWidget *widget,
2004 if (!widget || !gtk_widget_get_realized (widget))
2007 if (GTK_IS_PANED (widget))
2009 GtkPaned *paned = GTK_PANED (widget);
2010 GtkPanedPrivate *priv = paned->priv;
2012 get_child_panes (priv->child1, panes);
2013 *panes = g_list_prepend (*panes, widget);
2014 get_child_panes (priv->child2, panes);
2016 else if (GTK_IS_CONTAINER (widget))
2018 gtk_container_forall (GTK_CONTAINER (widget),
2019 (GtkCallback)get_child_panes, panes);
2024 get_all_panes (GtkPaned *paned)
2026 GtkPaned *topmost = NULL;
2027 GList *result = NULL;
2030 for (w = GTK_WIDGET (paned); w != NULL; w = gtk_widget_get_parent (w))
2032 if (GTK_IS_PANED (w))
2033 topmost = GTK_PANED (w);
2038 get_child_panes (GTK_WIDGET (topmost), &result);
2040 return g_list_reverse (result);
2044 gtk_paned_find_neighbours (GtkPaned *paned,
2051 all_panes = get_all_panes (paned);
2052 g_assert (all_panes);
2054 this_link = g_list_find (all_panes, paned);
2056 g_assert (this_link);
2058 if (this_link->next)
2059 *next = this_link->next->data;
2061 *next = all_panes->data;
2063 if (this_link->prev)
2064 *prev = this_link->prev->data;
2066 *prev = g_list_last (all_panes)->data;
2068 g_list_free (all_panes);
2072 gtk_paned_move_handle (GtkPaned *paned,
2073 GtkScrollType scroll)
2075 GtkPanedPrivate *priv = paned->priv;
2077 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2084 SINGLE_STEP_SIZE = 1,
2088 new_position = old_position = gtk_paned_get_position (paned);
2093 case GTK_SCROLL_STEP_LEFT:
2094 case GTK_SCROLL_STEP_UP:
2095 case GTK_SCROLL_STEP_BACKWARD:
2096 increment = - SINGLE_STEP_SIZE;
2099 case GTK_SCROLL_STEP_RIGHT:
2100 case GTK_SCROLL_STEP_DOWN:
2101 case GTK_SCROLL_STEP_FORWARD:
2102 increment = SINGLE_STEP_SIZE;
2105 case GTK_SCROLL_PAGE_LEFT:
2106 case GTK_SCROLL_PAGE_UP:
2107 case GTK_SCROLL_PAGE_BACKWARD:
2108 increment = - PAGE_STEP_SIZE;
2111 case GTK_SCROLL_PAGE_RIGHT:
2112 case GTK_SCROLL_PAGE_DOWN:
2113 case GTK_SCROLL_PAGE_FORWARD:
2114 increment = PAGE_STEP_SIZE;
2117 case GTK_SCROLL_START:
2118 new_position = priv->min_position;
2121 case GTK_SCROLL_END:
2122 new_position = priv->max_position;
2132 increment = -increment;
2134 new_position = old_position + increment;
2137 new_position = CLAMP (new_position, priv->min_position, priv->max_position);
2139 if (old_position != new_position)
2140 gtk_paned_set_position (paned, new_position);
2149 gtk_paned_restore_focus (GtkPaned *paned)
2151 GtkPanedPrivate *priv = paned->priv;
2153 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2155 if (priv->saved_focus &&
2156 gtk_widget_get_sensitive (priv->saved_focus))
2158 gtk_widget_grab_focus (priv->saved_focus);
2162 /* the saved focus is somehow not available for focusing,
2164 * 1) tabbing into the paned widget
2165 * if that didn't work,
2166 * 2) unset focus for the window if there is one
2169 if (!gtk_widget_child_focus (GTK_WIDGET (paned), GTK_DIR_TAB_FORWARD))
2171 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
2173 if (GTK_IS_WINDOW (toplevel))
2174 gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
2178 gtk_paned_set_saved_focus (paned, NULL);
2179 gtk_paned_set_first_paned (paned, NULL);
2184 gtk_paned_accept_position (GtkPaned *paned)
2186 GtkPanedPrivate *priv = paned->priv;
2188 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2190 priv->original_position = -1;
2191 gtk_paned_restore_focus (paned);
2201 gtk_paned_cancel_position (GtkPaned *paned)
2203 GtkPanedPrivate *priv = paned->priv;
2205 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2207 if (priv->original_position != -1)
2209 gtk_paned_set_position (paned, priv->original_position);
2210 priv->original_position = -1;
2213 gtk_paned_restore_focus (paned);
2221 gtk_paned_cycle_handle_focus (GtkPaned *paned,
2224 GtkPanedPrivate *priv = paned->priv;
2225 GtkPaned *next, *prev;
2227 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2229 GtkPaned *focus = NULL;
2231 if (!priv->first_paned)
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.
2238 gtk_paned_set_first_paned (paned, paned);
2241 gtk_paned_find_neighbours (paned, &next, &prev);
2243 if (reversed && prev &&
2244 prev != paned && paned != priv->first_paned)
2248 else if (!reversed && next &&
2249 next != paned && next != priv->first_paned)
2255 gtk_paned_accept_position (paned);
2261 gtk_paned_set_saved_focus (focus, priv->saved_focus);
2262 gtk_paned_set_first_paned (focus, priv->first_paned);
2264 gtk_paned_set_saved_focus (paned, NULL);
2265 gtk_paned_set_first_paned (paned, NULL);
2267 gtk_widget_grab_focus (GTK_WIDGET (focus));
2269 if (!gtk_widget_is_focus (GTK_WIDGET (paned)))
2271 priv->original_position = -1;
2272 focus->priv->original_position = gtk_paned_get_position (focus);
2277 GtkContainer *container = GTK_CONTAINER (paned);
2280 GtkPaned *prev, *next;
2281 GtkWidget *toplevel;
2282 GtkWidget *focus_child;
2284 gtk_paned_find_neighbours (paned, &next, &prev);
2285 focus_child = gtk_container_get_focus_child (container);
2287 if (focus_child == priv->child1)
2300 else if (focus_child == priv->child2)
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.
2327 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (paned));
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);
2334 gtk_widget_grab_focus (GTK_WIDGET (focus));
2341 gtk_paned_toggle_handle_focus (GtkPaned *paned)
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
2347 if (gtk_widget_is_focus (GTK_WIDGET (paned)))
2348 gtk_paned_accept_position (paned);
2354 * gtk_paned_get_handle_window:
2355 * @paned: a #GtkPaned
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.
2362 * Return value: (transfer none): the paned's handle window.
2367 gtk_paned_get_handle_window (GtkPaned *paned)
2369 g_return_val_if_fail (GTK_IS_PANED (paned), NULL);
2371 return paned->priv->handle;