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

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.206     kia         7: 
1.1       cvs         8: /*
1.51      francesc    9:  * Authors: Francesc Campoy Flores
1.202     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.206     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.206     kia        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.206     kia        97: #endif /* TEMPLATES */
1.171     kia        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;
1.206     kia       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;
1.206     kia       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);
1.206     kia       260: 
1.83      kia       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));
1.206     kia       268:     current = current->NextPath;
1.83      kia       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;
1.206     kia       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:   }
1.206     kia       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)
1.206     kia       324:                 *list = element;
1.129     vatton    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)
1.206     kia       344:             *list = element;
1.129     vatton    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.206     kia       418:   Load a template and create the instance file - update images and
1.108     vatton    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.206     kia       507: #endif /* TEMPLATES */
1.107     kia       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.206     kia       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);
1.206     kia       746:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL,
1.90      kia       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:   }
1.206     kia       758: 
1.90      kia       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.
1.206     kia       787: 
1.138     vatton    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);
1.206     kia       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: 
1.206     kia       862:                 /* Prepare insertion.*/
1.138     vatton    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.
1.206     kia       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: 
1.206     kia       977:                       /* Prepare insertion.*/
1.179     kia       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.206     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 */
1.206     kia      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.206     kia      1083: 
1.104     kia      1084:           /* Insert */
                   1085:           newEl = Template_InsertUseChildren(doc, el, decl);
                   1086:           for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
                   1087:           {
                   1088:             TtaRegisterElementCreate(child, doc);
                   1089:           }
1.206     kia      1090: 
1.104     kia      1091:           TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
                   1092:           TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
1.206     kia      1093: 
1.117     kia      1094:           /* xt:currentType attribute.*/
                   1095:           SetAttributeStringValueWithUndo(el, Template_ATTR_currentType, result);
1.206     kia      1096: 
1.104     kia      1097:           /* Finish insertion. */
                   1098:           TtaCloseUndoSequence(doc);
1.114     vatton   1099:           TtaSetDocumentModified (doc);
1.104     kia      1100:           TtaSetStructureChecking (oldStructureChecking, doc);
1.206     kia      1101: 
1.104     kia      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);
1.206     kia      1148: 
1.181     vatton   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.206     kia      1154: #endif /* TEMPLATES */
1.103     kia      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.206     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);
1.206     kia      1248: 
1.191     kia      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.206     kia      1265: #endif
1.191     kia      1266:       // Mark loaded
                   1267:       t->state |= templloaded;
                   1268:       TtaSetDocumentUnmodified (doc);
                   1269:       UpdateTemplateMenus (doc);
1.206     kia      1270: 
                   1271: #ifdef AMAYA_DEBUG
1.191     kia      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");
1.206     kia      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:               }
1.206     kia      1353: 
1.103     kia      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.206     kia      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.206     kia      1508: 
1.200     kia      1509:   if (elType.ElSSchema == templateSSchema && (
1.206     kia      1510:       elType.ElTypeNum == Template_EL_TEXT_UNIT ||
1.200     kia      1511:       elType.ElTypeNum == Template_EL_component))
1.115     kia      1512:   {
                   1513:     return FALSE;
                   1514:   }
1.206     kia      1515: 
1.113     kia      1516:   // Can not insert.
                   1517:   return TRUE;
1.101     kia      1518: #endif /* TEMPLATES*/
1.102     vatton   1519:   return FALSE;
1.101     kia      1520: }
                   1521: 
                   1522: /*----------------------------------------------------------------------
                   1523:   TemplateElementWillBeDeleted
                   1524:   Processed when an element will be deleted in a template context.
                   1525:   ----------------------------------------------------------------------*/
                   1526: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1527: {
1.107     kia      1528: #ifdef TEMPLATES
                   1529:   Document       doc = event->document;
                   1530:   Element        elem = event->element;
1.163     vatton   1531:   Element        xtElem, parent = NULL, sibling;
1.117     kia      1532:   ElementType    xtType, elType;
1.107     kia      1533:   char*          type;
                   1534:   Declaration    dec;
1.115     kia      1535:   SSchema        templateSSchema;
1.107     kia      1536:   XTigerTemplate t;
1.156     vatton   1537:   ThotBool       selparent = FALSE;
1.206     kia      1538: 
1.115     kia      1539:   if(event->info==1)
                   1540:     return FALSE;
                   1541: 
1.112     vatton   1542:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1543:     return TRUE;
                   1544: 
1.138     vatton   1545:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.107     kia      1546:   if (templateSSchema == NULL)
                   1547:     return FALSE; // let Thot do the job
1.206     kia      1548: 
1.192     kia      1549:   t = GetXTigerDocTemplate(doc);
                   1550:   if(Template_IsTemplate(t)||Template_IsLibrary(t))
                   1551:     return FALSE; // If template or library, pass to specialized functions.
1.122     kia      1552: 
1.107     kia      1553:   xtElem = GetFirstTemplateParentElement(elem);
1.112     vatton   1554:   if (xtElem)
1.107     kia      1555:   {
                   1556:     xtType = TtaGetElementType(xtElem);
1.206     kia      1557: 
                   1558: 
1.109     kia      1559: 
1.112     vatton   1560:     if (xtType.ElTypeNum==Template_EL_bag)
1.117     kia      1561:     {
                   1562:       elType = TtaGetElementType(elem);
                   1563:       if(elType.ElSSchema==templateSSchema &&
                   1564:         (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
                   1565:       {
                   1566:         // Remove element manually.
                   1567:         TtaOpenUndoSequence(doc, elem, elem, 0, 0);
                   1568:         TtaRegisterElementDelete(elem, doc);
                   1569:         TtaDeleteTree(elem, doc);
                   1570:         TtaCloseUndoSequence(doc);
                   1571:         return TRUE;
                   1572:       }
                   1573:       else
                   1574:         return FALSE; // xt:bag always allow remove children.
                   1575:     }
1.112     vatton   1576:     else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107     kia      1577:     {
1.109     kia      1578:       parent = TtaGetParent(elem);
1.117     kia      1579:       if (xtElem!=parent)
                   1580:       {
                   1581:         type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
                   1582:         dec = Template_GetDeclaration(t, type);
                   1583:         TtaFreeMemory(type);
1.206     kia      1584: 
1.117     kia      1585:         if (dec && dec->nature == XmlElementNat)
1.206     kia      1586:           return FALSE; // Can remove element only if in xt:use current type is base language element.
1.117     kia      1587:         else
                   1588:           return TRUE;
1.107     kia      1589:       }
1.109     kia      1590:     }
1.112     vatton   1591:     else if (xtType.ElTypeNum==Template_EL_repeat)
1.109     kia      1592:     {
1.156     vatton   1593:       sibling = TtaGetSuccessor (elem);
                   1594:       if (sibling == NULL)
                   1595:         {
                   1596:           // there is no next element
                   1597:           sibling = TtaGetPredecessor (elem);
                   1598:           if (sibling == NULL)
                   1599:             selparent = TRUE;
                   1600:         }
1.109     kia      1601:       TtaRegisterElementDelete(elem, doc);
                   1602:       TtaDeleteTree(elem, doc);
                   1603:       InstantiateRepeat(t, xtElem, doc, TRUE);
1.156     vatton   1604:       if (selparent)
                   1605:         // look for the new sibling
                   1606:         sibling = TtaGetFirstChild (parent);
                   1607:       if (sibling)
                   1608:         TtaSelectElement(doc, sibling);
1.151     quint    1609:       else
1.156     vatton   1610:         TtaSelectElement(doc, parent);
1.109     kia      1611:       return TRUE;
1.107     kia      1612:     }
                   1613:   }
1.206     kia      1614: 
1.109     kia      1615:   //TODO Test if current element is use or repeat.
                   1616:   // Because if an element is delete and it is the unique child of its parent,
1.206     kia      1617:   // the parent intends to destroy itself.
                   1618: 
1.107     kia      1619:   return TRUE;
                   1620: #else /* TEMPLATES */
1.101     kia      1621:   return FALSE;
1.107     kia      1622: #endif /* TEMPLATES */
1.101     kia      1623: }
                   1624: 
1.109     kia      1625: /*----------------------------------------------------------------------
                   1626:   CurrentTypeWillBeExported
                   1627:   Check if the xt:currentType attribute can be exported
                   1628:   ----------------------------------------------------------------------*/
                   1629: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
                   1630: {
                   1631: #ifdef TEMPLATES
1.110     kia      1632: 
1.112     vatton   1633:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1634:     return TRUE;
                   1635: 
1.112     vatton   1636:   if (IsTemplateDocument(event->document))
1.109     kia      1637:     return TRUE;
                   1638: #endif /* TEMPLATES */
                   1639:   return FALSE;
                   1640: }
1.127     kia      1641: 
                   1642: /*----------------------------------------------------------------------
                   1643:   TemplateAttrInMenu
                   1644:   Called by Thot when building the Attributes menu for template elements.
                   1645:   ----------------------------------------------------------------------*/
                   1646: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
                   1647: {
                   1648: #ifdef TEMPLATES
                   1649:   // Prevent from showing attributes for template instance but not templates.
                   1650:   if(IsTemplateInstanceDocument(event->document))
                   1651:     return TRUE;
                   1652:   else
                   1653: #endif /* TEMPLATES */
                   1654:     return FALSE;
                   1655: }
1.160     kia      1656: 
1.167     kia      1657: /*----------------------------------------------------------------------
1.168     kia      1658:   CreateTemplateFromDocument
                   1659:   Create a template from the current document.
1.167     kia      1660:   ----------------------------------------------------------------------*/
                   1661: void CreateTemplateFromDocument(Document doc, View view)
                   1662: {
1.171     kia      1663: #ifdef TEMPLATES
1.205     vatton   1664:   char     buffer[MAX_LENGTH], suffix[10];
1.196     vatton   1665: 
1.204     vatton   1666:   if (IsW3Path (DocumentURLs[doc]) &&
1.205     vatton   1667:       DocumentMeta[doc] && DocumentMeta[doc]->full_content_location)
                   1668:     {
1.196     vatton   1669:     // use the location instead of the current URI
1.206     kia      1670: 
1.205     vatton   1671:     strcpy (buffer, DocumentMeta[doc]->full_content_location);
                   1672:     }
1.204     vatton   1673:   else
                   1674:     strcpy (buffer, DocumentURLs[doc]);
                   1675: 
                   1676:   // remove the current suffix
                   1677:   TtaExtractSuffix (buffer, suffix);
1.196     vatton   1678:   // the new suffix
1.168     kia      1679:   strcat(buffer, ".xtd");
                   1680:   DontReplaceOldDoc = TRUE;
                   1681:   CreateTemplate(doc, buffer);
1.204     vatton   1682:   // by default .xtd files are xml files
                   1683:   TtaSetDocumentCharset (doc, UTF_8, FALSE);
1.171     kia      1684: #endif /* TEMPLATES */
1.167     kia      1685: }
                   1686: 
1.169     kia      1687: /*----------------------------------------------------------------------
                   1688:   UpdateTemplateMenus
                   1689:   ----------------------------------------------------------------------*/
                   1690: void UpdateTemplateMenus (Document doc)
                   1691: {
1.196     vatton   1692:   if (IsTemplateInstanceDocument(doc))
1.180     kia      1693:     {
                   1694:       // Instance document
                   1695:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1696:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1697:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1698:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1699:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1700:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180     kia      1701:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196     vatton   1702:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1703:     }
1.183     vatton   1704:   else if (DocumentURLs[doc] && IsXTiger (DocumentURLs[doc]))
1.180     kia      1705:     {
                   1706:       // Template document
                   1707:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1708:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1709:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1710:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1711:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeat);
                   1712:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeatComp);
1.206     kia      1713:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateFreeBox);
1.196     vatton   1714:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1715:     }
1.169     kia      1716:   else
1.180     kia      1717:     {
                   1718:       //Standard document
                   1719:       TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
                   1720:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1721:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1722:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1723:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1724:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180     kia      1725:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196     vatton   1726:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1727:     }
1.169     kia      1728: }
1.171     kia      1729: 
                   1730: /*----------------------------------------------------------------------
                   1731:   UninstanciateTemplateDocument
                   1732:   An instance of a template is tranformed into a template-less docuemnt.
                   1733:   Remove link between XTigerTemplate structure and document.
                   1734:   ----------------------------------------------------------------------*/
                   1735: void UninstanciateTemplateDocument(Document doc)
                   1736: {
                   1737: #ifdef TEMPLATES
                   1738:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1739:   if(t)
                   1740:     Template_Close(t);
1.206     kia      1741: #endif /* TEMPLATES */
1.171     kia      1742: }
                   1743: 
                   1744: 
                   1745: /*----------------------------------------------------------------------
                   1746:   Template_PrepareInstance
                   1747:   Allocate XTigerTemplate structure for instance and initialize template
                   1748:   url and template version.
                   1749:   ----------------------------------------------------------------------*/
                   1750: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
                   1751: {
                   1752: #ifdef TEMPLATES
                   1753:   XTigerTemplate t = GetXTigerTemplate(fileName);
                   1754:   if(!t)
                   1755:     t = NewXTigerTemplate(fileName);
                   1756:   t->state           = templInstance;
                   1757:   t->templateVersion = template_version;
                   1758:   t->base_uri        = template_url;
                   1759:   t->doc             = doc;
1.178     kia      1760:   t->ref             = 1;
1.206     kia      1761: 
1.171     kia      1762: #endif /* TEMPLATES */
                   1763: }
                   1764: 
                   1765: 
                   1766: /*----------------------------------------------------------------------
                   1767:   SetDocumentAsXTigerTemplate
                   1768:   Set the document template structure as template.
                   1769:   ----------------------------------------------------------------------*/
                   1770: void SetDocumentAsXTigerTemplate(Document doc)
                   1771: {
                   1772: #ifdef TEMPLATES
                   1773:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1774:   if(t)
                   1775:     t->state |= templTemplate;
1.206     kia      1776: #endif /* TEMPLATES */
1.171     kia      1777: }
                   1778: 
                   1779: /*----------------------------------------------------------------------
                   1780:   SetDocumentAsXTigerLibrary
                   1781:   Set the document template structure as template library.
                   1782:   ----------------------------------------------------------------------*/
                   1783: void SetDocumentAsXTigerLibrary(Document doc)
                   1784: {
                   1785: #ifdef TEMPLATES
                   1786:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1787:   if(t)
                   1788:     t->state |= templLibrary;
1.206     kia      1789: #endif /* TEMPLATES */
1.171     kia      1790: }
1.174     kia      1791: 
                   1792: 
                   1793: /*----------------------------------------------------------------------
                   1794:   TemplateCreateTextBox
                   1795:   Create a xt:use types="string" box around the selection.
                   1796:   ----------------------------------------------------------------------*/
                   1797: void TemplateCreateTextBox(Document doc, View view)
                   1798: {
                   1799: #ifdef TEMPLATES
                   1800:   Element     selElem;
                   1801:   ElementType selType;
                   1802:   int         firstChar, lastChar;
                   1803:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1804:   ElementType useType;
                   1805:   Element     use;
                   1806:   char        buffer[128];
1.189     kia      1807:   ThotBool    oldStructureChecking;
1.206     kia      1808: 
1.174     kia      1809:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
1.184     vatton   1810:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174     kia      1811: 
                   1812:   if (!TtaGetDocumentAccessMode(doc))
                   1813:     return;
1.206     kia      1814: 
1.174     kia      1815:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1816:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1817:     {
                   1818:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1819:       if(selElem)
                   1820:         {
                   1821:           selType =  TtaGetElementType(selElem);
                   1822:           if(!TtaIsLeaf(selType))
                   1823:             {
                   1824:               selElem = TtaGetFirstLeaf(selElem);
                   1825:               selType = TtaGetElementType(selElem);
                   1826:               firstChar = lastChar = 0;
                   1827:             }
1.207   ! vatton   1828:           if (selType.ElTypeNum == 1)
1.174     kia      1829:             {
1.207   ! vatton   1830:               // request the element label
1.174     kia      1831:               QueryStringFromUser(label, title, buffer, 127);
1.207   ! vatton   1832:               if (buffer[0] == EOS)
        !          1833:                 return;
        !          1834: 
1.174     kia      1835:               useType.ElSSchema = sstempl;
                   1836:               useType.ElTypeNum = Template_EL_useSimple;
1.189     kia      1837:               oldStructureChecking = TtaGetStructureChecking (doc);
                   1838:               TtaSetStructureChecking (FALSE, doc);
                   1839: 
1.174     kia      1840:               if(firstChar==0)
                   1841:                 {
                   1842:                   use = TtaNewElement(doc, useType);
                   1843:                   if(use)
                   1844:                     {
                   1845:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1846:                       TtaInsertSibling(use, selElem, FALSE, doc);
                   1847:                       TtaRegisterElementCreate(use, doc);
                   1848:                       TtaRegisterElementDelete (selElem, doc);
                   1849:                       TtaRemoveTree(selElem, doc);
                   1850:                       TtaInsertFirstChild(&selElem, use, doc);
                   1851:                       TtaRegisterElementDelete (selElem, doc);
                   1852:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1853:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);
                   1854:                       TtaCloseUndoSequence(doc);
                   1855:                       TtaSelectElement(doc, use);
                   1856:                     }
                   1857:                 }
                   1858:               else
                   1859:                 {
                   1860:                   GenerateInlineElement(Template_EL_useSimple, sstempl, 0, "", TRUE);
                   1861:                   TtaGiveFirstSelectedElement(doc, &use, &firstChar, &lastChar);
                   1862:                   selType =  TtaGetElementType(use);
                   1863:                   if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_useSimple)
                   1864:                     {
                   1865:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
1.206     kia      1866:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);
1.174     kia      1867:                     }
                   1868:                 }
1.206     kia      1869: 
1.189     kia      1870:               TtaSetStructureChecking (oldStructureChecking, doc);
1.206     kia      1871: 
1.174     kia      1872:             }
1.206     kia      1873: 
1.174     kia      1874:         }
                   1875:     }
                   1876: #endif /* TEMPLATES */
                   1877: }
                   1878: 
                   1879: 
                   1880: /*----------------------------------------------------------------------
                   1881:   TemplateCreateFreeBox
                   1882:   Create a xt:bag types="string" box around the selection.
                   1883:   ----------------------------------------------------------------------*/
                   1884: void TemplateCreateFreeBox(Document doc, View view)
                   1885: {
                   1886: #ifdef TEMPLATES
1.185     kia      1887:   Element     selElem, selElem2, parent, parent2, current, child = NULL;
1.174     kia      1888:   ElementType selType, selType2;
                   1889:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1890:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1891: 
                   1892:   ElementType bagType;
                   1893:   Element     bag;
                   1894:   char        buffer[128];
                   1895: 
                   1896:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
1.184     vatton   1897:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174     kia      1898: 
                   1899:   if (!TtaGetDocumentAccessMode(doc))
                   1900:     return;
1.206     kia      1901: 
1.174     kia      1902:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1903:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1904:     {
                   1905:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1906:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1.207   ! vatton   1907:       if (selElem && selElem2)
        !          1908:         {
        !          1909:           // request the element label
        !          1910:           QueryStringFromUser(label, title, buffer, 127);
        !          1911:           if (buffer[0] == EOS)
        !          1912:             return;
1.206     kia      1913: 
1.174     kia      1914:           selType =  TtaGetElementType(selElem);
                   1915:           selType2 =  TtaGetElementType(selElem2);
                   1916:           bagType.ElSSchema = sstempl;
                   1917:           bagType.ElTypeNum = Template_EL_bag;
1.185     kia      1918:           parent  = TtaGetParent(selElem);
                   1919:           parent2 = TtaGetParent(selElem2);
1.206     kia      1920: 
1.185     kia      1921:           if(firstChar==0 && firstChar2==0 && parent == parent2)
1.174     kia      1922:             {
1.185     kia      1923:               ThotBool        oldStructureChecking;
                   1924:               DisplayMode     dispMode;
                   1925:               dispMode = TtaGetDisplayMode (doc);
                   1926:               if (dispMode == DisplayImmediately)
                   1927:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   1928:               oldStructureChecking = TtaGetStructureChecking (doc);
                   1929:               TtaSetStructureChecking (FALSE, doc);
                   1930: 
                   1931:               // Create and insert xt:bag element
                   1932:               bag = TtaNewElement(doc, bagType);
                   1933:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1934:               TtaInsertSibling(bag, selElem, TRUE, doc);
                   1935:               TtaRegisterElementCreate(bag, doc);
                   1936: 
                   1937:               TtaNextSibling(&selElem2);
                   1938:               while(selElem!=selElem2)
1.174     kia      1939:                 {
1.185     kia      1940:                   current = selElem;
                   1941:                   TtaNextSibling(&selElem);
1.206     kia      1942: 
1.185     kia      1943:                   TtaRegisterElementDelete (current, doc);
                   1944:                   TtaRemoveTree(current, doc);
                   1945:                   if(child)
                   1946:                     TtaInsertSibling(current, child, FALSE, doc);
                   1947:                   else
                   1948:                     TtaInsertFirstChild(&current, bag, doc);
                   1949:                   TtaRegisterElementDelete (current, doc);
                   1950:                   child = current;
1.174     kia      1951:                 }
1.206     kia      1952: 
1.185     kia      1953:               SetAttributeStringValue(bag, Template_ATTR_types, "any");
                   1954:               SetAttributeStringValue(bag, Template_ATTR_title, buffer);
                   1955:               TtaCloseUndoSequence(doc);
                   1956:               TtaSelectElement(doc, bag);
1.206     kia      1957: 
1.185     kia      1958:               TtaSetStructureChecking (oldStructureChecking, doc);
                   1959:               TtaSetDisplayMode (doc, dispMode);
1.174     kia      1960:             }
                   1961:         }
                   1962:     }
                   1963: #endif /* TEMPLATES */
                   1964: }
                   1965: 
1.180     kia      1966: /*----------------------------------------------------------------------
1.198     kia      1967:   TemplateCreateUnion
                   1968:   Create a xt:union around the selection.
                   1969:   ----------------------------------------------------------------------*/
                   1970: void TemplateCreateUnion(Document doc, View view)
                   1971: {
                   1972: #ifdef TEMPLATES
1.199     kia      1973:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   1974:   Element        head, sibling, unionEl;
                   1975:   ElementType    unionType;
                   1976:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1977:   char          *proposed, *name = NULL, *types=NULL;
1.200     kia      1978: 
                   1979:   Element     selElem;
                   1980:   ElementType selType;
                   1981:   int         firstChar, lastChar;
1.206     kia      1982: 
                   1983: 
1.199     kia      1984:   if(t && sstempl)
                   1985:     {
1.200     kia      1986:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1987:       selType =  TtaGetElementType(selElem);
                   1988:       if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_union)
                   1989:         Template_ModifyUnionElement(doc, selElem);
                   1990:       else
1.199     kia      1991:         {
1.200     kia      1992:           proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
                   1993:           if(QueryUnionFromUser(proposed, NULL, &name, &types, TRUE))
                   1994:             {
                   1995:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.206     kia      1996: 
1.200     kia      1997:               head = TemplateFindHead(doc);
                   1998:               sibling = TtaGetLastChild(head);
1.206     kia      1999: 
1.200     kia      2000:               unionType.ElSSchema = sstempl;
                   2001:               unionType.ElTypeNum = Template_EL_union;
                   2002:               unionEl = TtaNewElement(doc, unionType);
1.206     kia      2003: 
1.200     kia      2004:               if(sibling)
                   2005:                 TtaInsertSibling(unionEl, sibling, FALSE, doc);
                   2006:               else
                   2007:                 {
                   2008:                   sibling = unionEl;
                   2009:                   TtaInsertFirstChild(&sibling, head, doc);
                   2010:                 }
                   2011:               Template_SetName(doc, unionEl, name, TRUE);
1.206     kia      2012: 
1.200     kia      2013:               SetAttributeStringValue(unionEl, Template_ATTR_includeAt, types);
                   2014:               TtaSetAccessRight(unionEl, ReadOnly, doc);
1.206     kia      2015: 
1.200     kia      2016:               TtaRegisterElementCreate(unionEl, doc);
1.206     kia      2017: 
1.200     kia      2018:               TtaSelectElement(doc, unionEl);
                   2019:               TtaCloseUndoSequence(doc);
1.206     kia      2020: 
1.200     kia      2021:               Template_DeclareNewUnion (t, name, types, "");
                   2022:               TtaFreeMemory(proposed);
                   2023:               TtaFreeMemory(name);
                   2024:               TtaFreeMemory(types);
1.206     kia      2025:             }
1.200     kia      2026:         }
1.206     kia      2027:     }
1.199     kia      2028: 
1.200     kia      2029: #endif /* TEMPLATES */
                   2030: }
1.199     kia      2031: 
1.200     kia      2032: /*----------------------------------------------------------------------
                   2033:   Template_ModifyUnionElement
                   2034:   Query the user to modify an xt:union
                   2035:   ----------------------------------------------------------------------*/
                   2036: void Template_ModifyUnionElement(Document doc, Element unionEl)
                   2037: {
                   2038:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2039:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   2040:   ElementType    unionType;
                   2041:   char          *proposed, *checked, *name, *types=NULL;
1.206     kia      2042: 
1.200     kia      2043:   if(doc && unionEl && t && sstempl)
                   2044:     {
                   2045:       unionType = TtaGetElementType(unionEl);
                   2046:       if(unionType.ElSSchema==sstempl && unionType.ElTypeNum==Template_EL_union)
                   2047:         {
                   2048:           proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
                   2049:           checked  = GetAttributeStringValueFromNum(unionEl, Template_ATTR_includeAt, NULL);
                   2050:           name     = GetAttributeStringValueFromNum(unionEl, Template_ATTR_name, NULL);
                   2051:           if(QueryUnionFromUser(proposed, checked, &name, &types, FALSE))
1.199     kia      2052:             {
1.200     kia      2053:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2054:               SetAttributeStringValueWithUndo(unionEl, Template_ATTR_includeAt, types);
                   2055:               TtaCloseUndoSequence(doc);
1.199     kia      2056:             }
1.200     kia      2057:           TtaFreeMemory(proposed);
                   2058:           TtaFreeMemory(checked);
                   2059:           TtaFreeMemory(name);
                   2060:           TtaFreeMemory(types);
1.199     kia      2061:         }
1.200     kia      2062:     }
1.198     kia      2063: }
                   2064: 
                   2065: /*----------------------------------------------------------------------
1.180     kia      2066:   TemplateCreateRepeat
                   2067:   Create a xt:repeat around the selection.
                   2068:   ----------------------------------------------------------------------*/
                   2069: void TemplateCreateRepeat(Document doc, View view)
                   2070: {
1.198     kia      2071:   Template_CreateRepeatFromSelection(doc, FALSE);
                   2072: }
                   2073: 
                   2074: /*----------------------------------------------------------------------
                   2075:   TemplateCreateRepeatComp
                   2076:   Create a xt:component with the selection and use it in a new xt:repeat
                   2077:   ----------------------------------------------------------------------*/
                   2078: void TemplateCreateRepeatComp(Document doc, View view)
                   2079: {
                   2080:   Template_CreateRepeatFromSelection(doc, TRUE);
                   2081: }
                   2082: 
                   2083: /*----------------------------------------------------------------------
                   2084:   Template_CreateRepeatFromSelection
                   2085:   Create a xt:repeat with the selection.
                   2086:   If selection is empty, insert an inline xt:use.
1.206     kia      2087:   If createComp is false, create a xt:use with types at the selected block.
1.198     kia      2088:   If createComp is true, create a component with the selection.
                   2089:   Return the xt:use element.
                   2090:   ----------------------------------------------------------------------*/
                   2091: Element Template_CreateRepeatFromSelection(Document doc, ThotBool createComp)
                   2092: {
1.180     kia      2093: #ifdef TEMPLATES
1.187     kia      2094:   ThotBool    oldStructureChecking;
                   2095:   DisplayMode dispMode;
                   2096:   Element     selElem, selElem2, parent, parent2;
                   2097:   ElementType selType, selType2;
1.180     kia      2098:   int         firstChar, lastChar, firstChar2, lastChar2;
                   2099:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
1.197     kia      2100:   const char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATCOMP);
                   2101:   const char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATLABEL);
                   2102:   char        buffer[128];
1.180     kia      2103: 
1.186     kia      2104:   ElementType repType;
1.187     kia      2105:   Element     rep, use;
1.180     kia      2106: 
                   2107:   if (!TtaGetDocumentAccessMode(doc))
1.198     kia      2108:     return NULL;
1.206     kia      2109: 
1.180     kia      2110:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   2111:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2112:     {
                   2113:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2114:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1.185     kia      2115: 
1.180     kia      2116:       if(selElem && selElem2)
                   2117:         {
1.207   ! vatton   2118:           // request the element label
        !          2119:           QueryStringFromUser(label, title, buffer, 127);
        !          2120:           if (buffer[0] == EOS)
        !          2121:             return NULL;
        !          2122: 
1.180     kia      2123:           selType =  TtaGetElementType(selElem);
                   2124:           selType2 =  TtaGetElementType(selElem2);
                   2125:           repType.ElSSchema = sstempl;
1.207   ! vatton   2126:           repType.ElTypeNum = Template_EL_repeat;          
1.185     kia      2127:           parent  = TtaGetParent(selElem);
                   2128:           parent2 = TtaGetParent(selElem2);
1.197     kia      2129:           if(firstChar==0 && firstChar2==0 && parent == parent2 && buffer[0]!=0)
1.180     kia      2130:             {
1.187     kia      2131:               dispMode = TtaGetDisplayMode (doc);
                   2132:               if (dispMode == DisplayImmediately)
                   2133:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   2134:               oldStructureChecking = TtaGetStructureChecking (doc);
                   2135:               TtaSetStructureChecking (FALSE, doc);
1.206     kia      2136: 
                   2137:               if(selElem==selElem2 && selType.ElSSchema==sstempl &&
                   2138:                   (selType.ElTypeNum==Template_EL_useEl ||
1.187     kia      2139:                    selType.ElTypeNum==Template_EL_useSimple))
                   2140:                 use = selElem;
                   2141:               else
1.198     kia      2142:                 use = Template_CreateUseFromSelection(doc, createComp);
1.187     kia      2143:               if(use)
                   2144:                 {
                   2145:                   TtaExtendUndoSequence(doc);
1.206     kia      2146: 
1.187     kia      2147:                   rep = TtaNewElement(doc, repType);
1.206     kia      2148: 
1.187     kia      2149:                   TtaInsertSibling(rep, use, FALSE, doc);
1.197     kia      2150:                   SetAttributeStringValue(rep, Template_ATTR_title, buffer);
1.187     kia      2151:                   TtaRegisterElementCreate(rep, doc);
1.206     kia      2152: 
1.187     kia      2153:                   TtaRegisterElementDelete (use, doc);
                   2154:                   TtaRemoveTree(use, doc);
1.206     kia      2155: 
1.187     kia      2156:                   TtaInsertFirstChild(&use, rep, doc);
                   2157:                   TtaRegisterElementCreate(use, doc);
1.206     kia      2158: 
1.187     kia      2159:                   TtaSelectElement(doc, use);
                   2160:                   TtaCloseUndoSequence(doc);
                   2161:                 }
1.206     kia      2162: 
1.187     kia      2163:               TtaSetStructureChecking (oldStructureChecking, doc);
                   2164:               TtaSetDisplayMode (doc, dispMode);
1.198     kia      2165:               return rep;
1.187     kia      2166:             }
                   2167:         }
                   2168:     }
                   2169: #endif /* TEMPLATES */
1.198     kia      2170:   return NULL;
1.195     kia      2171: }
                   2172: 
1.187     kia      2173: /*----------------------------------------------------------------------
                   2174:   TemplateCreateUseBox
                   2175:   Create a xt:use around the selection.
                   2176:   ----------------------------------------------------------------------*/
                   2177: void TemplateCreateUseBox(Document doc, View view)
                   2178: {
                   2179: #ifdef TEMPLATES
1.197     kia      2180:   Template_CreateUseFromSelection(doc, FALSE);
1.187     kia      2181: #endif /* TEMPLATES */
                   2182: }
                   2183: 
1.194     kia      2184: /*----------------------------------------------------------------------
1.195     kia      2185:   TemplateCreateUseCompBox
                   2186:   Create a xt:use around the selection.
                   2187:   ----------------------------------------------------------------------*/
                   2188: void TemplateCreateUseCompBox(Document doc, View view)
                   2189: {
                   2190: #ifdef TEMPLATES
1.197     kia      2191:   Template_CreateUseFromSelection(doc, TRUE);
1.195     kia      2192: #endif /* TEMPLATES */
                   2193: }
                   2194: 
                   2195: /*----------------------------------------------------------------------
1.194     kia      2196:   Template_CreateInlineUse
                   2197:   Create an inline xt:use with the selection.
                   2198:   ----------------------------------------------------------------------*/
1.206     kia      2199: static Element Template_CreateInlineUse(Document doc)
1.194     kia      2200: {
                   2201: #ifdef TEMPLATES
                   2202:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   2203:   int            firstChar, lastChar;
1.206     kia      2204:   Element        selElem, use = NULL;
1.194     kia      2205:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2206:   char          *proposed, *label = NULL, *types=NULL;
1.206     kia      2207:   ThotBool       option;
                   2208: 
1.194     kia      2209:   if(t && sstempl)
                   2210:     {
                   2211:       proposed = Template_GetInlineLevelDeclarations(t, TRUE, TRUE);
                   2212:       if(QueryNewUseFromUser(proposed, &label, &types, &option))
                   2213:         {
                   2214:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2215: 
                   2216:           GenerateInlineElement(Template_EL_useEl, sstempl, 0, NULL, TRUE);
1.206     kia      2217: 
1.194     kia      2218:           TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.206     kia      2219: 
1.197     kia      2220:           use = selElem;
                   2221:           TtaNextSibling(&use);
                   2222: 
1.194     kia      2223:           SetAttributeStringValue(use, Template_ATTR_title, label);
                   2224:           SetAttributeStringValue(use, Template_ATTR_types, types);
1.206     kia      2225: 
1.194     kia      2226:           TtaSelectElement(doc, use);
                   2227:           TtaCloseUndoSequence(doc);
                   2228:         }
1.206     kia      2229: 
1.194     kia      2230:       TtaFreeMemory(proposed);
                   2231:       TtaFreeMemory(label);
                   2232:       TtaFreeMemory(types);
1.206     kia      2233:       return use;
                   2234:     }
                   2235: 
1.194     kia      2236: #endif /* TEMPLATES */
                   2237:   return FALSE;
1.206     kia      2238: 
1.194     kia      2239: }
                   2240: 
                   2241: /*----------------------------------------------------------------------
                   2242:   Template_CreateEmptyBlockUse
                   2243:   Create a block xt:use with the selection.
                   2244:   ----------------------------------------------------------------------*/
1.206     kia      2245: static Element Template_CreateEmptyBlockUse(Document doc)
1.194     kia      2246: {
                   2247: #ifdef TEMPLATES
                   2248:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   2249:   ElementType    useType;
                   2250:   int            firstChar, lastChar;
                   2251:   Element        selElem, use;
                   2252:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2253:   char          *proposed, *label = NULL, *types=NULL;
                   2254:   ThotBool       option, res = FALSE;
1.206     kia      2255: 
                   2256: 
1.194     kia      2257:   if(t && sstempl)
                   2258:     {
                   2259:       proposed = Template_GetBlockLevelDeclarations(t, TRUE);
                   2260:       if(QueryNewUseFromUser(proposed, &label, &types, &option))
                   2261:         {
                   2262:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2263: 
                   2264:           TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2265:           useType.ElSSchema = sstempl;
                   2266:           useType.ElTypeNum = Template_EL_useEl;
                   2267:           use = TtaNewElement(doc, useType);
1.206     kia      2268: 
1.194     kia      2269:           TtaInsertSibling(use, selElem, FALSE, doc);
                   2270:           TtaDeleteTree(selElem, doc);
1.206     kia      2271: 
1.194     kia      2272:           SetAttributeStringValue(use, Template_ATTR_title, label);
                   2273:           SetAttributeStringValue(use, Template_ATTR_types, types);
                   2274:           TtaRegisterElementCreate(use, doc);
                   2275:           res = TRUE;
1.206     kia      2276: 
1.194     kia      2277:           TtaSelectElement(doc, use);
                   2278:           TtaCloseUndoSequence(doc);
                   2279: 
                   2280:         }
1.206     kia      2281: 
1.194     kia      2282:       TtaFreeMemory(proposed);
                   2283:       TtaFreeMemory(label);
                   2284:       TtaFreeMemory(types);
1.206     kia      2285:       return use;
                   2286:     }
                   2287: 
1.194     kia      2288: #endif /* TEMPLATES */
                   2289:   return FALSE;
                   2290: }
1.187     kia      2291: 
                   2292: /*----------------------------------------------------------------------
                   2293:   Template_CreateUseFromSelection
                   2294:   Create a xt:use with the selection.
1.197     kia      2295:   If selection is empty, insert an inline xt:use.
1.206     kia      2296:   If createComp is false, create a xt:use with types at the selected block.
1.197     kia      2297:   If createComp is true, create a component with the selection.
                   2298:   Return the xt:use element.
1.187     kia      2299:   ----------------------------------------------------------------------*/
1.197     kia      2300: Element Template_CreateUseFromSelection(Document doc, ThotBool createComp)
1.187     kia      2301: {
                   2302: #ifdef TEMPLATES
1.206     kia      2303:   ThotBool    oldStructureChecking, option;
1.187     kia      2304:   DisplayMode dispMode;
                   2305:   Element     selElem, selElem2, parent, parent2;
                   2306:   ElementType selType, selType2;
                   2307:   int         firstChar, lastChar, firstChar2, lastChar2;
1.194     kia      2308:   SSchema     sstempl = TtaGetSSchema ("Template", doc),
                   2309:               sshtml  = TtaGetSSchema ("HTML", doc);
1.206     kia      2310:   Attribute   optionAttr;
                   2311:   XTigerTemplate t;
1.187     kia      2312: 
                   2313:   ElementType useType;
                   2314:   Element     use = NULL, comp, prev;
1.206     kia      2315:   char       *proposed, *checked, *types=NULL;
1.187     kia      2316:   char        buffer[128];
                   2317:   int         sz = 128;
                   2318: 
                   2319:   if (!TtaGetDocumentAccessMode(doc))
                   2320:     return NULL;
1.206     kia      2321: 
1.187     kia      2322:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   2323:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2324:     {
                   2325:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2326:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   2327: 
1.206     kia      2328: //      printf(">> %p %d %d %s (%d)\n", selElem, firstChar, lastChar, TtaGetElementTypeName(TtaGetElementType(selElem)), TtaGetElementType(selElem).ElTypeNum);
                   2329: //      printf(" > %p %d %d %s (%d)\n", selElem2, firstChar2, lastChar2, TtaGetElementTypeName(TtaGetElementType(selElem2)), TtaGetElementType(selElem2).ElTypeNum);
                   2330: 
                   2331:       t = GetXTigerDocTemplate(doc);
                   2332: 
                   2333:       if(selElem && selElem2 && t)
1.187     kia      2334:         {
                   2335:           selType =  TtaGetElementType(selElem);
                   2336:           selType2 =  TtaGetElementType(selElem2);
                   2337:           parent  = TtaGetParent(selElem);
                   2338:           parent2 = TtaGetParent(selElem2);
                   2339: 
1.194     kia      2340:           dispMode = TtaGetDisplayMode (doc);
                   2341:           if (dispMode == DisplayImmediately)
                   2342:             TtaSetDisplayMode (doc, DeferredDisplay);
                   2343:           oldStructureChecking = TtaGetStructureChecking (doc);
                   2344:           TtaSetStructureChecking (FALSE, doc);
1.206     kia      2345: 
1.187     kia      2346:           if(firstChar==0 && firstChar2==0 && parent == parent2)
                   2347:             {
1.206     kia      2348:               if(selType.ElSSchema==sstempl)
                   2349:                 {
                   2350:                   // The selection is a template element.
                   2351:                   switch(selType.ElTypeNum)
                   2352:                   {
                   2353:                     case Template_EL_useEl:
                   2354:                     case Template_EL_useSimple:
                   2355:                       // TODO query to add/remove some types
                   2356:                       if(TtaGetElementVolume(selElem)==0)
                   2357:                         {
                   2358:                           proposed = Template_GetAllDeclarations(t, TRUE, TRUE, TRUE);
                   2359:                           checked  = GetAttributeStringValueFromNum(selElem, Template_ATTR_types, NULL);
                   2360:                           option   = GetAttributeIntValueFromNum(selElem, Template_ATTR_option)==Template_ATTR_option_VAL_option_set;
                   2361:                           if(QueryUseFromUser(proposed, checked, buffer, 128, &types, &option))
                   2362:                             {
                   2363:                               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2364:                               SetAttributeStringValueWithUndo(selElem, Template_ATTR_types, types);
                   2365:                               SetAttributeStringValueWithUndo(selElem, Template_ATTR_title, buffer);
                   2366:                               if(option)
                   2367:                                 SetAttributeIntValue(selElem, Template_ATTR_option, Template_ATTR_option_VAL_option_set, TRUE);
                   2368:                               else
                   2369:                                 SetAttributeIntValue(selElem, Template_ATTR_option, Template_ATTR_option_VAL_option_unset, TRUE);
                   2370: 
                   2371:                               TtaCloseUndoSequence(doc);
                   2372:                             }
                   2373:                           TtaFreeMemory(proposed);
                   2374:                           TtaFreeMemory(checked);
                   2375:                           TtaFreeMemory(types);
                   2376:                           createComp = FALSE;
                   2377:                         }
                   2378:                       break;
                   2379:                     default:
                   2380:                       // Do nothing
                   2381:                       break;
                   2382:                   }
                   2383:                 }
                   2384:               else if(selType.ElSSchema==sshtml && (selType.ElTypeNum==HTML_EL_Element ||
                   2385:                   selType.ElTypeNum==HTML_EL_Basic_Elem ||
                   2386:                   (selType.ElTypeNum==HTML_EL_Paragraph && TtaGetElementVolume(selElem)==0)))
                   2387:                 {
                   2388:                   // Create a block-level xt:use instead of selection.
                   2389: 
                   2390:                   use = Template_CreateEmptyBlockUse(doc);
                   2391:                 }
1.194     kia      2392:               else
1.186     kia      2393:                 {
1.194     kia      2394:                   prev = selElem;
                   2395:                   TtaPreviousSibling(&prev);
1.197     kia      2396: 
                   2397:                   if(createComp)
                   2398:                     {
1.206     kia      2399:                       // Create a xt:component from selection and create a xt:use using it
1.197     kia      2400:                       comp = Template_CreateComponentFromSelection(doc);
                   2401:                       if(comp)
                   2402:                         {
                   2403:                           TtaExtendUndoSequence(doc);
1.206     kia      2404: 
1.197     kia      2405:                           useType.ElSSchema = sstempl;
                   2406:                           useType.ElTypeNum = Template_EL_useEl;
                   2407:                           use = TtaNewElement(doc, useType);
                   2408:                           if(prev)
                   2409:                             TtaInsertSibling(use, prev, FALSE, doc);
                   2410:                           else
                   2411:                             TtaInsertFirstChild(&use, parent, doc);
1.206     kia      2412: 
1.197     kia      2413:                           GiveAttributeStringValueFromNum(comp, Template_ATTR_name, buffer, &sz);
                   2414:                           SetAttributeStringValue(use, Template_ATTR_types, buffer);
                   2415:                           TtaRegisterElementCreate(use, doc);
1.206     kia      2416: 
1.197     kia      2417:                           TtaSelectElement(doc, use);
1.206     kia      2418: 
1.197     kia      2419:                           TtaCloseUndoSequence(doc);
                   2420:                         }
                   2421:                     }
                   2422:                   else
1.194     kia      2423:                     {
1.197     kia      2424:                       // Create a xt:use around the selection
                   2425:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.206     kia      2426: 
1.197     kia      2427:                       TtaRegisterElementDelete (selElem, doc);
                   2428:                       TtaRemoveTree(selElem, doc);
                   2429: 
1.194     kia      2430:                       useType.ElSSchema = sstempl;
1.197     kia      2431:                       useType.ElTypeNum = Template_EL_useSimple;
1.194     kia      2432:                       use = TtaNewElement(doc, useType);
                   2433:                       if(prev)
                   2434:                         TtaInsertSibling(use, prev, FALSE, doc);
                   2435:                       else
                   2436:                         TtaInsertFirstChild(&use, parent, doc);
1.206     kia      2437: 
1.197     kia      2438:                       TtaInsertFirstChild(&selElem, use, doc);
1.206     kia      2439: 
1.197     kia      2440:                       SetAttributeStringValue(use, Template_ATTR_types, TtaGetElementTypeName(TtaGetElementType(selElem)));
1.206     kia      2441: 
1.194     kia      2442:                       TtaRegisterElementCreate(use, doc);
1.206     kia      2443: 
1.194     kia      2444:                       TtaCloseUndoSequence(doc);
                   2445:                     }
1.186     kia      2446:                 }
                   2447:             }
1.206     kia      2448: /*          else if(selElem==selElem2)
                   2449:             {
                   2450:               printf(">> %d - %d / %d - %d\n", firstChar, lastChar, firstChar2, lastChar2);
                   2451:             }*/
1.194     kia      2452:           else if(lastChar<firstChar)
1.206     kia      2453:               use = Template_CreateInlineUse(doc);
                   2454: 
1.194     kia      2455:           TtaSetStructureChecking (oldStructureChecking, doc);
                   2456:           TtaSetDisplayMode (doc, dispMode);
1.206     kia      2457: 
1.186     kia      2458:         }
                   2459:     }
1.187     kia      2460:   return use;
                   2461: #else /* TEMPLATES */
                   2462:   return NULL;
1.186     kia      2463: #endif /* TEMPLATES */
                   2464: }
                   2465: 
                   2466: /*----------------------------------------------------------------------
1.187     kia      2467:   Template_CreateComponentFromSelection
1.186     kia      2468:   Create a xt:component with the selection and move it into the xt:head.
1.187     kia      2469:   Return the xt:component element.
1.186     kia      2470:   ----------------------------------------------------------------------*/
1.187     kia      2471: Element Template_CreateComponentFromSelection(Document doc)
1.186     kia      2472: {
                   2473: #ifdef TEMPLATES
1.187     kia      2474:   ThotBool    oldStructureChecking;
                   2475:   DisplayMode dispMode;
1.186     kia      2476:   Element     selElem, selElem2, parent, parent2, current, child, head;
                   2477:   ElementType selType, selType2;
                   2478:   int         firstChar, lastChar, firstChar2, lastChar2;
                   2479:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
1.192     kia      2480:   XTigerTemplate t = GetXTigerDocTemplate(doc);
1.206     kia      2481: 
1.186     kia      2482:   ElementType compType;
                   2483:   Element     comp = NULL;
                   2484:   char        buffer[128];
1.200     kia      2485:   int         sz = 128;
1.185     kia      2486: 
1.186     kia      2487:   const char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_NEWCOMP);
1.204     vatton   2488:   const char *label = TtaGetMessage (AMAYA, AM_NAME);
1.186     kia      2489: 
1.192     kia      2490:   if(doc && t && TtaGetDocumentAccessMode(doc) &&
1.186     kia      2491:       TtaGetDocumentAccessMode(doc) && sstempl &&
                   2492:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2493:     {
                   2494:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2495:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   2496: 
                   2497:       if(selElem && selElem2)
                   2498:         {
                   2499:           selType =  TtaGetElementType(selElem);
                   2500:           selType2 =  TtaGetElementType(selElem2);
                   2501: 
                   2502:           QueryStringFromUser(label, title, buffer, 127);
1.190     kia      2503:           if(buffer[0]!=EOS)
1.186     kia      2504:             {
1.190     kia      2505:               head = TemplateFindHead(doc);
1.206     kia      2506: 
1.190     kia      2507:               compType.ElSSchema = sstempl;
                   2508:               compType.ElTypeNum = Template_EL_component;
1.206     kia      2509: 
1.190     kia      2510:               parent  = TtaGetParent(selElem);
                   2511:               parent2 = TtaGetParent(selElem2);
1.206     kia      2512: 
1.190     kia      2513:               if(head && firstChar==0 && firstChar2==0 && parent == parent2)
1.180     kia      2514:                 {
1.190     kia      2515:                   dispMode = TtaGetDisplayMode (doc);
                   2516:                   if (dispMode == DisplayImmediately)
                   2517:                     TtaSetDisplayMode (doc, DeferredDisplay);
                   2518:                   oldStructureChecking = TtaGetStructureChecking (doc);
                   2519:                   TtaSetStructureChecking (FALSE, doc);
1.206     kia      2520: 
1.190     kia      2521:                   child = TtaGetLastChild(head);
                   2522:                   TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2523:                   comp = TtaNewElement(doc, compType);
1.185     kia      2524:                   if(child)
1.190     kia      2525:                     TtaInsertSibling(comp, child, FALSE, doc);
1.185     kia      2526:                   else
1.190     kia      2527:                     TtaInsertFirstChild(&comp, head, doc);
                   2528:                   TtaRegisterElementCreate(comp, doc);
1.206     kia      2529: 
1.190     kia      2530:                   TtaNextSibling(&selElem2);
                   2531:                   child = NULL;
                   2532:                   while(selElem!=selElem2)
                   2533:                     {
                   2534:                       current = selElem;
                   2535:                       TtaNextSibling(&selElem);
1.206     kia      2536: 
1.190     kia      2537:                       TtaRegisterElementDelete (current, doc);
                   2538:                       TtaRemoveTree(current, doc);
                   2539:                       if(child)
                   2540:                         TtaInsertSibling(current, child, FALSE, doc);
                   2541:                       else
                   2542:                         TtaInsertFirstChild(&current, comp, doc);
                   2543:                       TtaRegisterElementDelete (current, doc);
                   2544:                       child = current;
                   2545:                     }
1.200     kia      2546:                   if(!Template_SetName(doc, comp, buffer, TRUE))
                   2547:                       GiveAttributeStringValueFromNum(comp, Template_ATTR_name, buffer, &sz);
1.206     kia      2548: 
1.190     kia      2549:                   TtaCloseUndoSequence(doc);
                   2550:                   TtaSelectElement(doc, comp);
1.206     kia      2551: 
1.192     kia      2552:                   Template_DeclareNewComponent(t, buffer, comp);
1.206     kia      2553: 
1.190     kia      2554:                   TtaSetStructureChecking (oldStructureChecking, doc);
                   2555:                   TtaSetDisplayMode (doc, dispMode);
1.180     kia      2556:                 }
                   2557:             }
                   2558:         }
                   2559:     }
1.186     kia      2560:   return comp;
1.187     kia      2561: #else /* TEMPLATES */
                   2562:   return NULL;
1.206     kia      2563: #endif /* TEMPLATES */
1.180     kia      2564: }
1.187     kia      2565: 
1.192     kia      2566: /*----------------------------------------------------------------------
                   2567:   TemplateComponentWillBeDeleted
                   2568:   Processed when a component element will be deleted in a template context.
                   2569:   ----------------------------------------------------------------------*/
                   2570: ThotBool TemplateComponentWillBeDeleted (NotifyElement *event)
                   2571: {
                   2572: #ifdef TEMPLATES
                   2573:   XTigerTemplate t = GetXTigerDocTemplate(event->document);
                   2574:   char* elemName = GetAttributeStringValueFromNum(event->element, Template_ATTR_name, NULL);
1.206     kia      2575: 
1.192     kia      2576:   if(Template_IsUsedComponent(t, event->document, elemName))
                   2577:     {
                   2578:       TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_TEMPLATE_USEDCOMP_CANTREMOVE);
                   2579:       return TRUE;
                   2580:     }
1.206     kia      2581: #endif /* TEMPLATES */
1.192     kia      2582:   return FALSE;
                   2583: }
1.187     kia      2584: 
1.200     kia      2585: /*----------------------------------------------------------------------
                   2586:   UnionClicked
1.206     kia      2587:   Called when a xt:union is clicked
1.200     kia      2588:   ----------------------------------------------------------------------*/
                   2589: void UnionClicked(NotifyElement* event)
                   2590: {
                   2591:   if(event->document && event->element)
                   2592:     TtaSelectElement(event->document, event->element);
                   2593: }
                   2594: 
                   2595: /*----------------------------------------------------------------------
                   2596:   UnionDoubleClicked
1.206     kia      2597:   Called when a xt:union is double clicked
1.200     kia      2598:   ----------------------------------------------------------------------*/
                   2599: ThotBool UnionDoubleClicked(NotifyElement* event)
                   2600: {
                   2601:   Template_ModifyUnionElement(event->document, event->element);
                   2602:   TtaSelectElement(event->document, event->element);
                   2603:   return TRUE;
                   2604: }
                   2605: 
                   2606: /*----------------------------------------------------------------------
                   2607:   TemplateNameAttributeDeleted
1.206     kia      2608:   Called when a xt:name will be deleted
1.200     kia      2609:   ----------------------------------------------------------------------*/
                   2610: ThotBool TemplateNameAttributeDeleted(NotifyAttribute* event)
                   2611: {
                   2612:   // Prevent xt:name deletion
                   2613:   return TRUE;
                   2614: }
                   2615: 
                   2616: /*----------------------------------------------------------------------
                   2617:   TemplateNameAttributeModified
1.206     kia      2618:   Called when a xt:name will be modified
1.200     kia      2619:   ----------------------------------------------------------------------*/
                   2620: ThotBool TemplateNameAttributeModified(NotifyAttribute* event)
                   2621: {
                   2622:   // Prevent xt:name modification
                   2623:   return TRUE;
                   2624: }
                   2625: 
                   2626: /*----------------------------------------------------------------------
                   2627:   TemplateNameAttributeCreated
1.206     kia      2628:   Called when a xt:name have been created
1.200     kia      2629:   ----------------------------------------------------------------------*/
1.203     vatton   2630: void TemplateNameAttributeCreated(NotifyAttribute* event)
1.200     kia      2631: {
1.202     kia      2632:   MakeUniqueName(event->element, event->document, TRUE, FALSE);
1.200     kia      2633: }
                   2634: 
                   2635: 
                   2636: /*----------------------------------------------------------------------
                   2637:   TemplateNameAttrInMenu
                   2638:   Called by Thot when building the Attributes menu for template elements.
                   2639:   ----------------------------------------------------------------------*/
                   2640: ThotBool TemplateNameAttrInMenu (NotifyAttribute * event)
                   2641: {
                   2642: #ifdef TEMPLATES
                   2643:   ElementType type = TtaGetElementType(event->element);
1.206     kia      2644:   if(type.ElTypeNum==Template_EL_component ||
1.200     kia      2645:       type.ElTypeNum==Template_EL_union)
                   2646:     return TRUE;
                   2647: #endif /* TEMPLATES */
                   2648:     return FALSE;
                   2649: }
                   2650: 

Webmaster