IDE: improve "use unit dialog", patch from Anton

git-svn-id: trunk@32148 -
This commit is contained in:
juha 2011-09-01 20:41:14 +00:00
parent fd5e6e3062
commit d49856facd

View File

@ -33,7 +33,8 @@ uses
Classes, SysUtils, Forms, Controls, ComCtrls, StdCtrls, ExtCtrls, Buttons, Classes, SysUtils, Forms, Controls, ComCtrls, StdCtrls, ExtCtrls, Buttons,
ButtonPanel, Dialogs, LCLProc, FileProcs, Graphics, LCLType, EditBtn, StrUtils, ButtonPanel, Dialogs, LCLProc, FileProcs, Graphics, LCLType, EditBtn, StrUtils,
SourceEditor, LazIDEIntf, IDEImagesIntf, LazarusIDEStrConsts, ProjectIntf, SourceEditor, LazIDEIntf, IDEImagesIntf, LazarusIDEStrConsts, ProjectIntf,
Project, CodeCache, CodeToolManager, IdentCompletionTool, ListFilterEdit; Project, CodeCache, CodeToolManager, IdentCompletionTool, CodeAtom, CodeTree,
PascalParserTool, ListFilterEdit, LinkScanner;
type type
@ -48,6 +49,7 @@ type
procedure AllUnitsCheckBoxChange(Sender: TObject); procedure AllUnitsCheckBoxChange(Sender: TObject);
procedure FormCreate(Sender: TObject); procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject); procedure FormDestroy(Sender: TObject);
procedure SectionRadioGroupClick(Sender: TObject);
procedure UnitsListBoxDblClick(Sender: TObject); procedure UnitsListBoxDblClick(Sender: TObject);
procedure UnitsListBoxDrawItem(Control: TWinControl; Index: Integer; procedure UnitsListBoxDrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState); ARect: TRect; State: TOwnerDrawState);
@ -61,7 +63,7 @@ type
procedure CreateOtherUnitsList; procedure CreateOtherUnitsList;
function SelectedUnit: string; function SelectedUnit: string;
function InterfaceSelected: Boolean; function InterfaceSelected: Boolean;
procedure EnableOnlyInterface; procedure DetermineUsesSection(ACode: TCodeBuffer; ACursorPos: TPoint);
public public
end; end;
@ -74,7 +76,7 @@ implementation
function ShowUseUnitDialog: TModalResult; function ShowUseUnitDialog: TModalResult;
var var
UseProjUnitDlg: TUseUnitDialog; UseUnitDlg: TUseUnitDialog;
SrcEdit: TSourceEditor; SrcEdit: TSourceEditor;
s: String; s: String;
CTRes: Boolean; CTRes: Boolean;
@ -83,18 +85,23 @@ begin
if not LazarusIDE.BeginCodeTools then exit; if not LazarusIDE.BeginCodeTools then exit;
// get cursor position // get cursor position
SrcEdit:=SourceEditorManager.ActiveEditor; SrcEdit:=SourceEditorManager.ActiveEditor;
UseProjUnitDlg:=TUseUnitDialog.Create(nil); UseUnitDlg:=TUseUnitDialog.Create(nil);
try try
Result:=UseProjUnitDlg.GetAvailableProjUnits(SrcEdit); Result:=UseUnitDlg.GetAvailableProjUnits(SrcEdit);
if Result<>mrOK then exit; if Result<>mrOK then exit;
// there is only main uses section in program/library/package // there is only main uses section in program/library/package
if SrcEdit.GetProjectFile=Project1.MainUnitInfo then if SrcEdit.GetProjectFile=Project1.MainUnitInfo then begin
UseProjUnitDlg.EnableOnlyInterface; // only main (interface) section is available
UseUnitDlg.SectionRadioGroup.Enabled := False
end else begin
// automatic choise of dest uses-section by cursor position
UseUnitDlg.DetermineUsesSection(SrcEdit.CodeBuffer, SrcEdit.GetCursorTextXY);
end;
// Show the dialog. // Show the dialog.
if UseProjUnitDlg.ShowModal=mrOk then begin if UseUnitDlg.ShowModal=mrOk then begin
s:=UseProjUnitDlg.SelectedUnit; s:=UseUnitDlg.SelectedUnit;
if s <> '' then begin if s <> '' then begin
if UseProjUnitDlg.InterfaceSelected then if UseUnitDlg.InterfaceSelected then
CTRes:=CodeToolBoss.AddUnitToMainUsesSection(SrcEdit.CodeBuffer, s, '') CTRes:=CodeToolBoss.AddUnitToMainUsesSection(SrcEdit.CodeBuffer, s, '')
else else
CTRes:=CodeToolBoss.AddUnitToImplementationUsesSection(SrcEdit.CodeBuffer, s, ''); CTRes:=CodeToolBoss.AddUnitToImplementationUsesSection(SrcEdit.CodeBuffer, s, '');
@ -105,7 +112,7 @@ begin
end; end;
end; end;
finally finally
UseProjUnitDlg.Free; UseUnitDlg.Free;
CodeToolBoss.SourceCache.ClearAllSourceLogEntries; CodeToolBoss.SourceCache.ClearAllSourceLogEntries;
end; end;
end; end;
@ -121,7 +128,7 @@ begin
SectionRadioGroup.Items.Clear; SectionRadioGroup.Items.Clear;
SectionRadioGroup.Items.Add(dlgInsertInterface); SectionRadioGroup.Items.Add(dlgInsertInterface);
SectionRadioGroup.Items.Add(dlgInsertImplementation); SectionRadioGroup.Items.Add(dlgInsertImplementation);
SectionRadioGroup.ItemIndex:=1; SectionRadioGroup.ItemIndex:=0;
ButtonPanel1.OKButton.Caption:=lisOk; ButtonPanel1.OKButton.Caption:=lisOk;
ButtonPanel1.CancelButton.Caption:=dlgCancel; ButtonPanel1.CancelButton.Caption:=dlgCancel;
UnitImgInd := IDEImages.LoadImage(16, 'item_unit'); UnitImgInd := IDEImages.LoadImage(16, 'item_unit');
@ -136,6 +143,12 @@ begin
FMainUsedUnits.Free; FMainUsedUnits.Free;
end; end;
procedure TUseUnitDialog.SectionRadioGroupClick(Sender: TObject);
begin
if Visible then
FilterEdit.SetFocus;
end;
procedure TUseUnitDialog.AllUnitsCheckBoxChange(Sender: TObject); procedure TUseUnitDialog.AllUnitsCheckBoxChange(Sender: TObject);
var var
i: Integer; i: Integer;
@ -285,10 +298,30 @@ begin
Result:=SectionRadioGroup.ItemIndex=0; Result:=SectionRadioGroup.ItemIndex=0;
end; end;
procedure TUseUnitDialog.EnableOnlyInterface; procedure TUseUnitDialog.DetermineUsesSection(ACode: TCodeBuffer; ACursorPos: TPoint);
var
CursorPos: TCodeXYPosition;
CleanCursorPos: Integer;
CursorNode: TCodeTreeNode;
begin begin
SectionRadioGroup.ItemIndex := 0; if not CodeToolBoss.InitCurCodeTool(ACode) then Exit;
SectionRadioGroup.Enabled := False; with CodeToolBoss.CurCodeTool do
begin
CursorPos := CodeXYPosition(ACursorPos.X, ACursorPos.Y, ACode);
ActivateGlobalWriteLock;
try
// build code tree
BuildTreeAndGetCleanPos(trTillCursor,lsrEnd,CursorPos,CleanCursorPos,
[btSetIgnoreErrorPos,btLoadDirtySource,btCursorPosOutAllowed]);
// find CodeTreeNode at cursor
if (Tree.Root = nil) or (Tree.Root.StartPos > CleanCursorPos) then Exit;
CursorNode := BuildSubTreeAndFindDeepestNodeAtPos(CleanCursorPos, True);
if CursorNode.HasParentOfType(ctnImplementation) then
SectionRadioGroup.ItemIndex := 1;
finally
DeactivateGlobalWriteLock
end;
end;
end; end;
end. end.