timeddlg.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. {
  2. Timed dialogs for Free Vision
  3. Copyright (c) 2004 by Free Pascal core team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public
  11. License along with this library; if not, write to the Free
  12. Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  13. MA 02110-1301, USA.
  14. ****************************************************************************}
  15. UNIT timeddlg;
  16. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  17. INTERFACE
  18. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  19. {====Include file to sort compiler platform out =====================}
  20. {$I platform.inc}
  21. {====================================================================}
  22. {==== Compiler directives ===========================================}
  23. {$IFNDEF PPC_FPC}{ FPC doesn't support these switches }
  24. {$F-} { Near calls are okay }
  25. {$A+} { Word Align Data }
  26. {$B-} { Allow short circuit boolean evaluations }
  27. {$O+} { This unit may be overlaid }
  28. {$G+} { 286 Code optimization - if you're on an 8088 get a real computer }
  29. {$P-} { Normal string variables }
  30. {$N-} { No 80x87 code generation }
  31. {$E+} { Emulation is on }
  32. {$ENDIF}
  33. {$X+} { Extended syntax is ok }
  34. {$R-} { Disable range checking }
  35. {$S-} { Disable Stack Checking }
  36. {$I-} { Disable IO Checking }
  37. {$Q-} { Disable Overflow Checking }
  38. {$V-} { Turn off strict VAR strings }
  39. {====================================================================}
  40. USES objects, dialogs, fvconsts, drivers, views; { Standard GFV unit }
  41. type
  42. TTimedDialog = object (TDialog)
  43. Secs: longint;
  44. constructor Init (var Bounds: TRect; ATitle: TTitleStr; ASecs: word);
  45. constructor Load (var S: TStream);
  46. procedure GetEvent (var Event: TEvent); virtual;
  47. procedure Store (var S: TStream); virtual;
  48. private
  49. Secs0: longint;
  50. Secs2: longint;
  51. DayWrap: boolean;
  52. end;
  53. PTimedDialog = ^TTimedDialog;
  54. (* Must be always included in TTimeDialog! *)
  55. TTimedDialogText = object (TStaticText)
  56. constructor Init (var Bounds: TRect);
  57. procedure GetText (var S: string); virtual;
  58. end;
  59. PTimedDialogText = ^TTimedDialogText;
  60. const
  61. RTimedDialog: TStreamRec = (
  62. ObjType: idTimedDialog;
  63. {$IFDEF BP_VMTLink} { BP style VMT link }
  64. VmtLink: Ofs (TypeOf (TTimedDialog)^);
  65. {$ELSE} { Alt style VMT link }
  66. VmtLink: TypeOf (TTimedDialog);
  67. {$ENDIF BP_VMTLink}
  68. Load: @TTimedDialog.Load;
  69. Store: @TTimedDialog.Store
  70. );
  71. RTimedDialogText: TStreamRec = (
  72. ObjType: idTimedDialogText;
  73. {$IFDEF BP_VMTLink} { BP style VMT link }
  74. VmtLink: Ofs (TypeOf (TTimedDialogText)^);
  75. {$ELSE} { Alt style VMT link }
  76. VmtLink: TypeOf (TTimedDialogText);
  77. {$ENDIF BP_VMTLink}
  78. Load: @TTimedDialogText.Load;
  79. Store: @TTimedDialogText.Store
  80. );
  81. procedure RegisterTimedDialog;
  82. FUNCTION TimedMessageBox (Const Msg: String; Params: Pointer;
  83. AOptions: Word; ASecs: Word): Word;
  84. {-TimedMessageBoxRect------------------------------------------------
  85. TimedMessageBoxRect allows the specification of a TRect for the message box
  86. to occupy.
  87. ---------------------------------------------------------------------}
  88. FUNCTION TimedMessageBoxRect (Var R: TRect; Const Msg: String; Params: Pointer;
  89. AOptions: Word; ASecs: Word): Word;
  90. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  91. IMPLEMENTATION
  92. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  93. USES
  94. dos,
  95. app, {resource,} msgbox; { Standard GFV units }
  96. {***************************************************************************}
  97. { INTERFACE ROUTINES }
  98. {***************************************************************************}
  99. constructor TTimedDialogText.Init (var Bounds: TRect);
  100. begin
  101. inherited Init (Bounds, '');
  102. end;
  103. procedure TTimedDialogText.GetText (var S: string);
  104. begin
  105. if Owner <> nil
  106. (* and (TypeOf (Owner^) = TypeOf (TTimedDialog)) *)
  107. then
  108. begin
  109. Str (PTimedDialog (Owner)^.Secs, S);
  110. S := #3 + S;
  111. end
  112. else
  113. S := '';
  114. end;
  115. constructor TTimedDialog.Init (var Bounds: TRect; ATitle: TTitleStr;
  116. ASecs: word);
  117. var
  118. H, M, S, S100: word;
  119. begin
  120. inherited Init (Bounds, ATitle);
  121. GetTime (H, M, S, S100);
  122. Secs0 := H * 3600 + M * 60 + S;
  123. Secs2 := Secs0 + ASecs;
  124. Secs := ASecs;
  125. DayWrap := Secs2 > 24 * 3600;
  126. end;
  127. procedure TTimedDialog.GetEvent (var Event: TEvent);
  128. var
  129. H, M, S, S100: word;
  130. Secs1: longint;
  131. begin
  132. inherited GetEvent (Event);
  133. GetTime (H, M, S, S100);
  134. Secs1 := H * 3600 + M * 60 + S;
  135. if DayWrap then Inc (Secs1, 24 * 3600);
  136. if Secs2 - Secs1 <> Secs then
  137. begin
  138. Secs := Secs2 - Secs1;
  139. if Secs < 0 then
  140. Secs := 0;
  141. (* If remaining seconds are displayed in one of included views, update them. *)
  142. Redraw;
  143. end;
  144. with Event do
  145. if (Secs = 0) and (What = evNothing) then
  146. begin
  147. What := evCommand;
  148. Command := cmCancel;
  149. end;
  150. end;
  151. constructor TTimedDialog.Load (var S: TStream);
  152. begin
  153. inherited Load (S);
  154. S.Read (Secs, SizeOf (Secs));
  155. S.Read (Secs0, SizeOf (Secs0));
  156. S.Read (Secs2, SizeOf (Secs2));
  157. S.Read (DayWrap, SizeOf (DayWrap));
  158. end;
  159. procedure TTimedDialog.Store (var S: TStream);
  160. begin
  161. inherited Store (S);
  162. S.Write (Secs, SizeOf (Secs));
  163. S.Write (Secs0, SizeOf (Secs0));
  164. S.Write (Secs2, SizeOf (Secs2));
  165. S.Write (DayWrap, SizeOf (DayWrap));
  166. end;
  167. function TimedMessageBox (const Msg: string; Params: pointer;
  168. AOptions: word; ASecs: word): word;
  169. var
  170. R: TRect;
  171. begin
  172. R.Assign(0, 0, 40, 10); { Assign area }
  173. if (AOptions AND mfInsertInApp = 0) then { Non app insert }
  174. R.Move((Desktop^.Size.X - R.B.X) div 2,
  175. (Desktop^.Size.Y - R.B.Y) div 2) { Calculate position }
  176. else
  177. R.Move((Application^.Size.X - R.B.X) div 2,
  178. (Application^.Size.Y - R.B.Y) div 2); { Calculate position }
  179. TimedMessageBox := TimedMessageBoxRect (R, Msg, Params,
  180. AOptions, ASecs); { Create message box }
  181. end;
  182. function TimedMessageBoxRect (var R: TRect; const Msg: string; Params: pointer;
  183. AOptions: word; ASecs: word): word;
  184. var
  185. Dlg: PTimedDialog;
  186. TimedText: PTimedDialogText;
  187. begin
  188. Dlg := New (PTimedDialog, Init (R, MsgBoxTitles [AOptions
  189. and $3], ASecs)); { Create dialog }
  190. with Dlg^ do
  191. begin
  192. R.Assign (3, Size.Y - 5, Size.X - 2, Size.Y - 4);
  193. New (TimedText, Init (R));
  194. Insert (TimedText);
  195. R.Assign (3, 2, Size.X - 2, Size.Y - 5); { Assign area for text }
  196. end;
  197. TimedMessageBoxRect := MessageBoxRectDlg (Dlg, R, Msg, Params, AOptions);
  198. Dispose (Dlg, Done); { Dispose of dialog }
  199. end;
  200. procedure RegisterTimedDialog;
  201. begin
  202. RegisterType (RTimedDialog);
  203. RegisterType (RTimedDialogText);
  204. end;
  205. begin
  206. RegisterTimedDialog;
  207. end.