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

1.1       cvs         1: /*
                      2:  *
1.129     vatton      3:  *  (c) COPYRIGHT INRIA and W3C, 1996-2005
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
1.135     vatton     16: #include "wx/wx.h"
1.107     carcone    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.116     gully      27: #include "appdialogue_wx.h"
                     28: #include "message_wx.h"
                     29: 
1.25      cvs        30: /* structure to register sub-documents in MakeBook function*/
                     31: typedef struct _SubDoc
1.135     vatton     32: {
                     33:   struct _SubDoc  *SDnext;
                     34:   Element          SDel;
                     35:   char            *SDname;
                     36: }SubDoc;
1.25      cvs        37: 
1.29      cvs        38: /* the structure used for the GetIncludedDocuments_callback function */
1.58      cvs        39: typedef struct _IncludeCtxt
1.53      cvs        40: {
1.58      cvs        41:   Element              div; /* enclosing element for the search */
                     42:   Element              link; /* current processed link */
1.104     vatton     43:   char                *utf8path; /* called url */
1.85      cvs        44:   char                 *name; /* the fragment name */
1.58      cvs        45:   struct _IncludeCtxt  *ctxt; /* the previous context */
                     46: } IncludeCtxt;
1.29      cvs        47: 
1.55      cvs        48: /* shared with windialogapi.c */
                     49: ThotBool         PrintURL;
                     50: ThotBool        NumberLinks;
                     51: ThotBool        WithToC;
                     52: ThotBool         IgnoreCSS;
                     53: 
1.139     vatton     54: static struct _SubDoc  *SubDocs = NULL;
1.85      cvs        55: static char             PSfile[MAX_PATH];
                     56: static char             PPrinter[MAX_PATH];
                     57: static char            *DocPrintURL;
1.139     vatton     58: static Document                      DocPrint;
1.2       cvs        59: static int              PaperPrint;
1.73      cvs        60: static int              ManualFeed = PP_OFF;
1.71      cvs        61: static int              Orientation;
1.2       cvs        62: static int              PageSize;
1.71      cvs        63: static int              PagePerSheet;
1.1       cvs        64: 
1.114     vatton     65: #include "EDITORactions_f.h"
1.1       cvs        66: #include "init_f.h"
                     67: #include "HTMLactions_f.h"
                     68: #include "HTMLbook_f.h"
                     69: #include "HTMLedit_f.h"
1.25      cvs        70: #include "HTMLhistory_f.h"
1.51      cvs        71: #include "UIcss_f.h"
1.1       cvs        72: 
1.107     carcone    73: #ifdef _WINGUI
1.37      cvs        74: #include "wininclude.h"
1.103     cvs        75: #endif /* _WINGUI */
1.31      cvs        76: 
1.107     carcone    77: #ifdef _WX
1.135     vatton     78: #include "wxdialogapi_f.h"
1.107     carcone    79: #endif /* _WX */
                     80: 
1.101     gully      81: static ThotBool GetIncludedDocuments (Element el, Element link,
1.135     vatton     82:                                       Document doc, IncludeCtxt *prev);
1.1       cvs        83: /*----------------------------------------------------------------------
1.94      vatton     84:   RedisplayDocument redisplays a view of a document
                     85:   ----------------------------------------------------------------------*/
                     86: void  RedisplayDocument(Document doc, View view)
                     87: {
1.95      vatton     88:   Element             el;
                     89:   int                 position;
                     90:   int                 distance;
                     91: 
                     92:   /* get the current position in the document */
                     93:   position = RelativePosition (doc, &distance);
1.94      vatton     94:   TtaSetDisplayMode (doc, NoComputedDisplay);
                     95:   TtaSetDisplayMode (doc, DisplayImmediately);
1.95      vatton     96:   /* show the document at the same position */
                     97:   el = ElementAtPosition (doc, position);
                     98:   TtaShowElement (doc, view, el, distance);
1.94      vatton     99: }
                    100: 
                    101: 
                    102: /*----------------------------------------------------------------------
1.16      cvs       103:   RegisterSubDoc adds a new entry in SubDoc table.
                    104:   ----------------------------------------------------------------------*/
1.94      vatton    105: static void RegisterSubDoc (Element el, char *url)
1.16      cvs       106: {
                    107:   struct _SubDoc  *entry, *last;
                    108: 
                    109:   if (url == NULL || url[0] == EOS)
                    110:     return;
                    111: 
1.101     gully     112:   entry = (struct _SubDoc *)TtaGetMemory (sizeof (struct _SubDoc));
1.16      cvs       113:   entry->SDnext = NULL;
                    114:   entry->SDel = el;
1.83      cvs       115:   entry->SDname = TtaStrdup (url);
1.16      cvs       116: 
                    117:   if (SubDocs == NULL)
                    118:     SubDocs = entry;
                    119:   else
                    120:     {
                    121:       last = SubDocs;
                    122:       while (last->SDnext != NULL)
1.135     vatton    123:         last = last->SDnext;
1.16      cvs       124:       last->SDnext = entry;
                    125:     }
                    126: }
                    127: 
                    128: 
                    129: /*----------------------------------------------------------------------
                    130:   SearchSubDoc searches whether a document name is registered or not
                    131:   within the SubDoc table.
                    132:   Return the DIV element that correspond to the sub-document or NULL.
                    133:   ----------------------------------------------------------------------*/
1.94      vatton    134: static Element SearchSubDoc (char *url)
1.16      cvs       135: {
                    136:   Element          el;
                    137:   struct _SubDoc  *entry;
1.47      cvs       138:   ThotBool         docFound;
1.16      cvs       139: 
                    140:   if (url == NULL || url[0] == EOS)
                    141:     return (NULL);
                    142: 
                    143:   entry = SubDocs;
                    144:   docFound = FALSE;
                    145:   el = NULL;
                    146:   while (!docFound && entry != NULL)
                    147:     {
1.83      cvs       148:       docFound = (strcmp (url, entry->SDname) == 0);
1.16      cvs       149:       if (!docFound)
1.135     vatton    150:         entry = entry->SDnext;
1.16      cvs       151:       else
1.135     vatton    152:         /* document found -> return the DIV element */
                    153:         el = entry->SDel;
1.16      cvs       154:     }
                    155:   return (el);
                    156: }
                    157: 
                    158: /*----------------------------------------------------------------------
                    159:   FreeSubDocTable frees all entries in SubDoc table.
                    160:   ----------------------------------------------------------------------*/
1.94      vatton    161: static void FreeSubDocTable ()
1.16      cvs       162: {
                    163:   struct _SubDoc  *entry, *last;
                    164: 
                    165:   entry = SubDocs;
                    166:   while (entry != NULL)
                    167:     {
                    168:       last = entry;
                    169:       entry = entry->SDnext;
                    170:       TtaFreeMemory (last->SDname);
                    171:       TtaFreeMemory (last);
                    172:     }
                    173:   SubDocs = NULL;
                    174: }
                    175: 
                    176: 
                    177: /*----------------------------------------------------------------------
1.1       cvs       178:   SetInternalLinks
1.8       cvs       179:   Associate an InternalLink attribute with all anchor (A) elements of the
                    180:   document which designate an element in the same document.
1.1       cvs       181:   InternalLink is a Thot reference attribute that links a source and a
                    182:   target anchor and that allows P schemas to display and print cross-references
                    183:   ----------------------------------------------------------------------*/
1.94      vatton    184: void SetInternalLinks (Document document)
1.1       cvs       185: {
1.32      cvs       186:   Element              el, div, link, target, sibling;
                    187:   ElementType          elType, linkType;
1.16      cvs       188:   Attribute            HrefAttr, IntLinkAttr;
1.17      cvs       189:   Attribute             attr, ExtLinkAttr;
1.16      cvs       190:   AttributeType                attrType;
1.85      cvs       191:   char                *text, *ptr, *url; 
1.83      cvs       192:   char                  number[10];
                    193:   char                  value[MAX_LENGTH];
1.25      cvs       194:   int                  length, i, volume;
                    195:   int                   status, position;
1.47      cvs       196:   ThotBool              split;
1.1       cvs       197: 
1.16      cvs       198:   /* Remember the current status of the document */
                    199:   status = TtaIsDocumentModified (document);
1.32      cvs       200:   el = TtaGetMainRoot (document);
                    201:   volume = TtaGetElementVolume (el);
                    202:   elType = TtaGetElementType (el);
                    203:   elType.ElTypeNum = HTML_EL_AnyLink;
1.16      cvs       204:   attrType.AttrSSchema = elType.ElSSchema;
1.32      cvs       205:   /* looks for all links in the document */
1.16      cvs       206:   link = el;
                    207:   while (link != NULL)
                    208:     {
1.25      cvs       209:       /* display the progression of the work */
                    210:       el = link;
                    211:       position = 0;
                    212:       while (el != NULL)
1.135     vatton    213:         {
                    214:           sibling = el;
                    215:           do
                    216:             {
                    217:               /* add volume of each previous element */
                    218:               TtaPreviousSibling (&sibling);
                    219:               if (sibling != NULL)
                    220:                 position += TtaGetElementVolume (sibling);
                    221:             }
                    222:           while (sibling != NULL);
                    223:           el = TtaGetParent (el);
                    224:         }
1.83      cvs       225:       sprintf (number, "%d", position*100/volume);
1.25      cvs       226:       TtaSetStatus (document, 1, TtaGetMessage (AMAYA, AM_UPDATED_LINK), number);
                    227:       TtaHandlePendingEvents ();
1.16      cvs       228:       link = TtaSearchTypedElement (elType, SearchForward, link);
                    229:       if (link != NULL)
1.135     vatton    230:         /* a link has been found */
                    231:         {
                    232:           linkType = TtaGetElementType (link);
                    233:           if (linkType.ElTypeNum == HTML_EL_Anchor)
                    234:             attrType.AttrTypeNum = HTML_ATTR_HREF_;
                    235:           else
                    236:             attrType.AttrTypeNum = HTML_ATTR_cite;
                    237:           HrefAttr = TtaGetAttribute (link, attrType);
                    238:           attrType.AttrTypeNum = HTML_ATTR_InternalLink;
                    239:           IntLinkAttr = TtaGetAttribute (link, attrType);
                    240:           attrType.AttrTypeNum = HTML_ATTR_ExternalLink;
                    241:           ExtLinkAttr = TtaGetAttribute (link, attrType);
                    242:           if (HrefAttr == NULL)
                    243:             /* this element is not a link (no href or cite attribute) */
                    244:             /* remove attributes InternalLink and ExternalLink if they
                    245:                are present */
                    246:             {
                    247:               if (IntLinkAttr != NULL)
                    248:                 TtaRemoveAttribute (link, IntLinkAttr, document);
                    249:               if (ExtLinkAttr != NULL)
                    250:                 TtaRemoveAttribute (link, ExtLinkAttr, document);         
                    251:             }
                    252:           else
                    253:             /* this element has an HREF or cite attribute */
                    254:             {
                    255:               length = TtaGetTextAttributeLength (HrefAttr);
                    256:               text = (char *)TtaGetMemory (length + 1);
                    257:               TtaGiveTextAttributeValue (HrefAttr, text, &length);
                    258: 
                    259:               /* does an external link become an internal link ? */
                    260:               if (document == DocBook && SubDocs != NULL)
                    261:                 {
                    262:                   ptr = strrchr (text, '#');
                    263:                   url = text;
                    264:                   split = FALSE;
                    265:                   if (ptr == text)
                    266:                     /* a local link */
                    267:                     url = NULL;
                    268:                   else if (ptr != NULL)
                    269:                     {
                    270:                       /* split url and name part */
                    271:                       ptr[0] = EOS;
                    272:                       split = TRUE;
                    273:                     }
                    274: 
                    275:                   /* Is it a sub-document */
                    276:                   div = SearchSubDoc (url);
                    277:                   if (split)
                    278:                     /* retore the mark */
                    279:                     ptr[0] = '#';
                    280: 
                    281:                   if (div == NULL)
                    282:                     {
                    283:                       /* it's not a sub-document */
                    284:                       if (url == NULL)
                    285:                         /* a local link */
                    286:                         ptr = &text[1];
                    287:                       else
                    288:                         /* still an externa; link */
                    289:                         ptr = NULL;
                    290:                     }
                    291:                   else
                    292:                     {
                    293:                       /* this link becomes internal */
                    294:                       if (ptr != NULL)
                    295:                         {
                    296:                           /* get the target name */
                    297:                           strcpy (value, ptr);
                    298:                           length = strlen (value);
                    299:                           /* check whether the name changed */
                    300:                           i = 0;
                    301:                           target = SearchNAMEattribute (document, &value[1], NULL, NULL);
                    302:                           while (target != NULL)
                    303:                             {
                    304:                               /* is it the right NAME */
                    305:                               if (TtaIsAncestor (target, div))
                    306:                                 target = NULL;
                    307:                               else
                    308:                                 {
                    309:                                   /* continue the search */
                    310:                                   i++;
                    311:                                   sprintf (&value[length], "%d", i);
                    312:                                   target = SearchNAMEattribute (document,
                    313:                                                                 &value[1], NULL, NULL);
                    314:                                 }
                    315:                             }
                    316:                         }
                    317:                       else
                    318:                         {
                    319:                           /* get the DIV name */
                    320:                           attrType.AttrTypeNum = HTML_ATTR_ID;
                    321:                           attr = TtaGetAttribute (div, attrType);
                    322:                           length = 200;
                    323:                           value[0] = '#';
                    324:                           TtaGiveTextAttributeValue (attr, &value[1], &length);
                    325:                         }
                    326:                       ptr = &value[1];
                    327:                       TtaSetAttributeText (HrefAttr, value, link, document);
                    328:                     }
                    329:                 }
                    330:               else if (text[0] == '#')
                    331:                 ptr = &text[1];
                    332:               else
                    333:                 ptr = NULL;
                    334: 
                    335:               if (ptr != NULL)
                    336:                 /* it's an internal link. Attach an attribute InternalLink */
                    337:                 /* to the link, if this attribute does not exist yet */
                    338:                 {
                    339:                   if (IntLinkAttr == NULL)
                    340:                     {
                    341:                       attrType.AttrTypeNum = HTML_ATTR_InternalLink;
                    342:                       IntLinkAttr = TtaNewAttribute (attrType);
                    343:                       TtaAttachAttribute (link, IntLinkAttr, document);
                    344:                     }
                    345:                   /* looks for the target element */
                    346:                   target = SearchNAMEattribute (document, ptr, NULL, NULL);
                    347:                   if (target != NULL)
                    348:                     /* set the Thot link */
                    349:                     TtaSetAttributeReference (IntLinkAttr, link, document,
                    350:                                               target);
                    351:                 }
                    352:               else
                    353:                 /* it's an external link */
                    354:                 {
                    355:                   /* Remove the InternalLink attribute if it is present */
                    356:                   if (IntLinkAttr != NULL)
                    357:                     TtaRemoveAttribute (link, IntLinkAttr, document);
                    358:                   /* create an ExternalLink attribute if there is none */
                    359:                   if (ExtLinkAttr == NULL)
                    360:                     {
                    361:                       attrType.AttrTypeNum = HTML_ATTR_ExternalLink;
                    362:                       ExtLinkAttr = TtaNewAttribute (attrType);
                    363:                       TtaAttachAttribute (link, ExtLinkAttr, document);
                    364:                     }
                    365:                 }
                    366:               TtaFreeMemory (text);
                    367:             }
                    368:         }
1.16      cvs       369:     }
                    370:   /* Reset document status */
                    371:   if (!status)
                    372:     TtaSetDocumentUnmodified (document);
1.3       cvs       373: }
                    374: 
                    375: /*----------------------------------------------------------------------
                    376:   CheckPrintingDocument reinitialize printing parameters as soon as
                    377:   the printing document changes.
                    378:   ----------------------------------------------------------------------*/
1.89      vatton    379: static void CheckPrintingDocument (Document document)
1.3       cvs       380: {
1.83      cvs       381:   char         docName[MAX_LENGTH];
                    382:   char        *ptr; 
                    383:   char         suffix[MAX_LENGTH];
1.81      cvs       384:   int            lg;
                    385: 
                    386:   if (DocPrint != document || DocPrintURL == NULL ||
1.83      cvs       387:       strcmp(DocPrintURL, DocumentURLs[document]))
1.81      cvs       388:     {
                    389:       /* initialize print parameters */
                    390:       TtaFreeMemory (DocPrintURL);
                    391:       DocPrint = document;
                    392:       DocPrintURL = TtaStrdup (DocumentURLs[document]);
                    393:       
                    394:       /* define the new default PS file */
                    395:       ptr = TtaGetEnvString ("APP_TMPDIR");
                    396:       if (ptr != NULL && TtaCheckDirectory (ptr))
1.135     vatton    397:         strcpy (PSfile, ptr);
1.81      cvs       398:       else
1.135     vatton    399:         strcpy (PSfile, TtaGetDefEnvString ("APP_TMPDIR"));
1.83      cvs       400:       lg = strlen (PSfile);
                    401:       if (PSfile[lg - 1] == DIR_SEP)
1.135     vatton    402:         PSfile[--lg] = EOS;
1.83      cvs       403:       strcpy (docName, TtaGetDocumentName (document));
1.90      vatton    404:       TtaExtractSuffix (docName, suffix);
1.83      cvs       405:       sprintf (&PSfile[lg], "%c%s.ps", DIR_SEP, docName);
1.81      cvs       406:       TtaSetPsFile (PSfile);
                    407:     }
1.3       cvs       408: }
                    409: 
                    410: /*----------------------------------------------------------------------
1.135     vatton    411:   PrintDocument prints the document using predefined parameters.
                    412:   ----------------------------------------------------------------------*/  
1.91      vatton    413: static void PrintDocument (Document doc, View view)
1.3       cvs       414: {
1.126     cvs       415: #if defined(_WINDOWS) && defined(_WX) && defined(IV)
1.116     gully     416:   /* On windows and with wxWidgets, disable printing for the moment */
                    417:   wxMessageDialog messagedialog( NULL,
1.135     vatton    418:                                  TtaConvMessageToWX("Not implemented yet"), 
                    419:                                  _T("Warning"),
                    420:                                  (long) wxOK | wxICON_EXCLAMATION | wxSTAY_ON_TOP);
1.116     gully     421:   messagedialog.ShowModal();
                    422: #else /* defined(_WINDOWS) && defined(_WX) */
1.34      cvs       423:   AttributeType      attrType;
1.84      cvs       424:   ElementType        elType;
1.34      cvs       425:   Attribute          attr;
1.84      cvs       426:   Element            el, docEl;
1.85      cvs       427:   char              *files, *dir;
1.84      cvs       428:   char               viewsToPrint[MAX_PATH];
1.47      cvs       429:   ThotBool           status, textFile;
1.3       cvs       430: 
1.38      cvs       431:   textFile = (DocumentTypes[doc] == docText ||
1.75      cvs       432:               DocumentTypes[doc] == docSource ||
1.135     vatton    433:               DocumentTypes[doc] == docCSS);
1.38      cvs       434: 
1.81      cvs       435:   /* initialize printing information */
1.75      cvs       436:   CheckPrintingDocument (doc);
1.83      cvs       437:   strcpy (viewsToPrint, "Formatted_view ");
1.75      cvs       438:   if (DocumentTypes[doc] == docHTML && WithToC)
1.83      cvs       439:     strcat (viewsToPrint, "Table_of_contents ");
1.75      cvs       440:   
                    441:   if (textFile)
                    442:     {
                    443:       if (PageSize == PP_A4)
1.135     vatton    444:         {
                    445:           if (Orientation == PP_Landscape)
                    446:             TtaSetPrintSchema ("TextFilePL");
                    447:           else
                    448:             TtaSetPrintSchema ("TextFilePP");
                    449:         }
1.75      cvs       450:       else
1.135     vatton    451:         {
                    452:           if (Orientation == PP_Landscape)
                    453:             TtaSetPrintSchema ("TextFileUSL");
                    454:           else
                    455:             TtaSetPrintSchema ("TextFilePPUS");
                    456:         }
1.75      cvs       457:     }
                    458:   else if (DocumentTypes[doc] == docSVG)
1.88      vatton    459:     TtaSetPrintSchema ("SVGP");
1.75      cvs       460:   else if (DocumentTypes[doc] == docMath)
1.82      cvs       461:     TtaSetPrintSchema ("MathMLP");
1.75      cvs       462:   else if (DocumentTypes[doc] == docAnnot)
1.82      cvs       463:     TtaSetPrintSchema ("AnnotP");
1.113     quint     464:   else if (DocumentTypes[doc] == docHTML)
1.75      cvs       465:     {
1.113     quint     466:       if (NumberLinks)
1.135     vatton    467:         /* display numbered links */
                    468:         {
                    469:           /* associate an attribute InternalLink with all anchors refering
                    470:              a target in the same document.  This allows P schemas to work
                    471:              properly */
                    472:           SetInternalLinks (DocPrint);
                    473:           if (PageSize == PP_A4)
                    474:             {
                    475:               if (Orientation == PP_Landscape)
                    476:                 TtaSetPrintSchema ("HTMLPLL");
                    477:               else
                    478:                 TtaSetPrintSchema ("HTMLPLP");
                    479:             }
                    480:           else
                    481:             {
                    482:               if (Orientation == PP_Landscape)
                    483:                 TtaSetPrintSchema ("HTMLUSLL");
                    484:               else
                    485:                 TtaSetPrintSchema ("HTMLPLPUS");
                    486:             }
                    487:           strcat (viewsToPrint, "Links_view ");
                    488:         }
1.113     quint     489:       else if (PageSize == PP_A4)
1.135     vatton    490:         {
                    491:           if (Orientation == PP_Landscape)
                    492:             TtaSetPrintSchema ("HTMLPL");
                    493:           else
                    494:             TtaSetPrintSchema ("HTMLPP");
                    495:         }
1.75      cvs       496:       else
1.135     vatton    497:         {
                    498:           if (Orientation == PP_Landscape)
                    499:             TtaSetPrintSchema ("HTMLUSL");
                    500:           else
                    501:             TtaSetPrintSchema ("HTMLPPUS");
                    502:         }    
1.75      cvs       503:     }
                    504:   status = TtaIsDocumentModified (doc);
1.71      cvs       505: 
1.75      cvs       506:   if (textFile || DocumentTypes[doc] == docImage ||
                    507:       DocumentTypes[doc] == docHTML)
                    508:     {
                    509:       /* post or remove the PrintURL attribute */
                    510:       attrType.AttrSSchema = TtaGetDocumentSSchema (doc);
1.84      cvs       511:       elType.ElSSchema = attrType.AttrSSchema;
1.75      cvs       512:       if (textFile)
1.135     vatton    513:         {
                    514:           elType. ElTypeNum = TextFile_EL_TextFile;
                    515:           attrType.AttrTypeNum = TextFile_ATTR_PrintURL;
                    516:         }
1.75      cvs       517:       else
1.135     vatton    518:         {
                    519:           elType. ElTypeNum = HTML_EL_HTML;
                    520:           attrType.AttrTypeNum = HTML_ATTR_PrintURL;
                    521:         }
1.84      cvs       522:       docEl = TtaGetMainRoot (doc);
                    523:       el = TtaSearchTypedElement (elType, SearchForward, docEl);
1.75      cvs       524:       attr = TtaGetAttribute (el, attrType);
                    525:       if (!attr && PrintURL)
1.135     vatton    526:         {
                    527:           attr = TtaNewAttribute (attrType);
                    528:           TtaAttachAttribute (el, attr, doc);
                    529:         }
1.75      cvs       530:       if (attr && !PrintURL)
1.135     vatton    531:         TtaRemoveAttribute (el, attr, doc);
1.75      cvs       532:     }
                    533:   
                    534:   /* get the path dir where css files have to be stored */
1.113     quint     535:   if ((DocumentTypes[doc] == docHTML || DocumentTypes[doc] == docSVG ||
                    536:        DocumentTypes[doc] == docXml) &&
1.78      cvs       537:       !IgnoreCSS)
1.75      cvs       538:     {
                    539:       TtaGetPrintNames (&files, &dir);
                    540:       /* store css files and get the list of names */
                    541:       files = CssToPrint (doc, dir);
                    542:     }
                    543:   else
                    544:     files = NULL;
                    545:   TtaPrint (DocPrint, viewsToPrint, files);
                    546:   if (files)
                    547:     TtaFreeMemory (files);
                    548:   if (!status)
                    549:     TtaSetDocumentUnmodified (doc);
1.116     gully     550: #endif /* defined(_WINDOWS) && defined(_WX) */
1.1       cvs       551: }
                    552: 
1.45      cvs       553: /*----------------------------------------------------------------------
1.135     vatton    554:   PrintAs prints the document using predefined parameters.
                    555:   ----------------------------------------------------------------------*/  
1.89      vatton    556: void PrintAs (Document doc, View view)
1.45      cvs       557: {
1.103     cvs       558: #ifdef _WINGUI
1.72      cvs       559:   DocPrint = doc;
                    560:   ReusePrinterDC ();
1.103     cvs       561: #else /* _WINGUI */
1.45      cvs       562:   PrintDocument (doc, view);
1.103     cvs       563: #endif /* _WINGUI */
1.45      cvs       564: }
1.1       cvs       565: 
                    566: /*----------------------------------------------------------------------
1.135     vatton    567:   CallbackImage handle return of Print form.                   
1.1       cvs       568:   ----------------------------------------------------------------------*/
1.85      cvs       569: void CallbackPrint (int ref, int typedata, char *data)
1.1       cvs       570: {
1.133     cvs       571:   long int            val;
1.1       cvs       572: 
1.133     cvs       573:   val = (long int) data;
1.53      cvs       574:   switch (ref - BasePrint)
1.1       cvs       575:     {
1.69      cvs       576:     case FormPrint:
                    577:       TtaDestroyDialogue (BasePrint + FormPrint);
1.1       cvs       578:       switch (val)
1.135     vatton    579:         {
                    580:         case 1:
                    581:           TtaSetPrintCommand (PPrinter);
                    582:           TtaSetPsFile (PSfile);
                    583:           /* update the environment variable */
                    584:           TtaSetEnvString ("THOTPRINT", PPrinter, TRUE);
                    585:           TtaSaveAppRegistry ();
                    586:           PrintDocument (DocPrint, 1);
                    587:           break;
                    588:         default:
                    589:           break;
                    590:         }
1.1       cvs       591:       break;
1.69      cvs       592:     case PrintOptions:
1.1       cvs       593:       switch (val)
1.135     vatton    594:         {
                    595:         case 0:
                    596:           /* Manual feed option */
                    597:           if (ManualFeed == PP_ON)
                    598:             ManualFeed = PP_OFF;
                    599:           else
                    600:             ManualFeed = PP_ON;
                    601:           TtaSetPrintParameter (PP_ManualFeed, ManualFeed);
                    602:           break;
                    603:         case 1:
                    604:           /* Toc option */
                    605:           WithToC = !WithToC;
                    606:           break;
                    607:         case 2:
                    608:           /* NumberLinks option */
                    609:           NumberLinks = !NumberLinks;
                    610:         case 3:
                    611:           /* URL option */
                    612:           PrintURL = !PrintURL;
                    613:           break;
                    614:         case 4:
                    615:           /* CSS option */
                    616:           IgnoreCSS = !IgnoreCSS;
                    617:           break;
                    618:         }
1.1       cvs       619:       break;
1.69      cvs       620:     case PaperFormat:
1.1       cvs       621:       /* page size submenu */
                    622:       switch (val)
1.135     vatton    623:         {
                    624:         case 0:
                    625:           PageSize = PP_A4;
                    626:           break;
                    627:         case 1:
                    628:           PageSize = PP_US;
                    629:           break;
                    630:         }
1.81      cvs       631:       TtaSetPrintParameter (PP_PaperSize, PageSize);
1.1       cvs       632:       break;
1.71      cvs       633:     case PaperOrientation:
                    634:       /* orientation submenu */
                    635:       Orientation = val;
1.81      cvs       636:       TtaSetPrintParameter (PP_Orientation, Orientation);
1.71      cvs       637:       break;
                    638:     case PPagesPerSheet:
                    639:       /* pages per sheet submenu */
                    640:       switch (val)
1.135     vatton    641:         {
                    642:         case 0:
                    643:           PagePerSheet = 1;
                    644:           break;
                    645:         case 1:
                    646:           PagePerSheet = 2;
                    647:           break;
                    648:         case 2:
                    649:           PagePerSheet = 4;
                    650:           break;
                    651:         }
1.81      cvs       652:       TtaSetPrintParameter (PP_PagesPerSheet, PagePerSheet);
1.71      cvs       653:       break;
1.69      cvs       654:     case PrintSupport:
1.1       cvs       655:       /* paper print/save PostScript submenu */
                    656:       switch (val)
1.135     vatton    657:         {
                    658:         case 0:
                    659:           if (PaperPrint == PP_PS)
                    660:             {
                    661:               PaperPrint = PP_PRINTER;
1.110     cvs       662: #ifdef _GTK
1.135     vatton    663:               TtaSetTextForm (BasePrint + PPrinterName, PPrinter);
1.110     cvs       664: #endif /* _GTK */
1.135     vatton    665:               TtaSetPrintParameter (PP_Destination, PaperPrint);
                    666:             }
                    667:           break;
                    668:         case 1:
                    669:           if (PaperPrint == PP_PRINTER)
                    670:             {
                    671:               PaperPrint = PP_PS;
1.110     cvs       672: #ifdef _GTK
1.135     vatton    673:               TtaSetTextForm (BasePrint + PPrinterName, PSfile);
1.110     cvs       674: #endif /* _GTK */
1.135     vatton    675:               TtaSetPrintParameter (PP_Destination, PaperPrint);
                    676:             }
                    677:           break;
                    678:         }
1.1       cvs       679:       break;
1.69      cvs       680:     case PPrinterName:
1.131     vatton    681:       if (data[0] != EOS)
1.135     vatton    682:         {
                    683:           if (PaperPrint == PP_PRINTER)
                    684:             /* text capture zone for the printer name */
                    685:             strncpy (PPrinter, data, MAX_PATH);
                    686:           else
                    687:             /* text capture zone for the name of the PostScript file */
                    688:             strncpy (PSfile, data, MAX_PATH);
                    689:         }
1.1       cvs       690:       break;
                    691:     }
                    692: }
                    693: 
                    694: /*----------------------------------------------------------------------
                    695:   ----------------------------------------------------------------------*/
1.94      vatton    696: void InitPrint (void)
1.1       cvs       697: {
1.83      cvs       698:   char* ptr;
1.1       cvs       699: 
1.135     vatton    700:   BasePrint = TtaSetCallback ((Proc)CallbackPrint, PRINT_MAX_REF);
                    701:   DocPrint = 0;
                    702:   DocPrintURL = NULL;
                    703: 
                    704:   /* read default printer variable */
                    705:   ptr = TtaGetEnvString ("THOTPRINT");
                    706:   if (ptr == NULL)
                    707:     strcpy (PPrinter, "");
                    708:   else
                    709:     strcpy (PPrinter, ptr);
                    710:   TtaSetPrintCommand (PPrinter);
                    711:   PaperPrint = PP_PRINTER;
                    712:   TtaSetPrintParameter (PP_Destination, PaperPrint);
                    713: 
                    714:   /* define the new default PrintSchema */
                    715:   NumberLinks = FALSE;
                    716:   WithToC = FALSE;
                    717:   IgnoreCSS = FALSE;
                    718:   PrintURL = TRUE;
                    719:   PageSize = TtaGetPrintParameter (PP_PaperSize);        
                    720:   TtaSetPrintSchema ("");
                    721:   /* no manual feed */
                    722:   ManualFeed = PP_OFF;
                    723:   TtaSetPrintParameter (PP_ManualFeed, ManualFeed);
                    724:   PagePerSheet = 1;
                    725:   TtaSetPrintParameter (PP_PagesPerSheet, PagePerSheet);
1.1       cvs       726: }
                    727: 
                    728: /*----------------------------------------------------------------------
1.3       cvs       729:   SetupAndPrint sets printing parameters and starts the printing process
1.1       cvs       730:   ----------------------------------------------------------------------*/
1.91      vatton    731: void SetupAndPrint (Document doc, View view)
1.1       cvs       732: {
1.107     carcone   733: #ifdef _GTK
1.83      cvs       734:   char           bufMenu[MAX_LENGTH];
1.91      vatton    735:   int            i;
1.107     carcone   736: #endif /* _GTK */
1.91      vatton    737:   ThotBool       textFile;
1.38      cvs       738: 
1.81      cvs       739:   textFile = (DocumentTypes[doc] == docText || DocumentTypes[doc] == docCSS);
                    740:   /* Print form */
                    741:   CheckPrintingDocument (doc);
1.27      cvs       742: 
1.107     carcone   743: #ifdef _GTK
1.81      cvs       744:   TtaNewSheet (BasePrint + FormPrint, TtaGetViewFrame (doc, view), 
1.135     vatton    745:                TtaGetMessage (LIB, TMSG_LIB_PRINT), 1,
                    746:                TtaGetMessage (LIB, TMSG_BUTTON_PRINT), FALSE, 3, 'L', D_CANCEL);
1.81      cvs       747: 
                    748:   /* Paper format submenu */
                    749:   i = 0;
                    750:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_A4));
1.83      cvs       751:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       752:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_US));
                    753:   TtaNewSubmenu (BasePrint + PaperFormat, BasePrint + FormPrint, 0,
1.135     vatton    754:                  TtaGetMessage (LIB, TMSG_PAPER_SIZE), 2, bufMenu, NULL, 0, TRUE);
1.81      cvs       755:   if (PageSize == PP_US)
                    756:     TtaSetMenuForm (BasePrint + PaperFormat, 1);
                    757:   else
                    758:     TtaSetMenuForm (BasePrint + PaperFormat, 0);
                    759:   
                    760:   /* Orientation submenu */
                    761:   i = 0;
                    762:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (AMAYA, AM_PORTRAIT));
1.83      cvs       763:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       764:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (AMAYA, AM_LANDSCAPE));
                    765:   TtaNewSubmenu (BasePrint + PaperOrientation, BasePrint + FormPrint, 0,
1.135     vatton    766:                  TtaGetMessage (AMAYA, AM_ORIENTATION), 2, bufMenu, NULL, 0, TRUE);
1.81      cvs       767:   if (Orientation == PP_Landscape)
                    768:     TtaSetMenuForm (BasePrint + PaperOrientation, 1);
                    769:   else
                    770:     TtaSetMenuForm (BasePrint + PaperOrientation, 0);
                    771:   /* Pages per sheet submenu */
                    772:   i = 0;
                    773:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_1_PAGE_SHEET));
1.83      cvs       774:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       775:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_2_PAGE_SHEET));
1.83      cvs       776:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       777:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_4_PAGE_SHEET));
                    778:   TtaNewSubmenu (BasePrint + PPagesPerSheet, BasePrint + FormPrint, 0,
1.135     vatton    779:                  TtaGetMessage (LIB, TMSG_REDUCTION), 3, bufMenu, NULL, 0, TRUE);
1.81      cvs       780:   if (PagePerSheet == 1)
                    781:     TtaSetMenuForm (BasePrint + PPagesPerSheet, 0);
                    782:   else if (PagePerSheet == 2)
                    783:     TtaSetMenuForm (BasePrint + PPagesPerSheet, 1);
                    784:   else
                    785:     TtaSetMenuForm (BasePrint + PPagesPerSheet, 2);
                    786:     
                    787:   /* Print to paper/ Print to file submenu */
                    788:   i = 0;
                    789:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_PRINTER));
1.83      cvs       790:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       791:   sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_PS_FILE));
                    792:   TtaNewSubmenu (BasePrint + PrintSupport, BasePrint + FormPrint, 0,
1.135     vatton    793:                  TtaGetMessage (LIB, TMSG_OUTPUT), 2, bufMenu, NULL, 0, TRUE);
1.81      cvs       794: 
                    795:   /* PaperPrint selector */
                    796:   TtaNewTextForm (BasePrint + PPrinterName, BasePrint + FormPrint, NULL, 30, 1, TRUE);
                    797:   if (PaperPrint == PP_PRINTER)
                    798:     {
                    799:       TtaSetMenuForm (BasePrint + PrintSupport, 0);
                    800:       TtaSetTextForm (BasePrint + PPrinterName, PPrinter);
                    801:     }
                    802:   else
                    803:     {
                    804:       TtaSetMenuForm (BasePrint + PrintSupport, 1);
                    805:       TtaSetTextForm (BasePrint + PPrinterName, PSfile);
                    806:     }
1.1       cvs       807: 
1.81      cvs       808:   /* The toggle */
                    809:   i = 0;
                    810:   sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (LIB, TMSG_MANUAL_FEED));
1.83      cvs       811:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       812:   sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_PRINT_TOC));
1.83      cvs       813:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       814:   sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_NUMBERED_LINKS));
1.83      cvs       815:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       816:   sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_PRINT_URL));
1.83      cvs       817:   i += strlen (&bufMenu[i]) + 1;
1.81      cvs       818:   sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_WITH_CSS));
                    819:   TtaNewToggleMenu (BasePrint + PrintOptions, BasePrint + FormPrint,
1.135     vatton    820:                     TtaGetMessage (LIB, TMSG_OPTIONS), 5, bufMenu, NULL, FALSE);
1.81      cvs       821:   if (ManualFeed == PP_ON)
                    822:     TtaSetToggleMenu (BasePrint + PrintOptions, 0, TRUE);
                    823:   else
                    824:     TtaSetToggleMenu (BasePrint + PrintOptions, 0, FALSE);
                    825:   TtaSetToggleMenu (BasePrint + PrintOptions, 1, WithToC);
                    826:   TtaSetToggleMenu (BasePrint + PrintOptions, 2, NumberLinks);
                    827:   TtaSetToggleMenu (BasePrint + PrintOptions, 3, PrintURL);
                    828:   TtaSetToggleMenu (BasePrint + PrintOptions, 4, IgnoreCSS);
                    829:   
                    830:   /* activates the Print form */
                    831:   TtaShowDialogue (BasePrint+FormPrint, FALSE);
                    832:   if (textFile)
                    833:     {
                    834:       /* invalid dialogue entries */
1.102     gully     835:       TtaRedrawMenuEntry (BasePrint + PrintOptions, 1, NULL, (ThotColor)-1, FALSE);
                    836:       TtaRedrawMenuEntry (BasePrint + PrintOptions, 2, NULL, (ThotColor)-1, FALSE);
1.81      cvs       837:     }
1.107     carcone   838: #endif /* _GTK */
                    839: #ifdef _WINGUI
1.81      cvs       840:   CreatePrintDlgWindow (TtaGetViewFrame (doc, view), PSfile);
1.103     cvs       841: #endif /* _WINGUI */
1.107     carcone   842: #ifdef _WX
1.108     gully     843:   {
1.125     gully     844:     int paper_format;
                    845:     int orientation;
                    846:     int disposition;
                    847:     int paper_print;
1.128     cvs       848:     ThotBool manual_feed;
                    849:     ThotBool with_toc;
                    850:     ThotBool with_links;
                    851:     ThotBool with_url;
                    852:     ThotBool ignore_css;
1.125     gully     853: 
                    854:     /* Paper format submenu */
                    855:     if (PageSize == PP_US)
                    856:       paper_format = 1;
                    857:     else
                    858:       paper_format = 0;
                    859: 
                    860:     /* Orientation submenu */
                    861:     if (Orientation == PP_Landscape)
                    862:       orientation = 1;
                    863:     else
                    864:       orientation = 0;
                    865: 
                    866:     /* Pages per sheet submenu */
                    867:     if (PagePerSheet == 1)
                    868:       disposition = 0;
                    869:     else if (PagePerSheet == 2)
                    870:       disposition = 1;
                    871:     else
                    872:       disposition = 2;
                    873: 
                    874:     /* PaperPrint selector */
                    875:     if (PaperPrint == PP_PRINTER)
                    876:       paper_print = 0;
                    877:     else
                    878:       paper_print = 1;
                    879:     
                    880:     /* The toggle */
                    881:     manual_feed = (ManualFeed == PP_ON);
                    882:     with_toc = WithToC;
                    883:     with_links = NumberLinks;
                    884:     with_url = PrintURL;
                    885:     ignore_css = IgnoreCSS;
                    886: 
                    887:     /* The dialog creation */
1.108     gully     888:     ThotBool created;
1.125     gully     889:     created = CreatePrintDlgWX (BasePrint + FormPrint, TtaGetViewFrame (doc, view),
1.135     vatton    890:                                 PPrinter,
                    891:                                 PSfile, 
                    892:                                 paper_format,
                    893:                                 orientation,
                    894:                                 disposition,
                    895:                                 paper_print,
                    896:                                 manual_feed,
                    897:                                 with_toc,
                    898:                                 with_links,
                    899:                                 with_url,
                    900:                                 ignore_css);
1.108     gully     901:     if (created)
                    902:       {
1.135     vatton    903:         TtaShowDialogue (BasePrint+FormPrint, FALSE);
1.108     gully     904:       }
                    905:   }
                    906:   
1.107     carcone   907: #endif /* _WX */
1.1       cvs       908: }
                    909: 
                    910: /*----------------------------------------------------------------------
                    911:   UpdateURLsInSubtree
                    912:   Update NAMEs and URLs in subtree of el element, to take into account
                    913:   the move from one document to another.
                    914:   If a NAME attribute already exists in the new document, it is changed
                    915:   to avoid duplicate names.
                    916:   Transform the HREF and SRC attribute to make them independent from their
                    917:   former base.
                    918:   ----------------------------------------------------------------------*/
1.99      cvs       919: void UpdateURLsInSubtree (NotifyElement *event, Element el)
1.1       cvs       920: {
1.77      cvs       921:   Element             nextEl, child;
                    922:   ElementType         elType;
                    923:   SSchema             HTMLschema;
1.1       cvs       924: 
                    925:   nextEl = TtaGetFirstChild (el);
1.82      cvs       926:   HTMLschema = TtaGetSSchema ("HTML", event->document);
1.99      cvs       927:   if (HTMLschema)
1.1       cvs       928:     {
1.141     vatton    929:       // first check the id of the enclosing division
                    930:       MakeUniqueName (el, event->document, TRUE, TRUE);
1.99      cvs       931:       elType.ElSSchema = HTMLschema;
1.136     vatton    932:       while (nextEl)
1.135     vatton    933:         {
1.136     vatton    934:           CheckPastedElement (nextEl, event->document, 0, event->position, TRUE);
1.135     vatton    935:           /* manage included links and anchors */
                    936:           elType.ElTypeNum = HTML_EL_Anchor;
                    937:           child = TtaSearchTypedElement (elType, SearchInTree, nextEl);
                    938:           while (child)
                    939:             {
1.136     vatton    940:               CheckPastedElement (child, event->document, 0, event->position, TRUE);
1.135     vatton    941:               child = TtaSearchTypedElementInTree (elType, SearchForward, nextEl, child);
                    942:             }
1.99      cvs       943:          
1.135     vatton    944:           /* manage included links and anchors */
                    945:           elType.ElTypeNum = HTML_EL_PICTURE_UNIT;
                    946:           child = TtaSearchTypedElement (elType, SearchInTree, nextEl);
                    947:           while (child)
                    948:             {
1.136     vatton    949:               CheckPastedElement (child, event->document, 0, event->position, TRUE);
1.135     vatton    950:               child = TtaSearchTypedElementInTree (elType, SearchForward, nextEl, child);
                    951:             }
                    952:           TtaNextSibling (&nextEl);
                    953:         }
1.1       cvs       954:     }
                    955: }
                    956: 
                    957: 
                    958: /*----------------------------------------------------------------------
                    959:   MoveDocumentBody
1.58      cvs       960:   Copy the elements contained in the BODY of the document sourceDoc at the
                    961:   position of the element el in the document destDoc.
                    962:   Delete the element containing el and all its empty ancestors.
1.1       cvs       963:   If deleteTree is TRUE, copied elements are deleted from the source
                    964:   document.
1.58      cvs       965:   Return the root element that delimits the new inserted part (a div).
1.1       cvs       966:   ----------------------------------------------------------------------*/
1.80      cvs       967: static Element MoveDocumentBody (Element el, Document destDoc,
1.135     vatton    968:                                  Document sourceDoc, char *target,
                    969:                                  char *url, ThotBool deleteTree)
1.1       cvs       970: {
1.140     vatton    971:   Element               root, ancestor, elem, firstInserted, div;
1.12      cvs       972:   Element          lastInserted, srce, copy, old, parent, sibling;
1.140     vatton    973:   ElementType       elType;
                    974:   SSchema          nature;
1.12      cvs       975:   NotifyElement    event;
1.143     vatton    976:   int              i, j;
                    977:   ThotBool         checkingMode, isID;
1.1       cvs       978: 
1.67      cvs       979:   div = NULL;
1.140     vatton    980:   if (target)
1.13      cvs       981:     {
                    982:       /* locate the target element within the source document */
1.98      vatton    983:       root = SearchNAMEattribute (sourceDoc, target, NULL, NULL);
1.13      cvs       984:       elType = TtaGetElementType (root);
1.84      cvs       985:       isID = (elType.ElTypeNum != HTML_EL_Anchor &&
1.135     vatton    986:               elType.ElTypeNum != HTML_EL_MAP);
1.13      cvs       987:     }
                    988:   else
                    989:     {
                    990:       isID = FALSE;
                    991:       /* get the BODY element of source document */
                    992:       root = TtaGetMainRoot (sourceDoc);
                    993:       elType = TtaGetElementType (root);
                    994:       elType.ElTypeNum = HTML_EL_BODY;
                    995:       root = TtaSearchTypedElement (elType, SearchForward, root);
                    996:     }
                    997: 
1.140     vatton    998:   if (root)
1.12      cvs       999:     {
                   1000:       /* don't check the abstract tree against the structure schema */
                   1001:       checkingMode = TtaGetStructureChecking (destDoc);
1.124     vatton   1002:       TtaSetStructureChecking (FALSE, destDoc);
1.58      cvs      1003:       /* get elem, the ancestor of el which is a child of a DIV or BODY
1.135     vatton   1004:          element in the destination document. The copied elements will be
                   1005:          inserted just before this element. */
1.58      cvs      1006:       elem = el;
1.12      cvs      1007:       do
1.135     vatton   1008:         {
                   1009:           ancestor = TtaGetParent (elem);
                   1010:           if (ancestor != NULL)
                   1011:             {
                   1012:               elType = TtaGetElementType (ancestor);
                   1013:               if (elType.ElTypeNum == HTML_EL_BODY ||
                   1014:                   elType.ElTypeNum == HTML_EL_Division)
                   1015:                 ancestor = NULL;
                   1016:               else
                   1017:                 elem = ancestor;
                   1018:             }
                   1019:         }
1.140     vatton   1020:       while (ancestor);
1.12      cvs      1021:       parent = TtaGetParent (elem);
1.14      cvs      1022: 
                   1023:       /* insert a DIV element */
1.15      cvs      1024:       elType.ElTypeNum = HTML_EL_Division;
1.16      cvs      1025:       lastInserted = TtaNewElement (destDoc, elType);
                   1026:       TtaInsertSibling (lastInserted, elem, TRUE, destDoc);
1.58      cvs      1027:       /* this delimits the new inserted part of the document */
1.16      cvs      1028:       RegisterSubDoc (lastInserted, url);
1.76      kahan    1029:       CreateTargetAnchor (destDoc, lastInserted, FALSE, FALSE);
1.58      cvs      1030:       div = lastInserted;
1.14      cvs      1031: 
1.140     vatton   1032:       // check if new natures are added
                   1033:       if (DocumentMeta[destDoc] && !DocumentMeta[destDoc]->compound)
                   1034:         {
                   1035:           nature = NULL;
                   1036:           TtaNextNature (sourceDoc, &nature);
                   1037:           if (nature)
                   1038:             DocumentMeta[destDoc]->compound = TRUE;
                   1039:         }
                   1040: 
1.12      cvs      1041:       /* do copy */
1.16      cvs      1042:       firstInserted = NULL;
1.17      cvs      1043:       if (isID)
1.135     vatton   1044:         srce = root;
1.17      cvs      1045:       else
1.135     vatton   1046:         srce = TtaGetFirstChild (root);
1.140     vatton   1047:       while (srce)
1.135     vatton   1048:         {
                   1049:           copy = TtaCopyTree (srce, sourceDoc, destDoc, parent);
                   1050:           if (copy != NULL)
                   1051:             {
                   1052:               if (firstInserted == NULL)
                   1053:                 /* this is the first copied element. Insert it before elem */
                   1054:                 {
                   1055:                   TtaInsertFirstChild (&copy, lastInserted, destDoc);
                   1056:                   firstInserted = copy;
                   1057:                 }
                   1058:               else
                   1059:                 /* insert the new copied element after the element previously
                   1060:                    copied */
                   1061:                 TtaInsertSibling (copy, lastInserted, FALSE, destDoc);
                   1062:               lastInserted = copy;
                   1063:               /* update the NAMEs and URLs in the copied element */
                   1064:               event.event = TteElemPaste;
                   1065:               event.document = destDoc;
                   1066:               event.element = copy;
                   1067:               event.elementType = TtaGetElementType (copy);
                   1068:               event.position = sourceDoc;
                   1069:               event.info = 0;
                   1070:               UpdateURLsInSubtree(&event, copy);
                   1071:             }
                   1072:           /* get the next element in the source document */
                   1073:           old = srce;
                   1074:           TtaNextSibling (&srce);
                   1075:           if (deleteTree)
1.143     vatton   1076:             {
                   1077:               // remove the current selection if it concerns the removed subtree
                   1078:               TtaGiveFirstSelectedElement (sourceDoc, &elem, &i, &j);
                   1079:               if (elem)
                   1080:                 {
                   1081:                   if (TtaIsAncestor (elem, old))
                   1082:                     TtaCancelSelection (sourceDoc);
                   1083:                   else
                   1084:                     {
                   1085:                       TtaGiveLastSelectedElement (sourceDoc, &elem, &i, &j);
                   1086:                       if (elem &&TtaIsAncestor (elem, old))
                   1087:                         TtaCancelSelection (sourceDoc);
                   1088:                     }
                   1089:                 }
                   1090:               TtaDeleteTree (old, sourceDoc);
                   1091:             }
1.135     vatton   1092:           /* Stop here if the target points to a specific element with an ID */
                   1093:           if (isID)
                   1094:             srce = NULL;
                   1095:         }
1.12      cvs      1096:       
                   1097:       /* delete the element(s) containing the link to the copied document */
1.58      cvs      1098:       /* delete the parent element of el and all empty ancestors */
                   1099:       elem = TtaGetParent (el);
1.12      cvs      1100:       do
1.135     vatton   1101:         {
                   1102:           sibling = elem;
                   1103:           TtaNextSibling (&sibling);
                   1104:           if (sibling == NULL)
                   1105:             {
                   1106:               sibling = elem;
                   1107:               TtaPreviousSibling (&sibling);
                   1108:               if (sibling == NULL)
                   1109:                 elem = TtaGetParent (elem);
                   1110:             }
                   1111:         }
1.12      cvs      1112:       while (sibling == NULL);
                   1113:       TtaDeleteTree (elem, destDoc);
                   1114:       /* restore previous chacking mode */
1.124     vatton   1115:       TtaSetStructureChecking (checkingMode, destDoc);
1.12      cvs      1116:     }
1.58      cvs      1117:   /* return the address of the new division */
                   1118:   return (div);
1.1       cvs      1119: }
                   1120: 
1.29      cvs      1121: 
1.58      cvs      1122: /*----------------------------------------------------------------------
                   1123:   CloseMakeBook
                   1124:   ----------------------------------------------------------------------*/
                   1125: static void CloseMakeBook (Document document)
                   1126: {
                   1127:   ResetStop (document);
                   1128:   /* update internal links */
                   1129:   SetInternalLinks (document);
                   1130:   /* if the document changed force the browser mode */
1.139     vatton   1131:   if (SubDocs)
1.128     cvs      1132:     /* send a warning to the user to avoid to save this document */
                   1133:     /* remove registered  sub-documents */
                   1134:     FreeSubDocTable ();
1.58      cvs      1135:   DocBook = 0;
                   1136:   TtaSetStatus (document, 1, TtaGetMessage (AMAYA, AM_DOCUMENT_LOADED), NULL);
                   1137: }
                   1138: 
                   1139: 
                   1140: /*----------------------------------------------------------------------
                   1141:   GetIncludedDocuments_callback finishes the GetIncludedDocuments procedure
                   1142:   ----------------------------------------------------------------------*/
                   1143: void   GetIncludedDocuments_callback (int newdoc, int status, 
1.135     vatton   1144:                                       char *urlName,
                   1145:                                       char *outputfile, 
                   1146:                                       AHTHeaders *http_headers,
                   1147:                                       void * context)
1.29      cvs      1148: {
1.58      cvs      1149:   Element              link, div;
                   1150:   IncludeCtxt          *ctx, *prev;
1.104     vatton   1151:   char                *utf8path, *ptr;
1.58      cvs      1152:   ThotBool              found = FALSE;
1.29      cvs      1153: 
                   1154:   /* restore GetIncludedDocuments's context */
1.58      cvs      1155:   ctx = (IncludeCtxt *) context;  
1.29      cvs      1156:   if (!ctx)
                   1157:     return;
                   1158: 
1.58      cvs      1159:   div = NULL;
1.29      cvs      1160:   link = ctx->link;
1.58      cvs      1161:   ptr = ctx->name;
1.104     vatton   1162:   utf8path = ctx->utf8path;
                   1163:   if (utf8path)
1.29      cvs      1164:     {
1.58      cvs      1165:       if (newdoc && newdoc != DocBook)
1.135     vatton   1166:         {
                   1167:           /* it's not the DocBook itself */
                   1168:           /* copy the target document at the position of the link */
                   1169:           TtaSetDocumentModified (DocBook);
                   1170:           div = MoveDocumentBody (link, DocBook, newdoc, ptr, utf8path,
                   1171:                                   (ThotBool)(newdoc == IncludedDocument));
                   1172:         }
1.58      cvs      1173:       /* global variables */
                   1174:       FreeDocumentResource (IncludedDocument);
                   1175:       TtaCloseDocument (IncludedDocument);
                   1176:       IncludedDocument = 0;
1.29      cvs      1177:     }
1.58      cvs      1178: 
1.104     vatton   1179:   if (div)
1.29      cvs      1180:     {
1.58      cvs      1181:       /* new starting point for the search */
                   1182:       ctx->link = div;
                   1183:       found = GetIncludedDocuments (div, div, DocBook, ctx);
1.29      cvs      1184:     }
1.104     vatton   1185: 
1.58      cvs      1186:   while (!found && ctx)
                   1187:     {
                   1188:       /* this sub-document has no more inclusion, examine the caller */
                   1189:       div = ctx->div;
                   1190:       link = ctx->link;
                   1191:       prev = ctx->ctxt;
1.104     vatton   1192:       TtaFreeMemory (utf8path);
                   1193:       utf8path = NULL;
1.58      cvs      1194:       TtaFreeMemory (ctx);
                   1195:       ctx = prev;
                   1196:       found = GetIncludedDocuments (div, link, DocBook, ctx);
                   1197:     }
                   1198:   if (!found)
                   1199:     /* all links are now managed */
                   1200:     CloseMakeBook (DocBook);
1.29      cvs      1201: }
                   1202: 
1.1       cvs      1203: /*----------------------------------------------------------------------
                   1204:   GetIncludedDocuments
1.58      cvs      1205:   Look forward within the element el, starting from element link, for a 
                   1206:   link (A) with attribute rel="chapter" or rel="subdocument" and replace
                   1207:   that link by the contents of the target document.
                   1208:   Return TRUE if one inclusion is launched.
1.1       cvs      1209:   ----------------------------------------------------------------------*/
1.96      vatton   1210: static ThotBool GetIncludedDocuments (Element el, Element link,
1.135     vatton   1211:                                       Document doc, IncludeCtxt *prev)
1.1       cvs      1212: {
1.58      cvs      1213:   ElementType           elType;
                   1214:   Attribute            attr;
                   1215:   AttributeType                attrType;
                   1216:   Document             newdoc;
                   1217:   IncludeCtxt          *ctx = NULL;
1.104     vatton   1218:   char                *utf8path, *ptr;
1.29      cvs      1219:   int                  length;
1.58      cvs      1220:   ThotBool              found = FALSE;
1.29      cvs      1221: 
1.58      cvs      1222:   /* look for anchors with the attribute rel within the element  el */
                   1223:   attr = NULL;
1.96      vatton   1224:   attrType.AttrSSchema = TtaGetSSchema ("HTML", doc);
1.58      cvs      1225:   elType.ElSSchema = attrType.AttrSSchema;
                   1226:   elType.ElTypeNum = HTML_EL_Anchor;
1.29      cvs      1227: 
1.58      cvs      1228:   /* Get only one included file each time */
                   1229:   while (link && attr == NULL)
1.29      cvs      1230:     {
1.58      cvs      1231:       link = TtaSearchTypedElementInTree (elType, SearchForward, el, link);
                   1232:       if (link)
1.135     vatton   1233:         {
                   1234:           attrType.AttrTypeNum = HTML_ATTR_REL;
                   1235:           attr = TtaGetAttribute (link, attrType);
                   1236:         }
1.58      cvs      1237:       if (attr)
1.135     vatton   1238:         {
                   1239:           length = TtaGetTextAttributeLength (attr);
                   1240:           utf8path = (char *)TtaGetMemory (length + 1);
                   1241:           TtaGiveTextAttributeValue (attr, utf8path, &length);
                   1242:           /* Valid rel values are rel="chapter" or rel="subdocument" */
                   1243:           if (strcasecmp (utf8path, "chapter") &&
                   1244:               strcasecmp (utf8path, "subdocument"))
                   1245:             attr = NULL;
                   1246:           TtaFreeMemory (utf8path);
                   1247:         }
1.58      cvs      1248:   
                   1249:       if (attr)
1.135     vatton   1250:         {
                   1251:           /* a link with attribute rel="Chapter" has been found */
                   1252:           attrType.AttrTypeNum = HTML_ATTR_HREF_;
                   1253:           attr = TtaGetAttribute (link, attrType);
                   1254:         }
1.58      cvs      1255:       if (attr)
1.135     vatton   1256:         /* this link has an attribute HREF */
                   1257:         {
                   1258:           length = TtaGetTextAttributeLength (attr);
                   1259:           utf8path = (char *)TtaGetMemory (length + 1);
                   1260:           TtaGiveTextAttributeValue (attr, utf8path, &length);
                   1261:           ptr = strrchr (utf8path, '#');
                   1262:           if (ptr)
                   1263:             {
                   1264:               /* link to a particular position within a document */
                   1265:               if (ptr == utf8path)
                   1266:                 {
                   1267:                   /* local link */
                   1268:                   TtaFreeMemory (utf8path);
                   1269:                   utf8path = NULL;
                   1270:                 }
                   1271:               else
                   1272:                 {
                   1273:                   ptr[0] = EOS;
                   1274:                   ptr = &ptr[1];
                   1275:                 }
                   1276:             }
1.58      cvs      1277:                  
1.135     vatton   1278:           if (utf8path)
                   1279:             /* this link designates an external document */
                   1280:             {
                   1281:               /* create a new document and loads the target document */
                   1282:               IncludedDocument = TtaNewDocument ("HTML", "tmp");
                   1283:               if (IncludedDocument != 0)
                   1284:                 {
                   1285:                   TtaSetStatus (doc, 1, TtaGetMessage (AMAYA, AM_FETCHING), utf8path);
                   1286:                   ctx = (IncludeCtxt *)TtaGetMemory (sizeof (IncludeCtxt));
                   1287:                   ctx->div =  el;
                   1288:                   ctx->link = link;
                   1289:                   ctx->utf8path = utf8path; /* the URL of the document */
                   1290:                   ctx->name = ptr;
                   1291:                   ctx->ctxt = prev; /* previous context */
                   1292:                   /* Get the reference of the calling document */
                   1293:                   SetStopButton (doc);
                   1294:                   newdoc = GetAmayaDoc (utf8path, NULL, IncludedDocument,
                   1295:                                         doc, CE_MAKEBOOK, FALSE, 
1.144   ! cvs      1296:                                         (void (*)(int, int, char*, char*, char*, const AHTHeaders*, void*)) GetIncludedDocuments_callback,
1.135     vatton   1297:                                         (void *) ctx);
                   1298:                   found = TRUE;
                   1299:                 }
                   1300:             }
                   1301:           else
                   1302:             TtaFreeMemory (utf8path);
                   1303:         }
1.29      cvs      1304:     }
1.58      cvs      1305:   return (found);
1.1       cvs      1306: }
                   1307: 
                   1308: 
                   1309: /*----------------------------------------------------------------------
                   1310:   MakeBook
                   1311:   Replace all links in a document which have an attribute REL="chapter"
1.5       cvs      1312:   or REL="subdocument" by the corresponding target document.
1.1       cvs      1313:   ----------------------------------------------------------------------*/
1.96      vatton   1314: void MakeBook (Document doc, View view)
1.1       cvs      1315: {
1.58      cvs      1316:   Element          root, body;
                   1317:   ElementType      elType;
1.1       cvs      1318: 
1.139     vatton   1319:   if (SubDocs)
                   1320:     // another make_book is wi=orking
                   1321:     return;
1.58      cvs      1322:   /* stops all current transfers on this document */
1.96      vatton   1323:   StopTransfer (doc, 1);
1.58      cvs      1324:   /* simulate a transfert in the main document */
1.96      vatton   1325:   DocBook = doc;
1.58      cvs      1326:   IncludedDocument = 0;
1.96      vatton   1327:   root = TtaGetMainRoot (doc);
1.58      cvs      1328:   elType = TtaGetElementType (root);
                   1329:   elType.ElTypeNum = HTML_EL_BODY;
                   1330:   body = TtaSearchTypedElement (elType, SearchForward, root);
                   1331:   if (body)
1.96      vatton   1332:     GetIncludedDocuments (body, body, doc, NULL);
1.1       cvs      1333: }
1.105     vatton   1334: 
                   1335: /*----------------------------------------------------------------------
                   1336:   ----------------------------------------------------------------------*/
                   1337: void ReadAsUTF_8 (Document doc, View view)
                   1338: {
                   1339:   ReparseAs (doc, view, FALSE, UTF_8);
                   1340: }
                   1341: 
                   1342: /*----------------------------------------------------------------------
                   1343:   ----------------------------------------------------------------------*/
                   1344: void ReadAsISO_8859_1 (Document doc, View view)
                   1345: {
                   1346:   ReparseAs (doc, view, FALSE, ISO_8859_1);
                   1347: }
                   1348: 
                   1349: /*----------------------------------------------------------------------
                   1350:   ----------------------------------------------------------------------*/
                   1351: void ReadAsISO_8859_2 (Document doc, View view)
                   1352: {
                   1353:   ReparseAs (doc, view, FALSE, ISO_8859_2);
                   1354: }
                   1355: 
                   1356: /*----------------------------------------------------------------------
                   1357:   ----------------------------------------------------------------------*/
                   1358: void ReadAsISO_8859_3 (Document doc, View view)
                   1359: {
                   1360:   ReparseAs (doc, view, FALSE, ISO_8859_3);
                   1361: }
                   1362: 
                   1363: /*----------------------------------------------------------------------
                   1364:   ----------------------------------------------------------------------*/
                   1365: void ReadAsISO_8859_4 (Document doc, View view)
                   1366: {
                   1367:   ReparseAs (doc, view, FALSE, ISO_8859_4);
                   1368: }
                   1369: 
                   1370: /*----------------------------------------------------------------------
                   1371:   ----------------------------------------------------------------------*/
                   1372: void ReadAsISO_8859_5 (Document doc, View view)
                   1373: {
                   1374:   ReparseAs (doc, view, FALSE, ISO_8859_5);
                   1375: }
                   1376: 
                   1377: /*----------------------------------------------------------------------
                   1378:   ----------------------------------------------------------------------*/
                   1379: void ReadAsISO_8859_6 (Document doc, View view)
                   1380: {
                   1381:   ReparseAs (doc, view, FALSE, ISO_8859_6);
                   1382: }
                   1383: 
                   1384: /*----------------------------------------------------------------------
                   1385:   ----------------------------------------------------------------------*/
                   1386: void ReadAsISO_8859_7 (Document doc, View view)
                   1387: {
                   1388:   ReparseAs (doc, view, FALSE, ISO_8859_7);
                   1389: }
                   1390: 
                   1391: /*----------------------------------------------------------------------
                   1392:   ----------------------------------------------------------------------*/
                   1393: void ReadAsISO_8859_8 (Document doc, View view)
                   1394: {
                   1395:   ReparseAs (doc, view, FALSE, ISO_8859_8);
                   1396: }
                   1397: 
                   1398: /*----------------------------------------------------------------------
                   1399:   ----------------------------------------------------------------------*/
                   1400: void ReadAsISO_8859_9 (Document doc, View view)
                   1401: {
                   1402:   ReparseAs (doc, view, FALSE, ISO_8859_9);
                   1403: }
                   1404: 
                   1405: /*----------------------------------------------------------------------
                   1406:   ----------------------------------------------------------------------*/
                   1407: void ReadAsISO_8859_15 (Document doc, View view)
                   1408: {
                   1409:   ReparseAs (doc, view, FALSE, ISO_8859_15);
                   1410: }
                   1411: 
                   1412: /*----------------------------------------------------------------------
                   1413:   ----------------------------------------------------------------------*/
                   1414: void ReadAsKOI8_R (Document doc, View view)
                   1415: {
                   1416:   ReparseAs (doc, view, FALSE, KOI8_R);
                   1417: }
                   1418: 
                   1419: /*----------------------------------------------------------------------
                   1420:   ----------------------------------------------------------------------*/
                   1421: void ReadAsWINDOWS_1250 (Document doc, View view)
                   1422: {
                   1423:   ReparseAs (doc, view, FALSE, WINDOWS_1250);
                   1424: }
                   1425: 
                   1426: /*----------------------------------------------------------------------
                   1427:   ----------------------------------------------------------------------*/
                   1428: void ReadAsWINDOWS_1251 (Document doc, View view)
                   1429: {
                   1430:   ReparseAs (doc, view, FALSE, WINDOWS_1251);
                   1431: }
                   1432: 
                   1433: /*----------------------------------------------------------------------
                   1434:   ----------------------------------------------------------------------*/
                   1435: void ReadAsWINDOWS_1252 (Document doc, View view)
                   1436: {
                   1437:   ReparseAs (doc, view, FALSE, WINDOWS_1252);
                   1438: }
                   1439: 
                   1440: /*----------------------------------------------------------------------
                   1441:   ----------------------------------------------------------------------*/
                   1442: void ReadAsWINDOWS_1253 (Document doc, View view)
                   1443: {
                   1444:   ReparseAs (doc, view, FALSE, WINDOWS_1253);
                   1445: }
                   1446: 
                   1447: /*----------------------------------------------------------------------
                   1448:   ----------------------------------------------------------------------*/
                   1449: void ReadAsWINDOWS_1254 (Document doc, View view)
                   1450: {
                   1451:   ReparseAs (doc, view, FALSE, WINDOWS_1254);
                   1452: }
                   1453: 
                   1454: /*----------------------------------------------------------------------
                   1455:   ----------------------------------------------------------------------*/
                   1456: void ReadAsWINDOWS_1255 (Document doc, View view)
                   1457: {
                   1458:   ReparseAs (doc, view, FALSE, WINDOWS_1255);
                   1459: }
                   1460: 
                   1461: /*----------------------------------------------------------------------
                   1462:   ----------------------------------------------------------------------*/
                   1463: void ReadAsWINDOWS_1256 (Document doc, View view)
                   1464: {
                   1465:   ReparseAs (doc, view, FALSE, WINDOWS_1256);
                   1466: }
                   1467: 
                   1468: /*----------------------------------------------------------------------
                   1469:   ----------------------------------------------------------------------*/
                   1470: void ReadAsWINDOWS_1257 (Document doc, View view)
                   1471: {
                   1472:   ReparseAs (doc, view, FALSE, WINDOWS_1257);
                   1473: }
                   1474: 
                   1475: /*----------------------------------------------------------------------
                   1476:   ----------------------------------------------------------------------*/
1.134     vatton   1477: void ReadAsGB_2312 (Document doc, View view)
                   1478: {
                   1479:   ReparseAs (doc, view, FALSE, GB_2312);
                   1480: }
                   1481: 
                   1482: /*----------------------------------------------------------------------
                   1483:   ----------------------------------------------------------------------*/
1.105     vatton   1484: void ReadAsISO_2022_JP (Document doc, View view)
                   1485: {
                   1486:   ReparseAs (doc, view, FALSE, ISO_2022_JP);
                   1487: }
                   1488: 
                   1489: /*----------------------------------------------------------------------
                   1490:   ----------------------------------------------------------------------*/
                   1491: void ReadAsEUC_JP (Document doc, View view)
                   1492: {
                   1493:   ReparseAs (doc, view, FALSE, EUC_JP);
                   1494: }
                   1495: 
                   1496: /*----------------------------------------------------------------------
                   1497:   ----------------------------------------------------------------------*/
                   1498: void ReadAsSHIFT_JIS (Document doc, View view)
                   1499: {
                   1500:   ReparseAs (doc, view, FALSE, SHIFT_JIS);
                   1501: }
1.114     vatton   1502: 
                   1503: /*----------------------------------------------------------------------
1.119     vatton   1504:   SectionNumbering generates numbers for all HTML Hi elements after
                   1505:   the current position.
                   1506:   ----------------------------------------------------------------------*/
                   1507: void SectionNumbering (Document doc, View view)
                   1508: {
1.120     vatton   1509:   Element             el, new_, child;
                   1510:   DisplayMode         dispMode;
                   1511:   SSchema             HTMLschema;
                   1512:   ElementType         elType, childType, searchedType1, searchedType2;
                   1513:   ElementType         searchedType3, searchedType4, searchedType5;
                   1514:   Language            lang;
                   1515:   char                s[MAX_LENGTH], n[20], *text;
                   1516:   int                 nH2, nH3, nH4, nH5, nH6, length, i;
1.123     vatton   1517:   ThotBool            closeUndo, change = FALSE;
1.120     vatton   1518: 
                   1519:   /* check if there is HTML Hi elements and if the current position is
1.135     vatton   1520:      within a HTML Body element */
1.120     vatton   1521:   dispMode = TtaGetDisplayMode (doc);
                   1522: 
                   1523:   /* check if there is any HTML element within the document */
                   1524:   HTMLschema = TtaGetSSchema ("HTML", doc);
                   1525:   if (HTMLschema == NULL)
                   1526:     /* no HTML element */
                   1527:     return;
                   1528: 
                   1529:   if (TtaPrepareUndo (doc))
1.135     vatton   1530:     closeUndo = FALSE;
1.120     vatton   1531:   else
                   1532:     {
                   1533:       closeUndo = TRUE;
                   1534:       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1535:     }
                   1536: 
                   1537:   el = TtaGetMainRoot (doc);
                   1538:   searchedType1.ElSSchema = HTMLschema;
                   1539:   searchedType1.ElTypeNum = HTML_EL_H2;
                   1540:   searchedType2.ElSSchema = HTMLschema;
                   1541:   searchedType2.ElTypeNum = HTML_EL_H3;
                   1542:   searchedType3.ElSSchema = HTMLschema;
                   1543:   searchedType3.ElTypeNum = HTML_EL_H4;
                   1544:   searchedType4.ElSSchema = HTMLschema;
                   1545:   searchedType4.ElTypeNum = HTML_EL_H5;
                   1546:   searchedType5.ElSSchema = HTMLschema;
                   1547:   searchedType5.ElTypeNum = HTML_EL_H6;
                   1548:   nH2 = nH3 = nH4 = nH5 = nH6 = 0;
                   1549:   while (el)
                   1550:     {
                   1551:       el = TtaSearchElementAmong5Types (searchedType1, searchedType2,
1.135     vatton   1552:                                         searchedType3, searchedType4,
                   1553:                                         searchedType5, SearchForward, el);
1.120     vatton   1554:       if (el)
1.135     vatton   1555:         {
                   1556:           /* generate the text number */
                   1557:           elType = TtaGetElementType (el);
                   1558:           s[0] = EOS;
                   1559:           switch (elType.ElTypeNum)
                   1560:             {
                   1561:             case HTML_EL_H2:
                   1562:               nH2++;
                   1563:               nH3 = nH4 = nH5 = nH6 = 0;
                   1564:               sprintf (s, "%d.", nH2);
                   1565:               break;
                   1566:             case HTML_EL_H3:
                   1567:               nH3++;
                   1568:               nH4 = nH5 = nH6 = 0;
                   1569:               if (nH2)
                   1570:                 sprintf (s, "%d.", nH2);
                   1571:               sprintf (n, "%d.", nH3);
                   1572:               strcat (s, n);
                   1573:               break;
                   1574:             case HTML_EL_H4:
                   1575:               nH4++;
                   1576:               nH5 = nH6 = 0;
                   1577:               if (nH2)
                   1578:                 sprintf (s, "%d.", nH2);
                   1579:               if (nH3)
                   1580:                 {
                   1581:                   sprintf (n, "%d.", nH3);
                   1582:                   strcat (s, n);
                   1583:                 }
                   1584:               sprintf (n, "%d.", nH4);
                   1585:               strcat (s, n);
                   1586:               break;
                   1587:             case HTML_EL_H5:
                   1588:               nH5++;
                   1589:               nH6 = 0;
                   1590:               if (nH2)
                   1591:                 sprintf (s, "%d.", nH2);
                   1592:               if (nH3)
                   1593:                 {
                   1594:                   sprintf (n, "%d.", nH3);
                   1595:                   strcat (s, n);
                   1596:                 }
                   1597:               if (nH4)
                   1598:                 {
                   1599:                   sprintf (n, "%d.", nH4);
                   1600:                   strcat (s, n);
                   1601:                 }
                   1602:               sprintf (n, "%d.", nH5);
                   1603:               strcat (s, n);
                   1604:               break;
                   1605:             case HTML_EL_H6:
                   1606:               nH6++;
                   1607:               if (nH2)
                   1608:                 sprintf (s, "%d.", nH2);
                   1609:               if (nH3)
                   1610:                 {
                   1611:                   sprintf (n, "%d.", nH3);
                   1612:                   strcat (s, n);
                   1613:                 }
                   1614:               if (nH4)
                   1615:                 {
                   1616:                   sprintf (n, "%d.", nH4);
                   1617:                   strcat (s, n);
                   1618:                 }
                   1619:               if (nH5)
                   1620:                 {
                   1621:                   sprintf (n, "%d.", nH5);
                   1622:                   strcat (s, n);
                   1623:                 }
                   1624:               sprintf (n, "%d.", nH6);
                   1625:               strcat (s, n);
                   1626:               break;
                   1627:             default: break;
                   1628:             }
                   1629:           strcat (s, " ");
                   1630: 
                   1631:           /* look for the first leaf child */
                   1632:           child = el;
                   1633:           if (child)
                   1634:             {
                   1635:               do
                   1636:                 {
                   1637:                   child = TtaGetFirstChild (child);
                   1638:                   childType = TtaGetElementType (child);
                   1639:                 }
                   1640:               while (!TtaIsLeaf (childType) && childType.ElSSchema == HTMLschema);
                   1641:             }
                   1642:           if (child && childType.ElSSchema == HTMLschema &&
                   1643:               childType.ElTypeNum == HTML_EL_TEXT_UNIT)
                   1644:             {
                   1645:               /* the first leaf child is a text unit */
                   1646:               new_ = NULL;
                   1647:               /* check the text contents */
                   1648:               length = TtaGetTextLength (child) + 1;
                   1649:               text = (char *)TtaGetMemory (length);
                   1650:               TtaGiveTextContent (child, (unsigned char *)text, &length, &lang);
                   1651:               /* remove the old number */
                   1652:               i = 0;
                   1653:               while (isdigit (text[i]) || text[i] == '.')
                   1654:                 i++;
1.138     vatton   1655:               // remove extra spaces
                   1656:               while (text[i] == SPACE)
                   1657:                 i++;
1.135     vatton   1658:               TtaRegisterElementReplace (child, doc);
                   1659:               TtaSetTextContent (child, (unsigned char *)s, Latin_Script, doc);
                   1660:               TtaAppendTextContent (child, (unsigned char *)&text[i], doc);
                   1661:               TtaFreeMemory (text);
                   1662:               change = TRUE;
                   1663:             }
                   1664:           else
                   1665:             {
                   1666:               /* add a new text element */
                   1667:               elType.ElTypeNum = HTML_EL_TEXT_UNIT;
                   1668:               new_ = TtaNewElement (doc, elType);
                   1669:               if (child)
                   1670:                 /* insert before */
                   1671:                 TtaInsertSibling (new_, child, TRUE, doc);
                   1672:               else
                   1673:                 TtaInsertFirstChild (&new_, el, doc);
                   1674:               TtaSetTextContent (new_, (unsigned char *)s, Latin_Script, doc);
                   1675:               TtaRegisterElementCreate (new_, doc);
                   1676:               change = TRUE;
                   1677:             }
                   1678:         }
1.120     vatton   1679:     }
                   1680: 
                   1681:   if (closeUndo)
                   1682:     TtaCloseUndoSequence (doc);
                   1683:   if (dispMode == DisplayImmediately)
                   1684:     TtaSetDisplayMode (doc, dispMode);
1.123     vatton   1685:   if (change)
                   1686:     TtaSetDocumentModified (doc);
1.119     vatton   1687: }
                   1688: 
                   1689: /*----------------------------------------------------------------------
1.114     vatton   1690:   MakeToC generates a Table of Contents at the current position.
                   1691:   Looks for all HTML Hi elements after the current position.
                   1692:   ----------------------------------------------------------------------*/
                   1693: void MakeToc (Document doc, View view)
                   1694: {
                   1695:   Element             el, new_, *list, parent, copy, srce, child, prev;
                   1696:   Element             toc, lH2, lH3, lH4, lH5, lH6, item;
                   1697:   ElementType         elType, searchedType1, searchedType2;
                   1698:   ElementType         searchedType3, searchedType4, searchedType5;
                   1699:   ElementType         ulType, copyType;
                   1700:   AttributeType       attrType;
                   1701:   Attribute           attr;
                   1702:   DisplayMode         dispMode;
                   1703:   char               *s, *id;
                   1704:   int                 firstChar, i;
1.142     vatton   1705:   ThotBool            closeUndo, retry = TRUE;;
1.114     vatton   1706: 
                   1707:   /* check if there is HTML Hi elements and if the current position is
1.135     vatton   1708:      within a HTML Body element */
1.114     vatton   1709:   dispMode = TtaGetDisplayMode (doc);
                   1710: 
                   1711:   /* get the insert point */
                   1712:   TtaGiveFirstSelectedElement (doc, &el, &firstChar, &i);
                   1713:   if (el == NULL || TtaIsReadOnly (el))
1.117     vatton   1714:     {
                   1715:       /* no selection */
                   1716:       TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_NO_INSERT_POINT);
1.135     vatton   1717:       return;
1.117     vatton   1718:     }
1.137     vatton   1719:   else if (TtaIsReadOnly (el))
                   1720:     {
                   1721:       /* read-only */
                   1722:       TtaDisplaySimpleMessage (CONFIRM, LIB, TMSG_EL_RO);
                   1723:       return;
                   1724:     }
                   1725: 
1.114     vatton   1726:   elType = TtaGetElementType (el);
                   1727:   s = TtaGetSSchemaName (elType.ElSSchema);
                   1728:   if (strcmp (s, "HTML"))
                   1729:     /* not within HTML element */
                   1730:     return;
                   1731:   if (!HTMLelementAllowed (doc))
                   1732:     /* the creation of an HTML element is not allowed here */
                   1733:     return;
                   1734: 
                   1735:   if (dispMode == DisplayImmediately)
                   1736:     TtaSetDisplayMode (doc, SuspendDisplay);
                   1737: 
                   1738:   if (TtaPrepareUndo (doc))
1.135     vatton   1739:     closeUndo = FALSE;
1.114     vatton   1740:   else
                   1741:     {
                   1742:       closeUndo = TRUE;
                   1743:       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1744:     }
                   1745: 
                   1746:   attrType.AttrSSchema = elType.ElSSchema;
                   1747:   ulType.ElSSchema = elType.ElSSchema;
1.115     vatton   1748:   ulType.ElTypeNum = HTML_EL_Unnumbered_List;
1.114     vatton   1749:   searchedType1.ElSSchema = elType.ElSSchema;
                   1750:   searchedType1.ElTypeNum = HTML_EL_H2;
                   1751:   searchedType2.ElSSchema = elType.ElSSchema;
                   1752:   searchedType2.ElTypeNum = HTML_EL_H3;
                   1753:   searchedType3.ElSSchema = elType.ElSSchema;
                   1754:   searchedType3.ElTypeNum = HTML_EL_H4;
                   1755:   searchedType4.ElSSchema = elType.ElSSchema;
                   1756:   searchedType4.ElTypeNum = HTML_EL_H5;
                   1757:   searchedType5.ElSSchema = elType.ElSSchema;
                   1758:   searchedType5.ElTypeNum = HTML_EL_H6;
1.115     vatton   1759:   toc = lH2 = lH3 = lH4 = lH5 = lH6 = prev = NULL;
1.114     vatton   1760:   list = NULL;
1.142     vatton   1761:   while (retry)
1.114     vatton   1762:     {
1.142     vatton   1763:       while (el)
1.135     vatton   1764:         {
1.142     vatton   1765:           el = TtaSearchElementAmong5Types (searchedType1, searchedType2,
                   1766:                                             searchedType3, searchedType4,
                   1767:                                             searchedType5, SearchForward, el);
                   1768:           if (el)
1.135     vatton   1769:             {
1.142     vatton   1770:               if (toc == NULL)
1.135     vatton   1771:                 {
1.142     vatton   1772:                   /* genetate the enclosing division */
                   1773:                   elType.ElTypeNum = HTML_EL_Division;
                   1774:                   TtaCreateElement (elType, doc);
                   1775:                   TtaGiveFirstSelectedElement (doc, &child, &firstChar, &i);
                   1776:                   toc = TtaGetTypedAncestor (child, elType);
                   1777:                   TtaRegisterElementDelete (child, doc);
                   1778:                   TtaRemoveTree (child, doc);
                   1779:                   if (toc)
                   1780:                     {
                   1781:                       /* it's the last created element */
                   1782:                       attrType.AttrTypeNum = HTML_ATTR_Class;
                   1783:                       attr = TtaNewAttribute (attrType);
                   1784:                       TtaAttachAttribute (toc, attr, doc);
                   1785:                       TtaSetAttributeText (attr, "toc", toc, doc);
                   1786:                       TtaRegisterAttributeCreate (attr, toc, doc);
                   1787:                     }
1.135     vatton   1788:                 }
1.142     vatton   1789:               if (toc == NULL)
                   1790:                 el = NULL;
                   1791:               else
1.135     vatton   1792:                 {
1.142     vatton   1793:                   /* does the element have an ID attribute already? */
                   1794:                   attrType.AttrTypeNum = HTML_ATTR_ID;
1.135     vatton   1795:                   attr = TtaGetAttribute (el, attrType);
1.142     vatton   1796:                   if (!attr)
                   1797:                     {
                   1798:                       /* generate the ID if it does't exist */
                   1799:                       CreateTargetAnchor (doc, el, TRUE, TRUE);
                   1800:                       attr = TtaGetAttribute (el, attrType);
                   1801:                     }
                   1802:                   i = TtaGetTextAttributeLength (attr) + 1;
                   1803:                   id = (char *)TtaGetMemory (i + 1);
                   1804:                   id[0] = '#';
                   1805:                   TtaGiveTextAttributeValue (attr, &id[1], &i);
1.114     vatton   1806:              
1.142     vatton   1807:                   /* locate or generate the list */
                   1808:                   elType = TtaGetElementType (el);
                   1809:                   if (elType.ElTypeNum == HTML_EL_H2)
                   1810:                     {
                   1811:                       parent = toc;
                   1812:                       lH3 = lH4 = lH5 = lH6 = NULL;
                   1813:                       list = &lH2;
                   1814:                     }
                   1815:                   else if (elType.ElTypeNum == HTML_EL_H3)
                   1816:                     {
                   1817:                       if (lH2)
                   1818:                         parent = TtaGetLastChild (lH2);
                   1819:                       else
                   1820:                         parent = toc;
                   1821:                       lH4 = lH5 = lH6 = NULL;
                   1822:                       list = &lH3;
                   1823:                     }
                   1824:                   else if (elType.ElTypeNum == HTML_EL_H4)
                   1825:                     {
                   1826:                       if (lH3)
                   1827:                         parent =  TtaGetLastChild (lH3);
                   1828:                       else if (lH2)
                   1829:                         parent =  TtaGetLastChild (lH2);
                   1830:                       else
                   1831:                         parent = toc;
                   1832:                       lH5 = lH6 = NULL;
                   1833:                       list = &lH4;
                   1834:                     }
                   1835:                   else if (elType.ElTypeNum == HTML_EL_H5)
                   1836:                     {
                   1837:                       if (lH4)
                   1838:                         parent =  TtaGetLastChild (lH4);
                   1839:                       else if (lH3)
                   1840:                         parent =  TtaGetLastChild (lH3);
                   1841:                       else if (lH2)
                   1842:                         parent =  TtaGetLastChild (lH2);
                   1843:                       else
                   1844:                         parent = toc;
                   1845:                       lH6 = NULL;
                   1846:                       list = &lH5;
                   1847:                     }
                   1848:                   else if (elType.ElTypeNum == HTML_EL_H6)
                   1849:                     {
                   1850:                       if (lH5)
                   1851:                         parent =  TtaGetLastChild (lH5);
                   1852:                       else if (lH4)
                   1853:                         parent =  TtaGetLastChild (lH4);
                   1854:                       else if (lH3)
                   1855:                         parent =  TtaGetLastChild (lH3);
                   1856:                       else if (lH2)
                   1857:                         parent =  TtaGetLastChild (lH2);
                   1858:                       else
                   1859:                         parent = toc;
                   1860:                       list = &lH6;
                   1861:                     }
1.135     vatton   1862: 
1.142     vatton   1863:                   if (*list == NULL)
1.135     vatton   1864:                     {
1.142     vatton   1865:                       /* generate the list */
                   1866:                       *list = TtaNewElement (doc, ulType);
                   1867:                       child = TtaGetLastChild (parent);
                   1868:                       if (child)
                   1869:                         TtaInsertSibling (*list, child, FALSE, doc);
                   1870:                       else
                   1871:                         TtaInsertFirstChild (list, parent, doc);
                   1872:                       TtaRegisterElementCreate (*list, doc);
1.135     vatton   1873:                     }
1.142     vatton   1874:                   /* generate the list item */
                   1875:                   elType.ElTypeNum = HTML_EL_List_Item;
                   1876:                   item = TtaNewElement (doc, elType);
                   1877:                   /* generate the HTML_EL_Pseudo_paragraph */
                   1878:                   elType.ElTypeNum =  HTML_EL_Pseudo_paragraph;
                   1879:                   parent = TtaNewElement (doc, elType);
                   1880:                   TtaInsertFirstChild (&parent, item, doc);
                   1881:                   /* generate the link anchor */
                   1882:                   elType.ElTypeNum = HTML_EL_Anchor;
                   1883:                   new_ = TtaNewElement (doc, elType);
                   1884:                   TtaInsertFirstChild (&new_, parent, doc);
                   1885:                   attrType.AttrTypeNum = HTML_ATTR_HREF_;
                   1886:                   attr = TtaNewAttribute (attrType);
                   1887:                   TtaAttachAttribute (new_, attr, doc);
                   1888:                   TtaSetAttributeText (attr, id, new_, doc);
                   1889:                   TtaFreeMemory (id);
                   1890:                   id = NULL;
                   1891:                   /* get a copy of the Hi contents */
                   1892:                   srce = TtaGetFirstChild (el);
                   1893:                   prev = NULL;
                   1894:                   parent = NULL;
                   1895:                   while (srce)
1.135     vatton   1896:                     {
1.142     vatton   1897:                       copyType = TtaGetElementType (srce);
                   1898:                       if (copyType.ElTypeNum == HTML_EL_Anchor &&
                   1899:                           copyType.ElSSchema == elType.ElSSchema)
1.135     vatton   1900:                         {
1.142     vatton   1901:                           /* copy the anchor contents instead of the anchor */
                   1902:                           parent = srce;
                   1903:                           srce = TtaGetFirstChild (parent);
1.135     vatton   1904:                         }
                   1905:                       if (srce)
1.142     vatton   1906:                         {
                   1907:                           /* copy children of the next source */
                   1908:                           copy = TtaCopyTree (srce, doc, doc, new_);
                   1909:                           if (copy)
                   1910:                             {
                   1911:                               if (prev == NULL)
                   1912:                                 /* this is the first copied element. Insert it before elem */
                   1913:                                 TtaInsertFirstChild (&copy, new_, doc);
                   1914:                               else
                   1915:                                 /* insert the new copied element after the element previously
                   1916:                                    copied */
                   1917:                                 TtaInsertSibling (copy, prev, FALSE, doc);
                   1918:                               prev = copy;
                   1919:                             }
                   1920:                           TtaNextSibling (&srce);
                   1921:                         }
                   1922:                       if (srce == NULL && parent)
                   1923:                         {
                   1924:                           /* copy children of an anchor */
                   1925:                           srce = parent;
                   1926:                           parent = NULL;
                   1927:                           if (srce)
                   1928:                             TtaNextSibling (&srce);
                   1929:                         }
1.135     vatton   1930:                     }
1.142     vatton   1931:                   child = TtaGetLastChild (*list);
                   1932:                   if (child)
                   1933:                     TtaInsertSibling (item, child, FALSE, doc);
                   1934:                   else
                   1935:                     TtaInsertFirstChild (&item, *list, doc);
                   1936:                   TtaRegisterElementCreate (item, doc);
1.135     vatton   1937:                 }
                   1938:             }
                   1939:         }
1.142     vatton   1940:       // check if we have to retry in the whole document
                   1941:       if (toc)
                   1942:         retry = FALSE;
                   1943:       else if (retry)
                   1944:         { 
                   1945:           el = TtaGetMainRoot (doc);
                   1946:           retry = FALSE;
                   1947:         }
1.114     vatton   1948:     }
                   1949:   if (closeUndo)
                   1950:     TtaCloseUndoSequence (doc);
1.137     vatton   1951: 
                   1952:   if (toc == NULL)
1.142     vatton   1953:     {
                   1954:       if (dispMode == DisplayImmediately)
                   1955:         TtaSetDisplayMode (doc, dispMode);
                   1956:       TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_NO_HEADING_FOUND);
                   1957:     }
1.137     vatton   1958:   else
1.115     vatton   1959:     {
1.137     vatton   1960:       /* force a complete redisplay to apply CSS */
                   1961:       TtaSetDisplayMode (doc, NoComputedDisplay);
                   1962:       if (dispMode == DisplayImmediately)
                   1963:         TtaSetDisplayMode (doc, dispMode);
                   1964:       /* select the end of the toc */
                   1965:       if (prev)
1.135     vatton   1966:         {
1.137     vatton   1967:           child = prev;
                   1968:           while (child)
                   1969:             {
                   1970:               child = TtaGetLastChild (prev);
                   1971:               if (child)
                   1972:                 prev = child;
                   1973:             }
                   1974:           elType = TtaGetElementType (prev);
                   1975:           if (elType.ElTypeNum == HTML_EL_TEXT_UNIT)
                   1976:             {
                   1977:               i = TtaGetElementVolume (prev);
                   1978:               TtaSelectString (doc, prev, i+1, i);
                   1979:             }
                   1980:           else
                   1981:             TtaSelectElement (doc, prev);
1.135     vatton   1982:         }
1.115     vatton   1983:     }
1.114     vatton   1984: }

Webmaster