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

192 lines
4.9 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
unit UnSwClient;
{$I BaseSwDefines.inc}
2006-01-06 08:11:17 +00:00
2006-01-05 06:03:24 +00:00
interface
uses
ActnList,
Classes,
Dialogs,
SysUtils,
ToolsAPI,
BaseSwClient,
2006-01-05 06:03:24 +00:00
UnSwDialog,
UnSwObjects;
2006-01-05 06:03:24 +00:00
type
TUnitSwitcherHook = class(TBaseSwitcherHook)
2006-01-05 06:03:24 +00:00
private
FViewFormAction: TContainedAction;
FViewUnitAction: 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();
end;
implementation
2006-01-05 06:03:24 +00:00
{ TUnitSwitcherHook}
constructor TUnitSwitcherHook.Create();
begin
inherited;
2006-01-05 06:03:24 +00:00
try
{
2006-01-05 06:03:24 +00:00
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
FViewFormAction := HookIDEAction('ViewFormCommand', NewExecute);
FViewUnitAction := HookIDEAction('ViewUnitCommand', NewExecute);
2006-01-05 06:03:24 +00:00
except
on E:EAssertionFailed do
ShowMessage('Error while loading UnitSwitcher: ' + E.Message);
end;
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;
end.