#include once "window9.bi"

#ifdef UNICODE
	ReDim Shared MdiClassName(0) As WString*15
#else	
	ReDim Shared MdiClassName(0) As ZString*15
#EndIf

function ChildWndProc(byval hDlg as hwnd, byval Msg as uint, byval wparam as wparam, byval lparam as lparam) as integer
	select case msg
		Case WM_ACTIVATE,WM_SETFOCUS,WM_KILLFOCUS,WM_CLOSE,WM_NOTIFY, _
			WM_KEYDOWN,WM_KEYUP, _
			WM_COMMAND,WM_TIMER,WM_HSCROLL,WM_VSCROLL, _
			WM_MENUSELECT, _
			WM_MOUSEMOVE, _
			WM_LBUTTONDOWN,WM_LBUTTONUP,WM_LBUTTONDBLCLK, _
			WM_RBUTTONDOWN,WM_RBUTTONUP,WM_RBUTTONDBLCLK, _
			WM_MBUTTONDOWN,WM_MBUTTONUP,WM_MBUTTONDBLCLK, _
			WM_MOUSEWHEEL, _
			WM_HOTKEY,WM_USER,WM_PARENTNOTIFY,WM_CTLCOLORLISTBOX,WM_PAINT,WM_CTLCOLORSTATIC,WM_VKEYTOITEM
			win9SetMDIClient(GetParent(hdlg))
			return CallWindowProc(Cast(WNDPROC,win9GetGuiWinProc()),hdlg,msg,wparam,lparam)
	end select
	return DefMDIChildProc(hDlg, Msg, wParam, lParam)
End Function

Function ClientMDIGadget(ByVal H_menu As HMENU, ByVal IDmenuMDI As Integer, ByVal Style  As Integer) As HWND Export
	Dim As CLIENTCREATESTRUCT ccs
	ccs.hWindowMenu  = H_menu
	ccs.idFirstChild = IDmenuMDI
	Dim As RECT rect
	GetClientRect(win9GetCurrent,@rect)
	Var hMDIClient = CreateWindow("MDICLIENT", "",WS_CHILD Or WS_VISIBLE Or style, _
	rect.left,rect.top,rect.right,rect.bottom, win9GetCurrent,0, GetModuleHandle(0), @ccs)
	win9SetMDIClient(hMDIClient)
	UseGadgetList(hMDIClient)
	Return hMDIClient
End Function

Function MDIGadget(ByRef sName As USTRING, _
					ByVal x As Integer, ByVal y As Integer, _
					ByVal w As Integer, ByVal h As Integer, _
					ByVal Style As Integer) As HWND Export
	Static MCN As Integer
	Dim wc As wndclassex
	MCN+=1
	#ifdef UNICODE
		ReDim Preserve MdiClassName(MCN) As WString*15
	#else	
		ReDim Preserve MdiClassName(MCN) As ZString*15
	#EndIf	
	MdiClassName(MCN)="ClassMDI_"&MCN
	With wc
		.cbSize        = SIZEOF( WNDCLASSEX )
		.style         = CS_HREDRAW or CS_VREDRAW
		.lpfnWndProc   = @ChildWndProc
		.hInstance     = GetModuleHandle(0)
		.hbrBackground = Cast(HBRUSH,16)
		.lpszClassName = Cast(LPCTSTR,@MdiClassName(MCN))
		.hIcon         = LoadIcon( NULL,IDI_APPLICATION )
		.hIconSm       = .hIcon
		.hCursor       = LoadCursor( NULL,IDC_ARROW)
	end With
	if(RegisterClassEx(@wc) = FALSE) then
		MessageBox(0,"Can not register window class","Error",0)
		end 1
	end If
	#ifdef UNICODE
		Return CreateWindowEx(WS_EX_MDICHILD,@MdiClassName(MCN) , *sName, WS_VISIBLE Or style, x,y,w,h, win9GetCurrent(), 0, 0,0)
	#else	
		Return CreateWindowEx(WS_EX_MDICHILD,Cast(LPCTSTR,@MdiClassName(MCN)) , sName, WS_VISIBLE Or style, x,y,w,h, win9GetCurrent(), 0, 0,0)
	#EndIf
End Function


