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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 * @Short_description: A container which allows you to position
28 * widgets at fixed coordinates
31 * The #GtkFixed widget is a container which can place child widgets
32 * at fixed positions and with fixed sizes, given in pixels. #GtkFixed
33 * performs no automatic layout management.
35 * For most applications, you should not use this container! It keeps
36 * you from having to learn about the other GTK+ containers, but it
37 * results in broken applications. With #GtkFixed, the following
38 * things will result in truncated text, overlapping widgets, and
42 * Themes, which may change widget sizes.
45 * Fonts other than the one you used to write the app will of course
46 * change the size of widgets containing text; keep in mind that
47 * users may use a larger font because of difficulty reading the
48 * default, or they may be using Windows or the framebuffer port of
49 * GTK+, where different fonts are available.
52 * Translation of text into other languages changes its size. Also,
53 * display of non-English text will use a different font in many
58 * In addition, the fixed widget can't properly be mirrored in
59 * right-to-left languages such as Hebrew and Arabic. i.e. normally
60 * GTK+ will flip the interface to put labels to the right of the
61 * thing they label, but it can't do that with #GtkFixed. So your
62 * application will not be usable in right-to-left languages.
64 * Finally, fixed positioning makes it kind of annoying to add/remove
65 * GUI elements, since you have to reposition all the other
66 * elements. This is a long-term maintenance problem for your
69 * If you know none of these things are an issue for your application,
70 * and prefer the simplicity of #GtkFixed, by all means use the
71 * widget. But you should be aware of the tradeoffs.
78 #include "gtkprivate.h"
82 struct _GtkFixedPrivate
93 static void gtk_fixed_realize (GtkWidget *widget);
94 static void gtk_fixed_get_preferred_width (GtkWidget *widget,
97 static void gtk_fixed_get_preferred_height (GtkWidget *widget,
100 static void gtk_fixed_size_allocate (GtkWidget *widget,
101 GtkAllocation *allocation);
102 static void gtk_fixed_add (GtkContainer *container,
104 static void gtk_fixed_remove (GtkContainer *container,
106 static void gtk_fixed_forall (GtkContainer *container,
107 gboolean include_internals,
108 GtkCallback callback,
109 gpointer callback_data);
110 static GType gtk_fixed_child_type (GtkContainer *container);
112 static void gtk_fixed_set_child_property (GtkContainer *container,
117 static void gtk_fixed_get_child_property (GtkContainer *container,
123 G_DEFINE_TYPE (GtkFixed, gtk_fixed, GTK_TYPE_CONTAINER)
126 gtk_fixed_class_init (GtkFixedClass *class)
128 GtkWidgetClass *widget_class;
129 GtkContainerClass *container_class;
131 widget_class = (GtkWidgetClass*) class;
132 container_class = (GtkContainerClass*) class;
134 widget_class->realize = gtk_fixed_realize;
135 widget_class->get_preferred_width = gtk_fixed_get_preferred_width;
136 widget_class->get_preferred_height = gtk_fixed_get_preferred_height;
137 widget_class->size_allocate = gtk_fixed_size_allocate;
139 container_class->add = gtk_fixed_add;
140 container_class->remove = gtk_fixed_remove;
141 container_class->forall = gtk_fixed_forall;
142 container_class->child_type = gtk_fixed_child_type;
143 container_class->set_child_property = gtk_fixed_set_child_property;
144 container_class->get_child_property = gtk_fixed_get_child_property;
145 gtk_container_class_handle_border_width (container_class);
147 gtk_container_class_install_child_property (container_class,
149 g_param_spec_int ("x",
151 P_("X position of child widget"),
152 G_MININT, G_MAXINT, 0,
153 GTK_PARAM_READWRITE));
155 gtk_container_class_install_child_property (container_class,
157 g_param_spec_int ("y",
159 P_("Y position of child widget"),
160 G_MININT, G_MAXINT, 0,
161 GTK_PARAM_READWRITE));
163 g_type_class_add_private (class, sizeof (GtkFixedPrivate));
167 gtk_fixed_child_type (GtkContainer *container)
169 return GTK_TYPE_WIDGET;
173 gtk_fixed_init (GtkFixed *fixed)
175 fixed->priv = G_TYPE_INSTANCE_GET_PRIVATE (fixed, GTK_TYPE_FIXED, GtkFixedPrivate);
177 gtk_widget_set_has_window (GTK_WIDGET (fixed), FALSE);
179 fixed->priv->children = NULL;
185 * Creates a new #GtkFixed.
187 * Returns: a new #GtkFixed.
192 return g_object_new (GTK_TYPE_FIXED, NULL);
195 static GtkFixedChild*
196 get_child (GtkFixed *fixed,
199 GtkFixedPrivate *priv = fixed->priv;
202 for (children = priv->children; children; children = children->next)
204 GtkFixedChild *child;
206 child = children->data;
208 if (child->widget == widget)
217 * @fixed: a #GtkFixed.
218 * @widget: the widget to add.
219 * @x: the horizontal position to place the widget at.
220 * @y: the vertical position to place the widget at.
222 * Adds a widget to a #GtkFixed container at the given position.
225 gtk_fixed_put (GtkFixed *fixed,
230 GtkFixedPrivate *priv = fixed->priv;
231 GtkFixedChild *child_info;
233 g_return_if_fail (GTK_IS_FIXED (fixed));
234 g_return_if_fail (GTK_IS_WIDGET (widget));
236 child_info = g_new (GtkFixedChild, 1);
237 child_info->widget = widget;
241 gtk_widget_set_parent (widget, GTK_WIDGET (fixed));
243 priv->children = g_list_append (priv->children, child_info);
247 gtk_fixed_move_internal (GtkFixed *fixed,
248 GtkFixedChild *child,
252 g_return_if_fail (GTK_IS_FIXED (fixed));
253 g_return_if_fail (gtk_widget_get_parent (child->widget) == GTK_WIDGET (fixed));
255 gtk_widget_freeze_child_notify (child->widget);
260 gtk_widget_child_notify (child->widget, "x");
266 gtk_widget_child_notify (child->widget, "y");
269 gtk_widget_thaw_child_notify (child->widget);
271 if (gtk_widget_get_visible (child->widget) &&
272 gtk_widget_get_visible (GTK_WIDGET (fixed)))
273 gtk_widget_queue_resize (GTK_WIDGET (fixed));
278 * @fixed: a #GtkFixed.
279 * @widget: the child widget.
280 * @x: the horizontal position to move the widget to.
281 * @y: the vertical position to move the widget to.
283 * Moves a child of a #GtkFixed container to the given position.
286 gtk_fixed_move (GtkFixed *fixed,
291 gtk_fixed_move_internal (fixed, get_child (fixed, widget), x, y);
295 gtk_fixed_set_child_property (GtkContainer *container,
301 GtkFixed *fixed = GTK_FIXED (container);
302 GtkFixedChild *fixed_child;
304 fixed_child = get_child (fixed, child);
309 gtk_fixed_move_internal (fixed,
311 g_value_get_int (value),
315 gtk_fixed_move_internal (fixed,
318 g_value_get_int (value));
321 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
327 gtk_fixed_get_child_property (GtkContainer *container,
333 GtkFixedChild *fixed_child;
335 fixed_child = get_child (GTK_FIXED (container), child);
340 g_value_set_int (value, fixed_child->x);
343 g_value_set_int (value, fixed_child->y);
346 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
352 gtk_fixed_realize (GtkWidget *widget)
354 GtkAllocation allocation;
356 GdkWindowAttr attributes;
357 gint attributes_mask;
359 if (!gtk_widget_get_has_window (widget))
360 GTK_WIDGET_CLASS (gtk_fixed_parent_class)->realize (widget);
363 gtk_widget_set_realized (widget, TRUE);
365 gtk_widget_get_allocation (widget, &allocation);
367 attributes.window_type = GDK_WINDOW_CHILD;
368 attributes.x = allocation.x;
369 attributes.y = allocation.y;
370 attributes.width = allocation.width;
371 attributes.height = allocation.height;
372 attributes.wclass = GDK_INPUT_OUTPUT;
373 attributes.visual = gtk_widget_get_visual (widget);
374 attributes.event_mask = gtk_widget_get_events (widget);
375 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK;
377 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
379 window = gdk_window_new (gtk_widget_get_parent_window (widget),
380 &attributes, attributes_mask);
381 gtk_widget_set_window (widget, window);
382 gtk_widget_register_window (widget, window);
384 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
390 gtk_fixed_get_preferred_width (GtkWidget *widget,
394 GtkFixed *fixed = GTK_FIXED (widget);
395 GtkFixedPrivate *priv = fixed->priv;
396 GtkFixedChild *child;
398 gint child_min, child_nat;
403 for (children = priv->children; children; children = children->next)
405 child = children->data;
407 if (!gtk_widget_get_visible (child->widget))
410 gtk_widget_get_preferred_width (child->widget, &child_min, &child_nat);
412 *minimum = MAX (*minimum, child->x + child_min);
413 *natural = MAX (*natural, child->x + child_nat);
418 gtk_fixed_get_preferred_height (GtkWidget *widget,
422 GtkFixed *fixed = GTK_FIXED (widget);
423 GtkFixedPrivate *priv = fixed->priv;
424 GtkFixedChild *child;
426 gint child_min, child_nat;
431 for (children = priv->children; children; children = children->next)
433 child = children->data;
435 if (!gtk_widget_get_visible (child->widget))
438 gtk_widget_get_preferred_height (child->widget, &child_min, &child_nat);
440 *minimum = MAX (*minimum, child->y + child_min);
441 *natural = MAX (*natural, child->y + child_nat);
446 gtk_fixed_size_allocate (GtkWidget *widget,
447 GtkAllocation *allocation)
449 GtkFixed *fixed = GTK_FIXED (widget);
450 GtkFixedPrivate *priv = fixed->priv;
451 GtkFixedChild *child;
452 GtkAllocation child_allocation;
453 GtkRequisition child_requisition;
456 gtk_widget_set_allocation (widget, allocation);
458 if (gtk_widget_get_has_window (widget))
460 if (gtk_widget_get_realized (widget))
461 gdk_window_move_resize (gtk_widget_get_window (widget),
468 for (children = priv->children; children; children = children->next)
470 child = children->data;
472 if (!gtk_widget_get_visible (child->widget))
475 gtk_widget_get_preferred_size (child->widget, &child_requisition, NULL);
476 child_allocation.x = child->x;
477 child_allocation.y = child->y;
479 if (!gtk_widget_get_has_window (widget))
481 child_allocation.x += allocation->x;
482 child_allocation.y += allocation->y;
485 child_allocation.width = child_requisition.width;
486 child_allocation.height = child_requisition.height;
487 gtk_widget_size_allocate (child->widget, &child_allocation);
492 gtk_fixed_add (GtkContainer *container,
495 gtk_fixed_put (GTK_FIXED (container), widget, 0, 0);
499 gtk_fixed_remove (GtkContainer *container,
502 GtkFixed *fixed = GTK_FIXED (container);
503 GtkFixedPrivate *priv = fixed->priv;
504 GtkFixedChild *child;
505 GtkWidget *widget_container = GTK_WIDGET (container);
508 for (children = priv->children; children; children = children->next)
510 child = children->data;
512 if (child->widget == widget)
514 gboolean was_visible = gtk_widget_get_visible (widget);
516 gtk_widget_unparent (widget);
518 priv->children = g_list_remove_link (priv->children, children);
519 g_list_free (children);
522 if (was_visible && gtk_widget_get_visible (widget_container))
523 gtk_widget_queue_resize (widget_container);
531 gtk_fixed_forall (GtkContainer *container,
532 gboolean include_internals,
533 GtkCallback callback,
534 gpointer callback_data)
536 GtkFixed *fixed = GTK_FIXED (container);
537 GtkFixedPrivate *priv = fixed->priv;
538 GtkFixedChild *child;
541 children = priv->children;
544 child = children->data;
545 children = children->next;
547 (* callback) (child->widget, callback_data);