// StatusBarEx.cpp : implementation file
//

#include "stdafx.h"
#include "StatusBarEx.h"

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

/////////////////////////////////////////////////////////////////////////////
// CStatusBarEx

CStatusBarEx::CStatusBarEx()
{
}

CStatusBarEx::~CStatusBarEx()
{
}


BEGIN_MESSAGE_MAP(CStatusBarEx, CStatusBar)
        //{{AFX_MSG_MAP(CStatusBarEx)
                // NOTE - the ClassWizard will add and remove mapping macros here.
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStatusBarEx message handlers

BOOL CStatusBarEx::Create(CWnd * pParentWnd, DWORD dwStyle, UINT nID)
{
        BOOL result = CStatusBar::Create(pParentWnd, dwStyle, nID);
        if (result)
        {
                CRect rc;
                GetItemRect(0, &rc);
                result = m_ctlProgress.Create(WS_CHILD, rc, this, nID);
        }

        return result;
}

void CStatusBarEx::ShowProgress(LPCTSTR pszMessage /*=NULL*/)
{
        CRect rc;
    GetItemRect (0, &rc);
        CPoint cent = rc.CenterPoint(); 

        // initialize the status message
        UINT offset=0;
        if (pszMessage != NULL)
        {
                CClientDC dc(this);
                CSize sz = dc.GetTextExtent(pszMessage, _tcslen(pszMessage));
                offset = sz.cx-20;

                SetPaneText(0, pszMessage, TRUE);
        }
        else
                SetPaneText(0, _T(""), TRUE);
        UpdateWindow();

        // get the progress control into position & show it
        int width = rc.Width();
        int height = rc.Height();
        rc.left = offset;
        width = max(50, min(200, width-rc.left));
        height = max(4, height-6);
        rc.right = rc.left + width;
        rc.top = cent.y - height/2;
        rc.bottom = cent.y + height/2;
    m_ctlProgress.MoveWindow(rc.left, rc.top, width, height, TRUE);
        m_ctlProgress.SetPos(0);
        m_ctlProgress.SetRange(0, 100);
        m_ctlProgress.ShowWindow(SW_SHOW);
}

void CStatusBarEx::SetProgress(UINT pos)
{
        m_ctlProgress.SetPos(pos);
        m_ctlProgress.UpdateWindow();
}

void CStatusBarEx::ShowProgress(UINT idsMessage)
{
        TRY {
                CString s;
                VERIFY(s.LoadString(idsMessage));
                ShowProgress(s);
        }
        CATCH_ALL(e)
        {
                //TCHAR msg[1024];
                //e->GetErrorMessage(msg, sizeof msg);
                //AfxMessageBox(msg, MB_ICONSTOP);
        }
        END_CATCH_ALL;
}

void CStatusBarEx::HideProgress()
{
        SetPaneText(0, _T(""), TRUE);
        m_ctlProgress.ShowWindow(SW_HIDE);
}

CProgressCtrl  * CStatusBarEx::GetProgressCtrl()
{
        return &m_ctlProgress;
}

void CStatusBarEx::SetProgressRange(int nMin, int nMax)
{
        m_ctlProgress.SetRange(nMin,nMax);
}