der->GetWidth();

				if (deltaLeft <= 0 && deltaRight <= 0)
				{
				}
				else if (deltaLeft > 0 && deltaRight <= 0)
				{
					rectFinalLeft.right += deltaLeft;
					rectFinalRight.left = rectFinalLeft.right + nSliderWidth;
					if (rectFinalRight.Width() < sizeMinRight.cx)
					{
						rectFinalRight.right = rectFinalRight.left + sizeMinRight.cx;
					}
				}
				else if (deltaLeft <= 0 && deltaRight > 0)
				{
					rectFinalLeft.right -= deltaRight;
					if (rectFinalLeft.Width() < sizeMinLeft.cx)
					{
						rectFinalLeft.right = rectFinalLeft.left + sizeMinLeft.cx;
					}

					rectFinalRight.left = rectFinalLeft.right + nSliderWidth;
					rectFinalRight.right = rectFinalRight.left + sizeMinRight.cx;
				}
				else if (deltaLeft > 0 && deltaRight > 0)
				{
					rectFinalLeft.right = rectFinalLeft.left + sizeMinLeft.cx;
					rectFinalRight.left = rectFinalLeft.right + nSliderWidth;
					rectFinalRight.right = rectFinalRight.left + sizeMinRight.cx;
				}

				rectFinalSlider.left = rectFinalLeft.right;
				rectFinalSlider.right = rectFinalSlider.left + nSliderWidth;

				dLeftPercent = ((double) rectFinalLeft.Width()) / rectContainer.Width() * 100;

				if (rectFinalLeft.Height() < sizeMinLeft.cy)
				{
					rectFinalLeft.bottom = rectFinalLeft.top + sizeMinLeft.cy;
					rectFinalRight.bottom = rectFinalRight.top + sizeMinLeft.cy;
				}
			}
		}

		if (m_pBarLeftTop != NULL)
		{
			hdwp = m_pBarLeftTop->MoveWindow(rectFinalLeft, bRedraw, hdwp);
			m_pBarLeftTop->SetLastPercentInPaneContainer((int) dLeftPercent);
		}

		if (m_pLeftContainer != NULL)
		{
			m_pLeftContainer->Resize(rectFinalLeft, hdwp, bRedraw);
			m_pLeftContainer->SetRecentPercent((int) dLeftPercent);
		}

		if (m_pBarRightBottom != NULL)
		{
			hdwp = m_pBarRightBottom->MoveWindow(rectFinalRight, bRedraw, hdwp);
			m_pBarRightBottom->SetLastPercentInPaneContainer(100 - (int) dLeftPercent);
		}

		if (m_pRightContainer != NULL)
		{
			m_pRightContainer->Resize(rectFinalRight, hdwp, bRedraw);
			m_pRightContainer->SetRecentPercent(100 - (int) dLeftPercent);
		}

		if (m_pSlider->IsPaneVisible())
		{
			hdwp = m_pSlider->MoveWindow(rectFinalSlider, bRedraw, hdwp);
		}

	}
}

int CPaneContainer::StretchPaneContainer(int nOffset, BOOL bStretchHorz, BOOL bLeftBar, BOOL bMoveSlider, HDWP& hdwp)
{
	ASSERT_VALID(this);

	if ((AfxGetMainWnd()->GetExStyle() & WS_EX_LAYOUTRTL) && bStretchHorz)
	{
		nOffset = -nOffset;
	}

	int nDirection = nOffset < 0 ? -1 : 1;

	CSize sizeStretch(0, 0);
	bStretchHorz ? sizeStretch.cx = nOffset : sizeStretch.cy = nOffset;

	int nAvailSpace = bStretchHorz ? CalcAvailableSpace(sizeStretch, bLeftBar).cx : CalcAvailableSpace(sizeStretch, bLeftBar).cy;
	// set the sign here
	int nActualSize = nDirection * min(abs(nOffset), abs(nAvailSpace));

	if (abs(nActualSize) == 0)
	{
		return 0;
	}

	// check whether the container's native slider has the same
	// orientation as stretch direction
	if (m_pSlider == NULL || (m_pSlider->IsHorizontal() && bStretchHorz || !m_pSlider->IsHorizontal() && !bStretchHorz))
	{
		// just use minimum of the avail. and req to stretch both bars and the
		// slider
		ResizePane(nActualSize, m_pBarLeftTop, m_pLeftContainer, bStretchHorz, bLeftBar, hdwp);
		ResizePane(nActualSize, m_pBarRightBottom, m_pRightContainer, bStretchHorz, bLeftBar, hdwp);
		// resize the slider
		if (bMoveSlider && m_pSlider != NULL)
		{
			CRect rectSlider;
			m_pSlider->GetWindowRect(rectSlider);
			if (m_pSlider->IsHorizontal())
			{
				bLeftBar ? rectSlider.right += nActualSize : rectSlider.left += nActualSize;
			}
			else
			{
				bLeftBar ? rectSlider.bottom += nActualSize : rectSlider.top += nActualSize;
			}
			if (m_pSlider->IsPaneVisible())
			{
				m_pSlider->GetParent()->ScreenToClient(rectSlider);
				m_pSlider->MoveWindow(rectSlider, FALSE, hdwp);
			}
		}
	}
	else
	{
		// treat bar's available space individually
		int nLeftAvailOffset  = CalcAvailablePaneSpace(nOffset, m_pBarLeftTop, m_pLeftContainer, bLeftBar);
		int nRigthAvailOffset = CalcAvailablePaneSpace(nOffset, m_pBarRightBottom, m_pRightContainer, bLeftBar);

		int nSliderOffset = 0;
		int nBarOffset = nActualSize;
		if (abs(nLeftAvailOffset) == abs(nRigthAvailOffset))
		{
			nSliderOffset = (abs(nLeftAvailOffset) / 2 + 1) * nDirection;
		}
		else
		{
			nSliderOffset = nActualSize;
		}

		CPoint pt(0, 0);
		bStretchHorz ? pt.x = nSliderOffset : pt.y = nSliderOffset;

		if (bMoveSlider)
		{
			m_pSlider->Move(pt);
		}

		if (bLeftBar)
		{
			ResizePane(nBarOffset, m_pBarRightBottom, m_pRightContainer, bStretchHorz, bLeftBar, hdwp);
		}
		else
		{
			ResizePane(nBarOffset, m_pBarLeftTop, m_pLeftContainer, bStretchHorz, bLeftBar, hdwp);
		}
	}

	return nActualSize;
}

int CPaneContainer::OnMoveInternalPaneDivider(int nOffset, HDWP& hdwp)
{

	ASSERT_VALID(this);
	ASSERT_VALID(m_pSlider);

	CRect rectLeft; rectLeft.SetRectEmpty();
	CRect rectRight; rectRight.SetRectEmpty();

	CSize sizeMinLeft;
	CSize sizeMinRight;

	if (m_pBarLeftTop != NULL)
	{
		m_pBarLeftTop->GetWindowRect(rectLeft);
		m_pBarLeftTop->GetMinSize(sizeMinLeft);
	}

	if (m_pLeftContainer != NULL)
	{
		m_pLeftContainer->GetWindowRect(rectLeft);
		m_pLeftContainer->GetMinSize(sizeMinLeft);
	}

	if (m_pBarRightBottom != NULL)
	{
		m_pBarRightBottom->GetWindowRect(rectRight);
		m_pBarRightBottom->GetMinSize(sizeMinRight);
	}

	if (m_pRightContainer != NULL)
	{
		m_pRightContainer->GetWindowRect(rectRight);
		m_pRightContainer->GetMinSize(sizeMinRight);
	}

	m_pSlider->GetParent()->ScreenToClient(rectLeft);
	m_pSlider->GetParent()->ScreenToClient(rectRight);

	if (!rectLeft.IsRectEmpty())
	{
		if (IsPaneDividerHorz())
		{
			rectLeft.bottom += nOffset;
			if (rectLeft.Height() < sizeMinLeft.cy)
			{
				rectLeft.bottom = rectLeft.top + sizeMinLeft.cy;
			}
		}
		else
		{
			rectLeft.right += nOffset;
			if (rectLeft.Width() < sizeMinLeft.cx)
			{
				rectLeft.right = rectLeft.left + sizeMinLeft.cx;
			}
		}
	}

	if (!rectRight.IsRectEmpty())
	{
		if (IsPaneDividerHorz())
		{
			rectRight.top += nOffset;
			if (rectRight.Height() < sizeMinRight.cy)
			{
				rectRight.top = rectRight.bottom - sizeMinRight.cy;
			}
		}
		else
		{
			rectRight.left += nOffset;
			if (rectRight.Width() < sizeMinRight.cx)
			{
				rectRight.left = rectRight.right - sizeMinRight.cx;
			}
		}
	}

	if (m_pBarLeftTop != NULL)
	{
		hdwp = m_pBarLeftTop->MoveWindow(rectLeft, TRUE, hdwp);
	}

	if (m_pLeftContainer != NULL)
	{
		m_pLeftContainer->Resize(rectLeft, hdwp);
	}

	if (m_pBarRightBottom != NULL)
	{
		hdwp = m_pBarRightBottom->MoveWindow(rectRight, TRUE, hdwp);
	}

	if (m_pRightContainer != NULL)
	{
		m_pRightContainer->Resize(rectRight, hdwp);
	}

	return nOffset;
}

void CPaneContainer::ResizePane(int nOffset, CPane* pBar, CPaneContainer* pContainer, BOOL bHorz, BOOL bLeftBar, HDWP& hdwp)
{
	ASSERT_VALID(this);
	if (pBar != NULL)
	{
		CRect rectBar;
		pBar->GetWindowRect(rectBar);
		if (bHorz)
		{
			bLeftBar ? rectBar.bottom += nOffset : rectBar.top -= nOffset;
		}
		else
		{
			bLeftBar ? rectBar.right += nOffset : rectBar.left += nOffset;
		}
		pBar->MovePane(rectBar, FALSE, hdwp);
	}
	else if (pContainer != NULL)
	{
		// the container will be stretched by "foregn" slider, threfore
		// if the native bar's slider is horizontal, a container
		// will be stretched vertically
		pContainer->StretchPaneContainer(nOffset, bHorz, bLeftBar, TRUE, hdwp);
	}
}

void CPaneContainer::Move(CPoint ptNewLeftTop)
{
	ASSERT_VALID(this);

	CRect rectLeft; rectLeft.SetRectEmpty();
	CRect rectRight; rectRight.SetRectEmpty();

	int nLeftOffset = 0;
	int nTopOffset = 0;

	if (m_pBarLeftTop != NULL)
	{
		m_pBarLeftTop->GetWindowRect(rectLeft);
		m_pBarLeftTop->SetWindowPos(NULL, ptNewLeftTop.x, ptNewLeftTop.y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
	}

	if (m_pLeftContainer != NULL)
	{
		m_pLeftContainer->GetWindowRect(rectLeft);
		m_pLeftContainer->Move(ptNewLeftTop);
	}

	nLeftOffset = rectLeft.Width();
	nTopOffset = rectLeft.Height();

	if (m_pSlider != NULL)
	{
		if (m_pSlider->IsHorizontal())
		{
			m_pSlider->SetWindowPos(NULL, ptNewLeftTop.x, ptNewLeftTop.y + nTopOffset, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
			nTopOffset += m_pSlider->GetWidth();
			nLeftOffset = 0;
		}
		else
		{
			m_pSlider->SetWindowPos(NULL, ptNewLeftTop.x + nLeftOffset, ptNewLeftTop.y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
			nLeftOffset += m_pSlider->GetWidth();
			nTopOffset = 0;
		}
	}

	if (m_pBarRightBottom != NULL)
	{
		m_pBarRightBottom->SetWindowPos(NULL, ptNewLeftTop.x + nLeftOffset, ptNewLeftTop.y + nTopOffset,
			0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
	}

	if (m_pRightContainer != NULL)
	{
		CPoint pt(ptNewLeftTop.x + nLeftOffset, ptNewLeftTop.y + nTopOffset);
		m_pRightContainer->Move(pt);
	}
}

void CPaneContainer::MoveWnd(CWnd* pWnd, int nOffset, BOOL bHorz)
{
	ASSERT_VALID(this);

	if (pWnd != NULL)
	{
		CWnd* pParent = pWnd->GetParent();
		ASSERT_VALID(pParent);

		CRect rectWnd;
		CRect rectParent;

		pParent->GetClientRect(rectParent);
		pWnd->GetWindowRect(rectWnd);
		pParent->ScreenToClient(rectWnd);

		int nActualOffset = bHorz ? rectWnd.left - rectParent.left : rectWnd.top - rectParent.top;

		bHorz ? rectWnd.OffsetRect(CPoint(nOffset - nActualOffset, 0)) : rectWnd.OffsetRect(CPoint(0, nOffset - nActualOffset));

		pWnd->MoveWindow(rectWnd, TRUE);
	}
}

int CPaneContainer::CalcAvailablePaneSpace(int nRequiredOffset, CPane* pBar, CPaneContainer* pContainer, BOOL bLeftBar)
{
	ASSERT_VALID(this);

	CRect rectBar;
	int nAvailableSpace = nRequiredOffset;

	if (pBar != NULL)
	{
		ASSERT_VALID(pBar);
		pBar->GetWindowRect(rectBar);
		if (IsPaneDividerHorz())
		{
			bLeftBar ? rectBar.bottom += nRequiredOffset : rectBar.top += nRequiredOffset;
			nAvailableSpace = pBar->CalcAvailableSize(rectBar).cy;
		}
		else
		{
			bLeftBar ? rectBar.right += nRequiredOffset : rectBar.left += nRequiredOffset;
			nAvailableSpace = pBar->CalcAvailableSize(rectBar).cx;
		}
	}
	else if (pContainer != NULL)
	{
		ASSERT_VALID(pContainer);
		nAvailableSpace = IsPaneDividerHorz() ? pContainer->CalcAvailableSpace(CSize(0, nRequiredOffset), bLeftBar).cy :
		pContainer->CalcAvailableSpace(CSize(nRequiredOffset, 0), bLeftBar).cx;
	}

	return nAvailableSpace;
}

CSize CPaneContainer::CalcAvailableSpace(CSize sizeStretch, BOOL bLeftBar)
{
	ASSERT_VALID(this);

	CRect rectWndOrg;
	GetWindowRect(rectWndOrg);
	CRect rectWndNew = rectWndOrg;

	if (bLeftBar)
	{
		rectWndNew.right += sizeStretch.cx;
		rectWndNew.bottom += sizeStretch.cy;
	}
	else
	{
		rectWndNew.left += sizeStretch.cx;
		rectWndNew.top += sizeStretch.cy;
	}

	CSize sizeMin;
	GetMinSize(sizeMin);

	CSize sizeAvailable(sizeStretch.cx, sizeStretch.cy);

	if (rectWndNew.Width() < sizeMin.cx)
	{
		sizeAvailable.cx = rectWndOrg.Width() - sizeMin.cx;
		// if already less or eq. to minimum
		if (sizeAvailable.cx < 0)
		{
			sizeAvailable.cx = 0;
		}

		// preserve direction
		if (sizeStretch.cx < 0)
		{
			sizeAvailable.cx = -sizeAvailable.cx;
		}
	}

	if (rectWndNew.Height() < sizeMin.cy)
	{
		sizeAvailable.cy = rectWndNew.Height() - sizeMin.cy;
		if (sizeAvailable.cy < 0)
		{
			sizeAvailable.cy = 0;
		}

		// preserve direction
		if (sizeStretch.cy < 0)
		{
			sizeAvailable.cy = -sizeAvailable.cy;
		}
	}

	return sizeAvailable;
}

BOOL CPaneContainer::IsLeftPaneContainer() const
{
	ASSERT_VALID(this);

	if (m_pParentContainer == NULL)
	{
		return TRUE;
	}

	if (m_pParentContainer->GetLeftPaneContainer() == this)
	{
		return TRUE;
	}

	if (m_pParentContainer->GetRightPaneContainer() == this)
	{
		return FALSE;
	}

	return FALSE;
}

BOOL CPaneContainer::IsLeftPane(CDockablePane* pBar) const
{
	if (pBar == m_pBarLeftTop)
	{
		return TRUE;
	}

	if (pBar == m_pBarRightBottom)
	{
		return FALSE;
	}

	return FALSE;
}

BOOL CPaneContainer::IsEmpty() const
{
	ASSERT_VALID(this);

	return(m_pBarLeftTop == NULL && m_pBarRightBottom == NULL && (m_pLeftContainer == NULL || m_pLeftContainer->IsEmpty()) &&
		(m_pRightContainer == NULL || m_pRightContainer->IsEmpty()));
}

BOOL CPaneContainer::IsLeftPartEmpty(BOOL bCheckVisibility) const
{
	ASSERT_VALID(this);
	return((m_pBarLeftTop == NULL || bCheckVisibility && m_pBarLeftTop != NULL  && !m_pBarLeftTop->IsPaneVisible()) &&
		(m_pLeftContainer == NULL || m_pLeftContainer->IsEmpty() || bCheckVisibility && m_pLeftContainer != NULL && !m_pLeftContainer->IsVisible()));
}

BOOL CPaneContainer::IsRightPartEmpty(BOOL bCheckVisibility) const
{
	ASSERT_VALID(this);
	return((m_pBarRightBottom == NULL || bCheckVisibility && m_pBarRightBottom != NULL && !m_pBarRightBottom->IsPaneVisible()) &&
		(m_pRightContainer == NULL || m_pRightContainer->IsEmpty() || bCheckVisibility && m_pRightContainer != NULL && !m_pRightContainer->IsVisible()));
}

BOOL CPaneContainer::IsVisible() const
{
	ASSERT_VALID(this);

	return(m_pBarLeftTop != NULL && m_pBarLeftTop->IsPaneVisible() || m_pBarRightBottom != NULL && m_pBarRightBottom->IsPaneVisible() ||
		m_pLeftContainer != NULL && m_pLeftContainer->IsVisible() || m_pRightContainer != NULL && m_pRightContainer->IsVisible());
}

void CPaneContainer::CheckPaneDividerVisibility()
{
	ASSERT_VALID(this);

	BOOL bLeftContainerVisible = FALSE;
	BOOL bRightContainerVisible = FALSE;
	BOOL bLeftBarVisible = m_pBarLeftTop != NULL && m_pBarLeftTop->IsPaneVisible();
	BOOL bRightBarVisible = m_pBarRightBottom != NULL && m_pBarRightBottom->IsPaneVisible();

	if (m_pLeftContainer != NULL)
	{
		m_pLeftContainer->CheckPaneDividerVisibility();
		bLeftContainerVisible = m_pLeftContainer->IsVisible();
	}

	if (m_pRightContainer != NULL)
	{
		m_pRightContainer->CheckPaneDividerVisibility();
		bRightContainerVisible = m_pRightContainer->IsVisible();
	}

	if (m_pSlider == NULL)
	{
		return;
	}

	BOOL bShow = FALSE;
	if (bLeftBarVisible && bRightBarVisible || bLeftBarVisible && bRightContainerVisible ||
		bRightBarVisible && bLeftContainerVisible || bLeftContainerVisible && bRightContainerVisible)
	{
		bShow = TRUE;
	}

	m_pSlider->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
}

void CPaneContainer::Serialize(CArchive& ar)
{
	CObject::Serialize(ar);

	if (ar.IsStoring())
	{
		if (m_pBarLeftTop != NULL)
		{
			int nBarID = m_pBarLeftTop->GetDlgCtrlID();
			if (nBarID != -1)
			{
				ar << nBarID;
			}
			else
			{
				SaveTabbedPane(ar, m_pBarLeftTop);
			}
		}
		else
		{
			ar << (int) 0;
		}

		if (m_pBarRightBottom != NULL)
		{
			int nBarID = m_pBarRightBottom->GetDlgCtrlID();
			if (nBarID != -1)
			{
				ar << nBarID;
			}
			else
			{
				SaveTabbedPane(ar, m_pBarRightBottom);
			}
		}
		else
		{
			ar << (int) 0;
		}

		if (m_pSlider != NULL)
		{
			ar << m_pSlider->GetDlgCtrlID();
			m_pSlider->Serialize(ar);
		}
		else
		{
			ar << (int) 0;
		}

		ar << (BOOL)(m_pLeftContainer != NULL);
		if (m_pLeftContainer != NULL)
		{
			m_pLeftContainer->Serialize(ar);
		}

		ar << (BOOL)(m_pRightContainer != NULL);
		if (m_pRightContainer != NULL)
		{
			m_pRightContainer->Serialize(ar);
		}
	}
	else
	{
		ar >> m_nSavedLeftBarID;

		if (m_nSavedLeftBarID == -1)
		{
			m_pBarLeftTop = LoadTabbedPane(ar, m_lstSavedSiblingBarIDsLeft);
		}

		ar >> m_nSavedRightBarID;

		if (m_nSavedRightBarID == -1)
		{
			m_pBarRightBottom = LoadTabbedPane(ar, m_lstSavedSiblingBarIDsRight);
		}

		ar >> m_nSavedSliderID;

		if (m_nSavedSliderID != NULL)
		{
			m_pSlider = DYNAMIC_DOWNCAST(CPaneDivider, CPaneDivider::m_pSliderRTC->CreateObject());
			ASSERT_VALID(m_pSlider);

			m_pSlider->Init(FALSE, m_pContainerManager->m_pDockSite);
			m_pSlider->Serialize(ar);
			m_pSlider->SetPaneContainerManager(m_pContainerManager);
			m_pContainerManager->m_lstSliders.AddTail(m_pSlider);
		}

		BOOL bLeftContainerPresent = FALSE;
		ar >> bLeftContainerPresent;

		CRuntimeClass* pContainerRTC = m_pContainerManager->GetPaneContainerRTC();

		if (bLeftContainerPresent)
		{
			if (pContainerRTC == NULL)
			{
				m_pLeftContainer = new CPaneContainer(m_pContainerManager);
			}
			else
			{
				m_pLeftContainer = (CPaneContainer*) pContainerRTC->CreateObject();
				m_pLeftContainer->SetPaneContainerManager(m_pContainerManager);
			}
			m_pLeftContainer->Serialize(ar);
			m_pLeftContainer->SetParentPaneContainer(this);
		}

		BOOL bRightContainerPresent = FALSE;
		ar >> bRightContainerPresent;

		if (bRightContainerPresent)
		{
			if (pContainerRTC == NULL)
			{
				m_pRightContainer = new CPaneContainer(m_pContainerManager);
			}
			else
			{
				m_pRightContainer = (CPaneContainer*) pContainerRTC->CreateObject();
				m_pRightContainer->SetPaneContainerManager(m_pContainerManager);
			}

			m_pRightContainer->Serialize(ar);
			m_pRightContainer->SetParentPaneContainer(this);
		}
	}
}

BOOL CPaneContainer::SetUpByID(UINT nID, CDockablePane* pBar)
{
	ASSERT_KINDOF(CDockablePane, pBar);
	if (m_nSavedLeftBarID == nID)
	{
		m_pBarLeftTop = pBar;
		return TRUE;
	}

	if (m_nSavedRightBarID == nID)
	{
		m_pBarRightBottom = pBar;
		return TRUE;
	}

	if (m_pLeftContainer != NULL &&
		m_pLeftContainer->SetUpByID(nID, pBar))
	{
		return TRUE;
	}

	if (m_pRightContainer != NULL)
	{
		return m_pRightContainer->SetUpByID(nID, pBar);
	}

	return FALSE;
}

void CPaneContainer::SaveTabbedPane(CArchive& ar, CDockablePane* pBar)
{
	ASSERT_KINDOF(CBaseTabbedPane, pBar);
	CBaseTabbedPane* pTabbedBar = DYNAMIC_DOWNCAST(CBaseTabbedPane, pBar);
	ASSERT(ar.IsStoring());

	if (pTabbedBar->GetTabsNum() > 0)
	{
		ar << (int) -1;
		pTabbedBar->SaveSiblingBarIDs(ar);
		ar << pTabbedBar;
		ar << pTabbedBar->GetStyle();

		pTabbedBar->SerializeTabWindow(ar);
	}
}

CDockablePane* CPaneContainer::LoadTabbedPane(CArchive& ar, CList<UINT, UINT>& lstBarIDs)
{
	ASSERT(ar.IsLoading());

	CDockablePane* pBar = NULL;
	DWORD dwStyle = 0;

	CBaseTabbedPane::LoadSiblingPaneIDs(ar, lstBarIDs);
	ar >> pBar;
	ar >> dwStyle;

	if (!pBar->Create(_T(""), m_pContainerManager->m_pDockSite, pBar->m_rectSavedDockedRect, TRUE, (UINT) -1, dwStyle, pBar->GetControlBarStyle()))
	{
		TRACE0("Failed to create tab docking bar");
		ASSERT(FALSE);
		lstBarIDs.RemoveAll();
		delete pBar;
		return NULL;
	}

	ASSERT_KINDOF(CBaseTabbedPane, pBar);
	((CBaseTabbedPane*)pBar)->SerializeTabWindow(ar);
	((CBaseTabbedPane*)pBar)->SetAutoDestroy(TRUE);

	return pBar;
}

CDockablePane* CPaneContainer::FindTabbedPane(UINT nID)
{
	ASSERT_VALID(this);

	if (m_lstSavedSiblingBarIDsLeft.Find(nID) != NULL)
	{
		return m_pBarLeftTop;
	}

	if (m_lstSavedSiblingBarIDsRight.Find(nID) != NULL)
	{
		return m_pBarRightBottom;
	}

	if (m_pLeftContainer != NULL)
	{
		CDockablePane* pBar = m_pLeftContainer->FindTabbedPane(nID);
		if (pBar != NULL)
		{
			return pBar;
		}
	}

	if (m_pRightContainer != NULL)
	{
		return m_pRightContainer->FindTabbedPane(nID);
	}

	return NULL;
}

CList<UINT, UINT>* CPaneContainer::GetAssociatedSiblingPaneIDs(CDockablePane* pBar)
{
	if (pBar == m_pBarLeftTop)
	{
		return &m_lstSavedSiblingBarIDsLeft;
	}

	if (pBar == m_pBarRightBottom)
	{
		return &m_lstSavedSiblingBarIDsRight;
	}
	return NULL;
}

void CPaneContainer::SetPaneContainerManager(CPaneContainerManager* p, BOOL bDeep)
{
	m_pContainerManager = p;

	if (bDeep)
	{
		if (m_pLeftContainer != NULL)
		{
			m_pLeftContainer->SetPaneContainerManager(p, bDeep);
		}

		if (m_pRightContainer != NULL)
		{
			m_pRightContainer->SetPaneContainerManager(p, bDeep);
		}
	}
}

int  CPaneContainer::GetNodeCount() const
{
	int nCount = 1;

	if (m_pLeftContainer != NULL)
	{
		nCount += m_pLeftContainer->GetNodeCount();
	}

	if (m_pRightContainer != NULL)
	{
		nCount += m_pRightContainer->GetNodeCount();
	}

	return nCount;
}

CPaneContainer* CPaneContainer::Copy(CPaneContainer* pParentContainer)
{
	// we should copy container and pointers to contained bars
	// only if these bars are visible;
	// unvisible parts of the new container shold be cleared
	CRuntimeClass* pContainerRTC = m_pContainerManager->GetPaneContainerRTC();
	CPaneContainer* pNewContainer = NULL;

	if (pContainerRTC == NULL)
	{
		pNewContainer = new CPaneContainer(m_pContainerManager, m_pBarLeftTop, m_pBarRightBottom, m_pSlider);
	}
	else
	{
		pNewContainer = (CPaneContainer*) pContainerRTC->CreateObject();
		pNewContainer->SetPaneContainerManager(m_pContainerManager);
		pNewContainer->SetPane(m_pBarLeftTop, TRUE);
		pNewContainer->SetPane(m_pBarRightBottom, FALSE);
		pNewContainer->SetPaneDivider(m_pSlider);
	}

	if (m_pBarLeftTop != NULL)
	{
		if (m_pBarLeftTop->GetStyle() & WS_VISIBLE)
		{
			m_pBarLeftTop = NULL;
		}
		else
		{
			pNewContainer->SetPane(NULL, TRUE);
		}
	}
	if (m_pBarRightBottom != NULL)
	{
		if (m_pBarRightBottom->GetStyle() & WS_VISIBLE)
		{
			m_pBarRightBottom = NULL;
		}
		else
		{
			pNewContainer->SetPane(NULL, FALSE);
		}
	}

	pNewContainer->SetParentPaneContainer(pParentContainer);

	if (m_pLeftContainer != NULL)
	{
		CPaneContainer* pNewLeftContainer = m_pLeftContainer->Copy(pNewContainer);
		pNewContainer->SetPaneContainer(pNewLeftContainer, TRUE);
	}

	if (m_pRightContainer != NULL)
	{

		CPaneContainer* pNewRightContainer = m_pRightContainer->Copy(pNewContainer);
		pNewContainer->SetPaneContainer(pNewRightContainer, FALSE);
	}

	if (m_pSlider != NULL)
	{
		if (m_pSlider->GetStyle() & WS_VISIBLE)
		{
			m_dwRecentSliderStyle = m_pSlider->GetPaneDividerStyle();
			m_pSlider->GetClientRect(m_rectRecentSlider);
			m_bIsRecentSliderHorz = m_pSlider->IsHorizontal();
			m_pSlider = NULL;
		}
		else
		{
			pNewContainer->SetPaneDivider(NULL);
		}
	}

	return pNewContainer;
}

BOOL CPaneContainer::IsPaneDividerHorz() const
{
	return m_pSlider != NULL ? m_pSlider->IsHorizontal() : m_bIsRecentSliderHorz;
}

int  CPaneContainer::GetTotalReferenceCount() const
{
	int nRefCount = m_dwRefCount;

	if (m_pRightContainer != NULL)
	{
		nRefCount += m_pRightContainer->GetTotalReferenceCount();
	}

	if (m_pLeftContainer != NULL)
	{
		nRefCount += m_pLeftContainer->GetTotalReferenceCount();
	}

	return nRefCount;
}

void CPaneContainer::RemoveNonValidPanes()
{
	if (m_pContainerManager == NULL)
	{
		return;
	}

	if (m_pBarLeftTop != NULL)
	{
		if (!m_pContainerManager->CheckAndRemoveNonValidPane(m_pBarLeftTop))
		{
			m_pBarLeftTop = NULL;
		}
	}

	if (m_pLeftContainer != NULL)
	{
		m_pLeftContainer->RemoveNonValidPanes();
	}

	if (m_pBarRightBottom != NULL)
	{
		if (!m_pContainerManager->CheckAndRemoveNonValidPane(m_pBarRightBottom))
		{
			m_pBarRightBottom = NULL;
		}
	}

	if (m_pRightContainer != NULL)
	{
		m_pRightContainer->RemoveNonValidPanes();
	}
}

void CPaneContainer::OnShowPane(CDockablePane* pBar, BOOL bShow)
{
	if (bShow)
	{
		return;
	}

	CWnd* pDockSite = m_pContainerManager->GetDockSiteFrameWnd();
	ASSERT_VALID(pDockSite);

	CRect rectContainer;
	GetWindowRect(rectContainer, TRUE);
	pDockSite->ScreenToClient(rectContainer);

	if (m_pBarLeftTop != NULL && m_pBarLeftTop != pBar)
	{
		m_pBarLeftTop->SetWindowPos(NULL, rectContainer.left, rectContainer.top,
			rectContainer.Width(), rectContainer.Height(), SWP_NOZORDER | SWP_NOACTIVATE);
	}
	else if (m_pBarRightBottom != NULL && m_pBarRightBottom != pBar)
	{
		m_pBarRightBottom->SetWindowPos(NULL, rectContainer.left, rectContainer.top,
			rectContainer.Width(), rectContainer.Height(), SWP_NOZORDER | SWP_NOACTIVATE);
	}
	else if (m_pLeftContainer != NULL)
	{
		HDWP hdwp = NULL;
		m_pLeftContainer->Resize(rectContainer, hdwp, TRUE);
	}
	else if (m_pRightContainer != NULL)
	{
		HDWP hdwp = NULL;
		m_pRightContainer->Resize(rectContainer, hdwp, TRUE);
	}
}



