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