1 /* Fo
2  * fo-property-widows.c: 'widows' 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-widows.h"
17 
18 
19 #define FO_PROPERTY_WIDOWS_INITIAL	2
20 /* Inherited: TRUE */
21 /* Shorthand: FALSE */
22 /* widows */
23 /* <integer> | inherit */
24 /* NO ENUMERATED VALUE */
25 
26 
27 struct _FoPropertyWidows
28 {
29   FoProperty parent_instance;
30 };
31 
32 struct _FoPropertyWidowsClass
33 {
34   FoPropertyClass parent_class;
35 };
36 
37 static void fo_property_widows_init         (FoPropertyWidows      *property_widows);
38 static void fo_property_widows_class_init   (FoPropertyWidowsClass *klass);
39 static void fo_property_widows_finalize     (GObject       *object);
40 
41 static FoDatatype* fo_property_widows_resolve_enum (const gchar *token,
42                                                     FoContext   *context,
43                                                     GError     **error);
44 static FoDatatype* fo_property_widows_validate (FoDatatype *datatype,
45                                                 FoContext  *context,
46                                                 GError    **error);
47 
48 static const gchar class_name[] = "widows";
49 static gpointer parent_class;
50 
51 /**
52  * fo_property_widows_get_type:
53  *
54  * Register the #FoPropertyWidows type if not already registered and
55  * return its #GType value.
56  *
57  * Return value: #GType of #FoPropertyWidows.
58  **/
59 GType
fo_property_widows_get_type(void)60 fo_property_widows_get_type (void)
61 {
62   static GType object_type = 0;
63 
64   if (!object_type)
65     {
66       static const GTypeInfo object_info =
67       {
68         sizeof (FoPropertyWidowsClass),
69         NULL,           /* base_init */
70         NULL,           /* base_finalize */
71         (GClassInitFunc) fo_property_widows_class_init,
72         NULL,           /* class_finalize */
73         NULL,           /* class_data */
74         sizeof (FoPropertyWidows),
75         0,              /* n_preallocs */
76         (GInstanceInitFunc) fo_property_widows_init,
77 	NULL		/* value_table */
78       };
79 
80       object_type = g_type_register_static (FO_TYPE_PROPERTY,
81                                             class_name,
82                                             &object_info, 0);
83     }
84 
85   return object_type;
86 }
87 
88 /**
89  * fo_property_widows_init:
90  * @widows: #FoPropertyWidows object to initialise.
91  *
92  * Implements #GInstanceInitFunc for #FoPropertyWidows.
93  **/
94 void
fo_property_widows_init(FoPropertyWidows * widows)95 fo_property_widows_init (FoPropertyWidows *widows)
96 {
97   FO_PROPERTY (widows)->value =
98     g_object_ref (g_object_new (FO_TYPE_INTEGER,
99 				"value",
100 				FO_PROPERTY_WIDOWS_INITIAL,
101 				NULL));
102 }
103 
104 /**
105  * fo_property_widows_class_init:
106  * @klass: #FoPropertyWidowsClass object to initialise.
107  *
108  * Implements #GClassInitFunc for #FoPropertyWidowsClass.
109  **/
110 void
fo_property_widows_class_init(FoPropertyWidowsClass * klass)111 fo_property_widows_class_init (FoPropertyWidowsClass *klass)
112 {
113   GObjectClass *object_class = G_OBJECT_CLASS (klass);
114   FoPropertyClass *property_class = FO_PROPERTY_CLASS (klass);
115 
116   parent_class = g_type_class_peek_parent (klass);
117 
118   object_class->finalize = fo_property_widows_finalize;
119 
120   property_class->is_inherited = TRUE;
121   property_class->is_shorthand = FALSE;
122   property_class->resolve_enum =
123     fo_property_widows_resolve_enum;
124   property_class->validate =
125     fo_property_widows_validate;
126   property_class->get_initial =
127     fo_property_widows_get_initial;
128 }
129 
130 /**
131  * fo_property_widows_finalize:
132  * @object: #FoPropertyWidows object to finalize.
133  *
134  * Implements #GObjectFinalizeFunc for #FoPropertyWidows.
135  **/
136 void
fo_property_widows_finalize(GObject * object)137 fo_property_widows_finalize (GObject *object)
138 {
139   FoPropertyWidows *widows;
140 
141   widows = FO_PROPERTY_WIDOWS (object);
142 
143   G_OBJECT_CLASS (parent_class)->finalize (object);
144 }
145 
146 
147 /**
148  * fo_property_widows_new:
149  *
150  * Creates a new #FoPropertyWidows initialized to default value.
151  *
152  * Return value: the new #FoPropertyWidows.
153  **/
154 FoProperty*
fo_property_widows_new(void)155 fo_property_widows_new (void)
156 {
157   FoProperty* widows;
158 
159   widows =
160     FO_PROPERTY (g_object_new (fo_property_widows_get_type (),
161                                NULL));
162 
163   return widows;
164 }
165 
166 /**
167  * fo_property_widows_resolve_enum:
168  * @token:   Token from the XML attribute value to be evaluated as an
169  *           enumeration token.
170  * @context: #FoContext object from which to possibly inherit values.
171  * @error:   Information about any error that has occurred.
172  *
173  * Compare @token against the enumeration tokens that are valid for the
174  * current FO property.  If @token is valid, returns either an #FoEnum datatype
175  * representing the enumeration token or a different datatype representing
176  * the enumeration token's resolved value.  If @token is not valid,
177  * sets @error and returns NULL.
178  *
179  * Return value: Resolved enumeration value or NULL.
180  **/
181 FoDatatype*
fo_property_widows_resolve_enum(const gchar * token,FoContext * context,GError ** error)182 fo_property_widows_resolve_enum (const gchar *token,
183                                  FoContext   *context,
184                                  GError     **error)
185 {
186   g_return_val_if_fail (token != NULL, NULL);
187   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
188   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
189 
190 
191       g_set_error (error,
192 		   FO_FO_ERROR,
193 		   FO_FO_ERROR_ENUMERATION_TOKEN,
194 		   _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
195 		   class_name,
196 		   token);
197       return NULL;
198 }
199 
200 /**
201  * fo_property_widows_validate:
202  * @datatype: #FoDatatype to be validated against allowed datatypes and
203  *            values for current property.
204  * @context:  #FoContext object from which to possibly inherit values.
205  * @error:    Information about any error that has occurred.
206  *
207  * Validates @datatype against allowed values.  Returns @datatype, a
208  * replacement datatype value, or NULL if validation failed.
209  *
210  * Return value: Valid datatype value or NULL.
211  **/
212 FoDatatype*
fo_property_widows_validate(FoDatatype * datatype,FoContext * context,GError ** error)213 fo_property_widows_validate (FoDatatype *datatype,
214                              FoContext  *context,
215                              GError    **error)
216 {
217   FoDatatype *new_datatype;
218 
219   g_return_val_if_fail (datatype != NULL, NULL);
220   g_return_val_if_fail (FO_IS_DATATYPE (datatype), NULL);
221   g_return_val_if_fail (context != NULL, NULL);
222   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
223   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
224 
225   if (FO_IS_INTEGER (datatype))
226     {
227       return datatype;
228     }
229   else if (FO_IS_NUMBER (datatype))
230     {
231       new_datatype =
232 	fo_integer_new_with_value ((gint) fo_number_get_value (datatype));
233 
234       g_object_unref (datatype);
235 
236       return new_datatype;
237     }
238   else
239     {
240       gchar *datatype_sprintf = fo_object_sprintf (datatype);
241 
242       g_set_error (error,
243 		   FO_FO_ERROR,
244 		   FO_FO_ERROR_DATATYPE,
245 		   _(fo_fo_error_messages[FO_FO_ERROR_DATATYPE]),
246 		   class_name,
247 		   datatype_sprintf,
248 		   g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
249 
250       g_object_unref (datatype);
251 
252       g_free (datatype_sprintf);
253 
254       return NULL;
255     }
256 }
257 
258 /**
259  * fo_property_widows_get_initial:
260  *
261  * Get an instance of the property with the correct initial value.
262  *
263  * Return value: An instance of the property.
264  **/
265 FoProperty*
fo_property_widows_get_initial(void)266 fo_property_widows_get_initial (void)
267 {
268   static FoProperty *widows = NULL;
269 
270   if (widows == NULL)
271     {
272       widows = fo_property_widows_new ();
273     }
274 
275   return widows;
276 }
277