#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	type_window,
	type_folder,
};

//put additional include files here

/// Modification Log:
/// Sim 12-11-2009 QA81-14817 SUPPORT_SEARCH_PE_FOLDER
/// CPY 12/14/09 QA81-14817 RENAME_PAGE_VAR_TO_NAME
/// Sophy 12/22/2009 QA80-14817-S3 SUPPORT_ACTIVATE_TARGET_IF_FIND_IN_PE_PATH
/// Sophy 12/22/2009 QA80-14855 IMPROVE_RESULT_MESSAGE_FOR_PE_PATH
/// CPY 2/08/10 UNDO_RENAME_PAGE_VAR_TO_NAME, since Mr Nojo's tool already used this XF, even though the var=page part is really poor choice of name
/// CPY 2010-02-08 CURRENT_PATH_OUTPUT_TO_STR_VAR_BROKEN_IN_81_SR2, due to Sophy 12/22/2009 QA80-14855 IMPROVE_RESULT_MESSAGE_FOR_PE_PATH
/// Folger 10/21/2010 ORG-1303-P1 CLEANUP_ERROR_MESSAGE_THROWING_IN_PE_XFS


//put your own support static functions here
///---Sim 12-11-2009 QA81-14817 SUPPORT_SEARCH_PE_FOLDER
// this function used to find the specified subfolder in the root folder
static bool _find_subfolder(string &strFolder, LPCSTR lpcszRootPath = NULL)
{
	// trim white space
	strFolder.TrimRight();
	strFolder.TrimLeft();
	
	Folder fld(lpcszRootPath);  // get root folder path
	
	if(fld.IsValid())   // if root folder exist
	{
		foreach(Folder subfolder in fld.Subfolders)  // loop all subfolders
		{
			// if subfolder exist
			if(0 == subfolder.GetName().CompareNoCase(strFolder) )
			{
				strFolder = subfolder.GetPath();
				return true;				
			}
			else
			{			
				if( subfolder.Subfolders.Count() > 0 )  // search subfolder's subfolder
				{
					string strSubfolder = subfolder.GetPath();
					if ( _find_subfolder(strFolder, strSubfolder) )
						return true;
				}
			}
		}
	}
	return false;
}
// this function used to find the specified subfolder in the active folder
static bool _find_subfolder_in_acitve_folder(string &strFolder)
{
	Folder fld = Project.ActiveFolder();
	string strSubfolder = fld.GetPath();
	return _find_subfolder(strFolder, strSubfolder);
}
///---END QA81-14817 SUPPORT_SEARCH_PE_FOLDER


void pe_path(const string& page, string& path, const int& type, const int& active)
{
	string strpath;
	//---- CPY 12/14/09 QA81-14817 RENAME_PAGE_VAR_TO_NAME
	//	string page = name;
	//----
	bool bCurrentPath = false; //---- CPY 2010-02-08 CURRENT_PATH_OUTPUT_TO_STR_VAR_BROKEN_IN_81_SR2
	
	///Sophy 12/22/2009 QA80-14855 IMPROVE_RESULT_MESSAGE_FOR_PE_PATH
	if ( type_folder == type && path ) //folder, need to check input folder
	{
		Folder fdrStart(path);
		if ( !fdrStart.IsValid() )
		{
			///------ Folger 10/21/2010 ORG-1303-P1 CLEANUP_ERROR_MESSAGE_THROWING_IN_PE_XFS
			//warning_msg_box(XF_CD_NOT_VALID_PATH, path, false, 'R');
			//return;
			XF_THROW_EX(XF_CD_NOT_VALID_PATH, path);
			///------ End CLEANUP_ERROR_MESSAGE_THROWING_IN_PE_XFS
		}
	}
	///end IMPROVE_RESULT_MESSAGE_FOR_PE_PATH
	if( page.IsEmpty() )
	{
		strpath = Project.ActiveFolder().GetPath();
		//---- CPY 2010-02-08 CURRENT_PATH_OUTPUT_TO_STR_VAR_BROKEN_IN_81_SR2
		/////Sophy 12/22/2009 QA80-14855 IMPROVE_RESULT_MESSAGE_FOR_PE_PATH
		//warning_msg_box(XF_PE_PATH_CURRENT_PATH, strpath, false, 'R');
		//return;
		bCurrentPath = true;
		//----
		///end IMPROVE_RESULT_MESSAGE_FOR_PE_PATH
	}
	else
	{
		///---Sim 12-11-2009 QA81-14817 SUPPORT_SEARCH_PE_FOLDER
		switch ( type )
		{
		case type_window:
		///---END QA81-14817 SUPPORT_SEARCH_PE_FOLDER
			Folder fldLocation = Project.GetFolderWithPage(page);
			if( fldLocation.IsValid() )
			{
				strpath = fldLocation.GetPath();
				///Sophy 12/22/2009 QA80-14817-S3 SUPPORT_ACTIVATE_TARGET_IF_FIND_IN_PE_PATH
				if ( active )
				{
					Page pg = fldLocation.Pages(page);
					if ( pg )
						pg.CheckShowActivate();
				}
				///end SUPPORT_ACTIVATE_TARGET_IF_FIND_IN_PE_PATH
			}
		///---Sim 12-11-2009 QA81-14817 SUPPORT_SEARCH_PE_FOLDER
			break;
		case type_folder:
			///---Sim 12-11-2009 QA81-14817 SUPPORT_SEARCH_PE_FOLDER
			string strFolder = page;
			string strStartPath = ( path) ? path : "";
			bool bRet;
			if ( strStartPath.IsEmpty() )
				bRet = _find_subfolder_in_acitve_folder(strFolder);
			else
				bRet = _find_subfolder(strFolder, strStartPath);
			
			if ( bRet )
				strpath = strFolder;
			///Sophy 12/22/2009 QA80-14817-S3 SUPPORT_ACTIVATE_TARGET_IF_FIND_IN_PE_PATH
			if ( active && !strpath.IsEmpty() )
			{
				Folder fdrTarget(strpath);
				if ( fdrTarget.IsValid() )
				{
					fdrTarget.Activate();
				}
			}
			///end SUPPORT_ACTIVATE_TARGET_IF_FIND_IN_PE_PATH
			break;
		}
		///---END QA81-14817 SUPPORT_SEARCH_PE_FOLDER
	}
	if(path)
		path = strpath;
	else
	{
		if(strpath.IsEmpty())
			///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
			//printf("Error!!! %s: not a valid page!\n", page);
			/// Max 10/24/07 USE_WRONG_PROTOTYPE
			//warning_msg_box(XFERR_PE_PATH_INVALID_PAGE, false, 'E');
			///---Sim 12-11-2009 QA81-14817 SUPPORT_SEARCH_PE_FOLDER
			//warning_msg_box(XFERR_PE_PATH_INVALID_PAGE, page, false, 'E');
			{
				int nErrCode = XFERR_PE_PATH_INVALID_PAGE;
				if ( type_folder == type )
					nErrCode = XFERR_PE_PATH_INVALID_SUB_FOLDER;
				///------ Folger 10/21/2010 ORG-1303-P1 CLEANUP_ERROR_MESSAGE_THROWING_IN_PE_XFS
				//warning_msg_box(nErrCode, page, false, 'E');
				XF_THROW_EX(nErrCode, page);
				///------ End CLEANUP_ERROR_MESSAGE_THROWING_IN_PE_XFS
			}
			///---END QA81-14817 SUPPORT_SEARCH_PE_FOLDER
			/// END USE_WRONG_PROTOTYPE
			///end UPDATE_XF_ERR_THROW
		else
			///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
			//printf("Current path is %s\n", strpath);
			/// Max 10/24/07 USE_WRONG_PROTOTYPE
			//warning_msg_box(XF_PE_PATH_CURRENT_PATH, false, 'R');
			///Sophy 12/22/2009 QA80-14855 IMPROVE_RESULT_MESSAGE_FOR_PE_PATH
			//warning_msg_box(XF_PE_PATH_CURRENT_PATH, strpath, false, 'R');
			//---- CPY 2010-02-08 CURRENT_PATH_OUTPUT_TO_STR_VAR_BROKEN_IN_81_SR2
			//warning_msg_box(XF_PE_PATH_FOUND_PATH, strpath, false, 'R');
			warning_msg_box(bCurrentPath?XF_PE_PATH_CURRENT_PATH:XF_PE_PATH_FOUND_PATH, strpath, false, 'R');
			//----
			///end IMPROVE_RESULT_MESSAGE_FOR_PE_PATH
			/// END USE_WRONG_PROTOTYPE
			///end UPDATE_XF_ERR_THROW
	}
	
}

//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int pe_path_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return pe_path_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

int pe_path_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return false;
}
//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void pe_path_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int pe_path_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
