From 4c3dea71117319c34bbcd2a00f5421be00f9d994 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Mon, 28 May 2012 02:54:50 -0300 Subject: [PATCH] support switching variables between assignment and key (WIP) This is a little awkward: to change a variable used as key to an assignment, one first had to change the name such that it doesn't clash, hit Enter, and then edit the variable again to change its type. Variable type changes should pick up the edit in progress and allow a type change to also imply acceptance of the variable. --- gui_frame.c | 19 +++++++++++++++-- gui_frame.h | 10 +++++++-- gui_status.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++-- gui_status.h | 6 ++++-- 4 files changed, 85 insertions(+), 8 deletions(-) diff --git a/gui_frame.c b/gui_frame.c index 8d99a28..a56f899 100644 --- a/gui_frame.c +++ b/gui_frame.c @@ -572,7 +572,7 @@ static void add_sep(GtkWidget *box, int size) /* ----- variable name editor ---------------------------------------------- */ -static int find_var_in_frame(const struct frame *frame, const char *name, +int find_var_in_frame(const struct frame *frame, const char *name, const struct var *self) { const struct table *table; @@ -581,7 +581,8 @@ static int find_var_in_frame(const struct frame *frame, const char *name, for (table = frame->tables; table; table = table->next) for (var = table->vars; var; var = var->next) - if (var != self && !strcmp(var->name, name)) + if (var != self && !var->key && + !strcmp(var->name, name)) return 1; for (loop = frame->loops; loop; loop = loop->next) if (&loop->var != self && !strcmp(loop->var.name, name)) @@ -596,6 +597,8 @@ static int validate_var_name(const char *s, void *ctx) if (!is_id(s)) return 0; + if (var->key) + return 1; return !find_var_in_frame(var->frame, s, var); } @@ -651,6 +654,7 @@ static void edit_var(struct var *var, status_set_name("Variable name", "%s", var->name); show_var_value(var, var->frame); edit_nothing(); + edit_var_type(var); edit_unique_with_values(&var->name, validate_var_name, var, set_values, user, max_values, "Variable name. " @@ -658,6 +662,17 @@ static void edit_var(struct var *var, } +static void set_col_values(void *user, const struct value *values, + int n_values); + + +void reselect_var(struct var *var) +{ + + edit_var(var, set_col_values, var, -1); +} + + /* ----- value editor ------------------------------------------------------ */ diff --git a/gui_frame.h b/gui_frame.h index 2922605..e4bfef2 100644 --- a/gui_frame.h +++ b/gui_frame.h @@ -1,8 +1,8 @@ /* * gui_frame.h - GUI, frame window * - * Written 2009, 2010 by Werner Almesberger - * Copyright 2009, 2010 by Werner Almesberger + * Written 2009, 2010, 2012 by Werner Almesberger + * Copyright 2009, 2010, 2012 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 @@ -16,10 +16,16 @@ #include +#include "obj.h" + extern int show_vars; +int find_var_in_frame(const struct frame *frame, const char *name, + const struct var *self); +void reselect_var(struct var *var); + void make_popups(void); void select_frame(struct frame *frame); diff --git a/gui_status.c b/gui_status.c index d10a76c..df4bae4 100644 --- a/gui_status.c +++ b/gui_status.c @@ -1,8 +1,8 @@ /* * gui_status.c - GUI, status area * - * Written 2009-2011 by Werner Almesberger - * Copyright 2009-2011 by Werner Almesberger + * Written 2009-2012 by Werner Almesberger + * Copyright 2009-2012 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 @@ -27,6 +27,7 @@ #include "gui_util.h" #include "gui_style.h" #include "gui_canvas.h" +#include "gui_frame.h" #include "gui.h" #include "gui_status.h" @@ -186,6 +187,59 @@ static void entry_color(GtkWidget *widget, const char *color) } +/* ----- variable type display and change ---------------------------------- */ + + +static struct var *curr_var; +static GtkWidget *var_type; + + +static void show_var_type(void) +{ + gtk_label_set_text(GTK_LABEL(var_type), + curr_var->key ? "key" : "assign"); +} + + +static gboolean var_type_button_press_event(GtkWidget *widget, + GdkEventButton *event, gpointer data) +{ + switch (event->button) { + case 1: + if (curr_var->key && + find_var_in_frame(curr_var->frame, curr_var->name, + curr_var)) + return TRUE; + curr_var->key = !curr_var->key; + show_var_type(); + break; + } + /* + * We can't just redraw() here, because changing the variable type may + * also affect lots of other things. So we change the world and hope + * we end up selecting the same variable afterwards. + */ + change_world(); + reselect_var(curr_var); + return TRUE; +} + + +void edit_var_type(struct var *var) +{ + vacate_widget(status_box_x); + curr_var = var; + var_type = label_in_box_new(NULL, "Variable type. Click to cycle."); + gtk_container_add(GTK_CONTAINER(status_box_x), box_of_label(var_type)); + label_in_box_bg(var_type, COLOR_SELECTOR); + g_signal_connect(G_OBJECT(box_of_label(var_type)), + "button_press_event", G_CALLBACK(var_type_button_press_event), + NULL); + show_var_type(); + gtk_widget_show_all(status_box_x); +} + + /* ----- pad type display and change --------------------------------------- */ diff --git a/gui_status.h b/gui_status.h index 52dde22..0101adc 100644 --- a/gui_status.h +++ b/gui_status.h @@ -1,8 +1,8 @@ /* * gui_status.h - GUI, status area * - * Written 2009, 2010 by Werner Almesberger - * Copyright 2009, 2010 by Werner Almesberger + * Written 2009, 2010, 2012 by Werner Almesberger + * Copyright 2009, 2010, 2012 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 @@ -18,6 +18,7 @@ #include "coord.h" #include "expr.h" +#include "obj.h" enum curr_unit { @@ -31,6 +32,7 @@ enum curr_unit { extern enum curr_unit curr_unit; +void edit_var_type(struct var *var); void edit_pad_type(enum pad_type *type); void edit_unique(const char **s, int (*validate)(const char *s, void *ctx),