
void CBoxPannelLayout::InitBoxLayout()
{
	std::vector<module_layout_info> vtModLayoutInfo;
	CA_BoxHelper::GetModuleLayoutInfo(vtModLayoutInfo);
	if (vtModLayoutInfo.empty())  return;

	for (size_t i = 0; i < vtModLayoutInfo.size(); i++)
	{
		BOOL bFind = FALSE;
		int nModGuidIndex = -1;
		int nW = 32, nH = 32;
		for (size_t j = 0; j < m_vtModuleCommInfo.size(); j++)
		{
			ModuleCommInfo* pModCommInfo = m_vtModuleCommInfo[j];
			if (pModCommInfo && pModCommInfo->strModGuid == vtModLayoutInfo[i].ac_guid)
			{
				bFind = TRUE;
				nW = pModCommInfo->nW;
				nH = pModCommInfo->nH;
				nModGuidIndex = j;
				break;
			}
		}

		// 没有对应的common信息
		if (!bFind)
		{
			ModuleCommInfo* pModCommInfo = new ModuleCommInfo;
			pModCommInfo->strModGuid = vtModLayoutInfo[i].ac_guid;
			CA_BoxHelper::GetModulePixInfo(pModCommInfo->strModGuid.c_str(), pModCommInfo->m_vtPixel);
			pModCommInfo->strModName = CA_BoxHelper::GetModuleName(pModCommInfo->strModGuid.c_str());
			nW = pModCommInfo->nW = CA_BoxHelper::GetModuleWidth(pModCommInfo->strModGuid.c_str());
			nH = pModCommInfo->nH = CA_BoxHelper::GetModuleHeight(pModCommInfo->strModGuid.c_str());
			BOOL bSpecialShaped = FALSE;
			bSpecialShaped = (UINT)nH * nW > pModCommInfo->m_vtPixel.size();
			
			//CReate Rgn
			HRGN hRgnTemp = bSpecialShaped ? CreateRectRgn(0, 0, 0, 0) : 0;
			int nPixUnit = m_nPixUnit;
			CRect rcUnit;
			int x = 0, y = 0;
			if (bSpecialShaped)
			{
				int nBorder = 2;
				CRect rect(0, 0, pModCommInfo->nW * nPixUnit + nBorder, pModCommInfo->nH * nPixUnit + nBorder);
				for (size_t c = 0; c < pModCommInfo->m_vtPixel.size(); c++)
				{
					//异形 
					x = LOWORD(pModCommInfo->m_vtPixel[c]);
					y = HIWORD(pModCommInfo->m_vtPixel[c]);
					rcUnit.top = nPixUnit * y + nBorder;
					rcUnit.left = nPixUnit * x + nBorder;
					rcUnit.right = rcUnit.left + nPixUnit - 1;
					rcUnit.bottom = rcUnit.top + nPixUnit - 1;

					HRGN hRgnUnit = CreateRectRgnIndirect(&rcUnit);
					if (hRgnUnit != NULL)
					{
						::CombineRgn(hRgnTemp, hRgnTemp, hRgnUnit, RGN_OR);
						::DeleteObject(hRgnUnit);
					}
				}
				
				HRGN hBorder = CreateRectRgnIndirect(&rect);
				rect.InflateRect(-nBorder, -nBorder);
				HRGN hClip = CreateRectRgnIndirect(&rect);
				::CombineRgn(hBorder, hBorder, hClip, RGN_DIFF);
				::CombineRgn(hRgnTemp, hRgnTemp, hBorder, RGN_OR);
				::DeleteObject(hBorder);
				::DeleteObject(hClip);
			}
			pModCommInfo->hRgn = hRgnTemp;
			m_vtModuleCommInfo.push_back(pModCommInfo);
			nModGuidIndex = (int)m_vtModuleCommInfo.size() - 1;
		}

		CModuleCmptGraph*  pCmpt = new CModuleCmptGraph(this);
		int nJx = vtModLayoutInfo[i].i_jx_idx;
		pCmpt->SetJXInfo(nJx, vtModLayoutInfo[i].i_jx_module_idx);
		pCmpt->SetModGuidIndex(nModGuidIndex);
		AddComponent(pCmpt, CRect(CPoint(vtModLayoutInfo[i].i_x, vtModLayoutInfo[i].i_y), CSize(nW, nH)));

		if (nJx >= 0)
		{
			MAP_JX_MODINFO::iterator it = m_mapJx2ModInfo.find(nJx);
			if (it != m_mapJx2ModInfo.end())
			{
				it->second.push_back(pCmpt);
			}
			else
			{
				m_mapJx2ModInfo[nJx].push_back(pCmpt);
			}
		}
		else
		{
			nJx = -1;
			m_mapJx2ModInfo[nJx].push_back(pCmpt);
		}
	}

	for (MAP_JX_MODINFO::iterator it = m_mapJx2ModInfo.begin(); it != m_mapJx2ModInfo.end(); it++)
	{
		m_lstJX.push_back(it->first);
		if (!it->second.empty())
		{
			std::sort(it->second.begin(), it->second.end(), CModuleCmptGraph::FunSortVarJxOder);
		}
	}

	SaveData();
}


CCmptGraphBase* CBoxPannelLayout::GetCmptFromPoint(POINT& pt, CRect& rect, BOOL bSel /*= TRUE*/)
{
	CCmptGraphBase* pComponent = NULL;
	std::vector<CCmptGraphBase*> vtCmpt;
	for (std::vector<CCmptGraphBase*>::iterator it = m_vtComponents.begin(); it != m_vtComponents.end(); it++)
	{
		pComponent = *it;
		if (pComponent)
		{
			pComponent->GetOuterRect(rect, m_rect.left, m_rect.top);
			if (rect.PtInRect(pt))
			{
				vtCmpt.push_back(pComponent); 
			}
		}
	}

	if (vtCmpt.empty()) return NULL;

	CModuleCmptGraph* pCmpt = NULL;

	if (vtCmpt.size() > 1)
	{
		std::sort(vtCmpt.begin(), vtCmpt.end(), CCmptGraphBase::SortByTimeTicket);
		pCmpt = (CModuleCmptGraph*)*vtCmpt.rbegin();
	}
	else
	{
		pCmpt = (CModuleCmptGraph*)*vtCmpt.rbegin();
	}

	if (pCmpt && bSel)
	{
		if (pCmpt->m_nModJx == -1)
		{
			MAP_JX_MODINFO::iterator itFind = m_mapJx2ModInfo.find(pCmpt->m_nModJx);
			if (itFind != m_mapJx2ModInfo.end())
			{
				pCmpt->m_dwLastUpdateTMTicket = GetTickCount();
				std::sort(itFind->second.begin(), itFind->second.end(), CCmptGraphBase::SortByTimeTicket);
			}
			else
			{
				ATLASSERT(FALSE);
			}
		}
		else
		{
			MAP_JX_MODINFO::iterator itFind = m_mapJx2ModInfo.find(pCmpt->m_nModJx);
			if (itFind != m_mapJx2ModInfo.end())
			{
				for (std::vector<CModuleCmptGraph*>::iterator itVec = itFind->second.begin(); itVec != itFind->second.end(); itVec++)
				{
					CModuleCmptGraph* pCmptTem = *itVec;
					pCmptTem->m_dwLastUpdateTMTicket = GetTickCount();
				}
				//pCmpt->m_dwLastUpdateTMTicket += 100;
				//std::sort(itFind->second.begin(), itFind->second.end(), CCmptGraphBase::SortByTimeTicket);

				std::list<int>::iterator itList = std::find(m_lstJX.begin(), m_lstJX.end(), pCmpt->m_nModJx);
				if (itList != m_lstJX.end())
				{
					m_lstJX.erase(itList);
					m_lstJX.push_back(pCmpt->m_nModJx);
				}
				else
				{
					ATLASSERT(FALSE);
				}
			}
			else
			{
				ATLASSERT(FALSE);
			}
		}

		pCmpt->GetOuterRect(rect, m_rect.left, m_rect.top);
	}

	return pCmpt;
}































