beta-0.89.2
[luatex.git] / source / libs / poppler / poppler-src / poppler / Form.h
blob3778ff6f2ef9709484e801151f78e50b7ea7cd30
1 //========================================================================
2 //
3 // Form.h
4 //
5 // This file is licensed under the GPLv2 or later
6 //
7 // Copyright 2006 Julien Rebetez <julienr@svn.gnome.org>
8 // Copyright 2007, 2008, 2011 Carlos Garcia Campos <carlosgc@gnome.org>
9 // Copyright 2007-2010, 2012 Albert Astals Cid <aacid@kde.org>
10 // Copyright 2010 Mark Riedesel <mark@klowner.com>
11 // Copyright 2011 Pino Toscano <pino@kde.org>
12 // Copyright 2012 Fabio D'Urso <fabiodurso@hotmail.it>
13 // Copyright 2013 Adrian Johnson <ajohnson@redneon.com>
15 //========================================================================
17 #ifndef FORM_H
18 #define FORM_H
20 #ifdef USE_GCC_PRAGMAS
21 #pragma interface
22 #endif
24 #include "Object.h"
25 #include "Annot.h"
27 #include <set>
29 class GooString;
30 class Array;
31 class Dict;
32 class Annot;
33 class AnnotWidget;
34 class Annots;
35 class LinkAction;
36 class GfxResources;
37 class PDFDoc;
39 enum FormFieldType {
40 formButton,
41 formText,
42 formChoice,
43 formSignature,
44 formUndef
47 enum FormButtonType {
48 formButtonCheck,
49 formButtonPush,
50 formButtonRadio
53 enum VariableTextQuadding {
54 quaddingLeftJustified,
55 quaddingCentered,
56 quaddingRightJustified
59 class Form;
60 class FormField;
61 class FormFieldButton;
62 class FormFieldText;
63 class FormFieldSignature;
64 class FormFieldChoice;
66 //------------------------------------------------------------------------
67 // FormWidget
68 // A FormWidget represents the graphical part of a field and is "attached"
69 // to a page.
70 //------------------------------------------------------------------------
72 class FormWidget {
73 public:
74 virtual ~FormWidget();
76 // Check if point is inside the field bounding rect
77 GBool inRect(double x, double y) const;
79 // Get the field bounding rect
80 void getRect(double *x1, double *y1, double *x2, double *y2) const;
82 unsigned getID () { return ID; }
83 void setID (unsigned int i) { ID=i; }
85 FormField *getField () { return field; }
86 FormFieldType getType() { return type; }
88 Object* getObj() { return &obj; }
89 Ref getRef() { return ref; }
91 void setChildNum (unsigned i) { childNum = i; }
92 unsigned getChildNum () { return childNum; }
94 double getFontSize() const;
96 GooString *getPartialName() const;
97 GooString *getAlternateUiName() const;
98 GooString *getMappingName() const;
99 GooString *getFullyQualifiedName();
101 GBool isModified () const;
103 bool isReadOnly() const;
105 LinkAction *getActivationAction();
106 LinkAction *getAdditionalAction(Annot::FormAdditionalActionsType type);
108 // return the unique ID corresponding to pageNum/fieldNum
109 static int encodeID (unsigned pageNum, unsigned fieldNum);
110 // decode id and retrieve pageNum and fieldNum
111 static void decodeID (unsigned id, unsigned* pageNum, unsigned* fieldNum);
113 void createWidgetAnnotation();
114 AnnotWidget *getWidgetAnnotation() const { return widget; }
116 virtual void updateWidgetAppearance() = 0;
118 #ifdef DEBUG_FORMS
119 void print(int indent = 0);
120 #endif
122 protected:
123 FormWidget(PDFDoc *docA, Object *aobj, unsigned num, Ref aref, FormField *fieldA);
125 AnnotWidget *widget;
126 FormField* field;
127 FormFieldType type;
128 Object obj;
129 Ref ref;
130 PDFDoc *doc;
131 XRef *xref;
133 //index of this field in the parent's child list
134 unsigned childNum;
137 Field ID is an (unsigned) integer, calculated as follow :
138 the first sizeof/2 bits are the field number, relative to the page
139 the last sizeof/2 bits are the page number
140 [page number | field number]
141 (encoding) id = (pageNum << 4*sizeof(unsigned)) + fieldNum;
142 (decoding) pageNum = id >> 4*sizeof(unsigned); fieldNum = (id << 4*sizeof(unsigned)) >> 4*sizeof(unsigned);
144 unsigned ID;
147 //------------------------------------------------------------------------
148 // FormWidgetButton
149 //------------------------------------------------------------------------
151 class FormWidgetButton: public FormWidget {
152 public:
153 FormWidgetButton(PDFDoc *docA, Object *dict, unsigned num, Ref ref, FormField *p);
154 ~FormWidgetButton ();
156 FormButtonType getButtonType() const;
158 void setState (GBool state);
159 GBool getState ();
161 char* getOnStr();
162 void setAppearanceState(const char *state);
163 void updateWidgetAppearance();
165 protected:
166 GooString *onStr;
167 FormFieldButton *parent;
170 //------------------------------------------------------------------------
171 // FormWidgetText
172 //------------------------------------------------------------------------
174 class FormWidgetText: public FormWidget {
175 public:
176 FormWidgetText(PDFDoc *docA, Object *dict, unsigned num, Ref ref, FormField *p);
177 //return the field's content (UTF16BE)
178 GooString* getContent() ;
179 //return a copy of the field's content (UTF16BE)
180 GooString* getContentCopy();
182 //except a UTF16BE string
183 void setContent(GooString* new_content);
185 void updateWidgetAppearance();
187 bool isMultiline () const;
188 bool isPassword () const;
189 bool isFileSelect () const;
190 bool noSpellCheck () const;
191 bool noScroll () const;
192 bool isComb () const;
193 bool isRichText () const;
194 int getMaxLen () const;
195 protected:
196 FormFieldText *parent;
199 //------------------------------------------------------------------------
200 // FormWidgetChoice
201 //------------------------------------------------------------------------
203 class FormWidgetChoice: public FormWidget {
204 public:
205 FormWidgetChoice(PDFDoc *docA, Object *dict, unsigned num, Ref ref, FormField *p);
206 ~FormWidgetChoice();
208 int getNumChoices();
209 //return the display name of the i-th choice (UTF16BE)
210 GooString* getChoice(int i);
211 //select the i-th choice
212 void select (int i);
214 //toggle selection of the i-th choice
215 void toggle (int i);
217 //deselect everything
218 void deselectAll ();
220 //except a UTF16BE string
221 //only work for editable combo box, set the user-entered text as the current choice
222 void setEditChoice(GooString* new_content);
224 GooString* getEditChoice ();
226 void updateWidgetAppearance();
227 bool isSelected (int i);
229 bool isCombo () const;
230 bool hasEdit () const;
231 bool isMultiSelect () const;
232 bool noSpellCheck () const;
233 bool commitOnSelChange () const;
234 bool isListBox () const;
235 protected:
236 bool _checkRange (int i);
237 FormFieldChoice *parent;
240 //------------------------------------------------------------------------
241 // FormWidgetSignature
242 //------------------------------------------------------------------------
244 class FormWidgetSignature: public FormWidget {
245 public:
246 FormWidgetSignature(PDFDoc *docA, Object *dict, unsigned num, Ref ref, FormField *p);
247 void updateWidgetAppearance();
248 protected:
249 FormFieldSignature *parent;
252 //------------------------------------------------------------------------
253 // FormField
254 // A FormField implements the logical side of a field and is "attached" to
255 // the Catalog. This is an internal class and client applications should
256 // only interact with FormWidgets.
257 //------------------------------------------------------------------------
259 class FormField {
260 public:
261 FormField(PDFDoc *docA, Object *aobj, const Ref& aref, FormField *parent, std::set<int> *usedParents, FormFieldType t=formUndef);
263 virtual ~FormField();
265 // Accessors.
266 FormFieldType getType() { return type; }
267 Object* getObj() { return &obj; }
268 Ref getRef() { return ref; }
270 void setReadOnly (bool b) { readOnly = b; }
271 bool isReadOnly () const { return readOnly; }
273 GooString* getDefaultAppearance() const { return defaultAppearance; }
274 GBool hasTextQuadding() const { return hasQuadding; }
275 VariableTextQuadding getTextQuadding() const { return quadding; }
277 GooString *getPartialName() const { return partialName; }
278 GooString *getAlternateUiName() const { return alternateUiName; }
279 GooString *getMappingName() const { return mappingName; }
280 GooString *getFullyQualifiedName();
282 FormWidget* findWidgetByRef (Ref aref);
283 int getNumWidgets() { return terminal ? numChildren : 0; }
284 FormWidget *getWidget(int i) { return terminal ? widgets[i] : NULL; }
286 // only implemented in FormFieldButton
287 virtual void fillChildrenSiblingsID ();
289 void createWidgetAnnotations();
291 #ifdef DEBUG_FORMS
292 void printTree(int indent = 0);
293 virtual void print(int indent = 0);
294 #endif
297 protected:
298 void _createWidget (Object *obj, Ref aref);
299 void createChildren(std::set<int> *usedParents);
300 void updateChildrenAppearance();
302 FormFieldType type; // field type
303 Ref ref;
304 bool terminal;
305 Object obj;
306 PDFDoc *doc;
307 XRef *xref;
308 FormField **children;
309 FormField *parent;
310 int numChildren;
311 FormWidget **widgets;
312 bool readOnly;
314 GooString *partialName; // T field
315 GooString *alternateUiName; // TU field
316 GooString *mappingName; // TM field
317 GooString *fullyQualifiedName;
319 // Variable Text
320 GooString *defaultAppearance;
321 GBool hasQuadding;
322 VariableTextQuadding quadding;
324 private:
325 FormField() {}
329 //------------------------------------------------------------------------
330 // FormFieldButton
331 //------------------------------------------------------------------------
333 class FormFieldButton: public FormField {
334 public:
335 FormFieldButton(PDFDoc *docA, Object *dict, const Ref& ref, FormField *parent, std::set<int> *usedParents);
337 FormButtonType getButtonType () { return btype; }
339 bool noToggleToOff () const { return noAllOff; }
341 // returns gTrue if the state modification is accepted
342 GBool setState (char *state);
343 GBool getState(char *state);
345 char *getAppearanceState() { return appearanceState.isName() ? appearanceState.getName() : NULL; }
347 void fillChildrenSiblingsID ();
349 void setNumSiblings (int num);
350 void setSibling (int i, FormFieldButton *id) { siblings[i] = id; }
352 //For radio buttons, return the fields of the other radio buttons in the same group
353 FormFieldButton* getSibling (int i) const { return siblings[i]; }
354 int getNumSiblings () const { return numSiblings; }
356 #ifdef DEBUG_FORMS
357 void print(int indent = 0);
358 #endif
360 virtual ~FormFieldButton();
361 protected:
362 void updateState(char *state);
364 FormFieldButton** siblings; // IDs of dependent buttons (each button of a radio field has all the others buttons
365 // of the same field in this array)
366 int numSiblings;
368 FormButtonType btype;
369 int size;
370 int active_child; //only used for combo box
371 bool noAllOff;
372 Object appearanceState; // V
375 //------------------------------------------------------------------------
376 // FormFieldText
377 //------------------------------------------------------------------------
379 class FormFieldText: public FormField {
380 public:
381 FormFieldText(PDFDoc *docA, Object *dict, const Ref& ref, FormField *parent, std::set<int> *usedParents);
383 GooString* getContent () { return content; }
384 GooString* getContentCopy ();
385 void setContentCopy (GooString* new_content);
386 virtual ~FormFieldText();
388 bool isMultiline () const { return multiline; }
389 bool isPassword () const { return password; }
390 bool isFileSelect () const { return fileSelect; }
391 bool noSpellCheck () const { return doNotSpellCheck; }
392 bool noScroll () const { return doNotScroll; }
393 bool isComb () const { return comb; }
394 bool isRichText () const { return richText; }
396 int getMaxLen () const { return maxLen; }
398 #ifdef DEBUG_FORMS
399 void print(int indent = 0);
400 #endif
401 protected:
402 GooString* content;
403 bool multiline;
404 bool password;
405 bool fileSelect;
406 bool doNotSpellCheck;
407 bool doNotScroll;
408 bool comb;
409 bool richText;
410 int maxLen;
413 //------------------------------------------------------------------------
414 // FormFieldChoice
415 //------------------------------------------------------------------------
417 class FormFieldChoice: public FormField {
418 public:
419 FormFieldChoice(PDFDoc *docA, Object *aobj, const Ref& ref, FormField *parent, std::set<int> *usedParents);
421 virtual ~FormFieldChoice();
423 int getNumChoices() { return numChoices; }
424 GooString* getChoice(int i) { return choices ? choices[i].optionName : NULL; }
425 GooString* getExportVal (int i) { return choices ? choices[i].exportVal : NULL; }
426 // For multi-select choices it returns the first one
427 GooString* getSelectedChoice();
429 //select the i-th choice
430 void select (int i);
432 //toggle selection of the i-th choice
433 void toggle (int i);
435 //deselect everything
436 void deselectAll ();
438 //only work for editable combo box, set the user-entered text as the current choice
439 void setEditChoice(GooString* new_content);
441 GooString* getEditChoice ();
443 bool isSelected (int i) { return choices[i].selected; }
445 int getNumSelected ();
447 bool isCombo () const { return combo; }
448 bool hasEdit () const { return edit; }
449 bool isMultiSelect () const { return multiselect; }
450 bool noSpellCheck () const { return doNotSpellCheck; }
451 bool commitOnSelChange () const { return doCommitOnSelChange; }
452 bool isListBox () const { return !combo; }
454 int getTopIndex() const { return topIdx; }
456 #ifdef DEBUG_FORMS
457 void print(int indent = 0);
458 #endif
460 protected:
461 void unselectAll();
462 void updateSelection();
464 bool combo;
465 bool edit;
466 bool multiselect;
467 bool doNotSpellCheck;
468 bool doCommitOnSelChange;
470 struct ChoiceOpt {
471 GooString* exportVal; //the export value ("internal" name)
472 GooString* optionName; //displayed name
473 bool selected; //if this choice is selected
476 int numChoices;
477 ChoiceOpt* choices;
478 GooString* editedChoice;
479 int topIdx; // TI
482 //------------------------------------------------------------------------
483 // FormFieldSignature
484 //------------------------------------------------------------------------
486 class FormFieldSignature: public FormField {
487 public:
488 FormFieldSignature(PDFDoc *docA, Object *dict, const Ref& ref, FormField *parent, std::set<int> *usedParents);
490 virtual ~FormFieldSignature();
492 #ifdef DEBUG_FORMS
493 void print(int indent = 0);
494 #endif
497 //------------------------------------------------------------------------
498 // Form
499 // This class handle the document-wide part of Form (things in the acroForm
500 // Catalog entry).
501 //------------------------------------------------------------------------
503 class Form {
504 public:
505 Form(PDFDoc *docA, Object* acroForm);
507 ~Form();
509 // Look up an inheritable field dictionary entry.
510 static Object *fieldLookup(Dict *field, const char *key, Object *obj);
512 /* Creates a new Field of the type specified in obj's dict.
513 used in Form::Form and FormField::FormField */
514 static FormField *createFieldFromDict (Object* obj, PDFDoc *docA, const Ref& aref, FormField *parent, std::set<int> *usedParents);
516 Object *getObj () const { return acroForm; }
517 GBool getNeedAppearances () const { return needAppearances; }
518 int getNumFields() const { return numFields; }
519 FormField* getRootField(int i) const { return rootFields[i]; }
520 GooString* getDefaultAppearance() const { return defaultAppearance; }
521 VariableTextQuadding getTextQuadding() const { return quadding; }
522 GfxResources* getDefaultResources() const { return defaultResources; }
523 Object* getDefaultResourcesObj() { return &resDict; }
525 FormWidget* findWidgetByRef (Ref aref);
527 void postWidgetsLoad();
528 private:
529 FormField** rootFields;
530 int numFields;
531 int size;
532 PDFDoc *doc;
533 XRef* xref;
534 Object *acroForm;
535 GBool needAppearances;
536 GfxResources *defaultResources;
537 Object resDict;
539 // Variable Text
540 GooString *defaultAppearance;
541 VariableTextQuadding quadding;
544 //------------------------------------------------------------------------
545 // FormPageWidgets
546 //------------------------------------------------------------------------
548 class FormPageWidgets {
549 public:
550 FormPageWidgets (Annots* annots, unsigned int page, Form *form);
551 ~FormPageWidgets();
553 int getNumWidgets() const { return numWidgets; }
554 FormWidget* getWidget(int i) const { return widgets[i]; }
556 private:
557 FormWidget** widgets;
558 int numWidgets;
559 int size;
562 #endif