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/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
28namespace pdfium {
29
30namespace {
31
32const int kItemTextMargin = 2;
33
34} // namespace
35
37 const Properties& properties,
38 CFWL_Widget* pOuter)
39 : CFWL_Widget(app, properties, pOuter) {}
40
41CFWL_ListBox::~CFWL_ListBox() = default;
42
43void CFWL_ListBox::Trace(cppgc::Visitor* visitor) const {
44 CFWL_Widget::Trace(visitor);
45 visitor->Trace(m_pHorzScrollBar);
46 visitor->Trace(m_pVertScrollBar);
47}
48
52
54 if (IsLocked())
55 return;
56
59 m_iTTOAligns = FDE_TextAlignment::kCenterLeft;
60 break;
63 break;
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
76 CFX_RectF rect = m_pHorzScrollBar->GetWidgetRect();
77 if (rect.Contains(point))
79 }
81 CFX_RectF rect = m_pVertScrollBar->GetWidgetRect();
82 if (rect.Contains(point))
84 }
85 if (m_ClientRect.Contains(point))
88}
89
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);
101 rtClip.height -= m_fScorllBarWidth;
103 rtClip.width -= m_fScorllBarWidth;
104
105 pGraphics->SetClipRect(matrix.TransformRect(rtClip));
107 DrawBkground(pGraphics, matrix);
108
109 DrawItems(pGraphics, matrix);
110}
111
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
124 int32_t idx = GetSelIndex(nIndexSel);
125 if (idx < 0)
126 return nullptr;
127 return GetItem(this, idx);
128}
129
130int32_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
146void 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
163 XFA_FWL_VKEYCODE dwKeyCode) {
164 Item* hRet = nullptr;
165 switch (dwKeyCode) {
166 case XFA_FWL_VKEY_Up:
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
192void 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
213bool CFWL_ListBox::IsMultiSelection() const {
215}
216
217void 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
232void 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
245CFWL_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
257void 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
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
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
316void 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;
326 param.m_pRtData = &m_StaticRect;
327 if (!IsEnabled())
328 param.m_dwStates = CFWL_PartState::kDisabled;
329 GetThemeProvider()->DrawBackground(param);
330}
331
332void 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
364void 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;
371 dwPartStates = CFWL_PartState::kDisabled;
372 else if (pItem && pItem->IsSelected())
373 dwPartStates = CFWL_PartState::kSelected;
374
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
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
418CFX_SizeF CFWL_ListBox::CalcSize() {
419 m_ClientRect = GetClientRect();
420 m_ContentRect = m_ClientRect;
421 CFX_RectF rtUIMargin;
422 if (!GetOuter()) {
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)
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);
471 0 ||
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)
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);
501 0 ||
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
519void 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
531float 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
545float CFWL_ListBox::GetScrollWidth() {
546 return GetThemeProvider()->GetScrollBarWidth();
547}
548
551 return GetThemeProvider()->GetFontSize(part) + 2 * kItemTextMargin;
552}
553
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
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
573 return m_pVertScrollBar && m_pVertScrollBar->IsVisible() &&
574 ScrollBarPropertiesPresent();
575}
576
578 return m_pHorzScrollBar && m_pHorzScrollBar->IsVisible() &&
579 ScrollBarPropertiesPresent();
580}
581
586
588 if (!IsEnabled())
589 return;
590
591 switch (pMessage->GetType()) {
593 OnFocusGained();
594 break;
596 OnFocusLost();
597 break;
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 }
613 OnMouseWheel(static_cast<CFWL_MessageMouseWheel*>(pMessage));
614 break;
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())
625}
626
628 if (!pEvent)
629 return;
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
643 const CFX_Matrix& matrix) {
644 DrawWidget(pGraphics, matrix);
645}
646
647void CFWL_ListBox::OnFocusGained() {
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 }
655 RepaintRect(m_ClientRect);
656}
657
658void CFWL_ListBox::OnFocusLost() {
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 }
666 RepaintRect(m_ClientRect);
667}
668
669void 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) {
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);
695 SetGrab(true);
696 RepaintRect(m_ClientRect);
697}
698
699void CFWL_ListBox::OnLButtonUp(CFWL_MessageMouse* pMsg) {
700 if (!m_bLButtonDown)
701 return;
702
703 m_bLButtonDown = false;
704 SetGrab(false);
705}
706
707void CFWL_ListBox::OnMouseWheel(CFWL_MessageMouseWheel* pMsg) {
708 if (IsShowVertScrollBar())
709 m_pVertScrollBar->GetDelegate()->OnProcessMessage(pMsg);
710}
711
712void 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:
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
731void 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);
754}
755
756bool 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
812int32_t CFWL_ListBox::CountItems(const CFWL_Widget* pWidget) const {
813 return fxcrt::CollectionSize<int32_t>(m_ItemArray);
814}
815
817 int32_t nIndex) const {
818 if (nIndex < 0 || nIndex >= CountItems(pWidget))
819 return nullptr;
820 return m_ItemArray[nIndex].get();
821}
822
823int32_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
834 m_ItemArray.push_back(std::make_unique<Item>(wsAdd));
835 return m_ItemArray.back().get();
836}
837
838void 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
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
861 m_ItemArray.clear();
862}
863
865
866CFWL_ListBox::Item::~Item() = default;
867
868} // namespace pdfium
#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
#define FWL_STYLE_WGT_NoBackground
Definition cfwl_widget.h:41
#define FWL_STATE_WGT_Invisible
Definition cfwl_widget.h:45
#define FWL_STYLE_WGT_VScroll
Definition cfwl_widget.h:39
#define FWL_STATE_WGT_Focused
Definition cfwl_widget.h:44
#define FWL_STATE_WGT_Disabled
Definition cfwl_widget.h:43
StateRestorer(CFGAS_GEGraphics *graphics)
void SetClipRect(const CFX_RectF &rect)
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)
Type GetType() const
Definition cfwl_event.h:41
CFWL_Widget * GetSrcTarget() const
Definition cfwl_event.h:42
Item(const WideString &text)
WideString GetText() const
CFX_RectF GetRect() const
void SetSelected(bool enable)
void SetRect(const CFX_RectF &rect)
void SetFocused(bool enable)
void OnDrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
bool ScrollBarPropertiesPresent() const
bool IsShowHorzScrollBar() const
void Update() override
int32_t GetSelIndex(int32_t nIndex)
Item * GetItemAtPoint(const CFX_PointF &point)
Item * GetItem(const CFWL_Widget *pWidget, int32_t nIndex) const
void DeleteString(Item *pItem)
FWL_Type GetClassID() const override
bool ScrollToVisible(Item *hItem)
void SetSelItem(Item *hItem, bool bSelect)
void RemoveAt(int32_t iIndex)
Item * GetListItem(Item *hItem, XFA_FWL_VKEYCODE dwKeyCode)
CFWL_ListBox(CFWL_App *pApp, const Properties &properties, CFWL_Widget *pOuter)
FWL_WidgetHit HitTest(const CFX_PointF &point) override
Item * GetSelItem(int32_t nIndexSel)
void OnProcessMessage(CFWL_Message *pMessage) override
void DrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
int32_t CountItems(const CFWL_Widget *pWidget) const
void SetSelection(Item *hStart, Item *hEnd, bool bSelected)
Item * AddString(const WideString &wsAdd)
~CFWL_ListBox() override
void Trace(cppgc::Visitor *visitor) const override
bool IsShowVertScrollBar() const
void OnProcessEvent(CFWL_Event *pEvent) override
int32_t GetItemIndex(CFWL_Widget *pWidget, Item *pItem)
const uint32_t m_dwKeyCodeOrChar
const KeyCommand m_dwCmd
Type GetType() const
CFWL_Widget * GetDstTarget() const
void SetTrackPos(float fTrackPos)
void SetPos(float fPos)
float GetPageSize() const
float GetStepSize() const
void GetRange(float *fMin, float *fMax) const
FDE_TextStyle m_dwTTOStyles
FDE_TextAlignment m_iTTOAlign
void OnProcessMessage(CFWL_Message *pMessage) override
void RepaintRect(const CFX_RectF &pRect)
bool HasBorder() const
CFWL_Widget * GetOuter() const
void SetGrab(bool bSet)
CFWL_Widget(CFWL_App *app, const Properties &properties, CFWL_Widget *pOuter)
void DrawBorder(CFGAS_GEGraphics *pGraphics, CFWL_ThemePart::Part iPartBorder, const CFX_Matrix &pMatrix)
virtual CFX_RectF GetClientRect()
uint32_t GetStyleExts() const
IFWL_ThemeProvider * GetThemeProvider() const
bool IsLocked() const
Properties m_Properties
bool IsEnabled() const
virtual void DrawText(const CFWL_ThemeText &pParams)=0
virtual void DrawBackground(const CFWL_ThemeBackground &pParams)=0
CFX_PTemplate< float > CFX_PointF
CFX_STemplate< float > CFX_SizeF
FDE_TextAlignment
Definition cfde_data.h:14
@ XFA_FWL_VKEY_Up
@ XFA_FWL_VKEY_Home
@ XFA_FWL_VKEY_Tab
@ XFA_FWL_VKEY_End
@ XFA_FWL_VKEY_Down
fxcrt::WideString WideString
Definition widestring.h:207