Annotation of Amaya/amaya/templateInstantiate.c, revision 1.120

1.19      vatton      1: /*
                      2:  *
1.104     vatton      3:  *  COPYRIGHT INRIA and W3C, 2006-2009
1.19      vatton      4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
                      7: 
1.1       vatton      8: #include "templates.h"
                      9: #define THOT_EXPORT extern
1.71      vatton     10: #include "amaya.h"
1.75      quint      11: #include "css.h"
1.1       vatton     12: #include "templateDeclarations.h"
                     13: 
1.8       kia        14: #include "Elemlist.h"
1.71      vatton     15: #include "AHTURLTools_f.h"
1.63      kia        16: #include "wxdialogapi_f.h"
1.1       vatton     17: #include "EDITimage_f.h"
1.84      kia        18: #include "HTMLedit_f.h"
1.1       vatton     19: #include "HTMLsave_f.h"
1.34      vatton     20: #include "HTMLtable_f.h"
1.49      vatton     21: #include "html2thot_f.h"
1.1       vatton     22: #include "init_f.h"
                     23: #include "templates_f.h"
                     24: #include "templateDeclarations_f.h"
                     25: #include "templateInstantiate_f.h"
                     26: #include "Templatebuilder_f.h"
1.63      kia        27: #include "templateLoad_f.h"
1.1       vatton     28: #include "templateUtils_f.h"
                     29: #include "fetchHTMLname_f.h"
                     30: #include "Template.h"
1.94      vatton     31: #include "fetchXMLname_f.h"
1.86      vatton     32: #include "styleparser_f.h"
1.94      vatton     33: 
1.1       vatton     34: #ifdef TEMPLATES
                     35: #define TEMPLATE_SCHEMA_NAME "Template"
                     36: #endif /* TEMPLATES */
                     37: 
                     38: 
1.49      vatton     39: 
                     40: /*----------------------------------------------------------------------
                     41:   Template_InsertRepeatChildAfter
                     42:   Insert a child to a xt:repeat
                     43:   The decl parameter must be valid and will not be verified. It must be a
                     44:     direct child element or the "use in the use" for union elements.
                     45:   @param el element (xt:repeat) in which insert a new element
                     46:   @param decl Template declaration of the element to insert
                     47:   @param elPrev Element (xt:use) after which insert the new elem, NULL if first.
                     48:   @return The inserted element 
                     49:   ----------------------------------------------------------------------*/
                     50: Element Template_InsertRepeatChildAfter (Document doc, Element el,
                     51:                                          Declaration decl, Element elPrev)
                     52: {
                     53: #ifdef TEMPLATES
1.104     vatton     54:   Element     child; /* First xt:use of the repeat.*/
                     55:   Element     use, parent; /* xt:use to insert.*/
1.49      vatton     56:   ElementType useType;  /* type of xt:use.*/
1.102     vatton     57:   char       *types = NULL;
1.118     vatton     58:   ThotBool    isInstance;
1.49      vatton     59: 
                     60:   if (!TtaGetDocumentAccessMode (doc))
                     61:     return NULL;
1.56      kia        62:   
1.49      vatton     63:   /* Copy xt:use with xt:types param */
1.104     vatton     64:   child = TtaGetFirstChild (el);
                     65:   useType = TtaGetElementType (child);
                     66:   use = TtaCopyElement (child, doc, doc, el);
                     67:   types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
1.118     vatton     68:   isInstance = IsTemplateInstanceDocument(doc);
                     69:   if (isInstance && useType.ElTypeNum != Template_EL_useSimple)
1.104     vatton     70:     TtaChangeElementType (use, Template_EL_useSimple);
1.102     vatton     71:   if (types)
                     72:     {
                     73:       SetAttributeStringValueWithUndo (use, Template_ATTR_types, types);
                     74:       TtaFreeMemory (types);
                     75:     }
                     76:   else
                     77:     SetAttributeStringValueWithUndo (use, Template_ATTR_types, decl->name);
1.49      vatton     78:   /* insert it */
                     79:   if (elPrev)
1.104     vatton     80:     TtaInsertSibling (use, elPrev, FALSE, doc);
1.49      vatton     81:   else
1.104     vatton     82:     TtaInsertSibling (use, child, TRUE, doc);
                     83:   // look for the enclosing target element
                     84:   parent = GetParentLine (use, useType.ElSSchema);
1.118     vatton     85:   if (isInstance)
                     86:     Template_InsertUseChildren (doc, use, decl, parent, TRUE);
                     87:   else
                     88:     {
                     89:       child = Template_FillEmpty (use,doc);
                     90:       TtaSelectElement (doc, child);
                     91:     }
1.102     vatton     92:   SetAttributeStringValueWithUndo (use, Template_ATTR_title, decl->name);
                     93:   SetAttributeStringValueWithUndo (use, Template_ATTR_currentType, decl->name);
1.49      vatton     94:   TtaRegisterElementCreate (use, doc);
                     95:   return use;
                     96: #else /* TEMPLATES */
                     97:   return NULL;
                     98: #endif /* TEMPLATES */
                     99: }
                    100: 
                    101: /*----------------------------------------------------------------------
                    102:   Template_InsertBagChild
                    103:   Insert a child to a xt:bag at the current insertion point.
                    104:   The decl parameter must be valid and will not be verified.
1.102     vatton    105:   @param sel the refered element. If NULL use the selection
                    106:   @param bag element (xt:bag) in which insert a new element
1.49      vatton    107:   @param decl Template declaration of the element to insert
                    108:   @return The inserted element
                    109:   ----------------------------------------------------------------------*/
1.102     vatton    110: Element Template_InsertBagChild (Document doc, Element sel, Element bag,
                    111:                                  Declaration decl, ThotBool before)
1.49      vatton    112: {
                    113: #ifdef TEMPLATES
                    114:   ElementType newElType, selType;
1.119     vatton    115:   Element     use = NULL, el;
1.102     vatton    116:   SSchema     sstempl;
                    117:   int         start, end;
                    118:   ThotBool    open;
1.49      vatton    119: 
                    120:   if (!TtaGetDocumentAccessMode (doc) || !decl)
                    121:     return NULL;
                    122: 
1.102     vatton    123:   TtaGiveFirstSelectedElement (doc, &el, &start, &end);
                    124:   if (sel == NULL)
                    125:     sel = el;
                    126:   if (sel == bag || TtaIsAncestor (sel, bag))
                    127:     {
                    128:       // opent the undo sequence if needed
                    129:       open = TtaHasUndoSequence (doc);
                    130:       if (!open)
                    131:         TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    132:       sstempl = TtaGetSSchema ("Template", doc);
                    133:       selType = TtaGetElementType (sel);
                    134:       if (decl->blockLevel == 2 && 
                    135:           (TtaIsLeaf (selType) || !IsTemplateElement (sel)))
                    136:         {
                    137:           // force the insertion of a block level element at the right position
                    138:           while (sel && IsCharacterLevelElement (sel))
                    139:             sel = TtaGetParent (sel);
                    140:           if (sel)
                    141:             TtaSelectElement (doc, sel);
                    142:         }
                    143: 
                    144:       if (decl->nature == XmlElementNat)
                    145:         {
                    146:           if (el == NULL && sel != bag)
                    147:             // force a selection
                    148:             TtaSelectElement (doc, sel);
1.119     vatton    149:           GIType (decl->name, &newElType, doc);
                    150:           el = TtaNewTree (doc, newElType, "");
1.102     vatton    151:           if (sel == bag)
                    152:             {
                    153:               // insert first an empty element
1.119     vatton    154:               TtaInsertFirstChild (&el, bag, doc);
                    155:               TtaRegisterElementCreate (el, doc);
                    156:               sel = TtaGetFirstChild (el);
                    157:               if (sel == NULL)
                    158:                 sel = el;
                    159:               TtaSelectElement (doc, sel);
1.102     vatton    160:             }
1.119     vatton    161:           else
1.102     vatton    162:             {
1.119     vatton    163:               // insert the new element before or after
                    164:               TtaInsertSibling (el, sel, before, doc);
                    165:               TtaRegisterElementCreate (el, doc);
                    166:               sel = TtaGetFirstChild (el);
                    167:               if (sel == NULL)
                    168:                 sel = el;
                    169:               TtaSelectElement (doc, sel);
1.102     vatton    170:             }
1.119     vatton    171:           //TtaGiveFirstSelectedElement (doc, &sel, &start, &end);
1.102     vatton    172:         }
                    173:       else if (decl->nature == ComponentNat)
                    174:         {
                    175:           // create a use element
                    176:           newElType.ElTypeNum = Template_EL_useSimple;
                    177:           newElType.ElSSchema = sstempl;
                    178:           use = TtaNewElement(doc, newElType);
                    179:           if (use)
                    180:             {
1.104     vatton    181:               Template_InsertUseChildren (doc, use, decl, NULL, TRUE);
1.102     vatton    182:               if (sel != bag)
                    183:                 TtaInsertSibling (use, sel, before, doc);
                    184:               else
                    185:                  TtaInsertFirstChild (&use, bag, doc);
1.110     vatton    186:               SetAttributeStringValueWithUndo (use, Template_ATTR_types, decl->name);
                    187:               SetAttributeStringValueWithUndo (use, Template_ATTR_title, decl->name);
                    188:               SetAttributeStringValueWithUndo (use, Template_ATTR_currentType, decl->name);
1.102     vatton    189:               TtaRegisterElementCreate (use, doc);
1.105     vatton    190:               
1.102     vatton    191:               sel = use;
                    192:             }
                    193:         }
                    194:       else if (decl->nature == UnionNat)
                    195:         {
                    196:           newElType.ElTypeNum = Template_EL_useEl;
                    197:           newElType.ElSSchema = sstempl;
                    198:         }
                    199:       // close the undo sequence
                    200:       if (!open)
                    201:         TtaCloseUndoSequence (doc);
                    202:       return sel;
1.56      kia       203:     }
1.49      vatton    204: #endif /* TEMPLATES */
                    205:   return NULL;
                    206: }
                    207: 
1.80      kia       208: /*----------------------------------------------------------------------
                    209:   InstantiateAttribute
                    210:   ----------------------------------------------------------------------*/
                    211: static void InstantiateAttribute (XTigerTemplate t, Element el, Document doc)
                    212: {
                    213: #ifdef TEMPLATES
                    214:   AttributeType  useType, nameType, defaultType, attrType;
                    215:   Attribute      useAttr, nameAttr, defAttr, attr;
                    216:   ElementType    elType;
                    217:   Element        parent;
                    218:   char           *text, *elementName;
                    219:   ThotBool       level;
                    220:   NotifyAttribute event;
                    221:   int             val;
                    222: 
                    223:   parent = TtaGetParent (el);
                    224:   if (!parent)
                    225:     return;
                    226:   // if attribute "use" has value "optional", don't do anything
                    227:   useType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    228:   useType.AttrTypeNum = Template_ATTR_useAt;
                    229:   useAttr = TtaGetAttribute (el, useType);
                    230:   if (useAttr)
                    231:     // there is a "use" attribute. Check its value
                    232:     {
                    233:       val = TtaGetAttributeValue(useAttr);
                    234:       if (val == Template_ATTR_useAt_VAL_optional)
                    235:       {
                    236:         return;
                    237:       }
                    238:     }
                    239:     
                    240:   // get the "name" and "default" attributes
                    241:   nameType.AttrSSchema = defaultType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    242:   nameType.AttrTypeNum = Template_ATTR_ref_name;
                    243:   defaultType.AttrTypeNum = Template_ATTR_defaultAt;
                    244:   nameAttr = TtaGetAttribute (el, nameType);
                    245:   defAttr = TtaGetAttribute (el, defaultType);
                    246:   if (nameAttr)
                    247:     {
                    248:       text = GetAttributeStringValue (el, nameAttr, NULL);
                    249:       if (text)
                    250:         {
                    251:           elType = TtaGetElementType (parent);
                    252:           elementName = TtaGetElementTypeName (elType);
                    253:           level = TRUE;
                    254:           MapHTMLAttribute (text, &attrType, elementName, &level, doc);
                    255:           TtaFreeMemory(text);
                    256:           attr = TtaNewAttribute (attrType);
                    257:           if (attr)
                    258:             {
                    259:               TtaAttachAttribute (parent, attr, doc);
                    260:               if (defAttr)
                    261:                 {
                    262:                   text = GetAttributeStringValue (el, defAttr, NULL);
                    263:                   if (text)
1.116     vatton    264:                     {
                    265:                       TtaSetAttributeText(attr, text, parent, doc);
                    266:                       TtaFreeMemory(text);
                    267:                     }
                    268:                   else if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML") &&
                    269:                            // if it's a src arttribute for an image, load the image
                    270:                            elType.ElTypeNum == HTML_EL_IMG)
1.80      kia       271:                     if (attrType.AttrTypeNum == HTML_ATTR_SRC &&
                    272:                         attrType.AttrSSchema == elType.ElSSchema)
                    273:                       {
                    274:                         event.document = doc;
                    275:                         event.element = parent;
                    276:                         event.attribute = attr;
                    277:                         SRCattrModified (&event);
                    278:                       }
                    279:                 }
                    280:             }
                    281:         }
                    282:     }
                    283: #endif /* TEMPLATES */
                    284: }
                    285: 
                    286: /*----------------------------------------------------------------------
                    287:   ParseTemplate
1.92      vatton    288:   parentLine points to the enclosing pseudo paragraph or paragraph
1.99      vatton    289:   Parameter loading is TRUE when the document is not already loaded.
1.92      vatton    290:   Return the parentline to be considered for next elements
1.80      kia       291:   ----------------------------------------------------------------------*/
1.113     vatton    292: Element ParseTemplate (XTigerTemplate t, Element el, Document doc,
                    293:                        Element parentLine, ThotBool loading)
1.80      kia       294: {
                    295: #ifdef TEMPLATES
                    296:   AttributeType attType;
                    297:   Attribute     att;
1.101     vatton    298:   Element       next, child = NULL, savedInline, prev, parent = NULL;
1.103     vatton    299:   ElementType   elType, otherType;
1.92      vatton    300:   Declaration   dec;
1.114     vatton    301:   int           option;
1.94      vatton    302:   char         *name, *types;
1.114     vatton    303:   ThotBool      opt;
1.80      kia       304: 
                    305:   if (!t || !el)
1.92      vatton    306:     return parentLine;
                    307: 
                    308:   savedInline = parentLine;
                    309:   elType = TtaGetElementType (el);
1.94      vatton    310:   attType.AttrSSchema = elType.ElSSchema;
1.80      kia       311:   name = TtaGetSSchemaName (elType.ElSSchema);
                    312:   if (!strcmp (name, "Template"))
                    313:     {
1.92      vatton    314:       switch (elType.ElTypeNum)
1.80      kia       315:         {
                    316:         case Template_EL_head :
                    317:           //Remove it and all of its children
                    318:           TtaDeleteTree(el, doc);
                    319:           //We must stop searching into this tree
1.92      vatton    320:           return parentLine;
1.80      kia       321:         case Template_EL_component :
                    322:           // remove the name attribute
                    323:           attType.AttrTypeNum = Template_ATTR_name;
1.87      vatton    324:           name = GetAttributeStringValueFromNum (el, Template_ATTR_name, NULL);
1.80      kia       325:           TtaRemoveAttribute (el, TtaGetAttribute (el, attType), doc);
                    326:           // replace the component by a use
1.93      vatton    327:           prev = el;
                    328:           TtaPreviousSibling (&prev);
                    329:           if (prev == NULL)
                    330:             {
                    331:               next = el;
                    332:               TtaNextSibling (&next);
                    333:               if (next == NULL)
                    334:                 parent = TtaGetParent (el);
                    335:             }
                    336:           TtaRemoveTree (el, doc);
                    337:           TtaChangeElementType (el, Template_EL_useSimple);
1.80      kia       338:           // generate the types attribute
                    339:           attType.AttrTypeNum = Template_ATTR_types;
                    340:           att = TtaNewAttribute (attType);
                    341:           TtaAttachAttribute (el, att, doc);
                    342:           if (name)
                    343:             TtaSetAttributeText (att, name, el, doc);
                    344:           // generate the title attribute
                    345:           attType.AttrTypeNum = Template_ATTR_title;
                    346:           att = TtaNewAttribute (attType);
                    347:           TtaAttachAttribute (el, att, doc);
                    348:           if (name)
                    349:             TtaSetAttributeText (att, name, el, doc);
                    350:           // generate the currentType attribute
                    351:           attType.AttrTypeNum = Template_ATTR_currentType;
                    352:           att = TtaNewAttribute (attType);
                    353:           TtaAttachAttribute (el, att, doc);
                    354:           if (name)
                    355:             TtaSetAttributeText (att, name, el, doc);
1.93      vatton    356:           /* now reinsert the element new map */
                    357:           if (prev != NULL)
                    358:             TtaInsertSibling (el, prev, FALSE, doc);
                    359:           else if (next != NULL)
                    360:             TtaInsertSibling (el, next, TRUE, doc);
                    361:           else
                    362:             TtaInsertFirstChild (&el, parent, doc);
1.80      kia       363:           TtaFreeMemory(name);
1.94      vatton    364:           Template_FixAccessRight (t, el, doc);
                    365:           TtaUpdateAccessRightInViews (doc, el);
1.80      kia       366:           break;
                    367:         case Template_EL_bag :
1.105     vatton    368:           Template_FixAccessRight (t, el, doc);
                    369:           TtaUpdateAccessRightInViews (doc, el);
1.80      kia       370:           break;
                    371:         case Template_EL_useEl :
                    372:         case Template_EL_useSimple :
                    373:           /* if this use element is not empty, don't do anything: it is
                    374:              supposed to contain a valid instance. This should be
                    375:              checked, though */
                    376:             // add the initial indicator
1.94      vatton    377:           types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
                    378:           if (types)
1.87      vatton    379:             {
1.94      vatton    380:               child = TtaGetFirstChild (el);
                    381:               if (!strcmp (types, "string") || !strcmp (types, "number"))
1.120   ! vatton    382:                 {
        !           383:                   if (!loading)
        !           384:                     // don't apply to a loaded instance
        !           385:                     AddPromptIndicator (el, doc);
        !           386:                 }
1.92      vatton    387:               else
                    388:                 {
                    389:                   // avoid to have a block element within a pseudo paragraph
1.94      vatton    390:                   dec = Template_GetDeclaration (t, types);
1.92      vatton    391:                   if (dec && dec->blockLevel == 2 && parentLine)
                    392:                     {
                    393:                       // move the use element after the paragraph
                    394:                       child = TtaGetParent (el);
1.94      vatton    395:                       otherType = TtaGetElementType (child);
                    396:                       if (otherType.ElSSchema != elType.ElSSchema ||
                    397:                           otherType.ElTypeNum == Template_EL_repeat)
1.92      vatton    398:                         // not need to move the parent element
                    399:                           child = el;
                    400:                       next = child;
                    401:                       prev = parentLine;
                    402:                       while (child)
                    403:                         {
                    404:                           // move the element and next siblings after the pseudo paragraph
                    405:                           TtaNextSibling (&next);
                    406:                           TtaRemoveTree (child, doc);
                    407:                           TtaInsertSibling (child, prev, FALSE, doc);
                    408:                           prev = child;
                    409:                           child = next;
                    410:                         }
                    411:                       // elements are now out of the parent line
                    412:                       savedInline = NULL;
1.97      vatton    413:                       parentLine = NULL;
1.109     vatton    414:                       child = TtaGetFirstChild (el);
1.92      vatton    415:                     }
1.94      vatton    416: 
                    417:                   // generate the currentType attribute
                    418:                   otherType = TtaGetElementType (child);
1.109     vatton    419:                   if (otherType.ElSSchema == elType.ElSSchema &&
                    420:                       otherType.ElTypeNum == Template_EL_TemplateObject)
1.94      vatton    421:                     {
1.109     vatton    422:                       // not already instantiated
                    423:                       TtaDeleteTree (child, doc);
                    424:                       child = NULL;
1.94      vatton    425:                     }
                    426:                   else
                    427:                     {
1.109     vatton    428:                       attType.AttrTypeNum = Template_ATTR_currentType;
                    429:                       att = TtaGetAttribute (el, attType);
                    430:                       if (att == NULL)
                    431:                         {
                    432:                           att = TtaNewAttribute (attType);
                    433:                           TtaAttachAttribute (el, att, doc);
                    434:                         }
                    435:                       if (otherType.ElTypeNum == 1)
                    436:                         TtaSetAttributeText (att, "string", el, doc);
                    437:                       else
                    438:                         {
                    439:                           name = (char *)GetXMLElementName (otherType, doc);
                    440:                           if (name && strcmp (name,"???"))
                    441:                             TtaSetAttributeText (att, name, el, doc);
                    442:                         }
1.94      vatton    443:                     }
1.92      vatton    444:                 }
1.87      vatton    445:             }
1.94      vatton    446:           if (child == NULL)
1.114     vatton    447:             {
                    448:               option = GetAttributeIntValueFromNum (el, Template_ATTR_option);
1.115     vatton    449:               opt = (option == 0 || option == Template_ATTR_option_VAL_option_set);
                    450:               if (opt)
                    451:                 InstantiateUse (t, el, doc, parentLine, FALSE);
1.114     vatton    452:             }
1.94      vatton    453:           else
                    454:             {
                    455:               Template_FixAccessRight (t, el, doc);
                    456:               TtaUpdateAccessRightInViews (doc, el);
                    457:             }
1.109     vatton    458:           TtaFreeMemory (types);
1.80      kia       459:           break;
                    460:         case Template_EL_attribute :
                    461:           if (!loading)
                    462:             InstantiateAttribute (t, el, doc);
                    463:           break;
                    464:         case Template_EL_repeat :
1.104     vatton    465:           InstantiateRepeat (t, el, doc, parentLine, FALSE);
1.80      kia       466:           break;
                    467:         default :
                    468:           break;
                    469:         }
                    470:     }
1.92      vatton    471:   else if (!strcmp (name, "HTML") &&
                    472:            (elType.ElTypeNum == HTML_EL_Pseudo_paragraph ||
                    473:             elType.ElTypeNum == HTML_EL_Paragraph))
                    474:     parentLine = el;
1.80      kia       475: 
                    476:   child = TtaGetFirstChild (el);
                    477:   while (child)
                    478:     {
1.92      vatton    479:       next = child;
                    480:       TtaNextSibling (&next);
                    481:       parentLine = ParseTemplate (t, child, doc, parentLine, loading);
                    482:       child = next;
1.80      kia       483:     }
1.92      vatton    484:   return savedInline;
1.80      kia       485: #endif /* TEMPLATES */
                    486: }
                    487: 
1.63      kia       488: /*----------------------------------------------------------------------
                    489:   CreateTemplate
                    490:   Create a template from any document.
                    491:   ----------------------------------------------------------------------*/
1.89      vatton    492: void CreateTemplate (Document doc, char *templatePath)
1.63      kia       493: {
                    494: #ifdef TEMPLATES
                    495:   Element           root, head, elem, xt, title, child, last;
                    496:   ElementType       elType, xtType;
1.64      kia       497:   char             *s;
1.63      kia       498:   SSchema           templSchema;
                    499:   XTigerTemplate    t;
                    500:   
1.89      vatton    501:   if (IsTemplateInstanceDocument(doc))
1.63      kia       502:     {
                    503:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_INSTANCE),
                    504:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    505:       return;
                    506:     }
                    507: 
1.89      vatton    508:   if (IsTemplateDocument(doc))
1.63      kia       509:     {
                    510:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_TEMPLATE),
                    511:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    512:       return;
                    513:     }
                    514:   
1.64      kia       515:   root = TtaGetRootElement(doc);
                    516:   elType = TtaGetElementType (root);
                    517:   // get the target document type
                    518:   s = TtaGetSSchemaName (elType.ElSSchema);
1.63      kia       519:   
1.64      kia       520:   TtaNewNature (doc, elType.ElSSchema,  NULL, "Template", "TemplateP");
1.96      vatton    521:   TtaSetANamespaceDeclaration (doc, root, "xt", Template_URI);
                    522:   templSchema = TtaGetSSchema ("Template", doc);
                    523:   TtaSetUriSSchema (templSchema, Template_URI);
1.64      kia       524: 
                    525:   // Insert xt:head and others
                    526:   TtaSetStructureChecking (FALSE, doc);
                    527:   if (strcmp (s, "HTML") == 0)
1.63      kia       528:     {
1.64      kia       529:       // Initialize the xt:head
                    530:       elType.ElTypeNum = HTML_EL_HEAD;
1.101     vatton    531:       xtType.ElSSchema = templSchema;
1.64      kia       532:       head = TtaSearchTypedElement (elType, SearchInTree, root);
                    533:       if(head)
                    534:         {
                    535:           xtType.ElTypeNum = Template_EL_head;
                    536:           xt = TtaNewElement(doc, xtType);
                    537:           elem = TtaGetLastChild(head);
                    538:           if(elem)
                    539:               TtaInsertSibling(xt, elem, FALSE, doc);
                    540:           else
                    541:               TtaInsertFirstChild(&xt, head, doc);
                    542:           
                    543:           SetAttributeStringValue(xt, Template_ATTR_version, Template_Current_Version);
                    544:           SetAttributeStringValue(xt, Template_ATTR_templateVersion, "1.0");
                    545:         }
1.63      kia       546:       
1.64      kia       547:       // Initialize the document title
                    548:       elType.ElTypeNum = HTML_EL_TITLE;
                    549:       title = TtaSearchTypedElement (elType, SearchInTree, root);
1.96      vatton    550:       if (title)
1.63      kia       551:         {
1.64      kia       552:           // Create xt:use for title
                    553:           xtType.ElTypeNum = Template_EL_useSimple;
1.96      vatton    554:           xt = TtaNewElement (doc, xtType);
1.64      kia       555:           TtaInsertFirstChild(&xt, title, doc);
1.96      vatton    556:           SetAttributeStringValue (xt, Template_ATTR_types, "string");
                    557:           SetAttributeStringValue (xt, Template_ATTR_title, "title");
1.64      kia       558:           
                    559:           // Move current title content to xt:use
                    560:           last = NULL;
                    561:           while(child = TtaGetLastChild(title), child!=NULL)
1.63      kia       562:             {
1.96      vatton    563:               if (child == xt)
1.64      kia       564:                 break;
1.96      vatton    565:               TtaRemoveTree (child, doc);
                    566:               if (last)
                    567:                 TtaInsertSibling (child, last, FALSE, doc);
1.63      kia       568:               else
1.96      vatton    569:                 TtaInsertFirstChild (&child, xt, doc);
1.64      kia       570:               last = child;
1.63      kia       571:             }
                    572:         }
1.64      kia       573:     }
                    574:   else
                    575:     {
                    576:       xtType.ElSSchema = templSchema;
                    577:       xtType.ElTypeNum = Template_EL_head;
1.96      vatton    578:       xt = TtaNewElement (doc, xtType);
                    579:       TtaInsertFirstChild (&xt, root, doc);
                    580:       SetAttributeStringValue (xt, Template_ATTR_version, Template_Current_Version);
                    581:       SetAttributeStringValue (xt, Template_ATTR_templateVersion, "1.0");      
1.64      kia       582:     }
                    583:   // Save changes
                    584:   TtaSetStructureChecking (TRUE, doc);
1.86      vatton    585:   if (DocumentTypes[doc] == docHTML)
                    586:     // avoid positionned boxes to overlap the xt:head section
                    587:     SetBodyAbsolutePosition (doc);
                    588: 
1.64      kia       589:   TtaClearUndoHistory (doc);
                    590:   RemoveParsingErrors (doc);
1.63      kia       591: 
1.64      kia       592:   TtaFreeMemory(DocumentURLs[doc]);
                    593:   DocumentURLs[doc] = TtaStrdup(templatePath);
                    594:   
                    595:   if(DocumentMeta[doc]==NULL)
                    596:     DocumentMeta[doc] = DocumentMetaDataAlloc();
                    597:   
                    598:   DocumentMeta[doc]->method = CE_TEMPLATE;
                    599:   if(DocumentMeta[doc]->initial_url)
                    600:     {
                    601:       TtaFreeMemory(DocumentMeta[doc]->initial_url);
                    602:       DocumentMeta[doc]->initial_url = NULL;
                    603:     }
                    604:   TtaSetDocumentModified (doc);
1.63      kia       605: 
1.64      kia       606:   // Load template-related infos :
                    607:   // like LoadTemplate(..)
                    608:   t = LookForXTigerTemplate(templatePath);
                    609:   t->doc = doc;
1.89      vatton    610:   Template_PrepareTemplate(t, doc);
1.67      kia       611:   //  DocumentTypes[doc] = docTemplate;
                    612:   t->state |= templloaded|templTemplate;
1.63      kia       613: 
1.98      vatton    614: #ifdef TEMPLATE_DEBUG  
1.64      kia       615:     DumpAllDeclarations();
1.98      vatton    616: #endif /* TEMPLATE_DEBUG */
1.64      kia       617:     
                    618:   /* Update the URL combo box */
                    619:   AddURLInCombobox (DocumentURLs[doc], NULL, FALSE);
                    620:   TtaSetTextZone (doc, 1, URL_list);
                    621:   /* Update template menus */
                    622:   UpdateTemplateMenus(doc);
                    623: 
1.63      kia       624: #endif /* TEMPLATES */  
                    625: }
                    626: 
1.1       vatton    627: /*----------------------------------------------------------------------
                    628:   CreateInstance
1.41      vatton    629:   basedoc is the displayed doc that launchs the creation of instance
1.3       vatton    630:   ----------------------------------------------------------------------*/
1.99      vatton    631: void CreateInstance (char *templatePath, char *instancePath,
                    632:                      char *docname,  DocumentType docType, int basedoc)
1.3       vatton    633: {
1.1       vatton    634: #ifdef TEMPLATES
1.41      vatton    635:   Document          doc = 0, newdoc = 0;
1.90      vatton    636:   Element           root, title, text;
1.41      vatton    637:   ElementType       elType;
1.90      vatton    638:   CHARSET           charset, ocharset;
                    639:   char             *s, *charsetname, *ocharsetname, *localFile;
1.1       vatton    640: 
1.35      kia       641:   XTigerTemplate t = GetXTigerTemplate(templatePath);
1.1       vatton    642:   if (t == NULL)
1.29      vatton    643:     {
                    644:       // the template cannot be loaded
                    645:       InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                    646:       return;
                    647:     }
1.41      vatton    648:   // the template document
1.5       vatton    649:   doc = GetTemplateDocument (t);
1.41      vatton    650:   // localize the new created document
                    651:   if (DontReplaceOldDoc)
                    652:     newdoc = TtaGetNextDocumentIndex ();
                    653:   else
                    654:     newdoc = basedoc;
1.90      vatton    655:   
                    656:   // close current undo sepquence in the template document
                    657:   if (TtaHasUndoSequence (doc))
                    658:     TtaCloseUndoSequence (doc);
                    659: 
                    660:   // update the charset if needed
                    661:   charsetname = TtaGetEnvString ("DOCUMENT_CHARSET");
                    662:   charset = TtaGetCharset (charsetname);
                    663:   ocharsetname = DocumentMeta[doc]->charset;
                    664:   ocharset =  TtaGetCharset (ocharsetname);
                    665:   if (charset != UNDEFINED_CHARSET &&
                    666:       DocumentMeta[doc]->charset &&
                    667:       strcmp (charsetname, DocumentMeta[doc]->charset))
                    668:     {
                    669:       TtaSetDocumentCharset (doc, charset, FALSE);
                    670:       DocumentMeta[doc]->charset = TtaStrdup (charsetname);
                    671:       SetNamespacesAndDTD (doc, FALSE);
                    672:     }
1.54      vatton    673: 
1.72      vatton    674:   // register the document type to open the right page model
                    675:   DocumentTypes[newdoc] = docType;
1.90      vatton    676:   // Generate the instance content as a copy of the template
                    677:   localFile = SaveDocumentToNewDoc(doc, newdoc, instancePath);
                    678:   Template_PrepareInstance (instancePath, newdoc, t->version, templatePath);
                    679:   Template_AddReference (t);
                    680: 
                    681:   // Revert template changes
                    682:   TtaSetDocumentCharset (doc, ocharset, FALSE);
1.111     vatton    683:   TtaFreeMemory (DocumentMeta[doc]->charset);
1.90      vatton    684:   DocumentMeta[doc]->charset = ocharsetname;
                    685:   // Now parse the instance
                    686:   // The xtiger PI will be added and components will be removed
                    687:   GetAmayaDoc (instancePath, NULL, basedoc, basedoc, CE_INSTANCE,
                    688:                !DontReplaceOldDoc, NULL, NULL);
                    689:   if (DocumentMeta[newdoc])
                    690:     DocumentMeta[newdoc]->method = CE_ABSOLUTE;
                    691:   // Generate the HTML document title
                    692:   root = TtaGetRootElement(newdoc);
                    693:   elType = TtaGetElementType (root);
                    694:   // get the target document type
                    695:   s = TtaGetSSchemaName (elType.ElSSchema);
                    696:   if (strcmp (s, "HTML") == 0)
1.1       vatton    697:     {
1.90      vatton    698:       // Initialize the document title
                    699:       elType.ElTypeNum = HTML_EL_TITLE;
                    700:       title = TtaSearchTypedElement (elType, SearchInTree, root);
                    701:       text = TtaGetFirstChild (title);
                    702:       while (text)
1.1       vatton    703:         {
1.90      vatton    704:           elType = TtaGetElementType (text);
                    705:           if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
1.14      vatton    706:             {
1.90      vatton    707:               TtaSetTextContent (text, (unsigned char*)Answer_text,
                    708:                                  TtaGetDefaultLanguage (), newdoc);
                    709:               text = NULL;
                    710:               SetNewTitle (newdoc);
1.14      vatton    711:             }
1.90      vatton    712:           else if ((elType.ElTypeNum == Template_EL_useEl ||
                    713:                     elType.ElTypeNum == Template_EL_useSimple) &&
                    714:                    !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                    715:             // Ignore the template use element
                    716:             text = TtaGetFirstChild (text);
                    717:           else
                    718:             // Look for the first text child
                    719:             TtaNextSibling (&text);
1.63      kia       720:         }
1.90      vatton    721:     }
1.54      vatton    722: 
1.90      vatton    723:   // Insert XTiger PI
                    724:   Template_InsertXTigerPI(newdoc, t);   
                    725:   // Parse template to fill structure and remove extra data
1.92      vatton    726:   ParseTemplate (t, root, newdoc, NULL, FALSE);
1.90      vatton    727:   TtaFreeMemory (localFile);
                    728:   TtaClearUndoHistory (newdoc);
                    729:   RemoveParsingErrors (newdoc);
                    730:   TtaSetDocumentModified (newdoc);
                    731:   UpdateTemplateMenus(newdoc);
1.1       vatton    732: #endif /* TEMPLATES */
                    733: }
                    734: 
                    735: 
                    736: #ifdef TEMPLATES
                    737: /*----------------------------------------------------------------------
                    738:   ProcessAttr
1.9       vatton    739:   Look for all "attribute" elements in the subtree and instantiate them
1.3       vatton    740:   ----------------------------------------------------------------------*/
1.1       vatton    741: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
                    742: {
                    743:   Element      child;
                    744:   ElementType  elType;
                    745: 
                    746:   for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
                    747:     {
                    748:       elType = TtaGetElementType (child);
                    749:       if (elType.ElTypeNum == Template_EL_attribute &&
                    750:           !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
                    751:         InstantiateAttribute (t, child, doc);
                    752:       else
                    753:         ProcessAttr (t, child, doc);
                    754:     }
                    755: }
                    756: #endif /* TEMPLATES */
                    757: 
1.12      kia       758: 
                    759: /*----------------------------------------------------------------------
                    760:   Template_GetNewSimpleTypeInstance
1.109     vatton    761:   Create an new instance of xt:use/SimpleType for the document doc.
                    762:   Return the new element
1.12      kia       763:   ----------------------------------------------------------------------*/
1.109     vatton    764: Element Template_GetNewSimpleTypeInstance(Document doc)
1.12      kia       765: {
                    766:   Element           newEl = NULL;
                    767: #ifdef TEMPLATES
                    768:   ElementType       elType;
1.73      kia       769:   const char       *empty = " ";
1.24      vatton    770: 
1.38      vatton    771:   elType.ElSSchema = TtaGetSSchema("Template", doc);
1.12      kia       772:   elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    773:   newEl = TtaNewElement (doc, elType);
                    774:   TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
1.24      vatton    775: #endif /* TEMPLATES */
1.12      kia       776:   return newEl;
                    777: }
                    778: 
                    779: /*----------------------------------------------------------------------
                    780:   Template_GetNewXmlElementInstance
1.109     vatton    781:   Create an new instance of xt:use/XmlElement for the document doc.
                    782:   The parameter decl gives the type of the element of new element.
                    783:   Return the new element
1.12      kia       784:   ----------------------------------------------------------------------*/
1.109     vatton    785: Element Template_GetNewXmlElementInstance(Document doc, Declaration decl)
1.12      kia       786: {
                    787:   Element           newEl = NULL;
                    788: #ifdef TEMPLATES
                    789:   ElementType       elType;
                    790: 
1.24      vatton    791:   GIType (decl->name, &elType, doc);
                    792:   if (elType.ElTypeNum != 0)
1.23      kia       793:     newEl = TtaNewTree (doc, elType, "");
1.12      kia       794: #endif /* TEMPLATES */
                    795:   return newEl;
                    796: }
                    797: 
1.34      vatton    798: 
                    799: /*----------------------------------------------------------------------
                    800:   InsertWithNotify applies pre and post functions when inserting the new
                    801:   element el after child (if not NULL) or as first child of parent.
                    802:   ----------------------------------------------------------------------*/
                    803: Element InsertWithNotify (Element el, Element child, Element parent, Document doc)
                    804: {
                    805:   ElementType      elType;
1.116     vatton    806:   AttributeType    attrType;
                    807:   Attribute        attr;
1.34      vatton    808:   NotifyElement    event;
                    809:   char            *name;
                    810:   ThotBool         isRow = FALSE, isCell = FALSE;
1.50      vatton    811:   ThotBool         isImage = FALSE;
                    812:   ThotBool         oldStructureChecking;
                    813: 
                    814:   // avoid to check attributes now
                    815:   oldStructureChecking = TtaGetStructureChecking (doc);
                    816:   TtaSetStructureChecking (FALSE, doc);
1.34      vatton    817: 
                    818:   elType = TtaGetElementType (el);
                    819:   name = TtaGetSSchemaName (elType.ElSSchema);
                    820:   isCell = ((!strcmp (name,"HTML") &&
                    821:              elType.ElTypeNum == HTML_EL_Data_cell ||
                    822:              elType.ElTypeNum == HTML_EL_Heading_cell) ||
                    823:             (!strcmp (name,"MathML") && elType.ElTypeNum == MathML_EL_MTD));
                    824:   isRow = ((!strcmp (name,"HTML") && elType.ElTypeNum == HTML_EL_Table_row) ||
                    825:            (!strcmp (name,"MathML") &&
                    826:             (elType.ElTypeNum == MathML_EL_MTR ||
                    827:              elType.ElTypeNum == MathML_EL_MLABELEDTR)));
1.50      vatton    828:   isImage = (!strcmp (name,"HTML") && 
                    829:               (elType.ElTypeNum == HTML_EL_IMG || elType.ElTypeNum == HTML_EL_Object));
1.34      vatton    830:   if (child)
                    831:     TtaInsertSibling (el, child, FALSE, doc);
                    832:   else
                    833:     TtaInsertFirstChild (&el, parent, doc);
1.50      vatton    834:   TtaSetStructureChecking (oldStructureChecking, doc);
1.34      vatton    835: 
1.50      vatton    836:   if (isImage)
1.116     vatton    837:     {
                    838:       // check if the src attribute is there
                    839:       attrType.AttrSSchema = elType.ElSSchema;
                    840:       if (elType.ElTypeNum == HTML_EL_IMG)
                    841:         attrType.AttrTypeNum = HTML_ATTR_SRC;
                    842:       else
                    843:          attrType.AttrTypeNum = HTML_ATTR_data;
                    844:       attr = TtaGetAttribute (el, attrType);
                    845:       if (attr == NULL)
                    846:         InsertImageOrObject (el, doc);
                    847:     }
1.50      vatton    848:   else if (isCell)
1.34      vatton    849:     {
                    850:       // a cell is created
1.39      quint     851:       NewCell (el, doc, TRUE, TRUE, TRUE);
1.34      vatton    852:     }
                    853:   else if (isRow)
                    854:     {
                    855:       // a row is created
                    856:       event.element = el;
                    857:       event.document = doc;
                    858:       RowPasted (&event);
                    859:     }
1.50      vatton    860:   
                    861:   if (!strcmp (name,"HTML"))
                    862:     {
1.104     vatton    863:       // special management for images and objets
1.50      vatton    864:       elType.ElTypeNum = HTML_EL_IMG;
                    865:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    866:       while (child)
                    867:         {
                    868:           InsertImageOrObject (child, doc);
                    869:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    870:         }
                    871:       elType.ElTypeNum = HTML_EL_Object;
                    872:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    873:       while (child)
                    874:         {
                    875:           InsertImageOrObject (child, doc);
                    876:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    877:         }
                    878:     }
1.34      vatton    879:   return el;
                    880: }
                    881: 
                    882: 
1.12      kia       883: /*----------------------------------------------------------------------
1.118     vatton    884:   Template_FillEmpty generates an empty contents
                    885:   ----------------------------------------------------------------------*/
                    886: Element Template_FillEmpty (Element el, Document doc)
                    887: {
                    888: #ifdef TEMPLATES
                    889:   Element         child;
                    890:   ElementType     elType;
                    891: 
                    892:   // generate a content
                    893:   child = TtaGetFirstChild (el);
                    894:   if (child == NULL)
                    895:     {
                    896:       elType = TtaGetElementType (el);
                    897:       elType.ElTypeNum = Template_EL_TemplateObject;
                    898:       child = TtaNewElement (doc, elType);
                    899:       TtaInsertFirstChild (&child, el, doc);
                    900:     }
                    901:   return child;
                    902: #endif /* TEMPLATES */
                    903:   return NULL;
                    904: }
                    905: 
                    906: /*----------------------------------------------------------------------
1.16      kia       907:   Template_InsertUseChildren
                    908:   Insert children to a xt:use
                    909:   The dec parameter must be valid and will not be verified. It must be a
                    910:     direct child element (for union elements).
                    911:   @param el element (xt:use) in which insert a new element
                    912:   @param dec Template declaration of the element to insert
                    913:   @return The inserted element (the xt:use element if insertion is multiple as component)
1.104     vatton    914:   The parentLine parameter points to the enclosing line if any.
1.16      kia       915:   ----------------------------------------------------------------------*/
1.104     vatton    916: Element Template_InsertUseChildren (Document doc, Element el, Declaration dec,
                    917:                                     Element parentLine, ThotBool registerUndo)
1.16      kia       918: {
1.99      vatton    919:   Element         newEl = NULL;
1.16      kia       920: #ifdef TEMPLATES
1.99      vatton    921:   Element         current = NULL;
1.104     vatton    922:   Element         child = NULL, prev, next;
                    923:   ElementType     childType, elType; 
                    924:   SSchema         sshtml;
1.99      vatton    925:   XTigerTemplate  t;
1.17      kia       926:   
1.25      vatton    927:   if (TtaGetDocumentAccessMode(doc))
1.16      kia       928:   {
1.23      kia       929:     switch (dec->nature)
                    930:     {
                    931:       case SimpleTypeNat:
1.109     vatton    932:         newEl = Template_GetNewSimpleTypeInstance(doc);
1.34      vatton    933:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       934:         break;
                    935:       case XmlElementNat:
1.109     vatton    936:         newEl = Template_GetNewXmlElementInstance(doc, dec);
1.34      vatton    937:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       938:         break;
                    939:       case ComponentNat:
                    940:         newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
1.74      kia       941:         ProcessAttr (dec->usedIn, newEl, doc);
1.104     vatton    942:         elType = TtaGetElementType (el);
1.23      kia       943:         /* Copy elements from new use to existing use. */
1.98      vatton    944: #ifdef TEMPLATE_DEBUG
1.74      kia       945:         DumpSubtree(newEl, doc, 0);
1.98      vatton    946: #endif /* TEMPLATE_DEBUG */
1.104     vatton    947:         sshtml = TtaGetSSchema ("HTML", doc);
1.102     vatton    948:         t = GetXTigerDocTemplate( doc);
                    949:         child = TtaGetFirstChild  (newEl);
1.76      vatton    950:         while (child)
                    951:           {
                    952:             // move the new subtree to the document
                    953:             TtaRemoveTree (child, doc);
1.104     vatton    954:             childType = TtaGetElementType (child);
                    955:             if (parentLine)
                    956:               {
                    957:                 if (childType.ElSSchema == sshtml &&
                    958:                     childType.ElTypeNum == HTML_EL_Pseudo_paragraph)
                    959:                   {
                    960:                     prev = TtaGetFirstChild  (child);
                    961:                     while (prev)
                    962:                       {
                    963:                         next = prev;
                    964:                         TtaNextSibling (&next);
                    965:                         TtaRemoveTree (prev, doc);
                    966:                         current = InsertWithNotify (prev, current, el, doc);
                    967:                         prev = next;
                    968:                       }
1.108     vatton    969:                     TtaDeleteTree (child, doc);
1.104     vatton    970:                   }
1.108     vatton    971:                 else
                    972:                   current = InsertWithNotify (child, current, el, doc);
1.104     vatton    973:               }
                    974:             else
                    975:               {
                    976:                 current = InsertWithNotify (child, current, el, doc);
                    977:                 // check if a new paragraph is inserted
                    978:                 if (childType.ElSSchema == sshtml &&
                    979:                     childType.ElTypeNum == HTML_EL_Paragraph)
                    980:                   Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
                    981:                 else
                    982:                   {
                    983:                     childType.ElSSchema = sshtml;
                    984:                     childType.ElTypeNum = HTML_EL_Paragraph;
                    985:                     child = TtaSearchTypedElement (childType, SearchInTree, current);
                    986:                     while (child)
                    987:                       {
                    988:                         Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
                    989:                         child = TtaSearchTypedElement (childType, SearchInTree, child);
                    990:                       }
                    991:                   }
                    992:               }
1.99      vatton    993:             child = TtaGetFirstChild (newEl);
1.76      vatton    994:           }
1.99      vatton    995: 
1.102     vatton    996:         TtaDeleteTree (newEl, doc);
1.23      kia       997:         newEl = el;
                    998:         break;
                    999:       default :
                   1000:         //Impossible
                   1001:         break;   
                   1002:     }
1.44      kia      1003:     Template_FixAccessRight (dec->usedIn, el, doc);
1.105     vatton   1004:     if (dec->nature == ComponentNat)
                   1005:       Component_FixAccessRight (el, doc);
1.102     vatton   1006:     TtaUpdateAccessRightInViews (doc, el);
1.23      kia      1007:   }  
1.16      kia      1008: #endif /* TEMPLATES */
                   1009:   return newEl;
                   1010: }
                   1011: 
1.40      kia      1012: 
                   1013: /*----------------------------------------------------------------------
1.105     vatton   1014:   Component_FixAccessRight locks children of the component
                   1015:   ----------------------------------------------------------------------*/
                   1016: void Component_FixAccessRight (Element el, Document doc)
                   1017: {
                   1018: #ifdef TEMPLATES
                   1019:   Element     child;
                   1020:   
                   1021:   if (el && doc)
                   1022:     {
                   1023:       TtaSetAccessRight (el, ReadOnly, doc);
                   1024:       // fix access right to children
                   1025:       child = TtaGetFirstChild (el);
                   1026:       while (child)
                   1027:         {
                   1028:           TtaSetAccessRight (child, ReadOnly, doc);
                   1029:           TtaNextSibling (&child);
                   1030:         }
                   1031:     }
                   1032: #endif /* TEMPLATES */
                   1033: }
                   1034: 
                   1035: /*----------------------------------------------------------------------
1.97      vatton   1036:   Template_FixAccessRight fixes access rights of the el element
1.40      kia      1037:   ----------------------------------------------------------------------*/
1.41      vatton   1038: void Template_FixAccessRight (XTigerTemplate t, Element el, Document doc)
1.40      kia      1039: {
                   1040: #ifdef TEMPLATES
                   1041:   ElementType elType;
                   1042:   Element     child;
1.97      vatton   1043:   Declaration decl;
1.88      vatton   1044:   char        currentType[MAX_LENGTH], *ptr;
1.40      kia      1045:   
                   1046:   if (t && el && doc)
                   1047:     {
                   1048:       elType = TtaGetElementType(el);
1.41      vatton   1049:       if (elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.40      kia      1050:         {
1.41      vatton   1051:           switch (elType.ElTypeNum)
1.40      kia      1052:             {
                   1053:             case Template_EL_TEXT_UNIT:
1.41      vatton   1054:               //TtaSetAccessRight( el, ReadWrite, doc);
                   1055:               return;
1.105     vatton   1056:             case Template_EL_component:
                   1057:               Component_FixAccessRight (el, doc);
                   1058:               break;
1.40      kia      1059:             case Template_EL_useEl:
                   1060:             case Template_EL_useSimple:
                   1061:               GiveAttributeStringValueFromNum(el, Template_ATTR_currentType,
                   1062:                                               (char*)currentType, NULL);
1.88      vatton   1063:               if (currentType[0] == EOS)
                   1064:                 {
                   1065:                   GiveAttributeStringValueFromNum(el, Template_ATTR_types,
                   1066:                                                   (char*)currentType, NULL);
                   1067:                   ptr = strstr (currentType, " ");
                   1068:                   if (ptr)
                   1069:                     *ptr = EOS;
                   1070:                 }
1.40      kia      1071:               decl = Template_GetDeclaration(t, currentType);
                   1072:               if (decl)
                   1073:                 {
                   1074:                   switch (decl->nature)
                   1075:                     {
                   1076:                       case SimpleTypeNat:
1.41      vatton   1077:                         TtaSetAccessRight (el, ReadWrite, doc);
                   1078:                         return;
1.105     vatton   1079:                       case ComponentNat:
1.107     vatton   1080:                         TtaSetAccessRight (el, ReadOnly, doc);
                   1081:                          break;
                   1082:                         //Component_FixAccessRight (el, doc);
                   1083:                         //return;
1.97      vatton   1084:                       case XmlElementNat:
1.105     vatton   1085:                         if (TtaIsSetReadOnly (el))
                   1086:                           break;
1.97      vatton   1087:                         child = TtaGetFirstChild (el);
                   1088:                         if (child)
                   1089:                           TtaSetAccessRight (child, ReadWrite, doc);
1.40      kia      1090:                       default:
1.41      vatton   1091:                         TtaSetAccessRight (el, ReadOnly, doc);
                   1092:                          break;
1.40      kia      1093:                     }
                   1094:                 }
                   1095:               break;
                   1096:             case Template_EL_bag:
1.45      vatton   1097:             case Template_EL_repeat:
1.40      kia      1098:               TtaSetAccessRight(el, ReadWrite, doc);
                   1099:               break;
                   1100:             default:
                   1101:               TtaSetAccessRight(el, ReadOnly, doc);
                   1102:               break;
                   1103:             }
                   1104:         }
                   1105: 
1.92      vatton   1106:       // fix access right to children
1.41      vatton   1107:       child = TtaGetFirstChild (el);
                   1108:       while (child)
1.40      kia      1109:         {
1.41      vatton   1110:           Template_FixAccessRight (t, child, doc);
                   1111:           TtaNextSibling (&child);
1.40      kia      1112:         }
                   1113:     }
                   1114: #endif /* TEMPLATES */
                   1115: }
                   1116: 
1.16      kia      1117: /*----------------------------------------------------------------------
1.46      vatton   1118:   AddPromptIndicator
                   1119:   ----------------------------------------------------------------------*/
                   1120: void AddPromptIndicator (Element el, Document doc)
                   1121: {
                   1122: #ifdef TEMPLATES
                   1123:   ElementType         elType;
                   1124:   AttributeType       attrType;
                   1125:   Attribute           att;
                   1126: 
1.66      vatton   1127:   if (el)
                   1128:     {
                   1129:       elType = TtaGetElementType (el);
                   1130:       attrType.AttrSSchema = elType.ElSSchema;
                   1131:       attrType.AttrTypeNum = Template_ATTR_prompt;
1.69      vatton   1132:       att = TtaGetAttribute (el, attrType);
                   1133:       if (att == NULL)
                   1134:         {
                   1135:           att = TtaNewAttribute (attrType);
                   1136:           TtaAttachAttribute (el, att, doc);
                   1137:         }
1.66      vatton   1138:     }
1.46      vatton   1139: #endif /* TEMPLATES */
                   1140: }
                   1141: 
                   1142: /*----------------------------------------------------------------------
1.104     vatton   1143:   Template_SetInline manages inline elements
                   1144:   registerUndo says if changes must be registered
                   1145:   ----------------------------------------------------------------------*/
                   1146: void Template_SetInline (Element el, SSchema sstempl, Document doc, ThotBool registerUndo)
                   1147: {
                   1148: #ifdef TEMPLATES
                   1149:   Element         child = NULL;
                   1150:   ElementType     elType1, elType2, elType3;
                   1151: 
                   1152:   if (el)
                   1153:     {
                   1154:       elType1 = TtaGetElementType (el);
                   1155:       if (elType1.ElSSchema == sstempl)
                   1156:         // apply to hte current template element
                   1157:         SetAttributeIntValue (el, Template_ATTR_SetInLine,
                   1158:                               Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
                   1159:       else
                   1160:         elType1.ElSSchema = sstempl;
                   1161:       elType1.ElTypeNum = Template_EL_useSimple;
                   1162:       elType2.ElTypeNum = Template_EL_useEl;
                   1163:       elType2.ElSSchema = elType1.ElSSchema;
                   1164:       elType3.ElTypeNum = Template_EL_repeat;
                   1165:       elType3.ElSSchema = elType1.ElSSchema;
                   1166:       child = TtaSearchElementAmong5Types (elType1, elType2, elType3, elType3, elType3,
                   1167:                                            SearchForward, el);
                   1168:       while (child && TtaIsAncestor (child, el))
                   1169:         {
                   1170:           SetAttributeIntValue (child, Template_ATTR_SetInLine,
                   1171:                                 Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
                   1172:           child = TtaSearchElementAmong5Types (elType1, elType2,
                   1173:                                                elType3, elType3, elType3,
                   1174:                                                SearchForward, child);
                   1175:         }
                   1176:     }
                   1177: #endif /* TEMPLATES */
                   1178: }
                   1179: 
                   1180: /*----------------------------------------------------------------------
                   1181:   InstantiateUse intantiate the use element el.
                   1182:   The parentLine parameter points to the enclosing line if any.
1.3       vatton   1183:   ----------------------------------------------------------------------*/
1.1       vatton   1184: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.104     vatton   1185:                         Element parentLine, ThotBool registerUndo)
1.1       vatton   1186: {
                   1187: #ifdef TEMPLATES
1.109     vatton   1188:   Element          child = NULL;
1.1       vatton   1189:   ElementType      elType;
                   1190:   Declaration      dec;
1.109     vatton   1191:   int              size, nbitems, i, option;
1.1       vatton   1192:   struct menuType  *items;
1.104     vatton   1193:   char             *types;
1.109     vatton   1194:   ThotBool          oldStructureChecking, hidden;
1.1       vatton   1195: 
1.25      vatton   1196:   if (!t)
1.23      kia      1197:     return NULL;
                   1198: 
1.1       vatton   1199:   /* get the value of the "types" attribute */
1.12      kia      1200:   elType = TtaGetElementType (el);
1.32      vatton   1201:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36      vatton   1202:   if (!types || types[0] == EOS)
                   1203:     {
                   1204:       TtaFreeMemory (types);
                   1205:       return NULL;
                   1206:     }
1.104     vatton   1207:   if (!strcmp (types, "string") || !strcmp (types, "number"))
1.109     vatton   1208:     {
                   1209:       child = TtaGetFirstChild (el);
                   1210:       if (child == NULL)
                   1211:         {
                   1212:           child = Template_GetNewSimpleTypeInstance(doc);
                   1213:           child = InsertWithNotify (child, NULL, el, doc);
                   1214:         }
                   1215:       AddPromptIndicator (el, doc);
                   1216:     }
1.104     vatton   1217:   else
1.1       vatton   1218:     {
1.104     vatton   1219:       giveItems (types, size, &items, &nbitems);
                   1220:       // No structure checking
                   1221:       oldStructureChecking = TtaGetStructureChecking (doc);
                   1222:       TtaSetStructureChecking (FALSE, doc);
1.109     vatton   1223:       hidden = ElementIsOptional (el);
                   1224:       if (hidden)
                   1225:         {
                   1226:           // check the current option value
                   1227:           option = GetAttributeIntValueFromNum (el, Template_ATTR_option);
                   1228:           hidden = (option == Template_ATTR_option_VAL_option_unset);
                   1229:         }
                   1230:       if (nbitems == 1 || !hidden)
1.104     vatton   1231:         /* only one type in the "types" attribute */
                   1232:         {
                   1233:           dec = Template_GetDeclaration (t, items[0].label);
                   1234:           if (dec)
1.110     vatton   1235:             child = Template_InsertUseChildren (doc, el, dec, parentLine, registerUndo);
1.112     vatton   1236:           if (nbitems == 1 && elType.ElTypeNum != Template_EL_useSimple)
                   1237:             {
                   1238:               TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
                   1239:               if (registerUndo)
                   1240:                 TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
                   1241:             }
1.104     vatton   1242:         }
                   1243:       else
1.27      kia      1244:         {
1.104     vatton   1245:           // insert almost a pseudo element
                   1246:           elType.ElTypeNum = Template_EL_TemplateObject;
1.109     vatton   1247:           child = TtaNewElement (doc, elType);
                   1248:           TtaInsertFirstChild (&child, el, doc);
1.27      kia      1249:         }
1.104     vatton   1250: 
                   1251:       for (i = 0; i < nbitems; i++)
                   1252:         TtaFreeMemory(items[i].label);
                   1253:       TtaFreeMemory(items);
                   1254: 
                   1255:       if (parentLine)
                   1256:         // display the element in line
                   1257:         Template_SetInline (el, elType.ElSSchema, doc, registerUndo);
                   1258:       TtaSetStructureChecking (oldStructureChecking, doc);
1.1       vatton   1259:     }
1.32      vatton   1260:   TtaFreeMemory (types);
1.44      kia      1261:   Template_FixAccessRight (t, el, doc);
                   1262:   TtaUpdateAccessRightInViews (doc, el);
1.109     vatton   1263:   return child;
1.23      kia      1264: #else /* TEMPLATES */
                   1265:   return NULL;
1.1       vatton   1266: #endif /* TEMPLATES */
                   1267: }
                   1268: 
                   1269: /*----------------------------------------------------------------------
1.22      kia      1270:   InstantiateRepeat
                   1271:   Check for min and max param and validate xt:repeat element content.
                   1272:   @param registerUndo True to register undo creation sequences.
1.104     vatton   1273:   The parentLine parameter points to the enclosing line if any.
1.3       vatton   1274:   ----------------------------------------------------------------------*/
1.46      vatton   1275: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc,
1.104     vatton   1276:                         Element parentLine, ThotBool registerUndo)
1.1       vatton   1277: {
                   1278: #ifdef TEMPLATES
1.32      vatton   1279:   Element        child, newChild;
1.52      vatton   1280:   ElementType    newElType;
                   1281:   Attribute      minAtt,  maxAtt;
                   1282:   AttributeType  minType, maxType;
                   1283:   char          *text, *types = NULL, *title = NULL;
                   1284:   int            curVal, minVal,  maxVal;
1.32      vatton   1285:   int            childrenCount;
                   1286: 
1.25      vatton   1287:   if (!t)
1.23      kia      1288:     return;
                   1289: 
1.1       vatton   1290:   //Preparing types
1.52      vatton   1291:   minType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.1       vatton   1292:   minType.AttrTypeNum = Template_ATTR_minOccurs;
1.52      vatton   1293:   maxType.AttrSSchema =  minType.AttrSSchema;
1.1       vatton   1294:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
1.52      vatton   1295:   newElType.ElSSchema = minType.AttrSSchema;
                   1296:   //Get minOccurs and maxOccurs attributes
1.1       vatton   1297:   minAtt = TtaGetAttribute (el, minType);
                   1298:   maxAtt = TtaGetAttribute (el, maxType);
                   1299:   //Get the values
                   1300:   if (minAtt)
                   1301:     {
1.10      kia      1302:       text = GetAttributeStringValue(el, minAtt, NULL);
1.1       vatton   1303:       if (text)
                   1304:         {
                   1305:           minVal = atoi(text);
                   1306:           TtaFreeMemory(text);
1.52      vatton   1307:           curVal = minVal;
1.1       vatton   1308:         }
                   1309:       else
                   1310:         //Error : Attribute with no value
                   1311:         return;
                   1312:     }
                   1313:   else
1.52      vatton   1314:     {
                   1315:       minVal = 0;
                   1316:       curVal = 1;
                   1317:     }
1.1       vatton   1318: 
                   1319:   if (maxAtt)
                   1320:     {
1.10      kia      1321:       text = GetAttributeStringValue (el, maxAtt, NULL);
1.1       vatton   1322:       if (text)
                   1323:         {
                   1324:           if (!strcmp (text, "*"))
                   1325:             maxVal = INT_MAX;
                   1326:           else
                   1327:             maxVal = atoi (text);
                   1328:           TtaFreeMemory (text);
                   1329:         }
                   1330:       else
                   1331:         //Error : Attribute with no value
                   1332:         return;
                   1333:     }
                   1334:   else
                   1335:     maxVal = INT_MAX;
                   1336: 
                   1337:   text = (char*)TtaGetMemory(MAX_LENGTH);
1.52      vatton   1338:   //Create non existing min max attributes
                   1339:   if (minAtt == NULL)
1.1       vatton   1340:     {      
1.32      vatton   1341:       minAtt = TtaNewAttribute (minType);
                   1342:       sprintf (text, "%d", minVal);
                   1343:       TtaAttachAttribute (el, minAtt, doc);
                   1344:       TtaSetAttributeText (minAtt, text, el, doc);
1.25      vatton   1345:       if (registerUndo)
1.32      vatton   1346:         TtaRegisterAttributeCreate (minAtt, el, doc);
1.1       vatton   1347:     }
                   1348: 
1.52      vatton   1349:   if (maxAtt == NULL)
1.1       vatton   1350:     {  
1.32      vatton   1351:       maxAtt = TtaNewAttribute (maxType);
                   1352:       if (maxVal < INT_MAX)
                   1353:         sprintf(text, "%d", maxVal);
1.1       vatton   1354:       else
1.32      vatton   1355:         sprintf (text, "*");
                   1356:       TtaAttachAttribute (el, maxAtt, doc);      
                   1357:       TtaSetAttributeText (maxAtt, text, el, doc);
1.25      vatton   1358:       if (registerUndo)
1.32      vatton   1359:         TtaRegisterAttributeCreate (maxAtt, el, doc);
1.1       vatton   1360:     }
1.52      vatton   1361:   TtaFreeMemory(text);
1.1       vatton   1362: 
1.52      vatton   1363:   //We must have minOccurs children
1.104     vatton   1364:   child = TtaGetFirstChild (el);
1.1       vatton   1365:   if (!child)
                   1366:     //Error : a repeat must have at least one child which will be the model
                   1367:     return;
                   1368:   
1.104     vatton   1369:   for (childrenCount = 0; child; TtaNextSibling(&child))
                   1370:     //TODO : Check that every child is valid
                   1371:     childrenCount ++;
1.1       vatton   1372: 
                   1373:   if (childrenCount > maxVal)
                   1374:     //Error : too many children!
                   1375:     return;  
                   1376: 
1.104     vatton   1377: 
                   1378:   if (parentLine)
                   1379:     // display the element in line
                   1380:     Template_SetInline (el, minType.AttrSSchema, doc, registerUndo);
                   1381: 
1.1       vatton   1382:   child = TtaGetLastChild(el);
1.32      vatton   1383:   types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
                   1384:   title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
1.52      vatton   1385:   newElType.ElTypeNum = Template_EL_useEl;
1.32      vatton   1386:   while (childrenCount < curVal)
1.1       vatton   1387:     {
1.32      vatton   1388:       newChild = TtaNewElement (doc, newElType);
1.27      kia      1389:       // Insert it
1.32      vatton   1390:       TtaInsertSibling (newChild, child, FALSE, doc);
                   1391:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
                   1392:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.104     vatton   1393:       InstantiateUse (t, newChild, doc, parentLine, TRUE);
1.27      kia      1394:       
1.25      vatton   1395:       if (registerUndo)
1.34      vatton   1396:         TtaRegisterElementCreate (newChild, doc);
1.1       vatton   1397:       child = newChild;
                   1398:       childrenCount++;
                   1399:     }
1.27      kia      1400:     
1.44      kia      1401:   Template_FixAccessRight (t, el, doc);
                   1402:   TtaUpdateAccessRightInViews (doc, el);
1.32      vatton   1403:   TtaFreeMemory (types);
                   1404:   TtaFreeMemory (title);
1.1       vatton   1405: #endif /* TEMPLATES */
                   1406: }
                   1407: 
                   1408: /*----------------------------------------------------------------------
1.80      kia      1409:   Template_InsertXTigerPI
                   1410:   Insert the XTiger PI element in template instance.
                   1411:   Param t is the XTigerTemplate structure of the template,
                   1412:   not the template instance one.
1.3       vatton   1413:   ----------------------------------------------------------------------*/
1.80      kia      1414: void Template_InsertXTigerPI(Document doc, XTigerTemplate t)
1.1       vatton   1415: {
                   1416: #ifdef TEMPLATES
1.47      kia      1417:   ElementType     elType;
1.65      vatton   1418:   Element         root, piElem, doctype, line, text, elNew, elFound;
1.47      kia      1419:   char           *s, *charsetname = NULL, buffer[MAX_LENGTH];
1.1       vatton   1420:   int             pi_type;
                   1421: 
1.80      kia      1422:   if (!t || !doc)
1.23      kia      1423:     return;
1.47      kia      1424: 
1.80      kia      1425:   root =  TtaGetMainRoot (doc);
1.90      vatton   1426:   if (root == NULL)
                   1427:     return;
1.1       vatton   1428:   //Look for PIs
                   1429:   /* check if the document has a DOCTYPE declaration */
                   1430: #ifdef ANNOTATIONS
                   1431:   if (DocumentTypes[doc]  == docAnnot)
                   1432:     elType = TtaGetElementType (root);
                   1433:   else
                   1434: #endif /* ANNOTATIONS */
                   1435:     elType = TtaGetElementType (root);
                   1436:   s = TtaGetSSchemaName (elType.ElSSchema);
                   1437:   if (strcmp (s, "HTML") == 0)
                   1438:     {
                   1439:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                   1440:       pi_type = HTML_EL_XMLPI;
                   1441:     }
                   1442: #ifdef _SVG
                   1443:   else if (strcmp (s, "SVG") == 0)
                   1444:     {
                   1445:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                   1446:       pi_type = SVG_EL_XMLPI;
                   1447:     }
                   1448: #endif /* _SVG */
                   1449:   else if (strcmp (s, "MathML") == 0)
                   1450:     {
                   1451:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                   1452:       pi_type = MathML_EL_XMLPI;
                   1453:     }
                   1454:   else
                   1455:     {
                   1456:       elType.ElTypeNum = XML_EL_doctype;
                   1457:       pi_type = XML_EL_xmlpi;
                   1458:     }
1.54      vatton   1459: 
1.1       vatton   1460:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
1.65      vatton   1461:   if (doctype == NULL)
1.1       vatton   1462:     {
1.65      vatton   1463:       elType.ElTypeNum = pi_type;      
                   1464:       piElem = TtaSearchTypedElement (elType, SearchInTree, root);
                   1465:       if (piElem == NULL)
                   1466:         {
                   1467:           /* generate the XML declaration */
                   1468:           /* Check the Thot abstract tree against the structure schema. */
                   1469:           TtaSetStructureChecking (FALSE, doc);
                   1470:           piElem = TtaNewTree (doc, elType, "");
                   1471:           TtaInsertFirstChild (&piElem, root, doc);
                   1472:           line = TtaGetFirstChild (piElem);
                   1473:           text = TtaGetFirstChild (line);
                   1474:           strcpy (buffer, "xml version=\"1.0\" encoding=\"");
                   1475:           charsetname = UpdateDocumentCharset (doc);
                   1476:           strcat (buffer, charsetname);
                   1477:           strcat (buffer, "\"");
                   1478:           TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                   1479:           TtaSetStructureChecking (TRUE, doc);
1.90      vatton   1480:           TtaFreeMemory (charsetname);
                   1481:           TtaRegisterElementCreate (piElem, doc);
1.65      vatton   1482:         }
1.1       vatton   1483:     }
                   1484:   
                   1485:   /* generate the XTiger PI */
                   1486:   /* Check the Thot abstract tree against the structure schema. */
                   1487:   TtaSetStructureChecking (FALSE, doc);
                   1488:   elType.ElTypeNum = pi_type;
1.65      vatton   1489:   elNew = TtaNewTree (doc, elType, "");
                   1490:   if (doctype)
                   1491:     TtaInsertSibling (elNew, doctype, FALSE, doc);
                   1492:   else
                   1493:     TtaInsertSibling (elNew, piElem, FALSE, doc);
                   1494:   line = TtaGetFirstChild (elNew);
                   1495:   text = TtaGetFirstChild (line);
1.1       vatton   1496:   strcpy (buffer, "xtiger template=\"");
1.100     vatton   1497:   if (t->uri)
                   1498:     strcat (buffer, t->uri);
                   1499:   else if (t->base_uri)
                   1500:     strcat (buffer, t->uri);
1.17      kia      1501:   strcat (buffer, "\" version=\"");
1.20      vatton   1502:   if (t->version)
                   1503:     strcat (buffer, t->version);
                   1504:   else
                   1505:     strcat (buffer, "0.8");
1.1       vatton   1506:   strcat (buffer, "\"");
1.25      vatton   1507:   if (t->templateVersion)
1.20      vatton   1508:     {
                   1509:       strcat (buffer, " templateVersion=\"");
                   1510:       strcat (buffer, t->templateVersion);
                   1511:       strcat (buffer, "\"");
                   1512:     }
1.1       vatton   1513:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
1.90      vatton   1514:   TtaRegisterElementCreate (elNew, doc);
1.1       vatton   1515:   TtaSetStructureChecking (TRUE, doc);
1.5       vatton   1516: 
                   1517:   // update the document title
1.47      kia      1518:   if (!strcmp (s, "HTML"))
1.5       vatton   1519:     {
                   1520:       elType.ElTypeNum = HTML_EL_TITLE;
                   1521:       elFound = TtaSearchTypedElement (elType, SearchInTree, root);
1.90      vatton   1522:       text = TtaGetFirstChild (elFound);
                   1523:       while (text)
1.5       vatton   1524:         {
1.90      vatton   1525:           elType = TtaGetElementType (text);
                   1526:           if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
                   1527:             {
                   1528:               TtaRegisterElementReplace (text, doc);
                   1529:               TtaSetTextContent (text, (unsigned char*)Answer_text,
                   1530:                                  TtaGetDefaultLanguage (), doc);
                   1531:               text = NULL;
                   1532:             }
                   1533:           else if ((elType.ElTypeNum == Template_EL_useEl ||
                   1534:                     elType.ElTypeNum == Template_EL_useSimple) &&
                   1535:                    !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                   1536:             // Ignore the template use element
                   1537:             text = TtaGetFirstChild (text);
                   1538:           else
                   1539:             // Look for the first text child
                   1540:             TtaNextSibling (&text);
1.5       vatton   1541:         }
                   1542:     }
1.1       vatton   1543: #endif /* TEMPLATES */
                   1544: }
                   1545: 
1.80      kia      1546: 
1.1       vatton   1547: /*----------------------------------------------------------------------
1.68      kia      1548:   Template_PreInstantiateComponents
                   1549:   Instantiates all components in order to improve editing.
1.3       vatton   1550:   ----------------------------------------------------------------------*/
1.35      kia      1551: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1       vatton   1552: {
                   1553: #ifdef TEMPLATES 
1.90      vatton   1554:   ForwardIterator iter;
                   1555:   Declaration     dec;
                   1556:   SearchSetNode   node;
                   1557: 
1.25      vatton   1558:   if (!t)
1.23      kia      1559:     return;
                   1560: 
1.90      vatton   1561:   if (Template_IsInstance (t))
                   1562:     {
1.98      vatton   1563: #ifdef TEMPLATE_DEBUG
1.90      vatton   1564:       DumpAllDeclarations();
1.98      vatton   1565: #endif /* TEMPLATE_DEBUG */  
1.90      vatton   1566:       iter = SearchSet_GetForwardIterator(GetComponents(t));
1.98      vatton   1567: #ifdef TEMPLATE_DEBUG
1.90      vatton   1568:       printf("Template_PreInstantiateComponents %s\n", t->uri);
1.98      vatton   1569: #endif /* TEMPLATE_DEBUG */  
1.90      vatton   1570:       ITERATOR_FOREACH(iter, SearchSetNode, node)
                   1571:         {
                   1572:           dec = (Declaration) node->elem;
1.92      vatton   1573:           ParseTemplate(t, GetComponentContent(dec), GetTemplateDocument(t), NULL, TRUE);
1.90      vatton   1574:         }
                   1575:       TtaFreeMemory(iter);
1.1       vatton   1576:     }
                   1577: #endif /* TEMPLATES */
                   1578: }
1.84      kia      1579: 
                   1580: /*----------------------------------------------------------------------
                   1581:   Template_SetName
                   1582:   Set the xt:component or xt:union element xt:name attribute.
                   1583:   Make it unique.
                   1584:   Return TRUE if the name is not modified.
                   1585:   ----------------------------------------------------------------------*/
1.96      vatton   1586: ThotBool Template_SetName (Document doc, Element el, const char *name, ThotBool withUndo)
1.84      kia      1587: {
                   1588: #ifdef TEMPLATES 
1.95      vatton   1589:   AttributeType attType;
                   1590:   Attribute     attr;
1.96      vatton   1591:   ThotBool      res, res2;
1.95      vatton   1592: 
                   1593:   if (doc && el && name)
1.84      kia      1594:     {
1.95      vatton   1595:       attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                   1596:       attType.AttrTypeNum = Template_ATTR_name;
                   1597:       attr = TtaGetAttribute(el, attType);
                   1598:       if (attr == NULL)
                   1599:         {
                   1600:           attr = TtaNewAttribute (attType);
                   1601:           TtaAttachAttribute (el, attr, doc);
1.96      vatton   1602:           if (withUndo)
                   1603:             TtaRegisterAttributeCreate (attr, el, doc);
1.95      vatton   1604:         }
1.96      vatton   1605:       if (withUndo)
                   1606:          TtaRegisterAttributeReplace(attr, el, doc);
1.95      vatton   1607:       TtaSetAttributeText (attr, name, el, doc);
1.96      vatton   1608:       res = TtaIsValidID (attr, TRUE);
                   1609:       res2 = !MakeUniqueName(el, doc, TRUE, FALSE);
                   1610:       return (res || res2);
                   1611:     }
                   1612: #endif /* TEMPLATES */
                   1613:   return FALSE;
                   1614: }
                   1615: 
                   1616: /*----------------------------------------------------------------------
                   1617:   Template_SetName
                   1618:   Set the xt:component or xt:union element xt:name attribute.
                   1619:   Make it unique.
                   1620:   Return TRUE if the name is not modified.
                   1621:   ----------------------------------------------------------------------*/
                   1622: ThotBool Template_SetLabel (Document doc, Element el, const char *label, ThotBool withUndo)
                   1623: {
                   1624: #ifdef TEMPLATES 
                   1625:   AttributeType attType;
                   1626:   Attribute     attr;
                   1627: 
                   1628:   if (doc && el && label)
                   1629:     {
                   1630:       attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                   1631:       attType.AttrTypeNum = Template_ATTR_title;
                   1632:       attr = TtaGetAttribute(el, attType);
                   1633:       if (attr == NULL)
                   1634:         {
                   1635:           attr = TtaNewAttribute (attType);
                   1636:           TtaAttachAttribute (el, attr, doc);
                   1637:           if (withUndo)
                   1638:             TtaRegisterAttributeCreate (attr, el, doc);
                   1639:         }
                   1640:       if (withUndo)
                   1641:          TtaRegisterAttributeReplace(attr, el, doc);
                   1642:       TtaSetAttributeText (attr, label, el, doc);
1.95      vatton   1643:       return TtaIsValidID (attr, TRUE);
1.84      kia      1644:     }
                   1645: #endif /* TEMPLATES */
                   1646:   return FALSE;
                   1647: }
                   1648: 

Webmaster