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

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

Webmaster