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

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

Webmaster