
// FYEDrawDemoVCView.cpp: CFYEDrawDemoVCView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "FYEDrawDemoVC.h"
#endif

#include "FYEDrawDemoVCDoc.h"
#include "FYEDrawDemoVCView.h"

#include "DevicePropDlg.h"
#include "LinePropDlg.h"
#include "FillPropDlg.h"
#include "TextPropDlg.h"
#include "FontPropDlg.h"
#include "ImgPropDlg.h"
#include "CanvansPropDlg.h"
#include "ZoomPropDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#pragma comment(lib,"FYEDC.lib")
extern "C" __declspec(dllimport) class CECanvas;
extern "C" __declspec(dllimport) class CShapeElement;

extern "C" __declspec(dllimport) CECanvas * NewFYEDC();
extern "C" __declspec(dllimport) void CreateFYEDC(CECanvas * canvas, HWND hParentWnd, int x, int y, int w, int h);
extern "C" __declspec(dllimport) void LoadShapes(CECanvas * canvas, const char* fileName);
extern "C" __declspec(dllimport) void DeleteFYEDC(CECanvas * canvas);
extern "C" __declspec(dllimport) void SetCanvasRect(CECanvas * canvas, int left, int top, int right, int bottom);
extern "C" __declspec(dllimport) void GetCanvasRect(CECanvas* canvas, int& left, int& top, int& right, int& bottom);
extern "C" __declspec(dllimport) int GetShapeCount(CECanvas * canvas);
extern "C" __declspec(dllimport) CShapeElement* GetShape(CECanvas* canvas, const char* type, const char* name);
extern "C" __declspec(dllimport) CShapeElement* GetShapeAt(CECanvas* canvas, int index);
extern "C" __declspec(dllimport) int GetCompElementCount(CECanvas * canvas, const char* type, const char* name);
extern "C" __declspec(dllimport) int GetCompElement(CShapeElement * *shapes, CECanvas * canvas, const char* type, const char* name);

extern "C" __declspec(dllimport) int GetShapeTypeCount(CECanvas* canvas, const char* type);
extern "C" __declspec(dllimport) int GetShapeTitleCount(CECanvas* canvas, const char* title);
extern "C" __declspec(dllimport) int GetShapeNameCount(CECanvas* canvas, const char* name);

extern "C" __declspec(dllimport) void GetShapeType(CShapeElement* shape, wchar_t* type);
extern "C" __declspec(dllimport) void GetShapeTitle(CShapeElement* shape, wchar_t* title);
extern "C" __declspec(dllimport) void SetShapeTitle(CECanvas* canvas, CShapeElement* shape, const char* title);
extern "C" __declspec(dllimport) void GetShapeName(CShapeElement* shape, wchar_t* name);
extern "C" __declspec(dllimport) void SetShapeName(CECanvas* canvas, CShapeElement* shape, const char* name);

extern "C" __declspec(dllimport) int GetShapeGroupId(CShapeElement* shape);
extern "C" __declspec(dllimport) int GetBackColor(CECanvas* canvas);
extern "C" __declspec(dllimport) void SetBackColor(CECanvas* canvas, int color);
extern "C" __declspec(dllimport) void SetNoClientColor(CECanvas* canvas, int color);
extern "C" __declspec(dllimport) void SetLineColor(CECanvas* canvas, CShapeElement* shape, int color);
extern "C" __declspec(dllimport) int GetLineColor(CShapeElement* shape);
extern "C" __declspec(dllimport) void SetLineStyle(CECanvas* canvas, CShapeElement* shape, int style);
extern "C" __declspec(dllimport) int GetLineStyle(CShapeElement* shape);
extern "C" __declspec(dllimport) void SetLineEndMode(CECanvas* canvas, CShapeElement* shape, int mode);
extern "C" __declspec(dllimport) int GetLineEndMode(CShapeElement* shape);
extern "C" __declspec(dllimport) void SetLineWidth(CECanvas* canvas, CShapeElement* shape, int width);
extern "C" __declspec(dllimport) int GetLineWidth(CShapeElement* shape);
extern "C" __declspec(dllimport) void SetFillColor(CECanvas* canvas, CShapeElement* shape, int color);
extern "C" __declspec(dllimport) int GetFillColor(CShapeElement* shape);
extern "C" __declspec(dllimport) void SetIsFill(CECanvas* canvas, CShapeElement* shape, BOOL isFill);
extern "C" __declspec(dllimport) int GetIsFill(CShapeElement* shape);
extern "C" __declspec(dllimport) void SetFontColor(CECanvas* canvas, CShapeElement* shape, int color);
extern "C" __declspec(dllimport) int GetFontColor(CShapeElement* shape);
extern "C" __declspec(dllimport) void SetFontType(CECanvas* canvas, CShapeElement* shape, const char* fntype);
extern "C" __declspec(dllimport) void GetFontType(CShapeElement* shape, wchar_t* fntype);
extern "C" __declspec(dllimport) void SetFontSize(CECanvas* canvas, CShapeElement* shape, int size);
extern "C" __declspec(dllimport) int GetFontSize(CShapeElement* shape);
extern "C" __declspec(dllimport) void SetShapeText(CECanvas* canvas, CShapeElement* shape, const char* text);
extern "C" __declspec(dllimport) void GetShapeText(CShapeElement* shape, wchar_t* text);
extern "C" __declspec(dllimport) void SetTextVisible(CECanvas* canvas, CShapeElement* shape, BOOL isShow);
extern "C" __declspec(dllimport) int GetTextVisible(CShapeElement* shape);
extern "C" __declspec(dllimport) int GetImageStatus(CShapeElement* shape);
extern "C" __declspec(dllimport) void SetImageStatus(CECanvas* canvas, CShapeElement* shape, int status);

extern "C" __declspec(dllimport) void SetVirtualSize(CECanvas* canvas, int width, int height);
extern "C" __declspec(dllimport) void GetVirtualSize(CECanvas* canvas, int& width, int& height);
extern "C" __declspec(dllimport) void SetZoom(CECanvas* canvas, double zoom);
extern "C" __declspec(dllimport) double GetZoom(CECanvas* canvas);
extern "C" __declspec(dllimport) void SetZoomFactor(CECanvas* canvas, double factor);
extern "C" __declspec(dllimport) double GetZoomFactor(CECanvas* canvas);
extern "C" __declspec(dllimport) void SetGridShow(CECanvas* canvas, BOOL bShow);
extern "C" __declspec(dllimport) BOOL SaveToPicture(CECanvas* canvas, const char* fileName);

// CFYEDrawDemoVCView

IMPLEMENT_DYNCREATE(CFYEDrawDemoVCView, CView)

BEGIN_MESSAGE_MAP(CFYEDrawDemoVCView, CView)
	ON_COMMAND(ID_FILE_EXPORT, &CFYEDrawDemoVCView::OnFileExport)
	ON_COMMAND(ID_PROP_DEVICE, &CFYEDrawDemoVCView::OnPropDevice)
	ON_COMMAND(ID_PROP_LINE, &CFYEDrawDemoVCView::OnPropLine)
	ON_COMMAND(ID_PROP_AREA, &CFYEDrawDemoVCView::OnPropArea)
	ON_COMMAND(ID_PROP_TEXT, &CFYEDrawDemoVCView::OnPropText)
	ON_COMMAND(ID_PROP_FONT, &CFYEDrawDemoVCView::OnPropFont)
	ON_COMMAND(ID_PROP_VANVAS, &CFYEDrawDemoVCView::OnPropVanvas)
	ON_COMMAND(ID_PROP_ZOOM, &CFYEDrawDemoVCView::OnPropZoom)
	ON_COMMAND(ID_PROP_IMG, &CFYEDrawDemoVCView::OnPropImg)
	ON_WM_SIZE()
	ON_COMMAND(ID_FILE_NEW, &CFYEDrawDemoVCView::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, &CFYEDrawDemoVCView::OnFileOpen)
END_MESSAGE_MAP()


// CFYEDrawDemoVCView 构造/析构

CECanvas* m_shapeCanvas = NULL;
int m_iShapeSeqNum = 0;

CFYEDrawDemoVCView::CFYEDrawDemoVCView() noexcept
{
	// TODO: 在此处添加构造代码
}

CFYEDrawDemoVCView::~CFYEDrawDemoVCView()
{
}

BOOL CFYEDrawDemoVCView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CFYEDrawDemoVCView 绘图

void CFYEDrawDemoVCView::OnDraw(CDC* /*pDC*/)
{
	CFYEDrawDemoVCDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码
}


// CFYEDrawDemoVCView 诊断

#ifdef _DEBUG
void CFYEDrawDemoVCView::AssertValid() const
{
	CView::AssertValid();
}

void CFYEDrawDemoVCView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CFYEDrawDemoVCDoc* CFYEDrawDemoVCView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFYEDrawDemoVCDoc)));
	return (CFYEDrawDemoVCDoc*)m_pDocument;
}
#endif //_DEBUG


// CFYEDrawDemoVCView 消息处理程序
void CFYEDrawDemoVCView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	if (m_shapeCanvas != NULL)
	{
		CRect rect;
		GetWindowRect(&rect);
		ScreenToClient(&rect);	//将控件大小转换为在对话框中的区域坐标
		SetCanvasRect(m_shapeCanvas, rect.left, rect.top, rect.right, rect.bottom);
	}
	else
	{
		CRect clRect;
		GetClientRect(clRect);
		m_shapeCanvas = NewFYEDC();
		CreateFYEDC(m_shapeCanvas, this->m_hWnd, clRect.left, clRect.top, clRect.right, clRect.bottom);
	}
}

void CFYEDrawDemoVCView::OnFileNew()
{
	// TODO: 在此添加命令处理程序代码
	
}

void CFYEDrawDemoVCView::OnFileOpen()
{
	// TODO: 在此添加命令处理程序代码
	CString fn = "C:\\Users\\ldong\\Desktop\\111.txt";

	CFileDialog fileDlg(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		_T("All File (*.*)|*.*|*.txt|*.txt|*.ini|*.ini||"), NULL);
	if (fileDlg.DoModal() == IDOK)
	{
		fn = fileDlg.GetPathName();	//得到完整的文件名和目录名拓展名  
		//if (fileName.GetLength() > 0)				//支持保存为空，即删除图片
		{
			LoadShapes(m_shapeCanvas, fn);
			GetParent()->SetWindowTextA(fn + " - 浮云E绘图3.0 API Demo（VC版）");
		}
	}
}


void CFYEDrawDemoVCView::OnFileExport()
{
	// TODO: 在此添加命令处理程序代码

	CFileDialog fileDlg(false, NULL, "*.jpg", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		_T("All File (*.*)|*.*|*.jpg|*.jpg|*.bmp|*.bmp||"), NULL);
	
	if (fileDlg.DoModal() == IDOK)
	{
		CString fn = fileDlg.GetPathName();	//得到完整的文件名和目录名拓展名  
		//if (fileName.GetLength() > 0)				//支持保存为空，即删除图片
		{
			SaveToPicture(m_shapeCanvas, fn);
		}
	}
}


void CFYEDrawDemoVCView::OnPropDevice()
{
	// TODO: 在此添加命令处理程序代码
	CDevicePropDlg dlg;

	CShapeElement* shape = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
	if (shape != NULL)
	{
		wchar_t* sT = new wchar_t[200];
		GetShapeTitle(shape, sT);
		dlg.m_sTitle = CString(sT);
		delete []sT;

		wchar_t* sN = new wchar_t[200];
		GetShapeName(shape, sN);
		dlg.m_sName = CString(sN);
		delete []sN;

		if (dlg.DoModal() == IDOK)
		{
			m_iShapeSeqNum = dlg.m_iShapeID;
			CShapeElement* shape2 = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
			if (shape2 != NULL)
			{
				SetShapeTitle(m_shapeCanvas, shape2, dlg.m_sTitle);
				SetShapeName(m_shapeCanvas, shape2, dlg.m_sName);
			}
		}
	}

}


void CFYEDrawDemoVCView::OnPropLine()
{
	// TODO: 在此添加命令处理程序代码
	CLinePropDlg dlg;

	CShapeElement* shape = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
	if (shape != NULL)
	{
		dlg.m_iLineColor = GetLineColor(shape);
		dlg.m_iLineWidth = GetLineWidth(shape);
		dlg.m_iLineStyle = GetLineStyle(shape);			//0普通实线 1-- 2.. 5无
		dlg.m_iEndMode = GetLineEndMode(shape);			//0无 11起点○ 12终端○ 13两端○  21起点● 22终端● 23两端● 31起点← 32终端← 33两端←

		if (dlg.DoModal() == IDOK)
		{
			m_iShapeSeqNum = dlg.m_iShapeID;

			dlg.m_iLineStyle = (dlg.m_iLineStyle == 0 || dlg.m_iLineStyle == 1 || dlg.m_iLineStyle == 2 || dlg.m_iLineStyle == 5) 
				? dlg.m_iLineStyle : 0;
			dlg.m_iEndMode = (dlg.m_iEndMode == 0 || dlg.m_iEndMode == 11 || dlg.m_iEndMode == 12 || dlg.m_iEndMode == 13 
				|| dlg.m_iEndMode == 21 || dlg.m_iEndMode == 22 || dlg.m_iEndMode == 23 
				|| dlg.m_iEndMode == 31 || dlg.m_iEndMode == 32 || dlg.m_iEndMode == 33)
				? dlg.m_iEndMode : 0;

			CShapeElement* shape2 = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
			if (shape2 != NULL)
			{
				SetLineColor(m_shapeCanvas, shape2, dlg.m_iLineColor);
				SetLineWidth(m_shapeCanvas, shape2, dlg.m_iLineWidth);
				SetLineStyle(m_shapeCanvas, shape2, dlg.m_iLineStyle);
				SetLineEndMode(m_shapeCanvas, shape2, dlg.m_iEndMode);
			}
		}
	}
}


void CFYEDrawDemoVCView::OnPropArea()
{
	// TODO: 在此添加命令处理程序代码
	CFillPropDlg dlg;

	CShapeElement* shape = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
	if (shape != NULL)
	{
		dlg.m_bChkFill = GetIsFill(shape);
		dlg.m_iFillColor = GetFillColor(shape);

		if (dlg.DoModal() == IDOK)
		{
			m_iShapeSeqNum = dlg.m_iShapeID;
			CShapeElement* shape2 = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
			if (shape2 != NULL)
			{
				SetIsFill(m_shapeCanvas, shape2, dlg.m_bChkFill);
				SetFillColor(m_shapeCanvas, shape2, dlg.m_iFillColor);
			}
		}
	}
}

void CFYEDrawDemoVCView::OnPropText()
{
	// TODO: 在此添加命令处理程序代码
	CTextPropDlg dlg;

	CShapeElement* shape = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
	if (shape != NULL)
	{
		dlg.m_bShow = GetTextVisible(shape);

		wchar_t* sT = new wchar_t[200];
		GetShapeText(shape, sT);
		dlg.m_sContent = CString(sT);
		delete []sT;
		

		if (dlg.DoModal() == IDOK)
		{
			m_iShapeSeqNum = dlg.m_iShapeID;
			CShapeElement* shape2 = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
			if (shape2 != NULL)
			{
				SetTextVisible(m_shapeCanvas, shape2, dlg.m_bShow);
				SetShapeText(m_shapeCanvas, shape2, dlg.m_sContent);
			}
		}
	}
}

void CFYEDrawDemoVCView::OnPropFont()
{
	// TODO: 在此添加命令处理程序代码
	CFontPropDlg dlg;

	CShapeElement* shape = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
	if (shape != NULL)
	{
		wchar_t* sT = new wchar_t[200];
		GetFontType(shape, sT);
		dlg.m_sFontType = CString(sT);
		delete[]sT;
		
		dlg.m_iFontColor = GetFontColor(shape);
		dlg.m_iFontSize = GetFontSize(shape);

		if (dlg.DoModal() == IDOK)
		{
			m_iShapeSeqNum = dlg.m_iShapeID;
			CShapeElement* shape2 = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
			if (shape2 != NULL)
			{
				SetFontType(m_shapeCanvas, shape2, dlg.m_sFontType);
				SetFontColor(m_shapeCanvas, shape2, dlg.m_iFontColor);
				SetFontSize(m_shapeCanvas, shape2, dlg.m_iFontSize);
			}
		}
	}
}

void CFYEDrawDemoVCView::OnPropImg()
{
	// TODO: 在此添加命令处理程序代码
	CImgPropDlg dlg;

	CShapeElement* shape = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
	
	wchar_t* sT = new wchar_t[200];
	GetShapeType(shape, sT);
	CString sType = CString(sT);
	delete[]sT;

	if (sType != "image")
	{
		CString msg;
		msg.Format("当前第%d个图元不是图片图元", m_iShapeSeqNum+1);
		AfxMessageBox(msg);
		return;
	}

	if (shape != NULL)
	{
		dlg.m_iShapeID = m_iShapeSeqNum;
		dlg.m_iImgStatus = GetImageStatus(shape);

		if (dlg.DoModal() == IDOK)
		{
			m_iShapeSeqNum = dlg.m_iShapeID;
			CShapeElement* shape2 = GetShapeAt(m_shapeCanvas, m_iShapeSeqNum);
			if (shape2 != NULL)
			{
				SetImageStatus(m_shapeCanvas, shape2, dlg.m_iImgStatus);
			}
		}
	}
}

void CFYEDrawDemoVCView::OnPropVanvas()
{
	// TODO: 在此添加命令处理程序代码
	CCanvansPropDlg dlg;

	int l = 0;
	int r = 0;
	int t = 0;
	int b = 0;
	GetCanvasRect(m_shapeCanvas, l, t, r, b);
	dlg.m_iCanvWidth = r - l;
	dlg.m_iCanvHeight = b - t;

	int w = 0;
	int h = 0;
	GetVirtualSize(m_shapeCanvas, w, h);
	dlg.m_iVirWidth = w;
	dlg.m_iVirHeight = h;

	dlg.m_bGridShow = 0;

	if (dlg.DoModal() == IDOK)
	{
		CRect rect;
		GetWindowRect(&rect);
		ScreenToClient(&rect);

		SetCanvasRect(m_shapeCanvas, rect.left, rect.top, dlg.m_iCanvWidth + rect.left, dlg.m_iCanvHeight + rect.top);
		SetVirtualSize(m_shapeCanvas, dlg.m_iVirWidth, dlg.m_iVirHeight);
		SetGridShow(m_shapeCanvas, dlg.m_bGridShow != 0);
	}
}


void CFYEDrawDemoVCView::OnPropZoom()
{
	// TODO: 在此添加命令处理程序代码
	CZoomPropDlg dlg;

	dlg.m_fZoom = GetZoom(m_shapeCanvas);
	dlg.m_fZoomFactor = GetZoomFactor(m_shapeCanvas);
	if (dlg.DoModal() == IDOK)
	{
		SetZoom(m_shapeCanvas, dlg.m_fZoom);
		SetZoomFactor(m_shapeCanvas, dlg.m_fZoomFactor);
	}

}
