c5f2b95533fe4d47f171ad72e3d83696b2623692
[pdfium.git] / fpdfsdk / include / fsdk_annothandler.h
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 #ifndef FPDFSDK_INCLUDE_FSDK_ANNOTHANDLER_H_
8 #define FPDFSDK_INCLUDE_FSDK_ANNOTHANDLER_H_
9
10 #include "../../core/include/fxcrt/fx_basic.h"
11
12 class CFFL_IFormFiller;
13 class CFX_RenderDevice;
14 class CPDFDoc_Environment;
15 class CPDFSDK_Annot;
16 class CPDFSDK_PageView;
17 class CPDF_Annot;
18 class CPDF_Matrix;
19 class CPDF_Point;
20 class CPDF_Rect;
21
22 class IPDFSDK_AnnotHandler {
23  public:
24   virtual ~IPDFSDK_AnnotHandler() {}
25
26   virtual CFX_ByteString GetType() = 0;
27
28   virtual CFX_ByteString GetName() = 0;
29
30   virtual FX_BOOL CanAnswer(CPDFSDK_Annot* pAnnot) = 0;
31
32   virtual CPDFSDK_Annot* NewAnnot(CPDF_Annot* pAnnot,
33                                   CPDFSDK_PageView* pPage) = 0;
34
35   virtual void ReleaseAnnot(CPDFSDK_Annot* pAnnot) = 0;
36
37   virtual void DeleteAnnot(CPDFSDK_Annot* pAnnot) = 0;
38
39   virtual CPDF_Rect GetViewBBox(CPDFSDK_PageView* pPageView,
40                                 CPDFSDK_Annot* pAnnot) = 0;
41
42   virtual FX_BOOL HitTest(CPDFSDK_PageView* pPageView,
43                           CPDFSDK_Annot* pAnnot,
44                           const CPDF_Point& point) = 0;
45
46   virtual void OnDraw(CPDFSDK_PageView* pPageView,
47                       CPDFSDK_Annot* pAnnot,
48                       CFX_RenderDevice* pDevice,
49                       CPDF_Matrix* pUser2Device,
50                       FX_DWORD dwFlags) = 0;
51
52   virtual void OnDrawSleep(CPDFSDK_PageView* pPageView,
53                            CPDFSDK_Annot* pAnnot,
54                            CFX_RenderDevice* pDevice,
55                            CPDF_Matrix* pUser2Device,
56                            const CPDF_Rect& rcWindow,
57                            FX_DWORD dwFlags) = 0;
58
59   virtual void OnCreate(CPDFSDK_Annot* pAnnot) = 0;
60
61   virtual void OnLoad(CPDFSDK_Annot* pAnnot) = 0;
62
63   virtual void OnDelete(CPDFSDK_Annot* pAnnot) = 0;
64
65   virtual void OnRelease(CPDFSDK_Annot* pAnnot) = 0;
66
67   virtual void OnMouseEnter(CPDFSDK_PageView* pPageView,
68                             CPDFSDK_Annot* pAnnot,
69                             FX_DWORD nFlag) = 0;
70   virtual void OnMouseExit(CPDFSDK_PageView* pPageView,
71                            CPDFSDK_Annot* pAnnot,
72                            FX_DWORD nFlag) = 0;
73
74   virtual FX_BOOL OnLButtonDown(CPDFSDK_PageView* pPageView,
75                                 CPDFSDK_Annot* pAnnot,
76                                 FX_DWORD nFlags,
77                                 const CPDF_Point& point) = 0;
78   virtual FX_BOOL OnLButtonUp(CPDFSDK_PageView* pPageView,
79                               CPDFSDK_Annot* pAnnot,
80                               FX_DWORD nFlags,
81                               const CPDF_Point& point) = 0;
82   virtual FX_BOOL OnLButtonDblClk(CPDFSDK_PageView* pPageView,
83                                   CPDFSDK_Annot* pAnnot,
84                                   FX_DWORD nFlags,
85                                   const CPDF_Point& point) = 0;
86   virtual FX_BOOL OnMouseMove(CPDFSDK_PageView* pPageView,
87                               CPDFSDK_Annot* pAnnot,
88                               FX_DWORD nFlags,
89                               const CPDF_Point& point) = 0;
90   virtual FX_BOOL OnMouseWheel(CPDFSDK_PageView* pPageView,
91                                CPDFSDK_Annot* pAnnot,
92                                FX_DWORD nFlags,
93                                short zDelta,
94                                const CPDF_Point& point) = 0;
95   virtual FX_BOOL OnRButtonDown(CPDFSDK_PageView* pPageView,
96                                 CPDFSDK_Annot* pAnnot,
97                                 FX_DWORD nFlags,
98                                 const CPDF_Point& point) = 0;
99   virtual FX_BOOL OnRButtonUp(CPDFSDK_PageView* pPageView,
100                               CPDFSDK_Annot* pAnnot,
101                               FX_DWORD nFlags,
102                               const CPDF_Point& point) = 0;
103   virtual FX_BOOL OnRButtonDblClk(CPDFSDK_PageView* pPageView,
104                                   CPDFSDK_Annot* pAnnot,
105                                   FX_DWORD nFlags,
106                                   const CPDF_Point& point) = 0;
107   // by wjm.
108   virtual FX_BOOL OnChar(CPDFSDK_Annot* pAnnot,
109                          FX_DWORD nChar,
110                          FX_DWORD nFlags) = 0;
111   virtual FX_BOOL OnKeyDown(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag) = 0;
112   virtual FX_BOOL OnKeyUp(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag) = 0;
113
114   virtual void OnDeSelected(CPDFSDK_Annot* pAnnot) = 0;
115   virtual void OnSelected(CPDFSDK_Annot* pAnnot) = 0;
116
117   virtual FX_BOOL OnSetFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag) = 0;
118   virtual FX_BOOL OnKillFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag) = 0;
119 };
120
121 class CPDFSDK_BFAnnotHandler : public IPDFSDK_AnnotHandler {
122  public:
123   CPDFSDK_BFAnnotHandler(CPDFDoc_Environment* pApp)
124       : m_pApp(pApp), m_pFormFiller(NULL) {}
125   ~CPDFSDK_BFAnnotHandler() override {}
126
127   // IPDFSDK_AnnotHandler
128   CFX_ByteString GetType() override { return CFX_ByteString("Widget"); }
129   CFX_ByteString GetName() override { return CFX_ByteString("WidgetHandler"); }
130   FX_BOOL CanAnswer(CPDFSDK_Annot* pAnnot) override;
131   CPDFSDK_Annot* NewAnnot(CPDF_Annot* pAnnot, CPDFSDK_PageView* pPage) override;
132   void ReleaseAnnot(CPDFSDK_Annot* pAnnot) override;
133   void DeleteAnnot(CPDFSDK_Annot* pAnnot) override {}
134   CPDF_Rect GetViewBBox(CPDFSDK_PageView* pPageView,
135                         CPDFSDK_Annot* pAnnot) override;
136   FX_BOOL HitTest(CPDFSDK_PageView* pPageView,
137                   CPDFSDK_Annot* pAnnot,
138                   const CPDF_Point& point) override;
139   void OnDraw(CPDFSDK_PageView* pPageView,
140               CPDFSDK_Annot* pAnnot,
141               CFX_RenderDevice* pDevice,
142               CPDF_Matrix* pUser2Device,
143               FX_DWORD dwFlags) override;
144   void OnDrawSleep(CPDFSDK_PageView* pPageView,
145                    CPDFSDK_Annot* pAnnot,
146                    CFX_RenderDevice* pDevice,
147                    CPDF_Matrix* pUser2Device,
148                    const CPDF_Rect& rcWindow,
149                    FX_DWORD dwFlags) override {}
150   void OnCreate(CPDFSDK_Annot* pAnnot) override;
151   void OnLoad(CPDFSDK_Annot* pAnnot) override;
152   void OnDelete(CPDFSDK_Annot* pAnnot) override {}
153   void OnRelease(CPDFSDK_Annot* pAnnot) override {}
154   void OnMouseEnter(CPDFSDK_PageView* pPageView,
155                     CPDFSDK_Annot* pAnnot,
156                     FX_DWORD nFlag) override;
157   void OnMouseExit(CPDFSDK_PageView* pPageView,
158                    CPDFSDK_Annot* pAnnot,
159                    FX_DWORD nFlag) override;
160   FX_BOOL OnLButtonDown(CPDFSDK_PageView* pPageView,
161                         CPDFSDK_Annot* pAnnot,
162                         FX_DWORD nFlags,
163                         const CPDF_Point& point) override;
164   FX_BOOL OnLButtonUp(CPDFSDK_PageView* pPageView,
165                       CPDFSDK_Annot* pAnnot,
166                       FX_DWORD nFlags,
167                       const CPDF_Point& point) override;
168   FX_BOOL OnLButtonDblClk(CPDFSDK_PageView* pPageView,
169                           CPDFSDK_Annot* pAnnot,
170                           FX_DWORD nFlags,
171                           const CPDF_Point& point) override;
172   FX_BOOL OnMouseMove(CPDFSDK_PageView* pPageView,
173                       CPDFSDK_Annot* pAnnot,
174                       FX_DWORD nFlags,
175                       const CPDF_Point& point) override;
176   FX_BOOL OnMouseWheel(CPDFSDK_PageView* pPageView,
177                        CPDFSDK_Annot* pAnnot,
178                        FX_DWORD nFlags,
179                        short zDelta,
180                        const CPDF_Point& point) override;
181   FX_BOOL OnRButtonDown(CPDFSDK_PageView* pPageView,
182                         CPDFSDK_Annot* pAnnot,
183                         FX_DWORD nFlags,
184                         const CPDF_Point& point) override;
185   FX_BOOL OnRButtonUp(CPDFSDK_PageView* pPageView,
186                       CPDFSDK_Annot* pAnnot,
187                       FX_DWORD nFlags,
188                       const CPDF_Point& point) override;
189   FX_BOOL OnRButtonDblClk(CPDFSDK_PageView* pPageView,
190                           CPDFSDK_Annot* pAnnot,
191                           FX_DWORD nFlags,
192                           const CPDF_Point& point) override {
193     return FALSE;
194   }
195   FX_BOOL OnChar(CPDFSDK_Annot* pAnnot,
196                  FX_DWORD nChar,
197                  FX_DWORD nFlags) override;
198   FX_BOOL OnKeyDown(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag) override;
199   FX_BOOL OnKeyUp(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag) override;
200   void OnDeSelected(CPDFSDK_Annot* pAnnot) override {}
201   void OnSelected(CPDFSDK_Annot* pAnnot) override {}
202   FX_BOOL OnSetFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag) override;
203   FX_BOOL OnKillFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag) override;
204
205   void SetFormFiller(CFFL_IFormFiller* pFiller) { m_pFormFiller = pFiller; }
206   CFFL_IFormFiller* GetFormFiller() { return m_pFormFiller; }
207
208  private:
209   CPDFDoc_Environment* m_pApp;
210   CFFL_IFormFiller* m_pFormFiller;
211 };
212
213 #define CBA_AnnotHandlerArray CFX_ArrayTemplate<IPDFSDK_AnnotHandler*>
214 class CPDFSDK_AnnotHandlerMgr {
215  public:
216   // Destroy the handler
217   explicit CPDFSDK_AnnotHandlerMgr(CPDFDoc_Environment* pApp);
218   virtual ~CPDFSDK_AnnotHandlerMgr();
219
220  public:
221   void RegisterAnnotHandler(IPDFSDK_AnnotHandler* pAnnotHandler);
222   void UnRegisterAnnotHandler(IPDFSDK_AnnotHandler* pAnnotHandler);
223
224   virtual CPDFSDK_Annot* NewAnnot(CPDF_Annot* pAnnot,
225                                   CPDFSDK_PageView* pPageView);
226   virtual void ReleaseAnnot(CPDFSDK_Annot* pAnnot);
227
228   virtual void Annot_OnCreate(CPDFSDK_Annot* pAnnot);
229   virtual void Annot_OnLoad(CPDFSDK_Annot* pAnnot);
230
231  public:
232   IPDFSDK_AnnotHandler* GetAnnotHandler(CPDFSDK_Annot* pAnnot) const;
233   virtual void Annot_OnDraw(CPDFSDK_PageView* pPageView,
234                             CPDFSDK_Annot* pAnnot,
235                             CFX_RenderDevice* pDevice,
236                             CPDF_Matrix* pUser2Device,
237                             FX_DWORD dwFlags);
238
239   virtual void Annot_OnMouseEnter(CPDFSDK_PageView* pPageView,
240                                   CPDFSDK_Annot* pAnnot,
241                                   FX_DWORD nFlags);
242   virtual void Annot_OnMouseExit(CPDFSDK_PageView* pPageView,
243                                  CPDFSDK_Annot* pAnnot,
244                                  FX_DWORD nFlags);
245
246   virtual FX_BOOL Annot_OnLButtonDown(CPDFSDK_PageView* pPageView,
247                                       CPDFSDK_Annot* pAnnot,
248                                       FX_DWORD nFlags,
249                                       const CPDF_Point& point);
250   virtual FX_BOOL Annot_OnLButtonUp(CPDFSDK_PageView* pPageView,
251                                     CPDFSDK_Annot* pAnnot,
252                                     FX_DWORD nFlags,
253                                     const CPDF_Point& point);
254   virtual FX_BOOL Annot_OnLButtonDblClk(CPDFSDK_PageView* pPageView,
255                                         CPDFSDK_Annot* pAnnot,
256                                         FX_DWORD nFlags,
257                                         const CPDF_Point& point);
258
259   virtual FX_BOOL Annot_OnMouseMove(CPDFSDK_PageView* pPageView,
260                                     CPDFSDK_Annot* pAnnot,
261                                     FX_DWORD nFlags,
262                                     const CPDF_Point& point);
263   virtual FX_BOOL Annot_OnMouseWheel(CPDFSDK_PageView* pPageView,
264                                      CPDFSDK_Annot* pAnnot,
265                                      FX_DWORD nFlags,
266                                      short zDelta,
267                                      const CPDF_Point& point);
268   virtual FX_BOOL Annot_OnRButtonDown(CPDFSDK_PageView* pPageView,
269                                       CPDFSDK_Annot* pAnnot,
270                                       FX_DWORD nFlags,
271                                       const CPDF_Point& point);
272   virtual FX_BOOL Annot_OnRButtonUp(CPDFSDK_PageView* pPageView,
273                                     CPDFSDK_Annot* pAnnot,
274                                     FX_DWORD nFlags,
275                                     const CPDF_Point& point);
276
277   virtual FX_BOOL Annot_OnChar(CPDFSDK_Annot* pAnnot,
278                                FX_DWORD nChar,
279                                FX_DWORD nFlags);
280   virtual FX_BOOL Annot_OnKeyDown(CPDFSDK_Annot* pAnnot,
281                                   int nKeyCode,
282                                   int nFlag);
283   virtual FX_BOOL Annot_OnKeyUp(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag);
284
285   virtual FX_BOOL Annot_OnSetFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag);
286   virtual FX_BOOL Annot_OnKillFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag);
287
288   virtual CPDF_Rect Annot_OnGetViewBBox(CPDFSDK_PageView* pPageView,
289                                         CPDFSDK_Annot* pAnnot);
290   virtual FX_BOOL Annot_OnHitTest(CPDFSDK_PageView* pPageView,
291                                   CPDFSDK_Annot* pAnnot,
292                                   const CPDF_Point& point);
293
294  private:
295   IPDFSDK_AnnotHandler* GetAnnotHandler(const CFX_ByteString& sType) const;
296   CPDFSDK_Annot* GetNextAnnot(CPDFSDK_Annot* pSDKAnnot, FX_BOOL bNext);
297
298  private:
299   CBA_AnnotHandlerArray m_Handlers;
300   CFX_MapByteStringToPtr m_mapType2Handler;
301   CPDFDoc_Environment* m_pApp;
302 };
303
304 typedef int (*AI_COMPARE)(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
305
306 class CPDFSDK_AnnotIterator {
307  public:
308   CPDFSDK_AnnotIterator(CPDFSDK_PageView* pPageView,
309                         FX_BOOL bReverse,
310                         FX_BOOL bIgnoreTopmost = FALSE,
311                         FX_BOOL bCircle = FALSE,
312                         CFX_PtrArray* pList = NULL);
313   virtual ~CPDFSDK_AnnotIterator() {}
314
315   virtual CPDFSDK_Annot* Next(const CPDFSDK_Annot* pCurrent);
316   virtual CPDFSDK_Annot* Prev(const CPDFSDK_Annot* pCurrent);
317   virtual CPDFSDK_Annot* Next(int& index);
318   virtual CPDFSDK_Annot* Prev(int& index);
319   virtual int Count() { return m_pIteratorAnnotList.GetSize(); }
320
321   virtual FX_BOOL InitIteratorAnnotList(CPDFSDK_PageView* pPageView,
322                                         CFX_PtrArray* pList = NULL);
323
324   void InsertSort(CFX_PtrArray& arrayList, AI_COMPARE pCompare);
325
326  protected:
327   CPDFSDK_Annot* NextAnnot(const CPDFSDK_Annot* pCurrent);
328   CPDFSDK_Annot* PrevAnnot(const CPDFSDK_Annot* pCurrent);
329   CPDFSDK_Annot* NextAnnot(int& index);
330   CPDFSDK_Annot* PrevAnnot(int& index);
331
332   CFX_PtrArray m_pIteratorAnnotList;
333   FX_BOOL m_bReverse;
334   FX_BOOL m_bIgnoreTopmost;
335   FX_BOOL m_bCircle;
336 };
337
338 #endif  // FPDFSDK_INCLUDE_FSDK_ANNOTHANDLER_H_