gimppalettemenu.c 9.16 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimppalettemenu.c
 * Copyright (C) 2004  Michael Natterer <mitch@gimp.org>
 *
 * 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"
#include "gimpui.h"

#include "libgimp-intl.h"


#define PALETTE_SELECT_DATA_KEY  "gimp-palette-selct-data"


typedef struct _PaletteSelect PaletteSelect;

struct _PaletteSelect
{
  gchar                  *title;
  GimpRunPaletteCallback  callback;
  gpointer                data;

  GtkWidget              *button;
  GtkWidget              *label;

  gchar                  *palette_name;      /* Local copy */

  const gchar            *temp_palette_callback;
};


/*  local function prototypes  */

static void   gimp_palette_select_widget_callback (const gchar   *name,
                                                   gboolean       closing,
                                                   gpointer       data);
static void   gimp_palette_select_widget_clicked  (GtkWidget     *widget,
                                                   PaletteSelect *palette_sel);
static void   gimp_palette_select_widget_destroy  (GtkWidget     *widget,
                                                   PaletteSelect *palette_sel);

61 62 63 64 65 66 67 68 69 70 71
static void   gimp_palette_select_drag_data_received (GtkWidget        *widget,
                                                      GdkDragContext   *context,
                                                      gint              x,
                                                      gint              y,
                                                      GtkSelectionData *selection,
                                                      guint             info,
                                                      guint             time);


static const GtkTargetEntry target = { "application/x-gimp-palette-name", 0 };

72 73 74 75 76 77 78 79 80 81 82 83 84 85

/**
 * gimp_palette_select_widget_new:
 * @title:        Title of the dialog to use or %NULL means to use the default
 *                title.
 * @palette_name: Initial palette name.
 * @callback:     A function to call when the selected palette changes.
 * @data:         A pointer to arbitary data to be used in the call to @callback.
 *
 * Creates a new #GtkWidget that completely controls the selection of
 * a palette.  This widget is suitable for placement in a table in a
 * plug-in dialog.
 *
 * Returns: A #GtkWidget that you can use in your UI.
86 87
 *
 * Since: GIMP 2.2
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
 */
GtkWidget *
gimp_palette_select_widget_new (const gchar            *title,
                                const gchar            *palette_name,
                                GimpRunPaletteCallback  callback,
                                gpointer                data)
{
  PaletteSelect *palette_sel;
  GtkWidget     *hbox;
  GtkWidget     *image;

  g_return_val_if_fail (callback != NULL, NULL);

  if (! title)
    title = _("Palette Selection");

  palette_sel = g_new0 (PaletteSelect, 1);

  palette_sel->title    = g_strdup (title);
  palette_sel->callback = callback;
  palette_sel->data     = data;

  palette_sel->palette_name = g_strdup (palette_name);

  palette_sel->button = gtk_button_new ();

  g_signal_connect (palette_sel->button, "clicked",
                    G_CALLBACK (gimp_palette_select_widget_clicked),
                    palette_sel);
  g_signal_connect (palette_sel->button, "destroy",
                    G_CALLBACK (gimp_palette_select_widget_destroy),
                    palette_sel);

121 122 123 124 125 126 127 128 129 130 131
  gtk_drag_dest_set (GTK_WIDGET (palette_sel->button),
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
                     &target, 1,
                     GDK_ACTION_COPY);

  g_signal_connect (palette_sel->button, "drag-data-received",
                    G_CALLBACK (gimp_palette_select_drag_data_received),
                    NULL);

132 133 134 135
  hbox = gtk_hbox_new (FALSE, 4);
  gtk_container_add (GTK_CONTAINER (palette_sel->button), hbox);
  gtk_widget_show (hbox);

136 137 138 139
  image = gtk_image_new_from_stock (GIMP_STOCK_PALETTE, GTK_ICON_SIZE_BUTTON);
  gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
  gtk_widget_show (image);

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  palette_sel->label = gtk_label_new (palette_name);
  gtk_box_pack_start (GTK_BOX (hbox), palette_sel->label, TRUE, TRUE, 4);
  gtk_widget_show (palette_sel->label);

  g_object_set_data (G_OBJECT (palette_sel->button),
                     PALETTE_SELECT_DATA_KEY, palette_sel);

  return palette_sel->button;
}

/**
 * gimp_palette_select_widget_close:
 * @widget: A palette select widget.
 *
 * Closes the popup window associated with @widget.
155 156
 *
 * Since: GIMP 2.2
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
 */
void
gimp_palette_select_widget_close (GtkWidget *widget)
{
  PaletteSelect *palette_sel;

  palette_sel = g_object_get_data (G_OBJECT (widget), PALETTE_SELECT_DATA_KEY);

  g_return_if_fail (palette_sel != NULL);

  if (palette_sel->temp_palette_callback)
    {
      gimp_palette_select_destroy (palette_sel->temp_palette_callback);
      palette_sel->temp_palette_callback = NULL;
    }
}

/**
 * gimp_palette_select_widget_set;
 * @widget:       A palette select widget.
 * @palette_name: Palette name to set; %NULL means no change.
 *
 * Sets the current palette for the palette select widget.  Calls the
 * callback function if one was supplied in the call to
 * gimp_palette_select_widget_new().
182 183
 *
 * Since: GIMP 2.2
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
 */
void
gimp_palette_select_widget_set (GtkWidget   *widget,
                                const gchar *palette_name)
{
  PaletteSelect *palette_sel;

  palette_sel = g_object_get_data (G_OBJECT (widget), PALETTE_SELECT_DATA_KEY);

  g_return_if_fail (palette_sel != NULL);

  if (palette_sel->temp_palette_callback)
    gimp_palettes_set_popup (palette_sel->temp_palette_callback, palette_name);
  else
    gimp_palette_select_widget_callback (palette_name, FALSE, palette_sel);
}


/*  private functions  */

static void
gimp_palette_select_widget_callback (const gchar *name,
                                     gboolean     closing,
                                     gpointer     data)
{
  PaletteSelect *palette_sel = (PaletteSelect *) data;

  g_free (palette_sel->palette_name);
  palette_sel->palette_name = g_strdup (name);

  gtk_label_set_text (GTK_LABEL (palette_sel->label), name);

  if (palette_sel->callback)
    palette_sel->callback (name, closing, palette_sel->data);

  if (closing)
    palette_sel->temp_palette_callback = NULL;
}

static void
gimp_palette_select_widget_clicked (GtkWidget     *widget,
                                    PaletteSelect *palette_sel)
{
  if (palette_sel->temp_palette_callback)
    {
      /*  calling gimp_palettes_set_popup() raises the dialog  */
      gimp_palettes_set_popup (palette_sel->temp_palette_callback,
                               palette_sel->palette_name);
    }
  else
    {
      palette_sel->temp_palette_callback =
        gimp_palette_select_new (palette_sel->title,
                                 palette_sel->palette_name,
                                 gimp_palette_select_widget_callback,
                                 palette_sel);
    }
}

static void
gimp_palette_select_widget_destroy (GtkWidget     *widget,
                                    PaletteSelect *palette_sel)
{
  if (palette_sel->temp_palette_callback)
    {
      gimp_palette_select_destroy (palette_sel->temp_palette_callback);
      palette_sel->temp_palette_callback = NULL;
    }

  g_free (palette_sel->title);
  g_free (palette_sel->palette_name);
  g_free (palette_sel);
}
257 258 259 260 261 262 263 264 265 266

static void
gimp_palette_select_drag_data_received (GtkWidget        *widget,
                                        GdkDragContext   *context,
                                        gint              x,
                                        gint              y,
                                        GtkSelectionData *selection,
                                        guint             info,
                                        guint             time)
{
267
  gchar *str;
268

269 270 271 272 273 274
  if ((selection->format != 8) || (selection->length < 1))
    {
      g_warning ("Received invalid palette data!");
      return;
    }

275
  str = g_strndup ((const gchar *) selection->data, selection->length);
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290
  if (g_utf8_validate (str, -1, NULL))
    {
      gint     pid;
      gpointer unused;
      gint     name_offset = 0;

      if (sscanf (str, "%i:%p:%n", &pid, &unused, &name_offset) >= 2 &&
          pid == gimp_getpid () && name_offset > 0)
        {
          gchar *name = str + name_offset;

          gimp_palette_select_widget_set (widget, name);
        }
    }
291

292
  g_free (str);
293
}