Menu

[r3136]: / branches / 3.x / Src / Browser.UIOMgr.pas  Maximize  Restore  History

Download this file

381 lines (347 with data), 13.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
{
* Browser.UIOMgr.pas
*
* Class that wraps the IE web browser control and provides ability to load and
* save HTML from files, streams or strings. Also simplifies navigation to
* documents stored locally or in resources and exposes some HTML events.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is Browser.UIOMgr.pas, formerly UWBIOMgr.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit Browser.UIOMgr;
interface
uses
// Delphi
SHDocVw, Classes,
// Project
UHTMLEvents;
type
{
TWBNavigateEvent:
Type of event triggered just before browser control navigates to a new
document. Handle this event to intervene in, cancel or get information about
the navigation.
@param Sender [in] Object triggering event.
@param URL [in] URL to be navigated to.
@param Cancel [in/out] False when called. Set true to prevent the browser
navigating to the URL.
}
TWBNavigateEvent = procedure(Sender: TObject; const URL: string;
var Cancel: Boolean) of object;
{
TWBIOMgr:
Class that wraps the IE web browser control and provides ability to load and
save HTML from files, streams or strings. Also simplifies navigation to
documents stored locally or in resources and exposes some HTML events.
}
TWBIOMgr = class(TObject)
strict private
fWB: TWebBrowser; // Reference to managed webbrowser
fOnNavigate: TWBNavigateEvent; // Handler for OnNavigate event
fOnHTMLEvent: THTMLEvent; // Handler for OnHTMLEvent event
fDocEvents: THTMLDocEventSink; // Event sink for browser document events
fWdwEvents: THTMLWdwEventSink; // Event since for browser window events
procedure HTMLEventHandler(Sender: TObject;
const EventInfo: THTMLEventInfo);
{Handles OnEvent events triggered by browser document and window event
sinks.
@param Sender [in] Not used.
@param EventInfo [in] Object providing information about the event.
}
procedure WaitForDocToLoad;
{Waits for a document to complete loading.
@except EBug raised if there is no document or it is not a valid HTML
document.
}
procedure NavigateHandler(Sender: TObject; const pDisp: IDispatch;
var URL, Flags, TargetFrameName, PostData, Headers: OleVariant;
var Cancel: WordBool);
{Handles web browser navigation events by triggering own OnNavigate event.
@param Sender [in] Not used.
@param pDisp [in] Not used.
@param URL [in/out] URL to access, passed to OnNavigate event handler.
Left unchanged.
@param Flags [in/out] Not used.
@param TargetFrameName [in/out] Not used.
@param PostData [in/out] Not used.
@param Headers [in/out] Not used.
@param Cancel [in] False when passed in. Set to true to cancel browser's
own navigation or leave false to permit browser to handle navigation.
}
procedure InternalLoadDocumentFromStream(const Stream: TStream);
{Updates the web browser's current document from HTML read from stream.
@param Stream [in] Stream containing valid HTML source code.
@except EBug raised if updated document is not valid.
}
procedure SetBodyHTML(const HTML: string);
{Sets inner HTML of browser's current document <body>.
@param HTML [in] Required inner HTML for <body> tag.
}
procedure EmptyDocument;
{Creates an empty document. This method guarantees that the browser
contains a valid document object. The browser displays a blank page.
@except EBug raised if document is not valid.
}
procedure NavigateToURL(const URL: string);
{Navigates to a document at a specified URL.
@param URL [in] Full URL of the document.
@except EBug raised if new document is not valid.
}
public
constructor Create(const WB: TWebBrowser);
{Object constructor. Sets up object.
@param WB [in] Managed webbrowser control. Must not be nil.
}
destructor Destroy; override;
{Object destructor. Tears down object and disconnects event sinks.
}
procedure LoadFromString(const HTML: string);
{Loads and displays valid HTML source from a string.
@param HTML [in] String containing the HTML source.
@except EBug raised if document is not valid.
}
procedure NavigateToResource(const Module: HMODULE; const ResName: PChar;
const ResType: PChar = nil);
{Navigates to the document stored as a resource in a module.
@param Module [in] Handle of module containing resource.
@param ResName [in] Name of the resource
@param ResType [in] Type of resource (RT_HTML is assumed if ResType is
nil).
@except EBug raised if document is not valid.
}
function HTMLDocumentExists: Boolean;
{Checks if a valid HTML document is loaded in the browser control.
@return True if valid HTML document is loaded, False if not.
}
procedure ReplaceExistingBodyHTML(const HTML: string);
{Replaces body HTML of an existing HTML document.
@param HTML [in] New body HTML.
}
property OnNavigate: TWBNavigateEvent
read fOnNavigate write fOnNavigate;
{Event triggered when browser control is about to navigate to a new
document. Handle this event to intervene in navigation process}
property OnHTMLEvent: THTMLEvent
read fOnHTMLEvent write fOnHTMLEvent;
{Event triggered in response to browser's "standard" window and document
event sink events}
end;
implementation
uses
// Delphi
SysUtils, ActiveX,
// Project
Browser.UControlHelper, UHTMLDocHelper, UResourceUtils;
{ TWBIOMgr }
constructor TWBIOMgr.Create(const WB: TWebBrowser);
{Object constructor. Sets up object.
@param WB [in] Managed webbrowser control. Must not be nil.
}
begin
Assert(Assigned(WB), ClassName + '.Create: WB is nil');
inherited Create;
fWB := WB;
fWB.OnBeforeNavigate2 := NavigateHandler;
// Create event sinks and set event handlers
fDocEvents := THTMLDocEventSink.Create;
fDocEvents.OnEvent := HTMLEventHandler;
fWdwEvents := THTMLWdwEventSink.Create;
fWdwEvents.OnEvent := HTMLEventHandler;
end;
destructor TWBIOMgr.Destroy;
{Object destructor. Tears down object and disconnects event sinks.
}
begin
fWdwEvents.Disconnect;
FreeAndNil(fWdwEvents);
fDocEvents.Disconnect;
FreeAndNil(fDocEvents);
inherited;
end;
procedure TWBIOMgr.EmptyDocument;
{Creates an empty document. This method guarantees that the browser
contains a valid document object. The browser displays a blank page.
@except EBug raised if document is not valid.
}
begin
// Load the special blank document
NavigateToURL('about:blank');
end;
function TWBIOMgr.HTMLDocumentExists: Boolean;
{Checks if a valid HTML document is loaded in the browser control.
@return True if valid HTML document is loaded, False if not.
}
begin
Result := THTMLDocHelper.IsValidDocument(fWB.Document);
end;
procedure TWBIOMgr.HTMLEventHandler(Sender: TObject;
const EventInfo: THTMLEventInfo);
{Handles OnEvent events triggered by browser document and window event sinks.
@param Sender [in] Not used.
@param EventInfo [in] Object providing information about the event.
}
begin
if Assigned(fOnHTMLEvent) then
fOnHTMLEvent(Self, EventInfo);
end;
procedure TWBIOMgr.InternalLoadDocumentFromStream(const Stream: TStream);
{Updates the web browser's current document from HTML read from stream.
@param Stream [in] Stream containing valid HTML source code.
@except EBug raised if updated document is not valid.
}
var
PersistStreamInit: IPersistStreamInit; // object used to load stream into doc
StreamAdapter: IStream; // IStream interface to stream
begin
Assert(Assigned(fWB.Document),
ClassName + '.InternalLoadDocumentFromStream: No document loaded');
// Get IPersistStreamInit interface on document object
if fWB.Document.QueryInterface(
IPersistStreamInit, PersistStreamInit
) = S_OK then
begin
// Clear document
if PersistStreamInit.InitNew = S_OK then
begin
// Load data from Stream into WebBrowser
StreamAdapter:= TStreamAdapter.Create(Stream);
PersistStreamInit.Load(StreamAdapter);
// Wait for document to finish loading
WaitForDocToLoad;
end;
end;
end;
procedure TWBIOMgr.LoadFromString(const HTML: string);
{Loads and displays valid HTML source from a string.
@param HTML [in] String containing the HTML source.
@except EBug raised if document is not valid.
}
var
Stm: TMemoryStream; // stream that receives HTML to be loaded
// ---------------------------------------------------------------------------
// Writes bytes from byte array B to Stm
procedure WriteBytes(const B: TBytes);
begin
if Length(B) > 0 then
Stm.WriteBuffer(Pointer(B)^, Length(B));
end;
// ---------------------------------------------------------------------------
begin
Stm := TMemoryStream.Create;
try
// Write HTML in Unicode Little Endian format with BOM
WriteBytes(TEncoding.Unicode.GetPreamble);
WriteBytes(TEncoding.Unicode.GetBytes(HTML));
Stm.Position := 0;
EmptyDocument;
InternalLoadDocumentFromStream(Stm);
finally
Stm.Free;
end;
end;
procedure TWBIOMgr.NavigateHandler(Sender: TObject; const pDisp: IDispatch;
var URL, Flags, TargetFrameName, PostData, Headers: OleVariant;
var Cancel: WordBool);
{Handles web browser navigation events by triggering own OnNavigate event.
@param Sender [in] Not used.
@param pDisp [in] Not used.
@param URL [in/out] URL to access, passed to OnNavigate event handler.
Left unchanged.
@param Flags [in/out] Not used.
@param TargetFrameName [in/out] Not used.
@param PostData [in/out] Not used.
@param Headers [in/out] Not used.
@param Cancel [in] False when passed in. Set to true to cancel browser's own
navigation or leave false to permit browser to handle navigation.
}
var
DoCancel: Boolean; // re-typing of Cancel parameter
begin
if Assigned(fOnNavigate) then
begin
DoCancel := Cancel;
fOnNavigate(Sender, URL, DoCancel);
Cancel := DoCancel;
end;
end;
procedure TWBIOMgr.NavigateToResource(const Module: HMODULE; const ResName,
ResType: PChar);
{Navigates to the document stored as a resource in a module.
@param Module [in] Handle of module containing resource.
@param ResName [in] Name of the resource
@param ResType [in] Type of resource (RT_HTML is assumed if ResType is nil).
@except EBug raised if document is not valid.
}
begin
NavigateToURL(MakeResourceURL(Module, ResName, ResType));
end;
procedure TWBIOMgr.NavigateToURL(const URL: string);
{Navigates to a document at a specified URL.
@param URL [in] Full URL of the document.
@except EBug raised if new document is not valid.
}
begin
// Do the navigation, don't use cache or history and wait for document to load
fWB.Navigate(
WideString(URL), navNoHistory or navNoReadFromCache or navNoWriteToCache
);
WaitForDocToLoad;
end;
procedure TWBIOMgr.ReplaceExistingBodyHTML(const HTML: string);
{Replaces body HTML of an existing HTML document.
@param HTML [in] New body HTML.
}
begin
Assert(Self.HTMLDocumentExists,
ClassName + '.ReplaceExistingBodyHTML: No HTML document exists');
SetBodyHTML(HTML);
end;
procedure TWBIOMgr.SetBodyHTML(const HTML: string);
{Sets inner HTML of browser's current document <body>.
@param HTML [in] Required inner HTML for <body> tag.
}
begin
Assert(THTMLDocHelper.IsValidDocument(fWB.Document),
ClassName + '.SetBodyHTML: Invalid or no document loaded in browser control'
);
THTMLDocHelper.SetInnerHTML(THTMLDocHelper.GetBodyElem(fWB.Document), HTML);
end;
procedure TWBIOMgr.WaitForDocToLoad;
{Waits for a document to complete loading.
@except EBug raised if there is no document or it is not a valid HTML
document.
}
begin
// NOTE: do not call this method in a FormCreate event handler since the
// browser will never reach this state - use a FormShow event handler instead
TWBControlHelper.WaitForValidDocToLoad(fWB); // can raise EBug
// connect event sinks to browser document and window
fDocEvents.Connect(fWB.Document);
fWdwEvents.Connect(THTMLDocHelper.ParentWindow(fWB.Document));
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.