/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef _MSC_VER
#pragma warning (disable: 4786)
#endif

#define STRICT
#include <Windows.h>
#include <string>
#include <map>
#include <list>
#include <vector>
#include <boost/shared_ptr.hpp>
#include "WinOnly.h"
#include "WinBe.h"
#include "NativeView.h"
#include "NativeWindow.h"
#include "NativeApplication.h"


using namespace winBe;


// constructor
NativeWindow::NativeWindow (BWindow* window) :
hWnd (0)
{
	this->window = window;
}


// destructor
NativeWindow::~NativeWindow ()
{
	//winBe::debugMessage ("destructing NativeWindow");
	destroy ();
}


// destroy
void NativeWindow::destroy ()
{
	//if (::IsWindow (hWnd))
		//::DestroyWindow (hWnd);

	NativeApplication::instance ().deleteWindow (window);
}


// getClientRect
RECT NativeWindow::getClientRect () const
{
	RECT rect;
	::GetClientRect (hWnd, &rect);
	return rect;
}


// createWindow
void NativeWindow::createWindow (int left, int top, int right, int bottom)
{
	hWnd = ::CreateWindow ("BWindow", "BWindow title", WS_OVERLAPPEDWINDOW, left, top, right - left, bottom - top, NULL, NULL, NativeApplication::instance ().getHINSTANCE (), window);
	::ShowWindow (hWnd, SW_SHOW);
}


// createView
NativeView* NativeWindow::createView (BView* view, const char* name, int left, int top, int right, int bottom, NativeView* parentView)
{
	NativeView* nativeView = NativeApplication::instance ().addView (view, this);

	HWND hParentWnd = getHWND ();
	if (parentView)
		hParentWnd = parentView->getHWND ();

	nativeView->createWindow (name, left, top, right, bottom, hParentWnd);
	return nativeView;
}


RECT NativeWindow::getRect () const
{
	RECT rect;
	::GetWindowRect (hWnd, &rect);
	return rect;
}


void NativeWindow::setRect (const RECT& rect)
{
	::MoveWindow (hWnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, TRUE);
}


// getBWindow
BWindow* NativeWindow::getBWindow (HWND hWnd)
{
	return reinterpret_cast<BWindow*>(GetWindowLong (hWnd, 0));
}


// NativeWindowWndProc
LRESULT CALLBACK winBe::NativeWindowWndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	LRESULT result = 0;
	bool processed = true;

	BWindow* window = NativeWindow::getBWindow (hWnd);

	switch (message) {

		case WM_CLOSE:
			if (winBe::callWindowQuitRequested (window))
				DestroyWindow (hWnd);
			break;

		case WM_CREATE:
		{
			CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lParam);
			SetWindowLong (hWnd, 0, reinterpret_cast<LONG>(cs->lpCreateParams));
			window = NativeWindow::getBWindow (hWnd);
			break;
		}

		case WM_ERASEBKGND:
			result = 1;
			break;

		default:
			processed = false;
	}

	if (!processed)
		result = ::DefWindowProc (hWnd, message, wParam, lParam);

	return result;
}
