diff --git a/uutlEvent.pas b/uutlEvent.pas index fd772d3..e971483 100644 --- a/uutlEvent.pas +++ b/uutlEvent.pas @@ -27,20 +27,22 @@ type ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TutlEvent = class(TInterfacedObject, IutlEvent) private + fSender: TObject; fEventType: TutlEventType; fTimestamp: Single; public + property Sender: TObject read fSender; property EventType: TutlEventType read fEventType; property Timestamp: Single read fTimestamp; - constructor Create(const aEventType: TutlEventType); + constructor Create(const aSender: TObject; const aEventType: TutlEventType); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// IutlEventListener = interface(IUnknown) ['{BC45E26B-96F7-4151-87F1-C330C8C668E5}'] - procedure DispatchEvent(const aEvent: IutlEvent); + procedure DispatchEvent(aEvent: IutlEvent); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -54,7 +56,7 @@ type public property Empty: Boolean read GetEmpty; - procedure DispatchEvent(const aEvent: IutlEvent); virtual; + procedure DispatchEvent(aEvent: IutlEvent); virtual; function RegisterListener(const aListener: IutlEventListener): Boolean; function UnregisterListener(const aListener: IutlEventListener): Boolean; @@ -65,11 +67,11 @@ type ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TutlEventListenerCallback = class(TInterfacedObject, IutlEventListener) public type - TCallback = procedure(const aEvent: IutlEvent) of object; + TCallback = procedure(aEvent: IutlEvent) of object; private fCallback: TCallback; private { IEventListener } - procedure DispatchEvent(const aEvent: IutlEvent); + procedure DispatchEvent(aEvent: IutlEvent); public constructor Create(const aCallback: TCallback); end; @@ -85,7 +87,7 @@ type function PopEvent: IutlEvent; private { IEventListener } - procedure DispatchEvent(const aEvent: IutlEvent); + procedure DispatchEvent(aEvent: IutlEvent); public function RegisterListener(const aListener: IutlEventListener): Boolean; @@ -100,6 +102,9 @@ type implementation uses +{$IFDEF LOG_DEBUG} + uutlLogger, +{$ENDIF} uutlTiming; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -113,11 +118,15 @@ end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TutlEvent////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -constructor TutlEvent.Create(const aEventType: TutlEventType); +constructor TutlEvent.Create(const aSender: TObject; const aEventType: TutlEventType); begin inherited Create; - fTimestamp := GetMicroTime / 1000000; + fSender := aSender; fEventType := aEventType; + fTimestamp := GetMicroTime / 1000000; +{$IFDEF LOG_DEBUG} + utlLogger.Debug(self, 'dispatch event (Sender=%s[%p]; EventType=%0.10d; Timestamp=%10.5f)', [ fSender.ClassName, Pointer(fSender), fEventType, fTimestamp ]); +{$ENDIF} end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -142,7 +151,7 @@ begin end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -procedure TutlEventListenerSet.DispatchEvent(const aEvent: IutlEvent); +procedure TutlEventListenerSet.DispatchEvent(aEvent: IutlEvent); var l: IutlEventListener; begin @@ -180,7 +189,7 @@ end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TutlEventListenerCallback////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -procedure TutlEventListenerCallback.DispatchEvent(const aEvent: IutlEvent); +procedure TutlEventListenerCallback.DispatchEvent(aEvent: IutlEvent); begin fCallback(aEvent); end; @@ -210,7 +219,7 @@ begin end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -procedure TutlEventListenerAsync.DispatchEvent(const aEvent: IutlEvent); +procedure TutlEventListenerAsync.DispatchEvent(aEvent: IutlEvent); begin fEventLock.Enter; try diff --git a/uutlEventManager.pas b/uutlEventManager.pas index d845386..408c7e3 100644 --- a/uutlEventManager.pas +++ b/uutlEventManager.pas @@ -17,12 +17,13 @@ type TutlMouseButtons = set of TMouseButton; TutlWinControlEvent = class(TutlEvent) private - fSender: TControl; + function GetControl: TControl; + public - property Sender: TControl read fSender; + property Control: TControl read GetControl; constructor Create( - const aEventType: TutlEventType; - const aSender: TControl); + const aSender: TControl; + const aEventType: TutlEventType); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -38,8 +39,8 @@ type property ScreenPos: TPoint read fScreenPos; constructor Create( - const aEventType: TutlEventType; const aSender: TControl; + const aEventType: TutlEventType; const aButtons: TutlMouseButtons; const aClientPos: TPoint); end; @@ -73,8 +74,8 @@ type property KeyCode: Word read fKeyCode; constructor Create( - const aEventType: TutlEventType; const aSender: TControl; + const aEventType: TutlEventType; const aCharCode: WideChar; const aKeyCode: Word); end; @@ -92,8 +93,8 @@ type property ClientHeight: Cardinal read fClientHeight; constructor Create( - const aEventType: TutlEventType; const aSender: TControl; + const aEventType: TutlEventType; const aScreenRect: TRect; const aClientWidth: Cardinal; const aClientHeight: Cardinal); @@ -208,7 +209,7 @@ type property Mouse: TMouseState read fMouse; property Window: TWindowState read fWindow; - procedure DispatchEvent(const aEvent: IutlEvent); override; + procedure DispatchEvent(aEvent: IutlEvent); override; procedure AttachEvents(const aControl: TWinControl; const aTypes: TutlEventTypes); end; @@ -240,25 +241,32 @@ type ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TutlWinControlEvent//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -constructor TutlWinControlEvent.Create(const aEventType: TutlEventType; const aSender: TControl); +function TutlWinControlEvent.GetControl: TControl; +begin + result := (Sender as TControl); +end; + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +constructor TutlWinControlEvent.Create( + const aSender: TControl; + const aEventType: TutlEventType); begin - inherited Create(aEventType); - fSender := aSender; + inherited Create(aSender, aEventType); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TutlMouseEvent///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TutlMouseEvent.Create( - const aEventType: TutlEventType; const aSender: TControl; + const aEventType: TutlEventType; const aButtons: TutlMouseButtons; const aClientPos: TPoint); begin - inherited Create(aEventType, aSender); + inherited Create(aSender, aEventType); fButtons := aButtons; fClientPos := aClientPos; - fScreenPos := fSender.ClientToScreen(fClientPos); + fScreenPos := Control.ClientToScreen(fClientPos); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -270,23 +278,23 @@ constructor TutlMouseWheelEvent.Create( const aClientPos: TPoint); begin if (aWheelDelta < 0) - then inherited Create(TutlWinControlEventManager.EVENT_MOUSE_WHEEL_DOWN, aSender) - else inherited Create(TutlWinControlEventManager.EVENT_MOUSE_WHEEL_UP, aSender); + then inherited Create(aSender, TutlWinControlEventManager.EVENT_MOUSE_WHEEL_DOWN) + else inherited Create(aSender, TutlWinControlEventManager.EVENT_MOUSE_WHEEL_UP); fWheelDelta := aWheelDelta; fClientPos := aClientPos; - fScreenPos := fSender.ClientToScreen(fClientPos); + fScreenPos := Control.ClientToScreen(fClientPos); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TutlKeyEvent/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TutlKeyEvent.Create( - const aEventType: TutlEventType; const aSender: TControl; + const aEventType: TutlEventType; const aCharCode: WideChar; const aKeyCode: Word); begin - inherited Create(aEventType, aSender); + inherited Create(aSender, aEventType); fCharCode := aCharCode; fKeyCode := aKeyCode; end; @@ -295,13 +303,13 @@ end; //TutlWindowEvent//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TutlWindowEvent.Create( - const aEventType: TutlEventType; const aSender: TControl; + const aEventType: TutlEventType; const aScreenRect: TRect; const aClientWidth: Cardinal; const aClientHeight: Cardinal); begin - inherited Create(aEventType, aSender); + inherited Create(aSender, aEventType); fScreenRect := aScreenRect; fClientWidth := aClientWidth; fClientHeight := aClientHeight; @@ -472,8 +480,8 @@ function TutlWinControlEventManager.CreateMouseEvent(aSender: TObject; aType: Tu aButtons: TutlMouseButtons; aClientPos: TPoint): TutlMouseEvent; begin result := TutlMouseEvent.Create( - aType, (aSender as TControl), + aType, aButtons, aClientPos); end; @@ -494,8 +502,8 @@ begin if (aType = EVENT_KEY_DOWN) and fKeyboard.KeyState[aKey and $FF] then aType := EVENT_KEY_REPEAT; result := TutlKeyEvent.Create( - aType, (aSender as TControl), + aType, VKCodeToCharCode(aKey, fKeyboard.Modifiers), aKey); end; @@ -510,15 +518,15 @@ begin p1 := ClientToScreen(Point(Width, Height)); end; result := TutlWindowEvent.Create( - aType, (aSender as TControl), + aType, Rect(p0.x, p0.y, p1.x, p1.y), (aSender as TWinControl).ClientWidth, (aSender as TWinControl).ClientHeight); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -procedure TutlWinControlEventManager.DispatchEvent(const aEvent: IutlEvent); +procedure TutlWinControlEventManager.DispatchEvent(aEvent: IutlEvent); begin RecordEvent(aEvent); inherited DispatchEvent(aEvent);