xref: /aosp_15_r20/external/pdfium/xfa/fwl/cfwl_caret.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 {
17 
18 const uint32_t kBlinkPeriodMs = 600;
19 
20 constexpr int kStateHighlight = (1 << 0);
21 
22 }  // namespace
23 
CFWL_Caret(CFWL_App * app,const Properties & properties,CFWL_Widget * pOuter)24 CFWL_Caret::CFWL_Caret(CFWL_App* app,
25                        const Properties& properties,
26                        CFWL_Widget* pOuter)
27     : CFWL_Widget(app, properties, pOuter) {
28   SetStates(kStateHighlight);
29 }
30 
31 CFWL_Caret::~CFWL_Caret() = default;
32 
GetClassID() const33 FWL_Type CFWL_Caret::GetClassID() const {
34   return FWL_Type::Caret;
35 }
36 
Update()37 void CFWL_Caret::Update() {}
38 
DrawWidget(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & matrix)39 void CFWL_Caret::DrawWidget(CFGAS_GEGraphics* pGraphics,
40                             const CFX_Matrix& matrix) {
41   if (!pGraphics)
42     return;
43 
44   DrawCaretBK(pGraphics, matrix);
45 }
46 
ShowCaret()47 void CFWL_Caret::ShowCaret() {
48   m_pTimer = std::make_unique<CFX_Timer>(GetFWLApp()->GetTimerHandler(), this,
49                                          kBlinkPeriodMs);
50   RemoveStates(FWL_STATE_WGT_Invisible);
51   SetStates(kStateHighlight);
52 }
53 
HideCaret()54 void CFWL_Caret::HideCaret() {
55   m_pTimer.reset();
56   SetStates(FWL_STATE_WGT_Invisible);
57 }
58 
DrawCaretBK(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & mtMatrix)59 void CFWL_Caret::DrawCaretBK(CFGAS_GEGraphics* pGraphics,
60                              const CFX_Matrix& mtMatrix) {
61   if (!(m_Properties.m_dwStates & kStateHighlight))
62     return;
63 
64   CFWL_ThemeBackground param(CFWL_ThemePart::Part::kBackground, this,
65                              pGraphics);
66   param.m_PartRect = CFX_RectF(0, 0, GetWidgetRect().Size());
67   param.m_dwStates = CFWL_PartState::kHightLight;
68   param.m_matrix = mtMatrix;
69   GetThemeProvider()->DrawBackground(param);
70 }
71 
OnProcessMessage(CFWL_Message * pMessage)72 void CFWL_Caret::OnProcessMessage(CFWL_Message* pMessage) {}
73 
OnDrawWidget(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & matrix)74 void CFWL_Caret::OnDrawWidget(CFGAS_GEGraphics* pGraphics,
75                               const CFX_Matrix& matrix) {
76   DrawWidget(pGraphics, matrix);
77 }
78 
OnTimerFired()79 void CFWL_Caret::OnTimerFired() {
80   if (!(GetStates() & kStateHighlight))
81     SetStates(kStateHighlight);
82   else
83     RemoveStates(kStateHighlight);
84 
85   CFX_RectF rt = GetWidgetRect();
86   RepaintRect(CFX_RectF(0, 0, rt.width + 1, rt.height));
87 }
88