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