// SchemeDoc.h : interface of the CSchemeDoc class
//


#pragma once

#include <set>
#include <gdiplus.h>
#include "BlockItem_Virtual.h"
#include "ViewersDoc.h"

typedef struct 
{
	CDocument *pDoc;
	CBlockItem *pItem;
}AUTOTRIGGERHANDLE, *LPAUTOTRIGGERHANDLE;

using namespace Gdiplus;

#define WM_DATAUPDATE	WM_USER + 7

class CProcessThread;

class CSchemeDoc : public CDocument
{
protected: // create from serialization only
	CSchemeDoc();
	DECLARE_DYNCREATE(CSchemeDoc)

// Attributes
public:

// Operations
public:

// Overrides
public:
	virtual BOOL OnNewDocument();
	virtual void Serialize(CArchive& ar);

// Implementation
public:
	virtual ~CSchemeDoc();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

private:
	CProcessThread *m_pProcessThread;
// Generated message map functions
protected:
	DECLARE_MESSAGE_MAP()
public:
	CWnd* m_pWndMain;
	Font m_fontText;
	Font m_fontPortText;
	CBlockItem_Virtual* m_pBlockRoot;
	BOOL m_bRunningAuto;
	virtual void OnCloseDocument();
	//BOOL TestBlockPointer(CBlockItem_Virtual* ptr);
	VOID Update(CView* pSender);
	virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);
	
	std::set<CBlockItem*> m_setRun;
	std::set<CBlockItem*> m_setOwnTrigger;
	std::set<CBlockItem*> m_setAllBlocks;

	std::set<LPVIEWERSETDATA> m_setViewers;

	DWORD m_dwLastProcessId;
	CRITICAL_SECTION m_csViewer;

	BOOL UpdateViewerData(LPVIEWERSETDATA pViewerSetData);

	BOOL UpdateViewers_Safe();
	BOOL OnIdleCustom(LONG lCount);
	LPARAM UpdateViewers(BOOL bRefresh = TRUE);
	LPARAM DestroyViewer(IViewer* pViewer);
	static BOOL WINAPI TriggerProc(HANDLE h);
	BOOL ProcessBlock(CBlockItem *pBlock);

	afx_msg void OnProcessTrigger();
	afx_msg void OnUpdateProcessTrigger(CCmdUI *pCmdUI);

	afx_msg void OnProcessStart();
	afx_msg void OnUpdateProcessStart(CCmdUI *pCmdUI);
	
	afx_msg void OnProcessStop();
	afx_msg void OnUpdateProcessStop(CCmdUI *pCmdUI);
	virtual BOOL OnSaveDocument(LPCTSTR lpszPathName);
	virtual BOOL CanCloseFrame(CFrameWnd* pFrame);
	afx_msg void OnProcessTriggerasync();
	afx_msg void OnUpdateProcessTriggerasync(CCmdUI *pCmdUI);
};


