Mercurial > hg > xmms-sid
comparison src/xs_curve.c @ 374:58079c6180a0
More removal of original curve code.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 09 Nov 2005 10:01:03 +0000 |
parents | df6f12a00305 |
children | 0f255a575720 |
comparison
equal
deleted
inserted
replaced
373:684d09ff564b | 374:58079c6180a0 |
---|---|
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> | 1 #include <stdlib.h> |
28 #include <string.h> | 2 #include <string.h> |
29 #include <math.h> | 3 #include <math.h> |
30 | 4 |
31 #include "xs_curve.h" | 5 #include "xs_curve.h" |
54 ARG_MIN_Y, | 28 ARG_MIN_Y, |
55 ARG_MAX_Y | 29 ARG_MAX_Y |
56 }; | 30 }; |
57 | 31 |
58 static GtkDrawingAreaClass *parent_class = NULL; | 32 static GtkDrawingAreaClass *parent_class = NULL; |
59 static guint curve_type_changed_signal = 0; | |
60 | 33 |
61 | 34 |
62 /* forward declarations: */ | 35 /* forward declarations: */ |
63 static void xs_curve_class_init(XSCurveClass * class); | 36 static void xs_curve_class_init(XSCurveClass * class); |
64 static void xs_curve_init(XSCurve * curve); | 37 static void xs_curve_init(XSCurve * curve); |
99 | 72 |
100 object_class->set_arg = xs_curve_set_arg; | 73 object_class->set_arg = xs_curve_set_arg; |
101 object_class->get_arg = xs_curve_get_arg; | 74 object_class->get_arg = xs_curve_get_arg; |
102 object_class->finalize = xs_curve_finalize; | 75 object_class->finalize = xs_curve_finalize; |
103 | 76 |
104 curve_type_changed_signal = | |
105 gtk_signal_new("curve_type_changed", GTK_RUN_FIRST, object_class->type, | |
106 GTK_SIGNAL_OFFSET(XSCurveClass, curve_type_changed), | |
107 gtk_marshal_NONE__NONE, GTK_TYPE_NONE, 0); | |
108 gtk_object_class_add_signals(object_class, &curve_type_changed_signal, 1); | |
109 | |
110 gtk_object_add_arg_type("XSCurve::curve_type", GTK_TYPE_CURVE_TYPE, GTK_ARG_READWRITE, ARG_CURVE_TYPE); | |
111 gtk_object_add_arg_type("XSCurve::min_x", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_MIN_X); | 77 gtk_object_add_arg_type("XSCurve::min_x", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_MIN_X); |
112 gtk_object_add_arg_type("XSCurve::max_x", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_MAX_X); | 78 gtk_object_add_arg_type("XSCurve::max_x", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_MAX_X); |
113 gtk_object_add_arg_type("XSCurve::min_y", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_MIN_Y); | 79 gtk_object_add_arg_type("XSCurve::min_y", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_MIN_Y); |
114 gtk_object_add_arg_type("XSCurve::max_y", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_MAX_Y); | 80 gtk_object_add_arg_type("XSCurve::max_y", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_MAX_Y); |
115 } | 81 } |
116 | 82 |
117 static void xs_curve_init(XSCurve * curve) | 83 static void xs_curve_init(XSCurve * curve) |
118 { | 84 { |
119 gint old_mask; | 85 gint old_mask; |
120 | 86 |
121 curve->cursor_type = GDK_TOP_LEFT_ARROW; | |
122 curve->pixmap = NULL; | 87 curve->pixmap = NULL; |
123 curve->curve_type = GTK_CURVE_TYPE_SPLINE; | |
124 curve->height = 0; | 88 curve->height = 0; |
125 curve->grab_point = -1; | 89 curve->grab_point = -1; |
126 | 90 |
127 curve->num_points = 0; | 91 curve->num_points = 0; |
128 curve->point = 0; | 92 curve->point = 0; |
144 static void xs_curve_set_arg(GtkObject * object, GtkArg * arg, guint arg_id) | 108 static void xs_curve_set_arg(GtkObject * object, GtkArg * arg, guint arg_id) |
145 { | 109 { |
146 XSCurve *curve = XS_CURVE(object); | 110 XSCurve *curve = XS_CURVE(object); |
147 | 111 |
148 switch (arg_id) { | 112 switch (arg_id) { |
149 case ARG_CURVE_TYPE: | |
150 xs_curve_set_curve_type(curve, GTK_VALUE_ENUM(*arg)); | |
151 break; | |
152 case ARG_MIN_X: | 113 case ARG_MIN_X: |
153 xs_curve_set_range(curve, GTK_VALUE_FLOAT(*arg), curve->max_x, curve->min_y, curve->max_y); | 114 xs_curve_set_range(curve, GTK_VALUE_FLOAT(*arg), curve->max_x, curve->min_y, curve->max_y); |
154 break; | 115 break; |
155 case ARG_MAX_X: | 116 case ARG_MAX_X: |
156 xs_curve_set_range(curve, curve->min_x, GTK_VALUE_FLOAT(*arg), curve->min_y, curve->max_y); | 117 xs_curve_set_range(curve, curve->min_x, GTK_VALUE_FLOAT(*arg), curve->min_y, curve->max_y); |
167 static void xs_curve_get_arg(GtkObject * object, GtkArg * arg, guint arg_id) | 128 static void xs_curve_get_arg(GtkObject * object, GtkArg * arg, guint arg_id) |
168 { | 129 { |
169 XSCurve *curve = XS_CURVE(object); | 130 XSCurve *curve = XS_CURVE(object); |
170 | 131 |
171 switch (arg_id) { | 132 switch (arg_id) { |
172 case ARG_CURVE_TYPE: | |
173 GTK_VALUE_ENUM(*arg) = curve->curve_type; | |
174 break; | |
175 case ARG_MIN_X: | 133 case ARG_MIN_X: |
176 GTK_VALUE_FLOAT(*arg) = curve->min_x; | 134 GTK_VALUE_FLOAT(*arg) = curve->min_x; |
177 break; | 135 break; |
178 case ARG_MAX_X: | 136 case ARG_MAX_X: |
179 GTK_VALUE_FLOAT(*arg) = curve->max_x; | 137 GTK_VALUE_FLOAT(*arg) = curve->max_x; |
188 arg->type = GTK_TYPE_INVALID; | 146 arg->type = GTK_TYPE_INVALID; |
189 break; | 147 break; |
190 } | 148 } |
191 } | 149 } |
192 | 150 |
193 static int project(gfloat value, gfloat min, gfloat max, int norm) | 151 static int xs_project(gfloat value, gfloat min, gfloat max, int norm) |
194 { | 152 { |
195 return (norm - 1) * ((value - min) / (max - min)) + 0.5; | 153 return (norm - 1) * ((value - min) / (max - min)) + 0.5; |
196 } | 154 } |
197 | 155 |
198 static gfloat unproject(gint value, gfloat min, gfloat max, int norm) | 156 static gfloat xs_unproject(gint value, gfloat min, gfloat max, int norm) |
199 { | 157 { |
200 return value / (gfloat) (norm - 1) * (max - min) + min; | 158 return value / (gfloat) (norm - 1) * (max - min) + min; |
201 } | 159 } |
202 | 160 |
203 /* Solve the tridiagonal equation system that determines the second | 161 /* Solve the tridiagonal equation system that determines the second |
253 } | 211 } |
254 | 212 |
255 static void xs_curve_interpolate(XSCurve * c, gint width, gint height) | 213 static void xs_curve_interpolate(XSCurve * c, gint width, gint height) |
256 { | 214 { |
257 gfloat *vector; | 215 gfloat *vector; |
258 int i; | 216 gint i; |
259 | 217 |
260 vector = g_malloc(width * sizeof(vector[0])); | 218 vector = g_malloc(width * sizeof(vector[0])); |
261 | 219 |
262 xs_curve_get_vector(c, width, vector); | 220 xs_curve_get_vector(c, width, vector); |
263 | 221 |
294 state = GTK_STATE_INSENSITIVE; | 252 state = GTK_STATE_INSENSITIVE; |
295 | 253 |
296 style = GTK_WIDGET(c)->style; | 254 style = GTK_WIDGET(c)->style; |
297 | 255 |
298 /* clear the pixmap: */ | 256 /* clear the pixmap: */ |
299 gtk_paint_flat_box(style, c->pixmap, GTK_STATE_NORMAL, GTK_SHADOW_NONE, | 257 gtk_paint_flat_box(style, c->pixmap, |
300 NULL, GTK_WIDGET(c), "curve_bg", 0, 0, width + RADIUS * 2, height + RADIUS * 2); | 258 GTK_STATE_NORMAL, GTK_SHADOW_NONE, |
259 NULL, GTK_WIDGET(c), "curve_bg", | |
260 0, 0, width + RADIUS * 2, height + RADIUS * 2); | |
261 | |
301 /* draw the grid lines: (XXX make more meaningful) */ | 262 /* draw the grid lines: (XXX make more meaningful) */ |
302 for (i = 0; i < 5; i++) { | 263 for (i = 0; i < 5; i++) { |
303 gdk_draw_line(c->pixmap, style->dark_gc[state], | 264 gdk_draw_line(c->pixmap, style->dark_gc[state], |
304 RADIUS, i * (height / 4.0) + RADIUS, width + RADIUS, i * (height / 4.0) + RADIUS); | 265 RADIUS, i * (height / 4.0) + RADIUS, |
266 width + RADIUS, i * (height / 4.0) + RADIUS); | |
267 | |
305 gdk_draw_line(c->pixmap, style->dark_gc[state], | 268 gdk_draw_line(c->pixmap, style->dark_gc[state], |
306 i * (width / 4.0) + RADIUS, RADIUS, i * (width / 4.0) + RADIUS, height + RADIUS); | 269 i * (width / 4.0) + RADIUS, RADIUS, |
270 i * (width / 4.0) + RADIUS, height + RADIUS); | |
307 } | 271 } |
308 | 272 |
309 gdk_draw_points(c->pixmap, style->fg_gc[state], c->point, c->num_points); | 273 gdk_draw_points(c->pixmap, style->fg_gc[state], c->point, c->num_points); |
310 if (c->curve_type != GTK_CURVE_TYPE_FREE) | |
311 for (i = 0; i < c->num_ctlpoints; ++i) { | |
312 gint x, y; | |
313 | |
314 if (c->ctlpoint[i][0] < c->min_x) | |
315 continue; | |
316 | |
317 x = project(c->ctlpoint[i][0], c->min_x, c->max_x, width); | |
318 y = height - project(c->ctlpoint[i][1], c->min_y, c->max_y, height); | |
319 | |
320 /* draw a bullet: */ | |
321 gdk_draw_arc(c->pixmap, style->fg_gc[state], TRUE, x, y, RADIUS * 2, RADIUS * 2, 0, 360 * 64); | |
322 } | |
323 | 274 |
324 gdk_draw_pixmap(GTK_WIDGET(c)->window, style->fg_gc[state], c->pixmap, | 275 gdk_draw_pixmap(GTK_WIDGET(c)->window, style->fg_gc[state], c->pixmap, |
325 0, 0, 0, 0, width + RADIUS * 2, height + RADIUS * 2); | 276 0, 0, 0, 0, width + RADIUS * 2, height + RADIUS * 2); |
326 } | 277 } |
327 | 278 |
328 static gint xs_curve_graph_events(GtkWidget * widget, GdkEvent * event, XSCurve * c) | 279 static gint xs_curve_graph_events(GtkWidget * widget, GdkEvent * event, XSCurve * c) |
329 { | 280 { |
330 GdkCursorType new_type = c->cursor_type; | |
331 gint i, src, dst, leftbound, rightbound; | 281 gint i, src, dst, leftbound, rightbound; |
332 GdkEventButton *bevent; | 282 GdkEventButton *bevent; |
333 GdkEventMotion *mevent; | 283 GdkEventMotion *mevent; |
334 GtkWidget *w; | 284 GtkWidget *w; |
335 gint tx, ty; | 285 gint tx, ty; |
343 width = w->allocation.width - RADIUS * 2; | 293 width = w->allocation.width - RADIUS * 2; |
344 height = w->allocation.height - RADIUS * 2; | 294 height = w->allocation.height - RADIUS * 2; |
345 | 295 |
346 if ((width < 0) || (height < 0)) | 296 if ((width < 0) || (height < 0)) |
347 return FALSE; | 297 return FALSE; |
348 | 298 |
349 /* get the pointer position */ | |
350 gdk_window_get_pointer(w->window, &tx, &ty, NULL); | |
351 x = CLAMP((tx - RADIUS), 0, width - 1); | |
352 y = CLAMP((ty - RADIUS), 0, height - 1); | |
353 | |
354 min_x = c->min_x; | |
355 | |
356 distance = ~0U; | |
357 for (i = 0; i < c->num_ctlpoints; ++i) { | |
358 cx = project(c->ctlpoint[i][0], min_x, c->max_x, width); | |
359 if ((guint) abs(x - cx) < distance) { | |
360 distance = abs(x - cx); | |
361 closest_point = i; | |
362 } | |
363 } | |
364 | |
365 switch (event->type) { | |
366 case GDK_CONFIGURE: | |
367 if (c->pixmap) | |
368 gdk_pixmap_unref(c->pixmap); | |
369 c->pixmap = 0; | |
370 /* fall through */ | |
371 case GDK_EXPOSE: | |
372 if (!c->pixmap) | |
373 c->pixmap = gdk_pixmap_new(w->window, w->allocation.width, w->allocation.height, -1); | |
374 xs_curve_draw(c, width, height); | |
375 break; | |
376 | |
377 case GDK_BUTTON_PRESS: | |
378 gtk_grab_add(widget); | |
379 | |
380 bevent = (GdkEventButton *) event; | |
381 new_type = GDK_TCROSS; | |
382 | |
383 switch (c->curve_type) { | |
384 case GTK_CURVE_TYPE_LINEAR: | |
385 case GTK_CURVE_TYPE_SPLINE: | |
386 if (distance > MIN_DISTANCE) { | |
387 /* insert a new control point */ | |
388 if (c->num_ctlpoints > 0) { | |
389 cx = project(c->ctlpoint[closest_point][0], min_x, c->max_x, width); | |
390 if (x > cx) | |
391 ++closest_point; | |
392 } | |
393 ++c->num_ctlpoints; | |
394 c->ctlpoint = g_realloc(c->ctlpoint, c->num_ctlpoints * sizeof(*c->ctlpoint)); | |
395 for (i = c->num_ctlpoints - 1; i > closest_point; --i) | |
396 memcpy(c->ctlpoint + i, c->ctlpoint + i - 1, sizeof(*c->ctlpoint)); | |
397 } | |
398 c->grab_point = closest_point; | |
399 c->ctlpoint[c->grab_point][0] = unproject(x, min_x, c->max_x, width); | |
400 c->ctlpoint[c->grab_point][1] = unproject(height - y, c->min_y, c->max_y, height); | |
401 | |
402 xs_curve_interpolate(c, width, height); | |
403 break; | |
404 | |
405 case GTK_CURVE_TYPE_FREE: | |
406 c->point[x].x = RADIUS + x; | |
407 c->point[x].y = RADIUS + y; | |
408 c->grab_point = x; | |
409 c->last = y; | |
410 break; | |
411 } | |
412 xs_curve_draw(c, width, height); | |
413 break; | |
414 | |
415 case GDK_BUTTON_RELEASE: | |
416 gtk_grab_remove(widget); | |
417 | |
418 /* delete inactive points: */ | |
419 if (c->curve_type != GTK_CURVE_TYPE_FREE) { | |
420 for (src = dst = 0; src < c->num_ctlpoints; ++src) { | |
421 if (c->ctlpoint[src][0] >= min_x) { | |
422 memcpy(c->ctlpoint + dst, c->ctlpoint + src, sizeof(*c->ctlpoint)); | |
423 ++dst; | |
424 } | |
425 } | |
426 if (dst < src) { | |
427 c->num_ctlpoints -= (src - dst); | |
428 if (c->num_ctlpoints <= 0) { | |
429 c->num_ctlpoints = 1; | |
430 c->ctlpoint[0][0] = min_x; | |
431 c->ctlpoint[0][1] = c->min_y; | |
432 xs_curve_interpolate(c, width, height); | |
433 xs_curve_draw(c, width, height); | |
434 } | |
435 c->ctlpoint = g_realloc(c->ctlpoint, c->num_ctlpoints * sizeof(*c->ctlpoint)); | |
436 } | |
437 } | |
438 new_type = GDK_FLEUR; | |
439 c->grab_point = -1; | |
440 break; | |
441 | |
442 case GDK_MOTION_NOTIFY: | |
443 mevent = (GdkEventMotion *) event; | |
444 | |
445 switch (c->curve_type) { | |
446 case GTK_CURVE_TYPE_LINEAR: | |
447 case GTK_CURVE_TYPE_SPLINE: | |
448 if (c->grab_point == -1) { | |
449 /* if no point is grabbed... */ | |
450 if (distance <= MIN_DISTANCE) | |
451 new_type = GDK_FLEUR; | |
452 else | |
453 new_type = GDK_TCROSS; | |
454 } else { | |
455 /* drag the grabbed point */ | |
456 new_type = GDK_TCROSS; | |
457 | |
458 leftbound = -MIN_DISTANCE; | |
459 if (c->grab_point > 0) | |
460 leftbound = project(c->ctlpoint[c->grab_point - 1][0], min_x, c->max_x, width); | |
461 | |
462 rightbound = width + RADIUS * 2 + MIN_DISTANCE; | |
463 if (c->grab_point + 1 < c->num_ctlpoints) | |
464 rightbound = project(c->ctlpoint[c->grab_point + 1][0], min_x, c->max_x, width); | |
465 | |
466 if (tx <= leftbound || tx >= rightbound | |
467 || ty > height + RADIUS * 2 + MIN_DISTANCE || ty < -MIN_DISTANCE) | |
468 c->ctlpoint[c->grab_point][0] = min_x - 1.0; | |
469 else { | |
470 rx = unproject(x, min_x, c->max_x, width); | |
471 ry = unproject(height - y, c->min_y, c->max_y, height); | |
472 c->ctlpoint[c->grab_point][0] = rx; | |
473 c->ctlpoint[c->grab_point][1] = ry; | |
474 } | |
475 xs_curve_interpolate(c, width, height); | |
476 xs_curve_draw(c, width, height); | |
477 } | |
478 break; | |
479 | |
480 case GTK_CURVE_TYPE_FREE: | |
481 if (c->grab_point != -1) { | |
482 if (c->grab_point > x) { | |
483 x1 = x; | |
484 x2 = c->grab_point; | |
485 y1 = y; | |
486 y2 = c->last; | |
487 } else { | |
488 x1 = c->grab_point; | |
489 x2 = x; | |
490 y1 = c->last; | |
491 y2 = y; | |
492 } | |
493 | |
494 if (x2 != x1) | |
495 for (i = x1; i <= x2; i++) { | |
496 c->point[i].x = RADIUS + i; | |
497 c->point[i].y = RADIUS + (y1 + ((y2 - y1) * (i - x1)) / (x2 - x1)); | |
498 } else { | |
499 c->point[x].x = RADIUS + x; | |
500 c->point[x].y = RADIUS + y; | |
501 } | |
502 c->grab_point = x; | |
503 c->last = y; | |
504 xs_curve_draw(c, width, height); | |
505 } | |
506 if (mevent->state & GDK_BUTTON1_MASK) | |
507 new_type = GDK_TCROSS; | |
508 else | |
509 new_type = GDK_PENCIL; | |
510 break; | |
511 } | |
512 if (new_type != (GdkCursorType) c->cursor_type) { | |
513 GdkCursor *cursor; | |
514 | |
515 c->cursor_type = new_type; | |
516 | |
517 cursor = gdk_cursor_new(c->cursor_type); | |
518 gdk_window_set_cursor(w->window, cursor); | |
519 gdk_cursor_destroy(cursor); | |
520 } | |
521 break; | |
522 | |
523 default: | |
524 break; | |
525 } | |
526 return FALSE; | 299 return FALSE; |
527 } | |
528 | |
529 void xs_curve_set_curve_type(XSCurve * c, GtkCurveType new_type) | |
530 { | |
531 gfloat rx, dx; | |
532 gint x, i; | |
533 | |
534 if (new_type != c->curve_type) { | |
535 gint width, height; | |
536 | |
537 width = GTK_WIDGET(c)->allocation.width - RADIUS * 2; | |
538 height = GTK_WIDGET(c)->allocation.height - RADIUS * 2; | |
539 | |
540 if (new_type == GTK_CURVE_TYPE_FREE) { | |
541 xs_curve_interpolate(c, width, height); | |
542 c->curve_type = new_type; | |
543 } else if (c->curve_type == GTK_CURVE_TYPE_FREE) { | |
544 if (c->ctlpoint) | |
545 g_free(c->ctlpoint); | |
546 c->num_ctlpoints = 9; | |
547 c->ctlpoint = g_malloc(c->num_ctlpoints * sizeof(*c->ctlpoint)); | |
548 | |
549 rx = 0.0; | |
550 dx = (width - 1) / (gfloat) (c->num_ctlpoints - 1); | |
551 | |
552 for (i = 0; i < c->num_ctlpoints; ++i, rx += dx) { | |
553 x = (int) (rx + 0.5); | |
554 c->ctlpoint[i][0] = unproject(x, c->min_x, c->max_x, width); | |
555 c->ctlpoint[i][1] = | |
556 unproject(RADIUS + height - c->point[x].y, c->min_y, c->max_y, height); | |
557 } | |
558 c->curve_type = new_type; | |
559 xs_curve_interpolate(c, width, height); | |
560 } else { | |
561 c->curve_type = new_type; | |
562 xs_curve_interpolate(c, width, height); | |
563 } | |
564 gtk_signal_emit(GTK_OBJECT(c), curve_type_changed_signal); | |
565 xs_curve_draw(c, width, height); | |
566 } | |
567 } | 300 } |
568 | 301 |
569 static void xs_curve_size_graph(XSCurve * curve) | 302 static void xs_curve_size_graph(XSCurve * curve) |
570 { | 303 { |
571 gint width, height; | 304 gint width, height; |
587 gtk_drawing_area_size(GTK_DRAWING_AREA(curve), width + RADIUS * 2, height + RADIUS * 2); | 320 gtk_drawing_area_size(GTK_DRAWING_AREA(curve), width + RADIUS * 2, height + RADIUS * 2); |
588 } | 321 } |
589 | 322 |
590 static void xs_curve_reset_vector(XSCurve * curve) | 323 static void xs_curve_reset_vector(XSCurve * curve) |
591 { | 324 { |
592 if (curve->ctlpoint) | |
593 g_free(curve->ctlpoint); | |
594 | |
595 curve->num_ctlpoints = 2; | |
596 curve->ctlpoint = g_malloc(2 * sizeof(curve->ctlpoint[0])); | |
597 curve->ctlpoint[0][0] = curve->min_x; | |
598 curve->ctlpoint[0][1] = curve->min_y; | |
599 curve->ctlpoint[1][0] = curve->max_x; | |
600 curve->ctlpoint[1][1] = curve->max_y; | |
601 | |
602 if (curve->pixmap) { | |
603 gint width, height; | |
604 | |
605 width = GTK_WIDGET(curve)->allocation.width - RADIUS * 2; | |
606 height = GTK_WIDGET(curve)->allocation.height - RADIUS * 2; | |
607 | |
608 if (curve->curve_type == GTK_CURVE_TYPE_FREE) { | |
609 curve->curve_type = GTK_CURVE_TYPE_LINEAR; | |
610 xs_curve_interpolate(curve, width, height); | |
611 curve->curve_type = GTK_CURVE_TYPE_FREE; | |
612 } else | |
613 xs_curve_interpolate(curve, width, height); | |
614 xs_curve_draw(curve, width, height); | |
615 } | |
616 } | 325 } |
617 | 326 |
618 void xs_curve_reset(XSCurve * c) | 327 void xs_curve_reset(XSCurve * c) |
619 { | 328 { |
620 GtkCurveType old_type; | 329 } |
621 | 330 |
622 old_type = c->curve_type; | |
623 c->curve_type = GTK_CURVE_TYPE_SPLINE; | |
624 xs_curve_reset_vector(c); | |
625 | |
626 if (old_type != GTK_CURVE_TYPE_SPLINE) | |
627 gtk_signal_emit(GTK_OBJECT(c), curve_type_changed_signal); | |
628 } | |
629 | |
630 void xs_curve_set_gamma(XSCurve * c, gfloat gamma) | |
631 { | |
632 gfloat x, one_over_gamma, height, one_over_width; | |
633 GtkCurveType old_type; | |
634 gint i; | |
635 | |
636 if (c->num_points < 2) | |
637 return; | |
638 | |
639 old_type = c->curve_type; | |
640 c->curve_type = GTK_CURVE_TYPE_FREE; | |
641 | |
642 if (gamma <= 0) | |
643 one_over_gamma = 1.0; | |
644 else | |
645 one_over_gamma = 1.0 / gamma; | |
646 one_over_width = 1.0 / (c->num_points - 1); | |
647 height = c->height; | |
648 for (i = 0; i < c->num_points; ++i) { | |
649 x = (gfloat) i / (c->num_points - 1); | |
650 c->point[i].x = RADIUS + i; | |
651 c->point[i].y = RADIUS + (height * (1.0 - pow(x, one_over_gamma)) + 0.5); | |
652 } | |
653 | |
654 if (old_type != GTK_CURVE_TYPE_FREE) | |
655 gtk_signal_emit(GTK_OBJECT(c), curve_type_changed_signal); | |
656 | |
657 xs_curve_draw(c, c->num_points, c->height); | |
658 } | |
659 | 331 |
660 void xs_curve_set_range(XSCurve * curve, gfloat min_x, gfloat max_x, gfloat min_y, gfloat max_y) | 332 void xs_curve_set_range(XSCurve * curve, gfloat min_x, gfloat max_x, gfloat min_y, gfloat max_y) |
661 { | 333 { |
662 curve->min_x = min_x; | 334 curve->min_x = min_x; |
663 curve->max_x = max_x; | 335 curve->max_x = max_x; |
666 | 338 |
667 xs_curve_size_graph(curve); | 339 xs_curve_size_graph(curve); |
668 xs_curve_reset_vector(curve); | 340 xs_curve_reset_vector(curve); |
669 } | 341 } |
670 | 342 |
671 void xs_curve_set_vector(XSCurve * c, int veclen, gfloat vector[]) | |
672 { | |
673 GtkCurveType old_type; | |
674 gfloat rx, dx, ry; | |
675 gint i, height; | |
676 | |
677 old_type = c->curve_type; | |
678 c->curve_type = GTK_CURVE_TYPE_FREE; | |
679 | |
680 if (c->point) | |
681 height = GTK_WIDGET(c)->allocation.height - RADIUS * 2; | |
682 else { | |
683 height = (c->max_y - c->min_y); | |
684 if (height > gdk_screen_height() / 4) | |
685 height = gdk_screen_height() / 4; | |
686 | |
687 c->height = height; | |
688 c->num_points = veclen; | |
689 c->point = g_malloc(c->num_points * sizeof(c->point[0])); | |
690 } | |
691 rx = 0; | |
692 dx = (veclen - 1.0) / (c->num_points - 1.0); | |
693 | |
694 for (i = 0; i < c->num_points; ++i, rx += dx) { | |
695 ry = vector[(int) (rx + 0.5)]; | |
696 if (ry > c->max_y) | |
697 ry = c->max_y; | |
698 if (ry < c->min_y) | |
699 ry = c->min_y; | |
700 c->point[i].x = RADIUS + i; | |
701 c->point[i].y = RADIUS + height - project(ry, c->min_y, c->max_y, height); | |
702 } | |
703 if (old_type != GTK_CURVE_TYPE_FREE) | |
704 gtk_signal_emit(GTK_OBJECT(c), curve_type_changed_signal); | |
705 | |
706 xs_curve_draw(c, c->num_points, height); | |
707 } | |
708 | |
709 void xs_curve_get_vector(XSCurve * c, int veclen, gfloat vector[]) | |
710 { | |
711 gfloat rx, ry, dx, dy, min_x, delta_x, *mem, *xv, *yv, *y2v, prev; | |
712 gint dst, i, x, next, num_active_ctlpoints = 0, first_active = -1; | |
713 | |
714 min_x = c->min_x; | |
715 | |
716 if (c->curve_type != GTK_CURVE_TYPE_FREE) { | |
717 /* count active points: */ | |
718 prev = min_x - 1.0; | |
719 for (i = num_active_ctlpoints = 0; i < c->num_ctlpoints; ++i) | |
720 if (c->ctlpoint[i][0] > prev) { | |
721 if (first_active < 0) | |
722 first_active = i; | |
723 prev = c->ctlpoint[i][0]; | |
724 ++num_active_ctlpoints; | |
725 } | |
726 | |
727 /* handle degenerate case: */ | |
728 if (num_active_ctlpoints < 2) { | |
729 if (num_active_ctlpoints > 0) | |
730 ry = c->ctlpoint[first_active][1]; | |
731 else | |
732 ry = c->min_y; | |
733 if (ry < c->min_y) | |
734 ry = c->min_y; | |
735 if (ry > c->max_y) | |
736 ry = c->max_y; | |
737 for (x = 0; x < veclen; ++x) | |
738 vector[x] = ry; | |
739 return; | |
740 } | |
741 } | |
742 | |
743 switch (c->curve_type) { | |
744 case GTK_CURVE_TYPE_SPLINE: | |
745 mem = g_malloc(3 * num_active_ctlpoints * sizeof(gfloat)); | |
746 xv = mem; | |
747 yv = mem + num_active_ctlpoints; | |
748 y2v = mem + 2 * num_active_ctlpoints; | |
749 | |
750 prev = min_x - 1.0; | |
751 for (i = dst = 0; i < c->num_ctlpoints; ++i) | |
752 if (c->ctlpoint[i][0] > prev) { | |
753 prev = c->ctlpoint[i][0]; | |
754 xv[dst] = c->ctlpoint[i][0]; | |
755 yv[dst] = c->ctlpoint[i][1]; | |
756 ++dst; | |
757 } | |
758 | |
759 spline_solve(num_active_ctlpoints, xv, yv, y2v); | |
760 | |
761 rx = min_x; | |
762 dx = (c->max_x - min_x) / (veclen - 1); | |
763 for (x = 0; x < veclen; ++x, rx += dx) { | |
764 ry = spline_eval(num_active_ctlpoints, xv, yv, y2v, rx); | |
765 if (ry < c->min_y) | |
766 ry = c->min_y; | |
767 if (ry > c->max_y) | |
768 ry = c->max_y; | |
769 vector[x] = ry; | |
770 } | |
771 | |
772 g_free(mem); | |
773 break; | |
774 | |
775 case GTK_CURVE_TYPE_LINEAR: | |
776 dx = (c->max_x - min_x) / (veclen - 1); | |
777 rx = min_x; | |
778 ry = c->min_y; | |
779 dy = 0.0; | |
780 i = first_active; | |
781 for (x = 0; x < veclen; ++x, rx += dx) { | |
782 if (rx >= c->ctlpoint[i][0]) { | |
783 if (rx > c->ctlpoint[i][0]) | |
784 ry = c->min_y; | |
785 dy = 0.0; | |
786 next = i + 1; | |
787 while (next < c->num_ctlpoints && c->ctlpoint[next][0] <= c->ctlpoint[i][0]) | |
788 ++next; | |
789 if (next < c->num_ctlpoints) { | |
790 delta_x = c->ctlpoint[next][0] - c->ctlpoint[i][0]; | |
791 dy = ((c->ctlpoint[next][1] - c->ctlpoint[i][1]) | |
792 / delta_x); | |
793 dy *= dx; | |
794 ry = c->ctlpoint[i][1]; | |
795 i = next; | |
796 } | |
797 } | |
798 vector[x] = ry; | |
799 ry += dy; | |
800 } | |
801 break; | |
802 | |
803 case GTK_CURVE_TYPE_FREE: | |
804 if (c->point) { | |
805 rx = 0.0; | |
806 dx = c->num_points / (double) veclen; | |
807 for (x = 0; x < veclen; ++x, rx += dx) | |
808 vector[x] = unproject(RADIUS + c->height - c->point[(int) rx].y, | |
809 c->min_y, c->max_y, c->height); | |
810 } else | |
811 memset(vector, 0, veclen * sizeof(vector[0])); | |
812 break; | |
813 } | |
814 } | |
815 | 343 |
816 GtkWidget *xs_curve_new(void) | 344 GtkWidget *xs_curve_new(void) |
817 { | 345 { |
818 return gtk_type_new(xs_curve_get_type()); | 346 return gtk_type_new(xs_curve_get_type()); |
819 } | 347 } |
348 | |
820 | 349 |
821 static void xs_curve_finalize(GtkObject * object) | 350 static void xs_curve_finalize(GtkObject * object) |
822 { | 351 { |
823 XSCurve *curve; | 352 XSCurve *curve; |
824 | 353 |
833 if (curve->ctlpoint) | 362 if (curve->ctlpoint) |
834 g_free(curve->ctlpoint); | 363 g_free(curve->ctlpoint); |
835 | 364 |
836 (*GTK_OBJECT_CLASS(parent_class)->finalize) (object); | 365 (*GTK_OBJECT_CLASS(parent_class)->finalize) (object); |
837 } | 366 } |
367 | |
368 | |
369 void xs_curve_get_vector(XSCurve *curve, int veclen, gfloat vector[]) | |
370 { | |
371 } | |
372 | |
373 void xs_curve_set_vector(XSCurve *curve, int veclen, gfloat vector[]) | |
374 { | |
375 } | |
376 | |
377 |