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/stl_util.h"
14 #include "xfa/fde/cfde_textout.h"
15 #include "xfa/fwl/cfwl_app.h"
16 #include "xfa/fwl/cfwl_combobox.h"
17 #include "xfa/fwl/cfwl_event.h"
18 #include "xfa/fwl/cfwl_eventmouse.h"
19 #include "xfa/fwl/cfwl_messagekey.h"
20 #include "xfa/fwl/cfwl_messagekillfocus.h"
21 #include "xfa/fwl/cfwl_messagemouse.h"
22 #include "xfa/fwl/cfwl_messagemousewheel.h"
23 #include "xfa/fwl/cfwl_messagesetfocus.h"
24 #include "xfa/fwl/cfwl_notedriver.h"
25 #include "xfa/fwl/cfwl_themebackground.h"
26 #include "xfa/fwl/cfwl_themepart.h"
27 #include "xfa/fwl/cfwl_themetext.h"
28 #include "xfa/fwl/cfwl_widgetmgr.h"
29 #include "xfa/fwl/ifwl_themeprovider.h"
30 
31 #define FWL_WGT_CalcHeight 2048
32 #define FWL_WGT_CalcWidth 2048
33 #define FWL_WGT_CalcMultiLineDefWidth 120.0f
34 
CFWL_Widget(const CFWL_App * app,std::unique_ptr<CFWL_WidgetProperties> properties,CFWL_Widget * pOuter)35 CFWL_Widget::CFWL_Widget(const CFWL_App* app,
36                          std::unique_ptr<CFWL_WidgetProperties> properties,
37                          CFWL_Widget* pOuter)
38     : m_pOwnerApp(app),
39       m_pWidgetMgr(app->GetWidgetMgr()),
40       m_pProperties(std::move(properties)),
41       m_pOuter(pOuter) {
42   ASSERT(m_pWidgetMgr);
43   ASSERT(m_pProperties);
44   m_pWidgetMgr->InsertWidget(m_pProperties->m_pParent, this);
45 }
46 
~CFWL_Widget()47 CFWL_Widget::~CFWL_Widget() {
48   CHECK(!IsLocked());  // Prefer hard stop to UaF.
49   NotifyDriver();
50   m_pWidgetMgr->RemoveWidget(this);
51 }
52 
IsForm() const53 bool CFWL_Widget::IsForm() const {
54   return false;
55 }
56 
GetAutosizedWidgetRect()57 CFX_RectF CFWL_Widget::GetAutosizedWidgetRect() {
58   return CFX_RectF();
59 }
60 
GetWidgetRect()61 CFX_RectF CFWL_Widget::GetWidgetRect() {
62   return m_pProperties->m_rtWidget;
63 }
64 
InflateWidgetRect(CFX_RectF & rect)65 void CFWL_Widget::InflateWidgetRect(CFX_RectF& rect) {
66   if (!HasBorder())
67     return;
68 
69   float fBorder = GetCXBorderSize();
70   rect.Inflate(fBorder, fBorder);
71 }
72 
SetWidgetRect(const CFX_RectF & rect)73 void CFWL_Widget::SetWidgetRect(const CFX_RectF& rect) {
74   m_pProperties->m_rtWidget = rect;
75 }
76 
GetClientRect()77 CFX_RectF CFWL_Widget::GetClientRect() {
78   return GetEdgeRect();
79 }
80 
SetParent(CFWL_Widget * pParent)81 void CFWL_Widget::SetParent(CFWL_Widget* pParent) {
82   m_pProperties->m_pParent = pParent;
83   m_pWidgetMgr->SetParent(pParent, this);
84 }
85 
ModifyStyles(uint32_t dwStylesAdded,uint32_t dwStylesRemoved)86 void CFWL_Widget::ModifyStyles(uint32_t dwStylesAdded,
87                                uint32_t dwStylesRemoved) {
88   m_pProperties->m_dwStyles =
89       (m_pProperties->m_dwStyles & ~dwStylesRemoved) | dwStylesAdded;
90 }
91 
GetStylesEx() const92 uint32_t CFWL_Widget::GetStylesEx() const {
93   return m_pProperties->m_dwStyleExes;
94 }
GetStates() const95 uint32_t CFWL_Widget::GetStates() const {
96   return m_pProperties->m_dwStates;
97 }
98 
ModifyStylesEx(uint32_t dwStylesExAdded,uint32_t dwStylesExRemoved)99 void CFWL_Widget::ModifyStylesEx(uint32_t dwStylesExAdded,
100                                  uint32_t dwStylesExRemoved) {
101   m_pProperties->m_dwStyleExes =
102       (m_pProperties->m_dwStyleExes & ~dwStylesExRemoved) | dwStylesExAdded;
103 }
104 
NotifyHideChildWidget(CFWL_WidgetMgr * widgetMgr,CFWL_Widget * widget,CFWL_NoteDriver * noteDriver)105 static void NotifyHideChildWidget(CFWL_WidgetMgr* widgetMgr,
106                                   CFWL_Widget* widget,
107                                   CFWL_NoteDriver* noteDriver) {
108   CFWL_Widget* child = widgetMgr->GetFirstChildWidget(widget);
109   while (child) {
110     noteDriver->NotifyTargetHide(child);
111     NotifyHideChildWidget(widgetMgr, child, noteDriver);
112     child = widgetMgr->GetNextSiblingWidget(child);
113   }
114 }
115 
SetStates(uint32_t dwStates)116 void CFWL_Widget::SetStates(uint32_t dwStates) {
117   m_pProperties->m_dwStates |= dwStates;
118   if (IsVisible())
119     return;
120 
121   CFWL_NoteDriver* noteDriver = GetOwnerApp()->GetNoteDriver();
122   noteDriver->NotifyTargetHide(this);
123 
124   CFWL_WidgetMgr* widgetMgr = GetOwnerApp()->GetWidgetMgr();
125   CFWL_Widget* child = widgetMgr->GetFirstChildWidget(this);
126   while (child) {
127     noteDriver->NotifyTargetHide(child);
128     NotifyHideChildWidget(widgetMgr, child, noteDriver);
129     child = widgetMgr->GetNextSiblingWidget(child);
130   }
131   return;
132 }
133 
RemoveStates(uint32_t dwStates)134 void CFWL_Widget::RemoveStates(uint32_t dwStates) {
135   m_pProperties->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 
SetThemeProvider(IFWL_ThemeProvider * pThemeProvider)175 void CFWL_Widget::SetThemeProvider(IFWL_ThemeProvider* pThemeProvider) {
176   m_pProperties->m_pThemeProvider = pThemeProvider;
177 }
178 
IsEnabled() const179 bool CFWL_Widget::IsEnabled() const {
180   return (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled) == 0;
181 }
182 
HasBorder() const183 bool CFWL_Widget::HasBorder() const {
184   return !!(m_pProperties->m_dwStyles & FWL_WGTSTYLE_Border);
185 }
186 
IsVisible() const187 bool CFWL_Widget::IsVisible() const {
188   return !(m_pProperties->m_dwStates & FWL_WGTSTATE_Invisible);
189 }
190 
IsOverLapper() const191 bool CFWL_Widget::IsOverLapper() const {
192   return (m_pProperties->m_dwStyles & FWL_WGTSTYLE_WindowTypeMask) ==
193          FWL_WGTSTYLE_OverLapper;
194 }
195 
IsPopup() const196 bool CFWL_Widget::IsPopup() const {
197   return !!(m_pProperties->m_dwStyles & FWL_WGTSTYLE_Popup);
198 }
199 
IsChild() const200 bool CFWL_Widget::IsChild() const {
201   return !!(m_pProperties->m_dwStyles & FWL_WGTSTYLE_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_pProperties->m_rtWidget.width,
213                    m_pProperties->m_rtWidget.height);
214   if (HasBorder())
215     rtEdge.Deflate(GetCXBorderSize(), GetCYBorderSize());
216   return rtEdge;
217 }
218 
GetCXBorderSize() const219 float CFWL_Widget::GetCXBorderSize() const {
220   IFWL_ThemeProvider* theme = GetAvailableTheme();
221   return theme ? theme->GetCXBorderSize() : 0.0f;
222 }
223 
GetCYBorderSize() const224 float CFWL_Widget::GetCYBorderSize() const {
225   IFWL_ThemeProvider* theme = GetAvailableTheme();
226   return theme ? theme->GetCYBorderSize() : 0.0f;
227 }
228 
GetRelativeRect() const229 CFX_RectF CFWL_Widget::GetRelativeRect() const {
230   return CFX_RectF(0, 0, m_pProperties->m_rtWidget.width,
231                    m_pProperties->m_rtWidget.height);
232 }
233 
GetAvailableTheme() const234 IFWL_ThemeProvider* CFWL_Widget::GetAvailableTheme() const {
235   if (m_pProperties->m_pThemeProvider)
236     return m_pProperties->m_pThemeProvider.Get();
237 
238   const CFWL_Widget* pUp = this;
239   do {
240     pUp = pUp->IsPopup() ? m_pWidgetMgr->GetOwnerWidget(pUp)
241                          : m_pWidgetMgr->GetParentWidget(pUp);
242     if (pUp) {
243       IFWL_ThemeProvider* pRet = pUp->GetThemeProvider();
244       if (pRet)
245         return pRet;
246     }
247   } while (pUp);
248   return nullptr;
249 }
250 
CalcTextSize(const WideString & wsText,IFWL_ThemeProvider * pTheme,bool bMultiLine)251 CFX_SizeF CFWL_Widget::CalcTextSize(const WideString& wsText,
252                                     IFWL_ThemeProvider* pTheme,
253                                     bool bMultiLine) {
254   if (!pTheme)
255     return CFX_SizeF();
256 
257   CFWL_ThemeText calPart;
258   calPart.m_pWidget = this;
259   calPart.m_wsText = wsText;
260   if (bMultiLine)
261     calPart.m_dwTTOStyles.line_wrap_ = true;
262   else
263     calPart.m_dwTTOStyles.single_line_ = true;
264 
265   calPart.m_iTTOAlign = FDE_TextAlignment::kTopLeft;
266   float fWidth = bMultiLine ? FWL_WGT_CalcMultiLineDefWidth : FWL_WGT_CalcWidth;
267   CFX_RectF rect(0, 0, fWidth, FWL_WGT_CalcHeight);
268   pTheme->CalcTextRect(calPart, &rect);
269   return CFX_SizeF(rect.width, rect.height);
270 }
271 
CalcTextRect(const WideString & wsText,IFWL_ThemeProvider * pTheme,const FDE_TextStyle & dwTTOStyles,FDE_TextAlignment iTTOAlign,CFX_RectF * pRect)272 void CFWL_Widget::CalcTextRect(const WideString& wsText,
273                                IFWL_ThemeProvider* pTheme,
274                                const FDE_TextStyle& dwTTOStyles,
275                                FDE_TextAlignment iTTOAlign,
276                                CFX_RectF* pRect) {
277   CFWL_ThemeText calPart;
278   calPart.m_pWidget = this;
279   calPart.m_wsText = wsText;
280   calPart.m_dwTTOStyles = dwTTOStyles;
281   calPart.m_iTTOAlign = iTTOAlign;
282   pTheme->CalcTextRect(calPart, pRect);
283 }
284 
SetGrab(bool bSet)285 void CFWL_Widget::SetGrab(bool bSet) {
286   CFWL_NoteDriver* pDriver = GetOwnerApp()->GetNoteDriver();
287   pDriver->SetGrab(this, bSet);
288 }
289 
RegisterEventTarget(CFWL_Widget * pEventSource)290 void CFWL_Widget::RegisterEventTarget(CFWL_Widget* pEventSource) {
291   CFWL_NoteDriver* pNoteDriver = GetOwnerApp()->GetNoteDriver();
292   pNoteDriver->RegisterEventTarget(this, pEventSource);
293 }
294 
UnregisterEventTarget()295 void CFWL_Widget::UnregisterEventTarget() {
296   CFWL_NoteDriver* pNoteDriver = GetOwnerApp()->GetNoteDriver();
297   pNoteDriver->UnregisterEventTarget(this);
298 }
299 
DispatchEvent(CFWL_Event * pEvent)300 void CFWL_Widget::DispatchEvent(CFWL_Event* pEvent) {
301   if (m_pOuter) {
302     m_pOuter->GetDelegate()->OnProcessEvent(pEvent);
303     return;
304   }
305   CFWL_NoteDriver* pNoteDriver = GetOwnerApp()->GetNoteDriver();
306   pNoteDriver->SendEvent(pEvent);
307 }
308 
RepaintRect(const CFX_RectF & pRect)309 void CFWL_Widget::RepaintRect(const CFX_RectF& pRect) {
310   m_pWidgetMgr->RepaintWidget(this, pRect);
311 }
312 
DrawBackground(CXFA_Graphics * pGraphics,CFWL_Part iPartBk,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)313 void CFWL_Widget::DrawBackground(CXFA_Graphics* pGraphics,
314                                  CFWL_Part iPartBk,
315                                  IFWL_ThemeProvider* pTheme,
316                                  const CFX_Matrix* pMatrix) {
317   CFWL_ThemeBackground param;
318   param.m_pWidget = this;
319   param.m_iPart = iPartBk;
320   param.m_pGraphics = pGraphics;
321   if (pMatrix)
322     param.m_matrix = *pMatrix;
323   param.m_rtPart = GetRelativeRect();
324   pTheme->DrawBackground(param);
325 }
326 
DrawBorder(CXFA_Graphics * pGraphics,CFWL_Part iPartBorder,IFWL_ThemeProvider * pTheme,const CFX_Matrix & matrix)327 void CFWL_Widget::DrawBorder(CXFA_Graphics* pGraphics,
328                              CFWL_Part iPartBorder,
329                              IFWL_ThemeProvider* pTheme,
330                              const CFX_Matrix& matrix) {
331   CFWL_ThemeBackground param;
332   param.m_pWidget = this;
333   param.m_iPart = iPartBorder;
334   param.m_pGraphics = pGraphics;
335   param.m_matrix = matrix;
336   param.m_rtPart = GetRelativeRect();
337   pTheme->DrawBackground(param);
338 }
339 
NotifyDriver()340 void CFWL_Widget::NotifyDriver() {
341   CFWL_NoteDriver* pDriver = GetOwnerApp()->GetNoteDriver();
342   pDriver->NotifyTargetDestroy(this);
343 }
344 
GetOffsetFromParent(CFWL_Widget * pParent)345 CFX_SizeF CFWL_Widget::GetOffsetFromParent(CFWL_Widget* pParent) {
346   if (pParent == this)
347     return CFX_SizeF();
348 
349   CFWL_WidgetMgr* pWidgetMgr = GetOwnerApp()->GetWidgetMgr();
350   CFX_SizeF szRet(m_pProperties->m_rtWidget.left,
351                   m_pProperties->m_rtWidget.top);
352 
353   CFWL_Widget* pDstWidget = GetParent();
354   while (pDstWidget && pDstWidget != pParent) {
355     CFX_RectF rtDst = pDstWidget->GetWidgetRect();
356     szRet += CFX_SizeF(rtDst.left, rtDst.top);
357     pDstWidget = pWidgetMgr->GetParentWidget(pDstWidget);
358   }
359   return szRet;
360 }
361 
IsParent(CFWL_Widget * pParent)362 bool CFWL_Widget::IsParent(CFWL_Widget* pParent) {
363   CFWL_Widget* pUpWidget = GetParent();
364   while (pUpWidget) {
365     if (pUpWidget == pParent)
366       return true;
367     pUpWidget = pUpWidget->GetParent();
368   }
369   return false;
370 }
371 
OnProcessMessage(CFWL_Message * pMessage)372 void CFWL_Widget::OnProcessMessage(CFWL_Message* pMessage) {
373   CFWL_Widget* pWidget = pMessage->GetDstTarget();
374   if (!pWidget)
375     return;
376 
377   switch (pMessage->GetType()) {
378     case CFWL_Message::Type::Mouse: {
379       CFWL_MessageMouse* pMsgMouse = static_cast<CFWL_MessageMouse*>(pMessage);
380       CFWL_EventMouse evt(pWidget, pWidget);
381       evt.m_dwCmd = pMsgMouse->m_dwCmd;
382       pWidget->DispatchEvent(&evt);
383       break;
384     }
385     default:
386       break;
387   }
388 }
389 
OnProcessEvent(CFWL_Event * pEvent)390 void CFWL_Widget::OnProcessEvent(CFWL_Event* pEvent) {}
391 
OnDrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)392 void CFWL_Widget::OnDrawWidget(CXFA_Graphics* pGraphics,
393                                const CFX_Matrix& matrix) {}
394 
ScopedUpdateLock(CFWL_Widget * widget)395 CFWL_Widget::ScopedUpdateLock::ScopedUpdateLock(CFWL_Widget* widget)
396     : widget_(widget) {
397   widget_->LockUpdate();
398 }
399 
~ScopedUpdateLock()400 CFWL_Widget::ScopedUpdateLock::~ScopedUpdateLock() {
401   widget_->UnlockUpdate();
402 }
403