Cleanup caret show/hide code
This CL splits the cursor code into specific show/hide methods to make it
clearer what is happening at each point.
Review-Url: https://codereview.chromium.org/2535623002
diff --git a/xfa/fwl/core/cfwl_caret.cpp b/xfa/fwl/core/cfwl_caret.cpp
index b4ebf2c..b42945c 100644
--- a/xfa/fwl/core/cfwl_caret.cpp
+++ b/xfa/fwl/core/cfwl_caret.cpp
@@ -55,15 +55,19 @@
DrawCaretBK(pGraphics, m_pProperties->m_pThemeProvider, pMatrix);
}
-void CFWL_Caret::ShowCaret(bool bFlag) {
+void CFWL_Caret::ShowCaret() {
+ if (m_pTimerInfo)
+ m_pTimerInfo->StopTimer();
+ m_pTimerInfo = m_pTimer->StartTimer(kFrequency, true);
+ SetStates(FWL_WGTSTATE_Invisible, false);
+}
+
+void CFWL_Caret::HideCaret() {
if (m_pTimerInfo) {
m_pTimerInfo->StopTimer();
m_pTimerInfo = nullptr;
}
- if (bFlag)
- m_pTimerInfo = m_pTimer->StartTimer(kFrequency, true);
-
- SetStates(FWL_WGTSTATE_Invisible, !bFlag);
+ SetStates(FWL_WGTSTATE_Invisible, true);
}
void CFWL_Caret::DrawCaretBK(CFX_Graphics* pGraphics,
diff --git a/xfa/fwl/core/cfwl_caret.h b/xfa/fwl/core/cfwl_caret.h
index ad38f7a..ada0405 100644
--- a/xfa/fwl/core/cfwl_caret.h
+++ b/xfa/fwl/core/cfwl_caret.h
@@ -34,7 +34,8 @@
const CFX_Matrix* pMatrix) override;
void Update() override;
- void ShowCaret(bool bFlag = true);
+ void ShowCaret();
+ void HideCaret();
private:
class Timer : public CFWL_Timer {
diff --git a/xfa/fwl/core/cfwl_comboedit.cpp b/xfa/fwl/core/cfwl_comboedit.cpp
index 32e4cbb..8ee9da6 100644
--- a/xfa/fwl/core/cfwl_comboedit.cpp
+++ b/xfa/fwl/core/cfwl_comboedit.cpp
@@ -39,7 +39,7 @@
}
m_pProperties->m_dwStates &= ~FWL_WGTSTATE_Focused;
- ShowCaret(false);
+ HideCaret(nullptr);
}
void CFWL_ComboEdit::OnProcessMessage(CFWL_Message* pMessage) {
diff --git a/xfa/fwl/core/cfwl_edit.cpp b/xfa/fwl/core/cfwl_edit.cpp
index 751bf43..0844da3 100644
--- a/xfa/fwl/core/cfwl_edit.cpp
+++ b/xfa/fwl/core/cfwl_edit.cpp
@@ -81,7 +81,7 @@
CFWL_Edit::~CFWL_Edit() {
if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused)
- ShowCaret(false);
+ HideCaret(nullptr);
ClearRecord();
}
@@ -125,7 +125,7 @@
void CFWL_Edit::SetStates(uint32_t dwStates, bool bSet) {
if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Invisible) ||
(m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled)) {
- ShowCaret(false);
+ HideCaret(nullptr);
}
CFWL_Widget::SetStates(dwStates, bSet);
}
@@ -890,9 +890,10 @@
rtCaret.width = right - rtCaret.left;
}
- bool bShow =
- m_pProperties->m_dwStates & FWL_WGTSTATE_Focused && !rtCaret.IsEmpty();
- ShowCaret(bShow, &rtCaret);
+ if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused && !rtCaret.IsEmpty())
+ ShowCaret(&rtCaret);
+ else
+ HideCaret(&rtCaret);
}
CFWL_ScrollBar* CFWL_Edit::UpdateScroll() {
@@ -1166,55 +1167,65 @@
m_pHorzScrollBar.reset(sb);
}
-bool FWL_ShowCaret(CFWL_Widget* pWidget,
- bool bVisible,
- const CFX_RectF* pRtAnchor) {
- CXFA_FFWidget* pXFAWidget =
- static_cast<CXFA_FFWidget*>(pWidget->GetLayoutItem());
- if (!pXFAWidget)
- return false;
-
- IXFA_DocEnvironment* pDocEnvironment =
- pXFAWidget->GetDoc()->GetDocEnvironment();
- if (!pDocEnvironment)
- return false;
-
- if (bVisible) {
- CFX_Matrix mt;
- pXFAWidget->GetRotateMatrix(mt);
- CFX_RectF rt(*pRtAnchor);
- mt.TransformRect(rt);
- pDocEnvironment->DisplayCaret(pXFAWidget, bVisible, &rt);
- return true;
- }
-
- pDocEnvironment->DisplayCaret(pXFAWidget, bVisible, pRtAnchor);
- return true;
-}
-
-void CFWL_Edit::ShowCaret(bool bVisible, CFX_RectF* pRect) {
+void CFWL_Edit::ShowCaret(CFX_RectF* pRect) {
if (m_pCaret) {
- m_pCaret->ShowCaret(bVisible);
- if (bVisible && !pRect->IsEmpty())
+ m_pCaret->ShowCaret();
+ if (!pRect->IsEmpty())
m_pCaret->SetWidgetRect(*pRect);
Repaint(&m_rtEngine);
return;
}
CFWL_Widget* pOuter = this;
- if (bVisible) {
- pRect->Offset(m_pProperties->m_rtWidget.left,
- m_pProperties->m_rtWidget.top);
- }
+ pRect->Offset(m_pProperties->m_rtWidget.left, m_pProperties->m_rtWidget.top);
while (pOuter->GetOuter()) {
pOuter = pOuter->GetOuter();
- if (bVisible) {
- CFX_RectF rtOuter;
- pOuter->GetWidgetRect(rtOuter);
- pRect->Offset(rtOuter.left, rtOuter.top);
- }
+
+ CFX_RectF rtOuter;
+ pOuter->GetWidgetRect(rtOuter);
+ pRect->Offset(rtOuter.left, rtOuter.top);
}
- FWL_ShowCaret(pOuter, bVisible, pRect);
+
+ CXFA_FFWidget* pXFAWidget =
+ static_cast<CXFA_FFWidget*>(pOuter->GetLayoutItem());
+ if (!pXFAWidget)
+ return;
+
+ IXFA_DocEnvironment* pDocEnvironment =
+ pXFAWidget->GetDoc()->GetDocEnvironment();
+ if (!pDocEnvironment)
+ return;
+
+ CFX_Matrix mt;
+ pXFAWidget->GetRotateMatrix(mt);
+
+ CFX_RectF rt(*pRect);
+ mt.TransformRect(rt);
+ pDocEnvironment->DisplayCaret(pXFAWidget, true, &rt);
+}
+
+void CFWL_Edit::HideCaret(CFX_RectF* pRect) {
+ if (m_pCaret) {
+ m_pCaret->HideCaret();
+ Repaint(&m_rtEngine);
+ return;
+ }
+
+ CFWL_Widget* pOuter = this;
+ while (pOuter->GetOuter())
+ pOuter = pOuter->GetOuter();
+
+ CXFA_FFWidget* pXFAWidget =
+ static_cast<CXFA_FFWidget*>(pOuter->GetLayoutItem());
+ if (!pXFAWidget)
+ return;
+
+ IXFA_DocEnvironment* pDocEnvironment =
+ pXFAWidget->GetDoc()->GetDocEnvironment();
+ if (!pDocEnvironment)
+ return;
+
+ pDocEnvironment->DisplayCaret(pXFAWidget, false, pRect);
}
bool CFWL_Edit::ValidateNumberChar(FX_WCHAR cNum) {
@@ -1250,8 +1261,8 @@
void CFWL_Edit::InitCaret() {
if (!m_pCaret) {
if ((m_pProperties->m_dwStyleExes & FWL_STYLEEXT_EDT_InnerCaret)) {
- m_pCaret.reset(new CFWL_Caret(
- m_pOwnerApp, pdfium::MakeUnique<CFWL_WidgetProperties>(), this));
+ m_pCaret = pdfium::MakeUnique<CFWL_Caret>(
+ m_pOwnerApp, pdfium::MakeUnique<CFWL_WidgetProperties>(), this);
m_pCaret->SetParent(this);
m_pCaret->SetStates(m_pProperties->m_dwStates);
}
@@ -1371,7 +1382,7 @@
UpdateCaret();
} else if (m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) {
m_pProperties->m_dwStates &= ~FWL_WGTSTATE_Focused;
- ShowCaret(false);
+ HideCaret(nullptr);
if ((dwStyleEx & FWL_STYLEEXT_EDT_NoHideSel) == 0) {
int32_t nSel = CountSelRanges();
if (nSel > 0) {
diff --git a/xfa/fwl/core/cfwl_edit.h b/xfa/fwl/core/cfwl_edit.h
index d560bc5..b8b3b14 100644
--- a/xfa/fwl/core/cfwl_edit.h
+++ b/xfa/fwl/core/cfwl_edit.h
@@ -119,7 +119,8 @@
void SetScrollOffset(FX_FLOAT fScrollOffset);
protected:
- void ShowCaret(bool bVisible, CFX_RectF* pRect = nullptr);
+ void ShowCaret(CFX_RectF* pRect);
+ void HideCaret(CFX_RectF* pRect);
const CFX_RectF& GetRTClient() const { return m_rtClient; }
CFDE_TxtEdtEngine* GetTxtEdtEngine() { return &m_EdtEngine; }