1 /* Fo
2  * fo-property-text-align.c: 'text-align' property
3  *
4  * Copyright (C) 2001-2005 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-text-align.h"
17 
18 /* text-align */
19 /* Inherited: TRUE */
20 /* Shorthand: FALSE */
21 /* start | center | end | justify | inside | outside | left | right | <string> | inherit */
22 /* Initial value: start */
23 
24 struct _FoPropertyTextAlign
25 {
26   FoProperty parent_instance;
27 };
28 
29 struct _FoPropertyTextAlignClass
30 {
31   FoPropertyClass parent_class;
32 };
33 
34 static void fo_property_text_align_init         (FoPropertyTextAlign      *property_text_align);
35 static void fo_property_text_align_class_init   (FoPropertyTextAlignClass *klass);
36 static void fo_property_text_align_finalize     (GObject       *object);
37 
38 static FoDatatype * fo_property_text_align_resolve_enum (const gchar *token,
39                                                           FoContext   *context,
40                                                           GError     **error);
41 static FoDatatype * fo_property_text_align_validate (FoDatatype *datatype,
42                                                      FoContext  *context,
43                                                      GError    **error);
44 
45 static const gchar class_name[] = "text-align";
46 static gpointer parent_class;
47 
48 /**
49  * fo_property_text_align_get_type:
50  *
51  * Register the #FoPropertyTextAlign type if not already registered and
52  * return its #GType value.
53  *
54  * Return value: #GType of #FoPropertyTextAlign.
55  **/
56 GType
fo_property_text_align_get_type(void)57 fo_property_text_align_get_type (void)
58 {
59   static GType object_type = 0;
60 
61   if (!object_type)
62     {
63       static const GTypeInfo object_info =
64       {
65         sizeof (FoPropertyTextAlignClass),
66         NULL,           /* base_init */
67         NULL,           /* base_finalize */
68         (GClassInitFunc) fo_property_text_align_class_init,
69         NULL,           /* class_finalize */
70         NULL,           /* class_data */
71         sizeof (FoPropertyTextAlign),
72         0,              /* n_preallocs */
73         (GInstanceInitFunc) fo_property_text_align_init,
74 	NULL		/* value_table */
75       };
76 
77       object_type = g_type_register_static (FO_TYPE_PROPERTY,
78                                             class_name,
79                                             &object_info, 0);
80     }
81 
82   return object_type;
83 }
84 
85 /**
86  * fo_property_text_align_init:
87  * @text_align: #FoPropertyTextAlign object to initialise.
88  *
89  * Implements #GInstanceInitFunc for #FoPropertyTextAlign.
90  **/
91 void
fo_property_text_align_init(FoPropertyTextAlign * text_align)92 fo_property_text_align_init (FoPropertyTextAlign *text_align)
93 {
94   FO_PROPERTY (text_align)->value =
95     g_object_ref (fo_enum_factory_get_enum_by_nick ("start"));
96 }
97 
98 /**
99  * fo_property_text_align_class_init:
100  * @klass: #FoPropertyTextAlignClass object to initialise.
101  *
102  * Implements #GClassInitFunc for #FoPropertyTextAlignClass.
103  **/
104 void
fo_property_text_align_class_init(FoPropertyTextAlignClass * klass)105 fo_property_text_align_class_init (FoPropertyTextAlignClass *klass)
106 {
107   GObjectClass *object_class = G_OBJECT_CLASS (klass);
108   FoPropertyClass *property_class = FO_PROPERTY_CLASS (klass);
109 
110   parent_class = g_type_class_peek_parent (klass);
111 
112   object_class->finalize = fo_property_text_align_finalize;
113 
114   property_class->is_inherited = TRUE;
115   property_class->is_shorthand = FALSE;
116   property_class->resolve_enum =
117     fo_property_text_align_resolve_enum;
118   property_class->validate =
119     fo_property_text_align_validate;
120   property_class->get_initial =
121     fo_property_text_align_get_initial;
122 }
123 
124 /**
125  * fo_property_text_align_finalize:
126  * @object: #FoPropertyTextAlign object to finalize.
127  *
128  * Implements #GObjectFinalizeFunc for #FoPropertyTextAlign.
129  **/
130 void
fo_property_text_align_finalize(GObject * object)131 fo_property_text_align_finalize (GObject *object)
132 {
133   FoPropertyTextAlign *text_align;
134 
135   text_align = FO_PROPERTY_TEXT_ALIGN (object);
136 
137   G_OBJECT_CLASS (parent_class)->finalize (object);
138 }
139 
140 
141 /**
142  * fo_property_text_align_new:
143  *
144  * Creates a new #FoPropertyTextAlign initialized to default value.
145  *
146  * Return value: the new #FoPropertyTextAlign.
147  **/
148 FoProperty*
fo_property_text_align_new(void)149 fo_property_text_align_new (void)
150 {
151   FoProperty* text_align;
152 
153   text_align =
154     FO_PROPERTY (g_object_new (fo_property_text_align_get_type (),
155                                NULL));
156 
157   return text_align;
158 }
159 
160 /**
161  * fo_property_text_align_resolve_enum:
162  * @token:   Token from the XML attribute value to be evaluated as an
163  *           enumeration token.
164  * @context: #FoContext object from which to possibly inherit values.
165  * @error:   Information about any error that has occurred.
166  *
167  * Compare @token against the enumeration tokens that are valid for the
168  * current FO property.  If @token is valid, returns either an #FoEnum datatype
169  * representing the enumeration token or a different datatype representing
170  * the enumeration token's resolved value.  If @token is not valid,
171  * sets @error and returns NULL.
172  *
173  * Return value: Resolved enumeration value or NULL.
174  **/
175 FoDatatype*
fo_property_text_align_resolve_enum(const gchar * token,FoContext * context,GError ** error)176 fo_property_text_align_resolve_enum (const gchar *token,
177                                      FoContext   *context,
178                                      GError     **error)
179 {
180   g_return_val_if_fail (token != 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 ((strcmp (token, "start") == 0) ||
185       (strcmp (token, "center") == 0) ||
186       (strcmp (token, "end") == 0) ||
187       (strcmp (token, "justify") == 0) ||
188       (strcmp (token, "inside") == 0) ||
189       (strcmp (token, "outside") == 0) ||
190       (strcmp (token, "left") == 0) ||
191       (strcmp (token, "right") == 0))
192     {
193       return g_object_ref (fo_enum_factory_get_enum_by_nick (token));
194     }
195   else
196     {
197       g_set_error (error,
198 		   FO_FO_ERROR,
199 		   FO_FO_ERROR_ENUMERATION_TOKEN,
200 		   _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
201 		   class_name,
202 		   token);
203       return NULL;
204     }
205 }
206 
207 /**
208  * fo_property_text_align_validate:
209  * @datatype: #FoDatatype to be validated against allowed datatypes and
210  *            values for current property.
211  * @context:  #FoContext object from which to possibly inherit values.
212  * @error:    Information about any error that has occurred.
213  *
214  * Validates @datatype against allowed values.  Returns @datatype, a
215  * replacement datatype value, or NULL if validation failed.
216  *
217  * Return value: Valid datatype value or NULL.
218  **/
219 FoDatatype*
fo_property_text_align_validate(FoDatatype * datatype,FoContext * context,GError ** error)220 fo_property_text_align_validate (FoDatatype *datatype,
221                                  FoContext  *context,
222                                  GError    **error)
223 {
224   FoDatatype *new_datatype;
225   GError     *tmp_error = NULL;
226   gchar      *token;
227 
228   g_return_val_if_fail (datatype != NULL, NULL);
229   g_return_val_if_fail (FO_IS_DATATYPE (datatype), NULL);
230   g_return_val_if_fail (context != NULL, NULL);
231   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
232   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
233 
234   if (FO_IS_ENUM (datatype))
235     {
236       FoEnumEnum value = fo_enum_get_value (datatype);
237 
238       if ((value == FO_ENUM_ENUM_START) ||
239           (value == FO_ENUM_ENUM_CENTER) ||
240           (value == FO_ENUM_ENUM_END) ||
241           (value == FO_ENUM_ENUM_JUSTIFY) ||
242           (value == FO_ENUM_ENUM_INSIDE) ||
243           (value == FO_ENUM_ENUM_OUTSIDE) ||
244           (value == FO_ENUM_ENUM_LEFT) ||
245           (value == FO_ENUM_ENUM_RIGHT))
246 	{
247 	  return datatype;
248 	}
249       else
250 	{
251 	  gchar *datatype_sprintf = fo_object_sprintf (datatype);
252 
253 	  g_set_error (error,
254 		       FO_FO_ERROR,
255 		       FO_FO_ERROR_ENUMERATION_TOKEN,
256 		       _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
257 		       class_name,
258 		       datatype_sprintf,
259 		       g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
260 
261 	  g_object_unref (datatype);
262 
263 	  g_free (datatype_sprintf);
264 
265 	  return NULL;
266 	}
267     }
268   else if (FO_IS_STRING (datatype))
269     {
270       token = fo_string_get_value (datatype);
271 
272       new_datatype =
273         fo_property_text_align_resolve_enum (token, context, &tmp_error);
274 
275       g_object_unref (datatype);
276 
277       fo_propagate_and_return_val_if_error (error, tmp_error, NULL);
278 
279       return new_datatype;
280     }
281   else if (FO_IS_NAME (datatype))
282     {
283       token = fo_name_get_value (datatype);
284 
285       new_datatype =
286         fo_property_text_align_resolve_enum (token, context, &tmp_error);
287 
288       g_object_unref (datatype);
289 
290       fo_propagate_and_return_val_if_error (error, tmp_error, NULL);
291 
292       return new_datatype;
293     }
294   else if (FO_IS_STRING (datatype))
295     {
296       return datatype;
297     }
298   else
299     {
300       gchar *datatype_sprintf = fo_object_sprintf (datatype);
301 
302       g_set_error (error,
303 		   FO_FO_ERROR,
304 		   FO_FO_ERROR_DATATYPE,
305 		   _(fo_fo_error_messages[FO_FO_ERROR_DATATYPE]),
306 		   class_name,
307 		   datatype_sprintf,
308 		   g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
309 
310       g_object_unref (datatype);
311 
312       g_free (datatype_sprintf);
313 
314       return NULL;
315     }
316 }
317 
318 /**
319  * fo_property_text_align_get_initial:
320  *
321  * Get an instance of the property with the correct initial value.
322  *
323  * Return value: An instance of the property.
324  **/
325 FoProperty*
fo_property_text_align_get_initial(void)326 fo_property_text_align_get_initial (void)
327 {
328   static FoProperty *text_align = NULL;
329 
330   if (text_align == NULL)
331     {
332       text_align =
333 	fo_property_text_align_new ();
334     }
335 
336   return text_align;
337 }
338