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

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:   QueryMenu
                    686:   Show a context menu to query a choice.
                    687:   @param items space-separated choice list string.
                    688:   @return The choosed item 0-based index or -1 if none. 
                    689:   ----------------------------------------------------------------------*/
1.89      kia       690: static int QueryMenu(Document doc, char* items)
                    691: {
                    692:   int nbitems, size;
                    693:   struct menuType *itemlist;
                    694:   char *menuString;
                    695:   
1.112     vatton    696:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       697:     return -1;
                    698:   
1.89      kia       699:   size = strlen(items);
                    700:   giveItems (items, size, &itemlist, &nbitems);
                    701:   menuString = createMenuString (itemlist, nbitems);
                    702:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    703:                      nbitems, menuString , NULL, false, 'L');
                    704:   TtaFreeMemory (menuString);
                    705:   ReturnOption = -1;
                    706:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    707:   TtaWaitShowProcDialogue ();
                    708:   TtaDestroyDialogue (BaseDialog + OptionMenu);
                    709:   TtaFreeMemory (itemlist);
                    710:   return ReturnOption;
                    711: }
                    712: 
1.99      kia       713: /*----------------------------------------------------------------------
                    714:   QueryStringFromMenu
                    715:   Show a context menu to query a choice.
                    716:   @param items space-separated choice list string.
                    717:   @return The choosed item string or NULL if none.
                    718:   ----------------------------------------------------------------------*/
1.90      kia       719: static char* QueryStringFromMenu(Document doc, char* items)
                    720: {
                    721:   int nbitems, size;
                    722:   struct menuType *itemlist;
                    723:   char *menuString;
                    724:   char *result = NULL;
                    725:   
1.112     vatton    726:   if (!TtaGetDocumentAccessMode(doc))
                    727:     return NULL;
                    728:   if (items == NULL)
1.110     kia       729:     return NULL;
1.90      kia       730:   size = strlen(items);
                    731:   giveItems (items, size, &itemlist, &nbitems);
                    732:   menuString = createMenuString (itemlist, nbitems);
                    733:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    734:                      nbitems, menuString , NULL, false, 'L');
                    735:   TtaFreeMemory (menuString);
                    736:   ReturnOption = -1;
                    737:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    738:   TtaWaitShowProcDialogue ();
                    739:   TtaDestroyDialogue (BaseDialog + OptionMenu);
                    740:   
1.112     vatton    741:   if (ReturnOption!=-1)
1.90      kia       742:   {
                    743:     result = TtaStrdup(itemlist[ReturnOption].label);
                    744:   }
                    745:   
                    746:   TtaFreeMemory (itemlist);
                    747:   return result;
                    748: }
1.92      kia       749: #endif /* TEMPLATES */
1.90      kia       750: 
1.56      francesc  751: /*----------------------------------------------------------------------
1.79      quint     752:   RepeatButtonClicked
1.89      kia       753:   Called when a repeat button is clicked.
                    754:   Can be called for useEl, useSimple or repeat.
                    755:   If called for useEl or useSimple, the new element must be added after.
                    756:   If called for repeat, the element must be added before all.
                    757:    
1.56      francesc  758:   Shows a menu with all the types that can be used in a use element.
                    759:   ----------------------------------------------------------------------*/
1.79      quint     760: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  761: {
                    762: #ifdef TEMPLATES
1.89      kia       763:   Document        doc = event->document;
                    764:   Element         el = event->element;
                    765:   ElementType     elType;
1.90      kia       766:   XTigerTemplate  t;
                    767:   Declaration     decl;
                    768:   Element         repeatEl = el;
                    769:   Element         firstEl;
                    770:   Element         newEl = NULL;
1.89      kia       771:   char*           types;
1.90      kia       772:   ThotBool        oldStructureChecking;
1.95      kia       773:   View            view;
1.104     kia       774:   char*           listtypes;
                    775:   char*           result;
                    776: 
1.112     vatton    777:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       778:     return TRUE;
1.89      kia       779:   
1.95      kia       780:   TtaGetActiveView (&doc, &view);
                    781:   if (view != 1)
                    782:     return FALSE; /* let Thot perform normal operation */
                    783: 
1.112     vatton    784: #ifdef AMAYA_DEBUG
1.109     kia       785:   printf("Template url : %s\n", DocumentMeta[doc]->template_url);
1.112     vatton    786: #endif/* AMAYA_DEBUG */
1.109     kia       787: 
1.89      kia       788:   TtaCancelSelection(doc);
1.90      kia       789:   
                    790:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.89      kia       791:   elType = TtaGetElementType(el);
1.112     vatton    792:   while (elType.ElTypeNum!=Template_EL_repeat)
1.89      kia       793:   {
1.90      kia       794:     repeatEl = TtaGetParent(repeatEl);
1.112     vatton    795:     if (repeatEl==NULL)
1.89      kia       796:       break;
1.90      kia       797:     elType = TtaGetElementType(repeatEl);
1.89      kia       798:   }
1.112     vatton    799:   if (repeatEl)
1.89      kia       800:   {
1.112     vatton    801:     if (Template_CanInsertRepeatChild(repeatEl))
1.90      kia       802:     {
1.98      kia       803:       firstEl = TtaGetFirstChild(repeatEl);
1.104     kia       804:       types = GetAttributeStringValueFromNum(firstEl, Template_ATTR_types, NULL);
1.112     vatton    805:       if (types)
1.90      kia       806:       {
1.104     kia       807:         listtypes = Template_ExpandTypes(t, types);
                    808:         result = QueryStringFromMenu(doc, listtypes);
1.112     vatton    809:         if (result)
1.98      kia       810:         {
1.104     kia       811:           decl = Template_GetDeclaration(t, result);
1.112     vatton    812:           if (decl)
1.104     kia       813:           {
                    814:             /* Prepare insertion.*/          
                    815:             oldStructureChecking = TtaGetStructureChecking (doc);
                    816:             TtaSetStructureChecking (FALSE, doc);
                    817:             TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
                    818:             /* Insert. */
1.112     vatton    819:             if (el==repeatEl)
1.104     kia       820:               newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, NULL);
                    821:             else
                    822:               newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, el);
                    823:               
                    824:             /* Finish insertion.*/
                    825:             TtaCloseUndoSequence(doc);
1.114   ! vatton    826:             TtaSetDocumentModified (doc);
1.104     kia       827:             TtaSetStructureChecking (oldStructureChecking, doc);
1.99      kia       828:             
1.104     kia       829:             firstEl = GetFirstEditableElement(newEl);
1.112     vatton    830:             if (firstEl)
1.104     kia       831:             {
                    832:               TtaSelectElement (doc, firstEl);
                    833:               TtaSetStatusSelectedElement(doc, view, firstEl);
                    834:             }
                    835:             else
                    836:             {
                    837:               TtaSelectElement (doc, newEl);
                    838:               TtaSetStatusSelectedElement(doc, view, newEl);
                    839:             }
1.98      kia       840:           }
                    841:         }
1.90      kia       842:       }
1.104     kia       843:       TtaFreeMemory(types);
1.98      kia       844:       TtaFreeMemory(listtypes);
                    845:       TtaFreeMemory(result);
                    846:     }
1.112     vatton    847:     else /* if (Template_CanInsertRepeatChild(repeatEl)) */
1.98      kia       848:     {
                    849:       TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.90      kia       850:     }
1.89      kia       851:   }
1.77      vatton    852:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc  853: #endif /* TEMPLATES */
1.94      kia       854:   return TRUE;
                    855: }
                    856: 
                    857: /*----------------------------------------------------------------------
                    858:   UseButtonClicked
                    859:   Shows a menu with all the types that can be used in a use element.
                    860:   ----------------------------------------------------------------------*/
                    861: ThotBool UseButtonClicked (NotifyElement *event)
                    862: {
                    863: #ifdef TEMPLATES
                    864:   Document        doc = event->document;
                    865:   Element         el = event->element;
1.99      kia       866:   Element         child;
1.94      kia       867:   ElementType     elType;
                    868:   XTigerTemplate  t;
                    869:   Declaration     decl;
                    870:   Element         firstEl;
                    871:   Element         newEl = NULL;
                    872:   char*           types;
                    873:   ThotBool        oldStructureChecking;
1.95      kia       874:   View            view;
1.104     kia       875:   char*           listtypes;
                    876:   char*           result;
1.95      kia       877: 
1.112     vatton    878:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       879:     return TRUE;
                    880:     
1.95      kia       881:   TtaGetActiveView (&doc, &view);
                    882:   if (view != 1)
                    883:     return FALSE; /* let Thot perform normal operation */
1.94      kia       884:   
                    885:   TtaCancelSelection(doc);
                    886:   
                    887:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
                    888:   if (!t)
                    889:     return FALSE; /* let Thot perform normal operation */
                    890:   elType = TtaGetElementType(el);
                    891: 
                    892:   firstEl = TtaGetFirstChild(el);
1.112     vatton    893:   if (firstEl)
1.94      kia       894:   {
                    895:     RepeatButtonClicked(event);
                    896:   }
                    897:   else
                    898:   {
1.104     kia       899:     types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.112     vatton    900:     if (types)
1.94      kia       901:     {
1.104     kia       902:       listtypes = Template_ExpandTypes(t, types);
                    903:       result = QueryStringFromMenu(doc, listtypes);
1.112     vatton    904:       if (result)
1.94      kia       905:       {
1.104     kia       906:         decl = Template_GetDeclaration(t, result);
1.112     vatton    907:         if (decl)
1.99      kia       908:         {
1.104     kia       909:           /* Prepare insertion.*/
                    910:           oldStructureChecking = TtaGetStructureChecking (doc);
                    911:           TtaSetStructureChecking (FALSE, doc);
                    912:           TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
                    913:           
                    914:           /* Insert */
                    915:           newEl = Template_InsertUseChildren(doc, el, decl);
                    916:           
                    917:           for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
                    918:           {
                    919:             TtaRegisterElementCreate(child, doc);
                    920:           }
                    921:           
                    922:           TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
                    923:           TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
                    924:           
                    925:           /* Finish insertion. */
                    926:           TtaCloseUndoSequence(doc);
1.114   ! vatton    927:           TtaSetDocumentModified (doc);
1.104     kia       928:           TtaSetStructureChecking (oldStructureChecking, doc);
                    929:           
                    930:           firstEl = GetFirstEditableElement(newEl);
1.112     vatton    931:           if (firstEl)
1.104     kia       932:           {
                    933:             TtaSelectElement (doc, firstEl);
                    934:             TtaSetStatusSelectedElement(doc, view, firstEl);
                    935:           }
                    936:           else
                    937:           {
                    938:             TtaSelectElement (doc, newEl);
                    939:             TtaSetStatusSelectedElement(doc, view, newEl);
                    940:           }
1.98      kia       941:         }
1.94      kia       942:       }
                    943:     }
1.104     kia       944:     TtaFreeMemory(types);
1.94      kia       945:     TtaFreeMemory(listtypes);
                    946:     TtaFreeMemory(result);
                    947:   }
                    948:   
                    949:   return TRUE;
                    950: #endif /* TEMPLATES */
1.56      francesc  951:        return TRUE;
                    952: }
1.64      francesc  953: 
1.89      kia       954: 
1.103     kia       955: /*----------------------------------------------------------------------
                    956:   UseSimpleButtonClicked
                    957:   ----------------------------------------------------------------------*/
                    958: ThotBool UseSimpleButtonClicked (NotifyElement *event)
                    959: {
                    960: #ifdef TEMPLATES
1.112     vatton    961:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia       962:     return TRUE;
                    963: 
1.103     kia       964:   ElementType parentType = TtaGetElementType(TtaGetParent(event->element));
1.112     vatton    965:   if (parentType.ElTypeNum == Template_EL_repeat)
1.103     kia       966:     return RepeatButtonClicked(event);
                    967: #endif /* TEMPLATES */  
                    968:   return FALSE;
                    969: }
1.94      kia       970: 
                    971: /*----------------------------------------------------------------------
                    972:   OptionButtonClicked
                    973:   ----------------------------------------------------------------------*/
                    974: ThotBool OptionButtonClicked (NotifyElement *event)
                    975: {
                    976: #ifdef TEMPLATES
                    977:   Element         child, grandChild, next;
                    978:   ElementType     elType, elType1;
                    979:   Document        doc;
                    980:   XTigerTemplate  t;
                    981:   View            view;
                    982: 
1.112     vatton    983:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia       984:     return TRUE;
                    985: 
1.94      kia       986:   TtaGetActiveView (&doc, &view);
                    987:   if (view != 1)
                    988:     return FALSE; /* let Thot perform normal operation */
1.110     kia       989: 
1.94      kia       990:   doc = event->document;
                    991:   child = TtaGetFirstChild (event->element);
                    992:   if (!child)
                    993:     return FALSE; /* let Thot perform normal operation */
                    994:   elType = TtaGetElementType (child);
                    995:   elType1 = TtaGetElementType (event->element);
                    996:   if ((elType.ElTypeNum != Template_EL_useEl &&
                    997:        elType.ElTypeNum != Template_EL_useSimple) ||
                    998:       elType.ElSSchema != elType1.ElSSchema)
                    999:     return FALSE;
                   1000: 
                   1001:   TtaCancelSelection (doc);
                   1002:   grandChild = TtaGetFirstChild (child);
                   1003:   if (!grandChild)
                   1004:     /* the "use" element is empty. Instantiate it */
                   1005:     {
                   1006:       t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
                   1007:       if (!t)
                   1008:         return FALSE; // no template ?!?!
                   1009:       InstantiateUse (t, child, doc, TRUE);
                   1010:     }
                   1011:   else
                   1012:     /* remove the content of the "use" element */
                   1013:     {
                   1014:       do
                   1015:         {
                   1016:           next = grandChild;
                   1017:           TtaNextSibling (&next);
                   1018:           TtaDeleteTree (grandChild, doc);
                   1019:           grandChild = next;
                   1020:         }
                   1021:       while (next);
                   1022:     }
                   1023:   TtaSelectElement (doc, event->element);
                   1024:   return TRUE; /* don't let Thot perform normal operation */
                   1025: #endif /* TEMPLATES */
                   1026:   return TRUE;
                   1027: }
                   1028: 
1.111     vatton   1029: /*----------------------------------------------------------------------
                   1030:   CheckTemplate checks if the template of the instance is loaded
                   1031:   ----------------------------------------------------------------------*/
                   1032: void CheckTemplate (Document doc)
                   1033: {
                   1034: #ifdef TEMPLATES
                   1035:   if (DocumentMeta[doc] && DocumentMeta[doc]->template_url &&
                   1036:       !Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url))
                   1037:     {
                   1038:       // the template cannot be loaded
1.112     vatton   1039:       InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
1.111     vatton   1040:       TtaSetAccessRight (TtaGetRootElement (doc), ReadOnly, doc);
1.112     vatton   1041:       TtaSetDocumentAccessMode (doc, 0); // document readonly
1.111     vatton   1042:     }
                   1043: #endif /* TEMPLATES */
                   1044: }
1.94      kia      1045: 
1.66      vatton   1046: /*----------------------------------------------------------------------
1.108     vatton   1047:   OpeningInstance checks if it is a template instance needs.
                   1048:   If it's an instance and the template is not loaded, load it into a
                   1049:   temporary file
1.66      vatton   1050:   ----------------------------------------------------------------------*/
1.76      vatton   1051: void OpeningInstance (char *fileName, Document doc)
1.65      francesc 1052: {
                   1053: #ifdef TEMPLATES
1.76      vatton   1054:   XTigerTemplate   t;
1.103     kia      1055:   char            *content, *ptr, *begin;
1.76      vatton   1056:   gzFile           stream;
                   1057:   char             buffer[2000];
1.77      vatton   1058:   int              res;
1.65      francesc 1059: 
1.76      vatton   1060:   stream = TtaGZOpen (fileName);
                   1061:   if (stream != 0)
1.65      francesc 1062:     {
1.76      vatton   1063:       res = gzread (stream, buffer, 1999);
                   1064:       if (res >= 0)
1.65      francesc 1065:         {
1.81      vatton   1066:           buffer[res] = EOS;
1.103     kia      1067:           begin = strstr (buffer, "<?xtiger");
                   1068:           
1.112     vatton   1069:           if (begin)
1.103     kia      1070:           {
                   1071:             // Search for template version
                   1072:             ptr = strstr (begin, "templateVersion");
                   1073:             if (ptr)
                   1074:               ptr = strstr (ptr, "=");
                   1075:             if (ptr)
                   1076:               ptr = strstr (ptr, "\"");
                   1077:             if (ptr)
                   1078:               {
                   1079:                 // template URI
                   1080:                 content = &ptr[1];
                   1081:                 ptr = strstr (content, "\"");
                   1082:               }
                   1083:             if (ptr)
                   1084:               {
                   1085:                 *ptr = EOS;
                   1086:                 //Get now the template URI
                   1087:                 DocumentMeta[doc]->template_version = TtaStrdup (content);
                   1088:                 *ptr = '"';
                   1089:               }
                   1090:            
                   1091:             // Search for template uri
                   1092:             ptr = strstr (begin, "template");
                   1093:             if (ptr && ptr[8] != 'V')
                   1094:               ptr = strstr (ptr, "=");
                   1095:             if (ptr)
                   1096:               ptr = strstr (ptr, "\"");
                   1097:             if (ptr)
                   1098:               {
                   1099:                 // template URI
                   1100:                 content = &ptr[1];
                   1101:                 ptr = strstr (content, "\"");
                   1102:               }
                   1103:             if (ptr)
                   1104:               {
                   1105:                 *ptr = EOS;
                   1106:                 //Get now the template URI
                   1107:                 DocumentMeta[doc]->template_url = TtaStrdup (content);
                   1108:                 if (Templates_Dic == NULL)
                   1109:                   InitializeTemplateEnvironment ();
                   1110:                 t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
                   1111:                 if (!t)
                   1112:                   {
1.108     vatton   1113:                     LoadTemplate (doc, content);
1.103     kia      1114:                     t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
                   1115:                   }
                   1116:                 AddUser (t);
                   1117:                 *ptr = '"';
                   1118:               }
                   1119:           }
1.65      francesc 1120:         }
                   1121:     }
1.76      vatton   1122:   TtaGZClose (stream);
1.65      francesc 1123: #endif /* TEMPLATES */
                   1124: }
                   1125: 
1.64      francesc 1126: /*----------------------------------------------------------------------
1.65      francesc 1127:   ClosingInstance
1.64      francesc 1128:   Callback called before closing a document. Checks for unused templates.
                   1129:   ----------------------------------------------------------------------*/
1.65      francesc 1130: ThotBool ClosingInstance(NotifyDialog* dialog)
1.64      francesc 1131: {
1.65      francesc 1132: #ifdef TEMPLATES
                   1133:   //If it is a template all has been already freed
1.76      vatton   1134:   if (DocumentMeta[dialog->document] == NULL)
                   1135:     return FALSE;
1.65      francesc 1136: 
                   1137:   char *turl = DocumentMeta[dialog->document]->template_url;
1.73      vatton   1138:   if (turl)
1.104     kia      1139:   {
                   1140:     XTigerTemplate t = (XTigerTemplate) Dictionary_Get (Templates_Dic, turl);
                   1141:     if (t)
                   1142:       RemoveUser (t);
                   1143:     TtaFreeMemory (turl);
                   1144:     DocumentMeta[dialog->document]->template_url = NULL;
                   1145:   }
                   1146:   
1.112     vatton   1147:   if (DocumentMeta[dialog->document]->template_version)
1.104     kia      1148:   {
                   1149:     TtaFreeMemory(DocumentMeta[dialog->document]->template_version);
                   1150:     DocumentMeta[dialog->document]->template_version = NULL;
                   1151:   }
1.65      francesc 1152: #endif /* TEMPLATES */
                   1153:   return FALSE;
1.64      francesc 1154: }
1.87      kia      1155: 
                   1156: 
                   1157: /*----------------------------------------------------------------------
                   1158:   GetFirstTemplateParentElement
                   1159:   Return the first element wich has "Template" as SShema name or null if none.
                   1160:   ----------------------------------------------------------------------*/
                   1161: ThotBool IsTemplateElement(Element elem)
                   1162: {
                   1163: #ifdef TEMPLATES
                   1164:   return strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
                   1165:                                                     , TEMPLATE_SSHEMA_NAME)==0;
                   1166: #else
                   1167:   return FALSE;
                   1168: #endif /* TEMPLATES */
                   1169: }
                   1170: 
                   1171: 
                   1172: /*----------------------------------------------------------------------
                   1173:   GetFirstTemplateParentElement
                   1174:   Return the first element wich has "Template" as SShema name or null if none.
                   1175:   ----------------------------------------------------------------------*/
                   1176: Element GetFirstTemplateParentElement(Element elem)
                   1177: {
                   1178: #ifdef TEMPLATES
                   1179:   elem = TtaGetParent(elem);
1.112     vatton   1180:   while (elem!=NULL && strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
1.87      kia      1181:                                                     , TEMPLATE_SSHEMA_NAME)!=0)
                   1182:   {
                   1183:     elem = TtaGetParent(elem);
                   1184:   }
                   1185:   return elem;
                   1186: #else
                   1187:   return NULL;
                   1188: #endif /* TEMPLATES */
                   1189: }
1.101     kia      1190: 
1.103     kia      1191: 
1.101     kia      1192: /*----------------------------------------------------------------------
1.102     vatton   1193:   TemplateElementWillBeCreated
1.101     kia      1194:   Processed when an element will be created in a template context.
                   1195:   ----------------------------------------------------------------------*/
1.102     vatton   1196: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101     kia      1197: {
1.103     kia      1198: #ifdef TEMPLATES
                   1199:   ElementType elType = event->elementType;
                   1200:   Element     parent = event->element;
                   1201:   ElementType parentType = TtaGetElementType(parent);
1.113     kia      1202:   Element     ancestor;
                   1203:   ElementType ancestorType;
                   1204:   SSchema     templateSSchema;
                   1205:   char*       types;
                   1206:   ThotBool    b;
1.101     kia      1207: 
1.112     vatton   1208:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1209:     return TRUE;
                   1210: 
1.113     kia      1211:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.102     vatton   1212:   if (templateSSchema == NULL)
                   1213:     return FALSE; // let Thot do the job
1.103     kia      1214:   
1.106     vatton   1215: #ifdef AMAYA_DEBUG 
1.113     kia      1216:   printf("TemplateElementWillBeCreated %s:%s:%d\n", TtaGetSSchemaName(elType.ElSSchema), TtaGetElementTypeName(elType), elType.ElTypeNum);
1.103     kia      1217:   printf("    ^^ %s:%s\n", TtaGetSSchemaName(parentType.ElSSchema), TtaGetElementTypeName(parentType));
1.106     vatton   1218: #endif /* AMAYA_DEBUG */
1.103     kia      1219: 
1.113     kia      1220: 
                   1221:   // Fisrt, test if in a xt:bag or in a base-element xt:use
                   1222:   if(parentType.ElSSchema==templateSSchema)
                   1223:     ancestor = parent;
                   1224:   else
                   1225:     ancestor = GetFirstTemplateParentElement(parent);
                   1226: 
                   1227:   if(ancestor)
                   1228:   {
                   1229:     ancestorType = TtaGetElementType(ancestor);
                   1230: 
                   1231: #ifdef AMAYA_DEBUG 
                   1232:   printf("    <> %s:%s\n", TtaGetSSchemaName(ancestorType.ElSSchema), TtaGetElementTypeName(ancestorType));
                   1233: #endif /* AMAYA_DEBUG */
                   1234: 
                   1235:     if(ancestorType.ElTypeNum==Template_EL_bag)
                   1236:     {
                   1237:       types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_types, NULL);
                   1238:       b = Template_CanInsertElementInBag(event->document, elType, types);
                   1239:       TtaFreeMemory(types);
                   1240:       return !b;      
                   1241:     }
                   1242:     else if(ancestorType.ElTypeNum==Template_EL_useSimple || ancestorType.ElTypeNum==Template_EL_useEl)
                   1243:     {
                   1244:       types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
                   1245:       b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position); 
                   1246:       return !b;      
                   1247:       
                   1248:     }
                   1249:   }
                   1250:   // Can not insert.
                   1251:   return TRUE;
1.101     kia      1252: #endif /* TEMPLATES*/
1.102     vatton   1253:   return FALSE;
1.101     kia      1254: }
                   1255: 
                   1256: /*----------------------------------------------------------------------
                   1257:   TemplateElementWillBeDeleted
                   1258:   Processed when an element will be deleted in a template context.
                   1259:   ----------------------------------------------------------------------*/
                   1260: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1261: {
1.107     kia      1262: #ifdef TEMPLATES
                   1263:   Document       doc = event->document;
                   1264:   Element        elem = event->element;
                   1265:   Element        xtElem, parent;
1.109     kia      1266:   Element        sibling;
1.107     kia      1267:   ElementType    xtType;
                   1268:   char*          type;
                   1269:   Declaration    dec;
                   1270:   SSchema        templateSSchema = TtaGetSSchema ("Template", event->document);
                   1271:   XTigerTemplate t;
                   1272: 
1.112     vatton   1273:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1274:     return TRUE;
                   1275: 
1.107     kia      1276:   if (templateSSchema == NULL)
                   1277:     return FALSE; // let Thot do the job
                   1278:   
1.103     kia      1279:   
1.107     kia      1280:   xtElem = GetFirstTemplateParentElement(elem);
1.112     vatton   1281:   if (xtElem)
1.107     kia      1282:   {
                   1283:     xtType = TtaGetElementType(xtElem);
1.109     kia      1284:     t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
                   1285: 
1.112     vatton   1286:     if (xtType.ElTypeNum==Template_EL_bag)
1.109     kia      1287:       return FALSE; // xt:bag always allow remove children.
1.112     vatton   1288:     else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107     kia      1289:     {
1.109     kia      1290:       parent = TtaGetParent(elem);
1.112     vatton   1291:       if (xtElem!=parent){
1.109     kia      1292:       type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
                   1293:       dec = Template_GetDeclaration(t, type);
                   1294:       TtaFreeMemory(type);
1.112     vatton   1295:       if (dec->nature == XmlElementNat)
1.109     kia      1296:         return FALSE; // Can remove element only if in xt:use current type is base language element. 
                   1297:       else
                   1298:         return TRUE;
1.107     kia      1299:       }
1.109     kia      1300:     }
1.112     vatton   1301:     else if (xtType.ElTypeNum==Template_EL_repeat)
1.109     kia      1302:     {
                   1303:       sibling = TtaGetSuccessor(elem);
                   1304:       TtaRegisterElementDelete(elem, doc);
                   1305:       TtaDeleteTree(elem, doc);
                   1306:       InstantiateRepeat(t, xtElem, doc, TRUE);
                   1307:       TtaSelectElement(doc, sibling);
                   1308:       return TRUE;
1.107     kia      1309:     }
                   1310:   }
1.109     kia      1311:   
                   1312:   //TODO Test if current element is use or repeat.
                   1313:   // Because if an element is delete and it is the unique child of its parent,
                   1314:   // the parent intends to destroy itself. 
                   1315:   
1.107     kia      1316:   return TRUE;
                   1317: #else /* TEMPLATES */
1.101     kia      1318:   return FALSE;
1.107     kia      1319: #endif /* TEMPLATES */
1.101     kia      1320: }
                   1321: 
1.109     kia      1322: /*----------------------------------------------------------------------
                   1323:   CurrentTypeWillBeExported
                   1324:   Check if the xt:currentType attribute can be exported
                   1325:   ----------------------------------------------------------------------*/
                   1326: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
                   1327: {
                   1328: #ifdef TEMPLATES
1.110     kia      1329: 
1.112     vatton   1330:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1331:     return TRUE;
                   1332: 
1.112     vatton   1333:   if (IsTemplateDocument(event->document))
1.109     kia      1334:     return TRUE;
                   1335: #endif /* TEMPLATES */
                   1336:   return FALSE;
                   1337: }

Webmaster