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

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

Webmaster