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

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.158     kia       845: #ifdef AMAYA_DEBUG
                    846: void FillInsertableElemList (Document doc, Element elem, DLList list);
                    847: #endif /* AMAYA_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:         {
1.158     kia       918: #ifdef AMAYA_DEBUG
1.209     vatton    919:           printf("BagButtonClicked : \n  > %s\n", listtypes);
                    920:           //      {
                    921:           //        DLList list = DLList_Create();
                    922:           //        FillInsertableElemList (doc, TtaGetFirstChild(bagEl), list);
                    923:           //        DLListNode node;
                    924:           //        ForwardIterator iter = DLList_GetForwardIterator(list);
                    925:           //        ITERATOR_FOREACH(iter, DLListNode, node)
                    926:           //        {
                    927:           //          ElemListElement elem = (ElemListElement)node->elem;
                    928:           //          printf("  + %s\n", ElemListElement_GetName(elem));
                    929:           //        }
                    930:           //        DLList_Destroy(list);
                    931:           //      }
1.158     kia       932: #endif /* AMAYA_DEBUG */
1.209     vatton    933:           result = QueryStringFromMenu (doc, listtypes);
                    934:           TtaFreeMemory (listtypes);
                    935:           if (result)
                    936:             {
                    937:               decl = Template_GetDeclaration (t, result);
                    938:               if (decl)
                    939:                 {
                    940:                   dispMode = TtaGetDisplayMode (doc);
                    941:                   if (dispMode == DisplayImmediately)
                    942:                     /* don't set NoComputedDisplay
                    943:                        -> it breaks down views formatting when Enter generates new elements  */
                    944:                     TtaSetDisplayMode (doc, DeferredDisplay);
                    945: 
                    946:                   /* Prepare insertion.*/
                    947:                   oldStructureChecking = TtaGetStructureChecking (doc);
                    948:                   TtaSetStructureChecking (FALSE, doc);
                    949:                   TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    950: 
                    951:                   /* Insert */
                    952:                   if (el == bagEl)
                    953:                     {
                    954:                       el = TtaGetFirstChild (el);
                    955:                       TtaSelectElement (doc, el);
                    956:                       TtaInsertAnyElement (doc, TRUE);
                    957:                     }
                    958:                   else
                    959:                     {
                    960:                       TtaSelectElement (doc, el);
                    961:                       TtaInsertAnyElement (doc, FALSE);
                    962:                     }
                    963:                   newEl = Template_InsertBagChild (doc, bagEl, decl, FALSE);
                    964: 
                    965:                   /* Finish insertion.*/
                    966:                   TtaCloseUndoSequence (doc);
                    967:                   TtaSetDocumentModified (doc);
                    968:                   TtaSetStructureChecking (oldStructureChecking, doc);
                    969:                   // restore the display
                    970:                   TtaSetDisplayMode (doc, dispMode);
                    971:                   firstEl = GetFirstEditableElement (newEl);
                    972:                   if (firstEl)
                    973:                     {
                    974:                       TtaSelectElement (doc, firstEl);
                    975:                       TtaSetStatusSelectedElement (doc, view, firstEl);
                    976:                     }
                    977:                   else
                    978:                     {
                    979:                       TtaSelectElement (doc, newEl);
                    980:                       TtaSetStatusSelectedElement (doc, view, newEl);
                    981:                     }
                    982:                 }
                    983:             }
                    984:         }
                    985:       TtaFreeMemory (result);
                    986:     }
1.138     vatton    987: #endif /* TEMPLATES */
                    988:   return TRUE; /* don't let Thot perform normal operation */
                    989: }
                    990: 
                    991: /*----------------------------------------------------------------------
1.209     vatton    992:   DoReplicateUseElement insert a new element after the el child of
                    993:   repeatEl or as the first child of repeatEl.
                    994:   ----------------------------------------------------------------------*/
                    995: void DoReplicateUseElement (XTigerTemplate t, Document doc, int view,
                    996:                             Element el, Element repeatEl, char *name)
                    997: {
                    998:   Declaration     decl;
                    999:   Element         newEl, firstEl, prevRepeat;
                   1000:   ThotBool        oldStructureChecking;
                   1001:   DisplayMode     dispMode;
                   1002: 
                   1003:   if (repeatEl == Creating_repeat)
                   1004:     return;
                   1005:   prevRepeat = Creating_repeat;
                   1006:   Creating_repeat = repeatEl;
                   1007: 
                   1008:   decl = Template_GetDeclaration (t, name);
                   1009:   if (decl)
                   1010:     {
                   1011:       dispMode = TtaGetDisplayMode (doc);
                   1012:       if (dispMode == DisplayImmediately)
                   1013:         /* don't set NoComputedDisplay
                   1014:            -> it breaks down views formatting when Enter generates new elements  */
                   1015:         TtaSetDisplayMode (doc, DeferredDisplay);
                   1016:       /* Prepare insertion.*/
                   1017:       oldStructureChecking = TtaGetStructureChecking (doc);
                   1018:       TtaSetStructureChecking (FALSE, doc);
                   1019:       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1020:       /* Insert. */
                   1021:       if (el == repeatEl)
                   1022:         newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, NULL);
                   1023:       else
                   1024:         newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, el);
                   1025:       
                   1026:       /* Finish insertion.*/
                   1027:       TtaCloseUndoSequence(doc);
                   1028:       TtaSetDocumentModified (doc);
                   1029:       TtaSetStructureChecking (oldStructureChecking, doc);
                   1030:       
                   1031:       // restore the display
                   1032:       TtaSetDisplayMode (doc, dispMode);
                   1033:       firstEl = GetFirstEditableElement (newEl);
                   1034:       if (firstEl)
                   1035:         {
                   1036:           TtaSelectElement (doc, firstEl);
                   1037:           TtaSetStatusSelectedElement (doc, view, firstEl);
                   1038:         }
                   1039:       else
                   1040:         {
                   1041:           TtaSelectElement (doc, newEl);
                   1042:           TtaSetStatusSelectedElement (doc, view, newEl);
                   1043:         }
                   1044:     }
                   1045:   Creating_repeat = prevRepeat;
                   1046: }
                   1047: 
                   1048: /*----------------------------------------------------------------------
1.79      quint    1049:   RepeatButtonClicked
1.89      kia      1050:   Called when a repeat button is clicked.
                   1051:   Can be called for useEl, useSimple or repeat.
                   1052:   If called for useEl or useSimple, the new element must be added after.
                   1053:   If called for repeat, the element must be added before all.
1.206     kia      1054: 
1.56      francesc 1055:   Shows a menu with all the types that can be used in a use element.
                   1056:   ----------------------------------------------------------------------*/
1.79      quint    1057: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc 1058: {
                   1059: #ifdef TEMPLATES
1.89      kia      1060:   Document        doc = event->document;
                   1061:   Element         el = event->element;
                   1062:   ElementType     elType;
1.90      kia      1063:   XTigerTemplate  t;
                   1064:   Element         repeatEl = el;
                   1065:   Element         firstEl;
1.95      kia      1066:   View            view;
1.162     vatton   1067:   char           *listtypes = NULL;
                   1068:   char           *result = NULL;
1.104     kia      1069: 
1.112     vatton   1070:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia      1071:     return TRUE;
1.182     vatton   1072:   if (!IsTemplateInstanceDocument (doc))
                   1073:     return FALSE; /* let Thot perform normal operation */
1.148     kia      1074: 
1.95      kia      1075:   TtaGetActiveView (&doc, &view);
                   1076:   if (view != 1)
                   1077:     return FALSE; /* let Thot perform normal operation */
                   1078: 
1.89      kia      1079:   TtaCancelSelection(doc);
1.171     kia      1080:   t = GetXTigerDocTemplate(doc);
1.89      kia      1081:   elType = TtaGetElementType(el);
1.138     vatton   1082:   while (elType.ElTypeNum != Template_EL_repeat)
1.179     kia      1083:     {
                   1084:       repeatEl = TtaGetParent(repeatEl);
                   1085:       if (repeatEl == NULL)
                   1086:         break;
                   1087:       elType = TtaGetElementType(repeatEl);
                   1088:     }
1.112     vatton   1089:   if (repeatEl)
1.90      kia      1090:     {
1.179     kia      1091:       if (Template_CanInsertRepeatChild (repeatEl))
                   1092:         {
                   1093:           firstEl = TtaGetFirstChild (repeatEl);
                   1094:           listtypes = Template_GetListTypes (t, firstEl);
                   1095:           if (listtypes)
                   1096:             {
1.158     kia      1097: #ifdef AMAYA_DEBUG
1.179     kia      1098:               printf("RepeatButtonClicked : \n  > %s\n", listtypes);
1.158     kia      1099: #endif /* AMAYA_DEBUG */
1.179     kia      1100: 
                   1101:               result = QueryStringFromMenu (doc, listtypes);
                   1102:               TtaFreeMemory (listtypes);
                   1103:               if (result)
1.209     vatton   1104:                 DoReplicateUseElement (t, doc, view, el, repeatEl, result);
1.104     kia      1105:             }
1.179     kia      1106:           TtaFreeMemory(result);
                   1107:           DumpSubtree(repeatEl, doc, 0);
                   1108: 
                   1109:         }
                   1110:       else /* if (Template_CanInsertRepeatChild(repeatEl)) */
                   1111:         {
                   1112:           TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.98      kia      1113:         }
                   1114:     }
1.77      vatton   1115:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc 1116: #endif /* TEMPLATES */
1.94      kia      1117:   return TRUE;
                   1118: }
                   1119: 
                   1120: /*----------------------------------------------------------------------
                   1121:   UseButtonClicked
                   1122:   Shows a menu with all the types that can be used in a use element.
                   1123:   ----------------------------------------------------------------------*/
                   1124: ThotBool UseButtonClicked (NotifyElement *event)
                   1125: {
                   1126: #ifdef TEMPLATES
                   1127:   Document        doc = event->document;
                   1128:   Element         el = event->element;
1.99      kia      1129:   Element         child;
1.214     vatton   1130:   ElementType     elType, childType;
1.94      kia      1131:   XTigerTemplate  t;
                   1132:   Declaration     decl;
                   1133:   Element         firstEl;
                   1134:   Element         newEl = NULL;
                   1135:   char*           types;
                   1136:   ThotBool        oldStructureChecking;
1.95      kia      1137:   View            view;
1.133     vatton   1138:   char*           listtypes = NULL;
                   1139:   char*           result = NULL;
1.95      kia      1140: 
1.182     vatton   1141:   if (!TtaGetDocumentAccessMode (doc))
1.110     kia      1142:     return TRUE;
1.182     vatton   1143:   if (!IsTemplateInstanceDocument (doc))
                   1144:     return FALSE; /* let Thot perform normal operation */
1.206     kia      1145: 
1.95      kia      1146:   TtaGetActiveView (&doc, &view);
                   1147:   if (view != 1)
                   1148:     return FALSE; /* let Thot perform normal operation */
1.148     kia      1149: 
1.94      kia      1150:   TtaCancelSelection(doc);
1.171     kia      1151:   t = GetXTigerDocTemplate(doc);
1.94      kia      1152:   if (!t)
                   1153:     return FALSE; /* let Thot perform normal operation */
                   1154: 
1.214     vatton   1155:   elType = TtaGetElementType (el);
                   1156:   firstEl = TtaGetFirstChild (el);
                   1157:   childType = TtaGetElementType (firstEl);
                   1158:   if (firstEl &&
                   1159:       // TemplateObject is just a place holder
                   1160:       (childType.ElSSchema != elType.ElSSchema ||
                   1161:       childType.ElTypeNum != Template_EL_TemplateObject))
                   1162:     RepeatButtonClicked(event);
1.94      kia      1163:   else
                   1164:     {
1.209     vatton   1165:       types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
                   1166:       if (types)
                   1167:         {
                   1168:           listtypes = Template_ExpandTypes(t, types, NULL, FALSE);
1.158     kia      1169: #ifdef AMAYA_DEBUG
1.209     vatton   1170:           printf("UseButtonClicked : \n  > %s\n", listtypes);
1.158     kia      1171: #endif /* AMAYA_DEBUG */
1.206     kia      1172: 
1.209     vatton   1173:           result = QueryStringFromMenu(doc, listtypes);
                   1174:           if (result)
                   1175:             {
                   1176:               decl = Template_GetDeclaration(t, result);
                   1177:               if (decl)
                   1178:                 {
                   1179:                   /* Prepare insertion.*/
                   1180:                   oldStructureChecking = TtaGetStructureChecking (doc);
                   1181:                   TtaSetStructureChecking (FALSE, doc);
                   1182:                   TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.206     kia      1183: 
1.209     vatton   1184:                   /* Insert */
                   1185:                   newEl = Template_InsertUseChildren(doc, el, decl);
                   1186:                   for (child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
                   1187:                     {
1.213     vatton   1188:                       TtaRegisterElementCreate (child, doc);
1.209     vatton   1189:                     }
1.206     kia      1190: 
1.209     vatton   1191:                   TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
1.213     vatton   1192:                   TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
1.206     kia      1193: 
1.209     vatton   1194:                   /* xt:currentType attribute.*/
                   1195:                   SetAttributeStringValueWithUndo(el, Template_ATTR_currentType, result);
1.206     kia      1196: 
1.209     vatton   1197:                   /* Finish insertion. */
                   1198:                   TtaCloseUndoSequence(doc);
                   1199:                   TtaSetDocumentModified (doc);
                   1200:                   TtaSetStructureChecking (oldStructureChecking, doc);
1.206     kia      1201: 
1.213     vatton   1202:                   firstEl = GetFirstEditableElement (newEl);
1.209     vatton   1203:                   if (firstEl)
                   1204:                     {
                   1205:                       TtaSelectElement (doc, firstEl);
1.213     vatton   1206:                       TtaSetStatusSelectedElement (doc, view, firstEl);
1.209     vatton   1207:                     }
                   1208:                   else
                   1209:                     {
                   1210:                       TtaSelectElement (doc, newEl);
                   1211:                       TtaSetStatusSelectedElement(doc, view, newEl);
                   1212:                     }
                   1213:                 }
                   1214:             }
                   1215:         }
                   1216:       TtaFreeMemory(types);
                   1217:       TtaFreeMemory(listtypes);
                   1218:       TtaFreeMemory(result);
                   1219:     }
1.148     kia      1220: 
1.94      kia      1221:   return TRUE;
                   1222: #endif /* TEMPLATES */
1.56      francesc 1223:        return TRUE;
                   1224: }
1.64      francesc 1225: 
1.89      kia      1226: 
1.103     kia      1227: /*----------------------------------------------------------------------
                   1228:   UseSimpleButtonClicked
                   1229:   ----------------------------------------------------------------------*/
                   1230: ThotBool UseSimpleButtonClicked (NotifyElement *event)
                   1231: {
                   1232: #ifdef TEMPLATES
1.181     vatton   1233:   ElementType     elType, parentType;
                   1234:   AttributeType   attrType;
                   1235:   Attribute       att;
                   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.181     vatton   1242:   elType = TtaGetElementType (event->element);
                   1243:   attrType.AttrSSchema = elType.ElSSchema;
                   1244:   attrType.AttrTypeNum = Template_ATTR_option;
                   1245:   att = TtaGetAttribute (event->element, attrType);
                   1246:   if (att)
                   1247:     return OptionButtonClicked (event);
1.206     kia      1248: 
1.181     vatton   1249:   parentType = TtaGetElementType (TtaGetParent( event->element));
1.112     vatton   1250:   if (parentType.ElTypeNum == Template_EL_repeat)
1.138     vatton   1251:     return RepeatButtonClicked (event);
                   1252:   else if (parentType.ElTypeNum == Template_EL_bag)
                   1253:     return BagButtonClicked (event);
1.206     kia      1254: #endif /* TEMPLATES */
1.103     kia      1255:   return FALSE;
                   1256: }
1.94      kia      1257: 
                   1258: /*----------------------------------------------------------------------
                   1259:   OptionButtonClicked
                   1260:   ----------------------------------------------------------------------*/
                   1261: ThotBool OptionButtonClicked (NotifyElement *event)
                   1262: {
                   1263: #ifdef TEMPLATES
1.145     kia      1264:   Element         useEl, contentEl, next;
1.182     vatton   1265:   ElementType     useType;
1.94      kia      1266:   Document        doc;
                   1267:   XTigerTemplate  t;
                   1268:   View            view;
                   1269: 
1.182     vatton   1270:   if (!TtaGetDocumentAccessMode (event->document))
1.110     kia      1271:     return TRUE;
1.182     vatton   1272:   if (!IsTemplateInstanceDocument (event->document))
                   1273:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1274: 
1.94      kia      1275:   TtaGetActiveView (&doc, &view);
                   1276:   if (view != 1)
                   1277:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1278: 
1.94      kia      1279:   doc = event->document;
1.182     vatton   1280:   useEl = event->element;
1.145     kia      1281:   if (!useEl)
1.94      kia      1282:     return FALSE; /* let Thot perform normal operation */
1.145     kia      1283:   useType = TtaGetElementType (useEl);
1.182     vatton   1284:   if (useType.ElTypeNum != Template_EL_useEl &&
                   1285:       useType.ElTypeNum != Template_EL_useSimple)
1.94      kia      1286:     return FALSE;
                   1287: 
1.145     kia      1288:   TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
1.94      kia      1289:   TtaCancelSelection (doc);
1.145     kia      1290:   contentEl = TtaGetFirstChild (useEl);
                   1291:   if (!contentEl)
1.94      kia      1292:     /* the "use" element is empty. Instantiate it */
                   1293:     {
1.171     kia      1294:       t = GetXTigerDocTemplate (doc);
1.94      kia      1295:       if (!t)
                   1296:         return FALSE; // no template ?!?!
1.145     kia      1297:       InstantiateUse (t, useEl, doc, TRUE);
1.94      kia      1298:     }
                   1299:   else
                   1300:     /* remove the content of the "use" element */
                   1301:     {
                   1302:       do
                   1303:         {
1.145     kia      1304:           next = contentEl;
1.94      kia      1305:           TtaNextSibling (&next);
1.145     kia      1306:           TtaRegisterElementDelete(contentEl, doc);
                   1307:           TtaDeleteTree (contentEl, doc);
                   1308:           contentEl = next;
1.94      kia      1309:         }
                   1310:       while (next);
1.145     kia      1311:       if (NeedAMenu (useEl, doc))
                   1312:         {
                   1313:           TtaChangeTypeOfElement (useEl, doc, Template_EL_useEl);
                   1314:           TtaRegisterElementTypeChange(useEl, Template_EL_useSimple, doc);
                   1315:         }
1.94      kia      1316:     }
                   1317:   TtaSelectElement (doc, event->element);
1.145     kia      1318:   TtaCloseUndoSequence(doc);
1.94      kia      1319:   return TRUE; /* don't let Thot perform normal operation */
                   1320: #endif /* TEMPLATES */
                   1321:   return TRUE;
                   1322: }
                   1323: 
1.191     kia      1324: 
                   1325: 
1.111     vatton   1326: /*----------------------------------------------------------------------
1.191     kia      1327:   Template_FillFromDocument
                   1328:   Fill XTigerTemplate structure with the content of the document.
                   1329:   Load dependencies if needed.
1.111     vatton   1330:   ----------------------------------------------------------------------*/
1.191     kia      1331: void Template_FillFromDocument (Document doc)
1.111     vatton   1332: {
                   1333: #ifdef TEMPLATES
1.191     kia      1334:   XTigerTemplate t = GetXTigerTemplate (DocumentURLs[doc]);
                   1335:   Element        root;
1.206     kia      1336: 
1.193     vatton   1337:   if (t)
1.111     vatton   1338:     {
1.193     vatton   1339: #ifdef AMAYA_DEBUG
1.212     vatton   1340:       printf("Template_FillFromDocument state: %d\n", t->state);
1.193     vatton   1341: #endif
1.191     kia      1342:       SetTemplateDocument (t, doc);
1.210     vatton   1343:       Template_PrepareTemplate(t, doc);
1.220     vatton   1344:       if (IsTemplateInstanceDocument (doc))
1.144     vatton   1345:         {
1.193     vatton   1346: #ifdef AMAYA_DEBUG
1.191     kia      1347:           printf("  > instance\n");
1.193     vatton   1348: #endif
1.144     vatton   1349:           // fix all access rights in the instance
1.191     kia      1350:           root = TtaGetRootElement (doc);
                   1351:           TtaSetAccessRight (root, ReadOnly, doc);
1.166     kia      1352:           Template_FixAccessRight (t, root, doc);
1.144     vatton   1353:           TtaUpdateAccessRightInViews (doc, root);
                   1354:         }
1.193     vatton   1355: #ifdef AMAYA_DEBUG
1.209     vatton   1356:       else if (t->state&templLibraryFlag)
1.191     kia      1357:         printf("  > library\n");
1.209     vatton   1358:       else if (t->state&templTemplate)
1.191     kia      1359:         printf("  > template\n");
1.206     kia      1360: #endif
1.191     kia      1361:       // Mark loaded
                   1362:       t->state |= templloaded;
                   1363:       TtaSetDocumentUnmodified (doc);
                   1364:       UpdateTemplateMenus (doc);
1.206     kia      1365: 
                   1366: #ifdef AMAYA_DEBUG
1.191     kia      1367:       DumpAllDeclarations();
                   1368: #endif /* AMAYA_DEBUG */
                   1369:     }
                   1370: #endif /* TEMPLATES */
                   1371: }
                   1372: 
                   1373: 
                   1374: /*----------------------------------------------------------------------
                   1375:   Template_CheckAndPrepareTemplate checks if the document is a template
                   1376:   or a library and prepare XTigerTemplate structure to use it.
                   1377:   ----------------------------------------------------------------------*/
                   1378: ThotBool Template_CheckAndPrepareTemplate(char* docURL)
                   1379: {
                   1380: #ifdef TEMPLATES
                   1381:   XTigerTemplate t = NULL; //GetXTigerTemplate(docURL);
1.193     vatton   1382: 
1.212     vatton   1383:   if (IsXTiger(docURL))
                   1384:     {
1.193     vatton   1385: #ifdef AMAYA_DEBUG
1.212     vatton   1386:       printf("Template_CheckAndPrepareTemplate %s templTemplate\n", docURL);
1.193     vatton   1387: #endif
1.219     vatton   1388:       t = LookForXTigerTemplate (docURL);
1.191     kia      1389:       t->state |= templTemplate;
1.111     vatton   1390:     }
1.193     vatton   1391:   else if (IsXTigerLibrary(docURL))
1.191     kia      1392:     {
1.212     vatton   1393: #ifdef AMAYA_DEBUG
                   1394:       printf("Template_CheckAndPrepareTemplate %s templLibrary\n", docURL);
                   1395: #endif
1.219     vatton   1396:       t = LookForXTigerLibrary (docURL);
1.191     kia      1397:       t->state |= templLibrary;
                   1398:     }
1.193     vatton   1399:   return t != NULL;
1.111     vatton   1400: #endif /* TEMPLATES */
1.191     kia      1401:   return FALSE;
1.111     vatton   1402: }
1.94      kia      1403: 
1.191     kia      1404: 
1.66      vatton   1405: /*----------------------------------------------------------------------
1.213     vatton   1406:   Template_CheckAndPrepareInstance checks if it is a template instance.
1.108     vatton   1407:   If it's an instance and the template is not loaded, load it into a
                   1408:   temporary file
1.66      vatton   1409:   ----------------------------------------------------------------------*/
1.213     vatton   1410: void Template_CheckAndPrepareInstance (char *localFileName, Document doc,
                   1411:                                        char* docURL)
1.65      francesc 1412: {
                   1413: #ifdef TEMPLATES
1.76      vatton   1414:   XTigerTemplate   t;
1.103     kia      1415:   char            *content, *ptr, *begin;
1.76      vatton   1416:   gzFile           stream;
                   1417:   char             buffer[2000];
1.77      vatton   1418:   int              res;
1.213     vatton   1419:   char            *template_version = NULL, *template_url = NULL;
1.65      francesc 1420: 
1.171     kia      1421:   stream = TtaGZOpen (localFileName);
1.76      vatton   1422:   if (stream != 0)
1.65      francesc 1423:     {
1.76      vatton   1424:       res = gzread (stream, buffer, 1999);
                   1425:       if (res >= 0)
1.65      francesc 1426:         {
1.81      vatton   1427:           buffer[res] = EOS;
1.103     kia      1428:           begin = strstr (buffer, "<?xtiger");
1.206     kia      1429: 
1.112     vatton   1430:           if (begin)
1.209     vatton   1431:             {
                   1432:               // Search for template version
                   1433:               ptr = strstr (begin, "templateVersion");
                   1434:               if (ptr)
                   1435:                 ptr = strstr (ptr, "=");
                   1436:               if (ptr)
                   1437:                 ptr = strstr (ptr, "\"");
                   1438:               if (ptr)
                   1439:                 {
                   1440:                   // template URI
                   1441:                   content = &ptr[1];
                   1442:                   ptr = strstr (content, "\"");
                   1443:                 }
                   1444:               if (ptr)
                   1445:                 {
                   1446:                   *ptr = EOS;
                   1447:                   //Get now the template URI
                   1448:                   template_version = TtaStrdup (content);
                   1449:                   *ptr = '"';
                   1450:                 }
                   1451: 
                   1452:               // Search for template uri
                   1453:               ptr = strstr (begin, "template");
                   1454:               if (ptr && ptr[8] != 'V')
                   1455:                 ptr = strstr (ptr, "=");
                   1456:               if (ptr)
                   1457:                 ptr = strstr (ptr, "\"");
                   1458:               if (ptr)
                   1459:                 {
                   1460:                   // template URI
                   1461:                   content = &ptr[1];
                   1462:                   ptr = strstr (content, "\"");
                   1463:                 }
                   1464:               if (ptr)
                   1465:                 {
                   1466:                   *ptr = EOS;
                   1467:                   //Get now the template URI
                   1468:                   template_url = TtaStrdup (content);
                   1469:                   t = GetXTigerTemplate (template_url);
                   1470:                   if (!t)
                   1471:                     {
                   1472:                       LoadTemplate (doc, template_url);
1.219     vatton   1473:                       t = GetXTigerTemplate (template_url);
1.209     vatton   1474:                     }
1.219     vatton   1475:                   Template_PrepareInstance (docURL, doc, template_version, template_url);
1.209     vatton   1476:                   Template_AddReference (t);
                   1477:                   *ptr = '"';
                   1478:                 }
                   1479:             }
1.65      francesc 1480:         }
                   1481:     }
1.171     kia      1482:   TtaFreeMemory(template_version);
                   1483:   TtaFreeMemory(template_url);
1.76      vatton   1484:   TtaGZClose (stream);
1.65      francesc 1485: #endif /* TEMPLATES */
                   1486: }
                   1487: 
1.64      francesc 1488: /*----------------------------------------------------------------------
1.178     kia      1489:   ClosingTemplateDocument
                   1490:   Callback called before closing a document which uses templates.
1.64      francesc 1491:   ----------------------------------------------------------------------*/
1.178     kia      1492: ThotBool ClosingTemplateDocument(NotifyDialog* dialog)
1.64      francesc 1493: {
1.65      francesc 1494: #ifdef TEMPLATES
1.171     kia      1495:   XTigerTemplate t = GetXTigerDocTemplate(dialog->document);
1.209     vatton   1496:   if (t)
                   1497:     Template_RemoveReference(t);
1.65      francesc 1498: #endif /* TEMPLATES */
                   1499:   return FALSE;
1.64      francesc 1500: }
1.87      kia      1501: 
                   1502: /*----------------------------------------------------------------------
1.120     kia      1503:   IsTemplateElement
1.138     vatton   1504:   Test if an element is a template element.
1.87      kia      1505:   ----------------------------------------------------------------------*/
1.140     vatton   1506: ThotBool IsTemplateElement (Element elem)
1.87      kia      1507: {
                   1508: #ifdef TEMPLATES
1.138     vatton   1509:   ElementType     elType;
                   1510: 
                   1511:   elType = TtaGetElementType(elem);
                   1512:   if (elType.ElSSchema)
1.164     kia      1513:     return (strcmp(TtaGetSSchemaName(elType.ElSSchema) , "Template") == 0);
1.138     vatton   1514: #endif /* TEMPLATES */
1.87      kia      1515:   return FALSE;
                   1516: }
                   1517: 
                   1518: 
                   1519: /*----------------------------------------------------------------------
                   1520:   GetFirstTemplateParentElement
1.138     vatton   1521:   Return the first element which has "Template" as schema name or null.
1.87      kia      1522:   ----------------------------------------------------------------------*/
                   1523: Element GetFirstTemplateParentElement(Element elem)
                   1524: {
                   1525: #ifdef TEMPLATES
1.138     vatton   1526:   ElementType     elType;
                   1527: 
                   1528:   elem = TtaGetParent (elem);
                   1529:   elType = TtaGetElementType(elem);
                   1530:   while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
1.209     vatton   1531:     {
                   1532:       elem = TtaGetParent (elem);
                   1533:       elType = TtaGetElementType(elem);
                   1534:     }
1.87      kia      1535:   return elem;
                   1536: #else
                   1537:   return NULL;
                   1538: #endif /* TEMPLATES */
                   1539: }
1.101     kia      1540: 
1.103     kia      1541: 
1.101     kia      1542: /*----------------------------------------------------------------------
1.209     vatton   1543:   IsBeginningSelected
                   1544:   Returns TRUE if the selection is athe beginning of an element
                   1545:   ----------------------------------------------------------------------*/
                   1546: ThotBool IsBeginningSelected (Element el, Document doc)
                   1547: {
                   1548:   Element     selElem, prev;
                   1549:   int         firstChar, lastChar;
                   1550: 
                   1551:   TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1552:   if (firstChar <= 1 && lastChar < firstChar)
                   1553:     {
                   1554:       while (selElem && selElem != el)
                   1555:         {
                   1556:           prev = selElem;
                   1557:           TtaPreviousSibling (&prev);
                   1558:           if (prev)
                   1559:             return FALSE;
                   1560:           selElem = TtaGetParent (selElem);
                   1561:         }
                   1562:       return TRUE;
                   1563:     }
                   1564:   return FALSE;
                   1565: }
                   1566: 
                   1567: /*----------------------------------------------------------------------
1.102     vatton   1568:   TemplateElementWillBeCreated
1.101     kia      1569:   Processed when an element will be created in a template context.
                   1570:   ----------------------------------------------------------------------*/
1.102     vatton   1571: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101     kia      1572: {
1.103     kia      1573: #ifdef TEMPLATES
1.209     vatton   1574:   ElementType     elType = event->elementType;
                   1575:   Element         parent = event->element;
                   1576:   ElementType     parentType = TtaGetElementType(parent);
                   1577:   Element         ancestor, el, next;
                   1578:   ElementType     ancestorType;
                   1579:   SSchema         templateSSchema;
                   1580:   XTigerTemplate  t;
                   1581:   char           *types, *ptr, *name = NULL;
                   1582:   int             len, view, i, doc = event->document;
                   1583:   ThotBool        b;
1.101     kia      1584: 
1.209     vatton   1585:   if (event->info == 1)
1.115     kia      1586:     return FALSE;
                   1587: 
1.193     vatton   1588: #ifdef AMAYA_DEBUG
1.185     kia      1589:   printf("TemplateElementWillBeCreated\n");
1.193     vatton   1590: #endif
1.209     vatton   1591:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia      1592:     return TRUE;
                   1593: 
1.209     vatton   1594:   templateSSchema = TtaGetSSchema ("Template", doc);
1.102     vatton   1595:   if (templateSSchema == NULL)
                   1596:     return FALSE; // let Thot do the job
1.115     kia      1597: 
1.113     kia      1598:   // Fisrt, test if in a xt:bag or in a base-element xt:use
1.209     vatton   1599:   if (parentType.ElSSchema == templateSSchema)
1.113     kia      1600:     ancestor = parent;
                   1601:   else
1.147     vatton   1602:     ancestor = GetFirstTemplateParentElement (parent);
1.113     kia      1603: 
1.147     vatton   1604:   if (ancestor)
1.209     vatton   1605:     {
                   1606:       ancestorType = TtaGetElementType(ancestor);
                   1607:       if (ancestorType.ElTypeNum == Template_EL_bag)
                   1608:         {
                   1609:           // only check the use child
                   1610:           if (ancestor != parent)
                   1611:             return  FALSE; // let Thot do the job
                   1612:           if (elType.ElSSchema == templateSSchema &&
                   1613:               (elType.ElTypeNum == Template_EL_useSimple ||
                   1614:                elType.ElTypeNum == Template_EL_useEl))
                   1615:             return FALSE;
                   1616:           return !Template_CanInsertElementInBagElement (doc, elType, ancestor);
                   1617:         }
                   1618:       else if (ancestorType.ElTypeNum == Template_EL_repeat)
                   1619:         {
                   1620:           if (elType.ElSSchema != templateSSchema ||
                   1621:               (elType.ElTypeNum != Template_EL_useSimple &&
                   1622:                elType.ElTypeNum != Template_EL_useEl))
                   1623:             return TRUE; // don't let Thot do the job
1.210     vatton   1624:           t = GetXTigerDocTemplate (doc);
1.209     vatton   1625:           el = NULL;
                   1626:           i = 0;
                   1627:           next = TtaGetFirstChild (ancestor);
                   1628:           while (next && i < event->position)
                   1629:             {
                   1630:               el = next;
                   1631:               TtaNextSibling (&next);
                   1632:               i++;
                   1633:             }
                   1634:           if (el)
                   1635:             next = el;
1.210     vatton   1636:           name = GetUsedTypeName (next);
1.215     vatton   1637:           DoReplicateUseElement (t, doc, 1, el, ancestor, name);
1.209     vatton   1638:           TtaFreeMemory(name);
                   1639:           return TRUE; // don't let Thot do the job
                   1640:         }
                   1641:       else if (ancestorType.ElTypeNum == Template_EL_useSimple ||
                   1642:                ancestorType.ElTypeNum == Template_EL_useEl)
                   1643:         {
                   1644:           // only check the bag child @@@ will be check exclude/include later
                   1645:           if (elType.ElSSchema == templateSSchema &&
                   1646:               (elType.ElTypeNum == Template_EL_useSimple ||
                   1647:                elType.ElTypeNum == Template_EL_useEl))
                   1648:             return TRUE; // don't let Thot do the job
                   1649:           if (ancestor != parent)
1.215     vatton   1650:             return  (TtaIsReadOnly (parent) != 0); // let or not Thot do the job
1.209     vatton   1651:           types = GetAttributeStringValueFromNum (ancestor, Template_ATTR_currentType, NULL);
                   1652:           b = Template_CanInsertElementInUse (doc, elType, types,
                   1653:                                               parent, event->position);
                   1654:           if (types && !b)
                   1655:             {
                   1656:               // check with equivalent component
                   1657:               name = (char *)GetXMLElementName (elType, doc);
                   1658:               if (name)
                   1659:                 {
                   1660:                   len = strlen (name);
                   1661:                   ptr = strstr (types, name);
                   1662:                   if (ptr && len &&
                   1663:                       (ptr == types || ptr[-1] == SPACE) &&
                   1664:                       (ptr[len] == EOS || ptr[len] == SPACE))
                   1665:                     {
                   1666:                       parent = TtaGetParent(ancestor);
                   1667:                       parentType = TtaGetElementType(parent);
                   1668:                       if (parentType.ElSSchema == templateSSchema &&
                   1669:                           parentType.ElTypeNum == Template_EL_repeat)
                   1670:                         {
                   1671:                           // duplicate the current use
                   1672:                             TtaGetActiveView (&doc, &view);
                   1673:                             t = GetXTigerDocTemplate(doc);
                   1674:                             if (IsBeginningSelected (ancestor, doc))
                   1675:                               TtaPreviousSibling (&ancestor);
                   1676:                              DoReplicateUseElement (t, doc, view, ancestor, parent, name);
                   1677:                         }
                   1678:                     }
                   1679:                 }
                   1680:             }
                   1681:           TtaFreeMemory(types);
                   1682:           return !b;
                   1683:         }
                   1684:       else if (ancestorType.ElTypeNum == Template_EL_component)
                   1685:         // allow all changes
1.116     kia      1686:         return FALSE;
1.113     kia      1687:     }
1.206     kia      1688: 
1.210     vatton   1689:   if (!IsTemplateInstanceDocument(doc) &&
                   1690:        elType.ElSSchema == templateSSchema &&
1.209     vatton   1691:       (elType.ElTypeNum == Template_EL_TEXT_UNIT ||
                   1692:        elType.ElTypeNum == Template_EL_component))
                   1693:       return FALSE;
                   1694:   // Cannot insert.
1.113     kia      1695:   return TRUE;
1.101     kia      1696: #endif /* TEMPLATES*/
1.102     vatton   1697:   return FALSE;
1.101     kia      1698: }
                   1699: 
                   1700: /*----------------------------------------------------------------------
                   1701:   TemplateElementWillBeDeleted
                   1702:   Processed when an element will be deleted in a template context.
                   1703:   ----------------------------------------------------------------------*/
                   1704: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1705: {
1.107     kia      1706: #ifdef TEMPLATES
                   1707:   Document       doc = event->document;
                   1708:   Element        elem = event->element;
1.163     vatton   1709:   Element        xtElem, parent = NULL, sibling;
1.117     kia      1710:   ElementType    xtType, elType;
1.107     kia      1711:   char*          type;
                   1712:   Declaration    dec;
1.115     kia      1713:   SSchema        templateSSchema;
1.107     kia      1714:   XTigerTemplate t;
1.156     vatton   1715:   ThotBool       selparent = FALSE;
1.206     kia      1716: 
1.209     vatton   1717:   if (event->info==1)
1.115     kia      1718:     return FALSE;
                   1719: 
1.112     vatton   1720:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1721:     return TRUE;
                   1722: 
1.138     vatton   1723:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.107     kia      1724:   if (templateSSchema == NULL)
                   1725:     return FALSE; // let Thot do the job
1.206     kia      1726: 
1.192     kia      1727:   t = GetXTigerDocTemplate(doc);
1.209     vatton   1728:   if (Template_IsTemplate(t)||Template_IsLibrary(t))
1.192     kia      1729:     return FALSE; // If template or library, pass to specialized functions.
1.122     kia      1730: 
1.107     kia      1731:   xtElem = GetFirstTemplateParentElement(elem);
1.112     vatton   1732:   if (xtElem)
1.209     vatton   1733:     {
                   1734:       xtType = TtaGetElementType(xtElem);
                   1735: 
1.206     kia      1736: 
                   1737: 
1.209     vatton   1738:       if (xtType.ElTypeNum==Template_EL_bag)
                   1739:         {
                   1740:           elType = TtaGetElementType(elem);
                   1741:           if (elType.ElSSchema==templateSSchema &&
                   1742:               (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
                   1743:             {
                   1744:               // Remove element manually.
                   1745:               TtaOpenUndoSequence(doc, elem, elem, 0, 0);
                   1746:               TtaRegisterElementDelete(elem, doc);
                   1747:               TtaDeleteTree(elem, doc);
                   1748:               TtaCloseUndoSequence(doc);
                   1749:               return TRUE;
                   1750:             }
                   1751:           else
                   1752:             return FALSE; // xt:bag always allow remove children.
                   1753:         }
                   1754:       else if (xtType.ElTypeNum == Template_EL_useSimple ||
                   1755:                xtType.ElTypeNum == Template_EL_useEl)
                   1756:         {
                   1757:           parent = TtaGetParent(elem);
                   1758:           if (xtElem != parent)
                   1759:             {
                   1760:               type = GetAttributeStringValueFromNum (xtElem, Template_ATTR_currentType, NULL);
                   1761:               dec = Template_GetDeclaration(t, type);
                   1762:               TtaFreeMemory (type);
1.109     kia      1763: 
1.209     vatton   1764:               if (dec && dec->nature == XmlElementNat)
                   1765:                 return FALSE; // Can remove element only if in xt:use current type is base language element.
                   1766:               else
                   1767:                 return TRUE;
                   1768:             }
                   1769:         }
                   1770:       else if (xtType.ElTypeNum == Template_EL_repeat)
1.156     vatton   1771:         {
1.209     vatton   1772:           sibling = TtaGetSuccessor (elem);
1.156     vatton   1773:           if (sibling == NULL)
1.209     vatton   1774:             {
                   1775:               // there is no next element
                   1776:               sibling = TtaGetPredecessor (elem);
                   1777:               if (sibling == NULL)
                   1778:                 selparent = TRUE;
                   1779:             }
                   1780:           TtaRegisterElementDelete(elem, doc);
                   1781:           TtaDeleteTree(elem, doc);
                   1782:           InstantiateRepeat(t, xtElem, doc, TRUE);
                   1783:           if (selparent)
                   1784:             // look for the new sibling
                   1785:             sibling = TtaGetFirstChild (parent);
                   1786:           if (sibling)
                   1787:             TtaSelectElement(doc, sibling);
                   1788:           else
                   1789:             TtaSelectElement(doc, parent);
                   1790:           return TRUE;
1.156     vatton   1791:         }
1.107     kia      1792:     }
1.206     kia      1793: 
1.109     kia      1794:   //TODO Test if current element is use or repeat.
                   1795:   // Because if an element is delete and it is the unique child of its parent,
1.206     kia      1796:   // the parent intends to destroy itself.
                   1797: 
1.107     kia      1798:   return TRUE;
                   1799: #else /* TEMPLATES */
1.101     kia      1800:   return FALSE;
1.107     kia      1801: #endif /* TEMPLATES */
1.101     kia      1802: }
                   1803: 
1.109     kia      1804: /*----------------------------------------------------------------------
                   1805:   CurrentTypeWillBeExported
                   1806:   Check if the xt:currentType attribute can be exported
                   1807:   ----------------------------------------------------------------------*/
                   1808: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
                   1809: {
                   1810: #ifdef TEMPLATES
1.110     kia      1811: 
1.112     vatton   1812:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1813:     return TRUE;
                   1814: 
1.112     vatton   1815:   if (IsTemplateDocument(event->document))
1.109     kia      1816:     return TRUE;
                   1817: #endif /* TEMPLATES */
                   1818:   return FALSE;
                   1819: }
1.127     kia      1820: 
                   1821: /*----------------------------------------------------------------------
                   1822:   TemplateAttrInMenu
                   1823:   Called by Thot when building the Attributes menu for template elements.
                   1824:   ----------------------------------------------------------------------*/
                   1825: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
                   1826: {
                   1827: #ifdef TEMPLATES
                   1828:   // Prevent from showing attributes for template instance but not templates.
1.209     vatton   1829:   if (IsTemplateInstanceDocument(event->document))
1.127     kia      1830:     return TRUE;
                   1831:   else
                   1832: #endif /* TEMPLATES */
                   1833:     return FALSE;
                   1834: }
1.160     kia      1835: 
1.167     kia      1836: /*----------------------------------------------------------------------
1.168     kia      1837:   CreateTemplateFromDocument
                   1838:   Create a template from the current document.
1.167     kia      1839:   ----------------------------------------------------------------------*/
                   1840: void CreateTemplateFromDocument(Document doc, View view)
                   1841: {
1.171     kia      1842: #ifdef TEMPLATES
1.205     vatton   1843:   char     buffer[MAX_LENGTH], suffix[10];
1.196     vatton   1844: 
1.204     vatton   1845:   if (IsW3Path (DocumentURLs[doc]) &&
1.205     vatton   1846:       DocumentMeta[doc] && DocumentMeta[doc]->full_content_location)
                   1847:     {
1.209     vatton   1848:       // use the location instead of the current URI
1.206     kia      1849: 
1.209     vatton   1850:       strcpy (buffer, DocumentMeta[doc]->full_content_location);
1.205     vatton   1851:     }
1.204     vatton   1852:   else
                   1853:     strcpy (buffer, DocumentURLs[doc]);
                   1854: 
                   1855:   // remove the current suffix
                   1856:   TtaExtractSuffix (buffer, suffix);
1.196     vatton   1857:   // the new suffix
1.168     kia      1858:   strcat(buffer, ".xtd");
                   1859:   DontReplaceOldDoc = TRUE;
1.221   ! vatton   1860:   CreateTemplate (doc, buffer);
1.204     vatton   1861:   // by default .xtd files are xml files
                   1862:   TtaSetDocumentCharset (doc, UTF_8, FALSE);
1.171     kia      1863: #endif /* TEMPLATES */
1.167     kia      1864: }
                   1865: 
1.169     kia      1866: /*----------------------------------------------------------------------
                   1867:   UpdateTemplateMenus
                   1868:   ----------------------------------------------------------------------*/
                   1869: void UpdateTemplateMenus (Document doc)
                   1870: {
1.196     vatton   1871:   if (IsTemplateInstanceDocument(doc))
1.180     kia      1872:     {
                   1873:       // Instance document
                   1874:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1875:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1876:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1877:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1878:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1879:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180     kia      1880:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196     vatton   1881:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1882:     }
1.213     vatton   1883:   else if (DocumentURLs[doc] && DocumentTypes[doc] != docSource &&
                   1884:            IsXTiger (DocumentURLs[doc]))
1.180     kia      1885:     {
                   1886:       // Template document
                   1887:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1888:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1889:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1890:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1891:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeat);
                   1892:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeatComp);
1.206     kia      1893:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateFreeBox);
1.196     vatton   1894:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1895:     }
1.169     kia      1896:   else
1.180     kia      1897:     {
                   1898:       //Standard document
                   1899:       TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
                   1900:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187     kia      1901:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195     kia      1902:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
                   1903:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1904:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180     kia      1905:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196     vatton   1906:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.180     kia      1907:     }
1.169     kia      1908: }
1.171     kia      1909: 
                   1910: /*----------------------------------------------------------------------
                   1911:   UninstanciateTemplateDocument
                   1912:   An instance of a template is tranformed into a template-less docuemnt.
                   1913:   Remove link between XTigerTemplate structure and document.
                   1914:   ----------------------------------------------------------------------*/
                   1915: void UninstanciateTemplateDocument(Document doc)
                   1916: {
                   1917: #ifdef TEMPLATES
                   1918:   XTigerTemplate t = GetXTigerDocTemplate(doc);
1.209     vatton   1919:   if (t)
1.171     kia      1920:     Template_Close(t);
1.206     kia      1921: #endif /* TEMPLATES */
1.171     kia      1922: }
                   1923: 
                   1924: 
                   1925: /*----------------------------------------------------------------------
                   1926:   Template_PrepareInstance
                   1927:   Allocate XTigerTemplate structure for instance and initialize template
                   1928:   url and template version.
                   1929:   ----------------------------------------------------------------------*/
                   1930: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
                   1931: {
                   1932: #ifdef TEMPLATES
                   1933:   XTigerTemplate t = GetXTigerTemplate(fileName);
1.209     vatton   1934:   if (!t)
1.171     kia      1935:     t = NewXTigerTemplate(fileName);
                   1936:   t->state           = templInstance;
1.213     vatton   1937:   t->templateVersion = TtaStrdup (template_version);
                   1938:   t->base_uri        = TtaStrdup (template_url);
1.171     kia      1939:   t->doc             = doc;
1.178     kia      1940:   t->ref             = 1;
1.206     kia      1941: 
1.171     kia      1942: #endif /* TEMPLATES */
                   1943: }
                   1944: 
                   1945: 
                   1946: /*----------------------------------------------------------------------
                   1947:   SetDocumentAsXTigerTemplate
                   1948:   Set the document template structure as template.
                   1949:   ----------------------------------------------------------------------*/
                   1950: void SetDocumentAsXTigerTemplate(Document doc)
                   1951: {
                   1952: #ifdef TEMPLATES
                   1953:   XTigerTemplate t = GetXTigerDocTemplate(doc);
1.209     vatton   1954:   if (t)
1.171     kia      1955:     t->state |= templTemplate;
1.206     kia      1956: #endif /* TEMPLATES */
1.171     kia      1957: }
                   1958: 
                   1959: /*----------------------------------------------------------------------
                   1960:   SetDocumentAsXTigerLibrary
                   1961:   Set the document template structure as template library.
                   1962:   ----------------------------------------------------------------------*/
1.218     vatton   1963: void SetDocumentAsXTigerLibrary (Document doc)
1.171     kia      1964: {
                   1965: #ifdef TEMPLATES
1.221   ! vatton   1966:   XTigerTemplate t = GetXTigerDocTemplate (doc);
1.209     vatton   1967:   if (t)
1.171     kia      1968:     t->state |= templLibrary;
1.206     kia      1969: #endif /* TEMPLATES */
1.171     kia      1970: }
1.174     kia      1971: 
                   1972: /*----------------------------------------------------------------------
                   1973:   TemplateCreateTextBox
                   1974:   Create a xt:use types="string" box around the selection.
                   1975:   ----------------------------------------------------------------------*/
1.221   ! vatton   1976: void Template_CreateTextBox (Document doc, ThotBool createComp)
1.174     kia      1977: {
                   1978: #ifdef TEMPLATES
1.221   ! vatton   1979:   Element     selElem, comp, use, child, copy, head;
        !          1980:   ElementType selType, useType, compType;
        !          1981:   XTigerTemplate t;
1.174     kia      1982:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
1.221   ! vatton   1983:   int         firstChar, lastChar, sz = 128;
1.174     kia      1984:   char        buffer[128];
1.221   ! vatton   1985:   const char *title, *label;
1.218     vatton   1986:   ThotBool    oldStructureChecking, open;
1.206     kia      1987: 
1.174     kia      1988:   if (!TtaGetDocumentAccessMode(doc))
                   1989:     return;
1.206     kia      1990: 
1.209     vatton   1991:   if (doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1.174     kia      1992:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1993:     {
                   1994:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.209     vatton   1995:       if (selElem)
1.174     kia      1996:         {
                   1997:           selType =  TtaGetElementType(selElem);
1.209     vatton   1998:           if (!TtaIsLeaf(selType))
1.174     kia      1999:             {
                   2000:               selElem = TtaGetFirstLeaf(selElem);
                   2001:               selType = TtaGetElementType(selElem);
                   2002:               firstChar = lastChar = 0;
                   2003:             }
1.207     vatton   2004:           if (selType.ElTypeNum == 1)
1.174     kia      2005:             {
1.207     vatton   2006:               // request the element label
1.221   ! vatton   2007:               title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
        !          2008:               if (createComp)
        !          2009:                 label = TtaGetMessage (AMAYA, AM_NAME);
        !          2010:               else
        !          2011:                 label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
        !          2012:               QueryStringFromUser(label, title, buffer, 32);
1.207     vatton   2013:               if (buffer[0] == EOS)
1.218     vatton   2014:                 // stop the creation
1.207     vatton   2015:                 return;
                   2016: 
1.218     vatton   2017:               open = TtaHasUndoSequence (doc);
1.174     kia      2018:               useType.ElSSchema = sstempl;
                   2019:               useType.ElTypeNum = Template_EL_useSimple;
1.189     kia      2020:               oldStructureChecking = TtaGetStructureChecking (doc);
                   2021:               TtaSetStructureChecking (FALSE, doc);
1.218     vatton   2022:               if (firstChar == 0)
1.174     kia      2023:                 {
1.218     vatton   2024:                   // the whole string is selected
1.174     kia      2025:                   use = TtaNewElement(doc, useType);
1.209     vatton   2026:                   if (use)
1.174     kia      2027:                     {
1.218     vatton   2028:                       if (!open)
                   2029:                         TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.221   ! vatton   2030:                       // create the use element
        !          2031:                       TtaInsertSibling (use, selElem, FALSE, doc);
        !          2032:                       SetAttributeStringValue (use, Template_ATTR_types, "string");
        !          2033:                       Template_SetLabel (doc, use, buffer, FALSE);
        !          2034:                       TtaRegisterElementCreate (use, doc);
        !          2035:                       // move the string
1.174     kia      2036:                       TtaRegisterElementDelete (selElem, doc);
1.218     vatton   2037:                       TtaRemoveTree (selElem, doc);
1.221   ! vatton   2038:                       TtaInsertFirstChild (&selElem, use, doc);
        !          2039:                       TtaRegisterElementCreate (selElem, doc);
1.218     vatton   2040:                       if (!open)
                   2041:                         TtaCloseUndoSequence (doc);
                   2042:                       TtaSelectElement (doc, use);
1.174     kia      2043:                     }
                   2044:                 }
1.218     vatton   2045:               else if (GenerateInlineElement (Template_EL_useSimple, sstempl, 0, "", TRUE))
1.174     kia      2046:                 {
1.218     vatton   2047:                   // only a piece of the string is selected
                   2048:                   TtaGiveFirstSelectedElement (doc, &use, &firstChar, &lastChar);
                   2049:                   selType = TtaGetElementType (use);
                   2050:                   while (use &&
                   2051:                          (selType.ElSSchema != sstempl ||
                   2052:                           selType.ElTypeNum != Template_EL_useSimple))
1.174     kia      2053:                     {
1.218     vatton   2054:                       // look for the enclosing use element
                   2055:                       use = TtaGetParent (use);
                   2056:                       selType = TtaGetElementType (use);
                   2057:                     }
                   2058:                   if (selType.ElSSchema == sstempl &&
                   2059:                       selType.ElTypeNum == Template_EL_useSimple)
                   2060:                     {
                   2061:                       if (!open)
                   2062:                         TtaExtendUndoSequence (doc);
1.221   ! vatton   2063:                       Template_SetLabel (doc, use, buffer, TRUE);
        !          2064:                       if (createComp)
        !          2065:                         {
        !          2066:                          // Create the component
        !          2067:                           head = TemplateFindHead (doc);
        !          2068:                           child = TtaGetLastChild (head);
        !          2069:                           compType.ElSSchema = sstempl;
        !          2070:                           compType.ElTypeNum = Template_EL_component;
        !          2071:                           comp = TtaNewElement(doc, compType);
        !          2072:                           if (child)
        !          2073:                             TtaInsertSibling(comp, child, FALSE, doc);
        !          2074:                           else
        !          2075:                             TtaInsertFirstChild (&comp, head, doc);
        !          2076:                           child = TtaGetFirstChild (use);
        !          2077:                           copy = TtaCopyTree (child, doc, doc, comp);
        !          2078:                           TtaInsertFirstChild (&copy, comp, doc);
        !          2079:                           // register and update the component name
        !          2080:                           Template_SetName (doc, comp, buffer, FALSE);
        !          2081:                           TtaRegisterElementCreate (comp, doc);
        !          2082:                           GiveAttributeStringValueFromNum (comp, Template_ATTR_name, buffer, &sz);
        !          2083:                           // Declare the new component
        !          2084:                           t = GetXTigerDocTemplate(doc);
        !          2085:                           Template_DeclareNewComponent (t, buffer, comp, 1);
        !          2086:                           SetAttributeStringValueWithUndo (use, Template_ATTR_types, buffer);
        !          2087:                           SetAttributeStringValueWithUndo (use, Template_ATTR_currentType, buffer);
        !          2088:                           TtaSelectElement (doc, use);
        !          2089:                         }
        !          2090:                       else
        !          2091:                         {
        !          2092:                           SetAttributeStringValueWithUndo (use, Template_ATTR_types, "string");
        !          2093:                           // update the status bar
        !          2094:                           TtaSetStatusSelectedElement (doc, 1, use);
        !          2095:                         }
1.218     vatton   2096:                       if (!open)
1.221   ! vatton   2097:                         TtaCloseUndoSequence (doc);
1.174     kia      2098:                     }
                   2099:                 }
1.189     kia      2100:               TtaSetStructureChecking (oldStructureChecking, doc);
1.174     kia      2101:             }
                   2102:         }
                   2103:     }
                   2104: #endif /* TEMPLATES */
                   2105: }
                   2106: 
                   2107: 
                   2108: /*----------------------------------------------------------------------
1.221   ! vatton   2109:   TemplateCreateTextBox
        !          2110:   Create a xt:use types="string" box around the selection.
        !          2111:   ----------------------------------------------------------------------*/
        !          2112: void TemplateCreateTextBox (Document doc, View view)
        !          2113: {
        !          2114: #ifdef TEMPLATES
        !          2115:   Template_CreateTextBox (doc, FALSE);
        !          2116: #endif /* TEMPLATES */
        !          2117: }
        !          2118: 
        !          2119: 
        !          2120: /*----------------------------------------------------------------------
1.174     kia      2121:   TemplateCreateFreeBox
                   2122:   Create a xt:bag types="string" box around the selection.
                   2123:   ----------------------------------------------------------------------*/
1.218     vatton   2124: void TemplateCreateFreeBox (Document doc, View view)
1.174     kia      2125: {
                   2126: #ifdef TEMPLATES
1.218     vatton   2127:   ThotBool        oldStructureChecking;
                   2128:   DisplayMode     dispMode;
                   2129:   Element         selElem, selElem2, parent, parent2, current;
                   2130:   Element         bag, child = NULL;
                   2131:   ElementType     selType, selType2, bagType;
                   2132:   SSchema         sstempl, sshtml;
                   2133:   int             firstChar, lastChar, firstChar2, lastChar2;
                   2134:   char        buffer[128],  *title, *label;
1.174     kia      2135: 
                   2136:   if (!TtaGetDocumentAccessMode(doc))
                   2137:     return;
1.218     vatton   2138: 
                   2139:   sstempl = TtaGetSSchema ("Template", doc);
                   2140:   if (doc && sstempl &&
1.174     kia      2141:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2142:     {
                   2143:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.218     vatton   2144:       if (selElem)
1.207     vatton   2145:         {
1.218     vatton   2146:           TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   2147:           sshtml  = TtaGetSSchema ("HTML", doc);
                   2148:           selType = TtaGetElementType(selElem);
                   2149:           selType2 = TtaGetElementType(selElem2);
                   2150:           bagType.ElSSchema = sstempl;
                   2151:           bagType.ElTypeNum = Template_EL_bag;
                   2152:           parent = TtaGetParent(selElem);
                   2153:           parent2 = TtaGetParent(selElem2);
                   2154:           if (firstChar != 0 || firstChar2 != 0 || parent != parent2)
                   2155:             {
                   2156:               TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
                   2157:               return;
                   2158:             }
                   2159:           else if ((selType.ElSSchema == sshtml &&
                   2160:                     (selType.ElTypeNum == HTML_EL_List_Item ||
                   2161:                      selType.ElTypeNum == HTML_EL_Term ||
                   2162:                      selType.ElTypeNum == HTML_EL_Definition ||
                   2163:                      selType.ElTypeNum == HTML_EL_Option_item ||
                   2164:                      selType.ElTypeNum == HTML_EL_CAPTION ||
                   2165:                      selType.ElTypeNum == HTML_EL_Table_row ||
                   2166:                      selType.ElTypeNum == HTML_EL_thead ||
                   2167:                      selType.ElTypeNum == HTML_EL_tbody ||
                   2168:                      selType.ElTypeNum == HTML_EL_tfoot)) ||
                   2169:                    (selType2.ElSSchema == sshtml &&
                   2170:                     (selType2.ElTypeNum == HTML_EL_List_Item ||
                   2171:                      selType2.ElTypeNum == HTML_EL_Term ||
                   2172:                      selType2.ElTypeNum == HTML_EL_Definition ||
                   2173:                      selType2.ElTypeNum == HTML_EL_Option_item ||
                   2174:                      selType2.ElTypeNum == HTML_EL_CAPTION ||
                   2175:                      selType2.ElTypeNum == HTML_EL_Table_row ||
                   2176:                      selType2.ElTypeNum == HTML_EL_thead ||
                   2177:                      selType2.ElTypeNum == HTML_EL_tbody ||
                   2178:                      selType2.ElTypeNum == HTML_EL_tfoot)))
                   2179:             {
                   2180:               TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
                   2181:               return;
                   2182:             }
                   2183:           else if (selElem != selElem2 &&
                   2184:                    ((selType.ElSSchema == sshtml &&
                   2185:                     (selType.ElTypeNum == HTML_EL_Table_cell ||
                   2186:                      selType.ElTypeNum == HTML_EL_Data_cell ||
                   2187:                      selType.ElTypeNum == HTML_EL_Heading_cell)) ||
                   2188:                    (selType2.ElSSchema == sshtml &&
                   2189:                     (selType2.ElTypeNum == HTML_EL_Table_cell ||
                   2190:                      selType2.ElTypeNum == HTML_EL_Data_cell ||
                   2191:                      selType2.ElTypeNum == HTML_EL_Heading_cell))))
                   2192:             {
                   2193:               TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
                   2194:               return;
                   2195:             }
                   2196: 
1.207     vatton   2197:           // request the element label
1.218     vatton   2198:           title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
                   2199:           label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.221   ! vatton   2200:           QueryStringFromUser(label, title, buffer, 32);
1.207     vatton   2201:           if (buffer[0] == EOS)
                   2202:             return;
1.206     kia      2203: 
1.218     vatton   2204:           dispMode = TtaGetDisplayMode (doc);
                   2205:           if (dispMode == DisplayImmediately)
                   2206:             TtaSetDisplayMode (doc, DeferredDisplay);
                   2207:           oldStructureChecking = TtaGetStructureChecking (doc);
                   2208:           TtaSetStructureChecking (FALSE, doc);
1.206     kia      2209: 
1.218     vatton   2210:           // Create and insert xt:bag element
                   2211:           bag = TtaNewElement(doc, bagType);
                   2212:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2213:           if (selType.ElSSchema == sshtml &&
                   2214:               (selType.ElTypeNum == HTML_EL_Table_cell ||
                   2215:                selType.ElTypeNum == HTML_EL_Data_cell ||
                   2216:                selType.ElTypeNum == HTML_EL_Heading_cell))
1.174     kia      2217:             {
1.221   ! vatton   2218:               // change the selection to children
1.218     vatton   2219:               selElem = TtaGetFirstChild (selElem);
                   2220:               selElem2 = TtaGetLastChild (selElem2);
                   2221:             }
1.185     kia      2222: 
1.218     vatton   2223:           TtaInsertSibling (bag, selElem, TRUE, doc);
1.221   ! vatton   2224:           SetAttributeStringValue (bag, Template_ATTR_types, "any");
        !          2225:           Template_SetLabel (doc, bag, buffer, FALSE);
1.218     vatton   2226:           TtaRegisterElementCreate (bag, doc);
1.221   ! vatton   2227:           // move the selection into the new bag
1.218     vatton   2228:           TtaNextSibling (&selElem2);
                   2229:           while (selElem != selElem2)
                   2230:             {
                   2231:               current = selElem;
                   2232:               TtaNextSibling(&selElem);
                   2233:               TtaRegisterElementDelete (current, doc);
                   2234:               TtaRemoveTree (current, doc);
                   2235:               if (child)
                   2236:                 TtaInsertSibling(current, child, FALSE, doc);
                   2237:               else
                   2238:                 TtaInsertFirstChild(&current, bag, doc);
                   2239:               TtaRegisterElementDelete (current, doc);
                   2240:               child = current;
                   2241:             }
1.185     kia      2242: 
1.218     vatton   2243:           TtaSetDisplayMode (doc, dispMode);
                   2244:           TtaCloseUndoSequence(doc);
                   2245:           TtaSelectElement (doc, bag);
                   2246:           // update the status bar
                   2247:           TtaSetStatusSelectedElement (doc, 1, bag);
                   2248:           TtaSetStructureChecking (oldStructureChecking, doc);
1.174     kia      2249:         }
                   2250:     }
                   2251: #endif /* TEMPLATES */
                   2252: }
                   2253: 
1.180     kia      2254: /*----------------------------------------------------------------------
1.198     kia      2255:   TemplateCreateUnion
                   2256:   Create a xt:union around the selection.
                   2257:   ----------------------------------------------------------------------*/
1.218     vatton   2258: void TemplateCreateUnion (Document doc, View view)
1.198     kia      2259: {
                   2260: #ifdef TEMPLATES
1.199     kia      2261:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
1.218     vatton   2262:   Element        head, sibling, unionEl, selElem;
                   2263:   ElementType    unionType, selType;
1.199     kia      2264:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2265:   char          *proposed, *name = NULL, *types=NULL;
1.218     vatton   2266:   int            firstChar, lastChar;
1.206     kia      2267: 
1.209     vatton   2268:   if (t && sstempl)
1.199     kia      2269:     {
1.200     kia      2270:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   2271:       selType =  TtaGetElementType(selElem);
1.218     vatton   2272:       if (selType.ElSSchema == sstempl && selType.ElTypeNum == Template_EL_union)
1.200     kia      2273:         Template_ModifyUnionElement(doc, selElem);
                   2274:       else
1.199     kia      2275:         {
1.200     kia      2276:           proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
1.209     vatton   2277:           if (QueryUnionFromUser(proposed, NULL, &name, &types, TRUE))
1.200     kia      2278:             {
                   2279:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.206     kia      2280: 
1.200     kia      2281:               head = TemplateFindHead(doc);
                   2282:               sibling = TtaGetLastChild(head);
1.206     kia      2283: 
1.200     kia      2284:               unionType.ElSSchema = sstempl;
                   2285:               unionType.ElTypeNum = Template_EL_union;
                   2286:               unionEl = TtaNewElement(doc, unionType);
1.206     kia      2287: 
1.209     vatton   2288:               if (sibling)
1.200     kia      2289:                 TtaInsertSibling(unionEl, sibling, FALSE, doc);
                   2290:               else
                   2291:                 {
                   2292:                   sibling = unionEl;
                   2293:                   TtaInsertFirstChild(&sibling, head, doc);
                   2294:                 }
1.221   ! vatton   2295:               Template_SetName(doc, unionEl, name, FALSE);
1.200     kia      2296:               SetAttributeStringValue(unionEl, Template_ATTR_includeAt, types);
                   2297:               TtaSetAccessRight(unionEl, ReadOnly, doc);
                   2298:               TtaRegisterElementCreate(unionEl, doc);
                   2299:               TtaSelectElement(doc, unionEl);
                   2300:               TtaCloseUndoSequence(doc);
                   2301:               Template_DeclareNewUnion (t, name, types, "");
                   2302:               TtaFreeMemory(proposed);
                   2303:               TtaFreeMemory(name);
                   2304:               TtaFreeMemory(types);
1.206     kia      2305:             }
1.200     kia      2306:         }
1.206     kia      2307:     }
1.200     kia      2308: #endif /* TEMPLATES */
                   2309: }
1.199     kia      2310: 
1.200     kia      2311: /*----------------------------------------------------------------------
                   2312:   Template_ModifyUnionElement
                   2313:   Query the user to modify an xt:union
                   2314:   ----------------------------------------------------------------------*/
                   2315: void Template_ModifyUnionElement(Document doc, Element unionEl)
                   2316: {
                   2317:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   2318:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   2319:   ElementType    unionType;
                   2320:   char          *proposed, *checked, *name, *types=NULL;
1.206     kia      2321: 
1.209     vatton   2322:   if (doc && unionEl && t && sstempl)
1.200     kia      2323:     {
                   2324:       unionType = TtaGetElementType(unionEl);
1.218     vatton   2325:       if (unionType.ElSSchema == sstempl && unionType.ElTypeNum == Template_EL_union)
1.200     kia      2326:         {
                   2327:           proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
1.218     vatton   2328:           checked = GetAttributeStringValueFromNum(unionEl, Template_ATTR_includeAt, NULL);
                   2329:           name = GetAttributeStringValueFromNum(unionEl, Template_ATTR_name, NULL);
1.209     vatton   2330:           if (QueryUnionFromUser(proposed, checked, &name, &types, FALSE))
1.199     kia      2331:             {
1.200     kia      2332:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2333:               SetAttributeStringValueWithUndo(unionEl, Template_ATTR_includeAt, types);
                   2334:               TtaCloseUndoSequence(doc);
1.199     kia      2335:             }
1.200     kia      2336:           TtaFreeMemory(proposed);
                   2337:           TtaFreeMemory(checked);
                   2338:           TtaFreeMemory(name);
                   2339:           TtaFreeMemory(types);
1.199     kia      2340:         }
1.200     kia      2341:     }
1.198     kia      2342: }
                   2343: 
                   2344: /*----------------------------------------------------------------------
1.180     kia      2345:   TemplateCreateRepeat
                   2346:   Create a xt:repeat around the selection.
                   2347:   ----------------------------------------------------------------------*/
1.218     vatton   2348: void TemplateCreateRepeat (Document doc, View view)
1.180     kia      2349: {
1.218     vatton   2350:   Template_CreateRepeatFromSelection (doc, view, FALSE);
1.198     kia      2351: }
                   2352: 
                   2353: /*----------------------------------------------------------------------
                   2354:   TemplateCreateRepeatComp
                   2355:   Create a xt:component with the selection and use it in a new xt:repeat
                   2356:   ----------------------------------------------------------------------*/
1.217     vatton   2357: void TemplateCreateRepeatComp (Document doc, View view)
1.198     kia      2358: {
1.218     vatton   2359:   Template_CreateRepeatFromSelection (doc, view, TRUE);
1.198     kia      2360: }
                   2361: 
                   2362: /*----------------------------------------------------------------------
                   2363:   Template_CreateRepeatFromSelection
                   2364:   Create a xt:repeat with the selection.
                   2365:   If selection is empty, insert an inline xt:use.
1.206     kia      2366:   If createComp is false, create a xt:use with types at the selected block.
1.198     kia      2367:   If createComp is true, create a component with the selection.
                   2368:   Return the xt:use element.
                   2369:   ----------------------------------------------------------------------*/
1.218     vatton   2370: Element Template_CreateRepeatFromSelection (Document doc, int view,
                   2371:                                             ThotBool createComp)
1.198     kia      2372: {
1.180     kia      2373: #ifdef TEMPLATES
1.187     kia      2374:   ThotBool    oldStructureChecking;
                   2375:   DisplayMode dispMode;
1.220     vatton   2376:   Element     selElem, selElem2, parent, parent2, rep = NULL, use;
                   2377:   ElementType selType, selType2, repType;
1.180     kia      2378:   int         firstChar, lastChar, firstChar2, lastChar2;
                   2379:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
1.220     vatton   2380:   const char *title, *label;
                   2381:   char        buffer[128], *types;
1.180     kia      2382: 
                   2383:   if (!TtaGetDocumentAccessMode(doc))
1.198     kia      2384:     return NULL;
1.206     kia      2385: 
1.209     vatton   2386:   if (doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1.180     kia      2387:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2388:     {
                   2389:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.220     vatton   2390:       selType =  TtaGetElementType(selElem);
1.180     kia      2391:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1.220     vatton   2392:       selType2 =  TtaGetElementType(selElem2);
                   2393:       repType.ElSSchema = sstempl;
                   2394:       repType.ElTypeNum = Template_EL_repeat;
                   2395:       parent  = TtaGetParent(selElem);
                   2396:       parent2  = TtaGetParent(selElem2);
                   2397:       if (selElem && selElem2 && firstChar == 0 && firstChar2 == 0 &&
                   2398:           parent == parent2)
1.180     kia      2399:         {
1.220     vatton   2400:           oldStructureChecking = TtaGetStructureChecking (doc);
                   2401:           if (selElem == selElem2 && selType.ElSSchema == sstempl &&
                   2402:               (selType.ElTypeNum == Template_EL_useEl ||
                   2403:                selType.ElTypeNum == Template_EL_useSimple))
                   2404:             use = selElem;
                   2405:           else
                   2406:             // create first the use element
                   2407:             use = Template_CreateUseFromSelection (doc, view, createComp);
                   2408:           if (use)
                   2409:             {
                   2410:               // avoid to repeat a use string
                   2411:               types = GetAttributeStringValueFromNum (use, Template_ATTR_types, NULL);
                   2412:               if (types && strcmp (types, "string"))
1.187     kia      2413:                 {
1.220     vatton   2414:                   TtaFreeMemory (types);
                   2415:                   // request the element label
                   2416:                   title = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATCOMP);
                   2417:                   label = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATLABEL);
1.221   ! vatton   2418:                   QueryStringFromUser(label, title, buffer, 32);
1.220     vatton   2419:                   if (buffer[0] == EOS)
                   2420:                     return NULL;
                   2421:                   if (use == selElem)
                   2422:                     TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2423:                   else
                   2424:                     TtaExtendUndoSequence(doc);
                   2425:                   dispMode = TtaGetDisplayMode (doc);
                   2426:                   if (dispMode == DisplayImmediately)
                   2427:                     TtaSetDisplayMode (doc, DeferredDisplay);
                   2428:                   TtaSetStructureChecking (FALSE, doc);
                   2429:                   // create the repeat element
1.187     kia      2430:                   rep = TtaNewElement(doc, repType);
                   2431:                   TtaInsertSibling(rep, use, FALSE, doc);
1.221   ! vatton   2432:                   Template_SetLabel (doc, rep, buffer, FALSE);
1.220     vatton   2433:                   TtaRegisterElementCreate (rep, doc);
1.221   ! vatton   2434:                   // move the use element inot the repeat
1.187     kia      2435:                   TtaRegisterElementDelete (use, doc);
                   2436:                   TtaRemoveTree(use, doc);
1.220     vatton   2437:                   TtaInsertFirstChild (&use, rep, doc);
                   2438:                   TtaRegisterElementCreate (use, doc);
                   2439:                   TtaSetDisplayMode (doc, dispMode);
                   2440:                   TtaSelectElement (doc, use);
                   2441:                   TtaCloseUndoSequence (doc);
                   2442:                   TtaSetStructureChecking (oldStructureChecking, doc);
                   2443:                   // register document modification
                   2444:                   TtaSetDocumentModified (doc);
1.187     kia      2445:                 }
                   2446:             }
1.220     vatton   2447:           return rep;
                   2448:         }
                   2449:       else
                   2450:         {
                   2451:           TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
                   2452:           return NULL;
1.187     kia      2453:         }
                   2454:     }
                   2455: #endif /* TEMPLATES */
1.198     kia      2456:   return NULL;
1.195     kia      2457: }
                   2458: 
1.187     kia      2459: /*----------------------------------------------------------------------
                   2460:   TemplateCreateUseBox
                   2461:   Create a xt:use around the selection.
                   2462:   ----------------------------------------------------------------------*/
1.217     vatton   2463: void TemplateCreateUseBox (Document doc, View view)
1.187     kia      2464: {
                   2465: #ifdef TEMPLATES
1.218     vatton   2466:   Template_CreateUseFromSelection (doc, view, FALSE);
1.187     kia      2467: #endif /* TEMPLATES */
                   2468: }
                   2469: 
1.194     kia      2470: /*----------------------------------------------------------------------
1.195     kia      2471:   TemplateCreateUseCompBox
                   2472:   Create a xt:use around the selection.
                   2473:   ----------------------------------------------------------------------*/
1.217     vatton   2474: void TemplateCreateUseCompBox (Document doc, View view)
1.195     kia      2475: {
                   2476: #ifdef TEMPLATES
1.218     vatton   2477:   Template_CreateUseFromSelection (doc, view, TRUE);
1.195     kia      2478: #endif /* TEMPLATES */
                   2479: }
                   2480: 
                   2481: /*----------------------------------------------------------------------
1.194     kia      2482:   Template_CreateInlineUse
                   2483:   Create an inline xt:use with the selection.
                   2484:   ----------------------------------------------------------------------*/
1.221   ! vatton   2485: static Element Template_CreateInlineUse (Document doc)
1.194     kia      2486: {
                   2487: #ifdef TEMPLATES
                   2488:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
1.206     kia      2489:   Element        selElem, use = NULL;
1.221   ! vatton   2490:   ElementType    selType;
1.194     kia      2491:   XTigerTemplate t = GetXTigerDocTemplate(doc);
1.221   ! vatton   2492:   Declaration    dec;
1.194     kia      2493:   char          *proposed, *label = NULL, *types=NULL;
1.221   ! vatton   2494:   int            firstChar, lastChar;
1.206     kia      2495:   ThotBool       option;
                   2496: 
1.209     vatton   2497:   if (t && sstempl)
1.194     kia      2498:     {
1.221   ! vatton   2499:       TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
        !          2500:       if (selElem == NULL)
        !          2501:         return NULL;
1.194     kia      2502:       proposed = Template_GetInlineLevelDeclarations(t, TRUE, TRUE);
1.221   ! vatton   2503:       if (proposed &&
        !          2504:           QueryNewUseFromUser (proposed, &label, &types, &option))
1.194     kia      2505:         {
1.221   ! vatton   2506:           if (label && types)
        !          2507:             {
        !          2508:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
        !          2509:               if (strstr (types, " ") != NULL)
        !          2510:                 GenerateInlineElement (Template_EL_useEl, sstempl, 0, NULL, TRUE);
        !          2511:               else
        !          2512:                 {
        !          2513:                   // check if it's an union
        !          2514:                   dec = Template_GetUnionDeclaration (t, types);
        !          2515:                   if (dec)
        !          2516:                     GenerateInlineElement (Template_EL_useEl, sstempl, 0, NULL, TRUE);
        !          2517:                   else
        !          2518:                     GenerateInlineElement (Template_EL_useSimple, sstempl, 0, NULL, TRUE);
        !          2519:                 }
1.197     kia      2520: 
1.221   ! vatton   2521:               // complete the creation of the use element
        !          2522:               TtaGiveFirstSelectedElement (doc, &use, &firstChar, &lastChar);
        !          2523:               selType = TtaGetElementType (use);
        !          2524:               while (use &&
        !          2525:                      (selType.ElSSchema != sstempl ||
        !          2526:                       selType.ElTypeNum != Template_EL_useSimple))
        !          2527:                 {
        !          2528:                   // look for the enclosing use element
        !          2529:                   use = TtaGetParent (use);
        !          2530:                   selType = TtaGetElementType (use);
        !          2531:                 }
        !          2532:               SetAttributeStringValueWithUndo (use, Template_ATTR_types, types);
        !          2533:               Template_SetLabel (doc, use, label, TRUE);
        !          2534:               TtaSelectElement (doc, use);
        !          2535:               TtaCloseUndoSequence (doc);
        !          2536:             }
1.194     kia      2537:         }
1.221   ! vatton   2538:       TtaFreeMemory (proposed);
        !          2539:       TtaFreeMemory (label);
        !          2540:       TtaFreeMemory (types);
1.206     kia      2541:       return use;
                   2542:     }
1.194     kia      2543: #endif /* TEMPLATES */
1.221   ! vatton   2544:   return NULL;
1.194     kia      2545: }
                   2546: 
                   2547: /*----------------------------------------------------------------------
                   2548:   Template_CreateEmptyBlockUse
                   2549:   Create a block xt:use with the selection.
                   2550:   ----------------------------------------------------------------------*/
1.221   ! vatton   2551: static Element Template_CreateEmptyBlockUse (Document doc)
1.194     kia      2552: {
                   2553: #ifdef TEMPLATES
                   2554:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
                   2555:   ElementType    useType;
                   2556:   Element        selElem, use;
                   2557:   XTigerTemplate t = GetXTigerDocTemplate(doc);
1.221   ! vatton   2558:   Declaration    dec;
1.194     kia      2559:   char          *proposed, *label = NULL, *types=NULL;
1.221   ! vatton   2560:   int            firstChar, lastChar;
        !          2561:   ThotBool       option;
1.206     kia      2562: 
                   2563: 
1.209     vatton   2564:   if (t && sstempl)
1.194     kia      2565:     {
1.221   ! vatton   2566:       TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
        !          2567:       if (selElem == NULL)
        !          2568:         return NULL;
        !          2569: 
1.194     kia      2570:       proposed = Template_GetBlockLevelDeclarations(t, TRUE);
1.221   ! vatton   2571:       if (proposed &&
        !          2572:           QueryNewUseFromUser(proposed, &label, &types, &option))
1.194     kia      2573:         {
1.221   ! vatton   2574:           if (label && types)
        !          2575:             {
        !          2576:               TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
        !          2577:               // create the use element
        !          2578:               useType.ElSSchema = sstempl;
        !          2579:               if (strstr (types, " ") != NULL)
        !          2580:                 useType.ElTypeNum = Template_EL_useEl;
        !          2581:               else
        !          2582:                 {
        !          2583:                   // check if it's an union
        !          2584:                   dec = Template_GetUnionDeclaration (t, types);
        !          2585:                   if (dec)
        !          2586:                     useType.ElTypeNum = Template_EL_useEl;
        !          2587:                   else
        !          2588:                     useType.ElTypeNum = Template_EL_useSimple;
        !          2589:                 }
        !          2590:               use = TtaNewElement (doc, useType);
        !          2591:               TtaInsertSibling (use, selElem, FALSE, doc);
        !          2592:               SetAttributeStringValue (use, Template_ATTR_types, types);
        !          2593:               Template_SetLabel (doc, use, label, FALSE);
        !          2594:               TtaRegisterElementCreate (use, doc);
        !          2595:               // remove the current selection
        !          2596:               TtaDeleteTree (selElem, doc);
        !          2597:               TtaSelectElement (doc, use);
        !          2598:               TtaCloseUndoSequence (doc);
        !          2599:             }
1.194     kia      2600:         }
1.221   ! vatton   2601:       TtaFreeMemory (proposed);
        !          2602:       TtaFreeMemory (label);
        !          2603:       TtaFreeMemory (types);
1.206     kia      2604:       return use;
                   2605:     }
                   2606: 
1.194     kia      2607: #endif /* TEMPLATES */
1.221   ! vatton   2608:   return NULL;
1.194     kia      2609: }
1.187     kia      2610: 
                   2611: /*----------------------------------------------------------------------
                   2612:   Template_CreateUseFromSelection
                   2613:   Create a xt:use with the selection.
1.197     kia      2614:   If selection is empty, insert an inline xt:use.
1.206     kia      2615:   If createComp is false, create a xt:use with types at the selected block.
1.197     kia      2616:   If createComp is true, create a component with the selection.
                   2617:   Return the xt:use element.
1.187     kia      2618:   ----------------------------------------------------------------------*/
1.218     vatton   2619: Element Template_CreateUseFromSelection (Document doc, int view,
                   2620:                                          ThotBool createComp)
1.187     kia      2621: {
                   2622: #ifdef TEMPLATES
1.217     vatton   2623:   DisplayMode    dispMode;
                   2624:   Element        selElem, selElem2, parent, parent2;
                   2625:   Element        use = NULL, comp, prev, next;
                   2626:   ElementType    selType, selType2, useType;
                   2627:   SSchema        sstempl, sshtml;
1.206     kia      2628:   XTigerTemplate t;
1.217     vatton   2629:   int            firstChar, lastChar, firstChar2, lastChar2;
                   2630:   int            sz = 128, option;
                   2631:   char          *proposed, *checked, *types = NULL;
                   2632:   char           buffer[128];
                   2633:   const char    *title, *label, *name = NULL;
                   2634:   ThotBool       oldStructureChecking, opt = FALSE, getContent = FALSE;
1.187     kia      2635: 
                   2636:   if (!TtaGetDocumentAccessMode(doc))
                   2637:     return NULL;
1.206     kia      2638: 
1.217     vatton   2639:   sstempl = TtaGetSSchema ("Template", doc);
                   2640:   sshtml  = TtaGetSSchema ("HTML", doc);
1.209     vatton   2641:   if (doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1.187     kia      2642:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2643:     {
1.217     vatton   2644:       TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
                   2645:       TtaGiveLastSelectedElement (doc, &selElem2, &firstChar2, &lastChar2);
                   2646:       t = GetXTigerDocTemplate (doc);
                   2647:       if (selElem == NULL || t == NULL)
                   2648:         return NULL;
1.187     kia      2649: 
1.217     vatton   2650:       selType =  TtaGetElementType(selElem);
                   2651:       selType2 =  TtaGetElementType(selElem2);
                   2652:       parent  = TtaGetParent(selElem);
                   2653:       parent2 = TtaGetParent(selElem2);
                   2654:       dispMode = TtaGetDisplayMode (doc);
                   2655:       oldStructureChecking = TtaGetStructureChecking (doc);
                   2656:       TtaSetStructureChecking (FALSE, doc);
                   2657:       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.206     kia      2658: 
1.217     vatton   2659:       if (firstChar == 0 && firstChar2 == 0 && parent == parent2)
1.187     kia      2660:         {
1.217     vatton   2661:           if (selType.ElSSchema == sstempl)
1.187     kia      2662:             {
1.217     vatton   2663:               // The selection is a template element.
                   2664:               switch(selType.ElTypeNum)
1.206     kia      2665:                 {
1.217     vatton   2666:                 case Template_EL_useEl:
                   2667:                 case Template_EL_useSimple:
                   2668:                   // TODO query to add/remove some types
                   2669:                   if (TtaGetElementVolume(selElem) == 0)
1.209     vatton   2670:                     {
1.217     vatton   2671:                       proposed = Template_GetAllDeclarations(t, TRUE, TRUE, TRUE);
                   2672:                       checked = GetAttributeStringValueFromNum (selElem, Template_ATTR_types, NULL);
                   2673:                       option = GetAttributeIntValueFromNum (selElem, Template_ATTR_option);
                   2674:                       opt = (option == Template_ATTR_option_VAL_option_set);
                   2675:                       if (QueryUseFromUser (proposed, checked, buffer, 128, &types, &opt))
1.206     kia      2676:                         {
1.217     vatton   2677:                           SetAttributeStringValueWithUndo(selElem, Template_ATTR_types, types);
                   2678:                           SetAttributeStringValueWithUndo(selElem, Template_ATTR_title, buffer);
                   2679:                           if (opt)
                   2680:                             SetAttributeIntValue (selElem, Template_ATTR_option, Template_ATTR_option_VAL_option_set, TRUE);
                   2681:                           else
                   2682:                             SetAttributeIntValue (selElem, Template_ATTR_option, Template_ATTR_option_VAL_option_unset, TRUE);
                   2683:                           // register document modification
                   2684:                           TtaSetDocumentModified (doc);
1.206     kia      2685:                         }
1.217     vatton   2686:                       TtaFreeMemory (proposed);
                   2687:                       TtaFreeMemory (checked);
                   2688:                       TtaFreeMemory (types);
                   2689:                       createComp = FALSE;
1.209     vatton   2690:                     }
1.217     vatton   2691:                   break;
                   2692:                 default:
                   2693:                   // Do nothing
                   2694:                   break;
1.206     kia      2695:                 }
1.217     vatton   2696:             }
                   2697:           else if (selElem != selElem2 &&
                   2698:                    ((selType.ElSSchema == sshtml &&
                   2699:                     (selType.ElTypeNum == HTML_EL_List_Item ||
                   2700:                      selType.ElTypeNum == HTML_EL_Term ||
                   2701:                      selType.ElTypeNum == HTML_EL_Definition ||
                   2702:                      selType.ElTypeNum == HTML_EL_Option_item ||
                   2703:                      selType.ElTypeNum == HTML_EL_CAPTION ||
                   2704:                      selType.ElTypeNum == HTML_EL_Table_row ||
                   2705:                      selType.ElTypeNum == HTML_EL_Table_cell ||
                   2706:                      selType.ElTypeNum == HTML_EL_Data_cell ||
                   2707:                      selType.ElTypeNum == HTML_EL_Heading_cell ||
                   2708:                      selType.ElTypeNum == HTML_EL_thead ||
                   2709:                      selType.ElTypeNum == HTML_EL_tbody ||
                   2710:                      selType.ElTypeNum == HTML_EL_tfoot)) ||
                   2711:                    (selType2.ElSSchema == sshtml &&
                   2712:                     (selType2.ElTypeNum == HTML_EL_List_Item ||
                   2713:                      selType2.ElTypeNum == HTML_EL_Term ||
                   2714:                      selType2.ElTypeNum == HTML_EL_Definition ||
                   2715:                      selType2.ElTypeNum == HTML_EL_Option_item ||
                   2716:                      selType2.ElTypeNum == HTML_EL_CAPTION ||
                   2717:                      selType2.ElTypeNum == HTML_EL_Table_row ||
                   2718:                      selType2.ElTypeNum == HTML_EL_Table_cell ||
                   2719:                      selType2.ElTypeNum == HTML_EL_Data_cell ||
                   2720:                      selType2.ElTypeNum == HTML_EL_Heading_cell ||
                   2721:                      selType2.ElTypeNum == HTML_EL_thead ||
                   2722:                      selType2.ElTypeNum == HTML_EL_tbody ||
                   2723:                      selType2.ElTypeNum == HTML_EL_tfoot))))
                   2724:             {
                   2725:               TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
                   2726:               return NULL;
                   2727:             }
                   2728:           else if (selType.ElSSchema == sshtml &&
                   2729:                    selElem == selElem2 &&
                   2730:                    (selType.ElTypeNum == HTML_EL_Element ||
                   2731:                     selType.ElTypeNum == HTML_EL_Basic_Elem ||
                   2732:                     (selType.ElTypeNum == HTML_EL_Paragraph &&
                   2733:                      TtaGetElementVolume(selElem) == 0)))
                   2734:             // Create a block-level xt:use instead of selection.
                   2735:             use = Template_CreateEmptyBlockUse (doc);
                   2736:           else
                   2737:             {
1.218     vatton   2738:               if (dispMode == DisplayImmediately)
                   2739:                 TtaSetDisplayMode (doc, DeferredDisplay);
1.217     vatton   2740:               prev = selElem;
                   2741:               TtaPreviousSibling(&prev);
                   2742:               next = selElem2;
                   2743:               TtaNextSibling (&next);
                   2744:               if (selElem == selElem2 &&
                   2745:                   selType2.ElSSchema == sshtml &&
                   2746:                   (selType.ElTypeNum == HTML_EL_List_Item ||
                   2747:                    selType.ElTypeNum == HTML_EL_Term ||
                   2748:                    selType.ElTypeNum == HTML_EL_Definition ||
                   2749:                    selType.ElTypeNum == HTML_EL_Option_item ||
                   2750:                    selType.ElTypeNum == HTML_EL_CAPTION ||
                   2751:                    selType.ElTypeNum == HTML_EL_Table_row ||
                   2752:                    selType.ElTypeNum == HTML_EL_Table_cell ||
                   2753:                    selType.ElTypeNum == HTML_EL_Data_cell ||
                   2754:                    selType.ElTypeNum == HTML_EL_Heading_cell ||
                   2755:                    selType.ElTypeNum == HTML_EL_thead ||
                   2756:                    selType.ElTypeNum == HTML_EL_tbody ||
                   2757:                    selType.ElTypeNum == HTML_EL_tfoot))
1.206     kia      2758:                 {
1.217     vatton   2759:                   // special management
                   2760:                   createComp = FALSE;
1.218     vatton   2761:                   if (selType.ElTypeNum == HTML_EL_Table_cell ||
                   2762:                       selType.ElTypeNum == HTML_EL_Data_cell ||
                   2763:                       selType.ElTypeNum == HTML_EL_Heading_cell)
                   2764:                     // generate the use element around the first child
                   2765:                     // instead of the cell
                   2766:                     getContent = TRUE;
1.206     kia      2767:                 }
1.217     vatton   2768:               if (createComp)
1.186     kia      2769:                 {
1.217     vatton   2770:                   // Create a component from selection and a use using it
                   2771:                   comp = Template_CreateComponentFromSelection(doc);
                   2772:                   if (comp)
1.197     kia      2773:                     {
1.217     vatton   2774:                       useType.ElSSchema = sstempl;
                   2775:                       useType.ElTypeNum = Template_EL_useSimple;
                   2776:                       use = TtaNewElement (doc, useType);
                   2777:                       if (prev)
                   2778:                         TtaInsertSibling (use, prev, FALSE, doc);
                   2779:                       else if (next)
                   2780:                         TtaInsertSibling (use, next, TRUE, doc);
                   2781:                       else
                   2782:                         TtaInsertFirstChild (&use, parent, doc);
                   2783:                       GiveAttributeStringValueFromNum (comp, Template_ATTR_name, buffer, &sz);
1.221   ! vatton   2784:                       // Declare the new component
        !          2785:                       Template_DeclareNewComponent (t, buffer, comp, 2);
1.217     vatton   2786:                       SetAttributeStringValue (use, Template_ATTR_types, buffer);
                   2787:                       SetAttributeStringValue (use, Template_ATTR_currentType, buffer);
                   2788:                       SetAttributeStringValue (use, Template_ATTR_title, buffer);
                   2789:                       TtaRegisterElementCreate (use, doc);
                   2790:                       TtaSelectElement (doc, use);
                   2791:                       // register document modification
                   2792:                       TtaSetDocumentModified (doc);
1.197     kia      2793:                     }
1.217     vatton   2794:                 }
                   2795:               else
                   2796:                 {
                   2797:                   title = TtaGetMessage (AMAYA, AM_TEMPLATE_USELABEL);
                   2798:                   label = TtaGetMessage (AMAYA, AM_NAME);
1.221   ! vatton   2799:                   QueryStringFromUser (label, title, buffer, 32);
1.220     vatton   2800:                   if (buffer[0] != EOS)
1.194     kia      2801:                     {
1.197     kia      2802:                       // Create a xt:use around the selection
1.194     kia      2803:                       useType.ElSSchema = sstempl;
1.197     kia      2804:                       useType.ElTypeNum = Template_EL_useSimple;
1.217     vatton   2805:                       use = TtaNewElement (doc, useType);
                   2806:                       if (getContent)
                   2807:                         {
                   2808:                           parent = selElem;
                   2809:                           selElem = TtaGetFirstChild (parent);
                   2810:                           selElem2 = TtaGetLastChild (parent);
                   2811:                           prev = next = NULL;
                   2812:                         }
                   2813:                       if (selElem)
                   2814:                         {
                   2815:                           name = TtaGetElementTypeName(TtaGetElementType(selElem));
                   2816:                           TtaRegisterElementDelete (selElem, doc);
                   2817:                           TtaRemoveTree(selElem, doc);
                   2818:                           TtaInsertFirstChild (&selElem, use, doc);
                   2819:                         }
                   2820:                       else
                   2821:                         {
                   2822:                           useType.ElTypeNum = Template_EL_TEXT_UNIT;
                   2823:                           selElem = TtaNewElement (doc, useType);
                   2824:                           TtaInsertFirstChild (&selElem, use, doc);
                   2825:                         }
1.209     vatton   2826:                       if (prev)
1.194     kia      2827:                         TtaInsertSibling(use, prev, FALSE, doc);
1.217     vatton   2828:                       else if (next)
                   2829:                         TtaInsertSibling(use, next,TRUE, doc);
1.194     kia      2830:                       else
                   2831:                         TtaInsertFirstChild(&use, parent, doc);
1.217     vatton   2832:                       if (name && strcmp (name, "???") &&
                   2833:                           strcmp (name, "Pseudo_paragraph"))
                   2834:                         {
                   2835:                           SetAttributeStringValue (use, Template_ATTR_types, name);
                   2836:                           SetAttributeStringValue (use, Template_ATTR_currentType, name);
                   2837:                         }
                   2838:                       else
                   2839:                         {
                   2840:                           SetAttributeStringValue (use, Template_ATTR_types, "string");
                   2841:                           SetAttributeStringValue (use, Template_ATTR_currentType, "string");
1.218     vatton   2842:                         }
1.221   ! vatton   2843:                       Template_SetLabel (doc, use, buffer, FALSE);
1.218     vatton   2844:                       TtaRegisterElementCreate (use, doc);
1.217     vatton   2845:                       TtaSelectElement (doc, use);
                   2846:                       // register document modification
                   2847:                       TtaSetDocumentModified (doc);
1.194     kia      2848:                     }
1.186     kia      2849:                 }
1.218     vatton   2850:               if (dispMode == DisplayImmediately)
                   2851:                 TtaSetDisplayMode (doc, dispMode);
1.186     kia      2852:             }
1.217     vatton   2853:         }
                   2854:       else if (lastChar < firstChar)
1.218     vatton   2855:         use = Template_CreateInlineUse (doc);
                   2856:       else
1.221   ! vatton   2857:         Template_CreateTextBox (doc, createComp);
1.206     kia      2858: 
1.217     vatton   2859:       TtaSetStructureChecking (oldStructureChecking, doc);
                   2860:       TtaCloseUndoSequence(doc);
1.186     kia      2861:     }
1.187     kia      2862:   return use;
                   2863: #else /* TEMPLATES */
                   2864:   return NULL;
1.186     kia      2865: #endif /* TEMPLATES */
                   2866: }
                   2867: 
                   2868: /*----------------------------------------------------------------------
1.187     kia      2869:   Template_CreateComponentFromSelection
1.186     kia      2870:   Create a xt:component with the selection and move it into the xt:head.
1.187     kia      2871:   Return the xt:component element.
1.186     kia      2872:   ----------------------------------------------------------------------*/
1.217     vatton   2873: Element Template_CreateComponentFromSelection (Document doc)
1.186     kia      2874: {
                   2875: #ifdef TEMPLATES
1.217     vatton   2876:   ThotBool       oldStructureChecking;
                   2877:   DisplayMode    dispMode;
                   2878:   Element        selElem, selElem2, parent, parent2, current, child, head;
                   2879:   Element        comp = NULL;
                   2880:   ElementType    selType, selType2, compType;
                   2881:   SSchema        sstempl = TtaGetSSchema ("Template", doc);
1.192     kia      2882:   XTigerTemplate t = GetXTigerDocTemplate(doc);
1.217     vatton   2883:   int            firstChar, lastChar, firstChar2, lastChar2;
                   2884:   char           buffer[128];
                   2885:   const char    *title, *label;
                   2886:   ThotBool       closeUndo = FALSE;
1.186     kia      2887: 
1.209     vatton   2888:   if (doc && t && TtaGetDocumentAccessMode(doc) &&
1.186     kia      2889:       TtaGetDocumentAccessMode(doc) && sstempl &&
                   2890:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   2891:     {
1.217     vatton   2892:       TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
                   2893:       if (selElem == NULL)
                   2894:         {
                   2895:           TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
                   2896:           return NULL;
                   2897:         }
                   2898:       TtaGiveLastSelectedElement (doc, &selElem2, &firstChar2, &lastChar2);
                   2899:       selType = TtaGetElementType(selElem);
                   2900:       selType2 = TtaGetElementType(selElem2);
                   2901:       parent  = TtaGetParent(selElem);
                   2902:       parent2 = TtaGetParent(selElem2);
                   2903:       label = TtaGetMessage (AMAYA, AM_NAME);
                   2904:       title = TtaGetMessage (AMAYA, AM_TEMPLATE_NEWCOMP);
1.221   ! vatton   2905:       QueryStringFromUser(label, title, buffer, 32);
1.217     vatton   2906:       if (buffer[0] != EOS)
1.186     kia      2907:         {
1.221   ! vatton   2908:           head = TemplateFindHead (doc);
1.217     vatton   2909:           compType.ElSSchema = sstempl;
                   2910:           compType.ElTypeNum = Template_EL_component;
                   2911:           if (head && firstChar == 0 && firstChar2 == 0 && parent == parent2)
1.186     kia      2912:             {
1.217     vatton   2913:               dispMode = TtaGetDisplayMode (doc);
                   2914:               oldStructureChecking = TtaGetStructureChecking (doc);
                   2915:               TtaSetStructureChecking (FALSE, doc);
1.221   ! vatton   2916:               child = TtaGetLastChild (head);
1.217     vatton   2917:               if (TtaHasUndoSequence (doc))
                   2918:                 closeUndo = FALSE;
                   2919:               else
                   2920:                 {
                   2921:                   closeUndo = TRUE;
                   2922:                   TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   2923:                 }
1.206     kia      2924: 
1.220     vatton   2925:               if (dispMode == DisplayImmediately)
                   2926:                 TtaSetDisplayMode (doc, DeferredDisplay);
1.217     vatton   2927:               // Create the component
                   2928:               comp = TtaNewElement(doc, compType);
                   2929:               if (child)
                   2930:                 TtaInsertSibling(comp, child, FALSE, doc);
                   2931:               else
                   2932:                 TtaInsertFirstChild (&comp, head, doc);
                   2933:               // register and update the component name
1.221   ! vatton   2934:               Template_SetName (doc, comp, buffer, FALSE);
1.217     vatton   2935: 
                   2936:               TtaNextSibling (&selElem2);
                   2937:               child = NULL;
                   2938:               while (selElem != selElem2)
1.180     kia      2939:                 {
1.217     vatton   2940:                   current = selElem;
                   2941:                   TtaNextSibling (&selElem);
                   2942:                   TtaRegisterElementDelete (current, doc);
                   2943:                   TtaRemoveTree (current, doc);
1.209     vatton   2944:                   if (child)
1.217     vatton   2945:                     TtaInsertSibling (current, child, FALSE, doc);
1.185     kia      2946:                   else
1.217     vatton   2947:                     TtaInsertFirstChild (&current, comp, doc);
                   2948:                   //TtaRegisterElementCreate (current, doc);
                   2949:                   child = current;
1.180     kia      2950:                 }
1.217     vatton   2951:               TtaRegisterElementCreate (comp, doc);
                   2952:               if (closeUndo)
                   2953:                 TtaCloseUndoSequence(doc);
                   2954:               TtaSetStructureChecking (oldStructureChecking, doc);
                   2955:               if (dispMode == DisplayImmediately)
                   2956:                 TtaSetDisplayMode (doc, dispMode);
1.180     kia      2957:             }
                   2958:         }
                   2959:     }
1.186     kia      2960:   return comp;
1.187     kia      2961: #else /* TEMPLATES */
                   2962:   return NULL;
1.206     kia      2963: #endif /* TEMPLATES */
1.180     kia      2964: }
1.187     kia      2965: 
1.192     kia      2966: /*----------------------------------------------------------------------
                   2967:   TemplateComponentWillBeDeleted
                   2968:   Processed when a component element will be deleted in a template context.
                   2969:   ----------------------------------------------------------------------*/
                   2970: ThotBool TemplateComponentWillBeDeleted (NotifyElement *event)
                   2971: {
                   2972: #ifdef TEMPLATES
                   2973:   XTigerTemplate t = GetXTigerDocTemplate(event->document);
1.220     vatton   2974:   char          *elemName;
1.206     kia      2975: 
1.220     vatton   2976:   elemName = GetAttributeStringValueFromNum(event->element, Template_ATTR_name, NULL);
                   2977:   if (Template_IsUsedComponent (t, event->document, elemName))
1.192     kia      2978:     {
                   2979:       TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_TEMPLATE_USEDCOMP_CANTREMOVE);
                   2980:       return TRUE;
                   2981:     }
1.206     kia      2982: #endif /* TEMPLATES */
1.192     kia      2983:   return FALSE;
                   2984: }
1.187     kia      2985: 
1.200     kia      2986: /*----------------------------------------------------------------------
                   2987:   UnionClicked
1.206     kia      2988:   Called when a xt:union is clicked
1.200     kia      2989:   ----------------------------------------------------------------------*/
                   2990: void UnionClicked(NotifyElement* event)
                   2991: {
1.209     vatton   2992:   if (event->document && event->element)
1.221   ! vatton   2993:     TtaSelectElement (event->document, event->element);
1.200     kia      2994: }
                   2995: 
                   2996: /*----------------------------------------------------------------------
                   2997:   UnionDoubleClicked
1.206     kia      2998:   Called when a xt:union is double clicked
1.200     kia      2999:   ----------------------------------------------------------------------*/
                   3000: ThotBool UnionDoubleClicked(NotifyElement* event)
                   3001: {
                   3002:   Template_ModifyUnionElement(event->document, event->element);
                   3003:   TtaSelectElement(event->document, event->element);
                   3004:   return TRUE;
                   3005: }
                   3006: 
                   3007: /*----------------------------------------------------------------------
                   3008:   TemplateNameAttributeDeleted
1.206     kia      3009:   Called when a xt:name will be deleted
1.200     kia      3010:   ----------------------------------------------------------------------*/
                   3011: ThotBool TemplateNameAttributeDeleted(NotifyAttribute* event)
                   3012: {
                   3013:   // Prevent xt:name deletion
                   3014:   return TRUE;
                   3015: }
                   3016: 
                   3017: /*----------------------------------------------------------------------
                   3018:   TemplateNameAttributeModified
1.206     kia      3019:   Called when a xt:name will be modified
1.200     kia      3020:   ----------------------------------------------------------------------*/
                   3021: ThotBool TemplateNameAttributeModified(NotifyAttribute* event)
                   3022: {
                   3023:   // Prevent xt:name modification
                   3024:   return TRUE;
                   3025: }
                   3026: 
                   3027: /*----------------------------------------------------------------------
                   3028:   TemplateNameAttributeCreated
1.206     kia      3029:   Called when a xt:name have been created
1.200     kia      3030:   ----------------------------------------------------------------------*/
1.203     vatton   3031: void TemplateNameAttributeCreated(NotifyAttribute* event)
1.200     kia      3032: {
1.202     kia      3033:   MakeUniqueName(event->element, event->document, TRUE, FALSE);
1.200     kia      3034: }
                   3035: 
                   3036: 
                   3037: /*----------------------------------------------------------------------
                   3038:   TemplateNameAttrInMenu
                   3039:   Called by Thot when building the Attributes menu for template elements.
                   3040:   ----------------------------------------------------------------------*/
                   3041: ThotBool TemplateNameAttrInMenu (NotifyAttribute * event)
                   3042: {
                   3043: #ifdef TEMPLATES
                   3044:   ElementType type = TtaGetElementType(event->element);
1.209     vatton   3045:   if (type.ElTypeNum==Template_EL_component ||
1.200     kia      3046:       type.ElTypeNum==Template_EL_union)
                   3047:     return TRUE;
                   3048: #endif /* TEMPLATES */
1.209     vatton   3049:   return FALSE;
1.200     kia      3050: }
                   3051: 

Webmaster