]> Pileus Git - ~andy/gtk/blob - gtk/gtkhsv.c
Use the slice allocator for many small allocations.
[~andy/gtk] / gtk / gtkhsv.c
1 /* HSV color selector for GTK+
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Authors: Simon Budig <Simon.Budig@unix-ag.org> (original code)
6  *          Federico Mena-Quintero <federico@gimp.org> (cleanup for GTK+)
7  *          Jonathan Blandford <jrb@redhat.com> (cleanup for GTK+)
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <config.h>
26 #include <math.h>
27 #include <string.h>
28 #include "gtkhsv.h"
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkbindings.h"
31 #include "gtkcontainer.h"
32 #include "gtkmarshalers.h"
33 #include "gtkintl.h"
34 #include "gtkalias.h"
35
36 /*
37  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
38  * file for a list of people on the GTK+ Team.  See the ChangeLog
39  * files for a list of changes.  These files are distributed with
40  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
41  */
42
43 /* Default width/height */
44 #define DEFAULT_SIZE 100
45
46 /* Default ring width */
47 #define DEFAULT_RING_WIDTH 10
48
49
50 /* Dragging modes */
51 typedef enum {
52   DRAG_NONE,
53   DRAG_H,
54   DRAG_SV
55 } DragMode;
56
57 /* Private part of the GtkHSV structure */
58 typedef struct {
59   /* Color value */
60   double h;
61   double s;
62   double v;
63   
64   /* Size and ring width */
65   int size;
66   int ring_width;
67   
68   /* Window for capturing events */
69   GdkWindow *window;
70   
71   /* GC for drawing */
72   GdkGC *gc;
73   
74   /* Dragging mode */
75   DragMode mode;
76
77   guint focus_on_ring : 1;
78   
79 } HSVPrivate;
80
81 \f
82
83 /* Signal IDs */
84
85 enum {
86   CHANGED,
87   MOVE,
88   LAST_SIGNAL
89 };
90
91 static void     gtk_hsv_class_init     (GtkHSVClass      *class);
92 static void     gtk_hsv_init           (GtkHSV           *hsv);
93 static void     gtk_hsv_destroy        (GtkObject        *object);
94 static void     gtk_hsv_map            (GtkWidget        *widget);
95 static void     gtk_hsv_unmap          (GtkWidget        *widget);
96 static void     gtk_hsv_realize        (GtkWidget        *widget);
97 static void     gtk_hsv_unrealize      (GtkWidget        *widget);
98 static void     gtk_hsv_size_request   (GtkWidget        *widget,
99                                         GtkRequisition   *requisition);
100 static void     gtk_hsv_size_allocate  (GtkWidget        *widget,
101                                         GtkAllocation    *allocation);
102 static gint     gtk_hsv_button_press   (GtkWidget        *widget,
103                                         GdkEventButton   *event);
104 static gint     gtk_hsv_button_release (GtkWidget        *widget,
105                                         GdkEventButton   *event);
106 static gint     gtk_hsv_motion         (GtkWidget        *widget,
107                                         GdkEventMotion   *event);
108 static gint     gtk_hsv_expose         (GtkWidget        *widget,
109                                         GdkEventExpose   *event);
110 static gboolean gtk_hsv_grab_broken    (GtkWidget          *widget,
111                                         GdkEventGrabBroken *event);
112 static gboolean gtk_hsv_focus          (GtkWidget        *widget,
113                                         GtkDirectionType  direction);
114 static void     gtk_hsv_move           (GtkHSV           *hsv,
115                                         GtkDirectionType  dir);
116
117 static guint hsv_signals[LAST_SIGNAL];
118 static GtkWidgetClass *parent_class;
119
120
121 /**
122  * gtk_hsv_get_type:
123  * @void:
124  *
125  * Registers the &GtkHSV class if necessary, and returns the type ID associated
126  * to it.
127  *
128  * Return value: The type ID of the &GtkHSV class.
129  **/
130 GType
131 gtk_hsv_get_type (void)
132 {
133   static GType hsv_type = 0;
134   
135   if (!hsv_type) {
136     static const GTypeInfo hsv_info = {
137       sizeof (GtkHSVClass),
138       NULL,             /* base_init */
139       NULL,             /* base_finalize */
140       (GClassInitFunc) gtk_hsv_class_init,
141       NULL,             /* class_finalize */
142       NULL,             /* class_data */
143       sizeof (GtkHSV),
144       0,                /* n_preallocs */
145       (GInstanceInitFunc) gtk_hsv_init,
146     };
147     
148     hsv_type = g_type_register_static (GTK_TYPE_WIDGET, I_("GtkHSV"),
149                                        &hsv_info, 0);
150   }
151   
152   return hsv_type;
153 }
154
155 /* Class initialization function for the HSV color selector */
156 static void
157 gtk_hsv_class_init (GtkHSVClass *class)
158 {
159   GtkObjectClass *object_class;
160   GtkWidgetClass *widget_class;
161   GtkHSVClass    *hsv_class;
162   GtkBindingSet  *binding_set;
163   
164   object_class = (GtkObjectClass *) class;
165   widget_class = (GtkWidgetClass *) class;
166   hsv_class = GTK_HSV_CLASS (class);
167   
168   parent_class = g_type_class_peek_parent (class);
169   
170   object_class->destroy = gtk_hsv_destroy;
171   
172   widget_class->map = gtk_hsv_map;
173   widget_class->unmap = gtk_hsv_unmap;                                      
174   widget_class->realize = gtk_hsv_realize;
175   widget_class->unrealize = gtk_hsv_unrealize;
176   widget_class->size_request = gtk_hsv_size_request;
177   widget_class->size_allocate = gtk_hsv_size_allocate;
178   widget_class->button_press_event = gtk_hsv_button_press;
179   widget_class->button_release_event = gtk_hsv_button_release;
180   widget_class->motion_notify_event = gtk_hsv_motion;
181   widget_class->expose_event = gtk_hsv_expose;
182   widget_class->focus = gtk_hsv_focus;
183   widget_class->grab_broken_event = gtk_hsv_grab_broken;
184   
185   hsv_class->move = gtk_hsv_move;
186   
187   hsv_signals[CHANGED] =
188     g_signal_new (I_("changed"),
189                   G_OBJECT_CLASS_TYPE (object_class),
190                   G_SIGNAL_RUN_FIRST,
191                   G_STRUCT_OFFSET (GtkHSVClass, changed),
192                   NULL, NULL,
193                   _gtk_marshal_VOID__VOID,
194                   G_TYPE_NONE, 0);
195
196   hsv_signals[MOVE] =
197     g_signal_new (I_("move"),
198                   G_OBJECT_CLASS_TYPE (object_class),
199                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
200                   G_STRUCT_OFFSET (GtkHSVClass, move),
201                   NULL, NULL,
202                   _gtk_marshal_VOID__ENUM,
203                   G_TYPE_NONE, 1,
204                   GTK_TYPE_DIRECTION_TYPE);
205
206   binding_set = gtk_binding_set_by_class (class);
207
208   gtk_binding_entry_add_signal (binding_set, GDK_Up, 0,
209                                 "move", 1,
210                                 G_TYPE_ENUM, GTK_DIR_UP);
211   gtk_binding_entry_add_signal (binding_set, GDK_KP_Up, 0,
212                                 "move", 1,
213                                 G_TYPE_ENUM, GTK_DIR_UP);
214   
215   gtk_binding_entry_add_signal (binding_set, GDK_Down, 0,
216                                 "move", 1,
217                                 G_TYPE_ENUM, GTK_DIR_DOWN);
218   gtk_binding_entry_add_signal (binding_set, GDK_KP_Down, 0,
219                                 "move", 1,
220                                 G_TYPE_ENUM, GTK_DIR_DOWN);
221
222   
223   gtk_binding_entry_add_signal (binding_set, GDK_Right, 0,
224                                 "move", 1,
225                                 G_TYPE_ENUM, GTK_DIR_RIGHT);
226   gtk_binding_entry_add_signal (binding_set, GDK_KP_Right, 0,
227                                 "move", 1,
228                                 G_TYPE_ENUM, GTK_DIR_RIGHT);
229   
230   gtk_binding_entry_add_signal (binding_set, GDK_Left, 0,
231                                 "move", 1,
232                                 G_TYPE_ENUM, GTK_DIR_LEFT);
233   gtk_binding_entry_add_signal (binding_set, GDK_KP_Left, 0,
234                                 "move", 1,
235                                 G_TYPE_ENUM, GTK_DIR_LEFT);
236 }
237
238 /* Object initialization function for the HSV color selector */
239 static void
240 gtk_hsv_init (GtkHSV *hsv)
241 {
242   HSVPrivate *priv;
243   
244   priv = g_new0 (HSVPrivate, 1);
245   hsv->priv = priv;
246   
247   GTK_WIDGET_SET_FLAGS (hsv, GTK_NO_WINDOW);
248   GTK_WIDGET_SET_FLAGS (hsv, GTK_CAN_FOCUS);
249   
250   priv->h = 0.0;
251   priv->s = 0.0;
252   priv->v = 0.0;
253   
254   priv->size = DEFAULT_SIZE;
255   priv->ring_width = DEFAULT_RING_WIDTH;
256 }
257
258 /* Destroy handler for the HSV color selector */
259 static void
260 gtk_hsv_destroy (GtkObject *object)
261 {
262   GtkHSV *hsv;
263   
264   g_return_if_fail (GTK_IS_HSV (object));
265   
266   hsv = GTK_HSV (object);
267
268   if (hsv->priv)
269     {
270       g_free (hsv->priv);
271       hsv->priv = NULL;
272     }
273
274   GTK_OBJECT_CLASS (parent_class)->destroy (object);
275 }
276
277 /* Default signal handlers */
278
279     
280 /* Map handler for the HSV color selector */
281
282 static void
283 gtk_hsv_map (GtkWidget *widget)
284 {
285   GtkHSV *hsv;
286   HSVPrivate *priv;
287
288   hsv = GTK_HSV (widget);
289   priv = hsv->priv;
290
291   GTK_WIDGET_CLASS (parent_class)->map (widget);
292
293   gdk_window_show (priv->window);
294 }
295
296 /* Unmap handler for the HSV color selector */
297
298 static void
299 gtk_hsv_unmap (GtkWidget *widget)
300 {
301   GtkHSV *hsv;
302   HSVPrivate *priv;
303
304   hsv = GTK_HSV (widget);
305   priv = hsv->priv;
306
307   gdk_window_hide (priv->window);
308
309   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
310 }                                                                           
311                                       
312 /* Realize handler for the HSV color selector */
313 static void
314 gtk_hsv_realize (GtkWidget *widget)
315 {
316   GtkHSV *hsv;
317   HSVPrivate *priv;
318   GdkWindowAttr attr;
319   int attr_mask;
320   GdkWindow *parent_window;
321   
322   hsv = GTK_HSV (widget);
323   priv = hsv->priv;
324   
325   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
326   
327   /* Create window */
328   
329   attr.window_type = GDK_WINDOW_CHILD;
330   attr.x = widget->allocation.x;
331   attr.y = widget->allocation.y;
332   attr.width = widget->allocation.width;
333   attr.height = widget->allocation.height;
334   attr.wclass = GDK_INPUT_ONLY;
335   attr.event_mask = gtk_widget_get_events (widget);
336   attr.event_mask |= (GDK_KEY_PRESS_MASK
337                       | GDK_BUTTON_PRESS_MASK
338                       | GDK_BUTTON_RELEASE_MASK
339                       | GDK_POINTER_MOTION_MASK
340                       | GDK_ENTER_NOTIFY_MASK
341                       | GDK_LEAVE_NOTIFY_MASK);
342   
343   attr_mask = GDK_WA_X | GDK_WA_Y;
344   
345   parent_window = gtk_widget_get_parent_window (widget);
346   
347   widget->window = parent_window;
348   g_object_ref (widget->window);
349   
350   priv->window = gdk_window_new (parent_window, &attr, attr_mask);
351   gdk_window_set_user_data (priv->window, hsv);
352   
353   widget->style = gtk_style_attach (widget->style, widget->window);
354   
355   /* Create GC */
356   
357   priv->gc = gdk_gc_new (parent_window);
358 }
359
360 /* Unrealize handler for the HSV color selector */
361 static void
362 gtk_hsv_unrealize (GtkWidget *widget)
363 {
364   GtkHSV *hsv;
365   HSVPrivate *priv;
366   
367   hsv = GTK_HSV (widget);
368   priv = hsv->priv;
369   
370   gdk_window_set_user_data (priv->window, NULL);
371   gdk_window_destroy (priv->window);
372   priv->window = NULL;
373   
374   g_object_unref (priv->gc);
375   priv->gc = NULL;
376   
377   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
378     GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
379 }
380
381 /* Size_request handler for the HSV color selector */
382 static void
383 gtk_hsv_size_request (GtkWidget      *widget,
384                       GtkRequisition *requisition)
385 {
386   GtkHSV *hsv = GTK_HSV (widget);
387   HSVPrivate *priv = hsv->priv;
388   gint focus_width;
389   gint focus_pad;
390
391   gtk_widget_style_get (widget,
392                         "focus-line-width", &focus_width,
393                         "focus-padding", &focus_pad,
394                         NULL);
395   
396   requisition->width = priv->size + 2 * (focus_width + focus_pad);
397   requisition->height = priv->size + 2 * (focus_width + focus_pad);
398 }
399
400 /* Size_allocate handler for the HSV color selector */
401 static void
402 gtk_hsv_size_allocate (GtkWidget     *widget,
403                        GtkAllocation *allocation)
404 {
405   GtkHSV *hsv;
406   HSVPrivate *priv;
407   
408   hsv = GTK_HSV (widget);
409   priv = hsv->priv;
410   
411   widget->allocation = *allocation;
412   
413   if (GTK_WIDGET_REALIZED (widget))
414     gdk_window_move_resize (priv->window,
415                             allocation->x,
416                             allocation->y,
417                             allocation->width,
418                             allocation->height);
419 }
420
421
422 /* Utility functions */
423
424 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
425
426 /* Converts from HSV to RGB */
427 static void
428 hsv_to_rgb (gdouble *h,
429             gdouble *s,
430             gdouble *v)
431 {
432   gdouble hue, saturation, value;
433   gdouble f, p, q, t;
434   
435   if (*s == 0.0)
436     {
437       *h = *v;
438       *s = *v;
439       *v = *v; /* heh */
440     }
441   else
442     {
443       hue = *h * 6.0;
444       saturation = *s;
445       value = *v;
446       
447       if (hue == 6.0)
448         hue = 0.0;
449       
450       f = hue - (int) hue;
451       p = value * (1.0 - saturation);
452       q = value * (1.0 - saturation * f);
453       t = value * (1.0 - saturation * (1.0 - f));
454       
455       switch ((int) hue)
456         {
457         case 0:
458           *h = value;
459           *s = t;
460           *v = p;
461           break;
462           
463         case 1:
464           *h = q;
465           *s = value;
466           *v = p;
467           break;
468           
469         case 2:
470           *h = p;
471           *s = value;
472           *v = t;
473           break;
474           
475         case 3:
476           *h = p;
477           *s = q;
478           *v = value;
479           break;
480           
481         case 4:
482           *h = t;
483           *s = p;
484           *v = value;
485           break;
486           
487         case 5:
488           *h = value;
489           *s = p;
490           *v = q;
491           break;
492           
493         default:
494           g_assert_not_reached ();
495         }
496     }
497 }
498
499 /* Converts from RGB to HSV */
500 static void
501 rgb_to_hsv (gdouble *r,
502             gdouble *g,
503             gdouble *b)
504 {
505   gdouble red, green, blue;
506   gdouble h, s, v;
507   gdouble min, max;
508   gdouble delta;
509   
510   red = *r;
511   green = *g;
512   blue = *b;
513   
514   h = 0.0;
515   
516   if (red > green)
517     {
518       if (red > blue)
519         max = red;
520       else
521         max = blue;
522       
523       if (green < blue)
524         min = green;
525       else
526         min = blue;
527     }
528   else
529     {
530       if (green > blue)
531         max = green;
532       else
533         max = blue;
534       
535       if (red < blue)
536         min = red;
537       else
538         min = blue;
539     }
540   
541   v = max;
542   
543   if (max != 0.0)
544     s = (max - min) / max;
545   else
546     s = 0.0;
547   
548   if (s == 0.0)
549     h = 0.0;
550   else
551     {
552       delta = max - min;
553       
554       if (red == max)
555         h = (green - blue) / delta;
556       else if (green == max)
557         h = 2 + (blue - red) / delta;
558       else if (blue == max)
559         h = 4 + (red - green) / delta;
560       
561       h /= 6.0;
562       
563       if (h < 0.0)
564         h += 1.0;
565       else if (h > 1.0)
566         h -= 1.0;
567     }
568   
569   *r = h;
570   *g = s;
571   *b = v;
572 }
573
574 /* Computes the vertices of the saturation/value triangle */
575 static void
576 compute_triangle (GtkHSV *hsv,
577                   gint   *hx,
578                   gint   *hy,
579                   gint   *sx,
580                   gint   *sy,
581                   gint   *vx,
582                   gint   *vy)
583 {
584   HSVPrivate *priv;
585   gdouble center;
586   gdouble inner, outer;
587   gdouble angle;
588   
589   priv = hsv->priv;
590   
591   center = GTK_WIDGET (hsv)->requisition.width / 2.0;
592   outer = priv->size / 2.0;
593   inner = outer - priv->ring_width;
594   angle = priv->h * 2.0 * G_PI;
595   
596   *hx = floor (center + cos (angle) * inner + 0.5);
597   *hy = floor (center - sin (angle) * inner + 0.5);
598   *sx = floor (center + cos (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
599   *sy = floor (center - sin (angle + 2.0 * G_PI / 3.0) * inner + 0.5);
600   *vx = floor (center + cos (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
601   *vy = floor (center - sin (angle + 4.0 * G_PI / 3.0) * inner + 0.5);
602 }
603
604 /* Computes whether a point is inside the hue ring */
605 static gboolean
606 is_in_ring (GtkHSV *hsv,
607             gdouble x,
608             gdouble y)
609 {
610   HSVPrivate *priv;
611   gdouble dx, dy, dist;
612   gdouble center, inner, outer;
613   
614   priv = hsv->priv;
615   
616   center = priv->size / 2.0;
617   outer = priv->size / 2.0;
618   inner = outer - priv->ring_width;
619   
620   dx = x - center;
621   dy = center - y;
622   dist = dx * dx + dy * dy;
623   
624   return (dist >= inner * inner && dist <= outer * outer);
625 }
626
627 /* Computes a saturation/value pair based on the mouse coordinates */
628 static void
629 compute_sv (GtkHSV  *hsv,
630             gdouble  x,
631             gdouble  y,
632             gdouble *s,
633             gdouble *v)
634 {
635   int ihx, ihy, isx, isy, ivx, ivy;
636   double hx, hy, sx, sy, vx, vy;
637   double center;
638   
639   compute_triangle (hsv, &ihx, &ihy, &isx, &isy, &ivx, &ivy);
640   center = GTK_WIDGET (hsv)->requisition.width / 2.0;
641   hx = ihx - center;
642   hy = center - ihy;
643   sx = isx - center;
644   sy = center - isy;
645   vx = ivx - center;
646   vy = center - ivy;
647   x -= center;
648   y = center - y;
649   
650   if (vx * (x - sx) + vy * (y - sy) < 0.0)
651     {
652       *s = 1.0;
653       *v = (((x - sx) * (hx - sx) + (y - sy) * (hy-sy))
654             / ((hx - sx) * (hx - sx) + (hy - sy) * (hy - sy)));
655       
656       if (*v < 0.0)
657         *v = 0.0;
658       else if (*v > 1.0)
659         *v = 1.0;
660     }
661   else if (hx * (x - sx) + hy * (y - sy) < 0.0)
662     {
663       *s = 0.0;
664       *v = (((x - sx) * (vx - sx) + (y - sy) * (vy - sy))
665             / ((vx - sx) * (vx - sx) + (vy - sy) * (vy - sy)));
666       
667       if (*v < 0.0)
668         *v = 0.0;
669       else if (*v > 1.0)
670         *v = 1.0;
671     }
672   else if (sx * (x - hx) + sy * (y - hy) < 0.0)
673     {
674       *v = 1.0;
675       *s = (((x - vx) * (hx - vx) + (y - vy) * (hy - vy)) /
676             ((hx - vx) * (hx - vx) + (hy - vy) * (hy - vy)));
677       
678       if (*s < 0.0)
679         *s = 0.0;
680       else if (*s > 1.0)
681         *s = 1.0;
682     }
683   else
684     {
685       *v = (((x - sx) * (hy - vy) - (y - sy) * (hx - vx))
686             / ((vx - sx) * (hy - vy) - (vy - sy) * (hx - vx)));
687       
688       if (*v<= 0.0)
689         {
690           *v = 0.0;
691           *s = 0.0;
692         }
693       else
694         {
695           if (*v > 1.0)
696             *v = 1.0;
697
698           if (fabs (hy - vy) < fabs (hx - vx))
699             *s = (x - sx - *v * (vx - sx)) / (*v * (hx - vx));
700           else
701             *s = (y - sy - *v * (vy - sy)) / (*v * (hy - vy));
702             
703           if (*s < 0.0)
704             *s = 0.0;
705           else if (*s > 1.0)
706             *s = 1.0;
707         }
708     }
709 }
710
711 /* Computes whether a point is inside the saturation/value triangle */
712 static gboolean
713 is_in_triangle (GtkHSV *hsv,
714                 gdouble x,
715                 gdouble y)
716 {
717   int hx, hy, sx, sy, vx, vy;
718   double det, s, v;
719   
720   compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
721   
722   det = (vx - sx) * (hy - sy) - (vy - sy) * (hx - sx);
723   
724   s = ((x - sx) * (hy - sy) - (y - sy) * (hx - sx)) / det;
725   v = ((vx - sx) * (y - sy) - (vy - sy) * (x - sx)) / det;
726   
727   return (s >= 0.0 && v >= 0.0 && s + v <= 1.0);
728 }
729
730 /* Computes a value based on the mouse coordinates */
731 static double
732 compute_v (GtkHSV *hsv,
733            gdouble x,
734            gdouble y)
735 {
736   double center;
737   double dx, dy;
738   double angle;
739   
740   center = GTK_WIDGET (hsv)->requisition.width / 2.0;
741   dx = x - center;
742   dy = center - y;
743   
744   angle = atan2 (dy, dx);
745   if (angle < 0.0)
746     angle += 2.0 * G_PI;
747   
748   return angle / (2.0 * G_PI);
749 }
750
751 /* Event handlers */
752
753 static void
754 set_cross_grab (GtkHSV *hsv,
755                 guint32 time)
756 {
757   HSVPrivate *priv;
758   GdkCursor *cursor;
759   
760   priv = hsv->priv;
761   
762   cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (hsv)),
763                                        GDK_CROSSHAIR);
764   gdk_pointer_grab (priv->window, FALSE,
765                     (GDK_POINTER_MOTION_MASK
766                      | GDK_POINTER_MOTION_HINT_MASK
767                      | GDK_BUTTON_RELEASE_MASK),
768                     NULL,
769                     cursor,
770                     time);
771   gdk_cursor_unref (cursor);
772 }
773
774 static gboolean 
775 gtk_hsv_grab_broken (GtkWidget          *widget,
776                      GdkEventGrabBroken *event)
777 {
778   GtkHSV *hsv = GTK_HSV (widget);
779   HSVPrivate *priv;
780   
781   priv = hsv->priv;
782   
783   priv->mode = DRAG_NONE;
784   
785   return TRUE;
786 }
787
788 /* Button_press_event handler for the HSV color selector */
789 static gint
790 gtk_hsv_button_press (GtkWidget      *widget,
791                       GdkEventButton *event)
792 {
793   GtkHSV *hsv;
794   HSVPrivate *priv;
795   double x, y;
796   
797   hsv = GTK_HSV (widget);
798   priv = hsv->priv;
799   
800   if (priv->mode != DRAG_NONE || event->button != 1)
801     return FALSE;
802   
803   x = event->x;
804   y = event->y;
805   
806   if (is_in_ring (hsv, x, y))
807     {
808       priv->mode = DRAG_H;
809       set_cross_grab (hsv, event->time);
810       
811       gtk_hsv_set_color (hsv,
812                          compute_v (hsv, x, y),
813                          priv->s,
814                          priv->v);
815
816       gtk_widget_grab_focus (widget);
817       priv->focus_on_ring = TRUE;
818       
819       return TRUE;
820     }
821   
822   if (is_in_triangle (hsv, x, y))
823     {
824       gdouble s, v;
825       
826       priv->mode = DRAG_SV;
827       set_cross_grab (hsv, event->time);
828       
829       compute_sv (hsv, x, y, &s, &v);
830       gtk_hsv_set_color (hsv, priv->h, s, v);
831
832       gtk_widget_grab_focus (widget);
833       priv->focus_on_ring = FALSE;
834       
835       return TRUE;
836     }
837   
838   return FALSE;
839 }
840
841 /* Button_release_event handler for the HSV color selector */
842 static gint
843 gtk_hsv_button_release (GtkWidget      *widget,
844                         GdkEventButton *event)
845 {
846   GtkHSV *hsv;
847   HSVPrivate *priv;
848   DragMode mode;
849   gdouble x, y;
850   
851   hsv = GTK_HSV (widget);
852   priv = hsv->priv;
853   
854   if (priv->mode == DRAG_NONE || event->button != 1)
855     return FALSE;
856   
857   /* Set the drag mode to DRAG_NONE so that signal handlers for "catched"
858    * can see that this is the final color state.
859    */
860   
861   mode = priv->mode;
862   priv->mode = DRAG_NONE;
863   
864   x = event->x;
865   y = event->y;
866   
867   if (mode == DRAG_H)
868     gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
869   else if (mode == DRAG_SV) {
870     double s, v;
871     
872     compute_sv (hsv, x, y, &s, &v);
873     gtk_hsv_set_color (hsv, priv->h, s, v);
874   } else
875     g_assert_not_reached ();
876   
877   gdk_display_pointer_ungrab (gdk_drawable_get_display (event->window),
878                               event->time);
879   return TRUE;
880 }
881
882 /* Motion_notify_event handler for the HSV color selector */
883 static gint
884 gtk_hsv_motion (GtkWidget      *widget,
885                 GdkEventMotion *event)
886 {
887   GtkHSV *hsv;
888   HSVPrivate *priv;
889   double x, y;
890   gint ix, iy;
891   GdkModifierType mods;
892   
893   hsv = GTK_HSV (widget);
894   priv = hsv->priv;
895   
896   if (priv->mode == DRAG_NONE)
897     return FALSE;
898   
899   if (event->is_hint)
900     {
901       gdk_window_get_pointer (priv->window, &ix, &iy, &mods);
902       x = ix;
903       y = iy;
904     }
905   else
906     {
907       x = event->x;
908       y = event->y;
909     }
910   
911   if (priv->mode == DRAG_H)
912     {
913       gtk_hsv_set_color (hsv, compute_v (hsv, x, y), priv->s, priv->v);
914       return TRUE;
915     }
916   else if (priv->mode == DRAG_SV)
917     {
918       double s, v;
919       
920       compute_sv (hsv, x, y, &s, &v);
921       gtk_hsv_set_color (hsv, priv->h, s, v);
922       return TRUE;
923     }
924   
925   g_assert_not_reached ();
926   return FALSE;
927 }
928
929
930 /* Redrawing */
931
932 /* Paints the hue ring */
933 static void
934 paint_ring (GtkHSV      *hsv,
935             cairo_t     *cr,
936             gint         x,
937             gint         y,
938             gint         width,
939             gint         height)
940 {
941   GtkWidget *widget = GTK_WIDGET (hsv);
942   HSVPrivate *priv;
943   int xx, yy;
944   gdouble dx, dy, dist;
945   gdouble center;
946   gdouble inner, outer;
947   guint32 *buf, *p;
948   gdouble angle;
949   gdouble hue;
950   gdouble r, g, b;
951   cairo_surface_t *source;
952   cairo_t *source_cr;
953   gint focus_width;
954   gint focus_pad;
955
956   gtk_widget_style_get (widget,
957                         "focus-line-width", &focus_width,
958                         "focus-padding", &focus_pad,
959                         NULL);
960   
961   priv = hsv->priv;
962   
963   center = widget->requisition.width / 2.0;
964   
965   outer = priv->size / 2.0;
966   inner = outer - priv->ring_width;
967   
968   /* Create an image initialized with the ring colors */
969   
970   buf = g_new (guint32, width * height);
971   
972   for (yy = 0; yy < height; yy++)
973     {
974       p = buf + yy * width;
975       
976       dy = -(yy + y - center);
977       
978       for (xx = 0; xx < width; xx++)
979         {
980           dx = xx + x - center;
981           
982           dist = dx * dx + dy * dy;
983           if (dist < ((inner-1) * (inner-1)) || dist > ((outer+1) * (outer+1)))
984             {
985               *p++ = 0;
986               continue;
987             }
988           
989           angle = atan2 (dy, dx);
990           if (angle < 0.0)
991             angle += 2.0 * G_PI;
992           
993           hue = angle / (2.0 * G_PI);
994           
995           r = hue;
996           g = 1.0;
997           b = 1.0;
998           hsv_to_rgb (&r, &g, &b);
999           
1000           *p++ = (((int)floor (r * 255 + 0.5) << 16) |
1001                   ((int)floor (g * 255 + 0.5) << 8) |
1002                   (int)floor (b * 255 + 0.5));
1003         }
1004     }
1005
1006   source = cairo_image_surface_create_for_data ((char *)buf,
1007                                                 CAIRO_FORMAT_RGB24,
1008                                                 width, height, 4 * width);
1009
1010   /* Now draw the value marker onto the source image, so that it
1011    * will get properly clipped at the edges of the ring
1012    */
1013   source_cr = cairo_create (source);
1014   
1015   r = priv->h;
1016   g = 1.0;
1017   b = 1.0;
1018   hsv_to_rgb (&r, &g, &b);
1019   
1020   if (INTENSITY (r, g, b) > 0.5)
1021     cairo_set_source_rgb (source_cr, 0., 0., 0.);
1022   else
1023     cairo_set_source_rgb (source_cr, 1., 1., 1.);
1024
1025   cairo_move_to (source_cr, -x + center, - y + center);
1026   cairo_line_to (source_cr,
1027                  -x + center + cos (priv->h * 2.0 * G_PI) * center,
1028                  -y + center - sin (priv->h * 2.0 * G_PI) * center);
1029   cairo_stroke (source_cr);
1030   cairo_destroy (source_cr);
1031
1032   /* Draw the ring using the source image */
1033
1034   cairo_save (cr);
1035     
1036   cairo_set_source_surface (cr, source, x, y);
1037   cairo_surface_destroy (source);
1038
1039   cairo_set_line_width (cr, priv->ring_width);
1040   cairo_new_path (cr);
1041   cairo_arc (cr,
1042              center, center,
1043              priv->size / 2. - priv->ring_width / 2.,
1044              0, 2 * G_PI);
1045   cairo_stroke (cr);
1046   
1047   cairo_restore (cr);
1048   
1049   g_free (buf);
1050 }
1051
1052 /* Converts an HSV triplet to an integer RGB triplet */
1053 static void
1054 get_color (gdouble h,
1055            gdouble s,
1056            gdouble v,
1057            gint   *r,
1058            gint   *g,
1059            gint   *b)
1060 {
1061   hsv_to_rgb (&h, &s, &v);
1062   
1063   *r = floor (h * 255 + 0.5);
1064   *g = floor (s * 255 + 0.5);
1065   *b = floor (v * 255 + 0.5);
1066 }
1067
1068 #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
1069
1070 #define LERP(a, b, v1, v2, i) (((v2) - (v1) != 0)                                       \
1071                                ? ((a) + ((b) - (a)) * ((i) - (v1)) / ((v2) - (v1)))     \
1072                                : (a))
1073
1074 /* Number of pixels we extend out from the edges when creating
1075  * color source to avoid artifacts
1076  */
1077 #define PAD 3
1078
1079 /* Paints the HSV triangle */
1080 static void
1081 paint_triangle (GtkHSV      *hsv,
1082                 cairo_t     *cr,
1083                 gint         x,
1084                 gint         y,
1085                 gint         width,
1086                 gint         height)
1087 {
1088   GtkWidget *widget = GTK_WIDGET (hsv);
1089   HSVPrivate *priv;
1090   gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1091   gint x1, y1, r1, g1, b1; /* First vertex in scanline order */
1092   gint x2, y2, r2, g2, b2; /* Second vertex */
1093   gint x3, y3, r3, g3, b3; /* Third vertex */
1094   gint t;
1095   guint32 *buf, *p, c;
1096   gint xl, xr, rl, rr, gl, gr, bl, br; /* Scanline data */
1097   gint xx, yy;
1098   gint x_interp, y_interp;
1099   gint x_start, x_end;
1100   cairo_surface_t *source;
1101   gdouble r, g, b;
1102   gchar *detail;
1103   
1104   priv = hsv->priv;
1105   
1106   /* Compute triangle's vertices */
1107   
1108   compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1109   
1110   x1 = hx;
1111   y1 = hy;
1112   get_color (priv->h, 1.0, 1.0, &r1, &g1, &b1);
1113   
1114   x2 = sx;
1115   y2 = sy;
1116   get_color (priv->h, 1.0, 0.0, &r2, &g2, &b2);
1117   
1118   x3 = vx;
1119   y3 = vy;
1120   get_color (priv->h, 0.0, 1.0, &r3, &g3, &b3);
1121   
1122   if (y2 > y3)
1123     {
1124       SWAP (x2, x3, t);
1125       SWAP (y2, y3, t);
1126       SWAP (r2, r3, t);
1127       SWAP (g2, g3, t);
1128       SWAP (b2, b3, t);
1129     }
1130   
1131   if (y1 > y3)
1132     {
1133       SWAP (x1, x3, t);
1134       SWAP (y1, y3, t);
1135       SWAP (r1, r3, t);
1136       SWAP (g1, g3, t);
1137       SWAP (b1, b3, t);
1138     }
1139   
1140   if (y1 > y2)
1141     {
1142       SWAP (x1, x2, t);
1143       SWAP (y1, y2, t);
1144       SWAP (r1, r2, t);
1145       SWAP (g1, g2, t);
1146       SWAP (b1, b2, t);
1147     }
1148   
1149   /* Shade the triangle */
1150   
1151   buf = g_new (guint32, width * height);
1152   
1153   for (yy = 0; yy < height; yy++)
1154     {
1155       p = buf + yy * width;
1156       
1157       if (yy + y >= y1 - PAD && yy + y < y3 + PAD) {
1158         y_interp = CLAMP (yy + y, y1, y3);
1159         
1160         if (y_interp < y2)
1161           {
1162             xl = LERP (x1, x2, y1, y2, y_interp);
1163             
1164             rl = LERP (r1, r2, y1, y2, y_interp);
1165             gl = LERP (g1, g2, y1, y2, y_interp);
1166             bl = LERP (b1, b2, y1, y2, y_interp);
1167           }
1168         else
1169           {
1170             xl = LERP (x2, x3, y2, y3, y_interp);
1171             
1172             rl = LERP (r2, r3, y2, y3, y_interp);
1173             gl = LERP (g2, g3, y2, y3, y_interp);
1174             bl = LERP (b2, b3, y2, y3, y_interp);
1175           }
1176         
1177         xr = LERP (x1, x3, y1, y3, y_interp);
1178         
1179         rr = LERP (r1, r3, y1, y3, y_interp);
1180         gr = LERP (g1, g3, y1, y3, y_interp);
1181         br = LERP (b1, b3, y1, y3, y_interp);
1182         
1183         if (xl > xr)
1184           {
1185             SWAP (xl, xr, t);
1186             SWAP (rl, rr, t);
1187             SWAP (gl, gr, t);
1188             SWAP (bl, br, t);
1189           }
1190
1191         x_start = MAX (xl - PAD, x);
1192         x_end = MIN (xr + PAD, x + width);
1193         x_start = MIN (x_start, x_end);
1194
1195         c = (rl << 16) | (gl << 8) | bl;
1196
1197         for (xx = x; xx < x_start; xx++)
1198           *p++ = c;
1199           
1200         for (; xx < x_end; xx++)
1201           {
1202             x_interp = CLAMP (xx, xl, xr);
1203                 
1204             *p++ = ((LERP (rl, rr, xl, xr, x_interp) << 16) |
1205                     (LERP (gl, gr, xl, xr, x_interp) << 8) |
1206                     LERP (bl, br, xl, xr, x_interp));
1207           }
1208
1209         c = (rr << 16) | (gr << 8) | br;
1210
1211         for (; xx < x + width; xx++)
1212           *p++ = c;
1213       }
1214     }
1215
1216   source = cairo_image_surface_create_for_data ((char *)buf,
1217                                                 CAIRO_FORMAT_RGB24,
1218                                                 width, height, 4 * width);
1219   
1220   /* Draw a triangle with the image as a source */
1221
1222   cairo_set_source_surface (cr, source, x, y);
1223   cairo_surface_destroy (source);
1224   
1225   cairo_move_to (cr, x1, y1);
1226   cairo_line_to (cr, x2, y2);
1227   cairo_line_to (cr, x3, y3);
1228   cairo_close_path (cr);
1229   cairo_fill (cr);
1230   
1231   g_free (buf);
1232   
1233   /* Draw value marker */
1234   
1235   xx = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1236   yy = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1237   
1238   r = priv->h;
1239   g = priv->s;
1240   b = priv->v;
1241   hsv_to_rgb (&r, &g, &b);
1242
1243   if (INTENSITY (r, g, b) > 0.5)
1244     {
1245       detail = "colorwheel_light";
1246       cairo_set_source_rgb (cr, 0., 0., 0.);
1247     }
1248   else
1249     {
1250       detail = "colorwheel_dark";
1251       cairo_set_source_rgb (cr, 1., 1., 1.);
1252     }
1253
1254 #define RADIUS 4
1255 #define FOCUS_RADIUS 6
1256
1257   cairo_new_path (cr);
1258   cairo_arc (cr, xx, yy, RADIUS, 0, 2 * G_PI);
1259   cairo_stroke (cr);
1260   
1261   /* Draw focus outline */
1262
1263   if (GTK_WIDGET_HAS_FOCUS (hsv) &&
1264       !priv->focus_on_ring)
1265     {
1266       gint focus_width;
1267       gint focus_pad;
1268
1269       gtk_widget_style_get (widget,
1270                             "focus-line-width", &focus_width,
1271                             "focus-padding", &focus_pad,
1272                             NULL);
1273   
1274       gtk_paint_focus (widget->style, widget->window,
1275                        GTK_WIDGET_STATE (widget),
1276                        NULL, widget, detail,
1277                        widget->allocation.x + xx - FOCUS_RADIUS - focus_width - focus_pad, 
1278                        widget->allocation.y + yy - FOCUS_RADIUS - focus_width - focus_pad, 
1279                        2 * (FOCUS_RADIUS + focus_width + focus_pad), 
1280                        2 * (FOCUS_RADIUS + focus_width + focus_pad));
1281     }
1282   
1283 }
1284
1285 /* Paints the contents of the HSV color selector */
1286 static void
1287 paint (GtkHSV      *hsv,
1288        cairo_t     *cr,
1289        gint         x,
1290        gint         y,
1291        gint         width,
1292        gint         height)
1293 {
1294   paint_ring (hsv, cr, x, y, width, height);
1295   paint_triangle (hsv, cr, x, y, width, height);
1296 }
1297
1298 /* Expose_event handler for the HSV color selector */
1299 static gint
1300 gtk_hsv_expose (GtkWidget      *widget,
1301                 GdkEventExpose *event)
1302 {
1303   GtkHSV *hsv;
1304   HSVPrivate *priv;
1305   GdkRectangle rect, dest;
1306   cairo_t *cr;
1307   
1308   hsv = GTK_HSV (widget);
1309   priv = hsv->priv;
1310   
1311   if (!(GTK_WIDGET_DRAWABLE (widget) && event->window == widget->window))
1312     return FALSE;
1313
1314   rect.x = widget->allocation.x;
1315   rect.y = widget->allocation.y;
1316   rect.width = widget->allocation.width;
1317   rect.height = widget->allocation.height;
1318   
1319   if (!gdk_rectangle_intersect (&event->area, &rect, &dest))
1320     return FALSE;
1321   
1322   cr = gdk_cairo_create (widget->window);
1323
1324   cairo_translate (cr, widget->allocation.x, widget->allocation.y);
1325   paint (hsv, cr,
1326          dest.x - widget->allocation.x,
1327          dest.y - widget->allocation.y,
1328          dest.width, dest.height);
1329   cairo_destroy (cr);
1330
1331   if (GTK_WIDGET_HAS_FOCUS (hsv) && priv->focus_on_ring)
1332     gtk_paint_focus (widget->style, widget->window,
1333                      GTK_WIDGET_STATE (widget),
1334                      &event->area, widget, NULL,
1335                      widget->allocation.x,
1336                      widget->allocation.y, 
1337                      widget->allocation.width, 
1338                      widget->allocation.height);
1339
1340   return FALSE;
1341 }
1342
1343 static gboolean
1344 gtk_hsv_focus (GtkWidget       *widget,
1345                GtkDirectionType dir)
1346 {
1347   GtkHSV *hsv;
1348   HSVPrivate *priv;
1349
1350   hsv = GTK_HSV (widget);
1351   priv = hsv->priv;
1352
1353   if (!GTK_WIDGET_HAS_FOCUS (hsv))
1354     {
1355       if (dir == GTK_DIR_TAB_BACKWARD)
1356         priv->focus_on_ring = FALSE;
1357       else
1358         priv->focus_on_ring = TRUE;
1359
1360       gtk_widget_grab_focus (GTK_WIDGET (hsv));
1361       return TRUE;
1362     }
1363   
1364   switch (dir)
1365     {
1366     case GTK_DIR_UP:
1367       if (priv->focus_on_ring)
1368         return FALSE;
1369       else
1370         priv->focus_on_ring = TRUE;
1371       break;
1372
1373     case GTK_DIR_DOWN:
1374       if (priv->focus_on_ring)
1375         priv->focus_on_ring = FALSE;
1376       else
1377         return FALSE;
1378       break;
1379
1380     case GTK_DIR_LEFT:
1381     case GTK_DIR_TAB_BACKWARD:
1382       if (priv->focus_on_ring)
1383         return FALSE;
1384       else
1385         priv->focus_on_ring = TRUE;
1386       break;
1387
1388     case GTK_DIR_RIGHT:
1389     case GTK_DIR_TAB_FORWARD:
1390       if (priv->focus_on_ring)
1391         priv->focus_on_ring = FALSE;
1392       else
1393         return FALSE;
1394       break;
1395     }
1396
1397   gtk_widget_queue_draw (GTK_WIDGET (hsv));
1398
1399   return TRUE;
1400 }
1401
1402 /**
1403  * gtk_hsv_new:
1404  * @void:
1405  *
1406  * Creates a new HSV color selector.
1407  *
1408  * Return value: A newly-created HSV color selector.
1409  **/
1410 GtkWidget*
1411 gtk_hsv_new (void)
1412 {
1413   return g_object_new (GTK_TYPE_HSV, NULL);
1414 }
1415
1416 /**
1417  * gtk_hsv_set_color:
1418  * @hsv: An HSV color selector.
1419  * @h: Hue.
1420  * @s: Saturation.
1421  * @v: Value.
1422  *
1423  * Sets the current color in an HSV color selector.  Color component values must
1424  * be in the [0.0, 1.0] range.
1425  **/
1426 void
1427 gtk_hsv_set_color (GtkHSV *hsv,
1428                    gdouble h,
1429                    gdouble s,
1430                    gdouble v)
1431 {
1432   HSVPrivate *priv;
1433   
1434   g_return_if_fail (hsv != NULL);
1435   g_return_if_fail (GTK_IS_HSV (hsv));
1436   g_return_if_fail (h >= 0.0 && h <= 1.0);
1437   g_return_if_fail (s >= 0.0 && s <= 1.0);
1438   g_return_if_fail (v >= 0.0 && v <= 1.0);
1439   
1440   priv = hsv->priv;
1441   
1442   priv->h = h;
1443   priv->s = s;
1444   priv->v = v;
1445   
1446   g_signal_emit (hsv, hsv_signals[CHANGED], 0);
1447   
1448   gtk_widget_queue_draw (GTK_WIDGET (hsv));
1449 }
1450
1451 /**
1452  * gtk_hsv_get_color:
1453  * @hsv: An HSV color selector.
1454  * @h: Return value for the hue.
1455  * @s: Return value for the saturation.
1456  * @v: Return value for the value.
1457  *
1458  * Queries the current color in an HSV color selector.  Returned values will be
1459  * in the [0.0, 1.0] range.
1460  **/
1461 void
1462 gtk_hsv_get_color (GtkHSV *hsv, double *h, double *s, double *v)
1463 {
1464   HSVPrivate *priv;
1465   
1466   g_return_if_fail (GTK_IS_HSV (hsv));
1467   
1468   priv = hsv->priv;
1469   
1470   if (h)
1471     *h = priv->h;
1472   
1473   if (s)
1474     *s = priv->s;
1475   
1476   if (v)
1477     *v = priv->v;
1478 }
1479
1480 /**
1481  * gtk_hsv_set_metrics:
1482  * @hsv: An HSV color selector.
1483  * @size: Diameter for the hue ring.
1484  * @ring_width: Width of the hue ring.
1485  *
1486  * Sets the size and ring width of an HSV color selector.
1487  **/
1488 void
1489 gtk_hsv_set_metrics (GtkHSV *hsv,
1490                      gint    size,
1491                      gint    ring_width)
1492 {
1493   HSVPrivate *priv;
1494   int same_size;
1495   
1496   g_return_if_fail (GTK_IS_HSV (hsv));
1497   g_return_if_fail (size > 0);
1498   g_return_if_fail (ring_width > 0);
1499   g_return_if_fail (2 * ring_width + 1 <= size);
1500   
1501   priv = hsv->priv;
1502   
1503   same_size = (priv->size == size);
1504   
1505   priv->size = size;
1506   priv->ring_width = ring_width;
1507   
1508   if (same_size)
1509     gtk_widget_queue_draw (GTK_WIDGET (hsv));
1510   else
1511     gtk_widget_queue_resize (GTK_WIDGET (hsv));
1512 }
1513
1514 /**
1515  * gtk_hsv_get_metrics:
1516  * @hsv: An HSV color selector.
1517  * @size: Return value for the diameter of the hue ring.
1518  * @ring_width: Return value for the width of the hue ring.
1519  *
1520  * Queries the size and ring width of an HSV color selector.
1521  **/
1522 void
1523 gtk_hsv_get_metrics (GtkHSV *hsv,
1524                      gint   *size,
1525                      gint   *ring_width)
1526 {
1527   HSVPrivate *priv;
1528   
1529   g_return_if_fail (GTK_IS_HSV (hsv));
1530   
1531   priv = hsv->priv;
1532   
1533   if (size)
1534     *size = priv->size;
1535   
1536   if (ring_width)
1537     *ring_width = priv->ring_width;
1538 }
1539
1540 /**
1541  * gtk_hsv_is_adjusting:
1542  * @hsv:
1543  *
1544  * An HSV color selector can be said to be adjusting if multiple rapid changes
1545  * are being made to its value, for example, when the user is adjusting the
1546  * value with the mouse.  This function queries whether the HSV color selector
1547  * is being adjusted or not.
1548  *
1549  * Return value: TRUE if clients can ignore changes to the color value, since
1550  * they may be transitory, or FALSE if they should consider the color value
1551  * status to be final.
1552  **/
1553 gboolean
1554 gtk_hsv_is_adjusting (GtkHSV *hsv)
1555 {
1556   HSVPrivate *priv;
1557   
1558   g_return_val_if_fail (GTK_IS_HSV (hsv), FALSE);
1559   
1560   priv = hsv->priv;
1561
1562   return priv->mode != DRAG_NONE;
1563 }
1564
1565 /**
1566  * gtk_hsv_to_rgb:
1567  * @h: Hue.
1568  * @s: Saturation.
1569  * @v: Value.
1570  * @r: Return value for the red component.
1571  * @g: Return value for the green component.
1572  * @b: Return value for the blue component.
1573  * 
1574  * Converts a color from HSV space to RGB.  Input values must be in the
1575  * [0.0, 1.0] range; output values will be in the same range.
1576  **/
1577 void
1578 gtk_hsv_to_rgb (gdouble  h,
1579                 gdouble  s,
1580                 gdouble  v,
1581                 gdouble *r,
1582                 gdouble *g,
1583                 gdouble *b)
1584 {
1585   g_return_if_fail (h >= 0.0 && h <= 1.0);
1586   g_return_if_fail (s >= 0.0 && s <= 1.0);
1587   g_return_if_fail (v >= 0.0 && v <= 1.0);
1588   
1589   hsv_to_rgb (&h, &s, &v);
1590   
1591   if (r)
1592     *r = h;
1593   
1594   if (g)
1595     *g = s;
1596   
1597   if (b)
1598     *b = v;
1599 }
1600
1601 /**
1602  * gtk_hsv_to_rgb:
1603  * @r: Red.
1604  * @g: Green.
1605  * @b: Blue.
1606  * @h: Return value for the hue component.
1607  * @s: Return value for the saturation component.
1608  * @v: Return value for the value component.
1609  * 
1610  * Converts a color from RGB space to HSV.  Input values must be in the
1611  * [0.0, 1.0] range; output values will be in the same range.
1612  **/
1613 void
1614 gtk_rgb_to_hsv (gdouble  r,
1615                 gdouble  g,
1616                 gdouble  b,
1617                 gdouble *h,
1618                 gdouble *s,
1619                 gdouble *v)
1620 {
1621   g_return_if_fail (r >= 0.0 && r <= 1.0);
1622   g_return_if_fail (g >= 0.0 && g <= 1.0);
1623   g_return_if_fail (b >= 0.0 && b <= 1.0);
1624   
1625   rgb_to_hsv (&r, &g, &b);
1626   
1627   if (h)
1628     *h = r;
1629   
1630   if (s)
1631     *s = g;
1632   
1633   if (v)
1634     *v = b;
1635 }
1636
1637 static void
1638 gtk_hsv_move (GtkHSV          *hsv,
1639               GtkDirectionType dir)
1640 {
1641   HSVPrivate *priv;
1642   gdouble hue, sat, val;
1643   gint hx, hy, sx, sy, vx, vy; /* HSV vertices */
1644   gint x, y; /* position in triangle */
1645   
1646   priv = hsv->priv;
1647
1648   hue = priv->h;
1649   sat = priv->s;
1650   val = priv->v;
1651
1652   compute_triangle (hsv, &hx, &hy, &sx, &sy, &vx, &vy);
1653
1654   x = floor (sx + (vx - sx) * priv->v + (hx - vx) * priv->s * priv->v + 0.5);
1655   y = floor (sy + (vy - sy) * priv->v + (hy - vy) * priv->s * priv->v + 0.5);
1656
1657 #define HUE_DELTA 0.002
1658   switch (dir)
1659     {
1660     case GTK_DIR_UP:
1661       if (priv->focus_on_ring)
1662         hue += HUE_DELTA;
1663       else
1664         {
1665           y -= 1;
1666           compute_sv (hsv, x, y, &sat, &val);
1667         }
1668       break;
1669
1670     case GTK_DIR_DOWN:
1671       if (priv->focus_on_ring)
1672         hue -= HUE_DELTA;
1673       else
1674         {
1675           y += 1;
1676           compute_sv (hsv, x, y, &sat, &val);
1677         }
1678       break;
1679
1680     case GTK_DIR_LEFT:
1681       if (priv->focus_on_ring)
1682         hue += HUE_DELTA;
1683       else
1684         {
1685           x -= 1;
1686           compute_sv (hsv, x, y, &sat, &val);
1687         }
1688       break;
1689
1690     case GTK_DIR_RIGHT:
1691       if (priv->focus_on_ring)
1692         hue -= HUE_DELTA
1693           ;
1694       else
1695         {
1696           x += 1;
1697           compute_sv (hsv, x, y, &sat, &val);
1698         }
1699       break;
1700
1701     default:
1702       /* we don't care about the tab directions */
1703       break;
1704     }
1705
1706   /* Wrap */
1707   if (hue < 0.0)
1708     hue = 1.0;
1709   else if (hue > 1.0)
1710     hue = 0.0;
1711   
1712   gtk_hsv_set_color (hsv, hue, sat, val);
1713 }
1714
1715 #define __GTK_HSV_C__
1716 #include "gtkaliasdef.c"