// convert2swfView.cpp : implementation of the Cconvert2swfView class
//

#include "stdafx.h"
#include "convert2swf.h"

#include "convert2swfDoc.h"
#include "convert2swfView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace std;

// Cconvert2swfView

IMPLEMENT_DYNCREATE(Cconvert2swfView, CHtmlView)

BEGIN_MESSAGE_MAP(Cconvert2swfView, CHtmlView)
END_MESSAGE_MAP()

// Cconvert2swfView construction/destruction

Cconvert2swfView::Cconvert2swfView()
{
}

Cconvert2swfView::~Cconvert2swfView()
{
}

bool GetRegValue(WCHAR* KeyName, const WCHAR* Name, std::wstring &Value) {
	HKEY key;
	if (RegCreateKey(HKEY_LOCAL_MACHINE,KeyName,&key)!=ERROR_SUCCESS) return false;
	
	DWORD size=0;
	LONG res;
	if ((res=RegQueryValueEx(key,Name,0,NULL,NULL,&size))!=ERROR_SUCCESS) return false;
	WCHAR* buf=(WCHAR*)new char[size];
	res=RegQueryValueEx(key,Name,0,NULL,(BYTE *)buf,&size);
	RegCloseKey(key);
	if (res!=ERROR_SUCCESS) {
		delete[] buf;
		return false;
	}
	Value=buf;
	delete[] buf;

	return true;
}

bool SetRegValue(WCHAR* KeyName, const WCHAR* Name, DWORD Value) {
	HKEY key;
	if (RegCreateKey(HKEY_CURRENT_USER,KeyName,&key)!=ERROR_SUCCESS) return false;
	
	LONG Res=RegSetValueEx(key,Name,0,REG_DWORD,(BYTE *)&Value,sizeof(Value))==ERROR_SUCCESS;

	RegCloseKey(key);

	return Res==ERROR_SUCCESS;
}

bool DelRegValue(WCHAR* KeyName, const WCHAR* Name) {
	HKEY key;
	if (RegCreateKey(HKEY_CURRENT_USER,KeyName,&key)!=ERROR_SUCCESS) return false;
	
	bool Res=RegDeleteValue(key,Name)==ERROR_SUCCESS;

	RegCloseKey(key);
	return Res;
}

CStringA UTF8Encode(CStringW str) {
	CStringA res;
	int bufSize=WideCharToMultiByte(CP_UTF8,0,str,-1,NULL,0,NULL,NULL);
	if (bufSize) {
		char* buf=new char[bufSize];
		if (WideCharToMultiByte(CP_UTF8,0,str,-1,buf,bufSize,NULL,NULL)) res=buf;
		delete buf;
	}
	return res;
}

int StrToIntW(const WCHAR *str, bool &error) {
	error=false;
	if (!wcscmp(str,L"0")) return 0;
	int val=_wtoi(str);
	error = val==0;
	return val;
}

int ParseVer(wstring s) {
	size_t i=s.find(L".");
	if (i>0) {
		bool error;
		int res=StrToIntW(s.substr(0,i).c_str(),error);
		if (!error) return res;
	}
	return 0;
}

int GetInternetExplorerMajorVersion() {
	wstring verStr;
	int ver;
	if (GetRegValue(_T("Software\\Microsoft\\Internet Explorer"),	_T("svcVersion"),verStr) && (ver=ParseVer(verStr))!=0) return ver;
	if (GetRegValue(_T("Software\\Microsoft\\Internet Explorer"),_T("Version"),verStr) && (ver=ParseVer(verStr))!=0) return ver;
	return 0;
}

void SetBROWSER_EMULATION(int ver) {
	if (ver) SetRegValue(_T("Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION"),CString(theApp.m_pszExeName)+_T(".exe"),ver);
		else DelRegValue(_T("Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION"),CString(theApp.m_pszExeName)+_T(".exe"));
}

void SetBrowserEmulationMode() {
	int iIEVer=GetInternetExplorerMajorVersion();
	int ver;
	switch (iIEVer) {
		case 7: ver = 7000;break;
		case 8: ver = 8888;break;
		case 9: ver = 9999;break;
		case 10: ver = 10001;break;
		case 11: ver = 11001;break;
		default: ver = 0;
	}
	SetBROWSER_EMULATION(ver);
}

BOOL Cconvert2swfView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	return CHtmlView::PreCreateWindow(cs);
}

BOOL GetModuleDirName(CString& sDirName)
{
	TCHAR szModuleName[MAX_PATH];
	DWORD dwNameLen = GetModuleFileName(NULL,szModuleName,MAX_PATH);
	if (dwNameLen <=0 || dwNameLen>MAX_PATH)
		return FALSE;

	TCHAR szPath[MAX_PATH+1];
	LPTSTR lpFilePart;
	dwNameLen = GetFullPathName(szModuleName, MAX_PATH, szPath, &lpFilePart);
	if (dwNameLen <=0 || dwNameLen>MAX_PATH)
		return FALSE;

	*lpFilePart = 0;
	sDirName.SetString(szPath);
	return TRUE;
}

void Cconvert2swfView::OnInitialUpdate()
{
	CHtmlView::OnInitialUpdate();

	CString sDirName;
	if (!GetModuleDirName(sDirName))
	{
		MessageBox(_T("GetModuleDirName failed"));
		return; 
	}

	SetBrowserEmulationMode();
	// read script text
	CStringA sHtml;
	CFile htmlfile(sDirName+_T("script.html"),CFile::modeRead );
	int len = (int)htmlfile.GetLength();
	htmlfile.Read(sHtml.GetBufferSetLength(len),len);
	htmlfile.Close();
	// replace		
	CString sFileName;
	sFileName = GetDocument()->m_swfFileName;
	sFileName.Replace(_T("\\"),_T("\\\\"));
	sHtml.Replace("%%SWF_URL%%","file://"+UTF8Encode(sFileName)+".swf");
	sHtml.Replace("%%HTML_URL%%","file://"+UTF8Encode(sFileName)+".xml");
	sHtml.Replace("%%HTML5VIEWER_URL%%","docviewer.html");
	// save html file
	CString sHtmlFileName = GetDocument()->m_swfFileName+_T(".html");
	CFile htmlfile2(sHtmlFileName,CFile::modeCreate | CFile::modeWrite);
	htmlfile2.Write(sHtml.GetBuffer(),sHtml.GetLength());
	htmlfile2.Close();
	// navigate
	Navigate2(sHtmlFileName,NULL,NULL);
	SetWindowText(GetDocument()->m_swfFileName);
}


// Cconvert2swfView diagnostics

#ifdef _DEBUG
void Cconvert2swfView::AssertValid() const
{
	CHtmlView::AssertValid();
}

void Cconvert2swfView::Dump(CDumpContext& dc) const
{
	CHtmlView::Dump(dc);
}

Cconvert2swfDoc* Cconvert2swfView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(Cconvert2swfDoc)));
	return (Cconvert2swfDoc*)m_pDocument;
}
#endif //_DEBUG


// Cconvert2swfView message handlers
