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