LCL: Change selection properly when setting TListBox.ItemIndex. Issue #34967, patch from Denis Kozlov.

git-svn-id: trunk@62312 -
This commit is contained in:
juha 2019-11-28 20:39:42 +00:00
parent 733d74e407
commit 065d7f1e25
2 changed files with 55 additions and 5 deletions

View File

@ -25,8 +25,14 @@ type
------------------------------------------------------------------------------}
procedure TCustomListBox.AssignCacheToItemData(const AIndex: Integer;
const AData: Pointer);
var
SelectItem: Boolean;
begin
if PCustomListBoxItemRecord(AData)^.Selected or (not MultiSelect and (FItemIndex = AIndex)) then
if MultiSelect then
SelectItem := PCustomListBoxItemRecord(AData)^.Selected
else
SelectItem := FItemIndex = AIndex;
if SelectItem then
begin
LockSelectionChange;
SendItemSelected(AIndex, True);
@ -90,6 +96,38 @@ begin
PCustomListBoxItemRecord(AData)^.Selected := Selected[AIndex];
end;
procedure TCustomListBox.ClearSelectedCache;
var
i: Integer;
CacheItems: TExtendedStringList;
begin
if not FCacheValid then
raise EInvalidOperation.Create('Cache is not valid.');
CacheItems := FItems as TExtendedStringList;
for i := 0 to CacheItems.Count - 1 do
PCustomListBoxItemRecord(CacheItems.Records[i])^.Selected := False;
end;
procedure TCustomListBox.SetSelectedCache(Index: Integer; IsSelected: Boolean);
var
CacheItems: TExtendedStringList;
begin
if not FCacheValid then
raise EInvalidOperation.Create('Cache is not valid.');
CacheItems := FItems as TExtendedStringList;
PCustomListBoxItemRecord(CacheItems.Records[Index])^.Selected := IsSelected;
end;
function TCustomListBox.GetSelectedCache(Index: Integer): Boolean;
var
CacheItems: TExtendedStringList;
begin
if not FCacheValid then
raise EInvalidOperation.Create('Cache is not valid.');
CacheItems := FItems as TExtendedStringList;
Result := PCustomListBoxItemRecord(CacheItems.Records[Index])^.Selected;
end;
{------------------------------------------------------------------------------
procedure TCustomListBox.InitializeWnd
------------------------------------------------------------------------------}
@ -392,7 +430,7 @@ begin
if HandleAllocated then
SendItemSelected(Index, Val)
else
PCustomListBoxItemRecord(GetCachedData(Index))^.Selected := Val;
SetSelectedCache(Index, Val);
end;
end;
@ -405,7 +443,7 @@ begin
if HandleAllocated then
Result := TWSCustomListBoxClass(WidgetSetClass).GetSelected(Self, Index)
else
Result := PCustomListBoxItemRecord(GetCachedData(Index))^.Selected;
Result := GetSelectedCache(Index);
end;
{------------------------------------------------------------------------------}
@ -610,8 +648,17 @@ begin
RaiseIndexOutOfBounds(AIndex);
if AIndex < 0 then AIndex := -1;
FItemIndex := AIndex;
if HandleAllocated and ([csLoading,csDestroying]*ComponentState=[]) then
SendItemIndex;
if HandleAllocated then
begin
if ([csLoading,csDestroying]*ComponentState=[]) then
SendItemIndex;
end
else
begin
ClearSelectedCache;
if FItemIndex >= 0 then
SetSelectedCache(FItemIndex, True);
end;
DoSelectionChange(false);
end;

View File

@ -540,6 +540,9 @@ type
procedure LMSelChange(var TheMessage); message LM_SelChange;
procedure WMLButtonUp(Var Message: TLMLButtonUp); message LM_LBUTTONUP;
procedure SendItemSelected(Index: integer; IsSelected: boolean);
procedure ClearSelectedCache;
procedure SetSelectedCache(Index: Integer; IsSelected: Boolean);
function GetSelectedCache(Index: Integer): Boolean;
protected
class procedure WSRegisterClass; override;
procedure AssignItemDataToCache(const AIndex: Integer; const AData: Pointer); virtual; // called to store item data while the handle isn't created