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

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

Webmaster