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

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
  3.  *
  4.  * gimpunit.c
  5.  * Copyright (C) 1999-2000 Michael Natterer <mitch@gimp.org>
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program 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
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20.  */
  21.  
  22. #include "config.h"
  23.  
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26.  
  27. #include <glib.h>
  28.  
  29. #include "unitrc.h"
  30.  
  31. #include "app_procs.h"
  32. #include "gimprc.h"
  33.  
  34. /* NOTE:
  35.  *
  36.  * one of our header files is in libgimp/ (see the note there)
  37.  */
  38. #include "libgimp/gimpunit.h"
  39. #include "libgimp/gimpenv.h"
  40.  
  41. #include "libgimp/gimpintl.h"
  42.  
  43.  
  44. /* internal structures */
  45.  
  46. typedef struct
  47. {
  48.   gboolean  delete_on_exit;
  49.   gdouble   factor;
  50.   gint      digits;
  51.   gchar    *identifier;
  52.   gchar    *symbol;
  53.   gchar    *abbreviation;
  54.   gchar    *singular;
  55.   gchar    *plural;
  56. } GimpUnitDef;
  57.  
  58. /*  these are the built-in units
  59.  */
  60. static GimpUnitDef gimp_unit_defs[GIMP_UNIT_END] =
  61. {
  62.   /* pseudo unit */
  63.   { FALSE,  0.0, 0, "pixels",      "px", "px", N_("pixel"),      N_("pixels") },
  64.  
  65.   /* standard units */
  66.   { FALSE,  1.0, 2, "inches",      "''", "in", N_("inch"),       N_("inches") },
  67.   { FALSE, 25.4, 1, "millimeters", "mm", "mm", N_("millimeter"), N_("millimeters") },
  68.  
  69.   /* professional units */
  70.   { FALSE, 72.0, 0, "points",      "pt", "pt", N_("point"),      N_("points") },
  71.   { FALSE,  6.0, 1, "picas",       "pc", "pc", N_("pica"),       N_("picas") },
  72. };
  73.  
  74. /*  not a unit at all but kept here to have the strings in one place
  75.  */
  76. static GimpUnitDef gimp_unit_percent =
  77. {
  78.   FALSE,    0.0, 0, "percent",     "%",  "%",  N_("percent"),    N_("percent")
  79. };
  80.  
  81. static GSList* user_units = NULL;
  82. static gint    number_of_user_units = 0;
  83.  
  84. /* private functions */
  85.  
  86. static GimpUnitDef *
  87. gimp_unit_get_user_unit (GimpUnit unit)
  88. {
  89.   return g_slist_nth_data (user_units, unit - GIMP_UNIT_END);
  90. }
  91.  
  92.  
  93. /* public functions */
  94.  
  95. gint
  96. gimp_unit_get_number_of_units (void)
  97. {
  98.   return GIMP_UNIT_END + number_of_user_units;
  99. }
  100.  
  101. gint
  102. gimp_unit_get_number_of_built_in_units (void)
  103. {
  104.   return GIMP_UNIT_END;
  105. }
  106.  
  107.  
  108. GimpUnit
  109. gimp_unit_new (gchar   *identifier,
  110.            gdouble  factor,
  111.            gint     digits,
  112.            gchar   *symbol,
  113.            gchar   *abbreviation,
  114.            gchar   *singular,
  115.            gchar   *plural)
  116. {
  117.   GimpUnitDef *user_unit;
  118.  
  119.   user_unit = g_new (GimpUnitDef, 1);
  120.   user_unit->delete_on_exit = TRUE;
  121.   user_unit->factor         = factor;
  122.   user_unit->digits         = digits;
  123.   user_unit->identifier     = g_strdup (identifier);
  124.   user_unit->symbol         = g_strdup (symbol);
  125.   user_unit->abbreviation   = g_strdup (abbreviation);
  126.   user_unit->singular       = g_strdup (singular);
  127.   user_unit->plural         = g_strdup (plural);
  128.  
  129.   user_units = g_slist_append (user_units, user_unit);
  130.   number_of_user_units++;
  131.  
  132.   return GIMP_UNIT_END + number_of_user_units - 1;
  133. }
  134.  
  135.  
  136. gboolean
  137. gimp_unit_get_deletion_flag (GimpUnit unit)
  138. {
  139.   g_return_val_if_fail ((unit >= GIMP_UNIT_PIXEL) && 
  140.             (unit < (GIMP_UNIT_END + number_of_user_units)), FALSE);
  141.  
  142.   if (unit < GIMP_UNIT_END)
  143.     return FALSE;
  144.  
  145.   return gimp_unit_get_user_unit (unit)->delete_on_exit;
  146. }
  147.  
  148. void
  149. gimp_unit_set_deletion_flag (GimpUnit unit,
  150.                  gboolean deletion_flag)
  151. {
  152.   g_return_if_fail ((unit >= GIMP_UNIT_END) && 
  153.             (unit < (GIMP_UNIT_END + number_of_user_units)));
  154.  
  155.   gimp_unit_get_user_unit (unit)->delete_on_exit =
  156.     deletion_flag ? TRUE : FALSE;
  157. }
  158.  
  159.  
  160. gdouble
  161. gimp_unit_get_factor (GimpUnit unit)
  162. {
  163.   g_return_val_if_fail ((unit >= GIMP_UNIT_PIXEL) && 
  164.             (unit < (GIMP_UNIT_END + number_of_user_units)),
  165.             gimp_unit_defs[GIMP_UNIT_INCH].factor);
  166.  
  167.   if (unit < GIMP_UNIT_END)
  168.     return gimp_unit_defs[unit].factor;
  169.  
  170.   return gimp_unit_get_user_unit (unit)->factor;
  171. }
  172.  
  173.  
  174. gint
  175. gimp_unit_get_digits (GimpUnit unit)
  176. {
  177.   g_return_val_if_fail ((unit >= GIMP_UNIT_PIXEL) &&
  178.             (unit < (GIMP_UNIT_END + number_of_user_units)),
  179.             gimp_unit_defs[GIMP_UNIT_INCH].digits);
  180.  
  181.   if (unit < GIMP_UNIT_END)
  182.     return gimp_unit_defs[unit].digits;
  183.  
  184.   return gimp_unit_get_user_unit (unit)->digits;
  185. }
  186.  
  187.  
  188. gchar * 
  189. gimp_unit_get_identifier (GimpUnit unit)
  190. {
  191.   g_return_val_if_fail ((unit >= GIMP_UNIT_PIXEL) && 
  192.             (unit < (GIMP_UNIT_END + number_of_user_units)) ||
  193.             (unit == GIMP_UNIT_PERCENT),
  194.             gimp_unit_defs[GIMP_UNIT_INCH].identifier);
  195.  
  196.   if (unit < GIMP_UNIT_END)
  197.     return gimp_unit_defs[unit].identifier;
  198.  
  199.   if (unit == GIMP_UNIT_PERCENT)
  200.     return gimp_unit_percent.identifier;
  201.  
  202.   return gimp_unit_get_user_unit (unit)->identifier;
  203. }
  204.  
  205.  
  206. gchar *
  207. gimp_unit_get_symbol (GimpUnit unit)
  208. {
  209.   g_return_val_if_fail ((unit >= GIMP_UNIT_PIXEL) &&
  210.             (unit < (GIMP_UNIT_END + number_of_user_units)) ||
  211.             (unit == GIMP_UNIT_PERCENT),
  212.             gimp_unit_defs[GIMP_UNIT_INCH].symbol);
  213.  
  214.   if (unit < GIMP_UNIT_END)
  215.     return gimp_unit_defs[unit].symbol;
  216.  
  217.   if (unit == GIMP_UNIT_PERCENT)
  218.     return gimp_unit_percent.symbol;
  219.  
  220.   return gimp_unit_get_user_unit (unit)->symbol;
  221. }
  222.  
  223.  
  224. gchar *
  225. gimp_unit_get_abbreviation (GimpUnit unit)
  226. {
  227.   g_return_val_if_fail ((unit >= GIMP_UNIT_PIXEL) &&
  228.             (unit < (GIMP_UNIT_END + number_of_user_units)) ||
  229.             (unit == GIMP_UNIT_PERCENT),
  230.             gimp_unit_defs[GIMP_UNIT_INCH].abbreviation);
  231.  
  232.   if (unit < GIMP_UNIT_END)
  233.     return gimp_unit_defs[unit].abbreviation;
  234.  
  235.   if (unit == GIMP_UNIT_PERCENT)
  236.     return gimp_unit_percent.abbreviation;
  237.  
  238.   return gimp_unit_get_user_unit (unit)->abbreviation;
  239. }
  240.  
  241.  
  242. gchar *
  243. gimp_unit_get_singular (GimpUnit unit)
  244. {
  245.   g_return_val_if_fail ((unit >= GIMP_UNIT_PIXEL) &&
  246.             (unit < (GIMP_UNIT_END + number_of_user_units)) ||
  247.             (unit == GIMP_UNIT_PERCENT),
  248.             gettext (gimp_unit_defs[GIMP_UNIT_INCH].singular));
  249.  
  250.   if (unit < GIMP_UNIT_END)
  251.     return gettext (gimp_unit_defs[unit].singular);
  252.  
  253.   if (unit == GIMP_UNIT_PERCENT)
  254.     return gettext (gimp_unit_percent.singular);
  255.  
  256.   return gettext (gimp_unit_get_user_unit (unit)->singular);
  257. }
  258.  
  259.  
  260. gchar *
  261. gimp_unit_get_plural (GimpUnit unit)
  262. {
  263.   g_return_val_if_fail ((unit >= GIMP_UNIT_PIXEL) &&
  264.             (unit < (GIMP_UNIT_END + number_of_user_units)) ||
  265.             (unit == GIMP_UNIT_PERCENT),
  266.             gettext (gimp_unit_defs[GIMP_UNIT_INCH].plural));
  267.  
  268.   if (unit < GIMP_UNIT_END)
  269.     return gettext (gimp_unit_defs[unit].plural);
  270.  
  271.   if (unit == GIMP_UNIT_PERCENT)
  272.     return gettext (gimp_unit_percent.plural);
  273.  
  274.   return gettext (gimp_unit_get_user_unit (unit)->plural);
  275. }
  276.  
  277.  
  278. /*  unitrc functions  **********/
  279.  
  280. void
  281. parse_unitrc (void)
  282. {
  283.   gchar *filename;
  284.  
  285.   filename = gimp_personal_rc_file ("unitrc");
  286.   parse_gimprc_file (filename);
  287.   g_free (filename);
  288. }
  289.  
  290.  
  291. void
  292. save_unitrc (void)
  293. {
  294.   gint   i;
  295.   gchar *filename;
  296.   FILE  *fp;
  297.  
  298.   filename = gimp_personal_rc_file ("unitrc");
  299.  
  300.   fp = fopen (filename, "w");
  301.   g_free (filename);
  302.   if (!fp)
  303.     return;
  304.  
  305.   fprintf (fp,
  306.        "# GIMP unitrc\n" 
  307.        "# This file contains your user unit database. You can\n"
  308.        "# modify this list with the unit editor. You are not\n"
  309.        "# supposed to edit it manually, but of course you can do.\n"
  310.        "# This file will be entirely rewritten every time you\n"
  311.        "# quit the gimp.\n\n");
  312.  
  313.   /*  save user defined units  */
  314.   for (i = gimp_unit_get_number_of_built_in_units ();
  315.        i < gimp_unit_get_number_of_units ();
  316.        i++)
  317.     if (gimp_unit_get_deletion_flag (i) == FALSE)
  318.       {
  319.     fprintf (fp,
  320.          "(unit-info \"%s\"\n"
  321.          "   (factor %f)\n"
  322.          "   (digits %d)\n"
  323.          "   (symbol \"%s\")\n"
  324.          "   (abbreviation \"%s\")\n"
  325.          "   (singular \"%s\")\n"
  326.          "   (plural \"%s\"))\n\n",
  327.          gimp_unit_get_identifier (i),
  328.          gimp_unit_get_factor (i),
  329.          gimp_unit_get_digits (i),
  330.          gimp_unit_get_symbol (i),
  331.          gimp_unit_get_abbreviation (i),
  332.          gimp_unit_get_singular (i),
  333.          gimp_unit_get_plural (i));
  334.       }
  335.  
  336.   fclose (fp);
  337. }
  338.