////////////////////////////////////////////////////////////////////////// // Class Name: SItemBox // Description: Items Container // Creator: huangjianxiong // Version: 2011.7.8 - 1.0 - Create ////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "sitembox.h" namespace SOUI { SItemBox::SItemBox():m_nItemWid(100),m_nItemHei(100),m_nSepHei(5),m_nSepWid(5) { } SWindow* SItemBox::InsertItem(LPCWSTR pszXml,int iItem/*=-1*/,BOOL bEnsureVisible/*=FALSE*/) { pugi::xml_document xmlDoc; if(!xmlDoc.load_buffer(pszXml,wcslen(pszXml),pugi::parse_default,pugi::encoding_utf16)) return NULL; SWindow *pChild=m_pFirstChild,*pPrevChild=ICWND_FIRST; for(int iChild=0; iChildGetWindow(GSW_NEXTSIBLING); } SWindow *pPanel=new SWindow; InsertChild(pPanel, pPrevChild); pPanel->CreateChildren(xmlDoc.first_child()); pPanel->SetVisible(TRUE); pPanel->GetLayoutParam()->SetSpecifiedSize(Horz,m_nItemWid); pPanel->GetLayoutParam()->SetSpecifiedSize(Vert, m_nItemHei); UpdateScroll(); ReLayout(); if(bEnsureVisible) EnsureVisible(pPanel); Invalidate(); return pPanel; } SWindow* SItemBox::InsertItem(pugi::xml_node xmlNode,int iItem/*=-1*/,BOOL bEnsureVisible/*=FALSE*/) { if (!xmlNode) return NULL; SWindow *pChild=m_pFirstChild,*pPrevChild=ICWND_FIRST; for(int iChild=0; iChildGetWindow(GSW_NEXTSIBLING); } SWindow *pPanel=new SWindow; InsertChild(pPanel, pPrevChild); pPanel->CreateChildren(xmlNode); pPanel->SetVisible(TRUE); pPanel->GetLayoutParam()->SetSpecifiedSize(Horz,m_nItemWid); pPanel->GetLayoutParam()->SetSpecifiedSize(Vert,m_nItemHei); UpdateScroll(); ReLayout(); if(bEnsureVisible) EnsureVisible(pPanel); Invalidate(); return pPanel; } BOOL SItemBox::RemoveItem(UINT iItem) { if(iItem>=GetItemCount()) return FALSE; SWindow *pChild=m_pFirstChild; for(UINT iChild=0; iChildGetWindow(GSW_NEXTSIBLING); } DestroyChild(pChild); UpdateScroll(); ReLayout(); Invalidate(); return TRUE; } BOOL SItemBox::RemoveItem(SWindow * pChild) { if (DestroyChild(pChild)) { UpdateScroll(); ReLayout(); Invalidate(); return TRUE; } return FALSE; } void SItemBox::BringWindowAfter(SWindow * pChild, SWindow * pInsertAfter) { RemoveChild(pChild); InsertChild(pChild, pInsertAfter); } BOOL SItemBox::SetNewPosition(SWindow * pChild, DWORD nPos, BOOL bEnsureVisible) { if (pChild == NULL) { return FALSE; } SWindow * pCurChild = m_pFirstChild; DWORD nCurPos = 0; for (; pCurChild != NULL; pCurChild = pCurChild->GetWindow(GSW_NEXTSIBLING)) { if (pCurChild == pChild) { break; } ++nCurPos; } if (pCurChild == NULL) { return FALSE; } if (nCurPos == nPos) { if (bEnsureVisible) { EnsureVisible(pChild); } Invalidate(); return TRUE; } if (nPos == 0) { BringWindowAfter(pChild, ICWND_FIRST); } else { SWindow * pNewNext = m_pFirstChild; for (UINT i = 0; i < nPos && pNewNext != NULL; i++) { pNewNext = pNewNext->GetWindow(GSW_NEXTSIBLING); } BringWindowAfter(pChild, pNewNext); } UpdateScroll(); ReLayout(); if (bEnsureVisible) { EnsureVisible(pChild); } Invalidate(); return TRUE; } int SItemBox::GetItemPos(SWindow * lpCurItem) { if (lpCurItem == NULL) { return -1; } int nPos = 0; for (SWindow *pChild = m_pFirstChild; pChild != NULL; pChild = pChild->GetWindow(GSW_NEXTSIBLING), ++nPos) { if (pChild == lpCurItem) { return nPos; } } return -1; } void SItemBox::RemoveAllItems() { SWindow::OnDestroy(); UpdateScroll(); Invalidate(); } UINT SItemBox::GetItemCount() { return GetChildrenCount(); } void SItemBox::PageUp() { OnScroll(TRUE,SB_PAGEUP,0); } void SItemBox::PageDown() { OnScroll(TRUE,SB_PAGEDOWN,0); } void SItemBox::EnsureVisible(SWindow *pItem) { if(!HasScrollBar(TRUE)) return; SASSERT(pItem); CRect rcItem; pItem->GetWindowRect(&rcItem); int yOffset=0; if(rcItem.bottom>GetWindowRect().bottom) { yOffset=rcItem.bottom-GetWindowRect().bottom; } else if(rcItem.topMove(rcItem); pChild=pChild->GetWindow(GSW_NEXTSIBLING); iItem++; } } BOOL SItemBox::OnScroll(BOOL bVertical,UINT uCode,int nPos) { if(!__super::OnScroll(bVertical,uCode,nPos)) return FALSE; ReLayout(); return TRUE; } int SItemBox::GetScrollLineSize(BOOL bVertical) { if(bVertical) return m_nItemHei+m_nSepHei; else return m_nItemWid+m_nSepWid; } BOOL SItemBox::CreateChildren(pugi::xml_node xmlNode) { if(!xmlNode) return FALSE; RemoveAllItems(); pugi::xml_node xmlParent=xmlNode.parent(); pugi::xml_node xmlItem=xmlParent.child(L"item"); while(xmlItem) { SWindow *pChild=new SWindow; InsertChild(pChild); pChild->InitFromXml(xmlItem); pChild->SetVisible(TRUE); pChild->GetLayoutParam()->SetSpecifiedSize(Horz,m_nItemWid); pChild->GetLayoutParam()->SetSpecifiedSize(Vert,m_nItemHei); xmlItem=xmlItem.next_sibling(L"item"); } return TRUE; } }//namespace SOUI