unit wWindowText0;

{  Feel free to use this unit as you want.
   First developped by Bernard Gisin

   This unit defines an object and a procedure to create and write
   very easilly in a text window.
   It can already be done with the  DBW(...  procedure,
   But here the text length is unlimited.
   Simply calling  WriteWin(wNumber, strS)  will create a
   window, if the window number  (wNumber mod 16)  doesn't exist,
   and will write the string into it.

   Often it is usefull to be able to write text in a window.
   At any moment, the user can modify and save this text.
-----------------------------------------------------}

INTERFACE
//"""""""

uses wWindow0, wRichTextFrame0, wGlobals2, windows;

type
  PWindowText0 = ^TWindowText0;
  TWindowText0 = object(TWindow)
  //""""""""""""""""""""""""""""
//  hWin    : HWND;  // Handle on the window, got from TWindowBase
//  hParent : HWND;  // Handle on the parent, got from TWindowBase
//  poParent: PWindowBase0;  // Pointer on the parent, got from TWindowBase

  oRichEditFrame   : TRichTextFrame;
  // RichEditFrame control contained in the window

  wWinNumber : word32;
  // Remember which number was associated to this window.

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

  procedure MenuCreate; virtual;
  // Creates the menu

  procedure WinCreate(strWinClass:ShortString); virtual;
  // Set  strWinClass=''  to use the class defined in the application object

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

  function WindowProcFirst(hWindow:HWND; wMsg:UINT; wPar:WPARAM; lPar:LPARAM):LRESULT; virtual;
  // Handles messages for the library, before calling  WindowProc
  // It is usually not overriden

  end; // TWindowText0 = object(TWindow)
  PWindowText = ^TWindowText;
  TWindowText = TWindowText0;

procedure WriteWin(wWinP:word32; strS:ShortString);
//=================================================
// If the window having number  (wWinP mod 16) doesn't exist, create it.
// Write the string into it.

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

uses wMenus0, wParamApplic2, wMessages2, wObjectCreatorLOW2, wDebug0;

const
  glfDoInit : boolean = TRUE;
  MAXWIN = 15;
  // Can have MAXWIN + 1 different text windows.

var
  glapoWin : array[0..MAXWIN] of PWindowText0;
  // Pointers on the 16 possible windows

var nUnused : integer; // to avoid error in DevPascal while compiling

procedure WriteWin(wWinP:word32; strS:ShortString);
//=================================================
// If the window having number  (wWinP mod 16) doesn't exist, create it.
// Write the string into it.
var
  ww          : word;
  strWNameL   : ShortString;

begin
if glfDoInit then begin
   for ww:=0 to MAXWIN do glapoWin[ww]:=NIL;
   glfDoInit:=FALSE;
   end;

// Limit the number of possible windows
wWinP:=wWinP mod (MAXWIN+1);

// If the window doesn't exist create it
if (glapoWin[wWinP] = NIL) then begin
   strWNameL:='RichText Window No : ' + IntToStr(wWinP,0);
   glapoWin[wWinP]:=PWindowText(gglpoObjCreate^.CreateWindowText(strWNameL));

   // Remember the number under which it was created
   glapoWin[wWinP]^.wWinNumber:=wWinP;

   // Give a name to this window, to remember its position
   glapoWin[wWinP]^.strWindowName:=strWNameL; 

   // Create the window
   glapoWin[wWinP]^.WinCreate('');
   end;

// Add the string at the end of the text
glapoWin[wWinP]^.oRichEditFrame.AppendText(strS);
end;  //  WriteWin

//******************************************************************
// TWindowText0 = object(TWindow)
//******************************************************************

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

if (@Self <> gglpoMainWindow) then begin
   // Set its parent, and add a new child to its parent.
   poParent:=gglpoMainWindow;
   gglpoMainWindow^.AddChildObjectWindow(@Self);
   end;

// Change its style
//dwStyle:=... ?

// The window can be created with another method than WriteWin(...)
wWinNumber:=word32(-1);

// To have a Rich Text control, you must create the object, and
// later the corresponding window.
// The third thing to do will be to send it the menu messages.
oRichEditFrame.Init(@Self);

// The RichEdit Frame takes all the client area place
oRichEditFrame.vXSizeRel := 1;
oRichEditFrame.vYSizeRel := 1;

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

destructor TWindowText0.Done;
//===========================
begin
// remove it from the list of child Windows of its parent.
if (poParent <> NIL) then poParent^.RemoveChildObjectWindow(@Self);
poParent:=NIL;

// destroy the associated RichEditFrame  Already done
//if (oRichEditFrame.hWin <> 0) then oRichEditFrame.Done;
//oRichEditFrame.hWin:=0;

// This window doesn't exist any more
if (wWinNumber <= MAXWIN) then glapoWin[wWinNumber]:=NIL;

inherited Done;
end;  // TWindowText0.Done

procedure TWindowText0.MenuCreate;
//================================
// By default, does nothing.
// Is overriden to create a menu in the window
begin
// here, the menu is automatically created.
// By default it is defined at number  2100010000  in StandardEn.msg
if (poMenu = NIL) then poMenu:=PMenu(gglpoObjCreate^.CreateMenu(@Self));
hMenuWin:=poMenu^.MenuMsgCreate(2100010000);
end;  // TWindowText0.MenuCreate

procedure TWindowText0.WinCreate(strWinClass:ShortString);
//=====================================================
// Create the window, and insert an edit box and a status line in it.
var
  wD1,wD2,wD3,wD4,wD5, // data associated with the dialog file name
  wErrorL     : Word32;
begin
if (strWindowName <> '') and (gglpoPrm <> NIL) then begin
   gglpoPrm^.oSDL.GetStringDataW5(strWindowName, wD1,wD2,wD3,wD4,wD5,wErrorL);
   if (wErrorL = 0) then begin
      // Position set by the last position of the window
      // But size remains as computed (or set) otherwise.
      nLeft   :=wD1;
      nTop    :=wD2;
      nWidth  :=wD3;
      nHeight :=wD4;
      wSizeState:=wD5;
      end; // if (wErrorL = 0) then begin
   end; // if (strWindowName <> '') then begin

// here strWinClass=''.
inherited WinCreate(strWinClass);

// Tells the richEdit that there is a status line
oRichEditFrame.poStatusLine:=poStatusLine;

// Create the Rich Text Window.
oRichEditFrame.CtrlCreate;
end;  // TWindowText0.WinCreate

function TWindowText0.WMCommand(wPar:WPARAM; lPar:LPARAM):LRESULT;
//=============================================================
// Handles the menu messages
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;

// for the menus,  lPar = 0
//=========================

// Let the RichEditFrame control interpret the message.
// It return TRUE, if it did, FALSE if the message was not for him.
// Typically, messages with  wNrMenu > 30000 are for the library, and
// should not be treated here.
if (oRichEditFrame.WMCommand(LoWord(wPar))) then exit;

// if not treated by the RichText control, do the library default treatement.
WMCommand:=inherited WMCommand(wPar, lPar);
end;  // TWindowText0.WMCommand

function TWindowText0.WindowProcFirst(hWindow:HWND; wMsg:UINT; wPar:WPARAM; lPar:LPARAM):LRESULT;
//===========================================================================================
// Handles messages for the library, before calling  WindowProc
var
  wErrorL     : word32;
begin
if (wMsg = WM_SetFocus) then SetFocus(oRichEditFrame.hWin);

if (wMsg = WM_NCDestroy) then begin
   // If the window has a name, then its size and position can be associated
   // with it in the String - Data Link file. Save these values
   if (strWindowName <> '') and (gglpoPrm <> NIL) then begin
      gglpoPrm^.oSDL.SetStringDataW5(strWindowName, nLeft,nTop,nWidth,nHeight,wSizeState,wErrorL);
      end; // if (strWindowName <> '') then begin
   end;

WindowProcFirst:=inherited WindowProcFirst(hWindow, wMsg, wPar, lPar);
end;  // WindowText0.WindowProcFirst

end. // unit wWindowText0;

