Getting colder with our second freeze... it's 3.31.91 release day and string freeze, upload a tarball and lock those strings 🏂

gtkcolorchooserdialog.c 9 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 Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20
 */

#include "config.h"

#include "gtkdialog.h"
21
#include "gtkdialogprivate.h"
22
#include "gtkbutton.h"
23
#include "gtkbox.h"
24
#include "gtkprivate.h"
25
#include "gtkintl.h"
26
#include "gtksettings.h"
27 28 29 30 31

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

Matthias Clasen's avatar
Matthias Clasen committed
32 33 34 35 36 37 38 39 40 41 42
/**
 * 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
 */
43 44 45

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

enum
{
  PROP_ZERO,
Matthias Clasen's avatar
Matthias Clasen committed
52 53
  PROP_RGBA,
  PROP_USE_ALPHA,
54
  PROP_SHOW_EDITOR
55 56 57 58 59
};

static void gtk_color_chooser_dialog_iface_init (GtkColorChooserInterface *iface);

G_DEFINE_TYPE_WITH_CODE (GtkColorChooserDialog, gtk_color_chooser_dialog, GTK_TYPE_DIALOG,
60
                         G_ADD_PRIVATE (GtkColorChooserDialog)
61 62 63 64 65 66 67 68
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_COLOR_CHOOSER,
                                                gtk_color_chooser_dialog_iface_init))

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

72 73 74 75 76 77 78 79 80 81 82 83
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);
}

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

static void
94 95 96
gtk_color_chooser_dialog_response (GtkDialog *dialog,
                                   gint       response_id,
                                   gpointer   user_data)
97
{
98 99
  if (response_id == GTK_RESPONSE_OK)
    save_color (GTK_COLOR_CHOOSER_DIALOG (dialog));
100 101 102 103 104
}

static void
gtk_color_chooser_dialog_init (GtkColorChooserDialog *cc)
{
105
  cc->priv = gtk_color_chooser_dialog_get_instance_private (cc);
106

107
  gtk_widget_init_template (GTK_WIDGET (cc));
108 109 110 111 112 113 114 115 116 117 118
  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (cc));

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (cc),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
G_GNUC_END_IGNORE_DEPRECATIONS

  g_signal_connect (cc, "response",
                    G_CALLBACK (gtk_color_chooser_dialog_response), NULL);
119 120
}

121
static void
122
gtk_color_chooser_dialog_unmap (GtkWidget *widget)
123
{
124 125
  GTK_WIDGET_CLASS (gtk_color_chooser_dialog_parent_class)->unmap (widget);

126 127 128
  /* We never want the dialog to come up with the editor,
   * even if it was showing the editor the last time it was used.
   */
129
  g_object_set (widget, "show-editor", FALSE, NULL);
130 131
}

132 133 134 135 136 137
static void
gtk_color_chooser_dialog_get_property (GObject    *object,
                                       guint       prop_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
{
Matthias Clasen's avatar
Matthias Clasen committed
138
  GtkColorChooserDialog *cd = GTK_COLOR_CHOOSER_DIALOG (object);
139 140 141 142
  GtkColorChooser *cc = GTK_COLOR_CHOOSER (object);

  switch (prop_id)
    {
Matthias Clasen's avatar
Matthias Clasen committed
143
    case PROP_RGBA:
144 145 146
      {
        GdkRGBA color;

Matthias Clasen's avatar
Matthias Clasen committed
147
        gtk_color_chooser_get_rgba (cc, &color);
148 149
        g_value_set_boxed (value, &color);
      }
150
      break;
Matthias Clasen's avatar
Matthias Clasen committed
151 152
    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
153
      break;
154 155 156
    case PROP_SHOW_EDITOR:
      {
        gboolean show_editor;
Matthias Clasen's avatar
Matthias Clasen committed
157
        g_object_get (cd->priv->chooser, "show-editor", &show_editor, NULL);
158 159 160
        g_value_set_boolean (value, show_editor);
      }
      break;
161 162 163 164 165 166 167 168 169 170 171 172
    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
173
  GtkColorChooserDialog *cd = GTK_COLOR_CHOOSER_DIALOG (object);
174 175 176 177
  GtkColorChooser *cc = GTK_COLOR_CHOOSER (object);

  switch (prop_id)
    {
Matthias Clasen's avatar
Matthias Clasen committed
178 179
    case PROP_RGBA:
      gtk_color_chooser_set_rgba (cc, g_value_get_boxed (value));
180
      break;
Matthias Clasen's avatar
Matthias Clasen committed
181
    case PROP_USE_ALPHA:
182 183 184 185 186
      if (gtk_color_chooser_get_use_alpha (GTK_COLOR_CHOOSER (cd->priv->chooser)) != g_value_get_boolean (value))
        {
          gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (cd->priv->chooser), g_value_get_boolean (value));
          g_object_notify_by_pspec (object, pspec);
        }
187 188
      break;
    case PROP_SHOW_EDITOR:
Matthias Clasen's avatar
Matthias Clasen committed
189
      g_object_set (cd->priv->chooser,
190 191 192
                    "show-editor", g_value_get_boolean (value),
                    NULL);
      break;
193 194 195 196 197 198 199 200 201 202
    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);
203
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
204 205 206 207

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

208
  widget_class->unmap = gtk_color_chooser_dialog_unmap;
209

Matthias Clasen's avatar
Matthias Clasen committed
210 211
  g_object_class_override_property (object_class, PROP_RGBA, "rgba");
  g_object_class_override_property (object_class, PROP_USE_ALPHA, "use-alpha");
212 213 214 215
  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));

216 217 218
  /* Bind class to template
   */
  gtk_widget_class_set_template_from_resource (widget_class,
219
					       "/org/gtk/libgtk/ui/gtkcolorchooserdialog.ui");
220
  gtk_widget_class_bind_template_child_private (widget_class, GtkColorChooserDialog, chooser);
221 222
  gtk_widget_class_bind_template_callback (widget_class, propagate_notify);
  gtk_widget_class_bind_template_callback (widget_class, color_activated_cb);
223 224 225
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
226 227
gtk_color_chooser_dialog_get_rgba (GtkColorChooser *chooser,
                                   GdkRGBA         *color)
228 229 230
{
  GtkColorChooserDialog *cc = GTK_COLOR_CHOOSER_DIALOG (chooser);

Matthias Clasen's avatar
Matthias Clasen committed
231
  gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (cc->priv->chooser), color);
232 233 234
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
235 236
gtk_color_chooser_dialog_set_rgba (GtkColorChooser *chooser,
                                   const GdkRGBA   *color)
237 238 239
{
  GtkColorChooserDialog *cc = GTK_COLOR_CHOOSER_DIALOG (chooser);

Matthias Clasen's avatar
Matthias Clasen committed
240
  gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (cc->priv->chooser), color);
241 242
}

243 244
static void
gtk_color_chooser_dialog_add_palette (GtkColorChooser *chooser,
245
                                      GtkOrientation   orientation,
246 247 248 249 250 251 252
                                      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),
253
                                 orientation, colors_per_line, n_colors, colors);
254 255
}

256 257 258
static void
gtk_color_chooser_dialog_iface_init (GtkColorChooserInterface *iface)
{
Matthias Clasen's avatar
Matthias Clasen committed
259 260
  iface->get_rgba = gtk_color_chooser_dialog_get_rgba;
  iface->set_rgba = gtk_color_chooser_dialog_set_rgba;
261
  iface->add_palette = gtk_color_chooser_dialog_add_palette;
262 263
}

Matthias Clasen's avatar
Matthias Clasen committed
264 265 266 267 268 269 270
/**
 * 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.
 *
271
 * Returns: a new #GtkColorChooserDialog
Matthias Clasen's avatar
Matthias Clasen committed
272 273 274
 *
 * Since: 3.4
 */
275 276 277 278 279 280 281 282 283 284 285 286 287
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);
}