Partly related to issue
 https://bugs.freepascal.org/view.php?id=38565

I changed function TListItem.GetState to use enum TListItemState directly
instead of casting it to an integer.
It allows to remove many ugly typecasts.
I don't see any problems caused by it. None of the properties are
published, thus component streaming is not a problem.
However I don't know TListView very well, somebody may spot a potential
problem.
So, is it safe to apply? Please see the attached patch.

Juha
From 2807655b1928a9caff738a357b63f3c07aba4077 Mon Sep 17 00:00:00 2001
From: Juha <juha.mannine...@gmail.com>
Date: Sun, 21 Mar 2021 10:28:57 +0200
Subject: [PATCH] State index TListItemState.

---
 lcl/comctrls.pp          | 12 ++++++------
 lcl/include/listitem.inc | 19 +++++++------------
 2 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/lcl/comctrls.pp b/lcl/comctrls.pp
index 8e3115455..f797e52a8 100644
--- a/lcl/comctrls.pp
+++ b/lcl/comctrls.pp
@@ -1014,7 +1014,7 @@ type
     function GetLeft: Integer;
     function GetListView: TCustomListView;
     function GetPosition: TPoint;
-    function GetState(const ALisOrd: Integer): Boolean;
+    function GetState(AState: TListItemState): Boolean;
     function GetImageIndex: TImageIndex; virtual;
     function GetIndex: Integer; virtual;
     function GetStateIndex: TImageIndex; virtual;
@@ -1029,7 +1029,7 @@ type
     procedure WSUpdateState;
 
     procedure SetChecked(AValue: Boolean);
-    procedure SetState(const ALisOrd: Integer; const AIsSet: Boolean);
+    procedure SetState(AState: TListItemState; AIsSet: Boolean);
     procedure SetData(const AValue: Pointer);
     procedure SetImageIndex(const AValue: TImageIndex); virtual;
     procedure SetLeft(Value: Integer);
@@ -1058,17 +1058,17 @@ type
 
     property Caption : String read GetCaption write SetCaption;
     property Checked : Boolean read GetChecked write SetChecked;
-    property Cut: Boolean index Ord(lisCut) read GetState write SetState;
+    property Cut: Boolean index lisCut read GetState write SetState;
     property Data: Pointer read FData write SetData;
-    property DropTarget: Boolean index Ord(lisDropTarget) read GetState write SetState;
-    property Focused: Boolean index Ord(lisFocused) read GetState write SetState;
+    property DropTarget: Boolean index lisDropTarget read GetState write SetState;
+    property Focused: Boolean index lisFocused read GetState write SetState;
     property Index: Integer read GetIndex;
     property ImageIndex: TImageIndex read GetImageIndex write SetImageIndex default -1;
     property Left: Integer read GetLeft write SetLeft;
     property ListView: TCustomListView read GetListView;
     property Owner: TListItems read FOwner;
     property Position: TPoint read GetPosition write SetPosition;
-    property Selected: Boolean index Ord(lisSelected) read GetState write SetState;
+    property Selected: Boolean index lisSelected read GetState write SetState;
     property StateIndex: TImageIndex read GetStateIndex write SetStateIndex;
     property SubItems: TStrings read GetSubItems write SetSubItems;
     property SubItemImages[const AIndex: Integer]: Integer read GetSubItemImages write SetSubItemImages;
diff --git a/lcl/include/listitem.inc b/lcl/include/listitem.inc
index 0b003e7a3..0c7e058ee 100644
--- a/lcl/include/listitem.inc
+++ b/lcl/include/listitem.inc
@@ -542,12 +542,10 @@ end;
 {------------------------------------------------------------------------------}
 {   TListItem GetState                                                         }
 {------------------------------------------------------------------------------}
-function TListItem.GetState(const ALisOrd: Integer): Boolean;
+function TListItem.GetState(AState: TListItemState): Boolean;
 var
-  AState: TListItemState;
   LV: TCustomListView;
 begin
-  AState := TListItemState(ALisOrd);
   LV := FOwner.FOwner;
 
   if WSUpdateAllowed
@@ -562,7 +560,7 @@ begin
 end;
 
 {------------------------------------------------------------------------------}
-{   TListItem GetStates                                                         }
+{   TListItem GetStates                                                        }
 {------------------------------------------------------------------------------}
 function TListItem.GetStates: TListItemStates;
 var
@@ -577,10 +575,10 @@ begin
   else
   begin
     Result := [];
-    if GetState(Ord(lisCut)) then ;
-    if GetState(Ord(lisDropTarget)) then ;
-    if GetState(Ord(lisSelected)) then ;
-    if GetState(Ord(lisFocused)) then ;
+    if GetState(lisCut) then ;
+    if GetState(lisDropTarget) then ;
+    if GetState(lisSelected) then ;
+    if GetState(lisFocused) then ;
     Result := FStates;
   end;
 end;
@@ -746,13 +744,10 @@ end;
 {------------------------------------------------------------------------------}
 {   TListItem SetState                                                         }
 {------------------------------------------------------------------------------}
-procedure TListItem.SetState(const ALisOrd: Integer; const AIsSet: Boolean);
+procedure TListItem.SetState(AState: TListItemState; AIsSet: Boolean);
 var
-  AState: TListItemState;
   LV: TCustomListView;
 begin
-  AState := TListItemState(ALisOrd);
-
   if (AState in FStates) = AIsSet then Exit;
 
   if AIsSet then
-- 
2.31.0

-- 
_______________________________________________
lazarus mailing list
lazarus@lists.lazarus-ide.org
https://lists.lazarus-ide.org/listinfo/lazarus

Reply via email to