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