Annotation of Amaya/amaya/HTMLbook.c, revision 1.17
1.1 cvs 1: /*
2: *
3: * (c) COPYRIGHT MIT and INRIA, 1996.
4: * Please first read the full copyright statement in file COPYRIGHT.
5: *
6: */
7:
8: /*
9: * Initialization functions and button functions of Amaya application.
10: *
11: * Authors: V. Quint, I. Vatton
12: */
13:
14:
15: /* Included headerfiles */
16: #define THOT_EXPORT
17: #include "amaya.h"
18: #include "print.h"
19:
20: #define NumFormPrint 1
21: #define NumMenuOptions 2
22: #define NumMenuPaperFormat 3
23: #define NumMenuSupport 4
24: #define NumZonePrinterName 5
25: #define PRINT_MAX_REF 6
26:
27: /* Thot printer variables */
1.2 cvs 28: static int PaperPrint;
29: static int ManualFeed;
30: static int PageSize;
31: static char PSdir[MAX_PATH];
32: static char pPrinter[MAX_PATH];
1.1 cvs 33: static Document docPrint;
34: static boolean numberLinks;
35: static boolean withToC;
36: static int basePrint;
37:
1.16 cvs 38: /* structure to register sub-documents */
39: typedef struct _SubDoc
40: {
41: struct _SubDoc *SDnext;
42: Element SDel;
43: char *SDname;
44: }SubDoc;
45: static struct _SubDoc *SubDocs = NULL;
46: static Document docBook = 0;
47:
1.1 cvs 48: #include "init_f.h"
49: #include "HTMLactions_f.h"
50: #include "HTMLbook_f.h"
51: #include "HTMLedit_f.h"
52:
53:
54: /*----------------------------------------------------------------------
1.16 cvs 55: RegisterSubDoc adds a new entry in SubDoc table.
56: ----------------------------------------------------------------------*/
57: #ifdef __STDC__
58: static void RegisterSubDoc (Element el, char *url)
59: #else
60: static void RegisterSubDoc (el, url)
61: Element el;
62: char *url;
63: #endif
64: {
65: struct _SubDoc *entry, *last;
66:
67: if (url == NULL || url[0] == EOS)
68: return;
69:
70: entry = TtaGetMemory (sizeof (struct _SubDoc));
71: entry->SDnext = NULL;
72: entry->SDel = el;
73: entry->SDname = TtaStrdup (url);
74:
75: if (SubDocs == NULL)
76: SubDocs = entry;
77: else
78: {
79: last = SubDocs;
80: while (last->SDnext != NULL)
81: last = last->SDnext;
82: last->SDnext = entry;
83: }
84: }
85:
86:
87: /*----------------------------------------------------------------------
88: SearchSubDoc searches whether a document name is registered or not
89: within the SubDoc table.
90: Return the DIV element that correspond to the sub-document or NULL.
91: ----------------------------------------------------------------------*/
92: #ifdef __STDC__
93: static Element SearchSubDoc (char *url)
94: #else
95: static Element SearchSubDoc (url)
96: char *url;
97: #endif
98: {
99: Element el;
100: struct _SubDoc *entry;
101: boolean docFound;
102:
103: if (url == NULL || url[0] == EOS)
104: return (NULL);
105:
106: entry = SubDocs;
107: docFound = FALSE;
108: el = NULL;
109: while (!docFound && entry != NULL)
110: {
111: docFound = (strcmp (url, entry->SDname) == 0);
112: if (!docFound)
113: entry = entry->SDnext;
114: else
115: /* document found -> return the DIV element */
116: el = entry->SDel;
117: }
118: return (el);
119: }
120:
121: /*----------------------------------------------------------------------
122: FreeSubDocTable frees all entries in SubDoc table.
123: ----------------------------------------------------------------------*/
124: static void FreeSubDocTable ()
125: {
126: struct _SubDoc *entry, *last;
127:
128: entry = SubDocs;
129: while (entry != NULL)
130: {
131: last = entry;
132: entry = entry->SDnext;
133: TtaFreeMemory (last->SDname);
134: TtaFreeMemory (last);
135: }
136: SubDocs = NULL;
137: }
138:
139:
140:
141: /*----------------------------------------------------------------------
1.1 cvs 142: SetInternalLinks
1.8 cvs 143: Associate an InternalLink attribute with all anchor (A) elements of the
144: document which designate an element in the same document.
1.1 cvs 145: InternalLink is a Thot reference attribute that links a source and a
146: target anchor and that allows P schemas to display and print cross-references
1.8 cvs 147: Associate an ExternalLink attribute with all anchor (A) elements of the
148: document which designate an element in another document.
1.1 cvs 149: ----------------------------------------------------------------------*/
150: #ifdef __STDC__
1.4 cvs 151: void SetInternalLinks (Document document)
1.1 cvs 152: #else
1.4 cvs 153: void SetInternalLinks (document)
1.1 cvs 154: Document document;
155: #endif
156: {
1.16 cvs 157: Element root, el, div;
158: Element link, target;
159: ElementType elType;
160: Attribute HrefAttr, IntLinkAttr;
1.17 ! cvs 161: Attribute attr, ExtLinkAttr;
1.16 cvs 162: AttributeType attrType;
163: char *text, *ptr, *url;
164: char value[MAX_LENGTH];
165: int length, i;
166: int status;
167: boolean split;
1.1 cvs 168:
1.16 cvs 169: /* Remember the current status of the document */
170: status = TtaIsDocumentModified (document);
171: root = TtaGetMainRoot (document);
172: elType = TtaGetElementType (root);
173: elType.ElTypeNum = HTML_EL_BODY;
174: el = TtaSearchTypedElement (elType, SearchForward, root);
1.1 cvs 175:
1.16 cvs 176: elType.ElTypeNum = HTML_EL_Anchor;
177: attrType.AttrSSchema = elType.ElSSchema;
178: /* looks for all anchors in the document */
179: link = el;
180: split = FALSE;
181: while (link != NULL)
182: {
183: link = TtaSearchTypedElement (elType, SearchForward, link);
184: if (link != NULL)
185: /* an anchor has been found */
186: {
187: attrType.AttrTypeNum = HTML_ATTR_HREF_;
188: HrefAttr = TtaGetAttribute (link, attrType);
189: attrType.AttrTypeNum = HTML_ATTR_InternalLink;
190: IntLinkAttr = TtaGetAttribute (link, attrType);
191: attrType.AttrTypeNum = HTML_ATTR_ExternalLink;
192: ExtLinkAttr = TtaGetAttribute (link, attrType);
193: if (HrefAttr == NULL)
194: /* this anchor is not a link (no href attribute) */
195: /* remove attributes InternalLink and ExternalLink if they
196: are present */
197: {
1.8 cvs 198: if (IntLinkAttr != NULL)
1.16 cvs 199: TtaRemoveAttribute (link, IntLinkAttr, document);
200: if (ExtLinkAttr != NULL)
201: TtaRemoveAttribute (link, ExtLinkAttr, document);
202: }
203: else
204: /* this anchor has an HREF attribute */
205: {
206: length = TtaGetTextAttributeLength (HrefAttr);
207: text = TtaGetMemory (length + 1);
208: TtaGiveTextAttributeValue (HrefAttr, text, &length);
209:
210: /* does an external link become an internal link ? */
211: if (document == docBook && SubDocs != NULL)
212: {
213: ptr = strrchr (text, '#');
214: url = text;
215: if (ptr == text)
216: /* a local link */
217: url = NULL;
1.17 ! cvs 218: else if (ptr != NULL)
1.16 cvs 219: {
220: /* split url and name part */
221: ptr[0] = EOS;
222: split = TRUE;
223: }
224:
225: /* Is it a sub-document */
226: div = SearchSubDoc (url);
227: if (split)
228: /* retore the mark */
229: ptr[0] = '#';
230:
231: if (div == NULL)
232: {
233: /* it's not a sub-document */
234: if (url == NULL)
235: /* a local link */
236: ptr = &text[1];
237: else
238: /* still an externa; link */
239: ptr = NULL;
240: }
241: else
242: {
243: /* this link becomes internal */
1.17 ! cvs 244: if (ptr != NULL)
1.16 cvs 245: {
1.17 ! cvs 246: /* get the target name */
! 247: strcpy (value, ptr);
! 248: length = strlen (value);
! 249: /* check whether the name changed */
! 250: i = 0;
! 251: target = SearchNAMEattribute (document, &value[1], NULL);
! 252: while (target != NULL)
1.16 cvs 253: {
1.17 ! cvs 254: /* is it the right NAME */
! 255: if (TtaIsAncestor (target, div))
! 256: target = NULL;
! 257: else
! 258: {
! 259: /* continue the search */
! 260: i++;
! 261: sprintf (&value[length], "%d", i);
! 262: target = SearchNAMEattribute (document, &value[1], NULL);
! 263: }
1.16 cvs 264: }
265: }
1.17 ! cvs 266: else
! 267: {
! 268: /* get the DIV name */
! 269: attrType.AttrTypeNum = HTML_ATTR_ID;
! 270: attr = TtaGetAttribute (div, attrType);
! 271: length = 200;
! 272: value[0] = '#';
! 273: TtaGiveTextAttributeValue (attr, &value[1], &length);
! 274: }
1.16 cvs 275: ptr = &value[1];
276: TtaSetAttributeText (HrefAttr, value, link, document);
277: }
278: }
279: else if (text[0] == '#')
280: ptr = &text[1];
281: else
282: ptr = NULL;
283:
284: if (ptr != NULL)
285: /* it's an internal link. Attach an attribute InternalLink */
286: /* to the link, if this attribute does not exist yet */
287: {
288: if (IntLinkAttr == NULL)
289: {
290: attrType.AttrTypeNum = HTML_ATTR_InternalLink;
291: IntLinkAttr = TtaNewAttribute (attrType);
292: TtaAttachAttribute (link, IntLinkAttr, document);
293: }
294: /* looks for the target element */
295: target = SearchNAMEattribute (document, ptr, NULL);
296: if (target != NULL)
297: /* set the Thot link */
298: TtaSetAttributeReference (IntLinkAttr, link, document, target, document);
299: }
300: else
301: /* it's an external link */
302: {
303: /* Remove the InternalLink attribute if it is present */
304: if (IntLinkAttr != NULL)
305: TtaRemoveAttribute (link, IntLinkAttr, document);
306: /* create an ExternalLink attribute if there is none */
307: if (ExtLinkAttr == NULL)
308: {
309: attrType.AttrTypeNum = HTML_ATTR_ExternalLink;
310: ExtLinkAttr = TtaNewAttribute (attrType);
311: TtaAttachAttribute (link, ExtLinkAttr, document);
312: }
313: }
314: TtaFreeMemory (text);
315: }
316: }
317: }
318: /* Reset document status */
319: if (!status)
320: TtaSetDocumentUnmodified (document);
1.3 cvs 321: }
322:
323: /*----------------------------------------------------------------------
324: CheckPrintingDocument reinitialize printing parameters as soon as
325: the printing document changes.
326: ----------------------------------------------------------------------*/
327: #ifdef __STDC__
328: static void CheckPrintingDocument (Document document)
329: #else
330: static void CheckPrintingDocument (document)
331: Document document;
332: #endif
333: {
334: char docName[MAX_LENGTH];
335: char *ptr, suffix[MAX_LENGTH];
336: int lg;
337:
338: if (docPrint != document)
339: {
340: /* initialize print parameters */
341: docPrint = document;
342:
343: /* define the new default PS file */
344: ptr = TtaGetEnvString ("TMPDIR");
345: if (ptr != NULL && TtaCheckDirectory (ptr))
346: {
347: strcpy(PSdir,ptr);
348: lg = strlen(PSdir);
349: if (PSdir[lg - 1] == DIR_SEP)
350: PSdir[--lg] = '\0';
351: }
352: else
353: {
1.6 cvs 354: # ifdef _WINDOWS
1.3 cvs 355: strcpy (PSdir,"C:\\TEMP");
1.6 cvs 356: # else /* !_WINDOWS */
1.3 cvs 357: strcpy (PSdir,"/tmp");
1.6 cvs 358: # endif /* !_WINDOWS */
1.3 cvs 359: lg = strlen (PSdir);
360: }
361: strcpy (docName, TtaGetDocumentName (document));
362: ExtractSuffix (docName, suffix);
363: sprintf (&PSdir[lg], "/%s.ps", docName);
364: TtaSetPsFile (PSdir);
365: /* define the new default PrintSchema */
366: numberLinks = FALSE;
367: withToC = FALSE;
368: TtaSetPrintSchema ("");
369: /* no manual feed */
370: ManualFeed = PP_OFF;
371: TtaSetPrintParameter (PP_ManualFeed, ManualFeed);
372: }
373: }
374:
375:
376: /*----------------------------------------------------------------------
377: PrintAs prints the document using predefined parameters.
378: ----------------------------------------------------------------------*/
379: #ifdef __STDC__
380: void PrintAs (Document document, View view)
381: #else /* __STDC__ */
382: void PrintAs (document, view)
383: Document document;
384: #endif /* __STDC__ */
385: {
1.9 cvs 386: char viewsToPrint[MAX_PATH];
1.3 cvs 387:
388: CheckPrintingDocument (document);
389: strcpy (viewsToPrint, "Formatted_view ");
390: if (withToC)
391: strcat (viewsToPrint, "Table_of_contents ");
392: if (numberLinks)
1.8 cvs 393: /* display numbered links */
1.3 cvs 394: {
1.8 cvs 395: /* associate an attribute InternalLink with all anchors refering
396: a target in the same document. This allows P schemas to work
397: properly */
398: SetInternalLinks (docPrint);
1.3 cvs 399: if (PageSize == PP_A4)
400: TtaSetPrintSchema ("HTMLPLP");
401: else
402: TtaSetPrintSchema ("HTMLPLPUS");
403: strcat (viewsToPrint, "Links_view ");
404: }
405: TtaPrint (docPrint, viewsToPrint);
1.1 cvs 406: }
407:
408:
409: /*----------------------------------------------------------------------
410: CallbackImage manage returns of Picture form.
411: ----------------------------------------------------------------------*/
412: #ifdef __STDC__
413: void CallbackPrint (int ref, int typedata, char *data)
414: #else /* __STDC__ */
415: void CallbackPrint (ref, typedata, data)
416: int ref;
417: int typedata;
418: char *data;
419: #endif /* __STDC__ */
420: {
421: int val;
422:
423: val = (int) data;
424: switch (ref - basePrint)
425: {
426: case NumFormPrint:
427: TtaDestroyDialogue (basePrint+NumFormPrint);
428: switch (val)
429: {
430: case 1:
431: /* confirms the paper print option */
432: /* the other options are not taken into account without this
433: confirmation */
1.2 cvs 434: TtaSetPrintParameter (PP_Destination, PaperPrint);
435: TtaSetPrintParameter (PP_ManualFeed, ManualFeed);
436: TtaSetPrintParameter (PP_PaperSize, PageSize);
437: TtaSetPrintCommand (pPrinter);
438: TtaSetPsFile (PSdir);
1.3 cvs 439: PrintAs (docPrint, 1);
1.1 cvs 440: break;
1.2 cvs 441: case 0:
442: PaperPrint = TtaGetPrintParameter (PP_Destination);
443: ManualFeed = TtaGetPrintParameter (PP_ManualFeed);
444: PageSize = TtaGetPrintParameter (PP_PaperSize);
445: TtaGetPrintCommand (pPrinter);
446: TtaGetPsFile (PSdir);
447: break;
1.1 cvs 448: default:
449: break;
450: }
451: break;
452: case NumMenuOptions:
453: switch (val)
454: {
455: case 0:
456: /* Manual feed option */
1.2 cvs 457: if (ManualFeed == PP_ON)
458: ManualFeed = PP_OFF;
459: else
460: ManualFeed = PP_ON;
1.1 cvs 461: break;
462: case 1:
463: /* Toc option */
464: withToC = !withToC;
465: break;
466: case 2:
1.3 cvs 467: /* numberLinks option */
1.1 cvs 468: numberLinks = !numberLinks;
469: break;
470: }
471: break;
472: case NumMenuPaperFormat:
473: /* page size submenu */
474: switch (val)
475: {
476: case 0:
1.2 cvs 477: PageSize = PP_A4;
1.1 cvs 478: break;
479: case 1:
1.2 cvs 480: PageSize = PP_US;
1.1 cvs 481: break;
482: }
483: break;
484: case NumMenuSupport:
485: /* paper print/save PostScript submenu */
486: switch (val)
487: {
488: case 0:
1.2 cvs 489: if (PaperPrint == PP_PS)
1.1 cvs 490: {
1.2 cvs 491: PaperPrint = PP_PRINTER;
1.1 cvs 492: TtaSetTextForm (basePrint+NumZonePrinterName, pPrinter);
493: }
494: break;
495: case 1:
1.2 cvs 496: if (PaperPrint == PP_PRINTER)
1.1 cvs 497: {
1.2 cvs 498: PaperPrint = PP_PS;
1.1 cvs 499: TtaSetTextForm (basePrint+NumZonePrinterName, PSdir);
500: }
501: break;
502: }
503: break;
504: case NumZonePrinterName:
505: if (data[0] != '\0')
1.2 cvs 506: if (PaperPrint == PP_PRINTER)
1.1 cvs 507: /* text capture zone for the printer name */
508: strncpy (pPrinter, data, MAX_PATH);
509: else
510: /* text capture zone for the name of the PostScript file */
511: strncpy (PSdir, data, MAX_PATH);
512: break;
513: }
514: }
515:
516: /*----------------------------------------------------------------------
517: ----------------------------------------------------------------------*/
518: #ifdef __STDC__
519: void InitPrint (void)
520: #else /* __STDC__ */
521: void InitPrint ()
522: #endif /* __STDC__ */
523: {
524: char *ptr;
525:
526: basePrint = TtaSetCallback (CallbackPrint, PRINT_MAX_REF);
527: docPrint = 0;
528:
529: /* init printer variables */
530: /* read default printer variable */
531: ptr = TtaGetEnvString ("THOTPRINT");
532: if (ptr == NULL)
533: strcpy (pPrinter, "");
534: else
535: strcpy (pPrinter, ptr);
536:
1.2 cvs 537: PageSize = PP_A4;
538: PaperPrint = PP_PRINTER;
539: TtaSetPrintParameter (PP_Destination, PaperPrint);
540: TtaSetPrintParameter (PP_PaperSize, PageSize);
541: TtaSetPrintCommand (pPrinter);
1.1 cvs 542: }
543:
544: /*----------------------------------------------------------------------
1.3 cvs 545: SetupAndPrint sets printing parameters and starts the printing process
1.1 cvs 546: ----------------------------------------------------------------------*/
547: #ifdef __STDC__
548: void SetupAndPrint (Document document, View view)
549: #else
550: void SetupAndPrint (document, view)
551: Document document;
552: View view;
553: #endif
554: {
1.10 cvs 555: # ifndef _WINDOWS
1.2 cvs 556: char bufMenu[MAX_LENGTH];
1.3 cvs 557: int i;
1.1 cvs 558:
559: /* Print form */
1.3 cvs 560: CheckPrintingDocument (document);
1.1 cvs 561: TtaNewSheet (basePrint+NumFormPrint, TtaGetViewFrame (document, view),
562: TtaGetMessage (LIB, TMSG_LIB_PRINT),
1.3 cvs 563: 1, TtaGetMessage (AMAYA, AM_BUTTON_PRINT), FALSE, 2, 'L', D_CANCEL);
1.1 cvs 564: i = 0;
565: sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (LIB, TMSG_MANUAL_FEED));
566: i += strlen (&bufMenu[i]) + 1;
567: sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_PRINT_TOC));
568: i += strlen (&bufMenu[i]) + 1;
569: sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_NUMBERED_LINKS));
570: TtaNewToggleMenu (basePrint+NumMenuOptions, basePrint+NumFormPrint,
571: TtaGetMessage (LIB, TMSG_OPTIONS), 3, bufMenu, NULL, FALSE);
1.2 cvs 572: if (ManualFeed == PP_ON)
1.1 cvs 573: TtaSetToggleMenu (basePrint+NumMenuOptions, 0, TRUE);
574: if (withToC)
575: TtaSetToggleMenu (basePrint+NumMenuOptions, 1, TRUE);
576: if (numberLinks)
577: TtaSetToggleMenu (basePrint+NumMenuOptions, 2, TRUE);
578:
579: /* Paper format submenu */
580: i = 0;
581: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_A4));
582: i += strlen (&bufMenu[i]) + 1;
583: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_US));
584: TtaNewSubmenu (basePrint+NumMenuPaperFormat, basePrint+NumFormPrint, 0,
585: TtaGetMessage (LIB, TMSG_PAPER_SIZE), 2, bufMenu, NULL, FALSE);
1.2 cvs 586: if (PageSize == PP_US)
1.1 cvs 587: TtaSetMenuForm (basePrint+NumMenuPaperFormat, 1);
588: else
589: TtaSetMenuForm (basePrint+NumMenuPaperFormat, 0);
590:
591: /* Print to paper/ Print to file submenu */
592: i = 0;
593: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_PRINTER));
594: i += strlen (&bufMenu[i]) + 1;
595: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_PS_FILE));
596: TtaNewSubmenu (basePrint+NumMenuSupport, basePrint+NumFormPrint, 0,
597: TtaGetMessage (LIB, TMSG_OUTPUT), 2, bufMenu, NULL, TRUE);
598: /* text capture zone for the printer name */
599: TtaNewTextForm (basePrint+NumZonePrinterName, basePrint+NumFormPrint, NULL, 30, 1, FALSE);
600:
601: /* initialization of the PaperPrint selector */
1.2 cvs 602: if (PaperPrint == PP_PRINTER)
1.1 cvs 603: {
604: TtaSetMenuForm (basePrint+NumMenuSupport, 0);
605: TtaSetTextForm (basePrint+NumZonePrinterName, pPrinter);
606: }
607: else
608: {
609: TtaSetMenuForm (basePrint+NumMenuSupport, 1);
610: TtaSetTextForm (basePrint+NumZonePrinterName, PSdir);
611: }
612:
613: /* activates the Print form */
1.10 cvs 614: TtaShowDialogue (basePrint+NumFormPrint, FALSE);
615: # else /* _WINDOWS */
1.11 cvs 616: CreatePrintDlgWindow (TtaGetViewFrame (document, view));
1.10 cvs 617: # endif /* _WINDOWS */
1.1 cvs 618: }
619:
620: /*----------------------------------------------------------------------
621: SectionNumbering
622: Execute the "Section Numbering" command
623: ----------------------------------------------------------------------*/
624: #ifdef __STDC__
625: void SectionNumbering (Document document, View view)
626: #else
627: void SectionNumbering (document, view)
628: Document document;
629: View view;
630: #endif
631: {
632: ChangeAttrOnRoot (document, HTML_ATTR_SectionNumbering);
633: }
634:
635: /*----------------------------------------------------------------------
636: UpdateURLsInSubtree
637: Update NAMEs and URLs in subtree of el element, to take into account
638: the move from one document to another.
639: If a NAME attribute already exists in the new document, it is changed
640: to avoid duplicate names.
641: Transform the HREF and SRC attribute to make them independent from their
642: former base.
643: ----------------------------------------------------------------------*/
644: #ifdef __STDC__
645: static void UpdateURLsInSubtree (NotifyElement *event, Element el)
646: #else
647: static void UpdateURLsInSubtree (event, el)
648: NotifyElement *event;
649: Element el;
650: #endif
651: {
652: Element nextEl;
653:
654: event->element = el;
655: ElementPasted (event);
656: nextEl = TtaGetFirstChild (el);
657: while (nextEl != NULL)
658: {
659: UpdateURLsInSubtree (event, nextEl);
660: TtaNextSibling (&nextEl);
661: }
662: }
663:
664:
665: /*----------------------------------------------------------------------
666: MoveDocumentBody
667: Copy the elements contained in the BODY of document sourceDoc to the
668: position of element *el in document destDoc.
669: Delete the element containing *el and all its empty ancestors.
670: If deleteTree is TRUE, copied elements are deleted from the source
671: document.
672: ----------------------------------------------------------------------*/
673: #ifdef __STDC__
1.16 cvs 674: static void MoveDocumentBody (Element *el, Document destDoc, Document sourceDoc, char *target, char *url, boolean deleteTree)
1.1 cvs 675: #else
1.16 cvs 676: static void MoveDocumentBody (el, destDoc, sourceDoc, target, url, deleteTree)
1.12 cvs 677: Element *el;
678: Document destDoc;
679: Document sourceDoc;
680: char *target;
1.16 cvs 681: char *url;
1.12 cvs 682: boolean deleteTree;
1.1 cvs 683: #endif
684: {
1.14 cvs 685: Element root, ancestor, elem, firstInserted;
1.12 cvs 686: Element lastInserted, srce, copy, old, parent, sibling;
687: ElementType elType;
688: NotifyElement event;
689: int checkingMode;
1.13 cvs 690: boolean isID;
1.1 cvs 691:
1.13 cvs 692: if (target != NULL)
693: {
694: /* locate the target element within the source document */
695: root = SearchNAMEattribute (sourceDoc, target, NULL);
696: elType = TtaGetElementType (root);
697: isID = (elType.ElTypeNum != HTML_EL_Anchor
698: && elType.ElTypeNum != HTML_EL_MAP);
699: }
700: else
701: {
702: isID = FALSE;
703: /* get the BODY element of source document */
704: root = TtaGetMainRoot (sourceDoc);
705: elType = TtaGetElementType (root);
706: elType.ElTypeNum = HTML_EL_BODY;
707: root = TtaSearchTypedElement (elType, SearchForward, root);
708: }
709:
710: if (root != NULL)
1.12 cvs 711: {
712: /* don't check the abstract tree against the structure schema */
713: checkingMode = TtaGetStructureChecking (destDoc);
714: TtaSetStructureChecking (0, destDoc);
715: /* get elem, the ancestor of *el which is a child of a DIV or BODY
716: element in the destination document. The copied elements will be
717: inserted just before this element. */
718: elem = *el;
719: do
1.1 cvs 720: {
1.12 cvs 721: ancestor = TtaGetParent (elem);
722: if (ancestor != NULL)
723: {
724: elType = TtaGetElementType (ancestor);
725: if (elType.ElTypeNum == HTML_EL_BODY ||
726: elType.ElTypeNum == HTML_EL_Division)
727: ancestor = NULL;
728: else
729: elem = ancestor;
730: }
1.1 cvs 731: }
1.12 cvs 732: while (ancestor != NULL);
733: parent = TtaGetParent (elem);
1.14 cvs 734:
735: /* insert a DIV element */
1.15 cvs 736: elType.ElTypeNum = HTML_EL_Division;
1.16 cvs 737: lastInserted = TtaNewElement (destDoc, elType);
738: TtaInsertSibling (lastInserted, elem, TRUE, destDoc);
739: RegisterSubDoc (lastInserted, url);
1.17 ! cvs 740: CreateTargetAnchor (destDoc, lastInserted);
1.14 cvs 741:
1.12 cvs 742: /* do copy */
1.16 cvs 743: firstInserted = NULL;
1.17 ! cvs 744: if (isID)
! 745: srce = root;
! 746: else
! 747: srce = TtaGetFirstChild (root);
1.12 cvs 748: while (srce != NULL)
1.1 cvs 749: {
1.12 cvs 750: copy = TtaCopyTree (srce, sourceDoc, destDoc, parent);
751: if (copy != NULL)
752: {
1.16 cvs 753: if (firstInserted == NULL)
1.12 cvs 754: /* this is the first copied element. Insert it before elem */
755: {
1.16 cvs 756: TtaInsertFirstChild (©, lastInserted, destDoc);
1.12 cvs 757: firstInserted = copy;
758: }
759: else
760: /* insert the new copied element after the element previously
761: copied */
762: TtaInsertSibling (copy, lastInserted, FALSE, destDoc);
763: lastInserted = copy;
764: /* update the NAMEs and URLs in the copied element */
765: event.document = destDoc;
766: event.position = sourceDoc;
767: UpdateURLsInSubtree(&event, copy);
768: }
769: /* get the next element in the source document */
770: old = srce;
771: TtaNextSibling (&srce);
772: if (deleteTree)
773: TtaDeleteTree (old, sourceDoc);
1.13 cvs 774: /* Stop here if the target points to a specific element with an ID */
775: if (isID)
776: srce = NULL;
1.1 cvs 777: }
1.12 cvs 778:
779: /* delete the element(s) containing the link to the copied document */
780: /* delete the parent element of *el and all empty ancestors */
781: elem = TtaGetParent (*el);
782: do
1.1 cvs 783: {
1.12 cvs 784: sibling = elem;
785: TtaNextSibling (&sibling);
786: if (sibling == NULL)
787: {
788: sibling = elem;
789: TtaPreviousSibling (&sibling);
790: if (sibling == NULL)
791: elem = TtaGetParent (elem);
792: }
1.1 cvs 793: }
1.12 cvs 794: while (sibling == NULL);
795: TtaDeleteTree (elem, destDoc);
796: /* restore previous chacking mode */
797: TtaSetStructureChecking (checkingMode, destDoc);
798: /* return the address of the first copied element */
799: *el = firstInserted;
800: }
1.1 cvs 801: }
802:
803: /*----------------------------------------------------------------------
804: GetIncludedDocuments
805: Look forward, starting from element el, for a link (A) with attribute
1.5 cvs 806: REL="chapter" or REL="subdocument" and replace that link by the contents
807: of the target document.
1.1 cvs 808: ----------------------------------------------------------------------*/
809: #ifdef __STDC__
810: static Element GetIncludedDocuments (Element el, Document document)
811: #else
812: static Element GetIncludedDocuments (el, document)
813: Element el;
814: Document document;
815: #endif
816: {
817: Element link, next;
818: Attribute RelAttr, HrefAttr;
819: AttributeType attrType;
820: int length;
1.12 cvs 821: char *text, *ptr, *url;
1.1 cvs 822: Document includedDocument, newdoc;
823:
824: attrType.AttrSSchema = TtaGetDocumentSSchema (document);
825: attrType.AttrTypeNum = HTML_ATTR_REL;
826: link = el;
827: RelAttr = NULL;
1.5 cvs 828: /* looks for an anchor having an attribute REL="chapter" or
829: REL="subdocument" */
1.1 cvs 830: while (link != NULL && RelAttr == NULL)
831: {
832: TtaSearchAttribute (attrType, SearchForward, link, &link, &RelAttr);
833: if (link != NULL && RelAttr != NULL)
834: {
835: length = TtaGetTextAttributeLength (RelAttr);
836: text = TtaGetMemory (length + 1);
837: TtaGiveTextAttributeValue (RelAttr, text, &length);
1.5 cvs 838: if (strcasecmp (text, "chapter") && strcasecmp (text, "subdocument"))
1.1 cvs 839: RelAttr = NULL;
840: TtaFreeMemory (text);
841: }
842: }
843:
844: if (RelAttr != NULL && link != NULL)
845: /* a link with attribute REL="Chapter" has been found */
846: {
847: next = link;
848: attrType.AttrTypeNum = HTML_ATTR_HREF_;
849: HrefAttr = TtaGetAttribute (link, attrType);
850: if (HrefAttr != NULL)
851: /* this link has an attribute HREF */
852: {
853: length = TtaGetTextAttributeLength (HrefAttr);
854: text = TtaGetMemory (length + 1);
855: TtaGiveTextAttributeValue (HrefAttr, text, &length);
856: ptr = strrchr (text, '#');
1.12 cvs 857: url = text;
858: if (ptr != NULL)
859: {
860: if (ptr == text)
861: url = NULL;
1.16 cvs 862: /* link to a particular position within a remote document */
1.12 cvs 863: ptr[0] = EOS;
864: ptr = &ptr[1];
865: }
866:
867: if (url != NULL)
868: /* this link designate an external document */
1.1 cvs 869: {
870: /* create a new document and loads the target document */
871: includedDocument = TtaNewDocument ("HTML", "tmp");
1.12 cvs 872: TtaSetStatus (document, 1, TtaGetMessage (AMAYA, AM_FETCHING), url);
873: newdoc = GetHTMLDocument (url, NULL, includedDocument,
1.1 cvs 874: document, DC_TRUE);
875: if (newdoc != 0 && newdoc != document)
1.12 cvs 876: {
1.1 cvs 877: /* it's not the document itself */
878: /* copy the target document at the position of the link */
1.16 cvs 879: MoveDocumentBody (&next, document, newdoc, ptr, url,
1.1 cvs 880: newdoc == includedDocument);
1.12 cvs 881: }
1.1 cvs 882: FreeDocumentResource (includedDocument);
883: TtaCloseDocument (includedDocument);
884: }
1.12 cvs 885:
1.1 cvs 886: TtaFreeMemory (text);
887: }
888: return (next);
889: }
890: return (NULL);
891: }
892:
893:
894: /*----------------------------------------------------------------------
895: MakeBook
896: Replace all links in a document which have an attribute REL="chapter"
1.5 cvs 897: or REL="subdocument" by the corresponding target document.
1.1 cvs 898: ----------------------------------------------------------------------*/
899: #ifdef __STDC__
900: void MakeBook (Document document, View view)
901: #else
902: void MakeBook (document, view)
903: Document document;
904: View view;
905: #endif
906: {
907: Element root, body, el;
908: ElementType elType;
909:
1.16 cvs 910: docBook = document;
1.1 cvs 911: root = TtaGetMainRoot (document);
912: elType = TtaGetElementType (root);
913: elType.ElTypeNum = HTML_EL_BODY;
914: body = TtaSearchTypedElement (elType, SearchForward, root);
915: TtaSetDocumentModified (document);
916: el = body;
917: while (el != NULL)
918: el = GetIncludedDocuments (el, document);
1.7 cvs 919: TtaSetStatus (document, 1, TtaGetMessage (AMAYA, AM_DOCUMENT_LOADED), "");
1.16 cvs 920: /* update internal links */
921: SetInternalLinks (document);
922: /* remove registered sub-documents */
923: FreeSubDocTable ();
924: docBook = 0;
1.1 cvs 925: }
Webmaster