Get rid of FX_LPCSTR cast.
[pdfium.git] / fpdfsdk / src / formfiller / FFL_TextField.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.h"
8 //#include "../include/FFL_FormFiller.h"
9 #include "../../include/formfiller/FFL_TextField.h"
10 #include "../../include/formfiller/FFL_CBA_Fontmap.h"
11 //#include "../include/FFL_Notify.h"
12
13 CFFL_EditUndoItem::CFFL_EditUndoItem(CPWL_Edit* pEdit)
14 {
15 }
16
17 CFFL_EditUndoItem::~CFFL_EditUndoItem()
18 {
19 }
20
21 void CFFL_EditUndoItem::Undo()
22 {
23 }
24
25 void CFFL_EditUndoItem::Redo()
26 {
27 }
28
29 CFX_WideString CFFL_EditUndoItem::GetDescr()
30 {
31         return L"Input";
32 }
33
34 void CFFL_EditUndoItem::Release()
35 {
36         delete this;
37 }
38
39 /* ------------------------------- CFFL_TextField ------------------------------- */
40
41 CFFL_TextField::CFFL_TextField(CPDFDoc_Environment* pApp, CPDFSDK_Annot* pAnnot) :
42         CFFL_FormFiller(pApp, pAnnot),
43         m_pFontMap(NULL)//,
44         //m_pSpellCheck(NULL)
45 {
46         m_State.nStart = m_State.nEnd = 0;
47 }
48
49 CFFL_TextField::~CFFL_TextField()
50 {
51         if (m_pFontMap)
52         {
53                 delete m_pFontMap;
54                 m_pFontMap = NULL;
55         }
56
57 }
58
59 PWL_CREATEPARAM CFFL_TextField::GetCreateParam()
60 {
61         PWL_CREATEPARAM cp = CFFL_FormFiller::GetCreateParam();
62
63         ASSERT(m_pWidget != NULL);
64         int nFlags = m_pWidget->GetFieldFlags();
65
66
67         if (nFlags & FIELDFLAG_PASSWORD)
68         {               
69                 cp.dwFlags |= PES_PASSWORD;
70         }
71
72         if (!(nFlags & FIELDFLAG_DONOTSPELLCHECK))
73         {               
74         }
75
76         if (nFlags & FIELDFLAG_MULTILINE)
77         {               
78                 cp.dwFlags |= PES_MULTILINE | PES_AUTORETURN | PES_TOP;
79
80                 if (!(nFlags & FIELDFLAG_DONOTSCROLL))
81                 {
82                         cp.dwFlags |= PWS_VSCROLL | PES_AUTOSCROLL;
83                 }
84         }
85         else
86         {
87                 cp.dwFlags |= PES_CENTER;
88
89                 if (!(nFlags & FIELDFLAG_DONOTSCROLL))
90                 {
91                         cp.dwFlags |= PES_AUTOSCROLL;
92                 }
93         }
94
95         if (nFlags & FIELDFLAG_COMB)
96         {               
97                 cp.dwFlags |= PES_CHARARRAY;
98         }
99
100         if (nFlags & FIELDFLAG_RICHTEXT)
101         {               
102                 cp.dwFlags |= PES_RICH;
103         }
104
105         cp.dwFlags |= PES_UNDO;
106         
107         switch (m_pWidget->GetAlignment())
108         {
109         default:
110         case BF_ALIGN_LEFT:
111                 cp.dwFlags |= PES_LEFT;
112                 break;
113         case BF_ALIGN_MIDDLE:
114                 cp.dwFlags |= PES_MIDDLE;
115                 break;
116         case BF_ALIGN_RIGHT:
117                 cp.dwFlags |= PES_RIGHT;
118                 break;
119         }
120
121         if (!m_pFontMap)
122         {
123                 ASSERT(this->m_pApp != NULL);
124                 m_pFontMap = new CBA_FontMap(m_pWidget, /*ISystemHandle::GetSystemHandler(m_pApp)*/m_pApp->GetSysHandler());
125                 m_pFontMap->Initial();
126         }
127         cp.pFontMap = m_pFontMap;
128         cp.pFocusHandler = this;
129
130         return cp;
131 }
132
133 CPWL_Wnd* CFFL_TextField::NewPDFWindow(const PWL_CREATEPARAM& cp, CPDFSDK_PageView* pPageView)
134 {
135         CPWL_Edit * pWnd = new CPWL_Edit();
136                 pWnd->AttachFFLData(this);
137         pWnd->Create(cp);
138
139
140
141         ASSERT(m_pApp != NULL);
142         CFFL_IFormFiller* pIFormFiller = m_pApp->GetIFormFiller();
143         pWnd->SetFillerNotify(pIFormFiller);
144
145         ASSERT(m_pWidget != NULL);
146         FX_INT32 nMaxLen = m_pWidget->GetMaxLen();
147         CFX_WideString swValue = m_pWidget->GetValue();
148         
149         if (nMaxLen > 0)
150         {
151                 if (pWnd->HasFlag(PES_CHARARRAY))
152                 {
153                         pWnd->SetCharArray(nMaxLen);
154                         pWnd->SetAlignFormatV(PEAV_CENTER);
155                 }
156                 else
157                 {
158                         pWnd->SetLimitChar(nMaxLen);
159                 }
160         }
161         
162         pWnd->SetText(swValue);
163         
164         return pWnd;
165 }
166
167
168 FX_BOOL CFFL_TextField::OnChar(CPDFSDK_Annot* pAnnot, FX_UINT nChar, FX_UINT nFlags)
169 {
170         switch (nChar)
171         {
172         case FWL_VKEY_Return:
173                 if (!(m_pWidget->GetFieldFlags() & FIELDFLAG_MULTILINE))
174                 {
175                         CPDFSDK_PageView* pPageView = this->GetCurPageView();
176                         ASSERT(pPageView != NULL);
177                         m_bValid = !m_bValid;
178                         CPDF_Rect rcAnnot = pAnnot->GetRect();
179                         m_pApp->FFI_Invalidate(pAnnot->GetPDFPage(), rcAnnot.left, rcAnnot.top, rcAnnot.right, rcAnnot.bottom);
180  
181                         if (m_bValid)
182                         {
183                                 if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, TRUE))
184                                         pWnd->SetFocus();
185                         }
186                         else
187                         {
188                                 if (CommitData(pPageView, nFlags))
189                                 {
190                                         DestroyPDFWindow(pPageView);
191                                         return TRUE;
192                                 }
193                                 else
194                                 {
195                                         return FALSE;
196                                 }
197                         }
198                 }
199                 break;
200         case FWL_VKEY_Escape:
201                 {
202                         CPDFSDK_PageView* pPageView = this->GetCurPageView();
203                         ASSERT(pPageView != NULL);
204                         EscapeFiller(pPageView,TRUE);
205                         return TRUE;
206                 }
207         }
208
209         return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
210 }
211
212 FX_BOOL CFFL_TextField::IsDataChanged(CPDFSDK_PageView* pPageView)
213 {
214         ASSERT(m_pWidget != NULL);
215
216         if (CPWL_Edit * pEdit = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE))
217                 return pEdit->GetText() != m_pWidget->GetValue();
218
219         return FALSE;
220 }
221
222 void CFFL_TextField::SaveData(CPDFSDK_PageView* pPageView)
223 {
224         ASSERT(m_pWidget != NULL);
225
226         if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE))
227         {
228                 CFX_WideString sOldValue = m_pWidget->GetValue();
229                 CFX_WideString sNewValue = pWnd->GetText();
230
231                 m_pWidget->SetValue(sNewValue, FALSE);  
232                 m_pWidget->ResetFieldAppearance(TRUE);
233                 m_pWidget->UpdateField();
234                 SetChangeMark();
235         }
236 }
237
238 void CFFL_TextField::GetActionData(CPDFSDK_PageView* pPageView, CPDF_AAction::AActionType type,
239                                                                         PDFSDK_FieldAction& fa)
240 {
241         switch (type)
242         {
243         case CPDF_AAction::KeyStroke:
244                 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE))
245                 {
246                         fa.bFieldFull = pWnd->IsTextFull();     
247
248                         fa.sValue = pWnd->GetText();
249                         
250                         if (fa.bFieldFull)
251                         {
252                                 fa.sChange = L"";
253                                 fa.sChangeEx = L"";
254                         }
255                 }
256                 break;
257         case CPDF_AAction::Validate:
258                 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE))
259                 {
260                         fa.sValue = pWnd->GetText();
261                 }
262                 break;
263         case CPDF_AAction::LoseFocus:
264         case CPDF_AAction::GetFocus:
265                 ASSERT(m_pWidget != NULL);
266                 fa.sValue = m_pWidget->GetValue();
267                 break;
268         default:
269                 break;
270         }
271 }
272
273 void CFFL_TextField::SetActionData(CPDFSDK_PageView* pPageView, CPDF_AAction::AActionType type, 
274                                                                         const PDFSDK_FieldAction& fa)
275 {
276         switch (type)
277         {
278         case CPDF_AAction::KeyStroke:
279                 if (CPWL_Edit * pEdit = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE))
280                 {
281                         pEdit->SetFocus();
282                         pEdit->SetSel(fa.nSelStart, fa.nSelEnd);
283                         pEdit->ReplaceSel(fa.sChange);
284                 }
285                 break;
286         default:
287                 break;
288         }
289 }
290
291
292 FX_BOOL CFFL_TextField::IsActionDataChanged(CPDF_AAction::AActionType type, const PDFSDK_FieldAction& faOld, 
293                                                                         const PDFSDK_FieldAction& faNew)
294 {
295         switch (type)
296         {
297         case CPDF_AAction::KeyStroke:
298                 return (!faOld.bFieldFull && faOld.nSelEnd != faNew.nSelEnd) || faOld.nSelStart != faNew.nSelStart ||
299                         faOld.sChange != faNew.sChange;
300         default:
301                 break;
302         }
303
304         return FALSE;
305 }
306
307 void CFFL_TextField::SaveState(CPDFSDK_PageView* pPageView)
308 {
309         ASSERT(pPageView != NULL);
310
311         if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE))
312         {
313                 pWnd->GetSel(m_State.nStart, m_State.nEnd);
314                 m_State.sValue = pWnd->GetText();
315         }
316 }
317
318 void CFFL_TextField::RestoreState(CPDFSDK_PageView* pPageView)
319 {
320         ASSERT(pPageView != NULL);
321
322         if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, TRUE))
323         {
324                 pWnd->SetText(m_State.sValue);
325                 pWnd->SetSel(m_State.nStart, m_State.nEnd);
326         }
327 }
328
329 CPWL_Wnd* CFFL_TextField::ResetPDFWindow(CPDFSDK_PageView* pPageView, FX_BOOL bRestoreValue)
330 {
331         if (bRestoreValue)
332                 SaveState(pPageView);
333
334         DestroyPDFWindow(pPageView);
335
336         CPWL_Wnd* pRet = NULL;
337
338         if (bRestoreValue)
339         {
340                 RestoreState(pPageView);
341                 pRet = this->GetPDFWindow(pPageView, FALSE);
342         }
343         else
344                 pRet = this->GetPDFWindow(pPageView, TRUE);
345
346         m_pWidget->UpdateField();
347         
348         return pRet;
349 }
350
351 void CFFL_TextField::OnSetFocus(CPWL_Wnd* pWnd)
352 {
353         ASSERT(m_pApp != NULL);
354         
355         ASSERT(pWnd != NULL);
356  
357         if (pWnd->GetClassName() == PWL_CLASSNAME_EDIT)
358         {
359                 CPWL_Edit* pEdit = (CPWL_Edit*)pWnd;
360                 pEdit->SetCharSet(134);
361                 pEdit->SetCodePage(936);
362  
363                 pEdit->SetReadyToInput();
364                 CFX_WideString wsText = pEdit->GetText();
365                 int nCharacters = wsText.GetLength();
366                 CFX_ByteString bsUTFText = wsText.UTF16LE_Encode();
367                 unsigned short* pBuffer = (unsigned short*)bsUTFText.c_str();
368                 m_pApp->FFI_OnSetFieldInputFocus(m_pWidget->GetFormField(), pBuffer, nCharacters, TRUE);
369  
370                 pEdit->SetEditNotify(this);
371                 //pUndo->BeginEdit(pDocument);
372         }
373 }
374
375 void CFFL_TextField::OnKillFocus(CPWL_Wnd* pWnd)
376 {
377
378 }
379
380 FX_BOOL CFFL_TextField::CanCopy(CPDFSDK_Document* pDocument)
381 {
382         return FALSE;
383 }
384
385 FX_BOOL CFFL_TextField::CanCut(CPDFSDK_Document* pDocument)
386 {
387         return FALSE;
388 }
389
390 FX_BOOL CFFL_TextField::CanPaste(CPDFSDK_Document* pDocument)
391 {
392         return FALSE;
393 }
394
395 void CFFL_TextField::DoCopy(CPDFSDK_Document* pDocument)
396 {
397
398 }
399
400 void CFFL_TextField::DoCut(CPDFSDK_Document* pDocument)
401 {
402 }
403
404 void CFFL_TextField::DoPaste(CPDFSDK_Document* pDocument)
405 {
406
407 }
408
409 void CFFL_TextField::OnAddUndo(CPWL_Edit* pEdit)
410 {
411 }
412