// WaitThread.cpp : implementation file
//

#include "stdafx.h"
#include "PostInstallUtils.h"
#include "WaitThread.h"
#include "WaitDialog.h"


// CWaitThread

IMPLEMENT_DYNCREATE(CWaitThread, CWinThread)

CWaitThread::CWaitThread(
    CString sMessage,
    CWnd*   pParent
    )
: m_pWaitDialog(NULL)
, m_sMessageText(sMessage)
, m_pParent(pParent)
{
}

CWaitThread::~CWaitThread()
{
}

BOOL CWaitThread::InitInstance()
{
    BOOL    bRet;
    CRect   oDialogClientRect, oParentPostion, oDialogNewPosition;
	m_pWaitDialog = new CWaitDialog(m_pParent);
    m_pWaitDialog->m_sMessageText = m_sMessageText;
    m_pMainWnd = m_pWaitDialog;
    bRet = m_pWaitDialog->Create(
        _T("WAITDIALOG")
        );
    if (NULL != m_pParent)
    {
        m_pParent->GetWindowRect(oParentPostion);
        m_pMainWnd->GetClientRect(oDialogClientRect);
        oDialogNewPosition.left = (oParentPostion.right + oParentPostion.left) / 2 - oDialogClientRect.right / 2;
        oDialogNewPosition.right = oDialogNewPosition.left + oDialogClientRect.right;
        oDialogNewPosition.top = (oParentPostion.bottom + oParentPostion.top) / 2 - oDialogClientRect.bottom / 2;
        oDialogNewPosition.bottom = oDialogNewPosition.top + oDialogClientRect.bottom;
        m_pMainWnd->MoveWindow(
            oDialogNewPosition,
            FALSE
            );
    }
	m_pMainWnd->ShowWindow( SW_SHOW );
	m_pMainWnd->UpdateWindow( );
    bRet = BringWindowToTop(*m_pMainWnd);
	return TRUE;
}

int CWaitThread::ExitInstance()
{
    BOOL    bRet;
    HWND    hParent;
    if (NULL != m_pParent)
    {
        hParent = m_pParent->Detach();
        delete m_pParent;
        m_pParent = NULL;
    }
	if (NULL != m_pWaitDialog)
    {
        bRet = m_pWaitDialog->DestroyWindow();
        delete m_pWaitDialog;
        m_pWaitDialog = NULL;
    }
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CWaitThread, CWinThread)
END_MESSAGE_MAP()


// CWaitThread message handlers
