home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D11 / TVSRC.ZIP / MSGBOX.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  5.7 KB  |  179 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Turbo Pascal Version 7.0                        }
  5. {       Turbo Vision Unit                               }
  6. {                                                       }
  7. {       Copyright (c) 1992 Borland International        }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. unit MsgBox;
  12.  
  13. {$O+,F+,X+,I-,S-}
  14.  
  15. interface
  16.  
  17. uses Objects;
  18.  
  19. const
  20.  
  21. { Message box classes }
  22.  
  23.   mfWarning      = $0000;       { Display a Warning box }
  24.   mfError        = $0001;       { Dispaly a Error box }
  25.   mfInformation  = $0002;       { Display an Information Box }
  26.   mfConfirmation = $0003;       { Display a Confirmation Box }
  27.  
  28.   mfInsertInApp  = $0080;       { Insert message box into application }
  29.                                 { instead of the Desktop }
  30.  
  31. { Message box button flags }
  32.  
  33.   mfYesButton    = $0100;       { Put a Yes button into the dialog }
  34.   mfNoButton     = $0200;       { Put a No button into the dialog }
  35.   mfOKButton     = $0400;       { Put an OK button into the dialog }
  36.   mfCancelButton = $0800;       { Put a Cancel button into the dialog }
  37.  
  38.   mfYesNoCancel  = mfYesButton + mfNoButton + mfCancelButton;
  39.                                 { Standard Yes, No, Cancel dialog }
  40.   mfOKCancel     = mfOKButton + mfCancelButton;
  41.                                 { Standard OK, Cancel dialog }
  42.  
  43. { MessageBox displays the given string in a standard sized      }
  44. { dialog box. Before the dialog is displayed the Msg and Params }
  45. { are passed to FormatStr.  The resulting string is displayed   }
  46. { as a TStaticText view in the dialog.                          }
  47.  
  48. function MessageBox(const Msg: String; Params: Pointer;
  49.   AOptions: Word): Word;
  50.  
  51. { MessageBoxRec allows the specification of a TRect for the     }
  52. { message box to occupy.                                        }
  53.  
  54. function MessageBoxRect(var R: TRect; const Msg: String; Params: Pointer;
  55.   AOptions: Word): Word;
  56.  
  57. { InputBox displays a simple dialog that allows the user to     }
  58. { type in a string.                                             }
  59.  
  60. function InputBox(const Title, ALabel: String; var S: String;
  61.   Limit: Byte): Word;
  62.  
  63. { InputBoxRect is like InputBox but allows the specification of }
  64. { a rectangle.                                                  }
  65.  
  66. function InputBoxRect(var Bounds: TRect; const Title, ALabel: String;
  67.   var S: String;  Limit: Byte): Word;
  68.  
  69. implementation
  70.  
  71. uses Drivers, Views, Dialogs, App;
  72.  
  73. function MessageBox(const Msg: String; Params: Pointer;
  74.   AOptions: Word): Word;
  75. var
  76.   R: TRect;
  77. begin
  78.   R.Assign(0, 0, 40, 9);
  79.   if AOptions and mfInsertInApp = 0 then
  80.     R.Move((Desktop^.Size.X - R.B.X) div 2, (Desktop^.Size.Y - R.B.Y) div 2)
  81.   else R.Move((Application^.Size.X - R.B.X) div 2, (Application^.Size.Y - R.B.Y) div 2);
  82.   MessageBox := MessageBoxRect(R, Msg, Params, AOptions);
  83. end;
  84.  
  85. function MessageBoxRect(var R: TRect; const Msg: String; Params: Pointer;
  86.   AOptions: Word): Word;
  87. const
  88.   ButtonName: array[0..3] of string[6] =
  89.     ('~Y~es', '~N~o', 'O~K~', 'Cancel');
  90.   Commands: array[0..3] of word =
  91.     (cmYes, cmNo, cmOK, cmCancel);
  92.   Titles: array[0..3] of string[11] =
  93.     ('Warning','Error','Information','Confirm');
  94. var
  95.   I, X, ButtonCount: Integer;
  96.   Dialog: PDialog;
  97.   Control: PView;
  98.   T: TRect;
  99.   ButtonList: array[0..4] of PView;
  100.   S: String;
  101. begin
  102.   Dialog := New(PDialog,
  103.     Init(R, Titles[AOptions and $3]));
  104.   with Dialog^ do
  105.   begin
  106.     R.Assign(3, 2, Size.X - 2, Size.Y - 3);
  107.     FormatStr(S, Msg, Params^);
  108.     Control := New(PStaticText, Init(R, S));
  109.     Insert(Control);
  110.     X := -2;
  111.     ButtonCount := 0;
  112.     for I := 0 to 3 do
  113.       if AOptions and ($0100 shl I) <> 0 then
  114.       begin
  115.         R.Assign(0, 0, 10, 2);
  116.         Control := New(PButton, Init(R, ButtonName[I], Commands[i],
  117.           bfNormal));
  118.         Inc(X, Control^.Size.X + 2);
  119.         ButtonList[ButtonCount] := Control;
  120.         Inc(ButtonCount);
  121.       end;
  122.     X := (Size.X - X) shr 1;
  123.     for I := 0 to ButtonCount - 1 do
  124.     begin
  125.       Control := ButtonList[I];
  126.       Insert(Control);
  127.       Control^.MoveTo(X, Size.Y - 3);
  128.       Inc(X, Control^.Size.X + 2);
  129.     end;
  130.     SelectNext(False);
  131.   end;
  132.   if AOptions and mfInsertInApp = 0 then
  133.     MessageBoxRect := DeskTop^.ExecView(Dialog)
  134.   else MessageBoxRect := Application^.ExecView(Dialog);
  135.   Dispose(Dialog, Done);
  136. end;
  137.  
  138. function InputBox(const Title, ALabel: String; var S: String;
  139.   Limit: Byte): Word;
  140. var
  141.   R: TRect;
  142. begin
  143.   R.Assign(0, 0, 60, 8);
  144.   R.Move((Desktop^.Size.X - R.B.X) div 2, (Desktop^.Size.Y - R.B.Y) div 2);
  145.   InputBox := InputBoxRect(R, Title, ALabel, S, Limit);
  146. end;
  147.  
  148. function InputBoxRect(var Bounds: TRect; const Title, ALabel: String;
  149.   var S: String;  Limit: Byte): Word;
  150. var
  151.   Dialog: PDialog;
  152.   Control: PView;
  153.   R: TRect;
  154.   C: Word;
  155. begin
  156.   Dialog := New(PDialog, Init(Bounds, Title));
  157.   with Dialog^ do
  158.   begin
  159.     R.Assign(4 + CStrLen(ALabel), 2, Size.X - 3, 3);
  160.     Control := New(PInputLine, Init(R, Limit));
  161.     Insert(Control);
  162.     R.Assign(2, 2, 3 + CStrLen(ALabel), 3);
  163.     Insert(New(PLabel, Init(R, ALabel, Control)));
  164.     R.Assign(Size.X - 24, Size.Y - 4, Size.X - 14, Size.Y - 2);
  165.     Insert(New(PButton, Init(R, 'O~K~', cmOk, bfDefault)));
  166.     Inc(R.A.X, 12); Inc(R.B.X, 12);
  167.     Insert(New(PButton, Init(R, 'Cancel', cmCancel, bfNormal)));
  168.     Inc(R.A.X, 12); Inc(R.B.X, 12);
  169.     SelectNext(False);
  170.   end;
  171.   Dialog^.SetData(S);
  172.   C := DeskTop^.ExecView(Dialog);
  173.   if C <> cmCancel then Dialog^.GetData(S);
  174.   Dispose(Dialog, Done);
  175.   InputBoxRect := C;
  176. end;
  177.  
  178. end.
  179.