gtkcolorchooserdialog.c 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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 Jardon's avatar
Javier Jardon committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21
 */

#include "config.h"

#include "gtkdialog.h"
#include "gtkstock.h"
22
#include "gtkbutton.h"
23
#include "gtkbox.h"
24
#include "gtkprivate.h"
25 26 27 28 29 30
#include "gtkintl.h"

#include "gtkcolorchooserprivate.h"
#include "gtkcolorchooserdialog.h"
#include "gtkcolorchooserwidget.h"

Matthias Clasen's avatar
Matthias Clasen committed
31 32 33 34 35 36 37 38 39 40 41
/**
 * SECTION:gtkcolorchooserdialog
 * @Short_description: A dialog for choosing colors
 * @Title: GtkColorChooserDialog
 * @See_also: #GtkColorChooser, #GtkDialog
 *
 * The #GtkColorChooserDialog widget is a dialog for choosing
 * a color. It implements the #GtkColorChooser interface.
 *
 * Since: 3.4
 */
42 43 44

struct _GtkColorChooserDialogPrivate
{
Matthias Clasen's avatar
Matthias Clasen committed
45
  GtkWidget *chooser;
46 47 48 49 50 51 52 53

  GtkWidget *select_button;
  GtkWidget *cancel_button;
};

enum
{
  PROP_ZERO,
Matthias Clasen's avatar
Matthias Clasen committed
54 55
  PROP_RGBA,
  PROP_USE_ALPHA,
56
  PROP_SHOW_EDITOR
57 58 59 60 61 62 63 64 65 66 67 68 69
};

static void gtk_color_chooser_dialog_iface_init (GtkColorChooserInterface *iface);

G_DEFINE_TYPE_WITH_CODE (GtkColorChooserDialog, gtk_color_chooser_dialog, GTK_TYPE_DIALOG,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_COLOR_CHOOSER,
                                                gtk_color_chooser_dialog_iface_init))

static void
propagate_notify (GObject               *o,
                  GParamSpec            *pspec,
                  GtkColorChooserDialog *cc)
{
70
  g_object_notify (G_OBJECT (cc), pspec->name);
71 72
}

73 74 75 76 77 78 79 80 81 82 83 84
static void
save_color (GtkColorChooserDialog *dialog)
{
  GdkRGBA color;

  /* This causes the color chooser widget to save the
   * selected and custom colors to GSettings.
   */
  gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (dialog), &color);
  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (dialog), &color);
}

85 86 87 88 89
static void
color_activated_cb (GtkColorChooser *chooser,
                    GdkRGBA         *color,
                    GtkDialog       *dialog)
{
90 91 92 93 94 95 96 97 98
  save_color (GTK_COLOR_CHOOSER_DIALOG (dialog));
  gtk_dialog_response (dialog, GTK_RESPONSE_OK);
}

static void
selected_cb (GtkButton *button,
             GtkDialog *dialog)
{
  save_color (GTK_COLOR_CHOOSER_DIALOG (dialog));
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  gtk_dialog_response (dialog, GTK_RESPONSE_OK);
}

static void
gtk_color_chooser_dialog_init (GtkColorChooserDialog *cc)
{
  GtkColorChooserDialogPrivate *priv;
  GtkDialog *dialog = GTK_DIALOG (cc);
  GtkWidget *action_area, *content_area;

  cc->priv = G_TYPE_INSTANCE_GET_PRIVATE (cc,
                                          GTK_TYPE_COLOR_CHOOSER_DIALOG,
                                          GtkColorChooserDialogPrivate);
  priv = cc->priv;

  content_area = gtk_dialog_get_content_area (dialog);
  action_area = gtk_dialog_get_action_area (dialog);

  gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
  gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
  gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
  gtk_box_set_spacing (GTK_BOX (action_area), 6);

  gtk_widget_push_composite_child ();

  gtk_window_set_resizable (GTK_WINDOW (cc), FALSE);

  /* Create the content area */
Matthias Clasen's avatar
Matthias Clasen committed
127 128 129
  priv->chooser = gtk_color_chooser_widget_new ();
  gtk_container_set_border_width (GTK_CONTAINER (priv->chooser), 5);
  gtk_widget_show (priv->chooser);
130
  gtk_box_pack_start (GTK_BOX (content_area),
Matthias Clasen's avatar
Matthias Clasen committed
131
                      priv->chooser, TRUE, TRUE, 0);
132

Matthias Clasen's avatar
Matthias Clasen committed
133
  g_signal_connect (priv->chooser, "notify::rgba",
134 135
                    G_CALLBACK (propagate_notify), cc);

136 137 138
  g_signal_connect (priv->chooser, "notify::show-editor",
                    G_CALLBACK (propagate_notify), cc);

Matthias Clasen's avatar
Matthias Clasen committed
139
  g_signal_connect (priv->chooser, "color-activated",
140 141 142 143 144 145
                    G_CALLBACK (color_activated_cb), cc);

  /* Create the action area */
  priv->cancel_button = gtk_dialog_add_button (dialog,
                                               GTK_STOCK_CANCEL,
                                               GTK_RESPONSE_CANCEL);
146 147 148 149 150 151 152 153 154 155 156

  /* We emit the response for the Select button manually,
   * since we want to save the color first
   */
  priv->select_button = gtk_button_new_from_stock (_("_Select"));
  g_signal_connect (priv->select_button, "clicked",
                    G_CALLBACK (selected_cb), dialog);
  gtk_widget_set_can_default (priv->select_button, TRUE);
  gtk_widget_show (priv->select_button);
  gtk_box_pack_end (GTK_BOX (gtk_dialog_get_action_area (dialog)),
                    priv->select_button, FALSE, TRUE, 0);
157 158 159 160 161 162 163 164 165 166 167 168
  gtk_widget_grab_default (priv->select_button);

  gtk_dialog_set_alternative_button_order (dialog,
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

  gtk_window_set_title (GTK_WINDOW (cc), _("Select a Color"));

  gtk_widget_pop_composite_child ();
}

169 170 171 172 173 174 175
static void
gtk_color_chooser_dialog_map (GtkWidget *widget)
{
  /* We never want the dialog to come up with the editor,
   * even if it was showing the editor the last time it was used.
   */
  g_object_set (GTK_COLOR_CHOOSER_DIALOG (widget)->priv->chooser,
176
                "show-editor", FALSE, NULL);
177 178

  GTK_WIDGET_CLASS (gtk_color_chooser_dialog_parent_class)->map (widget);
179 180
}

181 182 183 184 185 186
static void
gtk_color_chooser_dialog_get_property (GObject    *object,
                                       guint       prop_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
{
Matthias Clasen's avatar
Matthias Clasen committed
187
  GtkColorChooserDialog *cd = GTK_COLOR_CHOOSER_DIALOG (object);
188 189 190 191
  GtkColorChooser *cc = GTK_COLOR_CHOOSER (object);

  switch (prop_id)
    {
Matthias Clasen's avatar
Matthias Clasen committed
192
    case PROP_RGBA:
193 194 195
      {
        GdkRGBA color;

Matthias Clasen's avatar
Matthias Clasen committed
196
        gtk_color_chooser_get_rgba (cc, &color);
197 198
        g_value_set_boxed (value, &color);
      }
199
      break;
Matthias Clasen's avatar
Matthias Clasen committed
200 201
    case PROP_USE_ALPHA:
      g_value_set_boolean (value, gtk_color_chooser_get_use_alpha (GTK_COLOR_CHOOSER (cd->priv->chooser)));
Matthias Clasen's avatar
Matthias Clasen committed
202
      break;
203 204 205
    case PROP_SHOW_EDITOR:
      {
        gboolean show_editor;
Matthias Clasen's avatar
Matthias Clasen committed
206
        g_object_get (cd->priv->chooser, "show-editor", &show_editor, NULL);
207 208 209
        g_value_set_boolean (value, show_editor);
      }
      break;
210 211 212 213 214 215 216 217 218 219 220 221
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_color_chooser_dialog_set_property (GObject      *object,
                                       guint         prop_id,
                                       const GValue *value,
                                       GParamSpec   *pspec)
{
Matthias Clasen's avatar
Matthias Clasen committed
222
  GtkColorChooserDialog *cd = GTK_COLOR_CHOOSER_DIALOG (object);
223 224 225 226
  GtkColorChooser *cc = GTK_COLOR_CHOOSER (object);

  switch (prop_id)
    {
Matthias Clasen's avatar
Matthias Clasen committed
227 228
    case PROP_RGBA:
      gtk_color_chooser_set_rgba (cc, g_value_get_boxed (value));
229
      break;
Matthias Clasen's avatar
Matthias Clasen committed
230 231
    case PROP_USE_ALPHA:
      gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (cd->priv->chooser), g_value_get_boolean (value));
232 233
      break;
    case PROP_SHOW_EDITOR:
Matthias Clasen's avatar
Matthias Clasen committed
234
      g_object_set (cd->priv->chooser,
235 236 237
                    "show-editor", g_value_get_boolean (value),
                    NULL);
      break;
238 239 240 241 242 243 244 245 246 247
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_color_chooser_dialog_class_init (GtkColorChooserDialogClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
248
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
249 250 251 252

  object_class->get_property = gtk_color_chooser_dialog_get_property;
  object_class->set_property = gtk_color_chooser_dialog_set_property;

253 254
  widget_class->map = gtk_color_chooser_dialog_map;

Matthias Clasen's avatar
Matthias Clasen committed
255 256
  g_object_class_override_property (object_class, PROP_RGBA, "rgba");
  g_object_class_override_property (object_class, PROP_USE_ALPHA, "use-alpha");
257 258 259 260
  g_object_class_install_property (object_class, PROP_SHOW_EDITOR,
      g_param_spec_boolean ("show-editor", P_("Show editor"), P_("Show editor"),
                            FALSE, GTK_PARAM_READWRITE));

261 262 263 264 265

  g_type_class_add_private (class, sizeof (GtkColorChooserDialogPrivate));
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
266 267
gtk_color_chooser_dialog_get_rgba (GtkColorChooser *chooser,
                                   GdkRGBA         *color)
268 269 270
{
  GtkColorChooserDialog *cc = GTK_COLOR_CHOOSER_DIALOG (chooser);

Matthias Clasen's avatar
Matthias Clasen committed
271
  gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (cc->priv->chooser), color);
272 273 274
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
275 276
gtk_color_chooser_dialog_set_rgba (GtkColorChooser *chooser,
                                   const GdkRGBA   *color)
277 278 279
{
  GtkColorChooserDialog *cc = GTK_COLOR_CHOOSER_DIALOG (chooser);

Matthias Clasen's avatar
Matthias Clasen committed
280
  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (cc->priv->chooser), color);
281 282
}

283 284
static void
gtk_color_chooser_dialog_add_palette (GtkColorChooser *chooser,
285
                                      GtkOrientation   orientation,
286 287 288 289 290 291 292
                                      gint             colors_per_line,
                                      gint             n_colors,
                                      GdkRGBA         *colors)
{
  GtkColorChooserDialog *cc = GTK_COLOR_CHOOSER_DIALOG (chooser);

  gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (cc->priv->chooser),
293
                                 orientation, colors_per_line, n_colors, colors);
294 295
}

296 297 298
static void
gtk_color_chooser_dialog_iface_init (GtkColorChooserInterface *iface)
{
Matthias Clasen's avatar
Matthias Clasen committed
299 300
  iface->get_rgba = gtk_color_chooser_dialog_get_rgba;
  iface->set_rgba = gtk_color_chooser_dialog_set_rgba;
301
  iface->add_palette = gtk_color_chooser_dialog_add_palette;
302 303
}

Matthias Clasen's avatar
Matthias Clasen committed
304 305 306 307 308 309 310 311 312 313 314
/**
 * gtk_color_chooser_dialog_new:
 * @title: (allow-none): Title of the dialog, or %NULL
 * @parent: (allow-none): Transient parent of the dialog, or %NULL
 *
 * Creates a new #GtkColorChooserDialog.
 *
 * Return value: a new #GtkColorChooserDialog
 *
 * Since: 3.4
 */
315 316 317 318 319 320 321 322 323 324 325 326 327
GtkWidget *
gtk_color_chooser_dialog_new (const gchar *title,
                              GtkWindow   *parent)
{
  GtkColorChooserDialog *dialog;

  dialog = g_object_new (GTK_TYPE_COLOR_CHOOSER_DIALOG,
                         "title", title,
                         "transient-for", parent,
                         NULL);

  return GTK_WIDGET (dialog);
}