]> Pileus Git - ~andy/gtk/blob - gtk/gtkvpaned.c
Opaque resizing + prelighting for paned widget. move reszing logic to
[~andy/gtk] / gtk / gtkvpaned.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 "gtkvpaned.h"
28
29 static void     gtk_vpaned_class_init     (GtkVPanedClass *klass);
30 static void     gtk_vpaned_init           (GtkVPaned      *vpaned);
31 static void     gtk_vpaned_size_request   (GtkWidget      *widget,
32                                            GtkRequisition *requisition);
33 static void     gtk_vpaned_size_allocate  (GtkWidget      *widget,
34                                            GtkAllocation  *allocation);
35
36 static gpointer parent_class;
37
38 GtkType
39 gtk_vpaned_get_type (void)
40 {
41   static GtkType vpaned_type = 0;
42
43   if (!vpaned_type)
44     {
45       static const GtkTypeInfo vpaned_info =
46       {
47         "GtkVPaned",
48         sizeof (GtkVPaned),
49         sizeof (GtkVPanedClass),
50         (GtkClassInitFunc) gtk_vpaned_class_init,
51         (GtkObjectInitFunc) gtk_vpaned_init,
52         /* reserved_1 */ NULL,
53         /* reserved_2 */ NULL,
54         (GtkClassInitFunc) NULL,
55       };
56
57       vpaned_type = gtk_type_unique (GTK_TYPE_PANED, &vpaned_info);
58     }
59
60   return vpaned_type;
61 }
62
63 static void
64 gtk_vpaned_class_init (GtkVPanedClass *class)
65 {
66   GtkWidgetClass *widget_class;
67
68   parent_class = gtk_type_class (GTK_TYPE_PANED);
69   
70   widget_class = (GtkWidgetClass *) class;
71
72   widget_class->size_request = gtk_vpaned_size_request;
73   widget_class->size_allocate = gtk_vpaned_size_allocate;
74 }
75
76 static void
77 gtk_vpaned_init (GtkVPaned *vpaned)
78 {
79   GtkPaned *paned;
80
81   g_return_if_fail (GTK_IS_PANED (vpaned));
82
83   paned = GTK_PANED (vpaned);
84
85   paned->cursor_type = GDK_SB_V_DOUBLE_ARROW;
86   paned->orientation = GTK_ORIENTATION_HORIZONTAL;
87 }
88
89 GtkWidget *
90 gtk_vpaned_new (void)
91 {
92   GtkVPaned *vpaned;
93
94   vpaned = gtk_type_new (GTK_TYPE_VPANED);
95
96   return GTK_WIDGET (vpaned);
97 }
98
99 static void
100 gtk_vpaned_size_request (GtkWidget      *widget,
101                          GtkRequisition *requisition)
102 {
103   GtkPaned *paned = GTK_PANED (widget);
104   GtkRequisition child_requisition;
105
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->width = MAX (requisition->width, child_requisition.width);
122       requisition->height += child_requisition.height;
123     }
124
125   requisition->height += GTK_CONTAINER (paned)->border_width * 2;
126   requisition->width += GTK_CONTAINER (paned)->border_width * 2;
127   
128   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
129       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
130     {
131       gint handle_size;
132
133       gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
134       requisition->height += handle_size;
135     }
136 }
137
138 static void
139 gtk_vpaned_size_allocate (GtkWidget     *widget,
140                           GtkAllocation *allocation)
141 {
142   GtkPaned *paned = GTK_PANED (widget);
143   gint border_width = GTK_CONTAINER (paned)->border_width;
144
145   widget->allocation = *allocation;
146
147   if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1) &&
148       paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
149     {
150       GtkRequisition child1_requisition;
151       GtkRequisition child2_requisition;
152       GtkAllocation child1_allocation;
153       GtkAllocation child2_allocation;
154       gint handle_size;
155       
156       gtk_widget_style_get (widget, "handle_size", &handle_size, NULL);
157
158       gtk_widget_get_child_requisition (paned->child1, &child1_requisition);
159       gtk_widget_get_child_requisition (paned->child2, &child2_requisition);
160     
161       gtk_paned_compute_position (paned,
162                                   MAX (1, widget->allocation.height
163                                        - handle_size
164                                        - 2 * border_width),
165                                   child1_requisition.height,
166                                   child2_requisition.height);
167
168       paned->handle_pos.x = widget->allocation.x + border_width;
169       paned->handle_pos.y = widget->allocation.y + paned->child1_size + border_width;
170       paned->handle_pos.width = MAX (1, (gint) widget->allocation.width - 2 * border_width);
171       paned->handle_pos.height = handle_size;
172       
173       if (GTK_WIDGET_REALIZED(widget))
174         {
175           gdk_window_show (paned->handle);
176           gdk_window_move_resize (paned->handle,
177                                   paned->handle_pos.x,
178                                   paned->handle_pos.y,
179                                   paned->handle_pos.width,
180                                   handle_size);
181         }
182
183       child1_allocation.width = child2_allocation.width = MAX (1, (gint) allocation->width - border_width * 2);
184       child1_allocation.height = paned->child1_size;
185       child1_allocation.x = child2_allocation.x = widget->allocation.x + border_width;
186       child1_allocation.y = widget->allocation.y + border_width;
187       
188       child2_allocation.y = child1_allocation.y + child1_allocation.height + paned->handle_pos.height;
189       child2_allocation.height = MAX (1, widget->allocation.y + widget->allocation.height - child2_allocation.y - border_width);
190       
191       /* Now allocate the childen, making sure, when resizing not to
192        * overlap the windows
193        */
194       if (GTK_WIDGET_MAPPED (widget) &&
195           paned->child1->allocation.height < child1_allocation.height)
196         {
197           gtk_widget_size_allocate (paned->child2, &child2_allocation);
198           gtk_widget_size_allocate (paned->child1, &child1_allocation);
199         }
200       else
201         {
202           gtk_widget_size_allocate (paned->child1, &child1_allocation);
203           gtk_widget_size_allocate (paned->child2, &child2_allocation);
204         }
205     }
206   else
207     {
208       GtkAllocation child_allocation;
209
210       if (GTK_WIDGET_REALIZED (widget))      
211         gdk_window_hide (paned->handle);
212           
213       child_allocation.x = widget->allocation.x + border_width;
214       child_allocation.y = widget->allocation.y + border_width;
215       child_allocation.width = MAX (1, allocation->width - 2 * border_width);
216       child_allocation.height = MAX (1, allocation->height - 2 * border_width);
217       
218       if (paned->child1 && GTK_WIDGET_VISIBLE (paned->child1))
219         gtk_widget_size_allocate (paned->child1, &child_allocation);
220       else if (paned->child2 && GTK_WIDGET_VISIBLE (paned->child2))
221         gtk_widget_size_allocate (paned->child2, &child_allocation);
222     }
223 }