#ifndef __UtilObj_h
#define __UtilObj_h

// 03.07.08 Fukushiro M. 1_xZkqqHx_iNhDiNoNiPjM_xHqqkZx_ ()
//#include "../MolipSDK/MpgMolipExport.h"	// For FTObjectType.

#include "LayerManager.h"	// For FDOZ_ON_SELECT.

class FCObjDynabase;

//----------<NOT EXPORT TO VIEWER>-----------

extern void GetResizeFrame (DBLRect& mmrtFrame,
							FCObjDynabase* const * pObjs, long lObjs);
extern void GetResizeFrame (DBLRect& mmrtFrame, const DWORDSet& stObjId);
extern const wchar_t* FFObjTypeToStr (FTObjectType nType);
// 05.04.28 Fukushiro M. 1_xZkqqHx_iNhDjCmHiJmB_xHqqkZx_ ()
extern DBLPoint FFGetFramePoint (const DBLRect& mmrtFrame, WORD wPosition);

//----------</NOT EXPORT TO VIEWER>-----------

inline FCObjDynabase* GetObject (FCObjDynabase* pObj) { return pObj; }

/*************************************************************************
 * <_xZkqqHx_iKnGjAjE_xHqqkZx_>	FFAddToLZSorted
 *
 * <_xZkqqHx_iLeAjEfM_xHqqkZx_>	_xZkqqHx_iChJiDeJiBfLiDfPiBfLiCmJiPfNiCmBiCmEiDfMiBfLiDgHiClDiCoKiClNjFjEjFgJiPfHiNiH_xHqqkZx_ vObjSorted _xZkqqHx_iCmJiBeB_xHqqkZx_pObj_xZkqqHx_iCpA_xHqqkZx_
 *			_xZkqqHx_jDeLjAnIiCmIiImKjChFiCmJjBhNjDpMiClHiCoJiBeC_xHqqkZx_
 *
 * <_xZkqqHx_iIpIjAjE_xHqqkZx_>	vSorted		:_xZkqqHx_iDfMiBfLiDgHiClDiCoKiClNjFjEjFgJiPfHiNiHiBeC_xHqqkZx_
 *			pObj		:_xZkqqHx_jCmHiJmBiClHiCoJjFjEjFgJiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jFnEjCgM_xHqqkZx_>	_xZkqqHx_jBhNjDpMiClDiCoKiClNiImKjChFiCpAjFnEiClHiBeCiKpJiCmJjFjEjFgJiCkKiPfHiNiHjCiGiCmJjBlGiNnNiClHiCoJiPoKiNiHiCmNiBeB_xHqqkZx_
 *			DWORD(-1) _xZkqqHx_iCpAjFnEiClHiBeC_xHqqkZx_
 *			_xZkqqHx_iClBiCmMiDfGiBfLiDgHiPoDiCmMjFjEjFgJiCmFiCmIiCkCiPoKiNiHiCoA_xHqqkZx_DWORD(-1) _xZkqqHx_iCpAjFnEiClHiBeC_xHqqkZx_
 *
 * <_xZkqqHx_jHjKjHpA_xHqqkZx_>	99.04.27 Fukushiro M. _xZkqqHx_iNoMjAkM_xHqqkZx_
 *			00.12.18 Fukushiro M. _xZkqqHx_iDiMiDeDiDiEiBfLjBmOiJjOiBeC_xHqqkZx_
 *			00.12.30 Fukushiro M. _xZkqqHx_iDgFiDjDiDhGiDiMiBfLiDgHiJlLiBeC_xHqqkZx_
 *************************************************************************/
template<class T>
DWORD FFAddToLZSorted (vector<T>& vSorted, T& item)
{
//----- 00.12.30 Fukushiro M. _xZkqqHx_jCmHiJmBiOgO_xHqqkZx_ ()-----
	// _xZkqqHx_iDfOiBfLiDfBiDgCiDgHjFjEjFgJiCmM_xHqqkZx_LZ_xZkqqHx_iDeJiBfLiDfPiBfLiCpAiLiBiCnPiCoJiBeC_xHqqkZx_
	FCLayerZOrder lzTarg = g_pDoc->ObjDynabaseToLZOrder(GetObject(item));
	// _xZkqqHx_iClBiCmMiDfGiBfLiDgHiCmJjBlGiNnNiClFiCmIiCkCjFjEjFgJiCmMiPoKiNiHiCmNjFjMiLeBiBeC_xHqqkZx_
	if (lzTarg.dwLayerIdx == DWORD(-1)) return DWORD(-1);
	// _xZkqqHx_jBeJjBpAjFhDiJmCjEfMiCmIiDiMiDeDiDiEiBfLiCmMjFjEjFgJiCmMiPoKiNiHiCmNjFjMiLeBiBeC_xHqqkZx_
	WORD wFlag;
	g_pDoc->GetCurrentSheet()->GetLayerInfo(NULL, &wFlag, lzTarg.dwLayerIdx);
	if (!(wFlag & FDOZ_ON_SELECT)) return DWORD(-1);
//----- 00.12.30 Fukushiro M. _xZkqqHx_jCmHiJmBiPeJ_xHqqkZx_ ()-----
	// vSorted_xZkqqHx_iCkKiLpDiCmMiPoKiNiHiBeC_xHqqkZx_
	if (vSorted.empty())
	{
		vSorted.push_back(item);
		return 0;
	}
//----- 00.12.30 Fukushiro M. _xZkqqHx_iNoNiPjMiOgO_xHqqkZx_ ()-----
//	// _xZkqqHx_iDfOiBfLiDfBiDgCiDgHjFjEjFgJiCmM_xHqqkZx_LZ_xZkqqHx_iDeJiBfLiDfPiBfLiCpAiLiBiCnPiCoJiBeC_xHqqkZx_
//	FCLayerZOrder lzTarg = g_pDoc->ObjDynabaseToLZOrder(GetObject(item));
//----- 00.12.30 Fukushiro M. _xZkqqHx_iNoNiPjMiPeJ_xHqqkZx_ ()-----
	// _xZkqqHx_jFjEjFgJiPfHiNiHiCmMiNmFiPiJiCmMjFjEjFgJiCoGiCoIiCoAiBeBiDfOiBfLiDfBiDgCiDgHiCkKiPkMiClDiCkCiCkJiCmHiCkEiCkJiCpAjClCiCnHiCoJiBeC_xHqqkZx_
	FCLayerZOrder lzCur = g_pDoc->ObjDynabaseToLZOrder(GetObject(vSorted.front()));
	if (lzTarg < lzCur)
	{
		vSorted.insert(vSorted.begin(), item);
		return 0;
	} else if (lzTarg == lzCur)
	{
		return DWORD(-1);
	}
	// _xZkqqHx_jFjEjFgJiPfHiNiHiCmMiNmFiMoDiCmMjFjEjFgJiCoGiCoIiCoAiBeBiDfOiBfLiDfBiDgCiDgHiCkKjBoFiCkLiCkCiCkJiCmHiCkEiCkJiCpAjClCiCnHiCoJiBeC_xHqqkZx_
	lzCur = g_pDoc->ObjDynabaseToLZOrder(GetObject(vSorted.back()));
	if (lzCur < lzTarg)
	{
		vSorted.push_back(item);
		return vSorted.size() - 1;
	} else if (lzTarg == lzCur)
	{
		return DWORD(-1);
	}
	DWORD dwMin = 0;
	DWORD dwMax = vSorted.size() - 1;
	while (1 < dwMax - dwMin)
	{
		DWORD dwCur = (dwMin + dwMax) / 2;
		lzCur = g_pDoc->ObjDynabaseToLZOrder(GetObject(vSorted[dwCur]));
		if (lzTarg < lzCur)
		{
			dwMax = dwCur;
		} else if (lzCur < lzTarg)
		{
			dwMin = dwCur;
		} else
		{
			return DWORD(-1);
		}
	}
	vSorted.insert(&vSorted.at(dwMax), item);
	return dwMax;
} // FFAddToLZSorted.

#endif//__UtilObj_h
