home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / libgimp / gimpunit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-31  |  9.2 KB  |  358 lines

  1. /* LIBGIMP - The GIMP Library
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
  3.  *
  4.  * gimpunit.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 2 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, write to the
  19.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20.  * Boston, MA 02111-1307, USA.
  21.  */
  22.  
  23. #include "config.h"
  24.  
  25. #include <glib.h>
  26.  
  27. #include "gimpunit.h"
  28. #include "gimpunit_pdb.h"
  29.  
  30. #include "libgimp-intl.h"
  31.  
  32. /*  internal structures  */
  33.  
  34. typedef struct
  35. {
  36.   gboolean  delete_on_exit;
  37.   gdouble   factor;
  38.   gint      digits;
  39.   gchar    *identifier;
  40.   gchar    *symbol;
  41.   gchar    *abbreviation;
  42.   gchar    *singular;
  43.   gchar    *plural;
  44. } GimpUnitDef;
  45.  
  46. static GimpUnitDef gimp_unit_defs[GIMP_UNIT_END] =
  47. {
  48.   /* pseudo unit */
  49.   { FALSE,  0.0, 0, "pixels",      "px", "px", N_("pixel"),      N_("pixels") },
  50.  
  51.   /* standard units */
  52.   { FALSE,  1.0, 2, "inches",      "''", "in", N_("inch"),       N_("inches") },
  53.   { FALSE, 25.4, 1, "millimeters", "mm", "mm", N_("millimeter"), N_("millimeters") },
  54.  
  55.   /* professional units */
  56.   { FALSE, 72.0, 0, "points",      "pt", "pt", N_("point"),      N_("points") },
  57.   { FALSE,  6.0, 1, "picas",       "pc", "pc", N_("pica"),       N_("picas") },
  58. };
  59.  
  60. /*  not a unit at all but kept here to have the strings in one place
  61.  */
  62. static GimpUnitDef gimp_unit_percent =
  63. {
  64.   FALSE,    0.0, 0, "percent",     "%",  "%",  N_("percent"),    N_("percent")
  65. };
  66.  
  67.  
  68. /**
  69.  * gimp_unit_get_number_of_units:
  70.  *
  71.  * Returns the number of units which are known to the #GimpUnit system.
  72.  *
  73.  * Returns: The number of defined units.
  74.  *
  75.  */
  76. gint
  77. gimp_unit_get_number_of_units (void)
  78. {
  79.   return _gimp_unit_get_number_of_units ();
  80. }
  81.  
  82. /**
  83.  * gimp_unit_get_number_of_built_in_units:
  84.  *
  85.  * Returns the number of #GimpUnit's which are hardcoded in the unit system
  86.  * (UNIT_INCH, UNIT_MM, UNIT_POINT, UNIT_PICA and the two "pseudo unit"
  87.  *  UNIT_PIXEL).
  88.  *
  89.  * Returns: The number of built-in units.
  90.  *
  91.  */
  92. gint
  93. gimp_unit_get_number_of_built_in_units (void)
  94. {
  95.   return GIMP_UNIT_END;
  96. }
  97.  
  98. /**
  99.  * gimp_unit_new:
  100.  * @identifier: The unit's identifier string.
  101.  * @factor: The unit's factor (how many units are in one inch).
  102.  * @digits: The unit's suggested number of digits (see gimp_unit_get_digits()).
  103.  * @symbol: The symbol of the unit (e.g. "''" for inch).
  104.  * @abbreviation: The abbreviation of the unit.
  105.  * @singular: The singular form of the unit.
  106.  * @plural: The plural form of the unit.
  107.  *
  108.  * Returns the integer ID of the new #GimpUnit.
  109.  *
  110.  * Note that a new unit is always created with it's deletion flag
  111.  * set to #TRUE. You will have to set it to #FALSE with
  112.  * gimp_unit_set_deletion_flag() to make the unit definition persistent.
  113.  *
  114.  * Returns: The ID of the new unit.
  115.  *
  116.  */
  117. GimpUnit
  118. gimp_unit_new (gchar   *identifier,
  119.            gdouble  factor,
  120.            gint     digits,
  121.            gchar   *symbol,
  122.            gchar   *abbreviation,
  123.            gchar   *singular,
  124.            gchar   *plural)
  125. {
  126.   return _gimp_unit_new (identifier,
  127.              factor,
  128.              digits,
  129.              symbol,
  130.              abbreviation,
  131.              singular,
  132.              plural);
  133. }
  134.  
  135. /**
  136.  * gimp_unit_get_deletion_flag:
  137.  * @unit: The unit you want to know the @deletion_flag of.
  138.  *
  139.  * Returns: The unit's @deletion_flag.
  140.  *
  141.  */
  142. gboolean
  143. gimp_unit_get_deletion_flag (GimpUnit unit)
  144. {
  145.   g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, TRUE);
  146.  
  147.   if (unit < GIMP_UNIT_END)
  148.     return FALSE;
  149.  
  150.   return _gimp_unit_get_deletion_flag (unit);
  151. }
  152.  
  153. /**
  154.  * gimp_unit_set_deletion_flag:
  155.  * @unit: The unit you want to set the @deletion_flag for.
  156.  * @deletion_flag: The new deletion_flag.
  157.  *
  158.  * Sets a #GimpUnit's @deletion_flag. If the @deletion_flag of a unit is
  159.  * #TRUE when GIMP exits, this unit will not be saved in the uses's
  160.  * "unitrc" file.
  161.  *
  162.  * Trying to change the @deletion_flag of a built-in unit will be silently
  163.  * ignored.
  164.  *
  165.  */
  166. void
  167. gimp_unit_set_deletion_flag (GimpUnit unit,
  168.                  gboolean deletion_flag)
  169. {
  170.   g_return_if_fail (unit >= GIMP_UNIT_PIXEL);
  171.  
  172.   if (unit < GIMP_UNIT_END)
  173.     return;
  174.  
  175.   _gimp_unit_set_deletion_flag (unit,
  176.                 deletion_flag);
  177. }
  178.  
  179. /**
  180.  * gimp_unit_get_factor:
  181.  * @unit: The unit you want to know the factor of.
  182.  *
  183.  * A #GimpUnit's @factor is defined to be:
  184.  *
  185.  * distance_in_units == (@factor * distance_in_inches)
  186.  *
  187.  * Returns 0 for @unit == GIMP_UNIT_PIXEL.
  188.  *
  189.  * Returns: The unit's factor.
  190.  *
  191.  */
  192. gdouble
  193. gimp_unit_get_factor (GimpUnit unit)
  194. {
  195.   g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 1.0);
  196.  
  197.   if (unit < GIMP_UNIT_END)
  198.     return gimp_unit_defs[unit].factor;
  199.  
  200.   return _gimp_unit_get_factor (unit);
  201. }
  202.  
  203. /**
  204.  * gimp_unit_get_digits:
  205.  * @unit: The unit you want to know the digits.
  206.  *
  207.  * Returns the number of digits an entry field should provide to get
  208.  * approximately the same accuracy as an inch input field with two digits.
  209.  *
  210.  * Returns 0 for @unit == GIMP_UNIT_PIXEL.
  211.  *
  212.  * Returns: The suggested number of digits.
  213.  *
  214.  */
  215. gint
  216. gimp_unit_get_digits (GimpUnit unit)
  217. {
  218.   g_return_val_if_fail (unit >= GIMP_UNIT_INCH, 0);
  219.  
  220.   if (unit < GIMP_UNIT_END)
  221.     return gimp_unit_defs[unit].digits;
  222.  
  223.   return _gimp_unit_get_digits (unit);
  224. }
  225.  
  226. /**
  227.  * gimp_unit_get_identifier:
  228.  * @unit: The unit you want to know the identifier of.
  229.  *
  230.  * This is an unstranslated string.
  231.  *
  232.  * NOTE: This string has to be g_free()'d by plugins but is a pointer to a
  233.  *       constant string when this function is used from inside the GIMP.
  234.  *
  235.  * Returns: The unit's identifier.
  236.  *
  237.  */
  238. gchar * 
  239. gimp_unit_get_identifier (GimpUnit unit)
  240. {
  241.   g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, g_strdup (""));
  242.  
  243.   if (unit < GIMP_UNIT_END)
  244.     return g_strdup (gimp_unit_defs[unit].identifier);
  245.  
  246.   if (unit == GIMP_UNIT_PERCENT)
  247.     return g_strdup (gimp_unit_percent.identifier);
  248.  
  249.   return _gimp_unit_get_identifier (unit);
  250. }
  251.  
  252. /**
  253.  * gimp_unit_get_symbol:
  254.  * @unit: The unit you want to know the symbol of.
  255.  *
  256.  * This is e.g. "''" for UNIT_INCH.
  257.  *
  258.  * NOTE: This string has to be g_free()'d by plugins but is a pointer to a
  259.  *       constant string when this function is used from inside the GIMP.
  260.  *
  261.  * Returns: The unit's symbol.
  262.  *
  263.  */
  264. gchar *
  265. gimp_unit_get_symbol (GimpUnit unit)
  266. {
  267.   g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, g_strdup (""));
  268.  
  269.   if (unit < GIMP_UNIT_END)
  270.     return g_strdup (gimp_unit_defs[unit].symbol);
  271.  
  272.   if (unit == GIMP_UNIT_PERCENT)
  273.     return g_strdup (gimp_unit_percent.symbol);
  274.  
  275.   return _gimp_unit_get_symbol (unit);
  276. }
  277.  
  278. /**
  279.  * gimp_unit_get_abbreviation:
  280.  * @unit: The unit you want to know the abbreviation of.
  281.  *
  282.  * For built-in units, this function returns the translated abbreviation
  283.  * of the unit.
  284.  *
  285.  * NOTE: This string has to be g_free()'d by plugins but is a pointer to a
  286.  *       constant string when this function is used from inside the GIMP.
  287.  *
  288.  * Returns: The unit's abbreviation.
  289.  *
  290.  */
  291. gchar *
  292. gimp_unit_get_abbreviation (GimpUnit unit)
  293. {
  294.   g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, g_strdup (""));
  295.  
  296.   if (unit < GIMP_UNIT_END)
  297.     return g_strdup (gimp_unit_defs[unit].abbreviation);
  298.  
  299.   if (unit == GIMP_UNIT_PERCENT)
  300.     return g_strdup (gimp_unit_percent.abbreviation);
  301.  
  302.   return _gimp_unit_get_abbreviation (unit);
  303. }
  304.  
  305. /**
  306.  * gimp_unit_get_singular:
  307.  * @unit: The unit you want to know the singular form of.
  308.  *
  309.  * For built-in units, this function returns the translated singular form
  310.  * of the unit's name.
  311.  *
  312.  * NOTE: This string has to be g_free()'d by plugins but is a pointer to a
  313.  *       constant string when this function is used from inside the GIMP.
  314.  *
  315.  * Returns: The unit's singular form.
  316.  *
  317.  */
  318. gchar *
  319. gimp_unit_get_singular (GimpUnit unit)
  320. {
  321.   g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, g_strdup (""));
  322.  
  323.   if (unit < GIMP_UNIT_END)
  324.     return g_strdup (gettext (gimp_unit_defs[unit].singular));
  325.  
  326.   if (unit == GIMP_UNIT_PERCENT)
  327.     return g_strdup (gettext (gimp_unit_percent.singular));
  328.  
  329.   return _gimp_unit_get_singular (unit);
  330. }
  331.  
  332. /**
  333.  * gimp_unit_get_plural:
  334.  * @unit: The unit you want to know the plural form of.
  335.  *
  336.  * For built-in units, this function returns the translated plural form
  337.  * of the unit's name.
  338.  *
  339.  * NOTE: This string has to be g_free()'d by plugins but is a pointer to a
  340.  *       constant string when this function is used from inside the GIMP.
  341.  *
  342.  * Returns: The unit's plural form.
  343.  *
  344.  */
  345. gchar *
  346. gimp_unit_get_plural (GimpUnit unit)
  347. {
  348.   g_return_val_if_fail (unit >= GIMP_UNIT_PIXEL, g_strdup (""));
  349.  
  350.   if (unit < GIMP_UNIT_END)
  351.     return g_strdup (gettext (gimp_unit_defs[unit].plural));
  352.  
  353.   if (unit == GIMP_UNIT_PERCENT)
  354.     return g_strdup (gettext (gimp_unit_percent.plural));
  355.  
  356.   return _gimp_unit_get_plural (unit);
  357. }
  358.