//
//	Copyright © 2006 Nemetschek North America 
//	All Rights Reserved.
//
// Created by Vladislav Stanev

#include "StdHeaders.h"

#include "PullDownMenuCtrl.h"

#include "Dialog.h"

#include "VectorWorks/ISDK.h"

using namespace VectorWorks::VWUI;

VWPullDownMenuCtrl::VWPullDownMenuCtrl(TControlID id)
: VWControl( id )
{
}

VWPullDownMenuCtrl::~VWPullDownMenuCtrl()
{
}

bool VWPullDownMenuCtrl::CreateControl(VWDialog* pDlg, short widthInCharacters)
{
	farrIndexToMarker.clear();
	::GS_CreatePullDownMenu( gCBP, pDlg->GetControlID(), fControlID, widthInCharacters );
	return VWControl::CreateControl( pDlg );
}

void VWPullDownMenuCtrl::DeleteAllItems()
{
	::GS_DeleteAllItems( gCBP, fpParentDlg->GetControlID(), fControlID );
	farrIndexToMarker.clear();
}

void VWPullDownMenuCtrl::AddItem(const TXString& itemText, size_t insertAt /* = (size_t)-1*/, long marker /*= 0*/ )
{
	if ( insertAt == (size_t) -1 )
		insertAt		= this->GetItemsCount();

	Str255		szText;
	itemText.CopyInto( szText );

	::GS_AddChoice( gCBP, fpParentDlg->GetControlID(), fControlID, szText, DemoteTo<short>( kVStanev, insertAt ) );

	// set marker for this item
	if ( insertAt >= farrIndexToMarker.size() ) {
		// make sure we have enough space for the marker of this element
		farrIndexToMarker.resize( insertAt + 1 );
	}

	farrIndexToMarker[ insertAt ]	= marker;
}

void VWPullDownMenuCtrl::RemoveItem(size_t itemIndex)
{
	DSTOPIF( itemIndex >= this->GetItemsCount(), (kVStanev, "Deleting item = %d that doesn't exist in VWPullDownMenuCtrl", itemIndex) );

	ASSERTN( kVStanev, itemIndex < farrIndexToMarker.size() );
	if ( itemIndex < farrIndexToMarker.size() ) {
		farrIndexToMarker.erase( farrIndexToMarker.begin() + itemIndex );
	}
	::GS_RemoveChoice( gCBP, fpParentDlg->GetControlID(), fControlID, DemoteTo<short>( kVStanev, itemIndex ) );
}

size_t VWPullDownMenuCtrl::GetItemsCount() const
{
	short numItems	= -1;
	::GS_GetChoiceCount( gCBP, fpParentDlg->GetControlID(), fControlID, numItems );
	return (size_t) numItems;
}

size_t VWPullDownMenuCtrl::GetItemIndex(const TXString& itemText) const
{
	GSStr255		szText;
	itemText.CopyInto( szText );

	short nItemIndex	= -1;
	::GS_GetChoiceIndex( gCBP, fpParentDlg->GetControlID(), fControlID, szText, nItemIndex );
	return (size_t) nItemIndex;
}

void VWPullDownMenuCtrl::GetItemText(size_t itemIndex, TXString& outText) const
{
	GSStr255		str255;
	::GS_GetChoiceText( gCBP, fpParentDlg->GetControlID(), fControlID, DemoteTo<short>( kVStanev, itemIndex ), str255 );
	outText	= str255;
}

long VWPullDownMenuCtrl::GetItemMarker(size_t itemIndex) const
{
	long	marker	= 0;
	if ( itemIndex < farrIndexToMarker.size() ) {
		marker	= farrIndexToMarker[ itemIndex ];
	}

	return marker;
}

void VWPullDownMenuCtrl::SetItemMarker(size_t itemIndex, long marker)
{
	if ( itemIndex < farrIndexToMarker.size() ) {
		farrIndexToMarker[ itemIndex ]	= marker;
	}
}

size_t VWPullDownMenuCtrl::GetSelectedIndex() const
{
	short	nItemIndex	= -1;
	::GS_GetSelectedChoiceIndex( gCBP, fpParentDlg->GetControlID(), fControlID, nItemIndex );
	return (size_t) nItemIndex;
}

void VWPullDownMenuCtrl::SelectIndex(size_t itemIndex)
{
	::GS_SelectChoice( gCBP, fpParentDlg->GetControlID(), fControlID, DemoteTo<short>( kVStanev, itemIndex ) );
}

long VWPullDownMenuCtrl::GetSelectedItemMarker() const
{
	size_t	selIndex	= this->GetSelectedIndex();
	return this->GetItemMarker( selIndex );
}

bool VWPullDownMenuCtrl::SelectItemWithMarker(long marker)
{
	bool		bOk			= false;
	size_t		markersCnt	= farrIndexToMarker.size();
	for(size_t i=0; i<markersCnt; i++) {
		const long&		thisMarker	= farrIndexToMarker[ i ];
		if ( thisMarker == marker ) {
			this->SelectIndex( i );
			bOk			= true;
			// STOP! pull downs can only have one selectoin
			break;
		}
	}

	return bOk;
}

void VWPullDownMenuCtrl::EnableNotifyPullDown()
{
	::GS_NotifyPullDownClicked( gCBP, fpParentDlg->GetControlID(), fControlID );
}

// Set the indicated item as enabled or disabled, which should cause it to be greyed and not selectable.
void VWPullDownMenuCtrl::SetItemEnabled(size_t itemIndex, bool enabled)
{
	gSDK->SetListItemEnabled(fpParentDlg->GetControlID(), fControlID, DemoteTo<short>( kTArdoin, itemIndex ), enabled);
}

