/* OTAnyGrammarEditor.c
 *
 * Copyright (C) 1997-2002 Paul Boersma
 *
 * 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
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * pb 1998/12/30
 * pb 2002/07/16 GPL
 */

#include "OTAnyGrammarEditor.h"
#include "EditorM.h"

#define OTAnyGrammarEditor_members HyperPage_members \
	int selected;
#define OTAnyGrammarEditor_methods HyperPage_methods
class_create_opaque (OTAnyGrammarEditor, HyperPage)

FORM (OTAnyGrammarEditor, cb_editRanking, "Edit ranking", 0);
	LABEL ("constraint", "");
	REAL ("Ranking value", "100.0");
	REAL ("Disharmony", "100.0");
	OK
OTAnyGrammar grammar = my data;
OTConstraint constraint;
if (my selected < 1 || my selected > grammar -> constraints -> size) return Melder_error ("Select a constraint first.");
constraint = grammar -> constraints -> item [my selected];
SET_STRING ("constraint", OTAnyGrammar_constraintName (grammar, constraint -> which))
SET_REAL ("Ranking value", constraint -> ranking)
SET_REAL ("Disharmony", constraint -> disharmony)
DO
	OTAnyGrammar grammar = my data;
	OTConstraint constraint = grammar -> constraints -> item [my selected];
	Editor_save (me, "Edit ranking");
	constraint -> ranking = GET_REAL ("Ranking value");
	constraint -> disharmony = GET_REAL ("Disharmony");
	Graphics_updateWs (my g);
END

DIRECT (OTAnyGrammarEditor, cb_sortZero)
	Editor_save (me, "Sort zero");
	OTAnyGrammar_sort (my data, 0.0);
	Graphics_updateWs (my g);
END

DIRECT (OTAnyGrammarEditor, cb_OTAnyGrammarEditor_help) Melder_help ("OTAnyGrammarEditor"); END

DIRECT (OTAnyGrammarEditor, cb_OTAnyGrammar_help) Melder_help ("OTAnyGrammar"); END

DIRECT (OTAnyGrammarEditor, cb_OT_learning) Melder_help ("OT learning"); END

static void createMenus (I) {
	iam (OTAnyGrammarEditor);
	inherited (OTAnyGrammarEditor) createMenus (me);
	Editor_addCommand (me, "Edit", "-- edit ot --", 0, NULL);
	Editor_addCommand (me, "Edit", "Sort zero", '0', cb_sortZero);
	Editor_addCommand (me, "Edit", "Edit ranking...", 'E', cb_editRanking);
	Editor_addCommand (me, "Help", "OTAnyGrammarEditor help", '?', cb_OTAnyGrammarEditor_help);
	Editor_addCommand (me, "Help", "OTAnyGrammar help", 0, cb_OTAnyGrammar_help);
	Editor_addCommand (me, "Help", "OT learning", 0, cb_OT_learning);
}

static void draw (I) {
	iam (OTAnyGrammarEditor);
	OTAnyGrammar grammar = my data;
	static char text [1000];
	int icons;
	for (icons = 1; icons <= grammar -> constraints -> size; icons ++) {
		OTConstraint constraint = grammar -> constraints -> item [icons];
		sprintf (text, "%s@@%d|%s@   %.3f   %.3f", icons == my selected ? "\\sp " : "   ", icons,
			OTAnyGrammar_constraintName (grammar, constraint -> which),
			constraint -> ranking, constraint -> disharmony);
		HyperPage_listItem (me, text);
	}
}

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

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

OTAnyGrammarEditor OTAnyGrammarEditor_create (Widget parent, const char *title, OTAnyGrammar grammar) {
	OTAnyGrammarEditor me = new (OTAnyGrammarEditor);
	my data = grammar;
	if (! HyperPage_init (me, parent, title, grammar))
		{ forget (me); return Melder_errorp ("OTAnyGrammarEditor not created."); }
	return me;
}

/* End of file OTAnyGrammarEditor.c */
