/******************************************************************************/
/*                                                                            */
/*  NAME_3DAG - Get the name of a 3DAG description file                       */
/*                                                                            */
/*  Note that name_3DAG_file_init must be called once to initialize the       */
/*  ofn and structure.  We do it this way so that user changes become defaults*/
/*                                                                            */
/******************************************************************************/

#define STRICT
#include <windows.h>
#include <commctrl.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include "const.h"
#include "classes.h"
#include "funcdefs.h"
#include "lin.rh"
#include "extern.h"

static OPENFILENAME ofn ;

void name_3DAG_file_init ()
{
   int i ;

/*
   This awkwardness is necessary because of the way the filter must be stored.
   It is pairs of strings (user prompt, name filter) terminated with an
   extra NULL.  To ensure that the strings are contiguous in memory, we
   use | to signify NULL-to-be in the declaration, then do the replacement.
   But of course we cannot modify a static string, so we need to copy it first!
*/

   static char *szFiltstr =
      "TXT (*.TXT)|*.txt|All files (*.*)|*.*|" ;
   static char szFilter[256] ;

   strcpy_s ( szFilter , szFiltstr ) ;
   for (i=0 ; szFilter[i] != 0 ; i++) {  // Replace | with NULL
      if (szFilter[i] == '|') // To guarantee all strings contiguous and
         szFilter[i] = 0 ;    // terminated with double NULL
      }

   ofn.lStructSize = sizeof(OPENFILENAME) ;
   ofn.hInstance = NULL ;
   ofn.lpstrFilter = szFilter ;     // Filter defined above
   ofn.lpstrCustomFilter = NULL ;   // Do not allow user-defined filters
   ofn.nMaxCustFilter = 0 ;         // Ignored, but set it anyway
   ofn.nFilterIndex = 1 ;           // Strings in filter are origin 1
   ofn.lpstrFile = NULL ;           // Set in name_net_file
   ofn.lpstrFileTitle = NULL ;      // We do not need the simple name
   ofn.nMaxFileTitle = 0 ;          // Ignored, but set it anyway
   ofn.lpstrInitialDir = NULL ;     // Start with current directory
   ofn.lpstrTitle = "3DAG file to read" ;
   ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NONETWORKBUTTON ;
   ofn.lpstrDefExt = "txt" ;        // Default extension if user types name
   ofn.lCustData = 0L ;             // Not used (for hook function only)
   ofn.lpfnHook = NULL ;            // No hook function
   ofn.lpTemplateName = NULL ;      // No resource template
}

int name_3DAG_file ( HWND hwnd , char *name )
{
   ofn.hwndOwner = hwnd ;       // Handle of owner window
   ofn.lpstrFile = name ;       // Full name will go here
   ofn.nMaxFile = 255 ;         // Length of that array

   return GetOpenFileName ( &ofn ) ;
}
