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

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

Webmaster