2
***************************************************************************
4
* This source is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This code is distributed in the hope that it will be useful, but *
10
* WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12
* General Public License for more details. *
14
* A copy of the GNU General Public License is available on the World *
15
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
16
* obtain it by writing to the Free Software Foundation, *
17
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************
21
Author: Mattias Gaertner
24
An IDE dialog to add a "With" block to the sourceeditor selection.
33
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
34
ButtonPanel, StdCtrls, Grids,
36
CodeCache, CodeToolManager, FileProcs, FindDeclarationTool,
38
IDEDialogs, LazIDEIntf, SrcEditorIntf, CodyStrConsts, CodyUtils;
42
{ TAddWithBlockDialog }
44
TAddWithBlockDialog = class(TForm)
45
ButtonPanel1: TButtonPanel;
46
WithExprGroupBox: TGroupBox;
47
WithExprStringGrid: TStringGrid;
48
procedure ButtonPanel1OKButtonClick(Sender: TObject);
49
procedure FormCreate(Sender: TObject);
52
SrcEdit: TSourceEditorInterface;
56
Candidates: TStringList;
57
function Check(aSrcEdit: TSourceEditorInterface): boolean;
60
procedure ShowAddWithBlockDialog(Sender: TObject);
62
function CompareObjectThenText(List: TStringList; Index1, Index2: Integer): integer;
66
procedure ShowAddWithBlockDialog(Sender: TObject);
68
AddWithBlockDialog: TAddWithBlockDialog;
69
SrcEdit: TSourceEditorInterface;
73
// commit changes form source editor to codetools
74
if not LazarusIDE.BeginCodeTools then exit;
75
// check context at cursor
76
SrcEdit:=SourceEditorManagerIntf.ActiveEditor;
77
if (SrcEdit=nil) or (SrcEdit.Selection='') then begin
78
IDEMessageDialog(crsCWError,
79
crsPleaseSelectSomeCodeInTheSourceEditor, mtError, [mbCancel]);
83
AddWithBlockDialog:=TAddWithBlockDialog.Create(nil);
85
if not AddWithBlockDialog.Check(SrcEdit) then exit;
86
if AddWithBlockDialog.ShowModal<>mrOk then exit;
88
with AddWithBlockDialog do begin
89
i:=WithExprStringGrid.Row;
91
debugln(['ShowAddWithBlockDialog nothing selected']);
94
WithExpr:=WithExprStringGrid.Cells[0,i];
95
if not CodeToolBoss.AddWithBlock(Code,StartPos.X,StartPos.Y,
96
EndPos.X,EndPos.Y,WithExpr,nil)
98
LazarusIDE.DoJumpToCodeToolBossError;
104
AddWithBlockDialog.Free;
108
function CompareObjectThenText(List: TStringList; Index1, Index2: Integer
114
n1:=PtrUInt(List.Objects[Index1]);
115
n2:=PtrUInt(List.Objects[Index2]);
116
if n1>n2 then exit(-1);
117
if n2>n1 then exit(1);
118
Result:=CompareText(List[Index1],List[Index2]);
121
{ TAddWithBlockDialog }
123
procedure TAddWithBlockDialog.FormCreate(Sender: TObject);
125
Caption:=crsAddWithBlock;
126
WithExprGroupBox.Caption:=crsSelectExpression;
127
WithExprStringGrid.Columns[0].Title.Caption:=crsExpression;
128
WithExprStringGrid.Columns[1].Title.Caption:=crsCount;
130
ButtonPanel1.HelpButton.Caption:=crsHelp;
131
ButtonPanel1.OKButton.Caption:=crsBTNOK;
132
ButtonPanel1.CancelButton.Caption:=crsBTNCancel;
135
procedure TAddWithBlockDialog.ButtonPanel1OKButtonClick(Sender: TObject);
139
i:=WithExprStringGrid.Row;
144
function TAddWithBlockDialog.Check(aSrcEdit: TSourceEditorInterface): boolean;
150
Code:=TCodeBuffer(SrcEdit.CodeToolsBuffer);
151
StartPos:=SrcEdit.BlockBegin;
152
EndPos:=SrcEdit.BlockEnd;
153
Candidates:=TStringList.Create;
155
//debugln(['TAddWithBlockDialog.Check ',dbgs(StartPos),' ',dbgs(EndPos)]);
156
if not CodeToolBoss.AddWithBlock(Code,StartPos.X,StartPos.Y,EndPos.X,EndPos.Y,
159
debugln(['ShowAddWithBlockDialog CodeToolBoss.AddWithBlock check failed']);
160
LazarusIDE.DoJumpToCodeToolBossError;
164
if Candidates.Count=0 then begin
165
IDEMessageDialog(crsCWError,
166
crsPleaseSelectSomeCodeInTheSourceEditorForANewWithBl,mtError,[mbCancel]);
170
Candidates.CustomSort(@CompareObjectThenText);
172
WithExprStringGrid.RowCount:=Candidates.Count+1;
173
for i:=0 to Candidates.Count-1 do begin
174
WithExprStringGrid.Cells[0,i+1]:=Candidates[i];
175
WithExprStringGrid.Cells[1,i+1]:=IntToStr(PtrUInt(Candidates.Objects[i]));
178
WithExprStringGrid.Row:=1;
179
WithExprStringGrid.Col:=0;