1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
31 #include "gtkprivate.h"
35 struct _GtkFixedPrivate
46 static void gtk_fixed_realize (GtkWidget *widget);
47 static void gtk_fixed_get_preferred_width (GtkWidget *widget,
50 static void gtk_fixed_get_preferred_height (GtkWidget *widget,
53 static void gtk_fixed_size_allocate (GtkWidget *widget,
54 GtkAllocation *allocation);
55 static void gtk_fixed_add (GtkContainer *container,
57 static void gtk_fixed_remove (GtkContainer *container,
59 static void gtk_fixed_forall (GtkContainer *container,
60 gboolean include_internals,
62 gpointer callback_data);
63 static GType gtk_fixed_child_type (GtkContainer *container);
65 static void gtk_fixed_set_child_property (GtkContainer *container,
70 static void gtk_fixed_get_child_property (GtkContainer *container,
76 G_DEFINE_TYPE (GtkFixed, gtk_fixed, GTK_TYPE_CONTAINER)
79 gtk_fixed_class_init (GtkFixedClass *class)
81 GtkWidgetClass *widget_class;
82 GtkContainerClass *container_class;
84 widget_class = (GtkWidgetClass*) class;
85 container_class = (GtkContainerClass*) class;
87 widget_class->realize = gtk_fixed_realize;
88 widget_class->get_preferred_width = gtk_fixed_get_preferred_width;
89 widget_class->get_preferred_height = gtk_fixed_get_preferred_height;
90 widget_class->size_allocate = gtk_fixed_size_allocate;
92 container_class->add = gtk_fixed_add;
93 container_class->remove = gtk_fixed_remove;
94 container_class->forall = gtk_fixed_forall;
95 container_class->child_type = gtk_fixed_child_type;
96 container_class->set_child_property = gtk_fixed_set_child_property;
97 container_class->get_child_property = gtk_fixed_get_child_property;
98 gtk_container_class_handle_border_width (container_class);
100 gtk_container_class_install_child_property (container_class,
102 g_param_spec_int ("x",
104 P_("X position of child widget"),
105 G_MININT, G_MAXINT, 0,
106 GTK_PARAM_READWRITE));
108 gtk_container_class_install_child_property (container_class,
110 g_param_spec_int ("y",
112 P_("Y position of child widget"),
113 G_MININT, G_MAXINT, 0,
114 GTK_PARAM_READWRITE));
116 g_type_class_add_private (class, sizeof (GtkFixedPrivate));
120 gtk_fixed_child_type (GtkContainer *container)
122 return GTK_TYPE_WIDGET;
126 gtk_fixed_init (GtkFixed *fixed)
128 fixed->priv = G_TYPE_INSTANCE_GET_PRIVATE (fixed, GTK_TYPE_FIXED, GtkFixedPrivate);
130 gtk_widget_set_has_window (GTK_WIDGET (fixed), FALSE);
132 fixed->priv->children = NULL;
138 return g_object_new (GTK_TYPE_FIXED, NULL);
141 static GtkFixedChild*
142 get_child (GtkFixed *fixed,
145 GtkFixedPrivate *priv = fixed->priv;
148 for (children = priv->children; children; children = children->next)
150 GtkFixedChild *child;
152 child = children->data;
154 if (child->widget == widget)
162 gtk_fixed_put (GtkFixed *fixed,
167 GtkFixedPrivate *priv = fixed->priv;
168 GtkFixedChild *child_info;
170 g_return_if_fail (GTK_IS_FIXED (fixed));
171 g_return_if_fail (GTK_IS_WIDGET (widget));
173 child_info = g_new (GtkFixedChild, 1);
174 child_info->widget = widget;
178 gtk_widget_set_parent (widget, GTK_WIDGET (fixed));
180 priv->children = g_list_append (priv->children, child_info);
184 gtk_fixed_move_internal (GtkFixed *fixed,
185 GtkFixedChild *child,
189 g_return_if_fail (GTK_IS_FIXED (fixed));
190 g_return_if_fail (gtk_widget_get_parent (child->widget) == GTK_WIDGET (fixed));
192 gtk_widget_freeze_child_notify (child->widget);
197 gtk_widget_child_notify (child->widget, "x");
203 gtk_widget_child_notify (child->widget, "y");
206 gtk_widget_thaw_child_notify (child->widget);
208 if (gtk_widget_get_visible (child->widget) &&
209 gtk_widget_get_visible (GTK_WIDGET (fixed)))
210 gtk_widget_queue_resize (GTK_WIDGET (fixed));
214 gtk_fixed_move (GtkFixed *fixed,
219 gtk_fixed_move_internal (fixed, get_child (fixed, widget), x, y);
223 gtk_fixed_set_child_property (GtkContainer *container,
229 GtkFixed *fixed = GTK_FIXED (container);
230 GtkFixedChild *fixed_child;
232 fixed_child = get_child (fixed, child);
237 gtk_fixed_move_internal (fixed,
239 g_value_get_int (value),
243 gtk_fixed_move_internal (fixed,
246 g_value_get_int (value));
249 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
255 gtk_fixed_get_child_property (GtkContainer *container,
261 GtkFixedChild *fixed_child;
263 fixed_child = get_child (GTK_FIXED (container), child);
268 g_value_set_int (value, fixed_child->x);
271 g_value_set_int (value, fixed_child->y);
274 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
280 gtk_fixed_realize (GtkWidget *widget)
282 GtkAllocation allocation;
284 GdkWindowAttr attributes;
285 gint attributes_mask;
287 if (!gtk_widget_get_has_window (widget))
288 GTK_WIDGET_CLASS (gtk_fixed_parent_class)->realize (widget);
291 gtk_widget_set_realized (widget, TRUE);
293 gtk_widget_get_allocation (widget, &allocation);
295 attributes.window_type = GDK_WINDOW_CHILD;
296 attributes.x = allocation.x;
297 attributes.y = allocation.y;
298 attributes.width = allocation.width;
299 attributes.height = allocation.height;
300 attributes.wclass = GDK_INPUT_OUTPUT;
301 attributes.visual = gtk_widget_get_visual (widget);
302 attributes.event_mask = gtk_widget_get_events (widget);
303 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK;
305 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
307 window = gdk_window_new (gtk_widget_get_parent_window (widget),
308 &attributes, attributes_mask);
309 gtk_widget_set_window (widget, window);
310 gdk_window_set_user_data (window, widget);
312 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
318 gtk_fixed_get_preferred_width (GtkWidget *widget,
322 GtkFixed *fixed = GTK_FIXED (widget);
323 GtkFixedPrivate *priv = fixed->priv;
324 GtkFixedChild *child;
326 gint child_min, child_nat;
331 for (children = priv->children; children; children = children->next)
333 child = children->data;
335 if (!gtk_widget_get_visible (child->widget))
338 gtk_widget_get_preferred_width (child->widget, &child_min, &child_nat);
340 *minimum = MAX (*minimum, child->x + child_min);
341 *natural = MAX (*natural, child->x + child_nat);
346 gtk_fixed_get_preferred_height (GtkWidget *widget,
350 GtkFixed *fixed = GTK_FIXED (widget);
351 GtkFixedPrivate *priv = fixed->priv;
352 GtkFixedChild *child;
354 gint child_min, child_nat;
359 for (children = priv->children; children; children = children->next)
361 child = children->data;
363 if (!gtk_widget_get_visible (child->widget))
366 gtk_widget_get_preferred_height (child->widget, &child_min, &child_nat);
368 *minimum = MAX (*minimum, child->y + child_min);
369 *natural = MAX (*natural, child->y + child_nat);
374 gtk_fixed_size_allocate (GtkWidget *widget,
375 GtkAllocation *allocation)
377 GtkFixed *fixed = GTK_FIXED (widget);
378 GtkFixedPrivate *priv = fixed->priv;
379 GtkFixedChild *child;
380 GtkAllocation child_allocation;
381 GtkRequisition child_requisition;
384 gtk_widget_set_allocation (widget, allocation);
386 if (gtk_widget_get_has_window (widget))
388 if (gtk_widget_get_realized (widget))
389 gdk_window_move_resize (gtk_widget_get_window (widget),
396 for (children = priv->children; children; children = children->next)
398 child = children->data;
400 if (!gtk_widget_get_visible (child->widget))
403 gtk_widget_get_preferred_size (child->widget, &child_requisition, NULL);
404 child_allocation.x = child->x;
405 child_allocation.y = child->y;
407 if (!gtk_widget_get_has_window (widget))
409 child_allocation.x += allocation->x;
410 child_allocation.y += allocation->y;
413 child_allocation.width = child_requisition.width;
414 child_allocation.height = child_requisition.height;
415 gtk_widget_size_allocate (child->widget, &child_allocation);
420 gtk_fixed_add (GtkContainer *container,
423 gtk_fixed_put (GTK_FIXED (container), widget, 0, 0);
427 gtk_fixed_remove (GtkContainer *container,
430 GtkFixed *fixed = GTK_FIXED (container);
431 GtkFixedPrivate *priv = fixed->priv;
432 GtkFixedChild *child;
433 GtkWidget *widget_container = GTK_WIDGET (container);
436 for (children = priv->children; children; children = children->next)
438 child = children->data;
440 if (child->widget == widget)
442 gboolean was_visible = gtk_widget_get_visible (widget);
444 gtk_widget_unparent (widget);
446 priv->children = g_list_remove_link (priv->children, children);
447 g_list_free (children);
450 if (was_visible && gtk_widget_get_visible (widget_container))
451 gtk_widget_queue_resize (widget_container);
456 children = children->next;
461 gtk_fixed_forall (GtkContainer *container,
462 gboolean include_internals,
463 GtkCallback callback,
464 gpointer callback_data)
466 GtkFixed *fixed = GTK_FIXED (container);
467 GtkFixedPrivate *priv = fixed->priv;
468 GtkFixedChild *child;
471 for (children = priv->children; children; children = children->next)
473 child = children->data;
475 (* callback) (child->widget, callback_data);