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

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

Webmaster