#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	option_lname,
	option_lsname,
	option_enumln,
};
//put additional include files here

/*
* Iris 12/06/2006 IF_-1_AS_T_NUM_OF_SHEETS_FROM_TEMPLATE
* CPY 8/9/2007 ADDED_SUPPORT_FOR_MATRIX_BOOK
* CPY 3/31/08 QA70-11336 NEWBOOK_LONGNAME_ENUM_CLEANUP			
* Hong 03/10/10 QA80-15180 SUPPORT_PAGE_LONGNAME_BE_UNIQUE
*/
//------- CPY 3/31/08 QA70-11336 NEWBOOK_LONGNAME_ENUM_CLEANUP
// I found it better to just rewrite the whole thing so old code is kept here
/*
//put your own support static functions here
enum {
	LONGNAME_ONLY,
	LONG_AND_SHORT_NAME,
};
	//------ CPY 8/9/2007 ADDED_SUPPORT_FOR_MATRIX_BOOK
	//WorksheetPage wp;
	Page wp;
	int nPageType = mat? EXIST_MATRIX:EXIST_WKS;
	//------ 
	int nNeededSheets = sheet;
	//---- Iris 12/06/2006 IF_-1_AS_T_NUM_OF_SHEETS_FROM_TEMPLATE
	//if(nNeededSheets < 0) // get out if named book already existed, number of sheet is 0 if not found
	if( !name.IsEmpty() )
	//---- 
	{
		wp = Project.FindPage(name, nPageType, -1);
		if(wp)
		{
			result = wp.GetName();
			return;
		}
		/// Iris 12/06/2006 IF_-1_AS_T_NUM_OF_SHEETS_FROM_TEMPLATE
		//// not found, need to create
		//nNeededSheets = 0;
		///end IF_-1_AS_T_NUM_OF_SHEETS_FROM_TEMPLATE
	}
	
	DWORD dwCntrl = hidden > 0? CREATE_HIDDEN:CREATE_VISIBLE;
	if ( keep )
		dwCntrl |= CREATE_KEEP_LAYER_NAMES;
	//------ CPY 8/9/2007 ADDED_SUPPORT_FOR_MATRIX_BOOK
	//wp.Create(template, dwCntrl);
	if(mat)
	{
		MatrixPage mpg;
		mpg.Create(template, dwCntrl);
		wp = mpg;
	}
	else
	{
		WorksheetPage wpg;
		wpg.Create(template, dwCntrl);
		wp = wpg;
	}
	//------ end 
	if(name.GetLength() > 0)
	{
		switch(option)
		{
		case LONGNAME_ONLY:
			wp.SetName(name, OCD_KEEP_SN | OCD_LONG_NAME | OCD_ENUM_NEXT);
			break;
		default:
			wp.SetName(name, OCD_LONG_NAME | OCD_ENUM_NEXT);
			break;
		}
	}
	
	
	wp.TitleShow = WIN_TITLE_SHOW_BOTH;
	result = wp.GetName();
	/// Iris 12/06/2006 IF_-1_AS_T_NUM_OF_SHEETS_FROM_TEMPLATE
	if(nNeededSheets < 0)
		return;
	///end 1_AS_T_NUM_OF_SHEETS_FROM_TEMPLATE
	
	int nLayers = wp.Layers.Count();
	if(nNeededSheets > nLayers)
	{
		for(int ii = nLayers; ii < nNeededSheets; ii++)
			wp.AddLayer();
	}
	else if(nNeededSheets < nLayers)
	{
		for(int ii = nNeededSheets; ii < nLayers; ii++)
		{
			Worksheet wks = wp.Layers(nNeededSheets);
			wks.Destroy(OCD_DEL_LINKED_OBJS);
		}
	}
*/



void newbook(const string& name, const int& sheet, const string& template, string& result, const int& hidden, const int& option, const int& mat, const int& keep, const int& chkname)
{
	
	//------- CPY 3/31/08 QA70-11336 NEWBOOK_LONGNAME_ENUM_CLEANUP
	// newly written code, old code saved above
	
	Page wp;
	int nPageType = mat? EXIST_MATRIX:EXIST_WKS;
	BOOL bNameGiven = !name.IsEmpty();
	int nNeededSheets = sheet;
	if(bNameGiven && chkname)
	{
		wp = Project.FindPage(name, nPageType, -1);
		if(wp)
		{
			result = wp.GetName();
			return;
		}
	}
	
	DWORD dwCntrl = hidden > 0? CREATE_HIDDEN:CREATE_VISIBLE;
	if ( keep )
		dwCntrl |= CREATE_KEEP_LAYER_NAMES;
	
	if(mat)
	{
		MatrixPage mpg;
		mpg.Create(template, dwCntrl);
		wp = mpg;
	}
	else
	{
		WorksheetPage wpg;
		wpg.Create(template, dwCntrl);
		wp = wpg;
	}	
	if(bNameGiven)
	{
		// only if name end in 1 that we enum
		char szNamePrefix[2*MAXLINE];
		int nn = string_to_prefix_end_number(szNamePrefix, name);
		switch(option)
		{
		case option_enumln:
			{
				string strName = szNamePrefix;
				strName += "1"; // must terminate with 1 to enable internal code to add 1 and enum
				/// Hong 03/10/10 QA80-15180 SUPPORT_PAGE_LONGNAME_BE_UNIQUE
				//wp.SetName(strName, OCD_KEEP_SN | OCD_LONG_NAME | OCD_ENUM_NEXT);
				wp.SetName(strName, OCD_KEEP_SN | OCD_LONG_NAME | OCD_ENUM_NEXT_KEEP_CONSISTENT);
				/// end SUPPORT_PAGE_LONGNAME_BE_UNIQUE
			}
			break;
		case option_lname:
			wp.SetName(name, OCD_KEEP_SN | OCD_LONG_NAME);
			break;
		default:
			wp.SetName(name, OCD_LONG_NAME | OCD_ENUM_NEXT);
			break;
		}
	}	
	wp.TitleShow = WIN_TITLE_SHOW_BOTH;
	result = wp.GetName();
	if(nNeededSheets < 0)
		return;	
	int nLayers = wp.Layers.Count();
	if(nNeededSheets > nLayers)
	{
		for(int ii = nLayers; ii < nNeededSheets; ii++)
			wp.AddLayer();
	}
	else if(nNeededSheets < nLayers)
	{
		for(int ii = nNeededSheets; ii < nLayers; ii++)
		{
			Worksheet wks = wp.Layers(nNeededSheets);
			wks.Destroy(OCD_DEL_LINKED_OBJS);
		}
	}
	//------- end NEWBOOK_LONGNAME_ENUM_CLEANUP
	
}

//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 newbook_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return newbook_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

int newbook_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 newbook_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 newbook_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
