/* Anakrino - .NET Reflection Browser
 * Copyright (C) 2001-2002  Jay Freeman (saurik)
*/

/*
 *        Redistribution and use in source and binary
 * forms, with or without modification, are permitted
 * provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the
 *    above copyright notice, this list of conditions
 *    and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions
 *    and the following disclaimer in the documentation
 *    and/or other materials provided with the
 *    distribution.
 * 3. The name of the author may not be used to endorse
 *    or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "stdafx.h"

#include "menes-com/IRegistrar.h"
#include "menes-com/Factory.h"

#include "CallGraphViewer.h"
#include "DecompiledViewer.h"
#include "DecompilerTool.h"
#include "DocumentationViewer.h"
#include "HeaderViewer.h"
#include "MasterBinder.h"
#include "ReflectionBrowser.h"
#include "SearchBrowser.h"
//#include "TachyonViewer.h"

#if _MSC_VER >= 1300 // As Jason would say:  Fucking Moses!
namespace WTL { CTraceCategory atlTraceUI(L"atlTraceUI"); }
#endif // _MSC_VER >= 1300

CAppModule _Module;

int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
	CMessageLoop theLoop;
	_Module.AddMessageLoop(&theLoop);

	Diapexis::Handle<Anakrino::MasterBinder> wndMain(_idof(Anakrino::MasterBinder));
	if(wndMain->CreateEx() == NULL) {
		ATLTRACE(_T("Main window creation failed!\n"));
		return 0;
	}

	wndMain->ShowWindow(nCmdShow);

	int nRet = theLoop.Run();

	_Module.RemoveMessageLoop();
	return nRet;
}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpstrCmdLine, int nCmdShow)
{
	//HRESULT hRes = ::CoInitialize(NULL);
	//HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
	//ATLASSERT(SUCCEEDED(hRes));

	ManualOverride.SetName("Anakrino");
	Diapexis::PexInitialize();
	Anakrino::Pexify(&ManualOverride);

	XMLPlatformUtils::Initialize();

#if (_WIN32_IE >= 0x0300)
	INITCOMMONCONTROLSEX iccx;
	iccx.dwSize = sizeof(iccx);
	iccx.dwICC = ICC_COOL_CLASSES | ICC_BAR_CLASSES;
	BOOL bRet = ::InitCommonControlsEx(&iccx);
	bRet;
	ATLASSERT(bRet);
#else
	::InitCommonControls();
#endif

	HRESULT hRes = _Module.Init(NULL, hInstance, &LIBID_ATLLib);
	ATLASSERT(SUCCEEDED(hRes));

	/* // This loads the RichTextBox library.
	HINSTANCE hInstRich = ::LoadLibrary(CRichEditCtrl::GetLibraryName());
	ATLASSERT(hInstRich != NULL);*/

	AtlAxWinInit();

	int nRet = Run(lpstrCmdLine, nCmdShow);

	/* // This unloads the RichTextBox library.
	::FreeLibrary(hInstRich);*/

	XMLPlatformUtils::Terminate();

	_Module.Term();
	//::CoUninitialize();
	Diapexis::PexTerminate();

	return nRet;
}

namespace Anakrino {

void Pexify(Diapexis::IRegistrar *registrar) {
	std::wstring name = _nameof(MasterBinder);
	clsid_t id = _idof(MasterBinder);

	registrar->Register(name, id, new Diapexis::SimpleFactory<MasterBinder>);

	#define Register(Op) registrar->Register(L"Anakrino." L ## #Op, _idof(Op), new Diapexis::SimpleFactory<Op>);

    Register(CallGraphViewer)
	Register(DecompiledViewer)
	Register(DecompilerTool)
	Register(DocumentationViewer)
	Register(HeaderViewer)
	Register(MasterBinder)
	Register(ReflectionBrowser)
	Register(SearchBrowser)
	//Register(TachyonViewer)
}

}