]> Pileus Git - ~andy/gtk/blob - gtk/gtkhpaned.c
Don't assume any more that the gtkwidget.c code will queue a redraw on us
[~andy/gtk] / gtk / gtkhpaned.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 #include "gtkhpaned.h"
20 #include "gtkmain.h"
21 #include "gtksignal.h"
22
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,
30                                          GdkRectangle *area);
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);
38
39 guint
40 gtk_hpaned_get_type (void)
41 {
42   static guint hpaned_type = 0;
43
44   if (!hpaned_type)
45     {
46       static const GtkTypeInfo hpaned_info =
47       {
48         "GtkHPaned",
49         sizeof (GtkHPaned),
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,
56       };
57
58       hpaned_type = gtk_type_unique (gtk_paned_get_type (), &hpaned_info);
59     }
60
61   return hpaned_type;
62 }
63
64 static void
65 gtk_hpaned_class_init (GtkHPanedClass *class)
66 {
67   GtkWidgetClass *widget_class;
68
69   widget_class = (GtkWidgetClass*) class;
70
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;
77 }
78
79 static void
80 gtk_hpaned_init (GtkHPaned *hpaned)
81 {
82 }
83
84 GtkWidget*
85 gtk_hpaned_new (void)
86 {
87   GtkHPaned *hpaned;
88
89   hpaned = gtk_type_new (gtk_hpaned_get_type ());
90
91   return GTK_WIDGET (hpaned);
92 }
93
94 static void
95 gtk_hpaned_size_request (GtkWidget      *widget,
96                          GtkRequisition *requisition)
97 {
98   GtkPaned *paned;
99
100   g_return_if_fail (widget != NULL);
101   g_return_if_fail (GTK_IS_HPANED (widget));
102   g_return_if_fail (requisition != NULL);
103
104   paned = GTK_PANED (widget);
105   requisition->width = 0;
106   requisition->height = 0;
107
108   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
109     {
110       gtk_widget_size_request (paned->child1, &paned->child1->requisition);
111
112       requisition->height = paned->child1->requisition.height;
113       requisition->width = paned->child1->requisition.width;
114     }
115
116   if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
117     {
118       gtk_widget_size_request (paned->child2, &paned->child2->requisition);
119
120       requisition->height = MAX(requisition->height,
121                                 paned->child2->requisition.height);
122       requisition->width += paned->child2->requisition.width;
123     }
124
125   requisition->width += GTK_CONTAINER (paned)->border_width * 2 + paned->gutter_size;
126   requisition->height += GTK_CONTAINER (paned)->border_width * 2;
127 }
128
129 static void
130 gtk_hpaned_size_allocate (GtkWidget     *widget,
131                           GtkAllocation *allocation)
132 {
133   GtkPaned *paned;
134   GtkAllocation child1_allocation;
135   GtkAllocation child2_allocation;
136   GdkRectangle old_groove_rectangle;
137   guint16 border_width;
138
139   g_return_if_fail (widget != NULL);
140   g_return_if_fail (GTK_IS_HPANED (widget));
141   g_return_if_fail (allocation != NULL);
142
143   widget->allocation = *allocation;
144
145   paned = GTK_PANED (widget);
146   border_width = GTK_CONTAINER (paned)->border_width;
147
148   gtk_paned_compute_position (paned,
149                               widget->allocation.width
150                                 - paned->gutter_size
151                                 - 2 * border_width,
152                               paned->child1 ? paned->child1->requisition.width : 0,
153                               paned->child2 ? paned->child2->requisition.width : 0);
154   
155   /* Move the handle before the children so we don't get extra expose events */
156
157   paned->handle_xpos = paned->child1_size + border_width + paned->gutter_size / 2 - paned->handle_size / 2;
158   paned->handle_ypos = allocation->height - border_width - 2*paned->handle_size;
159
160   if (GTK_WIDGET_REALIZED (widget))
161     {
162       gdk_window_move_resize (widget->window,
163                               allocation->x, allocation->y,
164                               allocation->width, allocation->height);
165       
166       gdk_window_move (paned->handle, paned->handle_xpos, paned->handle_ypos);
167     }
168
169   child1_allocation.height = child2_allocation.height = MAX (1, (gint)allocation->height - border_width * 2);
170   child1_allocation.width = paned->child1_size;
171   child1_allocation.x = border_width;
172   child1_allocation.y = child2_allocation.y = border_width;
173   
174   old_groove_rectangle = paned->groove_rectangle;
175
176   paned->groove_rectangle.x = child1_allocation.x 
177     + child1_allocation.width + paned->gutter_size / 2 - 1;
178   paned->groove_rectangle.y = 0;
179   paned->groove_rectangle.width = 2;
180   paned->groove_rectangle.height = allocation->height;
181       
182   if (GTK_WIDGET_DRAWABLE (widget) &&
183       ((paned->groove_rectangle.x != old_groove_rectangle.x) ||
184        (paned->groove_rectangle.y != old_groove_rectangle.y) ||
185        (paned->groove_rectangle.width != old_groove_rectangle.width) ||
186        (paned->groove_rectangle.height != old_groove_rectangle.height)))
187     {
188       gtk_widget_queue_clear_area (widget,
189                                    old_groove_rectangle.x,
190                                    old_groove_rectangle.y,
191                                    old_groove_rectangle.width,
192                                    old_groove_rectangle.height);
193       gtk_widget_queue_draw_area (widget,
194                                   paned->groove_rectangle.x,
195                                   paned->groove_rectangle.y,
196                                   paned->groove_rectangle.width,
197                                   paned->groove_rectangle.height);
198     }
199   
200   child2_allocation.x = paned->groove_rectangle.x + paned->gutter_size / 2 + 1;
201   child2_allocation.width = MAX (1, (gint)allocation->width
202     - child2_allocation.x - border_width);
203   
204   /* Now allocate the childen, making sure, when resizing not to
205    * overlap the windows */
206   if (GTK_WIDGET_MAPPED(widget) &&
207       paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
208       paned->child1->allocation.width < child1_allocation.width)
209     {
210       if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
211         gtk_widget_size_allocate (paned->child2, &child2_allocation);
212       gtk_widget_size_allocate (paned->child1, &child1_allocation);      
213     }
214   else
215     {
216       if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
217         gtk_widget_size_allocate (paned->child1, &child1_allocation);
218       if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
219         gtk_widget_size_allocate (paned->child2, &child2_allocation);
220     }
221 }
222
223 static void
224 gtk_hpaned_draw (GtkWidget    *widget,
225                 GdkRectangle *area)
226 {
227   GtkPaned *paned;
228   GdkRectangle child_area;
229   guint16 border_width;
230
231   g_return_if_fail (widget != NULL);
232   g_return_if_fail (GTK_IS_PANED (widget));
233
234   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
235     {
236       paned = GTK_PANED (widget);
237       border_width = GTK_CONTAINER (paned)->border_width;
238
239       gdk_window_clear_area (widget->window,
240                              area->x, area->y, area->width, area->height);
241       if (paned->child1 &&
242           gtk_widget_intersect (paned->child1, area, &child_area))
243         gtk_widget_draw (paned->child1, &child_area);
244       if (paned->child2 &&
245           gtk_widget_intersect (paned->child2, area, &child_area))
246         gtk_widget_draw (paned->child2, &child_area);
247
248       gtk_paint_vline(widget->style, widget->window, GTK_STATE_NORMAL,
249                       area, widget, "hpaned", 
250                       0, widget->allocation.height - 1,
251                       border_width + paned->child1_size + paned->gutter_size / 2 - 1);
252     }
253 }
254
255 static void
256 gtk_hpaned_xor_line (GtkPaned *paned)
257 {
258   GtkWidget *widget;
259   GdkGCValues values;
260   guint16 xpos;
261
262   widget = GTK_WIDGET(paned);
263
264   if (!paned->xor_gc)
265     {
266       values.foreground = widget->style->white;
267       values.function = GDK_XOR;
268       values.subwindow_mode = GDK_INCLUDE_INFERIORS;
269       paned->xor_gc = gdk_gc_new_with_values (widget->window,
270                                               &values,
271                                               GDK_GC_FOREGROUND |
272                                               GDK_GC_FUNCTION |
273                                               GDK_GC_SUBWINDOW);
274     }
275
276   xpos = paned->child1_size
277     + GTK_CONTAINER(paned)->border_width + paned->gutter_size / 2;
278
279   gdk_draw_line (widget->window, paned->xor_gc,
280                  xpos,
281                  0,
282                  xpos,
283                  widget->allocation.height - 1);
284 }
285
286 static gint
287 gtk_hpaned_button_press (GtkWidget *widget, GdkEventButton *event)
288 {
289   GtkPaned *paned;
290
291   g_return_val_if_fail (widget != NULL,FALSE);
292   g_return_val_if_fail (GTK_IS_PANED (widget),FALSE);
293
294   paned = GTK_PANED (widget);
295
296   if (!paned->in_drag &&
297       (event->window == paned->handle) && (event->button == 1))
298     {
299       paned->in_drag = TRUE;
300       /* We need a server grab here, not gtk_grab_add(), since
301        * we don't want to pass events on to the widget's children */
302       gdk_pointer_grab (paned->handle, FALSE,
303                         GDK_POINTER_MOTION_HINT_MASK 
304                         | GDK_BUTTON1_MOTION_MASK 
305                         | GDK_BUTTON_RELEASE_MASK,
306                         NULL, NULL, event->time);
307       paned->child1_size += event->x - paned->handle_size / 2;
308       paned->child1_size = CLAMP (paned->child1_size, 0,
309                                   widget->allocation.width - paned->gutter_size
310                                   - 2 * GTK_CONTAINER (paned)->border_width);
311       gtk_hpaned_xor_line (paned);
312     }
313
314   return TRUE;
315 }
316
317 static gint
318 gtk_hpaned_button_release (GtkWidget *widget, GdkEventButton *event)
319 {
320   GtkPaned *paned;
321
322   g_return_val_if_fail (widget != NULL,FALSE);
323   g_return_val_if_fail (GTK_IS_PANED (widget),FALSE);
324
325   paned = GTK_PANED (widget);
326
327   if (paned->in_drag && (event->button == 1))
328     {
329       gtk_hpaned_xor_line (paned);
330       paned->in_drag = FALSE;
331       paned->position_set = TRUE;
332       gdk_pointer_ungrab (event->time);
333       gtk_widget_queue_resize (GTK_WIDGET (paned));
334     }
335
336   return TRUE;
337 }
338
339 static gint
340 gtk_hpaned_motion (GtkWidget *widget, GdkEventMotion *event)
341 {
342   GtkPaned *paned;
343   gint x;
344
345   g_return_val_if_fail (widget != NULL, FALSE);
346   g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
347
348   paned = GTK_PANED (widget);
349
350   if (event->is_hint || event->window != widget->window)
351     gtk_widget_get_pointer(widget, &x, NULL);
352   else
353     x = event->x;
354
355   if (paned->in_drag)
356     {
357       gint size = x - GTK_CONTAINER (paned)->border_width - paned->gutter_size/2;
358       
359       gtk_hpaned_xor_line (paned);
360       paned->child1_size = CLAMP (size,
361                                   paned->min_position,
362                                   paned->max_position);
363       gtk_hpaned_xor_line (paned);
364     }
365
366   return TRUE;
367 }