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

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

Webmaster