]> Pileus Git - ~andy/gtk/blob - gtk/gtkmisc.c
Minor documentation improvements
[~andy/gtk] / gtk / gtkmisc.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "config.h"
28 #include "gtkcontainer.h"
29 #include "gtkmisc.h"
30 #include "gtkintl.h"
31 #include "gtkprivate.h"
32
33
34 struct _GtkMiscPrivate
35 {
36   gfloat        xalign;
37   gfloat        yalign;
38
39   guint16       xpad;
40   guint16       ypad;
41 };
42
43 enum {
44   PROP_0,
45   PROP_XALIGN,
46   PROP_YALIGN,
47   PROP_XPAD,
48   PROP_YPAD
49 };
50
51 static void gtk_misc_realize      (GtkWidget    *widget);
52 static void gtk_misc_set_property (GObject         *object,
53                                    guint            prop_id,
54                                    const GValue    *value,
55                                    GParamSpec      *pspec);
56 static void gtk_misc_get_property (GObject         *object,
57                                    guint            prop_id,
58                                    GValue          *value,
59                                    GParamSpec      *pspec);
60
61
62 G_DEFINE_ABSTRACT_TYPE (GtkMisc, gtk_misc, GTK_TYPE_WIDGET)
63
64 static void
65 gtk_misc_class_init (GtkMiscClass *class)
66 {
67   GObjectClass   *gobject_class;
68   GtkWidgetClass *widget_class;
69
70   gobject_class = G_OBJECT_CLASS (class);
71   widget_class = (GtkWidgetClass*) class;
72
73   gobject_class->set_property = gtk_misc_set_property;
74   gobject_class->get_property = gtk_misc_get_property;
75   
76   widget_class->realize = gtk_misc_realize;
77
78   g_object_class_install_property (gobject_class,
79                                    PROP_XALIGN,
80                                    g_param_spec_float ("xalign",
81                                                        P_("X align"),
82                                                        P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
83                                                        0.0,
84                                                        1.0,
85                                                        0.5,
86                                                        GTK_PARAM_READWRITE));
87
88   g_object_class_install_property (gobject_class,
89                                    PROP_YALIGN,
90                                    g_param_spec_float ("yalign",
91                                                        P_("Y align"),
92                                                        P_("The vertical alignment, from 0 (top) to 1 (bottom)"),
93                                                        0.0,
94                                                        1.0,
95                                                        0.5,
96                                                        GTK_PARAM_READWRITE));
97
98   g_object_class_install_property (gobject_class,
99                                    PROP_XPAD,
100                                    g_param_spec_int ("xpad",
101                                                      P_("X pad"),
102                                                      P_("The amount of space to add on the left and right of the widget, in pixels"),
103                                                      0,
104                                                      G_MAXINT,
105                                                      0,
106                                                      GTK_PARAM_READWRITE));
107
108   g_object_class_install_property (gobject_class,
109                                    PROP_YPAD,
110                                    g_param_spec_int ("ypad",
111                                                      P_("Y pad"),
112                                                      P_("The amount of space to add on the top and bottom of the widget, in pixels"),
113                                                      0,
114                                                      G_MAXINT,
115                                                      0,
116                                                      GTK_PARAM_READWRITE));
117
118   g_type_class_add_private (class, sizeof (GtkMiscPrivate));
119 }
120
121 static void
122 gtk_misc_init (GtkMisc *misc)
123 {
124   GtkMiscPrivate *priv;
125
126   misc->priv = G_TYPE_INSTANCE_GET_PRIVATE (misc,
127                                             GTK_TYPE_MISC,
128                                             GtkMiscPrivate);
129   priv = misc->priv;
130
131   priv->xalign = 0.5;
132   priv->yalign = 0.5;
133   priv->xpad = 0;
134   priv->ypad = 0;
135 }
136
137 static void
138 gtk_misc_set_property (GObject      *object,
139                        guint         prop_id,
140                        const GValue *value,
141                        GParamSpec   *pspec)
142 {
143   GtkMisc *misc = GTK_MISC (object);
144   GtkMiscPrivate *priv = misc->priv;
145
146   switch (prop_id)
147     {
148     case PROP_XALIGN:
149       gtk_misc_set_alignment (misc, g_value_get_float (value), priv->yalign);
150       break;
151     case PROP_YALIGN:
152       gtk_misc_set_alignment (misc, priv->xalign, g_value_get_float (value));
153       break;
154     case PROP_XPAD:
155       gtk_misc_set_padding (misc, g_value_get_int (value), priv->ypad);
156       break;
157     case PROP_YPAD:
158       gtk_misc_set_padding (misc, priv->xpad, g_value_get_int (value));
159       break;
160     default:
161       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162       break;
163     }
164 }
165
166 static void
167 gtk_misc_get_property (GObject      *object,
168                        guint         prop_id,
169                        GValue       *value,
170                        GParamSpec   *pspec)
171 {
172   GtkMisc *misc = GTK_MISC (object);
173   GtkMiscPrivate *priv = misc->priv;
174
175   switch (prop_id)
176     {
177     case PROP_XALIGN:
178       g_value_set_float (value, priv->xalign);
179       break;
180     case PROP_YALIGN:
181       g_value_set_float (value, priv->yalign);
182       break;
183     case PROP_XPAD:
184       g_value_set_int (value, priv->xpad);
185       break;
186     case PROP_YPAD:
187       g_value_set_int (value, priv->ypad);
188       break;
189     default:
190       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191       break;
192     }
193 }
194
195 void
196 gtk_misc_set_alignment (GtkMisc *misc,
197                         gfloat   xalign,
198                         gfloat   yalign)
199 {
200   GtkMiscPrivate *priv;
201   GtkWidget *widget;
202
203   g_return_if_fail (GTK_IS_MISC (misc));
204
205   priv = misc->priv;
206
207   if (xalign < 0.0)
208     xalign = 0.0;
209   else if (xalign > 1.0)
210     xalign = 1.0;
211
212   if (yalign < 0.0)
213     yalign = 0.0;
214   else if (yalign > 1.0)
215     yalign = 1.0;
216
217   if ((xalign != priv->xalign) || (yalign != priv->yalign))
218     {
219       g_object_freeze_notify (G_OBJECT (misc));
220       if (xalign != priv->xalign)
221         g_object_notify (G_OBJECT (misc), "xalign");
222
223       if (yalign != priv->yalign)
224         g_object_notify (G_OBJECT (misc), "yalign");
225
226       priv->xalign = xalign;
227       priv->yalign = yalign;
228       
229       /* clear the area that was allocated before the change
230        */
231       widget = GTK_WIDGET (misc);
232       if (gtk_widget_is_drawable (widget))
233         gtk_widget_queue_draw (widget);
234
235       g_object_thaw_notify (G_OBJECT (misc));
236     }
237 }
238
239 /**
240  * gtk_misc_get_alignment:
241  * @misc: a #GtkMisc
242  * @xalign: (out) (allow-none): location to store X alignment of @misc, or %NULL
243  * @yalign: (out) (allow-none): location to store Y alignment of @misc, or %NULL
244  *
245  * Gets the X and Y alignment of the widget within its allocation. 
246  * See gtk_misc_set_alignment().
247  **/
248 void
249 gtk_misc_get_alignment (GtkMisc *misc,
250                         gfloat  *xalign,
251                         gfloat  *yalign)
252 {
253   GtkMiscPrivate *priv;
254
255   g_return_if_fail (GTK_IS_MISC (misc));
256
257   priv = misc->priv;
258
259   if (xalign)
260     *xalign = priv->xalign;
261   if (yalign)
262     *yalign = priv->yalign;
263 }
264
265 void
266 gtk_misc_set_padding (GtkMisc *misc,
267                       gint     xpad,
268                       gint     ypad)
269 {
270   GtkMiscPrivate *priv;
271
272   g_return_if_fail (GTK_IS_MISC (misc));
273
274   priv = misc->priv;
275
276   if (xpad < 0)
277     xpad = 0;
278   if (ypad < 0)
279     ypad = 0;
280
281   if ((xpad != priv->xpad) || (ypad != priv->ypad))
282     {
283       g_object_freeze_notify (G_OBJECT (misc));
284       if (xpad != priv->xpad)
285         g_object_notify (G_OBJECT (misc), "xpad");
286
287       if (ypad != priv->ypad)
288         g_object_notify (G_OBJECT (misc), "ypad");
289
290       priv->xpad = xpad;
291       priv->ypad = ypad;
292
293       if (gtk_widget_is_drawable (GTK_WIDGET (misc)))
294         gtk_widget_queue_resize (GTK_WIDGET (misc));
295
296       g_object_thaw_notify (G_OBJECT (misc));
297     }
298 }
299
300 /**
301  * gtk_misc_get_padding:
302  * @misc: a #GtkMisc
303  * @xpad: (out) (allow-none): location to store padding in the X
304  *        direction, or %NULL
305  * @ypad: (out) (allow-none): location to store padding in the Y
306  *        direction, or %NULL
307  *
308  * Gets the padding in the X and Y directions of the widget. 
309  * See gtk_misc_set_padding().
310  **/
311 void
312 gtk_misc_get_padding (GtkMisc *misc,
313                       gint    *xpad,
314                       gint    *ypad)
315 {
316   GtkMiscPrivate *priv;
317
318   g_return_if_fail (GTK_IS_MISC (misc));
319
320   priv = misc->priv;
321
322   if (xpad)
323     *xpad = priv->xpad;
324   if (ypad)
325     *ypad = priv->ypad;
326 }
327
328 static void
329 gtk_misc_realize (GtkWidget *widget)
330 {
331   GtkAllocation allocation;
332   GdkWindow *window;
333   GdkWindowAttr attributes;
334   gint attributes_mask;
335
336   gtk_widget_set_realized (widget, TRUE);
337
338   if (!gtk_widget_get_has_window (widget))
339     {
340       window = gtk_widget_get_parent_window (widget);
341       gtk_widget_set_window (widget, window);
342       g_object_ref (window);
343     }
344   else
345     {
346       gtk_widget_get_allocation (widget, &allocation);
347
348       attributes.window_type = GDK_WINDOW_CHILD;
349       attributes.x = allocation.x;
350       attributes.y = allocation.y;
351       attributes.width = allocation.width;
352       attributes.height = allocation.height;
353       attributes.wclass = GDK_INPUT_OUTPUT;
354       attributes.visual = gtk_widget_get_visual (widget);
355       attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
356       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
357
358       window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
359       gtk_widget_set_window (widget, window);
360       gdk_window_set_user_data (window, widget);
361       gdk_window_set_background_pattern (window, NULL);
362     }
363 }