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