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

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

Webmaster