Initial commit.
[pdfium.git] / fpdfsdk / src / formfiller / FFL_CheckBox.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4  \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "../../include/formfiller/FormFiller.h"\r
8 #include "../../include/formfiller/FFL_FormFiller.h"\r
9 #include "../../include/formfiller/FFL_CheckBox.h"\r
10 \r
11 \r
12 /* ------------------------------- CFFL_CheckBox ------------------------------- */\r
13 \r
14 CFFL_CheckBox::CFFL_CheckBox(CPDFDoc_Environment* pApp, CPDFSDK_Widget* pWidget) :\r
15         CFFL_Button(pApp, pWidget)\r
16 {\r
17 }\r
18 \r
19 CFFL_CheckBox::~CFFL_CheckBox()\r
20 {\r
21 }\r
22 \r
23 CPWL_Wnd* CFFL_CheckBox::NewPDFWindow(const PWL_CREATEPARAM& cp, CPDFSDK_PageView* pPageView)\r
24 {\r
25         CPWL_CheckBox* pWnd = new CPWL_CheckBox();\r
26         pWnd->Create(cp);\r
27 \r
28         ASSERT(m_pWidget != NULL);\r
29         pWnd->SetCheck(m_pWidget->IsChecked());\r
30         \r
31         return pWnd;\r
32 }\r
33 \r
34 FX_BOOL CFFL_CheckBox::OnKeyDown(CPDFSDK_Annot* pAnnot, FX_UINT nKeyCode, FX_UINT nFlags)\r
35 {\r
36         switch (nKeyCode)\r
37         {\r
38         case FWL_VKEY_Return:\r
39         case FWL_VKEY_Space:\r
40                 return TRUE;\r
41         default:\r
42                 return CFFL_FormFiller::OnKeyDown(pAnnot, nKeyCode, nFlags);\r
43         }\r
44 }\r
45 FX_BOOL CFFL_CheckBox::OnChar(CPDFSDK_Annot* pAnnot, FX_UINT nChar, FX_UINT nFlags)\r
46 {\r
47         switch (nChar)\r
48         {\r
49         case FWL_VKEY_Return:   \r
50         case FWL_VKEY_Space:\r
51                 {\r
52                         CFFL_IFormFiller* pIFormFiller = m_pApp->GetIFormFiller();\r
53                         ASSERT(pIFormFiller != NULL);\r
54 \r
55                         CPDFSDK_PageView* pPageView = pAnnot->GetPageView();\r
56                         ASSERT(pPageView != NULL);\r
57 \r
58                         FX_BOOL bReset = FALSE;\r
59                         FX_BOOL bExit = FALSE;\r
60 \r
61                         pIFormFiller->OnButtonUp(m_pWidget, pPageView, bReset, bExit,nFlags);\r
62 \r
63                         if (bReset) return TRUE;\r
64                         if (bExit) return TRUE;\r
65 \r
66                         CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);\r
67 \r
68                         if (CPWL_CheckBox * pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, TRUE))\r
69                                 pWnd->SetCheck(!pWnd->IsChecked());\r
70 \r
71                         CommitData(pPageView,nFlags);\r
72                         return TRUE;\r
73                 }\r
74         default:\r
75                 return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);\r
76         }\r
77 }\r
78 \r
79 FX_BOOL CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)\r
80 {\r
81         CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);\r
82 \r
83         if (IsValid())\r
84         {\r
85                 if (CPWL_CheckBox * pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, TRUE))\r
86                 {\r
87                         CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;\r
88                         pWnd->SetCheck(!pWidget->IsChecked());\r
89                 //      pWnd->SetCheck(!pWnd->IsChecked());\r
90                 }\r
91 \r
92                 if (!CommitData(pPageView, nFlags)) return FALSE;\r
93         }\r
94 \r
95         return TRUE;\r
96 }\r
97 \r
98 FX_BOOL CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView)\r
99 {\r
100 \r
101         ASSERT(m_pWidget != NULL);\r
102 \r
103         if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, FALSE))\r
104         {\r
105                 return pWnd->IsChecked() != m_pWidget->IsChecked();\r
106         }\r
107 \r
108         return FALSE;\r
109 }\r
110 \r
111 void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView)\r
112 {\r
113 \r
114         ASSERT(m_pWidget != NULL);\r
115 \r
116         if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, FALSE))\r
117         {\r
118                 \r
119                 FX_BOOL bNewChecked = pWnd->IsChecked();\r
120                 \r
121 \r
122                 if (bNewChecked)\r
123                 {\r
124                         CPDF_FormField* pField = m_pWidget->GetFormField();\r
125                         ASSERT(pField != NULL);\r
126 \r
127                         for (FX_INT32 i=0,sz=pField->CountControls(); i<sz; i++)\r
128                         {\r
129                                 if (CPDF_FormControl* pCtrl = pField->GetControl(i))\r
130                                 {\r
131                                         if (pCtrl->IsChecked())\r
132                                         {\r
133                                                 break;\r
134                                         }\r
135                                 }\r
136                         }\r
137                 }\r
138 \r
139                 m_pWidget->SetCheck(bNewChecked, FALSE);\r
140                 m_pWidget->UpdateField();\r
141                 SetChangeMark();\r
142         }\r
143 \r
144 }\r