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