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
|
2006-01-05 21:04:59 +00:00
|
|
|
FOldUnitExecute: TNotifyEvent;
|
|
|
|
FOldFormExecute: TNotifyEvent;
|
2006-01-05 06:03:24 +00:00
|
|
|
FViewUnitAction: TContainedAction;
|
2006-01-05 21:04:59 +00:00
|
|
|
FViewFormAction: TContainedAction;
|
2006-01-05 06:03:24 +00:00
|
|
|
protected
|
2006-01-05 21:04:59 +00:00
|
|
|
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
|
2006-01-05 21:04:59 +00:00
|
|
|
FOldUnitExecute := pAction.OnExecute;
|
2006-01-05 06:03:24 +00:00
|
|
|
pAction.OnExecute := NewExecute;
|
|
|
|
FViewUnitAction := pAction;
|
2006-01-05 21:04:59 +00:00
|
|
|
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.');
|
2006-01-05 21:04:59 +00:00
|
|
|
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
|
2006-01-05 21:04:59 +00:00
|
|
|
if Assigned(FViewFormAction) then
|
|
|
|
FViewFormAction.OnExecute := FOldFormExecute;
|
|
|
|
|
2006-01-05 06:03:24 +00:00
|
|
|
if Assigned(FViewUnitAction) then
|
2006-01-05 21:04:59 +00:00
|
|
|
FViewUnitAction.OnExecute := FOldUnitExecute;
|
2006-01-05 06:03:24 +00:00
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2006-01-05 21:04:59 +00:00
|
|
|
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
|
2006-01-05 21:04:59 +00:00
|
|
|
iActive: Integer;
|
|
|
|
ifProject: IOTAProject;
|
2006-01-05 06:03:24 +00:00
|
|
|
iModule: Integer;
|
2006-01-05 21:04:59 +00:00
|
|
|
pActive: TUnSwUnit;
|
2006-01-05 06:03:24 +00:00
|
|
|
pUnits: TUnSwUnitList;
|
|
|
|
|
|
|
|
begin
|
2006-01-06 08:11:17 +00:00
|
|
|
ifProject := ActiveProject();
|
2006-01-05 21:04:59 +00:00
|
|
|
if not Assigned(ifProject) then
|
2006-01-05 06:03:24 +00:00
|
|
|
exit;
|
|
|
|
|
|
|
|
pUnits := TUnSwUnitList.Create();
|
|
|
|
try
|
2006-01-05 21:04:59 +00:00
|
|
|
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
|
|
|
|
2006-01-05 21:04:59 +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.
|