doc: refactored to be more mainstream
[adg.git] / src / adg / adg-param-dress.c
blob6ee55e4b9378bb28692a5459b970e79330c1b50b
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-param-dress
23 * @Section_Id:AdgParamDress
24 * @title: AdgParamDress
25 * @short_description: Metadata for dress specification
27 * The %ADG_TYPE_PARAM_DRESS type is a parameter specification
28 * compatible with %G_TYPE_PARAM_ENUM that provides additional
29 * validation: it rejects values that are incompatibles (that
30 * is, that are not related) with the current one. Check the
31 * adg_dress_is_related() documentation for details on what
32 * "related" means.
34 * Internally, the value setting is performed by calling the
35 * adg_dress_set() API.
37 * Since: 1.0
38 **/
41 #include "adg-internal.h"
42 #include "adg-dress.h"
44 #include "adg-param-dress.h"
47 typedef struct _AdgParamSpecDress AdgParamSpecDress;
49 struct _AdgParamSpecDress {
50 GParamSpecEnum parent;
51 AdgDress source_dress;
55 static void _adg_param_dress_init (GParamSpec *pspec);
56 static void _adg_param_dress_set_default (GParamSpec *pspec,
57 GValue *value);
58 static gboolean _adg_param_dress_validate (GParamSpec *pspec,
59 GValue *value);
60 static gint _adg_param_dress_cmp (GParamSpec *pspec,
61 const GValue *value1,
62 const GValue *value2);
65 GType
66 adg_param_dress_get_type(void)
68 static GType type = 0;
70 if (G_UNLIKELY(type == 0)) {
71 /* const */ GParamSpecTypeInfo pspec_info = {
72 sizeof(AdgParamSpecDress), /* instance_size */
73 0, /* n_preallocs */
74 _adg_param_dress_init,
75 G_TYPE_INVALID, /* value_type */
76 NULL, /* finalize */
77 _adg_param_dress_set_default,
78 _adg_param_dress_validate,
79 _adg_param_dress_cmp,
81 pspec_info.value_type = ADG_TYPE_DRESS;
82 type = g_param_type_register_static(g_intern_static_string("AdgParamDress"),
83 &pspec_info);
86 return type;
90 /**
91 * adg_param_spec_dress:
92 * @name: canonical name
93 * @nick: nickname of the param
94 * @blurb: brief desciption
95 * @dress: the #AdgDress dress
96 * @flags: a combination of #GParamFlags
98 * Creates a param spec to hold a dress value. This is similar to
99 * g_param_spec_enum() but rejects a new dress value if it is not
100 * related with the old one. The setting is performed via
101 * adg_dress_set(), so check its documentation for details.
103 * Returns: (transfer full): the newly allocated #GParamSpec.
105 * Since: 1.0
107 GParamSpec *
108 adg_param_spec_dress(const gchar *name, const gchar *nick, const gchar *blurb,
109 AdgDress dress, GParamFlags flags)
111 AdgParamSpecDress *dspec;
113 dspec = g_param_spec_internal(ADG_TYPE_PARAM_DRESS,
114 name, nick, blurb, flags);
115 dspec->source_dress = dress;
117 return (GParamSpec *) dspec;
121 static void
122 _adg_param_dress_init(GParamSpec *pspec)
124 AdgParamSpecDress *dspec = ADG_PARAM_SPEC_DRESS(pspec);
125 dspec->source_dress = ADG_DRESS_UNDEFINED;
128 static void
129 _adg_param_dress_set_default(GParamSpec *pspec,
130 GValue *value)
132 value->data[0].v_long = ADG_PARAM_SPEC_DRESS(pspec)->source_dress;
135 static gboolean
136 _adg_param_dress_validate(GParamSpec *pspec, GValue *value)
138 AdgParamSpecDress *dspec;
139 AdgDress *dress;
140 AdgDress wanted_dress;
142 dspec = ADG_PARAM_SPEC_DRESS(pspec);
143 dress = (AdgDress *) &value->data[0].v_long;
144 wanted_dress = *dress;
146 /* Fallback to the source dress, returned in case of errors */
147 *dress = dspec->source_dress;
149 /* This method will fail (that is, it leaves *dress untouched)
150 * if the current *dress value (source_dress) and wanted_dress
151 * are not related */
152 adg_dress_set(dress, wanted_dress);
154 return *dress != wanted_dress;
157 static gint
158 _adg_param_dress_cmp(GParamSpec *pspec,
159 const GValue *value1,
160 const GValue *value2)
162 glong v1 = value1->data[0].v_long;
163 glong v2 = value2->data[0].v_long;
165 return v1 < v2 ? -1 : (v1 > v2);