gtkcolorbutton.c 27.9 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 40
#include "gtkdnd.h"
#include "gtkmarshalers.h"
41
#include "gtkprivate.h"
42 43
#include "gtkintl.h"

44 45 46 47 48 49 50

/**
 * SECTION:gtkcolorbutton
 * @Short_description: A button to launch a color selection dialog
 * @Title: GtkColorButton
 * @See_also: #GtkColorSelectionDialog, #GtkFontButton
 *
51
 * The #GtkColorButton is a button which displays the currently selected
52
 * color and allows to open a color selection dialog to change the color.
53
 * It is suitable widget for selecting a color in a preference dialog.
54 55 56
 */


57
struct _GtkColorButtonPrivate
58
{
59
  GtkWidget *swatch;    /* Widget where we draw the color sample */
60
  GtkWidget *cs_dialog; /* Color selection dialog */
61

62
  gchar *title;         /* Title for the color selection window */
63 64
  GdkRGBA rgba;

65 66 67 68
  guint use_alpha : 1;  /* Use alpha or not */
};

/* Properties */
69
enum
70 71 72 73 74
{
  PROP_0,
  PROP_USE_ALPHA,
  PROP_TITLE,
  PROP_COLOR,
75 76
  PROP_ALPHA,
  PROP_RGBA
77 78 79
};

/* Signals */
80
enum
81 82 83 84 85 86
{
  COLOR_SET,
  LAST_SIGNAL
};

/* gobject signals */
87 88 89 90 91 92 93 94 95
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);
96 97

/* gtkwidget signals */
98 99
static void gtk_color_button_state_changed (GtkWidget        *widget,
                                            GtkStateType      previous_state);
100 101

/* gtkbutton signals */
102
static void gtk_color_button_clicked       (GtkButton        *button);
103 104

/* source side drag signals */
105 106 107
static void gtk_color_button_drag_begin    (GtkWidget        *widget,
                                            GdkDragContext   *context,
                                            gpointer          data);
108 109 110 111 112
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
113
                                            GtkColorButton   *button);
114 115 116

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


static guint color_button_signals[LAST_SIGNAL] = { 0 };

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

Matthias Clasen's avatar
Matthias Clasen committed
130 131 132
static void gtk_color_button_iface_init (GtkColorChooserInterface *iface);

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

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

  gobject_class = G_OBJECT_CLASS (klass);
  widget_class = GTK_WIDGET_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;
  widget_class->state_changed = gtk_color_button_state_changed;
  button_class->clicked = gtk_color_button_clicked;
  klass->color_set = NULL;

  /**
   * GtkColorButton:use-alpha:
   *
Matthias Clasen's avatar
Matthias Clasen committed
158 159 160
   * 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.
161 162 163 164 165
   *
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
                                   PROP_USE_ALPHA,
166
                                   g_param_spec_boolean ("use-alpha", P_("Use alpha"),
167
                                                         P_("Whether to give the color an alpha value"),
168
                                                         FALSE,
169
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
170 171 172 173 174 175 176 177 178 179

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

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

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

  /**
221
   * GtkColorButton:rgba:
222 223 224 225 226 227 228 229 230 231 232 233 234 235
   *
   * 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));


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

static void
260
gtk_color_button_state_changed (GtkWidget   *widget,
261
                                GtkStateType previous_state)
262
{
263
  gtk_widget_queue_draw (widget);
264 265 266 267
}

static void
gtk_color_button_drag_data_received (GtkWidget        *widget,
268 269 270 271 272 273
                                     GdkDragContext   *context,
                                     gint              x,
                                     gint              y,
                                     GtkSelectionData *selection_data,
                                     guint             info,
                                     guint32           time,
Matthias Clasen's avatar
Matthias Clasen committed
274
                                     GtkColorButton   *button)
275
{
276
  GtkColorButtonPrivate *priv = button->priv;
277
  gint length;
278 279
  guint16 *dropped;

280 281 282
  length = gtk_selection_data_get_length (selection_data);

  if (length < 0)
283 284
    return;

285 286 287
  /* We accept drops with the wrong format, since the KDE color
   * chooser incorrectly drops application/x-color with format 8.
   */
288
  if (length != 8)
289
    {
Matthias Clasen's avatar
Matthias Clasen committed
290
      g_warning ("%s: Received invalid color data", G_STRFUNC);
291 292 293 294
      return;
    }


295
  dropped = (guint16 *) gtk_selection_data_get_data (selection_data);
296

297 298 299 300
  priv->rgba.red = dropped[0] / 65535.;
  priv->rgba.green = dropped[1] / 65535.;
  priv->rgba.blue = dropped[2] / 65535.;
  priv->rgba.alpha = dropped[3] / 65535.;
301

302
  gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
303

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

Matthias Clasen's avatar
Matthias Clasen committed
306 307 308 309 310
  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));
311 312 313 314
}

static void
set_color_icon (GdkDragContext *context,
315
                GdkRGBA        *rgba)
316
{
317 318
  cairo_surface_t *surface;
  cairo_t *cr;
319

320
  surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 48, 32);
321
  cr = cairo_create (surface);
322

323 324
  gdk_cairo_set_source_rgba (cr, rgba);
  cairo_paint (cr);
325

326 327 328 329
  gtk_drag_set_icon_surface (context, surface);

  cairo_destroy (cr);
  cairo_surface_destroy (surface);
330 331 332 333
}

static void
gtk_color_button_drag_begin (GtkWidget      *widget,
334 335
                             GdkDragContext *context,
                             gpointer        data)
336
{
Matthias Clasen's avatar
Matthias Clasen committed
337
  GtkColorButton *button = data;
338

Matthias Clasen's avatar
Matthias Clasen committed
339
  set_color_icon (context, &button->priv->rgba);
340 341 342 343
}

static void
gtk_color_button_drag_data_get (GtkWidget        *widget,
344 345 346 347
                                GdkDragContext   *context,
                                GtkSelectionData *selection_data,
                                guint             info,
                                guint             time,
Matthias Clasen's avatar
Matthias Clasen committed
348
                                GtkColorButton   *button)
349
{
350
  GtkColorButtonPrivate *priv = button->priv;
351 352
  guint16 dropped[4];

353 354 355 356
  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);
357

358 359
  gtk_selection_data_set (selection_data,
                          gtk_selection_data_get_target (selection_data),
360
                          16, (guchar *)dropped, 8);
361 362 363
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
364
gtk_color_button_init (GtkColorButton *button)
365
{
366
  GtkColorButtonPrivate *priv;
367 368 369 370
  PangoLayout *layout;
  PangoRectangle rect;

  /* Create the widgets */
371
  priv = button->priv = gtk_color_button_get_instance_private (button);
372

373
  priv->swatch = gtk_color_swatch_new ();
Matthias Clasen's avatar
Matthias Clasen committed
374
  layout = gtk_widget_create_pango_layout (GTK_WIDGET (button), "Black");
375
  pango_layout_get_pixel_extents (layout, NULL, &rect);
376 377
  g_object_unref (layout);

378
  gtk_widget_set_size_request (priv->swatch, rect.width, rect.height);
379

380 381
  gtk_container_add (GTK_CONTAINER (button), priv->swatch);
  gtk_widget_show (priv->swatch);
382

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

  /* Start with opaque black, alpha disabled */
386 387 388 389 390
  priv->rgba.red = 0;
  priv->rgba.green = 0;
  priv->rgba.blue = 0;
  priv->rgba.alpha = 1;
  priv->use_alpha = FALSE;
391

Matthias Clasen's avatar
Matthias Clasen committed
392
  gtk_drag_dest_set (GTK_WIDGET (button),
393 394 395 396
                     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
397
  gtk_drag_source_set (GTK_WIDGET (button),
398 399 400
                       GDK_BUTTON1_MASK|GDK_BUTTON3_MASK,
                       drop_types, 1,
                       GDK_ACTION_COPY);
Matthias Clasen's avatar
Matthias Clasen committed
401 402 403 404 405 406
  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);
407 408 409 410 411
}

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

415 416
  if (priv->cs_dialog != NULL)
    gtk_widget_destroy (priv->cs_dialog);
417

418
  g_free (priv->title);
419

Matthias Clasen's avatar
Matthias Clasen committed
420
  G_OBJECT_CLASS (gtk_color_button_parent_class)->finalize (object);
421 422 423 424 425 426
}


/**
 * gtk_color_button_new:
 *
427 428 429 430 431 432 433
 * 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.
434
 *
435
 * Returns: a new color button
436 437 438 439 440 441 442 443 444 445 446
 *
 * Since: 2.4
 */
GtkWidget *
gtk_color_button_new (void)
{
  return g_object_new (GTK_TYPE_COLOR_BUTTON, NULL);
}

/**
 * gtk_color_button_new_with_color:
447
 * @color: A #GdkColor to set the current color with
448
 *
449
 * Creates a new color button.
450
 *
451
 * Returns: a new color button
452 453
 *
 * Since: 2.4
454 455
 *
 * Deprecated: 3.4: Use gtk_color_button_new_with_rgba() instead.
456 457
 */
GtkWidget *
458
gtk_color_button_new_with_color (const GdkColor *color)
459 460 461 462
{
  return g_object_new (GTK_TYPE_COLOR_BUTTON, "color", color, NULL);
}

Matthias Clasen's avatar
Matthias Clasen committed
463 464
/**
 * gtk_color_button_new_with_rgba:
465
 * @rgba: A #GdkRGBA to set the current color with
Matthias Clasen's avatar
Matthias Clasen committed
466 467 468 469 470 471 472
 *
 * Creates a new color button.
 *
 * Returns: a new color button
 *
 * Since: 3.0
 */
473 474 475 476 477 478
GtkWidget *
gtk_color_button_new_with_rgba (const GdkRGBA *rgba)
{
  return g_object_new (GTK_TYPE_COLOR_BUTTON, "rgba", rgba, NULL);
}

479
static gboolean
480 481
dialog_destroy (GtkWidget *widget,
                gpointer   data)
482
{
Matthias Clasen's avatar
Matthias Clasen committed
483
  GtkColorButton *button = GTK_COLOR_BUTTON (data);
484

Matthias Clasen's avatar
Matthias Clasen committed
485
  button->priv->cs_dialog = NULL;
486 487 488 489 490

  return FALSE;
}

static void
491 492 493
dialog_response (GtkDialog *dialog,
                 gint       response,
                 gpointer   data)
494
{
495 496 497 498
  if (response == GTK_RESPONSE_CANCEL)
    gtk_widget_hide (GTK_WIDGET (dialog));
  else if (response == GTK_RESPONSE_OK)
    {
Matthias Clasen's avatar
Matthias Clasen committed
499
      GtkColorButton *button = GTK_COLOR_BUTTON (data);
500
      GtkColorButtonPrivate *priv = button->priv;
501

502 503
      gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (dialog), &priv->rgba);
      gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
504 505 506

      gtk_widget_hide (GTK_WIDGET (dialog));

507
      g_object_ref (button);
Matthias Clasen's avatar
Matthias Clasen committed
508
      g_signal_emit (button, color_button_signals[COLOR_SET], 0);
509

Matthias Clasen's avatar
Matthias Clasen committed
510 511 512 513 514
      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));
515
      g_object_unref (button);
516
    }
517 518
}

519
/* Create the dialog and connects its buttons */
520
static void
521
ensure_dialog (GtkColorButton *button)
522
{
523
  GtkColorButtonPrivate *priv = button->priv;
524
  GtkWidget *parent, *dialog;
525

526
  if (priv->cs_dialog != NULL)
527
    return;
528

529
  parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
530

531
  priv->cs_dialog = dialog = gtk_color_chooser_dialog_new (priv->title, NULL);
532

533 534 535 536
  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));
537

538
    gtk_window_set_modal (GTK_WINDOW (dialog),
539
                          gtk_window_get_modal (GTK_WINDOW (parent)));
540 541 542 543 544 545 546
  }

  g_signal_connect (dialog, "response",
                    G_CALLBACK (dialog_response), button);
  g_signal_connect (dialog, "destroy",
                    G_CALLBACK (dialog_destroy), button);
}
547

548 549 550 551 552

static void
gtk_color_button_clicked (GtkButton *b)
{
  GtkColorButton *button = GTK_COLOR_BUTTON (b);
553
  GtkColorButtonPrivate *priv = button->priv;
554 555 556

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

558
  gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (priv->cs_dialog), priv->use_alpha);
559

560
  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (priv->cs_dialog), &priv->rgba);
561

562
  gtk_window_present (GTK_WINDOW (priv->cs_dialog));
563 564 565 566
}

/**
 * gtk_color_button_set_color:
Matthias Clasen's avatar
Matthias Clasen committed
567
 * @button: a #GtkColorButton
568
 * @color: A #GdkColor to set the current color with
569 570 571 572
 *
 * Sets the current color to be @color.
 *
 * Since: 2.4
573
 *
Matthias Clasen's avatar
Matthias Clasen committed
574
 * Deprecated: Use gtk_color_chooser_set_rgba() instead.
575
 */
576
void
Matthias Clasen's avatar
Matthias Clasen committed
577
gtk_color_button_set_color (GtkColorButton *button,
578
                            const GdkColor *color)
579
{
580 581
  GtkColorButtonPrivate *priv = button->priv;

Matthias Clasen's avatar
Matthias Clasen committed
582
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
583
  g_return_if_fail (color != NULL);
584

585 586 587
  priv->rgba.red = color->red / 65535.;
  priv->rgba.green = color->green / 65535.;
  priv->rgba.blue = color->blue / 65535.;
588

589
  gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
590

Matthias Clasen's avatar
Matthias Clasen committed
591 592
  g_object_notify (G_OBJECT (button), "color");
  g_object_notify (G_OBJECT (button), "rgba");
593 594 595 596 597
}


/**
 * gtk_color_button_set_alpha:
Matthias Clasen's avatar
Matthias Clasen committed
598
 * @button: a #GtkColorButton
599
 * @alpha: an integer between 0 and 65535
600
 *
601
 * Sets the current opacity to be @alpha.
602 603
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
604 605
 *
 * Deprecated: 3.4: Use gtk_color_chooser_set_rgba() instead.
606
 */
607
void
Matthias Clasen's avatar
Matthias Clasen committed
608
gtk_color_button_set_alpha (GtkColorButton *button,
609
                            guint16         alpha)
610
{
611 612
  GtkColorButtonPrivate *priv = button->priv;

Matthias Clasen's avatar
Matthias Clasen committed
613
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
614

615
  priv->rgba.alpha = alpha / 65535.;
616

617
  gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
618

Matthias Clasen's avatar
Matthias Clasen committed
619 620
  g_object_notify (G_OBJECT (button), "alpha");
  g_object_notify (G_OBJECT (button), "rgba");
621 622 623 624
}

/**
 * gtk_color_button_get_color:
Matthias Clasen's avatar
Matthias Clasen committed
625
 * @button: a #GtkColorButton
626
 * @color: (out): a #GdkColor to fill in with the current color
627 628 629 630
 *
 * Sets @color to be the current color in the #GtkColorButton widget.
 *
 * Since: 2.4
631
 *
Matthias Clasen's avatar
Matthias Clasen committed
632
 * Deprecated: 3.4: Use gtk_color_chooser_get_rgba() instead.
633
 */
634
void
Matthias Clasen's avatar
Matthias Clasen committed
635
gtk_color_button_get_color (GtkColorButton *button,
636
                            GdkColor       *color)
637
{
638 639
  GtkColorButtonPrivate *priv = button->priv;

Matthias Clasen's avatar
Matthias Clasen committed
640
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
641

642 643 644
  color->red = (guint16) (priv->rgba.red * 65535);
  color->green = (guint16) (priv->rgba.green * 65535);
  color->blue = (guint16) (priv->rgba.blue * 65535);
645 646 647 648
}

/**
 * gtk_color_button_get_alpha:
Matthias Clasen's avatar
Matthias Clasen committed
649
 * @button: a #GtkColorButton
650
 *
651
 * Returns the current alpha value.
652
 *
653
 * Returns: an integer between 0 and 65535
654 655
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
656 657
 *
 * Deprecated: 3.4: Use gtk_color_chooser_get_rgba() instead.
658
 */
659
guint16
Matthias Clasen's avatar
Matthias Clasen committed
660
gtk_color_button_get_alpha (GtkColorButton *button)
661
{
Matthias Clasen's avatar
Matthias Clasen committed
662
  g_return_val_if_fail (GTK_IS_COLOR_BUTTON (button), 0);
663

Matthias Clasen's avatar
Matthias Clasen committed
664
  return (guint16) (button->priv->rgba.alpha * 65535);
665 666 667
}

/**
668
 * gtk_color_button_set_rgba: (skip)
Matthias Clasen's avatar
Matthias Clasen committed
669
 * @button: a #GtkColorButton
670 671 672 673 674
 * @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
675 676
 *
 * Deprecated: 3.4: Use gtk_color_chooser_set_rgba() instead.
677
 */
678
void
Matthias Clasen's avatar
Matthias Clasen committed
679
gtk_color_button_set_rgba (GtkColorButton *button,
680 681
                           const GdkRGBA  *rgba)
{
682 683
  GtkColorButtonPrivate *priv = button->priv;

Matthias Clasen's avatar
Matthias Clasen committed
684
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
685 686
  g_return_if_fail (rgba != NULL);

687 688
  priv->rgba = *rgba;
  gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (priv->swatch), &priv->rgba);
689

Matthias Clasen's avatar
Matthias Clasen committed
690 691 692
  g_object_notify (G_OBJECT (button), "color");
  g_object_notify (G_OBJECT (button), "alpha");
  g_object_notify (G_OBJECT (button), "rgba");
693 694 695
}

/**
696
 * gtk_color_button_get_rgba: (skip)
Matthias Clasen's avatar
Matthias Clasen committed
697
 * @button: a #GtkColorButton
698
 * @rgba: (out): a #GdkRGBA to fill in with the current color
699 700 701 702
 *
 * Sets @rgba to be the current color in the #GtkColorButton widget.
 *
 * Since: 3.0
Matthias Clasen's avatar
Matthias Clasen committed
703 704
 *
 * Deprecated: 3.4: Use gtk_color_chooser_get_rgba() instead.
705
 */
706
void
Matthias Clasen's avatar
Matthias Clasen committed
707
gtk_color_button_get_rgba (GtkColorButton *button,
708 709
                           GdkRGBA        *rgba)
{
Matthias Clasen's avatar
Matthias Clasen committed
710
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
711 712
  g_return_if_fail (rgba != NULL);

Matthias Clasen's avatar
Matthias Clasen committed
713
  *rgba = button->priv->rgba;
714 715
}

716
static void
717 718
set_use_alpha (GtkColorButton *button,
               gboolean        use_alpha)
719
{
720 721
  GtkColorButtonPrivate *priv = button->priv;

722 723
  use_alpha = (use_alpha != FALSE);

724
  if (priv->use_alpha != use_alpha)
725
    {
726
      priv->use_alpha = use_alpha;
727

728
      gtk_color_swatch_set_use_alpha (GTK_COLOR_SWATCH (priv->swatch), use_alpha);
729 730 731 732 733

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

734 735
/**
 * gtk_color_button_set_use_alpha:
Matthias Clasen's avatar
Matthias Clasen committed
736
 * @button: a #GtkColorButton
737
 * @use_alpha: %TRUE if color button should use alpha channel, %FALSE if not
738 739 740 741
 *
 * Sets whether or not the color button should use the alpha channel.
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
742 743
 *
 * Deprecated: 3.4: Use gtk_color_chooser_set_use_alpha() instead.
744 745
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
746
gtk_color_button_set_use_alpha (GtkColorButton *button,
747
                                gboolean        use_alpha)
748
{
Matthias Clasen's avatar
Matthias Clasen committed
749
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
750
  set_use_alpha (button, use_alpha);
751 752 753 754
}

/**
 * gtk_color_button_get_use_alpha:
Matthias Clasen's avatar
Matthias Clasen committed
755
 * @button: a #GtkColorButton
756
 *
757
 * Does the color selection dialog use the alpha channel ?
758
 *
759
 * Returns: %TRUE if the color sample uses alpha channel, %FALSE if not
760 761
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
762 763
 *
 * Deprecated: 3.4: Use gtk_color_chooser_get_use_alpha() instead.
764 765
 */
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
766
gtk_color_button_get_use_alpha (GtkColorButton *button)
767
{
Matthias Clasen's avatar
Matthias Clasen committed
768
  g_return_val_if_fail (GTK_IS_COLOR_BUTTON (button), FALSE);
769

Matthias Clasen's avatar
Matthias Clasen committed
770
  return button->priv->use_alpha;
771 772 773 774 775
}


/**
 * gtk_color_button_set_title:
Matthias Clasen's avatar
Matthias Clasen committed
776
 * @button: a #GtkColorButton
777
 * @title: String containing new window title
778 779 780 781 782 783
 *
 * Sets the title for the color selection dialog.
 *
 * Since: 2.4
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
784
gtk_color_button_set_title (GtkColorButton *button,
785
                            const gchar    *title)
786
{
787
  GtkColorButtonPrivate *priv = button->priv;
788 789
  gchar *old_title;

Matthias Clasen's avatar
Matthias Clasen committed
790
  g_return_if_fail (GTK_IS_COLOR_BUTTON (button));
791

792 793
  old_title = priv->title;
  priv->title = g_strdup (title);
794 795
  g_free (old_title);

796 797
  if (priv->cs_dialog)
    gtk_window_set_title (GTK_WINDOW (priv->cs_dialog), priv->title);
798

Matthias Clasen's avatar
Matthias Clasen committed
799
  g_object_notify (G_OBJECT (button), "title");
800 801 802 803
}

/**
 * gtk_color_button_get_title:
Matthias Clasen's avatar
Matthias Clasen committed
804
 * @button: a #GtkColorButton
805 806 807 808 809 810 811
 *
 * Gets the title of the color selection dialog.
 *
 * Returns: An internal string, do not free the return value
 *
 * Since: 2.4
 */
812
const gchar *
Matthias Clasen's avatar
Matthias Clasen committed
813
gtk_color_button_get_title (GtkColorButton *button)
814
{
Matthias Clasen's avatar
Matthias Clasen committed
815
  g_return_val_if_fail (GTK_IS_COLOR_BUTTON (button), NULL);
816

Matthias Clasen's avatar
Matthias Clasen committed
817
  return button->priv->title;
818 819 820 821
}

static void
gtk_color_button_set_property (GObject      *object,
822 823 824
                               guint         param_id,
                               const GValue *value,
                               GParamSpec   *pspec)
825
{
Matthias Clasen's avatar
Matthias Clasen committed
826
  GtkColorButton *button = GTK_COLOR_BUTTON (object);
827

828
  switch (param_id)
829 830
    {
    case PROP_USE_ALPHA:
831
      set_use_alpha (button, g_value_get_boolean (value));
832 833
      break;
    case PROP_TITLE:
Matthias Clasen's avatar
Matthias Clasen committed
834
      gtk_color_button_set_title (button, g_value_get_string (value));
835 836
      break;
    case PROP_COLOR:
837 838 839 840 841 842 843 844 845 846 847
      {
        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;

848
        gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (button), &rgba);
849
      }
850 851
      break;
    case PROP_ALPHA:
852
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Matthias Clasen's avatar
Matthias Clasen committed
853
      gtk_color_button_set_alpha (button, g_value_get_uint (value));
854
G_GNUC_END_IGNORE_DEPRECATIONS
855
      break;
856
    case PROP_RGBA:
857
      gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (button), g_value_get_boxed (value));
858
      break;
859 860 861 862 863 864 865 866
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
    }
}

static void
gtk_color_button_get_property (GObject    *object,
867 868 869
                               guint       param_id,
                               GValue     *value,
                               GParamSpec *pspec)
870
{
Matthias Clasen's avatar
Matthias Clasen committed
871
  GtkColorButton *button = GTK_COLOR_BUTTON (object);
872

873
  switch (param_id)
874 875
    {
    case PROP_USE_ALPHA:
876
      g_value_set_boolean (value, button->priv->use_alpha);
877 878
      break;
    case PROP_TITLE:
Matthias Clasen's avatar
Matthias Clasen committed
879
      g_value_set_string (value, gtk_color_button_get_title (button));
880 881
      break;
    case PROP_COLOR:
882 883 884 885
      {
        GdkColor color;
        GdkRGBA rgba;

886
        gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (button), &rgba);
887 888 889 890 891 892 893

        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);
      }
894 895
      break;
    case PROP_ALPHA:
896 897 898 899 900 901 902
      {
        guint16 alpha;

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

        g_value_set_uint (value, alpha);
      }
903
      break;
904 905 906 907
    case PROP_RGBA:
      {
        GdkRGBA rgba;

908
        gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (button), &rgba);
909 910 911
        g_value_set_boxed (value, &rgba);
      }
      break;
912 913 914 915 916
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
    }
}
Matthias Clasen's avatar
Matthias Clasen committed
917

918 919
static void
gtk_color_button_add_palette (GtkColorChooser *chooser,
920
                              GtkOrientation   orientation,
921 922 923 924 925 926
                              gint             colors_per_line,
                              gint             n_colors,
                              GdkRGBA         *colors)
{
  GtkColorButton *button = GTK_COLOR_BUTTON (chooser);

927 928 929
  ensure_dialog (button);

  gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (button->priv->cs_dialog),
930
                                 orientation, colors_per_line, n_colors, colors);
931 932
}

Matthias Clasen's avatar
Matthias Clasen committed
933 934 935 936 937 938
typedef void (* get_rgba) (GtkColorChooser *, GdkRGBA *);
typedef void (* set_rgba) (GtkColorChooser *, const GdkRGBA *);

static void
gtk_color_button_iface_init (GtkColorChooserInterface *iface)
{
939
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Matthias Clasen's avatar
Matthias Clasen committed
940 941
  iface->get_rgba = (get_rgba)gtk_color_button_get_rgba;
  iface->set_rgba = (set_rgba)gtk_color_button_set_rgba;
942
G_GNUC_END_IGNORE_DEPRECATIONS
943
  iface->add_palette = gtk_color_button_add_palette;
Matthias Clasen's avatar
Matthias Clasen committed
944
}