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

#include "StdHeaders.h"

#include "EditTextCtrl.h"

#include "Dialog.h"

using namespace VectorWorks::VWUI;

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

VWEditTextCtrl::~VWEditTextCtrl()
{
}

bool VWEditTextCtrl::CreateControl(VWDialog* pDlg, const TXString& text, short widthInCharacters, short heightInLines/*=1*/)
{
	::GS_CreateEditText( gCBP, pDlg->GetControlID(), fControlID, text, widthInCharacters, heightInLines );
	return VWControl::CreateControl( pDlg );
}

void VWEditTextCtrl::GetText255(TXString& string)
{
	GSStr255	strArr;
	::GS_GetItemText( gCBP, fpParentDlg->GetControlID(), fControlID, strArr );
	string		= strArr;
}

void VWEditTextCtrl::GetMultilineText(TXString& string)
{
	GSHandle	hText	= ::GS_GetMultilineText( gCBP, fpParentDlg->GetControlID(), fControlID );
	char*		szText	= *hText;
	size_t		size	= 0;
	::GS_GetHandleSize( gCBP, hText, size );
	szText[size]		= 0x00;

	string				= szText;

	// dont dispose zero size handle -- it crashes
	if ( size > 0 ) {
		::GS_DisposeHandle( gCBP, hText );
	}
}

void VWEditTextCtrl::SetText255(const TXString& string)
{
	GSStr255	strArr;
	string.CopyInto( strArr );
	::GS_SetItemText( gCBP, fpParentDlg->GetControlID(), fControlID, strArr );
}

void VWEditTextCtrl::SetMultilineText(const TXString& string)
{
	size_t		len		= string.GetLength();
	GSHandle	hText	= ::GS_NewHandle( gCBP, len+1 );
	::GS_HLock( gCBP, hText );
	char*		szText	= *hText;
	string.CopyInto( szText/*, len*/ );
	::GS_HUnlock( gCBP, hText );

	::GS_SetMultilineText( gCBP, fpParentDlg->GetControlID(), fControlID, hText );

	::GS_DisposeHandle( gCBP, hText );
}

void VWEditTextCtrl::SelectContent()
{
	::GS_SelectEditText( gCBP, fpParentDlg->GetControlID(), fControlID );
}

void VWEditTextCtrl::DeselectContent()
{
	gSDK->DeselectEditText( fpParentDlg->GetControlID(), fControlID );
}
