]> Pileus Git - ~andy/gtk/blob - gtk/gtkalignment.c
b562cff77084ac208503d2b9df55ab1530bb7511
[~andy/gtk] / gtk / gtkalignment.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 "gtkalignment.h"
19
20
21 static void gtk_alignment_class_init    (GtkAlignmentClass *klass);
22 static void gtk_alignment_init          (GtkAlignment      *alignment);
23 static void gtk_alignment_size_request  (GtkWidget         *widget,
24                                          GtkRequisition    *requisition);
25 static void gtk_alignment_size_allocate (GtkWidget         *widget,
26                                          GtkAllocation     *allocation);
27
28
29 guint
30 gtk_alignment_get_type ()
31 {
32   static guint alignment_type = 0;
33
34   if (!alignment_type)
35     {
36       GtkTypeInfo alignment_info =
37       {
38         "GtkAlignment",
39         sizeof (GtkAlignment),
40         sizeof (GtkAlignmentClass),
41         (GtkClassInitFunc) gtk_alignment_class_init,
42         (GtkObjectInitFunc) gtk_alignment_init,
43         (GtkArgFunc) NULL,
44       };
45
46       alignment_type = gtk_type_unique (gtk_bin_get_type (), &alignment_info);
47     }
48
49   return alignment_type;
50 }
51
52 static void
53 gtk_alignment_class_init (GtkAlignmentClass *class)
54 {
55   GtkWidgetClass *widget_class;
56
57   widget_class = (GtkWidgetClass*) class;
58
59   widget_class->size_request = gtk_alignment_size_request;
60   widget_class->size_allocate = gtk_alignment_size_allocate;
61 }
62
63 static void
64 gtk_alignment_init (GtkAlignment *alignment)
65 {
66   GTK_WIDGET_SET_FLAGS (alignment, GTK_NO_WINDOW | GTK_BASIC);
67
68   alignment->xalign = 0.5;
69   alignment->yalign = 0.5;
70   alignment->xscale = 1.0;
71   alignment->yscale = 1.0;
72 }
73
74 GtkWidget*
75 gtk_alignment_new (gfloat xalign,
76                    gfloat yalign,
77                    gfloat xscale,
78                    gfloat yscale)
79 {
80   GtkAlignment *alignment;
81
82   alignment = gtk_type_new (gtk_alignment_get_type ());
83
84   alignment->xalign = CLAMP (xalign, 0.0, 1.0);
85   alignment->yalign = CLAMP (yalign, 0.0, 1.0);
86   alignment->xscale = CLAMP (xscale, 0.0, 1.0);
87   alignment->yscale = CLAMP (yscale, 0.0, 1.0);
88
89   return GTK_WIDGET (alignment);
90 }
91
92 void
93 gtk_alignment_set (GtkAlignment *alignment,
94                    gfloat        xalign,
95                    gfloat        yalign,
96                    gfloat        xscale,
97                    gfloat        yscale)
98 {
99   g_return_if_fail (alignment != NULL);
100   g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
101
102   xalign = CLAMP (xalign, 0.0, 1.0);
103   yalign = CLAMP (yalign, 0.0, 1.0);
104   xscale = CLAMP (xscale, 0.0, 1.0);
105   yscale = CLAMP (yscale, 0.0, 1.0);
106
107   if ((alignment->xalign != xalign) ||
108       (alignment->yalign != yalign) ||
109       (alignment->xscale != xscale) ||
110       (alignment->yscale != yscale))
111     {
112       alignment->xalign = xalign;
113       alignment->yalign = yalign;
114       alignment->xscale = xscale;
115       alignment->yscale = yscale;
116
117       gtk_widget_size_allocate (GTK_WIDGET (alignment), &(GTK_WIDGET (alignment)->allocation));
118       gtk_widget_queue_draw (GTK_WIDGET (alignment));
119     }
120 }
121
122
123 static void
124 gtk_alignment_size_request (GtkWidget      *widget,
125                             GtkRequisition *requisition)
126 {
127   GtkAlignment *alignment;
128   GtkBin *bin;
129
130   g_return_if_fail (widget != NULL);
131   g_return_if_fail (GTK_IS_ALIGNMENT (widget));
132   g_return_if_fail (requisition != NULL);
133
134   alignment = GTK_ALIGNMENT (widget);
135   bin = GTK_BIN (widget);
136
137   requisition->width = GTK_CONTAINER (widget)->border_width * 2;
138   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
139
140   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
141     {
142       gtk_widget_size_request (bin->child, &bin->child->requisition);
143
144       requisition->width += bin->child->requisition.width;
145       requisition->height += bin->child->requisition.height;
146     }
147 }
148
149 static void
150 gtk_alignment_size_allocate (GtkWidget     *widget,
151                              GtkAllocation *allocation)
152 {
153   GtkAlignment *alignment;
154   GtkBin *bin;
155   GtkAllocation child_allocation;
156   gint width, height;
157   gint x, y;
158
159   g_return_if_fail (widget != NULL);
160   g_return_if_fail (GTK_IS_ALIGNMENT (widget));
161   g_return_if_fail (allocation != NULL);
162
163   widget->allocation = *allocation;
164   alignment = GTK_ALIGNMENT (widget);
165   bin = GTK_BIN (widget);
166
167   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
168     {
169       x = GTK_CONTAINER (alignment)->border_width;
170       y = GTK_CONTAINER (alignment)->border_width;
171       width = allocation->width - 2 * x;
172       height = allocation->height - 2 * y;
173
174       if (width > bin->child->requisition.width)
175         child_allocation.width = (bin->child->requisition.width *
176                                   (1.0 - alignment->xscale) +
177                                   width * alignment->xscale);
178       else
179         child_allocation.width = width;
180
181       if (height > bin->child->requisition.height)
182         child_allocation.height = (bin->child->requisition.height *
183                                   (1.0 - alignment->yscale) +
184                                   height * alignment->yscale);
185       else
186         child_allocation.height = height;
187
188       child_allocation.x = alignment->xalign * (width - child_allocation.width) + allocation->x + x;
189       child_allocation.y = alignment->yalign * (height - child_allocation.height) + allocation->y + y;
190
191       gtk_widget_size_allocate (bin->child, &child_allocation);
192     }
193 }