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