ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/ibcontrols/IBLookupComboEditBox.pas
(Generate patch)

Comparing ibx/trunk/ibcontrols/IBLookupComboEditBox.pas (file contents):
Revision 31 by tony, Tue Jul 14 15:31:25 2015 UTC vs.
Revision 64 by tony, Thu Jun 29 11:11:22 2017 UTC

# Line 31 | Line 31 | interface
31  
32   uses
33    Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, DbCtrls,
34 <  ExtCtrls, IBSQLParser, DB, StdCtrls, IBCustomDataSet;
34 >  ExtCtrls, IBSQLParser, DB, StdCtrls, IBCustomDataSet, LCLVersion;
35  
36   type
37  
# Line 92 | Line 92 | type
92      FUpdating: boolean;
93      FInserting: boolean;
94      FExiting: boolean;
95 +    FForceAutoComplete: boolean;
96 +    FInCheckAndInsert: boolean;
97      FLastKeyValue: variant;
98 +    FCurText: string;
99      procedure DoActiveChanged(Data: PtrInt);
100      function GetAutoCompleteText: TComboBoxAutoCompleteText;
101      function GetListSource: TDataSource;
# Line 143 | Line 146 | type
146  
147   implementation
148  
149 < uses IBQuery, LCLType, Variants, LCLProc;
149 > uses IBQuery, LCLType, Variants, LCLProc, LazUTF8;
150  
151   { TIBLookupControlLink }
152  
# Line 313 | Line 316 | procedure TIBLookupComboEditBox.UpdateLi
316   var
317    iSelStart: Integer; // char position
318    sCompleteText, sPrefixText, sResultText: string;
316  curText: string;
319   begin
320    if assigned(ListSource) and assigned(ListSource.DataSet) and (ListSource.DataSet is TIBCustomDataSet)
321       and ListSource.DataSet.Active then
322    begin
323 +    FCurText := Text;
324      FUpdating := true;
325      try
326           iSelStart := SelStart;//Capture original cursor position
327           if ((iSelStart < UTF8Length(Text)) and
328             (cbactEndOfLineComplete in AutoCompleteText)) then
329                  Exit;
327         curText := Text;
330           sPrefixText := UTF8Copy(Text, 1, iSelStart);
331           ListSource.DataSet.Active := false;
332           ListSource.DataSet.Active :=  true;
333 <         Text := curText;
334 <         if not FExiting and Focused and (Text <> '')then
333 >         Text := FCurText;
334 >         if not FExiting and (FForceAutoComplete or Focused) and (FCurText <> '')then
335           begin
336             if ListSource.DataSet.Active and (ListSource.DataSet.RecordCount > 0) then
337             begin
338               sCompleteText := ListSource.DataSet.FieldByName(ListField).AsString;
339 <             if (sCompleteText <> Text) then
339 >             if (sCompleteText <> FCurText) then
340               begin
341                 sResultText := sCompleteText;
342                 if ((cbactEndOfLineComplete in AutoCompleteText) and
# Line 359 | Line 361 | end;
361   procedure TIBLookupComboEditBox.UpdateSQL(Sender: TObject;
362    Parser: TSelectSQLParser);
363   var FieldPosition: integer;
364 +    FilterText: string;
365   begin
366    if FFiltered then
367    begin
368 +    if FUpdating then
369 +      FilterText := FCurText
370 +    else
371 +      FilterText := Text;
372      if cbactSearchCaseSensitive in AutoCompleteText then
373        Parser.Add2WhereClause(GetRelationNameQualifier + '"' + ListField + '" Like ''' +
374 <                                  SQLSafe(Text) + '%''')
374 >                                  SQLSafe(FilterText) + '%''')
375      else
376 <      Parser.Add2WhereClause(GetRelationNameQualifier + 'Upper("' + ListField + '") Like Upper(''' +
377 <                                  SQLSafe(Text) + '%'')');
376 >      Parser.Add2WhereClause('Upper(' + GetRelationNameQualifier + '"' +  ListField + '") Like Upper(''' +
377 >                                  SQLSafe(FilterText) + '%'')');
378  
379 <  end;
380 <  if cbactSearchAscending in AutoCompleteText then
381 <  begin
382 <    FieldPosition := Parser.GetFieldPosition(ListField);
376 <    if FieldPosition = 0 then Exit;
379 >    if cbactSearchAscending in AutoCompleteText then
380 >    begin
381 >      FieldPosition := Parser.GetFieldPosition(ListField);
382 >      if FieldPosition = 0 then Exit;
383  
384 <    Parser.OrderByClause := IntToStr(FieldPosition) + ' ascending';
384 >      Parser.OrderByClause := IntToStr(FieldPosition) + ' ascending';
385 >    end;
386    end;
387   end;
388  
# Line 395 | Line 402 | procedure TIBLookupComboEditBox.CheckAnd
402   var Accept: boolean;
403      NewKeyValue: variant;
404   begin
405 <  if AutoInsert and (Text <> '') and assigned(ListSource) and assigned(ListSource.DataSet)
406 <     and ListSource.DataSet.Active and (ListSource.DataSet.RecordCount = 0) then
405 >  if FInCheckAndInsert then Exit;
406 >  FInCheckAndInsert := true;
407    try
408 <    {Is it OK to insert a new list member?}
409 <    Accept := true;
410 <    if assigned(FOnCanAutoInsert) then
411 <       OnCanAutoInsert(self,Text,Accept);
412 <    if not Accept then
413 <    begin
414 <      ResetParser;
415 <      Text := FOriginalTextValue;
416 <      SelectAll;
417 <      Exit;
418 <    end;
408 >       if AutoInsert and (Text <> '') and assigned(ListSource) and assigned(ListSource.DataSet)
409 >          and ListSource.DataSet.Active and (ListSource.DataSet.RecordCount = 0) then
410 >       try
411 >         {Is it OK to insert a new list member?}
412 >         Accept := true;
413 >         if assigned(FOnCanAutoInsert) then
414 >            OnCanAutoInsert(self,Text,Accept);
415 >         if not Accept then
416 >         begin
417 >           ResetParser;
418 >           Text := FOriginalTextValue;
419 >           SelectAll;
420 >           Exit;
421 >         end;
422  
423 <    FInserting := true;
424 <    try
425 <      {New Value}
426 <      FFiltered := false;
427 <      if assigned(FOnAutoInsert) then
428 <      begin
429 <        {In an OnAutoInsert handler, the client is expected to insert the new
430 <         row into the List DataSet and to set the KeyValue property to the
431 <         value of the primary key of the new row.}
432 <        OnAutoInsert(self,Text,NewKeyValue);
433 <      end
434 <      else
435 <      begin
436 <        ListSource.DataSet.Append;
437 <        {The new KeyValue should be determined by an external generator or
438 <         in the "OnInsert" handler. If it is the same as the ListField, then
439 <         it will be set from the UpdateLinkData method}
440 <        try
441 <          ListSource.DataSet.Post;
442 <        except
443 <          ListSource.DataSet.Cancel;
444 <          raise;
445 <        end;
446 <        NewKeyValue := ListSource.DataSet.FieldByName(KeyField).AsVariant;
447 <      end;
448 <      Text := ''; {Ensure full list}
449 <      UpdateList;
450 <      KeyValue := NewKeyValue;
451 <      UpdateData(nil); {Force sync with DataField}
452 <    finally
453 <      FInserting := false
454 <    end;
455 <  except
456 <    Text := FOriginalTextValue;
457 <    ResetParser;
458 <    raise;
423 >         FInserting := true;
424 >         try
425 >           {New Value}
426 >           FFiltered := false;
427 >           if assigned(FOnAutoInsert) then
428 >           begin
429 >             {In an OnAutoInsert handler, the client is expected to insert the new
430 >              row into the List DataSet and to set the KeyValue property to the
431 >              value of the primary key of the new row.}
432 >             OnAutoInsert(self,Text,NewKeyValue);
433 >           end
434 >           else
435 >           begin
436 >             ListSource.DataSet.Append;
437 >             {The new KeyValue should be determined by an external generator or
438 >              in the "OnInsert" handler. If it is the same as the ListField, then
439 >              it will be set from the UpdateLinkData method}
440 >             try
441 >               ListSource.DataSet.Post;
442 >             except
443 >               ListSource.DataSet.Cancel;
444 >               raise;
445 >             end;
446 >             NewKeyValue := ListSource.DataSet.FieldByName(KeyField).AsVariant;
447 >           end;
448 >           Text := ''; {Ensure full list}
449 >           UpdateList;
450 >           KeyValue := NewKeyValue;
451 >           UpdateData(nil); {Force sync with DataField}
452 >         finally
453 >           FInserting := false
454 >         end;
455 >       except
456 >         Text := FOriginalTextValue;
457 >         ResetParser;
458 >         raise;
459 >       end;
460 >  finally
461 >    FInCheckAndInsert := false
462    end;
463   end;
464  
# Line 486 | Line 499 | begin
499      SelectAll;
500    end
501    else
502 <  if (IsEditableTextKey(Key) or (Key = VK_BACK))
490 <     and AutoComplete and (Style <> csDropDownList) and
491 <     (not (cbactEndOfLineComplete in AutoCompleteText) or (SelStart = UTF8Length(Text))) then
492 <    FTimer.Interval := FKeyPressInterval
493 <  else
502 >  begin
503      FTimer.Interval := 0;
504 +    if (IsEditableTextKey(Key) or (Key = VK_BACK))
505 +       and AutoComplete and (Style <> csDropDownList) and
506 +       (not (cbactEndOfLineComplete in AutoCompleteText) or (SelStart = UTF8Length(Text))) then
507 +      FTimer.Interval := FKeyPressInterval;
508 +  end;
509   end;
510  
511   procedure TIBLookupComboEditBox.Loaded;
# Line 555 | Line 569 | end;
569  
570   procedure TIBLookupComboEditBox.EditingDone;
571   begin
572 +  FForceAutoComplete := true;
573 +  try
574 +  if FTimer.Interval <> 0 then
575 +    HandleTimer(nil);
576 +  finally
577 +    FForceAutoComplete := false;
578 +  end;
579    CheckAndInsert;
580    inherited EditingDone;
581   end;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines