31 |
|
|
32 |
|
uses |
33 |
|
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, DbCtrls, |
34 |
< |
ExtCtrls, IBSQLParser, DB, StdCtrls; |
34 |
> |
ExtCtrls, IBSQLParser, DB, StdCtrls, IBCustomDataSet; |
35 |
|
|
36 |
|
type |
37 |
|
|
54 |
|
FOwner: TIBLookupComboEditBox; |
55 |
|
protected |
56 |
|
procedure ActiveChanged; override; |
57 |
– |
procedure DataEvent(Event: TDataEvent; Info: Ptrint); override; |
57 |
|
procedure RecordChanged(Field: TField); override; |
58 |
|
procedure UpdateData; override; |
59 |
|
public |
60 |
|
constructor Create(AOwner: TIBLookupComboEditBox); |
61 |
|
end; |
62 |
|
|
63 |
+ |
{ TIBLookupControlLink } |
64 |
+ |
|
65 |
+ |
TIBLookupControlLink = class(TIBControlLink) |
66 |
+ |
private |
67 |
+ |
FOwner: TIBLookupComboEditBox; |
68 |
+ |
protected |
69 |
+ |
procedure UpdateSQL(Sender: TObject); override; |
70 |
+ |
public |
71 |
+ |
constructor Create(AOwner: TIBLookupComboEditBox); |
72 |
+ |
end; |
73 |
+ |
|
74 |
|
|
75 |
|
{ TIBLookupComboEditBox } |
76 |
|
|
79 |
|
FCanAutoInsert: TCanAutoInsert; |
80 |
|
{ Private declarations } |
81 |
|
FDataLink: TIBLookupComboDataLink; |
82 |
+ |
FIBLookupControlLink: TIBLookupControlLink; |
83 |
|
FAutoComplete: boolean; |
84 |
|
FAutoInsert: boolean; |
85 |
|
FKeyPressInterval: integer; |
98 |
|
function GetListSource: TDataSource; |
99 |
|
function GetRelationNameQualifier: string; |
100 |
|
procedure HandleTimer(Sender: TObject); |
101 |
+ |
procedure IBControlLinkChanged; |
102 |
|
procedure ResetParser; |
103 |
|
procedure RecordChanged(Sender: TObject; aField: TField); |
104 |
|
procedure SetAutoCompleteText(AValue: TComboBoxAutoCompleteText); |
114 |
|
procedure DoEnter; override; |
115 |
|
procedure DoExit; override; |
116 |
|
procedure KeyUp(var Key: Word; Shift: TShiftState); override; |
117 |
+ |
procedure Loaded; override; |
118 |
+ |
procedure Notification(AComponent: TComponent; Operation: TOperation); override; |
119 |
|
procedure SetItemIndex(const Val: integer); override; |
120 |
+ |
function SQLSafe(aText: string): string; |
121 |
|
procedure UpdateShowing; override; |
122 |
+ |
|
123 |
|
public |
124 |
|
{ Public declarations } |
125 |
|
constructor Create(TheComponent: TComponent); override; |
134 |
|
property ItemHeight; |
135 |
|
property ItemWidth; |
136 |
|
property ListSource: TDataSource read GetListSource write SetListSource; |
137 |
< |
property KeyPressInterval: integer read FKeyPressInterval write FKeyPressInterval default 500; |
137 |
> |
property KeyPressInterval: integer read FKeyPressInterval write FKeyPressInterval default 200; |
138 |
|
property RelationName: string read FRelationName write FRelationName; |
139 |
|
property OnAutoInsert: TAutoInsert read FOnAutoInsert write FOnAutoInsert; |
140 |
|
property OnCanAutoInsert: TCanAutoInsert read FOnCanAutoInsert write FOnCanAutoInsert; |
143 |
|
|
144 |
|
implementation |
145 |
|
|
146 |
< |
uses IBQuery, IBCustomDataSet, LCLType, Variants, LCLProc; |
146 |
> |
uses IBQuery, LCLType, Variants, LCLProc; |
147 |
|
|
148 |
< |
{ TIBLookupComboDataLink } |
148 |
> |
{ TIBLookupControlLink } |
149 |
|
|
150 |
< |
procedure TIBLookupComboDataLink.ActiveChanged; |
150 |
> |
constructor TIBLookupControlLink.Create(AOwner: TIBLookupComboEditBox); |
151 |
|
begin |
152 |
< |
FOwner.ActiveChanged(self) |
152 |
> |
inherited Create; |
153 |
> |
FOwner := AOwner; |
154 |
|
end; |
155 |
|
|
156 |
< |
procedure TIBLookupComboDataLink.DataEvent(Event: TDataEvent; Info: Ptrint); |
156 |
> |
procedure TIBLookupControlLink.UpdateSQL(Sender: TObject); |
157 |
|
begin |
158 |
< |
{If we are not visible then avoid unnecessary work} |
159 |
< |
if not FOwner.Showing then Exit; |
158 |
> |
FOwner.UpdateSQL(self,TIBParserDataSet(Sender).Parser) |
159 |
> |
end; |
160 |
|
|
161 |
< |
if (Event = deCheckBrowseMode) and (Info = 1) and not DataSet.Active then |
162 |
< |
begin |
163 |
< |
if (DataSet is TIBDataSet) then |
164 |
< |
FOwner.UpdateSQL(self,TIBDataSet(DataSet).Parser) |
165 |
< |
else |
149 |
< |
if (DataSet is TIBQuery) then |
150 |
< |
FOwner.UpdateSQL(self,TIBQuery(DataSet).Parser) |
151 |
< |
end |
152 |
< |
else |
153 |
< |
inherited DataEvent(Event, Info); |
161 |
> |
{ TIBLookupComboDataLink } |
162 |
> |
|
163 |
> |
procedure TIBLookupComboDataLink.ActiveChanged; |
164 |
> |
begin |
165 |
> |
FOwner.ActiveChanged(self) |
166 |
|
end; |
167 |
|
|
168 |
|
procedure TIBLookupComboDataLink.RecordChanged(Field: TField); |
191 |
|
UpdateList |
192 |
|
end; |
193 |
|
|
194 |
+ |
procedure TIBLookupComboEditBox.IBControlLinkChanged; |
195 |
+ |
begin |
196 |
+ |
if (ListSource <> nil) and (ListSource.DataSet <> nil) and (ListSource.DataSet is TIBParserDataSet) then |
197 |
+ |
FIBLookupControlLink.IBDataSet := TIBCustomDataSet(ListSource.DataSet) |
198 |
+ |
else |
199 |
+ |
FIBLookupControlLink.IBDataSet := nil; |
200 |
+ |
end; |
201 |
+ |
|
202 |
|
function TIBLookupComboEditBox.GetListSource: TDataSource; |
203 |
|
begin |
204 |
|
Result := inherited ListSource; |
216 |
|
begin |
217 |
|
if not FInserting and not FUpdating then |
218 |
|
Application.QueueAsyncCall(@DoActiveChanged,0); |
219 |
+ |
IBControlLinkChanged; |
220 |
|
end; |
221 |
|
|
222 |
|
procedure TIBLookupComboEditBox.DoActiveChanged(Data: PtrInt); |
258 |
|
end; |
259 |
|
|
260 |
|
procedure TIBLookupComboEditBox.ResetParser; |
261 |
+ |
var curKeyValue: variant; |
262 |
|
begin |
263 |
|
if FFiltered then |
264 |
|
begin |
265 |
|
FFiltered := false; |
266 |
+ |
curKeyValue := KeyValue; |
267 |
+ |
Text := ''; {Ensure full list} |
268 |
|
UpdateList; |
269 |
+ |
KeyValue := curKeyValue; |
270 |
|
UpdateData(self); {Force Scroll} |
271 |
|
end; |
272 |
|
end; |
301 |
|
begin |
302 |
|
FDataLink.DataSource := AValue; |
303 |
|
inherited ListSource := AValue; |
304 |
+ |
IBControlLinkChanged; |
305 |
|
end; |
306 |
|
end; |
307 |
|
|
347 |
|
SelStart := iSelStart; |
348 |
|
SelLength := UTF8Length(Text); |
349 |
|
end; |
350 |
+ |
KeyValue := ListSource.DataSet.FieldByName(KeyField).AsVariant; |
351 |
|
end; |
352 |
|
end; |
353 |
|
finally |
363 |
|
if FFiltered then |
364 |
|
begin |
365 |
|
if cbactSearchCaseSensitive in AutoCompleteText then |
366 |
< |
Parser.Add2WhereClause(GetRelationNameQualifier + '"' + ListField + '" Like ''' + Text + '%''') |
366 |
> |
Parser.Add2WhereClause(GetRelationNameQualifier + '"' + ListField + '" Like ''' + |
367 |
> |
SQLSafe(Text) + '%''') |
368 |
|
else |
369 |
< |
Parser.Add2WhereClause(GetRelationNameQualifier + 'Upper("' + ListField + '") Like Upper(''' + Text + '%'')'); |
369 |
> |
Parser.Add2WhereClause(GetRelationNameQualifier + 'Upper("' + ListField + '") Like Upper(''' + |
370 |
> |
SQLSafe(Text) + '%'')'); |
371 |
|
|
372 |
|
end; |
373 |
|
if cbactSearchAscending in AutoCompleteText then |
381 |
|
|
382 |
|
procedure TIBLookupComboEditBox.HandleEnter(Data: PtrInt); |
383 |
|
begin |
384 |
< |
SelectAll |
384 |
> |
if AppDestroying in Application.Flags then Exit; |
385 |
> |
SelectAll |
386 |
|
end; |
387 |
|
|
388 |
|
procedure TIBLookupComboEditBox.UpdateLinkData(Sender: TObject); |
459 |
|
|
460 |
|
procedure TIBLookupComboEditBox.DoExit; |
461 |
|
begin |
462 |
+ |
if FTimer.Interval <> 0 then |
463 |
+ |
HandleTimer(nil); |
464 |
|
FExiting := true; |
465 |
|
try |
466 |
|
CheckAndInsert; |
491 |
|
(not (cbactEndOfLineComplete in AutoCompleteText) or (SelStart = UTF8Length(Text))) then |
492 |
|
FTimer.Interval := FKeyPressInterval |
493 |
|
else |
494 |
< |
FTimer.Interval := 0 |
494 |
> |
FTimer.Interval := 0; |
495 |
> |
end; |
496 |
> |
|
497 |
> |
procedure TIBLookupComboEditBox.Loaded; |
498 |
> |
begin |
499 |
> |
inherited Loaded; |
500 |
> |
IBControlLinkChanged; |
501 |
> |
end; |
502 |
> |
|
503 |
> |
procedure TIBLookupComboEditBox.Notification(AComponent: TComponent; |
504 |
> |
Operation: TOperation); |
505 |
> |
begin |
506 |
> |
inherited Notification(AComponent, Operation); |
507 |
> |
if (Operation = opRemove) and (AComponent = DataSource) then |
508 |
> |
ListSource := nil; |
509 |
|
end; |
510 |
|
|
511 |
|
procedure TIBLookupComboEditBox.SetItemIndex(const Val: integer); |
514 |
|
FLastKeyValue := KeyValue; |
515 |
|
end; |
516 |
|
|
517 |
+ |
function TIBLookupComboEditBox.SQLSafe(aText: string): string; |
518 |
+ |
var I: integer; |
519 |
+ |
begin |
520 |
+ |
Result := ''; |
521 |
+ |
for I := 1 to length(aText) do |
522 |
+ |
if aText[I] = '''' then |
523 |
+ |
Result := Result + '''''' |
524 |
+ |
else |
525 |
+ |
Result := Result + aText[I]; |
526 |
+ |
end; |
527 |
+ |
|
528 |
|
procedure TIBLookupComboEditBox.UpdateShowing; |
529 |
|
begin |
530 |
|
inherited UpdateShowing; |
536 |
|
begin |
537 |
|
inherited Create(TheComponent); |
538 |
|
FDataLink := TIBLookupComboDataLink.Create(self); |
539 |
< |
FKeyPressInterval := 500; |
539 |
> |
FIBLookupControlLink := TIBLookupControlLink.Create(self); |
540 |
> |
FKeyPressInterval := 200; |
541 |
|
FAutoComplete := true; |
542 |
|
FTimer := TTimer.Create(nil); |
543 |
|
FTimer.Interval := 0; |
548 |
|
destructor TIBLookupComboEditBox.Destroy; |
549 |
|
begin |
550 |
|
if assigned(FDataLink) then FDataLink.Free; |
551 |
+ |
if assigned(FIBLookupControlLink) then FIBLookupControlLink.Free; |
552 |
|
if assigned(FTimer) then FTimer.Free; |
553 |
|
inherited Destroy; |
554 |
|
end; |