//---------------------------------------------------------------------------
extern "C" int _export PASCAL UIShowFolderSelectorForm(void *reserved, char *buf)
{
	TFolderSelectorForm *form = new TFolderSelectorForm(Application);
	form->ShellTreeView->Path = buf; // initial directory
	if(form->ShellTreeView->Selected)
	{
		form->ShellTreeView->Selected->Expanded = true;
		TTreeNode * topnode = form->ShellTreeView->Selected;
		if(topnode->Count >= 1)
		{
			TTreeNode * longestitem = NULL;
			int longesttextsize = -1;
			for(int i = 0; i < topnode->Count; i++)
			{
				TTreeNode *subnode = topnode->Item[i];
				TRect rect = subnode->DisplayRect(true);
				if(rect.right > longesttextsize)
				{
					longesttextsize = rect.right;
					longestitem = topnode->Item[i];
				}
			}
			if(longestitem)
				longestitem->MakeVisible();

//		form->ShellTreeView->Selected->MakeVisible();
// 		INT min, max;
//		::GetScrollRange(form->ShellTreeView->Handle, SB_HORZ, &min, &max);
//		::SetScrollPos(form->ShellTreeView->Handle, SB_HORZ, max, true);
//		form->ShellTreeView->Selected->MakeVisible();
//		TreeView_EnsureVisible(form->ShellTreeView->Selected->Handle,
//			form->ShellTreeView->Selected->ItemId);
		}
		form->ShellTreeView->TopItem = topnode;
		topnode->MakeVisible();
	}

	int res = form->ShowModal();
	if(res == mrOk)
	{
		if(DirectoryExists(form->Selected))
		{
			form->Selected = IncludeTrailingBackslash(form->Selected);
		}
		strcpy(buf, form->Selected.c_str());
	}
	delete form;
	return res;
}
__fastcall TRelFolderSelectorForm::TRelFolderSelectorForm(TComponent* Owner)
	: TForm(Owner)
{
	ShellTreeView->Path = ExtractFileDir(ParamStr(0));

	// adjust components
	Label1->Left = SPACER;
	Label1->Top = SPACER;
	ShellComboBox->Left = SPACER;
	ShellComboBox->Top = SPACER + Label1->Height + 2;
	ShellComboBox->Width = ClientWidth - SPACER * 2;
	ShellTreeView->Left = SPACER;
	ShellTreeView->Top = ShellComboBox->Height + ShellComboBox->Top;
	ShellTreeView->Width = ClientWidth - SPACER * 2;
	OKButton->Top = ClientHeight - OKButton->Height - SPACER;
	CancelButton->Top = ClientHeight - CancelButton->Height - SPACER;
	OKButton->Left = ClientWidth - (StatusBar->Width + SPACER + CancelButton->Width
		+ SPACER + OKButton->Width);
	CancelButton->Left = ClientWidth - ( StatusBar->Width + SPACER + CancelButton->Width);
	ShellTreeView->Height = CancelButton->Top - SPACER - ShellTreeView->Top;

	if(ShellTreeView->Selected)
	{
		ShellTreeView->Selected->Expanded = true;
		TTreeNode * topnode = ShellTreeView->Selected;
		if(topnode->Count >= 1)
		{
			TTreeNode * longestitem = NULL;
			int longesttextsize = -1;
			for(int i = 0; i < topnode->Count; i++)
			{
				TTreeNode *subnode = topnode->Item[i];
				TRect rect = subnode->DisplayRect(true);
				if(rect.right > longesttextsize)
				{
					longesttextsize = rect.right;
					longestitem = topnode->Item[i];
				}
			}
			if(longestitem)
				longestitem->MakeVisible();
		}
		ShellTreeView->TopItem = topnode;
		topnode->MakeVisible();
	}

}