1 /* Fo
2 * fo-property-number-rows-spanned.c: 'number-rows-spanned' 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-rows-spanned.h"
18 #include "property/fo-property-util.h"
19
20 /* number-rows-spanned */
21 /* Inherited: FALSE */
22 /* Shorthand: FALSE */
23 /* <number> */
24 /* Initial value: 1 */
25
26 struct _FoPropertyNumberRowsSpanned
27 {
28 FoProperty parent_instance;
29 };
30
31 struct _FoPropertyNumberRowsSpannedClass
32 {
33 FoPropertyClass parent_class;
34 };
35
36 static void fo_property_number_rows_spanned_init (FoPropertyNumberRowsSpanned *property_number_rows_spanned);
37 static void fo_property_number_rows_spanned_class_init (FoPropertyNumberRowsSpannedClass *klass);
38 static void fo_property_number_rows_spanned_finalize (GObject *object);
39
40 static FoDatatype* fo_property_number_rows_spanned_validate (FoDatatype *datatype,
41 FoContext *context,
42 GError **error);
43
44 static const gchar class_name[] = "number-rows-spanned";
45 static gpointer parent_class;
46
47 /**
48 * fo_property_number_rows_spanned_get_type:
49 *
50 * Register the #FoPropertyNumberRowsSpanned type if not already registered and
51 * return its #GType value.
52 *
53 * Return value: #GType of #FoPropertyNumberRowsSpanned.
54 **/
55 GType
fo_property_number_rows_spanned_get_type(void)56 fo_property_number_rows_spanned_get_type (void)
57 {
58 static GType object_type = 0;
59
60 if (!object_type)
61 {
62 static const GTypeInfo object_info =
63 {
64 sizeof (FoPropertyNumberRowsSpannedClass),
65 NULL, /* base_init */
66 NULL, /* base_finalize */
67 (GClassInitFunc) fo_property_number_rows_spanned_class_init,
68 NULL, /* class_finalize */
69 NULL, /* class_data */
70 sizeof (FoPropertyNumberRowsSpanned),
71 0, /* n_preallocs */
72 (GInstanceInitFunc) fo_property_number_rows_spanned_init,
73 NULL /* value_table */
74 };
75
76 object_type = g_type_register_static (FO_TYPE_PROPERTY,
77 class_name,
78 &object_info, 0);
79 }
80
81 return object_type;
82 }
83
84 /**
85 * fo_property_number_rows_spanned_init:
86 * @number_rows_spanned: #FoPropertyNumberRowsSpanned object to initialise.
87 *
88 * Implements #GInstanceInitFunc for #FoPropertyNumberRowsSpanned.
89 **/
90 void
fo_property_number_rows_spanned_init(FoPropertyNumberRowsSpanned * number_rows_spanned)91 fo_property_number_rows_spanned_init (FoPropertyNumberRowsSpanned *number_rows_spanned)
92 {
93 FO_PROPERTY (number_rows_spanned)->value =
94 g_object_ref (fo_integer_get_integer_one ());
95 }
96
97 /**
98 * fo_property_number_rows_spanned_class_init:
99 * @klass: #FoPropertyNumberRowsSpannedClass object to initialise.
100 *
101 * Implements #GClassInitFunc for #FoPropertyNumberRowsSpannedClass.
102 **/
103 void
fo_property_number_rows_spanned_class_init(FoPropertyNumberRowsSpannedClass * klass)104 fo_property_number_rows_spanned_class_init (FoPropertyNumberRowsSpannedClass *klass)
105 {
106 GObjectClass *object_class = G_OBJECT_CLASS (klass);
107 FoPropertyClass *property_class = FO_PROPERTY_CLASS (klass);
108
109 parent_class = g_type_class_peek_parent (klass);
110
111 object_class->finalize = fo_property_number_rows_spanned_finalize;
112
113 property_class->is_inherited = FALSE;
114 property_class->is_shorthand = FALSE;
115 property_class->resolve_enum =
116 fo_property_util_resolve_no_enum;
117 property_class->validate =
118 fo_property_number_rows_spanned_validate;
119 property_class->get_initial =
120 fo_property_number_rows_spanned_get_initial;
121 }
122
123 /**
124 * fo_property_number_rows_spanned_finalize:
125 * @object: #FoPropertyNumberRowsSpanned object to finalize.
126 *
127 * Implements #GObjectFinalizeFunc for #FoPropertyNumberRowsSpanned.
128 **/
129 void
fo_property_number_rows_spanned_finalize(GObject * object)130 fo_property_number_rows_spanned_finalize (GObject *object)
131 {
132 FoPropertyNumberRowsSpanned *number_rows_spanned;
133
134 number_rows_spanned = FO_PROPERTY_NUMBER_ROWS_SPANNED (object);
135
136 G_OBJECT_CLASS (parent_class)->finalize (object);
137 }
138
139
140 /**
141 * fo_property_number_rows_spanned_new:
142 *
143 * Creates a new #FoPropertyNumberRowsSpanned initialized to default value.
144 *
145 * Return value: the new #FoPropertyNumberRowsSpanned.
146 **/
147 FoProperty*
fo_property_number_rows_spanned_new(void)148 fo_property_number_rows_spanned_new (void)
149 {
150 FoProperty* number_rows_spanned;
151
152 number_rows_spanned =
153 FO_PROPERTY (g_object_new (fo_property_number_rows_spanned_get_type (),
154 NULL));
155
156 return number_rows_spanned;
157 }
158
159 /**
160 * fo_property_number_rows_spanned_validate:
161 * @datatype: #FoDatatype to be validated against allowed datatypes and
162 * values for current property.
163 * @context: #FoContext object from which to possibly inherit values.
164 * @error: Information about any error that has occurred.
165 *
166 * Validates @datatype against allowed values. Returns @datatype, a
167 * replacement datatype value, or NULL if validation failed.
168 *
169 * Return value: Valid datatype value or NULL.
170 **/
171 FoDatatype*
fo_property_number_rows_spanned_validate(FoDatatype * datatype,FoContext * context,GError ** error)172 fo_property_number_rows_spanned_validate (FoDatatype *datatype,
173 FoContext *context,
174 GError **error)
175 {
176 g_return_val_if_fail (datatype != NULL, NULL);
177 g_return_val_if_fail (FO_IS_DATATYPE (datatype), NULL);
178 g_return_val_if_fail (context != NULL, NULL);
179 g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
180 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
181
182 if (FO_IS_NUMBER (datatype))
183 {
184 FoDatatype *new_datatype;
185 gfloat number_value = fo_number_get_value (datatype);
186 gint integer_value = number_value + 0.5;
187
188 g_object_unref (datatype);
189
190 new_datatype = fo_integer_new_with_value (MAX (integer_value, 1));
191
192 return new_datatype;
193 }
194 else if (FO_IS_INTEGER (datatype))
195 {
196 return datatype;
197 }
198 else
199 {
200 gchar *datatype_sprintf = fo_object_sprintf (datatype);
201
202 g_set_error (error,
203 FO_FO_ERROR,
204 FO_FO_ERROR_DATATYPE,
205 _(fo_fo_error_messages[FO_FO_ERROR_DATATYPE]),
206 class_name,
207 datatype_sprintf,
208 g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
209
210 g_object_unref (datatype);
211
212 g_free (datatype_sprintf);
213
214 return NULL;
215 }
216 }
217
218 /**
219 * fo_property_number_rows_spanned_get_initial:
220 *
221 * Get an instance of the property with the correct initial value.
222 *
223 * Return value: An instance of the property.
224 **/
225 FoProperty*
fo_property_number_rows_spanned_get_initial(void)226 fo_property_number_rows_spanned_get_initial (void)
227 {
228 static FoProperty *number_rows_spanned = NULL;
229
230 if (number_rows_spanned == NULL)
231 {
232 number_rows_spanned =
233 fo_property_number_rows_spanned_new ();
234 }
235
236 return number_rows_spanned;
237 }
238