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

1.1       cvs         1: /*
                      2:  *
1.162     vatton      3:  *  COPYRIGHT INRIA and W3C, 1996-2008
1.1       cvs         4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
1.14      cvs         7:  
1.1       cvs         8: /*
1.51      francesc    9:  * Authors: Francesc Campoy Flores
1.201   ! vatton     10:  *          Emilien Kia
1.1       cvs        11:  *
                     12:  */
                     13: 
1.194     kia        14: 
1.1       cvs        15: #define THOT_EXPORT extern
                     16: #include "amaya.h"
                     17: #include "document.h"
1.99      kia        18: #include "undo.h"
1.90      kia        19: #include "containers.h"
                     20: #include "Elemlist.h"
1.92      kia        21: #include "templates.h"
                     22: 
1.46      vatton     23: #ifdef TEMPLATES
                     24: #include "Template.h"
1.52      vatton     25: #include "templateDeclarations.h"
1.140     vatton     26: 
                     27: #include "html2thot_f.h"
1.174     kia        28: #include "HTMLedit_f.h"
1.122     kia        29: #include "templates_f.h"
1.89      kia        30: #include "templateUtils_f.h"
1.67      quint      31: #include "templateLoad_f.h"
                     32: #include "templateDeclarations_f.h"
1.76      vatton     33: #include "templateInstantiate_f.h"
1.145     kia        34: #include "Templatebuilder_f.h"
1.28      tollenae   35: #include "appdialogue_wx.h"
1.29      tollenae   36: #include "init_f.h"
1.46      vatton     37: #include "wxdialogapi_f.h"
1.60      francesc   38: #include "AHTURLTools_f.h"
1.52      vatton     39: #endif /* TEMPLATES */
1.1       cvs        40: 
1.87      kia        41: #include "fetchXMLname_f.h"
1.83      kia        42: #include "MENUconf.h"
                     43: 
                     44: /* Paths from which looking for templates.*/
                     45: static Prop_Templates_Path *TemplateRepositoryPaths;
                     46: 
1.87      kia        47: /*----------------------------------------------------------------------
                     48:   IsTemplateInstanceDocument: Test if a document is a template instance
                     49:   doc : Document to test
                     50:   return : TRUE if the document is a template instance
                     51:   ----------------------------------------------------------------------*/
1.106     vatton     52: ThotBool IsTemplateInstanceDocument(Document doc)
                     53: {
1.87      kia        54: #ifdef TEMPLATES
1.171     kia        55:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                     56:   if(t)
1.176     vatton     57:     return (t->state & templInstance) != 0;
1.171     kia        58:   else
                     59:     return FALSE;
                     60: #else /* TEMPLATES */
1.88      cvs        61:   return FALSE;
1.171     kia        62: #endif /* TEMPLATES */ 
1.87      kia        63: }
                     64: 
1.83      kia        65: /*----------------------------------------------------------------------
1.109     kia        66:   IsTemplateDocument: Test if a document is a template (not an instance)
                     67:   doc : Document to test
1.167     kia        68:   return : TRUE if the document is a template
1.109     kia        69:   ----------------------------------------------------------------------*/
1.183     vatton     70: ThotBool IsTemplateDocument (Document doc)
1.109     kia        71: {
                     72: #ifdef TEMPLATES
1.171     kia        73:   XTigerTemplate t = GetXTigerDocTemplate(doc);
1.183     vatton     74:   if (t)
                     75:     return ((t->state & templTemplate) != 0);
1.171     kia        76:   else
                     77:     return FALSE;
1.183     vatton     78: #endif /* TEMPLATES */ 
1.171     kia        79:   return FALSE;
                     80: }
                     81: 
                     82: 
                     83: /*----------------------------------------------------------------------
                     84:   Test if a document is an internal template.
                     85:   (no instance is opened and it is not edited)
                     86:   ----------------------------------------------------------------------*/
                     87: ThotBool IsInternalTemplateDocument(Document doc)
                     88: {
                     89: #ifdef TEMPLATES
                     90:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                     91:   if(t)
                     92:     return (t->state&templInternal)!=0;
                     93:   else
                     94:     return FALSE;
                     95: #else /* TEMPLATES */
1.109     kia        96:   return FALSE;
1.171     kia        97: #endif /* TEMPLATES */ 
                     98: }
                     99: 
                    100: /*----------------------------------------------------------------------
                    101:   Return the URL of an instance template.
                    102:   ----------------------------------------------------------------------*/
                    103: char* GetDocumentInstanceTemplateUrl(Document doc)
                    104: {
                    105: #ifdef TEMPLATES
                    106:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                    107:   if(t)
                    108:     return t->base_uri;
                    109:   else
                    110:     return FALSE;
                    111: #else /* TEMPLATES */
                    112:   return NULL;
                    113: #endif /* TEMPLATES */ 
1.109     kia       114: }
                    115: 
1.146     vatton    116: /*----------------------------------------------------------------------
                    117:   CheckPromptIndicator checks if the element is a prompt text unit
                    118:   ----------------------------------------------------------------------*/
                    119: ThotBool CheckPromptIndicator (Element el, Document doc)
                    120: {
                    121: #ifdef TEMPLATES
                    122:   ElementType     elType;
                    123:   Element         parent;
                    124:   AttributeType   attrType;
                    125:   Attribute       att;
                    126:   SSchema         templateSSchema;
                    127: 
1.173     vatton    128:   if (!IsTemplateInstanceDocument(doc))
                    129:     /* let Thot perform normal operation */
                    130:     return FALSE;
1.146     vatton    131:   elType = TtaGetElementType (el);
                    132:   templateSSchema = TtaGetSSchema ("Template", doc);
                    133:        if (elType.ElTypeNum == HTML_EL_TEXT_UNIT)
                    134:     {
                    135:       parent = TtaGetParent (el);
                    136:       elType = TtaGetElementType (parent);
                    137:       while (parent && elType.ElSSchema != templateSSchema)
                    138:         {
                    139:           parent = TtaGetParent (parent);
                    140:           elType = TtaGetElementType (parent);
                    141:         }
                    142:       if (parent &&
                    143:           (elType.ElTypeNum == Template_EL_useEl ||
                    144:            elType.ElTypeNum == Template_EL_useSimple))
                    145:         {
                    146:           // there is a parent template use
                    147:           attrType.AttrSSchema = elType.ElSSchema;
                    148:           attrType.AttrTypeNum = Template_ATTR_prompt;
                    149:           att = TtaGetAttribute (parent, attrType);
                    150:           if (att)
1.151     quint     151:             {
                    152:               TtaSelectElement (doc, el);
                    153:               return TRUE;
                    154:             }
1.146     vatton    155:         }
                    156:     }
                    157: #endif /* TEMPLATES */
1.173     vatton    158:   /* let Thot perform normal operation */
1.146     vatton    159:   return FALSE;
                    160: }
                    161: 
                    162: /*----------------------------------------------------------------------
                    163:   RemovePromptIndicator removes the enclosing prompt indicator
                    164:   ----------------------------------------------------------------------*/
1.153     vatton    165: ThotBool RemovePromptIndicator (NotifyOnTarget *event)
1.146     vatton    166: {
                    167: #ifdef TEMPLATES
                    168:   ElementType     elType;
1.153     vatton    169:   Element         parent, el;
1.146     vatton    170:   AttributeType   attrType;
                    171:   Attribute       att;
1.153     vatton    172:   Document        doc;
1.146     vatton    173:   SSchema         templateSSchema;
                    174: 
1.153     vatton    175:   el = event->element;
                    176:   doc = event->document;
1.146     vatton    177:   elType = TtaGetElementType (el);
                    178:   templateSSchema = TtaGetSSchema ("Template", doc);
1.153     vatton    179:   parent = TtaGetParent (el);
                    180:   elType = TtaGetElementType (parent);
                    181:   while (parent && elType.ElSSchema != templateSSchema)
1.146     vatton    182:     {
1.153     vatton    183:       parent = TtaGetParent (parent);
1.146     vatton    184:       elType = TtaGetElementType (parent);
1.153     vatton    185:     }
                    186:   if (parent &&
                    187:       (elType.ElTypeNum == Template_EL_useEl ||
                    188:        elType.ElTypeNum == Template_EL_useSimple))
                    189:     {
                    190:       // there is a parent template use
                    191:       attrType.AttrSSchema = elType.ElSSchema;
                    192:       attrType.AttrTypeNum = Template_ATTR_prompt;
                    193:       att = TtaGetAttribute (parent, attrType);
                    194:       if (att)
1.146     vatton    195:         {
1.153     vatton    196:           TtaRegisterAttributeDelete (att, parent, doc);
                    197:           TtaRemoveAttribute (parent, att, doc);
1.146     vatton    198:         }
                    199:     }
                    200: #endif /* TEMPLATES */
1.153     vatton    201:   return FALSE;                /* let Thot perform normal operation */
1.146     vatton    202: }
                    203: 
1.109     kia       204: 
                    205: /*----------------------------------------------------------------------
1.108     vatton    206:   AllocTemplateRepositoryListElement: allocates an element for the list
                    207:   of template repositories.
1.83      kia       208:   path : path of the new element
                    209:   return : address of the new element
                    210:   ----------------------------------------------------------------------*/
                    211: void* AllocTemplateRepositoryListElement (const char* path, void* prevElement)
                    212: {
1.129     vatton    213:   Prop_Templates_Path *element;
                    214: 
                    215:   element  = (Prop_Templates_Path*)TtaGetMemory (sizeof(Prop_Templates_Path));
                    216:   memset (element, 0, sizeof(Prop_Templates_Path));
                    217:   strncpy (element->Path, path, MAX_LENGTH - 1);
1.83      kia       218:   if (prevElement)
1.129     vatton    219:     {
                    220:       element->NextPath = ((Prop_Templates_Path*)prevElement)->NextPath;
                    221:       ((Prop_Templates_Path*)prevElement)->NextPath = element;
                    222:     }
1.83      kia       223:   return element;
                    224: }
                    225: 
                    226: 
                    227: /*----------------------------------------------------------------------
                    228:   FreeTemplateRepositoryList: Free the list of template repositories.
                    229:   list : address of the list (address of the first element).
                    230:   ----------------------------------------------------------------------*/
                    231: void FreeTemplateRepositoryList (void* list)
                    232: {
1.131     vatton    233:   Prop_Templates_Path **l = (Prop_Templates_Path**) list;
                    234:   Prop_Templates_Path  *element = *l;
                    235: 
1.83      kia       236:   l = NULL;
                    237:   while (element)
                    238:   {
                    239:     Prop_Templates_Path* next = element->NextPath;
1.131     vatton    240:     TtaFreeMemory (element);
1.83      kia       241:     element = next;
                    242:   }
                    243: }
                    244: 
                    245: /*----------------------------------------------------------------------
                    246:   CopyTemplateRepositoryList: Copy a list of template repositories.
                    247:   src : address of the list (address of the first element).
                    248:   dst : address where copy the list
                    249:   ----------------------------------------------------------------------*/
1.91      vatton    250: static void CopyTemplateRepositoryList (const Prop_Templates_Path** src,
                    251:                                         Prop_Templates_Path** dst)
1.83      kia       252: {
                    253:   Prop_Templates_Path *element=NULL, *current=NULL;
                    254:   
1.131     vatton    255:   if (*src)
1.83      kia       256:   {
                    257:     *dst = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    258:     (*dst)->NextPath = NULL;
                    259:     strcpy((*dst)->Path, (*src)->Path);
                    260:     
                    261:     element = (*src)->NextPath;
                    262:     current = *dst;
                    263:   }
                    264: 
1.106     vatton    265:   while (element)
                    266:     {
1.83      kia       267:     current->NextPath = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    268:     current = current->NextPath; 
                    269:     current->NextPath = NULL;
                    270:     strcpy(current->Path, element->Path);
                    271:     element = element->NextPath;
1.106     vatton    272:     }
1.83      kia       273: }
                    274: 
                    275: /*----------------------------------------------------------------------
                    276:   LoadTemplateRepositoryList: Load the list of template repositories.
                    277:   list   : address of the list (address of the first element).
                    278:   return : the number of readed repository paths.
                    279:   ----------------------------------------------------------------------*/
                    280: static int LoadTemplateRepositoryList (Prop_Templates_Path** list)
                    281: {
                    282:   Prop_Templates_Path *element, *current = NULL;
                    283:   char *path, *homePath;
                    284:   unsigned char *c;
                    285:   int nb = 0;
                    286:   FILE *file;
                    287:   
1.131     vatton    288:   //clean up the curent list
                    289:   FreeTemplateRepositoryList (list);
                    290: 
                    291:   // open the file
1.83      kia       292:   path = (char *) TtaGetMemory (MAX_LENGTH);
1.86      vatton    293:   homePath       = TtaGetEnvString ("APP_HOME");
1.106     vatton    294:   sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83      kia       295:   file = TtaReadOpen ((char *)path);
1.84      kia       296:   if (!file)
                    297:   {
                    298:     /* The config file dont exist, create it. */
                    299:     file = TtaWriteOpen ((char *)path);
1.122     kia       300:     fprintf (file, "http://www.w3.org/Amaya/Templates/cv.xtd\n");
1.144     vatton    301:     fprintf (file, "http://www.w3.org/Amaya/Templates/slides.xtd\n");
                    302:     fprintf (file, "http://www.w3.org/Amaya/Templates/ACM-Proc-Article.xtd\n");
1.84      kia       303:     TtaWriteClose (file);
                    304:     /* Retry to open it.*/
                    305:     file = TtaReadOpen ((char *)path);
                    306:   }
                    307:   
1.83      kia       308:   if (file)
1.129     vatton    309:     {
1.131     vatton    310:       // read the file
1.129     vatton    311:       c = (unsigned char*)path;
                    312:       *c = EOS;
                    313:       while (TtaReadByte (file, c))
                    314:         {
                    315:           if (*c == 13 || *c == EOL)
                    316:             *c = EOS;
                    317:           if (*c == EOS && c != (unsigned char*)path )
                    318:             {
                    319:               element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    320:               element->NextPath = NULL;
                    321:               strcpy (element->Path, path);
1.148     kia       322: 
1.129     vatton    323:               if (*list == NULL)
                    324:                 *list = element; 
                    325:               else
                    326:                 current->NextPath = element;
                    327:               current = element;
                    328:               nb++;
1.83      kia       329: 
1.129     vatton    330:               c = (unsigned char*) path;
                    331:               *c = EOS;
                    332:             }
                    333:           else
                    334:             c++;
                    335:         }
                    336:       if (c != (unsigned char*)path && *path != EOS)
                    337:         {
                    338:           element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    339:           *(c+1) = EOS;
                    340:           strcpy (element->Path, path);
                    341:           element->NextPath = NULL;
1.148     kia       342: 
1.129     vatton    343:           if (*list == NULL)
                    344:             *list = element; 
                    345:           else
                    346:             current->NextPath = element;
                    347:           nb++;
                    348:         }
                    349:       TtaReadClose (file);
1.83      kia       350:     }
                    351:   TtaFreeMemory(path);
                    352:   return nb;
                    353: }
                    354: 
                    355: /*----------------------------------------------------------------------
                    356:   SaveTemplateRepositoryList: Save the list of template repositories.
                    357:   list   : address of the list (address of the first element).
                    358:   ----------------------------------------------------------------------*/
                    359: static void SaveTemplateRepositoryList (const Prop_Templates_Path** list)
                    360: {
                    361:   const Prop_Templates_Path *element;
                    362:   char *path, *homePath;
                    363:   unsigned char *c;
                    364:   FILE *file;
                    365: 
                    366:   path = (char *) TtaGetMemory (MAX_LENGTH);
                    367:   homePath       = TtaGetEnvString ("APP_HOME");
1.106     vatton    368:   sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83      kia       369: 
                    370:   file = TtaWriteOpen ((char *)path);
                    371:   c = (unsigned char*)path;
                    372:   *c = EOS;
                    373:   if (file)
                    374:   {
                    375:     element = *list;
                    376:     while (element)
                    377:     {
                    378:       fprintf(file, "%s\n", element->Path);
                    379:       element = element->NextPath;
                    380:     }
                    381:     TtaWriteClose (file);
                    382:   }
                    383: }
                    384: 
                    385: /*----------------------------------------------------------------------
                    386:   GetTemplateRepositoryList: Get the list of template repositories from template environment.
                    387:   list : address of the list (address of the first element).
                    388:   ----------------------------------------------------------------------*/
                    389: void GetTemplateRepositoryList (void* list)
                    390: {
                    391:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                    392:   CopyTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths, l);
                    393: }
                    394: 
                    395: /*----------------------------------------------------------------------
                    396:   SetTemplateRepositoryList: Set the list of template repositories environment.
                    397:   list : address of the list (address of the first element).
                    398:   ----------------------------------------------------------------------*/
                    399: void SetTemplateRepositoryList (const void* list)
                    400: {
                    401:   const Prop_Templates_Path** l = (const Prop_Templates_Path**) list;
                    402:   CopyTemplateRepositoryList((const Prop_Templates_Path**)l, &TemplateRepositoryPaths);
                    403:   SaveTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths);
                    404: }
                    405: 
                    406: /*-----------------------------------------------------------------------
                    407:    InitTemplates
                    408:    Initializes the annotation library
                    409:   -----------------------------------------------------------------------*/
                    410: void InitTemplates ()
                    411: {
                    412:   TtaSetEnvBoolean ("SHOW_TEMPLATES", TRUE, FALSE);
1.131     vatton    413:   LoadTemplateRepositoryList (&TemplateRepositoryPaths);
1.83      kia       414: }
                    415: 
                    416: 
1.1       cvs       417: /*----------------------------------------------------------------------
1.108     vatton    418:   Load a template and create the instance file - update images and 
                    419:   stylesheets related to the template.
                    420:   ----------------------------------------------------------------------*/
                    421: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
                    422: {
                    423: #ifdef TEMPLATES
1.170     vatton    424:   DocumentType docType;
                    425:   int          len, i;
                    426:   char        *s;
                    427:   char              suffix[6];
                    428:   ThotBool     dontReplace = DontReplaceOldDoc;
1.108     vatton    429: 
                    430:   if (!IsW3Path (docname) && TtaFileExist (docname))
                    431:     {
                    432:       s = (char *)TtaGetMemory (strlen (docname) +
                    433:                                 strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
                    434:       sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
                    435:       InitConfirm (0, 0, s);
                    436:       TtaFreeMemory (s);
                    437:       if (!UserAnswer)
                    438:         return;
                    439:     }
1.170     vatton    440:   docType = LoadTemplate (0, templatename);
                    441:   if (docType != docFree)
1.165     kia       442:     {
1.170     vatton    443:       /* check if the file suffix is conform to the document type */
                    444:       s = (char *)TtaGetMemory (strlen (docname) + 10);
                    445:       strcpy (s, docname);
                    446:       if (!IsXMLName (docname))
                    447:         {
                    448:           // by default no suffix is added
                    449:           suffix[0] = EOS;
                    450:           if (IsMathMLName (docname) && docType != docMath)
                    451:             strcpy (suffix, "mml");
                    452:           else if (IsSVGName (docname) && docType != docSVG)
                    453:             strcpy (suffix, "svg");
                    454:           else if (IsHTMLName (docname) && docType != docHTML)
                    455:             strcpy (suffix, "xml");
                    456:           if (suffix[0] != EOS)
                    457:             {
                    458:               // change or update the suffix
                    459:               len = strlen (s);
                    460:               for (i = len-1; i > 0 && s[i] != '.'; i--);
                    461:               if (s[i] != '.')
                    462:                 {
                    463:                   /* there is no suffix */
                    464:                   s[i++] = '.';
                    465:                   strcpy (&s[i], suffix);
                    466:                 }
                    467:               else
                    468:                 {
                    469:                   /* there is a suffix */
                    470:                   i++;
                    471:                   strcpy (&s[i], suffix);
                    472:                 }
                    473:             }
                    474:         }
                    475:       // now create the instance
1.165     kia       476:       DontReplaceOldDoc = dontReplace;
1.177     vatton    477:       CreateInstance (templatename, s, docname, docType, doc);
1.170     vatton    478:       TtaFreeMemory (s);
1.165     kia       479:     }
1.108     vatton    480: #endif /* TEMPLATES */
                    481: }
                    482: 
1.53      vatton    483: 
1.109     kia       484: 
1.52      vatton    485: /*----------------------------------------------------------------------
1.107     kia       486:   PreventReloadingTemplate
                    487:   Prevent reloading a template.
                    488:   You must call AllowReloadingTemplate when finish.
                    489:   Usefull for reload an instance without reloading the template.
                    490:   ----------------------------------------------------------------------*/
                    491: void PreventReloadingTemplate(char* template_url)
                    492: {
                    493: #ifdef TEMPLATES
1.178     kia       494:   Template_AddReference(GetXTigerTemplate (template_url));
1.107     kia       495: #endif /* TEMPLATES */
                    496: }
                    497: 
                    498: /*----------------------------------------------------------------------
                    499:   AllowReloadingTemplate
                    500:   Allow reloading a template.
                    501:   You must call it after each PreventReloadingTemplate call.
                    502:   ----------------------------------------------------------------------*/
                    503: void AllowReloadingTemplate(char* template_url)
                    504: {
                    505: #ifdef TEMPLATES
1.178     kia       506:   Template_RemoveReference(GetXTigerTemplate (template_url));
1.107     kia       507: #endif /* TEMPLATES */  
                    508: }
                    509: 
                    510: 
1.137     vatton    511: /*----------------------------------------------------------------------
                    512:   ----------------------------------------------------------------------*/
1.134     kia       513: ThotBool isEOSorWhiteSpace (const char c)
                    514: {
1.137     vatton    515:   return c == SPACE || c == TAB || c ==  EOL || c ==__CR__ || c == EOS;
                    516: }
                    517: ThotBool isWhiteSpace (const char c)
                    518: {
                    519:   return c == SPACE || c == TAB || c == EOL || c ==__CR__;
1.134     kia       520: }
                    521: 
1.107     kia       522: /*----------------------------------------------------------------------
1.87      kia       523:   giveItems : Lists type items from string
                    524:   example : "one two three" is extracted to {one, two, three}
                    525:   note : item type are setted to SimpleTypeNat
                    526:   text : text from which list items
                    527:   size : size of text in characters
                    528:   items : address of exctracted item list
                    529:   nbitems : items number in items list
1.52      vatton    530:   ----------------------------------------------------------------------*/
1.76      vatton    531: void giveItems (char *text, int size, struct menuType **items, int *nbitems)
1.1       cvs       532: {
1.70      quint     533: #ifdef TEMPLATES
1.148     kia       534:   ThotBool         inElement = TRUE;
1.52      vatton    535:   struct menuType *menu;
                    536:   char            *iter;
1.148     kia       537:   char             temp[128];
1.52      vatton    538:   int              i;
1.148     kia       539:   int              labelSize;
1.28      tollenae  540: 
1.148     kia       541:   *nbitems = 1;
                    542:   for (i = 0; i < size; i++)
1.52      vatton    543:     {
                    544:       if (isEOSorWhiteSpace (text[i]))
                    545:         {
                    546:           if (inElement)
                    547:             inElement = FALSE;
                    548:         }
                    549:       else if (!inElement)
                    550:         {
                    551:           inElement = TRUE;
                    552:           (*nbitems)++;
                    553:         }
                    554:     }
1.51      francesc  555: 
1.148     kia       556:   menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
                    557:   iter = text;
                    558:   for (i = 0; i < *nbitems; i++)
                    559:     {
1.52      vatton    560:       labelSize = 0;
1.137     vatton    561:       while (isWhiteSpace (*iter))
1.52      vatton    562:         iter++;
1.137     vatton    563:       if (*iter != EOS)
                    564:         {
                    565:           while (!isEOSorWhiteSpace (*iter))
                    566:             {
                    567:               temp[labelSize++] = *iter;
                    568:               iter++;
                    569:             }
1.52      vatton    570: 
1.137     vatton    571:           temp[labelSize] = EOS;
                    572:           menu[i].label = (char *) TtaStrdup (temp);
                    573:           menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
1.52      vatton    574:         }
                    575:     }
1.137     vatton    576:   *items = menu;
1.70      quint     577: #endif /* TEMPLATES */
1.28      tollenae  578: }
1.37      tollenae  579: 
1.70      quint     580: #ifdef TEMPLATES
1.52      vatton    581: /*----------------------------------------------------------------------
                    582:   ----------------------------------------------------------------------*/
                    583: static char *createMenuString (const struct menuType* items, const int nbItems)
                    584: {
                    585:   char *result, *iter;
1.148     kia       586:   int   size = 0;
1.52      vatton    587:   int   i;
                    588: 
1.148     kia       589:   for (i=0; i < nbItems; i++)
                    590:     size += 2 + strlen (items[i].label);
1.52      vatton    591: 
1.148     kia       592:   result = (char *) TtaGetMemory (size);
                    593:   iter = result;
                    594:   for (i=0; i < nbItems; i++)
1.52      vatton    595:     {
                    596:       *iter = 'B';
                    597:       ++iter;
1.148     kia       598: 
1.52      vatton    599:       strcpy (iter, items[i].label);
                    600:       iter += strlen (items[i].label)+1;
                    601:     }
1.148     kia       602:   return result;
1.36      tollenae  603: }
1.71      quint     604: #endif /* TEMPLATES */
1.29      tollenae  605: 
1.185     kia       606: 
                    607: /*----------------------------------------------------------------------
1.71      quint     608:   UseToBeCreated
                    609:   An new use element will be created by the user through some generic editing
                    610:   command
                    611:   -----------------------------------------------------------------------*/
                    612: ThotBool UseToBeCreated (NotifyElement *event)
                    613: {
                    614: #ifdef TEMPLATES
1.122     kia       615:   ElementType   parentType;
1.138     vatton    616:   SSchema       templateSSchema = TtaGetSSchema ("Template", event->document);  
1.130     vatton    617:   if (templateSSchema)
1.122     kia       618:   {
1.130     vatton    619:     parentType = TtaGetElementType (event->element);
                    620:     if (parentType.ElSSchema == templateSSchema &&
                    621:         parentType.ElTypeNum == Template_EL_repeat)
                    622:       return !Template_CanInsertRepeatChild (event->element);
                    623:     return TemplateElementWillBeCreated (event);
1.122     kia       624:   }
1.52      vatton    625: #endif /* TEMPLATES */
1.71      quint     626:   return FALSE; /* let Thot perform normal operation */
                    627: }
                    628: 
                    629: /*----------------------------------------------------------------------
                    630:   UseCreated
                    631:   A new "use" element has just been created by the user with a generic editing
                    632:   command.
                    633:   -----------------------------------------------------------------------*/
                    634: void UseCreated (NotifyElement *event)
                    635: {
                    636: #ifdef TEMPLATES
1.148     kia       637:   Document        doc = event->document;
                    638:   Element         el = event->element;
1.117     kia       639:   Element         parent;
                    640:   Element         first;
                    641:   ElementType     parentType;
                    642:   XTigerTemplate  t;
                    643:   SSchema         templateSSchema;
1.130     vatton    644:   char*           types, *text = NULL;
1.148     kia       645: 
1.112     vatton    646:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       647:     return;
                    648: 
1.72      quint     649:   if (TtaGetFirstChild (el))
                    650:     /* this Use element has already some content. It has already been
                    651:        instanciated */
                    652:     return;
1.117     kia       653: 
1.171     kia       654:   t = GetXTigerDocTemplate (doc);
1.71      quint     655:   if (!t)
                    656:     return; // no template ?!?!
1.101     kia       657: 
1.138     vatton    658:   templateSSchema = TtaGetSSchema ("Template", doc);
1.117     kia       659:   parent = TtaGetParent(el);
                    660:   parentType = TtaGetElementType(parent);
1.148     kia       661: 
1.130     vatton    662:   if (parentType.ElSSchema == templateSSchema &&
                    663:       parentType.ElTypeNum == Template_EL_repeat)
1.117     kia       664:   {
1.130     vatton    665:     first = TtaGetFirstChild (parent);
                    666:     if (first)
1.117     kia       667:     {
1.130     vatton    668:       types = GetAttributeStringValueFromNum (first, Template_ATTR_types, NULL);
                    669:       if (types)
1.117     kia       670:       {
1.130     vatton    671:         SetAttributeStringValueWithUndo (el, Template_ATTR_types, types);
                    672:         text = GetAttributeStringValueFromNum (el, Template_ATTR_title, NULL);
                    673:         SetAttributeStringValueWithUndo (first, Template_ATTR_title, text);
                    674:         TtaFreeMemory (text);
                    675:         text = GetAttributeStringValueFromNum (el, Template_ATTR_currentType, NULL);
                    676:         SetAttributeStringValueWithUndo (first, Template_ATTR_currentType, text);
                    677:         TtaFreeMemory (text);
                    678:         TtaFreeMemory (types);
1.117     kia       679:       }
                    680:     }
                    681:   }
                    682: 
1.76      vatton    683:   InstantiateUse (t, el, doc, TRUE);
1.71      quint     684: #endif /* TEMPLATES */
                    685: }
1.29      tollenae  686: 
1.89      kia       687: 
1.98      kia       688: /*----------------------------------------------------------------------
                    689:   Template_CanInsertRepeatChild
                    690:   Test if a xt:repeat child can be inserted (number between params min and max).
                    691:   @param el element (xt:repeat) to test
                    692:   @return True if an element can be inserted.
                    693:   ----------------------------------------------------------------------*/
                    694: ThotBool Template_CanInsertRepeatChild(Element el)
                    695: {
                    696: #ifdef TEMPLATES
1.154     vatton    697:   char     *max;
                    698:   int       maxVal, curVal;
                    699:   Element   child;
1.148     kia       700: 
1.104     kia       701:   max = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
1.191     kia       702:   if (max && max[0]!=EOS)
1.104     kia       703:     {
1.154     vatton    704:       if (!strcmp(max, "*"))
                    705:         {
                    706:           TtaFreeMemory(max);
                    707:           return TRUE;
                    708:         }
                    709:       maxVal = atoi (max);
                    710:       TtaFreeMemory (max);
1.104     kia       711:       curVal = 0;
1.105     vatton    712:       for (child = TtaGetFirstChild(el); child; TtaNextSibling(&child))
1.104     kia       713:         curVal++;
1.154     vatton    714:       return (curVal < maxVal);
1.104     kia       715:     }
                    716:   else
1.98      kia       717:     return TRUE;
                    718: #endif /* TEMPLATES */
                    719:   return FALSE;
                    720: }
1.96      kia       721: 
1.89      kia       722: 
1.92      kia       723: #ifdef TEMPLATES
1.99      kia       724: /*----------------------------------------------------------------------
                    725:   QueryStringFromMenu
                    726:   Show a context menu to query a choice.
                    727:   @param items space-separated choice list string.
                    728:   @return The choosed item string or NULL if none.
                    729:   ----------------------------------------------------------------------*/
1.138     vatton    730: static char* QueryStringFromMenu (Document doc, char* items)
1.90      kia       731: {
                    732:   int nbitems, size;
                    733:   struct menuType *itemlist;
                    734:   char *menuString;
                    735:   char *result = NULL;
1.148     kia       736: 
1.138     vatton    737:   if (!TtaGetDocumentAccessMode (doc))
1.112     vatton    738:     return NULL;
                    739:   if (items == NULL)
1.110     kia       740:     return NULL;
1.138     vatton    741:   size = strlen (items);
                    742:   if (size == 0)
                    743:     return NULL;
1.90      kia       744:   giveItems (items, size, &itemlist, &nbitems);
                    745:   menuString = createMenuString (itemlist, nbitems);
                    746:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    747:                      nbitems, menuString , NULL, false, 'L');
                    748:   TtaFreeMemory (menuString);
                    749:   ReturnOption = -1;
                    750:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    751:   TtaWaitShowProcDialogue ();
                    752:   TtaDestroyDialogue (BaseDialog + OptionMenu);
1.148     kia       753: 
1.112     vatton    754:   if (ReturnOption!=-1)
1.90      kia       755:   {
                    756:     result = TtaStrdup(itemlist[ReturnOption].label);
                    757:   }
                    758:   
                    759:   TtaFreeMemory (itemlist);
                    760:   return result;
                    761: }
1.92      kia       762: #endif /* TEMPLATES */
1.90      kia       763: 
1.158     kia       764: #ifdef AMAYA_DEBUG
                    765: void FillInsertableElemList (Document doc, Element elem, DLList list);
                    766: #endif /* AMAYA_DEBUG */
1.162     vatton    767: /*----------------------------------------------------------------------
                    768:   ----------------------------------------------------------------------*/
                    769: char *Template_GetListTypes (XTigerTemplate t, Element el)
                    770: {
                    771: #ifdef TEMPLATES
                    772:   char  *listtypes = NULL, *types;
                    773: 
                    774:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
                    775:   if (types)
                    776:     listtypes = Template_ExpandTypes (t, types, el, FALSE);
                    777:   return listtypes;
                    778: #endif /* TEMPLATES */
                    779: }
1.158     kia       780: 
1.56      francesc  781: /*----------------------------------------------------------------------
1.138     vatton    782:   BagButtonClicked
                    783:   Called when a bag button is clicked.
                    784:   Can be called for useEl, useSimple or bag.
                    785:   If called for useEl or useSimple, the new element must be added after.
                    786:   If called for bag, the element must be added before all.
                    787:    
                    788:   Shows a menu with all the types that can be used in the bag.
                    789:   ----------------------------------------------------------------------*/
                    790: ThotBool BagButtonClicked (NotifyElement *event)
                    791: {
                    792: #ifdef TEMPLATES
                    793:   Document        doc = event->document;
                    794:   Element         el = event->element;
                    795:   ElementType     elType;
                    796:   XTigerTemplate  t;
                    797:   Declaration     decl;
                    798:   Element         bagEl = el;
                    799:   Element         firstEl;
                    800:   Element         newEl = NULL;
                    801:   View            view;
                    802:   SSchema         templateSSchema;
1.162     vatton    803:   char           *listtypes = NULL;
                    804:   char           *result = NULL;
1.138     vatton    805:   ThotBool        oldStructureChecking;
                    806:   DisplayMode     dispMode;
                    807: 
1.182     vatton    808:   if (!TtaGetDocumentAccessMode (doc))
1.138     vatton    809:     return TRUE;
1.182     vatton    810:   if (!IsTemplateInstanceDocument (doc))
                    811:     return FALSE; /* let Thot perform normal operation */
1.148     kia       812: 
1.138     vatton    813:   TtaGetActiveView (&doc, &view);
                    814:   if (view != 1)
                    815:     return FALSE; /* let Thot perform normal operation */
                    816: 
                    817:   TtaCancelSelection (doc);
                    818:   templateSSchema = TtaGetSSchema ("Template", doc);  
1.171     kia       819:   t = GetXTigerDocTemplate(doc);
1.138     vatton    820:   elType = TtaGetElementType (el);
                    821:   while (bagEl &&
                    822:          (elType.ElSSchema != templateSSchema ||
                    823:           elType.ElTypeNum != Template_EL_bag))
                    824:     {
                    825:       bagEl = TtaGetParent (bagEl);
                    826:       elType = TtaGetElementType (bagEl);
                    827:     }
                    828: 
                    829:   if (bagEl)
                    830:   {
1.162     vatton    831:     listtypes = Template_GetListTypes (t, bagEl);
                    832:     if (listtypes)
1.138     vatton    833:       {
1.158     kia       834: #ifdef AMAYA_DEBUG
                    835:       printf("BagButtonClicked : \n  > %s\n", listtypes);
                    836: //      {
                    837: //        DLList list = DLList_Create();
                    838: //        FillInsertableElemList (doc, TtaGetFirstChild(bagEl), list);
                    839: //        DLListNode node;
                    840: //        ForwardIterator iter = DLList_GetForwardIterator(list);
                    841: //        ITERATOR_FOREACH(iter, DLListNode, node)
                    842: //        {
                    843: //          ElemListElement elem = (ElemListElement)node->elem;
                    844: //          printf("  + %s\n", ElemListElement_GetName(elem));
                    845: //        }
                    846: //        DLList_Destroy(list);
                    847: //      }
                    848: #endif /* AMAYA_DEBUG */
1.138     vatton    849:         result = QueryStringFromMenu (doc, listtypes);
                    850:         TtaFreeMemory (listtypes);
                    851:         if (result)
                    852:           {
                    853:             decl = Template_GetDeclaration (t, result);
                    854:             if (decl)
                    855:               {
                    856:                 dispMode = TtaGetDisplayMode (doc);
                    857:                 if (dispMode == DisplayImmediately)
                    858:                   /* don't set NoComputedDisplay
                    859:                      -> it breaks down views formatting when Enter generates new elements  */
1.139     vatton    860:                   TtaSetDisplayMode (doc, DeferredDisplay);
1.138     vatton    861: 
                    862:                 /* Prepare insertion.*/          
                    863:                 oldStructureChecking = TtaGetStructureChecking (doc);
                    864:                 TtaSetStructureChecking (FALSE, doc);
                    865:                 TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    866: 
                    867:                 /* Insert */
                    868:                 if (el == bagEl)
                    869:                   {
                    870:                     el = TtaGetFirstChild (el);
                    871:                     TtaSelectElement (doc, el);
                    872:                     TtaInsertAnyElement (doc, TRUE);
                    873:                   }
                    874:                 else
                    875:                   {
                    876:                     TtaSelectElement (doc, el);
                    877:                     TtaInsertAnyElement (doc, FALSE);
                    878:                   }
1.155     kia       879:                 newEl = Template_InsertBagChild (doc, bagEl, decl, FALSE);
1.138     vatton    880: 
                    881:                 /* Finish insertion.*/
                    882:                 TtaCloseUndoSequence (doc);
                    883:                 TtaSetDocumentModified (doc);
                    884:                 TtaSetStructureChecking (oldStructureChecking, doc);
                    885:                 // restore the display
                    886:                 TtaSetDisplayMode (doc, dispMode);
                    887:                 firstEl = GetFirstEditableElement (newEl);
                    888:                 if (firstEl)
                    889:                   {
                    890:                     TtaSelectElement (doc, firstEl);
                    891:                     TtaSetStatusSelectedElement (doc, view, firstEl);
                    892:                   }
                    893:                 else
                    894:                   {
                    895:                     TtaSelectElement (doc, newEl);
                    896:                     TtaSetStatusSelectedElement (doc, view, newEl);
                    897:                   }
                    898:               }
                    899:           }
                    900:       }
                    901:     TtaFreeMemory (result);
                    902:   }
                    903: #endif /* TEMPLATES */
                    904:   return TRUE; /* don't let Thot perform normal operation */
                    905: }
                    906: 
                    907: /*----------------------------------------------------------------------
1.79      quint     908:   RepeatButtonClicked
1.89      kia       909:   Called when a repeat button is clicked.
                    910:   Can be called for useEl, useSimple or repeat.
                    911:   If called for useEl or useSimple, the new element must be added after.
                    912:   If called for repeat, the element must be added before all.
                    913:    
1.56      francesc  914:   Shows a menu with all the types that can be used in a use element.
                    915:   ----------------------------------------------------------------------*/
1.79      quint     916: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  917: {
                    918: #ifdef TEMPLATES
1.89      kia       919:   Document        doc = event->document;
                    920:   Element         el = event->element;
                    921:   ElementType     elType;
1.90      kia       922:   XTigerTemplate  t;
                    923:   Declaration     decl;
                    924:   Element         repeatEl = el;
                    925:   Element         firstEl;
                    926:   Element         newEl = NULL;
1.95      kia       927:   View            view;
1.162     vatton    928:   char           *listtypes = NULL;
                    929:   char           *result = NULL;
1.138     vatton    930:   ThotBool        oldStructureChecking;
                    931:   DisplayMode     dispMode;
1.104     kia       932: 
1.112     vatton    933:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       934:     return TRUE;
1.182     vatton    935:   if (!IsTemplateInstanceDocument (doc))
                    936:     return FALSE; /* let Thot perform normal operation */
1.148     kia       937: 
1.95      kia       938:   TtaGetActiveView (&doc, &view);
                    939:   if (view != 1)
                    940:     return FALSE; /* let Thot perform normal operation */
                    941: 
1.89      kia       942:   TtaCancelSelection(doc);
1.171     kia       943:   t = GetXTigerDocTemplate(doc);
1.89      kia       944:   elType = TtaGetElementType(el);
1.138     vatton    945:   while (elType.ElTypeNum != Template_EL_repeat)
1.179     kia       946:     {
                    947:       repeatEl = TtaGetParent(repeatEl);
                    948:       if (repeatEl == NULL)
                    949:         break;
                    950:       elType = TtaGetElementType(repeatEl);
                    951:     }
1.112     vatton    952:   if (repeatEl)
1.90      kia       953:     {
1.179     kia       954:       if (Template_CanInsertRepeatChild (repeatEl))
                    955:         {
                    956:           firstEl = TtaGetFirstChild (repeatEl);
                    957:           listtypes = Template_GetListTypes (t, firstEl);
                    958:           if (listtypes)
                    959:             {
1.158     kia       960: #ifdef AMAYA_DEBUG
1.179     kia       961:               printf("RepeatButtonClicked : \n  > %s\n", listtypes);
1.158     kia       962: #endif /* AMAYA_DEBUG */
1.179     kia       963: 
                    964:               result = QueryStringFromMenu (doc, listtypes);
                    965:               TtaFreeMemory (listtypes);
                    966:               if (result)
                    967:                 {
                    968:                   decl = Template_GetDeclaration (t, result);
                    969:                   if (decl)
                    970:                     {
                    971:                       dispMode = TtaGetDisplayMode (doc);
                    972:                       if (dispMode == DisplayImmediately)
                    973:                         /* don't set NoComputedDisplay
1.138     vatton    974:                  -> it breaks down views formatting when Enter generates new elements  */
1.179     kia       975:                         TtaSetDisplayMode (doc, DeferredDisplay);
                    976: 
                    977:                       /* Prepare insertion.*/          
                    978:                       oldStructureChecking = TtaGetStructureChecking (doc);
                    979:                       TtaSetStructureChecking (FALSE, doc);
                    980:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    981:                       /* Insert. */
                    982:                       if (el == repeatEl)
                    983:                         newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, NULL);
                    984:                       else
                    985:                         newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, el);
1.138     vatton    986: 
1.179     kia       987:                       /* Finish insertion.*/
                    988:                       TtaCloseUndoSequence(doc);
                    989:                       TtaSetDocumentModified (doc);
                    990:                       TtaSetStructureChecking (oldStructureChecking, doc);
                    991: 
                    992:                       // restore the display
                    993:                       TtaSetDisplayMode (doc, dispMode);
                    994:                       firstEl = GetFirstEditableElement (newEl);
                    995:                       if (firstEl)
                    996:                         {
                    997:                           TtaSelectElement (doc, firstEl);
                    998:                           TtaSetStatusSelectedElement (doc, view, firstEl);
                    999:                         }
                   1000:                       else
                   1001:                         {
                   1002:                           TtaSelectElement (doc, newEl);
                   1003:                           TtaSetStatusSelectedElement (doc, view, newEl);
                   1004:                         }
                   1005:                     }
                   1006:                 }
1.104     kia      1007:             }
1.179     kia      1008:           TtaFreeMemory(result);
                   1009:           DumpSubtree(repeatEl, doc, 0);
                   1010: 
                   1011:         }
                   1012:       else /* if (Template_CanInsertRepeatChild(repeatEl)) */
                   1013:         {
                   1014:           TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.98      kia      1015:         }
                   1016:     }
1.77      vatton   1017:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc 1018: #endif /* TEMPLATES */
1.94      kia      1019:   return TRUE;
                   1020: }
                   1021: 
                   1022: /*----------------------------------------------------------------------
                   1023:   UseButtonClicked
                   1024:   Shows a menu with all the types that can be used in a use element.
                   1025:   ----------------------------------------------------------------------*/
                   1026: ThotBool UseButtonClicked (NotifyElement *event)
                   1027: {
                   1028: #ifdef TEMPLATES
                   1029:   Document        doc = event->document;
                   1030:   Element         el = event->element;
1.99      kia      1031:   Element         child;
1.94      kia      1032:   ElementType     elType;
                   1033:   XTigerTemplate  t;
                   1034:   Declaration     decl;
                   1035:   Element         firstEl;
                   1036:   Element         newEl = NULL;
                   1037:   char*           types;
                   1038:   ThotBool        oldStructureChecking;
1.95      kia      1039:   View            view;
1.133     vatton   1040:   char*           listtypes = NULL;
                   1041:   char*           result = NULL;
1.95      kia      1042: 
1.182     vatton   1043:   if (!TtaGetDocumentAccessMode (doc))
1.110     kia      1044:     return TRUE;
1.182     vatton   1045:   if (!IsTemplateInstanceDocument (doc))
                   1046:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1047:     
1.95      kia      1048:   TtaGetActiveView (&doc, &view);
                   1049:   if (view != 1)
                   1050:     return FALSE; /* let Thot perform normal operation */
1.148     kia      1051: 
1.94      kia      1052:   TtaCancelSelection(doc);
1.171     kia      1053:   t = GetXTigerDocTemplate(doc);
1.94      kia      1054:   if (!t)
                   1055:     return FALSE; /* let Thot perform normal operation */
                   1056:   elType = TtaGetElementType(el);
                   1057: 
                   1058:   firstEl = TtaGetFirstChild(el);
1.112     vatton   1059:   if (firstEl)
1.94      kia      1060:   {
                   1061:     RepeatButtonClicked(event);
                   1062:   }
                   1063:   else
                   1064:   {
1.104     kia      1065:     types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.112     vatton   1066:     if (types)
1.94      kia      1067:     {
1.142     kia      1068:       listtypes = Template_ExpandTypes(t, types, NULL, FALSE);
1.158     kia      1069: #ifdef AMAYA_DEBUG
                   1070:       printf("UseButtonClicked : \n  > %s\n", listtypes);
                   1071: #endif /* AMAYA_DEBUG */
                   1072:       
1.104     kia      1073:       result = QueryStringFromMenu(doc, listtypes);
1.112     vatton   1074:       if (result)
1.94      kia      1075:       {
1.104     kia      1076:         decl = Template_GetDeclaration(t, result);
1.112     vatton   1077:         if (decl)
1.99      kia      1078:         {
1.104     kia      1079:           /* Prepare insertion.*/
                   1080:           oldStructureChecking = TtaGetStructureChecking (doc);
                   1081:           TtaSetStructureChecking (FALSE, doc);
1.138     vatton   1082:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104     kia      1083:           
                   1084:           /* Insert */
                   1085:           newEl = Template_InsertUseChildren(doc, el, decl);
                   1086:           for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
                   1087:           {
                   1088:             TtaRegisterElementCreate(child, doc);
                   1089:           }
                   1090:           
                   1091:           TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
                   1092:           TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
                   1093:           
1.117     kia      1094:           /* xt:currentType attribute.*/
                   1095:           SetAttributeStringValueWithUndo(el, Template_ATTR_currentType, result);
                   1096:           
1.104     kia      1097:           /* Finish insertion. */
                   1098:           TtaCloseUndoSequence(doc);
1.114     vatton   1099:           TtaSetDocumentModified (doc);
1.104     kia      1100:           TtaSetStructureChecking (oldStructureChecking, doc);
                   1101:           
                   1102:           firstEl = GetFirstEditableElement(newEl);
1.112     vatton   1103:           if (firstEl)
1.104     kia      1104:           {
                   1105:             TtaSelectElement (doc, firstEl);
                   1106:             TtaSetStatusSelectedElement(doc, view, firstEl);
                   1107:           }
                   1108:           else
                   1109:           {
                   1110:             TtaSelectElement (doc, newEl);
                   1111:             TtaSetStatusSelectedElement(doc, view, newEl);
                   1112:           }
1.98      kia      1113:         }
1.94      kia      1114:       }
                   1115:     }
1.104     kia      1116:     TtaFreeMemory(types);
1.94      kia      1117:     TtaFreeMemory(listtypes);
                   1118:     TtaFreeMemory(result);
                   1119:   }
1.148     kia      1120: 
1.94      kia      1121:   return TRUE;
                   1122: #endif /* TEMPLATES */
1.56      francesc 1123:        return TRUE;
                   1124: }
1.64      francesc 1125: 
1.89      kia      1126: 
1.103     kia      1127: /*----------------------------------------------------------------------
                   1128:   UseSimpleButtonClicked
                   1129:   ----------------------------------------------------------------------*/
                   1130: ThotBool UseSimpleButtonClicked (NotifyElement *event)
                   1131: {
                   1132: #ifdef TEMPLATES
1.181     vatton   1133:   ElementType     elType, parentType;
                   1134:   AttributeType   attrType;
                   1135:   Attribute       att;
                   1136: 
1.182     vatton   1137:   if (!TtaGetDocumentAccessMode (event->document))
1.110     kia      1138:     return TRUE;
1.182     vatton   1139:   if (!IsTemplateInstanceDocument (event->document))
                   1140:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1141: 
1.181     vatton   1142:   elType = TtaGetElementType (event->element);
                   1143:   attrType.AttrSSchema = elType.ElSSchema;
                   1144:   attrType.AttrTypeNum = Template_ATTR_option;
                   1145:   att = TtaGetAttribute (event->element, attrType);
                   1146:   if (att)
                   1147:     return OptionButtonClicked (event);
                   1148:   
                   1149:   parentType = TtaGetElementType (TtaGetParent( event->element));
1.112     vatton   1150:   if (parentType.ElTypeNum == Template_EL_repeat)
1.138     vatton   1151:     return RepeatButtonClicked (event);
                   1152:   else if (parentType.ElTypeNum == Template_EL_bag)
                   1153:     return BagButtonClicked (event);
1.103     kia      1154: #endif /* TEMPLATES */  
                   1155:   return FALSE;
                   1156: }
1.94      kia      1157: 
                   1158: /*----------------------------------------------------------------------
                   1159:   OptionButtonClicked
                   1160:   ----------------------------------------------------------------------*/
                   1161: ThotBool OptionButtonClicked (NotifyElement *event)
                   1162: {
                   1163: #ifdef TEMPLATES
1.145     kia      1164:   Element         useEl, contentEl, next;
1.182     vatton   1165:   ElementType     useType;
1.94      kia      1166:   Document        doc;
                   1167:   XTigerTemplate  t;
                   1168:   View            view;
                   1169: 
1.182     vatton   1170:   if (!TtaGetDocumentAccessMode (event->document))
1.110     kia      1171:     return TRUE;
1.182     vatton   1172:   if (!IsTemplateInstanceDocument (event->document))
                   1173:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1174: 
1.94      kia      1175:   TtaGetActiveView (&doc, &view);
                   1176:   if (view != 1)
                   1177:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1178: 
1.94      kia      1179:   doc = event->document;
1.182     vatton   1180:   useEl = event->element;
1.145     kia      1181:   if (!useEl)
1.94      kia      1182:     return FALSE; /* let Thot perform normal operation */
1.145     kia      1183:   useType = TtaGetElementType (useEl);
1.182     vatton   1184:   if (useType.ElTypeNum != Template_EL_useEl &&
                   1185:       useType.ElTypeNum != Template_EL_useSimple)
1.94      kia      1186:     return FALSE;
                   1187: 
1.145     kia      1188:   TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
1.94      kia      1189:   TtaCancelSelection (doc);
1.145     kia      1190:   contentEl = TtaGetFirstChild (useEl);
                   1191:   if (!contentEl)
1.94      kia      1192:     /* the "use" element is empty. Instantiate it */
                   1193:     {
1.171     kia      1194:       t = GetXTigerDocTemplate (doc);
1.94      kia      1195:       if (!t)
                   1196:         return FALSE; // no template ?!?!
1.145     kia      1197:       InstantiateUse (t, useEl, doc, TRUE);
1.94      kia      1198:     }
                   1199:   else
                   1200:     /* remove the content of the "use" element */
                   1201:     {
                   1202:       do
                   1203:         {
1.145     kia      1204:           next = contentEl;
1.94      kia      1205:           TtaNextSibling (&next);
1.145     kia      1206:           TtaRegisterElementDelete(contentEl, doc);
                   1207:           TtaDeleteTree (contentEl, doc);
                   1208:           contentEl = next;
1.94      kia      1209:         }
                   1210:       while (next);
1.145     kia      1211:       if (NeedAMenu (useEl, doc))
                   1212:         {
                   1213:           TtaChangeTypeOfElement (useEl, doc, Template_EL_useEl);
                   1214:           TtaRegisterElementTypeChange(useEl, Template_EL_useSimple, doc);
                   1215:         }
1.94      kia      1216:     }
                   1217:   TtaSelectElement (doc, event->element);
1.145     kia      1218:   TtaCloseUndoSequence(doc);
1.94      kia      1219:   return TRUE; /* don't let Thot perform normal operation */
                   1220: #endif /* TEMPLATES */
                   1221:   return TRUE;
                   1222: }
                   1223: 
1.191     kia      1224: 
                   1225: 
1.111     vatton   1226: /*----------------------------------------------------------------------
1.191     kia      1227:   Template_FillFromDocument
                   1228:   Fill XTigerTemplate structure with the content of the document.
                   1229:   Load dependencies if needed.
1.111     vatton   1230:   ----------------------------------------------------------------------*/
1.191     kia      1231: void Template_FillFromDocument (Document doc)
1.111     vatton   1232: {
                   1233: #ifdef TEMPLATES
1.191     kia      1234:   XTigerTemplate t = GetXTigerTemplate (DocumentURLs[doc]);
                   1235:   Element        root;
1.165     kia      1236:   
1.193     vatton   1237: #ifdef AMAYA_DEBUG
1.191     kia      1238:   printf("Template_FillFromDocument\n");
1.193     vatton   1239: #endif
                   1240:   if (t)
1.111     vatton   1241:     {
1.193     vatton   1242: #ifdef AMAYA_DEBUG
1.191     kia      1243:       printf("plop : %d\n", t->state);
1.193     vatton   1244: #endif
1.191     kia      1245:       SetTemplateDocument (t, doc);
1.144     vatton   1246: 
1.191     kia      1247:       Template_PrepareTemplate(t);
                   1248:       
                   1249:       if(IsTemplateInstanceDocument(doc))
1.144     vatton   1250:         {
1.193     vatton   1251: #ifdef AMAYA_DEBUG
1.191     kia      1252:           printf("  > instance\n");
1.193     vatton   1253: #endif
1.144     vatton   1254:           // fix all access rights in the instance
1.191     kia      1255:           root = TtaGetRootElement (doc);
                   1256:           TtaSetAccessRight (root, ReadOnly, doc);
1.166     kia      1257:           Template_FixAccessRight (t, root, doc);
1.144     vatton   1258:           TtaUpdateAccessRightInViews (doc, root);
                   1259:         }
1.193     vatton   1260: #ifdef AMAYA_DEBUG
1.191     kia      1261:       else if(t->state&templLibraryFlag)
                   1262:         printf("  > library\n");
                   1263:       else if(t->state&templTemplate)
                   1264:         printf("  > template\n");
1.193     vatton   1265: #endif      
1.191     kia      1266:       // Mark loaded
                   1267:       t->state |= templloaded;
                   1268:       TtaSetDocumentUnmodified (doc);
                   1269:       UpdateTemplateMenus (doc);
                   1270:       
                   1271: #ifdef AMAYA_DEBUG  
                   1272:       DumpAllDeclarations();
                   1273: #endif /* AMAYA_DEBUG */
                   1274:     }
                   1275: #endif /* TEMPLATES */
                   1276: }
                   1277: 
                   1278: 
                   1279: /*----------------------------------------------------------------------
                   1280:   Template_CheckAndPrepareTemplate checks if the document is a template
                   1281:   or a library and prepare XTigerTemplate structure to use it.
                   1282:   ----------------------------------------------------------------------*/
                   1283: ThotBool Template_CheckAndPrepareTemplate(char* docURL)
                   1284: {
                   1285: #ifdef TEMPLATES
                   1286:   XTigerTemplate t = NULL; //GetXTigerTemplate(docURL);
1.193     vatton   1287: 
                   1288: #ifdef AMAYA_DEBUG
1.191     kia      1289:   printf("Template_CheckAndPrepareTemplate %s\n", docURL);
1.193     vatton   1290: #endif
                   1291:   if (IsXTiger(docURL))
1.191     kia      1292:     {
                   1293:       t = LookForXTigerTemplate(docURL);
                   1294:       t->state |= templTemplate;
1.111     vatton   1295:     }
1.193     vatton   1296:   else if (IsXTigerLibrary(docURL))
1.191     kia      1297:     {
                   1298:       t = LookForXTigerLibrary(docURL);
                   1299:       t->state |= templLibrary;
                   1300:     }
1.193     vatton   1301:   return t != NULL;
1.111     vatton   1302: #endif /* TEMPLATES */
1.191     kia      1303:   return FALSE;
1.111     vatton   1304: }
1.94      kia      1305: 
1.191     kia      1306: 
1.66      vatton   1307: /*----------------------------------------------------------------------
1.191     kia      1308:   Template_CheckAndPrepareInstance checks if it is a template instance needs.
1.108     vatton   1309:   If it's an instance and the template is not loaded, load it into a
                   1310:   temporary file
1.66      vatton   1311:   ----------------------------------------------------------------------*/
1.191     kia      1312: void Template_CheckAndPrepareInstance (char *localFileName, Document doc, char* docURL)
1.65      francesc 1313: {
                   1314: #ifdef TEMPLATES
1.76      vatton   1315:   XTigerTemplate   t;
1.103     kia      1316:   char            *content, *ptr, *begin;
1.76      vatton   1317:   gzFile           stream;
                   1318:   char             buffer[2000];
1.77      vatton   1319:   int              res;
1.171     kia      1320:   char            *template_version = NULL,
                   1321:                   *template_url = NULL;
1.65      francesc 1322: 
1.171     kia      1323:   stream = TtaGZOpen (localFileName);
1.76      vatton   1324:   if (stream != 0)
1.65      francesc 1325:     {
1.76      vatton   1326:       res = gzread (stream, buffer, 1999);
                   1327:       if (res >= 0)
1.65      francesc 1328:         {
1.81      vatton   1329:           buffer[res] = EOS;
1.103     kia      1330:           begin = strstr (buffer, "<?xtiger");
                   1331:           
1.112     vatton   1332:           if (begin)
1.103     kia      1333:           {
                   1334:             // Search for template version
                   1335:             ptr = strstr (begin, "templateVersion");
                   1336:             if (ptr)
                   1337:               ptr = strstr (ptr, "=");
                   1338:             if (ptr)
                   1339:               ptr = strstr (ptr, "\"");
                   1340:             if (ptr)
                   1341:               {
                   1342:                 // template URI
                   1343:                 content = &ptr[1];
                   1344:                 ptr = strstr (content, "\"");
                   1345:               }
                   1346:             if (ptr)
                   1347:               {
                   1348:                 *ptr = EOS;
                   1349:                 //Get now the template URI
1.171     kia      1350:                 template_version = TtaStrdup (content);
1.103     kia      1351:                 *ptr = '"';
                   1352:               }
                   1353:            
                   1354:             // Search for template uri
                   1355:             ptr = strstr (begin, "template");
                   1356:             if (ptr && ptr[8] != 'V')
                   1357:               ptr = strstr (ptr, "=");
                   1358:             if (ptr)
                   1359:               ptr = strstr (ptr, "\"");
                   1360:             if (ptr)
                   1361:               {
                   1362:                 // template URI
                   1363:                 content = &ptr[1];
                   1364:                 ptr = strstr (content, "\"");
                   1365:               }
                   1366:             if (ptr)
                   1367:               {
                   1368:                 *ptr = EOS;
                   1369:                 //Get now the template URI
1.171     kia      1370:                 template_url = TtaStrdup (content);
                   1371: 
                   1372:                 t = GetXTigerTemplate (template_url);
1.103     kia      1373:                 if (!t)
                   1374:                   {
1.171     kia      1375:                     LoadTemplate (doc, template_url);
                   1376:                     t = GetXTigerTemplate(template_url);
1.103     kia      1377:                   }
1.171     kia      1378:                 Template_PrepareInstance(docURL, doc, template_version, template_url);
                   1379:                 template_version = NULL;
                   1380:                 template_url     = NULL;
                   1381:                 Template_AddReference (t);
1.103     kia      1382:                 *ptr = '"';
                   1383:               }
                   1384:           }
1.65      francesc 1385:         }
                   1386:     }
1.171     kia      1387:   TtaFreeMemory(template_version);
                   1388:   TtaFreeMemory(template_url);
1.76      vatton   1389:   TtaGZClose (stream);
1.65      francesc 1390: #endif /* TEMPLATES */
                   1391: }
                   1392: 
1.64      francesc 1393: /*----------------------------------------------------------------------
1.178     kia      1394:   ClosingTemplateDocument
                   1395:   Callback called before closing a document which uses templates.
1.64      francesc 1396:   ----------------------------------------------------------------------*/
1.178     kia      1397: ThotBool ClosingTemplateDocument(NotifyDialog* dialog)
1.64      francesc 1398: {
1.65      francesc 1399: #ifdef TEMPLATES
1.171     kia      1400:   XTigerTemplate t = GetXTigerDocTemplate(dialog->document);
                   1401:   if(t)
1.178     kia      1402:       Template_RemoveReference(t);
1.65      francesc 1403: #endif /* TEMPLATES */
                   1404:   return FALSE;
1.64      francesc 1405: }
1.87      kia      1406: 
                   1407: /*----------------------------------------------------------------------
1.120     kia      1408:   IsTemplateElement
1.138     vatton   1409:   Test if an element is a template element.
1.87      kia      1410:   ----------------------------------------------------------------------*/
1.140     vatton   1411: ThotBool IsTemplateElement (Element elem)
1.87      kia      1412: {
                   1413: #ifdef TEMPLATES
1.138     vatton   1414:   ElementType     elType;
                   1415: 
                   1416:   elType = TtaGetElementType(elem);
                   1417:   if (elType.ElSSchema)
1.164     kia      1418:     return (strcmp(TtaGetSSchemaName(elType.ElSSchema) , "Template") == 0);
1.138     vatton   1419: #endif /* TEMPLATES */
1.87      kia      1420:   return FALSE;
                   1421: }
                   1422: 
                   1423: 
                   1424: /*----------------------------------------------------------------------
                   1425:   GetFirstTemplateParentElement
1.138     vatton   1426:   Return the first element which has "Template" as schema name or null.
1.87      kia      1427:   ----------------------------------------------------------------------*/
                   1428: Element GetFirstTemplateParentElement(Element elem)
                   1429: {
                   1430: #ifdef TEMPLATES
1.138     vatton   1431:   ElementType     elType;
                   1432: 
                   1433:   elem = TtaGetParent (elem);
                   1434:   elType = TtaGetElementType(elem);
                   1435:   while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
                   1436: {
                   1437:     elem = TtaGetParent (elem);
                   1438:     elType = TtaGetElementType(elem);
1.87      kia      1439:   }
                   1440:   return elem;
                   1441: #else
                   1442:   return NULL;
                   1443: #endif /* TEMPLATES */
                   1444: }
1.101     kia      1445: 
1.103     kia      1446: 
1.101     kia      1447: /*----------------------------------------------------------------------
1.102     vatton   1448:   TemplateElementWillBeCreated
1.101     kia      1449:   Processed when an element will be created in a template context.
                   1450:   ----------------------------------------------------------------------*/
1.102     vatton   1451: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101     kia      1452: {
1.103     kia      1453: #ifdef TEMPLATES
                   1454:   ElementType elType = event->elementType;
                   1455:   Element     parent = event->element;
                   1456:   ElementType parentType = TtaGetElementType(parent);
1.113     kia      1457:   Element     ancestor;
                   1458:   ElementType ancestorType;
                   1459:   SSchema     templateSSchema;
                   1460:   char*       types;
                   1461:   ThotBool    b;
1.101     kia      1462: 
1.115     kia      1463:   if(event->info==1)
                   1464:     return FALSE;
                   1465: 
1.193     vatton   1466: #ifdef AMAYA_DEBUG
1.185     kia      1467:   printf("TemplateElementWillBeCreated\n");
1.193     vatton   1468: #endif
1.112     vatton   1469:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1470:     return TRUE;
                   1471: 
1.138     vatton   1472:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.102     vatton   1473:   if (templateSSchema == NULL)
                   1474:     return FALSE; // let Thot do the job
1.115     kia      1475: 
1.113     kia      1476:   // Fisrt, test if in a xt:bag or in a base-element xt:use
1.147     vatton   1477:   if(parentType.ElSSchema == templateSSchema)
1.113     kia      1478:     ancestor = parent;
                   1479:   else
1.147     vatton   1480:     ancestor = GetFirstTemplateParentElement (parent);
1.113     kia      1481: 
1.147     vatton   1482:   if (ancestor)
1.113     kia      1483:   {
                   1484:     ancestorType = TtaGetElementType(ancestor);
1.147     vatton   1485:     if (ancestorType.ElTypeNum == Template_EL_bag)
1.113     kia      1486:     {
1.147     vatton   1487:       // only check the use child
                   1488:       if (ancestor != parent)
                   1489:         return  FALSE; // let Thot do the job
                   1490:       if (elType.ElSSchema == templateSSchema &&
                   1491:           (elType.ElTypeNum == Template_EL_useSimple ||
                   1492:            elType.ElTypeNum == Template_EL_useEl))
1.116     kia      1493:         return FALSE;
1.147     vatton   1494:       return !Template_CanInsertElementInBagElement (event->document, elType, ancestor);      
1.113     kia      1495:     }
1.147     vatton   1496:     else if(ancestorType.ElTypeNum == Template_EL_useSimple ||
                   1497:             ancestorType.ElTypeNum == Template_EL_useEl)
1.113     kia      1498:     {
1.147     vatton   1499:       // only check the bag child @@@ will be check exclude/include later
1.172     kia      1500:       if (ancestor != parent)
                   1501:         return  FALSE; // let Thot do the job
1.113     kia      1502:       types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
1.128     kia      1503:       b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position);
                   1504:       TtaFreeMemory(types);
1.115     kia      1505:       return !b;
1.113     kia      1506:     }
                   1507:   }
1.115     kia      1508:   
1.200     kia      1509:   if (elType.ElSSchema == templateSSchema && (
                   1510:       elType.ElTypeNum == Template_EL_TEXT_UNIT || 
                   1511:       elType.ElTypeNum == Template_EL_component))
1.115     kia      1512:   {
                   1513:     return FALSE;
                   1514:   }
                   1515:   
1.113     kia      1516:   // Can not insert.
                   1517:   return TRUE;
1.101     kia      1518: #endif /* TEMPLATES*/
1.102     vatton   1519:   return FALSE;
1.101     kia      1520: }
                   1521: 
                   1522: /*----------------------------------------------------------------------
                   1523:   TemplateElementWillBeDeleted
                   1524:   Processed when an element will be deleted in a template context.
                   1525:   ----------------------------------------------------------------------*/
                   1526: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1527: {
1.107     kia      1528: #ifdef TEMPLATES
                   1529:   Document       doc = event->document;
                   1530:   Element        elem = event->element;
1.163     vatton   1531:   Element        xtElem, parent = NULL, sibling;
1.117     kia      1532:   ElementType    xtType, elType;
1.107     kia      1533:   char*          type;
                   1534:   Declaration    dec;
1.115     kia      1535:   SSchema        templateSSchema;
1.107     kia      1536:   XTigerTemplate t;
1.156     vatton   1537:   ThotBool       selparent = FALSE;
1.192     kia      1538:   
1.115     kia      1539:   if(event->info==1)
                   1540:     return FALSE;
                   1541: 
1.112     vatton   1542:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1543:     return TRUE;
                   1544: 
1.138     vatton   1545:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.107     kia      1546:   if (templateSSchema == NULL)
                   1547:     return FALSE; // let Thot do the job
1.192     kia      1548:   
                   1549:   t = GetXTigerDocTemplate(doc);
                   1550:   if(Template_IsTemplate(t)||Template_IsLibrary(t))
                   1551:     return FALSE; // If template or library, pass to specialized functions.
1.122     kia      1552: 
1.107     kia      1553:   xtElem = GetFirstTemplateParentElement(elem);
1.112     vatton   1554:   if (xtElem)
1.107     kia      1555:   {
                   1556:     xtType = TtaGetElementType(xtElem);
1.117     kia      1557:     
1.192     kia      1558:     
1.109     kia      1559: 
1.112     vatton   1560:     if (xtType.ElTypeNum==Template_EL_bag)
1.117     kia      1561:     {
                   1562:       elType = TtaGetElementType(elem);
                   1563:       if(elType.ElSSchema==templateSSchema &&
                   1564:         (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
                   1565:       {
                   1566:         // Remove element manually.
                   1567:         TtaOpenUndoSequence(doc, elem, elem, 0, 0);
                   1568:         TtaRegisterElementDelete(elem, doc);
                   1569:         TtaDeleteTree(elem, doc);
                   1570:         TtaCloseUndoSequence(doc);
                   1571:         return TRUE;
                   1572:       }
                   1573:       else
                   1574:         return FALSE; // xt:bag always allow remove children.
                   1575:     }
1.112     vatton   1576:     else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107     kia      1577:     {
1.109     kia      1578:       parent = TtaGetParent(elem);
1.117     kia      1579:       if (xtElem!=parent)
                   1580:       {
                   1581:         type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
                   1582:         dec = Template_GetDeclaration(t, type);
                   1583:         TtaFreeMemory(type);
                   1584:         
                   1585:         if (dec && dec->nature == XmlElementNat)
                   1586:           return FALSE; // Can remove element only if in xt:use current type is base language element. 
                   1587:         else
                   1588:           return TRUE;
1.107     kia      1589:       }
1.109     kia      1590:     }
1.112     vatton   1591:     else if (xtType.ElTypeNum==Template_EL_repeat)
1.109     kia      1592:     {
1.156     vatton   1593:       sibling = TtaGetSuccessor (elem);
                   1594:       if (sibling == NULL)
                   1595:         {
                   1596:           // there is no next element
                   1597:           sibling = TtaGetPredecessor (elem);
                   1598:           if (sibling == NULL)
                   1599:             selparent = TRUE;
                   1600:         }
1.109     kia      1601:       TtaRegisterElementDelete(elem, doc);
                   1602:       TtaDeleteTree(elem, doc);
                   1603:       InstantiateRepeat(t, xtElem, doc, TRUE);
1.156     vatton   1604:       if (selparent)
                   1605:         // look for the new sibling
                   1606:         sibling = TtaGetFirstChild (parent);
                   1607:       if (sibling)
                   1608:         TtaSelectElement(doc, sibling);
1.151     quint    1609:       else
1.156     vatton   1610:         TtaSelectElement(doc, parent);
1.109     kia      1611:       return TRUE;
1.107     kia      1612:     }
                   1613:   }
1.109     kia      1614:   
                   1615:   //TODO Test if current element is use or repeat.
                   1616:   // Because if an element is delete and it is the unique child of its parent,
                   1617:   // the parent intends to destroy itself. 
                   1618:   
1.107     kia      1619:   return TRUE;
                   1620: #else /* TEMPLATES */
1.101     kia      1621:   return FALSE;
1.107     kia      1622: #endif /* TEMPLATES */
1.101     kia      1623: }
                   1624: 
1.109     kia      1625: /*----------------------------------------------------------------------
                   1626:   CurrentTypeWillBeExported
                   1627:   Check if the xt:currentType attribute can be exported
                   1628:   ----------------------------------------------------------------------*/
                   1629: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
                   1630: {
                   1631: #ifdef TEMPLATES
1.110     kia      1632: 
1.112     vatton   1633:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1634:     return TRUE;
                   1635: 
1.112     vatton   1636:   if (IsTemplateDocument(event->document))
1.109     kia      1637:     return TRUE;
                   1638: #endif /* TEMPLATES */
                   1639:   return FALSE;
                   1640: }
1.127     kia      1641: 
                   1642: /*----------------------------------------------------------------------
                   1643:   TemplateAttrInMenu
                   1644:   Called by Thot when building the Attributes menu for template elements.
                   1645:   ----------------------------------------------------------------------*/
                   1646: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
                   1647: {
                   1648: #ifdef TEMPLATES
                   1649:   // Prevent from showing attributes for template instance but not templates.
                   1650:   if(IsTemplateInstanceDocument(event->document))
                   1651:     return TRUE;
                   1652:   else
                   1653: #endif /* TEMPLATES */
                   1654:     return FALSE;
                   1655: }
1.160     kia      1656: 
1.167     kia      1657: /*----------------------------------------------------------------------
1.168     kia      1658:   CreateTemplateFromDocument
                   1659:   Create a template from the current document.
1.167     kia      1660:   ----------------------------------------------------------------------*/
                   1661: void CreateTemplateFromDocument(Document doc, View view)
                   1662: {
1.171     kia      1663: #ifdef TEMPLATES
1.196     vatton   1664:   char buffer[MAX_LENGTH], suffix[10];
                   1665: 
                   1666:   // removet the current suffix
                   1667:   strcpy (buffer, DocumentURLs[doc]);
                   1668:   TtaExtractSuffix (buffer, suffix);
                   1669:   if (suffix[0] == EOS && IsW3Path (buffer) &&
                   1670:       DocumentMeta[doc] && DocumentMeta[doc]->content_location)
                   1671:     // use the location instead of the current URI
                   1672:     strcpy (buffer, DocumentMeta[doc]->content_location);
                   1673:   // the new suffix
1.168     kia      1674:   strcat(buffer, ".xtd");
                   1675:   DontReplaceOldDoc = TRUE;
                   1676:   CreateTemplate(doc, buffer);
1.171     kia      1677: #endif /* TEMPLATES */
1.167     kia      1678: }
                   1679: 
1.169     kia      1680: /*----------------------------------------------------------------------
                   1681:   UpdateTemplateMenus
                   1682:   ----------------------------------------------------------------------*/
                   1683: void UpdateTemplateMenus (Document doc)
                   1684: {
1.196     vatton   1685:   if (IsTemplateInstanceDocument(doc))
1.180     kia      1686:     {
                   1687:       // Instance document
                   1688:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1689:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1690:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1691:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1692:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1693:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180     kia      1694:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196     vatton   1695:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1696:     }
1.183     vatton   1697:   else if (DocumentURLs[doc] && IsXTiger (DocumentURLs[doc]))
1.180     kia      1698:     {
                   1699:       // Template document
                   1700:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1701:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1702:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1703:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1704:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeat);
                   1705:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180     kia      1706:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateFreeBox);      
1.196     vatton   1707:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1708:     }
1.169     kia      1709:   else
1.180     kia      1710:     {
                   1711:       //Standard document
                   1712:       TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
                   1713:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1714:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1715:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1716:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1717:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180     kia      1718:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196     vatton   1719:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1720:     }
1.169     kia      1721: }
1.171     kia      1722: 
                   1723: /*----------------------------------------------------------------------
                   1724:   UninstanciateTemplateDocument
                   1725:   An instance of a template is tranformed into a template-less docuemnt.
                   1726:   Remove link between XTigerTemplate structure and document.
                   1727:   ----------------------------------------------------------------------*/
                   1728: void UninstanciateTemplateDocument(Document doc)
                   1729: {
                   1730: #ifdef TEMPLATES
                   1731:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1732:   if(t)
                   1733:     Template_Close(t);
                   1734: #endif /* TEMPLATES */  
                   1735: }
                   1736: 
                   1737: 
                   1738: /*----------------------------------------------------------------------
                   1739:   Template_PrepareInstance
                   1740:   Allocate XTigerTemplate structure for instance and initialize template
                   1741:   url and template version.
                   1742:   ----------------------------------------------------------------------*/
                   1743: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
                   1744: {
                   1745: #ifdef TEMPLATES
                   1746:   XTigerTemplate t = GetXTigerTemplate(fileName);
                   1747:   if(!t)
                   1748:     t = NewXTigerTemplate(fileName);
                   1749:   t->state           = templInstance;
                   1750:   t->templateVersion = template_version;
                   1751:   t->base_uri        = template_url;
                   1752:   t->doc             = doc;
1.178     kia      1753:   t->ref             = 1;
1.171     kia      1754:   
                   1755: #endif /* TEMPLATES */
                   1756: }
                   1757: 
                   1758: 
                   1759: /*----------------------------------------------------------------------
                   1760:   SetDocumentAsXTigerTemplate
                   1761:   Set the document template structure as template.
                   1762:   ----------------------------------------------------------------------*/
                   1763: void SetDocumentAsXTigerTemplate(Document doc)
                   1764: {
                   1765: #ifdef TEMPLATES
                   1766:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1767:   if(t)
                   1768:     t->state |= templTemplate;
                   1769: #endif /* TEMPLATES */  
                   1770: }
                   1771: 
                   1772: /*----------------------------------------------------------------------
                   1773:   SetDocumentAsXTigerLibrary
                   1774:   Set the document template structure as template library.
                   1775:   ----------------------------------------------------------------------*/
                   1776: void SetDocumentAsXTigerLibrary(Document doc)
                   1777: {
                   1778: #ifdef TEMPLATES
                   1779:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1780:   if(t)
                   1781:     t->state |= templLibrary;
                   1782: #endif /* TEMPLATES */  
                   1783: }
1.174     kia      1784: 
                   1785: 
                   1786: /*----------------------------------------------------------------------
                   1787:   TemplateCreateTextBox
                   1788:   Create a xt:use types="string" box around the selection.
                   1789:   ----------------------------------------------------------------------*/
                   1790: void TemplateCreateTextBox(Document doc, View view)
                   1791: {
                   1792: #ifdef TEMPLATES
                   1793:   Element     selElem;
                   1794:   ElementType selType;
                   1795:   int         firstChar, lastChar;
                   1796:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1797:   ElementType useType;
                   1798:   Element     use;
                   1799:   char        buffer[128];
1.189     kia      1800:   ThotBool    oldStructureChecking;
1.174     kia      1801:   
                   1802:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
1.184     vatton   1803:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174     kia      1804: 
                   1805:   if (!TtaGetDocumentAccessMode(doc))
                   1806:     return;
                   1807:   
                   1808:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1809:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1810:     {
                   1811:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1812:       if(selElem)
                   1813:         {
                   1814:           selType =  TtaGetElementType(selElem);
                   1815:           if(!TtaIsLeaf(selType))
                   1816:             {
                   1817:               selElem = TtaGetFirstLeaf(selElem);
                   1818:               selType = TtaGetElementType(selElem);
                   1819:               firstChar = lastChar = 0;
                   1820:             }
                   1821:           
                   1822:           if(selType.ElTypeNum==1)
                   1823:             {
                   1824:               QueryStringFromUser(label, title, buffer, 127);
                   1825:               useType.ElSSchema = sstempl;
                   1826:               useType.ElTypeNum = Template_EL_useSimple;
                   1827: 
1.189     kia      1828:               oldStructureChecking = TtaGetStructureChecking (doc);
                   1829:               TtaSetStructureChecking (FALSE, doc);
                   1830: 
1.174     kia      1831:               if(firstChar==0)
                   1832:                 {
                   1833:                   use = TtaNewElement(doc, useType);
                   1834:                   if(use)
                   1835:                     {
                   1836:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1837:                       TtaInsertSibling(use, selElem, FALSE, doc);
                   1838:                       TtaRegisterElementCreate(use, doc);
                   1839:                       TtaRegisterElementDelete (selElem, doc);
                   1840:                       TtaRemoveTree(selElem, doc);
                   1841:                       TtaInsertFirstChild(&selElem, use, doc);
                   1842:                       TtaRegisterElementDelete (selElem, doc);
                   1843:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1844:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);
                   1845:                       TtaCloseUndoSequence(doc);
                   1846:                       TtaSelectElement(doc, use);
                   1847:                     }
                   1848:                 }
                   1849:               else
                   1850:                 {
                   1851:                   GenerateInlineElement(Template_EL_useSimple, sstempl, 0, "", TRUE);
                   1852:                   TtaGiveFirstSelectedElement(doc, &use, &firstChar, &lastChar);
                   1853:                   selType =  TtaGetElementType(use);
                   1854:                   if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_useSimple)
                   1855:                     {
                   1856:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1857:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);                      
                   1858:                     }
                   1859:                 }
1.189     kia      1860:               
                   1861:               TtaSetStructureChecking (oldStructureChecking, doc);
                   1862:               
1.174     kia      1863:             }
                   1864:           
                   1865:         }
                   1866:     }
                   1867: #endif /* TEMPLATES */
                   1868: }
                   1869: 
                   1870: 
                   1871: /*----------------------------------------------------------------------
                   1872:   TemplateCreateFreeBox
                   1873:   Create a xt:bag types="string" box around the selection.
                   1874:   ----------------------------------------------------------------------*/
                   1875: void TemplateCreateFreeBox(Document doc, View view)
                   1876: {
                   1877: #ifdef TEMPLATES
1.185     kia      1878:   Element     selElem, selElem2, parent, parent2, current, child = NULL;
1.174     kia      1879:   ElementType selType, selType2;
                   1880:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1881:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1882: 
                   1883:   ElementType bagType;
                   1884:   Element     bag;
                   1885:   char        buffer[128];
                   1886: 
                   1887:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
1.184     vatton   1888:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174     kia      1889: 
                   1890:   if (!TtaGetDocumentAccessMode(doc))
                   1891:     return;
                   1892:   
                   1893:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1894:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1895:     {
                   1896:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1897:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   1898:       
                   1899:       if(selElem && selElem2)
                   1900:         {
                   1901:           selType =  TtaGetElementType(selElem);
                   1902:           selType2 =  TtaGetElementType(selElem2);
                   1903: 
                   1904:           QueryStringFromUser(label, title, buffer, 127);
                   1905:           bagType.ElSSchema = sstempl;
                   1906:           bagType.ElTypeNum = Template_EL_bag;
                   1907:           
1.185     kia      1908:           parent  = TtaGetParent(selElem);
                   1909:           parent2 = TtaGetParent(selElem2);
                   1910:           
                   1911:           if(firstChar==0 && firstChar2==0 && parent == parent2)
1.174     kia      1912:             {
1.185     kia      1913:               ThotBool        oldStructureChecking;
                   1914:               DisplayMode     dispMode;
                   1915:               dispMode = TtaGetDisplayMode (doc);
                   1916:               if (dispMode == DisplayImmediately)
                   1917:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   1918:               oldStructureChecking = TtaGetStructureChecking (doc);
                   1919:               TtaSetStructureChecking (FALSE, doc);
                   1920: 
                   1921:               // Create and insert xt:bag element
                   1922:               bag = TtaNewElement(doc, bagType);
                   1923:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1924:               TtaInsertSibling(bag, selElem, TRUE, doc);
                   1925:               TtaRegisterElementCreate(bag, doc);
                   1926: 
                   1927:               TtaNextSibling(&selElem2);
                   1928:               while(selElem!=selElem2)
1.174     kia      1929:                 {
1.185     kia      1930:                   current = selElem;
                   1931:                   TtaNextSibling(&selElem);
1.174     kia      1932:                   
1.185     kia      1933:                   TtaRegisterElementDelete (current, doc);
                   1934:                   TtaRemoveTree(current, doc);
                   1935:                   if(child)
                   1936:                     TtaInsertSibling(current, child, FALSE, doc);
                   1937:                   else
                   1938:                     TtaInsertFirstChild(&current, bag, doc);
                   1939:                   TtaRegisterElementDelete (current, doc);
                   1940:                   child = current;
1.174     kia      1941:                 }
1.185     kia      1942:               
                   1943:               SetAttributeStringValue(bag, Template_ATTR_types, "any");
                   1944:               SetAttributeStringValue(bag, Template_ATTR_title, buffer);
                   1945:               TtaCloseUndoSequence(doc);
                   1946:               TtaSelectElement(doc, bag);
                   1947:               
                   1948:               TtaSetStructureChecking (oldStructureChecking, doc);
                   1949:               TtaSetDisplayMode (doc, dispMode);
1.174     kia      1950:             }
                   1951:         }
                   1952:     }
                   1953: #endif /* TEMPLATES */
                   1954: }
                   1955: 
1.180     kia      1956: /*----------------------------------------------------------------------
1.198     kia      1957:   TemplateCreateUnion
                   1958:   Create a xt:union around the selection.
                   1959:   ----------------------------------------------------------------------*/
                   1960: void TemplateCreateUnion(Document doc, View view)
                   1961: {
                   1962: #ifdef TEMPLATES
1.199     kia      1963:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   1964:   Element        head, sibling, unionEl;
                   1965:   ElementType    unionType;
                   1966:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1967:   char          *proposed, *name = NULL, *types=NULL;
1.200     kia      1968: 
                   1969:   Element     selElem;
                   1970:   ElementType selType;
                   1971:   int         firstChar, lastChar;
                   1972:   
1.199     kia      1973:   
                   1974:   if(t && sstempl)
                   1975:     {
1.200     kia      1976:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1977:       selType =  TtaGetElementType(selElem);
                   1978:       if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_union)
                   1979:         Template_ModifyUnionElement(doc, selElem);
                   1980:       else
1.199     kia      1981:         {
1.200     kia      1982:           proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
                   1983:           if(QueryUnionFromUser(proposed, NULL, &name, &types, TRUE))
                   1984:             {
                   1985:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1986:     
                   1987:               head = TemplateFindHead(doc);
                   1988:               sibling = TtaGetLastChild(head);
                   1989:     
                   1990:               unionType.ElSSchema = sstempl;
                   1991:               unionType.ElTypeNum = Template_EL_union;
                   1992:               unionEl = TtaNewElement(doc, unionType);
                   1993:               
                   1994:               if(sibling)
                   1995:                 TtaInsertSibling(unionEl, sibling, FALSE, doc);
                   1996:               else
                   1997:                 {
                   1998:                   sibling = unionEl;
                   1999:                   TtaInsertFirstChild(&sibling, head, doc);
                   2000:                 }
                   2001:               Template_SetName(doc, unionEl, name, TRUE);
                   2002:               
                   2003:               SetAttributeStringValue(unionEl, Template_ATTR_includeAt, types);
                   2004:               TtaSetAccessRight(unionEl, ReadOnly, doc);
                   2005:               
                   2006:               TtaRegisterElementCreate(unionEl, doc);
                   2007:               
                   2008:               TtaSelectElement(doc, unionEl);
                   2009:               TtaCloseUndoSequence(doc);
                   2010:               
                   2011:               Template_DeclareNewUnion (t, name, types, "");
                   2012:               TtaFreeMemory(proposed);
                   2013:               TtaFreeMemory(name);
                   2014:               TtaFreeMemory(types);
                   2015:             }          
                   2016:         }
                   2017:     }  
1.199     kia      2018: 
1.200     kia      2019: #endif /* TEMPLATES */
                   2020: }
1.199     kia      2021: 
1.200     kia      2022: /*----------------------------------------------------------------------
                   2023:   Template_ModifyUnionElement
                   2024:   Query the user to modify an xt:union
                   2025:   ----------------------------------------------------------------------*/
                   2026: void Template_ModifyUnionElement(Document doc, Element unionEl)
                   2027: {
                   2028:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2029:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   2030:   ElementType    unionType;
                   2031:   char          *proposed, *checked, *name, *types=NULL;
                   2032:   
                   2033:   if(doc && unionEl && t && sstempl)
                   2034:     {
                   2035:       unionType = TtaGetElementType(unionEl);
                   2036:       if(unionType.ElSSchema==sstempl && unionType.ElTypeNum==Template_EL_union)
                   2037:         {
                   2038:           proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
                   2039:           checked  = GetAttributeStringValueFromNum(unionEl, Template_ATTR_includeAt, NULL);
                   2040:           name     = GetAttributeStringValueFromNum(unionEl, Template_ATTR_name, NULL);
                   2041:           if(QueryUnionFromUser(proposed, checked, &name, &types, FALSE))
1.199     kia      2042:             {
1.200     kia      2043:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2044:               SetAttributeStringValueWithUndo(unionEl, Template_ATTR_includeAt, types);
                   2045:               TtaCloseUndoSequence(doc);
1.199     kia      2046:             }
1.200     kia      2047:           TtaFreeMemory(proposed);
                   2048:           TtaFreeMemory(checked);
                   2049:           TtaFreeMemory(name);
                   2050:           TtaFreeMemory(types);
1.199     kia      2051:         }
1.200     kia      2052:     }
1.198     kia      2053: }
                   2054: 
                   2055: /*----------------------------------------------------------------------
1.180     kia      2056:   TemplateCreateRepeat
                   2057:   Create a xt:repeat around the selection.
                   2058:   ----------------------------------------------------------------------*/
                   2059: void TemplateCreateRepeat(Document doc, View view)
                   2060: {
1.198     kia      2061:   Template_CreateRepeatFromSelection(doc, FALSE);
                   2062: }
                   2063: 
                   2064: /*----------------------------------------------------------------------
                   2065:   TemplateCreateRepeatComp
                   2066:   Create a xt:component with the selection and use it in a new xt:repeat
                   2067:   ----------------------------------------------------------------------*/
                   2068: void TemplateCreateRepeatComp(Document doc, View view)
                   2069: {
                   2070:   Template_CreateRepeatFromSelection(doc, TRUE);
                   2071: }
                   2072: 
                   2073: /*----------------------------------------------------------------------
                   2074:   Template_CreateRepeatFromSelection
                   2075:   Create a xt:repeat with the selection.
                   2076:   If selection is empty, insert an inline xt:use.
                   2077:   If createComp is false, create a xt:use with types at the selected block. 
                   2078:   If createComp is true, create a component with the selection.
                   2079:   Return the xt:use element.
                   2080:   ----------------------------------------------------------------------*/
                   2081: Element Template_CreateRepeatFromSelection(Document doc, ThotBool createComp)
                   2082: {
1.180     kia      2083: #ifdef TEMPLATES
1.187     kia      2084:   ThotBool    oldStructureChecking;
                   2085:   DisplayMode dispMode;
                   2086:   Element     selElem, selElem2, parent, parent2;
                   2087:   ElementType selType, selType2;
1.180     kia      2088:   int         firstChar, lastChar, firstChar2, lastChar2;
                   2089:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
1.197     kia      2090:   const char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATCOMP);
                   2091:   const char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATLABEL);
                   2092:   char        buffer[128];
1.180     kia      2093: 
1.186     kia      2094:   ElementType repType;
1.187     kia      2095:   Element     rep, use;
1.180     kia      2096: 
                   2097:   if (!TtaGetDocumentAccessMode(doc))
1.198     kia      2098:     return NULL;
1.180     kia      2099:   
                   2100:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   2101:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2102:     {
                   2103:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2104:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1.185     kia      2105: 
1.180     kia      2106:       if(selElem && selElem2)
                   2107:         {
                   2108:           selType =  TtaGetElementType(selElem);
                   2109:           selType2 =  TtaGetElementType(selElem2);
                   2110: 
                   2111:           repType.ElSSchema = sstempl;
                   2112:           repType.ElTypeNum = Template_EL_repeat;
1.186     kia      2113:           
1.185     kia      2114:           parent  = TtaGetParent(selElem);
                   2115:           parent2 = TtaGetParent(selElem2);
                   2116: 
1.197     kia      2117:           QueryStringFromUser(label, title, buffer, 127);
                   2118:           if(firstChar==0 && firstChar2==0 && parent == parent2 && buffer[0]!=0)
1.180     kia      2119:             {
1.187     kia      2120:               dispMode = TtaGetDisplayMode (doc);
                   2121:               if (dispMode == DisplayImmediately)
                   2122:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   2123:               oldStructureChecking = TtaGetStructureChecking (doc);
                   2124:               TtaSetStructureChecking (FALSE, doc);
                   2125:               
                   2126:               if(selElem==selElem2 && selType.ElSSchema==sstempl && 
                   2127:                   (selType.ElTypeNum==Template_EL_useEl || 
                   2128:                    selType.ElTypeNum==Template_EL_useSimple))
                   2129:                 use = selElem;
                   2130:               else
1.198     kia      2131:                 use = Template_CreateUseFromSelection(doc, createComp);
1.187     kia      2132:               if(use)
                   2133:                 {
                   2134:                   TtaExtendUndoSequence(doc);
                   2135:                   
                   2136:                   rep = TtaNewElement(doc, repType);
1.197     kia      2137:                   
1.187     kia      2138:                   TtaInsertSibling(rep, use, FALSE, doc);
1.197     kia      2139:                   SetAttributeStringValue(rep, Template_ATTR_title, buffer);
1.187     kia      2140:                   TtaRegisterElementCreate(rep, doc);
1.197     kia      2141:                   
1.187     kia      2142:                   TtaRegisterElementDelete (use, doc);
                   2143:                   TtaRemoveTree(use, doc);
                   2144:                   
                   2145:                   TtaInsertFirstChild(&use, rep, doc);
                   2146:                   TtaRegisterElementCreate(use, doc);
                   2147:                   
                   2148:                   TtaSelectElement(doc, use);
                   2149:                   TtaCloseUndoSequence(doc);
                   2150:                 }
                   2151:               
                   2152:               TtaSetStructureChecking (oldStructureChecking, doc);
                   2153:               TtaSetDisplayMode (doc, dispMode);
1.198     kia      2154:               return rep;
1.187     kia      2155:             }
                   2156:         }
                   2157:     }
                   2158: #endif /* TEMPLATES */
1.198     kia      2159:   return NULL;
1.195     kia      2160: }
                   2161: 
1.187     kia      2162: /*----------------------------------------------------------------------
                   2163:   TemplateCreateUseBox
                   2164:   Create a xt:use around the selection.
                   2165:   ----------------------------------------------------------------------*/
                   2166: void TemplateCreateUseBox(Document doc, View view)
                   2167: {
                   2168: #ifdef TEMPLATES
1.197     kia      2169:   Template_CreateUseFromSelection(doc, FALSE);
1.187     kia      2170: #endif /* TEMPLATES */
                   2171: }
                   2172: 
1.194     kia      2173: /*----------------------------------------------------------------------
1.195     kia      2174:   TemplateCreateUseCompBox
                   2175:   Create a xt:use around the selection.
                   2176:   ----------------------------------------------------------------------*/
                   2177: void TemplateCreateUseCompBox(Document doc, View view)
                   2178: {
                   2179: #ifdef TEMPLATES
1.197     kia      2180:   Template_CreateUseFromSelection(doc, TRUE);
1.195     kia      2181: #endif /* TEMPLATES */
                   2182: }
                   2183: 
                   2184: /*----------------------------------------------------------------------
1.194     kia      2185:   Template_CreateInlineUse
                   2186:   Create an inline xt:use with the selection.
                   2187:   ----------------------------------------------------------------------*/
                   2188: static ThotBool Template_CreateInlineUse(Document doc)
                   2189: {
                   2190: #ifdef TEMPLATES
                   2191:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   2192:   int            firstChar, lastChar;
                   2193:   Element        selElem, use;
                   2194:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2195:   char          *proposed, *label = NULL, *types=NULL;
                   2196:   ThotBool       option, res = FALSE;
                   2197:   
                   2198:   if(t && sstempl)
                   2199:     {
                   2200:       proposed = Template_GetInlineLevelDeclarations(t, TRUE, TRUE);
                   2201:       if(QueryNewUseFromUser(proposed, &label, &types, &option))
                   2202:         {
                   2203:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2204: 
                   2205:           GenerateInlineElement(Template_EL_useEl, sstempl, 0, NULL, TRUE);
                   2206:           
                   2207:           TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.197     kia      2208:           
                   2209:           use = selElem;
                   2210:           TtaNextSibling(&use);
                   2211: 
1.194     kia      2212:           SetAttributeStringValue(use, Template_ATTR_title, label);
                   2213:           SetAttributeStringValue(use, Template_ATTR_types, types);
                   2214:           res = TRUE;
                   2215:           
                   2216:           TtaSelectElement(doc, use);
                   2217:           TtaCloseUndoSequence(doc);
                   2218:         }
                   2219:       
                   2220:       TtaFreeMemory(proposed);
                   2221:       TtaFreeMemory(label);
                   2222:       TtaFreeMemory(types);
                   2223:       return res;
                   2224:     }  
                   2225:   
                   2226: #endif /* TEMPLATES */
                   2227:   return FALSE;
                   2228:  
                   2229: }
                   2230: 
                   2231: /*----------------------------------------------------------------------
                   2232:   Template_CreateEmptyBlockUse
                   2233:   Create a block xt:use with the selection.
                   2234:   ----------------------------------------------------------------------*/
                   2235: static ThotBool Template_CreateEmptyBlockUse(Document doc)
                   2236: {
                   2237: #ifdef TEMPLATES
                   2238:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   2239:   ElementType    useType;
                   2240:   int            firstChar, lastChar;
                   2241:   Element        selElem, use;
                   2242:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2243:   char          *proposed, *label = NULL, *types=NULL;
                   2244:   ThotBool       option, res = FALSE;
                   2245:   
                   2246:   
                   2247:   if(t && sstempl)
                   2248:     {
                   2249:       proposed = Template_GetBlockLevelDeclarations(t, TRUE);
                   2250:       if(QueryNewUseFromUser(proposed, &label, &types, &option))
                   2251:         {
                   2252:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2253: 
                   2254:           TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2255:           useType.ElSSchema = sstempl;
                   2256:           useType.ElTypeNum = Template_EL_useEl;
                   2257:           use = TtaNewElement(doc, useType);
                   2258:           
                   2259:           TtaInsertSibling(use, selElem, FALSE, doc);
                   2260:           TtaDeleteTree(selElem, doc);
                   2261:           
                   2262:           SetAttributeStringValue(use, Template_ATTR_title, label);
                   2263:           SetAttributeStringValue(use, Template_ATTR_types, types);
                   2264:           TtaRegisterElementCreate(use, doc);
                   2265:           res = TRUE;
                   2266:           
                   2267:           TtaSelectElement(doc, use);
                   2268:           TtaCloseUndoSequence(doc);
                   2269: 
                   2270:         }
                   2271:       
                   2272:       TtaFreeMemory(proposed);
                   2273:       TtaFreeMemory(label);
                   2274:       TtaFreeMemory(types);
                   2275:       return res;    
                   2276:     }  
                   2277:   
                   2278: #endif /* TEMPLATES */
                   2279:   return FALSE;
                   2280: }
1.187     kia      2281: 
                   2282: /*----------------------------------------------------------------------
                   2283:   Template_CreateUseFromSelection
                   2284:   Create a xt:use with the selection.
1.197     kia      2285:   If selection is empty, insert an inline xt:use.
                   2286:   If createComp is false, create a xt:use with types at the selected block. 
                   2287:   If createComp is true, create a component with the selection.
                   2288:   Return the xt:use element.
1.187     kia      2289:   ----------------------------------------------------------------------*/
1.197     kia      2290: Element Template_CreateUseFromSelection(Document doc, ThotBool createComp)
1.187     kia      2291: {
                   2292: #ifdef TEMPLATES
                   2293:   ThotBool    oldStructureChecking;
                   2294:   DisplayMode dispMode;
                   2295:   Element     selElem, selElem2, parent, parent2;
                   2296:   ElementType selType, selType2;
                   2297:   int         firstChar, lastChar, firstChar2, lastChar2;
1.194     kia      2298:   SSchema     sstempl = TtaGetSSchema ("Template", doc),
                   2299:               sshtml  = TtaGetSSchema ("HTML", doc);
1.187     kia      2300: 
                   2301:   ElementType useType;
                   2302:   Element     use = NULL, comp, prev;
                   2303:   char        buffer[128];
                   2304:   int         sz = 128;
                   2305: 
                   2306:   if (!TtaGetDocumentAccessMode(doc))
                   2307:     return NULL;
                   2308:   
                   2309:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   2310:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2311:     {
                   2312:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2313:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   2314: 
                   2315:       if(selElem && selElem2)
                   2316:         {
                   2317:           selType =  TtaGetElementType(selElem);
                   2318:           selType2 =  TtaGetElementType(selElem2);
                   2319:           parent  = TtaGetParent(selElem);
                   2320:           parent2 = TtaGetParent(selElem2);
                   2321: 
1.194     kia      2322:           dispMode = TtaGetDisplayMode (doc);
                   2323:           if (dispMode == DisplayImmediately)
                   2324:             TtaSetDisplayMode (doc, DeferredDisplay);
                   2325:           oldStructureChecking = TtaGetStructureChecking (doc);
                   2326:           TtaSetStructureChecking (FALSE, doc);
                   2327:           
1.187     kia      2328:           if(firstChar==0 && firstChar2==0 && parent == parent2)
                   2329:             {
1.194     kia      2330:               if(selType.ElSSchema==sshtml && selType.ElTypeNum==HTML_EL_Element)
                   2331:                 Template_CreateEmptyBlockUse(doc);
                   2332:               else
1.186     kia      2333:                 {
1.194     kia      2334:                   prev = selElem;
                   2335:                   TtaPreviousSibling(&prev);
1.197     kia      2336: 
                   2337:                   if(createComp)
                   2338:                     {
                   2339:                       // Create a xt:component from selection and create a xt:use using it 
                   2340:                       comp = Template_CreateComponentFromSelection(doc);
                   2341:                       if(comp)
                   2342:                         {
                   2343:                           TtaExtendUndoSequence(doc);
                   2344:                           
                   2345:                           useType.ElSSchema = sstempl;
                   2346:                           useType.ElTypeNum = Template_EL_useEl;
                   2347:                           use = TtaNewElement(doc, useType);
                   2348:                           if(prev)
                   2349:                             TtaInsertSibling(use, prev, FALSE, doc);
                   2350:                           else
                   2351:                             TtaInsertFirstChild(&use, parent, doc);
                   2352:                           
                   2353:                           GiveAttributeStringValueFromNum(comp, Template_ATTR_name, buffer, &sz);
                   2354:                           SetAttributeStringValue(use, Template_ATTR_types, buffer);
                   2355:                           TtaRegisterElementCreate(use, doc);
                   2356:                           
                   2357:                           TtaSelectElement(doc, use);
                   2358:                           TtaCloseUndoSequence(doc);
                   2359:                         }
                   2360:                     }
                   2361:                   else
1.194     kia      2362:                     {
1.197     kia      2363:                       // Create a xt:use around the selection
                   2364:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.194     kia      2365:                       
1.197     kia      2366:                       TtaRegisterElementDelete (selElem, doc);
                   2367:                       TtaRemoveTree(selElem, doc);
                   2368: 
1.194     kia      2369:                       useType.ElSSchema = sstempl;
1.197     kia      2370:                       useType.ElTypeNum = Template_EL_useSimple;
1.194     kia      2371:                       use = TtaNewElement(doc, useType);
                   2372:                       if(prev)
                   2373:                         TtaInsertSibling(use, prev, FALSE, doc);
                   2374:                       else
                   2375:                         TtaInsertFirstChild(&use, parent, doc);
                   2376:                       
1.197     kia      2377:                       TtaInsertFirstChild(&selElem, use, doc);
                   2378:                       
                   2379:                       SetAttributeStringValue(use, Template_ATTR_types, TtaGetElementTypeName(TtaGetElementType(selElem)));
                   2380:                       
1.194     kia      2381:                       TtaRegisterElementCreate(use, doc);
                   2382:                       
                   2383:                       TtaCloseUndoSequence(doc);
                   2384:                     }
1.186     kia      2385:                 }
                   2386:             }
1.194     kia      2387:           else if(lastChar<firstChar)
                   2388:               Template_CreateInlineUse(doc);
                   2389:           
                   2390:           TtaSetStructureChecking (oldStructureChecking, doc);
                   2391:           TtaSetDisplayMode (doc, dispMode);
                   2392:           
1.186     kia      2393:         }
                   2394:     }
1.187     kia      2395:   return use;
                   2396: #else /* TEMPLATES */
                   2397:   return NULL;
1.186     kia      2398: #endif /* TEMPLATES */
                   2399: }
                   2400: 
                   2401: /*----------------------------------------------------------------------
1.187     kia      2402:   Template_CreateComponentFromSelection
1.186     kia      2403:   Create a xt:component with the selection and move it into the xt:head.
1.187     kia      2404:   Return the xt:component element.
1.186     kia      2405:   ----------------------------------------------------------------------*/
1.187     kia      2406: Element Template_CreateComponentFromSelection(Document doc)
1.186     kia      2407: {
                   2408: #ifdef TEMPLATES
1.187     kia      2409:   ThotBool    oldStructureChecking;
                   2410:   DisplayMode dispMode;
1.186     kia      2411:   Element     selElem, selElem2, parent, parent2, current, child, head;
                   2412:   ElementType selType, selType2;
                   2413:   int         firstChar, lastChar, firstChar2, lastChar2;
                   2414:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
1.192     kia      2415:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2416:   
1.186     kia      2417:   ElementType compType;
                   2418:   Element     comp = NULL;
                   2419:   char        buffer[128];
1.200     kia      2420:   int         sz = 128;
1.185     kia      2421: 
1.186     kia      2422:   const char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_NEWCOMP);
                   2423:   const char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
                   2424: 
1.192     kia      2425:   if(doc && t && TtaGetDocumentAccessMode(doc) &&
1.186     kia      2426:       TtaGetDocumentAccessMode(doc) && sstempl &&
                   2427:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2428:     {
                   2429:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2430:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   2431: 
                   2432:       if(selElem && selElem2)
                   2433:         {
                   2434:           selType =  TtaGetElementType(selElem);
                   2435:           selType2 =  TtaGetElementType(selElem2);
                   2436: 
                   2437:           QueryStringFromUser(label, title, buffer, 127);
1.190     kia      2438:           if(buffer[0]!=EOS)
1.186     kia      2439:             {
1.190     kia      2440:               head = TemplateFindHead(doc);
                   2441:               
                   2442:               compType.ElSSchema = sstempl;
                   2443:               compType.ElTypeNum = Template_EL_component;
                   2444:     
                   2445:               parent  = TtaGetParent(selElem);
                   2446:               parent2 = TtaGetParent(selElem2);
1.185     kia      2447:               
1.190     kia      2448:               if(head && firstChar==0 && firstChar2==0 && parent == parent2)
1.180     kia      2449:                 {
1.190     kia      2450:                   dispMode = TtaGetDisplayMode (doc);
                   2451:                   if (dispMode == DisplayImmediately)
                   2452:                     TtaSetDisplayMode (doc, DeferredDisplay);
                   2453:                   oldStructureChecking = TtaGetStructureChecking (doc);
                   2454:                   TtaSetStructureChecking (FALSE, doc);
                   2455:     
                   2456:                   child = TtaGetLastChild(head);
                   2457:                   TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2458:                   comp = TtaNewElement(doc, compType);
1.185     kia      2459:                   if(child)
1.190     kia      2460:                     TtaInsertSibling(comp, child, FALSE, doc);
1.185     kia      2461:                   else
1.190     kia      2462:                     TtaInsertFirstChild(&comp, head, doc);
                   2463:                   TtaRegisterElementCreate(comp, doc);
                   2464:                   
                   2465:                   TtaNextSibling(&selElem2);
                   2466:                   child = NULL;
                   2467:                   while(selElem!=selElem2)
                   2468:                     {
                   2469:                       current = selElem;
                   2470:                       TtaNextSibling(&selElem);
                   2471:                       
                   2472:                       TtaRegisterElementDelete (current, doc);
                   2473:                       TtaRemoveTree(current, doc);
                   2474:                       if(child)
                   2475:                         TtaInsertSibling(current, child, FALSE, doc);
                   2476:                       else
                   2477:                         TtaInsertFirstChild(&current, comp, doc);
                   2478:                       TtaRegisterElementDelete (current, doc);
                   2479:                       child = current;
                   2480:                     }
1.200     kia      2481:                   if(!Template_SetName(doc, comp, buffer, TRUE))
                   2482:                       GiveAttributeStringValueFromNum(comp, Template_ATTR_name, buffer, &sz);
1.190     kia      2483:                   
                   2484:                   TtaCloseUndoSequence(doc);
                   2485:                   TtaSelectElement(doc, comp);
                   2486:                   
1.192     kia      2487:                   Template_DeclareNewComponent(t, buffer, comp);
                   2488:                   
1.190     kia      2489:                   TtaSetStructureChecking (oldStructureChecking, doc);
                   2490:                   TtaSetDisplayMode (doc, dispMode);
1.180     kia      2491:                 }
                   2492:             }
                   2493:         }
                   2494:     }
1.186     kia      2495:   return comp;
1.187     kia      2496: #else /* TEMPLATES */
                   2497:   return NULL;
                   2498: #endif /* TEMPLATES */ 
1.180     kia      2499: }
1.187     kia      2500: 
1.192     kia      2501: /*----------------------------------------------------------------------
                   2502:   TemplateComponentWillBeDeleted
                   2503:   Processed when a component element will be deleted in a template context.
                   2504:   ----------------------------------------------------------------------*/
                   2505: ThotBool TemplateComponentWillBeDeleted (NotifyElement *event)
                   2506: {
                   2507: #ifdef TEMPLATES
                   2508:   XTigerTemplate t = GetXTigerDocTemplate(event->document);
                   2509:   char* elemName = GetAttributeStringValueFromNum(event->element, Template_ATTR_name, NULL);
                   2510:   
                   2511:   if(Template_IsUsedComponent(t, event->document, elemName))
                   2512:     {
                   2513:       TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_TEMPLATE_USEDCOMP_CANTREMOVE);
                   2514:       return TRUE;
                   2515:     }
                   2516: #endif /* TEMPLATES */ 
                   2517:   return FALSE;
                   2518: }
1.187     kia      2519: 
1.200     kia      2520: /*----------------------------------------------------------------------
                   2521:   UnionClicked
                   2522:   Called when a xt:union is clicked 
                   2523:   ----------------------------------------------------------------------*/
                   2524: void UnionClicked(NotifyElement* event)
                   2525: {
                   2526:   if(event->document && event->element)
                   2527:     TtaSelectElement(event->document, event->element);
                   2528: }
                   2529: 
                   2530: /*----------------------------------------------------------------------
                   2531:   UnionDoubleClicked
                   2532:   Called when a xt:union is double clicked 
                   2533:   ----------------------------------------------------------------------*/
                   2534: ThotBool UnionDoubleClicked(NotifyElement* event)
                   2535: {
                   2536:   Template_ModifyUnionElement(event->document, event->element);
                   2537:   TtaSelectElement(event->document, event->element);
                   2538:   return TRUE;
                   2539: }
                   2540: 
                   2541: /*----------------------------------------------------------------------
                   2542:   TemplateNameAttributeDeleted
                   2543:   Called when a xt:name will be deleted 
                   2544:   ----------------------------------------------------------------------*/
                   2545: ThotBool TemplateNameAttributeDeleted(NotifyAttribute* event)
                   2546: {
                   2547:   // Prevent xt:name deletion
                   2548:   return TRUE;
                   2549: }
                   2550: 
                   2551: /*----------------------------------------------------------------------
                   2552:   TemplateNameAttributeModified
                   2553:   Called when a xt:name will be modified 
                   2554:   ----------------------------------------------------------------------*/
                   2555: ThotBool TemplateNameAttributeModified(NotifyAttribute* event)
                   2556: {
                   2557:   // Prevent xt:name modification
                   2558:   return TRUE;
                   2559: }
                   2560: 
                   2561: /*----------------------------------------------------------------------
                   2562:   TemplateNameAttributeCreated
                   2563:   Called when a xt:name have been created 
                   2564:   ----------------------------------------------------------------------*/
                   2565: ThotBool TemplateNameAttributeCreated(NotifyAttribute* event)
                   2566: {
1.201   ! vatton   2567:   ThotBool ret;
        !          2568: 
        !          2569:   ret = MakeUniqueName(event->element, event->document, event->attribute, FALSE);
1.200     kia      2570:   return TRUE;
                   2571: }
                   2572: 
                   2573: 
                   2574: /*----------------------------------------------------------------------
                   2575:   TemplateNameAttrInMenu
                   2576:   Called by Thot when building the Attributes menu for template elements.
                   2577:   ----------------------------------------------------------------------*/
                   2578: ThotBool TemplateNameAttrInMenu (NotifyAttribute * event)
                   2579: {
                   2580: #ifdef TEMPLATES
                   2581:   ElementType type = TtaGetElementType(event->element);
                   2582:   if(type.ElTypeNum==Template_EL_component || 
                   2583:       type.ElTypeNum==Template_EL_union)
                   2584:     return TRUE;
                   2585: #endif /* TEMPLATES */
                   2586:     return FALSE;
                   2587: }
                   2588: 
1.187     kia      2589: 

Webmaster