xref: /aosp_15_r20/external/pdfium/xfa/fwl/cfwl_widget.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 "xfa/fwl/cfwl_widget.h"
8 
9 #include <algorithm>
10 #include <utility>
11 #include <vector>
12 
13 #include "third_party/base/check.h"
14 #include "v8/include/cppgc/visitor.h"
15 #include "xfa/fde/cfde_textout.h"
16 #include "xfa/fwl/cfwl_app.h"
17 #include "xfa/fwl/cfwl_combobox.h"
18 #include "xfa/fwl/cfwl_event.h"
19 #include "xfa/fwl/cfwl_eventmouse.h"
20 #include "xfa/fwl/cfwl_messagekey.h"
21 #include "xfa/fwl/cfwl_messagekillfocus.h"
22 #include "xfa/fwl/cfwl_messagemouse.h"
23 #include "xfa/fwl/cfwl_messagemousewheel.h"
24 #include "xfa/fwl/cfwl_messagesetfocus.h"
25 #include "xfa/fwl/cfwl_notedriver.h"
26 #include "xfa/fwl/cfwl_themebackground.h"
27 #include "xfa/fwl/cfwl_themepart.h"
28 #include "xfa/fwl/cfwl_themetext.h"
29 #include "xfa/fwl/cfwl_widgetmgr.h"
30 #include "xfa/fwl/ifwl_themeprovider.h"
31 
32 namespace {
33 
34 constexpr float kCalcHeight = 2048.0f;
35 constexpr float kCalcWidth = 2048.0f;
36 constexpr float kCalcMultiLineDefWidth = 120.0f;
37 
38 }  // namespace
39 
CFWL_Widget(CFWL_App * app,const Properties & properties,CFWL_Widget * pOuter)40 CFWL_Widget::CFWL_Widget(CFWL_App* app,
41                          const Properties& properties,
42                          CFWL_Widget* pOuter)
43     : m_Properties(properties),
44       m_pFWLApp(app),
45       m_pWidgetMgr(app->GetWidgetMgr()),
46       m_pOuter(pOuter) {
47   m_pWidgetMgr->InsertWidget(m_pOuter, this);
48 }
49 
50 CFWL_Widget::~CFWL_Widget() = default;
51 
PreFinalize()52 void CFWL_Widget::PreFinalize() {
53   CHECK(!IsLocked());  // Prefer hard stop to UaF.
54   NotifyDriver();
55   m_pWidgetMgr->RemoveWidget(this);
56 }
57 
Trace(cppgc::Visitor * visitor) const58 void CFWL_Widget::Trace(cppgc::Visitor* visitor) const {
59   visitor->Trace(m_pAdapterIface);
60   visitor->Trace(m_pFWLApp);
61   visitor->Trace(m_pWidgetMgr);
62   visitor->Trace(m_pDelegate);
63   visitor->Trace(m_pOuter);
64 }
65 
IsForm() const66 bool CFWL_Widget::IsForm() const {
67   return false;
68 }
69 
GetAutosizedWidgetRect()70 CFX_RectF CFWL_Widget::GetAutosizedWidgetRect() {
71   return CFX_RectF();
72 }
73 
GetWidgetRect()74 CFX_RectF CFWL_Widget::GetWidgetRect() {
75   return m_WidgetRect;
76 }
77 
InflateWidgetRect(CFX_RectF & rect)78 void CFWL_Widget::InflateWidgetRect(CFX_RectF& rect) {
79   if (!HasBorder())
80     return;
81 
82   float fBorder = GetCXBorderSize();
83   rect.Inflate(fBorder, fBorder);
84 }
85 
SetWidgetRect(const CFX_RectF & rect)86 void CFWL_Widget::SetWidgetRect(const CFX_RectF& rect) {
87   m_WidgetRect = rect;
88 }
89 
GetClientRect()90 CFX_RectF CFWL_Widget::GetClientRect() {
91   return GetEdgeRect();
92 }
93 
ModifyStyles(uint32_t dwStylesAdded,uint32_t dwStylesRemoved)94 void CFWL_Widget::ModifyStyles(uint32_t dwStylesAdded,
95                                uint32_t dwStylesRemoved) {
96   m_Properties.m_dwStyles &= ~dwStylesRemoved;
97   m_Properties.m_dwStyles |= dwStylesAdded;
98 }
99 
ModifyStyleExts(uint32_t dwStyleExtsAdded,uint32_t dwStyleExtsRemoved)100 void CFWL_Widget::ModifyStyleExts(uint32_t dwStyleExtsAdded,
101                                   uint32_t dwStyleExtsRemoved) {
102   m_Properties.m_dwStyleExts &= ~dwStyleExtsRemoved;
103   m_Properties.m_dwStyleExts |= dwStyleExtsAdded;
104 }
105 
NotifyHideChildWidget(CFWL_WidgetMgr * widgetMgr,CFWL_Widget * widget,CFWL_NoteDriver * noteDriver)106 static void NotifyHideChildWidget(CFWL_WidgetMgr* widgetMgr,
107                                   CFWL_Widget* widget,
108                                   CFWL_NoteDriver* noteDriver) {
109   CFWL_Widget* child = widgetMgr->GetFirstChildWidget(widget);
110   while (child) {
111     noteDriver->NotifyTargetHide(child);
112     NotifyHideChildWidget(widgetMgr, child, noteDriver);
113     child = widgetMgr->GetNextSiblingWidget(child);
114   }
115 }
116 
SetStates(uint32_t dwStates)117 void CFWL_Widget::SetStates(uint32_t dwStates) {
118   m_Properties.m_dwStates |= dwStates;
119   if (IsVisible())
120     return;
121 
122   CFWL_NoteDriver* noteDriver = GetFWLApp()->GetNoteDriver();
123   noteDriver->NotifyTargetHide(this);
124 
125   CFWL_WidgetMgr* widgetMgr = GetFWLApp()->GetWidgetMgr();
126   CFWL_Widget* child = widgetMgr->GetFirstChildWidget(this);
127   while (child) {
128     noteDriver->NotifyTargetHide(child);
129     NotifyHideChildWidget(widgetMgr, child, noteDriver);
130     child = widgetMgr->GetNextSiblingWidget(child);
131   }
132 }
133 
RemoveStates(uint32_t dwStates)134 void CFWL_Widget::RemoveStates(uint32_t dwStates) {
135   m_Properties.m_dwStates &= ~dwStates;
136 }
137 
HitTest(const CFX_PointF & point)138 FWL_WidgetHit CFWL_Widget::HitTest(const CFX_PointF& point) {
139   if (GetClientRect().Contains(point))
140     return FWL_WidgetHit::Client;
141   if (HasBorder() && GetRelativeRect().Contains(point))
142     return FWL_WidgetHit::Border;
143   return FWL_WidgetHit::Unknown;
144 }
145 
TransformTo(CFWL_Widget * pWidget,const CFX_PointF & point)146 CFX_PointF CFWL_Widget::TransformTo(CFWL_Widget* pWidget,
147                                     const CFX_PointF& point) {
148   CFX_SizeF szOffset;
149   if (IsParent(pWidget)) {
150     szOffset = GetOffsetFromParent(pWidget);
151   } else {
152     szOffset = pWidget->GetOffsetFromParent(this);
153     szOffset.width = -szOffset.width;
154     szOffset.height = -szOffset.height;
155   }
156   return point + CFX_PointF(szOffset.width, szOffset.height);
157 }
158 
GetMatrix() const159 CFX_Matrix CFWL_Widget::GetMatrix() const {
160   CFWL_Widget* parent = GetParent();
161   std::vector<CFWL_Widget*> parents;
162   while (parent) {
163     parents.push_back(parent);
164     parent = parent->GetParent();
165   }
166 
167   CFX_Matrix matrix;
168   for (size_t i = parents.size(); i >= 2; i--) {
169     CFX_RectF rect = parents[i - 2]->GetWidgetRect();
170     matrix.TranslatePrepend(rect.left, rect.top);
171   }
172   return matrix;
173 }
174 
GetThemeProvider() const175 IFWL_ThemeProvider* CFWL_Widget::GetThemeProvider() const {
176   return GetFWLApp()->GetThemeProvider();
177 }
178 
IsEnabled() const179 bool CFWL_Widget::IsEnabled() const {
180   return (m_Properties.m_dwStates & FWL_STATE_WGT_Disabled) == 0;
181 }
182 
HasBorder() const183 bool CFWL_Widget::HasBorder() const {
184   return !!(m_Properties.m_dwStyles & FWL_STYLE_WGT_Border);
185 }
186 
IsVisible() const187 bool CFWL_Widget::IsVisible() const {
188   return !(m_Properties.m_dwStates & FWL_STATE_WGT_Invisible);
189 }
190 
IsOverLapper() const191 bool CFWL_Widget::IsOverLapper() const {
192   return (m_Properties.m_dwStyles & FWL_STYLE_WGT_WindowTypeMask) ==
193          FWL_STYLE_WGT_OverLapper;
194 }
195 
IsPopup() const196 bool CFWL_Widget::IsPopup() const {
197   return !!(m_Properties.m_dwStyles & FWL_STYLE_WGT_Popup);
198 }
199 
IsChild() const200 bool CFWL_Widget::IsChild() const {
201   return !!(m_Properties.m_dwStyles & FWL_STYLE_WGT_Child);
202 }
203 
GetOutmost() const204 CFWL_Widget* CFWL_Widget::GetOutmost() const {
205   CFWL_Widget* pOuter = const_cast<CFWL_Widget*>(this);
206   while (pOuter->GetOuter())
207     pOuter = pOuter->GetOuter();
208   return pOuter;
209 }
210 
GetEdgeRect() const211 CFX_RectF CFWL_Widget::GetEdgeRect() const {
212   CFX_RectF rtEdge(0, 0, m_WidgetRect.width, m_WidgetRect.height);
213   if (HasBorder())
214     rtEdge.Deflate(GetCXBorderSize(), GetCYBorderSize());
215   return rtEdge;
216 }
217 
GetCXBorderSize() const218 float CFWL_Widget::GetCXBorderSize() const {
219   return GetThemeProvider()->GetCXBorderSize();
220 }
221 
GetCYBorderSize() const222 float CFWL_Widget::GetCYBorderSize() const {
223   return GetThemeProvider()->GetCYBorderSize();
224 }
225 
GetRelativeRect() const226 CFX_RectF CFWL_Widget::GetRelativeRect() const {
227   return CFX_RectF(0, 0, m_WidgetRect.width, m_WidgetRect.height);
228 }
229 
CalcTextSize(const WideString & wsText,bool bMultiLine)230 CFX_SizeF CFWL_Widget::CalcTextSize(const WideString& wsText, bool bMultiLine) {
231   CFWL_ThemeText calPart(CFWL_ThemePart::Part::kNone, this, nullptr);
232   calPart.m_wsText = wsText;
233   if (bMultiLine)
234     calPart.m_dwTTOStyles.line_wrap_ = true;
235   else
236     calPart.m_dwTTOStyles.single_line_ = true;
237 
238   calPart.m_iTTOAlign = FDE_TextAlignment::kTopLeft;
239   float fWidth = bMultiLine ? kCalcMultiLineDefWidth : kCalcWidth;
240   CFX_RectF rect(0, 0, fWidth, kCalcHeight);
241   GetThemeProvider()->CalcTextRect(calPart, &rect);
242   return CFX_SizeF(rect.width, rect.height);
243 }
244 
CalcTextRect(const WideString & wsText,const FDE_TextStyle & dwTTOStyles,FDE_TextAlignment iTTOAlign,CFX_RectF * pRect)245 void CFWL_Widget::CalcTextRect(const WideString& wsText,
246                                const FDE_TextStyle& dwTTOStyles,
247                                FDE_TextAlignment iTTOAlign,
248                                CFX_RectF* pRect) {
249   CFWL_ThemeText calPart(CFWL_ThemePart::Part::kNone, this, nullptr);
250   calPart.m_wsText = wsText;
251   calPart.m_dwTTOStyles = dwTTOStyles;
252   calPart.m_iTTOAlign = iTTOAlign;
253   GetThemeProvider()->CalcTextRect(calPart, pRect);
254 }
255 
SetGrab(bool bSet)256 void CFWL_Widget::SetGrab(bool bSet) {
257   CFWL_NoteDriver* pDriver = GetFWLApp()->GetNoteDriver();
258   pDriver->SetGrab(bSet ? this : nullptr);
259 }
260 
UnregisterEventTarget()261 void CFWL_Widget::UnregisterEventTarget() {
262   CFWL_NoteDriver* pNoteDriver = GetFWLApp()->GetNoteDriver();
263   pNoteDriver->UnregisterEventTarget(this);
264 }
265 
DispatchEvent(CFWL_Event * pEvent)266 void CFWL_Widget::DispatchEvent(CFWL_Event* pEvent) {
267   if (m_pOuter) {
268     m_pOuter->GetDelegate()->OnProcessEvent(pEvent);
269     return;
270   }
271   CFWL_NoteDriver* pNoteDriver = GetFWLApp()->GetNoteDriver();
272   pNoteDriver->SendEvent(pEvent);
273 }
274 
RepaintRect(const CFX_RectF & pRect)275 void CFWL_Widget::RepaintRect(const CFX_RectF& pRect) {
276   m_pWidgetMgr->RepaintWidget(this, pRect);
277 }
278 
DrawBackground(CFGAS_GEGraphics * pGraphics,CFWL_ThemePart::Part iPartBk,const CFX_Matrix & mtMatrix)279 void CFWL_Widget::DrawBackground(CFGAS_GEGraphics* pGraphics,
280                                  CFWL_ThemePart::Part iPartBk,
281                                  const CFX_Matrix& mtMatrix) {
282   CFWL_ThemeBackground param(iPartBk, this, pGraphics);
283   param.m_matrix = mtMatrix;
284   param.m_PartRect = GetRelativeRect();
285   GetThemeProvider()->DrawBackground(param);
286 }
287 
DrawBorder(CFGAS_GEGraphics * pGraphics,CFWL_ThemePart::Part iPartBorder,const CFX_Matrix & matrix)288 void CFWL_Widget::DrawBorder(CFGAS_GEGraphics* pGraphics,
289                              CFWL_ThemePart::Part iPartBorder,
290                              const CFX_Matrix& matrix) {
291   CFWL_ThemeBackground param(iPartBorder, this, pGraphics);
292   param.m_matrix = matrix;
293   param.m_PartRect = GetRelativeRect();
294   GetThemeProvider()->DrawBackground(param);
295 }
296 
NotifyDriver()297 void CFWL_Widget::NotifyDriver() {
298   CFWL_NoteDriver* pDriver = GetFWLApp()->GetNoteDriver();
299   pDriver->NotifyTargetDestroy(this);
300 }
301 
GetOffsetFromParent(CFWL_Widget * pParent)302 CFX_SizeF CFWL_Widget::GetOffsetFromParent(CFWL_Widget* pParent) {
303   if (pParent == this)
304     return CFX_SizeF();
305 
306   CFX_SizeF szRet(m_WidgetRect.left, m_WidgetRect.top);
307   CFWL_WidgetMgr* pWidgetMgr = GetFWLApp()->GetWidgetMgr();
308   CFWL_Widget* pDstWidget = GetParent();
309   while (pDstWidget && pDstWidget != pParent) {
310     CFX_RectF rtDst = pDstWidget->GetWidgetRect();
311     szRet += CFX_SizeF(rtDst.left, rtDst.top);
312     pDstWidget = pWidgetMgr->GetParentWidget(pDstWidget);
313   }
314   return szRet;
315 }
316 
IsParent(CFWL_Widget * pParent)317 bool CFWL_Widget::IsParent(CFWL_Widget* pParent) {
318   CFWL_Widget* pUpWidget = GetParent();
319   while (pUpWidget) {
320     if (pUpWidget == pParent)
321       return true;
322     pUpWidget = pUpWidget->GetParent();
323   }
324   return false;
325 }
326 
OnProcessMessage(CFWL_Message * pMessage)327 void CFWL_Widget::OnProcessMessage(CFWL_Message* pMessage) {
328   CFWL_Widget* pWidget = pMessage->GetDstTarget();
329   if (!pWidget)
330     return;
331 
332   switch (pMessage->GetType()) {
333     case CFWL_Message::Type::kMouse: {
334       CFWL_MessageMouse* pMsgMouse = static_cast<CFWL_MessageMouse*>(pMessage);
335       CFWL_EventMouse evt(pWidget, pWidget, pMsgMouse->m_dwCmd);
336       pWidget->DispatchEvent(&evt);
337       break;
338     }
339     default:
340       break;
341   }
342 }
343 
OnProcessEvent(CFWL_Event * pEvent)344 void CFWL_Widget::OnProcessEvent(CFWL_Event* pEvent) {}
345 
ScopedUpdateLock(CFWL_Widget * widget)346 CFWL_Widget::ScopedUpdateLock::ScopedUpdateLock(CFWL_Widget* widget)
347     : widget_(widget) {
348   widget_->LockUpdate();
349 }
350 
~ScopedUpdateLock()351 CFWL_Widget::ScopedUpdateLock::~ScopedUpdateLock() {
352   widget_->UnlockUpdate();
353 }
354