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

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

Webmaster