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

Comparing ibx/trunk/ibcontrols/IBLookupComboEditBox.pas (file contents):
Revision 29 by tony, Sat May 9 11:37:49 2015 UTC vs.
Revision 209 by tony, Wed Mar 14 12:48:51 2018 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 54 | Line 54 | type
54      FOwner: TIBLookupComboEditBox;
55    protected
56      procedure ActiveChanged; override;
57 +    procedure DataEvent(Event: TDataEvent; Info: Ptrint); override;
58      procedure RecordChanged(Field: TField); override;
59      procedure UpdateData; override;
60    public
# Line 92 | Line 93 | type
93      FUpdating: boolean;
94      FInserting: boolean;
95      FExiting: boolean;
96 +    FForceAutoComplete: boolean;
97 +    FInCheckAndInsert: boolean;
98      FLastKeyValue: variant;
99 +    FCurText: string;
100 +    FModified: boolean;
101      procedure DoActiveChanged(Data: PtrInt);
102      function GetAutoCompleteText: TComboBoxAutoCompleteText;
103      function GetListSource: TDataSource;
# Line 119 | Line 124 | type
124      procedure SetItemIndex(const Val: integer); override;
125      function SQLSafe(aText: string): string;
126      procedure UpdateShowing; override;
127 <
127 >    procedure UpdateData(Sender: TObject); override;
128    public
129      { Public declarations }
130      constructor Create(TheComponent: TComponent); override;
# Line 143 | Line 148 | type
148  
149   implementation
150  
151 < uses IBQuery, LCLType, Variants, LCLProc;
151 > uses IBQuery, LCLType, Variants, LCLProc, LazUTF8;
152  
153   { TIBLookupControlLink }
154  
# Line 165 | Line 170 | begin
170    FOwner.ActiveChanged(self)
171   end;
172  
173 + procedure TIBLookupComboDataLink.DataEvent(Event: TDataEvent; Info: Ptrint);
174 + begin
175 +  inherited DataEvent(Event, Info);
176 +  if Event = deLayoutChange then
177 +   FOwner.LookupCache := FOwner.LookupCache; {sneaky way of calling UpdateLookup}
178 + end;
179 +
180   procedure TIBLookupComboDataLink.RecordChanged(Field: TField);
181   begin
182    FOwner.RecordChanged(self,Field);
# Line 313 | Line 325 | procedure TIBLookupComboEditBox.UpdateLi
325   var
326    iSelStart: Integer; // char position
327    sCompleteText, sPrefixText, sResultText: string;
316  curText: string;
328   begin
329    if assigned(ListSource) and assigned(ListSource.DataSet) and (ListSource.DataSet is TIBCustomDataSet)
330       and ListSource.DataSet.Active then
331    begin
332 +    FCurText := Text;
333      FUpdating := true;
334      try
335           iSelStart := SelStart;//Capture original cursor position
336           if ((iSelStart < UTF8Length(Text)) and
337             (cbactEndOfLineComplete in AutoCompleteText)) then
338                  Exit;
327         curText := Text;
339           sPrefixText := UTF8Copy(Text, 1, iSelStart);
340           ListSource.DataSet.Active := false;
341           ListSource.DataSet.Active :=  true;
342 <         Text := curText;
343 <         if not FExiting and Focused and (Text <> '')then
342 >         Text := FCurText;
343 >         if not FExiting and (FForceAutoComplete or Focused) and (FCurText <> '')then
344           begin
345             if ListSource.DataSet.Active and (ListSource.DataSet.RecordCount > 0) then
346             begin
347               sCompleteText := ListSource.DataSet.FieldByName(ListField).AsString;
348 <             if (sCompleteText <> Text) then
348 >             if (sCompleteText <> FCurText) then
349               begin
350                 sResultText := sCompleteText;
351                 if ((cbactEndOfLineComplete in AutoCompleteText) and
# Line 347 | Line 358 | begin
358                 SelStart := iSelStart;
359                 SelLength := UTF8Length(Text);
360               end;
361 +             KeyValue := ListSource.DataSet.FieldByName(KeyField).AsVariant;
362 +           end
363 +           else
364 +           begin
365 +             SelStart := iSelStart;
366 +             SelLength := 0;
367             end;
368           end;
369      finally
370        FUpdating := false
371      end;
372 +    FModified := true;
373    end;
374   end;
375  
376   procedure TIBLookupComboEditBox.UpdateSQL(Sender: TObject;
377    Parser: TSelectSQLParser);
378   var FieldPosition: integer;
379 +    FilterText: string;
380   begin
381    if FFiltered then
382    begin
383 +    if FUpdating then
384 +      FilterText := FCurText
385 +    else
386 +      FilterText := Text;
387      if cbactSearchCaseSensitive in AutoCompleteText then
388        Parser.Add2WhereClause(GetRelationNameQualifier + '"' + ListField + '" Like ''' +
389 <                                  SQLSafe(Text) + '%''')
389 >                                  SQLSafe(FilterText) + '%''')
390      else
391 <      Parser.Add2WhereClause(GetRelationNameQualifier + 'Upper("' + ListField + '") Like Upper(''' +
392 <                                  SQLSafe(Text) + '%'')');
391 >      Parser.Add2WhereClause('Upper(' + GetRelationNameQualifier + '"' +  ListField + '") Like Upper(''' +
392 >                                  SQLSafe(FilterText) + '%'')');
393  
394 <  end;
395 <  if cbactSearchAscending in AutoCompleteText then
396 <  begin
397 <    FieldPosition := Parser.GetFieldPosition(ListField);
375 <    if FieldPosition = 0 then Exit;
394 >    if cbactSearchAscending in AutoCompleteText then
395 >    begin
396 >      FieldPosition := Parser.GetFieldPosition(ListField);
397 >      if FieldPosition = 0 then Exit;
398  
399 <    Parser.OrderByClause := IntToStr(FieldPosition) + ' ascending';
399 >      Parser.OrderByClause := IntToStr(FieldPosition) + ' ascending';
400 >    end;
401    end;
402   end;
403  
404   procedure TIBLookupComboEditBox.HandleEnter(Data: PtrInt);
405   begin
406 +  if AppDestroying in Application.Flags then Exit;
407     SelectAll
408   end;
409  
# Line 393 | Line 417 | procedure TIBLookupComboEditBox.CheckAnd
417   var Accept: boolean;
418      NewKeyValue: variant;
419   begin
420 <  if AutoInsert and (Text <> '') and assigned(ListSource) and assigned(ListSource.DataSet)
421 <     and ListSource.DataSet.Active and (ListSource.DataSet.RecordCount = 0) then
420 >  if FInCheckAndInsert then Exit;
421 >  FInCheckAndInsert := true;
422    try
423 <    {Is it OK to insert a new list member?}
424 <    Accept := true;
425 <    if assigned(FOnCanAutoInsert) then
426 <       OnCanAutoInsert(self,Text,Accept);
427 <    if not Accept then
428 <    begin
429 <      ResetParser;
430 <      Text := FOriginalTextValue;
431 <      SelectAll;
432 <      Exit;
433 <    end;
423 >       if AutoInsert and (Text <> '') and assigned(ListSource) and assigned(ListSource.DataSet)
424 >          and ListSource.DataSet.Active and (ListSource.DataSet.RecordCount = 0) then
425 >       try
426 >         {Is it OK to insert a new list member?}
427 >         Accept := true;
428 >         if assigned(FOnCanAutoInsert) then
429 >            OnCanAutoInsert(self,Text,Accept);
430 >         if not Accept then
431 >         begin
432 >           ResetParser;
433 >           Text := FOriginalTextValue;
434 >           SelectAll;
435 >           Exit;
436 >         end;
437  
438 <    FInserting := true;
439 <    try
440 <      {New Value}
441 <      FFiltered := false;
442 <      if assigned(FOnAutoInsert) then
443 <      begin
444 <        {In an OnAutoInsert handler, the client is expected to insert the new
445 <         row into the List DataSet and to set the KeyValue property to the
446 <         value of the primary key of the new row.}
447 <        OnAutoInsert(self,Text,NewKeyValue);
448 <      end
449 <      else
450 <      begin
451 <        ListSource.DataSet.Append;
452 <        {The new KeyValue should be determined by an external generator or
453 <         in the "OnInsert" handler. If it is the same as the ListField, then
454 <         it will be set from the UpdateLinkData method}
455 <        try
456 <          ListSource.DataSet.Post;
457 <        except
458 <          ListSource.DataSet.Cancel;
459 <          raise;
460 <        end;
461 <        NewKeyValue := ListSource.DataSet.FieldByName(KeyField).AsVariant;
462 <      end;
463 <      Text := ''; {Ensure full list}
464 <      UpdateList;
465 <      KeyValue := NewKeyValue;
466 <      UpdateData(nil); {Force sync with DataField}
467 <    finally
468 <      FInserting := false
469 <    end;
470 <  except
471 <    Text := FOriginalTextValue;
472 <    ResetParser;
473 <    raise;
438 >         FInserting := true;
439 >         try
440 >           {New Value}
441 >           FFiltered := false;
442 >           if assigned(FOnAutoInsert) then
443 >           begin
444 >             {In an OnAutoInsert handler, the client is expected to insert the new
445 >              row into the List DataSet and to set the KeyValue property to the
446 >              value of the primary key of the new row.}
447 >             OnAutoInsert(self,Text,NewKeyValue);
448 >           end
449 >           else
450 >           begin
451 >             ListSource.DataSet.Append;
452 >             {The new KeyValue should be determined by an external generator or
453 >              in the "OnInsert" handler. If it is the same as the ListField, then
454 >              it will be set from the UpdateLinkData method}
455 >             try
456 >               ListSource.DataSet.Post;
457 >             except
458 >               ListSource.DataSet.Cancel;
459 >               raise;
460 >             end;
461 >             NewKeyValue := ListSource.DataSet.FieldByName(KeyField).AsVariant;
462 >           end;
463 >           Text := ''; {Ensure full list}
464 >           UpdateList;
465 >           KeyValue := NewKeyValue;
466 >           UpdateData(nil); {Force sync with DataField}
467 >         finally
468 >           FInserting := false
469 >         end;
470 >       except
471 >         Text := FOriginalTextValue;
472 >         ResetParser;
473 >         raise;
474 >       end;
475 >  finally
476 >    FInCheckAndInsert := false
477    end;
478   end;
479  
# Line 457 | Line 487 | end;
487  
488   procedure TIBLookupComboEditBox.DoExit;
489   begin
490 +  if FTimer.Interval <> 0 then
491 +    HandleTimer(nil);
492    FExiting := true;
493    try
494      CheckAndInsert;
# Line 482 | Line 514 | begin
514      SelectAll;
515    end
516    else
517 <  if (IsEditableTextKey(Key) or (Key = VK_BACK))
486 <     and AutoComplete and (Style <> csDropDownList) and
487 <     (not (cbactEndOfLineComplete in AutoCompleteText) or (SelStart = UTF8Length(Text))) then
488 <    FTimer.Interval := FKeyPressInterval
489 <  else
517 >  begin
518      FTimer.Interval := 0;
519 +    if (IsEditableTextKey(Key) or (Key = VK_BACK))
520 +       and AutoComplete and (Style <> csDropDownList) and
521 +       (not (cbactEndOfLineComplete in AutoCompleteText) or (SelStart = UTF8Length(Text))) then
522 +      FTimer.Interval := FKeyPressInterval;
523 +  end;
524   end;
525  
526   procedure TIBLookupComboEditBox.Loaded;
# Line 528 | Line 561 | begin
561      ActiveChanged(nil);
562   end;
563  
564 + procedure TIBLookupComboEditBox.UpdateData(Sender: TObject);
565 + begin
566 +  inherited UpdateData(Sender);
567 +  FModified := false;
568 + end;
569 +
570   constructor TIBLookupComboEditBox.Create(TheComponent: TComponent);
571   begin
572    inherited Create(TheComponent);
# Line 546 | Line 585 | begin
585    if assigned(FDataLink) then FDataLink.Free;
586    if assigned(FIBLookupControlLink) then FIBLookupControlLink.Free;
587    if assigned(FTimer) then FTimer.Free;
588 +  Application.RemoveAsyncCalls(self);
589    inherited Destroy;
590   end;
591  
592   procedure TIBLookupComboEditBox.EditingDone;
593   begin
594 +  FForceAutoComplete := true;
595 +  try
596 +  if FTimer.Interval <> 0 then
597 +    HandleTimer(nil);
598 +  finally
599 +    FForceAutoComplete := false;
600 +  end;
601    CheckAndInsert;
602 +  if FModified then
603 +    Change; {ensure Update}
604    inherited EditingDone;
605   end;
606  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines