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

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

Webmaster