Split CPWL_ListBox::Item::m_dwStates into discrete bools

-- Use std::swap() in one obvious place.
-- Add const to one member.
-- Tidy some loops.

Change-Id: Iad86b1f725f2f8ddf62f843803215283405d2cfa
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/83193
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/xfa/fwl/cfwl_listbox.cpp b/xfa/fwl/cfwl_listbox.cpp
index 118af64..dffdf84 100644
--- a/xfa/fwl/cfwl_listbox.cpp
+++ b/xfa/fwl/cfwl_listbox.cpp
@@ -112,9 +112,7 @@
   int32_t iCount = CountItems(this);
   for (int32_t i = 0; i < iCount; i++) {
     Item* pItem = GetItem(this, i);
-    if (!pItem)
-      continue;
-    if (pItem->GetStates() & FWL_ITEMSTATE_LTB_Selected)
+    if (pItem && pItem->IsSelected())
       iRet++;
   }
   return iRet;
@@ -134,7 +132,7 @@
     Item* pItem = GetItem(this, i);
     if (!pItem)
       return -1;
-    if (pItem->GetStates() & FWL_ITEMSTATE_LTB_Selected) {
+    if (pItem->IsSelected()) {
       if (index == nIndex)
         return i;
       index++;
@@ -154,7 +152,7 @@
     return;
   }
   if (IsMultiSelection())
-    SetSelectionDirect(pItem, bSelect);
+    pItem->SetSelected(bSelect);
   else
     SetSelection(pItem, pItem, bSelect);
 }
@@ -191,38 +189,28 @@
 void CFWL_ListBox::SetSelection(Item* hStart, Item* hEnd, bool bSelected) {
   int32_t iStart = GetItemIndex(this, hStart);
   int32_t iEnd = GetItemIndex(this, hEnd);
-  if (iStart > iEnd) {
-    int32_t iTemp = iStart;
-    iStart = iEnd;
-    iEnd = iTemp;
-  }
+  if (iStart > iEnd)
+    std::swap(iStart, iEnd);
   if (bSelected) {
     int32_t iCount = CountItems(this);
-    for (int32_t i = 0; i < iCount; i++)
-      SetSelectionDirect(GetItem(this, i), false);
+    for (int32_t i = 0; i < iCount; i++) {
+      Item* pItem = GetItem(this, i);
+      if (pItem)
+        pItem->SetSelected(false);
+    }
   }
-  for (; iStart <= iEnd; iStart++)
-    SetSelectionDirect(GetItem(this, iStart), bSelected);
-}
-
-void CFWL_ListBox::SetSelectionDirect(Item* pItem, bool bSelect) {
-  if (!pItem)
-    return;
-
-  uint32_t dwOldStyle = pItem->GetStates();
-  bSelect ? dwOldStyle |= FWL_ITEMSTATE_LTB_Selected
-          : dwOldStyle &= ~FWL_ITEMSTATE_LTB_Selected;
-  pItem->SetStates(dwOldStyle);
+  while (iStart <= iEnd) {
+    Item* pItem = GetItem(this, iStart);
+    if (pItem)
+      pItem->SetSelected(bSelected);
+    ++iStart;
+  }
 }
 
 bool CFWL_ListBox::IsMultiSelection() const {
   return m_Properties.m_dwStyleExts & FWL_STYLEEXT_LTB_MultiSelection;
 }
 
-bool CFWL_ListBox::IsItemSelected(Item* pItem) {
-  return pItem && (pItem->GetStates() & FWL_ITEMSTATE_LTB_Selected) != 0;
-}
-
 void CFWL_ListBox::ClearSelection() {
   bool bMulti = IsMultiSelection();
   int32_t iCount = CountItems(this);
@@ -230,9 +218,9 @@
     Item* pItem = GetItem(this, i);
     if (!pItem)
       continue;
-    if (!(pItem->GetStates() & FWL_ITEMSTATE_LTB_Selected))
+    if (!pItem->IsSelected())
       continue;
-    SetSelectionDirect(pItem, false);
+    pItem->SetSelected(false);
     if (!bMulti)
       return;
   }
@@ -256,8 +244,8 @@
   for (int32_t i = 0; i < iCount; i++) {
     Item* pItem = GetItem(this, i);
     if (!pItem)
-      return nullptr;
-    if (pItem->GetStates() & FWL_ITEMSTATE_LTB_Focused)
+      break;
+    if (pItem->IsFocused())
       return pItem;
   }
   return nullptr;
@@ -268,16 +256,10 @@
   if (pItem == hFocus)
     return;
 
-  if (hFocus) {
-    uint32_t dwStyle = hFocus->GetStates();
-    dwStyle &= ~FWL_ITEMSTATE_LTB_Focused;
-    hFocus->SetStates(dwStyle);
-  }
-  if (pItem) {
-    uint32_t dwStyle = pItem->GetStates();
-    dwStyle |= FWL_ITEMSTATE_LTB_Focused;
-    pItem->SetStates(dwStyle);
-  }
+  if (hFocus)
+    hFocus->SetFocused(false);
+  if (pItem)
+    pItem->SetFocused(true);
 }
 
 CFWL_ListBox::Item* CFWL_ListBox::GetItemAtPoint(const CFX_PointF& point) {
@@ -381,15 +363,14 @@
                             int32_t Index,
                             const CFX_RectF& rtItem,
                             const CFX_Matrix& mtMatrix) {
-  uint32_t dwItemStyles = pItem ? pItem->GetStates() : 0;
-  uint32_t dwPartStates = CFWL_PartState_Normal;
+  CFWL_PartStateMask dwPartStates = CFWL_PartState_Normal;
   if (m_Properties.m_dwStates & FWL_STATE_WGT_Disabled)
     dwPartStates = CFWL_PartState_Disabled;
-  else if (dwItemStyles & FWL_ITEMSTATE_LTB_Selected)
+  else if (pItem && pItem->IsSelected())
     dwPartStates = CFWL_PartState_Selected;
 
-  if (m_Properties.m_dwStates & FWL_STATE_WGT_Focused &&
-      dwItemStyles & FWL_ITEMSTATE_LTB_Focused) {
+  if ((m_Properties.m_dwStates & FWL_STATE_WGT_Focused) && pItem &&
+      pItem->IsFocused()) {
     dwPartStates |= CFWL_PartState_Focused;
   }
 
@@ -703,14 +684,13 @@
 
   if (IsMultiSelection()) {
     if (pMsg->m_dwFlags & FWL_KEYFLAG_Ctrl) {
-      bool bSelected = IsItemSelected(pItem);
-      SetSelectionDirect(pItem, !bSelected);
+      pItem->SetSelected(!pItem->IsSelected());
       m_hAnchor = pItem;
     } else if (pMsg->m_dwFlags & FWL_KEYFLAG_Shift) {
       if (m_hAnchor)
         SetSelection(m_hAnchor, pItem, true);
       else
-        SetSelectionDirect(pItem, true);
+        pItem->SetSelected(true);
     } else {
       SetSelection(pItem, pItem, true);
       m_hAnchor = pItem;
@@ -768,7 +748,7 @@
       if (m_hAnchor)
         SetSelection(m_hAnchor, pItem, true);
       else
-        SetSelectionDirect(pItem, true);
+        pItem->SetSelected(true);
     } else {
       SetSelection(pItem, pItem, true);
       m_hAnchor = pItem;
@@ -878,9 +858,8 @@
   if (iSel >= 0) {
     Item* item = GetItem(this, iSel);
     if (item)
-      item->SetStates(item->GetStates() | FWL_ITEMSTATE_LTB_Selected);
+      item->SetSelected(true);
   }
-
   m_ItemArray.erase(m_ItemArray.begin() + nIndex);
 }
 
diff --git a/xfa/fwl/cfwl_listbox.h b/xfa/fwl/cfwl_listbox.h
index 42a8a7d..e2f337f 100644
--- a/xfa/fwl/cfwl_listbox.h
+++ b/xfa/fwl/cfwl_listbox.h
@@ -21,8 +21,6 @@
 #define FWL_STYLEEXT_LTB_RightAlign (2L << 4)
 #define FWL_STYLEEXT_LTB_AlignMask (3L << 4)
 #define FWL_STYLEEXT_LTB_ShowScrollBarFocus (1L << 10)
-#define FWL_ITEMSTATE_LTB_Selected (1L << 0)
-#define FWL_ITEMSTATE_LTB_Focused (1L << 1)
 
 class CFWL_MessageMouse;
 class CFWL_MessageMouseWheel;
@@ -34,16 +32,19 @@
     explicit Item(const WideString& text);
     ~Item();
 
+    bool IsSelected() const { return m_bIsSelected; }
+    void SetSelected(bool enable) { m_bIsSelected = enable; }
+    bool IsFocused() const { return m_bIsFocused; }
+    void SetFocused(bool enable) { m_bIsFocused = enable; }
     CFX_RectF GetRect() const { return m_ItemRect; }
     void SetRect(const CFX_RectF& rect) { m_ItemRect = rect; }
-    uint32_t GetStates() const { return m_dwStates; }
-    void SetStates(uint32_t dwStates) { m_dwStates = dwStates; }
     WideString GetText() const { return m_wsText; }
 
    private:
-    uint32_t m_dwStates = 0;
+    bool m_bIsSelected = false;
+    bool m_bIsFocused = false;
     CFX_RectF m_ItemRect;
-    WideString m_wsText;
+    const WideString m_wsText;
   };
 
   CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED;
@@ -93,9 +94,7 @@
   const CFX_RectF& GetRTClient() const { return m_ClientRect; }
 
  private:
-  void SetSelectionDirect(Item* hItem, bool bSelect);
   bool IsMultiSelection() const;
-  bool IsItemSelected(Item* hItem);
   void ClearSelection();
   void SelectAll();
   Item* GetFocusedItem();
diff --git a/xfa/fxfa/cxfa_fflistbox.cpp b/xfa/fxfa/cxfa_fflistbox.cpp
index 7784cda..5d985f1 100644
--- a/xfa/fxfa/cxfa_fflistbox.cpp
+++ b/xfa/fxfa/cxfa_fflistbox.cpp
@@ -112,7 +112,7 @@
 
   for (int32_t i = 0; i < iSels; ++i) {
     CFWL_ListBox::Item* hlistItem = pListBox->GetItem(nullptr, iSelArray[i]);
-    if (!(hlistItem->GetStates() & FWL_ITEMSTATE_LTB_Selected))
+    if (!hlistItem->IsSelected())
       return true;
   }
   return false;