-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathODDBFind.pas
420 lines (384 loc) · 11.9 KB
/
ODDBFind.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
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
unit ODDBFind;
{
TODFindMenu Component
My Power Station Technology (Pty) Ltd - was Orbital Decisions
P.O.Box 1080, Milnerton 7435, South Africa
http://www.orbital.co.za/text/prodlist.htm
Copyright (c) 1998-2019
Use at your own risk!
}
interface
uses
Windows, Messages, SysUtils, Classes, VCL.Graphics, VCL.Controls, VCL.Forms, VCL.Dialogs,
VCL.Menus, DB, VCL.StdCtrls, VCL.Buttons, VCL.ExtCtrls, VCL.DBCtrls, VCL.DBGrids;
type
TODDataLink = class(TDataLink)
private
FOnActiveChange: TNotifyEvent;
function GetDataSet: TDataSet;
protected
procedure ActiveChanged; override;
public
property DataSet: TDataSet read GetDataSet;
property OnActiveChange: TNotifyEvent read FOnActiveChange write FOnActiveChange;
end;
TODFindForm = class(TForm)
Bevel1: TBevel;
FieldCombo: TComboBox;
Label1: TLabel;
Label2: TLabel;
OKBtn: TBitBtn;
CancelBtn: TBitBtn;
ValueEdit: TEdit;
procedure FormShow(Sender: TObject);
procedure FieldComboChange(Sender: TObject);
end;
TODFindMenu = class(TPopupMenu)
private
FUseCount: Integer;
FDataLink: TODDataLink;
FDataField: string;
FFindForm: TODFindForm;
FFindItem, FFindNextItem: TMenuItem;
FFindCaption, FFindNextCaption: string;
FFindShortCut, FFindNextShortCut: TShortCut;
FAutoField, FAutoSource, FShowPrev, FFindNext: Boolean;
FOptions: TLocateOptions;
FOnFind, FOnFindNext: TNotifyEvent;
function GetAbout: string;
procedure SetAbout(Value: string);
function GetDataSource: TDataSource;
procedure SetDataSource(Value: TDataSource);
procedure SetDataField(Value: string);
procedure SetFindShortCut(Value: TShortCut);
procedure SetFindNextShortCut(Value: TShortCut);
protected
procedure DoUpdate(Sender: TObject);
// procedure DoFind(Sender: TObject);
// procedure DoFindNext(Sender: TObject);
procedure UpdateFields;
function FindRecord(FromStart: Boolean): Boolean;
public
procedure DoFind(Sender: TObject);
procedure DoFindNext(Sender: TObject);
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
published
property About: string read GetAbout write SetAbout stored False;
property DataSource: TDataSource read GetDataSource write SetDataSource;
property DataField: string read FDataField write SetDataField;
property FindCaption: string read FFindCaption write FFindCaption;
property FindNextCaption: string read FFindNextCaption write FFindNextCaption;
property FindShortCut: TShortCut read FFindShortCut write SetFindShortCut;
property FindNextShortCut: TShortCut read FFindNextShortCut write SetFindNextShortCut;
property Options: TLocateOptions read FOptions write FOptions default [loCaseInsensitive, loPartialKey];
property AutoField: Boolean read FAutoField write FAutoField default True;
property AutoSource: Boolean read FAutoSource write FAutoSource default False;
property ShowPrev: Boolean read FShowPrev write FShowPrev default False;
property OnFind: TNotifyEvent read FOnFind write FOnFind;
property OnFindNext: TNotifyEvent read FOnFindNext write FOnFindNext;
end;
var
ODFindForm: TODFindForm;
implementation
{$R *.DFM}
const
ODVersion = '5.0.0';
DemoVersion = False;
HintTrigger = 5;
//TODDataLink
procedure TODDataLink.ActiveChanged;
begin
inherited ActiveChanged;
if Assigned(FOnActiveChange) then
FOnActiveChange(Self);
end;
function TODDataLink.GetDataSet: TDataSet;
begin
if DataSource <> nil then
Result := DataSource.DataSet
else
Result := nil;
end;
//TODFindForm
procedure TODFindForm.FormShow(Sender: TObject);
begin
OKBtn.Enabled := FieldCombo.ItemIndex > -1;
if OKBtn.Enabled then
ValueEdit.SetFocus
else
FieldCombo.SetFocus;
end;
procedure TODFindForm.FieldComboChange(Sender: TObject);
begin
OKBtn.Enabled := FieldCombo.ItemIndex > -1;
ValueEdit.Text := '';
ValueEdit.SetFocus;
end;
//TODFindMenu
constructor TODFindMenu.Create(AOwner: TComponent);
function InsertItem(ACaption: string; AHandler: TNotifyEvent): TMenuItem;
begin
Result := TMenuItem.Create(Self);
Result.Caption := ACaption;
Result.OnClick := AHandler;
Items.Insert(0, Result);
end;
begin
inherited Create(AOwner);
FUseCount := 0;
FDataLink := TODDataLink.Create;
// FDataLink.Control := Self;
FDataLink.OnActiveChange := DoUpdate;
FFindCaption := '&Find...';
FFindNextCaption := 'Find &Next';
FAutoField := True;
FOptions := [loCaseInsensitive, loPartialKey];
if not (csDesigning in ComponentState) then
begin
FFindForm := TODFindForm.Create(Self);
if Items.Count > 0 then InsertItem('-', nil);
FFindNextItem := InsertItem(FFindNextCaption, DoFindNext);
FFindNextItem.ShortCut := FFindNextShortCut;
FFindItem := InsertItem(FFindCaption, DoFind);
FFindItem.ShortCut := FFindShortCut;
end;
// SetEditMenu(FEditMenu);
end;
destructor TODFindMenu.Destroy;
begin
FDataLink.Free;
inherited Destroy;
end;
procedure TODFindMenu.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = FDataLink.DataSource) then
SetDataSource(nil);
end;
procedure TODFindMenu.DoUpdate(Sender: TObject);
begin
if csDesigning in ComponentState then Exit;
FFindItem.Enabled :=
(FDataLink.DataSet <> nil) and FDataLink.DataSet.Active;
if not FFindItem.Enabled then
FFindNextItem.Enabled := False;
UpdateFields;
end;
procedure TODFindMenu.UpdateFields;
var
ix: Integer;
begin
// if csDesigning in ComponentState then Exit;
FFindForm.FieldCombo.Items.Clear;
if FDataLink.DataSet <> nil then
with FDataLink.DataSet do
for ix := 0 to FieldCount-1 do
with Fields[ix] do
if Visible then
FFindForm.FieldCombo.Items.AddObject(DisplayLabel, Pointer(Index));
end;
procedure TODFindMenu.DoFind(Sender: TObject);
var
st: string;
df: TField;
ix: Integer;
begin
if DemoVersion then
begin
Inc(FUseCount);
if FUseCount = HintTrigger then
begin
MessageDlg('Orbital Decisions FindMenu Component - Unregistered version',
mtInformation, [mbOK], 0);
FUseCount := 0;
end;
end;
if FFindForm = nil then
FFindForm := TODFindForm.Create(Application);
if FFindForm.FieldCombo.Items.Count = 0 then
UpdateFields;
if not (csDesigning in ComponentState) then
begin
FFindNextItem.Enabled := False;
if PopupComponent = nil then //likely invoked by Popup or shortcut key
if Owner is TForm then
PopupComponent := TForm(Owner).ActiveControl;
if FAutoSource then
if PopupComponent is TDBEdit then
DataSource := TDBEdit(PopupComponent).DataSource
else if PopupComponent is TDBComboBox then
DataSource := TDBComboBox(PopupComponent).DataSource
else if PopupComponent is TDBListBox then
DataSource := TDBListBox(PopupComponent).DataSource
else if PopupComponent is TDBLookupComboBox then
DataSource := TDBLookupComboBox(PopupComponent).DataSource
else if PopupComponent is TDBLookupListBox then
DataSource := TDBLookupListBox(PopupComponent).DataSource
else if PopupComponent is TDBGrid then
DataSource := TDBGrid(PopupComponent).DataSource;
if (DataField = '') and FAutoField then
begin
if PopupComponent is TDBEdit then
st := TDBEdit(PopupComponent).DataField
else if PopupComponent is TDBComboBox then
st := TDBComboBox(PopupComponent).DataField
else if PopupComponent is TDBListBox then
st := TDBListBox(PopupComponent).DataField
else if PopupComponent is TDBLookupComboBox then
st := TDBLookupComboBox(PopupComponent).DataField
else if PopupComponent is TDBLookupListBox then
st := TDBLookupListBox(PopupComponent).DataField
else if (PopupComponent is TDBGrid) and
(TDBGrid(PopupComponent).SelectedField <> nil) then
st := TDBGrid(PopupComponent).SelectedField.FieldName;
end
else st := DataField;
df := FDataLink.DataSet.FindField(st);
with FFindForm.FieldCombo do
if df <> nil then
ItemIndex := Items.IndexOf(df.DisplayLabel)
else
ItemIndex := -1;
end;
if not FShowPrev then
FFindForm.ValueEdit.Text := '';
if FFindForm.ShowModal = ID_OK then
begin
ix := Longint(FFindForm.FieldCombo.Items.Objects[
FFindForm.FieldCombo.ItemIndex]);
if DataSource.DataSet.Fields[ix].FieldKind = fkData then
FFindNext := FDataLink.DataSet.Locate(
DataSource.DataSet.Fields[ix].FieldName,
FFindForm.ValueEdit.Text, FOptions)
else
FFindNext := FindRecord(True); //do sequential search on calc or lookup fields
if not (csDesigning in ComponentState) then
begin
FFindNextItem.Enabled := FFindNext;
if Assigned(FOnFind) then FOnFind(Self);
end;
if not FFindNext then ShowMessage('No matches found.');
end;
end;
procedure TODFindMenu.DoFindNext(Sender: TObject);
begin
if not FFindNext then Exit;
FFindNext := FindRecord(False);
if not FFindNext then
ShowMessage('No more matches found.');
if not (csDesigning in ComponentState) then
begin
FFindNextItem.Enabled := FFindNext;
if Assigned(FOnFindNext) then FOnFindNext(Self);
end;
end;
function TODFindMenu.FindRecord(FromStart: Boolean): Boolean;
var
bm: TBookmark;
s1, s2: string;
begin
Result := False;
with FDataLink.DataSet do
begin
bm := GetBookmark;
Screen.Cursor := crHourglass;
DisableControls;
try
if FromStart then First else Next;
while not EOF do
begin
s1 := FFindForm.ValueEdit.Text;
with FFindForm.FieldCombo do
s2 := FDataLink.DataSet.Fields[Longint(Items.Objects[ItemIndex])].AsString;
if loCaseInsensitive in FOptions then
begin
s1 := UpperCase(s1);
s2 := UpperCase(s2);
end;
if loPartialKey in FOptions then
Result := Pos(s1, s2) = 1
else
Result := s1 = s2;
if Result then Break else Next;
end;
if not Result then GotoBookmark(bm);
finally
EnableControls;
Screen.Cursor := crDefault;
FreeBookmark(bm);
end;
end;
end;
function TODFindMenu.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TODFindMenu.SetDataSource(Value: TDataSource);
begin
FDatalink.DataSource := Value;
if not (csDesigning in ComponentState) then
begin
FFindItem.Enabled := (Value <> nil) and
(Value.DataSet <> nil) and Value.DataSet.Active;
FFindNextItem.Enabled := False;
end;
if Value <> nil then
Value.FreeNotification(Self);
end;
procedure TODFindMenu.SetDataField(Value: string);
begin
if FDataField <> Value then
begin
FDataField := Value;
if not (csDesigning in ComponentState) then
begin
FFindNextItem.Enabled := False;
FFindForm.ValueEdit.Text := '';
end;
end;
end;
procedure TODFindMenu.SetFindShortCut(Value: TShortCut);
begin
FFindShortCut := Value;
if FFindItem <> nil then
FFindItem.ShortCut := Value;
end;
procedure TODFindMenu.SetFindNextShortCut(Value: TShortCut);
begin
FFindNextShortCut := Value;
if FFindNextItem <> nil then
FFindNextItem.ShortCut := Value;
end;
{
procedure TODFindMenu.SetEditMenu(Value: Boolean);
function InsertItem(ACaption: string; AHandler: TNotifyEvent): TMenuItem;
begin
Result := TMenuItem.Create(Self);
Result.Caption := ACaption;
Result.OnClick := AHandler;
Items.Insert(0, Result);
end;
begin
FDefaultMenu := Value;
if FDefaultMenu then
begin
InsertItem(
InsertItem('-', nil);
end;
end;
}
function TODFindMenu.GetAbout: string;
begin
Result := 'Version ' + ODVersion;
if DemoVersion then
Result := Result + ' demo';
end;
procedure TODFindMenu.SetAbout(Value: string);
begin
{do nothing}
end;
//TODFindMenuEditor
end.