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

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.193   ! vatton   1236: #ifdef AMAYA_DEBUG
1.191     kia      1237:   printf("Template_FillFromDocument\n");
1.193   ! vatton   1238: #endif
        !          1239:   if (t)
1.111     vatton   1240:     {
1.193   ! vatton   1241: #ifdef AMAYA_DEBUG
1.191     kia      1242:       printf("plop : %d\n", t->state);
1.193   ! vatton   1243: #endif
1.191     kia      1244:       SetTemplateDocument (t, doc);
1.144     vatton   1245: 
1.191     kia      1246:       Template_PrepareTemplate(t);
                   1247:       
                   1248:       if(IsTemplateInstanceDocument(doc))
1.144     vatton   1249:         {
1.193   ! vatton   1250: #ifdef AMAYA_DEBUG
1.191     kia      1251:           printf("  > instance\n");
1.193   ! vatton   1252: #endif
1.144     vatton   1253:           // fix all access rights in the instance
1.191     kia      1254:           root = TtaGetRootElement (doc);
                   1255:           TtaSetAccessRight (root, ReadOnly, doc);
1.166     kia      1256:           Template_FixAccessRight (t, root, doc);
1.144     vatton   1257:           TtaUpdateAccessRightInViews (doc, root);
                   1258:         }
1.193   ! vatton   1259: #ifdef AMAYA_DEBUG
1.191     kia      1260:       else if(t->state&templLibraryFlag)
                   1261:         printf("  > library\n");
                   1262:       else if(t->state&templTemplate)
                   1263:         printf("  > template\n");
1.193   ! vatton   1264: #endif      
1.191     kia      1265:       // Mark loaded
                   1266:       t->state |= templloaded;
                   1267:       TtaSetDocumentUnmodified (doc);
                   1268:       UpdateTemplateMenus (doc);
                   1269:       
                   1270: #ifdef AMAYA_DEBUG  
                   1271:       DumpAllDeclarations();
                   1272: #endif /* AMAYA_DEBUG */
                   1273:     }
                   1274: #endif /* TEMPLATES */
                   1275: }
                   1276: 
                   1277: 
                   1278: /*----------------------------------------------------------------------
                   1279:   Template_CheckAndPrepareTemplate checks if the document is a template
                   1280:   or a library and prepare XTigerTemplate structure to use it.
                   1281:   ----------------------------------------------------------------------*/
                   1282: ThotBool Template_CheckAndPrepareTemplate(char* docURL)
                   1283: {
                   1284: #ifdef TEMPLATES
                   1285:   XTigerTemplate t = NULL; //GetXTigerTemplate(docURL);
1.193   ! vatton   1286: 
        !          1287: #ifdef AMAYA_DEBUG
1.191     kia      1288:   printf("Template_CheckAndPrepareTemplate %s\n", docURL);
1.193   ! vatton   1289: #endif
        !          1290:   if (IsXTiger(docURL))
1.191     kia      1291:     {
                   1292:       t = LookForXTigerTemplate(docURL);
                   1293:       t->state |= templTemplate;
1.111     vatton   1294:     }
1.193   ! vatton   1295:   else if (IsXTigerLibrary(docURL))
1.191     kia      1296:     {
                   1297:       t = LookForXTigerLibrary(docURL);
                   1298:       t->state |= templLibrary;
                   1299:     }
1.193   ! vatton   1300:   return t != NULL;
1.111     vatton   1301: #endif /* TEMPLATES */
1.191     kia      1302:   return FALSE;
1.111     vatton   1303: }
1.94      kia      1304: 
1.191     kia      1305: 
1.66      vatton   1306: /*----------------------------------------------------------------------
1.191     kia      1307:   Template_CheckAndPrepareInstance checks if it is a template instance needs.
1.108     vatton   1308:   If it's an instance and the template is not loaded, load it into a
                   1309:   temporary file
1.66      vatton   1310:   ----------------------------------------------------------------------*/
1.191     kia      1311: void Template_CheckAndPrepareInstance (char *localFileName, Document doc, char* docURL)
1.65      francesc 1312: {
                   1313: #ifdef TEMPLATES
1.76      vatton   1314:   XTigerTemplate   t;
1.103     kia      1315:   char            *content, *ptr, *begin;
1.76      vatton   1316:   gzFile           stream;
                   1317:   char             buffer[2000];
1.77      vatton   1318:   int              res;
1.171     kia      1319:   char            *template_version = NULL,
                   1320:                   *template_url = NULL;
1.65      francesc 1321: 
1.171     kia      1322:   stream = TtaGZOpen (localFileName);
1.76      vatton   1323:   if (stream != 0)
1.65      francesc 1324:     {
1.76      vatton   1325:       res = gzread (stream, buffer, 1999);
                   1326:       if (res >= 0)
1.65      francesc 1327:         {
1.81      vatton   1328:           buffer[res] = EOS;
1.103     kia      1329:           begin = strstr (buffer, "<?xtiger");
                   1330:           
1.112     vatton   1331:           if (begin)
1.103     kia      1332:           {
                   1333:             // Search for template version
                   1334:             ptr = strstr (begin, "templateVersion");
                   1335:             if (ptr)
                   1336:               ptr = strstr (ptr, "=");
                   1337:             if (ptr)
                   1338:               ptr = strstr (ptr, "\"");
                   1339:             if (ptr)
                   1340:               {
                   1341:                 // template URI
                   1342:                 content = &ptr[1];
                   1343:                 ptr = strstr (content, "\"");
                   1344:               }
                   1345:             if (ptr)
                   1346:               {
                   1347:                 *ptr = EOS;
                   1348:                 //Get now the template URI
1.171     kia      1349:                 template_version = TtaStrdup (content);
1.103     kia      1350:                 *ptr = '"';
                   1351:               }
                   1352:            
                   1353:             // Search for template uri
                   1354:             ptr = strstr (begin, "template");
                   1355:             if (ptr && ptr[8] != 'V')
                   1356:               ptr = strstr (ptr, "=");
                   1357:             if (ptr)
                   1358:               ptr = strstr (ptr, "\"");
                   1359:             if (ptr)
                   1360:               {
                   1361:                 // template URI
                   1362:                 content = &ptr[1];
                   1363:                 ptr = strstr (content, "\"");
                   1364:               }
                   1365:             if (ptr)
                   1366:               {
                   1367:                 *ptr = EOS;
                   1368:                 //Get now the template URI
1.171     kia      1369:                 template_url = TtaStrdup (content);
                   1370: 
                   1371:                 t = GetXTigerTemplate (template_url);
1.103     kia      1372:                 if (!t)
                   1373:                   {
1.171     kia      1374:                     LoadTemplate (doc, template_url);
                   1375:                     t = GetXTigerTemplate(template_url);
1.103     kia      1376:                   }
1.171     kia      1377:                 Template_PrepareInstance(docURL, doc, template_version, template_url);
                   1378:                 template_version = NULL;
                   1379:                 template_url     = NULL;
                   1380:                 Template_AddReference (t);
1.103     kia      1381:                 *ptr = '"';
                   1382:               }
                   1383:           }
1.65      francesc 1384:         }
                   1385:     }
1.171     kia      1386:   TtaFreeMemory(template_version);
                   1387:   TtaFreeMemory(template_url);
1.76      vatton   1388:   TtaGZClose (stream);
1.65      francesc 1389: #endif /* TEMPLATES */
                   1390: }
                   1391: 
1.64      francesc 1392: /*----------------------------------------------------------------------
1.178     kia      1393:   ClosingTemplateDocument
                   1394:   Callback called before closing a document which uses templates.
1.64      francesc 1395:   ----------------------------------------------------------------------*/
1.178     kia      1396: ThotBool ClosingTemplateDocument(NotifyDialog* dialog)
1.64      francesc 1397: {
1.65      francesc 1398: #ifdef TEMPLATES
1.171     kia      1399:   XTigerTemplate t = GetXTigerDocTemplate(dialog->document);
                   1400:   if(t)
1.178     kia      1401:       Template_RemoveReference(t);
1.65      francesc 1402: #endif /* TEMPLATES */
                   1403:   return FALSE;
1.64      francesc 1404: }
1.87      kia      1405: 
                   1406: /*----------------------------------------------------------------------
1.120     kia      1407:   IsTemplateElement
1.138     vatton   1408:   Test if an element is a template element.
1.87      kia      1409:   ----------------------------------------------------------------------*/
1.140     vatton   1410: ThotBool IsTemplateElement (Element elem)
1.87      kia      1411: {
                   1412: #ifdef TEMPLATES
1.138     vatton   1413:   ElementType     elType;
                   1414: 
                   1415:   elType = TtaGetElementType(elem);
                   1416:   if (elType.ElSSchema)
1.164     kia      1417:     return (strcmp(TtaGetSSchemaName(elType.ElSSchema) , "Template") == 0);
1.138     vatton   1418: #endif /* TEMPLATES */
1.87      kia      1419:   return FALSE;
                   1420: }
                   1421: 
                   1422: 
                   1423: /*----------------------------------------------------------------------
                   1424:   GetFirstTemplateParentElement
1.138     vatton   1425:   Return the first element which has "Template" as schema name or null.
1.87      kia      1426:   ----------------------------------------------------------------------*/
                   1427: Element GetFirstTemplateParentElement(Element elem)
                   1428: {
                   1429: #ifdef TEMPLATES
1.138     vatton   1430:   ElementType     elType;
                   1431: 
                   1432:   elem = TtaGetParent (elem);
                   1433:   elType = TtaGetElementType(elem);
                   1434:   while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
                   1435: {
                   1436:     elem = TtaGetParent (elem);
                   1437:     elType = TtaGetElementType(elem);
1.87      kia      1438:   }
                   1439:   return elem;
                   1440: #else
                   1441:   return NULL;
                   1442: #endif /* TEMPLATES */
                   1443: }
1.101     kia      1444: 
1.103     kia      1445: 
1.101     kia      1446: /*----------------------------------------------------------------------
1.102     vatton   1447:   TemplateElementWillBeCreated
1.101     kia      1448:   Processed when an element will be created in a template context.
                   1449:   ----------------------------------------------------------------------*/
1.102     vatton   1450: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101     kia      1451: {
1.103     kia      1452: #ifdef TEMPLATES
                   1453:   ElementType elType = event->elementType;
                   1454:   Element     parent = event->element;
                   1455:   ElementType parentType = TtaGetElementType(parent);
1.113     kia      1456:   Element     ancestor;
                   1457:   ElementType ancestorType;
                   1458:   SSchema     templateSSchema;
                   1459:   char*       types;
                   1460:   ThotBool    b;
1.101     kia      1461: 
1.115     kia      1462:   if(event->info==1)
                   1463:     return FALSE;
                   1464: 
1.193   ! vatton   1465: #ifdef AMAYA_DEBUG
1.185     kia      1466:   printf("TemplateElementWillBeCreated\n");
1.193   ! vatton   1467: #endif
1.112     vatton   1468:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1469:     return TRUE;
                   1470: 
1.138     vatton   1471:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.102     vatton   1472:   if (templateSSchema == NULL)
                   1473:     return FALSE; // let Thot do the job
1.115     kia      1474: 
1.113     kia      1475:   // Fisrt, test if in a xt:bag or in a base-element xt:use
1.147     vatton   1476:   if(parentType.ElSSchema == templateSSchema)
1.113     kia      1477:     ancestor = parent;
                   1478:   else
1.147     vatton   1479:     ancestor = GetFirstTemplateParentElement (parent);
1.113     kia      1480: 
1.147     vatton   1481:   if (ancestor)
1.113     kia      1482:   {
                   1483:     ancestorType = TtaGetElementType(ancestor);
1.147     vatton   1484:     if (ancestorType.ElTypeNum == Template_EL_bag)
1.113     kia      1485:     {
1.147     vatton   1486:       // only check the use child
                   1487:       if (ancestor != parent)
                   1488:         return  FALSE; // let Thot do the job
                   1489:       if (elType.ElSSchema == templateSSchema &&
                   1490:           (elType.ElTypeNum == Template_EL_useSimple ||
                   1491:            elType.ElTypeNum == Template_EL_useEl))
1.116     kia      1492:         return FALSE;
1.147     vatton   1493:       return !Template_CanInsertElementInBagElement (event->document, elType, ancestor);      
1.113     kia      1494:     }
1.147     vatton   1495:     else if(ancestorType.ElTypeNum == Template_EL_useSimple ||
                   1496:             ancestorType.ElTypeNum == Template_EL_useEl)
1.113     kia      1497:     {
1.147     vatton   1498:       // only check the bag child @@@ will be check exclude/include later
1.172     kia      1499:       if (ancestor != parent)
                   1500:         return  FALSE; // let Thot do the job
1.113     kia      1501:       types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
1.128     kia      1502:       b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position);
                   1503:       TtaFreeMemory(types);
1.115     kia      1504:       return !b;
1.113     kia      1505:     }
                   1506:   }
1.115     kia      1507:   
1.147     vatton   1508:   if (elType.ElSSchema == templateSSchema && elType.ElTypeNum == Template_EL_TEXT_UNIT)
1.115     kia      1509:   {
                   1510:     return FALSE;
                   1511:   }
                   1512:   
1.113     kia      1513:   // Can not insert.
                   1514:   return TRUE;
1.101     kia      1515: #endif /* TEMPLATES*/
1.102     vatton   1516:   return FALSE;
1.101     kia      1517: }
                   1518: 
                   1519: /*----------------------------------------------------------------------
                   1520:   TemplateElementWillBeDeleted
                   1521:   Processed when an element will be deleted in a template context.
                   1522:   ----------------------------------------------------------------------*/
                   1523: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1524: {
1.107     kia      1525: #ifdef TEMPLATES
                   1526:   Document       doc = event->document;
                   1527:   Element        elem = event->element;
1.163     vatton   1528:   Element        xtElem, parent = NULL, sibling;
1.117     kia      1529:   ElementType    xtType, elType;
1.107     kia      1530:   char*          type;
                   1531:   Declaration    dec;
1.115     kia      1532:   SSchema        templateSSchema;
1.107     kia      1533:   XTigerTemplate t;
1.156     vatton   1534:   ThotBool       selparent = FALSE;
1.192     kia      1535:   
1.115     kia      1536:   if(event->info==1)
                   1537:     return FALSE;
                   1538: 
1.112     vatton   1539:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1540:     return TRUE;
                   1541: 
1.138     vatton   1542:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.107     kia      1543:   if (templateSSchema == NULL)
                   1544:     return FALSE; // let Thot do the job
1.192     kia      1545:   
                   1546:   t = GetXTigerDocTemplate(doc);
                   1547:   if(Template_IsTemplate(t)||Template_IsLibrary(t))
                   1548:     return FALSE; // If template or library, pass to specialized functions.
1.122     kia      1549: 
1.107     kia      1550:   xtElem = GetFirstTemplateParentElement(elem);
1.112     vatton   1551:   if (xtElem)
1.107     kia      1552:   {
                   1553:     xtType = TtaGetElementType(xtElem);
1.117     kia      1554:     
1.192     kia      1555:     
1.109     kia      1556: 
1.112     vatton   1557:     if (xtType.ElTypeNum==Template_EL_bag)
1.117     kia      1558:     {
                   1559:       elType = TtaGetElementType(elem);
                   1560:       if(elType.ElSSchema==templateSSchema &&
                   1561:         (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
                   1562:       {
                   1563:         // Remove element manually.
                   1564:         TtaOpenUndoSequence(doc, elem, elem, 0, 0);
                   1565:         TtaRegisterElementDelete(elem, doc);
                   1566:         TtaDeleteTree(elem, doc);
                   1567:         TtaCloseUndoSequence(doc);
                   1568:         return TRUE;
                   1569:       }
                   1570:       else
                   1571:         return FALSE; // xt:bag always allow remove children.
                   1572:     }
1.112     vatton   1573:     else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107     kia      1574:     {
1.109     kia      1575:       parent = TtaGetParent(elem);
1.117     kia      1576:       if (xtElem!=parent)
                   1577:       {
                   1578:         type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
                   1579:         dec = Template_GetDeclaration(t, type);
                   1580:         TtaFreeMemory(type);
                   1581:         
                   1582:         if (dec && dec->nature == XmlElementNat)
                   1583:           return FALSE; // Can remove element only if in xt:use current type is base language element. 
                   1584:         else
                   1585:           return TRUE;
1.107     kia      1586:       }
1.109     kia      1587:     }
1.112     vatton   1588:     else if (xtType.ElTypeNum==Template_EL_repeat)
1.109     kia      1589:     {
1.156     vatton   1590:       sibling = TtaGetSuccessor (elem);
                   1591:       if (sibling == NULL)
                   1592:         {
                   1593:           // there is no next element
                   1594:           sibling = TtaGetPredecessor (elem);
                   1595:           if (sibling == NULL)
                   1596:             selparent = TRUE;
                   1597:         }
1.109     kia      1598:       TtaRegisterElementDelete(elem, doc);
                   1599:       TtaDeleteTree(elem, doc);
                   1600:       InstantiateRepeat(t, xtElem, doc, TRUE);
1.156     vatton   1601:       if (selparent)
                   1602:         // look for the new sibling
                   1603:         sibling = TtaGetFirstChild (parent);
                   1604:       if (sibling)
                   1605:         TtaSelectElement(doc, sibling);
1.151     quint    1606:       else
1.156     vatton   1607:         TtaSelectElement(doc, parent);
1.109     kia      1608:       return TRUE;
1.107     kia      1609:     }
                   1610:   }
1.109     kia      1611:   
                   1612:   //TODO Test if current element is use or repeat.
                   1613:   // Because if an element is delete and it is the unique child of its parent,
                   1614:   // the parent intends to destroy itself. 
                   1615:   
1.107     kia      1616:   return TRUE;
                   1617: #else /* TEMPLATES */
1.101     kia      1618:   return FALSE;
1.107     kia      1619: #endif /* TEMPLATES */
1.101     kia      1620: }
                   1621: 
1.109     kia      1622: /*----------------------------------------------------------------------
                   1623:   CurrentTypeWillBeExported
                   1624:   Check if the xt:currentType attribute can be exported
                   1625:   ----------------------------------------------------------------------*/
                   1626: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
                   1627: {
                   1628: #ifdef TEMPLATES
1.110     kia      1629: 
1.112     vatton   1630:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1631:     return TRUE;
                   1632: 
1.112     vatton   1633:   if (IsTemplateDocument(event->document))
1.109     kia      1634:     return TRUE;
                   1635: #endif /* TEMPLATES */
                   1636:   return FALSE;
                   1637: }
1.127     kia      1638: 
                   1639: /*----------------------------------------------------------------------
                   1640:   TemplateAttrInMenu
                   1641:   Called by Thot when building the Attributes menu for template elements.
                   1642:   ----------------------------------------------------------------------*/
                   1643: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
                   1644: {
                   1645: #ifdef TEMPLATES
                   1646:   // Prevent from showing attributes for template instance but not templates.
                   1647:   if(IsTemplateInstanceDocument(event->document))
                   1648:     return TRUE;
                   1649:   else
                   1650: #endif /* TEMPLATES */
                   1651:     return FALSE;
                   1652: }
1.160     kia      1653: 
1.167     kia      1654: /*----------------------------------------------------------------------
1.168     kia      1655:   CreateTemplateFromDocument
                   1656:   Create a template from the current document.
1.167     kia      1657:   ----------------------------------------------------------------------*/
                   1658: void CreateTemplateFromDocument(Document doc, View view)
                   1659: {
1.171     kia      1660: #ifdef TEMPLATES
1.168     kia      1661:   char buffer[MAX_LENGTH];
                   1662:   strcpy(buffer, DocumentURLs[doc]);
                   1663:   strcat(buffer, ".xtd");
                   1664:   DontReplaceOldDoc = TRUE;
                   1665:   CreateTemplate(doc, buffer);
1.171     kia      1666: #endif /* TEMPLATES */
1.167     kia      1667: }
                   1668: 
1.169     kia      1669: /*----------------------------------------------------------------------
                   1670:   UpdateTemplateMenus
                   1671:   ----------------------------------------------------------------------*/
                   1672: void UpdateTemplateMenus (Document doc)
                   1673: {
1.180     kia      1674:   if(IsTemplateInstanceDocument(doc))
                   1675:     {
                   1676:       // Instance document
                   1677:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1678:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1679:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.180     kia      1680:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
                   1681:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1682:     }
1.183     vatton   1683:   else if (DocumentURLs[doc] && IsXTiger (DocumentURLs[doc]))
1.180     kia      1684:     {
                   1685:       // Template document
                   1686:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1687:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1688:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseBox);
1.180     kia      1689:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateFreeBox);      
                   1690:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeat);
                   1691:     }
1.169     kia      1692:   else
1.180     kia      1693:     {
                   1694:       //Standard document
                   1695:       TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
                   1696:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1697:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.180     kia      1698:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
                   1699:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1700:     }
1.169     kia      1701: }
1.171     kia      1702: 
                   1703: /*----------------------------------------------------------------------
                   1704:   UninstanciateTemplateDocument
                   1705:   An instance of a template is tranformed into a template-less docuemnt.
                   1706:   Remove link between XTigerTemplate structure and document.
                   1707:   ----------------------------------------------------------------------*/
                   1708: void UninstanciateTemplateDocument(Document doc)
                   1709: {
                   1710: #ifdef TEMPLATES
                   1711:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1712:   if(t)
                   1713:     Template_Close(t);
                   1714: #endif /* TEMPLATES */  
                   1715: }
                   1716: 
                   1717: 
                   1718: /*----------------------------------------------------------------------
                   1719:   Template_PrepareInstance
                   1720:   Allocate XTigerTemplate structure for instance and initialize template
                   1721:   url and template version.
                   1722:   ----------------------------------------------------------------------*/
                   1723: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
                   1724: {
                   1725: #ifdef TEMPLATES
                   1726:   XTigerTemplate t = GetXTigerTemplate(fileName);
                   1727:   if(!t)
                   1728:     t = NewXTigerTemplate(fileName);
                   1729:   t->state           = templInstance;
                   1730:   t->templateVersion = template_version;
                   1731:   t->base_uri        = template_url;
                   1732:   t->doc             = doc;
1.178     kia      1733:   t->ref             = 1;
1.171     kia      1734:   
                   1735: #endif /* TEMPLATES */
                   1736: }
                   1737: 
                   1738: 
                   1739: /*----------------------------------------------------------------------
                   1740:   SetDocumentAsXTigerTemplate
                   1741:   Set the document template structure as template.
                   1742:   ----------------------------------------------------------------------*/
                   1743: void SetDocumentAsXTigerTemplate(Document doc)
                   1744: {
                   1745: #ifdef TEMPLATES
                   1746:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1747:   if(t)
                   1748:     t->state |= templTemplate;
                   1749: #endif /* TEMPLATES */  
                   1750: }
                   1751: 
                   1752: /*----------------------------------------------------------------------
                   1753:   SetDocumentAsXTigerLibrary
                   1754:   Set the document template structure as template library.
                   1755:   ----------------------------------------------------------------------*/
                   1756: void SetDocumentAsXTigerLibrary(Document doc)
                   1757: {
                   1758: #ifdef TEMPLATES
                   1759:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1760:   if(t)
                   1761:     t->state |= templLibrary;
                   1762: #endif /* TEMPLATES */  
                   1763: }
1.174     kia      1764: 
                   1765: 
                   1766: /*----------------------------------------------------------------------
                   1767:   TemplateCreateTextBox
                   1768:   Create a xt:use types="string" box around the selection.
                   1769:   ----------------------------------------------------------------------*/
                   1770: void TemplateCreateTextBox(Document doc, View view)
                   1771: {
                   1772: #ifdef TEMPLATES
                   1773:   Element     selElem;
                   1774:   ElementType selType;
                   1775:   int         firstChar, lastChar;
                   1776:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1777:   ElementType useType;
                   1778:   Element     use;
                   1779:   char        buffer[128];
1.189     kia      1780:   ThotBool    oldStructureChecking;
1.174     kia      1781:   
                   1782:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
1.184     vatton   1783:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174     kia      1784: 
                   1785:   if (!TtaGetDocumentAccessMode(doc))
                   1786:     return;
                   1787:   
                   1788:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1789:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1790:     {
                   1791:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1792:       if(selElem)
                   1793:         {
                   1794:           selType =  TtaGetElementType(selElem);
                   1795:           if(!TtaIsLeaf(selType))
                   1796:             {
                   1797:               selElem = TtaGetFirstLeaf(selElem);
                   1798:               selType = TtaGetElementType(selElem);
                   1799:               firstChar = lastChar = 0;
                   1800:             }
                   1801:           
                   1802:           if(selType.ElTypeNum==1)
                   1803:             {
                   1804:               QueryStringFromUser(label, title, buffer, 127);
                   1805:               useType.ElSSchema = sstempl;
                   1806:               useType.ElTypeNum = Template_EL_useSimple;
                   1807: 
1.189     kia      1808:               oldStructureChecking = TtaGetStructureChecking (doc);
                   1809:               TtaSetStructureChecking (FALSE, doc);
                   1810: 
1.174     kia      1811:               if(firstChar==0)
                   1812:                 {
                   1813:                   use = TtaNewElement(doc, useType);
                   1814:                   if(use)
                   1815:                     {
                   1816:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1817:                       TtaInsertSibling(use, selElem, FALSE, doc);
                   1818:                       TtaRegisterElementCreate(use, doc);
                   1819:                       TtaRegisterElementDelete (selElem, doc);
                   1820:                       TtaRemoveTree(selElem, doc);
                   1821:                       TtaInsertFirstChild(&selElem, use, doc);
                   1822:                       TtaRegisterElementDelete (selElem, doc);
                   1823:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1824:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);
                   1825:                       TtaCloseUndoSequence(doc);
                   1826:                       TtaSelectElement(doc, use);
                   1827:                     }
                   1828:                 }
                   1829:               else
                   1830:                 {
                   1831:                   GenerateInlineElement(Template_EL_useSimple, sstempl, 0, "", TRUE);
                   1832:                   TtaGiveFirstSelectedElement(doc, &use, &firstChar, &lastChar);
                   1833:                   selType =  TtaGetElementType(use);
                   1834:                   if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_useSimple)
                   1835:                     {
                   1836:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1837:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);                      
                   1838:                     }
                   1839:                 }
1.189     kia      1840:               
                   1841:               TtaSetStructureChecking (oldStructureChecking, doc);
                   1842:               
1.174     kia      1843:             }
                   1844:           
                   1845:         }
                   1846:     }
                   1847: #endif /* TEMPLATES */
                   1848: }
                   1849: 
                   1850: 
                   1851: /*----------------------------------------------------------------------
                   1852:   TemplateCreateFreeBox
                   1853:   Create a xt:bag types="string" box around the selection.
                   1854:   ----------------------------------------------------------------------*/
                   1855: void TemplateCreateFreeBox(Document doc, View view)
                   1856: {
                   1857: #ifdef TEMPLATES
1.185     kia      1858:   Element     selElem, selElem2, parent, parent2, current, child = NULL;
1.174     kia      1859:   ElementType selType, selType2;
                   1860:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1861:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1862: 
                   1863:   ElementType bagType;
                   1864:   Element     bag;
                   1865:   char        buffer[128];
                   1866: 
                   1867:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
1.184     vatton   1868:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174     kia      1869: 
                   1870:   if (!TtaGetDocumentAccessMode(doc))
                   1871:     return;
                   1872:   
                   1873:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1874:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1875:     {
                   1876:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1877:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   1878:       
                   1879:       if(selElem && selElem2)
                   1880:         {
                   1881:           selType =  TtaGetElementType(selElem);
                   1882:           selType2 =  TtaGetElementType(selElem2);
                   1883: 
                   1884:           QueryStringFromUser(label, title, buffer, 127);
                   1885:           bagType.ElSSchema = sstempl;
                   1886:           bagType.ElTypeNum = Template_EL_bag;
                   1887:           
1.185     kia      1888:           parent  = TtaGetParent(selElem);
                   1889:           parent2 = TtaGetParent(selElem2);
                   1890:           
                   1891:           if(firstChar==0 && firstChar2==0 && parent == parent2)
1.174     kia      1892:             {
1.185     kia      1893:               ThotBool        oldStructureChecking;
                   1894:               DisplayMode     dispMode;
                   1895:               dispMode = TtaGetDisplayMode (doc);
                   1896:               if (dispMode == DisplayImmediately)
                   1897:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   1898:               oldStructureChecking = TtaGetStructureChecking (doc);
                   1899:               TtaSetStructureChecking (FALSE, doc);
                   1900: 
                   1901:               // Create and insert xt:bag element
                   1902:               bag = TtaNewElement(doc, bagType);
                   1903:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1904:               TtaInsertSibling(bag, selElem, TRUE, doc);
                   1905:               TtaRegisterElementCreate(bag, doc);
                   1906: 
                   1907:               TtaNextSibling(&selElem2);
                   1908:               while(selElem!=selElem2)
1.174     kia      1909:                 {
1.185     kia      1910:                   current = selElem;
                   1911:                   TtaNextSibling(&selElem);
1.174     kia      1912:                   
1.185     kia      1913:                   TtaRegisterElementDelete (current, doc);
                   1914:                   TtaRemoveTree(current, doc);
                   1915:                   if(child)
                   1916:                     TtaInsertSibling(current, child, FALSE, doc);
                   1917:                   else
                   1918:                     TtaInsertFirstChild(&current, bag, doc);
                   1919:                   TtaRegisterElementDelete (current, doc);
                   1920:                   child = current;
1.174     kia      1921:                 }
1.185     kia      1922:               
                   1923:               SetAttributeStringValue(bag, Template_ATTR_types, "any");
                   1924:               SetAttributeStringValue(bag, Template_ATTR_title, buffer);
                   1925:               TtaCloseUndoSequence(doc);
                   1926:               TtaSelectElement(doc, bag);
                   1927:               
                   1928:               TtaSetStructureChecking (oldStructureChecking, doc);
                   1929:               TtaSetDisplayMode (doc, dispMode);
1.174     kia      1930:             }
                   1931:         }
                   1932:     }
                   1933: #endif /* TEMPLATES */
                   1934: }
                   1935: 
1.180     kia      1936: /*----------------------------------------------------------------------
                   1937:   TemplateCreateRepeat
                   1938:   Create a xt:repeat around the selection.
                   1939:   ----------------------------------------------------------------------*/
                   1940: void TemplateCreateRepeat(Document doc, View view)
                   1941: {
                   1942: #ifdef TEMPLATES
1.187     kia      1943:   ThotBool    oldStructureChecking;
                   1944:   DisplayMode dispMode;
                   1945:   Element     selElem, selElem2, parent, parent2;
                   1946:   ElementType selType, selType2;
1.180     kia      1947:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1948:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1949: 
1.186     kia      1950:   ElementType repType;
1.187     kia      1951:   Element     rep, use;
1.180     kia      1952: 
                   1953:   if (!TtaGetDocumentAccessMode(doc))
                   1954:     return;
                   1955:   
                   1956:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1957:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1958:     {
                   1959:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1960:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1.185     kia      1961: 
1.180     kia      1962:       if(selElem && selElem2)
                   1963:         {
                   1964:           selType =  TtaGetElementType(selElem);
                   1965:           selType2 =  TtaGetElementType(selElem2);
                   1966: 
                   1967:           repType.ElSSchema = sstempl;
                   1968:           repType.ElTypeNum = Template_EL_repeat;
1.186     kia      1969:           
1.185     kia      1970:           parent  = TtaGetParent(selElem);
                   1971:           parent2 = TtaGetParent(selElem2);
                   1972: 
                   1973:           if(firstChar==0 && firstChar2==0 && parent == parent2)
1.180     kia      1974:             {
1.187     kia      1975:               dispMode = TtaGetDisplayMode (doc);
                   1976:               if (dispMode == DisplayImmediately)
                   1977:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   1978:               oldStructureChecking = TtaGetStructureChecking (doc);
                   1979:               TtaSetStructureChecking (FALSE, doc);
                   1980:               
                   1981:               if(selElem==selElem2 && selType.ElSSchema==sstempl && 
                   1982:                   (selType.ElTypeNum==Template_EL_useEl || 
                   1983:                    selType.ElTypeNum==Template_EL_useSimple))
                   1984:                 use = selElem;
                   1985:               else
                   1986:                 use = Template_CreateUseFromSelection(doc);
                   1987:               if(use)
                   1988:                 {
                   1989:                   TtaExtendUndoSequence(doc);
                   1990:                   
                   1991:                   rep = TtaNewElement(doc, repType);
                   1992:                   TtaInsertSibling(rep, use, FALSE, doc);
                   1993:                   TtaRegisterElementCreate(rep, doc);
                   1994: 
                   1995:                   TtaRegisterElementDelete (use, doc);
                   1996:                   TtaRemoveTree(use, doc);
                   1997:                   
                   1998:                   TtaInsertFirstChild(&use, rep, doc);
                   1999:                   TtaRegisterElementCreate(use, doc);
                   2000:                   
                   2001:                   TtaSelectElement(doc, use);
                   2002:                   TtaCloseUndoSequence(doc);
                   2003:                 }
                   2004:               
                   2005:               TtaSetStructureChecking (oldStructureChecking, doc);
                   2006:               TtaSetDisplayMode (doc, dispMode);
                   2007:             }
                   2008:         }
                   2009:     }
                   2010: #endif /* TEMPLATES */
                   2011: }
                   2012: 
                   2013: 
                   2014: /*----------------------------------------------------------------------
                   2015:   TemplateCreateUseBox
                   2016:   Create a xt:use around the selection.
                   2017:   ----------------------------------------------------------------------*/
                   2018: void TemplateCreateUseBox(Document doc, View view)
                   2019: {
                   2020: #ifdef TEMPLATES
                   2021:   Template_CreateUseFromSelection(doc);
                   2022: #endif /* TEMPLATES */
                   2023: }
                   2024: 
                   2025: 
                   2026: /*----------------------------------------------------------------------
                   2027:   Template_CreateUseFromSelection
                   2028:   Create a xt:use with the selection.
                   2029:   Return the xt:component element.
                   2030:   ----------------------------------------------------------------------*/
                   2031: Element Template_CreateUseFromSelection(Document doc)
                   2032: {
                   2033: #ifdef TEMPLATES
                   2034:   ThotBool    oldStructureChecking;
                   2035:   DisplayMode dispMode;
                   2036:   Element     selElem, selElem2, parent, parent2;
                   2037:   ElementType selType, selType2;
                   2038:   int         firstChar, lastChar, firstChar2, lastChar2;
                   2039:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   2040: 
                   2041:   ElementType useType;
                   2042:   Element     use = NULL, comp, prev;
                   2043:   char        buffer[128];
                   2044:   int         sz = 128;
                   2045: 
                   2046:   if (!TtaGetDocumentAccessMode(doc))
                   2047:     return NULL;
                   2048:   
                   2049:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   2050:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2051:     {
                   2052:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2053:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   2054: 
                   2055:       if(selElem && selElem2)
                   2056:         {
                   2057:           selType =  TtaGetElementType(selElem);
                   2058:           selType2 =  TtaGetElementType(selElem2);
                   2059:           parent  = TtaGetParent(selElem);
                   2060:           parent2 = TtaGetParent(selElem2);
                   2061: 
                   2062:           if(firstChar==0 && firstChar2==0 && parent == parent2)
                   2063:             {
1.185     kia      2064:               dispMode = TtaGetDisplayMode (doc);
                   2065:               if (dispMode == DisplayImmediately)
                   2066:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   2067:               oldStructureChecking = TtaGetStructureChecking (doc);
                   2068:               TtaSetStructureChecking (FALSE, doc);
                   2069:               
1.186     kia      2070:               prev = selElem;
                   2071:               TtaPreviousSibling(&prev);
                   2072:               
1.187     kia      2073:               comp = Template_CreateComponentFromSelection(doc);
1.186     kia      2074:               if(comp)
                   2075:                 {
                   2076:                   TtaExtendUndoSequence(doc);
                   2077:                   
1.187     kia      2078:                   useType.ElSSchema = sstempl;
                   2079:                   useType.ElTypeNum = Template_EL_useEl;
                   2080:                   use = TtaNewElement(doc, useType);
1.186     kia      2081:                   if(prev)
1.187     kia      2082:                     TtaInsertSibling(use, prev, FALSE, doc);
1.186     kia      2083:                   else
1.187     kia      2084:                     TtaInsertFirstChild(&use, parent, doc);
1.186     kia      2085:                   
                   2086:                   GiveAttributeStringValueFromNum(comp, Template_ATTR_name, buffer, &sz);
                   2087:                   SetAttributeStringValue(use, Template_ATTR_types, buffer);
                   2088:                   TtaRegisterElementCreate(use, doc);
                   2089:                   
1.187     kia      2090:                   TtaSelectElement(doc, use);
1.186     kia      2091:                   TtaCloseUndoSequence(doc);
                   2092:                 }
1.185     kia      2093:               
1.186     kia      2094:               TtaSetStructureChecking (oldStructureChecking, doc);
                   2095:               TtaSetDisplayMode (doc, dispMode);
                   2096:             }
                   2097:         }
                   2098:     }
1.187     kia      2099:   return use;
                   2100: #else /* TEMPLATES */
                   2101:   return NULL;
1.186     kia      2102: #endif /* TEMPLATES */
                   2103: }
                   2104: 
                   2105: /*----------------------------------------------------------------------
1.187     kia      2106:   Template_CreateComponentFromSelection
1.186     kia      2107:   Create a xt:component with the selection and move it into the xt:head.
1.187     kia      2108:   Return the xt:component element.
1.186     kia      2109:   ----------------------------------------------------------------------*/
1.187     kia      2110: Element Template_CreateComponentFromSelection(Document doc)
1.186     kia      2111: {
                   2112: #ifdef TEMPLATES
1.187     kia      2113:   ThotBool    oldStructureChecking;
                   2114:   DisplayMode dispMode;
1.186     kia      2115:   Element     selElem, selElem2, parent, parent2, current, child, head;
                   2116:   ElementType selType, selType2;
                   2117:   int         firstChar, lastChar, firstChar2, lastChar2;
                   2118:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
1.192     kia      2119:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2120:   
1.186     kia      2121:   ElementType compType;
                   2122:   Element     comp = NULL;
                   2123:   char        buffer[128];
1.185     kia      2124: 
1.186     kia      2125:   const char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_NEWCOMP);
                   2126:   const char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
                   2127: 
1.192     kia      2128:   if(doc && t && TtaGetDocumentAccessMode(doc) &&
1.186     kia      2129:       TtaGetDocumentAccessMode(doc) && sstempl &&
                   2130:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2131:     {
                   2132:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2133:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   2134: 
                   2135:       if(selElem && selElem2)
                   2136:         {
                   2137:           selType =  TtaGetElementType(selElem);
                   2138:           selType2 =  TtaGetElementType(selElem2);
                   2139: 
                   2140:           QueryStringFromUser(label, title, buffer, 127);
1.190     kia      2141:           if(buffer[0]!=EOS)
1.186     kia      2142:             {
1.190     kia      2143:               head = TemplateFindHead(doc);
                   2144:               
                   2145:               compType.ElSSchema = sstempl;
                   2146:               compType.ElTypeNum = Template_EL_component;
                   2147:     
                   2148:               parent  = TtaGetParent(selElem);
                   2149:               parent2 = TtaGetParent(selElem2);
1.185     kia      2150:               
1.190     kia      2151:               if(head && firstChar==0 && firstChar2==0 && parent == parent2)
1.180     kia      2152:                 {
1.190     kia      2153:                   dispMode = TtaGetDisplayMode (doc);
                   2154:                   if (dispMode == DisplayImmediately)
                   2155:                     TtaSetDisplayMode (doc, DeferredDisplay);
                   2156:                   oldStructureChecking = TtaGetStructureChecking (doc);
                   2157:                   TtaSetStructureChecking (FALSE, doc);
                   2158:     
                   2159:                   child = TtaGetLastChild(head);
                   2160:                   TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2161:                   comp = TtaNewElement(doc, compType);
1.185     kia      2162:                   if(child)
1.190     kia      2163:                     TtaInsertSibling(comp, child, FALSE, doc);
1.185     kia      2164:                   else
1.190     kia      2165:                     TtaInsertFirstChild(&comp, head, doc);
                   2166:                   TtaRegisterElementCreate(comp, doc);
                   2167:                   
                   2168:                   TtaNextSibling(&selElem2);
                   2169:                   child = NULL;
                   2170:                   while(selElem!=selElem2)
                   2171:                     {
                   2172:                       current = selElem;
                   2173:                       TtaNextSibling(&selElem);
                   2174:                       
                   2175:                       TtaRegisterElementDelete (current, doc);
                   2176:                       TtaRemoveTree(current, doc);
                   2177:                       if(child)
                   2178:                         TtaInsertSibling(current, child, FALSE, doc);
                   2179:                       else
                   2180:                         TtaInsertFirstChild(&current, comp, doc);
                   2181:                       TtaRegisterElementDelete (current, doc);
                   2182:                       child = current;
                   2183:                     }
                   2184:                   
                   2185:                   SetAttributeStringValue(comp, Template_ATTR_name, buffer);
                   2186:                   TtaCloseUndoSequence(doc);
                   2187:                   TtaSelectElement(doc, comp);
                   2188:                   
1.192     kia      2189:                   Template_DeclareNewComponent(t, buffer, comp);
                   2190:                   
1.190     kia      2191:                   TtaSetStructureChecking (oldStructureChecking, doc);
                   2192:                   TtaSetDisplayMode (doc, dispMode);
1.180     kia      2193:                 }
                   2194:             }
                   2195:         }
                   2196:     }
1.186     kia      2197:   return comp;
1.187     kia      2198: #else /* TEMPLATES */
                   2199:   return NULL;
                   2200: #endif /* TEMPLATES */ 
1.180     kia      2201: }
1.187     kia      2202: 
1.192     kia      2203: /*----------------------------------------------------------------------
                   2204:   TemplateComponentWillBeDeleted
                   2205:   Processed when a component element will be deleted in a template context.
                   2206:   ----------------------------------------------------------------------*/
                   2207: ThotBool TemplateComponentWillBeDeleted (NotifyElement *event)
                   2208: {
                   2209: #ifdef TEMPLATES
                   2210:   XTigerTemplate t = GetXTigerDocTemplate(event->document);
                   2211:   char* elemName = GetAttributeStringValueFromNum(event->element, Template_ATTR_name, NULL);
                   2212:   
                   2213:   if(Template_IsUsedComponent(t, event->document, elemName))
                   2214:     {
                   2215:       TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_TEMPLATE_USEDCOMP_CANTREMOVE);
                   2216:       return TRUE;
                   2217:     }
                   2218: #endif /* TEMPLATES */ 
                   2219:   return FALSE;
                   2220: }
1.187     kia      2221: 
                   2222: 

Webmaster