Bringing apdf from vendor into main branch.
[AROS-Contrib.git] / apdf / xpdf / AcroForm.h
blob1c9e44569faefc1224fb471f08b7117046412e83
1 //========================================================================
2 //
3 // AcroForm.h
4 //
5 // Copyright 2000-2005 Emmanuel Lesueur
6 //
7 //========================================================================
9 #ifndef ACROFORM_H
10 #define ACROFORM_H
12 #ifdef __GNUC__
13 #pragma interface
14 #endif
16 #include "Object.h"
17 #include "Annotations.h"
19 class WidgetAnnot;
20 class RadioField;
21 class Action;
22 class ActionContext;
23 class SubmitContext;
25 enum FieldType {
26 fieldNode,
27 fieldPushButton,
28 fieldRadio,
29 fieldCheckbox,
30 fieldText,
31 fieldChoice,
32 fieldSig
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 //------------------------------------------------------------------------
49 // FieldAttrs
50 //------------------------------------------------------------------------
52 class FieldAttrs {
53 public:
55 // Constructor.
56 FieldAttrs(FieldAttrs *attrs, Dict *dict);
58 // Destructor.
59 ~FieldAttrs();
61 // Accessors.
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; }
79 private:
80 Object value;
81 Object defaultValue;
82 int flags;
83 Object defaultResources;
84 GString *defaultAppearance;
85 int quadding;
86 GString *typeName;
87 Object opt;
88 int topIndex;
89 int maxLen;
90 GString *name;
91 Ref ref;
95 //------------------------------------------------------------------------
96 // SubmitContext
97 //------------------------------------------------------------------------
99 class SubmitContext {
100 public:
102 // Destructor.
103 virtual ~SubmitContext() {}
105 // write data.
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 //------------------------------------------------------------------------
115 // SubmitFDFContext
116 //------------------------------------------------------------------------
118 class SubmitFDFContext : public SubmitContext {
119 public:
121 // Constructor.
122 SubmitFDFContext(const char *fileName);
124 // Destructor.
125 virtual ~SubmitFDFContext();
127 // write data.
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();
135 private:
136 FILE *file;
140 //------------------------------------------------------------------------
141 // SubmitHTMLGetContext
142 //------------------------------------------------------------------------
144 class SubmitHTMLGetContext : public SubmitContext {
145 public:
147 // Constructor.
148 SubmitHTMLGetContext();
150 // Destructor.
151 virtual ~SubmitHTMLGetContext();
153 // Write data.
154 virtual void beginField(GString *name);
155 virtual void addValue(GString *value);
156 virtual void addNameValue(GString *value);
158 // Get the result.
159 GString *getContents() const { return str; }
161 private:
162 GString *str;
166 //------------------------------------------------------------------------
167 // SubmitHTMLPostContext
168 //------------------------------------------------------------------------
170 class SubmitHTMLPostContext : public SubmitContext {
171 public:
173 // Constructor.
174 SubmitHTMLPostContext(const char *fileName);
176 // Destructor.
177 virtual ~SubmitHTMLPostContext();
179 // Write data.
180 virtual void beginField(GString *name);
181 virtual void addValue(GString *value);
182 virtual void addNameValue(GString *value);
184 private:
185 FILE *file;
189 //------------------------------------------------------------------------
190 // Field
191 //------------------------------------------------------------------------
193 class Field {
194 public:
196 // Constructor.
197 Field(XRef *xref, Dict *dict, FieldAttrs *attrs);
199 // Destructor.
200 virtual ~Field();
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; }
247 // Get the ref.
248 int getRefNum() { return ref.num; }
249 int getRefGen() { return ref.gen; }
251 // Reset this field and its chilren.
252 void resetFieldsTree();
254 // Submit the field.
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);
261 // Add a child.
262 void addChild(Field *field) { field->next = kids; kids = field; }
264 protected:
265 // Submit the field value.
266 virtual GBool doSubmit(SubmitContext *);
268 int flags;
270 private:
271 Ref ref;
272 GString *name;
273 GString *userName;
274 GString *mappingName;
275 WidgetAnnot *annot;
276 Field *next; // next field in the same level
277 Field *kids; // first kid
278 AnnotState annotState;
279 GBool exportFlag;
283 //------------------------------------------------------------------------
284 // NodeField
285 //------------------------------------------------------------------------
287 class NodeField : public Field {
288 public:
290 // Constructor.
291 NodeField(XRef *xref, Dict *dict, FieldAttrs *attrs);
293 // Get the field type.
294 virtual FieldType getType() { return fieldNode; }
298 //------------------------------------------------------------------------
299 // PushButtonField
300 //------------------------------------------------------------------------
302 class PushButtonField : public Field {
303 public:
305 // Constructor.
306 PushButtonField(XRef *xref, Dict *dict, FieldAttrs *attrs);
308 // Get the field type.
309 virtual FieldType getType() { return fieldPushButton; }
313 //------------------------------------------------------------------------
314 // CheckboxField
315 //------------------------------------------------------------------------
317 class CheckboxField : public Field {
318 public:
320 // Constructor.
321 CheckboxField(XRef *xref, Dict *dict, FieldAttrs *attrs);
323 // Destructor.
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 *);
341 // Get value.
342 char *getValue() { return value.getName(); }
344 // Set parent radio
345 void setRadio(RadioField *radio1) { radio = radio1; }
347 // Toggle the state
348 void toggle();
350 protected:
351 // Submit the field value.
352 virtual GBool doSubmit(SubmitContext *);
354 private:
355 Object value;
356 Object defaultValue;
357 RadioField *radio;
361 //------------------------------------------------------------------------
362 // RadioField
363 //------------------------------------------------------------------------
365 class RadioField : public Field {
366 public:
368 // Constructor.
369 RadioField(XRef *xref, Dict *dict, FieldAttrs *attrs);
371 // Destructor.
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; }
398 // Add a checkbox.
399 void addChoice(CheckboxField *);
401 protected:
402 // Submit the field value.
403 virtual GBool doSubmit(SubmitContext *);
405 private:
406 int numChoices;
407 int maxChoices;
408 CheckboxField **choices;
409 Object value;
410 Object defaultValue;
414 //------------------------------------------------------------------------
415 // TextBaseField
416 //------------------------------------------------------------------------
418 class TextBaseField : public Field {
419 public:
421 // Constructor.
422 TextBaseField(XRef *xref, Dict *dict, FieldAttrs *attrs);
424 // Destructor.
425 virtual ~TextBaseField();
427 // Associate an annotation.
428 virtual void setAnnot(WidgetAnnot *annot1);
430 /*Dict *getDefaultResources();
432 GString *getDefaultAppearance();
434 getHighlight();*/
436 int getQuadding() { return quadding; }
438 int getOrigHeight();
440 protected:
441 GString *makeAppStreamData();
442 void buildAppearance();
443 void rebuildAppearance();
444 virtual GString *getValueAppearance() =0;
446 Object defaultResources;
447 GString *defaultAppearance;
448 int origHeight;
449 int quadding;
450 int lines;
451 GBool changed;
452 XRef *xref;
456 //------------------------------------------------------------------------
457 // ChoiceField
458 //------------------------------------------------------------------------
460 class ChoiceField : public TextBaseField {
461 public:
463 // Constructor.
464 ChoiceField(XRef *xref, Dict *dict, FieldAttrs *attrs);
466 // Destructor.
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.
491 int getNumEntries();
493 // Get a choice entry.
494 GString *getEntry(int);
496 // Set a new value.
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 *);
505 protected:
506 // Submit the field value.
507 virtual GBool doSubmit(SubmitContext *);
509 private:
510 virtual GString *getValueAppearance();
512 Object opt;
513 GString *value;
514 GString *defaultValue;
515 int topIndex;
519 //------------------------------------------------------------------------
520 // TextField
521 //------------------------------------------------------------------------
523 class TextField : public TextBaseField {
524 public:
526 // Constructor.
527 TextField(XRef *xref, Dict *dict, FieldAttrs *attrs);
529 // Destructor.
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();
551 getHighlight();*/
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; }
561 protected:
562 // Submit the field value.
563 virtual GBool doSubmit(SubmitContext *);
565 private:
566 virtual GString *getValueAppearance();
568 Object value;
569 Object defaultValue;
570 int maxLen;
574 //------------------------------------------------------------------------
575 // AcroForm
576 //------------------------------------------------------------------------
578 class AcroForm {
579 public:
581 // Constructor.
582 AcroForm(XRef *, Object *);
584 // Destructor.
585 ~AcroForm();
587 // Was the AcroForm created successfully?
588 GBool isOk() { return ok; }
590 // Get the number of fields.
591 int getNumFields() { return numFields; }
593 // Get a field.
594 Field *getField(int n) { return fields[n]; }
596 // Get the number of root fields.
597 int getNumRootFields() { return numRootFields; }
599 // Get a root field.
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);
615 private:
616 Field *readFieldTree(XRef *, Object *, FieldAttrs *);
617 void addField(Field *);
618 GBool importField(GString *name, Object *dict);
620 Field **fields;
621 Field **rootFields;
622 int numFields;
623 int numRootFields;
624 int maxFields;
625 GBool needAppearances;
626 GBool ok;
629 #endif