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