unit wSaveLoadFile0;

{ Feel free to use this unit as you want.
  First developped by Bernard Gisin
  This unit uses object programming to simplify the developpment
  of programs.

  This little unit help loading and saving files
  Open a dialog box to save or load a file.
----------------------------------}

// To tell FreePascal that the application is a Graphical User Interface
// i.e. is running in windows, not in text mode.
{$APPTYPE GUI}

INTERFACE
//"""""""

uses wGlobals2, windows;

type
  PSaveLoadFile0 = ^TSaveLoadFile0;
  TSaveLoadFile0 = object
  //"""""""""""""""""""""
  strPathName : ShortString;
  // complete name with its directory and extension,
  // of the file to be saved or loaded

  rNameRec    : OpenFileName;
  // To open or save a file. For its dialog box
  // Default extension, filters etc. See the .Init part

  strFilters : ShortString;
  // Filters to choose a file. See the .Init part

  strExtension    : string[12];
  // Default extension. See the .Init part

  constructor Init;
  destructor  Done; virtual;

  function LoadFileDlg(hParentP:HWND):word32; virtual;
  // Dialog box to choose a file to be opened
  // Return 0 if a file has been choosen.
  // Return 1 if the CANCEL button has been choosen

  function SaveFileDlg(hParentP:HWND):word32; virtual;
  // Dialog box to choose a file to be saved
  // Return 0 if a file has been choosen.
  // Return 1 if the CANCEL button has been choosen

  end; // TSaveLoadFile0 = object
  TSaveLoadFile =  TSaveLoadFile0;
  PSaveLoadFile = ^TSaveLoadFile;

IMPLEMENTATION
//""""""""""""

uses wWindowBase0, wDebug0;

var glnNotUsed : integer; // To avoid an error in the DevPascal 1.9 environment

//***************************************************
// TSaveLoadFile0 = object
//***************************************************

constructor TSaveLoadFile0.Init;
//==============================
begin
// One possible filter definition
strFilters := 'All files (*.*)'#0'*.*'#0 +
              'Text files (*.txt)'#0'*.txt'#0 +
              'Bitmap files (*.bmp)'#0'*.bmp'#0#0;
strExtension :='txt';

// Choose a file from a dialog box
FillChar(rNameRec,SizeOf(rNameRec),0);
strPathName[1] := #0;
strExtension[length(strExtension)+1]:=#0;
strFilters[length(strFilters)+1]:=#0;
strFilters[length(strFilters)+2]:=#0;
With rNameRec Do Begin
   LStructSize := SizeOf(rNameRec);
   hWndOwner   := 0;
   LpStrFilter := @strFilters[1];
   LpStrFile   := @strPathName[1];
   NMaxFile    := Max_Path;
   Flags       := OFN_Explorer Or OFN_HideReadOnly;
   LpStrDefExt := @strExtension;
   End; // with
end;  // TSaveLoadFile0.Init

destructor TSaveLoadFile0.Done;
//=============================
begin
end;  // TSaveLoadFile0.Done

function TSaveLoadFile0.LoadFileDlg(hParentP:HWND):word32;
//========================================================
// Dialog box to choose a file to be opened
// Return 0 if a file has been choosen.
// Return 1 if the CANCEL button has been choosen
begin
// The file must exist to open it
rNameRec.Flags:=rNameRec.Flags or OFN_FileMustExist;

// Must set the owner of the dialog box
rNameRec.hWndOwner:=hParentP;

if GetOpenFileName(@rNameRec) then begin
   // OK button choosen
   LoadFileDlg:=0;

   // Convert null terminated string to ShortString
   strPathName[0]:=Char(StrLen(@strPathName[1]));
   end
else  LoadFileDlg:=1; // The CANCEL button has been choosen
end;  // TSaveLoadFile0.LoadFileDlg

function TSaveLoadFile0.SaveFileDlg(hParentP:HWND):word32;
//========================================================
// Dialog box to choose a file to be saved
// Return 0 if a file has been choosen.
// Return 1 if the CANCEL button has been choosen
begin
// The file to be saved can be inexistant.
rNameRec.Flags:=rNameRec.Flags and (not OFN_FileMustExist);

// Must set the owner of the dialog box
rNameRec.hWndOwner:=hParentP;

if GetSaveFileName(@rNameRec) then begin
   // OK button choosen
   SaveFileDlg:=0;

   // Convert null terminated string to ShortString
   strPathName[0]:=Char(StrLen(@strPathName[1]));
   end
else SaveFileDlg:=1; // The CANCEL button has been choosen
end;  // TSaveLoadFile0.SaveFileDlg

end.  // unit wSaveLoadFile0;

