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