#Include once "window9.bi"

ReDim Shared MdiClassName(0) As ZString*15

function ChildWndProc(hDlg as hwnd,Msg as uint,wparam as wparam,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
    MDISUB(GetParent(hdlg))
    return CallWindowProc(Cast(WNDPROC,GetFbGuiWinProc()),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=WS_CLIPCHILDREN Or WS_CLIPSIBLINGS  Or WS_VSCROLL Or WS_HSCROLL) As HWND Export
  Dim As CLIENTCREATESTRUCT ccs
  ccs.hWindowMenu  = H_menu
  ccs.idFirstChild = IDmenuMDI
  Dim As RECT rect
  GetClientRect(GETGURRENTAL,@rect)
  Var hMDIClient = CreateWindow("MDICLIENT", "",WS_CHILD Or WS_VISIBLE Or style, _
                                rect.left,rect.top,rect.right,rect.bottom, GETGURRENTAL,0, GetModuleHandle(0), @ccs)
  MDISUB(hMDIClient)
  UseGadgetList(hMDIClient)
  Return hMDIClient
End Function

Function MDIGadget(ByVal Name_ As String, _
                   ByVal x As Integer,ByVal y As Integer,ByVal width_ As Integer,ByVal height_ As Integer, _
                   ByVal Style As Integer=WS_OVERLAPPEDWINDOW) As HWND Export
  Static MCN As Integer
  Dim wc As wndclassex
  MCN+=1
  ReDim Preserve MdiClassName(MCN) As ZString*15
  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
  Return CreateWindowEx(WS_EX_MDICHILD,Cast(LPCTSTR,@MdiClassName(MCN)) , name_, WS_VISIBLE Or style, x,y,width_,height_,GETGURRENTAL(), 0, 0,0)
End Function


