gtkcolorscale.c 10.7 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
 */

#include "config.h"

20
#include "gtkcolorscaleprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
21

22
#include "gtkcolorchooserprivate.h"
23
#include "gtkgesturelongpress.h"
24
#include "gtkcolorutils.h"
Matthias Clasen's avatar
Matthias Clasen committed
25
#include "gtkorientable.h"
26
#include "gtkstylecontext.h"
Matthias Clasen's avatar
Matthias Clasen committed
27
#include "gtkaccessible.h"
Matthias Clasen's avatar
Matthias Clasen committed
28
#include "gtkprivate.h"
29 30
#include "gtkintl.h"

31 32
#include <math.h>

33 34 35 36 37 38
struct _GtkColorScalePrivate
{
  cairo_surface_t *surface;
  gint width, height;
  GdkRGBA color;
  GtkColorScaleType type;
39

40
  GtkGesture *long_press_gesture;
41 42
};

Matthias Clasen's avatar
Matthias Clasen committed
43 44 45 46 47 48
enum
{
  PROP_ZERO,
  PROP_SCALE_TYPE
};

49 50 51 52 53
static void hold_action (GtkGestureLongPress *gesture,
                         gdouble              x,
                         gdouble              y,
                         GtkColorScale       *scale);

54
G_DEFINE_TYPE_WITH_PRIVATE (GtkColorScale, gtk_color_scale, GTK_TYPE_SCALE)
55

56 57 58 59 60 61 62 63
static void
gtk_color_scale_get_trough_size (GtkColorScale *scale,
                                 gint *x_offset_out,
                                 gint *y_offset_out,
                                 gint *width_out,
                                 gint *height_out)
{
  GtkWidget *widget = GTK_WIDGET (scale);
64
  gint width, height;
65 66 67 68 69 70 71 72
  gint x_offset, y_offset;
  gint slider_width, slider_height;

  gtk_widget_style_get (widget,
                        "slider-width", &slider_width,
                        "slider-length", &slider_height,
                        NULL);

73 74
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);
75

76 77
  x_offset = 0;
  y_offset = 0;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

  /* if the slider has a vertical shape, draw the trough asymmetric */
  if (slider_width > slider_height)
    {
      if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
        {
          if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
            x_offset += (gint) floor (slider_width / 2.0);

          width = (gint) floor (slider_width / 2.0);
        }
      else
        {
          height = (gint) floor (slider_width / 2.0);
        }
    }

  if (width_out)
    *width_out = width;
  if (height_out)
    *height_out = height;
  if (x_offset_out)
    *x_offset_out = x_offset;
  if (y_offset_out)
    *y_offset_out = y_offset;
}

105
static void
Matthias Clasen's avatar
Matthias Clasen committed
106
create_surface (GtkColorScale *scale)
107 108 109
{
  GtkWidget *widget = GTK_WIDGET (scale);
  cairo_surface_t *surface;
Matthias Clasen's avatar
Matthias Clasen committed
110
  gint width, height;
111 112 113 114

  if (!gtk_widget_get_realized (widget))
    return;

115 116 117
  gtk_color_scale_get_trough_size (scale,
                                   NULL, NULL,
                                   &width, &height);
118

Matthias Clasen's avatar
Matthias Clasen committed
119 120
  if (!scale->priv->surface ||
      width != scale->priv->width ||
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
      height != scale->priv->height)
    {
      surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
                                                   CAIRO_CONTENT_COLOR,
                                                   width, height);
      if (scale->priv->surface)
        cairo_surface_destroy (scale->priv->surface);
      scale->priv->surface = surface;
      scale->priv->width = width;
      scale->priv->height= height;
    }
  else
    surface = scale->priv->surface;

  if (width == 1 || height == 1)
    return;

Matthias Clasen's avatar
Matthias Clasen committed
138
  if (scale->priv->type == GTK_COLOR_SCALE_HUE)
139
    {
Matthias Clasen's avatar
Matthias Clasen committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
      cairo_t *cr;
      gint stride;
      cairo_surface_t *tmp;
      guint red, green, blue;
      guint32 *data, *p;
      gdouble h;
      gdouble r, g, b;
      gdouble f;
      gint x, y;

      stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);

      data = g_malloc (height * stride);

      f = 1.0 / (height - 1);
      for (y = 0; y < height; y++)
156
        {
Matthias Clasen's avatar
Matthias Clasen committed
157 158 159 160 161 162 163 164 165 166
          h = CLAMP (y * f, 0.0, 1.0);
          p = data + y * (stride / 4);
          for (x = 0; x < width; x++)
            {
              gtk_hsv_to_rgb (h, 1, 1, &r, &g, &b);
              red = CLAMP (r * 255, 0, 255);
              green = CLAMP (g * 255, 0, 255);
              blue = CLAMP (b * 255, 0, 255);
              p[x] = (red << 16) | (green << 8) | blue;
            }
167 168
        }

Matthias Clasen's avatar
Matthias Clasen committed
169 170 171
      tmp = cairo_image_surface_create_for_data ((guchar *)data, CAIRO_FORMAT_RGB24,
                                                 width, height, stride);
      cr = cairo_create (surface);
172

Matthias Clasen's avatar
Matthias Clasen committed
173 174
      cairo_set_source_surface (cr, tmp, 0, 0);
      cairo_paint (cr);
175

Matthias Clasen's avatar
Matthias Clasen committed
176 177 178
      cairo_destroy (cr);
      cairo_surface_destroy (tmp);
      g_free (data);
179
    }
Matthias Clasen's avatar
Matthias Clasen committed
180 181 182 183 184 185
  else if (scale->priv->type == GTK_COLOR_SCALE_ALPHA)
    {
      cairo_t *cr;
      cairo_pattern_t *pattern;
      cairo_matrix_t matrix;
      GdkRGBA *color;
186

Matthias Clasen's avatar
Matthias Clasen committed
187
      cr = cairo_create (surface);
188

Matthias Clasen's avatar
Matthias Clasen committed
189 190 191
      cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
      cairo_paint (cr);
      cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
192

193
      pattern = _gtk_color_chooser_get_checkered_pattern ();
Matthias Clasen's avatar
Matthias Clasen committed
194 195 196 197
      cairo_matrix_init_scale (&matrix, 0.125, 0.125);
      cairo_pattern_set_matrix (pattern, &matrix);
      cairo_mask (cr, pattern);
      cairo_pattern_destroy (pattern);
198

Matthias Clasen's avatar
Matthias Clasen committed
199
      color = &scale->priv->color;
200

Matthias Clasen's avatar
Matthias Clasen committed
201 202 203 204 205 206
      pattern = cairo_pattern_create_linear (0, 0, width, 0);
      cairo_pattern_add_color_stop_rgba (pattern, 0, color->red, color->green, color->blue, 0);
      cairo_pattern_add_color_stop_rgba (pattern, width, color->red, color->green, color->blue, 1);
      cairo_set_source (cr, pattern);
      cairo_paint (cr);
      cairo_pattern_destroy (pattern);
207

Matthias Clasen's avatar
Matthias Clasen committed
208
      cairo_destroy (cr);
209 210 211 212 213 214 215 216
    }
}

static gboolean
scale_draw (GtkWidget *widget,
            cairo_t   *cr)
{
  GtkColorScale *scale = GTK_COLOR_SCALE (widget);
217
  gint width, height, x_offset, y_offset;
218
  cairo_pattern_t *pattern;
219 220

  create_surface (scale);
221 222 223
  gtk_color_scale_get_trough_size (scale,
                                   &x_offset, &y_offset,
                                   &width, &height);
224 225

  cairo_save (cr);
226 227
  cairo_translate (cr, x_offset, y_offset);
  cairo_rectangle (cr, 0, 0, width, height);
228

229 230 231 232 233 234 235 236 237 238 239
  pattern = cairo_pattern_create_for_surface (scale->priv->surface);
  if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL &&
      gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
    {
      cairo_matrix_t matrix;

      cairo_matrix_init_scale (&matrix, -1, 1);
      cairo_matrix_translate (&matrix, -width, 0);
      cairo_pattern_set_matrix (pattern, &matrix);
    }
  cairo_set_source (cr, pattern);
240
  cairo_fill (cr);
241

242 243
  cairo_pattern_destroy (pattern);

244 245 246 247 248 249 250 251 252 253
  cairo_restore (cr);

  GTK_WIDGET_CLASS (gtk_color_scale_parent_class)->draw (widget, cr);

  return FALSE;
}

static void
gtk_color_scale_init (GtkColorScale *scale)
{
254 255
  scale->priv = gtk_color_scale_get_instance_private (scale);

256
  gtk_widget_add_events (GTK_WIDGET (scale), GDK_TOUCH_MASK);
257 258 259 260

  scale->priv->long_press_gesture = gtk_gesture_long_press_new (GTK_WIDGET (scale));
  g_signal_connect (scale->priv->long_press_gesture, "pressed",
                    G_CALLBACK (hold_action), scale);
261
  gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (scale->priv->long_press_gesture),
262
                                              GTK_PHASE_TARGET);
263 264 265 266 267 268 269 270 271 272
}

static void
scale_finalize (GObject *object)
{
  GtkColorScale *scale = GTK_COLOR_SCALE (object);

  if (scale->priv->surface)
    cairo_surface_destroy (scale->priv->surface);

273
  g_clear_object (&scale->priv->long_press_gesture);
274

275 276 277 278
  G_OBJECT_CLASS (gtk_color_scale_parent_class)->finalize (object);
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
279 280 281 282
scale_get_property (GObject    *object,
                    guint       prop_id,
                    GValue     *value,
                    GParamSpec *pspec)
283
{
Matthias Clasen's avatar
Matthias Clasen committed
284
  GtkColorScale *scale = GTK_COLOR_SCALE (object);
285

Matthias Clasen's avatar
Matthias Clasen committed
286
  switch (prop_id)
287
    {
Matthias Clasen's avatar
Matthias Clasen committed
288 289 290 291 292 293
    case PROP_SCALE_TYPE:
      g_value_set_int (value, scale->priv->type);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
294 295 296
    }
}

297
static void
Matthias Clasen's avatar
Matthias Clasen committed
298 299
scale_set_type (GtkColorScale     *scale,
                GtkColorScaleType  type)
300
{
Matthias Clasen's avatar
Matthias Clasen committed
301 302
  AtkObject *atk_obj;

303
  scale->priv->type = type;
Matthias Clasen's avatar
Matthias Clasen committed
304 305 306 307 308 309 310 311

  atk_obj = gtk_widget_get_accessible (GTK_WIDGET (scale));
  if (GTK_IS_ACCESSIBLE (atk_obj))
    {
      if (type == GTK_COLOR_SCALE_HUE)
        atk_object_set_name (atk_obj, C_("Color channel", "Hue"));
      else if (type == GTK_COLOR_SCALE_ALPHA)
        atk_object_set_name (atk_obj, C_("Color channel", "Alpha"));
312
      atk_object_set_role (atk_obj, ATK_ROLE_COLOR_CHOOSER);
Matthias Clasen's avatar
Matthias Clasen committed
313
    }
314 315
}

Matthias Clasen's avatar
Matthias Clasen committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
static void
scale_set_property (GObject      *object,
                    guint         prop_id,
                    const GValue *value,
                    GParamSpec   *pspec)
{
  GtkColorScale *scale = GTK_COLOR_SCALE (object);

  switch (prop_id)
    {
    case PROP_SCALE_TYPE:
      scale_set_type (scale, (GtkColorScaleType)g_value_get_int (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

335
static void
336 337 338 339
hold_action (GtkGestureLongPress *gesture,
             gdouble              x,
             gdouble              y,
             GtkColorScale       *scale)
340 341 342 343 344 345
{
  gboolean handled;

  g_signal_emit_by_name (scale, "popup-menu", &handled);
}

Matthias Clasen's avatar
Matthias Clasen committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
static void
gtk_color_scale_class_init (GtkColorScaleClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

  object_class->finalize = scale_finalize;
  object_class->get_property = scale_get_property;
  object_class->set_property = scale_set_property;

  widget_class->draw = scale_draw;

  g_object_class_install_property (object_class, PROP_SCALE_TYPE,
      g_param_spec_int ("scale-type", P_("Scale type"), P_("Scale type"),
                        0, 1, 0,
                        GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}

void
gtk_color_scale_set_rgba (GtkColorScale *scale,
                          const GdkRGBA *color)
{
  scale->priv->color = *color;
  scale->priv->width = -1; /* force surface refresh */
  create_surface (scale);
  gtk_widget_queue_draw (GTK_WIDGET (scale));
}

374
GtkWidget *
375 376
gtk_color_scale_new (GtkAdjustment     *adjustment,
                     GtkColorScaleType  type)
377
{
378 379 380 381 382
  return g_object_new (GTK_TYPE_COLOR_SCALE,
                       "adjustment", adjustment,
                       "draw-value", FALSE,
                       "scale-type", type,
                       NULL);
383
}