source: cprs/branches/tmg-cprs/CPRS-Chart/rHTMLTools.pas@ 454

Last change on this file since 454 was 453, checked in by Kevin Toppenberg, 16 years ago

Initial upload of TMG-CPRS 1.0.26.69

File size: 9.1 KB
Line 
1unit rHTMLTools;
2(*
3 This entire unit was created by K. Toppenberg, starting on 5/27/05
4 It will hold additional functions to support HTML display of notes
5 and printing of HTML notes.
6*)
7
8interface
9
10uses Windows, SysUtils, Classes, Printers, ComCtrls,
11 ShDocVw, {//kt added ShDocVw 5-2-05 for TWebBrowser access}
12 ORFn; {//kt for RedrawActivate}
13
14procedure PrintHTMLReport(Lines: TStringList; var ErrMsg: string); //kt added 5-2-05
15function IsHTMLDocument(Lines : TStrings): boolean; //kt added 5-2-05
16procedure WaitForBrowserOK(MaxSecDelay: integer);
17procedure ActivateWebBrowser;
18procedure ActivateMemo;
19function CheckForImageLink(Lines: TStrings; ImageList : TStringList) : boolean;
20
21implementation
22
23uses fNotes,
24 fImages,
25 StrUtils; {//kt added 5-2-05 rTIU for frmNotes.WebBrowser access}
26
27
28procedure PrintHTMLReport(Lines: TStringList; var ErrMsg: string);
29//Note:
30// I use two web browsers because sometimes the display web browser
31// would be changed by other parts of CPRS during the printing
32// process, causing the incorrect page to print out. By having a
33// browser just for printing, this will hopefully not happen.
34//
35// Web browser printing options:
36// OLECMDEXECOPT_DODEFAULT Use the default behavior, whether prompting the user for input or not.
37// OLECMDEXECOPT_PROMPTUSER Execute the command after obtaining user input.
38// OLECMDEXECOPT_DONTPROMPTUSER Execute the command without prompting the user.
39
40var
41 Status: OLECMDF;
42 HTMLfilename : string;
43 //Pauses : integer;
44begin
45 try
46 begin
47 HTMLfilename := ExtractFilePath(ParamStr(0)) + 'printing_html_note.html';
48 Lines.SaveToFile(HTMLfilename); //write the note to a file,
49 frmNotes.WebBrowser1.Navigate(HTMLfilename); //now navigate to file.
50 ActivateWebBrowser;
51 Status := frmNotes.WebBrowser1.QueryStatusWB(OLECMDID_PRINT); //"can you print?" -- get print command status
52 //Note: If I print multiple documents, I think there may be a problem if
53 // document #2 asks to print, and it is not yet done with doc #1
54 // As it is now, it will simply report an error. Solutions would
55 // be to wait a certain period of time and then ask for status again.
56 // OR, I could wait after printing until it is done....
57 WaitForBrowserOK(10); //wait up to 10 seconds
58 if (Status and OLECMDF_ENABLED)>0 then begin
59 frmNotes.WebBrowser1.ExecWB(OLECMDID_PRINT,OLECMDEXECOPT_PROMPTUSER);
60 //Here I want to wait until it is done printing.
61 //Note: this doesn't do what I want. Status is immediately OK.
62 WaitForBrowserOK(10); //wait up to 10 seconds
63 end else begin
64 ErrMsg := 'The web browser reports being unable to print. Trying printing this document by itself.';
65 end;
66 end;
67 finally
68 begin
69 //any needed final code goes here.
70 end;
71 end;
72end;
73
74
75procedure WaitForBrowserOK(MaxSecDelay: integer);
76var
77 CumulativeDelay : integer;
78 Status: OLECMDF;
79 MaxMSDelay: integer;
80
81const
82 DelayStep = 1000;
83begin
84 MaxMSDelay:=MaxSecDelay*1000;
85 CumulativeDelay := 0;
86 while ((Status and OLECMDF_ENABLED)<=0) and (CumulativeDelay < MaxMSDelay) do begin
87 sleep(DelayStep);
88 CumulativeDelay := CumulativeDelay + DelayStep;
89 Status := frmNotes.WebBrowser1.QueryStatusWB(OLECMDID_PRINT); //"can you print?" -- get print command status
90 //Beep;
91 end;
92end;
93
94
95Procedure ScanForSubs(Lines : TStrings);
96//Purpose: To scan note for constant $CPRS$ and replace with CPRS's actual directory
97var i : integer;
98 CPRSDir : string;
99begin
100 for i := 0 to Lines.Count-1 do begin
101 if Pos('$CPRSDIR$',Lines.Strings[i])>0 then begin
102 CPRSDir := ExcludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
103 Lines.Strings[i] := AnsiReplaceStr(Lines.Strings[i],'$CPRSDIR$',CPRSDir);
104 //Ensure images are downloaded before passing page to web browser
105 frmImages.timLoadImagesTimer(nil); //only downloads 1 image each call
106 end;
107 end;
108end;
109
110
111//kt added following 5-2-05
112function IsHTMLDocument(Lines : TStrings): boolean;
113{purpose: To look at the note loaded into Lines and determine if it is
114 an HTML document.
115 Test used: if document contains <!DOCTYPE HTML" or <HTML>
116 This is not a fool-proof test... }
117var
118 i:integer; s : string;
119begin
120 i := 0;
121 Result := false; //default of false
122 while (i <= Lines.Count-1) do begin
123 s := UpperCase(Lines.Strings[i]);
124 if (Pos('<!DOCTYPE HTML',s) > 0) or (Pos('<HTML>',s) > 0) then begin
125 Result := true;
126 break;
127 end;
128 Inc(i);
129 end;
130 if Result = true then ScanForSubs(Lines);
131end;
132//kt end of addition from 5-2-05
133
134
135procedure ActivateWebBrowser;
136begin
137 with frmNotes do begin
138 MemNote.Lines.SaveToFile(HTMLfilename); //write the note to a file,
139 //kt I later delete the file on destruction of this form (on CPRS exiting)
140 WebBrowser1.Visible := true;
141 WebBrowser1.TabStop := true;
142 WebBrowser1.Navigate(HTMLfilename); //now navigate to file.
143 WebBrowser1.BringToFront;
144 memNote.Visible := false;
145 memNote.TabStop := false;
146 end;
147end;
148
149procedure ActivateMemo;
150begin
151 with frmNotes do begin
152 WebBrowser1.Visible := false;
153 //WebBrowser1.Navigate('about:blank'); //if I leave this here, "Print Selected" doesn't work properly
154 //DeleteFile(HTMLfilename); //no error if file doesn't exist.
155 WebBrowser1.TabStop := false;
156 memNote.Visible := true;
157 memNote.TabStop := true;
158 memNote.BringToFront;
159 RedrawActivate(frmNotes.memNote.Handle);
160 end;
161end;
162
163
164
165//kt added the following 1/1/05
166function CheckForImageLink(Lines: TStrings; ImageList : TStringList) : boolean;
167{Purpose: To scan memNote memo for a link to an image. If found, return link(s)
168 input: none:
169 output: Will return a string list holding 1 or more links
170 Notes: Here will be the <img .. > format scanned for:
171
172 Here is some opening text...
173 <img src="http://www.geocities.com/kdtop3/OpenVistA.jpg" alt="Image Title 1">
174 And here is some more text
175 <img src="http://www.geocities.com/kdtop3/OpenVistA_small.jpg" alt="Image Title 2">
176 And the saga continues...
177 <img src="http://www.geocities.com/kdtop3/pics/Image100.gif" alt="Image Title 3">
178 As with html, end-of-lines and white space is not preserved or significant
179}
180
181 function GetBetween (var Text : AnsiString; OpenTag,CloseTag : string;
182 KeepTags : Boolean) : string;
183 {Purpose: Gets text between Open and Close tags. Removes any CR's or LF's
184 Input: Text - the text to work on. It IS changed as code is removed
185 KeepTags - true if want tag return in result
186 false if tag not in result (still is removed from Text)
187 Output: Text is changed.
188 Result=the code between the opening and closing tags
189 Note: Both OpenTag and CloseTag MUST be present for anything to happen.
190 }
191
192 procedure CutInThree(var Text : AnsiString; p1, p2 : Integer; var s1,s2,s3 : AnsiString);
193 {Purpose: Cut input string Text into 3 parts, with cut points given by p1 & p2.
194 p1 points to first character to be in s2
195 p2 points to last character to be in s2 }
196 begin
197 s1 := ''; s2 := ''; s3 := '';
198 if p1 > 1 then s1 := MidStr(Text, 1, p1-1);
199 s2 := MidStr(Text, p1, p2-p1+1);
200 s3 := MidStr(Text, p2+1, Length(Text)-p2);
201 end;
202
203 var
204 p1,p2 : integer;
205 s1,s2,s3 : AnsiString;
206
207 begin
208 Result := ''; //default of no result.
209
210 p1 := Pos(UpperCase(OpenTag), UpperCase(Text));
211 if (p1 > 0) then begin
212 p2 := Pos(UpperCase(CloseTag),UpperCase(Text)) + Length(CloseTag) -1;
213 if ((p2 > 0) and (p2 > p1)) then begin
214 CutInThree (Text, p1,p2, s1,Result,s3);
215 Text := s1+s3;
216 //Now, remove any CR's or LF's
217 repeat
218 p1 := Pos (Chr(13),Result);
219 if p1= 0 then p1 := Pos (Chr(10),Result);
220 if (p1 > 0) then begin
221 CutInThree (Result, p1,p1, s1,s2,s3);
222 Result := s1+s3;
223// Text := MidStr(Text,1,p1-1) + MidStr(Text,p1+1,Length(Text)-p1);
224 end;
225 until (p1=0);
226 //Now cut off boundry tags if requested.
227 if not KeepTags then begin
228 p1 := Length(OpenTag) + 1;
229 p2 := Length (Result) - Length (CloseTag);
230 CutInThree (Result, p1,p2, s1,s2,s3);
231 Result := s2;
232 end;
233 end;
234 end;
235 end;
236
237var
238 Text : AnsiString;
239 Line : string;
240
241begin
242 Result := false; //set default
243 if (ImageList <> nil) then begin
244 ImageList.Clear; //set default
245 Text := Lines.Text; //Get entire note into one long string
246 repeat
247 Line := GetBetween (Text, '<img', '>', true);
248 if Line <> '' then begin
249 ImageList.Add(Line);
250 Result := true;
251 end;
252 until Line = '';
253 //Note: The following works, but need to replace removed links
254 // with "[Title]" Work on later...
255 //memNote.Lines.Text := Text;
256 end;
257end;
258
259
260
261end.
Note: See TracBrowser for help on using the repository browser.