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 g_return_if_fail (GTK_IS_PANED (widget));
228 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
229 paned = GTK_PANED (widget);
231 widget->window = gtk_widget_get_parent_window (widget);
232 gdk_window_ref (widget->window);
234 attributes.window_type = GDK_WINDOW_CHILD;
235 attributes.wclass = GDK_INPUT_ONLY;
236 attributes.x = paned->handle_pos.x;
237 attributes.y = paned->handle_pos.y;
238 attributes.width = paned->handle_pos.width;
239 attributes.height = paned->handle_pos.height;
240 attributes.cursor = gdk_cursor_new (paned->cursor_type);
241 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
242 GDK_BUTTON_RELEASE_MASK |
243 GDK_POINTER_MOTION_MASK |
244 GDK_POINTER_MOTION_HINT_MASK);
245 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_CURSOR;
247 paned->handle = gdk_window_new (widget->window,
248 &attributes, attributes_mask);
249 gdk_window_set_user_data (paned->handle, paned);
250 gdk_cursor_destroy (attributes.cursor);
252 widget->style = gtk_style_attach (widget->style, widget->window);
254 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
255 paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
256 gdk_window_show (paned->handle);
260 gtk_paned_unrealize (GtkWidget *widget)
264 g_return_if_fail (GTK_IS_PANED (widget));
266 paned = GTK_PANED (widget);
270 gdk_gc_destroy (paned->xor_gc);
271 paned->xor_gc = NULL;
276 gdk_window_set_user_data (paned->handle, NULL);
277 gdk_window_destroy (paned->handle);
278 paned->handle = NULL;
281 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
282 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
286 gtk_paned_map (GtkWidget *widget)
288 GtkPaned *paned = GTK_PANED (widget);
290 g_return_if_fail (GTK_IS_PANED (widget));
292 gdk_window_show (paned->handle);
294 GTK_WIDGET_CLASS (parent_class)->map (widget);
298 gtk_paned_unmap (GtkWidget *widget)
300 GtkPaned *paned = GTK_PANED (widget);
302 g_return_if_fail (GTK_IS_PANED (widget));
304 gdk_window_hide (paned->handle);
306 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
310 gtk_paned_expose (GtkWidget *widget,
311 GdkEventExpose *event)
313 GtkPaned *paned = GTK_PANED (widget);
315 g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
317 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
318 paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
319 paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
323 region = gdk_region_rectangle (&paned->handle_pos);
324 gdk_region_intersect (region, event->region);
326 if (!gdk_region_empty (region))
330 gdk_region_get_clipbox (region, &clip);
332 gtk_paint_handle (widget->style, widget->window,
333 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
334 &clip, widget, "paned",
335 paned->handle_pos.x, paned->handle_pos.y,
336 paned->handle_pos.width, paned->handle_pos.height,
340 gdk_region_destroy (region);
343 /* Chain up to draw children */
344 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
350 gtk_paned_add1 (GtkPaned *paned,
353 gtk_paned_pack1 (paned, widget, FALSE, TRUE);
357 gtk_paned_add2 (GtkPaned *paned,
360 gtk_paned_pack2 (paned, widget, TRUE, TRUE);
364 gtk_paned_pack1 (GtkPaned *paned,
369 g_return_if_fail (GTK_IS_PANED (paned));
370 g_return_if_fail (GTK_IS_WIDGET (child));
374 paned->child1 = child;
375 paned->child1_resize = resize;
376 paned->child1_shrink = shrink;
378 gtk_widget_set_parent (child, GTK_WIDGET (paned));
383 gtk_paned_pack2 (GtkPaned *paned,
388 g_return_if_fail (GTK_IS_PANED (paned));
389 g_return_if_fail (GTK_IS_WIDGET (child));
393 paned->child2 = child;
394 paned->child2_resize = resize;
395 paned->child2_shrink = shrink;
397 gtk_widget_set_parent (child, GTK_WIDGET (paned));
403 gtk_paned_add (GtkContainer *container,
408 g_return_if_fail (GTK_IS_PANED (container));
409 g_return_if_fail (widget != NULL);
411 paned = GTK_PANED (container);
414 gtk_paned_add1 (GTK_PANED (container), widget);
415 else if (!paned->child2)
416 gtk_paned_add2 (GTK_PANED (container), widget);
420 gtk_paned_remove (GtkContainer *container,
424 gboolean was_visible;
426 g_return_if_fail (GTK_IS_PANED (container));
427 g_return_if_fail (widget != NULL);
429 paned = GTK_PANED (container);
430 was_visible = GTK_WIDGET_VISIBLE (widget);
432 if (paned->child1 == widget)
434 gtk_widget_unparent (widget);
436 paned->child1 = NULL;
438 if (was_visible && GTK_WIDGET_VISIBLE (container))
439 gtk_widget_queue_resize (GTK_WIDGET (container));
441 else if (paned->child2 == widget)
443 gtk_widget_unparent (widget);
445 paned->child2 = NULL;
447 if (was_visible && GTK_WIDGET_VISIBLE (container))
448 gtk_widget_queue_resize (GTK_WIDGET (container));
453 gtk_paned_forall (GtkContainer *container,
454 gboolean include_internals,
455 GtkCallback callback,
456 gpointer callback_data)
460 g_return_if_fail (GTK_IS_PANED (container));
461 g_return_if_fail (callback != NULL);
463 paned = GTK_PANED (container);
466 (*callback) (paned->child1, callback_data);
468 (*callback) (paned->child2, callback_data);
472 * gtk_paned_get_position:
473 * @paned: a #GtkPaned widget
475 * Obtains the position of the divider between the two panes.
477 * Return value: position of the divider
480 gtk_paned_get_position (GtkPaned *paned)
482 g_return_val_if_fail (GTK_IS_PANED (paned), 0);
484 return paned->child1_size;
488 * gtk_paned_set_position:
489 * @paned: a #GtkPaned widget
490 * @position: pixel position of divider, a negative value means that the position
493 * Sets the position of the divider between the two panes.
496 gtk_paned_set_position (GtkPaned *paned,
501 g_return_if_fail (GTK_IS_PANED (paned));
503 object = G_OBJECT (paned);
507 /* We don't clamp here - the assumption is that
508 * if the total allocation changes at the same time
509 * as the position, the position set is with reference
510 * to the new total size. If only the position changes,
511 * then clamping will occur in gtk_paned_compute_position()
514 paned->child1_size = position;
515 paned->position_set = TRUE;
519 paned->position_set = FALSE;
522 g_object_freeze_notify (object);
523 g_object_notify (object, "position");
524 g_object_notify (object, "position_set");
525 g_object_thaw_notify (object);
527 gtk_widget_queue_resize (GTK_WIDGET (paned));
531 gtk_paned_compute_position (GtkPaned *paned,
538 g_return_if_fail (GTK_IS_PANED (paned));
540 old_position = paned->child1_size;
542 paned->min_position = paned->child1_shrink ? 0 : child1_req;
544 paned->max_position = allocation;
545 if (!paned->child2_shrink)
546 paned->max_position = MAX (1, paned->max_position - child2_req);
548 if (!paned->position_set)
550 if (paned->child1_resize && !paned->child2_resize)
551 paned->child1_size = MAX (1, allocation - child2_req);
552 else if (!paned->child1_resize && paned->child2_resize)
553 paned->child1_size = child1_req;
554 else if (child1_req + child2_req != 0)
555 paned->child1_size = allocation * ((gdouble)child1_req / (child1_req + child2_req));
557 paned->child1_size = allocation * 0.5;
561 /* If the position was set before the initial allocation.
562 * (paned->last_allocation <= 0) just clamp it and leave it.
564 if (paned->last_allocation > 0)
566 if (paned->child1_resize && !paned->child2_resize)
567 paned->child1_size += allocation - paned->last_allocation;
568 else if (!(!paned->child1_resize && paned->child2_resize))
569 paned->child1_size = allocation * ((gdouble) paned->child1_size / (paned->last_allocation));
573 paned->child1_size = CLAMP (paned->child1_size,
575 paned->max_position);
577 if (paned->child1_size != old_position)
578 g_object_notify (G_OBJECT (paned), "position");
580 paned->last_allocation = allocation;