Initial commit.
[pdfium.git] / fpdfsdk / src / formfiller / FFL_RadioButton.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_RadioButton.h"\r
10 //#include "../include/FFL_Undo.h"\r
11 \r
12 /* ------------------------------- CFFL_RadioButton ------------------------------- */\r
13 \r
14 CFFL_RadioButton::CFFL_RadioButton(CPDFDoc_Environment* pApp, CPDFSDK_Annot* pWidget) :\r
15         CFFL_Button(pApp, pWidget)\r
16 {\r
17 }\r
18 \r
19 CFFL_RadioButton::~CFFL_RadioButton()\r
20 {\r
21 }\r
22 \r
23 CPWL_Wnd* CFFL_RadioButton::NewPDFWindow(const PWL_CREATEPARAM& cp, CPDFSDK_PageView* pPageView)\r
24 {\r
25         CPWL_RadioButton* pWnd = new CPWL_RadioButton();\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_RadioButton::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 \r
46 FX_BOOL CFFL_RadioButton::OnChar(CPDFSDK_Annot* pAnnot, FX_UINT nChar, FX_UINT nFlags)\r
47 {\r
48         switch (nChar)\r
49         {\r
50         case FWL_VKEY_Return:   \r
51         case FWL_VKEY_Space:\r
52                 {\r
53                         CFFL_IFormFiller* pIFormFiller = m_pApp->GetIFormFiller();\r
54                         ASSERT(pIFormFiller != NULL);\r
55 \r
56                         CPDFSDK_PageView* pPageView = pAnnot->GetPageView();\r
57                         ASSERT(pPageView != NULL);\r
58 \r
59                         FX_BOOL bReset = FALSE;\r
60                         FX_BOOL bExit = FALSE;\r
61 \r
62                         pIFormFiller->OnButtonUp(m_pWidget, pPageView, bReset, bExit,nFlags);\r
63 \r
64                         if (bReset) return TRUE;\r
65                         if (bExit) return TRUE;\r
66 \r
67                         CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);\r
68 \r
69                         if (CPWL_RadioButton * pWnd = (CPWL_RadioButton*)GetPDFWindow(pPageView, TRUE))\r
70                                 pWnd->SetCheck(TRUE);\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_RadioButton::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_RadioButton * pWnd = (CPWL_RadioButton*)GetPDFWindow(pPageView, TRUE))\r
86                         pWnd->SetCheck(TRUE);\r
87 \r
88                 if (!CommitData(pPageView,nFlags)) return FALSE;\r
89         }\r
90 \r
91         return TRUE;\r
92 }\r
93 \r
94 FX_BOOL CFFL_RadioButton::IsDataChanged(CPDFSDK_PageView* pPageView)\r
95 {\r
96         ASSERT(m_pWidget != NULL);\r
97 \r
98         if (CPWL_RadioButton* pWnd = (CPWL_RadioButton*)GetPDFWindow(pPageView, FALSE))\r
99         {\r
100                 return pWnd->IsChecked() != m_pWidget->IsChecked();\r
101         }\r
102 \r
103         return FALSE;\r
104 }\r
105 \r
106 void CFFL_RadioButton::SaveData(CPDFSDK_PageView* pPageView)\r
107 {\r
108         ASSERT(m_pWidget != NULL);\r
109 \r
110         if (CPWL_RadioButton* pWnd = (CPWL_RadioButton*)GetPDFWindow(pPageView, FALSE))\r
111         {\r
112                 \r
113                 FX_BOOL bNewChecked = pWnd->IsChecked();\r
114 \r
115                 if (bNewChecked)\r
116                 {\r
117                         CPDF_FormField* pField = m_pWidget->GetFormField();\r
118                         ASSERT(pField != NULL);\r
119 \r
120                         for (FX_INT32 i=0,sz=pField->CountControls(); i<sz; i++)\r
121                         {\r
122                                 if (CPDF_FormControl* pCtrl = pField->GetControl(i))\r
123                                 {\r
124                                         if (pCtrl->IsChecked())\r
125                                         {\r
126                                                 break;\r
127                                         }\r
128                                 }\r
129                         }\r
130                 }\r
131 \r
132                 m_pWidget->SetCheck(bNewChecked, FALSE);\r
133                 m_pWidget->UpdateField();\r
134                 SetChangeMark();\r
135         }\r
136 }\r
137 \r