1 /* Fo
2 * fo-percentage.c: Percentage datatype
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 "fo-utils.h"
11 #include "fo-datatype.h"
12 #include "fo-datatype-private.h"
13 #include "fo-numeric-private.h"
14 #include "fo-percentage.h"
15
16
17 enum {
18 PROP_0,
19 PROP_VALUE
20 };
21
22 struct _FoPercentage
23 {
24 FoDatatype parent_instance;
25
26 gdouble value;
27 };
28
29 struct _FoPercentageClass
30 {
31 FoDatatypeClass parent_class;
32
33 };
34
35 static void fo_percentage_init (FoPercentage *percentage);
36 static void fo_percentage_class_init (FoPercentageClass *klass);
37 static void fo_percentage_set_property (GObject *object,
38 guint prop_id,
39 const GValue *value,
40 GParamSpec *pspec);
41 static void fo_percentage_get_property (GObject *object,
42 guint prop_id,
43 GValue *value,
44 GParamSpec *pspec);
45 static void fo_percentage_finalize (GObject *object);
46
47 static FoDatatype* fo_percentage_copy (FoDatatype *datatype);
48 static gchar* fo_percentage_sprintf (FoObject *object);
49
50 static gpointer parent_class;
51
52 /**
53 * fo_percentage_get_type:
54 *
55 * Register the #FoPercentage object type.
56 *
57 * Return value: #GType value of the #FoPercentage object type.
58 **/
59 GType
fo_percentage_get_type(void)60 fo_percentage_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 (FoPercentageClass),
69 (GBaseInitFunc) NULL,
70 (GBaseFinalizeFunc) NULL,
71 (GClassInitFunc) fo_percentage_class_init,
72 NULL, /* class_finalize */
73 NULL, /* class_data */
74 sizeof (FoPercentage),
75 0, /* n_preallocs */
76 (GInstanceInitFunc) fo_percentage_init,
77 NULL /* value_table */
78 };
79
80 object_type = g_type_register_static (FO_TYPE_DATATYPE,
81 "FoPercentage",
82 &object_info, 0);
83 }
84
85 return object_type;
86 }
87
88 /**
89 * fo_percentage_init:
90 * @percentage: #FoPercentage object to initialise.
91 *
92 * Implements #GInstanceInitFunc for #FoPercentage.
93 **/
94 void
fo_percentage_init(FoPercentage * percentage)95 fo_percentage_init (FoPercentage *percentage)
96 {
97 percentage->value = 0;
98 }
99
100 /**
101 * fo_percentage_class_init:
102 * @klass: #FoPercentageClass object to initialise.
103 *
104 * Implements #GClassInitFunc for #FoPercentageClass.
105 **/
106 void
fo_percentage_class_init(FoPercentageClass * klass)107 fo_percentage_class_init (FoPercentageClass *klass)
108 {
109 GObjectClass *object_class = G_OBJECT_CLASS (klass);
110
111 parent_class = g_type_class_peek_parent (klass);
112
113 object_class->finalize = fo_percentage_finalize;
114
115 object_class->set_property = fo_percentage_set_property;
116 object_class->get_property = fo_percentage_get_property;
117
118 g_object_class_install_property (object_class,
119 PROP_VALUE,
120 g_param_spec_double ("value",
121 _("Value"),
122 _("Percentage value"),
123 -G_MAXDOUBLE,
124 G_MAXDOUBLE,
125 0,
126 G_PARAM_READWRITE));
127
128 FO_DATATYPE_CLASS (klass)->copy = fo_percentage_copy;
129 FO_OBJECT_CLASS (klass)->print_sprintf = fo_percentage_sprintf;
130 }
131
132 /**
133 * fo_percentage_finalize:
134 * @object: #FoPercentage object to finalize.
135 *
136 * Implements #GObjectFinalizeFunc for #FoPercentage.
137 **/
138 void
fo_percentage_finalize(GObject * object)139 fo_percentage_finalize (GObject *object)
140 {
141 FoPercentage *percentage;
142
143 percentage = FO_PERCENTAGE (object);
144
145 G_OBJECT_CLASS (parent_class)->finalize (object);
146 }
147
148
149 /**
150 * fo_percentage_get_property:
151 * @object: #GObject whose property will be retrieved.
152 * @prop_id: Property ID assigned when property registered.
153 * @value: #GValue to set with property value.
154 * @pspec: Parameter specification for this property type.
155 *
156 * Implements #GObjectGetPropertyFunc for #FoPercentage.
157 **/
158 void
fo_percentage_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)159 fo_percentage_get_property (GObject *object,
160 guint prop_id,
161 GValue *value,
162 GParamSpec *pspec)
163 {
164 FoDatatype *percentage;
165
166 percentage = FO_DATATYPE (object);
167
168 switch (prop_id)
169 {
170 case PROP_VALUE:
171 g_value_set_double (value, fo_percentage_get_value (percentage));
172 break;
173 default:
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 break;
176 }
177 }
178
179 /**
180 * fo_percentage_set_property:
181 * @object: #GObject whose property will be set.
182 * @prop_id: Property ID assigned when property registered.
183 * @value: New value for property.
184 * @pspec: Parameter specification for this property type.
185 *
186 * Implements #GObjectSetPropertyFunc for #FoPercentage.
187 **/
188 void
fo_percentage_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)189 fo_percentage_set_property (GObject *object,
190 guint prop_id,
191 const GValue *value,
192 GParamSpec *pspec)
193 {
194 FoDatatype *percentage;
195
196 percentage = FO_DATATYPE (object);
197
198 switch (prop_id)
199 {
200 case PROP_VALUE:
201 fo_percentage_set_value (percentage, g_value_get_double (value));
202 break;
203 default:
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 break;
206 }
207 }
208
209 /**
210 * fo_percentage_new:
211 *
212 * Creates a new #FoPercentage initialized to default value.
213 *
214 * Return value: the new #FoPercentage.
215 **/
216 FoDatatype *
fo_percentage_new(void)217 fo_percentage_new (void)
218 {
219 FoDatatype *percentage;
220
221 percentage = FO_DATATYPE (g_object_new (fo_percentage_get_type (),
222 NULL));
223
224 return percentage;
225 }
226
227 /**
228 * fo_percentage_new_with_value:
229 * @value: Value of new #FoPercentage.
230 *
231 * Creates a new #FoPercentage initialized to value of @value. For
232 * example, fo_percentage_new_with_value(100) creates a new
233 * #FoPercentage with value equal to 100%.
234 *
235 * Return value: the new #FoPercentage.
236 **/
237 FoDatatype *
fo_percentage_new_with_value(gdouble value)238 fo_percentage_new_with_value (gdouble value)
239 {
240 FoDatatype *percentage;
241
242 percentage = fo_percentage_new ();
243
244 fo_percentage_set_value (percentage, value);
245
246 return percentage;
247 }
248
249 /**
250 * fo_percentage_get_value:
251 * @percentage: #FoPercentage.
252 *
253 * Get the value of @percentage.
254 *
255 * Return value: Numeric value of @percentage.
256 **/
257 gdouble
fo_percentage_get_value(FoDatatype * percentage)258 fo_percentage_get_value (FoDatatype *percentage)
259 {
260 g_return_val_if_fail (percentage != NULL, 0);
261 g_return_val_if_fail (FO_IS_PERCENTAGE (percentage), 0);
262
263 return ((FoPercentage *) percentage)->value;
264 }
265
266 /**
267 * fo_percentage_set_value:
268 * @percentage: #FoPercentage.
269 * @new_value: New value for @percentage.
270 *
271 * Set the value of @percentage.
272 **/
273 void
fo_percentage_set_value(FoDatatype * percentage,gdouble new_value)274 fo_percentage_set_value (FoDatatype *percentage,
275 gdouble new_value)
276 {
277 g_return_if_fail (percentage != NULL);
278 g_return_if_fail (FO_IS_PERCENTAGE (percentage));
279
280 ((FoPercentage *) percentage)->value = new_value;
281 /*g_object_notify(G_OBJECT(percentage), "value");*/
282 }
283
284 /**
285 * fo_percentage_copy:
286 * @datatype: #FoPercentage to be copied.
287 *
288 * Create a copy of @datatype.
289 *
290 * Return value: New #FoPercentage.
291 **/
292 FoDatatype*
fo_percentage_copy(FoDatatype * datatype)293 fo_percentage_copy(FoDatatype *datatype)
294 {
295 FoDatatype* percentage;
296
297 g_return_val_if_fail (datatype != NULL, NULL);
298 g_return_val_if_fail (FO_IS_PERCENTAGE (datatype), NULL);
299
300 percentage = fo_percentage_new ();
301 FO_PERCENTAGE (percentage)->value = FO_PERCENTAGE (datatype)->value;
302
303 return (percentage);
304 }
305
306 /**
307 * fo_percentage_sprintf:
308 * @object: #FoPercentage to be printed.
309 *
310 * Creates string representation of value of @object.
311 *
312 * String must be freed by caller.
313 *
314 * Return value: String representation of @object.
315 **/
316 gchar*
fo_percentage_sprintf(FoObject * object)317 fo_percentage_sprintf (FoObject *object)
318 {
319 g_return_val_if_fail (object != NULL, NULL);
320 g_return_val_if_fail (FO_IS_PERCENTAGE (object), NULL);
321
322 return (g_strdup_printf("%0g%%", FO_PERCENTAGE (object)->value));
323 }
324
325 /**
326 * fo_percentage_get_percentage_hundred:
327 *
328 * Get an #FoPercentage with the well-known value of 100%.
329 *
330 * Return value: The #FoPercentage.
331 **/
332 FoDatatype*
fo_percentage_get_percentage_hundred(void)333 fo_percentage_get_percentage_hundred (void)
334 {
335 static FoDatatype *percentage = NULL;
336
337 if (percentage == NULL)
338 {
339 percentage = fo_percentage_new_with_value (100.0);
340 }
341
342 return percentage;
343 }
344