1 /* Fo
2  * fo-property-padding.c: 'padding' property
3  *
4  * Copyright (C) 2001 Sun Microsystems
5  * Copyright (C) 2007 Menteith Consulting Ltd
6  *
7  * See COPYING for the status of this software.
8  */
9 
10 #include <string.h>
11 #include "fo-utils.h"
12 #include "fo-context.h"
13 #include "datatype/fo-datatype.h"
14 #include "property/fo-property-private.h"
15 #include "property/fo-property-font-size.h"
16 #include "property/fo-property-padding.h"
17 #include "property/fo-property-util.h"
18 
19 /* padding */
20 /* Inherited: FALSE */
21 /* Shorthand: TRUE */
22 /* <padding-width>{1,4} | inherit */
23 /* Initial value: not defined for shorthand properties */
24 
25 struct _FoPropertyPadding
26 {
27   FoProperty parent_instance;
28 };
29 
30 struct _FoPropertyPaddingClass
31 {
32   FoPropertyClass parent_class;
33 };
34 
35 static void fo_property_padding_init         (FoPropertyPadding      *property_padding);
36 static void fo_property_padding_class_init   (FoPropertyPaddingClass *klass);
37 static void fo_property_padding_finalize     (GObject       *object);
38 
39 static FoDatatype* fo_property_padding_validate (FoDatatype *datatype,
40                                                  FoContext  *context,
41                                                  GError    **error);
42 
43 static const gchar class_name[] = "padding";
44 static gpointer parent_class;
45 
46 /**
47  * fo_property_padding_get_type:
48  *
49  * Register the #FoPropertyPadding type if not already registered and
50  * return its #GType value.
51  *
52  * Return value: #GType of #FoPropertyPadding.
53  **/
54 GType
fo_property_padding_get_type(void)55 fo_property_padding_get_type (void)
56 {
57   static GType object_type = 0;
58 
59   if (!object_type)
60     {
61       static const GTypeInfo object_info =
62       {
63         sizeof (FoPropertyPaddingClass),
64         NULL,           /* base_init */
65         NULL,           /* base_finalize */
66         (GClassInitFunc) fo_property_padding_class_init,
67         NULL,           /* class_finalize */
68         NULL,           /* class_data */
69         sizeof (FoPropertyPadding),
70         0,              /* n_preallocs */
71         (GInstanceInitFunc) fo_property_padding_init,
72 	NULL		/* value_table */
73       };
74 
75       object_type = g_type_register_static (FO_TYPE_PROPERTY,
76                                             class_name,
77                                             &object_info, 0);
78     }
79 
80   return object_type;
81 }
82 
83 /**
84  * fo_property_padding_init:
85  * @padding: #FoPropertyPadding object to initialise.
86  *
87  * Implements #GInstanceInitFunc for #FoPropertyPadding.
88  **/
89 void
fo_property_padding_init(FoPropertyPadding * padding)90 fo_property_padding_init (FoPropertyPadding *padding)
91 {
92   FO_PROPERTY (padding)->value =
93     NULL;
94 }
95 
96 /**
97  * fo_property_padding_class_init:
98  * @klass: #FoPropertyPaddingClass object to initialise.
99  *
100  * Implements #GClassInitFunc for #FoPropertyPaddingClass.
101  **/
102 void
fo_property_padding_class_init(FoPropertyPaddingClass * klass)103 fo_property_padding_class_init (FoPropertyPaddingClass *klass)
104 {
105   GObjectClass *object_class = G_OBJECT_CLASS (klass);
106   FoPropertyClass *property_class = FO_PROPERTY_CLASS (klass);
107 
108   parent_class = g_type_class_peek_parent (klass);
109 
110   object_class->finalize = fo_property_padding_finalize;
111 
112   property_class->expr_eval = fo_expr_padding_eval;
113   property_class->is_inherited = FALSE;
114   property_class->is_shorthand = TRUE;
115   property_class->resolve_enum =
116     fo_property_util_resolve_no_enum;
117   property_class->resolve_percent =
118     fo_property_util_resolve_ipdim_percent;
119   property_class->validate =
120     fo_property_padding_validate;
121   property_class->get_initial =
122     fo_property_padding_get_initial;
123 }
124 
125 /**
126  * fo_property_padding_finalize:
127  * @object: #FoPropertyPadding object to finalize.
128  *
129  * Implements #GObjectFinalizeFunc for #FoPropertyPadding.
130  **/
131 void
fo_property_padding_finalize(GObject * object)132 fo_property_padding_finalize (GObject *object)
133 {
134   FoPropertyPadding *padding;
135 
136   padding = FO_PROPERTY_PADDING (object);
137 
138   G_OBJECT_CLASS (parent_class)->finalize (object);
139 }
140 
141 
142 /**
143  * fo_property_padding_new:
144  *
145  * Creates a new #FoPropertyPadding initialized to default value.
146  *
147  * Return value: the new #FoPropertyPadding.
148  **/
149 FoProperty*
fo_property_padding_new(void)150 fo_property_padding_new (void)
151 {
152   FoProperty* padding;
153 
154   padding =
155     FO_PROPERTY (g_object_new (fo_property_padding_get_type (),
156                                NULL));
157 
158   return padding;
159 }
160 
161 /**
162  * fo_property_padding_validate:
163  * @datatype: #FoDatatype to be validated against allowed datatypes and
164  *            values for current property.
165  * @context:  #FoContext object from which to possibly inherit values.
166  * @error:    Information about any error that has occurred.
167  *
168  * Validates @datatype against allowed values.  Returns @datatype, a
169  * replacement datatype value, or NULL if validation failed.
170  *
171  * Return value: Valid datatype value or NULL.
172  **/
173 FoDatatype*
fo_property_padding_validate(FoDatatype * datatype,FoContext * context,GError ** error)174 fo_property_padding_validate (FoDatatype *datatype,
175                               FoContext  *context,
176                               GError    **error)
177 {
178   g_return_val_if_fail (datatype != NULL, NULL);
179   g_return_val_if_fail (FO_IS_DATATYPE (datatype), NULL);
180   g_return_val_if_fail (context != NULL, NULL);
181   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
182   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
183 
184   if (FO_IS_TBLR (datatype))
185     {
186       return datatype;
187     }
188   else
189     {
190       gchar *datatype_sprintf = fo_object_sprintf (datatype);
191 
192       g_set_error (error,
193 		   FO_FO_ERROR,
194 		   FO_FO_ERROR_DATATYPE,
195 		   _(fo_fo_error_messages[FO_FO_ERROR_DATATYPE]),
196 		   class_name,
197 		   datatype_sprintf,
198 		   g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
199 
200       g_object_unref (datatype);
201 
202       g_free (datatype_sprintf);
203 
204       return NULL;
205     }
206 }
207 
208 /**
209  * fo_property_padding_get_initial:
210  *
211  * Get an instance of the property with the correct initial value.
212  *
213  * Return value: An instance of the property.
214  **/
215 FoProperty*
fo_property_padding_get_initial(void)216 fo_property_padding_get_initial (void)
217 {
218   static FoProperty *padding = NULL;
219 
220   if (padding == NULL)
221     {
222       padding =
223 	fo_property_padding_new ();
224     }
225 
226   return padding;
227 }
228