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

  1. (***********************************************************************
  2.      Dialog Objects as Enhancements to Turbo Power OOP Professional
  3.                   New Communications Technology, Inc.
  4.                              Version 1.0
  5.                           by John Poindexter
  6.                              June 24, 1990
  7. ************************************************************************)
  8. {$I ULDEFINE.INC}
  9.  
  10. {$IFNDEF dlDEBUG}
  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 ULDial;
  17.  
  18. Interface
  19.  
  20. Uses OpRoot, OpDos, OpCrt, OpMouse, OpInline, OpString, OpCmd,
  21.      OpFrame, OpWindow, OpPick, OpField, OpEntry, OpKey,
  22.      ULRoot;
  23.  
  24. const
  25.  
  26. (* Status Handler Return Codes *)
  27.   scOk      = 1;
  28.   scCancel  = 2;
  29.   scRetry   = 3;
  30.   scTimeOut = 99;
  31.  
  32. type
  33.  
  34. (************************************************************************
  35.   DialogPick is a descendant of PickList for use as a WindowField
  36. ************************************************************************)
  37.  
  38.   DialogPickPtr = ^DialogPick;
  39.   DialogPick = object(PickList)
  40.     dpChoices : MStringArrayPtr;
  41.     constructor Init(X1,Y1,X2,Y2: byte; var Colors: ColorSet;
  42.                     Options: longint; ItemWidth: byte;
  43.                     NumItems: word; Orientation: pkGenlProc;
  44.                     CommandHandler: pkGenlProc;
  45.                     PickOptions: word; Choices: MStringArrayPtr);
  46.     procedure ItemString(Item: word; Mode: pkMode; var IType: pkItemType;
  47.                          var IString: string); virtual;
  48.     procedure ProcessSelf; virtual;
  49.   end;
  50.  
  51. (************************************************************************
  52.   DialogBox displays text, a string entry field and provides choices
  53.   for exiting.
  54. ************************************************************************)
  55.  
  56.   DialogBoxPtr = ^DialogBox;
  57.   DialogBox = object(Root)
  58.     dlX1,dlY1,dlX2,dlY2 : word;   {Coordinates of Entry Screen}
  59.     dlHeader : string[78];
  60.     dlHeaderPos : HeaderPosType;
  61.     dlText : MStringArrayPtr;
  62.     dlChoices : MStringArrayPtr;
  63.     dlTNum, dlCNum : byte;
  64.     dlEntry : EntryScreenPtr;
  65.     dlPick : DialogPickPtr;
  66.     dlPrompt : string;
  67.     dlpRow, dlpCol, dlfRow, dlfCol: word;
  68.     dlRows : word;
  69.     dlPicture : string;
  70.     dlfWidth : word;
  71.     dlHelpIndex : word;
  72.     dlEditSt: string;
  73.     dlTimeOut : word;
  74.     dlLastChoice : word;
  75.     dlLastError: word;
  76.     dlNumTextLines : byte;
  77.     dlTotalTextChars : word;
  78.     dlNumChoices : byte;
  79.     dlTotalChoiceChars : word;
  80.     constructor Init(NumTextLines, TotalTextChars,
  81.                      NumChoices, TotalChoiceChars: word);
  82.     destructor Done; virtual;
  83.     procedure Clear;
  84.     function GetLastError: word;
  85.     procedure Process; virtual;
  86.     procedure AddMessageString(Msg: string);
  87.     procedure AddChoiceString(Choice: string);
  88.     procedure AddStringEntryField(Prompt: string; pRow, pCol: word;
  89.                                   Picture: string; fRow, fCol: word;
  90.                                   fWidth: byte; HelpIndex: word;
  91.                                   EditSt: string);
  92.     function CreateBox: boolean; virtual;
  93.     procedure AddHeader(S: string; Posn: HeaderPosType);
  94.     function GetLastChoice: word;
  95.     function GetEditedString: string;
  96.     procedure SetTimeOut(Delay: word);
  97.   end;
  98.  
  99. (***********************************************************************)
  100. Implementation
  101. (***********************************************************************)
  102.  
  103. (* DialogPick Methods *)
  104.  
  105. constructor DialogPick.Init(X1,Y1,X2,Y2: byte; var Colors: ColorSet;
  106.                             Options: longint; ItemWidth: byte;
  107.                             NumItems: word; Orientation: pkGenlProc;
  108.                             CommandHandler: pkGenlProc;
  109.                             PickOptions: word; Choices: MStringArrayPtr);
  110. begin
  111.   if not PickList.InitAbstractDeluxe(X1,Y1,X2,Y2,Colors,Options,ItemWidth,
  112.                                      NumItems,Orientation,CommandHandler,
  113.                                      PickOptions) then Fail;
  114.   dpChoices := Choices;
  115. end;
  116.  
  117. procedure DialogPick.ItemString(Item: word; Mode: pkMode; var IType: pkItemType;
  118.                                var IString: string);
  119. begin
  120.   if Mode = pkGetType then Exit;
  121.   IString := dpChoices^.GetString(Item);
  122.   Case Mode of
  123.     pkDisplay : begin
  124.                   Insert(^B, Istring, Length(Istring));
  125.                   Insert(^B, Istring, 4);
  126.                   Insert(^A, Istring, 4);
  127.                   Insert(^A, Istring, 3);
  128.                   Insert(^B, Istring, 3);
  129.                   Insert(^B, Istring, 2);
  130.                 end;
  131.     pkSearch  : IString := Copy(IString, 3, Length(IString)-4);
  132.   end;
  133. end;
  134.  
  135. procedure DialogPick.ProcessSelf;
  136. begin
  137.   PickList.ProcessSelf;
  138.   if (GetLastCommand = ccSelect) or (GetLastcommand = ccMouseSel) then
  139.     SetLastCommand(ccDone)
  140.   else if GetLastCommand = ccUser0 then SetLastCommand(ccBackTab);
  141. end;
  142.  
  143. (* DialogBox Methods
  144.  
  145.                    dlX1
  146.                dlY1┌─────────────────────┐
  147.                    │     X1        X2    │
  148.                    │   Y1┌────┐┌────┐    │
  149.                    │     └────┘└────┘    │
  150.                    └─────────────────────┘dlY2
  151.                                       dlX2
  152. *)
  153. constructor DialogBox.Init(NumTextLines, TotalTextChars,
  154.                            NumChoices, TotalChoiceChars: word);
  155. begin
  156.   if not Root.Init then Fail;
  157.   dlPrompt := '';
  158.   dlpRow := 0;
  159.   dlpCol := 0;
  160.   dlPicture := '';
  161.   dlfRow := 0;
  162.   dlfCol := 0;
  163.   dlRows := 0;
  164.   dlfWidth := 0;
  165.   dlHelpIndex := 0;
  166.   dlEditSt := '';
  167.   dlLastError := 0;
  168.   dlTimeOut := 0;
  169.   dlLastChoice := 0;
  170.   dlHeader := '';
  171.   dlEntry := nil;
  172.   dlPick := nil;
  173.   dlNumTextLines := NumTextLines;
  174.   dlTotalTextChars := TotalTextChars;
  175.   dlNumChoices := NumChoices;
  176.   dlTotalChoiceChars := TotalChoiceChars;
  177.   dlText := New(MStringArrayPtr,Init(NumTextLines, TotalTextChars));
  178.   dlChoices := New(MStringArrayPtr,Init(NumChoices, TotalChoiceChars));
  179.   if (dlText = nil) or (dlChoices = nil) then
  180.   begin
  181.     if dlText <> nil then Dispose(dlText, Done);
  182.     if dlChoices <> nil then Dispose(dlChoices, Done);
  183.     Root.Done;
  184.     Fail;
  185.   end;
  186. end;
  187.  
  188. destructor DialogBox.Done;
  189. begin
  190.   if dlEntry <> nil then Dispose(dlEntry, Done); {this also destoys dlPick}
  191.   if dlChoices <> nil then Dispose(dlChoices,Done);
  192.   if dlText <> nil then Dispose(dlText,Done);
  193.   PickCommands.AddCommand(ccUp, 1, Up, 0);  {restore normal commands}
  194.   Root.Done;
  195. end;
  196.  
  197. procedure DialogBox.Clear;
  198. begin
  199.   dlPrompt := '';
  200.   dlpRow := 0;
  201.   dlpCol := 0;
  202.   dlPicture := '';
  203.   dlfRow := 0;
  204.   dlfCol := 0;
  205.   dlRows := 0;
  206.   dlfWidth := 0;
  207.   dlHelpIndex := 0;
  208.   dlEditSt := '';
  209.   dlLastError := 0;
  210.   dlTimeOut := 0;
  211.   dlLastChoice := 0;
  212.   dlHeader := '';
  213.   if dlEntry <> nil then Dispose(dlEntry, Done); {this also destoys dlPick}
  214.   dlEntry := nil;
  215.   dlPick := nil;
  216.   if dlChoices <> nil then Dispose(dlChoices,Done);
  217.   if dlText <> nil then Dispose(dlText,Done);
  218.   dlText := New(MStringArrayPtr,Init(dlNumTextLines, dlTotalTextChars));
  219.   dlChoices := New(MStringArrayPtr,Init(dlNumChoices, dlTotalChoiceChars));
  220. end;
  221.  
  222. function DialogBox.GetLastError;
  223. begin
  224.   GetLastError := dlLastError;
  225.   dlLastError := 0;
  226. end;
  227.  
  228. procedure DialogBox.Process;
  229. var
  230.   LastCommand : word;
  231.   TimeOut : longint;
  232. begin
  233.   if not CreateBox then
  234.   begin
  235.     SimpStatus(ucULRoot, dlLastError, 'Creation DialogBox failed.');
  236.     Done;
  237.     Halt(1);
  238.   end;
  239.   if dlTimeOut <> 0 then
  240.   with dlEntry^ do
  241.   begin
  242.     Draw;
  243.     TimeOut := TimeMS + dlTimeOut;
  244.     Repeat until KeyPressed or (TimeMS > TimeOut);
  245.     if not KeyPressed then
  246.     begin
  247.       dlLastChoice := scTimeOut;
  248.       Exit;
  249.     end;
  250.   end;
  251.   with dlEntry^ do
  252.   begin
  253.     ClearErrors;
  254.     Repeat
  255.       Process;
  256.       LastCommand := GetLastCommand;
  257.     until (LastCommand = ccDone) or (LastCommand = ccError);
  258.     Erase;
  259.     if LastCommand = ccError then
  260.     begin
  261.       dlLastError := RawError;
  262.       SimpStatus(ucULDial, dlLastError, 'DialogBox problem.');
  263.       Done;
  264.       Halt(1);
  265.     end;
  266.     dlLastChoice := dlPick^.GetLastChoice;
  267.   end;
  268. end;
  269.  
  270. procedure DialogBox.AddMessageString(Msg: string);
  271. var
  272.   status : word;
  273.   Len : byte absolute Msg;
  274. begin
  275.   if Len > (ScreenWidth - 2) then Len := ScreenWidth-2;
  276.   status := dlText^.AddMString(Msg);
  277.   if status = 0 then dlLastError := ecOutOfMemory;
  278. end;
  279.  
  280. procedure DialogBox.AddChoiceString(Choice: string);
  281. var
  282.   Status : word;
  283.   Temp : string;
  284.   Len : byte absolute temp;
  285.   MaxLen : byte;
  286.   i,j : byte;
  287. begin
  288.   i := 0;
  289.   MaxLen := 0;
  290.   Repeat
  291.     Inc(i);
  292.     Temp := ExtractWord(i,Choice,[' ']);
  293.     MaxLen := MaxWord(MaxLen,Len);
  294.   until Len = 0;
  295.   Dec(i);
  296.   for j := 1 to i do
  297.   begin
  298.     Temp := '│ '+Pad(ExtractWord(j,Choice,[' ']),MaxLen)+' │';
  299.     status := dlChoices^.AddMString(temp);
  300.   end;
  301.   if status = 0 then dlLastError := ecOutOfMemory;
  302. end;
  303.  
  304. procedure DialogBox.AddStringEntryField(Prompt: string; pRow, pCol: word;
  305.                               Picture: string; fRow, fCol: word;
  306.                               fWidth: byte; HelpIndex: word;
  307.                               EditSt: string);
  308. begin
  309.   dlPrompt := Prompt;
  310.   if pRow = fRow      then begin dlpRow := 1; dlfRow := 1; dlRows := 1; end
  311.   else if pRow < fRow then begin dlpRow := 1; dlfRow := 2; dlRows := 2; end
  312.   else                     begin dlpRow := 2; dlfRow := 1; dlRows := 2; end;
  313.   dlpCol := pCol;
  314.   dlfCol := fCol;
  315.   dlPicture := Picture;
  316.   dlfWidth := fWidth;
  317.   dlHelpIndex := HelpIndex;
  318.   dlEditSt := EditSt;
  319. end;
  320.  
  321. function DialogBox.CreateBox: boolean;
  322. const
  323.   SelColorFlex : FlexAttrs = (0,0,0,0);
  324.   SelMonoFlex  : FlexAttrs = (0,0,0,0);
  325.   UnsColorFlex : FlexAttrs = (0,0,0,0);
  326.   UnsMonoFlex  : FlexAttrs = (0,0,0,0);
  327.  
  328. var
  329.   X1,Y1,X2 : word;  {coordinates of PickList}
  330.   WWidth, Twidth, Cwidth, Pwidth : byte;
  331.   status : word;
  332.   i : byte;
  333.   Line : string;
  334.   Len : byte absolute Line;
  335.  
  336.   function BoxLine(Num,CWid,PWid: byte; ChL,ChR: char): string;
  337.   var j : byte;
  338.   begin
  339.     Line := '';
  340.     for j := 1 to Num do
  341.     begin
  342.       Line := Line+ChL+CharStr('─',Cwid-2)+ChR;
  343.       if Len > Pwid then begin Len := (j-1)*Cwid; Exit; end;
  344.     end;
  345.     BoxLine := Line;
  346.   end;
  347.  
  348. begin
  349.   CreateBox := false;
  350.   if (dlEntry <> nil) and (dlPick <> nil) then
  351.   begin
  352.     CreateBox := true;
  353.     Exit;
  354.   end;
  355.   { Calculate whether Text or Choices are widest }
  356.   WWidth := ScreenWidth - 2;
  357.   Twidth := dlText^.GetMaxLen;
  358.   if Twidth > WWidth then Twidth := WWidth;
  359.   dlTNum := dlText^.NumStrings;
  360.   if dlTNum > ScreenHeight-5-dlRows then dlTNum := ScreenHeight-5-dlRows;
  361.   Cwidth := dlChoices^.GetMaxLen;
  362.   dlCNum := dlChoices^.NumStrings;
  363.   if (dlCNum = 0) then
  364.   begin
  365.     dlLastError := epFatal+ecNoChoice;
  366.     Exit;
  367.   end;
  368.   Pwidth := dlCNum * Cwidth;
  369.   if (Pwidth > Twidth) then
  370.   begin
  371.     if Pwidth > WWidth then Pwidth := WWidth
  372.     else WWidth := Pwidth;
  373.   end
  374.   else WWidth := Twidth;
  375.   { If there is a StringEntryField then, calculate widest.}
  376.   if dlRows > 0 then
  377.   begin
  378.     if dlpRow = dlfRow then
  379.     begin
  380.       Twidth := dlfCol+dlfWidth-1;
  381.       if Twidth > ScreenWidth-2 then Twidth := ScreenWidth-2;
  382.     end
  383.     else Twidth := MaxWord(dlpCol+Length(dlPrompt)-1, dlfCol+dlfWidth-1);
  384.     WWidth := MaxWord(WWidth, Twidth);
  385.     if Twidth < WWidth then
  386.     begin
  387.       Twidth := (WWidth - Twidth) div 2;
  388.       dlpCol := dlpCol + Twidth;
  389.       dlfCol := dlfCol + Twidth;
  390.     end;
  391.   end;
  392.   X1 := Center1(ScreenWidth,Pwidth);
  393.   X2 := Center2(X1,PWidth);
  394.   dlX1 := Center1(ScreenWidth,WWidth);
  395.   dlX2 := Center2(dlX1,WWidth);
  396.   dlY1 := Center1(ScreenHeight,dlTNum+3+dlRows);
  397.   dlY2 := Center2(dlY1,dlTNum+3+dlRows);
  398.   Y1 := dlY2 - 1;
  399.   dlPick := New(DialogPickPtr,Init(X1,Y1,X2,Y1,ULRootColorSet,
  400.             wClear+wNoCoversBuffer, Cwidth, dlCNum, PickHorizontal,
  401.             SingleChoice, DefPickOptions-pkStick, dlChoices));
  402.   if dlPick = nil then Exit;
  403.   with ULRootColorSet do
  404.   begin
  405.     UnsColorFlex[0] := TextColor;
  406.     UnsMonoFlex[0]  := TextMono;
  407.     UnsColorFlex[1] := FlexAHelpColor;
  408.     UnsMonoFlex[1]  := FlexAHelpMono;
  409.     UnsColorFlex[2] := TextColor;
  410.     UnsMonoFlex[2]  := TextMono;
  411.     SelColorFlex[0] := TextColor;
  412.     SelMonoFlex[0]  := TextMono;
  413.     SelColorFlex[1] := FlexAHelpColor;
  414.     SelMonoFlex[1]  := FlexAHelpMono;
  415.     SelColorFlex[2] := SelItemColor;
  416.     SelMonoFlex[2]  := SelItemMono;
  417.   end;
  418.   with dlPick^ do
  419.   begin
  420.     SetPickFlex(pkNormal, True, SelColorFlex, SelMonoFlex);
  421.     SetPickFlex(pkNormal, False, UnsColorFlex, UnsMonoFlex);
  422.     SetErrorProc(SimpStatus);
  423.     SetSearchMode(PickCharSearch);
  424.   end;
  425.   PickCommands.AddCommand(ccUser0, 1, Up, 0);
  426.   dlEntry := New(EntryScreenPtr, InitCustom(dlX1,dlY1,dlX2,dlY2,
  427.                  ULRootColorSet, wClear+wBordered));
  428.   if dlEntry = nil then Exit;
  429.   {$IFDEF UseMouse}
  430.   if MouseInstalled then
  431.   begin
  432.     PickCommands.cpOptionsOn(cpEnableMouse);
  433.     EntryCommands.cpOptionsOn(cpEnableMouse);
  434.     MouseGotoXY(X1+1,Y1);
  435.   end;
  436.   {$ENDIF}
  437.   with dlEntry^ do
  438.   begin
  439.     SetErrorProc(SimpStatus);
  440.     if dlHeader <> '' then wFrame.AddHeader(dlHeader, dlHeaderPos);
  441.     wFrame.AddShadow(shBR, shOverWrite);
  442.     for i := 1 to dlTNum do
  443.     begin
  444.       Line := dlText^.GetStringPtr(i)^;
  445.       if Len > WWidth then Len := WWidth;
  446.       AddTextField(Center(Line,WWidth),i,1);
  447.     end;
  448.     Y1 := dlTNum+dlRows+1;
  449.     X1 := X1-dlX1+1;
  450.     AddTextField(BoxLine(dlCNum,Cwidth,Pwidth,'┌','┐'), Y1,X1);
  451.     AddTextField(BoxLine(dlCNum,Cwidth,Pwidth,'└','┘'), Y1+2,X1);
  452.     if dlRows > 0 then
  453.     begin
  454.       esFieldOptionsOff(efAutoAdvance);
  455.       AddStringField(dlPrompt,dlTNum+dlpRow,dlpCol,dlPicture,
  456.                      dlTNum+dlfRow,dlfCol,dlfWidth,
  457.                      dlHelpIndex,dlEditSt);
  458.     end;
  459.     AddWindowField('',Y1+1,X1,Y1+1,X1, dlHelpIndex,dlPick^);
  460.     dlLastError := RawError;
  461.     if dlLastError <> 0 then Exit;
  462.   end;
  463.   CreateBox := true;
  464. end;
  465.  
  466. procedure DialogBox.AddHeader(S: string; Posn: HeaderPosType);
  467. begin
  468.   dlHeaderPos := Posn;
  469.   dlHeader := S;
  470. end;
  471.  
  472. function DialogBox.GetLastChoice: word;
  473. begin
  474.   GetLastChoice := dlLastChoice;
  475. end;
  476.  
  477. function DialogBox.GetEditedString: string;
  478. begin
  479.   GetEditedString := dlEditSt;
  480. end;
  481.  
  482. procedure DialogBox.SetTimeOut(Delay: word);
  483. begin
  484.   dlTimeOut := Delay;
  485. end;
  486.  
  487. (***************************)
  488.  
  489. {Initialization}
  490. begin
  491. end.
  492.