]> Pileus Git - ~andy/gtk/blob - gtk/gtkhpaned.c
Use the correct screen for getting the height. (Fix from Stephen Browne,
[~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
36 static gpointer parent_class;
37
38 GType
39 gtk_hpaned_get_type (void)
40 {
41   static GType hpaned_type = 0;
42
43   if (!hpaned_type)
44     {
45       static const GTypeInfo hpaned_info =
46       {
47         sizeof (GtkHPanedClass),
48         NULL,           /* base_init */
49         NULL,           /* base_finalize */
50         (GClassInitFunc) gtk_hpaned_class_init,
51         NULL,           /* class_finalize */
52         NULL,           /* class_data */
53         sizeof (GtkHPaned),
54         0,              /* n_preallocs */
55         (GInstanceInitFunc) gtk_hpaned_init,
56       };
57
58       hpaned_type = g_type_register_static (GTK_TYPE_PANED, "GtkHPaned",
59                                             &hpaned_info, 0);
60     }
61
62   return hpaned_type;
63 }
64
65 static void
66 gtk_hpaned_class_init (GtkHPanedClass *class)
67 {
68   GtkWidgetClass *widget_class;
69
70   parent_class = g_type_class_peek_parent (class);
71   
72   widget_class = (GtkWidgetClass *) class;
73
74   widget_class->size_request = gtk_hpaned_size_request;
75   widget_class->size_allocate = gtk_hpaned_size_allocate;
76 }
77
78 static void
79 gtk_hpaned_init (GtkHPaned *hpaned)
80 {
81   GtkPaned *paned;
82
83   g_return_if_fail (GTK_IS_PANED (hpaned));
84
85   paned = GTK_PANED (hpaned);
86   
87   paned->cursor_type = GDK_SB_H_DOUBLE_ARROW;
88   paned->orientation = GTK_ORIENTATION_VERTICAL;
89 }
90
91 GtkWidget *
92 gtk_hpaned_new (void)
93 {
94   GtkHPaned *hpaned;
95
96   hpaned = g_object_new (GTK_TYPE_HPANED, NULL);
97
98   return GTK_WIDGET (hpaned);
99 }
100
101 static void
102 gtk_hpaned_size_request (GtkWidget      *widget,
103                          GtkRequisition *requisition)
104 {
105   GtkPaned *paned = GTK_PANED (widget);
106   GtkRequisition child_requisition;
107
108   requisition->width = 0;
109   requisition->height = 0;
110
111   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
112     {
113       gtk_widget_size_request (paned->child1, &child_requisition);
114
115       requisition->height = child_requisition.height;
116       requisition->width = child_requisition.width;
117     }
118
119   if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
120     {
121       gtk_widget_size_request (paned->child2, &child_requisition);
122
123       requisition->height = MAX(requisition->height, child_requisition.height);
124       requisition->width += child_requisition.width;
125     }
126
127   requisition->width += GTK_CONTAINER (paned)->border_width * 2;
128   requisition->height += GTK_CONTAINER (paned)->border_width * 2;
129
130   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
131       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
132     {
133       gint handle_size;
134       
135       gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
136       requisition->width += handle_size;
137     }
138 }
139
140 static void
141 gtk_hpaned_size_allocate (GtkWidget     *widget,
142                           GtkAllocation *allocation)
143 {
144   GtkPaned *paned = GTK_PANED (widget);
145   gint border_width = GTK_CONTAINER (paned)->border_width;
146
147   widget->allocation = *allocation;
148
149   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
150       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
151     {
152       GtkAllocation child1_allocation;
153       GtkAllocation child2_allocation;
154       GtkRequisition child1_requisition;
155       GtkRequisition child2_requisition;
156       gint handle_size;
157       
158       gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
159
160       gtk_widget_get_child_requisition (paned->child1, &child1_requisition);
161       gtk_widget_get_child_requisition (paned->child2, &child2_requisition);
162       
163       gtk_paned_compute_position (paned,
164                                   MAX (1, widget->allocation.width
165                                        - handle_size
166                                        - 2 * border_width),
167                                   child1_requisition.width,
168                                   child2_requisition.width);
169       
170       paned->handle_pos.x = widget->allocation.x + paned->child1_size + border_width;
171       paned->handle_pos.y = widget->allocation.y + border_width;
172       paned->handle_pos.width = handle_size;
173       paned->handle_pos.height = MAX (1, widget->allocation.height - 2 * border_width);
174       
175       if (GTK_WIDGET_REALIZED (widget))
176         {
177           if (GTK_WIDGET_MAPPED (widget))
178             gdk_window_show (paned->handle);
179           gdk_window_move_resize (paned->handle,
180                                   paned->handle_pos.x,
181                                   paned->handle_pos.y,
182                                   handle_size,
183                                   paned->handle_pos.height);
184         }
185       
186       child1_allocation.height = child2_allocation.height = MAX (1, (gint) allocation->height - border_width * 2);
187       child1_allocation.width = MAX (1, paned->child1_size);
188       child1_allocation.x = widget->allocation.x + border_width;
189       child1_allocation.y = child2_allocation.y = widget->allocation.y + border_width;
190       
191       child2_allocation.x = child1_allocation.x + paned->child1_size + paned->handle_pos.width;
192       child2_allocation.width = MAX (1, widget->allocation.x + widget->allocation.width - child2_allocation.x - border_width);
193
194       /* Now allocate the childen, making sure, when resizing not to
195        * overlap the windows
196        */
197       if (GTK_WIDGET_MAPPED (widget) &&
198           paned->child1->allocation.width < child1_allocation.width)
199         {
200           gtk_widget_size_allocate (paned->child2, &child2_allocation);
201           gtk_widget_size_allocate (paned->child1, &child1_allocation);
202         }
203       else
204         {
205           gtk_widget_size_allocate (paned->child1, &child1_allocation);
206           gtk_widget_size_allocate (paned->child2, &child2_allocation);
207         }
208     }
209   else
210     {
211       GtkAllocation child_allocation;
212
213       if (GTK_WIDGET_REALIZED (widget))      
214         gdk_window_hide (paned->handle);
215
216       if (paned->child1)
217         gtk_widget_set_child_visible (paned->child1, TRUE);
218       if (paned->child2)
219         gtk_widget_set_child_visible (paned->child2, TRUE);
220
221       child_allocation.x = widget->allocation.x + border_width;
222       child_allocation.y = widget->allocation.y + border_width;
223       child_allocation.width = MAX (1, allocation->width - 2 * border_width);
224       child_allocation.height = MAX (1, allocation->height - 2 * border_width);
225       
226       if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
227         gtk_widget_size_allocate (paned->child1, &child_allocation);
228       else if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
229         gtk_widget_size_allocate (paned->child2, &child_allocation);
230     }
231 }