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
< 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
) {
572 value
->data
[0].v_int
= CLAMP (value
->data
[0].v_int
,
574 gtk_unit_get_number_of_units () - 1);
577 return value
->data
[0].v_int
!= oval
;
581 * gtk_param_spec_unit:
582 * @name: Canonical name of the param
583 * @nick: Nickname of the param
584 * @blurb: Brief desciption of param.
585 * @allow_pixels: Whether "pixels" is an allowed unit.
586 * @allow_percent: Whether "perecent" is an allowed unit.
587 * @default_value: Unit to use if none is assigned.
588 * @flags: a combination of #GParamFlags
590 * Creates a param spec to hold a units param.
591 * See g_param_spec_internal() for more information.
593 * Returns: a newly allocated #GParamSpec instance
598 gtk_param_spec_unit (const gchar
*name
,
601 gboolean allow_pixels
,
602 gboolean allow_percent
,
603 GtkCMUnit default_value
,
606 GtkParamSpecUnit
*pspec
;
607 GParamSpecInt
*ispec
;
609 pspec
= g_param_spec_internal (GTK_TYPE_PARAM_UNIT
,
610 name
, nick
, blurb
, flags
);
612 ispec
= G_PARAM_SPEC_INT (pspec
);
614 ispec
->default_value
= default_value
;
615 ispec
->minimum
= allow_pixels
? CM_UNIT_PIXEL
: CM_UNIT_INCH
;
616 ispec
->maximum
= CM_UNIT_PERCENT
- 1;
618 pspec
->allow_percent
= allow_percent
;
620 return G_PARAM_SPEC (pspec
);
624 * gtk_pixels_to_units:
625 * @pixels: value in pixels
626 * @unit: unit to convert to
627 * @resolution: resloution in DPI
629 * Converts a @value specified in pixels to @unit.
631 * Returns: @pixels converted to units.
636 gtk_pixels_to_units (gdouble pixels
,
640 if (unit
== CM_UNIT_PIXEL
)
643 return pixels
* gtk_unit_get_factor (unit
) / resolution
;
647 * gtk_units_to_pixels:
648 * @value: value in units
649 * @unit: unit of @value
650 * @resolution: resloution in DPI
652 * Converts a @value specified in @unit to pixels.
654 * Returns: @value converted to pixels.
659 gtk_units_to_pixels (gdouble value
,
663 if (unit
== CM_UNIT_PIXEL
)
666 return value
* resolution
/ gtk_unit_get_factor (unit
);
670 * gtk_units_to_points:
671 * @value: value in units
672 * @unit: unit of @value
673 * @resolution: resloution in DPI
675 * Converts a @value specified in @unit to points.
677 * Returns: @value converted to points.
682 gtk_units_to_points (gdouble value
,
686 if (unit
== CM_UNIT_POINT
)
689 if (unit
== CM_UNIT_PIXEL
)
690 return (value
* gtk_unit_get_factor (CM_UNIT_POINT
) / resolution
);
693 gtk_unit_get_factor (CM_UNIT_POINT
) / gtk_unit_get_factor (unit
));