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