Скачать BarMenu - Simple OOP Unit for Text Menu

13.09.1991
Скачать файл (1,94 Кб)

Program MenuDemo;
Uses MenuUnit, Crt;
Var
  Choice: Integer;
  M: BBMenu;
BEGIN
  CLRSCR;
  M.Init( 25, 7, 18);
  M.AddPrompt('Open a New File');
  M.AddPrompt('Existing File');
  M.AddPrompt('Close A File');
  M.AddPrompt('Quit');
  Choice:= M.GetChoice;
  GotoXY(1, 24); ClrEol;
  Case Choice OF
    0: Writeln('No Choice Was Made');
    1: Writeln('Procedure NewFile Should Be Called');
    2: Writeln('Procedure ExistingFile Should Be Called');
    3: Writeln('Procedure CloseFile Should Be Called');
    4: Writeln('Quitting ...');
  END;
  M.Done;
END.
 
     --------- cut here: filename=menuunit.pas ----------
UNIT MenuUnit;
Interface
USES CRT;
CONST MaxMsgLen = 32;
TYPE
  MessageString = String[MaxMsgLen];
 
  EntryPointer = ^EntryType;
  EntryType = Object
    Prev, Next          : EntryPointer;
    XCor, YCor, ChoiceNo: Integer;
    Message             : MessageString;
    Constructor Init(ipr, inx: EntryPointer;
                      ix, iy, ic: Integer; im: MessageString);
    Procedure Draw(Selected: Boolean);
    Function GetChoice: Integer;
  END;
 
  BBMenu = Object
    XCor, YCor, Wid, Choices: Integer;
    FirstEntry, CurEntry    : EntryPointer;
    Constructor Init(ix, iy, iw: Integer);
    Destructor Done;
    Procedure AddPrompt(im: MessageString);
    Procedure Draw;
    Function GetChoice: Integer;
  END;
 
IMPLEMENTATION
  Constructor BBMenu.Init(ix, iy, iw: Integer);
  BEGIN
    XCor:= ix;
    YCor:= iy;
    Wid:= iw;
    IF Wid > MaxMsgLen THEN Wid:= MaxMsgLen;
    IF XCor + Wid > 80 THEN Wid:= 80 - XCor;
    FirstEntry:= NIL;
    Choices:= 0;
  END;
 
  Destructor BBMenu.Done;
  BEGIN
    IF FIrstEntry <> NIL THEN
    BEGIN
      FirstEntry^.Prev^.Next:= NIL;
      REPEAT
        CurEntry:= FirstEntry;
        FirstEntry:= FirstEntry^.Next;
        Dispose(CurEntry);
      UNTIL FirstEntry = NIL;
    END;
  END;
 
  Procedure BBMenu.AddPrompt(im: MessageString);
  VAR EP: EntryPointer;
  BEGIN
    INC(Choices);
    FillChar(im[Length(im) + 1], Wid - Length(im), ' ' );
    Im[0]:= Char(Wid);
    IF FirstEntry = NIL THEN
      BEGIN
       ...