
#include "quiz.h"

extern LONG		AutoRequest ();

#define REQ_WIDTH	300
#define REQ_HEIGHT	50

struct IntuiText yes_text = {
	FP , BP , COMPLEMENT|JAM1 , 5 , 3 , NULL , (UBYTE*) "Yes" , NULL
};

struct IntuiText no_text = {
	FP , BP , COMPLEMENT|JAM1 , 5 , 3 , NULL , (UBYTE*) "No" , NULL
};

struct IntuiText ok_text = {
	FP , BP , COMPLEMENT|JAM1 , 5 , 3 , NULL , (UBYTE*) "OK" , NULL
};

char body_buf[ 100 ];
struct IntuiText body_text = {
	FP , BP , COMPLEMENT|JAM1 , 10 , 7 , NULL , (UBYTE*) &body_buf[0] , NULL
};


do_gadget ( gadget )
struct Gadget *gadget;
{
	char *p;
	char *word;
	char *trans;
	char *error_at;
	char *sentence;
	int i;
	struct thai_phrase *phrase;

	if ( isthai ( (int)gadget->GadgetID ) ) {

		switch ( entry ) {

		case TW_ENTRY :
			edit_thai ( screen_word.thai , TW_ENTRY , (char)gadget->GadgetID );
			break;

		case TS_ENTRY :
			edit_thai ( screen_sentence.thai , TS_ENTRY , (char)gadget->GadgetID );
			break;

		default :
			/* off_thai_keyboard (); */
			break;
		}
	}
	else {

		switch ( gadget->GadgetID ) {

		case CHOOSE_SENTENCE :
			random_sentence ();
			scanning = CHOOSE_SENTENCE;
			wordptr = chosen_sentence->thai;
			reset_next_prev ();
			screen_sentence.thai[0] = '\0';
			screen_sentence.phonetic[0] = '\0';
			screen_sentence.english[0] = '\0';
			show_sentence = 0;
			refresh_screen ();
			break;

		case SPEAK_SENTENCE :
			if ( screen_sentence.phonetic[0] != '\0' )
				speak ( screen_sentence.phonetic );
			else
				speak ( chosen_sentence->phonetic );
			break;

		case THAI_SENTENCE :
			show_sentence |= SHOW_THAI;
			strcpy ( screen_sentence.thai , chosen_sentence->thai );
			refresh_gadget ( TS_ENTRY );
			break;

		case PHONETIC_SENTENCE :
			show_sentence |= SHOW_PHONETIC;
			strcpy ( screen_sentence.phonetic , chosen_sentence->phonetic );
			refresh_gadget ( PS_ENTRY );
			break;

		case ENGLISH_SENTENCE :
			show_sentence |= SHOW_ENGLISH;
			strcpy ( screen_sentence.english , chosen_sentence->english );
			refresh_gadget ( ES_ENTRY );
			break;

		case RIGHT_SENTENCE :
			file_changed = TRUE;
			chosen_sentence->right++;
			break;

		case WRONG_SENTENCE :
			file_changed = TRUE;
			chosen_sentence->wrong++;
			break;

		case ADD_SENTENCE :
			if ( thai_search ( &sentence_head , screen_sentence.thai ,
				NEXT_ENTRY ) != NULL ) {

				strcpy ( body_buf , "Sentence exists - add anyway?" );
				if( !AutoRequest ( window , &body_text , &yes_text , &no_text ,
					(LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT ) )

					break;
			}

			file_changed = TRUE;
			if ( !add_phrase ( &sentence_head , &screen_sentence ) ) {
				strcpy ( body_buf , "Add failed (out of memory?)" );
				AutoRequest ( window , &body_text , (char*)NULL , &ok_text ,
					(LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT );
			}
			clr_sentence ();
			break;

		case DEL_SENTENCE :
			file_changed = TRUE;
			del_phrase ( &sentence_head , chosen_sentence );
			clr_sentence ();
			break;

		case REPLACE_SENTENCE :
			file_changed = TRUE;
			replace_phrase ( chosen_sentence , &screen_sentence );
			break;

		case CHOOSE_WORD :
			random_word ();
			scanning = -1;
			reset_next_prev ();
			screen_word.thai[0] = '\0';
			screen_word.phonetic[0] = '\0';
			screen_word.english[0] = '\0';
			show_word = 0;
			refresh_screen ();
			break;

		case SPEAK_WORD :
			if ( screen_word.phonetic[0] != '\0' )
				speak ( screen_word.phonetic );
			else
				speak ( chosen_word->phonetic );
			break;

		case THAI_WORD :
			show_word |= SHOW_THAI;
			strcpy ( screen_word.thai , chosen_word->thai );
			refresh_gadget ( TW_ENTRY );
			break;

		case PHONETIC_WORD :
			show_word |= SHOW_PHONETIC;
			strcpy ( screen_word.phonetic , chosen_word->phonetic );
			refresh_gadget ( PW_ENTRY );
			break;

		case ENGLISH_WORD :
			show_word |= SHOW_ENGLISH;
			strcpy ( screen_word.english , chosen_word->english );
			refresh_gadget ( EW_ENTRY );
			break;

		case RIGHT_WORD :
			file_changed = TRUE;
			chosen_word->right++;
			break;

		case WRONG_WORD :
			file_changed = TRUE;
			chosen_word->wrong++;
			break;

		case ADD_WORD :
			if ( thai_search ( &word_head , screen_word.thai ,
				NEXT_ENTRY ) != NULL ) {

				strcpy ( body_buf , "Word exists - add anyway?" );
				if( !AutoRequest ( window , &body_text , &yes_text , &no_text ,
					(LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT ) )

					break;
			}

			file_changed = TRUE;
			if ( !add_phrase ( &word_head , &screen_word ) ) {
				strcpy ( body_buf , "Add failed (out of memory?)" );
				AutoRequest ( window , &body_text , (char*)NULL , &ok_text ,
					(LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT );
			}
			clr_word ();
			break;

		case DEL_WORD :
			file_changed = TRUE;
			del_phrase ( &word_head , chosen_word );
			clr_word ();
			break;

		case REPLACE_WORD :
			file_changed = TRUE;
			replace_phrase ( chosen_word , &screen_word );
			break;

		case TS_SEARCH :
			strcpy ( scan.thai , screen_sentence.thai );
			phrase = thai_search ( &sentence_head , scan.thai , NEXT_ENTRY );
			if ( phrase != NULL ) {
				show_sentence = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
				scanning = TS_SEARCH;
				new_sentence ( phrase );
			}
			else
				not_found ();
			break;

		case PS_SEARCH :
			strcpy ( scan.phonetic , screen_sentence.phonetic );
			phrase = phonetic_search ( &sentence_head , scan.phonetic ,
				NEXT_ENTRY );
			if ( phrase != NULL ) {
				show_sentence = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
				scanning = PS_SEARCH;
				new_sentence ( phrase );
			}
			else
				not_found ();
			break;

		case ES_SEARCH :
			strcpy ( scan.english , screen_sentence.english );
			phrase = english_search ( &sentence_head , scan.english ,
				NEXT_ENTRY );
			if ( phrase != NULL ) {
				show_sentence = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
				scanning = ES_SEARCH;
				new_sentence ( phrase );
			}
			else
				not_found ();
			break;

		case TW_SEARCH :
			strcpy ( scan.thai , screen_word.thai );
			phrase = thai_search ( &word_head , scan.thai , NEXT_ENTRY );
			if ( phrase != NULL ) {
				show_word = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
				scanning = TW_SEARCH;
				new_word ( phrase );
			}
			else
				not_found ();
			break;

		case PW_SEARCH :
			strcpy ( scan.phonetic , screen_word.phonetic );
			phrase = phonetic_search ( &word_head , scan.phonetic ,
				NEXT_ENTRY );
			if ( phrase != NULL ) {
				show_word = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
				scanning = PW_SEARCH;
				new_word ( phrase );
			}
			else
				not_found ();
			break;

		case EW_SEARCH :
			strcpy ( scan.english , screen_word.english );
			phrase = english_search ( &word_head , scan.english , NEXT_ENTRY );
			if ( phrase != NULL ) {
				show_word = SHOW_THAI | SHOW_PHONETIC | SHOW_ENGLISH;
				scanning = EW_SEARCH;
				new_word ( phrase );
			}
			else
				not_found ();
			break;

		case NEXT_ENTRY :
			do_scan ( NEXT_ENTRY );
			break;

		case PREV_ENTRY :
			do_scan ( PREV_ENTRY );
			break;

		case TS_ENTRY :
			entry = TS_ENTRY;
			/* on_thai_keyboard (); */
			break;

		case PS_ENTRY :
			break;

		case ES_ENTRY :
			break;

		case TW_ENTRY :
			entry = TW_ENTRY;
			/* on_thai_keyboard (); */
			break;

		case PW_ENTRY :
			break;

		case EW_ENTRY :
			break;

		case TRANSLATE_SENTENCE :
			if ( screen_sentence.thai[0] != '\0' )
				sentence = screen_sentence.thai;
			else
				sentence = chosen_sentence->thai;
			error_at = split_sentence ( sentence );
			if ( error_at != NULL ) {
				/* error occured at 'error_at' */
				sprintf ( body_buf , "Failed at character %d" ,
					error_at - sentence + 1 );
				AutoRequest ( window , &body_text , (char*)NULL , &ok_text ,
					(LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT );

			}
			else {
				screen_sentence.phonetic[0] = '\0';
				for ( i = 0; i < num_split_words; i++ ) {
					strcat ( screen_sentence.phonetic , split_words[i]->phonetic );
				}
				cur_split_word = -1;
				scanning = CHOOSE_WORD;
				show_word = SHOW_THAI | SHOW_ENGLISH | SHOW_PHONETIC;
				refresh_gadget ( PS_ENTRY );
			}
			break;

		case TRANSLATE_WORD :
			if ( screen_word.thai[0] != '\0' )
				trans = translate ( screen_word.thai );
			else
				trans = translate ( chosen_word->thai );
			if ( trans != NULL )
				strcpy ( screen_word.phonetic , trans );
			refresh_gadget ( PW_ENTRY );
			break;

		default :
			break;
		}
	}
}


edit_thai ( str , field , thai_char )
char *str;
int field;
char thai_char;
{
	char *p;

	p = str;
	while ( *p != '\0' )
		p++;
	if ( thai_char == TC_BACK_SPACE ) {
		if ( p > str )
			*(p-1) = '\0';
	}
	else if ( thai_char == TC_CLEAR ) {
		*str = '\0';
	}
	else {
		*p++ = thai_char;
		*p = '\0';
	}
	redraw_thai ( field );
}


not_found ()
{
	strcpy ( body_buf , "Not Found" );
	AutoRequest ( window , &body_text , (char*)NULL , &ok_text ,
		(LONG)0 , (LONG)0 , (LONG)REQ_WIDTH , (LONG)REQ_HEIGHT );
}
