LazMapViewer: No longer delegate execution of user events to plugin manager.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@9537 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz 2024-12-16 00:43:38 +00:00
parent 434196e0e2
commit 48e69cae28
4 changed files with 102 additions and 132 deletions

View File

@ -5,7 +5,7 @@ unit Unit1;
interface
uses
Classes, SysUtils, Types,
Classes, SysUtils,
LCLIntf, Forms, Controls, Graphics, ExtCtrls, StdCtrls, Dialogs, Spin,
TAGraph, TATools,
mvMapViewer, mvPluginCore, mvPlugins;

View File

@ -17,6 +17,7 @@ uses
begin
RequireDerivedFormResource := True;
Application.Title := 'MapScale_Demo';
Application.Scaled := True;
{$PUSH}{$WARN 5044 OFF}
Application.MainFormOnTaskbar := True;

View File

@ -439,28 +439,23 @@ type
protected
procedure AddMapView(AMapView: TMapView); virtual;
procedure RemoveMapView(AMapView: TMapView); virtual;
procedure DefaultMouseEvent(AMapView: TMapView; AButton: TMouseButton;
AShift: TShiftState; X, Y: Integer; AMapEvent: TMouseEvent);
procedure DefaultNotifyEventHandler(AMapView: TMapView; AMapEvent: TNotifyEvent);
function AfterDrawObjects(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; virtual;
function AfterPaint(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; virtual;
function BeforeDrawObjects(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; virtual;
function CenterMove(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; virtual;
function MouseDown(AMapView: TMapView; AButton: TMouseButton; AShift: TShiftState;
X, Y: Integer; AMapEvent: TMouseEvent): Boolean; virtual;
function MouseEnter(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; virtual;
function MouseLeave(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; virtual;
function MouseMove(AMapView: TMapView; AShift: TShiftState; X,Y: Integer;
AMapEvent: TMouseMoveEvent): Boolean; virtual;
function MouseUp(AMapView: TMapView; AButton: TMouseButton; AShift: TShiftState;
X, Y: Integer; AMapEvent: TMouseEvent): Boolean; virtual;
function MouseWheel(AMapView: TMapView; AShift: TShiftState; AWheelDelta: Integer;
AMousePos: TPoint): Boolean; virtual;
function ZoomChange(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; virtual;
protected
function AfterDrawObjects(AMapView: TMapView): Boolean; virtual;
function AfterPaint(AMapView: TMapView): Boolean; virtual;
function BeforeDrawObjects(AMapView: TMapView): Boolean; virtual;
function CenterMove(AMapView: TMapView): Boolean; virtual;
function GPSItemsModified(AMapView: TMapView; ModifiedList: TGPSObjectList;
ActualObjs: TGPSObjList; Adding: Boolean): Boolean; virtual;
function MouseDown(AMapView: TMapView; AButton: TMouseButton; AShift: TShiftState;
X, Y: Integer): Boolean; virtual;
function MouseEnter(AMapView: TMapView): Boolean; virtual;
function MouseLeave(AMapView: TMapView): Boolean; virtual;
function MouseMove(AMapView: TMapView; AShift: TShiftState; X,Y: Integer): Boolean; virtual;
function MouseUp(AMapView: TMapView; AButton: TMouseButton; AShift: TShiftState;
X, Y: Integer): Boolean; virtual;
function MouseWheel(AMapView: TMapView; AShift: TShiftState; AWheelDelta: Integer;
AMousePos: TPoint): Boolean; virtual;
function ZoomChange(AMapView: TMapView): Boolean; virtual;
public
end;
@ -2218,7 +2213,9 @@ end;
procedure TMapView.DoZoomChange(Sender: TObject);
begin
GetPluginManager.ZoomChange(Self, FOnZoomChange);
GetPluginManager.ZoomChange(Self);
if Assigned(FOnZoomChange) then
FOnZoomChange(Self);
end;
function TMapView.GetOnChange: TNotifyEvent;
@ -2554,14 +2551,19 @@ var
savedOnMouseDown: TMouseEvent;
lHandled : Boolean;
begin
{ The inherited method calls the user's OnMouseDown at its end. But we
always want the plugin methods to be executed before the user event.
The following code makes sure that this happens. }
savedOnMouseDown := OnMouseDown;
try
OnMouseDown := nil; // Avoid handling user OnMouseDown before the plugin manager
OnMouseDown := nil;
inherited MouseDown(Button, Shift, X, Y);
finally
OnMouseDown := savedOnMouseDown;
end;
lHandled := GetPluginManager.MouseDown(Self, Button, Shift, X, Y, OnMouseDown);
lHandled := GetPluginManager.MouseDown(Self, Button, Shift, X, Y);
if Assigned(OnMouseDown) then
OnMouseDown(Self, Button, Shift, X, Y);
if EditingEnabled then
begin
@ -2588,17 +2590,9 @@ end;
procedure TMapView.MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
var
savedOnMouseUp: TMouseEvent;
begin
savedOnMouseUp := OnMouseUp;
try
OnMouseUp := nil; // Avoid handling user OnMouseUp before the plugin manager
inherited MouseUp(Button, Shift, X, Y);
finally
OnMouseUp := savedOnMouseUp;
end;
GetPluginManager.MouseUp(Self, Button, Shift, X, Y, OnMouseUp);
GetPluginManager.MouseUp(Self, Button, Shift, X, Y);
inherited; // This fires the OnMouseUp event, and nothing else.
if IsActive then
if Button = mbLeft then
@ -2646,16 +2640,20 @@ procedure TMapView.MouseMove(Shift: TShiftState; X, Y: Integer);
var
savedOnMouseMove: TMouseMoveEvent;
lHandled : Boolean;
lHandled: Boolean;
begin
{ The inherited MouseMove handles the OnMouseMove user event after some operations.
The following code makes sure that it is executed AFTER the plugins. }
savedOnMouseMove := OnMouseMove;
try
OnMouseMove := nil; // Avoid handling user OnMouseMove before plugin manager
OnMouseMove := nil;
inherited MouseMove(Shift, X, Y);
finally
OnMouseMove := savedOnMouseMove;
end;
lHandled := GetPluginManager.MouseMove(Self, Shift, X, Y, OnMouseMove);
lHandled := GetPluginManager.MouseMove(Self, Shift, X, Y);
if Assigned(OnMouseMove) then
OnMouseMove(Self, Shift, X, Y);
if IsActive then
begin
Engine.MouseMove(self,Shift,X,Y);
@ -2670,12 +2668,14 @@ end;
procedure TMapView.MouseEnter;
begin
GetPluginManager.MouseEnter(Self, OnMouseEnter);
GetPluginManager.MouseEnter(Self);
inherited; // this calls OnMouseEnter, and nothing else
end;
procedure TMapView.MouseLeave;
begin
GetPluginManager.MouseLeave(Self, OnMouseLeave);
GetPluginManager.MouseLeave(Self);
inherited; // this fires OnMouseLeave, and nothing else
end;
procedure TMapView.Notification(AComponent: TComponent; Operation: TOperation);
@ -2748,15 +2748,23 @@ const
if Cyclic then
W := Min(1 shl Zoom * TileSize.CX, W);
GetPluginManager.BeforeDrawObjects(Self, FBeforeDrawObjectsEvent);
GetPluginManager.BeforeDrawObjects(Self);
if Assigned(FBeforeDrawObjectsEvent) then
FBeforeDrawObjectsEvent(Self);
DrawObjects(Default(TTileId), 0, 0, W - 1, ClientHeight);
GetPluginManager.AfterDrawObjects(Self, FAfterDrawObjectsEvent);
GetPluginManager.AfterDrawObjects(Self);
if Assigned(FAfterDrawObjectsEvent) then
FAfterDrawObjectsEvent(Self);
DrawingEngine.PaintToCanvas(Canvas);
if DebugTiles then
DrawCenter;
GetPluginManager.AfterPaint(Self, FAfterPaintEvent);
GetPluginManager.AfterPaint(Self);
if Assigned(FAfterPaintEvent) then
FAfterPaintEvent(Self);
end;
procedure DragDraw;
@ -3211,7 +3219,9 @@ end;
procedure TMapView.DoCenterMove(Sender: TObject);
begin
GetPluginManager.CenterMove(Self, FOnCenterMove);
GetPluginManager.CenterMove(Self);
if Assigned(FOnCenterMove) then
FOnCenterMove(Self);
end;
procedure TMapView.DoDrawStretchedTile(const TileId: TTileID; X, Y: Integer;
@ -3224,7 +3234,6 @@ begin
if FDebugTiles then
DoDrawTileInfo(TileID, X, Y);
end;
procedure TMapView.DoDrawTile(const TileId: TTileId; X, Y: integer;
@ -4370,115 +4379,93 @@ end;
procedure TMvCustomPluginManager.AddMapView(AMapView: TMapView);
begin
//
Unused(AMapView);
end;
{ Just executes the handler assigned to the OnAfterDrawObjects event of the
mapview. The descendant plugin manager will have to iterate over all plugins
used by it. It will have to set the function result to true of onlf the
plugin has "handled" the event. }
function TMvCustomPluginManager.AfterDrawObjects(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvCustomPluginManager.AfterDrawObjects(AMapView: TMapView): Boolean;
begin
Unused(AMapView);
Result := False;
DefaultNotifyEventHandler(AMapView, AMapEvent);
end;
function TMvCustomPluginManager.AfterPaint(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvCustomPluginManager.AfterPaint(AMapView: TMapView): Boolean;
begin
Unused(AMapView);
Result := False;
DefaultNotifyEventHandler(AMapView, AMapEvent);
end;
function TMvCustomPluginManager.BeforeDrawObjects(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvCustomPluginManager.BeforeDrawObjects(AMapView: TMapView): Boolean;
begin
Unused(AMapView);
Result := False;
DefaultNotifyEventHandler(AMapView, AMapEvent);
end;
function TMvCustomPluginManager.CenterMove(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvCustomPluginManager.CenterMove(AMapView: TMapView): Boolean;
begin
Unused(AMapView);
Result := False;
DefaultNotifyEventHandler(AMapView, AMapEvent);
end;
procedure TMvCustomPluginManager.DefaultMouseEvent(AMapView: TMapView;
AButton: TMouseButton; AShift: TShiftState; X, Y: Integer; AMapEvent: TMouseEvent);
begin
if Assigned(AMapEvent) then
AMapEvent(AMapView, AButton, AShift, X, Y);
end;
procedure TMvCustomPluginManager.DefaultNotifyEventHandler(AMapView: TMapView;
AMapEvent: TNotifyEvent);
begin
if Assigned(AMapEvent) then
AMapEvent(AMapView);
end;
function TMvCustomPluginManager.GPSItemsModified(AMapView: TMapView;
ModifiedList: TGPSObjectList; ActualObjs: TGPSObjList; Adding: Boolean): Boolean;
begin
Unused(AMapView);
Unused(ModifiedList, ActualObjs, Adding);
Result := false;
end;
function TMvCustomPluginManager.MouseDown(AMapView: TMapView;
AButton: TMouseButton; AShift: TShiftState; X, Y: Integer;
AMapEvent: TMouseEvent): Boolean;
AButton: TMouseButton; AShift: TShiftState; X, Y: Integer): Boolean;
begin
Unused(AMapView, AButton, AShift);
Unused(X, Y);
Result := False;
DefaultMouseEvent(AMapView, AButton, AShift, X, Y, AMapEvent);
end;
function TMvCustomPluginManager.MouseEnter(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvCustomPluginManager.MouseEnter(AMapView: TMapView): Boolean;
begin
Unused(AMapView);
Result := False;
DefaultNotifyEventHandler(AMapView, AMapEvent);
end;
function TMvCustomPluginManager.MouseLeave(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvCustomPluginManager.MouseLeave(AMapView: TMapView): Boolean;
begin
Unused(AMapView);
Result := False;
DefaultNotifyEventHandler(AMapView, AMapEvent);
end;
function TMvCustomPluginManager.MouseMove(AMapView: TMapView;
AShift: TShiftState; X, Y: Integer; AMapEvent: TMouseMoveEvent): Boolean;
AShift: TShiftState; X, Y: Integer): Boolean;
begin
Unused(AMapView);
Result := False;
if Assigned(AMapEvent) then
AMapEvent(AMapView, AShift, X, Y);
end;
function TMvCustomPluginManager.MouseUp(AMapView: TMapView;
AButton: TMouseButton; AShift: TShiftState; X, Y: Integer;
AMapEvent: TMouseEvent): Boolean;
AButton: TMouseButton; AShift: TShiftState; X, Y: Integer): Boolean;
begin
Unused(AMapView, AButton, AShift);
Unused(X, Y);
Result := False;
DefaultMouseEvent(AMapView, AButton, AShift, X, Y, AMapEvent);
end;
// No user event here; it is handled by the MapView itself.
function TMvCustomPluginManager.MouseWheel(AMapView: TMapView; AShift: TShiftState;
AWheelDelta: Integer; AMousePos: TPoint): Boolean;
begin
Unused(AMapView);
Unused(AShift, AWheelDelta, AMousePos);
Result := False;
end;
procedure TMvCustomPluginManager.RemoveMapView(AMapView: TMapView);
begin
//
Unused(AMapView);
end;
function TMvCustomPluginManager.ZoomChange(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean;
function TMvCustomPluginManager.ZoomChange(AMapView: TMapView): Boolean;
begin
Unused(AMapView);
Result := false;
DefaultNotifyEventHandler(AMapView, AMapEvent);
end;
end.

View File

@ -180,23 +180,22 @@ type
procedure SetName(const AValue: TComponentName); override;
protected
// Dispatching events to be handled by the plugins
function AfterDrawObjects(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; override;
function AfterPaint(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; override;
function BeforeDrawObjects(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; override;
function CenterMove(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; override;
function AfterDrawObjects(AMapView: TMapView): Boolean; override;
function AfterPaint(AMapView: TMapView): Boolean; override;
function BeforeDrawObjects(AMapView: TMapView): Boolean; override;
function CenterMove(AMapView: TMapView): Boolean; override;
function GPSItemsModified(AMapView: TMapView; ModifiedList: TGPSObjectList;
ActualObjs: TGPSObjList; Adding: Boolean): Boolean; override;
function MouseDown(AMapView: TMapView; AButton: TMouseButton; AShift: TShiftState;
X, Y: Integer; AMapEvent: TMouseEvent): Boolean; override;
function MouseEnter(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; override;
function MouseLeave(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; override;
function MouseMove(AMapView: TMapView; AShift: TShiftState; X,Y: Integer;
AMapEvent: TMouseMoveEvent): Boolean; override;
X, Y: Integer): Boolean; override;
function MouseEnter(AMapView: TMapView): Boolean; override;
function MouseLeave(AMapView: TMapView): Boolean; override;
function MouseMove(AMapView: TMapView; AShift: TShiftState; X,Y: Integer): Boolean; override;
function MouseUp(AMapView: TMapView; AButton: TMouseButton; AShift: TShiftState;
X, Y: Integer; AMapEvent: TMouseEvent): Boolean; override;
X, Y: Integer): Boolean; override;
function MouseWheel(AMapView: TMapView; AShift: TShiftState; AWheelDelta: Integer;
AMousePos: TPoint): Boolean; override;
function ZoomChange(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean; override;
function ZoomChange(AMapView: TMapView): Boolean; override;
// procedure ZoomChanging(AMapView: TMapView; NewZoom: Integer; var Allow: Boolean; AMapEvent); override;
public
@ -691,8 +690,7 @@ begin
FMapList.Add(AMapView);
end;
function TMvPluginManager.AfterDrawObjects(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvPluginManager.AfterDrawObjects(AMapView: TMapView): Boolean;
var
i: Integer;
plugin: TMvCustomPlugin;
@ -704,11 +702,9 @@ begin
if HandlePlugin(plugin, AMapView) then
plugin.AfterDrawObjects(AMapView, Result);
end;
inherited AfterDrawObjects(AMapView, AMapEvent);
end;
function TMvPluginManager.AfterPaint(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvPluginManager.AfterPaint(AMapView: TMapView): Boolean;
var
i: Integer;
plugin: TMvCustomPlugin;
@ -720,11 +716,9 @@ begin
if HandlePlugin(plugin, AMapView) then
plugin.AfterPaint(AMapView, Result);
end;
inherited AfterPaint(AMapView, AMapEvent);
end;
function TMvPluginManager.BeforeDrawObjects(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvPluginManager.BeforeDrawObjects(AMapView: TMapView): Boolean;
var
i: Integer;
plugin: TMvCustomPlugin;
@ -736,11 +730,9 @@ begin
if HandlePlugin(plugin, AMapView) then
plugin.BeforeDrawObjects(AMapView, Result);
end;
inherited BeforeDrawObjects(AMapView, AMapEvent);
end;
function TMvPluginManager.CenterMove(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvPluginManager.CenterMove(AMapView: TMapView): Boolean;
var
i: Integer;
plugin: TMvCustomPlugin;
@ -752,7 +744,6 @@ begin
if HandlePlugin(plugin, AMapView) then
plugin.CenterMove(AMapView, Result);
end;
inherited CenterMove(AMapView, AMapEvent);
end;
procedure TMvPluginManager.GetChildren(Proc: TGetChildProc; Root: TComponent);
@ -803,7 +794,7 @@ begin
end;
function TMvPluginManager.MouseDown(AMapView: TMapView; AButton: TMouseButton;
AShift: TShiftState; X, Y: Integer; AMapEvent: TMouseEvent): Boolean;
AShift: TShiftState; X, Y: Integer): Boolean;
var
i: Integer;
plugin: TMvCustomPlugin;
@ -815,11 +806,9 @@ begin
if HandlePlugin(plugin, AMapView) then
plugin.MouseDown(AMapView, AButton, AShift, X, Y, Result);
end;
inherited MouseDown(AMapView, AButton, AShift, X, Y, AMapEvent);
end;
function TMvPluginManager.MouseEnter(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvPluginManager.MouseEnter(AMapView: TMapView): Boolean;
var
i: Integer;
plugin: TMvCustomPlugin;
@ -831,11 +820,9 @@ begin
if HandlePlugin(plugin, AMapView) then
plugin.MouseEnter(AMapView, Result);
end;
inherited MouseEnter(AMapView, AMapEvent);
end;
function TMvPluginManager.MouseLeave(AMapView: TMapView;
AMapEvent: TNotifyEvent): Boolean;
function TMvPluginManager.MouseLeave(AMapView: TMapView): Boolean;
var
i: Integer;
plugin: TMvCustomPlugin;
@ -847,11 +834,10 @@ begin
if HandlePlugin(plugin, AMapView) then
plugin.MouseLeave(AMapView, Result);
end;
inherited MouseLeave(AMapView, AMapEvent);
end;
function TMvPluginManager.MouseMove(AMapView: TMapView; AShift: TShiftState;
X, Y: Integer; AMapEvent: TMouseMoveEvent): Boolean;
X, Y: Integer): Boolean;
var
i: Integer;
plugin: TMvCustomPlugin;
@ -863,11 +849,10 @@ begin
if HandlePlugin(plugin, AMapView) then
plugin.MouseMove(AMapView, AShift, X, Y, Result);
end;
inherited MouseMove(AMapView, AShift, X, Y, AMapEvent);
end;
function TMvPluginManager.MouseUp(AMapView: TMapView; AButton: TMouseButton;
AShift: TShiftState; X, Y: Integer; AMapEvent: TMouseEvent): Boolean;
AShift: TShiftState; X, Y: Integer): Boolean;
var
i: Integer;
plugin: TMvCustomPlugin;
@ -879,7 +864,6 @@ begin
if HandlePlugin(plugin, AMapView) then
plugin.MouseUp(AMapView, AButton, AShift, X, Y, Result);
end;
inherited MouseUp(AMapView, AButton, AShift, X, Y, AMapEvent);
end;
function TMvPluginManager.MouseWheel(AMapView: TMapView; AShift: TShiftState;
@ -895,7 +879,6 @@ begin
if HandlePlugin(plugin, AMapView) then
plugin.MouseWheel(AMapView, AShift, AWheelDelta, AMousePos, Result);
end;
// No user event here; it is handled by the Mapview itself
end;
procedure TMvPluginManager.Notification(AComponent: TComponent; Operation: TOperation);
@ -934,7 +917,7 @@ begin
PluginList.ChangeNamePrefix(oldName, AValue);
end;
function TMvPluginManager.ZoomChange(AMapView: TMapView; AMapEvent: TNotifyEvent): Boolean;
function TMvPluginManager.ZoomChange(AMapView: TMapView): Boolean;
var
i: Integer;
plugin: TMvCustomPlugin;
@ -946,7 +929,6 @@ begin
if HandlePlugin(plugin, AMapView) then
plugin.ZoomChange(AMapView, Result);
end;
inherited ZoomChange(AMapView, AMapEvent);
end;
(*