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

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

Webmaster