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 GtkTypeInfo hpaned_info =
50 sizeof (GtkHPanedClass),
51 (GtkClassInitFunc) gtk_hpaned_class_init,
52 (GtkObjectInitFunc) gtk_hpaned_init,
53 /* reversed_1 */ NULL,
54 /* reversed_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)
100 g_return_if_fail (widget != NULL);
101 g_return_if_fail (GTK_IS_HPANED (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->height = MAX(requisition->height,
121 paned->child2->requisition.height);
122 requisition->width += paned->child2->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 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_HPANED (widget));
140 g_return_if_fail (allocation != NULL);
142 widget->allocation = *allocation;
144 paned = GTK_PANED (widget);
145 border_width = GTK_CONTAINER (paned)->border_width;
147 if (!paned->position_set)
149 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
150 paned->child1_size = paned->child1->requisition.width;
152 paned->child1_size = 0;
155 paned->child1_size = CLAMP (paned->child1_size, 0,
156 allocation->width - paned->gutter_size
157 - 2 * GTK_CONTAINER (paned)->border_width);
159 /* Move the handle before the children so we don't get extra expose events */
161 paned->handle_xpos = paned->child1_size + border_width + paned->gutter_size / 2 - paned->handle_size / 2;
162 paned->handle_ypos = allocation->height - border_width - 2*paned->handle_size;
164 if (GTK_WIDGET_REALIZED (widget))
166 gdk_window_move_resize (widget->window,
167 allocation->x, allocation->y,
168 allocation->width, allocation->height);
170 gdk_window_move (paned->handle, paned->handle_xpos, paned->handle_ypos);
173 if (GTK_WIDGET_MAPPED (widget))
175 gdk_window_clear_area (widget->window,
176 paned->groove_rectangle.x,
177 paned->groove_rectangle.y,
178 paned->groove_rectangle.width,
179 paned->groove_rectangle.height);
182 child1_allocation.height = child2_allocation.height = MAX (1, allocation->height - border_width * 2);
183 child1_allocation.width = paned->child1_size;
184 child1_allocation.x = border_width;
185 child1_allocation.y = child2_allocation.y = border_width;
187 paned->groove_rectangle.x = child1_allocation.x
188 + child1_allocation.width + paned->gutter_size / 2 - 1;
189 paned->groove_rectangle.y = 0;
190 paned->groove_rectangle.width = 2;
191 paned->groove_rectangle.height = allocation->height;
193 child2_allocation.x = paned->groove_rectangle.x + paned->gutter_size / 2 + 1;
194 child2_allocation.width = MAX (1, allocation->width
195 - child2_allocation.x - border_width);
197 /* Now allocate the childen, making sure, when resizing not to
198 * overlap the windows */
199 if (GTK_WIDGET_MAPPED(widget) &&
200 paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
201 paned->child1->allocation.width < child1_allocation.width)
203 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
204 gtk_widget_size_allocate (paned->child2, &child2_allocation);
205 gtk_widget_size_allocate (paned->child1, &child1_allocation);
209 if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
210 gtk_widget_size_allocate (paned->child1, &child1_allocation);
211 if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
212 gtk_widget_size_allocate (paned->child2, &child2_allocation);
217 gtk_hpaned_draw (GtkWidget *widget,
221 GdkRectangle child_area;
222 guint16 border_width;
224 g_return_if_fail (widget != NULL);
225 g_return_if_fail (GTK_IS_PANED (widget));
227 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
229 paned = GTK_PANED (widget);
230 border_width = GTK_CONTAINER (paned)->border_width;
233 gtk_widget_intersect (paned->child1, area, &child_area))
234 gtk_widget_draw (paned->child1, &child_area);
236 gtk_widget_intersect (paned->child2, area, &child_area))
237 gtk_widget_draw (paned->child2, &child_area);
239 gdk_draw_line (widget->window,
240 widget->style->dark_gc[widget->state],
241 border_width + paned->child1_size + paned->gutter_size / 2 - 1,
243 border_width + paned->child1_size + paned->gutter_size / 2 - 1,
244 widget->allocation.height - 1);
245 gdk_draw_line (widget->window,
246 widget->style->light_gc[widget->state],
247 border_width + paned->child1_size + paned->gutter_size / 2,
249 border_width + paned->child1_size + paned->gutter_size / 2,
250 widget->allocation.height - 1);
255 gtk_hpaned_xor_line (GtkPaned *paned)
261 widget = GTK_WIDGET(paned);
265 values.foreground = widget->style->white;
266 values.function = GDK_XOR;
267 values.subwindow_mode = GDK_INCLUDE_INFERIORS;
268 paned->xor_gc = gdk_gc_new_with_values (widget->window,
275 xpos = paned->child1_size
276 + GTK_CONTAINER(paned)->border_width + paned->gutter_size / 2;
278 gdk_draw_line (widget->window, paned->xor_gc,
282 widget->allocation.height - 1);
286 gtk_hpaned_button_press (GtkWidget *widget, GdkEventButton *event)
290 g_return_val_if_fail (widget != NULL,FALSE);
291 g_return_val_if_fail (GTK_IS_PANED (widget),FALSE);
293 paned = GTK_PANED (widget);
295 if (!paned->in_drag &&
296 (event->window == paned->handle) && (event->button == 1))
298 paned->in_drag = TRUE;
299 /* We need a server grab here, not gtk_grab_add(), since
300 * we don't want to pass events on to the widget's children */
301 gdk_pointer_grab (paned->handle, FALSE,
302 GDK_POINTER_MOTION_HINT_MASK
303 | GDK_BUTTON1_MOTION_MASK
304 | GDK_BUTTON_RELEASE_MASK,
305 NULL, NULL, event->time);
306 paned->child1_size += event->x - paned->handle_size / 2;
307 paned->child1_size = CLAMP (paned->child1_size, 0,
308 widget->allocation.width - paned->gutter_size
309 - 2 * GTK_CONTAINER (paned)->border_width);
310 gtk_hpaned_xor_line (paned);
317 gtk_hpaned_button_release (GtkWidget *widget, GdkEventButton *event)
321 g_return_val_if_fail (widget != NULL,FALSE);
322 g_return_val_if_fail (GTK_IS_PANED (widget),FALSE);
324 paned = GTK_PANED (widget);
326 if (paned->in_drag && (event->button == 1))
328 gtk_hpaned_xor_line (paned);
329 paned->in_drag = FALSE;
330 paned->position_set = TRUE;
331 gdk_pointer_ungrab (event->time);
332 gtk_widget_queue_resize (GTK_WIDGET (paned));
339 gtk_hpaned_motion (GtkWidget *widget, GdkEventMotion *event)
344 g_return_val_if_fail (widget != NULL, FALSE);
345 g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
347 if (event->is_hint || event->window != widget->window)
348 gtk_widget_get_pointer(widget, &x, NULL);
352 paned = GTK_PANED (widget);
356 gtk_hpaned_xor_line (paned);
357 paned->child1_size = x - GTK_CONTAINER (paned)->border_width - paned->gutter_size / 2;
358 paned->child1_size = CLAMP (paned->child1_size, 0,
359 widget->allocation.width - paned->gutter_size
360 - 2 * GTK_CONTAINER (paned)->border_width);
361 gtk_hpaned_xor_line (paned);