From 7eb3ac5ba29269662534a6b30b88e2b56996f05d Mon Sep 17 00:00:00 2001 From: werner Date: Sun, 30 May 2010 09:04:26 +0000 Subject: [PATCH] When dragging an endpoint of an inverted measurement, we didn't consider that inst->base and inst->u.meas.end are swapped, causing fped to crash in vec_at. Also introduced a universal swap() function. - util.h (swap): new swap function for arguments of any type - gui_meas.c (begin_drag_move_meas, draw_move_meas): swap the endpoints if moving an inverted measurement - coord.c (sort_coord): use swap() instead of swap_coord - coord.h, coord.c (swap_coord): removed - gui_util.c (save_pix_buf): use swap() instead of open-coding the swap - kicad.c (kicad_centric): use sort_coord instead of "manually" sorting the coordinates git-svn-id: http://svn.openmoko.org/trunk/eda/fped@5968 99fdad57-331a-0410-800a-d7fa5415bdb3 --- coord.c | 19 +++++-------------- coord.h | 5 ++--- gui_meas.c | 18 +++++++++++++++--- gui_util.c | 15 ++++----------- kicad.c | 12 +----------- util.h | 9 +++++++-- 6 files changed, 34 insertions(+), 44 deletions(-) diff --git a/coord.c b/coord.c index b9ee3e2..9bf2b90 100644 --- a/coord.c +++ b/coord.c @@ -1,8 +1,8 @@ /* * coord.c - Coordinate representation and basic operations * - * Written 2009 by Werner Almesberger - * Copyright 2009 by Werner Almesberger + * Written 2009, 2010 by Werner Almesberger + * Copyright 2009, 2010 by Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -13,6 +13,7 @@ #include +#include "util.h" #include "coord.h" @@ -153,22 +154,12 @@ double theta(struct coord c, struct coord p) /* ----- sorting coordinates ----------------------------------------------- */ -void swap_coord(unit_type *a, unit_type *b) -{ - unit_type tmp; - - tmp = *a; - *a = *b; - *b = tmp; -} - - void sort_coord(struct coord *min, struct coord *max) { if (min->x > max->x) - swap_coord(&min->x, &max->x); + swap(min->x, max->x); if (min->y > max->y) - swap_coord(&min->y, &max->y); + swap(min->y, max->y); } diff --git a/coord.h b/coord.h index 6955160..dd9f235 100644 --- a/coord.h +++ b/coord.h @@ -1,8 +1,8 @@ /* * coord.h - Coordinate representation and basic operations * - * Written 2009 by Werner Almesberger - * Copyright 2009 by Werner Almesberger + * Written 2009, 2010 by Werner Almesberger + * Copyright 2009, 2010 by Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -87,7 +87,6 @@ struct coord rotate_r(struct coord c, unit_type r, double angle); double theta_vec(struct coord v); double theta(struct coord c, struct coord p); -void swap_coord(unit_type *a, unit_type *b); void sort_coord(struct coord *min, struct coord *max); unit_type dist_point(struct coord a, struct coord b); diff --git a/gui_meas.c b/gui_meas.c index 5776534..89b1f5f 100644 --- a/gui_meas.c +++ b/gui_meas.c @@ -189,7 +189,8 @@ static void meas_highlight_b(void) struct pix_buf *draw_move_meas(struct inst *inst, struct coord pos, int i) { - return draw_move_line_common(inst, inst->u.meas.end, pos, i); + return draw_move_line_common(inst, inst->u.meas.end, pos, + inst->obj->u.meas.inverted ? 1-i : i); } @@ -349,6 +350,7 @@ static struct inst *vec_at(const struct vec *vec, struct coord pos) void begin_drag_move_meas(struct inst *inst, int i) { const struct meas *meas = &inst->obj->u.meas; + struct coord a, b; switch (meas->type) { case mt_xy_next: @@ -367,14 +369,24 @@ void begin_drag_move_meas(struct inst *inst, int i) abort(); } highlight = meas_highlight_b; + + /* + * We're setting up the same conditions as after picking the first + * point when making a new measurement. Thus, we set meas_inst to the + * vector to the endpoint we're not moving. + */ + a = inst->base; + b = inst->u.meas.end; + if (inst->obj->u.meas.inverted) + swap(a, b); switch (i) { case 0: mode = meas->type < 3 ? next_to_min : max_to_min; - meas_inst = vec_at(inst->obj->u.meas.high, inst->u.meas.end); + meas_inst = vec_at(inst->obj->u.meas.high, b); break; case 1: mode = min_to_next_or_max; - meas_inst = vec_at(inst->obj->base, inst->base); + meas_inst = vec_at(inst->obj->base, a); break; default: abort(); diff --git a/gui_util.c b/gui_util.c index f22c009..36fe66d 100644 --- a/gui_util.c +++ b/gui_util.c @@ -65,19 +65,12 @@ struct pix_buf *save_pix_buf(GdkDrawable *da, int xa, int ya, int xb, int yb, int border) { struct pix_buf *buf; - int tmp; int w, h; - if (xa > xb) { - tmp = xa; - xa = xb; - xb = tmp; - } - if (ya > yb) { - tmp = ya; - ya = yb; - yb = tmp; - } + if (xa > xb) + swap(xa, xb); + if (ya > yb) + swap(ya, yb); buf = alloc_type(struct pix_buf); buf->da = da; buf->x = xa-border; diff --git a/kicad.c b/kicad.c index 4eb4b50..9ab863e 100644 --- a/kicad.c +++ b/kicad.c @@ -30,23 +30,13 @@ static void kicad_centric(struct coord a, struct coord b, struct coord *center, struct coord *size) { struct coord min, max; - unit_type tmp; min.x = units_to_kicad(a.x); min.y = units_to_kicad(a.y); max.x = units_to_kicad(b.x); max.y = units_to_kicad(b.y); - if (min.x > max.x) { - tmp = min.x; - min.x = max.x; - max.x = tmp; - } - if (min.y > max.y) { - tmp = min.y; - min.y = max.y; - max.y = tmp; - } + sort_coord(&min, &max); size->x = max.x-min.x; size->y = max.y-min.y; diff --git a/util.h b/util.h index 426f0ae..6574030 100644 --- a/util.h +++ b/util.h @@ -1,8 +1,8 @@ /* * util.h - Common utility functions * - * Written 2006, 2009 by Werner Almesberger - * Copyright 2006, 2009 Werner Almesberger + * Written 2006, 2009, 2010 by Werner Almesberger + * Copyright 2006, 2009, 2010 Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -51,6 +51,11 @@ strnalloc_tmp[n] = 0; \ strnalloc_tmp; }) +#define swap(a, b) \ + ({ typeof(a) swap_tmp = (a); \ + (a) = (b); \ + (b) = swap_tmp; }) + char *stralloc_vprintf(const char *fmt, va_list ap); char *stralloc_printf(const char *fmt, ...)