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
cxfa_fflistbox.cpp
Go to the documentation of this file.
1// Copyright 2017 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/fxfa/cxfa_fflistbox.h"
8
9#include <algorithm>
10#include <utility>
11#include <vector>
12
13#include "core/fxcrt/stl_util.h"
14#include "third_party/base/check.h"
15#include "v8/include/cppgc/visitor.h"
16#include "xfa/fwl/cfwl_listbox.h"
17#include "xfa/fwl/cfwl_notedriver.h"
18#include "xfa/fwl/cfwl_widget.h"
19#include "xfa/fxfa/cxfa_eventparam.h"
20#include "xfa/fxfa/parser/cxfa_para.h"
21
22namespace {
23
24CFWL_ListBox* ToListBox(CFWL_Widget* widget) {
25 return static_cast<CFWL_ListBox*>(widget);
26}
27
28} // namespace
29
30CXFA_FFListBox::CXFA_FFListBox(CXFA_Node* pNode) : CXFA_FFDropDown(pNode) {}
31
32CXFA_FFListBox::~CXFA_FFListBox() = default;
33
34void CXFA_FFListBox::PreFinalize() {
35 if (GetNormalWidget()) {
36 CFWL_NoteDriver* pNoteDriver =
37 GetNormalWidget()->GetFWLApp()->GetNoteDriver();
38 pNoteDriver->UnregisterEventTarget(GetNormalWidget());
39 }
40}
41
42void CXFA_FFListBox::Trace(cppgc::Visitor* visitor) const {
43 CXFA_FFDropDown::Trace(visitor);
44 visitor->Trace(m_pOldDelegate);
45}
46
47bool CXFA_FFListBox::LoadWidget() {
48 DCHECK(!IsLoaded());
49
50 CFWL_ListBox* pListBox = cppgc::MakeGarbageCollected<CFWL_ListBox>(
51 GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp(),
52 CFWL_Widget::Properties(), nullptr);
54 0xFFFFFFFF);
55 SetNormalWidget(pListBox);
56 pListBox->SetAdapterIface(this);
57
58 CFWL_NoteDriver* pNoteDriver = pListBox->GetFWLApp()->GetNoteDriver();
59 pNoteDriver->RegisterEventTarget(pListBox, pListBox);
60 m_pOldDelegate = pListBox->GetDelegate();
61 pListBox->SetDelegate(this);
62
63 {
64 CFWL_Widget::ScopedUpdateLock update_lock(pListBox);
65 std::vector<WideString> displayables = m_pNode->GetChoiceListItems(false);
66 std::vector<WideString> settables = m_pNode->GetChoiceListItems(true);
67 if (displayables.size() > settables.size())
68 displayables.resize(settables.size());
69
70 for (const auto& label : displayables)
71 pListBox->AddString(label);
72
73 uint32_t dwExtendedStyle = FWL_STYLEEXT_LTB_ShowScrollBarFocus;
74 if (m_pNode->IsChoiceListMultiSelect())
75 dwExtendedStyle |= FWL_STYLEEXT_LTB_MultiSelection;
76
77 dwExtendedStyle |= GetAlignment();
78 pListBox->ModifyStyleExts(dwExtendedStyle, 0xFFFFFFFF);
79 for (int32_t selected : m_pNode->GetSelectedItems())
80 pListBox->SetSelItem(pListBox->GetItem(nullptr, selected), true);
81 }
82
84}
85
86bool CXFA_FFListBox::OnKillFocus(CXFA_FFWidget* pNewFocus) {
89
90 return pNewFocus && CXFA_FFField::OnKillFocus(pNewFocus);
91}
92
93bool CXFA_FFListBox::CommitData() {
94 auto* pListBox = ToListBox(GetNormalWidget());
95 std::vector<int32_t> iSelArray;
96 int32_t iSels = pListBox->CountSelItems();
97 for (int32_t i = 0; i < iSels; ++i)
98 iSelArray.push_back(pListBox->GetSelIndex(i));
99
100 m_pNode->SetSelectedItems(iSelArray, true, false, true);
101 return true;
102}
103
104bool CXFA_FFListBox::IsDataChanged() {
105 std::vector<int32_t> iSelArray = m_pNode->GetSelectedItems();
106 int32_t iOldSels = fxcrt::CollectionSize<int32_t>(iSelArray);
107 auto* pListBox = ToListBox(GetNormalWidget());
108 int32_t iSels = pListBox->CountSelItems();
109 if (iOldSels != iSels)
110 return true;
111
112 for (int32_t i = 0; i < iSels; ++i) {
113 CFWL_ListBox::Item* hlistItem = pListBox->GetItem(nullptr, iSelArray[i]);
114 if (!hlistItem->IsSelected())
115 return true;
116 }
117 return false;
118}
119
120uint32_t CXFA_FFListBox::GetAlignment() {
121 CXFA_Para* para = m_pNode->GetParaIfExists();
122 if (!para)
123 return 0;
124
125 uint32_t dwExtendedStyle = 0;
126 switch (para->GetHorizontalAlign()) {
127 case XFA_AttributeValue::Center:
128 dwExtendedStyle |= FWL_STYLEEXT_LTB_CenterAlign;
129 break;
130 case XFA_AttributeValue::Justify:
131 break;
132 case XFA_AttributeValue::JustifyAll:
133 break;
134 case XFA_AttributeValue::Radix:
135 break;
136 case XFA_AttributeValue::Right:
137 dwExtendedStyle |= FWL_STYLEEXT_LTB_RightAlign;
138 break;
139 default:
140 dwExtendedStyle |= FWL_STYLEEXT_LTB_LeftAlign;
141 break;
142 }
143 return dwExtendedStyle;
144}
145
146bool CXFA_FFListBox::UpdateFWLData() {
147 auto* pListBox = ToListBox(GetNormalWidget());
148 if (!pListBox)
149 return false;
150
151 std::vector<int32_t> iSelArray = m_pNode->GetSelectedItems();
152 std::vector<CFWL_ListBox::Item*> selItemArray(iSelArray.size());
153 std::transform(iSelArray.begin(), iSelArray.end(), selItemArray.begin(),
154 [pListBox](int32_t val) { return pListBox->GetSelItem(val); });
155
156 pListBox->SetSelItem(pListBox->GetSelItem(-1), false);
157 for (CFWL_ListBox::Item* pItem : selItemArray)
158 pListBox->SetSelItem(pItem, true);
159
160 GetNormalWidget()->Update();
161 return true;
162}
163
164void CXFA_FFListBox::OnSelectChanged(CFWL_Widget* pWidget) {
166 eParam.m_wsPrevText = m_pNode->GetValue(XFA_ValuePicture::kRaw);
167 m_pNode->ProcessEvent(GetDocView(), XFA_AttributeValue::Change, &eParam);
168}
169
170void CXFA_FFListBox::SetItemState(int32_t nIndex, bool bSelected) {
171 auto* pListBox = ToListBox(GetNormalWidget());
172 pListBox->SetSelItem(pListBox->GetSelItem(nIndex), bSelected);
173 GetNormalWidget()->Update();
175}
176
177void CXFA_FFListBox::InsertItem(const WideString& wsLabel, int32_t nIndex) {
178 ToListBox(GetNormalWidget())->AddString(wsLabel);
179 GetNormalWidget()->Update();
181}
182
183void CXFA_FFListBox::DeleteItem(int32_t nIndex) {
184 auto* pListBox = ToListBox(GetNormalWidget());
185 if (nIndex < 0)
186 pListBox->DeleteAll();
187 else
188 pListBox->DeleteString(pListBox->GetItem(nullptr, nIndex));
189
190 pListBox->Update();
192}
193
194void CXFA_FFListBox::OnProcessMessage(CFWL_Message* pMessage) {
195 m_pOldDelegate->OnProcessMessage(pMessage);
196}
197
198void CXFA_FFListBox::OnProcessEvent(CFWL_Event* pEvent) {
200 switch (pEvent->GetType()) {
202 OnSelectChanged(GetNormalWidget());
203 break;
204 default:
205 break;
206 }
207 m_pOldDelegate->OnProcessEvent(pEvent);
208}
209
210void CXFA_FFListBox::OnDrawWidget(CFGAS_GEGraphics* pGraphics,
211 const CFX_Matrix& matrix) {
212 m_pOldDelegate->OnDrawWidget(pGraphics, matrix);
213}
214
216 return FormFieldType::kXFA_ListBox;
217}
#define FWL_STYLEEXT_LTB_MultiSelection
#define FWL_STYLEEXT_LTB_ShowScrollBarFocus
#define FWL_STYLEEXT_LTB_CenterAlign
#define FWL_STYLEEXT_LTB_RightAlign
#define FWL_STYLEEXT_LTB_LeftAlign
#define FWL_STYLE_WGT_NoBackground
Definition cfwl_widget.h:39
#define FWL_STYLE_WGT_VScroll
Definition cfwl_widget.h:37
Type GetType() const
Definition cfwl_event.h:39
bool IsSelected() const
void UnregisterEventTarget(CFWL_Widget *pListener)
void RegisterEventTarget(CFWL_Widget *pListener, CFWL_Widget *pEventSource)
void SetDelegate(IFWL_WidgetDelegate *delegate)
void SetAdapterIface(AdapterIface *pItem)
virtual void ModifyStyleExts(uint32_t dwStyleExtsAdded, uint32_t dwStyleExtsRemoved)
void ModifyStyles(uint32_t dwStylesAdded, uint32_t dwStylesRemoved)
CFWL_App * GetFWLApp() const
CXFA_EventParam(XFA_EVENTTYPE type)
CXFA_FFDropDown(CXFA_Node *pNode)
bool OnKillFocus(CXFA_FFWidget *pNewWidget) override
bool ProcessCommittedData()
bool LoadWidget() override
void SetNormalWidget(CFWL_Widget *widget)
void OnProcessEvent(CFWL_Event *pEvent) override
bool UpdateFWLData() override
void DeleteItem(int32_t nIndex) override
void InsertItem(const WideString &wsLabel, int32_t nIndex) override
~CXFA_FFListBox() override
void OnProcessMessage(CFWL_Message *pMessage) override
void SetItemState(int32_t nIndex, bool bSelected)
void OnProcessEvent(CFWL_Event *pEvent) override
void OnSelectChanged(CFWL_Widget *pWidget)
bool CommitData() override
bool OnKillFocus(CXFA_FFWidget *pNewWidget) override
FormFieldType GetFormFieldType() override
void OnDrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
bool LoadWidget() override
bool IsDataChanged() override
XFA_AttributeValue GetHorizontalAlign()
Definition cxfa_para.cpp:57
FormFieldType
@ XFA_EVENT_Change
XFA_AttributeValue
Definition fxfa_basic.h:60
Definition heap.h:12