]> Pileus Git - ~andy/gtk/blob - gtk/gtkcolorplane.c
Move the plane into a separate widget
[~andy/gtk] / gtk / gtkcolorplane.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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include "gtkhsv.h"
23 #include "gtkcolorplane.h"
24
25 struct _GtkColorPlanePrivate
26 {
27   cairo_surface_t *surface;
28   gdouble h, s, v;
29   gint x, y;
30   gboolean in_drag;
31 };
32
33 enum
34 {
35   CHANGED,
36   LAST_SIGNAL
37 };
38
39 guint signals[LAST_SIGNAL];
40
41 G_DEFINE_TYPE (GtkColorPlane, gtk_color_plane, GTK_TYPE_DRAWING_AREA)
42
43 static gboolean
44 sv_draw (GtkWidget *widget,
45          cairo_t   *cr)
46 {
47   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
48   gint x, y;
49   gint width, height;
50
51   cairo_set_source_surface (cr, plane->priv->surface, 0, 0);
52   cairo_paint (cr);
53
54   x = plane->priv->x;
55   y = plane->priv->y;
56   width = gtk_widget_get_allocated_width (widget);
57   height = gtk_widget_get_allocated_height (widget);
58
59   cairo_move_to (cr, 0,     y + 0.5);
60   cairo_line_to (cr, width, y + 0.5);
61
62   cairo_move_to (cr, x + 0.5, 0);
63   cairo_line_to (cr, x + 0.5, height);
64
65   if (gtk_widget_has_visible_focus (widget))
66     {
67       cairo_set_line_width (cr, 3.0);
68       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.6);
69       cairo_stroke_preserve (cr);
70
71       cairo_set_line_width (cr, 1.0);
72       cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.8);
73       cairo_stroke (cr);
74     }
75   else
76     {
77       cairo_set_line_width (cr, 1.0);
78       cairo_set_source_rgba (cr, 0.8, 0.8, 0.8, 0.8);
79       cairo_stroke (cr);
80     }
81
82   return FALSE;
83 }
84
85 static void
86 create_sv_surface (GtkColorPlane *plane)
87 {
88   GtkWidget *widget = GTK_WIDGET (plane);
89   cairo_t *cr;
90   cairo_surface_t *surface;
91   gint width, height, stride;
92   cairo_surface_t *tmp;
93   guint red, green, blue;
94   guint32 *data, *p;
95   gdouble h, s, v;
96   gdouble r, g, b;
97   gdouble sf, vf;
98   gint x, y;
99
100   if (!gtk_widget_get_realized (widget))
101     return;
102
103   width = gtk_widget_get_allocated_width (widget);
104   height = gtk_widget_get_allocated_height (widget);
105
106   surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
107                                                CAIRO_CONTENT_COLOR,
108                                                width, height);
109
110   if (plane->priv->surface)
111     cairo_surface_destroy (plane->priv->surface);
112   plane->priv->surface = surface;
113
114   if (width == 1 || height == 1)
115     return;
116
117   stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
118
119   data = g_malloc (4 * height * stride);
120
121   h = plane->priv->h;
122   sf = 1.0 / (height - 1);
123   vf = 1.0 / (width - 1);
124   for (y = 0; y < height; y++)
125     {
126       s = CLAMP (1.0 - y * sf, 0.0, 1.0);
127       p = data + y * (stride / 4);
128       for (x = 0; x < width; x++)
129         {
130           v = x * vf;
131           gtk_hsv_to_rgb (h, s, v, &r, &g, &b);
132           red = CLAMP (r * 255, 0, 255);
133           green = CLAMP (g * 255, 0, 255);
134           blue = CLAMP (b * 255, 0, 255);
135           p[x] = (red << 16) | (green << 8) | blue;
136         }
137     }
138
139   tmp = cairo_image_surface_create_for_data ((guchar *)data, CAIRO_FORMAT_RGB24,
140                                              width, height, stride);
141   cr = cairo_create (surface);
142
143   cairo_set_source_surface (cr, tmp, 0, 0);
144   cairo_paint (cr);
145
146   cairo_destroy (cr);
147   cairo_surface_destroy (tmp);
148   g_free (data);
149 }
150
151 static void
152 hsv_to_xy (GtkColorPlane *plane)
153 {
154   gint width, height;
155
156   width = gtk_widget_get_allocated_width (GTK_WIDGET (plane));
157   height = gtk_widget_get_allocated_height (GTK_WIDGET (plane));
158
159   plane->priv->x = CLAMP (width * plane->priv->v, 0, width - 1);
160   plane->priv->y = CLAMP (height * (1 - plane->priv->s), 0, height - 1);
161 }
162
163 static gboolean
164 sv_configure (GtkWidget         *widget,
165               GdkEventConfigure *event)
166 {
167   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
168
169   create_sv_surface (plane);
170   hsv_to_xy (plane);
171   return TRUE;
172 }
173
174 static void
175 set_cross_grab (GtkWidget *widget,
176                 GdkDevice *device,
177                 guint32    time)
178 {
179   GdkCursor *cursor;
180
181   cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (widget)),
182                                        GDK_CROSSHAIR);
183   gdk_device_grab (device,
184                    gtk_widget_get_window (widget),
185                    GDK_OWNERSHIP_NONE,
186                    FALSE,
187                    GDK_POINTER_MOTION_MASK
188                     | GDK_POINTER_MOTION_HINT_MASK
189                     | GDK_BUTTON_RELEASE_MASK,
190                    cursor,
191                    time);
192   g_object_unref (cursor);
193 }
194
195 static gboolean
196 sv_grab_broken (GtkWidget          *widget,
197                 GdkEventGrabBroken *event)
198 {
199   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
200
201   plane->priv->in_drag = FALSE;
202
203   return TRUE;
204 }
205
206 static void
207 sv_update_color (GtkColorPlane *plane,
208                  gint           x,
209                  gint           y)
210 {
211   GtkWidget *widget = GTK_WIDGET (plane);
212
213   plane->priv->x = x;
214   plane->priv->y = y;
215
216   plane->priv->s = CLAMP (1 - y * (1.0 / gtk_widget_get_allocated_height (widget)), 0, 1);
217   plane->priv->v = CLAMP (x * (1.0 / gtk_widget_get_allocated_width (widget)), 0, 1);
218   g_signal_emit (plane, signals[CHANGED], 0);
219   gtk_widget_queue_draw (widget);
220 }
221
222 static gboolean
223 sv_button_press (GtkWidget      *widget,
224                  GdkEventButton *event)
225 {
226   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
227
228   if (plane->priv->in_drag || event->button != GDK_BUTTON_PRIMARY)
229     return FALSE;
230
231   plane->priv->in_drag = TRUE;
232   set_cross_grab (widget, gdk_event_get_device ((GdkEvent*)event), event->time);
233   sv_update_color (plane, event->x, event->y);
234   gtk_widget_grab_focus (widget);
235
236   return TRUE;
237 }
238
239 static gboolean
240 sv_button_release (GtkWidget      *widget,
241                    GdkEventButton *event)
242 {
243   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
244
245   if (!plane->priv->in_drag || event->button != GDK_BUTTON_PRIMARY)
246     return FALSE;
247
248   plane->priv->in_drag = FALSE;
249
250   sv_update_color (plane, event->x, event->y);
251   gdk_device_ungrab (gdk_event_get_device ((GdkEvent *) event), event->time);
252
253   return TRUE;
254 }
255
256 static gboolean
257 sv_motion (GtkWidget      *widget,
258            GdkEventMotion *event)
259 {
260   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
261
262   if (!plane->priv->in_drag)
263     return FALSE;
264
265   gdk_event_request_motions (event);
266   sv_update_color (plane, event->x, event->y);
267
268   return TRUE;
269 }
270
271 static void
272 sv_move (GtkColorPlane *plane,
273          gdouble        ds,
274          gdouble        dv)
275 {
276   if (plane->priv->s + ds > 1)
277     {
278       if (plane->priv->s < 1)
279         plane->priv->s = 1;
280       else
281         goto error;
282     }
283   else if (plane->priv->s + ds < 0)
284     {
285       if (plane->priv->s > 0)
286         plane->priv->s = 0;
287       else
288         goto error;
289     }
290   else
291     {
292       plane->priv->s += ds;
293     }
294
295   if (plane->priv->v + dv > 1)
296     {
297       if (plane->priv->v < 1)
298         plane->priv->v = 1;
299       else
300         goto error;
301     }
302   else if (plane->priv->v + dv < 0)
303     {
304       if (plane->priv->v > 0)
305         plane->priv->v = 0;
306       else
307         goto error;
308     }
309   else
310     {
311       plane->priv->v += dv;
312     }
313
314   hsv_to_xy (plane);
315   g_signal_emit (plane, signals[CHANGED], 0);
316
317   gtk_widget_queue_draw (GTK_WIDGET (plane));
318   return;
319
320 error:
321   gtk_widget_error_bell (GTK_WIDGET (plane));
322 }
323
324 static gboolean
325 sv_key_press (GtkWidget      *widget,
326               GdkEventKey    *event)
327 {
328   GtkColorPlane *plane = GTK_COLOR_PLANE (widget);
329   gdouble step;
330
331   /* FIXME: turn into bindings */
332   if ((event->state & GDK_MOD1_MASK) != 0)
333     step = 0.1;
334   else
335     step = 0.01;
336
337   if (event->keyval == GDK_KEY_Up ||
338       event->keyval == GDK_KEY_KP_Up)
339     sv_move (plane, step, 0);
340   else if (event->keyval == GDK_KEY_Down ||
341            event->keyval == GDK_KEY_KP_Down)
342     sv_move (plane, -step, 0);
343   else if (event->keyval == GDK_KEY_Left ||
344            event->keyval == GDK_KEY_KP_Left)
345     sv_move (plane, 0, -step);
346   else if (event->keyval == GDK_KEY_Right ||
347            event->keyval == GDK_KEY_KP_Right)
348     sv_move (plane, 0, step);
349   else
350     return FALSE;
351
352   return TRUE;
353 }
354
355 static void
356 gtk_color_plane_init (GtkColorPlane *plane)
357 {
358   plane->priv = G_TYPE_INSTANCE_GET_PRIVATE (plane,
359                                              GTK_TYPE_COLOR_PLANE,
360                                              GtkColorPlanePrivate);
361   gtk_widget_set_can_focus (GTK_WIDGET (plane), TRUE);
362   gtk_widget_set_events (GTK_WIDGET (plane), GDK_KEY_PRESS_MASK
363                                              | GDK_BUTTON_PRESS_MASK
364                                              | GDK_BUTTON_RELEASE_MASK
365                                              | GDK_POINTER_MOTION_MASK);
366 }
367
368 static void
369 sv_finalize (GObject *object)
370 {
371   GtkColorPlane *plane = GTK_COLOR_PLANE (object);
372
373   cairo_surface_destroy (plane->priv->surface);
374
375   G_OBJECT_CLASS (gtk_color_plane_parent_class)->finalize (object);
376 }
377
378 static void
379 gtk_color_plane_class_init (GtkColorPlaneClass *class)
380 {
381   GObjectClass *object_class = G_OBJECT_CLASS (class);
382   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
383
384   object_class->finalize = sv_finalize;
385
386   widget_class->draw = sv_draw;
387   widget_class->configure_event = sv_configure;
388   widget_class->button_press_event = sv_button_press;
389   widget_class->button_release_event = sv_button_release;
390   widget_class->motion_notify_event = sv_motion;
391   widget_class->grab_broken_event = sv_grab_broken;
392   widget_class->key_press_event = sv_key_press;
393
394   signals[CHANGED] =
395     g_signal_new ("changed",
396                   GTK_TYPE_COLOR_PLANE,
397                   G_SIGNAL_RUN_FIRST,
398                   G_STRUCT_OFFSET (GtkColorPlaneClass, changed),
399                   NULL, NULL,
400                   NULL,
401                   G_TYPE_NONE, 0);
402
403   g_type_class_add_private (class, sizeof (GtkColorPlanePrivate));
404 }
405
406 gdouble
407 gtk_color_plane_get_h (GtkColorPlane *plane)
408 {
409   return plane->priv->h;
410 }
411
412 gdouble
413 gtk_color_plane_get_s (GtkColorPlane *plane)
414 {
415   return plane->priv->s;
416 }
417
418 gdouble
419 gtk_color_plane_get_v (GtkColorPlane *plane)
420 {
421   return plane->priv->v;
422 }
423
424 void
425 gtk_color_plane_set_h (GtkColorPlane *plane,
426                        gdouble        h)
427 {
428   plane->priv->h = h;
429   create_sv_surface (plane);
430   gtk_widget_queue_draw (GTK_WIDGET (plane));
431 }
432
433 void
434 gtk_color_plane_set_s (GtkColorPlane *plane,
435                        gdouble        s)
436 {
437   plane->priv->s = s;
438   hsv_to_xy (plane);
439   gtk_widget_queue_draw (GTK_WIDGET (plane));
440 }
441
442 void
443 gtk_color_plane_set_v (GtkColorPlane *plane,
444                        gdouble        v)
445 {
446   plane->priv->v = v;
447   hsv_to_xy (plane);
448   gtk_widget_queue_draw (GTK_WIDGET (plane));
449 }
450
451 GtkWidget *
452 gtk_color_plane_new (void)
453 {
454   return (GtkWidget *) g_object_new (GTK_TYPE_COLOR_PLANE, NULL);
455 }