]> Pileus Git - ~andy/gtk/blob - gtk/gtkcurve.c
56523f845a89ad741553fe096401fdcddef5ce45
[~andy/gtk] / gtk / gtkcurve.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1997 David Mosberger
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 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <config.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <math.h>
31
32 #include "gtkalias.h"
33 #include "gtkcurve.h"
34 #include "gtkdrawingarea.h"
35 #include "gtkmain.h"
36 #include "gtkmarshalers.h"
37 #include "gtkradiobutton.h"
38 #include "gtktable.h"
39 #include "gtkintl.h"
40
41 #define RADIUS          3       /* radius of the control points */
42 #define MIN_DISTANCE    8       /* min distance between control points */
43
44 #define GRAPH_MASK      (GDK_EXPOSURE_MASK |            \
45                          GDK_POINTER_MOTION_MASK |      \
46                          GDK_POINTER_MOTION_HINT_MASK | \
47                          GDK_ENTER_NOTIFY_MASK |        \
48                          GDK_BUTTON_PRESS_MASK |        \
49                          GDK_BUTTON_RELEASE_MASK |      \
50                          GDK_BUTTON1_MOTION_MASK)
51
52 enum {
53   PROP_0,
54   PROP_CURVE_TYPE,
55   PROP_MIN_X,
56   PROP_MAX_X,
57   PROP_MIN_Y,
58   PROP_MAX_Y
59 };
60
61 static GtkDrawingAreaClass *parent_class = NULL;
62 static guint curve_type_changed_signal = 0;
63
64
65 /* forward declarations: */
66 static void gtk_curve_class_init   (GtkCurveClass *class);
67 static void gtk_curve_init         (GtkCurve      *curve);
68 static void gtk_curve_get_property  (GObject              *object,
69                                      guint                 param_id,
70                                      GValue               *value,
71                                      GParamSpec           *pspec);
72 static void gtk_curve_set_property  (GObject              *object,
73                                      guint                 param_id,
74                                      const GValue         *value,
75                                      GParamSpec           *pspec);
76 static void gtk_curve_finalize     (GObject       *object);
77 static gint gtk_curve_graph_events (GtkWidget     *widget, 
78                                     GdkEvent      *event, 
79                                     GtkCurve      *c);
80 static void gtk_curve_size_graph   (GtkCurve      *curve);
81
82 GType
83 gtk_curve_get_type (void)
84 {
85   static GType curve_type = 0;
86
87   if (!curve_type)
88     {
89       static const GTypeInfo curve_info =
90       {
91         sizeof (GtkCurveClass),
92         NULL,           /* base_init */
93         NULL,           /* base_finalize */
94         (GClassInitFunc) gtk_curve_class_init,
95         NULL,           /* class_finalize */
96         NULL,           /* class_data */
97         sizeof (GtkCurve),
98         0,              /* n_preallocs */
99         (GInstanceInitFunc) gtk_curve_init,
100       };
101
102       curve_type = g_type_register_static (GTK_TYPE_DRAWING_AREA, "GtkCurve",
103                                            &curve_info, 0);
104     }
105   return curve_type;
106 }
107
108 static void
109 gtk_curve_class_init (GtkCurveClass *class)
110 {
111   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
112   
113   parent_class = g_type_class_peek_parent (class);
114   
115   gobject_class->finalize = gtk_curve_finalize;
116
117   gobject_class->set_property = gtk_curve_set_property;
118   gobject_class->get_property = gtk_curve_get_property;
119   
120   g_object_class_install_property (gobject_class,
121                                    PROP_CURVE_TYPE,
122                                    g_param_spec_enum ("curve_type",
123                                                       P_("Curve type"),
124                                                       P_("Is this curve linear, spline interpolated, or free-form"),
125                                                       GTK_TYPE_CURVE_TYPE,
126                                                       GTK_CURVE_TYPE_LINEAR,
127                                                       G_PARAM_READABLE |
128                                                       G_PARAM_WRITABLE));
129   g_object_class_install_property (gobject_class,
130                                    PROP_MIN_X,
131                                    g_param_spec_float ("min_x",
132                                                        P_("Minimum X"),
133                                                        P_("Minimum possible value for X"),
134                                                        -G_MAXFLOAT,
135                                                        G_MAXFLOAT,
136                                                        0.0,
137                                                        G_PARAM_READABLE |
138                                                        G_PARAM_WRITABLE));
139   g_object_class_install_property (gobject_class,
140                                    PROP_MAX_X,
141                                    g_param_spec_float ("max_x",
142                                                        P_("Maximum X"),
143                                                        P_("Maximum possible X value"),
144                                                        -G_MAXFLOAT,
145                                                        G_MAXFLOAT,
146                                                        1.0,
147                                                        G_PARAM_READABLE |
148                                                        G_PARAM_WRITABLE));
149   g_object_class_install_property (gobject_class,
150                                    PROP_MIN_Y,
151                                    g_param_spec_float ("min_y",
152                                                        P_("Minimum Y"),
153                                                        P_("Minimum possible value for Y"),
154                                                        -G_MAXFLOAT,
155                                                        G_MAXFLOAT,
156                                                        0.0,
157                                                        G_PARAM_READABLE |
158                                                        G_PARAM_WRITABLE));  
159   g_object_class_install_property (gobject_class,
160                                    PROP_MAX_Y,
161                                    g_param_spec_float ("max_y",
162                                                        P_("Maximum Y"),
163                                                        P_("Maximum possible value for Y"),
164                                                        -G_MAXFLOAT,
165                                                        G_MAXFLOAT,
166                                                        1.0,
167                                                        G_PARAM_READABLE |
168                                                        G_PARAM_WRITABLE));
169
170   curve_type_changed_signal =
171     g_signal_new ("curve_type_changed",
172                    G_OBJECT_CLASS_TYPE (gobject_class),
173                    G_SIGNAL_RUN_FIRST,
174                    G_STRUCT_OFFSET (GtkCurveClass, curve_type_changed),
175                    NULL, NULL,
176                    _gtk_marshal_VOID__VOID,
177                    G_TYPE_NONE, 0);
178 }
179
180 static void
181 gtk_curve_init (GtkCurve *curve)
182 {
183   gint old_mask;
184
185   curve->cursor_type = GDK_TOP_LEFT_ARROW;
186   curve->pixmap = NULL;
187   curve->curve_type = GTK_CURVE_TYPE_SPLINE;
188   curve->height = 0;
189   curve->grab_point = -1;
190
191   curve->num_points = 0;
192   curve->point = NULL;
193
194   curve->num_ctlpoints = 0;
195   curve->ctlpoint = NULL;
196
197   curve->min_x = 0.0;
198   curve->max_x = 1.0;
199   curve->min_y = 0.0;
200   curve->max_y = 1.0;
201
202   old_mask = gtk_widget_get_events (GTK_WIDGET (curve));
203   gtk_widget_set_events (GTK_WIDGET (curve), old_mask | GRAPH_MASK);
204   g_signal_connect (curve, "event",
205                     G_CALLBACK (gtk_curve_graph_events), curve);
206   gtk_curve_size_graph (curve);
207 }
208
209 static void
210 gtk_curve_set_property (GObject              *object,
211                         guint                 prop_id,
212                         const GValue         *value,
213                         GParamSpec           *pspec)
214 {
215   GtkCurve *curve = GTK_CURVE (object);
216   
217   switch (prop_id)
218     {
219     case PROP_CURVE_TYPE:
220       gtk_curve_set_curve_type (curve, g_value_get_enum (value));
221       break;
222     case PROP_MIN_X:
223       gtk_curve_set_range (curve, g_value_get_float (value), curve->max_x,
224                            curve->min_y, curve->max_y);
225       break;
226     case PROP_MAX_X:
227       gtk_curve_set_range (curve, curve->min_x, g_value_get_float (value),
228                            curve->min_y, curve->max_y);
229       break;    
230     case PROP_MIN_Y:
231       gtk_curve_set_range (curve, curve->min_x, curve->max_x,
232                            g_value_get_float (value), curve->max_y);
233       break;
234     case PROP_MAX_Y:
235       gtk_curve_set_range (curve, curve->min_x, curve->max_x,
236                            curve->min_y, g_value_get_float (value));
237       break;
238     default:
239       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240       break;
241     }
242 }
243
244 static void
245 gtk_curve_get_property (GObject              *object,
246                         guint                 prop_id,
247                         GValue               *value,
248                         GParamSpec           *pspec)
249 {
250   GtkCurve *curve = GTK_CURVE (object);
251
252   switch (prop_id)
253     {
254     case PROP_CURVE_TYPE:
255       g_value_set_enum (value, curve->curve_type);
256       break;
257     case PROP_MIN_X:
258       g_value_set_float (value, curve->min_x);
259       break;
260     case PROP_MAX_X:
261       g_value_set_float (value, curve->max_x);
262       break;
263     case PROP_MIN_Y:
264       g_value_set_float (value, curve->min_y);
265       break;
266     case PROP_MAX_Y:
267       g_value_set_float (value, curve->max_y);
268       break;
269     default:
270       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271       break;
272     }
273 }
274
275 static int
276 project (gfloat value, gfloat min, gfloat max, int norm)
277 {
278   return (norm - 1) * ((value - min) / (max - min)) + 0.5;
279 }
280
281 static gfloat
282 unproject (gint value, gfloat min, gfloat max, int norm)
283 {
284   return value / (gfloat) (norm - 1) * (max - min) + min;
285 }
286
287 /* Solve the tridiagonal equation system that determines the second
288    derivatives for the interpolation points.  (Based on Numerical
289    Recipies 2nd Edition.) */
290 static void
291 spline_solve (int n, gfloat x[], gfloat y[], gfloat y2[])
292 {
293   gfloat p, sig, *u;
294   gint i, k;
295
296   u = g_malloc ((n - 1) * sizeof (u[0]));
297
298   y2[0] = u[0] = 0.0;   /* set lower boundary condition to "natural" */
299
300   for (i = 1; i < n - 1; ++i)
301     {
302       sig = (x[i] - x[i - 1]) / (x[i + 1] - x[i - 1]);
303       p = sig * y2[i - 1] + 2.0;
304       y2[i] = (sig - 1.0) / p;
305       u[i] = ((y[i + 1] - y[i])
306               / (x[i + 1] - x[i]) - (y[i] - y[i - 1]) / (x[i] - x[i - 1]));
307       u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p;
308     }
309
310   y2[n - 1] = 0.0;
311   for (k = n - 2; k >= 0; --k)
312     y2[k] = y2[k] * y2[k + 1] + u[k];
313
314   g_free (u);
315 }
316
317 static gfloat
318 spline_eval (int n, gfloat x[], gfloat y[], gfloat y2[], gfloat val)
319 {
320   gint k_lo, k_hi, k;
321   gfloat h, b, a;
322
323   /* do a binary search for the right interval: */
324   k_lo = 0; k_hi = n - 1;
325   while (k_hi - k_lo > 1)
326     {
327       k = (k_hi + k_lo) / 2;
328       if (x[k] > val)
329         k_hi = k;
330       else
331         k_lo = k;
332     }
333
334   h = x[k_hi] - x[k_lo];
335   g_assert (h > 0.0);
336
337   a = (x[k_hi] - val) / h;
338   b = (val - x[k_lo]) / h;
339   return a*y[k_lo] + b*y[k_hi] +
340     ((a*a*a - a)*y2[k_lo] + (b*b*b - b)*y2[k_hi]) * (h*h)/6.0;
341 }
342
343 static void
344 gtk_curve_interpolate (GtkCurve *c, gint width, gint height)
345 {
346   gfloat *vector;
347   int i;
348
349   vector = g_malloc (width * sizeof (vector[0]));
350
351   gtk_curve_get_vector (c, width, vector);
352
353   c->height = height;
354   if (c->num_points != width)
355     {
356       c->num_points = width;
357       if (c->point)
358         g_free (c->point);
359       c->point = g_malloc (c->num_points * sizeof (c->point[0]));
360     }
361
362   for (i = 0; i < width; ++i)
363     {
364       c->point[i].x = RADIUS + i;
365       c->point[i].y = RADIUS + height
366         - project (vector[i], c->min_y, c->max_y, height);
367     }
368
369   g_free (vector);
370 }
371
372 static void
373 gtk_curve_draw (GtkCurve *c, gint width, gint height)
374 {
375   GtkStateType state;
376   GtkStyle *style;
377   gint i;
378
379   if (!c->pixmap)
380     return;
381
382   if (c->height != height || c->num_points != width)
383     gtk_curve_interpolate (c, width, height);
384
385   state = GTK_STATE_NORMAL;
386   if (!GTK_WIDGET_IS_SENSITIVE (GTK_WIDGET (c)))
387     state = GTK_STATE_INSENSITIVE;
388
389   style = GTK_WIDGET (c)->style;
390
391   /* clear the pixmap: */
392   gtk_paint_flat_box (style, c->pixmap, GTK_STATE_NORMAL, GTK_SHADOW_NONE,
393                       NULL, GTK_WIDGET (c), "curve_bg",
394                       0, 0, width + RADIUS * 2, height + RADIUS * 2);
395   /* draw the grid lines: (XXX make more meaningful) */
396   for (i = 0; i < 5; i++)
397     {
398       gdk_draw_line (c->pixmap, style->dark_gc[state],
399                      RADIUS, i * (height / 4.0) + RADIUS,
400                      width + RADIUS, i * (height / 4.0) + RADIUS);
401       gdk_draw_line (c->pixmap, style->dark_gc[state],
402                      i * (width / 4.0) + RADIUS, RADIUS,
403                      i * (width / 4.0) + RADIUS, height + RADIUS);
404     }
405
406   gdk_draw_points (c->pixmap, style->fg_gc[state], c->point, c->num_points);
407   if (c->curve_type != GTK_CURVE_TYPE_FREE)
408     for (i = 0; i < c->num_ctlpoints; ++i)
409       {
410         gint x, y;
411
412         if (c->ctlpoint[i][0] < c->min_x)
413           continue;
414
415         x = project (c->ctlpoint[i][0], c->min_x, c->max_x,
416                      width);
417         y = height -
418           project (c->ctlpoint[i][1], c->min_y, c->max_y,
419                    height);
420
421         /* draw a bullet: */
422         gdk_draw_arc (c->pixmap, style->fg_gc[state], TRUE, x, y,
423                       RADIUS * 2, RADIUS*2, 0, 360*64);
424       }
425   gdk_draw_drawable (GTK_WIDGET (c)->window, style->fg_gc[state], c->pixmap,
426                      0, 0, 0, 0, width + RADIUS * 2, height + RADIUS * 2);
427 }
428
429 static gint
430 gtk_curve_graph_events (GtkWidget *widget, GdkEvent *event, GtkCurve *c)
431 {
432   GdkCursorType new_type = c->cursor_type;
433   gint i, src, dst, leftbound, rightbound;
434   GdkEventButton *bevent;
435   GdkEventMotion *mevent;
436   GtkWidget *w;
437   gint tx, ty;
438   gint cx, x, y, width, height;
439   gint closest_point = 0;
440   gfloat rx, ry, min_x;
441   guint distance;
442   gint x1, x2, y1, y2;
443   gint retval = FALSE;
444
445   w = GTK_WIDGET (c);
446   width = w->allocation.width - RADIUS * 2;
447   height = w->allocation.height - RADIUS * 2;
448
449   if ((width < 0) || (height < 0))
450     return FALSE;
451
452   /*  get the pointer position  */
453   gdk_window_get_pointer (w->window, &tx, &ty, NULL);
454   x = CLAMP ((tx - RADIUS), 0, width-1);
455   y = CLAMP ((ty - RADIUS), 0, height-1);
456
457   min_x = c->min_x;
458
459   distance = ~0U;
460   for (i = 0; i < c->num_ctlpoints; ++i)
461     {
462       cx = project (c->ctlpoint[i][0], min_x, c->max_x, width);
463       if ((guint) abs (x - cx) < distance)
464         {
465           distance = abs (x - cx);
466           closest_point = i;
467         }
468     }
469
470   switch (event->type)
471     {
472     case GDK_CONFIGURE:
473       if (c->pixmap)
474         g_object_unref (c->pixmap);
475       c->pixmap = NULL;
476       /* fall through */
477     case GDK_EXPOSE:
478       if (!c->pixmap)
479         c->pixmap = gdk_pixmap_new (w->window,
480                                     w->allocation.width,
481                                     w->allocation.height, -1);
482       gtk_curve_draw (c, width, height);
483       break;
484
485     case GDK_BUTTON_PRESS:
486       gtk_grab_add (widget);
487
488       bevent = (GdkEventButton *) event;
489       new_type = GDK_TCROSS;
490
491       switch (c->curve_type)
492         {
493         case GTK_CURVE_TYPE_LINEAR:
494         case GTK_CURVE_TYPE_SPLINE:
495           if (distance > MIN_DISTANCE)
496             {
497               /* insert a new control point */
498               if (c->num_ctlpoints > 0)
499                 {
500                   cx = project (c->ctlpoint[closest_point][0], min_x,
501                                 c->max_x, width);
502                   if (x > cx)
503                     ++closest_point;
504                 }
505               ++c->num_ctlpoints;
506               c->ctlpoint =
507                 g_realloc (c->ctlpoint,
508                            c->num_ctlpoints * sizeof (*c->ctlpoint));
509               for (i = c->num_ctlpoints - 1; i > closest_point; --i)
510                 memcpy (c->ctlpoint + i, c->ctlpoint + i - 1,
511                         sizeof (*c->ctlpoint));
512             }
513           c->grab_point = closest_point;
514           c->ctlpoint[c->grab_point][0] =
515             unproject (x, min_x, c->max_x, width);
516           c->ctlpoint[c->grab_point][1] =
517             unproject (height - y, c->min_y, c->max_y, height);
518
519           gtk_curve_interpolate (c, width, height);
520           break;
521
522         case GTK_CURVE_TYPE_FREE:
523           c->point[x].x = RADIUS + x;
524           c->point[x].y = RADIUS + y;
525           c->grab_point = x;
526           c->last = y;
527           break;
528         }
529       gtk_curve_draw (c, width, height);
530       retval = TRUE;
531       break;
532
533     case GDK_BUTTON_RELEASE:
534       gtk_grab_remove (widget);
535
536       /* delete inactive points: */
537       if (c->curve_type != GTK_CURVE_TYPE_FREE)
538         {
539           for (src = dst = 0; src < c->num_ctlpoints; ++src)
540             {
541               if (c->ctlpoint[src][0] >= min_x)
542                 {
543                   memcpy (c->ctlpoint + dst, c->ctlpoint + src,
544                           sizeof (*c->ctlpoint));
545                   ++dst;
546                 }
547             }
548           if (dst < src)
549             {
550               c->num_ctlpoints -= (src - dst);
551               if (c->num_ctlpoints <= 0)
552                 {
553                   c->num_ctlpoints = 1;
554                   c->ctlpoint[0][0] = min_x;
555                   c->ctlpoint[0][1] = c->min_y;
556                   gtk_curve_interpolate (c, width, height);
557                   gtk_curve_draw (c, width, height);
558                 }
559               c->ctlpoint =
560                 g_realloc (c->ctlpoint,
561                            c->num_ctlpoints * sizeof (*c->ctlpoint));
562             }
563         }
564       new_type = GDK_FLEUR;
565       c->grab_point = -1;
566       retval = TRUE;
567       break;
568
569     case GDK_MOTION_NOTIFY:
570       mevent = (GdkEventMotion *) event;
571
572       switch (c->curve_type)
573         {
574         case GTK_CURVE_TYPE_LINEAR:
575         case GTK_CURVE_TYPE_SPLINE:
576           if (c->grab_point == -1)
577             {
578               /* if no point is grabbed...  */
579               if (distance <= MIN_DISTANCE)
580                 new_type = GDK_FLEUR;
581               else
582                 new_type = GDK_TCROSS;
583             }
584           else
585             {
586               /* drag the grabbed point  */
587               new_type = GDK_TCROSS;
588
589               leftbound = -MIN_DISTANCE;
590               if (c->grab_point > 0)
591                 leftbound = project (c->ctlpoint[c->grab_point - 1][0],
592                                      min_x, c->max_x, width);
593
594               rightbound = width + RADIUS * 2 + MIN_DISTANCE;
595               if (c->grab_point + 1 < c->num_ctlpoints)
596                 rightbound = project (c->ctlpoint[c->grab_point + 1][0],
597                                       min_x, c->max_x, width);
598
599               if (tx <= leftbound || tx >= rightbound
600                   || ty > height + RADIUS * 2 + MIN_DISTANCE
601                   || ty < -MIN_DISTANCE)
602                 c->ctlpoint[c->grab_point][0] = min_x - 1.0;
603               else
604                 {
605                   rx = unproject (x, min_x, c->max_x, width);
606                   ry = unproject (height - y, c->min_y, c->max_y, height);
607                   c->ctlpoint[c->grab_point][0] = rx;
608                   c->ctlpoint[c->grab_point][1] = ry;
609                 }
610               gtk_curve_interpolate (c, width, height);
611               gtk_curve_draw (c, width, height);
612             }
613           break;
614
615         case GTK_CURVE_TYPE_FREE:
616           if (c->grab_point != -1)
617             {
618               if (c->grab_point > x)
619                 {
620                   x1 = x;
621                   x2 = c->grab_point;
622                   y1 = y;
623                   y2 = c->last;
624                 }
625               else
626                 {
627                   x1 = c->grab_point;
628                   x2 = x;
629                   y1 = c->last;
630                   y2 = y;
631                 }
632
633               if (x2 != x1)
634                 for (i = x1; i <= x2; i++)
635                   {
636                     c->point[i].x = RADIUS + i;
637                     c->point[i].y = RADIUS +
638                       (y1 + ((y2 - y1) * (i - x1)) / (x2 - x1));
639                   }
640               else
641                 {
642                   c->point[x].x = RADIUS + x;
643                   c->point[x].y = RADIUS + y;
644                 }
645               c->grab_point = x;
646               c->last = y;
647               gtk_curve_draw (c, width, height);
648             }
649           if (mevent->state & GDK_BUTTON1_MASK)
650             new_type = GDK_TCROSS;
651           else
652             new_type = GDK_PENCIL;
653           break;
654         }
655       if (new_type != (GdkCursorType) c->cursor_type)
656         {
657           GdkCursor *cursor;
658
659           c->cursor_type = new_type;
660
661           cursor = gdk_cursor_new_for_display (gtk_widget_get_display (w),
662                                               c->cursor_type);
663           gdk_window_set_cursor (w->window, cursor);
664           gdk_cursor_unref (cursor);
665         }
666       retval = TRUE;
667       break;
668
669     default:
670       break;
671     }
672
673   return retval;
674 }
675
676 void
677 gtk_curve_set_curve_type (GtkCurve *c, GtkCurveType new_type)
678 {
679   gfloat rx, dx;
680   gint x, i;
681
682   if (new_type != c->curve_type)
683     {
684       gint width, height;
685
686       width  = GTK_WIDGET (c)->allocation.width - RADIUS * 2;
687       height = GTK_WIDGET (c)->allocation.height - RADIUS * 2;
688
689       if (new_type == GTK_CURVE_TYPE_FREE)
690         {
691           gtk_curve_interpolate (c, width, height);
692           c->curve_type = new_type;
693         }
694       else if (c->curve_type == GTK_CURVE_TYPE_FREE)
695         {
696           if (c->ctlpoint)
697             g_free (c->ctlpoint);
698           c->num_ctlpoints = 9;
699           c->ctlpoint = g_malloc (c->num_ctlpoints * sizeof (*c->ctlpoint));
700
701           rx = 0.0;
702           dx = (width - 1) / (gfloat) (c->num_ctlpoints - 1);
703
704           for (i = 0; i < c->num_ctlpoints; ++i, rx += dx)
705             {
706               x = (int) (rx + 0.5);
707               c->ctlpoint[i][0] =
708                 unproject (x, c->min_x, c->max_x, width);
709               c->ctlpoint[i][1] =
710                 unproject (RADIUS + height - c->point[x].y,
711                            c->min_y, c->max_y, height);
712             }
713           c->curve_type = new_type;
714           gtk_curve_interpolate (c, width, height);
715         }
716       else
717         {
718           c->curve_type = new_type;
719           gtk_curve_interpolate (c, width, height);
720         }
721       g_signal_emit (c, curve_type_changed_signal, 0);
722       g_object_notify (G_OBJECT (c), "curve_type");
723       gtk_curve_draw (c, width, height);
724     }
725 }
726
727 static void
728 gtk_curve_size_graph (GtkCurve *curve)
729 {
730   gint width, height;
731   gfloat aspect;
732   GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (curve)); 
733
734   width  = (curve->max_x - curve->min_x) + 1;
735   height = (curve->max_y - curve->min_y) + 1;
736   aspect = width / (gfloat) height;
737   if (width > gdk_screen_get_width (screen) / 4)
738     width  = gdk_screen_get_width (screen) / 4;
739   if (height > gdk_screen_get_height (screen) / 4)
740     height = gdk_screen_get_height (screen) / 4;
741
742   if (aspect < 1.0)
743     width  = height * aspect;
744   else
745     height = width / aspect;
746
747   gtk_widget_set_size_request (GTK_WIDGET (curve),
748                                width + RADIUS * 2,
749                                height + RADIUS * 2);
750 }
751
752 static void
753 gtk_curve_reset_vector (GtkCurve *curve)
754 {
755   if (curve->ctlpoint)
756     g_free (curve->ctlpoint);
757
758   curve->num_ctlpoints = 2;
759   curve->ctlpoint = g_malloc (2 * sizeof (curve->ctlpoint[0]));
760   curve->ctlpoint[0][0] = curve->min_x;
761   curve->ctlpoint[0][1] = curve->min_y;
762   curve->ctlpoint[1][0] = curve->max_x;
763   curve->ctlpoint[1][1] = curve->max_y;
764
765   if (curve->pixmap)
766     {
767       gint width, height;
768
769       width = GTK_WIDGET (curve)->allocation.width - RADIUS * 2;
770       height = GTK_WIDGET (curve)->allocation.height - RADIUS * 2;
771
772       if (curve->curve_type == GTK_CURVE_TYPE_FREE)
773         {
774           curve->curve_type = GTK_CURVE_TYPE_LINEAR;
775           gtk_curve_interpolate (curve, width, height);
776           curve->curve_type = GTK_CURVE_TYPE_FREE;
777         }
778       else
779         gtk_curve_interpolate (curve, width, height);
780       gtk_curve_draw (curve, width, height);
781     }
782 }
783
784 void
785 gtk_curve_reset (GtkCurve *c)
786 {
787   GtkCurveType old_type;
788
789   old_type = c->curve_type;
790   c->curve_type = GTK_CURVE_TYPE_SPLINE;
791   gtk_curve_reset_vector (c);
792
793   if (old_type != GTK_CURVE_TYPE_SPLINE)
794     {
795        g_signal_emit (c, curve_type_changed_signal, 0);
796        g_object_notify (G_OBJECT (c), "curve_type");
797     }
798 }
799
800 void
801 gtk_curve_set_gamma (GtkCurve *c, gfloat gamma)
802 {
803   gfloat x, one_over_gamma, height, one_over_width;
804   GtkCurveType old_type;
805   gint i;
806
807   if (c->num_points < 2)
808     return;
809
810   old_type = c->curve_type;
811   c->curve_type = GTK_CURVE_TYPE_FREE;
812
813   if (gamma <= 0)
814     one_over_gamma = 1.0;
815   else
816     one_over_gamma = 1.0 / gamma;
817   one_over_width = 1.0 / (c->num_points - 1);
818   height = c->height;
819   for (i = 0; i < c->num_points; ++i)
820     {
821       x = (gfloat) i / (c->num_points - 1);
822       c->point[i].x = RADIUS + i;
823       c->point[i].y =
824         RADIUS + (height * (1.0 - pow (x, one_over_gamma)) + 0.5);
825     }
826
827   if (old_type != GTK_CURVE_TYPE_FREE)
828     g_signal_emit (c, curve_type_changed_signal, 0);
829
830   gtk_curve_draw (c, c->num_points, c->height);
831 }
832
833 void
834 gtk_curve_set_range (GtkCurve *curve,
835                      gfloat    min_x,
836                      gfloat    max_x,
837                      gfloat    min_y,
838                      gfloat    max_y)
839 {
840   g_object_freeze_notify (G_OBJECT (curve));
841   if (curve->min_x != min_x) {
842      curve->min_x = min_x;
843      g_object_notify (G_OBJECT (curve), "min_x");
844   }
845   if (curve->max_x != max_x) {
846      curve->max_x = max_x;
847      g_object_notify (G_OBJECT (curve), "max_x");
848   }
849   if (curve->min_y != min_y) {
850      curve->min_y = min_y;
851      g_object_notify (G_OBJECT (curve), "min_y");
852   }
853   if (curve->max_y != max_y) {
854      curve->max_y = max_y;
855      g_object_notify (G_OBJECT (curve), "max_y");
856   }
857   g_object_thaw_notify (G_OBJECT (curve));
858
859   gtk_curve_size_graph (curve);
860   gtk_curve_reset_vector (curve);
861 }
862
863 void
864 gtk_curve_set_vector (GtkCurve *c, int veclen, gfloat vector[])
865 {
866   GtkCurveType old_type;
867   gfloat rx, dx, ry;
868   gint i, height;
869   GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (c));
870
871   old_type = c->curve_type;
872   c->curve_type = GTK_CURVE_TYPE_FREE;
873
874   if (c->point)
875     height = GTK_WIDGET (c)->allocation.height - RADIUS * 2;
876   else
877     {
878       height = (c->max_y - c->min_y);
879       if (height > gdk_screen_get_height (screen) / 4)
880         height = gdk_screen_get_height (screen) / 4;
881
882       c->height = height;
883       c->num_points = veclen;
884       c->point = g_malloc (c->num_points * sizeof (c->point[0]));
885     }
886   rx = 0;
887   dx = (veclen - 1.0) / (c->num_points - 1.0);
888
889   for (i = 0; i < c->num_points; ++i, rx += dx)
890     {
891       ry = vector[(int) (rx + 0.5)];
892       if (ry > c->max_y) ry = c->max_y;
893       if (ry < c->min_y) ry = c->min_y;
894       c->point[i].x = RADIUS + i;
895       c->point[i].y =
896         RADIUS + height - project (ry, c->min_y, c->max_y, height);
897     }
898   if (old_type != GTK_CURVE_TYPE_FREE)
899     {
900        g_signal_emit (c, curve_type_changed_signal, 0);
901        g_object_notify (G_OBJECT (c), "curve_type");
902     }
903
904   gtk_curve_draw (c, c->num_points, height);
905 }
906
907 void
908 gtk_curve_get_vector (GtkCurve *c, int veclen, gfloat vector[])
909 {
910   gfloat rx, ry, dx, dy, min_x, delta_x, *mem, *xv, *yv, *y2v, prev;
911   gint dst, i, x, next, num_active_ctlpoints = 0, first_active = -1;
912
913   min_x = c->min_x;
914
915   if (c->curve_type != GTK_CURVE_TYPE_FREE)
916     {
917       /* count active points: */
918       prev = min_x - 1.0;
919       for (i = num_active_ctlpoints = 0; i < c->num_ctlpoints; ++i)
920         if (c->ctlpoint[i][0] > prev)
921           {
922             if (first_active < 0)
923               first_active = i;
924             prev = c->ctlpoint[i][0];
925             ++num_active_ctlpoints;
926           }
927
928       /* handle degenerate case: */
929       if (num_active_ctlpoints < 2)
930         {
931           if (num_active_ctlpoints > 0)
932             ry = c->ctlpoint[first_active][1];
933           else
934             ry = c->min_y;
935           if (ry < c->min_y) ry = c->min_y;
936           if (ry > c->max_y) ry = c->max_y;
937           for (x = 0; x < veclen; ++x)
938             vector[x] = ry;
939           return;
940         }
941     }
942
943   switch (c->curve_type)
944     {
945     case GTK_CURVE_TYPE_SPLINE:
946       mem = g_malloc (3 * num_active_ctlpoints * sizeof (gfloat));
947       xv  = mem;
948       yv  = mem + num_active_ctlpoints;
949       y2v = mem + 2*num_active_ctlpoints;
950
951       prev = min_x - 1.0;
952       for (i = dst = 0; i < c->num_ctlpoints; ++i)
953         if (c->ctlpoint[i][0] > prev)
954           {
955             prev    = c->ctlpoint[i][0];
956             xv[dst] = c->ctlpoint[i][0];
957             yv[dst] = c->ctlpoint[i][1];
958             ++dst;
959           }
960
961       spline_solve (num_active_ctlpoints, xv, yv, y2v);
962
963       rx = min_x;
964       dx = (c->max_x - min_x) / (veclen - 1);
965       for (x = 0; x < veclen; ++x, rx += dx)
966         {
967           ry = spline_eval (num_active_ctlpoints, xv, yv, y2v, rx);
968           if (ry < c->min_y) ry = c->min_y;
969           if (ry > c->max_y) ry = c->max_y;
970           vector[x] = ry;
971         }
972
973       g_free (mem);
974       break;
975
976     case GTK_CURVE_TYPE_LINEAR:
977       dx = (c->max_x - min_x) / (veclen - 1);
978       rx = min_x;
979       ry = c->min_y;
980       dy = 0.0;
981       i  = first_active;
982       for (x = 0; x < veclen; ++x, rx += dx)
983         {
984           if (rx >= c->ctlpoint[i][0])
985             {
986               if (rx > c->ctlpoint[i][0])
987                 ry = c->min_y;
988               dy = 0.0;
989               next = i + 1;
990               while (next < c->num_ctlpoints
991                      && c->ctlpoint[next][0] <= c->ctlpoint[i][0])
992                 ++next;
993               if (next < c->num_ctlpoints)
994                 {
995                   delta_x = c->ctlpoint[next][0] - c->ctlpoint[i][0];
996                   dy = ((c->ctlpoint[next][1] - c->ctlpoint[i][1])
997                         / delta_x);
998                   dy *= dx;
999                   ry = c->ctlpoint[i][1];
1000                   i = next;
1001                 }
1002             }
1003           vector[x] = ry;
1004           ry += dy;
1005         }
1006       break;
1007
1008     case GTK_CURVE_TYPE_FREE:
1009       if (c->point)
1010         {
1011           rx = 0.0;
1012           dx = c->num_points / (double) veclen;
1013           for (x = 0; x < veclen; ++x, rx += dx)
1014             vector[x] = unproject (RADIUS + c->height - c->point[(int) rx].y,
1015                                    c->min_y, c->max_y,
1016                                    c->height);
1017         }
1018       else
1019         memset (vector, 0, veclen * sizeof (vector[0]));
1020       break;
1021     }
1022 }
1023
1024 GtkWidget*
1025 gtk_curve_new (void)
1026 {
1027   return g_object_new (GTK_TYPE_CURVE, NULL);
1028 }
1029
1030 static void
1031 gtk_curve_finalize (GObject *object)
1032 {
1033   GtkCurve *curve;
1034
1035   g_return_if_fail (GTK_IS_CURVE (object));
1036
1037   curve = GTK_CURVE (object);
1038   if (curve->pixmap)
1039     g_object_unref (curve->pixmap);
1040   if (curve->point)
1041     g_free (curve->point);
1042   if (curve->ctlpoint)
1043     g_free (curve->ctlpoint);
1044
1045   G_OBJECT_CLASS (parent_class)->finalize (object);
1046 }