1e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov// Copyright 2014 PDFium Authors. All rights reserved.
2e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov// Use of this source code is governed by a BSD-style license that can be
3e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov// found in the LICENSE file.
4ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
5e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
74d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#include "fpdfsdk/formfiller/cffl_radiobutton.h"
8e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
94d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
104d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#include "fpdfsdk/cpdfsdk_widget.h"
114d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#include "fpdfsdk/formfiller/cffl_formfiller.h"
12d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann#include "fpdfsdk/pwl/cpwl_special_button.h"
134d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#include "public/fpdf_fwlevent.h"
14e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
154d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. MoltmannCFFL_RadioButton::CFFL_RadioButton(CPDFSDK_FormFillEnvironment* pApp,
16d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann                                   CPDFSDK_Widget* pWidget)
17ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    : CFFL_Button(pApp, pWidget) {}
18e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
19ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. MoltmannCFFL_RadioButton::~CFFL_RadioButton() {}
20e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
21d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. MoltmannCPWL_Wnd* CFFL_RadioButton::NewPDFWindow(const CPWL_Wnd::CreateParams& cp) {
22d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  auto* pWnd = new CPWL_RadioButton();
23ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  pWnd->Create(cp);
24ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  pWnd->SetCheck(m_pWidget->IsChecked());
25ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  return pWnd;
26e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
27e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
284d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFFL_RadioButton::OnKeyDown(CPDFSDK_Annot* pAnnot,
294d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                 uint32_t nKeyCode,
304d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                 uint32_t nFlags) {
31ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  switch (nKeyCode) {
32ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    case FWL_VKEY_Return:
33ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    case FWL_VKEY_Space:
344d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      return true;
35ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    default:
36ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      return CFFL_FormFiller::OnKeyDown(pAnnot, nKeyCode, nFlags);
37ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  }
38e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
39e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
404d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFFL_RadioButton::OnChar(CPDFSDK_Annot* pAnnot,
414d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                              uint32_t nChar,
424d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                              uint32_t nFlags) {
43ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  switch (nChar) {
44ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    case FWL_VKEY_Return:
45ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    case FWL_VKEY_Space: {
46ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
47ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      ASSERT(pPageView);
48ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
49d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann      CPDFSDK_Annot::ObservedPtr pObserved(m_pWidget.Get());
50d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann      if (m_pFormFillEnv->GetInteractiveFormFiller()->OnButtonUp(
51d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann              &pObserved, pPageView, nFlags) ||
52d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann          !pObserved) {
534d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann        return true;
54d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann      }
55ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
56ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
57d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann      CPWL_RadioButton* pWnd = GetRadioButton(pPageView, true);
58d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann      if (pWnd)
594d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann        pWnd->SetCheck(true);
60d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann      return CommitData(pPageView, nFlags);
61ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    }
62ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    default:
63ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
64ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  }
65e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
66e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
674d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFFL_RadioButton::OnLButtonUp(CPDFSDK_PageView* pPageView,
684d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                   CPDFSDK_Annot* pAnnot,
694d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                   uint32_t nFlags,
7033357cad1fd1321a2b38d2963e2585f27ce980a2Philip P. Moltmann                                   const CFX_PointF& point) {
71ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
72e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
73d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  if (!IsValid())
74d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann    return true;
75e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
76d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  CPWL_RadioButton* pWnd = GetRadioButton(pPageView, true);
77d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  if (pWnd)
78d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann    pWnd->SetCheck(true);
79e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
80d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  return CommitData(pPageView, nFlags);
81e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
82e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
834d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFFL_RadioButton::IsDataChanged(CPDFSDK_PageView* pPageView) {
84d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  CPWL_RadioButton* pWnd = GetRadioButton(pPageView, false);
85d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
86e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
87e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
88ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmannvoid CFFL_RadioButton::SaveData(CPDFSDK_PageView* pPageView) {
89d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  CPWL_RadioButton* pWnd = GetRadioButton(pPageView, false);
90d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  if (!pWnd)
91d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann    return;
92d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann
93d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  bool bNewChecked = pWnd->IsChecked();
94d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann
95d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  if (bNewChecked) {
96d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann    CPDF_FormField* pField = m_pWidget->GetFormField();
97d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann    for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) {
98d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann      if (CPDF_FormControl* pCtrl = pField->GetControl(i)) {
99d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann        if (pCtrl->IsChecked()) {
100d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann          break;
101ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        }
102ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      }
103ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    }
104ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  }
105d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  CPDFSDK_Widget::ObservedPtr observed_widget(m_pWidget.Get());
106d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  CFFL_RadioButton::ObservedPtr observed_this(this);
107d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann
108d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  m_pWidget->SetCheck(bNewChecked, false);
109d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  if (!observed_widget)
110d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann    return;
111d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  m_pWidget->UpdateField();
112d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  if (!observed_widget || !observed_this)
113d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann    return;
114d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  SetChangeMark();
115d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann}
116d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann
117d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. MoltmannCPWL_RadioButton* CFFL_RadioButton::GetRadioButton(CPDFSDK_PageView* pPageView,
118d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann                                                   bool bNew) {
119d904c1ec7e8d1d86ed56f0dd252435d12cd345aePhilip P. Moltmann  return static_cast<CPWL_RadioButton*>(GetPDFWindow(pPageView, bNew));
120e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
121