home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / common / xbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-22  |  32.8 KB  |  1,327 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * X10 and X11 bitmap (XBM) loading and saving file filter for the GIMP.
  5.  * XBM code Copyright (C) 1998 Gordon Matzigkeit
  6.  *
  7.  * The XBM reading and writing code was written from scratch by Gordon
  8.  * Matzigkeit <gord@gnu.org> based on the XReadBitmapFile(3X11) manual
  9.  * page distributed with X11R6 and by staring at valid XBM files.  It
  10.  * does not contain any code written for other XBM file loaders.
  11.  *
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the Free Software
  24.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  25.  
  26. /* Release 1.0, 1998-02-04, Gordon Matzigkeit <gord@gnu.org>:
  27.  *   - Load and save X10 and X11 bitmaps.
  28.  *   - Allow the user to specify the C identifier prefix.
  29.  *
  30.  * TODO:
  31.  *   - Parsing is very tolerant, and the algorithms are quite hairy, so
  32.  *     load_image should be carefully tested to make sure there are no XBM's
  33.  *     that fail.
  34.  */
  35.  
  36. /* Set this for debugging. */
  37. /* #define VERBOSE 2 */
  38.  
  39. #include "config.h"
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <ctype.h>
  45.  
  46. #include <gtk/gtk.h>
  47.  
  48. #include <libgimp/gimp.h>
  49. #include <libgimp/gimpui.h>
  50.  
  51. #include "libgimp/stdplugins-intl.h"
  52.  
  53.  
  54. /* Wear your GIMP with pride! */
  55. #define DEFAULT_USE_COMMENT TRUE
  56. #define MAX_COMMENT         72
  57. #define MAX_MASK_EXT        32
  58.  
  59. /* C identifier prefix. */
  60. #define DEFAULT_PREFIX "bitmap"
  61. #define MAX_PREFIX     24
  62.  
  63. /* Whether or not to save as X10 bitmap. */
  64. #define DEFAULT_X10_FORMAT FALSE
  65.  
  66. typedef struct _XBMSaveVals
  67. {
  68.   gchar    comment[MAX_COMMENT + 1];
  69.   gint     x10_format;
  70.   gint     use_hot;
  71.   gint     x_hot;
  72.   gint     y_hot;
  73.   gchar    prefix[MAX_PREFIX + 1];
  74.   gboolean write_mask;
  75.   gchar    mask_ext[MAX_MASK_EXT + 1];
  76. } XBMSaveVals;
  77.  
  78. static XBMSaveVals xsvals =
  79. {
  80.   "###",        /* comment */
  81.   DEFAULT_X10_FORMAT,    /* x10_format */
  82.   FALSE,
  83.   0,            /* x_hot */
  84.   0,            /* y_hot */
  85.   DEFAULT_PREFIX,    /* prefix */
  86.   FALSE,                /* write_mask */
  87.   "_mask"
  88. };
  89.  
  90. typedef struct _XBMSaveInterface
  91. {
  92.   gint run;
  93. } XBMSaveInterface;
  94.  
  95. static XBMSaveInterface xsint =
  96. {
  97.   FALSE            /* run */
  98. };
  99.  
  100.  
  101. /* Declare some local functions.
  102.  */
  103. static void   query   (void);
  104. static void   run     (gchar      *name,
  105.                gint        nparams,
  106.                GimpParam  *param,
  107.                gint       *nreturn_vals,
  108.                GimpParam **return_vals);
  109.  
  110. static gint32 load_image              (gchar     *filename);
  111. static gint   save_image              (gchar     *filename,
  112.                        gchar     *prefix,
  113.                        gchar     *comment,
  114.                        gboolean   save_mask,
  115.                        gint32     image_ID, 
  116.                        gint32     drawable_ID);
  117. static gint   save_dialog             (gint32     drawable_ID);
  118. static void   save_ok_callback        (GtkWidget *widget,
  119.                        gpointer   data);
  120. static void   comment_entry_callback  (GtkWidget *widget,
  121.                        gpointer   data);
  122. static void   prefix_entry_callback   (GtkWidget *widget,
  123.                        gpointer   data);
  124. static void   mask_ext_entry_callback (GtkWidget *widget,
  125.                        gpointer   data);
  126.  
  127. GimpPlugInInfo PLUG_IN_INFO =
  128. {
  129.   NULL,  /* init_proc  */
  130.   NULL,  /* quit_proc  */
  131.   query, /* query_proc */
  132.   run,   /* run_proc   */
  133. };
  134.  
  135. MAIN ()
  136.  
  137. #ifdef VERBOSE
  138. static int verbose = VERBOSE;
  139. #endif
  140.  
  141. static void
  142. query (void)
  143. {
  144.   static GimpParamDef load_args[] =
  145.   {
  146.     { GIMP_PDB_INT32,  "run_mode",     "Interactive, non-interactive" },
  147.     { GIMP_PDB_STRING, "filename",     "The name of the file to load" },
  148.     { GIMP_PDB_STRING, "raw_filename", "The name entered" }
  149.   };
  150.   static gint nload_args = sizeof (load_args) / sizeof (load_args[0]);
  151.  
  152.   static GimpParamDef load_return_vals[] =
  153.   {
  154.     { GIMP_PDB_IMAGE,  "image",        "Output image" }
  155.   };
  156.   static gint nload_return_vals = (sizeof (load_return_vals) /
  157.                    sizeof (load_return_vals[0]));
  158.  
  159.   static GimpParamDef save_args[] =
  160.   {
  161.     { GIMP_PDB_INT32,    "run_mode",       "Interactive, non-interactive" },
  162.     { GIMP_PDB_IMAGE,    "image",          "Input image" },
  163.     { GIMP_PDB_DRAWABLE, "drawable",       "Drawable to save" },
  164.     { GIMP_PDB_STRING,   "filename",       "The name of the file to save" },
  165.     { GIMP_PDB_STRING,   "raw_filename",   "The name entered" },
  166.     { GIMP_PDB_STRING,   "comment",        "Image description (maximum 72 bytes)" },
  167.     { GIMP_PDB_INT32,    "x10",            "Save in X10 format" },
  168.     { GIMP_PDB_INT32,    "x_hot",          "X coordinate of hotspot" },
  169.     { GIMP_PDB_INT32,    "y_hot",          "Y coordinate of hotspot" },
  170.     { GIMP_PDB_STRING,   "prefix",         "Identifier prefix [determined from filename]"},
  171.     { GIMP_PDB_INT32,    "write_mask",     "(0 = ignore, 1 = save as extra file)" },
  172.     { GIMP_PDB_STRING,   "mask_extension", "Extension of the mask file" }
  173.   } ;
  174.   static gint nsave_args = sizeof (save_args) / sizeof (save_args[0]);
  175.  
  176.   gimp_install_procedure ("file_xbm_load",
  177.                           "Load a file in X10 or X11 bitmap (XBM) file format",
  178.                           "Load a file in X10 or X11 bitmap (XBM) file format.  XBM is a lossless format for flat black-and-white (two color indexed) images.",
  179.                           "Gordon Matzigkeit",
  180.                           "Gordon Matzigkeit",
  181.                           "1998",
  182.                           "<Load>/XBM",
  183.               NULL,
  184.                           GIMP_PLUGIN,
  185.                           nload_args, nload_return_vals,
  186.                           load_args, load_return_vals);
  187.  
  188.   gimp_install_procedure ("file_xbm_save",
  189.                           "Save a file in X10 or X11 bitmap (XBM) file format",
  190.                           "Save a file in X10 or X11 bitmap (XBM) file format.  XBM is a lossless format for flat black-and-white (two color indexed) images.",
  191.               "Gordon Matzigkeit",
  192.                           "Gordon Matzigkeit",
  193.                           "1998",
  194.                           "<Save>/XBM",
  195.               "INDEXED",
  196.                           GIMP_PLUGIN,
  197.                           nsave_args, 0,
  198.                           save_args, NULL);
  199.  
  200.   gimp_register_load_handler ("file_xbm_load",
  201.                   "xbm,icon,bitmap",
  202.                   "");
  203.   gimp_register_save_handler ("file_xbm_save",
  204.                   "xbm,icon,bitmap",
  205.                   "");
  206. }
  207.  
  208. static gchar *
  209. init_prefix (gchar *filename)
  210. {
  211.   gchar *p, *prefix;
  212.   gint len;
  213.  
  214.   prefix = g_basename (filename);
  215.  
  216.   /* Strip any extension. */
  217.   p = strrchr (prefix, '.');
  218.   if (p && p != prefix)
  219.     len = MIN (MAX_PREFIX, p - prefix);
  220.   else
  221.     len = MAX_PREFIX;
  222.  
  223.   memset (xsvals.prefix, 0, sizeof (xsvals.prefix));
  224.   strncpy (xsvals.prefix, prefix, len);
  225.  
  226.   return xsvals.prefix;
  227. }
  228.  
  229. static void
  230. run (gchar      *name,
  231.      gint        nparams,
  232.      GimpParam  *param,
  233.      gint       *nreturn_vals,
  234.      GimpParam **return_vals)
  235. {
  236.   static GimpParam   values[2];
  237.   GimpRunModeType    run_mode;
  238.   GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
  239.   gint32             image_ID;
  240.   gint32             drawable_ID;
  241.   GimpParasite      *parasite = NULL; 
  242.   gchar             *mask_filename = NULL;
  243.   GimpExportReturnType export = GIMP_EXPORT_CANCEL;
  244.  
  245.   INIT_I18N_UI();
  246.   strncpy (xsvals.comment, _("Created with The GIMP"), MAX_COMMENT);
  247.  
  248.   run_mode = param[0].data.d_int32;
  249.  
  250.   *nreturn_vals = 1;
  251.   *return_vals  = values;
  252.   values[0].type          = GIMP_PDB_STATUS;
  253.   values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  254.  
  255. #ifdef VERBOSE
  256.   if (verbose)
  257.     printf ("XBM: RUN %s\n", name);
  258. #endif
  259.  
  260.   if (strcmp (name, "file_xbm_load") == 0)
  261.     {
  262.       image_ID = load_image (param[1].data.d_string);
  263.  
  264.       if (image_ID != -1)
  265.         {
  266.           *nreturn_vals = 2;
  267.           values[1].type         = GIMP_PDB_IMAGE;
  268.           values[1].data.d_image = image_ID;
  269.         }
  270.       else
  271.         {
  272.           status = GIMP_PDB_EXECUTION_ERROR;
  273.         }
  274.     }
  275.   else if (strcmp (name, "file_xbm_save") == 0)
  276.     {
  277.       image_ID = param[1].data.d_int32;
  278.       drawable_ID = param[2].data.d_int32;
  279.  
  280.       /*  eventually export the image */ 
  281.       switch (run_mode)
  282.     {
  283.     case GIMP_RUN_INTERACTIVE:
  284.     case GIMP_RUN_WITH_LAST_VALS:
  285.       gimp_ui_init ("xbm", FALSE);
  286.       export = gimp_export_image (&image_ID, &drawable_ID, "XBM",
  287.                       GIMP_EXPORT_CAN_HANDLE_INDEXED |
  288.                       GIMP_EXPORT_CAN_HANDLE_ALPHA );
  289.       if (export == GIMP_EXPORT_CANCEL)
  290.         {
  291.           values[0].data.d_status = GIMP_PDB_CANCEL;
  292.           return;
  293.       }
  294.       break;
  295.     default:
  296.       break;
  297.     }
  298.  
  299.       switch (run_mode)
  300.     {
  301.     case GIMP_RUN_INTERACTIVE:
  302.     case GIMP_RUN_WITH_LAST_VALS:
  303.       /*  Possibly retrieve data  */
  304.       gimp_get_data ("file_xbm_save", &xsvals);
  305.  
  306.       /* Always override the prefix with the filename. */
  307.       mask_filename = g_strdup (init_prefix (param[3].data.d_string));
  308.       break;
  309.  
  310.     case GIMP_RUN_NONINTERACTIVE:
  311.       /*  Make sure all the required arguments are there!  */
  312.       if (nparams < 5)
  313.         {
  314.           status = GIMP_PDB_CALLING_ERROR;
  315.         }
  316.       else
  317.         {
  318.           gint i = 5;
  319.  
  320.           if (nparams > i)
  321.         {
  322.           memset (xsvals.comment, 0, sizeof (xsvals.comment));
  323.           strncpy (xsvals.comment, param[i].data.d_string,
  324.                MAX_COMMENT);
  325.         }
  326.  
  327.           i ++;
  328.           if (nparams > i)
  329.         xsvals.x10_format = (param[i].data.d_int32) ? TRUE : FALSE;
  330.  
  331.           i += 2;
  332.           if (nparams > i)
  333.         {
  334.           /* They've asked for a hotspot. */
  335.           xsvals.use_hot = TRUE;
  336.           xsvals.x_hot = param[i - 1].data.d_int32;
  337.           xsvals.y_hot = param[i].data.d_int32;
  338.         }
  339.  
  340.           mask_filename = g_strdup (init_prefix (param[3].data.d_string));
  341.  
  342.           i ++;
  343.           if (nparams > i)
  344.         {
  345.           memset (xsvals.prefix, 0, sizeof (xsvals.prefix));
  346.           strncpy (xsvals.prefix, param[i].data.d_string,
  347.                MAX_PREFIX);
  348.         }
  349.  
  350.           i += 2;
  351.           if (nparams > i)
  352.         {
  353.           xsvals.write_mask = param[i - 1].data.d_int32;
  354.           memset (xsvals.mask_ext, 0, sizeof (xsvals.mask_ext));
  355.           strncpy (xsvals.mask_ext, param[i].data.d_string,
  356.                MAX_MASK_EXT);
  357.         }
  358.  
  359.           i ++;
  360.           /* Too many arguments. */
  361.           if (nparams > i)
  362.         status = GIMP_PDB_CALLING_ERROR;
  363.         }
  364.       break;
  365.  
  366.     default:
  367.       break;
  368.     }
  369.  
  370.       if (run_mode == GIMP_RUN_INTERACTIVE)
  371.     {
  372.       /* Get the parasites */
  373.           parasite = gimp_image_parasite_find (image_ID, "gimp-comment");
  374.  
  375.           if (parasite)
  376.             {
  377.           gpointer data;
  378.           gint     size;
  379.  
  380.           data = gimp_parasite_data (parasite);
  381.           size = gimp_parasite_data_size (parasite);
  382.  
  383.           strncpy (xsvals.comment, data, MIN (size, MAX_COMMENT));
  384.           xsvals.comment[MIN (size, MAX_COMMENT) + 1] = 0;
  385.  
  386.           gimp_parasite_free (parasite);
  387.         }
  388.  
  389.           parasite = gimp_image_parasite_find (image_ID, "hot-spot");
  390.  
  391.           if (parasite)
  392.         {
  393.           gpointer data;
  394.           gint     x, y;
  395.  
  396.           data = gimp_parasite_data (parasite);
  397.  
  398.           if (sscanf (data, "%i %i", &x, &y) == 2)
  399.            {
  400.              xsvals.use_hot = TRUE;
  401.              xsvals.x_hot = x;
  402.              xsvals.y_hot = y;
  403.            }
  404.          gimp_parasite_free (parasite);
  405.        }
  406.  
  407.       /*  Acquire information with a dialog  */
  408.       if (! save_dialog (drawable_ID))
  409.         status = GIMP_PDB_CANCEL;
  410.     }
  411.  
  412.       if (status == GIMP_PDB_SUCCESS)
  413.     {
  414.       gchar *temp;
  415.       gchar *mask_prefix;
  416.       gchar *dirname;
  417.  
  418.       temp = mask_filename;
  419.  
  420.       if ((dirname = g_dirname (param[3].data.d_string)) != NULL)
  421.         {
  422.           mask_filename = g_strdup_printf ("%s/%s%s.xbm",
  423.                            dirname, temp, xsvals.mask_ext);
  424.           g_free (dirname);
  425.         }
  426.       else
  427.         {
  428.           mask_filename = g_strdup_printf ("%s%s.xbm",
  429.                            temp, xsvals.mask_ext);
  430.         }
  431.  
  432.       g_free (temp);
  433.  
  434.       /* Change any non-alphanumeric prefix characters to underscores. */
  435.       temp = xsvals.prefix;
  436.       while (*temp)
  437.         {
  438.           if (!isalnum (*temp))
  439.         *temp = '_';
  440.           temp ++;
  441.         }
  442.  
  443.       mask_prefix = g_strdup_printf ("%s%s", xsvals.prefix, xsvals.mask_ext);
  444.  
  445.       if (save_image (param[3].data.d_string,
  446.               xsvals.prefix,
  447.               xsvals.comment,
  448.               FALSE,
  449.               image_ID, drawable_ID) &&
  450.  
  451.           xsvals.write_mask &&
  452.  
  453.           save_image (mask_filename,
  454.               mask_prefix,
  455.               xsvals.comment,
  456.               TRUE,
  457.               image_ID, drawable_ID))
  458.         {
  459.           /*  Store xsvals data  */
  460.           gimp_set_data ("file_xbm_save", &xsvals, sizeof (xsvals));
  461.         }
  462.       else
  463.         {
  464.           status = GIMP_PDB_EXECUTION_ERROR;
  465.         }
  466.  
  467.       g_free (mask_prefix);
  468.       g_free (mask_filename);
  469.     }
  470.  
  471.       if (export == GIMP_EXPORT_EXPORT)
  472.     gimp_image_delete (image_ID);
  473.     }
  474.   else
  475.     {
  476.       status = GIMP_PDB_CALLING_ERROR;
  477.     }
  478.  
  479.   values[0].data.d_status = status;
  480. }
  481.  
  482.  
  483. /* Return the value of a digit. */
  484. static gint
  485. getval (gint c, 
  486.     gint base)
  487. {
  488.   static guchar *digits = "0123456789abcdefABCDEF";
  489.   gint val;
  490.  
  491.   /* Include uppercase hex digits. */
  492.   if (base == 16)
  493.     base = 22;
  494.  
  495.   /* Find a match. */
  496.   for (val = 0; val < base; val ++)
  497.     if (c == digits[val])
  498.       return (val < 16) ? val : (val - 6);
  499.   return -1;
  500. }
  501.  
  502.  
  503. /* Get a comment */
  504. static gchar *
  505. fgetcomment (FILE *fp)
  506. {
  507.   GString *str = NULL;
  508.   gint comment, c;
  509.  
  510.   comment = 0;
  511.   do
  512.     {
  513.       c = fgetc (fp);
  514.       if (comment)
  515.     {
  516.       if (c == '*')
  517.         {
  518.           /* In a comment, with potential to leave. */
  519.           comment = 1;
  520.         }
  521.       else if (comment == 1 && c == '/')
  522.         {
  523.           gchar *retval;
  524.  
  525.           /* Leaving a comment. */
  526.           comment = 0;
  527.  
  528.           retval = g_strstrip (g_strdup (str->str));
  529.           g_string_free (str, TRUE);
  530.           return retval;
  531.         }
  532.       else
  533.         {
  534.           /* In a comment, with no potential to leave. */
  535.           comment = 2;
  536.           g_string_append_c (str, c);
  537.         }
  538.     }
  539.       else
  540.     {
  541.       /* Not in a comment. */
  542.       if (c == '/')
  543.         {
  544.           /* Potential to enter a comment. */
  545.           c = fgetc (fp);
  546.           if (c == '*')
  547.         {
  548.           /* Entered a comment, with no potential to leave. */
  549.           comment = 2;
  550.           str = g_string_new (NULL);
  551.         }
  552.           else
  553.         {
  554.           /* put everything back and return */
  555.           ungetc (c, fp);
  556.           c = '/';
  557.           ungetc (c, fp);
  558.           return NULL;
  559.         }
  560.         }
  561.       else if (isspace (c))
  562.         {
  563.           /* Skip leading whitespace */
  564.           continue;
  565.         }
  566.     }
  567.     }
  568.   while (comment && c != EOF);
  569.  
  570.   if (str)
  571.     g_string_free (str, TRUE);
  572.  
  573.   return NULL;
  574. }
  575.  
  576.  
  577. /* Same as fgetc, but skip C-style comments and insert whitespace. */
  578. static gint
  579. cpp_fgetc (FILE *fp)
  580. {
  581.   gint comment, c;
  582.  
  583.   /* FIXME: insert whitespace as advertised. */
  584.   comment = 0;
  585.   do
  586.     {
  587.       c = fgetc (fp);
  588.       if (comment)
  589.     {
  590.       if (c == '*')
  591.         /* In a comment, with potential to leave. */
  592.         comment = 1;
  593.       else if (comment == 1 && c == '/')
  594.         /* Leaving a comment. */
  595.         comment = 0;
  596.       else
  597.         /* In a comment, with no potential to leave. */
  598.         comment = 2;
  599.     }
  600.       else
  601.     {
  602.       /* Not in a comment. */
  603.       if (c == '/')
  604.         {
  605.           /* Potential to enter a comment. */
  606.           c = fgetc (fp);
  607.           if (c == '*')
  608.         /* Entered a comment, with no potential to leave. */
  609.         comment = 2;
  610.           else
  611.         {
  612.           /* Just a slash in the open. */
  613.           ungetc (c, fp);
  614.           c = '/';
  615.         }
  616.         }
  617.     }
  618.     }
  619.   while (comment && c != EOF);
  620.   return c;
  621. }
  622.  
  623.  
  624. /* Match a string with a file. */
  625. static gint
  626. match (FILE  *fp, 
  627.        gchar *s)
  628. {
  629.   gint c;
  630.  
  631.   do
  632.     {
  633.       c = fgetc (fp);
  634.       if (c == *s)
  635.     s ++;
  636.       else
  637.     break;
  638.     }
  639.   while (c != EOF && *s);
  640.  
  641.   if (!*s)
  642.     return TRUE;
  643.  
  644.   if (c != EOF)
  645.     ungetc (c, fp);
  646.   return FALSE;
  647. }
  648.  
  649.  
  650. /* Read the next integer from the file, skipping all non-integers. */
  651. static gint
  652. get_int (FILE *fp)
  653. {
  654.   int digval, base, val, c;
  655.  
  656.   do
  657.     c = cpp_fgetc (fp);
  658.   while (c != EOF && !isdigit (c));
  659.  
  660.   if (c == EOF)
  661.     return 0;
  662.  
  663.   /* Check for the base. */
  664.   if (c == '0')
  665.     {
  666.       c = fgetc (fp);
  667.       if (c == 'x' || c == 'X')
  668.     {
  669.       c = fgetc (fp);
  670.       base = 16;
  671.     }
  672.       else if (isdigit (c))
  673.     base = 8;
  674.       else
  675.     {
  676.       ungetc (c, fp);
  677.       return 0;
  678.     }
  679.     }
  680.   else
  681.     base = 10;
  682.  
  683.   val = 0;
  684.   for (;;)
  685.     {
  686.       digval = getval (c, base);
  687.       if (digval == -1)
  688.     {
  689.       ungetc (c, fp);
  690.       break;
  691.     }
  692.       val *= base;
  693.       val += digval;
  694.       c = fgetc (fp);
  695.     }
  696.  
  697.   return val;
  698. }
  699.  
  700.  
  701. static gint
  702. load_image (gchar *filename)
  703. {
  704.   FILE *fp;
  705.   gint32 image_ID, layer_ID;
  706.  
  707.   GimpPixelRgn  pixel_rgn;
  708.   GimpDrawable *drawable;
  709.   guchar *data;
  710.   gint    intbits;
  711.   gint    width = 0;
  712.   gint    height = 0;
  713.   gint    x_hot = 0;
  714.   gint    y_hot = 0;
  715.   gint    c, i, j, k;
  716.   gint    tileheight, rowoffset;
  717.  
  718.   gchar *name_buf;
  719.   gchar *comment;
  720.  
  721.   guchar cmap[] =
  722.   {
  723.     0x00, 0x00, 0x00,        /* black */
  724.     0xff, 0xff, 0xff        /* white */
  725.   };
  726.  
  727.   fp = fopen (filename, "rb");
  728.   if (!fp)
  729.     {
  730.       g_message (_("XBM: cannot open \"%s\"\n"), filename);
  731.       return -1;
  732.     }
  733.  
  734.   name_buf = g_strdup_printf (_("Loading %s:"), filename);
  735.   gimp_progress_init (name_buf);
  736.   g_free (name_buf);
  737.  
  738.   comment = fgetcomment (fp);
  739.  
  740.   /* Loosely parse the header */
  741.   intbits = height = width = 0;
  742.   c = ' ';
  743.   do
  744.     {
  745.       if (isspace (c))
  746.     {
  747.       if (match (fp, "char"))
  748.         {
  749.           c = fgetc (fp);
  750.           if (isspace (c))
  751.         {
  752.           intbits = 8;
  753.           continue;
  754.         }
  755.         }
  756.       else if (match (fp, "short"))
  757.         {
  758.           c = fgetc (fp);
  759.           if (isspace (c))
  760.         {
  761.           intbits = 16;
  762.           continue;
  763.         }
  764.         }
  765.     }
  766.  
  767.       if (c == '_')
  768.     {
  769.       if (match (fp, "width"))
  770.         {
  771.           c = fgetc (fp);
  772.           if (isspace (c))
  773.         {
  774.           width = get_int (fp);
  775.           continue;
  776.         }
  777.         }
  778.       else if (match (fp, "height"))
  779.         {
  780.           c = fgetc (fp);
  781.           if (isspace (c))
  782.         {
  783.           height = get_int (fp);
  784.           continue;
  785.         }
  786.         }
  787.       else if (match (fp, "x_hot"))
  788.         {
  789.           c = fgetc (fp);
  790.           if (isspace (c))
  791.         {
  792.           x_hot = get_int (fp);
  793.           continue;
  794.         }
  795.         }
  796.       else if (match (fp, "y_hot"))
  797.         {
  798.           c = fgetc (fp);
  799.           if (isspace (c))
  800.         {
  801.           y_hot = get_int (fp);
  802.           continue;
  803.         }
  804.         }
  805.     }
  806.  
  807.       c = cpp_fgetc (fp);
  808.     }
  809.   while (c != '{' && c != EOF);
  810.  
  811.   if (c == EOF)
  812.     {
  813.       g_message (_("XBM: cannot read header (ftell == %ld)\n"), ftell (fp));
  814.       return -1;
  815.     }
  816.  
  817.   if (width == 0)
  818.     {
  819.       g_message (_("XBM: no image width specified\n"));
  820.       return -1;
  821.     }
  822.  
  823.   if (height == 0)
  824.     {
  825.       g_message (_("XBM: no image height specified\n"));
  826.       return -1;
  827.     }
  828.  
  829.   if (intbits == 0)
  830.     {
  831.       g_message (_("XBM: no image data type specified\n"));
  832.       return -1;
  833.     }
  834.  
  835.   image_ID = gimp_image_new (width, height, GIMP_INDEXED);
  836.   gimp_image_set_filename (image_ID, filename);
  837.  
  838.   if (comment)
  839.     {
  840.       GimpParasite *parasite;
  841.  
  842.       parasite = gimp_parasite_new ("gimp-comment",
  843.                     GIMP_PARASITE_PERSISTENT,
  844.                     strlen (comment) + 1, (gpointer) comment);
  845.       gimp_image_parasite_attach (image_ID, parasite);
  846.       gimp_parasite_free (parasite);
  847.  
  848.       g_free (comment);
  849.     }
  850.  
  851.   x_hot = CLAMP (x_hot, 0, width);
  852.   y_hot = CLAMP (y_hot, 0, height);
  853.  
  854.   if (x_hot > 0 || y_hot > 0)
  855.     {
  856.       GimpParasite *parasite;
  857.       gchar        *str;
  858.  
  859.       str = g_strdup_printf ("%d %d", x_hot, y_hot);
  860.       parasite = gimp_parasite_new ("hot-spot",
  861.                     GIMP_PARASITE_PERSISTENT,
  862.                     strlen (str) + 1, (gpointer) str);
  863.       g_free (str);
  864.       gimp_image_parasite_attach (image_ID, parasite);
  865.       gimp_parasite_free (parasite);
  866.     }
  867.  
  868.   /* Set a black-and-white colormap. */
  869.   gimp_image_set_cmap (image_ID, cmap, 2);
  870.  
  871.   layer_ID = gimp_layer_new (image_ID,
  872.                  _("Background"),
  873.                  width, height,
  874.                  GIMP_INDEXED_IMAGE,
  875.                  100,
  876.                  GIMP_NORMAL_MODE);
  877.   gimp_image_add_layer (image_ID, layer_ID, 0);
  878.  
  879.   drawable = gimp_drawable_get (layer_ID);
  880.  
  881.   /* Prepare the pixel region. */
  882.   gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, width, height, TRUE, FALSE);
  883.  
  884.   /* Allocate the data. */
  885.   tileheight = gimp_tile_height ();
  886.   data = (guchar *) g_malloc (width * tileheight);
  887.  
  888.   for (i = 0; i < height; i += tileheight)
  889.     {
  890.       tileheight = MIN (tileheight, height - i);
  891.  
  892. #ifdef VERBOSE
  893.       if (verbose > 1)
  894.     printf ("XBM: reading %dx(%d+%d) pixel region\n", width, i,
  895.         tileheight);
  896. #endif
  897.  
  898.       /* Parse the data from the file */
  899.       for (j = 0; j < tileheight; j ++)
  900.     {
  901.       /* Read each row. */
  902.       rowoffset = j * width;
  903.       for (k = 0; k < width; k ++)
  904.         {
  905.           /* Expand each integer into INTBITS pixels. */
  906.           if (k % intbits == 0)
  907.         {
  908.           c = get_int (fp);
  909.  
  910.           /* Flip all the bits so that 1's become black and
  911.                      0's become white. */
  912.           c ^= 0xffff;
  913.         }
  914.  
  915.           data[rowoffset + k] = c & 1;
  916.           c >>= 1;
  917.         }
  918.     }
  919.  
  920.       /* Put the data into the image. */
  921.       gimp_progress_update ((double) (i + tileheight) / (double) height);
  922.       gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i, width, tileheight);
  923.     }
  924.  
  925.   g_free (data);
  926.  
  927.   gimp_drawable_flush (drawable);
  928.   gimp_drawable_detach (drawable);
  929.  
  930.   fclose (fp);
  931.  
  932.   return image_ID;
  933. }
  934.  
  935. static gboolean
  936. save_image (gchar    *filename,
  937.         gchar    *prefix,
  938.         gchar    *comment,
  939.         gboolean  save_mask,
  940.         gint32    image_ID,
  941.         gint32    drawable_ID)
  942. {
  943.   GimpDrawable *drawable;
  944.   GimpPixelRgn  pixel_rgn;
  945.   FILE *fp;
  946.  
  947.   gint width, height, colors, dark;
  948.   gint intbits, lineints, need_comma, nints, rowoffset, tileheight;
  949.   gint c, i, j, k, thisbit;
  950.  
  951.   gboolean has_alpha;
  952.   gint     bpp;
  953.  
  954.   guchar *data, *cmap, *name_buf, *intfmt;
  955.  
  956.   drawable = gimp_drawable_get (drawable_ID);
  957.   width  = drawable->width;
  958.   height = drawable->height;
  959.   cmap = gimp_image_get_cmap (image_ID, &colors);
  960.  
  961.   if (!gimp_drawable_is_indexed (drawable_ID) || colors > 2)
  962.     {
  963.       /* The image is not black-and-white. */
  964.       g_message (_("The image which you are trying to save as\n"
  965.            "an XBM contains more than two colors.\n\n"
  966.            "Please convert it to a black and white\n"
  967.            "(1-bit) indexed image and try again."));
  968.       return FALSE;
  969.     }
  970.  
  971.   has_alpha = gimp_drawable_has_alpha (drawable_ID);
  972.  
  973.   if (!has_alpha && save_mask)
  974.     {
  975.       g_message (_("You cannot save a cursor mask for an image\n"
  976.            "which has no alpha channel."));
  977.       return FALSE;
  978.     }
  979.  
  980.   bpp = gimp_drawable_bpp (drawable_ID);
  981.  
  982.   name_buf = g_strdup_printf (_("Saving %s:"), filename);
  983.   gimp_progress_init (name_buf);
  984.   g_free (name_buf);
  985.  
  986.   /* Figure out which color is black, and which is white. */
  987.   dark = 0;
  988.   if (colors > 1)
  989.     {
  990.       gint first, second;
  991.  
  992.       /* Maybe the second color is darker than the first. */
  993.       first  = (cmap[0] * cmap[0]) + (cmap[1] * cmap[1]) + (cmap[2] * cmap[2]);
  994.       second = (cmap[3] * cmap[3]) + (cmap[4] * cmap[4]) + (cmap[5] * cmap[5]);
  995.  
  996.       if (second < first)
  997.     dark = 1;
  998.     }
  999.  
  1000.   /* Now actually save the data. */
  1001.   fp = fopen (filename, "w");
  1002.   if (!fp)
  1003.     {
  1004.       g_message (_("XBM: cannot create \"%s\"\n"), filename);
  1005.       return FALSE;
  1006.     }
  1007.  
  1008.   /* Maybe write the image comment. */
  1009.   if (*comment)
  1010.     fprintf (fp, "/* %s */\n", comment);
  1011.  
  1012.   /* Write out the image height and width. */
  1013.   fprintf (fp, "#define %s_width %d\n",  prefix, width);
  1014.   fprintf (fp, "#define %s_height %d\n", prefix, height);
  1015.  
  1016.   /* Write out the hotspot, if any. */
  1017.   if (xsvals.use_hot)
  1018.     {
  1019.       fprintf (fp, "#define %s_x_hot %d\n", prefix, xsvals.x_hot);
  1020.       fprintf (fp, "#define %s_y_hot %d\n", prefix, xsvals.y_hot);
  1021.     }
  1022.  
  1023.   /* Now write the actual data. */
  1024.   if (xsvals.x10_format)
  1025.     {
  1026.       /* We can fit 9 hex shorts on a single line. */
  1027.       lineints = 9;
  1028.       intbits = 16;
  1029.       intfmt = " 0x%04x";
  1030.     }
  1031.   else
  1032.     {
  1033.       /* We can fit 12 hex chars on a single line. */
  1034.       lineints = 12;
  1035.       intbits = 8;
  1036.       intfmt = " 0x%02x";
  1037.     }
  1038.  
  1039.   fprintf (fp, "static %s %s_bits[] = {\n  ",
  1040.        xsvals.x10_format ? "unsigned short" : "unsigned char", prefix);
  1041.  
  1042.   /* Allocate a new set of pixels. */
  1043.   tileheight = gimp_tile_height ();
  1044.   data = (guchar *) g_malloc (width * tileheight * bpp);
  1045.  
  1046.   gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, width, height,
  1047.                FALSE, FALSE);
  1048.  
  1049.   /* Write out the integers. */
  1050.   need_comma = 0;
  1051.   nints = 0;
  1052.   for (i = 0; i < height; i += tileheight)
  1053.     {
  1054.       /* Get a horizontal slice of the image. */
  1055.       tileheight = MIN (tileheight, height - i);
  1056.       gimp_pixel_rgn_get_rect (&pixel_rgn, data, 0, i, width, tileheight);
  1057.  
  1058. #ifdef VERBOSE
  1059.       if (verbose > 1)
  1060.     printf ("TGA: writing %dx(%d+%d) pixel region\n",
  1061.         width, i, tileheight);
  1062. #endif
  1063.  
  1064.       for (j = 0; j < tileheight; j ++)
  1065.     {
  1066.       /* Write out a row at a time. */
  1067.       rowoffset = j * width * bpp;
  1068.       c = 0;
  1069.       thisbit = 0;
  1070.  
  1071.       for (k = 0; k < width * bpp; k += bpp)
  1072.         {
  1073.           if (k != 0 && thisbit == intbits)
  1074.         {
  1075.           /* Output a completed integer. */
  1076.           if (need_comma)
  1077.             fputc (',', fp);
  1078.           need_comma = 1;
  1079.  
  1080.           /* Maybe start a new line. */
  1081.           if (nints ++ >= lineints)
  1082.             {
  1083.               nints = 1;
  1084.               fputs ("\n  ", fp);
  1085.             }
  1086.           fprintf (fp, intfmt, c);
  1087.  
  1088.           /* Start a new integer. */
  1089.           c = 0;
  1090.           thisbit = 0;
  1091.         }
  1092.  
  1093.           /* Pack INTBITS pixels into an integer. */
  1094.           if (save_mask)
  1095.         {
  1096.           c |= ((data[rowoffset + k + 1] < 128) ? 0 : 1) << (thisbit ++);
  1097.         }
  1098.           else
  1099.         {
  1100.           if (has_alpha && (data[rowoffset + k + 1] < 128))
  1101.             c |= 0 << (thisbit ++);
  1102.           else
  1103.             c |= ((data[rowoffset + k] == dark) ? 1 : 0) << (thisbit ++);
  1104.         }
  1105.         }
  1106.  
  1107.       if (thisbit != 0)
  1108.         {
  1109.           /* Write out the last oddball int. */
  1110.           if (need_comma)
  1111.         fputc (',', fp);
  1112.           need_comma = 1;
  1113.  
  1114.           /* Maybe start a new line. */
  1115.           if (nints ++ == lineints)
  1116.         {
  1117.           nints = 1;
  1118.           fputs ("\n  ", fp);
  1119.         }
  1120.           fprintf (fp, intfmt, c);
  1121.         }
  1122.     }
  1123.  
  1124.       gimp_progress_update ((double) (i + tileheight) / (double) height);
  1125.     }
  1126.  
  1127.   /* Write the trailer. */
  1128.   fprintf (fp, " };\n");
  1129.   fclose (fp);
  1130.   return TRUE;
  1131. }
  1132.  
  1133. static gint
  1134. save_dialog (gint32 drawable_ID)
  1135. {
  1136.   GtkWidget *dlg;
  1137.   GtkWidget *frame;
  1138.   GtkWidget *vbox;
  1139.   GtkWidget *toggle;
  1140.   GtkWidget *table;
  1141.   GtkWidget *entry;
  1142.   GtkWidget *spinbutton;
  1143.   GtkObject *adj;
  1144.  
  1145.   dlg = gimp_dialog_new (_("Save as XBM"), "xbm",
  1146.              gimp_standard_help_func, "filters/xbm.html",
  1147.              GTK_WIN_POS_MOUSE,
  1148.              FALSE, TRUE, FALSE,
  1149.  
  1150.              _("OK"), save_ok_callback,
  1151.              NULL, NULL, NULL, TRUE, FALSE,
  1152.              _("Cancel"), gtk_widget_destroy,
  1153.              NULL, 1, NULL, FALSE, TRUE,
  1154.  
  1155.              NULL);
  1156.  
  1157.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  1158.               GTK_SIGNAL_FUNC (gtk_main_quit),
  1159.               NULL);
  1160.  
  1161.   /* parameter settings */
  1162.   frame = gtk_frame_new (_("XBM Options"));
  1163.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  1164.   gtk_container_set_border_width (GTK_CONTAINER (frame), 6);
  1165.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), frame, TRUE, TRUE, 0);
  1166.   gtk_widget_show (frame);
  1167.  
  1168.   vbox = gtk_vbox_new (FALSE, 4);
  1169.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
  1170.   gtk_container_add (GTK_CONTAINER (frame), vbox);
  1171.  
  1172.   /*  X10 format  */
  1173.   toggle = gtk_check_button_new_with_label (_("X10 Format Bitmap"));
  1174.   gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
  1175.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  1176.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1177.               &xsvals.x10_format);
  1178.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), xsvals.x10_format);
  1179.   gtk_widget_show (toggle);
  1180.  
  1181.   table = gtk_table_new (2, 2, FALSE);
  1182.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  1183.   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  1184.   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
  1185.   gtk_widget_show (table);
  1186.  
  1187.   /* prefix */
  1188.   entry = gtk_entry_new_with_max_length (MAX_PREFIX);
  1189.   gtk_entry_set_text (GTK_ENTRY (entry), xsvals.prefix);
  1190.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
  1191.                  _("Identifier Prefix:"), 1.0, 0.5,
  1192.                  entry, 1, TRUE);
  1193.   gtk_signal_connect (GTK_OBJECT (entry), "changed",
  1194.                       GTK_SIGNAL_FUNC (prefix_entry_callback),
  1195.                       NULL);
  1196.  
  1197.   /* comment string. */
  1198.   entry = gtk_entry_new_with_max_length (MAX_COMMENT);
  1199.   gtk_widget_set_usize (entry, 240, 0);
  1200.   gtk_entry_set_text (GTK_ENTRY (entry), xsvals.comment);
  1201.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
  1202.                  _("Comment:"), 1.0, 0.5,
  1203.                  entry, 1, TRUE);
  1204.   gtk_signal_connect (GTK_OBJECT (entry), "changed",
  1205.                       GTK_SIGNAL_FUNC (comment_entry_callback),
  1206.                       NULL);
  1207.  
  1208.   /* hotspot toggle */
  1209.   toggle = gtk_check_button_new_with_label (_("Write Hot Spot Values"));
  1210.   gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
  1211.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  1212.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1213.               &xsvals.use_hot);
  1214.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), xsvals.use_hot);
  1215.   gtk_widget_show (toggle);
  1216.  
  1217.   table = gtk_table_new (2, 2, FALSE);
  1218.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  1219.   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  1220.   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
  1221.   gtk_widget_show (table);
  1222.  
  1223.   gtk_object_set_data (GTK_OBJECT (toggle), "set_sensitive", table);
  1224.   gtk_widget_set_sensitive (table, xsvals.use_hot);
  1225.  
  1226.   spinbutton = gimp_spin_button_new (&adj, xsvals.x_hot, 0,
  1227.                      gimp_drawable_width (drawable_ID) - 1,
  1228.                      1, 1, 1, 0, 0);
  1229.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  1230.               GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
  1231.               &xsvals.x_hot);
  1232.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
  1233.                  _("Hot Spot X:"), 1.0, 0.5,
  1234.                  spinbutton, 1, TRUE);
  1235.  
  1236.   spinbutton = gimp_spin_button_new (&adj, xsvals.y_hot, 0,
  1237.                      gimp_drawable_height (drawable_ID) - 1,
  1238.                      1, 1, 1, 0, 0);
  1239.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  1240.               GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
  1241.               &xsvals.y_hot);
  1242.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
  1243.                  _("Y:"), 1.0, 0.5,
  1244.                  spinbutton, 1, TRUE);
  1245.  
  1246.   /* mask file */
  1247.   frame = gtk_frame_new (_("Mask File"));
  1248.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  1249.   gtk_widget_show (frame);
  1250.  
  1251.   table = gtk_table_new (2, 2, FALSE);
  1252.   gtk_container_set_border_width (GTK_CONTAINER (table), 2);
  1253.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  1254.   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  1255.   gtk_container_add (GTK_CONTAINER (frame), table);
  1256.   gtk_widget_show (table);
  1257.  
  1258.   toggle = gtk_check_button_new_with_label (_("Write Extra Mask File"));
  1259.   gtk_table_attach_defaults (GTK_TABLE (table), toggle, 0, 2, 0, 1);
  1260.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  1261.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1262.               &xsvals.write_mask);
  1263.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), xsvals.write_mask);
  1264.   gtk_widget_show (toggle);
  1265.  
  1266.   entry = gtk_entry_new_with_max_length (MAX_MASK_EXT);
  1267.   gtk_entry_set_text (GTK_ENTRY (entry), xsvals.mask_ext);
  1268.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
  1269.                  _("Mask File Extension:"), 1.0, 0.5,
  1270.                  entry, 1, TRUE);
  1271.   gtk_signal_connect (GTK_OBJECT (entry), "changed",
  1272.                       GTK_SIGNAL_FUNC (mask_ext_entry_callback),
  1273.                       NULL);
  1274.  
  1275.   gtk_object_set_data (GTK_OBJECT (toggle), "set_sensitive", entry);
  1276.   gtk_widget_set_sensitive (entry, xsvals.write_mask);
  1277.  
  1278.   gtk_widget_set_sensitive (frame, gimp_drawable_has_alpha (drawable_ID));
  1279.  
  1280.   /* Done. */
  1281.   gtk_widget_show (vbox);
  1282.   gtk_widget_show (dlg);
  1283.  
  1284.   gtk_main ();
  1285.   gdk_flush ();
  1286.  
  1287.   return xsint.run;
  1288. }
  1289.  
  1290.  
  1291. /* Update the comment string. */
  1292. static void
  1293. comment_entry_callback (GtkWidget *widget,
  1294.             gpointer   data)
  1295. {
  1296.   memset (xsvals.comment, 0, sizeof (xsvals.comment));
  1297.   strncpy (xsvals.comment,
  1298.        gtk_entry_get_text (GTK_ENTRY (widget)), MAX_COMMENT);
  1299. }
  1300.  
  1301. static void
  1302. prefix_entry_callback (GtkWidget *widget,
  1303.                gpointer   data)
  1304. {
  1305.   memset (xsvals.prefix, 0, sizeof (xsvals.prefix));
  1306.   strncpy (xsvals.prefix,
  1307.        gtk_entry_get_text (GTK_ENTRY (widget)), MAX_PREFIX);
  1308. }
  1309.  
  1310. static void
  1311. mask_ext_entry_callback (GtkWidget *widget,
  1312.                gpointer   data)
  1313. {
  1314.   memset (xsvals.prefix, 0, sizeof (xsvals.mask_ext));
  1315.   strncpy (xsvals.mask_ext,
  1316.        gtk_entry_get_text (GTK_ENTRY (widget)), MAX_MASK_EXT);
  1317. }
  1318.  
  1319. static void
  1320. save_ok_callback (GtkWidget *widget,
  1321.           gpointer   data)
  1322. {
  1323.   xsint.run = TRUE;
  1324.  
  1325.   gtk_widget_destroy (GTK_WIDGET (data));
  1326. }
  1327.