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

1.1       cvs         1: /*
                      2:  *
1.162     vatton      3:  *  COPYRIGHT INRIA and W3C, 1996-2008
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.171     kia        10:  *          Ă‰milien Kia
1.1       cvs        11:  *
                     12:  */
                     13: 
1.194     kia        14: 
1.1       cvs        15: #define THOT_EXPORT extern
                     16: #include "amaya.h"
                     17: #include "document.h"
1.99      kia        18: #include "undo.h"
1.90      kia        19: #include "containers.h"
                     20: #include "Elemlist.h"
1.92      kia        21: #include "templates.h"
                     22: 
1.46      vatton     23: #ifdef TEMPLATES
                     24: #include "Template.h"
1.52      vatton     25: #include "templateDeclarations.h"
1.140     vatton     26: 
                     27: #include "html2thot_f.h"
1.174     kia        28: #include "HTMLedit_f.h"
1.122     kia        29: #include "templates_f.h"
1.89      kia        30: #include "templateUtils_f.h"
1.67      quint      31: #include "templateLoad_f.h"
                     32: #include "templateDeclarations_f.h"
1.76      vatton     33: #include "templateInstantiate_f.h"
1.145     kia        34: #include "Templatebuilder_f.h"
1.28      tollenae   35: #include "appdialogue_wx.h"
1.29      tollenae   36: #include "init_f.h"
1.46      vatton     37: #include "wxdialogapi_f.h"
1.60      francesc   38: #include "AHTURLTools_f.h"
1.52      vatton     39: #endif /* TEMPLATES */
1.1       cvs        40: 
1.87      kia        41: #include "fetchXMLname_f.h"
1.83      kia        42: #include "MENUconf.h"
                     43: 
                     44: /* Paths from which looking for templates.*/
                     45: static Prop_Templates_Path *TemplateRepositoryPaths;
                     46: 
1.87      kia        47: /*----------------------------------------------------------------------
                     48:   IsTemplateInstanceDocument: Test if a document is a template instance
                     49:   doc : Document to test
                     50:   return : TRUE if the document is a template instance
                     51:   ----------------------------------------------------------------------*/
1.106     vatton     52: ThotBool IsTemplateInstanceDocument(Document doc)
                     53: {
1.87      kia        54: #ifdef TEMPLATES
1.171     kia        55:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                     56:   if(t)
1.176     vatton     57:     return (t->state & templInstance) != 0;
1.171     kia        58:   else
                     59:     return FALSE;
                     60: #else /* TEMPLATES */
1.88      cvs        61:   return FALSE;
1.171     kia        62: #endif /* TEMPLATES */ 
1.87      kia        63: }
                     64: 
1.83      kia        65: /*----------------------------------------------------------------------
1.109     kia        66:   IsTemplateDocument: Test if a document is a template (not an instance)
                     67:   doc : Document to test
1.167     kia        68:   return : TRUE if the document is a template
1.109     kia        69:   ----------------------------------------------------------------------*/
1.183     vatton     70: ThotBool IsTemplateDocument (Document doc)
1.109     kia        71: {
                     72: #ifdef TEMPLATES
1.171     kia        73:   XTigerTemplate t = GetXTigerDocTemplate(doc);
1.183     vatton     74:   if (t)
                     75:     return ((t->state & templTemplate) != 0);
1.171     kia        76:   else
                     77:     return FALSE;
1.183     vatton     78: #endif /* TEMPLATES */ 
1.171     kia        79:   return FALSE;
                     80: }
                     81: 
                     82: 
                     83: /*----------------------------------------------------------------------
                     84:   Test if a document is an internal template.
                     85:   (no instance is opened and it is not edited)
                     86:   ----------------------------------------------------------------------*/
                     87: ThotBool IsInternalTemplateDocument(Document doc)
                     88: {
                     89: #ifdef TEMPLATES
                     90:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                     91:   if(t)
                     92:     return (t->state&templInternal)!=0;
                     93:   else
                     94:     return FALSE;
                     95: #else /* TEMPLATES */
1.109     kia        96:   return FALSE;
1.171     kia        97: #endif /* TEMPLATES */ 
                     98: }
                     99: 
                    100: /*----------------------------------------------------------------------
                    101:   Return the URL of an instance template.
                    102:   ----------------------------------------------------------------------*/
                    103: char* GetDocumentInstanceTemplateUrl(Document doc)
                    104: {
                    105: #ifdef TEMPLATES
                    106:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                    107:   if(t)
                    108:     return t->base_uri;
                    109:   else
                    110:     return FALSE;
                    111: #else /* TEMPLATES */
                    112:   return NULL;
                    113: #endif /* TEMPLATES */ 
1.109     kia       114: }
                    115: 
1.146     vatton    116: /*----------------------------------------------------------------------
                    117:   CheckPromptIndicator checks if the element is a prompt text unit
                    118:   ----------------------------------------------------------------------*/
                    119: ThotBool CheckPromptIndicator (Element el, Document doc)
                    120: {
                    121: #ifdef TEMPLATES
                    122:   ElementType     elType;
                    123:   Element         parent;
                    124:   AttributeType   attrType;
                    125:   Attribute       att;
                    126:   SSchema         templateSSchema;
                    127: 
1.173     vatton    128:   if (!IsTemplateInstanceDocument(doc))
                    129:     /* let Thot perform normal operation */
                    130:     return FALSE;
1.146     vatton    131:   elType = TtaGetElementType (el);
                    132:   templateSSchema = TtaGetSSchema ("Template", doc);
                    133:        if (elType.ElTypeNum == HTML_EL_TEXT_UNIT)
                    134:     {
                    135:       parent = TtaGetParent (el);
                    136:       elType = TtaGetElementType (parent);
                    137:       while (parent && elType.ElSSchema != templateSSchema)
                    138:         {
                    139:           parent = TtaGetParent (parent);
                    140:           elType = TtaGetElementType (parent);
                    141:         }
                    142:       if (parent &&
                    143:           (elType.ElTypeNum == Template_EL_useEl ||
                    144:            elType.ElTypeNum == Template_EL_useSimple))
                    145:         {
                    146:           // there is a parent template use
                    147:           attrType.AttrSSchema = elType.ElSSchema;
                    148:           attrType.AttrTypeNum = Template_ATTR_prompt;
                    149:           att = TtaGetAttribute (parent, attrType);
                    150:           if (att)
1.151     quint     151:             {
                    152:               TtaSelectElement (doc, el);
                    153:               return TRUE;
                    154:             }
1.146     vatton    155:         }
                    156:     }
                    157: #endif /* TEMPLATES */
1.173     vatton    158:   /* let Thot perform normal operation */
1.146     vatton    159:   return FALSE;
                    160: }
                    161: 
                    162: /*----------------------------------------------------------------------
                    163:   RemovePromptIndicator removes the enclosing prompt indicator
                    164:   ----------------------------------------------------------------------*/
1.153     vatton    165: ThotBool RemovePromptIndicator (NotifyOnTarget *event)
1.146     vatton    166: {
                    167: #ifdef TEMPLATES
                    168:   ElementType     elType;
1.153     vatton    169:   Element         parent, el;
1.146     vatton    170:   AttributeType   attrType;
                    171:   Attribute       att;
1.153     vatton    172:   Document        doc;
1.146     vatton    173:   SSchema         templateSSchema;
                    174: 
1.153     vatton    175:   el = event->element;
                    176:   doc = event->document;
1.146     vatton    177:   elType = TtaGetElementType (el);
                    178:   templateSSchema = TtaGetSSchema ("Template", doc);
1.153     vatton    179:   parent = TtaGetParent (el);
                    180:   elType = TtaGetElementType (parent);
                    181:   while (parent && elType.ElSSchema != templateSSchema)
1.146     vatton    182:     {
1.153     vatton    183:       parent = TtaGetParent (parent);
1.146     vatton    184:       elType = TtaGetElementType (parent);
1.153     vatton    185:     }
                    186:   if (parent &&
                    187:       (elType.ElTypeNum == Template_EL_useEl ||
                    188:        elType.ElTypeNum == Template_EL_useSimple))
                    189:     {
                    190:       // there is a parent template use
                    191:       attrType.AttrSSchema = elType.ElSSchema;
                    192:       attrType.AttrTypeNum = Template_ATTR_prompt;
                    193:       att = TtaGetAttribute (parent, attrType);
                    194:       if (att)
1.146     vatton    195:         {
1.153     vatton    196:           TtaRegisterAttributeDelete (att, parent, doc);
                    197:           TtaRemoveAttribute (parent, att, doc);
1.146     vatton    198:         }
                    199:     }
                    200: #endif /* TEMPLATES */
1.153     vatton    201:   return FALSE;                /* let Thot perform normal operation */
1.146     vatton    202: }
                    203: 
1.109     kia       204: 
                    205: /*----------------------------------------------------------------------
1.108     vatton    206:   AllocTemplateRepositoryListElement: allocates an element for the list
                    207:   of template repositories.
1.83      kia       208:   path : path of the new element
                    209:   return : address of the new element
                    210:   ----------------------------------------------------------------------*/
                    211: void* AllocTemplateRepositoryListElement (const char* path, void* prevElement)
                    212: {
1.129     vatton    213:   Prop_Templates_Path *element;
                    214: 
                    215:   element  = (Prop_Templates_Path*)TtaGetMemory (sizeof(Prop_Templates_Path));
                    216:   memset (element, 0, sizeof(Prop_Templates_Path));
                    217:   strncpy (element->Path, path, MAX_LENGTH - 1);
1.83      kia       218:   if (prevElement)
1.129     vatton    219:     {
                    220:       element->NextPath = ((Prop_Templates_Path*)prevElement)->NextPath;
                    221:       ((Prop_Templates_Path*)prevElement)->NextPath = element;
                    222:     }
1.83      kia       223:   return element;
                    224: }
                    225: 
                    226: 
                    227: /*----------------------------------------------------------------------
                    228:   FreeTemplateRepositoryList: Free the list of template repositories.
                    229:   list : address of the list (address of the first element).
                    230:   ----------------------------------------------------------------------*/
                    231: void FreeTemplateRepositoryList (void* list)
                    232: {
1.131     vatton    233:   Prop_Templates_Path **l = (Prop_Templates_Path**) list;
                    234:   Prop_Templates_Path  *element = *l;
                    235: 
1.83      kia       236:   l = NULL;
                    237:   while (element)
                    238:   {
                    239:     Prop_Templates_Path* next = element->NextPath;
1.131     vatton    240:     TtaFreeMemory (element);
1.83      kia       241:     element = next;
                    242:   }
                    243: }
                    244: 
                    245: /*----------------------------------------------------------------------
                    246:   CopyTemplateRepositoryList: Copy a list of template repositories.
                    247:   src : address of the list (address of the first element).
                    248:   dst : address where copy the list
                    249:   ----------------------------------------------------------------------*/
1.91      vatton    250: static void CopyTemplateRepositoryList (const Prop_Templates_Path** src,
                    251:                                         Prop_Templates_Path** dst)
1.83      kia       252: {
                    253:   Prop_Templates_Path *element=NULL, *current=NULL;
                    254:   
1.131     vatton    255:   if (*src)
1.83      kia       256:   {
                    257:     *dst = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    258:     (*dst)->NextPath = NULL;
                    259:     strcpy((*dst)->Path, (*src)->Path);
                    260:     
                    261:     element = (*src)->NextPath;
                    262:     current = *dst;
                    263:   }
                    264: 
1.106     vatton    265:   while (element)
                    266:     {
1.83      kia       267:     current->NextPath = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    268:     current = current->NextPath; 
                    269:     current->NextPath = NULL;
                    270:     strcpy(current->Path, element->Path);
                    271:     element = element->NextPath;
1.106     vatton    272:     }
1.83      kia       273: }
                    274: 
                    275: /*----------------------------------------------------------------------
                    276:   LoadTemplateRepositoryList: Load the list of template repositories.
                    277:   list   : address of the list (address of the first element).
                    278:   return : the number of readed repository paths.
                    279:   ----------------------------------------------------------------------*/
                    280: static int LoadTemplateRepositoryList (Prop_Templates_Path** list)
                    281: {
                    282:   Prop_Templates_Path *element, *current = NULL;
                    283:   char *path, *homePath;
                    284:   unsigned char *c;
                    285:   int nb = 0;
                    286:   FILE *file;
                    287:   
1.131     vatton    288:   //clean up the curent list
                    289:   FreeTemplateRepositoryList (list);
                    290: 
                    291:   // open the file
1.83      kia       292:   path = (char *) TtaGetMemory (MAX_LENGTH);
1.86      vatton    293:   homePath       = TtaGetEnvString ("APP_HOME");
1.106     vatton    294:   sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83      kia       295:   file = TtaReadOpen ((char *)path);
1.84      kia       296:   if (!file)
                    297:   {
                    298:     /* The config file dont exist, create it. */
                    299:     file = TtaWriteOpen ((char *)path);
1.122     kia       300:     fprintf (file, "http://www.w3.org/Amaya/Templates/cv.xtd\n");
1.144     vatton    301:     fprintf (file, "http://www.w3.org/Amaya/Templates/slides.xtd\n");
                    302:     fprintf (file, "http://www.w3.org/Amaya/Templates/ACM-Proc-Article.xtd\n");
1.84      kia       303:     TtaWriteClose (file);
                    304:     /* Retry to open it.*/
                    305:     file = TtaReadOpen ((char *)path);
                    306:   }
                    307:   
1.83      kia       308:   if (file)
1.129     vatton    309:     {
1.131     vatton    310:       // read the file
1.129     vatton    311:       c = (unsigned char*)path;
                    312:       *c = EOS;
                    313:       while (TtaReadByte (file, c))
                    314:         {
                    315:           if (*c == 13 || *c == EOL)
                    316:             *c = EOS;
                    317:           if (*c == EOS && c != (unsigned char*)path )
                    318:             {
                    319:               element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    320:               element->NextPath = NULL;
                    321:               strcpy (element->Path, path);
1.148     kia       322: 
1.129     vatton    323:               if (*list == NULL)
                    324:                 *list = element; 
                    325:               else
                    326:                 current->NextPath = element;
                    327:               current = element;
                    328:               nb++;
1.83      kia       329: 
1.129     vatton    330:               c = (unsigned char*) path;
                    331:               *c = EOS;
                    332:             }
                    333:           else
                    334:             c++;
                    335:         }
                    336:       if (c != (unsigned char*)path && *path != EOS)
                    337:         {
                    338:           element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    339:           *(c+1) = EOS;
                    340:           strcpy (element->Path, path);
                    341:           element->NextPath = NULL;
1.148     kia       342: 
1.129     vatton    343:           if (*list == NULL)
                    344:             *list = element; 
                    345:           else
                    346:             current->NextPath = element;
                    347:           nb++;
                    348:         }
                    349:       TtaReadClose (file);
1.83      kia       350:     }
                    351:   TtaFreeMemory(path);
                    352:   return nb;
                    353: }
                    354: 
                    355: /*----------------------------------------------------------------------
                    356:   SaveTemplateRepositoryList: Save the list of template repositories.
                    357:   list   : address of the list (address of the first element).
                    358:   ----------------------------------------------------------------------*/
                    359: static void SaveTemplateRepositoryList (const Prop_Templates_Path** list)
                    360: {
                    361:   const Prop_Templates_Path *element;
                    362:   char *path, *homePath;
                    363:   unsigned char *c;
                    364:   FILE *file;
                    365: 
                    366:   path = (char *) TtaGetMemory (MAX_LENGTH);
                    367:   homePath       = TtaGetEnvString ("APP_HOME");
1.106     vatton    368:   sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83      kia       369: 
                    370:   file = TtaWriteOpen ((char *)path);
                    371:   c = (unsigned char*)path;
                    372:   *c = EOS;
                    373:   if (file)
                    374:   {
                    375:     element = *list;
                    376:     while (element)
                    377:     {
                    378:       fprintf(file, "%s\n", element->Path);
                    379:       element = element->NextPath;
                    380:     }
                    381:     TtaWriteClose (file);
                    382:   }
                    383: }
                    384: 
                    385: /*----------------------------------------------------------------------
                    386:   GetTemplateRepositoryList: Get the list of template repositories from template environment.
                    387:   list : address of the list (address of the first element).
                    388:   ----------------------------------------------------------------------*/
                    389: void GetTemplateRepositoryList (void* list)
                    390: {
                    391:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                    392:   CopyTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths, l);
                    393: }
                    394: 
                    395: /*----------------------------------------------------------------------
                    396:   SetTemplateRepositoryList: Set the list of template repositories environment.
                    397:   list : address of the list (address of the first element).
                    398:   ----------------------------------------------------------------------*/
                    399: void SetTemplateRepositoryList (const void* list)
                    400: {
                    401:   const Prop_Templates_Path** l = (const Prop_Templates_Path**) list;
                    402:   CopyTemplateRepositoryList((const Prop_Templates_Path**)l, &TemplateRepositoryPaths);
                    403:   SaveTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths);
                    404: }
                    405: 
                    406: /*-----------------------------------------------------------------------
                    407:    InitTemplates
                    408:    Initializes the annotation library
                    409:   -----------------------------------------------------------------------*/
                    410: void InitTemplates ()
                    411: {
                    412:   TtaSetEnvBoolean ("SHOW_TEMPLATES", TRUE, FALSE);
1.131     vatton    413:   LoadTemplateRepositoryList (&TemplateRepositoryPaths);
1.83      kia       414: }
                    415: 
                    416: 
1.1       cvs       417: /*----------------------------------------------------------------------
1.108     vatton    418:   Load a template and create the instance file - update images and 
                    419:   stylesheets related to the template.
                    420:   ----------------------------------------------------------------------*/
                    421: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
                    422: {
                    423: #ifdef TEMPLATES
1.170     vatton    424:   DocumentType docType;
                    425:   int          len, i;
                    426:   char        *s;
                    427:   char              suffix[6];
                    428:   ThotBool     dontReplace = DontReplaceOldDoc;
1.108     vatton    429: 
                    430:   if (!IsW3Path (docname) && TtaFileExist (docname))
                    431:     {
                    432:       s = (char *)TtaGetMemory (strlen (docname) +
                    433:                                 strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
                    434:       sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
                    435:       InitConfirm (0, 0, s);
                    436:       TtaFreeMemory (s);
                    437:       if (!UserAnswer)
                    438:         return;
                    439:     }
1.170     vatton    440:   docType = LoadTemplate (0, templatename);
                    441:   if (docType != docFree)
1.165     kia       442:     {
1.170     vatton    443:       /* check if the file suffix is conform to the document type */
                    444:       s = (char *)TtaGetMemory (strlen (docname) + 10);
                    445:       strcpy (s, docname);
                    446:       if (!IsXMLName (docname))
                    447:         {
                    448:           // by default no suffix is added
                    449:           suffix[0] = EOS;
                    450:           if (IsMathMLName (docname) && docType != docMath)
                    451:             strcpy (suffix, "mml");
                    452:           else if (IsSVGName (docname) && docType != docSVG)
                    453:             strcpy (suffix, "svg");
                    454:           else if (IsHTMLName (docname) && docType != docHTML)
                    455:             strcpy (suffix, "xml");
                    456:           if (suffix[0] != EOS)
                    457:             {
                    458:               // change or update the suffix
                    459:               len = strlen (s);
                    460:               for (i = len-1; i > 0 && s[i] != '.'; i--);
                    461:               if (s[i] != '.')
                    462:                 {
                    463:                   /* there is no suffix */
                    464:                   s[i++] = '.';
                    465:                   strcpy (&s[i], suffix);
                    466:                 }
                    467:               else
                    468:                 {
                    469:                   /* there is a suffix */
                    470:                   i++;
                    471:                   strcpy (&s[i], suffix);
                    472:                 }
                    473:             }
                    474:         }
                    475:       // now create the instance
1.165     kia       476:       DontReplaceOldDoc = dontReplace;
1.177     vatton    477:       CreateInstance (templatename, s, docname, docType, doc);
1.170     vatton    478:       TtaFreeMemory (s);
1.165     kia       479:     }
1.108     vatton    480: #endif /* TEMPLATES */
                    481: }
                    482: 
1.53      vatton    483: 
1.109     kia       484: 
1.52      vatton    485: /*----------------------------------------------------------------------
1.107     kia       486:   PreventReloadingTemplate
                    487:   Prevent reloading a template.
                    488:   You must call AllowReloadingTemplate when finish.
                    489:   Usefull for reload an instance without reloading the template.
                    490:   ----------------------------------------------------------------------*/
                    491: void PreventReloadingTemplate(char* template_url)
                    492: {
                    493: #ifdef TEMPLATES
1.178     kia       494:   Template_AddReference(GetXTigerTemplate (template_url));
1.107     kia       495: #endif /* TEMPLATES */
                    496: }
                    497: 
                    498: /*----------------------------------------------------------------------
                    499:   AllowReloadingTemplate
                    500:   Allow reloading a template.
                    501:   You must call it after each PreventReloadingTemplate call.
                    502:   ----------------------------------------------------------------------*/
                    503: void AllowReloadingTemplate(char* template_url)
                    504: {
                    505: #ifdef TEMPLATES
1.178     kia       506:   Template_RemoveReference(GetXTigerTemplate (template_url));
1.107     kia       507: #endif /* TEMPLATES */  
                    508: }
                    509: 
                    510: 
1.137     vatton    511: /*----------------------------------------------------------------------
                    512:   ----------------------------------------------------------------------*/
1.134     kia       513: ThotBool isEOSorWhiteSpace (const char c)
                    514: {
1.137     vatton    515:   return c == SPACE || c == TAB || c ==  EOL || c ==__CR__ || c == EOS;
                    516: }
                    517: ThotBool isWhiteSpace (const char c)
                    518: {
                    519:   return c == SPACE || c == TAB || c == EOL || c ==__CR__;
1.134     kia       520: }
                    521: 
1.107     kia       522: /*----------------------------------------------------------------------
1.87      kia       523:   giveItems : Lists type items from string
                    524:   example : "one two three" is extracted to {one, two, three}
                    525:   note : item type are setted to SimpleTypeNat
                    526:   text : text from which list items
                    527:   size : size of text in characters
                    528:   items : address of exctracted item list
                    529:   nbitems : items number in items list
1.52      vatton    530:   ----------------------------------------------------------------------*/
1.76      vatton    531: void giveItems (char *text, int size, struct menuType **items, int *nbitems)
1.1       cvs       532: {
1.70      quint     533: #ifdef TEMPLATES
1.148     kia       534:   ThotBool         inElement = TRUE;
1.52      vatton    535:   struct menuType *menu;
                    536:   char            *iter;
1.148     kia       537:   char             temp[128];
1.52      vatton    538:   int              i;
1.148     kia       539:   int              labelSize;
1.28      tollenae  540: 
1.148     kia       541:   *nbitems = 1;
                    542:   for (i = 0; i < size; i++)
1.52      vatton    543:     {
                    544:       if (isEOSorWhiteSpace (text[i]))
                    545:         {
                    546:           if (inElement)
                    547:             inElement = FALSE;
                    548:         }
                    549:       else if (!inElement)
                    550:         {
                    551:           inElement = TRUE;
                    552:           (*nbitems)++;
                    553:         }
                    554:     }
1.51      francesc  555: 
1.148     kia       556:   menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
                    557:   iter = text;
                    558:   for (i = 0; i < *nbitems; i++)
                    559:     {
1.52      vatton    560:       labelSize = 0;
1.137     vatton    561:       while (isWhiteSpace (*iter))
1.52      vatton    562:         iter++;
1.137     vatton    563:       if (*iter != EOS)
                    564:         {
                    565:           while (!isEOSorWhiteSpace (*iter))
                    566:             {
                    567:               temp[labelSize++] = *iter;
                    568:               iter++;
                    569:             }
1.52      vatton    570: 
1.137     vatton    571:           temp[labelSize] = EOS;
                    572:           menu[i].label = (char *) TtaStrdup (temp);
                    573:           menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
1.52      vatton    574:         }
                    575:     }
1.137     vatton    576:   *items = menu;
1.70      quint     577: #endif /* TEMPLATES */
1.28      tollenae  578: }
1.37      tollenae  579: 
1.70      quint     580: #ifdef TEMPLATES
1.52      vatton    581: /*----------------------------------------------------------------------
                    582:   ----------------------------------------------------------------------*/
                    583: static char *createMenuString (const struct menuType* items, const int nbItems)
                    584: {
                    585:   char *result, *iter;
1.148     kia       586:   int   size = 0;
1.52      vatton    587:   int   i;
                    588: 
1.148     kia       589:   for (i=0; i < nbItems; i++)
                    590:     size += 2 + strlen (items[i].label);
1.52      vatton    591: 
1.148     kia       592:   result = (char *) TtaGetMemory (size);
                    593:   iter = result;
                    594:   for (i=0; i < nbItems; i++)
1.52      vatton    595:     {
                    596:       *iter = 'B';
                    597:       ++iter;
1.148     kia       598: 
1.52      vatton    599:       strcpy (iter, items[i].label);
                    600:       iter += strlen (items[i].label)+1;
                    601:     }
1.148     kia       602:   return result;
1.36      tollenae  603: }
1.71      quint     604: #endif /* TEMPLATES */
1.29      tollenae  605: 
1.185     kia       606: 
                    607: /*----------------------------------------------------------------------
1.71      quint     608:   UseToBeCreated
                    609:   An new use element will be created by the user through some generic editing
                    610:   command
                    611:   -----------------------------------------------------------------------*/
                    612: ThotBool UseToBeCreated (NotifyElement *event)
                    613: {
                    614: #ifdef TEMPLATES
1.122     kia       615:   ElementType   parentType;
1.138     vatton    616:   SSchema       templateSSchema = TtaGetSSchema ("Template", event->document);  
1.130     vatton    617:   if (templateSSchema)
1.122     kia       618:   {
1.130     vatton    619:     parentType = TtaGetElementType (event->element);
                    620:     if (parentType.ElSSchema == templateSSchema &&
                    621:         parentType.ElTypeNum == Template_EL_repeat)
                    622:       return !Template_CanInsertRepeatChild (event->element);
                    623:     return TemplateElementWillBeCreated (event);
1.122     kia       624:   }
1.52      vatton    625: #endif /* TEMPLATES */
1.71      quint     626:   return FALSE; /* let Thot perform normal operation */
                    627: }
                    628: 
                    629: /*----------------------------------------------------------------------
                    630:   UseCreated
                    631:   A new "use" element has just been created by the user with a generic editing
                    632:   command.
                    633:   -----------------------------------------------------------------------*/
                    634: void UseCreated (NotifyElement *event)
                    635: {
                    636: #ifdef TEMPLATES
1.148     kia       637:   Document        doc = event->document;
                    638:   Element         el = event->element;
1.117     kia       639:   Element         parent;
                    640:   Element         first;
                    641:   ElementType     parentType;
                    642:   XTigerTemplate  t;
                    643:   SSchema         templateSSchema;
1.130     vatton    644:   char*           types, *text = NULL;
1.148     kia       645: 
1.112     vatton    646:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       647:     return;
                    648: 
1.72      quint     649:   if (TtaGetFirstChild (el))
                    650:     /* this Use element has already some content. It has already been
                    651:        instanciated */
                    652:     return;
1.117     kia       653: 
1.171     kia       654:   t = GetXTigerDocTemplate (doc);
1.71      quint     655:   if (!t)
                    656:     return; // no template ?!?!
1.101     kia       657: 
1.138     vatton    658:   templateSSchema = TtaGetSSchema ("Template", doc);
1.117     kia       659:   parent = TtaGetParent(el);
                    660:   parentType = TtaGetElementType(parent);
1.148     kia       661: 
1.130     vatton    662:   if (parentType.ElSSchema == templateSSchema &&
                    663:       parentType.ElTypeNum == Template_EL_repeat)
1.117     kia       664:   {
1.130     vatton    665:     first = TtaGetFirstChild (parent);
                    666:     if (first)
1.117     kia       667:     {
1.130     vatton    668:       types = GetAttributeStringValueFromNum (first, Template_ATTR_types, NULL);
                    669:       if (types)
1.117     kia       670:       {
1.130     vatton    671:         SetAttributeStringValueWithUndo (el, Template_ATTR_types, types);
                    672:         text = GetAttributeStringValueFromNum (el, Template_ATTR_title, NULL);
                    673:         SetAttributeStringValueWithUndo (first, Template_ATTR_title, text);
                    674:         TtaFreeMemory (text);
                    675:         text = GetAttributeStringValueFromNum (el, Template_ATTR_currentType, NULL);
                    676:         SetAttributeStringValueWithUndo (first, Template_ATTR_currentType, text);
                    677:         TtaFreeMemory (text);
                    678:         TtaFreeMemory (types);
1.117     kia       679:       }
                    680:     }
                    681:   }
                    682: 
1.76      vatton    683:   InstantiateUse (t, el, doc, TRUE);
1.71      quint     684: #endif /* TEMPLATES */
                    685: }
1.29      tollenae  686: 
1.89      kia       687: 
1.98      kia       688: /*----------------------------------------------------------------------
                    689:   Template_CanInsertRepeatChild
                    690:   Test if a xt:repeat child can be inserted (number between params min and max).
                    691:   @param el element (xt:repeat) to test
                    692:   @return True if an element can be inserted.
                    693:   ----------------------------------------------------------------------*/
                    694: ThotBool Template_CanInsertRepeatChild(Element el)
                    695: {
                    696: #ifdef TEMPLATES
1.154     vatton    697:   char     *max;
                    698:   int       maxVal, curVal;
                    699:   Element   child;
1.148     kia       700: 
1.104     kia       701:   max = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
1.191     kia       702:   if (max && max[0]!=EOS)
1.104     kia       703:     {
1.154     vatton    704:       if (!strcmp(max, "*"))
                    705:         {
                    706:           TtaFreeMemory(max);
                    707:           return TRUE;
                    708:         }
                    709:       maxVal = atoi (max);
                    710:       TtaFreeMemory (max);
1.104     kia       711:       curVal = 0;
1.105     vatton    712:       for (child = TtaGetFirstChild(el); child; TtaNextSibling(&child))
1.104     kia       713:         curVal++;
1.154     vatton    714:       return (curVal < maxVal);
1.104     kia       715:     }
                    716:   else
1.98      kia       717:     return TRUE;
                    718: #endif /* TEMPLATES */
                    719:   return FALSE;
                    720: }
1.96      kia       721: 
1.89      kia       722: 
1.92      kia       723: #ifdef TEMPLATES
1.99      kia       724: /*----------------------------------------------------------------------
                    725:   QueryStringFromMenu
                    726:   Show a context menu to query a choice.
                    727:   @param items space-separated choice list string.
                    728:   @return The choosed item string or NULL if none.
                    729:   ----------------------------------------------------------------------*/
1.138     vatton    730: static char* QueryStringFromMenu (Document doc, char* items)
1.90      kia       731: {
                    732:   int nbitems, size;
                    733:   struct menuType *itemlist;
                    734:   char *menuString;
                    735:   char *result = NULL;
1.148     kia       736: 
1.138     vatton    737:   if (!TtaGetDocumentAccessMode (doc))
1.112     vatton    738:     return NULL;
                    739:   if (items == NULL)
1.110     kia       740:     return NULL;
1.138     vatton    741:   size = strlen (items);
                    742:   if (size == 0)
                    743:     return NULL;
1.90      kia       744:   giveItems (items, size, &itemlist, &nbitems);
                    745:   menuString = createMenuString (itemlist, nbitems);
                    746:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    747:                      nbitems, menuString , NULL, false, 'L');
                    748:   TtaFreeMemory (menuString);
                    749:   ReturnOption = -1;
                    750:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    751:   TtaWaitShowProcDialogue ();
                    752:   TtaDestroyDialogue (BaseDialog + OptionMenu);
1.148     kia       753: 
1.112     vatton    754:   if (ReturnOption!=-1)
1.90      kia       755:   {
                    756:     result = TtaStrdup(itemlist[ReturnOption].label);
                    757:   }
                    758:   
                    759:   TtaFreeMemory (itemlist);
                    760:   return result;
                    761: }
1.92      kia       762: #endif /* TEMPLATES */
1.90      kia       763: 
1.158     kia       764: #ifdef AMAYA_DEBUG
                    765: void FillInsertableElemList (Document doc, Element elem, DLList list);
                    766: #endif /* AMAYA_DEBUG */
1.162     vatton    767: /*----------------------------------------------------------------------
                    768:   ----------------------------------------------------------------------*/
                    769: char *Template_GetListTypes (XTigerTemplate t, Element el)
                    770: {
                    771: #ifdef TEMPLATES
                    772:   char  *listtypes = NULL, *types;
                    773: 
                    774:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
                    775:   if (types)
                    776:     listtypes = Template_ExpandTypes (t, types, el, FALSE);
                    777:   return listtypes;
                    778: #endif /* TEMPLATES */
                    779: }
1.158     kia       780: 
1.56      francesc  781: /*----------------------------------------------------------------------
1.138     vatton    782:   BagButtonClicked
                    783:   Called when a bag button is clicked.
                    784:   Can be called for useEl, useSimple or bag.
                    785:   If called for useEl or useSimple, the new element must be added after.
                    786:   If called for bag, the element must be added before all.
                    787:    
                    788:   Shows a menu with all the types that can be used in the bag.
                    789:   ----------------------------------------------------------------------*/
                    790: ThotBool BagButtonClicked (NotifyElement *event)
                    791: {
                    792: #ifdef TEMPLATES
                    793:   Document        doc = event->document;
                    794:   Element         el = event->element;
                    795:   ElementType     elType;
                    796:   XTigerTemplate  t;
                    797:   Declaration     decl;
                    798:   Element         bagEl = el;
                    799:   Element         firstEl;
                    800:   Element         newEl = NULL;
                    801:   View            view;
                    802:   SSchema         templateSSchema;
1.162     vatton    803:   char           *listtypes = NULL;
                    804:   char           *result = NULL;
1.138     vatton    805:   ThotBool        oldStructureChecking;
                    806:   DisplayMode     dispMode;
                    807: 
1.182     vatton    808:   if (!TtaGetDocumentAccessMode (doc))
1.138     vatton    809:     return TRUE;
1.182     vatton    810:   if (!IsTemplateInstanceDocument (doc))
                    811:     return FALSE; /* let Thot perform normal operation */
1.148     kia       812: 
1.138     vatton    813:   TtaGetActiveView (&doc, &view);
                    814:   if (view != 1)
                    815:     return FALSE; /* let Thot perform normal operation */
                    816: 
                    817:   TtaCancelSelection (doc);
                    818:   templateSSchema = TtaGetSSchema ("Template", doc);  
1.171     kia       819:   t = GetXTigerDocTemplate(doc);
1.138     vatton    820:   elType = TtaGetElementType (el);
                    821:   while (bagEl &&
                    822:          (elType.ElSSchema != templateSSchema ||
                    823:           elType.ElTypeNum != Template_EL_bag))
                    824:     {
                    825:       bagEl = TtaGetParent (bagEl);
                    826:       elType = TtaGetElementType (bagEl);
                    827:     }
                    828: 
                    829:   if (bagEl)
                    830:   {
1.162     vatton    831:     listtypes = Template_GetListTypes (t, bagEl);
                    832:     if (listtypes)
1.138     vatton    833:       {
1.158     kia       834: #ifdef AMAYA_DEBUG
                    835:       printf("BagButtonClicked : \n  > %s\n", listtypes);
                    836: //      {
                    837: //        DLList list = DLList_Create();
                    838: //        FillInsertableElemList (doc, TtaGetFirstChild(bagEl), list);
                    839: //        DLListNode node;
                    840: //        ForwardIterator iter = DLList_GetForwardIterator(list);
                    841: //        ITERATOR_FOREACH(iter, DLListNode, node)
                    842: //        {
                    843: //          ElemListElement elem = (ElemListElement)node->elem;
                    844: //          printf("  + %s\n", ElemListElement_GetName(elem));
                    845: //        }
                    846: //        DLList_Destroy(list);
                    847: //      }
                    848: #endif /* AMAYA_DEBUG */
1.138     vatton    849:         result = QueryStringFromMenu (doc, listtypes);
                    850:         TtaFreeMemory (listtypes);
                    851:         if (result)
                    852:           {
                    853:             decl = Template_GetDeclaration (t, result);
                    854:             if (decl)
                    855:               {
                    856:                 dispMode = TtaGetDisplayMode (doc);
                    857:                 if (dispMode == DisplayImmediately)
                    858:                   /* don't set NoComputedDisplay
                    859:                      -> it breaks down views formatting when Enter generates new elements  */
1.139     vatton    860:                   TtaSetDisplayMode (doc, DeferredDisplay);
1.138     vatton    861: 
                    862:                 /* Prepare insertion.*/          
                    863:                 oldStructureChecking = TtaGetStructureChecking (doc);
                    864:                 TtaSetStructureChecking (FALSE, doc);
                    865:                 TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    866: 
                    867:                 /* Insert */
                    868:                 if (el == bagEl)
                    869:                   {
                    870:                     el = TtaGetFirstChild (el);
                    871:                     TtaSelectElement (doc, el);
                    872:                     TtaInsertAnyElement (doc, TRUE);
                    873:                   }
                    874:                 else
                    875:                   {
                    876:                     TtaSelectElement (doc, el);
                    877:                     TtaInsertAnyElement (doc, FALSE);
                    878:                   }
1.155     kia       879:                 newEl = Template_InsertBagChild (doc, bagEl, decl, FALSE);
1.138     vatton    880: 
                    881:                 /* Finish insertion.*/
                    882:                 TtaCloseUndoSequence (doc);
                    883:                 TtaSetDocumentModified (doc);
                    884:                 TtaSetStructureChecking (oldStructureChecking, doc);
                    885:                 // restore the display
                    886:                 TtaSetDisplayMode (doc, dispMode);
                    887:                 firstEl = GetFirstEditableElement (newEl);
                    888:                 if (firstEl)
                    889:                   {
                    890:                     TtaSelectElement (doc, firstEl);
                    891:                     TtaSetStatusSelectedElement (doc, view, firstEl);
                    892:                   }
                    893:                 else
                    894:                   {
                    895:                     TtaSelectElement (doc, newEl);
                    896:                     TtaSetStatusSelectedElement (doc, view, newEl);
                    897:                   }
                    898:               }
                    899:           }
                    900:       }
                    901:     TtaFreeMemory (result);
                    902:   }
                    903: #endif /* TEMPLATES */
                    904:   return TRUE; /* don't let Thot perform normal operation */
                    905: }
                    906: 
                    907: /*----------------------------------------------------------------------
1.79      quint     908:   RepeatButtonClicked
1.89      kia       909:   Called when a repeat button is clicked.
                    910:   Can be called for useEl, useSimple or repeat.
                    911:   If called for useEl or useSimple, the new element must be added after.
                    912:   If called for repeat, the element must be added before all.
                    913:    
1.56      francesc  914:   Shows a menu with all the types that can be used in a use element.
                    915:   ----------------------------------------------------------------------*/
1.79      quint     916: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  917: {
                    918: #ifdef TEMPLATES
1.89      kia       919:   Document        doc = event->document;
                    920:   Element         el = event->element;
                    921:   ElementType     elType;
1.90      kia       922:   XTigerTemplate  t;
                    923:   Declaration     decl;
                    924:   Element         repeatEl = el;
                    925:   Element         firstEl;
                    926:   Element         newEl = NULL;
1.95      kia       927:   View            view;
1.162     vatton    928:   char           *listtypes = NULL;
                    929:   char           *result = NULL;
1.138     vatton    930:   ThotBool        oldStructureChecking;
                    931:   DisplayMode     dispMode;
1.104     kia       932: 
1.112     vatton    933:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       934:     return TRUE;
1.182     vatton    935:   if (!IsTemplateInstanceDocument (doc))
                    936:     return FALSE; /* let Thot perform normal operation */
1.148     kia       937: 
1.95      kia       938:   TtaGetActiveView (&doc, &view);
                    939:   if (view != 1)
                    940:     return FALSE; /* let Thot perform normal operation */
                    941: 
1.89      kia       942:   TtaCancelSelection(doc);
1.171     kia       943:   t = GetXTigerDocTemplate(doc);
1.89      kia       944:   elType = TtaGetElementType(el);
1.138     vatton    945:   while (elType.ElTypeNum != Template_EL_repeat)
1.179     kia       946:     {
                    947:       repeatEl = TtaGetParent(repeatEl);
                    948:       if (repeatEl == NULL)
                    949:         break;
                    950:       elType = TtaGetElementType(repeatEl);
                    951:     }
1.112     vatton    952:   if (repeatEl)
1.90      kia       953:     {
1.179     kia       954:       if (Template_CanInsertRepeatChild (repeatEl))
                    955:         {
                    956:           firstEl = TtaGetFirstChild (repeatEl);
                    957:           listtypes = Template_GetListTypes (t, firstEl);
                    958:           if (listtypes)
                    959:             {
1.158     kia       960: #ifdef AMAYA_DEBUG
1.179     kia       961:               printf("RepeatButtonClicked : \n  > %s\n", listtypes);
1.158     kia       962: #endif /* AMAYA_DEBUG */
1.179     kia       963: 
                    964:               result = QueryStringFromMenu (doc, listtypes);
                    965:               TtaFreeMemory (listtypes);
                    966:               if (result)
                    967:                 {
                    968:                   decl = Template_GetDeclaration (t, result);
                    969:                   if (decl)
                    970:                     {
                    971:                       dispMode = TtaGetDisplayMode (doc);
                    972:                       if (dispMode == DisplayImmediately)
                    973:                         /* don't set NoComputedDisplay
1.138     vatton    974:                  -> it breaks down views formatting when Enter generates new elements  */
1.179     kia       975:                         TtaSetDisplayMode (doc, DeferredDisplay);
                    976: 
                    977:                       /* Prepare insertion.*/          
                    978:                       oldStructureChecking = TtaGetStructureChecking (doc);
                    979:                       TtaSetStructureChecking (FALSE, doc);
                    980:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    981:                       /* Insert. */
                    982:                       if (el == repeatEl)
                    983:                         newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, NULL);
                    984:                       else
                    985:                         newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, el);
1.138     vatton    986: 
1.179     kia       987:                       /* Finish insertion.*/
                    988:                       TtaCloseUndoSequence(doc);
                    989:                       TtaSetDocumentModified (doc);
                    990:                       TtaSetStructureChecking (oldStructureChecking, doc);
                    991: 
                    992:                       // restore the display
                    993:                       TtaSetDisplayMode (doc, dispMode);
                    994:                       firstEl = GetFirstEditableElement (newEl);
                    995:                       if (firstEl)
                    996:                         {
                    997:                           TtaSelectElement (doc, firstEl);
                    998:                           TtaSetStatusSelectedElement (doc, view, firstEl);
                    999:                         }
                   1000:                       else
                   1001:                         {
                   1002:                           TtaSelectElement (doc, newEl);
                   1003:                           TtaSetStatusSelectedElement (doc, view, newEl);
                   1004:                         }
                   1005:                     }
                   1006:                 }
1.104     kia      1007:             }
1.179     kia      1008:           TtaFreeMemory(result);
                   1009:           DumpSubtree(repeatEl, doc, 0);
                   1010: 
                   1011:         }
                   1012:       else /* if (Template_CanInsertRepeatChild(repeatEl)) */
                   1013:         {
                   1014:           TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.98      kia      1015:         }
                   1016:     }
1.77      vatton   1017:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc 1018: #endif /* TEMPLATES */
1.94      kia      1019:   return TRUE;
                   1020: }
                   1021: 
                   1022: /*----------------------------------------------------------------------
                   1023:   UseButtonClicked
                   1024:   Shows a menu with all the types that can be used in a use element.
                   1025:   ----------------------------------------------------------------------*/
                   1026: ThotBool UseButtonClicked (NotifyElement *event)
                   1027: {
                   1028: #ifdef TEMPLATES
                   1029:   Document        doc = event->document;
                   1030:   Element         el = event->element;
1.99      kia      1031:   Element         child;
1.94      kia      1032:   ElementType     elType;
                   1033:   XTigerTemplate  t;
                   1034:   Declaration     decl;
                   1035:   Element         firstEl;
                   1036:   Element         newEl = NULL;
                   1037:   char*           types;
                   1038:   ThotBool        oldStructureChecking;
1.95      kia      1039:   View            view;
1.133     vatton   1040:   char*           listtypes = NULL;
                   1041:   char*           result = NULL;
1.95      kia      1042: 
1.182     vatton   1043:   if (!TtaGetDocumentAccessMode (doc))
1.110     kia      1044:     return TRUE;
1.182     vatton   1045:   if (!IsTemplateInstanceDocument (doc))
                   1046:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1047:     
1.95      kia      1048:   TtaGetActiveView (&doc, &view);
                   1049:   if (view != 1)
                   1050:     return FALSE; /* let Thot perform normal operation */
1.148     kia      1051: 
1.94      kia      1052:   TtaCancelSelection(doc);
1.171     kia      1053:   t = GetXTigerDocTemplate(doc);
1.94      kia      1054:   if (!t)
                   1055:     return FALSE; /* let Thot perform normal operation */
                   1056:   elType = TtaGetElementType(el);
                   1057: 
                   1058:   firstEl = TtaGetFirstChild(el);
1.112     vatton   1059:   if (firstEl)
1.94      kia      1060:   {
                   1061:     RepeatButtonClicked(event);
                   1062:   }
                   1063:   else
                   1064:   {
1.104     kia      1065:     types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.112     vatton   1066:     if (types)
1.94      kia      1067:     {
1.142     kia      1068:       listtypes = Template_ExpandTypes(t, types, NULL, FALSE);
1.158     kia      1069: #ifdef AMAYA_DEBUG
                   1070:       printf("UseButtonClicked : \n  > %s\n", listtypes);
                   1071: #endif /* AMAYA_DEBUG */
                   1072:       
1.104     kia      1073:       result = QueryStringFromMenu(doc, listtypes);
1.112     vatton   1074:       if (result)
1.94      kia      1075:       {
1.104     kia      1076:         decl = Template_GetDeclaration(t, result);
1.112     vatton   1077:         if (decl)
1.99      kia      1078:         {
1.104     kia      1079:           /* Prepare insertion.*/
                   1080:           oldStructureChecking = TtaGetStructureChecking (doc);
                   1081:           TtaSetStructureChecking (FALSE, doc);
1.138     vatton   1082:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104     kia      1083:           
                   1084:           /* Insert */
                   1085:           newEl = Template_InsertUseChildren(doc, el, decl);
                   1086:           for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
                   1087:           {
                   1088:             TtaRegisterElementCreate(child, doc);
                   1089:           }
                   1090:           
                   1091:           TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
                   1092:           TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
                   1093:           
1.117     kia      1094:           /* xt:currentType attribute.*/
                   1095:           SetAttributeStringValueWithUndo(el, Template_ATTR_currentType, result);
                   1096:           
1.104     kia      1097:           /* Finish insertion. */
                   1098:           TtaCloseUndoSequence(doc);
1.114     vatton   1099:           TtaSetDocumentModified (doc);
1.104     kia      1100:           TtaSetStructureChecking (oldStructureChecking, doc);
                   1101:           
                   1102:           firstEl = GetFirstEditableElement(newEl);
1.112     vatton   1103:           if (firstEl)
1.104     kia      1104:           {
                   1105:             TtaSelectElement (doc, firstEl);
                   1106:             TtaSetStatusSelectedElement(doc, view, firstEl);
                   1107:           }
                   1108:           else
                   1109:           {
                   1110:             TtaSelectElement (doc, newEl);
                   1111:             TtaSetStatusSelectedElement(doc, view, newEl);
                   1112:           }
1.98      kia      1113:         }
1.94      kia      1114:       }
                   1115:     }
1.104     kia      1116:     TtaFreeMemory(types);
1.94      kia      1117:     TtaFreeMemory(listtypes);
                   1118:     TtaFreeMemory(result);
                   1119:   }
1.148     kia      1120: 
1.94      kia      1121:   return TRUE;
                   1122: #endif /* TEMPLATES */
1.56      francesc 1123:        return TRUE;
                   1124: }
1.64      francesc 1125: 
1.89      kia      1126: 
1.103     kia      1127: /*----------------------------------------------------------------------
                   1128:   UseSimpleButtonClicked
                   1129:   ----------------------------------------------------------------------*/
                   1130: ThotBool UseSimpleButtonClicked (NotifyElement *event)
                   1131: {
                   1132: #ifdef TEMPLATES
1.181     vatton   1133:   ElementType     elType, parentType;
                   1134:   AttributeType   attrType;
                   1135:   Attribute       att;
                   1136: 
1.182     vatton   1137:   if (!TtaGetDocumentAccessMode (event->document))
1.110     kia      1138:     return TRUE;
1.182     vatton   1139:   if (!IsTemplateInstanceDocument (event->document))
                   1140:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1141: 
1.181     vatton   1142:   elType = TtaGetElementType (event->element);
                   1143:   attrType.AttrSSchema = elType.ElSSchema;
                   1144:   attrType.AttrTypeNum = Template_ATTR_option;
                   1145:   att = TtaGetAttribute (event->element, attrType);
                   1146:   if (att)
                   1147:     return OptionButtonClicked (event);
                   1148:   
                   1149:   parentType = TtaGetElementType (TtaGetParent( event->element));
1.112     vatton   1150:   if (parentType.ElTypeNum == Template_EL_repeat)
1.138     vatton   1151:     return RepeatButtonClicked (event);
                   1152:   else if (parentType.ElTypeNum == Template_EL_bag)
                   1153:     return BagButtonClicked (event);
1.103     kia      1154: #endif /* TEMPLATES */  
                   1155:   return FALSE;
                   1156: }
1.94      kia      1157: 
                   1158: /*----------------------------------------------------------------------
                   1159:   OptionButtonClicked
                   1160:   ----------------------------------------------------------------------*/
                   1161: ThotBool OptionButtonClicked (NotifyElement *event)
                   1162: {
                   1163: #ifdef TEMPLATES
1.145     kia      1164:   Element         useEl, contentEl, next;
1.182     vatton   1165:   ElementType     useType;
1.94      kia      1166:   Document        doc;
                   1167:   XTigerTemplate  t;
                   1168:   View            view;
                   1169: 
1.182     vatton   1170:   if (!TtaGetDocumentAccessMode (event->document))
1.110     kia      1171:     return TRUE;
1.182     vatton   1172:   if (!IsTemplateInstanceDocument (event->document))
                   1173:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1174: 
1.94      kia      1175:   TtaGetActiveView (&doc, &view);
                   1176:   if (view != 1)
                   1177:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1178: 
1.94      kia      1179:   doc = event->document;
1.182     vatton   1180:   useEl = event->element;
1.145     kia      1181:   if (!useEl)
1.94      kia      1182:     return FALSE; /* let Thot perform normal operation */
1.145     kia      1183:   useType = TtaGetElementType (useEl);
1.182     vatton   1184:   if (useType.ElTypeNum != Template_EL_useEl &&
                   1185:       useType.ElTypeNum != Template_EL_useSimple)
1.94      kia      1186:     return FALSE;
                   1187: 
1.145     kia      1188:   TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
1.94      kia      1189:   TtaCancelSelection (doc);
1.145     kia      1190:   contentEl = TtaGetFirstChild (useEl);
                   1191:   if (!contentEl)
1.94      kia      1192:     /* the "use" element is empty. Instantiate it */
                   1193:     {
1.171     kia      1194:       t = GetXTigerDocTemplate (doc);
1.94      kia      1195:       if (!t)
                   1196:         return FALSE; // no template ?!?!
1.145     kia      1197:       InstantiateUse (t, useEl, doc, TRUE);
1.94      kia      1198:     }
                   1199:   else
                   1200:     /* remove the content of the "use" element */
                   1201:     {
                   1202:       do
                   1203:         {
1.145     kia      1204:           next = contentEl;
1.94      kia      1205:           TtaNextSibling (&next);
1.145     kia      1206:           TtaRegisterElementDelete(contentEl, doc);
                   1207:           TtaDeleteTree (contentEl, doc);
                   1208:           contentEl = next;
1.94      kia      1209:         }
                   1210:       while (next);
1.145     kia      1211:       if (NeedAMenu (useEl, doc))
                   1212:         {
                   1213:           TtaChangeTypeOfElement (useEl, doc, Template_EL_useEl);
                   1214:           TtaRegisterElementTypeChange(useEl, Template_EL_useSimple, doc);
                   1215:         }
1.94      kia      1216:     }
                   1217:   TtaSelectElement (doc, event->element);
1.145     kia      1218:   TtaCloseUndoSequence(doc);
1.94      kia      1219:   return TRUE; /* don't let Thot perform normal operation */
                   1220: #endif /* TEMPLATES */
                   1221:   return TRUE;
                   1222: }
                   1223: 
1.191     kia      1224: 
                   1225: 
1.111     vatton   1226: /*----------------------------------------------------------------------
1.191     kia      1227:   Template_FillFromDocument
                   1228:   Fill XTigerTemplate structure with the content of the document.
                   1229:   Load dependencies if needed.
1.111     vatton   1230:   ----------------------------------------------------------------------*/
1.191     kia      1231: void Template_FillFromDocument (Document doc)
1.111     vatton   1232: {
                   1233: #ifdef TEMPLATES
1.191     kia      1234:   XTigerTemplate t = GetXTigerTemplate (DocumentURLs[doc]);
                   1235:   Element        root;
1.165     kia      1236:   
1.193     vatton   1237: #ifdef AMAYA_DEBUG
1.191     kia      1238:   printf("Template_FillFromDocument\n");
1.193     vatton   1239: #endif
                   1240:   if (t)
1.111     vatton   1241:     {
1.193     vatton   1242: #ifdef AMAYA_DEBUG
1.191     kia      1243:       printf("plop : %d\n", t->state);
1.193     vatton   1244: #endif
1.191     kia      1245:       SetTemplateDocument (t, doc);
1.144     vatton   1246: 
1.191     kia      1247:       Template_PrepareTemplate(t);
                   1248:       
                   1249:       if(IsTemplateInstanceDocument(doc))
1.144     vatton   1250:         {
1.193     vatton   1251: #ifdef AMAYA_DEBUG
1.191     kia      1252:           printf("  > instance\n");
1.193     vatton   1253: #endif
1.144     vatton   1254:           // fix all access rights in the instance
1.191     kia      1255:           root = TtaGetRootElement (doc);
                   1256:           TtaSetAccessRight (root, ReadOnly, doc);
1.166     kia      1257:           Template_FixAccessRight (t, root, doc);
1.144     vatton   1258:           TtaUpdateAccessRightInViews (doc, root);
                   1259:         }
1.193     vatton   1260: #ifdef AMAYA_DEBUG
1.191     kia      1261:       else if(t->state&templLibraryFlag)
                   1262:         printf("  > library\n");
                   1263:       else if(t->state&templTemplate)
                   1264:         printf("  > template\n");
1.193     vatton   1265: #endif      
1.191     kia      1266:       // Mark loaded
                   1267:       t->state |= templloaded;
                   1268:       TtaSetDocumentUnmodified (doc);
                   1269:       UpdateTemplateMenus (doc);
                   1270:       
                   1271: #ifdef AMAYA_DEBUG  
                   1272:       DumpAllDeclarations();
                   1273: #endif /* AMAYA_DEBUG */
                   1274:     }
                   1275: #endif /* TEMPLATES */
                   1276: }
                   1277: 
                   1278: 
                   1279: /*----------------------------------------------------------------------
                   1280:   Template_CheckAndPrepareTemplate checks if the document is a template
                   1281:   or a library and prepare XTigerTemplate structure to use it.
                   1282:   ----------------------------------------------------------------------*/
                   1283: ThotBool Template_CheckAndPrepareTemplate(char* docURL)
                   1284: {
                   1285: #ifdef TEMPLATES
                   1286:   XTigerTemplate t = NULL; //GetXTigerTemplate(docURL);
1.193     vatton   1287: 
                   1288: #ifdef AMAYA_DEBUG
1.191     kia      1289:   printf("Template_CheckAndPrepareTemplate %s\n", docURL);
1.193     vatton   1290: #endif
                   1291:   if (IsXTiger(docURL))
1.191     kia      1292:     {
                   1293:       t = LookForXTigerTemplate(docURL);
                   1294:       t->state |= templTemplate;
1.111     vatton   1295:     }
1.193     vatton   1296:   else if (IsXTigerLibrary(docURL))
1.191     kia      1297:     {
                   1298:       t = LookForXTigerLibrary(docURL);
                   1299:       t->state |= templLibrary;
                   1300:     }
1.193     vatton   1301:   return t != NULL;
1.111     vatton   1302: #endif /* TEMPLATES */
1.191     kia      1303:   return FALSE;
1.111     vatton   1304: }
1.94      kia      1305: 
1.191     kia      1306: 
1.66      vatton   1307: /*----------------------------------------------------------------------
1.191     kia      1308:   Template_CheckAndPrepareInstance checks if it is a template instance needs.
1.108     vatton   1309:   If it's an instance and the template is not loaded, load it into a
                   1310:   temporary file
1.66      vatton   1311:   ----------------------------------------------------------------------*/
1.191     kia      1312: void Template_CheckAndPrepareInstance (char *localFileName, Document doc, char* docURL)
1.65      francesc 1313: {
                   1314: #ifdef TEMPLATES
1.76      vatton   1315:   XTigerTemplate   t;
1.103     kia      1316:   char            *content, *ptr, *begin;
1.76      vatton   1317:   gzFile           stream;
                   1318:   char             buffer[2000];
1.77      vatton   1319:   int              res;
1.171     kia      1320:   char            *template_version = NULL,
                   1321:                   *template_url = NULL;
1.65      francesc 1322: 
1.171     kia      1323:   stream = TtaGZOpen (localFileName);
1.76      vatton   1324:   if (stream != 0)
1.65      francesc 1325:     {
1.76      vatton   1326:       res = gzread (stream, buffer, 1999);
                   1327:       if (res >= 0)
1.65      francesc 1328:         {
1.81      vatton   1329:           buffer[res] = EOS;
1.103     kia      1330:           begin = strstr (buffer, "<?xtiger");
                   1331:           
1.112     vatton   1332:           if (begin)
1.103     kia      1333:           {
                   1334:             // Search for template version
                   1335:             ptr = strstr (begin, "templateVersion");
                   1336:             if (ptr)
                   1337:               ptr = strstr (ptr, "=");
                   1338:             if (ptr)
                   1339:               ptr = strstr (ptr, "\"");
                   1340:             if (ptr)
                   1341:               {
                   1342:                 // template URI
                   1343:                 content = &ptr[1];
                   1344:                 ptr = strstr (content, "\"");
                   1345:               }
                   1346:             if (ptr)
                   1347:               {
                   1348:                 *ptr = EOS;
                   1349:                 //Get now the template URI
1.171     kia      1350:                 template_version = TtaStrdup (content);
1.103     kia      1351:                 *ptr = '"';
                   1352:               }
                   1353:            
                   1354:             // Search for template uri
                   1355:             ptr = strstr (begin, "template");
                   1356:             if (ptr && ptr[8] != 'V')
                   1357:               ptr = strstr (ptr, "=");
                   1358:             if (ptr)
                   1359:               ptr = strstr (ptr, "\"");
                   1360:             if (ptr)
                   1361:               {
                   1362:                 // template URI
                   1363:                 content = &ptr[1];
                   1364:                 ptr = strstr (content, "\"");
                   1365:               }
                   1366:             if (ptr)
                   1367:               {
                   1368:                 *ptr = EOS;
                   1369:                 //Get now the template URI
1.171     kia      1370:                 template_url = TtaStrdup (content);
                   1371: 
                   1372:                 t = GetXTigerTemplate (template_url);
1.103     kia      1373:                 if (!t)
                   1374:                   {
1.171     kia      1375:                     LoadTemplate (doc, template_url);
                   1376:                     t = GetXTigerTemplate(template_url);
1.103     kia      1377:                   }
1.171     kia      1378:                 Template_PrepareInstance(docURL, doc, template_version, template_url);
                   1379:                 template_version = NULL;
                   1380:                 template_url     = NULL;
                   1381:                 Template_AddReference (t);
1.103     kia      1382:                 *ptr = '"';
                   1383:               }
                   1384:           }
1.65      francesc 1385:         }
                   1386:     }
1.171     kia      1387:   TtaFreeMemory(template_version);
                   1388:   TtaFreeMemory(template_url);
1.76      vatton   1389:   TtaGZClose (stream);
1.65      francesc 1390: #endif /* TEMPLATES */
                   1391: }
                   1392: 
1.64      francesc 1393: /*----------------------------------------------------------------------
1.178     kia      1394:   ClosingTemplateDocument
                   1395:   Callback called before closing a document which uses templates.
1.64      francesc 1396:   ----------------------------------------------------------------------*/
1.178     kia      1397: ThotBool ClosingTemplateDocument(NotifyDialog* dialog)
1.64      francesc 1398: {
1.65      francesc 1399: #ifdef TEMPLATES
1.171     kia      1400:   XTigerTemplate t = GetXTigerDocTemplate(dialog->document);
                   1401:   if(t)
1.178     kia      1402:       Template_RemoveReference(t);
1.65      francesc 1403: #endif /* TEMPLATES */
                   1404:   return FALSE;
1.64      francesc 1405: }
1.87      kia      1406: 
                   1407: /*----------------------------------------------------------------------
1.120     kia      1408:   IsTemplateElement
1.138     vatton   1409:   Test if an element is a template element.
1.87      kia      1410:   ----------------------------------------------------------------------*/
1.140     vatton   1411: ThotBool IsTemplateElement (Element elem)
1.87      kia      1412: {
                   1413: #ifdef TEMPLATES
1.138     vatton   1414:   ElementType     elType;
                   1415: 
                   1416:   elType = TtaGetElementType(elem);
                   1417:   if (elType.ElSSchema)
1.164     kia      1418:     return (strcmp(TtaGetSSchemaName(elType.ElSSchema) , "Template") == 0);
1.138     vatton   1419: #endif /* TEMPLATES */
1.87      kia      1420:   return FALSE;
                   1421: }
                   1422: 
                   1423: 
                   1424: /*----------------------------------------------------------------------
                   1425:   GetFirstTemplateParentElement
1.138     vatton   1426:   Return the first element which has "Template" as schema name or null.
1.87      kia      1427:   ----------------------------------------------------------------------*/
                   1428: Element GetFirstTemplateParentElement(Element elem)
                   1429: {
                   1430: #ifdef TEMPLATES
1.138     vatton   1431:   ElementType     elType;
                   1432: 
                   1433:   elem = TtaGetParent (elem);
                   1434:   elType = TtaGetElementType(elem);
                   1435:   while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
                   1436: {
                   1437:     elem = TtaGetParent (elem);
                   1438:     elType = TtaGetElementType(elem);
1.87      kia      1439:   }
                   1440:   return elem;
                   1441: #else
                   1442:   return NULL;
                   1443: #endif /* TEMPLATES */
                   1444: }
1.101     kia      1445: 
1.103     kia      1446: 
1.101     kia      1447: /*----------------------------------------------------------------------
1.102     vatton   1448:   TemplateElementWillBeCreated
1.101     kia      1449:   Processed when an element will be created in a template context.
                   1450:   ----------------------------------------------------------------------*/
1.102     vatton   1451: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101     kia      1452: {
1.103     kia      1453: #ifdef TEMPLATES
                   1454:   ElementType elType = event->elementType;
                   1455:   Element     parent = event->element;
                   1456:   ElementType parentType = TtaGetElementType(parent);
1.113     kia      1457:   Element     ancestor;
                   1458:   ElementType ancestorType;
                   1459:   SSchema     templateSSchema;
                   1460:   char*       types;
                   1461:   ThotBool    b;
1.101     kia      1462: 
1.115     kia      1463:   if(event->info==1)
                   1464:     return FALSE;
                   1465: 
1.193     vatton   1466: #ifdef AMAYA_DEBUG
1.185     kia      1467:   printf("TemplateElementWillBeCreated\n");
1.193     vatton   1468: #endif
1.112     vatton   1469:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1470:     return TRUE;
                   1471: 
1.138     vatton   1472:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.102     vatton   1473:   if (templateSSchema == NULL)
                   1474:     return FALSE; // let Thot do the job
1.115     kia      1475: 
1.113     kia      1476:   // Fisrt, test if in a xt:bag or in a base-element xt:use
1.147     vatton   1477:   if(parentType.ElSSchema == templateSSchema)
1.113     kia      1478:     ancestor = parent;
                   1479:   else
1.147     vatton   1480:     ancestor = GetFirstTemplateParentElement (parent);
1.113     kia      1481: 
1.147     vatton   1482:   if (ancestor)
1.113     kia      1483:   {
                   1484:     ancestorType = TtaGetElementType(ancestor);
1.147     vatton   1485:     if (ancestorType.ElTypeNum == Template_EL_bag)
1.113     kia      1486:     {
1.147     vatton   1487:       // only check the use child
                   1488:       if (ancestor != parent)
                   1489:         return  FALSE; // let Thot do the job
                   1490:       if (elType.ElSSchema == templateSSchema &&
                   1491:           (elType.ElTypeNum == Template_EL_useSimple ||
                   1492:            elType.ElTypeNum == Template_EL_useEl))
1.116     kia      1493:         return FALSE;
1.147     vatton   1494:       return !Template_CanInsertElementInBagElement (event->document, elType, ancestor);      
1.113     kia      1495:     }
1.147     vatton   1496:     else if(ancestorType.ElTypeNum == Template_EL_useSimple ||
                   1497:             ancestorType.ElTypeNum == Template_EL_useEl)
1.113     kia      1498:     {
1.147     vatton   1499:       // only check the bag child @@@ will be check exclude/include later
1.172     kia      1500:       if (ancestor != parent)
                   1501:         return  FALSE; // let Thot do the job
1.113     kia      1502:       types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
1.128     kia      1503:       b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position);
                   1504:       TtaFreeMemory(types);
1.115     kia      1505:       return !b;
1.113     kia      1506:     }
                   1507:   }
1.115     kia      1508:   
1.147     vatton   1509:   if (elType.ElSSchema == templateSSchema && elType.ElTypeNum == Template_EL_TEXT_UNIT)
1.115     kia      1510:   {
                   1511:     return FALSE;
                   1512:   }
                   1513:   
1.113     kia      1514:   // Can not insert.
                   1515:   return TRUE;
1.101     kia      1516: #endif /* TEMPLATES*/
1.102     vatton   1517:   return FALSE;
1.101     kia      1518: }
                   1519: 
                   1520: /*----------------------------------------------------------------------
                   1521:   TemplateElementWillBeDeleted
                   1522:   Processed when an element will be deleted in a template context.
                   1523:   ----------------------------------------------------------------------*/
                   1524: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1525: {
1.107     kia      1526: #ifdef TEMPLATES
                   1527:   Document       doc = event->document;
                   1528:   Element        elem = event->element;
1.163     vatton   1529:   Element        xtElem, parent = NULL, sibling;
1.117     kia      1530:   ElementType    xtType, elType;
1.107     kia      1531:   char*          type;
                   1532:   Declaration    dec;
1.115     kia      1533:   SSchema        templateSSchema;
1.107     kia      1534:   XTigerTemplate t;
1.156     vatton   1535:   ThotBool       selparent = FALSE;
1.192     kia      1536:   
1.115     kia      1537:   if(event->info==1)
                   1538:     return FALSE;
                   1539: 
1.112     vatton   1540:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1541:     return TRUE;
                   1542: 
1.138     vatton   1543:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.107     kia      1544:   if (templateSSchema == NULL)
                   1545:     return FALSE; // let Thot do the job
1.192     kia      1546:   
                   1547:   t = GetXTigerDocTemplate(doc);
                   1548:   if(Template_IsTemplate(t)||Template_IsLibrary(t))
                   1549:     return FALSE; // If template or library, pass to specialized functions.
1.122     kia      1550: 
1.107     kia      1551:   xtElem = GetFirstTemplateParentElement(elem);
1.112     vatton   1552:   if (xtElem)
1.107     kia      1553:   {
                   1554:     xtType = TtaGetElementType(xtElem);
1.117     kia      1555:     
1.192     kia      1556:     
1.109     kia      1557: 
1.112     vatton   1558:     if (xtType.ElTypeNum==Template_EL_bag)
1.117     kia      1559:     {
                   1560:       elType = TtaGetElementType(elem);
                   1561:       if(elType.ElSSchema==templateSSchema &&
                   1562:         (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
                   1563:       {
                   1564:         // Remove element manually.
                   1565:         TtaOpenUndoSequence(doc, elem, elem, 0, 0);
                   1566:         TtaRegisterElementDelete(elem, doc);
                   1567:         TtaDeleteTree(elem, doc);
                   1568:         TtaCloseUndoSequence(doc);
                   1569:         return TRUE;
                   1570:       }
                   1571:       else
                   1572:         return FALSE; // xt:bag always allow remove children.
                   1573:     }
1.112     vatton   1574:     else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107     kia      1575:     {
1.109     kia      1576:       parent = TtaGetParent(elem);
1.117     kia      1577:       if (xtElem!=parent)
                   1578:       {
                   1579:         type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
                   1580:         dec = Template_GetDeclaration(t, type);
                   1581:         TtaFreeMemory(type);
                   1582:         
                   1583:         if (dec && dec->nature == XmlElementNat)
                   1584:           return FALSE; // Can remove element only if in xt:use current type is base language element. 
                   1585:         else
                   1586:           return TRUE;
1.107     kia      1587:       }
1.109     kia      1588:     }
1.112     vatton   1589:     else if (xtType.ElTypeNum==Template_EL_repeat)
1.109     kia      1590:     {
1.156     vatton   1591:       sibling = TtaGetSuccessor (elem);
                   1592:       if (sibling == NULL)
                   1593:         {
                   1594:           // there is no next element
                   1595:           sibling = TtaGetPredecessor (elem);
                   1596:           if (sibling == NULL)
                   1597:             selparent = TRUE;
                   1598:         }
1.109     kia      1599:       TtaRegisterElementDelete(elem, doc);
                   1600:       TtaDeleteTree(elem, doc);
                   1601:       InstantiateRepeat(t, xtElem, doc, TRUE);
1.156     vatton   1602:       if (selparent)
                   1603:         // look for the new sibling
                   1604:         sibling = TtaGetFirstChild (parent);
                   1605:       if (sibling)
                   1606:         TtaSelectElement(doc, sibling);
1.151     quint    1607:       else
1.156     vatton   1608:         TtaSelectElement(doc, parent);
1.109     kia      1609:       return TRUE;
1.107     kia      1610:     }
                   1611:   }
1.109     kia      1612:   
                   1613:   //TODO Test if current element is use or repeat.
                   1614:   // Because if an element is delete and it is the unique child of its parent,
                   1615:   // the parent intends to destroy itself. 
                   1616:   
1.107     kia      1617:   return TRUE;
                   1618: #else /* TEMPLATES */
1.101     kia      1619:   return FALSE;
1.107     kia      1620: #endif /* TEMPLATES */
1.101     kia      1621: }
                   1622: 
1.109     kia      1623: /*----------------------------------------------------------------------
                   1624:   CurrentTypeWillBeExported
                   1625:   Check if the xt:currentType attribute can be exported
                   1626:   ----------------------------------------------------------------------*/
                   1627: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
                   1628: {
                   1629: #ifdef TEMPLATES
1.110     kia      1630: 
1.112     vatton   1631:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1632:     return TRUE;
                   1633: 
1.112     vatton   1634:   if (IsTemplateDocument(event->document))
1.109     kia      1635:     return TRUE;
                   1636: #endif /* TEMPLATES */
                   1637:   return FALSE;
                   1638: }
1.127     kia      1639: 
                   1640: /*----------------------------------------------------------------------
                   1641:   TemplateAttrInMenu
                   1642:   Called by Thot when building the Attributes menu for template elements.
                   1643:   ----------------------------------------------------------------------*/
                   1644: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
                   1645: {
                   1646: #ifdef TEMPLATES
                   1647:   // Prevent from showing attributes for template instance but not templates.
                   1648:   if(IsTemplateInstanceDocument(event->document))
                   1649:     return TRUE;
                   1650:   else
                   1651: #endif /* TEMPLATES */
                   1652:     return FALSE;
                   1653: }
1.160     kia      1654: 
1.167     kia      1655: /*----------------------------------------------------------------------
1.168     kia      1656:   CreateTemplateFromDocument
                   1657:   Create a template from the current document.
1.167     kia      1658:   ----------------------------------------------------------------------*/
                   1659: void CreateTemplateFromDocument(Document doc, View view)
                   1660: {
1.171     kia      1661: #ifdef TEMPLATES
1.196     vatton   1662:   char buffer[MAX_LENGTH], suffix[10];
                   1663: 
                   1664:   // removet the current suffix
                   1665:   strcpy (buffer, DocumentURLs[doc]);
                   1666:   TtaExtractSuffix (buffer, suffix);
                   1667:   if (suffix[0] == EOS && IsW3Path (buffer) &&
                   1668:       DocumentMeta[doc] && DocumentMeta[doc]->content_location)
                   1669:     // use the location instead of the current URI
                   1670:     strcpy (buffer, DocumentMeta[doc]->content_location);
                   1671:   // the new suffix
1.168     kia      1672:   strcat(buffer, ".xtd");
                   1673:   DontReplaceOldDoc = TRUE;
                   1674:   CreateTemplate(doc, buffer);
1.171     kia      1675: #endif /* TEMPLATES */
1.167     kia      1676: }
                   1677: 
1.169     kia      1678: /*----------------------------------------------------------------------
                   1679:   UpdateTemplateMenus
                   1680:   ----------------------------------------------------------------------*/
                   1681: void UpdateTemplateMenus (Document doc)
                   1682: {
1.196     vatton   1683:   if (IsTemplateInstanceDocument(doc))
1.180     kia      1684:     {
                   1685:       // Instance document
                   1686:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1687:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1688:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1689:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1690:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1691:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180     kia      1692:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196     vatton   1693:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1694:     }
1.183     vatton   1695:   else if (DocumentURLs[doc] && IsXTiger (DocumentURLs[doc]))
1.180     kia      1696:     {
                   1697:       // Template document
                   1698:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1699:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1700:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1701:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1702:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeat);
                   1703:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180     kia      1704:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateFreeBox);      
1.196     vatton   1705:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1706:     }
1.169     kia      1707:   else
1.180     kia      1708:     {
                   1709:       //Standard document
                   1710:       TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
                   1711:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1712:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1713:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1714:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1715:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180     kia      1716:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196     vatton   1717:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1718:     }
1.169     kia      1719: }
1.171     kia      1720: 
                   1721: /*----------------------------------------------------------------------
                   1722:   UninstanciateTemplateDocument
                   1723:   An instance of a template is tranformed into a template-less docuemnt.
                   1724:   Remove link between XTigerTemplate structure and document.
                   1725:   ----------------------------------------------------------------------*/
                   1726: void UninstanciateTemplateDocument(Document doc)
                   1727: {
                   1728: #ifdef TEMPLATES
                   1729:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1730:   if(t)
                   1731:     Template_Close(t);
                   1732: #endif /* TEMPLATES */  
                   1733: }
                   1734: 
                   1735: 
                   1736: /*----------------------------------------------------------------------
                   1737:   Template_PrepareInstance
                   1738:   Allocate XTigerTemplate structure for instance and initialize template
                   1739:   url and template version.
                   1740:   ----------------------------------------------------------------------*/
                   1741: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
                   1742: {
                   1743: #ifdef TEMPLATES
                   1744:   XTigerTemplate t = GetXTigerTemplate(fileName);
                   1745:   if(!t)
                   1746:     t = NewXTigerTemplate(fileName);
                   1747:   t->state           = templInstance;
                   1748:   t->templateVersion = template_version;
                   1749:   t->base_uri        = template_url;
                   1750:   t->doc             = doc;
1.178     kia      1751:   t->ref             = 1;
1.171     kia      1752:   
                   1753: #endif /* TEMPLATES */
                   1754: }
                   1755: 
                   1756: 
                   1757: /*----------------------------------------------------------------------
                   1758:   SetDocumentAsXTigerTemplate
                   1759:   Set the document template structure as template.
                   1760:   ----------------------------------------------------------------------*/
                   1761: void SetDocumentAsXTigerTemplate(Document doc)
                   1762: {
                   1763: #ifdef TEMPLATES
                   1764:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1765:   if(t)
                   1766:     t->state |= templTemplate;
                   1767: #endif /* TEMPLATES */  
                   1768: }
                   1769: 
                   1770: /*----------------------------------------------------------------------
                   1771:   SetDocumentAsXTigerLibrary
                   1772:   Set the document template structure as template library.
                   1773:   ----------------------------------------------------------------------*/
                   1774: void SetDocumentAsXTigerLibrary(Document doc)
                   1775: {
                   1776: #ifdef TEMPLATES
                   1777:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1778:   if(t)
                   1779:     t->state |= templLibrary;
                   1780: #endif /* TEMPLATES */  
                   1781: }
1.174     kia      1782: 
                   1783: 
                   1784: /*----------------------------------------------------------------------
                   1785:   TemplateCreateTextBox
                   1786:   Create a xt:use types="string" box around the selection.
                   1787:   ----------------------------------------------------------------------*/
                   1788: void TemplateCreateTextBox(Document doc, View view)
                   1789: {
                   1790: #ifdef TEMPLATES
                   1791:   Element     selElem;
                   1792:   ElementType selType;
                   1793:   int         firstChar, lastChar;
                   1794:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1795:   ElementType useType;
                   1796:   Element     use;
                   1797:   char        buffer[128];
1.189     kia      1798:   ThotBool    oldStructureChecking;
1.174     kia      1799:   
                   1800:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
1.184     vatton   1801:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174     kia      1802: 
                   1803:   if (!TtaGetDocumentAccessMode(doc))
                   1804:     return;
                   1805:   
                   1806:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1807:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1808:     {
                   1809:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1810:       if(selElem)
                   1811:         {
                   1812:           selType =  TtaGetElementType(selElem);
                   1813:           if(!TtaIsLeaf(selType))
                   1814:             {
                   1815:               selElem = TtaGetFirstLeaf(selElem);
                   1816:               selType = TtaGetElementType(selElem);
                   1817:               firstChar = lastChar = 0;
                   1818:             }
                   1819:           
                   1820:           if(selType.ElTypeNum==1)
                   1821:             {
                   1822:               QueryStringFromUser(label, title, buffer, 127);
                   1823:               useType.ElSSchema = sstempl;
                   1824:               useType.ElTypeNum = Template_EL_useSimple;
                   1825: 
1.189     kia      1826:               oldStructureChecking = TtaGetStructureChecking (doc);
                   1827:               TtaSetStructureChecking (FALSE, doc);
                   1828: 
1.174     kia      1829:               if(firstChar==0)
                   1830:                 {
                   1831:                   use = TtaNewElement(doc, useType);
                   1832:                   if(use)
                   1833:                     {
                   1834:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1835:                       TtaInsertSibling(use, selElem, FALSE, doc);
                   1836:                       TtaRegisterElementCreate(use, doc);
                   1837:                       TtaRegisterElementDelete (selElem, doc);
                   1838:                       TtaRemoveTree(selElem, doc);
                   1839:                       TtaInsertFirstChild(&selElem, use, doc);
                   1840:                       TtaRegisterElementDelete (selElem, doc);
                   1841:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1842:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);
                   1843:                       TtaCloseUndoSequence(doc);
                   1844:                       TtaSelectElement(doc, use);
                   1845:                     }
                   1846:                 }
                   1847:               else
                   1848:                 {
                   1849:                   GenerateInlineElement(Template_EL_useSimple, sstempl, 0, "", TRUE);
                   1850:                   TtaGiveFirstSelectedElement(doc, &use, &firstChar, &lastChar);
                   1851:                   selType =  TtaGetElementType(use);
                   1852:                   if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_useSimple)
                   1853:                     {
                   1854:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1855:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);                      
                   1856:                     }
                   1857:                 }
1.189     kia      1858:               
                   1859:               TtaSetStructureChecking (oldStructureChecking, doc);
                   1860:               
1.174     kia      1861:             }
                   1862:           
                   1863:         }
                   1864:     }
                   1865: #endif /* TEMPLATES */
                   1866: }
                   1867: 
                   1868: 
                   1869: /*----------------------------------------------------------------------
                   1870:   TemplateCreateFreeBox
                   1871:   Create a xt:bag types="string" box around the selection.
                   1872:   ----------------------------------------------------------------------*/
                   1873: void TemplateCreateFreeBox(Document doc, View view)
                   1874: {
                   1875: #ifdef TEMPLATES
1.185     kia      1876:   Element     selElem, selElem2, parent, parent2, current, child = NULL;
1.174     kia      1877:   ElementType selType, selType2;
                   1878:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1879:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1880: 
                   1881:   ElementType bagType;
                   1882:   Element     bag;
                   1883:   char        buffer[128];
                   1884: 
                   1885:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
1.184     vatton   1886:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174     kia      1887: 
                   1888:   if (!TtaGetDocumentAccessMode(doc))
                   1889:     return;
                   1890:   
                   1891:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1892:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1893:     {
                   1894:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1895:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   1896:       
                   1897:       if(selElem && selElem2)
                   1898:         {
                   1899:           selType =  TtaGetElementType(selElem);
                   1900:           selType2 =  TtaGetElementType(selElem2);
                   1901: 
                   1902:           QueryStringFromUser(label, title, buffer, 127);
                   1903:           bagType.ElSSchema = sstempl;
                   1904:           bagType.ElTypeNum = Template_EL_bag;
                   1905:           
1.185     kia      1906:           parent  = TtaGetParent(selElem);
                   1907:           parent2 = TtaGetParent(selElem2);
                   1908:           
                   1909:           if(firstChar==0 && firstChar2==0 && parent == parent2)
1.174     kia      1910:             {
1.185     kia      1911:               ThotBool        oldStructureChecking;
                   1912:               DisplayMode     dispMode;
                   1913:               dispMode = TtaGetDisplayMode (doc);
                   1914:               if (dispMode == DisplayImmediately)
                   1915:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   1916:               oldStructureChecking = TtaGetStructureChecking (doc);
                   1917:               TtaSetStructureChecking (FALSE, doc);
                   1918: 
                   1919:               // Create and insert xt:bag element
                   1920:               bag = TtaNewElement(doc, bagType);
                   1921:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1922:               TtaInsertSibling(bag, selElem, TRUE, doc);
                   1923:               TtaRegisterElementCreate(bag, doc);
                   1924: 
                   1925:               TtaNextSibling(&selElem2);
                   1926:               while(selElem!=selElem2)
1.174     kia      1927:                 {
1.185     kia      1928:                   current = selElem;
                   1929:                   TtaNextSibling(&selElem);
1.174     kia      1930:                   
1.185     kia      1931:                   TtaRegisterElementDelete (current, doc);
                   1932:                   TtaRemoveTree(current, doc);
                   1933:                   if(child)
                   1934:                     TtaInsertSibling(current, child, FALSE, doc);
                   1935:                   else
                   1936:                     TtaInsertFirstChild(&current, bag, doc);
                   1937:                   TtaRegisterElementDelete (current, doc);
                   1938:                   child = current;
1.174     kia      1939:                 }
1.185     kia      1940:               
                   1941:               SetAttributeStringValue(bag, Template_ATTR_types, "any");
                   1942:               SetAttributeStringValue(bag, Template_ATTR_title, buffer);
                   1943:               TtaCloseUndoSequence(doc);
                   1944:               TtaSelectElement(doc, bag);
                   1945:               
                   1946:               TtaSetStructureChecking (oldStructureChecking, doc);
                   1947:               TtaSetDisplayMode (doc, dispMode);
1.174     kia      1948:             }
                   1949:         }
                   1950:     }
                   1951: #endif /* TEMPLATES */
                   1952: }
                   1953: 
1.180     kia      1954: /*----------------------------------------------------------------------
                   1955:   TemplateCreateRepeat
                   1956:   Create a xt:repeat around the selection.
                   1957:   ----------------------------------------------------------------------*/
                   1958: void TemplateCreateRepeat(Document doc, View view)
                   1959: {
                   1960: #ifdef TEMPLATES
1.187     kia      1961:   ThotBool    oldStructureChecking;
                   1962:   DisplayMode dispMode;
                   1963:   Element     selElem, selElem2, parent, parent2;
                   1964:   ElementType selType, selType2;
1.180     kia      1965:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1966:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
1.197   ! kia      1967:   const char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATCOMP);
        !          1968:   const char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATLABEL);
        !          1969:   char        buffer[128];
1.180     kia      1970: 
1.186     kia      1971:   ElementType repType;
1.187     kia      1972:   Element     rep, use;
1.180     kia      1973: 
                   1974:   if (!TtaGetDocumentAccessMode(doc))
                   1975:     return;
                   1976:   
                   1977:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1978:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1979:     {
                   1980:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1981:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1.185     kia      1982: 
1.180     kia      1983:       if(selElem && selElem2)
                   1984:         {
                   1985:           selType =  TtaGetElementType(selElem);
                   1986:           selType2 =  TtaGetElementType(selElem2);
                   1987: 
                   1988:           repType.ElSSchema = sstempl;
                   1989:           repType.ElTypeNum = Template_EL_repeat;
1.186     kia      1990:           
1.185     kia      1991:           parent  = TtaGetParent(selElem);
                   1992:           parent2 = TtaGetParent(selElem2);
                   1993: 
1.197   ! kia      1994:           QueryStringFromUser(label, title, buffer, 127);
        !          1995:           if(firstChar==0 && firstChar2==0 && parent == parent2 && buffer[0]!=0)
1.180     kia      1996:             {
1.187     kia      1997:               dispMode = TtaGetDisplayMode (doc);
                   1998:               if (dispMode == DisplayImmediately)
                   1999:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   2000:               oldStructureChecking = TtaGetStructureChecking (doc);
                   2001:               TtaSetStructureChecking (FALSE, doc);
                   2002:               
                   2003:               if(selElem==selElem2 && selType.ElSSchema==sstempl && 
                   2004:                   (selType.ElTypeNum==Template_EL_useEl || 
                   2005:                    selType.ElTypeNum==Template_EL_useSimple))
                   2006:                 use = selElem;
                   2007:               else
1.197   ! kia      2008:                 use = Template_CreateUseFromSelection(doc, TRUE);
1.187     kia      2009:               if(use)
                   2010:                 {
                   2011:                   TtaExtendUndoSequence(doc);
                   2012:                   
                   2013:                   rep = TtaNewElement(doc, repType);
1.197   ! kia      2014:                   
1.187     kia      2015:                   TtaInsertSibling(rep, use, FALSE, doc);
1.197   ! kia      2016:                   SetAttributeStringValue(rep, Template_ATTR_title, buffer);
1.187     kia      2017:                   TtaRegisterElementCreate(rep, doc);
1.197   ! kia      2018:                   
1.187     kia      2019:                   TtaRegisterElementDelete (use, doc);
                   2020:                   TtaRemoveTree(use, doc);
                   2021:                   
                   2022:                   TtaInsertFirstChild(&use, rep, doc);
                   2023:                   TtaRegisterElementCreate(use, doc);
                   2024:                   
                   2025:                   TtaSelectElement(doc, use);
                   2026:                   TtaCloseUndoSequence(doc);
                   2027:                 }
                   2028:               
                   2029:               TtaSetStructureChecking (oldStructureChecking, doc);
                   2030:               TtaSetDisplayMode (doc, dispMode);
                   2031:             }
                   2032:         }
                   2033:     }
                   2034: #endif /* TEMPLATES */
                   2035: }
                   2036: 
1.195     kia      2037: /*----------------------------------------------------------------------
1.196     vatton   2038:   TemplateCreateUnion
                   2039:   Create a xt:union around the selection.
                   2040:   ----------------------------------------------------------------------*/
                   2041: void TemplateCreateUnion(Document doc, View view)
                   2042: {
                   2043: #ifdef TEMPLATES
                   2044: #endif /* TEMPLATES */
                   2045: }
                   2046: 
                   2047: /*----------------------------------------------------------------------
1.195     kia      2048:   TemplateCreateRepeatComp
                   2049:   Create a xt:repeat around the selection.
                   2050:   ----------------------------------------------------------------------*/
                   2051: void TemplateCreateRepeatComp(Document doc, View view)
                   2052: {
                   2053:   
                   2054: }
                   2055: 
1.187     kia      2056: /*----------------------------------------------------------------------
                   2057:   TemplateCreateUseBox
                   2058:   Create a xt:use around the selection.
                   2059:   ----------------------------------------------------------------------*/
                   2060: void TemplateCreateUseBox(Document doc, View view)
                   2061: {
                   2062: #ifdef TEMPLATES
1.197   ! kia      2063:   Template_CreateUseFromSelection(doc, FALSE);
1.187     kia      2064: #endif /* TEMPLATES */
                   2065: }
                   2066: 
1.194     kia      2067: /*----------------------------------------------------------------------
1.195     kia      2068:   TemplateCreateUseCompBox
                   2069:   Create a xt:use around the selection.
                   2070:   ----------------------------------------------------------------------*/
                   2071: void TemplateCreateUseCompBox(Document doc, View view)
                   2072: {
                   2073: #ifdef TEMPLATES
1.197   ! kia      2074:   Template_CreateUseFromSelection(doc, TRUE);
1.195     kia      2075: #endif /* TEMPLATES */
                   2076: }
                   2077: 
                   2078: /*----------------------------------------------------------------------
1.194     kia      2079:   Template_CreateInlineUse
                   2080:   Create an inline xt:use with the selection.
                   2081:   ----------------------------------------------------------------------*/
                   2082: static ThotBool Template_CreateInlineUse(Document doc)
                   2083: {
                   2084: #ifdef TEMPLATES
                   2085:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   2086:   int            firstChar, lastChar;
                   2087:   Element        selElem, use;
                   2088:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2089:   char          *proposed, *label = NULL, *types=NULL;
                   2090:   ThotBool       option, res = FALSE;
                   2091:   
                   2092:   if(t && sstempl)
                   2093:     {
                   2094:       proposed = Template_GetInlineLevelDeclarations(t, TRUE, TRUE);
                   2095:       if(QueryNewUseFromUser(proposed, &label, &types, &option))
                   2096:         {
                   2097:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2098: 
                   2099:           GenerateInlineElement(Template_EL_useEl, sstempl, 0, NULL, TRUE);
                   2100:           
                   2101:           TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.197   ! kia      2102:           
        !          2103:           use = selElem;
        !          2104:           TtaNextSibling(&use);
        !          2105: 
1.194     kia      2106:           SetAttributeStringValue(use, Template_ATTR_title, label);
                   2107:           SetAttributeStringValue(use, Template_ATTR_types, types);
                   2108:           res = TRUE;
                   2109:           
                   2110:           TtaSelectElement(doc, use);
                   2111:           TtaCloseUndoSequence(doc);
                   2112:         }
                   2113:       
                   2114:       TtaFreeMemory(proposed);
                   2115:       TtaFreeMemory(label);
                   2116:       TtaFreeMemory(types);
                   2117:       return res;
                   2118:     }  
                   2119:   
                   2120: #endif /* TEMPLATES */
                   2121:   return FALSE;
                   2122:  
                   2123: }
                   2124: 
                   2125: /*----------------------------------------------------------------------
                   2126:   Template_CreateEmptyBlockUse
                   2127:   Create a block xt:use with the selection.
                   2128:   ----------------------------------------------------------------------*/
                   2129: static ThotBool Template_CreateEmptyBlockUse(Document doc)
                   2130: {
                   2131: #ifdef TEMPLATES
                   2132:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   2133:   ElementType    useType;
                   2134:   int            firstChar, lastChar;
                   2135:   Element        selElem, use;
                   2136:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2137:   char          *proposed, *label = NULL, *types=NULL;
                   2138:   ThotBool       option, res = FALSE;
                   2139:   
                   2140:   
                   2141:   if(t && sstempl)
                   2142:     {
                   2143:       proposed = Template_GetBlockLevelDeclarations(t, TRUE);
                   2144:       if(QueryNewUseFromUser(proposed, &label, &types, &option))
                   2145:         {
                   2146:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2147: 
                   2148:           TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2149:           useType.ElSSchema = sstempl;
                   2150:           useType.ElTypeNum = Template_EL_useEl;
                   2151:           use = TtaNewElement(doc, useType);
                   2152:           
                   2153:           TtaInsertSibling(use, selElem, FALSE, doc);
                   2154:           TtaDeleteTree(selElem, doc);
                   2155:           
                   2156:           SetAttributeStringValue(use, Template_ATTR_title, label);
                   2157:           SetAttributeStringValue(use, Template_ATTR_types, types);
                   2158:           TtaRegisterElementCreate(use, doc);
                   2159:           res = TRUE;
                   2160:           
                   2161:           TtaSelectElement(doc, use);
                   2162:           TtaCloseUndoSequence(doc);
                   2163: 
                   2164:         }
                   2165:       
                   2166:       TtaFreeMemory(proposed);
                   2167:       TtaFreeMemory(label);
                   2168:       TtaFreeMemory(types);
                   2169:       return res;    
                   2170:     }  
                   2171:   
                   2172: #endif /* TEMPLATES */
                   2173:   return FALSE;
                   2174: }
1.187     kia      2175: 
                   2176: /*----------------------------------------------------------------------
                   2177:   Template_CreateUseFromSelection
                   2178:   Create a xt:use with the selection.
1.197   ! kia      2179:   If selection is empty, insert an inline xt:use.
        !          2180:   If createComp is false, create a xt:use with types at the selected block. 
        !          2181:   If createComp is true, create a component with the selection.
        !          2182:   Return the xt:use element.
1.187     kia      2183:   ----------------------------------------------------------------------*/
1.197   ! kia      2184: Element Template_CreateUseFromSelection(Document doc, ThotBool createComp)
1.187     kia      2185: {
                   2186: #ifdef TEMPLATES
                   2187:   ThotBool    oldStructureChecking;
                   2188:   DisplayMode dispMode;
                   2189:   Element     selElem, selElem2, parent, parent2;
                   2190:   ElementType selType, selType2;
                   2191:   int         firstChar, lastChar, firstChar2, lastChar2;
1.194     kia      2192:   SSchema     sstempl = TtaGetSSchema ("Template", doc),
                   2193:               sshtml  = TtaGetSSchema ("HTML", doc);
1.187     kia      2194: 
                   2195:   ElementType useType;
                   2196:   Element     use = NULL, comp, prev;
                   2197:   char        buffer[128];
                   2198:   int         sz = 128;
                   2199: 
                   2200:   if (!TtaGetDocumentAccessMode(doc))
                   2201:     return NULL;
                   2202:   
                   2203:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   2204:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2205:     {
                   2206:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2207:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   2208: 
                   2209:       if(selElem && selElem2)
                   2210:         {
                   2211:           selType =  TtaGetElementType(selElem);
                   2212:           selType2 =  TtaGetElementType(selElem2);
                   2213:           parent  = TtaGetParent(selElem);
                   2214:           parent2 = TtaGetParent(selElem2);
                   2215: 
1.194     kia      2216:           dispMode = TtaGetDisplayMode (doc);
                   2217:           if (dispMode == DisplayImmediately)
                   2218:             TtaSetDisplayMode (doc, DeferredDisplay);
                   2219:           oldStructureChecking = TtaGetStructureChecking (doc);
                   2220:           TtaSetStructureChecking (FALSE, doc);
                   2221:           
1.187     kia      2222:           if(firstChar==0 && firstChar2==0 && parent == parent2)
                   2223:             {
1.194     kia      2224:               if(selType.ElSSchema==sshtml && selType.ElTypeNum==HTML_EL_Element)
                   2225:                 Template_CreateEmptyBlockUse(doc);
                   2226:               else
1.186     kia      2227:                 {
1.194     kia      2228:                   prev = selElem;
                   2229:                   TtaPreviousSibling(&prev);
1.197   ! kia      2230: 
        !          2231:                   if(createComp)
        !          2232:                     {
        !          2233:                       // Create a xt:component from selection and create a xt:use using it 
        !          2234:                       comp = Template_CreateComponentFromSelection(doc);
        !          2235:                       if(comp)
        !          2236:                         {
        !          2237:                           TtaExtendUndoSequence(doc);
        !          2238:                           
        !          2239:                           useType.ElSSchema = sstempl;
        !          2240:                           useType.ElTypeNum = Template_EL_useEl;
        !          2241:                           use = TtaNewElement(doc, useType);
        !          2242:                           if(prev)
        !          2243:                             TtaInsertSibling(use, prev, FALSE, doc);
        !          2244:                           else
        !          2245:                             TtaInsertFirstChild(&use, parent, doc);
        !          2246:                           
        !          2247:                           GiveAttributeStringValueFromNum(comp, Template_ATTR_name, buffer, &sz);
        !          2248:                           SetAttributeStringValue(use, Template_ATTR_types, buffer);
        !          2249:                           TtaRegisterElementCreate(use, doc);
        !          2250:                           
        !          2251:                           TtaSelectElement(doc, use);
        !          2252:                           TtaCloseUndoSequence(doc);
        !          2253:                         }
        !          2254:                     }
        !          2255:                   else
1.194     kia      2256:                     {
1.197   ! kia      2257:                       // Create a xt:use around the selection
        !          2258:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.194     kia      2259:                       
1.197   ! kia      2260:                       TtaRegisterElementDelete (selElem, doc);
        !          2261:                       TtaRemoveTree(selElem, doc);
        !          2262: 
1.194     kia      2263:                       useType.ElSSchema = sstempl;
1.197   ! kia      2264:                       useType.ElTypeNum = Template_EL_useSimple;
1.194     kia      2265:                       use = TtaNewElement(doc, useType);
                   2266:                       if(prev)
                   2267:                         TtaInsertSibling(use, prev, FALSE, doc);
                   2268:                       else
                   2269:                         TtaInsertFirstChild(&use, parent, doc);
                   2270:                       
1.197   ! kia      2271:                       TtaInsertFirstChild(&selElem, use, doc);
        !          2272:                       
        !          2273:                       SetAttributeStringValue(use, Template_ATTR_types, TtaGetElementTypeName(TtaGetElementType(selElem)));
        !          2274:                       
1.194     kia      2275:                       TtaRegisterElementCreate(use, doc);
                   2276:                       
                   2277:                       TtaCloseUndoSequence(doc);
                   2278:                     }
1.186     kia      2279:                 }
                   2280:             }
1.194     kia      2281:           else if(lastChar<firstChar)
                   2282:               Template_CreateInlineUse(doc);
                   2283:           
                   2284:           TtaSetStructureChecking (oldStructureChecking, doc);
                   2285:           TtaSetDisplayMode (doc, dispMode);
                   2286:           
1.186     kia      2287:         }
                   2288:     }
1.187     kia      2289:   return use;
                   2290: #else /* TEMPLATES */
                   2291:   return NULL;
1.186     kia      2292: #endif /* TEMPLATES */
                   2293: }
                   2294: 
                   2295: /*----------------------------------------------------------------------
1.187     kia      2296:   Template_CreateComponentFromSelection
1.186     kia      2297:   Create a xt:component with the selection and move it into the xt:head.
1.187     kia      2298:   Return the xt:component element.
1.186     kia      2299:   ----------------------------------------------------------------------*/
1.187     kia      2300: Element Template_CreateComponentFromSelection(Document doc)
1.186     kia      2301: {
                   2302: #ifdef TEMPLATES
1.187     kia      2303:   ThotBool    oldStructureChecking;
                   2304:   DisplayMode dispMode;
1.186     kia      2305:   Element     selElem, selElem2, parent, parent2, current, child, head;
                   2306:   ElementType selType, selType2;
                   2307:   int         firstChar, lastChar, firstChar2, lastChar2;
                   2308:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
1.192     kia      2309:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2310:   
1.186     kia      2311:   ElementType compType;
                   2312:   Element     comp = NULL;
                   2313:   char        buffer[128];
1.185     kia      2314: 
1.186     kia      2315:   const char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_NEWCOMP);
                   2316:   const char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
                   2317: 
1.192     kia      2318:   if(doc && t && TtaGetDocumentAccessMode(doc) &&
1.186     kia      2319:       TtaGetDocumentAccessMode(doc) && sstempl &&
                   2320:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2321:     {
                   2322:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2323:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   2324: 
                   2325:       if(selElem && selElem2)
                   2326:         {
                   2327:           selType =  TtaGetElementType(selElem);
                   2328:           selType2 =  TtaGetElementType(selElem2);
                   2329: 
                   2330:           QueryStringFromUser(label, title, buffer, 127);
1.190     kia      2331:           if(buffer[0]!=EOS)
1.186     kia      2332:             {
1.190     kia      2333:               head = TemplateFindHead(doc);
                   2334:               
                   2335:               compType.ElSSchema = sstempl;
                   2336:               compType.ElTypeNum = Template_EL_component;
                   2337:     
                   2338:               parent  = TtaGetParent(selElem);
                   2339:               parent2 = TtaGetParent(selElem2);
1.185     kia      2340:               
1.190     kia      2341:               if(head && firstChar==0 && firstChar2==0 && parent == parent2)
1.180     kia      2342:                 {
1.190     kia      2343:                   dispMode = TtaGetDisplayMode (doc);
                   2344:                   if (dispMode == DisplayImmediately)
                   2345:                     TtaSetDisplayMode (doc, DeferredDisplay);
                   2346:                   oldStructureChecking = TtaGetStructureChecking (doc);
                   2347:                   TtaSetStructureChecking (FALSE, doc);
                   2348:     
                   2349:                   child = TtaGetLastChild(head);
                   2350:                   TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2351:                   comp = TtaNewElement(doc, compType);
1.185     kia      2352:                   if(child)
1.190     kia      2353:                     TtaInsertSibling(comp, child, FALSE, doc);
1.185     kia      2354:                   else
1.190     kia      2355:                     TtaInsertFirstChild(&comp, head, doc);
                   2356:                   TtaRegisterElementCreate(comp, doc);
                   2357:                   
                   2358:                   TtaNextSibling(&selElem2);
                   2359:                   child = NULL;
                   2360:                   while(selElem!=selElem2)
                   2361:                     {
                   2362:                       current = selElem;
                   2363:                       TtaNextSibling(&selElem);
                   2364:                       
                   2365:                       TtaRegisterElementDelete (current, doc);
                   2366:                       TtaRemoveTree(current, doc);
                   2367:                       if(child)
                   2368:                         TtaInsertSibling(current, child, FALSE, doc);
                   2369:                       else
                   2370:                         TtaInsertFirstChild(&current, comp, doc);
                   2371:                       TtaRegisterElementDelete (current, doc);
                   2372:                       child = current;
                   2373:                     }
                   2374:                   
                   2375:                   SetAttributeStringValue(comp, Template_ATTR_name, buffer);
                   2376:                   TtaCloseUndoSequence(doc);
                   2377:                   TtaSelectElement(doc, comp);
                   2378:                   
1.192     kia      2379:                   Template_DeclareNewComponent(t, buffer, comp);
                   2380:                   
1.190     kia      2381:                   TtaSetStructureChecking (oldStructureChecking, doc);
                   2382:                   TtaSetDisplayMode (doc, dispMode);
1.180     kia      2383:                 }
                   2384:             }
                   2385:         }
                   2386:     }
1.186     kia      2387:   return comp;
1.187     kia      2388: #else /* TEMPLATES */
                   2389:   return NULL;
                   2390: #endif /* TEMPLATES */ 
1.180     kia      2391: }
1.187     kia      2392: 
1.192     kia      2393: /*----------------------------------------------------------------------
                   2394:   TemplateComponentWillBeDeleted
                   2395:   Processed when a component element will be deleted in a template context.
                   2396:   ----------------------------------------------------------------------*/
                   2397: ThotBool TemplateComponentWillBeDeleted (NotifyElement *event)
                   2398: {
                   2399: #ifdef TEMPLATES
                   2400:   XTigerTemplate t = GetXTigerDocTemplate(event->document);
                   2401:   char* elemName = GetAttributeStringValueFromNum(event->element, Template_ATTR_name, NULL);
                   2402:   
                   2403:   if(Template_IsUsedComponent(t, event->document, elemName))
                   2404:     {
                   2405:       TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_TEMPLATE_USEDCOMP_CANTREMOVE);
                   2406:       return TRUE;
                   2407:     }
                   2408: #endif /* TEMPLATES */ 
                   2409:   return FALSE;
                   2410: }
1.187     kia      2411: 
                   2412: 

Webmaster