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_ffcombobox.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/fxfa/cxfa_ffcombobox.h"
8
9#include <utility>
10#include <vector>
11
12#include "third_party/base/check.h"
13#include "v8/include/cppgc/visitor.h"
14#include "xfa/fwl/cfwl_combobox.h"
15#include "xfa/fwl/cfwl_eventselectchanged.h"
16#include "xfa/fwl/cfwl_notedriver.h"
17#include "xfa/fxfa/cxfa_eventparam.h"
18#include "xfa/fxfa/cxfa_ffdocview.h"
19#include "xfa/fxfa/parser/cxfa_para.h"
20
21namespace {
22
23CFWL_ComboBox* ToComboBox(CFWL_Widget* widget) {
24 return static_cast<CFWL_ComboBox*>(widget);
25}
26
27const CFWL_ComboBox* ToComboBox(const CFWL_Widget* widget) {
28 return static_cast<const CFWL_ComboBox*>(widget);
29}
30
31} // namespace
32
33CXFA_FFComboBox::CXFA_FFComboBox(CXFA_Node* pNode) : CXFA_FFDropDown(pNode) {}
34
35CXFA_FFComboBox::~CXFA_FFComboBox() = default;
36
37void CXFA_FFComboBox::Trace(cppgc::Visitor* visitor) const {
38 CXFA_FFDropDown::Trace(visitor);
39 visitor->Trace(m_pOldDelegate);
40}
41
42CXFA_FFComboBox* CXFA_FFComboBox::AsComboBox() {
43 return this;
44}
45
46CFX_RectF CXFA_FFComboBox::GetBBox(FocusOption focus) {
47 if (focus == kDrawFocus)
48 return CFX_RectF();
50}
51
52bool CXFA_FFComboBox::PtInActiveRect(const CFX_PointF& point) {
53 auto* pComboBox = ToComboBox(GetNormalWidget());
54 return pComboBox && pComboBox->GetBBox().Contains(point);
55}
56
57bool CXFA_FFComboBox::LoadWidget() {
58 DCHECK(!IsLoaded());
59
60 CFWL_ComboBox* pComboBox = cppgc::MakeGarbageCollected<CFWL_ComboBox>(
61 GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp());
62 SetNormalWidget(pComboBox);
63 pComboBox->SetAdapterIface(this);
64
65 CFWL_NoteDriver* pNoteDriver = pComboBox->GetFWLApp()->GetNoteDriver();
66 pNoteDriver->RegisterEventTarget(pComboBox, pComboBox);
67 m_pOldDelegate = pComboBox->GetDelegate();
68 pComboBox->SetDelegate(this);
69
70 {
71 CFWL_Widget::ScopedUpdateLock update_lock(pComboBox);
72 for (const auto& label : m_pNode->GetChoiceListItems(false))
73 pComboBox->AddString(label);
74
75 std::vector<int32_t> iSelArray = m_pNode->GetSelectedItems();
76 if (iSelArray.empty())
77 pComboBox->SetEditText(m_pNode->GetValue(XFA_ValuePicture::kRaw));
78 else
79 pComboBox->SetCurSel(iSelArray.front());
80
82 }
83
85}
86
87void CXFA_FFComboBox::UpdateWidgetProperty() {
88 auto* pComboBox = ToComboBox(GetNormalWidget());
89 if (!pComboBox)
90 return;
91
92 uint32_t dwExtendedStyle = 0;
93 uint32_t dwEditStyles = FWL_STYLEEXT_EDT_ReadOnly;
94 dwExtendedStyle |= UpdateUIProperty();
95 if (m_pNode->IsChoiceListAllowTextEntry()) {
96 dwEditStyles &= ~FWL_STYLEEXT_EDT_ReadOnly;
97 dwExtendedStyle |= FWL_STYLEEXT_CMB_DropDown;
98 }
99 if (!m_pNode->IsOpenAccess() || !GetDoc()->GetXFADoc()->IsInteractive()) {
100 dwEditStyles |= FWL_STYLEEXT_EDT_ReadOnly;
101 dwExtendedStyle |= FWL_STYLEEXT_CMB_ReadOnly;
102 }
103 dwExtendedStyle |= GetAlignment();
104 GetNormalWidget()->ModifyStyleExts(dwExtendedStyle, 0xFFFFFFFF);
105
106 if (!m_pNode->IsHorizontalScrollPolicyOff())
107 dwEditStyles |= FWL_STYLEEXT_EDT_AutoHScroll;
108
109 pComboBox->EditModifyStyleExts(dwEditStyles, 0xFFFFFFFF);
110}
111
112bool CXFA_FFComboBox::OnRButtonUp(Mask<XFA_FWL_KeyFlag> dwFlags,
113 const CFX_PointF& point) {
114 if (!CXFA_FFField::OnRButtonUp(dwFlags, point))
115 return false;
116
117 GetDoc()->PopupMenu(this, point);
118 return true;
119}
120
121bool CXFA_FFComboBox::OnKillFocus(CXFA_FFWidget* pNewWidget) {
124
125 return pNewWidget && CXFA_FFField::OnKillFocus(pNewWidget);
126}
127
128void CXFA_FFComboBox::OpenDropDownList() {
129 ToComboBox(GetNormalWidget())->ShowDropDownList();
130}
131
132bool CXFA_FFComboBox::CommitData() {
133 return m_pNode->SetValue(XFA_ValuePicture::kRaw, m_wsNewValue);
134}
135
136bool CXFA_FFComboBox::IsDataChanged() {
137 WideString wsText = GetCurrentText();
138 if (m_pNode->GetValue(XFA_ValuePicture::kRaw) == wsText)
139 return false;
140
141 m_wsNewValue = std::move(wsText);
142 return true;
143}
144
145void CXFA_FFComboBox::FWLEventSelChange(CXFA_EventParam* pParam) {
147 pParam->m_wsPrevText = ToComboBox(GetNormalWidget())->GetEditText();
148 m_pNode->ProcessEvent(GetDocView(), XFA_AttributeValue::Change, pParam);
149}
150
151WideString CXFA_FFComboBox::GetCurrentText() const {
152 auto* pFWLcombobox = ToComboBox(GetNormalWidget());
153 WideString wsText = pFWLcombobox->GetEditText();
154 int32_t iCursel = pFWLcombobox->GetCurSel();
155 if (iCursel >= 0) {
156 WideString wsSel = pFWLcombobox->GetTextByIndex(iCursel);
157 if (wsSel == wsText)
158 wsText = m_pNode->GetChoiceListItem(iCursel, true).value_or(L"");
159 }
160 return wsText;
161}
162
163uint32_t CXFA_FFComboBox::GetAlignment() {
164 CXFA_Para* para = m_pNode->GetParaIfExists();
165 if (!para)
166 return 0;
167
168 uint32_t dwExtendedStyle = 0;
169 switch (para->GetHorizontalAlign()) {
170 case XFA_AttributeValue::Center:
171 dwExtendedStyle |=
173 break;
174 case XFA_AttributeValue::Justify:
175 dwExtendedStyle |= FWL_STYLEEXT_CMB_EditJustified;
176 break;
177 case XFA_AttributeValue::JustifyAll:
178 break;
179 case XFA_AttributeValue::Radix:
180 break;
181 case XFA_AttributeValue::Right:
182 break;
183 default:
184 dwExtendedStyle |=
186 break;
187 }
188
189 switch (para->GetVerticalAlign()) {
190 case XFA_AttributeValue::Middle:
191 dwExtendedStyle |= FWL_STYLEEXT_CMB_EditVCenter;
192 break;
193 case XFA_AttributeValue::Bottom:
194 dwExtendedStyle |= FWL_STYLEEXT_CMB_EditVFar;
195 break;
196 default:
197 dwExtendedStyle |= FWL_STYLEEXT_CMB_EditVNear;
198 break;
199 }
200 return dwExtendedStyle;
201}
202
203bool CXFA_FFComboBox::UpdateFWLData() {
204 auto* pComboBox = ToComboBox(GetNormalWidget());
205 if (!pComboBox)
206 return false;
207
208 std::vector<int32_t> iSelArray = m_pNode->GetSelectedItems();
209 if (!iSelArray.empty()) {
210 pComboBox->SetCurSel(iSelArray.front());
211 } else {
212 pComboBox->SetCurSel(-1);
213 pComboBox->SetEditText(m_pNode->GetValue(XFA_ValuePicture::kRaw));
214 }
215 pComboBox->Update();
216 return true;
217}
218
219bool CXFA_FFComboBox::CanUndo() {
220 return m_pNode->IsChoiceListAllowTextEntry() &&
221 ToComboBox(GetNormalWidget())->EditCanUndo();
222}
223
224bool CXFA_FFComboBox::CanRedo() {
225 return m_pNode->IsChoiceListAllowTextEntry() &&
226 ToComboBox(GetNormalWidget())->EditCanRedo();
227}
228
229bool CXFA_FFComboBox::CanCopy() {
230 return ToComboBox(GetNormalWidget())->EditCanCopy();
231}
232
233bool CXFA_FFComboBox::CanCut() {
234 return m_pNode->IsOpenAccess() && m_pNode->IsChoiceListAllowTextEntry() &&
235 ToComboBox(GetNormalWidget())->EditCanCut();
236}
237
238bool CXFA_FFComboBox::CanPaste() {
239 return m_pNode->IsChoiceListAllowTextEntry() && m_pNode->IsOpenAccess();
240}
241
242bool CXFA_FFComboBox::CanSelectAll() {
243 return ToComboBox(GetNormalWidget())->EditCanSelectAll();
244}
245
246bool CXFA_FFComboBox::Undo() {
247 return m_pNode->IsChoiceListAllowTextEntry() &&
248 ToComboBox(GetNormalWidget())->EditUndo();
249}
250
251bool CXFA_FFComboBox::Redo() {
252 return m_pNode->IsChoiceListAllowTextEntry() &&
253 ToComboBox(GetNormalWidget())->EditRedo();
254}
255
256absl::optional<WideString> CXFA_FFComboBox::Copy() {
257 return ToComboBox(GetNormalWidget())->EditCopy();
258}
259
260absl::optional<WideString> CXFA_FFComboBox::Cut() {
261 if (!m_pNode->IsChoiceListAllowTextEntry())
262 return absl::nullopt;
263
264 return ToComboBox(GetNormalWidget())->EditCut();
265}
266
267bool CXFA_FFComboBox::Paste(const WideString& wsPaste) {
268 return m_pNode->IsChoiceListAllowTextEntry() &&
269 ToComboBox(GetNormalWidget())->EditPaste(wsPaste);
270}
271
272void CXFA_FFComboBox::SelectAll() {
273 ToComboBox(GetNormalWidget())->EditSelectAll();
274}
275
276void CXFA_FFComboBox::Delete() {
277 ToComboBox(GetNormalWidget())->EditDelete();
278}
279
280void CXFA_FFComboBox::DeSelect() {
281 ToComboBox(GetNormalWidget())->EditDeSelect();
282}
283
284WideString CXFA_FFComboBox::GetText() {
285 return GetCurrentText();
286}
287
288FormFieldType CXFA_FFComboBox::GetFormFieldType() {
289 return FormFieldType::kXFA_ComboBox;
290}
291
292void CXFA_FFComboBox::SetItemState(int32_t nIndex, bool bSelected) {
293 ToComboBox(GetNormalWidget())->SetCurSel(bSelected ? nIndex : -1);
294 GetNormalWidget()->Update();
296}
297
298void CXFA_FFComboBox::InsertItem(const WideString& wsLabel, int32_t nIndex) {
299 ToComboBox(GetNormalWidget())->AddString(wsLabel);
300 GetNormalWidget()->Update();
302}
303
304void CXFA_FFComboBox::DeleteItem(int32_t nIndex) {
305 if (nIndex < 0)
306 ToComboBox(GetNormalWidget())->RemoveAll();
307 else
308 ToComboBox(GetNormalWidget())->RemoveAt(nIndex);
309
310 GetNormalWidget()->Update();
312}
313
314void CXFA_FFComboBox::OnTextChanged(CFWL_Widget* pWidget,
315 const WideString& wsChanged) {
317 eParam.m_wsPrevText = m_pNode->GetValue(XFA_ValuePicture::kRaw);
318 eParam.m_wsChange = wsChanged;
319 FWLEventSelChange(&eParam);
320}
321
322void CXFA_FFComboBox::OnSelectChanged(CFWL_Widget* pWidget, bool bLButtonUp) {
324 eParam.m_wsPrevText = m_pNode->GetValue(XFA_ValuePicture::kRaw);
325 FWLEventSelChange(&eParam);
326 if (m_pNode->IsChoiceListCommitOnSelect() && bLButtonUp)
327 m_pDocView->SetFocusNode(nullptr);
328}
329
330void CXFA_FFComboBox::OnPreOpen(CFWL_Widget* pWidget) {
332 m_pNode->ProcessEvent(GetDocView(), XFA_AttributeValue::PreOpen, &eParam);
333}
334
335void CXFA_FFComboBox::OnPostOpen(CFWL_Widget* pWidget) {
337 m_pNode->ProcessEvent(GetDocView(), XFA_AttributeValue::PostOpen, &eParam);
338}
339
340void CXFA_FFComboBox::OnProcessMessage(CFWL_Message* pMessage) {
341 m_pOldDelegate->OnProcessMessage(pMessage);
342}
343
344void CXFA_FFComboBox::OnProcessEvent(CFWL_Event* pEvent) {
346 switch (pEvent->GetType()) {
348 auto* postEvent = static_cast<CFWL_EventSelectChanged*>(pEvent);
349 OnSelectChanged(GetNormalWidget(), postEvent->GetLButtonUp());
350 break;
351 }
353 WideString wsChanged;
354 OnTextChanged(GetNormalWidget(), wsChanged);
355 break;
356 }
358 OnPreOpen(GetNormalWidget());
359 break;
360 }
362 OnPostOpen(GetNormalWidget());
363 break;
364 }
365 default:
366 break;
367 }
368 m_pOldDelegate->OnProcessEvent(pEvent);
369}
370
371void CXFA_FFComboBox::OnDrawWidget(CFGAS_GEGraphics* pGraphics,
372 const CFX_Matrix& matrix) {
373 m_pOldDelegate->OnDrawWidget(pGraphics, matrix);
374}
#define FWL_STYLEEXT_CMB_ListItemLeftAlign
#define FWL_STYLEEXT_CMB_EditVCenter
#define FWL_STYLEEXT_CMB_EditVFar
#define FWL_STYLEEXT_CMB_DropDown
#define FWL_STYLEEXT_CMB_ListItemCenterAlign
#define FWL_STYLEEXT_CMB_EditVNear
#define FWL_STYLEEXT_CMB_EditHNear
#define FWL_STYLEEXT_CMB_EditHCenter
#define FWL_STYLEEXT_CMB_ReadOnly
#define FWL_STYLEEXT_CMB_EditJustified
#define FWL_STYLEEXT_EDT_AutoHScroll
Definition cfwl_edit.h:22
#define FWL_STYLEEXT_EDT_ReadOnly
Definition cfwl_edit.h:19
void SetCurSel(int32_t iSel)
Type GetType() const
Definition cfwl_event.h:39
void RegisterEventTarget(CFWL_Widget *pListener, CFWL_Widget *pEventSource)
void SetDelegate(IFWL_WidgetDelegate *delegate)
void SetAdapterIface(AdapterIface *pItem)
CFWL_App * GetFWLApp() const
constexpr CFX_RectF()=default
XFA_EVENTTYPE m_eType
CXFA_EventParam(XFA_EVENTTYPE type)
bool CanRedo() override
void OnPostOpen(CFWL_Widget *pWidget)
void DeleteItem(int32_t nIndex) override
bool PtInActiveRect(const CFX_PointF &point) override
void Delete() override
bool LoadWidget() override
bool OnKillFocus(CXFA_FFWidget *pNewWidget) override
void OnPreOpen(CFWL_Widget *pWidget)
bool CanCut() override
bool CanUndo() override
void UpdateWidgetProperty() override
void OnProcessEvent(CFWL_Event *pEvent) override
void OnSelectChanged(CFWL_Widget *pWidget, bool bLButtonUp)
bool Paste(const WideString &wsPaste) override
bool OnRButtonUp(Mask< XFA_FWL_KeyFlag > dwFlags, const CFX_PointF &point) override
bool CanPaste() override
bool UpdateFWLData() override
CFX_RectF GetBBox(FocusOption focus) override
void SetItemState(int32_t nIndex, bool bSelected)
bool CanCopy() override
bool Undo() override
bool CanSelectAll() override
void OnTextChanged(CFWL_Widget *pWidget, const WideString &wsChanged)
FormFieldType GetFormFieldType() override
void OnDrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
void DeSelect() override
~CXFA_FFComboBox() override
bool Redo() override
void OnProcessMessage(CFWL_Message *pMessage) override
bool IsDataChanged() override
absl::optional< WideString > Cut() override
CXFA_FFComboBox * AsComboBox() override
absl::optional< WideString > Copy() override
bool CommitData() override
void InsertItem(const WideString &wsLabel, int32_t nIndex) override
WideString GetText() override
void SelectAll() override
CXFA_FFDropDown(CXFA_Node *pNode)
bool OnKillFocus(CXFA_FFWidget *pNewWidget) override
bool ProcessCommittedData()
bool LoadWidget() override
bool OnRButtonUp(Mask< XFA_FWL_KeyFlag > dwFlags, const CFX_PointF &point) override
void SetNormalWidget(CFWL_Widget *widget)
void OnProcessEvent(CFWL_Event *pEvent) override
uint32_t UpdateUIProperty()
virtual CFX_RectF GetBBox(FocusOption focus)
CXFA_FFDoc * GetDoc()
XFA_AttributeValue GetHorizontalAlign()
Definition cxfa_para.cpp:57
XFA_AttributeValue GetVerticalAlign()
Definition cxfa_para.cpp:63
FormFieldType
@ XFA_EVENT_PostOpen
@ XFA_EVENT_PreOpen
@ XFA_EVENT_Change
@ XFA_EVENT_Unknown
XFA_FWL_KeyFlag
XFA_AttributeValue
Definition fxfa_basic.h:60
Definition heap.h:12