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

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

Webmaster