{----------------------------------------------------------------------------

                      HighSpeed Pascal for the Amiga

                                MENU DEMO

                   Programmed by Martin Eskildsen 1991

                   Copyright (c) 1991 by D-House I ApS
                          All rights reserved


  Version : Date (dd.mm.yy) : Comment
  -----------------------------------
    1.00 : 21.07.91 : First version
    1.01 : 17.09.91 : Modified for new libraries
    1.02 : 06.11.91 : Final for first release
    1.10 : 22.04.92 : Updated to use correct unsigned types, Wait() fixed

----------------------------------------------------------------------------}

program MenuDemo;

uses Init, Intuition, Exec, Graphics, MenuUtil;

var
  BaseMenu : pMenu;           { The first menu in the menu strip }
  m        : pMenu;           { Used temporarily }
  bool     : boolean;         { Dummy }
  i        : integer;         { Index }
  iPtr     : pIntuiText;

procedure SelectLoop;
var
  dummy    : longint;
  Imessage : pIntuiMessage;   { The messages sent to this program }
  class    : long;            { Message class }
  code     : word;            { Message code }
  quit     : boolean;         { TRUE = done showing menu }
  mNum     : integer;         { Menu number selected }
  iNum     : integer;         { Item number selected }
  s        : string;          { A string }
  ItemPtr  : pMenuItem;       { Pointer to a menu item structure }
  TextPtr  : pIntuiText;      { Pointer to a IntuiText structure }

begin
  quit := FALSE;                           { Not done yet }
  repeat
    dummy := Wait(BitMask(OutputWindow^.UserPort^.MP_SIGBIT));
                                        { Wait for something to happen }

    Imessage := pIntuiMessage(GetMsg(OutputWindow^.UserPort));
                                        { Find out what }
    while Imessage <> NIL do begin
      class := Imessage^.class;         { Save for later use }
      code  := Imessage^.code;
      ReplyMsg(pMessage(Imessage));     { Accept this message }
      case class of
        MENUPICK     : begin            { The right mouse key was pressed }
                         if code <> MENUNULL then begin { Pointed at menu? }
                           mNum := MenuNum(code);
                           iNum := ItemNum(code);
                           quit := (mNum = 0) and (iNum = 2);
                                   { Menu 1 }     { Quit item }
                           ItemPtr := pMenuItem(ItemAddress(BaseMenu, code));
                           TextPtr := pIntuiText(ItemPtr^.ItemFill);
                           s := 'You chose ' + RetrieveStr(TextPtr^.IText);
                         end
                         else s := '-- You made no selection --';
                         while length(s) < 60 do s := s + ' ';
                         with OutputWindow^ do begin
                           Move_(RPort, 20,20);         { Where to write }
                           Text_(RPort, @s[1], length(s))   { this text }
                         end
                       end;
        CLOSEWINDOW_ : quit := TRUE
      end;
      Imessage := pIntuiMessage(GetMsg(OutputWindow^.UserPort)) { Get next }
    end;
  until quit
end;

begin
  if PrepareEnvironment('Menu') then begin

    Message('First, we''ll build the menu structures');
    InitMenu(BaseMenu, 'Menu 1 ');
    AddItem (BaseMenu, 'Simple item',    ITEMENABLED or HIGHCOMP, $0000, #0);
    AddItem (BaseMenu, 'Disabled item',                 HIGHCOMP, $0000, #0);
    AddItem (BaseMenu, 'Quit          ', ITEMENABLED or HIGHCOMP
                                                     or COMMSEQ , $0000, 'Q');
    AddMenu (m, BaseMenu, 'Menu 2 ');
    for i := 1 to 20 do
      AddItem (m, 'Item ' + chr(i + ord('@')), ITEMENABLED or HIGHCOMP, $0000, #0);

    Message('Menus need a window, so let''s make one');
    with OutputWinDef do                     { Tell Intuition that we want }
      IDCMPflags := IDCMPflags or MENUPICK;  { menu events }
    OpenOutputWindow;

    Message('Let''s attach the menu strip to the Output window');

    bool := SetMenuStrip(OutputWindow, BaseMenu);

    Inform('Now, activate the Output window and press right mouse button');
    SelectLoop;
    
    Inform('Let''s change Menu 1 a bit. Please examine the menu');
    ClearMenuStrip(OutputWindow);
    iPtr := BaseMenu^.FirstItem^.ItemFill;
    iPtr^.IText := CStrConstPtr('Yo! I changed!');
    bool := SetMenuStrip(OutputWindow, BaseMenu);
    SelectLoop;

    ClearMenuStrip(OutputWindow);
    Message('How nice. The menu strip has been removed and so will the Output window');
    CloseOutputWindow;

    CloseDown
  end
end.
