#include <common/window.hpp>
#include <common/instance.hpp>
#include <common/dde.hpp>
#include <common/mmsystem.hpp>

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