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 "fpdfsdk/formfiller/cffl_radiobutton.h"
8
9 #include <utility>
10
11 #include "core/fpdfdoc/cpdf_formcontrol.h"
12 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
13 #include "fpdfsdk/cpdfsdk_widget.h"
14 #include "fpdfsdk/formfiller/cffl_formfiller.h"
15 #include "fpdfsdk/pwl/cpwl_special_button.h"
16 #include "public/fpdf_fwlevent.h"
17
CFFL_RadioButton(CPDFSDK_FormFillEnvironment * pApp,CPDFSDK_Widget * pWidget)18 CFFL_RadioButton::CFFL_RadioButton(CPDFSDK_FormFillEnvironment* pApp,
19 CPDFSDK_Widget* pWidget)
20 : CFFL_Button(pApp, pWidget) {}
21
~CFFL_RadioButton()22 CFFL_RadioButton::~CFFL_RadioButton() {}
23
NewPWLWindow(const CPWL_Wnd::CreateParams & cp,std::unique_ptr<IPWL_SystemHandler::PerWindowData> pAttachedData)24 std::unique_ptr<CPWL_Wnd> CFFL_RadioButton::NewPWLWindow(
25 const CPWL_Wnd::CreateParams& cp,
26 std::unique_ptr<IPWL_SystemHandler::PerWindowData> pAttachedData) {
27 auto pWnd =
28 pdfium::MakeUnique<CPWL_RadioButton>(cp, std::move(pAttachedData));
29 pWnd->Realize();
30 pWnd->SetCheck(m_pWidget->IsChecked());
31 return std::move(pWnd);
32 }
33
OnKeyDown(uint32_t nKeyCode,uint32_t nFlags)34 bool CFFL_RadioButton::OnKeyDown(uint32_t nKeyCode, uint32_t nFlags) {
35 switch (nKeyCode) {
36 case FWL_VKEY_Return:
37 case FWL_VKEY_Space:
38 return true;
39 default:
40 return CFFL_FormFiller::OnKeyDown(nKeyCode, nFlags);
41 }
42 }
43
OnChar(CPDFSDK_Annot * pAnnot,uint32_t nChar,uint32_t nFlags)44 bool CFFL_RadioButton::OnChar(CPDFSDK_Annot* pAnnot,
45 uint32_t nChar,
46 uint32_t nFlags) {
47 switch (nChar) {
48 case FWL_VKEY_Return:
49 case FWL_VKEY_Space: {
50 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
51 ASSERT(pPageView);
52
53 ObservedPtr<CPDFSDK_Annot> pObserved(m_pWidget.Get());
54 if (m_pFormFillEnv->GetInteractiveFormFiller()->OnButtonUp(
55 &pObserved, pPageView, nFlags) ||
56 !pObserved) {
57 return true;
58 }
59
60 CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
61 CPWL_RadioButton* pWnd = GetRadioButton(pPageView, true);
62 if (pWnd)
63 pWnd->SetCheck(true);
64 return CommitData(pPageView, nFlags);
65 }
66 default:
67 return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
68 }
69 }
70
OnLButtonUp(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,uint32_t nFlags,const CFX_PointF & point)71 bool CFFL_RadioButton::OnLButtonUp(CPDFSDK_PageView* pPageView,
72 CPDFSDK_Annot* pAnnot,
73 uint32_t nFlags,
74 const CFX_PointF& point) {
75 CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
76
77 if (!IsValid())
78 return true;
79
80 CPWL_RadioButton* pWnd = GetRadioButton(pPageView, true);
81 if (pWnd)
82 pWnd->SetCheck(true);
83
84 return CommitData(pPageView, nFlags);
85 }
86
IsDataChanged(CPDFSDK_PageView * pPageView)87 bool CFFL_RadioButton::IsDataChanged(CPDFSDK_PageView* pPageView) {
88 CPWL_RadioButton* pWnd = GetRadioButton(pPageView, false);
89 return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
90 }
91
SaveData(CPDFSDK_PageView * pPageView)92 void CFFL_RadioButton::SaveData(CPDFSDK_PageView* pPageView) {
93 CPWL_RadioButton* pWnd = GetRadioButton(pPageView, false);
94 if (!pWnd)
95 return;
96
97 bool bNewChecked = pWnd->IsChecked();
98 if (bNewChecked) {
99 CPDF_FormField* pField = m_pWidget->GetFormField();
100 for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) {
101 if (CPDF_FormControl* pCtrl = pField->GetControl(i)) {
102 if (pCtrl->IsChecked())
103 break;
104 }
105 }
106 }
107 ObservedPtr<CPDFSDK_Widget> observed_widget(m_pWidget.Get());
108 ObservedPtr<CFFL_RadioButton> observed_this(this);
109 m_pWidget->SetCheck(bNewChecked, NotificationOption::kDoNotNotify);
110 if (!observed_widget)
111 return;
112
113 m_pWidget->UpdateField();
114 if (!observed_widget || !observed_this)
115 return;
116
117 SetChangeMark();
118 }
119
GetRadioButton(CPDFSDK_PageView * pPageView,bool bNew)120 CPWL_RadioButton* CFFL_RadioButton::GetRadioButton(CPDFSDK_PageView* pPageView,
121 bool bNew) {
122 return static_cast<CPWL_RadioButton*>(GetPWLWindow(pPageView, bNew));
123 }
124