/usr/lib/lazarus/0.9.30.4/converter/replacenamesunit.pas is in lazarus-src-0.9.30.4 0.9.30.4-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | unit ReplaceNamesUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
Grids, Buttons, ExtCtrls, Menus, ButtonPanel, CodeToolsStructs, SynRegExpr,
LazarusIDEStrConsts, ConverterTypes;
type
{ TStringMapUpdater }
TStringMapUpdater = class
private
fStringMap: TStringToStringTree;
fMapNames: TStringList; // Names (keys) in fStringMap.
fSeenNames: TStringList;
public
constructor Create(AStringsMap: TStringToStringTree);
destructor Destroy; override;
function FindReplacement(AIdent: string; out AReplacement: string): boolean;
end;
{ TGridUpdater }
TGridUpdater = class(TStringMapUpdater)
private
fGrid: TStringGrid;
GridEndInd: Integer;
public
constructor Create(AStringsMap: TStringToStringTree; AGrid: TStringGrid);
destructor Destroy; override;
function AddUnique(AOldIdent: string): string;
end;
{ TReplaceForm }
TReplaceForm = class(TForm)
ButtonPanel: TButtonPanel;
InsertRow1: TMenuItem;
DeleteRow1: TMenuItem;
Grid: TStringGrid;
PopupMenu1: TPopupMenu;
procedure FormCreate(Sender: TObject);
procedure PopupMenu1Popup(Sender: TObject);
procedure InsertRow1Click(Sender: TObject);
procedure DeleteRow1Click(Sender: TObject);
procedure GridEditingDone(Sender: TObject);
procedure GridSetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: string);
procedure btnOKClick(Sender: TObject);
private
IsLasRow: Boolean;
public
end;
function FromMapToGrid(AMap: TStringToStringTree; AGrid: TStringGrid): boolean;
function FromGridToMap(AMap: TStringToStringTree; AGrid: TStringGrid;
AllowEmptyValues: boolean = true): boolean;
function EditMap(AMap: TStringToStringTree; AFormTitle: string): TModalResult;
function EditCoordOffsets(AOffs: TVisualOffsets; aTitle: string): TModalResult;
implementation
{$R *.lfm}
function FromMapToGrid(AMap: TStringToStringTree; AGrid: TStringGrid): boolean;
// Copy strings from Map to Grid.
var
OldIdent, NewIdent: string;
List: TStringList;
i: Integer;
begin
Result:=true;
List:=TStringList.Create;
try
AGrid.BeginUpdate;
AMap.GetNames(List);
for i:=1 to List.Count do begin // Skip the fixed row in grid.
OldIdent:=List[i-1];
NewIdent:=AMap[OldIdent];
if AGrid.RowCount<i+2 then
AGrid.RowCount:=i+2; // Leave one empty row to the end.
AGrid.Cells[0,i]:=OldIdent;
AGrid.Cells[1,i]:=NewIdent;
end;
AGrid.EndUpdate;
finally
List.Free;
end;
end;
function FromGridToMap(AMap: TStringToStringTree; AGrid: TStringGrid;
AllowEmptyValues: boolean = true): boolean;
var
OldIdent, NewIdent: string;
i: Integer;
begin
Result:=true;
AMap.Clear;
// Collect (maybe edited) properties from StringGrid to fStringMap.
for i:=1 to AGrid.RowCount-1 do begin // Skip the fixed row.
OldIdent:=AGrid.Cells[0,i];
NewIdent:=AGrid.Cells[1,i];
if OldIdent<>'' then begin
if AllowEmptyValues or (NewIdent<>'') then
AMap[OldIdent]:=NewIdent;
end;
end;
end;
function EditMap(AMap: TStringToStringTree; AFormTitle: string): TModalResult;
var
RNForm: TReplaceForm;
begin
RNForm:=TReplaceForm.Create(nil);
try
RNForm.Caption:=AFormTitle;
RNForm.Grid.Columns[0].Title.Caption:=lisConvDelphiName;
RNForm.Grid.Columns[1].Title.Caption:=lisConvNewName;
FromMapToGrid(AMap, RNForm.Grid);
Result:=RNForm.ShowModal;
if Result=mrOK then
FromGridToMap(AMap, RNForm.Grid);
finally
RNForm.Free;
end;
end;
// Functions for visual offsets values:
function FromListToGrid(AOffs: TVisualOffsets; AGrid: TStringGrid): boolean;
// Copy strings from coordinale list to grid.
var
i: Integer;
begin
Result:=true;
AGrid.BeginUpdate;
for i:=1 to AOffs.Count do begin // Skip the fixed row in grid.
if AGrid.RowCount<i+2 then
AGrid.RowCount:=i+2; // Leave one empty row to the end.
AGrid.Cells[0,i]:=AOffs[i-1].ParentType;
AGrid.Cells[1,i]:=IntToStr(AOffs[i-1].Top);
AGrid.Cells[2,i]:=IntToStr(AOffs[i-1].Left);
end;
AGrid.EndUpdate;
end;
function FromGridToList(AOffs: TVisualOffsets; AGrid: TStringGrid): boolean;
var
ParentType: string;
i, xTop, xLeft: Integer;
begin
Result:=true;
AOffs.Clear;
// Collect (maybe edited) properties from StringGrid to fStringMap.
for i:=1 to AGrid.RowCount-1 do begin // Skip the fixed row.
ParentType:=AGrid.Cells[0,i];
if ParentType<>'' then begin
xTop:=0;
try
xTop:=StrToInt(AGrid.Cells[1,i]);
except on EConvertError do
ShowMessage('Top value must be a number. Now: '+AGrid.Cells[1,i]);
end;
xLeft:=0;
try
xLeft:=StrToInt(AGrid.Cells[2,i]);
except on EConvertError do
ShowMessage('Left value must be a number. Now: '+AGrid.Cells[2,i]);
end;
AOffs.Add(TVisualOffset.Create(ParentType, xTop, xLeft));
end;
end;
end;
function EditCoordOffsets(AOffs: TVisualOffsets; aTitle: string): TModalResult;
var
xForm: TReplaceForm;
begin
xForm:=TReplaceForm.Create(nil);
try
xForm.Caption:=aTitle;
xForm.Grid.Columns[0].Title.Caption:=lisConvParentContainer;
xForm.Grid.Columns[1].Title.Caption:=lisConvTopOff;
xForm.Grid.Columns.Add.Title.Caption:=lisConvLeftOff;
FromListToGrid(AOffs, xForm.Grid);
Result:=xForm.ShowModal;
if Result=mrOK then
FromGridToList(AOffs, xForm.Grid);
finally
xForm.Free;
end;
end;
{ TStringMapUpdater }
constructor TStringMapUpdater.Create(AStringsMap: TStringToStringTree);
begin
fStringMap:=AStringsMap;
fMapNames:=TStringList.Create;
fStringMap.GetNames(fMapNames);
fSeenNames:=TStringList.Create;
end;
destructor TStringMapUpdater.Destroy;
begin
fSeenNames.Free;
fMapNames.Free;
inherited Destroy;
end;
function TStringMapUpdater.FindReplacement(AIdent: string;
out AReplacement: string): boolean;
// Try to find a matching replacement using regular expression.
var
RE: TRegExpr;
i: Integer;
Key: string;
begin
if fStringMap.Contains(AIdent) then begin
AReplacement:=fStringMap[AIdent];
Result:=true;
end
else begin // Not found by name, try regexp.
Result:=false;
AReplacement:='';
RE:=TRegExpr.Create;
try
for i:=0 to fMapNames.Count-1 do begin
Key:=fMapNames[i]; // fMapNames has names extracted from fStringMap.
// If key contains special chars, assume it is a regexp.
if (Pos('(',Key)>0) or (Pos('*',Key)>0) or (Pos('+',Key)>0) then begin
RE.Expression:=Key;
if RE.Exec(AIdent) then begin // Match with regexp.
AReplacement:=RE.Substitute(fStringMap[Key]);
Result:=true;
Break;
end;
end;
end;
finally
RE.Free;
end;
end;
end;
{ TGridUpdater }
constructor TGridUpdater.Create(AStringsMap: TStringToStringTree; AGrid: TStringGrid);
begin
inherited Create(AStringsMap);
fGrid:=AGrid;
GridEndInd:=1;
end;
destructor TGridUpdater.Destroy;
begin
inherited Destroy;
end;
function TGridUpdater.AddUnique(AOldIdent: string): string;
// Add a new Delphi -> Lazarus mapping to the grid.
// Returns the replacement string.
begin
if fSeenNames.IndexOf(AOldIdent)<0 then begin
// Add only one instance of each name.
fSeenNames.Append(AOldIdent);
FindReplacement(AOldIdent, Result);
if fGrid.RowCount<GridEndInd+1 then
fGrid.RowCount:=GridEndInd+1;
fGrid.Cells[0,GridEndInd]:=AOldIdent;
fGrid.Cells[1,GridEndInd]:=Result;
Inc(GridEndInd);
end;
end;
{ TReplaceForm }
procedure TReplaceForm.FormCreate(Sender: TObject);
begin
Caption:=lisReplacements;
ButtonPanel.OKButton.Caption := lisOk;
ButtonPanel.HelpButton.Caption := lisMenuHelp;
ButtonPanel.CancelButton.Caption := dlgCancel;
IsLasRow:=false;
end;
procedure TReplaceForm.PopupMenu1Popup(Sender: TObject);
var
ControlCoord, NewCell: TPoint;
begin
ControlCoord := Grid.ScreenToControl(PopupMenu1.PopupPoint);
NewCell:=Grid.MouseToCell(ControlCoord);
Grid.Col:=NewCell.X;
Grid.Row:=NewCell.Y;
end;
procedure TReplaceForm.InsertRow1Click(Sender: TObject);
begin
Grid.InsertColRow(False, Grid.Row);
end;
procedure TReplaceForm.DeleteRow1Click(Sender: TObject);
begin
Grid.DeleteColRow(False, Grid.Row);
end;
// Add rows automatically to the end of the grid
// using OnSetEditText and OnEditingDone handlers and IsLasRow flag.
procedure TReplaceForm.GridEditingDone(Sender: TObject);
var
sg: TStringGrid;
begin
if IsLasRow then begin
sg:=Sender as TStringGrid;
sg.RowCount:=sg.RowCount+1;
IsLasRow:=false;
end;
end;
procedure TReplaceForm.GridSetEditText(Sender: TObject; ACol,
ARow: Integer; const Value: string);
begin
if ARow = (Sender as TStringGrid).RowCount-1 then
IsLasRow:=Value<>'';
end;
procedure TReplaceForm.btnOKClick(Sender: TObject);
begin
ModalResult:=mrOK;
end;
end.
|