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

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: /*----------------------------------------------------------------------
                    607:   RepeatToBeCreated
                    608:   An new repeat element will be created by the user through some generic editing
                    609:   command
                    610:   -----------------------------------------------------------------------*/
                    611: ThotBool RepeatToBeCreated (NotifyElement *event)
                    612: {
                    613:   printf("RepeatToBeCreated\n");
                    614:   return FALSE;
                    615: }
                    616: 
                    617: /*----------------------------------------------------------------------
                    618:   RepeatCreated
                    619:   A new "repeat" element has just been created by the user with a generic editing
                    620:   command.
                    621:   -----------------------------------------------------------------------*/
                    622: void RepeatCreated (NotifyElement *event)
                    623: {
                    624:   printf("RepeatCreated\n");  
                    625: }
                    626: 
1.71      quint     627: /*----------------------------------------------------------------------
                    628:   UseToBeCreated
                    629:   An new use element will be created by the user through some generic editing
                    630:   command
                    631:   -----------------------------------------------------------------------*/
                    632: ThotBool UseToBeCreated (NotifyElement *event)
                    633: {
                    634: #ifdef TEMPLATES
1.122     kia       635:   ElementType   parentType;
1.138     vatton    636:   SSchema       templateSSchema = TtaGetSSchema ("Template", event->document);  
1.130     vatton    637:   if (templateSSchema)
1.122     kia       638:   {
1.130     vatton    639:     parentType = TtaGetElementType (event->element);
                    640:     if (parentType.ElSSchema == templateSSchema &&
                    641:         parentType.ElTypeNum == Template_EL_repeat)
                    642:       return !Template_CanInsertRepeatChild (event->element);
                    643:     return TemplateElementWillBeCreated (event);
1.122     kia       644:   }
1.52      vatton    645: #endif /* TEMPLATES */
1.71      quint     646:   return FALSE; /* let Thot perform normal operation */
                    647: }
                    648: 
                    649: /*----------------------------------------------------------------------
                    650:   UseCreated
                    651:   A new "use" element has just been created by the user with a generic editing
                    652:   command.
                    653:   -----------------------------------------------------------------------*/
                    654: void UseCreated (NotifyElement *event)
                    655: {
                    656: #ifdef TEMPLATES
1.148     kia       657:   Document        doc = event->document;
                    658:   Element         el = event->element;
1.117     kia       659:   Element         parent;
                    660:   Element         first;
                    661:   ElementType     parentType;
                    662:   XTigerTemplate  t;
                    663:   SSchema         templateSSchema;
1.130     vatton    664:   char*           types, *text = NULL;
1.148     kia       665: 
1.112     vatton    666:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       667:     return;
                    668: 
1.72      quint     669:   if (TtaGetFirstChild (el))
                    670:     /* this Use element has already some content. It has already been
                    671:        instanciated */
                    672:     return;
1.117     kia       673: 
1.171     kia       674:   t = GetXTigerDocTemplate (doc);
1.71      quint     675:   if (!t)
                    676:     return; // no template ?!?!
1.101     kia       677: 
1.138     vatton    678:   templateSSchema = TtaGetSSchema ("Template", doc);
1.117     kia       679:   parent = TtaGetParent(el);
                    680:   parentType = TtaGetElementType(parent);
1.148     kia       681: 
1.130     vatton    682:   if (parentType.ElSSchema == templateSSchema &&
                    683:       parentType.ElTypeNum == Template_EL_repeat)
1.117     kia       684:   {
1.130     vatton    685:     first = TtaGetFirstChild (parent);
                    686:     if (first)
1.117     kia       687:     {
1.130     vatton    688:       types = GetAttributeStringValueFromNum (first, Template_ATTR_types, NULL);
                    689:       if (types)
1.117     kia       690:       {
1.130     vatton    691:         SetAttributeStringValueWithUndo (el, Template_ATTR_types, types);
                    692:         text = GetAttributeStringValueFromNum (el, Template_ATTR_title, NULL);
                    693:         SetAttributeStringValueWithUndo (first, Template_ATTR_title, text);
                    694:         TtaFreeMemory (text);
                    695:         text = GetAttributeStringValueFromNum (el, Template_ATTR_currentType, NULL);
                    696:         SetAttributeStringValueWithUndo (first, Template_ATTR_currentType, text);
                    697:         TtaFreeMemory (text);
                    698:         TtaFreeMemory (types);
1.117     kia       699:       }
                    700:     }
                    701:   }
                    702: 
1.76      vatton    703:   InstantiateUse (t, el, doc, TRUE);
1.71      quint     704: #endif /* TEMPLATES */
                    705: }
1.29      tollenae  706: 
1.89      kia       707: 
1.98      kia       708: /*----------------------------------------------------------------------
                    709:   Template_CanInsertRepeatChild
                    710:   Test if a xt:repeat child can be inserted (number between params min and max).
                    711:   @param el element (xt:repeat) to test
                    712:   @return True if an element can be inserted.
                    713:   ----------------------------------------------------------------------*/
                    714: ThotBool Template_CanInsertRepeatChild(Element el)
                    715: {
                    716: #ifdef TEMPLATES
1.154     vatton    717:   char     *max;
                    718:   int       maxVal, curVal;
                    719:   Element   child;
1.148     kia       720: 
1.104     kia       721:   max = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
1.105     vatton    722:   if (max)
1.104     kia       723:     {
1.154     vatton    724:       if (!strcmp(max, "*"))
                    725:         {
                    726:           TtaFreeMemory(max);
                    727:           return TRUE;
                    728:         }
                    729:       maxVal = atoi (max);
                    730:       TtaFreeMemory (max);
1.104     kia       731:       curVal = 0;
1.105     vatton    732:       for (child = TtaGetFirstChild(el); child; TtaNextSibling(&child))
1.104     kia       733:         curVal++;
1.154     vatton    734:       return (curVal < maxVal);
1.104     kia       735:     }
                    736:   else
1.98      kia       737:     return TRUE;
                    738: #endif /* TEMPLATES */
                    739:   return FALSE;
                    740: }
1.96      kia       741: 
1.89      kia       742: 
1.92      kia       743: #ifdef TEMPLATES
1.99      kia       744: /*----------------------------------------------------------------------
                    745:   QueryStringFromMenu
                    746:   Show a context menu to query a choice.
                    747:   @param items space-separated choice list string.
                    748:   @return The choosed item string or NULL if none.
                    749:   ----------------------------------------------------------------------*/
1.138     vatton    750: static char* QueryStringFromMenu (Document doc, char* items)
1.90      kia       751: {
                    752:   int nbitems, size;
                    753:   struct menuType *itemlist;
                    754:   char *menuString;
                    755:   char *result = NULL;
1.148     kia       756: 
1.138     vatton    757:   if (!TtaGetDocumentAccessMode (doc))
1.112     vatton    758:     return NULL;
                    759:   if (items == NULL)
1.110     kia       760:     return NULL;
1.138     vatton    761:   size = strlen (items);
                    762:   if (size == 0)
                    763:     return NULL;
1.90      kia       764:   giveItems (items, size, &itemlist, &nbitems);
                    765:   menuString = createMenuString (itemlist, nbitems);
                    766:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    767:                      nbitems, menuString , NULL, false, 'L');
                    768:   TtaFreeMemory (menuString);
                    769:   ReturnOption = -1;
                    770:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    771:   TtaWaitShowProcDialogue ();
                    772:   TtaDestroyDialogue (BaseDialog + OptionMenu);
1.148     kia       773: 
1.112     vatton    774:   if (ReturnOption!=-1)
1.90      kia       775:   {
                    776:     result = TtaStrdup(itemlist[ReturnOption].label);
                    777:   }
                    778:   
                    779:   TtaFreeMemory (itemlist);
                    780:   return result;
                    781: }
1.92      kia       782: #endif /* TEMPLATES */
1.90      kia       783: 
1.158     kia       784: #ifdef AMAYA_DEBUG
                    785: void FillInsertableElemList (Document doc, Element elem, DLList list);
                    786: #endif /* AMAYA_DEBUG */
1.162     vatton    787: /*----------------------------------------------------------------------
                    788:   ----------------------------------------------------------------------*/
                    789: char *Template_GetListTypes (XTigerTemplate t, Element el)
                    790: {
                    791: #ifdef TEMPLATES
                    792:   char  *listtypes = NULL, *types;
                    793: 
                    794:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
                    795:   if (types)
                    796:     listtypes = Template_ExpandTypes (t, types, el, FALSE);
                    797:   return listtypes;
                    798: #endif /* TEMPLATES */
                    799: }
1.158     kia       800: 
1.56      francesc  801: /*----------------------------------------------------------------------
1.138     vatton    802:   BagButtonClicked
                    803:   Called when a bag button is clicked.
                    804:   Can be called for useEl, useSimple or bag.
                    805:   If called for useEl or useSimple, the new element must be added after.
                    806:   If called for bag, the element must be added before all.
                    807:    
                    808:   Shows a menu with all the types that can be used in the bag.
                    809:   ----------------------------------------------------------------------*/
                    810: ThotBool BagButtonClicked (NotifyElement *event)
                    811: {
                    812: #ifdef TEMPLATES
                    813:   Document        doc = event->document;
                    814:   Element         el = event->element;
                    815:   ElementType     elType;
                    816:   XTigerTemplate  t;
                    817:   Declaration     decl;
                    818:   Element         bagEl = el;
                    819:   Element         firstEl;
                    820:   Element         newEl = NULL;
                    821:   View            view;
                    822:   SSchema         templateSSchema;
1.162     vatton    823:   char           *listtypes = NULL;
                    824:   char           *result = NULL;
1.138     vatton    825:   ThotBool        oldStructureChecking;
                    826:   DisplayMode     dispMode;
                    827: 
1.182     vatton    828:   if (!TtaGetDocumentAccessMode (doc))
1.138     vatton    829:     return TRUE;
1.182     vatton    830:   if (!IsTemplateInstanceDocument (doc))
                    831:     return FALSE; /* let Thot perform normal operation */
1.148     kia       832: 
1.138     vatton    833:   TtaGetActiveView (&doc, &view);
                    834:   if (view != 1)
                    835:     return FALSE; /* let Thot perform normal operation */
                    836: 
                    837:   TtaCancelSelection (doc);
                    838:   templateSSchema = TtaGetSSchema ("Template", doc);  
1.171     kia       839:   t = GetXTigerDocTemplate(doc);
1.138     vatton    840:   elType = TtaGetElementType (el);
                    841:   while (bagEl &&
                    842:          (elType.ElSSchema != templateSSchema ||
                    843:           elType.ElTypeNum != Template_EL_bag))
                    844:     {
                    845:       bagEl = TtaGetParent (bagEl);
                    846:       elType = TtaGetElementType (bagEl);
                    847:     }
                    848: 
                    849:   if (bagEl)
                    850:   {
1.162     vatton    851:     listtypes = Template_GetListTypes (t, bagEl);
                    852:     if (listtypes)
1.138     vatton    853:       {
1.158     kia       854: #ifdef AMAYA_DEBUG
                    855:       printf("BagButtonClicked : \n  > %s\n", listtypes);
                    856: //      {
                    857: //        DLList list = DLList_Create();
                    858: //        FillInsertableElemList (doc, TtaGetFirstChild(bagEl), list);
                    859: //        DLListNode node;
                    860: //        ForwardIterator iter = DLList_GetForwardIterator(list);
                    861: //        ITERATOR_FOREACH(iter, DLListNode, node)
                    862: //        {
                    863: //          ElemListElement elem = (ElemListElement)node->elem;
                    864: //          printf("  + %s\n", ElemListElement_GetName(elem));
                    865: //        }
                    866: //        DLList_Destroy(list);
                    867: //      }
                    868: #endif /* AMAYA_DEBUG */
1.138     vatton    869:         result = QueryStringFromMenu (doc, listtypes);
                    870:         TtaFreeMemory (listtypes);
                    871:         if (result)
                    872:           {
                    873:             decl = Template_GetDeclaration (t, result);
                    874:             if (decl)
                    875:               {
                    876:                 dispMode = TtaGetDisplayMode (doc);
                    877:                 if (dispMode == DisplayImmediately)
                    878:                   /* don't set NoComputedDisplay
                    879:                      -> it breaks down views formatting when Enter generates new elements  */
1.139     vatton    880:                   TtaSetDisplayMode (doc, DeferredDisplay);
1.138     vatton    881: 
                    882:                 /* Prepare insertion.*/          
                    883:                 oldStructureChecking = TtaGetStructureChecking (doc);
                    884:                 TtaSetStructureChecking (FALSE, doc);
                    885:                 TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    886: 
                    887:                 /* Insert */
                    888:                 if (el == bagEl)
                    889:                   {
                    890:                     el = TtaGetFirstChild (el);
                    891:                     TtaSelectElement (doc, el);
                    892:                     TtaInsertAnyElement (doc, TRUE);
                    893:                   }
                    894:                 else
                    895:                   {
                    896:                     TtaSelectElement (doc, el);
                    897:                     TtaInsertAnyElement (doc, FALSE);
                    898:                   }
1.155     kia       899:                 newEl = Template_InsertBagChild (doc, bagEl, decl, FALSE);
1.138     vatton    900: 
                    901:                 /* Finish insertion.*/
                    902:                 TtaCloseUndoSequence (doc);
                    903:                 TtaSetDocumentModified (doc);
                    904:                 TtaSetStructureChecking (oldStructureChecking, doc);
                    905:                 // restore the display
                    906:                 TtaSetDisplayMode (doc, dispMode);
                    907:                 firstEl = GetFirstEditableElement (newEl);
                    908:                 if (firstEl)
                    909:                   {
                    910:                     TtaSelectElement (doc, firstEl);
                    911:                     TtaSetStatusSelectedElement (doc, view, firstEl);
                    912:                   }
                    913:                 else
                    914:                   {
                    915:                     TtaSelectElement (doc, newEl);
                    916:                     TtaSetStatusSelectedElement (doc, view, newEl);
                    917:                   }
                    918:               }
                    919:           }
                    920:       }
                    921:     TtaFreeMemory (result);
                    922:   }
                    923: #endif /* TEMPLATES */
                    924:   return TRUE; /* don't let Thot perform normal operation */
                    925: }
                    926: 
                    927: /*----------------------------------------------------------------------
1.79      quint     928:   RepeatButtonClicked
1.89      kia       929:   Called when a repeat button is clicked.
                    930:   Can be called for useEl, useSimple or repeat.
                    931:   If called for useEl or useSimple, the new element must be added after.
                    932:   If called for repeat, the element must be added before all.
                    933:    
1.56      francesc  934:   Shows a menu with all the types that can be used in a use element.
                    935:   ----------------------------------------------------------------------*/
1.79      quint     936: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  937: {
                    938: #ifdef TEMPLATES
1.89      kia       939:   Document        doc = event->document;
                    940:   Element         el = event->element;
                    941:   ElementType     elType;
1.90      kia       942:   XTigerTemplate  t;
                    943:   Declaration     decl;
                    944:   Element         repeatEl = el;
                    945:   Element         firstEl;
                    946:   Element         newEl = NULL;
1.95      kia       947:   View            view;
1.162     vatton    948:   char           *listtypes = NULL;
                    949:   char           *result = NULL;
1.138     vatton    950:   ThotBool        oldStructureChecking;
                    951:   DisplayMode     dispMode;
1.104     kia       952: 
1.112     vatton    953:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       954:     return TRUE;
1.182     vatton    955:   if (!IsTemplateInstanceDocument (doc))
                    956:     return FALSE; /* let Thot perform normal operation */
1.148     kia       957: 
1.95      kia       958:   TtaGetActiveView (&doc, &view);
                    959:   if (view != 1)
                    960:     return FALSE; /* let Thot perform normal operation */
                    961: 
1.89      kia       962:   TtaCancelSelection(doc);
1.171     kia       963:   t = GetXTigerDocTemplate(doc);
1.89      kia       964:   elType = TtaGetElementType(el);
1.138     vatton    965:   while (elType.ElTypeNum != Template_EL_repeat)
1.179     kia       966:     {
                    967:       repeatEl = TtaGetParent(repeatEl);
                    968:       if (repeatEl == NULL)
                    969:         break;
                    970:       elType = TtaGetElementType(repeatEl);
                    971:     }
1.112     vatton    972:   if (repeatEl)
1.90      kia       973:     {
1.179     kia       974:       if (Template_CanInsertRepeatChild (repeatEl))
                    975:         {
                    976:           firstEl = TtaGetFirstChild (repeatEl);
                    977:           listtypes = Template_GetListTypes (t, firstEl);
                    978:           if (listtypes)
                    979:             {
1.158     kia       980: #ifdef AMAYA_DEBUG
1.179     kia       981:               printf("RepeatButtonClicked : \n  > %s\n", listtypes);
1.158     kia       982: #endif /* AMAYA_DEBUG */
1.179     kia       983: 
                    984:               result = QueryStringFromMenu (doc, listtypes);
                    985:               TtaFreeMemory (listtypes);
                    986:               if (result)
                    987:                 {
                    988:                   decl = Template_GetDeclaration (t, result);
                    989:                   if (decl)
                    990:                     {
                    991:                       dispMode = TtaGetDisplayMode (doc);
                    992:                       if (dispMode == DisplayImmediately)
                    993:                         /* don't set NoComputedDisplay
1.138     vatton    994:                  -> it breaks down views formatting when Enter generates new elements  */
1.179     kia       995:                         TtaSetDisplayMode (doc, DeferredDisplay);
                    996: 
                    997:                       /* Prepare insertion.*/          
                    998:                       oldStructureChecking = TtaGetStructureChecking (doc);
                    999:                       TtaSetStructureChecking (FALSE, doc);
                   1000:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1001:                       /* Insert. */
                   1002:                       if (el == repeatEl)
                   1003:                         newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, NULL);
                   1004:                       else
                   1005:                         newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, el);
1.138     vatton   1006: 
1.179     kia      1007:                       /* Finish insertion.*/
                   1008:                       TtaCloseUndoSequence(doc);
                   1009:                       TtaSetDocumentModified (doc);
                   1010:                       TtaSetStructureChecking (oldStructureChecking, doc);
                   1011: 
                   1012:                       // restore the display
                   1013:                       TtaSetDisplayMode (doc, dispMode);
                   1014:                       firstEl = GetFirstEditableElement (newEl);
                   1015:                       if (firstEl)
                   1016:                         {
                   1017:                           TtaSelectElement (doc, firstEl);
                   1018:                           TtaSetStatusSelectedElement (doc, view, firstEl);
                   1019:                         }
                   1020:                       else
                   1021:                         {
                   1022:                           TtaSelectElement (doc, newEl);
                   1023:                           TtaSetStatusSelectedElement (doc, view, newEl);
                   1024:                         }
                   1025:                     }
                   1026:                 }
1.104     kia      1027:             }
1.179     kia      1028:           TtaFreeMemory(result);
                   1029:           DumpSubtree(repeatEl, doc, 0);
                   1030: 
                   1031:         }
                   1032:       else /* if (Template_CanInsertRepeatChild(repeatEl)) */
                   1033:         {
                   1034:           TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.98      kia      1035:         }
                   1036:     }
1.77      vatton   1037:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc 1038: #endif /* TEMPLATES */
1.94      kia      1039:   return TRUE;
                   1040: }
                   1041: 
                   1042: /*----------------------------------------------------------------------
                   1043:   UseButtonClicked
                   1044:   Shows a menu with all the types that can be used in a use element.
                   1045:   ----------------------------------------------------------------------*/
                   1046: ThotBool UseButtonClicked (NotifyElement *event)
                   1047: {
                   1048: #ifdef TEMPLATES
                   1049:   Document        doc = event->document;
                   1050:   Element         el = event->element;
1.99      kia      1051:   Element         child;
1.94      kia      1052:   ElementType     elType;
                   1053:   XTigerTemplate  t;
                   1054:   Declaration     decl;
                   1055:   Element         firstEl;
                   1056:   Element         newEl = NULL;
                   1057:   char*           types;
                   1058:   ThotBool        oldStructureChecking;
1.95      kia      1059:   View            view;
1.133     vatton   1060:   char*           listtypes = NULL;
                   1061:   char*           result = NULL;
1.95      kia      1062: 
1.182     vatton   1063:   if (!TtaGetDocumentAccessMode (doc))
1.110     kia      1064:     return TRUE;
1.182     vatton   1065:   if (!IsTemplateInstanceDocument (doc))
                   1066:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1067:     
1.95      kia      1068:   TtaGetActiveView (&doc, &view);
                   1069:   if (view != 1)
                   1070:     return FALSE; /* let Thot perform normal operation */
1.148     kia      1071: 
1.94      kia      1072:   TtaCancelSelection(doc);
1.171     kia      1073:   t = GetXTigerDocTemplate(doc);
1.94      kia      1074:   if (!t)
                   1075:     return FALSE; /* let Thot perform normal operation */
                   1076:   elType = TtaGetElementType(el);
                   1077: 
                   1078:   firstEl = TtaGetFirstChild(el);
1.112     vatton   1079:   if (firstEl)
1.94      kia      1080:   {
                   1081:     RepeatButtonClicked(event);
                   1082:   }
                   1083:   else
                   1084:   {
1.104     kia      1085:     types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.112     vatton   1086:     if (types)
1.94      kia      1087:     {
1.142     kia      1088:       listtypes = Template_ExpandTypes(t, types, NULL, FALSE);
1.158     kia      1089: #ifdef AMAYA_DEBUG
                   1090:       printf("UseButtonClicked : \n  > %s\n", listtypes);
                   1091: #endif /* AMAYA_DEBUG */
                   1092:       
1.104     kia      1093:       result = QueryStringFromMenu(doc, listtypes);
1.112     vatton   1094:       if (result)
1.94      kia      1095:       {
1.104     kia      1096:         decl = Template_GetDeclaration(t, result);
1.112     vatton   1097:         if (decl)
1.99      kia      1098:         {
1.104     kia      1099:           /* Prepare insertion.*/
                   1100:           oldStructureChecking = TtaGetStructureChecking (doc);
                   1101:           TtaSetStructureChecking (FALSE, doc);
1.138     vatton   1102:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104     kia      1103:           
                   1104:           /* Insert */
                   1105:           newEl = Template_InsertUseChildren(doc, el, decl);
                   1106:           for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
                   1107:           {
                   1108:             TtaRegisterElementCreate(child, doc);
                   1109:           }
                   1110:           
                   1111:           TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
                   1112:           TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
                   1113:           
1.117     kia      1114:           /* xt:currentType attribute.*/
                   1115:           SetAttributeStringValueWithUndo(el, Template_ATTR_currentType, result);
                   1116:           
1.104     kia      1117:           /* Finish insertion. */
                   1118:           TtaCloseUndoSequence(doc);
1.114     vatton   1119:           TtaSetDocumentModified (doc);
1.104     kia      1120:           TtaSetStructureChecking (oldStructureChecking, doc);
                   1121:           
                   1122:           firstEl = GetFirstEditableElement(newEl);
1.112     vatton   1123:           if (firstEl)
1.104     kia      1124:           {
                   1125:             TtaSelectElement (doc, firstEl);
                   1126:             TtaSetStatusSelectedElement(doc, view, firstEl);
                   1127:           }
                   1128:           else
                   1129:           {
                   1130:             TtaSelectElement (doc, newEl);
                   1131:             TtaSetStatusSelectedElement(doc, view, newEl);
                   1132:           }
1.98      kia      1133:         }
1.94      kia      1134:       }
                   1135:     }
1.104     kia      1136:     TtaFreeMemory(types);
1.94      kia      1137:     TtaFreeMemory(listtypes);
                   1138:     TtaFreeMemory(result);
                   1139:   }
1.148     kia      1140: 
1.94      kia      1141:   return TRUE;
                   1142: #endif /* TEMPLATES */
1.56      francesc 1143:        return TRUE;
                   1144: }
1.64      francesc 1145: 
1.89      kia      1146: 
1.103     kia      1147: /*----------------------------------------------------------------------
                   1148:   UseSimpleButtonClicked
                   1149:   ----------------------------------------------------------------------*/
                   1150: ThotBool UseSimpleButtonClicked (NotifyElement *event)
                   1151: {
                   1152: #ifdef TEMPLATES
1.181     vatton   1153:   ElementType     elType, parentType;
                   1154:   AttributeType   attrType;
                   1155:   Attribute       att;
                   1156: 
1.182     vatton   1157:   if (!TtaGetDocumentAccessMode (event->document))
1.110     kia      1158:     return TRUE;
1.182     vatton   1159:   if (!IsTemplateInstanceDocument (event->document))
                   1160:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1161: 
1.181     vatton   1162:   elType = TtaGetElementType (event->element);
                   1163:   attrType.AttrSSchema = elType.ElSSchema;
                   1164:   attrType.AttrTypeNum = Template_ATTR_option;
                   1165:   att = TtaGetAttribute (event->element, attrType);
                   1166:   if (att)
                   1167:     return OptionButtonClicked (event);
                   1168:   
                   1169:   parentType = TtaGetElementType (TtaGetParent( event->element));
1.112     vatton   1170:   if (parentType.ElTypeNum == Template_EL_repeat)
1.138     vatton   1171:     return RepeatButtonClicked (event);
                   1172:   else if (parentType.ElTypeNum == Template_EL_bag)
                   1173:     return BagButtonClicked (event);
1.103     kia      1174: #endif /* TEMPLATES */  
                   1175:   return FALSE;
                   1176: }
1.94      kia      1177: 
                   1178: /*----------------------------------------------------------------------
                   1179:   OptionButtonClicked
                   1180:   ----------------------------------------------------------------------*/
                   1181: ThotBool OptionButtonClicked (NotifyElement *event)
                   1182: {
                   1183: #ifdef TEMPLATES
1.145     kia      1184:   Element         useEl, contentEl, next;
1.182     vatton   1185:   ElementType     useType;
1.94      kia      1186:   Document        doc;
                   1187:   XTigerTemplate  t;
                   1188:   View            view;
                   1189: 
1.182     vatton   1190:   if (!TtaGetDocumentAccessMode (event->document))
1.110     kia      1191:     return TRUE;
1.182     vatton   1192:   if (!IsTemplateInstanceDocument (event->document))
                   1193:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1194: 
1.94      kia      1195:   TtaGetActiveView (&doc, &view);
                   1196:   if (view != 1)
                   1197:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1198: 
1.94      kia      1199:   doc = event->document;
1.182     vatton   1200:   useEl = event->element;
1.145     kia      1201:   if (!useEl)
1.94      kia      1202:     return FALSE; /* let Thot perform normal operation */
1.145     kia      1203:   useType = TtaGetElementType (useEl);
1.182     vatton   1204:   if (useType.ElTypeNum != Template_EL_useEl &&
                   1205:       useType.ElTypeNum != Template_EL_useSimple)
1.94      kia      1206:     return FALSE;
                   1207: 
1.145     kia      1208:   TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
1.94      kia      1209:   TtaCancelSelection (doc);
1.145     kia      1210:   contentEl = TtaGetFirstChild (useEl);
                   1211:   if (!contentEl)
1.94      kia      1212:     /* the "use" element is empty. Instantiate it */
                   1213:     {
1.171     kia      1214:       t = GetXTigerDocTemplate (doc);
1.94      kia      1215:       if (!t)
                   1216:         return FALSE; // no template ?!?!
1.145     kia      1217:       InstantiateUse (t, useEl, doc, TRUE);
1.94      kia      1218:     }
                   1219:   else
                   1220:     /* remove the content of the "use" element */
                   1221:     {
                   1222:       do
                   1223:         {
1.145     kia      1224:           next = contentEl;
1.94      kia      1225:           TtaNextSibling (&next);
1.145     kia      1226:           TtaRegisterElementDelete(contentEl, doc);
                   1227:           TtaDeleteTree (contentEl, doc);
                   1228:           contentEl = next;
1.94      kia      1229:         }
                   1230:       while (next);
1.145     kia      1231:       if (NeedAMenu (useEl, doc))
                   1232:         {
                   1233:           TtaChangeTypeOfElement (useEl, doc, Template_EL_useEl);
                   1234:           TtaRegisterElementTypeChange(useEl, Template_EL_useSimple, doc);
                   1235:         }
1.94      kia      1236:     }
                   1237:   TtaSelectElement (doc, event->element);
1.145     kia      1238:   TtaCloseUndoSequence(doc);
1.94      kia      1239:   return TRUE; /* don't let Thot perform normal operation */
                   1240: #endif /* TEMPLATES */
                   1241:   return TRUE;
                   1242: }
                   1243: 
1.111     vatton   1244: /*----------------------------------------------------------------------
                   1245:   CheckTemplate checks if the template of the instance is loaded
1.126     vatton   1246:   Return TRUE if the template is loaded
1.111     vatton   1247:   ----------------------------------------------------------------------*/
                   1248: void CheckTemplate (Document doc)
                   1249: {
                   1250: #ifdef TEMPLATES
1.144     vatton   1251:   Element    root;
1.165     kia      1252:   
1.183     vatton   1253:   if (IsTemplateInstanceDocument(doc))
1.111     vatton   1254:     {
1.144     vatton   1255:       XTigerTemplate   t;
                   1256: 
                   1257:       root = TtaGetRootElement (doc);
                   1258:       TtaSetAccessRight (root, ReadOnly, doc);
1.171     kia      1259:       t = GetXTigerDocTemplate (doc);
1.144     vatton   1260:       if (t == NULL)
                   1261:         {
                   1262:           // the template cannot be loaded
                   1263:           InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                   1264:           TtaSetDocumentAccessMode (doc, 0); // document readonly
                   1265:         }
                   1266:       else
                   1267:         {
                   1268:           // fix all access rights in the instance
1.171     kia      1269:           Template_PrepareTemplate(t);
1.166     kia      1270:           Template_FixAccessRight (t, root, doc);
1.144     vatton   1271:           TtaUpdateAccessRightInViews (doc, root);
                   1272:         }
1.111     vatton   1273:     }
                   1274: #endif /* TEMPLATES */
                   1275: }
1.94      kia      1276: 
1.66      vatton   1277: /*----------------------------------------------------------------------
1.108     vatton   1278:   OpeningInstance checks if it is a template instance needs.
                   1279:   If it's an instance and the template is not loaded, load it into a
                   1280:   temporary file
1.66      vatton   1281:   ----------------------------------------------------------------------*/
1.171     kia      1282: void OpeningInstance (char *localFileName, Document doc, char* docURL)
1.65      francesc 1283: {
                   1284: #ifdef TEMPLATES
1.76      vatton   1285:   XTigerTemplate   t;
1.103     kia      1286:   char            *content, *ptr, *begin;
1.76      vatton   1287:   gzFile           stream;
                   1288:   char             buffer[2000];
1.77      vatton   1289:   int              res;
1.171     kia      1290:   char            *template_version = NULL,
                   1291:                   *template_url = NULL;
1.65      francesc 1292: 
1.171     kia      1293:   stream = TtaGZOpen (localFileName);
1.76      vatton   1294:   if (stream != 0)
1.65      francesc 1295:     {
1.76      vatton   1296:       res = gzread (stream, buffer, 1999);
                   1297:       if (res >= 0)
1.65      francesc 1298:         {
1.81      vatton   1299:           buffer[res] = EOS;
1.103     kia      1300:           begin = strstr (buffer, "<?xtiger");
                   1301:           
1.112     vatton   1302:           if (begin)
1.103     kia      1303:           {
                   1304:             // Search for template version
                   1305:             ptr = strstr (begin, "templateVersion");
                   1306:             if (ptr)
                   1307:               ptr = strstr (ptr, "=");
                   1308:             if (ptr)
                   1309:               ptr = strstr (ptr, "\"");
                   1310:             if (ptr)
                   1311:               {
                   1312:                 // template URI
                   1313:                 content = &ptr[1];
                   1314:                 ptr = strstr (content, "\"");
                   1315:               }
                   1316:             if (ptr)
                   1317:               {
                   1318:                 *ptr = EOS;
                   1319:                 //Get now the template URI
1.171     kia      1320:                 template_version = TtaStrdup (content);
1.103     kia      1321:                 *ptr = '"';
                   1322:               }
                   1323:            
                   1324:             // Search for template uri
                   1325:             ptr = strstr (begin, "template");
                   1326:             if (ptr && ptr[8] != 'V')
                   1327:               ptr = strstr (ptr, "=");
                   1328:             if (ptr)
                   1329:               ptr = strstr (ptr, "\"");
                   1330:             if (ptr)
                   1331:               {
                   1332:                 // template URI
                   1333:                 content = &ptr[1];
                   1334:                 ptr = strstr (content, "\"");
                   1335:               }
                   1336:             if (ptr)
                   1337:               {
                   1338:                 *ptr = EOS;
                   1339:                 //Get now the template URI
1.171     kia      1340:                 template_url = TtaStrdup (content);
                   1341: 
                   1342:                 t = GetXTigerTemplate (template_url);
1.103     kia      1343:                 if (!t)
                   1344:                   {
1.171     kia      1345:                     LoadTemplate (doc, template_url);
                   1346:                     t = GetXTigerTemplate(template_url);
1.103     kia      1347:                   }
1.171     kia      1348:                 Template_PrepareInstance(docURL, doc, template_version, template_url);
                   1349:                 template_version = NULL;
                   1350:                 template_url     = NULL;
                   1351:                 Template_AddReference (t);
1.103     kia      1352:                 *ptr = '"';
                   1353:               }
                   1354:           }
1.65      francesc 1355:         }
                   1356:     }
1.171     kia      1357:   TtaFreeMemory(template_version);
                   1358:   TtaFreeMemory(template_url);
1.76      vatton   1359:   TtaGZClose (stream);
1.65      francesc 1360: #endif /* TEMPLATES */
                   1361: }
                   1362: 
1.64      francesc 1363: /*----------------------------------------------------------------------
1.178     kia      1364:   ClosingTemplateDocument
                   1365:   Callback called before closing a document which uses templates.
1.64      francesc 1366:   ----------------------------------------------------------------------*/
1.178     kia      1367: ThotBool ClosingTemplateDocument(NotifyDialog* dialog)
1.64      francesc 1368: {
1.65      francesc 1369: #ifdef TEMPLATES
1.171     kia      1370:   XTigerTemplate t = GetXTigerDocTemplate(dialog->document);
                   1371:   if(t)
1.178     kia      1372:       Template_RemoveReference(t);
1.65      francesc 1373: #endif /* TEMPLATES */
                   1374:   return FALSE;
1.64      francesc 1375: }
1.87      kia      1376: 
                   1377: /*----------------------------------------------------------------------
1.120     kia      1378:   IsTemplateElement
1.138     vatton   1379:   Test if an element is a template element.
1.87      kia      1380:   ----------------------------------------------------------------------*/
1.140     vatton   1381: ThotBool IsTemplateElement (Element elem)
1.87      kia      1382: {
                   1383: #ifdef TEMPLATES
1.138     vatton   1384:   ElementType     elType;
                   1385: 
                   1386:   elType = TtaGetElementType(elem);
                   1387:   if (elType.ElSSchema)
1.164     kia      1388:     return (strcmp(TtaGetSSchemaName(elType.ElSSchema) , "Template") == 0);
1.138     vatton   1389: #endif /* TEMPLATES */
1.87      kia      1390:   return FALSE;
                   1391: }
                   1392: 
                   1393: 
                   1394: /*----------------------------------------------------------------------
                   1395:   GetFirstTemplateParentElement
1.138     vatton   1396:   Return the first element which has "Template" as schema name or null.
1.87      kia      1397:   ----------------------------------------------------------------------*/
                   1398: Element GetFirstTemplateParentElement(Element elem)
                   1399: {
                   1400: #ifdef TEMPLATES
1.138     vatton   1401:   ElementType     elType;
                   1402: 
                   1403:   elem = TtaGetParent (elem);
                   1404:   elType = TtaGetElementType(elem);
                   1405:   while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
                   1406: {
                   1407:     elem = TtaGetParent (elem);
                   1408:     elType = TtaGetElementType(elem);
1.87      kia      1409:   }
                   1410:   return elem;
                   1411: #else
                   1412:   return NULL;
                   1413: #endif /* TEMPLATES */
                   1414: }
1.101     kia      1415: 
1.103     kia      1416: 
1.101     kia      1417: /*----------------------------------------------------------------------
1.102     vatton   1418:   TemplateElementWillBeCreated
1.101     kia      1419:   Processed when an element will be created in a template context.
                   1420:   ----------------------------------------------------------------------*/
1.102     vatton   1421: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101     kia      1422: {
1.103     kia      1423: #ifdef TEMPLATES
                   1424:   ElementType elType = event->elementType;
                   1425:   Element     parent = event->element;
                   1426:   ElementType parentType = TtaGetElementType(parent);
1.113     kia      1427:   Element     ancestor;
                   1428:   ElementType ancestorType;
                   1429:   SSchema     templateSSchema;
                   1430:   char*       types;
                   1431:   ThotBool    b;
1.101     kia      1432: 
1.115     kia      1433:   if(event->info==1)
                   1434:     return FALSE;
                   1435: 
1.185     kia      1436:   printf("TemplateElementWillBeCreated\n");
                   1437:   
1.112     vatton   1438:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1439:     return TRUE;
                   1440: 
1.138     vatton   1441:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.102     vatton   1442:   if (templateSSchema == NULL)
                   1443:     return FALSE; // let Thot do the job
1.115     kia      1444: 
1.113     kia      1445:   // Fisrt, test if in a xt:bag or in a base-element xt:use
1.147     vatton   1446:   if(parentType.ElSSchema == templateSSchema)
1.113     kia      1447:     ancestor = parent;
                   1448:   else
1.147     vatton   1449:     ancestor = GetFirstTemplateParentElement (parent);
1.113     kia      1450: 
1.147     vatton   1451:   if (ancestor)
1.113     kia      1452:   {
                   1453:     ancestorType = TtaGetElementType(ancestor);
1.147     vatton   1454:     if (ancestorType.ElTypeNum == Template_EL_bag)
1.113     kia      1455:     {
1.147     vatton   1456:       // only check the use child
                   1457:       if (ancestor != parent)
                   1458:         return  FALSE; // let Thot do the job
                   1459:       if (elType.ElSSchema == templateSSchema &&
                   1460:           (elType.ElTypeNum == Template_EL_useSimple ||
                   1461:            elType.ElTypeNum == Template_EL_useEl))
1.116     kia      1462:         return FALSE;
1.147     vatton   1463:       return !Template_CanInsertElementInBagElement (event->document, elType, ancestor);      
1.113     kia      1464:     }
1.147     vatton   1465:     else if(ancestorType.ElTypeNum == Template_EL_useSimple ||
                   1466:             ancestorType.ElTypeNum == Template_EL_useEl)
1.113     kia      1467:     {
1.147     vatton   1468:       // only check the bag child @@@ will be check exclude/include later
1.172     kia      1469:       if (ancestor != parent)
                   1470:         return  FALSE; // let Thot do the job
1.113     kia      1471:       types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
1.128     kia      1472:       b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position);
                   1473:       TtaFreeMemory(types);
1.115     kia      1474:       return !b;
1.113     kia      1475:     }
                   1476:   }
1.115     kia      1477:   
1.147     vatton   1478:   if (elType.ElSSchema == templateSSchema && elType.ElTypeNum == Template_EL_TEXT_UNIT)
1.115     kia      1479:   {
                   1480:     return FALSE;
                   1481:   }
                   1482:   
1.113     kia      1483:   // Can not insert.
                   1484:   return TRUE;
1.101     kia      1485: #endif /* TEMPLATES*/
1.102     vatton   1486:   return FALSE;
1.101     kia      1487: }
                   1488: 
                   1489: /*----------------------------------------------------------------------
                   1490:   TemplateElementWillBeDeleted
                   1491:   Processed when an element will be deleted in a template context.
                   1492:   ----------------------------------------------------------------------*/
                   1493: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1494: {
1.107     kia      1495: #ifdef TEMPLATES
                   1496:   Document       doc = event->document;
                   1497:   Element        elem = event->element;
1.163     vatton   1498:   Element        xtElem, parent = NULL, sibling;
1.117     kia      1499:   ElementType    xtType, elType;
1.107     kia      1500:   char*          type;
                   1501:   Declaration    dec;
1.115     kia      1502:   SSchema        templateSSchema;
1.107     kia      1503:   XTigerTemplate t;
1.156     vatton   1504:   ThotBool       selparent = FALSE;
1.107     kia      1505: 
1.115     kia      1506:   if(event->info==1)
                   1507:     return FALSE;
                   1508: 
1.112     vatton   1509:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1510:     return TRUE;
                   1511: 
1.138     vatton   1512:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.107     kia      1513:   if (templateSSchema == NULL)
                   1514:     return FALSE; // let Thot do the job
1.122     kia      1515: 
1.107     kia      1516:   xtElem = GetFirstTemplateParentElement(elem);
1.112     vatton   1517:   if (xtElem)
1.107     kia      1518:   {
                   1519:     xtType = TtaGetElementType(xtElem);
1.117     kia      1520:     
1.171     kia      1521:     t = GetXTigerDocTemplate(doc);
1.109     kia      1522: 
1.112     vatton   1523:     if (xtType.ElTypeNum==Template_EL_bag)
1.117     kia      1524:     {
                   1525:       elType = TtaGetElementType(elem);
                   1526:       if(elType.ElSSchema==templateSSchema &&
                   1527:         (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
                   1528:       {
                   1529:         // Remove element manually.
                   1530:         TtaOpenUndoSequence(doc, elem, elem, 0, 0);
                   1531:         TtaRegisterElementDelete(elem, doc);
                   1532:         TtaDeleteTree(elem, doc);
                   1533:         TtaCloseUndoSequence(doc);
                   1534:         return TRUE;
                   1535:       }
                   1536:       else
                   1537:         return FALSE; // xt:bag always allow remove children.
                   1538:     }
1.112     vatton   1539:     else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107     kia      1540:     {
1.109     kia      1541:       parent = TtaGetParent(elem);
1.117     kia      1542:       if (xtElem!=parent)
                   1543:       {
                   1544:         type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
                   1545:         dec = Template_GetDeclaration(t, type);
                   1546:         TtaFreeMemory(type);
                   1547:         
                   1548:         if (dec && dec->nature == XmlElementNat)
                   1549:           return FALSE; // Can remove element only if in xt:use current type is base language element. 
                   1550:         else
                   1551:           return TRUE;
1.107     kia      1552:       }
1.109     kia      1553:     }
1.112     vatton   1554:     else if (xtType.ElTypeNum==Template_EL_repeat)
1.109     kia      1555:     {
1.156     vatton   1556:       sibling = TtaGetSuccessor (elem);
                   1557:       if (sibling == NULL)
                   1558:         {
                   1559:           // there is no next element
                   1560:           sibling = TtaGetPredecessor (elem);
                   1561:           if (sibling == NULL)
                   1562:             selparent = TRUE;
                   1563:         }
1.109     kia      1564:       TtaRegisterElementDelete(elem, doc);
                   1565:       TtaDeleteTree(elem, doc);
                   1566:       InstantiateRepeat(t, xtElem, doc, TRUE);
1.156     vatton   1567:       if (selparent)
                   1568:         // look for the new sibling
                   1569:         sibling = TtaGetFirstChild (parent);
                   1570:       if (sibling)
                   1571:         TtaSelectElement(doc, sibling);
1.151     quint    1572:       else
1.156     vatton   1573:         TtaSelectElement(doc, parent);
1.109     kia      1574:       return TRUE;
1.107     kia      1575:     }
                   1576:   }
1.109     kia      1577:   
                   1578:   //TODO Test if current element is use or repeat.
                   1579:   // Because if an element is delete and it is the unique child of its parent,
                   1580:   // the parent intends to destroy itself. 
                   1581:   
1.107     kia      1582:   return TRUE;
                   1583: #else /* TEMPLATES */
1.101     kia      1584:   return FALSE;
1.107     kia      1585: #endif /* TEMPLATES */
1.101     kia      1586: }
                   1587: 
1.109     kia      1588: /*----------------------------------------------------------------------
                   1589:   CurrentTypeWillBeExported
                   1590:   Check if the xt:currentType attribute can be exported
                   1591:   ----------------------------------------------------------------------*/
                   1592: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
                   1593: {
                   1594: #ifdef TEMPLATES
1.110     kia      1595: 
1.112     vatton   1596:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1597:     return TRUE;
                   1598: 
1.112     vatton   1599:   if (IsTemplateDocument(event->document))
1.109     kia      1600:     return TRUE;
                   1601: #endif /* TEMPLATES */
                   1602:   return FALSE;
                   1603: }
1.127     kia      1604: 
                   1605: /*----------------------------------------------------------------------
                   1606:   TemplateAttrInMenu
                   1607:   Called by Thot when building the Attributes menu for template elements.
                   1608:   ----------------------------------------------------------------------*/
                   1609: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
                   1610: {
                   1611: #ifdef TEMPLATES
                   1612:   // Prevent from showing attributes for template instance but not templates.
                   1613:   if(IsTemplateInstanceDocument(event->document))
                   1614:     return TRUE;
                   1615:   else
                   1616: #endif /* TEMPLATES */
                   1617:     return FALSE;
                   1618: }
1.160     kia      1619: 
1.167     kia      1620: /*----------------------------------------------------------------------
1.168     kia      1621:   CreateTemplateFromDocument
                   1622:   Create a template from the current document.
1.167     kia      1623:   ----------------------------------------------------------------------*/
                   1624: void CreateTemplateFromDocument(Document doc, View view)
                   1625: {
1.171     kia      1626: #ifdef TEMPLATES
1.168     kia      1627:   char buffer[MAX_LENGTH];
                   1628:   strcpy(buffer, DocumentURLs[doc]);
                   1629:   strcat(buffer, ".xtd");
                   1630:   DontReplaceOldDoc = TRUE;
                   1631:   CreateTemplate(doc, buffer);
1.171     kia      1632: #endif /* TEMPLATES */
1.167     kia      1633: }
                   1634: 
1.169     kia      1635: /*----------------------------------------------------------------------
                   1636:   UpdateTemplateMenus
                   1637:   ----------------------------------------------------------------------*/
                   1638: void UpdateTemplateMenus (Document doc)
                   1639: {
1.180     kia      1640:   if(IsTemplateInstanceDocument(doc))
                   1641:     {
                   1642:       // Instance document
                   1643:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1644:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1645:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.180     kia      1646:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
                   1647:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1648:     }
1.183     vatton   1649:   else if (DocumentURLs[doc] && IsXTiger (DocumentURLs[doc]))
1.180     kia      1650:     {
                   1651:       // Template document
                   1652:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1653:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1654:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseBox);
1.180     kia      1655:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateFreeBox);      
                   1656:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeat);
                   1657:     }
1.169     kia      1658:   else
1.180     kia      1659:     {
                   1660:       //Standard document
                   1661:       TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
                   1662:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1663:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.180     kia      1664:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
                   1665:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1666:     }
1.169     kia      1667: }
1.171     kia      1668: 
                   1669: /*----------------------------------------------------------------------
                   1670:   UninstanciateTemplateDocument
                   1671:   An instance of a template is tranformed into a template-less docuemnt.
                   1672:   Remove link between XTigerTemplate structure and document.
                   1673:   ----------------------------------------------------------------------*/
                   1674: void UninstanciateTemplateDocument(Document doc)
                   1675: {
                   1676: #ifdef TEMPLATES
                   1677:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1678:   if(t)
                   1679:     Template_Close(t);
                   1680: #endif /* TEMPLATES */  
                   1681: }
                   1682: 
                   1683: 
                   1684: /*----------------------------------------------------------------------
                   1685:   Template_PrepareInstance
                   1686:   Allocate XTigerTemplate structure for instance and initialize template
                   1687:   url and template version.
                   1688:   ----------------------------------------------------------------------*/
                   1689: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
                   1690: {
                   1691: #ifdef TEMPLATES
                   1692:   XTigerTemplate t = GetXTigerTemplate(fileName);
                   1693:   if(!t)
                   1694:     t = NewXTigerTemplate(fileName);
                   1695:   t->state           = templInstance;
                   1696:   t->templateVersion = template_version;
                   1697:   t->base_uri        = template_url;
                   1698:   t->doc             = doc;
1.178     kia      1699:   t->ref             = 1;
1.171     kia      1700:   
                   1701: #endif /* TEMPLATES */
                   1702: }
                   1703: 
                   1704: 
                   1705: /*----------------------------------------------------------------------
                   1706:   SetDocumentAsXTigerTemplate
                   1707:   Set the document template structure as template.
                   1708:   ----------------------------------------------------------------------*/
                   1709: void SetDocumentAsXTigerTemplate(Document doc)
                   1710: {
                   1711: #ifdef TEMPLATES
                   1712:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1713:   if(t)
                   1714:     t->state |= templTemplate;
                   1715: #endif /* TEMPLATES */  
                   1716: }
                   1717: 
                   1718: /*----------------------------------------------------------------------
                   1719:   SetDocumentAsXTigerLibrary
                   1720:   Set the document template structure as template library.
                   1721:   ----------------------------------------------------------------------*/
                   1722: void SetDocumentAsXTigerLibrary(Document doc)
                   1723: {
                   1724: #ifdef TEMPLATES
                   1725:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1726:   if(t)
                   1727:     t->state |= templLibrary;
                   1728: #endif /* TEMPLATES */  
                   1729: }
1.174     kia      1730: 
                   1731: 
                   1732: /*----------------------------------------------------------------------
                   1733:   TemplateCreateTextBox
                   1734:   Create a xt:use types="string" box around the selection.
                   1735:   ----------------------------------------------------------------------*/
                   1736: void TemplateCreateTextBox(Document doc, View view)
                   1737: {
                   1738: #ifdef TEMPLATES
                   1739:   Element     selElem;
                   1740:   ElementType selType;
                   1741:   int         firstChar, lastChar;
                   1742:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1743:   ElementType useType;
                   1744:   Element     use;
                   1745:   char        buffer[128];
                   1746:   
                   1747:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
1.184     vatton   1748:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174     kia      1749: 
                   1750:   if (!TtaGetDocumentAccessMode(doc))
                   1751:     return;
                   1752:   
                   1753:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1754:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1755:     {
                   1756:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1757:       if(selElem)
                   1758:         {
                   1759:           selType =  TtaGetElementType(selElem);
                   1760:           if(!TtaIsLeaf(selType))
                   1761:             {
                   1762:               selElem = TtaGetFirstLeaf(selElem);
                   1763:               selType = TtaGetElementType(selElem);
                   1764:               firstChar = lastChar = 0;
                   1765:             }
                   1766:           
                   1767:           if(selType.ElTypeNum==1)
                   1768:             {
                   1769:               QueryStringFromUser(label, title, buffer, 127);
                   1770:               useType.ElSSchema = sstempl;
                   1771:               useType.ElTypeNum = Template_EL_useSimple;
                   1772: 
                   1773:               if(firstChar==0)
                   1774:                 {
                   1775:                   use = TtaNewElement(doc, useType);
                   1776:                   if(use)
                   1777:                     {
                   1778:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1779:                       TtaInsertSibling(use, selElem, FALSE, doc);
                   1780:                       TtaRegisterElementCreate(use, doc);
                   1781:                       TtaRegisterElementDelete (selElem, doc);
                   1782:                       TtaRemoveTree(selElem, doc);
                   1783:                       TtaInsertFirstChild(&selElem, use, doc);
                   1784:                       TtaRegisterElementDelete (selElem, doc);
                   1785:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1786:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);
                   1787:                       TtaCloseUndoSequence(doc);
                   1788:                       TtaSelectElement(doc, use);
                   1789:                     }
                   1790:                 }
                   1791:               else
                   1792:                 {
                   1793:                   GenerateInlineElement(Template_EL_useSimple, sstempl, 0, "", TRUE);
                   1794:                   TtaGiveFirstSelectedElement(doc, &use, &firstChar, &lastChar);
                   1795:                   selType =  TtaGetElementType(use);
                   1796:                   if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_useSimple)
                   1797:                     {
                   1798:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1799:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);                      
                   1800:                     }
                   1801:                 }
                   1802:             }
                   1803:           
                   1804:         }
                   1805:     }
                   1806: #endif /* TEMPLATES */
                   1807: }
                   1808: 
                   1809: 
                   1810: /*----------------------------------------------------------------------
                   1811:   TemplateCreateFreeBox
                   1812:   Create a xt:bag types="string" box around the selection.
                   1813:   ----------------------------------------------------------------------*/
                   1814: void TemplateCreateFreeBox(Document doc, View view)
                   1815: {
                   1816: #ifdef TEMPLATES
1.185     kia      1817:   Element     selElem, selElem2, parent, parent2, current, child = NULL;
1.174     kia      1818:   ElementType selType, selType2;
                   1819:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1820:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1821: 
                   1822:   ElementType bagType;
                   1823:   Element     bag;
                   1824:   char        buffer[128];
                   1825: 
                   1826:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
1.184     vatton   1827:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174     kia      1828: 
                   1829:   if (!TtaGetDocumentAccessMode(doc))
                   1830:     return;
                   1831:   
                   1832:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1833:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1834:     {
                   1835:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1836:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   1837:       
                   1838:       if(selElem && selElem2)
                   1839:         {
                   1840:           selType =  TtaGetElementType(selElem);
                   1841:           selType2 =  TtaGetElementType(selElem2);
                   1842: 
                   1843:           QueryStringFromUser(label, title, buffer, 127);
                   1844:           bagType.ElSSchema = sstempl;
                   1845:           bagType.ElTypeNum = Template_EL_bag;
                   1846:           
1.185     kia      1847:           parent  = TtaGetParent(selElem);
                   1848:           parent2 = TtaGetParent(selElem2);
                   1849:           
                   1850:           if(firstChar==0 && firstChar2==0 && parent == parent2)
1.174     kia      1851:             {
1.185     kia      1852:               ThotBool        oldStructureChecking;
                   1853:               DisplayMode     dispMode;
                   1854:               dispMode = TtaGetDisplayMode (doc);
                   1855:               if (dispMode == DisplayImmediately)
                   1856:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   1857:               oldStructureChecking = TtaGetStructureChecking (doc);
                   1858:               TtaSetStructureChecking (FALSE, doc);
                   1859: 
                   1860:               // Create and insert xt:bag element
                   1861:               bag = TtaNewElement(doc, bagType);
                   1862:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1863:               TtaInsertSibling(bag, selElem, TRUE, doc);
                   1864:               TtaRegisterElementCreate(bag, doc);
                   1865: 
                   1866:               TtaNextSibling(&selElem2);
                   1867:               while(selElem!=selElem2)
1.174     kia      1868:                 {
1.185     kia      1869:                   current = selElem;
                   1870:                   TtaNextSibling(&selElem);
1.174     kia      1871:                   
1.185     kia      1872:                   TtaRegisterElementDelete (current, doc);
                   1873:                   TtaRemoveTree(current, doc);
                   1874:                   if(child)
                   1875:                     TtaInsertSibling(current, child, FALSE, doc);
                   1876:                   else
                   1877:                     TtaInsertFirstChild(&current, bag, doc);
                   1878:                   TtaRegisterElementDelete (current, doc);
                   1879:                   child = current;
1.174     kia      1880:                 }
1.185     kia      1881:               
                   1882:               SetAttributeStringValue(bag, Template_ATTR_types, "any");
                   1883:               SetAttributeStringValue(bag, Template_ATTR_title, buffer);
                   1884:               TtaCloseUndoSequence(doc);
                   1885:               TtaSelectElement(doc, bag);
                   1886:               
                   1887:               TtaSetStructureChecking (oldStructureChecking, doc);
                   1888:               TtaSetDisplayMode (doc, dispMode);
1.174     kia      1889:             }
                   1890:         }
                   1891:     }
                   1892: #endif /* TEMPLATES */
                   1893: }
                   1894: 
1.180     kia      1895: /*----------------------------------------------------------------------
                   1896:   TemplateCreateRepeat
                   1897:   Create a xt:repeat around the selection.
                   1898:   ----------------------------------------------------------------------*/
                   1899: void TemplateCreateRepeat(Document doc, View view)
                   1900: {
                   1901: #ifdef TEMPLATES
1.187     kia      1902:   ThotBool    oldStructureChecking;
                   1903:   DisplayMode dispMode;
                   1904:   Element     selElem, selElem2, parent, parent2;
                   1905:   ElementType selType, selType2;
1.180     kia      1906:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1907:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1908: 
1.186     kia      1909:   ElementType repType;
1.187     kia      1910:   Element     rep, use;
1.180     kia      1911: 
                   1912:   if (!TtaGetDocumentAccessMode(doc))
                   1913:     return;
                   1914:   
                   1915:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1916:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1917:     {
                   1918:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1919:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1.185     kia      1920: 
1.180     kia      1921:       if(selElem && selElem2)
                   1922:         {
                   1923:           selType =  TtaGetElementType(selElem);
                   1924:           selType2 =  TtaGetElementType(selElem2);
                   1925: 
                   1926:           repType.ElSSchema = sstempl;
                   1927:           repType.ElTypeNum = Template_EL_repeat;
1.186     kia      1928:           
1.185     kia      1929:           parent  = TtaGetParent(selElem);
                   1930:           parent2 = TtaGetParent(selElem2);
                   1931: 
                   1932:           if(firstChar==0 && firstChar2==0 && parent == parent2)
1.180     kia      1933:             {
1.187     kia      1934:               dispMode = TtaGetDisplayMode (doc);
                   1935:               if (dispMode == DisplayImmediately)
                   1936:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   1937:               oldStructureChecking = TtaGetStructureChecking (doc);
                   1938:               TtaSetStructureChecking (FALSE, doc);
                   1939:               
                   1940:               if(selElem==selElem2 && selType.ElSSchema==sstempl && 
                   1941:                   (selType.ElTypeNum==Template_EL_useEl || 
                   1942:                    selType.ElTypeNum==Template_EL_useSimple))
                   1943:                 use = selElem;
                   1944:               else
                   1945:                 use = Template_CreateUseFromSelection(doc);
                   1946:               if(use)
                   1947:                 {
                   1948:                   TtaExtendUndoSequence(doc);
                   1949:                   
                   1950:                   rep = TtaNewElement(doc, repType);
                   1951:                   TtaInsertSibling(rep, use, FALSE, doc);
                   1952:                   TtaRegisterElementCreate(rep, doc);
                   1953: 
                   1954:                   TtaRegisterElementDelete (use, doc);
                   1955:                   TtaRemoveTree(use, doc);
                   1956:                   
                   1957:                   TtaInsertFirstChild(&use, rep, doc);
                   1958:                   TtaRegisterElementCreate(use, doc);
                   1959:                   
                   1960:                   TtaSelectElement(doc, use);
                   1961:                   TtaCloseUndoSequence(doc);
                   1962:                 }
                   1963:               
                   1964:               TtaSetStructureChecking (oldStructureChecking, doc);
                   1965:               TtaSetDisplayMode (doc, dispMode);
                   1966:             }
                   1967:         }
                   1968:     }
                   1969: #endif /* TEMPLATES */
                   1970: }
                   1971: 
                   1972: 
                   1973: /*----------------------------------------------------------------------
                   1974:   TemplateCreateUseBox
                   1975:   Create a xt:use around the selection.
                   1976:   ----------------------------------------------------------------------*/
                   1977: void TemplateCreateUseBox(Document doc, View view)
                   1978: {
                   1979: #ifdef TEMPLATES
                   1980:   Template_CreateUseFromSelection(doc);
                   1981: #endif /* TEMPLATES */
                   1982: }
                   1983: 
                   1984: 
                   1985: /*----------------------------------------------------------------------
                   1986:   Template_CreateUseFromSelection
                   1987:   Create a xt:use with the selection.
                   1988:   Return the xt:component element.
                   1989:   ----------------------------------------------------------------------*/
                   1990: Element Template_CreateUseFromSelection(Document doc)
                   1991: {
                   1992: #ifdef TEMPLATES
                   1993:   ThotBool    oldStructureChecking;
                   1994:   DisplayMode dispMode;
                   1995:   Element     selElem, selElem2, parent, parent2;
                   1996:   ElementType selType, selType2;
                   1997:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1998:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1999: 
                   2000:   ElementType useType;
                   2001:   Element     use = NULL, comp, prev;
                   2002:   char        buffer[128];
                   2003:   int         sz = 128;
                   2004: 
                   2005:   if (!TtaGetDocumentAccessMode(doc))
                   2006:     return NULL;
                   2007:   
                   2008:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   2009:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2010:     {
                   2011:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2012:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   2013: 
                   2014:       if(selElem && selElem2)
                   2015:         {
                   2016:           selType =  TtaGetElementType(selElem);
                   2017:           selType2 =  TtaGetElementType(selElem2);
                   2018:           parent  = TtaGetParent(selElem);
                   2019:           parent2 = TtaGetParent(selElem2);
                   2020: 
                   2021:           if(firstChar==0 && firstChar2==0 && parent == parent2)
                   2022:             {
1.185     kia      2023:               dispMode = TtaGetDisplayMode (doc);
                   2024:               if (dispMode == DisplayImmediately)
                   2025:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   2026:               oldStructureChecking = TtaGetStructureChecking (doc);
                   2027:               TtaSetStructureChecking (FALSE, doc);
                   2028:               
1.186     kia      2029:               prev = selElem;
                   2030:               TtaPreviousSibling(&prev);
                   2031:               
1.187     kia      2032:               comp = Template_CreateComponentFromSelection(doc);
1.186     kia      2033:               if(comp)
                   2034:                 {
                   2035:                   TtaExtendUndoSequence(doc);
                   2036:                   
1.187     kia      2037:                   useType.ElSSchema = sstempl;
                   2038:                   useType.ElTypeNum = Template_EL_useEl;
                   2039:                   use = TtaNewElement(doc, useType);
1.186     kia      2040:                   if(prev)
1.187     kia      2041:                     TtaInsertSibling(use, prev, FALSE, doc);
1.186     kia      2042:                   else
1.187     kia      2043:                     TtaInsertFirstChild(&use, parent, doc);
1.186     kia      2044:                   
                   2045:                   GiveAttributeStringValueFromNum(comp, Template_ATTR_name, buffer, &sz);
                   2046:                   SetAttributeStringValue(use, Template_ATTR_types, buffer);
                   2047:                   TtaRegisterElementCreate(use, doc);
                   2048:                   
1.187     kia      2049:                   TtaSelectElement(doc, use);
1.186     kia      2050:                   TtaCloseUndoSequence(doc);
                   2051:                 }
1.185     kia      2052:               
1.186     kia      2053:               TtaSetStructureChecking (oldStructureChecking, doc);
                   2054:               TtaSetDisplayMode (doc, dispMode);
                   2055:             }
                   2056:         }
                   2057:     }
1.187     kia      2058:   return use;
                   2059: #else /* TEMPLATES */
                   2060:   return NULL;
1.186     kia      2061: #endif /* TEMPLATES */
                   2062: }
                   2063: 
                   2064: /*----------------------------------------------------------------------
1.187     kia      2065:   Template_CreateComponentFromSelection
1.186     kia      2066:   Create a xt:component with the selection and move it into the xt:head.
1.187     kia      2067:   Return the xt:component element.
1.186     kia      2068:   ----------------------------------------------------------------------*/
1.187     kia      2069: Element Template_CreateComponentFromSelection(Document doc)
1.186     kia      2070: {
                   2071: #ifdef TEMPLATES
1.187     kia      2072:   ThotBool    oldStructureChecking;
                   2073:   DisplayMode dispMode;
1.186     kia      2074:   Element     selElem, selElem2, parent, parent2, current, child, head;
                   2075:   ElementType selType, selType2;
                   2076:   int         firstChar, lastChar, firstChar2, lastChar2;
                   2077:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   2078: 
                   2079:   ElementType compType;
                   2080:   Element     comp = NULL;
                   2081:   char        buffer[128];
1.185     kia      2082: 
1.186     kia      2083:   const char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_NEWCOMP);
                   2084:   const char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
                   2085: 
                   2086:   if(doc && TtaGetDocumentAccessMode(doc) &&
                   2087:       TtaGetDocumentAccessMode(doc) && sstempl &&
                   2088:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2089:     {
                   2090:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2091:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   2092: 
                   2093:       if(selElem && selElem2)
                   2094:         {
                   2095:           selType =  TtaGetElementType(selElem);
                   2096:           selType2 =  TtaGetElementType(selElem2);
                   2097: 
                   2098:           QueryStringFromUser(label, title, buffer, 127);
                   2099:           
                   2100:           head = TemplateFindHead(doc);
                   2101:           
                   2102:           compType.ElSSchema = sstempl;
                   2103:           compType.ElTypeNum = Template_EL_component;
                   2104: 
                   2105:           parent  = TtaGetParent(selElem);
                   2106:           parent2 = TtaGetParent(selElem2);
                   2107:           
                   2108:           if(head && firstChar==0 && firstChar2==0 && parent == parent2)
                   2109:             {
                   2110:               dispMode = TtaGetDisplayMode (doc);
                   2111:               if (dispMode == DisplayImmediately)
                   2112:                 TtaSetDisplayMode (doc, DeferredDisplay);
                   2113:               oldStructureChecking = TtaGetStructureChecking (doc);
                   2114:               TtaSetStructureChecking (FALSE, doc);
                   2115: 
                   2116:               child = TtaGetLastChild(head);
                   2117:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.185     kia      2118:               comp = TtaNewElement(doc, compType);
1.186     kia      2119:               if(child)
                   2120:                 TtaInsertSibling(comp, child, FALSE, doc);
                   2121:               else
                   2122:                 TtaInsertFirstChild(&comp, head, doc);
1.185     kia      2123:               TtaRegisterElementCreate(comp, doc);
                   2124:               
                   2125:               TtaNextSibling(&selElem2);
1.186     kia      2126:               child = NULL;
1.185     kia      2127:               while(selElem!=selElem2)
1.180     kia      2128:                 {
1.185     kia      2129:                   current = selElem;
                   2130:                   TtaNextSibling(&selElem);
1.180     kia      2131:                   
1.185     kia      2132:                   TtaRegisterElementDelete (current, doc);
                   2133:                   TtaRemoveTree(current, doc);
                   2134:                   if(child)
                   2135:                     TtaInsertSibling(current, child, FALSE, doc);
                   2136:                   else
                   2137:                     TtaInsertFirstChild(&current, comp, doc);
                   2138:                   TtaRegisterElementDelete (current, doc);
                   2139:                   child = current;
1.180     kia      2140:                 }
1.185     kia      2141:               
                   2142:               SetAttributeStringValue(comp, Template_ATTR_name, buffer);
                   2143:               TtaCloseUndoSequence(doc);
1.186     kia      2144:               TtaSelectElement(doc, comp);
1.185     kia      2145:               
                   2146:               TtaSetStructureChecking (oldStructureChecking, doc);
                   2147:               TtaSetDisplayMode (doc, dispMode);
1.180     kia      2148:             }
                   2149:         }
                   2150:     }
1.186     kia      2151:   return comp;
1.187     kia      2152: #else /* TEMPLATES */
                   2153:   return NULL;
                   2154: #endif /* TEMPLATES */ 
1.180     kia      2155: }
1.187     kia      2156: 
                   2157: 
                   2158: 

Webmaster