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_notedriver.h"
13#include "xfa/fwl/cfwl_themebackground.h"
14#include "xfa/fwl/cfwl_timerinfo.h"
15#include "xfa/fwl/cfwl_widgetproperties.h"
16#include "xfa/fwl/ifwl_themeprovider.h"
17
18namespace {
19
20const uint32_t kFrequency = 400;
21
22}  // namespace
23
24CFWL_Caret::CFWL_Caret(const CFWL_App* app,
25                       std::unique_ptr<CFWL_WidgetProperties> properties,
26                       CFWL_Widget* pOuter)
27    : CFWL_Widget(app, std::move(properties), pOuter),
28      m_pTimer(new CFWL_Caret::Timer(this)),
29      m_pTimerInfo(nullptr) {
30  SetStates(FWL_STATE_CAT_HightLight);
31}
32
33CFWL_Caret::~CFWL_Caret() {
34  if (m_pTimerInfo) {
35    m_pTimerInfo->StopTimer();
36    m_pTimerInfo = nullptr;
37  }
38}
39
40FWL_Type CFWL_Caret::GetClassID() const {
41  return FWL_Type::Caret;
42}
43
44void CFWL_Caret::Update() {}
45
46void CFWL_Caret::DrawWidget(CFX_Graphics* pGraphics,
47                            const CFX_Matrix* pMatrix) {
48  if (!pGraphics)
49    return;
50  if (!m_pProperties->m_pThemeProvider)
51    m_pProperties->m_pThemeProvider = GetAvailableTheme();
52  if (!m_pProperties->m_pThemeProvider)
53    return;
54
55  DrawCaretBK(pGraphics, m_pProperties->m_pThemeProvider, pMatrix);
56}
57
58void CFWL_Caret::ShowCaret() {
59  if (m_pTimerInfo)
60    m_pTimerInfo->StopTimer();
61  m_pTimerInfo = m_pTimer->StartTimer(kFrequency, true);
62  RemoveStates(FWL_WGTSTATE_Invisible);
63}
64
65void CFWL_Caret::HideCaret() {
66  if (m_pTimerInfo) {
67    m_pTimerInfo->StopTimer();
68    m_pTimerInfo = nullptr;
69  }
70  SetStates(FWL_WGTSTATE_Invisible);
71}
72
73void CFWL_Caret::DrawCaretBK(CFX_Graphics* pGraphics,
74                             IFWL_ThemeProvider* pTheme,
75                             const CFX_Matrix* pMatrix) {
76  if (!(m_pProperties->m_dwStates & FWL_STATE_CAT_HightLight))
77    return;
78
79  CFWL_ThemeBackground param;
80  param.m_pWidget = this;
81  param.m_pGraphics = pGraphics;
82  param.m_rtPart = CFX_RectF(0, 0, GetWidgetRect().Size());
83  param.m_iPart = CFWL_Part::Background;
84  param.m_dwStates = CFWL_PartState_HightLight;
85  if (pMatrix)
86    param.m_matrix.Concat(*pMatrix);
87  pTheme->DrawBackground(&param);
88}
89
90void CFWL_Caret::OnProcessMessage(CFWL_Message* pMessage) {}
91
92void CFWL_Caret::OnDrawWidget(CFX_Graphics* pGraphics,
93                              const CFX_Matrix* pMatrix) {
94  DrawWidget(pGraphics, pMatrix);
95}
96
97CFWL_Caret::Timer::Timer(CFWL_Caret* pCaret) : CFWL_Timer(pCaret) {}
98
99void CFWL_Caret::Timer::Run(CFWL_TimerInfo* pTimerInfo) {
100  CFWL_Caret* pCaret = static_cast<CFWL_Caret*>(m_pWidget);
101  if (!(pCaret->GetStates() & FWL_STATE_CAT_HightLight))
102    pCaret->SetStates(FWL_STATE_CAT_HightLight);
103  else
104    pCaret->RemoveStates(FWL_STATE_CAT_HightLight);
105
106  CFX_RectF rt = pCaret->GetWidgetRect();
107  pCaret->RepaintRect(CFX_RectF(0, 0, rt.width + 1, rt.height));
108}
109