xref: /aosp_15_r20/external/pdfium/xfa/fwl/cfwl_listbox.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2014 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "xfa/fwl/cfwl_listbox.h"
8 
9 #include <algorithm>
10 #include <memory>
11 #include <utility>
12 
13 #include "core/fxcrt/stl_util.h"
14 #include "third_party/base/numerics/safe_conversions.h"
15 #include "v8/include/cppgc/visitor.h"
16 #include "xfa/fde/cfde_textout.h"
17 #include "xfa/fgas/graphics/cfgas_gegraphics.h"
18 #include "xfa/fwl/cfwl_app.h"
19 #include "xfa/fwl/cfwl_messagekey.h"
20 #include "xfa/fwl/cfwl_messagemouse.h"
21 #include "xfa/fwl/cfwl_messagemousewheel.h"
22 #include "xfa/fwl/cfwl_themebackground.h"
23 #include "xfa/fwl/cfwl_themepart.h"
24 #include "xfa/fwl/cfwl_themetext.h"
25 #include "xfa/fwl/fwl_widgetdef.h"
26 #include "xfa/fwl/ifwl_themeprovider.h"
27 
28 namespace {
29 
30 const int kItemTextMargin = 2;
31 
32 }  // namespace
33 
CFWL_ListBox(CFWL_App * app,const Properties & properties,CFWL_Widget * pOuter)34 CFWL_ListBox::CFWL_ListBox(CFWL_App* app,
35                            const Properties& properties,
36                            CFWL_Widget* pOuter)
37     : CFWL_Widget(app, properties, pOuter) {}
38 
39 CFWL_ListBox::~CFWL_ListBox() = default;
40 
Trace(cppgc::Visitor * visitor) const41 void CFWL_ListBox::Trace(cppgc::Visitor* visitor) const {
42   CFWL_Widget::Trace(visitor);
43   visitor->Trace(m_pHorzScrollBar);
44   visitor->Trace(m_pVertScrollBar);
45 }
46 
GetClassID() const47 FWL_Type CFWL_ListBox::GetClassID() const {
48   return FWL_Type::ListBox;
49 }
50 
Update()51 void CFWL_ListBox::Update() {
52   if (IsLocked())
53     return;
54 
55   switch (m_Properties.m_dwStyleExts & FWL_STYLEEXT_LTB_AlignMask) {
56     case FWL_STYLEEXT_LTB_LeftAlign:
57       m_iTTOAligns = FDE_TextAlignment::kCenterLeft;
58       break;
59     case FWL_STYLEEXT_LTB_RightAlign:
60       m_iTTOAligns = FDE_TextAlignment::kCenterRight;
61       break;
62     case FWL_STYLEEXT_LTB_CenterAlign:
63     default:
64       m_iTTOAligns = FDE_TextAlignment::kCenter;
65       break;
66   }
67   m_TTOStyles.single_line_ = true;
68   m_fScorllBarWidth = GetScrollWidth();
69   CalcSize();
70 }
71 
HitTest(const CFX_PointF & point)72 FWL_WidgetHit CFWL_ListBox::HitTest(const CFX_PointF& point) {
73   if (IsShowHorzScrollBar()) {
74     CFX_RectF rect = m_pHorzScrollBar->GetWidgetRect();
75     if (rect.Contains(point))
76       return FWL_WidgetHit::HScrollBar;
77   }
78   if (IsShowVertScrollBar()) {
79     CFX_RectF rect = m_pVertScrollBar->GetWidgetRect();
80     if (rect.Contains(point))
81       return FWL_WidgetHit::VScrollBar;
82   }
83   if (m_ClientRect.Contains(point))
84     return FWL_WidgetHit::Client;
85   return FWL_WidgetHit::Unknown;
86 }
87 
DrawWidget(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & matrix)88 void CFWL_ListBox::DrawWidget(CFGAS_GEGraphics* pGraphics,
89                               const CFX_Matrix& matrix) {
90   if (!pGraphics)
91     return;
92 
93   CFGAS_GEGraphics::StateRestorer restorer(pGraphics);
94   if (HasBorder())
95     DrawBorder(pGraphics, CFWL_ThemePart::Part::kBorder, matrix);
96 
97   CFX_RectF rtClip(m_ContentRect);
98   if (IsShowHorzScrollBar())
99     rtClip.height -= m_fScorllBarWidth;
100   if (IsShowVertScrollBar())
101     rtClip.width -= m_fScorllBarWidth;
102 
103   pGraphics->SetClipRect(matrix.TransformRect(rtClip));
104   if ((m_Properties.m_dwStyles & FWL_STYLE_WGT_NoBackground) == 0)
105     DrawBkground(pGraphics, matrix);
106 
107   DrawItems(pGraphics, matrix);
108 }
109 
CountSelItems()110 int32_t CFWL_ListBox::CountSelItems() {
111   int32_t iRet = 0;
112   int32_t iCount = CountItems(this);
113   for (int32_t i = 0; i < iCount; i++) {
114     Item* pItem = GetItem(this, i);
115     if (pItem && pItem->IsSelected())
116       iRet++;
117   }
118   return iRet;
119 }
120 
GetSelItem(int32_t nIndexSel)121 CFWL_ListBox::Item* CFWL_ListBox::GetSelItem(int32_t nIndexSel) {
122   int32_t idx = GetSelIndex(nIndexSel);
123   if (idx < 0)
124     return nullptr;
125   return GetItem(this, idx);
126 }
127 
GetSelIndex(int32_t nIndex)128 int32_t CFWL_ListBox::GetSelIndex(int32_t nIndex) {
129   int32_t index = 0;
130   int32_t iCount = CountItems(this);
131   for (int32_t i = 0; i < iCount; i++) {
132     Item* pItem = GetItem(this, i);
133     if (!pItem)
134       return -1;
135     if (pItem->IsSelected()) {
136       if (index == nIndex)
137         return i;
138       index++;
139     }
140   }
141   return -1;
142 }
143 
SetSelItem(Item * pItem,bool bSelect)144 void CFWL_ListBox::SetSelItem(Item* pItem, bool bSelect) {
145   if (!pItem) {
146     if (bSelect) {
147       SelectAll();
148     } else {
149       ClearSelection();
150       SetFocusItem(nullptr);
151     }
152     return;
153   }
154   if (IsMultiSelection())
155     pItem->SetSelected(bSelect);
156   else
157     SetSelection(pItem, pItem, bSelect);
158 }
159 
GetListItem(Item * pItem,XFA_FWL_VKEYCODE dwKeyCode)160 CFWL_ListBox::Item* CFWL_ListBox::GetListItem(Item* pItem,
161                                               XFA_FWL_VKEYCODE dwKeyCode) {
162   Item* hRet = nullptr;
163   switch (dwKeyCode) {
164     case XFA_FWL_VKEY_Up:
165     case XFA_FWL_VKEY_Down:
166     case XFA_FWL_VKEY_Home:
167     case XFA_FWL_VKEY_End: {
168       const bool bUp = dwKeyCode == XFA_FWL_VKEY_Up;
169       const bool bDown = dwKeyCode == XFA_FWL_VKEY_Down;
170       const bool bHome = dwKeyCode == XFA_FWL_VKEY_Home;
171       int32_t iDstItem = -1;
172       if (bUp || bDown) {
173         int32_t index = GetItemIndex(this, pItem);
174         iDstItem = dwKeyCode == XFA_FWL_VKEY_Up ? index - 1 : index + 1;
175       } else if (bHome) {
176         iDstItem = 0;
177       } else {
178         int32_t iCount = CountItems(this);
179         iDstItem = iCount - 1;
180       }
181       hRet = GetItem(this, iDstItem);
182       break;
183     }
184     default:
185       break;
186   }
187   return hRet;
188 }
189 
SetSelection(Item * hStart,Item * hEnd,bool bSelected)190 void CFWL_ListBox::SetSelection(Item* hStart, Item* hEnd, bool bSelected) {
191   int32_t iStart = GetItemIndex(this, hStart);
192   int32_t iEnd = GetItemIndex(this, hEnd);
193   if (iStart > iEnd)
194     std::swap(iStart, iEnd);
195   if (bSelected) {
196     int32_t iCount = CountItems(this);
197     for (int32_t i = 0; i < iCount; i++) {
198       Item* pItem = GetItem(this, i);
199       if (pItem)
200         pItem->SetSelected(false);
201     }
202   }
203   while (iStart <= iEnd) {
204     Item* pItem = GetItem(this, iStart);
205     if (pItem)
206       pItem->SetSelected(bSelected);
207     ++iStart;
208   }
209 }
210 
IsMultiSelection() const211 bool CFWL_ListBox::IsMultiSelection() const {
212   return m_Properties.m_dwStyleExts & FWL_STYLEEXT_LTB_MultiSelection;
213 }
214 
ClearSelection()215 void CFWL_ListBox::ClearSelection() {
216   bool bMulti = IsMultiSelection();
217   int32_t iCount = CountItems(this);
218   for (int32_t i = 0; i < iCount; i++) {
219     Item* pItem = GetItem(this, i);
220     if (!pItem)
221       continue;
222     if (!pItem->IsSelected())
223       continue;
224     pItem->SetSelected(false);
225     if (!bMulti)
226       return;
227   }
228 }
229 
SelectAll()230 void CFWL_ListBox::SelectAll() {
231   if (!IsMultiSelection())
232     return;
233 
234   int32_t iCount = CountItems(this);
235   if (iCount <= 0)
236     return;
237 
238   Item* pItemStart = GetItem(this, 0);
239   Item* pItemEnd = GetItem(this, iCount - 1);
240   SetSelection(pItemStart, pItemEnd, false);
241 }
242 
GetFocusedItem()243 CFWL_ListBox::Item* CFWL_ListBox::GetFocusedItem() {
244   int32_t iCount = CountItems(this);
245   for (int32_t i = 0; i < iCount; i++) {
246     Item* pItem = GetItem(this, i);
247     if (!pItem)
248       break;
249     if (pItem->IsFocused())
250       return pItem;
251   }
252   return nullptr;
253 }
254 
SetFocusItem(Item * pItem)255 void CFWL_ListBox::SetFocusItem(Item* pItem) {
256   Item* hFocus = GetFocusedItem();
257   if (pItem == hFocus)
258     return;
259 
260   if (hFocus)
261     hFocus->SetFocused(false);
262   if (pItem)
263     pItem->SetFocused(true);
264 }
265 
GetItemAtPoint(const CFX_PointF & point)266 CFWL_ListBox::Item* CFWL_ListBox::GetItemAtPoint(const CFX_PointF& point) {
267   CFX_PointF pos = point - m_ContentRect.TopLeft();
268   float fPosX = 0.0f;
269   if (m_pHorzScrollBar)
270     fPosX = m_pHorzScrollBar->GetPos();
271 
272   float fPosY = 0.0;
273   if (m_pVertScrollBar)
274     fPosY = m_pVertScrollBar->GetPos();
275 
276   int32_t nCount = CountItems(this);
277   for (int32_t i = 0; i < nCount; i++) {
278     Item* pItem = GetItem(this, i);
279     if (!pItem)
280       continue;
281 
282     CFX_RectF rtItem = pItem->GetRect();
283     rtItem.Offset(-fPosX, -fPosY);
284     if (rtItem.Contains(pos))
285       return pItem;
286   }
287   return nullptr;
288 }
289 
ScrollToVisible(Item * pItem)290 bool CFWL_ListBox::ScrollToVisible(Item* pItem) {
291   if (!m_pVertScrollBar)
292     return false;
293 
294   CFX_RectF rtItem = pItem ? pItem->GetRect() : CFX_RectF();
295   bool bScroll = false;
296   float fPosY = m_pVertScrollBar->GetPos();
297   rtItem.Offset(0, -fPosY + m_ContentRect.top);
298   if (rtItem.top < m_ContentRect.top) {
299     fPosY += rtItem.top - m_ContentRect.top;
300     bScroll = true;
301   } else if (rtItem.bottom() > m_ContentRect.bottom()) {
302     fPosY += rtItem.bottom() - m_ContentRect.bottom();
303     bScroll = true;
304   }
305   if (!bScroll)
306     return false;
307 
308   m_pVertScrollBar->SetPos(fPosY);
309   m_pVertScrollBar->SetTrackPos(fPosY);
310   RepaintRect(m_ClientRect);
311   return true;
312 }
313 
DrawBkground(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & mtMatrix)314 void CFWL_ListBox::DrawBkground(CFGAS_GEGraphics* pGraphics,
315                                 const CFX_Matrix& mtMatrix) {
316   if (!pGraphics)
317     return;
318 
319   CFWL_ThemeBackground param(CFWL_ThemePart::Part::kBackground, this,
320                              pGraphics);
321   param.m_matrix = mtMatrix;
322   param.m_PartRect = m_ClientRect;
323   if (IsShowHorzScrollBar() && IsShowVertScrollBar())
324     param.m_pRtData = &m_StaticRect;
325   if (!IsEnabled())
326     param.m_dwStates = CFWL_PartState::kDisabled;
327   GetThemeProvider()->DrawBackground(param);
328 }
329 
DrawItems(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & mtMatrix)330 void CFWL_ListBox::DrawItems(CFGAS_GEGraphics* pGraphics,
331                              const CFX_Matrix& mtMatrix) {
332   float fPosX = 0.0f;
333   if (m_pHorzScrollBar)
334     fPosX = m_pHorzScrollBar->GetPos();
335 
336   float fPosY = 0.0f;
337   if (m_pVertScrollBar)
338     fPosY = m_pVertScrollBar->GetPos();
339 
340   CFX_RectF rtView(m_ContentRect);
341   if (m_pHorzScrollBar)
342     rtView.height -= m_fScorllBarWidth;
343   if (m_pVertScrollBar)
344     rtView.width -= m_fScorllBarWidth;
345 
346   int32_t iCount = CountItems(this);
347   for (int32_t i = 0; i < iCount; i++) {
348     CFWL_ListBox::Item* pItem = GetItem(this, i);
349     if (!pItem)
350       continue;
351 
352     CFX_RectF rtItem = pItem->GetRect();
353     rtItem.Offset(m_ContentRect.left - fPosX, m_ContentRect.top - fPosY);
354     if (rtItem.bottom() < m_ContentRect.top)
355       continue;
356     if (rtItem.top >= m_ContentRect.bottom())
357       break;
358     DrawItem(pGraphics, pItem, i, rtItem, mtMatrix);
359   }
360 }
361 
DrawItem(CFGAS_GEGraphics * pGraphics,Item * pItem,int32_t Index,const CFX_RectF & rtItem,const CFX_Matrix & mtMatrix)362 void CFWL_ListBox::DrawItem(CFGAS_GEGraphics* pGraphics,
363                             Item* pItem,
364                             int32_t Index,
365                             const CFX_RectF& rtItem,
366                             const CFX_Matrix& mtMatrix) {
367   Mask<CFWL_PartState> dwPartStates = CFWL_PartState::kNormal;
368   if (m_Properties.m_dwStates & FWL_STATE_WGT_Disabled)
369     dwPartStates = CFWL_PartState::kDisabled;
370   else if (pItem && pItem->IsSelected())
371     dwPartStates = CFWL_PartState::kSelected;
372 
373   if ((m_Properties.m_dwStates & FWL_STATE_WGT_Focused) && pItem &&
374       pItem->IsFocused()) {
375     dwPartStates |= CFWL_PartState::kFocused;
376   }
377 
378   CFX_RectF rtFocus(rtItem);  // Must outlive |bg_param|.
379   CFWL_ThemeBackground bg_param(CFWL_ThemePart::Part::kListItem, this,
380                                 pGraphics);
381   bg_param.m_dwStates = dwPartStates;
382   bg_param.m_matrix = mtMatrix;
383   bg_param.m_PartRect = rtItem;
384   bg_param.m_bMaximize = true;
385   bg_param.m_pRtData = &rtFocus;
386   if (m_pVertScrollBar && !m_pHorzScrollBar &&
387       (dwPartStates & CFWL_PartState::kFocused)) {
388     bg_param.m_PartRect.left += 1;
389     bg_param.m_PartRect.width -= (m_fScorllBarWidth + 1);
390     rtFocus.Deflate(0.5, 0.5, 1 + m_fScorllBarWidth, 1);
391   }
392 
393   IFWL_ThemeProvider* pTheme = GetThemeProvider();
394   pTheme->DrawBackground(bg_param);
395   if (!pItem)
396     return;
397 
398   WideString wsText = pItem->GetText();
399   if (wsText.GetLength() <= 0)
400     return;
401 
402   CFX_RectF rtText(rtItem);
403   rtText.Deflate(kItemTextMargin, kItemTextMargin);
404 
405   CFWL_ThemeText textParam(CFWL_ThemePart::Part::kListItem, this, pGraphics);
406   textParam.m_dwStates = dwPartStates;
407   textParam.m_matrix = mtMatrix;
408   textParam.m_PartRect = rtText;
409   textParam.m_wsText = std::move(wsText);
410   textParam.m_dwTTOStyles = m_TTOStyles;
411   textParam.m_iTTOAlign = m_iTTOAligns;
412   textParam.m_bMaximize = true;
413   pTheme->DrawText(textParam);
414 }
415 
CalcSize()416 CFX_SizeF CFWL_ListBox::CalcSize() {
417   m_ClientRect = GetClientRect();
418   m_ContentRect = m_ClientRect;
419   CFX_RectF rtUIMargin;
420   if (!GetOuter()) {
421     CFWL_ThemePart part(CFWL_ThemePart::Part::kNone, this);
422     CFX_RectF pUIMargin = GetThemeProvider()->GetUIMargin(part);
423     m_ContentRect.Deflate(pUIMargin.left, pUIMargin.top, pUIMargin.width,
424                           pUIMargin.height);
425   }
426 
427   float fWidth = GetMaxTextWidth();
428   fWidth += 2 * kItemTextMargin;
429 
430   float fActualWidth = m_ClientRect.width - rtUIMargin.left - rtUIMargin.width;
431   fWidth = std::max(fWidth, fActualWidth);
432   m_fItemHeight = CalcItemHeight();
433 
434   int32_t iCount = CountItems(this);
435   CFX_SizeF fs;
436   for (int32_t i = 0; i < iCount; i++) {
437     Item* htem = GetItem(this, i);
438     UpdateItemSize(htem, fs, fWidth, m_fItemHeight);
439   }
440 
441   float iHeight = m_ClientRect.height;
442   bool bShowVertScr = false;
443   bool bShowHorzScr = false;
444   if (!bShowVertScr && (m_Properties.m_dwStyles & FWL_STYLE_WGT_VScroll))
445     bShowVertScr = (fs.height > iHeight);
446 
447   float fMax = 0.0f;
448   if (bShowVertScr) {
449     if (!m_pVertScrollBar)
450       InitVerticalScrollBar();
451 
452     CFX_RectF rtScrollBar(m_ClientRect.right() - m_fScorllBarWidth,
453                           m_ClientRect.top, m_fScorllBarWidth,
454                           m_ClientRect.height - 1);
455     if (bShowHorzScr)
456       rtScrollBar.height -= m_fScorllBarWidth;
457 
458     m_pVertScrollBar->SetWidgetRect(rtScrollBar);
459     fMax = std::max(fs.height - m_ContentRect.height, m_fItemHeight);
460 
461     m_pVertScrollBar->SetRange(0.0f, fMax);
462     m_pVertScrollBar->SetPageSize(rtScrollBar.height * 9 / 10);
463     m_pVertScrollBar->SetStepSize(m_fItemHeight);
464 
465     float fPos = std::clamp(m_pVertScrollBar->GetPos(), 0.0f, fMax);
466     m_pVertScrollBar->SetPos(fPos);
467     m_pVertScrollBar->SetTrackPos(fPos);
468     if ((m_Properties.m_dwStyleExts & FWL_STYLEEXT_LTB_ShowScrollBarFocus) ==
469             0 ||
470         (m_Properties.m_dwStates & FWL_STATE_WGT_Focused)) {
471       m_pVertScrollBar->RemoveStates(FWL_STATE_WGT_Invisible);
472     }
473     m_pVertScrollBar->Update();
474   } else if (m_pVertScrollBar) {
475     m_pVertScrollBar->SetPos(0);
476     m_pVertScrollBar->SetTrackPos(0);
477     m_pVertScrollBar->SetStates(FWL_STATE_WGT_Invisible);
478   }
479   if (bShowHorzScr) {
480     if (!m_pHorzScrollBar)
481       InitHorizontalScrollBar();
482 
483     CFX_RectF rtScrollBar(m_ClientRect.left,
484                           m_ClientRect.bottom() - m_fScorllBarWidth,
485                           m_ClientRect.width, m_fScorllBarWidth);
486     if (bShowVertScr)
487       rtScrollBar.width -= m_fScorllBarWidth;
488 
489     m_pHorzScrollBar->SetWidgetRect(rtScrollBar);
490     fMax = fs.width - rtScrollBar.width;
491     m_pHorzScrollBar->SetRange(0.0f, fMax);
492     m_pHorzScrollBar->SetPageSize(fWidth * 9 / 10);
493     m_pHorzScrollBar->SetStepSize(fWidth / 10);
494 
495     float fPos = std::clamp(m_pHorzScrollBar->GetPos(), 0.0f, fMax);
496     m_pHorzScrollBar->SetPos(fPos);
497     m_pHorzScrollBar->SetTrackPos(fPos);
498     if ((m_Properties.m_dwStyleExts & FWL_STYLEEXT_LTB_ShowScrollBarFocus) ==
499             0 ||
500         (m_Properties.m_dwStates & FWL_STATE_WGT_Focused)) {
501       m_pHorzScrollBar->RemoveStates(FWL_STATE_WGT_Invisible);
502     }
503     m_pHorzScrollBar->Update();
504   } else if (m_pHorzScrollBar) {
505     m_pHorzScrollBar->SetPos(0);
506     m_pHorzScrollBar->SetTrackPos(0);
507     m_pHorzScrollBar->SetStates(FWL_STATE_WGT_Invisible);
508   }
509   if (bShowVertScr && bShowHorzScr) {
510     m_StaticRect = CFX_RectF(m_ClientRect.right() - m_fScorllBarWidth,
511                              m_ClientRect.bottom() - m_fScorllBarWidth,
512                              m_fScorllBarWidth, m_fScorllBarWidth);
513   }
514   return fs;
515 }
516 
UpdateItemSize(Item * pItem,CFX_SizeF & size,float fWidth,float fItemHeight) const517 void CFWL_ListBox::UpdateItemSize(Item* pItem,
518                                   CFX_SizeF& size,
519                                   float fWidth,
520                                   float fItemHeight) const {
521   if (pItem) {
522     CFX_RectF rtItem(0, size.height, fWidth, fItemHeight);
523     pItem->SetRect(rtItem);
524   }
525   size.width = fWidth;
526   size.height += fItemHeight;
527 }
528 
GetMaxTextWidth()529 float CFWL_ListBox::GetMaxTextWidth() {
530   float fRet = 0.0f;
531   int32_t iCount = CountItems(this);
532   for (int32_t i = 0; i < iCount; i++) {
533     Item* pItem = GetItem(this, i);
534     if (!pItem)
535       continue;
536 
537     CFX_SizeF sz = CalcTextSize(pItem->GetText(), false);
538     fRet = std::max(fRet, sz.width);
539   }
540   return fRet;
541 }
542 
GetScrollWidth()543 float CFWL_ListBox::GetScrollWidth() {
544   return GetThemeProvider()->GetScrollBarWidth();
545 }
546 
CalcItemHeight()547 float CFWL_ListBox::CalcItemHeight() {
548   CFWL_ThemePart part(CFWL_ThemePart::Part::kNone, this);
549   return GetThemeProvider()->GetFontSize(part) + 2 * kItemTextMargin;
550 }
551 
InitVerticalScrollBar()552 void CFWL_ListBox::InitVerticalScrollBar() {
553   if (m_pVertScrollBar)
554     return;
555 
556   m_pVertScrollBar = cppgc::MakeGarbageCollected<CFWL_ScrollBar>(
557       GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp(),
558       Properties{0, FWL_STYLEEXT_SCB_Vert, FWL_STATE_WGT_Invisible}, this);
559 }
560 
InitHorizontalScrollBar()561 void CFWL_ListBox::InitHorizontalScrollBar() {
562   if (m_pHorzScrollBar)
563     return;
564 
565   m_pHorzScrollBar = cppgc::MakeGarbageCollected<CFWL_ScrollBar>(
566       GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp(),
567       Properties{0, FWL_STYLEEXT_SCB_Horz, FWL_STATE_WGT_Invisible}, this);
568 }
569 
IsShowVertScrollBar() const570 bool CFWL_ListBox::IsShowVertScrollBar() const {
571   return m_pVertScrollBar && m_pVertScrollBar->IsVisible() &&
572          ScrollBarPropertiesPresent();
573 }
574 
IsShowHorzScrollBar() const575 bool CFWL_ListBox::IsShowHorzScrollBar() const {
576   return m_pHorzScrollBar && m_pHorzScrollBar->IsVisible() &&
577          ScrollBarPropertiesPresent();
578 }
579 
ScrollBarPropertiesPresent() const580 bool CFWL_ListBox::ScrollBarPropertiesPresent() const {
581   return !(m_Properties.m_dwStyleExts & FWL_STYLEEXT_LTB_ShowScrollBarFocus) ||
582          (m_Properties.m_dwStates & FWL_STATE_WGT_Focused);
583 }
584 
OnProcessMessage(CFWL_Message * pMessage)585 void CFWL_ListBox::OnProcessMessage(CFWL_Message* pMessage) {
586   if (!IsEnabled())
587     return;
588 
589   switch (pMessage->GetType()) {
590     case CFWL_Message::Type::kSetFocus:
591       OnFocusGained();
592       break;
593     case CFWL_Message::Type::kKillFocus:
594       OnFocusLost();
595       break;
596     case CFWL_Message::Type::kMouse: {
597       CFWL_MessageMouse* pMsg = static_cast<CFWL_MessageMouse*>(pMessage);
598       switch (pMsg->m_dwCmd) {
599         case CFWL_MessageMouse::MouseCommand::kLeftButtonDown:
600           OnLButtonDown(pMsg);
601           break;
602         case CFWL_MessageMouse::MouseCommand::kLeftButtonUp:
603           OnLButtonUp(pMsg);
604           break;
605         default:
606           break;
607       }
608       break;
609     }
610     case CFWL_Message::Type::kMouseWheel:
611       OnMouseWheel(static_cast<CFWL_MessageMouseWheel*>(pMessage));
612       break;
613     case CFWL_Message::Type::kKey: {
614       CFWL_MessageKey* pMsg = static_cast<CFWL_MessageKey*>(pMessage);
615       if (pMsg->m_dwCmd == CFWL_MessageKey::KeyCommand::kKeyDown)
616         OnKeyDown(pMsg);
617       break;
618     }
619   }
620   // Dst target could be |this|, continue only if not destroyed by above.
621   if (pMessage->GetDstTarget())
622     CFWL_Widget::OnProcessMessage(pMessage);
623 }
624 
OnProcessEvent(CFWL_Event * pEvent)625 void CFWL_ListBox::OnProcessEvent(CFWL_Event* pEvent) {
626   if (!pEvent)
627     return;
628   if (pEvent->GetType() != CFWL_Event::Type::Scroll)
629     return;
630 
631   CFWL_Widget* pSrcTarget = pEvent->GetSrcTarget();
632   if ((pSrcTarget == m_pVertScrollBar && m_pVertScrollBar) ||
633       (pSrcTarget == m_pHorzScrollBar && m_pHorzScrollBar)) {
634     CFWL_EventScroll* pScrollEvent = static_cast<CFWL_EventScroll*>(pEvent);
635     OnScroll(static_cast<CFWL_ScrollBar*>(pSrcTarget),
636              pScrollEvent->GetScrollCode(), pScrollEvent->GetPos());
637   }
638 }
639 
OnDrawWidget(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & matrix)640 void CFWL_ListBox::OnDrawWidget(CFGAS_GEGraphics* pGraphics,
641                                 const CFX_Matrix& matrix) {
642   DrawWidget(pGraphics, matrix);
643 }
644 
OnFocusGained()645 void CFWL_ListBox::OnFocusGained() {
646   if (GetStyleExts() & FWL_STYLEEXT_LTB_ShowScrollBarFocus) {
647     if (m_pVertScrollBar)
648       m_pVertScrollBar->RemoveStates(FWL_STATE_WGT_Invisible);
649     if (m_pHorzScrollBar)
650       m_pHorzScrollBar->RemoveStates(FWL_STATE_WGT_Invisible);
651   }
652   m_Properties.m_dwStates |= FWL_STATE_WGT_Focused;
653   RepaintRect(m_ClientRect);
654 }
655 
OnFocusLost()656 void CFWL_ListBox::OnFocusLost() {
657   if (GetStyleExts() & FWL_STYLEEXT_LTB_ShowScrollBarFocus) {
658     if (m_pVertScrollBar)
659       m_pVertScrollBar->SetStates(FWL_STATE_WGT_Invisible);
660     if (m_pHorzScrollBar)
661       m_pHorzScrollBar->SetStates(FWL_STATE_WGT_Invisible);
662   }
663   m_Properties.m_dwStates &= ~FWL_STATE_WGT_Focused;
664   RepaintRect(m_ClientRect);
665 }
666 
OnLButtonDown(CFWL_MessageMouse * pMsg)667 void CFWL_ListBox::OnLButtonDown(CFWL_MessageMouse* pMsg) {
668   m_bLButtonDown = true;
669 
670   Item* pItem = GetItemAtPoint(pMsg->m_pos);
671   if (!pItem)
672     return;
673 
674   if (IsMultiSelection()) {
675     if (pMsg->m_dwFlags & XFA_FWL_KeyFlag::kCtrl) {
676       pItem->SetSelected(!pItem->IsSelected());
677       m_hAnchor = pItem;
678     } else if (pMsg->m_dwFlags & XFA_FWL_KeyFlag::kShift) {
679       if (m_hAnchor)
680         SetSelection(m_hAnchor, pItem, true);
681       else
682         pItem->SetSelected(true);
683     } else {
684       SetSelection(pItem, pItem, true);
685       m_hAnchor = pItem;
686     }
687   } else {
688     SetSelection(pItem, pItem, true);
689   }
690 
691   SetFocusItem(pItem);
692   ScrollToVisible(pItem);
693   SetGrab(true);
694   RepaintRect(m_ClientRect);
695 }
696 
OnLButtonUp(CFWL_MessageMouse * pMsg)697 void CFWL_ListBox::OnLButtonUp(CFWL_MessageMouse* pMsg) {
698   if (!m_bLButtonDown)
699     return;
700 
701   m_bLButtonDown = false;
702   SetGrab(false);
703 }
704 
OnMouseWheel(CFWL_MessageMouseWheel * pMsg)705 void CFWL_ListBox::OnMouseWheel(CFWL_MessageMouseWheel* pMsg) {
706   if (IsShowVertScrollBar())
707     m_pVertScrollBar->GetDelegate()->OnProcessMessage(pMsg);
708 }
709 
OnKeyDown(CFWL_MessageKey * pMsg)710 void CFWL_ListBox::OnKeyDown(CFWL_MessageKey* pMsg) {
711   auto dwKeyCode = static_cast<XFA_FWL_VKEYCODE>(pMsg->m_dwKeyCodeOrChar);
712   switch (dwKeyCode) {
713     case XFA_FWL_VKEY_Tab:
714     case XFA_FWL_VKEY_Up:
715     case XFA_FWL_VKEY_Down:
716     case XFA_FWL_VKEY_Home:
717     case XFA_FWL_VKEY_End: {
718       Item* pItem = GetListItem(GetFocusedItem(), dwKeyCode);
719       bool bShift = !!(pMsg->m_dwFlags & XFA_FWL_KeyFlag::kShift);
720       bool bCtrl = !!(pMsg->m_dwFlags & XFA_FWL_KeyFlag::kCtrl);
721       OnVK(pItem, bShift, bCtrl);
722       break;
723     }
724     default:
725       break;
726   }
727 }
728 
OnVK(Item * pItem,bool bShift,bool bCtrl)729 void CFWL_ListBox::OnVK(Item* pItem, bool bShift, bool bCtrl) {
730   if (!pItem)
731     return;
732 
733   if (IsMultiSelection()) {
734     if (bCtrl) {
735       // Do nothing.
736     } else if (bShift) {
737       if (m_hAnchor)
738         SetSelection(m_hAnchor, pItem, true);
739       else
740         pItem->SetSelected(true);
741     } else {
742       SetSelection(pItem, pItem, true);
743       m_hAnchor = pItem;
744     }
745   } else {
746     SetSelection(pItem, pItem, true);
747   }
748 
749   SetFocusItem(pItem);
750   ScrollToVisible(pItem);
751   RepaintRect(CFX_RectF(0, 0, m_WidgetRect.width, m_WidgetRect.height));
752 }
753 
OnScroll(CFWL_ScrollBar * pScrollBar,CFWL_EventScroll::Code dwCode,float fPos)754 bool CFWL_ListBox::OnScroll(CFWL_ScrollBar* pScrollBar,
755                             CFWL_EventScroll::Code dwCode,
756                             float fPos) {
757   float fMin;
758   float fMax;
759   pScrollBar->GetRange(&fMin, &fMax);
760   float iCurPos = pScrollBar->GetPos();
761   float fStep = pScrollBar->GetStepSize();
762   switch (dwCode) {
763     case CFWL_EventScroll::Code::Min: {
764       fPos = fMin;
765       break;
766     }
767     case CFWL_EventScroll::Code::Max: {
768       fPos = fMax;
769       break;
770     }
771     case CFWL_EventScroll::Code::StepBackward: {
772       fPos -= fStep;
773       if (fPos < fMin + fStep / 2)
774         fPos = fMin;
775       break;
776     }
777     case CFWL_EventScroll::Code::StepForward: {
778       fPos += fStep;
779       if (fPos > fMax - fStep / 2)
780         fPos = fMax;
781       break;
782     }
783     case CFWL_EventScroll::Code::PageBackward: {
784       fPos -= pScrollBar->GetPageSize();
785       if (fPos < fMin)
786         fPos = fMin;
787       break;
788     }
789     case CFWL_EventScroll::Code::PageForward: {
790       fPos += pScrollBar->GetPageSize();
791       if (fPos > fMax)
792         fPos = fMax;
793       break;
794     }
795     case CFWL_EventScroll::Code::Pos:
796     case CFWL_EventScroll::Code::TrackPos:
797     case CFWL_EventScroll::Code::None:
798       break;
799     case CFWL_EventScroll::Code::EndScroll:
800       return false;
801   }
802   if (iCurPos != fPos) {
803     pScrollBar->SetPos(fPos);
804     pScrollBar->SetTrackPos(fPos);
805     RepaintRect(m_ClientRect);
806   }
807   return true;
808 }
809 
CountItems(const CFWL_Widget * pWidget) const810 int32_t CFWL_ListBox::CountItems(const CFWL_Widget* pWidget) const {
811   return fxcrt::CollectionSize<int32_t>(m_ItemArray);
812 }
813 
GetItem(const CFWL_Widget * pWidget,int32_t nIndex) const814 CFWL_ListBox::Item* CFWL_ListBox::GetItem(const CFWL_Widget* pWidget,
815                                           int32_t nIndex) const {
816   if (nIndex < 0 || nIndex >= CountItems(pWidget))
817     return nullptr;
818   return m_ItemArray[nIndex].get();
819 }
820 
GetItemIndex(CFWL_Widget * pWidget,Item * pItem)821 int32_t CFWL_ListBox::GetItemIndex(CFWL_Widget* pWidget, Item* pItem) {
822   auto it = std::find_if(m_ItemArray.begin(), m_ItemArray.end(),
823                          [pItem](const std::unique_ptr<Item>& candidate) {
824                            return candidate.get() == pItem;
825                          });
826   return it != m_ItemArray.end()
827              ? pdfium::base::checked_cast<int32_t>(it - m_ItemArray.begin())
828              : -1;
829 }
830 
AddString(const WideString & wsAdd)831 CFWL_ListBox::Item* CFWL_ListBox::AddString(const WideString& wsAdd) {
832   m_ItemArray.push_back(std::make_unique<Item>(wsAdd));
833   return m_ItemArray.back().get();
834 }
835 
RemoveAt(int32_t iIndex)836 void CFWL_ListBox::RemoveAt(int32_t iIndex) {
837   if (iIndex < 0 || static_cast<size_t>(iIndex) >= m_ItemArray.size())
838     return;
839   m_ItemArray.erase(m_ItemArray.begin() + iIndex);
840 }
841 
DeleteString(Item * pItem)842 void CFWL_ListBox::DeleteString(Item* pItem) {
843   int32_t nIndex = GetItemIndex(this, pItem);
844   if (nIndex < 0 || static_cast<size_t>(nIndex) >= m_ItemArray.size())
845     return;
846 
847   int32_t iSel = nIndex + 1;
848   if (iSel >= CountItems(this))
849     iSel = nIndex - 1;
850   if (iSel >= 0) {
851     Item* item = GetItem(this, iSel);
852     if (item)
853       item->SetSelected(true);
854   }
855   m_ItemArray.erase(m_ItemArray.begin() + nIndex);
856 }
857 
DeleteAll()858 void CFWL_ListBox::DeleteAll() {
859   m_ItemArray.clear();
860 }
861 
Item(const WideString & text)862 CFWL_ListBox::Item::Item(const WideString& text) : m_wsText(text) {}
863 
864 CFWL_ListBox::Item::~Item() = default;
865