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

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

Webmaster