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/>.
23 #include "claws-features.h"
29 #include <glib-object.h>
32 GtkCMUnitVtable _gtk_unit_vtable = { NULL, };
36 gtk_base_init (GtkCMUnitVtable *vtable)
38 static gboolean gtk_base_initialized = FALSE;
40 g_return_if_fail (vtable != NULL);
42 if (gtk_base_initialized)
43 g_error ("gtk_base_init() must only be called once!");
45 _gtk_unit_vtable = *vtable;
47 gtk_base_initialized = TRUE;
54 * @short_description: Provides a collection of predefined units and
55 * functions for creating user-defined units.
56 * @see_also: #GtkCMUnitMenu, #GtkSizeEntry.
58 * Provides a collection of predefined units and functions for
59 * creating user-defined units.
63 static void unit_to_string (const GValue *src_value,
65 static void string_to_unit (const GValue *src_value,
69 gtk_unit_get_type (void)
71 static GType unit_type = 0;
75 const GTypeInfo type_info = { 0, };
77 unit_type = g_type_register_static (G_TYPE_INT, "GtkCMUnit",
80 g_value_register_transform_func (unit_type, G_TYPE_STRING,
82 g_value_register_transform_func (G_TYPE_STRING, unit_type,
90 unit_to_string (const GValue *src_value,
93 GtkCMUnit unit = (GtkCMUnit) g_value_get_int (src_value);
95 g_value_set_string (dest_value, gtk_unit_get_identifier (unit));
99 string_to_unit (const GValue *src_value,
106 str = g_value_get_string (src_value);
111 num_units = gtk_unit_get_number_of_units ();
113 for (i = CM_UNIT_PIXEL; i < num_units; i++)
114 if (strcmp (str, gtk_unit_get_identifier (i)) == 0)
119 if (strcmp (str, gtk_unit_get_identifier (CM_UNIT_PERCENT)) == 0)
125 g_value_set_int (dest_value, i);
129 g_warning ("Can't convert string to GtkCMUnit.");
134 * gtk_unit_get_number_of_units:
136 * Returns the number of units which are known to the #GtkCMUnit system.
138 * Returns: The number of defined units.
141 gtk_unit_get_number_of_units (void)
143 g_return_val_if_fail (_gtk_unit_vtable.unit_get_number_of_units != NULL,
146 return _gtk_unit_vtable.unit_get_number_of_units ();
150 * gtk_unit_get_number_of_built_in_units:
152 * Returns the number of #GtkCMUnit's which are hardcoded in the unit system
153 * (UNIT_INCH, UNIT_MM, UNIT_POINT, UNIT_PICA and the two "pseudo unit"
156 * Returns: The number of built-in units.
159 gtk_unit_get_number_of_built_in_units (void)
161 g_return_val_if_fail (_gtk_unit_vtable.unit_get_number_of_built_in_units
162 != NULL, CM_UNIT_END);
164 return _gtk_unit_vtable.unit_get_number_of_built_in_units ();
169 * @identifier: The unit's identifier string.
170 * @factor: The unit's factor (how many units are in one inch).
171 * @digits: The unit's suggested number of digits (see gtk_unit_get_digits()).
172 * @symbol: The symbol of the unit (e.g. "''" for inch).
173 * @abbreviation: The abbreviation of the unit.
174 * @singular: The singular form of the unit.
175 * @plural: The plural form of the unit.
177 * Returns the integer ID of the new #GtkCMUnit.
179 * Note that a new unit is always created with it's deletion flag
180 * set to %TRUE. You will have to set it to %FALSE with
181 * gtk_unit_set_deletion_flag() to make the unit definition persistent.
183 * Returns: The ID of the new unit.
186 gtk_unit_new (gchar *identifier,
194 g_return_val_if_fail (_gtk_unit_vtable.unit_new != NULL, CM_UNIT_INCH);
196 return _gtk_unit_vtable.unit_new (identifier, factor, digits,
197 symbol, abbreviation, singular, plural);
201 * gtk_unit_get_deletion_flag:
202 * @unit: The unit you want to know the @deletion_flag of.
204 * Returns: The unit's @deletion_flag.
207 gtk_unit_get_deletion_flag (GtkCMUnit unit)
209 g_return_val_if_fail (_gtk_unit_vtable.unit_get_deletion_flag != NULL, FALSE);
211 return _gtk_unit_vtable.unit_get_deletion_flag (unit);
215 * gtk_unit_set_deletion_flag:
216 * @unit: The unit you want to set the @deletion_flag for.
217 * @deletion_flag: The new deletion_flag.
219 * Sets a #GtkCMUnit's @deletion_flag. If the @deletion_flag of a unit is
220 * %TRUE when GTK exits, this unit will not be saved in the users's
223 * Trying to change the @deletion_flag of a built-in unit will be silently
227 gtk_unit_set_deletion_flag (GtkCMUnit unit,
228 gboolean deletion_flag)
230 g_return_if_fail (_gtk_unit_vtable.unit_set_deletion_flag != NULL);
232 _gtk_unit_vtable.unit_set_deletion_flag (unit, deletion_flag);
236 * gtk_unit_get_factor:
237 * @unit: The unit you want to know the factor of.
239 * A #GtkCMUnit's @factor is defined to be:
241 * distance_in_units == (@factor * distance_in_inches)
243 * Returns 0 for @unit == CM_UNIT_PIXEL.
245 * Returns: The unit's factor.
248 gtk_unit_get_factor (GtkCMUnit unit)
250 g_return_val_if_fail (_gtk_unit_vtable.unit_get_factor != NULL, 1.0);
252 return _gtk_unit_vtable.unit_get_factor (unit);
256 * gtk_unit_get_digits:
257 * @unit: The unit you want to know the digits.
259 * Returns the number of digits an entry field should provide to get
260 * approximately the same accuracy as an inch input field with two digits.
262 * Returns 0 for @unit == CM_UNIT_PIXEL.
264 * Returns: The suggested number of digits.
267 gtk_unit_get_digits (GtkCMUnit unit)
269 g_return_val_if_fail (_gtk_unit_vtable.unit_get_digits != NULL, 2);
271 return _gtk_unit_vtable.unit_get_digits (unit);
275 * gtk_unit_get_identifier:
276 * @unit: The unit you want to know the identifier of.
278 * This is an unstranslated string and must not be changed or freed.
280 * Returns: The unit's identifier.
283 gtk_unit_get_identifier (GtkCMUnit unit)
285 g_return_val_if_fail (_gtk_unit_vtable.unit_get_identifier != NULL, NULL);
287 return _gtk_unit_vtable.unit_get_identifier (unit);
291 * gtk_unit_get_symbol:
292 * @unit: The unit you want to know the symbol of.
294 * This is e.g. "''" for UNIT_INCH.
296 * NOTE: This string must not be changed or freed.
298 * Returns: The unit's symbol.
301 gtk_unit_get_symbol (GtkCMUnit unit)
303 g_return_val_if_fail (_gtk_unit_vtable.unit_get_symbol != NULL, NULL);
305 return _gtk_unit_vtable.unit_get_symbol (unit);
309 * gtk_unit_get_abbreviation:
310 * @unit: The unit you want to know the abbreviation of.
312 * For built-in units, this function returns the translated abbreviation
315 * NOTE: This string must not be changed or freed.
317 * Returns: The unit's abbreviation.
320 gtk_unit_get_abbreviation (GtkCMUnit unit)
322 g_return_val_if_fail (_gtk_unit_vtable.unit_get_abbreviation != NULL, NULL);
324 return _gtk_unit_vtable.unit_get_abbreviation (unit);
328 * gtk_unit_get_singular:
329 * @unit: The unit you want to know the singular form of.
331 * For built-in units, this function returns the translated singular form
332 * of the unit's name.
334 * NOTE: This string must not be changed or freed.
336 * Returns: The unit's singular form.
339 gtk_unit_get_singular (GtkCMUnit unit)
341 g_return_val_if_fail (_gtk_unit_vtable.unit_get_singular != NULL, NULL);
343 return _gtk_unit_vtable.unit_get_singular (unit);
347 * gtk_unit_get_plural:
348 * @unit: The unit you want to know the plural form of.
350 * For built-in units, this function returns the translated plural form
351 * of the unit's name.
353 * NOTE: This string must not be changed or freed.
355 * Returns: The unit's plural form.
358 gtk_unit_get_plural (GtkCMUnit unit)
360 g_return_val_if_fail (_gtk_unit_vtable.unit_get_plural != NULL, NULL);
362 return _gtk_unit_vtable.unit_get_plural (unit);
375 va_start (args, fmt);
377 printed = g_vsnprintf (buf + start, len - start, fmt, args);
379 printed = len - start;
387 * gtk_unit_format_string:
388 * @format: A printf-like format string which is used to create the unit
392 * The @format string supports the following percent expansions:
394 * <informaltable pgwide="1" frame="none" role="enum">
395 * <tgroup cols="2"><colspec colwidth="1*"/><colspec colwidth="8*"/>
399 * <entry>Factor (how many units make up an inch)</entry>
403 * <entry>Symbol (e.g. "''" for CM_UNIT_INCH)</entry>
407 * <entry>Abbreviation</entry>
411 * <entry>Singular</entry>
415 * <entry>Plural</entry>
419 * <entry>Literal percent</entry>
425 * Returns: A newly allocated string with above percent expressions
426 * replaced with the resp. strings for @unit.
431 gtk_unit_format_string (const gchar *format,
437 g_return_val_if_fail (format != NULL, NULL);
438 g_return_val_if_fail (unit == CM_UNIT_PERCENT ||
439 (unit >= CM_UNIT_PIXEL &&
440 unit < gtk_unit_get_number_of_units ()), NULL);
442 while (i < (sizeof (buffer) - 1) && *format)
451 g_warning ("%s: unit-menu-format string ended within %%-sequence",
459 case 'f': /* factor (how many units make up an inch) */
460 i += print (buffer, sizeof (buffer), i, "%f",
461 gtk_unit_get_factor (unit));
464 case 'y': /* symbol ("''" for inch) */
465 i += print (buffer, sizeof (buffer), i, "%s",
466 gtk_unit_get_symbol (unit));
469 case 'a': /* abbreviation */
470 i += print (buffer, sizeof (buffer), i, "%s",
471 gtk_unit_get_abbreviation (unit));
474 case 's': /* singular */
475 i += print (buffer, sizeof (buffer), i, "%s",
476 gtk_unit_get_singular (unit));
479 case 'p': /* plural */
480 i += print (buffer, sizeof (buffer), i, "%s",
481 gtk_unit_get_plural (unit));
485 g_warning ("%s: unit-menu-format contains unknown format "
486 "sequence '%%%c'", G_STRFUNC, *format);
492 buffer[i++] = *format;
499 buffer[MIN (i, sizeof (buffer) - 1)] = 0;
501 return g_strdup (buffer);
505 * GTK_TYPE_PARAM_UNIT
508 #define GTK_PARAM_SPEC_UNIT(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GTK_TYPE_PARAM_UNIT, GtkParamSpecUnit))
510 typedef struct _GtkParamSpecUnit GtkParamSpecUnit;
512 struct _GtkParamSpecUnit
514 GParamSpecInt parent_instance;
516 gboolean allow_percent;
519 static void gtk_param_unit_class_init (GParamSpecClass *class);
520 static gboolean gtk_param_unit_value_validate (GParamSpec *pspec,
524 * gtk_param_unit_get_type:
526 * Reveals the object type
528 * Returns: the #GType for a unit param object
533 gtk_param_unit_get_type (void)
535 static GType spec_type = 0;
539 const GTypeInfo type_info =
541 sizeof (GParamSpecClass),
543 (GClassInitFunc) gtk_param_unit_class_init,
545 sizeof (GtkParamSpecUnit),
549 spec_type = g_type_register_static (G_TYPE_PARAM_INT,
558 gtk_param_unit_class_init (GParamSpecClass *class)
560 class->value_type = GTK_TYPE_UNIT;
561 class->value_validate = gtk_param_unit_value_validate;
565 gtk_param_unit_value_validate (GParamSpec *pspec,
568 GParamSpecInt *ispec = G_PARAM_SPEC_INT (pspec);
569 GtkParamSpecUnit *uspec = GTK_PARAM_SPEC_UNIT (pspec);
570 gint oval = value->data[0].v_int;
572 if (uspec->allow_percent && value->data[0].v_int != CM_UNIT_PERCENT) {
573 value->data[0].v_int = CLAMP (value->data[0].v_int,
575 gtk_unit_get_number_of_units () - 1);
578 return value->data[0].v_int != oval;
582 * gtk_param_spec_unit:
583 * @name: Canonical name of the param
584 * @nick: Nickname of the param
585 * @blurb: Brief desciption of param.
586 * @allow_pixels: Whether "pixels" is an allowed unit.
587 * @allow_percent: Whether "perecent" is an allowed unit.
588 * @default_value: Unit to use if none is assigned.
589 * @flags: a combination of #GParamFlags
591 * Creates a param spec to hold a units param.
592 * See g_param_spec_internal() for more information.
594 * Returns: a newly allocated #GParamSpec instance
599 gtk_param_spec_unit (const gchar *name,
602 gboolean allow_pixels,
603 gboolean allow_percent,
604 GtkCMUnit default_value,
607 GtkParamSpecUnit *pspec;
608 GParamSpecInt *ispec;
610 pspec = g_param_spec_internal (GTK_TYPE_PARAM_UNIT,
611 name, nick, blurb, flags);
613 ispec = G_PARAM_SPEC_INT (pspec);
615 ispec->default_value = default_value;
616 ispec->minimum = allow_pixels ? CM_UNIT_PIXEL : CM_UNIT_INCH;
617 ispec->maximum = CM_UNIT_PERCENT - 1;
619 pspec->allow_percent = allow_percent;
621 return G_PARAM_SPEC (pspec);
625 * gtk_pixels_to_units:
626 * @pixels: value in pixels
627 * @unit: unit to convert to
628 * @resolution: resloution in DPI
630 * Converts a @value specified in pixels to @unit.
632 * Returns: @pixels converted to units.
637 gtk_pixels_to_units (gdouble pixels,
641 if (unit == CM_UNIT_PIXEL)
644 return pixels * gtk_unit_get_factor (unit) / resolution;
648 * gtk_units_to_pixels:
649 * @value: value in units
650 * @unit: unit of @value
651 * @resolution: resloution in DPI
653 * Converts a @value specified in @unit to pixels.
655 * Returns: @value converted to pixels.
660 gtk_units_to_pixels (gdouble value,
664 if (unit == CM_UNIT_PIXEL)
667 return value * resolution / gtk_unit_get_factor (unit);
671 * gtk_units_to_points:
672 * @value: value in units
673 * @unit: unit of @value
674 * @resolution: resloution in DPI
676 * Converts a @value specified in @unit to points.
678 * Returns: @value converted to points.
683 gtk_units_to_points (gdouble value,
687 if (unit == CM_UNIT_POINT)
690 if (unit == CM_UNIT_PIXEL)
691 return (value * gtk_unit_get_factor (CM_UNIT_POINT) / resolution);
694 gtk_unit_get_factor (CM_UNIT_POINT) / gtk_unit_get_factor (unit));