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

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

Webmaster