cffl_radiobutton.cpp revision 4d3acf4ec42bf6e838f9060103aff98fbf170794
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"
124d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#include "fpdfsdk/pdfwindow/PWL_SpecialButton.h"
134d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann#include "public/fpdf_fwlevent.h"
14e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
154d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. MoltmannCFFL_RadioButton::CFFL_RadioButton(CPDFSDK_FormFillEnvironment* pApp,
16ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                                   CPDFSDK_Annot* pWidget)
17ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    : CFFL_Button(pApp, pWidget) {}
18e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
19ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. MoltmannCFFL_RadioButton::~CFFL_RadioButton() {}
20e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
21ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. MoltmannCPWL_Wnd* CFFL_RadioButton::NewPDFWindow(const PWL_CREATEPARAM& cp,
22ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                                         CPDFSDK_PageView* pPageView) {
23ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  CPWL_RadioButton* pWnd = new CPWL_RadioButton();
24ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  pWnd->Create(cp);
25e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
26ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  pWnd->SetCheck(m_pWidget->IsChecked());
27ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
28ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  return pWnd;
29e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
30e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
314d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFFL_RadioButton::OnKeyDown(CPDFSDK_Annot* pAnnot,
324d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                 uint32_t nKeyCode,
334d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                 uint32_t nFlags) {
34ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  switch (nKeyCode) {
35ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    case FWL_VKEY_Return:
36ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    case FWL_VKEY_Space:
374d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      return true;
38ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    default:
39ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      return CFFL_FormFiller::OnKeyDown(pAnnot, nKeyCode, nFlags);
40ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  }
41e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
42e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
434d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFFL_RadioButton::OnChar(CPDFSDK_Annot* pAnnot,
444d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                              uint32_t nChar,
454d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                              uint32_t nFlags) {
46ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  switch (nChar) {
47ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    case FWL_VKEY_Return:
48ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    case FWL_VKEY_Space: {
49ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
50ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      ASSERT(pPageView);
51ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
524d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      bool bReset = false;
534d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      bool bExit = false;
544d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      CPDFSDK_Annot::ObservedPtr pObserved(m_pWidget);
554d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      m_pFormFillEnv->GetInteractiveFormFiller()->OnButtonUp(
564d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          &pObserved, pPageView, bReset, bExit, nFlags);
574d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      if (!pObserved || bReset || bExit)
584d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann        return true;
59ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
60ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
61ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      if (CPWL_RadioButton* pWnd =
624d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann              (CPWL_RadioButton*)GetPDFWindow(pPageView, true))
634d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann        pWnd->SetCheck(true);
64ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      CommitData(pPageView, nFlags);
654d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      return true;
66ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    }
67ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    default:
68ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
69ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  }
70e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
71e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
724d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFFL_RadioButton::OnLButtonUp(CPDFSDK_PageView* pPageView,
734d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                   CPDFSDK_Annot* pAnnot,
744d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                   uint32_t nFlags,
754d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann                                   const CFX_FloatPoint& point) {
76ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
77e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
78ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  if (IsValid()) {
79ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    if (CPWL_RadioButton* pWnd =
804d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann            (CPWL_RadioButton*)GetPDFWindow(pPageView, true))
814d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      pWnd->SetCheck(true);
82e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
83ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    if (!CommitData(pPageView, nFlags))
844d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann      return false;
85ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  }
86e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
874d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return true;
88e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
89e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
904d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmannbool CFFL_RadioButton::IsDataChanged(CPDFSDK_PageView* pPageView) {
91ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  if (CPWL_RadioButton* pWnd =
924d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          (CPWL_RadioButton*)GetPDFWindow(pPageView, false)) {
93ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    return pWnd->IsChecked() != m_pWidget->IsChecked();
94ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  }
95e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
964d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann  return false;
97e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
98e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov
99ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmannvoid CFFL_RadioButton::SaveData(CPDFSDK_PageView* pPageView) {
100ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  if (CPWL_RadioButton* pWnd =
1014d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann          (CPWL_RadioButton*)GetPDFWindow(pPageView, false)) {
1024d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    bool bNewChecked = pWnd->IsChecked();
103ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
104ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    if (bNewChecked) {
105ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      CPDF_FormField* pField = m_pWidget->GetFormField();
106ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) {
107ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        if (CPDF_FormControl* pCtrl = pField->GetControl(i)) {
108ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann          if (pCtrl->IsChecked()) {
109ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann            break;
110ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann          }
111ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        }
112ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann      }
113ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    }
114ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
1154d3acf4ec42bf6e838f9060103aff98fbf170794Philip P. Moltmann    m_pWidget->SetCheck(bNewChecked, false);
116ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    m_pWidget->UpdateField();
117ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    SetChangeMark();
118ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann  }
119e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov}
120