]> Pileus Git - ~andy/gtk/blob - gtk/gtkhpaned.c
fix allocation to be relative to paned->allocation, now that this is a
[~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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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-2000.  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 "gtkhpaned.h"
28
29 static void     gtk_hpaned_class_init     (GtkHPanedClass *klass);
30 static void     gtk_hpaned_init           (GtkHPaned      *hpaned);
31 static void     gtk_hpaned_size_request   (GtkWidget      *widget,
32                                            GtkRequisition *requisition);
33 static void     gtk_hpaned_size_allocate  (GtkWidget      *widget,
34                                            GtkAllocation  *allocation);
35 static void     gtk_hpaned_xor_line       (GtkPaned       *paned);
36 static gboolean gtk_hpaned_button_press   (GtkWidget      *widget,
37                                            GdkEventButton *event);
38 static gboolean gtk_hpaned_button_release (GtkWidget      *widget,
39                                            GdkEventButton *event);
40 static gboolean gtk_hpaned_motion         (GtkWidget      *widget,
41                                            GdkEventMotion *event);
42
43 static gpointer parent_class;
44
45 GtkType
46 gtk_hpaned_get_type (void)
47 {
48   static GtkType hpaned_type = 0;
49
50   if (!hpaned_type)
51     {
52       static const GtkTypeInfo hpaned_info =
53       {
54         "GtkHPaned",
55         sizeof (GtkHPaned),
56         sizeof (GtkHPanedClass),
57         (GtkClassInitFunc) gtk_hpaned_class_init,
58         (GtkObjectInitFunc) gtk_hpaned_init,
59         /* reserved_1 */ NULL,
60         /* reserved_2 */ NULL,
61         (GtkClassInitFunc) NULL,
62       };
63
64       hpaned_type = gtk_type_unique (GTK_TYPE_PANED, &hpaned_info);
65     }
66
67   return hpaned_type;
68 }
69
70 static void
71 gtk_hpaned_class_init (GtkHPanedClass *class)
72 {
73   GtkWidgetClass *widget_class;
74
75   parent_class = gtk_type_class (GTK_TYPE_PANED);
76   
77   widget_class = (GtkWidgetClass *) class;
78
79   widget_class->size_request = gtk_hpaned_size_request;
80   widget_class->size_allocate = gtk_hpaned_size_allocate;
81   widget_class->button_press_event = gtk_hpaned_button_press;
82   widget_class->button_release_event = gtk_hpaned_button_release;
83   widget_class->motion_notify_event = gtk_hpaned_motion;
84 }
85
86 static void
87 gtk_hpaned_init (GtkHPaned *hpaned)
88 {
89   GtkPaned *paned;
90
91   g_return_if_fail (GTK_IS_PANED (hpaned));
92
93   paned = GTK_PANED (hpaned);
94   
95   paned->cursor_type = GDK_SB_H_DOUBLE_ARROW;
96   paned->orientation = GTK_ORIENTATION_VERTICAL;
97 }
98
99 GtkWidget *
100 gtk_hpaned_new (void)
101 {
102   GtkHPaned *hpaned;
103
104   hpaned = gtk_type_new (GTK_TYPE_HPANED);
105
106   return GTK_WIDGET (hpaned);
107 }
108
109 static void
110 gtk_hpaned_size_request (GtkWidget      *widget,
111                          GtkRequisition *requisition)
112 {
113   GtkPaned *paned = GTK_PANED (widget);
114   GtkRequisition child_requisition;
115
116   requisition->width = 0;
117   requisition->height = 0;
118
119   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
120     {
121       gtk_widget_size_request (paned->child1, &child_requisition);
122
123       requisition->height = child_requisition.height;
124       requisition->width = child_requisition.width;
125     }
126
127   if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
128     {
129       gtk_widget_size_request (paned->child2, &child_requisition);
130
131       requisition->height = MAX(requisition->height, child_requisition.height);
132       requisition->width += child_requisition.width;
133     }
134
135   requisition->width += GTK_CONTAINER (paned)->border_width * 2;
136   requisition->height += GTK_CONTAINER (paned)->border_width * 2;
137
138   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
139       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
140     {
141       gint handle_size;
142       
143       gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
144       requisition->width += handle_size;
145     }
146 }
147
148 static void
149 gtk_hpaned_size_allocate (GtkWidget     *widget,
150                           GtkAllocation *allocation)
151 {
152   GtkPaned *paned = GTK_PANED (widget);
153   gint border_width = GTK_CONTAINER (paned)->border_width;
154
155   widget->allocation = *allocation;
156
157   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
158       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
159     {
160       GtkAllocation child1_allocation;
161       GtkAllocation child2_allocation;
162       GtkRequisition child1_requisition;
163       GtkRequisition child2_requisition;
164       gint handle_size;
165       
166       gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
167
168       gtk_widget_get_child_requisition (paned->child1, &child1_requisition);
169       gtk_widget_get_child_requisition (paned->child2, &child2_requisition);
170       
171       gtk_paned_compute_position (paned,
172                                   MAX (1, widget->allocation.width
173                                        - handle_size
174                                        - 2 * border_width),
175                                   child1_requisition.width,
176                                   child2_requisition.width);
177       
178       paned->handle_pos.x = widget->allocation.x + paned->child1_size + border_width;
179       paned->handle_pos.y = widget->allocation.y + border_width;
180       paned->handle_pos.width = handle_size;
181       paned->handle_pos.height = MAX (1, widget->allocation.height - 2 * border_width);
182       
183       if (GTK_WIDGET_REALIZED (widget))
184         {
185           gdk_window_show (paned->handle);
186           gdk_window_move_resize (paned->handle,
187                                   paned->handle_pos.x,
188                                   paned->handle_pos.y,
189                                   handle_size,
190                                   paned->handle_pos.height);
191         }
192       
193       child1_allocation.height = child2_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
194       child1_allocation.width = paned->child1_size;
195       child1_allocation.x = widget->allocation.x + border_width;
196       child1_allocation.y = child2_allocation.y = widget->allocation.y + border_width;
197       
198       child2_allocation.x = child1_allocation.x + child1_allocation.width +  paned->handle_pos.width;
199       child2_allocation.width = MAX (1, (gint) allocation->width - child2_allocation.x - border_width);
200       
201       /* Now allocate the childen, making sure, when resizing not to
202        * overlap the windows
203        */
204       if (GTK_WIDGET_MAPPED (widget) &&
205           paned->child1->allocation.width < child1_allocation.width)
206         {
207           gtk_widget_size_allocate (paned->child2, &child2_allocation);
208           gtk_widget_size_allocate (paned->child1, &child1_allocation);
209         }
210       else
211         {
212           gtk_widget_size_allocate (paned->child1, &child1_allocation);
213           gtk_widget_size_allocate (paned->child2, &child2_allocation);
214         }
215     }
216   else
217     {
218       GtkAllocation child_allocation;
219
220       if (GTK_WIDGET_REALIZED (widget))      
221         gdk_window_hide (paned->handle);
222           
223       child_allocation.x = widget->allocation.x + border_width;
224       child_allocation.y = widget->allocation.y + border_width;
225       child_allocation.width = MAX (1, allocation->width - 2 * border_width);
226       child_allocation.height = MAX (1, allocation->height - 2 * border_width);
227       
228       if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
229         gtk_widget_size_allocate (paned->child1, &child_allocation);
230       else if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
231         gtk_widget_size_allocate (paned->child2, &child_allocation);
232     }
233 }
234
235 static void
236 gtk_hpaned_xor_line (GtkPaned *paned)
237 {
238   GtkWidget *widget;
239   GdkGCValues values;
240   guint16 xpos;
241   gint handle_size;
242
243   widget = GTK_WIDGET (paned);
244
245   gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
246
247   if (!paned->xor_gc)
248     {
249       values.function = GDK_INVERT;
250       values.subwindow_mode = GDK_INCLUDE_INFERIORS;
251       paned->xor_gc = gdk_gc_new_with_values (widget->window,
252                                               &values,
253                                               GDK_GC_FUNCTION | GDK_GC_SUBWINDOW);
254     }
255
256   gdk_gc_set_line_attributes (paned->xor_gc, 2, GDK_LINE_SOLID,
257                               GDK_CAP_NOT_LAST, GDK_JOIN_BEVEL);
258
259   xpos = widget->allocation.x + paned->child1_size
260     + GTK_CONTAINER (paned)->border_width + handle_size / 2;
261
262   gdk_draw_line (widget->window, paned->xor_gc,
263                  xpos,
264                  widget->allocation.y,
265                  xpos,
266                  widget->allocation.y + widget->allocation.height - 1);
267 }
268
269 static gboolean
270 gtk_hpaned_button_press (GtkWidget      *widget,
271                          GdkEventButton *event)
272 {
273   GtkPaned *paned = GTK_PANED (widget);
274   gint handle_size;
275
276   gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
277
278   if (!paned->in_drag &&
279       event->window == paned->handle && event->button == 1)
280     {
281       paned->in_drag = TRUE;
282       /* We need a server grab here, not gtk_grab_add(), since
283        * we don't want to pass events on to the widget's children */
284       gdk_pointer_grab(paned->handle, FALSE,
285                        GDK_POINTER_MOTION_HINT_MASK
286                        | GDK_BUTTON1_MOTION_MASK
287                        | GDK_BUTTON_RELEASE_MASK,
288                        NULL, NULL, event->time);
289       paned->child1_size += event->x - handle_size / 2;
290       paned->child1_size = CLAMP (paned->child1_size, 0,
291                                   widget->allocation.width
292                                   - handle_size
293                                   - 2 * GTK_CONTAINER (paned)->border_width);
294       gtk_hpaned_xor_line (paned);
295
296       return TRUE;
297     }
298
299   return FALSE;
300 }
301
302 static gboolean
303 gtk_hpaned_button_release (GtkWidget      *widget,
304                            GdkEventButton *event)
305 {
306   GtkPaned *paned = GTK_PANED (widget);
307   GObject *object = G_OBJECT (widget);
308
309   if (paned->in_drag && (event->button == 1))
310     {
311       gtk_hpaned_xor_line (paned);
312       paned->in_drag = FALSE;
313       paned->position_set = TRUE;
314       gdk_pointer_ungrab (event->time);
315       gtk_widget_queue_resize (GTK_WIDGET (paned));
316       g_object_freeze_notify (object);
317       g_object_notify (object, "position");
318       g_object_notify (object, "position_set");
319       g_object_thaw_notify (object);
320   
321       return TRUE;
322     }
323
324   return FALSE;
325 }
326
327 static gboolean
328 gtk_hpaned_motion (GtkWidget      *widget,
329                    GdkEventMotion *event)
330 {
331   GtkPaned *paned = GTK_PANED (widget);
332   gint x;
333   gint handle_size;
334
335   gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
336
337   gtk_widget_get_pointer (widget, &x, NULL);
338
339   if (paned->in_drag)
340     {
341       gint size = x - GTK_CONTAINER (paned)->border_width - handle_size / 2;
342
343       gtk_hpaned_xor_line (paned);
344       paned->child1_size = CLAMP (size, paned->min_position, paned->max_position);
345       gtk_hpaned_xor_line (paned);
346     }
347
348   return TRUE;
349 }