2007-12-07 15:33:00 +00:00
|
|
|
{: Provides a base for hooking IDE actions.
|
|
|
|
|
|
|
|
Last changed: $Date$
|
|
|
|
Revision: $Rev$
|
|
|
|
Author: $Author$
|
|
|
|
}
|
|
|
|
|
|
|
|
{$ASSERTIONS ON}
|
|
|
|
|
|
|
|
unit BaseSwClient;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
2018-10-12 12:53:02 +00:00
|
|
|
{$IF CompilerVersion >= 32}
|
|
|
|
System.Actions,
|
|
|
|
{$IFEND}
|
2007-12-07 15:33:00 +00:00
|
|
|
ActnList,
|
|
|
|
Classes;
|
|
|
|
|
|
|
|
type
|
|
|
|
PHookedAction = ^THookedAction;
|
|
|
|
THookedAction = record
|
|
|
|
Action: TContainedAction;
|
|
|
|
OldOnExecute: TNotifyEvent;
|
|
|
|
NewOnExecute: TNotifyEvent;
|
2007-12-10 20:29:57 +00:00
|
|
|
OldOnUpdate: TNotifyEvent;
|
|
|
|
NewOnUpdate: TNotifyEvent;
|
2007-12-07 15:33:00 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2008-09-20 09:35:24 +00:00
|
|
|
TBaseSwitcherHook = class(TComponent)
|
2007-12-07 15:33:00 +00:00
|
|
|
private
|
|
|
|
FHookedActions: TList;
|
|
|
|
protected
|
2010-02-16 12:39:29 +00:00
|
|
|
function GetIDEActionList: TCustomActionList;
|
|
|
|
|
2007-12-07 15:33:00 +00:00
|
|
|
function GetHookedActionIndex(AAction: TContainedAction): Integer;
|
|
|
|
function GetHookedAction(AAction: TContainedAction): PHookedAction;
|
|
|
|
|
2007-12-10 20:29:57 +00:00
|
|
|
procedure HookAction(AAction: TContainedAction; AOnExecute: TNotifyEvent; AOnUpdate: TNotifyEvent = nil);
|
|
|
|
function HookIDEAction(const AName: String; AOnExecute: TNotifyEvent; AOnUpdate: TNotifyEvent = nil): TContainedAction;
|
2007-12-07 15:33:00 +00:00
|
|
|
procedure UnhookActionIndex(AIndex: Integer);
|
|
|
|
procedure UnhookAction(AAction: TContainedAction);
|
|
|
|
|
|
|
|
procedure OldActionExecute(AAction: TObject);
|
2007-12-10 20:29:57 +00:00
|
|
|
procedure OldActionUpdate(AAction: TObject);
|
2008-09-20 09:35:24 +00:00
|
|
|
|
|
|
|
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
|
2007-12-07 15:33:00 +00:00
|
|
|
public
|
2008-09-20 09:35:24 +00:00
|
|
|
constructor Create(); reintroduce;
|
2007-12-07 15:33:00 +00:00
|
|
|
destructor Destroy(); override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
|
|
|
SysUtils,
|
|
|
|
ToolsAPI;
|
|
|
|
|
|
|
|
|
|
|
|
{ TBaseSwitcherHook }
|
|
|
|
constructor TBaseSwitcherHook.Create();
|
|
|
|
begin
|
2008-09-20 09:35:24 +00:00
|
|
|
inherited Create(nil);
|
2007-12-07 15:33:00 +00:00
|
|
|
|
|
|
|
FHookedActions := TList.Create();
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
destructor TBaseSwitcherHook.Destroy();
|
|
|
|
var
|
|
|
|
actionIndex: Integer;
|
|
|
|
|
|
|
|
begin
|
|
|
|
for actionIndex := Pred(FHookedActions.Count) downto 0 do
|
|
|
|
UnhookActionIndex(actionIndex);
|
|
|
|
|
|
|
|
FreeAndNil(FHookedActions);
|
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2010-02-16 12:39:29 +00:00
|
|
|
function TBaseSwitcherHook.GetIDEActionList: TCustomActionList;
|
|
|
|
var
|
|
|
|
ntaServices: INTAServices;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Assert(Assigned(BorlandIDEServices), 'BorlandIDEServices not available.');
|
|
|
|
Assert(Supports(BorlandIDEServices, INTAServices, ntaServices),
|
|
|
|
'BorlandIDEServices does not support the ' +
|
|
|
|
'INTAServices interface.');
|
|
|
|
|
|
|
|
Result := ntaServices.ActionList;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-12-07 15:33:00 +00:00
|
|
|
function TBaseSwitcherHook.GetHookedActionIndex(AAction: TContainedAction): Integer;
|
|
|
|
var
|
|
|
|
actionIndex: Integer;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := -1;
|
|
|
|
|
|
|
|
for actionIndex := Pred(FHookedActions.Count) downto 0 do
|
|
|
|
if PHookedAction(FHookedActions[actionIndex]).Action = AAction then
|
|
|
|
begin
|
|
|
|
Result := actionIndex;
|
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TBaseSwitcherHook.GetHookedAction(AAction: TContainedAction): PHookedAction;
|
|
|
|
var
|
|
|
|
actionIndex: Integer;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
actionIndex := GetHookedActionIndex(AAction);
|
|
|
|
|
|
|
|
if actionIndex > -1 then
|
|
|
|
Result := FHookedActions[actionIndex];
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-12-10 20:29:57 +00:00
|
|
|
procedure TBaseSwitcherHook.HookAction(AAction: TContainedAction; AOnExecute, AOnUpdate: TNotifyEvent);
|
2007-12-07 15:33:00 +00:00
|
|
|
var
|
|
|
|
hookedAction: PHookedAction;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Assert(GetHookedActionIndex(AAction) = -1, 'Action is already hooked');
|
|
|
|
|
|
|
|
New(hookedAction);
|
|
|
|
hookedAction^.Action := AAction;
|
|
|
|
hookedAction^.OldOnExecute := AAction.OnExecute;
|
|
|
|
hookedAction^.NewOnExecute := AOnExecute;
|
|
|
|
AAction.OnExecute := AOnExecute;
|
2007-12-10 20:29:57 +00:00
|
|
|
|
|
|
|
hookedAction^.OldOnUpdate := AAction.OnUpdate;
|
|
|
|
hookedAction^.NewOnUpdate := AOnUpdate;
|
|
|
|
|
|
|
|
if Assigned(AOnUpdate) then
|
|
|
|
AAction.OnUpdate := AOnUpdate;
|
|
|
|
|
|
|
|
FHookedActions.Add(hookedAction);
|
2008-09-20 09:35:24 +00:00
|
|
|
AAction.FreeNotification(Self);
|
2007-12-07 15:33:00 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-12-10 20:29:57 +00:00
|
|
|
function TBaseSwitcherHook.HookIDEAction(const AName: String;
|
|
|
|
AOnExecute, AOnUpdate: TNotifyEvent): TContainedAction;
|
2007-12-07 15:33:00 +00:00
|
|
|
var
|
2010-02-16 12:39:29 +00:00
|
|
|
actionList: TCustomActionList;
|
2007-12-07 15:33:00 +00:00
|
|
|
actionIndex: Integer;
|
|
|
|
action: TContainedAction;
|
|
|
|
|
|
|
|
begin
|
2010-02-16 12:39:29 +00:00
|
|
|
Result := nil;
|
|
|
|
actionList := GetIDEActionList;
|
2007-12-07 15:33:00 +00:00
|
|
|
|
2010-02-16 12:39:29 +00:00
|
|
|
for actionIndex := 0 to Pred(actionList.ActionCount) do
|
2007-12-07 15:33:00 +00:00
|
|
|
begin
|
2010-02-16 12:39:29 +00:00
|
|
|
action := actionList.Actions[actionIndex];
|
2007-12-07 15:33:00 +00:00
|
|
|
if action.Name = AName then
|
|
|
|
begin
|
|
|
|
Result := action;
|
2007-12-10 20:29:57 +00:00
|
|
|
HookAction(action, AOnExecute, AOnUpdate);
|
2007-12-07 15:33:00 +00:00
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
Assert(Assigned(Result), AName + ' action is not available in the IDE.');
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2008-09-20 09:35:24 +00:00
|
|
|
procedure TBaseSwitcherHook.Notification(AComponent: TComponent; Operation: TOperation);
|
|
|
|
begin
|
|
|
|
if (Operation = opRemove) and (AComponent is TContainedAction) then
|
|
|
|
UnhookAction(TContainedAction(AComponent));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-12-07 15:33:00 +00:00
|
|
|
procedure TBaseSwitcherHook.UnhookActionIndex(AIndex: Integer);
|
|
|
|
var
|
|
|
|
hookedAction: PHookedAction;
|
|
|
|
action: TContainedAction;
|
|
|
|
|
|
|
|
begin
|
|
|
|
hookedAction := FHookedActions[AIndex];
|
|
|
|
action := TContainedAction(hookedAction^.Action);
|
2008-09-20 09:35:24 +00:00
|
|
|
action.RemoveFreeNotification(Self);
|
2007-12-07 15:33:00 +00:00
|
|
|
|
2008-09-20 09:35:24 +00:00
|
|
|
action.OnExecute := hookedAction^.OldOnExecute;
|
|
|
|
action.OnUpdate := hookedAction^.OldOnUpdate;
|
2007-12-07 15:33:00 +00:00
|
|
|
|
|
|
|
Dispose(hookedAction);
|
|
|
|
FHookedActions.Delete(AIndex);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TBaseSwitcherHook.UnhookAction(AAction: TContainedAction);
|
|
|
|
var
|
|
|
|
actionIndex: Integer;
|
|
|
|
|
|
|
|
begin
|
|
|
|
actionIndex := GetHookedActionIndex(AAction);
|
|
|
|
if actionIndex > -1 then
|
|
|
|
UnhookActionIndex(actionIndex);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TBaseSwitcherHook.OldActionExecute(AAction: TObject);
|
|
|
|
var
|
|
|
|
hookedAction: PHookedAction;
|
|
|
|
|
|
|
|
begin
|
|
|
|
if AAction is TContainedAction then
|
|
|
|
begin
|
|
|
|
hookedAction := GetHookedAction(TContainedAction(AAction));
|
|
|
|
|
2007-12-10 20:29:57 +00:00
|
|
|
if Assigned(hookedAction) and Assigned(hookedAction^.OldOnExecute) then
|
|
|
|
hookedAction^.OldOnExecute(AAction);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TBaseSwitcherHook.OldActionUpdate(AAction: TObject);
|
|
|
|
var
|
|
|
|
hookedAction: PHookedAction;
|
|
|
|
|
|
|
|
begin
|
|
|
|
if AAction is TContainedAction then
|
|
|
|
begin
|
|
|
|
hookedAction := GetHookedAction(TContainedAction(AAction));
|
|
|
|
|
|
|
|
if Assigned(hookedAction) and Assigned(hookedAction^.OldOnUpdate) then
|
|
|
|
hookedAction^.OldOnUpdate(AAction);
|
2007-12-07 15:33:00 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|