/***********************************************************************
!!!!!! DO NOT MODIFY !!!!!!

GacGen.exe Resource.xml

This file is generated by Workflow compiler
https://github.com/vczh-libraries
***********************************************************************/

#include "Demo.h"
/* CodePack:BeginIgnore() */
#ifndef VCZH_DEBUG_NO_REFLECTION
/* CodePack:ConditionOff(VCZH_DEBUG_NO_REFLECTION, DemoReflection.h) */
#include "DemoReflection.h"
#endif
/* CodePack:EndIgnore() */

#if defined( _MSC_VER)
#pragma warning(push)
#pragma warning(disable:4250)
#elif defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wparentheses-equality"
#elif defined(__GNUC__)
#pragma GCC diagnostic push
#endif

#define GLOBAL_SYMBOL ::vl_workflow_global::Demo::
#define GLOBAL_NAME ::vl_workflow_global::Demo::Instance().
#define GLOBAL_OBJ &::vl_workflow_global::Demo::Instance()

/* USER_CONTENT_BEGIN(custom global declarations) */
/* USER_CONTENT_END() */

/***********************************************************************
Class (::demo::FindWindow)
***********************************************************************/

namespace demo
{
	bool FindWindow::FindNext(const ::vl::WString& toFind, bool caseSensitive, bool down)
	{/* USER_CONTENT_BEGIN(::demo::FindWindow) */
		auto position = textBox->GetCaretEnd();
		auto rowCount = textBox->GetRowCount();
		auto normalization = caseSensitive ? Locale::None : Locale::IgnoreCase;

		for (vint i = position.row; 0 <= i&&i < rowCount; (down ? i++ : i--))
		{
			auto rowText = textBox->GetRowText(i);
			auto fragment = rowText;
			if (i == position.row)
			{
				if (down)
				{
					fragment = rowText.Right(rowText.Length() - position.column);
				}
				else
				{
					fragment = rowText.Left(position.column);
				}
			}

			auto pair = down
				? INVLOC.FindFirst(fragment, toFind, normalization)
				: INVLOC.FindLast(fragment, toFind, normalization)
				;

			if (pair.key != -1)
			{
				TextPos begin, end;
				if (i == position.row && down)
				{
					begin = TextPos(i, pair.key + position.column);
				}
				else
				{
					begin = TextPos(i, pair.key);
				}

				if (down)
				{
					end = TextPos(begin.row, begin.column + pair.value);
				}
				else
				{
					end = TextPos(begin.row, begin.column);
					begin.column += pair.value;
				}

				textBox->Select(begin, end);
				textBox->SetFocused();
				return true;
			}
		}

		return false;
	}/* USER_CONTENT_END() */

	FindWindow::FindWindow()
		: ::vl::presentation::controls::GuiWindow(::vl::presentation::theme::ThemeName::Window)
		, textBox(static_cast<::vl::presentation::controls::GuiMultilineTextBox*>(nullptr))
	{
		auto __vwsn_resource_ = ::vl::__vwsn::This(::vl::presentation::GetResourceManager())->GetResourceFromClassName(::vl::WString::Unmanaged(L"demo::FindWindow"));
		auto __vwsn_resolver_ = ::vl::Ptr<::vl::presentation::GuiResourcePathResolver>(new ::vl::presentation::GuiResourcePathResolver(__vwsn_resource_, ::vl::__vwsn::This(__vwsn_resource_.Obj())->GetWorkingDirectory()));
		::vl::__vwsn::This(this)->SetResourceResolver(__vwsn_resolver_);
		::vl::__vwsn::This(this)->__vwsn_demo_FindWindow_Initialize(this);
	}

	FindWindow::~FindWindow()
	{
		this->FinalizeInstanceRecursively(static_cast<::vl::presentation::controls::GuiControlHost*>(this));
	}

}
#undef GLOBAL_SYMBOL
#undef GLOBAL_NAME
#undef GLOBAL_OBJ

#if defined( _MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
