#include once "window9.bi"

Dim Shared As OPENFILENAME g_ofn

#ifdef UNICODE
    Dim shared As WString * 1000000 g_filename_1000000  	
#else	
    Dim shared As zString * 1000000 g_filename_1000000  	
#EndIf

dim shared iAddress As Integer
dim shared bFlagEmptyAddress As Byte
dim shared bFlagSkip As Byte

Function OpenFileRequester(ByRef Title As USTRING, _
                        ByRef curentdir As USTRING, _
                        ByRef sPattern As USTRING, _
                        ByVal flag As Integer, _
                        ByRef templateName As USTRING,_
                        ByVal hParentWin as HWND = 0) As USTRING Export
    dim as HWND hwFocus = GetFocus()
    iAddress=0
    bFlagEmptyAddress=0
    bFlagSkip=0
    Dim fbguiTemp As Integer
    dim as USTRING sRet
    g_filename_1000000 = templateName
    #ifdef UNICODE
        dim as Long iLen = len(sPattern)	
        dim as UByte ptr pPatternDest = callocate((iLen+2)*2)
        if pPatternDest = 0 then return sRet
        dim iIndex as Long	
        dim as UByte ptr pPatternString = cast(Byte ptr , *sPattern)	
        for i as Long = 0 to iLen*2		
            if pPatternString[i] = &h7C then			
                pPatternString[i] = 0			
            EndIf		
            pPatternDest[iIndex] = pPatternString[i]		
            iIndex +=1		
        Next
    #EndIf
    
    With g_ofn
        .hwndOwner       = hParentWin
        .nFileOffset     = 0
        .Flags           = OFN_EXPLORER Or OFN_FILEMUSTEXIST Or OFN_OVERWRITEPROMPT Or flag
        .lStructSize     = SizeOf(OPENFILENAME)
        .nMaxFile        = SizeOf(g_filename_1000000)
        #ifdef UNICODE
            .lpstrFilter     = cast(LPCWSTR,pPatternDest)
            .lpstrFile       = @g_filename_1000000
            .lpstrInitialDir = *curentdir
            .lpstrTitle      = *Title
        #else	
            .lpstrFilter     = Cast(LPCTSTR,StrPtr(sPattern) )
            .lpstrFile       = StrPtr(g_filename_1000000)
            .lpstrInitialDir = Cast(LPCTSTR,StrPtr(curentdir))
            .lpstrTitle      = Cast(LPCTSTR,StrPtr(Title))			
        #EndIf		
    End With
    
    if( GetOpenFileName( @g_ofn ) = 0 ) then
    Else
        sRet = g_filename_1000000
    end If
    #ifdef UNICODE		
        if pPatternDest then deallocate(pPatternDest)		
    #EndIf
    SetFocus(hwFocus)
    Return sRet
End Function

Function NextSelectedFilename() As USTRING Export
    dim as USTRING sRet
    If g_ofn.nFileOffset< Len(g_filename_1000000) Then
        Static bFlag As Byte
        If bFlag=0 Then
            bFlag=1
            sRet = g_filename_1000000
            Return sRet
        Else
            bFlag=0
            g_filename_1000000=""
            sRet = ""
            Return sRet
        EndIf
    Else
        If bFlagEmptyAddress=0  Then
            If bFlagSkip=0 Then
                iAddress = g_ofn.nFileOffset
                #ifdef UNICODE
                    iAddress = Cast(Integer,@g_filename_1000000) + iAddress*2
                #else	 
                    iAddress = Cast(Integer,@g_filename_1000000) + iAddress
                #EndIf			
            EndIf			
            dim sTemp As USTRING = PeekS(Cast(Any ptr,iAddress))
            iAddress=iAddress+Len(sTemp)+1
            If Peek(iAddress) = 0 Then bFlagEmptyAddress=1
            bFlagSkip=1
            If Right(g_filename_1000000,1)<>"\" Then
                sRet = g_filename_1000000 & "\" & sTemp
                Return sRet
            EndIf
            sRet = g_filename_1000000  & sTemp
        Else
            iAddress=0
            bFlagEmptyAddress=0
            bFlagSkip=0
            g_filename_1000000=""
            sRet = ""		
        endif		
        Return sRet
    EndIf
End Function
