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/.
27 #include "gtkvpaned.h"
29 static void gtk_vpaned_class_init (GtkVPanedClass *klass);
30 static void gtk_vpaned_init (GtkVPaned *vpaned);
31 static void gtk_vpaned_size_request (GtkWidget *widget,
32 GtkRequisition *requisition);
33 static void gtk_vpaned_size_allocate (GtkWidget *widget,
34 GtkAllocation *allocation);
35 static void gtk_vpaned_xor_line (GtkPaned *paned);
36 static gboolean gtk_vpaned_button_press (GtkWidget *widget,
37 GdkEventButton *event);
38 static gboolean gtk_vpaned_button_release (GtkWidget *widget,
39 GdkEventButton *event);
40 static gboolean gtk_vpaned_motion (GtkWidget *widget,
41 GdkEventMotion *event);
43 static gpointer parent_class;
46 gtk_vpaned_get_type (void)
48 static GtkType vpaned_type = 0;
52 static const GtkTypeInfo vpaned_info =
56 sizeof (GtkVPanedClass),
57 (GtkClassInitFunc) gtk_vpaned_class_init,
58 (GtkObjectInitFunc) gtk_vpaned_init,
59 /* reserved_1 */ NULL,
60 /* reserved_2 */ NULL,
61 (GtkClassInitFunc) NULL,
64 vpaned_type = gtk_type_unique (GTK_TYPE_PANED, &vpaned_info);
71 gtk_vpaned_class_init (GtkVPanedClass *class)
73 GtkWidgetClass *widget_class;
75 parent_class = gtk_type_class (GTK_TYPE_PANED);
77 widget_class = (GtkWidgetClass *) class;
79 widget_class->size_request = gtk_vpaned_size_request;
80 widget_class->size_allocate = gtk_vpaned_size_allocate;
81 widget_class->button_press_event = gtk_vpaned_button_press;
82 widget_class->button_release_event = gtk_vpaned_button_release;
83 widget_class->motion_notify_event = gtk_vpaned_motion;
87 gtk_vpaned_init (GtkVPaned *vpaned)
91 g_return_if_fail (GTK_IS_PANED (vpaned));
93 paned = GTK_PANED (vpaned);
95 paned->cursor_type = GDK_SB_V_DOUBLE_ARROW;
96 paned->orientation = GTK_ORIENTATION_HORIZONTAL;
100 gtk_vpaned_new (void)
104 vpaned = gtk_type_new (GTK_TYPE_VPANED);
106 return GTK_WIDGET (vpaned);
110 gtk_vpaned_size_request (GtkWidget *widget,
111 GtkRequisition *requisition)
113 GtkPaned *paned = GTK_PANED (widget);
114 GtkRequisition child_requisition;
116 requisition->width = 0;
117 requisition->height = 0;
119 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
121 gtk_widget_size_request (paned->child1, &child_requisition);
123 requisition->height = child_requisition.height;
124 requisition->width = child_requisition.width;
127 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
129 gtk_widget_size_request (paned->child2, &child_requisition);
131 requisition->width = MAX (requisition->width, child_requisition.width);
132 requisition->height += child_requisition.height;
135 requisition->height += GTK_CONTAINER (paned)->border_width * 2;
136 requisition->width += GTK_CONTAINER (paned)->border_width * 2;
138 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
139 paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
143 gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
144 requisition->height += handle_size;
149 gtk_vpaned_size_allocate (GtkWidget *widget,
150 GtkAllocation *allocation)
152 GtkPaned *paned = GTK_PANED (widget);
153 gint border_width = GTK_CONTAINER (paned)->border_width;
155 widget->allocation = *allocation;
157 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
158 paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
160 GtkRequisition child1_requisition;
161 GtkRequisition child2_requisition;
162 GtkAllocation child1_allocation;
163 GtkAllocation child2_allocation;
166 gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
168 gtk_widget_get_child_requisition (paned->child1, &child1_requisition);
169 gtk_widget_get_child_requisition (paned->child2, &child2_requisition);
171 gtk_paned_compute_position (paned,
172 MAX (1, widget->allocation.height
175 child1_requisition.height,
176 child2_requisition.height);
178 paned->handle_pos.x = widget->allocation.x + border_width;
179 paned->handle_pos.y = widget->allocation.y + paned->child1_size + border_width;
180 paned->handle_pos.width = MAX (1, (gint) widget->allocation.width - 2 * border_width);
181 paned->handle_pos.height = handle_size;
183 if (GTK_WIDGET_REALIZED(widget))
185 gdk_window_show (paned->handle);
186 gdk_window_move_resize (paned->handle,
189 paned->handle_pos.width,
193 child1_allocation.width = child2_allocation.width = MAX (1, (gint) allocation->width - border_width * 2);
194 child1_allocation.height = paned->child1_size;
195 child1_allocation.x = child2_allocation.x = widget->allocation.x + border_width;
196 child1_allocation.y = widget->allocation.y + border_width;
198 child2_allocation.y = child1_allocation.y + child1_allocation.height + paned->handle_pos.height;
199 child2_allocation.height = MAX (1, widget->allocation.y + widget->allocation.height - child2_allocation.y - border_width);
201 /* Now allocate the childen, making sure, when resizing not to
202 * overlap the windows
204 if (GTK_WIDGET_MAPPED (widget) &&
205 paned->child1->allocation.height < child1_allocation.height)
207 gtk_widget_size_allocate (paned->child2, &child2_allocation);
208 gtk_widget_size_allocate (paned->child1, &child1_allocation);
212 gtk_widget_size_allocate (paned->child1, &child1_allocation);
213 gtk_widget_size_allocate (paned->child2, &child2_allocation);
218 GtkAllocation child_allocation;
220 if (GTK_WIDGET_REALIZED (widget))
221 gdk_window_hide (paned->handle);
223 child_allocation.x = widget->allocation.x + border_width;
224 child_allocation.y = widget->allocation.y + border_width;
225 child_allocation.width = MAX (1, allocation->width - 2 * border_width);
226 child_allocation.height = MAX (1, allocation->height - 2 * border_width);
228 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
229 gtk_widget_size_allocate (paned->child1, &child_allocation);
230 else if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
231 gtk_widget_size_allocate (paned->child2, &child_allocation);
236 gtk_vpaned_xor_line (GtkPaned *paned)
243 widget = GTK_WIDGET (paned);
245 gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
249 values.function = GDK_INVERT;
250 values.subwindow_mode = GDK_INCLUDE_INFERIORS;
251 paned->xor_gc = gdk_gc_new_with_values (widget->window,
253 GDK_GC_FUNCTION | GDK_GC_SUBWINDOW);
256 gdk_gc_set_line_attributes (paned->xor_gc, 2, GDK_LINE_SOLID,
257 GDK_CAP_NOT_LAST, GDK_JOIN_BEVEL);
259 ypos = widget->allocation.y + paned->child1_size
260 + GTK_CONTAINER (paned)->border_width + handle_size / 2;
262 gdk_draw_line (widget->window, paned->xor_gc,
263 widget->allocation.x,
265 widget->allocation.x + widget->allocation.width - 1,
270 gtk_vpaned_button_press (GtkWidget *widget,
271 GdkEventButton *event)
273 GtkPaned *paned = GTK_PANED (widget);
276 gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
278 if (!paned->in_drag &&
279 (event->window == paned->handle) && (event->button == 1))
281 paned->in_drag = TRUE;
282 /* We need a server grab here, not gtk_grab_add(), since
283 * we don't want to pass events on to the widget's children */
284 gdk_pointer_grab (paned->handle, FALSE,
285 GDK_POINTER_MOTION_HINT_MASK
286 | GDK_BUTTON1_MOTION_MASK
287 | GDK_BUTTON_RELEASE_MASK, NULL, NULL,
289 paned->child1_size += event->y - handle_size / 2;
290 paned->child1_size = CLAMP (paned->child1_size, 0,
291 widget->allocation.height -
293 2 * GTK_CONTAINER (paned)->border_width);
294 gtk_vpaned_xor_line(paned);
303 gtk_vpaned_button_release (GtkWidget *widget,
304 GdkEventButton *event)
306 GtkPaned *paned = GTK_PANED (widget);
307 GObject *object = G_OBJECT (widget);
309 if (paned->in_drag && (event->button == 1))
311 gtk_vpaned_xor_line (paned);
312 paned->in_drag = FALSE;
313 paned->position_set = TRUE;
314 gdk_pointer_ungrab (event->time);
315 gtk_widget_queue_resize (GTK_WIDGET (paned));
316 g_object_freeze_notify (object);
317 g_object_notify (object, "position");
318 g_object_notify (object, "position_set");
319 g_object_thaw_notify (object);
328 gtk_vpaned_motion (GtkWidget *widget,
329 GdkEventMotion *event)
331 GtkPaned *paned = GTK_PANED (widget);
335 gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
337 gtk_widget_get_pointer (widget, NULL, &y);
341 gint size = y - GTK_CONTAINER (paned)->border_width - handle_size / 2;
343 gtk_vpaned_xor_line (paned);
344 paned->child1_size = CLAMP (size, paned->min_position, paned->max_position);
345 gtk_vpaned_xor_line (paned);