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; procedure TCustomListBox.AssignCacheToItemData(const AIndex: Integer;
const AData: Pointer); const AData: Pointer);
var
SelectItem: Boolean;
begin 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 begin
LockSelectionChange; LockSelectionChange;
SendItemSelected(AIndex, True); SendItemSelected(AIndex, True);
@ -90,6 +96,38 @@ begin
PCustomListBoxItemRecord(AData)^.Selected := Selected[AIndex]; PCustomListBoxItemRecord(AData)^.Selected := Selected[AIndex];
end; 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 procedure TCustomListBox.InitializeWnd
------------------------------------------------------------------------------} ------------------------------------------------------------------------------}
@ -392,7 +430,7 @@ begin
if HandleAllocated then if HandleAllocated then
SendItemSelected(Index, Val) SendItemSelected(Index, Val)
else else
PCustomListBoxItemRecord(GetCachedData(Index))^.Selected := Val; SetSelectedCache(Index, Val);
end; end;
end; end;
@ -405,7 +443,7 @@ begin
if HandleAllocated then if HandleAllocated then
Result := TWSCustomListBoxClass(WidgetSetClass).GetSelected(Self, Index) Result := TWSCustomListBoxClass(WidgetSetClass).GetSelected(Self, Index)
else else
Result := PCustomListBoxItemRecord(GetCachedData(Index))^.Selected; Result := GetSelectedCache(Index);
end; end;
{------------------------------------------------------------------------------} {------------------------------------------------------------------------------}
@ -610,8 +648,17 @@ begin
RaiseIndexOutOfBounds(AIndex); RaiseIndexOutOfBounds(AIndex);
if AIndex < 0 then AIndex := -1; if AIndex < 0 then AIndex := -1;
FItemIndex := AIndex; FItemIndex := AIndex;
if HandleAllocated and ([csLoading,csDestroying]*ComponentState=[]) then if HandleAllocated then
SendItemIndex; begin
if ([csLoading,csDestroying]*ComponentState=[]) then
SendItemIndex;
end
else
begin
ClearSelectedCache;
if FItemIndex >= 0 then
SetSelectedCache(FItemIndex, True);
end;
DoSelectionChange(false); DoSelectionChange(false);
end; end;

View File

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