gimpcolorbutton.c 33.6 KB
Newer Older
1
/* LIBGIMP - The GIMP Library
2
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3
 *
4
 * gimpcolorbutton.c
5
 * Copyright (C) 1999-2001 Sven Neumann
6
 *
7
 * This library is free software: you can redistribute it and/or
Marc Lehmann's avatar
Marc Lehmann committed
8
 * modify it under the terms of the GNU Lesser General Public
9
 * 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
 *
 * 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
15 16
 * Library General Public License for more details.
 *
Marc Lehmann's avatar
Marc Lehmann committed
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
19
 * <https://www.gnu.org/licenses/>.
20 21
 */

22 23
#include "config.h"

24 25
#include <string.h>

26
#include <gegl.h>
27 28
#include <gtk/gtk.h>

29
#include "libgimpcolor/gimpcolor.h"
30
#include "libgimpconfig/gimpconfig.h"
31

32
#include "gimpwidgetstypes.h"
33

34
#include "gimpcolorarea.h"
35
#include "gimpcolorbutton.h"
36 37 38 39
#include "gimpcolornotebook.h"
#include "gimpcolorselection.h"
#include "gimpdialog.h"
#include "gimphelpui.h"
40
#include "gimpicons.h"
41
#include "gimpwidgets-private.h"
42 43

#include "libgimp/libgimp-intl.h"
44

45

46 47 48 49 50 51 52 53 54 55 56
/**
 * SECTION: gimpcolorbutton
 * @title: GimpColorButton
 * @short_description: Widget for selecting a color from a simple button.
 * @see_also: #libgimpcolor-gimpcolorspace
 *
 * This widget provides a simple button with a preview showing the
 * color.
 *
 * On click a color selection dialog is opened. Additionally the
 * button supports Drag and Drop and has a right-click menu that
57 58
 * allows one to choose the color from the current FG or BG color. If
 * the user changes the color, the "color-changed" signal is emitted.
59 60 61
 **/


62 63
#define COLOR_BUTTON_KEY "gimp-color-button"
#define RESPONSE_RESET   1
64

65 66 67 68
#define TODOUBLE(i) (i / 65535.0)
#define TOUINT16(d) ((guint16) (d * 65535 + 0.5))


69 70 71 72 73
#define GIMP_COLOR_BUTTON_COLOR_FG    "color-button-use-foreground"
#define GIMP_COLOR_BUTTON_COLOR_BG    "color-button-use-background"
#define GIMP_COLOR_BUTTON_COLOR_BLACK "color-button-use-black"
#define GIMP_COLOR_BUTTON_COLOR_WHITE "color-button-use-white"

74

75 76 77 78 79 80
enum
{
  COLOR_CHANGED,
  LAST_SIGNAL
};

81 82 83
enum
{
  PROP_0,
84 85 86
  PROP_TITLE,
  PROP_COLOR,
  PROP_TYPE,
87 88
  PROP_UPDATE,
  PROP_AREA_WIDTH,
89 90
  PROP_AREA_HEIGHT,
  PROP_COLOR_CONFIG
91 92
};

93

94 95
struct _GimpColorButtonPrivate
{
96 97 98 99 100
  gchar           *title;
  gboolean         continuous_update;

  GtkWidget       *color_area;
  GtkWidget       *dialog;
101 102
  GtkWidget       *selection;

103 104
  GtkUIManager    *ui_manager;

105 106 107
  GimpColorConfig *config;
};

108
#define GET_PRIVATE(obj) (((GimpColorButton *) (obj))->priv)
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 141 142 143
static void     gimp_color_button_class_init     (GimpColorButtonClass *klass);
static void     gimp_color_button_init           (GimpColorButton      *button,
                                                  GimpColorButtonClass *klass);

static void     gimp_color_button_finalize            (GObject         *object);
static void     gimp_color_button_dispose             (GObject         *object);
static void     gimp_color_button_get_property        (GObject         *object,
                                                       guint            property_id,
                                                       GValue          *value,
                                                       GParamSpec      *pspec);
static void     gimp_color_button_set_property        (GObject         *object,
                                                       guint            property_id,
                                                       const GValue    *value,
                                                       GParamSpec      *pspec);

static gboolean gimp_color_button_button_press        (GtkWidget       *widget,
                                                       GdkEventButton  *bevent);
static void     gimp_color_button_state_flags_changed (GtkWidget       *widget,
                                                       GtkStateFlags    prev_state);
static void     gimp_color_button_clicked             (GtkButton       *button);
static GType    gimp_color_button_get_action_type     (GimpColorButton *button);

static void     gimp_color_button_dialog_response     (GtkWidget       *dialog,
                                                       gint             response_id,
                                                       GimpColorButton *button);
static void     gimp_color_button_use_color           (GtkAction       *action,
                                                       GimpColorButton *button);
static void     gimp_color_button_area_changed        (GtkWidget       *color_area,
                                                       GimpColorButton *button);
static void     gimp_color_button_selection_changed   (GtkWidget       *selection,
                                                       GimpColorButton *button);
static void     gimp_color_button_help_func           (const gchar     *help_id,
                                                       gpointer         help_data);
144 145


146
static const GtkActionEntry actions[] =
147
{
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  { "color-button-popup", NULL,
    "Color Button Menu", NULL, NULL,
    NULL
  },

  { GIMP_COLOR_BUTTON_COLOR_FG, NULL,
    N_("_Foreground Color"), NULL, NULL,
    G_CALLBACK (gimp_color_button_use_color)
  },
  { GIMP_COLOR_BUTTON_COLOR_BG, NULL,
    N_("_Background Color"), NULL, NULL,
    G_CALLBACK (gimp_color_button_use_color)
  },
  { GIMP_COLOR_BUTTON_COLOR_BLACK, NULL,
    N_("Blac_k"), NULL, NULL,
    G_CALLBACK (gimp_color_button_use_color)
  },
  { GIMP_COLOR_BUTTON_COLOR_WHITE, NULL,
    N_("_White"), NULL, NULL,
    G_CALLBACK (gimp_color_button_use_color)
  }
169
};
170

171
static guint   gimp_color_button_signals[LAST_SIGNAL] = { 0 };
172

173
static GimpButtonClass * parent_class = NULL;
174

175

176 177
GType
gimp_color_button_get_type (void)
178
{
179
  static GType button_type = 0;
180

181
  if (! button_type)
182
    {
183
      const GTypeInfo button_info =
184 185 186 187 188 189 190 191 192 193 194
      {
        sizeof (GimpColorButtonClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_color_button_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpColorButton),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_color_button_init,
      };
195

196
      button_type = g_type_register_static (GIMP_TYPE_BUTTON,
197 198
                                            "GimpColorButton",
                                            &button_info, 0);
199
    }
200

201
  return button_type;
202 203 204
}

static void
205
gimp_color_button_class_init (GimpColorButtonClass *klass)
206
{
207 208 209
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
210
  GimpRGB         color;
211

212
  parent_class = g_type_class_peek_parent (klass);
213

214
  gimp_color_button_signals[COLOR_CHANGED] =
215
    g_signal_new ("color-changed",
216 217 218 219 220 221
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpColorButtonClass, color_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
222

223 224 225 226
  object_class->finalize            = gimp_color_button_finalize;
  object_class->dispose             = gimp_color_button_dispose;
  object_class->get_property        = gimp_color_button_get_property;
  object_class->set_property        = gimp_color_button_set_property;
227

228 229
  widget_class->button_press_event  = gimp_color_button_button_press;
  widget_class->state_flags_changed = gimp_color_button_state_flags_changed;
230

231
  button_class->clicked             = gimp_color_button_clicked;
232

233 234
  klass->color_changed              = NULL;
  klass->get_action_type            = gimp_color_button_get_action_type;
235

236 237
  gimp_rgba_set (&color, 0.0, 0.0, 0.0, 1.0);

238 239 240 241 242
  /**
   * GimpColorButton:title:
   *
   * The title to be used for the color selection dialog.
   *
243
   * Since: 2.4
244 245
   */
  g_object_class_install_property (object_class, PROP_TITLE,
246 247 248
                                   g_param_spec_string ("title",
                                                        "Title",
                                                        "The title to be used for the color selection dialog",
249
                                                        NULL,
250
                                                        GIMP_PARAM_READWRITE |
251
                                                        G_PARAM_CONSTRUCT));
252 253 254 255 256
  /**
   * GimpColorButton:color:
   *
   * The color displayed in the button's color area.
   *
257
   * Since: 2.4
258 259
   */
  g_object_class_install_property (object_class, PROP_COLOR,
260 261 262
                                   gimp_param_spec_rgb ("color",
                                                        "Color",
                                                        "The color displayed in the button's color area",
263
                                                        TRUE, &color,
264
                                                        GIMP_PARAM_READWRITE |
265 266 267 268 269 270
                                                        G_PARAM_CONSTRUCT));
  /**
   * GimpColorButton:type:
   *
   * The type of the button's color area.
   *
271
   * Since: 2.4
272 273
   */
  g_object_class_install_property (object_class, PROP_TYPE,
274 275 276
                                   g_param_spec_enum ("type",
                                                      "Type",
                                                      "The type of the button's color area",
277 278
                                                      GIMP_TYPE_COLOR_AREA_TYPE,
                                                      GIMP_COLOR_AREA_FLAT,
279
                                                      GIMP_PARAM_READWRITE |
280 281 282 283 284 285
                                                      G_PARAM_CONSTRUCT));
  /**
   * GimpColorButton:continuous-update:
   *
   * The update policy of the color button.
   *
286
   * Since: 2.4
287 288 289
   */
  g_object_class_install_property (object_class, PROP_UPDATE,
                                   g_param_spec_boolean ("continuous-update",
290 291
                                                         "Contiguous Update",
                                                         "The update policy of the color button",
292 293 294
                                                         FALSE,
                                                         G_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT));
295 296 297 298 299
  /**
   * GimpColorButton:area-width:
   *
   * The minimum width of the button's #GimpColorArea.
   *
300
   * Since: 2.8
301 302 303
   */
  g_object_class_install_property (object_class, PROP_AREA_WIDTH,
                                   g_param_spec_int ("area-width",
304 305
                                                     "Area Width",
                                                     "The minimum width of the button's GimpColorArea",
306 307 308 309 310 311 312 313
                                                     1, G_MAXINT, 16,
                                                     G_PARAM_WRITABLE |
                                                     G_PARAM_CONSTRUCT));
  /**
   * GimpColorButton:area-height:
   *
   * The minimum height of the button's #GimpColorArea.
   *
314
   * Since: 2.8
315 316 317
   */
  g_object_class_install_property (object_class, PROP_AREA_HEIGHT,
                                   g_param_spec_int ("area-height",
318 319
                                                     "Area Height",
                                                     "The minimum height of the button's GimpColorArea",
320 321 322
                                                     1, G_MAXINT, 16,
                                                     G_PARAM_WRITABLE |
                                                     G_PARAM_CONSTRUCT));
323 324 325 326 327 328 329 330 331 332
  /**
   * GimpColorButton:color-config:
   *
   * The #GimpColorConfig object used for the button's #GimpColorArea
   * and #GimpColorSelection.
   *
   * Since: 2.10
   */
  g_object_class_install_property (object_class, PROP_COLOR_CONFIG,
                                   g_param_spec_object ("color-config",
333 334
                                                        "Color Config",
                                                        "The color config object used",
335 336 337 338
                                                        GIMP_TYPE_COLOR_CONFIG,
                                                        G_PARAM_READWRITE));

  g_type_class_add_private (object_class, sizeof (GimpColorButtonPrivate));
339 340 341
}

static void
342 343
gimp_color_button_init (GimpColorButton      *button,
                        GimpColorButtonClass *klass)
344
{
345
  GimpColorButtonPrivate *priv;
346 347
  GtkActionGroup         *group;
  gint                    i;
348

349 350 351 352 353 354
  button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
                                              GIMP_TYPE_COLOR_BUTTON,
                                              GimpColorButtonPrivate);

  priv = GET_PRIVATE (button);

355 356 357
  priv->color_area = g_object_new (GIMP_TYPE_COLOR_AREA,
                                   "drag-mask", GDK_BUTTON1_MASK,
                                   NULL);
358

359
  g_signal_connect (priv->color_area, "color-changed",
360 361
                    G_CALLBACK (gimp_color_button_area_changed),
                    button);
362

363 364
  gtk_container_add (GTK_CONTAINER (button), priv->color_area);
  gtk_widget_show (priv->color_area);
365

366
  /* right-click opens a popup */
367
  priv->ui_manager = gtk_ui_manager_new ();
368 369 370 371 372

  group = gtk_action_group_new ("color-button");

  for (i = 0; i < G_N_ELEMENTS (actions); i++)
    {
373 374
      const gchar *label   = gettext (actions[i].label);
      const gchar *tooltip = gettext (actions[i].tooltip);
375 376 377
      GtkAction   *action;

      action = g_object_new (klass->get_action_type (button),
378 379 380 381
                             "name",      actions[i].name,
                             "label",     label,
                             "tooltip",   tooltip,
                             "icon-name", actions[i].stock_id,
382 383 384 385 386 387 388 389 390 391 392 393 394
                             NULL);

      if (actions[i].callback)
        g_signal_connect (action, "activate",
                          actions[i].callback,
                          button);

      gtk_action_group_add_action_with_accel (group, action,
                                              actions[i].accelerator);

      g_object_unref (action);
    }

395
  gtk_ui_manager_insert_action_group (priv->ui_manager, group, -1);
396 397 398
  g_object_unref (group);

  gtk_ui_manager_add_ui_from_string
399
    (priv->ui_manager,
400 401 402 403 404 405 406 407 408 409
     "<ui>\n"
     "  <popup action=\"color-button-popup\">\n"
     "    <menuitem action=\"" GIMP_COLOR_BUTTON_COLOR_FG "\" />\n"
     "    <menuitem action=\"" GIMP_COLOR_BUTTON_COLOR_BG "\" />\n"
     "    <separator />\n"
     "    <menuitem action=\"" GIMP_COLOR_BUTTON_COLOR_BLACK "\" />\n"
     "    <menuitem action=\"" GIMP_COLOR_BUTTON_COLOR_WHITE "\" />\n"
     "  </popup>\n"
     "</ui>\n",
     -1, NULL);
410 411
}

412
static void
413
gimp_color_button_finalize (GObject *object)
414
{
415
  GimpColorButtonPrivate *priv = GET_PRIVATE (object);
416

417
  g_clear_pointer (&priv->title, g_free);
418

419 420 421
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

422 423 424
static void
gimp_color_button_dispose (GObject *object)
{
425 426
  GimpColorButton        *button = GIMP_COLOR_BUTTON (object);
  GimpColorButtonPrivate *priv   = GET_PRIVATE (button);
427

428
  g_clear_pointer (&priv->dialog, gtk_widget_destroy);
429
  priv->selection = NULL;
430

431
  g_clear_pointer (&priv->color_area, gtk_widget_destroy);
432

433
  g_clear_object (&priv->ui_manager);
434

435 436
  gimp_color_button_set_color_config (button, NULL);

437 438 439
  G_OBJECT_CLASS (parent_class)->dispose (object);
}

440 441 442 443 444 445
static void
gimp_color_button_get_property (GObject    *object,
                                guint       property_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
446
  GimpColorButtonPrivate *priv = GET_PRIVATE (object);
447 448 449 450

  switch (property_id)
    {
    case PROP_TITLE:
451
      g_value_set_string (value, priv->title);
452 453
      break;

454
    case PROP_COLOR:
455
      g_object_get_property (G_OBJECT (priv->color_area), "color", value);
456 457 458
      break;

    case PROP_TYPE:
459
      g_object_get_property (G_OBJECT (priv->color_area), "type", value);
460 461 462
      break;

    case PROP_UPDATE:
463
      g_value_set_boolean (value, priv->continuous_update);
464 465
      break;

466 467 468 469
    case PROP_COLOR_CONFIG:
      g_value_set_object (value, priv->config);
      break;

470 471 472 473 474 475 476 477 478 479 480 481
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_color_button_set_property (GObject      *object,
                                guint         property_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
482 483 484
  GimpColorButton        *button = GIMP_COLOR_BUTTON (object);
  GimpColorButtonPrivate *priv   = GET_PRIVATE (object);
  gint                    other;
485 486 487 488

  switch (property_id)
    {
    case PROP_TITLE:
489
      gimp_color_button_set_title (button, g_value_get_string (value));
490 491
      break;

492
    case PROP_COLOR:
493
      g_object_set_property (G_OBJECT (priv->color_area), "color", value);
494 495 496
      break;

    case PROP_TYPE:
497
      g_object_set_property (G_OBJECT (priv->color_area), "type", value);
498 499 500 501 502 503
      break;

    case PROP_UPDATE:
      gimp_color_button_set_update (button, g_value_get_boolean (value));
      break;

504
    case PROP_AREA_WIDTH:
505 506
      gtk_widget_get_size_request (priv->color_area, NULL, &other);
      gtk_widget_set_size_request (priv->color_area,
507 508 509 510
                                   g_value_get_int (value), other);
      break;

    case PROP_AREA_HEIGHT:
511 512
      gtk_widget_get_size_request (priv->color_area, &other, NULL);
      gtk_widget_set_size_request (priv->color_area,
513 514 515
                                   other, g_value_get_int (value));
      break;

516 517 518 519
    case PROP_COLOR_CONFIG:
      gimp_color_button_set_color_config (button, g_value_get_object (value));
      break;

520 521 522 523 524 525
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

526 527 528 529
static gboolean
gimp_color_button_button_press (GtkWidget      *widget,
                                GdkEventButton *bevent)
{
530
  GimpColorButtonPrivate *priv = GET_PRIVATE (widget);
531

532
  if (gdk_event_triggers_context_menu ((GdkEvent *) bevent))
533
    {
534
      GtkWidget *menu = gtk_ui_manager_get_widget (priv->ui_manager,
535
                                                   "/color-button-popup");
536 537

      gtk_menu_set_screen (GTK_MENU (menu), gtk_widget_get_screen (widget));
538

539
      gtk_menu_popup_at_pointer (GTK_MENU (menu), (GdkEvent *) bevent);
540 541
    }

542
  return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, bevent);
543 544
}

545
static void
546 547
gimp_color_button_state_flags_changed (GtkWidget     *widget,
                                       GtkStateFlags  previous_state)
548
{
549
  GimpColorButtonPrivate *priv = GET_PRIVATE (widget);
550

551 552
  if (! gtk_widget_is_sensitive (widget) && priv->dialog)
    gtk_widget_hide (priv->dialog);
553

554 555 556
  if (GTK_WIDGET_CLASS (parent_class)->state_flags_changed)
    GTK_WIDGET_CLASS (parent_class)->state_flags_changed (widget,
                                                          previous_state);
557
}
558

559 560 561
static void
gimp_color_button_clicked (GtkButton *button)
{
562 563 564
  GimpColorButton        *color_button = GIMP_COLOR_BUTTON (button);
  GimpColorButtonPrivate *priv         = GET_PRIVATE (button);
  GimpRGB                 color;
565

566
  if (! priv->dialog)
567 568 569
    {
      GtkWidget *dialog;

570 571
      dialog = priv->dialog =
        gimp_dialog_new (priv->title, "gimp-color-button",
572
                         gtk_widget_get_toplevel (GTK_WIDGET (button)), 0,
573 574
                         gimp_color_button_help_func, NULL,

575 576 577
                         _("_Reset"),  RESPONSE_RESET,
                         _("_Cancel"), GTK_RESPONSE_CANCEL,
                         _("_OK"),     GTK_RESPONSE_OK,
578 579 580

                         NULL);

581 582
      g_object_set_data (G_OBJECT (dialog), COLOR_BUTTON_KEY, button);

583
      gimp_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
584 585 586 587
                                               RESPONSE_RESET,
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);
588

589 590 591 592 593
      g_signal_connect (dialog, "response",
                        G_CALLBACK (gimp_color_button_dialog_response),
                        color_button);
      g_signal_connect (dialog, "destroy",
                        G_CALLBACK (gtk_widget_destroyed),
594
                        &priv->dialog);
595

596 597 598
      priv->selection = gimp_color_selection_new ();
      gtk_container_set_border_width (GTK_CONTAINER (priv->selection), 6);
      gimp_color_selection_set_show_alpha (GIMP_COLOR_SELECTION (priv->selection),
599
                                           gimp_color_button_has_alpha (color_button));
600 601
      gimp_color_selection_set_config (GIMP_COLOR_SELECTION (priv->selection),
                                       priv->config);
602
      gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
603 604
                          priv->selection, TRUE, TRUE, 0);
      gtk_widget_show (priv->selection);
605

606
      g_signal_connect (priv->selection, "color-changed",
607 608
                        G_CALLBACK (gimp_color_button_selection_changed),
                        button);
609 610 611 612
    }

  gimp_color_button_get_color (color_button, &color);

613
  g_signal_handlers_block_by_func (priv->selection,
614 615 616
                                   gimp_color_button_selection_changed,
                                   button);

617 618
  gimp_color_selection_set_color (GIMP_COLOR_SELECTION (priv->selection), &color);
  gimp_color_selection_set_old_color (GIMP_COLOR_SELECTION (priv->selection),
619 620
                                      &color);

621
  g_signal_handlers_unblock_by_func (priv->selection,
622 623
                                     gimp_color_button_selection_changed,
                                     button);
624

625
  gtk_window_present (GTK_WINDOW (priv->dialog));
626 627 628 629 630 631 632 633 634 635 636
}

static GType
gimp_color_button_get_action_type (GimpColorButton *button)
{
  return GTK_TYPE_ACTION;
}


/*  public functions  */

637 638
/**
 * gimp_color_button_new:
639 640
 * @title:  String that will be used as title for the color_selector.
 * @width:  Width of the colorpreview in pixels.
641
 * @height: Height of the colorpreview in pixels.
642 643
 * @color:  A pointer to a #GimpRGB color.
 * @type:   The type of transparency to be displayed.
644
 *
645 646 647 648 649
 * Creates a new #GimpColorButton widget.
 *
 * This returns a button with a preview showing the color.
 * When the button is clicked a GtkColorSelectionDialog is opened.
 * If the user changes the color the new color is written into the
650
 * array that was used to pass the initial color and the "color-changed"
651
 * signal is emitted.
652
 *
653 654 655
 * Returns: Pointer to the new #GimpColorButton widget.
 **/
GtkWidget *
656
gimp_color_button_new (const gchar       *title,
657 658 659 660
                       gint               width,
                       gint               height,
                       const GimpRGB     *color,
                       GimpColorAreaType  type)
661
{
662
  g_return_val_if_fail (color != NULL, NULL);
663 664 665 666 667 668 669 670 671 672
  g_return_val_if_fail (width > 0, NULL);
  g_return_val_if_fail (height > 0, NULL);

  return g_object_new (GIMP_TYPE_COLOR_BUTTON,
                       "title",       title,
                       "type",        type,
                       "color",       color,
                       "area-width",  width,
                       "area-height", height,
                       NULL);
673 674
}

675 676 677
/**
 * gimp_color_button_set_title:
 * @button: a #GimpColorButton.
678
 * @title:  the new title.
679 680 681
 *
 * Sets the @button dialog's title.
 *
682
 * Since: 2.10
683 684 685 686 687
 **/
void
gimp_color_button_set_title (GimpColorButton *button,
                             const gchar     *title)
{
688 689
  GimpColorButtonPrivate *priv;

690 691 692
  g_return_if_fail (GIMP_IS_COLOR_BUTTON (button));
  g_return_if_fail (title != NULL);

693 694 695 696
  priv = GET_PRIVATE (button);

  g_free (priv->title);
  priv->title = g_strdup (title);
697

698 699
  if (priv->dialog)
    gtk_window_set_title (GTK_WINDOW (priv->dialog), title);
700 701 702 703 704 705 706 707 708 709

  g_object_notify (G_OBJECT (button), "title");
}

/**
 * gimp_color_button_get_title:
 * @button: a #GimpColorButton.
 *
 * Returns: The @button dialog's title.
 *
710
 * Since: 2.10
711 712 713 714
 **/
const gchar *
gimp_color_button_get_title (GimpColorButton *button)
{
715 716
  GimpColorButtonPrivate *priv;

717 718
  g_return_val_if_fail (GIMP_IS_COLOR_BUTTON (button), NULL);

719 720 721
  priv = GET_PRIVATE (button);

  return priv->title;
722 723
}

724
/**
725
 * gimp_color_button_set_color:
726
 * @button: Pointer to a #GimpColorButton.
727
 * @color:  Pointer to the new #GimpRGB color.
728
 *
729
 * Sets the @button to the given @color.
730
 **/
731
void
732
gimp_color_button_set_color (GimpColorButton *button,
733
                             const GimpRGB   *color)
734
{
735 736
  GimpColorButtonPrivate *priv;

737
  g_return_if_fail (GIMP_IS_COLOR_BUTTON (button));
738 739
  g_return_if_fail (color != NULL);

740 741 742
  priv = GET_PRIVATE (button);

  gimp_color_area_set_color (GIMP_COLOR_AREA (priv->color_area), color);
743 744

  g_object_notify (G_OBJECT (button), "color");
745 746 747
}

/**
748
 * gimp_color_button_get_color:
749
 * @button: Pointer to a #GimpColorButton.
750
 * @color:  Pointer to a #GimpRGB struct used to return the color.
751
 *
752
 * Retrieves the currently set color from the @button.
753
 **/
754
void
755
gimp_color_button_get_color (GimpColorButton *button,
756
                             GimpRGB         *color)
757
{
758 759
  GimpColorButtonPrivate *priv;

760
  g_return_if_fail (GIMP_IS_COLOR_BUTTON (button));
761 762
  g_return_if_fail (color != NULL);

763 764 765
  priv = GET_PRIVATE (button);

  gimp_color_area_get_color (GIMP_COLOR_AREA (priv->color_area), color);
766 767
}

768
/**
769
 * gimp_color_button_has_alpha:
770
 * @button: Pointer to a #GimpColorButton.
771
 *
772
 * Checks whether the @buttons shows transparency information.
773
 *
774 775
 * Returns: %TRUE if the @button shows transparency information, %FALSE
 * otherwise.
776
 **/
777
gboolean
778
gimp_color_button_has_alpha (GimpColorButton *button)
779
{
780 781
  GimpColorButtonPrivate *priv;

782
  g_return_val_if_fail (GIMP_IS_COLOR_BUTTON (button), FALSE);
783

784 785 786
  priv = GET_PRIVATE (button);

  return gimp_color_area_has_alpha (GIMP_COLOR_AREA (priv->color_area));
787 788
}

789 790 791 792 793 794 795
/**
 * gimp_color_button_set_type:
 * @button: Pointer to a #GimpColorButton.
 * @type: the new #GimpColorAreaType
 *
 * Sets the @button to the given @type. See also gimp_color_area_set_type().
 **/
796
void
797
gimp_color_button_set_type (GimpColorButton   *button,
798
                            GimpColorAreaType  type)
799
{
800 801
  GimpColorButtonPrivate *priv;

802
  g_return_if_fail (GIMP_IS_COLOR_BUTTON (button));
803

804 805 806
  priv = GET_PRIVATE (button);

  gimp_color_area_set_type (GIMP_COLOR_AREA (priv->color_area), type);
807 808

  g_object_notify (G_OBJECT (button), "type");
809 810
}

811 812 813 814 815 816 817 818 819 820
/**
 * gimp_color_button_get_update:
 * @button: A #GimpColorButton widget.
 *
 * Returns the color button's @continuous_update property.
 *
 * Return value: the @continuous_update property.
 **/
gboolean
gimp_color_button_get_update (GimpColorButton *button)
821
{
822 823
  GimpColorButtonPrivate *priv;

824
  g_return_val_if_fail (GIMP_IS_COLOR_BUTTON (button), FALSE);
825

826 827 828
  priv = GET_PRIVATE (button);

  return priv->continuous_update;
829 830 831 832 833 834 835
}

/**
 * gimp_color_button_set_update:
 * @button:     A #GimpColorButton widget.
 * @continuous: The new setting of the @continuous_update property.
 *
836
 * When set to #TRUE, the @button will emit the "color-changed"
837 838 839 840 841 842 843
 * continuously while the color is changed in the color selection
 * dialog.
 **/
void
gimp_color_button_set_update (GimpColorButton *button,
                              gboolean         continuous)
{
844 845
  GimpColorButtonPrivate *priv;

846
  g_return_if_fail (GIMP_IS_COLOR_BUTTON (button));
847

848 849
  priv = GET_PRIVATE (button);

850
  if (continuous != priv->continuous_update)
851
    {
852
      priv->continuous_update = continuous ? TRUE : FALSE;
853

854
      if (priv->selection)
855
        {
856
          GimpRGB color;
857

858
          if (priv->continuous_update)
859
            {
860 861
              gimp_color_selection_get_color (GIMP_COLOR_SELECTION (priv->selection),
                                              &color);
862 863 864 865
              gimp_color_button_set_color (button, &color);
            }
          else
            {
866 867
              gimp_color_selection_get_old_color (GIMP_COLOR_SELECTION (priv->selection),
                                                  &color);
868 869 870
              gimp_color_button_set_color (button, &color);
            }
        }
871 872

      g_object_notify (G_OBJECT (button), "continuous-update");
873 874
    }
}
875

876 877 878 879 880 881 882 883 884 885 886 887 888 889
/**
 * gimp_color_button_set_color_config:
 * @button: a #GimpColorButton widget.
 * @config: a #GimpColorConfig object.
 *
 * Sets the color management configuration to use with this color button's
 * #GimpColorArea.
 *
 * Since: 2.10
 */
void
gimp_color_button_set_color_config (GimpColorButton *button,
                                    GimpColorConfig *config)
{
890 891
  GimpColorButtonPrivate *priv;

892 893 894
  g_return_if_fail (GIMP_IS_COLOR_BUTTON (button));
  g_return_if_fail (config == NULL || GIMP_IS_COLOR_CONFIG (config));

895 896
  priv = GET_PRIVATE (button);

897
  if (g_set_object (&priv->config, config))
898
    {
899 900
      if (priv->color_area)
        gimp_color_area_set_color_config (GIMP_COLOR_AREA (priv->color_area),
901 902 903 904 905 906
                                          priv->config);

      if (priv->selection)
        gimp_color_selection_set_config (GIMP_COLOR_SELECTION (priv->selection),
                                         priv->config);
    }
907 908 909 910 911 912 913 914
}

/**
 * gimp_color_button_get_ui_manager:
 * @button: a #GimpColorButton.
 *
 * Returns: The @button's #GtkUIManager.
 *
915
 * Since: 2.10
916 917 918 919
 **/
GtkUIManager *
gimp_color_button_get_ui_manager (GimpColorButton *button)
{
920 921
  GimpColorButtonPrivate *priv;

922 923
  g_return_val_if_fail (GIMP_IS_COLOR_BUTTON (button), NULL);

924 925 926
  priv = GET_PRIVATE (button);

  return priv->ui_manager;
927 928
}

929

930
/*  private functions  */
931

932
static void
933 934 935
gimp_color_button_dialog_response (GtkWidget       *dialog,
                                   gint             response_id,
                                   GimpColorButton *button)
936
{
937 938
  GimpColorButtonPrivate *priv = GET_PRIVATE (button);
  GimpRGB                 color;
939

940 941 942
  switch (response_id)
    {
    case RESPONSE_RESET:
943
      gimp_color_selection_reset (GIMP_COLOR_SELECTION (priv->selection));
944
      break;
945

946
    case GTK_RESPONSE_OK:
947
      if (! priv->continuous_update)
948
        {
949
          gimp_color_selection_get_color (GIMP_COLOR_SELECTION (priv->selection),
950 951 952
                                          &color);
          gimp_color_button_set_color (button, &color);
        }
953

954 955
      gtk_widget_hide (dialog);
      break;
956

957
    default:
958
      if (priv->continuous_update)
959
        {
960
          gimp_color_selection_get_old_color (GIMP_COLOR_SELECTION (priv->selection),
961 962 963
                                              &color);
          gimp_color_button_set_color (button, &color);
        }
964

965 966 967
      gtk_widget_hide (dialog);
      break;
    }
968 969
}

970
static void
971 972
gimp_color_button_use_color (GtkAction       *action,
                             GimpColorButton *button)
973
{
974 975
  const gchar *name;
  GimpRGB      color;
976

977 978
  name = gtk_action_get_name (action);
  gimp_color_button_get_color (button, &color);
979

980
  if (! strcmp (name, GIMP_COLOR_BUTTON_COLOR_FG))
981
    {
982 983
      if (_gimp_get_foreground_func)
        _gimp_get_foreground_func (&color);
984 985
      else
        gimp_rgba_set (&color, 0.0, 0.0, 0.0, 1.0);
986 987 988
    }
  else if (! strcmp (name, GIMP_COLOR_BUTTON_COLOR_BG))
    {
989 990
      if (_gimp_get_background_func)
        _gimp_get_background_func (&color);
991 992
      else
        gimp_rgba_set (&color, 1.0, 1.0, 1.0, 1.0);
993 994 995
    }
  else if (! strcmp (name, GIMP_COLOR_BUTTON_COLOR_BLACK))
    {
996
      gimp_rgba_set (&color, 0.0, 0.0, 0.0, 1.0);
997 998 999
    }
  else if (! strcmp (name, GIMP_COLOR_BUTTON_COLOR_WHITE))
    {
1000
      gimp_rgba_set (&color, 1.0, 1.0, 1.0, 1.0);
1001
    }
1002

1003
  gimp_color_button_set_color (button, &color);
1004 1005 1006
}

static void
1007 1008
gimp_color_button_area_changed (GtkWidget       *color_area,
                                GimpColorButton *button)
1009
{
1010
  GimpColorButtonPrivate *priv = GET_PRIVATE (button);
1011

1012 1013 1014
  if (priv->selection)
    {
      GimpRGB color;
1015

1016
      gimp_color_button_get_color (button, &color);
1017

1018
      g_signal_handlers_block_by_func (priv->selection,
1019 1020
                                       gimp_color_button_selection_changed,
                                       button);
1021

1022 1023
      gimp_color_selection_set_color (GIMP_COLOR_SELECTION (priv->selection),
                                      &color);
1024

1025
      g_signal_handlers_unblock_by_func (priv->selection,
1026 1027
                                         gimp_color_button_selection_changed,
                                         button);
1028 1029
    }

1030
  g_signal_emit (button, gimp_color_button_signals[COLOR_CHANGED], 0);
1031 1032
}

1033 1034 1035 1036
static void
gimp_color_button_selection_changed (GtkWidget       *selection,
                                     GimpColorButton *button)
{
1037 1038 1039
  GimpColorButtonPrivate *priv = GET_PRIVATE (button);

  if (priv->continuous_update)
1040 1041 1042 1043 1044
    {
      GimpRGB color;

      gimp_color_selection_get_color (GIMP_COLOR_SELECTION (selection), &color);

1045
      g_signal_handlers_block_by_func (priv->color_area,
1046 1047 1048
                                       gimp_color_button_area_changed,
                                       button);

1049
      gimp_color_area_set_color (GIMP_COLOR_AREA (priv->color_area), &color);
1050

1051
      g_signal_handlers_unblock_by_func (priv->color_area,
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
                                         gimp_color_button_area_changed,
                                         button);

      g_signal_emit (button, gimp_color_button_signals[COLOR_CHANGED], 0);
    }
}

static void
gimp_color_button_help_func (const gchar *help_id,
                             gpointer     help_data)
{
1063 1064 1065 1066 1067
  GimpColorButton        *button;
  GimpColorButtonPrivate *priv;
  GimpColorSelection     *selection;
  GimpColorNotebook      *notebook;
  GimpColorSelector      *current;
1068

1069 1070
  button = g_object_get_data (G_OBJECT (help_data), COLOR_BUTTON_KEY);
  priv   = GET_PRIVATE (button);
1071

1072 1073
  selection = GIMP_COLOR_SELECTION (priv->selection);
  notebook = GIMP_COLOR_NOTEBOOK (gimp_color_selection_get_notebook (selection));
1074

1075 1076 1077
  current = gimp_color_notebook_get_current_selector (notebook);

  help_id = GIMP_COLOR_SELECTOR_GET_CLASS (current)->help_id;
1078 1079 1080

  gimp_standard_help_func (help_id, NULL);
}