1
0
mirror of synced 2024-11-15 00:43:50 +00:00
unitswitcher/Source/UnSwClient.pas

206 lines
5.4 KiB
ObjectPascal
Raw Normal View History

2006-01-05 06:03:24 +00:00
{$ASSERTIONS ON}
unit UnSwClient;
2006-01-06 08:11:17 +00:00
// Since Delphi 7 is the lowest supported version at this point, assume
// all other versions support the IOTAModuleServices.GetActiveProject method...
{$UNDEF PROJWORKAROUND}
{$IFDEF VER150}
{$DEFINE PROJWORKAROUND}
{$ENDIF}
2006-01-05 06:03:24 +00:00
interface
implementation
uses
ActnList,
Classes,
Dialogs,
SysUtils,
ToolsAPI,
UnSwDialog,
UnSwObjects;
type
TUnitSwitcherHook = class(TObject)
private
FOldUnitExecute: TNotifyEvent;
FOldFormExecute: TNotifyEvent;
2006-01-05 06:03:24 +00:00
FViewUnitAction: TContainedAction;
FViewFormAction: TContainedAction;
2006-01-05 06:03:24 +00:00
protected
function ActiveFileName(): String;
2006-01-06 08:11:17 +00:00
function ActiveGroup(): IOTAProjectGroup;
function ActiveProject(): IOTAProject;
2006-01-05 06:03:24 +00:00
procedure NewExecute(Sender: TObject); virtual;
public
constructor Create();
destructor Destroy(); override;
end;
{ TUnitSwitcherHook}
constructor TUnitSwitcherHook.Create();
var
iAction: Integer;
ifNTA: INTAServices;
pAction: TContainedAction;
begin
try
Assert(Assigned(BorlandIDEServices), 'BorlandIDEServices not available.');
Assert(Supports(BorlandIDEServices, INTAServices, ifNTA),
'BorlandIDEServices does not support the ' +
'INTAServices interface.');
Assert(Supports(BorlandIDEServices, IOTAModuleServices),
'BorlandIDEServices does not support the ' +
'IOTAModuleServices interface.');
2006-01-06 08:11:17 +00:00
Assert(Supports(BorlandIDEServices, IOTAActionServices),
'BorlandIDEServices does not support the ' +
'IOTAActionServices interface.');
2006-01-05 06:03:24 +00:00
for iAction := 0 to Pred(ifNTA.ActionList.ActionCount) do
begin
pAction := ifNTA.ActionList.Actions[iAction];
if pAction.Name = 'ViewUnitCommand' then
begin
FOldUnitExecute := pAction.OnExecute;
2006-01-05 06:03:24 +00:00
pAction.OnExecute := NewExecute;
FViewUnitAction := pAction;
end else if pAction.Name = 'ViewFormCommand' then
begin
FOldFormExecute := pAction.OnExecute;
pAction.OnExecute := NewExecute;
FViewFormAction := pAction;
2006-01-05 06:03:24 +00:00
end;
end;
Assert(Assigned(FViewUnitAction), 'ViewUnitCommand action is not' +
'available in the IDE.');
Assert(Assigned(FViewFormAction), 'ViewFormCommand action is not' +
'available in the IDE.');
2006-01-05 06:03:24 +00:00
except
on E:EAssertionFailed do
ShowMessage('Error while loading UnitSwitcher: ' + E.Message);
end;
end;
destructor TUnitSwitcherHook.Destroy();
begin
if Assigned(FViewFormAction) then
FViewFormAction.OnExecute := FOldFormExecute;
2006-01-05 06:03:24 +00:00
if Assigned(FViewUnitAction) then
FViewUnitAction.OnExecute := FOldUnitExecute;
2006-01-05 06:03:24 +00:00
inherited;
end;
function TUnitSwitcherHook.ActiveFileName(): String;
var
ifModule: IOTAModule;
begin
Result := '';
ifModule := (BorlandIDEServices as IOTAModuleServices).CurrentModule;
if Assigned(ifModule) then
begin
if Assigned(ifModule.CurrentEditor) then
Result := ifModule.FileName;
end;
end;
2006-01-06 08:11:17 +00:00
function TUnitSwitcherHook.ActiveGroup(): IOTAProjectGroup;
var
ifModule: IOTAModule;
ifModules: IOTAModuleServices;
iModule: Integer;
begin
Result := nil;
ifModules := (BorlandIDEServices as IOTAModuleServices);
for iModule := 0 to Pred(ifModules.ModuleCount) do
begin
ifModule := ifModules.Modules[iModule];
if Supports(ifModule, IOTAProjectGroup, Result) then
break;
end;
end;
function TUnitSwitcherHook.ActiveProject(): IOTAProject;
{$IFDEF PROJWORKAROUND}
var
ifGroup: IOTAProjectGroup;
ifModule: IOTAModule;
ifModules: IOTAModuleServices;
iModule: Integer;
{$ENDIF}
begin
{$IFDEF PROJWORKAROUND}
Result := nil;
ifGroup := ActiveGroup();
if not Assigned(ifGroup) then
begin
ifModules := (BorlandIDEServices as IOTAModuleServices);
for iModule := 0 to Pred(ifModules.ModuleCount) do
begin
ifModule := ifModules.Modules[iModule];
if Supports(ifModule, IOTAProject, Result) then
break;
end;
end else
Result := ifGroup.ActiveProject;
{$ELSE}
Result := (BorlandIDEServices as IOTAModuleServices).GetActiveProject
{$ENDIF}
end;
2006-01-05 06:03:24 +00:00
procedure TUnitSwitcherHook.NewExecute(Sender: TObject);
var
iActive: Integer;
ifProject: IOTAProject;
2006-01-05 06:03:24 +00:00
iModule: Integer;
pActive: TUnSwUnit;
2006-01-05 06:03:24 +00:00
pUnits: TUnSwUnitList;
begin
2006-01-06 08:11:17 +00:00
ifProject := ActiveProject();
if not Assigned(ifProject) then
2006-01-05 06:03:24 +00:00
exit;
pUnits := TUnSwUnitList.Create();
try
pUnits.Add(TUnSwProjectUnit.Create(ifProject));
for iModule := 0 to Pred(ifProject.GetModuleCount) do
pUnits.Add(TUnSwModuleUnit.Create(ifProject.GetModule(iModule)));
pActive := nil;
iActive := pUnits.IndexOfFileName(ActiveFileName());
if iActive > -1 then
pActive := pUnits[iActive];
2006-01-05 06:03:24 +00:00
pActive := TfrmUnSwDialog.Execute(pUnits, (Sender = FViewFormAction),
pActive);
if Assigned(pActive) then
pActive.Activate((Sender = FViewUnitAction));
2006-01-05 06:03:24 +00:00
finally
FreeAndNil(pUnits);
end;
end;
var
GUnitSwitcher: TUnitSwitcherHook;
initialization
GUnitSwitcher := TUnitSwitcherHook.Create();
finalization
FreeAndNil(GUnitSwitcher);
end.