// DiagramEngDoc.h : interface of the CDiagramEngDoc class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_DIAGRAMENGDOC_H__9DA8C218_5A85_4AB4_B77B_A9F8FF828990__INCLUDED_)
#define AFX_DIAGRAMENGDOC_H__9DA8C218_5A85_4AB4_B77B_A9F8FF828990__INCLUDED_

#include "SystemModel.h"	// Added by ClassView
#include "Clipboard.h"		// rqd. of CClipboard obj.
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

// DiagramEng (start)
// User defined constructs
enum EBlockType{eChirpSignal, eConstBlock, eDerivativeBlock, eDivideBlock, eGainBlock, eIntegratorBlock, eLinearFnBlock, 
				eOutputBlock, eSignalGenBlock, eSubsysBlock, eSubsysInBlock, eSubsysOutBlock, eSumBlock, eTransferFnBlock};

// AssertManual() - explict version to work with Debug and Release versions of the exe.
#define AssertManual(expr) do{ if(!(expr)) {PrintAssertMsg(__FILE__, __LINE__); __asm{int 3}; } }while(0)

// See below class definition for global fn defn.
// DiagramEng (end)

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

// Attributes
public:

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CDiagramEngDoc)
	public:
	virtual BOOL OnNewDocument();
	virtual void Serialize(CArchive& ar);
	//}}AFX_VIRTUAL

// Implementation
public:
	int TrackAnnotation(CPoint point, CWnd *pWnd);
	int FineMoveAnnotation(int key_flag);
	int DeleteAnnotation(void);
	void SaveSystemModelToList(void);
	virtual void SetModifiedFlag(BOOL bModified);
	void DeleteSystemModelList(void);
	int GetEditRedoFlag(void) const;
	int GetEditUndoFlag(void) const;
	int GetSelectAllFlag(void) const;
	int ResetSelectAll(void);
	void ResetDefaultGeometry(void);
	void SetDeltaLength(double length);
	double GetDeltaLength(void) const;
	const CString& GetFilePath(void);
	void OnFileSaveAccessor(void);
	void SetFilePath(CString path);
	void OnFileOpenAccessor(void);
	int OnFileCloseAccessor(void);
	int ReadDiagramEngDataFromFile(CString file_path);
	void WriteDiagramEngDataToFile(CString file_path, int display);
	void DeleteOutputBlockView(CBlock *ptr_blk);
	void SnapConnectionEndPointsAfterInitialConstruction(void);
	int FineMoveConnectionBendPoint(int key_flag);
	int FineMoveBlock(int key_flag);
	void FineMoveItem(int key_flag);
	int GetFineMoveFlag(void) const;
	void SetFineMoveFlag(int fine_move_flag);
	int DeletePort(void);
	int SetBlockPortProperties(CPoint point);
	int SetConnectionProperties(CPoint point);
	int DetermineCurrentAndIntersectRects(CRectTracker temp_tracker, CPoint item_posn, double delta);
	int DetermineTrackerDeltaPosition(CWnd *pWnd, CPoint point, CPoint tracker_init, CPoint &delta_posn);
	void SetKeyFlagTrack(int key_flag);
	int TrackMultipleItems(CPoint point, CWnd *pWnd);
	int CheckPointOnLineSegment(CPoint prev_pt, CPoint current_pt, CPoint point);
	int DoubleLeftClickBlock(CPoint point);
	void DisconnectTailPointsFromBendPoints(CConnection *con);
	void DisconnectTailPointFromBendPoint(CPoint *bend_pt);
	void DisconnectEndPointsFromPorts(CBlock *block);
	void DisconnectEndPointFromPort(CPort *port);
	CPoint* SnapConnectionTailPointToBendPoint(CPoint &tail_pt);
	int DeleteConnection(void);
	int DeleteConnectionBendPoint(void);
	int DeleteBlock(void);
	int TrackConnectionBendPoint(CPoint point, CWnd *pWnd);
	CPort* SnapConnectionHeadPointToPort(CPoint &head_pt);
	CPort* SnapConnectionTailPointToPort(CPoint &tail_pt);
	int TrackConnectionEndPoint(CPoint point, CWnd *pWnd);
	int TrackBlock(CPoint point, CWnd *pWnd);
	int TrackItem(CPoint point, CWnd* pWnd);
	CRectTracker GetRectTracker(void) const;
	void SetContextMenuPoint(CPoint point);
	void ConstructTransferFnBlock(void);
	void ConstructSubsystemOutBlock(void);
	void ConstructSubsystemInBlock(void);
	void ConstructSubsystemBlock(void);
	void ConstructSignalGeneratorBlock(void);
	void ConstructOutputBlock(void);
	void ConstructLinearFnBlock(void);
	void ConstructDivideBlock(void);
	void ConstructIntegratorBlock(void);
	void ConstructDerivativeBlock(void);
	void ConstructChirpSignalBlock(void);
	void ConstructSumBlock(void);
	void AssignBlockPosition(CPoint &block_posn);
	void ConstructGainBlock(void);
	void CheckSystemModel(void);
	CSystemModel &GetSystemModel(void);
	void ConstructConstantBlock(void);
	int ConstructBlock(EBlockType e_block_type);
	void UpdateCommonOpsToolbar(CString string);
	virtual ~CDiagramEngDoc();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	//{{AFX_MSG(CDiagramEngDoc)
	afx_msg void OnEditSelectAll();
	afx_msg void OnModelBuild();
	afx_msg void OnModelBuildSubsystem();
	afx_msg void OnSimStart();
	afx_msg void OnSimStop();
	afx_msg void OnFormatShowAnnotations();
	afx_msg void OnToolsDiagnosticInfo();
	afx_msg void OnHelpUsingDiagramEng();
	afx_msg void OnBlockCtsDerivative();
	afx_msg void OnBlockCtsIntegrator();
	afx_msg void OnBlockCtsTransferfn();
	afx_msg void OnBlockMathopsDivide();
	afx_msg void OnBlockMathopsGain();
	afx_msg void OnBlockMathopsSum();
	afx_msg void OnBlockSinkOutput();
	afx_msg void OnBlockSourceConst();
	afx_msg void OnBlockSourceLinearfn();
	afx_msg void OnBlockSourceSignalgen();
	afx_msg void OnBlockSubsysSubsys();
	afx_msg void OnBlockSubsysSubsysin();
	afx_msg void OnBlockSubsysSubsysout();
	afx_msg void OnDeleteItem();
	afx_msg void OnInsertBendPoint();
	afx_msg void OnInitTrackMultipleItems();
	afx_msg void OnSimNumericalSolver();
	afx_msg void OnEditAddMultipleBlocks();
	afx_msg void OnEditDeleteGroupedItems();
	afx_msg void OnSetItemProperties();
	afx_msg void OnFineMoveItem();
	afx_msg void OnReverseBlockDirection();
	afx_msg void OnSetOutputSignal();
	afx_msg void OnFileSave();
	afx_msg void OnFileSaveAs();
	afx_msg void OnFileOpen();
	afx_msg int OnFileClose();
	afx_msg void OnEditCut();
	afx_msg void OnEditCopy();
	afx_msg void OnEditPaste();
	afx_msg void OnEditUndo();
	afx_msg void OnEditRedo();
	afx_msg void OnFormatAnnotation();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
private:
	int m_iEditRedoFlag;
	int m_iEditUndoFlag;
	int m_iIndexSystemModel;
	int m_iNSystemModels;
	
	int m_iFineMoveFlag;
	int m_iRubberBandCreated;
	int m_iSelectAll;
	int m_iKeyFlagTrack;
	double m_dDeltaLength;					// standard reference delta length for user GUI elements
	CPoint m_ptContextMenu;					// coords of the context menu after ScreenToClient() conversion.
	CClipboard m_Clipboard;					// clipboard obj.
	CRectTracker m_RectTracker;
	CString m_strFilePath;					// path of file used in serialization
	CSystemModel m_SystemModel;				// system model obj.
	list<CSystemModel*> m_lstSystemModel;	// list of address of system model object.
};

// DiagramEng (start)

// -- GLOBAL FUNCTIONS
CDiagramEngDoc* GetDocumentGlobalFn(void);		// gets a ptr to the doc
int AssertManualFn(bool expr, char *file_name, int line);
int ConvertDoubleMatrixToString(CString &str, double **matrix, int nrows, int ncols);
int ConvertDoubleVectorToString(CString &str, double *vec, int length);
double **ConvertStringMatrixToDoubleMatrix(char **str_matrix, int nrows, int ncols);
double **ConvertStringToDouble(CString string, int &nrows, int &ncols);
int DetermineDataDimsByStrpbrk(char *in_str, int &nrows, int &ncols, int &max_row_length);
int DetermineDataDimsByStrtok(char *in_str, int &nrows, int &ncols, int &max_row_length);
int GaussJordanEliminationFullPivoting(double **A, int n, double **B, int m);
void MatrixMultiply(int nrowsA, int ncolsA, int ncolsB, double **A, double **B, double **C);
void PrintAssertMsg(char *file_name, int line);
int PrintMatrix(double **M, int nrows, int ncols);
int PrintMatrix(int **M, int nrows, int ncols);
int PrintVector(double *v, int ncols);
int PrintVector(float *v, int ncols);
int PrintVector(int *v, int ncols);
char *StripInputString(char *input_str);
void Swap(double &a, double &b);
void UpdateAllViewsForAllDocs(void);

// DiagramEng (end)

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DIAGRAMENGDOC_H__9DA8C218_5A85_4AB4_B77B_A9F8FF828990__INCLUDED_)
