gimpbrushselect.c 7.48 KB
Newer Older
1 2 3 4 5
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpbrushselect.c
 *
6
 * This library is free software: you can redistribute it and/or
7 8
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
9
 * version 3 of the License, or (at your option) any later version.
10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
18
 * <https://www.gnu.org/licenses/>.
19 20 21 22 23 24 25
 */

#include "config.h"

#include "gimp.h"


26
typedef struct
27 28 29 30 31 32 33 34 35 36 37 38 39
{
  gchar                *brush_callback;
  guint                 idle_id;
  gchar                *brush_name;
  gdouble               opacity;
  gint                  spacing;
  gint                  paint_mode;
  gint                  width;
  gint                  height;
  guchar               *brush_mask_data;
  GimpRunBrushCallback  callback;
  gboolean              closing;
  gpointer              data;
40
} GimpBrushData;
41 42 43 44


/*  local function prototypes  */

45 46
static void      gimp_brush_data_free     (GimpBrushData    *data);

47 48 49 50 51 52
static void      gimp_temp_brush_run      (const gchar      *name,
                                           gint              nparams,
                                           const GimpParam  *param,
                                           gint             *nreturn_vals,
                                           GimpParam       **return_vals);
static gboolean  gimp_temp_brush_run_idle (GimpBrushData    *brush_data);
53 54 55 56 57 58 59 60 61 62


/*  private variables  */

static GHashTable *gimp_brush_select_ht = NULL;


/*  public functions  */

const gchar *
63
gimp_brush_select_new (const gchar          *title,
64 65 66
                       const gchar          *brush_name,
                       gdouble               opacity,
                       gint                  spacing,
67
                       GimpLayerMode         paint_mode,
68 69 70
                       GimpRunBrushCallback  callback,
                       gpointer              data)
{
71
  static const GimpParamDef args[] =
72
  {
73 74 75 76 77
    { GIMP_PDB_STRING,    "str",           "String"                     },
    { GIMP_PDB_FLOAT,     "opacity",       "Opacity"                    },
    { GIMP_PDB_INT32,     "spacing",       "Spacing"                    },
    { GIMP_PDB_INT32,     "paint mode",    "Paint mode"                 },
    { GIMP_PDB_INT32,     "mask width",    "Brush width"                },
78
    { GIMP_PDB_INT32,     "mask height"    "Brush height"               },
79 80
    { GIMP_PDB_INT32,     "mask len",      "Length of brush mask data"  },
    { GIMP_PDB_INT8ARRAY, "mask data",     "The brush mask data"        },
81
    { GIMP_PDB_INT32,     "dialog status", "If the dialog was closing "
82
                                           "[0 = No, 1 = Yes]"          }
83 84
  };

85
  gchar *brush_callback = gimp_procedural_db_temp_name ();
86 87

  gimp_install_temp_proc (brush_callback,
Sven Neumann's avatar
Sven Neumann committed
88 89
                          "Temporary brush popup callback procedure",
                          "",
90 91 92
                          "",
                          "",
                          "",
Sven Neumann's avatar
Sven Neumann committed
93
                          NULL,
94
                          "",
Sven Neumann's avatar
Sven Neumann committed
95 96 97 98
                          GIMP_TEMPORARY,
                          G_N_ELEMENTS (args), 0,
                          args, NULL,
                          gimp_temp_brush_run);
99 100 101 102 103 104 105 106 107 108

  if (gimp_brushes_popup (brush_callback, title, brush_name,
                          opacity, spacing, paint_mode))
    {
      GimpBrushData *brush_data;

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

      /* Now add to hash table so we can find it again */
      if (! gimp_brush_select_ht)
109 110 111 112 113 114
        {
          gimp_brush_select_ht =
            g_hash_table_new_full (g_str_hash, g_str_equal,
                                   g_free,
                                   (GDestroyNotify) gimp_brush_data_free);
        }
115

116
      brush_data = g_slice_new0 (GimpBrushData);
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

      brush_data->brush_callback = brush_callback;
      brush_data->callback       = callback;
      brush_data->data           = data;

      g_hash_table_insert (gimp_brush_select_ht, brush_callback, brush_data);

      return brush_callback;
    }

  gimp_uninstall_temp_proc (brush_callback);
  g_free (brush_callback);

  return NULL;
}

void
gimp_brush_select_destroy (const gchar *brush_callback)
{
  GimpBrushData *brush_data;

  g_return_if_fail (brush_callback != NULL);
  g_return_if_fail (gimp_brush_select_ht != NULL);

  brush_data = g_hash_table_lookup (gimp_brush_select_ht, brush_callback);

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

  if (brush_data->idle_id)
150 151 152 153
    g_source_remove (brush_data->idle_id);

  g_free (brush_data->brush_name);
  g_free (brush_data->brush_mask_data);
154 155 156 157 158 159 160 161 162 163 164 165

  if (brush_data->brush_callback)
    gimp_brushes_close_popup (brush_data->brush_callback);

  gimp_uninstall_temp_proc (brush_callback);

  g_hash_table_remove (gimp_brush_select_ht, brush_callback);
}


/*  private functions  */

166 167 168 169 170 171
static void
gimp_brush_data_free (GimpBrushData *data)
{
  g_slice_free (GimpBrushData, data);
}

172
static void
173 174 175 176 177
gimp_temp_brush_run (const gchar      *name,
                     gint              nparams,
                     const GimpParam  *param,
                     gint             *nreturn_vals,
                     GimpParam       **return_vals)
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
{
  static GimpParam  values[1];
  GimpBrushData    *brush_data;

  brush_data = g_hash_table_lookup (gimp_brush_select_ht, name);

  if (! brush_data)
    {
      g_warning ("Can't find internal brush data");
    }
  else
    {
      g_free (brush_data->brush_name);
      g_free (brush_data->brush_mask_data);

      brush_data->brush_name      = g_strdup (param[0].data.d_string);
      brush_data->opacity         = param[1].data.d_float;
      brush_data->spacing         = param[2].data.d_int32;
      brush_data->paint_mode      = param[3].data.d_int32;
      brush_data->width           = param[4].data.d_int32;
      brush_data->height          = param[5].data.d_int32;
      brush_data->brush_mask_data = g_memdup (param[7].data.d_int8array,
                                              param[6].data.d_int32);
      brush_data->closing         = param[8].data.d_int32;

      if (! brush_data->idle_id)
        brush_data->idle_id = g_idle_add ((GSourceFunc) gimp_temp_brush_run_idle,
                                          brush_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_brush_run_idle (GimpBrushData *brush_data)
{
  brush_data->idle_id = 0;

  if (brush_data->callback)
    brush_data->callback (brush_data->brush_name,
                          brush_data->opacity,
                          brush_data->spacing,
                          brush_data->paint_mode,
                          brush_data->width,
                          brush_data->height,
                          brush_data->brush_mask_data,
                          brush_data->closing,
                          brush_data->data);

  if (brush_data->closing)
    {
      gchar *brush_callback = brush_data->brush_callback;

      brush_data->brush_callback = NULL;
      gimp_brush_select_destroy (brush_callback);
    }

  return FALSE;
}