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