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

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.87      vatton    382:                 AddPromptIndicator (el, doc);
1.92      vatton    383:               else
                    384:                 {
                    385:                   // avoid to have a block element within a pseudo paragraph
1.94      vatton    386:                   dec = Template_GetDeclaration (t, types);
1.92      vatton    387:                   if (dec && dec->blockLevel == 2 && parentLine)
                    388:                     {
                    389:                       // move the use element after the paragraph
                    390:                       child = TtaGetParent (el);
1.94      vatton    391:                       otherType = TtaGetElementType (child);
                    392:                       if (otherType.ElSSchema != elType.ElSSchema ||
                    393:                           otherType.ElTypeNum == Template_EL_repeat)
1.92      vatton    394:                         // not need to move the parent element
                    395:                           child = el;
                    396:                       next = child;
                    397:                       prev = parentLine;
                    398:                       while (child)
                    399:                         {
                    400:                           // move the element and next siblings after the pseudo paragraph
                    401:                           TtaNextSibling (&next);
                    402:                           TtaRemoveTree (child, doc);
                    403:                           TtaInsertSibling (child, prev, FALSE, doc);
                    404:                           prev = child;
                    405:                           child = next;
                    406:                         }
                    407:                       // elements are now out of the parent line
                    408:                       savedInline = NULL;
1.97      vatton    409:                       parentLine = NULL;
1.109     vatton    410:                       child = TtaGetFirstChild (el);
1.92      vatton    411:                     }
1.94      vatton    412: 
                    413:                   // generate the currentType attribute
                    414:                   otherType = TtaGetElementType (child);
1.109     vatton    415:                   if (otherType.ElSSchema == elType.ElSSchema &&
                    416:                       otherType.ElTypeNum == Template_EL_TemplateObject)
1.94      vatton    417:                     {
1.109     vatton    418:                       // not already instantiated
                    419:                       TtaDeleteTree (child, doc);
                    420:                       child = NULL;
1.94      vatton    421:                     }
                    422:                   else
                    423:                     {
1.109     vatton    424:                       attType.AttrTypeNum = Template_ATTR_currentType;
                    425:                       att = TtaGetAttribute (el, attType);
                    426:                       if (att == NULL)
                    427:                         {
                    428:                           att = TtaNewAttribute (attType);
                    429:                           TtaAttachAttribute (el, att, doc);
                    430:                         }
                    431:                       if (otherType.ElTypeNum == 1)
                    432:                         TtaSetAttributeText (att, "string", el, doc);
                    433:                       else
                    434:                         {
                    435:                           name = (char *)GetXMLElementName (otherType, doc);
                    436:                           if (name && strcmp (name,"???"))
                    437:                             TtaSetAttributeText (att, name, el, doc);
                    438:                         }
1.94      vatton    439:                     }
1.92      vatton    440:                 }
1.87      vatton    441:             }
1.94      vatton    442:           if (child == NULL)
1.114     vatton    443:             {
                    444:               option = GetAttributeIntValueFromNum (el, Template_ATTR_option);
1.115     vatton    445:               opt = (option == 0 || option == Template_ATTR_option_VAL_option_set);
                    446:               if (opt)
                    447:                 InstantiateUse (t, el, doc, parentLine, FALSE);
1.114     vatton    448:             }
1.94      vatton    449:           else
                    450:             {
                    451:               Template_FixAccessRight (t, el, doc);
                    452:               TtaUpdateAccessRightInViews (doc, el);
                    453:             }
1.109     vatton    454:           TtaFreeMemory (types);
1.80      kia       455:           break;
                    456:         case Template_EL_attribute :
                    457:           if (!loading)
                    458:             InstantiateAttribute (t, el, doc);
                    459:           break;
                    460:         case Template_EL_repeat :
1.104     vatton    461:           InstantiateRepeat (t, el, doc, parentLine, FALSE);
1.80      kia       462:           break;
                    463:         default :
                    464:           break;
                    465:         }
                    466:     }
1.92      vatton    467:   else if (!strcmp (name, "HTML") &&
                    468:            (elType.ElTypeNum == HTML_EL_Pseudo_paragraph ||
                    469:             elType.ElTypeNum == HTML_EL_Paragraph))
                    470:     parentLine = el;
1.80      kia       471: 
                    472:   child = TtaGetFirstChild (el);
                    473:   while (child)
                    474:     {
1.92      vatton    475:       next = child;
                    476:       TtaNextSibling (&next);
                    477:       parentLine = ParseTemplate (t, child, doc, parentLine, loading);
                    478:       child = next;
1.80      kia       479:     }
1.92      vatton    480:   return savedInline;
1.80      kia       481: #endif /* TEMPLATES */
                    482: }
                    483: 
1.63      kia       484: /*----------------------------------------------------------------------
                    485:   CreateTemplate
                    486:   Create a template from any document.
                    487:   ----------------------------------------------------------------------*/
1.89      vatton    488: void CreateTemplate (Document doc, char *templatePath)
1.63      kia       489: {
                    490: #ifdef TEMPLATES
                    491:   Element           root, head, elem, xt, title, child, last;
                    492:   ElementType       elType, xtType;
1.64      kia       493:   char             *s;
1.63      kia       494:   SSchema           templSchema;
                    495:   XTigerTemplate    t;
                    496:   
1.89      vatton    497:   if (IsTemplateInstanceDocument(doc))
1.63      kia       498:     {
                    499:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_INSTANCE),
                    500:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    501:       return;
                    502:     }
                    503: 
1.89      vatton    504:   if (IsTemplateDocument(doc))
1.63      kia       505:     {
                    506:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_TEMPLATE),
                    507:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    508:       return;
                    509:     }
                    510:   
1.64      kia       511:   root = TtaGetRootElement(doc);
                    512:   elType = TtaGetElementType (root);
                    513:   // get the target document type
                    514:   s = TtaGetSSchemaName (elType.ElSSchema);
1.63      kia       515:   
1.64      kia       516:   TtaNewNature (doc, elType.ElSSchema,  NULL, "Template", "TemplateP");
1.96      vatton    517:   TtaSetANamespaceDeclaration (doc, root, "xt", Template_URI);
                    518:   templSchema = TtaGetSSchema ("Template", doc);
                    519:   TtaSetUriSSchema (templSchema, Template_URI);
1.64      kia       520: 
                    521:   // Insert xt:head and others
                    522:   TtaSetStructureChecking (FALSE, doc);
                    523:   if (strcmp (s, "HTML") == 0)
1.63      kia       524:     {
1.64      kia       525:       // Initialize the xt:head
                    526:       elType.ElTypeNum = HTML_EL_HEAD;
1.101     vatton    527:       xtType.ElSSchema = templSchema;
1.64      kia       528:       head = TtaSearchTypedElement (elType, SearchInTree, root);
                    529:       if(head)
                    530:         {
                    531:           xtType.ElTypeNum = Template_EL_head;
                    532:           xt = TtaNewElement(doc, xtType);
                    533:           elem = TtaGetLastChild(head);
                    534:           if(elem)
                    535:               TtaInsertSibling(xt, elem, FALSE, doc);
                    536:           else
                    537:               TtaInsertFirstChild(&xt, head, doc);
                    538:           
                    539:           SetAttributeStringValue(xt, Template_ATTR_version, Template_Current_Version);
                    540:           SetAttributeStringValue(xt, Template_ATTR_templateVersion, "1.0");
                    541:         }
1.63      kia       542:       
1.64      kia       543:       // Initialize the document title
                    544:       elType.ElTypeNum = HTML_EL_TITLE;
                    545:       title = TtaSearchTypedElement (elType, SearchInTree, root);
1.96      vatton    546:       if (title)
1.63      kia       547:         {
1.64      kia       548:           // Create xt:use for title
                    549:           xtType.ElTypeNum = Template_EL_useSimple;
1.96      vatton    550:           xt = TtaNewElement (doc, xtType);
1.64      kia       551:           TtaInsertFirstChild(&xt, title, doc);
1.96      vatton    552:           SetAttributeStringValue (xt, Template_ATTR_types, "string");
                    553:           SetAttributeStringValue (xt, Template_ATTR_title, "title");
1.64      kia       554:           
                    555:           // Move current title content to xt:use
                    556:           last = NULL;
                    557:           while(child = TtaGetLastChild(title), child!=NULL)
1.63      kia       558:             {
1.96      vatton    559:               if (child == xt)
1.64      kia       560:                 break;
1.96      vatton    561:               TtaRemoveTree (child, doc);
                    562:               if (last)
                    563:                 TtaInsertSibling (child, last, FALSE, doc);
1.63      kia       564:               else
1.96      vatton    565:                 TtaInsertFirstChild (&child, xt, doc);
1.64      kia       566:               last = child;
1.63      kia       567:             }
                    568:         }
1.64      kia       569:     }
                    570:   else
                    571:     {
                    572:       xtType.ElSSchema = templSchema;
                    573:       xtType.ElTypeNum = Template_EL_head;
1.96      vatton    574:       xt = TtaNewElement (doc, xtType);
                    575:       TtaInsertFirstChild (&xt, root, doc);
                    576:       SetAttributeStringValue (xt, Template_ATTR_version, Template_Current_Version);
                    577:       SetAttributeStringValue (xt, Template_ATTR_templateVersion, "1.0");      
1.64      kia       578:     }
                    579:   // Save changes
                    580:   TtaSetStructureChecking (TRUE, doc);
1.86      vatton    581:   if (DocumentTypes[doc] == docHTML)
                    582:     // avoid positionned boxes to overlap the xt:head section
                    583:     SetBodyAbsolutePosition (doc);
                    584: 
1.64      kia       585:   TtaClearUndoHistory (doc);
                    586:   RemoveParsingErrors (doc);
1.63      kia       587: 
1.64      kia       588:   TtaFreeMemory(DocumentURLs[doc]);
                    589:   DocumentURLs[doc] = TtaStrdup(templatePath);
                    590:   
                    591:   if(DocumentMeta[doc]==NULL)
                    592:     DocumentMeta[doc] = DocumentMetaDataAlloc();
                    593:   
                    594:   DocumentMeta[doc]->method = CE_TEMPLATE;
                    595:   if(DocumentMeta[doc]->initial_url)
                    596:     {
                    597:       TtaFreeMemory(DocumentMeta[doc]->initial_url);
                    598:       DocumentMeta[doc]->initial_url = NULL;
                    599:     }
                    600:   TtaSetDocumentModified (doc);
1.63      kia       601: 
1.64      kia       602:   // Load template-related infos :
                    603:   // like LoadTemplate(..)
                    604:   t = LookForXTigerTemplate(templatePath);
                    605:   t->doc = doc;
1.89      vatton    606:   Template_PrepareTemplate(t, doc);
1.67      kia       607:   //  DocumentTypes[doc] = docTemplate;
                    608:   t->state |= templloaded|templTemplate;
1.63      kia       609: 
1.98      vatton    610: #ifdef TEMPLATE_DEBUG  
1.64      kia       611:     DumpAllDeclarations();
1.98      vatton    612: #endif /* TEMPLATE_DEBUG */
1.64      kia       613:     
                    614:   /* Update the URL combo box */
                    615:   AddURLInCombobox (DocumentURLs[doc], NULL, FALSE);
                    616:   TtaSetTextZone (doc, 1, URL_list);
                    617:   /* Update template menus */
                    618:   UpdateTemplateMenus(doc);
                    619: 
1.63      kia       620: #endif /* TEMPLATES */  
                    621: }
                    622: 
1.1       vatton    623: /*----------------------------------------------------------------------
                    624:   CreateInstance
1.41      vatton    625:   basedoc is the displayed doc that launchs the creation of instance
1.3       vatton    626:   ----------------------------------------------------------------------*/
1.99      vatton    627: void CreateInstance (char *templatePath, char *instancePath,
                    628:                      char *docname,  DocumentType docType, int basedoc)
1.3       vatton    629: {
1.1       vatton    630: #ifdef TEMPLATES
1.41      vatton    631:   Document          doc = 0, newdoc = 0;
1.90      vatton    632:   Element           root, title, text;
1.41      vatton    633:   ElementType       elType;
1.90      vatton    634:   CHARSET           charset, ocharset;
                    635:   char             *s, *charsetname, *ocharsetname, *localFile;
1.1       vatton    636: 
1.35      kia       637:   XTigerTemplate t = GetXTigerTemplate(templatePath);
1.1       vatton    638:   if (t == NULL)
1.29      vatton    639:     {
                    640:       // the template cannot be loaded
                    641:       InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                    642:       return;
                    643:     }
1.41      vatton    644:   // the template document
1.5       vatton    645:   doc = GetTemplateDocument (t);
1.41      vatton    646:   // localize the new created document
                    647:   if (DontReplaceOldDoc)
                    648:     newdoc = TtaGetNextDocumentIndex ();
                    649:   else
                    650:     newdoc = basedoc;
1.90      vatton    651:   
                    652:   // close current undo sepquence in the template document
                    653:   if (TtaHasUndoSequence (doc))
                    654:     TtaCloseUndoSequence (doc);
                    655: 
                    656:   // update the charset if needed
                    657:   charsetname = TtaGetEnvString ("DOCUMENT_CHARSET");
                    658:   charset = TtaGetCharset (charsetname);
                    659:   ocharsetname = DocumentMeta[doc]->charset;
                    660:   ocharset =  TtaGetCharset (ocharsetname);
                    661:   if (charset != UNDEFINED_CHARSET &&
                    662:       DocumentMeta[doc]->charset &&
                    663:       strcmp (charsetname, DocumentMeta[doc]->charset))
                    664:     {
                    665:       TtaSetDocumentCharset (doc, charset, FALSE);
                    666:       DocumentMeta[doc]->charset = TtaStrdup (charsetname);
                    667:       SetNamespacesAndDTD (doc, FALSE);
                    668:     }
1.54      vatton    669: 
1.72      vatton    670:   // register the document type to open the right page model
                    671:   DocumentTypes[newdoc] = docType;
1.90      vatton    672:   // Generate the instance content as a copy of the template
                    673:   localFile = SaveDocumentToNewDoc(doc, newdoc, instancePath);
                    674:   Template_PrepareInstance (instancePath, newdoc, t->version, templatePath);
                    675:   Template_AddReference (t);
                    676: 
                    677:   // Revert template changes
                    678:   TtaSetDocumentCharset (doc, ocharset, FALSE);
1.111     vatton    679:   TtaFreeMemory (DocumentMeta[doc]->charset);
1.90      vatton    680:   DocumentMeta[doc]->charset = ocharsetname;
                    681:   // Now parse the instance
                    682:   // The xtiger PI will be added and components will be removed
                    683:   GetAmayaDoc (instancePath, NULL, basedoc, basedoc, CE_INSTANCE,
                    684:                !DontReplaceOldDoc, NULL, NULL);
                    685:   if (DocumentMeta[newdoc])
                    686:     DocumentMeta[newdoc]->method = CE_ABSOLUTE;
                    687:   // Generate the HTML document title
                    688:   root = TtaGetRootElement(newdoc);
                    689:   elType = TtaGetElementType (root);
                    690:   // get the target document type
                    691:   s = TtaGetSSchemaName (elType.ElSSchema);
                    692:   if (strcmp (s, "HTML") == 0)
1.1       vatton    693:     {
1.90      vatton    694:       // Initialize the document title
                    695:       elType.ElTypeNum = HTML_EL_TITLE;
                    696:       title = TtaSearchTypedElement (elType, SearchInTree, root);
                    697:       text = TtaGetFirstChild (title);
                    698:       while (text)
1.1       vatton    699:         {
1.90      vatton    700:           elType = TtaGetElementType (text);
                    701:           if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
1.14      vatton    702:             {
1.90      vatton    703:               TtaSetTextContent (text, (unsigned char*)Answer_text,
                    704:                                  TtaGetDefaultLanguage (), newdoc);
                    705:               text = NULL;
                    706:               SetNewTitle (newdoc);
1.14      vatton    707:             }
1.90      vatton    708:           else if ((elType.ElTypeNum == Template_EL_useEl ||
                    709:                     elType.ElTypeNum == Template_EL_useSimple) &&
                    710:                    !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                    711:             // Ignore the template use element
                    712:             text = TtaGetFirstChild (text);
                    713:           else
                    714:             // Look for the first text child
                    715:             TtaNextSibling (&text);
1.63      kia       716:         }
1.90      vatton    717:     }
1.54      vatton    718: 
1.90      vatton    719:   // Insert XTiger PI
                    720:   Template_InsertXTigerPI(newdoc, t);   
                    721:   // Parse template to fill structure and remove extra data
1.92      vatton    722:   ParseTemplate (t, root, newdoc, NULL, FALSE);
1.90      vatton    723:   TtaFreeMemory (localFile);
                    724:   TtaClearUndoHistory (newdoc);
                    725:   RemoveParsingErrors (newdoc);
                    726:   TtaSetDocumentModified (newdoc);
                    727:   UpdateTemplateMenus(newdoc);
1.1       vatton    728: #endif /* TEMPLATES */
                    729: }
                    730: 
                    731: 
                    732: #ifdef TEMPLATES
                    733: /*----------------------------------------------------------------------
                    734:   ProcessAttr
1.9       vatton    735:   Look for all "attribute" elements in the subtree and instantiate them
1.3       vatton    736:   ----------------------------------------------------------------------*/
1.1       vatton    737: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
                    738: {
                    739:   Element      child;
                    740:   ElementType  elType;
                    741: 
                    742:   for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
                    743:     {
                    744:       elType = TtaGetElementType (child);
                    745:       if (elType.ElTypeNum == Template_EL_attribute &&
                    746:           !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
                    747:         InstantiateAttribute (t, child, doc);
                    748:       else
                    749:         ProcessAttr (t, child, doc);
                    750:     }
                    751: }
                    752: #endif /* TEMPLATES */
                    753: 
1.12      kia       754: 
                    755: /*----------------------------------------------------------------------
                    756:   Template_GetNewSimpleTypeInstance
1.109     vatton    757:   Create an new instance of xt:use/SimpleType for the document doc.
                    758:   Return the new element
1.12      kia       759:   ----------------------------------------------------------------------*/
1.109     vatton    760: Element Template_GetNewSimpleTypeInstance(Document doc)
1.12      kia       761: {
                    762:   Element           newEl = NULL;
                    763: #ifdef TEMPLATES
                    764:   ElementType       elType;
1.73      kia       765:   const char       *empty = " ";
1.24      vatton    766: 
1.38      vatton    767:   elType.ElSSchema = TtaGetSSchema("Template", doc);
1.12      kia       768:   elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    769:   newEl = TtaNewElement (doc, elType);
                    770:   TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
1.24      vatton    771: #endif /* TEMPLATES */
1.12      kia       772:   return newEl;
                    773: }
                    774: 
                    775: /*----------------------------------------------------------------------
                    776:   Template_GetNewXmlElementInstance
1.109     vatton    777:   Create an new instance of xt:use/XmlElement for the document doc.
                    778:   The parameter decl gives the type of the element of new element.
                    779:   Return the new element
1.12      kia       780:   ----------------------------------------------------------------------*/
1.109     vatton    781: Element Template_GetNewXmlElementInstance(Document doc, Declaration decl)
1.12      kia       782: {
                    783:   Element           newEl = NULL;
                    784: #ifdef TEMPLATES
                    785:   ElementType       elType;
                    786: 
1.24      vatton    787:   GIType (decl->name, &elType, doc);
                    788:   if (elType.ElTypeNum != 0)
1.23      kia       789:     newEl = TtaNewTree (doc, elType, "");
1.12      kia       790: #endif /* TEMPLATES */
                    791:   return newEl;
                    792: }
                    793: 
1.34      vatton    794: 
                    795: /*----------------------------------------------------------------------
                    796:   InsertWithNotify applies pre and post functions when inserting the new
                    797:   element el after child (if not NULL) or as first child of parent.
                    798:   ----------------------------------------------------------------------*/
                    799: Element InsertWithNotify (Element el, Element child, Element parent, Document doc)
                    800: {
                    801:   ElementType      elType;
1.116     vatton    802:   AttributeType    attrType;
                    803:   Attribute        attr;
1.34      vatton    804:   NotifyElement    event;
                    805:   char            *name;
                    806:   ThotBool         isRow = FALSE, isCell = FALSE;
1.50      vatton    807:   ThotBool         isImage = FALSE;
                    808:   ThotBool         oldStructureChecking;
                    809: 
                    810:   // avoid to check attributes now
                    811:   oldStructureChecking = TtaGetStructureChecking (doc);
                    812:   TtaSetStructureChecking (FALSE, doc);
1.34      vatton    813: 
                    814:   elType = TtaGetElementType (el);
                    815:   name = TtaGetSSchemaName (elType.ElSSchema);
                    816:   isCell = ((!strcmp (name,"HTML") &&
                    817:              elType.ElTypeNum == HTML_EL_Data_cell ||
                    818:              elType.ElTypeNum == HTML_EL_Heading_cell) ||
                    819:             (!strcmp (name,"MathML") && elType.ElTypeNum == MathML_EL_MTD));
                    820:   isRow = ((!strcmp (name,"HTML") && elType.ElTypeNum == HTML_EL_Table_row) ||
                    821:            (!strcmp (name,"MathML") &&
                    822:             (elType.ElTypeNum == MathML_EL_MTR ||
                    823:              elType.ElTypeNum == MathML_EL_MLABELEDTR)));
1.50      vatton    824:   isImage = (!strcmp (name,"HTML") && 
                    825:               (elType.ElTypeNum == HTML_EL_IMG || elType.ElTypeNum == HTML_EL_Object));
1.34      vatton    826:   if (child)
                    827:     TtaInsertSibling (el, child, FALSE, doc);
                    828:   else
                    829:     TtaInsertFirstChild (&el, parent, doc);
1.50      vatton    830:   TtaSetStructureChecking (oldStructureChecking, doc);
1.34      vatton    831: 
1.50      vatton    832:   if (isImage)
1.116     vatton    833:     {
                    834:       // check if the src attribute is there
                    835:       attrType.AttrSSchema = elType.ElSSchema;
                    836:       if (elType.ElTypeNum == HTML_EL_IMG)
                    837:         attrType.AttrTypeNum = HTML_ATTR_SRC;
                    838:       else
                    839:          attrType.AttrTypeNum = HTML_ATTR_data;
                    840:       attr = TtaGetAttribute (el, attrType);
                    841:       if (attr == NULL)
                    842:         InsertImageOrObject (el, doc);
                    843:     }
1.50      vatton    844:   else if (isCell)
1.34      vatton    845:     {
                    846:       // a cell is created
1.39      quint     847:       NewCell (el, doc, TRUE, TRUE, TRUE);
1.34      vatton    848:     }
                    849:   else if (isRow)
                    850:     {
                    851:       // a row is created
                    852:       event.element = el;
                    853:       event.document = doc;
                    854:       RowPasted (&event);
                    855:     }
1.50      vatton    856:   
                    857:   if (!strcmp (name,"HTML"))
                    858:     {
1.104     vatton    859:       // special management for images and objets
1.50      vatton    860:       elType.ElTypeNum = HTML_EL_IMG;
                    861:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    862:       while (child)
                    863:         {
                    864:           InsertImageOrObject (child, doc);
                    865:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    866:         }
                    867:       elType.ElTypeNum = HTML_EL_Object;
                    868:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    869:       while (child)
                    870:         {
                    871:           InsertImageOrObject (child, doc);
                    872:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    873:         }
                    874:     }
1.34      vatton    875:   return el;
                    876: }
                    877: 
                    878: 
1.12      kia       879: /*----------------------------------------------------------------------
1.118     vatton    880:   Template_FillEmpty generates an empty contents
                    881:   ----------------------------------------------------------------------*/
                    882: Element Template_FillEmpty (Element el, Document doc)
                    883: {
                    884: #ifdef TEMPLATES
                    885:   Element         child;
                    886:   ElementType     elType;
                    887: 
                    888:   // generate a content
                    889:   child = TtaGetFirstChild (el);
                    890:   if (child == NULL)
                    891:     {
                    892:       elType = TtaGetElementType (el);
                    893:       elType.ElTypeNum = Template_EL_TemplateObject;
                    894:       child = TtaNewElement (doc, elType);
                    895:       TtaInsertFirstChild (&child, el, doc);
                    896:     }
                    897:   return child;
                    898: #endif /* TEMPLATES */
                    899:   return NULL;
                    900: }
                    901: 
                    902: /*----------------------------------------------------------------------
1.16      kia       903:   Template_InsertUseChildren
                    904:   Insert children to a xt:use
                    905:   The dec parameter must be valid and will not be verified. It must be a
                    906:     direct child element (for union elements).
                    907:   @param el element (xt:use) in which insert a new element
                    908:   @param dec Template declaration of the element to insert
                    909:   @return The inserted element (the xt:use element if insertion is multiple as component)
1.104     vatton    910:   The parentLine parameter points to the enclosing line if any.
1.16      kia       911:   ----------------------------------------------------------------------*/
1.104     vatton    912: Element Template_InsertUseChildren (Document doc, Element el, Declaration dec,
                    913:                                     Element parentLine, ThotBool registerUndo)
1.16      kia       914: {
1.99      vatton    915:   Element         newEl = NULL;
1.16      kia       916: #ifdef TEMPLATES
1.99      vatton    917:   Element         current = NULL;
1.104     vatton    918:   Element         child = NULL, prev, next;
                    919:   ElementType     childType, elType; 
                    920:   SSchema         sshtml;
1.99      vatton    921:   XTigerTemplate  t;
1.17      kia       922:   
1.25      vatton    923:   if (TtaGetDocumentAccessMode(doc))
1.16      kia       924:   {
1.23      kia       925:     switch (dec->nature)
                    926:     {
                    927:       case SimpleTypeNat:
1.109     vatton    928:         newEl = Template_GetNewSimpleTypeInstance(doc);
1.34      vatton    929:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       930:         break;
                    931:       case XmlElementNat:
1.109     vatton    932:         newEl = Template_GetNewXmlElementInstance(doc, dec);
1.34      vatton    933:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       934:         break;
                    935:       case ComponentNat:
                    936:         newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
1.74      kia       937:         ProcessAttr (dec->usedIn, newEl, doc);
1.104     vatton    938:         elType = TtaGetElementType (el);
1.23      kia       939:         /* Copy elements from new use to existing use. */
1.98      vatton    940: #ifdef TEMPLATE_DEBUG
1.74      kia       941:         DumpSubtree(newEl, doc, 0);
1.98      vatton    942: #endif /* TEMPLATE_DEBUG */
1.104     vatton    943:         sshtml = TtaGetSSchema ("HTML", doc);
1.102     vatton    944:         t = GetXTigerDocTemplate( doc);
                    945:         child = TtaGetFirstChild  (newEl);
1.76      vatton    946:         while (child)
                    947:           {
                    948:             // move the new subtree to the document
                    949:             TtaRemoveTree (child, doc);
1.104     vatton    950:             childType = TtaGetElementType (child);
                    951:             if (parentLine)
                    952:               {
                    953:                 if (childType.ElSSchema == sshtml &&
                    954:                     childType.ElTypeNum == HTML_EL_Pseudo_paragraph)
                    955:                   {
                    956:                     prev = TtaGetFirstChild  (child);
                    957:                     while (prev)
                    958:                       {
                    959:                         next = prev;
                    960:                         TtaNextSibling (&next);
                    961:                         TtaRemoveTree (prev, doc);
                    962:                         current = InsertWithNotify (prev, current, el, doc);
                    963:                         prev = next;
                    964:                       }
1.108     vatton    965:                     TtaDeleteTree (child, doc);
1.104     vatton    966:                   }
1.108     vatton    967:                 else
                    968:                   current = InsertWithNotify (child, current, el, doc);
1.104     vatton    969:               }
                    970:             else
                    971:               {
                    972:                 current = InsertWithNotify (child, current, el, doc);
                    973:                 // check if a new paragraph is inserted
                    974:                 if (childType.ElSSchema == sshtml &&
                    975:                     childType.ElTypeNum == HTML_EL_Paragraph)
                    976:                   Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
                    977:                 else
                    978:                   {
                    979:                     childType.ElSSchema = sshtml;
                    980:                     childType.ElTypeNum = HTML_EL_Paragraph;
                    981:                     child = TtaSearchTypedElement (childType, SearchInTree, current);
                    982:                     while (child)
                    983:                       {
                    984:                         Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
                    985:                         child = TtaSearchTypedElement (childType, SearchInTree, child);
                    986:                       }
                    987:                   }
                    988:               }
1.99      vatton    989:             child = TtaGetFirstChild (newEl);
1.76      vatton    990:           }
1.99      vatton    991: 
1.102     vatton    992:         TtaDeleteTree (newEl, doc);
1.23      kia       993:         newEl = el;
                    994:         break;
                    995:       default :
                    996:         //Impossible
                    997:         break;   
                    998:     }
1.44      kia       999:     Template_FixAccessRight (dec->usedIn, el, doc);
1.105     vatton   1000:     if (dec->nature == ComponentNat)
                   1001:       Component_FixAccessRight (el, doc);
1.102     vatton   1002:     TtaUpdateAccessRightInViews (doc, el);
1.23      kia      1003:   }  
1.16      kia      1004: #endif /* TEMPLATES */
                   1005:   return newEl;
                   1006: }
                   1007: 
1.40      kia      1008: 
                   1009: /*----------------------------------------------------------------------
1.105     vatton   1010:   Component_FixAccessRight locks children of the component
                   1011:   ----------------------------------------------------------------------*/
                   1012: void Component_FixAccessRight (Element el, Document doc)
                   1013: {
                   1014: #ifdef TEMPLATES
                   1015:   Element     child;
                   1016:   
                   1017:   if (el && doc)
                   1018:     {
                   1019:       TtaSetAccessRight (el, ReadOnly, doc);
                   1020:       // fix access right to children
                   1021:       child = TtaGetFirstChild (el);
                   1022:       while (child)
                   1023:         {
                   1024:           TtaSetAccessRight (child, ReadOnly, doc);
                   1025:           TtaNextSibling (&child);
                   1026:         }
                   1027:     }
                   1028: #endif /* TEMPLATES */
                   1029: }
                   1030: 
                   1031: /*----------------------------------------------------------------------
1.97      vatton   1032:   Template_FixAccessRight fixes access rights of the el element
1.40      kia      1033:   ----------------------------------------------------------------------*/
1.41      vatton   1034: void Template_FixAccessRight (XTigerTemplate t, Element el, Document doc)
1.40      kia      1035: {
                   1036: #ifdef TEMPLATES
                   1037:   ElementType elType;
                   1038:   Element     child;
1.97      vatton   1039:   Declaration decl;
1.88      vatton   1040:   char        currentType[MAX_LENGTH], *ptr;
1.40      kia      1041:   
                   1042:   if (t && el && doc)
                   1043:     {
                   1044:       elType = TtaGetElementType(el);
1.41      vatton   1045:       if (elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.40      kia      1046:         {
1.41      vatton   1047:           switch (elType.ElTypeNum)
1.40      kia      1048:             {
                   1049:             case Template_EL_TEXT_UNIT:
1.41      vatton   1050:               //TtaSetAccessRight( el, ReadWrite, doc);
                   1051:               return;
1.105     vatton   1052:             case Template_EL_component:
                   1053:               Component_FixAccessRight (el, doc);
                   1054:               break;
1.40      kia      1055:             case Template_EL_useEl:
                   1056:             case Template_EL_useSimple:
                   1057:               GiveAttributeStringValueFromNum(el, Template_ATTR_currentType,
                   1058:                                               (char*)currentType, NULL);
1.88      vatton   1059:               if (currentType[0] == EOS)
                   1060:                 {
                   1061:                   GiveAttributeStringValueFromNum(el, Template_ATTR_types,
                   1062:                                                   (char*)currentType, NULL);
                   1063:                   ptr = strstr (currentType, " ");
                   1064:                   if (ptr)
                   1065:                     *ptr = EOS;
                   1066:                 }
1.40      kia      1067:               decl = Template_GetDeclaration(t, currentType);
                   1068:               if (decl)
                   1069:                 {
                   1070:                   switch (decl->nature)
                   1071:                     {
                   1072:                       case SimpleTypeNat:
1.41      vatton   1073:                         TtaSetAccessRight (el, ReadWrite, doc);
                   1074:                         return;
1.105     vatton   1075:                       case ComponentNat:
1.107     vatton   1076:                         TtaSetAccessRight (el, ReadOnly, doc);
                   1077:                          break;
                   1078:                         //Component_FixAccessRight (el, doc);
                   1079:                         //return;
1.97      vatton   1080:                       case XmlElementNat:
1.105     vatton   1081:                         if (TtaIsSetReadOnly (el))
                   1082:                           break;
1.97      vatton   1083:                         child = TtaGetFirstChild (el);
                   1084:                         if (child)
                   1085:                           TtaSetAccessRight (child, ReadWrite, doc);
1.40      kia      1086:                       default:
1.41      vatton   1087:                         TtaSetAccessRight (el, ReadOnly, doc);
                   1088:                          break;
1.40      kia      1089:                     }
                   1090:                 }
                   1091:               break;
                   1092:             case Template_EL_bag:
1.45      vatton   1093:             case Template_EL_repeat:
1.40      kia      1094:               TtaSetAccessRight(el, ReadWrite, doc);
                   1095:               break;
                   1096:             default:
                   1097:               TtaSetAccessRight(el, ReadOnly, doc);
                   1098:               break;
                   1099:             }
                   1100:         }
                   1101: 
1.92      vatton   1102:       // fix access right to children
1.41      vatton   1103:       child = TtaGetFirstChild (el);
                   1104:       while (child)
1.40      kia      1105:         {
1.41      vatton   1106:           Template_FixAccessRight (t, child, doc);
                   1107:           TtaNextSibling (&child);
1.40      kia      1108:         }
                   1109:     }
                   1110: #endif /* TEMPLATES */
                   1111: }
                   1112: 
1.16      kia      1113: /*----------------------------------------------------------------------
1.46      vatton   1114:   AddPromptIndicator
                   1115:   ----------------------------------------------------------------------*/
                   1116: void AddPromptIndicator (Element el, Document doc)
                   1117: {
                   1118: #ifdef TEMPLATES
                   1119:   ElementType         elType;
                   1120:   AttributeType       attrType;
                   1121:   Attribute           att;
                   1122: 
1.66      vatton   1123:   if (el)
                   1124:     {
                   1125:       elType = TtaGetElementType (el);
                   1126:       attrType.AttrSSchema = elType.ElSSchema;
                   1127:       attrType.AttrTypeNum = Template_ATTR_prompt;
1.69      vatton   1128:       att = TtaGetAttribute (el, attrType);
                   1129:       if (att == NULL)
                   1130:         {
                   1131:           att = TtaNewAttribute (attrType);
                   1132:           TtaAttachAttribute (el, att, doc);
                   1133:         }
1.66      vatton   1134:     }
1.46      vatton   1135: #endif /* TEMPLATES */
                   1136: }
                   1137: 
                   1138: /*----------------------------------------------------------------------
1.104     vatton   1139:   Template_SetInline manages inline elements
                   1140:   registerUndo says if changes must be registered
                   1141:   ----------------------------------------------------------------------*/
                   1142: void Template_SetInline (Element el, SSchema sstempl, Document doc, ThotBool registerUndo)
                   1143: {
                   1144: #ifdef TEMPLATES
                   1145:   Element         child = NULL;
                   1146:   ElementType     elType1, elType2, elType3;
                   1147: 
                   1148:   if (el)
                   1149:     {
                   1150:       elType1 = TtaGetElementType (el);
                   1151:       if (elType1.ElSSchema == sstempl)
                   1152:         // apply to hte current template element
                   1153:         SetAttributeIntValue (el, Template_ATTR_SetInLine,
                   1154:                               Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
                   1155:       else
                   1156:         elType1.ElSSchema = sstempl;
                   1157:       elType1.ElTypeNum = Template_EL_useSimple;
                   1158:       elType2.ElTypeNum = Template_EL_useEl;
                   1159:       elType2.ElSSchema = elType1.ElSSchema;
                   1160:       elType3.ElTypeNum = Template_EL_repeat;
                   1161:       elType3.ElSSchema = elType1.ElSSchema;
                   1162:       child = TtaSearchElementAmong5Types (elType1, elType2, elType3, elType3, elType3,
                   1163:                                            SearchForward, el);
                   1164:       while (child && TtaIsAncestor (child, el))
                   1165:         {
                   1166:           SetAttributeIntValue (child, Template_ATTR_SetInLine,
                   1167:                                 Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
                   1168:           child = TtaSearchElementAmong5Types (elType1, elType2,
                   1169:                                                elType3, elType3, elType3,
                   1170:                                                SearchForward, child);
                   1171:         }
                   1172:     }
                   1173: #endif /* TEMPLATES */
                   1174: }
                   1175: 
                   1176: /*----------------------------------------------------------------------
                   1177:   InstantiateUse intantiate the use element el.
                   1178:   The parentLine parameter points to the enclosing line if any.
1.3       vatton   1179:   ----------------------------------------------------------------------*/
1.1       vatton   1180: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.104     vatton   1181:                         Element parentLine, ThotBool registerUndo)
1.1       vatton   1182: {
                   1183: #ifdef TEMPLATES
1.109     vatton   1184:   Element          child = NULL;
1.1       vatton   1185:   ElementType      elType;
                   1186:   Declaration      dec;
1.109     vatton   1187:   int              size, nbitems, i, option;
1.1       vatton   1188:   struct menuType  *items;
1.104     vatton   1189:   char             *types;
1.109     vatton   1190:   ThotBool          oldStructureChecking, hidden;
1.1       vatton   1191: 
1.25      vatton   1192:   if (!t)
1.23      kia      1193:     return NULL;
                   1194: 
1.1       vatton   1195:   /* get the value of the "types" attribute */
1.12      kia      1196:   elType = TtaGetElementType (el);
1.32      vatton   1197:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36      vatton   1198:   if (!types || types[0] == EOS)
                   1199:     {
                   1200:       TtaFreeMemory (types);
                   1201:       return NULL;
                   1202:     }
1.104     vatton   1203:   if (!strcmp (types, "string") || !strcmp (types, "number"))
1.109     vatton   1204:     {
                   1205:       child = TtaGetFirstChild (el);
                   1206:       if (child == NULL)
                   1207:         {
                   1208:           child = Template_GetNewSimpleTypeInstance(doc);
                   1209:           child = InsertWithNotify (child, NULL, el, doc);
                   1210:         }
                   1211:       AddPromptIndicator (el, doc);
                   1212:     }
1.104     vatton   1213:   else
1.1       vatton   1214:     {
1.104     vatton   1215:       giveItems (types, size, &items, &nbitems);
                   1216:       // No structure checking
                   1217:       oldStructureChecking = TtaGetStructureChecking (doc);
                   1218:       TtaSetStructureChecking (FALSE, doc);
1.109     vatton   1219:       hidden = ElementIsOptional (el);
                   1220:       if (hidden)
                   1221:         {
                   1222:           // check the current option value
                   1223:           option = GetAttributeIntValueFromNum (el, Template_ATTR_option);
                   1224:           hidden = (option == Template_ATTR_option_VAL_option_unset);
                   1225:         }
                   1226:       if (nbitems == 1 || !hidden)
1.104     vatton   1227:         /* only one type in the "types" attribute */
                   1228:         {
                   1229:           dec = Template_GetDeclaration (t, items[0].label);
                   1230:           if (dec)
1.110     vatton   1231:             child = Template_InsertUseChildren (doc, el, dec, parentLine, registerUndo);
1.112     vatton   1232:           if (nbitems == 1 && elType.ElTypeNum != Template_EL_useSimple)
                   1233:             {
                   1234:               TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
                   1235:               if (registerUndo)
                   1236:                 TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
                   1237:             }
1.104     vatton   1238:         }
                   1239:       else
1.27      kia      1240:         {
1.104     vatton   1241:           // insert almost a pseudo element
                   1242:           elType.ElTypeNum = Template_EL_TemplateObject;
1.109     vatton   1243:           child = TtaNewElement (doc, elType);
                   1244:           TtaInsertFirstChild (&child, el, doc);
1.27      kia      1245:         }
1.104     vatton   1246: 
                   1247:       for (i = 0; i < nbitems; i++)
                   1248:         TtaFreeMemory(items[i].label);
                   1249:       TtaFreeMemory(items);
                   1250: 
                   1251:       if (parentLine)
                   1252:         // display the element in line
                   1253:         Template_SetInline (el, elType.ElSSchema, doc, registerUndo);
                   1254:       TtaSetStructureChecking (oldStructureChecking, doc);
1.1       vatton   1255:     }
1.32      vatton   1256:   TtaFreeMemory (types);
1.44      kia      1257:   Template_FixAccessRight (t, el, doc);
                   1258:   TtaUpdateAccessRightInViews (doc, el);
1.109     vatton   1259:   return child;
1.23      kia      1260: #else /* TEMPLATES */
                   1261:   return NULL;
1.1       vatton   1262: #endif /* TEMPLATES */
                   1263: }
                   1264: 
                   1265: /*----------------------------------------------------------------------
1.22      kia      1266:   InstantiateRepeat
                   1267:   Check for min and max param and validate xt:repeat element content.
                   1268:   @param registerUndo True to register undo creation sequences.
1.104     vatton   1269:   The parentLine parameter points to the enclosing line if any.
1.3       vatton   1270:   ----------------------------------------------------------------------*/
1.46      vatton   1271: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc,
1.104     vatton   1272:                         Element parentLine, ThotBool registerUndo)
1.1       vatton   1273: {
                   1274: #ifdef TEMPLATES
1.32      vatton   1275:   Element        child, newChild;
1.52      vatton   1276:   ElementType    newElType;
                   1277:   Attribute      minAtt,  maxAtt;
                   1278:   AttributeType  minType, maxType;
                   1279:   char          *text, *types = NULL, *title = NULL;
                   1280:   int            curVal, minVal,  maxVal;
1.32      vatton   1281:   int            childrenCount;
                   1282: 
1.25      vatton   1283:   if (!t)
1.23      kia      1284:     return;
                   1285: 
1.1       vatton   1286:   //Preparing types
1.52      vatton   1287:   minType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.1       vatton   1288:   minType.AttrTypeNum = Template_ATTR_minOccurs;
1.52      vatton   1289:   maxType.AttrSSchema =  minType.AttrSSchema;
1.1       vatton   1290:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
1.52      vatton   1291:   newElType.ElSSchema = minType.AttrSSchema;
                   1292:   //Get minOccurs and maxOccurs attributes
1.1       vatton   1293:   minAtt = TtaGetAttribute (el, minType);
                   1294:   maxAtt = TtaGetAttribute (el, maxType);
                   1295:   //Get the values
                   1296:   if (minAtt)
                   1297:     {
1.10      kia      1298:       text = GetAttributeStringValue(el, minAtt, NULL);
1.1       vatton   1299:       if (text)
                   1300:         {
                   1301:           minVal = atoi(text);
                   1302:           TtaFreeMemory(text);
1.52      vatton   1303:           curVal = minVal;
1.1       vatton   1304:         }
                   1305:       else
                   1306:         //Error : Attribute with no value
                   1307:         return;
                   1308:     }
                   1309:   else
1.52      vatton   1310:     {
                   1311:       minVal = 0;
                   1312:       curVal = 1;
                   1313:     }
1.1       vatton   1314: 
                   1315:   if (maxAtt)
                   1316:     {
1.10      kia      1317:       text = GetAttributeStringValue (el, maxAtt, NULL);
1.1       vatton   1318:       if (text)
                   1319:         {
                   1320:           if (!strcmp (text, "*"))
                   1321:             maxVal = INT_MAX;
                   1322:           else
                   1323:             maxVal = atoi (text);
                   1324:           TtaFreeMemory (text);
                   1325:         }
                   1326:       else
                   1327:         //Error : Attribute with no value
                   1328:         return;
                   1329:     }
                   1330:   else
                   1331:     maxVal = INT_MAX;
                   1332: 
                   1333:   text = (char*)TtaGetMemory(MAX_LENGTH);
1.52      vatton   1334:   //Create non existing min max attributes
                   1335:   if (minAtt == NULL)
1.1       vatton   1336:     {      
1.32      vatton   1337:       minAtt = TtaNewAttribute (minType);
                   1338:       sprintf (text, "%d", minVal);
                   1339:       TtaAttachAttribute (el, minAtt, doc);
                   1340:       TtaSetAttributeText (minAtt, text, el, doc);
1.25      vatton   1341:       if (registerUndo)
1.32      vatton   1342:         TtaRegisterAttributeCreate (minAtt, el, doc);
1.1       vatton   1343:     }
                   1344: 
1.52      vatton   1345:   if (maxAtt == NULL)
1.1       vatton   1346:     {  
1.32      vatton   1347:       maxAtt = TtaNewAttribute (maxType);
                   1348:       if (maxVal < INT_MAX)
                   1349:         sprintf(text, "%d", maxVal);
1.1       vatton   1350:       else
1.32      vatton   1351:         sprintf (text, "*");
                   1352:       TtaAttachAttribute (el, maxAtt, doc);      
                   1353:       TtaSetAttributeText (maxAtt, text, el, doc);
1.25      vatton   1354:       if (registerUndo)
1.32      vatton   1355:         TtaRegisterAttributeCreate (maxAtt, el, doc);
1.1       vatton   1356:     }
1.52      vatton   1357:   TtaFreeMemory(text);
1.1       vatton   1358: 
1.52      vatton   1359:   //We must have minOccurs children
1.104     vatton   1360:   child = TtaGetFirstChild (el);
1.1       vatton   1361:   if (!child)
                   1362:     //Error : a repeat must have at least one child which will be the model
                   1363:     return;
                   1364:   
1.104     vatton   1365:   for (childrenCount = 0; child; TtaNextSibling(&child))
                   1366:     //TODO : Check that every child is valid
                   1367:     childrenCount ++;
1.1       vatton   1368: 
                   1369:   if (childrenCount > maxVal)
                   1370:     //Error : too many children!
                   1371:     return;  
                   1372: 
1.104     vatton   1373: 
                   1374:   if (parentLine)
                   1375:     // display the element in line
                   1376:     Template_SetInline (el, minType.AttrSSchema, doc, registerUndo);
                   1377: 
1.1       vatton   1378:   child = TtaGetLastChild(el);
1.32      vatton   1379:   types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
                   1380:   title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
1.52      vatton   1381:   newElType.ElTypeNum = Template_EL_useEl;
1.32      vatton   1382:   while (childrenCount < curVal)
1.1       vatton   1383:     {
1.32      vatton   1384:       newChild = TtaNewElement (doc, newElType);
1.27      kia      1385:       // Insert it
1.32      vatton   1386:       TtaInsertSibling (newChild, child, FALSE, doc);
                   1387:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
                   1388:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.104     vatton   1389:       InstantiateUse (t, newChild, doc, parentLine, TRUE);
1.27      kia      1390:       
1.25      vatton   1391:       if (registerUndo)
1.34      vatton   1392:         TtaRegisterElementCreate (newChild, doc);
1.1       vatton   1393:       child = newChild;
                   1394:       childrenCount++;
                   1395:     }
1.27      kia      1396:     
1.44      kia      1397:   Template_FixAccessRight (t, el, doc);
                   1398:   TtaUpdateAccessRightInViews (doc, el);
1.32      vatton   1399:   TtaFreeMemory (types);
                   1400:   TtaFreeMemory (title);
1.1       vatton   1401: #endif /* TEMPLATES */
                   1402: }
                   1403: 
                   1404: /*----------------------------------------------------------------------
1.80      kia      1405:   Template_InsertXTigerPI
                   1406:   Insert the XTiger PI element in template instance.
                   1407:   Param t is the XTigerTemplate structure of the template,
                   1408:   not the template instance one.
1.3       vatton   1409:   ----------------------------------------------------------------------*/
1.80      kia      1410: void Template_InsertXTigerPI(Document doc, XTigerTemplate t)
1.1       vatton   1411: {
                   1412: #ifdef TEMPLATES
1.47      kia      1413:   ElementType     elType;
1.65      vatton   1414:   Element         root, piElem, doctype, line, text, elNew, elFound;
1.47      kia      1415:   char           *s, *charsetname = NULL, buffer[MAX_LENGTH];
1.1       vatton   1416:   int             pi_type;
                   1417: 
1.80      kia      1418:   if (!t || !doc)
1.23      kia      1419:     return;
1.47      kia      1420: 
1.80      kia      1421:   root =  TtaGetMainRoot (doc);
1.90      vatton   1422:   if (root == NULL)
                   1423:     return;
1.1       vatton   1424:   //Look for PIs
                   1425:   /* check if the document has a DOCTYPE declaration */
                   1426: #ifdef ANNOTATIONS
                   1427:   if (DocumentTypes[doc]  == docAnnot)
                   1428:     elType = TtaGetElementType (root);
                   1429:   else
                   1430: #endif /* ANNOTATIONS */
                   1431:     elType = TtaGetElementType (root);
                   1432:   s = TtaGetSSchemaName (elType.ElSSchema);
                   1433:   if (strcmp (s, "HTML") == 0)
                   1434:     {
                   1435:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                   1436:       pi_type = HTML_EL_XMLPI;
                   1437:     }
                   1438: #ifdef _SVG
                   1439:   else if (strcmp (s, "SVG") == 0)
                   1440:     {
                   1441:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                   1442:       pi_type = SVG_EL_XMLPI;
                   1443:     }
                   1444: #endif /* _SVG */
                   1445:   else if (strcmp (s, "MathML") == 0)
                   1446:     {
                   1447:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                   1448:       pi_type = MathML_EL_XMLPI;
                   1449:     }
                   1450:   else
                   1451:     {
                   1452:       elType.ElTypeNum = XML_EL_doctype;
                   1453:       pi_type = XML_EL_xmlpi;
                   1454:     }
1.54      vatton   1455: 
1.1       vatton   1456:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
1.65      vatton   1457:   if (doctype == NULL)
1.1       vatton   1458:     {
1.65      vatton   1459:       elType.ElTypeNum = pi_type;      
                   1460:       piElem = TtaSearchTypedElement (elType, SearchInTree, root);
                   1461:       if (piElem == NULL)
                   1462:         {
                   1463:           /* generate the XML declaration */
                   1464:           /* Check the Thot abstract tree against the structure schema. */
                   1465:           TtaSetStructureChecking (FALSE, doc);
                   1466:           piElem = TtaNewTree (doc, elType, "");
                   1467:           TtaInsertFirstChild (&piElem, root, doc);
                   1468:           line = TtaGetFirstChild (piElem);
                   1469:           text = TtaGetFirstChild (line);
                   1470:           strcpy (buffer, "xml version=\"1.0\" encoding=\"");
                   1471:           charsetname = UpdateDocumentCharset (doc);
                   1472:           strcat (buffer, charsetname);
                   1473:           strcat (buffer, "\"");
                   1474:           TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                   1475:           TtaSetStructureChecking (TRUE, doc);
1.90      vatton   1476:           TtaFreeMemory (charsetname);
                   1477:           TtaRegisterElementCreate (piElem, doc);
1.65      vatton   1478:         }
1.1       vatton   1479:     }
                   1480:   
                   1481:   /* generate the XTiger PI */
                   1482:   /* Check the Thot abstract tree against the structure schema. */
                   1483:   TtaSetStructureChecking (FALSE, doc);
                   1484:   elType.ElTypeNum = pi_type;
1.65      vatton   1485:   elNew = TtaNewTree (doc, elType, "");
                   1486:   if (doctype)
                   1487:     TtaInsertSibling (elNew, doctype, FALSE, doc);
                   1488:   else
                   1489:     TtaInsertSibling (elNew, piElem, FALSE, doc);
                   1490:   line = TtaGetFirstChild (elNew);
                   1491:   text = TtaGetFirstChild (line);
1.1       vatton   1492:   strcpy (buffer, "xtiger template=\"");
1.100     vatton   1493:   if (t->uri)
                   1494:     strcat (buffer, t->uri);
                   1495:   else if (t->base_uri)
                   1496:     strcat (buffer, t->uri);
1.17      kia      1497:   strcat (buffer, "\" version=\"");
1.20      vatton   1498:   if (t->version)
                   1499:     strcat (buffer, t->version);
                   1500:   else
                   1501:     strcat (buffer, "0.8");
1.1       vatton   1502:   strcat (buffer, "\"");
1.25      vatton   1503:   if (t->templateVersion)
1.20      vatton   1504:     {
                   1505:       strcat (buffer, " templateVersion=\"");
                   1506:       strcat (buffer, t->templateVersion);
                   1507:       strcat (buffer, "\"");
                   1508:     }
1.1       vatton   1509:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
1.90      vatton   1510:   TtaRegisterElementCreate (elNew, doc);
1.1       vatton   1511:   TtaSetStructureChecking (TRUE, doc);
1.5       vatton   1512: 
                   1513:   // update the document title
1.47      kia      1514:   if (!strcmp (s, "HTML"))
1.5       vatton   1515:     {
                   1516:       elType.ElTypeNum = HTML_EL_TITLE;
                   1517:       elFound = TtaSearchTypedElement (elType, SearchInTree, root);
1.90      vatton   1518:       text = TtaGetFirstChild (elFound);
                   1519:       while (text)
1.5       vatton   1520:         {
1.90      vatton   1521:           elType = TtaGetElementType (text);
                   1522:           if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
                   1523:             {
                   1524:               TtaRegisterElementReplace (text, doc);
                   1525:               TtaSetTextContent (text, (unsigned char*)Answer_text,
                   1526:                                  TtaGetDefaultLanguage (), doc);
                   1527:               text = NULL;
                   1528:             }
                   1529:           else if ((elType.ElTypeNum == Template_EL_useEl ||
                   1530:                     elType.ElTypeNum == Template_EL_useSimple) &&
                   1531:                    !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                   1532:             // Ignore the template use element
                   1533:             text = TtaGetFirstChild (text);
                   1534:           else
                   1535:             // Look for the first text child
                   1536:             TtaNextSibling (&text);
1.5       vatton   1537:         }
                   1538:     }
1.1       vatton   1539: #endif /* TEMPLATES */
                   1540: }
                   1541: 
1.80      kia      1542: 
1.1       vatton   1543: /*----------------------------------------------------------------------
1.68      kia      1544:   Template_PreInstantiateComponents
                   1545:   Instantiates all components in order to improve editing.
1.3       vatton   1546:   ----------------------------------------------------------------------*/
1.35      kia      1547: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1       vatton   1548: {
                   1549: #ifdef TEMPLATES 
1.90      vatton   1550:   ForwardIterator iter;
                   1551:   Declaration     dec;
                   1552:   SearchSetNode   node;
                   1553: 
1.25      vatton   1554:   if (!t)
1.23      kia      1555:     return;
                   1556: 
1.90      vatton   1557:   if (Template_IsInstance (t))
                   1558:     {
1.98      vatton   1559: #ifdef TEMPLATE_DEBUG
1.90      vatton   1560:       DumpAllDeclarations();
1.98      vatton   1561: #endif /* TEMPLATE_DEBUG */  
1.90      vatton   1562:       iter = SearchSet_GetForwardIterator(GetComponents(t));
1.98      vatton   1563: #ifdef TEMPLATE_DEBUG
1.90      vatton   1564:       printf("Template_PreInstantiateComponents %s\n", t->uri);
1.98      vatton   1565: #endif /* TEMPLATE_DEBUG */  
1.90      vatton   1566:       ITERATOR_FOREACH(iter, SearchSetNode, node)
                   1567:         {
                   1568:           dec = (Declaration) node->elem;
1.92      vatton   1569:           ParseTemplate(t, GetComponentContent(dec), GetTemplateDocument(t), NULL, TRUE);
1.90      vatton   1570:         }
                   1571:       TtaFreeMemory(iter);
1.1       vatton   1572:     }
                   1573: #endif /* TEMPLATES */
                   1574: }
1.84      kia      1575: 
                   1576: /*----------------------------------------------------------------------
                   1577:   Template_SetName
                   1578:   Set the xt:component or xt:union element xt:name attribute.
                   1579:   Make it unique.
                   1580:   Return TRUE if the name is not modified.
                   1581:   ----------------------------------------------------------------------*/
1.96      vatton   1582: ThotBool Template_SetName (Document doc, Element el, const char *name, ThotBool withUndo)
1.84      kia      1583: {
                   1584: #ifdef TEMPLATES 
1.95      vatton   1585:   AttributeType attType;
                   1586:   Attribute     attr;
1.96      vatton   1587:   ThotBool      res, res2;
1.95      vatton   1588: 
                   1589:   if (doc && el && name)
1.84      kia      1590:     {
1.95      vatton   1591:       attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                   1592:       attType.AttrTypeNum = Template_ATTR_name;
                   1593:       attr = TtaGetAttribute(el, attType);
                   1594:       if (attr == NULL)
                   1595:         {
                   1596:           attr = TtaNewAttribute (attType);
                   1597:           TtaAttachAttribute (el, attr, doc);
1.96      vatton   1598:           if (withUndo)
                   1599:             TtaRegisterAttributeCreate (attr, el, doc);
1.95      vatton   1600:         }
1.96      vatton   1601:       if (withUndo)
                   1602:          TtaRegisterAttributeReplace(attr, el, doc);
1.95      vatton   1603:       TtaSetAttributeText (attr, name, el, doc);
1.96      vatton   1604:       res = TtaIsValidID (attr, TRUE);
                   1605:       res2 = !MakeUniqueName(el, doc, TRUE, FALSE);
                   1606:       return (res || res2);
                   1607:     }
                   1608: #endif /* TEMPLATES */
                   1609:   return FALSE;
                   1610: }
                   1611: 
                   1612: /*----------------------------------------------------------------------
                   1613:   Template_SetName
                   1614:   Set the xt:component or xt:union element xt:name attribute.
                   1615:   Make it unique.
                   1616:   Return TRUE if the name is not modified.
                   1617:   ----------------------------------------------------------------------*/
                   1618: ThotBool Template_SetLabel (Document doc, Element el, const char *label, ThotBool withUndo)
                   1619: {
                   1620: #ifdef TEMPLATES 
                   1621:   AttributeType attType;
                   1622:   Attribute     attr;
                   1623: 
                   1624:   if (doc && el && label)
                   1625:     {
                   1626:       attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                   1627:       attType.AttrTypeNum = Template_ATTR_title;
                   1628:       attr = TtaGetAttribute(el, attType);
                   1629:       if (attr == NULL)
                   1630:         {
                   1631:           attr = TtaNewAttribute (attType);
                   1632:           TtaAttachAttribute (el, attr, doc);
                   1633:           if (withUndo)
                   1634:             TtaRegisterAttributeCreate (attr, el, doc);
                   1635:         }
                   1636:       if (withUndo)
                   1637:          TtaRegisterAttributeReplace(attr, el, doc);
                   1638:       TtaSetAttributeText (attr, label, el, doc);
1.95      vatton   1639:       return TtaIsValidID (attr, TRUE);
1.84      kia      1640:     }
                   1641: #endif /* TEMPLATES */
                   1642:   return FALSE;
                   1643: }
                   1644: 

Webmaster