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