/usr/lib/lazarus/0.9.30.4/ide/searchresultview.pp 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 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 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 | {
/***************************************************************************
searchresultviewView.pp - SearchResult view
-------------------------------------------
TSearchResultsView is responsible for displaying the
Search Results of a find operation.
Initial Revision : Sat Nov 8th 2003
***************************************************************************/
***************************************************************************
* *
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
***************************************************************************
}
unit SearchResultView;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LCLProc, Forms, Controls, Graphics, Dialogs,
ComCtrls, ExtCtrls, StdCtrls, Buttons, LCLType, LCLIntf, Menus, strutils,
IDEWindowIntf, IDEOptionDefs, LazarusIDEStrConsts, EnvironmentOpts,
InputHistory, IDEProcs, Project, MainIntf, Clipbrd;
type
{ TLazSearchMatchPos }
TLazSearchMatchPos = class(TObject)
private
FFileEndPos: TPoint;
FFilename: string;
FFileStartPos: TPoint;
fMatchStart: integer;
fMatchLen: integer;
FNextInThisLine: TLazSearchMatchPos;
FShownFilename: string;
FTheText: string;
public
property MatchStart: integer read fMatchStart write fMatchStart;// start in TheText
property MatchLen: integer read fMatchLen write fMatchLen; // length in TheText
property Filename: string read FFilename write FFilename;
property FileStartPos: TPoint read FFileStartPos write FFileStartPos;
property FileEndPos: TPoint read FFileEndPos write FFileEndPos;
property TheText: string read FTheText write FTheText;
property ShownFilename: string read FShownFilename write FShownFilename;
property NextInThisLine: TLazSearchMatchPos read FNextInThisLine write FNextInThisLine;
destructor Destroy; override;
end;//TLazSearchMatchPos
{ TLazSearch }
TLazSearch = Class(TObject)
private
FReplaceText: string;
fSearchString: string;
fSearchOptions: TLazFindInFileSearchOptions;
fSearchDirectory: string;
fSearchMask: string;
public
property SearchString: string read fSearchString write fSearchString;
property ReplaceText: string read FReplaceText write FReplaceText;
property SearchOptions: TLazFindInFileSearchOptions read fSearchOptions
write fSearchOptions;
property SearchDirectory: string read fSearchDirectory
write fSearchDirectory;
property SearchMask: string read fSearchMask write fSearchMask;
end;//TLazSearch
{ TLazSearchResultTV }
TLazSearchResultTV = class(TCustomTreeView)
private
fSearchObject: TLazSearch;
FSkipped: integer;
fUpdateStrings: TStrings;
fBackUpStrings: TStrings;
fUpdating: boolean;
fUpdateCount: integer;
fShortenPathNeeded: boolean;
FSearchInListPhrases: string;
fFiltered: Boolean;
procedure SetSkipped(const AValue: integer);
procedure AddNode(Line: string; MatchPos: TLazSearchMatchPos);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property SearchObject: TLazSearch read fSearchObject write fSearchObject;
procedure BeginUpdate;
procedure EndUpdate;
procedure ShortenPaths;
procedure FreeObjectsTN(tnItems: TTreeNodes);
procedure FreeObjects(slItems: TStrings);
function BeautifyLine(const Filename: string; X, Y: integer;
const Line: string): string;
function BeautifyLine(SearchPos: TLazSearchMatchPos): string;
property BackUpStrings: TStrings read fBackUpStrings write fBackUpStrings;
property Filtered: Boolean read fFiltered write fFiltered;
property SearchInListPhrases: string read FSearchInListPhrases write FSearchInListPhrases;
property UpdateItems: TStrings read fUpdateStrings write fUpdateStrings;
property UpdateState: boolean read fUpdating;
property Skipped: integer read FSkipped write SetSkipped;
property Items;
function ItemsAsStrings: TStrings;
end;
{ TSearchResultsView }
TSearchResultsView = class(TForm)
mniCopySelected: TMenuItem;
mniCopyAll: TMenuItem;
mniCopyItem: TMenuItem;
popList: TPopupMenu;
SearchInListEdit: TEdit;
ImageList: TImageList;
ResultsNoteBook: TPageControl;
ToolBar: TToolBar;
SearchAgainButton: TToolButton;
ToolButton3: TToolButton;
FilterButton: TToolButton;
ClosePageButton: TToolButton;
ForwardSearchButton: TToolButton;
ResetResultsButton: TToolButton;
procedure ClosePageButtonClick(Sender: TObject);
procedure Form1Create(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure mniCopyAllClick(Sender: TObject);
procedure mniCopyItemClick(Sender: TObject);
procedure mniCopySelectedClick(Sender: TObject);
procedure ResultsNoteBookMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure TreeViewKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure ResultsNoteBookClosetabclicked(Sender: TObject);
procedure SearchAgainButtonClick(Sender: TObject);
procedure TreeViewAdvancedCustomDrawItem(Sender: TCustomTreeView;
Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage;
var PaintImages, DefaultDraw: Boolean);
procedure LazTVShowHint(Sender: TObject; HintInfo: PHintInfo);
procedure LazTVMousemove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
Procedure LazTVMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
procedure edSearchInListChange(Sender: TObject );
procedure ResultsNoteBookPageChanged (Sender: TObject );
procedure ForwardSearchButtonClick(Sender: TObject );
procedure ResetResultsButtonClick(Sender: TObject );
procedure SearchInListEditKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState );
procedure FilterButtonClick (Sender: TObject );
procedure TreeViewMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
FMaxItems: integer;
FWorkedSearchText: string;
FOnSelectionChanged: TNotifyEvent;
FMouseOverIndex: integer;
function BeautifyPageName(const APageName: string): string;
function GetPageIndex(const APageName: string): integer;
function GetTreeView(APageIndex: integer): TLazSearchResultTV;
procedure SetItems(Index: Integer; Value: TStrings);
function GetItems(Index: integer): TStrings;
procedure SetMaxItems(const AValue: integer);
procedure UpdateToolbar;
public
function AddSearch(const ResultsName: string;
const SearchText: string;
const ReplaceText: string;
const ADirectory: string;
const AMask: string;
const TheOptions: TLazFindInFileSearchOptions): TTabSheet;
function GetSourcePositon: TPoint;
function GetSourceFileName: string;
function GetSelectedText: string;
function GetSelectedMatchPos: TLazSearchMatchPos;
procedure AddMatch(const APageIndex: integer;
const Filename: string; const StartPos, EndPos: TPoint;
const TheText: string;
const MatchStart: integer; const MatchLen: integer);
procedure BeginUpdate(APageIndex: integer);
procedure EndUpdate(APageIndex: integer);
procedure Parse_Search_Phrases(var slPhrases: TStrings);
procedure ClosePage(PageIndex: integer);
property MaxItems: integer read FMaxItems write SetMaxItems;
property WorkedSearchText: string read FWorkedSearchText;
property OnSelectionChanged: TNotifyEvent read fOnSelectionChanged
write fOnSelectionChanged;
property Items[Index: integer]: TStrings read GetItems write SetItems;
end;
function SearchResultsView: TSearchResultsView;
procedure ShowSearchResultView(BringToFront: boolean);
var
OnSearchResultsViewSelectionChanged: TNotifyEvent = nil;
OnSearchAgainClicked: TNotifyEvent = nil;
implementation
{$R *.lfm}
{ TSearchResultsView }
const
MaxTextLen = 80;
var
SearchResultsViewSingleton: TSearchResultsView = nil;
function CopySearchMatchPos(var Src, Dest: TLazSearchMatchPos): Boolean;
begin
Result := False;
if ((Src = nil) or (Dest = nil)) then Exit;
Dest.MatchStart := Src.MatchStart;
Dest.MatchLen := Src.MatchLen;
Dest.Filename := Src.Filename;
Dest.FileStartPos := Src.FileStartPos;
Dest.FileEndPos := Src.FileEndPos;
Dest.TheText := Src.TheText;
Dest.ShownFilename := Src.ShownFilename;
Result := True;
end;
function GetTreeSelectedItemsAsText(ATreeView: TCustomTreeView): string;
var
sl: TStringList;
node: TTreeNode;
begin
sl:=TStringList.Create;
node := ATreeView.GetFirstMultiSelected;
while assigned(node) do
begin
sl.Add(node.Text);
node := node.GetNextMultiSelected;
end;
Result:=sl.Text;
sl.Free;
end;
function SearchResultsView: TSearchResultsView;
begin
Result := SearchResultsViewSingleton;
if SearchResultsViewSingleton <> nil then exit;
Application.CreateForm(TSearchResultsView, SearchResultsViewSingleton);
SearchResultsViewSingleton.OnSelectionChanged := OnSearchResultsViewSelectionChanged;
Result := SearchResultsViewSingleton;
end;
procedure ShowSearchResultView(BringToFront: boolean);
begin
IDEWindowCreators.ShowForm(SearchResultsView,BringToFront);
end;
procedure TSearchResultsView.Form1Create(Sender: TObject);
begin
FMaxItems:=50000;
ResultsNoteBook.Options:= ResultsNoteBook.Options+[nboShowCloseButtons];
ResultsNoteBook.Update;
Caption:=lisMenuViewSearchResults;
SearchAgainButton.Hint:=rsStartANewSearch;
ClosePageButton.Hint := rsCloseCurrentPage;
FilterButton.Hint:=rsFilterTheListWithTheCurrentFilterExpression;
ForwardSearchButton.Hint:=rsGoToTheNextItemInTheSearchList;
ResetResultsButton.Hint:=rsResetFilter;
SearchInListEdit.Hint:=rsEnterOneOrMorePhrasesThatYouWantToSearchOrFilterIn;
Name := NonModalIDEWindowNames[nmiwSearchResultsViewName];
fOnSelectionChanged:= nil;
ShowHint:= True;
fMouseOverIndex:= -1;
mniCopyItem.Caption := lisCopyItemToClipboard;
mniCopySelected.Caption := lisCopySelectedItemToClipboard;
mniCopyAll.Caption := lisCopyAllItemsToClipboard;
end;//Create
procedure TSearchResultsView.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Key = VK_ESCAPE) and (Parent=nil) then
begin
Key := VK_UNKNOWN;
Close;
end;
end;
procedure TSearchResultsView.mniCopyAllClick(Sender: TObject);
var
sl: TStrings;
begin
sl := (popList.PopupComponent as TLazSearchResultTV).ItemsAsStrings;
Clipboard.AsText := sl.Text;
sl.Free;
end;
procedure TSearchResultsView.mniCopyItemClick(Sender: TObject);
var
tv: TCustomTreeView;
node: TTreeNode;
begin
tv := popList.PopupComponent as TCustomTreeView;
with tv.ScreenToClient(popList.PopupPoint) do
node := tv.GetNodeAt(X, Y);
if node <> nil then
Clipboard.AsText := node.Text;
end;
procedure TSearchResultsView.mniCopySelectedClick(Sender: TObject);
begin
Clipboard.AsText := GetTreeSelectedItemsAsText(popList.PopupComponent as TCustomTreeView);
end;
procedure TSearchResultsView.ResultsNoteBookMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
TabIndex: LongInt;
begin
if (Button = mbMiddle) then begin
TabIndex := ResultsNoteBook.TabIndexAtClientPos(Point(X,Y));
if TabIndex >= 0 then
ResultsNoteBookClosetabclicked(ResultsNoteBook.Page[TabIndex]);
end;
end;
procedure TSearchResultsView.ClosePageButtonClick(Sender: TObject);
begin
ClosePage(ResultsNoteBook.PageIndex);
end;
{Keeps track of the Index of the Item the mouse is over, Sets ShowHint to true
if the Item length is longer than the TreeView client width.}
procedure TSearchResultsView.LazTVMousemove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
if Sender is TLazSearchResultTV then
begin
with Sender as TLazSearchResultTV do
begin
if Assigned(GetNodeAt(X, Y)) then
fMouseOverIndex:=GetNodeAt(X, Y).Index
else
fMouseOverIndex:=-1;
if (fMouseOverIndex > -1) and (fMouseOverIndex < Items.Count)
and (Canvas.TextWidth(Items[fMouseOverIndex].Text) > Width) then
ShowHint:= True
else
ShowHint:= False;
end;//with
end;//
end;//LazTVMousemove
{Keep track of the mouse position over the treeview when the wheel is used}
procedure TSearchResultsView.LazTVMouseWheel(Sender: TObject;
Shift: TShiftState;
WheelDelta: Integer;
MousePos: TPoint;
var Handled: Boolean);
begin
LazTVMouseMove(Sender,Shift,MousePos.X, MousePos.Y);
Handled:= false;
end;//LazTVMouseWheel
procedure TSearchResultsView.edSearchInListChange (Sender: TObject );
var CurrentTV: TLazSearchResultTV;
begin
CurrentTV := GetTreeView(ResultsNoteBook.PageIndex);
if Assigned(CurrentTV) then
CurrentTV.SearchInListPhrases := SearchInListEdit.Text;
end;
procedure TSearchResultsView.ResultsNoteBookPageChanged (Sender: TObject );
var CurrentTV: TLazSearchResultTV;
begin
CurrentTV := GetTreeView(ResultsNoteBook.PageIndex);
if Assigned(CurrentTV) then
SearchInListEdit.Text := CurrentTV.SearchInListPhrases;
UpdateToolbar;
end;
procedure TSearchResultsView.ForwardSearchButtonClick (Sender: TObject );
var CurrentTV: TLazSearchResultTV;
slPhrases: TStrings;
i, j, iCurrentIndex: Integer;
S: string;
begin
CurrentTV := GetTreeView(ResultsNoteBook.PageIndex);
if Assigned(CurrentTV) then
begin
if (Length(SearchInListEdit.Text) = 0) then Exit;//No Search Phrases specified.
if (CurrentTV.Items.Count <= 0) then Exit;
slPhrases := TStringList.Create;
try
if Assigned(CurrentTV.Selected) then
iCurrentIndex := CurrentTV.Selected.Index + 1
else
iCurrentIndex := 0;
if (iCurrentIndex > CurrentTV.Items.Count) then iCurrentIndex := CurrentTV.Items.Count;
if (iCurrentIndex < 0) then iCurrentIndex := 0;//Set to 1st list-item if none are selected
//Parse Phrases
Parse_Search_Phrases(slPhrases);
if (slPhrases.Count > 0) then
begin
for i:=iCurrentIndex to CurrentTV.Items.Count-1 do
begin
S := UpperCase(CurrentTV.Items[i].Text);//for case-insensitive search
for j:=0 to slPhrases.Count-1 do
begin
if (Pos(slPhrases[j], S) <> 0) then
begin
CurrentTV.Items[i].Selected:=True;//Set TreeView's itemindex
Exit;//Found what we looking for, exit
end;//End if (Pos(slPhrases[j], CurrentTV.Items[i]) <> 0)
end;//End for-loop j
end;//End for-loop i
end;//End if if (slPhrases.Count > 0)
finally
FreeAndNil(slPhrases);
SearchInListEdit.SetFocus;
end;//End try-finally
end;//End if Assigned(CurrentTV)
end;
procedure TSearchResultsView.ResetResultsButtonClick (Sender: TObject );
var
i: Integer;
CurrentTV: TLazSearchResultTV;
mpMatchPos, mpOrgMatchPos: TLazSearchMatchPos;
begin
CurrentTV := GetTreeView(ResultsNoteBook.PageIndex);
try
if assigned(CurrentTV) and CurrentTV.Filtered then
begin
if (CurrentTV.Items.Count > 0) then
begin
CurrentTV.FreeObjectsTN(CurrentTV.Items);//Free the objects
CurrentTV.Items.Clear;
end;//End if (CurrentTV.Items.Count > 0)
if (CurrentTV.BackUpStrings.Count > 0) then
begin
CurrentTV.Items.BeginUpdate;
for i:=0 to CurrentTV.BackUpStrings.Count-1 do
begin
mpOrgMatchPos := TLazSearchMatchPos(CurrentTV.BackUpStrings.Objects[i]);
if Assigned(mpOrgMatchPos) then
begin
mpMatchPos := TLazSearchMatchPos.Create;
if CopySearchMatchPos(mpOrgMatchPos, mpMatchPos) then
CurrentTV.AddNode(CurrentTV.BackUpStrings[i], mpMatchPos);
end;//End if Assigned(mpOrgMatchPos)
end;//End for-loop i
end;//End if (CurrentTV.BackUpStrings.Count > 0)
CurrentTV.Items.EndUpdate;
CurrentTV.Filtered := False;
end;//End if CurrentTV.Filtered
finally
SearchInListEdit.SetFocus;
end;//End try-finally
end;
procedure TSearchResultsView.SearchInListEditKeyDown (Sender: TObject;
var Key: Word; Shift: TShiftState );
begin
if (Key = VK_RETURN) then
ForwardSearchButtonClick(ForwardSearchButton);
end;
procedure TSearchResultsView.FilterButtonClick (Sender: TObject );
var
CurrentTV: TLazSearchResultTV;
mpMatchPos, mpOrgMatchPos: TLazSearchMatchPos;
slPhrases: TStrings;
i, j: Integer;
S: string;
begin
CurrentTV := GetTreeView(ResultsNoteBook.PageIndex);
if Assigned(CurrentTV) then
begin
if (Length(SearchInListEdit.Text) = 0) then Exit;//No Filter Phrases specified.
slPhrases := TStringList.Create;
try
//Parse Phrases
Parse_Search_Phrases(slPhrases);
//BackUp Result List
if not (CurrentTV.Filtered or (CurrentTV.BackUpStrings.Count > 0)) then
begin
if (CurrentTV.Items.Count <= 1) then Exit;
for i:=0 to CurrentTV.Items.Count-1 do
begin
mpOrgMatchPos := TLazSearchMatchPos(CurrentTV.Items[i].Data);
if Assigned(mpOrgMatchPos) then
begin
mpMatchPos := TLazSearchMatchPos.Create;
if CopySearchMatchPos(mpOrgMatchPos, mpMatchPos) then
CurrentTV.BackUpStrings.AddObject(CurrentTV.Items[i].Text, mpMatchPos);
end;//End if Assigned(mpOrgMatchPos)
end;//End for-loop i
end;//End if not (CurrentTV.Filtered or (CurrentTV.BackUpStrings.Count > 0))
if (CurrentTV.BackUpStrings.Count <= 0) then Exit;//Empty list
if (CurrentTV.Items.Count > 0) then
begin
CurrentTV.FreeObjectsTN(CurrentTV.Items);//Free the objects
CurrentTV.Items.Clear;//Clear the list
//Clear update items as their objects are freed together with CurrentTV.Items
CurrentTV.UpdateItems.Clear;
end;//End if (CurrentTV.Items.Count > 0)
if (slPhrases.Count > 0) then
begin
CurrentTV.Items.BeginUpdate;
for i:=0 to CurrentTV.BackUpStrings.Count-1 do
begin
S := UpperCase(CurrentTV.BackUpStrings[i]);//for case-insensitive search
for j:=0 to slPhrases.Count-1 do
begin
if (Pos(slPhrases[j], S) <> 0) then
begin
mpOrgMatchPos := TLazSearchMatchPos(CurrentTV.BackUpStrings.Objects[i]);
if Assigned(mpOrgMatchPos) then
begin
mpMatchPos := TLazSearchMatchPos.Create;
if CopySearchMatchPos(mpOrgMatchPos, mpMatchPos) then
CurrentTV.AddNode(CurrentTV.BackUpStrings[i], mpMatchPos);
end;//End if Assigned(mpOrgMatchPos)
end;//End if (Pos(slPhrases[j], S) <> 0)
end;//End for-loop j
end;//End for-loop i
CurrentTV.Items.EndUpdate;
CurrentTV.Filtered := True;
end;//End if if (slPhrases.Count > 0)
finally
FreeAndNil(slPhrases);
SearchInListEdit.SetFocus;
if (CurrentTV.Items.Count > 0) then CurrentTV.Items[0].Selected:=True;//Goto first item
end;//End try-finally
end;//End if Assigned(CurrentTV)
end;
procedure TSearchResultsView.TreeViewMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
TV: TCustomTreeView;
Node: TTreeNode;
begin
if Button<>mbLeft then exit;
TV:=Sender as TCustomTreeView;
Node:=TV.GetNodeAt(X,Y);
if Node=nil then exit;
if x<Node.DisplayTextLeft then exit;
//debugln(['TSearchResultsView.TreeViewMouseDown single=',([ssDouble,ssTriple,ssQuad]*Shift<>[]),' Option=',EnvironmentOptions.MsgViewDblClickJumps]);
if ([ssDouble,ssTriple,ssQuad]*Shift=[]) = EnvironmentOptions.MsgViewDblClickJumps
then exit;
if Assigned(fOnSelectionChanged) then
fOnSelectionChanged(Self);
end;
function TSearchResultsView.BeautifyPageName(const APageName: string): string;
const
MaxPageName = 22;
begin
Result:=SpecialCharsToHex(APageName);
if UTF8Length(Result)>MaxPageName then
Result:=UTF8Copy(Result,1,15)+'...';
end;
procedure TSearchResultsView.AddMatch(const APageIndex: integer;
const Filename: string; const StartPos, EndPos: TPoint;
const TheText: string;
const MatchStart: integer; const MatchLen: integer);
var
CurrentTV: TLazSearchResultTV;
SearchPos: TLazSearchMatchPos;
ShownText: String;
LastPos: TLazSearchMatchPos;
begin
CurrentTV:=GetTreeView(APageIndex);
if Assigned(CurrentTV) then
begin
if CurrentTV.UpdateState then begin
if CurrentTV.UpdateItems.Count>=MaxItems then begin
CurrentTV.Skipped:=CurrentTV.Skipped+1;
exit;
end;
end else begin
if CurrentTV.Items.Count>=MaxItems then begin
CurrentTV.Skipped:=CurrentTV.Skipped+1;
exit;
end;
end;
SearchPos:= TLazSearchMatchPos.Create;
SearchPos.MatchStart:=MatchStart;
SearchPos.MatchLen:=MatchLen;
SearchPos.Filename:=Filename;
SearchPos.FileStartPos:=StartPos;
SearchPos.FileEndPos:=EndPos;
SearchPos.TheText:=TheText;
SearchPos.ShownFilename:=SearchPos.Filename;
ShownText:=CurrentTV.BeautifyLine(SearchPos);
LastPos:=nil;
if CurrentTV.UpdateState then begin
if (CurrentTV.UpdateItems.Count>0)
and (CurrentTV.UpdateItems.Objects[CurrentTV.UpdateItems.Count-1] is TLazSearchMatchPos) then
LastPos:=TLazSearchMatchPos(CurrentTV.UpdateItems.Objects[CurrentTV.UpdateItems.Count-1]);
end else
if (CurrentTV.Items.Count>0) and Assigned(CurrentTV.Items[CurrentTV.Items.Count-1].Data) then
LastPos:=TLazSearchMatchPos(CurrentTV.Items[CurrentTV.Items.Count-1].Data);
if (LastPos<>nil) and (LastPos.Filename=SearchPos.Filename) and
(LastPos.FFileStartPos.Y=SearchPos.FFileStartPos.Y) and
(LastPos.FFileEndPos.Y=SearchPos.FFileEndPos.Y) then
begin
while (LastPos.NextInThisLine<>nil) do
LastPos := LastPos.NextInThisLine;
LastPos.NextInThisLine:=SearchPos
end
else if CurrentTV.UpdateState then
CurrentTV.UpdateItems.AddObject(ShownText, SearchPos)
else
CurrentTV.AddNode(ShownText, SearchPos);
CurrentTV.ShortenPaths;
end;//if
end;//AddMatch
Procedure TSearchResultsView.BeginUpdate(APageIndex: integer);
var
CurrentTV: TLazSearchResultTV;
begin
CurrentTV:= GetTreeView(APageIndex);
if Assigned(CurrentTV) then
CurrentTV.BeginUpdate;
UpdateToolbar;
end;//BeginUpdate
procedure TSearchResultsView.EndUpdate(APageIndex: integer);
var
CurrentTV: TLazSearchResultTV;
begin
CurrentTV:= GetTreeView(APageIndex);
if Assigned(CurrentTV) then
begin
CurrentTV.EndUpdate;
if CurrentTV.Items.Count>0 then begin
CurrentTV.Items[0].Selected:=True;
end;
end;
UpdateToolbar;
end;
procedure TSearchResultsView.Parse_Search_Phrases(var slPhrases: TStrings);
var i, iLength: Integer;
sPhrases, sPhrase: string;
begin
//Parse Phrases
sPhrases := SearchInListEdit.Text;
iLength := Length(sPhrases);
sPhrase := '';
for i:=1 to iLength do
begin
if ((sPhrases[i] = ' ') or (sPhrases[i] = ',') or (i = iLength)) then
begin
if not ((sPhrases[i] = ' ') or (sPhrases[i] = ',')) then
sPhrase := sPhrase + sPhrases[i];
if (sPhrase > ' ') then
slPhrases.Add(UpperCase(sPhrase));//End of phrase, add to phrase list
sPhrase := '';//Reset sPhrase
end else
begin
if (sPhrases[i] > ' ') then
sPhrase := sPhrase + sPhrases[i];
end;//End if ((sPhrases[i] = ' ') or (sPhrases[i] = ','))
end;//End for-loop i
end;
procedure TSearchResultsView.ClosePage(PageIndex: integer);
var
CurrentTV: TLazSearchResultTV;
begin
if (PageIndex<0) or (PageIndex>=ResultsNoteBook.PageCount) then exit;
CurrentTV:= GetTreeView(PageIndex);
if Assigned(CurrentTV) and CurrentTV.UpdateState then
exit;
ResultsNoteBook.Pages[PageIndex].Free;
if ResultsNoteBook.PageCount = 0 then
Hide;
end;
{Sets the Items from the treeview on the currently selected page in the
TNoteBook}
procedure TSearchResultsView.SetItems(Index: integer; Value: TStrings);
var
CurrentTV: TLazSearchResultTV;
begin
if Index > -1 then
begin
CurrentTV:= GetTreeView(Index);
if Assigned(CurrentTV) then
begin
if CurrentTV.UpdateState then
CurrentTV.UpdateItems.Assign(Value)
else
CurrentTV.Items.Assign(Value);
CurrentTV.Skipped:=0;
end;//if
end//if
end;//SetItems
function TSearchResultsView.GetItems(Index: integer): TStrings;
var
CurrentTV: TLazSearchResultTV;
begin
result:= nil;
CurrentTV:= GetTreeView(Index);
if Assigned(CurrentTV) then
begin
if CurrentTV.UpdateState then
result:= CurrentTV.UpdateItems
else
Result := CurrentTV.ItemsAsStrings;
end;//if
end;//GetItems
procedure TSearchResultsView.SetMaxItems(const AValue: integer);
begin
if FMaxItems=AValue then exit;
FMaxItems:=AValue;
end;
procedure TSearchResultsView.UpdateToolbar;
var
CurrentTV: TLazSearchResultTV;
state: Boolean;
begin
CurrentTV:= GetTreeView(ResultsNoteBook.PageIndex);
state := Assigned(CurrentTV) and not CurrentTV.UpdateState;
SearchAgainButton.Enabled := state;
ClosePageButton.Enabled := state;
FilterButton.Enabled := state;
ForwardSearchButton.Enabled := state;
ResetResultsButton.Enabled := state;
SearchInListEdit.Enabled := state;
end;
procedure TSearchResultsView.ResultsNoteBookCloseTabclicked(Sender: TObject);
begin
if (Sender is TTabSheet) then
ClosePage(TTabSheet(Sender).PageIndex)
end;
procedure TSearchResultsView.SearchAgainButtonClick(Sender: TObject);
var
CurrentTV: TLazSearchResultTV;
SearchObj: TLazSearch;
begin
CurrentTV:= GetTreeView(ResultsNoteBook.PageIndex);
if not Assigned(CurrentTV) then begin
MainIDEInterface.FindInFilesPerDialog(Project1);
end
else begin
SearchObj:= CurrentTV.SearchObject;
OnSearchAgainClicked(SearchObj);
MainIDEInterface.FindInFiles(Project1, SearchObj.SearchString);
end;
end;
procedure TSearchResultsView.TreeViewKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Key = VK_RETURN) and (Shift = []) then
begin
Key:=VK_UNKNOWN;
FOnSelectionChanged(Self);
end;
end;
{ Add Result will create a tab in the Results view window with an new
treeview or focus an existing TreeView and update it's searchoptions.}
function TSearchResultsView.AddSearch(const ResultsName: string;
const SearchText: string;
const ReplaceText: string;
const ADirectory: string;
const AMask: string;
const TheOptions: TLazFindInFileSearchOptions): TTabSheet;
var
NewTreeView: TLazSearchResultTV;
NewPage: LongInt;
i: integer;
SearchObj: TLazSearch;
begin
Result:= nil;
if Assigned(ResultsNoteBook) then
with ResultsNoteBook do
begin
FWorkedSearchText:=BeautifyPageName(ResultsName);
i:= GetPageIndex(FWorkedSearchText);
if i>=0 then
begin
NewTreeView:= GetTreeView(i);
PageIndex:= i;
//Free backup objects and list since its a new search with the same TreeView
NewTreeView.FreeObjects(NewTreeView.BackUpStrings);
NewTreeView.BackUpStrings.Clear;
NewTreeView.Filtered := False;
end//if
else
begin
NewPage:= TCustomNotebook(ResultsNoteBook).Pages.Add(FWorkedSearchText);
PageIndex:= NewPage;
Page[PageIndex].OnKeyDown := @TreeViewKeyDown;
if NewPage > -1 then
begin
NewTreeView:= TLazSearchResultTV.Create(Page[NewPage]);
with NewTreeView do
begin
Parent:= Page[NewPage];
Align:= alClient;
BorderSpacing.Around := 0;
OnKeyDown := @TreeViewKeyDown;
OnAdvancedCustomDrawItem:= @TreeViewAdvancedCustomDrawItem;
OnShowHint:= @LazTVShowHint;
OnMouseMove:= @LazTVMousemove;
OnMouseWheel:= @LazTVMouseWheel;
OnMouseDown:=@TreeViewMouseDown;
ShowHint:= true;
RowSelect := True; // we are using custom draw
Options := Options + [tvoAllowMultiselect] - [tvoThemedDraw];
PopupMenu := popList;
NewTreeView.Canvas.Brush.Color:= clWhite;
end;//with
end;//if
end;//else
SearchObj:=NewTreeView.SearchObject;
if SearchObj<>nil then begin
SearchObj.SearchString:= SearchText;
SearchObj.ReplaceText := ReplaceText;
SearchObj.SearchDirectory:= ADirectory;
SearchObj.SearchMask:= AMask;
SearchObj.SearchOptions:= TheOptions;
end;
NewTreeView.Skipped:=0;
Result:= Pages[PageIndex];
SearchInListEdit.Clear;
end;//with
end;//AddResult
procedure TSearchResultsView.LazTVShowHint(Sender: TObject;
HintInfo: PHintInfo);
var
MatchPos: TLazSearchMatchPos;
HintStr: string;
begin
if Sender is TLazSearchResultTV then
begin
With Sender as TLazSearchResultTV do
begin
if (fMouseOverIndex >= 0) and (fMouseOverIndex < Items.Count) then
begin
if Assigned(Items[fMouseOverIndex].Data) then
MatchPos:= TLazSearchMatchPos(Items[fMouseOverIndex].Data)
else
MatchPos:= nil;
if MatchPos<>nil then
HintStr:=MatchPos.Filename
+' ('+IntToStr(MatchPos.FileStartPos.Y)
+','+IntToStr(MatchPos.FileStartPos.X)+')'
+' '+MatchPos.TheText
else
HintStr:=Items[fMouseOverIndex].Text;
Hint:= HintStr;
end;//if
end;//with
end;//if
end;//LazTVShowHint
procedure TSearchResultsView.TreeViewAdvancedCustomDrawItem(
Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState;
Stage: TCustomDrawStage; var PaintImages, DefaultDraw: Boolean);
var
CurPart: string;
TheTop: integer;
MatchObj: TObject;
MatchPos,FirstMatchPos: TLazSearchMatchPos;
TextEnd, DrawnTextLength: integer;
ARect: TRect;
begin
if Stage <> cdPostPaint then Exit;
With Sender as TLazSearchResultTV do
begin
if [cdsSelected,cdsMarked] * State <> [] then
Canvas.Font.Color := clHighlightText;
ARect:=Node.DisplayRect(true);
Canvas.FillRect(ARect);
MatchObj := TLazSearchMatchPos(Node.Data);
if assigned(MatchObj) and (MatchObj is TLazSearchMatchPos) then
MatchPos:= TLazSearchMatchPos(Node.Data)
else
MatchPos:= nil;
if Assigned(MatchPos) then
begin
FirstMatchPos:=MatchPos;
TheTop:= ARect.Top;
TextEnd:=ARect.Left;
DrawnTextLength:=0;
CurPart:=MatchPos.ShownFilename+' ('+IntToStr(MatchPos.FileStartPos.Y)
+':'+IntToStr(MatchPos.FileStartPos.X);
MatchPos:=MatchPos.NextInThisLine;
SetBkMode(Canvas.Handle, TRANSPARENT);
while assigned(MatchPos) do begin
CurPart:=CurPart+','+IntToStr(MatchPos.FileStartPos.X);
MatchPos:=MatchPos.NextInThisLine;
end;
CurPart:=CurPart+') ';
Canvas.TextOut(TextEnd, TheTop, CurPart);
TextEnd:= TextEnd + Canvas.TextWidth(CurPart);
MatchPos:=FirstMatchPos;
while assigned(MatchPos) do begin
CurPart:=SpecialCharsToHex(copy(MatchPos.TheText,DrawnTextLength+1,MatchPos.MatchStart-1-DrawnTextLength));
DrawnTextLength:=MatchPos.MatchStart-1;
Canvas.TextOut(TextEnd, TheTop, CurPart);
TextEnd:= TextEnd + Canvas.TextWidth(CurPart);
CurPart:=SpecialCharsToHex(copy(MatchPos.TheText,DrawnTextLength+1,MatchPos.MatchLen));
DrawnTextLength:=DrawnTextLength+MatchPos.MatchLen;
if UTF8Length(CurPart)>MaxTextLen then
CurPart:=UTF8Copy(CurPart,1,MaxTextLen)+'...';
Canvas.Font.Style:= Canvas.Font.Style + [fsBold];
Canvas.TextOut(TextEnd, TheTop, CurPart);
TextEnd:= TextEnd + Canvas.TextWidth(CurPart);
Canvas.Font.Style:= Canvas.Font.Style - [fsBold];
if MatchPos.NextInThisLine=nil then begin
CurPart:=SpecialCharsToHex(copy(MatchPos.TheText, DrawnTextLength+1,Length(MatchPos.TheText)));
Canvas.TextOut(TextEnd, TheTop, CurPart);
end;
MatchPos:=MatchPos.NextInThisLine;
end;
end
else begin
// this is usually the filename only
// draw it here too, so that the correct colors are used
Canvas.TextOut(ARect.Left, ARect.Top, Node.Text);
end;//if
end;//with
end;//TreeViewDrawItem
{Returns the Position within the source file from a properly formated search
result}
function TSearchResultsView.GetSourcePositon: TPoint;
var
MatchPos: TLazSearchMatchPos;
begin
Result.x:= -1;
Result.y:= -1;
MatchPos:=GetSelectedMatchPos;
if MatchPos=nil then exit;
Result:=MatchPos.FileStartPos;
end;//GetSourcePositon
{Returns The file name portion of a properly formated search result}
function TSearchResultsView.GetSourceFileName: string;
var
MatchPos: TLazSearchMatchPos;
begin
MatchPos:=GetSelectedMatchPos;
if MatchPos=nil then
Result:=''
else
Result:=MatchPos.Filename;
end;//GetSourceFileName
{Returns the selected text in the currently active TreeView.}
function TSearchResultsView.GetSelectedText: string;
var
ThePage: TTabSheet;
TheTreeView: TLazSearchResultTV;
i: integer;
begin
result:= '';
i:= ResultsNoteBook.PageIndex;
if i > -1 then
begin
ThePage:= ResultsNoteBook.Pages[i];
if Assigned(ThePage) then
begin
TheTreeView:= GetTreeView(ThePage.PageIndex);
if Assigned(TheTreeView.Selected) then
Result:= TheTreeView.Selected.Text;
end;//if
end;//if
end;//GetSelectedText
function TSearchResultsView.GetSelectedMatchPos: TLazSearchMatchPos;
var
ThePage: TTabSheet;
TheTreeView: TLazSearchResultTV;
i: integer;
begin
Result:= nil;
i:= ResultsNoteBook.PageIndex;
if i > -1 then
begin
ThePage:= ResultsNoteBook.Pages[i];
if Assigned(ThePage) then
begin
TheTreeView:= GetTreeView(ThePage.PageIndex);
if Assigned(TheTreeView.Selected) then
Result := TLazSearchMatchPos(TheTreeView.Selected.Data);
end;
end;
end;
function TSearchResultsView.GetPageIndex(const APageName: string): integer;
var
Paren, i: integer;
PN: String;
begin
Result:= -1;
for i:= 0 to ResultsNoteBook.PageCount - 1 do
begin
PN:= ResultsNoteBook.Page[i].Caption;
Paren:= Pos(' (', PN);
if (Paren>0) and (PosEx(')', PN, Paren+2)>0) then
PN:= LeftStr(PN, Paren-1);
if PN = APageName then
begin
Result:= i;
break;
end;
end;
end;
{Returns a the TreeView control from a Tab if both the page and the TreeView
exist else returns nil}
function TSearchResultsView.GetTreeView(APageIndex: integer): TLazSearchResultTV;
var
i: integer;
ThePage: TTabSheet;
begin
Result:= nil;
if (APageIndex > -1) and (APageIndex < ResultsNoteBook.PageCount) then
begin
ThePage:= ResultsNoteBook.Pages[APageIndex];
if Assigned(ThePage) then
begin
for i:= 0 to ThePage.ComponentCount - 1 do
begin
if ThePage.Components[i] is TLazSearchResultTV then
begin
result:= TLazSearchResultTV(ThePage.Components[i]);
break;
end;
end;
end;
end;
end;
procedure TLazSearchResultTV.SetSkipped(const AValue: integer);
var
SrcList: TStrings;
s: String;
HasSkippedLine: Boolean;
SkippedLine: String;
begin
if FSkipped=AValue then exit;
FSkipped:=AValue;
s:=rsFoundButNotListedHere;
if fUpdating then
SrcList:=fUpdateStrings
else
SrcList:=ItemsAsStrings;
if (SrcList.Count>0) and (copy(SrcList[SrcList.Count-1],1,length(s))=s) then
HasSkippedLine:=true
else
HasSkippedLine:=false;
SkippedLine:=s+IntToStr(FSkipped);
if FSkipped>0 then begin
if HasSkippedLine then begin
SrcList[SrcList.Count-1]:=SkippedLine;
end else begin
SrcList.add(SkippedLine);
end;
end else begin
if HasSkippedLine then
SrcList.Delete(SrcList.Count-1);
end;
end;
procedure TLazSearchResultTV.AddNode(Line: string; MatchPos: TLazSearchMatchPos);
var
Node: TTreeNode;
ChildNode: TTreeNode;
begin
if MatchPos=nil then exit;
Node := Items.FindNodeWithText(MatchPos.FileName);
//enter a new file entry
if not Assigned(Node) then
Node := Items.Add(Node, MatchPos.FileName);
ChildNode := Items.AddChild(Node, Line);
Node.Expanded:=true;
ChildNode.Data := MatchPos;
end;
{******************************************************************************
TLazSearchResultTV
******************************************************************************}
Constructor TLazSearchResultTV.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ReadOnly := True;
fSearchObject:= TLazSearch.Create;
fUpdating:= false;
fUpdateCount:= 0;
fUpdateStrings:= TStringList.Create;
fBackUpStrings := TStringList.Create;
FSearchInListPhrases := '';
fFiltered := False;
end;//Create
Destructor TLazSearchResultTV.Destroy;
begin
if Assigned(fSearchObject) then
FreeAndNil(fSearchObject);
//if UpdateStrings is empty,
//means the objects are stored in Items due to filtering
//filtering clears UpdateStrings
if (fUpdateStrings.Count = 0) then
FreeObjectsTN(Items);
if Assigned(fUpdateStrings) then
begin
FreeObjects(fUpdateStrings);
FreeAndNil(fUpdateStrings);
end;//if
if Assigned(fBackUpStrings) then
begin
FreeObjects(fBackUpStrings);
FreeAndNil(fBackUpStrings);
end;//End if Assigned(fBackUpStrings)
inherited Destroy;
end;//Destroy
procedure TLazSearchResultTV.BeginUpdate;
var
s: TStrings;
begin
inc(fUpdateCount);
if (fUpdateCount = 1) then
begin
// save old treeview content
if Assigned(Items) then
begin
s := ItemsAsStrings;
fUpdateStrings.Assign(s);
s.Free;
end;
fUpdating:= true;
end;//if
end;//BeginUpdate
procedure TLazSearchResultTV.EndUpdate;
var
i: integer;
begin
if (fUpdateCount = 0) then
RaiseGDBException('TLazSearchResultTV.EndUpdate');
Dec(fUpdateCount);
if (fUpdateCount = 0) then
begin
ShortenPaths;
fUpdating:= false;
FreeObjectsTN(Items);
Items.BeginUpdate;
Items.Clear;
for i := 0 to fUpdateStrings.Count - 1 do
AddNode(fUpdateStrings[i], TLazSearchMatchPos(fUpdateStrings.Objects[i]));
Items.EndUpdate;
end;//if
end;//EndUpdate
procedure TLazSearchResultTV.ShortenPaths;
var
i: Integer;
AnObject: TObject;
SharedPath: String;
MatchPos: TLazSearchMatchPos;
SrcList: TStrings;
SharedLen: Integer;
ShownText: String;
FreeSrcList: Boolean;
begin
if fUpdateCount>0 then begin
fShortenPathNeeded:=true;
exit;
end;
fShortenPathNeeded:=false;
if fUpdating then begin
SrcList:=fUpdateStrings;
FreeSrcList:=false;
end else begin
SrcList:=ItemsAsStrings;
FreeSrcList:=true;
end;
try
// find shared path (the path of all filenames, that is the same)
SharedPath:='';
for i:=0 to SrcList.Count-1 do begin
AnObject:=SrcList.Objects[i];
if AnObject is TLazSearchMatchPos then begin
MatchPos:=TLazSearchMatchPos(AnObject);
if i=0 then
SharedPath:=ExtractFilePath(MatchPos.Filename)
else if (SharedPath<>'') then begin
SharedLen:=0;
while (SharedLen<length(MatchPos.Filename))
and (SharedLen<length(SharedPath))
and (MatchPos.Filename[SharedLen+1]=SharedPath[SharedLen+1])
do
inc(SharedLen);
while (SharedLen>0) and (SharedPath[SharedLen]<>PathDelim) do
dec(SharedLen);
if SharedLen<>length(SharedPath) then
SharedPath:=copy(SharedPath,1,SharedLen);
end;
end;
end;
// shorten shown paths
SharedLen:=length(SharedPath);
for i:=0 to SrcList.Count-1 do begin
AnObject:=SrcList.Objects[i];
if AnObject is TLazSearchMatchPos then begin
MatchPos:=TLazSearchMatchPos(AnObject);
MatchPos.ShownFilename:=copy(MatchPos.Filename,SharedLen+1,
length(MatchPos.Filename));
ShownText:=BeautifyLine(MatchPos);
SrcList[i]:=ShownText;
SrcList.Objects[i]:=MatchPos;
end;
end;
finally
if FreeSrcList then SrcList.Free;
end;
end;
procedure TLazSearchResultTV.FreeObjectsTN(tnItems: TTreeNodes);
var i: Integer;
begin
for i:=0 to tnItems.Count-1 do
if Assigned(tnItems[i].Data) then
TLazSearchMatchPos(tnItems[i].Data).Free;
end;
procedure TLazSearchResultTV.FreeObjects(slItems: TStrings);
var i: Integer;
begin
if (slItems.Count <= 0) then Exit;
for i:=0 to slItems.Count-1 do
begin
if Assigned(slItems.Objects[i]) then
slItems.Objects[i].Free;
end;//End for-loop
end;
function TLazSearchResultTV.BeautifyLine(const Filename: string; X, Y: integer;
const Line: string): string;
begin
Result:=SpecialCharsToHex(Line);
if UTF8Length(Result)>MaxTextLen then
Result:=UTF8Copy(Result,1,MaxTextLen)+'...';
Result:=Filename
+' ('+IntToStr(Y)
+','+IntToStr(X)+')'
+' '+Result;
end;
function TLazSearchResultTV.BeautifyLine(SearchPos: TLazSearchMatchPos
): string;
begin
Result:=BeautifyLine(SearchPos.ShownFilename,SearchPos.FileStartPos.X,
SearchPos.FileStartPos.Y,SearchPos.TheText);
end;
function TLazSearchResultTV.ItemsAsStrings: TStrings;
var
i: integer;
begin
Result := TStringList.Create;
for i := 0 to Items.Count - 1 do
Result.AddObject(Items[i].Text,TObject(Items[i].Data));
end;
{ TLazSearchMatchPos }
destructor TLazSearchMatchPos.Destroy;
begin
FreeAndNil(FNextInThisLine);
inherited Destroy;
end;
end.
|