Annotation of Amaya/amaya/HTMLbook.c, revision 1.8

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

Webmaster