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.
19 #include "gtkvpaned.h"
21 #include "gtksignal.h"
23 static void gtk_vpaned_class_init (GtkVPanedClass *klass);
24 static void gtk_vpaned_init (GtkVPaned *vpaned);
25 static void gtk_vpaned_size_request (GtkWidget *widget,
26 GtkRequisition *requisition);
27 static void gtk_vpaned_size_allocate (GtkWidget *widget,
28 GtkAllocation *allocation);
29 static void gtk_vpaned_draw (GtkWidget *widget,
31 static void gtk_vpaned_xor_line (GtkPaned *paned);
32 static gint gtk_vpaned_button_press (GtkWidget *widget,
33 GdkEventButton *event);
34 static gint gtk_vpaned_button_release (GtkWidget *widget,
35 GdkEventButton *event);
36 static gint gtk_vpaned_motion (GtkWidget *widget,
37 GdkEventMotion *event);
40 gtk_vpaned_get_type (void)
42 static guint vpaned_type = 0;
46 static const GtkTypeInfo vpaned_info =
50 sizeof (GtkVPanedClass),
51 (GtkClassInitFunc) gtk_vpaned_class_init,
52 (GtkObjectInitFunc) gtk_vpaned_init,
53 /* reserved_1 */ NULL,
54 /* reserved_2 */ NULL,
55 (GtkClassInitFunc) NULL,
58 vpaned_type = gtk_type_unique (gtk_paned_get_type (), &vpaned_info);
65 gtk_vpaned_class_init (GtkVPanedClass *class)
67 GtkWidgetClass *widget_class;
69 widget_class = (GtkWidgetClass*) class;
71 widget_class->size_request = gtk_vpaned_size_request;
72 widget_class->size_allocate = gtk_vpaned_size_allocate;
73 widget_class->draw = gtk_vpaned_draw;
74 widget_class->button_press_event = gtk_vpaned_button_press;
75 widget_class->button_release_event = gtk_vpaned_button_release;
76 widget_class->motion_notify_event = gtk_vpaned_motion;
80 gtk_vpaned_init (GtkVPaned *vpaned)
89 vpaned = gtk_type_new (gtk_vpaned_get_type ());
91 return GTK_WIDGET (vpaned);
95 gtk_vpaned_size_request (GtkWidget *widget,
96 GtkRequisition *requisition)
100 g_return_if_fail (widget != NULL);
101 g_return_if_fail (GTK_IS_VPANED (widget));
102 g_return_if_fail (requisition != NULL);
104 paned = GTK_PANED (widget);
105 requisition->width = 0;
106 requisition->height = 0;
108 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
110 gtk_widget_size_request (paned->child1, &paned->child1->requisition);
112 requisition->height = paned->child1->requisition.height;
113 requisition->width = paned->child1->requisition.width;
116 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
118 gtk_widget_size_request (paned->child2, &paned->child2->requisition);
120 requisition->width = MAX (requisition->width,
121 paned->child2->requisition.width);
122 requisition->height += paned->child2->requisition.height;
125 requisition->height += GTK_CONTAINER (paned)->border_width * 2 + paned->gutter_size;
126 requisition->width += GTK_CONTAINER (paned)->border_width * 2;
130 gtk_vpaned_size_allocate (GtkWidget *widget,
131 GtkAllocation *allocation)
134 GtkAllocation child1_allocation;
135 GtkAllocation child2_allocation;
136 guint16 border_width;
138 g_return_if_fail (widget != NULL);
139 g_return_if_fail (GTK_IS_VPANED (widget));
140 g_return_if_fail (allocation != NULL);
142 widget->allocation = *allocation;
144 paned = GTK_PANED (widget);
145 border_width = GTK_CONTAINER (widget)->border_width;
147 gtk_paned_compute_position (paned,
148 widget->allocation.height
151 paned->child1->requisition.height,
152 paned->child2->requisition.height);
154 /* Move the handle before the children so we don't get extra expose events */
156 paned->handle_xpos = allocation->width - border_width - 2 * paned->handle_size;
157 paned->handle_ypos = paned->child1_size + border_width + paned->gutter_size / 2 - paned->handle_size / 2;
159 if (GTK_WIDGET_REALIZED (widget))
161 gdk_window_move_resize (widget->window,
162 allocation->x, allocation->y,
163 allocation->width, allocation->height);
165 gdk_window_move (paned->handle, paned->handle_xpos, paned->handle_ypos);
168 if (GTK_WIDGET_MAPPED (widget))
170 gdk_window_clear_area (widget->window,
171 paned->groove_rectangle.x,
172 paned->groove_rectangle.y,
173 paned->groove_rectangle.width,
174 paned->groove_rectangle.height);
177 child1_allocation.width = child2_allocation.width = MAX (1, allocation->width - border_width * 2);
178 child1_allocation.height = paned->child1_size;
179 child1_allocation.x = child2_allocation.x = border_width;
180 child1_allocation.y = border_width;
182 paned->groove_rectangle.y = child1_allocation.y
183 + child1_allocation.height + paned->gutter_size / 2 - 1;
184 paned->groove_rectangle.x = 0;
185 paned->groove_rectangle.height = 2;
186 paned->groove_rectangle.width = allocation->width;
188 child2_allocation.y = paned->groove_rectangle.y + paned->gutter_size / 2 + 1;
189 child2_allocation.height = MAX (1, allocation->height
190 - child2_allocation.y - border_width);
192 /* Now allocate the childen, making sure, when resizing not to
193 * overlap the windows */
194 if (GTK_WIDGET_MAPPED(widget) &&
195 paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
196 paned->child1->allocation.height < child1_allocation.height)
198 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
199 gtk_widget_size_allocate (paned->child2, &child2_allocation);
200 gtk_widget_size_allocate (paned->child1, &child1_allocation);
204 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
205 gtk_widget_size_allocate (paned->child1, &child1_allocation);
206 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
207 gtk_widget_size_allocate (paned->child2, &child2_allocation);
212 gtk_vpaned_draw (GtkWidget *widget,
216 GdkRectangle child_area;
217 guint16 border_width;
219 g_return_if_fail (widget != NULL);
220 g_return_if_fail (GTK_IS_PANED (widget));
222 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
224 paned = GTK_PANED (widget);
225 border_width = GTK_CONTAINER (paned)->border_width;
227 gdk_window_clear_area (widget->window,
228 area->x, area->y, area->width, area->height);
230 gtk_widget_intersect (paned->child1, area, &child_area))
231 gtk_widget_draw (paned->child1, &child_area);
233 gtk_widget_intersect (paned->child2, area, &child_area))
234 gtk_widget_draw (paned->child2, &child_area);
236 gtk_paint_hline(widget->style, widget->window, GTK_STATE_NORMAL,
237 area, widget, "vpaned",
238 0, widget->allocation.width - 1,
239 border_width + paned->child1_size + paned->gutter_size / 2 - 1);
244 gtk_vpaned_xor_line (GtkPaned *paned)
250 widget = GTK_WIDGET(paned);
254 values.foreground = widget->style->white;
255 values.function = GDK_XOR;
256 values.subwindow_mode = GDK_INCLUDE_INFERIORS;
257 paned->xor_gc = gdk_gc_new_with_values (widget->window,
264 ypos = paned->child1_size
265 + GTK_CONTAINER (paned)->border_width + paned->gutter_size / 2;
267 gdk_draw_line (widget->window, paned->xor_gc,
270 widget->allocation.width - 1,
275 gtk_vpaned_button_press (GtkWidget *widget, GdkEventButton *event)
279 g_return_val_if_fail (widget != NULL,FALSE);
280 g_return_val_if_fail (GTK_IS_PANED (widget),FALSE);
282 paned = GTK_PANED (widget);
284 if (!paned->in_drag &&
285 (event->window == paned->handle) && (event->button == 1))
287 paned->in_drag = TRUE;
288 /* We need a server grab here, not gtk_grab_add(), since
289 * we don't want to pass events on to the widget's children */
290 gdk_pointer_grab (paned->handle, FALSE,
291 GDK_POINTER_MOTION_HINT_MASK
292 | GDK_BUTTON1_MOTION_MASK
293 | GDK_BUTTON_RELEASE_MASK,
294 NULL, NULL, event->time);
295 paned->child1_size += event->y - paned->handle_size / 2;
296 paned->child1_size = CLAMP (paned->child1_size, 0,
297 widget->allocation.height - paned->gutter_size
298 - 2 * GTK_CONTAINER (paned)->border_width);
299 gtk_vpaned_xor_line (paned);
306 gtk_vpaned_button_release (GtkWidget *widget, GdkEventButton *event)
310 g_return_val_if_fail (widget != NULL, FALSE);
311 g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
313 paned = GTK_PANED (widget);
315 if (paned->in_drag && (event->button == 1))
317 gtk_vpaned_xor_line (paned);
318 paned->in_drag = FALSE;
319 paned->position_set = TRUE;
320 gdk_pointer_ungrab (event->time);
321 gtk_widget_queue_resize (GTK_WIDGET (paned));
328 gtk_vpaned_motion (GtkWidget *widget, GdkEventMotion *event)
333 g_return_val_if_fail (widget != NULL, FALSE);
334 g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
336 if (event->is_hint || event->window != widget->window)
337 gtk_widget_get_pointer(widget, NULL, &y);
341 paned = GTK_PANED (widget);
345 gint size = y - GTK_CONTAINER (paned)->border_width - paned->gutter_size/2;
347 gtk_vpaned_xor_line (paned);
348 paned->child1_size = CLAMP (size,
350 paned->max_position);
351 gtk_vpaned_xor_line (paned);