comparison src/xs_curve.c @ 358:4f247b19c9ea

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