#include <WinTen.h>
#include <Windows.h>
#include <ClipApi.h>

#include <CommDlg.h>

static WORD wIndex;

//----------------------------------------------------------------------------//

CLIPPER cGetFile()   // ( cFileMask, cTitle, nDefaultMask )  --> cFileName
{
   OPENFILENAME ofn;
   char szDirName[ 100 ], szFile[ 256 ], szFileTitle[ 256 ], szFilter[ 300 ];
   char szTitle[ 100 ];
   WORD w = 0;
   BYTE bIndex = _parni( 3 );

   strcpy( szDirName, ".\\" );
   strcpy( szFilter, _parc( 1 ) );

   strcpy( szFile, _parc( 1 ) );
   strcpy( szTitle, _parc( 2 ) );

   _xunlock();

   while( * ( szFilter + w ) )
   {
      if( * ( szFilter + w ) == '|' )
      {
         * ( szFilter + w ) = 0;
         szFile[ 0 ] = 0;
      }
      w++;
   }
   szFilter[ w++ ] = 0;
   szFilter[ w++ ] = 0;
   szFilter[ w   ] = 0;

   _bset( ( char * ) &ofn, 0, sizeof( OPENFILENAME ) );

   ofn.lStructSize     = sizeof( OPENFILENAME );
   ofn.hwndOwner       = GetActiveWindow();
   ofn.lpstrFilter     = szFilter;
   ofn.nFilterIndex    = bIndex ? bIndex: 1;
   ofn.lpstrFile       = szFile;
   ofn.nMaxFile        = sizeof( szFile );
   ofn.lpstrFileTitle  = szFileTitle;
   ofn.nMaxFileTitle   = sizeof( szFileTitle );
   ofn.lpstrInitialDir = szDirName;
   ofn.lpstrTitle      = szTitle;
   ofn.Flags           = OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;

   wIndex = 0;

   GetOpenFileName( &ofn );

   wIndex = ofn.nFilterIndex;

   _retc( szFile );
}

//----------------------------------------------------------------------------//

CLIPPER NGETFILEFI()   //  LTER()    Returns the index of the filter selection
{
   _retni( wIndex );
}

//----------------------------------------------------------------------------//
