{ Test of the unit wDialogCreator2.pp
  While developping a new unit, it's a good idea to test it specially.
  The wDialogCreator2.pp unit is an important one, which allows you to create
  dialog boxes very easilly, just from a description made in the
  text message files.

  Normally, the  wDialogCreatorLOW2.pp  unit should be used.
  See example  tEx038.pp,  much better.
  --------------------------------------------}
program tEx035;

// 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
 ,wDialogCreator2
 ,wDialog0
 ,wButtonPush0
 ,wVariableNumLink2
 ,wWindowBase0
 ,wErrors0
 ,wGlobals2
 ,wDebug0
 ,Windows;

type
  PMyWindow = ^TMyWindow;
  TMyWindow = object(TWindow)
  //"""""""""""""""""""""""""
  oDC         : TDialogCreator2;
  // Normally, TDialogCreator2 is not used, only its parent
  // TDialogCreatorLOW2 is used.
  // But for the tests, when some debbuger display 'DBW0(...'
  // are used, it is good to use the high level dialog creator.

  wMemberValue1,
  wMemberValue2,
  wMemberValue3 : Word32;
  // Values changed in an edit box or combo box.

  strMember1,
  strMember2,
  strMember3 : string[50];
  // Values changed in an edit box.

  fShowValue : boolean;
  // For the test 6, show the value of the variable,
  // not for other tests.

  constructor Init(strWinTitlePrm:ShortString);
  destructor Done; virtual;

  procedure Test1; virtual;

  procedure Test2; virtual;

  procedure Test3; virtual;

  procedure Test4; virtual;
  // Test the tabulators : TABAFTER

  procedure Test5; virtual;
  // Test the tabulators : TABBEFORE

  procedure Test6; virtual;
  // Test the link between variables and edit boxes.

  procedure Test7; virtual;
  // Test that the memory is well released after the use of a dialog box

  procedure Info7_1; virtual;
  // Dialog box opened from the dialog box 7

  procedure Info7_2; virtual;
  // Dialog box opened from the dialog box 7

  procedure CtrlVarUpdate(poCtrl:PWindowBase0; pVar:pointer); virtual;
  // When a control changed its value and it is linked to a variable and
  // it belongs to a dialog belonging to a window, then
  // the control calls this method with a pointer on itself and
  // a pointer on the associated variable.
  // Either you know the type, or you can get it with
  // pControl(poCtrl)^.rVarLinkInfo.wVarType

  procedure DialogAllRestored(poDlg:PWindowBase0); virtual;
  // When a dialog box is ended with a Cancel, all variables associated
  // to controls are set back to their original values.
  // Thus the dialog box calls this method with a pointer on itself.

  procedure ButtonClicked(poCtrl:PWindowBase0; wCtrlID:Word32); virtual;
  // When a button is clicked, and it is in a dialog which belongs to a Window,
  // then the button calls this method, with a pointer on itself
  // and the ID it has been given.

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

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

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

wMemberValue1 :=250;
wMemberValue2 :=5678;
wMemberValue3 :=333;
strMember1    :='In the Edit';
strMember2    :='In The Combo Drop Down';
strMember3    :='Combo Drop Down List';

// For the whole program, if these numbers are not used by other
// variables in the program, (the end developper must not make this mistake)
// then these links will remain.
Word32Field(101, wMemberValue1);
Word32Field(102, wMemberValue2);
Word32Field(103, wMemberValue3);
StrField(111, strMember1, SizeOf(strMember1));
StrField(112, strMember2, SizeOf(strMember2));
StrField(113, strMember3, SizeOf(strMember3));

fShowValue:=FALSE;

// This line is all what is needed to have a status line.
new(poStatusLine, Init(@Self));

end;  // TMyWindow.Init

destructor TMyWindow.Done;
//=========================
begin

inherited Done;
end;  // TMyWindow.Done

procedure TMyWindow.Test1;
//========================
var
  oDCLoc1     : TDialogCreator2;
  poDialogL   : PDialog;
  wMsgNum     : word32;  // to test the varstring;
  nRet        : integer; // return value from the dialog box
begin
// just go through the creation,
// debugging info will be displayed in the debug window.

wMsgNum:=11050;
Word32Field(5001, wMsgNum);

oDCLoc1.Init;
poDialogL:=PDialog(oDCLoc1.DialogCreation(11199, @Self));
oDCLoc1.Done; // We have the dialog, the dialog creator can be disposed.

nRet:=poDialogL^.DialogExec;
DBW0(1, 'Dlg Ret value =' + ITS(nRet));

// It is a good habit to clear the link between a number and
// an invalid variable.
// The number 5001 is not more associated to a variable.
ClearField(5001);
end;  // TMyWindow.Test1

procedure TMyWindow.Test2;
//========================
var
  oDCLoc1     : TDialogCreator2;
  poDialogL   : PDialog;
  wMsgNum     : word32;  // to test the varstring;
  nRet        : integer; // return value from the dialog box
begin
// just go through the creation,
// debugging info will be displayed in the debug window.

wMsgNum:=11050;
Word32Field(5001, wMsgNum);

oDCLoc1.Init;
poDialogL:=PDialog(oDCLoc1.DialogCreation(11299, @Self));
oDCLoc1.Done; // We have the dialog, the dialog creator can be disposed.

nRet:=poDialogL^.DialogExec;
DBW0(1, 'Dlg Ret value =' + ITS(nRet));

// It is a good habit to clear the link between a number and
// an invalid variable.
// The number 5001 is not more associated to a variable.
ClearField(5001);
end;  // TMyWindow.Test2

procedure TMyWindow.Test3;
//========================
var
  poDialogL   : PDialog;
  wVal1,    // to have some data to put in the dialog box.
  wVal2,
  wVal3     : word32;  //
  strS1,
  strS2,
  strS3        : string[50];
  nRet        : integer; // return value from the dialog box
begin
// just go through the creation,
// debugging info will be displayed in the debug window.

Word32Field(5001, wVal1);   wVal1:=12345;
Word32Field(5002, wVal2);   wVal2:=2222222;
Word32Field(5003, wVal3);   wVal3:=-321;
StrField(5011, strS1, SizeOf(strS1));
StrField(5012, strS2, SizeOf(strS2));
StrField(5013, strS3, SizeOf(strS3));
strS1:='ABC_ the strS1 string.';
strS2:='A very long string, having not much to say, but saying it anyway !';
strS3:='The third string passed as a variable linked to the number 5013.';

oDC.Init;
poDialogL:=PDialog(oDC.DialogCreation(11399, @Self));
oDC.Done; // We have the dialog, the dialog creator can be disposed.

nRet:=poDialogL^.DialogExec;
DBW0(1, 'Dlg Ret value =' + ITS(nRet));

// It is a good habit to clear the link between a number and
// an invalid variable.
// The numbers 5001..5013 are not more associated to variables.
ClearField(5001);
ClearField(5002);
ClearField(5003);
ClearField(5012);
ClearField(5013);
end;  // TMyWindow.Test3

procedure TMyWindow.Test4;
//========================
var
  poDialogL   : PDialog;
  wVal1,    // to have some data to put in the dialog box.
  wVal2,
  wVal3     : word32;  //
  strS1,
  strS2,
  strS3        : string[50];
  nRet        : integer; // return value from the dialog box
begin
// just go through the creation,
// debugging info will be displayed in the debug window.

Word32Field(5001, wVal1);   wVal1:=12345;
Word32Field(5002, wVal2);   wVal2:=2222222;
Word32Field(5003, wVal3);   wVal3:=-321;
StrField(5011, strS1, SizeOf(strS1));
StrField(5012, strS2, SizeOf(strS2));
StrField(5013, strS3, SizeOf(strS3));
strS1:='ABC_ the strS1 string.';
strS2:='A very long string, having not much to say, but saying it anyway !';
strS3:='The third string passed as a variable linked to the number 5013.';

oDC.Init;
poDialogL:=PDialog(oDC.DialogCreation(11499, @Self));
oDC.Done; // We have the dialog, the dialog creator can be disposed.

nRet:=poDialogL^.DialogExec;
DBW0(1, 'Dlg Ret value =' + ITS(nRet));

// It is a good habit to clear the link between a number and
// an invalid variable.
// The numbers 5001..5013 are not more associated to variables.
ClearField(5001);
ClearField(5002);
ClearField(5003);
ClearField(5012);
ClearField(5013);
end;  // TMyWindow.Test4

procedure TMyWindow.Test5;
//========================
var
  poDialogL   : PDialog;
  wVal1,    // to have some data to put in the dialog box.
  wVal2,
  wVal3     : word32;  //
  strS1,
  strS2,
  strS3        : string[80];
  nRet        : integer; // return value from the dialog box
begin
// just go through the creation,
// debugging info will be displayed in the debug window.

Word32Field(5001, wVal1);   wVal1:=12345;
Word32Field(5002, wVal2);   wVal2:=2222222;
Word32Field(5003, wVal3);   wVal3:=-321;
StrField(5011, strS1, SizeOf(strS1));
StrField(5012, strS2, SizeOf(strS2));
StrField(5013, strS3, SizeOf(strS3));
strS1:='ABC_ the strS1 string.';
strS2:='A very long string, having not much to say, but saying it anyway !';
strS3:='The third string passed as a variable linked to the number 5013.';

oDC.Init;
poDialogL:=PDialog(oDC.DialogCreation(11599, @Self));
oDC.Done; // We have the dialog, the dialog creator can be disposed.

nRet:=poDialogL^.DialogExec;
DBW0(1, 'Dlg Ret value =' + ITS(nRet));

// It is a good habit to clear the link between a number and
// an invalid variable.
// The numbers 5001..5013 are not more associated to variables.
ClearField(5001);
ClearField(5002);
ClearField(5003);
ClearField(5012);
ClearField(5013);
end;  // TMyWindow.Test5

procedure TMyWindow.Test6;
//========================
var
  poDialogL   : PDialog;
  nRet        : integer; // return value from the dialog box
begin
// Comming back to the dialog box, will have the values changed the last time.
fShowValue:=TRUE;

oDC.Init;
poDialogL:=PDialog(oDC.DialogCreation(11699, @Self));
oDC.Done; // We have the dialog, the dialog creator can be disposed.

nRet:=poDialogL^.DialogExec;
DBW0(1, 'Dlg Ret value =' + ITS(nRet));

fShowValue:=FALSE;

DBW0(65536, 'wMemberValue1=' + ITS(wMemberValue1));
DBW0(65536, 'wMemberValue2=' + ITS(wMemberValue2));
DBW0(65536, 'strMember1="' + strMember1 + '"');
DBW0(65536, 'strMember2="' + strMember2 + '"');
DBW0(65536, 'strMember3="' + strMember3 + '"');
end;  // TMyWindow.Test6

procedure TMyWindow.Test7;
//========================
// Test that the memory is well released after the use of a dialog box
var
  poDialogL   : PDialog;
  nRet        : integer; // return value from the dialog box
  nMem1,      // before allocation memory for the menu
  nMem2,      // after allocation memory but before freeing the memory
  nMem3,      // after freeing the memory
  nMem4       // after destroying the menu
              // pMem3 should return to the same value as pMem1
              : int32;
begin
nMem1:=MemAvail; DBW0(128, 'nMem1=' + ITS(nMem1));

oDC.Init;

nMem2:=MemAvail; DBW0(128, 'nMem2=' + ITS(nMem2));

poDialogL:=PDialog(oDC.DialogCreation(11799, @Self));

nMem3:=MemAvail; DBW0(128, 'nMem3=' + ITS(nMem3));

oDC.Done; // We have the dialog, the dialog creator can be disposed.

nRet:=poDialogL^.DialogExec;
DBW0(1, 'Dlg Ret value =' + ITS(nRet));

nMem4:=MemAvail; DBW0(128, 'nMem4=' + ITS(nMem4));

DBW0(128, 'IF nMem4 = nMem1 =' + ITS(nMem1) + ' =' + ITS(nMem4) +
        '  all the memory was correctly disposed.');
end;  // TMyWindow.Test7

procedure TMyWindow.Info7_1;
//==========================
// Dialog box opened from the dialog box 7
var
  poDialogL   : PDialog;
begin
oDC.Init;
poDialogL:=PDialog(oDC.DialogCreation(11761, @Self));
oDC.Done; // We have the dialog, the dialog creator can be disposed.

poDialogL^.DialogExec;
end;  // TMyWindow.Info7_1

procedure TMyWindow.Info7_2;
//==========================
// Dialog box opened from the dialog box 7
var
  poDialogL   : PDialog;
begin
oDC.Init;
poDialogL:=PDialog(oDC.DialogCreation(11762, @Self));
oDC.Done; // We have the dialog, the dialog creator can be disposed.

poDialogL^.DialogExec;
end;  // TMyWindow.Info7_2

procedure TMyWindow.DialogAllRestored(poDlg:PWindowBase0);
//========================================================
begin
poDlg:=poDlg; // to avoid a hint
// a variable has changed in a dialog box.
DBW0(32768, '');
DBW0(32768, 'wMemberValue1=' + ITS(wMemberValue1));
DBW0(32768, 'wMemberValue2=' + ITS(wMemberValue2));
DBW0(32768, 'strMember1    ="' + strMember1 + '"');
DBW0(32768, 'strMember2    ="' + strMember2 + '"');
DBW0(32768, 'strMember3    ="' + strMember3 + '"');
end;  // TMyWindow.DialogAllRestored

procedure TMyWindow.CtrlVarUpdate(poCtrl:PWindowBase0; pVar:pointer);
//===================================================================
begin
pVar:=pVar;  // to avoid a hint.
DialogAllRestored(poCtrl^.poParent);
end;  // TMyWindow.CtrlVarUpdate

procedure TMyWindow.ButtonClicked(poCtrl:PWindowBase0; wCtrlID:Word32);
//=====================================================================
var
  poBtn       : PButtonPush;
begin
poBtn:=PButtonPush(poCtrl); // We know that it points to a push button object.
DBW0(65536, 'Button has been clicked, id=' + ITS(wCtrlID) + ' = ' + ITS(poBtn^.wCtrlID));
DBW0(65536, 'Button handle   = ' + ITS(poBtn^.hWin));
DBW0(65536, 'Button class    = ' + poBtn^.strClassName);
DBW0(65536, 'Button Win name = ' + poBtn^.strWindowName);
DBW0(65536, 'Button label    = ' + poBtn^.strCtrlLabel);
DBW0(65536, 'Button hint     = ' + poBtn^.strCtrlHint);
// poBtn^.poParent = pointer on the dialog box
end;  // TMyWindow.ButtonClicked

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; // test the tabulators : TABAFTER
   105 : Test5; // test the tabulators : TABBEFORE
   106 : Test6; // test links between variables and edit boxes
   107 : Test7; // test that memory is sell released.

   111 : Info7_1; // info associated with the Text7 dialog box.
   112 : Info7_2; // idem

   401 : MessageBox( hWin,'Test the wDialogCreator2.pp unit','Just a unit test.',
                     MB_OK Or MB_IconInformation);
   else WMCommand:=inherited WMCommand(wPar, lPar);
   end;  // Case NrMenu Of
end;  // TMyWindow.WMCommand

var
  oApp        : TApplication;
  poMyMainWin : PMyWindow;

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

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

// The App.WindowClass can be modified here, if wanted. (Not usual)
oApp.Run;
oApp.Done;  // The main window object is disposed here.
End.

