{ Testing the wStringDataLink2.pp unit.

  My goal is that if we give a name to a main Window or a dialog box,
  Its position (and size) are saved in a file, and each time they are
  reopened, they come to the place they where last time.

  To do this, I want to associate to a string another string, or
  4 or 5 numbers, storing information associated with this string.

  I want to :
  - Set the information associated to the string :
  - Get the information associated to the string :
  - Deleting a string and its data will come later, maybe.
  --------------------------------------------}
program tEx040;

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

Uses
  wApplication0
 ,wWindow0
 ,wVariableNumLink2
 ,wDialogCreatorLOW2
 ,wStringDataLink2
 ,wParamApplic2
 ,wDebug0
 ,wGlobals2
 ,Windows;

type TST85 = string[95];

type
  PMyWindow = ^TMyWindow;
  TMyWindow = object(TWindow)
  //"""""""""""""""""""""""""
  fFileDB : file of TST85;

  strDB : TST85;
  // To put in and retrieve data from the  TStringDataLink  object.

  oSDL : TStringDataLink;
  // To test this unit.
  // It works now, and is used in the wDialog0.pp unit to save the
  // dialogs positions, if a name is give in the .msg file as for example
  // @$DLGNAME("this is the name of a dialog box")

  // Data to add into the "_Prm.msg" file
  wData1,
  wData2,
  wData3,
  wData4,
  wData5 : Word32;

  strLink : ShortString;
  // The string linked to the data.

  constructor Init(strWinTitlePrm:ShortString);
  destructor Done; virtual;
  procedure WinCreate(strWinClass:ShortString); virtual;

  procedure Test1; virtual;
  // Set the five numbers data associated to a string

  procedure Test2; virtual;
  // Get the five numbers data associated to a string

  procedure Test3; virtual;
  // Get the string data associated to a string

  procedure Test4; virtual;
  // Give the list of all strings & data

  procedure Test5; virtual;
  // just a stupid test.

  function WMCommand(wPar:WPARAM; lPar:LPARAM):LRESULT; virtual;
  // Handles the menu messages

//  function WindowProc(hWindow:HWND; wMsg:UINT; wPar:WPARAM; lPar:LPARAM):LRESULT; virtual;
  end; // TMyWindow = object(TWindow0)

// var nNotUsed : integer; // only to avoid an error message while compliling in DevPascal 1.9

//******************************************************************
// TMyWindow = object(TWindow0)
//******************************************************************

constructor TMyWindow.Init(strWinTitlePrm:ShortString);
//=====================================================
begin
inherited Init(strWinTitlePrm);

// Once forever in this program, do some Variable - Number links
Word32Field(1001, wData1);
Word32Field(1002, wData2);
Word32Field(1003, wData3);
Word32Field(1004, wData4);
Word32Field(1005, wData5);
StrField( 2001, strLink, SizeOf(strLink));
end;  // TMyWindow.Init

destructor TMyWindow.Done;
//========================
begin
oSDL.Done;
inherited Done;
end;  // TMyWindow.Done

procedure TMyWindow.WinCreate(strWinClass:ShortString);
//=====================================================
// Create the window, and insert an edit box and a status line in it.
var
  wError      : Word32;
  strEXEDir   : ShortString;  // with the '\' at the end
  strEXEName  : String[80];
begin
inherited WinCreate(strWinClass);

// Put it at the same place as the parameters, but with another extension.
gglpoPrm^.GetParamDirName(strEXEDir,strEXEName);

oSDL.Init(strEXEDir + strEXEName + '.aTst');
oSDL.SetupStringDataLink(wError); // I hope there is no errors.
end;  // TMyWindow.WinCreate

procedure TMyWindow.Test1;
//========================
// Set the five numbers data associated to a string
var
  nRep        : integer;
  wError      : Word32;
begin
nRep:=DialogExecMSG(11199, @Self);

if (nRep = IDOK) then
   oSDL.SetStringDataW5(strLink, wData1,wData2,wData3,wData4,wData5,wError);

end;  // TMyWindow.Test1

procedure TMyWindow.Test2;
//========================
// Get the five numbers data associated to a string
var
  nRep        : integer;
  wError      : Word32;
begin
nRep:=DialogExecMSG(11299, @Self);

if (nRep = IDOK) then begin
   oSDL.GetStringDataW5(strLink, wData1,wData2,wData3,wData4,wData5,wError);

   if (wError = 1) then DBW0(1, 'Not Found : ' + strLink)
   else DBW0(1, 'Data=' + ITS5(wData1) + ITS5(wData2) + ITS5(wData3)
                        + ITS5(wData4) + ITS5(wData5));
   end;
end;  // TMyWindow.Test2

procedure TMyWindow.Test3;
//========================
// Get the string data associated to a string
var
  nRep        : integer;
  wError      : Word32;
  strDataL    : ShortString;
begin
nRep:=DialogExecMSG(11299, @Self);

if (nRep = IDOK) then begin
   oSDL.GetStringDataStr(strLink, strDataL, wError);

   if (wError = 1) then DBW0(1, 'Not Found : ' + strLink)
   else DBW0(1, 'Data=' + strDataL);
   end;
end;  // TMyWindow.Test3

procedure TMyWindow.Test4;
//========================
// Give the list of all strings & the associated data.
var
  ww          : Word32;
  strLinkL,
  strDataL    : ShortString;
begin
// For ww = 0, this is the warning at the beggining of the file.
for ww:=0 to oSDL.GetNumberStrings do begin
   oSDL.GetStringNumber(ww, strLinkL, strDataL);
   DBW(1, strLinkL + ' ---> ' + strDataL);
   end;
end;  // TMyWindow.Test4

procedure TMyWindow.Test5;
//========================
// just a stupid test.
begin
{$i-}
assign(fFileDB, 'Test.aaa');
fileMode:=2;
reset(fFileDB);
seek(fFileDB, 2); // Go after the second line

if (IOResult <> 0) then begin
   rewrite(fFileDB);

   strDB :='This is a first long string terminated with a CR LF ---------------------------------------------------------------';
   strDB[94]:=#13;
   strDB[95]:=#10;
   write(fFileDB, strDB);

   strDB :='This is a second long string terminated with a CR LF ---------------------------------------------------------------';
   strDB[94]:=#13;
   strDB[95]:=#10;
   write(fFileDB, strDB);

   close(fFileDB);

   DBW(1, 'IORes=' + ITS(IOResult));
   exit;
   end;

strDB :='This is a third long string terminated with a CR LF ---------------------------------------------------------------';
strDB[94]:=#13;
strDB[95]:=#10;

write(fFileDB, strDB);

strDB :='This is a fourth long string terminated with a CR LF -----------------------------------------------------';
strDB[94]:=#13;
strDB[95]:=#10;

write(fFileDB, strDB);

close(fFileDB);

DBW(1, 'IORes=' + ITS(IOResult));
end;  // TMyWindow.Test5

function TMyWindow.WMCommand(wPar:WPARAM; lPar:LPARAM):LRESULT;
//=============================================================
// Handles the menu messages
Var
  wNrMenu     : word;
begin
WMCommand := 0; // Default return value when processing a message

if (lPar <> 0) then begin
   // call the parent WMCommand method.
   WMCommand:=inherited WMCommand(wPar, lPar);
   exit;
   end; // if (lPar <> 0) then begin

// for the menus,  lPar = 0
//=========================
wNrMenu   := LoWord(wPar);
Case wNrMenu Of
   100 : DBW0(0, '');  // just open the debugging window
   101 : Test1;
   102 : Test2;
   103 : Test3;
   104 : Test4;
   105 : Test5;
   else WMCommand:=inherited WMCommand(wPar, lPar);
   end;  // Case NrMenu Of
end;  // TMyWindow.WMCommand

{ Not used !!
function TMyWindow.WindowProc(hWindow:HWND; wMsg:UINT; wPar:WPARAM; lPar:LPARAM):LRESULT;
//=======================================================================================
// All window messages are send here.

Begin
WindowProc := 0; // Default return value when processing a message

Case wMsg Of
   ????
   else begin // Case wMsg Of
      // Default processing of the messages.
      WindowProc:=inherited WindowProc(hWindow, wMsg, wPar, lPar);
      end; // Case wMsg Of
   End; // Case wMsg Of

End;  // TMyWindow.WindowProc
}
var
  oApp        : TApplication;
  poMyMainWin : PMyWindow;

Begin // MAIN ******
// Name under which the main window is registered. Spaces are not allowed
oApp.Init('Example040');

// Creation of the main window
new(poMyMainWin, Init('tEx040. Test the wStringDataLink2.pp unit.'));

oApp.Run;
oApp.Done;  // The main window object is disposed here.
End.

