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