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

1.1       cvs         1: /*
                      2:  *
1.27      vatton      3:  *  COPYRIGHT INRIA and W3C, 1996-2005
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.90      kia        17: #include "containers.h"
                     18: #include "Elemlist.h"
1.92      kia        19: #include "templates.h"
                     20: 
1.90      kia        21: 
1.46      vatton     22: #ifdef TEMPLATES
                     23: #include "Template.h"
1.52      vatton     24: #include "templateDeclarations.h"
1.89      kia        25: #include "templateUtils_f.h"
1.52      vatton     26: 
1.69      quint      27: #include "mydictionary_f.h"
1.67      quint      28: #include "templateLoad_f.h"
                     29: #include "templateDeclarations_f.h"
1.76      vatton     30: #include "templateInstantiate_f.h"
1.28      tollenae   31: #include "appdialogue_wx.h"
1.29      tollenae   32: #include "init_f.h"
1.46      vatton     33: #include "wxdialogapi_f.h"
1.60      francesc   34: #include "AHTURLTools_f.h"
                     35: 
1.52      vatton     36: #endif /* TEMPLATES */
1.1       cvs        37: 
1.87      kia        38: 
                     39: #include "fetchXMLname_f.h"
1.83      kia        40: #include "MENUconf.h"
                     41: 
1.87      kia        42: 
1.83      kia        43: /* Paths from which looking for templates.*/
                     44: static Prop_Templates_Path *TemplateRepositoryPaths;
                     45: 
1.87      kia        46: 
                     47: /*----------------------------------------------------------------------
                     48:   IsTemplateInstanceDocument: Test if a document is a template instance
                     49:   doc : Document to test
                     50:   return : TRUE if the document is a template instance
                     51:   ----------------------------------------------------------------------*/
                     52: ThotBool IsTemplateInstanceDocument(Document doc){
                     53: #ifdef TEMPLATES
                     54:   return (DocumentMeta[doc]!=NULL) && (DocumentMeta[doc]->template_url!=NULL);
                     55: #else  /* TEMPLATES */
1.88      cvs        56:   return FALSE;
1.87      kia        57: #endif /* TEMPLATES */
                     58: }
                     59: 
1.83      kia        60: /*----------------------------------------------------------------------
                     61:   AllocTemplateRepositoryListElement: alloc an element for the list of template repositories.
                     62:   path : path of the new element
                     63:   return : address of the new element
                     64:   ----------------------------------------------------------------------*/
                     65: void* AllocTemplateRepositoryListElement (const char* path, void* prevElement)
                     66: {
                     67:   Prop_Templates_Path *element = (Prop_Templates_Path*)TtaGetMemory (sizeof(Prop_Templates_Path));
                     68:   element->NextPath = NULL;
                     69:   strcpy (element->Path, path);
                     70:   if (prevElement)
                     71:   {
                     72:     element->NextPath = ((Prop_Templates_Path*)prevElement)->NextPath;
                     73:     ((Prop_Templates_Path*)prevElement)->NextPath = element;
                     74:   }
                     75:   return element;
                     76: }
                     77: 
                     78: 
                     79: /*----------------------------------------------------------------------
                     80:   FreeTemplateRepositoryList: Free the list of template repositories.
                     81:   list : address of the list (address of the first element).
                     82:   ----------------------------------------------------------------------*/
                     83: void FreeTemplateRepositoryList (void* list)
                     84: {
                     85:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                     86:   
                     87:   Prop_Templates_Path* element = *l;
                     88:   l = NULL;
                     89:   while (element)
                     90:   {
                     91:     Prop_Templates_Path* next = element->NextPath;
                     92:     TtaFreeMemory(element);
                     93:     element = next;
                     94:   }
                     95: }
                     96: 
                     97: /*----------------------------------------------------------------------
                     98:   CopyTemplateRepositoryList: Copy a list of template repositories.
                     99:   src : address of the list (address of the first element).
                    100:   dst : address where copy the list
                    101:   ----------------------------------------------------------------------*/
1.91      vatton    102: static void CopyTemplateRepositoryList (const Prop_Templates_Path** src,
                    103:                                         Prop_Templates_Path** dst)
1.83      kia       104: {
                    105:   Prop_Templates_Path *element=NULL, *current=NULL;
                    106:   
                    107:   if(*src!=NULL)
                    108:   {
                    109:     *dst = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    110:     (*dst)->NextPath = NULL;
                    111:     strcpy((*dst)->Path, (*src)->Path);
                    112:     
                    113:     element = (*src)->NextPath;
                    114:     current = *dst;
                    115:   }
                    116: 
                    117:   while (element){
                    118:     current->NextPath = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    119:     current = current->NextPath; 
                    120:     current->NextPath = NULL;
                    121:     strcpy(current->Path, element->Path);
                    122:     element = element->NextPath;
                    123:   }
                    124: }
                    125: 
                    126: /*----------------------------------------------------------------------
                    127:   LoadTemplateRepositoryList: Load the list of template repositories.
                    128:   list   : address of the list (address of the first element).
                    129:   return : the number of readed repository paths.
                    130:   ----------------------------------------------------------------------*/
                    131: static int LoadTemplateRepositoryList (Prop_Templates_Path** list)
                    132: {
                    133:   Prop_Templates_Path *element, *current = NULL;
                    134:   char *path, *homePath;
                    135:   unsigned char *c;
                    136:   int nb = 0;
                    137:   FILE *file;
                    138:   
                    139:   FreeTemplateRepositoryList(list);
                    140:   
                    141:   path = (char *) TtaGetMemory (MAX_LENGTH);
1.86      vatton    142:   homePath       = TtaGetEnvString ("APP_HOME");
1.83      kia       143:   sprintf (path, "%s%ctemplate-repositories.dat", homePath, DIR_SEP);
                    144:   
                    145:   file = TtaReadOpen ((char *)path);
1.84      kia       146:   if (!file)
                    147:   {
                    148:     /* The config file dont exist, create it. */
                    149:     file = TtaWriteOpen ((char *)path);
                    150:     fprintf(file, "%s%ctemplates%cen\n", homePath, DIR_SEP, DIR_SEP);
                    151:     TtaWriteClose (file);
                    152:     /* Retry to open it.*/
                    153:     file = TtaReadOpen ((char *)path);
                    154:   }
                    155:   
1.83      kia       156:   if (file)
                    157:   {
1.84      kia       158:     c = (unsigned char*)path;
                    159:     *c = EOS;
1.83      kia       160:     while (TtaReadByte (file, c)){
                    161:       if (*c==13 || *c==EOL)
                    162:         *c = EOS;
                    163:       if (*c==EOS && c!=(unsigned char*)path )
                    164:       {
                    165:         element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    166:         element->NextPath = NULL;
                    167:         strcpy (element->Path, path);
                    168:         
                    169:         if (*list == NULL)
                    170:           *list = element; 
                    171:         else
                    172:           current->NextPath = element;
                    173:         current = element;
                    174:         nb++;
                    175: 
                    176:         c = (unsigned char*) path;
                    177:         *c = EOS;
                    178:       }
                    179:       else
                    180:         c++;
                    181:     }
                    182:     if (c!=(unsigned char*)path && *path!=EOS)
                    183:     {
                    184:       element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    185:       *(c+1) = EOS;
                    186:       strcpy (element->Path, path);
                    187:       element->NextPath = NULL;
                    188:       
                    189:       if (*list == NULL)
                    190:         *list = element; 
                    191:       else
                    192:         current->NextPath = element;
                    193:       nb++;
                    194:     }
                    195:     TtaReadClose (file);
                    196:   }
                    197:   TtaFreeMemory(path);
                    198:   return nb;
                    199: }
                    200: 
                    201: /*----------------------------------------------------------------------
                    202:   SaveTemplateRepositoryList: Save the list of template repositories.
                    203:   list   : address of the list (address of the first element).
                    204:   ----------------------------------------------------------------------*/
                    205: static void SaveTemplateRepositoryList (const Prop_Templates_Path** list)
                    206: {
                    207:   const Prop_Templates_Path *element;
                    208:   char *path, *homePath;
                    209:   unsigned char *c;
                    210:   FILE *file;
                    211: 
                    212:   path = (char *) TtaGetMemory (MAX_LENGTH);
                    213:   homePath       = TtaGetEnvString ("APP_HOME");
                    214:   sprintf (path, "%s%ctemplate-repositories.dat", homePath, DIR_SEP);
                    215: 
                    216:   file = TtaWriteOpen ((char *)path);
                    217:   c = (unsigned char*)path;
                    218:   *c = EOS;
                    219:   if (file)
                    220:   {
                    221:     element = *list;
                    222:     while (element)
                    223:     {
                    224:       fprintf(file, "%s\n", element->Path);
                    225:       element = element->NextPath;
                    226:     }
                    227:     TtaWriteClose (file);
                    228:   }
                    229: }
                    230: 
                    231: /*----------------------------------------------------------------------
                    232:   GetTemplateRepositoryList: Get the list of template repositories from template environment.
                    233:   list : address of the list (address of the first element).
                    234:   ----------------------------------------------------------------------*/
                    235: void GetTemplateRepositoryList (void* list)
                    236: {
                    237:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                    238:   CopyTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths, l);
                    239: }
                    240: 
                    241: /*----------------------------------------------------------------------
                    242:   SetTemplateRepositoryList: Set the list of template repositories environment.
                    243:   list : address of the list (address of the first element).
                    244:   ----------------------------------------------------------------------*/
                    245: void SetTemplateRepositoryList (const void* list)
                    246: {
                    247:   const Prop_Templates_Path** l = (const Prop_Templates_Path**) list;
                    248:   CopyTemplateRepositoryList((const Prop_Templates_Path**)l, &TemplateRepositoryPaths);
                    249:   SaveTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths);
                    250: }
                    251: 
                    252: /*-----------------------------------------------------------------------
                    253:    InitTemplates
                    254:    Initializes the annotation library
                    255:   -----------------------------------------------------------------------*/
                    256: void InitTemplates ()
                    257: {
                    258:   TtaSetEnvBoolean ("SHOW_TEMPLATES", TRUE, FALSE);
                    259:   LoadTemplateRepositoryList(&TemplateRepositoryPaths);
                    260: }
                    261: 
                    262: 
                    263: 
1.87      kia       264: 
                    265: 
                    266: 
1.1       cvs       267: /*----------------------------------------------------------------------
1.51      francesc  268:   NewTemplate: Create the "new document from template" dialog
1.1       cvs       269:   ----------------------------------------------------------------------*/
1.18      cvs       270: void NewTemplate (Document doc, View view)
1.1       cvs       271: {
1.51      francesc  272: #ifdef TEMPLATES
1.76      vatton    273:   char        *templateDir = TtaGetEnvString ("TEMPLATES_DIRECTORY");
                    274:   ThotBool     created;
1.28      tollenae  275: 
1.76      vatton    276:   if (Templates_Dic == NULL)
                    277:     InitializeTemplateEnvironment ();
                    278:   created = CreateNewTemplateDocDlgWX (BaseDialog + OpenTemplate,
1.61      francesc  279:                                       /*TtaGetViewFrame (doc, view)*/NULL, doc,
1.52      vatton    280:                                       TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),templateDir);
1.51      francesc  281:   
1.28      tollenae  282:   if (created)
1.25      vatton    283:     {
1.28      tollenae  284:       TtaSetDialoguePosition ();
                    285:       TtaShowDialogue (BaseDialog + OpenTemplate, TRUE);
1.25      vatton    286:     }
1.51      francesc  287: 
1.52      vatton    288: #endif /* TEMPLATES */
1.1       cvs       289: }
1.25      vatton    290: 
1.53      vatton    291: /*----------------------------------------------------------------------
                    292:   Load a template and create the instance file - update images and 
                    293:   stylesheets related to the template.
                    294:   ----------------------------------------------------------------------*/
1.61      francesc  295: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
1.53      vatton    296: {
                    297: #ifdef TEMPLATES
                    298: 
1.60      francesc  299:   char *s;
1.65      francesc  300:   ThotBool dontReplace = DontReplaceOldDoc;
1.60      francesc  301: 
                    302:   if (!IsW3Path (docname) && TtaFileExist (docname))
                    303:     {
                    304:       s = (char *)TtaGetMemory (strlen (docname) +
                    305:                                 strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
                    306:       sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
                    307:       InitConfirm (0, 0, s);
1.61      francesc  308:       TtaFreeMemory (s);      
1.62      francesc  309:       if (!UserAnswer)
                    310:         return;
                    311:     }    
                    312: 
1.76      vatton    313:   LoadTemplate (0, templatename);
1.65      francesc  314:   DontReplaceOldDoc = dontReplace;
1.76      vatton    315:   CreateInstance (templatename, docname);
1.62      francesc  316:   
1.53      vatton    317: #endif /* TEMPLATES */
                    318: }
                    319: 
1.52      vatton    320: /*----------------------------------------------------------------------
1.87      kia       321:   giveItems : Lists type items from string
                    322:   example : "one two three" is extracted to {one, two, three}
                    323:   note : item type are setted to SimpleTypeNat
                    324:   text : text from which list items
                    325:   size : size of text in characters
                    326:   items : address of exctracted item list
                    327:   nbitems : items number in items list
1.52      vatton    328:   ----------------------------------------------------------------------*/
1.76      vatton    329: void giveItems (char *text, int size, struct menuType **items, int *nbitems)
1.1       cvs       330: {
1.70      quint     331: #ifdef TEMPLATES
1.52      vatton    332:        ThotBool         inElement = TRUE;
                    333:   struct menuType *menu;
                    334:   char            *iter;
                    335:        char             temp[128];
                    336:   int              i;
                    337:        int              labelSize;
1.28      tollenae  338: 
1.52      vatton    339:        *nbitems = 1;
                    340:        for (i = 0; i < size; i++)
                    341:     {
                    342:       if (isEOSorWhiteSpace (text[i]))
                    343:         {
                    344:           if (inElement)
                    345:             inElement = FALSE;
                    346:         }
                    347:       else if (!inElement)
                    348:         {
                    349:           inElement = TRUE;
                    350:           (*nbitems)++;
                    351:         }
                    352:     }
1.51      francesc  353: 
1.76      vatton    354:        menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
1.52      vatton    355:        iter = text;
                    356:        for (i = 0; i < *nbitems; i++)
                    357:     {          
                    358:       labelSize = 0;
                    359:       while (isEOSorWhiteSpace (*iter))
                    360:         iter++;
                    361: 
                    362:       while (!isEOSorWhiteSpace (*iter))
                    363:         {
                    364:           temp[labelSize++] = *iter;
                    365:           iter++;
                    366:         }
                    367: 
                    368:       temp[labelSize] = EOS;
1.76      vatton    369:       menu[i].label = (char *) TtaStrdup (temp);
1.68      quint     370:       menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
1.52      vatton    371:       *items = menu;
                    372:     }
1.70      quint     373: #endif /* TEMPLATES */
1.28      tollenae  374: }
1.37      tollenae  375: 
1.70      quint     376: #ifdef TEMPLATES
1.52      vatton    377: /*----------------------------------------------------------------------
                    378:   ----------------------------------------------------------------------*/
                    379: static char *createMenuString (const struct menuType* items, const int nbItems)
                    380: {
                    381:   char *result, *iter;
                    382:        int   size = 0;
                    383:   int   i;
                    384: 
                    385:        for (i=0; i < nbItems; i++)
                    386:                size += 2 + strlen (items[i].label);
                    387: 
1.76      vatton    388:        result = (char *) TtaGetMemory (size);
1.52      vatton    389:        iter = result;
                    390:        for (i=0; i < nbItems; i++)
                    391:     {
                    392:       *iter = 'B';
                    393:       ++iter;
1.51      francesc  394:                
1.52      vatton    395:       strcpy (iter, items[i].label);
                    396:       iter += strlen (items[i].label)+1;
                    397:     }
1.51      francesc  398:        return result;
1.36      tollenae  399: }
1.71      quint     400: #endif /* TEMPLATES */
1.29      tollenae  401: 
1.71      quint     402: /*----------------------------------------------------------------------
                    403:   UseToBeCreated
                    404:   An new use element will be created by the user through some generic editing
                    405:   command
                    406:   -----------------------------------------------------------------------*/
                    407: ThotBool UseToBeCreated (NotifyElement *event)
                    408: {
                    409: #ifdef TEMPLATES
1.75      quint     410:   Element        el;
1.72      quint     411:        Document       doc;
                    412: 
                    413:   el = event->element;
                    414:   doc = event->document;
                    415:   /* is there a limit to the number of elements in the xt:repeat ? */
1.71      quint     416:   /* @@@@@ */
1.52      vatton    417: #endif /* TEMPLATES */
1.71      quint     418:   return FALSE; /* let Thot perform normal operation */
                    419: }
                    420: 
                    421: /*----------------------------------------------------------------------
                    422:   UseCreated
                    423:   A new "use" element has just been created by the user with a generic editing
                    424:   command.
                    425:   -----------------------------------------------------------------------*/
                    426: void UseCreated (NotifyElement *event)
                    427: {
                    428: #ifdef TEMPLATES
                    429:        Document         doc;
                    430:        Element          el;
                    431:   XTigerTemplate   t;
                    432: 
                    433:        doc = event->document;
1.72      quint     434:   el = event->element;
                    435:   if (TtaGetFirstChild (el))
                    436:     /* this Use element has already some content. It has already been
                    437:        instanciated */
                    438:     return;
1.87      kia       439:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.71      quint     440:   if (!t)
                    441:     return; // no template ?!?!
1.76      vatton    442:   InstantiateUse (t, el, doc, TRUE);
1.71      quint     443: #endif /* TEMPLATES */
                    444: }
1.29      tollenae  445: 
1.98    ! kia       446: /*----------------------------------------------------------------------
        !           447:   Template_IncrementRepeatOccurNumber
        !           448:   Increment the number of occurs of a xt:repeat
        !           449:   @param el element (xt:repeat)
        !           450:   ----------------------------------------------------------------------*/
        !           451: void Template_IncrementRepeatOccurNumber(Element el)
        !           452: {
        !           453: #ifdef TEMPLATES
        !           454:   char* current;
        !           455:   char  newVal[8];
        !           456:   int curVal;
        !           457:   
        !           458:   current = GetAttributeStringValue(el, Template_ATTR_currentOccurs, NULL);
        !           459:   curVal = atoi(current);
        !           460:   curVal++;
        !           461:   TtaFreeMemory(current);
        !           462:   sprintf(newVal, "%d", curVal);
        !           463:   SetAttributeStringValue(el, Template_ATTR_currentOccurs, newVal);
        !           464: #endif /* TEMPLATES */
        !           465: }
        !           466: 
        !           467: /*----------------------------------------------------------------------
        !           468:   Template_DecrementRepeatOccurNumber
        !           469:   Decrement the number of occurs of a xt:repeat
        !           470:   @param el element (xt:repeat)
        !           471:   ----------------------------------------------------------------------*/
        !           472: void Template_DecrementRepeatOccurNumber(Element el)
        !           473: {
        !           474: #ifdef TEMPLATES
        !           475:   char* current;
        !           476:   char  newVal[8];
        !           477:   int curVal;
        !           478:   
        !           479:   current = GetAttributeStringValue(el, Template_ATTR_currentOccurs, NULL);
        !           480:   curVal = atoi(current);
        !           481:   curVal--;
        !           482:   TtaFreeMemory(current);
        !           483:   sprintf(newVal, "%d", curVal);
        !           484:   SetAttributeStringValue(el, Template_ATTR_currentOccurs, newVal);
        !           485: #endif /* TEMPLATES */
        !           486: }
        !           487: 
1.89      kia       488: 
1.98    ! kia       489: /*----------------------------------------------------------------------
        !           490:   Template_CanInsertRepeatChild
        !           491:   Test if a xt:repeat child can be inserted (number between params min and max).
        !           492:   @param el element (xt:repeat) to test
        !           493:   @return True if an element can be inserted.
        !           494:   ----------------------------------------------------------------------*/
        !           495: ThotBool Template_CanInsertRepeatChild(Element el)
        !           496: {
        !           497: #ifdef TEMPLATES
        !           498:   char* max;
        !           499:   char* current;
        !           500:   int maxVal, curVal;
        !           501:   
        !           502:   max = GetAttributeStringValue(el, Template_ATTR_maxOccurs, NULL);
        !           503:   if(!strcmp(max, "*")){
        !           504:     return TRUE;
        !           505:   }
        !           506: 
        !           507:   current = GetAttributeStringValue(el, Template_ATTR_currentOccurs, NULL);
        !           508:   curVal = atoi(current);
        !           509:   maxVal = atoi(max);
        !           510: 
        !           511:   return curVal<maxVal;
        !           512: 
        !           513: #endif /* TEMPLATES */
        !           514:   return FALSE;
        !           515: }
1.96      kia       516: 
1.89      kia       517: /*----------------------------------------------------------------------
                    518:   Template_InsertUseChildren
                    519:   Insert children to a xt:use
                    520:   The dec parameter must be valid and will not be verified. It must be a
                    521:     direct child element (for union elements).
                    522:   @param el element (xt:use) in which insert a new element
                    523:   @param dec Template declaration of the element to insert
1.96      kia       524:   @return The inserted element (the xt:use element if insertion is multiple as component)
1.89      kia       525:   ----------------------------------------------------------------------*/
                    526: Element Template_InsertUseChildren(Document doc, Element el, Declaration dec)
                    527: {
1.96      kia       528:   Element newEl   = NULL;
1.89      kia       529: #ifdef TEMPLATES
1.96      kia       530:   Element current = NULL;
                    531:   Element child   = NULL;
                    532:   char*     attrCurrentTypeValue;
1.89      kia       533: 
                    534:   switch (dec->nature)
                    535:   {
1.96      kia       536:     case SimpleTypeNat:
                    537:       newEl = Template_GetNewSimpleTypeInstance(doc, el, dec);
                    538:       TtaInsertFirstChild(&newEl, el, doc);
1.89      kia       539:       break;
1.96      kia       540:     case XmlElementNat:
                    541:       newEl = Template_GetNewXmlElementInstance(doc, el, dec);
                    542:       TtaInsertFirstChild(&newEl, el, doc);
1.89      kia       543:       break;
1.96      kia       544:     case ComponentNat:
                    545:       newEl = Template_GetNewComponentInstance(doc, el, dec);
                    546:       
                    547:       /* Copy elements from new use to existing use. */
                    548:       while((child = TtaGetFirstChild(newEl)))
                    549:       {
                    550:         TtaRemoveTree(child, doc);
                    551:         if(current)
                    552:         {
                    553:           TtaInsertSibling(child, current, FALSE, doc);
                    554:         }
                    555:         else
                    556:         {
                    557:           TtaInsertFirstChild(&child, el, doc);      
                    558:         }
                    559:         current = child; 
                    560:       }
                    561:       
                    562:       /* Copy currentType attribute. */
                    563:       attrCurrentTypeValue = GetAttributeStringValue(newEl, Template_ATTR_currentType, NULL);
                    564:       SetAttributeStringValue(el, Template_ATTR_currentType, attrCurrentTypeValue);
                    565:       
                    566:       TtaDeleteTree(newEl, doc);
                    567:       newEl = el;
1.89      kia       568:       break;
                    569:     case UnionNat :
                    570:       /* @@@@@ */
                    571:       break;
                    572:     default :
                    573:       //Impossible
                    574:       break;   
                    575:   }
1.97      kia       576:   
1.89      kia       577: #endif /* TEMPLATES */
1.96      kia       578:   return newEl;
1.89      kia       579: }
                    580: 
                    581: 
                    582: /*----------------------------------------------------------------------
                    583:   Template_InsertRepeatChildAfter
                    584:   Insert a child to a xt:repeat
                    585:   The decl parameter must be valid and will not be verified. It must be a
                    586:     direct child element or the "use in the use" for union elements.
                    587:   @param el element (xt:repeat) in which insert a new element
                    588:   @param decl Template declaration of the element to insert
                    589:   @param elPrev Element (xt:use) after which insert the new elem, NULL if first.
                    590:   @return The inserted element 
                    591:   ----------------------------------------------------------------------*/
                    592: Element Template_InsertRepeatChildAfter(Document doc, Element el, Declaration decl, Element elPrev)
                    593: {
                    594: #ifdef TEMPLATES
                    595:   Element useFirst; /* First xt:use of the repeat.*/
                    596:   Element use;      /* xt:use to insert.*/
                    597:   ElementType useType;  /* type of xt:use.*/
                    598:   
                    599:   /* Copy xt:use with xt:types param */
                    600:   useFirst = TtaGetFirstChild(el);
                    601:   useType = TtaGetElementType(useFirst);
                    602:   use = TtaCopyElement(useFirst, doc, doc, el);
                    603: 
                    604:   Template_InsertUseChildren(doc, use, decl);
                    605: 
                    606:   /* insert it */
                    607:   if(elPrev)
                    608:   {
                    609:     TtaInsertSibling(use, elPrev, FALSE, doc);
                    610:   }
                    611:   else
                    612:   {
                    613:     TtaInsertSibling(use, useFirst, TRUE, doc);
                    614:   }
1.97      kia       615:   
1.98    ! kia       616:   Template_IncrementRepeatOccurNumber(el);
        !           617:   
1.89      kia       618:   return use;
                    619:   
1.93      cvs       620: #else /* TEMPLATES */
                    621:   return NULL;
1.89      kia       622: #endif /* TEMPLATES */
                    623: }
                    624: 
                    625: /*----------------------------------------------------------------------
                    626:   Template_InsertRepeatChild
                    627:   Insert a child to a xt:repeat
                    628:   The decl parameter must be valid and will not be verified. It must be a
                    629:     direct child element or the "use in the use" for union elements.
                    630:   @param el element (repeat) in which insert a new element
                    631:   @param decl Template declaration of the element to insert
                    632:   @param pos Position of insertion (0 before all, 1 after first ... -1 after all)
                    633:   @return The inserted element
                    634:   ----------------------------------------------------------------------*/
                    635: Element Template_InsertRepeatChild(Document doc, Element el, Declaration decl, int pos)
                    636: {
                    637:   if(pos==0)
                    638:   {
                    639:     return Template_InsertRepeatChildAfter(doc, el, decl, NULL);
                    640:   }
                    641:   else if(pos==-1)
                    642:   {
                    643:     return Template_InsertRepeatChildAfter(doc, el, decl, TtaGetLastChild(el));
                    644:   }
                    645:   else
                    646:   {
                    647:     Element elem = TtaGetFirstChild(el);
                    648:     pos--;
                    649:     while(pos>0)
                    650:     {
                    651:       TtaNextSibling(&elem);
                    652:       pos--;
                    653:     }
                    654:     return Template_InsertRepeatChildAfter(doc, el, decl, elem);
                    655:   }
                    656: }
                    657: 
1.92      kia       658: #ifdef TEMPLATES
1.89      kia       659: static int QueryMenu(Document doc, char* items)
                    660: {
                    661:   int nbitems, size;
                    662:   struct menuType *itemlist;
                    663:   char *menuString;
                    664:   
                    665:   size = strlen(items);
                    666:   giveItems (items, size, &itemlist, &nbitems);
                    667:   menuString = createMenuString (itemlist, nbitems);
                    668:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    669:                      nbitems, menuString , NULL, false, 'L');
                    670:   TtaFreeMemory (menuString);
                    671:   ReturnOption = -1;
                    672:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    673:   TtaWaitShowProcDialogue ();
                    674:   TtaDestroyDialogue (BaseDialog + OptionMenu);
                    675:   TtaFreeMemory (itemlist);
                    676:   return ReturnOption;
                    677: }
                    678: 
1.90      kia       679: static char* QueryStringFromMenu(Document doc, char* items)
                    680: {
                    681:   int nbitems, size;
                    682:   struct menuType *itemlist;
                    683:   char *menuString;
                    684:   char *result = NULL;
                    685:   
                    686:   size = strlen(items);
                    687:   giveItems (items, size, &itemlist, &nbitems);
                    688:   menuString = createMenuString (itemlist, nbitems);
                    689:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    690:                      nbitems, menuString , NULL, false, 'L');
                    691:   TtaFreeMemory (menuString);
                    692:   ReturnOption = -1;
                    693:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    694:   TtaWaitShowProcDialogue ();
                    695:   TtaDestroyDialogue (BaseDialog + OptionMenu);
                    696:   
                    697:   if(ReturnOption!=-1)
                    698:   {
                    699:     result = TtaStrdup(itemlist[ReturnOption].label);
                    700:   }
                    701:   
                    702:   TtaFreeMemory (itemlist);
                    703:   return result;
                    704: }
1.92      kia       705: #endif /* TEMPLATES */
1.90      kia       706: 
1.56      francesc  707: /*----------------------------------------------------------------------
1.79      quint     708:   RepeatButtonClicked
1.89      kia       709:   Called when a repeat button is clicked.
                    710:   Can be called for useEl, useSimple or repeat.
                    711:   If called for useEl or useSimple, the new element must be added after.
                    712:   If called for repeat, the element must be added before all.
                    713:    
1.56      francesc  714:   Shows a menu with all the types that can be used in a use element.
                    715:   ----------------------------------------------------------------------*/
1.79      quint     716: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  717: {
                    718: #ifdef TEMPLATES
1.89      kia       719:   Document        doc = event->document;
                    720:   Element         el = event->element;
                    721:   ElementType     elType;
1.90      kia       722:   XTigerTemplate  t;
                    723:   Declaration     decl;
                    724:   Element         repeatEl = el;
                    725:   Element         firstEl;
                    726:   Element         newEl = NULL;
1.89      kia       727:   char*           types;
1.90      kia       728:   ThotBool        oldStructureChecking;
1.95      kia       729:   View            view;
1.89      kia       730:   
1.95      kia       731:   TtaGetActiveView (&doc, &view);
                    732:   if (view != 1)
                    733:     return FALSE; /* let Thot perform normal operation */
                    734: 
1.89      kia       735:   TtaCancelSelection(doc);
1.90      kia       736:   
                    737:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.89      kia       738:   elType = TtaGetElementType(el);
                    739:   while(elType.ElTypeNum!=Template_EL_repeat)
                    740:   {
1.90      kia       741:     repeatEl = TtaGetParent(repeatEl);
                    742:     if(repeatEl==NULL)
1.89      kia       743:       break;
1.90      kia       744:     elType = TtaGetElementType(repeatEl);
1.89      kia       745:   }
1.90      kia       746:   if(repeatEl)
1.89      kia       747:   {
1.98    ! kia       748:     if(Template_CanInsertRepeatChild(repeatEl))
1.90      kia       749:     {
1.98    ! kia       750:       firstEl = TtaGetFirstChild(repeatEl);
        !           751:       types = GetAttributeStringValue(firstEl, Template_ATTR_types, NULL);
        !           752:       
        !           753:       char* listtypes = Template_ExpandTypes(t, types);
        !           754:       char* result = QueryStringFromMenu(doc, listtypes);
        !           755:       if(result)
1.90      kia       756:       {
1.98    ! kia       757:         decl = GetDeclaration(t, result);
        !           758:         if(decl)
        !           759:         {
        !           760:           oldStructureChecking = TtaGetStructureChecking (doc);
        !           761:           TtaSetStructureChecking (FALSE, doc);
        !           762:           
        !           763:           if(el==repeatEl)
        !           764:             newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, NULL);
        !           765:           else
        !           766:             newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, el);
        !           767:           
        !           768:           TtaSetStructureChecking (oldStructureChecking, doc);
        !           769:           
        !           770:           firstEl = GetFirstEditableElement(newEl);
        !           771:           if(firstEl)
        !           772:           {
        !           773:             TtaSelectElement (doc, firstEl);
        !           774:             TtaSetStatusSelectedElement(doc, view, firstEl);
        !           775:           }
        !           776:           else
        !           777:           {
        !           778:             TtaSelectElement (doc, newEl);
        !           779:             TtaSetStatusSelectedElement(doc, view, newEl);
        !           780:           }
        !           781:         }
1.90      kia       782:       }
1.98    ! kia       783:       TtaFreeMemory(listtypes);
        !           784:       TtaFreeMemory(result);
        !           785:     }
        !           786:     else /* if(Template_CanInsertRepeatChild(repeatEl)) */
        !           787:     {
        !           788:       TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.90      kia       789:     }
1.89      kia       790:   }
1.77      vatton    791:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc  792: #endif /* TEMPLATES */
1.94      kia       793:   return TRUE;
                    794: }
                    795: 
                    796: /*----------------------------------------------------------------------
                    797:   UseButtonClicked
                    798:   Shows a menu with all the types that can be used in a use element.
                    799:   ----------------------------------------------------------------------*/
                    800: ThotBool UseButtonClicked (NotifyElement *event)
                    801: {
                    802: #ifdef TEMPLATES
                    803:   Document        doc = event->document;
                    804:   Element         el = event->element;
                    805:   ElementType     elType;
                    806:   XTigerTemplate  t;
                    807:   Declaration     decl;
                    808:   Element         firstEl;
                    809:   Element         newEl = NULL;
                    810:   char*           types;
                    811:   ThotBool        oldStructureChecking;
1.95      kia       812:   View            view;
                    813: 
                    814:   TtaGetActiveView (&doc, &view);
                    815:   if (view != 1)
                    816:     return FALSE; /* let Thot perform normal operation */
1.94      kia       817:   
                    818:   TtaCancelSelection(doc);
                    819:   
                    820:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
                    821:   if (!t)
                    822:     return FALSE; /* let Thot perform normal operation */
                    823:   elType = TtaGetElementType(el);
                    824: 
                    825:   firstEl = TtaGetFirstChild(el);
                    826:   if(firstEl)
                    827:   {
                    828:     RepeatButtonClicked(event);
                    829:   }
                    830:   else
                    831:   {
1.95      kia       832:     types = GetAttributeStringValue(el, Template_ATTR_types, NULL);
1.94      kia       833: 
                    834:     char* listtypes = Template_ExpandTypes(t, types);
                    835:     char* result = QueryStringFromMenu(doc, listtypes);
                    836:     if(result)
                    837:     {
                    838:       decl = GetDeclaration(t, result);
                    839:       if(decl)
                    840:       {
                    841:         oldStructureChecking = TtaGetStructureChecking (doc);
                    842:         TtaSetStructureChecking (FALSE, doc);
                    843:         
                    844:         newEl = Template_InsertUseChildren(doc, el, decl);
                    845:         
                    846:         TtaSetStructureChecking (oldStructureChecking, doc);
1.97      kia       847:         
                    848:         firstEl = GetFirstEditableElement(newEl);
                    849:         if(firstEl)
1.98    ! kia       850:         {
1.97      kia       851:           TtaSelectElement (doc, firstEl);
1.98    ! kia       852:           TtaSetStatusSelectedElement(doc, view, firstEl);
        !           853:         }
1.97      kia       854:         else
1.98    ! kia       855:         {
1.97      kia       856:           TtaSelectElement (doc, newEl);
1.98    ! kia       857:           TtaSetStatusSelectedElement(doc, view, newEl);
        !           858:         }
1.94      kia       859:       }
                    860:     }
                    861:     TtaFreeMemory(listtypes);
                    862:     TtaFreeMemory(result);
                    863:   }
                    864:   
                    865:   return TRUE;
                    866: #endif /* TEMPLATES */
1.56      francesc  867:        return TRUE;
                    868: }
1.64      francesc  869: 
1.89      kia       870: 
                    871: 
1.94      kia       872: 
                    873: 
                    874: /*----------------------------------------------------------------------
                    875:   OptionButtonClicked
                    876:   ----------------------------------------------------------------------*/
                    877: ThotBool OptionButtonClicked (NotifyElement *event)
                    878: {
                    879: #ifdef TEMPLATES
                    880:   Element         child, grandChild, next;
                    881:   ElementType     elType, elType1;
                    882:   Document        doc;
                    883:   XTigerTemplate  t;
                    884:   View            view;
                    885: 
                    886:   TtaGetActiveView (&doc, &view);
                    887:   if (view != 1)
                    888:     return FALSE; /* let Thot perform normal operation */
                    889:   doc = event->document;
                    890:   child = TtaGetFirstChild (event->element);
                    891:   if (!child)
                    892:     return FALSE; /* let Thot perform normal operation */
                    893:   elType = TtaGetElementType (child);
                    894:   elType1 = TtaGetElementType (event->element);
                    895:   if ((elType.ElTypeNum != Template_EL_useEl &&
                    896:        elType.ElTypeNum != Template_EL_useSimple) ||
                    897:       elType.ElSSchema != elType1.ElSSchema)
                    898:     return FALSE;
                    899: 
                    900:   TtaCancelSelection (doc);
                    901:   grandChild = TtaGetFirstChild (child);
                    902:   if (!grandChild)
                    903:     /* the "use" element is empty. Instantiate it */
                    904:     {
                    905:       t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
                    906:       if (!t)
                    907:         return FALSE; // no template ?!?!
                    908:       InstantiateUse (t, child, doc, TRUE);
                    909:     }
                    910:   else
                    911:     /* remove the content of the "use" element */
                    912:     {
                    913:       do
                    914:         {
                    915:           next = grandChild;
                    916:           TtaNextSibling (&next);
                    917:           TtaDeleteTree (grandChild, doc);
                    918:           grandChild = next;
                    919:         }
                    920:       while (next);
                    921:     }
                    922:   TtaSelectElement (doc, event->element);
                    923:   return TRUE; /* don't let Thot perform normal operation */
                    924: #endif /* TEMPLATES */
                    925:   return TRUE;
                    926: }
                    927: 
                    928: 
                    929: 
1.66      vatton    930: /*----------------------------------------------------------------------
                    931:   ----------------------------------------------------------------------*/
1.76      vatton    932: void OpeningInstance (char *fileName, Document doc)
1.65      francesc  933: {
                    934: #ifdef TEMPLATES
1.76      vatton    935:   XTigerTemplate   t;
                    936:   char            *content, *ptr;
                    937:   gzFile           stream;
                    938:   char             buffer[2000];
1.77      vatton    939:   int              res;
1.65      francesc  940: 
1.76      vatton    941:   stream = TtaGZOpen (fileName);
                    942:   if (stream != 0)
1.65      francesc  943:     {
1.76      vatton    944:       res = gzread (stream, buffer, 1999);
                    945:       if (res >= 0)
1.65      francesc  946:         {
1.81      vatton    947:           buffer[res] = EOS;
1.76      vatton    948:           ptr = strstr (buffer, "<?xtiger");
                    949:           if (ptr)
                    950:             ptr = strstr (ptr, "template");
                    951:           if (ptr)
                    952:             ptr = strstr (ptr, "=");
                    953:           if (ptr)
                    954:             ptr = strstr (ptr, "\"");
                    955:           if (ptr)
                    956:             {
                    957:               // template URI
                    958:               content = &ptr[1];
                    959:               ptr = strstr (content, "\"");
                    960:             }
                    961:           if (ptr)
                    962:             {
                    963:               *ptr = EOS;
                    964:               //Get now the template URI
                    965:               DocumentMeta[doc]->template_url = TtaStrdup (content);
                    966:               if (Templates_Dic == NULL)
                    967:                 InitializeTemplateEnvironment ();
1.87      kia       968:               t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
1.76      vatton    969:               if (!t)
                    970:                 {
                    971:                   LoadTemplate (0, content);
1.87      kia       972:                   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
1.76      vatton    973:                 }
                    974:               AddUser (t);
                    975:             }
1.65      francesc  976:         }
                    977:     }
1.76      vatton    978:   TtaGZClose (stream);
1.65      francesc  979: #endif /* TEMPLATES */
                    980: }
                    981: 
1.64      francesc  982: /*----------------------------------------------------------------------
1.65      francesc  983:   ClosingInstance
1.64      francesc  984:   Callback called before closing a document. Checks for unused templates.
                    985:   ----------------------------------------------------------------------*/
1.65      francesc  986: ThotBool ClosingInstance(NotifyDialog* dialog)
1.64      francesc  987: {
1.65      francesc  988: #ifdef TEMPLATES
                    989:   //If it is a template all has been already freed
1.76      vatton    990:   if (DocumentMeta[dialog->document] == NULL)
                    991:     return FALSE;
1.65      francesc  992: 
                    993:   char *turl = DocumentMeta[dialog->document]->template_url;
1.73      vatton    994:   if (turl)
1.65      francesc  995:     {
1.87      kia       996:       XTigerTemplate t = (XTigerTemplate) Dictionary_Get (Templates_Dic, turl);
1.73      vatton    997:       if (t)
1.76      vatton    998:         RemoveUser (t);
                    999:       TtaFreeMemory (turl);
1.65      francesc 1000:     }
                   1001: #endif /* TEMPLATES */
                   1002:   return FALSE;
1.64      francesc 1003: }
1.87      kia      1004: 
                   1005: 
                   1006: /*----------------------------------------------------------------------
                   1007:   GetFirstTemplateParentElement
                   1008:   Return the first element wich has "Template" as SShema name or null if none.
                   1009:   ----------------------------------------------------------------------*/
                   1010: ThotBool IsTemplateElement(Element elem)
                   1011: {
                   1012: #ifdef TEMPLATES
                   1013:   return strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
                   1014:                                                     , TEMPLATE_SSHEMA_NAME)==0;
                   1015: #else
                   1016:   return FALSE;
                   1017: #endif /* TEMPLATES */
                   1018: }
                   1019: 
                   1020: 
                   1021: /*----------------------------------------------------------------------
                   1022:   GetFirstTemplateParentElement
                   1023:   Return the first element wich has "Template" as SShema name or null if none.
                   1024:   ----------------------------------------------------------------------*/
                   1025: Element GetFirstTemplateParentElement(Element elem)
                   1026: {
                   1027: #ifdef TEMPLATES
                   1028:   elem = TtaGetParent(elem);
                   1029:   while(elem!=NULL && strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
                   1030:                                                     , TEMPLATE_SSHEMA_NAME)!=0)
                   1031:   {
                   1032:     elem = TtaGetParent(elem);
                   1033:   }
                   1034:   return elem;
                   1035: #else
                   1036:   return NULL;
                   1037: #endif /* TEMPLATES */
                   1038: }

Webmaster