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/.
28 #include "gtkcontainer.h"
31 #include "gtkprivate.h"
36 * @Short_description: Base class for widgets with alignments and padding
39 * The #GtkMisc widget is an abstract widget which is not useful itself, but
40 * is used to derive subclasses which have alignment and padding attributes.
42 * The horizontal and vertical padding attributes allows extra space to be
43 * added around the widget.
45 * The horizontal and vertical alignment attributes enable the widget to be
46 * positioned within its allocated area. Note that if the widget is added to
47 * a container in such a way that it expands automatically to fill its
48 * allocated area, the alignment settings will not alter the widgets position.
51 * Note that the desired effect can in most cases be achieved by using the
52 * #GtkWidget:halign, #GtkWidget:valign and #GtkWidget:margin properties
53 * on the child widget, so GtkMisc should not be used in new code.
58 struct _GtkMiscPrivate
75 static void gtk_misc_realize (GtkWidget *widget);
76 static void gtk_misc_set_property (GObject *object,
80 static void gtk_misc_get_property (GObject *object,
86 G_DEFINE_ABSTRACT_TYPE (GtkMisc, gtk_misc, GTK_TYPE_WIDGET)
89 gtk_misc_class_init (GtkMiscClass *class)
91 GObjectClass *gobject_class;
92 GtkWidgetClass *widget_class;
94 gobject_class = G_OBJECT_CLASS (class);
95 widget_class = (GtkWidgetClass*) class;
97 gobject_class->set_property = gtk_misc_set_property;
98 gobject_class->get_property = gtk_misc_get_property;
100 widget_class->realize = gtk_misc_realize;
102 g_object_class_install_property (gobject_class,
104 g_param_spec_float ("xalign",
106 P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
110 GTK_PARAM_READWRITE));
112 g_object_class_install_property (gobject_class,
114 g_param_spec_float ("yalign",
116 P_("The vertical alignment, from 0 (top) to 1 (bottom)"),
120 GTK_PARAM_READWRITE));
122 g_object_class_install_property (gobject_class,
124 g_param_spec_int ("xpad",
126 P_("The amount of space to add on the left and right of the widget, in pixels"),
130 GTK_PARAM_READWRITE));
132 g_object_class_install_property (gobject_class,
134 g_param_spec_int ("ypad",
136 P_("The amount of space to add on the top and bottom of the widget, in pixels"),
140 GTK_PARAM_READWRITE));
142 g_type_class_add_private (class, sizeof (GtkMiscPrivate));
146 gtk_misc_init (GtkMisc *misc)
148 GtkMiscPrivate *priv;
150 misc->priv = G_TYPE_INSTANCE_GET_PRIVATE (misc,
162 gtk_misc_set_property (GObject *object,
167 GtkMisc *misc = GTK_MISC (object);
168 GtkMiscPrivate *priv = misc->priv;
173 gtk_misc_set_alignment (misc, g_value_get_float (value), priv->yalign);
176 gtk_misc_set_alignment (misc, priv->xalign, g_value_get_float (value));
179 gtk_misc_set_padding (misc, g_value_get_int (value), priv->ypad);
182 gtk_misc_set_padding (misc, priv->xpad, g_value_get_int (value));
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 gtk_misc_get_property (GObject *object,
196 GtkMisc *misc = GTK_MISC (object);
197 GtkMiscPrivate *priv = misc->priv;
202 g_value_set_float (value, priv->xalign);
205 g_value_set_float (value, priv->yalign);
208 g_value_set_int (value, priv->xpad);
211 g_value_set_int (value, priv->ypad);
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 * gtk_misc_set_alignment:
222 * @xalign: the horizontal alignment, from 0 (left) to 1 (right).
223 * @yalign: the vertical alignment, from 0 (top) to 1 (bottom).
225 * Sets the alignment of the widget.
228 gtk_misc_set_alignment (GtkMisc *misc,
232 GtkMiscPrivate *priv;
235 g_return_if_fail (GTK_IS_MISC (misc));
241 else if (xalign > 1.0)
246 else if (yalign > 1.0)
249 if ((xalign != priv->xalign) || (yalign != priv->yalign))
251 g_object_freeze_notify (G_OBJECT (misc));
252 if (xalign != priv->xalign)
253 g_object_notify (G_OBJECT (misc), "xalign");
255 if (yalign != priv->yalign)
256 g_object_notify (G_OBJECT (misc), "yalign");
258 priv->xalign = xalign;
259 priv->yalign = yalign;
261 /* clear the area that was allocated before the change
263 widget = GTK_WIDGET (misc);
264 if (gtk_widget_is_drawable (widget))
265 gtk_widget_queue_draw (widget);
267 g_object_thaw_notify (G_OBJECT (misc));
272 * gtk_misc_get_alignment:
274 * @xalign: (out) (allow-none): location to store X alignment of @misc, or %NULL
275 * @yalign: (out) (allow-none): location to store Y alignment of @misc, or %NULL
277 * Gets the X and Y alignment of the widget within its allocation.
278 * See gtk_misc_set_alignment().
281 gtk_misc_get_alignment (GtkMisc *misc,
285 GtkMiscPrivate *priv;
287 g_return_if_fail (GTK_IS_MISC (misc));
292 *xalign = priv->xalign;
294 *yalign = priv->yalign;
298 * gtk_misc_set_padding:
300 * @xpad: the amount of space to add on the left and right of the widget,
302 * @ypad: the amount of space to add on the top and bottom of the widget,
305 * Sets the amount of space to add around the widget.
308 gtk_misc_set_padding (GtkMisc *misc,
312 GtkMiscPrivate *priv;
314 g_return_if_fail (GTK_IS_MISC (misc));
323 if ((xpad != priv->xpad) || (ypad != priv->ypad))
325 g_object_freeze_notify (G_OBJECT (misc));
326 if (xpad != priv->xpad)
327 g_object_notify (G_OBJECT (misc), "xpad");
329 if (ypad != priv->ypad)
330 g_object_notify (G_OBJECT (misc), "ypad");
335 if (gtk_widget_is_drawable (GTK_WIDGET (misc)))
336 gtk_widget_queue_resize (GTK_WIDGET (misc));
338 g_object_thaw_notify (G_OBJECT (misc));
343 * gtk_misc_get_padding:
345 * @xpad: (out) (allow-none): location to store padding in the X
346 * direction, or %NULL
347 * @ypad: (out) (allow-none): location to store padding in the Y
348 * direction, or %NULL
350 * Gets the padding in the X and Y directions of the widget.
351 * See gtk_misc_set_padding().
354 gtk_misc_get_padding (GtkMisc *misc,
358 GtkMiscPrivate *priv;
360 g_return_if_fail (GTK_IS_MISC (misc));
371 gtk_misc_realize (GtkWidget *widget)
373 GtkAllocation allocation;
375 GdkWindowAttr attributes;
376 gint attributes_mask;
378 gtk_widget_set_realized (widget, TRUE);
380 if (!gtk_widget_get_has_window (widget))
382 window = gtk_widget_get_parent_window (widget);
383 gtk_widget_set_window (widget, window);
384 g_object_ref (window);
388 gtk_widget_get_allocation (widget, &allocation);
390 attributes.window_type = GDK_WINDOW_CHILD;
391 attributes.x = allocation.x;
392 attributes.y = allocation.y;
393 attributes.width = allocation.width;
394 attributes.height = allocation.height;
395 attributes.wclass = GDK_INPUT_OUTPUT;
396 attributes.visual = gtk_widget_get_visual (widget);
397 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
398 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
400 window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
401 gtk_widget_set_window (widget, window);
402 gdk_window_set_user_data (window, widget);
403 gdk_window_set_background_pattern (window, NULL);
407 /* Semi-private function used by gtk widgets inheriting from
408 * GtkMisc that takes into account both css padding and border
409 * and the padding specified with the GtkMisc properties.
412 _gtk_misc_get_padding_and_border (GtkMisc *misc,
415 GtkStyleContext *context;
420 g_return_if_fail (GTK_IS_MISC (misc));
422 context = gtk_widget_get_style_context (GTK_WIDGET (misc));
423 state = gtk_widget_get_state_flags (GTK_WIDGET (misc));
425 gtk_style_context_get_padding (context, state, border);
427 gtk_misc_get_padding (misc, &xpad, &ypad);
429 border->left += xpad;
430 border->bottom += ypad;
431 border->right += xpad;
433 gtk_style_context_get_border (context, state, &tmp);
434 border->top += tmp.top;
435 border->right += tmp.right;
436 border->bottom += tmp.bottom;
437 border->left += tmp.left;