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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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 #include "gtksignal.h"
31 static void gtk_vpaned_class_init (GtkVPanedClass *klass);
32 static void gtk_vpaned_init (GtkVPaned *vpaned);
33 static void gtk_vpaned_size_request (GtkWidget *widget,
34 GtkRequisition *requisition);
35 static void gtk_vpaned_size_allocate (GtkWidget *widget,
36 GtkAllocation *allocation);
37 static void gtk_vpaned_draw (GtkWidget *widget,
39 static void gtk_vpaned_xor_line (GtkPaned *paned);
40 static gint gtk_vpaned_button_press (GtkWidget *widget,
41 GdkEventButton *event);
42 static gint gtk_vpaned_button_release (GtkWidget *widget,
43 GdkEventButton *event);
44 static gint gtk_vpaned_motion (GtkWidget *widget,
45 GdkEventMotion *event);
48 gtk_vpaned_get_type (void)
50 static GtkType vpaned_type = 0;
54 static const GtkTypeInfo vpaned_info =
58 sizeof (GtkVPanedClass),
59 (GtkClassInitFunc) gtk_vpaned_class_init,
60 (GtkObjectInitFunc) gtk_vpaned_init,
61 /* reserved_1 */ NULL,
62 /* reserved_2 */ NULL,
63 (GtkClassInitFunc) NULL,
66 vpaned_type = gtk_type_unique (gtk_paned_get_type (), &vpaned_info);
73 gtk_vpaned_class_init (GtkVPanedClass *class)
75 GtkWidgetClass *widget_class;
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->draw = gtk_vpaned_draw;
82 widget_class->button_press_event = gtk_vpaned_button_press;
83 widget_class->button_release_event = gtk_vpaned_button_release;
84 widget_class->motion_notify_event = gtk_vpaned_motion;
88 gtk_vpaned_init (GtkVPaned *vpaned)
97 vpaned = gtk_type_new (gtk_vpaned_get_type ());
99 return GTK_WIDGET (vpaned);
103 gtk_vpaned_size_request (GtkWidget *widget,
104 GtkRequisition *requisition)
107 GtkRequisition child_requisition;
109 g_return_if_fail (widget != NULL);
110 g_return_if_fail (GTK_IS_VPANED (widget));
111 g_return_if_fail (requisition != NULL);
113 paned = GTK_PANED (widget);
114 requisition->width = 0;
115 requisition->height = 0;
117 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
119 gtk_widget_size_request (paned->child1, &child_requisition);
121 requisition->height = child_requisition.height;
122 requisition->width = child_requisition.width;
125 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
127 gtk_widget_size_request (paned->child2, &child_requisition);
129 requisition->width = MAX (requisition->width, child_requisition.width);
130 requisition->height += child_requisition.height;
133 requisition->height += GTK_CONTAINER (paned)->border_width * 2 + paned->gutter_size;
134 requisition->width += GTK_CONTAINER (paned)->border_width * 2;
138 gtk_vpaned_size_allocate (GtkWidget *widget,
139 GtkAllocation *allocation)
142 GtkRequisition child1_requisition;
143 GtkRequisition child2_requisition;
144 GtkAllocation child1_allocation;
145 GtkAllocation child2_allocation;
146 GdkRectangle old_groove_rectangle;
147 guint16 border_width;
149 g_return_if_fail (widget != NULL);
150 g_return_if_fail (GTK_IS_VPANED (widget));
151 g_return_if_fail (allocation != NULL);
153 widget->allocation = *allocation;
155 paned = GTK_PANED (widget);
156 border_width = GTK_CONTAINER (widget)->border_width;
159 gtk_widget_get_child_requisition (paned->child1, &child1_requisition);
161 child1_requisition.height = 0;
164 gtk_widget_get_child_requisition (paned->child2, &child2_requisition);
166 child2_requisition.height = 0;
168 gtk_paned_compute_position (paned,
169 widget->allocation.height
172 child1_requisition.height,
173 child2_requisition.height);
175 /* Move the handle before the children so we don't get extra expose events */
177 paned->handle_xpos = allocation->width - border_width - 2 * paned->handle_size;
178 paned->handle_ypos = paned->child1_size + border_width + paned->gutter_size / 2 - paned->handle_size / 2;
180 if (GTK_WIDGET_REALIZED (widget))
182 gdk_window_move_resize (widget->window,
183 allocation->x, allocation->y,
184 allocation->width, allocation->height);
186 gdk_window_move (paned->handle, paned->handle_xpos, paned->handle_ypos);
189 child1_allocation.width = child2_allocation.width = MAX (1, (gint)allocation->width - border_width * 2);
190 child1_allocation.height = paned->child1_size;
191 child1_allocation.x = child2_allocation.x = border_width;
192 child1_allocation.y = border_width;
194 old_groove_rectangle = paned->groove_rectangle;
196 paned->groove_rectangle.y = child1_allocation.y
197 + child1_allocation.height + paned->gutter_size / 2 - 1;
198 paned->groove_rectangle.x = 0;
199 paned->groove_rectangle.height = 2;
200 paned->groove_rectangle.width = allocation->width;
202 if (GTK_WIDGET_DRAWABLE (widget) &&
203 ((paned->groove_rectangle.x != old_groove_rectangle.x) ||
204 (paned->groove_rectangle.y != old_groove_rectangle.y) ||
205 (paned->groove_rectangle.width != old_groove_rectangle.width) ||
206 (paned->groove_rectangle.height != old_groove_rectangle.height)))
208 gtk_widget_queue_clear_area (widget,
209 old_groove_rectangle.x,
210 old_groove_rectangle.y,
211 old_groove_rectangle.width,
212 old_groove_rectangle.height);
213 gtk_widget_queue_draw_area (widget,
214 paned->groove_rectangle.x,
215 paned->groove_rectangle.y,
216 paned->groove_rectangle.width,
217 paned->groove_rectangle.height);
220 child2_allocation.y = paned->groove_rectangle.y + paned->gutter_size / 2 + 1;
221 child2_allocation.height = MAX (1, (gint)allocation->height
222 - child2_allocation.y - border_width);
224 /* Now allocate the childen, making sure, when resizing not to
225 * overlap the windows */
226 if (GTK_WIDGET_MAPPED(widget) &&
227 paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
228 paned->child1->allocation.height < child1_allocation.height)
230 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
231 gtk_widget_size_allocate (paned->child2, &child2_allocation);
232 gtk_widget_size_allocate (paned->child1, &child1_allocation);
236 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
237 gtk_widget_size_allocate (paned->child1, &child1_allocation);
238 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
239 gtk_widget_size_allocate (paned->child2, &child2_allocation);
244 gtk_vpaned_draw (GtkWidget *widget,
248 GdkRectangle child_area;
249 guint16 border_width;
251 g_return_if_fail (widget != NULL);
252 g_return_if_fail (GTK_IS_PANED (widget));
254 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
256 paned = GTK_PANED (widget);
257 border_width = GTK_CONTAINER (paned)->border_width;
259 gdk_window_clear_area (widget->window,
260 area->x, area->y, area->width, area->height);
262 gtk_widget_intersect (paned->child1, area, &child_area))
263 gtk_widget_draw (paned->child1, &child_area);
265 gtk_widget_intersect (paned->child2, area, &child_area))
266 gtk_widget_draw (paned->child2, &child_area);
268 gtk_paint_hline(widget->style, widget->window, GTK_STATE_NORMAL,
269 area, widget, "vpaned",
270 0, widget->allocation.width - 1,
271 border_width + paned->child1_size + paned->gutter_size / 2 - 1);
276 gtk_vpaned_xor_line (GtkPaned *paned)
282 widget = GTK_WIDGET(paned);
286 values.foreground = widget->style->white;
287 values.function = GDK_XOR;
288 values.subwindow_mode = GDK_INCLUDE_INFERIORS;
289 paned->xor_gc = gdk_gc_new_with_values (widget->window,
296 ypos = paned->child1_size
297 + GTK_CONTAINER (paned)->border_width + paned->gutter_size / 2;
299 gdk_draw_line (widget->window, paned->xor_gc,
302 widget->allocation.width - 1,
307 gtk_vpaned_button_press (GtkWidget *widget, GdkEventButton *event)
311 g_return_val_if_fail (widget != NULL,FALSE);
312 g_return_val_if_fail (GTK_IS_PANED (widget),FALSE);
314 paned = GTK_PANED (widget);
316 if (!paned->in_drag &&
317 (event->window == paned->handle) && (event->button == 1))
319 paned->in_drag = TRUE;
320 /* We need a server grab here, not gtk_grab_add(), since
321 * we don't want to pass events on to the widget's children */
322 gdk_pointer_grab (paned->handle, FALSE,
323 GDK_POINTER_MOTION_HINT_MASK
324 | GDK_BUTTON1_MOTION_MASK
325 | GDK_BUTTON_RELEASE_MASK,
326 NULL, NULL, event->time);
327 paned->child1_size += event->y - paned->handle_size / 2;
328 paned->child1_size = CLAMP (paned->child1_size, 0,
329 widget->allocation.height - paned->gutter_size
330 - 2 * GTK_CONTAINER (paned)->border_width);
331 gtk_vpaned_xor_line (paned);
338 gtk_vpaned_button_release (GtkWidget *widget, GdkEventButton *event)
342 g_return_val_if_fail (widget != NULL, FALSE);
343 g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
345 paned = GTK_PANED (widget);
347 if (paned->in_drag && (event->button == 1))
349 gtk_vpaned_xor_line (paned);
350 paned->in_drag = FALSE;
351 paned->position_set = TRUE;
352 gdk_pointer_ungrab (event->time);
353 gtk_widget_queue_resize (GTK_WIDGET (paned));
360 gtk_vpaned_motion (GtkWidget *widget, GdkEventMotion *event)
365 g_return_val_if_fail (widget != NULL, FALSE);
366 g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
368 if (event->is_hint || event->window != widget->window)
369 gtk_widget_get_pointer(widget, NULL, &y);
373 paned = GTK_PANED (widget);
377 gint size = y - GTK_CONTAINER (paned)->border_width - paned->gutter_size/2;
379 gtk_vpaned_xor_line (paned);
380 paned->child1_size = CLAMP (size,
382 paned->max_position);
383 gtk_vpaned_xor_line (paned);