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