/* OTGrammarEditor.c */
/* Paul Boersma, August 21, 2001 */

#include "OTGrammarEditor.h"
#include "EditorM.h"

#define OTGrammarEditor_members HyperPage_members \
	long selected;
#define OTGrammarEditor_methods HyperPage_methods
class_create_opaque (OTGrammarEditor, HyperPage)

FORM (OTGrammarEditor, cb_evaluate, "Evaluate", 0)
	REAL ("Noise", "2.0")
	OK
DO
	Editor_save (me, "Evaluate");
	OTGrammar_newDisharmonies (my data, GET_REAL ("Noise"));
	Graphics_updateWs (my g);
END

DIRECT (OTGrammarEditor, cb_evaluate_noise_2_0)
	Editor_save (me, "Evaluate (noise 2.0)");
	OTGrammar_newDisharmonies (my data, 2.0);
	Graphics_updateWs (my g);
END

DIRECT (OTGrammarEditor, cb_evaluate_zeroNoise)
	Editor_save (me, "Evaluate (zero noise)");
	OTGrammar_newDisharmonies (my data, 0.0);
	Graphics_updateWs (my g);
END

FORM (OTGrammarEditor, cb_editRanking, "Edit ranking", 0);
	LABEL ("constraint", "");
	REAL ("Ranking value", "100.0");
	REAL ("Disharmony", "100.0");
	OK
OTGrammar ot = my data;
OTGrammarConstraint constraint;
if (my selected < 1 || my selected > ot -> numberOfConstraints) return Melder_error ("Select a constraint first.");
constraint = & ot -> constraints [ot -> index [my selected]];
SET_STRING ("constraint", constraint -> name)
SET_REAL ("Ranking value", constraint -> ranking)
SET_REAL ("Disharmony", constraint -> disharmony)
DO
	OTGrammar ot = my data;
	OTGrammarConstraint constraint = & ot -> constraints [ot -> index [my selected]];
	Editor_save (me, "Edit ranking");
	constraint -> ranking = GET_REAL ("Ranking value");
	constraint -> disharmony = GET_REAL ("Disharmony");
	OTGrammar_sort (ot);
	Graphics_updateWs (my g);
END

DIRECT (OTGrammarEditor, cb_OTGrammarEditor_help) Melder_help ("OTGrammarEditor"); END

DIRECT (OTGrammarEditor, cb_OTGrammar_help) Melder_help ("OTGrammar"); END

DIRECT (OTGrammarEditor, cb_OTLearningTutorial) Melder_help ("OT learning"); END

static void createMenus (I) {
	iam (OTGrammarEditor);
	inherited (OTGrammarEditor) createMenus (me);
	Editor_addCommand (me, "Edit", "-- edit ot --", 0, NULL);
	Editor_addCommand (me, "Edit", "Evaluate...", 0, cb_evaluate);
	Editor_addCommand (me, "Edit", "Evaluate (noise 2.0)", '2', cb_evaluate_noise_2_0);
	Editor_addCommand (me, "Edit", "Evaluate (zero noise)", '0', cb_evaluate_zeroNoise);
	Editor_addCommand (me, "Edit", "Edit ranking...", 'E', cb_editRanking);
	Editor_addCommand (me, "Help", "OTGrammarEditor help", '?', cb_OTGrammarEditor_help);
	Editor_addCommand (me, "Help", "OTGrammar help", 0, cb_OTGrammar_help);
	Editor_addCommand (me, "Help", "OT learning tutorial", 0, cb_OTLearningTutorial);
}

static OTGrammar drawTableau_ot;
static const char *drawTableau_input;
static void drawTableau (Graphics g) {
	OTGrammar_drawTableau (drawTableau_ot, g, drawTableau_input);
}

static void draw (I) {
	iam (OTGrammarEditor);
	OTGrammar ot = my data;
	static char text [1000];
	long icons, itab;
	HyperPage_listItem (me, "\t\t      %%ranking value\t      %disharmony");
	for (icons = 1; icons <= ot -> numberOfConstraints; icons ++) {
		OTGrammarConstraint constraint = & ot -> constraints [ot -> index [icons]];
		sprintf (text, "\t%s@@%ld|%s@\t      %.3f\t      %.3f", icons == my selected ? "\\sp " : "   ", icons,
			constraint -> name, constraint -> ranking, constraint -> disharmony);
		HyperPage_listItem (me, text);
	}
	Graphics_setAtSignIsLink (my g, FALSE);
	for (itab = 1; itab <= ot -> numberOfTableaus; itab ++) {
		OTGrammarTableau tableau = & ot -> tableaus [itab];
		double rowHeight = 0.25;
		double tableauHeight = rowHeight * (tableau -> numberOfCandidates + 2);
		drawTableau_ot = ot;
		drawTableau_input = tableau -> input;
		HyperPage_picture (me, 20, tableauHeight, drawTableau);
	}
	Graphics_setAtSignIsLink (my g, TRUE);
}

static int goToPage (I, const char *title) {
	iam (OTGrammarEditor);
	if (! title) return 1;
	my selected = atoi (title);
	return 1;
}

class_methods (OTGrammarEditor, HyperPage)
	class_method (createMenus)
	class_method (draw)
	us -> editable = TRUE;
	class_method (goToPage)
class_methods_end

OTGrammarEditor OTGrammarEditor_create (Widget parent, const char *title, OTGrammar ot) {
	OTGrammarEditor me = new (OTGrammarEditor);
	my data = ot;
	if (! HyperPage_init (me, parent, title, ot))
		{ forget (me); return Melder_errorp ("OTGrammarEditor not created."); }
	return me;
}

/* End of file OTGrammarEditor.c */
