

#ifndef __ttageditform_h__
#include "TTagEditForm.h"
#endif

#ifndef __tags_h__
#include "tags.h"
#endif

#ifndef PandaWave_XDIALOGS
#include <XDialog.h>
#endif

#ifndef PandaWave_XMESSAGES
#include <XMessages.h>
#endif

#ifndef PandaWave_XWINDOW
#include <XWindow.h>
#endif

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

#ifndef __ttageditheader_h__
#include "TTagEditHeader.h"
#endif

#ifndef __revengeapp_h__
#include "revengeapp.h"
#endif

#ifndef PandaWave_TPROJECTFORM
#include "TTagFileForm.h"
#endif

#ifndef __tagpick_h__
#include "tagpick.h"
#endif

#ifndef PandaWave_XLIST
#include <XList.h>
#endif


#ifndef PandaWave_XBUTTONS
#include <XButtons.h>
#endif

#ifndef PandaWave_XTEXTCONTROLS
#include <XTextControls.h>
#endif

#ifndef __tbitmapview_h__
#include "TBitmapView.h"
#endif

#ifndef __formatters_h__
#include "formatters.h"
#endif


XGPtrSet<TTagEditForm> TTagEditForm::changeNotifyList;


TTagEditForm::TTagEditForm( tag_entry *e )
{
	set_entry_form( e, this );
	fEntry = e;
	changeNotifyList += this;
}


TTagEditForm::~TTagEditForm()
{
	changeNotifyList -= this;
	set_entry_form( fEntry, 0 );
}


void TTagEditForm::DoFormInitialize()
{
	XGWindow *w = GetWindow();
	if (!w) return;
	TTagEditHeader *head = dynamic_cast<TTagEditHeader *>(XGViewFactory::CreateFromResource( KViewTagEditHeader, w ));
	if (!head) return;
	fFileBtn = dynamic_cast<XGCommandButton *>(head->FindViewByID(KMenuFindTagFile));
	fReloadBtn = dynamic_cast<XGCommandButton *>(head->FindViewByID(KMenuReloadTag));
	fApplyBtn = dynamic_cast<XGCommandButton *>(head->FindViewByID(KMenuApplyTagChanges));
	fFilePath = dynamic_cast<XGStaticText *>(head->FindViewByID(9802));
	if (fApplyBtn) fApplyBtn->SetDefault( true );
	
	FileChanged();
	
	XRect hr;
	head->GetAutoLocate( &hr, 0, 0, 0, 0 );
	for (XGView *v = w->GetChild(); v; v = v->GetSibling()) {
		if (v == head) continue;
		XRect loc;
		bool l,t,r,b;
		v->GetAutoLocate( &loc, &l, &t, &r, &b );
		if (t) {
			loc.top += hr.bottom;
		}
		if (!b) {
			loc.bottom += hr.bottom;
		}
		if (t || !b) {
			v->SetAutoLocate( loc, l, t, r, b );
		}
	}
	
	XPoint wsize = w->GetWindowSize();
	XPoint minsize = w->MinWindowSize();
	XPoint maxsize = w->MaxWindowSize();
	XPoint zoomsize = w->ZoomWindowSize();
	
	if (minsize.h < 300) minsize.h = 300;
	minsize.v += hr.bottom;
	w->SetMinWindowSize(minsize);
	
	if (maxsize.h || maxsize.v) {
		if (maxsize.h < 300) maxsize.h = 300;
		maxsize.v += hr.bottom;
		w->SetMaxWindowSize(maxsize);
	}
	
	if (zoomsize.h || zoomsize.v) {
		if (zoomsize.h < 300) zoomsize.h = 300;
		zoomsize.v += hr.bottom;
		w->SetZoomWindowSize(zoomsize);
	}
	
	if (wsize.h < 300) wsize.h = 300;
	wsize.v += hr.bottom;
	w->SetWindowSize(wsize);
}


void TTagEditForm::HandleTagChanged( tag /* group */, tag /* id */ )
{
	// do nothing at all
}


void announce_tag_change( tag group, tag id )
{
	for (int i = 0; i < TTagEditForm::changeNotifyList.Size(); ++i)
	{
		TTagEditForm::changeNotifyList[i]->HandleTagChanged( group, id );
	}
	tag_entry *e = get_entry( group, id );
	if (e)
	{
		tag_file *f = get_entry_file( e );
		TTagFileForm *file_form = get_file_form( f );
		if (file_form)
		{
			file_form->ReloadTableContents();
			file_form->NoticeItem( e, false, true );
			file_form->DoDocumentRenamed();
		}
		TTagEditForm *entry_form = get_entry_form( e );
		if (entry_form)
		{
			entry_form->TagInfoChanged();
		}
	}
	
}

void announce_tag_change( tag_entry *e )
{
	announce_tag_change( get_group_type(get_entry_group(e)), get_entry_id(e) );
}


BEGINDISPATCH(TTagEditForm,XGForm)
	DISPATCHTABLE(KXGGetMenuStatus,TTagEditForm::DoGetMenuStatus)
	DISPATCHTABLE(KXGDoMenuCommand,TTagEditForm::DoMenuCommand)
	DISPATCHTABLE(KXGControlClick,TTagEditForm::DoControlClick)
ENDDISPATCH


long TTagEditForm::DoGetMenuStatus( long arg, void *ptr )
{
	XGSMenuStatus *e = (XGSMenuStatus *)ptr;
	
	switch (arg) {
		case 9803:	// reload
			e->enable = NeedCommit();
			fReloadBtn->SetEnabled( e->enable );
			break;
		case 9804:	// apply
			e->enable = NeedCommit();
			fApplyBtn->SetEnabled( e->enable );
			break;
		case KMenuResourceInfo:
			e->enable = true;
			break;
		default:
			break;
	}
	return -1;
}


long TTagEditForm::DoMenuCommand( long arg, void *ptr )
{
	switch (arg) {
		case KMenuFindTagFile:
		case KMenuReloadTag:
		case KMenuApplyTagChanges:
			return this->DoControlClick( arg, ptr );
		case KMenuResourceInfo:
			TTagFileForm *form = get_file_form( get_entry_file( fEntry ) );
			if(form)
				form->GetInfoItem( fEntry );
			return 0;
		default:
			break;
	}
	return -1;
}


long TTagEditForm::DoControlClick( long arg, void * )
{
	switch (arg) {
		case KMenuFindTagFile: {// file
			TRevengeApp::OpenTagFileWindow( get_entry_file( fEntry ) );
			TTagFileForm *form = get_file_form( get_entry_file( fEntry ) );
			if (form) {
				form->NoticeItem( fEntry, true, true );
			}
			return 0;
		}
		case KMenuReloadTag:	// reload
			this->ReloadTag();
			return 0;
		case KMenuApplyTagChanges:	// apply
			this->CommitToTag();
			return 0;
		default: {
				XGView *view = GetWindow()->FindViewByID( arg );
				XGCheckButton *check = dynamic_cast< XGCheckButton * >(view);
				if (check) {
					check->SetValue( !check->GetValue() );
					return 0;
				}
				XGRadioButton *radio = dynamic_cast< XGRadioButton * >(view);
				if (radio) {
					for (XGView *scan = radio->GetParent()->GetChild(); scan; scan = scan->GetSibling()) {
						XGRadioButton *sib = dynamic_cast< XGRadioButton * >(scan);
						if (sib) {
							sib->SetValue( sib == radio );
						}
					}
				}
			} break;
	}
	return -1;
}


void TTagEditForm::TagInfoChanged()
{
	if (!fEntry) return;
	
	XGWindow *w = GetWindow();
	if (w) {
		char title[256];
		sprintf( title, "%s (%s/%s)",
			get_entry_name( fEntry ), 
			tagstr(get_entry_id( fEntry )),
			tagstr(get_entry_type( fEntry )) );
		w->SetTitle( title );
	}
}


bool TTagEditForm::DoQueryDestroyWindow()
{
	if (!NeedCommit()) return true;
	
	char title[256];
	XGWindow *w = GetWindow();
	if (!w) return true;
	w->GetTitle( title );
	long val = XGDialog::ModalAlert( KWindowConfirmEditorClose, title, 0, 0, 0 );
	
	if (val == 103) {	// discard
		return true;
	} else if (val == 102) { // cancel
		return false;
	} else if (val == 101) {
		CommitToTag();
		return true;
	} else {
		return false; // error?
	}
}


void TTagEditForm::FileChanged()
{
	char path[256];
	TRevengeApp::GetTagFilePath( path, get_entry_file( fEntry ) );
	if (fFilePath) {
		fFilePath->SetTitle( path );
	}
}


void TTagEditForm::SetInt( int view_id, int value )
{
	::SetInt( GetWindow(), view_id, value );
}


void TTagEditForm::SetFlag( int view_id, bool value )
{
	::SetFlag( GetWindow(), view_id, value );
}


void TTagEditForm::SetFloat( int view_id, double value, int precision )
{
	::SetFloat( GetWindow(), view_id, value, precision );
}


void TTagEditForm::SetFixed( int view_id, fixed value )
{
	::SetFixed( GetWindow(), view_id, value );
}

void TTagEditForm::SetFixedFraction( int view_id, fixed_fraction value )
{
	::SetFixedFraction( GetWindow(), view_id, value );
}

void TTagEditForm::SetPercentage( int view_id, fixed_fraction value )
{
	::SetPercentage( GetWindow(), view_id, value );
}

void TTagEditForm::SetShortFixed( int view_id, short_fixed value )
{
	::SetShortFixed( GetWindow(), view_id, value );
}

void TTagEditForm::SetShortFixedFraction( int view_id, short_fixed_fraction value )
{
	::SetShortFixedFraction( GetWindow(), view_id, value );
}

void TTagEditForm::SetShortPercentage( int view_id, short_fixed_fraction value )
{
	::SetShortPercentage( GetWindow(), view_id, value );
}

void TTagEditForm::SetShortWorldDistance( int view_id, short_world_distance value )
{
	::SetShortWorldDistance( GetWindow(), view_id, value );
}

void TTagEditForm::SetWorldDistance( int view_id, world_distance value )
{
	::SetWorldDistance( GetWindow(), view_id, value );
}

void TTagEditForm::SetText( int view_id, const char *value )
{
	::SetText( GetWindow(), view_id, value );
}


void TTagEditForm::SetTag( int view_id, tag type, tag value )
{
	::SetTag( GetWindow(), view_id, type, value );
}


void TTagEditForm::SetAngle( int view_id, int value )
{
	::SetAngle( GetWindow(), view_id, value );
}


void TTagEditForm::SetAngleInSeconds( int view_id, int value )
{
	::SetAngleInSeconds( GetWindow(), view_id, value );
}


void TTagEditForm::SetColor( int view_id, const struct rgb_color &rgb )
{
	::SetColor( GetWindow(), view_id, rgb );
}


int TTagEditForm::GetInt( int view_id )
{
	return ::GetInt( GetWindow(), view_id );
}


bool TTagEditForm::GetFlag( int view_id )
{
	return ::GetFlag( GetWindow(), view_id );
}


double TTagEditForm::GetFloat( int view_id )
{
	return ::GetFloat( GetWindow(), view_id );
}


fixed TTagEditForm::GetFixed( int view_id )
{
	return ::GetFixed( GetWindow(), view_id );
}

fixed_fraction TTagEditForm::GetFixedFraction( int view_id )
{
	return ::GetFixedFraction( GetWindow(), view_id );
}

fixed_fraction TTagEditForm::GetPercentage( int view_id )
{
	return ::GetPercentage( GetWindow(), view_id );
}

short_fixed TTagEditForm::GetShortFixed( int view_id )
{
	return ::GetShortFixed( GetWindow(), view_id );
}

short_fixed_fraction TTagEditForm::GetShortFixedFraction( int view_id )
{
	return ::GetShortFixedFraction( GetWindow(), view_id );
}

short_fixed_fraction TTagEditForm::GetShortPercentage( int view_id )
{
	return ::GetShortPercentage( GetWindow(), view_id );
}

short_world_distance TTagEditForm::GetShortWorldDistance( int view_id )
{
	return ::GetShortWorldDistance( GetWindow(), view_id );
}

world_distance  TTagEditForm::GetWorldDistance( int view_id )
{
	return ::GetWorldDistance( GetWindow(), view_id );
}

void TTagEditForm::GetText( int view_id, char *value, int maxlen )
{
	::GetText( GetWindow(), view_id, value, maxlen );
}


tag TTagEditForm::GetTag( int view_id )
{
	return ::GetTag( GetWindow(), view_id );
}


int TTagEditForm::GetAngle( int view_id )
{
	return ::GetAngle( GetWindow(), view_id );
}


int TTagEditForm::GetAngleInSeconds( int view_id )
{
	return ::GetAngleInSeconds( GetWindow(), view_id );
}

void TTagEditForm::SetEnabled( int view_id, bool enabled )
{
	XGWindow *window = GetWindow();
	if (!window) return;
	XGView *view = window->FindViewByID( view_id );
	if (!view) return;
	
	view->SetEnabled( enabled );
}


bool TTagEditForm::IsEnabled( int view_id )
{
	XGWindow *window = GetWindow();
	if (!window) return false;
	XGView *view = window->FindViewByID( view_id );
	if (!view) return false;
	
	return view->IsEnabled();
}


XGView *TTagEditForm::GetView( int view_id )
{
	XGWindow *window = GetWindow();
	if (!window) return false;
	return window->FindViewByID( view_id );
}


char TTagEditForm::get_delta( char lower, char upper )
{
	return ::get_delta( lower, upper );
}

char TTagEditForm::get_upper_bound( char lower, char delta )
{
	return ::get_upper_bound( lower, delta );
}

unsigned char TTagEditForm::get_delta( unsigned char lower, unsigned char upper )
{
	return ::get_delta( lower, upper );
}

unsigned char TTagEditForm::get_upper_bound( unsigned char lower, unsigned char delta )
{
	return ::get_upper_bound( lower, delta );
}

short TTagEditForm::get_delta( short lower, short upper )
{
	return ::get_delta( lower, upper );
}

short TTagEditForm::get_upper_bound( short lower, short delta )
{
	return ::get_upper_bound( lower, delta );
}

unsigned short TTagEditForm::get_delta( unsigned short lower, unsigned short upper )
{
	return ::get_delta( lower, upper );
}

unsigned short TTagEditForm::get_upper_bound( unsigned short lower, unsigned short delta )
{
	return ::get_upper_bound( lower, delta );
}

long TTagEditForm::get_delta( long lower, long upper )
{
	return ::get_delta( lower, upper );
}

long TTagEditForm::get_upper_bound( long lower, long delta )
{
	return ::get_upper_bound( lower, delta );
}

unsigned long TTagEditForm::get_delta( unsigned long lower, unsigned long upper )
{
	return ::get_delta( lower, upper );
}

unsigned long TTagEditForm::get_upper_bound( unsigned long lower, unsigned long delta )
{
	return ::get_upper_bound( lower, delta );
}

float TTagEditForm::get_delta( float lower, float upper )
{
	return ::get_delta( lower, upper );
}

float TTagEditForm::get_upper_bound( float lower, float delta )
{
	return ::get_upper_bound( lower, delta );
}

void TTagEditForm::SetVisible( int view_id, bool visible )
{
	XGView *v = GetView( view_id );
	if (v) {
		if (visible) {
			v->ShowView();
		} else {
			v->HideView();
		}
	}
}


bool TTagEditForm::IsVisible( int view_id )
{
	XGView *v = GetView( view_id );
	if (!v) return false;
	return v->IsVisible();
}

