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