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