Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfwl_listbox.cpp
Go to the documentation of this file.
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
28namespace {
29
30const int kItemTextMargin = 2;
31
32} // namespace
33
35 const Properties& properties,
36 CFWL_Widget* pOuter)
37 : CFWL_Widget(app, properties, pOuter) {}
38
39CFWL_ListBox::~CFWL_ListBox() = default;
40
41void CFWL_ListBox::Trace(cppgc::Visitor* visitor) const {
42 CFWL_Widget::Trace(visitor);
43 visitor->Trace(m_pHorzScrollBar);
44 visitor->Trace(m_pVertScrollBar);
45}
46
50
52 if (IsLocked())
53 return;
54
57 m_iTTOAligns = FDE_TextAlignment::kCenterLeft;
58 break;
61 break;
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
72FWL_WidgetHit CFWL_ListBox::HitTest(const CFX_PointF& point) {
74 CFX_RectF rect = m_pHorzScrollBar->GetWidgetRect();
75 if (rect.Contains(point))
77 }
79 CFX_RectF rect = m_pVertScrollBar->GetWidgetRect();
80 if (rect.Contains(point))
82 }
83 if (m_ClientRect.Contains(point))
86}
87
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);
99 rtClip.height -= m_fScorllBarWidth;
101 rtClip.width -= m_fScorllBarWidth;
102
103 pGraphics->SetClipRect(matrix.TransformRect(rtClip));
105 DrawBkground(pGraphics, matrix);
106
107 DrawItems(pGraphics, matrix);
108}
109
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
122 int32_t idx = GetSelIndex(nIndexSel);
123 if (idx < 0)
124 return nullptr;
125 return GetItem(this, idx);
126}
127
128int32_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
144void 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
161 XFA_FWL_VKEYCODE dwKeyCode) {
162 Item* hRet = nullptr;
163 switch (dwKeyCode) {
164 case XFA_FWL_VKEY_Up:
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
190void 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
211bool CFWL_ListBox::IsMultiSelection() const {
213}
214
215void 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
230void 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
243CFWL_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
255void 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
266CFWL_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
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
314void 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;
324 param.m_pRtData = &m_StaticRect;
325 if (!IsEnabled())
326 param.m_dwStates = CFWL_PartState::kDisabled;
327 GetThemeProvider()->DrawBackground(param);
328}
329
330void 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
362void 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;
369 dwPartStates = CFWL_PartState::kDisabled;
370 else if (pItem && pItem->IsSelected())
371 dwPartStates = CFWL_PartState::kSelected;
372
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
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
416CFX_SizeF CFWL_ListBox::CalcSize() {
417 m_ClientRect = GetClientRect();
418 m_ContentRect = m_ClientRect;
419 CFX_RectF rtUIMargin;
420 if (!GetOuter()) {
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)
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);
469 0 ||
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)
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);
499 0 ||
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
517void 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
529float 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
543float CFWL_ListBox::GetScrollWidth() {
544 return GetThemeProvider()->GetScrollBarWidth();
545}
546
549 return GetThemeProvider()->GetFontSize(part) + 2 * kItemTextMargin;
550}
551
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
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
571 return m_pVertScrollBar && m_pVertScrollBar->IsVisible() &&
572 ScrollBarPropertiesPresent();
573}
574
576 return m_pHorzScrollBar && m_pHorzScrollBar->IsVisible() &&
577 ScrollBarPropertiesPresent();
578}
579
584
586 if (!IsEnabled())
587 return;
588
589 switch (pMessage->GetType()) {
591 OnFocusGained();
592 break;
594 OnFocusLost();
595 break;
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 }
611 OnMouseWheel(static_cast<CFWL_MessageMouseWheel*>(pMessage));
612 break;
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())
623}
624
626 if (!pEvent)
627 return;
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
641 const CFX_Matrix& matrix) {
642 DrawWidget(pGraphics, matrix);
643}
644
645void CFWL_ListBox::OnFocusGained() {
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 }
653 RepaintRect(m_ClientRect);
654}
655
656void CFWL_ListBox::OnFocusLost() {
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 }
664 RepaintRect(m_ClientRect);
665}
666
667void 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) {
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);
693 SetGrab(true);
694 RepaintRect(m_ClientRect);
695}
696
697void CFWL_ListBox::OnLButtonUp(CFWL_MessageMouse* pMsg) {
698 if (!m_bLButtonDown)
699 return;
700
701 m_bLButtonDown = false;
702 SetGrab(false);
703}
704
705void CFWL_ListBox::OnMouseWheel(CFWL_MessageMouseWheel* pMsg) {
706 if (IsShowVertScrollBar())
707 m_pVertScrollBar->GetDelegate()->OnProcessMessage(pMsg);
708}
709
710void 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:
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
729void 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);
752}
753
754bool 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
810int32_t CFWL_ListBox::CountItems(const CFWL_Widget* pWidget) const {
811 return fxcrt::CollectionSize<int32_t>(m_ItemArray);
812}
813
815 int32_t nIndex) const {
816 if (nIndex < 0 || nIndex >= CountItems(pWidget))
817 return nullptr;
818 return m_ItemArray[nIndex].get();
819}
820
821int32_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
831CFWL_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
836void 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
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
859 m_ItemArray.clear();
860}
861
862CFWL_ListBox::Item::Item(const WideString& text) : m_wsText(text) {}
863
864CFWL_ListBox::Item::~Item() = default;
FDE_TextAlignment
Definition cfde_data.h:12
#define FWL_STYLEEXT_LTB_MultiSelection
#define FWL_STYLEEXT_LTB_AlignMask
#define FWL_STYLEEXT_LTB_ShowScrollBarFocus
#define FWL_STYLEEXT_LTB_CenterAlign
#define FWL_STYLEEXT_LTB_RightAlign
#define FWL_STYLEEXT_LTB_LeftAlign
#define FWL_STYLEEXT_SCB_Vert
#define FWL_STYLEEXT_SCB_Horz
CFWL_PartState
#define FWL_STYLE_WGT_NoBackground
Definition cfwl_widget.h:39
#define FWL_STATE_WGT_Invisible
Definition cfwl_widget.h:43
#define FWL_STYLE_WGT_VScroll
Definition cfwl_widget.h:37
FWL_Type
Definition cfwl_widget.h:46
#define FWL_STATE_WGT_Focused
Definition cfwl_widget.h:42
#define FWL_STATE_WGT_Disabled
Definition cfwl_widget.h:41
StateRestorer(CFGAS_GEGraphics *graphics)
void SetClipRect(const CFX_RectF &rect)
float GetPos() const
Code GetScrollCode() const
Type GetType() const
Definition cfwl_event.h:39
CFWL_Widget * GetSrcTarget() const
Definition cfwl_event.h:40
void SetSelected(bool enable)
bool IsSelected() const
CFX_RectF GetRect() const
Item(const WideString &text)
void SetFocused(bool enable)
bool IsFocused() const
void SetRect(const CFX_RectF &rect)
WideString GetText() const
bool ScrollBarPropertiesPresent() const
void SetSelItem(Item *hItem, bool bSelect)
FWL_WidgetHit HitTest(const CFX_PointF &point) override
void Trace(cppgc::Visitor *visitor) const override
Item * GetSelItem(int32_t nIndexSel)
void OnDrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
int32_t CountItems(const CFWL_Widget *pWidget) const
bool ScrollToVisible(Item *hItem)
int32_t GetItemIndex(CFWL_Widget *pWidget, Item *pItem)
void DrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
void InitHorizontalScrollBar()
void SetSelection(Item *hStart, Item *hEnd, bool bSelected)
bool IsShowVertScrollBar() const
Item * GetItem(const CFWL_Widget *pWidget, int32_t nIndex) const
void RemoveAt(int32_t iIndex)
FWL_Type GetClassID() const override
bool IsShowHorzScrollBar() const
void OnProcessEvent(CFWL_Event *pEvent) override
int32_t CountSelItems()
Item * GetListItem(Item *hItem, XFA_FWL_VKEYCODE dwKeyCode)
void Update() override
void OnProcessMessage(CFWL_Message *pMessage) override
Item * GetItemAtPoint(const CFX_PointF &point)
~CFWL_ListBox() override
CFWL_ListBox(CFWL_App *pApp, const Properties &properties, CFWL_Widget *pOuter)
void DeleteString(Item *pItem)
void InitVerticalScrollBar()
Item * AddString(const WideString &wsAdd)
float CalcItemHeight()
int32_t GetSelIndex(int32_t nIndex)
const KeyCommand m_dwCmd
const uint32_t m_dwKeyCodeOrChar
const MouseCommand m_dwCmd
Type GetType() const
CFWL_Widget * GetDstTarget() const
float GetPos() const
float GetStepSize() const
void GetRange(float *fMin, float *fMax) const
void SetPos(float fPos)
float GetPageSize() const
void SetTrackPos(float fTrackPos)
CFX_RectF m_PartRect
CFX_Matrix m_matrix
FDE_TextStyle m_dwTTOStyles
FDE_TextAlignment m_iTTOAlign
uint32_t GetStyleExts() const
CFWL_Widget(CFWL_App *app, const Properties &properties, CFWL_Widget *pOuter)
Properties m_Properties
CFWL_Widget * GetOuter() const
void OnProcessMessage(CFWL_Message *pMessage) override
bool HasBorder() const
CFX_RectF m_WidgetRect
void DrawBorder(CFGAS_GEGraphics *pGraphics, CFWL_ThemePart::Part iPartBorder, const CFX_Matrix &pMatrix)
void RepaintRect(const CFX_RectF &pRect)
IFWL_ThemeProvider * GetThemeProvider() const
bool IsEnabled() const
void SetGrab(bool bSet)
virtual CFX_RectF GetClientRect()
bool IsLocked() const
CFX_Matrix & operator=(const CFX_Matrix &other)=default
CFX_RectF TransformRect(const CFX_RectF &rect) const
CFX_RectF(const CFX_RectF &other)=default
void Offset(float dx, float dy)
constexpr CFX_RectF()=default
void Deflate(float off_left, float off_top, float off_right, float off_bottom)
CFX_RectF & operator=(const CFX_RectF &other)=default
float bottom() const
bool Contains(const PointType &p) const
float right() const
constexpr CFX_RectF(float dst_left, float dst_top, float dst_width, float dst_height)
void Deflate(float x, float y)
virtual void DrawBackground(const CFWL_ThemeBackground &pParams)=0
virtual void DrawText(const CFWL_ThemeText &pParams)=0
XFA_FWL_KeyFlag
XFA_FWL_VKEYCODE
@ XFA_FWL_VKEY_Down
@ XFA_FWL_VKEY_Home
@ XFA_FWL_VKEY_End
@ XFA_FWL_VKEY_Tab
@ XFA_FWL_VKEY_Up
FWL_WidgetHit
bool single_line_
Definition cfde_data.h:29