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

MDIWindow::MDIWindow(void)
{
}

MDIWindow::~MDIWindow()
{
}

BOOL MDIWindow::createWindow(FrameWindow &frameWindow,const String &className,const String &windowName,const String &menuName,const String &iconName)
{
  HINSTANCE hInstance(processInstance());
	mFrameWindow=&frameWindow;
	mViewMenu=menuName;
	mClassName=className;
  registerClass(hInstance,className,menuName,iconName);
	return createWindow(hInstance,className,windowName,menuName);
}

BOOL MDIWindow::createWindow(HINSTANCE hInstance,const String &className,const String &windowName,const String &menuName)
{
	MDICREATESTRUCT createStruct;

	createStruct.szClass=(char*)(String&)className;
	createStruct.szTitle=(char*)(String&)windowName;
	createStruct.hOwner=hInstance;
	createStruct.x=CW_USEDEFAULT;
	createStruct.y=CW_USEDEFAULT;
	createStruct.cx=CW_USEDEFAULT;
	createStruct.cy=CW_USEDEFAULT;
  createStruct.style=WS_SYSMENU|WS_CAPTION|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
	createStruct.lParam=(LONG)this;
	preCreate(createStruct);
	if(!mFrameWindow->getClient().sendMessage(WM_MDICREATE,0,(LONG)(LPMDICREATESTRUCT)&createStruct))return FALSE;
  if(createStruct.style&WS_VISIBLE)show(SW_SHOW);
	update();
	return TRUE;
}

void MDIWindow::registerClass(HINSTANCE hInstance,const String &className,const String &menuName,const String &iconName)
{
	WNDCLASS wndClass;
	HICON hIcon;

  if(::GetClassInfo(hInstance,(char*)(String&)className,&wndClass))return;
	if(iconName.isNull())hIcon=::LoadIcon(NULL,IDI_APPLICATION);
	else hIcon=::LoadIcon(processInstance(),iconName);
	wndClass.style         =CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS|CS_OWNDC;
	wndClass.lpfnWndProc   =(WNDPROC)GUIWindow::MDIProc;
  wndClass.cbClsExtra    =0;                          
	wndClass.cbWndExtra    =sizeof(MDIWindow*);
	wndClass.hInstance     =hInstance;                  
	wndClass.hIcon         =hIcon;
	wndClass.hCursor       =::LoadCursor(NULL,IDC_ARROW);    
	wndClass.hbrBackground =(HBRUSH)::GetStockObject(WHITE_BRUSH);
	wndClass.lpszMenuName  =(char*)(String&)menuName;
	wndClass.lpszClassName =(char*)(String&)className;
	preRegister(wndClass);
	::RegisterClass(&wndClass);
}

void MDIWindow::mdiActivate(CallbackData &someCallbackData)
{
  if(!someCallbackData.lParam())mFrameWindow->getClient().sendMessage(WM_MDISETMENU,(WPARAM)(HMENU)mFrameWindow->getFrameMenu(),(LPARAM)0);
	else
	{
	  PureMenu subMenu;
   mViewMenu.getSubMenu(mViewMenu.menuItemStringPos("Window"),subMenu);    
	  mFrameWindow->getClient().sendMessage(WM_MDISETMENU,(WPARAM)(HMENU)mViewMenu,(LPARAM)(HMENU)subMenu);
	}
	mFrameWindow->sendMessage(WM_MDIACTIVATE,someCallbackData.wParam(),someCallbackData.lParam());
	mFrameWindow->drawMenuBar();
}

// *** virtuals

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

// *** virtuals

void MDIWindow::preRegister(WNDCLASS &/*wndClass*/)
{
  return;
}

void MDIWindow::preCreate(MDICREATESTRUCT &/*createStruct*/)
{
  return;
}
