Convert all line endings to LF.
[pdfium.git] / fpdfsdk / src / formfiller / FFL_ComboBox.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_IFormFiller.h"
10 #include "../../include/formfiller/FFL_CBA_Fontmap.h"
11 #include "../../include/formfiller/FFL_ComboBox.h"
12
13
14 /* ------------------------------- CFFL_ComboBox ------------------------------- */
15
16 CFFL_ComboBox::CFFL_ComboBox(CPDFDoc_Environment* pApp, CPDFSDK_Annot* pAnnot) :
17         CFFL_FormFiller(pApp, pAnnot), m_pFontMap( NULL )
18 {
19         //m_pFontMap = new CBA_FontMap( pAnnot, GetSystemHandler() );
20         m_State.nIndex = 0;
21         m_State.nStart = 0;
22         m_State.nEnd   = 0;
23 }
24
25 CFFL_ComboBox::~CFFL_ComboBox()
26 {
27         if (m_pFontMap)
28         {
29                 delete m_pFontMap;
30                 m_pFontMap = NULL;
31         }
32
33 //      for (int i=0,sz=m_IMBox.GetSize(); i<sz; i++)
34 //      {
35 //              delete m_IMBox.GetAt(i);
36 //      }
37 // 
38 //      m_IMBox.RemoveAll();
39 }
40
41 PWL_CREATEPARAM CFFL_ComboBox::GetCreateParam()
42 {
43         PWL_CREATEPARAM cp = CFFL_FormFiller::GetCreateParam();
44
45         ASSERT(m_pWidget != NULL);
46
47         int nFlags = m_pWidget->GetFieldFlags();
48         
49         if (nFlags & FIELDFLAG_EDIT)
50         {               
51                 cp.dwFlags |= PCBS_ALLOWCUSTOMTEXT;
52         }
53
54         /*
55         if (nFlags & FIELDFLAG_COMMITONSELCHANGE)
56         {               
57                 m_bCommitOnSelectChange = TRUE;
58         }
59         */
60
61         if (!m_pFontMap)
62         {
63                 ASSERT(this->m_pApp != NULL);
64                 m_pFontMap = new CBA_FontMap(m_pWidget, GetSystemHandler());
65                 m_pFontMap->Initial();
66         }
67
68         cp.pFontMap = m_pFontMap;
69         cp.pFocusHandler = this;
70
71         return cp;
72 }
73
74 CPWL_Wnd* CFFL_ComboBox::NewPDFWindow(const PWL_CREATEPARAM& cp, CPDFSDK_PageView* pPageView)
75 {
76         CPWL_ComboBox * pWnd = new CPWL_ComboBox();
77         pWnd->AttachFFLData(this);
78         pWnd->Create(cp);
79
80         ASSERT(m_pApp != NULL);
81         CFFL_IFormFiller* pFormFiller = m_pApp->GetIFormFiller();
82         pWnd->SetFillerNotify(pFormFiller);
83
84         ASSERT(m_pWidget != NULL);
85         FX_INT32 nCurSel = m_pWidget->GetSelectedIndex(0);
86         
87         CFX_WideString swText;
88         
89         if (nCurSel < 0)
90                 swText = m_pWidget->GetValue();
91         else
92                 swText = m_pWidget->GetOptionLabel(nCurSel);
93         
94         for (FX_INT32 i=0,sz=m_pWidget->CountOptions(); i<sz; i++)
95         {
96                 pWnd->AddString(m_pWidget->GetOptionLabel(i));                  
97         }
98         
99         pWnd->SetSelect(nCurSel);
100         pWnd->SetText(swText);
101         
102         return pWnd;
103 }
104
105
106 FX_BOOL CFFL_ComboBox::OnChar(CPDFSDK_Annot* pAnnot, FX_UINT nChar, FX_UINT nFlags)
107 {
108         return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
109 }
110
111 FX_BOOL CFFL_ComboBox::IsDataChanged(CPDFSDK_PageView* pPageView)
112 {
113         if (CPWL_ComboBox * pWnd = (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE))
114         {
115                 FX_INT32 nCurSel = pWnd->GetSelect();
116
117                 ASSERT(m_pWidget != NULL);
118
119                 if (m_pWidget->GetFieldFlags() & FIELDFLAG_EDIT)
120                 {
121                         if (nCurSel >= 0)
122                         {
123                                 return nCurSel != m_pWidget->GetSelectedIndex(0);
124                         }
125                         else
126                         {
127                                 return pWnd->GetText() != m_pWidget->GetValue();
128                         }
129                 }
130                 else
131                 {
132                         return nCurSel != m_pWidget->GetSelectedIndex(0);
133                 }
134         }
135         
136         return FALSE;
137 }
138
139 void CFFL_ComboBox::SaveData(CPDFSDK_PageView* pPageView)
140 {
141         ASSERT(m_pWidget != NULL);
142
143         if (CPWL_ComboBox* pWnd = (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE))
144         {
145                 CFX_WideString swText = pWnd->GetText();
146                 FX_INT32 nCurSel = pWnd->GetSelect();
147
148                 //mantis:0004157
149                 FX_BOOL bSetValue = TRUE;
150
151                 if (m_pWidget->GetFieldFlags() & FIELDFLAG_EDIT)
152                 {
153                         if (nCurSel >= 0)
154                         {
155                                 if (swText != m_pWidget->GetOptionLabel(nCurSel))
156                                         bSetValue = TRUE;
157                                 else
158                                         bSetValue = FALSE;
159                         }
160                         else
161                                 bSetValue = TRUE;
162                 }
163                 else
164                         bSetValue = FALSE;
165
166                 CFX_WideString sOldValue;
167                 
168
169                 if (bSetValue)
170                 {
171                         sOldValue = m_pWidget->GetValue();
172                         m_pWidget->SetValue(swText, FALSE);
173                 }
174                 else
175                 {
176                         m_pWidget->GetSelectedIndex(0);
177                         m_pWidget->SetOptionSelection(nCurSel, TRUE, FALSE);
178                 }
179
180                 m_pWidget->ResetFieldAppearance(TRUE);
181                 m_pWidget->UpdateField();
182                 SetChangeMark();
183
184                 m_pWidget->GetPDFPage();
185                 
186
187         }
188 }
189
190  void CFFL_ComboBox::GetActionData( CPDFSDK_PageView* pPageView, CPDF_AAction::AActionType type, PDFSDK_FieldAction& fa)
191 {
192         switch (type)
193         {
194         case CPDF_AAction::KeyStroke:
195                 if (CPWL_ComboBox* pComboBox = (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE))
196                 {
197                         if (CPWL_Edit* pEdit = (CPWL_Edit*)*pComboBox)
198                         {
199                                 fa.bFieldFull = pEdit->IsTextFull();    
200                                 int nSelStart = 0;
201                                 int nSelEnd = 0;
202                                 pEdit->GetSel(nSelStart, nSelEnd);
203                                 fa.nSelEnd = nSelEnd;
204                                 fa.nSelStart = nSelStart;
205                                 fa.sValue = pEdit->GetText();
206                                 fa.sChangeEx = GetSelectExportText();
207
208                                 if (fa.bFieldFull)
209                                 {
210                                         fa.sChange = L"";
211                                         fa.sChangeEx = L"";
212                                 }
213                         }
214                 }
215                 break;
216         case CPDF_AAction::Validate:
217                 if (CPWL_ComboBox* pComboBox = (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE))
218                 {
219                         if (CPWL_Edit* pEdit = (CPWL_Edit*)*pComboBox)
220                         {
221                                 fa.sValue = pEdit->GetText();
222                         }
223                 }
224                 break;
225         case CPDF_AAction::LoseFocus:
226         case CPDF_AAction::GetFocus:
227                 ASSERT(m_pWidget != NULL);
228                 fa.sValue = m_pWidget->GetValue();
229                 break;
230         default:
231                 break;
232         }
233 }
234
235
236
237 void CFFL_ComboBox::SetActionData(CPDFSDK_PageView* pPageView, CPDF_AAction::AActionType type, 
238                                                                         const PDFSDK_FieldAction& fa)
239 {
240         switch (type)
241         {
242         case CPDF_AAction::KeyStroke:
243                 if (CPWL_ComboBox* pComboBox = (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE))
244                 {
245                         if (CPWL_Edit* pEdit = (CPWL_Edit*)*pComboBox)
246                         {
247                                 pEdit->SetSel(fa.nSelStart, fa.nSelEnd);
248                                 pEdit->ReplaceSel(fa.sChange);
249                         }
250                 }
251                 break;
252         default:
253                 break;
254         }
255 }
256
257 FX_BOOL CFFL_ComboBox::IsActionDataChanged(CPDF_AAction::AActionType type, const PDFSDK_FieldAction& faOld, 
258                                                                         const PDFSDK_FieldAction& faNew)
259 {
260         switch (type)
261         {
262         case CPDF_AAction::KeyStroke:
263                 return (!faOld.bFieldFull && faOld.nSelEnd != faNew.nSelEnd) || faOld.nSelStart != faNew.nSelStart ||
264                         faOld.sChange != faNew.sChange;
265         default:
266                 break;
267         }
268
269         return FALSE;
270 }
271
272 void CFFL_ComboBox::SaveState(CPDFSDK_PageView* pPageView)
273 {
274         ASSERT(pPageView != NULL);
275
276         if (CPWL_ComboBox* pComboBox = (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE))
277         {
278                 m_State.nIndex = pComboBox->GetSelect();
279
280                 if (CPWL_Edit* pEdit = (CPWL_Edit*)*pComboBox)
281                 {
282                         pEdit->GetSel(m_State.nStart, m_State.nEnd);
283                         m_State.sValue = pEdit->GetText();
284                 }
285         }
286 }
287
288 void CFFL_ComboBox::RestoreState(CPDFSDK_PageView* pPageView)
289 {
290         ASSERT(pPageView != NULL);
291
292         if (CPWL_ComboBox* pComboBox = (CPWL_ComboBox*)GetPDFWindow(pPageView, TRUE))
293         {
294                 if (m_State.nIndex >= 0)
295                         pComboBox->SetSelect(m_State.nIndex);
296                 else
297                 {
298                         if (CPWL_Edit* pEdit = (CPWL_Edit*)*pComboBox)
299                         {
300                                 pEdit->SetText(m_State.sValue);
301                                 pEdit->SetSel(m_State.nStart, m_State.nEnd);
302                         }
303                 }
304         }
305 }
306
307 CPWL_Wnd* CFFL_ComboBox::ResetPDFWindow(CPDFSDK_PageView* pPageView, FX_BOOL bRestoreValue)
308 {
309         if (bRestoreValue)
310                 SaveState(pPageView);
311         
312         DestroyPDFWindow(pPageView);
313         
314         CPWL_Wnd* pRet = NULL;
315         
316         if (bRestoreValue)
317         {
318                 RestoreState(pPageView);
319                 pRet = this->GetPDFWindow(pPageView, FALSE);
320         }
321         else
322                 pRet = this->GetPDFWindow(pPageView, TRUE);
323         
324         m_pWidget->UpdateField();
325         
326         return pRet;
327 }
328
329 void CFFL_ComboBox::OnKeyStroke(FX_BOOL bKeyDown, FX_UINT nFlag)
330 {
331         ASSERT(m_pWidget != NULL);
332         
333         int nFlags = m_pWidget->GetFieldFlags();
334         
335         if (nFlags & FIELDFLAG_COMMITONSELCHANGE)
336         {
337                 if (m_bValid)
338                 {
339                         CPDFSDK_PageView* pPageView = this->GetCurPageView();
340                         ASSERT(pPageView != NULL);
341
342                         if (CommitData(pPageView, nFlag))
343                         {
344                                 DestroyPDFWindow(pPageView);
345                                 m_bValid = FALSE;
346                         }
347                 }
348         }
349 }
350
351 void CFFL_ComboBox::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*)(FX_LPCSTR)bsUTFText;
368                 m_pApp->FFI_OnSetFieldInputFocus(m_pWidget->GetFormField(), pBuffer, nCharacters, TRUE);
369
370                 pEdit->SetEditNotify(this);
371         }
372 }
373
374 void CFFL_ComboBox::OnKillFocus(CPWL_Wnd* pWnd)
375 {
376         ASSERT(m_pApp != NULL);
377 }
378
379 FX_BOOL CFFL_ComboBox::CanCopy(CPDFSDK_Document* pDocument)
380 {
381         ASSERT(pDocument != NULL);
382
383         return FALSE;
384 }
385
386 FX_BOOL CFFL_ComboBox::CanCut(CPDFSDK_Document* pDocument)
387 {
388         ASSERT(pDocument != NULL);
389
390         return FALSE;
391 }
392
393 FX_BOOL CFFL_ComboBox::CanPaste(CPDFSDK_Document* pDocument)
394 {
395         ASSERT(pDocument != NULL);
396
397         return FALSE;
398 }
399
400 void CFFL_ComboBox::DoCopy(CPDFSDK_Document* pDocument)
401 {
402         ASSERT(pDocument != NULL);
403 }
404
405 void CFFL_ComboBox::DoCut(CPDFSDK_Document* pDocument)
406 {
407         ASSERT(pDocument != NULL);
408 }
409
410 void CFFL_ComboBox::DoPaste(CPDFSDK_Document* pDocument)
411 {
412         ASSERT(pDocument != NULL);
413 }
414
415 void CFFL_ComboBox::OnAddUndo(CPWL_Edit* pEdit)
416 {
417         ASSERT(pEdit != NULL);
418 }
419
420 CFX_WideString CFFL_ComboBox::GetSelectExportText()
421 {
422         CFX_WideString swRet;
423         
424         int nExport = -1;
425         CPDFSDK_PageView *pPageView = GetCurPageView();
426         if (CPWL_ComboBox * pComboBox = (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE))
427         {
428                 nExport = pComboBox->GetSelect();
429         }
430         
431         if (nExport >= 0)
432         {
433                 if (CPDF_FormField * pFormField = m_pWidget->GetFormField())
434                 {
435                         swRet = pFormField->GetOptionValue(nExport);
436                         if (swRet.IsEmpty())
437                                 swRet = pFormField->GetOptionLabel(nExport);
438                 }
439         }
440         
441         return swRet;
442 }