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,
72 curve_type = gtk_type_unique (gtk_drawing_area_get_type (), &curve_info);
78 gtk_curve_class_init (GtkCurveClass *class)
80 GtkObjectClass *object_class;
82 parent_class = gtk_type_class (gtk_drawing_area_get_type ());
84 object_class = (GtkObjectClass *) class;
86 curve_type_changed_signal =
87 gtk_signal_new ("curve_type_changed", GTK_RUN_FIRST, object_class->type,
88 GTK_SIGNAL_OFFSET (GtkCurveClass, curve_type_changed),
89 gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
90 gtk_object_class_add_signals (object_class, &curve_type_changed_signal, 1);
92 object_class->finalize = gtk_curve_finalize;
96 gtk_curve_init (GtkCurve *curve)
100 curve->cursor_type = GDK_TOP_LEFT_ARROW;
101 curve->pixmap = NULL;
102 curve->curve_type = GTK_CURVE_TYPE_SPLINE;
104 curve->grab_point = -1;
106 curve->num_points = 0;
109 curve->num_ctlpoints = 0;
110 curve->ctlpoint = NULL;
117 old_mask = gtk_widget_get_events (GTK_WIDGET (curve));
118 gtk_widget_set_events (GTK_WIDGET (curve), old_mask | GRAPH_MASK);
119 gtk_signal_connect (GTK_OBJECT (curve), "event",
120 (GtkSignalFunc) gtk_curve_graph_events, curve);
121 gtk_curve_size_graph (curve);
125 project (gfloat value, gfloat min, gfloat max, int norm)
127 return (norm - 1) * ((value - min) / (max - min)) + 0.5;
131 unproject (gint value, gfloat min, gfloat max, int norm)
133 return value / (gfloat) (norm - 1) * (max - min) + min;
136 /* Solve the tridiagonal equation system that determines the second
137 derivatives for the interpolation points. (Based on Numerical
138 Recipies 2nd Edition.) */
140 spline_solve (int n, gfloat x[], gfloat y[], gfloat y2[])
145 u = g_malloc ((n - 1) * sizeof (u[0]));
147 y2[0] = u[0] = 0.0; /* set lower boundary condition to "natural" */
149 for (i = 1; i < n - 1; ++i)
151 sig = (x[i] - x[i - 1]) / (x[i + 1] - x[i - 1]);
152 p = sig * y2[i - 1] + 2.0;
153 y2[i] = (sig - 1.0) / p;
154 u[i] = ((y[i + 1] - y[i])
155 / (x[i + 1] - x[i]) - (y[i] - y[i - 1]) / (x[i] - x[i - 1]));
156 u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p;
160 for (k = n - 2; k >= 0; --k)
161 y2[k] = y2[k] * y2[k + 1] + u[k];
167 spline_eval (int n, gfloat x[], gfloat y[], gfloat y2[], gfloat val)
172 /* do a binary search for the right interval: */
173 k_lo = 0; k_hi = n - 1;
174 while (k_hi - k_lo > 1)
176 k = (k_hi + k_lo) / 2;
183 h = x[k_hi] - x[k_lo];
186 a = (x[k_hi] - val) / h;
187 b = (val - x[k_lo]) / h;
188 return a*y[k_lo] + b*y[k_hi] +
189 ((a*a*a - a)*y2[k_lo] + (b*b*b - b)*y2[k_hi]) * (h*h)/6.0;
193 gtk_curve_interpolate (GtkCurve *c, gint width, gint height)
198 vector = g_malloc (width * sizeof (vector[0]));
200 gtk_curve_get_vector (c, width, vector);
203 if (c->num_points != width)
205 c->num_points = width;
208 c->point = g_malloc (c->num_points * sizeof (c->point[0]));
211 for (i = 0; i < width; ++i)
213 c->point[i].x = RADIUS + i;
214 c->point[i].y = RADIUS + height
215 - project (vector[i], c->min_y, c->max_y, height);
222 gtk_curve_draw (GtkCurve *c, gint width, gint height)
231 if (c->height != height || c->num_points != width)
232 gtk_curve_interpolate (c, width, height);
234 state = GTK_STATE_NORMAL;
235 if (!GTK_WIDGET_IS_SENSITIVE (GTK_WIDGET (c)))
236 state = GTK_STATE_INSENSITIVE;
238 style = GTK_WIDGET (c)->style;
240 /* clear the pixmap: */
241 gdk_draw_rectangle (c->pixmap, style->bg_gc[state], TRUE,
242 0, 0, width + RADIUS * 2, height + RADIUS * 2);
244 /* draw the grid lines: (XXX make more meaningful) */
245 for (i = 0; i < 5; i++)
247 gdk_draw_line (c->pixmap, style->dark_gc[state],
248 RADIUS, i * (height / 4.0) + RADIUS,
249 width + RADIUS, i * (height / 4.0) + RADIUS);
250 gdk_draw_line (c->pixmap, style->dark_gc[state],
251 i * (width / 4.0) + RADIUS, RADIUS,
252 i * (width / 4.0) + RADIUS, height + RADIUS);
255 gdk_draw_points (c->pixmap, style->fg_gc[state], c->point, c->num_points);
256 if (c->curve_type != GTK_CURVE_TYPE_FREE)
257 for (i = 0; i < c->num_ctlpoints; ++i)
261 if (c->ctlpoint[i][0] < c->min_x)
264 x = project (c->ctlpoint[i][0], c->min_x, c->max_x,
267 project (c->ctlpoint[i][1], c->min_y, c->max_y,
271 gdk_draw_arc (c->pixmap, style->fg_gc[state], TRUE, x, y,
272 RADIUS * 2, RADIUS*2, 0, 360*64);
274 gdk_draw_pixmap (GTK_WIDGET (c)->window, style->fg_gc[state], c->pixmap,
275 0, 0, 0, 0, width + RADIUS * 2, height + RADIUS * 2);
279 gtk_curve_graph_events (GtkWidget *widget, GdkEvent *event, GtkCurve *c)
281 GdkCursorType new_type = c->cursor_type;
282 gint i, src, dst, leftbound, rightbound;
283 GdkEventButton *bevent;
284 GdkEventMotion *mevent;
287 gint cx, x, y, width, height;
288 gint closest_point = 0;
289 gfloat rx, ry, min_x;
294 width = w->allocation.width - RADIUS * 2;
295 height = w->allocation.height - RADIUS * 2;
297 if ((width < 0) || (height < 0))
300 /* get the pointer position */
301 gdk_window_get_pointer (w->window, &tx, &ty, NULL);
302 x = CLAMP ((tx - RADIUS), 0, width-1);
303 y = CLAMP ((ty - RADIUS), 0, height-1);
308 for (i = 0; i < c->num_ctlpoints; ++i)
310 cx = project (c->ctlpoint[i][0], min_x, c->max_x, width);
311 if ((guint) abs (x - cx) < distance)
313 distance = abs (x - cx);
322 gdk_pixmap_unref (c->pixmap);
327 c->pixmap = gdk_pixmap_new (w->window,
329 w->allocation.height, -1);
330 gtk_curve_draw (c, width, height);
333 case GDK_BUTTON_PRESS:
334 gtk_grab_add (widget);
336 bevent = (GdkEventButton *) event;
337 new_type = GDK_TCROSS;
339 switch (c->curve_type)
341 case GTK_CURVE_TYPE_LINEAR:
342 case GTK_CURVE_TYPE_SPLINE:
343 if (distance > MIN_DISTANCE)
345 /* insert a new control point */
346 if (c->num_ctlpoints > 0)
348 cx = project (c->ctlpoint[closest_point][0], min_x,
355 g_realloc (c->ctlpoint,
356 c->num_ctlpoints * sizeof (*c->ctlpoint));
357 for (i = c->num_ctlpoints - 1; i > closest_point; --i)
358 memcpy (c->ctlpoint + i, c->ctlpoint + i - 1,
359 sizeof (*c->ctlpoint));
361 c->grab_point = closest_point;
362 c->ctlpoint[c->grab_point][0] =
363 unproject (x, min_x, c->max_x, width);
364 c->ctlpoint[c->grab_point][1] =
365 unproject (height - y, c->min_y, c->max_y, height);
367 gtk_curve_interpolate (c, width, height);
370 case GTK_CURVE_TYPE_FREE:
371 c->point[x].x = RADIUS + x;
372 c->point[x].y = RADIUS + y;
377 gtk_curve_draw (c, width, height);
380 case GDK_BUTTON_RELEASE:
381 gtk_grab_remove (widget);
383 /* delete inactive points: */
384 if (c->curve_type != GTK_CURVE_TYPE_FREE)
386 for (src = dst = 0; src < c->num_ctlpoints; ++src)
388 if (c->ctlpoint[src][0] >= min_x)
390 memcpy (c->ctlpoint + dst, c->ctlpoint + src,
391 sizeof (*c->ctlpoint));
397 c->num_ctlpoints -= (src - dst);
398 if (c->num_ctlpoints <= 0)
400 c->num_ctlpoints = 1;
401 c->ctlpoint[0][0] = min_x;
402 c->ctlpoint[0][1] = c->min_y;
403 gtk_curve_interpolate (c, width, height);
404 gtk_curve_draw (c, width, height);
407 g_realloc (c->ctlpoint,
408 c->num_ctlpoints * sizeof (*c->ctlpoint));
411 new_type = GDK_FLEUR;
415 case GDK_MOTION_NOTIFY:
416 mevent = (GdkEventMotion *) event;
418 switch (c->curve_type)
420 case GTK_CURVE_TYPE_LINEAR:
421 case GTK_CURVE_TYPE_SPLINE:
422 if (c->grab_point == -1)
424 /* if no point is grabbed... */
425 if (distance <= MIN_DISTANCE)
426 new_type = GDK_FLEUR;
428 new_type = GDK_TCROSS;
432 /* drag the grabbed point */
433 new_type = GDK_TCROSS;
435 leftbound = -MIN_DISTANCE;
436 if (c->grab_point > 0)
437 leftbound = project (c->ctlpoint[c->grab_point - 1][0],
438 min_x, c->max_x, width);
440 rightbound = width + RADIUS * 2 + MIN_DISTANCE;
441 if (c->grab_point + 1 < c->num_ctlpoints)
442 rightbound = project (c->ctlpoint[c->grab_point + 1][0],
443 min_x, c->max_x, width);
445 if (tx <= leftbound || tx >= rightbound
446 || ty > height + RADIUS * 2 + MIN_DISTANCE
447 || ty < -MIN_DISTANCE)
448 c->ctlpoint[c->grab_point][0] = min_x - 1.0;
451 rx = unproject (x, min_x, c->max_x, width);
452 ry = unproject (height - y, c->min_y, c->max_y, height);
453 c->ctlpoint[c->grab_point][0] = rx;
454 c->ctlpoint[c->grab_point][1] = ry;
456 gtk_curve_interpolate (c, width, height);
457 gtk_curve_draw (c, width, height);
461 case GTK_CURVE_TYPE_FREE:
462 if (c->grab_point != -1)
464 if (c->grab_point > x)
480 for (i = x1; i <= x2; i++)
482 c->point[i].x = RADIUS + i;
483 c->point[i].y = RADIUS +
484 (y1 + ((y2 - y1) * (i - x1)) / (x2 - x1));
488 c->point[x].x = RADIUS + x;
489 c->point[x].y = RADIUS + y;
493 gtk_curve_draw (c, width, height);
495 if (mevent->state & GDK_BUTTON1_MASK)
496 new_type = GDK_TCROSS;
498 new_type = GDK_PENCIL;
501 if (new_type != (GdkCursorType) c->cursor_type)
505 c->cursor_type = new_type;
507 cursor = gdk_cursor_new (c->cursor_type);
508 gdk_window_set_cursor (w->window, cursor);
509 gdk_cursor_destroy (cursor);
520 gtk_curve_set_curve_type (GtkCurve *c, GtkCurveType new_type)
525 if (new_type != c->curve_type)
529 width = GTK_WIDGET(c)->allocation.width - RADIUS * 2;
530 height = GTK_WIDGET(c)->allocation.height - RADIUS * 2;
532 if (new_type == GTK_CURVE_TYPE_FREE)
534 gtk_curve_interpolate (c, width, height);
535 c->curve_type = new_type;
537 else if (c->curve_type == GTK_CURVE_TYPE_FREE)
540 g_free (c->ctlpoint);
541 c->num_ctlpoints = 9;
542 c->ctlpoint = g_malloc (c->num_ctlpoints * sizeof (*c->ctlpoint));
545 dx = (width - 1) / (gfloat) (c->num_ctlpoints - 1);
547 for (i = 0; i < c->num_ctlpoints; ++i, rx += dx)
549 x = (int) (rx + 0.5);
551 unproject (x, c->min_x, c->max_x, width);
553 unproject (RADIUS + height - c->point[x].y,
554 c->min_y, c->max_y, height);
556 c->curve_type = new_type;
557 gtk_curve_interpolate (c, width, height);
561 c->curve_type = new_type;
562 gtk_curve_interpolate (c, width, height);
564 gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
565 gtk_curve_draw (c, width, height);
570 gtk_curve_size_graph (GtkCurve *curve)
575 width = (curve->max_x - curve->min_x) + 1;
576 height = (curve->max_y - curve->min_y) + 1;
577 aspect = width / (gfloat) height;
578 if (width > gdk_screen_width () / 4)
579 width = gdk_screen_width () / 4;
580 if (height > gdk_screen_height () / 4)
581 height = gdk_screen_height () / 4;
584 width = height * aspect;
586 height = width / aspect;
588 gtk_drawing_area_size (GTK_DRAWING_AREA (curve),
589 width + RADIUS * 2, height + RADIUS * 2);
593 gtk_curve_reset_vector (GtkCurve *curve)
596 g_free (curve->ctlpoint);
598 curve->num_ctlpoints = 2;
599 curve->ctlpoint = g_malloc (2 * sizeof (curve->ctlpoint[0]));
600 curve->ctlpoint[0][0] = curve->min_x;
601 curve->ctlpoint[0][1] = curve->min_y;
602 curve->ctlpoint[1][0] = curve->max_x;
603 curve->ctlpoint[1][1] = curve->max_y;
609 width = GTK_WIDGET (curve)->allocation.width - RADIUS * 2;
610 height = GTK_WIDGET (curve)->allocation.height - RADIUS * 2;
612 if (curve->curve_type == GTK_CURVE_TYPE_FREE)
614 curve->curve_type = GTK_CURVE_TYPE_LINEAR;
615 gtk_curve_interpolate (curve, width, height);
616 curve->curve_type = GTK_CURVE_TYPE_FREE;
619 gtk_curve_interpolate (curve, width, height);
620 gtk_curve_draw (curve, width, height);
625 gtk_curve_reset (GtkCurve *c)
627 GtkCurveType old_type;
629 old_type = c->curve_type;
630 c->curve_type = GTK_CURVE_TYPE_SPLINE;
631 gtk_curve_reset_vector (c);
633 if (old_type != GTK_CURVE_TYPE_SPLINE)
634 gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
638 gtk_curve_set_gamma (GtkCurve *c, gfloat gamma)
640 gfloat x, one_over_gamma, height, one_over_width;
641 GtkCurveType old_type;
644 if (c->num_points < 2)
647 old_type = c->curve_type;
648 c->curve_type = GTK_CURVE_TYPE_FREE;
651 one_over_gamma = 1.0;
653 one_over_gamma = 1.0 / gamma;
654 one_over_width = 1.0 / (c->num_points - 1);
656 for (i = 0; i < c->num_points; ++i)
658 x = (gfloat) i / (c->num_points - 1);
659 c->point[i].x = RADIUS + i;
661 RADIUS + (height * (1.0 - pow (x, one_over_gamma)) + 0.5);
664 if (old_type != GTK_CURVE_TYPE_FREE)
665 gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
667 gtk_curve_draw (c, c->num_points, c->height);
671 gtk_curve_set_range (GtkCurve *curve,
672 gfloat min_x, gfloat max_x, gfloat min_y, gfloat max_y)
674 curve->min_x = min_x;
675 curve->max_x = max_x;
676 curve->min_y = min_y;
677 curve->max_y = max_y;
679 gtk_curve_size_graph (curve);
680 gtk_curve_reset_vector (curve);
684 gtk_curve_set_vector (GtkCurve *c, int veclen, gfloat vector[])
686 GtkCurveType old_type;
690 old_type = c->curve_type;
691 c->curve_type = GTK_CURVE_TYPE_FREE;
694 height = GTK_WIDGET (c)->allocation.height - RADIUS * 2;
697 height = (c->max_y - c->min_y);
698 if (height > gdk_screen_height () / 4)
699 height = gdk_screen_height () / 4;
702 c->num_points = veclen;
703 c->point = g_malloc (c->num_points * sizeof (c->point[0]));
706 dx = (veclen - 1.0) / (c->num_points - 1.0);
708 for (i = 0; i < c->num_points; ++i, rx += dx)
710 ry = vector[(int) (rx + 0.5)];
711 if (ry > c->max_y) ry = c->max_y;
712 if (ry < c->min_y) ry = c->min_y;
713 c->point[i].x = RADIUS + i;
715 RADIUS + height - project (ry, c->min_y, c->max_y, height);
717 if (old_type != GTK_CURVE_TYPE_FREE)
718 gtk_signal_emit (GTK_OBJECT (c), curve_type_changed_signal);
720 gtk_curve_draw (c, c->num_points, height);
724 gtk_curve_get_vector (GtkCurve *c, int veclen, gfloat vector[])
726 gfloat rx, ry, dx, dy, min_x, delta_x, *mem, *xv, *yv, *y2v, prev;
727 gint dst, i, x, next, num_active_ctlpoints = 0, first_active = -1;
731 if (c->curve_type != GTK_CURVE_TYPE_FREE)
733 /* count active points: */
735 for (i = num_active_ctlpoints = 0; i < c->num_ctlpoints; ++i)
736 if (c->ctlpoint[i][0] > prev)
738 if (first_active < 0)
740 prev = c->ctlpoint[i][0];
741 ++num_active_ctlpoints;
744 /* handle degenerate case: */
745 if (num_active_ctlpoints < 2)
747 if (num_active_ctlpoints > 0)
748 ry = c->ctlpoint[first_active][1];
751 if (ry < c->min_y) ry = c->min_y;
752 if (ry > c->max_y) ry = c->max_y;
753 for (x = 0; x < veclen; ++x)
759 switch (c->curve_type)
761 case GTK_CURVE_TYPE_SPLINE:
762 mem = g_malloc (3 * num_active_ctlpoints * sizeof (gfloat));
764 yv = mem + num_active_ctlpoints;
765 y2v = mem + 2*num_active_ctlpoints;
768 for (i = dst = 0; i < c->num_ctlpoints; ++i)
769 if (c->ctlpoint[i][0] > prev)
771 prev = c->ctlpoint[i][0];
772 xv[dst] = c->ctlpoint[i][0];
773 yv[dst] = c->ctlpoint[i][1];
777 spline_solve (num_active_ctlpoints, xv, yv, y2v);
780 dx = (c->max_x - min_x) / (veclen - 1);
781 for (x = 0; x < veclen; ++x, rx += dx)
783 ry = spline_eval (num_active_ctlpoints, xv, yv, y2v, rx);
784 if (ry < c->min_y) ry = c->min_y;
785 if (ry > c->max_y) ry = c->max_y;
792 case GTK_CURVE_TYPE_LINEAR:
793 dx = (c->max_x - min_x) / (veclen - 1);
798 for (x = 0; x < veclen; ++x, rx += dx)
800 if (rx >= c->ctlpoint[i][0])
802 if (rx > c->ctlpoint[i][0])
806 while (next < c->num_ctlpoints
807 && c->ctlpoint[next][0] <= c->ctlpoint[i][0])
809 if (next < c->num_ctlpoints)
811 delta_x = c->ctlpoint[next][0] - c->ctlpoint[i][0];
812 dy = ((c->ctlpoint[next][1] - c->ctlpoint[i][1])
815 ry = c->ctlpoint[i][1];
824 case GTK_CURVE_TYPE_FREE:
828 dx = c->num_points / (double) veclen;
829 for (x = 0; x < veclen; ++x, rx += dx)
830 vector[x] = unproject (RADIUS + c->height - c->point[(int) rx].y,
835 memset (vector, 0, veclen * sizeof (vector[0]));
843 return gtk_type_new (gtk_curve_get_type ());
847 gtk_curve_finalize (GtkObject *object)
851 g_return_if_fail (object != NULL);
852 g_return_if_fail (GTK_IS_CURVE (object));
854 curve = GTK_CURVE (object);
856 gdk_pixmap_unref (curve->pixmap);
858 g_free (curve->point);
860 g_free (curve->ctlpoint);
862 (*GTK_OBJECT_CLASS (parent_class)->finalize) (object);