blob: 42786806d0fb5b82cc70288831bc3f36c0d224c7 [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
Lei Zhang633a3b72017-06-02 15:27:22 -07007#include "fpdfsdk/pdfwindow/cpwl_combo_box.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -08008
Lei Zhang7bef7c82017-05-31 23:52:00 -07009#include <algorithm>
10
dsinclair74a34fc2016-09-29 16:41:42 -070011#include "core/fxge/cfx_pathdata.h"
12#include "core/fxge/cfx_renderdevice.h"
dsinclair0bb385b2016-09-29 17:03:59 -070013#include "fpdfsdk/fxedit/fxet_list.h"
Lei Zhang633a3b72017-06-02 15:27:22 -070014#include "fpdfsdk/pdfwindow/cpwl_edit.h"
15#include "fpdfsdk/pdfwindow/cpwl_edit_ctrl.h"
16#include "fpdfsdk/pdfwindow/cpwl_list_box.h"
17#include "fpdfsdk/pdfwindow/cpwl_utils.h"
18#include "fpdfsdk/pdfwindow/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
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021#define PWLCB_DEFAULTFONTSIZE 12.0f
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
Dan Sinclairf528eee2017-02-14 11:52:07 -050023bool CPWL_CBListBox::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 CPWL_Wnd::OnLButtonUp(point, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025
dsinclair36177512016-07-19 10:16:10 -070026 if (!m_bMouseDown)
tsepez4cf55152016-11-02 14:37:54 -070027 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028
dsinclair36177512016-07-19 10:16:10 -070029 ReleaseCapture();
tsepez4cf55152016-11-02 14:37:54 -070030 m_bMouseDown = false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070031
dsinclair36177512016-07-19 10:16:10 -070032 if (!ClientHitTest(point))
tsepez4cf55152016-11-02 14:37:54 -070033 return true;
dsinclair36177512016-07-19 10:16:10 -070034 if (CPWL_Wnd* pParent = GetParentWindow())
35 pParent->OnNotify(this, PNM_LBUTTONUP, 0, PWL_MAKEDWORD(point.x, point.y));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036
tsepez4cf55152016-11-02 14:37:54 -070037 bool bExit = false;
38 OnNotifySelChanged(false, bExit, nFlag);
dsinclair36177512016-07-19 10:16:10 -070039
40 return !bExit;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041}
42
tsepez4cf55152016-11-02 14:37:54 -070043bool CPWL_CBListBox::OnKeyDownWithExit(uint16_t nChar,
44 bool& bExit,
45 uint32_t nFlag) {
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:
53 break;
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 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 switch (nChar) {
59 case FWL_VKEY_Up:
60 m_pList->OnVK_UP(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
61 break;
62 case FWL_VKEY_Down:
63 m_pList->OnVK_DOWN(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
64 break;
65 case FWL_VKEY_Home:
66 m_pList->OnVK_HOME(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
67 break;
68 case FWL_VKEY_Left:
69 m_pList->OnVK_LEFT(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
70 break;
71 case FWL_VKEY_End:
72 m_pList->OnVK_END(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
73 break;
74 case FWL_VKEY_Right:
75 m_pList->OnVK_RIGHT(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
76 break;
77 case FWL_VKEY_Delete:
78 break;
79 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080
tsepez4cf55152016-11-02 14:37:54 -070081 OnNotifySelChanged(true, bExit, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082
tsepez4cf55152016-11-02 14:37:54 -070083 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084}
85
tsepez4cf55152016-11-02 14:37:54 -070086bool CPWL_CBListBox::OnCharWithExit(uint16_t nChar,
87 bool& bExit,
88 uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 if (!m_pList->OnChar(nChar, IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag)))
tsepez4cf55152016-11-02 14:37:54 -070090 return false;
dsinclair36177512016-07-19 10:16:10 -070091 if (CPWL_ComboBox* pComboBox = (CPWL_ComboBox*)GetParentWindow())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 pComboBox->SetSelectText();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093
tsepez4cf55152016-11-02 14:37:54 -070094 OnNotifySelChanged(true, bExit, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095
tsepez4cf55152016-11-02 14:37:54 -070096 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097}
98
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099void CPWL_CBButton::GetThisAppearanceStream(CFX_ByteTextBuf& sAppStream) {
100 CPWL_Wnd::GetThisAppearanceStream(sAppStream);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700101
Tom Sepez281a9ea2016-02-26 14:24:28 -0800102 CFX_FloatRect rectWnd = CPWL_Wnd::GetWindowRect();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 if (IsVisible() && !rectWnd.IsEmpty()) {
105 CFX_ByteTextBuf sButton;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106
Dan Sinclairf528eee2017-02-14 11:52:07 -0500107 CFX_PointF ptCenter = GetCenterPoint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108
Dan Sinclairf528eee2017-02-14 11:52:07 -0500109 CFX_PointF pt1(ptCenter.x - PWL_CBBUTTON_TRIANGLE_HALFLEN,
110 ptCenter.y + PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
111 CFX_PointF pt2(ptCenter.x + PWL_CBBUTTON_TRIANGLE_HALFLEN,
112 ptCenter.y + PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
113 CFX_PointF pt3(ptCenter.x,
114 ptCenter.y - PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 if (IsFloatBigger(rectWnd.right - rectWnd.left,
117 PWL_CBBUTTON_TRIANGLE_HALFLEN * 2) &&
118 IsFloatBigger(rectWnd.top - rectWnd.bottom,
119 PWL_CBBUTTON_TRIANGLE_HALFLEN)) {
120 sButton << "0 g\n";
121 sButton << pt1.x << " " << pt1.y << " m\n";
122 sButton << pt2.x << " " << pt2.y << " l\n";
123 sButton << pt3.x << " " << pt3.y << " l\n";
124 sButton << pt1.x << " " << pt1.y << " l f\n";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 sAppStream << "q\n" << sButton << "Q\n";
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700127 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129}
130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131void CPWL_CBButton::DrawThisAppearance(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -0800132 CFX_Matrix* pUser2Device) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 CPWL_Wnd::DrawThisAppearance(pDevice, pUser2Device);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134
Tom Sepez281a9ea2016-02-26 14:24:28 -0800135 CFX_FloatRect rectWnd = CPWL_Wnd::GetWindowRect();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 if (IsVisible() && !rectWnd.IsEmpty()) {
Dan Sinclairf528eee2017-02-14 11:52:07 -0500138 CFX_PointF ptCenter = GetCenterPoint();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700139
Dan Sinclairf528eee2017-02-14 11:52:07 -0500140 CFX_PointF pt1(ptCenter.x - PWL_CBBUTTON_TRIANGLE_HALFLEN,
141 ptCenter.y + PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
142 CFX_PointF pt2(ptCenter.x + PWL_CBBUTTON_TRIANGLE_HALFLEN,
143 ptCenter.y + PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
144 CFX_PointF pt3(ptCenter.x,
145 ptCenter.y - PWL_CBBUTTON_TRIANGLE_HALFLEN * 0.5f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 if (IsFloatBigger(rectWnd.right - rectWnd.left,
148 PWL_CBBUTTON_TRIANGLE_HALFLEN * 2) &&
149 IsFloatBigger(rectWnd.top - rectWnd.bottom,
150 PWL_CBBUTTON_TRIANGLE_HALFLEN)) {
151 CFX_PathData path;
dan sinclairb147e072017-02-22 19:56:15 -0500152 path.AppendPoint(pt1, FXPT_TYPE::MoveTo, false);
153 path.AppendPoint(pt2, FXPT_TYPE::LineTo, false);
154 path.AppendPoint(pt3, FXPT_TYPE::LineTo, false);
155 path.AppendPoint(pt1, FXPT_TYPE::LineTo, false);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156
thestig1cd352e2016-06-07 17:53:06 -0700157 pDevice->DrawPath(&path, pUser2Device, nullptr,
Dan Sinclairfc54e052017-02-23 09:59:05 -0500158 PWL_DEFAULT_BLACKCOLOR.ToFXColor(GetTransparency()), 0,
159 FXFILL_ALTERNATE);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700160 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162}
163
Dan Sinclairf528eee2017-02-14 11:52:07 -0500164bool CPWL_CBButton::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 CPWL_Wnd::OnLButtonDown(point, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700166
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 SetCapture();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 if (CPWL_Wnd* pParent = GetParentWindow()) {
170 pParent->OnNotify(this, PNM_LBUTTONDOWN, 0,
171 PWL_MAKEDWORD(point.x, point.y));
172 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700173
tsepez4cf55152016-11-02 14:37:54 -0700174 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175}
176
Dan Sinclairf528eee2017-02-14 11:52:07 -0500177bool CPWL_CBButton::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 CPWL_Wnd::OnLButtonUp(point, nFlag);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700179
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 ReleaseCapture();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181
tsepez4cf55152016-11-02 14:37:54 -0700182 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183}
184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185CPWL_ComboBox::CPWL_ComboBox()
Tom Sepez45b9ae12017-05-18 08:34:03 -0700186 : m_pEdit(nullptr),
187 m_pButton(nullptr),
188 m_pList(nullptr),
189 m_bPopup(false),
Lei Zhang7bef7c82017-05-31 23:52:00 -0700190 m_bBottom(true),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 m_nSelectItem(-1),
thestig1cd352e2016-06-07 17:53:06 -0700192 m_pFillerNotify(nullptr) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193
Tom Sepezb084c1f2017-05-12 14:04:06 -0700194CPWL_ComboBox::~CPWL_ComboBox() {}
195
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196CFX_ByteString CPWL_ComboBox::GetClassName() const {
197 return "CPWL_ComboBox";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700198}
199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200void CPWL_ComboBox::OnCreate(PWL_CREATEPARAM& cp) {
201 cp.dwFlags &= ~PWS_HSCROLL;
202 cp.dwFlags &= ~PWS_VSCROLL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203}
204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205void CPWL_ComboBox::SetFocus() {
206 if (m_pEdit)
207 m_pEdit->SetFocus();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700208}
209
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210void CPWL_ComboBox::KillFocus() {
tsepez4cf55152016-11-02 14:37:54 -0700211 SetPopup(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 CPWL_Wnd::KillFocus();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213}
214
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215CFX_WideString CPWL_ComboBox::GetText() const {
216 if (m_pEdit) {
217 return m_pEdit->GetText();
218 }
219 return CFX_WideString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220}
221
tsepez067990c2016-09-13 06:46:40 -0700222void CPWL_ComboBox::SetText(const CFX_WideString& text) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 if (m_pEdit)
224 m_pEdit->SetText(text);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225}
226
tsepez067990c2016-09-13 06:46:40 -0700227void CPWL_ComboBox::AddString(const CFX_WideString& str) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 if (m_pList)
Dan Sinclair3ebd1212016-03-09 09:59:23 -0500229 m_pList->AddString(str);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230}
231
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232int32_t CPWL_ComboBox::GetSelect() const {
233 return m_nSelectItem;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700234}
235
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236void CPWL_ComboBox::SetSelect(int32_t nItemIndex) {
237 if (m_pList)
238 m_pList->Select(nItemIndex);
239
tsepez067990c2016-09-13 06:46:40 -0700240 m_pEdit->SetText(m_pList->GetText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 m_nSelectItem = nItemIndex;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700242}
243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244void CPWL_ComboBox::SetEditSel(int32_t nStartChar, int32_t nEndChar) {
tsepez067990c2016-09-13 06:46:40 -0700245 if (m_pEdit)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 m_pEdit->SetSel(nStartChar, nEndChar);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247}
248
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249void CPWL_ComboBox::GetEditSel(int32_t& nStartChar, int32_t& nEndChar) const {
250 nStartChar = -1;
251 nEndChar = -1;
252
tsepez067990c2016-09-13 06:46:40 -0700253 if (m_pEdit)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 m_pEdit->GetSel(nStartChar, nEndChar);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255}
256
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257void CPWL_ComboBox::Clear() {
tsepez067990c2016-09-13 06:46:40 -0700258 if (m_pEdit)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 m_pEdit->Clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260}
261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262void CPWL_ComboBox::CreateChildWnd(const PWL_CREATEPARAM& cp) {
263 CreateEdit(cp);
264 CreateButton(cp);
265 CreateListBox(cp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266}
267
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268void CPWL_ComboBox::CreateEdit(const PWL_CREATEPARAM& cp) {
Tom Sepezb084c1f2017-05-12 14:04:06 -0700269 if (m_pEdit)
270 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271
Tom Sepez45b9ae12017-05-18 08:34:03 -0700272 m_pEdit = new CPWL_CBEdit();
Tom Sepezcc205132017-05-16 14:01:47 -0700273 m_pEdit->AttachFFLData(m_pFormFiller.Get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274
Tom Sepezb084c1f2017-05-12 14:04:06 -0700275 PWL_CREATEPARAM ecp = cp;
276 ecp.pParentWnd = this;
277 ecp.dwFlags = PWS_VISIBLE | PWS_CHILD | PWS_BORDER | PES_CENTER |
278 PES_AUTOSCROLL | PES_UNDO;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279
Tom Sepezb084c1f2017-05-12 14:04:06 -0700280 if (HasFlag(PWS_AUTOFONTSIZE))
281 ecp.dwFlags |= PWS_AUTOFONTSIZE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282
Tom Sepezb084c1f2017-05-12 14:04:06 -0700283 if (!HasFlag(PCBS_ALLOWCUSTOMTEXT))
284 ecp.dwFlags |= PWS_READONLY;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285
Tom Sepezb084c1f2017-05-12 14:04:06 -0700286 ecp.rcRectWnd = CFX_FloatRect(0, 0, 0, 0);
287 ecp.dwBorderWidth = 0;
288 ecp.nBorderStyle = BorderStyle::SOLID;
289 m_pEdit->Create(ecp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290}
291
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292void CPWL_ComboBox::CreateButton(const PWL_CREATEPARAM& cp) {
Tom Sepezb084c1f2017-05-12 14:04:06 -0700293 if (m_pButton)
294 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700295
Tom Sepez45b9ae12017-05-18 08:34:03 -0700296 m_pButton = new CPWL_CBButton;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700297
Tom Sepezb084c1f2017-05-12 14:04:06 -0700298 PWL_CREATEPARAM bcp = cp;
299 bcp.pParentWnd = this;
300 bcp.dwFlags = PWS_VISIBLE | PWS_CHILD | PWS_BORDER | PWS_BACKGROUND;
301 bcp.sBackgroundColor = PWL_SCROLLBAR_BKCOLOR;
302 bcp.sBorderColor = PWL_DEFAULT_BLACKCOLOR;
303 bcp.dwBorderWidth = 2;
304 bcp.nBorderStyle = BorderStyle::BEVELED;
305 bcp.eCursorType = FXCT_ARROW;
306 m_pButton->Create(bcp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307}
308
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309void CPWL_ComboBox::CreateListBox(const PWL_CREATEPARAM& cp) {
Tom Sepezb084c1f2017-05-12 14:04:06 -0700310 if (m_pList)
311 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700312
Tom Sepez45b9ae12017-05-18 08:34:03 -0700313 m_pList = new CPWL_CBListBox();
Tom Sepezcc205132017-05-16 14:01:47 -0700314 m_pList->AttachFFLData(m_pFormFiller.Get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700315
Tom Sepezb084c1f2017-05-12 14:04:06 -0700316 PWL_CREATEPARAM lcp = cp;
317 lcp.pParentWnd = this;
318 lcp.dwFlags =
319 PWS_CHILD | PWS_BORDER | PWS_BACKGROUND | PLBS_HOVERSEL | PWS_VSCROLL;
320 lcp.nBorderStyle = BorderStyle::SOLID;
321 lcp.dwBorderWidth = 1;
322 lcp.eCursorType = FXCT_ARROW;
323 lcp.rcRectWnd = CFX_FloatRect(0, 0, 0, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324
Tom Sepezb084c1f2017-05-12 14:04:06 -0700325 if (cp.dwFlags & PWS_AUTOFONTSIZE)
326 lcp.fFontSize = PWLCB_DEFAULTFONTSIZE;
327 else
328 lcp.fFontSize = cp.fFontSize;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329
Tom Sepezb084c1f2017-05-12 14:04:06 -0700330 if (cp.sBorderColor.nColorType == COLORTYPE_TRANSPARENT)
331 lcp.sBorderColor = PWL_DEFAULT_BLACKCOLOR;
332
333 if (cp.sBackgroundColor.nColorType == COLORTYPE_TRANSPARENT)
334 lcp.sBackgroundColor = PWL_DEFAULT_WHITECOLOR;
335
336 m_pList->Create(lcp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337}
338
339void CPWL_ComboBox::RePosChildWnd() {
Lei Zhang7bef7c82017-05-31 23:52:00 -0700340 const CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 if (m_bPopup) {
Lei Zhang7bef7c82017-05-31 23:52:00 -0700342 const float fOldWindowHeight = m_rcOldWindow.Height();
343 const float fOldClientHeight = fOldWindowHeight - GetBorderWidth() * 2;
344
Tom Sepez281a9ea2016-02-26 14:24:28 -0800345 CFX_FloatRect rcList = CPWL_Wnd::GetWindowRect();
Lei Zhang7bef7c82017-05-31 23:52:00 -0700346 CFX_FloatRect rcButton = rcClient;
347 rcButton.left =
348 std::max(rcButton.right - PWL_COMBOBOX_BUTTON_WIDTH, rcClient.left);
349 CFX_FloatRect rcEdit = rcClient;
350 rcEdit.right = std::max(rcButton.left - 1.0f, rcEdit.left);
351 if (m_bBottom) {
352 rcButton.bottom = rcButton.top - fOldClientHeight;
353 rcEdit.bottom = rcEdit.top - fOldClientHeight;
354 rcList.top -= fOldWindowHeight;
355 } else {
356 rcButton.top = rcButton.bottom + fOldClientHeight;
357 rcEdit.top = rcEdit.bottom + fOldClientHeight;
358 rcList.bottom += fOldWindowHeight;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700359 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700360
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361 if (m_pButton)
tsepez4cf55152016-11-02 14:37:54 -0700362 m_pButton->Move(rcButton, true, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363
364 if (m_pEdit)
tsepez4cf55152016-11-02 14:37:54 -0700365 m_pEdit->Move(rcEdit, true, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366
367 if (m_pList) {
tsepez4cf55152016-11-02 14:37:54 -0700368 m_pList->SetVisible(true);
369 m_pList->Move(rcList, true, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 m_pList->ScrollToListItem(m_nSelectItem);
371 }
Lei Zhang7bef7c82017-05-31 23:52:00 -0700372 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 }
Lei Zhang7bef7c82017-05-31 23:52:00 -0700374
375 CFX_FloatRect rcButton = rcClient;
376 rcButton.left =
377 std::max(rcButton.right - PWL_COMBOBOX_BUTTON_WIDTH, rcClient.left);
378
379 if (m_pButton)
380 m_pButton->Move(rcButton, true, false);
381
382 CFX_FloatRect rcEdit = rcClient;
383 rcEdit.right = std::max(rcButton.left - 1.0f, rcEdit.left);
384
385 if (m_pEdit)
386 m_pEdit->Move(rcEdit, true, false);
387
388 if (m_pList)
389 m_pList->SetVisible(false);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700390}
391
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392void CPWL_ComboBox::SelectAll() {
393 if (m_pEdit && HasFlag(PCBS_ALLOWCUSTOMTEXT))
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700394 m_pEdit->SelectAll();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700395}
396
Tom Sepez281a9ea2016-02-26 14:24:28 -0800397CFX_FloatRect CPWL_ComboBox::GetFocusRect() const {
398 return CFX_FloatRect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700399}
400
tsepez4cf55152016-11-02 14:37:54 -0700401void CPWL_ComboBox::SetPopup(bool bPopup) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 if (!m_pList)
403 return;
404 if (bPopup == m_bPopup)
405 return;
Dan Sinclair05df0752017-03-14 14:43:42 -0400406 float fListHeight = m_pList->GetContentRect().Height();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407 if (!IsFloatBigger(fListHeight, 0.0f))
408 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700409
Lei Zhang7bef7c82017-05-31 23:52:00 -0700410 if (!bPopup) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 m_bPopup = bPopup;
tsepez4cf55152016-11-02 14:37:54 -0700412 Move(m_rcOldWindow, true, true);
Lei Zhang7bef7c82017-05-31 23:52:00 -0700413 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414 }
Lei Zhang7bef7c82017-05-31 23:52:00 -0700415
416 if (!m_pFillerNotify)
417 return;
418
419#ifdef PDF_ENABLE_XFA
420 bool bExit = false;
421 m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), bExit, 0);
422 if (bExit)
423 return;
424#endif // PDF_ENABLE_XFA
425
426 float fBorderWidth = m_pList->GetBorderWidth() * 2;
427 float fPopupMin = 0.0f;
428 if (m_pList->GetCount() > 3)
429 fPopupMin = m_pList->GetFirstHeight() * 3 + fBorderWidth;
430 float fPopupMax = fListHeight + fBorderWidth;
431
432 bool bBottom;
433 float fPopupRet;
434 m_pFillerNotify->QueryWherePopup(GetAttachedData(), fPopupMin, fPopupMax,
435 &bBottom, &fPopupRet);
436 if (!IsFloatBigger(fPopupRet, 0.0f))
437 return;
438
439 m_rcOldWindow = CPWL_Wnd::GetWindowRect();
440 m_bPopup = bPopup;
441 m_bBottom = bBottom;
442
443 CFX_FloatRect rcWindow = m_rcOldWindow;
444 if (bBottom)
445 rcWindow.bottom -= fPopupRet;
446 else
447 rcWindow.top += fPopupRet;
448
449 Move(rcWindow, true, true);
450#ifdef PDF_ENABLE_XFA
451 bExit = false;
452 m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), bExit, 0);
453#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454}
455
tsepez4cf55152016-11-02 14:37:54 -0700456bool CPWL_ComboBox::OnKeyDown(uint16_t nChar, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 if (!m_pList)
tsepez4cf55152016-11-02 14:37:54 -0700458 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 if (!m_pEdit)
tsepez4cf55152016-11-02 14:37:54 -0700460 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461
462 m_nSelectItem = -1;
463
464 switch (nChar) {
465 case FWL_VKEY_Up:
466 if (m_pList->GetCurSel() > 0) {
tsepez4cf55152016-11-02 14:37:54 -0700467 bool bExit = false;
Tom Sepez51da0932015-11-25 16:05:49 -0800468#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469 if (m_pFillerNotify) {
470 m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), bExit, nFlag);
471 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700472 return false;
473 bExit = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474 m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), bExit, nFlag);
475 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700476 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700477 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800478#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479 if (m_pList->OnKeyDownWithExit(nChar, bExit, nFlag)) {
480 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700481 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482 SetSelectText();
483 }
484 }
tsepez4cf55152016-11-02 14:37:54 -0700485 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486 case FWL_VKEY_Down:
487 if (m_pList->GetCurSel() < m_pList->GetCount() - 1) {
tsepez4cf55152016-11-02 14:37:54 -0700488 bool bExit = false;
Tom Sepez51da0932015-11-25 16:05:49 -0800489#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 if (m_pFillerNotify) {
491 m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), bExit, nFlag);
492 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700493 return false;
494 bExit = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), bExit, nFlag);
496 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700497 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800499#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500 if (m_pList->OnKeyDownWithExit(nChar, bExit, nFlag)) {
501 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700502 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700503 SetSelectText();
504 }
505 }
tsepez4cf55152016-11-02 14:37:54 -0700506 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700507 }
508
509 if (HasFlag(PCBS_ALLOWCUSTOMTEXT))
510 return m_pEdit->OnKeyDown(nChar, nFlag);
511
tsepez4cf55152016-11-02 14:37:54 -0700512 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700513}
514
tsepez4cf55152016-11-02 14:37:54 -0700515bool CPWL_ComboBox::OnChar(uint16_t nChar, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516 if (!m_pList)
tsepez4cf55152016-11-02 14:37:54 -0700517 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518
519 if (!m_pEdit)
tsepez4cf55152016-11-02 14:37:54 -0700520 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521
522 m_nSelectItem = -1;
523 if (HasFlag(PCBS_ALLOWCUSTOMTEXT))
524 return m_pEdit->OnChar(nChar, nFlag);
525
tsepez4cf55152016-11-02 14:37:54 -0700526 bool bExit = false;
Tom Sepez51da0932015-11-25 16:05:49 -0800527#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528 if (m_pFillerNotify) {
529 m_pFillerNotify->OnPopupPreOpen(GetAttachedData(), bExit, nFlag);
530 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700531 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700532
533 m_pFillerNotify->OnPopupPostOpen(GetAttachedData(), bExit, nFlag);
534 if (bExit)
tsepez4cf55152016-11-02 14:37:54 -0700535 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800537#endif // PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700538 return m_pList->OnCharWithExit(nChar, bExit, nFlag) ? bExit : false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700539}
540
541void CPWL_ComboBox::OnNotify(CPWL_Wnd* pWnd,
tsepezc3255f52016-03-25 14:52:27 -0700542 uint32_t msg,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543 intptr_t wParam,
544 intptr_t lParam) {
545 switch (msg) {
546 case PNM_LBUTTONDOWN:
Tom Sepez45b9ae12017-05-18 08:34:03 -0700547 if (pWnd == m_pButton) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700548 SetPopup(!m_bPopup);
549 return;
550 }
551 break;
552 case PNM_LBUTTONUP:
553 if (m_pEdit && m_pList) {
Tom Sepez45b9ae12017-05-18 08:34:03 -0700554 if (pWnd == m_pList) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700555 SetSelectText();
556 SelectAll();
557 m_pEdit->SetFocus();
tsepez4cf55152016-11-02 14:37:54 -0700558 SetPopup(false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559 return;
560 }
561 }
Tom Sepezb084c1f2017-05-12 14:04:06 -0700562 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 }
564
565 CPWL_Wnd::OnNotify(pWnd, msg, wParam, lParam);
566}
567
tsepez4cf55152016-11-02 14:37:54 -0700568bool CPWL_ComboBox::IsPopup() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 return m_bPopup;
570}
571
572void CPWL_ComboBox::SetSelectText() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573 m_pEdit->SelectAll();
tsepez067990c2016-09-13 06:46:40 -0700574 m_pEdit->ReplaceSel(m_pList->GetText());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575 m_pEdit->SelectAll();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576 m_nSelectItem = m_pList->GetCurSel();
577}
578
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579void CPWL_ComboBox::SetFillerNotify(IPWL_Filler_Notify* pNotify) {
580 m_pFillerNotify = pNotify;
581
582 if (m_pEdit)
583 m_pEdit->SetFillerNotify(pNotify);
584
585 if (m_pList)
586 m_pList->SetFillerNotify(pNotify);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700587}