]> Pileus Git - ~andy/gtk/blob - gtk/gtkcurve.c
( From: James Henstridge <james@daa.com.au> )
[~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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #include <stdlib.h>
19 #include <string.h>
20 #include <math.h>
21
22 #include "gtkcurve.h"
23 #include "gtkdrawingarea.h"
24 #include "gtkmain.h"
25 #include "gtkradiobutton.h"
26 #include "gtksignal.h"
27 #include "gtktable.h"
28
29 #define BOUNDS(a,x,y)   (((a) < (x)) ? (x) : (((a) > (y)) ? (y) : (a)))
30 #define RADIUS          3       /* radius of the control points */
31 #define MIN_DISTANCE    8       /* min distance between control points */
32
33 #define GRAPH_MASK      (GDK_EXPOSURE_MASK |            \
34                          GDK_POINTER_MOTION_MASK |      \
35                          GDK_POINTER_MOTION_HINT_MASK | \
36                          GDK_ENTER_NOTIFY_MASK |        \
37                          GDK_BUTTON_PRESS_MASK |        \
38                          GDK_BUTTON_RELEASE_MASK |      \
39                          GDK_BUTTON1_MOTION_MASK)
40
41 static GtkDrawingAreaClass *parent_class = NULL;
42 static guint curve_type_changed_signal = 0;
43
44
45 /* forward declarations: */
46 static void gtk_curve_class_init   (GtkCurveClass *class);
47 static void gtk_curve_init         (GtkCurve      *curve);
48 static void gtk_curve_finalize     (GtkObject     *object);
49 static gint gtk_curve_graph_events (GtkWidget     *widget, 
50                                     GdkEvent      *event, 
51                                     GtkCurve      *c);
52 static void gtk_curve_size_graph   (GtkCurve      *curve);
53
54 guint
55 gtk_curve_get_type (void)
56 {
57   static guint curve_type = 0;
58
59   if (!curve_type)
60     {
61       GtkTypeInfo curve_info =
62       {
63         "GtkCurve",
64         sizeof (GtkCurve),
65         sizeof (GtkCurveClass),
66         (GtkClassInitFunc) gtk_curve_class_init,
67         (GtkObjectInitFunc) gtk_curve_init,
68         (GtkArgSetFunc) NULL,
69         (GtkArgGetFunc) NULL,
70       };
71
72       curve_type = gtk_type_unique (gtk_drawing_area_get_type (), &curve_info);
73     }
74   return curve_type;
75 }
76
77 static void
78 gtk_curve_class_init (GtkCurveClass *class)
79 {
80   GtkObjectClass *object_class;
81
82   parent_class = gtk_type_class (gtk_drawing_area_get_type ());
83
84   object_class = (GtkObjectClass *) class;
85
86   curve_type_changed_signal =
87     gtk_signal_new ("curve_type_changed", GTK_RUN_FIRST, object_class->type,
88                     GTK_SIGNAL_OFFSET (GtkCurveClass, curve_type_changed),
89                     gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
90   gtk_object_class_add_signals (object_class, &curve_type_changed_signal, 1);
91
92   object_class->finalize = gtk_curve_finalize;
93 }
94
95 static void
96 gtk_curve_init (GtkCurve *curve)
97 {
98   gint old_mask;
99
100   curve->cursor_type = GDK_TOP_LEFT_ARROW;
101   curve->pixmap = NULL;
102   curve->curve_type = GTK_CURVE_TYPE_SPLINE;
103   curve->height = 0;
104   curve->grab_point = -1;
105
106   curve->num_points = 0;
107   curve->point = 0;
108
109   curve->num_ctlpoints = 0;
110   curve->ctlpoint = NULL;
111
112   curve->min_x = 0.0;
113   curve->max_x = 1.0;
114   curve->min_y = 0.0;
115   curve->max_y = 1.0;
116
117   old_mask = gtk_widget_get_events (GTK_WIDGET (curve));
118   gtk_widget_set_events (GTK_WIDGET (curve), old_mask | GRAPH_MASK);
119   gtk_signal_connect (GTK_OBJECT (curve), "event",
120                       (GtkSignalFunc) gtk_curve_graph_events, curve);
121   gtk_curve_size_graph (curve);
122 }
123
124 static int
125 project (gfloat value, gfloat min, gfloat max, int norm)
126 {
127   return (norm - 1) * ((value - min) / (max - min)) + 0.5;
128 }
129
130 static gfloat
131 unproject (gint value, gfloat min, gfloat max, int norm)
132 {
133   return value / (gfloat) (norm - 1) * (max - min) + min;
134 }
135
136 /* Solve the tridiagonal equation system that determines the second
137    derivatives for the interpolation points.  (Based on Numerical
138    Recipies 2nd Edition.) */
139 static void
140 spline_solve (int n, gfloat x[], gfloat y[], gfloat y2[])
141 {
142   gfloat p, sig, *u;
143   gint i, k;
144
145   u = g_malloc ((n - 1) * sizeof (u[0]));
146
147   y2[0] = u[0] = 0.0;   /* set lower boundary condition to "natural" */
148
149   for (i = 1; i < n - 1; ++i)
150     {
151       sig = (x[i] - x[i - 1]) / (x[i + 1] - x[i - 1]);
152       p = sig * y2[i - 1] + 2.0;
153       y2[i] = (sig - 1.0) / p;
154       u[i] = ((y[i + 1] - y[i])
155               / (x[i + 1] - x[i]) - (y[i] - y[i - 1]) / (x[i] - x[i - 1]));
156       u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p;
157     }
158
159   y2[n - 1] = 0.0;
160   for (k = n - 2; k >= 0; --k)
161     y2[k] = y2[k] * y2[k + 1] + u[k];
162
163   g_free (u);
164 }
165
166 static gfloat
167 spline_eval (int n, gfloat x[], gfloat y[], gfloat y2[], gfloat val)
168 {
169   gint k_lo, k_hi, k;
170   gfloat h, b, a;
171
172   /* do a binary search for the right interval: */
173   k_lo = 0; k_hi = n - 1;
174   while (k_hi - k_lo > 1)
175     {
176       k = (k_hi + k_lo) / 2;
177       if (x[k] > val)
178         k_hi = k;
179       else
180         k_lo = k;
181     }
182
183   h = x[k_hi] - x[k_lo];
184   g_assert (h > 0.0);
185
186   a = (x[k_hi] - val) / h;
187   b = (val - x[k_lo]) / h;
188   return a*y[k_lo] + b*y[k_hi] +
189     ((a*a*a - a)*y2[k_lo] + (b*b*b - b)*y2[k_hi]) * (h*h)/6.0;
190 }
191
192 static void
193 gtk_curve_interpolate (GtkCurve *c, gint width, gint height)
194 {
195   gfloat *vector;
196   int i;
197
198   vector = g_malloc (width * sizeof (vector[0]));
199
200   gtk_curve_get_vector (c, width, vector);
201
202   c->height = height;
203   if (c->num_points != width)
204     {
205       c->num_points = width;
206       if (c->point)
207         g_free (c->point);
208       c->point = g_malloc (c->num_points * sizeof (c->point[0]));
209     }
210
211   for (i = 0; i < width; ++i)
212     {
213       c->point[i].x = RADIUS + i;
214       c->point[i].y = RADIUS + height
215         - project (vector[i], c->min_y, c->max_y, height);
216     }
217 }
218
219 static void
220 gtk_curve_draw (GtkCurve *c, gint width, gint height)
221 {
222   GtkStateType state;
223   GtkStyle *style;
224   gint i;
225
226   if (!c->pixmap)
227     return;
228
229   if (c->height != height || c->num_points != width)
230     gtk_curve_interpolate (c, width, height);
231
232   state = GTK_STATE_NORMAL;
233   if (!GTK_WIDGET_IS_SENSITIVE (GTK_WIDGET (c)))
234     state = GTK_STATE_INSENSITIVE;
235
236   style = GTK_WIDGET (c)->style;
237
238   /* clear the pixmap: */
239   gdk_draw_rectangle (c->pixmap, style->bg_gc[state], TRUE,
240                       0, 0, width + RADIUS * 2, height + RADIUS * 2);
241
242   /* draw the grid lines: (XXX make more meaningful) */
243   for (i = 0; i < 5; i++)
244     {
245       gdk_draw_line (c->pixmap, style->dark_gc[state],
246                      RADIUS, i * (height / 4.0) + RADIUS,
247                      width + RADIUS, i * (height / 4.0) + RADIUS);
248       gdk_draw_line (c->pixmap, style->dark_gc[state],
249                      i * (width / 4.0) + RADIUS, RADIUS,
250                      i * (width / 4.0) + RADIUS, height + RADIUS);
251     }
252
253   gdk_draw_points (c->pixmap, style->fg_gc[state], c->point, c->num_points);
254   if (c->curve_type != GTK_CURVE_TYPE_FREE)
255     for (i = 0; i < c->num_ctlpoints; ++i)
256       {
257         gint x, y;
258
259         if (c->ctlpoint[i][0] < c->min_x)
260           continue;
261
262         x = project (c->ctlpoint[i][0], c->min_x, c->max_x,
263                      width);
264         y = height -
265           project (c->ctlpoint[i][1], c->min_y, c->max_y,
266                    height);
267
268         /* draw a bullet: */
269         gdk_draw_arc (c->pixmap, style->fg_gc[state], TRUE, x, y,
270                       RADIUS * 2, RADIUS*2, 0, 360*64);
271       }
272   gdk_draw_pixmap (GTK_WIDGET (c)->window, style->fg_gc[state], c->pixmap,
273                    0, 0, 0, 0, width + RADIUS * 2, height + RADIUS * 2);
274 }
275
276 static gint
277 gtk_curve_graph_events (GtkWidget *widget, GdkEvent *event, GtkCurve *c)
278 {
279   GdkCursorType new_type = c->cursor_type;
280   gint i, src, dst, leftbound, rightbound;
281   GdkEventButton *bevent;
282   GdkEventMotion *mevent;
283   GtkWidget *w;
284   gint tx, ty;
285   gint cx, x, y, width, height;
286   gint closest_point = 0;
287   gfloat rx, ry, min_x;
288   guint distance;
289   gint x1, x2, y1, y2;
290
291   w = GTK_WIDGET (c);
292   width = w->allocation.width - RADIUS * 2;
293   height = w->allocation.height - RADIUS * 2;
294
295   /*  get the pointer position  */
296   gdk_window_get_pointer (w->window, &tx, &ty, NULL);
297   x = BOUNDS ((tx - RADIUS), 0, width);
298   y = BOUNDS ((ty - RADIUS), 0, height);
299
300   min_x = c->min_x;
301
302   distance = ~0U;
303   for (i = 0; i < c->num_ctlpoints; ++i)
304     {
305       cx = project (c->ctlpoint[i][0], min_x, c->max_x, width);
306       if ((guint) abs (x - cx) < distance)
307         {
308           distance = abs (x - cx);
309           closest_point = i;
310         }
311     }
312
313   switch (event->type)
314     {
315     case GDK_CONFIGURE:
316       if (c->pixmap)
317         gdk_pixmap_unref (c->pixmap);
318       c->pixmap = 0;
319       /* fall through */
320     case GDK_EXPOSE:
321       if (!c->pixmap)
322         c->pixmap = gdk_pixmap_new (w->window,
323                                     w->allocation.width,
324                                     w->allocation.height, -1);
325       gtk_curve_draw (c, width, height);
326       break;
327
328     case GDK_BUTTON_PRESS:
329       gtk_grab_add (widget);
330
331       bevent = (GdkEventButton *) event;
332       new_type = GDK_TCROSS;
333
334       switch (c->curve_type)
335         {
336         case GTK_CURVE_TYPE_LINEAR:
337         case GTK_CURVE_TYPE_SPLINE:
338           if (distance > MIN_DISTANCE)
339             {
340               /* insert a new control point */
341               if (c->num_ctlpoints > 0)
342                 {
343                   cx = project (c->ctlpoint[closest_point][0], min_x,
344                                 c->max_x, width);
345                   if (x > cx)
346                     ++closest_point;
347                 }
348               ++c->num_ctlpoints;
349               c->ctlpoint =
350                 g_realloc (c->ctlpoint,
351                            c->num_ctlpoints * sizeof (*c->ctlpoint));
352               for (i = c->num_ctlpoints - 1; i > closest_point; --i)
353                 memcpy (c->ctlpoint + i, c->ctlpoint + i - 1,
354                         sizeof (*c->ctlpoint));
355             }
356           c->grab_point = closest_point;
357           c->ctlpoint[c->grab_point][0] =
358             unproject (x, min_x, c->max_x, width);
359           c->ctlpoint[c->grab_point][1] =
360             unproject (height - y, c->min_y, c->max_y, height);
361
362           gtk_curve_interpolate (c, width, height);
363           break;
364
365         case GTK_CURVE_TYPE_FREE:
366           c->point[x].x = RADIUS + x;
367           c->point[x].y = RADIUS + y;
368           c->grab_point = x;
369           c->last = y;
370           break;
371         }
372       gtk_curve_draw (c, width, height);
373       break;
374
375     case GDK_BUTTON_RELEASE:
376       gtk_grab_remove (widget);
377
378       /* delete inactive points: */
379       if (c->curve_type != GTK_CURVE_TYPE_FREE)
380         {
381           for (src = dst = 0; src < c->num_ctlpoints; ++src)
382             {
383               if (c->ctlpoint[src][0] >= min_x)
384                 {
385                   memcpy (c->ctlpoint + dst, c->ctlpoint + src,
386                           sizeof (*c->ctlpoint));
387                   ++dst;
388                 }
389             }
390           if (dst < src)
391             {
392               c->num_ctlpoints -= (src - dst);
393               if (c->num_ctlpoints <= 0)
394                 {
395                   c->num_ctlpoints = 1;
396                   c->ctlpoint[0][0] = min_x;
397                   c->ctlpoint[0][1] = c->min_y;
398                   gtk_curve_interpolate (c, width, height);
399                   gtk_curve_draw (c, width, height);
400                 }
401               c->ctlpoint =
402                 g_realloc (c->ctlpoint,
403                            c->num_ctlpoints * sizeof (*c->ctlpoint));
404             }
405         }
406       new_type = GDK_FLEUR;
407       c->grab_point = -1;
408       break;
409
410     case GDK_MOTION_NOTIFY:
411       mevent = (GdkEventMotion *) event;
412       if (mevent->is_hint)
413         {
414           mevent->x = tx;
415           mevent->y = ty;
416         }
417       switch (c->curve_type)
418         {
419         case GTK_CURVE_TYPE_LINEAR:
420         case GTK_CURVE_TYPE_SPLINE:
421           if (c->grab_point == -1)
422             {
423               /* if no point is grabbed...  */
424               if (distance <= MIN_DISTANCE)
425                 new_type = GDK_FLEUR;
426               else
427                 new_type = GDK_TCROSS;
428             }
429           else
430             {
431               /* drag the grabbed point  */
432               new_type = GDK_TCROSS;
433
434               leftbound = -MIN_DISTANCE;
435               if (c->grab_point > 0)
436                 leftbound = project (c->ctlpoint[c->grab_point - 1][0],
437                                      min_x, c->max_x, width);
438
439               rightbound = width + RADIUS * 2 + MIN_DISTANCE;
440               if (c->grab_point + 1 < c->num_ctlpoints)
441                 rightbound = project (c->ctlpoint[c->grab_point + 1][0],
442                                       min_x, c->max_x, width);
443
444               if (tx <= leftbound || tx >= rightbound
445                   || ty > height + RADIUS * 2 + MIN_DISTANCE
446                   || ty < -MIN_DISTANCE)
447                 c->ctlpoint[c->grab_point][0] = min_x - 1.0;
448               else
449                 {
450                   rx = unproject (x, min_x, c->max_x, width);
451                   ry = unproject (height - y, c->min_y, c->max_y, height);
452                   c->ctlpoint[c->grab_point][0] = rx;
453                   c->ctlpoint[c->grab_point][1] = ry;
454                 }
455               gtk_curve_interpolate (c, width, height);
456               gtk_curve_draw (c, width, height);
457             }
458           break;
459
460         case GTK_CURVE_TYPE_FREE:
461           if (c->grab_point != -1)
462             {
463               if (c->grab_point > x)
464                 {
465                   x1 = x;
466                   x2 = c->grab_point;
467                   y1 = y;
468                   y2 = c->last;
469                 }
470               else
471                 {
472                   x1 = c->grab_point;
473                   x2 = x;
474                   y1 = c->last;
475                   y2 = y;
476                 }
477
478               if (x2 != x1)
479                 for (i = x1; i <= x2; i++)
480                   {
481                     c->point[i].x = RADIUS + i;
482                     c->point[i].y = RADIUS +
483                       (y1 + ((y2 - y1) * (i - x1)) / (x2 - x1));
484                   }
485               else
486                 {
487                   c->point[x].x = RADIUS + x;
488                   c->point[x].y = RADIUS + y;
489                 }
490               c->grab_point = x;
491               c->last = y;
492               gtk_curve_draw (c, width, height);
493             }
494           if (mevent->state & GDK_BUTTON1_MASK)
495             new_type = GDK_TCROSS;
496           else
497             new_type = GDK_PENCIL;
498           break;
499         }
500       if (new_type != (GdkCursorType) c->cursor_type)
501         {
502           GdkCursor *cursor;
503
504           c->cursor_type = new_type;
505
506           cursor = gdk_cursor_new (c->cursor_type);
507           gdk_window_set_cursor (w->window, cursor);
508           gdk_cursor_destroy (cursor);
509         }
510       break;
511
512     default:
513       break;
514     }
515   return FALSE;
516 }
517
518 void
519 gtk_curve_set_curve_type (GtkCurve *c, GtkCurveType new_type)
520 {
521   gfloat rx, dx;
522   gint x, i;
523
524   if (new_type != c->curve_type)
525     {
526       gint width, height;
527
528       width  = GTK_WIDGET(c)->allocation.width - RADIUS * 2;
529       height = GTK_WIDGET(c)->allocation.height - RADIUS * 2;
530
531       if (new_type == GTK_CURVE_TYPE_FREE)
532         {
533           gtk_curve_interpolate (c, width, height);
534           c->curve_type = new_type;
535         }
536       else if (c->curve_type == GTK_CURVE_TYPE_FREE)
537         {
538           if (c->ctlpoint)
539             g_free (c->ctlpoint);
540           c->num_ctlpoints = 9;
541           c->ctlpoint = g_malloc (c->num_ctlpoints * sizeof (*c->ctlpoint));
542
543           rx = 0.0;
544           dx = (width - 1) / (gfloat) (c->num_ctlpoints - 1);
545
546           for (i = 0; i < c->num_ctlpoints; ++i, rx += dx)
547             {
548               x = (int) (rx + 0.5);
549               c->ctlpoint[i][0] =
550                 unproject (x, c->min_x, c->max_x, width);
551               c->ctlpoint[i][1] =
552                 unproject (RADIUS + height - c->point[x].y,
553                            c->min_y, c->max_y, height);
554             }
555           c->curve_type = new_type;
556           gtk_curve_interpolate (c, width, height);
557         }
558       else
559         {
560           c->curve_type = new_type;
561           gtk_curve_interpolate (c, width, height);
562         }
563       gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
564       gtk_curve_draw (c, width, height);
565     }
566 }
567
568 static void
569 gtk_curve_size_graph (GtkCurve *curve)
570 {
571   gint width, height;
572   gfloat aspect;
573
574   width  = (curve->max_x - curve->min_x) + 1;
575   height = (curve->max_y - curve->min_y) + 1;
576   aspect = width / (gfloat) height;
577   if (width > gdk_screen_width () / 4)
578     width  = gdk_screen_width () / 4;
579   if (height > gdk_screen_height () / 4)
580     height = gdk_screen_height () / 4;
581
582   if (aspect < 1.0)
583     width  = height * aspect;
584   else
585     height = width / aspect;
586
587   gtk_drawing_area_size (GTK_DRAWING_AREA (curve),
588                          width + RADIUS * 2, height + RADIUS * 2);
589 }
590
591 static void
592 gtk_curve_reset_vector (GtkCurve *curve)
593 {
594   if (curve->ctlpoint)
595     g_free (curve->ctlpoint);
596
597   curve->num_ctlpoints = 2;
598   curve->ctlpoint = g_malloc (2 * sizeof (curve->ctlpoint[0]));
599   curve->ctlpoint[0][0] = curve->min_x;
600   curve->ctlpoint[0][1] = curve->min_y;
601   curve->ctlpoint[1][0] = curve->max_x;
602   curve->ctlpoint[1][1] = curve->max_y;
603
604   if (curve->pixmap)
605     {
606       gint width, height;
607
608       width = GTK_WIDGET (curve)->allocation.width - RADIUS * 2;
609       height = GTK_WIDGET (curve)->allocation.height - RADIUS * 2;
610
611       if (curve->curve_type == GTK_CURVE_TYPE_FREE)
612         {
613           curve->curve_type = GTK_CURVE_TYPE_LINEAR;
614           gtk_curve_interpolate (curve, width, height);
615           curve->curve_type = GTK_CURVE_TYPE_FREE;
616         }
617       else
618         gtk_curve_interpolate (curve, width, height);
619       gtk_curve_draw (curve, width, height);
620     }
621 }
622
623 void
624 gtk_curve_reset (GtkCurve *c)
625 {
626   GtkCurveType old_type;
627
628   old_type = c->curve_type;
629   c->curve_type = GTK_CURVE_TYPE_SPLINE;
630   gtk_curve_reset_vector (c);
631
632   if (old_type != GTK_CURVE_TYPE_SPLINE)
633     gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
634 }
635
636 void
637 gtk_curve_set_gamma (GtkCurve *c, gfloat gamma)
638 {
639   gfloat x, one_over_gamma, height, one_over_width;
640   GtkCurveType old_type;
641   gint i;
642
643   if (c->num_points < 2)
644     return;
645
646   old_type = c->curve_type;
647   c->curve_type = GTK_CURVE_TYPE_FREE;
648
649   if (gamma <= 0)
650     one_over_gamma = 1.0;
651   else
652     one_over_gamma = 1.0 / gamma;
653   one_over_width = 1.0 / (c->num_points - 1);
654   height = c->height;
655   for (i = 0; i < c->num_points; ++i)
656     {
657       x = (gfloat) i / (c->num_points - 1);
658       c->point[i].x = RADIUS + i;
659       c->point[i].y =
660         RADIUS + (height * (1.0 - pow (x, one_over_gamma)) + 0.5);
661     }
662
663   if (old_type != GTK_CURVE_TYPE_FREE)
664     gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
665
666   gtk_curve_draw (c, c->num_points, c->height);
667 }
668
669 void
670 gtk_curve_set_range (GtkCurve *curve,
671                      gfloat min_x, gfloat max_x, gfloat min_y, gfloat max_y)
672 {
673   curve->min_x = min_x;
674   curve->max_x = max_x;
675   curve->min_y = min_y;
676   curve->max_y = max_y;
677
678   gtk_curve_size_graph (curve);
679   gtk_curve_reset_vector (curve);
680 }
681
682 void
683 gtk_curve_set_vector (GtkCurve *c, int veclen, gfloat vector[])
684 {
685   GtkCurveType old_type;
686   gfloat rx, dx, ry;
687   gint i, height;
688
689   old_type = c->curve_type;
690   c->curve_type = GTK_CURVE_TYPE_FREE;
691
692   if (c->point)
693     height = GTK_WIDGET (c)->allocation.height - RADIUS * 2;
694   else
695     {
696       height = (c->max_y - c->min_y);
697       if (height > gdk_screen_height () / 4)
698         height = gdk_screen_height () / 4;
699
700       c->height = height;
701       c->num_points = veclen;
702       c->point = g_malloc (c->num_points * sizeof (c->point[0]));
703     }
704   rx = 0;
705   dx = (veclen - 1.0) / (c->num_points - 1.0);
706
707   for (i = 0; i < c->num_points; ++i, rx += dx)
708     {
709       ry = vector[(int) (rx + 0.5)];
710       if (ry > c->max_y) ry = c->max_y;
711       if (ry < c->min_y) ry = c->min_y;
712       c->point[i].x = RADIUS + i;
713       c->point[i].y =
714         RADIUS + height - project (ry, c->min_y, c->max_y, height);
715     }
716   if (old_type != GTK_CURVE_TYPE_FREE)
717     gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
718
719   gtk_curve_draw (c, c->num_points, height);
720 }
721
722 void
723 gtk_curve_get_vector (GtkCurve *c, int veclen, gfloat vector[])
724 {
725   gfloat rx, ry, dx, dy, min_x, delta_x, *mem, *xv, *yv, *y2v, prev;
726   gint dst, i, x, next, num_active_ctlpoints = 0, first_active = -1;
727
728   min_x = c->min_x;
729
730   if (c->curve_type != GTK_CURVE_TYPE_FREE)
731     {
732       /* count active points: */
733       prev = min_x - 1.0;
734       for (i = num_active_ctlpoints = 0; i < c->num_ctlpoints; ++i)
735         if (c->ctlpoint[i][0] > prev)
736           {
737             if (first_active < 0)
738               first_active = i;
739             prev = c->ctlpoint[i][0];
740             ++num_active_ctlpoints;
741           }
742
743       /* handle degenerate case: */
744       if (num_active_ctlpoints < 2)
745         {
746           if (num_active_ctlpoints > 0)
747             ry = c->ctlpoint[first_active][1];
748           else
749             ry = c->min_y;
750           if (ry < c->min_y) ry = c->min_y;
751           if (ry > c->max_y) ry = c->max_y;
752           for (x = 0; x < veclen; ++x)
753             vector[x] = ry;
754           return;
755         }
756     }
757
758   switch (c->curve_type)
759     {
760     case GTK_CURVE_TYPE_SPLINE:
761       mem = g_malloc (3 * num_active_ctlpoints * sizeof (gfloat));
762       xv  = mem;
763       yv  = mem + num_active_ctlpoints;
764       y2v = mem + 2*num_active_ctlpoints;
765
766       prev = min_x - 1.0;
767       for (i = dst = 0; i < c->num_ctlpoints; ++i)
768         if (c->ctlpoint[i][0] > prev)
769           {
770             prev    = c->ctlpoint[i][0];
771             xv[dst] = c->ctlpoint[i][0];
772             yv[dst] = c->ctlpoint[i][1];
773             ++dst;
774           }
775
776       spline_solve (num_active_ctlpoints, xv, yv, y2v);
777
778       rx = min_x;
779       dx = (c->max_x - min_x) / (veclen - 1);
780       for (x = 0; x < veclen; ++x, rx += dx)
781         {
782           ry = spline_eval (num_active_ctlpoints, xv, yv, y2v, rx);
783           if (ry < c->min_y) ry = c->min_y;
784           if (ry > c->max_y) ry = c->max_y;
785           vector[x] = ry;
786         }
787
788       g_free (mem);
789       break;
790
791     case GTK_CURVE_TYPE_LINEAR:
792       dx = (c->max_x - min_x) / (veclen - 1);
793       rx = min_x;
794       ry = c->min_y;
795       dy = 0.0;
796       i  = first_active;
797       for (x = 0; x < veclen; ++x, rx += dx)
798         {
799           if (rx >= c->ctlpoint[i][0])
800             {
801               if (rx > c->ctlpoint[i][0])
802                 ry = c->min_y;
803               dy = 0.0;
804               next = i + 1;
805               while (next < c->num_ctlpoints
806                      && c->ctlpoint[next][0] <= c->ctlpoint[i][0])
807                 ++next;
808               if (next < c->num_ctlpoints)
809                 {
810                   delta_x = c->ctlpoint[next][0] - c->ctlpoint[i][0];
811                   dy = ((c->ctlpoint[next][1] - c->ctlpoint[i][1])
812                         / delta_x);
813                   dy *= dx;
814                   ry = c->ctlpoint[i][1];
815                   i = next;
816                 }
817             }
818           vector[x] = ry;
819           ry += dy;
820         }
821       break;
822
823     case GTK_CURVE_TYPE_FREE:
824       if (c->point)
825         {
826           rx = 0.0;
827           dx = c->num_points / (double) veclen;
828           for (x = 0; x < veclen; ++x, rx += dx)
829             vector[x] = unproject (RADIUS + c->height - c->point[(int) rx].y,
830                                    c->min_y, c->max_y,
831                                    c->height);
832         }
833       else
834         memset (vector, 0, veclen * sizeof (vector[0]));
835       break;
836     }
837 }
838
839 GtkWidget*
840 gtk_curve_new (void)
841 {
842   return gtk_type_new (gtk_curve_get_type ());
843 }
844
845 static void
846 gtk_curve_finalize (GtkObject *object)
847 {
848   GtkCurve *curve;
849
850   g_return_if_fail (object != NULL);
851   g_return_if_fail (GTK_IS_CURVE (object));
852
853   curve = GTK_CURVE (object);
854   if (curve->pixmap)
855     gdk_pixmap_unref (curve->pixmap);
856   if (curve->point)
857     g_free (curve->point);
858   if (curve->ctlpoint)
859     g_free (curve->ctlpoint);
860
861   (*GTK_OBJECT_CLASS (parent_class)->finalize) (object);
862 }