home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / DSKCACHE / EVALCACH.ZIP / CONTROLS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-10-26  |  5.1 KB  |  210 lines

  1. {
  2. **************************************************************************
  3. CONTROLS.PAS -- Text-based controls for screen-oriented programs
  4. Copyright 1989 L. Brett Glass -- All rights reserved.
  5. **************************************************************************
  6. }
  7. unit Controls;
  8.  
  9. interface
  10.  
  11. uses CRT,CharScrn;
  12.  
  13. type
  14.   ControlPtr = ^Control;
  15.   Control = object (TitledCharObject)
  16.     next : ControlPtr;
  17.     {Methods}
  18.     procedure Draw; virtual;
  19.     procedure DispTitle; virtual;
  20.     procedure CheckChar(c : Char);
  21.     procedure Manipulate; virtual;
  22.     end;
  23.  
  24.   ButtonCtrl = object (Control)
  25.     proc : Procedure;
  26.     {Methods}
  27.     constructor Init;
  28.     procedure Draw; virtual;
  29.     procedure DispTitle; virtual;
  30.     procedure Manipulate; virtual;
  31.     end;
  32.  
  33.   WordCtrl = object (Control)
  34.     value, fieldSize, max, min, increment, bigIncrement : Word;
  35.     unitStr : String[12];
  36.     {Methods}
  37.     procedure Draw; virtual;
  38.     procedure DispTitle; virtual;
  39.     procedure Manipulate; virtual;
  40.     end;
  41.  
  42. const
  43.   firstHelpLine : Word = 0; {Line on which help message should appear
  44.                              when control is active. 0 means no message.}
  45. var
  46.   helpLineLeft : ColType;
  47.   helpLineWidth : ColType;
  48.  
  49. procedure DrawChain(head : ControlPtr);
  50.  
  51. implementation
  52.  
  53. {$F+}
  54. procedure DoNothing;
  55.   begin {DoNothing}
  56.   end;  {DoNothing}
  57. {$F-}
  58.  
  59. procedure DrawChain(head : ControlPtr);
  60.   begin {DrawChain}
  61.   while head <> NIL do
  62.     begin
  63.     head^.Draw;
  64.     head := head^.next
  65.     end
  66.   end;  {DrawChain}
  67.  
  68. procedure WriteWithFirstHigh(x : ColType; y : RowType; s : String);
  69.   var
  70.     p : ScreenParms;
  71.     i : Word;
  72.   begin {WriteWithFirstHigh}
  73.   GoToXY(x,y);
  74.   if Length(s) > 0 then
  75.     begin
  76.     HighVideo;
  77.     Write(s[1]);
  78.     LowVideo;
  79.     for i := 2 to Length(s) do
  80.       Write(s[i]);
  81.     end
  82.   end;  {WriteWithFirstHigh}
  83.  
  84. procedure Control.Draw;
  85.   begin {Control.Draw}
  86.   textAttr := color;
  87.   WriteWithFirstHigh(x,y,title);
  88.   end;  {Control.Draw}
  89.  
  90. procedure Control.DispTitle;
  91.   begin {Control.DispTitle}
  92.   Control.Draw
  93.   end;  {Control.DispTitle}
  94.  
  95. procedure Control.CheckChar(c : Char);
  96.   begin {Control.CheckChar}
  97.   if (Length(title) > 0) and (c = title[1]) then
  98.     Manipulate
  99.   else
  100.     if next <> NIL then
  101.       next^.CheckChar(c);
  102.   end;  {Control.CheckChar}
  103.  
  104. procedure Control.Manipulate; {Dummy}
  105.   begin {Manipulate}
  106.   end;  {Manipulate}
  107.  
  108.  
  109. constructor ButtonCtrl.Init;
  110.   begin {ButtonCtrl.Init}
  111.   proc := DoNothing;
  112.   end;  {ButtonCtrl.Init}
  113.  
  114. procedure ButtonCtrl.Draw;
  115.   begin {ButtonCtrl.Draw}
  116.   Control.Draw
  117.   end;  {ButtonCtrl.Draw}
  118.  
  119. procedure ButtonCtrl.DispTitle;
  120.   begin {ButtonCtrl.DispTitle}
  121.   Control.Draw
  122.   end;  {ButtonCtrl.DispTitle}
  123.  
  124. procedure ButtonCtrl.Manipulate;
  125.   begin {ButtonCtrl.Manipulate}
  126.   proc
  127.   end;  {ButtonCtrl.Manipulate}
  128.  
  129.  
  130. procedure WordCtrl.Draw;
  131.   begin {WordCtrl.Draw}
  132.   Control.Draw;
  133.   Write(value:fieldSize,' ',unitStr);
  134.   end; {WordCtrl.Draw}
  135.  
  136. procedure WordCtrl.DispTitle;
  137.   begin {WordCtrl.DispTitle}
  138.   Control.Draw
  139.   end;  {WordCtrl.DispTitle}
  140.  
  141. procedure WordCtrl.Manipulate;
  142.   const
  143.     helpMsg : String[59] = 'Use '#27' and '#26' to set value. Hold Ctrl to change value faster.';
  144.     helpMsg2 : String[48] = 'Press Enter to accept, ESC to restore old value.';
  145.   var
  146.     modValue : LongInt;
  147.     start1, start2 : Byte;
  148.     valueX : ColType;
  149.     valueY : RowType;
  150.  
  151.   procedure EraseHelp;
  152.     begin {EraseHelp}
  153.     FillBox(start1,firstHelpLine,Length(helpMsg),1,' ');
  154.     FillBox(start2,Succ(firstHelpLine),Length(helpMsg2),1,' ');
  155.     end;  {EraseHelp}
  156.  
  157.   begin {WordCtrl.Manipulate}
  158.   if firstHelpLine <> 0 then
  159.     begin
  160.     TextAttr := color;
  161.     LowVideo;
  162.     start1 := Pred((SCREENWIDTH div 2) + FIRSTCOL) - Length(helpMsg) div 2;
  163.     CharsToScreen(start1,firstHelpLine,Length(helpMsg),helpMsg[1]);
  164.     start2 := Pred((SCREENWIDTH div 2) + FIRSTCOL) - Length(helpMsg2) div 2;
  165.     CharsToScreen(start2,Succ(firstHelpLine),Length(helpMsg2),helpMsg2[1]);
  166.     Control.Draw;
  167.     HighVideo;
  168.     modValue := value;
  169.     valueX := WhereX;
  170.     valuey := WhereY;
  171.     repeat
  172.       Write(modValue:fieldSize,' ',unitStr);
  173.       case ReadKey of
  174.         #27 : {ESC}
  175.           begin
  176.           EraseHelp;
  177.           Draw;
  178.           Exit
  179.           end;
  180.         #13 : {Enter}
  181.           begin
  182.           EraseHelp;
  183.           value := modValue;
  184.           Draw;
  185.           Exit
  186.           end;
  187.         #0 : {Special key}
  188.           begin
  189.           case ReadKey of
  190.             #75 : {Left Arrow}
  191.               Dec(modValue,increment);
  192.             #77 : {Right Arrow}
  193.               Inc(modValue,increment);
  194.             #115 : {Ctrl-Left Arrow}
  195.               Dec(modValue,bigIncrement);
  196.             #116 : {Ctrl-Right Arrow}
  197.               Inc(modValue,bigIncrement);
  198.             end;
  199.           if modValue > max then
  200.             modValue := max
  201.           else
  202.             if modValue < min then
  203.               modValue := min;
  204.           end
  205.         end;
  206.       GoToXY(valueX,valueY)
  207.       until FALSE;
  208.     end
  209.   end;  {WordCtrl.Manipulate}
  210. end.