
'no case sensitivity

#ifndef HAS_COREWIN
  uses corewin
#endif

% OFN_READONLY                 0x00000001
% OFN_OVERWRITEPROMPT          0x00000002
% OFN_HIDEREADONLY             0x00000004
% OFN_NOCHANGEDIR              0x00000008
% OFN_SHOWHELP                 0x00000010
% OFN_ENABLEHOOK               0x00000020
% OFN_ENABLETEMPLATE           0x00000040
% OFN_ENABLETEMPLATEHANDLE     0x00000080
% OFN_NOVALIDATE               0x00000100
% OFN_ALLOWMULTISELECT         0x00000200
% OFN_EXTENSIONDIFFERENT       0x00000400
% OFN_PATHMUSTEXIST            0x00000800
% OFN_FILEMUSTEXIST            0x00001000
% OFN_CREATEPROMPT             0x00002000
% OFN_SHAREAWARE               0x00004000
% OFN_NOREADONLYRETURN         0x00008000
% OFN_NOTESTFILECREATE         0x00010000
% OFN_NONETWORKBUTTON          0x00020000
% OFN_NOLONGNAMES              0x00040000     '// force no long names for 4.x modules
% OFN_EXPLORER                 0x00080000     '// new look commdlg
% OFN_NODEREFERENCELINKS       0x00100000
% OFN_LONGNAMES                0x00200000     '// force long names for 3.x modules
% OFN_ENABLEINCLUDENOTIFY      0x00400000     '// send include message to callback
% OFN_ENABLESIZING             0x00800000
% OFN_DONTADDTORECENT          0x02000000
% OFN_FORCESHOWHIDDEN          0x10000000     '// Show All files including System and hidden files


type OPENFILENAMEA
  dword  lStructSize
  sys    hwndOwner
  sys    hInstance
  char*  lpstrFilter
  char*  lpstrCustomFilter
  dword  nMaxCustFilter
  dword  nFilterIndex
  char*  lpstrFile
  dword  nMaxFile
  char*  lpstrFileTitle
  dword  nMaxFileTitle
  char*  lpstrInitialDir
  char*  lpstrTitle
  dword  Flags
  word   nFileOffset
  word   nFileExtension
  char*  lpstrDefExt
  long   lCustData
  sys    lpfnHook
  char*  lpTemplateName
end type

typedef OPENFILENAMEA OPENFILENAME

Declare GetModuleHandle      lib "kernel32.dll" alias "GetModuleHandleA" (optional char*n) as sys
Declare GetOpenFileName      Lib "comdlg32.dll" Alias "GetOpenFileNameA" (OPENFILENAME*opfn) As sys
Declare GetSaveFileName      Lib "comdlg32.dll" Alias "GetSaveFileNameA" (OPENFILENAME*opfn) As sys
Declare CommDlgExtendedError Lib "comdlg32.dll" () as dword


'FileDialog( $ iDir , $ filter ,$ title , % parent , flag )


Function FileDialog(string iDir,filter,Title,Name, sys Hwnd, Flags, a) As String
'===============================================================================

def FileNameLen 256

OPENFILENAME ofn
char         FileName[FileNameLen]
if Name then FileName=Name
int          retval	

 ofn.lStructSize       = sizeof(OPENFILENAME)
 ofn.hwndOwner         = hWnd
 ofn.hInstance         = GetModuleHandle
 ofn.lpstrFilter       = filter
 ofn.lpstrCustomFilter = null
 ofn.nMaxCustFilter    = 0
 ofn.nFilterIndex      = 1
 ofn.lpstrFile         = FileName 'coupling to char buffer
 ofn.nMaxFile          = FileNameLen
 ofn.lpstrFileTitle    = null
 ofn.nMaxFileTitle     = 0
 ofn.lpstrInitialDir   = idir
 ofn.lpstrTitle        = title
 ofn.Flags             = flags
 ofn.nFileOffset       = 0
 ofn.nFileExtension    = 0
 ofn.lpstrDefExt       = null
 ofn.lCustData         = 0
 ofn.lpfnHook          = 0
 ofn.lpTemplateName    = null


sys retval
'
if a then
 retval=GetSaveFileName(ofn)
else
 retval=GetOpenFileName(ofn)
end if
'
'http://msdn.microsoft.com/en-us/library/windows/desktop/ms646916(v=vs.85).aspx

if retval
  return filename
end if

End Function


Function GetFileName(string name, sys a, string filt="") as string
'=================================================================
'
'dir="OxygenBasic\demos\GUI"
'
sys    hwnd
string filter,dir,title
'
if filt then
  filter=filt
else
  string sep=chr(0)
  filter=
  "all files"+sep+"*.*"+sep+
  "text"+sep+"*.txt"+sep+
  "basic"+sep+"*.bas;*.o2bas"+sep+
  "include"+sep+"*.inc"+sep+
  "header"+sep+"*.h"+sep+
  sep
end if
'
if a then
  title="Save File as"
else
  title="Load File"
end if
'
sys flags = OFN_EXPLORER or OFN_OVERWRITEPROMPT or OFN_HIDEREADONLY
'
return FileDialog(dir,filter,title,name,hwnd,flags,a)
'
end function



'! SHBrowseForFolder Lib "shell32" (BrowseInfo *lpbi) as sys
'! SHGetPathFromIDList Lib "shell32" (sys pidList, sys lpBuffer) As sys



function BrowseCallbackProc(sys hwnd, int uMsg, sys wParam, lParam) as int, callback
====================================================================================
'Used with the BrowseForFolder function to select a default folder
  % BFFM_ENABLEOK       0x465
  % BFFM_SETSELECTION   0x466
  % BFFM_SETSTATUSTEXT  0x464
  % BFFM_INITIALIZED    1
  % BFFM_SELCHANGED     2
  % BFFM_VALIDATEFAILED 3
  '
  if uMsg = BFFM_INITIALIZED
    SendMessage(hwnd, BFFM_SETSELECTION, uMsg, lParam )
  endif
  return 0  ' the function should always return 0
end function


type BrowseInfo
  sys hWndOwner
  sys pIDLRoot
  sys pszDisplayName
  sys lpszTitle
  int ulFlags
  sys lpfnCallback
  sys lParam
  int iImage
end type
'
function BrowseForDir(sys hWnd,  string strTitle="", strInitDir="\" ) as string
===============================================================================
 '% BIF_RETURNONLYFSDIRS = 1
 '% BIF_EDITBOX = &H10
  % MAX_PATH = 260
  '
  BrowseInfo tbi
  tbi.hWndOwner = hwnd
  tbi.lpszTitle = strptr(strTitle)
 'tbi.ulFlags = BIF_RETURNONLYFSDIRS or BIF_EDITBOX
  tbi.lpfnCallback = @BrowseCallbackProc
  tbi.lParam = strptr(strInitDir)
  sys h = SHBrowseForFolder(@tbi)
  if h
    char s[MAX_PATH]
    SHGetPathFromIDList(h, s)
    return s
  endif
end function

'USAGE:
'sys hWnd=0
'print strDir = BrowseForDir(hWnd,"Select a Folder","c:\users")

