1 // Copyright 2014 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "fpdfsdk/pwl/cpwl_wnd.h"
8
9 #include <sstream>
10 #include <utility>
11 #include <vector>
12
13 #include "build/build_config.h"
14 #include "core/fxcrt/stl_util.h"
15 #include "core/fxge/cfx_renderdevice.h"
16 #include "fpdfsdk/pwl/cpwl_scroll_bar.h"
17 #include "public/fpdf_fwlevent.h"
18 #include "third_party/base/check.h"
19 #include "third_party/base/check_op.h"
20 #include "third_party/base/containers/contains.h"
21
22 namespace {
23
24 constexpr float kDefaultFontSize = 9.0f;
25
26 } // namespace
27
28 // static
29 const CFX_Color CPWL_Wnd::kDefaultBlackColor =
30 CFX_Color(CFX_Color::Type::kGray, 0);
31
32 // static
33 const CFX_Color CPWL_Wnd::kDefaultWhiteColor =
34 CFX_Color(CFX_Color::Type::kGray, 1);
35
CreateParams(CFX_Timer::HandlerIface * timer_handler,IPWL_FillerNotify * filler_notify,ProviderIface * provider)36 CPWL_Wnd::CreateParams::CreateParams(CFX_Timer::HandlerIface* timer_handler,
37 IPWL_FillerNotify* filler_notify,
38 ProviderIface* provider)
39 : pTimerHandler(timer_handler),
40 pFillerNotify(filler_notify),
41 pProvider(provider),
42 fFontSize(kDefaultFontSize),
43 sDash(3, 0, 0) {}
44
45 CPWL_Wnd::CreateParams::CreateParams(const CreateParams& other) = default;
46
47 CPWL_Wnd::CreateParams::~CreateParams() = default;
48
49 class CPWL_MsgControl final : public Observable {
50 public:
CPWL_MsgControl(const CPWL_Wnd * pWnd)51 explicit CPWL_MsgControl(const CPWL_Wnd* pWnd) : m_pCreatedWnd(pWnd) {}
52 ~CPWL_MsgControl() = default;
53
IsWndCreated(const CPWL_Wnd * pWnd) const54 bool IsWndCreated(const CPWL_Wnd* pWnd) const {
55 return m_pCreatedWnd == pWnd;
56 }
57
IsWndCaptureMouse(const CPWL_Wnd * pWnd) const58 bool IsWndCaptureMouse(const CPWL_Wnd* pWnd) const {
59 return pWnd && pdfium::Contains(m_MousePaths, pWnd);
60 }
61
IsMainCaptureKeyboard(const CPWL_Wnd * pWnd) const62 bool IsMainCaptureKeyboard(const CPWL_Wnd* pWnd) const {
63 return pWnd == m_pMainKeyboardWnd;
64 }
65
IsWndCaptureKeyboard(const CPWL_Wnd * pWnd) const66 bool IsWndCaptureKeyboard(const CPWL_Wnd* pWnd) const {
67 return pWnd && pdfium::Contains(m_KeyboardPaths, pWnd);
68 }
69
SetFocus(CPWL_Wnd * pWnd)70 void SetFocus(CPWL_Wnd* pWnd) {
71 m_KeyboardPaths = pWnd->GetAncestors();
72 m_pMainKeyboardWnd = pWnd;
73
74 // Note, pWnd may get destroyed in the OnSetFocus call.
75 pWnd->OnSetFocus();
76 }
77
KillFocus()78 void KillFocus() {
79 ObservedPtr<CPWL_MsgControl> observed_ptr(this);
80 if (!m_KeyboardPaths.empty()) {
81 CPWL_Wnd* pWnd = m_KeyboardPaths.front();
82 if (pWnd)
83 pWnd->OnKillFocus();
84 }
85 if (!observed_ptr)
86 return;
87
88 m_pMainKeyboardWnd = nullptr;
89 m_KeyboardPaths.clear();
90 }
91
SetCapture(CPWL_Wnd * pWnd)92 void SetCapture(CPWL_Wnd* pWnd) { m_MousePaths = pWnd->GetAncestors(); }
93
ReleaseCapture()94 void ReleaseCapture() { m_MousePaths.clear(); }
95
96 private:
97 std::vector<UnownedPtr<CPWL_Wnd>> m_MousePaths;
98 std::vector<UnownedPtr<CPWL_Wnd>> m_KeyboardPaths;
99 UnownedPtr<const CPWL_Wnd> m_pCreatedWnd;
100 UnownedPtr<const CPWL_Wnd> m_pMainKeyboardWnd;
101 };
102
103 // static
IsSHIFTKeyDown(Mask<FWL_EVENTFLAG> nFlag)104 bool CPWL_Wnd::IsSHIFTKeyDown(Mask<FWL_EVENTFLAG> nFlag) {
105 return !!(nFlag & FWL_EVENTFLAG_ShiftKey);
106 }
107
108 // static
IsCTRLKeyDown(Mask<FWL_EVENTFLAG> nFlag)109 bool CPWL_Wnd::IsCTRLKeyDown(Mask<FWL_EVENTFLAG> nFlag) {
110 return !!(nFlag & FWL_EVENTFLAG_ControlKey);
111 }
112
113 // static
IsALTKeyDown(Mask<FWL_EVENTFLAG> nFlag)114 bool CPWL_Wnd::IsALTKeyDown(Mask<FWL_EVENTFLAG> nFlag) {
115 return !!(nFlag & FWL_EVENTFLAG_AltKey);
116 }
117
118 // static
IsMETAKeyDown(Mask<FWL_EVENTFLAG> nFlag)119 bool CPWL_Wnd::IsMETAKeyDown(Mask<FWL_EVENTFLAG> nFlag) {
120 return !!(nFlag & FWL_EVENTFLAG_MetaKey);
121 }
122
123 // static
IsPlatformShortcutKey(Mask<FWL_EVENTFLAG> nFlag)124 bool CPWL_Wnd::IsPlatformShortcutKey(Mask<FWL_EVENTFLAG> nFlag) {
125 #if BUILDFLAG(IS_APPLE)
126 return IsMETAKeyDown(nFlag);
127 #else
128 return IsCTRLKeyDown(nFlag);
129 #endif
130 }
131
CPWL_Wnd(const CreateParams & cp,std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData)132 CPWL_Wnd::CPWL_Wnd(
133 const CreateParams& cp,
134 std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData)
135 : m_CreationParams(cp), m_pAttachedData(std::move(pAttachedData)) {}
136
~CPWL_Wnd()137 CPWL_Wnd::~CPWL_Wnd() {
138 DCHECK(!m_bCreated);
139 }
140
Realize()141 void CPWL_Wnd::Realize() {
142 DCHECK(!m_bCreated);
143
144 m_CreationParams.rcRectWnd.Normalize();
145 m_rcWindow = m_CreationParams.rcRectWnd;
146 m_rcClip = m_rcWindow;
147 if (!m_rcClip.IsEmpty()) {
148 m_rcClip.Inflate(1.0f, 1.0f);
149 m_rcClip.Normalize();
150 }
151 CreateMsgControl();
152
153 CreateParams ccp = m_CreationParams;
154 ccp.dwFlags &= 0xFFFF0000L; // remove sub styles
155 CreateScrollBar(ccp);
156 CreateChildWnd(ccp);
157 m_bVisible = HasFlag(PWS_VISIBLE);
158 OnCreated();
159 if (!RePosChildWnd())
160 return;
161
162 m_bCreated = true;
163 }
164
OnCreated()165 void CPWL_Wnd::OnCreated() {}
166
OnDestroy()167 void CPWL_Wnd::OnDestroy() {}
168
InvalidateProvider(ProviderIface * provider)169 void CPWL_Wnd::InvalidateProvider(ProviderIface* provider) {
170 if (m_CreationParams.pProvider.Get() == provider)
171 m_CreationParams.pProvider.Reset();
172 }
173
Destroy()174 void CPWL_Wnd::Destroy() {
175 KillFocus();
176 OnDestroy();
177 if (m_bCreated) {
178 m_pVScrollBar = nullptr;
179 while (!m_Children.empty()) {
180 std::unique_ptr<CPWL_Wnd> pChild = std::move(m_Children.back());
181 m_Children.pop_back();
182 pChild->Destroy();
183 }
184 if (m_pParent)
185 m_pParent->RemoveChild(this);
186 m_bCreated = false;
187 }
188 DestroyMsgControl();
189 }
190
Move(const CFX_FloatRect & rcNew,bool bReset,bool bRefresh)191 bool CPWL_Wnd::Move(const CFX_FloatRect& rcNew, bool bReset, bool bRefresh) {
192 if (!IsValid())
193 return true;
194
195 CFX_FloatRect rcOld = GetWindowRect();
196 m_rcWindow = rcNew;
197 m_rcWindow.Normalize();
198
199 if (bReset) {
200 if (rcOld.left != rcNew.left || rcOld.right != rcNew.right ||
201 rcOld.top != rcNew.top || rcOld.bottom != rcNew.bottom) {
202 if (!RePosChildWnd())
203 return false;
204 }
205 }
206 if (bRefresh && !InvalidateRectMove(rcOld, rcNew))
207 return false;
208
209 m_CreationParams.rcRectWnd = m_rcWindow;
210 return true;
211 }
212
InvalidateRectMove(const CFX_FloatRect & rcOld,const CFX_FloatRect & rcNew)213 bool CPWL_Wnd::InvalidateRectMove(const CFX_FloatRect& rcOld,
214 const CFX_FloatRect& rcNew) {
215 CFX_FloatRect rcUnion = rcOld;
216 rcUnion.Union(rcNew);
217
218 return InvalidateRect(&rcUnion);
219 }
220
DrawAppearance(CFX_RenderDevice * pDevice,const CFX_Matrix & mtUser2Device)221 void CPWL_Wnd::DrawAppearance(CFX_RenderDevice* pDevice,
222 const CFX_Matrix& mtUser2Device) {
223 if (IsValid() && IsVisible()) {
224 DrawThisAppearance(pDevice, mtUser2Device);
225 DrawChildAppearance(pDevice, mtUser2Device);
226 }
227 }
228
DrawThisAppearance(CFX_RenderDevice * pDevice,const CFX_Matrix & mtUser2Device)229 void CPWL_Wnd::DrawThisAppearance(CFX_RenderDevice* pDevice,
230 const CFX_Matrix& mtUser2Device) {
231 CFX_FloatRect rectWnd = GetWindowRect();
232 if (rectWnd.IsEmpty())
233 return;
234
235 if (HasFlag(PWS_BACKGROUND)) {
236 float width = static_cast<float>(GetBorderWidth() + GetInnerBorderWidth());
237 pDevice->DrawFillRect(&mtUser2Device, rectWnd.GetDeflated(width, width),
238 GetBackgroundColor(), GetTransparency());
239 }
240
241 if (HasFlag(PWS_BORDER)) {
242 pDevice->DrawBorder(&mtUser2Device, rectWnd,
243 static_cast<float>(GetBorderWidth()), GetBorderColor(),
244 GetBorderLeftTopColor(GetBorderStyle()),
245 GetBorderRightBottomColor(GetBorderStyle()),
246 GetBorderStyle(), GetTransparency());
247 }
248 }
249
DrawChildAppearance(CFX_RenderDevice * pDevice,const CFX_Matrix & mtUser2Device)250 void CPWL_Wnd::DrawChildAppearance(CFX_RenderDevice* pDevice,
251 const CFX_Matrix& mtUser2Device) {
252 for (const auto& pChild : m_Children) {
253 pChild->DrawAppearance(pDevice, mtUser2Device);
254 }
255 }
256
InvalidateRect(const CFX_FloatRect * pRect)257 bool CPWL_Wnd::InvalidateRect(const CFX_FloatRect* pRect) {
258 if (!IsValid())
259 return true;
260
261 ObservedPtr<CPWL_Wnd> thisObserved(this);
262 CFX_FloatRect rcRefresh = pRect ? *pRect : GetWindowRect();
263 if (!HasFlag(PWS_NOREFRESHCLIP)) {
264 CFX_FloatRect rcClip = GetClipRect();
265 if (!rcClip.IsEmpty())
266 rcRefresh.Intersect(rcClip);
267 }
268
269 CFX_FloatRect rcWin = PWLtoWnd(rcRefresh);
270 rcWin.Inflate(1, 1);
271 rcWin.Normalize();
272 GetFillerNotify()->InvalidateRect(m_pAttachedData.get(), rcWin);
273 return !!thisObserved;
274 }
275
OnKeyDown(FWL_VKEYCODE nKeyCode,Mask<FWL_EVENTFLAG> nFlag)276 bool CPWL_Wnd::OnKeyDown(FWL_VKEYCODE nKeyCode, Mask<FWL_EVENTFLAG> nFlag) {
277 if (!IsValid() || !IsVisible())
278 return false;
279 if (!IsWndCaptureKeyboard(this))
280 return false;
281 for (const auto& pChild : m_Children) {
282 if (IsWndCaptureKeyboard(pChild.get()))
283 return pChild->OnKeyDown(nKeyCode, nFlag);
284 }
285 return false;
286 }
287
OnChar(uint16_t nChar,Mask<FWL_EVENTFLAG> nFlag)288 bool CPWL_Wnd::OnChar(uint16_t nChar, Mask<FWL_EVENTFLAG> nFlag) {
289 if (!IsValid() || !IsVisible())
290 return false;
291 if (!IsWndCaptureKeyboard(this))
292 return false;
293 for (const auto& pChild : m_Children) {
294 if (IsWndCaptureKeyboard(pChild.get()))
295 return pChild->OnChar(nChar, nFlag);
296 }
297 return false;
298 }
299
300 #define PWL_IMPLEMENT_MOUSE_METHOD(mouse_method_name) \
301 bool CPWL_Wnd::mouse_method_name(Mask<FWL_EVENTFLAG> nFlag, \
302 const CFX_PointF& point) { \
303 if (!IsValid() || !IsVisible()) \
304 return false; \
305 if (IsWndCaptureMouse(this)) { \
306 for (const auto& pChild : m_Children) { \
307 if (IsWndCaptureMouse(pChild.get())) { \
308 return pChild->mouse_method_name(nFlag, point); \
309 } \
310 } \
311 SetCursor(); \
312 return false; \
313 } \
314 for (const auto& pChild : m_Children) { \
315 if (pChild->WndHitTest(point)) { \
316 return pChild->mouse_method_name(nFlag, point); \
317 } \
318 } \
319 if (WndHitTest(point)) \
320 SetCursor(); \
321 return false; \
322 }
323
324 PWL_IMPLEMENT_MOUSE_METHOD(OnLButtonDblClk)
PWL_IMPLEMENT_MOUSE_METHOD(OnLButtonDown)325 PWL_IMPLEMENT_MOUSE_METHOD(OnLButtonDown)
326 PWL_IMPLEMENT_MOUSE_METHOD(OnLButtonUp)
327 PWL_IMPLEMENT_MOUSE_METHOD(OnMouseMove)
328 #undef PWL_IMPLEMENT_MOUSE_METHOD
329
330 // Unlike their FWL counterparts, PWL windows don't handle right clicks.
331 bool CPWL_Wnd::OnRButtonDown(Mask<FWL_EVENTFLAG> nFlag,
332 const CFX_PointF& point) {
333 return false;
334 }
335
OnRButtonUp(Mask<FWL_EVENTFLAG> nFlag,const CFX_PointF & point)336 bool CPWL_Wnd::OnRButtonUp(Mask<FWL_EVENTFLAG> nFlag, const CFX_PointF& point) {
337 return false;
338 }
339
GetText()340 WideString CPWL_Wnd::GetText() {
341 return WideString();
342 }
343
GetSelectedText()344 WideString CPWL_Wnd::GetSelectedText() {
345 return WideString();
346 }
347
ReplaceAndKeepSelection(const WideString & text)348 void CPWL_Wnd::ReplaceAndKeepSelection(const WideString& text) {}
349
ReplaceSelection(const WideString & text)350 void CPWL_Wnd::ReplaceSelection(const WideString& text) {}
351
SelectAllText()352 bool CPWL_Wnd::SelectAllText() {
353 return false;
354 }
355
CanUndo()356 bool CPWL_Wnd::CanUndo() {
357 return false;
358 }
359
CanRedo()360 bool CPWL_Wnd::CanRedo() {
361 return false;
362 }
363
Undo()364 bool CPWL_Wnd::Undo() {
365 return false;
366 }
367
Redo()368 bool CPWL_Wnd::Redo() {
369 return false;
370 }
371
OnMouseWheel(Mask<FWL_EVENTFLAG> nFlag,const CFX_PointF & point,const CFX_Vector & delta)372 bool CPWL_Wnd::OnMouseWheel(Mask<FWL_EVENTFLAG> nFlag,
373 const CFX_PointF& point,
374 const CFX_Vector& delta) {
375 if (!IsValid() || !IsVisible())
376 return false;
377
378 SetCursor();
379 if (!IsWndCaptureKeyboard(this))
380 return false;
381
382 for (const auto& pChild : m_Children) {
383 if (IsWndCaptureKeyboard(pChild.get()))
384 return pChild->OnMouseWheel(nFlag, point, delta);
385 }
386 return false;
387 }
388
AddChild(std::unique_ptr<CPWL_Wnd> pWnd)389 void CPWL_Wnd::AddChild(std::unique_ptr<CPWL_Wnd> pWnd) {
390 DCHECK(!pWnd->m_pParent);
391 pWnd->m_pParent = this;
392 m_Children.push_back(std::move(pWnd));
393 }
394
RemoveChild(CPWL_Wnd * pWnd)395 void CPWL_Wnd::RemoveChild(CPWL_Wnd* pWnd) {
396 DCHECK_EQ(pWnd->m_pParent, this);
397 auto it =
398 std::find(m_Children.begin(), m_Children.end(), MakeFakeUniquePtr(pWnd));
399 if (it == m_Children.end())
400 return;
401
402 // TODO(tsepez): murky ownership.
403 it->release();
404 m_Children.erase(it);
405 }
406
SetScrollInfo(const PWL_SCROLL_INFO & info)407 void CPWL_Wnd::SetScrollInfo(const PWL_SCROLL_INFO& info) {}
408
SetScrollPosition(float pos)409 void CPWL_Wnd::SetScrollPosition(float pos) {}
410
ScrollWindowVertically(float pos)411 void CPWL_Wnd::ScrollWindowVertically(float pos) {}
412
NotifyLButtonDown(CPWL_Wnd * child,const CFX_PointF & pos)413 void CPWL_Wnd::NotifyLButtonDown(CPWL_Wnd* child, const CFX_PointF& pos) {}
414
NotifyLButtonUp(CPWL_Wnd * child,const CFX_PointF & pos)415 void CPWL_Wnd::NotifyLButtonUp(CPWL_Wnd* child, const CFX_PointF& pos) {}
416
NotifyMouseMove(CPWL_Wnd * child,const CFX_PointF & pos)417 void CPWL_Wnd::NotifyMouseMove(CPWL_Wnd* child, const CFX_PointF& pos) {}
418
GetWindowRect() const419 CFX_FloatRect CPWL_Wnd::GetWindowRect() const {
420 return m_rcWindow;
421 }
422
GetClientRect() const423 CFX_FloatRect CPWL_Wnd::GetClientRect() const {
424 CFX_FloatRect rcWindow = GetWindowRect();
425
426 float width = static_cast<float>(GetBorderWidth() + GetInnerBorderWidth());
427 CFX_FloatRect rcClient = rcWindow.GetDeflated(width, width);
428 if (CPWL_ScrollBar* pVSB = GetVScrollBar())
429 rcClient.right -= pVSB->GetScrollBarWidth();
430
431 rcClient.Normalize();
432 return rcWindow.Contains(rcClient) ? rcClient : CFX_FloatRect();
433 }
434
GetCenterPoint() const435 CFX_PointF CPWL_Wnd::GetCenterPoint() const {
436 CFX_FloatRect rcClient = GetClientRect();
437 return CFX_PointF((rcClient.left + rcClient.right) * 0.5f,
438 (rcClient.top + rcClient.bottom) * 0.5f);
439 }
440
HasFlag(uint32_t dwFlags) const441 bool CPWL_Wnd::HasFlag(uint32_t dwFlags) const {
442 return (m_CreationParams.dwFlags & dwFlags) != 0;
443 }
444
RemoveFlag(uint32_t dwFlags)445 void CPWL_Wnd::RemoveFlag(uint32_t dwFlags) {
446 m_CreationParams.dwFlags &= ~dwFlags;
447 }
448
GetBackgroundColor() const449 CFX_Color CPWL_Wnd::GetBackgroundColor() const {
450 return m_CreationParams.sBackgroundColor;
451 }
452
GetTextColor() const453 CFX_Color CPWL_Wnd::GetTextColor() const {
454 return m_CreationParams.sTextColor;
455 }
456
GetBorderStyle() const457 BorderStyle CPWL_Wnd::GetBorderStyle() const {
458 return m_CreationParams.nBorderStyle;
459 }
460
GetBorderWidth() const461 int32_t CPWL_Wnd::GetBorderWidth() const {
462 return HasFlag(PWS_BORDER) ? m_CreationParams.dwBorderWidth : 0;
463 }
464
GetInnerBorderWidth() const465 int32_t CPWL_Wnd::GetInnerBorderWidth() const {
466 return 0;
467 }
468
GetBorderColor() const469 CFX_Color CPWL_Wnd::GetBorderColor() const {
470 return HasFlag(PWS_BORDER) ? m_CreationParams.sBorderColor : CFX_Color();
471 }
472
GetBorderDash() const473 const CPWL_Dash& CPWL_Wnd::GetBorderDash() const {
474 return m_CreationParams.sDash;
475 }
476
GetVScrollBar() const477 CPWL_ScrollBar* CPWL_Wnd::GetVScrollBar() const {
478 return HasFlag(PWS_VSCROLL) ? m_pVScrollBar : nullptr;
479 }
480
CreateScrollBar(const CreateParams & cp)481 void CPWL_Wnd::CreateScrollBar(const CreateParams& cp) {
482 CreateVScrollBar(cp);
483 }
484
CreateVScrollBar(const CreateParams & cp)485 void CPWL_Wnd::CreateVScrollBar(const CreateParams& cp) {
486 if (m_pVScrollBar || !HasFlag(PWS_VSCROLL))
487 return;
488
489 CreateParams scp = cp;
490 scp.dwFlags = PWS_BACKGROUND | PWS_AUTOTRANSPARENT | PWS_NOREFRESHCLIP;
491 scp.sBackgroundColor = kDefaultWhiteColor;
492 scp.eCursorType = IPWL_FillerNotify::CursorStyle::kArrow;
493 scp.nTransparency = CPWL_ScrollBar::kTransparency;
494
495 auto pBar = std::make_unique<CPWL_ScrollBar>(scp, CloneAttachedData());
496 m_pVScrollBar = pBar.get();
497 AddChild(std::move(pBar));
498 m_pVScrollBar->Realize();
499 }
500
SetCapture()501 void CPWL_Wnd::SetCapture() {
502 if (CPWL_MsgControl* pMsgCtrl = GetMsgControl())
503 pMsgCtrl->SetCapture(this);
504 }
505
ReleaseCapture()506 void CPWL_Wnd::ReleaseCapture() {
507 for (const auto& pChild : m_Children)
508 pChild->ReleaseCapture();
509
510 if (CPWL_MsgControl* pMsgCtrl = GetMsgControl())
511 pMsgCtrl->ReleaseCapture();
512 }
513
SetFocus()514 void CPWL_Wnd::SetFocus() {
515 if (CPWL_MsgControl* pMsgCtrl = GetMsgControl()) {
516 if (!pMsgCtrl->IsMainCaptureKeyboard(this))
517 pMsgCtrl->KillFocus();
518 pMsgCtrl->SetFocus(this);
519 }
520 }
521
KillFocus()522 void CPWL_Wnd::KillFocus() {
523 if (CPWL_MsgControl* pMsgCtrl = GetMsgControl()) {
524 if (pMsgCtrl->IsWndCaptureKeyboard(this))
525 pMsgCtrl->KillFocus();
526 }
527 }
528
OnSetFocus()529 void CPWL_Wnd::OnSetFocus() {}
530
OnKillFocus()531 void CPWL_Wnd::OnKillFocus() {}
532
CloneAttachedData() const533 std::unique_ptr<IPWL_FillerNotify::PerWindowData> CPWL_Wnd::CloneAttachedData()
534 const {
535 return m_pAttachedData ? m_pAttachedData->Clone() : nullptr;
536 }
537
GetAncestors()538 std::vector<UnownedPtr<CPWL_Wnd>> CPWL_Wnd::GetAncestors() {
539 std::vector<UnownedPtr<CPWL_Wnd>> results;
540 for (CPWL_Wnd* pWnd = this; pWnd; pWnd = pWnd->GetParentWindow()) {
541 results.emplace_back(pWnd);
542 }
543 return results;
544 }
545
WndHitTest(const CFX_PointF & point) const546 bool CPWL_Wnd::WndHitTest(const CFX_PointF& point) const {
547 return IsValid() && IsVisible() && GetWindowRect().Contains(point);
548 }
549
ClientHitTest(const CFX_PointF & point) const550 bool CPWL_Wnd::ClientHitTest(const CFX_PointF& point) const {
551 return IsValid() && IsVisible() && GetClientRect().Contains(point);
552 }
553
SetVisible(bool bVisible)554 bool CPWL_Wnd::SetVisible(bool bVisible) {
555 if (!IsValid())
556 return true;
557
558 ObservedPtr<CPWL_Wnd> thisObserved(this);
559 for (const auto& pChild : m_Children) {
560 pChild->SetVisible(bVisible);
561 if (!thisObserved)
562 return false;
563 }
564
565 if (bVisible != m_bVisible) {
566 m_bVisible = bVisible;
567 if (!RePosChildWnd())
568 return false;
569
570 if (!InvalidateRect(nullptr))
571 return false;
572 }
573 return true;
574 }
575
SetClipRect(const CFX_FloatRect & rect)576 void CPWL_Wnd::SetClipRect(const CFX_FloatRect& rect) {
577 m_rcClip = rect;
578 m_rcClip.Normalize();
579 }
580
GetClipRect() const581 const CFX_FloatRect& CPWL_Wnd::GetClipRect() const {
582 return m_rcClip;
583 }
584
IsReadOnly() const585 bool CPWL_Wnd::IsReadOnly() const {
586 return HasFlag(PWS_READONLY);
587 }
588
RePosChildWnd()589 bool CPWL_Wnd::RePosChildWnd() {
590 CPWL_ScrollBar* pVSB = GetVScrollBar();
591 if (!pVSB)
592 return true;
593
594 CFX_FloatRect rcContent = GetWindowRect();
595 if (!rcContent.IsEmpty()) {
596 float width = static_cast<float>(GetBorderWidth() + GetInnerBorderWidth());
597 rcContent.Deflate(width, width);
598 rcContent.Normalize();
599 }
600 CFX_FloatRect rcVScroll =
601 CFX_FloatRect(rcContent.right - CPWL_ScrollBar::kWidth, rcContent.bottom,
602 rcContent.right - 1.0f, rcContent.top);
603
604 ObservedPtr<CPWL_Wnd> thisObserved(this);
605 pVSB->Move(rcVScroll, true, false);
606 if (!thisObserved)
607 return false;
608
609 return true;
610 }
611
CreateChildWnd(const CreateParams & cp)612 void CPWL_Wnd::CreateChildWnd(const CreateParams& cp) {}
613
SetCursor()614 void CPWL_Wnd::SetCursor() {
615 if (IsValid())
616 GetFillerNotify()->SetCursor(GetCreationParams()->eCursorType);
617 }
618
CreateMsgControl()619 void CPWL_Wnd::CreateMsgControl() {
620 if (!m_CreationParams.pMsgControl)
621 m_CreationParams.pMsgControl = new CPWL_MsgControl(this);
622 }
623
DestroyMsgControl()624 void CPWL_Wnd::DestroyMsgControl() {
625 CPWL_MsgControl* pMsgControl = GetMsgControl();
626 if (pMsgControl && pMsgControl->IsWndCreated(this))
627 delete pMsgControl;
628 }
629
GetMsgControl() const630 CPWL_MsgControl* CPWL_Wnd::GetMsgControl() const {
631 return m_CreationParams.pMsgControl;
632 }
633
IsCaptureMouse() const634 bool CPWL_Wnd::IsCaptureMouse() const {
635 return IsWndCaptureMouse(this);
636 }
637
IsWndCaptureMouse(const CPWL_Wnd * pWnd) const638 bool CPWL_Wnd::IsWndCaptureMouse(const CPWL_Wnd* pWnd) const {
639 CPWL_MsgControl* pCtrl = GetMsgControl();
640 return pCtrl && pCtrl->IsWndCaptureMouse(pWnd);
641 }
642
IsWndCaptureKeyboard(const CPWL_Wnd * pWnd) const643 bool CPWL_Wnd::IsWndCaptureKeyboard(const CPWL_Wnd* pWnd) const {
644 CPWL_MsgControl* pCtrl = GetMsgControl();
645 return pCtrl && pCtrl->IsWndCaptureKeyboard(pWnd);
646 }
647
IsFocused() const648 bool CPWL_Wnd::IsFocused() const {
649 CPWL_MsgControl* pCtrl = GetMsgControl();
650 return pCtrl && pCtrl->IsMainCaptureKeyboard(this);
651 }
652
GetFocusRect() const653 CFX_FloatRect CPWL_Wnd::GetFocusRect() const {
654 CFX_FloatRect rect = GetWindowRect();
655 if (!rect.IsEmpty()) {
656 rect.Inflate(1.0f, 1.0f);
657 rect.Normalize();
658 }
659 return rect;
660 }
661
GetFontSize() const662 float CPWL_Wnd::GetFontSize() const {
663 return m_CreationParams.fFontSize;
664 }
665
SetFontSize(float fFontSize)666 void CPWL_Wnd::SetFontSize(float fFontSize) {
667 m_CreationParams.fFontSize = fFontSize;
668 }
669
GetBorderLeftTopColor(BorderStyle nBorderStyle) const670 CFX_Color CPWL_Wnd::GetBorderLeftTopColor(BorderStyle nBorderStyle) const {
671 switch (nBorderStyle) {
672 case BorderStyle::kBeveled:
673 return CFX_Color(CFX_Color::Type::kGray, 1);
674 case BorderStyle::kInset:
675 return CFX_Color(CFX_Color::Type::kGray, 0.5f);
676 default:
677 return CFX_Color();
678 }
679 }
680
GetBorderRightBottomColor(BorderStyle nBorderStyle) const681 CFX_Color CPWL_Wnd::GetBorderRightBottomColor(BorderStyle nBorderStyle) const {
682 switch (nBorderStyle) {
683 case BorderStyle::kBeveled:
684 return GetBackgroundColor() / 2.0f;
685 case BorderStyle::kInset:
686 return CFX_Color(CFX_Color::Type::kGray, 0.75f);
687 default:
688 return CFX_Color();
689 }
690 }
691
GetTransparency()692 int32_t CPWL_Wnd::GetTransparency() {
693 return m_CreationParams.nTransparency;
694 }
695
SetTransparency(int32_t nTransparency)696 void CPWL_Wnd::SetTransparency(int32_t nTransparency) {
697 for (const auto& pChild : m_Children)
698 pChild->SetTransparency(nTransparency);
699
700 m_CreationParams.nTransparency = nTransparency;
701 }
702
GetWindowMatrix() const703 CFX_Matrix CPWL_Wnd::GetWindowMatrix() const {
704 CFX_Matrix mt;
705 if (ProviderIface* pProvider = GetProvider())
706 mt.Concat(pProvider->GetWindowMatrix(GetAttachedData()));
707 return mt;
708 }
709
PWLtoWnd(const CFX_FloatRect & rect) const710 CFX_FloatRect CPWL_Wnd::PWLtoWnd(const CFX_FloatRect& rect) const {
711 CFX_Matrix mt = GetWindowMatrix();
712 return mt.TransformRect(rect);
713 }
714