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

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

Webmaster