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/.
26 #include "gtkcontainer.h"
29 #include "gtkprivate.h"
34 * @Short_description: Base class for widgets with alignments and padding
37 * The #GtkMisc widget is an abstract widget which is not useful itself, but
38 * is used to derive subclasses which have alignment and padding attributes.
40 * The horizontal and vertical padding attributes allows extra space to be
41 * added around the widget.
43 * The horizontal and vertical alignment attributes enable the widget to be
44 * positioned within its allocated area. Note that if the widget is added to
45 * a container in such a way that it expands automatically to fill its
46 * allocated area, the alignment settings will not alter the widgets position.
49 * Note that the desired effect can in most cases be achieved by using the
50 * #GtkWidget:halign, #GtkWidget:valign and #GtkWidget:margin properties
51 * on the child widget, so GtkMisc should not be used in new code.
56 struct _GtkMiscPrivate
73 static void gtk_misc_realize (GtkWidget *widget);
74 static void gtk_misc_set_property (GObject *object,
78 static void gtk_misc_get_property (GObject *object,
84 G_DEFINE_ABSTRACT_TYPE (GtkMisc, gtk_misc, GTK_TYPE_WIDGET)
87 gtk_misc_class_init (GtkMiscClass *class)
89 GObjectClass *gobject_class;
90 GtkWidgetClass *widget_class;
92 gobject_class = G_OBJECT_CLASS (class);
93 widget_class = (GtkWidgetClass*) class;
95 gobject_class->set_property = gtk_misc_set_property;
96 gobject_class->get_property = gtk_misc_get_property;
98 widget_class->realize = gtk_misc_realize;
100 g_object_class_install_property (gobject_class,
102 g_param_spec_float ("xalign",
104 P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
108 GTK_PARAM_READWRITE));
110 g_object_class_install_property (gobject_class,
112 g_param_spec_float ("yalign",
114 P_("The vertical alignment, from 0 (top) to 1 (bottom)"),
118 GTK_PARAM_READWRITE));
120 g_object_class_install_property (gobject_class,
122 g_param_spec_int ("xpad",
124 P_("The amount of space to add on the left and right of the widget, in pixels"),
128 GTK_PARAM_READWRITE));
130 g_object_class_install_property (gobject_class,
132 g_param_spec_int ("ypad",
134 P_("The amount of space to add on the top and bottom of the widget, in pixels"),
138 GTK_PARAM_READWRITE));
140 g_type_class_add_private (class, sizeof (GtkMiscPrivate));
144 gtk_misc_init (GtkMisc *misc)
146 GtkMiscPrivate *priv;
148 misc->priv = G_TYPE_INSTANCE_GET_PRIVATE (misc,
160 gtk_misc_set_property (GObject *object,
165 GtkMisc *misc = GTK_MISC (object);
166 GtkMiscPrivate *priv = misc->priv;
171 gtk_misc_set_alignment (misc, g_value_get_float (value), priv->yalign);
174 gtk_misc_set_alignment (misc, priv->xalign, g_value_get_float (value));
177 gtk_misc_set_padding (misc, g_value_get_int (value), priv->ypad);
180 gtk_misc_set_padding (misc, priv->xpad, g_value_get_int (value));
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 gtk_misc_get_property (GObject *object,
194 GtkMisc *misc = GTK_MISC (object);
195 GtkMiscPrivate *priv = misc->priv;
200 g_value_set_float (value, priv->xalign);
203 g_value_set_float (value, priv->yalign);
206 g_value_set_int (value, priv->xpad);
209 g_value_set_int (value, priv->ypad);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 * gtk_misc_set_alignment:
220 * @xalign: the horizontal alignment, from 0 (left) to 1 (right).
221 * @yalign: the vertical alignment, from 0 (top) to 1 (bottom).
223 * Sets the alignment of the widget.
226 gtk_misc_set_alignment (GtkMisc *misc,
230 GtkMiscPrivate *priv;
233 g_return_if_fail (GTK_IS_MISC (misc));
239 else if (xalign > 1.0)
244 else if (yalign > 1.0)
247 if ((xalign != priv->xalign) || (yalign != priv->yalign))
249 g_object_freeze_notify (G_OBJECT (misc));
250 if (xalign != priv->xalign)
251 g_object_notify (G_OBJECT (misc), "xalign");
253 if (yalign != priv->yalign)
254 g_object_notify (G_OBJECT (misc), "yalign");
256 priv->xalign = xalign;
257 priv->yalign = yalign;
259 /* clear the area that was allocated before the change
261 widget = GTK_WIDGET (misc);
262 if (gtk_widget_is_drawable (widget))
263 gtk_widget_queue_draw (widget);
265 g_object_thaw_notify (G_OBJECT (misc));
270 * gtk_misc_get_alignment:
272 * @xalign: (out) (allow-none): location to store X alignment of @misc, or %NULL
273 * @yalign: (out) (allow-none): location to store Y alignment of @misc, or %NULL
275 * Gets the X and Y alignment of the widget within its allocation.
276 * See gtk_misc_set_alignment().
279 gtk_misc_get_alignment (GtkMisc *misc,
283 GtkMiscPrivate *priv;
285 g_return_if_fail (GTK_IS_MISC (misc));
290 *xalign = priv->xalign;
292 *yalign = priv->yalign;
296 * gtk_misc_set_padding:
298 * @xpad: the amount of space to add on the left and right of the widget,
300 * @ypad: the amount of space to add on the top and bottom of the widget,
303 * Sets the amount of space to add around the widget.
306 gtk_misc_set_padding (GtkMisc *misc,
310 GtkMiscPrivate *priv;
312 g_return_if_fail (GTK_IS_MISC (misc));
321 if ((xpad != priv->xpad) || (ypad != priv->ypad))
323 g_object_freeze_notify (G_OBJECT (misc));
324 if (xpad != priv->xpad)
325 g_object_notify (G_OBJECT (misc), "xpad");
327 if (ypad != priv->ypad)
328 g_object_notify (G_OBJECT (misc), "ypad");
333 if (gtk_widget_is_drawable (GTK_WIDGET (misc)))
334 gtk_widget_queue_resize (GTK_WIDGET (misc));
336 g_object_thaw_notify (G_OBJECT (misc));
341 * gtk_misc_get_padding:
343 * @xpad: (out) (allow-none): location to store padding in the X
344 * direction, or %NULL
345 * @ypad: (out) (allow-none): location to store padding in the Y
346 * direction, or %NULL
348 * Gets the padding in the X and Y directions of the widget.
349 * See gtk_misc_set_padding().
352 gtk_misc_get_padding (GtkMisc *misc,
356 GtkMiscPrivate *priv;
358 g_return_if_fail (GTK_IS_MISC (misc));
369 gtk_misc_realize (GtkWidget *widget)
371 GtkAllocation allocation;
373 GdkWindowAttr attributes;
374 gint attributes_mask;
376 gtk_widget_set_realized (widget, TRUE);
378 if (!gtk_widget_get_has_window (widget))
380 window = gtk_widget_get_parent_window (widget);
381 gtk_widget_set_window (widget, window);
382 g_object_ref (window);
386 gtk_widget_get_allocation (widget, &allocation);
388 attributes.window_type = GDK_WINDOW_CHILD;
389 attributes.x = allocation.x;
390 attributes.y = allocation.y;
391 attributes.width = allocation.width;
392 attributes.height = allocation.height;
393 attributes.wclass = GDK_INPUT_OUTPUT;
394 attributes.visual = gtk_widget_get_visual (widget);
395 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
396 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
398 window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
399 gtk_widget_set_window (widget, window);
400 gtk_widget_register_window (widget, window);
401 gdk_window_set_background_pattern (window, NULL);
405 /* Semi-private function used by gtk widgets inheriting from
406 * GtkMisc that takes into account both css padding and border
407 * and the padding specified with the GtkMisc properties.
410 _gtk_misc_get_padding_and_border (GtkMisc *misc,
413 GtkStyleContext *context;
418 g_return_if_fail (GTK_IS_MISC (misc));
420 context = gtk_widget_get_style_context (GTK_WIDGET (misc));
421 state = gtk_widget_get_state_flags (GTK_WIDGET (misc));
423 gtk_style_context_get_padding (context, state, border);
425 gtk_misc_get_padding (misc, &xpad, &ypad);
427 border->left += xpad;
428 border->bottom += ypad;
429 border->right += xpad;
431 gtk_style_context_get_border (context, state, &tmp);
432 border->top += tmp.top;
433 border->right += tmp.right;
434 border->bottom += tmp.bottom;
435 border->left += tmp.left;