#include <common/dwindow.hpp>
#include <common/string.hpp>
#include <common/mmsystem.hpp>
#include <common/instance.hpp>
#include <common/dde.hpp>

DWindow::~DWindow()
{
}

bool DWindow::createDialogParam(GUIWindow &frameWindow,GUIWindow &parentWindow,const String &strTemplate,LPARAM param)
{
  if(isValid()||!frameWindow.isValid()||!parentWindow.isValid()||strTemplate.isNull())return false;
	mFrameWindow=&frameWindow;
	mFrameWindow.disposition(PointerDisposition::Assume);
	setHandle(::CreateDialogParam(parentWindow.processInstance(),(LPSTR)(String&)strTemplate,parentWindow,DWindow::DlgProc,param));
	if(!isValid())return false;
	mFrameWindow->insertModelessDialog(*this);
	return true;
}

bool DWindow::createDialogParam(GUIWindow &parentWindow,const String &strTemplate,LPARAM param)
{
  if(isValid()||!parentWindow.isValid()||strTemplate.isNull())return false;
	mFrameWindow=&parentWindow;
	mFrameWindow.disposition(PointerDisposition::Assume);
	setHandle(::CreateDialogParam(parentWindow.processInstance(),(LPSTR)(String&)strTemplate,parentWindow,DWindow::DlgProc,param));
	if(!isValid())return false;
	mFrameWindow->insertModelessDialog(*this);
	return true;
}

String DWindow::getText(UINT controlID)const
{
  String strText;
	getText(controlID,strText);
	return strText;
}

WORD DWindow::getText(UINT controlID,String &textString)const
{
	WORD returnCode(FALSE);
	String workString;

	if(!isValid())return returnCode;
	returnCode=::GetDlgItemText(*this,controlID,workString,String::MaxString);
	textString=workString;
  return returnCode;
}

void DWindow::setText(UINT controlID,const String &setText)const
{
	if(!isValid())return;
	::SetDlgItemText(*this,controlID,(LPCTSTR)(LPSTR)((String&)setText));
}

BOOL DWindow::setInt(UINT controlID,int value)const
{
  if(!isValid())return FALSE;
	return ::SetDlgItemInt(*this,controlID,value,TRUE);
}

BOOL DWindow::getInt(UINT controlID,int &value)const
{
  BOOL returnCode(FALSE);

	if(!isValid())return returnCode;
	value=::GetDlgItemInt(*this,controlID,&returnCode,TRUE);
	return returnCode;
}

int DWindow::windowProcedure(HWND hWnd,WORD message,WPARAM wParam,LPARAM lParam)
{
  if(message>=WM_USER&&message<=0x7FFF)return (int)callHandlers(VectorHandler::UserHandler,CallbackData(message,lParam,hWnd));
	switch(message)
	{
	  case WM_CLOSE :
		  return (int)callHandlers(VectorHandler::CloseHandler,CallbackData(wParam,lParam,hWnd));
		case WM_TIMER :
			return (int)callHandlers(VectorHandler::TimerHandler,CallbackData(wParam,lParam,hWnd));
    case WM_CHANGECBCHAIN :
		  return (int)callHandlers(VectorHandler::ChangeCBChainHandler,CallbackData(wParam,lParam,hWnd));
		case WM_DRAWCLIPBOARD :
		  return (int)callHandlers(VectorHandler::DrawClipboardHandler,CallbackData(wParam,lParam,hWnd));
		case WM_COMMAND :
			return callHandlers(VectorHandler::CommandHandler,CallbackData(wParam,lParam,hWnd));
		case WM_COMPACTING :
			return callHandlers(VectorHandler::CompactingHandler,CallbackData(wParam,lParam,hWnd));
		case WM_ERASEBKGND :
		  return callHandlers(VectorHandler::EraseBackgroundHandler,CallbackData(wParam,lParam,hWnd));
		case WM_WININICHANGE :
			return callHandlers(VectorHandler::WinIniChangeHandler,CallbackData(wParam,lParam,hWnd));
		case WM_SYSCOLORCHANGE :
			return callHandlers(VectorHandler::SysColorChangeHandler,CallbackData(wParam,lParam,hWnd));
		case WM_LBUTTONDBLCLK :
			return callHandlers(VectorHandler::LeftButtonDoubleHandler,CallbackData(wParam,lParam,hWnd));
		case WM_LBUTTONDOWN :
			return callHandlers(VectorHandler::LeftButtonDownHandler,CallbackData(wParam,lParam,hWnd));
		case WM_LBUTTONUP :
			return callHandlers(VectorHandler::LeftButtonUpHandler,CallbackData(wParam,lParam,hWnd));
		case WM_RBUTTONDBLCLK :
			return callHandlers(VectorHandler::RightButtonDoubleHandler,CallbackData(wParam,lParam,hWnd));
		case WM_RBUTTONDOWN :
			return callHandlers(VectorHandler::RightButtonDownHandler,CallbackData(wParam,lParam,hWnd));
		case WM_RBUTTONUP :
			return callHandlers(VectorHandler::RightButtonUpHandler,CallbackData(wParam,lParam,hWnd));
		case WM_NCLBUTTONUP :
			return callHandlers(VectorHandler::NCLeftButtonUpHandler,CallbackData(wParam,lParam,hWnd));
		case WM_NCLBUTTONDOWN :
			return callHandlers(VectorHandler::NCLeftButtonDownHandler,CallbackData(wParam,lParam,hWnd));
		case WM_NCRBUTTONUP :
			return callHandlers(VectorHandler::NCRightButtonUpHandler,CallbackData(wParam,lParam,hWnd));
		case WM_NCRBUTTONDOWN :
			return callHandlers(VectorHandler::NCRightButtonDownHandler,CallbackData(wParam,lParam,hWnd));
		case WM_SETFOCUS :
			return callHandlers(VectorHandler::SetFocusHandler,CallbackData(wParam,lParam,hWnd));
		case WM_SETFONT :
			return callHandlers(VectorHandler::SetFontHandler,CallbackData(wParam,lParam,hWnd));
		case WM_KILLFOCUS :
			return callHandlers(VectorHandler::KillFocusHandler,CallbackData(wParam,lParam,hWnd));
		case WM_CTLCOLOR :
			return callHandlers(VectorHandler::ControlColorHandler,CallbackData(wParam,lParam,hWnd));
		case WM_CTLCOLORMSGBOX :
			return callHandlers(VectorHandler::ControlColorHandler,CallbackData(wParam,lParam,hWnd));
		case WM_CTLCOLOREDIT :
			return callHandlers(VectorHandler::ControlColorHandler,CallbackData(wParam,lParam,hWnd));
		case WM_CTLCOLORLISTBOX :
			return callHandlers(VectorHandler::ControlColorHandler,CallbackData(wParam,lParam,hWnd));
		case WM_CTLCOLORBTN :
			return callHandlers(VectorHandler::ControlColorHandler,CallbackData(wParam,lParam,hWnd));
		case WM_CTLCOLORDLG :
			return callHandlers(VectorHandler::ControlColorHandler,CallbackData(wParam,lParam,hWnd));
		case WM_CTLCOLORSCROLLBAR :
			return callHandlers(VectorHandler::ControlColorHandler,CallbackData(wParam,lParam,hWnd));
		case WM_CTLCOLORSTATIC :
			return callHandlers(VectorHandler::ControlColorHandler,CallbackData(wParam,lParam,hWnd));
		case WM_KEYUP :
			return callHandlers(VectorHandler::KeyUpHandler,CallbackData(wParam,lParam,hWnd));
		case WM_KEYDOWN :
			return callHandlers(VectorHandler::KeyDownHandler,CallbackData(wParam,lParam,hWnd));
		case WM_GETMINMAXINFO :
			return callHandlers(VectorHandler::MinMaxHandler,CallbackData(wParam,lParam,hWnd));
		case WM_ENTERIDLE :
			return callHandlers(VectorHandler::EnterIdleHandler,CallbackData(wParam,lParam,hWnd));
		case WM_MENUSELECT :
			return callHandlers(VectorHandler::MenuSelectHandler,CallbackData(wParam,lParam,hWnd));
		case WM_ACTIVATEAPP :
		  return callHandlers(VectorHandler::ActivateAppHandler,CallbackData(wParam,lParam,hWnd));
		case WM_DISPLAYCHANGE :
			return callHandlers(VectorHandler::DisplayChangeHandler,CallbackData(wParam,lParam,hWnd));
#if defined(__FLAT__)
		case WM_NOTIFY :
			return callHandlers(VectorHandler::NotifyHandler,CallbackData(wParam,lParam,hWnd));
#endif
		case WM_SIZE :
			return callHandlers(VectorHandler::SizeHandler,CallbackData(wParam,lParam,hWnd));
		case WM_PAINT :
			return handlePaintMessage(hWnd,message,wParam,lParam);
		case WM_DRAWITEM :
			return callHandlers(VectorHandler::DrawItemHandler,CallbackData(wParam,lParam,hWnd));
		case WM_CHAR :
			return callHandlers(VectorHandler::CharHandler,CallbackData(wParam,lParam,hWnd));
		case WM_DDE_INITIATE :
			return callHandlers(VectorHandler::DDEInitiateHandler,CallbackData(wParam,lParam,hWnd));
		case WM_DDE_ACK :
			return callHandlers(VectorHandler::DDEAckHandler,CallbackData(wParam,lParam,hWnd));
		case WM_DDE_REQUEST :
			return callHandlers(VectorHandler::DDERequestHandler,CallbackData(wParam,lParam,hWnd));
		case WM_DDE_TERMINATE :
			return callHandlers(VectorHandler::DDETerminateHandler,CallbackData(wParam,lParam,hWnd));
		case WM_DDE_DATA :
			return callHandlers(VectorHandler::DDEDataHandler,CallbackData(wParam,lParam,hWnd));
		case MM_WOM_OPEN :
			return callHandlers(VectorHandler::MMOpenHandler,CallbackData(wParam,lParam,hWnd));
		case MM_WOM_CLOSE :
			return callHandlers(VectorHandler::MMCloseHandler,CallbackData(wParam,lParam,hWnd));
		case MM_WOM_DONE :
			return callHandlers(VectorHandler::MMDoneHandler,CallbackData(wParam,lParam,hWnd));
		case WM_MOUSEMOVE :
			return callHandlers(VectorHandler::MouseMoveHandler,CallbackData(wParam,lParam,hWnd));
		case WM_GETDLGCODE :
			return callHandlers(VectorHandler::DialogCodeHandler,CallbackData(wParam,lParam,hWnd));
		case WM_MEASUREITEM :
			return callHandlers(VectorHandler::MeasureItemHandler,CallbackData(wParam,lParam,hWnd));
		case WM_HSCROLL :
		  return callHandlers(VectorHandler::HorizontalScrollHandler,CallbackData(wParam,lParam,hWnd));
	  case WM_VSCROLL :
		  return callHandlers(VectorHandler::VerticalScrollHandler,CallbackData(wParam,lParam,hWnd));
		case WM_DROPFILES :
		  return callHandlers(VectorHandler::DropFilesHandler,CallbackData(wParam,lParam,hWnd));
		case WM_MDIACTIVATE :
		  return callHandlers(VectorHandler::MDIActivateHandler,CallbackData(wParam,lParam,hWnd));
		case WM_SHOWWINDOW :
		  return callHandlers(VectorHandler::ShowWindowHandler,CallbackData(wParam,lParam,hWnd));
		case WM_CAPTURECHANGED :
		  return callHandlers(VectorHandler::CaptureChangedHandler,CallbackData(wParam,lParam,hWnd));
	  case WM_DEVICECHANGE :
		  return callHandlers(VectorHandler::DeviceChangeHandler,CallbackData(wParam,lParam,hWnd));
		case WM_DESTROY :
			callHandlers(VectorHandler::DestroyHandler,CallbackData(wParam,lParam,hWnd));
			InstanceData::removeInstanceData(hWnd);
			if(mFrameWindow.isOkay())mFrameWindow->removeModelessDialog(*this);
			setHandle(0);
			return FALSE;
		case WM_NCDESTROY :
		default :
      return FALSE;
	}
}
