home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / ULDIAL.ZIP / ULROOT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-06-25  |  12.7 KB  |  421 lines

  1. (***********************************************************************
  2.      General Ojbects as Enhancements to Turbo Power OOP Professional
  3.                   New Communications Technology, Inc.
  4.                              Version 1.0
  5.                           by John Poindexter
  6.                              June 1, 1990
  7. ************************************************************************)
  8. {$I ULDEFINE.INC}
  9.  
  10. {$IFNDEF roDEBUG}
  11. {$A-,B-,E+,F+,I+,N-,O+,R-,S-,V-}
  12. {$ELSE}
  13. {$A-,B-,E+,F+,I+,N-,O+,R+,S+,V-}
  14. {$ENDIF}
  15.  
  16. Unit ULRoot;
  17.  
  18. Interface
  19.  
  20. Uses OpRoot, OpCrt, OpColor, OpMouse, OpInline, OpString, OpCmd,
  21.      OpFrame, OpWindow, OpPick, OpField, OpEntry, OpKey;
  22.  
  23. const
  24.   ucULRoot  = 200;
  25.   ucULDial  = 201;
  26.   ucULData  = 202;
  27.   ucULDbase = 203;
  28.  
  29.   { Error Codes and Messages }
  30.   ecTooManyKeys         = 3001;
  31.   ecNoLists             = 3002;
  32.   ecKeyTooLong          = 3003;
  33.   ecInvalidDbaseNum     = 3004;
  34.   ecInvalidIndex        = 3005;
  35.   ecNoVRecBuf           = 3006;
  36.   ecRebuildReq          = 3007;
  37.   ecTooManyVar          = 3008;
  38.   ecDuplicateKeys       = 3009;
  39.   ecNoChoice            = 3010;
  40.  
  41.   emTooManyKeys         : string[13] = 'Too many keys';
  42.   emNoLists             : string[24] = 'Desc or Key Lists failed';
  43.   emKeyTooLong          : string[15] = 'Key is too long';
  44.   emInvalidDbaseNum     : string[31] = 'Requested Dbase not initialized';
  45.   emInvalidIndex        : string[33] = 'Invalid index for data descriptor';
  46.   emNoVRecBuf           : string[34] = 'VRec buffer too small or no memory';
  47.   emRebuildReq          : string[38] = 'Index is damaged. Select Ok to rebuild';
  48.   emTooManyVar          : string[36] = 'May only use 1 variable length field';
  49. { emESNotInitialized    : string[28] = 'Entry Screen not initialized';}
  50.   emNoChoice            : string[23] = 'DialogBox has no choice';
  51.   emISAM                : string[4]  = 'ISAM';
  52.   emStatusHandlerFail   : string[20] = 'Status Handler failed';
  53.   emPossibleRecovery    : string[35] = 'Recovery may be possible with Retry';
  54.  
  55.   mmAnyKeytoContinue    : string[27] = ' Press any key to continue ';
  56.  
  57.   SafetyBuffer : string[20] = '12345678901234567890';
  58.  
  59.   ULColorSet : ColorSet = (
  60.     TextColor       : BlackonLtGray; TextMono       : $07;
  61.     CtrlColor       : WhiteonBlue;   CtrlMono       : $07;
  62.     FrameColor      : YellowonBlue;  FrameMono      : $0F;
  63.     HeaderColor     : YellowonBlue;  HeaderMono     : $0F;
  64.     ShadowColor     : BlackonBlack;  ShadowMono     : $07;
  65.     HighlightColor  : WhiteonRed;    HighlightMono  : $70;
  66.     PromptColor     : BlackonLtGray; PromptMono     : $07;
  67.     SelPromptColor  : BlackonLtGray; SelPromptMono  : $07;
  68.     ProPromptColor  : BlueonLtGray;  ProPromptMono  : $07;
  69.     FieldColor      : BlackonLtGray; FieldMono      : $0F;
  70.     SelFieldColor   : WhiteonBlue;   SelFieldMono   : $70;
  71.     ProFieldColor   : BlueonLtGray;  ProFieldMono   : $07;
  72.     ScrollBarColor  : YellowonBlue;  ScrollBarMono  : $07;
  73.     SliderColor     : YellowonBlue;  SliderMono     : $07;
  74.     HotSpotColor    : BlackonCyan;   HotSpotMono    : $07;
  75.     BlockColor      : WhiteonBlue;   BlockMono      : $0F;
  76.     MarkerColor     : YellowonLtGray;MarkerMono     : $70;
  77.     DelimColor      : BlackonLtGray; DelimMono      : $0F;
  78.     SelDelimColor   : WhiteonBlue;   SelDelimMono   : $70;
  79.     ProDelimColor   : BlueonLtGray;  ProDelimMono   : $07;
  80.     SelItemColor    : WhiteonRed;    SelItemMono    : $70;
  81.     ProItemColor    : BrownonLtGray; ProItemMono    : $01;
  82.     HighItemColor   : WhiteonRed;    HighItemMono   : $0F;
  83.     AltItemColor    : BlueonLtGray;  AltItemMono    : $0F;
  84.     AltSelItemColor : LtBlueonLtGray;AltSelItemMono : $70;
  85.     FlexAHelpColor  : WhiteonLtGray; FlexAHelpMono  : $0F;
  86.     FlexBHelpColor  : YellowOnRed;   FlexBHelpMono  : $01;
  87.     FlexCHelpColor  : GreenonBlack;  FlexCHelpMono  : $70;
  88.     UnselXrefColor  : YellowonBlack; UnselXrefMono  : $09;
  89.     SelXrefColor    : WhiteonRed;    SelXrefMono    : $70;
  90.     MouseColor      : WhiteonRed;    MouseMono      : $70
  91.   );
  92.  
  93.   WindowStep : byte = 1;
  94.  
  95. var
  96.   ULRootColorSet : ColorSet;
  97.   HeadFootAttr : byte;
  98.  
  99. type
  100.  
  101. (************************************************************************
  102.   The IndexDblList object desends from DoubleList and adds a GET method
  103.   to return a pointer to the nth node.
  104. ************************************************************************)
  105.  
  106.   IndexDblListPtr = ^IndexDblList;
  107.   IndexDblList = object(DoubleList)
  108.     function Get(Index: word): DoubleNodePtr; virtual;
  109.   end;
  110.  
  111. (************************************************************************
  112.   The MStringArray descends from StringArray and adds a data field and
  113.   methods for determining and getting the max string length in the array.
  114.   For this to function you must use AddMString vice AddString.
  115. ************************************************************************)
  116.  
  117.   MStringArrayPtr = ^MStringArray;
  118.   MStringArray = object(StringArray)
  119.     msMaxLen : byte;
  120.     constructor Init(Num, Amount: word);
  121.     function AddMString(St : string): word;
  122.     function GetMaxLen: byte;
  123.   end;
  124.  
  125. (************************************************************************
  126.   Global Routines
  127. ************************************************************************)
  128.  
  129. procedure MoveCmdWindow(WP: CommandWindowPtr);
  130. procedure ResizeCmdWindow(WP: CommandWindowPtr);
  131. procedure ToggleZoom(WP: CommandWindowPtr);
  132. function IncPtr(P: pointer; W: word): pointer;
  133. function GetGoodCoord(first, wide, maxwide: byte): byte;
  134. function Extend(S : String; Len : Byte) : String;
  135. procedure SimpStatus(UnitCode:byte; var Code: word; Msg:string);
  136. function Center1(OuterWidth, InnerWidth: word): word;
  137. function Center2(FirstCoord, InnerWidth: word): word;
  138. procedure InitCrt;
  139. procedure RestoreCrt;
  140. procedure Abort;
  141. procedure WriteFooter(Prompt : String);
  142. function SizeOfObject(TypOf: pointer): word;
  143. procedure PromoteAncestor(Ancestor, TypOf: pointer);
  144.  
  145. (***********************************************************************)
  146. Implementation
  147. (***********************************************************************)
  148.  
  149. {$IFDEF UseAdjustableWindows}
  150.  
  151. procedure MoveCmdWindow(WP: CommandWindowPtr);
  152.   {-Move any window interactively}
  153. var
  154.   Finished : Boolean;
  155. begin
  156.   if WP^.IsZoomed then
  157.     Exit;
  158.   WriteFooter(' Use cursor keys to move, {Enter} to accept');
  159.   Finished := False;
  160.   with WP^ do
  161.     repeat
  162.       case ReadKeyWord of
  163.         $4700 : MoveWindow(-WindowStep, -WindowStep); {Home}
  164.         $4800 : MoveWindow(0, -WindowStep);           {Up arrow}
  165.         $4900 : MoveWindow(WindowStep, -WindowStep);  {PgUp}
  166.         $4B00 : MoveWindow(-WindowStep, 0);           {Left Arrow}
  167.         $4D00 : MoveWindow(WindowStep, 0);            {Right Arrow}
  168.         $4F00 : MoveWindow(-WindowStep, WindowStep);  {End}
  169.         $5000 : MoveWindow(0, WindowStep);            {Down arrow}
  170.         $5100 : MoveWindow(WindowStep, WindowStep);   {PgDn}
  171.         $1C0D : Finished := True;                     {Enter}
  172.       end;
  173.  
  174.       if ClassifyError(GetLastError) = etFatal then
  175.         Abort;
  176.     until Finished;
  177.  
  178.   WriteFooter('');
  179. end;
  180.  
  181. procedure ResizeCmdWindow(WP: CommandWindowPtr);
  182.   {-Resize any window interactively}
  183. var
  184.   Finished : Boolean;
  185. begin
  186.   if WP^.IsZoomed then
  187.     Exit;
  188.   WriteFooter(' Use cursor keys to resize, {Enter} to accept');
  189.   Finished := False;
  190.   with WP^ do
  191.     repeat
  192.       case ReadKeyWord of
  193.         $4700 : ResizeWindow(-WindowStep, -WindowStep); {Home}
  194.         $4800 : ResizeWindow(0, -WindowStep);           {Up}
  195.         $4900 : ResizeWindow(WindowStep, -WindowStep);  {PgUp}
  196.         $4B00 : ResizeWindow(-WindowStep, 0);           {Left}
  197.         $4D00 : ResizeWindow(WindowStep, 0);            {Right}
  198.         $4F00 : ResizeWindow(-WindowStep, WindowStep);  {End}
  199.         $5000 : ResizeWindow(0, WindowStep);            {Down}
  200.         $5100 : ResizeWindow(WindowStep, WindowStep);   {PgDn}
  201.         $1C0D : Finished := True;                       {Enter}
  202.       end;
  203.  
  204.       if ClassifyError(GetLastError) = etFatal then
  205.         Abort;
  206.     until Finished;
  207.  
  208.   WriteFooter('');
  209. end;
  210.  
  211. procedure ToggleZoom(WP: CommandWindowPtr);
  212.   {-Toggle zoom status of any window}
  213. begin
  214.   with WP^ do begin
  215.     if IsZoomed then
  216.       Unzoom
  217.     else
  218.       Zoom;
  219.  
  220.     if ClassifyError(GetLastError) = etFatal then
  221.       Abort;
  222.   end;
  223. end;
  224. {$ENDIF}
  225.  
  226. function IncPtr(P: pointer; W: word): pointer;
  227. begin
  228.   IncPtr := AddWordToPtr(Normalized(P), W)
  229. end;
  230.  
  231. function GetGoodCoord(first, wide, maxwide: byte): byte;
  232.   {adjusts first coordinate if necessary so that a display will fit on screen}
  233. var
  234.   i,j : integer;
  235. begin
  236.   i := first - 1 + wide;
  237.   if i > Succ(maxwide) then
  238.   begin
  239.     i := i - Succ(maxwide);
  240.     j := first - i;
  241.     if j < 2 then GetGoodCoord := 2
  242.     else GetGoodCoord := j;
  243.   end
  244.   else GetGoodCoord := first;
  245. end;
  246.  
  247. function Extend(S : String; Len : Byte) : String;
  248.   {-Pad or truncate string to specified length}
  249. var
  250.   SLen : Byte absolute S;
  251. begin
  252.   if SLen >= Len then begin
  253.     SLen := Len;
  254.     Extend := S;
  255.   end
  256.   else
  257.     Extend := Pad(S, Len);
  258. end;
  259.  
  260. const
  261.   SavedState : boolean = false;
  262.  
  263. var
  264.   (* Various Crt parameters that are saved for later restoration *)
  265.   SaveAttr : byte;
  266.   SaveChar : char;
  267.   SaveXY, SaveScanLines : word;
  268.   SaveMode : byte;
  269.   SaveDir : string[64];
  270.   SaveBreak, SaveEOF : boolean;
  271.   {$IFDEF UseMouse}
  272.   MouseState : boolean;
  273.   {$ENDIF}
  274.  
  275. (* Initializes Crt and Save parameters *)
  276. procedure InitCrt;
  277. begin
  278.   GetDir(0,SaveDir);
  279.   GetCursorState(SaveXY, SaveScanlines);
  280.   SaveBreak := CheckBreak;
  281.   SaveEOF := CheckEOF;
  282.   ReInitCrt;
  283.   SaveMode := LastMode;
  284.   SaveAttr := ReadAttrAtCursor;
  285.   SaveChar := ReadCharAtCursor;
  286.   SavedState := true;
  287.   {$IFDEF UseMouse}
  288.   if MouseInstalled then HideMousePrim(MouseState);
  289.   {$ENDIF}
  290. end;
  291.  
  292. (* Restores Global Parameters to their original *)
  293. procedure RestoreCrt;
  294. begin
  295.   {$IFDEF UseMouse}
  296.   if MouseInstalled then ShowMousePrim(MouseState);
  297.   {$ENDIF}
  298.   ChDir(SaveDir);
  299.   RestoreCursorState(SaveXY, SaveScanlines);
  300.   CheckBreak := SaveBreak;
  301.   CheckEOF := SaveEOF;
  302.   TextMode(SaveMode);
  303.   TextAttr := SaveAttr;
  304.   TextChar := SaveChar;
  305.   ClrScr;
  306. end;
  307.  
  308. (* Centering Functions *)
  309. function Center1(OuterWidth, InnerWidth: word): word;
  310. begin
  311.   Center1 := (OuterWidth - InnerWidth) div 2 + 1;
  312. end;
  313.  
  314. function Center2(FirstCoord, InnerWidth: word): word;
  315. begin
  316.   Center2 := FirstCoord + InnerWidth - 1;
  317. end;
  318.  
  319. (* Simple Status and Error Handler *)
  320. procedure SimpStatus(UnitCode:byte; var Code: word; Msg:string);
  321. begin
  322.   RingBell;
  323.   WriteLn(Msg, 'Unit: ',UnitCode,' Error: ',Code);
  324. end;
  325.  
  326. (* MStringArray Methods *)
  327. constructor MStringArray.Init(Num, Amount: word);
  328. begin
  329.   StringArray.Init(Num, Amount);
  330.   msMaxLen := 0;
  331. end;
  332.  
  333. function MStringArray.AddMString(St : string): word;
  334. var
  335.   Len : byte absolute St;
  336.   Index : word;
  337. begin
  338.   Index := AddString(St);
  339.   if Index <> 0 then msMaxLen := MaxWord(msMaxLen, Len);
  340.   AddMString := Index;
  341. end;
  342.  
  343. function MStringArray.GetMaxLen: byte;
  344. begin
  345.   GetMaxLen := msMaxLen;
  346. end;
  347.  
  348. (* IndexDblList Methods *)
  349. function IndexDblList.Get(Index: word): DoubleNodePtr;
  350. var i : word;
  351.     p : DoubleNodePtr;
  352. begin
  353.   if Index > Size then
  354.   begin
  355.     Get := nil;
  356.     Exit;
  357.   end;
  358.   p := Head;
  359.   for i := 2 to Index do p := Next(p);
  360.   Get := p;
  361. end;
  362.  
  363. (*********************)
  364.  
  365. procedure Abort;
  366.   {-Abort the program with an out-of-memory error message}
  367. begin
  368.   if SavedState then RestoreCrt
  369.   else
  370.   begin
  371.     NormalCursor;
  372.     ClrScr;
  373.   end;
  374.   WriteLn('Insufficient memory available to continue.');
  375.   Halt(1);
  376. end;
  377.  
  378. procedure WriteFooter(Prompt : String);
  379.   {-Write a footer on the menu line}
  380. {$IFDEF UseMouse}
  381. var
  382.   SaveMouse : Boolean;
  383. {$ENDIF}
  384. begin
  385.   {$IFDEF UseMouse}
  386.   HideMousePrim(SaveMouse);
  387.   {$ENDIF}
  388.  
  389.   FastWrite(Extend(Prompt, ScreenWidth), ScreenHeight, 1, HeadFootAttr);
  390.   GotoXYabs(Length(Prompt)+2, ScreenHeight);
  391.  
  392.   {$IFDEF UseMouse}
  393.   ShowMousePrim(SaveMouse);
  394.   {$ENDIF}
  395. end;
  396.  
  397. function SizeOfObject(TypOf: pointer): word;
  398.   { TypOf must have been returned by the TypeOf function which returns the
  399.     address of the VMT. The first word of the VMT is the size of the instance.}
  400. begin
  401.   SizeOfObject := word(TypOf^);
  402. end;
  403.  
  404. procedure PromoteAncestor(Ancestor, TypOf: pointer);
  405.   { This only works if the VMT link is the first two bytes of the ancestor
  406.     as in descendants of Root and TypOf has been returned by
  407.     TypeOf(Descendant). Otherwise it most probably will cause a crash! }
  408. var
  409.   VMTOfs : word;
  410. begin
  411.   VMTOfs := Word(PtrDiff(Ptr(DSeg,0),TypOf));
  412.   Move(VMTOfs, Ancestor^, 2);  {fixup VMT link}
  413. end;
  414.  
  415. (*******************************)
  416. begin
  417.   ULRootColorSet := ULColorSet;
  418.   with ULRootColorSet do
  419.   HeadFootAttr := ColorMono(HighLightColor, HighLightMono);
  420. End.
  421.