-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFind.pas
193 lines (173 loc) · 4.73 KB
/
Find.pas
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
//******************************************************************************
// LBA Text Editor 2 - editing lbt (text) files from Little Big Adventure 1 & 2
//
// Find unit.
// Contains searching and replacing routines.
//
// Copyright (C) Zink
// e-mail: zink@poczta.onet.pl
// See the GNU General Public License (License.txt) for details.
//******************************************************************************
unit Find;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls;
type
TFindForm = class(TForm)
Label1: TLabel;
Edit1: TEdit;
cbCase: TCheckBox;
Button1: TButton;
Button2: TButton;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Label3: TLabel;
Edit2: TEdit;
rbBegin: TRadioButton;
rbSelected: TRadioButton;
procedure FormShow(Sender: TObject);
procedure Edit1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Procedure ShowSpecial(Replace: Boolean);
end;
var
FindForm: TFindForm;
TextNum: Integer = -1;
TextPos: Integer = -1;
procedure CenterSelected;
procedure FindSelected;
Function FindText(ShowMsg: Boolean = True): Boolean;
Function FindSingle(Source: String; ShowMsg: Boolean = True): Boolean;
Procedure Replace;
implementation
{$R *.dfm}
uses LBATxt1, Editor, Lang, files;
procedure CenterSelected;
var Buffer: Integer;
begin
If Entries[Selected+1].YPos-Entries[Selected].YPos>=Form1.TextPB.Height then
Buffer:=Entries[Selected].YPos
else
Buffer:=(Entries[Selected].YPos+Entries[Selected+1].YPos-Form1.TextPB.Height) div 2;
If Buffer<0 then Buffer:=0;
If Buffer>Form1.TextScr.Max then Buffer:=Form1.TextScr.Max;
Form1.TextScr.Position:=Buffer;
DrawTexts;
end;
procedure FindSelected;
begin
If Entries[Selected+1].YPos-Entries[Selected].YPos>=Form1.TextPB.Height then
Form1.TextScr.Position:=Entries[Selected].YPos
else begin
If Entries[Selected].YPos-Form1.TextScr.Position<1 then
Form1.TextScr.Position:=Entries[Selected].YPos-1
else if Entries[Selected+1].YPos-Form1.TextScr.Position>Form1.TextPB.Height then
Form1.TextScr.Position:=Entries[Selected+1].YPos-Form1.TextPB.Height;
end;
DrawTexts;
end;
Function FindText(ShowMsg: Boolean = True): Boolean;
var a, b: Integer;
Text, TextToFind: String;
begin
Result:=True;
For a:=TextNum to High(Entries)-1 do begin
If FindForm.cbCase.Checked then begin
Text:=Entries[a].Text;
TextToFind:=FindForm.Edit1.Text;
end
else begin
Text:=LowerCase(Entries[a].Text);
TextToFind:=LowerCase(FindForm.Edit1.Text);
end;
For b:=1 to Length(Text)-Length(TextToFind)+1 do
If TextToFind=Copy(Text,b,Length(TextToFind)) then begin
TextNum:=a;
TextPos:=b;
Exit;
end;
end;
TextPos:=-1;
TextNum:=-1;
If ShowMsg then MessageBox(FindForm.Handle,PChar(sNotFound),'LBA Text Editor 2',MB_ICONINFORMATION+MB_OK);
Result:=False;
end;
Function FindSingle(Source: String; ShowMsg: Boolean = True): Boolean;
var a: Integer;
Text, TextToFind: String;
begin
Result:=True;
If FindForm.cbCase.Checked then begin
Text:=Source;
TextToFind:=FindForm.Edit1.Text;
end
else begin
Text:=LowerCase(Source);
TextToFind:=LowerCase(FindForm.Edit1.Text);
end;
For a:=TextPos to Length(Text)-Length(TextToFind)+1 do
If TextToFind=Copy(Text,a,Length(TextToFind)) then begin
TextPos:=a;
Exit;
end;
TextPos:=-1;
If ShowMsg then MessageBox(FindForm.Handle,PChar(sNotFound),'LBA Text Editor 2',MB_ICONINFORMATION+MB_OK);
Result:=False;
end;
Procedure ReplaceSingle;
begin
Delete(Entries[TextNum].Text,TextPos,Length(FindForm.Edit1.Text));
Insert(FindForm.Edit2.Text,Entries[TextNum].Text,TextPos);
end;
Procedure Replace;
var a: Integer;
begin
a:=0;
If FindForm.rbBegin.Checked then TextNum:=0
else TextNum:=Selected;
While FindText(False) do begin
ReplaceSingle;
Inc(a);
While FindSingle(Entries[TextNum].Text,False) do begin
ReplaceSingle;
Inc(a);
Inc(TextPos);
end;
Inc(TextNum);
end;
If a>0 then begin
MessageBox(Form1.Handle,PChar(Format(sMFound,[a])),'LBA Text Editor 2',MB_ICONINFORMATION+MB_OK);
SetModified;
end
else
MessageBox(Form1.Handle,PChar(sNotFound),'LBA Text Editor 2',MB_ICONINFORMATION+MB_OK);
end;
Procedure TFindForm.ShowSpecial(Replace: Boolean);
begin
If Replace then begin
PageControl1.ActivePageIndex:=0;
Button1.Caption:=sReplace;
end
else begin
PageControl1.ActivePageIndex:=1;
Button1.Caption:=sFind;
end;
ShowModal;
end;
procedure TFindForm.FormShow(Sender: TObject);
begin
Edit1Change(Self);
cbCase.Left:=335-Canvas.TextWidth(cbCase.Caption);
Edit1.SetFocus;
Edit1.SelectAll;
end;
procedure TFindForm.Edit1Change(Sender: TObject);
begin
Button1.Enabled:=Edit1.Text<>'';
end;
end.