gimppaletteselect.c 6.16 KB
Newer Older
1 2 3 4 5
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimppaletteselect.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
{
  gchar                  *palette_callback;
  guint                   idle_id;
  gchar                  *palette_name;
31
  gint                    num_colors;
32 33 34
  GimpRunPaletteCallback  callback;
  gboolean                closing;
  gpointer                data;
35
} GimpPaletteData;
36 37 38 39


/*  local function prototypes  */

40 41
static void      gimp_palette_data_free     (GimpPaletteData  *data);

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
static void      gimp_temp_palette_run      (const gchar      *name,
                                             gint              nparams,
                                             const GimpParam  *param,
                                             gint             *nreturn_vals,
                                             GimpParam       **return_vals);
static gboolean  gimp_temp_palette_run_idle (GimpPaletteData  *palette_data);


/*  private variables  */

static GHashTable *gimp_palette_select_ht = NULL;


/*  public functions  */

const gchar *
gimp_palette_select_new (const gchar            *title,
                         const gchar            *palette_name,
                         GimpRunPaletteCallback  callback,
                         gpointer                data)
{
63
  static const GimpParamDef args[] =
64
  {
65 66
    { GIMP_PDB_STRING, "str",           "String"                      },
    { GIMP_PDB_INT32,  "num colors",    "Number of colors"            },
67
    { GIMP_PDB_INT32,  "dialog status", "If the dialog was closing "
68
                                        "[0 = No, 1 = Yes]"           }
69 70 71 72 73
  };

  gchar *palette_callback = gimp_procedural_db_temp_name ();

  gimp_install_temp_proc (palette_callback,
Sven Neumann's avatar
Sven Neumann committed
74 75
                          "Temporary palette popup callback procedure",
                          "",
76 77 78
                          "",
                          "",
                          "",
Sven Neumann's avatar
Sven Neumann committed
79
                          NULL,
80
                          "",
Sven Neumann's avatar
Sven Neumann committed
81 82 83 84
                          GIMP_TEMPORARY,
                          G_N_ELEMENTS (args), 0,
                          args, NULL,
                          gimp_temp_palette_run);
85 86 87 88 89 90 91 92 93

  if (gimp_palettes_popup (palette_callback, title, palette_name))
    {
      GimpPaletteData *palette_data;

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

      /* Now add to hash table so we can find it again */
      if (! gimp_palette_select_ht)
94 95 96 97 98 99
        {
          gimp_palette_select_ht =
            g_hash_table_new_full (g_str_hash, g_str_equal,
                                   g_free,
                                   (GDestroyNotify) gimp_palette_data_free);
        }
100

101
      palette_data = g_slice_new0 (GimpPaletteData);
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

      palette_data->palette_callback = palette_callback;
      palette_data->callback      = callback;
      palette_data->data          = data;

      g_hash_table_insert (gimp_palette_select_ht,
                           palette_callback, palette_data);

      return palette_callback;
    }

  gimp_uninstall_temp_proc (palette_callback);
  g_free (palette_callback);

  return NULL;
}

void
gimp_palette_select_destroy (const gchar *palette_callback)
{
  GimpPaletteData *palette_data;

  g_return_if_fail (palette_callback != NULL);
  g_return_if_fail (gimp_palette_select_ht != NULL);

  palette_data = g_hash_table_lookup (gimp_palette_select_ht, palette_callback);

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

  if (palette_data->idle_id)
136 137 138
    g_source_remove (palette_data->idle_id);

  g_free (palette_data->palette_name);
139 140 141 142 143 144 145 146 147 148 149 150

  if (palette_data->palette_callback)
    gimp_palettes_close_popup (palette_data->palette_callback);

  gimp_uninstall_temp_proc (palette_callback);

  g_hash_table_remove (gimp_palette_select_ht, palette_callback);
}


/*  private functions  */

151 152 153 154 155 156
static void
gimp_palette_data_free (GimpPaletteData *data)
{
  g_slice_free (GimpPaletteData, data);
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
static void
gimp_temp_palette_run (const gchar      *name,
                       gint              nparams,
                       const GimpParam  *param,
                       gint             *nreturn_vals,
                       GimpParam       **return_vals)
{
  static GimpParam  values[1];
  GimpPaletteData  *palette_data;

  palette_data = g_hash_table_lookup (gimp_palette_select_ht, name);

  if (! palette_data)
    {
      g_warning ("Can't find internal palette data");
    }
  else
    {
      g_free (palette_data->palette_name);

      palette_data->palette_name = g_strdup (param[0].data.d_string);
178 179
      palette_data->num_colors   = param[1].data.d_int32;
      palette_data->closing      = param[2].data.d_int32;
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

      if (! palette_data->idle_id)
        palette_data->idle_id = g_idle_add ((GSourceFunc) gimp_temp_palette_run_idle,
                                            palette_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_palette_run_idle (GimpPaletteData *palette_data)
{
  palette_data->idle_id = 0;

  if (palette_data->callback)
    palette_data->callback (palette_data->palette_name,
                            palette_data->closing,
                            palette_data->data);

  if (palette_data->closing)
    {
      gchar *palette_callback = palette_data->palette_callback;

      palette_data->palette_callback = NULL;
      gimp_palette_select_destroy (palette_callback);
    }

  return FALSE;
}