1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2012 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkcolorscaleprivate.h"
24 #include "gtkcolorchooserprivate.h"
25 #include "gtkcolorutils.h"
26 #include "gtkorientable.h"
27 #include "gtkstylecontext.h"
28 #include "gtkaccessible.h"
29 #include "gtkprivate.h"
34 struct _GtkColorScalePrivate
36 cairo_surface_t *surface;
39 GtkColorScaleType type;
48 G_DEFINE_TYPE (GtkColorScale, gtk_color_scale, GTK_TYPE_SCALE)
51 gtk_color_scale_get_trough_size (GtkColorScale *scale,
57 GtkWidget *widget = GTK_WIDGET (scale);
58 gint width, height, focus_line_width, focus_padding;
59 gint x_offset, y_offset;
60 gint slider_width, slider_height;
62 gtk_widget_style_get (widget,
63 "focus-line-width", &focus_line_width,
64 "focus-padding", &focus_padding,
65 "slider-width", &slider_width,
66 "slider-length", &slider_height,
69 width = gtk_widget_get_allocated_width (widget) - 2 * (focus_line_width + focus_padding);
70 height = gtk_widget_get_allocated_height (widget) - 2 * (focus_line_width + focus_padding);
72 x_offset = focus_line_width + focus_padding;
73 y_offset = focus_line_width + focus_padding;
75 /* if the slider has a vertical shape, draw the trough asymmetric */
76 if (slider_width > slider_height)
78 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
80 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
81 x_offset += (gint) floor (slider_width / 2.0);
83 width = (gint) floor (slider_width / 2.0);
87 height = (gint) floor (slider_width / 2.0);
96 *x_offset_out = x_offset;
98 *y_offset_out = y_offset;
102 create_surface (GtkColorScale *scale)
104 GtkWidget *widget = GTK_WIDGET (scale);
105 cairo_surface_t *surface;
108 if (!gtk_widget_get_realized (widget))
111 gtk_color_scale_get_trough_size (scale,
115 if (!scale->priv->surface ||
116 width != scale->priv->width ||
117 height != scale->priv->height)
119 surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
122 if (scale->priv->surface)
123 cairo_surface_destroy (scale->priv->surface);
124 scale->priv->surface = surface;
125 scale->priv->width = width;
126 scale->priv->height= height;
129 surface = scale->priv->surface;
131 if (width == 1 || height == 1)
134 if (scale->priv->type == GTK_COLOR_SCALE_HUE)
138 cairo_surface_t *tmp;
139 guint red, green, blue;
146 stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
148 data = g_malloc (height * stride);
150 f = 1.0 / (height - 1);
151 for (y = 0; y < height; y++)
153 h = CLAMP (y * f, 0.0, 1.0);
154 p = data + y * (stride / 4);
155 for (x = 0; x < width; x++)
157 gtk_hsv_to_rgb (h, 1, 1, &r, &g, &b);
158 red = CLAMP (r * 255, 0, 255);
159 green = CLAMP (g * 255, 0, 255);
160 blue = CLAMP (b * 255, 0, 255);
161 p[x] = (red << 16) | (green << 8) | blue;
165 tmp = cairo_image_surface_create_for_data ((guchar *)data, CAIRO_FORMAT_RGB24,
166 width, height, stride);
167 cr = cairo_create (surface);
169 cairo_set_source_surface (cr, tmp, 0, 0);
173 cairo_surface_destroy (tmp);
176 else if (scale->priv->type == GTK_COLOR_SCALE_ALPHA)
179 cairo_pattern_t *pattern;
180 cairo_matrix_t matrix;
183 cr = cairo_create (surface);
185 cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
187 cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
189 pattern = _gtk_color_chooser_get_checkered_pattern ();
190 cairo_matrix_init_scale (&matrix, 0.125, 0.125);
191 cairo_pattern_set_matrix (pattern, &matrix);
192 cairo_mask (cr, pattern);
193 cairo_pattern_destroy (pattern);
195 color = &scale->priv->color;
197 pattern = cairo_pattern_create_linear (0, 0, width, 0);
198 cairo_pattern_add_color_stop_rgba (pattern, 0, color->red, color->green, color->blue, 0);
199 cairo_pattern_add_color_stop_rgba (pattern, width, color->red, color->green, color->blue, 1);
200 cairo_set_source (cr, pattern);
202 cairo_pattern_destroy (pattern);
209 scale_draw (GtkWidget *widget,
212 GtkColorScale *scale = GTK_COLOR_SCALE (widget);
213 gint width, height, x_offset, y_offset;
214 cairo_pattern_t *pattern;
216 create_surface (scale);
217 gtk_color_scale_get_trough_size (scale,
218 &x_offset, &y_offset,
222 cairo_translate (cr, x_offset, y_offset);
223 cairo_rectangle (cr, 0, 0, width, height);
225 pattern = cairo_pattern_create_for_surface (scale->priv->surface);
226 if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL &&
227 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
229 cairo_matrix_t matrix;
231 cairo_matrix_init_scale (&matrix, -1, 1);
232 cairo_matrix_translate (&matrix, -width, 0);
233 cairo_pattern_set_matrix (pattern, &matrix);
235 cairo_set_source (cr, pattern);
238 cairo_pattern_destroy (pattern);
242 GTK_WIDGET_CLASS (gtk_color_scale_parent_class)->draw (widget, cr);
248 gtk_color_scale_init (GtkColorScale *scale)
250 scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
251 GTK_TYPE_COLOR_SCALE,
252 GtkColorScalePrivate);
256 scale_finalize (GObject *object)
258 GtkColorScale *scale = GTK_COLOR_SCALE (object);
260 if (scale->priv->surface)
261 cairo_surface_destroy (scale->priv->surface);
263 G_OBJECT_CLASS (gtk_color_scale_parent_class)->finalize (object);
267 scale_get_property (GObject *object,
272 GtkColorScale *scale = GTK_COLOR_SCALE (object);
276 case PROP_SCALE_TYPE:
277 g_value_set_int (value, scale->priv->type);
280 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286 scale_set_type (GtkColorScale *scale,
287 GtkColorScaleType type)
291 scale->priv->type = type;
293 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (scale));
294 if (GTK_IS_ACCESSIBLE (atk_obj))
296 if (type == GTK_COLOR_SCALE_HUE)
297 atk_object_set_name (atk_obj, C_("Color channel", "Hue"));
298 else if (type == GTK_COLOR_SCALE_ALPHA)
299 atk_object_set_name (atk_obj, C_("Color channel", "Alpha"));
300 atk_object_set_role (gtk_widget_get_accessible (GTK_WIDGET (scale)), ATK_ROLE_COLOR_CHOOSER);
305 scale_set_property (GObject *object,
310 GtkColorScale *scale = GTK_COLOR_SCALE (object);
314 case PROP_SCALE_TYPE:
315 scale_set_type (scale, (GtkColorScaleType)g_value_get_int (value));
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
324 gtk_color_scale_class_init (GtkColorScaleClass *class)
326 GObjectClass *object_class = G_OBJECT_CLASS (class);
327 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
329 object_class->finalize = scale_finalize;
330 object_class->get_property = scale_get_property;
331 object_class->set_property = scale_set_property;
333 widget_class->draw = scale_draw;
335 g_object_class_install_property (object_class, PROP_SCALE_TYPE,
336 g_param_spec_int ("scale-type", P_("Scale type"), P_("Scale type"),
338 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
340 g_type_class_add_private (class, sizeof (GtkColorScalePrivate));
344 gtk_color_scale_set_rgba (GtkColorScale *scale,
345 const GdkRGBA *color)
347 scale->priv->color = *color;
348 scale->priv->width = -1; /* force surface refresh */
349 create_surface (scale);
350 gtk_widget_queue_draw (GTK_WIDGET (scale));
354 gtk_color_scale_new (GtkAdjustment *adjustment,
355 GtkColorScaleType type)
357 return (GtkWidget *) g_object_new (GTK_TYPE_COLOR_SCALE,
358 "adjustment", adjustment,