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

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

Webmaster