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

1.1       cvs         1: /*
                      2:  *
1.80      cvs         3:  *  (c) COPYRIGHT MIT and INRIA, 1996-2001
1.1       cvs         4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
1.65      cvs         7:  
1.1       cvs         8: /*
                      9:  * Initialization functions and button functions of Amaya application.
                     10:  *
                     11:  * Authors: V. Quint, I. Vatton
1.85      cvs        12:  *          R. Guetari (W3C/INRIA) - Windows version.
1.1       cvs        13:  */
                     14: 
                     15: 
                     16: /* Included headerfiles */
                     17: #define THOT_EXPORT
                     18: #include "amaya.h"
1.44      cvs        19: #include "AHTURLTools_f.h"
1.1       cvs        20: #include "print.h"
1.51      cvs        21: #include "css.h"
1.1       cvs        22: 
1.25      cvs        23: /* structure to register sub-documents in MakeBook function*/
                     24: typedef struct _SubDoc
                     25:   {
                     26:      struct _SubDoc  *SDnext;
                     27:      Element          SDel;
1.85      cvs        28:      char            *SDname;
1.25      cvs        29:   }SubDoc;
                     30: 
1.29      cvs        31: /* the structure used for the GetIncludedDocuments_callback function */
1.58      cvs        32: typedef struct _IncludeCtxt
1.53      cvs        33: {
1.58      cvs        34:   Element              div; /* enclosing element for the search */
                     35:   Element              link; /* current processed link */
1.85      cvs        36:   char                *url; /* called url */
                     37:   char                 *name; /* the fragment name */
1.58      cvs        38:   struct _IncludeCtxt  *ctxt; /* the previous context */
                     39: } IncludeCtxt;
1.29      cvs        40: 
1.55      cvs        41: /* shared with windialogapi.c */
                     42: ThotBool         PrintURL;
                     43: ThotBool        NumberLinks;
                     44: ThotBool        WithToC;
                     45: ThotBool         IgnoreCSS;
                     46: 
1.25      cvs        47: static struct _SubDoc  *SubDocs;
1.85      cvs        48: static char             PSfile[MAX_PATH];
                     49: static char             PPrinter[MAX_PATH];
                     50: static char            *DocPrintURL;
1.53      cvs        51: static Document                DocPrint;
1.2       cvs        52: static int              PaperPrint;
1.73      cvs        53: static int              ManualFeed = PP_OFF;
1.71      cvs        54: static int              Orientation;
1.2       cvs        55: static int              PageSize;
1.71      cvs        56: static int              PagePerSheet;
1.1       cvs        57: 
                     58: #include "init_f.h"
                     59: #include "HTMLactions_f.h"
                     60: #include "HTMLbook_f.h"
                     61: #include "HTMLedit_f.h"
1.25      cvs        62: #include "HTMLhistory_f.h"
1.51      cvs        63: #include "UIcss_f.h"
1.1       cvs        64: 
1.31      cvs        65: #ifdef _WINDOWS 
1.37      cvs        66: #include "wininclude.h"
1.31      cvs        67: #endif /* _WINDOWS */
                     68: 
1.58      cvs        69: static ThotBool GetIncludedDocuments ();
1.1       cvs        70: 
                     71: /*----------------------------------------------------------------------
1.16      cvs        72:   RegisterSubDoc adds a new entry in SubDoc table.
                     73:   ----------------------------------------------------------------------*/
1.85      cvs        74: static void         RegisterSubDoc (Element el, char *url)
1.16      cvs        75: {
                     76:   struct _SubDoc  *entry, *last;
                     77: 
                     78:   if (url == NULL || url[0] == EOS)
                     79:     return;
                     80: 
                     81:   entry = TtaGetMemory (sizeof (struct _SubDoc));
                     82:   entry->SDnext = NULL;
                     83:   entry->SDel = el;
1.83      cvs        84:   entry->SDname = TtaStrdup (url);
1.16      cvs        85: 
                     86:   if (SubDocs == NULL)
                     87:     SubDocs = entry;
                     88:   else
                     89:     {
                     90:       last = SubDocs;
                     91:       while (last->SDnext != NULL)
                     92:        last = last->SDnext;
                     93:       last->SDnext = entry;
                     94:     }
                     95: }
                     96: 
                     97: 
                     98: /*----------------------------------------------------------------------
                     99:   SearchSubDoc searches whether a document name is registered or not
                    100:   within the SubDoc table.
                    101:   Return the DIV element that correspond to the sub-document or NULL.
                    102:   ----------------------------------------------------------------------*/
1.85      cvs       103: static Element      SearchSubDoc (char *url)
1.16      cvs       104: {
                    105:   Element          el;
                    106:   struct _SubDoc  *entry;
1.47      cvs       107:   ThotBool         docFound;
1.16      cvs       108: 
                    109:   if (url == NULL || url[0] == EOS)
                    110:     return (NULL);
                    111: 
                    112:   entry = SubDocs;
                    113:   docFound = FALSE;
                    114:   el = NULL;
                    115:   while (!docFound && entry != NULL)
                    116:     {
1.83      cvs       117:       docFound = (strcmp (url, entry->SDname) == 0);
1.16      cvs       118:       if (!docFound)
                    119:        entry = entry->SDnext;
                    120:       else
                    121:        /* document found -> return the DIV element */
                    122:        el = entry->SDel;
                    123:     }
                    124:   return (el);
                    125: }
                    126: 
                    127: /*----------------------------------------------------------------------
                    128:   FreeSubDocTable frees all entries in SubDoc table.
                    129:   ----------------------------------------------------------------------*/
                    130: static void         FreeSubDocTable ()
                    131: {
                    132:   struct _SubDoc  *entry, *last;
                    133: 
                    134:   entry = SubDocs;
                    135:   while (entry != NULL)
                    136:     {
                    137:       last = entry;
                    138:       entry = entry->SDnext;
                    139:       TtaFreeMemory (last->SDname);
                    140:       TtaFreeMemory (last);
                    141:     }
                    142:   SubDocs = NULL;
                    143: }
                    144: 
                    145: 
                    146: 
                    147: /*----------------------------------------------------------------------
1.1       cvs       148:   SetInternalLinks
1.8       cvs       149:   Associate an InternalLink attribute with all anchor (A) elements of the
                    150:   document which designate an element in the same document.
1.1       cvs       151:   InternalLink is a Thot reference attribute that links a source and a
                    152:   target anchor and that allows P schemas to display and print cross-references
                    153:   ----------------------------------------------------------------------*/
1.4       cvs       154: void             SetInternalLinks (Document document)
1.1       cvs       155: {
1.32      cvs       156:   Element              el, div, link, target, sibling;
                    157:   ElementType          elType, linkType;
1.16      cvs       158:   Attribute            HrefAttr, IntLinkAttr;
1.17      cvs       159:   Attribute             attr, ExtLinkAttr;
1.16      cvs       160:   AttributeType                attrType;
1.85      cvs       161:   char                *text, *ptr, *url; 
1.83      cvs       162:   char                  number[10];
                    163:   char                  value[MAX_LENGTH];
1.25      cvs       164:   int                  length, i, volume;
                    165:   int                   status, position;
1.47      cvs       166:   ThotBool              split;
1.1       cvs       167: 
1.16      cvs       168:   /* Remember the current status of the document */
                    169:   status = TtaIsDocumentModified (document);
1.32      cvs       170:   el = TtaGetMainRoot (document);
                    171:   volume = TtaGetElementVolume (el);
                    172:   elType = TtaGetElementType (el);
                    173:   elType.ElTypeNum = HTML_EL_AnyLink;
1.16      cvs       174:   attrType.AttrSSchema = elType.ElSSchema;
1.32      cvs       175:   /* looks for all links in the document */
1.16      cvs       176:   link = el;
                    177:   while (link != NULL)
                    178:     {
1.25      cvs       179:       /* display the progression of the work */
                    180:       el = link;
                    181:       position = 0;
                    182:       while (el != NULL)
                    183:        {
                    184:          sibling = el;
                    185:          do
                    186:            {
                    187:              /* add volume of each previous element */
                    188:              TtaPreviousSibling (&sibling);
                    189:              if (sibling != NULL)
                    190:                position += TtaGetElementVolume (sibling);
                    191:            }
                    192:          while (sibling != NULL);
                    193:          el = TtaGetParent (el);
                    194:        }
1.83      cvs       195:       sprintf (number, "%d", position*100/volume);
1.25      cvs       196:       TtaSetStatus (document, 1, TtaGetMessage (AMAYA, AM_UPDATED_LINK), number);
                    197:       TtaHandlePendingEvents ();
1.16      cvs       198:       link = TtaSearchTypedElement (elType, SearchForward, link);
                    199:       if (link != NULL)
1.32      cvs       200:        /* a link has been found */
1.16      cvs       201:        {
1.32      cvs       202:          linkType = TtaGetElementType (link);
                    203:          if (linkType.ElTypeNum == HTML_EL_Anchor)
                    204:             attrType.AttrTypeNum = HTML_ATTR_HREF_;
                    205:          else
                    206:             attrType.AttrTypeNum = HTML_ATTR_cite;
1.16      cvs       207:          HrefAttr = TtaGetAttribute (link, attrType);
                    208:          attrType.AttrTypeNum = HTML_ATTR_InternalLink;
                    209:          IntLinkAttr = TtaGetAttribute (link, attrType);
                    210:          attrType.AttrTypeNum = HTML_ATTR_ExternalLink;
                    211:          ExtLinkAttr = TtaGetAttribute (link, attrType);
                    212:          if (HrefAttr == NULL)
1.32      cvs       213:            /* this element is not a link (no href or cite attribute) */
1.16      cvs       214:            /* remove attributes InternalLink and ExternalLink if they
                    215:               are present */
                    216:            {
1.8       cvs       217:              if (IntLinkAttr != NULL)
1.16      cvs       218:                TtaRemoveAttribute (link, IntLinkAttr, document);
                    219:              if (ExtLinkAttr != NULL)
                    220:                TtaRemoveAttribute (link, ExtLinkAttr, document);          
                    221:            }
                    222:          else
1.32      cvs       223:            /* this element has an HREF or cite attribute */
1.16      cvs       224:            {
                    225:              length = TtaGetTextAttributeLength (HrefAttr);
1.83      cvs       226:              text = TtaGetMemory (length + 1);
1.16      cvs       227:              TtaGiveTextAttributeValue (HrefAttr, text, &length);
                    228: 
                    229:              /* does an external link become an internal link ? */
1.25      cvs       230:              if (document == DocBook && SubDocs != NULL)
1.16      cvs       231:                {
1.83      cvs       232:                  ptr = strrchr (text, '#');
1.16      cvs       233:                  url = text;
1.18      cvs       234:                  split = FALSE;
1.16      cvs       235:                  if (ptr == text)
                    236:                      /* a local link */
                    237:                      url = NULL;
1.17      cvs       238:                  else if (ptr != NULL)
1.16      cvs       239:                    {
                    240:                      /* split url and name part */
                    241:                      ptr[0] = EOS;
                    242:                      split = TRUE;
                    243:                    }
                    244: 
                    245:                  /* Is it a sub-document */
                    246:                  div = SearchSubDoc (url);
                    247:                  if (split)
                    248:                    /* retore the mark */
                    249:                    ptr[0] = '#';
                    250: 
                    251:                  if (div == NULL)
                    252:                    {
                    253:                      /* it's not a sub-document */
                    254:                      if (url == NULL)
                    255:                        /* a local link */
                    256:                        ptr = &text[1];
                    257:                      else
                    258:                        /* still an externa; link */
                    259:                        ptr = NULL;
                    260:                    }
                    261:                  else
                    262:                    {
                    263:                      /* this link becomes internal */
1.17      cvs       264:                      if (ptr != NULL)
1.16      cvs       265:                        {
1.17      cvs       266:                          /* get the target name */
1.83      cvs       267:                          strcpy (value, ptr);
                    268:                          length = strlen (value);
1.17      cvs       269:                          /* check whether the name changed */
                    270:                          i = 0;
                    271:                          target = SearchNAMEattribute (document, &value[1], NULL);
                    272:                          while (target != NULL)
1.16      cvs       273:                            {
1.17      cvs       274:                              /* is it the right NAME */
                    275:                              if (TtaIsAncestor (target, div))
                    276:                                target = NULL;
                    277:                              else
                    278:                                {
                    279:                                  /* continue the search */
                    280:                                  i++;
1.83      cvs       281:                                  sprintf (&value[length], "%d", i);
1.32      cvs       282:                                  target = SearchNAMEattribute (document,
                    283:                                                        &value[1], NULL);
1.17      cvs       284:                                }
1.16      cvs       285:                            }
                    286:                        }
1.17      cvs       287:                      else
                    288:                        {
                    289:                          /* get the DIV name */
                    290:                          attrType.AttrTypeNum = HTML_ATTR_ID;
                    291:                          attr = TtaGetAttribute (div, attrType);
                    292:                          length = 200;
                    293:                          value[0] = '#';
                    294:                          TtaGiveTextAttributeValue (attr, &value[1], &length);
                    295:                        }
1.16      cvs       296:                      ptr = &value[1];
                    297:                      TtaSetAttributeText (HrefAttr, value, link, document);
                    298:                    }
                    299:                }
                    300:              else if (text[0] == '#')
                    301:                  ptr = &text[1];
                    302:              else
                    303:                ptr = NULL;
                    304: 
                    305:              if (ptr != NULL)
                    306:                /* it's an internal link. Attach an attribute InternalLink */
                    307:                /* to the link, if this attribute does not exist yet */
                    308:                {
                    309:                  if (IntLinkAttr == NULL)
                    310:                    {
                    311:                      attrType.AttrTypeNum = HTML_ATTR_InternalLink;
                    312:                      IntLinkAttr = TtaNewAttribute (attrType);
                    313:                      TtaAttachAttribute (link, IntLinkAttr, document);
                    314:                    }
                    315:                  /* looks for the target element */
                    316:                  target = SearchNAMEattribute (document, ptr, NULL);
                    317:                  if (target != NULL)
                    318:                    /* set the Thot link */
1.32      cvs       319:                    TtaSetAttributeReference (IntLinkAttr, link, document,
                    320:                                              target, document);
1.16      cvs       321:                }
                    322:              else
                    323:                /* it's an external link */
                    324:                {
                    325:                  /* Remove the InternalLink attribute if it is present */
                    326:                  if (IntLinkAttr != NULL)
                    327:                    TtaRemoveAttribute (link, IntLinkAttr, document);
                    328:                  /* create an ExternalLink attribute if there is none */
                    329:                  if (ExtLinkAttr == NULL)
                    330:                    {
                    331:                      attrType.AttrTypeNum = HTML_ATTR_ExternalLink;
                    332:                      ExtLinkAttr = TtaNewAttribute (attrType);
                    333:                      TtaAttachAttribute (link, ExtLinkAttr, document);
                    334:                    }
                    335:                }
                    336:              TtaFreeMemory (text);
                    337:            }
                    338:        }
                    339:     }
                    340:   /* Reset document status */
                    341:   if (!status)
                    342:     TtaSetDocumentUnmodified (document);
1.3       cvs       343: }
                    344: 
                    345: /*----------------------------------------------------------------------
                    346:   CheckPrintingDocument reinitialize printing parameters as soon as
                    347:   the printing document changes.
                    348:   ----------------------------------------------------------------------*/
1.89    ! vatton    349: static void CheckPrintingDocument (Document document)
1.3       cvs       350: {
1.83      cvs       351:   char         docName[MAX_LENGTH];
                    352:   char        *ptr; 
                    353:   char         suffix[MAX_LENGTH];
1.81      cvs       354:   int            lg;
                    355: 
                    356:   if (DocPrint != document || DocPrintURL == NULL ||
1.83      cvs       357:       strcmp(DocPrintURL, DocumentURLs[document]))
1.81      cvs       358:     {
                    359:       /* initialize print parameters */
                    360:       TtaFreeMemory (DocPrintURL);
                    361:       DocPrint = document;
                    362:       DocPrintURL = TtaStrdup (DocumentURLs[document]);
                    363:       
                    364:       /* define the new default PS file */
                    365:       ptr = TtaGetEnvString ("APP_TMPDIR");
                    366:       if (ptr != NULL && TtaCheckDirectory (ptr))
1.83      cvs       367:        strcpy (PSfile, ptr);
1.81      cvs       368:       else
1.83      cvs       369:        strcpy (PSfile, TtaGetDefEnvString ("APP_TMPDIR"));
                    370:       lg = strlen (PSfile);
                    371:       if (PSfile[lg - 1] == DIR_SEP)
                    372:        PSfile[--lg] = EOS;
                    373:       strcpy (docName, TtaGetDocumentName (document));
1.81      cvs       374:       ExtractSuffix (docName, suffix);
1.83      cvs       375:       sprintf (&PSfile[lg], "%c%s.ps", DIR_SEP, docName);
1.81      cvs       376:       TtaSetPsFile (PSfile);
                    377:     }
1.3       cvs       378: }
                    379: 
                    380: /*----------------------------------------------------------------------
1.72      cvs       381:    PrintDocument prints the document using predefined parameters.
1.3       cvs       382:    ----------------------------------------------------------------------*/  
1.45      cvs       383: static void         PrintDocument (Document doc, View view)
1.3       cvs       384: {
1.34      cvs       385:   AttributeType      attrType;
1.84      cvs       386:   ElementType        elType;
1.34      cvs       387:   Attribute          attr;
1.84      cvs       388:   Element            el, docEl;
1.85      cvs       389:   char              *files, *dir;
1.84      cvs       390:   char               viewsToPrint[MAX_PATH];
1.47      cvs       391:   ThotBool           status, textFile;
1.3       cvs       392: 
1.38      cvs       393:   textFile = (DocumentTypes[doc] == docText ||
1.75      cvs       394:               DocumentTypes[doc] == docSource ||
1.74      cvs       395:              DocumentTypes[doc] == docCSS);
1.38      cvs       396: 
1.81      cvs       397:   /* initialize printing information */
1.75      cvs       398:   CheckPrintingDocument (doc);
1.83      cvs       399:   strcpy (viewsToPrint, "Formatted_view ");
1.75      cvs       400:   if (DocumentTypes[doc] == docHTML && WithToC)
1.83      cvs       401:     strcat (viewsToPrint, "Table_of_contents ");
1.75      cvs       402:   
                    403:   if (textFile)
                    404:     {
                    405:       if (PageSize == PP_A4)
                    406:        {
                    407:          if (Orientation == PP_Landscape)
1.82      cvs       408:            TtaSetPrintSchema ("TextFilePL");
1.75      cvs       409:          else
1.82      cvs       410:            TtaSetPrintSchema ("TextFilePP");
1.75      cvs       411:        }
                    412:       else
                    413:        {
                    414:          if (Orientation == PP_Landscape)
1.82      cvs       415:            TtaSetPrintSchema ("TextFileUSL");
1.75      cvs       416:          else
1.82      cvs       417:            TtaSetPrintSchema ("TextFilePPUS");
1.75      cvs       418:        }
                    419:     }
                    420:   else if (DocumentTypes[doc] == docSVG)
1.88      vatton    421:     TtaSetPrintSchema ("SVGP");
1.75      cvs       422:   else if (DocumentTypes[doc] == docMath)
1.82      cvs       423:     TtaSetPrintSchema ("MathMLP");
1.75      cvs       424:   else if (DocumentTypes[doc] == docAnnot)
1.82      cvs       425:     TtaSetPrintSchema ("AnnotP");
1.75      cvs       426:   else if (DocumentTypes[doc] == docHTML && NumberLinks)
                    427:     /* display numbered links */
                    428:     {
                    429:       /* associate an attribute InternalLink with all anchors refering
                    430:         a target in the same document.  This allows P schemas to work
                    431:         properly */
                    432:       SetInternalLinks (DocPrint);
                    433:       if (PageSize == PP_A4)
                    434:        {
                    435:          if (Orientation == PP_Landscape)
1.82      cvs       436:            TtaSetPrintSchema ("HTMLPLL");
1.75      cvs       437:          else
1.82      cvs       438:            TtaSetPrintSchema ("HTMLPLP");
1.75      cvs       439:        }
                    440:       else
                    441:        {
                    442:          if (Orientation == PP_Landscape)
1.82      cvs       443:            TtaSetPrintSchema ("HTMLUSLL");
1.75      cvs       444:          else
1.82      cvs       445:            TtaSetPrintSchema ("HTMLPLPUS");
1.75      cvs       446:        }
1.83      cvs       447:       strcat (viewsToPrint, "Links_view ");
1.75      cvs       448:     }
                    449:   else if (PageSize == PP_A4)
                    450:     {
                    451:       if (Orientation == PP_Landscape)
1.82      cvs       452:        TtaSetPrintSchema ("HTMLPL");
1.75      cvs       453:       else
1.82      cvs       454:        TtaSetPrintSchema ("HTMLPP");
1.75      cvs       455:     }
                    456:   else
                    457:     {
                    458:       if (Orientation == PP_Landscape)
1.82      cvs       459:        TtaSetPrintSchema ("HTMLUSL");
1.75      cvs       460:       else
1.82      cvs       461:        TtaSetPrintSchema ("HTMLPPUS");
1.75      cvs       462:     }    
                    463:   
                    464:   status = TtaIsDocumentModified (doc);
1.71      cvs       465: 
1.75      cvs       466:   if (textFile || DocumentTypes[doc] == docImage ||
                    467:       DocumentTypes[doc] == docHTML)
                    468:     {
                    469:       /* post or remove the PrintURL attribute */
                    470:       attrType.AttrSSchema = TtaGetDocumentSSchema (doc);
1.84      cvs       471:       elType.ElSSchema = attrType.AttrSSchema;
1.75      cvs       472:       if (textFile)
1.84      cvs       473:        {
                    474:          elType. ElTypeNum = TextFile_EL_TextFile;
                    475:          attrType.AttrTypeNum = TextFile_ATTR_PrintURL;
                    476:        }
1.75      cvs       477:       else
1.84      cvs       478:        {
                    479:          elType. ElTypeNum = HTML_EL_HTML;
                    480:          attrType.AttrTypeNum = HTML_ATTR_PrintURL;
                    481:        }
                    482:       docEl = TtaGetMainRoot (doc);
                    483:       el = TtaSearchTypedElement (elType, SearchForward, docEl);
1.75      cvs       484:       attr = TtaGetAttribute (el, attrType);
                    485:       if (!attr && PrintURL)
                    486:        {
                    487:          attr = TtaNewAttribute (attrType);
                    488:          TtaAttachAttribute (el, attr, doc);
                    489:        }
                    490:       if (attr && !PrintURL)
                    491:        TtaRemoveAttribute (el, attr, doc);
                    492:     }
                    493:   
                    494:   /* get the path dir where css files have to be stored */
1.79      cvs       495:   if ((DocumentTypes[doc] == docHTML || DocumentTypes[doc] == docSVG) &&
1.78      cvs       496:       !IgnoreCSS)
1.75      cvs       497:     {
                    498:       TtaGetPrintNames (&files, &dir);
                    499:       /* store css files and get the list of names */
                    500:       files = CssToPrint (doc, dir);
                    501:     }
                    502:   else
                    503:     files = NULL;
                    504:   TtaPrint (DocPrint, viewsToPrint, files);
                    505:   if (files)
                    506:     TtaFreeMemory (files);
                    507:   if (!status)
                    508:     TtaSetDocumentUnmodified (doc);
1.1       cvs       509: }
                    510: 
1.45      cvs       511: /*----------------------------------------------------------------------
                    512:    PrintAs prints the document using predefined parameters.
                    513:    ----------------------------------------------------------------------*/  
1.89    ! vatton    514: void PrintAs (Document doc, View view)
1.45      cvs       515: {
1.72      cvs       516: #ifdef _WINDOWS
                    517:   DocPrint = doc;
                    518:   ReusePrinterDC ();
1.45      cvs       519: #else /* _WINDOWS */
                    520:   PrintDocument (doc, view);
                    521: #endif /* _WINDOWS */
                    522: }
1.1       cvs       523: 
                    524: /*----------------------------------------------------------------------
                    525:    CallbackImage manage returns of Picture form.                   
                    526:   ----------------------------------------------------------------------*/
1.85      cvs       527: void CallbackPrint (int ref, int typedata, char *data)
1.1       cvs       528: {
                    529:   int                 val;
                    530: 
                    531:   val = (int) data;
1.53      cvs       532:   switch (ref - BasePrint)
1.1       cvs       533:     {
1.69      cvs       534:     case FormPrint:
                    535:       TtaDestroyDialogue (BasePrint + FormPrint);
1.1       cvs       536:       switch (val)
                    537:        {
                    538:        case 1:
1.53      cvs       539:          TtaSetPrintCommand (PPrinter);
1.81      cvs       540:          TtaSetPsFile (PSfile);
1.40      cvs       541:          /* update the environment variable */
1.53      cvs       542:          TtaSetEnvString ("THOTPRINT", PPrinter, TRUE);
                    543:          PrintDocument (DocPrint, 1);
1.1       cvs       544:          break;
                    545:        default:
                    546:          break;
                    547:        }
                    548:       break;
1.69      cvs       549:     case PrintOptions:
1.1       cvs       550:       switch (val)
                    551:        {
                    552:        case 0:
                    553:          /* Manual feed option */
1.2       cvs       554:          if (ManualFeed == PP_ON)
                    555:            ManualFeed = PP_OFF;
                    556:          else
                    557:            ManualFeed = PP_ON;
1.81      cvs       558:          TtaSetPrintParameter (PP_ManualFeed, ManualFeed);
1.1       cvs       559:          break;
                    560:        case 1:
                    561:          /* Toc option */
1.53      cvs       562:          WithToC = !WithToC;
1.1       cvs       563:          break;
                    564:        case 2:
1.53      cvs       565:          /* NumberLinks option */
                    566:          NumberLinks = !NumberLinks;
1.34      cvs       567:        case 3:
                    568:          /* URL option */
1.53      cvs       569:          PrintURL = !PrintURL;
                    570:          break;
                    571:        case 4:
                    572:          /* CSS option */
                    573:          IgnoreCSS = !IgnoreCSS;
1.1       cvs       574:          break;
                    575:        }
                    576:       break;
1.69      cvs       577:     case PaperFormat:
1.1       cvs       578:       /* page size submenu */
                    579:       switch (val)
                    580:        {
                    581:        case 0:
1.2       cvs       582:          PageSize = PP_A4;
1.1       cvs       583:          break;
                    584:        case 1:
1.2       cvs       585:          PageSize = PP_US;
1.1       cvs       586:          break;
                    587:        }
1.81      cvs       588:       TtaSetPrintParameter (PP_PaperSize, PageSize);
1.1       cvs       589:       break;
1.71      cvs       590:     case PaperOrientation:
                    591:       /* orientation submenu */
                    592:       Orientation = val;
1.81      cvs       593:       TtaSetPrintParameter (PP_Orientation, Orientation);
1.71      cvs       594:       break;
                    595:     case PPagesPerSheet:
                    596:       /* pages per sheet submenu */
                    597:       switch (val)
                    598:        {
                    599:        case 0:
                    600:          PagePerSheet = 1;
                    601:          break;
                    602:        case 1:
                    603:          PagePerSheet = 2;
                    604:          break;
                    605:        case 2:
                    606:          PagePerSheet = 4;
                    607:          break;
                    608:        }
1.81      cvs       609:       TtaSetPrintParameter (PP_PagesPerSheet, PagePerSheet);
1.71      cvs       610:       break;
1.69      cvs       611:     case PrintSupport:
1.1       cvs       612:       /* paper print/save PostScript submenu */
                    613:       switch (val)
                    614:        {
                    615:        case 0:
1.2       cvs       616:          if (PaperPrint == PP_PS)
1.1       cvs       617:            {
1.2       cvs       618:              PaperPrint = PP_PRINTER;
1.69      cvs       619: #ifndef _WINDOWS
                    620:              TtaSetTextForm (BasePrint + PPrinterName, PPrinter);
                    621: #endif /* !_WINDOWS */
1.81      cvs       622:              TtaSetPrintParameter (PP_Destination, PaperPrint);
1.1       cvs       623:            }
                    624:          break;
                    625:        case 1:
1.2       cvs       626:          if (PaperPrint == PP_PRINTER)
1.1       cvs       627:            {
1.2       cvs       628:              PaperPrint = PP_PS;
1.80      cvs       629: #ifndef _WINDOWS
1.81      cvs       630:              TtaSetTextForm (BasePrint + PPrinterName, PSfile);
1.80      cvs       631: #endif /* !_WINDOWS */
1.81      cvs       632:              TtaSetPrintParameter (PP_Destination, PaperPrint);
1.1       cvs       633:            }
                    634:          break;
                    635:        }
                    636:       break;
1.69      cvs       637:     case PPrinterName:
1.1       cvs       638:       if (data[0] != '\0')
1.80      cvs       639:        {
1.2       cvs       640:        if (PaperPrint == PP_PRINTER)
1.40      cvs       641:            /* text capture zone for the printer name */
1.83      cvs       642:            strncpy (PPrinter, data, MAX_PATH);
1.1       cvs       643:        else
                    644:          /* text capture zone for the name of the PostScript file */
1.83      cvs       645:          strncpy (PSfile, data, MAX_PATH);
1.80      cvs       646:        }
1.1       cvs       647:       break;
                    648:     }
                    649: }
                    650: 
                    651: /*----------------------------------------------------------------------
                    652:   ----------------------------------------------------------------------*/
                    653: void                InitPrint (void)
                    654: {
1.83      cvs       655:   char* ptr;
1.1       cvs       656: 
1.53      cvs       657:    BasePrint = TtaSetCallback (CallbackPrint, PRINT_MAX_REF);
                    658:    DocPrint = 0;
1.81      cvs       659:    DocPrintURL = NULL;
1.1       cvs       660: 
                    661:    /* read default printer variable */
1.52      cvs       662:    ptr = TtaGetEnvString ("THOTPRINT");
1.1       cvs       663:    if (ptr == NULL)
1.83      cvs       664:      strcpy (PPrinter, "");
1.1       cvs       665:    else
1.83      cvs       666:      strcpy (PPrinter, ptr);
1.81      cvs       667:    TtaSetPrintCommand (PPrinter);
1.2       cvs       668:    PaperPrint = PP_PRINTER;
1.81      cvs       669:    TtaSetPrintParameter (PP_Destination, PaperPrint);
                    670: 
                    671:    /* define the new default PrintSchema */
                    672:    NumberLinks = FALSE;
                    673:    WithToC = FALSE;
                    674:    IgnoreCSS = FALSE;
1.53      cvs       675:    PrintURL = TRUE;
1.81      cvs       676:    PageSize = TtaGetPrintParameter (PP_PaperSize);       
1.82      cvs       677:    TtaSetPrintSchema ("");
1.81      cvs       678:    /* no manual feed */
                    679:    ManualFeed = PP_OFF;
                    680:    TtaSetPrintParameter (PP_ManualFeed, ManualFeed);
                    681:    PagePerSheet = 1;
                    682:    TtaSetPrintParameter (PP_PagesPerSheet, PagePerSheet);
1.1       cvs       683: }
                    684: 
                    685: /*----------------------------------------------------------------------
1.3       cvs       686:   SetupAndPrint sets printing parameters and starts the printing process
1.1       cvs       687:   ----------------------------------------------------------------------*/
1.38      cvs       688: void                SetupAndPrint (Document doc, View view)
1.1       cvs       689: {
1.70      cvs       690: #ifndef _WINDOWS
1.83      cvs       691:   char           bufMenu[MAX_LENGTH];
1.81      cvs       692:   int              i;
1.70      cvs       693: #endif /* !_WINDOWS */
1.81      cvs       694:   ThotBool           textFile;
1.38      cvs       695: 
1.81      cvs       696:   textFile = (DocumentTypes[doc] == docText || DocumentTypes[doc] == docCSS);
                    697:   /* Print form */
                    698:   CheckPrintingDocument (doc);
1.27      cvs       699: 
1.70      cvs       700: #ifndef _WINDOWS
1.81      cvs       701:   TtaNewSheet (BasePrint + FormPrint, TtaGetViewFrame (doc, view), 
                    702:               TtaGetMessage (LIB, TMSG_LIB_PRINT), 1,
                    703:               TtaGetMessage (AMAYA, AM_BUTTON_PRINT), FALSE, 3, 'L', D_CANCEL);
                    704: 
                    705:   /* Paper format submenu */
                    706:   i = 0;
                    707:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_A4));
1.83      cvs       708:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       709:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_US));
                    710:   TtaNewSubmenu (BasePrint + PaperFormat, BasePrint + FormPrint, 0,
                    711:                 TtaGetMessage (LIB, TMSG_PAPER_SIZE), 2, bufMenu, NULL, TRUE);
                    712:   if (PageSize == PP_US)
                    713:     TtaSetMenuForm (BasePrint + PaperFormat, 1);
                    714:   else
                    715:     TtaSetMenuForm (BasePrint + PaperFormat, 0);
                    716:   
                    717:   /* Orientation submenu */
                    718:   i = 0;
                    719:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (AMAYA, AM_PORTRAIT));
1.83      cvs       720:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       721:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (AMAYA, AM_LANDSCAPE));
                    722:   TtaNewSubmenu (BasePrint + PaperOrientation, BasePrint + FormPrint, 0,
                    723:                 TtaGetMessage (AMAYA, AM_ORIENTATION), 2, bufMenu, NULL, TRUE);
                    724:   if (Orientation == PP_Landscape)
                    725:     TtaSetMenuForm (BasePrint + PaperOrientation, 1);
                    726:   else
                    727:     TtaSetMenuForm (BasePrint + PaperOrientation, 0);
                    728:   /* Pages per sheet submenu */
                    729:   i = 0;
                    730:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_1_PAGE_SHEET));
1.83      cvs       731:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       732:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_2_PAGE_SHEET));
1.83      cvs       733:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       734:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_4_PAGE_SHEET));
                    735:   TtaNewSubmenu (BasePrint + PPagesPerSheet, BasePrint + FormPrint, 0,
                    736:                 TtaGetMessage (LIB, TMSG_REDUCTION), 3, bufMenu, NULL, TRUE);
                    737:   if (PagePerSheet == 1)
                    738:     TtaSetMenuForm (BasePrint + PPagesPerSheet, 0);
                    739:   else if (PagePerSheet == 2)
                    740:     TtaSetMenuForm (BasePrint + PPagesPerSheet, 1);
                    741:   else
                    742:     TtaSetMenuForm (BasePrint + PPagesPerSheet, 2);
                    743:     
                    744:   /* Print to paper/ Print to file submenu */
                    745:   i = 0;
                    746:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_PRINTER));
1.83      cvs       747:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       748:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_PS_FILE));
                    749:   TtaNewSubmenu (BasePrint + PrintSupport, BasePrint + FormPrint, 0,
                    750:                 TtaGetMessage (LIB, TMSG_OUTPUT), 2, bufMenu, NULL, TRUE);
                    751: 
                    752:   /* PaperPrint selector */
                    753:   TtaNewTextForm (BasePrint + PPrinterName, BasePrint + FormPrint, NULL, 30, 1, TRUE);
                    754:   if (PaperPrint == PP_PRINTER)
                    755:     {
                    756:       TtaSetMenuForm (BasePrint + PrintSupport, 0);
                    757:       TtaSetTextForm (BasePrint + PPrinterName, PPrinter);
                    758:     }
                    759:   else
                    760:     {
                    761:       TtaSetMenuForm (BasePrint + PrintSupport, 1);
                    762:       TtaSetTextForm (BasePrint + PPrinterName, PSfile);
                    763:     }
1.1       cvs       764: 
1.81      cvs       765:   /* The toggle */
                    766:   i = 0;
                    767:   sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (LIB, TMSG_MANUAL_FEED));
1.83      cvs       768:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       769:   sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_PRINT_TOC));
1.83      cvs       770:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       771:   sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_NUMBERED_LINKS));
1.83      cvs       772:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       773:   sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_PRINT_URL));
1.83      cvs       774:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       775:   sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_WITH_CSS));
                    776:   TtaNewToggleMenu (BasePrint + PrintOptions, BasePrint + FormPrint,
                    777:                    TtaGetMessage (LIB, TMSG_OPTIONS), 5, bufMenu, NULL, FALSE);
                    778:   if (ManualFeed == PP_ON)
                    779:     TtaSetToggleMenu (BasePrint + PrintOptions, 0, TRUE);
                    780:   else
                    781:     TtaSetToggleMenu (BasePrint + PrintOptions, 0, FALSE);
                    782:   TtaSetToggleMenu (BasePrint + PrintOptions, 1, WithToC);
                    783:   TtaSetToggleMenu (BasePrint + PrintOptions, 2, NumberLinks);
                    784:   TtaSetToggleMenu (BasePrint + PrintOptions, 3, PrintURL);
                    785:   TtaSetToggleMenu (BasePrint + PrintOptions, 4, IgnoreCSS);
                    786:   
                    787:   /* activates the Print form */
                    788:   TtaShowDialogue (BasePrint+FormPrint, FALSE);
                    789:   if (textFile)
                    790:     {
                    791:       /* invalid dialogue entries */
                    792:       TtaRedrawMenuEntry (BasePrint + PrintOptions, 1, NULL, -1, FALSE);
                    793:       TtaRedrawMenuEntry (BasePrint + PrintOptions, 2, NULL, -1, FALSE);
                    794:     }
1.70      cvs       795: #else  /* _WINDOWS */
1.81      cvs       796:   CreatePrintDlgWindow (TtaGetViewFrame (doc, view), PSfile);
1.70      cvs       797: #endif /* _WINDOWS */
1.1       cvs       798: }
                    799: 
                    800: /*----------------------------------------------------------------------
                    801:   UpdateURLsInSubtree
                    802:   Update NAMEs and URLs in subtree of el element, to take into account
                    803:   the move from one document to another.
                    804:   If a NAME attribute already exists in the new document, it is changed
                    805:   to avoid duplicate names.
                    806:   Transform the HREF and SRC attribute to make them independent from their
                    807:   former base.
                    808:   ----------------------------------------------------------------------*/
1.86      vatton    809: static void UpdateURLsInSubtree (NotifyElement *event, Element el)
1.1       cvs       810: {
1.77      cvs       811:   Element             nextEl, child;
                    812:   ElementType         elType;
                    813:   SSchema             HTMLschema;
1.1       cvs       814: 
                    815:   nextEl = TtaGetFirstChild (el);
1.82      cvs       816:   HTMLschema = TtaGetSSchema ("HTML", event->document);
1.77      cvs       817:   elType.ElSSchema = HTMLschema;
1.1       cvs       818:   while (nextEl != NULL)
                    819:     {
1.58      cvs       820:       event->element = nextEl;
                    821:       ElementPasted (event);
1.77      cvs       822: 
                    823:       /* manage included links and anchors */
                    824:       elType.ElTypeNum = HTML_EL_Anchor;
                    825:       child = TtaSearchTypedElement (elType, SearchInTree, nextEl);
                    826:       while (child)
                    827:        {
                    828:          event->element = child;
                    829:          ElementPasted (event);
                    830:          child = TtaSearchTypedElementInTree (elType, SearchForward, nextEl, child);
                    831:        }
                    832: 
                    833:       /* manage included links and anchors */
                    834:       elType.ElTypeNum = HTML_EL_PICTURE_UNIT;
                    835:       child = TtaSearchTypedElement (elType, SearchInTree, nextEl);
                    836:       while (child)
                    837:        {
                    838:          event->element = child;
                    839:          ElementPasted (event);
                    840:          child = TtaSearchTypedElementInTree (elType, SearchForward, nextEl, child);
                    841:        }
1.1       cvs       842:       TtaNextSibling (&nextEl);
                    843:     }
                    844: }
                    845: 
                    846: 
                    847: /*----------------------------------------------------------------------
                    848:   MoveDocumentBody
1.58      cvs       849:   Copy the elements contained in the BODY of the document sourceDoc at the
                    850:   position of the element el in the document destDoc.
                    851:   Delete the element containing el and all its empty ancestors.
1.1       cvs       852:   If deleteTree is TRUE, copied elements are deleted from the source
                    853:   document.
1.58      cvs       854:   Return the root element that delimits the new inserted part (a div).
1.1       cvs       855:   ----------------------------------------------------------------------*/
1.80      cvs       856: static Element MoveDocumentBody (Element el, Document destDoc,
1.85      cvs       857:                                 Document sourceDoc, char *target,
                    858:                                 char *url, ThotBool deleteTree)
1.1       cvs       859: {
1.58      cvs       860:   Element         root, ancestor, elem, firstInserted, div;
1.12      cvs       861:   Element          lastInserted, srce, copy, old, parent, sibling;
                    862:   ElementType     elType;
                    863:   NotifyElement    event;
                    864:   int             checkingMode;
1.47      cvs       865:   ThotBool         isID;
1.1       cvs       866: 
1.67      cvs       867:   div = NULL;
1.13      cvs       868:   if (target != NULL)
                    869:     {
                    870:       /* locate the target element within the source document */
                    871:       root = SearchNAMEattribute (sourceDoc, target, NULL);
                    872:       elType = TtaGetElementType (root);
1.84      cvs       873:       isID = (elType.ElTypeNum != HTML_EL_Anchor &&
                    874:              elType.ElTypeNum != HTML_EL_MAP);
1.13      cvs       875:     }
                    876:   else
                    877:     {
                    878:       isID = FALSE;
                    879:       /* get the BODY element of source document */
                    880:       root = TtaGetMainRoot (sourceDoc);
                    881:       elType = TtaGetElementType (root);
                    882:       elType.ElTypeNum = HTML_EL_BODY;
                    883:       root = TtaSearchTypedElement (elType, SearchForward, root);
                    884:     }
                    885: 
                    886:   if (root != NULL)
1.12      cvs       887:     {
                    888:       /* don't check the abstract tree against the structure schema */
                    889:       checkingMode = TtaGetStructureChecking (destDoc);
                    890:       TtaSetStructureChecking (0, destDoc);
1.58      cvs       891:       /* get elem, the ancestor of el which is a child of a DIV or BODY
1.12      cvs       892:         element in the destination document. The copied elements will be
                    893:         inserted just before this element. */
1.58      cvs       894:       elem = el;
1.12      cvs       895:       do
1.1       cvs       896:        {
1.12      cvs       897:          ancestor = TtaGetParent (elem);
                    898:          if (ancestor != NULL)
                    899:            {
                    900:              elType = TtaGetElementType (ancestor);
                    901:              if (elType.ElTypeNum == HTML_EL_BODY ||
                    902:                  elType.ElTypeNum == HTML_EL_Division)
                    903:                ancestor = NULL;
                    904:              else
                    905:                elem = ancestor;
                    906:            }
1.1       cvs       907:        }
1.12      cvs       908:       while (ancestor != NULL);
                    909:       parent = TtaGetParent (elem);
1.14      cvs       910: 
                    911:       /* insert a DIV element */
1.15      cvs       912:       elType.ElTypeNum = HTML_EL_Division;
1.16      cvs       913:       lastInserted = TtaNewElement (destDoc, elType);
                    914:       TtaInsertSibling (lastInserted, elem, TRUE, destDoc);
1.58      cvs       915:       /* this delimits the new inserted part of the document */
1.16      cvs       916:       RegisterSubDoc (lastInserted, url);
1.76      kahan     917:       CreateTargetAnchor (destDoc, lastInserted, FALSE, FALSE);
1.58      cvs       918:       div = lastInserted;
1.14      cvs       919: 
1.12      cvs       920:       /* do copy */
1.16      cvs       921:       firstInserted = NULL;
1.17      cvs       922:       if (isID)
                    923:        srce = root;
                    924:       else
                    925:        srce = TtaGetFirstChild (root);
1.12      cvs       926:       while (srce != NULL)
1.1       cvs       927:        {
1.12      cvs       928:          copy = TtaCopyTree (srce, sourceDoc, destDoc, parent);
                    929:          if (copy != NULL)
                    930:            {
1.16      cvs       931:              if (firstInserted == NULL)
1.12      cvs       932:                /* this is the first copied element. Insert it before elem */
                    933:                {
1.16      cvs       934:                  TtaInsertFirstChild (&copy, lastInserted, destDoc);
1.12      cvs       935:                  firstInserted = copy;
                    936:                }
                    937:              else
                    938:                /* insert the new copied element after the element previously
                    939:                   copied */
                    940:                TtaInsertSibling (copy, lastInserted, FALSE, destDoc);
                    941:              lastInserted = copy;
                    942:              /* update the NAMEs and URLs in the copied element */
                    943:              event.document = destDoc;
                    944:              event.position = sourceDoc;
                    945:              UpdateURLsInSubtree(&event, copy);
                    946:            }
                    947:          /* get the next element in the source document */
                    948:          old = srce;
                    949:          TtaNextSibling (&srce);
                    950:          if (deleteTree)
                    951:            TtaDeleteTree (old, sourceDoc);
1.13      cvs       952:          /* Stop here if the target points to a specific element with an ID */
                    953:          if (isID)
                    954:            srce = NULL;
1.1       cvs       955:        }
1.12      cvs       956:       
                    957:       /* delete the element(s) containing the link to the copied document */
1.58      cvs       958:       /* delete the parent element of el and all empty ancestors */
                    959:       elem = TtaGetParent (el);
1.12      cvs       960:       do
1.1       cvs       961:        {
1.12      cvs       962:          sibling = elem;
                    963:          TtaNextSibling (&sibling);
                    964:          if (sibling == NULL)
                    965:            {
                    966:              sibling = elem;
                    967:              TtaPreviousSibling (&sibling);
                    968:              if (sibling == NULL)
                    969:                elem = TtaGetParent (elem);
                    970:            }
1.1       cvs       971:        }
1.12      cvs       972:       while (sibling == NULL);
                    973:       TtaDeleteTree (elem, destDoc);
                    974:       /* restore previous chacking mode */
1.47      cvs       975:       TtaSetStructureChecking ((ThotBool)checkingMode, destDoc);
1.12      cvs       976:     }
1.58      cvs       977:   /* return the address of the new division */
                    978:   return (div);
1.1       cvs       979: }
                    980: 
1.29      cvs       981: 
1.58      cvs       982: /*----------------------------------------------------------------------
                    983:   CloseMakeBook
                    984:   ----------------------------------------------------------------------*/
                    985: static void CloseMakeBook (Document document)
                    986: {
                    987:   ResetStop (document);
                    988:   /* update internal links */
                    989:   SetInternalLinks (document);
                    990:   /* if the document changed force the browser mode */
                    991:   if (SubDocs)
                    992:     SetBrowserEditor (document);
                    993:   /* remove registered  sub-documents */
                    994:   FreeSubDocTable ();
                    995:   DocBook = 0;
                    996:   TtaSetStatus (document, 1, TtaGetMessage (AMAYA, AM_DOCUMENT_LOADED), NULL);
                    997: }
                    998: 
                    999: 
                   1000: /*----------------------------------------------------------------------
                   1001:   GetIncludedDocuments_callback finishes the GetIncludedDocuments procedure
                   1002:   ----------------------------------------------------------------------*/
                   1003: void   GetIncludedDocuments_callback (int newdoc, int status, 
1.85      cvs      1004:                                      char *urlName,
                   1005:                                      char *outputfile, 
1.63      cvs      1006:                                      AHTHeaders *http_headers,
1.58      cvs      1007:                                      void * context)
1.29      cvs      1008: {
1.58      cvs      1009:   Element              link, div;
                   1010:   IncludeCtxt          *ctx, *prev;
1.85      cvs      1011:   char                *url, *ptr;
1.58      cvs      1012:   ThotBool              found = FALSE;
1.29      cvs      1013: 
                   1014:   /* restore GetIncludedDocuments's context */
1.58      cvs      1015:   ctx = (IncludeCtxt *) context;  
1.29      cvs      1016:   if (!ctx)
                   1017:     return;
                   1018: 
1.58      cvs      1019:   div = NULL;
1.29      cvs      1020:   link = ctx->link;
1.58      cvs      1021:   ptr = ctx->name;
1.29      cvs      1022:   url = ctx->url;
1.58      cvs      1023:   if (url)
1.29      cvs      1024:     {
1.58      cvs      1025:       if (newdoc && newdoc != DocBook)
1.29      cvs      1026:        {
1.58      cvs      1027:          /* it's not the DocBook itself */
                   1028:          /* copy the target document at the position of the link */
                   1029:          TtaSetDocumentModified (DocBook);
                   1030:          div = MoveDocumentBody (link, DocBook, newdoc, ptr, url,
                   1031:                                  (ThotBool)(newdoc == IncludedDocument));
1.29      cvs      1032:        }
1.58      cvs      1033:       /* global variables */
                   1034:       FreeDocumentResource (IncludedDocument);
                   1035:       TtaCloseDocument (IncludedDocument);
                   1036:       IncludedDocument = 0;
1.29      cvs      1037:     }
1.58      cvs      1038: 
                   1039:   if (div != NULL)
1.29      cvs      1040:     {
1.58      cvs      1041:       /* new starting point for the search */
                   1042:       ctx->link = div;
                   1043:       found = GetIncludedDocuments (div, div, DocBook, ctx);
1.29      cvs      1044:     }
1.58      cvs      1045:   while (!found && ctx)
                   1046:     {
                   1047:       /* this sub-document has no more inclusion, examine the caller */
                   1048:       div = ctx->div;
                   1049:       link = ctx->link;
                   1050:       prev = ctx->ctxt;
                   1051:       TtaFreeMemory (url);
1.64      cvs      1052:          url = NULL;
1.58      cvs      1053:       TtaFreeMemory (ctx);
                   1054:       ctx = prev;
                   1055:       found = GetIncludedDocuments (div, link, DocBook, ctx);
                   1056:     }
                   1057:   if (!found)
                   1058:     /* all links are now managed */
                   1059:     CloseMakeBook (DocBook);
1.29      cvs      1060: }
                   1061: 
1.1       cvs      1062: /*----------------------------------------------------------------------
                   1063:   GetIncludedDocuments
1.58      cvs      1064:   Look forward within the element el, starting from element link, for a 
                   1065:   link (A) with attribute rel="chapter" or rel="subdocument" and replace
                   1066:   that link by the contents of the target document.
                   1067:   Return TRUE if one inclusion is launched.
1.1       cvs      1068:   ----------------------------------------------------------------------*/
1.80      cvs      1069: static ThotBool  GetIncludedDocuments (Element el, Element link,
                   1070:                                       Document document, IncludeCtxt *prev)
1.1       cvs      1071: {
1.58      cvs      1072:   ElementType           elType;
                   1073:   Attribute            attr;
                   1074:   AttributeType                attrType;
                   1075:   Document             newdoc;
                   1076:   IncludeCtxt          *ctx = NULL;
1.85      cvs      1077:   char                *text, *ptr, *url = NULL;
1.29      cvs      1078:   int                  length;
1.58      cvs      1079:   ThotBool              found = FALSE;
1.29      cvs      1080: 
1.58      cvs      1081:   /* look for anchors with the attribute rel within the element  el */
                   1082:   attr = NULL;
1.82      cvs      1083:   attrType.AttrSSchema = TtaGetSSchema ("HTML", document);
1.58      cvs      1084:   elType.ElSSchema = attrType.AttrSSchema;
                   1085:   elType.ElTypeNum = HTML_EL_Anchor;
1.29      cvs      1086: 
1.58      cvs      1087:   /* Get only one included file each time */
                   1088:   while (link && attr == NULL)
1.29      cvs      1089:     {
1.58      cvs      1090:       link = TtaSearchTypedElementInTree (elType, SearchForward, el, link);
                   1091:       if (link)
1.29      cvs      1092:        {
1.58      cvs      1093:          attrType.AttrTypeNum = HTML_ATTR_REL;
                   1094:          attr = TtaGetAttribute (link, attrType);
                   1095:        }
                   1096:       if (attr)
                   1097:        {
                   1098:          length = TtaGetTextAttributeLength (attr);
1.83      cvs      1099:          text = TtaGetMemory (length + 1);
1.58      cvs      1100:          TtaGiveTextAttributeValue (attr, text, &length);
                   1101:          /* Valid rel values are rel="chapter" or rel="subdocument" */
1.83      cvs      1102:          if (strcasecmp (text, "chapter") &&
                   1103:              strcasecmp (text, "subdocument"))
1.58      cvs      1104:            attr = NULL;
1.29      cvs      1105:          TtaFreeMemory (text);
                   1106:        }
1.58      cvs      1107:   
                   1108:       if (attr)
                   1109:        {
                   1110:          /* a link with attribute rel="Chapter" has been found */
                   1111:          attrType.AttrTypeNum = HTML_ATTR_HREF_;
                   1112:          attr = TtaGetAttribute (link, attrType);
                   1113:        }
                   1114:       if (attr)
                   1115:        /* this link has an attribute HREF */
                   1116:        {
                   1117:          length = TtaGetTextAttributeLength (attr);
1.83      cvs      1118:          text = TtaGetMemory (length + 1);
1.58      cvs      1119:          TtaGiveTextAttributeValue (attr, text, &length);
1.83      cvs      1120:          ptr = strrchr (text, '#');
1.58      cvs      1121:          url = text;
                   1122:          if (ptr != NULL)
                   1123:            {
                   1124:              if (ptr == text)
                   1125:                url = NULL;
                   1126:              /* link to a particular position within a remote document */
                   1127:              ptr[0] = EOS;
                   1128:              ptr = &ptr[1];
                   1129:            }
                   1130:                  
                   1131:          if (url != NULL)
                   1132:            /* this link designates an external document */
                   1133:            {
                   1134:              /* create a new document and loads the target document */
1.82      cvs      1135:              IncludedDocument = TtaNewDocument ("HTML", "tmp");
1.58      cvs      1136:              if (IncludedDocument != 0)
                   1137:                {
                   1138:                  TtaSetStatus (document, 1, TtaGetMessage (AMAYA, AM_FETCHING), url);
                   1139:                  ctx = TtaGetMemory (sizeof (IncludeCtxt));
                   1140:                  ctx->div =  el;
                   1141:                  ctx->link = link;
                   1142:                  ctx->url = url; /* the URL of the document */
                   1143:                  ctx->name = ptr;
                   1144:                  ctx->ctxt = prev; /* previous context */
                   1145:                  /* Get the reference of the calling document */
                   1146:                  SetStopButton (document);
                   1147:                  newdoc = GetHTMLDocument (url, NULL, IncludedDocument,
                   1148:                                            document, CE_MAKEBOOK, FALSE, 
                   1149:                                            (void *) GetIncludedDocuments_callback,
                   1150:                                            (void *) ctx);
                   1151:                  found = TRUE;
                   1152:                }
                   1153:            }
                   1154:          else
                   1155:            TtaFreeMemory (text);
                   1156:        }
1.29      cvs      1157:     }
1.58      cvs      1158:   return (found);
1.1       cvs      1159: }
                   1160: 
                   1161: 
                   1162: /*----------------------------------------------------------------------
                   1163:   MakeBook
                   1164:   Replace all links in a document which have an attribute REL="chapter"
1.5       cvs      1165:   or REL="subdocument" by the corresponding target document.
1.1       cvs      1166:   ----------------------------------------------------------------------*/
                   1167: void                MakeBook (Document document, View view)
                   1168: {
1.58      cvs      1169:   Element          root, body;
                   1170:   ElementType      elType;
1.1       cvs      1171: 
1.58      cvs      1172:   /* stops all current transfers on this document */
                   1173:   StopTransfer (document, 1);
                   1174:   /* simulate a transfert in the main document */
                   1175:   DocBook = document;
                   1176:   IncludedDocument = 0;
                   1177:   root = TtaGetMainRoot (document);
                   1178:   elType = TtaGetElementType (root);
                   1179:   elType.ElTypeNum = HTML_EL_BODY;
                   1180:   body = TtaSearchTypedElement (elType, SearchForward, root);
1.29      cvs      1181: 
1.58      cvs      1182:   if (body)
                   1183:     GetIncludedDocuments (body, body, document, NULL);
1.1       cvs      1184: }

Webmaster