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/src/foxitlib.h"
8#include "xfa/src/fwl/src/core/include/fwl_targetimp.h"
9#include "xfa/src/fwl/src/core/include/fwl_noteimp.h"
10#include "xfa/src/fwl/src/core/include/fwl_widgetimp.h"
11#include "xfa/src/fwl/src/basewidget/include/fwl_caretimp.h"
12
13// static
14IFWL_Caret* IFWL_Caret::Create(const CFWL_WidgetImpProperties& properties,
15                               IFWL_Widget* pOuter) {
16  IFWL_Caret* pCaret = new IFWL_Caret;
17  CFWL_CaretImp* pCaretImpl = new CFWL_CaretImp(properties, pOuter);
18  pCaret->SetImpl(pCaretImpl);
19  pCaretImpl->SetInterface(pCaret);
20  return pCaret;
21}
22IFWL_Caret::IFWL_Caret() {}
23FWL_ERR IFWL_Caret::ShowCaret(FX_BOOL bFlag) {
24  return static_cast<CFWL_CaretImp*>(GetImpl())->ShowCaret(bFlag);
25}
26FWL_ERR IFWL_Caret::GetFrequency(FX_DWORD& elapse) {
27  return static_cast<CFWL_CaretImp*>(GetImpl())->GetFrequency(elapse);
28}
29FWL_ERR IFWL_Caret::SetFrequency(FX_DWORD elapse) {
30  return static_cast<CFWL_CaretImp*>(GetImpl())->SetFrequency(elapse);
31}
32FWL_ERR IFWL_Caret::SetColor(CFX_Color crFill) {
33  return static_cast<CFWL_CaretImp*>(GetImpl())->SetColor(crFill);
34}
35
36CFWL_CaretImp::CFWL_CaretImp(const CFWL_WidgetImpProperties& properties,
37                             IFWL_Widget* pOuter)
38    : CFWL_WidgetImp(properties, pOuter),
39      m_hTimer(nullptr),
40      m_dwElapse(400),
41      m_bSetColor(FALSE) {
42  m_pTimer = new CFWL_CaretTimer(this);
43  SetStates(FWL_STATE_CAT_HightLight);
44}
45CFWL_CaretImp::~CFWL_CaretImp() {
46  if (m_pTimer) {
47    delete m_pTimer;
48    m_pTimer = NULL;
49  }
50}
51FWL_ERR CFWL_CaretImp::GetClassName(CFX_WideString& wsClass) const {
52  wsClass = FWL_CLASS_Caret;
53  return FWL_ERR_Succeeded;
54}
55FX_DWORD CFWL_CaretImp::GetClassID() const {
56  return FWL_CLASSHASH_Caret;
57}
58FWL_ERR CFWL_CaretImp::Initialize() {
59  if (CFWL_WidgetImp::Initialize() != FWL_ERR_Succeeded)
60    return FWL_ERR_Indefinite;
61  m_pDelegate = new CFWL_CaretImpDelegate(this);
62  return FWL_ERR_Succeeded;
63}
64FWL_ERR CFWL_CaretImp::Finalize() {
65  if (m_hTimer) {
66    FWL_StopTimer(m_hTimer);
67    m_hTimer = NULL;
68  }
69  delete m_pDelegate;
70  m_pDelegate = nullptr;
71  return CFWL_WidgetImp::Finalize();
72}
73FWL_ERR CFWL_CaretImp::DrawWidget(CFX_Graphics* pGraphics,
74                                  const CFX_Matrix* pMatrix) {
75  if (!pGraphics)
76    return FWL_ERR_Indefinite;
77  if (!m_pProperties->m_pThemeProvider)
78    m_pProperties->m_pThemeProvider = GetAvailableTheme();
79  if (!m_pProperties->m_pThemeProvider)
80    return FWL_ERR_Indefinite;
81  DrawCaretBK(pGraphics, m_pProperties->m_pThemeProvider, pMatrix);
82  return FWL_ERR_Succeeded;
83}
84FWL_ERR CFWL_CaretImp::ShowCaret(FX_BOOL bFlag) {
85  if (m_hTimer) {
86    FWL_StopTimer(m_hTimer);
87    m_hTimer = NULL;
88  }
89  if (bFlag) {
90    m_hTimer = FWL_StartTimer(m_pTimer, m_dwElapse);
91  }
92  return SetStates(FWL_WGTSTATE_Invisible, !bFlag);
93}
94FWL_ERR CFWL_CaretImp::GetFrequency(FX_DWORD& elapse) {
95  elapse = m_dwElapse;
96  return FWL_ERR_Succeeded;
97}
98FWL_ERR CFWL_CaretImp::SetFrequency(FX_DWORD elapse) {
99  m_dwElapse = elapse;
100  return FWL_ERR_Succeeded;
101}
102FWL_ERR CFWL_CaretImp::SetColor(CFX_Color crFill) {
103  m_bSetColor = TRUE;
104  m_crFill = crFill;
105  return FWL_ERR_Succeeded;
106}
107FX_BOOL CFWL_CaretImp::DrawCaretBK(CFX_Graphics* pGraphics,
108                                   IFWL_ThemeProvider* pTheme,
109                                   const CFX_Matrix* pMatrix) {
110  CFX_RectF rect;
111  GetWidgetRect(rect);
112  rect.Set(0, 0, rect.width, rect.height);
113  CFWL_ThemeBackground param;
114  param.m_pWidget = m_pInterface;
115  param.m_pGraphics = pGraphics;
116  param.m_rtPart = rect;
117  if (m_bSetColor) {
118    param.m_pData = &m_crFill;
119  }
120  if (!(m_pProperties->m_dwStates & FWL_STATE_CAT_HightLight)) {
121    return FWL_ERR_Succeeded;
122  }
123  param.m_iPart = FWL_PART_CAT_Background;
124  param.m_dwStates = FWL_PARTSTATE_CAT_HightLight;
125  if (pMatrix) {
126    param.m_matrix.Concat(*pMatrix);
127  }
128  pTheme->DrawBackground(&param);
129  return FWL_ERR_Succeeded;
130}
131CFWL_CaretImp::CFWL_CaretTimer::CFWL_CaretTimer(CFWL_CaretImp* m_pCaret) {
132  this->m_pCaret = m_pCaret;
133}
134int32_t CFWL_CaretImp::CFWL_CaretTimer::Run(FWL_HTIMER hTimer) {
135  if (m_pCaret->GetStates() & FWL_STATE_CAT_HightLight) {
136    m_pCaret->SetStates(FWL_STATE_CAT_HightLight, FALSE);
137  } else {
138    m_pCaret->SetStates(FWL_STATE_CAT_HightLight);
139  }
140  CFX_RectF rt;
141  m_pCaret->GetWidgetRect(rt);
142  rt.Set(0, 0, rt.width + 1, rt.height);
143  m_pCaret->Repaint(&rt);
144  return 1;
145}
146CFWL_CaretImpDelegate::CFWL_CaretImpDelegate(CFWL_CaretImp* pOwner)
147    : m_pOwner(pOwner) {}
148int32_t CFWL_CaretImpDelegate::OnProcessMessage(CFWL_Message* pMessage) {
149  return 1;
150}
151FWL_ERR CFWL_CaretImpDelegate::OnDrawWidget(CFX_Graphics* pGraphics,
152                                            const CFX_Matrix* pMatrix) {
153  return m_pOwner->DrawWidget(pGraphics, pMatrix);
154}
155