]> Pileus Git - ~andy/gtk/blob - gtk/gtkcolorscale.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkcolorscale.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2012 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include "gtkcolorscaleprivate.h"
21
22 #include "gtkcolorchooserprivate.h"
23 #include "gtkcolorutils.h"
24 #include "gtkorientable.h"
25 #include "gtkstylecontext.h"
26 #include "gtkaccessible.h"
27 #include "gtkpressandholdprivate.h"
28 #include "gtkprivate.h"
29 #include "gtkintl.h"
30
31 #include <math.h>
32
33 struct _GtkColorScalePrivate
34 {
35   cairo_surface_t *surface;
36   gint width, height;
37   GdkRGBA color;
38   GtkColorScaleType type;
39
40   GtkPressAndHold *press_and_hold;
41 };
42
43 enum
44 {
45   PROP_ZERO,
46   PROP_SCALE_TYPE
47 };
48
49 G_DEFINE_TYPE (GtkColorScale, gtk_color_scale, GTK_TYPE_SCALE)
50
51 static void
52 gtk_color_scale_get_trough_size (GtkColorScale *scale,
53                                  gint *x_offset_out,
54                                  gint *y_offset_out,
55                                  gint *width_out,
56                                  gint *height_out)
57 {
58   GtkWidget *widget = GTK_WIDGET (scale);
59   gint width, height, focus_line_width, focus_padding;
60   gint x_offset, y_offset;
61   gint slider_width, slider_height;
62
63   gtk_widget_style_get (widget,
64                         "focus-line-width", &focus_line_width,
65                         "focus-padding", &focus_padding,
66                         "slider-width", &slider_width,
67                         "slider-length", &slider_height,
68                         NULL);
69
70   width = gtk_widget_get_allocated_width (widget) - 2 * (focus_line_width + focus_padding);
71   height = gtk_widget_get_allocated_height (widget) - 2 * (focus_line_width + focus_padding);
72
73   x_offset = focus_line_width + focus_padding;
74   y_offset = focus_line_width + focus_padding;
75
76   /* if the slider has a vertical shape, draw the trough asymmetric */
77   if (slider_width > slider_height)
78     {
79       if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_VERTICAL)
80         {
81           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
82             x_offset += (gint) floor (slider_width / 2.0);
83
84           width = (gint) floor (slider_width / 2.0);
85         }
86       else
87         {
88           height = (gint) floor (slider_width / 2.0);
89         }
90     }
91
92   if (width_out)
93     *width_out = width;
94   if (height_out)
95     *height_out = height;
96   if (x_offset_out)
97     *x_offset_out = x_offset;
98   if (y_offset_out)
99     *y_offset_out = y_offset;
100 }
101
102 static void
103 create_surface (GtkColorScale *scale)
104 {
105   GtkWidget *widget = GTK_WIDGET (scale);
106   cairo_surface_t *surface;
107   gint width, height;
108
109   if (!gtk_widget_get_realized (widget))
110     return;
111
112   gtk_color_scale_get_trough_size (scale,
113                                    NULL, NULL,
114                                    &width, &height);
115
116   if (!scale->priv->surface ||
117       width != scale->priv->width ||
118       height != scale->priv->height)
119     {
120       surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
121                                                    CAIRO_CONTENT_COLOR,
122                                                    width, height);
123       if (scale->priv->surface)
124         cairo_surface_destroy (scale->priv->surface);
125       scale->priv->surface = surface;
126       scale->priv->width = width;
127       scale->priv->height= height;
128     }
129   else
130     surface = scale->priv->surface;
131
132   if (width == 1 || height == 1)
133     return;
134
135   if (scale->priv->type == GTK_COLOR_SCALE_HUE)
136     {
137       cairo_t *cr;
138       gint stride;
139       cairo_surface_t *tmp;
140       guint red, green, blue;
141       guint32 *data, *p;
142       gdouble h;
143       gdouble r, g, b;
144       gdouble f;
145       gint x, y;
146
147       stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
148
149       data = g_malloc (height * stride);
150
151       f = 1.0 / (height - 1);
152       for (y = 0; y < height; y++)
153         {
154           h = CLAMP (y * f, 0.0, 1.0);
155           p = data + y * (stride / 4);
156           for (x = 0; x < width; x++)
157             {
158               gtk_hsv_to_rgb (h, 1, 1, &r, &g, &b);
159               red = CLAMP (r * 255, 0, 255);
160               green = CLAMP (g * 255, 0, 255);
161               blue = CLAMP (b * 255, 0, 255);
162               p[x] = (red << 16) | (green << 8) | blue;
163             }
164         }
165
166       tmp = cairo_image_surface_create_for_data ((guchar *)data, CAIRO_FORMAT_RGB24,
167                                                  width, height, stride);
168       cr = cairo_create (surface);
169
170       cairo_set_source_surface (cr, tmp, 0, 0);
171       cairo_paint (cr);
172
173       cairo_destroy (cr);
174       cairo_surface_destroy (tmp);
175       g_free (data);
176     }
177   else if (scale->priv->type == GTK_COLOR_SCALE_ALPHA)
178     {
179       cairo_t *cr;
180       cairo_pattern_t *pattern;
181       cairo_matrix_t matrix;
182       GdkRGBA *color;
183
184       cr = cairo_create (surface);
185
186       cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
187       cairo_paint (cr);
188       cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
189
190       pattern = _gtk_color_chooser_get_checkered_pattern ();
191       cairo_matrix_init_scale (&matrix, 0.125, 0.125);
192       cairo_pattern_set_matrix (pattern, &matrix);
193       cairo_mask (cr, pattern);
194       cairo_pattern_destroy (pattern);
195
196       color = &scale->priv->color;
197
198       pattern = cairo_pattern_create_linear (0, 0, width, 0);
199       cairo_pattern_add_color_stop_rgba (pattern, 0, color->red, color->green, color->blue, 0);
200       cairo_pattern_add_color_stop_rgba (pattern, width, color->red, color->green, color->blue, 1);
201       cairo_set_source (cr, pattern);
202       cairo_paint (cr);
203       cairo_pattern_destroy (pattern);
204
205       cairo_destroy (cr);
206     }
207 }
208
209 static gboolean
210 scale_draw (GtkWidget *widget,
211             cairo_t   *cr)
212 {
213   GtkColorScale *scale = GTK_COLOR_SCALE (widget);
214   gint width, height, x_offset, y_offset;
215   cairo_pattern_t *pattern;
216
217   create_surface (scale);
218   gtk_color_scale_get_trough_size (scale,
219                                    &x_offset, &y_offset,
220                                    &width, &height);
221
222   cairo_save (cr);
223   cairo_translate (cr, x_offset, y_offset);
224   cairo_rectangle (cr, 0, 0, width, height);
225
226   pattern = cairo_pattern_create_for_surface (scale->priv->surface);
227   if (gtk_orientable_get_orientation (GTK_ORIENTABLE (widget)) == GTK_ORIENTATION_HORIZONTAL &&
228       gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
229     {
230       cairo_matrix_t matrix;
231
232       cairo_matrix_init_scale (&matrix, -1, 1);
233       cairo_matrix_translate (&matrix, -width, 0);
234       cairo_pattern_set_matrix (pattern, &matrix);
235     }
236   cairo_set_source (cr, pattern);
237   cairo_fill (cr);
238
239   cairo_pattern_destroy (pattern);
240
241   cairo_restore (cr);
242
243   GTK_WIDGET_CLASS (gtk_color_scale_parent_class)->draw (widget, cr);
244
245   return FALSE;
246 }
247
248 static void
249 gtk_color_scale_init (GtkColorScale *scale)
250 {
251   scale->priv = G_TYPE_INSTANCE_GET_PRIVATE (scale,
252                                              GTK_TYPE_COLOR_SCALE,
253                                              GtkColorScalePrivate);
254   gtk_widget_add_events (GTK_WIDGET (scale), GDK_TOUCH_MASK);
255 }
256
257 static void
258 scale_finalize (GObject *object)
259 {
260   GtkColorScale *scale = GTK_COLOR_SCALE (object);
261
262   if (scale->priv->surface)
263     cairo_surface_destroy (scale->priv->surface);
264
265   g_clear_object (&scale->priv->press_and_hold);
266
267   G_OBJECT_CLASS (gtk_color_scale_parent_class)->finalize (object);
268 }
269
270 static void
271 scale_get_property (GObject    *object,
272                     guint       prop_id,
273                     GValue     *value,
274                     GParamSpec *pspec)
275 {
276   GtkColorScale *scale = GTK_COLOR_SCALE (object);
277
278   switch (prop_id)
279     {
280     case PROP_SCALE_TYPE:
281       g_value_set_int (value, scale->priv->type);
282       break;
283     default:
284       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285       break;
286     }
287 }
288
289 static void
290 scale_set_type (GtkColorScale     *scale,
291                 GtkColorScaleType  type)
292 {
293   AtkObject *atk_obj;
294
295   scale->priv->type = type;
296
297   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (scale));
298   if (GTK_IS_ACCESSIBLE (atk_obj))
299     {
300       if (type == GTK_COLOR_SCALE_HUE)
301         atk_object_set_name (atk_obj, C_("Color channel", "Hue"));
302       else if (type == GTK_COLOR_SCALE_ALPHA)
303         atk_object_set_name (atk_obj, C_("Color channel", "Alpha"));
304       atk_object_set_role (gtk_widget_get_accessible (GTK_WIDGET (scale)), ATK_ROLE_COLOR_CHOOSER);
305     }
306 }
307
308 static void
309 scale_set_property (GObject      *object,
310                     guint         prop_id,
311                     const GValue *value,
312                     GParamSpec   *pspec)
313 {
314   GtkColorScale *scale = GTK_COLOR_SCALE (object);
315
316   switch (prop_id)
317     {
318     case PROP_SCALE_TYPE:
319       scale_set_type (scale, (GtkColorScaleType)g_value_get_int (value));
320       break;
321     default:
322       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323       break;
324     }
325 }
326
327 static void
328 hold_action (GtkPressAndHold *pah,
329              gint             x,
330              gint             y,
331              GtkColorScale   *scale)
332 {
333   gboolean handled;
334
335   g_signal_emit_by_name (scale, "popup-menu", &handled);
336 }
337
338 static gboolean
339 scale_touch (GtkWidget     *widget,
340              GdkEventTouch *event)
341 {
342   GtkColorScale *scale = GTK_COLOR_SCALE (widget);
343
344   if (!scale->priv->press_and_hold)
345     {
346       gint drag_threshold;
347
348       g_object_get (gtk_widget_get_settings (widget),
349                     "gtk-dnd-drag-threshold", &drag_threshold,
350                     NULL);
351
352       scale->priv->press_and_hold = gtk_press_and_hold_new ();
353
354       g_object_set (scale->priv->press_and_hold,
355                     "drag-threshold", drag_threshold,
356                     "hold-time", 1000,
357                     NULL);
358
359       g_signal_connect (scale->priv->press_and_hold, "hold",
360                         G_CALLBACK (hold_action), scale);
361     }
362
363   gtk_press_and_hold_process_event (scale->priv->press_and_hold, (GdkEvent *)event);
364
365   return GTK_WIDGET_CLASS (gtk_color_scale_parent_class)->touch_event (widget, event);
366 }
367
368
369 static void
370 gtk_color_scale_class_init (GtkColorScaleClass *class)
371 {
372   GObjectClass *object_class = G_OBJECT_CLASS (class);
373   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
374
375   object_class->finalize = scale_finalize;
376   object_class->get_property = scale_get_property;
377   object_class->set_property = scale_set_property;
378
379   widget_class->draw = scale_draw;
380   widget_class->touch_event = scale_touch;
381
382   g_object_class_install_property (object_class, PROP_SCALE_TYPE,
383       g_param_spec_int ("scale-type", P_("Scale type"), P_("Scale type"),
384                         0, 1, 0,
385                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
386
387   g_type_class_add_private (class, sizeof (GtkColorScalePrivate));
388 }
389
390 void
391 gtk_color_scale_set_rgba (GtkColorScale *scale,
392                           const GdkRGBA *color)
393 {
394   scale->priv->color = *color;
395   scale->priv->width = -1; /* force surface refresh */
396   create_surface (scale);
397   gtk_widget_queue_draw (GTK_WIDGET (scale));
398 }
399
400 GtkWidget *
401 gtk_color_scale_new (GtkAdjustment     *adjustment,
402                      GtkColorScaleType  type)
403 {
404   return (GtkWidget *) g_object_new (GTK_TYPE_COLOR_SCALE,
405                                      "adjustment", adjustment,
406                                      "draw-value", FALSE,
407                                      "scale-type", type,
408                                      NULL);
409 }