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