blob: 8c5e99ff09833976fcd2d7d3d2e596cfd068bfb9 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairc411eb92017-07-25 09:39:30 -04007#include "fpdfsdk/pwl/cpwl_combo_box.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -08008
Lei Zhang7bef7c82017-05-31 23:52:00 -07009#include <algorithm>
Henrique Nakashimaf1eae2c2017-06-29 11:18:49 -040010#include <sstream>
Lei Zhang7bef7c82017-05-31 23:52:00 -070011
dsinclair74a34fc2016-09-29 16:41:42 -070012#include "core/fxge/cfx_pathdata.h"
13#include "core/fxge/cfx_renderdevice.h"
Dan Sinclairc411eb92017-07-25 09:39:30 -040014#include "fpdfsdk/pwl/cpwl_edit.h"
15#include "fpdfsdk/pwl/cpwl_edit_ctrl.h"
16#include "fpdfsdk/pwl/cpwl_list_box.h"
17#include "fpdfsdk/pwl/cpwl_list_impl.h"
18#include "fpdfsdk/pwl/cpwl_wnd.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -080019#include "public/fpdf_fwlevent.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Dan Sinclaira9e28432017-07-05 14:18:14 -040021namespace {
22
23constexpr float kDefaultFontSize = 12.0f;
24constexpr float kTriangleHalfLength = 3.0f;
25
26} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Dan Sinclairf528eee2017-02-14 11:52:07 -050028bool CPWL_CBListBox::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029 CPWL_Wnd::OnLButtonUp(point, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030
dsinclair36177512016-07-19 10:16:10 -070031 if (!m_bMouseDown)
tsepez4cf55152016-11-02 14:37:54 -070032 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033
dsinclair36177512016-07-19 10:16:10 -070034 ReleaseCapture();
tsepez4cf55152016-11-02 14:37:54 -070035 m_bMouseDown = false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070036
dsinclair36177512016-07-19 10:16:10 -070037 if (!ClientHitTest(point))
tsepez4cf55152016-11-02 14:37:54 -070038 return true;
dsinclair36177512016-07-19 10:16:10 -070039 if (CPWL_Wnd* pParent = GetParentWindow())
Dan Sinclair7f6bec92017-07-05 14:13:16 -040040 pParent->NotifyLButtonUp(this, point);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
Dan Sinclair2b444412017-07-06 13:56:35 -040042 return !OnNotifySelectionChanged(false, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043}
44
Dan Sinclair07dbf432017-07-06 11:47:26 -040045bool CPWL_CBListBox::IsMovementKey(uint16_t nChar) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 switch (nChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 case FWL_VKEY_Up:
48 case FWL_VKEY_Down:
49 case FWL_VKEY_Home:
50 case FWL_VKEY_Left:
51 case FWL_VKEY_End:
52 case FWL_VKEY_Right:
Dan Sinclair07dbf432017-07-06 11:47:26 -040053 return true;
dsinclair36177512016-07-19 10:16:10 -070054 default:
tsepez4cf55152016-11-02 14:37:54 -070055 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 }
Dan Sinclair07dbf432017-07-06 11:47:26 -040057}
58
59bool CPWL_CBListBox::OnMovementKeyDown(uint16_t nChar, uint32_t nFlag) {
60 ASSERT(IsMovementKey(nChar));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 switch (nChar) {
63 case FWL_VKEY_Up:
64 m_pList->OnVK_UP(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
65 break;
66 case FWL_VKEY_Down:
67 m_pList->OnVK_DOWN(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
68 break;
69 case FWL_VKEY_Home:
70 m_pList->OnVK_HOME(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
71 break;
72 case FWL_VKEY_Left:
73 m_pList->OnVK_LEFT(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
74 break;
75 case FWL_VKEY_End:
76 m_pList->OnVK_END(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
77 break;
78 case FWL_VKEY_Right:
79 m_pList->OnVK_RIGHT(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
80 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 }
Dan Sinclair2b444412017-07-06 13:56:35 -040082 return OnNotifySelectionChanged(true, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083}
84
Dan Sinclair16fea942017-07-06 11:56:37 -040085bool CPWL_CBListBox::IsChar(uint16_t nChar, uint32_t nFlag) const {
86 return m_pList->OnChar(nChar, IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
87}
88
89bool CPWL_CBListBox::OnCharNotify(uint16_t nChar, uint32_t nFlag) {
dsinclair36177512016-07-19 10:16:10 -070090 if (CPWL_ComboBox* pComboBox = (CPWL_ComboBox*)GetParentWindow())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 pComboBox->SetSelectText();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092
Dan Sinclair2b444412017-07-06 13:56:35 -040093 return OnNotifySelectionChanged(true, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096void CPWL_CBButton::DrawThisAppearance(CFX_RenderDevice* pDevice,
Lei Zhangeb14e042017-08-15 13:56:43 -070097 const CFX_Matrix& mtUser2Device) {
98 CPWL_Wnd::DrawThisAppearance(pDevice, mtUser2Device);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
Tom Sepez281a9ea2016-02-26 14:24:28 -0800100 CFX_FloatRect rectWnd = CPWL_Wnd::GetWindowRect();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700101
Henrique Nakashimaf1eae2c2017-06-29 11:18:49 -0400102 if (!IsVisible() || rectWnd.IsEmpty())
103 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104
Henrique Nakashimaf1eae2c2017-06-29 11:18:49 -0400105 CFX_PointF ptCenter = GetCenterPoint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106
Dan Sinclaira9e28432017-07-05 14:18:14 -0400107 CFX_PointF pt1(ptCenter.x - kTriangleHalfLength,
108 ptCenter.y + kTriangleHalfLength * 0.5f);
109 CFX_PointF pt2(ptCenter.x + kTriangleHalfLength,
110 ptCenter.y + kTriangleHalfLength * 0.5f);
111 CFX_PointF pt3(ptCenter.x, ptCenter.y - kTriangleHalfLength * 0.5f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112
Dan Sinclaira9e28432017-07-05 14:18:14 -0400113 if (IsFloatBigger(rectWnd.right - rectWnd.left, kTriangleHalfLength * 2) &&
114 IsFloatBigger(rectWnd.top - rectWnd.bottom, kTriangleHalfLength)) {
Henrique Nakashimaf1eae2c2017-06-29 11:18:49 -0400115 CFX_PathData path;
116 path.AppendPoint(pt1, FXPT_TYPE::MoveTo, false);
117 path.AppendPoint(pt2, FXPT_TYPE::LineTo, false);
118 path.AppendPoint(pt3, FXPT_TYPE::LineTo, false);
119 path.AppendPoint(pt1, FXPT_TYPE::LineTo, false);
120
Lei Zhangeb14e042017-08-15 13:56:43 -0700121 pDevice->DrawPath(&path, &mtUser2Device, nullptr,
Henrique Nakashimaf1eae2c2017-06-29 11:18:49 -0400122 PWL_DEFAULT_BLACKCOLOR.ToFXColor(GetTransparency()), 0,
123 FXFILL_ALTERNATE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125}
126
Dan Sinclairf528eee2017-02-14 11:52:07 -0500127bool CPWL_CBButton::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 CPWL_Wnd::OnLButtonDown(point, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 SetCapture();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131
Dan Sinclair7f6bec92017-07-05 14:13:16 -0400132 if (CPWL_Wnd* pParent = GetParentWindow())
133 pParent->NotifyLButtonDown(this, point);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700134
tsepez4cf55152016-11-02 14:37:54 -0700135 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136}
137
Dan Sinclairf528eee2017-02-14 11:52:07 -0500138bool CPWL_CBButton::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 CPWL_Wnd::OnLButtonUp(point, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 ReleaseCapture();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142
tsepez4cf55152016-11-02 14:37:54 -0700143 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144}
145
Lei Zhang35162562017-06-09 01:04:52 -0700146CPWL_ComboBox::CPWL_ComboBox() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147
Tom Sepezb084c1f2017-05-12 14:04:06 -0700148CPWL_ComboBox::~CPWL_ComboBox() {}
149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150CFX_ByteString CPWL_ComboBox::GetClassName() const {
151 return "CPWL_ComboBox";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152}
153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154void CPWL_ComboBox::OnCreate(PWL_CREATEPARAM& cp) {
155 cp.dwFlags &= ~PWS_HSCROLL;
156 cp.dwFlags &= ~PWS_VSCROLL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700157}
158
Lei Zhang35162562017-06-09 01:04:52 -0700159void CPWL_ComboBox::OnDestroy() {
160 // Until cleanup takes place in the virtual destructor for CPWL_Wnd
161 // subclasses, implement the virtual OnDestroy method that does the
162 // cleanup first, then invokes the superclass OnDestroy ... gee,
163 // like a dtor would.
164 m_pList.Release();
165 m_pButton.Release();
166 m_pEdit.Release();
167 CPWL_Wnd::OnDestroy();
168}
169
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170void CPWL_ComboBox::SetFocus() {
171 if (m_pEdit)
172 m_pEdit->SetFocus();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173}
174
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175void CPWL_ComboBox::KillFocus() {
tsepez4cf55152016-11-02 14:37:54 -0700176 SetPopup(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 CPWL_Wnd::KillFocus();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178}
179
Diana Gagedce2d722017-06-20 11:17:11 -0700180CFX_WideString CPWL_ComboBox::GetSelectedText() {
181 if (m_pEdit)
182 return m_pEdit->GetSelectedText();
183
184 return CFX_WideString();
185}
186
Diana Gageab390972017-07-28 17:07:39 -0700187void CPWL_ComboBox::ReplaceSelection(const CFX_WideString& text) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700188 if (m_pEdit)
Diana Gageab390972017-07-28 17:07:39 -0700189 m_pEdit->ReplaceSelection(text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700190}
191
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192CFX_WideString CPWL_ComboBox::GetText() const {
193 if (m_pEdit) {
194 return m_pEdit->GetText();
195 }
196 return CFX_WideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700197}
198
tsepez067990c2016-09-13 06:46:40 -0700199void CPWL_ComboBox::SetText(const CFX_WideString& text) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 if (m_pEdit)
201 m_pEdit->SetText(text);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202}
203
tsepez067990c2016-09-13 06:46:40 -0700204void CPWL_ComboBox::AddString(const CFX_WideString& str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 if (m_pList)
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500206 m_pList->AddString(str);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207}
208
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209int32_t CPWL_ComboBox::GetSelect() const {
210 return m_nSelectItem;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211}
212
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213void CPWL_ComboBox::SetSelect(int32_t nItemIndex) {
214 if (m_pList)
215 m_pList->Select(nItemIndex);
216
tsepez067990c2016-09-13 06:46:40 -0700217 m_pEdit->SetText(m_pList->GetText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 m_nSelectItem = nItemIndex;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700219}
220
Diana Gage4d02e902017-07-20 17:20:31 -0700221void CPWL_ComboBox::SetEditSelection(int32_t nStartChar, int32_t nEndChar) {
tsepez067990c2016-09-13 06:46:40 -0700222 if (m_pEdit)
Diana Gage4d02e902017-07-20 17:20:31 -0700223 m_pEdit->SetSelection(nStartChar, nEndChar);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224}
225
Diana Gage4d02e902017-07-20 17:20:31 -0700226void CPWL_ComboBox::GetEditSelection(int32_t& nStartChar,
227 int32_t& nEndChar) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 nStartChar = -1;
229 nEndChar = -1;
230
tsepez067990c2016-09-13 06:46:40 -0700231 if (m_pEdit)
Diana Gage4d02e902017-07-20 17:20:31 -0700232 m_pEdit->GetSelection(nStartChar, nEndChar);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233}
234
Diana Gage22bf7a52017-07-21 11:33:18 -0700235void CPWL_ComboBox::ClearSelection() {
tsepez067990c2016-09-13 06:46:40 -0700236 if (m_pEdit)
Diana Gage22bf7a52017-07-21 11:33:18 -0700237 m_pEdit->ClearSelection();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238}
239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240void CPWL_ComboBox::CreateChildWnd(const PWL_CREATEPARAM& cp) {
241 CreateEdit(cp);
242 CreateButton(cp);
243 CreateListBox(cp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700244}
245
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246void CPWL_ComboBox::CreateEdit(const PWL_CREATEPARAM& cp) {
Tom Sepezb084c1f2017-05-12 14:04:06 -0700247 if (m_pEdit)
248 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249
Dan Sinclair0fdd1ae2017-07-05 16:00:48 -0400250 m_pEdit = new CPWL_Edit();
Tom Sepezcc205132017-05-16 14:01:47 -0700251 m_pEdit->AttachFFLData(m_pFormFiller.Get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252
Tom Sepezb084c1f2017-05-12 14:04:06 -0700253 PWL_CREATEPARAM ecp = cp;
254 ecp.pParentWnd = this;
255 ecp.dwFlags = PWS_VISIBLE | PWS_CHILD | PWS_BORDER | PES_CENTER |
256 PES_AUTOSCROLL | PES_UNDO;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257
Tom Sepezb084c1f2017-05-12 14:04:06 -0700258 if (HasFlag(PWS_AUTOFONTSIZE))
259 ecp.dwFlags |= PWS_AUTOFONTSIZE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260
Tom Sepezb084c1f2017-05-12 14:04:06 -0700261 if (!HasFlag(PCBS_ALLOWCUSTOMTEXT))
262 ecp.dwFlags |= PWS_READONLY;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263
Lei Zhangd24236a2017-06-29 18:28:58 -0700264 ecp.rcRectWnd = CFX_FloatRect();
Tom Sepezb084c1f2017-05-12 14:04:06 -0700265 ecp.dwBorderWidth = 0;
266 ecp.nBorderStyle = BorderStyle::SOLID;
267 m_pEdit->Create(ecp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268}
269
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270void CPWL_ComboBox::CreateButton(const PWL_CREATEPARAM& cp) {
Tom Sepezb084c1f2017-05-12 14:04:06 -0700271 if (m_pButton)
272 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273
Tom Sepez45b9ae12017-05-18 08:34:03 -0700274 m_pButton = new CPWL_CBButton;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Tom Sepezb084c1f2017-05-12 14:04:06 -0700276 PWL_CREATEPARAM bcp = cp;
277 bcp.pParentWnd = this;
278 bcp.dwFlags = PWS_VISIBLE | PWS_CHILD | PWS_BORDER | PWS_BACKGROUND;
Dan Sinclair7f55a542017-07-13 14:17:10 -0400279 bcp.sBackgroundColor = CFX_Color(COLORTYPE_RGB, 220.0f / 255.0f,
280 220.0f / 255.0f, 220.0f / 255.0f);
Tom Sepezb084c1f2017-05-12 14:04:06 -0700281 bcp.sBorderColor = PWL_DEFAULT_BLACKCOLOR;
282 bcp.dwBorderWidth = 2;
283 bcp.nBorderStyle = BorderStyle::BEVELED;
284 bcp.eCursorType = FXCT_ARROW;
285 m_pButton->Create(bcp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286}
287
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288void CPWL_ComboBox::CreateListBox(const PWL_CREATEPARAM& cp) {
Tom Sepezb084c1f2017-05-12 14:04:06 -0700289 if (m_pList)
290 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291
Tom Sepez45b9ae12017-05-18 08:34:03 -0700292 m_pList = new CPWL_CBListBox();
Tom Sepezcc205132017-05-16 14:01:47 -0700293 m_pList->AttachFFLData(m_pFormFiller.Get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700294
Tom Sepezb084c1f2017-05-12 14:04:06 -0700295 PWL_CREATEPARAM lcp = cp;
296 lcp.pParentWnd = this;
297 lcp.dwFlags =
298 PWS_CHILD | PWS_BORDER | PWS_BACKGROUND | PLBS_HOVERSEL | PWS_VSCROLL;
299 lcp.nBorderStyle = BorderStyle::SOLID;
300 lcp.dwBorderWidth = 1;
301 lcp.eCursorType = FXCT_ARROW;
Lei Zhangd24236a2017-06-29 18:28:58 -0700302 lcp.rcRectWnd = CFX_FloatRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303
Tom Sepezb084c1f2017-05-12 14:04:06 -0700304 if (cp.dwFlags & PWS_AUTOFONTSIZE)
Dan Sinclaira9e28432017-07-05 14:18:14 -0400305 lcp.fFontSize = kDefaultFontSize;
Tom Sepezb084c1f2017-05-12 14:04:06 -0700306 else
307 lcp.fFontSize = cp.fFontSize;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308
Tom Sepezb084c1f2017-05-12 14:04:06 -0700309 if (cp.sBorderColor.nColorType == COLORTYPE_TRANSPARENT)
310 lcp.sBorderColor = PWL_DEFAULT_BLACKCOLOR;
311
312 if (cp.sBackgroundColor.nColorType == COLORTYPE_TRANSPARENT)
313 lcp.sBackgroundColor = PWL_DEFAULT_WHITECOLOR;
314
315 m_pList->Create(lcp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316}
317
318void CPWL_ComboBox::RePosChildWnd() {
Lei Zhang7bef7c82017-05-31 23:52:00 -0700319 const CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 if (m_bPopup) {
Lei Zhang7bef7c82017-05-31 23:52:00 -0700321 const float fOldWindowHeight = m_rcOldWindow.Height();
322 const float fOldClientHeight = fOldWindowHeight - GetBorderWidth() * 2;
323
Tom Sepez281a9ea2016-02-26 14:24:28 -0800324 CFX_FloatRect rcList = CPWL_Wnd::GetWindowRect();
Lei Zhang7bef7c82017-05-31 23:52:00 -0700325 CFX_FloatRect rcButton = rcClient;
326 rcButton.left =
327 std::max(rcButton.right - PWL_COMBOBOX_BUTTON_WIDTH, rcClient.left);
328 CFX_FloatRect rcEdit = rcClient;
329 rcEdit.right = std::max(rcButton.left - 1.0f, rcEdit.left);
330 if (m_bBottom) {
331 rcButton.bottom = rcButton.top - fOldClientHeight;
332 rcEdit.bottom = rcEdit.top - fOldClientHeight;
333 rcList.top -= fOldWindowHeight;
334 } else {
335 rcButton.top = rcButton.bottom + fOldClientHeight;
336 rcEdit.top = rcEdit.bottom + fOldClientHeight;
337 rcList.bottom += fOldWindowHeight;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700338 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700339
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 if (m_pButton)
tsepez4cf55152016-11-02 14:37:54 -0700341 m_pButton->Move(rcButton, true, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342
343 if (m_pEdit)
tsepez4cf55152016-11-02 14:37:54 -0700344 m_pEdit->Move(rcEdit, true, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345
346 if (m_pList) {
tsepez4cf55152016-11-02 14:37:54 -0700347 m_pList->SetVisible(true);
348 m_pList->Move(rcList, true, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 m_pList->ScrollToListItem(m_nSelectItem);
350 }
Lei Zhang7bef7c82017-05-31 23:52:00 -0700351 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352 }
Lei Zhang7bef7c82017-05-31 23:52:00 -0700353
354 CFX_FloatRect rcButton = rcClient;
355 rcButton.left =
356 std::max(rcButton.right - PWL_COMBOBOX_BUTTON_WIDTH, rcClient.left);
357
358 if (m_pButton)
359 m_pButton->Move(rcButton, true, false);
360
361 CFX_FloatRect rcEdit = rcClient;
362 rcEdit.right = std::max(rcButton.left - 1.0f, rcEdit.left);
363
364 if (m_pEdit)
365 m_pEdit->Move(rcEdit, true, false);
366
367 if (m_pList)
368 m_pList->SetVisible(false);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700369}
370
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371void CPWL_ComboBox::SelectAll() {
372 if (m_pEdit && HasFlag(PCBS_ALLOWCUSTOMTEXT))
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700373 m_pEdit->SelectAll();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700374}
375
Tom Sepez281a9ea2016-02-26 14:24:28 -0800376CFX_FloatRect CPWL_ComboBox::GetFocusRect() const {
377 return CFX_FloatRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700378}
379
tsepez4cf55152016-11-02 14:37:54 -0700380void CPWL_ComboBox::SetPopup(bool bPopup) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 if (!m_pList)
382 return;
383 if (bPopup == m_bPopup)
384 return;
Dan Sinclair05df0752017-03-14 14:43:42 -0400385 float fListHeight = m_pList->GetContentRect().Height();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 if (!IsFloatBigger(fListHeight, 0.0f))
387 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700388
Lei Zhang7bef7c82017-05-31 23:52:00 -0700389 if (!bPopup) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 m_bPopup = bPopup;
tsepez4cf55152016-11-02 14:37:54 -0700391 Move(m_rcOldWindow, true, true);
Lei Zhang7bef7c82017-05-31 23:52:00 -0700392 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 }
Lei Zhang7bef7c82017-05-31 23:52:00 -0700394
395 if (!m_pFillerNotify)
396 return;
397
398#ifdef PDF_ENABLE_XFA
Dan Sinclair5fe98082017-07-06 11:13:02 -0400399 if (m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), 0))
Lei Zhang7bef7c82017-05-31 23:52:00 -0700400 return;
401#endif // PDF_ENABLE_XFA
402
403 float fBorderWidth = m_pList->GetBorderWidth() * 2;
404 float fPopupMin = 0.0f;
405 if (m_pList->GetCount() > 3)
406 fPopupMin = m_pList->GetFirstHeight() * 3 + fBorderWidth;
407 float fPopupMax = fListHeight + fBorderWidth;
408
409 bool bBottom;
410 float fPopupRet;
411 m_pFillerNotify->QueryWherePopup(GetAttachedData(), fPopupMin, fPopupMax,
412 &bBottom, &fPopupRet);
413 if (!IsFloatBigger(fPopupRet, 0.0f))
414 return;
415
416 m_rcOldWindow = CPWL_Wnd::GetWindowRect();
417 m_bPopup = bPopup;
418 m_bBottom = bBottom;
419
420 CFX_FloatRect rcWindow = m_rcOldWindow;
421 if (bBottom)
422 rcWindow.bottom -= fPopupRet;
423 else
424 rcWindow.top += fPopupRet;
425
426 Move(rcWindow, true, true);
427#ifdef PDF_ENABLE_XFA
Dan Sinclair5fe98082017-07-06 11:13:02 -0400428 m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), 0);
Lei Zhang7bef7c82017-05-31 23:52:00 -0700429#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430}
431
tsepez4cf55152016-11-02 14:37:54 -0700432bool CPWL_ComboBox::OnKeyDown(uint16_t nChar, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 if (!m_pList)
tsepez4cf55152016-11-02 14:37:54 -0700434 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 if (!m_pEdit)
tsepez4cf55152016-11-02 14:37:54 -0700436 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437
438 m_nSelectItem = -1;
439
440 switch (nChar) {
441 case FWL_VKEY_Up:
442 if (m_pList->GetCurSel() > 0) {
Tom Sepez51da0932015-11-25 16:05:49 -0800443#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444 if (m_pFillerNotify) {
Dan Sinclair5fe98082017-07-06 11:13:02 -0400445 if (m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), nFlag))
tsepez4cf55152016-11-02 14:37:54 -0700446 return false;
Dan Sinclair5fe98082017-07-06 11:13:02 -0400447 if (m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), nFlag))
tsepez4cf55152016-11-02 14:37:54 -0700448 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800450#endif // PDF_ENABLE_XFA
Dan Sinclair07dbf432017-07-06 11:47:26 -0400451 if (m_pList->IsMovementKey(nChar)) {
452 if (m_pList->OnMovementKeyDown(nChar, nFlag))
tsepez4cf55152016-11-02 14:37:54 -0700453 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 SetSelectText();
455 }
456 }
tsepez4cf55152016-11-02 14:37:54 -0700457 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458 case FWL_VKEY_Down:
459 if (m_pList->GetCurSel() < m_pList->GetCount() - 1) {
Tom Sepez51da0932015-11-25 16:05:49 -0800460#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 if (m_pFillerNotify) {
Dan Sinclair5fe98082017-07-06 11:13:02 -0400462 if (m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), nFlag))
tsepez4cf55152016-11-02 14:37:54 -0700463 return false;
Dan Sinclair5fe98082017-07-06 11:13:02 -0400464 if (m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), nFlag))
tsepez4cf55152016-11-02 14:37:54 -0700465 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800467#endif // PDF_ENABLE_XFA
Dan Sinclair07dbf432017-07-06 11:47:26 -0400468 if (m_pList->IsMovementKey(nChar)) {
469 if (m_pList->OnMovementKeyDown(nChar, nFlag))
tsepez4cf55152016-11-02 14:37:54 -0700470 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 SetSelectText();
472 }
473 }
tsepez4cf55152016-11-02 14:37:54 -0700474 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475 }
476
477 if (HasFlag(PCBS_ALLOWCUSTOMTEXT))
478 return m_pEdit->OnKeyDown(nChar, nFlag);
479
tsepez4cf55152016-11-02 14:37:54 -0700480 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481}
482
tsepez4cf55152016-11-02 14:37:54 -0700483bool CPWL_ComboBox::OnChar(uint16_t nChar, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484 if (!m_pList)
tsepez4cf55152016-11-02 14:37:54 -0700485 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486
487 if (!m_pEdit)
tsepez4cf55152016-11-02 14:37:54 -0700488 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489
490 m_nSelectItem = -1;
491 if (HasFlag(PCBS_ALLOWCUSTOMTEXT))
492 return m_pEdit->OnChar(nChar, nFlag);
493
Tom Sepez51da0932015-11-25 16:05:49 -0800494#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 if (m_pFillerNotify) {
Dan Sinclair5fe98082017-07-06 11:13:02 -0400496 if (m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), nFlag))
tsepez4cf55152016-11-02 14:37:54 -0700497 return false;
Dan Sinclair5fe98082017-07-06 11:13:02 -0400498 if (m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), nFlag))
tsepez4cf55152016-11-02 14:37:54 -0700499 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800501#endif // PDF_ENABLE_XFA
Dan Sinclair16fea942017-07-06 11:56:37 -0400502 if (!m_pList->IsChar(nChar, nFlag))
503 return false;
504 return m_pList->OnCharNotify(nChar, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505}
506
Dan Sinclair7f6bec92017-07-05 14:13:16 -0400507void CPWL_ComboBox::NotifyLButtonDown(CPWL_Wnd* child, const CFX_PointF& pos) {
508 if (child == m_pButton)
509 SetPopup(!m_bPopup);
510}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700511
Dan Sinclair7f6bec92017-07-05 14:13:16 -0400512void CPWL_ComboBox::NotifyLButtonUp(CPWL_Wnd* child, const CFX_PointF& pos) {
513 if (!m_pEdit || !m_pList || child != m_pList)
514 return;
515
516 SetSelectText();
517 SelectAll();
518 m_pEdit->SetFocus();
519 SetPopup(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520}
521
tsepez4cf55152016-11-02 14:37:54 -0700522bool CPWL_ComboBox::IsPopup() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523 return m_bPopup;
524}
525
526void CPWL_ComboBox::SetSelectText() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527 m_pEdit->SelectAll();
tsepez067990c2016-09-13 06:46:40 -0700528 m_pEdit->ReplaceSel(m_pList->GetText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700529 m_pEdit->SelectAll();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700530 m_nSelectItem = m_pList->GetCurSel();
531}
532
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700533void CPWL_ComboBox::SetFillerNotify(IPWL_Filler_Notify* pNotify) {
534 m_pFillerNotify = pNotify;
535
536 if (m_pEdit)
537 m_pEdit->SetFillerNotify(pNotify);
538
539 if (m_pList)
540 m_pList->SetFillerNotify(pNotify);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700541}