blob: 9f5ab9785856c662e4ec2fafc37e9a8a6bfd2c3a [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 sinclair89e904b2016-03-23 19:29:15 -04007#include "fpdfsdk/pdfwindow/PWL_ComboBox.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -08008
dsinclair74a34fc2016-09-29 16:41:42 -07009#include "core/fxge/cfx_pathdata.h"
10#include "core/fxge/cfx_renderdevice.h"
dsinclair0bb385b2016-09-29 17:03:59 -070011#include "fpdfsdk/fxedit/fxet_list.h"
dan sinclair89e904b2016-03-23 19:29:15 -040012#include "fpdfsdk/pdfwindow/PWL_Edit.h"
13#include "fpdfsdk/pdfwindow/PWL_EditCtrl.h"
14#include "fpdfsdk/pdfwindow/PWL_ListBox.h"
15#include "fpdfsdk/pdfwindow/PWL_Utils.h"
16#include "fpdfsdk/pdfwindow/PWL_Wnd.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -080017#include "public/fpdf_fwlevent.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019#define PWLCB_DEFAULTFONTSIZE 12.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Dan Sinclairf528eee2017-02-14 11:52:07 -050021bool CPWL_CBListBox::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022 CPWL_Wnd::OnLButtonUp(point, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
dsinclair36177512016-07-19 10:16:10 -070024 if (!m_bMouseDown)
tsepez4cf55152016-11-02 14:37:54 -070025 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
dsinclair36177512016-07-19 10:16:10 -070027 ReleaseCapture();
tsepez4cf55152016-11-02 14:37:54 -070028 m_bMouseDown = false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070029
dsinclair36177512016-07-19 10:16:10 -070030 if (!ClientHitTest(point))
tsepez4cf55152016-11-02 14:37:54 -070031 return true;
dsinclair36177512016-07-19 10:16:10 -070032 if (CPWL_Wnd* pParent = GetParentWindow())
33 pParent->OnNotify(this, PNM_LBUTTONUP, 0, PWL_MAKEDWORD(point.x, point.y));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034
tsepez4cf55152016-11-02 14:37:54 -070035 bool bExit = false;
36 OnNotifySelChanged(false, bExit, nFlag);
dsinclair36177512016-07-19 10:16:10 -070037
38 return !bExit;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039}
40
tsepez4cf55152016-11-02 14:37:54 -070041bool CPWL_CBListBox::OnKeyDownWithExit(uint16_t nChar,
42 bool& bExit,
43 uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 switch (nChar) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 case FWL_VKEY_Up:
46 case FWL_VKEY_Down:
47 case FWL_VKEY_Home:
48 case FWL_VKEY_Left:
49 case FWL_VKEY_End:
50 case FWL_VKEY_Right:
51 break;
dsinclair36177512016-07-19 10:16:10 -070052 default:
tsepez4cf55152016-11-02 14:37:54 -070053 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 switch (nChar) {
57 case FWL_VKEY_Up:
58 m_pList->OnVK_UP(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
59 break;
60 case FWL_VKEY_Down:
61 m_pList->OnVK_DOWN(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
62 break;
63 case FWL_VKEY_Home:
64 m_pList->OnVK_HOME(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
65 break;
66 case FWL_VKEY_Left:
67 m_pList->OnVK_LEFT(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
68 break;
69 case FWL_VKEY_End:
70 m_pList->OnVK_END(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
71 break;
72 case FWL_VKEY_Right:
73 m_pList->OnVK_RIGHT(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
74 break;
75 case FWL_VKEY_Delete:
76 break;
77 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078
tsepez4cf55152016-11-02 14:37:54 -070079 OnNotifySelChanged(true, bExit, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080
tsepez4cf55152016-11-02 14:37:54 -070081 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082}
83
tsepez4cf55152016-11-02 14:37:54 -070084bool CPWL_CBListBox::OnCharWithExit(uint16_t nChar,
85 bool& bExit,
86 uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 if (!m_pList->OnChar(nChar, IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag)))
tsepez4cf55152016-11-02 14:37:54 -070088 return false;
dsinclair36177512016-07-19 10:16:10 -070089 if (CPWL_ComboBox* pComboBox = (CPWL_ComboBox*)GetParentWindow())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 pComboBox->SetSelectText();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091
tsepez4cf55152016-11-02 14:37:54 -070092 OnNotifySelChanged(true, bExit, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093
tsepez4cf55152016-11-02 14:37:54 -070094 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095}
96
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097void CPWL_CBButton::GetThisAppearanceStream(CFX_ByteTextBuf& sAppStream) {
98 CPWL_Wnd::GetThisAppearanceStream(sAppStream);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070099
Tom Sepez281a9ea2016-02-26 14:24:28 -0800100 CFX_FloatRect rectWnd = CPWL_Wnd::GetWindowRect();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 if (IsVisible() && !rectWnd.IsEmpty()) {
103 CFX_ByteTextBuf sButton;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104
Dan Sinclairf528eee2017-02-14 11:52:07 -0500105 CFX_PointF ptCenter = GetCenterPoint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106
Dan Sinclairf528eee2017-02-14 11:52:07 -0500107 CFX_PointF pt1(ptCenter.x - PWL_CBBUTTON_TRIANGLE_HALFLEN,
108 ptCenter.y + PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
109 CFX_PointF pt2(ptCenter.x + PWL_CBBUTTON_TRIANGLE_HALFLEN,
110 ptCenter.y + PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
111 CFX_PointF pt3(ptCenter.x,
112 ptCenter.y - PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700113
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 if (IsFloatBigger(rectWnd.right - rectWnd.left,
115 PWL_CBBUTTON_TRIANGLE_HALFLEN * 2) &&
116 IsFloatBigger(rectWnd.top - rectWnd.bottom,
117 PWL_CBBUTTON_TRIANGLE_HALFLEN)) {
118 sButton << "0 g\n";
119 sButton << pt1.x << " " << pt1.y << " m\n";
120 sButton << pt2.x << " " << pt2.y << " l\n";
121 sButton << pt3.x << " " << pt3.y << " l\n";
122 sButton << pt1.x << " " << pt1.y << " l f\n";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 sAppStream << "q\n" << sButton << "Q\n";
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700125 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127}
128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129void CPWL_CBButton::DrawThisAppearance(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -0800130 CFX_Matrix* pUser2Device) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 CPWL_Wnd::DrawThisAppearance(pDevice, pUser2Device);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132
Tom Sepez281a9ea2016-02-26 14:24:28 -0800133 CFX_FloatRect rectWnd = CPWL_Wnd::GetWindowRect();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700134
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 if (IsVisible() && !rectWnd.IsEmpty()) {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500136 CFX_PointF ptCenter = GetCenterPoint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137
Dan Sinclairf528eee2017-02-14 11:52:07 -0500138 CFX_PointF pt1(ptCenter.x - PWL_CBBUTTON_TRIANGLE_HALFLEN,
139 ptCenter.y + PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
140 CFX_PointF pt2(ptCenter.x + PWL_CBBUTTON_TRIANGLE_HALFLEN,
141 ptCenter.y + PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
142 CFX_PointF pt3(ptCenter.x,
143 ptCenter.y - PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700144
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 if (IsFloatBigger(rectWnd.right - rectWnd.left,
146 PWL_CBBUTTON_TRIANGLE_HALFLEN * 2) &&
147 IsFloatBigger(rectWnd.top - rectWnd.bottom,
148 PWL_CBBUTTON_TRIANGLE_HALFLEN)) {
149 CFX_PathData path;
dan sinclairb147e072017-02-22 19:56:15 -0500150 path.AppendPoint(pt1, FXPT_TYPE::MoveTo, false);
151 path.AppendPoint(pt2, FXPT_TYPE::LineTo, false);
152 path.AppendPoint(pt3, FXPT_TYPE::LineTo, false);
153 path.AppendPoint(pt1, FXPT_TYPE::LineTo, false);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154
thestig1cd352e2016-06-07 17:53:06 -0700155 pDevice->DrawPath(&path, pUser2Device, nullptr,
Dan Sinclairfc54e052017-02-23 09:59:05 -0500156 PWL_DEFAULT_BLACKCOLOR.ToFXColor(GetTransparency()), 0,
157 FXFILL_ALTERNATE);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700158 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160}
161
Dan Sinclairf528eee2017-02-14 11:52:07 -0500162bool CPWL_CBButton::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 CPWL_Wnd::OnLButtonDown(point, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 SetCapture();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700166
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 if (CPWL_Wnd* pParent = GetParentWindow()) {
168 pParent->OnNotify(this, PNM_LBUTTONDOWN, 0,
169 PWL_MAKEDWORD(point.x, point.y));
170 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700171
tsepez4cf55152016-11-02 14:37:54 -0700172 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173}
174
Dan Sinclairf528eee2017-02-14 11:52:07 -0500175bool CPWL_CBButton::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 CPWL_Wnd::OnLButtonUp(point, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 ReleaseCapture();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179
tsepez4cf55152016-11-02 14:37:54 -0700180 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181}
182
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183CPWL_ComboBox::CPWL_ComboBox()
Tom Sepezb084c1f2017-05-12 14:04:06 -0700184 : m_bPopup(false),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 m_nPopupWhere(0),
186 m_nSelectItem(-1),
thestig1cd352e2016-06-07 17:53:06 -0700187 m_pFillerNotify(nullptr) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188
Tom Sepezb084c1f2017-05-12 14:04:06 -0700189CPWL_ComboBox::~CPWL_ComboBox() {}
190
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191CFX_ByteString CPWL_ComboBox::GetClassName() const {
192 return "CPWL_ComboBox";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193}
194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195void CPWL_ComboBox::OnCreate(PWL_CREATEPARAM& cp) {
196 cp.dwFlags &= ~PWS_HSCROLL;
197 cp.dwFlags &= ~PWS_VSCROLL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200void CPWL_ComboBox::SetFocus() {
201 if (m_pEdit)
202 m_pEdit->SetFocus();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203}
204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205void CPWL_ComboBox::KillFocus() {
tsepez4cf55152016-11-02 14:37:54 -0700206 SetPopup(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 CPWL_Wnd::KillFocus();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700208}
209
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210CFX_WideString CPWL_ComboBox::GetText() const {
211 if (m_pEdit) {
212 return m_pEdit->GetText();
213 }
214 return CFX_WideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700215}
216
tsepez067990c2016-09-13 06:46:40 -0700217void CPWL_ComboBox::SetText(const CFX_WideString& text) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 if (m_pEdit)
219 m_pEdit->SetText(text);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220}
221
tsepez067990c2016-09-13 06:46:40 -0700222void CPWL_ComboBox::AddString(const CFX_WideString& str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 if (m_pList)
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500224 m_pList->AddString(str);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225}
226
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227int32_t CPWL_ComboBox::GetSelect() const {
228 return m_nSelectItem;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229}
230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231void CPWL_ComboBox::SetSelect(int32_t nItemIndex) {
232 if (m_pList)
233 m_pList->Select(nItemIndex);
234
tsepez067990c2016-09-13 06:46:40 -0700235 m_pEdit->SetText(m_pList->GetText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 m_nSelectItem = nItemIndex;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237}
238
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239void CPWL_ComboBox::SetEditSel(int32_t nStartChar, int32_t nEndChar) {
tsepez067990c2016-09-13 06:46:40 -0700240 if (m_pEdit)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 m_pEdit->SetSel(nStartChar, nEndChar);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700242}
243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244void CPWL_ComboBox::GetEditSel(int32_t& nStartChar, int32_t& nEndChar) const {
245 nStartChar = -1;
246 nEndChar = -1;
247
tsepez067990c2016-09-13 06:46:40 -0700248 if (m_pEdit)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 m_pEdit->GetSel(nStartChar, nEndChar);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250}
251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252void CPWL_ComboBox::Clear() {
tsepez067990c2016-09-13 06:46:40 -0700253 if (m_pEdit)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 m_pEdit->Clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255}
256
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257void CPWL_ComboBox::CreateChildWnd(const PWL_CREATEPARAM& cp) {
258 CreateEdit(cp);
259 CreateButton(cp);
260 CreateListBox(cp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700261}
262
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263void CPWL_ComboBox::CreateEdit(const PWL_CREATEPARAM& cp) {
Tom Sepezb084c1f2017-05-12 14:04:06 -0700264 if (m_pEdit)
265 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266
Tom Sepezb084c1f2017-05-12 14:04:06 -0700267 m_pEdit = pdfium::MakeUnique<CPWL_CBEdit>();
Tom Sepezcc205132017-05-16 14:01:47 -0700268 m_pEdit->AttachFFLData(m_pFormFiller.Get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269
Tom Sepezb084c1f2017-05-12 14:04:06 -0700270 PWL_CREATEPARAM ecp = cp;
271 ecp.pParentWnd = this;
272 ecp.dwFlags = PWS_VISIBLE | PWS_CHILD | PWS_BORDER | PES_CENTER |
273 PES_AUTOSCROLL | PES_UNDO;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274
Tom Sepezb084c1f2017-05-12 14:04:06 -0700275 if (HasFlag(PWS_AUTOFONTSIZE))
276 ecp.dwFlags |= PWS_AUTOFONTSIZE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277
Tom Sepezb084c1f2017-05-12 14:04:06 -0700278 if (!HasFlag(PCBS_ALLOWCUSTOMTEXT))
279 ecp.dwFlags |= PWS_READONLY;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280
Tom Sepezb084c1f2017-05-12 14:04:06 -0700281 ecp.rcRectWnd = CFX_FloatRect(0, 0, 0, 0);
282 ecp.dwBorderWidth = 0;
283 ecp.nBorderStyle = BorderStyle::SOLID;
284 m_pEdit->Create(ecp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285}
286
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287void CPWL_ComboBox::CreateButton(const PWL_CREATEPARAM& cp) {
Tom Sepezb084c1f2017-05-12 14:04:06 -0700288 if (m_pButton)
289 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290
Tom Sepezb084c1f2017-05-12 14:04:06 -0700291 m_pButton = pdfium::MakeUnique<CPWL_CBButton>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292
Tom Sepezb084c1f2017-05-12 14:04:06 -0700293 PWL_CREATEPARAM bcp = cp;
294 bcp.pParentWnd = this;
295 bcp.dwFlags = PWS_VISIBLE | PWS_CHILD | PWS_BORDER | PWS_BACKGROUND;
296 bcp.sBackgroundColor = PWL_SCROLLBAR_BKCOLOR;
297 bcp.sBorderColor = PWL_DEFAULT_BLACKCOLOR;
298 bcp.dwBorderWidth = 2;
299 bcp.nBorderStyle = BorderStyle::BEVELED;
300 bcp.eCursorType = FXCT_ARROW;
301 m_pButton->Create(bcp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700302}
303
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304void CPWL_ComboBox::CreateListBox(const PWL_CREATEPARAM& cp) {
Tom Sepezb084c1f2017-05-12 14:04:06 -0700305 if (m_pList)
306 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307
Tom Sepezb084c1f2017-05-12 14:04:06 -0700308 m_pList = pdfium::MakeUnique<CPWL_CBListBox>();
Tom Sepezcc205132017-05-16 14:01:47 -0700309 m_pList->AttachFFLData(m_pFormFiller.Get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700310
Tom Sepezb084c1f2017-05-12 14:04:06 -0700311 PWL_CREATEPARAM lcp = cp;
312 lcp.pParentWnd = this;
313 lcp.dwFlags =
314 PWS_CHILD | PWS_BORDER | PWS_BACKGROUND | PLBS_HOVERSEL | PWS_VSCROLL;
315 lcp.nBorderStyle = BorderStyle::SOLID;
316 lcp.dwBorderWidth = 1;
317 lcp.eCursorType = FXCT_ARROW;
318 lcp.rcRectWnd = CFX_FloatRect(0, 0, 0, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319
Tom Sepezb084c1f2017-05-12 14:04:06 -0700320 if (cp.dwFlags & PWS_AUTOFONTSIZE)
321 lcp.fFontSize = PWLCB_DEFAULTFONTSIZE;
322 else
323 lcp.fFontSize = cp.fFontSize;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324
Tom Sepezb084c1f2017-05-12 14:04:06 -0700325 if (cp.sBorderColor.nColorType == COLORTYPE_TRANSPARENT)
326 lcp.sBorderColor = PWL_DEFAULT_BLACKCOLOR;
327
328 if (cp.sBackgroundColor.nColorType == COLORTYPE_TRANSPARENT)
329 lcp.sBackgroundColor = PWL_DEFAULT_WHITECOLOR;
330
331 m_pList->Create(lcp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332}
333
334void CPWL_ComboBox::RePosChildWnd() {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800335 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336
337 if (m_bPopup) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800338 CFX_FloatRect rclient = GetClientRect();
339 CFX_FloatRect rcButton = rclient;
340 CFX_FloatRect rcEdit = rcClient;
341 CFX_FloatRect rcList = CPWL_Wnd::GetWindowRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342
Dan Sinclair05df0752017-03-14 14:43:42 -0400343 float fOldWindowHeight = m_rcOldWindow.Height();
344 float fOldClientHeight = fOldWindowHeight - GetBorderWidth() * 2;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345
346 switch (m_nPopupWhere) {
347 case 0:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700348 rcButton.left = rcButton.right - PWL_COMBOBOX_BUTTON_WIDTH;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700349
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350 if (rcButton.left < rclient.left)
351 rcButton.left = rclient.left;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 rcButton.bottom = rcButton.top - fOldClientHeight;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700354
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700355 rcEdit.right = rcButton.left - 1.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700356
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 if (rcEdit.left < rclient.left)
358 rcEdit.left = rclient.left;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700359
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700360 if (rcEdit.right < rcEdit.left)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361 rcEdit.right = rcEdit.left;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700362
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 rcEdit.bottom = rcEdit.top - fOldClientHeight;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700364
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 rcList.top -= fOldWindowHeight;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700366
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700367 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 case 1:
369 rcButton.left = rcButton.right - PWL_COMBOBOX_BUTTON_WIDTH;
370
371 if (rcButton.left < rclient.left)
372 rcButton.left = rclient.left;
373
374 rcButton.top = rcButton.bottom + fOldClientHeight;
375
376 rcEdit.right = rcButton.left - 1.0f;
377
378 if (rcEdit.left < rclient.left)
379 rcEdit.left = rclient.left;
380
381 if (rcEdit.right < rcEdit.left)
382 rcEdit.right = rcEdit.left;
383
384 rcEdit.top = rcEdit.bottom + fOldClientHeight;
385
386 rcList.bottom += fOldWindowHeight;
387
388 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700389 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700390
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391 if (m_pButton)
tsepez4cf55152016-11-02 14:37:54 -0700392 m_pButton->Move(rcButton, true, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393
394 if (m_pEdit)
tsepez4cf55152016-11-02 14:37:54 -0700395 m_pEdit->Move(rcEdit, true, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396
397 if (m_pList) {
tsepez4cf55152016-11-02 14:37:54 -0700398 m_pList->SetVisible(true);
399 m_pList->Move(rcList, true, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 m_pList->ScrollToListItem(m_nSelectItem);
401 }
402 } else {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800403 CFX_FloatRect rcButton = rcClient;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404
405 rcButton.left = rcButton.right - PWL_COMBOBOX_BUTTON_WIDTH;
406
407 if (rcButton.left < rcClient.left)
408 rcButton.left = rcClient.left;
409
410 if (m_pButton)
tsepez4cf55152016-11-02 14:37:54 -0700411 m_pButton->Move(rcButton, true, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412
Tom Sepez281a9ea2016-02-26 14:24:28 -0800413 CFX_FloatRect rcEdit = rcClient;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414 rcEdit.right = rcButton.left - 1.0f;
415
416 if (rcEdit.left < rcClient.left)
417 rcEdit.left = rcClient.left;
418
419 if (rcEdit.right < rcEdit.left)
420 rcEdit.right = rcEdit.left;
421
422 if (m_pEdit)
tsepez4cf55152016-11-02 14:37:54 -0700423 m_pEdit->Move(rcEdit, true, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424
425 if (m_pList)
tsepez4cf55152016-11-02 14:37:54 -0700426 m_pList->SetVisible(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428}
429
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430void CPWL_ComboBox::SelectAll() {
431 if (m_pEdit && HasFlag(PCBS_ALLOWCUSTOMTEXT))
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700432 m_pEdit->SelectAll();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700433}
434
Tom Sepez281a9ea2016-02-26 14:24:28 -0800435CFX_FloatRect CPWL_ComboBox::GetFocusRect() const {
436 return CFX_FloatRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700437}
438
tsepez4cf55152016-11-02 14:37:54 -0700439void CPWL_ComboBox::SetPopup(bool bPopup) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440 if (!m_pList)
441 return;
442 if (bPopup == m_bPopup)
443 return;
Dan Sinclair05df0752017-03-14 14:43:42 -0400444 float fListHeight = m_pList->GetContentRect().Height();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700445 if (!IsFloatBigger(fListHeight, 0.0f))
446 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700447
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 if (bPopup) {
449 if (m_pFillerNotify) {
Tom Sepez51da0932015-11-25 16:05:49 -0800450#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700451 bool bExit = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), bExit, 0);
453 if (bExit)
454 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800455#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 int32_t nWhere = 0;
Dan Sinclair05df0752017-03-14 14:43:42 -0400457 float fPopupRet = 0.0f;
458 float fPopupMin = 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 if (m_pList->GetCount() > 3)
460 fPopupMin =
461 m_pList->GetFirstHeight() * 3 + m_pList->GetBorderWidth() * 2;
Dan Sinclair05df0752017-03-14 14:43:42 -0400462 float fPopupMax = fListHeight + m_pList->GetBorderWidth() * 2;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 m_pFillerNotify->QueryWherePopup(GetAttachedData(), fPopupMin, fPopupMax,
464 nWhere, fPopupRet);
465
466 if (IsFloatBigger(fPopupRet, 0.0f)) {
467 m_bPopup = bPopup;
468
Tom Sepez281a9ea2016-02-26 14:24:28 -0800469 CFX_FloatRect rcWindow = CPWL_Wnd::GetWindowRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 m_rcOldWindow = rcWindow;
471 switch (nWhere) {
472 default:
473 case 0:
474 rcWindow.bottom -= fPopupRet;
475 break;
476 case 1:
477 rcWindow.top += fPopupRet;
478 break;
479 }
480
481 m_nPopupWhere = nWhere;
tsepez4cf55152016-11-02 14:37:54 -0700482 Move(rcWindow, true, true);
Tom Sepez51da0932015-11-25 16:05:49 -0800483#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700484 bExit = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485 m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), bExit, 0);
486 if (bExit)
487 return;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800488#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489 }
490 }
491 } else {
492 m_bPopup = bPopup;
tsepez4cf55152016-11-02 14:37:54 -0700493 Move(m_rcOldWindow, true, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494 }
495}
496
tsepez4cf55152016-11-02 14:37:54 -0700497bool CPWL_ComboBox::OnKeyDown(uint16_t nChar, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 if (!m_pList)
tsepez4cf55152016-11-02 14:37:54 -0700499 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500 if (!m_pEdit)
tsepez4cf55152016-11-02 14:37:54 -0700501 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502
503 m_nSelectItem = -1;
504
505 switch (nChar) {
506 case FWL_VKEY_Up:
507 if (m_pList->GetCurSel() > 0) {
tsepez4cf55152016-11-02 14:37:54 -0700508 bool bExit = false;
Tom Sepez51da0932015-11-25 16:05:49 -0800509#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 if (m_pFillerNotify) {
511 m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), bExit, nFlag);
512 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700513 return false;
514 bExit = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515 m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), bExit, nFlag);
516 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700517 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800519#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520 if (m_pList->OnKeyDownWithExit(nChar, bExit, nFlag)) {
521 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700522 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523 SetSelectText();
524 }
525 }
tsepez4cf55152016-11-02 14:37:54 -0700526 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527 case FWL_VKEY_Down:
528 if (m_pList->GetCurSel() < m_pList->GetCount() - 1) {
tsepez4cf55152016-11-02 14:37:54 -0700529 bool bExit = false;
Tom Sepez51da0932015-11-25 16:05:49 -0800530#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700531 if (m_pFillerNotify) {
532 m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), bExit, nFlag);
533 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700534 return false;
535 bExit = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536 m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), bExit, nFlag);
537 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700538 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700539 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800540#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 if (m_pList->OnKeyDownWithExit(nChar, bExit, nFlag)) {
542 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700543 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544 SetSelectText();
545 }
546 }
tsepez4cf55152016-11-02 14:37:54 -0700547 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700548 }
549
550 if (HasFlag(PCBS_ALLOWCUSTOMTEXT))
551 return m_pEdit->OnKeyDown(nChar, nFlag);
552
tsepez4cf55152016-11-02 14:37:54 -0700553 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554}
555
tsepez4cf55152016-11-02 14:37:54 -0700556bool CPWL_ComboBox::OnChar(uint16_t nChar, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700557 if (!m_pList)
tsepez4cf55152016-11-02 14:37:54 -0700558 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559
560 if (!m_pEdit)
tsepez4cf55152016-11-02 14:37:54 -0700561 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562
563 m_nSelectItem = -1;
564 if (HasFlag(PCBS_ALLOWCUSTOMTEXT))
565 return m_pEdit->OnChar(nChar, nFlag);
566
tsepez4cf55152016-11-02 14:37:54 -0700567 bool bExit = false;
Tom Sepez51da0932015-11-25 16:05:49 -0800568#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 if (m_pFillerNotify) {
570 m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), bExit, nFlag);
571 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700572 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573
574 m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), bExit, nFlag);
575 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700576 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700577 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800578#endif // PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700579 return m_pList->OnCharWithExit(nChar, bExit, nFlag) ? bExit : false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580}
581
582void CPWL_ComboBox::OnNotify(CPWL_Wnd* pWnd,
tsepezc3255f52016-03-25 14:52:27 -0700583 uint32_t msg,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584 intptr_t wParam,
585 intptr_t lParam) {
586 switch (msg) {
587 case PNM_LBUTTONDOWN:
Tom Sepezb084c1f2017-05-12 14:04:06 -0700588 if (pWnd == m_pButton.get()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700589 SetPopup(!m_bPopup);
590 return;
591 }
592 break;
593 case PNM_LBUTTONUP:
594 if (m_pEdit && m_pList) {
Tom Sepezb084c1f2017-05-12 14:04:06 -0700595 if (pWnd == m_pList.get()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596 SetSelectText();
597 SelectAll();
598 m_pEdit->SetFocus();
tsepez4cf55152016-11-02 14:37:54 -0700599 SetPopup(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600 return;
601 }
602 }
Tom Sepezb084c1f2017-05-12 14:04:06 -0700603 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 }
605
606 CPWL_Wnd::OnNotify(pWnd, msg, wParam, lParam);
607}
608
tsepez4cf55152016-11-02 14:37:54 -0700609bool CPWL_ComboBox::IsPopup() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700610 return m_bPopup;
611}
612
613void CPWL_ComboBox::SetSelectText() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 m_pEdit->SelectAll();
tsepez067990c2016-09-13 06:46:40 -0700615 m_pEdit->ReplaceSel(m_pList->GetText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616 m_pEdit->SelectAll();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 m_nSelectItem = m_pList->GetCurSel();
618}
619
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700620void CPWL_ComboBox::SetFillerNotify(IPWL_Filler_Notify* pNotify) {
621 m_pFillerNotify = pNotify;
622
623 if (m_pEdit)
624 m_pEdit->SetFillerNotify(pNotify);
625
626 if (m_pList)
627 m_pList->SetFillerNotify(pNotify);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700628}