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

232 lines
6.2 KiB
ObjectPascal
Raw Normal View History

{: Connects UnitSwitcher to the IDE.
Last changed: $Date$
Revision: $Rev$
Author: $Author$
}
2006-01-05 06:03:24 +00:00
{$ASSERTIONS ON}
unit UnSwClient;
{$I UnSwDefines.inc}
2006-01-06 08:11:17 +00:00
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-05-23 19:33:05 +00:00
{$IFDEF DELPHI7ORLOWER}
2006-01-06 08:11:17 +00:00
function ActiveGroup(): IOTAProjectGroup;
{$ENDIF}
2006-01-06 08:11:17 +00:00
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
actionIndex: Integer;
ntaServices: INTAServices;
action: TContainedAction;
2006-01-05 06:03:24 +00:00
begin
try
Assert(Assigned(BorlandIDEServices), 'BorlandIDEServices not available.');
Assert(Supports(BorlandIDEServices, INTAServices, ntaServices),
2006-01-05 06:03:24 +00:00
'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 actionIndex := 0 to Pred(ntaServices.ActionList.ActionCount) do
2006-01-05 06:03:24 +00:00
begin
action := ntaServices.ActionList.Actions[actionIndex];
if action.Name = 'ViewUnitCommand' then
2006-01-05 06:03:24 +00:00
begin
FOldUnitExecute := action.OnExecute;
action.OnExecute := NewExecute;
FViewUnitAction := action;
end else if action.Name = 'ViewFormCommand' then
begin
FOldFormExecute := action.OnExecute;
action.OnExecute := NewExecute;
FViewFormAction := action;
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
module: IOTAModule;
begin
Result := '';
module := (BorlandIDEServices as IOTAModuleServices).CurrentModule;
if Assigned(module) then
begin
if Assigned(module.CurrentEditor) then
Result := module.FileName;
end;
end;
2006-05-23 19:33:05 +00:00
{$IFDEF DELPHI7ORLOWER}
2006-01-06 08:11:17 +00:00
function TUnitSwitcherHook.ActiveGroup(): IOTAProjectGroup;
var
module: IOTAModule;
moduleServices: IOTAModuleServices;
moduleIndex: Integer;
2006-01-06 08:11:17 +00:00
begin
Result := nil;
moduleServices := (BorlandIDEServices as IOTAModuleServices);
for moduleIndex := 0 to Pred(moduleServices.ModuleCount) do
2006-01-06 08:11:17 +00:00
begin
module := moduleServices.Modules[moduleIndex];
if Supports(module, IOTAProjectGroup, Result) then
2006-01-06 08:11:17 +00:00
break;
end;
end;
{$ENDIF}
2006-01-06 08:11:17 +00:00
function TUnitSwitcherHook.ActiveProject(): IOTAProject;
2006-05-23 19:33:05 +00:00
{$IFDEF DELPHI7ORLOWER}
2006-01-06 08:11:17 +00:00
var
projectGroup: IOTAProjectGroup;
module: IOTAModule;
moduleServices: IOTAModuleServices;
moduleIndex: Integer;
2006-01-06 08:11:17 +00:00
{$ENDIF}
begin
2006-05-23 19:33:05 +00:00
{$IFDEF DELPHI7ORLOWER}
Result := nil;
projectGroup := ActiveGroup();
if not Assigned(projectGroup) then
2006-01-06 08:11:17 +00:00
begin
moduleServices := (BorlandIDEServices as IOTAModuleServices);
for moduleIndex := 0 to Pred(moduleServices.ModuleCount) do
2006-01-06 08:11:17 +00:00
begin
module := moduleServices.Modules[moduleIndex];
if Supports(module, IOTAProject, Result) then
2006-01-06 08:11:17 +00:00
break;
end;
end else
Result := projectGroup.ActiveProject;
2006-01-06 08:11:17 +00:00
{$ELSE}
Result := (BorlandIDEServices as IOTAModuleServices).GetActiveProject();
2006-01-06 08:11:17 +00:00
{$ENDIF}
end;
2006-01-05 06:03:24 +00:00
procedure TUnitSwitcherHook.NewExecute(Sender: TObject);
var
activeIndex: Integer;
activeUnit: TUnSwUnit;
itemIndex: Integer;
moduleIndex: Integer;
project: IOTAProject;
selectedUnits: TUnSwUnitList;
unitList: TUnSwUnitList;
2007-02-16 17:19:22 +00:00
openDFM: Boolean;
openType: TUnSwActivateType;
fileName: string;
2006-01-05 06:03:24 +00:00
begin
project := ActiveProject();
if not Assigned(project) then
2006-01-05 06:03:24 +00:00
exit;
unitList := TUnSwUnitList.Create();
2006-01-05 06:03:24 +00:00
try
unitList.Add(TUnSwProjectUnit.Create(project));
for moduleIndex := 0 to Pred(project.GetModuleCount) do
unitList.Add(TUnSwModuleUnit.Create(project.GetModule(moduleIndex)));
activeUnit := nil;
2007-02-16 17:19:22 +00:00
fileName := ActiveFileName();
if SameText(ExtractFileExt(fileName), '.dfm') then
fileName := ChangeFileExt(fileName, '.pas');
activeIndex := unitList.IndexOfFileName(fileName);
if activeIndex > -1 then
activeUnit := unitList[activeIndex];
2006-01-05 06:03:24 +00:00
selectedUnits := TfrmUnSwDialog.Execute(unitList, (Sender = FViewFormAction),
2007-02-16 17:19:22 +00:00
openDFM, activeUnit);
if Assigned(selectedUnits) then
try
2007-02-16 17:19:22 +00:00
openType := atSource;
if openDFM then
openType := atDFM
else if Sender = FViewFormAction then
openType := atForm;
for itemIndex := 0 to Pred(selectedUnits.Count) do
2007-02-16 17:19:22 +00:00
selectedUnits[itemIndex].Activate(openType);
finally
FreeAndNil(selectedUnits);
end;
2006-01-05 06:03:24 +00:00
finally
FreeAndNil(unitList);
2006-01-05 06:03:24 +00:00
end;
end;
var
GUnitSwitcher: TUnitSwitcherHook;
initialization
GUnitSwitcher := TUnitSwitcherHook.Create();
finalization
FreeAndNil(GUnitSwitcher);
end.