{ Test of the unit wMenus0.pp
  To have contextual menus.
  --------------------------------------------}
program tEx036;

// 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
// ,wMenus0 // ??????  not used if you belive the compiler ?????? I don't understand.
 ,wDebug0
 ,Windows;

type
  PMyWindow = ^TMyWindow;
  TMyWindow = object(TWindow)
  //"""""""""""""""""""""""""

  constructor Init(strWinTitlePrm:ShortString);

  procedure WMPaint; virtual;

  procedure WMContextMenu(lPar:LPARAM); virtual;
  // Received if right click.
  // It will open a contextual menu

  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(TWindow)

constructor TMyWindow.Init(strWinTitlePrm:ShortString);
//=====================================================
begin
inherited Init(strWinTitlePrm);
// This line is all what is needed to have a status line.
new(poStatusLine, Init(@Self));
end;  // TMyWindow.Init

procedure TMyWindow.WMPaint;
//==========================
Var
  rRec        : rect;
  dc          : hdc;
  ps          : paintstruct;

begin
dc:=BeginPaint(hWin, @ps);
GetClientRect(hWin, @rRec);
DrawText( dc, 'Example 036 : Right click for a menu',-1,@rRec,
          DT_SINGLELINE or DT_CENTER or DT_VCENTER);
EndPaint(hWin, ps);
end;  // TMyWindow.WMPaint

procedure TMyWindow.WMContextMenu(lPar:LPARAM);
//=============================================
// Received if right click.
// It will open a contextual menu
//var  hMnuPopup   : HMENU;  rRec        : RECT;
begin
// The simpler way to open the contextual menu :
poMenu^.MenuPopupTrack(hWin, 11000, lPar, TPM_RIGHTBUTTON);

// Here it is a good idea to delete from memory the created popup menu.
// If you forget it, it doesn't matter, it will be
// destroyed automatically the next time you create the popup menu.
poMenu^.MenuPopupDestroy;

{ The complicate way to open the contextual menu :
hMnuPopup:=poMenu^.MenuPopupMsgCreate(11000);

// DBW0(1, 'hMnuPopup=' + ITS(hMnuPopup));

if (hMnuPopup <> 0) then begin
//   GetWindowRect(hWin, rRec);
   rRec.Left:=0;
   rRec.Top:=0;
   rRec.Right:=0;
   rRec.Bottom:=0;

   TrackPopupMenu(
     hMnuPopup,
     0                    // screen-position and mouse-button flags
//     or TPM_RIGHTALIGN
//     or TPM_LEFTALIGN
//     or TPM_CENTERALIGN
//     or TPM_LEFTBUTTON
     or TPM_RIGHTBUTTON
     or 0,
     LoWord(lPar),         // horizontal position, in screen coordinates
     HiWord(lPar),         // vertical position, in screen coordinates
     0,                    // reserved, must be zero
     hWin,                 // handle of owner window
     rRec
     );
   end;
}

end;  // WTMyWindow.MContextMenu

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;

// for the menus,  lPar = 0
//=========================
wNrMenu   := LoWord(wPar);
Case wNrMenu Of
   100..110 : DBW0(1, 'Menu select=' + ITS(wNrMenu));

   1001 : MessageBox( hWin,
          'Right click to have a contextual menu',
          'Test contextual menus', MB_OK Or MB_IconInformation);
   else WMCommand:=inherited WMCommand(wPar, lPar);
   end;  // Case NrMenu Of
end;  // TMyWindow.WMCommand

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
   WM_Paint   : WMPaint; // must redraw the client area part.
   WM_ContextMenu : WMContextMenu(lPar);

   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('Example036');

// Creation of the main window
new(poMyMainWin, Init('tEx036.  Test the wMenus0.pp unit, to have contextual menus'));

// Change the color of the background of the main window.
// to have the desktop color, just for beauty.
//oApp.WindowClass.hbrBackground :=GetStockObject(GRAY_BRUSH); // COLOR_BACKGROUND+1;

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

