Attribute VB_Name = "modApiDeclares"
Option Explicit

Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_CREATEPROMPT = &H2000

Private Type OPENFILENAME
    lStructSize As Long
    hWndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Private Declare Function GetSaveFileNameA Lib "comdlg32.dll" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetTempPathA Lib "kernel32" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function GetTempFileNameA Lib "kernel32" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Public Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Public Declare Function RemoveDirectory Lib "kernel32" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long

Public Function FileSaveDialog(ByVal hWndOwner As Long, ByVal Filter As String, ByVal Title As String, ByVal InitDir As String, ByVal strFileDefault As String) As String
    Dim ofn As OPENFILENAME, i As Long, sFile As String, sExt As String

    ofn.lpstrFile = strFileDefault & Space$(254 - Len(strFileDefault))
    ofn.lStructSize = Len(ofn)
    ofn.hWndOwner = hWndOwner
    ofn.hInstance = App.hInstance
    ofn.nMaxFile = 255
    ofn.lpstrFileTitle = Space$(254)
    ofn.nMaxFileTitle = 255
    ofn.lpstrInitialDir = InitDir
    ofn.lpstrTitle = Title
    ofn.flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT Or OFN_CREATEPROMPT
    ofn.lpstrFilter = FilterForDialog(Filter)
    ofn.nFilterIndex = 1
    i = GetSaveFileNameA(ofn)

    If (i) Then
        i = InStr(ofn.lpstrFile, Chr(0))
        If i > 0 Then
            sFile = Trim$(Left$(ofn.lpstrFile, i - 1))
        Else
            sFile = Trim$(ofn.lpstrFile)
        End If
        Do While Right$(sFile, 1) = "."
            sFile = Left$(sFile, Len(sFile) - 1)
        Loop
        If Len(sFile) > 0 Then
            sExt = LCase$(Right$(sFile, 4))
            Select Case ofn.nFilterIndex
                Case 1  ' PNG
                    If sExt <> ".png" Then sFile = sFile & ".png"
                Case 2  ' JPG
                    If sExt <> ".jpg" Then sFile = sFile & ".jpg"
            End Select
        End If
    End If
    FileSaveDialog = sFile
End Function

Private Function FilterForDialog(ByVal strFilter As String) As String
    Dim i As Integer, j As Integer
    strFilter = Trim$(strFilter)
    If Len(strFilter) = 0 Then Exit Function
    For i = Len(strFilter) To 0 Step -1
        If Mid$(strFilter, i, 1) <> "|" Then Exit For
    Next i
    strFilter = Left$(strFilter, i)
    If Len(strFilter) = 0 Then Exit Function
    i = 0
    Do
        i = InStr(i + 1, strFilter, "|"): If i = 0 Then Exit Do
        If i > 0 Then Mid$(strFilter, i, 1) = Chr(0)
    Loop
    strFilter = strFilter & Chr(0) & Chr(0)
    FilterForDialog = strFilter
End Function

Public Function GetTempFolderName() As String
    Dim s As String, i As Integer
    s = Space(260)
    i = GetTempFileNameA(GetTempPath, "~si", 0, s)
    s = Left$(s, InStr(1, s, Chr$(0)) - 1)
    DeleteFile s
    MkDir s
    GetTempFolderName = s & "\"
End Function

Public Function GetTempPath() As String
    Dim s As String, i As Integer
    s = Space(100)
    i = GetTempPathA(100, s)
    GetTempPath = Left$(s, i)
End Function
