]> Pileus Git - ~andy/gtk/blob - gtk/gtkvpaned.c
5b04c3cae4879262543d2fb44dd7a1e2ad568f18
[~andy/gtk] / gtk / gtkvpaned.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "gtkvpaned.h"
28 #include "gtkmain.h"
29 #include "gtksignal.h"
30
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,
38                                          GdkRectangle *area);
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);
46
47 GtkType
48 gtk_vpaned_get_type (void)
49 {
50   static GtkType vpaned_type = 0;
51
52   if (!vpaned_type)
53     {
54       static const GtkTypeInfo vpaned_info =
55       {
56         "GtkVPaned",
57         sizeof (GtkVPaned),
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,
64       };
65
66       vpaned_type = gtk_type_unique (gtk_paned_get_type (), &vpaned_info);
67     }
68
69   return vpaned_type;
70 }
71
72 static void
73 gtk_vpaned_class_init (GtkVPanedClass *class)
74 {
75   GtkWidgetClass *widget_class;
76
77   widget_class = (GtkWidgetClass*) class;
78
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;
85 }
86
87 static void
88 gtk_vpaned_init (GtkVPaned *vpaned)
89 {
90 }
91
92 GtkWidget*
93 gtk_vpaned_new (void)
94 {
95   GtkVPaned *vpaned;
96
97   vpaned = gtk_type_new (gtk_vpaned_get_type ());
98
99   return GTK_WIDGET (vpaned);
100 }
101
102 static void
103 gtk_vpaned_size_request (GtkWidget      *widget,
104                          GtkRequisition *requisition)
105 {
106   GtkPaned *paned;
107   GtkRequisition child_requisition;
108
109   g_return_if_fail (widget != NULL);
110   g_return_if_fail (GTK_IS_VPANED (widget));
111   g_return_if_fail (requisition != NULL);
112
113   paned = GTK_PANED (widget);
114   requisition->width = 0;
115   requisition->height = 0;
116
117   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
118     {
119       gtk_widget_size_request (paned->child1, &child_requisition);
120
121       requisition->height = child_requisition.height;
122       requisition->width = child_requisition.width;
123     }
124
125   if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
126     {
127       gtk_widget_size_request (paned->child2, &child_requisition);
128
129       requisition->width = MAX (requisition->width, child_requisition.width);
130       requisition->height += child_requisition.height;
131     }
132
133   requisition->height += GTK_CONTAINER (paned)->border_width * 2 + paned->gutter_size;
134   requisition->width += GTK_CONTAINER (paned)->border_width * 2;
135 }
136
137 static void
138 gtk_vpaned_size_allocate (GtkWidget     *widget,
139                           GtkAllocation *allocation)
140 {
141   GtkPaned *paned;
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;
148
149   g_return_if_fail (widget != NULL);
150   g_return_if_fail (GTK_IS_VPANED (widget));
151   g_return_if_fail (allocation != NULL);
152
153   widget->allocation = *allocation;
154
155   paned = GTK_PANED (widget);
156   border_width = GTK_CONTAINER (widget)->border_width;
157
158   if (paned->child1)
159     gtk_widget_get_child_requisition (paned->child1, &child1_requisition);
160   else
161     child1_requisition.height = 0;
162
163   if (paned->child2)
164     gtk_widget_get_child_requisition (paned->child2, &child2_requisition);
165   else
166     child2_requisition.height = 0;
167     
168   gtk_paned_compute_position (paned,
169                               widget->allocation.height
170                                 - paned->gutter_size
171                                 - 2 * border_width,
172                               child1_requisition.height,
173                               child2_requisition.height);
174
175   /* Move the handle before the children so we don't get extra expose events */
176
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;
179
180   if (GTK_WIDGET_REALIZED (widget))
181     {
182       gdk_window_move_resize (widget->window,
183                               allocation->x, allocation->y,
184                               allocation->width, allocation->height);
185       
186       gdk_window_move (paned->handle, paned->handle_xpos, paned->handle_ypos);
187     }
188
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;
193   
194   old_groove_rectangle = paned->groove_rectangle;
195
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;
201   
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)))
207     {
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);
218     }
219   
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);
223   
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)
229     {
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);      
233     }
234   else
235     {
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);
240     }
241 }
242
243 static void
244 gtk_vpaned_draw (GtkWidget    *widget,
245                 GdkRectangle *area)
246 {
247   GtkPaned *paned;
248   GdkRectangle child_area;
249   guint16 border_width;
250
251   g_return_if_fail (widget != NULL);
252   g_return_if_fail (GTK_IS_PANED (widget));
253
254   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
255     {
256       paned = GTK_PANED (widget);
257       border_width = GTK_CONTAINER (paned)->border_width;
258
259       gdk_window_clear_area (widget->window,
260                              area->x, area->y, area->width, area->height);
261       if (paned->child1 &&
262           gtk_widget_intersect (paned->child1, area, &child_area))
263         gtk_widget_draw (paned->child1, &child_area);
264       if (paned->child2 &&
265           gtk_widget_intersect (paned->child2, area, &child_area))
266         gtk_widget_draw (paned->child2, &child_area);
267
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);
272     }
273 }
274
275 static void
276 gtk_vpaned_xor_line (GtkPaned *paned)
277 {
278   GtkWidget *widget;
279   GdkGCValues values;
280   guint16 ypos;
281
282   widget = GTK_WIDGET(paned);
283
284   if (!paned->xor_gc)
285     {
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,
290                                               &values,
291                                               GDK_GC_FOREGROUND |
292                                               GDK_GC_FUNCTION |
293                                               GDK_GC_SUBWINDOW);
294     }
295
296   ypos = paned->child1_size
297     + GTK_CONTAINER (paned)->border_width + paned->gutter_size / 2;
298
299   gdk_draw_line (widget->window, paned->xor_gc,
300                  0,
301                  ypos,
302                  widget->allocation.width - 1,
303                  ypos);
304 }
305
306 static gint
307 gtk_vpaned_button_press (GtkWidget *widget, GdkEventButton *event)
308 {
309   GtkPaned *paned;
310
311   g_return_val_if_fail (widget != NULL,FALSE);
312   g_return_val_if_fail (GTK_IS_PANED (widget),FALSE);
313
314   paned = GTK_PANED (widget);
315
316   if (!paned->in_drag &&
317       (event->window == paned->handle) && (event->button == 1))
318     {
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);
332     }
333
334   return TRUE;
335 }
336
337 static gint
338 gtk_vpaned_button_release (GtkWidget *widget, GdkEventButton *event)
339 {
340   GtkPaned *paned;
341
342   g_return_val_if_fail (widget != NULL, FALSE);
343   g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
344
345   paned = GTK_PANED (widget);
346
347   if (paned->in_drag && (event->button == 1))
348     {
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));
354     }
355
356   return TRUE;
357 }
358
359 static gint
360 gtk_vpaned_motion (GtkWidget *widget, GdkEventMotion *event)
361 {
362   GtkPaned *paned;
363   gint y;
364
365   g_return_val_if_fail (widget != NULL, FALSE);
366   g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
367
368   if (event->is_hint || event->window != widget->window)
369       gtk_widget_get_pointer(widget, NULL, &y);
370   else
371       y = event->y;
372
373   paned = GTK_PANED (widget);
374
375   if (paned->in_drag)
376     {
377       gint size = y - GTK_CONTAINER (paned)->border_width - paned->gutter_size/2;
378       
379       gtk_vpaned_xor_line (paned);
380       paned->child1_size = CLAMP (size,
381                                   paned->min_position,
382                                   paned->max_position);
383       gtk_vpaned_xor_line (paned);
384     }
385
386   return TRUE;
387 }