From 7674be9b10469dbf12aeaa98af4fb615db3d5a04 Mon Sep 17 00:00:00 2001 From: werner Date: Tue, 4 Aug 2009 00:23:10 +0000 Subject: [PATCH] - fixed stupid realloc(..., len) error - rearranged input area and added vector component edit capability git-svn-id: http://svn.openmoko.org/trunk/eda/fped@5381 99fdad57-331a-0410-800a-d7fa5415bdb3 --- TODO | 3 +- expr.c | 2 +- gui.c | 2 + gui_canvas.c | 8 +- gui_status.c | 205 +++++++++++++++++++++++++++++++-------------------- gui_status.h | 11 ++- inst.c | 19 ++++- 7 files changed, 157 insertions(+), 93 deletions(-) diff --git a/TODO b/TODO index 12a53e9..074b8a6 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,5 @@ Missing features: - populate input area (still needed: mm/mil, rezoom) -- add vec editor (need to be able to edit name, x, and y) -- add obj editor - add table/var/loop editor (missing: add col/row, add/del var/table/loop) - add default unit (combine with grid unit selection ?) - consider adding auto/mm/mil selection for each dimension @@ -16,6 +14,7 @@ Error detection: Style: - make column of entry field greedily consume all unallocated space +- status area looks awful Bugs: - default silk width has no business being hard-coded in obj.c diff --git a/expr.c b/expr.c index b32e2d7..ae9cd87 100644 --- a/expr.c +++ b/expr.c @@ -365,7 +365,7 @@ char *expand(const char *name, const struct frame *frame) value_len = snprintf(num_buf, sizeof(num_buf), "%lg%s", value.n, str_unit(value)); len += value_len; - buf = realloc(buf, len); + buf = realloc(buf, len+1); if (!buf) abort(); strcpy(buf+i, num_buf); diff --git a/gui.c b/gui.c index 119c524..31e77d2 100644 --- a/gui.c +++ b/gui.c @@ -114,6 +114,7 @@ static void edit_var(struct var *var) { inst_select_outside(var, unselect_var); label_in_box_bg(var->widget, COLOR_VAR_EDITING); + status_set_type_entry("name ="); status_set_name(var->name); edit_unique(&var->name, validate_var_name, var); } @@ -491,6 +492,7 @@ static void edit_frame(struct frame *frame) { inst_select_outside(frame, unselect_frame); label_in_box_bg(frame->label, COLOR_FRAME_EDITING); + status_set_type_entry("name ="); status_set_name(frame->name); edit_unique(&frame->name, validate_frame_name, frame); } diff --git a/gui_canvas.c b/gui_canvas.c index fe5ffda..f7d2f9a 100644 --- a/gui_canvas.c +++ b/gui_canvas.c @@ -39,10 +39,10 @@ static void update_zoom(void) static void update_pos(struct coord pos) { - status_set_sys_pos("X %5.2lf Y %5.2lf mm", - units_to_mm(pos.x), units_to_mm(pos.y)); - status_set_user_pos("x %5.2lf y %5.2lf mm", - units_to_mm(pos.x-user_origin.x), units_to_mm(pos.y-user_origin.y)); + status_set_sys_x("X %5.2lf" , units_to_mm(pos.x)); + status_set_sys_y("Y %5.2lf" , units_to_mm(pos.y)); + status_set_user_x("x %5.2lf", units_to_mm(pos.x-user_origin.x)); + status_set_user_y("y %5.2lf", units_to_mm(pos.y-user_origin.y)); } diff --git a/gui_status.c b/gui_status.c index c26e4b6..f49fa4d 100644 --- a/gui_status.c +++ b/gui_status.c @@ -32,17 +32,17 @@ struct edit_ops { int (*activate)(GtkWidget *widget, const char *s, void *ctx); }; -static struct edit_ops *edit_ops = NULL; -static void *edit_ctx; - /* ----- setter functions -------------------------------------------------- */ static GtkWidget *status_name, *status_entry; +static GtkWidget *status_type_x, *status_type_y, *status_type_entry; +static GtkWidget *status_entry_x, *status_entry_y; static GtkWidget *status_x, *status_y; static GtkWidget *status_r, *status_angle; -static GtkWidget *status_sys_pos, *status_user_pos; +static GtkWidget *status_sys_x, *status_sys_y; +static GtkWidget *status_user_x, *status_user_y; static GtkWidget *status_zoom, *status_grid; static GtkWidget *status_msg; @@ -67,13 +67,18 @@ static void set_label(GtkWidget *label, const char *fmt, va_list ap) va_end(ap); \ } +SETTER(type_x) +SETTER(type_y) +SETTER(type_entry) SETTER(name) SETTER(x) SETTER(y) SETTER(r) SETTER(angle) -SETTER(sys_pos) -SETTER(user_pos) +SETTER(sys_x) +SETTER(sys_y) +SETTER(user_x) +SETTER(user_y) SETTER(zoom) SETTER(grid) @@ -83,18 +88,35 @@ SETTER(grid) void status_set_xy(struct coord coord) { - status_set_x("x = %5.2f mm", units_to_mm(coord.x)); - status_set_y("y = %5.2f mm", units_to_mm(coord.y)); + /* do dX/dY etc. stuff later */ + status_set_type_x("X ="); + status_set_type_y("Y ="); + + status_set_x("%5.2f mm", units_to_mm(coord.x)); + status_set_y("%5.2f mm", units_to_mm(coord.y)); } -static void entry_color(const char *color) +static void entry_color(GtkWidget *widget, const char *color) { GdkColor col; col = get_color(color); - gtk_widget_modify_base(GTK_WIDGET(status_entry), - GTK_STATE_NORMAL, &col); + gtk_widget_modify_base(widget, GTK_STATE_NORMAL, &col); +} + + +/* ----- helper functions -------------------------------------------------- */ + + +static void setup_edit(GtkWidget *widget, const char *s, + struct edit_ops *ops, void *ctx) +{ + gtk_entry_set_text(GTK_ENTRY(widget), s); + entry_color(widget, COLOR_EDIT_ASIS); + gtk_widget_show(widget); + gtk_object_set_data(GTK_OBJECT(widget), "edit-ops", ops); + gtk_object_set_data(GTK_OBJECT(widget), "edit-ctx", ctx); } @@ -114,11 +136,11 @@ static int unique_changed(GtkWidget *widget, const char *s, void *ctx) int ok; if (!strcmp(s, *unique_ctx->s)) { - entry_color(COLOR_EDIT_ASIS); + entry_color(widget, COLOR_EDIT_ASIS); return 1; } ok = !unique_ctx->validate || unique_ctx->validate(s, unique_ctx->ctx); - entry_color(ok ? COLOR_EDIT_GOOD : COLOR_EDIT_BAD); + entry_color(widget, ok ? COLOR_EDIT_GOOD : COLOR_EDIT_BAD); return ok; } @@ -131,7 +153,7 @@ static int unique_activate(GtkWidget *widget, const char *s, void *ctx) unique_ctx->validate && !unique_ctx->validate(s, unique_ctx->ctx)) return 0; *unique_ctx->s = unique(s); - entry_color(COLOR_EDIT_ASIS); + entry_color(widget, COLOR_EDIT_ASIS); return 1; } @@ -150,11 +172,7 @@ void edit_unique(const char **s, int (*validate)(const char *s, void *ctx), unique_ctx.s = s; unique_ctx.validate = validate; unique_ctx.ctx = ctx; - edit_ops = &edit_ops_unique; - edit_ctx = &unique_ctx; - gtk_entry_set_text(GTK_ENTRY(status_entry), *s); - entry_color(COLOR_EDIT_ASIS); - gtk_widget_show(status_entry); + setup_edit(status_entry, *s, &edit_ops_unique, &unique_ctx); } @@ -167,14 +185,14 @@ static int unique_null_changed(GtkWidget *widget, const char *s, void *ctx) int ok; if (!strcmp(s, *unique_ctx->s ? *unique_ctx->s : "")) { - entry_color(COLOR_EDIT_ASIS); + entry_color(widget, COLOR_EDIT_ASIS); return 1; } ok = !*s; if (!ok) ok = !unique_ctx->validate || unique_ctx->validate(s, unique_ctx->ctx); - entry_color(ok ? COLOR_EDIT_GOOD : COLOR_EDIT_BAD); + entry_color(widget, ok ? COLOR_EDIT_GOOD : COLOR_EDIT_BAD); return ok; } @@ -191,7 +209,7 @@ static int unique_null_activate(GtkWidget *widget, const char *s, void *ctx) return 0; *unique_ctx->s = unique(s); } - entry_color(COLOR_EDIT_ASIS); + entry_color(widget, COLOR_EDIT_ASIS); return 1; } @@ -210,11 +228,8 @@ void edit_unique_null(const char **s, unique_ctx.s = s; unique_ctx.validate = validate; unique_ctx.ctx = ctx; - edit_ops = &edit_ops_null_unique; - edit_ctx = &unique_ctx; - gtk_entry_set_text(GTK_ENTRY(status_entry), *s ? *s : ""); - entry_color(COLOR_EDIT_ASIS); - gtk_widget_show(status_entry); + setup_edit(status_entry, *s ? *s : "", + &edit_ops_null_unique, &unique_ctx); } @@ -234,11 +249,11 @@ static int name_changed(GtkWidget *widget, const char *s, void *ctx) int ok; if (!strcmp(s, *name_ctx->s)) { - entry_color(COLOR_EDIT_ASIS); + entry_color(widget, COLOR_EDIT_ASIS); return 1; } ok = !name_ctx->validate || name_ctx->validate(s, name_ctx->ctx); - entry_color(ok ? COLOR_EDIT_GOOD : COLOR_EDIT_BAD); + entry_color(widget, ok ? COLOR_EDIT_GOOD : COLOR_EDIT_BAD); return ok; } @@ -251,7 +266,7 @@ static int name_activate(GtkWidget *widget, const char *s, void *ctx) return 0; free(*name_ctx->s); *name_ctx->s = stralloc(s); - entry_color(COLOR_EDIT_ASIS); + entry_color(widget, COLOR_EDIT_ASIS); return 1; } @@ -269,11 +284,7 @@ void edit_name(char **s, int (*validate)(const char *s, void *ctx), void *ctx) name_ctx.s = s; name_ctx.validate = validate; name_ctx.ctx = ctx; - edit_ops = &edit_ops_name; - edit_ctx = &name_ctx; - gtk_entry_set_text(GTK_ENTRY(status_entry), *s); - entry_color(COLOR_EDIT_ASIS); - gtk_widget_show(status_entry); + setup_edit(status_entry, *s, &edit_ops_name, &name_ctx); } @@ -293,10 +304,10 @@ static int expr_changed(GtkWidget *widget, const char *s, void *ctx) expr = try_parse_expr(s); if (!expr) { - entry_color(COLOR_EDIT_BAD); + entry_color(widget, COLOR_EDIT_BAD); return 0; } - entry_color(COLOR_EDIT_GOOD); + entry_color(widget, COLOR_EDIT_GOOD); free_expr(expr); return 1; } @@ -313,7 +324,7 @@ static int expr_activate(GtkWidget *widget, const char *s, void *ctx) if (*anchor) free_expr(*anchor); *anchor = expr; - entry_color(COLOR_EDIT_ASIS); + entry_color(widget, COLOR_EDIT_ASIS); return 1; } @@ -324,17 +335,31 @@ static struct edit_ops edit_ops_expr = { }; -void edit_expr(struct expr **expr) +static void edit_any_expr(GtkWidget *widget, struct expr **expr) { char *s; - edit_ops = &edit_ops_expr; - edit_ctx = expr; s = unparse(*expr); - gtk_entry_set_text(GTK_ENTRY(status_entry), s); + setup_edit(widget, s, &edit_ops_expr, expr); free(s); - entry_color(COLOR_EDIT_ASIS); - gtk_widget_show(status_entry); +} + + +void edit_expr(struct expr **expr) +{ + edit_any_expr(status_entry, expr); +} + + +void edit_x(struct expr **expr) +{ + edit_any_expr(status_entry_x, expr); +} + + +void edit_y(struct expr **expr) +{ + edit_any_expr(status_entry_y, expr); } @@ -344,9 +369,13 @@ void edit_expr(struct expr **expr) static gboolean changed(GtkWidget *widget, GdkEventMotion *event, gpointer data) { - if (edit_ops && edit_ops->changed) - edit_ops->changed(widget, - gtk_entry_get_text(GTK_ENTRY(widget)), edit_ctx); + struct edit_ops *ops = + gtk_object_get_data(GTK_OBJECT(widget), "edit-ops"); + void *ctx = gtk_object_get_data(GTK_OBJECT(widget), "edit-ctx"); + + if (ops && ops->changed) + ops->changed(widget, gtk_entry_get_text(GTK_ENTRY(widget)), + ctx); return TRUE; } @@ -354,9 +383,13 @@ static gboolean changed(GtkWidget *widget, GdkEventMotion *event, static gboolean activate(GtkWidget *widget, GdkEventMotion *event, gpointer data) { - if (edit_ops && edit_ops->activate) - if (edit_ops->activate(widget, - gtk_entry_get_text(GTK_ENTRY(widget)), edit_ctx)) { + struct edit_ops *ops = + gtk_object_get_data(GTK_OBJECT(widget), "edit-ops"); + void *ctx = gtk_object_get_data(GTK_OBJECT(widget), "edit-ctx"); + + if (ops && ops->activate) + if (ops->activate(widget, + gtk_entry_get_text(GTK_ENTRY(widget)), ctx)) { inst_deselect(); change_world(); } @@ -366,8 +399,9 @@ static gboolean activate(GtkWidget *widget, GdkEventMotion *event, void edit_nothing(void) { - edit_ops = NULL; gtk_widget_hide(status_entry); + gtk_widget_hide(status_entry_x); + gtk_widget_hide(status_entry_y); } @@ -418,54 +452,65 @@ static GtkWidget *add_label(GtkWidget *tab, int col, int row) } +static GtkWidget *add_entry(GtkWidget *tab, int col, int row) +{ + GtkWidget *entry; + + entry = gtk_entry_new(); + gtk_entry_set_has_frame(GTK_ENTRY(entry), FALSE); + gtk_table_attach_defaults(GTK_TABLE(tab), entry, + col, col+1, row, row+1); + + g_signal_connect(G_OBJECT(entry), "changed", + G_CALLBACK(changed), entry); + g_signal_connect(G_OBJECT(entry), "activate", + G_CALLBACK(activate), entry); + + return entry; +} + + void make_status_area(GtkWidget *vbox) { - GtkWidget *tab, *hbox; + GtkWidget *tab; - tab = gtk_table_new(5, 2, FALSE); + tab = gtk_table_new(6, 3, FALSE); gtk_box_pack_start(GTK_BOX(vbox), tab, FALSE, TRUE, 0); - /* name and input */ + /* types */ - status_name = add_label(tab, 0, 0); - - /* - * @@@ this should make the entry consume all available space. alas, it - * doesn't work like that :-( - */ - hbox = gtk_hbox_new(TRUE, 0); - gtk_table_attach_defaults(GTK_TABLE(tab), hbox, - 0, 1, 1, 2); - - status_entry = gtk_entry_new(); - gtk_box_pack_start(GTK_BOX(hbox), status_entry, TRUE, TRUE, 0); - - gtk_entry_set_has_frame(GTK_ENTRY(status_entry), FALSE); - - g_signal_connect(G_OBJECT(status_entry), "changed", - G_CALLBACK(changed), status_entry); - g_signal_connect(G_OBJECT(status_entry), "activate", - G_CALLBACK(activate), status_entry); + status_type_x = add_label(tab, 0, 0); + status_type_y = add_label(tab, 0, 1); + status_type_entry = add_label(tab, 0, 2); /* x / y */ status_x = add_label(tab, 1, 0); + status_entry_x = add_entry(tab, 2, 0); status_y = add_label(tab, 1, 1); + status_entry_y = add_entry(tab, 2, 1); - /* r / angle */ + /* name and input */ - status_r = add_label(tab, 2, 0); - status_angle = add_label(tab, 2, 1); + status_name = add_label(tab, 1, 2); + status_entry = add_entry(tab, 2, 2); /* sys / user pos */ - status_sys_pos = add_label(tab, 3, 0); - status_user_pos = add_label(tab, 3, 1); + status_sys_x = add_label(tab, 3, 0); + status_sys_y = add_label(tab, 3, 1); + status_user_x = add_label(tab, 4, 0); + status_user_y = add_label(tab, 4, 1); + + /* r / angle */ + + status_r = add_label(tab, 3, 2); + status_angle = add_label(tab, 4, 2); /* zoom / grid */ - status_zoom = add_label(tab, 4, 0); - status_grid = add_label(tab, 4, 1); + status_zoom = add_label(tab, 5, 0); + status_grid = add_label(tab, 5, 1); /* message bar */ diff --git a/gui_status.h b/gui_status.h index 8d68d22..0ee3786 100644 --- a/gui_status.h +++ b/gui_status.h @@ -26,15 +26,22 @@ void edit_unique_null(const char **s, int (*validate)(const char *s, void *ctx), void *ctx); void edit_name(char **s, int (*validate)(const char *s, void *ctx), void *ctx); void edit_expr(struct expr **expr); +void edit_x(struct expr **expr); +void edit_y(struct expr **expr); void edit_nothing(void); +void status_set_type_x(const char *fmt, ...); +void status_set_type_y(const char *fmt, ...); +void status_set_type_entry(const char *fmt, ...); void status_set_name(const char *fmt, ...); void status_set_x(const char *fmt, ...); void status_set_y(const char *fmt, ...); void status_set_r(const char *fmt, ...); void status_set_angle(const char *fmt, ...); -void status_set_sys_pos(const char *fmt, ...); -void status_set_user_pos(const char *fmt, ...); +void status_set_sys_x(const char *fmt, ...); +void status_set_sys_y(const char *fmt, ...); +void status_set_user_x(const char *fmt, ...); +void status_set_user_y(const char *fmt, ...); void status_set_zoom(const char *fmt, ...); void status_set_grid(const char *fmt, ...); diff --git a/inst.c b/inst.c index eae251a..2670247 100644 --- a/inst.c +++ b/inst.c @@ -173,6 +173,9 @@ void inst_deselect(void) if (selected_inst) set_path(0); deselect_outside(); + status_set_type_x(""); + status_set_type_y(""); + status_set_type_entry(""); status_set_name(""); status_set_x(""); status_set_y(""); @@ -198,8 +201,10 @@ static void rect_status(struct coord a, struct coord b, unit_type width) status_set_angle("a = %3.1f deg", angle); } status_set_r("r = %5.2f mm", hypot(units_to_mm(d.x), units_to_mm(d.y))); - if (width != -1) - status_set_name("width = %5.2f mm", units_to_mm(width)); + if (width != -1) { + status_set_type_entry("width ="); + status_set_name("%5.2f mm", units_to_mm(width)); + } } @@ -280,9 +285,12 @@ static int validate_vec_name(const char *s, void *ctx) static void vec_op_select(struct inst *self) { + status_set_type_entry("ref ="); status_set_name(self->vec->name ? self->vec->name : ""); rect_status(self->base, self->u.rect.end, -1); edit_unique_null(&self->vec->name, validate_vec_name, self->vec); + edit_x(&self->vec->x); + edit_y(&self->vec->y); } @@ -414,6 +422,7 @@ static int validate_pad_name(const char *s, void *ctx) static void pad_op_select(struct inst *self) { + status_set_type_entry("label ="); status_set_name(self->u.name); rect_status(self->bbox.min, self->bbox.max, -1); edit_name(&self->obj->u.pad.name, validate_pad_name, NULL); @@ -459,7 +468,8 @@ static void arc_op_select(struct inst *self) self->u.arc.a1 == self->u.arc.a2 ? 360 : self->u.arc.a2-self->u.arc.a1); status_set_r("r = %5.2f mm", units_to_mm(self->u.arc.r)); - status_set_name("width = %5.2f mm", units_to_mm(self->u.arc.width)); + status_set_type_entry("width ="); + status_set_name("%5.2f mm", units_to_mm(self->u.arc.width)); edit_expr(&self->obj->u.arc.width); } @@ -516,7 +526,8 @@ static void meas_op_debug(struct inst *self) static void meas_op_select(struct inst *self) { rect_status(self->bbox.min, self->bbox.max, -1); - status_set_name("offset = %5.2f mm", units_to_mm(self->u.meas.offset)); + status_set_type_entry("width ="); + status_set_name("%5.2f mm", units_to_mm(self->u.meas.offset)); edit_expr(&self->obj->u.meas.offset); }