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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
{ Copyright (C) 2011,
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Original version by Juha Manninen
Icons added by Marcelo B Paula
All available units added to the list by Anton Panferov
}
unit UseUnitDlg;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, StdCtrls, ExtCtrls, Buttons, ButtonPanel,
Dialogs, LCLProc, FileProcs, Graphics, LCLType, SourceEditor, LazIDEIntf,
IDEImagesIntf, LazarusIDEStrConsts, ProjectIntf, IDEWindowIntf, Project,
CodeCache, CodeToolManager, IdentCompletionTool, CodeTree, ListFilterEdit,
LinkScanner, EnvironmentOpts, MainIntf, LazFileUtils;
type
TUseUnitDialogType = (udUseUnit, udOpenUnit);
{ TUseUnitDialog }
TUseUnitDialog = class(TForm)
ButtonPanel1: TButtonPanel;
AllUnitsCheckBox: TCheckBox;
FilterEdit: TListFilterEdit;
UnitsListBox: TListBox;
SectionRadioGroup: TRadioGroup;
procedure AllUnitsCheckBoxChange(Sender: TObject);
procedure FilterEditAfterFilter(Sender: TObject);
procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure SectionRadioGroupClick(Sender: TObject);
procedure UnitsListBoxDblClick(Sender: TObject);
procedure UnitsListBoxDrawItem({%H-}Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
procedure UnitsListBoxKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure UnitsListBoxMeasureItem({%H-}Control: TWinControl; {%H-}Index: Integer;
var AHeight: Integer);
private
UnitImgInd: Integer;
FMainUsedUnits: TStringList;
FImplUsedUnits: TStringList;
FProjUnits, FOtherUnits: TStringList;
DlgType: TUseUnitDialogType;
procedure AddImplUsedUnits;
function GetProjUnits(SrcEdit: TSourceEditor): Boolean;
procedure CreateOtherUnitsList;
function SelectedUnitFileName: string;
function SelectedUnit: string;
function InterfaceSelected: Boolean;
procedure DetermineUsesSection(ACode: TCodeBuffer);
procedure FillAvailableUnitsList;
public
end;
function ShowUseUnitDialog(const DefText: string; const aDlgType: TUseUnitDialogType): TModalResult;
implementation
{$R *.lfm}
function ShowUseUnitDialog(const DefText: string; const aDlgType: TUseUnitDialogType): TModalResult;
var
UseUnitDlg: TUseUnitDialog;
SrcEdit: TSourceEditor;
s: String;
CTRes: Boolean;
EnvOptions: TUseUnitDlgOptions;
begin
Result:=mrOk;
if not LazarusIDE.BeginCodeTools then begin
debugln(['ShowUseUnitDialog LazarusIDE.BeginCodeTools failed']);
exit;
end;
// get cursor position
SrcEdit:=SourceEditorManager.ActiveEditor;
if SrcEdit=nil then begin
debugln(['ShowUseUnitDialog no SrcEdit']);
exit;
end;
UseUnitDlg:=TUseUnitDialog.Create(nil);
try
UseUnitDlg.DlgType := aDlgType;
case aDlgType of
udUseUnit: UseUnitDlg.Caption := dlgUseUnitCaption;
udOpenUnit: UseUnitDlg.Caption := lisOpenUnit;
end;
if not UseUnitDlg.GetProjUnits(SrcEdit) then begin
debugln(['ShowUseUnitDialog UseUnitDlg.GetProjUnits(SrcEdit) failed: ',SrcEdit.FileName]);
Exit(mrCancel);
end;
UseUnitDlg.FillAvailableUnitsList;
// there is only main uses section in program/library/package
if SrcEdit.GetProjectFile=Project1.MainUnitInfo then
// only main (interface) section is available
UseUnitDlg.SectionRadioGroup.Enabled := False
else
// automatic choice of dest uses-section by cursor position
UseUnitDlg.DetermineUsesSection(SrcEdit.CodeBuffer);
// Read recent properties
EnvOptions := EnvironmentOptions.UseUnitDlgOptions;
UseUnitDlg.AllUnitsCheckBox.Checked := EnvOptions.AllUnits;
UseUnitDlg.SectionRadioGroup.ItemIndex := Ord(EnvOptions.AddToImplementation);
UseUnitDlg.SectionRadioGroup.Visible := aDlgType=udUseUnit;
if (UseUnitDlg.FilterEdit.Items.Count = 0)
and UseUnitDlg.AllUnitsCheckBox.Checked then begin
// No available units. This may not be a pascal source file.
ShowMessage(dlgNoAvailableUnits);
Exit(mrCancel);
end;
UseUnitDlg.FilterEdit.Text := DefText;
// Show the dialog.
if UseUnitDlg.ShowModal=mrOk then begin
// Write recent properties
EnvOptions.AllUnits := UseUnitDlg.AllUnitsCheckBox.Checked;
if aDlgType=udUseUnit then
EnvOptions.AddToImplementation := Boolean(UseUnitDlg.SectionRadioGroup.ItemIndex);
EnvironmentOptions.UseUnitDlgOptions := EnvOptions;
case aDlgType of
udUseUnit:
begin
s:=UseUnitDlg.SelectedUnit;
if s <> '' then begin
if UseUnitDlg.InterfaceSelected then
CTRes := CodeToolBoss.AddUnitToMainUsesSection(SrcEdit.CodeBuffer, s, '')
else
CTRes:=CodeToolBoss.AddUnitToImplementationUsesSection(SrcEdit.CodeBuffer, s, '');
if not CTRes then begin
LazarusIDE.DoJumpToCodeToolBossError;
exit(mrCancel);
end;
end;
end;
udOpenUnit:
begin
s:=UseUnitDlg.SelectedUnitFileName;
if FileExistsUTF8(s) then
Result := MainIDEInterface.DoOpenEditorFile(s,-1,-1,[ofAddToRecent])
else
exit(mrCancel);
end;
end;
end;
finally
UseUnitDlg.Free;
CodeToolBoss.SourceCache.ClearAllSourceLogEntries;
end;
end;
{ TUseUnitDialog }
procedure TUseUnitDialog.FormCreate(Sender: TObject);
begin
// Internationalization
IDEDialogLayoutList.ApplyLayout(Self, 500, 460);
AllUnitsCheckBox.Caption := dlgShowAllUnits;
SectionRadioGroup.Caption := dlgInsertSection;
SectionRadioGroup.Items.Clear;
SectionRadioGroup.Items.Add(dlgInsertInterface);
SectionRadioGroup.Items.Add(dlgInsertImplementation);
ButtonPanel1.OKButton.Caption:=lisMenuOk;
ButtonPanel1.CancelButton.Caption:=lisCancel;
UnitImgInd := IDEImages.LoadImage('item_unit');
FProjUnits:=TStringList.Create;
end;
procedure TUseUnitDialog.FormDestroy(Sender: TObject);
begin
FOtherUnits.Free;
FProjUnits.Free;
FImplUsedUnits.Free;
FMainUsedUnits.Free;
end;
procedure TUseUnitDialog.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
IDEDialogLayoutList.SaveLayout(Self);
end;
procedure TUseUnitDialog.SectionRadioGroupClick(Sender: TObject);
var
i: Integer;
begin
if not Assigned(FImplUsedUnits) then Exit;
if InterfaceSelected then
AddImplUsedUnits
else
for i := FilterEdit.Items.Count - 1 downto 0 do
if FilterEdit.Items.Objects[i] is TCodeTreeNode then
FilterEdit.Items.Delete(i);
FilterEdit.InvalidateFilter;
if Visible then
FilterEdit.SetFocus;
end;
procedure TUseUnitDialog.AllUnitsCheckBoxChange(Sender: TObject);
var
i: Integer;
begin
if not (Assigned(FMainUsedUnits) and Assigned(FImplUsedUnits)) then Exit;
if AllUnitsCheckBox.Checked then begin // Add other units
if not Assigned(FOtherUnits) then
CreateOtherUnitsList;
FilterEdit.Items.AddStrings(FOtherUnits);
end
else
for i := FilterEdit.Items.Count-1 downto 0 do
if FilterEdit.Items.Objects[i] is TIdentifierListItem then
FilterEdit.Items.Delete(i);
if Visible then
FilterEdit.SetFocus;
FilterEdit.InvalidateFilter;
end;
procedure TUseUnitDialog.UnitsListBoxDblClick(Sender: TObject);
begin
if UnitsListBox.ItemIndex >= 0 then
ModalResult := mrOK;
end;
procedure TUseUnitDialog.UnitsListBoxDrawItem(Control: TWinControl;
Index: Integer; ARect: TRect; State: TOwnerDrawState);
var
ena: Boolean;
begin
if Index < 0 then Exit;
with UnitsListBox do
begin
Canvas.FillRect(ARect);
ena := not Assigned(Items.Objects[Index]) or (Items.Objects[Index] is TCodeTreeNode);
if not (ena or (odSelected in State)) then
Canvas.Font.Color := clGreen;
IDEImages.Images_16.Draw(Canvas, 1, (ARect.Top+ARect.Bottom-IDEImages.Images_16.Height) div 2, UnitImgInd, ena);
if Items.Objects[Index] is TCodeTreeNode then
begin
// unit for moving: implementation->interface
Canvas.Pen.Color := clBlue;
Canvas.Pen.Width := 2;
Canvas.MoveTo(ARect.Left + 13, ARect.Top + 16);
Canvas.LineTo(ARect.Left + 13, ARect.Top + 8);
Canvas.LineTo(ARect.Left + 10, ARect.Top + 11);
Canvas.MoveTo(ARect.Left + 13, ARect.Top + 8);
Canvas.LineTo(ARect.Left + 15, ARect.Top + 11);
end;
Canvas.TextRect(ARect, ARect.Left + IDEImages.Images_16.Width + 4, ARect.Top, Items[Index]);
end;
end;
procedure TUseUnitDialog.UnitsListBoxKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
// A hack to prevent 'O' working as shortcut for OK-button.
// Should be removed when issue #20599 is resolved.
if (Key = VK_O) and (Shift = []) then
Key:=VK_UNKNOWN;
end;
procedure TUseUnitDialog.UnitsListBoxMeasureItem(Control: TWinControl;
Index: Integer; var AHeight: Integer);
begin
if (AHeight <= IDEImages.Images_16.Height) then
AHeight := IDEImages.Images_16.Height + 2;
end;
procedure TUseUnitDialog.AddImplUsedUnits;
var
i, j: Integer;
newUnit: string;
ImplNode: TObject;
begin
if FImplUsedUnits.Count = 0 then Exit;
i := 0; j := 0;
ImplNode := FImplUsedUnits.Objects[0];
newUnit := FImplUsedUnits[j];
with FilterEdit.Items do
begin
BeginUpdate;
try
while i <= Count - 1 do
begin
if Assigned(Objects[i]) then Break;
if CompareStr(FImplUsedUnits[j], Strings[i]) <= 0 then
begin
InsertObject(i, newUnit, ImplNode);
Inc(j);
if j >= FImplUsedUnits.Count then Exit;
newUnit := FImplUsedUnits[j];
end;
Inc(i);
end;
if j < FImplUsedUnits.Count then
for j := j to FImplUsedUnits.Count - 1 do
if i < Count then
InsertObject(i, FImplUsedUnits[j], ImplNode)
else
AddObject(FImplUsedUnits[j], ImplNode);
finally
EndUpdate;
end;
end;
end;
function TUseUnitDialog.GetProjUnits(SrcEdit: TSourceEditor): Boolean;
var
ProjFile: TUnitInfo;
CurrentUnitName, s: String;
x: Integer;
begin
Result := False;
FreeAndNil(FMainUsedUnits);
FreeAndNil(FImplUsedUnits);
if SrcEdit = nil then Exit;
Assert(Assigned(SrcEdit.CodeBuffer));
if DlgType=udUseUnit then
begin
if not CodeToolBoss.FindUsedUnitNames(SrcEdit.CodeBuffer, TStrings(FMainUsedUnits),
TStrings(FImplUsedUnits))
then begin
DebugLn(['ShowUseProjUnitDialog CodeToolBoss.FindUsedUnitNames failed']);
LazarusIDE.DoJumpToCodeToolBossError;
Exit;
end;
end else
begin
// don't filter units in current uses sections - use empty lists
FMainUsedUnits := TStringList.Create;
FImplUsedUnits := TStringList.Create;
end;
Result := True;
if Assigned(FMainUsedUnits) then begin
FMainUsedUnits.Sorted := True;
FMainUsedUnits.CaseSensitive := False;
end;
if Assigned(FImplUsedUnits) then begin
FImplUsedUnits.Sorted := True;
FImplUsedUnits.CaseSensitive := False;
end;
if SrcEdit.GetProjectFile is TUnitInfo then
CurrentUnitName := TUnitInfo(SrcEdit.GetProjectFile).Unit_Name
else
CurrentUnitName := '';
// Add available unit names to list
ProjFile:=Project1.FirstPartOfProject;
while ProjFile <> nil do begin
s := ProjFile.Unit_Name;
if s = CurrentUnitName then // current unit
s := '';
if (ProjFile <> Project1.MainUnitInfo) and (s <> '') then
if not FMainUsedUnits.Find(s, x) then
FProjUnits.AddObject(s, ProjFile);
ProjFile := ProjFile.NextPartOfProject;
end;
FProjUnits.Sorted := True;
end;
procedure TUseUnitDialog.CreateOtherUnitsList;
var
i, x: Integer;
curUnit: string;
SrcEdit: TSourceEditor;
begin
if not (Assigned(FMainUsedUnits) and Assigned(FImplUsedUnits)) then Exit;
Screen.Cursor:=crHourGlass;
try
FOtherUnits := TStringList.Create;
FOtherUnits.Sorted := True;
SrcEdit := SourceEditorManager.ActiveEditor;
with CodeToolBoss do
if GatherUnitNames(SrcEdit.CodeBuffer) then
begin
IdentifierList.Prefix := '';
for i := 0 to IdentifierList.GetFilteredCount - 1 do
begin
curUnit := IdentifierList.FilteredItems[i].Identifier;
if not FMainUsedUnits.Find(curUnit, x)
and not FImplUsedUnits.Find(curUnit, x)
and not FProjUnits.Find(curUnit, x) then
FOtherUnits.AddObject(IdentifierList.FilteredItems[i].Identifier,
IdentifierList.FilteredItems[i]);
end;
end;
finally
Screen.Cursor:=crDefault;
end;
end;
function TUseUnitDialog.SelectedUnit: string;
var
IdentItem: TIdentifierListItem;
CodeBuf: TCodeBuffer;
s: String;
begin
with UnitsListBox do
if ItemIndex >= 0 then
begin
if Items.Objects[ItemIndex] is TIdentifierListItem then
begin
IdentItem := TIdentifierListItem(Items.Objects[ItemIndex]);
Result := IdentItem.Identifier;
with CodeToolBoss.SourceChangeCache.BeautifyCodeOptions do
if WordExceptions.CheckExceptions(Result) then Exit;
CodeBuf := CodeToolBoss.FindUnitSource(SourceEditorManager.ActiveEditor.CodeBuffer, Result, '');
if Assigned(CodeBuf) then
begin
s := CodeToolBoss.GetSourceName(CodeBuf, True);
if s <> '' then
Result := s;
end;
end else
Result := Items[ItemIndex];
end else
Result := '';
end;
function TUseUnitDialog.SelectedUnitFileName: string;
var
CodeBuf: TCodeBuffer;
AObj: TObject;
begin
Result := '';
if UnitsListBox.ItemIndex < 0 then
Exit;
AObj := UnitsListBox.Items.Objects[UnitsListBox.ItemIndex];
if AObj is TIdentifierListItem then
begin
CodeBuf := CodeToolBoss.FindUnitSource(SourceEditorManager.ActiveEditor.CodeBuffer, TIdentifierListItem(AObj).Identifier, '');
if Assigned(CodeBuf) then
Result := CodeBuf.Filename;
end else
if AObj is TUnitInfo then
begin
Result := TUnitInfo(AObj).Filename;
end;
end;
function TUseUnitDialog.InterfaceSelected: Boolean;
begin
Result:=(not SectionRadioGroup.Enabled) or (SectionRadioGroup.ItemIndex=0);
end;
procedure TUseUnitDialog.DetermineUsesSection(ACode: TCodeBuffer);
var
ImplUsesNode: TCodeTreeNode;
i: Integer;
Tool: TCodeTool;
begin
CodeToolBoss.Explore(ACode,Tool,false);
if Tool=nil then exit;
// collect implementation use unit nodes
ImplUsesNode := Tool.FindImplementationUsesNode;
if Assigned(ImplUsesNode) then
for i := 0 to FImplUsedUnits.Count - 1 do
FImplUsedUnits.Objects[i] := ImplUsesNode;
// update
SectionRadioGroup.OnClick(SectionRadioGroup);
end;
procedure TUseUnitDialog.FillAvailableUnitsList;
var
curUnit: String;
i, x: Integer;
begin
if not (Assigned(FMainUsedUnits) and Assigned(FImplUsedUnits)) then Exit;
if not Assigned(FProjUnits) then Exit;
FilterEdit.Items.Clear;
for i := 0 to FProjUnits.Count - 1 do
begin
curUnit := FProjUnits[i];
if not FMainUsedUnits.Find(curUnit, x)
and not FImplUsedUnits.Find(curUnit, x) then
FilterEdit.Items.AddObject(FProjUnits[i], FProjUnits.Objects[i]);
end;
FilterEdit.InvalidateFilter;
end;
procedure TUseUnitDialog.FilterEditAfterFilter(Sender: TObject);
begin
if (UnitsListBox.Count > 0) and (UnitsListBox.ItemIndex = -1) then
UnitsListBox.ItemIndex := 0;
end;
end.
|