• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 "fpdfsdk/fpdfxfa/cpdfxfa_widgethandler.h"
8 
9 #include "fpdfsdk/cpdfsdk_annot.h"
10 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
11 #include "fpdfsdk/cpdfsdk_interactiveform.h"
12 #include "fpdfsdk/cpdfsdk_pageview.h"
13 #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
14 #include "fpdfsdk/fpdfxfa/cpdfxfa_widget.h"
15 #include "public/fpdf_fwlevent.h"
16 #include "xfa/fwl/cfwl_app.h"
17 #include "xfa/fwl/fwl_widgetdef.h"
18 #include "xfa/fwl/fwl_widgethit.h"
19 #include "xfa/fxfa/cxfa_ffdocview.h"
20 #include "xfa/fxfa/cxfa_ffpageview.h"
21 #include "xfa/fxfa/cxfa_ffwidget.h"
22 #include "xfa/fxfa/cxfa_ffwidgethandler.h"
23 #include "xfa/fxfa/fxfa_basic.h"
24 #include "xfa/fxfa/parser/cxfa_node.h"
25 #include "xfa/fxgraphics/cxfa_graphics.h"
26 
27 #define CHECK_FWL_VKEY_ENUM____(name)                                   \
28   static_assert(static_cast<int>(name) == static_cast<int>(XFA_##name), \
29                 "FWL_VKEYCODE enum mismatch")
30 
31 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Back);
32 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Tab);
33 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NewLine);
34 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Clear);
35 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Return);
36 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Shift);
37 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Control);
38 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Menu);
39 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Pause);
40 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Capital);
41 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Kana);
42 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Hangul);
43 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Junja);
44 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Final);
45 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Hanja);
46 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Kanji);
47 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Escape);
48 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Convert);
49 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NonConvert);
50 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Accept);
51 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_ModeChange);
52 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Space);
53 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Prior);
54 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Next);
55 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_End);
56 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Home);
57 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Left);
58 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Up);
59 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Right);
60 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Down);
61 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Select);
62 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Print);
63 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Execute);
64 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Snapshot);
65 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Insert);
66 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Delete);
67 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Help);
68 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_0);
69 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_1);
70 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_2);
71 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_3);
72 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_4);
73 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_5);
74 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_6);
75 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_7);
76 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_8);
77 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_9);
78 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_A);
79 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_B);
80 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_C);
81 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_D);
82 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_E);
83 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F);
84 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_G);
85 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_H);
86 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_I);
87 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_J);
88 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_K);
89 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_L);
90 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_M);
91 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_N);
92 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_O);
93 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_P);
94 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Q);
95 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_R);
96 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_S);
97 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_T);
98 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_U);
99 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_V);
100 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_W);
101 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_X);
102 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Y);
103 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Z);
104 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_LWin);
105 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Command);
106 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_RWin);
107 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Apps);
108 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Sleep);
109 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NumPad0);
110 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NumPad1);
111 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NumPad2);
112 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NumPad3);
113 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NumPad4);
114 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NumPad5);
115 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NumPad6);
116 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NumPad7);
117 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NumPad8);
118 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NumPad9);
119 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Multiply);
120 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Add);
121 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Separator);
122 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Subtract);
123 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Decimal);
124 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Divide);
125 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F1);
126 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F2);
127 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F3);
128 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F4);
129 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F5);
130 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F6);
131 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F7);
132 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F8);
133 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F9);
134 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F10);
135 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F11);
136 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F12);
137 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F13);
138 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F14);
139 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F15);
140 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F16);
141 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F17);
142 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F18);
143 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F19);
144 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F20);
145 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F21);
146 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F22);
147 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F23);
148 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_F24);
149 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NunLock);
150 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Scroll);
151 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_LShift);
152 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_RShift);
153 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_LControl);
154 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_RControl);
155 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_LMenu);
156 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_RMenu);
157 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_BROWSER_Back);
158 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_BROWSER_Forward);
159 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_BROWSER_Refresh);
160 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_BROWSER_Stop);
161 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_BROWSER_Search);
162 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_BROWSER_Favorites);
163 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_BROWSER_Home);
164 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_VOLUME_Mute);
165 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_VOLUME_Down);
166 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_VOLUME_Up);
167 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_MEDIA_NEXT_Track);
168 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_MEDIA_PREV_Track);
169 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_MEDIA_Stop);
170 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_MEDIA_PLAY_Pause);
171 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_MEDIA_LAUNCH_Mail);
172 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_MEDIA_LAUNCH_MEDIA_Select);
173 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_MEDIA_LAUNCH_APP1);
174 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_MEDIA_LAUNCH_APP2);
175 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_1);
176 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_Plus);
177 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_Comma);
178 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_Minus);
179 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_Period);
180 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_2);
181 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_3);
182 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_4);
183 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_5);
184 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_6);
185 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_7);
186 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_8);
187 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_102);
188 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_ProcessKey);
189 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Packet);
190 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Attn);
191 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Crsel);
192 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Exsel);
193 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Ereof);
194 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Play);
195 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Zoom);
196 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_NoName);
197 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_PA1);
198 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_OEM_Clear);
199 CHECK_FWL_VKEY_ENUM____(FWL_VKEY_Unknown);
200 
201 #undef CHECK_FWL_VKEY_ENUM____
202 
203 CPDFXFA_WidgetHandler::CPDFXFA_WidgetHandler() = default;
204 
205 CPDFXFA_WidgetHandler::~CPDFXFA_WidgetHandler() = default;
206 
SetFormFillEnvironment(CPDFSDK_FormFillEnvironment * pFormFillEnv)207 void CPDFXFA_WidgetHandler::SetFormFillEnvironment(
208     CPDFSDK_FormFillEnvironment* pFormFillEnv) {
209   m_pFormFillEnv = pFormFillEnv;
210 }
211 
CanAnswer(CPDFSDK_Annot * pAnnot)212 bool CPDFXFA_WidgetHandler::CanAnswer(CPDFSDK_Annot* pAnnot) {
213   CPDFXFA_Widget* pWidget = ToXFAWidget(pAnnot);
214   return pWidget && pWidget->GetXFAFFWidget();
215 }
216 
NewAnnot(CPDF_Annot * pAnnot,CPDFSDK_PageView * pPage)217 CPDFSDK_Annot* CPDFXFA_WidgetHandler::NewAnnot(CPDF_Annot* pAnnot,
218                                                CPDFSDK_PageView* pPage) {
219   return nullptr;
220 }
221 
NewAnnotForXFA(CXFA_FFWidget * pAnnot,CPDFSDK_PageView * pPage)222 std::unique_ptr<CPDFSDK_Annot> CPDFXFA_WidgetHandler::NewAnnotForXFA(
223     CXFA_FFWidget* pAnnot,
224     CPDFSDK_PageView* pPage) {
225   CPDFSDK_InteractiveForm* pForm = m_pFormFillEnv->GetInteractiveForm();
226   return pdfium::MakeUnique<CPDFXFA_Widget>(pAnnot, pPage, pForm);
227 }
228 
OnDraw(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,CFX_RenderDevice * pDevice,const CFX_Matrix & mtUser2Device,bool bDrawAnnots)229 void CPDFXFA_WidgetHandler::OnDraw(CPDFSDK_PageView* pPageView,
230                                    CPDFSDK_Annot* pAnnot,
231                                    CFX_RenderDevice* pDevice,
232                                    const CFX_Matrix& mtUser2Device,
233                                    bool bDrawAnnots) {
234   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
235   ASSERT(pXFAWidget);
236 
237   bool bIsHighlight = false;
238   if (pPageView->GetFormFillEnv()->GetFocusAnnot() != pAnnot)
239     bIsHighlight = true;
240 
241   CXFA_Graphics gs(pDevice);
242   GetXFAFFWidgetHandler(pXFAWidget)
243       ->RenderWidget(pXFAWidget->GetXFAFFWidget(), &gs, mtUser2Device,
244                      bIsHighlight);
245 
246   // to do highlight and shadow
247 }
248 
OnLoad(CPDFSDK_Annot * pAnnot)249 void CPDFXFA_WidgetHandler::OnLoad(CPDFSDK_Annot* pAnnot) {}
250 
ReleaseAnnot(std::unique_ptr<CPDFSDK_Annot> pAnnot)251 void CPDFXFA_WidgetHandler::ReleaseAnnot(
252     std::unique_ptr<CPDFSDK_Annot> pAnnot) {}
253 
GetViewBBox(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot)254 CFX_FloatRect CPDFXFA_WidgetHandler::GetViewBBox(CPDFSDK_PageView* pPageView,
255                                                  CPDFSDK_Annot* pAnnot) {
256   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
257   CXFA_Node* node = pXFAWidget->GetXFAFFWidget()->GetNode();
258   ASSERT(node->IsWidgetReady());
259 
260   CFX_RectF rcBBox = pXFAWidget->GetXFAFFWidget()->GetBBox(
261       node->GetFFWidgetType() == XFA_FFWidgetType::kSignature
262           ? CXFA_FFWidget::kDrawFocus
263           : CXFA_FFWidget::kDoNotDrawFocus);
264 
265   CFX_FloatRect rcWidget = rcBBox.ToFloatRect();
266   rcWidget.Inflate(1.0f, 1.0f);
267   return rcWidget;
268 }
269 
GetText(CPDFSDK_Annot * pAnnot)270 WideString CPDFXFA_WidgetHandler::GetText(CPDFSDK_Annot* pAnnot) {
271   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
272   if (!pXFAWidget)
273     return WideString();
274 
275   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
276   return pWidgetHandler->GetText(pXFAWidget->GetXFAFFWidget());
277 }
278 
GetSelectedText(CPDFSDK_Annot * pAnnot)279 WideString CPDFXFA_WidgetHandler::GetSelectedText(CPDFSDK_Annot* pAnnot) {
280   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
281   if (!pXFAWidget)
282     return WideString();
283 
284   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
285   return pWidgetHandler->GetSelectedText(pXFAWidget->GetXFAFFWidget());
286 }
287 
ReplaceSelection(CPDFSDK_Annot * pAnnot,const WideString & text)288 void CPDFXFA_WidgetHandler::ReplaceSelection(CPDFSDK_Annot* pAnnot,
289                                              const WideString& text) {
290   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
291   if (!pXFAWidget)
292     return;
293 
294   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
295   return pWidgetHandler->PasteText(pXFAWidget->GetXFAFFWidget(), text);
296 }
297 
CanUndo(CPDFSDK_Annot * pAnnot)298 bool CPDFXFA_WidgetHandler::CanUndo(CPDFSDK_Annot* pAnnot) {
299   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
300   if (!pXFAWidget)
301     return false;
302 
303   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
304   return pWidgetHandler->CanUndo(pXFAWidget->GetXFAFFWidget());
305 }
306 
CanRedo(CPDFSDK_Annot * pAnnot)307 bool CPDFXFA_WidgetHandler::CanRedo(CPDFSDK_Annot* pAnnot) {
308   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
309   if (!pXFAWidget)
310     return false;
311 
312   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
313   return pWidgetHandler->CanRedo(pXFAWidget->GetXFAFFWidget());
314 }
315 
Undo(CPDFSDK_Annot * pAnnot)316 bool CPDFXFA_WidgetHandler::Undo(CPDFSDK_Annot* pAnnot) {
317   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
318   if (!pXFAWidget)
319     return false;
320 
321   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
322   return pWidgetHandler->Undo(pXFAWidget->GetXFAFFWidget());
323 }
324 
Redo(CPDFSDK_Annot * pAnnot)325 bool CPDFXFA_WidgetHandler::Redo(CPDFSDK_Annot* pAnnot) {
326   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
327   if (!pXFAWidget)
328     return false;
329 
330   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
331   return pWidgetHandler->Redo(pXFAWidget->GetXFAFFWidget());
332 }
333 
HitTest(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,const CFX_PointF & point)334 bool CPDFXFA_WidgetHandler::HitTest(CPDFSDK_PageView* pPageView,
335                                     CPDFSDK_Annot* pAnnot,
336                                     const CFX_PointF& point) {
337   if (!pPageView)
338     return false;
339 
340   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
341   if (!pXFAWidget)
342     return false;
343 
344   CPDFSDK_FormFillEnvironment* pFormFillEnv = pPageView->GetFormFillEnv();
345   if (!pFormFillEnv)
346     return false;
347 
348   auto* pContext =
349       static_cast<CPDFXFA_Context*>(pFormFillEnv->GetDocExtension());
350   if (!pContext)
351     return false;
352 
353   CXFA_FFDocView* pDocView = pContext->GetXFADocView();
354   if (!pDocView)
355     return false;
356 
357   CXFA_FFWidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler();
358   return pWidgetHandler &&
359          pWidgetHandler->HitTest(pXFAWidget->GetXFAFFWidget(), point) !=
360              FWL_WidgetHit::Unknown;
361 }
362 
OnMouseEnter(CPDFSDK_PageView * pPageView,ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlag)363 void CPDFXFA_WidgetHandler::OnMouseEnter(CPDFSDK_PageView* pPageView,
364                                          ObservedPtr<CPDFSDK_Annot>* pAnnot,
365                                          uint32_t nFlag) {
366   if (!pPageView)
367     return;
368 
369   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot->Get());
370   if (!pXFAWidget)
371     return;
372 
373   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
374   pWidgetHandler->OnMouseEnter(pXFAWidget->GetXFAFFWidget());
375 }
376 
OnMouseExit(CPDFSDK_PageView * pPageView,ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlag)377 void CPDFXFA_WidgetHandler::OnMouseExit(CPDFSDK_PageView* pPageView,
378                                         ObservedPtr<CPDFSDK_Annot>* pAnnot,
379                                         uint32_t nFlag) {
380   if (!pPageView)
381     return;
382 
383   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot->Get());
384   if (!pXFAWidget)
385     return;
386 
387   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
388   pWidgetHandler->OnMouseExit(pXFAWidget->GetXFAFFWidget());
389 }
390 
OnLButtonDown(CPDFSDK_PageView * pPageView,ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlags,const CFX_PointF & point)391 bool CPDFXFA_WidgetHandler::OnLButtonDown(CPDFSDK_PageView* pPageView,
392                                           ObservedPtr<CPDFSDK_Annot>* pAnnot,
393                                           uint32_t nFlags,
394                                           const CFX_PointF& point) {
395   if (!pPageView)
396     return false;
397 
398   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot->Get());
399   if (!pXFAWidget)
400     return false;
401 
402   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
403   return pWidgetHandler->OnLButtonDown(pXFAWidget->GetXFAFFWidget(),
404                                        GetFWLFlags(nFlags), point);
405 }
406 
OnLButtonUp(CPDFSDK_PageView * pPageView,ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlags,const CFX_PointF & point)407 bool CPDFXFA_WidgetHandler::OnLButtonUp(CPDFSDK_PageView* pPageView,
408                                         ObservedPtr<CPDFSDK_Annot>* pAnnot,
409                                         uint32_t nFlags,
410                                         const CFX_PointF& point) {
411   if (!pPageView)
412     return false;
413 
414   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot->Get());
415   if (!pXFAWidget)
416     return false;
417 
418   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
419   return pWidgetHandler->OnLButtonUp(pXFAWidget->GetXFAFFWidget(),
420                                      GetFWLFlags(nFlags), point);
421 }
422 
OnLButtonDblClk(CPDFSDK_PageView * pPageView,ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlags,const CFX_PointF & point)423 bool CPDFXFA_WidgetHandler::OnLButtonDblClk(CPDFSDK_PageView* pPageView,
424                                             ObservedPtr<CPDFSDK_Annot>* pAnnot,
425                                             uint32_t nFlags,
426                                             const CFX_PointF& point) {
427   if (!pPageView)
428     return false;
429 
430   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot->Get());
431   if (!pXFAWidget)
432     return false;
433 
434   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
435   return pWidgetHandler->OnLButtonDblClk(pXFAWidget->GetXFAFFWidget(),
436                                          GetFWLFlags(nFlags), point);
437 }
438 
OnMouseMove(CPDFSDK_PageView * pPageView,ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlags,const CFX_PointF & point)439 bool CPDFXFA_WidgetHandler::OnMouseMove(CPDFSDK_PageView* pPageView,
440                                         ObservedPtr<CPDFSDK_Annot>* pAnnot,
441                                         uint32_t nFlags,
442                                         const CFX_PointF& point) {
443   if (!pPageView)
444     return false;
445 
446   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot->Get());
447   if (!pXFAWidget)
448     return false;
449 
450   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
451   return pWidgetHandler->OnMouseMove(pXFAWidget->GetXFAFFWidget(),
452                                      GetFWLFlags(nFlags), point);
453 }
454 
OnMouseWheel(CPDFSDK_PageView * pPageView,ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlags,short zDelta,const CFX_PointF & point)455 bool CPDFXFA_WidgetHandler::OnMouseWheel(CPDFSDK_PageView* pPageView,
456                                          ObservedPtr<CPDFSDK_Annot>* pAnnot,
457                                          uint32_t nFlags,
458                                          short zDelta,
459                                          const CFX_PointF& point) {
460   if (!pPageView)
461     return false;
462 
463   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot->Get());
464   if (!pXFAWidget)
465     return false;
466 
467   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
468   return pWidgetHandler->OnMouseWheel(pXFAWidget->GetXFAFFWidget(),
469                                       GetFWLFlags(nFlags), zDelta, point);
470 }
471 
OnRButtonDown(CPDFSDK_PageView * pPageView,ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlags,const CFX_PointF & point)472 bool CPDFXFA_WidgetHandler::OnRButtonDown(CPDFSDK_PageView* pPageView,
473                                           ObservedPtr<CPDFSDK_Annot>* pAnnot,
474                                           uint32_t nFlags,
475                                           const CFX_PointF& point) {
476   if (!pPageView)
477     return false;
478 
479   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot->Get());
480   if (!pXFAWidget)
481     return false;
482 
483   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
484   return pWidgetHandler->OnRButtonDown(pXFAWidget->GetXFAFFWidget(),
485                                        GetFWLFlags(nFlags), point);
486 }
487 
OnRButtonUp(CPDFSDK_PageView * pPageView,ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlags,const CFX_PointF & point)488 bool CPDFXFA_WidgetHandler::OnRButtonUp(CPDFSDK_PageView* pPageView,
489                                         ObservedPtr<CPDFSDK_Annot>* pAnnot,
490                                         uint32_t nFlags,
491                                         const CFX_PointF& point) {
492   if (!pPageView)
493     return false;
494 
495   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot->Get());
496   if (!pXFAWidget)
497     return false;
498 
499   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
500   return pWidgetHandler->OnRButtonUp(pXFAWidget->GetXFAFFWidget(),
501                                      GetFWLFlags(nFlags), point);
502 }
503 
OnRButtonDblClk(CPDFSDK_PageView * pPageView,ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlags,const CFX_PointF & point)504 bool CPDFXFA_WidgetHandler::OnRButtonDblClk(CPDFSDK_PageView* pPageView,
505                                             ObservedPtr<CPDFSDK_Annot>* pAnnot,
506                                             uint32_t nFlags,
507                                             const CFX_PointF& point) {
508   if (!pPageView)
509     return false;
510 
511   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot->Get());
512   if (!pXFAWidget)
513     return false;
514 
515   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
516   return pWidgetHandler->OnRButtonDblClk(pXFAWidget->GetXFAFFWidget(),
517                                          GetFWLFlags(nFlags), point);
518 }
519 
OnChar(CPDFSDK_Annot * pAnnot,uint32_t nChar,uint32_t nFlags)520 bool CPDFXFA_WidgetHandler::OnChar(CPDFSDK_Annot* pAnnot,
521                                    uint32_t nChar,
522                                    uint32_t nFlags) {
523   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
524   if (!pXFAWidget)
525     return false;
526 
527   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
528   return pWidgetHandler->OnChar(pXFAWidget->GetXFAFFWidget(), nChar,
529                                 GetFWLFlags(nFlags));
530 }
531 
OnKeyDown(CPDFSDK_Annot * pAnnot,int nKeyCode,int nFlag)532 bool CPDFXFA_WidgetHandler::OnKeyDown(CPDFSDK_Annot* pAnnot,
533                                       int nKeyCode,
534                                       int nFlag) {
535   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
536   if (!pXFAWidget)
537     return false;
538 
539   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
540   return pWidgetHandler->OnKeyDown(pXFAWidget->GetXFAFFWidget(), nKeyCode,
541                                    GetFWLFlags(nFlag));
542 }
543 
OnKeyUp(CPDFSDK_Annot * pAnnot,int nKeyCode,int nFlag)544 bool CPDFXFA_WidgetHandler::OnKeyUp(CPDFSDK_Annot* pAnnot,
545                                     int nKeyCode,
546                                     int nFlag) {
547   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
548   if (!pXFAWidget)
549     return false;
550 
551   CXFA_FFWidgetHandler* pWidgetHandler = GetXFAFFWidgetHandler(pXFAWidget);
552   return pWidgetHandler->OnKeyUp(pXFAWidget->GetXFAFFWidget(), nKeyCode,
553                                  GetFWLFlags(nFlag));
554 }
555 
OnSetFocus(ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlag)556 bool CPDFXFA_WidgetHandler::OnSetFocus(ObservedPtr<CPDFSDK_Annot>* pAnnot,
557                                        uint32_t nFlag) {
558   return true;
559 }
560 
OnKillFocus(ObservedPtr<CPDFSDK_Annot> * pAnnot,uint32_t nFlag)561 bool CPDFXFA_WidgetHandler::OnKillFocus(ObservedPtr<CPDFSDK_Annot>* pAnnot,
562                                         uint32_t nFlag) {
563   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot->Get());
564   if (!pXFAWidget)
565     return true;
566 
567   CXFA_FFWidget* hWidget = pXFAWidget->GetXFAFFWidget();
568   if (!hWidget)
569     return true;
570 
571   CXFA_FFPageView* pXFAPageView = hWidget->GetPageView();
572   if (!pXFAPageView)
573     return true;
574 
575   pXFAPageView->GetDocView()->SetFocus(nullptr);
576   return true;
577 }
578 
OnXFAChangedFocus(ObservedPtr<CPDFSDK_Annot> * pOldAnnot,ObservedPtr<CPDFSDK_Annot> * pNewAnnot)579 bool CPDFXFA_WidgetHandler::OnXFAChangedFocus(
580     ObservedPtr<CPDFSDK_Annot>* pOldAnnot,
581     ObservedPtr<CPDFSDK_Annot>* pNewAnnot) {
582   CXFA_FFWidgetHandler* pWidgetHandler = nullptr;
583   if (pOldAnnot->HasObservable())
584     pWidgetHandler = GetXFAFFWidgetHandler(pOldAnnot->Get());
585   else if (pNewAnnot->HasObservable())
586     pWidgetHandler = GetXFAFFWidgetHandler(pNewAnnot->Get());
587 
588   if (!pWidgetHandler)
589     return true;
590 
591   CPDFXFA_Widget* pNewXFAWidget = ToXFAWidget(pNewAnnot->Get());
592   if (!pNewXFAWidget)
593     return true;
594 
595   CXFA_FFWidget* hWidget = pNewXFAWidget->GetXFAFFWidget();
596   if (!hWidget)
597     return true;
598 
599   CXFA_FFPageView* pXFAPageView = hWidget->GetPageView();
600   if (!pXFAPageView)
601     return true;
602 
603   ObservedPtr<CXFA_FFPageView> pObservedXFAPageView(pXFAPageView);
604   bool bRet = pXFAPageView->GetDocView()->SetFocus(hWidget);
605 
606   // Check |pXFAPageView| again because |SetFocus| can trigger JS to destroy it.
607   if (pObservedXFAPageView &&
608       pXFAPageView->GetDocView()->GetFocusWidget() == hWidget) {
609     bRet = true;
610   }
611 
612   return bRet;
613 }
614 
SetIndexSelected(ObservedPtr<CPDFSDK_Annot> * pAnnot,int index,bool selected)615 bool CPDFXFA_WidgetHandler::SetIndexSelected(ObservedPtr<CPDFSDK_Annot>* pAnnot,
616                                              int index,
617                                              bool selected) {
618   return false;
619 }
620 
IsIndexSelected(ObservedPtr<CPDFSDK_Annot> * pAnnot,int index)621 bool CPDFXFA_WidgetHandler::IsIndexSelected(ObservedPtr<CPDFSDK_Annot>* pAnnot,
622                                             int index) {
623   return false;
624 }
625 
GetXFAFFWidgetHandler(CPDFSDK_Annot * pAnnot)626 CXFA_FFWidgetHandler* CPDFXFA_WidgetHandler::GetXFAFFWidgetHandler(
627     CPDFSDK_Annot* pAnnot) {
628   if (!pAnnot)
629     return nullptr;
630 
631   CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
632   if (!pPageView)
633     return nullptr;
634 
635   CPDFSDK_FormFillEnvironment* pFormFillEnv = pPageView->GetFormFillEnv();
636   if (!pFormFillEnv)
637     return nullptr;
638 
639   auto* pDoc = static_cast<CPDFXFA_Context*>(pFormFillEnv->GetDocExtension());
640   if (!pDoc)
641     return nullptr;
642 
643   CXFA_FFDocView* pDocView = pDoc->GetXFADocView();
644   if (!pDocView)
645     return nullptr;
646 
647   return pDocView->GetWidgetHandler();
648 }
649 
GetFWLFlags(uint32_t dwFlag)650 uint32_t CPDFXFA_WidgetHandler::GetFWLFlags(uint32_t dwFlag) {
651   uint32_t dwFWLFlag = 0;
652 
653   if (dwFlag & FWL_EVENTFLAG_ControlKey)
654     dwFWLFlag |= FWL_KEYFLAG_Ctrl;
655   if (dwFlag & FWL_EVENTFLAG_LeftButtonDown)
656     dwFWLFlag |= FWL_KEYFLAG_LButton;
657   if (dwFlag & FWL_EVENTFLAG_MiddleButtonDown)
658     dwFWLFlag |= FWL_KEYFLAG_MButton;
659   if (dwFlag & FWL_EVENTFLAG_RightButtonDown)
660     dwFWLFlag |= FWL_KEYFLAG_RButton;
661   if (dwFlag & FWL_EVENTFLAG_ShiftKey)
662     dwFWLFlag |= FWL_KEYFLAG_Shift;
663   if (dwFlag & FWL_EVENTFLAG_AltKey)
664     dwFWLFlag |= FWL_KEYFLAG_Alt;
665 
666   return dwFWLFlag;
667 }
668