gtkcolorchooser.c 5.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**
 * 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
 */

45 46 47 48 49 50 51 52 53 54 55 56 57
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
58 59 60 61 62 63 64 65 66
  /**
   * 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
   */
67
  g_object_interface_install_property (iface,
Matthias Clasen's avatar
Matthias Clasen committed
68
      g_param_spec_boxed ("rgba",
69 70 71 72 73
                          P_("Color"),
                          P_("Current color, as a GdkRGBA"),
                          GDK_TYPE_RGBA,
                          GTK_PARAM_READWRITE));

Matthias Clasen's avatar
Matthias Clasen committed
74 75 76 77 78 79 80 81 82 83 84 85 86
  /**
   * 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
87
  g_object_interface_install_property (iface,
Matthias Clasen's avatar
Matthias Clasen committed
88 89
      g_param_spec_boolean ("use-alpha",
                            P_("Use alpha"),
Matthias Clasen's avatar
Matthias Clasen committed
90 91 92
                            P_("Whether alpha should be shown"),
                            TRUE,
                            GTK_PARAM_READWRITE));
93

94 95
  /**
   * GtkColorChooser::color-activated:
Matthias Clasen's avatar
Matthias Clasen committed
96
   * @chooser: the object which received the signal
97 98 99 100 101 102
   * @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
103 104 105
   *
   * Since: 3.4
   */
106 107 108 109 110 111 112 113 114 115 116
  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,
                  1, G_TYPE_STRING);
}

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

124
/**
Matthias Clasen's avatar
Matthias Clasen committed
125
 * gtk_color_chooser_get_rgba:
126 127 128 129
 * @chooser: a #GtkColorChooser
 * @color: return location for the color
 *
 * Gets the currently-selected color.
Matthias Clasen's avatar
Matthias Clasen committed
130 131
 *
 * Since: 3.4
132 133
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
134 135
gtk_color_chooser_get_rgba (GtkColorChooser *chooser,
                            GdkRGBA         *color)
136 137 138
{
  g_return_if_fail (GTK_IS_COLOR_CHOOSER (chooser));

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

/**
Matthias Clasen's avatar
Matthias Clasen committed
143
 * gtk_color_chooser_set_rgba:
144 145 146
 * @chooser: a #GtkColorChooser
 * @color: the new color
 *
Matthias Clasen's avatar
Matthias Clasen committed
147
 * Sets the color.
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
}