1 // 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.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "fpdfsdk/pwl/cpwl_list_box.h"
8
9 #include <sstream>
10 #include <utility>
11
12 #include "core/fxge/cfx_renderdevice.h"
13 #include "fpdfsdk/pwl/cpwl_edit.h"
14 #include "fpdfsdk/pwl/cpwl_edit_ctrl.h"
15 #include "fpdfsdk/pwl/cpwl_edit_impl.h"
16 #include "fpdfsdk/pwl/cpwl_list_impl.h"
17 #include "fpdfsdk/pwl/cpwl_scroll_bar.h"
18 #include "fpdfsdk/pwl/cpwl_wnd.h"
19 #include "public/fpdf_fwlevent.h"
20 #include "third_party/base/ptr_util.h"
21
CPWL_List_Notify(CPWL_ListBox * pList)22 CPWL_List_Notify::CPWL_List_Notify(CPWL_ListBox* pList) : m_pList(pList) {
23 ASSERT(m_pList);
24 }
25
~CPWL_List_Notify()26 CPWL_List_Notify::~CPWL_List_Notify() {}
27
IOnSetScrollInfoY(float fPlateMin,float fPlateMax,float fContentMin,float fContentMax,float fSmallStep,float fBigStep)28 void CPWL_List_Notify::IOnSetScrollInfoY(float fPlateMin,
29 float fPlateMax,
30 float fContentMin,
31 float fContentMax,
32 float fSmallStep,
33 float fBigStep) {
34 PWL_SCROLL_INFO Info;
35 Info.fPlateWidth = fPlateMax - fPlateMin;
36 Info.fContentMin = fContentMin;
37 Info.fContentMax = fContentMax;
38 Info.fSmallStep = fSmallStep;
39 Info.fBigStep = fBigStep;
40 m_pList->SetScrollInfo(Info);
41
42 CPWL_ScrollBar* pScroll = m_pList->GetVScrollBar();
43 if (!pScroll)
44 return;
45
46 if (IsFloatBigger(Info.fPlateWidth, Info.fContentMax - Info.fContentMin) ||
47 IsFloatEqual(Info.fPlateWidth, Info.fContentMax - Info.fContentMin)) {
48 if (pScroll->IsVisible()) {
49 pScroll->SetVisible(false);
50 m_pList->RePosChildWnd();
51 }
52 } else {
53 if (!pScroll->IsVisible()) {
54 pScroll->SetVisible(true);
55 m_pList->RePosChildWnd();
56 }
57 }
58 }
59
IOnSetScrollPosY(float fy)60 void CPWL_List_Notify::IOnSetScrollPosY(float fy) {
61 m_pList->SetScrollPosition(fy);
62 }
63
IOnInvalidateRect(CFX_FloatRect * pRect)64 void CPWL_List_Notify::IOnInvalidateRect(CFX_FloatRect* pRect) {
65 m_pList->InvalidateRect(pRect);
66 }
67
CPWL_ListBox(const CreateParams & cp,std::unique_ptr<IPWL_SystemHandler::PerWindowData> pAttachedData)68 CPWL_ListBox::CPWL_ListBox(
69 const CreateParams& cp,
70 std::unique_ptr<IPWL_SystemHandler::PerWindowData> pAttachedData)
71 : CPWL_Wnd(cp, std::move(pAttachedData)),
72 m_pList(pdfium::MakeUnique<CPWL_ListCtrl>()) {}
73
74 CPWL_ListBox::~CPWL_ListBox() = default;
75
OnCreated()76 void CPWL_ListBox::OnCreated() {
77 m_pList->SetFontMap(GetFontMap());
78 m_pListNotify = pdfium::MakeUnique<CPWL_List_Notify>(this);
79 m_pList->SetNotify(m_pListNotify.get());
80
81 SetHoverSel(HasFlag(PLBS_HOVERSEL));
82 m_pList->SetMultipleSel(HasFlag(PLBS_MULTIPLESEL));
83 m_pList->SetFontSize(GetCreationParams()->fFontSize);
84
85 m_bHoverSel = HasFlag(PLBS_HOVERSEL);
86 }
87
OnDestroy()88 void CPWL_ListBox::OnDestroy() {
89 // Make sure the notifier is removed from the list as we are about to
90 // destroy the notifier and don't want to leave a dangling pointer.
91 m_pList->SetNotify(nullptr);
92 m_pListNotify.reset();
93 }
94
DrawThisAppearance(CFX_RenderDevice * pDevice,const CFX_Matrix & mtUser2Device)95 void CPWL_ListBox::DrawThisAppearance(CFX_RenderDevice* pDevice,
96 const CFX_Matrix& mtUser2Device) {
97 CPWL_Wnd::DrawThisAppearance(pDevice, mtUser2Device);
98
99 CFX_FloatRect rcPlate = m_pList->GetPlateRect();
100 CFX_FloatRect rcList = GetListRect();
101 CFX_FloatRect rcClient = GetClientRect();
102
103 for (int32_t i = 0, sz = m_pList->GetCount(); i < sz; i++) {
104 CFX_FloatRect rcItem = m_pList->GetItemRect(i);
105 if (rcItem.bottom > rcPlate.top || rcItem.top < rcPlate.bottom)
106 continue;
107
108 CFX_PointF ptOffset(rcItem.left, (rcItem.top + rcItem.bottom) * 0.5f);
109 if (CPWL_EditImpl* pEdit = m_pList->GetItemEdit(i)) {
110 CFX_FloatRect rcContent = pEdit->GetContentRect();
111 rcItem.Intersect(rcContent.Width() > rcClient.Width() ? rcList
112 : rcClient);
113 }
114
115 IPWL_SystemHandler* pSysHandler = GetSystemHandler();
116 if (m_pList->IsItemSelected(i)) {
117 if (pSysHandler->IsSelectionImplemented()) {
118 CPWL_EditImpl::DrawEdit(pDevice, mtUser2Device, m_pList->GetItemEdit(i),
119 GetTextColor().ToFXColor(255), rcList, ptOffset,
120 nullptr, pSysHandler, m_pFormFiller.Get());
121 pSysHandler->OutputSelectedRect(m_pFormFiller.Get(), rcItem);
122 } else {
123 pDevice->DrawFillRect(&mtUser2Device, rcItem,
124 ArgbEncode(255, 0, 51, 113));
125 CPWL_EditImpl::DrawEdit(pDevice, mtUser2Device, m_pList->GetItemEdit(i),
126 ArgbEncode(255, 255, 255, 255), rcList,
127 ptOffset, nullptr, pSysHandler,
128 m_pFormFiller.Get());
129 }
130 } else {
131 CPWL_EditImpl::DrawEdit(pDevice, mtUser2Device, m_pList->GetItemEdit(i),
132 GetTextColor().ToFXColor(255), rcList, ptOffset,
133 nullptr, pSysHandler, nullptr);
134 }
135 }
136 }
137
OnKeyDown(uint16_t nChar,uint32_t nFlag)138 bool CPWL_ListBox::OnKeyDown(uint16_t nChar, uint32_t nFlag) {
139 CPWL_Wnd::OnKeyDown(nChar, nFlag);
140
141 switch (nChar) {
142 default:
143 return false;
144 case FWL_VKEY_Up:
145 case FWL_VKEY_Down:
146 case FWL_VKEY_Home:
147 case FWL_VKEY_Left:
148 case FWL_VKEY_End:
149 case FWL_VKEY_Right:
150 break;
151 }
152
153 switch (nChar) {
154 case FWL_VKEY_Up:
155 m_pList->OnVK_UP(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
156 break;
157 case FWL_VKEY_Down:
158 m_pList->OnVK_DOWN(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
159 break;
160 case FWL_VKEY_Home:
161 m_pList->OnVK_HOME(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
162 break;
163 case FWL_VKEY_Left:
164 m_pList->OnVK_LEFT(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
165 break;
166 case FWL_VKEY_End:
167 m_pList->OnVK_END(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
168 break;
169 case FWL_VKEY_Right:
170 m_pList->OnVK_RIGHT(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
171 break;
172 case FWL_VKEY_Delete:
173 break;
174 }
175 OnNotifySelectionChanged(true, nFlag);
176 return true;
177 }
178
OnChar(uint16_t nChar,uint32_t nFlag)179 bool CPWL_ListBox::OnChar(uint16_t nChar, uint32_t nFlag) {
180 CPWL_Wnd::OnChar(nChar, nFlag);
181
182 if (!m_pList->OnChar(nChar, IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag)))
183 return false;
184
185 OnNotifySelectionChanged(true, nFlag);
186 return true;
187 }
188
OnLButtonDown(const CFX_PointF & point,uint32_t nFlag)189 bool CPWL_ListBox::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
190 CPWL_Wnd::OnLButtonDown(point, nFlag);
191
192 if (ClientHitTest(point)) {
193 m_bMouseDown = true;
194 SetFocus();
195 SetCapture();
196
197 m_pList->OnMouseDown(point, IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
198 }
199
200 return true;
201 }
202
OnLButtonUp(const CFX_PointF & point,uint32_t nFlag)203 bool CPWL_ListBox::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
204 CPWL_Wnd::OnLButtonUp(point, nFlag);
205
206 if (m_bMouseDown) {
207 ReleaseCapture();
208 m_bMouseDown = false;
209 }
210 OnNotifySelectionChanged(false, nFlag);
211 return true;
212 }
213
SetHoverSel(bool bHoverSel)214 void CPWL_ListBox::SetHoverSel(bool bHoverSel) {
215 m_bHoverSel = bHoverSel;
216 }
217
OnMouseMove(const CFX_PointF & point,uint32_t nFlag)218 bool CPWL_ListBox::OnMouseMove(const CFX_PointF& point, uint32_t nFlag) {
219 CPWL_Wnd::OnMouseMove(point, nFlag);
220
221 if (m_bHoverSel && !IsCaptureMouse() && ClientHitTest(point))
222 m_pList->Select(m_pList->GetItemIndex(point));
223 if (m_bMouseDown)
224 m_pList->OnMouseMove(point, IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
225
226 return true;
227 }
228
SetScrollInfo(const PWL_SCROLL_INFO & info)229 void CPWL_ListBox::SetScrollInfo(const PWL_SCROLL_INFO& info) {
230 if (CPWL_Wnd* pChild = GetVScrollBar())
231 pChild->SetScrollInfo(info);
232 }
233
SetScrollPosition(float pos)234 void CPWL_ListBox::SetScrollPosition(float pos) {
235 if (CPWL_Wnd* pChild = GetVScrollBar())
236 pChild->SetScrollPosition(pos);
237 }
238
ScrollWindowVertically(float pos)239 void CPWL_ListBox::ScrollWindowVertically(float pos) {
240 m_pList->SetScrollPos(CFX_PointF(0, pos));
241 }
242
RePosChildWnd()243 bool CPWL_ListBox::RePosChildWnd() {
244 if (!CPWL_Wnd::RePosChildWnd())
245 return false;
246
247 m_pList->SetPlateRect(GetListRect());
248 return true;
249 }
250
OnNotifySelectionChanged(bool bKeyDown,uint32_t nFlag)251 bool CPWL_ListBox::OnNotifySelectionChanged(bool bKeyDown, uint32_t nFlag) {
252 if (!m_pFillerNotify)
253 return false;
254
255 ObservedPtr<CPWL_Wnd> thisObserved(this);
256
257 WideString swChange = GetText();
258 WideString strChangeEx;
259 int nSelStart = 0;
260 int nSelEnd = swChange.GetLength();
261 bool bRC;
262 bool bExit;
263 std::tie(bRC, bExit) = m_pFillerNotify->OnBeforeKeyStroke(
264 GetAttachedData(), swChange, strChangeEx, nSelStart, nSelEnd, bKeyDown,
265 nFlag);
266
267 if (!thisObserved)
268 return false;
269
270 return bExit;
271 }
272
GetFocusRect() const273 CFX_FloatRect CPWL_ListBox::GetFocusRect() const {
274 if (m_pList->IsMultipleSel()) {
275 CFX_FloatRect rcCaret = m_pList->GetItemRect(m_pList->GetCaret());
276 rcCaret.Intersect(GetClientRect());
277 return rcCaret;
278 }
279
280 return CPWL_Wnd::GetFocusRect();
281 }
282
AddString(const WideString & str)283 void CPWL_ListBox::AddString(const WideString& str) {
284 m_pList->AddString(str);
285 }
286
GetText()287 WideString CPWL_ListBox::GetText() {
288 return m_pList->GetText();
289 }
290
SetFontSize(float fFontSize)291 void CPWL_ListBox::SetFontSize(float fFontSize) {
292 m_pList->SetFontSize(fFontSize);
293 }
294
GetFontSize() const295 float CPWL_ListBox::GetFontSize() const {
296 return m_pList->GetFontSize();
297 }
298
Select(int32_t nItemIndex)299 void CPWL_ListBox::Select(int32_t nItemIndex) {
300 m_pList->Select(nItemIndex);
301 }
302
Deselect(int32_t nItemIndex)303 void CPWL_ListBox::Deselect(int32_t nItemIndex) {
304 m_pList->Deselect(nItemIndex);
305 }
306
SetCaret(int32_t nItemIndex)307 void CPWL_ListBox::SetCaret(int32_t nItemIndex) {
308 m_pList->SetCaret(nItemIndex);
309 }
310
SetTopVisibleIndex(int32_t nItemIndex)311 void CPWL_ListBox::SetTopVisibleIndex(int32_t nItemIndex) {
312 m_pList->SetTopItem(nItemIndex);
313 }
314
ScrollToListItem(int32_t nItemIndex)315 void CPWL_ListBox::ScrollToListItem(int32_t nItemIndex) {
316 m_pList->ScrollToListItem(nItemIndex);
317 }
318
ResetContent()319 void CPWL_ListBox::ResetContent() {
320 m_pList->Clear();
321 }
322
Reset()323 void CPWL_ListBox::Reset() {
324 m_pList->Cancel();
325 }
326
IsMultipleSel() const327 bool CPWL_ListBox::IsMultipleSel() const {
328 return m_pList->IsMultipleSel();
329 }
330
GetCaretIndex() const331 int32_t CPWL_ListBox::GetCaretIndex() const {
332 return m_pList->GetCaret();
333 }
334
GetCurSel() const335 int32_t CPWL_ListBox::GetCurSel() const {
336 return m_pList->GetSelect();
337 }
338
IsItemSelected(int32_t nItemIndex) const339 bool CPWL_ListBox::IsItemSelected(int32_t nItemIndex) const {
340 return m_pList->IsItemSelected(nItemIndex);
341 }
342
GetTopVisibleIndex() const343 int32_t CPWL_ListBox::GetTopVisibleIndex() const {
344 m_pList->ScrollToListItem(m_pList->GetFirstSelected());
345 return m_pList->GetTopItem();
346 }
347
GetCount() const348 int32_t CPWL_ListBox::GetCount() const {
349 return m_pList->GetCount();
350 }
351
FindNext(int32_t nIndex,wchar_t nChar) const352 int32_t CPWL_ListBox::FindNext(int32_t nIndex, wchar_t nChar) const {
353 return m_pList->FindNext(nIndex, nChar);
354 }
355
GetContentRect() const356 CFX_FloatRect CPWL_ListBox::GetContentRect() const {
357 return m_pList->GetContentRect();
358 }
359
GetFirstHeight() const360 float CPWL_ListBox::GetFirstHeight() const {
361 return m_pList->GetFirstHeight();
362 }
363
GetListRect() const364 CFX_FloatRect CPWL_ListBox::GetListRect() const {
365 float width = static_cast<float>(GetBorderWidth() + GetInnerBorderWidth());
366 return GetWindowRect().GetDeflated(width, width);
367 }
368
OnMouseWheel(short zDelta,const CFX_PointF & point,uint32_t nFlag)369 bool CPWL_ListBox::OnMouseWheel(short zDelta,
370 const CFX_PointF& point,
371 uint32_t nFlag) {
372 if (zDelta < 0)
373 m_pList->OnVK_DOWN(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
374 else
375 m_pList->OnVK_UP(IsSHIFTpressed(nFlag), IsCTRLpressed(nFlag));
376
377 OnNotifySelectionChanged(false, nFlag);
378 return true;
379 }
380