~macslow/notify-osd/make-distcheck-pain

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* -*- mode:C; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Authored By: Tomas Frydrych  <tf@openedhand.com>
 *              Emmanuele Bassi  <ebassi@openedhand.com>
 *
 * Copyright (C) 2007 OpenedHand
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:egg-units
 * @short_description: A logical distance unit.
 *
 * Egg units are logical units with granularity greater than that of the
 * device units; they are used by #EggActorBox and the units-based family
 * of #EggActor functions. To convert between Egg units and device
 * units, use %EGG_UNITS_FROM_DEVICE and %EGG_UNITS_TO_DEVICE macros.
 *
 * #EggUnit<!-- -->s can be converted from other units like millimeters,
 * typographic points (at the current resolution) and percentages. It is
 * also possible to convert fixed point values to and from #EggUnit
 * values.
 *
 * In order to register a #EggUnit property, the #EggParamSpecUnit
 * #GParamSpec sub-class should be used:
 *
 * |[
 *   GParamSpec *pspec;
 *
 *   pspec = egg_param_spec_unit ("width",
 *                                    "Width",
 *                                    "Width of the actor, in units",
 *                                    0, EGG_MAXUNIT,
 *                                    0,
 *                                    G_PARAM_READWRITE);
 *   g_object_class_install_property (gobject_class, PROP_WIDTH, pspec);
 * ]|
 *
 * A #GValue holding units can be manipulated using egg_value_set_unit()
 * and egg_value_get_unit(). #GValue<!-- -->s containing a #EggUnit
 * value can also be transformed to #GValue<!-- -->s containing integer
 * values - with a loss of precision:
 *
 * |[
 *   static gboolean
 *   units_to_int (const GValue *src,
 *                 GValue       *dest)
 *   {
 *     g_return_val_if_fail (EGG_VALUE_HOLDS_UNIT (src), FALSE);
 *
 *     g_value_init (dest, G_TYPE_INT);
 *     return g_value_transform (src, &dest);
 *   }
 * ]|
 *
 * The code above is equivalent to:
 *
 * |[
 *   static gboolean
 *   units_to_int (const GValue *src,
 *                 GValue       *dest)
 *   {
 *     g_return_val_if_fail (EGG_VALUE_HOLDS_UNIT (src), FALSE);
 *
 *     g_value_init (dest, G_TYPE_INT);
 *     g_value_set_int (dest,
 *                      EGG_UNITS_TO_INT (egg_value_get_unit (src)));
 *
 *     return TRUE;
 *   }
 * ]|
 *
 * #EggUnit is available since Egg 0.4
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <glib-object.h>
#include <gobject/gvaluecollector.h>

#include "egg-units.h"
// #include "egg-private.h"
#include "egg-hack.h"

static GTypeInfo _info = {
 0,
 NULL,
 NULL,
 NULL,
 NULL,
 NULL,
 0,
 0,
 NULL,
 NULL,
};

static GTypeFundamentalInfo _finfo = { 0, };

static void
egg_value_init_unit (GValue *value)
{
  value->data[0].v_int = 0;
}

static void
egg_value_copy_unit (const GValue *src,
                         GValue       *dest)
{
  dest->data[0].v_int = src->data[0].v_int;
}

static gchar *
egg_value_collect_unit (GValue      *value,
                            guint        n_collect_values,
                            GTypeCValue *collect_values,
                            guint        collect_flags)
{
  value->data[0].v_int = collect_values[0].v_int;

  return NULL;
}

static gchar *
egg_value_lcopy_unit (const GValue *value,
                          guint         n_collect_values,
                          GTypeCValue  *collect_values,
                          guint         collect_flags)
{
  gint32 *units_p = collect_values[0].v_pointer;

  if (!units_p)
    return g_strdup_printf ("value location for `%s' passed as NULL",
                            G_VALUE_TYPE_NAME (value));

  *units_p = value->data[0].v_int;

  return NULL;
}

static void
egg_value_transform_unit_int (const GValue *src,
                                  GValue       *dest)
{
  dest->data[0].v_int = EGG_UNITS_TO_INT (src->data[0].v_int);
}

static void
egg_value_transform_int_unit (const GValue *src,
                                  GValue       *dest)
{
  dest->data[0].v_int = EGG_UNITS_FROM_INT (src->data[0].v_int);
}

static const GTypeValueTable _egg_unit_value_table = {
  egg_value_init_unit,
  NULL,
  egg_value_copy_unit,
  NULL,
  "i",
  egg_value_collect_unit,
  "p",
  egg_value_lcopy_unit
};

GType
egg_unit_get_type (void)
{
  static GType _egg_unit_type = 0;

  if (G_UNLIKELY (_egg_unit_type == 0))
    {
      _info.value_table = & _egg_unit_value_table;
      _egg_unit_type =
        g_type_register_fundamental (g_type_fundamental_next (),
                                     I_("EggUnit"),
                                     &_info, &_finfo, 0);

      g_value_register_transform_func (_egg_unit_type, G_TYPE_INT,
                                       egg_value_transform_unit_int);
      g_value_register_transform_func (G_TYPE_INT, _egg_unit_type,
                                       egg_value_transform_int_unit);
    }

  return _egg_unit_type;
}

/**
 * egg_value_set_unit:
 * @value: a #GValue initialized to #EGG_TYPE_UNIT
 * @units: the units to set
 *
 * Sets @value to @units
 *
 * Since: 0.8
 */
void
egg_value_set_unit (GValue      *value,
                        EggUnit  units)
{
  g_return_if_fail (EGG_VALUE_HOLDS_UNIT (value));

  value->data[0].v_int = units;
}

/**
 * egg_value_get_unit:
 * @value: a #GValue initialized to #EGG_TYPE_UNIT
 *
 * Gets the #EggUnit<!-- -->s contained in @value.
 *
 * Return value: the units inside the passed #GValue
 *
 * Since: 0.8
 */
EggUnit
egg_value_get_unit (const GValue *value)
{
  g_return_val_if_fail (EGG_VALUE_HOLDS_UNIT (value), 0);

  return value->data[0].v_int;
}

static void
param_unit_init (GParamSpec *pspec)
{
  EggParamSpecUnit *uspec = EGG_PARAM_SPEC_UNIT (pspec);

  uspec->minimum = EGG_MINUNIT;
  uspec->maximum = EGG_MAXUNIT;
  uspec->default_value = 0;
}

static void
param_unit_set_default (GParamSpec *pspec,
                        GValue     *value)
{
  value->data[0].v_int = EGG_PARAM_SPEC_UNIT (pspec)->default_value;
}

static gboolean
param_unit_validate (GParamSpec *pspec,
                     GValue     *value)
{
  EggParamSpecUnit *uspec = EGG_PARAM_SPEC_UNIT (pspec);
  gint oval = EGG_UNITS_TO_INT (value->data[0].v_int);
  gint min, max, val;

  g_assert (EGG_IS_PARAM_SPEC_UNIT (pspec));

  /* we compare the integer part of the value because the minimum
   * and maximum values cover just that part of the representation
   */
  min = uspec->minimum; 
  max = uspec->maximum;
  val = EGG_UNITS_TO_INT (value->data[0].v_int);

  val = CLAMP (val, min, max);
  if (val != oval)
    {
      value->data[0].v_int = val;
      return TRUE;
    }

  return FALSE;
}

static gint
param_unit_values_cmp (GParamSpec   *pspec,
                       const GValue *value1,
                       const GValue *value2)
{
  if (value1->data[0].v_int < value2->data[0].v_int)
    return -1;
  else
    return value1->data[0].v_int > value2->data[0].v_int;
}

GType
egg_param_unit_get_type (void)
{
  static GType pspec_type = 0;

  if (G_UNLIKELY (pspec_type == 0))
    {
      const GParamSpecTypeInfo pspec_info = {
        sizeof (EggParamSpecUnit),
        16,
        param_unit_init,
        EGG_TYPE_UNIT,
        NULL,
        param_unit_set_default,
        param_unit_validate,
        param_unit_values_cmp,
      };

      pspec_type = g_param_type_register_static (I_("EggParamSpecUnit"),
                                                 &pspec_info);
    }

  return pspec_type;
}

/**
 * egg_param_spec_unit:
 * @name: name of the property
 * @nick: short name
 * @blurb: description (can be translatable)
 * @minimum: lower boundary
 * @maximum: higher boundary
 * @default_value: default value
 * @flags: flags for the param spec
 *
 * Creates a #GParamSpec for properties using #EggUnit<!-- -->s.
 *
 * Return value: the newly created #GParamSpec
 *
 * Since: 0.8
 */
GParamSpec *
egg_param_spec_unit (const gchar *name,
                         const gchar *nick,
                         const gchar *blurb,
                         EggUnit  minimum,
                         EggUnit  maximum,
                         EggUnit  default_value,
                         GParamFlags  flags)
{
  EggParamSpecUnit *uspec;

  g_return_val_if_fail (default_value >= minimum && default_value <= maximum,
                        NULL);

  uspec = g_param_spec_internal (EGG_TYPE_PARAM_UNIT,
                                 name, nick, blurb,
                                 flags);
  uspec->minimum = minimum;
  uspec->maximum = maximum;
  uspec->default_value = default_value;

  return G_PARAM_SPEC (uspec);
}