1 //========================================================================
5 // Copyright 2000-2005 Emmanuel Lesueur
7 //========================================================================
17 #include "Annotations.h"
35 const int fldReadOnly
= 1 << 0;
36 const int fldRequired
= 1 << 1;
37 const int fldNoExport
= 1 << 2;
38 const int fldMultiline
= 1 << 12;
39 const int fldPassword
= 1 << 13;
40 const int fldNoToggleOff
= 1 << 14;
41 const int fldRadio
= 1 << 15;
42 const int fldPushButton
= 1 << 16;
43 const int fldPopUp
= 1 << 17;
44 const int fldEdit
= 1 << 18;
45 const int fldSort
= 1 << 19;
48 //------------------------------------------------------------------------
50 //------------------------------------------------------------------------
56 FieldAttrs(FieldAttrs
*attrs
, Dict
*dict
);
62 Object
*getValue(Object
*obj
) { return value
.copy(obj
); }
63 Object
*getDefaultValue(Object
*obj
) { return defaultValue
.copy(obj
); }
64 int getFlags() { return flags
; }
65 Object
*getDefaultResources(Object
*obj
) { return defaultResources
.copy(obj
); }
66 GString
*getDefaultAppearance() { return defaultAppearance
; }
67 int getQuadding() { return quadding
; }
68 GString
*getTypeName() { return typeName
; }
69 Object
*getOpt(Object
*obj
) { return opt
.copy(obj
); }
70 int getTopIndex() { return topIndex
; }
71 int getMaxLen() { return maxLen
; }
72 GString
*getName() { return name
; }
73 Ref
getRef() { return ref
; }
75 void setName(GString
*name1
) { delete name
; name
= NULL
; name
= name1
->copy(); }
76 void setFlags(int flags1
) { flags
= flags1
; }
77 void setRef(Ref ref1
) { ref
= ref1
; }
83 Object defaultResources
;
84 GString
*defaultAppearance
;
95 //------------------------------------------------------------------------
97 //------------------------------------------------------------------------
103 virtual ~SubmitContext() {}
106 virtual void beginField(GString
*name
) =0;
107 virtual void endField() {}
108 virtual void addValue(GString
*value
) =0;
109 virtual void addNameValue(GString
*value
) =0;
110 virtual void beginKids() {}
111 virtual void endKids() {}
114 //------------------------------------------------------------------------
116 //------------------------------------------------------------------------
118 class SubmitFDFContext
: public SubmitContext
{
122 SubmitFDFContext(const char *fileName
);
125 virtual ~SubmitFDFContext();
128 virtual void beginField(GString
*name
);
129 virtual void endField();
130 virtual void addValue(GString
*value
);
131 virtual void addNameValue(GString
*value
);
132 virtual void beginKids();
133 virtual void endKids();
140 //------------------------------------------------------------------------
141 // SubmitHTMLGetContext
142 //------------------------------------------------------------------------
144 class SubmitHTMLGetContext
: public SubmitContext
{
148 SubmitHTMLGetContext();
151 virtual ~SubmitHTMLGetContext();
154 virtual void beginField(GString
*name
);
155 virtual void addValue(GString
*value
);
156 virtual void addNameValue(GString
*value
);
159 GString
*getContents() const { return str
; }
166 //------------------------------------------------------------------------
167 // SubmitHTMLPostContext
168 //------------------------------------------------------------------------
170 class SubmitHTMLPostContext
: public SubmitContext
{
174 SubmitHTMLPostContext(const char *fileName
);
177 virtual ~SubmitHTMLPostContext();
180 virtual void beginField(GString
*name
);
181 virtual void addValue(GString
*value
);
182 virtual void addNameValue(GString
*value
);
189 //------------------------------------------------------------------------
191 //------------------------------------------------------------------------
197 Field(XRef
*xref
, Dict
*dict
, FieldAttrs
*attrs
);
202 // Was the field constructed successfully?
203 virtual GBool
isOk() { return gTrue
; }
205 // Get the field type.
206 virtual FieldType
getType() =0;
208 // Reset to default value.
209 virtual void reset() {}
211 // Has the field a value?
212 virtual GBool
hasValue() { return gFalse
; }
214 // Build the field appearance stream.
215 virtual Object
*buildAppearance(Object
*obj
) { return obj
->initNull(); }
217 // The widget is activated.
218 virtual Action
*down(ActionContext
*) { return NULL
; }
220 // Import an FDF field
221 virtual GBool
import(Dict
*);
223 // Associate an annotation.
224 virtual void setAnnot(WidgetAnnot
*annot1
);
226 // Get the associated annotation, if any.
227 WidgetAnnot
*getAnnot() { return annot
; }
229 // Get the partial field name.
230 GString
*getName() { return name
; }
232 // Is the field read-only?
233 GBool
isReadOnly() { return flags
& fldReadOnly
; }
235 // Is the field required?
236 GBool
isRequired() { return flags
& fldRequired
; }
238 // Is the field exportable?
239 GBool
noExport() { return flags
& fldNoExport
; }
241 // Get the user name.
242 GString
*getUserName() { return userName
; }
244 // Get the mapping name.
245 GString
*getMappingName() { return mappingName
; }
248 int getRefNum() { return ref
.num
; }
249 int getRefGen() { return ref
.gen
; }
251 // Reset this field and its chilren.
252 void resetFieldsTree();
255 void submit(SubmitContext
*);
257 // Select fields to include in a submit operation.
258 void setExportFlag(GBool f
) { exportFlag
= f
; }
259 void setExportFlags(GBool f
);
262 void addChild(Field
*field
) { field
->next
= kids
; kids
= field
; }
265 // Submit the field value.
266 virtual GBool
doSubmit(SubmitContext
*);
274 GString
*mappingName
;
276 Field
*next
; // next field in the same level
277 Field
*kids
; // first kid
278 AnnotState annotState
;
283 //------------------------------------------------------------------------
285 //------------------------------------------------------------------------
287 class NodeField
: public Field
{
291 NodeField(XRef
*xref
, Dict
*dict
, FieldAttrs
*attrs
);
293 // Get the field type.
294 virtual FieldType
getType() { return fieldNode
; }
298 //------------------------------------------------------------------------
300 //------------------------------------------------------------------------
302 class PushButtonField
: public Field
{
306 PushButtonField(XRef
*xref
, Dict
*dict
, FieldAttrs
*attrs
);
308 // Get the field type.
309 virtual FieldType
getType() { return fieldPushButton
; }
313 //------------------------------------------------------------------------
315 //------------------------------------------------------------------------
317 class CheckboxField
: public Field
{
321 CheckboxField(XRef
*xref
, Dict
*dict
, FieldAttrs
*attrs
);
324 virtual ~CheckboxField();
326 // Get the field type.
327 virtual FieldType
getType() { return fieldCheckbox
; }
329 // Reset to default value.
330 virtual void reset();
332 // The widget is activated.
333 virtual Action
*down(ActionContext
*);
335 // Has the field a value?
336 virtual GBool
hasValue() { return gTrue
; }
338 // Import an FDF field
339 virtual GBool
import(Dict
*);
342 char *getValue() { return value
.getName(); }
345 void setRadio(RadioField
*radio1
) { radio
= radio1
; }
351 // Submit the field value.
352 virtual GBool
doSubmit(SubmitContext
*);
361 //------------------------------------------------------------------------
363 //------------------------------------------------------------------------
365 class RadioField
: public Field
{
369 RadioField(XRef
*xref
, Dict
*dict
, FieldAttrs
*attrs
);
372 virtual ~RadioField();
374 // Were the radio and its checkboxes constructed successfully?
375 virtual GBool
isOk() { return numChoices
&& numChoices
== maxChoices
; }
377 // Get the field type.
378 virtual FieldType
getType() { return fieldRadio
; }
380 // Reset to default value.
381 virtual void reset();
383 // Has the field a value?
384 virtual GBool
hasValue() { return gTrue
; }
386 // Import an FDF field
387 virtual GBool
import(Dict
*);
389 // The widget is activated.
390 Action
*down(ActionContext
*, CheckboxField
*);
392 // Get the current value.
393 char *getValue() { return value
.getName(); }
395 // Can this radio be off?
396 GBool
isNoToggleToOff() const { return flags
& fldNoToggleOff
; }
399 void addChoice(CheckboxField
*);
402 // Submit the field value.
403 virtual GBool
doSubmit(SubmitContext
*);
408 CheckboxField
**choices
;
414 //------------------------------------------------------------------------
416 //------------------------------------------------------------------------
418 class TextBaseField
: public Field
{
422 TextBaseField(XRef
*xref
, Dict
*dict
, FieldAttrs
*attrs
);
425 virtual ~TextBaseField();
427 // Associate an annotation.
428 virtual void setAnnot(WidgetAnnot
*annot1
);
430 /*Dict *getDefaultResources();
432 GString *getDefaultAppearance();
436 int getQuadding() { return quadding
; }
441 GString
*makeAppStreamData();
442 void buildAppearance();
443 void rebuildAppearance();
444 virtual GString
*getValueAppearance() =0;
446 Object defaultResources
;
447 GString
*defaultAppearance
;
456 //------------------------------------------------------------------------
458 //------------------------------------------------------------------------
460 class ChoiceField
: public TextBaseField
{
464 ChoiceField(XRef
*xref
, Dict
*dict
, FieldAttrs
*attrs
);
467 virtual ~ChoiceField();
469 // Was the field constructed successfully?
470 virtual GBool
isOk() { return opt
.isArray(); }
472 // Get the field type.
473 virtual FieldType
getType() { return fieldChoice
; }
475 // Reset to default value.
476 virtual void reset();
478 // The widget is activated.
479 virtual Action
*down(ActionContext
*);
481 // Has the field a value?
482 virtual GBool
hasValue();
484 // Import an FDF field.
485 virtual GBool
import(Dict
*);
487 // Get the field's value.
488 GString
*getValue() const { return value
; }
490 // Get the number of entries.
493 // Get a choice entry.
494 GString
*getEntry(int);
497 void setChoice(int n
);
498 void setChoice(GString
*);
500 GBool
isPopUp() const { return flags
& fldPopUp
; }
501 GBool
isEditable() const { return flags
& fldEdit
; }
503 void update(ActionContext
*, GString
*);
506 // Submit the field value.
507 virtual GBool
doSubmit(SubmitContext
*);
510 virtual GString
*getValueAppearance();
514 GString
*defaultValue
;
519 //------------------------------------------------------------------------
521 //------------------------------------------------------------------------
523 class TextField
: public TextBaseField
{
527 TextField(XRef
*xref
, Dict
*dict
, FieldAttrs
*attrs
);
530 virtual ~TextField();
532 // Get the field type.
533 virtual FieldType
getType() { return fieldText
; }
535 // Reset to default value.
536 virtual void reset();
538 // The widget is activated.
539 virtual Action
*down(ActionContext
*);
541 // Has the field a value?
542 virtual GBool
hasValue() { return value
.isString(); }
544 // Import an FDF field
545 virtual GBool
import(Dict
*);
547 /*Dict *getDefaultResources();
549 GString *getDefaultAppearance();
553 void update(ActionContext
*, GString
*);
555 GBool
isMultiline() { return flags
& fldMultiline
; }
556 GBool
isPassword() { return flags
& fldPassword
; }
558 int getMaxLen() { return maxLen
; }
559 GString
*getValue() { return value
.isString() ? value
.getString() : NULL
; }
562 // Submit the field value.
563 virtual GBool
doSubmit(SubmitContext
*);
566 virtual GString
*getValueAppearance();
574 //------------------------------------------------------------------------
576 //------------------------------------------------------------------------
582 AcroForm(XRef
*, Object
*);
587 // Was the AcroForm created successfully?
588 GBool
isOk() { return ok
; }
590 // Get the number of fields.
591 int getNumFields() { return numFields
; }
594 Field
*getField(int n
) { return fields
[n
]; }
596 // Get the number of root fields.
597 int getNumRootFields() { return numRootFields
; }
600 Field
*getRootField(int n
) { return rootFields
[n
]; }
602 // Find a particular field.
603 Field
*findField(int num
, int gen
);
604 Field
*findField(GString
*);
606 // Import a FDF dictionary.
607 GBool
importFDF(Dict
*dict
);
609 // Import a FDF file.
610 GBool
importFDF(GString
*fileName
);
612 // Export a FDF file.
613 GBool
exportFDF(GString
*fileName
);
616 Field
*readFieldTree(XRef
*, Object
*, FieldAttrs
*);
617 void addField(Field
*);
618 GBool
importField(GString
*name
, Object
*dict
);
625 GBool needAppearances
;