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_caret.h"
8 
9 #include <utility>
10 
11 #include "third_party/base/ptr_util.h"
12 #include "xfa/fwl/cfwl_app.h"
13 #include "xfa/fwl/cfwl_notedriver.h"
14 #include "xfa/fwl/cfwl_themebackground.h"
15 #include "xfa/fwl/cfwl_widgetproperties.h"
16 #include "xfa/fwl/ifwl_themeprovider.h"
17 
18 namespace {
19 
20 const uint32_t kBlinkPeriodMs = 600;
21 
22 constexpr int kStateHighlight = (1 << 0);
23 
24 }  // namespace
25 
CFWL_Caret(const CFWL_App * app,std::unique_ptr<CFWL_WidgetProperties> properties,CFWL_Widget * pOuter)26 CFWL_Caret::CFWL_Caret(const CFWL_App* app,
27                        std::unique_ptr<CFWL_WidgetProperties> properties,
28                        CFWL_Widget* pOuter)
29     : CFWL_Widget(app, std::move(properties), pOuter) {
30   SetStates(kStateHighlight);
31 }
32 
33 CFWL_Caret::~CFWL_Caret() = default;
34 
GetClassID() const35 FWL_Type CFWL_Caret::GetClassID() const {
36   return FWL_Type::Caret;
37 }
38 
Update()39 void CFWL_Caret::Update() {}
40 
DrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)41 void CFWL_Caret::DrawWidget(CXFA_Graphics* pGraphics,
42                             const CFX_Matrix& matrix) {
43   if (!pGraphics)
44     return;
45   if (!m_pProperties->m_pThemeProvider)
46     m_pProperties->m_pThemeProvider = GetAvailableTheme();
47   if (!m_pProperties->m_pThemeProvider)
48     return;
49 
50   DrawCaretBK(pGraphics, m_pProperties->m_pThemeProvider.Get(), &matrix);
51 }
52 
ShowCaret()53 void CFWL_Caret::ShowCaret() {
54   m_pTimer = pdfium::MakeUnique<CFX_Timer>(
55       GetOwnerApp()->GetAdapterNative()->GetTimerHandler(), this,
56       kBlinkPeriodMs);
57   RemoveStates(FWL_WGTSTATE_Invisible);
58   SetStates(kStateHighlight);
59 }
60 
HideCaret()61 void CFWL_Caret::HideCaret() {
62   m_pTimer.reset();
63   SetStates(FWL_WGTSTATE_Invisible);
64 }
65 
DrawCaretBK(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)66 void CFWL_Caret::DrawCaretBK(CXFA_Graphics* pGraphics,
67                              IFWL_ThemeProvider* pTheme,
68                              const CFX_Matrix* pMatrix) {
69   if (!(m_pProperties->m_dwStates & kStateHighlight))
70     return;
71 
72   CFWL_ThemeBackground param;
73   param.m_pWidget = this;
74   param.m_pGraphics = pGraphics;
75   param.m_rtPart = CFX_RectF(0, 0, GetWidgetRect().Size());
76   param.m_iPart = CFWL_Part::Background;
77   param.m_dwStates = CFWL_PartState_HightLight;
78   if (pMatrix)
79     param.m_matrix.Concat(*pMatrix);
80   pTheme->DrawBackground(param);
81 }
82 
OnProcessMessage(CFWL_Message * pMessage)83 void CFWL_Caret::OnProcessMessage(CFWL_Message* pMessage) {}
84 
OnDrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)85 void CFWL_Caret::OnDrawWidget(CXFA_Graphics* pGraphics,
86                               const CFX_Matrix& matrix) {
87   DrawWidget(pGraphics, matrix);
88 }
89 
OnTimerFired()90 void CFWL_Caret::OnTimerFired() {
91   if (!(GetStates() & kStateHighlight))
92     SetStates(kStateHighlight);
93   else
94     RemoveStates(kStateHighlight);
95 
96   CFX_RectF rt = GetWidgetRect();
97   RepaintRect(CFX_RectF(0, 0, rt.width + 1, rt.height));
98 }
99