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

                     HighSpeed Pascal for the Amiga

                         MENU DEMO UTILITY UNIT

                  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 : 06.11.91 : Final for first release

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

unit MenuUtil;

INTERFACE

uses Init, Intuition, Graphics;

procedure InitMenu(var menu : pMenu; title : string);
{ Create new menu strip and first entry therein }

procedure AddMenu (var menu : pMenu; rootmenu : pMenu; title : string);
{ Add menu to menu strip }

procedure AddItem (menu : pMenu; title  : string;
                                 flg    : integer;
                                 mutual : longint;
                                 cmdCh  : char);
{ Add item to given menu }

IMPLEMENTATION

procedure InitMenu(var menu : pMenu; title : string);
begin
  new(menu);
  with menu^ do begin
    NextMenu   := NIL;                       { No more menus }
    LeftEdge   := 0;
    TopEdge    := 0;
    Width      := length(title) * 8 + 6;     { Amiga uses 8x8 std font }
    Height     := 0;
    Flags      := MENUENABLED;               { Yes, this menu is enabled }
    MenuName   := CStrConstPtr(title);
    FirstItem  := NIL                        { No items yet }
  end
end;

procedure AddMenu (var menu : pMenu; rootmenu : pMenu; title  : string);
var
  m : pMenu;
begin
  new(menu);

  m := rootmenu;                                { Make last menu in linked- }
  while m^.NextMenu <> NIL do m := m^.NextMenu; { list point at new }
  m^.NextMenu := menu;

  with menu^ do begin
    NextMenu   := NIL;                          { This is last menu in list }
    LeftEdge   := m^.LeftEdge + m^.Width;       { Calc position }
    TopEdge    := 0;
    Width      := length(title) * 8 + 6;
    Height     := 0;
    Flags      := MENUENABLED;
    MenuName   := CStrConstPtr(title);
    FirstItem  := NIL                           { No items yet }
  end;
end;

procedure AddItem (menu : pMenu; title  : string;
                                 flg    : integer;
                                 mutual : longint;
                                 cmdCh  : char);
var
  item    : pMenuItem;        { New item }
  i       : pMenuItem;
  textrec : pIntuiText;       { Item title is contained in this }
  MaxWid  : integer;          { Larget width in all items in this menu }
begin
  new(textrec);                        { Make new IntuiText for item title }
  with textrec^ do begin
    FrontPen  := 0;                    { Colors }
    BackPen   := 1;
    DrawMode  := JAM2;                 { Another of these wierd names... }
    LeftEdge  := 0;
    TopEdge   := 0;
    ITextFont := NIL;                  { Use std. font }
    IText     := CStrConstPtr(title);
    NextText  := NIL                   { No more texts }
  end;

  MaxWid := Max(Menu^.Width,           { Menu items are at least as wide }
                length(title) * 8 + 6);{ as the menu title (looks nice)  }
  new(item);

  i := menu^.FirstItem;                   { Update linked-list and find }
  if i = NIL then menu^.FirstItem := item { widest item box in the list }
  else begin
    while i^.NextItem <> NIL do begin
      MaxWid := Max(MaxWid, i^.Width);
      i := i^.NextItem
    end;
    i^.NextItem := item
  end;

  with item^ do begin
    NextItem      := NIL;              { This is last item in list }
    LeftEdge      := 0;
    if i = NIL then TopEdge := 0       { Calc y-position }
               else TopEdge := i^.TopEdge + i^.Height + 1;
    Width         := MaxWid;
    Height        := 8;
    Flags         := flg or ITEMTEXT;  { Always ITEMTEXT in this unit! }
    MutualExclude := mutual;
    ItemFill      := textrec;          { The IntuiText structure }
    SelectFill    := NIL;              { No alternate image }
    Command       := shortint(ord(cmdCh));   { Insert command char }
    SubItem       := NIL;              { No sub menus }
    NextSelect    := MENUNULL
  end;
  i := menu^.FirstItem;                { Make all item boxes equally wide }
  while i <> NIL do begin
    i^.Width := MaxWid;
    i := i^.NextItem
  end
end;

end.
