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/.
36 static void gtk_paned_class_init (GtkPanedClass *klass);
37 static void gtk_paned_init (GtkPaned *paned);
38 static void gtk_paned_set_property (GObject *object,
42 static void gtk_paned_get_property (GObject *object,
46 static void gtk_paned_realize (GtkWidget *widget);
47 static void gtk_paned_unrealize (GtkWidget *widget);
48 static void gtk_paned_map (GtkWidget *widget);
49 static void gtk_paned_unmap (GtkWidget *widget);
50 static gint gtk_paned_expose (GtkWidget *widget,
51 GdkEventExpose *event);
52 static void gtk_paned_add (GtkContainer *container,
54 static void gtk_paned_remove (GtkContainer *container,
56 static void gtk_paned_forall (GtkContainer *container,
57 gboolean include_internals,
59 gpointer callback_data);
60 static GtkType gtk_paned_child_type (GtkContainer *container);
62 static GtkContainerClass *parent_class = NULL;
66 gtk_paned_get_type (void)
68 static GtkType paned_type = 0;
72 static const GtkTypeInfo paned_info =
76 sizeof (GtkPanedClass),
77 (GtkClassInitFunc) gtk_paned_class_init,
78 (GtkObjectInitFunc) gtk_paned_init,
79 /* reserved_1 */ NULL,
80 /* reserved_2 */ NULL,
81 (GtkClassInitFunc) NULL,
84 paned_type = gtk_type_unique (GTK_TYPE_CONTAINER, &paned_info);
91 gtk_paned_class_init (GtkPanedClass *class)
93 GObjectClass *object_class;
94 GtkWidgetClass *widget_class;
95 GtkContainerClass *container_class;
97 object_class = (GObjectClass *) class;
98 widget_class = (GtkWidgetClass *) class;
99 container_class = (GtkContainerClass *) class;
101 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
103 object_class->set_property = gtk_paned_set_property;
104 object_class->get_property = gtk_paned_get_property;
106 widget_class->realize = gtk_paned_realize;
107 widget_class->unrealize = gtk_paned_unrealize;
108 widget_class->map = gtk_paned_map;
109 widget_class->unmap = gtk_paned_unmap;
110 widget_class->expose_event = gtk_paned_expose;
112 container_class->add = gtk_paned_add;
113 container_class->remove = gtk_paned_remove;
114 container_class->forall = gtk_paned_forall;
115 container_class->child_type = gtk_paned_child_type;
117 g_object_class_install_property (object_class,
119 g_param_spec_int ("position",
121 _("Position of paned separator in pixels (0 means all the way to the left/top)"),
125 G_PARAM_READABLE | G_PARAM_WRITABLE));
126 g_object_class_install_property (object_class,
128 g_param_spec_boolean ("position_set",
130 _("TRUE if the Position property should be used"),
132 G_PARAM_READABLE | G_PARAM_WRITABLE));
134 gtk_widget_class_install_style_property (widget_class,
135 g_param_spec_int ("handle_size",
137 _("Width of handle"),
145 gtk_paned_child_type (GtkContainer *container)
147 if (!GTK_PANED (container)->child1 || !GTK_PANED (container)->child2)
148 return GTK_TYPE_WIDGET;
150 return GTK_TYPE_NONE;
154 gtk_paned_init (GtkPaned *paned)
156 GTK_WIDGET_SET_FLAGS (paned, GTK_NO_WINDOW);
158 paned->child1 = NULL;
159 paned->child2 = NULL;
160 paned->handle = NULL;
161 paned->xor_gc = NULL;
162 paned->cursor_type = GDK_CROSS;
164 paned->handle_pos.width = 5;
165 paned->handle_pos.height = 5;
166 paned->position_set = FALSE;
167 paned->last_allocation = -1;
168 paned->in_drag = FALSE;
170 paned->handle_pos.x = -1;
171 paned->handle_pos.y = -1;
175 gtk_paned_set_property (GObject *object,
180 GtkPaned *paned = GTK_PANED (object);
185 gtk_paned_set_position (paned, g_value_get_int (value));
187 case PROP_POSITION_SET:
188 paned->position_set = g_value_get_boolean (value);
189 gtk_widget_queue_resize (GTK_WIDGET (paned));
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198 gtk_paned_get_property (GObject *object,
203 GtkPaned *paned = GTK_PANED (object);
208 g_value_set_int (value, paned->child1_size);
210 case PROP_POSITION_SET:
211 g_value_set_boolean (value, paned->position_set);
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 gtk_paned_realize (GtkWidget *widget)
223 GdkWindowAttr attributes;
224 gint attributes_mask;
226 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
227 paned = GTK_PANED (widget);
229 widget->window = gtk_widget_get_parent_window (widget);
230 gdk_window_ref (widget->window);
232 attributes.window_type = GDK_WINDOW_CHILD;
233 attributes.wclass = GDK_INPUT_ONLY;
234 attributes.x = paned->handle_pos.x;
235 attributes.y = paned->handle_pos.y;
236 attributes.width = paned->handle_pos.width;
237 attributes.height = paned->handle_pos.height;
238 attributes.cursor = gdk_cursor_new (paned->cursor_type);
239 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
240 GDK_BUTTON_RELEASE_MASK |
241 GDK_POINTER_MOTION_MASK |
242 GDK_POINTER_MOTION_HINT_MASK);
243 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_CURSOR;
245 paned->handle = gdk_window_new (widget->window,
246 &attributes, attributes_mask);
247 gdk_window_set_user_data (paned->handle, paned);
248 gdk_cursor_destroy (attributes.cursor);
250 widget->style = gtk_style_attach (widget->style, widget->window);
252 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
253 paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
254 gdk_window_show (paned->handle);
258 gtk_paned_unrealize (GtkWidget *widget)
260 GtkPaned *paned = GTK_PANED (widget);
264 gdk_gc_destroy (paned->xor_gc);
265 paned->xor_gc = NULL;
270 gdk_window_set_user_data (paned->handle, NULL);
271 gdk_window_destroy (paned->handle);
272 paned->handle = NULL;
275 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
276 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
280 gtk_paned_map (GtkWidget *widget)
282 GtkPaned *paned = GTK_PANED (widget);
284 gdk_window_show (paned->handle);
286 GTK_WIDGET_CLASS (parent_class)->map (widget);
290 gtk_paned_unmap (GtkWidget *widget)
292 GtkPaned *paned = GTK_PANED (widget);
294 gdk_window_hide (paned->handle);
296 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
300 gtk_paned_expose (GtkWidget *widget,
301 GdkEventExpose *event)
303 GtkPaned *paned = GTK_PANED (widget);
305 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
306 paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
307 paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
311 region = gdk_region_rectangle (&paned->handle_pos);
312 gdk_region_intersect (region, event->region);
314 if (!gdk_region_empty (region))
318 gdk_region_get_clipbox (region, &clip);
320 gtk_paint_handle (widget->style, widget->window,
321 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
322 &clip, widget, "paned",
323 paned->handle_pos.x, paned->handle_pos.y,
324 paned->handle_pos.width, paned->handle_pos.height,
328 gdk_region_destroy (region);
331 /* Chain up to draw children */
332 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
338 gtk_paned_add1 (GtkPaned *paned,
341 gtk_paned_pack1 (paned, widget, FALSE, TRUE);
345 gtk_paned_add2 (GtkPaned *paned,
348 gtk_paned_pack2 (paned, widget, TRUE, TRUE);
352 gtk_paned_pack1 (GtkPaned *paned,
357 g_return_if_fail (GTK_IS_PANED (paned));
358 g_return_if_fail (GTK_IS_WIDGET (child));
362 paned->child1 = child;
363 paned->child1_resize = resize;
364 paned->child1_shrink = shrink;
366 gtk_widget_set_parent (child, GTK_WIDGET (paned));
371 gtk_paned_pack2 (GtkPaned *paned,
376 g_return_if_fail (GTK_IS_PANED (paned));
377 g_return_if_fail (GTK_IS_WIDGET (child));
381 paned->child2 = child;
382 paned->child2_resize = resize;
383 paned->child2_shrink = shrink;
385 gtk_widget_set_parent (child, GTK_WIDGET (paned));
391 gtk_paned_add (GtkContainer *container,
396 g_return_if_fail (GTK_IS_PANED (container));
398 paned = GTK_PANED (container);
401 gtk_paned_add1 (paned, widget);
402 else if (!paned->child2)
403 gtk_paned_add2 (paned, widget);
407 gtk_paned_remove (GtkContainer *container,
411 gboolean was_visible;
413 paned = GTK_PANED (container);
414 was_visible = GTK_WIDGET_VISIBLE (widget);
416 if (paned->child1 == widget)
418 gtk_widget_unparent (widget);
420 paned->child1 = NULL;
422 if (was_visible && GTK_WIDGET_VISIBLE (container))
423 gtk_widget_queue_resize (GTK_WIDGET (container));
425 else if (paned->child2 == widget)
427 gtk_widget_unparent (widget);
429 paned->child2 = NULL;
431 if (was_visible && GTK_WIDGET_VISIBLE (container))
432 gtk_widget_queue_resize (GTK_WIDGET (container));
437 gtk_paned_forall (GtkContainer *container,
438 gboolean include_internals,
439 GtkCallback callback,
440 gpointer callback_data)
444 g_return_if_fail (callback != NULL);
446 paned = GTK_PANED (container);
449 (*callback) (paned->child1, callback_data);
451 (*callback) (paned->child2, callback_data);
455 * gtk_paned_get_position:
456 * @paned: a #GtkPaned widget
458 * Obtains the position of the divider between the two panes.
460 * Return value: position of the divider
463 gtk_paned_get_position (GtkPaned *paned)
465 g_return_val_if_fail (GTK_IS_PANED (paned), 0);
467 return paned->child1_size;
471 * gtk_paned_set_position:
472 * @paned: a #GtkPaned widget
473 * @position: pixel position of divider, a negative value means that the position
476 * Sets the position of the divider between the two panes.
479 gtk_paned_set_position (GtkPaned *paned,
484 g_return_if_fail (GTK_IS_PANED (paned));
486 object = G_OBJECT (paned);
490 /* We don't clamp here - the assumption is that
491 * if the total allocation changes at the same time
492 * as the position, the position set is with reference
493 * to the new total size. If only the position changes,
494 * then clamping will occur in gtk_paned_compute_position()
497 paned->child1_size = position;
498 paned->position_set = TRUE;
502 paned->position_set = FALSE;
505 g_object_freeze_notify (object);
506 g_object_notify (object, "position");
507 g_object_notify (object, "position_set");
508 g_object_thaw_notify (object);
510 gtk_widget_queue_resize (GTK_WIDGET (paned));
514 gtk_paned_compute_position (GtkPaned *paned,
521 g_return_if_fail (GTK_IS_PANED (paned));
523 old_position = paned->child1_size;
525 paned->min_position = paned->child1_shrink ? 0 : child1_req;
527 paned->max_position = allocation;
528 if (!paned->child2_shrink)
529 paned->max_position = MAX (1, paned->max_position - child2_req);
531 if (!paned->position_set)
533 if (paned->child1_resize && !paned->child2_resize)
534 paned->child1_size = MAX (1, allocation - child2_req);
535 else if (!paned->child1_resize && paned->child2_resize)
536 paned->child1_size = child1_req;
537 else if (child1_req + child2_req != 0)
538 paned->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req));
540 paned->child1_size = allocation * 0.5;
544 /* If the position was set before the initial allocation.
545 * (paned->last_allocation <= 0) just clamp it and leave it.
547 if (paned->last_allocation > 0)
549 if (paned->child1_resize && !paned->child2_resize)
550 paned->child1_size += allocation - paned->last_allocation;
551 else if (!(!paned->child1_resize && paned->child2_resize))
552 paned->child1_size = allocation * ((gdouble) paned->child1_size / (paned->last_allocation));
556 paned->child1_size = CLAMP (paned->child1_size,
558 paned->max_position);
560 if (paned->child1_size != old_position)
561 g_object_notify (G_OBJECT (paned), "position");
563 paned->last_allocation = allocation;