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