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 "gtkhpaned.h"
21 #include "gtksignal.h"
23 static void gtk_hpaned_class_init (GtkHPanedClass *klass);
24 static void gtk_hpaned_init (GtkHPaned *hpaned);
25 static void gtk_hpaned_size_request (GtkWidget *widget,
26 GtkRequisition *requisition);
27 static void gtk_hpaned_size_allocate (GtkWidget *widget,
28 GtkAllocation *allocation);
29 static void gtk_hpaned_draw (GtkWidget *widget,
31 static void gtk_hpaned_xor_line (GtkPaned *paned);
32 static gint gtk_hpaned_button_press (GtkWidget *widget,
33 GdkEventButton *event);
34 static gint gtk_hpaned_button_release (GtkWidget *widget,
35 GdkEventButton *event);
36 static gint gtk_hpaned_motion (GtkWidget *widget,
37 GdkEventMotion *event);
40 gtk_hpaned_get_type (void)
42 static guint hpaned_type = 0;
46 static const GtkTypeInfo hpaned_info =
50 sizeof (GtkHPanedClass),
51 (GtkClassInitFunc) gtk_hpaned_class_init,
52 (GtkObjectInitFunc) gtk_hpaned_init,
53 /* reserved_1 */ NULL,
54 /* reserved_2 */ NULL,
55 (GtkClassInitFunc) NULL,
58 hpaned_type = gtk_type_unique (gtk_paned_get_type (), &hpaned_info);
65 gtk_hpaned_class_init (GtkHPanedClass *class)
67 GtkWidgetClass *widget_class;
69 widget_class = (GtkWidgetClass*) class;
71 widget_class->size_request = gtk_hpaned_size_request;
72 widget_class->size_allocate = gtk_hpaned_size_allocate;
73 widget_class->draw = gtk_hpaned_draw;
74 widget_class->button_press_event = gtk_hpaned_button_press;
75 widget_class->button_release_event = gtk_hpaned_button_release;
76 widget_class->motion_notify_event = gtk_hpaned_motion;
80 gtk_hpaned_init (GtkHPaned *hpaned)
89 hpaned = gtk_type_new (gtk_hpaned_get_type ());
91 return GTK_WIDGET (hpaned);
95 gtk_hpaned_size_request (GtkWidget *widget,
96 GtkRequisition *requisition)
99 GtkRequisition child_requisition;
101 g_return_if_fail (widget != NULL);
102 g_return_if_fail (GTK_IS_HPANED (widget));
103 g_return_if_fail (requisition != NULL);
105 paned = GTK_PANED (widget);
106 requisition->width = 0;
107 requisition->height = 0;
109 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
111 gtk_widget_size_request (paned->child1, &child_requisition);
113 requisition->height = child_requisition.height;
114 requisition->width = child_requisition.width;
117 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
119 gtk_widget_size_request (paned->child2, &child_requisition);
121 requisition->height = MAX(requisition->height, child_requisition.height);
122 requisition->width += child_requisition.width;
125 requisition->width += GTK_CONTAINER (paned)->border_width * 2 + paned->gutter_size;
126 requisition->height += GTK_CONTAINER (paned)->border_width * 2;
130 gtk_hpaned_size_allocate (GtkWidget *widget,
131 GtkAllocation *allocation)
134 GtkRequisition child1_requisition;
135 GtkRequisition child2_requisition;
136 GtkAllocation child1_allocation;
137 GtkAllocation child2_allocation;
138 GdkRectangle old_groove_rectangle;
139 guint16 border_width;
141 g_return_if_fail (widget != NULL);
142 g_return_if_fail (GTK_IS_HPANED (widget));
143 g_return_if_fail (allocation != NULL);
145 widget->allocation = *allocation;
147 paned = GTK_PANED (widget);
148 border_width = GTK_CONTAINER (paned)->border_width;
151 gtk_widget_get_child_requisition (paned->child1, &child1_requisition);
153 child1_requisition.width = 0;
156 gtk_widget_get_child_requisition (paned->child2, &child2_requisition);
158 child2_requisition.width = 0;
160 gtk_paned_compute_position (paned,
161 widget->allocation.width
164 child1_requisition.width,
165 child2_requisition.width);
167 /* Move the handle before the children so we don't get extra expose events */
169 paned->handle_xpos = paned->child1_size + border_width + paned->gutter_size / 2 - paned->handle_size / 2;
170 paned->handle_ypos = allocation->height - border_width - 2*paned->handle_size;
172 if (GTK_WIDGET_REALIZED (widget))
174 gdk_window_move_resize (widget->window,
175 allocation->x, allocation->y,
176 allocation->width, allocation->height);
178 gdk_window_move (paned->handle, paned->handle_xpos, paned->handle_ypos);
181 child1_allocation.height = child2_allocation.height = MAX (1, (gint)allocation->height - border_width * 2);
182 child1_allocation.width = paned->child1_size;
183 child1_allocation.x = border_width;
184 child1_allocation.y = child2_allocation.y = border_width;
186 old_groove_rectangle = paned->groove_rectangle;
188 paned->groove_rectangle.x = child1_allocation.x
189 + child1_allocation.width + paned->gutter_size / 2 - 1;
190 paned->groove_rectangle.y = 0;
191 paned->groove_rectangle.width = 2;
192 paned->groove_rectangle.height = allocation->height;
194 if (GTK_WIDGET_DRAWABLE (widget) &&
195 ((paned->groove_rectangle.x != old_groove_rectangle.x) ||
196 (paned->groove_rectangle.y != old_groove_rectangle.y) ||
197 (paned->groove_rectangle.width != old_groove_rectangle.width) ||
198 (paned->groove_rectangle.height != old_groove_rectangle.height)))
200 gtk_widget_queue_clear_area (widget,
201 old_groove_rectangle.x,
202 old_groove_rectangle.y,
203 old_groove_rectangle.width,
204 old_groove_rectangle.height);
205 gtk_widget_queue_draw_area (widget,
206 paned->groove_rectangle.x,
207 paned->groove_rectangle.y,
208 paned->groove_rectangle.width,
209 paned->groove_rectangle.height);
212 child2_allocation.x = paned->groove_rectangle.x + paned->gutter_size / 2 + 1;
213 child2_allocation.width = MAX (1, (gint)allocation->width
214 - child2_allocation.x - border_width);
216 /* Now allocate the childen, making sure, when resizing not to
217 * overlap the windows */
218 if (GTK_WIDGET_MAPPED(widget) &&
219 paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
220 paned->child1->allocation.width < child1_allocation.width)
222 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
223 gtk_widget_size_allocate (paned->child2, &child2_allocation);
224 gtk_widget_size_allocate (paned->child1, &child1_allocation);
228 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
229 gtk_widget_size_allocate (paned->child1, &child1_allocation);
230 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
231 gtk_widget_size_allocate (paned->child2, &child2_allocation);
236 gtk_hpaned_draw (GtkWidget *widget,
240 GdkRectangle child_area;
241 guint16 border_width;
243 g_return_if_fail (widget != NULL);
244 g_return_if_fail (GTK_IS_PANED (widget));
246 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
248 paned = GTK_PANED (widget);
249 border_width = GTK_CONTAINER (paned)->border_width;
251 gdk_window_clear_area (widget->window,
252 area->x, area->y, area->width, area->height);
254 gtk_widget_intersect (paned->child1, area, &child_area))
255 gtk_widget_draw (paned->child1, &child_area);
257 gtk_widget_intersect (paned->child2, area, &child_area))
258 gtk_widget_draw (paned->child2, &child_area);
260 gtk_paint_vline(widget->style, widget->window, GTK_STATE_NORMAL,
261 area, widget, "hpaned",
262 0, widget->allocation.height - 1,
263 border_width + paned->child1_size + paned->gutter_size / 2 - 1);
268 gtk_hpaned_xor_line (GtkPaned *paned)
274 widget = GTK_WIDGET(paned);
278 values.foreground = widget->style->white;
279 values.function = GDK_XOR;
280 values.subwindow_mode = GDK_INCLUDE_INFERIORS;
281 paned->xor_gc = gdk_gc_new_with_values (widget->window,
288 xpos = paned->child1_size
289 + GTK_CONTAINER(paned)->border_width + paned->gutter_size / 2;
291 gdk_draw_line (widget->window, paned->xor_gc,
295 widget->allocation.height - 1);
299 gtk_hpaned_button_press (GtkWidget *widget, GdkEventButton *event)
303 g_return_val_if_fail (widget != NULL,FALSE);
304 g_return_val_if_fail (GTK_IS_PANED (widget),FALSE);
306 paned = GTK_PANED (widget);
308 if (!paned->in_drag &&
309 (event->window == paned->handle) && (event->button == 1))
311 paned->in_drag = TRUE;
312 /* We need a server grab here, not gtk_grab_add(), since
313 * we don't want to pass events on to the widget's children */
314 gdk_pointer_grab (paned->handle, FALSE,
315 GDK_POINTER_MOTION_HINT_MASK
316 | GDK_BUTTON1_MOTION_MASK
317 | GDK_BUTTON_RELEASE_MASK,
318 NULL, NULL, event->time);
319 paned->child1_size += event->x - paned->handle_size / 2;
320 paned->child1_size = CLAMP (paned->child1_size, 0,
321 widget->allocation.width - paned->gutter_size
322 - 2 * GTK_CONTAINER (paned)->border_width);
323 gtk_hpaned_xor_line (paned);
330 gtk_hpaned_button_release (GtkWidget *widget, GdkEventButton *event)
334 g_return_val_if_fail (widget != NULL,FALSE);
335 g_return_val_if_fail (GTK_IS_PANED (widget),FALSE);
337 paned = GTK_PANED (widget);
339 if (paned->in_drag && (event->button == 1))
341 gtk_hpaned_xor_line (paned);
342 paned->in_drag = FALSE;
343 paned->position_set = TRUE;
344 gdk_pointer_ungrab (event->time);
345 gtk_widget_queue_resize (GTK_WIDGET (paned));
352 gtk_hpaned_motion (GtkWidget *widget, GdkEventMotion *event)
357 g_return_val_if_fail (widget != NULL, FALSE);
358 g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
360 paned = GTK_PANED (widget);
362 if (event->is_hint || event->window != widget->window)
363 gtk_widget_get_pointer(widget, &x, NULL);
369 gint size = x - GTK_CONTAINER (paned)->border_width - paned->gutter_size/2;
371 gtk_hpaned_xor_line (paned);
372 paned->child1_size = CLAMP (size,
374 paned->max_position);
375 gtk_hpaned_xor_line (paned);