gtkcolorbutton.c 29.4 KB
Newer Older
1
/*
Cody Russell's avatar
Cody Russell committed
2
 * GTK - The GIMP Toolkit
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * Copyright (C) 1998, 1999 Red Hat, Inc.
 * All rights reserved.
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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 Library General Public
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 19 20 21 22 23 24 25
 */
/* Color picker button for GNOME
 *
 * Author: Federico Mena <federico@nuclecu.unam.mx>
 *
 * Modified by the GTK+ Team and others 2003.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
26
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 28
 */

29
#include "config.h"
30 31

#include "gtkcolorbutton.h"
32

33 34
#include "gtkbutton.h"
#include "gtkmain.h"
35
#include "gtkcolorchooser.h"
36
#include "gtkcolorchooserprivate.h"
37
#include "gtkcolorchooserdialog.h"
38
#include "gtkcolorswatchprivate.h"
39
#include "gtkdnd.h"
40
#include "gtkdragdest.h"
41
#include "gtkdragsource.h"
42
#include "gtkmarshalers.h"
43
#include "gtkprivate.h"
44 45
#include "gtkintl.h"

46 47 48 49 50 51 52

/**
 * SECTION:gtkcolorbutton
 * @Short_description: A button to launch a color selection dialog
 * @Title: GtkColorButton
 * @See_also: #GtkColorSelectionDialog, #GtkFontButton
 *
53
 * The #GtkColorButton is a button which displays the currently selected
54
 * color and allows to open a color selection dialog to change the color.
55
 * It is suitable widget for selecting a color in a preference dialog.
56 57 58 59 60
 *
 * # CSS nodes
 *
 * GtkColorButton has a single CSS node with name button. To differentiate
 * it from a plain #GtkButton, it gets the .color style class.
61 62 63
 */


64
struct _GtkColorButtonPrivate
65
{
66
  GtkWidget *swatch;    /* Widget where we draw the color sample */
67
  GtkWidget *cs_dialog; /* Color selection dialog */
68

69
  gchar *title;         /* Title for the color selection window */
70 71
  GdkRGBA rgba;

72
  guint use_alpha : 1;  /* Use alpha or not */
73
  guint show_editor : 1;
74 75 76
};

/* Properties */
77
enum
78 79 80 81 82
{
  PROP_0,
  PROP_USE_ALPHA,
  PROP_TITLE,
  PROP_COLOR,
83
  PROP_ALPHA,
84 85
  PROP_RGBA,
  PROP_SHOW_EDITOR
86 87 88
};

/* Signals */
89
enum
90 91 92 93 94 95
{
  COLOR_SET,
  LAST_SIGNAL
};

/* gobject signals */
96 97 98 99 100 101 102 103 104
static void gtk_color_button_finalize      (GObject          *object);
static void gtk_color_button_set_property  (GObject          *object,
                                            guint             param_id,
                                            const GValue     *value,
                                            GParamSpec       *pspec);
static void gtk_color_button_get_property  (GObject          *object,
                                            guint             param_id,
                                            GValue           *value,
                                            GParamSpec       *pspec);
105 106

/* gtkbutton signals */
107
static void gtk_color_button_clicked       (GtkButton        *button);
108 109

/* source side drag signals */
110 111 112
static void gtk_color_button_drag_begin    (GtkWidget        *widget,
                                            GdkDragContext   *context,
                                            gpointer          data);
113 114 115 116 117
static void gtk_color_button_drag_data_get (GtkWidget        *widget,
                                            GdkDragContext   *context,
                                            GtkSelectionData *selection_data,
                                            guint             info,
                                            guint             time,
Matthias Clasen's avatar
Matthias Clasen committed
118
                                            GtkColorButton   *button);
119 120 121

/* target side drag signals */
static void gtk_color_button_drag_data_received (GtkWidget        *widget,
122 123 124 125 126 127
                                                 GdkDragContext   *context,
                                                 gint              x,
                                                 gint              y,
                                                 GtkSelectionData *selection_data,
                                                 guint             info,
                                                 guint32           time,
Matthias Clasen's avatar
Matthias Clasen committed
128
                                                 GtkColorButton   *button);
129 130 131 132


static guint color_button_signals[LAST_SIGNAL] = { 0 };

133
static const GtkTargetEntry drop_types[] = { { "application/x-color", 0, 0 } };
134

Matthias Clasen's avatar
Matthias Clasen committed
135 136 137
static void gtk_color_button_iface_init (GtkColorChooserInterface *iface);

G_DEFINE_TYPE_WITH_CODE (GtkColorButton, gtk_color_button, GTK_TYPE_BUTTON,
138
                         G_ADD_PRIVATE (GtkColorButton)
Matthias Clasen's avatar
Matthias Clasen committed
139 140
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_COLOR_CHOOSER,
                                                gtk_color_button_iface_init))
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

static void
gtk_color_button_class_init (GtkColorButtonClass *klass)
{
  GObjectClass *gobject_class;
  GtkButtonClass *button_class;

  gobject_class = G_OBJECT_CLASS (klass);
  button_class = GTK_BUTTON_CLASS (klass);

  gobject_class->get_property = gtk_color_button_get_property;
  gobject_class->set_property = gtk_color_button_set_property;
  gobject_class->finalize = gtk_color_button_finalize;
  button_class->clicked = gtk_color_button_clicked;
  klass->color_set = NULL;

  /**
   * GtkColorButton:use-alpha:
   *
Matthias Clasen's avatar
Matthias Clasen committed
160 161 162
   * If this property is set to %TRUE, the color swatch on the button is
   * rendered against a checkerboard background to show its opacity and
   * the opacity slider is displayed in the color selection dialog.
163 164 165 166 167
   *
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
                                   PROP_USE_ALPHA,
168
                                   g_param_spec_boolean ("use-alpha", P_("Use alpha"),
169
                                                         P_("Whether to give the color an alpha value"),
170
                                                         FALSE,
171
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
172 173 174 175 176 177 178 179 180 181

  /**
   * GtkColorButton:title:
   *
   * The title of the color selection dialog
   *
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
                                   PROP_TITLE,
182 183
                                   g_param_spec_string ("title",
                                                        P_("Title"),
184
                                                        P_("The title of the color selection dialog"),
185
                                                        _("Pick a Color"),
186
                                                        GTK_PARAM_READWRITE));
187 188 189 190 191 192 193

  /**
   * GtkColorButton:color:
   *
   * The selected color.
   *
   * Since: 2.4
194 195
   *
   * Deprecated: 3.4: Use #GtkColorButton:rgba instead.
196
   */
Matthias Clasen's avatar
Matthias Clasen committed
197
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
198 199 200
  g_object_class_install_property (gobject_class,
                                   PROP_COLOR,
                                   g_param_spec_boxed ("color",
201 202
                                                       P_("Current Color"),
                                                       P_("The selected color"),
203
                                                       GDK_TYPE_COLOR,
204
                                                       GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
Matthias Clasen's avatar
Matthias Clasen committed
205
G_GNUC_END_IGNORE_DEPRECATIONS
206 207 208 209

  /**
   * GtkColorButton:alpha:
   *
210
   * The selected opacity value (0 fully transparent, 65535 fully opaque).
211 212 213 214 215 216
   *
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
                                   PROP_ALPHA,
                                   g_param_spec_uint ("alpha",
217 218
                                                      P_("Current Alpha"),
                                                      P_("The selected opacity value (0 fully transparent, 65535 fully opaque)"),
219
                                                      0, 65535, 65535,
220
                                                      GTK_PARAM_READWRITE));
221 222

  /**
223
   * GtkColorButton:rgba:
224 225 226 227 228 229 230 231 232 233 234 235 236 237
   *
   * The RGBA color.
   *
   * Since: 3.0
   */
  g_object_class_install_property (gobject_class,
                                   PROP_RGBA,
                                   g_param_spec_boxed ("rgba",
                                                       P_("Current RGBA Color"),
                                                       P_("The selected RGBA color"),
                                                       GDK_TYPE_RGBA,
                                                       GTK_PARAM_READWRITE));


238 239 240
  /**
   * GtkColorButton::color-set:
   * @widget: the object which received the signal.
241 242
   *
   * The ::color-set signal is emitted when the user selects a color.
243
   * When handling this signal, use gtk_color_button_get_rgba() to
244
   * find out which color was just selected.
245
   *
246
   * Note that this signal is only emitted when the user
247 248
   * changes the color. If you need to react to programmatic color changes
   * as well, use the notify::color signal.
249 250 251
   *
   * Since: 2.4
   */
252
  color_button_signals[COLOR_SET] = g_signal_new (I_("color-set"),
253 254 255 256
                                                  G_TYPE_FROM_CLASS (gobject_class),
                                                  G_SIGNAL_RUN_FIRST,
                                                  G_STRUCT_OFFSET (GtkColorButtonClass, color_set),
                                                  NULL, NULL,
257
                                                  NULL,
258
                                                  G_TYPE_NONE, 0);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

  /**
   * GtkColorButton:show-editor:
   *
   * Set this property to %TRUE to skip the palette
   * in the dialog and go directly to the color editor.
   *
   * This property should be used in cases where the palette
   * in the editor would be redundant, such as when the color
   * button is already part of a palette.
   *
   * Since: 3.20
   */
  g_object_class_install_property (gobject_class,
                                   PROP_SHOW_EDITOR,
                                   g_param_spec_boolean ("show-editor", P_("Show Editor"),
                                                         P_("Whether to show the color editor right away"),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
278 279 280 281
}

static void
gtk_color_button_drag_data_received (GtkWidget        *widget,
282 283 284 285 286 287
                                     GdkDragContext   *context,
                                     gint              x,
                                     gint              y,
                                     GtkSelectionData *selection_data,
                                     guint             info,
                                     guint32           time,
Matthias Clasen's avatar
Matthias Clasen committed
288
                                     GtkColorButton   *button)
289
{
290
  GtkColorButtonPrivate *priv = button->priv;
291
  gint length;
292 293
  guint16 *dropped;

294 295 296
  length = gtk_selection_data_get_length (selection_data);

  if (length < 0)
297 298
    return;

299 300 301
  /* We accept drops with the wrong format, since the KDE color
   * chooser incorrectly drops application/x-color with format 8.
   */
302
  if (length != 8)
303
    {
Matthias Clasen's avatar
Matthias Clasen committed
304
      g_warning ("%s: Received invalid color data", G_STRFUNC);
305 306 307 308
      return;
    }


309
  dropped = (guint16 *) gtk_selection_data_get_data (selection_data);
310

311 312 313 314
  priv->rgba.red = dropped[0] / 65535.;
  priv->rgba.green = dropped[1] / 65535.;
  priv->rgba.blue = dropped[2] / 65535.;
  priv->rgba.alpha = dropped[3] / 65535.;
315

316
  gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
317

Matthias Clasen's avatar
Matthias Clasen committed
318
  g_signal_emit (button, color_button_signals[COLOR_SET], 0);
319

Matthias Clasen's avatar
Matthias Clasen committed
320 321 322 323 324
  g_object_freeze_notify (G_OBJECT (button));
  g_object_notify (G_OBJECT (button), "color");
  g_object_notify (G_OBJECT (button), "alpha");
  g_object_notify (G_OBJECT (button), "rgba");
  g_object_thaw_notify (G_OBJECT (button));
325 326 327 328
}

static void
set_color_icon (GdkDragContext *context,
329
                GdkRGBA        *rgba)
330
{
331 332
  cairo_surface_t *surface;
  cairo_t *cr;
333

334
  surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 48, 32);
335
  cr = cairo_create (surface);
336

337 338
  gdk_cairo_set_source_rgba (cr, rgba);
  cairo_paint (cr);
339

340 341 342 343
  gtk_drag_set_icon_surface (context, surface);

  cairo_destroy (cr);
  cairo_surface_destroy (surface);
344 345 346 347
}

static void
gtk_color_button_drag_begin (GtkWidget      *widget,
348 349
                             GdkDragContext *context,
                             gpointer        data)
350
{
Matthias Clasen's avatar
Matthias Clasen committed
351
  GtkColorButton *button = data;
352

Matthias Clasen's avatar
Matthias Clasen committed
353
  set_color_icon (context, &button->priv->rgba);
354 355 356 357
}

static void
gtk_color_button_drag_data_get (GtkWidget        *widget,
358 359 360 361
                                GdkDragContext   *context,
                                GtkSelectionData *selection_data,
                                guint             info,
                                guint             time,
Matthias Clasen's avatar
Matthias Clasen committed
362
                                GtkColorButton   *button)
363
{
364
  GtkColorButtonPrivate *priv = button->priv;
365 366
  guint16 dropped[4];

367 368 369 370
  dropped[0] = (guint16) (priv->rgba.red * 65535);
  dropped[1] = (guint16) (priv->rgba.green * 65535);
  dropped[2] = (guint16) (priv->rgba.blue * 65535);
  dropped[3] = (guint16) (priv->rgba.alpha * 65535);
371

372 373
  gtk_selection_data_set (selection_data,
                          gtk_selection_data_get_target (selection_data),
374
                          16, (guchar *)dropped, 8);
375 376 377
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
378
gtk_color_button_init (GtkColorButton *button)
379
{
380
  GtkColorButtonPrivate *priv;
381 382
  PangoLayout *layout;
  PangoRectangle rect;
383
  GtkStyleContext *context;
384 385

  /* Create the widgets */
386
  priv = button->priv = gtk_color_button_get_instance_private (button);
387

388
  priv->swatch = gtk_color_swatch_new ();
Matthias Clasen's avatar
Matthias Clasen committed
389
  layout = gtk_widget_create_pango_layout (GTK_WIDGET (button), "Black");
390
  pango_layout_get_pixel_extents (layout, NULL, &rect);
391 392
  g_object_unref (layout);

393
  gtk_widget_set_size_request (priv->swatch, rect.width, rect.height);
394

395 396
  gtk_container_add (GTK_CONTAINER (button), priv->swatch);
  gtk_widget_show (priv->swatch);
397

Matthias Clasen's avatar
Matthias Clasen committed
398
  button->priv->title = g_strdup (_("Pick a Color")); /* default title */
399 400

  /* Start with opaque black, alpha disabled */
401 402 403 404 405
  priv->rgba.red = 0;
  priv->rgba.green = 0;
  priv->rgba.blue = 0;
  priv->rgba.alpha = 1;
  priv->use_alpha = FALSE;
406

Matthias Clasen's avatar
Matthias Clasen committed
407
  gtk_drag_dest_set (GTK_WIDGET (button),
408 409 410 411
                     GTK_DEST_DEFAULT_MOTION |
                     GTK_DEST_DEFAULT_HIGHLIGHT |
                     GTK_DEST_DEFAULT_DROP,
                     drop_types, 1, GDK_ACTION_COPY);
Matthias Clasen's avatar
Matthias Clasen committed
412
  gtk_drag_source_set (GTK_WIDGET (button),
413 414 415
                       GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
                       drop_types, 1,
                       GDK_ACTION_COPY);
Matthias Clasen's avatar
Matthias Clasen committed
416 417 418 419 420 421
  g_signal_connect (button, "drag-begin",
                    G_CALLBACK (gtk_color_button_drag_begin), button);
  g_signal_connect (button, "drag-data-received",
                    G_CALLBACK (gtk_color_button_drag_data_received), button);
  g_signal_connect (button, "drag-data-get",
                    G_CALLBACK (gtk_color_button_drag_data_get), button);
422 423 424

  context = gtk_widget_get_style_context (GTK_WIDGET (button));
  gtk_style_context_add_class (context, "color");
425 426 427 428 429
}

static void
gtk_color_button_finalize (GObject *object)
{
Matthias Clasen's avatar
Matthias Clasen committed
430
  GtkColorButton *button = GTK_COLOR_BUTTON (object);
431
  GtkColorButtonPrivate *priv = button->priv;
432

433 434
  if (priv->cs_dialog != NULL)
    gtk_widget_destroy (priv->cs_dialog);
435

436
  g_free (priv->title);
437

Matthias Clasen's avatar
Matthias Clasen committed
438
  G_OBJECT_CLASS (gtk_color_button_parent_class)->finalize (object);
439 440 441 442 443 444
}


/**
 * gtk_color_button_new:
 *
445 446 447 448 449 450 451
 * Creates a new color button.
 *
 * This returns a widget in the form of a small button containing
 * a swatch representing the current selected color. When the button
 * is clicked, a color-selection dialog will open, allowing the user
 * to select a color. The swatch will be updated to reflect the new
 * color when the user finishes.
452
 *
453
 * Returns: a new color button
454 455 456 457 458 459 460 461 462 463 464
 *
 * Since: 2.4
 */
GtkWidget *
gtk_color_button_new (void)
{
  return g_object_new (GTK_TYPE_COLOR_BUTTON, NULL);
}

/**
 * gtk_color_button_new_with_color:
465
 * @color: A #GdkColor to set the current color with
466
 *
467
 * Creates a new color button.
468
 *
469
 * Returns: a new color button
470 471
 *
 * Since: 2.4
472 473
 *
 * Deprecated: 3.4: Use gtk_color_button_new_with_rgba() instead.
474 475
 */
GtkWidget *
476
gtk_color_button_new_with_color (const GdkColor *color)
477 478 479 480
{
  return g_object_new (GTK_TYPE_COLOR_BUTTON, "color", color, NULL);
}

Matthias Clasen's avatar
Matthias Clasen committed
481 482
/**
 * gtk_color_button_new_with_rgba:
483
 * @rgba: A #GdkRGBA to set the current color with
Matthias Clasen's avatar
Matthias Clasen committed
484 485 486 487 488 489 490
 *
 * Creates a new color button.
 *
 * Returns: a new color button
 *
 * Since: 3.0
 */
491 492 493 494 495 496
GtkWidget *
gtk_color_button_new_with_rgba (const GdkRGBA *rgba)
{
  return g_object_new (GTK_TYPE_COLOR_BUTTON, "rgba", rgba, NULL);
}

497 498 499 500 501 502 503 504 505 506
static gboolean
dialog_delete_event (GtkWidget *dialog,
                     GdkEvent  *event,
                     gpointer   user_data)
{
  g_signal_emit_by_name (dialog, "response", GTK_RESPONSE_CANCEL);

  return TRUE;
}

507
static gboolean
508 509
dialog_destroy (GtkWidget *widget,
                gpointer   data)
510
{
Matthias Clasen's avatar
Matthias Clasen committed
511
  GtkColorButton *button = GTK_COLOR_BUTTON (data);
512

Matthias Clasen's avatar
Matthias Clasen committed
513
  button->priv->cs_dialog = NULL;
514 515 516 517 518

  return FALSE;
}

static void
519 520 521
dialog_response (GtkDialog *dialog,
                 gint       response,
                 gpointer   data)
522
{
523 524 525 526
  if (response == GTK_RESPONSE_CANCEL)
    gtk_widget_hide (GTK_WIDGET (dialog));
  else if (response == GTK_RESPONSE_OK)
    {
Matthias Clasen's avatar
Matthias Clasen committed
527
      GtkColorButton *button = GTK_COLOR_BUTTON (data);
528
      GtkColorButtonPrivate *priv = button->priv;
529

530 531
      gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (dialog), &priv->rgba);
      gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
532 533 534

      gtk_widget_hide (GTK_WIDGET (dialog));

535
      g_object_ref (button);
Matthias Clasen's avatar
Matthias Clasen committed
536
      g_signal_emit (button, color_button_signals[COLOR_SET], 0);
537

Matthias Clasen's avatar
Matthias Clasen committed
538 539 540 541 542
      g_object_freeze_notify (G_OBJECT (button));
      g_object_notify (G_OBJECT (button), "color");
      g_object_notify (G_OBJECT (button), "alpha");
      g_object_notify (G_OBJECT (button), "rgba");
      g_object_thaw_notify (G_OBJECT (button));
543
      g_object_unref (button);
544
    }
545 546
}

547
/* Create the dialog and connects its buttons */
548
static void
549
ensure_dialog (GtkColorButton *button)
550
{
551
  GtkColorButtonPrivate *priv = button->priv;
552
  GtkWidget *parent, *dialog;
553

554
  if (priv->cs_dialog != NULL)
555
    return;
556

557
  parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
558

559
  priv->cs_dialog = dialog = gtk_color_chooser_dialog_new (priv->title, NULL);
560

561 562 563 564
  if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
  {
    if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (dialog)))
      gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
565

566
    gtk_window_set_modal (GTK_WINDOW (dialog),
567
                          gtk_window_get_modal (GTK_WINDOW (parent)));
568 569 570 571 572 573
  }

  g_signal_connect (dialog, "response",
                    G_CALLBACK (dialog_response), button);
  g_signal_connect (dialog, "destroy",
                    G_CALLBACK (dialog_destroy), button);
574 575
  g_signal_connect (dialog, "delete-event",
                    G_CALLBACK (dialog_delete_event), button);
576
}
577

578 579 580 581 582

static void
gtk_color_button_clicked (GtkButton *b)
{
  GtkColorButton *button = GTK_COLOR_BUTTON (b);
583
  GtkColorButtonPrivate *priv = button->priv;
584 585 586

  /* if dialog already exists, make sure it's shown and raised */
  ensure_dialog (button);
587

588 589
  g_object_set (priv->cs_dialog, "show-editor", priv->show_editor, NULL);

590
  gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (priv->cs_dialog), priv->use_alpha);
591

592
  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (priv->cs_dialog), &priv->rgba);
593

594
  gtk_window_present (GTK_WINDOW (priv->cs_dialog));
595 596 597 598
}

/**
 * gtk_color_button_set_color:
Matthias Clasen's avatar
Matthias Clasen committed
599
 * @button: a #GtkColorButton
600
 * @color: A #GdkColor to set the current color with
601 602 603 604
 *
 * Sets the current color to be @color.
 *
 * Since: 2.4
605
 *
Matthias Clasen's avatar
Matthias Clasen committed
606
 * Deprecated: Use gtk_color_chooser_set_rgba() instead.
607
 */
608
void
Matthias Clasen's avatar
Matthias Clasen committed
609
gtk_color_button_set_color (GtkColorButton *button,
610
                            const GdkColor *color)
611
{
612 613
  GtkColorButtonPrivate *priv = button->priv;

Matthias Clasen's avatar
Matthias Clasen committed
614
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
615
  g_return_if_fail (color != NULL);
616

617 618 619
  priv->rgba.red = color->red / 65535.;
  priv->rgba.green = color->green / 65535.;
  priv->rgba.blue = color->blue / 65535.;
620

621
  gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
622

Matthias Clasen's avatar
Matthias Clasen committed
623 624
  g_object_notify (G_OBJECT (button), "color");
  g_object_notify (G_OBJECT (button), "rgba");
625 626 627 628 629
}


/**
 * gtk_color_button_set_alpha:
Matthias Clasen's avatar
Matthias Clasen committed
630
 * @button: a #GtkColorButton
631
 * @alpha: an integer between 0 and 65535
632
 *
633
 * Sets the current opacity to be @alpha.
634 635
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
636 637
 *
 * Deprecated: 3.4: Use gtk_color_chooser_set_rgba() instead.
638
 */
639
void
Matthias Clasen's avatar
Matthias Clasen committed
640
gtk_color_button_set_alpha (GtkColorButton *button,
641
                            guint16         alpha)
642
{
643 644
  GtkColorButtonPrivate *priv = button->priv;

Matthias Clasen's avatar
Matthias Clasen committed
645
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
646

647
  priv->rgba.alpha = alpha / 65535.;
648

649
  gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
650

Matthias Clasen's avatar
Matthias Clasen committed
651 652
  g_object_notify (G_OBJECT (button), "alpha");
  g_object_notify (G_OBJECT (button), "rgba");
653 654 655 656
}

/**
 * gtk_color_button_get_color:
Matthias Clasen's avatar
Matthias Clasen committed
657
 * @button: a #GtkColorButton
658
 * @color: (out): a #GdkColor to fill in with the current color
659 660 661 662
 *
 * Sets @color to be the current color in the #GtkColorButton widget.
 *
 * Since: 2.4
663
 *
Matthias Clasen's avatar
Matthias Clasen committed
664
 * Deprecated: 3.4: Use gtk_color_chooser_get_rgba() instead.
665
 */
666
void
Matthias Clasen's avatar
Matthias Clasen committed
667
gtk_color_button_get_color (GtkColorButton *button,
668
                            GdkColor       *color)
669
{
670 671
  GtkColorButtonPrivate *priv = button->priv;

Matthias Clasen's avatar
Matthias Clasen committed
672
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
673

674 675 676
  color->red = (guint16) (priv->rgba.red * 65535);
  color->green = (guint16) (priv->rgba.green * 65535);
  color->blue = (guint16) (priv->rgba.blue * 65535);
677 678 679 680
}

/**
 * gtk_color_button_get_alpha:
Matthias Clasen's avatar
Matthias Clasen committed
681
 * @button: a #GtkColorButton
682
 *
683
 * Returns the current alpha value.
684
 *
685
 * Returns: an integer between 0 and 65535
686 687
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
688 689
 *
 * Deprecated: 3.4: Use gtk_color_chooser_get_rgba() instead.
690
 */
691
guint16
Matthias Clasen's avatar
Matthias Clasen committed
692
gtk_color_button_get_alpha (GtkColorButton *button)
693
{
Matthias Clasen's avatar
Matthias Clasen committed
694
  g_return_val_if_fail (GTK_IS_COLOR_BUTTON (button), 0);
695

Matthias Clasen's avatar
Matthias Clasen committed
696
  return (guint16) (button->priv->rgba.alpha * 65535);
697 698 699
}

/**
700
 * gtk_color_button_set_rgba: (skip)
Matthias Clasen's avatar
Matthias Clasen committed
701
 * @button: a #GtkColorButton
702 703 704 705 706
 * @rgba: a #GdkRGBA to set the current color with
 *
 * Sets the current color to be @rgba.
 *
 * Since: 3.0
Matthias Clasen's avatar
Matthias Clasen committed
707 708
 *
 * Deprecated: 3.4: Use gtk_color_chooser_set_rgba() instead.
709
 */
710
void
Matthias Clasen's avatar
Matthias Clasen committed
711
gtk_color_button_set_rgba (GtkColorButton *button,
712 713
                           const GdkRGBA  *rgba)
{
714 715
  GtkColorButtonPrivate *priv = button->priv;

Matthias Clasen's avatar
Matthias Clasen committed
716
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
717 718
  g_return_if_fail (rgba != NULL);

719 720
  priv->rgba = *rgba;
  gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
721

Matthias Clasen's avatar
Matthias Clasen committed
722 723 724
  g_object_notify (G_OBJECT (button), "color");
  g_object_notify (G_OBJECT (button), "alpha");
  g_object_notify (G_OBJECT (button), "rgba");
725 726 727
}

/**
728
 * gtk_color_button_get_rgba: (skip)
Matthias Clasen's avatar
Matthias Clasen committed
729
 * @button: a #GtkColorButton
730
 * @rgba: (out): a #GdkRGBA to fill in with the current color
731 732 733 734
 *
 * Sets @rgba to be the current color in the #GtkColorButton widget.
 *
 * Since: 3.0
Matthias Clasen's avatar
Matthias Clasen committed
735 736
 *
 * Deprecated: 3.4: Use gtk_color_chooser_get_rgba() instead.
737
 */
738
void
Matthias Clasen's avatar
Matthias Clasen committed
739
gtk_color_button_get_rgba (GtkColorButton *button,
740 741
                           GdkRGBA        *rgba)
{
Matthias Clasen's avatar
Matthias Clasen committed
742
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
743 744
  g_return_if_fail (rgba != NULL);

Matthias Clasen's avatar
Matthias Clasen committed
745
  *rgba = button->priv->rgba;
746 747
}

748
static void
749 750
set_use_alpha (GtkColorButton *button,
               gboolean        use_alpha)
751
{
752 753
  GtkColorButtonPrivate *priv = button->priv;

754 755
  use_alpha = (use_alpha != FALSE);

756
  if (priv->use_alpha != use_alpha)
757
    {
758
      priv->use_alpha = use_alpha;
759

760
      gtk_color_swatch_set_use_alpha (GTK_COLOR_SWATCH (priv->swatch), use_alpha);
761 762 763 764 765

      g_object_notify (G_OBJECT (button), "use-alpha");
    }
}

766 767
/**
 * gtk_color_button_set_use_alpha:
Matthias Clasen's avatar
Matthias Clasen committed
768
 * @button: a #GtkColorButton
769
 * @use_alpha: %TRUE if color button should use alpha channel, %FALSE if not
770 771 772 773
 *
 * Sets whether or not the color button should use the alpha channel.
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
774 775
 *
 * Deprecated: 3.4: Use gtk_color_chooser_set_use_alpha() instead.
776 777
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
778
gtk_color_button_set_use_alpha (GtkColorButton *button,
779
                                gboolean        use_alpha)
780
{
Matthias Clasen's avatar
Matthias Clasen committed
781
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
782
  set_use_alpha (button, use_alpha);
783 784 785 786
}

/**
 * gtk_color_button_get_use_alpha:
Matthias Clasen's avatar
Matthias Clasen committed
787
 * @button: a #GtkColorButton
788
 *
789
 * Does the color selection dialog use the alpha channel ?
790
 *
791
 * Returns: %TRUE if the color sample uses alpha channel, %FALSE if not
792 793
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
794 795
 *
 * Deprecated: 3.4: Use gtk_color_chooser_get_use_alpha() instead.
796 797
 */
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
798
gtk_color_button_get_use_alpha (GtkColorButton *button)
799
{
Matthias Clasen's avatar
Matthias Clasen committed
800
  g_return_val_if_fail (GTK_IS_COLOR_BUTTON (button), FALSE);
801

Matthias Clasen's avatar
Matthias Clasen committed
802
  return button->priv->use_alpha;
803 804 805 806 807
}


/**
 * gtk_color_button_set_title:
Matthias Clasen's avatar
Matthias Clasen committed
808
 * @button: a #GtkColorButton
809
 * @title: String containing new window title
810 811 812 813 814 815
 *
 * Sets the title for the color selection dialog.
 *
 * Since: 2.4
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
816
gtk_color_button_set_title (GtkColorButton *button,
817
                            const gchar    *title)
818
{
819
  GtkColorButtonPrivate *priv = button->priv;
820 821
  gchar *old_title;

Matthias Clasen's avatar
Matthias Clasen committed
822
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
823

824 825
  old_title = priv->title;
  priv->title = g_strdup (title);
826 827
  g_free (old_title);

828 829
  if (priv->cs_dialog)
    gtk_window_set_title (GTK_WINDOW (priv->cs_dialog), priv->title);
830

Matthias Clasen's avatar
Matthias Clasen committed
831
  g_object_notify (G_OBJECT (button), "title");
832 833 834 835
}

/**
 * gtk_color_button_get_title:
Matthias Clasen's avatar
Matthias Clasen committed
836
 * @button: a #GtkColorButton
837 838 839 840 841 842 843
 *
 * Gets the title of the color selection dialog.
 *
 * Returns: An internal string, do not free the return value
 *
 * Since: 2.4
 */
844
const gchar *
Matthias Clasen's avatar
Matthias Clasen committed
845
gtk_color_button_get_title (GtkColorButton *button)
846
{
Matthias Clasen's avatar
Matthias Clasen committed
847
  g_return_val_if_fail (GTK_IS_COLOR_BUTTON (button), NULL);
848

Matthias Clasen's avatar
Matthias Clasen committed
849
  return button->priv->title;
850 851 852 853
}

static void
gtk_color_button_set_property (GObject      *object,
854 855 856
                               guint         param_id,
                               const GValue *value,
                               GParamSpec   *pspec)
857
{
Matthias Clasen's avatar
Matthias Clasen committed
858
  GtkColorButton *button = GTK_COLOR_BUTTON (object);
859

860
  switch (param_id)
861 862
    {
    case PROP_USE_ALPHA:
863
      set_use_alpha (button, g_value_get_boolean (value));
864 865
      break;
    case PROP_TITLE:
Matthias Clasen's avatar
Matthias Clasen committed
866
      gtk_color_button_set_title (button, g_value_get_string (value));
867 868
      break;
    case PROP_COLOR:
869 870 871 872 873 874 875 876 877 878 879
      {
        GdkColor *color;
        GdkRGBA rgba;

        color = g_value_get_boxed (value);

        rgba.red = color->red / 65535.0;
        rgba.green = color->green / 65535.0;
        rgba.blue = color->blue / 65535.0;
        rgba.alpha = 1.0;

880
        gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (button), &rgba);
881
      }
882 883
      break;
    case PROP_ALPHA:
884
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Matthias Clasen's avatar
Matthias Clasen committed
885
      gtk_color_button_set_alpha (button, g_value_get_uint (value));
886
G_GNUC_END_IGNORE_DEPRECATIONS
887
      break;
888
    case PROP_RGBA:
889
      gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (button), g_value_get_boxed (value));
890
      break;
891 892 893 894 895 896 897 898 899 900
    case PROP_SHOW_EDITOR:
      {
        gboolean show_editor = g_value_get_boolean (value);
        if (button->priv->show_editor != show_editor)
          {
            button->priv->show_editor = show_editor;
            g_object_notify (object, "show-editor");
          }
      }
      break;
901 902 903 904 905 906 907 908
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
    }
}

static void
gtk_color_button_get_property (GObject    *object,
909 910 911
                               guint       param_id,
                               GValue     *value,
                               GParamSpec *pspec)
912
{
Matthias Clasen's avatar
Matthias Clasen committed
913
  GtkColorButton *button = GTK_COLOR_BUTTON (object);
914

915
  switch (param_id)
916 917
    {
    case PROP_USE_ALPHA:
918
      g_value_set_boolean (value, button->priv->use_alpha);
919 920
      break;
    case PROP_TITLE:
Matthias Clasen's avatar
Matthias Clasen committed
921
      g_value_set_string (value, gtk_color_button_get_title (button));
922 923
      break;
    case PROP_COLOR:
924 925 926 927
      {
        GdkColor color;
        GdkRGBA rgba;

928
        gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (button), &rgba);
929 930 931 932 933 934 935

        color.red = (guint16) (rgba.red * 65535 + 0.5);
        color.green = (guint16) (rgba.green * 65535 + 0.5);
        color.blue = (guint16) (rgba.blue * 65535 + 0.5);

        g_value_set_boxed (value, &color);
      }
936 937
      break;
    case PROP_ALPHA:
938 939 940 941 942 943 944
      {
        guint16 alpha;

        alpha = (guint16) (button->priv->rgba.alpha * 65535 + 0.5);

        g_value_set_uint (value, alpha);
      }
945
      break;
946 947 948 949
    case PROP_RGBA:
      {
        GdkRGBA rgba;

950
        gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (button), &rgba);
951 952 953
        g_value_set_boxed (value, &rgba);
      }
      break;
954 955 956
    case PROP_SHOW_EDITOR:
      g_value_set_boolean (value, button->priv->show_editor);
      break;
957 958 959 960 961
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
    }
}
Matthias Clasen's avatar
Matthias Clasen committed
962

963 964
static void
gtk_color_button_add_palette (GtkColorChooser *chooser,
965
                              GtkOrientation   orientation,
966 967 968 969 970 971
                              gint             colors_per_line,
                              gint             n_colors,
                              GdkRGBA         *colors)
{
  GtkColorButton *button = GTK_COLOR_BUTTON (chooser);

972 973 974
  ensure_dialog (button);

  gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (button->priv->cs_dialog),
975
                                 orientation, colors_per_line, n_colors, colors);
976 977
}

Matthias Clasen's avatar
Matthias Clasen committed
978 979 980 981 982 983
typedef void (* get_rgba) (GtkColorChooser *, GdkRGBA *);
typedef void (* set_rgba) (GtkColorChooser *, const GdkRGBA *);

static void
gtk_color_button_iface_init (GtkColorChooserInterface *iface)
{
984
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Matthias Clasen's avatar
Matthias Clasen committed
985 986
  iface->get_rgba = (get_rgba)gtk_color_button_get_rgba;
  iface->set_rgba = (set_rgba)gtk_color_button_set_rgba;
987
G_GNUC_END_IGNORE_DEPRECATIONS
988
  iface->add_palette = gtk_color_button_add_palette;
Matthias Clasen's avatar
Matthias Clasen committed
989
}
990