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
cffl_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 "fpdfsdk/formfiller/cffl_listbox.h"
8
9#include <utility>
10
11#include "constants/form_flags.h"
12#include "core/fpdfdoc/cpdf_bafontmap.h"
13#include "core/fxcrt/containers/contains.h"
14#include "fpdfsdk/cpdfsdk_widget.h"
15#include "fpdfsdk/formfiller/cffl_interactiveformfiller.h"
16#include "fpdfsdk/formfiller/cffl_perwindowdata.h"
17#include "fpdfsdk/pwl/cpwl_list_box.h"
18
19CFFL_ListBox::CFFL_ListBox(CFFL_InteractiveFormFiller* pFormFiller,
20 CPDFSDK_Widget* pWidget)
21 : CFFL_TextObject(pFormFiller, pWidget) {}
22
23CFFL_ListBox::~CFFL_ListBox() = default;
24
27 uint32_t dwFieldFlag = m_pWidget->GetFieldFlags();
30
32
33 if (cp.dwFlags & PWS_AUTOFONTSIZE) {
34 constexpr float kDefaultListBoxFontSize = 12.0f;
35 cp.fFontSize = kDefaultListBoxFontSize;
36 }
37
38 cp.pFontMap = GetOrCreateFontMap();
39 return cp;
40}
41
43 const CPWL_Wnd::CreateParams& cp,
44 std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData) {
45 static_cast<CFFL_PerWindowData*>(pAttachedData.get())->SetFormField(this);
46 auto pWnd = std::make_unique<CPWL_ListBox>(cp, std::move(pAttachedData));
47 pWnd->Realize();
48
49 for (int32_t i = 0, sz = m_pWidget->CountOptions(); i < sz; i++)
50 pWnd->AddString(m_pWidget->GetOptionLabel(i));
51
52 if (pWnd->HasFlag(PLBS_MULTIPLESEL)) {
53 m_OriginSelections.clear();
54
55 bool bSetCaret = false;
56 for (int32_t i = 0, sz = m_pWidget->CountOptions(); i < sz; i++) {
57 if (m_pWidget->IsOptionSelected(i)) {
58 if (!bSetCaret) {
59 pWnd->SetCaret(i);
60 bSetCaret = true;
61 }
62 pWnd->Select(i);
63 m_OriginSelections.insert(i);
64 }
65 }
66 } else {
67 for (int i = 0, sz = m_pWidget->CountOptions(); i < sz; i++) {
68 if (m_pWidget->IsOptionSelected(i)) {
69 pWnd->Select(i);
70 break;
71 }
72 }
73 }
74
75 pWnd->SetTopVisibleIndex(m_pWidget->GetTopVisibleIndex());
76 return pWnd;
77}
78
79bool CFFL_ListBox::OnChar(CPDFSDK_Widget* pWidget,
80 uint32_t nChar,
81 Mask<FWL_EVENTFLAG> nFlags) {
82 return CFFL_TextObject::OnChar(pWidget, nChar, nFlags);
83}
84
85bool CFFL_ListBox::IsDataChanged(const CPDFSDK_PageView* pPageView) {
86 CPWL_ListBox* pListBox = GetPWLListBox(pPageView);
87 if (!pListBox)
88 return false;
89
90 if (m_pWidget->GetFieldFlags() & pdfium::form_flags::kChoiceMultiSelect) {
91 size_t nSelCount = 0;
92 for (int32_t i = 0, sz = pListBox->GetCount(); i < sz; ++i) {
93 if (pListBox->IsItemSelected(i)) {
94 if (!pdfium::Contains(m_OriginSelections, i))
95 return true;
96
97 ++nSelCount;
98 }
99 }
100
101 return nSelCount != m_OriginSelections.size();
102 }
103 return pListBox->GetCurSel() != m_pWidget->GetSelectedIndex(0);
104}
105
106void CFFL_ListBox::SaveData(const CPDFSDK_PageView* pPageView) {
107 CPWL_ListBox* pListBox = GetPWLListBox(pPageView);
108 if (!pListBox) {
109 return;
110 }
111 int32_t nNewTopIndex = pListBox->GetTopVisibleIndex();
112 ObservedPtr<CPWL_ListBox> observed_box(pListBox);
113 ObservedPtr<CPDFSDK_Widget> observed_widget(m_pWidget);
114 observed_widget->ClearSelection();
115 if (!observed_box || !observed_widget) {
116 return;
117 }
118 if (observed_widget->GetFieldFlags() &
120 for (int32_t i = 0, sz = observed_box->GetCount(); i < sz; i++) {
121 if (observed_box->IsItemSelected(i)) {
122 observed_widget->SetOptionSelection(i);
123 if (!observed_box || !observed_widget) {
124 return;
125 }
126 }
127 }
128 } else {
129 observed_widget->SetOptionSelection(observed_box->GetCurSel());
130 if (!observed_box || !observed_widget) {
131 return;
132 }
133 }
134 ObservedPtr<CFFL_ListBox> observed_this(this);
135 observed_widget->SetTopVisibleIndex(nNewTopIndex);
136 if (!observed_widget) {
137 return;
138 }
139 observed_widget->ResetFieldAppearance();
140 if (!observed_widget) {
141 return;
142 }
143 observed_widget->UpdateField();
144 if (!observed_widget || !observed_this) {
145 return;
146 }
147 observed_this->SetChangeMark();
148}
149
150void CFFL_ListBox::GetActionData(const CPDFSDK_PageView* pPageView,
152 CFFL_FieldAction& fa) {
153 switch (type) {
155 if (m_pWidget->GetFieldFlags() & pdfium::form_flags::kChoiceMultiSelect) {
156 fa.sValue.clear();
157 } else {
158 CPWL_ListBox* pListBox = GetPWLListBox(pPageView);
159 if (pListBox) {
160 int32_t nCurSel = pListBox->GetCurSel();
161 if (nCurSel >= 0)
162 fa.sValue = m_pWidget->GetOptionLabel(nCurSel);
163 }
164 }
165 break;
168 if (m_pWidget->GetFieldFlags() & pdfium::form_flags::kChoiceMultiSelect) {
169 fa.sValue.clear();
170 } else {
171 int32_t nCurSel = m_pWidget->GetSelectedIndex(0);
172 if (nCurSel >= 0)
173 fa.sValue = m_pWidget->GetOptionLabel(nCurSel);
174 }
175 break;
176 default:
177 break;
178 }
179}
180
181void CFFL_ListBox::SavePWLWindowState(const CPDFSDK_PageView* pPageView) {
182 CPWL_ListBox* pListBox = GetPWLListBox(pPageView);
183 if (!pListBox)
184 return;
185
186 for (int32_t i = 0, sz = pListBox->GetCount(); i < sz; i++) {
187 if (pListBox->IsItemSelected(i))
188 m_State.push_back(i);
189 }
190}
191
193 const CPDFSDK_PageView* pPageView) {
194 CPWL_ListBox* pListBox = CreateOrUpdatePWLListBox(pPageView);
195 if (!pListBox)
196 return;
197
198 for (const auto& item : m_State)
199 pListBox->Select(item);
200}
201
202bool CFFL_ListBox::SetIndexSelected(int index, bool selected) {
203 if (!IsValid())
204 return false;
205
206 if (index < 0 || index >= m_pWidget->CountOptions())
207 return false;
208
209 CPWL_ListBox* pListBox = GetPWLListBox(GetCurPageView());
210 if (!pListBox)
211 return false;
212
213 if (selected) {
214 pListBox->Select(index);
215 pListBox->SetCaret(index);
216 } else {
217 pListBox->Deselect(index);
218 pListBox->SetCaret(index);
219 }
220
221 return true;
222}
223
224bool CFFL_ListBox::IsIndexSelected(int index) {
225 if (!IsValid())
226 return false;
227
228 if (index < 0 || index >= m_pWidget->CountOptions())
229 return false;
230
231 CPWL_ListBox* pListBox = GetPWLListBox(GetCurPageView());
232 return pListBox && pListBox->IsItemSelected(index);
233}
234
235CPWL_ListBox* CFFL_ListBox::GetPWLListBox(
236 const CPDFSDK_PageView* pPageView) const {
237 return static_cast<CPWL_ListBox*>(GetPWLWindow(pPageView));
238}
239
240CPWL_ListBox* CFFL_ListBox::CreateOrUpdatePWLListBox(
241 const CPDFSDK_PageView* pPageView) {
242 return static_cast<CPWL_ListBox*>(CreateOrUpdatePWLWindow(pPageView));
243}
virtual bool OnChar(CPDFSDK_Widget *pAnnot, uint32_t nChar, Mask< FWL_EVENTFLAG > nFlags)
CPWL_Wnd * GetPWLWindow(const CPDFSDK_PageView *pPageView) const
CPWL_Wnd * CreateOrUpdatePWLWindow(const CPDFSDK_PageView *pPageView)
virtual CPWL_Wnd::CreateParams GetCreateParam()
CPDFSDK_PageView * GetCurPageView()
bool IsValid() const
bool IsDataChanged(const CPDFSDK_PageView *pPageView) override
CFFL_ListBox(CFFL_InteractiveFormFiller *pFormFiller, CPDFSDK_Widget *pWidget)
void GetActionData(const CPDFSDK_PageView *pPageView, CPDF_AAction::AActionType type, CFFL_FieldAction &fa) override
bool SetIndexSelected(int index, bool selected) override
void SavePWLWindowState(const CPDFSDK_PageView *pPageView) override
std::unique_ptr< CPWL_Wnd > NewPWLWindow(const CPWL_Wnd::CreateParams &cp, std::unique_ptr< IPWL_FillerNotify::PerWindowData > pAttachedData) override
void SaveData(const CPDFSDK_PageView *pPageView) override
~CFFL_ListBox() override
bool IsIndexSelected(int index) override
bool OnChar(CPDFSDK_Widget *pWidget, uint32_t nChar, Mask< FWL_EVENTFLAG > nFlags) override
void RecreatePWLWindowFromSavedState(const CPDFSDK_PageView *pPageView) override
CPWL_Wnd::CreateParams GetCreateParam() override
void SetFormField(CFFL_FormField *pFormField)
CPDF_BAFontMap * GetOrCreateFontMap()
CFFL_TextObject(CFFL_InteractiveFormFiller *pFormFiller, CPDFSDK_Widget *pWidget)
int32_t GetCurSel() const
void Deselect(int32_t nItemIndex)
void Select(int32_t nItemIndex)
int32_t GetTopVisibleIndex() const
void SetCaret(int32_t nItemIndex)
int32_t GetCount() const
bool IsItemSelected(int32_t nItemIndex) const
#define PWS_VSCROLL
Definition cpwl_wnd.h:32
#define PWS_AUTOFONTSIZE
Definition cpwl_wnd.h:35
#define PLBS_MULTIPLESEL
Definition cpwl_wnd.h:55
constexpr uint32_t kChoiceMultiSelect
Definition form_flags.h:39