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

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

Webmaster