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

#include "config.h"

24
#include <gegl.h>
25 26 27 28 29 30 31 32 33 34 35 36 37
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "gimp.h"

#include "gimpuitypes.h"
#include "gimppaletteselectbutton.h"
#include "gimpuimarshal.h"

#include "libgimp-intl.h"


38 39 40 41 42 43 44 45 46
/**
 * SECTION: gimppaletteselectbutton
 * @title: GimpPaletteSelect
 * @short_description: A button which pops up a palette select dialog.
 *
 * A button which pops up a palette select dialog.
 **/


47 48 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 79 80 81 82 83 84 85 86 87
#define GIMP_PALETTE_SELECT_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GIMP_TYPE_PALETTE_SELECT_BUTTON, GimpPaletteSelectButtonPrivate))

typedef struct _GimpPaletteSelectButtonPrivate GimpPaletteSelectButtonPrivate;

struct _GimpPaletteSelectButtonPrivate
{
  gchar     *title;

  gchar     *palette_name;      /* Local copy */

  GtkWidget *inside;
  GtkWidget *label;
};

enum
{
  PALETTE_SET,
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_TITLE,
  PROP_PALETTE_NAME
};


/*  local function prototypes  */

static void   gimp_palette_select_button_finalize     (GObject      *object);

static void   gimp_palette_select_button_set_property (GObject      *object,
                                                       guint         property_id,
                                                       const GValue *value,
                                                       GParamSpec   *pspec);
static void   gimp_palette_select_button_get_property (GObject      *object,
                                                       guint         property_id,
                                                       GValue       *value,
                                                       GParamSpec   *pspec);

88
static void   gimp_palette_select_button_clicked  (GimpPaletteSelectButton *button);
89 90 91 92 93

static void   gimp_palette_select_button_callback (const gchar *palette_name,
                                                   gboolean     dialog_closing,
                                                   gpointer     user_data);

94
static void   gimp_palette_select_drag_data_received (GimpPaletteSelectButton *button,
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
                                                      GdkDragContext          *context,
                                                      gint                     x,
                                                      gint                     y,
                                                      GtkSelectionData        *selection,
                                                      guint                    info,
                                                      guint                    time);

static GtkWidget * gimp_palette_select_button_create_inside (GimpPaletteSelectButton *palette_button);


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

static guint palette_button_signals[LAST_SIGNAL] = { 0 };


G_DEFINE_TYPE (GimpPaletteSelectButton, gimp_palette_select_button,
               GIMP_TYPE_SELECT_BUTTON)


static void
gimp_palette_select_button_class_init (GimpPaletteSelectButtonClass *klass)
{
  GObjectClass          *object_class        = G_OBJECT_CLASS (klass);
  GimpSelectButtonClass *select_button_class = GIMP_SELECT_BUTTON_CLASS (klass);

  object_class->finalize     = gimp_palette_select_button_finalize;
  object_class->set_property = gimp_palette_select_button_set_property;
  object_class->get_property = gimp_palette_select_button_get_property;

  select_button_class->select_destroy = gimp_palette_select_destroy;

  klass->palette_set = NULL;

  /**
   * GimpPaletteSelectButton:title:
   *
   * The title to be used for the palette selection popup dialog.
   *
133
   * Since: 2.4
134 135 136 137 138 139 140 141 142 143 144 145 146 147
   */
  g_object_class_install_property (object_class, PROP_TITLE,
                                   g_param_spec_string ("title",
                                                        "Title",
                                                        "The title to be used for the palette selection popup dialog",
                                                        _("Palette Selection"),
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  /**
   * GimpPaletteSelectButton:palette-name:
   *
   * The name of the currently selected palette.
   *
148
   * Since: 2.4
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
   */
  g_object_class_install_property (object_class, PROP_PALETTE_NAME,
                                   g_param_spec_string ("palette-name",
                                                        "Palette name",
                                                        "The name of the currently selected palette",
                                                        NULL,
                                                        GIMP_PARAM_READWRITE));

  /**
   * GimpPaletteSelectButton::palette-set:
   * @widget: the object which received the signal.
   * @palette_name: the name of the currently selected palette.
   * @dialog_closing: whether the dialog was closed or not.
   *
   * The ::palette-set signal is emitted when the user selects a palette.
   *
165
   * Since: 2.4
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
   */
  palette_button_signals[PALETTE_SET] =
    g_signal_new ("palette-set",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpPaletteSelectButtonClass, palette_set),
                  NULL, NULL,
                  _gimpui_marshal_VOID__STRING_BOOLEAN,
                  G_TYPE_NONE, 2,
                  G_TYPE_STRING,
                  G_TYPE_BOOLEAN);

  g_type_class_add_private (object_class,
                            sizeof (GimpPaletteSelectButtonPrivate));
}

static void
183
gimp_palette_select_button_init (GimpPaletteSelectButton *button)
184 185
{
  GimpPaletteSelectButtonPrivate *priv;
186 187

  priv = GIMP_PALETTE_SELECT_BUTTON_GET_PRIVATE (button);
188 189 190

  priv->palette_name = NULL;

191 192
  priv->inside = gimp_palette_select_button_create_inside (button);
  gtk_container_add (GTK_CONTAINER (button), priv->inside);
193 194 195 196 197 198 199 200 201 202 203 204 205
}

/**
 * gimp_palette_select_button_new:
 * @title:        Title of the dialog to use or %NULL to use the default title.
 * @palette_name: Initial palette name.
 *
 * 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.
 *
206
 * Since: 2.4
207 208 209 210 211
 */
GtkWidget *
gimp_palette_select_button_new (const gchar *title,
                                const gchar *palette_name)
{
212
  GtkWidget *button;
213 214

  if (title)
215
    button = g_object_new (GIMP_TYPE_PALETTE_SELECT_BUTTON,
216 217 218
                           "title",        title,
                           "palette-name", palette_name,
                           NULL);
219
  else
220
    button = g_object_new (GIMP_TYPE_PALETTE_SELECT_BUTTON,
221 222
                           "palette-name", palette_name,
                           NULL);
223

224
  return button;
225 226 227
}

/**
228 229
 * gimp_palette_select_button_get_palette:
 * @button: A #GimpPaletteSelectButton
230 231 232 233 234
 *
 * Retrieves the name of currently selected palette.
 *
 * Returns: an internal copy of the palette name which must not be freed.
 *
235
 * Since: 2.4
236
 */
Sven Neumann's avatar
Sven Neumann committed
237
const gchar *
238
gimp_palette_select_button_get_palette (GimpPaletteSelectButton *button)
239 240 241
{
  GimpPaletteSelectButtonPrivate *priv;

242
  g_return_val_if_fail (GIMP_IS_PALETTE_SELECT_BUTTON (button), NULL);
243

244
  priv = GIMP_PALETTE_SELECT_BUTTON_GET_PRIVATE (button);
245 246 247 248
  return priv->palette_name;
}

/**
249 250
 * gimp_palette_select_button_set_palette:
 * @button: A #GimpPaletteSelectButton
251 252 253 254
 * @palette_name: Palette name to set; %NULL means no change.
 *
 * Sets the current palette for the palette select button.
 *
255
 * Since: 2.4
256 257
 */
void
258 259
gimp_palette_select_button_set_palette (GimpPaletteSelectButton *button,
                                        const gchar             *palette_name)
260 261 262
{
  GimpSelectButton *select_button;

263
  g_return_if_fail (GIMP_IS_PALETTE_SELECT_BUTTON (button));
264

265
  select_button = GIMP_SELECT_BUTTON (button);
266 267

  if (select_button->temp_callback)
268 269 270
    {
      gimp_palettes_set_popup (select_button->temp_callback, palette_name);
    }
271
  else
272 273 274 275 276 277 278 279 280 281 282 283 284 285
    {
      gchar *name;
      gint   num_colors;

      if (palette_name && *palette_name)
        name = g_strdup (palette_name);
      else
        name = gimp_context_get_palette ();

      if (gimp_palette_get_info (name, &num_colors))
        gimp_palette_select_button_callback (name, FALSE, button);

      g_free (name);
    }
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
}


/*  private functions  */

static void
gimp_palette_select_button_finalize (GObject *object)
{
  GimpPaletteSelectButtonPrivate *priv;

  priv = GIMP_PALETTE_SELECT_BUTTON_GET_PRIVATE (object);

  g_free (priv->palette_name);
  priv->palette_name = NULL;

  g_free (priv->title);
  priv->title = NULL;

  G_OBJECT_CLASS (gimp_palette_select_button_parent_class)->finalize (object);
}

static void
gimp_palette_select_button_set_property (GObject      *object,
                                         guint         property_id,
                                         const GValue *value,
                                         GParamSpec   *pspec)
{
313
  GimpPaletteSelectButton        *button;
314 315
  GimpPaletteSelectButtonPrivate *priv;

316 317
  button = GIMP_PALETTE_SELECT_BUTTON (object);
  priv = GIMP_PALETTE_SELECT_BUTTON_GET_PRIVATE (button);
318 319 320 321 322 323 324

  switch (property_id)
    {
    case PROP_TITLE:
      priv->title = g_value_dup_string (value);
      break;
    case PROP_PALETTE_NAME:
325 326
      gimp_palette_select_button_set_palette (button,
                                              g_value_get_string (value));
327 328 329 330 331 332 333 334 335 336 337 338 339
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_palette_select_button_get_property (GObject    *object,
                                         guint       property_id,
                                         GValue     *value,
                                         GParamSpec *pspec)
{
340
  GimpPaletteSelectButton        *button;
341 342
  GimpPaletteSelectButtonPrivate *priv;

343 344
  button = GIMP_PALETTE_SELECT_BUTTON (object);
  priv = GIMP_PALETTE_SELECT_BUTTON_GET_PRIVATE (button);
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

  switch (property_id)
    {
    case PROP_TITLE:
      g_value_set_string (value, priv->title);
      break;
    case PROP_PALETTE_NAME:
      g_value_set_string (value, priv->palette_name);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_palette_select_button_callback (const gchar *palette_name,
                                     gboolean     dialog_closing,
                                     gpointer     user_data)
{
365
  GimpPaletteSelectButton        *button;
366 367 368
  GimpPaletteSelectButtonPrivate *priv;
  GimpSelectButton               *select_button;

369
  button = GIMP_PALETTE_SELECT_BUTTON (user_data);
370

371 372
  priv = GIMP_PALETTE_SELECT_BUTTON_GET_PRIVATE (button);
  select_button = GIMP_SELECT_BUTTON (button);
373 374 375 376 377 378 379 380 381

  g_free (priv->palette_name);
  priv->palette_name = g_strdup (palette_name);

  gtk_label_set_text (GTK_LABEL (priv->label), palette_name);

  if (dialog_closing)
    select_button->temp_callback = NULL;

382
  g_signal_emit (button, palette_button_signals[PALETTE_SET], 0,
383
                 palette_name, dialog_closing);
384
  g_object_notify (G_OBJECT (button), "palette-name");
385 386 387
}

static void
388
gimp_palette_select_button_clicked (GimpPaletteSelectButton *button)
389 390 391
{
  GimpPaletteSelectButtonPrivate *priv;
  GimpSelectButton               *select_button;
392 393 394

  priv = GIMP_PALETTE_SELECT_BUTTON_GET_PRIVATE (button);
  select_button = GIMP_SELECT_BUTTON (button);
395 396 397 398 399 400 401 402 403 404 405 406

  if (select_button->temp_callback)
    {
      /*  calling gimp_palettes_set_popup() raises the dialog  */
      gimp_palettes_set_popup (select_button->temp_callback,
                               priv->palette_name);
    }
  else
    {
      select_button->temp_callback =
        gimp_palette_select_new (priv->title, priv->palette_name,
                                 gimp_palette_select_button_callback,
407
                                 button);
408 409 410 411
    }
}

static void
412
gimp_palette_select_drag_data_received (GimpPaletteSelectButton *button,
413 414 415 416 417 418 419
                                        GdkDragContext          *context,
                                        gint                     x,
                                        gint                     y,
                                        GtkSelectionData        *selection,
                                        guint                    info,
                                        guint                    time)
{
420
  gint   length = gtk_selection_data_get_length (selection);
421 422
  gchar *str;

423
  if (gtk_selection_data_get_format (selection) != 8 || length < 1)
424
    {
425
      g_warning ("%s: received invalid palette data", G_STRFUNC);
426 427 428
      return;
    }

429 430
  str = g_strndup ((const gchar *) gtk_selection_data_get_data (selection),
                   length);
431 432 433 434 435 436 437 438 439 440 441 442

  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;

443
          gimp_palette_select_button_set_palette (button, name);
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
        }
    }

  g_free (str);
}

static GtkWidget *
gimp_palette_select_button_create_inside (GimpPaletteSelectButton *palette_button)
{
  GtkWidget                      *button;
  GtkWidget                      *hbox;
  GtkWidget                      *image;
  GimpPaletteSelectButtonPrivate *priv;

  priv = GIMP_PALETTE_SELECT_BUTTON_GET_PRIVATE (palette_button);

  gtk_widget_push_composite_child ();

  button = gtk_button_new ();

464
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
465 466
  gtk_container_add (GTK_CONTAINER (button), hbox);

467 468
  image = gtk_image_new_from_icon_name (GIMP_ICON_PALETTE,
                                        GTK_ICON_SIZE_BUTTON);
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
  gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);

  priv->label = gtk_label_new (priv->palette_name);
  gtk_box_pack_start (GTK_BOX (hbox), priv->label, TRUE, TRUE, 4);

  gtk_widget_show_all (button);

  g_signal_connect_swapped (button, "clicked",
                            G_CALLBACK (gimp_palette_select_button_clicked),
                            palette_button);

  gtk_drag_dest_set (GTK_WIDGET (button),
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_DROP,
                     &target, 1,
                     GDK_ACTION_COPY);

  g_signal_connect_swapped (button, "drag-data-received",
                            G_CALLBACK (gimp_palette_select_drag_data_received),
                            palette_button);

  gtk_widget_pop_composite_child ();

  return button;
}