# preset a dialog box and set the attribute
function quick_attr_tolerance(grp, attr_key,   TOLS,tols,i,currv)
{
	# this one is global, selecting qa_tolerance_remember() will store selection here
	lastval = 0;

	# Common tolerance values
	TOLS[0] = "0.1%";
	TOLS[1] = "1%";
	TOLS[2] = "5%";
	TOLS[3] = "10%";

	currv = propget("objbin", grp, "a/tolerance");

	# Have it in an str as well for the enum, plus look up init_idx
	# using the current value of the attribute
	tols = TOLS[0];
	for(i = 1; TOLS[i] != ""; i++) {
		tols = tols @ "\t" @ TOLS[i];
		if (currv == TOLS[i])
			lastval = i;
	}

	# create the dialog box
	dad("toldi", "new");
	dad("toldi", "begin_vbox");
		dad("toldi", "label", "Choose tolerance:");
		w_tol = dad("toldi", "enum", tols);
			dad("toldi", "onchange", "qa_tolerance_remember()");
			dad("toldi", "default", lastval);
		dad("toldi", "button_closes", "cancel", -1, "ok", 0);
	dad("toldi", "end");

	# run the dialog box as modal, so that the attribute can not be changed,
	# elsewhere, a second instance can not be open
	res = dad("toldi", "run_modal", "Tolerance value", "");

	# res is -1 when cancel is clicked and 0 for ok, as set in "button_closes"
	# above; when clicked ok, fetch result and set the attribute accordingly
	if (res == 0)
		propset("objbin", grp, "a/tolerance", TOLS[lastval]);

	# Return a 1 to indicate everything went well and trigger a refresh
	# Return 0 to indicate no error but no change
	# Return -1 to indicate error
	return 1;
}

# Executed when a new tolerance value is selected; remembers the index of
# the new value in a global fawk variable
function qa_tolerance_remember()
{
	lastval = dad("toldi", "get", w_tol);
}

# Runs once when the script is ran
function main(ARGV) {
	# register actions: the first is called directly by the user, the second
	# is called back from the dialog box
	fgw_func_reg(quick_attr_tolerance);
	fgw_func_reg(qa_tolerance_remember);

	# create a menu with the hotkey {a t}
	cookie = scriptcookie();
	createmenu("/main_menu/Plugins/script/assisted edit symbol tolerance", "quickattr(object, tolerance)", "Invokes assisted/quick attribute edit for the tolerance attribute", cookie, "<key>a;<key>t");
}
