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

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

Webmaster