]> Pileus Git - ~andy/gtk/blob - gtk/gtkhpaned.c
Fixed some bugs with set_default_size.
[~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   GtkRequisition child_requisition;
100
101   g_return_if_fail (widget != NULL);
102   g_return_if_fail (GTK_IS_HPANED (widget));
103   g_return_if_fail (requisition != NULL);
104
105   paned = GTK_PANED (widget);
106   requisition->width = 0;
107   requisition->height = 0;
108
109   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
110     {
111       gtk_widget_size_request (paned->child1, &child_requisition);
112
113       requisition->height = child_requisition.height;
114       requisition->width = child_requisition.width;
115     }
116
117   if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
118     {
119       gtk_widget_size_request (paned->child2, &child_requisition);
120
121       requisition->height = MAX(requisition->height, child_requisition.height);
122       requisition->width += child_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   GtkRequisition child1_requisition;
135   GtkRequisition child2_requisition;
136   GtkAllocation child1_allocation;
137   GtkAllocation child2_allocation;
138   GdkRectangle old_groove_rectangle;
139   guint16 border_width;
140
141   g_return_if_fail (widget != NULL);
142   g_return_if_fail (GTK_IS_HPANED (widget));
143   g_return_if_fail (allocation != NULL);
144
145   widget->allocation = *allocation;
146
147   paned = GTK_PANED (widget);
148   border_width = GTK_CONTAINER (paned)->border_width;
149
150   if (paned->child1)
151     gtk_widget_get_child_requisition (paned->child1, &child1_requisition);
152   else
153     child1_requisition.width = 0;
154
155   if (paned->child2)
156     gtk_widget_get_child_requisition (paned->child2, &child2_requisition);
157   else
158     child2_requisition.width = 0;
159     
160   gtk_paned_compute_position (paned,
161                               widget->allocation.width
162                                 - paned->gutter_size
163                                 - 2 * border_width,
164                               child1_requisition.width,
165                               child2_requisition.width);
166   
167   /* Move the handle before the children so we don't get extra expose events */
168
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;
171
172   if (GTK_WIDGET_REALIZED (widget))
173     {
174       gdk_window_move_resize (widget->window,
175                               allocation->x, allocation->y,
176                               allocation->width, allocation->height);
177       
178       gdk_window_move (paned->handle, paned->handle_xpos, paned->handle_ypos);
179     }
180
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;
185   
186   old_groove_rectangle = paned->groove_rectangle;
187
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;
193       
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)))
199     {
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);
210     }
211   
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);
215   
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)
221     {
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);      
225     }
226   else
227     {
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);
232     }
233 }
234
235 static void
236 gtk_hpaned_draw (GtkWidget    *widget,
237                 GdkRectangle *area)
238 {
239   GtkPaned *paned;
240   GdkRectangle child_area;
241   guint16 border_width;
242
243   g_return_if_fail (widget != NULL);
244   g_return_if_fail (GTK_IS_PANED (widget));
245
246   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
247     {
248       paned = GTK_PANED (widget);
249       border_width = GTK_CONTAINER (paned)->border_width;
250
251       gdk_window_clear_area (widget->window,
252                              area->x, area->y, area->width, area->height);
253       if (paned->child1 &&
254           gtk_widget_intersect (paned->child1, area, &child_area))
255         gtk_widget_draw (paned->child1, &child_area);
256       if (paned->child2 &&
257           gtk_widget_intersect (paned->child2, area, &child_area))
258         gtk_widget_draw (paned->child2, &child_area);
259
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);
264     }
265 }
266
267 static void
268 gtk_hpaned_xor_line (GtkPaned *paned)
269 {
270   GtkWidget *widget;
271   GdkGCValues values;
272   guint16 xpos;
273
274   widget = GTK_WIDGET(paned);
275
276   if (!paned->xor_gc)
277     {
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,
282                                               &values,
283                                               GDK_GC_FOREGROUND |
284                                               GDK_GC_FUNCTION |
285                                               GDK_GC_SUBWINDOW);
286     }
287
288   xpos = paned->child1_size
289     + GTK_CONTAINER(paned)->border_width + paned->gutter_size / 2;
290
291   gdk_draw_line (widget->window, paned->xor_gc,
292                  xpos,
293                  0,
294                  xpos,
295                  widget->allocation.height - 1);
296 }
297
298 static gint
299 gtk_hpaned_button_press (GtkWidget *widget, GdkEventButton *event)
300 {
301   GtkPaned *paned;
302
303   g_return_val_if_fail (widget != NULL,FALSE);
304   g_return_val_if_fail (GTK_IS_PANED (widget),FALSE);
305
306   paned = GTK_PANED (widget);
307
308   if (!paned->in_drag &&
309       (event->window == paned->handle) && (event->button == 1))
310     {
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);
324     }
325
326   return TRUE;
327 }
328
329 static gint
330 gtk_hpaned_button_release (GtkWidget *widget, GdkEventButton *event)
331 {
332   GtkPaned *paned;
333
334   g_return_val_if_fail (widget != NULL,FALSE);
335   g_return_val_if_fail (GTK_IS_PANED (widget),FALSE);
336
337   paned = GTK_PANED (widget);
338
339   if (paned->in_drag && (event->button == 1))
340     {
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));
346     }
347
348   return TRUE;
349 }
350
351 static gint
352 gtk_hpaned_motion (GtkWidget *widget, GdkEventMotion *event)
353 {
354   GtkPaned *paned;
355   gint x;
356
357   g_return_val_if_fail (widget != NULL, FALSE);
358   g_return_val_if_fail (GTK_IS_PANED (widget), FALSE);
359
360   paned = GTK_PANED (widget);
361
362   if (event->is_hint || event->window != widget->window)
363     gtk_widget_get_pointer(widget, &x, NULL);
364   else
365     x = event->x;
366
367   if (paned->in_drag)
368     {
369       gint size = x - GTK_CONTAINER (paned)->border_width - paned->gutter_size/2;
370       
371       gtk_hpaned_xor_line (paned);
372       paned->child1_size = CLAMP (size,
373                                   paned->min_position,
374                                   paned->max_position);
375       gtk_hpaned_xor_line (paned);
376     }
377
378   return TRUE;
379 }