gimpunitcache.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* LIBGIMP - The GIMP Library
  2. * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
  3. *
  4. * gimpunitcache.c
  5. * Copyright (C) 1999-2000 Michael Natterer <mitch@gimp.org>
  6. *
  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.
  11. *
  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. * Library General Public License for more details.
  16. *
  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/>.
  20. */
  21. #include "config.h"
  22. #include <glib-object.h>
  23. #include "libgimpbase/gimpbase.h"
  24. #include "gimpunitcache.h"
  25. #include "gimpunit_pdb.h"
  26. #include "libgimp-intl.h"
  27. /* internal structures */
  28. typedef struct
  29. {
  30. gdouble factor;
  31. gint digits;
  32. const gchar *identifier;
  33. const gchar *symbol;
  34. const gchar *abbreviation;
  35. const gchar *singular;
  36. const gchar *plural;
  37. } GimpUnitDef;
  38. static GimpUnitDef * gimp_unit_defs = NULL;
  39. static GimpUnit gimp_units_initialized = 0;
  40. /* not a unit at all but kept here to have the strings in one place
  41. */
  42. static const GimpUnitDef gimp_unit_percent =
  43. {
  44. 0.0, 0, "percent", "%", "%", N_("percent"), N_("percent")
  45. };
  46. static void gimp_unit_def_init (GimpUnitDef *unit_def,
  47. GimpUnit unit);
  48. static gboolean
  49. gimp_unit_init (GimpUnit unit)
  50. {
  51. gint i, n;
  52. if (unit < gimp_units_initialized)
  53. return TRUE;
  54. n = _gimp_unit_get_number_of_units ();
  55. if (unit >= n)
  56. return FALSE;
  57. gimp_unit_defs = g_renew (GimpUnitDef, gimp_unit_defs, n);
  58. for (i = gimp_units_initialized; i < n; i++)
  59. {
  60. gimp_unit_def_init (&gimp_unit_defs[i], i);
  61. }
  62. gimp_units_initialized = n;
  63. return TRUE;
  64. }
  65. static void
  66. gimp_unit_def_init (GimpUnitDef *unit_def,
  67. GimpUnit unit)
  68. {
  69. unit_def->factor = _gimp_unit_get_factor (unit);
  70. unit_def->digits = _gimp_unit_get_digits (unit);
  71. unit_def->identifier = _gimp_unit_get_identifier (unit);
  72. unit_def->symbol = _gimp_unit_get_symbol (unit);
  73. unit_def->abbreviation = _gimp_unit_get_abbreviation (unit);
  74. unit_def->singular = _gimp_unit_get_singular (unit);
  75. unit_def->plural = _gimp_unit_get_plural (unit);
  76. }
  77. gint
  78. _gimp_unit_cache_get_number_of_units (void)
  79. {
  80. return _gimp_unit_get_number_of_units ();
  81. }
  82. gint
  83. _gimp_unit_cache_get_number_of_built_in_units (void)
  84. {
  85. return GIMP_UNIT_END;
  86. }
  87. GimpUnit
  88. _gimp_unit_cache_new (gchar *identifier,
  89. gdouble factor,
  90. gint digits,
  91. gchar *symbol,
  92. gchar *abbreviation,
  93. gchar *singular,
  94. gchar *plural)
  95. {
  96. return _gimp_unit_new (identifier,
  97. factor,
  98. digits,
  99. symbol,
  100. abbreviation,
  101. singular,
  102. plural);
  103. }
  104. gboolean
  105. _gimp_unit_cache_get_deletion_flag (GimpUnit unit)
  106. {
  107. if (unit < GIMP_UNIT_END)
  108. return FALSE;
  109. return _gimp_unit_get_deletion_flag (unit);
  110. }
  111. void
  112. _gimp_unit_cache_set_deletion_flag (GimpUnit unit,
  113. gboolean deletion_flag)
  114. {
  115. if (unit < GIMP_UNIT_END)
  116. return;
  117. _gimp_unit_set_deletion_flag (unit,
  118. deletion_flag);
  119. }
  120. gdouble
  121. _gimp_unit_cache_get_factor (GimpUnit unit)
  122. {
  123. g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 1.0);
  124. if (unit == GIMP_UNIT_PERCENT)
  125. return gimp_unit_percent.factor;
  126. if (!gimp_unit_init (unit))
  127. return 1.0;
  128. return gimp_unit_defs[unit].factor;
  129. }
  130. gint
  131. _gimp_unit_cache_get_digits (GimpUnit unit)
  132. {
  133. g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 0);
  134. if (unit == GIMP_UNIT_PERCENT)
  135. return gimp_unit_percent.digits;
  136. if (!gimp_unit_init (unit))
  137. return 0;
  138. return gimp_unit_defs[unit].digits;
  139. }
  140. const gchar *
  141. _gimp_unit_cache_get_identifier (GimpUnit unit)
  142. {
  143. if (unit == GIMP_UNIT_PERCENT)
  144. return gimp_unit_percent.identifier;
  145. if (!gimp_unit_init (unit))
  146. return NULL;
  147. return gimp_unit_defs[unit].identifier;
  148. }
  149. const gchar *
  150. _gimp_unit_cache_get_symbol (GimpUnit unit)
  151. {
  152. if (unit == GIMP_UNIT_PERCENT)
  153. return gimp_unit_percent.symbol;
  154. if (!gimp_unit_init (unit))
  155. return NULL;
  156. return gimp_unit_defs[unit].symbol;
  157. }
  158. const gchar *
  159. _gimp_unit_cache_get_abbreviation (GimpUnit unit)
  160. {
  161. if (unit == GIMP_UNIT_PERCENT)
  162. return gimp_unit_percent.abbreviation;
  163. if (!gimp_unit_init (unit))
  164. return NULL;
  165. return gimp_unit_defs[unit].abbreviation;
  166. }
  167. const gchar *
  168. _gimp_unit_cache_get_singular (GimpUnit unit)
  169. {
  170. if (unit == GIMP_UNIT_PERCENT)
  171. return gettext (gimp_unit_percent.singular);
  172. if (!gimp_unit_init (unit))
  173. return NULL;
  174. return gettext (gimp_unit_defs[unit].singular);
  175. }
  176. const gchar *
  177. _gimp_unit_cache_get_plural (GimpUnit unit)
  178. {
  179. if (unit == GIMP_UNIT_PERCENT)
  180. return gettext (gimp_unit_percent.plural);
  181. if (!gimp_unit_init (unit))
  182. return NULL;
  183. return gettext (gimp_unit_defs[unit].plural);
  184. }