home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_01 / 2n01040a < prev    next >
Text File  |  1990-06-26  |  4KB  |  176 lines

  1.  
  2. (*
  3.         GuiMenu  --  Unit to Manage a Graphical Radio-Button Menu
  4.  
  5.         Turbo Pascal 4 Unit
  6.  
  7.         **************************************************
  8.         NOTE:   All compilations should have the boolean
  9.                 evaluation option set to "short-circuit"
  10.         **************************************************
  11.  
  12.         June 26, 1990  --  Michael Kelly  --  Version 1.01      *)
  13.  
  14. Unit GuiMenu;
  15.  
  16. Interface
  17.  
  18. Uses Panel, PanelDrv, Graph, Mouse, Crt;
  19.  
  20.     Type
  21.         PanelMenu = Array[1..MaxButton] of ButtonTag; { option strings }
  22.  
  23.     Var
  24.         Driver, Mode    : Integer;
  25.         InGraphicsMode  : Boolean;
  26.  
  27. (*
  28.  *  high level function to create graphics radio-button menu
  29.  *  and return user menu selection
  30.  *
  31.  *  returns:
  32.  *              -1 on error
  33.  *
  34.  *              user choice in range 1 to MaxButton on success
  35.  *)
  36. Function MainMenu(Var MenuChoices : PanelMenu) : Integer;
  37.  
  38. (*
  39.  *  calls CloseGraph to restore video if InGraphicsMode = True
  40.  *)
  41. Procedure CloseMenu;
  42.  
  43. Implementation
  44.  
  45. (*
  46.  *  sets up graphics mode and draws screen background
  47.  *
  48.  *  returns:
  49.  *              True on success and sets InGraphicsMode to True
  50.  *
  51.  *              False on error
  52.  *)
  53. Function PrepScreen : Boolean;
  54.     Var
  55.         MaxX, MaxY, counter : Integer;
  56.     Ok            : Boolean;
  57.  
  58. Begin
  59.   If not InGraphicsMode then
  60.   Begin
  61.     DetectGraph(Driver, Mode);
  62.     Ok := (Driver = EGA) and (Mode = EGAHi);
  63.     If (not Ok) or (RegisterBgiDriver(@EgaVgaDriverProc) < 0) then
  64.     Begin
  65.       PrepScreen := False;
  66.       Exit;
  67.     End;
  68.     InitGraph(Driver, Mode, '');
  69.     If GraphResult <> GrOk then
  70.     Begin
  71.       PrepScreen := False;
  72.       Exit;
  73.     End;
  74.     InGraphicsMode := True;
  75.   End;
  76.   SetVisualPage(1);
  77.   SetColor(Blue);
  78.   MaxY := GetMaxY;
  79.   MaxX := GetMaxX;
  80.   For counter := 0 to MaxY do
  81.     Line(0, counter, MaxX, counter);
  82.   PrepScreen := True;
  83. End;
  84. (* PrepScreen *)
  85.  
  86. (*
  87.  *  loads PanelDescriptor with screen coordinates, colors
  88.  *  and other data to define the appearance and configuration 
  89.  *  of the radio-button panel
  90.  *)
  91. Procedure LoadButtons(Var MyPanel : PanelDescriptor);
  92.     Var
  93.         butlen, xcoord, ycoord, k   : Integer;
  94.  
  95. Begin
  96.   butlen := 10;
  97.   With MyPanel do
  98.   Begin
  99.     UpperLeftX := 100;
  100.     UpperLeftY := 50;
  101.     LowerRightX := 540;
  102.     LowerRightY := 300;
  103.     NumButtons := MaxButton;
  104.     CurrentButton := 1;
  105.     xcoord := UpperLeftX + Gap;
  106.     ycoord := UpperLeftY + Gap;
  107.     For k := 1 to MaxButton do
  108.     Begin
  109.       With ButtonStyle do
  110.       Begin
  111.     RadioButton[k].State := Up;
  112.     RadioButton[k].UpperLeftX := xcoord;
  113.     RadioButton[k].UpperLeftY := ycoord;
  114.     ycoord := ycoord + butlen + butlen;
  115.       End;  { With ButtonStyle }
  116.     End;
  117.     With ButtonStyle do
  118.     Begin
  119.       SideLength := butlen;
  120.       BorderWidth := 3;
  121.       HighLightEdge := LightCyan;
  122.       ShadowEdge := DarkGray;
  123.       Normal := Cyan;
  124.       TagHighlight := White;
  125.       TagNormal := Black;
  126.     End;  { With ButtonStyle }
  127.   End;  { With MyPanel }
  128. End;
  129. (* LoadButtons *)
  130.  
  131. Function MainMenu(Var MenuChoices : PanelMenu) : Integer;
  132.     Var
  133.         Panel   : PanelDescriptor;
  134.         k       : Integer;
  135.  
  136. Begin
  137.   LoadButtons(Panel);
  138.   For k := 1 to MaxButton do
  139.     Panel.ButtonStyle.RadioButton[k].Tag := MenuChoices[k];
  140.   If (not PrepScreen) or (not DrawPanel(Panel)) then
  141.   Begin
  142.     MainMenu := -1;
  143.     Exit;
  144.   End;
  145.   SetVisualPage(0);
  146.   MouseInit;
  147.   If MouseOn then
  148.     ToggleMouseVisibility;
  149.   Repeat
  150.     k := ButtonPress(Panel);
  151.   Until (k <> 0) and (MenuChoices[k] <> '');
  152.   If MouseVisible then
  153.   Begin
  154.     ToggleMouseVisibility;
  155.     MouseReset;
  156.   End;
  157.   MainMenu := k;
  158. End;
  159. (* MainMenu *)
  160.  
  161. Procedure CloseMenu;
  162. Begin
  163.   If InGraphicsMode then
  164.   Begin
  165.     CloseGraph;
  166.     InGraphicsMode := False;
  167.   End;
  168. End;
  169. (* CloseMenu *)
  170.  
  171. { Initialization }
  172.  
  173. Begin
  174.   InGraphicsMode := False;
  175. End.
  176.