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

  1.  
  2. (*    Panel Unit  --  EgaVga Graphic Radio-Button Panel
  3.  
  4.         Turbo Pascal 4 Unit
  5.  
  6.         **************************************************
  7.         NOTE:   All compilations should have the boolean
  8.                 evaluation option set to "short-circuit"
  9.         **************************************************
  10.  
  11.     Supports MicroSoft compatible Mouse Driver
  12.  
  13.     June 26, 1990  --  Michael Kelly  --  Version 1.01      *)
  14.  
  15. Unit Panel;
  16.  
  17. InterFace
  18. Uses PanelDrv, Graph, Int16, Mouse;
  19.  
  20.     Const
  21.     Gap = 25;       { pixel space between buttons }
  22.     MaxButton = 10; { maximum number of buttons on panel }
  23.  
  24.     Type
  25.     ButtonState = (Up, Down);
  26.     ButtonTag = String[80]; { option string }
  27.     Button = Record         { button and associated option string }
  28.         State          : ButtonState;
  29.         UpperLeftX,
  30.         UpperLeftY,
  31.         TagColor    : Integer;
  32.                 Tag         : ButtonTag;
  33.     End;
  34.  
  35.     Buttons    = Array[1..MaxButton] of Button;
  36.  
  37.     ButtonDescriptor = Record   { attributes common to 3D buttons }
  38.         RadioButton    : Buttons;
  39.                 SideLength,
  40.         BorderWidth,
  41.         HighLightEdge,
  42.         ShadowEdge,
  43.         Normal,
  44.         TagHighLight,
  45.         TagNormal    : Integer;
  46.     End;
  47.  
  48.     PanelDescriptor = Record    { defines a radio-button panel }
  49.         UpperLeftX,
  50.         UpperLeftY,
  51.         LowerRightX,
  52.         LowerRightY,
  53.         NumButtons, CurrentButton    : Integer;
  54.         ButtonStyle            : ButtonDescriptor;
  55.     End;
  56.  
  57. (*
  58.  *  draws the panel defined by the PanelDescriptor parameter
  59.  *
  60.  *  returns:
  61.  *              True on success
  62.  *
  63.  *              False on error
  64.  *)
  65. Function DrawPanel(Var ButtonPanel : PanelDescriptor) : Boolean;
  66.  
  67. (*
  68.  *  polling function updates button panel appearence to reflect
  69.  *  keyboard and mouse input, and determines if a button has been
  70.  *  pressed
  71.  *
  72.  *  note:   does not wait for input
  73.  *
  74.  *  returns:
  75.  *              0 if no button pressed
  76.  *
  77.  *              user selection in range 1 to MaxButton if button pressed
  78.  *)
  79. Function ButtonPress(Var ButtonPanel : PanelDescriptor) : Integer;
  80.  
  81. Implementation
  82.  
  83. (*
  84.  *  draws the button outline using highlighting and shading
  85.  *
  86.  *  returns:
  87.  *              True on success
  88.  *
  89.  *              False on error
  90.  *)
  91. Function ButtonBorder(
  92.     Var ButtonInfo : ButtonDescriptor; ThisButton : Integer) : Boolean;
  93.  
  94.     Var
  95.     n, TopLColor, BotRColor, LSide,
  96.     xl, xr, yu, yd : Integer;
  97. Begin
  98.   With ButtonInfo do
  99.   Begin
  100.     If RadioButton[ThisButton].State = Up then
  101.     Begin
  102.       TopLColor := HighlightEdge;
  103.       BotRColor := ShadowEdge;
  104.     End
  105.     Else
  106.     Begin
  107.       TopLColor := ShadowEdge;
  108.       BotRColor := HighLightEdge;
  109.     End;
  110.     LSide := SideLength;
  111.     xl := RadioButton[ThisButton].UpperLeftX;
  112.     xr := xl + LSide;
  113.     yu := RadioButton[ThisButton].UpperLeftY;
  114.     yd := yu + LSide;
  115.     SetColor(TopLColor);
  116.     For n := 0 to BorderWidth - 1 do
  117.     Begin
  118.       Line(xl+n, yd-n, xl+n, yu+n);
  119.       Line(xl+n, yu+n, xr-n, yu+n);
  120.     End;
  121.     SetColor(BotRColor);
  122.     For n := 0 to BorderWidth - 1 do
  123.     Begin
  124.       Line(xr-n, yu+n+1, xr-n, yd-n);
  125.       Line(xr-n, yd-n, xl+n+1, yd-n);
  126.     End;
  127.   End; { With ButtonInfo }
  128.   ButtonBorder := GraphResult = GrOk;
  129. End;
  130. (* ButtonBorder *)
  131.  
  132. (*
  133. *  adjusts button state, highlighting and shading, then calls
  134. *  ButtonBorder to make the 3D button appear to be "Up"
  135. *
  136. *  returns:
  137. *              True on success
  138. *
  139. *              False on error
  140. *)
  141. Function ButtonUp(
  142.     Var ButtonInfo : ButtonDescriptor; ThisButton : Integer) : Boolean;
  143.  
  144. Begin
  145.   With ButtonInfo do
  146.   Begin
  147.     RadioButton[ThisButton].State := Up;
  148.     ButtonUp := ButtonBorder(ButtonInfo, ThisButton);
  149.   End; { With ButtonInfo }
  150. End;
  151. (* ButtonUp *)
  152.  
  153. (*
  154. *  adjusts button state, highlighting and shading, then calls
  155. *  ButtonBorder to make the 3D button appear to be "Down"
  156. *
  157. *  returns:
  158. *              True on success
  159. *
  160. *              False on error
  161. *)
  162. Function ButtonDown(
  163.     Var ButtonInfo : ButtonDescriptor; ThisButton : Integer) : Boolean;
  164.  
  165. Begin
  166.   With ButtonInfo do
  167.   Begin
  168.     RadioButton[ThisButton].State := Down;
  169.     ButtonDown := ButtonBorder(ButtonInfo, ThisButton);
  170.   End; { With ButtonInfo }
  171. End;
  172. (* ButtonDown *)
  173.  
  174. (*
  175. *  sets button state to "Up", then draws button and associated
  176. *  option string (ButtonTag)
  177. *
  178. *  returns:
  179. *              True on success
  180. *
  181. *              False on error
  182. *)
  183. Function DrawButton(
  184.     Var ButtonInfo : ButtonDescriptor; ThisButton : Integer) : Boolean;
  185.  
  186.     Var
  187.     x, y, color    : Integer;
  188.  
  189. Begin
  190.   If not ButtonUp(ButtonInfo, ThisButton) then
  191.   Begin
  192.     DrawButton := False;
  193.     exit;
  194.   End;
  195.   With ButtonInfo do
  196.   Begin
  197.     x := RadioButton[ThisButton].UpperLeftX + SideLength + Gap;
  198.     y := RadioButton[ThisButton].UpperLeftY + SideLength shr 2;
  199.     SetColor(RadioButton[ThisButton].TagColor);
  200.     OutTextXY(x, y, RadioButton[ThisButton].Tag);
  201.   End; { With ButtonInfo }
  202.   DrawButton := GraphResult = GrOk;
  203. End;
  204. (* DrawButton *)
  205.  
  206. Function DrawPanel(Var ButtonPanel : PanelDescriptor) : Boolean;
  207.     Var
  208.     HighLight, Shadow, Normal, TextHighlight, TextNormal     : Integer;
  209.     xl, xr, yu, yd, k    : Integer;
  210. Begin
  211.   If MouseVisible then
  212.     ToggleMouseVisibility;
  213.   With ButtonPanel do
  214.   Begin
  215.     HighLight := ButtonStyle.HighLightEdge;
  216.     Shadow := ButtonStyle.ShadowEdge;
  217.     Normal := ButtonStyle.Normal;
  218.     TextNormal := ButtonStyle.TagNormal;
  219.     TextHighLight := ButtonStyle.TagHighLight;
  220.     xl := UpperLeftX;
  221.     xr := LowerRightX;
  222.     yu := UpperLeftY;
  223.     yd := LowerRightY;
  224.     SetColor(HighLight);
  225.     For k := 0 to ButtonStyle.BorderWidth + 2 do
  226.     Begin
  227.       Line(xl+k, yd-k, xl+k, yu+k);
  228.       Line(xl+k, yu+k, xr-k, yu+k);
  229.     End;
  230.     SetColor(Shadow);
  231.     For k := 0 to ButtonStyle.BorderWidth + 2 do
  232.     Begin
  233.       Line(xr-k, yu+k+1, xr-k, yd-k);
  234.       Line(xr-k, yd-k, xl+k+1, yd-k);
  235.     End;
  236.     SetFillStyle(SolidFill, Normal);
  237.     Bar(xl+k, yu+k, xr-k, yd-k);
  238.     For k := 1 to NumButtons do
  239.     Begin
  240.       If k = CurrentButton then
  241.     ButtonStyle.RadioButton[k].TagColor := TextHighlight
  242.       Else
  243.     ButtonStyle.RadioButton[k].TagColor := TextNormal;
  244.       If not DrawButton(ButtonStyle, k) then
  245.       Begin
  246.     DrawPanel := False;
  247.     exit;
  248.       End;
  249.     End;
  250.   End; { With ButtonPanel }
  251.   If MouseOn then
  252.     ToggleMouseVisibility;
  253.   DrawPanel := True;
  254. End;
  255. (* DrawPanel *)
  256.  
  257. (*
  258. *  checks mouse, if active, for "left mouse button down" state
  259. *  and if True, checks mouse position to determine if a button
  260. *  has been "clicked on"
  261. *
  262. *  returns:
  263. *              0 if no button "clicked on"
  264. *
  265. *              number of button in range 1 to MaxButton "clicked on"
  266. *)
  267. Function ButtonClick(Var ButtonPanel : PanelDescriptor) : Integer;
  268.     Var
  269.     MX, MY, MouseButtonState : Word;
  270.     wid, len, k : Integer;
  271.     RButton : Buttons;
  272.     ThisButton : Button;
  273.  
  274. Begin
  275.   If not GetMousePosition(MX, MY, MouseButtonState) then
  276.   Begin
  277.     ButtonClick := 0;
  278.     Exit;
  279.   End;
  280.   If (MouseButtonState and 1) <> 1 then
  281.   Begin
  282.     ButtonClick := 0;
  283.     Exit;
  284.   End;
  285.   With ButtonPanel do
  286.   Begin
  287.     RButton := ButtonStyle.RadioButton;
  288.     wid := ButtonStyle.BorderWidth;
  289.     len := ButtonStyle.SideLength;
  290.     For k := 1 to NumButtons do
  291.     Begin
  292.       ThisButton := RButton[k];
  293.       With ThisButton do
  294.       Begin
  295.     If (MX >= (UpperLeftX - wid)) and
  296.       (MX <= (UpperLeftX + len + wid)) and
  297.     (MY >= (UpperLeftY - wid)) and
  298.     (MY <= (UpperLeftY + len + wid)) then
  299.     Begin
  300.       ButtonClick := k;
  301.       Exit;
  302.     End;
  303.       End; { With ThisButton }
  304.     End; { For }
  305.   End; { With ButtonPanel }
  306.   ButtonClick := 0;
  307. End;
  308. (* ButtonClick *)
  309.  
  310. Function ButtonPress(Var ButtonPanel : PanelDescriptor) : Integer;
  311.     Var
  312.     KeyCode, ScanCode    : Byte;
  313.     KeyState, Result, Done    : Boolean;
  314.     HiLite, Norm, k, choice    : Integer;
  315.     OldButton, ClickedOn    : Integer;
  316.  
  317. Begin
  318.   KeyCode := 0;
  319.   ScanCode := 0;
  320.   ClickedOn := 0;
  321.   Result := False;
  322.   Done := False;
  323.   choice := 0;
  324.   KeyState := key_ready(KeyCode, ScanCode);
  325.   If KeyState then
  326.     get_key(KeyCode, ScanCode);
  327.   If (not KeyState) and (MouseOn) then
  328.   Begin
  329.     ClickedOn := ButtonClick(ButtonPanel);
  330.     If (ClickedOn <> 0) and (ClickedOn <= ButtonPanel.NumButtons) then
  331.     Begin
  332.       KeyState := True;
  333.       If ClickedOn = ButtonPanel.CurrentButton then
  334.     KeyCode := CReturn
  335.       Else If ClickedOn = (ButtonPanel.CurrentButton - 1) then
  336.     ScanCode := UpKey
  337.       Else If ClickedOn = (ButtonPanel.CurrentButton + 1) then
  338.     ScanCode := DownKey
  339.       Else
  340.       Begin
  341.     With ButtonPanel do
  342.     Begin
  343.       Norm := ButtonStyle.TagNormal;
  344.       HiLite := ButtonStyle.TagHighLight;
  345.       OldButton := CurrentButton;
  346.       CurrentButton := ClickedOn;
  347.       ButtonStyle.RadioButton[OldButton].TagColor := Norm;
  348.       ButtonStyle.RadioButton[CurrentButton].TagColor := HiLite;
  349.       If MouseVisible then
  350.         ToggleMouseVisibility;
  351.       Result := DrawButton(ButtonStyle, OldButton);
  352.       Result := DrawButton(ButtonStyle, CurrentButton);
  353.       If not MouseVisible then
  354.         ToggleMouseVisibility;
  355.       KeyState := False;
  356.       Done := True;
  357.     End;  { With ButtonPanel }
  358.       End;  { Else }
  359.     End;  { If ClickedOn }
  360.   End;  { If MouseOn }
  361.   If KeyState then
  362.   Begin
  363.     If (MouseOn) and (MouseVisible) then
  364.       ToggleMouseVisibility;
  365.     With ButtonPanel do
  366.     Begin
  367.       HiLite := ButtonStyle.TagHighLight;
  368.       Norm := ButtonStyle.TagNormal;
  369.       Result := False;
  370.       Done := False;
  371.       If KeyCode = 0 then
  372.       Case ScanCode of
  373.     DownKey, UpKey:
  374.     Begin
  375.       OldButton := CurrentButton;
  376.       If ScanCode = DownKey then
  377.       Begin
  378.         If CurrentButton < NumButtons then
  379.           inc(CurrentButton)
  380.         Else
  381.           CurrentButton := 1;
  382.       End;
  383.       If ScanCode = UpKey then
  384.       Begin
  385.         If CurrentButton > 1 then
  386.           dec(CurrentButton)
  387.         Else
  388.           CurrentButton := NumButtons;
  389.       End;
  390.       ButtonStyle.RadioButton[OldButton].TagColor := Norm;
  391.       ButtonStyle.RadioButton[CurrentButton].TagColor := HiLite;
  392.       Result := DrawButton(ButtonStyle, OldButton);
  393.       Result := DrawButton(ButtonStyle, CurrentButton);
  394.       Done := True;
  395.     End;
  396.     HomeKey, EndKey:
  397.     Begin
  398.       OldButton := CurrentButton;
  399.       If ScanCode = EndKey then
  400.         CurrentButton := NumButtons
  401.       Else
  402.         CurrentButton := 1;
  403.       If OldButton <> CurrentButton then
  404.       Begin
  405.         ButtonStyle.RadioButton[OldButton].TagColor := Norm;
  406.         ButtonStyle.RadioButton[CurrentButton].TagColor := HiLite;
  407.         Result := DrawButton(ButtonStyle, OldButton);
  408.         Result := DrawButton(ButtonStyle, CurrentButton);
  409.         Done := True;
  410.       End;
  411.     End;
  412.       End; { Case }
  413.       If KeyCode <> 0 then
  414.       Case KeyCode of
  415.     CReturn :
  416.     Begin
  417.       Result := ButtonDown(ButtonStyle, CurrentButton);
  418.       choice := CurrentButton;
  419.       Done := True;
  420.     End;
  421.       End;  { Case }
  422.     End;  { With ButtonPanel }
  423.   End;  { If KeyState }
  424.   If (MouseOn) and (not MouseVisible) then
  425.     ToggleMouseVisibility;
  426.   ButtonPress := choice;
  427. End;
  428. (* ButtonPress *)
  429.  
  430. End.
  431.