#include "tags.h"
#include "tagprivate.h"

#include "text.h"

#ifndef PandaWave_RESOURCES
#include "RevengeResources.h"
#endif

#include "RevengeApp.h"
#include <XMessages.h>
#include "tagpick.h"
#include <assert.h>
#include <XPopup.h>
#include <XWindow.h>
#include <XTextControls.h>


// ==============================  BYTE SWAPPING DATA  ========================================


// ==============================  EDIT WINDOW  ========================================
void edit_text( tag_entry* e )
{
	TtextEditForm* form = new TtextEditForm( e );
	form->AttachWindow( 1026 );
}

void defaultnew_text( tag_entry *e )
{
	// nop
}


// ==============================  READ TAG DATA  ========================================
bool read_string_definition( string_definition* def, tag_entry* e )
{
	// text data is saved in Mac format (\r only), yaaf likes \n for it's text box display
	const void *data;
	int ii;
	
	if( e->length )
	{
		if( get_entry_data( e, &data, (unsigned *)&def->string_length ) )
		{
			memcpy( def->string_buffer, data, def->string_length );
			def->string_buffer[def->string_length] = '\0'; 
		
			// happy fun text formatting
			for( ii = 0; ii < def->string_length; ii++ )
			{
				if( def->string_buffer[ii] == '\r' )
					def->string_buffer[ii] = '\n';
			}
			
			return TRUE;
		}
	}
	else
	{
		def->string_length = 0;
		def->string_buffer[0] = '\0';
		return TRUE;
	}
	
	return FALSE;
}


// ==============================  SAVE TAG DATA  ========================================
bool save_string_definition( string_definition* def, tag_entry* e )
{
	// text data is saved in Mac format (\r only), yaaf likes \n for it's text box display
	int ii;
	
	// happy fun text formatting
	for( ii = 0; ii < def->string_length; ii++ )
	{
		if( def->string_buffer[ii] == '\n' )
			def->string_buffer[ii] = '\r';
	}
	
	set_entry_data( e, def->string_buffer, def->string_length, FALSE );

	// I have to assume that set_entry_data worked	
	return TRUE;
}


// ==============================  READ TAG DATA (WITH ID) ========================================
bool read_string_definition( string_definition* def, tag id )
{
	tag_entry* e;
	
	assert( def );
	
	e = get_entry( 'text', id );
	if( 0 == e ) {
		return FALSE;
	}
	
	return read_string_definition( def, e );	
}


// ==============================  SAVE TAG DATA (WITH ID) ========================================
bool save_string_definition( string_definition* def, tag id )
{
	tag_entry* e;
	
	assert( def );
	
	e = get_entry( 'text', id );
	if( 0 == e ) {
		return FALSE;
	}

	return save_string_definition( def, e );
}


// ==============================  DISPATCH  ========================================
BEGINDISPATCH(TtextEditForm,TTagEditForm)
	DISPATCHTABLE(KXGControlClick,TtextEditForm::DoControlClick)
ENDDISPATCH


TtextEditForm::TtextEditForm( struct tag_entry *e ) : TTagEditForm( e )
{
}

// ==============================  INITIALIZE FORM =======================================
void TtextEditForm::DoFormInitialize()
{
	TTagEditForm::DoFormInitialize();

}


// ==============================  INPUT HANDLING  ========================================
long TtextEditForm::DoControlClick( long arg, void *ptr )
{
#ifdef __MWERKS__
	#pragma unused(ptr)
#endif

	return -1;
}

		
// ========================================  SAVE TO TAG  ==================================================
void TtextEditForm::CommitToTag()
{
	GetValues();
	save_string_definition( &def, fEntry );
}


// ========================================  RELOAD TAG  ==================================================
void TtextEditForm::ReloadTag()
{
	read_string_definition( &def, fEntry );
	SetValues();
}


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TtextEditForm::NeedCommit()
{
	string_definition original;
	int ii;
	
	GetValues();

	// get the definition from the tag
	if( !read_string_definition( &original, fEntry ) )
		return TRUE;

	// happy fun text formatting
	for( ii = 0; ii < original.string_length; ii++ )
	{
		if( original.string_buffer[ii] == '\r' )
			original.string_buffer[ii] = '\n';
	}
	
	if( original.string_length == def.string_length )
		return memcmp( original.string_buffer, def.string_buffer, original.string_length );
	
	return TRUE;
}

		
// ========================================  SET INTERFACE VALUES  ==================================================
void TtextEditForm::SetValues()
{
	XGEditText *pEdit = dynamic_cast<XGEditText *>(GetWindow()->FindViewByID( 12601 ));
	
	pEdit->SetTitle( def.string_buffer );
}


// ========================================  GET INTERFACE VALUES  ==================================================
void TtextEditForm::GetValues()
{
	XGEditText *pEdit = dynamic_cast<XGEditText *>(GetWindow()->FindViewByID( 12601 ));

	def.string_length = pEdit->GetTitleEx( def.string_buffer, MAX_STRING_SIZE );
	
	// special case thing for 0 length strings (save a NUL to the tag)
	if( def.string_length == 0 )
	{
		def.string_length = 1;
		def.string_buffer[0] = '\0';
	}
}

