// EditWnd.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed.h"
#include "EditWnd.h"
#include <direct.h>

#define __mbsrchr(s,c) (char*)_mbsrchr((const unsigned char*)(s),(c))

extern CMW4GameEdApp theApp;

static DWORD CALLBACK MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
   CFile* pFile = (CFile*) dwCookie;

   *pcb = pFile->Read(pbBuff, cb);

   return 0;
}

static DWORD CALLBACK MyStreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
   CFile* pFile = (CFile*) dwCookie;

   pFile->Write(pbBuff, cb);
   *pcb = cb;

   return 0;
}


/////////////////////////////////////////////////////////////////////////////
// CEditWnd

CEditWnd::CEditWnd()
{
	CloseCallback = NULL;
}

CEditWnd::CEditWnd(char* string)
{
	path = string;
}

CEditWnd::~CEditWnd()
{
}


BEGIN_MESSAGE_MAP(CEditWnd, CEditorChildWnd)
	//{{AFX_MSG_MAP(CEditWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_CLOSE()
	ON_WM_VSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditWnd message handlers

BOOL CEditWnd::PreCreateWindow(CREATESTRUCT& cs) 
{
	cs.style |= WS_CHILD | WS_OVERLAPPEDWINDOW;
	
	return CWnd::PreCreateWindow(cs);
}

int CEditWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	RECT r;
	GetClientRect(&r);
	editWnd.Create(ES_LEFT | ES_MULTILINE | ES_WANTRETURN | ES_AUTOVSCROLL | WS_CHILD | WS_VISIBLE,r,this,0);

	editWnd.SetBackgroundColor(TRUE,RGB(255,255,0));
	
	LoadFile();

	return 0;
}

void CEditWnd::OnSize(UINT nType, int cx, int cy) 
{
	CEditorChildWnd::OnSize(nType, cx, cy);
	
	editWnd.SetWindowPos(NULL,0,0,cx,cy,SWP_NOZORDER);	
}

void CEditWnd::OnClose() 
{
	MessageBeep(MB_ICONQUESTION);
	if (MessageBox("Save this data?","Save",MB_ICONQUESTION | MB_YESNO) == IDYES)
	{
		if (path)
		{
			CFile cFile(path,CFile::modeCreate | CFile::modeWrite);
			EDITSTREAM es;

			es.dwCookie = (DWORD) &cFile;
			es.pfnCallback = MyStreamOutCallback; 
			editWnd.StreamOut(SF_TEXT, es);
			cFile.Close();
			if (CloseCallback)
				CloseCallback((char*)(LPCTSTR)path);
		}
		else
		{
			char scriptPath[MAX_PATH],oldDir[MAX_PATH],path[MAX_PATH],*p;
			OPENFILENAME ofn;

			_getcwd(oldDir,MAX_PATH - 1);

			strcpy(path,theApp.missionFileName);
			p = __mbsrchr(path,'\\');
			*p = 0;
			_chdir(path);

			memset(&scriptPath,0,MAX_PATH);
			memset(&ofn,0,sizeof(ofn));

			ofn.lStructSize = sizeof(ofn);
			ofn.hwndOwner = NULL;
			ofn.lpstrFilter = "ABL files {*.ABL}\0*.abl\0All Files {*.*}\0*.*\0\0";
			ofn.lpstrFile = scriptPath;
			ofn.nMaxFile = MAX_PATH;
			ofn.lpstrDefExt = "abl";

			if (GetSaveFileName(&ofn))
			{
				CFile cFile(ofn.lpstrFile,CFile::modeCreate | CFile::modeWrite);
				EDITSTREAM es;

				es.dwCookie = (DWORD) &cFile;
				es.pfnCallback = MyStreamOutCallback; 
				editWnd.StreamOut(SF_TEXT, es);
				cFile.Close();
				if (CloseCallback && !CloseCallback(ofn.lpstrFile))
					return;
			}
			else
				return;
		}
	}
	else
		LoadFile();

	ShowWindow(SW_HIDE);
//	CEditorChildWnd::OnClose();
}

void CEditWnd::LoadFile(char* filepath)
{
	if (filepath)
		path = filepath;

	if (path)
	{
		CFile cFile;
		EDITSTREAM es;

		if (cFile.Open(path,CFile::modeRead,NULL))
		{
   			es.dwCookie = (DWORD) &cFile;
			es.pfnCallback = MyStreamInCallback; 
			editWnd.StreamIn(SF_TEXT, es);
			cFile.Close();
		}
	}
}

void CEditWnd::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	
	CEditorChildWnd::OnVScroll(nSBCode, nPos, pScrollBar);
}
/////////////////////////////////////////////////////////////////////////////
// CEditCtrl

CEditCtrl::CEditCtrl()
{
}

CEditCtrl::~CEditCtrl()
{
}


BEGIN_MESSAGE_MAP(CEditCtrl, CRichEditCtrl)
	//{{AFX_MSG_MAP(CEditCtrl)
	ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
	ON_WM_CREATE()
	ON_WM_VSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditCtrl message handlers

int CEditCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CRichEditCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	SetEventMask(GetEventMask() | ENM_CHANGE);
	ShowScrollBar(SB_VERT);	
	
	return 0;
}

void CEditCtrl::OnChange() 
{
	SCROLLINFO	scroll;

	scroll.cbSize = sizeof(scroll);
	scroll.fMask = SIF_DISABLENOSCROLL | SIF_RANGE;// | SIF_PAGE;
	scroll.nMin = 0;
	scroll.nMax = GetLineCount();
//	scroll.nPage = ;

	SetScrollInfo(SB_VERT,&scroll);
}


void CEditCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if (nSBCode == SB_THUMBTRACK)
	{
		LineScroll(GetFirstVisibleLine() - GetScrollPos(SB_VERT));
	}
	else
		SetScrollPos(SB_VERT,GetFirstVisibleLine());

	CRichEditCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
}
