#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <event_utils.h>
#include <GetNbox.h>
#include <..\originlab\theme_utils.h>

//------ Folger 08/08/07 GROUP_OPERATION_AS_BLOCK_TO_UNDO_ONCE
#define BEFORE_UNDO_BLOCK				/*int idBefore = Project.UndoBlockId(); */{ UndoBlock block;
#define AFTER_UNDO_BLOCK				} /*int idAfter = Project.UndoBlockId();*/
//------ End GROUP_OPERATION_AS_BLOCK_TO_UNDO_ONCE


void wcolwidth(const Range& irng, const double& width)
{
	//------ Folger 01/18/08 QA70-10946 SUPPORT_MULTIPLE_SHEETS_OPERATION_WITH_SHEET_RANGE_LIST_CLASS
	/*
	int nSubRange = irng.GetNumRanges(), r1, r2, c1, c2;
	if(!nSubRange)
		XF_THROW(XFERR_INVALID_ARG)
	Worksheet wks;
	vector<uint> vnCols, vnIndeces, uBegins, uEnds;
	for(int ii = 0; ii < nSubRange; ii++)
	{
		irng.GetRange(ii, r1, c1, r2, c2, wks);
		if(c1 < 0) c1 = 0;
		if(c2 < 0) c2 = wks.GetNumCols() - 1;
		for(int nCol = c1; nCol <= c2; nCol++)
		{
			if(1 > vnCols.Find(vnIndeces, nCol))
				vnCols.Add(nCol);			
		}
	}
	int nRange = find_contiguous_range(vnCols, uBegins, uEnds);
	//	BEFORE_UNDO_BLOCK	//------ Folger 08/08/07 GROUP_OPERATION_AS_BLOCK_TO_UNDO_ONCE
	for(ii = 0; ii < nRange; ii++)
		wks_set_cell_heights(wks, uBegins[ii], uEnds[ii], width/6.6*100, true);	
	//	AFTER_UNDO_BLOCK	//------ Folger 08/08/07 GROUP_OPERATION_AS_BLOCK_TO_UNDO_ONCE
	*/
	SheetRangesList rngList(irng);
	int nNumSheet = rngList.GetNumSheets();
	
	//UndoBlock ub;
	for ( int ii=0; ii<nNumSheet; ++ii )
	{
		int nNumSubRange = rngList.GetNumSubRange(ii);
		Worksheet wks;
		vector<uint> vnCols, vnIndeces;
		for ( int jj=0; jj< nNumSubRange; ++jj )
		{
			ORANGE orng;
			if ( rngList.GetRange(ii, wks, jj, &orng) )
			{
				if ( orng.c1 < 0 ) orng.c1 = 0;
				if ( orng.c2 < 0 ) orng.c2 = wks.GetNumCols() - 1;
				for ( ; orng.c1<=orng.c2; ++orng.c1 )
				{
					if ( vnCols.Find(vnIndeces, orng.c1) < 1 )
						vnCols.Add(orng.c1);
				}
			}
		}
		if ( wks )
		{
			vector<uint> uBegins, uEnds;
			int nRange = find_contiguous_range(vnCols, uBegins, uEnds);
			for ( jj=0; jj<nRange; ++jj )
			{
				wks_set_cell_heights(wks, uBegins[jj], uEnds[jj], width/6.6*100, true);
			}
		}
	}
	
}

//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 wcolwidth_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return wcolwidth_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	int nErrCode = check_1_column(trGetN.irng, true);
	if(nErrCode != CER_NO_ERROR)
	{
		bOKEnable = false;
		strErrMsg = nErrCode;
		if(nErrCode == CER_COL_NUM_TOO_FEW_EX)
			strErrMsg += ":" + 1;
	}
	//------
	//if(GETNE_ON_INIT == nEventID && !lstrlen(trGetN.width.strVal))
	//{
		//int r1, r2, c1, c2;
		//Worksheet wks;
		//Range rData;
		//TreeNode trRange = trGetN.irng;
		//okxf_resolve_tree_construct_range(&trRange, &rData);
		//if(rData && rData.GetNumRanges())
			//rData.GetRange(0, r1, c1, r2, c2, wks);
		//int nSize = wks_get_cell_heights(wks, c1, c2, true);
		//trGetN.width.strVal = (string)(round(nSize/100.0*6.6, 0));
	//}
	return true;

}

int wcolwidth_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 wcolwidth_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if (nGetNDialog == 1)
	{
		int r1, r2, c1, c2;
		Worksheet wks;
		Range rData;
		TreeNode trRange = trGetN.irng;
		okxf_resolve_tree_construct_range(&trRange, &rData);
		if(rData && rData.GetNumRanges())
			rData.GetRange(0, r1, c1, r2, c2, wks);
		int nSize = wks_get_cell_heights(wks, c1, c2, true);
		trGetN.width.strVal = (string)(round(nSize/100.0*6.6, 0));
	}
}
//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 wcolwidth_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
