1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1997 David Mosberger
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.
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.
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.
24 #include "gtkdrawingarea.h"
26 #include "gtkradiobutton.h"
27 #include "gtksignal.h"
30 #define RADIUS 3 /* radius of the control points */
31 #define MIN_DISTANCE 8 /* min distance between control points */
33 #define GRAPH_MASK (GDK_EXPOSURE_MASK | \
34 GDK_POINTER_MOTION_MASK | \
35 GDK_POINTER_MOTION_HINT_MASK | \
36 GDK_ENTER_NOTIFY_MASK | \
37 GDK_BUTTON_PRESS_MASK | \
38 GDK_BUTTON_RELEASE_MASK | \
39 GDK_BUTTON1_MOTION_MASK)
41 static GtkDrawingAreaClass *parent_class = NULL;
42 static guint curve_type_changed_signal = 0;
45 /* forward declarations: */
46 static void gtk_curve_class_init (GtkCurveClass *class);
47 static void gtk_curve_init (GtkCurve *curve);
48 static void gtk_curve_finalize (GtkObject *object);
49 static gint gtk_curve_graph_events (GtkWidget *widget,
52 static void gtk_curve_size_graph (GtkCurve *curve);
55 gtk_curve_get_type (void)
57 static GtkType curve_type = 0;
61 GtkTypeInfo curve_info =
65 sizeof (GtkCurveClass),
66 (GtkClassInitFunc) gtk_curve_class_init,
67 (GtkObjectInitFunc) gtk_curve_init,
68 /* reserved_1 */ NULL,
69 /* reserved_2 */ NULL,
70 (GtkClassInitFunc) NULL,
73 curve_type = gtk_type_unique (gtk_drawing_area_get_type (), &curve_info);
79 gtk_curve_class_init (GtkCurveClass *class)
81 GtkObjectClass *object_class;
83 parent_class = gtk_type_class (gtk_drawing_area_get_type ());
85 object_class = (GtkObjectClass *) class;
87 curve_type_changed_signal =
88 gtk_signal_new ("curve_type_changed", GTK_RUN_FIRST, object_class->type,
89 GTK_SIGNAL_OFFSET (GtkCurveClass, curve_type_changed),
90 gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
91 gtk_object_class_add_signals (object_class, &curve_type_changed_signal, 1);
93 object_class->finalize = gtk_curve_finalize;
97 gtk_curve_init (GtkCurve *curve)
101 curve->cursor_type = GDK_TOP_LEFT_ARROW;
102 curve->pixmap = NULL;
103 curve->curve_type = GTK_CURVE_TYPE_SPLINE;
105 curve->grab_point = -1;
107 curve->num_points = 0;
110 curve->num_ctlpoints = 0;
111 curve->ctlpoint = NULL;
118 old_mask = gtk_widget_get_events (GTK_WIDGET (curve));
119 gtk_widget_set_events (GTK_WIDGET (curve), old_mask | GRAPH_MASK);
120 gtk_signal_connect (GTK_OBJECT (curve), "event",
121 (GtkSignalFunc) gtk_curve_graph_events, curve);
122 gtk_curve_size_graph (curve);
126 project (gfloat value, gfloat min, gfloat max, int norm)
128 return (norm - 1) * ((value - min) / (max - min)) + 0.5;
132 unproject (gint value, gfloat min, gfloat max, int norm)
134 return value / (gfloat) (norm - 1) * (max - min) + min;
137 /* Solve the tridiagonal equation system that determines the second
138 derivatives for the interpolation points. (Based on Numerical
139 Recipies 2nd Edition.) */
141 spline_solve (int n, gfloat x[], gfloat y[], gfloat y2[])
146 u = g_malloc ((n - 1) * sizeof (u[0]));
148 y2[0] = u[0] = 0.0; /* set lower boundary condition to "natural" */
150 for (i = 1; i < n - 1; ++i)
152 sig = (x[i] - x[i - 1]) / (x[i + 1] - x[i - 1]);
153 p = sig * y2[i - 1] + 2.0;
154 y2[i] = (sig - 1.0) / p;
155 u[i] = ((y[i + 1] - y[i])
156 / (x[i + 1] - x[i]) - (y[i] - y[i - 1]) / (x[i] - x[i - 1]));
157 u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p;
161 for (k = n - 2; k >= 0; --k)
162 y2[k] = y2[k] * y2[k + 1] + u[k];
168 spline_eval (int n, gfloat x[], gfloat y[], gfloat y2[], gfloat val)
173 /* do a binary search for the right interval: */
174 k_lo = 0; k_hi = n - 1;
175 while (k_hi - k_lo > 1)
177 k = (k_hi + k_lo) / 2;
184 h = x[k_hi] - x[k_lo];
187 a = (x[k_hi] - val) / h;
188 b = (val - x[k_lo]) / h;
189 return a*y[k_lo] + b*y[k_hi] +
190 ((a*a*a - a)*y2[k_lo] + (b*b*b - b)*y2[k_hi]) * (h*h)/6.0;
194 gtk_curve_interpolate (GtkCurve *c, gint width, gint height)
199 vector = g_malloc (width * sizeof (vector[0]));
201 gtk_curve_get_vector (c, width, vector);
204 if (c->num_points != width)
206 c->num_points = width;
209 c->point = g_malloc (c->num_points * sizeof (c->point[0]));
212 for (i = 0; i < width; ++i)
214 c->point[i].x = RADIUS + i;
215 c->point[i].y = RADIUS + height
216 - project (vector[i], c->min_y, c->max_y, height);
223 gtk_curve_draw (GtkCurve *c, gint width, gint height)
232 if (c->height != height || c->num_points != width)
233 gtk_curve_interpolate (c, width, height);
235 state = GTK_STATE_NORMAL;
236 if (!GTK_WIDGET_IS_SENSITIVE (GTK_WIDGET (c)))
237 state = GTK_STATE_INSENSITIVE;
239 style = GTK_WIDGET (c)->style;
241 /* clear the pixmap: */
242 gdk_draw_rectangle (c->pixmap, style->bg_gc[state], TRUE,
243 0, 0, width + RADIUS * 2, height + RADIUS * 2);
245 /* draw the grid lines: (XXX make more meaningful) */
246 for (i = 0; i < 5; i++)
248 gdk_draw_line (c->pixmap, style->dark_gc[state],
249 RADIUS, i * (height / 4.0) + RADIUS,
250 width + RADIUS, i * (height / 4.0) + RADIUS);
251 gdk_draw_line (c->pixmap, style->dark_gc[state],
252 i * (width / 4.0) + RADIUS, RADIUS,
253 i * (width / 4.0) + RADIUS, height + RADIUS);
256 gdk_draw_points (c->pixmap, style->fg_gc[state], c->point, c->num_points);
257 if (c->curve_type != GTK_CURVE_TYPE_FREE)
258 for (i = 0; i < c->num_ctlpoints; ++i)
262 if (c->ctlpoint[i][0] < c->min_x)
265 x = project (c->ctlpoint[i][0], c->min_x, c->max_x,
268 project (c->ctlpoint[i][1], c->min_y, c->max_y,
272 gdk_draw_arc (c->pixmap, style->fg_gc[state], TRUE, x, y,
273 RADIUS * 2, RADIUS*2, 0, 360*64);
275 gdk_draw_pixmap (GTK_WIDGET (c)->window, style->fg_gc[state], c->pixmap,
276 0, 0, 0, 0, width + RADIUS * 2, height + RADIUS * 2);
280 gtk_curve_graph_events (GtkWidget *widget, GdkEvent *event, GtkCurve *c)
282 GdkCursorType new_type = c->cursor_type;
283 gint i, src, dst, leftbound, rightbound;
284 GdkEventButton *bevent;
285 GdkEventMotion *mevent;
288 gint cx, x, y, width, height;
289 gint closest_point = 0;
290 gfloat rx, ry, min_x;
295 width = w->allocation.width - RADIUS * 2;
296 height = w->allocation.height - RADIUS * 2;
298 if ((width < 0) || (height < 0))
301 /* get the pointer position */
302 gdk_window_get_pointer (w->window, &tx, &ty, NULL);
303 x = CLAMP ((tx - RADIUS), 0, width-1);
304 y = CLAMP ((ty - RADIUS), 0, height-1);
309 for (i = 0; i < c->num_ctlpoints; ++i)
311 cx = project (c->ctlpoint[i][0], min_x, c->max_x, width);
312 if ((guint) abs (x - cx) < distance)
314 distance = abs (x - cx);
323 gdk_pixmap_unref (c->pixmap);
328 c->pixmap = gdk_pixmap_new (w->window,
330 w->allocation.height, -1);
331 gtk_curve_draw (c, width, height);
334 case GDK_BUTTON_PRESS:
335 gtk_grab_add (widget);
337 bevent = (GdkEventButton *) event;
338 new_type = GDK_TCROSS;
340 switch (c->curve_type)
342 case GTK_CURVE_TYPE_LINEAR:
343 case GTK_CURVE_TYPE_SPLINE:
344 if (distance > MIN_DISTANCE)
346 /* insert a new control point */
347 if (c->num_ctlpoints > 0)
349 cx = project (c->ctlpoint[closest_point][0], min_x,
356 g_realloc (c->ctlpoint,
357 c->num_ctlpoints * sizeof (*c->ctlpoint));
358 for (i = c->num_ctlpoints - 1; i > closest_point; --i)
359 memcpy (c->ctlpoint + i, c->ctlpoint + i - 1,
360 sizeof (*c->ctlpoint));
362 c->grab_point = closest_point;
363 c->ctlpoint[c->grab_point][0] =
364 unproject (x, min_x, c->max_x, width);
365 c->ctlpoint[c->grab_point][1] =
366 unproject (height - y, c->min_y, c->max_y, height);
368 gtk_curve_interpolate (c, width, height);
371 case GTK_CURVE_TYPE_FREE:
372 c->point[x].x = RADIUS + x;
373 c->point[x].y = RADIUS + y;
378 gtk_curve_draw (c, width, height);
381 case GDK_BUTTON_RELEASE:
382 gtk_grab_remove (widget);
384 /* delete inactive points: */
385 if (c->curve_type != GTK_CURVE_TYPE_FREE)
387 for (src = dst = 0; src < c->num_ctlpoints; ++src)
389 if (c->ctlpoint[src][0] >= min_x)
391 memcpy (c->ctlpoint + dst, c->ctlpoint + src,
392 sizeof (*c->ctlpoint));
398 c->num_ctlpoints -= (src - dst);
399 if (c->num_ctlpoints <= 0)
401 c->num_ctlpoints = 1;
402 c->ctlpoint[0][0] = min_x;
403 c->ctlpoint[0][1] = c->min_y;
404 gtk_curve_interpolate (c, width, height);
405 gtk_curve_draw (c, width, height);
408 g_realloc (c->ctlpoint,
409 c->num_ctlpoints * sizeof (*c->ctlpoint));
412 new_type = GDK_FLEUR;
416 case GDK_MOTION_NOTIFY:
417 mevent = (GdkEventMotion *) event;
419 switch (c->curve_type)
421 case GTK_CURVE_TYPE_LINEAR:
422 case GTK_CURVE_TYPE_SPLINE:
423 if (c->grab_point == -1)
425 /* if no point is grabbed... */
426 if (distance <= MIN_DISTANCE)
427 new_type = GDK_FLEUR;
429 new_type = GDK_TCROSS;
433 /* drag the grabbed point */
434 new_type = GDK_TCROSS;
436 leftbound = -MIN_DISTANCE;
437 if (c->grab_point > 0)
438 leftbound = project (c->ctlpoint[c->grab_point - 1][0],
439 min_x, c->max_x, width);
441 rightbound = width + RADIUS * 2 + MIN_DISTANCE;
442 if (c->grab_point + 1 < c->num_ctlpoints)
443 rightbound = project (c->ctlpoint[c->grab_point + 1][0],
444 min_x, c->max_x, width);
446 if (tx <= leftbound || tx >= rightbound
447 || ty > height + RADIUS * 2 + MIN_DISTANCE
448 || ty < -MIN_DISTANCE)
449 c->ctlpoint[c->grab_point][0] = min_x - 1.0;
452 rx = unproject (x, min_x, c->max_x, width);
453 ry = unproject (height - y, c->min_y, c->max_y, height);
454 c->ctlpoint[c->grab_point][0] = rx;
455 c->ctlpoint[c->grab_point][1] = ry;
457 gtk_curve_interpolate (c, width, height);
458 gtk_curve_draw (c, width, height);
462 case GTK_CURVE_TYPE_FREE:
463 if (c->grab_point != -1)
465 if (c->grab_point > x)
481 for (i = x1; i <= x2; i++)
483 c->point[i].x = RADIUS + i;
484 c->point[i].y = RADIUS +
485 (y1 + ((y2 - y1) * (i - x1)) / (x2 - x1));
489 c->point[x].x = RADIUS + x;
490 c->point[x].y = RADIUS + y;
494 gtk_curve_draw (c, width, height);
496 if (mevent->state & GDK_BUTTON1_MASK)
497 new_type = GDK_TCROSS;
499 new_type = GDK_PENCIL;
502 if (new_type != (GdkCursorType) c->cursor_type)
506 c->cursor_type = new_type;
508 cursor = gdk_cursor_new (c->cursor_type);
509 gdk_window_set_cursor (w->window, cursor);
510 gdk_cursor_destroy (cursor);
521 gtk_curve_set_curve_type (GtkCurve *c, GtkCurveType new_type)
526 if (new_type != c->curve_type)
530 width = GTK_WIDGET(c)->allocation.width - RADIUS * 2;
531 height = GTK_WIDGET(c)->allocation.height - RADIUS * 2;
533 if (new_type == GTK_CURVE_TYPE_FREE)
535 gtk_curve_interpolate (c, width, height);
536 c->curve_type = new_type;
538 else if (c->curve_type == GTK_CURVE_TYPE_FREE)
541 g_free (c->ctlpoint);
542 c->num_ctlpoints = 9;
543 c->ctlpoint = g_malloc (c->num_ctlpoints * sizeof (*c->ctlpoint));
546 dx = (width - 1) / (gfloat) (c->num_ctlpoints - 1);
548 for (i = 0; i < c->num_ctlpoints; ++i, rx += dx)
550 x = (int) (rx + 0.5);
552 unproject (x, c->min_x, c->max_x, width);
554 unproject (RADIUS + height - c->point[x].y,
555 c->min_y, c->max_y, height);
557 c->curve_type = new_type;
558 gtk_curve_interpolate (c, width, height);
562 c->curve_type = new_type;
563 gtk_curve_interpolate (c, width, height);
565 gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
566 gtk_curve_draw (c, width, height);
571 gtk_curve_size_graph (GtkCurve *curve)
576 width = (curve->max_x - curve->min_x) + 1;
577 height = (curve->max_y - curve->min_y) + 1;
578 aspect = width / (gfloat) height;
579 if (width > gdk_screen_width () / 4)
580 width = gdk_screen_width () / 4;
581 if (height > gdk_screen_height () / 4)
582 height = gdk_screen_height () / 4;
585 width = height * aspect;
587 height = width / aspect;
589 gtk_drawing_area_size (GTK_DRAWING_AREA (curve),
590 width + RADIUS * 2, height + RADIUS * 2);
594 gtk_curve_reset_vector (GtkCurve *curve)
597 g_free (curve->ctlpoint);
599 curve->num_ctlpoints = 2;
600 curve->ctlpoint = g_malloc (2 * sizeof (curve->ctlpoint[0]));
601 curve->ctlpoint[0][0] = curve->min_x;
602 curve->ctlpoint[0][1] = curve->min_y;
603 curve->ctlpoint[1][0] = curve->max_x;
604 curve->ctlpoint[1][1] = curve->max_y;
610 width = GTK_WIDGET (curve)->allocation.width - RADIUS * 2;
611 height = GTK_WIDGET (curve)->allocation.height - RADIUS * 2;
613 if (curve->curve_type == GTK_CURVE_TYPE_FREE)
615 curve->curve_type = GTK_CURVE_TYPE_LINEAR;
616 gtk_curve_interpolate (curve, width, height);
617 curve->curve_type = GTK_CURVE_TYPE_FREE;
620 gtk_curve_interpolate (curve, width, height);
621 gtk_curve_draw (curve, width, height);
626 gtk_curve_reset (GtkCurve *c)
628 GtkCurveType old_type;
630 old_type = c->curve_type;
631 c->curve_type = GTK_CURVE_TYPE_SPLINE;
632 gtk_curve_reset_vector (c);
634 if (old_type != GTK_CURVE_TYPE_SPLINE)
635 gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
639 gtk_curve_set_gamma (GtkCurve *c, gfloat gamma)
641 gfloat x, one_over_gamma, height, one_over_width;
642 GtkCurveType old_type;
645 if (c->num_points < 2)
648 old_type = c->curve_type;
649 c->curve_type = GTK_CURVE_TYPE_FREE;
652 one_over_gamma = 1.0;
654 one_over_gamma = 1.0 / gamma;
655 one_over_width = 1.0 / (c->num_points - 1);
657 for (i = 0; i < c->num_points; ++i)
659 x = (gfloat) i / (c->num_points - 1);
660 c->point[i].x = RADIUS + i;
662 RADIUS + (height * (1.0 - pow (x, one_over_gamma)) + 0.5);
665 if (old_type != GTK_CURVE_TYPE_FREE)
666 gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
668 gtk_curve_draw (c, c->num_points, c->height);
672 gtk_curve_set_range (GtkCurve *curve,
673 gfloat min_x, gfloat max_x, gfloat min_y, gfloat max_y)
675 curve->min_x = min_x;
676 curve->max_x = max_x;
677 curve->min_y = min_y;
678 curve->max_y = max_y;
680 gtk_curve_size_graph (curve);
681 gtk_curve_reset_vector (curve);
685 gtk_curve_set_vector (GtkCurve *c, int veclen, gfloat vector[])
687 GtkCurveType old_type;
691 old_type = c->curve_type;
692 c->curve_type = GTK_CURVE_TYPE_FREE;
695 height = GTK_WIDGET (c)->allocation.height - RADIUS * 2;
698 height = (c->max_y - c->min_y);
699 if (height > gdk_screen_height () / 4)
700 height = gdk_screen_height () / 4;
703 c->num_points = veclen;
704 c->point = g_malloc (c->num_points * sizeof (c->point[0]));
707 dx = (veclen - 1.0) / (c->num_points - 1.0);
709 for (i = 0; i < c->num_points; ++i, rx += dx)
711 ry = vector[(int) (rx + 0.5)];
712 if (ry > c->max_y) ry = c->max_y;
713 if (ry < c->min_y) ry = c->min_y;
714 c->point[i].x = RADIUS + i;
716 RADIUS + height - project (ry, c->min_y, c->max_y, height);
718 if (old_type != GTK_CURVE_TYPE_FREE)
719 gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
721 gtk_curve_draw (c, c->num_points, height);
725 gtk_curve_get_vector (GtkCurve *c, int veclen, gfloat vector[])
727 gfloat rx, ry, dx, dy, min_x, delta_x, *mem, *xv, *yv, *y2v, prev;
728 gint dst, i, x, next, num_active_ctlpoints = 0, first_active = -1;
732 if (c->curve_type != GTK_CURVE_TYPE_FREE)
734 /* count active points: */
736 for (i = num_active_ctlpoints = 0; i < c->num_ctlpoints; ++i)
737 if (c->ctlpoint[i][0] > prev)
739 if (first_active < 0)
741 prev = c->ctlpoint[i][0];
742 ++num_active_ctlpoints;
745 /* handle degenerate case: */
746 if (num_active_ctlpoints < 2)
748 if (num_active_ctlpoints > 0)
749 ry = c->ctlpoint[first_active][1];
752 if (ry < c->min_y) ry = c->min_y;
753 if (ry > c->max_y) ry = c->max_y;
754 for (x = 0; x < veclen; ++x)
760 switch (c->curve_type)
762 case GTK_CURVE_TYPE_SPLINE:
763 mem = g_malloc (3 * num_active_ctlpoints * sizeof (gfloat));
765 yv = mem + num_active_ctlpoints;
766 y2v = mem + 2*num_active_ctlpoints;
769 for (i = dst = 0; i < c->num_ctlpoints; ++i)
770 if (c->ctlpoint[i][0] > prev)
772 prev = c->ctlpoint[i][0];
773 xv[dst] = c->ctlpoint[i][0];
774 yv[dst] = c->ctlpoint[i][1];
778 spline_solve (num_active_ctlpoints, xv, yv, y2v);
781 dx = (c->max_x - min_x) / (veclen - 1);
782 for (x = 0; x < veclen; ++x, rx += dx)
784 ry = spline_eval (num_active_ctlpoints, xv, yv, y2v, rx);
785 if (ry < c->min_y) ry = c->min_y;
786 if (ry > c->max_y) ry = c->max_y;
793 case GTK_CURVE_TYPE_LINEAR:
794 dx = (c->max_x - min_x) / (veclen - 1);
799 for (x = 0; x < veclen; ++x, rx += dx)
801 if (rx >= c->ctlpoint[i][0])
803 if (rx > c->ctlpoint[i][0])
807 while (next < c->num_ctlpoints
808 && c->ctlpoint[next][0] <= c->ctlpoint[i][0])
810 if (next < c->num_ctlpoints)
812 delta_x = c->ctlpoint[next][0] - c->ctlpoint[i][0];
813 dy = ((c->ctlpoint[next][1] - c->ctlpoint[i][1])
816 ry = c->ctlpoint[i][1];
825 case GTK_CURVE_TYPE_FREE:
829 dx = c->num_points / (double) veclen;
830 for (x = 0; x < veclen; ++x, rx += dx)
831 vector[x] = unproject (RADIUS + c->height - c->point[(int) rx].y,
836 memset (vector, 0, veclen * sizeof (vector[0]));
844 return gtk_type_new (gtk_curve_get_type ());
848 gtk_curve_finalize (GtkObject *object)
852 g_return_if_fail (object != NULL);
853 g_return_if_fail (GTK_IS_CURVE (object));
855 curve = GTK_CURVE (object);
857 gdk_pixmap_unref (curve->pixmap);
859 g_free (curve->point);
861 g_free (curve->ctlpoint);
863 (*GTK_OBJECT_CLASS (parent_class)->finalize) (object);