Annotation of Amaya/amaya/templates.c, revision 1.46

1.1       cvs         1: /*
                      2:  *
1.27      vatton      3:  *  COPYRIGHT INRIA and W3C, 1996-2005
1.1       cvs         4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
1.14      cvs         7:  
1.1       cvs         8: /*
                      9:  * Amaya browser functions called form Thot and declared in HTML.A.
                     10:  * These functions concern links and other HTML general features.
                     11:  *
1.15      cvs        12:  * Authors: I. Vatton, L. Bonameau, S. Bonhomme (INRIA)
1.1       cvs        13:  *
                     14:  */
                     15: 
                     16: /* Included headerfiles */
1.29      tollenae   17: 
                     18: 
1.1       cvs        19: #define THOT_EXPORT extern
                     20: #include "amaya.h"
                     21: #include "css.h"
                     22: #include "document.h"
                     23: #include "view.h"
1.46    ! vatton     24: #ifdef TEMPLATES
        !            25: #include "Template.h"
        !            26: #endif /* TEMPLATES */
        !            27: 
        !            28: #include "AHTURLTools_f.h"
1.28      tollenae   29: #include "appdialogue_wx.h"
1.46    ! vatton     30: #include "css_f.h"
1.29      tollenae   31: #include "init_f.h"
1.32      tollenae   32: #include "parser.h"
1.46    ! vatton     33: #include "HTMLhistory_f.h"
1.29      tollenae   34: #include "HTMLimage_f.h"
1.32      tollenae   35: #include "HTMLsave_f.h"
1.46    ! vatton     36: #include "tree.h"
        !            37: #include "wxdialogapi_f.h"
1.1       cvs        38: 
                     39: /* content of the attr name of the meta tag defining the doc's destination URL */
1.18      cvs        40: #define META_TEMPLATE_NAME "AMAYA_TEMPLATE"
1.1       cvs        41: /* URL of the script providing templates (for reload) */
1.18      cvs        42: static char   *script_URL;
1.3       cvs        43: 
                     44: 
1.1       cvs        45: /*----------------------------------------------------------------------
1.34      tollenae   46:    NewTemplate: Create the "new document from template's" dialog
1.1       cvs        47:   ----------------------------------------------------------------------*/
1.18      cvs        48: void NewTemplate (Document doc, View view)
1.1       cvs        49: {
1.31      tollenae   50: #ifdef TEMPLATES
1.28      tollenae   51:   char *templateDir ;
                     52: 
                     53:   templateDir = (char *) TtaGetMemory (MAX_LENGTH);
                     54: 
                     55:   // Amaya's templates directory (only french yet)
1.46    ! vatton     56:   sprintf ((char *)templateDir, "%s%ctemplates%cfr%c",
        !            57:            TtaGetEnvString ("THOTDIR"),DIR_SEP,DIR_SEP,DIR_SEP);
1.28      tollenae   58:   
                     59:   char s [MAX_LENGTH];
                     60:   
                     61:   TtaExtractName (DocumentURLs[doc], s, DocumentName);
                     62:   strcpy (DirectoryName, s);
                     63: 
                     64:   int window_id  = TtaGetDocumentWindowId( doc, view );
                     65:   ThotWindow p_window = (ThotWindow) TtaGetWindowFromId(window_id);
                     66: 
                     67:   
                     68:   ThotBool created = CreateNewTemplateDocDlgWX(BaseDialog + OpenTemplate,
                     69:                                               p_window,
                     70:                                               doc,
1.31      tollenae   71:                                               TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),
1.28      tollenae   72:                                               templateDir,
                     73:                                               s);
                     74:   
                     75:   if (created)
1.25      vatton     76:     {
1.28      tollenae   77:       TtaSetDialoguePosition ();
                     78:       TtaShowDialogue (BaseDialog + OpenTemplate, TRUE);
1.25      vatton     79:     }
1.28      tollenae   80: 
1.31      tollenae   81: #endif // TEMPLATES
1.1       cvs        82: }
1.25      vatton     83: 
1.28      tollenae   84: 
                     85: 
                     86: 
1.1       cvs        87: /*----------------------------------------------------------------------
                     88:    OpenTemplateDocument: Process the meta of a template document,
                     89:      changes the URL, try to save it and warn the user if it cannot be
                     90:      saved.
                     91:   ----------------------------------------------------------------------*/
1.28      tollenae   92: 
1.18      cvs        93: void OpenTemplateDocument (Document doc)
1.1       cvs        94: {
1.43      tollenae   95: #ifdef TEMPLATES
1.1       cvs        96:   ElementType         metaElType;
                     97:   Element             metaEl;
                     98:   Attribute           metaAttr;
                     99:   AttributeType       metaAttrType;
                    100:   ThotBool            found = FALSE;
                    101:   int                 length;
1.24      vatton    102:   char               *utf8path, *path;
                    103:   char               *URLdir, *URLname;
1.1       cvs       104: 
                    105:   metaElType.ElSSchema = TtaGetDocumentSSchema (doc);
                    106:   metaElType.ElTypeNum = HTML_EL_META;
                    107:   metaAttrType.AttrSSchema = TtaGetDocumentSSchema (doc);
                    108:   metaAttrType.AttrTypeNum = HTML_ATTR_meta_name;
                    109:   /* search the meta element */
                    110:   metaEl = TtaGetMainRoot (doc);
1.24      vatton    111:   path = NULL;
1.1       cvs       112:   do
                    113:     {
                    114:       metaEl = TtaSearchTypedElement (metaElType, SearchForward, metaEl);
                    115:       if (metaEl != NULL)
                    116:         {
                    117:           metaAttr = TtaGetAttribute (metaEl, metaAttrType); 
                    118:           if (metaAttr != NULL)
                    119:             {
1.24      vatton    120:              length = TtaGetTextAttributeLength (metaAttr) + 1;
                    121:              utf8path = (char *) TtaGetMemory (length);
                    122:              TtaGiveTextAttributeValue (metaAttr, utf8path, &length);
                    123:              path = (char *)TtaConvertMbsToByte ((unsigned char *)utf8path,
                    124:                                                  TtaGetDefaultCharset ());
                    125:              TtaFreeMemory (utf8path);
                    126:               found = !strcmp (path, META_TEMPLATE_NAME);
1.26      cvs       127:              /* free previous path if necessary */
                    128:           TtaFreeMemory (path);
                    129:                  path = NULL;
1.1       cvs       130:             }
                    131:         }
                    132:     }
1.24      vatton    133:   while (metaEl && !found);
                    134: 
1.1       cvs       135:   if (found)
                    136:     {
                    137:       /* get the url of the document */
                    138:       metaAttrType.AttrTypeNum = HTML_ATTR_meta_content; 
                    139:       metaAttr = TtaGetAttribute (metaEl, metaAttrType);
                    140:       if (metaAttr != NULL)
                    141:        {
1.24      vatton    142:          length = TtaGetTextAttributeLength (metaAttr) + 1;
                    143:          utf8path = (char *) TtaGetMemory (length);
                    144:          TtaGiveTextAttributeValue (metaAttr, utf8path, &length);
                    145:          path = (char *)TtaConvertMbsToByte ((unsigned char *)utf8path,
                    146:                                              TtaGetDefaultCharset ());
                    147:          TtaFreeMemory (utf8path);
1.1       cvs       148:        }
1.24      vatton    149: 
1.1       cvs       150:       /* Delete the meta element */
                    151:       TtaDeleteTree (metaEl, doc);  
1.24      vatton    152:       if (script_URL == NULL)
                    153:        script_URL = TtaStrdup (DocumentURLs[doc]);
                    154:       TtaFreeMemory (DocumentURLs[doc]);
                    155:       DocumentURLs[doc] = TtaStrdup (path);
                    156:       DocumentMeta[doc]->method = CE_TEMPLATE;
                    157:       AddURLInCombobox (DocumentURLs[doc], NULL, TRUE);
                    158:       TtaSetTextZone (doc, 1, URL_list);
                    159:       TtaSetDocumentUnmodified (doc);
                    160:       
1.1       cvs       161:       /* set the document name and dir */
1.24      vatton    162:       URLname = strrchr (path, URL_SEP);
1.5       cvs       163:       if (URLname)
                    164:        {
                    165:          URLname[0] = EOS;
                    166:          URLname++;
1.24      vatton    167:          URLdir = path;
1.5       cvs       168:          TtaSetDocumentDirectory (doc, URLdir);
1.9       cvs       169:          TtaSetDocumentName (doc, URLname);
1.5       cvs       170:          /* SetBrowserEditor(doc); */ 
                    171:        }
                    172:       else
                    173:        {
1.16      cvs       174:          TtaSetDocumentDirectory (doc, "");
1.24      vatton    175:          TtaSetDocumentName (doc, path);
1.5       cvs       176:        }
1.9       cvs       177:       SetWindowTitle (doc, doc, 0);
1.1       cvs       178:     }
1.24      vatton    179:   TtaFreeMemory (path);
1.43      tollenae  180: #endif /* TEMPLATES */
1.1       cvs       181: }
                    182:   
                    183: /*----------------------------------------------------------------------
                    184:   ReloadTemplateParams : restores the script URL and method into meta
                    185:   to reload a template
                    186:   ----------------------------------------------------------------------*/
1.18      cvs       187: void ReloadTemplateParams (char **docURL, ClickEvent *method)
1.1       cvs       188: {
                    189:    *method = CE_FORM_GET;
1.5       cvs       190:    TtaFreeMemory (*docURL);
1.17      cvs       191:    *docURL = TtaStrdup (script_URL); 
1.1       cvs       192: }
1.28      tollenae  193: 
                    194: 
                    195: /*------------------------------------------------------------------------
                    196:   InitTemplateList : fills template list with HTML files in 
                    197:   the templates directory
                    198:   ----------------------------------------------------------------------*/
                    199: void InitTemplateList ()
                    200: {
1.43      tollenae  201: #ifdef TEMPLATES
1.28      tollenae  202:   int i, nb,len;
                    203:   unsigned char *urlstring,c;
                    204:   char          *app_home;
                    205:   FILE          *file;
                    206:   CHARSET       encoding;
                    207:   
                    208:   TtaFreeMemory(Template_list);
                    209:   
                    210:   urlstring = (unsigned char *) TtaGetMemory (MAX_LENGTH);
                    211:   
                    212:   /* open the file list_url.dat into APP_HOME directory */
                    213:   app_home = TtaGetEnvString ("APP_HOME");
                    214: 
                    215:   sprintf ((char *)urlstring, "%s%clist_url_utf8.dat", app_home, DIR_SEP);
                    216:   encoding = UTF_8;
                    217:   
                    218:   file = TtaReadOpen ((char *)urlstring);
                    219: 
                    220:   *urlstring = EOS;
                    221:   if (file)
                    222:     {
                    223:       /* get the size of the file */
                    224:       fseek (file, 0L, 2);     
                    225:       /* end of the file */
1.30      gully     226:       Template_list_len = (ftell (file) * 4) + GetMaxURLList() + 4;
1.28      tollenae  227:       Template_list = (char *)TtaGetMemory (Template_list_len);
                    228:       Template_list[0] = EOS;
                    229:       fseek (file, 0L, 0);     /* beginning of the file */
                    230:       /* initialize the list by reading the file */
                    231:       i = 0;
                    232:       nb = 0;
                    233:       while (TtaReadByte (file, &c))
1.43      tollenae  234:         {
                    235:           if (c == '"')
                    236:             {
                    237:               len = 0;
                    238:               urlstring[len] = EOS;
                    239:               while (len < MAX_LENGTH && TtaReadByte (file, &c) && c != EOL)
                    240:                 {
                    241:                   if (c == '"')
                    242:                     urlstring[len] = EOS;
                    243:                   else if (c == 13) /* Carriage return */
                    244:                     urlstring[len] = EOS;
                    245:                   else
                    246:                     urlstring[len++] = (char)c;
                    247:                 }
                    248:               urlstring[len] = EOS;
                    249:               if (i > 0 && len)
                    250:                 /* add an EOS between two urls */
                    251:                 URL_list[i++] = EOS;
                    252:               if (len)
                    253:                 {
                    254:                   nb++;
                    255:                   strcpy ((char *)&Template_list[i], (char *)urlstring);
                    256:                   i += len;
                    257:                 }
                    258:             }
                    259:         }
1.28      tollenae  260:       Template_list[i + 1] = EOS;
                    261:       TtaReadClose (file);
                    262:     }
                    263:   TtaFreeMemory (urlstring);
1.43      tollenae  264: #endif /* TEMPLATES */
1.28      tollenae  265: }
1.37      tollenae  266: 
1.46    ! vatton    267: /*----------------------------------------------------------------------
        !           268:  Insert the meta element identifying the template's instance
        !           269:   ----------------------------------------------------------------------*/
1.42      tollenae  270: void InsertInstanceMeta (Document newdoc)
1.36      tollenae  271: {
1.43      tollenae  272: #ifdef TEMPLATES
1.42      tollenae  273:   Element head = TtaGetMainRoot (newdoc);
1.36      tollenae  274:   Element meta;
                    275:   ElementType elType;
                    276:   AttributeType        attrType;
                    277:   Attribute attr;
1.42      tollenae  278:   char *temp_id, *name;
                    279:   int length;
                    280:   ThotBool found = false;
1.36      tollenae  281:   
                    282:   elType.ElSSchema = TtaGetSSchema ("HTML", newdoc);
                    283:   elType.ElTypeNum = HTML_EL_HEAD;
1.42      tollenae  284:   head = TtaSearchTypedElement(elType, SearchInTree, head);
1.36      tollenae  285:   if (head)
                    286:     {
                    287:       attrType.AttrTypeNum = HTML_ATTR_meta_name;
                    288:       attrType.AttrSSchema = elType.ElSSchema;
1.42      tollenae  289: 
                    290:       elType.ElTypeNum = HTML_EL_META;      
                    291:       /* Search the template_id meta in the template document */
                    292:       meta = TtaSearchTypedElement(elType, SearchInTree, head);
                    293:       while (meta && !found)
                    294:         {
                    295:           attr = TtaGetAttribute (meta, attrType);
                    296:           if (attr != NULL)
                    297:             {
                    298:               length = TtaGetTextAttributeLength (attr) + 1;
                    299:               name = (char *) TtaGetMemory (length);
                    300:               TtaGiveTextAttributeValue (attr, name, &length);
                    301:               if (!strcmp (name, "template_id"))
                    302:                 {
                    303:                   /* The element is the template ID meta */
                    304:                   attrType.AttrTypeNum = HTML_ATTR_meta_content;
                    305:                   attr = TtaGetAttribute (meta, attrType);
                    306:                   
                    307:                   if (attr)
                    308:                     {
                    309:                       length = TtaGetTextAttributeLength (attr) + 1;
                    310:                       temp_id = (char *) TtaGetMemory (length);
                    311:                       TtaGiveTextAttributeValue (attr, temp_id, &length);
                    312:                       found = True;
                    313:                     }
                    314:                   TtaFreeMemory(name);
                    315:                 }
                    316:             }
                    317:           meta = TtaSearchTypedElement(elType, SearchForward, meta);
                    318:         }
                    319:       /* now "temp_id" should contain the content of template_id meta */
1.36      tollenae  320:       
1.42      tollenae  321:       if (found)
                    322:         {
                    323:           meta = TtaNewElement (newdoc, elType);
                    324:           
                    325:           /* Create the "name" attribute */
                    326:           
                    327:           attrType.AttrTypeNum = HTML_ATTR_meta_name;
                    328:           attr = TtaNewAttribute (attrType);
                    329:           TtaAttachAttribute (meta, attr, newdoc);
                    330:           TtaSetAttributeText (attr, "template", meta, newdoc);
                    331: 
                    332:           /* Create the "content" attribute */
                    333:           attrType.AttrTypeNum = HTML_ATTR_meta_content;
                    334:           attr = TtaNewAttribute (attrType);
                    335:           TtaAttachAttribute (meta, attr, newdoc);
                    336:           TtaSetAttributeText (attr, temp_id, meta, newdoc);
                    337:           
                    338:           TtaInsertFirstChild (&meta, head, newdoc);
                    339:         }
                    340:     }  
1.43      tollenae  341: #endif /*TEMPLATES*/
1.36      tollenae  342: }
1.29      tollenae  343: 
                    344: 
1.46    ! vatton    345: /*----------------------------------------------------------------------
        !           346:   ----------------------------------------------------------------------*/
        !           347: static void UnlockContentElements (Document doc, Element el)
1.39      tollenae  348: {
1.43      tollenae  349: #ifdef TEMPLATES
1.39      tollenae  350:   ElementType elType;
                    351: 
1.46    ! vatton    352:   if (el) 
1.39      tollenae  353:     {
                    354:       elType = TtaGetElementType (el);
                    355:       if (TtaIsLeaf (elType))
1.46    ! vatton    356:         /* It's a content element */
        !           357:         TtaSetAccessRight (el, ReadWrite, doc);
1.39      tollenae  358:       else
                    359:         {
1.46    ! vatton    360:           el = TtaGetFirstChild(el);
        !           361:           while (el)
        !           362:             {
        !           363:               UnlockContentElements (doc,el);
        !           364:               TtaNextSibling (&el);
        !           365:             }
1.39      tollenae  366:         }
                    367:     }
1.43      tollenae  368: #endif /*TEMPLATES*/
1.39      tollenae  369: }
                    370: 
1.46    ! vatton    371: /*----------------------------------------------------------------------
        !           372:   CheckFreeAreas
        !           373:   Parse the subtree from el, set read-write access to the child elements
        !           374:   of  free_struct elements.
        !           375:   ----------------------------------------------------------------------*/
        !           376: static void CheckFreeAreas (Document doc, Element el)
1.32      tollenae  377: {
                    378: #ifdef TEMPLATES
1.46    ! vatton    379:   ElementType  elType;
        !           380:   char        *s;
1.32      tollenae  381:   
1.46    ! vatton    382:   el = TtaGetFirstChild(el);
        !           383:   while (el)
1.32      tollenae  384:     {
1.46    ! vatton    385:       elType = TtaGetElementType (el);
        !           386:       s = TtaGetSSchemaName (elType.ElSSchema);
1.32      tollenae  387:       /* The element is not a leaf */
1.46    ! vatton    388:       if (!strcmp (s,"Template") &&
        !           389:           elType.ElTypeNum == Template_EL_FREE_STRUCT)
        !           390:         /* The element has a free structure */
        !           391:         TtaSetAccessRight (el, ReadWrite, doc);
        !           392:       else if (!strcmp (s,"Template") &&
        !           393:                elType.ElTypeNum == Template_EL_FREE_CONTENT)
        !           394:         /* The element has free content */
        !           395:         UnlockContentElements (doc, el);
        !           396:       else
        !           397:         /* The element has a fixed structure */
        !           398:         /* So we look for a free structure in
        !           399:            the subtree */
        !           400:         CheckFreeAreas(doc, el);
        !           401:       TtaNextSibling (&el);
1.32      tollenae  402:     }
                    403: #endif /* TEMPLATES */
                    404: }
1.29      tollenae  405: 
1.46    ! vatton    406: /*----------------------------------------------------------------------
1.32      tollenae  407:   Load a template and create the instance file - update images and 
1.29      tollenae  408:   stylesheets related to the template.
1.46    ! vatton    409:   ----------------------------------------------------------------------*/
1.29      tollenae  410: int CreateInstanceOfTemplate (Document doc, char *templatename, char *docname,
1.46    ! vatton    411:                               DocumentType docType)
1.29      tollenae  412: {
1.43      tollenae  413: #ifdef TEMPLATES
1.41      cvs       414:   Element       el;
1.46    ! vatton    415:   char                *s;
1.29      tollenae  416:   int           newdoc, len;
                    417:   ThotBool      stopped_flag;
                    418: 
1.46    ! vatton    419:   if (!IsW3Path (docname) && TtaFileExist (docname))
        !           420:     {
        !           421:       s = (char *)TtaGetMemory (strlen (docname) +
        !           422:                                 strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
        !           423:       sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
        !           424:       InitConfirm (0, 0, s);
        !           425:       TtaFreeMemory (s);
        !           426:       if (!UserAnswer)
        !           427:         return 0;
        !           428:     }
        !           429: 
        !           430:   if (InNewWindow || DontReplaceOldDoc)
        !           431:     {
        !           432:       newdoc = InitDocAndView (doc,
        !           433:                                !DontReplaceOldDoc /* replaceOldDoc */,
        !           434:                                InNewWindow, /* inNewWindow */
        !           435:                                DocumentName, (DocumentType)docType, 0, FALSE,
        !           436:                                L_Other, (ClickEvent)CE_ABSOLUTE);
        !           437:       DontReplaceOldDoc = FALSE;
        !           438:     }
        !           439:   else
        !           440:     {
        !           441:       /* record the current position in the history */
        !           442:       AddDocHistory (doc, DocumentURLs[doc], 
        !           443:                      DocumentMeta[doc]->initial_url,
        !           444:                      DocumentMeta[doc]->form_data,
        !           445:                      DocumentMeta[doc]->method);
        !           446:       newdoc = InitDocAndView (doc,
        !           447:                                !DontReplaceOldDoc /* replaceOldDoc */,
        !           448:                                InNewWindow, /* inNewWindow */
        !           449:                                DocumentName, (DocumentType)docType, 0, FALSE,
        !           450:                                L_Other, (ClickEvent)CE_ABSOLUTE);
        !           451:     }
        !           452: 
        !           453:   //W3Loading = doc;
        !           454:   //BackupDocument = doc;
1.33      tollenae  455:   if (newdoc != 0)
1.29      tollenae  456:     {
1.46    ! vatton    457:       TtaExtractName (templatename, DirectoryName, DocumentName);
        !           458:       //AddURLInCombobox (docname, NULL, TRUE);
        !           459:       //TtaSetTextZone (newdoc, 1, URL_list);
        !           460: #ifdef IV
        !           461:         if (DocumentMeta[newdoc]->xmlformat && !plainText)
        !           462:           StartXmlParser (newdoc,      localdoc, documentname, tempdir,
        !           463:                           pathname, xmlDec, withDoctype, FALSE);
        !           464:         else
        !           465:           StartParser (newdoc, localdoc, documentname, tempdir,
        !           466:                        pathname, plainText, FALSE);
        !           467: #endif
1.29      tollenae  468:       LoadDocument (newdoc, templatename, NULL, NULL, CE_ABSOLUTE,
1.33      tollenae  469:                     "", DocumentName, NULL, FALSE, &DontReplaceOldDoc);
1.46    ! vatton    470:       InsertInstanceMeta (newdoc);
1.33      tollenae  471:       
1.32      tollenae  472:       /* Update URLs of linked documents */
                    473:       SetRelativeURLs (newdoc, docname);
1.33      tollenae  474:       
1.29      tollenae  475:       /* change its URL */
                    476:       TtaFreeMemory (DocumentURLs[newdoc]);
                    477:       len = strlen (docname) + 1;
                    478:       DocumentURLs[newdoc] = TtaStrdup (docname);
                    479:       DocumentSource[newdoc] = 0;
                    480:       /* add the URI in the combobox string */
                    481:       AddURLInCombobox (docname, NULL, FALSE);
                    482:       TtaSetTextZone (newdoc, 1, URL_list);
                    483:       /* change its directory name */
                    484:       TtaSetDocumentDirectory (newdoc, DirectoryName);
                    485: 
                    486:       TtaSetDocumentModified (newdoc);
                    487:       W3Loading = 0;           /* loading is complete now */
                    488:       DocNetworkStatus[newdoc] = AMAYA_NET_ACTIVE;
                    489:       stopped_flag = FetchAndDisplayImages (newdoc, AMAYA_LOAD_IMAGE, NULL);
                    490:       if (!stopped_flag)
1.32      tollenae  491:         {
                    492:           DocNetworkStatus[newdoc] = AMAYA_NET_INACTIVE;
                    493:           /* almost one file is restored */
                    494:           TtaSetStatus (newdoc, 1, TtaGetMessage (AMAYA, AM_DOCUMENT_LOADED),
                    495:                           NULL);
                    496:         }
1.29      tollenae  497:       /* check parsing errors */
                    498:       CheckParsingErrors (newdoc);
1.46    ! vatton    499:       /* Set elements access rights according to free_* elements */
1.41      cvs       500:       el = TtaGetMainRoot (newdoc);
1.46    ! vatton    501:       if (el)
        !           502:         {
        !           503:           TtaSetAccessRight (el, ReadOnly, newdoc);
        !           504:           CheckFreeAreas (newdoc, el);
        !           505:         }
1.29      tollenae  506:     }
1.33      tollenae  507:    BackupDocument = 0;
                    508:    return (newdoc);
1.43      tollenae  509: #endif /* TEMPLATES */
1.29      tollenae  510: }
                    511: 
1.46    ! vatton    512: /*----------------------------------------------------------------------
1.42      tollenae  513:   Here we put all the actions to performs when we know a document 
                    514:   is an instance
1.46    ! vatton    515:   ----------------------------------------------------------------------*/
1.37      tollenae  516: void LoadInstanceOfTemplate (Document doc)
                    517: {
1.43      tollenae  518: #ifdef TEMPLATES
                    519:   ThotBool   show;
                    520:   Element    el = TtaGetMainRoot (doc);
                    521:   char      *css_url;
                    522:   PInfoPtr            pInfo;
                    523:   CSSInfoPtr css;
                    524: 
1.46    ! vatton    525:   el = TtaGetMainRoot (doc);
        !           526:   TtaSetAccessRight (el, ReadOnly, doc);
        !           527:   CheckFreeAreas (doc, el);
1.43      tollenae  528:   TtaGetEnvBoolean ("SHOW_TEMPLATES", &show);
                    529:   css_url = (char *) TtaGetMemory (MAX_LENGTH);
                    530:   sprintf (css_url, "%s%camaya%chide_template.css", TtaGetEnvString ("THOTDIR"),DIR_SEP,DIR_SEP);
                    531:   if (!show)
                    532:     {
                    533:       /* We don't want to show the frames around template's elements,
                    534:          so we load a stylesheet with border: none for all template's 
                    535:          elements */
                    536:       
                    537:       LoadStyleSheet (css_url, doc, NULL, NULL, NULL, CSS_ALL, FALSE);
                    538:     }
                    539:   else
                    540:     {
                    541:       css = SearchCSS (doc, css_url, NULL, &pInfo);
                    542:       if (css)
                    543:         RemoveStyle (css_url, doc, TRUE, TRUE, NULL, CSS_EXTERNAL_STYLE);
                    544:     }
                    545:   TtaFreeMemory(css_url);
                    546: #endif /* TEMPLATES */
1.37      tollenae  547: }
1.42      tollenae  548: 
                    549: 
1.46    ! vatton    550: /*----------------------------------------------------------------------
1.42      tollenae  551:   ThotBool isTemplateInstance (Document doc) 
                    552:   Return true if the document is an instance 
1.46    ! vatton    553:   ----------------------------------------------------------------------*/
1.42      tollenae  554: ThotBool IsTemplateInstance (Document doc)
                    555: {
1.43      tollenae  556: #ifdef TEMPLATES
1.42      tollenae  557:   Element el = TtaGetMainRoot (doc);
                    558:   Element meta;
                    559:   ElementType metaType;
                    560:   AttributeType        attrType;
                    561:   Attribute attr;
                    562:   ThotBool found = false;
                    563:   char* name,* version;
                    564:   int length;
                    565:   
                    566:   /* metaType is first used to find a meta element */
                    567:   metaType.ElSSchema = TtaGetSSchema ("HTML", doc);
                    568:   metaType.ElTypeNum = HTML_EL_META;
                    569:   meta = TtaSearchTypedElement(metaType, SearchInTree, el);
                    570: 
                    571:   /* attrType will be used to find the name attribute of a meta */
                    572:   attrType.AttrSSchema = metaType.ElSSchema;
                    573:   attrType.AttrTypeNum = HTML_ATTR_meta_name;
                    574:   while (meta && !found)
                    575:     {
                    576:       attr = TtaGetAttribute (meta, attrType);
                    577:       if (attr != NULL)
                    578:         /* The element contains a name attribute */
                    579:         {
                    580:           length = TtaGetTextAttributeLength (attr) + 1;
                    581:           name = (char *) TtaGetMemory (length);
                    582:           TtaGiveTextAttributeValue (attr, name, &length);
                    583: 
                    584:           if (!strcmp(name, "template"))
                    585:             {
                    586:               /* The element is the template meta, the document is a template instance */
                    587:               attrType.AttrTypeNum = HTML_ATTR_meta_content;
                    588:               attr = TtaGetAttribute (meta, attrType);
                    589:               
                    590:               if (attr)
                    591:                 {
                    592:                   length = TtaGetTextAttributeLength (attr) + 1;
                    593:                   version = (char *) TtaGetMemory (length);
                    594:                   TtaGiveTextAttributeValue (attr, version, &length);
                    595:                   
                    596:                   DocumentMeta[doc]->template_version = version;
                    597:                   TtaFreeMemory (version);
                    598:                   found = True;
                    599:                 }
                    600:             }
                    601:           TtaFreeMemory(name);
                    602:         }
                    603:       meta = TtaSearchTypedElement(metaType, SearchForward, meta);
                    604:     }
                    605:   
                    606:   return found;
1.43      tollenae  607: #else /* TEMPLATES */
1.44      cvs       608:   return FALSE;
1.43      tollenae  609: #endif /* TEMPLATES */
1.42      tollenae  610: }

Webmaster