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

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

Webmaster