gtkcolorchooser.c 7.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GTK - The GIMP Toolkit
 *
 * Copyright (C) 2012, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25 26
 */

#include "config.h"

#include "gtkcolorchooser.h"
#include "gtkcolorchooserprivate.h"
#include "gtkintl.h"
#include "gtktypebuiltins.h"
#include "gtkprivate.h"

Matthias Clasen's avatar
Matthias Clasen committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/**
 * SECTION:gtkcolorchooser
 * @Short_description: Interface implemented by widgets for choosing colors
 * @Title: GtkColorChooser
 * @See_also: #GtkColorChooserDialog, #GtkColorChooserWidget, #GtkColorButton
 *
 * #GtkColorChooser is an interface that is implemented by widgets
 * for choosing colors. Depending on the situation, colors may be
 * allowed to have alpha (translucency).
 *
 * In GTK+, the main widgets that implement this interface are
 * #GtkColorChooserWidget, #GtkColorChooserDialog and #GtkColorButton.
 *
 * Since: 3.4
 */

43 44 45 46 47 48 49 50 51 52 53 54 55
enum
{
  COLOR_ACTIVATED,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

G_DEFINE_INTERFACE (GtkColorChooser, gtk_color_chooser, G_TYPE_OBJECT);

static void
gtk_color_chooser_default_init (GtkColorChooserInterface *iface)
{
Matthias Clasen's avatar
Matthias Clasen committed
56 57 58 59 60 61 62 63 64
  /**
   * GtkColorChooser:rgba:
   *
   * The ::rgba property contains the currently selected color,
   * as a #GdkRGBA struct. The property can be set to change
   * the current selection programmatically.
   *
   * Since: 3.4
   */
65
  g_object_interface_install_property (iface,
Matthias Clasen's avatar
Matthias Clasen committed
66
      g_param_spec_boxed ("rgba",
67 68 69 70 71
                          P_("Color"),
                          P_("Current color, as a GdkRGBA"),
                          GDK_TYPE_RGBA,
                          GTK_PARAM_READWRITE));

Matthias Clasen's avatar
Matthias Clasen committed
72 73 74 75 76 77 78 79 80 81 82 83 84
  /**
   * GtkColorChooser:use-alpha:
   *
   * When ::use-alpha is %TRUE, colors may have alpha (translucency)
   * information. When it is %FALSE, the #GdkRGBA struct obtained
   * via the #GtkColorChooser:rgba property will be forced to have
   * alpha == 1.
   *
   * Implementations are expected to show alpha by rendering the color
   * over a non-uniform background (like a checkerboard pattern).
   *
   * Since: 3.4
   */
Matthias Clasen's avatar
Matthias Clasen committed
85
  g_object_interface_install_property (iface,
Matthias Clasen's avatar
Matthias Clasen committed
86 87
      g_param_spec_boolean ("use-alpha",
                            P_("Use alpha"),
Matthias Clasen's avatar
Matthias Clasen committed
88 89
                            P_("Whether alpha should be shown"),
                            TRUE,
90
                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
91

92 93
  /**
   * GtkColorChooser::color-activated:
Matthias Clasen's avatar
Matthias Clasen committed
94
   * @chooser: the object which received the signal
95 96 97 98 99 100
   * @color: the color
   *
   * Emitted when a color is activated from the color chooser.
   * This usually happens when the user clicks a color swatch,
   * or a color is selected and the user presses one of the keys
   * Space, Shift+Space, Return or Enter.
Matthias Clasen's avatar
Matthias Clasen committed
101 102 103
   *
   * Since: 3.4
   */
104 105 106 107 108 109 110 111
  signals[COLOR_ACTIVATED] =
    g_signal_new ("color-activated",
                  GTK_TYPE_COLOR_CHOOSER,
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkColorChooserInterface, color_activated),
                  NULL, NULL,
                  NULL,
                  G_TYPE_NONE,
112
                  1, GDK_TYPE_RGBA);
113 114
}

Matthias Clasen's avatar
Matthias Clasen committed
115 116 117 118 119 120 121
void
_gtk_color_chooser_color_activated (GtkColorChooser *chooser,
                                    const GdkRGBA   *color)
{
  g_signal_emit (chooser, signals[COLOR_ACTIVATED], 0, color);
}

122
/**
Matthias Clasen's avatar
Matthias Clasen committed
123
 * gtk_color_chooser_get_rgba:
124
 * @chooser: a #GtkColorChooser
125
 * @color: (out): a #GdkRGBA to fill in with the current color
126 127
 *
 * Gets the currently-selected color.
Matthias Clasen's avatar
Matthias Clasen committed
128 129
 *
 * Since: 3.4
130 131
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
132 133
gtk_color_chooser_get_rgba (GtkColorChooser *chooser,
                            GdkRGBA         *color)
134 135 136
{
  g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));

Matthias Clasen's avatar
Matthias Clasen committed
137
  GTK_COLOR_CHOOSER_GET_IFACE (chooser)->get_rgba (chooser, color);
138 139 140
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
141
 * gtk_color_chooser_set_rgba:
142 143 144
 * @chooser: a #GtkColorChooser
 * @color: the new color
 *
Matthias Clasen's avatar
Matthias Clasen committed
145
 * Sets the color.
146 147
 *
 * Since: 3.4
148 149
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
150 151
gtk_color_chooser_set_rgba (GtkColorChooser *chooser,
                            const GdkRGBA   *color)
152 153 154 155
{
  g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));
  g_return_if_fail (color != NULL);

Matthias Clasen's avatar
Matthias Clasen committed
156
  GTK_COLOR_CHOOSER_GET_IFACE (chooser)->set_rgba (chooser, color);
157 158
}

Matthias Clasen's avatar
Matthias Clasen committed
159 160 161 162 163 164 165 166 167 168 169
/**
 * gtk_color_chooser_get_use_alpha:
 * @chooser: a #GtkColorChooser
 *
 * Returns whether the color chooser shows the alpha channel.
 *
 * Returns: %TRUE if the color chooser uses the alpha channel,
 *     %FALSE if not
 *
 * Since: 3.4
 */
Matthias Clasen's avatar
Matthias Clasen committed
170
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
171
gtk_color_chooser_get_use_alpha (GtkColorChooser *chooser)
Matthias Clasen's avatar
Matthias Clasen committed
172
{
Matthias Clasen's avatar
Matthias Clasen committed
173
  gboolean use_alpha;
Matthias Clasen's avatar
Matthias Clasen committed
174 175 176

  g_return_val_if_fail (GTK_IS_COLOR_CHOOSER (chooser), TRUE);

Matthias Clasen's avatar
Matthias Clasen committed
177
  g_object_get (chooser, "use-alpha", &use_alpha, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
178

Matthias Clasen's avatar
Matthias Clasen committed
179
  return use_alpha;
Matthias Clasen's avatar
Matthias Clasen committed
180 181
}

Matthias Clasen's avatar
Matthias Clasen committed
182 183 184 185 186 187 188 189 190
/**
 * gtk_color_chooser_set_use_alpha:
 * @chooser: a #GtkColorChooser
 * @use_alpha: %TRUE if color chooser should use alpha channel, %FALSE if not
 *
 * Sets whether or not the color chooser should use the alpha channel.
 *
 * Since: 3.4
 */
Matthias Clasen's avatar
Matthias Clasen committed
191
void
Matthias Clasen's avatar
Matthias Clasen committed
192 193
gtk_color_chooser_set_use_alpha (GtkColorChooser *chooser,
                                 gboolean         use_alpha)
Matthias Clasen's avatar
Matthias Clasen committed
194 195 196 197
{

  g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));

Matthias Clasen's avatar
Matthias Clasen committed
198
  g_object_set (chooser, "use-alpha", use_alpha, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
199
}
200

201 202 203
/**
 * gtk_color_chooser_add_palette:
 * @chooser: a #GtkColorChooser
204 205
 * @orientation: %GTK_ORIENTATION_HORIZONTAL if the palette should
 *     be displayed in rows, %GTK_ORIENTATION_VERTICAL for columns
206 207
 * @colors_per_line: the number of colors to show in each row/column
 * @n_colors: the total number of elements in @colors
208
 * @colors: (allow-none) (array length=n_colors): the colors of the palette, or %NULL
209
 *
210
 * Adds a palette to the color chooser. If @orientation is horizontal,
211 212 213 214 215 216 217 218 219 220 221
 * the colors are grouped in rows, with @colors_per_line colors
 * in each row. If @horizontal is %FALSE, the colors are grouped
 * in columns instead.
 *
 * The default color palette of #GtkColorChooserWidget has
 * 27 colors, organized in columns of 3 colors. The default gray
 * palette has 9 grays in a single row.
 *
 * The layout of the color chooser widget works best when the
 * palettes have 9-10 columns.
 *
222
 * Calling this function for the first time has the
223 224
 * side effect of removing the default color and gray palettes
 * from the color chooser.
225 226 227 228
 *
 * If @colors is %NULL, removes all previously added palettes.
 *
 * Since: 3.4
229
 */
230 231
void
gtk_color_chooser_add_palette (GtkColorChooser *chooser,
232
                               GtkOrientation   orientation,
233 234 235 236 237 238
                               gint             colors_per_line,
                               gint             n_colors,
                               GdkRGBA         *colors)
{
  g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));

239
  if (GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette)
240
    GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette (chooser, orientation, colors_per_line, n_colors, colors);
241
}
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

cairo_pattern_t *
_gtk_color_chooser_get_checkered_pattern (void)
{
  /* need to respect pixman's stride being a multiple of 4 */
  static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
                                   0x00, 0xFF, 0x00, 0x00 };
  static cairo_surface_t *checkered = NULL;
  cairo_pattern_t *pattern;

  if (checkered == NULL)
    checkered = cairo_image_surface_create_for_data (data,
                                                     CAIRO_FORMAT_A8,
                                                     2, 2, 4);

  pattern = cairo_pattern_create_for_surface (checkered);
  cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
  cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);

  return pattern;
}