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
12/* ------------------------------- CFFL_CheckBox ------------------------------- */
13
14CFFL_CheckBox::CFFL_CheckBox(CPDFDoc_Environment* pApp, CPDFSDK_Widget* pWidget) :
15	CFFL_Button(pApp, pWidget)
16{
17}
18
19CFFL_CheckBox::~CFFL_CheckBox()
20{
21}
22
23CPWL_Wnd* CFFL_CheckBox::NewPDFWindow(const PWL_CREATEPARAM& cp, CPDFSDK_PageView* pPageView)
24{
25	CPWL_CheckBox* pWnd = new CPWL_CheckBox();
26	pWnd->Create(cp);
27
28	ASSERT(m_pWidget != NULL);
29	pWnd->SetCheck(m_pWidget->IsChecked());
30
31	return pWnd;
32}
33
34FX_BOOL	CFFL_CheckBox::OnKeyDown(CPDFSDK_Annot* pAnnot, FX_UINT nKeyCode, FX_UINT nFlags)
35{
36	switch (nKeyCode)
37	{
38	case FWL_VKEY_Return:
39	case FWL_VKEY_Space:
40		return TRUE;
41	default:
42		return CFFL_FormFiller::OnKeyDown(pAnnot, nKeyCode, nFlags);
43	}
44}
45FX_BOOL	CFFL_CheckBox::OnChar(CPDFSDK_Annot* pAnnot, FX_UINT nChar, FX_UINT nFlags)
46{
47	switch (nChar)
48	{
49	case FWL_VKEY_Return:
50	case FWL_VKEY_Space:
51		{
52			CFFL_IFormFiller* pIFormFiller = m_pApp->GetIFormFiller();
53			ASSERT(pIFormFiller != NULL);
54
55			CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
56			ASSERT(pPageView != NULL);
57
58			FX_BOOL bReset = FALSE;
59			FX_BOOL bExit = FALSE;
60
61			pIFormFiller->OnButtonUp(m_pWidget, pPageView, bReset, bExit,nFlags);
62
63			if (bReset) return TRUE;
64			if (bExit) return TRUE;
65
66			CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
67
68			if (CPWL_CheckBox * pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, TRUE))
69				pWnd->SetCheck(!pWnd->IsChecked());
70
71			CommitData(pPageView,nFlags);
72			return TRUE;
73		}
74	default:
75		return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
76	}
77}
78
79FX_BOOL CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
80{
81	CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
82
83	if (IsValid())
84	{
85		if (CPWL_CheckBox * pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, TRUE))
86		{
87			CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
88			pWnd->SetCheck(!pWidget->IsChecked());
89		//	pWnd->SetCheck(!pWnd->IsChecked());
90		}
91
92		if (!CommitData(pPageView, nFlags)) return FALSE;
93	}
94
95	return TRUE;
96}
97
98FX_BOOL	CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView)
99{
100
101	ASSERT(m_pWidget != NULL);
102
103	if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, FALSE))
104	{
105		return pWnd->IsChecked() != m_pWidget->IsChecked();
106	}
107
108	return FALSE;
109}
110
111void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView)
112{
113
114	ASSERT(m_pWidget != NULL);
115
116	if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, FALSE))
117	{
118
119		FX_BOOL bNewChecked = pWnd->IsChecked();
120
121
122		if (bNewChecked)
123		{
124			CPDF_FormField* pField = m_pWidget->GetFormField();
125			ASSERT(pField != NULL);
126
127			for (FX_INT32 i=0,sz=pField->CountControls(); i<sz; i++)
128			{
129				if (CPDF_FormControl* pCtrl = pField->GetControl(i))
130				{
131					if (pCtrl->IsChecked())
132					{
133						break;
134					}
135				}
136			}
137		}
138
139		m_pWidget->SetCheck(bNewChecked, FALSE);
140		m_pWidget->UpdateField();
141		SetChangeMark();
142	}
143
144}
145