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

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

Webmaster