#include "StdAfx.h"

#include "SampleDlg4.h"
#include "PluginResource.h"

// dialog control IDs
#define		kDialogID             12003
#define		kOK                   1
#define		kCancel               2
#define		kTabControl           4
#define		kButtonsTab           5
#define		kButtonsGrp           6
#define		kButtonText           7
#define		kButtonCtrl           8
#define		kClrButtonText        9
#define		kClrButtonCtrl        10
#define		kColorPopup           11
#define		kUnnamed4             12
#define		kAnotherTab           13
#define		kUnnamed8             14
#define		kSampleText           15
#define		kCustomCtrlTab        16
#define		kUnnamed              17
#define		kUnnamed1             18
#define		kUnnamed2             19
#define		kUnnamed3             20
#define		kTreeCtrlTab          21
#define		kTreeCtrl             22
#define		kLeftButton           2605
#define		kThumbNailTab         24
#define		kThumbCtrlSelGrp      25
#define		kThumbNailCtrl        26
#define		kHatchesRadio         27
#define		kWallHatchesRadio     28
#define		kGradientsRadio       29
#define		kImagesRadio          30
#define		kGetButton            31
#define		kSettButton           32



using namespace UI;

EVENT_DISPATCH_MAP_BEGIN( CSampleDlg4 );
ADD_DISPATCH_EVENT( kButtonCtrl, OnButtonCtrl );
ADD_DISPATCH_EVENT( kClrButtonCtrl, OnColorButton );
ADD_DISPATCH_EVENT( kUnnamed4, OnColorPopup );
ADD_DISPATCH_EVENT( kLeftButton, OnLefButton );

ADD_DISPATCH_EVENT( kHatchesRadio, OnHatchesRadio );
ADD_DISPATCH_EVENT( kWallHatchesRadio, OnWallHatchesRadio );
ADD_DISPATCH_EVENT( kGradientsRadio, OnGradientsRadio );
ADD_DISPATCH_EVENT( kImagesRadio, OnImagesRadio );
ADD_DISPATCH_EVENT( kGetButton, OnGetThumbnailButton );
ADD_DISPATCH_EVENT( kSettButton, OnSetThumbnailButton );
EVENT_DISPATCH_MAP_END;

CSampleDlg4::CSampleDlg4() :
	fMyCustomCtrl1( kUnnamed ),
	fMyCustomCtrl2( kUnnamed3 )
{
	fActiveType					= kThumbnailType_Hatches;
	fThumbPatternIndex			= 0;

	this->BuildResourceImages( false );
}

CSampleDlg4::~CSampleDlg4()
{
	this->BuildResourceImages( true );
}

bool CSampleDlg4::CreateDialogLayout()
{
	return this->CreateDialogLayoutFromRsrc( kDialogID );
}

void CSampleDlg4::OnInizializeContent()
{
	VWDialog::OnInizializeContent();

/*	// TODO: Fill up your dialog controls here
	VWTreeCtrl*		pTreeCtrl	= this->GetTreeCtrlByID( kTreeCtrl );

	pTreeCtrl->AddChildItem( "hello" );
	pTreeCtrl->AddChildItem( "bye" );
	VWTreeCtrlItem item = pTreeCtrl->AddChildItem( "a" );
	item.AddChildItem( "c" );
	item.AddChildItem( "b" );*/
	
	this->FillThumbnail( kThumbnailType_Hatches );
}

void CSampleDlg4::OnDDXInitialize()
{
	this->AddDDX_Control( & fMyCustomCtrl1 );
	this->AddDDX_Control( & fMyCustomCtrl2 );
	this->AddDDX_CheckGroupBox( kUnnamed8, & fUnnamed8 );

	this->AddDDX_ColorButton( kClrButtonCtrl, & fColorButton );
	this->AddDDX_ColorPopup( kUnnamed4, & fColorPopup );
}

void CSampleDlg4::OnUpdateUI()
{
	VWDialog::OnUpdateUI();
}

void CSampleDlg4::OnButtonCtrl(long controlID, VWDialogEventArgs& eventArgs)
{
}

void CSampleDlg4::OnColorButton(long controlID, VWDialogEventArgs& eventArgs)
{
	fColorPopup	= fColorButton;

	this->UpdateData( true );
}

void CSampleDlg4::OnColorPopup(long controlID, VWDialogEventArgs& eventArgs)
{
	fColorButton	= fColorPopup;

	this->UpdateData( true );
}

void CSampleDlg4::OnLefButton(long controlID, VWDialogEventArgs& eventArgs)
{
	VWDialog::AlertInform( "Left button clicked" );
}

void CSampleDlg4::BuildResourceImages(bool bFree)
{
	if ( bFree ) {
		// delete default resource lists
		::GS_DisposeResourceList( gCBP, fHatchResListID );
		fHatchResListItemsCnt				= 0;
		::GS_DisposeResourceList( gCBP, fWallHatchResListID );
		fWallHatchResListItemsCnt				= 0;
		::GS_DisposeResourceList( gCBP, fGradientResListID );
		fGradientResListItemsCnt			= 0;
		::GS_DisposeResourceList( gCBP, fImageResListID );
		fImageResListItemsCnt				= 0;
	}
	else {
		// build default resource lists
		TXString	folderName;
		fHatchResListItemsCnt		= 0;
		fHatchResListID				= ::GS_BuildResourceList( gCBP, kHatchDefNode, kDefaultHatchesFolder, folderName, fHatchResListItemsCnt );
		
		fWallHatchResListItemsCnt	= 0;
		fWallHatchResListID			= ::GS_BuildResourceList( gCBP, kHatchDefNode, kDefaultWallsHatchesFolder, folderName, fWallHatchResListItemsCnt );

		fGradientResListItemsCnt	= 0;
		fGradientResListID			= ::GS_BuildResourceList( gCBP, kGradientDefNode, kDefaultGradientsFolder, folderName, fGradientResListItemsCnt );

		fImageResListItemsCnt		= 0;
		fImageResListID				= ::GS_BuildResourceList( gCBP, kImageDefNode, kDefaultImageFillsFolder, folderName, fImageResListItemsCnt );
	}
}

void CSampleDlg4::FillThumbnail(EThumbnailType type)
{
	VWThumbnailPopupCtrl*	pThumbnail		= this->GetThumbnailPopupCtrlByID( kThumbNailCtrl );

	fActiveType		= type;
	pThumbnail->RemoveAllImages();

	switch( type ) {
		case kThumbnailType_Hatches:
			for(long i=0; i<fHatchResListItemsCnt; i++) {
				pThumbnail->AddImageFromResource( fHatchResListID, i );
			}
			break;
		case kThumbnailType_WallHatches:
			for(long i=0; i<fWallHatchResListItemsCnt; i++) {
				pThumbnail->AddImageFromResource( fWallHatchResListID, i );
			}
			break;
		case kThumbnailType_Gradients:
			for(long i=0; i<fGradientResListItemsCnt; i++) {
				pThumbnail->AddImageFromResource( fGradientResListID, i );
			}
			break;
		case kThumbnailType_Images:
			for(long i=0; i<fImageResListItemsCnt; i++) {
				pThumbnail->AddImageFromResource( fImageResListID, i );
			}
			break;
	}

	// ensure selection
	if ( ::GS_InternalIndexToHandle( gCBP, fThumbPatternIndex ) ) {
		pThumbnail->SelectItem( fThumbPatternIndex );
		size_t		selIndex	= pThumbnail->GetObjectItemIndex( fThumbPatternIndex );
		if ( selIndex == size_t(-1) && pThumbnail->GetItemsCount() > 0 ) {
			pThumbnail->SelectItem( size_t(0) );
		}
	}
}

void CSampleDlg4::OnHatchesRadio(long controlID, VWDialogEventArgs& eventArgs)
{
	this->FillThumbnail( kThumbnailType_Hatches );
}

void CSampleDlg4::OnWallHatchesRadio(long controlID, VWDialogEventArgs& eventArgs)
{
	this->FillThumbnail( kThumbnailType_WallHatches );
}

void CSampleDlg4::OnGradientsRadio(long controlID, VWDialogEventArgs& eventArgs)
{
	this->FillThumbnail( kThumbnailType_Gradients );
}

void CSampleDlg4::OnImagesRadio(long controlID, VWDialogEventArgs& eventArgs)
{
	this->FillThumbnail( kThumbnailType_Images );
}

void CSampleDlg4::OnGetThumbnailButton (long controlID, VWDialogEventArgs& eventArgs)
{
	VWThumbnailPopupCtrl*	pThumbnail		= this->GetThumbnailPopupCtrlByID( kThumbNailCtrl );

	fThumbPatternIndex				= 0;

	InternalIndex	popupIntIndex	= pThumbnail->GetSelectedItem();;
	size_t			popupIndex		= pThumbnail->GetSelectedItemIndex();

	if ( fActiveType == kThumbnailType_Hatches ) {
		InternalName	name;
		::GS_InternalIndexToNameN( gCBP, popupIntIndex, name );

		if ( popupIndex != size_t(-1) && name[0] == 0 ) {
			MCObjectHandle	hResource	= ::GS_ImportResourceToCurrentFile( gCBP, fHatchResListID, popupIndex + 1 );

			::GS_GetObjectName( gCBP, hResource, name );
			if ( ! ::GS_NameToInternalIndexN( gCBP, name, popupIntIndex ) ) {
				popupIntIndex	= 0;
			}
		}

		fThumbPatternIndex	= popupIntIndex;
	}
	else if ( fActiveType == kThumbnailType_WallHatches ) {
		InternalName	name;
		::GS_InternalIndexToNameN( gCBP, popupIntIndex, name );

		if ( popupIndex != size_t(-1) && name[0] == 0 ) {
			MCObjectHandle	hResource	= ::GS_ImportResourceToCurrentFile( gCBP, fWallHatchResListID, popupIndex + 1 );

			::GS_GetObjectName( gCBP, hResource, name );
			if ( ! ::GS_NameToInternalIndexN( gCBP, name, popupIntIndex ) ) {
				popupIntIndex	= 0;
			}
		}

		fThumbPatternIndex	= popupIntIndex;
	}
	else if ( fActiveType == kThumbnailType_Gradients ) {
		InternalName	name;
		::GS_InternalIndexToNameN( gCBP, popupIntIndex, name );

		if ( popupIndex != size_t(-1) && name[0] == 0 ) {
			MCObjectHandle	hResource	= ::GS_ImportResourceToCurrentFile( gCBP, fGradientResListID, popupIndex + 1 );

			InternalName	name;
			::GS_GetObjectName( gCBP, hResource, name );
			if ( ! ::GS_NameToInternalIndexN( gCBP, name, popupIntIndex ) ) {
				popupIntIndex	= 0;
			}
		}

		fThumbPatternIndex	= popupIntIndex;
	}
	else if ( fActiveType == kThumbnailType_Images ) {
		InternalName	name;
		::GS_InternalIndexToNameN( gCBP, popupIntIndex, name );

		if ( popupIndex != size_t(-1) && name[0] == 0 ) {
			MCObjectHandle	hResource	= ::GS_ImportResourceToCurrentFile( gCBP, fImageResListID, popupIndex + 1 );

			InternalName	name;
			::GS_GetObjectName( gCBP, hResource, name );
			if ( ! ::GS_NameToInternalIndexN( gCBP, name, popupIntIndex ) ) {
				popupIntIndex	= 0;
			}
		}

		fThumbPatternIndex	= popupIntIndex;
	}

	TXString text;
	text.Format( "Selected pattern index=0x%X\nPress 'Set' to select this one in the thumbnail", fThumbPatternIndex );
	VWDialog::AlertInform( text );

	// reload the thumbnail so it shows correct values after we have imported
	// this is needed because we need to have the thumbnail update
	this->BuildResourceImages( true );
	this->BuildResourceImages( false );
	this->FillThumbnail( fActiveType );
}

void CSampleDlg4::OnSetThumbnailButton(long controlID, VWDialogEventArgs& eventArgs)
{
	// recongnize the pattern index
	MCObjectHandle		hPatObject		= ::GS_InternalIndexToHandle( gCBP, fThumbPatternIndex );
	short				patObjType		= ::GS_GetObjectType( gCBP, hPatObject );

	this->GetRadioButtonCtrlByID( kHatchesRadio )->SetState( false );
	this->GetRadioButtonCtrlByID( kWallHatchesRadio )->SetState( false );
	this->GetRadioButtonCtrlByID( kGradientsRadio )->SetState( false );
	this->GetRadioButtonCtrlByID( kImagesRadio )->SetState( false );

	switch( patObjType ) {
		case kGradientDefNode:
			this->FillThumbnail( kThumbnailType_Gradients );
			this->GetRadioButtonCtrlByID( kGradientsRadio )->SetState( true );
			break;
		case kImageDefNode:
			this->FillThumbnail( kThumbnailType_Images );
			this->GetRadioButtonCtrlByID( kImagesRadio )->SetState( true );
			break;
		case kHatchDefNode:
		default:
			this->FillThumbnail( kThumbnailType_Hatches );
			this->GetRadioButtonCtrlByID( kHatchesRadio )->SetState( true );
			break;
	}
}



