// DlgCopyBLOB.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "DlgCopyBLOB.h"
#include "TransportSocketClient.h"
#include <apl_gui.h>

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

#define ID_TIMER_COPY_BLOB 50010

/* 05_06_02
UINT WaitThread(LPVOID lpParameter){
CDlgCopyBLOB* dlg=(CDlgCopyBLOB*)lpParameter;
CaplSetResourceHandle res_handle(module_inst);
if(dlg != 0){
dlg->DoModal();
}
delete dlg;
return 0;
}
05_06_02 */


/////////////////////////////////////////////////////////////////////////////
// CWaitDlgThread

IMPLEMENT_DYNCREATE(CWaitDlgThread, CWinThread)

CWaitDlgThread::CWaitDlgThread()
{
	m_dlg = 0;
//	OutputDebugString("CWaitDlgThread::CWaitDlgThread()\n");
}

CWaitDlgThread::~CWaitDlgThread()
{
	m_dlg = 0;
//	OutputDebugString("CWaitDlgThread::~CWaitDlgThread()\n");
}


BOOL CWaitDlgThread::InitInstance()
{
	if(m_dlg != 0){
		m_pMainWnd=m_dlg;
//	Перестановку ресурсов перенес в OnInitDialog
//		OutputDebugString("CWaitDlgThread-AfxSetResourceHandle(module_inst);\n");
//		AfxSetResourceHandle(module_inst);
		m_dlg->DoModal();
//		AfxSetResourceHandle(m_hInstOld);
//		OutputDebugString("CWaitDlgThread-AfxSetResourceHandle(m_hInstOld);\n");
		m_dlg->m_thread = 0;
		m_dlg = 0;
	}
	return FALSE;
}

int CWaitDlgThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
//	OutputDebugString("CWaitDlgThread::ExitInstance()\n");
	return CWinThread::ExitInstance();
}

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

/////////////////////////////////////////////////////////////////////////////
// CWaitDlgThread message handlers



/////////////////////////////////////////////////////////////////////////////
// CDlgCopyBLOB dialog


CDlgCopyBLOB::CDlgCopyBLOB(CWnd* pParent /*=NULL*/)
	: CaplTranslateDialog(CDlgCopyBLOB::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgCopyBLOB)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_titul = _T("");
	m_position = 0;
	m_FlagBreak = false;
	m_FlagEnd = false;
	m_thread = 0;
	m_is_foreground = true;
	m_AplTransport = 0;
	InitializeCriticalSection(&m_fbo);
}

CDlgCopyBLOB::~CDlgCopyBLOB()
{
	DeleteCriticalSection(&m_fbo);
//	OutputDebugString("CDlgCopyBLOB::~CDlgCopyBLOB()\n");
}

void CDlgCopyBLOB::DoDataExchange(CDataExchange* pDX)
{
	CaplTranslateDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgCopyBLOB)
	DDX_Control(pDX, IDC_PROGRESS, m_Progress);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgCopyBLOB, CaplTranslateDialog)
	//{{AFX_MSG_MAP(CDlgCopyBLOB)
	ON_BN_CLICKED(ID_BREAK, OnBreak)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgCopyBLOB message handlers

void CDlgCopyBLOB::OnBreak() 
{
	// TODO: Add your control notification handler code here
	EnterCriticalSection(&m_fbo);
	m_FlagBreak = true;
	if(m_AplTransport != 0)
		m_AplTransport->SetBreak(true);
	LeaveCriticalSection(&m_fbo);
}

void CDlgCopyBLOB::OnTimer(UINT nIDEvent) 
{
	EnterCriticalSection(&m_fbo);
	if(m_FlagEnd){
		KillTimer(ID_TIMER_COPY_BLOB);
		CaplTranslateDialog::OnCancel();
	}
	LeaveCriticalSection(&m_fbo);
	if(ID_TIMER_COPY_BLOB==nIDEvent){
		int position = 0;
		CString str = _T("");
		EnterCriticalSection(&m_fbo);
		position=m_position;
		LeaveCriticalSection(&m_fbo);
		m_Progress.SetPos(position);
		str.Format(_T(" - %i %%"),position);
		SetWindowText(m_titul+str);
	}
	if(m_is_foreground){
		SetForegroundWindow();
		//SetActiveWindow( );
	}
	CaplTranslateDialog::OnTimer(nIDEvent);
}

void CDlgCopyBLOB::OnCancel() 
{
	///	CaplTranslateDialog::OnCancel();
	OnBreak();
}

bool CDlgCopyBLOB::SetProgress(int pos)
{
	EnterCriticalSection(&m_fbo);
	bool ret=m_FlagBreak;
	m_position=pos;
	if(m_FlagBreak){
		CWnd *wnd=AfxGetMainWnd();
		if(wnd != 0)
		{
			wnd->SetForegroundWindow( );
			//wnd->SetActiveWindow( );
		}
	}else{
		CWnd *wnd=AfxGetMainWnd();
		if(wnd== 0){
			m_is_foreground = true;
		}else{
			m_is_foreground=wnd==wnd->GetForegroundWindow();
		}
	}
	LeaveCriticalSection(&m_fbo);
	return ret;
}

BOOL CDlgCopyBLOB::OnInitDialog() 
{
	CaplSetResourceHandle res_handle(module_inst);

	CaplTranslateDialog::OnInitDialog();

	ModifyStyleEx(0, WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CONTROLPARENT);   
	ModifyStyle(0, WS_MAXIMIZEBOX);   

	m_position = 0;
	
	m_Progress.SetRange( 0, 100);
	m_FlagBreak = false;
	SetWindowText(m_titul);
	
	SetTimer(ID_TIMER_COPY_BLOB,500,0);
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


void CDlgCopyBLOB::StartThread()
{
/* 05_06_02
 	AFX_THREADPROC pfnThreadProc;
	pfnThreadProc=WaitThread;
	AfxBeginThread(pfnThreadProc,this);
	05+06+02 */
//	OutputDebugString("CDlgCopyBLOB::StartThread()\n");

	//m_main_wnd=AfxGetMainWnd();
	m_thread=(CWaitDlgThread*)AfxBeginThread(RUNTIME_CLASS(CWaitDlgThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
	m_thread->m_dlg=this;
	m_thread->ResumeThread();
}

void CDlgCopyBLOB::StopThread()
{
//	OutputDebugString("CDlgCopyBLOB::StopThread()\n");
	EnterCriticalSection(&m_fbo);
	m_FlagEnd = true;
	LeaveCriticalSection(&m_fbo);
	while(m_thread != 0){
		Sleep(50);
	}
//	OutputDebugString("CDlgCopyBLOB::End StopThread()\n");
}
