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
cpwl_combo_box.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/pwl/cpwl_combo_box.h"
8
9#include <algorithm>
10#include <utility>
11
12#include "constants/ascii.h"
13#include "fpdfsdk/pwl/cpwl_cbbutton.h"
14#include "fpdfsdk/pwl/cpwl_cblistbox.h"
15#include "fpdfsdk/pwl/cpwl_edit.h"
16#include "fpdfsdk/pwl/ipwl_fillernotify.h"
17#include "public/fpdf_fwlevent.h"
18
19namespace {
20
21constexpr float kComboBoxDefaultFontSize = 12.0f;
22constexpr int kDefaultButtonWidth = 13;
23
24} // namespace
25
26CPWL_ComboBox::CPWL_ComboBox(
27 const CreateParams& cp,
28 std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData)
29 : CPWL_Wnd(cp, std::move(pAttachedData)) {
30 GetCreationParams()->dwFlags &= ~PWS_VSCROLL;
31}
32
33CPWL_ComboBox::~CPWL_ComboBox() = default;
34
35void CPWL_ComboBox::OnDestroy() {
36 // Until cleanup takes place in the virtual destructor for CPWL_Wnd
37 // subclasses, implement the virtual OnDestroy method that does the
38 // cleanup first, then invokes the superclass OnDestroy ... gee,
39 // like a dtor would.
40 m_pList.ExtractAsDangling();
41 m_pButton.ExtractAsDangling();
42 m_pEdit.ExtractAsDangling();
44}
45
46void CPWL_ComboBox::SetFocus() {
47 if (m_pEdit)
48 m_pEdit->SetFocus();
49}
50
51void CPWL_ComboBox::KillFocus() {
52 if (!SetPopup(false))
53 return;
54
56}
57
58WideString CPWL_ComboBox::GetSelectedText() {
59 if (m_pEdit)
60 return m_pEdit->GetSelectedText();
61
62 return WideString();
63}
64
65void CPWL_ComboBox::ReplaceAndKeepSelection(const WideString& text) {
66 if (m_pEdit)
67 m_pEdit->ReplaceAndKeepSelection(text);
68}
69
70void CPWL_ComboBox::ReplaceSelection(const WideString& text) {
71 if (m_pEdit)
72 m_pEdit->ReplaceSelection(text);
73}
74
75bool CPWL_ComboBox::SelectAllText() {
76 return m_pEdit && m_pEdit->SelectAllText();
77}
78
79bool CPWL_ComboBox::CanUndo() {
80 return m_pEdit && m_pEdit->CanUndo();
81}
82
83bool CPWL_ComboBox::CanRedo() {
84 return m_pEdit && m_pEdit->CanRedo();
85}
86
87bool CPWL_ComboBox::Undo() {
88 return m_pEdit && m_pEdit->Undo();
89}
90
91bool CPWL_ComboBox::Redo() {
92 return m_pEdit && m_pEdit->Redo();
93}
94
95WideString CPWL_ComboBox::GetText() {
96 return m_pEdit ? m_pEdit->GetText() : WideString();
97}
98
99void CPWL_ComboBox::SetText(const WideString& text) {
100 if (m_pEdit)
101 m_pEdit->SetText(text);
102}
103
104void CPWL_ComboBox::AddString(const WideString& str) {
105 if (m_pList)
106 m_pList->AddString(str);
107}
108
109int32_t CPWL_ComboBox::GetSelect() const {
110 return m_nSelectItem;
111}
112
113void CPWL_ComboBox::SetSelect(int32_t nItemIndex) {
114 if (m_pList)
115 m_pList->Select(nItemIndex);
116
117 m_pEdit->SetText(m_pList->GetText());
118 m_nSelectItem = nItemIndex;
119}
120
121void CPWL_ComboBox::SetEditSelection(int32_t nStartChar, int32_t nEndChar) {
122 if (m_pEdit)
123 m_pEdit->SetSelection(nStartChar, nEndChar);
124}
125
126void CPWL_ComboBox::ClearSelection() {
127 if (m_pEdit)
128 m_pEdit->ClearSelection();
129}
130
131void CPWL_ComboBox::CreateChildWnd(const CreateParams& cp) {
132 CreateEdit(cp);
133 CreateButton(cp);
134 CreateListBox(cp);
135}
136
137void CPWL_ComboBox::CreateEdit(const CreateParams& cp) {
138 if (m_pEdit)
139 return;
140
141 CreateParams ecp = cp;
142 ecp.dwFlags =
144
147
150
152 ecp.dwBorderWidth = 0;
154
155 auto pEdit = std::make_unique<CPWL_Edit>(ecp, CloneAttachedData());
156 m_pEdit = pEdit.get();
157 AddChild(std::move(pEdit));
158 m_pEdit->Realize();
159}
160
161void CPWL_ComboBox::CreateButton(const CreateParams& cp) {
162 if (m_pButton)
163 return;
164
165 CreateParams bcp = cp;
168 220.0f / 255.0f, 220.0f / 255.0f);
170 bcp.dwBorderWidth = 2;
173
174 auto pButton = std::make_unique<CPWL_CBButton>(bcp, CloneAttachedData());
175 m_pButton = pButton.get();
176 AddChild(std::move(pButton));
177 m_pButton->Realize();
178}
179
180void CPWL_ComboBox::CreateListBox(const CreateParams& cp) {
181 if (m_pList)
182 return;
183
184 CreateParams lcp = cp;
187 lcp.dwBorderWidth = 1;
190 lcp.fFontSize =
191 (cp.dwFlags & PWS_AUTOFONTSIZE) ? kComboBoxDefaultFontSize : cp.fFontSize;
192
195
198
199 auto pList = std::make_unique<CPWL_CBListBox>(lcp, CloneAttachedData());
200 m_pList = pList.get();
201 AddChild(std::move(pList));
202 m_pList->Realize();
203}
204
205bool CPWL_ComboBox::RepositionChildWnd() {
206 ObservedPtr<CPWL_ComboBox> this_observed(this);
207 const CFX_FloatRect rcClient = GetClientRect();
208 if (m_bPopup) {
209 const float fOldWindowHeight = m_rcOldWindow.Height();
210 const float fOldClientHeight = fOldWindowHeight - GetBorderWidth() * 2;
211
213 CFX_FloatRect rcButton = rcClient;
214 rcButton.left =
215 std::max(rcButton.right - kDefaultButtonWidth, rcClient.left);
216 CFX_FloatRect rcEdit = rcClient;
217 rcEdit.right = std::max(rcButton.left - 1.0f, rcEdit.left);
218 if (m_bBottom) {
219 rcButton.bottom = rcButton.top - fOldClientHeight;
220 rcEdit.bottom = rcEdit.top - fOldClientHeight;
221 rcList.top -= fOldWindowHeight;
222 } else {
223 rcButton.top = rcButton.bottom + fOldClientHeight;
224 rcEdit.top = rcEdit.bottom + fOldClientHeight;
225 rcList.bottom += fOldWindowHeight;
226 }
227
228 if (m_pButton) {
229 m_pButton->Move(rcButton, true, false);
230 if (!this_observed) {
231 return false;
232 }
233 }
234
235 if (m_pEdit) {
236 m_pEdit->Move(rcEdit, true, false);
237 if (!this_observed) {
238 return false;
239 }
240 }
241
242 if (m_pList) {
243 if (!m_pList->SetVisible(true) || !this_observed) {
244 return false;
245 }
246
247 if (!m_pList->Move(rcList, true, false) || !this_observed) {
248 return false;
249 }
250
251 m_pList->ScrollToListItem(m_nSelectItem);
252 if (!this_observed) {
253 return false;
254 }
255 }
256 return true;
257 }
258
259 CFX_FloatRect rcButton = rcClient;
260 rcButton.left = std::max(rcButton.right - kDefaultButtonWidth, rcClient.left);
261
262 if (m_pButton) {
263 m_pButton->Move(rcButton, true, false);
264 if (!this_observed) {
265 return false;
266 }
267 }
268
269 CFX_FloatRect rcEdit = rcClient;
270 rcEdit.right = std::max(rcButton.left - 1.0f, rcEdit.left);
271
272 if (m_pEdit) {
273 m_pEdit->Move(rcEdit, true, false);
274 if (!this_observed) {
275 return false;
276 }
277 }
278
279 if (m_pList) {
280 if (!m_pList->SetVisible(false)) {
281 m_pList = nullptr; // Gone, dangling even.
282 return false;
283 }
284 if (!this_observed) {
285 return false;
286 }
287 }
288
289 return true;
290}
291
292void CPWL_ComboBox::SelectAll() {
293 if (m_pEdit && HasFlag(PCBS_ALLOWCUSTOMTEXT))
294 m_pEdit->SelectAllText();
295}
296
297CFX_FloatRect CPWL_ComboBox::GetFocusRect() const {
298 return CFX_FloatRect();
299}
300
301bool CPWL_ComboBox::SetPopup(bool bPopup) {
302 if (!m_pList)
303 return true;
304 if (bPopup == m_bPopup)
305 return true;
306 float fListHeight = m_pList->GetContentRect().Height();
307 if (!FXSYS_IsFloatBigger(fListHeight, 0.0f))
308 return true;
309
310 if (!bPopup) {
311 m_bPopup = bPopup;
312 return Move(m_rcOldWindow, true, true);
313 }
314
315 ObservedPtr<CPWL_ComboBox> this_observed(this);
316 if (GetFillerNotify()->OnPopupPreOpen(GetAttachedData(), {}))
317 return !!this_observed;
318 if (!this_observed) {
319 return false;
320 }
321
322 float fBorderWidth = m_pList->GetBorderWidth() * 2;
323 float fPopupMin = 0.0f;
324 if (m_pList->GetCount() > 3)
325 fPopupMin = m_pList->GetFirstHeight() * 3 + fBorderWidth;
326 float fPopupMax = fListHeight + fBorderWidth;
327
328 bool bBottom;
329 float fPopupRet;
330 GetFillerNotify()->QueryWherePopup(GetAttachedData(), fPopupMin, fPopupMax,
331 &bBottom, &fPopupRet);
332 if (!FXSYS_IsFloatBigger(fPopupRet, 0.0f))
333 return true;
334
335 m_rcOldWindow = CPWL_Wnd::GetWindowRect();
336 m_bPopup = bPopup;
337 m_bBottom = bBottom;
338
339 CFX_FloatRect rcWindow = m_rcOldWindow;
340 if (bBottom)
341 rcWindow.bottom -= fPopupRet;
342 else
343 rcWindow.top += fPopupRet;
344
345 if (!Move(rcWindow, true, true))
346 return false;
347
348 GetFillerNotify()->OnPopupPostOpen(GetAttachedData(), {});
349 return !!this_observed;
350}
351
352bool CPWL_ComboBox::OnKeyDown(FWL_VKEYCODE nKeyCode,
353 Mask<FWL_EVENTFLAG> nFlag) {
354 if (!m_pList)
355 return false;
356 if (!m_pEdit)
357 return false;
358
359 ObservedPtr<CPWL_Wnd> this_observed(this);
360 m_nSelectItem = -1;
361
362 switch (nKeyCode) {
363 case FWL_VKEY_Up:
364 if (m_pList->GetCurSel() > 0) {
365 if (GetFillerNotify()->OnPopupPreOpen(GetAttachedData(), nFlag) ||
366 !this_observed) {
367 return false;
368 }
369 if (GetFillerNotify()->OnPopupPostOpen(GetAttachedData(), nFlag) ||
370 !this_observed) {
371 return false;
372 }
373 if (m_pList->IsMovementKey(nKeyCode)) {
374 if (m_pList->OnMovementKeyDown(nKeyCode, nFlag) || !this_observed) {
375 return false;
376 }
378 }
379 }
380 return true;
381 case FWL_VKEY_Down:
382 if (m_pList->GetCurSel() < m_pList->GetCount() - 1) {
383 if (GetFillerNotify()->OnPopupPreOpen(GetAttachedData(), nFlag) ||
384 !this_observed) {
385 return false;
386 }
387 if (GetFillerNotify()->OnPopupPostOpen(GetAttachedData(), nFlag) ||
388 !this_observed) {
389 return false;
390 }
391 if (m_pList->IsMovementKey(nKeyCode)) {
392 if (m_pList->OnMovementKeyDown(nKeyCode, nFlag) || !this_observed) {
393 return false;
394 }
396 }
397 }
398 return true;
399 default:
400 break;
401 }
402
403 if (HasFlag(PCBS_ALLOWCUSTOMTEXT))
404 return m_pEdit->OnKeyDown(nKeyCode, nFlag);
405
406 return false;
407}
408
409bool CPWL_ComboBox::OnChar(uint16_t nChar, Mask<FWL_EVENTFLAG> nFlag) {
410 if (!m_pList)
411 return false;
412
413 if (!m_pEdit)
414 return false;
415
416 // In a combo box if the ENTER/SPACE key is pressed, show the combo box
417 // options.
418 switch (nChar) {
419 case pdfium::ascii::kReturn:
420 if (!SetPopup(!IsPopup())) {
421 return false;
422 }
424 return true;
425 case pdfium::ascii::kSpace:
426 // Show the combo box options with space only if the combo box is not
427 // editable
429 if (!IsPopup()) {
430 if (!SetPopup(/*bPopUp=*/true)) {
431 return false;
432 }
434 }
435 return true;
436 }
437 break;
438 default:
439 break;
440 }
441
442 m_nSelectItem = -1;
443 if (HasFlag(PCBS_ALLOWCUSTOMTEXT))
444 return m_pEdit->OnChar(nChar, nFlag);
445
446 ObservedPtr<CPWL_Wnd> this_observed(this);
447 if (GetFillerNotify()->OnPopupPreOpen(GetAttachedData(), nFlag) ||
448 !this_observed) {
449 return false;
450 }
451 if (GetFillerNotify()->OnPopupPostOpen(GetAttachedData(), nFlag) ||
452 !this_observed) {
453 return false;
454 }
455 if (!m_pList->IsChar(nChar, nFlag))
456 return false;
457 return m_pList->OnCharNotify(nChar, nFlag);
458}
459
460void CPWL_ComboBox::NotifyLButtonDown(CPWL_Wnd* child, const CFX_PointF& pos) {
461 if (child == m_pButton) {
462 (void)SetPopup(!m_bPopup);
463 // Note, |this| may no longer be viable at this point. If more work needs to
464 // be done, check the return value of SetPopup().
465 }
466}
467
468void CPWL_ComboBox::NotifyLButtonUp(CPWL_Wnd* child, const CFX_PointF& pos) {
469 if (!m_pEdit || !m_pList || child != m_pList)
470 return;
471
474 m_pEdit->SetFocus();
475 (void)SetPopup(false);
476 // Note, |this| may no longer be viable at this point. If more work needs to
477 // be done, check the return value of SetPopup().
478}
479
480bool CPWL_ComboBox::IsPopup() const {
481 return m_bPopup;
482}
483
484void CPWL_ComboBox::SetSelectText() {
485 m_pEdit->SelectAllText();
486 m_pEdit->ReplaceSelection(m_pList->GetText());
487 m_pEdit->SelectAllText();
488 m_nSelectItem = m_pList->GetCurSel();
489}
BorderStyle
constexpr CFX_FloatRect()=default
CFX_FloatRect & operator=(const CFX_FloatRect &that)=default
float Height() const
void KillFocus() override
void SetSelect(int32_t nItemIndex)
void SetText(const WideString &text)
CFX_FloatRect GetFocusRect() const override
CPWL_ComboBox(const CreateParams &cp, std::unique_ptr< IPWL_FillerNotify::PerWindowData > pAttachedData)
void AddString(const WideString &str)
bool Undo() override
bool IsPopup() const
bool CanRedo() override
WideString GetSelectedText() override
~CPWL_ComboBox() override
void ReplaceSelection(const WideString &text) override
void ReplaceAndKeepSelection(const WideString &text) override
bool OnKeyDown(FWL_VKEYCODE nKeyCode, Mask< FWL_EVENTFLAG > nFlag) override
int32_t GetSelect() const
WideString GetText() override
bool OnChar(uint16_t nChar, Mask< FWL_EVENTFLAG > nFlag) override
bool CanUndo() override
bool SelectAllText() override
void NotifyLButtonDown(CPWL_Wnd *child, const CFX_PointF &pos) override
void SetFocus() override
void CreateChildWnd(const CreateParams &cp) override
void OnDestroy() override
void SetEditSelection(int32_t nStartChar, int32_t nEndChar)
void NotifyLButtonUp(CPWL_Wnd *child, const CFX_PointF &pos) override
bool Redo() override
bool RepositionChildWnd() override
IPWL_FillerNotify::CursorStyle eCursorType
Definition cpwl_wnd.h:126
CFX_FloatRect rcRectWnd
Definition cpwl_wnd.h:105
CFX_Color sBackgroundColor
Definition cpwl_wnd.h:113
BorderStyle nBorderStyle
Definition cpwl_wnd.h:114
virtual void KillFocus()
Definition cpwl_wnd.cpp:542
IPWL_FillerNotify * GetFillerNotify() const
Definition cpwl_wnd.h:245
bool HasFlag(uint32_t dwFlags) const
Definition cpwl_wnd.cpp:462
virtual CFX_FloatRect GetClientRect() const
Definition cpwl_wnd.cpp:444
CFX_FloatRect GetWindowRect() const
Definition cpwl_wnd.cpp:440
int32_t GetBorderWidth() const
Definition cpwl_wnd.cpp:482
CreateParams * GetCreationParams()
Definition cpwl_wnd.h:238
virtual void OnDestroy()
Definition cpwl_wnd.cpp:187
IPWL_FillerNotify::PerWindowData * GetAttachedData() const
Definition cpwl_wnd.h:209
bool Move(const CFX_FloatRect &rcNew, bool bReset, bool bRefresh)
Definition cpwl_wnd.cpp:211
static const CFX_Color kDefaultWhiteColor
Definition cpwl_wnd.h:80
static const CFX_Color kDefaultBlackColor
Definition cpwl_wnd.h:79
#define PWS_READONLY
Definition cpwl_wnd.h:34
#define PES_AUTOSCROLL
Definition cpwl_wnd.h:48
#define PES_UNDO
Definition cpwl_wnd.h:50
#define PWS_BACKGROUND
Definition cpwl_wnd.h:31
#define PWS_VSCROLL
Definition cpwl_wnd.h:32
#define PWS_VISIBLE
Definition cpwl_wnd.h:33
#define PWS_BORDER
Definition cpwl_wnd.h:30
#define PCBS_ALLOWCUSTOMTEXT
Definition cpwl_wnd.h:59
#define PWS_AUTOFONTSIZE
Definition cpwl_wnd.h:35
#define PES_CENTER
Definition cpwl_wnd.h:46
#define PLBS_HOVERSEL
Definition cpwl_wnd.h:56
@ FWL_VKEY_Up
@ FWL_VKEY_Down
#define FXSYS_IsFloatBigger(fa, fb)
Definition fx_system.h:36
constexpr uint8_t kReturn
Definition ascii.h:20
constexpr uint8_t kSpace
Definition ascii.h:25
Type nColorType
Definition cfx_color.h:54
CFX_Color & operator=(const CFX_Color &that)=default
constexpr CFX_Color(Type type=CFX_Color::Type::kTransparent, float color1=0.0f, float color2=0.0f, float color3=0.0f, float color4=0.0f)
Definition cfx_color.h:27