9 static void aspect_bin_size_request(GtkWidget *, GtkRequisition *);
10 static void aspect_bin_size_allocate(GtkWidget *, GtkAllocation *);
11 static void aspect_bin_add(GtkContainer *, GtkWidget *);
12 static void aspect_bin_remove(GtkContainer *, GtkWidget *);
13 static void aspect_bin_forall(GtkContainer *, gboolean, GtkCallback, gpointer);
14 static GType aspect_bin_child_type(GtkContainer *);
16 static void aspect_bin_get_property(GObject *, guint, GValue *, GParamSpec *);
17 static void aspect_bin_set_property(GObject *, guint, const GValue *,
20 G_DEFINE_TYPE(AspectBin, aspect_bin, GTK_TYPE_CONTAINER)
22 static void aspect_bin_init(AspectBin *abin)
24 GTK_WIDGET_SET_FLAGS(abin, GTK_NO_WINDOW);
30 abin->constrain = FALSE;
33 static void aspect_bin_class_init(AspectBinClass *class)
35 GObjectClass *object_class = G_OBJECT_CLASS(class);
36 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
37 GtkContainerClass *container_class = GTK_CONTAINER_CLASS(class);
39 widget_class->size_request = aspect_bin_size_request;
40 widget_class->size_allocate = aspect_bin_size_allocate;
42 container_class->add = aspect_bin_add;
43 container_class->remove = aspect_bin_remove;
44 container_class->forall = aspect_bin_forall;
45 container_class->child_type = aspect_bin_child_type;
47 object_class->set_property = aspect_bin_set_property;
48 object_class->get_property = aspect_bin_get_property;
50 g_object_class_install_property(object_class, PROP_CONSTRAIN,
51 g_param_spec_boolean("constrain",
53 "TRUE if side child should expand only as much as the"
59 GtkWidget *aspect_bin_new(void)
61 return GTK_WIDGET(g_object_new(ASPECT_BIN_TYPE, NULL));
64 static void aspect_bin_set_property(GObject *object,
69 AspectBin *abin = ASPECT_BIN(object);
73 abin->constrain = g_value_get_boolean(value);
74 gtk_widget_queue_resize(GTK_WIDGET(abin));
77 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
81 static void aspect_bin_get_property(GObject *object,
86 AspectBin *abin = ASPECT_BIN(object);
90 g_value_set_boolean(value, abin->constrain);
93 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
97 static void aspect_bin_add(GtkContainer *container, GtkWidget *widget)
100 g_return_if_fail(IS_ASPECT_BIN(container));
101 abin = ASPECT_BIN(container);
104 aspect_bin_set_body(abin, widget, 1);
105 else if (!abin->side)
106 aspect_bin_set_side(abin, widget);
108 g_warning("AspectBin cannot have more than 2 children.\n");
111 static void aspect_bin_remove(GtkContainer *container, GtkWidget *child)
113 AspectBin *abin = ASPECT_BIN(container);
115 if (abin->body == child) {
116 aspect_bin_set_body(abin, NULL, 1);
117 } else if (abin->side == child) {
118 aspect_bin_set_side(abin, NULL);
122 static void aspect_bin_forall(GtkContainer *container,
123 gboolean include_internals,
124 GtkCallback callback,
125 gpointer callback_data)
127 AspectBin *abin = ASPECT_BIN(container);
128 g_return_if_fail(callback != NULL);
131 callback(abin->body, callback_data);
133 callback(abin->side, callback_data);
136 static GType aspect_bin_child_type(GtkContainer *container)
138 if (!ASPECT_BIN(container)->body || !ASPECT_BIN(container)->side)
139 return GTK_TYPE_WIDGET;
144 aspect_bin_size_request(GtkWidget *widget, GtkRequisition *requisition)
146 AspectBin *abin = ASPECT_BIN(widget);
147 GtkRequisition creq = {0}, areq = {0};
149 if (abin->side && GTK_WIDGET_VISIBLE(abin->side)) {
150 gtk_widget_size_request(abin->side, &creq);
153 if (abin->body && GTK_WIDGET_VISIBLE(abin->body)) {
155 gtk_widget_size_request(abin->body, &areq);
156 wtmp = areq.height * abin->ratio + 0.5;
157 htmp = areq.width / abin->ratio + 0.5;
159 if (wtmp > areq.width) {
166 requisition->width = areq.width + creq.width;
167 requisition->height = MAX(areq.height, creq.height);
171 aspect_bin_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
173 AspectBin *abin = ASPECT_BIN(widget);
174 GtkRequisition creq = {0};
175 GtkAllocation csize = {0}, asize = {0};
177 /* First find the best fit for the body. */
178 if (abin->body && GTK_WIDGET_VISIBLE(abin->body)) {
179 asize.height = allocation->height;
180 asize.width = asize.height * abin->ratio + 0.5;
182 if (asize.width > allocation->width) {
183 asize.width = allocation->width;
184 asize.height = asize.width / abin->ratio + 0.5;
188 /* Now try to fit the side. */
189 if (abin->side && GTK_WIDGET_VISIBLE(abin->side)) {
190 gtk_widget_get_child_requisition(abin->side, &creq);
192 if (allocation->width - asize.width < creq.width) {
193 /* It didn't fit, squish the constrained guy. */
194 asize.width = allocation->width - creq.width;
195 asize.height = asize.width / abin->ratio + 0.5;
198 csize.width = allocation->width - asize.width;
199 csize.height = MIN(allocation->height, creq.height);
200 if (abin->constrain) {
201 csize.height = MAX(csize.height, asize.height);
203 csize.x = asize.width;
206 csize.y = (allocation->height - csize.height) * abin->align + 0.5;
207 asize.y = (allocation->height - asize.height) * abin->align + 0.5;
210 gtk_widget_size_allocate(abin->body, &asize);
212 gtk_widget_size_allocate(abin->side, &csize);
216 set_widget(GtkWidget **dest, GtkWidget *parent, GtkWidget *widget)
218 gboolean need_resize = FALSE;
224 need_resize |= GTK_WIDGET_VISIBLE(*dest);
225 gtk_widget_unparent(*dest);
230 gtk_widget_set_parent(widget, parent);
231 need_resize |= GTK_WIDGET_VISIBLE(widget);
234 return GTK_WIDGET_VISIBLE(parent) && need_resize;
237 void aspect_bin_set_body(AspectBin *abin, GtkWidget *widget, gfloat ratio)
239 g_return_if_fail(IS_ASPECT_BIN(abin));
240 g_return_if_fail(widget == NULL || GTK_IS_WIDGET(widget));
241 g_return_if_fail(widget == NULL || widget->parent == NULL);
243 if (set_widget(&abin->body, GTK_WIDGET(abin), widget))
244 gtk_widget_queue_resize(GTK_WIDGET(abin));
247 void aspect_bin_set_side(AspectBin *abin, GtkWidget *widget)
249 g_return_if_fail(IS_ASPECT_BIN(abin));
250 g_return_if_fail(widget == NULL || GTK_IS_WIDGET(widget));
251 g_return_if_fail(widget == NULL || widget->parent == NULL);
253 if (set_widget(&abin->side, GTK_WIDGET(abin), widget))
254 gtk_widget_queue_resize(GTK_WIDGET(abin));