gimppatternselect.c 6.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimppatternselect.c
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include "gimp.h"


typedef struct _GimpPatternData  GimpPatternData;

struct _GimpPatternData
{
  gchar                  *pattern_callback;
  guint                   idle_id;
  gchar                  *pattern_name;
  gint                    width;
  gint                    height;
  gint                    bytes;
  guchar                 *pattern_mask_data;
  GimpRunPatternCallback  callback;
  gboolean                closing;
  gpointer                data;
};


/*  local function prototypes  */

46
static void      gimp_temp_pattern_run      (const gchar      *name,
47
                                             gint              nparams,
48
                                             const GimpParam  *param,
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
                                             gint             *nreturn_vals,
                                             GimpParam       **return_vals);
static gboolean  gimp_temp_pattern_run_idle (GimpPatternData  *pattern_data);


/*  private variables  */

static GHashTable *gimp_pattern_select_ht = NULL;


/*  public functions  */

const gchar *
gimp_pattern_select_new (const gchar            *title,
                         const gchar            *pattern_name,
                         GimpRunPatternCallback  callback,
                         gpointer                data)
{
  static GimpParamDef args[] =
  {
    { GIMP_PDB_STRING,   "str",           "String" },
    { GIMP_PDB_INT32,    "mask width",    "Pattern width" },
    { GIMP_PDB_INT32,    "mask height",   "Pattern heigth" },
    { GIMP_PDB_INT32,    "mask bpp",      "Pattern bytes per pixel" },
    { GIMP_PDB_INT32,    "mask len",      "Length of pattern mask data" },
    { GIMP_PDB_INT8ARRAY,"mask data",     "The pattern mask data" },
    { GIMP_PDB_INT32,    "dialog status", "If the dialog was closing "
                                          "[0 = No, 1 = Yes]" },
  };

79
  gchar *pattern_callback = gimp_procedural_db_temp_name ();
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

  gimp_install_temp_proc (pattern_callback,
			  "Temporary pattern popup callback procedure",
			  "",
			  "Andy Thomas",
			  "Andy Thomas",
			  "1997",
			  NULL,
			  "RGB*, GRAY*",
			  GIMP_TEMPORARY,
			  G_N_ELEMENTS (args), 0,
			  args, NULL,
			  gimp_temp_pattern_run);

  if (gimp_patterns_popup (pattern_callback, title, pattern_name))
    {
      GimpPatternData *pattern_data;

      gimp_extension_enable (); /* Allow callbacks to be watched */

      /* Now add to hash table so we can find it again */
      if (! gimp_pattern_select_ht)
        gimp_pattern_select_ht = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                        g_free, g_free);

      pattern_data = g_new0 (GimpPatternData, 1);

      pattern_data->pattern_callback = pattern_callback;
      pattern_data->callback         = callback;
      pattern_data->data             = data;

      g_hash_table_insert (gimp_pattern_select_ht,
                           pattern_callback, pattern_data);

      return pattern_callback;
    }

  gimp_uninstall_temp_proc (pattern_callback);
  g_free (pattern_callback);

  return NULL;
}

void
gimp_pattern_select_destroy (const gchar *pattern_callback)
{
  GimpPatternData *pattern_data;

  g_return_if_fail (pattern_callback != NULL);
  g_return_if_fail (gimp_pattern_select_ht != NULL);

  pattern_data = g_hash_table_lookup (gimp_pattern_select_ht,
                                      pattern_callback);

  if (! pattern_data)
    {
      g_warning ("Can't find internal pattern data");
      return;
    }

  if (pattern_data->idle_id)
141 142 143 144
    g_source_remove (pattern_data->idle_id);

  g_free (pattern_data->pattern_name);
  g_free (pattern_data->pattern_mask_data);
145 146 147 148 149 150 151 152 153 154 155 156 157

  if (pattern_data->pattern_callback)
    gimp_patterns_close_popup (pattern_data->pattern_callback);

  gimp_uninstall_temp_proc (pattern_callback);

  g_hash_table_remove (gimp_pattern_select_ht, pattern_callback);
}


/*  private functions  */

static void
158 159 160 161 162
gimp_temp_pattern_run (const gchar      *name,
                       gint              nparams,
                       const GimpParam  *param,
                       gint             *nreturn_vals,
                       GimpParam       **return_vals)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
{
  static GimpParam  values[1];
  GimpPatternData  *pattern_data;

  pattern_data = g_hash_table_lookup (gimp_pattern_select_ht, name);

  if (! pattern_data)
    {
      g_warning ("Can't find internal pattern data");
    }
  else
    {
      g_free (pattern_data->pattern_name);
      g_free (pattern_data->pattern_mask_data);

      pattern_data->pattern_name      = g_strdup (param[0].data.d_string);
      pattern_data->width             = param[1].data.d_int32;
      pattern_data->height            = param[2].data.d_int32;
      pattern_data->bytes             = param[3].data.d_int32;
      pattern_data->pattern_mask_data = g_memdup (param[5].data.d_int8array,
                                                  param[4].data.d_int32);
      pattern_data->closing           = param[6].data.d_int32;

      if (! pattern_data->idle_id)
        pattern_data->idle_id = g_idle_add ((GSourceFunc) gimp_temp_pattern_run_idle,
                                            pattern_data);
    }

  *nreturn_vals = 1;
  *return_vals  = values;

  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_SUCCESS;
}

static gboolean
gimp_temp_pattern_run_idle (GimpPatternData *pattern_data)
{
  pattern_data->idle_id = 0;

  if (pattern_data->callback)
    pattern_data->callback (pattern_data->pattern_name,
                            pattern_data->width,
                            pattern_data->height,
                            pattern_data->bytes,
                            pattern_data->pattern_mask_data,
                            pattern_data->closing,
                            pattern_data->data);

  if (pattern_data->closing)
    {
      gchar *pattern_callback = pattern_data->pattern_callback;

      pattern_data->pattern_callback = NULL;
      gimp_pattern_select_destroy (pattern_callback);
    }

  return FALSE;
}