timeddlg.pas 7.4 KB

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