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

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

Webmaster