# HG changeset patch # User Matti Hamalainen # Date 1352541539 -7200 # Node ID 8b4c016802eaf344dc2fd4a273774bb5f19a473a # Parent cc2789a06d44c8d52217ea1b9921d18b8a38a6f8 Cosmetics. diff -r cc2789a06d44 -r 8b4c016802ea src/xs_curve.c --- a/src/xs_curve.c Sat Nov 10 11:51:26 2012 +0200 +++ b/src/xs_curve.c Sat Nov 10 11:58:59 2012 +0200 @@ -33,18 +33,19 @@ #include -#define RADIUS 3 /* radius of the control points */ -#define RADIUS2 (RADIUS * 2) -#define MIN_DISTANCE 7 /* min distance between control points */ +#define CP_RADIUS 3 /* radius of the control points */ +#define CP_RADIUS2 (CP_RADIUS * 2) +#define CP_MIN_DISTANCE 7 /* min distance between control points */ -#define GRAPH_MASK (GDK_EXPOSURE_MASK | \ - GDK_POINTER_MOTION_MASK | \ - GDK_POINTER_MOTION_HINT_MASK | \ - GDK_ENTER_NOTIFY_MASK | \ - GDK_BUTTON_PRESS_MASK | \ - GDK_BUTTON_RELEASE_MASK | \ - GDK_BUTTON1_MOTION_MASK) +#define GRAPH_MASK ( \ + GDK_EXPOSURE_MASK | \ + GDK_POINTER_MOTION_MASK | \ + GDK_POINTER_MOTION_HINT_MASK | \ + GDK_ENTER_NOTIFY_MASK | \ + GDK_BUTTON_PRESS_MASK | \ + GDK_BUTTON_RELEASE_MASK | \ + GDK_BUTTON1_MOTION_MASK) #define GET_X(i) curve->ctlpoints[i].x #define GET_Y(i) curve->ctlpoints[i].y @@ -73,8 +74,10 @@ { static GtkType curve_type = 0; - if (!curve_type) { - static const GtkTypeInfo curve_info = { + if (!curve_type) + { + static const GtkTypeInfo curve_info = + { "XSCurve", sizeof(XSCurve), sizeof(XSCurveClass), @@ -136,19 +139,20 @@ { XSCurve *curve = XS_CURVE(object); - switch (arg_id) { - case ARG_MIN_X: - xs_curve_set_range(curve, GTK_VALUE_FLOAT(*arg), curve->max_x, curve->min_y, curve->max_y); - break; - case ARG_MAX_X: - xs_curve_set_range(curve, curve->min_x, GTK_VALUE_FLOAT(*arg), curve->min_y, curve->max_y); - break; - case ARG_MIN_Y: - xs_curve_set_range(curve, curve->min_x, curve->max_x, GTK_VALUE_FLOAT(*arg), curve->max_y); - break; - case ARG_MAX_Y: - xs_curve_set_range(curve, curve->min_x, curve->max_x, curve->min_y, GTK_VALUE_FLOAT(*arg)); - break; + switch (arg_id) + { + case ARG_MIN_X: + xs_curve_set_range(curve, GTK_VALUE_FLOAT(*arg), curve->max_x, curve->min_y, curve->max_y); + break; + case ARG_MAX_X: + xs_curve_set_range(curve, curve->min_x, GTK_VALUE_FLOAT(*arg), curve->min_y, curve->max_y); + break; + case ARG_MIN_Y: + xs_curve_set_range(curve, curve->min_x, curve->max_x, GTK_VALUE_FLOAT(*arg), curve->max_y); + break; + case ARG_MAX_Y: + xs_curve_set_range(curve, curve->min_x, curve->max_x, curve->min_y, GTK_VALUE_FLOAT(*arg)); + break; } } @@ -157,22 +161,23 @@ { XSCurve *curve = XS_CURVE(object); - switch (arg_id) { - case ARG_MIN_X: - GTK_VALUE_FLOAT(*arg) = curve->min_x; - break; - case ARG_MAX_X: - GTK_VALUE_FLOAT(*arg) = curve->max_x; - break; - case ARG_MIN_Y: - GTK_VALUE_FLOAT(*arg) = curve->min_y; - break; - case ARG_MAX_Y: - GTK_VALUE_FLOAT(*arg) = curve->max_y; - break; - default: - arg->type = GTK_TYPE_INVALID; - break; + switch (arg_id) + { + case ARG_MIN_X: + GTK_VALUE_FLOAT(*arg) = curve->min_x; + break; + case ARG_MAX_X: + GTK_VALUE_FLOAT(*arg) = curve->max_x; + break; + case ARG_MIN_Y: + GTK_VALUE_FLOAT(*arg) = curve->min_y; + break; + case ARG_MAX_Y: + GTK_VALUE_FLOAT(*arg) = curve->max_y; + break; + default: + arg->type = GTK_TYPE_INVALID; + break; } } @@ -226,19 +231,20 @@ GTK_STATE_NORMAL, GTK_SHADOW_NONE, NULL, GTK_WIDGET(curve), "curve_bg", 0, 0, - width + RADIUS2, - height + RADIUS2); + width + CP_RADIUS2, + height + CP_RADIUS2); /* Draw the grid */ - for (i = 0; i < 5; i++) { + for (i = 0; i < 5; i++) + { gdk_draw_line(curve->pixmap, style->dark_gc[state], - RADIUS, i * (height / 4.0) + RADIUS, - width + RADIUS, i * (height / 4.0) + RADIUS); + CP_RADIUS, i * (height / 4.0) + CP_RADIUS, + width + CP_RADIUS, i * (height / 4.0) + CP_RADIUS); gdk_draw_line(curve->pixmap, style->dark_gc[state], - i * (width / 4.0) + RADIUS, RADIUS, - i * (width / 4.0) + RADIUS, height + RADIUS); + i * (width / 4.0) + CP_RADIUS, CP_RADIUS, + i * (width / 4.0) + CP_RADIUS, height + CP_RADIUS); } #if 1 @@ -250,35 +256,45 @@ /* Draw each curve segment */ if (curve->nctlpoints > 5) - for (i = 0; i < curve->nctlpoints; i++, ++p0, ++p1, ++p2, ++p3) { + for (i = 0; i < curve->nctlpoints; i++, ++p0, ++p1, ++p2, ++p3) + { gint n; gfloat k1, k2, a, b, c, d, x; if (p1->x == p2->x) continue; - if (p0->x == p1->x && p2->x == p3->x) { + if (p0->x == p1->x && p2->x == p3->x) + { k1 = k2 = (p2->y - p1->y) / (p2->x - p1->x); - } else if (p0->x == p1->x) { + } + else if (p0->x == p1->x) + { k2 = (p3->y - p1->y) / (p3->x - p1->x); k1 = (3 * (p2->y - p1->y) / (p2->x - p1->x) - k2) / 2; - } else if (p2->x == p3->x) { + } + else if (p2->x == p3->x) + { k1 = (p2->y - p0->y) / (p2->x - p0->x); k2 = (3 * (p2->y - p1->y) / (p2->x - p1->x) - k1) / 2; - } else { + } + else + { k1 = (p2->y - p0->y) / (p2->x - p0->x); k2 = (p3->y - p1->y) / (p3->x - p1->x); } xs_cubic_coeff(p1->x, p1->y, p2->x, p2->y, k1, k2, &a, &b, &c, &d); - for (x = p1->x; x <= p2->x; x += res, n++) { + for (x = p1->x; x <= p2->x; x += res, n++) + { gfloat y = ((a * x + b) * x + c) * x + d; gint qx, qy; - qx = RADIUS + xs_project(x, curve->min_x, curve->max_x, width); - qy = RADIUS + xs_project(y, curve->min_y, curve->max_y, height); + qx = CP_RADIUS + xs_project(x, curve->min_x, curve->max_x, width); + qy = CP_RADIUS + xs_project(y, curve->min_y, curve->max_y, height); - if (ox != -1) { + if (ox != -1) + { gdk_draw_line(curve->pixmap, style->fg_gc[state], ox, oy, qx, qy); } @@ -289,7 +305,8 @@ #endif /* Draw control points */ - for (i = 0; i < curve->nctlpoints; ++i) { + for (i = 0; i < curve->nctlpoints; ++i) + { gint x, y; GtkStateType cstate; @@ -300,25 +317,26 @@ x = xs_project(GET_X(i), curve->min_x, curve->max_x, width); y = xs_project(GET_Y(i), curve->min_y, curve->max_y, height); - if (i == curve->grab_point) { + if (i == curve->grab_point) + { cstate = GTK_STATE_SELECTED; gdk_draw_line(curve->pixmap, style->fg_gc[cstate], - x + RADIUS, RADIUS, x + RADIUS, height + RADIUS); + x + CP_RADIUS, CP_RADIUS, x + CP_RADIUS, height + CP_RADIUS); gdk_draw_line(curve->pixmap, style->fg_gc[cstate], - RADIUS, y + RADIUS, width + RADIUS, y + RADIUS); + CP_RADIUS, y + CP_RADIUS, width + CP_RADIUS, y + CP_RADIUS); } else cstate = state; gdk_draw_arc(curve->pixmap, style->fg_gc[cstate], TRUE, - x, y, RADIUS2, RADIUS2, 0, 360 * 64); + x, y, CP_RADIUS2, CP_RADIUS2, 0, 360 * 64); } /* Draw pixmap in the widget */ gdk_draw_pixmap(GTK_WIDGET(curve)->window, - style->fg_gc[state], curve->pixmap, - 0, 0, 0, 0, - width + RADIUS2, - height + RADIUS2); + style->fg_gc[state], curve->pixmap, + 0, 0, 0, 0, + width + CP_RADIUS2, + height + CP_RADIUS2); } @@ -331,162 +349,179 @@ guint distance; w = GTK_WIDGET(curve); - width = w->allocation.width - RADIUS2; - height = w->allocation.height - RADIUS2; + width = w->allocation.width - CP_RADIUS2; + height = w->allocation.height - CP_RADIUS2; if ((width < 0) || (height < 0)) return FALSE; /* get the pointer position */ gdk_window_get_pointer(w->window, &tx, &ty, NULL); - x = CLAMP((tx - RADIUS), 0, width - 1); - y = CLAMP((ty - RADIUS), 0, height - 1); + x = CLAMP((tx - CP_RADIUS), 0, width - 1); + y = CLAMP((ty - CP_RADIUS), 0, height - 1); min_x = curve->min_x; distance = ~0U; - for (i = 0; i < curve->nctlpoints; ++i) { + for (i = 0; i < curve->nctlpoints; ++i) + { cx = xs_project(GET_X(i), min_x, curve->max_x, width); - if ((guint) abs(x - cx) < distance) { + if ((guint) abs(x - cx) < distance) + { distance = abs(x - cx); closest_point = i; } } /* Act based on event type */ - switch (event->type) { - case GDK_CONFIGURE: - if (curve->pixmap) - gdk_pixmap_unref(curve->pixmap); - curve->pixmap = 0; + switch (event->type) + { + case GDK_CONFIGURE: + if (curve->pixmap) + gdk_pixmap_unref(curve->pixmap); + curve->pixmap = 0; + + /* fall through */ - /* fall through */ + case GDK_EXPOSE: + if (!curve->pixmap) + { + curve->pixmap = gdk_pixmap_new(w->window, + w->allocation.width, w->allocation.height, -1); + } + xs_curve_draw(curve, width, height); + break; + + case GDK_BUTTON_PRESS: + gtk_grab_add(widget); - case GDK_EXPOSE: - if (!curve->pixmap) { - curve->pixmap = gdk_pixmap_new(w->window, - w->allocation.width, w->allocation.height, -1); - } - xs_curve_draw(curve, width, height); - break; + bevent = (GdkEventButton *) event; + new_type = GDK_TCROSS; - case GDK_BUTTON_PRESS: - gtk_grab_add(widget); - - bevent = (GdkEventButton *) event; - new_type = GDK_TCROSS; - - if (distance > MIN_DISTANCE) { - /* insert a new control point */ - if (curve->nctlpoints > 0) { - cx = xs_project(GET_X(closest_point), min_x, curve->max_x, width); - if (x > cx) closest_point++; + if (distance > CP_MIN_DISTANCE) + { + /* insert a new control point */ + if (curve->nctlpoints > 0) + { + cx = xs_project(GET_X(closest_point), min_x, curve->max_x, width); + if (x > cx) closest_point++; + } + + curve->nctlpoints++; + + curve->ctlpoints = g_realloc(curve->ctlpoints, + curve->nctlpoints * sizeof(*curve->ctlpoints)); + + for (i = curve->nctlpoints - 1; i > closest_point; --i) + { + memcpy(curve->ctlpoints + i, + curve->ctlpoints + i - 1, + sizeof(*curve->ctlpoints)); + } } - curve->nctlpoints++; - - curve->ctlpoints = g_realloc(curve->ctlpoints, - curve->nctlpoints * sizeof(*curve->ctlpoints)); + curve->grab_point = closest_point; + GET_X(curve->grab_point) = xs_unproject(x, min_x, curve->max_x, width); + GET_Y(curve->grab_point) = xs_unproject(y, curve->min_y, curve->max_y, height); + + xs_curve_draw(curve, width, height); + break; + + case GDK_BUTTON_RELEASE: + { + gint src, dst; - for (i = curve->nctlpoints - 1; i > closest_point; --i) { - memcpy(curve->ctlpoints + i, - curve->ctlpoints + i - 1, - sizeof(*curve->ctlpoints)); + gtk_grab_remove(widget); + + /* delete inactive points: */ + for (src = dst = 0; src < curve->nctlpoints; ++src) + { + if (GET_X(src) >= min_x) + { + memcpy(curve->ctlpoints + dst, + curve->ctlpoints + src, + sizeof(*curve->ctlpoints)); + dst++; + } } - } - - curve->grab_point = closest_point; - GET_X(curve->grab_point) = xs_unproject(x, min_x, curve->max_x, width); - GET_Y(curve->grab_point) = xs_unproject(y, curve->min_y, curve->max_y, height); - xs_curve_draw(curve, width, height); - break; + if (dst < src) + { + curve->nctlpoints -= (src - dst); + if (curve->nctlpoints <= 0) + { + curve->nctlpoints = 1; + GET_X(0) = min_x; + GET_Y(0) = curve->min_y; + xs_curve_draw(curve, width, height); + } + curve->ctlpoints = g_realloc(curve->ctlpoints, + curve->nctlpoints * sizeof(*curve->ctlpoints)); + } - case GDK_BUTTON_RELEASE: - { - gint src, dst; - - gtk_grab_remove(widget); + new_type = GDK_FLEUR; + curve->grab_point = -1; + } + xs_curve_draw(curve, width, height); + break; - /* delete inactive points: */ - for (src = dst = 0; src < curve->nctlpoints; ++src) { - if (GET_X(src) >= min_x) { - memcpy(curve->ctlpoints + dst, - curve->ctlpoints + src, - sizeof(*curve->ctlpoints)); - dst++; - } - } + case GDK_MOTION_NOTIFY: + if (curve->grab_point == -1) + { + /* if no point is grabbed... */ + if (distance <= CP_MIN_DISTANCE) + new_type = GDK_FLEUR; + else + new_type = GDK_TCROSS; + } else { + gint leftbound, rightbound; + + /* drag the grabbed point */ + new_type = GDK_TCROSS; + + leftbound = -CP_MIN_DISTANCE; + if (curve->grab_point > 0) + { + leftbound = xs_project( + GET_X(curve->grab_point-1), + min_x, curve->max_x, width); + } - if (dst < src) { - curve->nctlpoints -= (src - dst); - if (curve->nctlpoints <= 0) { - curve->nctlpoints = 1; - GET_X(0) = min_x; - GET_Y(0) = curve->min_y; + rightbound = width + CP_RADIUS2 + CP_MIN_DISTANCE; + if (curve->grab_point + 1 < curve->nctlpoints) + { + rightbound = xs_project( + GET_X(curve->grab_point+1), + min_x, curve->max_x, width); + } + + if ((tx <= leftbound) || (tx >= rightbound) || + (ty > height + CP_RADIUS2 + CP_MIN_DISTANCE) || (ty < -CP_MIN_DISTANCE)) { + GET_X(curve->grab_point) = min_x - 1.0; + } + else + { + GET_X(curve->grab_point) = + xs_unproject(x, min_x, curve->max_x, width); + GET_Y(curve->grab_point) = + xs_unproject(y, curve->min_y, curve->max_y, height); + } + xs_curve_draw(curve, width, height); } - curve->ctlpoints = g_realloc(curve->ctlpoints, - curve->nctlpoints * sizeof(*curve->ctlpoints)); - } - - new_type = GDK_FLEUR; - curve->grab_point = -1; - } - xs_curve_draw(curve, width, height); - break; - - case GDK_MOTION_NOTIFY: - if (curve->grab_point == -1) { - /* if no point is grabbed... */ - if (distance <= MIN_DISTANCE) - new_type = GDK_FLEUR; - else - new_type = GDK_TCROSS; - } else { - gint leftbound, rightbound; - /* drag the grabbed point */ - new_type = GDK_TCROSS; - - leftbound = -MIN_DISTANCE; - if (curve->grab_point > 0) { - leftbound = xs_project( - GET_X(curve->grab_point-1), - min_x, curve->max_x, width); + /* See if cursor type was changed and update accordingly */ + if (new_type != (GdkCursorType) curve->cursor_type) + { + GdkCursor *cursor; + curve->cursor_type = new_type; + cursor = gdk_cursor_new(curve->cursor_type); + gdk_window_set_cursor(w->window, cursor); + gdk_cursor_destroy(cursor); } + break; - rightbound = width + RADIUS2 + MIN_DISTANCE; - if (curve->grab_point + 1 < curve->nctlpoints) { - rightbound = xs_project( - GET_X(curve->grab_point+1), - min_x, curve->max_x, width); - } - - if ((tx <= leftbound) || (tx >= rightbound) || - (ty > height + RADIUS2 + MIN_DISTANCE) || (ty < -MIN_DISTANCE)) { - GET_X(curve->grab_point) = min_x - 1.0; - } else { - GET_X(curve->grab_point) = - xs_unproject(x, min_x, curve->max_x, width); - GET_Y(curve->grab_point) = - xs_unproject(y, curve->min_y, curve->max_y, height); - } - - xs_curve_draw(curve, width, height); - } - - /* See if cursor type was changed and update accordingly */ - if (new_type != (GdkCursorType) curve->cursor_type) { - GdkCursor *cursor; - curve->cursor_type = new_type; - cursor = gdk_cursor_new(curve->cursor_type); - gdk_window_set_cursor(w->window, cursor); - gdk_cursor_destroy(cursor); - } - break; - - default: - break; + default: + break; } return FALSE; @@ -513,17 +548,18 @@ else height = width / aspect; - gtk_drawing_area_size(GTK_DRAWING_AREA(curve), width + RADIUS2, height + RADIUS2); + gtk_drawing_area_size(GTK_DRAWING_AREA(curve), width + CP_RADIUS2, height + CP_RADIUS2); } static void xs_curve_update(XSCurve *curve) { - if (curve->pixmap) { + if (curve->pixmap) + { gint width, height; - width = GTK_WIDGET(curve)->allocation.width - RADIUS2; - height = GTK_WIDGET(curve)->allocation.height - RADIUS2; + width = GTK_WIDGET(curve)->allocation.width - CP_RADIUS2; + height = GTK_WIDGET(curve)->allocation.height - CP_RADIUS2; xs_curve_draw(curve, width, height); } } @@ -566,7 +602,8 @@ gboolean xs_curve_realloc_data(XSCurve *curve, gint npoints) { - if (npoints != curve->nctlpoints) { + if (npoints != curve->nctlpoints) + { curve->nctlpoints = npoints; curve->ctlpoints = (xs_point_t *) g_realloc(curve->ctlpoints, curve->nctlpoints * sizeof(*curve->ctlpoints)); @@ -598,7 +635,8 @@ GET_X(1) = curve->min_x; GET_Y(1) = curve->min_y; - for (i = 0; i < npoints; i++) { + for (i = 0; i < npoints; i++) + { GET_X(i+2) = points[i].x; GET_Y(i+2) = points[i].y; } @@ -624,7 +662,8 @@ return FALSE; *npoints = n; - for (i = 2; i < curve->nctlpoints - 2; i++) { + for (i = 2; i < curve->nctlpoints - 2; i++) + { (*points)[i].x = GET_X(i); (*points)[i].y = GET_Y(i); }