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
|
unit TextSearch;
interface
uses
gnugettext, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TntStdCtrls, ComCtrls, TntComCtrls, TntForms, ExtCtrls,
Options, AuxFuncs;
type
TTextSearchOption = (
tsoBackwards, // Search backwards instead of forward.
tsoEntireScope, // Search in entire text, not only in selected text.
tsoIgnoreNonSpacing, // Ignore non-spacing characters in search.
tsoMatchCase, // Case sensitive search.
tsoPrompt, // Ask user for each replace action.
tsoRegularExpression, // Search using regular expressions.
tsoReplace, // Replace, not simple search.
tsoReplaceAll, // Replace all occurences
tsoSelectedOnly, // Search in selected text only.
tsoSpaceCompress, // Handle several consecutive white spaces as one white space,
// so "ab cd" will match "ab cd" and "ab cd".
tsoWholeWord // Match entire words only.
);
TTextSearchOptions = set of TTextSearchOption;
TTextSearchEvent = function (Sender: TObject;
SearchText: WideString; ReplaceText: WideString;
SearchOptions: TTextSearchOptions): Integer of object;
TTextSearchForm = class(TTntForm)
PageControl: TTntPageControl;
SearchTabSheet: TTntTabSheet;
ReplaceTabSheet: TTntTabSheet;
SearchLbl: TTntLabel;
SearchLU: TTntComboBox;
SearchBtn: TTntButton;
CloseBtn: TTntButton;
ReplacesSearchLbl: TTntLabel;
ReplaceSourceLU: TTntComboBox;
ReplaceWithLbl: TTntLabel;
ReplaceWithLU: TTntComboBox;
ReplaceAllBtn: TTntButton;
Close2Btn: TTntButton;
OptionsTabSheet: TTntTabSheet;
ReplaceBtn: TTntButton;
DetailsBtn: TTntButton;
Details2Btn: TTntButton;
OptionPnl: TPanel;
ScopeGBox: TTntGroupBox;
SearchInSelectedColumnRBtn: TTntRadioButton;
SearchAllColumnsRBtn: TTntRadioButton;
DirectionGBox: TTntGroupBox;
SearchDownRBtn: TTntRadioButton;
SearchUpRBtn: TTntRadioButton;
OptionsGBox: TTntGroupBox;
CaseSensitiveCBox: TTntCheckBox;
WholeWordsOnlyCBox: TTntCheckBox;
UseRegExCBox: TTntCheckBox;
TntGroupBox1: TTntGroupBox;
SearchFromCursorRBtn: TTntRadioButton;
SearchFromTopRBtn: TTntRadioButton;
NoMatchFoundPnl: TPanel;
constructor Create(Sender: TComponent;
OptionProvider: IOptionProvider); reintroduce;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure CloseBtnClick(Sender: TObject);
procedure SearchBtnClick(Sender: TObject);
procedure ReplaceBtnClick(Sender: TObject);
procedure ReplaceAllBtnClick(Sender: TObject);
procedure CaseSensitiveCBoxClick(Sender: TObject);
procedure UpdateCurrentOptions;
procedure FormDeactivate(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure DetailsBtnClick(Sender: TObject);
procedure PageControlChange(Sender: TObject);
private
{ Private declarations }
FOnSearch: TTextSearchEvent;
FCurrentOptions: TTextSearchOptions;
FOptionProvider: IOptionProvider;
FDetailsVisible: Boolean;
protected
procedure SetIOptionProvider(OptionProvider: IOptionProvider);
procedure SetDetailsVisible(DetailsVisible: Boolean);
public
{ Public declarations }
property OnSearch: TTextSearchEvent read FOnSearch write FOnSearch;
property OptionProvider: IOptionProvider read FOptionProvider write SetIOptionProvider;
property DetailsVisible: Boolean read FDetailsVisible write SetDetailsVisible;
end;
var
TextSearchForm: TTextSearchForm;
implementation
{$R *.dfm}
// -----------------------------------------------------------------------------
constructor TTextSearchForm.Create(Sender: TComponent;
OptionProvider: IOptionProvider);
begin
inherited Create(Sender);
InitForm(self);
FOptionProvider := OptionProvider;
if (FOptionProvider=nil) then
raise Exception.Create('No option provider given.');
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.FormCreate(Sender: TObject);
begin
PageControl.ActivePageIndex := 0;
OptionsTabSheet.TabVisible := False;
Height := 198;
if (FOptionProvider<>nil) and
(Not(FOptionProvider.RestoreWindowPos(self, False)))then
begin
Left:=(Screen.Width+Width) div 2 - Width;
Top:=(Screen.Height+Height) div 2 - Height;
end;
FCurrentOptions := [];
DetailsVisible := FOptionProvider.OptionAsBoolean['ShowTextSearchDetails'];
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.FormDestroy(Sender: TObject);
begin
if (FOptionProvider<>nil) then
FOptionProvider.AddWindowPos(self);
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
TextSearchForm := nil;
Action := caFree;
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.CloseBtnClick(Sender: TObject);
begin
Close;
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.SearchBtnClick(Sender: TObject);
begin
if Assigned(FOnSearch) then
begin
NoMatchFoundPnl.Visible :=
(FOnSearch(Self, SearchLU.Text, '', FCurrentOptions)=0);
end;
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.ReplaceBtnClick(Sender: TObject);
var
SearchOptions: TTextSearchOptions;
begin
if Assigned(FOnSearch) then
begin
SearchOptions := FCurrentOptions;
include(SearchOptions, tsoReplace);
NoMatchFoundPnl.Visible :=
(FOnSearch(Self, ReplaceSourceLU.Text, ReplaceWithLU.Text, SearchOptions)=0);
end;
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.ReplaceAllBtnClick(Sender: TObject);
var
SearchOptions: TTextSearchOptions;
begin
if Assigned(FOnSearch) then
begin
SearchOptions := FCurrentOptions;
include(SearchOptions, tsoReplaceAll);
FOnSearch(Self, ReplaceSourceLU.Text, ReplaceWithLU.Text, SearchOptions);
end;
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.CaseSensitiveCBoxClick(Sender: TObject);
begin
UpdateCurrentOptions;
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.UpdateCurrentOptions;
begin
FCurrentOptions := [];
if CaseSensitiveCBox.Checked then
include(FCurrentOptions, tsoMatchCase);
if WholeWordsOnlyCBox.Checked then
include(FCurrentOptions, tsoWholeWord);
if UseRegExCBox.Checked then
include(FCurrentOptions, tsoRegularExpression);
if SearchInSelectedColumnRBtn.Checked then
include(FCurrentOptions, tsoSelectedOnly);
if SearchUpRBtn.Checked then
include(FCurrentOptions, tsoBackwards);
if SearchFromTopRBtn.Checked then
include(FCurrentOptions, tsoEntireScope);
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.FormDeactivate(Sender: TObject);
begin
AlphaBlend := True;
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.FormActivate(Sender: TObject);
begin
AlphaBlend := False;
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Key=VK_RETURN) and (Shift=[]) and
(PageControl.ActivePage=SearchTabSheet) then
SearchBtnClick(self)
else
if (Key=VK_ESCAPE) and (Shift=[]) then
Close;
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.DetailsBtnClick(Sender: TObject);
begin
DetailsVisible := Not(DetailsVisible);
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.PageControlChange(Sender: TObject);
begin
if OptionPnl.Parent<>OptionsTabSheet then
begin
OptionPnl.Parent := PageControl.ActivePage;
OptionPnl.Left := 0;
OptionPnl.Top := 82;
end;
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.SetIOptionProvider(OptionProvider: IOptionProvider);
begin
FOptionProvider := OptionProvider;
end;
// -----------------------------------------------------------------------------
procedure TTextSearchForm.SetDetailsVisible(DetailsVisible: Boolean);
begin
FDetailsVisible := DetailsVisible;
FOptionProvider.OptionAsBoolean['ShowTextSearchDetails'] := DetailsVisible;
if (DetailsVisible) and (OptionPnl.Parent=OptionsTabSheet) then
begin
Height := 198 + 148;
OptionPnl.Align := alNone;
OptionPnl.Parent := PageControl.ActivePage;
OptionPnl.Left := 0;
OptionPnl.Top := 82;
OptionPnl.Height := 160;
DetailsBtn.Caption := _('<< Details');
Details2Btn.Caption := DetailsBtn.Caption;
end
else if Not(DetailsVisible) and Not(OptionPnl.Parent=OptionsTabSheet) then
begin
OptionPnl.Parent := OptionsTabSheet;
Height := 198;
DetailsBtn.Caption := _('Details >>');
Details2Btn.Caption := DetailsBtn.Caption;
end;
end;
end.
|