1 /* LIBGTK - The GTK Library
2 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
5 * Copyright (C) 2003 Michael Natterer <mitch@gimp.org>
7 * This library is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see
19 * <http://www.gnu.org/licenses/>.
28 #include <glib-object.h>
31 GtkCMUnitVtable _gtk_unit_vtable = { NULL, };
35 gtk_base_init (GtkCMUnitVtable *vtable)
37 static gboolean gtk_base_initialized = FALSE;
39 g_return_if_fail (vtable != NULL);
41 if (gtk_base_initialized)
42 g_error ("gtk_base_init() must only be called once!");
44 _gtk_unit_vtable = *vtable;
46 gtk_base_initialized = TRUE;
53 * @short_description: Provides a collection of predefined units and
54 * functions for creating user-defined units.
55 * @see_also: #GtkCMUnitMenu, #GtkSizeEntry.
57 * Provides a collection of predefined units and functions for
58 * creating user-defined units.
62 static void unit_to_string (const GValue *src_value,
64 static void string_to_unit (const GValue *src_value,
68 gtk_unit_get_type (void)
70 static GType unit_type = 0;
74 const GTypeInfo type_info = { 0, };
76 unit_type = g_type_register_static (G_TYPE_INT, "GtkCMUnit",
79 g_value_register_transform_func (unit_type, G_TYPE_STRING,
81 g_value_register_transform_func (G_TYPE_STRING, unit_type,
89 unit_to_string (const GValue *src_value,
92 GtkCMUnit unit = (GtkCMUnit) g_value_get_int (src_value);
94 g_value_set_string (dest_value, gtk_unit_get_identifier (unit));
98 string_to_unit (const GValue *src_value,
105 str = g_value_get_string (src_value);
110 num_units = gtk_unit_get_number_of_units ();
112 for (i = CM_UNIT_PIXEL; i < num_units; i++)
113 if (strcmp (str, gtk_unit_get_identifier (i)) == 0)
118 if (strcmp (str, gtk_unit_get_identifier (CM_UNIT_PERCENT)) == 0)
124 g_value_set_int (dest_value, i);
128 g_warning ("Can't convert string to GtkCMUnit.");
133 * gtk_unit_get_number_of_units:
135 * Returns the number of units which are known to the #GtkCMUnit system.
137 * Returns: The number of defined units.
140 gtk_unit_get_number_of_units (void)
142 g_return_val_if_fail (_gtk_unit_vtable.unit_get_number_of_units != NULL,
145 return _gtk_unit_vtable.unit_get_number_of_units ();
149 * gtk_unit_get_number_of_built_in_units:
151 * Returns the number of #GtkCMUnit's which are hardcoded in the unit system
152 * (UNIT_INCH, UNIT_MM, UNIT_POINT, UNIT_PICA and the two "pseudo unit"
155 * Returns: The number of built-in units.
158 gtk_unit_get_number_of_built_in_units (void)
160 g_return_val_if_fail (_gtk_unit_vtable.unit_get_number_of_built_in_units
161 != NULL, CM_UNIT_END);
163 return _gtk_unit_vtable.unit_get_number_of_built_in_units ();
168 * @identifier: The unit's identifier string.
169 * @factor: The unit's factor (how many units are in one inch).
170 * @digits: The unit's suggested number of digits (see gtk_unit_get_digits()).
171 * @symbol: The symbol of the unit (e.g. "''" for inch).
172 * @abbreviation: The abbreviation of the unit.
173 * @singular: The singular form of the unit.
174 * @plural: The plural form of the unit.
176 * Returns the integer ID of the new #GtkCMUnit.
178 * Note that a new unit is always created with it's deletion flag
179 * set to %TRUE. You will have to set it to %FALSE with
180 * gtk_unit_set_deletion_flag() to make the unit definition persistent.
182 * Returns: The ID of the new unit.
185 gtk_unit_new (gchar *identifier,
193 g_return_val_if_fail (_gtk_unit_vtable.unit_new != NULL, CM_UNIT_INCH);
195 return _gtk_unit_vtable.unit_new (identifier, factor, digits,
196 symbol, abbreviation, singular, plural);
200 * gtk_unit_get_deletion_flag:
201 * @unit: The unit you want to know the @deletion_flag of.
203 * Returns: The unit's @deletion_flag.
206 gtk_unit_get_deletion_flag (GtkCMUnit unit)
208 g_return_val_if_fail (_gtk_unit_vtable.unit_get_deletion_flag != NULL, FALSE);
210 return _gtk_unit_vtable.unit_get_deletion_flag (unit);
214 * gtk_unit_set_deletion_flag:
215 * @unit: The unit you want to set the @deletion_flag for.
216 * @deletion_flag: The new deletion_flag.
218 * Sets a #GtkCMUnit's @deletion_flag. If the @deletion_flag of a unit is
219 * %TRUE when GTK exits, this unit will not be saved in the users's
222 * Trying to change the @deletion_flag of a built-in unit will be silently
226 gtk_unit_set_deletion_flag (GtkCMUnit unit,
227 gboolean deletion_flag)
229 g_return_if_fail (_gtk_unit_vtable.unit_set_deletion_flag != NULL);
231 _gtk_unit_vtable.unit_set_deletion_flag (unit, deletion_flag);
235 * gtk_unit_get_factor:
236 * @unit: The unit you want to know the factor of.
238 * A #GtkCMUnit's @factor is defined to be:
240 * distance_in_units == (@factor * distance_in_inches)
242 * Returns 0 for @unit == CM_UNIT_PIXEL.
244 * Returns: The unit's factor.
247 gtk_unit_get_factor (GtkCMUnit unit)
249 g_return_val_if_fail (_gtk_unit_vtable.unit_get_factor != NULL, 1.0);
251 return _gtk_unit_vtable.unit_get_factor (unit);
255 * gtk_unit_get_digits:
256 * @unit: The unit you want to know the digits.
258 * Returns the number of digits an entry field should provide to get
259 * approximately the same accuracy as an inch input field with two digits.
261 * Returns 0 for @unit == CM_UNIT_PIXEL.
263 * Returns: The suggested number of digits.
266 gtk_unit_get_digits (GtkCMUnit unit)
268 g_return_val_if_fail (_gtk_unit_vtable.unit_get_digits != NULL, 2);
270 return _gtk_unit_vtable.unit_get_digits (unit);
274 * gtk_unit_get_identifier:
275 * @unit: The unit you want to know the identifier of.
277 * This is an unstranslated string and must not be changed or freed.
279 * Returns: The unit's identifier.
282 gtk_unit_get_identifier (GtkCMUnit unit)
284 g_return_val_if_fail (_gtk_unit_vtable.unit_get_identifier != NULL, NULL);
286 return _gtk_unit_vtable.unit_get_identifier (unit);
290 * gtk_unit_get_symbol:
291 * @unit: The unit you want to know the symbol of.
293 * This is e.g. "''" for UNIT_INCH.
295 * NOTE: This string must not be changed or freed.
297 * Returns: The unit's symbol.
300 gtk_unit_get_symbol (GtkCMUnit unit)
302 g_return_val_if_fail (_gtk_unit_vtable.unit_get_symbol != NULL, NULL);
304 return _gtk_unit_vtable.unit_get_symbol (unit);
308 * gtk_unit_get_abbreviation:
309 * @unit: The unit you want to know the abbreviation of.
311 * For built-in units, this function returns the translated abbreviation
314 * NOTE: This string must not be changed or freed.
316 * Returns: The unit's abbreviation.
319 gtk_unit_get_abbreviation (GtkCMUnit unit)
321 g_return_val_if_fail (_gtk_unit_vtable.unit_get_abbreviation != NULL, NULL);
323 return _gtk_unit_vtable.unit_get_abbreviation (unit);
327 * gtk_unit_get_singular:
328 * @unit: The unit you want to know the singular form of.
330 * For built-in units, this function returns the translated singular form
331 * of the unit's name.
333 * NOTE: This string must not be changed or freed.
335 * Returns: The unit's singular form.
338 gtk_unit_get_singular (GtkCMUnit unit)
340 g_return_val_if_fail (_gtk_unit_vtable.unit_get_singular != NULL, NULL);
342 return _gtk_unit_vtable.unit_get_singular (unit);
346 * gtk_unit_get_plural:
347 * @unit: The unit you want to know the plural form of.
349 * For built-in units, this function returns the translated plural form
350 * of the unit's name.
352 * NOTE: This string must not be changed or freed.
354 * Returns: The unit's plural form.
357 gtk_unit_get_plural (GtkCMUnit unit)
359 g_return_val_if_fail (_gtk_unit_vtable.unit_get_plural != NULL, NULL);
361 return _gtk_unit_vtable.unit_get_plural (unit);
374 va_start (args, fmt);
376 printed = g_vsnprintf (buf + start, len - start, fmt, args);
378 printed = len - start;
386 * gtk_unit_format_string:
387 * @format: A printf-like format string which is used to create the unit
391 * The @format string supports the following percent expansions:
393 * <informaltable pgwide="1" frame="none" role="enum">
394 * <tgroup cols="2"><colspec colwidth="1*"/><colspec colwidth="8*"/>
398 * <entry>Factor (how many units make up an inch)</entry>
402 * <entry>Symbol (e.g. "''" for CM_UNIT_INCH)</entry>
406 * <entry>Abbreviation</entry>
410 * <entry>Singular</entry>
414 * <entry>Plural</entry>
418 * <entry>Literal percent</entry>
424 * Returns: A newly allocated string with above percent expressions
425 * replaced with the resp. strings for @unit.
430 gtk_unit_format_string (const gchar *format,
436 g_return_val_if_fail (format != NULL, NULL);
437 g_return_val_if_fail (unit == CM_UNIT_PERCENT ||
438 (unit >= CM_UNIT_PIXEL &&
439 unit < gtk_unit_get_number_of_units ()), NULL);
441 while (i < (sizeof (buffer) - 1) && *format)
450 g_warning ("%s: unit-menu-format string ended within %%-sequence",
458 case 'f': /* factor (how many units make up an inch) */
459 i += print (buffer, sizeof (buffer), i, "%f",
460 gtk_unit_get_factor (unit));
463 case 'y': /* symbol ("''" for inch) */
464 i += print (buffer, sizeof (buffer), i, "%s",
465 gtk_unit_get_symbol (unit));
468 case 'a': /* abbreviation */
469 i += print (buffer, sizeof (buffer), i, "%s",
470 gtk_unit_get_abbreviation (unit));
473 case 's': /* singular */
474 i += print (buffer, sizeof (buffer), i, "%s",
475 gtk_unit_get_singular (unit));
478 case 'p': /* plural */
479 i += print (buffer, sizeof (buffer), i, "%s",
480 gtk_unit_get_plural (unit));
484 g_warning ("%s: unit-menu-format contains unknown format "
485 "sequence '%%%c'", G_STRFUNC, *format);
491 buffer[i++] = *format;
498 buffer[MIN (i, sizeof (buffer) - 1)] = 0;
500 return g_strdup (buffer);
504 * GTK_TYPE_PARAM_UNIT
507 #define GTK_PARAM_SPEC_UNIT(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GTK_TYPE_PARAM_UNIT, GtkParamSpecUnit))
509 typedef struct _GtkParamSpecUnit GtkParamSpecUnit;
511 struct _GtkParamSpecUnit
513 GParamSpecInt parent_instance;
515 gboolean allow_percent;
518 static void gtk_param_unit_class_init (GParamSpecClass *class);
519 static gboolean gtk_param_unit_value_validate (GParamSpec *pspec,
523 * gtk_param_unit_get_type:
525 * Reveals the object type
527 * Returns: the #GType for a unit param object
532 gtk_param_unit_get_type (void)
534 static GType spec_type = 0;
538 const GTypeInfo type_info =
540 sizeof (GParamSpecClass),
542 (GClassInitFunc) gtk_param_unit_class_init,
544 sizeof (GtkParamSpecUnit),
548 spec_type = g_type_register_static (G_TYPE_PARAM_INT,
557 gtk_param_unit_class_init (GParamSpecClass *class)
559 class->value_type = GTK_TYPE_UNIT;
560 class->value_validate = gtk_param_unit_value_validate;
564 gtk_param_unit_value_validate (GParamSpec *pspec,
567 GParamSpecInt *ispec = G_PARAM_SPEC_INT (pspec);
568 GtkParamSpecUnit *uspec = GTK_PARAM_SPEC_UNIT (pspec);
569 gint oval = value->data[0].v_int;
571 if (uspec->allow_percent && value->data[0].v_int == CM_UNIT_PERCENT)
573 value->data[0].v_int = value->data[0].v_int;
577 value->data[0].v_int = CLAMP (value->data[0].v_int,
579 gtk_unit_get_number_of_units () - 1);
582 return value->data[0].v_int != oval;
586 * gtk_param_spec_unit:
587 * @name: Canonical name of the param
588 * @nick: Nickname of the param
589 * @blurb: Brief desciption of param.
590 * @allow_pixels: Whether "pixels" is an allowed unit.
591 * @allow_percent: Whether "perecent" is an allowed unit.
592 * @default_value: Unit to use if none is assigned.
593 * @flags: a combination of #GParamFlags
595 * Creates a param spec to hold a units param.
596 * See g_param_spec_internal() for more information.
598 * Returns: a newly allocated #GParamSpec instance
603 gtk_param_spec_unit (const gchar *name,
606 gboolean allow_pixels,
607 gboolean allow_percent,
608 GtkCMUnit default_value,
611 GtkParamSpecUnit *pspec;
612 GParamSpecInt *ispec;
614 pspec = g_param_spec_internal (GTK_TYPE_PARAM_UNIT,
615 name, nick, blurb, flags);
617 ispec = G_PARAM_SPEC_INT (pspec);
619 ispec->default_value = default_value;
620 ispec->minimum = allow_pixels ? CM_UNIT_PIXEL : CM_UNIT_INCH;
621 ispec->maximum = CM_UNIT_PERCENT - 1;
623 pspec->allow_percent = allow_percent;
625 return G_PARAM_SPEC (pspec);
629 * gtk_pixels_to_units:
630 * @pixels: value in pixels
631 * @unit: unit to convert to
632 * @resolution: resloution in DPI
634 * Converts a @value specified in pixels to @unit.
636 * Returns: @pixels converted to units.
641 gtk_pixels_to_units (gdouble pixels,
645 if (unit == CM_UNIT_PIXEL)
648 return pixels * gtk_unit_get_factor (unit) / resolution;
652 * gtk_units_to_pixels:
653 * @value: value in units
654 * @unit: unit of @value
655 * @resolution: resloution in DPI
657 * Converts a @value specified in @unit to pixels.
659 * Returns: @value converted to pixels.
664 gtk_units_to_pixels (gdouble value,
668 if (unit == CM_UNIT_PIXEL)
671 return value * resolution / gtk_unit_get_factor (unit);
675 * gtk_units_to_points:
676 * @value: value in units
677 * @unit: unit of @value
678 * @resolution: resloution in DPI
680 * Converts a @value specified in @unit to points.
682 * Returns: @value converted to points.
687 gtk_units_to_points (gdouble value,
691 if (unit == CM_UNIT_POINT)
694 if (unit == CM_UNIT_PIXEL)
695 return (value * gtk_unit_get_factor (CM_UNIT_POINT) / resolution);
698 gtk_unit_get_factor (CM_UNIT_POINT) / gtk_unit_get_factor (unit));