{ Example of a program using the  wMenus0.pp  unit.
  This is the third example which uses really the Message file, and
  and a description of its menu in the message file.
  It uses the facilities of the library.
  The menu is generated automatically, because the corresponding
  message file exist (tEx031.msg) and line index 10000 is :
  10000 AutoCreate
  # here the menu description comes, c.f. tEx032.msg

  Also the handling of window messages is done automatically,
  the disposal of the menu object also.

  This example is the same as example tEx031.pp but adds the
  functionality of a little editor, similar to the one of example tEx008.pp
  See how many code lines have been removed, because they are automatically
  treated in the library.

  It's only to show how simple it became to have the beginning
  of an editor if the GAP library is used.
  --------------------------------------------}
program tEx032;

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

Uses
  Windows
 ,wApplication0
 ,wWindow0
 ,wRichText0
 ,wDebug0;

type
  PMyWindow = ^TMyWindow;
  TMyWindow = object(TWindow)
  //"""""""""""""""""""""""""
  oRichEdit   : TRichText0;   // RichEdit control contained in the window

  // Can we write text, or only read it ?
  fReadOnly : boolean;

  constructor Init(strWinTitlePrm:ShortString);

  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
  end; // TMyWindow = object(TWindow)

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

oRichEdit.Init(@Self);

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

// Can we write text, or only read it ?  We can wirte by default.
fReadOnly :=FALSE;
end;  // TMyWindow.Init

procedure TMyWindow.WinCreate(strWinClass:ShortString);
//=====================================================
// Create the window, and insert an edit box and a status line in it.
begin
// here strWinClass=''.
inherited WinCreate(strWinClass);

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

// Size = the whole client areat
oRichEdit.vXSizeRel:=1;  // if you don't specify, it is set automatically
oRichEdit.vYSizeRel:=1;  // but for clarity, it is better to specify.

oRichEdit.CtrlCreate;
SetFocus(oRichEdit.hWin);
end;  // TMyWindow.WinCreate

function TMyWindow.WMCommand(wPar:WPARAM; lPar:LPARAM):LRESULT;
//=============================================================
// Handles the menu messages
Var
  wNrMenu,
  wCheck      // to set a check mark in a menu.
              : 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;

// for the menus,  lPar = 0
//=========================
wNrMenu   := LoWord(wPar);
Case wNrMenu Of
   101 : oRichEdit.NewText;
   102 : oRichEdit.LoadRICH;
   103 : oRichEdit.SaveRICH;
   104 : oRichEdit.SaveRICHas;

   201 : oRichEdit.UndoText;
   202 : oRichEdit.CutText;
   203 : oRichEdit.CopyText;
   204 : oRichEdit.PasteText;
   205 : oRichEdit.SelectAllText;
   301 : oRichEdit.SelectFont;

   302 : begin // toggle the read only flag.
      fReadOnly:=not fReadOnly;

      // tell the control to set or reset its readonly mode.
      SendMessage(oRichEdit.hWin, EM_SETREADONLY, WPARAM(fReadOnly), 0);

      // Get the handle on the menu
//      hMenuWin:=GetMenu(hWindow);  already done

      // change the menu check mark
      if fReadOnly then wCheck:=MF_CHECKED
      else              wCheck:=MF_UNCHECKED;
      CheckMenuItem(hMenuWin, wNrMenu, MF_BYCOMMAND or wCheck);
      end;

   401 : MessageBox( hWin,'To see the simplicity of the menu creation',
                     'See how many code lines have been removed since tEx008.pp',
                     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('Example032');

// Creation of the main window
new(poMyMainWin, Init('tEx032. A little text editor. Very simple to create.'));

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

