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_caret.h"
8
9 #include <utility>
10
11 #include "xfa/fwl/cfwl_app.h"
12 #include "xfa/fwl/cfwl_notedriver.h"
13 #include "xfa/fwl/cfwl_themebackground.h"
14 #include "xfa/fwl/ifwl_themeprovider.h"
15
16 namespace pdfium {
17
18 namespace {
19
20 const uint32_t kBlinkPeriodMs = 600;
21
22 constexpr int kStateHighlight = (1 << 0);
23
24 } // namespace
25
CFWL_Caret(CFWL_App * app,const Properties & properties,CFWL_Widget * pOuter)26 CFWL_Caret::CFWL_Caret(CFWL_App* app,
27 const Properties& properties,
28 CFWL_Widget* pOuter)
29 : CFWL_Widget(app, 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(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & matrix)41 void CFWL_Caret::DrawWidget(CFGAS_GEGraphics* pGraphics,
42 const CFX_Matrix& matrix) {
43 if (!pGraphics)
44 return;
45
46 DrawCaretBK(pGraphics, matrix);
47 }
48
ShowCaret()49 void CFWL_Caret::ShowCaret() {
50 m_pTimer = std::make_unique<CFX_Timer>(GetFWLApp()->GetTimerHandler(), this,
51 kBlinkPeriodMs);
52 RemoveStates(FWL_STATE_WGT_Invisible);
53 SetStates(kStateHighlight);
54 }
55
HideCaret()56 void CFWL_Caret::HideCaret() {
57 m_pTimer.reset();
58 SetStates(FWL_STATE_WGT_Invisible);
59 }
60
DrawCaretBK(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & mtMatrix)61 void CFWL_Caret::DrawCaretBK(CFGAS_GEGraphics* pGraphics,
62 const CFX_Matrix& mtMatrix) {
63 if (!(m_Properties.m_dwStates & kStateHighlight))
64 return;
65
66 CFWL_ThemeBackground param(CFWL_ThemePart::Part::kBackground, this,
67 pGraphics);
68 param.m_PartRect = CFX_RectF(0, 0, GetWidgetRect().Size());
69 param.m_dwStates = CFWL_PartState::kHightLight;
70 param.m_matrix = mtMatrix;
71 GetThemeProvider()->DrawBackground(param);
72 }
73
OnProcessMessage(CFWL_Message * pMessage)74 void CFWL_Caret::OnProcessMessage(CFWL_Message* pMessage) {}
75
OnDrawWidget(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & matrix)76 void CFWL_Caret::OnDrawWidget(CFGAS_GEGraphics* pGraphics,
77 const CFX_Matrix& matrix) {
78 DrawWidget(pGraphics, matrix);
79 }
80
OnTimerFired()81 void CFWL_Caret::OnTimerFired() {
82 if (!(GetStates() & kStateHighlight))
83 SetStates(kStateHighlight);
84 else
85 RemoveStates(kStateHighlight);
86
87 CFX_RectF rt = GetWidgetRect();
88 RepaintRect(CFX_RectF(0, 0, rt.width + 1, rt.height));
89 }
90
91 } // namespace pdfium
92