home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vp21beta.zip / ATVSRC.RAR / MSGBOX.PAS < prev    next >
Pascal/Delphi Source File  |  2000-08-15  |  6KB  |  182 lines

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