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

1.1       cvs         1: /*
                      2:  *
1.100     vatton      3:  *  COPYRIGHT INRIA and W3C, 1996-2007
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.1       cvs        10:  *
                     11:  */
                     12: 
                     13: #define THOT_EXPORT extern
                     14: #include "amaya.h"
                     15: #include "document.h"
1.52      vatton     16: 
1.99      kia        17: #include "undo.h"
                     18: 
1.90      kia        19: #include "containers.h"
                     20: #include "Elemlist.h"
1.92      kia        21: #include "templates.h"
                     22: 
1.90      kia        23: 
1.46      vatton     24: #ifdef TEMPLATES
                     25: #include "Template.h"
1.52      vatton     26: #include "templateDeclarations.h"
1.89      kia        27: #include "templateUtils_f.h"
1.52      vatton     28: 
1.69      quint      29: #include "mydictionary_f.h"
1.67      quint      30: #include "templateLoad_f.h"
                     31: #include "templateDeclarations_f.h"
1.76      vatton     32: #include "templateInstantiate_f.h"
1.28      tollenae   33: #include "appdialogue_wx.h"
1.29      tollenae   34: #include "init_f.h"
1.46      vatton     35: #include "wxdialogapi_f.h"
1.60      francesc   36: #include "AHTURLTools_f.h"
                     37: 
1.52      vatton     38: #endif /* TEMPLATES */
1.1       cvs        39: 
1.87      kia        40: 
                     41: #include "fetchXMLname_f.h"
1.83      kia        42: #include "MENUconf.h"
                     43: 
1.87      kia        44: 
1.83      kia        45: /* Paths from which looking for templates.*/
                     46: static Prop_Templates_Path *TemplateRepositoryPaths;
                     47: 
1.87      kia        48: 
                     49: /*----------------------------------------------------------------------
                     50:   IsTemplateInstanceDocument: Test if a document is a template instance
                     51:   doc : Document to test
                     52:   return : TRUE if the document is a template instance
                     53:   ----------------------------------------------------------------------*/
1.106     vatton     54: ThotBool IsTemplateInstanceDocument(Document doc)
                     55: {
1.87      kia        56: #ifdef TEMPLATES
                     57:   return (DocumentMeta[doc]!=NULL) && (DocumentMeta[doc]->template_url!=NULL);
                     58: #else  /* TEMPLATES */
1.88      cvs        59:   return FALSE;
1.87      kia        60: #endif /* TEMPLATES */
                     61: }
                     62: 
1.83      kia        63: /*----------------------------------------------------------------------
                     64:   AllocTemplateRepositoryListElement: alloc an element for the list of template repositories.
                     65:   path : path of the new element
                     66:   return : address of the new element
                     67:   ----------------------------------------------------------------------*/
                     68: void* AllocTemplateRepositoryListElement (const char* path, void* prevElement)
                     69: {
                     70:   Prop_Templates_Path *element = (Prop_Templates_Path*)TtaGetMemory (sizeof(Prop_Templates_Path));
                     71:   element->NextPath = NULL;
                     72:   strcpy (element->Path, path);
                     73:   if (prevElement)
                     74:   {
                     75:     element->NextPath = ((Prop_Templates_Path*)prevElement)->NextPath;
                     76:     ((Prop_Templates_Path*)prevElement)->NextPath = element;
                     77:   }
                     78:   return element;
                     79: }
                     80: 
                     81: 
                     82: /*----------------------------------------------------------------------
                     83:   FreeTemplateRepositoryList: Free the list of template repositories.
                     84:   list : address of the list (address of the first element).
                     85:   ----------------------------------------------------------------------*/
                     86: void FreeTemplateRepositoryList (void* list)
                     87: {
                     88:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                     89:   
                     90:   Prop_Templates_Path* element = *l;
                     91:   l = NULL;
                     92:   while (element)
                     93:   {
                     94:     Prop_Templates_Path* next = element->NextPath;
                     95:     TtaFreeMemory(element);
                     96:     element = next;
                     97:   }
                     98: }
                     99: 
                    100: /*----------------------------------------------------------------------
                    101:   CopyTemplateRepositoryList: Copy a list of template repositories.
                    102:   src : address of the list (address of the first element).
                    103:   dst : address where copy the list
                    104:   ----------------------------------------------------------------------*/
1.91      vatton    105: static void CopyTemplateRepositoryList (const Prop_Templates_Path** src,
                    106:                                         Prop_Templates_Path** dst)
1.83      kia       107: {
                    108:   Prop_Templates_Path *element=NULL, *current=NULL;
                    109:   
                    110:   if(*src!=NULL)
                    111:   {
                    112:     *dst = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    113:     (*dst)->NextPath = NULL;
                    114:     strcpy((*dst)->Path, (*src)->Path);
                    115:     
                    116:     element = (*src)->NextPath;
                    117:     current = *dst;
                    118:   }
                    119: 
1.106     vatton    120:   while (element)
                    121:     {
1.83      kia       122:     current->NextPath = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    123:     current = current->NextPath; 
                    124:     current->NextPath = NULL;
                    125:     strcpy(current->Path, element->Path);
                    126:     element = element->NextPath;
1.106     vatton    127:     }
1.83      kia       128: }
                    129: 
                    130: /*----------------------------------------------------------------------
                    131:   LoadTemplateRepositoryList: Load the list of template repositories.
                    132:   list   : address of the list (address of the first element).
                    133:   return : the number of readed repository paths.
                    134:   ----------------------------------------------------------------------*/
                    135: static int LoadTemplateRepositoryList (Prop_Templates_Path** list)
                    136: {
                    137:   Prop_Templates_Path *element, *current = NULL;
                    138:   char *path, *homePath;
                    139:   unsigned char *c;
                    140:   int nb = 0;
                    141:   FILE *file;
                    142:   
                    143:   FreeTemplateRepositoryList(list);
                    144:   
                    145:   path = (char *) TtaGetMemory (MAX_LENGTH);
1.86      vatton    146:   homePath       = TtaGetEnvString ("APP_HOME");
1.106     vatton    147:   sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83      kia       148:   
                    149:   file = TtaReadOpen ((char *)path);
1.84      kia       150:   if (!file)
                    151:   {
                    152:     /* The config file dont exist, create it. */
                    153:     file = TtaWriteOpen ((char *)path);
1.106     vatton    154:     fprintf (file, "%s%ctemplate.xtd\n", homePath, DIR_SEP);
1.84      kia       155:     TtaWriteClose (file);
                    156:     /* Retry to open it.*/
                    157:     file = TtaReadOpen ((char *)path);
                    158:   }
                    159:   
1.83      kia       160:   if (file)
                    161:   {
1.84      kia       162:     c = (unsigned char*)path;
                    163:     *c = EOS;
1.83      kia       164:     while (TtaReadByte (file, c)){
                    165:       if (*c==13 || *c==EOL)
                    166:         *c = EOS;
                    167:       if (*c==EOS && c!=(unsigned char*)path )
                    168:       {
                    169:         element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    170:         element->NextPath = NULL;
                    171:         strcpy (element->Path, path);
                    172:         
                    173:         if (*list == NULL)
                    174:           *list = element; 
                    175:         else
                    176:           current->NextPath = element;
                    177:         current = element;
                    178:         nb++;
                    179: 
                    180:         c = (unsigned char*) path;
                    181:         *c = EOS;
                    182:       }
                    183:       else
                    184:         c++;
                    185:     }
                    186:     if (c!=(unsigned char*)path && *path!=EOS)
                    187:     {
                    188:       element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    189:       *(c+1) = EOS;
                    190:       strcpy (element->Path, path);
                    191:       element->NextPath = NULL;
                    192:       
                    193:       if (*list == NULL)
                    194:         *list = element; 
                    195:       else
                    196:         current->NextPath = element;
                    197:       nb++;
                    198:     }
                    199:     TtaReadClose (file);
                    200:   }
                    201:   TtaFreeMemory(path);
                    202:   return nb;
                    203: }
                    204: 
                    205: /*----------------------------------------------------------------------
                    206:   SaveTemplateRepositoryList: Save the list of template repositories.
                    207:   list   : address of the list (address of the first element).
                    208:   ----------------------------------------------------------------------*/
                    209: static void SaveTemplateRepositoryList (const Prop_Templates_Path** list)
                    210: {
                    211:   const Prop_Templates_Path *element;
                    212:   char *path, *homePath;
                    213:   unsigned char *c;
                    214:   FILE *file;
                    215: 
                    216:   path = (char *) TtaGetMemory (MAX_LENGTH);
                    217:   homePath       = TtaGetEnvString ("APP_HOME");
1.106     vatton    218:   sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83      kia       219: 
                    220:   file = TtaWriteOpen ((char *)path);
                    221:   c = (unsigned char*)path;
                    222:   *c = EOS;
                    223:   if (file)
                    224:   {
                    225:     element = *list;
                    226:     while (element)
                    227:     {
                    228:       fprintf(file, "%s\n", element->Path);
                    229:       element = element->NextPath;
                    230:     }
                    231:     TtaWriteClose (file);
                    232:   }
                    233: }
                    234: 
                    235: /*----------------------------------------------------------------------
                    236:   GetTemplateRepositoryList: Get the list of template repositories from template environment.
                    237:   list : address of the list (address of the first element).
                    238:   ----------------------------------------------------------------------*/
                    239: void GetTemplateRepositoryList (void* list)
                    240: {
                    241:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                    242:   CopyTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths, l);
                    243: }
                    244: 
                    245: /*----------------------------------------------------------------------
                    246:   SetTemplateRepositoryList: Set the list of template repositories environment.
                    247:   list : address of the list (address of the first element).
                    248:   ----------------------------------------------------------------------*/
                    249: void SetTemplateRepositoryList (const void* list)
                    250: {
                    251:   const Prop_Templates_Path** l = (const Prop_Templates_Path**) list;
                    252:   CopyTemplateRepositoryList((const Prop_Templates_Path**)l, &TemplateRepositoryPaths);
                    253:   SaveTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths);
                    254: }
                    255: 
                    256: /*-----------------------------------------------------------------------
                    257:    InitTemplates
                    258:    Initializes the annotation library
                    259:   -----------------------------------------------------------------------*/
                    260: void InitTemplates ()
                    261: {
                    262:   TtaSetEnvBoolean ("SHOW_TEMPLATES", TRUE, FALSE);
                    263:   LoadTemplateRepositoryList(&TemplateRepositoryPaths);
                    264: }
                    265: 
                    266: 
1.1       cvs       267: /*----------------------------------------------------------------------
1.51      francesc  268:   NewTemplate: Create the "new document from template" dialog
1.1       cvs       269:   ----------------------------------------------------------------------*/
1.18      cvs       270: void NewTemplate (Document doc, View view)
1.1       cvs       271: {
1.51      francesc  272: #ifdef TEMPLATES
1.76      vatton    273:   char        *templateDir = TtaGetEnvString ("TEMPLATES_DIRECTORY");
                    274:   ThotBool     created;
1.28      tollenae  275: 
1.76      vatton    276:   if (Templates_Dic == NULL)
                    277:     InitializeTemplateEnvironment ();
                    278:   created = CreateNewTemplateDocDlgWX (BaseDialog + OpenTemplate,
1.61      francesc  279:                                       /*TtaGetViewFrame (doc, view)*/NULL, doc,
1.52      vatton    280:                                       TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),templateDir);
1.51      francesc  281:   
1.28      tollenae  282:   if (created)
1.25      vatton    283:     {
1.28      tollenae  284:       TtaSetDialoguePosition ();
                    285:       TtaShowDialogue (BaseDialog + OpenTemplate, TRUE);
1.25      vatton    286:     }
1.51      francesc  287: 
1.52      vatton    288: #endif /* TEMPLATES */
1.1       cvs       289: }
1.25      vatton    290: 
1.53      vatton    291: 
1.52      vatton    292: /*----------------------------------------------------------------------
1.107   ! kia       293:   PreventReloadingTemplate
        !           294:   Prevent reloading a template.
        !           295:   You must call AllowReloadingTemplate when finish.
        !           296:   Usefull for reload an instance without reloading the template.
        !           297:   ----------------------------------------------------------------------*/
        !           298: void PreventReloadingTemplate(char* template_url)
        !           299: {
        !           300: #ifdef TEMPLATES
        !           301:   XTigerTemplate t = (XTigerTemplate) Dictionary_Get (Templates_Dic, template_url);
        !           302:   if(t)
        !           303:     t->users++;
        !           304: #endif /* TEMPLATES */
        !           305: }
        !           306: 
        !           307: /*----------------------------------------------------------------------
        !           308:   AllowReloadingTemplate
        !           309:   Allow reloading a template.
        !           310:   You must call it after each PreventReloadingTemplate call.
        !           311:   ----------------------------------------------------------------------*/
        !           312: void AllowReloadingTemplate(char* template_url)
        !           313: {
        !           314: #ifdef TEMPLATES
        !           315:   XTigerTemplate t = (XTigerTemplate) Dictionary_Get (Templates_Dic, template_url);
        !           316:   if(t)
        !           317:     t->users--;  
        !           318: #endif /* TEMPLATES */  
        !           319: }
        !           320: 
        !           321: 
        !           322: /*----------------------------------------------------------------------
1.87      kia       323:   giveItems : Lists type items from string
                    324:   example : "one two three" is extracted to {one, two, three}
                    325:   note : item type are setted to SimpleTypeNat
                    326:   text : text from which list items
                    327:   size : size of text in characters
                    328:   items : address of exctracted item list
                    329:   nbitems : items number in items list
1.52      vatton    330:   ----------------------------------------------------------------------*/
1.76      vatton    331: void giveItems (char *text, int size, struct menuType **items, int *nbitems)
1.1       cvs       332: {
1.70      quint     333: #ifdef TEMPLATES
1.52      vatton    334:        ThotBool         inElement = TRUE;
                    335:   struct menuType *menu;
                    336:   char            *iter;
                    337:        char             temp[128];
                    338:   int              i;
                    339:        int              labelSize;
1.28      tollenae  340: 
1.52      vatton    341:        *nbitems = 1;
                    342:        for (i = 0; i < size; i++)
                    343:     {
                    344:       if (isEOSorWhiteSpace (text[i]))
                    345:         {
                    346:           if (inElement)
                    347:             inElement = FALSE;
                    348:         }
                    349:       else if (!inElement)
                    350:         {
                    351:           inElement = TRUE;
                    352:           (*nbitems)++;
                    353:         }
                    354:     }
1.51      francesc  355: 
1.76      vatton    356:        menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
1.52      vatton    357:        iter = text;
                    358:        for (i = 0; i < *nbitems; i++)
                    359:     {          
                    360:       labelSize = 0;
                    361:       while (isEOSorWhiteSpace (*iter))
                    362:         iter++;
                    363: 
                    364:       while (!isEOSorWhiteSpace (*iter))
                    365:         {
                    366:           temp[labelSize++] = *iter;
                    367:           iter++;
                    368:         }
                    369: 
                    370:       temp[labelSize] = EOS;
1.76      vatton    371:       menu[i].label = (char *) TtaStrdup (temp);
1.68      quint     372:       menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
1.52      vatton    373:       *items = menu;
                    374:     }
1.70      quint     375: #endif /* TEMPLATES */
1.28      tollenae  376: }
1.37      tollenae  377: 
1.70      quint     378: #ifdef TEMPLATES
1.52      vatton    379: /*----------------------------------------------------------------------
                    380:   ----------------------------------------------------------------------*/
                    381: static char *createMenuString (const struct menuType* items, const int nbItems)
                    382: {
                    383:   char *result, *iter;
                    384:        int   size = 0;
                    385:   int   i;
                    386: 
                    387:        for (i=0; i < nbItems; i++)
                    388:                size += 2 + strlen (items[i].label);
                    389: 
1.76      vatton    390:        result = (char *) TtaGetMemory (size);
1.52      vatton    391:        iter = result;
                    392:        for (i=0; i < nbItems; i++)
                    393:     {
                    394:       *iter = 'B';
                    395:       ++iter;
1.51      francesc  396:                
1.52      vatton    397:       strcpy (iter, items[i].label);
                    398:       iter += strlen (items[i].label)+1;
                    399:     }
1.51      francesc  400:        return result;
1.36      tollenae  401: }
1.71      quint     402: #endif /* TEMPLATES */
1.29      tollenae  403: 
1.71      quint     404: /*----------------------------------------------------------------------
                    405:   UseToBeCreated
                    406:   An new use element will be created by the user through some generic editing
                    407:   command
                    408:   -----------------------------------------------------------------------*/
                    409: ThotBool UseToBeCreated (NotifyElement *event)
                    410: {
                    411: #ifdef TEMPLATES
1.75      quint     412:   Element        el;
1.72      quint     413:        Document       doc;
                    414: 
                    415:   el = event->element;
                    416:   doc = event->document;
1.101     kia       417:   
1.106     vatton    418: #ifdef AMAYA_DEBUG
1.101     kia       419:   printf("UseToBeCreated\n");
1.106     vatton    420: #endif /* AMAYA_DEBUG */
1.101     kia       421:   
1.72      quint     422:   /* is there a limit to the number of elements in the xt:repeat ? */
1.71      quint     423:   /* @@@@@ */
1.52      vatton    424: #endif /* TEMPLATES */
1.71      quint     425:   return FALSE; /* let Thot perform normal operation */
                    426: }
                    427: 
                    428: /*----------------------------------------------------------------------
                    429:   UseCreated
                    430:   A new "use" element has just been created by the user with a generic editing
                    431:   command.
                    432:   -----------------------------------------------------------------------*/
                    433: void UseCreated (NotifyElement *event)
                    434: {
                    435: #ifdef TEMPLATES
                    436:        Document         doc;
                    437:        Element          el;
                    438:   XTigerTemplate   t;
                    439: 
                    440:        doc = event->document;
1.72      quint     441:   el = event->element;
                    442:   if (TtaGetFirstChild (el))
                    443:     /* this Use element has already some content. It has already been
                    444:        instanciated */
                    445:     return;
1.87      kia       446:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.71      quint     447:   if (!t)
                    448:     return; // no template ?!?!
1.101     kia       449: 
1.76      vatton    450:   InstantiateUse (t, el, doc, TRUE);
1.71      quint     451: #endif /* TEMPLATES */
                    452: }
1.29      tollenae  453: 
1.98      kia       454: /*----------------------------------------------------------------------
                    455:   Template_IncrementRepeatOccurNumber
                    456:   Increment the number of occurs of a xt:repeat
                    457:   @param el element (xt:repeat)
                    458:   ----------------------------------------------------------------------*/
                    459: void Template_IncrementRepeatOccurNumber(Element el)
                    460: {
                    461: #ifdef TEMPLATES
                    462:   char* current;
                    463:   char  newVal[8];
                    464:   int curVal;
                    465:   
1.104     kia       466:   current = GetAttributeStringValueFromNum(el, Template_ATTR_currentOccurs, NULL);
                    467:   if(current)
                    468:   {
                    469:     curVal = atoi(current);
                    470:     curVal++;
                    471:     TtaFreeMemory(current);
                    472:     sprintf(newVal, "%d", curVal);
                    473:     SetAttributeStringValue(el, Template_ATTR_currentOccurs, newVal);
                    474:   }
1.98      kia       475: #endif /* TEMPLATES */
                    476: }
                    477: 
                    478: /*----------------------------------------------------------------------
                    479:   Template_DecrementRepeatOccurNumber
                    480:   Decrement the number of occurs of a xt:repeat
                    481:   @param el element (xt:repeat)
                    482:   ----------------------------------------------------------------------*/
                    483: void Template_DecrementRepeatOccurNumber(Element el)
                    484: {
                    485: #ifdef TEMPLATES
                    486:   char* current;
                    487:   char  newVal[8];
                    488:   int curVal;
                    489:   
1.104     kia       490:   current = GetAttributeStringValueFromNum(el, Template_ATTR_currentOccurs, NULL);
                    491:   if(current)
                    492:   {
                    493:     curVal = atoi(current);
                    494:     curVal--;
                    495:     TtaFreeMemory(current);
                    496:     sprintf(newVal, "%d", curVal);
                    497:     SetAttributeStringValue(el, Template_ATTR_currentOccurs, newVal);
                    498:   }
1.98      kia       499: #endif /* TEMPLATES */
                    500: }
                    501: 
1.89      kia       502: 
1.98      kia       503: /*----------------------------------------------------------------------
                    504:   Template_CanInsertRepeatChild
                    505:   Test if a xt:repeat child can be inserted (number between params min and max).
                    506:   @param el element (xt:repeat) to test
                    507:   @return True if an element can be inserted.
                    508:   ----------------------------------------------------------------------*/
                    509: ThotBool Template_CanInsertRepeatChild(Element el)
                    510: {
                    511: #ifdef TEMPLATES
                    512:   char* max;
                    513:   char* current;
                    514:   int maxVal, curVal;
1.104     kia       515:   Element child;
1.98      kia       516:   
1.104     kia       517:   max = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
1.105     vatton    518:   if (max)
1.104     kia       519:   {
1.105     vatton    520:     if(!strcmp(max, "*"))
                    521:       {
                    522:         TtaFreeMemory(max);
                    523:         return TRUE;
                    524:       }
                    525:     maxVal = atoi (max);
                    526:     TtaFreeMemory (max);
1.104     kia       527: 
                    528:     current = GetAttributeStringValueFromNum(el, Template_ATTR_currentOccurs, NULL);
1.105     vatton    529:     if (current)
1.104     kia       530:     {
1.105     vatton    531:       curVal = atoi (current);
                    532:       TtaFreeMemory (current);
1.104     kia       533:     }
                    534:     else
                    535:     {
                    536:       curVal = 0;
1.105     vatton    537:       for (child = TtaGetFirstChild(el); child; TtaNextSibling(&child))
1.104     kia       538:       {
                    539:         curVal++;
                    540:       }
                    541:     }
                    542:   
                    543:     return curVal<maxVal;
                    544:   }
                    545:   else
1.98      kia       546:     return TRUE;
                    547: #endif /* TEMPLATES */
                    548:   return FALSE;
                    549: }
1.96      kia       550: 
1.89      kia       551: 
                    552: /*----------------------------------------------------------------------
                    553:   Template_InsertRepeatChildAfter
                    554:   Insert a child to a xt:repeat
                    555:   The decl parameter must be valid and will not be verified. It must be a
                    556:     direct child element or the "use in the use" for union elements.
                    557:   @param el element (xt:repeat) in which insert a new element
                    558:   @param decl Template declaration of the element to insert
                    559:   @param elPrev Element (xt:use) after which insert the new elem, NULL if first.
                    560:   @return The inserted element 
                    561:   ----------------------------------------------------------------------*/
                    562: Element Template_InsertRepeatChildAfter(Document doc, Element el, Declaration decl, Element elPrev)
                    563: {
                    564: #ifdef TEMPLATES
                    565:   Element useFirst; /* First xt:use of the repeat.*/
                    566:   Element use;      /* xt:use to insert.*/
                    567:   ElementType useType;  /* type of xt:use.*/
                    568:   
                    569:   /* Copy xt:use with xt:types param */
                    570:   useFirst = TtaGetFirstChild(el);
                    571:   useType = TtaGetElementType(useFirst);
                    572:   use = TtaCopyElement(useFirst, doc, doc, el);
                    573: 
                    574:   Template_InsertUseChildren(doc, use, decl);
                    575: 
                    576:   /* insert it */
                    577:   if(elPrev)
                    578:   {
                    579:     TtaInsertSibling(use, elPrev, FALSE, doc);
                    580:   }
                    581:   else
                    582:   {
                    583:     TtaInsertSibling(use, useFirst, TRUE, doc);
                    584:   }
1.99      kia       585: 
                    586:   TtaRegisterElementCreate(use, doc);
1.97      kia       587:   
1.98      kia       588:   Template_IncrementRepeatOccurNumber(el);
                    589:   
1.89      kia       590:   return use;
                    591:   
1.93      cvs       592: #else /* TEMPLATES */
                    593:   return NULL;
1.89      kia       594: #endif /* TEMPLATES */
                    595: }
                    596: 
                    597: /*----------------------------------------------------------------------
                    598:   Template_InsertRepeatChild
                    599:   Insert a child to a xt:repeat
                    600:   The decl parameter must be valid and will not be verified. It must be a
                    601:     direct child element or the "use in the use" for union elements.
                    602:   @param el element (repeat) in which insert a new element
                    603:   @param decl Template declaration of the element to insert
                    604:   @param pos Position of insertion (0 before all, 1 after first ... -1 after all)
                    605:   @return The inserted element
                    606:   ----------------------------------------------------------------------*/
                    607: Element Template_InsertRepeatChild(Document doc, Element el, Declaration decl, int pos)
                    608: {
                    609:   if(pos==0)
                    610:   {
                    611:     return Template_InsertRepeatChildAfter(doc, el, decl, NULL);
                    612:   }
                    613:   else if(pos==-1)
                    614:   {
                    615:     return Template_InsertRepeatChildAfter(doc, el, decl, TtaGetLastChild(el));
                    616:   }
                    617:   else
                    618:   {
                    619:     Element elem = TtaGetFirstChild(el);
                    620:     pos--;
                    621:     while(pos>0)
                    622:     {
                    623:       TtaNextSibling(&elem);
                    624:       pos--;
                    625:     }
                    626:     return Template_InsertRepeatChildAfter(doc, el, decl, elem);
                    627:   }
                    628: }
                    629: 
1.92      kia       630: #ifdef TEMPLATES
1.99      kia       631: /*----------------------------------------------------------------------
                    632:   QueryMenu
                    633:   Show a context menu to query a choice.
                    634:   @param items space-separated choice list string.
                    635:   @return The choosed item 0-based index or -1 if none. 
                    636:   ----------------------------------------------------------------------*/
1.89      kia       637: static int QueryMenu(Document doc, char* items)
                    638: {
                    639:   int nbitems, size;
                    640:   struct menuType *itemlist;
                    641:   char *menuString;
                    642:   
                    643:   size = strlen(items);
                    644:   giveItems (items, size, &itemlist, &nbitems);
                    645:   menuString = createMenuString (itemlist, nbitems);
                    646:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    647:                      nbitems, menuString , NULL, false, 'L');
                    648:   TtaFreeMemory (menuString);
                    649:   ReturnOption = -1;
                    650:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    651:   TtaWaitShowProcDialogue ();
                    652:   TtaDestroyDialogue (BaseDialog + OptionMenu);
                    653:   TtaFreeMemory (itemlist);
                    654:   return ReturnOption;
                    655: }
                    656: 
1.99      kia       657: /*----------------------------------------------------------------------
                    658:   QueryStringFromMenu
                    659:   Show a context menu to query a choice.
                    660:   @param items space-separated choice list string.
                    661:   @return The choosed item string or NULL if none.
                    662:   ----------------------------------------------------------------------*/
1.90      kia       663: static char* QueryStringFromMenu(Document doc, char* items)
                    664: {
                    665:   int nbitems, size;
                    666:   struct menuType *itemlist;
                    667:   char *menuString;
                    668:   char *result = NULL;
                    669:   
                    670:   size = strlen(items);
                    671:   giveItems (items, size, &itemlist, &nbitems);
                    672:   menuString = createMenuString (itemlist, nbitems);
                    673:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    674:                      nbitems, menuString , NULL, false, 'L');
                    675:   TtaFreeMemory (menuString);
                    676:   ReturnOption = -1;
                    677:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    678:   TtaWaitShowProcDialogue ();
                    679:   TtaDestroyDialogue (BaseDialog + OptionMenu);
                    680:   
                    681:   if(ReturnOption!=-1)
                    682:   {
                    683:     result = TtaStrdup(itemlist[ReturnOption].label);
                    684:   }
                    685:   
                    686:   TtaFreeMemory (itemlist);
                    687:   return result;
                    688: }
1.92      kia       689: #endif /* TEMPLATES */
1.90      kia       690: 
1.56      francesc  691: /*----------------------------------------------------------------------
1.79      quint     692:   RepeatButtonClicked
1.89      kia       693:   Called when a repeat button is clicked.
                    694:   Can be called for useEl, useSimple or repeat.
                    695:   If called for useEl or useSimple, the new element must be added after.
                    696:   If called for repeat, the element must be added before all.
                    697:    
1.56      francesc  698:   Shows a menu with all the types that can be used in a use element.
                    699:   ----------------------------------------------------------------------*/
1.79      quint     700: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  701: {
                    702: #ifdef TEMPLATES
1.89      kia       703:   Document        doc = event->document;
                    704:   Element         el = event->element;
                    705:   ElementType     elType;
1.90      kia       706:   XTigerTemplate  t;
                    707:   Declaration     decl;
                    708:   Element         repeatEl = el;
                    709:   Element         firstEl;
                    710:   Element         newEl = NULL;
1.89      kia       711:   char*           types;
1.90      kia       712:   ThotBool        oldStructureChecking;
1.95      kia       713:   View            view;
1.104     kia       714:   char*           listtypes;
                    715:   char*           result;
                    716: 
1.89      kia       717:   
1.95      kia       718:   TtaGetActiveView (&doc, &view);
                    719:   if (view != 1)
                    720:     return FALSE; /* let Thot perform normal operation */
                    721: 
1.89      kia       722:   TtaCancelSelection(doc);
1.90      kia       723:   
                    724:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.89      kia       725:   elType = TtaGetElementType(el);
                    726:   while(elType.ElTypeNum!=Template_EL_repeat)
                    727:   {
1.90      kia       728:     repeatEl = TtaGetParent(repeatEl);
                    729:     if(repeatEl==NULL)
1.89      kia       730:       break;
1.90      kia       731:     elType = TtaGetElementType(repeatEl);
1.89      kia       732:   }
1.90      kia       733:   if(repeatEl)
1.89      kia       734:   {
1.98      kia       735:     if(Template_CanInsertRepeatChild(repeatEl))
1.90      kia       736:     {
1.98      kia       737:       firstEl = TtaGetFirstChild(repeatEl);
1.104     kia       738:       types = GetAttributeStringValueFromNum(firstEl, Template_ATTR_types, NULL);
                    739:       if(types)
1.90      kia       740:       {
1.104     kia       741:         listtypes = Template_ExpandTypes(t, types);
                    742:         result = QueryStringFromMenu(doc, listtypes);
                    743:         if(result)
1.98      kia       744:         {
1.104     kia       745:           decl = Template_GetDeclaration(t, result);
                    746:           if(decl)
                    747:           {
                    748:             /* Prepare insertion.*/          
                    749:             oldStructureChecking = TtaGetStructureChecking (doc);
                    750:             TtaSetStructureChecking (FALSE, doc);
                    751:             TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
                    752:             
                    753:             /* Insert. */
                    754:             if(el==repeatEl)
                    755:               newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, NULL);
                    756:             else
                    757:               newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, el);
                    758:               
                    759:             /* Finish insertion.*/
                    760:             TtaCloseUndoSequence(doc);
                    761:             TtaSetStructureChecking (oldStructureChecking, doc);
1.99      kia       762:             
1.104     kia       763:             firstEl = GetFirstEditableElement(newEl);
                    764:             if(firstEl)
                    765:             {
                    766:               TtaSelectElement (doc, firstEl);
                    767:               TtaSetStatusSelectedElement(doc, view, firstEl);
                    768:             }
                    769:             else
                    770:             {
                    771:               TtaSelectElement (doc, newEl);
                    772:               TtaSetStatusSelectedElement(doc, view, newEl);
                    773:             }
1.98      kia       774:           }
                    775:         }
1.90      kia       776:       }
1.104     kia       777:       TtaFreeMemory(types);
1.98      kia       778:       TtaFreeMemory(listtypes);
                    779:       TtaFreeMemory(result);
                    780:     }
                    781:     else /* if(Template_CanInsertRepeatChild(repeatEl)) */
                    782:     {
                    783:       TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.90      kia       784:     }
1.89      kia       785:   }
1.77      vatton    786:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc  787: #endif /* TEMPLATES */
1.94      kia       788:   return TRUE;
                    789: }
                    790: 
                    791: /*----------------------------------------------------------------------
                    792:   UseButtonClicked
                    793:   Shows a menu with all the types that can be used in a use element.
                    794:   ----------------------------------------------------------------------*/
                    795: ThotBool UseButtonClicked (NotifyElement *event)
                    796: {
                    797: #ifdef TEMPLATES
                    798:   Document        doc = event->document;
                    799:   Element         el = event->element;
1.99      kia       800:   Element         child;
1.94      kia       801:   ElementType     elType;
                    802:   XTigerTemplate  t;
                    803:   Declaration     decl;
                    804:   Element         firstEl;
                    805:   Element         newEl = NULL;
                    806:   char*           types;
                    807:   ThotBool        oldStructureChecking;
1.95      kia       808:   View            view;
1.104     kia       809:   char*           listtypes;
                    810:   char*           result;
1.95      kia       811: 
                    812:   TtaGetActiveView (&doc, &view);
                    813:   if (view != 1)
                    814:     return FALSE; /* let Thot perform normal operation */
1.94      kia       815:   
                    816:   TtaCancelSelection(doc);
                    817:   
                    818:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
                    819:   if (!t)
                    820:     return FALSE; /* let Thot perform normal operation */
                    821:   elType = TtaGetElementType(el);
                    822: 
                    823:   firstEl = TtaGetFirstChild(el);
                    824:   if(firstEl)
                    825:   {
                    826:     RepeatButtonClicked(event);
                    827:   }
                    828:   else
                    829:   {
1.104     kia       830:     types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
                    831:     if(types)
1.94      kia       832:     {
1.104     kia       833:       listtypes = Template_ExpandTypes(t, types);
                    834:       result = QueryStringFromMenu(doc, listtypes);
                    835:       if(result)
1.94      kia       836:       {
1.104     kia       837:         decl = Template_GetDeclaration(t, result);
                    838:         if(decl)
1.99      kia       839:         {
1.104     kia       840:           /* Prepare insertion.*/
                    841:           oldStructureChecking = TtaGetStructureChecking (doc);
                    842:           TtaSetStructureChecking (FALSE, doc);
                    843:           TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
                    844:           
                    845:           /* Insert */
                    846:           newEl = Template_InsertUseChildren(doc, el, decl);
                    847:           
                    848:           for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
                    849:           {
                    850:             TtaRegisterElementCreate(child, doc);
                    851:           }
                    852:           
                    853:           TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
                    854:           TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
                    855:           
                    856:           /* Finish insertion. */
                    857:           TtaCloseUndoSequence(doc);
                    858:           TtaSetStructureChecking (oldStructureChecking, doc);
                    859:           
                    860:           firstEl = GetFirstEditableElement(newEl);
                    861:           if(firstEl)
                    862:           {
                    863:             TtaSelectElement (doc, firstEl);
                    864:             TtaSetStatusSelectedElement(doc, view, firstEl);
                    865:           }
                    866:           else
                    867:           {
                    868:             TtaSelectElement (doc, newEl);
                    869:             TtaSetStatusSelectedElement(doc, view, newEl);
                    870:           }
1.98      kia       871:         }
1.94      kia       872:       }
                    873:     }
1.104     kia       874:     TtaFreeMemory(types);
1.94      kia       875:     TtaFreeMemory(listtypes);
                    876:     TtaFreeMemory(result);
                    877:   }
                    878:   
                    879:   return TRUE;
                    880: #endif /* TEMPLATES */
1.56      francesc  881:        return TRUE;
                    882: }
1.64      francesc  883: 
1.89      kia       884: 
1.103     kia       885: /*----------------------------------------------------------------------
                    886:   UseSimpleButtonClicked
                    887:   ----------------------------------------------------------------------*/
                    888: ThotBool UseSimpleButtonClicked (NotifyElement *event)
                    889: {
                    890: #ifdef TEMPLATES
                    891:   ElementType parentType = TtaGetElementType(TtaGetParent(event->element));
                    892:   if(parentType.ElTypeNum == Template_EL_repeat)
                    893:     return RepeatButtonClicked(event);
                    894: #endif /* TEMPLATES */  
                    895:   return FALSE;
                    896: }
1.94      kia       897: 
                    898: /*----------------------------------------------------------------------
                    899:   OptionButtonClicked
                    900:   ----------------------------------------------------------------------*/
                    901: ThotBool OptionButtonClicked (NotifyElement *event)
                    902: {
                    903: #ifdef TEMPLATES
                    904:   Element         child, grandChild, next;
                    905:   ElementType     elType, elType1;
                    906:   Document        doc;
                    907:   XTigerTemplate  t;
                    908:   View            view;
                    909: 
                    910:   TtaGetActiveView (&doc, &view);
                    911:   if (view != 1)
                    912:     return FALSE; /* let Thot perform normal operation */
                    913:   doc = event->document;
                    914:   child = TtaGetFirstChild (event->element);
                    915:   if (!child)
                    916:     return FALSE; /* let Thot perform normal operation */
                    917:   elType = TtaGetElementType (child);
                    918:   elType1 = TtaGetElementType (event->element);
                    919:   if ((elType.ElTypeNum != Template_EL_useEl &&
                    920:        elType.ElTypeNum != Template_EL_useSimple) ||
                    921:       elType.ElSSchema != elType1.ElSSchema)
                    922:     return FALSE;
                    923: 
                    924:   TtaCancelSelection (doc);
                    925:   grandChild = TtaGetFirstChild (child);
                    926:   if (!grandChild)
                    927:     /* the "use" element is empty. Instantiate it */
                    928:     {
                    929:       t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
                    930:       if (!t)
                    931:         return FALSE; // no template ?!?!
                    932:       InstantiateUse (t, child, doc, TRUE);
                    933:     }
                    934:   else
                    935:     /* remove the content of the "use" element */
                    936:     {
                    937:       do
                    938:         {
                    939:           next = grandChild;
                    940:           TtaNextSibling (&next);
                    941:           TtaDeleteTree (grandChild, doc);
                    942:           grandChild = next;
                    943:         }
                    944:       while (next);
                    945:     }
                    946:   TtaSelectElement (doc, event->element);
                    947:   return TRUE; /* don't let Thot perform normal operation */
                    948: #endif /* TEMPLATES */
                    949:   return TRUE;
                    950: }
                    951: 
                    952: 
                    953: 
1.66      vatton    954: /*----------------------------------------------------------------------
                    955:   ----------------------------------------------------------------------*/
1.76      vatton    956: void OpeningInstance (char *fileName, Document doc)
1.65      francesc  957: {
                    958: #ifdef TEMPLATES
1.76      vatton    959:   XTigerTemplate   t;
1.103     kia       960:   char            *content, *ptr, *begin;
1.76      vatton    961:   gzFile           stream;
                    962:   char             buffer[2000];
1.77      vatton    963:   int              res;
1.65      francesc  964: 
1.76      vatton    965:   stream = TtaGZOpen (fileName);
                    966:   if (stream != 0)
1.65      francesc  967:     {
1.76      vatton    968:       res = gzread (stream, buffer, 1999);
                    969:       if (res >= 0)
1.65      francesc  970:         {
1.81      vatton    971:           buffer[res] = EOS;
1.103     kia       972:           begin = strstr (buffer, "<?xtiger");
                    973:           
                    974:           if(begin)
                    975:           {
                    976:             // Search for template version
                    977:             ptr = strstr (begin, "templateVersion");
                    978:             if (ptr)
                    979:               ptr = strstr (ptr, "=");
                    980:             if (ptr)
                    981:               ptr = strstr (ptr, "\"");
                    982:             if (ptr)
                    983:               {
                    984:                 // template URI
                    985:                 content = &ptr[1];
                    986:                 ptr = strstr (content, "\"");
                    987:               }
                    988:             if (ptr)
                    989:               {
                    990:                 *ptr = EOS;
                    991:                 //Get now the template URI
                    992:                 DocumentMeta[doc]->template_version = TtaStrdup (content);
                    993:                 *ptr = '"';
                    994:               }
                    995:            
                    996:             // Search for template uri
                    997:             ptr = strstr (begin, "template");
                    998:             if (ptr && ptr[8] != 'V')
                    999:               ptr = strstr (ptr, "=");
                   1000:             if (ptr)
                   1001:               ptr = strstr (ptr, "\"");
                   1002:             if (ptr)
                   1003:               {
                   1004:                 // template URI
                   1005:                 content = &ptr[1];
                   1006:                 ptr = strstr (content, "\"");
                   1007:               }
                   1008:             if (ptr)
                   1009:               {
                   1010:                 *ptr = EOS;
                   1011:                 //Get now the template URI
                   1012:                 DocumentMeta[doc]->template_url = TtaStrdup (content);
                   1013:                 if (Templates_Dic == NULL)
                   1014:                   InitializeTemplateEnvironment ();
                   1015:                 t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
                   1016:                 if (!t)
                   1017:                   {
1.106     vatton   1018:                     LoadTemplate (0, content, fileName);
1.103     kia      1019:                     t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
                   1020:                   }
                   1021:                 AddUser (t);
                   1022:                 *ptr = '"';
                   1023:               }
                   1024:           }
1.65      francesc 1025:         }
                   1026:     }
1.76      vatton   1027:   TtaGZClose (stream);
1.65      francesc 1028: #endif /* TEMPLATES */
                   1029: }
                   1030: 
1.64      francesc 1031: /*----------------------------------------------------------------------
1.65      francesc 1032:   ClosingInstance
1.64      francesc 1033:   Callback called before closing a document. Checks for unused templates.
                   1034:   ----------------------------------------------------------------------*/
1.65      francesc 1035: ThotBool ClosingInstance(NotifyDialog* dialog)
1.64      francesc 1036: {
1.65      francesc 1037: #ifdef TEMPLATES
                   1038:   //If it is a template all has been already freed
1.76      vatton   1039:   if (DocumentMeta[dialog->document] == NULL)
                   1040:     return FALSE;
1.65      francesc 1041: 
                   1042:   char *turl = DocumentMeta[dialog->document]->template_url;
1.73      vatton   1043:   if (turl)
1.104     kia      1044:   {
                   1045:     XTigerTemplate t = (XTigerTemplate) Dictionary_Get (Templates_Dic, turl);
                   1046:     if (t)
                   1047:       RemoveUser (t);
                   1048:     TtaFreeMemory (turl);
                   1049:     DocumentMeta[dialog->document]->template_url = NULL;
                   1050:   }
                   1051:   
                   1052:   if(DocumentMeta[dialog->document]->template_version)
                   1053:   {
                   1054:     TtaFreeMemory(DocumentMeta[dialog->document]->template_version);
                   1055:     DocumentMeta[dialog->document]->template_version = NULL;
                   1056:   }
1.65      francesc 1057: #endif /* TEMPLATES */
                   1058:   return FALSE;
1.64      francesc 1059: }
1.87      kia      1060: 
                   1061: 
                   1062: /*----------------------------------------------------------------------
                   1063:   GetFirstTemplateParentElement
                   1064:   Return the first element wich has "Template" as SShema name or null if none.
                   1065:   ----------------------------------------------------------------------*/
                   1066: ThotBool IsTemplateElement(Element elem)
                   1067: {
                   1068: #ifdef TEMPLATES
                   1069:   return strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
                   1070:                                                     , TEMPLATE_SSHEMA_NAME)==0;
                   1071: #else
                   1072:   return FALSE;
                   1073: #endif /* TEMPLATES */
                   1074: }
                   1075: 
                   1076: 
                   1077: /*----------------------------------------------------------------------
                   1078:   GetFirstTemplateParentElement
                   1079:   Return the first element wich has "Template" as SShema name or null if none.
                   1080:   ----------------------------------------------------------------------*/
                   1081: Element GetFirstTemplateParentElement(Element elem)
                   1082: {
                   1083: #ifdef TEMPLATES
                   1084:   elem = TtaGetParent(elem);
                   1085:   while(elem!=NULL && strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
                   1086:                                                     , TEMPLATE_SSHEMA_NAME)!=0)
                   1087:   {
                   1088:     elem = TtaGetParent(elem);
                   1089:   }
                   1090:   return elem;
                   1091: #else
                   1092:   return NULL;
                   1093: #endif /* TEMPLATES */
                   1094: }
1.101     kia      1095: 
1.103     kia      1096: 
1.101     kia      1097: /*----------------------------------------------------------------------
1.102     vatton   1098:   TemplateElementWillBeCreated
1.101     kia      1099:   Processed when an element will be created in a template context.
                   1100:   ----------------------------------------------------------------------*/
1.102     vatton   1101: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101     kia      1102: {
1.103     kia      1103: #ifdef TEMPLATES
                   1104:   ElementType elType = event->elementType;
                   1105:   Element     parent = event->element;
                   1106:   ElementType parentType = TtaGetElementType(parent);
1.104     kia      1107: //  Element     ancestor;
                   1108: //  ElementType ancestorType;
1.103     kia      1109: 
1.102     vatton   1110:   SSchema     templateSSchema = TtaGetSSchema ("Template", event->document);
1.101     kia      1111: 
1.102     vatton   1112:   if (templateSSchema == NULL)
                   1113:     return FALSE; // let Thot do the job
1.103     kia      1114:   
1.106     vatton   1115: #ifdef AMAYA_DEBUG 
1.103     kia      1116:   printf("TemplateElementWillBeCreated %s:%s\n", TtaGetSSchemaName(elType.ElSSchema), TtaGetElementTypeName(elType));
                   1117:   printf("    ^^ %s:%s\n", TtaGetSSchemaName(parentType.ElSSchema), TtaGetElementTypeName(parentType));
1.106     vatton   1118: #endif /* AMAYA_DEBUG */
1.103     kia      1119:   return FALSE;
                   1120: 
                   1121: //
                   1122: //  // A xt:use within a xt:repeat
                   1123: //  if((elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl) && parentType.ElTypeNum==Template_EL_repeat)
                   1124: //  {
                   1125: //      printf("    Intend to insert xt:repeat element\n");
                   1126: //      return !Template_CanInsertRepeatChild(parent);
                   1127: //  }
                   1128: //  else
                   1129: //  {
                   1130: //    ancestor = parent;
                   1131: //    while (ancestor)
                   1132: //    {
                   1133: //      ancestorType = TtaGetElementType(ancestor);
                   1134: //      printf("    >> %s:%s\n", TtaGetSSchemaName(ancestorType.ElSSchema), TtaGetElementTypeName(ancestorType));
                   1135: //      if (ancestorType.ElSSchema == templateSSchema && ancestorType.ElTypeNum == Template_EL_bag)
                   1136: //      {
1.104     kia      1137: //        char* types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_types, NULL);
1.103     kia      1138: //        ThotBool b = Template_CanInsertElementInBag(event->document, elType, types); 
                   1139: //        printf("    Intend to insert xt:bag element : %s\n", b?"TRUE":"FALSE");
                   1140: //        return !b;
                   1141: //      }
                   1142: //      ancestor = TtaGetParent(ancestor);
                   1143: //    }
                   1144: //  }
                   1145: //  // Can not insert.
                   1146: //  return TRUE;
1.101     kia      1147: #endif /* TEMPLATES*/
1.102     vatton   1148:   return FALSE;
1.101     kia      1149: }
                   1150: 
                   1151: /*----------------------------------------------------------------------
                   1152:   TemplateElementWillBeDeleted
                   1153:   Processed when an element will be deleted in a template context.
                   1154:   ----------------------------------------------------------------------*/
                   1155: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1156: {
1.107   ! kia      1157: #ifdef TEMPLATES
        !          1158:   Document       doc = event->document;
        !          1159:   Element        elem = event->element;
        !          1160:   Element        xtElem, parent;
        !          1161:   ElementType    xtType;
        !          1162:   char*          type;
        !          1163:   Declaration    dec;
        !          1164:   SSchema        templateSSchema = TtaGetSSchema ("Template", event->document);
        !          1165:   XTigerTemplate t;
        !          1166: 
        !          1167:   printf("TemplateElementWillBeDeleted : %s\n", TtaGetElementTypeName(TtaGetElementType(elem)));
        !          1168:   
        !          1169:   if (templateSSchema == NULL)
        !          1170:     return FALSE; // let Thot do the job
        !          1171:   
1.103     kia      1172:   
1.107   ! kia      1173:   xtElem = GetFirstTemplateParentElement(elem);
        !          1174:   if(xtElem)
        !          1175:   {
        !          1176:     xtType = TtaGetElementType(xtElem);
        !          1177:     if(xtType.ElSSchema==templateSSchema)
        !          1178:     {
        !          1179:       t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
        !          1180: 
        !          1181:       if(xtType.ElTypeNum==Template_EL_bag)
        !          1182:         return FALSE; // xt:bag always allow remove children.
        !          1183:       else if(xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
        !          1184:       {
        !          1185:         parent = TtaGetParent(elem);
        !          1186:         if(xtElem==parent){
        !          1187:           //TODO replace current xt:use child element by its initial content (reset element)
        !          1188:           return TRUE; // Cant remove use direct child. It is mandatory.
        !          1189:         }
        !          1190:         type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
        !          1191:         dec = Template_GetDeclaration(t, type);
        !          1192:         TtaFreeMemory(type);
        !          1193:         if(dec->nature == XmlElementNat)
        !          1194:           return FALSE; // Can remove element only if in xt:use current type is base language element. 
        !          1195:         else
        !          1196:           return TRUE;
        !          1197:       }
        !          1198:       else if(xtType.ElTypeNum==Template_EL_repeat)
        !          1199:       {
        !          1200:         printf("Must remove xt:repeat element and validate xt:repeat content.\n");
        !          1201:         TtaRemoveTree(elem, doc);
        !          1202:         TtaDeleteTree(elem, doc);
        !          1203:         //InstantiateRepeat(t, xtType, doc);
        !          1204:       }
        !          1205:     }
        !          1206:   }
        !          1207:   return TRUE;
        !          1208: #else /* TEMPLATES */
1.101     kia      1209:   return FALSE;
1.107   ! kia      1210: #endif /* TEMPLATES */
1.101     kia      1211: }
                   1212: 
                   1213: 

Webmaster