// Copyright (c) Iuri Apollonio 1998
// Use & modify as you want & need, and leave those 3 lines.
// http://www.codeguru.com


// GfxOutBarCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "OutBarCtrl.h"

#define _ID_GFX_SMALLICON	"Small Icon View"
#define _ID_GFX_LARGEICON	"Large Icon View"
#define _ID_GFX_REMOVEITEM	"Remove item"
#define _ID_GFX_RENAMEITEM	"Rename item"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// COutBarCtrl

IMPLEMENT_DYNCREATE(COutBarCtrl, CWnd)

COutBarCtrl::COutBarCtrl()
{
	RegisterWindowClass();
	crBackGroundColor   = GetSysColor(COLOR_WINDOW);//GetSysColor(COLOR_BACKGROUND);
	crSelectedBkColor   = GetSysColor(COLOR_ACTIVECAPTION);
	crSelectedBkColor   = RGB(174,217,121);//RGB(136,158,197);//GetDepthColor(crSelectedBkColor,50);

	crSelectedTextColor = GetSysColor(COLOR_BTNTEXT);//GetSysColor(COLOR_CAPTIONTEXT);
	crTextColor = GetSysColor(COLOR_WINDOWTEXT);
	cr3dFace = GetSysColor(COLOR_3DFACE);
	crLightBorder = GetSysColor(COLOR_3DLIGHT);
	crHilightBorder = GetSysColor(COLOR_3DHILIGHT);
	crShadowBorder = GetSysColor(COLOR_3DSHADOW);
	crDkShadowBorder = GetSysColor(COLOR_3DDKSHADOW);

	pBlackPen = new CPen(PS_SOLID, 1, RGB(0,0,0));

	iFolderHeight = 22;

	dwFlags = fAnimation|fSmallIcon;

	iSelFolder = 0;

	iLastFolderHighlighted = -1;
	iLastSelectedFolder = -1;
	
	pLargeImageList = NULL;
	pSmallImageList = NULL;

	iFirstItem = 0;

	xSmallIconLabelOffset = 2;
	yLargeIconLabelOffset = 3;

	ySmallIconSpacing = 0;
	yLargeIconSpacing = 0;

	xLeftMargin = 0;
	yTopMargin = 0;

	bUpArrow = bDownArrow = false;

	rcUpArrow = rcDownArrow = CRect(0,0,0,0);

	bUpPressed = bDownPressed = false;

	bLooping = false;

	bPressedHighlight = false;

	lAnimationTickCount = 10;
	m_nCurItemIndex  = -1;
	m_nPrevItemIndex = -1;

	LOGFONT lf;
	CFont * ft = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	ft->GetLogFont(&lf);
	ft->DeleteObject();
	m_Font.CreateFontIndirect(&lf);
	m_bRoundFolder = FALSE;
	m_bRoundItem   = TRUE;

	m_xBorderGap = 2;
	m_yBorderGap = 1;
	m_bIsFocused = FALSE;
}

COutBarCtrl::~COutBarCtrl()
{
	for (int t = 0; t < arFolder.GetSize(); t++)
	{
		if (arFolder.GetAt(t)) delete (CBarFolder*) arFolder.GetAt(t);
	}
	arFolder.RemoveAll();
	delete pBlackPen;
}

BEGIN_MESSAGE_MAP(COutBarCtrl, CWnd)
	//{{AFX_MSG_MAP(COutBarCtrl)
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_SIZE()
	ON_MESSAGE(WM_GETFONT, OnGetFont)
	ON_MESSAGE(WM_SETFONT, OnSetFont)
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COutBarCtrl message handlers
void COutBarCtrl::SetFolderText(const int index, const char * text)
{
	ASSERT(index >= 0 && index < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(index);
	if (pbf->cName) 
	{
		delete [] pbf->cName;
		pbf->cName = NULL;
	}
	pbf->cName = new char [lstrlen(text)+1];
	lstrcpy(pbf->cName, text);
}

void COutBarCtrl::SetItemText(const int index, const char * text)
{
	ASSERT(iSelFolder >= 0 && iSelFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iSelFolder);
	CBarItem * pi = (CBarItem *) pbf->arItems.GetAt(index);
	if (pi->cItem) 
	{
		delete [] pi->cItem;
		pi->cItem = NULL;
	}
	pi->cItem = new char [lstrlen(text)+1];
	lstrcpy(pi->cItem, text);
}

BOOL COutBarCtrl::Create(DWORD dwStyle, const RECT & rect, CWnd * pParentWnd, UINT nID, const DWORD dwFlag)
{
	dwFlags = dwFlag;
	return CWnd::CreateEx(WS_EX_STATICEDGE,NSOUTBAR_CLASSNAME, NULL, dwStyle|WS_CHILD, rect, pParentWnd, nID);
	//GFXOUTBARCTRL_CLASSNAME
}

bool COutBarCtrl::IsSmallIconView() const
{
	return dwFlags&fSmallIcon;
}

void COutBarCtrl::SetSmallIconView(const bool bSet)
{
	iFirstItem = 0;

	if (bSet)
	{
		if (!IsSmallIconView()) 
		{
			dwFlags |= fSmallIcon;
			CRect rc;
			GetInsideRect(rc);
			InvalidateRect(rc, false);
		}
	}
	else
	{
		if (IsSmallIconView()) 
		{
			dwFlags &= ~fSmallIcon;
			CRect rc;
			GetInsideRect(rc);
			InvalidateRect(rc, false);
		}
	}
}

DWORD COutBarCtrl::GetFlag() const
{
	return dwFlags;
}

void COutBarCtrl::ModifyFlag(const DWORD & dwRemove, const DWORD & dwAdd, const UINT redraw)
{
	if (dwRemove)	dwFlags &= ~(dwRemove);
	if (dwAdd)		dwFlags |= dwAdd;

	if (GetSafeHwnd())
	{
		if (redraw != 0) SetWindowPos(0,0,0,0,0,SWP_NOZORDER|SWP_NOSIZE|SWP_NOMOVE|redraw);
		else Invalidate();
	}
}

int COutBarCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	return 0;
}

void COutBarCtrl::OnTimer(UINT nIDEvent) 
{
/*
	CPoint pt(GetMessagePos());

	ScreenToClient(&pt);

	CRect rc;
	GetClientRect(&rc);
	if (!(rc.PtInRect(pt)))
	{
		HighlightFolder(-1);
		HighlightItem(-1);
		KillTimer(1);
	}
*/
	CWnd::OnTimer(nIDEvent);
}

void COutBarCtrl::OnPaint() 
{
	CRect clientRc;
	GetClientRect(&clientRc);
	CPaintDC dc(this);
	CDC memDC;
	memDC.CreateCompatibleDC(&dc);
	CBitmap bmp;
	bmp.CreateCompatibleBitmap(&dc, clientRc.Width(), clientRc.Height());
	CBitmap * obmp = memDC.SelectObject(&bmp);

	CDC * pDC = &memDC;

	pDC->FillSolidRect(clientRc, crBackGroundColor);
	CRect rc;
	GetInsideRect(rc);
	if (!GetFolderChild()) pDC->FillSolidRect(rc, crBackGroundColor);

	DrawBorder(pDC,clientRc);
	
	int max = arFolder.GetSize();
	CRect frc;
	for (int t = 0; t < max; t++)
	{
		GetFolderRect(t, frc);
		DrawFolder(pDC, t, frc, false);
	}
	if(max && !GetFolderChild())
	{
		int f=0,l=0;
		if(iSelFolder == 3)
		{
			GetVisibleRange(iSelFolder, f,l);	
		}
		GetVisibleRange(iSelFolder, f,l);
		rcUpArrow.SetRect(0,0,GetSystemMetrics(SM_CXVSCROLL), GetSystemMetrics(SM_CXVSCROLL));
		rcDownArrow = rcUpArrow;
		rcUpArrow.OffsetRect(rc.right - 5 - GetSystemMetrics(SM_CXVSCROLL), rc.top +5);
		rcDownArrow.OffsetRect(rc.right - 5 - GetSystemMetrics(SM_CXVSCROLL), rc.bottom - 5 - GetSystemMetrics(SM_CXVSCROLL));

		if (f > 0)
		{
			if (bUpPressed) pDC->DrawFrameControl(rcUpArrow, DFC_SCROLL, DFCS_SCROLLUP|DFCS_PUSHED|DFCS_FLAT|DFCS_MONO);
			else pDC->DrawFrameControl(rcUpArrow, DFC_SCROLL, DFCS_SCROLLUP|DFCS_FLAT|DFCS_MONO);
			bUpArrow = true;
			pDC->ExcludeClipRect(rcUpArrow);
		}
		else bUpArrow = false;

		if (l < GetItemCount() - 1)
		{
			if (bDownPressed) pDC->DrawFrameControl(rcDownArrow, DFC_SCROLL, DFCS_SCROLLDOWN|DFCS_PUSHED|DFCS_FLAT|DFCS_MONO);
			else pDC->DrawFrameControl(rcDownArrow, DFC_SCROLL, DFCS_SCROLLDOWN|DFCS_FLAT|DFCS_MONO);
			bDownArrow = true;
			pDC->ExcludeClipRect(rcDownArrow);
		}
		else bDownArrow = false;

		PaintItems(pDC, iSelFolder, rc);
	}

	dc.BitBlt(clientRc.left, clientRc.top, clientRc.Width(), clientRc.Height(), &memDC, 0,0, SRCCOPY);

	memDC.SelectObject(obmp);
	memDC.DeleteDC();
	bmp.DeleteObject();
}

BOOL COutBarCtrl::OnEraseBkgnd(CDC* pDC) 
{
	return true;
}

bool COutBarCtrl::GetFolderRect(const int iIndex, CRect & rect) const
{
	int tops = 0;
	int max = arFolder.GetSize();
	ASSERT(iIndex >= 0 && iIndex < max);

	if(m_bRoundFolder)
		tops = 1;
	if (iIndex >= 0 && iIndex < max)
	{
		CRect rc;
		GetClientRect(rc);
		rc.left  += m_xBorderGap;
		rc.right -= m_xBorderGap;
        rc.top   += m_yBorderGap;
		rc.bottom-= m_yBorderGap;

		if (iIndex > iSelFolder)
			rect.SetRect(rc.left, rc.bottom - ((max - iIndex)) * iFolderHeight,rc.right,rc.bottom - (max - iIndex - 1) * iFolderHeight -tops);
		else 
			rect.SetRect(rc.left, rc.top + iIndex * iFolderHeight +tops, rc.right,rc.top + (1 + iIndex) * iFolderHeight);
		return true;
	}
	return false;
}

void COutBarCtrl::GetItemRect(const int iFolder, const int iIndex, CRect & rect)
{
	CRect rc;
	GetInsideRect(rc);
	int top = rc.top+3;
	CSize sz(0,0);
	int y = 0;
	for (int t = 0; t < iIndex; t++)
	{
		sz = GetItemSize(iFolder, t, ircAll);
		top += sz.cy;

		if (IsSmallIconView()) 
		{
			top += ySmallIconSpacing; 
		}
		else 
		{
			top += yLargeIconSpacing;
		}
		if (t == iFirstItem - 1) y = top - rc.top;

	}
	sz = GetItemSize(iFolder, iIndex, ircAll);
	rect.SetRect(rc.left, top, rc.left + sz.cx, top + sz.cy);

	rect.top -= y;
	rect.bottom -= y;

	rect.left += xLeftMargin;
	rect.top  += yTopMargin;
	rect.bottom += yTopMargin;
	rect.right = rc.right - xLeftMargin;
	if (!IsSmallIconView()) 
	{
		rect.left = rc.left;
		rect.right = rc.right;
	}
}

void COutBarCtrl::DrawFolder(CDC * pDC, const int iIdx, CRect rect, const bool bSelected)
{
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iIdx);

	int obk = pDC->SetBkMode(TRANSPARENT);
	CFont * of = pDC->SelectObject(&m_Font);
	if (!bSelected)
	{
		if(!m_bRoundFolder)
		{
		CPen pn1(PS_SOLID, 1, crBackGroundColor);
		CPen * op = pDC->SelectObject(&pn1);
		pDC->MoveTo(rect.left, rect.top);
		pDC->LineTo(rect.right, rect.top);
		pDC->SelectObject(op);

		//rect.InflateRect(-1,-1);
		//pDC->FillSolidRect(rect, cr3dFace);
		//rect.InflateRect(+1,+1);
		//pDC->RoundRect(rect, CPoint(5,5));

		pDC->Draw3dRect(rect,crHilightBorder,crDkShadowBorder);
		rect.InflateRect(-1,-1);
		pDC->FillSolidRect(rect, cr3dFace);
		}
		else
		{
		int depth = -50;
		int bkdepth = 10;
		COLORREF color,color2;
		if(iSelFolder == iIdx)
			bkdepth = 25;
		color  = GetDepthColor(crBackGroundColor,depth);
		color2 = GetDepthColor(crBackGroundColor,bkdepth);
		if(iSelFolder == iIdx)
		{
			color  = GetDepthColor(color,-30);
			color2 = RGB(174,227,131);//RGB(174,227,151);		
		}
		CBrush brushBK(color2);
		CPen penBorder;
		CBrush* pOldBrush = pDC->SelectObject(&brushBK);
		
		penBorder.CreatePen(PS_SOLID, 1,color);
		CPen* pOldPen = pDC->SelectObject(&penBorder);

		
		pDC->RoundRect(rect, CPoint(6, 6));
		pDC->SelectObject(pOldBrush);
		pDC->SelectObject(pOldPen);
		}
		
		rect.left+=xSmallIconLabelOffset;
		pDC->DrawText(CString(pbf->cName), rect, DT_LEFT|DT_VCENTER|DT_SINGLELINE);
//		pDC->DrawText(CString(pbf->cName), rect, DT_LEFT|DT_VCENTER|DT_SINGLELINE);
		pDC->SetBkMode(obk);
		pDC->SelectObject(of);
	}
	else
	{
		if(!m_bRoundFolder)
		{
		CPen pn1(PS_SOLID, 1, crBackGroundColor);
		CPen * op = pDC->SelectObject(&pn1);
		pDC->MoveTo(rect.left+1, rect.top);
		pDC->LineTo(rect.right, rect.top);
		pDC->SelectObject(op);

		//pDC->Draw3dRect(rect,crDkShadowBorder,crDkShadowBorder);
		pDC->DrawFocusRect(rect);

		rect.InflateRect(-1,-1);

		pDC->FillSolidRect(rect, cr3dFace);
		}
		else
		{
		int depth = -70;
		COLORREF color,color2;
		color  = GetDepthColor(crBackGroundColor,depth);
		color2 = GetDepthColor(crBackGroundColor,-10);		 
		CBrush brushBK(color2);
		CPen penBorder;
		CBrush* pOldBrush = pDC->SelectObject(&brushBK);
		
		penBorder.CreatePen(PS_SOLID, 1,color);
		CPen* pOldPen = pDC->SelectObject(&penBorder);

		
		pDC->RoundRect(rect, CPoint(6, 6));
		pDC->SelectObject(pOldBrush);
		pDC->SelectObject(pOldPen);
		}
		rect.left+=2;
		pDC->DrawText(CString(pbf->cName), rect, DT_LEFT|DT_VCENTER|DT_SINGLELINE);
		pDC->SetBkMode(obk);
		pDC->SelectObject(of);
	}
}

int COutBarCtrl::AddFolder(const char * cFolderName, const DWORD exData)
{
	CBarFolder * pbf = new CBarFolder(cFolderName, exData);
	ASSERT(pbf);
	arFolder.Add((void *)pbf);

	return arFolder.GetSize() - 1;
}

COutBarCtrl::CBarFolder::CBarFolder(const char * name, DWORD exData)
{
	cName = NULL;
	dwData = exData;

	if (name)
	{
		cName = new char[lstrlen(name)+1];
		ASSERT(cName);
		lstrcpy(cName, name);
	}
	pLargeImageList = NULL;
	pSmallImageList = NULL;
	pChild = NULL;
}

COutBarCtrl::CBarFolder::~CBarFolder()
{
	if (cName) delete [] cName;
	for (int t = 0; t < arItems.GetSize(); t++)
		if (arItems.GetAt(t)) delete (CBarFolder*) arItems.GetAt(t);

	arItems.RemoveAll();
}

void COutBarCtrl::GetInsideRect(CRect & rect) const
{
	GetClientRect(rect);
	rect.left  += m_xBorderGap;
	rect.right -= m_xBorderGap;
    rect.top   += m_yBorderGap;
	rect.bottom-= m_yBorderGap;

	if (arFolder.GetSize() > 0)
	{
		int max = arFolder.GetSize();
		rect.top += iFolderHeight * (iSelFolder + 1) - 1;//+ 2;
		rect.bottom -= (max - iSelFolder - 1)*iFolderHeight;
		return;
	}
}

int COutBarCtrl::HitTestEx(const CPoint & point, int &index)
{
	if (bUpArrow && rcUpArrow.PtInRect(point)) return htUpScroll;
	if (bDownArrow && rcDownArrow.PtInRect(point)) return htDownScroll;

	int max = arFolder.GetSize(), t;

	CRect rc;
	for (t = 0; t < max; t++)
	{
		GetFolderRect(t, rc);
		if (rc.PtInRect(point)) 
		{
			index = t;
			return htFolder;
		}
	}
	GetInsideRect(rc);
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iSelFolder);
	max = pbf->GetItemCount();
	for (t = iFirstItem; t < max; t++)
	{
		CBarItem * pi = (CBarItem *) pbf->arItems.GetAt(t);
		CRect itemRc;
		if (!IsSmallIconView()) 
		{
			GetIconRect(iSelFolder, t, itemRc);
			if (itemRc.PtInRect(point))
			{
				index = t;
				return htItem;
			}
			else if (itemRc.top > rc.bottom) break;
			GetLabelRect(iSelFolder, t, itemRc);
			itemRc.top -= yLargeIconLabelOffset;

			if (itemRc.PtInRect(point))
			{
				index = t;
				return htItem;
			}
			else if (itemRc.top > rc.bottom) break;
		}
		else
		{
			GetItemRect(iSelFolder, t, itemRc);
			if (itemRc.PtInRect(point))
			{
				index = t;
				return htItem;
			}
			else if (itemRc.top > rc.bottom) break;
		}
	}
	return htNothing;
}

void COutBarCtrl::HighlightFolder(const int index)
{
	CWnd * pf = GetFocus();
	if (pf != NULL && pf != this && IsChild(pf)) return;

	if (iLastFolderHighlighted == index) return;

	if (iLastFolderHighlighted >= 0)
	{
		CRect rc;
		if (GetFolderRect(iLastFolderHighlighted, rc))
		{
			CClientDC dc(this);
			CPen pn(PS_SOLID, 1, crBackGroundColor);//crShadowBorder);
			CPen * op = dc.SelectObject(&pn);
			dc.MoveTo(rc.left, rc.bottom-1);
			dc.LineTo(rc.right-1, rc.bottom-1);
			dc.LineTo(rc.right-1, rc.top);
			CPen pn1(PS_SOLID, 1, cr3dFace);
			dc.SelectObject(&pn1);
			dc.MoveTo(rc.left+1, rc.bottom-2);
			dc.LineTo(rc.right-2, rc.bottom-2);
			dc.LineTo(rc.right-2, rc.top+1);
			dc.SelectObject(op);
		}
	}
	iLastFolderHighlighted = index;
	if (iLastFolderHighlighted >= 0)
	{
		CRect rc;
		if (GetFolderRect(iLastFolderHighlighted, rc))
		{
			CClientDC dc(this);
			CPen pn(PS_SOLID, 1, crDkShadowBorder);
			CPen * op = dc.SelectObject(&pn);
			dc.MoveTo(rc.left, rc.bottom-1);
			dc.LineTo(rc.right-1, rc.bottom-1);
			dc.LineTo(rc.right-1, rc.top);
			CPen pn1(PS_SOLID, 1, crBackGroundColor);
			dc.SelectObject(&pn1);
			dc.MoveTo(rc.left+1, rc.bottom-2);
			dc.LineTo(rc.right-2, rc.bottom-2);
			dc.LineTo(rc.right-2, rc.top+1);
			dc.SelectObject(op);
		}
	}
}

void COutBarCtrl::OnLButtonUp(UINT nFlags, CPoint point) 
{

}
void COutBarCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if (GetFocus() != this) SetFocus();

	int index, ht = HitTestEx(point, index);
	
	CRect inRc;
	GetInsideRect(inRc);

	if (ht == htFolder)
	{
		bool bHigh = true;
		CRect rc;
		GetFolderRect(index, rc);

		if (::GetCapture() == NULL)
		{
			SetCapture();
			ASSERT(this == GetCapture());
			CClientDC dc(this);
			DrawFolder(&dc, index, rc, true);
			AfxLockTempMaps();
			for (;;)
			{
				MSG msg;
				VERIFY(::GetMessage(&msg, NULL, 0, 0));

				if (CWnd::GetCapture() != this) break;

				switch (msg.message)
				{
				case WM_MOUSEMOVE:
					{
						CPoint pt(msg.lParam);
						int idx, ht1 = HitTestEx(pt, idx);
						if (ht1 == htFolder && idx == index)
						{
							if (!bHigh)
							{
								DrawFolder(&dc, index, rc, true);
								bHigh = true;
							}
						}
						else
						{
							if (bHigh)
							{
								DrawFolder(&dc, index, rc, false);
								bHigh = false;
							}
						}
					}
					break;

				case WM_LBUTTONUP:
					{
						if (bHigh)
						{
							DrawFolder(&dc, index, rc, false);
							bHigh = false;
						}
						CPoint pt(msg.lParam);
						int idx, ht1 = HitTestEx(pt, idx);
						if (ht1 == htFolder && idx != iSelFolder)
							SetSelFolder(idx,0);
					}

					goto ExitLoop2;

				case WM_KEYDOWN:	
					if (msg.wParam != VK_ESCAPE) 
						break;

				default:
					DispatchMessage(&msg);
					break;
				}
			}

		ExitLoop2:
			ReleaseCapture();
			AfxUnlockTempMaps(FALSE);
		}
		if (bHigh) InvalidateRect(rc, false);

	}
	else iLastSelectedFolder = -1;

	if (ht == htItem)
	{
		bool bHigh = true, bDragging = false;
		CRect rc;
		GetItemRect(iSelFolder, index, rc);

		HCURSOR hCur = GetCursor();

		if (::GetCapture() == NULL)
		{
			SetCapture();
			ASSERT(this == GetCapture());
			AfxLockTempMaps();
			for (;;)
			{
				MSG msg;
				VERIFY(::GetMessage(&msg, NULL, 0, 0));

				if (CWnd::GetCapture() != this) break;

				switch (msg.message)
				{

				case WM_LBUTTONUP:
					{
						CPoint pt(msg.lParam);
						int idx, ht1 = HitTestEx(pt, idx);
						if(ht1 == htItem && idx == index)
						{
							HighlightItem(index);
							GetOwner()->SendMessage(WM_OUTBAR_NOTIFY, NM_OB_ITEMCLICK, idx);
						}
						
					}

					goto ExitLoop4;

				default:
					DispatchMessage(&msg);
					break;
				}
			}

		ExitLoop4:
			ReleaseCapture();
			AfxUnlockTempMaps(FALSE);
			if (bDragging) 
			{
				Invalidate();
			}
		}
		bPressedHighlight = false;
		if (bHigh) InvalidateRect(rc, false);
	}

	if (ht == htDownScroll)
	{
		bLooping = true;
		bool bHigh = true;

		if (::GetCapture() == NULL)
		{
			SetCapture();
			ASSERT(this == GetCapture());
			CClientDC dc(this);
			dc.DrawFrameControl(rcDownArrow, DFC_SCROLL, DFCS_SCROLLDOWN|DFCS_PUSHED|DFCS_FLAT|DFCS_MONO);
			//SetTimer(2,300,NULL);
			AfxLockTempMaps();
			for (;;)
			{
				MSG msg;
				VERIFY(::GetMessage(&msg, NULL, 0, 0));

				if (CWnd::GetCapture() != this) break;

				switch (msg.message)
				{
				case WM_MOUSEMOVE:
					{
						CPoint pt(msg.lParam);
						if (rcDownArrow.PtInRect(pt))
						{
							if (bHigh == false)
							{
								dc.DrawFrameControl(rcDownArrow, DFC_SCROLL, DFCS_SCROLLDOWN|DFCS_PUSHED|DFCS_FLAT|DFCS_MONO);
								bHigh = true;
								bDownArrow = true;
								bDownPressed = true;
							}
						}
						else
						{
							if (bHigh == true)
							{
								dc.DrawFrameControl(rcDownArrow, DFC_SCROLL, DFCS_SCROLLDOWN|DFCS_FLAT|DFCS_MONO);
								bHigh = false;
								bDownArrow = false;
								bDownPressed = false;
							}
						}
					}
					break;

				case WM_LBUTTONUP:
					{
						if (bHigh)
						{
							dc.DrawFrameControl(rcDownArrow, DFC_SCROLL, DFCS_SCROLLDOWN|DFCS_FLAT|DFCS_MONO);
							bHigh = false;
						}
						bDownArrow = false;
						CPoint pt(msg.lParam);
						if (rcDownArrow.PtInRect(pt))
						{
							CRect itrc;
							GetItemRect(iSelFolder, GetItemCount() - 1, itrc);
							CRect crc;
							GetInsideRect(crc);
							if (itrc.bottom > crc.bottom)
							{
								iFirstItem ++;
								InvalidateRect(crc, true);
							}
						}
					}
					goto ExitLoop3;


				case WM_TIMER:
					{
						if (msg.wParam == 2)
						{
							if (bHigh)
							{
								CPoint pt(msg.pt);
								ScreenToClient(&pt);
								if (rcDownArrow.PtInRect(pt))
								{
									bDownPressed = true;
									CRect itrc;
									GetItemRect(iSelFolder, GetItemCount() - 1, itrc);
									CRect crc;
									GetInsideRect(crc);
									if (itrc.bottom > crc.bottom)
									{
										iFirstItem ++;
										InvalidateRect(crc, true);
									}
									else goto ExitLoop3;
								}
								else bDownPressed = false;
							}
						}
						break;
					}

				case WM_KEYDOWN:	
					if (msg.wParam != VK_ESCAPE) 
						break;

				default:
					DispatchMessage(&msg);
					break;
				}
			}

		ExitLoop3:
			KillTimer(2);
			ReleaseCapture();
			AfxUnlockTempMaps(FALSE);
			bLooping = false;
			bDownPressed = false;
			bDownArrow = false;
			CRect crc;
			GetInsideRect(crc);
			InvalidateRect(crc, true);
		}
	}

	if (ht == htUpScroll)
	{
		bLooping = true;
		bool bHigh = true;

		if (::GetCapture() == NULL)
		{
			SetCapture();
			ASSERT(this == GetCapture());
			CClientDC dc(this);
			dc.DrawFrameControl(rcUpArrow, DFC_SCROLL, DFCS_SCROLLUP|DFCS_PUSHED|DFCS_FLAT|DFCS_MONO);
			//SetTimer(2,300,NULL);
			AfxLockTempMaps();
			for (;;)
			{
				MSG msg;
				VERIFY(::GetMessage(&msg, NULL, 0, 0));

				if (CWnd::GetCapture() != this) break;

				switch (msg.message)
				{
				case WM_MOUSEMOVE:
					{
						CPoint pt(msg.lParam);
						if (rcUpArrow.PtInRect(pt))
						{
							if (bHigh == false)
							{
								dc.DrawFrameControl(rcUpArrow, DFC_SCROLL, DFCS_SCROLLUP|DFCS_PUSHED|DFCS_FLAT|DFCS_MONO);
								bHigh = true;
								bUpArrow = true;
								bUpPressed = true;
							}
						}
						else
						{
							if (bHigh == true)
							{
								dc.DrawFrameControl(rcUpArrow, DFC_SCROLL, DFCS_SCROLLUP|DFCS_FLAT|DFCS_MONO);
								bHigh = false;
								bUpArrow = false;
								bUpPressed = false;
							}
						}
					}
					break;

				case WM_LBUTTONUP:
					{
						if (bHigh)
						{
							dc.DrawFrameControl(rcUpArrow, DFC_SCROLL, DFCS_SCROLLUP|DFCS_FLAT|DFCS_MONO);
							bHigh = false;
						}
						bUpArrow = false;
						CPoint pt(msg.lParam);
						if (rcUpArrow.PtInRect(pt))
						{
							if (iFirstItem > 0 ) 
							{
								iFirstItem --;
								CRect crc;
								GetInsideRect(crc);
								InvalidateRect(crc, true);
							}
						}
					}

					goto ExitLoop;

				case WM_TIMER:
					{
						if (msg.wParam == 2)
						{
							if (bHigh)
							{
								CPoint pt(msg.pt);
								ScreenToClient(&pt);
								if (rcUpArrow.PtInRect(pt))
								{
									bUpPressed = true;
									if (iFirstItem > 0) 
									{
										iFirstItem --;
										CRect crc;
										GetInsideRect(crc);
										InvalidateRect(crc, true);
									}
									else goto ExitLoop;
								}
								else bUpPressed = false;
							}
						}
						break;
					}
				case WM_KEYDOWN:	
					if (msg.wParam != VK_ESCAPE) break;

				default:
					DispatchMessage(&msg);
					break;
				}
			}

		ExitLoop:
			KillTimer(2);
			ReleaseCapture();
			AfxUnlockTempMaps(FALSE);
			bLooping = false;
			bUpPressed = false;
			bUpArrow = false;
			CRect crc;
			GetInsideRect(crc);
			InvalidateRect(crc, true);
		}
	}
	
	CWnd::OnLButtonDown(nFlags, point);
}


int COutBarCtrl::InsertItem(const int folder, const int index, const char * text, const int image, const DWORD exData)
{
	ASSERT(folder >= 0 && folder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(folder);
	return pbf->InsertItem(index, text, image, exData);
}

int COutBarCtrl::GetItemCount() const
{
	ASSERT(iSelFolder >= 0 && iSelFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iSelFolder);
	return 	pbf->GetItemCount();
}

void COutBarCtrl::SetSelFolder(const int FolderIndex,const int ItemIndex/*=-1*/)
{
	ASSERT(FolderIndex >= 0 && FolderIndex < GetFolderCount());

	CWnd * pc = GetFolderChild();
	if(pc) pc->ShowWindow(SW_HIDE);

	if(dwFlags&fAnimation && lAnimationTickCount >= 0) 
		AnimateFolderScroll(iSelFolder,FolderIndex);

	iSelFolder = FolderIndex;
	iFirstItem = 0;

	pc = GetFolderChild();
	if (pc)
	{
		CRect rc;
		GetInsideRect(rc);
		pc->MoveWindow(rc);
		pc->ShowWindow(SW_SHOW);
	}	
	m_nPrevItemIndex = m_nCurItemIndex = ItemIndex;
	if(ItemIndex >=0 && IsValidItem(ItemIndex))
		GetOwner()->SendMessage(WM_OUTBAR_NOTIFY, NM_OB_FOLDERCLICK,ItemIndex);

	Invalidate();
}
							
int COutBarCtrl::GetFolderCount() const
{
	return arFolder.GetSize();
}

int COutBarCtrl::GetSelFolder() const
{
	return iSelFolder;
}

void COutBarCtrl::RemoveFolder(const int index)
{
	ASSERT(index >= 0 && index < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(index);
	delete pbf;
	arFolder.RemoveAt(index);
	if (iSelFolder >= index) iSelFolder = index - 1;
	if (iSelFolder < 0 && GetFolderCount() > 0) iSelFolder = 0;
	Invalidate();
}

void COutBarCtrl::RemoveAllFolder()
{
	int cnt = GetFolderCount();
	for(int i = 0; i < cnt; i ++)
		RemoveFolder(0);
}

int COutBarCtrl::CBarFolder::InsertItem(int index, const char * text, const int image, const DWORD exData)
{
	if (index < 0 || index > GetItemCount()) index = GetItemCount();

	int c = GetItemCount();

	CBarItem * pbf = new CBarItem(text, image, exData);
	ASSERT(pbf);
	if (index < GetItemCount()) 
		arItems.InsertAt(index, (void *) pbf);
	else 
		arItems.Add((void *) pbf);

	c = GetItemCount();

	return index;
}

int COutBarCtrl::GetCountPerPage() const
{
	return 0;
}

CImageList * COutBarCtrl::SetImageList(CImageList * pImageList, int nImageList)
{
	CImageList * o = NULL;
	if (nImageList == fSmallIcon)
	{
		o = pSmallImageList;
		pSmallImageList = pImageList;
	}
	else if (nImageList == fLargeIcon)
	{
		o = pLargeImageList;
		pLargeImageList = pImageList;
	}
	return o;
}

CImageList * COutBarCtrl::GetImageList(CImageList * pImageList, int nImageList)
{
	if (nImageList == fSmallIcon) return pSmallImageList;
	else if (nImageList == fLargeIcon) return pLargeImageList;
	return NULL;
}

CImageList * COutBarCtrl::SetFolderImageList(const int folder, CImageList * pImageList, int nImageList)
{
	ASSERT(folder >= 0 && folder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(folder);
	CImageList * o = NULL;
	if (nImageList == fSmallIcon)
	{
		o = pbf->pSmallImageList;
		pbf->pSmallImageList = pImageList;
	}
	else if (nImageList == fLargeIcon)
	{
		o = pbf->pLargeImageList;
		pbf->pLargeImageList = pImageList;
	}
	return o;
}


COutBarCtrl::CBarItem::CBarItem(const char * name, const int image, DWORD exData)
{
	cItem = NULL;
	iImageIndex = image;
	dwData = exData;
	if (name)
	{
		cItem = new char[lstrlen(name)+1];
		ASSERT(cItem);
		lstrcpy(cItem, name);
	}
}

COutBarCtrl::CBarItem::~CBarItem()
{
	if (cItem) delete [] cItem;
}

int COutBarCtrl::CBarFolder::GetItemCount()
{
	return arItems.GetSize();
}

void COutBarCtrl::PaintItems(CDC * pDC, const int iFolder, CRect rc)
{
	ASSERT(iFolder >= 0 && iFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iFolder);
	int max = pbf->GetItemCount(), t;
	CRect irc;
	GetInsideRect(irc);
	int isdc = pDC->SaveDC();
	CRgn itemRegion;
	itemRegion.CreateRectRgnIndirect(&irc);
	pDC->SelectClipRgn(&itemRegion);
	itemRegion.DeleteObject();
	for (t = iFirstItem; t < max; t++)
	{
		CBarItem * pi = (CBarItem *) pbf->arItems.GetAt(t);
		CRect itemRc;
		GetItemRect(iFolder, t, itemRc);
		if (itemRc.top > rc.bottom) break;
		else 
		{
			if(t == m_nCurItemIndex)
				DrawItem(pDC, iFolder, itemRc, t,true);
			else
				DrawItem(pDC, iFolder, itemRc, t,false);
			
		}
	}
	pDC->RestoreDC(isdc);
}


CSize COutBarCtrl::GetItemSize(const int iFolder, const int index, const int type)
{
	ASSERT(iFolder >= 0 && iFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iFolder);

	ASSERT(index >= 0 && index < pbf->GetItemCount());
	CBarItem * pi = (CBarItem *) pbf->arItems.GetAt(index);

	CSize szImage(0,0);
	CSize szLabel(0,0);
	CSize szAll(0,0);

	if (pi->iImageIndex >= 0)
	{
		if (type != ircLabel)
		{
			CImageList * il = GetFolderImageList(iFolder, IsSmallIconView());
			ASSERT(il);
			if (il)
			{
				IMAGEINFO ii;
				il->GetImageInfo(pi->iImageIndex, &ii);
				szImage = CRect(ii.rcImage).Size();
			}
		}
	}
	if (pi->cItem)
	{
		if (type != ircIcon)
		{
			CClientDC dc(this);
			CFont * oft = (CFont *) dc.SelectObject(CFont::FromHandle((HFONT)GetStockObject(DEFAULT_GUI_FONT)));
			if (IsSmallIconView())
			{
				szLabel = dc.GetTextExtent(pi->cItem, lstrlen(pi->cItem));
				szLabel.cy += 10;
			}
			else
			{
				CRect rc;
				GetInsideRect(rc);
				rc.bottom = rc.top;
				dc.DrawText(pi->cItem, lstrlen(pi->cItem), rc, DT_CALCRECT|DT_CENTER|DT_WORDBREAK);
				szLabel = rc.Size();
			}
			dc.SelectObject(oft);
		}
	}
	if (IsSmallIconView())
	{
		if (type == ircIcon) szAll = szImage;
		else if (type == ircLabel) szAll = szLabel;
		else if (type == ircAll) szAll = CSize(szImage.cx + szLabel.cx + xSmallIconLabelOffset, szImage.cy > szLabel.cy ? szImage.cy : szLabel.cy);
	}
	else
	{
		if (type == ircIcon) szAll = szImage;
		else if (type == ircLabel) szAll = szLabel;
		else if (type == ircAll) 
		{
			szAll = CSize(szImage.cx > szLabel.cx ? szImage.cx : szLabel.cx, szLabel.cy + szImage.cy + yLargeIconLabelOffset + yLargeIconSpacing);
		}
	}
	//szAll.cy += 1;
	return szAll;
}

CImageList * COutBarCtrl::GetFolderImageList(const int index, const bool bSmall) const
{
	ASSERT(index >= 0 && index < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(index);
	if (bSmall)
	{
		if (pbf->pSmallImageList) return pbf->pSmallImageList; else return pSmallImageList;
	}
	if (pbf->pLargeImageList) return pbf->pLargeImageList; else return pLargeImageList;
}

void COutBarCtrl::DrawItem(CDC * pDC, const int iFolder, CRect rc, const int index,bool bselected)
{
	CRect rcItem= rc;
	CImageList * ima = GetFolderImageList(iFolder, IsSmallIconView());

	ASSERT(iFolder >= 0 && iFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iFolder);

	ASSERT(index >= 0 && index < pbf->GetItemCount());
	CBarItem * pi = (CBarItem *) pbf->arItems.GetAt(index);

	//ASSERT(pi && ima);

	CFont * oft = (CFont *) pDC->SelectObject(&m_Font);
	COLORREF ocr;
	int obk = pDC->SetBkMode(TRANSPARENT);

	if(bselected)
	{
		if(!m_bRoundItem)
		{
		  pDC->FillSolidRect(rcItem, crSelectedBkColor);
		  ocr = pDC->SetTextColor(crSelectedTextColor);
		}
		else
		{
		int depth = -100;
		COLORREF colorborder,colorback;
		COLORREF crtext = GetSysColor(COLOR_WINDOWTEXT);
		
		colorborder = GetDepthColor(crSelectedBkColor,depth);
		colorback   = crSelectedBkColor;//GetDepthColor(crBackGroundColor,20);		 
		crtext  = crSelectedTextColor;
		CBrush brushBK(colorback);
		CPen penBorder;
		CBrush* pOldBrush = pDC->SelectObject(&brushBK);
		
		penBorder.CreatePen(PS_SOLID, 1,colorborder);
		CPen* pOldPen = pDC->SelectObject(&penBorder);

		
		pDC->RoundRect(rcItem, CPoint(6, 6));
		pDC->SelectObject(pOldBrush);
		pDC->SelectObject(pOldPen);
		ocr = pDC->SetTextColor(crtext);
		}
		
	}
	else
		ocr = pDC->SetTextColor(crTextColor);

	if (IsSmallIconView())
	{
		CSize szImage=(0,0);
		if (ima)
		{
			IMAGEINFO ii;
			ima->GetImageInfo(pi->iImageIndex, &ii);
			szImage = CRect(ii.rcImage).Size();
			CPoint pt;
			pt.x = rc.left + 2;
			pt.y = rc.top + (rc.Height() - szImage.cy) / 2;
			ima->Draw(pDC, pi->iImageIndex, pt, ILD_NORMAL);
		}
		rc.left += szImage.cx + xSmallIconLabelOffset + 1;
		rc.top+=2;
		pDC->DrawText(pi->cItem,lstrlen(pi->cItem),rc,DT_VCENTER|DT_WORDBREAK|DT_SINGLELINE| DT_NOPREFIX | DT_NOCLIP|DT_WORD_ELLIPSIS);
	}
	else
	{
		CSize szImage=(0,0);
		if (ima)
		{
			IMAGEINFO ii;
			ima->GetImageInfo(pi->iImageIndex, &ii);
			szImage = CRect(ii.rcImage).Size();
			CPoint pt;
			pt.x = rc.left + (rc.Width() - szImage.cx) / 2;
			pt.y = rc.top;// + (rc.Height() - szImage.cy) / 2;
			ima->Draw(pDC, pi->iImageIndex, pt, ILD_NORMAL);
		}
		rc.top += szImage.cy + yLargeIconLabelOffset;
		pDC->DrawText(pi->cItem, lstrlen(pi->cItem), rc, DT_CENTER|DT_WORDBREAK| DT_NOPREFIX );
	}
	if(bselected)
	{
		if(!m_bRoundItem)
		{
			pDC->Draw3dRect(rcItem, crDkShadowBorder, cr3dFace);
			pDC->DrawFocusRect(rcItem);
		}
		if(m_bIsFocused)
		{
			RECT rcmark;
			rcmark.bottom = rcItem.bottom -3;
			rcmark.top = rcmark.bottom - 8;
			rcmark.right = rcItem.right - 3;
			rcmark.left = rcmark.right - 8;
			DrawFocusMark(pDC,rcmark);
		}
	}

	pDC->SetTextColor(ocr);
	pDC->SelectObject(oft);
	pDC->SetBkMode(obk);

}
	
void COutBarCtrl::GetVisibleRange(const int iFolder, int & first, int & last)
{
	first = iFirstItem;
	CRect rc;
	GetInsideRect(rc);

	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iFolder);
	int max = pbf->GetItemCount(), t;
	for (t = iFirstItem; t < max; t++)
	{
		CBarItem * pi = (CBarItem *) pbf->arItems.GetAt(t);
		CRect itemRc;
		GetItemRect(iFolder, t, itemRc);
		if (itemRc.bottom > rc.bottom) 
		{
			last = t - 1;
			break;
		}
		else last = t;
	}
}

void COutBarCtrl::OnSize(UINT nType, int cx, int cy) 
{
	bUpArrow = bDownArrow = false;

	CWnd::OnSize(nType, cx, cy);

	int t, max = GetFolderCount();
	CRect rc;
	GetInsideRect(rc);

	for (t = 0; t < max; t++)
	{
		CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(t);
		CWnd * pc = GetFolderChild(t);
		if (pc) pc->SetWindowPos(0, rc.left, rc.top, rc.Width(), rc.Height(), SWP_NOZORDER);
	}
}

void COutBarCtrl::HighlightItem(const int index)
{
	CWnd * pf = GetFocus();
	if (pf != NULL && pf != this && IsChild(pf)) return;

	CClientDC dc(this);
	CRect irc;
	GetInsideRect(irc);
	int isdc = dc.SaveDC();
	CRgn itemRegion;
	itemRegion.CreateRectRgnIndirect(&irc);
	dc.SelectClipRgn(&itemRegion);
	itemRegion.DeleteObject();

	
	
	if(index >= 0 && IsValidItem(index))
	{
		m_nCurItemIndex = index;
		if(m_nPrevItemIndex >= 0 && IsValidItem(m_nPrevItemIndex)) 
		{		
			CRect rcitem;
			GetItemRect(iSelFolder,m_nPrevItemIndex,rcitem);
			DrawItem(&dc,iSelFolder,rcitem,m_nPrevItemIndex,FALSE);
		}
		CRect rcitem;
		GetItemRect(iSelFolder,m_nCurItemIndex,rcitem);
		DrawItem(&dc,iSelFolder,rcitem,m_nCurItemIndex,TRUE);
		m_nPrevItemIndex = m_nCurItemIndex;
	}
	InvalidateRect(&irc);
	dc.RestoreDC(isdc);
}

void COutBarCtrl::GetIconRect(const int iFolder, const int iIndex, CRect & rect)
{
	CRect rc;
	GetInsideRect(rc);
	int top = rc.top;
	CSize sz(0,0);
	int y = 0;
	int spacing = IsSmallIconView() ? ySmallIconSpacing : yLargeIconSpacing;

	for (int t = 0; t < iIndex; t++)
	{
		sz = GetItemSize(iFolder, t, ircAll);
		top += sz.cy;
		top += spacing;
		if (t == iFirstItem - 1) y = top - rc.top;
	}
	top += spacing;

	sz = GetItemSize(iFolder, iIndex, ircIcon);
	if (IsSmallIconView())
	{
		rect.SetRect(rc.left, top, rc.left + sz.cx, top + sz.cy);
		rect.left += xLeftMargin + 2;
		rect.right += xLeftMargin + 2;
		rect.top -= yTopMargin;
		rect.bottom -= yTopMargin;

		rect.top -= y;
		rect.bottom -= y;
	}
	else
	{
		rect.SetRect(rc.left + (rc.Width() - sz.cx)/2, top, rc.left + (rc.Width() - sz.cx)/2 + sz.cx, top + sz.cy);
		rect.top -= y + 3;
		rect.bottom -= y + 2;
	}
}

void COutBarCtrl::GetLabelRect(const int iFolder, const int iIndex, CRect & rect)
{
	CRect rc;
	GetInsideRect(rc);
	int top = rc.top;
	CSize sz(0,0);
	int y = 0;
	int spacing = IsSmallIconView() ? ySmallIconSpacing : yLargeIconSpacing;

	for (int t = 0; t < iIndex; t++)
	{
		sz = GetItemSize(iFolder, t, ircAll);
		top += sz.cy;
		top += spacing;
		if (t == iFirstItem - 1) y = top - rc.top;
	}
	top += spacing;

	sz = GetItemSize(iFolder, iIndex, ircAll);

	CSize szLabel = GetItemSize(iFolder, iIndex, ircLabel);

	if (IsSmallIconView())
	{
		rect.SetRect(rc.left, top, rc.left + sz.cx, top + sz.cy);
		rect.left += xLeftMargin + 2;
		rect.right += xLeftMargin + 2;
		rect.top  += yTopMargin - 5;
		rect.bottom += yTopMargin - 5;
	}
	else
	{
		rect.SetRect(rc.left + (rc.Width() - sz.cx)/2, top, rc.left + (rc.Width() - sz.cx)/2 + sz.cx, top + sz.cy);
		rect.top -= y + 3;
		rect.bottom -= y + 2;

		rect.bottom -=  yLargeIconSpacing;
		rect.top    = rect.bottom - szLabel.cy;
	}
}

void COutBarCtrl::RemoveItem(const int index)
{
 	ASSERT(iSelFolder >= 0 && iSelFolder < GetFolderCount());
 	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iSelFolder);
 	ASSERT(index >= 0 && index < pbf->GetItemCount());
 	if (IsValidItem(index))
 	{
 		CBarItem * i = (CBarItem *) pbf->arItems.GetAt(index);
 		delete i;
 
 		pbf->arItems.RemoveAt(index);
 		CRect rc;
 		GetInsideRect(rc);
 		InvalidateRect(rc);
 	}
}

bool COutBarCtrl::IsValidItem(const int index) const
{
	ASSERT(iSelFolder >= 0 && iSelFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iSelFolder);
	return (index >= 0 && index < pbf->GetItemCount());
}

bool COutBarCtrl::IsValidItem(const int nfolder,const int index) const
{
	ASSERT(nfolder >= 0 && nfolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(nfolder);
	return (index >= 0 && index < pbf->GetItemCount());
}


DWORD COutBarCtrl::GetItemData(const int index) const
{
	ASSERT(iSelFolder >= 0 && iSelFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iSelFolder);
	if (IsValidItem(index))
	{
		CBarItem * i = (CBarItem *) pbf->arItems.GetAt(index);
		return i->dwData;
	}
	return 0;
}

DWORD COutBarCtrl::GetItemData(const int nfolder,const int index) const
{
	ASSERT(nfolder >= 0 && nfolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(nfolder);
	if (IsValidItem(nfolder,index))
	{
		CBarItem * i = (CBarItem *) pbf->arItems.GetAt(index);
		return i->dwData;
	}
	return 0;
}

int COutBarCtrl::GetItemImage(const int index) const
{
	ASSERT(iSelFolder >= 0 && iSelFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iSelFolder);
	if (IsValidItem(index))
	{
		CBarItem * i = (CBarItem *) pbf->arItems.GetAt(index);
		return i->iImageIndex;
	}
	return 0;
}

void COutBarCtrl::SetItemData(const int index, const DWORD dwData)
{
	ASSERT(iSelFolder >= 0 && iSelFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iSelFolder);
	if (IsValidItem(index))
	{
		CBarItem * i = (CBarItem *) pbf->arItems.GetAt(index);
		i->dwData = dwData;
	}
}

void COutBarCtrl::SetItemImage(const int index, const int iImage)
{
	ASSERT(iSelFolder >= 0 && iSelFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iSelFolder);
	if (IsValidItem(index))
	{
		CBarItem * i = (CBarItem *) pbf->arItems.GetAt(index);
		i->iImageIndex = iImage;
	}
}

void COutBarCtrl::AnimateFolderScroll(const int iFrom, const int iTo)
{
	ASSERT(iFrom >= 0 && iFrom < GetFolderCount());
	ASSERT(iTo >= 0 && iTo < GetFolderCount());

	CRect rc, rc1, frc, frc1,rcb;
	GetInsideRect(rc);
	rc1.SetRect(0,0,rc.Width(),rc.Height());
	GetFolderRect(iTo, frc);
	frc1 = frc;

	CClientDC dc(this);
	CDC memDC;
	memDC.CreateCompatibleDC(&dc);
	CBitmap bmpFrom, bmpTo;
	bmpFrom.CreateCompatibleBitmap(&dc, rc1.Width(), rc1.Height() + frc.Height() * 2);
	bmpTo.CreateCompatibleBitmap(&dc, rc1.Width(), rc1.Height() + frc.Height() * 2);
	CDC * pDC = &memDC;

	CWnd * pWnd1 = GetFolderChild(iFrom);
	CWnd * pWnd2 = GetFolderChild(iTo);


	CBitmap * obmp = pDC->SelectObject(&bmpFrom);
	if (iTo > iFrom)
	{
		rc1.bottom += frc.Height() * 2;
		pDC->FillSolidRect(rc1, crBackGroundColor);
		if (pWnd1) 
		{
			BOOL bPrev = pWnd1->ShowWindow(SW_SHOW);
			pWnd1->SendMessage(WM_ERASEBKGND, (WPARAM) pDC->GetSafeHdc(), (LPARAM) 0);
			pWnd1->SendMessage(WM_PAINT, (WPARAM) pDC->GetSafeHdc(), (LPARAM) 0);
			if (!bPrev) pWnd1->ShowWindow(SW_HIDE);
		}
		else PaintItems(pDC, iFrom, rc1);

		pDC->SelectObject(&bmpTo);
		rcb.SetRect(0,0,rc1.Width(),rc1.Height() + frc.Height() * 2);
		pDC->FillSolidRect(rcb, crBackGroundColor);
		frc.SetRect(0,0,frc.Width(),frc.Height());
		rc1.SetRect(0,frc.Height(),rc.Width(), rc.Height()+frc.Height() * 2);
		pDC->FillSolidRect(rc1, crBackGroundColor);

		if (pWnd2) 
		{
			CPoint ovpt = pDC->SetViewportOrg(0, frc.Height());
			BOOL bPrev = pWnd2->ShowWindow(SW_SHOW);
			pWnd2->SendMessage(WM_ERASEBKGND, (WPARAM) pDC->GetSafeHdc(), (LPARAM) 0);
			pWnd2->SendMessage(WM_PAINT, (WPARAM) pDC->GetSafeHdc(), (LPARAM) 0);
			if (!bPrev) pWnd2->ShowWindow(SW_HIDE);
			pDC->SetViewportOrg(ovpt);
		}
		else PaintItems(pDC, iTo, rc1);
		DrawFolder(pDC, iTo, frc, false);
	}
	else
	{
		frc.SetRect(0,0,frc.Width(),frc.Height());
		rc1.top += frc.Height();
		rc1.bottom += frc.Height() * 2;

		rcb.SetRect(0,0,rc1.Width(),rc1.Height() + frc.Height() * 2);
		pDC->FillSolidRect(rcb, crBackGroundColor);

		pDC->FillSolidRect(rc1, crBackGroundColor);
		if (pWnd1) 
		{
			CPoint ovpt = pDC->SetViewportOrg(0, frc.Height());
			BOOL bPrev = pWnd1->ShowWindow(SW_SHOW);
			pWnd1->SendMessage(WM_ERASEBKGND, (WPARAM) pDC->GetSafeHdc(), (LPARAM) 0);
			pWnd1->SendMessage(WM_PAINT, (WPARAM) pDC->GetSafeHdc(), (LPARAM) 0);
			if (!bPrev) pWnd1->ShowWindow(SW_HIDE);
			pDC->SetViewportOrg(ovpt);
		}
		else PaintItems(pDC, iFrom, rc1);

		DrawFolder(pDC, iFrom, frc, false);

		pDC->SelectObject(&bmpTo);
		rcb.SetRect(0,0,rc1.Width(),rc1.Height() + frc.Height() * 2);
		pDC->FillSolidRect(rcb, crBackGroundColor);

		rc1.SetRect(0,0,rc.Width(), rc.Height() + frc.Height() * 2);
		pDC->FillSolidRect(rc1, crBackGroundColor);

		if (pWnd2) 
		{
			BOOL bPrev = pWnd2->ShowWindow(SW_SHOW);
			pWnd2->SendMessage(WM_ERASEBKGND, (WPARAM) pDC->GetSafeHdc(), (LPARAM) 0);
			pWnd2->SendMessage(WM_PAINT, (WPARAM) pDC->GetSafeHdc(), (LPARAM) 0);
			if (!bPrev) pWnd2->ShowWindow(SW_HIDE);
		}
		else PaintItems(pDC, iTo, rc1);
	}

	if (iTo > iFrom)
	{
		CRect rcFrom, rcTo;
		GetFolderRect(iFrom, rcFrom);
		GetFolderRect(iTo, rcTo);
		int fh = rcFrom.Height();
		for (int y = rcTo.top - fh; y > rcFrom.bottom; y -= fh)
		{
			pDC->SelectObject(&bmpFrom);
			dc.BitBlt(rc.left, rcFrom.bottom + 1, rc.Width(), y - rcFrom.bottom - 1, pDC, 0,fh, SRCCOPY);
			pDC->SelectObject(&bmpTo);
			dc.BitBlt(rc.left, y, rc.Width(), rc.bottom - y + fh, pDC, 0,0, SRCCOPY);
			Sleep(lAnimationTickCount);
		}
	}
	else
	{
		CRect rcFrom, rcTo;
		GetFolderRect(iFrom, rcFrom);
		int fh = rcFrom.Height();
		rcTo.SetRect(rc.left, rc.bottom, rc.right, rc.bottom - fh);
		for (int y = rcFrom.top + 1; y < rcTo.top - fh; y += fh)
		{
			pDC->SelectObject(&bmpTo);
			dc.BitBlt(rc.left, rcFrom.top, rc.Width(), y - rcFrom.top - 1, pDC, 0, fh*2, SRCCOPY);
			pDC->SelectObject(&bmpFrom);
			dc.BitBlt(rc.left, y, rc.Width(), rc.bottom - y, pDC, 0,0, SRCCOPY);
			Sleep(lAnimationTickCount);
		}
	}

	pDC->SelectObject(obmp);
}

void COutBarCtrl::GetItemText(const int index, LPSTR lpItemText)
{
	ASSERT(iSelFolder >= 0 && iSelFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iSelFolder);
	if (IsValidItem(index))
	{
		CBarItem * pi = (CBarItem *) pbf->arItems.GetAt(index);
		if(pi->cItem)
			strcpy(lpItemText,pi->cItem);
	}
}

CString COutBarCtrl::GetItemText(const int index)
{
	CString item;

	ASSERT(iSelFolder >= 0 && iSelFolder < GetFolderCount());
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iSelFolder);
	if (IsValidItem(index))
	{
		CBarItem * pi = (CBarItem *) pbf->arItems.GetAt(index);
		if (pi->cItem) item = pi->cItem;
	}
	return item;
}

int COutBarCtrl::AddFolderBar(const char * pFolder, CWnd * pSon, const DWORD exData)
{
	CBarFolder * pbf = new CBarFolder(pFolder, exData);
	ASSERT(pbf);
	pbf->pChild = pSon;

	arFolder.Add((void *)pbf);

	return arFolder.GetSize() - 1;
}

CWnd * COutBarCtrl::GetFolderChild(int iFolder)
{
	if (GetFolderCount())
	{
		if (iFolder < 0) iFolder = iSelFolder;

		CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iFolder);
		return pbf->pChild;
	}
	return NULL;
}

void COutBarCtrl::GetFolderName(int iFolder,LPSTR lpName)
{
	if(iFolder < 0) iFolder = iSelFolder;
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iFolder);
	strcpy(lpName,pbf->cName);
}

DWORD COutBarCtrl::GetFolderData(int iFolder)
{
	if (iFolder < 0) iFolder = iSelFolder;
	CBarFolder * pbf = (CBarFolder *) arFolder.GetAt(iFolder);
	return pbf->dwData;
}

BOOL COutBarCtrl::RegisterWindowClass()
{
	WNDCLASS wndcls; 
	HINSTANCE hInst = AfxGetInstanceHandle();

	if (!(::GetClassInfo(hInst, NSOUTBAR_CLASSNAME, &wndcls)))
	{
		// otherwise we need to register a new class
		wndcls.style			= CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
		wndcls.lpfnWndProc		= ::DefWindowProc;
		wndcls.cbClsExtra		= wndcls.cbWndExtra = 0;
		wndcls.hInstance		= hInst;
		wndcls.hIcon			= NULL;
		wndcls.hCursor			= ::LoadCursor(NULL, IDC_ARROW);
#if 1		
		wndcls.hbrBackground	= (HBRUSH) (COLOR_3DFACE + 1);
#else
		wndcls.hbrBackground	= NULL;
#endif
		wndcls.lpszMenuName		= NULL;
		wndcls.lpszClassName	= NSOUTBAR_CLASSNAME;

		if (!AfxRegisterClass(&wndcls)) {
			AfxThrowResourceException();
			return FALSE;
		}
	}

	return TRUE;

}

int COutBarCtrl::GetSelectedItem()
{
	return m_nCurItemIndex;
}

LRESULT COutBarCtrl::OnSetFont(WPARAM hFont, LPARAM /*lParam */)
{
	LRESULT result = Default();

	CFont *pFont = CFont::FromHandle((HFONT)hFont);
	if (pFont) {
		LOGFONT lf;
		pFont->GetLogFont(&lf);

		m_Font.DeleteObject();
		m_Font.CreateFontIndirect(&lf);
	}
	if(::IsWindow(GetSafeHwnd())) 
		Invalidate();

	return result;
}

LRESULT COutBarCtrl::OnGetFont(WPARAM wParam, LPARAM lParam)
{
	UNUSED(wParam); UNUSED(lParam);
	return (LRESULT) m_Font.m_hObject;
}

void COutBarCtrl::SetBackgroundColor(COLORREF color)
{
	crBackGroundColor = color;
	Invalidate();
}

COLORREF COutBarCtrl::GetDepthColor(COLORREF color, int depth)
{
	COLORREF newcolor = color;
	BYTE r,g,b;
	r = GetRValue(newcolor);
	g = GetGValue(newcolor);
	b = GetBValue(newcolor);
	r = (r + depth <= 255) ? ((r + depth > 0) ?r+depth:0) : 255;//r;
	g = (g + depth <= 255) ? ((g + depth > 0) ?g+depth:0) : 255;//g;
	b = (b + depth <= 255) ? ((b + depth > 0) ?b+depth:0) : 255;//b;
	newcolor = RGB(r,g,b);
	return newcolor;
}

void COutBarCtrl::SetRoundFolder(bool flag)
{
	m_bRoundFolder = flag;
	Invalidate();
}
void COutBarCtrl::SetRoundItem(bool flag)
{
	m_bRoundItem = (bool)flag;
	Invalidate();
}

void COutBarCtrl::DrawBorder(CDC *pDC, CRect rect)
{
	int depth = -100;
	COLORREF color,color2;
    color = RGB(180, 180, 180);//GetDepthColor(crBackGroundColor,depth);
	color2 = GetDepthColor(crBackGroundColor,0);
		
	CBrush brushBK(color2);
	CPen penBorder;
	CBrush* pOldBrush = pDC->SelectObject(&brushBK);
		
	penBorder.CreatePen(PS_SOLID, 1,color);
	CPen* pOldPen = pDC->SelectObject(&penBorder);

//	pDC->Draw3dRect(rect,crDkShadowBorder,crHilightBorder);
	pDC->RoundRect(rect, CPoint(6,6));
	pDC->SelectObject(pOldBrush);
	pDC->SelectObject(pOldPen);

}

void COutBarCtrl::DrawFocusMark(CDC *pDC, RECT rect)
{
	CPen lightpen(PS_SOLID, 1, ::GetSysColor(COLOR_3DHIGHLIGHT)),
	  //darkpen(PS_SOLID,  1, ::GetSysColor(COLOR_3DSHADOW)),//COLOR_3DDKSHADOW
		  darkpen(PS_SOLID,  1, ::GetSysColor(COLOR_3DDKSHADOW)),//
	 *pOldPen = pDC->GetCurrentPen();
	//LEFT-TOP LINE	
	pDC->SelectObject(&lightpen);
	pDC->MoveTo(rect.right, rect.top);
	pDC->LineTo(rect.left, rect.bottom);

	//RIGHT-BOTTOM LINE	
	pDC->SelectObject(&darkpen);
	pDC->MoveTo(rect.right, rect.top);
	pDC->LineTo(rect.right, rect.bottom);
	pDC->LineTo(rect.left, rect.bottom);
	pDC->SelectObject(pOldPen);
}

void COutBarCtrl::OnSetFocus(CWnd* pOldWnd) 
{
	CWnd::OnSetFocus(pOldWnd);
	
	m_bIsFocused = TRUE;
//	SetCursor(LoadCursor(NULL,
	Invalidate();
	
}

void COutBarCtrl::OnKillFocus(CWnd* pNewWnd) 
{
	CWnd::OnKillFocus(pNewWnd);
	
	m_bIsFocused = FALSE;
	Invalidate();
	
}

