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