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