From 77e3fc5cebfb19aed09f920536200e017cff9a0b Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 15 Jun 2017 12:37:33 -0700 Subject: Change some CFFL classes to use early returns. Also switch C-style casts to the appropriate C++ casts, and add helper functions in some cases. Change-Id: I73f1ab36c6c89ced9d2b7b98393805142661dcac Reviewed-on: https://pdfium-review.googlesource.com/6650 Commit-Queue: dsinclair Reviewed-by: dsinclair --- fpdfsdk/formfiller/cffl_checkbox.cpp | 53 +++++----- fpdfsdk/formfiller/cffl_checkbox.h | 5 + fpdfsdk/formfiller/cffl_interactiveformfiller.cpp | 108 ++++++++++----------- fpdfsdk/formfiller/cffl_radiobutton.cpp | 61 ++++++------ fpdfsdk/formfiller/cffl_radiobutton.h | 5 + fpdfsdk/formfiller/cffl_textfield.cpp | 112 +++++++++++----------- fpdfsdk/formfiller/cffl_textfield.h | 3 + 7 files changed, 183 insertions(+), 164 deletions(-) (limited to 'fpdfsdk/formfiller') diff --git a/fpdfsdk/formfiller/cffl_checkbox.cpp b/fpdfsdk/formfiller/cffl_checkbox.cpp index a81458e92d..a89e76ac0d 100644 --- a/fpdfsdk/formfiller/cffl_checkbox.cpp +++ b/fpdfsdk/formfiller/cffl_checkbox.cpp @@ -58,7 +58,8 @@ bool CFFL_CheckBox::OnChar(CPDFSDK_Annot* pAnnot, return true; CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags); - if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, true)) + CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true); + if (pWnd) pWnd->SetCheck(!pWnd->IsChecked()); return CommitData(pPageView, nFlags); @@ -74,40 +75,46 @@ bool CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView, const CFX_PointF& point) { CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point); - if (IsValid()) { - if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, true)) { - CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot; - pWnd->SetCheck(!pWidget->IsChecked()); - } + if (!IsValid()) + return true; - return CommitData(pPageView, nFlags); + CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true); + if (pWnd) { + CPDFSDK_Widget* pWidget = static_cast(pAnnot); + pWnd->SetCheck(!pWidget->IsChecked()); } - return true; + return CommitData(pPageView, nFlags); } bool CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView) { - CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, false); + CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false); return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked(); } void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView) { - if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, false)) { - bool bNewChecked = pWnd->IsChecked(); - - if (bNewChecked) { - CPDF_FormField* pField = m_pWidget->GetFormField(); - for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) { - if (CPDF_FormControl* pCtrl = pField->GetControl(i)) { - if (pCtrl->IsChecked()) { - break; - } + CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false); + if (!pWnd) + return; + + bool bNewChecked = pWnd->IsChecked(); + if (bNewChecked) { + CPDF_FormField* pField = m_pWidget->GetFormField(); + for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) { + if (CPDF_FormControl* pCtrl = pField->GetControl(i)) { + if (pCtrl->IsChecked()) { + break; } } } - - m_pWidget->SetCheck(bNewChecked, false); - m_pWidget->UpdateField(); - SetChangeMark(); } + + m_pWidget->SetCheck(bNewChecked, false); + m_pWidget->UpdateField(); + SetChangeMark(); +} + +CPWL_CheckBox* CFFL_CheckBox::GetCheckBox(CPDFSDK_PageView* pPageView, + bool bNew) { + return static_cast(GetPDFWindow(pPageView, bNew)); } diff --git a/fpdfsdk/formfiller/cffl_checkbox.h b/fpdfsdk/formfiller/cffl_checkbox.h index dd3184f4c4..f6c7172c1c 100644 --- a/fpdfsdk/formfiller/cffl_checkbox.h +++ b/fpdfsdk/formfiller/cffl_checkbox.h @@ -9,6 +9,8 @@ #include "fpdfsdk/formfiller/cffl_formfiller.h" +class CPWL_CheckBox; + class CFFL_CheckBox : public CFFL_Button { public: CFFL_CheckBox(CPDFSDK_FormFillEnvironment* pApp, CPDFSDK_Widget* pWidget); @@ -26,6 +28,9 @@ class CFFL_CheckBox : public CFFL_Button { const CFX_PointF& point) override; bool IsDataChanged(CPDFSDK_PageView* pPageView) override; void SaveData(CPDFSDK_PageView* pPageView) override; + + private: + CPWL_CheckBox* GetCheckBox(CPDFSDK_PageView* pPageView, bool bNew); }; #endif // FPDFSDK_FORMFILLER_CFFL_CHECKBOX_H_ diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp index dca3194b7b..7647fb19ff 100644 --- a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp +++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp @@ -917,67 +917,63 @@ void CFFL_InteractiveFormFiller::OnBeforeKeyStroke( } #endif // PDF_ENABLE_XFA - if (!m_bNotifying) { - if (pData->pWidget->GetAAction(CPDF_AAction::KeyStroke).GetDict()) { - m_bNotifying = true; - int nAge = pData->pWidget->GetAppearanceAge(); - int nValueAge = pData->pWidget->GetValueAge(); + if (m_bNotifying) + return; - CPDFSDK_FormFillEnvironment* pFormFillEnv = - pData->pPageView->GetFormFillEnv(); + if (!pData->pWidget->GetAAction(CPDF_AAction::KeyStroke).GetDict()) + return; - PDFSDK_FieldAction fa; - fa.bModifier = m_pFormFillEnv->IsCTRLKeyDown(nFlag); - fa.bShift = m_pFormFillEnv->IsSHIFTKeyDown(nFlag); - fa.sChange = strChange; - fa.sChangeEx = strChangeEx; - fa.bKeyDown = bKeyDown; - fa.bWillCommit = false; - fa.bRC = true; - fa.nSelStart = nSelStart; - fa.nSelEnd = nSelEnd; - - pFormFiller->GetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa); - pFormFiller->SaveState(pData->pPageView); - - CPDFSDK_Annot::ObservedPtr pObserved(pData->pWidget); - if (pData->pWidget->OnAAction(CPDF_AAction::KeyStroke, fa, - pData->pPageView)) { - if (!pObserved || !IsValidAnnot(pData->pPageView, pData->pWidget)) { - bExit = true; - m_bNotifying = false; - return; - } + CFX_AutoRestorer restorer(&m_bNotifying); + m_bNotifying = true; + int nAge = pData->pWidget->GetAppearanceAge(); + int nValueAge = pData->pWidget->GetValueAge(); - if (nAge != pData->pWidget->GetAppearanceAge()) { - CPWL_Wnd* pWnd = pFormFiller->ResetPDFWindow( - pData->pPageView, nValueAge == pData->pWidget->GetValueAge()); - pData = reinterpret_cast(pWnd->GetAttachedData()); - bExit = true; - } + CPDFSDK_FormFillEnvironment* pFormFillEnv = + pData->pPageView->GetFormFillEnv(); - if (fa.bRC) { - pFormFiller->SetActionData(pData->pPageView, CPDF_AAction::KeyStroke, - fa); - bRC = false; - } else { - pFormFiller->RestoreState(pData->pPageView); - bRC = false; - } + PDFSDK_FieldAction fa; + fa.bModifier = m_pFormFillEnv->IsCTRLKeyDown(nFlag); + fa.bShift = m_pFormFillEnv->IsSHIFTKeyDown(nFlag); + fa.sChange = strChange; + fa.sChangeEx = strChangeEx; + fa.bKeyDown = bKeyDown; + fa.bWillCommit = false; + fa.bRC = true; + fa.nSelStart = nSelStart; + fa.nSelEnd = nSelEnd; - if (pFormFillEnv->GetFocusAnnot() != pData->pWidget) { - pFormFiller->CommitData(pData->pPageView, nFlag); - bExit = true; - } - } else { - if (!IsValidAnnot(pData->pPageView, pData->pWidget)) { - bExit = true; - m_bNotifying = false; - return; - } - } + pFormFiller->GetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa); + pFormFiller->SaveState(pData->pPageView); - m_bNotifying = false; - } + CPDFSDK_Annot::ObservedPtr pObserved(pData->pWidget); + if (!pData->pWidget->OnAAction(CPDF_AAction::KeyStroke, fa, + pData->pPageView)) { + if (!IsValidAnnot(pData->pPageView, pData->pWidget)) + bExit = true; + return; + } + + if (!pObserved || !IsValidAnnot(pData->pPageView, pData->pWidget)) { + bExit = true; + return; } + + if (nAge != pData->pWidget->GetAppearanceAge()) { + CPWL_Wnd* pWnd = pFormFiller->ResetPDFWindow( + pData->pPageView, nValueAge == pData->pWidget->GetValueAge()); + pData = reinterpret_cast(pWnd->GetAttachedData()); + bExit = true; + } + + if (fa.bRC) + pFormFiller->SetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa); + else + pFormFiller->RestoreState(pData->pPageView); + bRC = false; + + if (pFormFillEnv->GetFocusAnnot() == pData->pWidget) + return; + + pFormFiller->CommitData(pData->pPageView, nFlag); + bExit = true; } diff --git a/fpdfsdk/formfiller/cffl_radiobutton.cpp b/fpdfsdk/formfiller/cffl_radiobutton.cpp index c6ce432d0c..9ffdf494ca 100644 --- a/fpdfsdk/formfiller/cffl_radiobutton.cpp +++ b/fpdfsdk/formfiller/cffl_radiobutton.cpp @@ -57,8 +57,8 @@ bool CFFL_RadioButton::OnChar(CPDFSDK_Annot* pAnnot, return true; CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags); - if (CPWL_RadioButton* pWnd = - (CPWL_RadioButton*)GetPDFWindow(pPageView, true)) + CPWL_RadioButton* pWnd = GetRadioButton(pPageView, true); + if (pWnd) pWnd->SetCheck(true); return CommitData(pPageView, nFlags); } @@ -73,44 +73,45 @@ bool CFFL_RadioButton::OnLButtonUp(CPDFSDK_PageView* pPageView, const CFX_PointF& point) { CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point); - if (IsValid()) { - if (CPWL_RadioButton* pWnd = - (CPWL_RadioButton*)GetPDFWindow(pPageView, true)) - pWnd->SetCheck(true); + if (!IsValid()) + return true; - return CommitData(pPageView, nFlags); - } + CPWL_RadioButton* pWnd = GetRadioButton(pPageView, true); + if (pWnd) + pWnd->SetCheck(true); - return true; + return CommitData(pPageView, nFlags); } bool CFFL_RadioButton::IsDataChanged(CPDFSDK_PageView* pPageView) { - if (CPWL_RadioButton* pWnd = - (CPWL_RadioButton*)GetPDFWindow(pPageView, false)) { - return pWnd->IsChecked() != m_pWidget->IsChecked(); - } - - return false; + CPWL_RadioButton* pWnd = GetRadioButton(pPageView, false); + return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked(); } void CFFL_RadioButton::SaveData(CPDFSDK_PageView* pPageView) { - if (CPWL_RadioButton* pWnd = - (CPWL_RadioButton*)GetPDFWindow(pPageView, false)) { - bool bNewChecked = pWnd->IsChecked(); - - if (bNewChecked) { - CPDF_FormField* pField = m_pWidget->GetFormField(); - for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) { - if (CPDF_FormControl* pCtrl = pField->GetControl(i)) { - if (pCtrl->IsChecked()) { - break; - } + CPWL_RadioButton* pWnd = GetRadioButton(pPageView, false); + if (!pWnd) + return; + + bool bNewChecked = pWnd->IsChecked(); + + if (bNewChecked) { + CPDF_FormField* pField = m_pWidget->GetFormField(); + for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) { + if (CPDF_FormControl* pCtrl = pField->GetControl(i)) { + if (pCtrl->IsChecked()) { + break; } } } - - m_pWidget->SetCheck(bNewChecked, false); - m_pWidget->UpdateField(); - SetChangeMark(); } + + m_pWidget->SetCheck(bNewChecked, false); + m_pWidget->UpdateField(); + SetChangeMark(); +} + +CPWL_RadioButton* CFFL_RadioButton::GetRadioButton(CPDFSDK_PageView* pPageView, + bool bNew) { + return static_cast(GetPDFWindow(pPageView, bNew)); } diff --git a/fpdfsdk/formfiller/cffl_radiobutton.h b/fpdfsdk/formfiller/cffl_radiobutton.h index 905fc1b016..29b7e3835a 100644 --- a/fpdfsdk/formfiller/cffl_radiobutton.h +++ b/fpdfsdk/formfiller/cffl_radiobutton.h @@ -9,6 +9,8 @@ #include "fpdfsdk/formfiller/cffl_formfiller.h" +class CPWL_RadioButton; + class CFFL_RadioButton : public CFFL_Button { public: CFFL_RadioButton(CPDFSDK_FormFillEnvironment* pApp, CPDFSDK_Widget* pWidget); @@ -26,6 +28,9 @@ class CFFL_RadioButton : public CFFL_Button { const CFX_PointF& point) override; bool IsDataChanged(CPDFSDK_PageView* pPageView) override; void SaveData(CPDFSDK_PageView* pPageView) override; + + private: + CPWL_RadioButton* GetRadioButton(CPDFSDK_PageView* pPageView, bool bNew); }; #endif // FPDFSDK_FORMFILLER_CFFL_RADIOBUTTON_H_ diff --git a/fpdfsdk/formfiller/cffl_textfield.cpp b/fpdfsdk/formfiller/cffl_textfield.cpp index c34bf3e54e..da8c14d0ff 100644 --- a/fpdfsdk/formfiller/cffl_textfield.cpp +++ b/fpdfsdk/formfiller/cffl_textfield.cpp @@ -97,26 +97,28 @@ bool CFFL_TextField::OnChar(CPDFSDK_Annot* pAnnot, uint32_t nChar, uint32_t nFlags) { switch (nChar) { - case FWL_VKEY_Return: - if (!(m_pWidget->GetFieldFlags() & FIELDFLAG_MULTILINE)) { - CPDFSDK_PageView* pPageView = GetCurPageView(true); - ASSERT(pPageView); - m_bValid = !m_bValid; - m_pFormFillEnv->Invalidate(pAnnot->GetUnderlyingPage(), - pAnnot->GetRect().ToFxRect()); - - if (m_bValid) { - if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true)) - pWnd->SetFocus(); - } else { - if (CommitData(pPageView, nFlags)) { - DestroyPDFWindow(pPageView); - return true; - } - return false; - } + case FWL_VKEY_Return: { + if (m_pWidget->GetFieldFlags() & FIELDFLAG_MULTILINE) + break; + + CPDFSDK_PageView* pPageView = GetCurPageView(true); + ASSERT(pPageView); + m_bValid = !m_bValid; + m_pFormFillEnv->Invalidate(pAnnot->GetUnderlyingPage(), + pAnnot->GetRect().ToFxRect()); + + if (m_bValid) { + if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true)) + pWnd->SetFocus(); + break; } - break; + + if (!CommitData(pPageView, nFlags)) + return false; + + DestroyPDFWindow(pPageView); + return true; + } case FWL_VKEY_Escape: { CPDFSDK_PageView* pPageView = GetCurPageView(true); ASSERT(pPageView); @@ -129,22 +131,22 @@ bool CFFL_TextField::OnChar(CPDFSDK_Annot* pAnnot, } bool CFFL_TextField::IsDataChanged(CPDFSDK_PageView* pPageView) { - if (CPWL_Edit* pEdit = (CPWL_Edit*)GetPDFWindow(pPageView, false)) - return pEdit->GetText() != m_pWidget->GetValue(); - - return false; + CPWL_Edit* pEdit = GetEdit(pPageView, false); + return pEdit && pEdit->GetText() != m_pWidget->GetValue(); } void CFFL_TextField::SaveData(CPDFSDK_PageView* pPageView) { - if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) { - CFX_WideString sOldValue = m_pWidget->GetValue(); - CFX_WideString sNewValue = pWnd->GetText(); - - m_pWidget->SetValue(sNewValue, false); - m_pWidget->ResetFieldAppearance(true); - m_pWidget->UpdateField(); - SetChangeMark(); - } + CPWL_Edit* pWnd = GetEdit(pPageView, false); + if (!pWnd) + return; + + CFX_WideString sOldValue = m_pWidget->GetValue(); + CFX_WideString sNewValue = pWnd->GetText(); + + m_pWidget->SetValue(sNewValue, false); + m_pWidget->ResetFieldAppearance(true); + m_pWidget->UpdateField(); + SetChangeMark(); } void CFFL_TextField::GetActionData(CPDFSDK_PageView* pPageView, @@ -152,7 +154,7 @@ void CFFL_TextField::GetActionData(CPDFSDK_PageView* pPageView, PDFSDK_FieldAction& fa) { switch (type) { case CPDF_AAction::KeyStroke: - if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) { + if (CPWL_Edit* pWnd = GetEdit(pPageView, false)) { fa.bFieldFull = pWnd->IsTextFull(); fa.sValue = pWnd->GetText(); @@ -164,7 +166,7 @@ void CFFL_TextField::GetActionData(CPDFSDK_PageView* pPageView, } break; case CPDF_AAction::Validate: - if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) { + if (CPWL_Edit* pWnd = GetEdit(pPageView, false)) { fa.sValue = pWnd->GetText(); } break; @@ -182,7 +184,7 @@ void CFFL_TextField::SetActionData(CPDFSDK_PageView* pPageView, const PDFSDK_FieldAction& fa) { switch (type) { case CPDF_AAction::KeyStroke: - if (CPWL_Edit* pEdit = (CPWL_Edit*)GetPDFWindow(pPageView, false)) { + if (CPWL_Edit* pEdit = GetEdit(pPageView, false)) { pEdit->SetFocus(); pEdit->SetSel(fa.nSelStart, fa.nSelEnd); pEdit->ReplaceSel(fa.sChange); @@ -211,19 +213,23 @@ bool CFFL_TextField::IsActionDataChanged(CPDF_AAction::AActionType type, void CFFL_TextField::SaveState(CPDFSDK_PageView* pPageView) { ASSERT(pPageView); - if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) { - pWnd->GetSel(m_State.nStart, m_State.nEnd); - m_State.sValue = pWnd->GetText(); - } + CPWL_Edit* pWnd = GetEdit(pPageView, false); + if (!pWnd) + return; + + pWnd->GetSel(m_State.nStart, m_State.nEnd); + m_State.sValue = pWnd->GetText(); } void CFFL_TextField::RestoreState(CPDFSDK_PageView* pPageView) { ASSERT(pPageView); - if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, true)) { - pWnd->SetText(m_State.sValue); - pWnd->SetSel(m_State.nStart, m_State.nEnd); - } + CPWL_Edit* pWnd = GetEdit(pPageView, true); + if (!pWnd) + return; + + pWnd->SetText(m_State.sValue); + pWnd->SetSel(m_State.nStart, m_State.nEnd); } CPWL_Wnd* CFFL_TextField::ResetPDFWindow(CPDFSDK_PageView* pPageView, @@ -235,25 +241,17 @@ CPWL_Wnd* CFFL_TextField::ResetPDFWindow(CPDFSDK_PageView* pPageView, CPWL_Wnd* pRet = nullptr; - if (bRestoreValue) { + if (bRestoreValue) RestoreState(pPageView); - pRet = GetPDFWindow(pPageView, false); - } else { - pRet = GetPDFWindow(pPageView, true); - } - + pRet = GetPDFWindow(pPageView, !bRestoreValue); m_pWidget->UpdateField(); - return pRet; } #ifdef PDF_ENABLE_XFA bool CFFL_TextField::IsFieldFull(CPDFSDK_PageView* pPageView) { - if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) { - return pWnd->IsTextFull(); - } - - return false; + CPWL_Edit* pWnd = GetEdit(pPageView, false); + return pWnd && pWnd->IsTextFull(); } #endif // PDF_ENABLE_XFA @@ -271,3 +269,7 @@ void CFFL_TextField::OnSetFocus(CPWL_Wnd* pWnd) { auto* pBuffer = reinterpret_cast(bsUTFText.c_str()); m_pFormFillEnv->OnSetFieldInputFocus(pBuffer, nCharacters, true); } + +CPWL_Edit* CFFL_TextField::GetEdit(CPDFSDK_PageView* pPageView, bool bNew) { + return static_cast(GetPDFWindow(pPageView, bNew)); +} diff --git a/fpdfsdk/formfiller/cffl_textfield.h b/fpdfsdk/formfiller/cffl_textfield.h index cde477a392..621c1184e1 100644 --- a/fpdfsdk/formfiller/cffl_textfield.h +++ b/fpdfsdk/formfiller/cffl_textfield.h @@ -16,6 +16,7 @@ #define BF_ALIGN_RIGHT 2 class CBA_FontMap; +class CPWL_Edit; struct FFL_TextFieldState { FFL_TextFieldState() : nStart(0), nEnd(0) {} @@ -59,6 +60,8 @@ class CFFL_TextField : public CFFL_FormFiller, public IPWL_FocusHandler { #endif // PDF_ENABLE_XFA private: + CPWL_Edit* GetEdit(CPDFSDK_PageView* pPageView, bool bNew); + std::unique_ptr m_pFontMap; FFL_TextFieldState m_State; }; -- cgit v1.2.3