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

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

Webmaster