Menu

[r111]: / trunk / Src / UHTMLEvents.pas  Maximize  Restore  History

Download this file

598 lines (552 with data), 22.1 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
{
* UHTMLEvents.pas
*
* Provides event sinks for the web browser control. Sinks for the browser's
* document and window objects are implemented.
*
* v1.0 of 18 Oct 2007 - Original version.
*
*
* ***** 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 UHTMLEvents.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2007 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s): None
*
* ***** END LICENSE BLOCK *****
}
unit UHTMLEvents;
{$WARN UNSAFE_TYPE OFF}
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
// Delphi
MSHTML, ActiveX,
// Project
USimpleDispatch;
type
{
THTMLEventInfo:
Class that provides information about a HTML event.
}
THTMLEventInfo = class(TObject)
private
fCanCancel: Boolean;
{Value of CanCancel property}
fCancelled: Boolean;
{Value of Cancelled property}
fArgs: IHTMLEventObj;
{Value of Args property}
fDispatchId: Integer;
{Value of DispatchId property}
procedure SetCancelled(const Value: Boolean);
{Write accessor for Cancelled property. Sets property only if CanCancel
property is True.
@param Value [in] New property value.
}
function GetName: string;
{Read accessor for Name property.
@return Name of event.
}
public
constructor Create(const DispatchID: Integer;
const Args: IHTMLEventObj; const CanCancel: Boolean);
{Class constructor. Sets up object.
@param DispatchId [in] Dispatch ID of the event.
@param Args [in] Information about the event provided by browser.
@param CanCancel [in] Flag indicating whether event can be cancelled or
not.
}
property DispatchId: Integer read fDispatchId;
{Dispatch ID of the event}
property Name: string read GetName;
{Name of the event. (Same as Args.type_)}
property CanCancel: Boolean read fCanCancel;
{Flag indicating whether event can be cancelled. When true the Cancelled
property cannot be set}
property Cancelled: Boolean read fCancelled write SetCancelled
default False;
{Flag indicating whether the event should be cancelled. Set to true to
cancel the event. If CanCancel is false the value of Cancelled cannot be
changed and is always False}
property Args: IHTMLEventObj read fArgs;
{Object exposing information about the event provided the browser object}
end;
{
THTMLEvent:
Type of the OnEvent event.
@param Sender [in] Reference to object that triggered the event.
@param EventInfo [in] Object providing information about the event.
}
THTMLEvent = procedure(Sender: TObject; const EventInfo: THTMLEventInfo)
of object;
{
TAbstractHTMLEventSink:
Abstract base class for non reference counted HTML event sinks. Provides
core functionality and some helper methods.
}
TAbstractHTMLEventSink = class(TSimpleDispatch,
IUnknown, IDispatch
)
private
fSinkIID: TGUID;
{Supported event interface}
fSource: IDispatch;
{Reference to event source object}
fConnectionCookie: Integer;
{Unique value that identifies connection between event source and sink}
fOnEvent: THTMLEvent;
{Reference to any OnEvent handler}
protected
procedure DoInvoke(var InvokeInfo: TInvokeInfo); override;
{Dispatches events via abstract DispatchEvent method after checking for
valid context.
@param InvokeInfo [in/out] Information about method / property
invocation. Structure is updated with information event invocation.
}
procedure DispatchEvent(var InvokeInfo: TInvokeInfo); virtual; abstract;
{Abstract method called when a event is to be dispatched.
@param InvokeInfo [in/out] Information about event invocation. Structure
is to be updated with information result of invocation.
}
procedure GetEventArgParam(var InvokeInfo: TInvokeInfo;
out EventArgs: IHTMLEventObj);
{Gets the single IHTMLEventObj parameter of a "standard" HTML event.
Notifies caller of error if there is more than one parameter or if
parameter is wrong type.
@param InvokeInfo [in/out] Information about method invocation.
Structure is updated with information about any error.
@param EventArgs [out] Set to object giving information information
about event arguments or nil if parameter is not valid.
}
procedure DispatchStdEvent(var InvokeInfo: TInvokeInfo;
const CanCancel: Boolean);
{Triggers given "standard" event, passing information about the event.
Sets function result if event is a function. Notifies an error if method
call is not a valid standard event.
@param InvokeInfo [in/out] Information about event method invocation.
Structure is updated with information about return value and any error
condition.
@param CanCancel [in] Flag indicating whether event can be cancelled.
}
{ IUnknown }
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
{Checks the specified interface is supported by this object. If so
reference to interface is passed out. Supported interfaces are IUnknown,
IDispatch and interface implemented by event sink.
@param [in] IID specifies interface being queried.
@param [out] Reference to interface implementation or nil if not
supported.
@result S_OK if interface supported or E_NOINTERFACE if not supported.
}
public
constructor Create(const SinkIID: TGUID);
{Class constructor. Sets up the object.
@param SinkIID [in] Interface of events to be sunk by this object.
}
destructor Destroy; override;
{Class destructor. Disconnects the sink from the event source and tears
down the object.
}
procedure Connect(const Source: IDispatch);
{Connects the event sink to the object that is the source of the events.
@param Source [in] Reference to source object.
}
procedure Disconnect;
{Disconnects the event sink from the event source object.
}
property OnEvent: THTMLEvent read fOnEvent write fOnEvent;
{Event triggered when the browser notifies the sink of a "standard" event}
end;
const
// Event dispids from HTMLDocumentEvents2 dispinterface
cDocEventOnHelp = -2147418102;
cDocEventOnClick = -600;
cDocEventOnDblClick = -601;
cDocEventOnKeyDown = -602;
cDocEventOnKeyUp = -604;
cDocEventOnKeyPress = -603;
cDocEventOnMouseDown = -605;
cDocEventOnMouseMove = -606;
cDocEventOnMouseUp = -607;
cDocEventOnMouseOut = -2147418103;
cDocEventOnMouseOver = -2147418104;
cDocEventOnReadyStateChange = -609;
cDocEventOnBeforeUpdate = -2147418108;
cDocEventOnAfterUpdate = -2147418107;
cDocEventOnRowExit = -2147418106;
cDocEventOnRowEnter = -2147418105;
cDocEventOnDragStart = -2147418101;
cDocEventOnSelectStart = -2147418100;
cDocEventOnErrorUpdate = -2147418099;
cDocEventOnContextMenu = 1023;
cDocEventOnStop = 1026;
cDocEventOnRowsDelete = -2147418080;
cDocEventOnRowsInserted = -2147418079;
cDocEventOnCellChange = -2147418078;
cDocEventOnPropertyChange = -2147418093;
cDocEventOnDatasetChanged = -2147418098;
cDocEventOnDataAvailable = -2147418097;
cDocEventOnDatasetComplete = -2147418096;
cDocEventOnBeforeEditFocus = 1027;
type
{
THTMLDocEventSink:
Event sink for HTML document events. Sinks HTMLDocumentEvents2 events.
}
THTMLDocEventSink = class(TAbstractHTMLEventSink,
IUnknown, IDispatch
)
protected
procedure DispatchEvent(var InvokeInfo: TInvokeInfo); override;
{Dispatches HTMLDocumentEvents2 events.
@param InvokeInfo [in] Structure containing about event to be
dispatched. Fields of this structure are updated to notify result of
event invocation.
}
public
constructor Create;
{Class constructor. Sets up object to sink HTMLDocumentEvents2 events.
}
end;
const
// Event dispids from HTMLDocumentEvents2 dispinterface
cWdwEventOnLoad = 1003;
cWdwEventOnUnload = 1008;
cWdwEventOnHelp = -2147418102;
cWdwEventOnFocus = -2147418111;
cWdwEventOnBlur = -2147418112;
cWdwEventOnError = 1002;
cWdwEventOnResize = 1016;
cWdwEventOnScroll = 1014;
cWdwEventOnBeforeUnload = 1017;
cWdwEventOnBeforePrint = 1024;
cWdwEventOnAfterPrint = 1025;
type
{
THTMLWdwErrorEvent:
Type of the OnError event.
@param Sender [in] Reference to object that triggered the event.
@param Desc [in] Description of error.
@param URL [in] URL of document triggering error.
@param Line [in] Line number where error occurred.
}
THTMLWdwErrorEvent = procedure(Sender: TObject; const Desc, URL: string;
const Line: Integer) of object;
{
THTMLDocEventSink:
Event sink for HTML document events. Sinks HTMLDocumentEvents2 events.
}
THTMLWdwEventSink = class(TAbstractHTMLEventSink,
IUnknown, IDispatch
)
private
fOnError: THTMLWdwErrorEvent;
{Reference to any OnError event handler}
protected
procedure DispatchEvent(var InvokeInfo: TInvokeInfo); override;
{Dispatches HTMLWindowEvents2 events.
@param InvokeInfo [in] Structure containing about event to be
dispatched. Fields of this structure are updated to notify result of
event invocation.
}
public
constructor Create;
{Class constructor. Sets up object to sink HTMLWindowEvents2 events.
}
property OnError: THTMLWdwErrorEvent read fOnError write fOnError;
{Event triggered when browser's onerror event is invoked}
end;
implementation
uses
// Project
SysUtils, Variants, Windows, ComObj;
{ THTMLEventInfo }
constructor THTMLEventInfo.Create(const DispatchID: Integer;
const Args: IHTMLEventObj; const CanCancel: Boolean);
{Class constructor. Sets up object.
@param DispatchId [in] Dispatch ID of the event.
@param Args [in] Information about the event provided by browser.
@param CanCancel [in] Flag indicating whether event can be cancelled or not.
}
begin
inherited Create;
// Initialise properties
fDispatchId := DispatchID;
fArgs := Args;
fCanCancel := CanCancel;
fCancelled := False;
end;
function THTMLEventInfo.GetName: string;
{Read accessor for Name property.
@return Name of event.
}
begin
Result := fArgs.type_;
end;
procedure THTMLEventInfo.SetCancelled(const Value: Boolean);
{Write accessor for Cancelled property. Sets property only if CanCancel
property is True.
@param Value [in] New property value.
}
begin
if CanCancel then
fCancelled := Value;
end;
{ TAbstractHTMLEventSink }
procedure TAbstractHTMLEventSink.Connect(const Source: IDispatch);
{Connects the event sink to the object that is the source of the events.
@param Source [in] Reference to source object.
}
begin
Disconnect;
fSource := Source;
InterfaceConnect(fSource, fSinkIID, Self, fConnectionCookie);
end;
constructor TAbstractHTMLEventSink.Create(const SinkIID: TGUID);
{Class constructor. Sets up the object.
@param SinkIID [in] Interface of events to be sunk by this object.
}
begin
inherited Create;
fSinkIID := SinkIID;
end;
destructor TAbstractHTMLEventSink.Destroy;
{Class destructor. Disconnects the sink from the event source and tears down
the object.
}
begin
Disconnect;
inherited;
end;
procedure TAbstractHTMLEventSink.Disconnect;
{Disconnects the event sink from the event source object.
}
begin
if Assigned(fSource) then
begin
InterfaceDisconnect(fSource, fSinkIID, fConnectionCookie);
fSource := nil;
end;
end;
procedure TAbstractHTMLEventSink.DispatchStdEvent(var InvokeInfo: TInvokeInfo;
const CanCancel: Boolean);
{Triggers given "standard" event, passing information about the event. Sets
function result if event is a function. Notifies an error if method call is
not a valid standard event.
@param InvokeInfo [in/out] Information about event method invocation.
Structure is updated with information about return value and any error
condition.
@param CanCancel [in] Flag indicating whether event can be cancelled.
}
var
EventArgs: IHTMLEventObj; // object encapsulating event arguments
EventInfo: THTMLEventInfo; // object providing information about event
begin
// Get event arguments from method's parameter list: stores any error code in
// InvokeInfo
GetEventArgParam(InvokeInfo, EventArgs);
if not Succeeded(InvokeInfo.SCode) then
Exit;
// Create object to store info about the event
EventInfo := THTMLEventInfo.Create(
InvokeInfo.DispatchID, EventArgs, CanCancel
);
try
// Trigger the event
if Assigned(fOnEvent) then
fOnEvent(Self, EventInfo);
// Store whether event is cancelled as return value only if event is
// cancelable.
if EventInfo.CanCancel then
InvokeInfo.FnResult := not EventInfo.Cancelled
finally
FreeAndNil(EventInfo);
end;
end;
procedure TAbstractHTMLEventSink.DoInvoke(var InvokeInfo: TInvokeInfo);
{Dispatches events via abstract DispatchEvent method after checking for valid
context.
@param InvokeInfo [in/out] Information about method / property invocation.
Structure is updated with information event invocation.
}
begin
// We only accept method calls, not properties
if InvokeInfo.Flags and DISPATCH_METHOD = 0 then
raise Exception.Create( // ** do not localise
'TAbstractHTMLEventSink does not support properties'
);
// We don't handle named parameters
if InvokeInfo.Params.cNamedArgs > 0 then
raise Exception.Create( // ** do not localise
'TAbstractHTMLEventSink does not support named methods'
);
DispatchEvent(InvokeInfo);
end;
procedure TAbstractHTMLEventSink.GetEventArgParam(var InvokeInfo: TInvokeInfo;
out EventArgs: IHTMLEventObj);
{Gets the single IHTMLEventObj parameter of a "standard" HTML event. Notifies
caller of error if there is more than one parameter or if parameter is wrong
type.
@param InvokeInfo [in/out] Information about method invocation. Structure is
updated with information about any error.
@param EventArgs [out] Set to object giving information information about
event arguments or nil if parameter is not valid.
}
begin
EventArgs := nil;
if InvokeInfo.Params.cArgs = 1 then
// Get interface IHTMLEventObj interface from only param
// GetValidParam sets error info in InvokeInfo if param not valid
GetValidParam(InvokeInfo, 0, varDispatch, IHTMLEventObj, EventArgs)
else
// Incorrect number of params: set error code accordingly
InvokeInfo.SCode := DISP_E_BADPARAMCOUNT;
end;
function TAbstractHTMLEventSink.QueryInterface(const IID: TGUID;
out Obj): HResult;
{Checks the specified interface is supported by this object. If so reference
to interface is passed out. Supported interfaces are IUnknown, IDispatch and
interface implemented by event sink.
@param [in] IID specifies interface being queried.
@param [out] Reference to interface implementation or nil if not supported.
@return S_OK if interface supported or E_NOINTERFACE if not supported.
}
begin
Result := S_OK;
if GetInterface(IID, Obj) then
Exit;
if IsEqualGUID(fSinkIID, IID) and GetInterface(IDispatch, Obj) then
Exit;
Result := E_NOINTERFACE;
Pointer(Obj) := nil;
end;
{ THTMLDocEventSink }
constructor THTMLDocEventSink.Create;
{Class constructor. Sets up object to sink HTMLDocumentEvents2 events.
}
begin
inherited Create(HTMLDocumentEvents2);
end;
procedure THTMLDocEventSink.DispatchEvent(var InvokeInfo: TInvokeInfo);
{Dispatches HTMLDocumentEvents2 events.
@param InvokeInfo [in] Structure containing about event to be dispatched.
Fields of this structure are updated to notify result of event invocation.
}
begin
inherited;
// Dispatch events
case InvokeInfo.DispatchID of
cDocEventOnHelp: DispatchStdEvent(InvokeInfo, True);
cDocEventOnClick: DispatchStdEvent(InvokeInfo, True);
cDocEventOnDblClick: DispatchStdEvent(InvokeInfo, True);
cDocEventOnKeyDown: DispatchStdEvent(InvokeInfo, False);
cDocEventOnKeyUp: DispatchStdEvent(InvokeInfo, False);
cDocEventOnKeyPress: DispatchStdEvent(InvokeInfo, True);
cDocEventOnMouseDown: DispatchStdEvent(InvokeInfo, False);
cDocEventOnMouseMove: DispatchStdEvent(InvokeInfo, False);
cDocEventOnMouseUp: DispatchStdEvent(InvokeInfo, False);
cDocEventOnMouseOut: DispatchStdEvent(InvokeInfo, False);
cDocEventOnMouseOver: DispatchStdEvent(InvokeInfo, False);
cDocEventOnReadyStateChange: DispatchStdEvent(InvokeInfo, False);
cDocEventOnBeforeUpdate: DispatchStdEvent(InvokeInfo, True);
cDocEventOnAfterUpdate: DispatchStdEvent(InvokeInfo, False);
cDocEventOnRowExit: DispatchStdEvent(InvokeInfo, True);
cDocEventOnRowEnter: DispatchStdEvent(InvokeInfo, False);
cDocEventOnDragStart: DispatchStdEvent(InvokeInfo, True);
cDocEventOnSelectStart: DispatchStdEvent(InvokeInfo, True);
cDocEventOnErrorUpdate: DispatchStdEvent(InvokeInfo, True);
cDocEventOnContextMenu: DispatchStdEvent(InvokeInfo, True);
cDocEventOnStop: DispatchStdEvent(InvokeInfo, True);
cDocEventOnRowsDelete: DispatchStdEvent(InvokeInfo, False);
cDocEventOnRowsInserted: DispatchStdEvent(InvokeInfo, False);
cDocEventOnCellChange: DispatchStdEvent(InvokeInfo, False);
cDocEventOnPropertyChange: DispatchStdEvent(InvokeInfo, False);
cDocEventOnDatasetChanged: DispatchStdEvent(InvokeInfo, False);
cDocEventOnDataAvailable: DispatchStdEvent(InvokeInfo, False);
cDocEventOnDatasetComplete: DispatchStdEvent(InvokeInfo, False);
cDocEventOnBeforeEditFocus: DispatchStdEvent(InvokeInfo, False);
else
InvokeInfo.SCode := DISP_E_MEMBERNOTFOUND;
end;
end;
{ THTMLWdwEventSink }
constructor THTMLWdwEventSink.Create;
{Class constructor. Sets up object to sink HTMLWindowEvents2 events.
}
begin
inherited Create(HTMLWindowEvents2);
end;
procedure THTMLWdwEventSink.DispatchEvent(var InvokeInfo: TInvokeInfo);
{Dispatches HTMLWindowEvents2 events.
@param InvokeInfo [in] Structure containing about event to be dispatched.
Fields of this structure are updated to notify result of event invocation.
}
procedure DispatchErrorEvent;
{Dispatches the OnError event.
}
var
Description: WideString; // description of error
URL: WideString; // URL of document that encountered error
Line: Integer; // line number of error in document
V: OleVariant; // value of each validated parameter
begin
// NOTE: parameters are stored in reverse order in InvokeInfo.Params.rgvarg.
// check required number of params
if InvokeInfo.Params.cArgs <> 3 then
begin
InvokeInfo.SCode := DISP_E_BADPARAMCOUNT;
Exit;
end;
// validate 1st (description) param: must be string
if not GetValidParam(InvokeInfo, 2, varOleStr, V) then
Exit;
Description := V;
// validate 2nd (URL) param: must be string
if not GetValidParam(InvokeInfo, 1, varOleStr, V) then
Exit;
URL := V;
// validate 3rd (Line) param: must be integer
if not GetValidParam(InvokeInfo, 0, varInteger, V) then
Exit;
Line := V;
// Everything's OK: trigger any assigned event
if Assigned(fOnError) then
fOnError(Self, Description, URL, Line);
end;
begin
inherited;
// Dispatch events
case InvokeInfo.DispatchID of
cWdwEventOnLoad: DispatchStdEvent(InvokeInfo, False);
cWdwEventOnUnload: DispatchStdEvent(InvokeInfo, False);
cWdwEventOnHelp: DispatchStdEvent(InvokeInfo, True);
cWdwEventOnFocus: DispatchStdEvent(InvokeInfo, False);
cWdwEventOnBlur: DispatchStdEvent(InvokeInfo, False);
cWdwEventOnError: DispatchErrorEvent;
cWdwEventOnResize: DispatchStdEvent(InvokeInfo, False);
cWdwEventOnScroll: DispatchStdEvent(InvokeInfo, False);
cWdwEventOnBeforeUnload: DispatchStdEvent(InvokeInfo, False);
cWdwEventOnBeforePrint: DispatchStdEvent(InvokeInfo, False);
cWdwEventOnAfterPrint: DispatchStdEvent(InvokeInfo, False);
else
InvokeInfo.SCode := DISP_E_MEMBERNOTFOUND;
end;
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.