#Include once "window9.bi"

Dim Shared ofn  As OPENFILENAME
Dim shared filename99914352566474_        As ZString * 1000000

Function OpenFileRequester Alias "OpenFileRequester"(ByVal Title As String,ByVal curentdir As String, ByVal Pattern As String,ByVal flag As Integer=0, ByVal templateName As String = "") As String Export
  Dim fbguiTemp As Integer
  filename99914352566474_ = templateName

  With ofn
    .hwndOwner       = 0
    .lStructSize     = SizeOf(OPENFILENAME)
    .lpstrFilter     = Cast(LPCTSTR,StrPtr(Pattern) )
    .lpstrFile       = StrPtr(filename99914352566474_)
    .nFileOffset     = 0
    .nMaxFile        = SizeOf(filename99914352566474_ )
    .lpstrFileTitle  = Cast(LPTSTR,StrPtr(Title))
    .nMaxFileTitle   = SizeOf(Title)
    .lpstrInitialDir = Cast(LPCTSTR,StrPtr(curentdir))
    .lpstrTitle      = Cast(LPCTSTR,StrPtr(Title))
    .Flags           = OFN_EXPLORER Or OFN_FILEMUSTEXIST Or OFN_OVERWRITEPROMPT Or flag
  End With

  if( GetOpenFileName( @ofn ) = 0 ) then
    Return ""
  Else
    Return filename99914352566474_
  end If
End Function

Function NextSelectedFilename Alias "NextSelectedFilename"() As String Export
  Static e As Byte
  If ofn.nFileOffset< Len(filename99914352566474_) Then
    If e=0 Then
      e=1
      Return filename99914352566474_
    Else
      e=0
      filename99914352566474_=""
      Return ""
    EndIf
  Else
    Static a As Integer
    Static b As Byte
    Static c As Byte
    If b=0  Then
      If c=0 Then
        a=ofn.nFileOffset
        a=Cast(Integer,@filename99914352566474_) + a
      EndIf

      dim aa As string=PeekS(Cast(Any ptr,a))
      'debug aa
      a=a+Len(aa)+1
      If Peek(a) = 0 Then b=1
      c=1
      If Right(filename99914352566474_,1)<>"\" Then
        Return filename99914352566474_ & "\" & aa  
      EndIf
      Return filename99914352566474_  & aa  
    Else
      a=0
      b=0
      c=0
      filename99914352566474_=""
      Return ""
    endif
  EndIf
End Function
