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

1.1       cvs         1: /*
                      2:  *
1.162     vatton      3:  *  COPYRIGHT INRIA and W3C, 1996-2008
1.1       cvs         4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
1.14      cvs         7:  
1.1       cvs         8: /*
1.51      francesc    9:  * Authors: Francesc Campoy Flores
1.171     kia        10:  *          Ă‰milien Kia
1.1       cvs        11:  *
                     12:  */
                     13: 
                     14: #define THOT_EXPORT extern
                     15: #include "amaya.h"
                     16: #include "document.h"
1.99      kia        17: #include "undo.h"
1.90      kia        18: #include "containers.h"
                     19: #include "Elemlist.h"
1.92      kia        20: #include "templates.h"
                     21: 
1.46      vatton     22: #ifdef TEMPLATES
                     23: #include "Template.h"
1.52      vatton     24: #include "templateDeclarations.h"
1.140     vatton     25: 
                     26: #include "html2thot_f.h"
1.174     kia        27: #include "HTMLedit_f.h"
1.122     kia        28: #include "templates_f.h"
1.89      kia        29: #include "templateUtils_f.h"
1.67      quint      30: #include "templateLoad_f.h"
                     31: #include "templateDeclarations_f.h"
1.76      vatton     32: #include "templateInstantiate_f.h"
1.145     kia        33: #include "Templatebuilder_f.h"
1.28      tollenae   34: #include "appdialogue_wx.h"
1.29      tollenae   35: #include "init_f.h"
1.46      vatton     36: #include "wxdialogapi_f.h"
1.60      francesc   37: #include "AHTURLTools_f.h"
1.52      vatton     38: #endif /* TEMPLATES */
1.1       cvs        39: 
1.87      kia        40: #include "fetchXMLname_f.h"
1.83      kia        41: #include "MENUconf.h"
                     42: 
                     43: /* Paths from which looking for templates.*/
                     44: static Prop_Templates_Path *TemplateRepositoryPaths;
                     45: 
1.87      kia        46: /*----------------------------------------------------------------------
                     47:   IsTemplateInstanceDocument: Test if a document is a template instance
                     48:   doc : Document to test
                     49:   return : TRUE if the document is a template instance
                     50:   ----------------------------------------------------------------------*/
1.106     vatton     51: ThotBool IsTemplateInstanceDocument(Document doc)
                     52: {
1.87      kia        53: #ifdef TEMPLATES
1.171     kia        54:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                     55:   if(t)
1.176     vatton     56:     return (t->state & templInstance) != 0;
1.171     kia        57:   else
                     58:     return FALSE;
                     59: #else /* TEMPLATES */
1.88      cvs        60:   return FALSE;
1.171     kia        61: #endif /* TEMPLATES */ 
1.87      kia        62: }
                     63: 
1.83      kia        64: /*----------------------------------------------------------------------
1.109     kia        65:   IsTemplateDocument: Test if a document is a template (not an instance)
                     66:   doc : Document to test
1.167     kia        67:   return : TRUE if the document is a template
1.109     kia        68:   ----------------------------------------------------------------------*/
                     69: ThotBool IsTemplateDocument(Document doc)
                     70: {
                     71: #ifdef TEMPLATES
1.171     kia        72:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                     73:   if(t)
                     74:     return (t->state&templTemplate)!=0;
                     75:   else
                     76:     return FALSE;
                     77: #else /* TEMPLATES */
                     78:   return FALSE;
                     79: #endif /* TEMPLATES */ 
                     80: }
                     81: 
                     82: 
                     83: /*----------------------------------------------------------------------
                     84:   Test if a document is an internal template.
                     85:   (no instance is opened and it is not edited)
                     86:   ----------------------------------------------------------------------*/
                     87: ThotBool IsInternalTemplateDocument(Document doc)
                     88: {
                     89: #ifdef TEMPLATES
                     90:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                     91:   if(t)
                     92:     return (t->state&templInternal)!=0;
                     93:   else
                     94:     return FALSE;
                     95: #else /* TEMPLATES */
1.109     kia        96:   return FALSE;
1.171     kia        97: #endif /* TEMPLATES */ 
                     98: }
                     99: 
                    100: /*----------------------------------------------------------------------
                    101:   Return the URL of an instance template.
                    102:   ----------------------------------------------------------------------*/
                    103: char* GetDocumentInstanceTemplateUrl(Document doc)
                    104: {
                    105: #ifdef TEMPLATES
                    106:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                    107:   if(t)
                    108:     return t->base_uri;
                    109:   else
                    110:     return FALSE;
                    111: #else /* TEMPLATES */
                    112:   return NULL;
                    113: #endif /* TEMPLATES */ 
1.109     kia       114: }
                    115: 
1.146     vatton    116: /*----------------------------------------------------------------------
                    117:   CheckPromptIndicator checks if the element is a prompt text unit
                    118:   ----------------------------------------------------------------------*/
                    119: ThotBool CheckPromptIndicator (Element el, Document doc)
                    120: {
                    121: #ifdef TEMPLATES
                    122:   ElementType     elType;
                    123:   Element         parent;
                    124:   AttributeType   attrType;
                    125:   Attribute       att;
                    126:   SSchema         templateSSchema;
                    127: 
1.173     vatton    128:   if (!IsTemplateInstanceDocument(doc))
                    129:     /* let Thot perform normal operation */
                    130:     return FALSE;
1.146     vatton    131:   elType = TtaGetElementType (el);
                    132:   templateSSchema = TtaGetSSchema ("Template", doc);
                    133:        if (elType.ElTypeNum == HTML_EL_TEXT_UNIT)
                    134:     {
                    135:       parent = TtaGetParent (el);
                    136:       elType = TtaGetElementType (parent);
                    137:       while (parent && elType.ElSSchema != templateSSchema)
                    138:         {
                    139:           parent = TtaGetParent (parent);
                    140:           elType = TtaGetElementType (parent);
                    141:         }
                    142:       if (parent &&
                    143:           (elType.ElTypeNum == Template_EL_useEl ||
                    144:            elType.ElTypeNum == Template_EL_useSimple))
                    145:         {
                    146:           // there is a parent template use
                    147:           attrType.AttrSSchema = elType.ElSSchema;
                    148:           attrType.AttrTypeNum = Template_ATTR_prompt;
                    149:           att = TtaGetAttribute (parent, attrType);
                    150:           if (att)
1.151     quint     151:             {
                    152:               TtaSelectElement (doc, el);
                    153:               return TRUE;
                    154:             }
1.146     vatton    155:         }
                    156:     }
                    157: #endif /* TEMPLATES */
1.173     vatton    158:   /* let Thot perform normal operation */
1.146     vatton    159:   return FALSE;
                    160: }
                    161: 
                    162: /*----------------------------------------------------------------------
                    163:   RemovePromptIndicator removes the enclosing prompt indicator
                    164:   ----------------------------------------------------------------------*/
1.153     vatton    165: ThotBool RemovePromptIndicator (NotifyOnTarget *event)
1.146     vatton    166: {
                    167: #ifdef TEMPLATES
                    168:   ElementType     elType;
1.153     vatton    169:   Element         parent, el;
1.146     vatton    170:   AttributeType   attrType;
                    171:   Attribute       att;
1.153     vatton    172:   Document        doc;
1.146     vatton    173:   SSchema         templateSSchema;
                    174: 
1.153     vatton    175:   el = event->element;
                    176:   doc = event->document;
1.146     vatton    177:   elType = TtaGetElementType (el);
                    178:   templateSSchema = TtaGetSSchema ("Template", doc);
1.153     vatton    179:   parent = TtaGetParent (el);
                    180:   elType = TtaGetElementType (parent);
                    181:   while (parent && elType.ElSSchema != templateSSchema)
1.146     vatton    182:     {
1.153     vatton    183:       parent = TtaGetParent (parent);
1.146     vatton    184:       elType = TtaGetElementType (parent);
1.153     vatton    185:     }
                    186:   if (parent &&
                    187:       (elType.ElTypeNum == Template_EL_useEl ||
                    188:        elType.ElTypeNum == Template_EL_useSimple))
                    189:     {
                    190:       // there is a parent template use
                    191:       attrType.AttrSSchema = elType.ElSSchema;
                    192:       attrType.AttrTypeNum = Template_ATTR_prompt;
                    193:       att = TtaGetAttribute (parent, attrType);
                    194:       if (att)
1.146     vatton    195:         {
1.153     vatton    196:           TtaRegisterAttributeDelete (att, parent, doc);
                    197:           TtaRemoveAttribute (parent, att, doc);
1.146     vatton    198:         }
                    199:     }
                    200: #endif /* TEMPLATES */
1.153     vatton    201:   return FALSE;                /* let Thot perform normal operation */
1.146     vatton    202: }
                    203: 
1.109     kia       204: 
                    205: /*----------------------------------------------------------------------
1.108     vatton    206:   AllocTemplateRepositoryListElement: allocates an element for the list
                    207:   of template repositories.
1.83      kia       208:   path : path of the new element
                    209:   return : address of the new element
                    210:   ----------------------------------------------------------------------*/
                    211: void* AllocTemplateRepositoryListElement (const char* path, void* prevElement)
                    212: {
1.129     vatton    213:   Prop_Templates_Path *element;
                    214: 
                    215:   element  = (Prop_Templates_Path*)TtaGetMemory (sizeof(Prop_Templates_Path));
                    216:   memset (element, 0, sizeof(Prop_Templates_Path));
                    217:   strncpy (element->Path, path, MAX_LENGTH - 1);
1.83      kia       218:   if (prevElement)
1.129     vatton    219:     {
                    220:       element->NextPath = ((Prop_Templates_Path*)prevElement)->NextPath;
                    221:       ((Prop_Templates_Path*)prevElement)->NextPath = element;
                    222:     }
1.83      kia       223:   return element;
                    224: }
                    225: 
                    226: 
                    227: /*----------------------------------------------------------------------
                    228:   FreeTemplateRepositoryList: Free the list of template repositories.
                    229:   list : address of the list (address of the first element).
                    230:   ----------------------------------------------------------------------*/
                    231: void FreeTemplateRepositoryList (void* list)
                    232: {
1.131     vatton    233:   Prop_Templates_Path **l = (Prop_Templates_Path**) list;
                    234:   Prop_Templates_Path  *element = *l;
                    235: 
1.83      kia       236:   l = NULL;
                    237:   while (element)
                    238:   {
                    239:     Prop_Templates_Path* next = element->NextPath;
1.131     vatton    240:     TtaFreeMemory (element);
1.83      kia       241:     element = next;
                    242:   }
                    243: }
                    244: 
                    245: /*----------------------------------------------------------------------
                    246:   CopyTemplateRepositoryList: Copy a list of template repositories.
                    247:   src : address of the list (address of the first element).
                    248:   dst : address where copy the list
                    249:   ----------------------------------------------------------------------*/
1.91      vatton    250: static void CopyTemplateRepositoryList (const Prop_Templates_Path** src,
                    251:                                         Prop_Templates_Path** dst)
1.83      kia       252: {
                    253:   Prop_Templates_Path *element=NULL, *current=NULL;
                    254:   
1.131     vatton    255:   if (*src)
1.83      kia       256:   {
                    257:     *dst = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    258:     (*dst)->NextPath = NULL;
                    259:     strcpy((*dst)->Path, (*src)->Path);
                    260:     
                    261:     element = (*src)->NextPath;
                    262:     current = *dst;
                    263:   }
                    264: 
1.106     vatton    265:   while (element)
                    266:     {
1.83      kia       267:     current->NextPath = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    268:     current = current->NextPath; 
                    269:     current->NextPath = NULL;
                    270:     strcpy(current->Path, element->Path);
                    271:     element = element->NextPath;
1.106     vatton    272:     }
1.83      kia       273: }
                    274: 
                    275: /*----------------------------------------------------------------------
                    276:   LoadTemplateRepositoryList: Load the list of template repositories.
                    277:   list   : address of the list (address of the first element).
                    278:   return : the number of readed repository paths.
                    279:   ----------------------------------------------------------------------*/
                    280: static int LoadTemplateRepositoryList (Prop_Templates_Path** list)
                    281: {
                    282:   Prop_Templates_Path *element, *current = NULL;
                    283:   char *path, *homePath;
                    284:   unsigned char *c;
                    285:   int nb = 0;
                    286:   FILE *file;
                    287:   
1.131     vatton    288:   //clean up the curent list
                    289:   FreeTemplateRepositoryList (list);
                    290: 
                    291:   // open the file
1.83      kia       292:   path = (char *) TtaGetMemory (MAX_LENGTH);
1.86      vatton    293:   homePath       = TtaGetEnvString ("APP_HOME");
1.106     vatton    294:   sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83      kia       295:   file = TtaReadOpen ((char *)path);
1.84      kia       296:   if (!file)
                    297:   {
                    298:     /* The config file dont exist, create it. */
                    299:     file = TtaWriteOpen ((char *)path);
1.122     kia       300:     fprintf (file, "http://www.w3.org/Amaya/Templates/cv.xtd\n");
1.144     vatton    301:     fprintf (file, "http://www.w3.org/Amaya/Templates/slides.xtd\n");
                    302:     fprintf (file, "http://www.w3.org/Amaya/Templates/ACM-Proc-Article.xtd\n");
1.84      kia       303:     TtaWriteClose (file);
                    304:     /* Retry to open it.*/
                    305:     file = TtaReadOpen ((char *)path);
                    306:   }
                    307:   
1.83      kia       308:   if (file)
1.129     vatton    309:     {
1.131     vatton    310:       // read the file
1.129     vatton    311:       c = (unsigned char*)path;
                    312:       *c = EOS;
                    313:       while (TtaReadByte (file, c))
                    314:         {
                    315:           if (*c == 13 || *c == EOL)
                    316:             *c = EOS;
                    317:           if (*c == EOS && c != (unsigned char*)path )
                    318:             {
                    319:               element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    320:               element->NextPath = NULL;
                    321:               strcpy (element->Path, path);
1.148     kia       322: 
1.129     vatton    323:               if (*list == NULL)
                    324:                 *list = element; 
                    325:               else
                    326:                 current->NextPath = element;
                    327:               current = element;
                    328:               nb++;
1.83      kia       329: 
1.129     vatton    330:               c = (unsigned char*) path;
                    331:               *c = EOS;
                    332:             }
                    333:           else
                    334:             c++;
                    335:         }
                    336:       if (c != (unsigned char*)path && *path != EOS)
                    337:         {
                    338:           element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    339:           *(c+1) = EOS;
                    340:           strcpy (element->Path, path);
                    341:           element->NextPath = NULL;
1.148     kia       342: 
1.129     vatton    343:           if (*list == NULL)
                    344:             *list = element; 
                    345:           else
                    346:             current->NextPath = element;
                    347:           nb++;
                    348:         }
                    349:       TtaReadClose (file);
1.83      kia       350:     }
                    351:   TtaFreeMemory(path);
                    352:   return nb;
                    353: }
                    354: 
                    355: /*----------------------------------------------------------------------
                    356:   SaveTemplateRepositoryList: Save the list of template repositories.
                    357:   list   : address of the list (address of the first element).
                    358:   ----------------------------------------------------------------------*/
                    359: static void SaveTemplateRepositoryList (const Prop_Templates_Path** list)
                    360: {
                    361:   const Prop_Templates_Path *element;
                    362:   char *path, *homePath;
                    363:   unsigned char *c;
                    364:   FILE *file;
                    365: 
                    366:   path = (char *) TtaGetMemory (MAX_LENGTH);
                    367:   homePath       = TtaGetEnvString ("APP_HOME");
1.106     vatton    368:   sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83      kia       369: 
                    370:   file = TtaWriteOpen ((char *)path);
                    371:   c = (unsigned char*)path;
                    372:   *c = EOS;
                    373:   if (file)
                    374:   {
                    375:     element = *list;
                    376:     while (element)
                    377:     {
                    378:       fprintf(file, "%s\n", element->Path);
                    379:       element = element->NextPath;
                    380:     }
                    381:     TtaWriteClose (file);
                    382:   }
                    383: }
                    384: 
                    385: /*----------------------------------------------------------------------
                    386:   GetTemplateRepositoryList: Get the list of template repositories from template environment.
                    387:   list : address of the list (address of the first element).
                    388:   ----------------------------------------------------------------------*/
                    389: void GetTemplateRepositoryList (void* list)
                    390: {
                    391:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                    392:   CopyTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths, l);
                    393: }
                    394: 
                    395: /*----------------------------------------------------------------------
                    396:   SetTemplateRepositoryList: Set the list of template repositories environment.
                    397:   list : address of the list (address of the first element).
                    398:   ----------------------------------------------------------------------*/
                    399: void SetTemplateRepositoryList (const void* list)
                    400: {
                    401:   const Prop_Templates_Path** l = (const Prop_Templates_Path**) list;
                    402:   CopyTemplateRepositoryList((const Prop_Templates_Path**)l, &TemplateRepositoryPaths);
                    403:   SaveTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths);
                    404: }
                    405: 
                    406: /*-----------------------------------------------------------------------
                    407:    InitTemplates
                    408:    Initializes the annotation library
                    409:   -----------------------------------------------------------------------*/
                    410: void InitTemplates ()
                    411: {
                    412:   TtaSetEnvBoolean ("SHOW_TEMPLATES", TRUE, FALSE);
1.131     vatton    413:   LoadTemplateRepositoryList (&TemplateRepositoryPaths);
1.83      kia       414: }
                    415: 
                    416: 
1.1       cvs       417: /*----------------------------------------------------------------------
1.108     vatton    418:   Load a template and create the instance file - update images and 
                    419:   stylesheets related to the template.
                    420:   ----------------------------------------------------------------------*/
                    421: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
                    422: {
                    423: #ifdef TEMPLATES
1.170     vatton    424:   DocumentType docType;
                    425:   int          len, i;
                    426:   char        *s;
                    427:   char              suffix[6];
                    428:   ThotBool     dontReplace = DontReplaceOldDoc;
1.108     vatton    429: 
                    430:   if (!IsW3Path (docname) && TtaFileExist (docname))
                    431:     {
                    432:       s = (char *)TtaGetMemory (strlen (docname) +
                    433:                                 strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
                    434:       sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
                    435:       InitConfirm (0, 0, s);
                    436:       TtaFreeMemory (s);
                    437:       if (!UserAnswer)
                    438:         return;
                    439:     }
1.170     vatton    440:   docType = LoadTemplate (0, templatename);
                    441:   if (docType != docFree)
1.165     kia       442:     {
1.170     vatton    443:       /* check if the file suffix is conform to the document type */
                    444:       s = (char *)TtaGetMemory (strlen (docname) + 10);
                    445:       strcpy (s, docname);
                    446:       if (!IsXMLName (docname))
                    447:         {
                    448:           // by default no suffix is added
                    449:           suffix[0] = EOS;
                    450:           if (IsMathMLName (docname) && docType != docMath)
                    451:             strcpy (suffix, "mml");
                    452:           else if (IsSVGName (docname) && docType != docSVG)
                    453:             strcpy (suffix, "svg");
                    454:           else if (IsHTMLName (docname) && docType != docHTML)
                    455:             strcpy (suffix, "xml");
                    456:           if (suffix[0] != EOS)
                    457:             {
                    458:               // change or update the suffix
                    459:               len = strlen (s);
                    460:               for (i = len-1; i > 0 && s[i] != '.'; i--);
                    461:               if (s[i] != '.')
                    462:                 {
                    463:                   /* there is no suffix */
                    464:                   s[i++] = '.';
                    465:                   strcpy (&s[i], suffix);
                    466:                 }
                    467:               else
                    468:                 {
                    469:                   /* there is a suffix */
                    470:                   i++;
                    471:                   strcpy (&s[i], suffix);
                    472:                 }
                    473:             }
                    474:         }
                    475:       // now create the instance
1.165     kia       476:       DontReplaceOldDoc = dontReplace;
1.177     vatton    477:       CreateInstance (templatename, s, docname, docType, doc);
1.170     vatton    478:       TtaFreeMemory (s);
1.165     kia       479:     }
1.108     vatton    480: #endif /* TEMPLATES */
                    481: }
                    482: 
1.53      vatton    483: 
1.109     kia       484: 
1.52      vatton    485: /*----------------------------------------------------------------------
1.107     kia       486:   PreventReloadingTemplate
                    487:   Prevent reloading a template.
                    488:   You must call AllowReloadingTemplate when finish.
                    489:   Usefull for reload an instance without reloading the template.
                    490:   ----------------------------------------------------------------------*/
                    491: void PreventReloadingTemplate(char* template_url)
                    492: {
                    493: #ifdef TEMPLATES
1.178     kia       494:   Template_AddReference(GetXTigerTemplate (template_url));
1.107     kia       495: #endif /* TEMPLATES */
                    496: }
                    497: 
                    498: /*----------------------------------------------------------------------
                    499:   AllowReloadingTemplate
                    500:   Allow reloading a template.
                    501:   You must call it after each PreventReloadingTemplate call.
                    502:   ----------------------------------------------------------------------*/
                    503: void AllowReloadingTemplate(char* template_url)
                    504: {
                    505: #ifdef TEMPLATES
1.178     kia       506:   Template_RemoveReference(GetXTigerTemplate (template_url));
1.107     kia       507: #endif /* TEMPLATES */  
                    508: }
                    509: 
                    510: 
1.137     vatton    511: /*----------------------------------------------------------------------
                    512:   ----------------------------------------------------------------------*/
1.134     kia       513: ThotBool isEOSorWhiteSpace (const char c)
                    514: {
1.137     vatton    515:   return c == SPACE || c == TAB || c ==  EOL || c ==__CR__ || c == EOS;
                    516: }
                    517: ThotBool isWhiteSpace (const char c)
                    518: {
                    519:   return c == SPACE || c == TAB || c == EOL || c ==__CR__;
1.134     kia       520: }
                    521: 
1.107     kia       522: /*----------------------------------------------------------------------
1.87      kia       523:   giveItems : Lists type items from string
                    524:   example : "one two three" is extracted to {one, two, three}
                    525:   note : item type are setted to SimpleTypeNat
                    526:   text : text from which list items
                    527:   size : size of text in characters
                    528:   items : address of exctracted item list
                    529:   nbitems : items number in items list
1.52      vatton    530:   ----------------------------------------------------------------------*/
1.76      vatton    531: void giveItems (char *text, int size, struct menuType **items, int *nbitems)
1.1       cvs       532: {
1.70      quint     533: #ifdef TEMPLATES
1.148     kia       534:   ThotBool         inElement = TRUE;
1.52      vatton    535:   struct menuType *menu;
                    536:   char            *iter;
1.148     kia       537:   char             temp[128];
1.52      vatton    538:   int              i;
1.148     kia       539:   int              labelSize;
1.28      tollenae  540: 
1.148     kia       541:   *nbitems = 1;
                    542:   for (i = 0; i < size; i++)
1.52      vatton    543:     {
                    544:       if (isEOSorWhiteSpace (text[i]))
                    545:         {
                    546:           if (inElement)
                    547:             inElement = FALSE;
                    548:         }
                    549:       else if (!inElement)
                    550:         {
                    551:           inElement = TRUE;
                    552:           (*nbitems)++;
                    553:         }
                    554:     }
1.51      francesc  555: 
1.148     kia       556:   menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
                    557:   iter = text;
                    558:   for (i = 0; i < *nbitems; i++)
                    559:     {
1.52      vatton    560:       labelSize = 0;
1.137     vatton    561:       while (isWhiteSpace (*iter))
1.52      vatton    562:         iter++;
1.137     vatton    563:       if (*iter != EOS)
                    564:         {
                    565:           while (!isEOSorWhiteSpace (*iter))
                    566:             {
                    567:               temp[labelSize++] = *iter;
                    568:               iter++;
                    569:             }
1.52      vatton    570: 
1.137     vatton    571:           temp[labelSize] = EOS;
                    572:           menu[i].label = (char *) TtaStrdup (temp);
                    573:           menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
1.52      vatton    574:         }
                    575:     }
1.137     vatton    576:   *items = menu;
1.70      quint     577: #endif /* TEMPLATES */
1.28      tollenae  578: }
1.37      tollenae  579: 
1.70      quint     580: #ifdef TEMPLATES
1.52      vatton    581: /*----------------------------------------------------------------------
                    582:   ----------------------------------------------------------------------*/
                    583: static char *createMenuString (const struct menuType* items, const int nbItems)
                    584: {
                    585:   char *result, *iter;
1.148     kia       586:   int   size = 0;
1.52      vatton    587:   int   i;
                    588: 
1.148     kia       589:   for (i=0; i < nbItems; i++)
                    590:     size += 2 + strlen (items[i].label);
1.52      vatton    591: 
1.148     kia       592:   result = (char *) TtaGetMemory (size);
                    593:   iter = result;
                    594:   for (i=0; i < nbItems; i++)
1.52      vatton    595:     {
                    596:       *iter = 'B';
                    597:       ++iter;
1.148     kia       598: 
1.52      vatton    599:       strcpy (iter, items[i].label);
                    600:       iter += strlen (items[i].label)+1;
                    601:     }
1.148     kia       602:   return result;
1.36      tollenae  603: }
1.71      quint     604: #endif /* TEMPLATES */
1.29      tollenae  605: 
1.71      quint     606: /*----------------------------------------------------------------------
                    607:   UseToBeCreated
                    608:   An new use element will be created by the user through some generic editing
                    609:   command
                    610:   -----------------------------------------------------------------------*/
                    611: ThotBool UseToBeCreated (NotifyElement *event)
                    612: {
                    613: #ifdef TEMPLATES
1.122     kia       614:   ElementType   parentType;
1.138     vatton    615:   SSchema       templateSSchema = TtaGetSSchema ("Template", event->document);  
1.130     vatton    616:   if (templateSSchema)
1.122     kia       617:   {
1.130     vatton    618:     parentType = TtaGetElementType (event->element);
                    619:     if (parentType.ElSSchema == templateSSchema &&
                    620:         parentType.ElTypeNum == Template_EL_repeat)
                    621:       return !Template_CanInsertRepeatChild (event->element);
                    622:     return TemplateElementWillBeCreated (event);
1.122     kia       623:   }
1.52      vatton    624: #endif /* TEMPLATES */
1.71      quint     625:   return FALSE; /* let Thot perform normal operation */
                    626: }
                    627: 
                    628: /*----------------------------------------------------------------------
                    629:   UseCreated
                    630:   A new "use" element has just been created by the user with a generic editing
                    631:   command.
                    632:   -----------------------------------------------------------------------*/
                    633: void UseCreated (NotifyElement *event)
                    634: {
                    635: #ifdef TEMPLATES
1.148     kia       636:   Document        doc = event->document;
                    637:   Element         el = event->element;
1.117     kia       638:   Element         parent;
                    639:   Element         first;
                    640:   ElementType     parentType;
                    641:   XTigerTemplate  t;
                    642:   SSchema         templateSSchema;
1.130     vatton    643:   char*           types, *text = NULL;
1.148     kia       644: 
1.112     vatton    645:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       646:     return;
                    647: 
1.72      quint     648:   if (TtaGetFirstChild (el))
                    649:     /* this Use element has already some content. It has already been
                    650:        instanciated */
                    651:     return;
1.117     kia       652: 
1.171     kia       653:   t = GetXTigerDocTemplate (doc);
1.71      quint     654:   if (!t)
                    655:     return; // no template ?!?!
1.101     kia       656: 
1.138     vatton    657:   templateSSchema = TtaGetSSchema ("Template", doc);
1.117     kia       658:   parent = TtaGetParent(el);
                    659:   parentType = TtaGetElementType(parent);
1.148     kia       660: 
1.130     vatton    661:   if (parentType.ElSSchema == templateSSchema &&
                    662:       parentType.ElTypeNum == Template_EL_repeat)
1.117     kia       663:   {
1.130     vatton    664:     first = TtaGetFirstChild (parent);
                    665:     if (first)
1.117     kia       666:     {
1.130     vatton    667:       types = GetAttributeStringValueFromNum (first, Template_ATTR_types, NULL);
                    668:       if (types)
1.117     kia       669:       {
1.130     vatton    670:         SetAttributeStringValueWithUndo (el, Template_ATTR_types, types);
                    671:         text = GetAttributeStringValueFromNum (el, Template_ATTR_title, NULL);
                    672:         SetAttributeStringValueWithUndo (first, Template_ATTR_title, text);
                    673:         TtaFreeMemory (text);
                    674:         text = GetAttributeStringValueFromNum (el, Template_ATTR_currentType, NULL);
                    675:         SetAttributeStringValueWithUndo (first, Template_ATTR_currentType, text);
                    676:         TtaFreeMemory (text);
                    677:         TtaFreeMemory (types);
1.117     kia       678:       }
                    679:     }
                    680:   }
                    681: 
1.76      vatton    682:   InstantiateUse (t, el, doc, TRUE);
1.71      quint     683: #endif /* TEMPLATES */
                    684: }
1.29      tollenae  685: 
1.89      kia       686: 
1.98      kia       687: /*----------------------------------------------------------------------
                    688:   Template_CanInsertRepeatChild
                    689:   Test if a xt:repeat child can be inserted (number between params min and max).
                    690:   @param el element (xt:repeat) to test
                    691:   @return True if an element can be inserted.
                    692:   ----------------------------------------------------------------------*/
                    693: ThotBool Template_CanInsertRepeatChild(Element el)
                    694: {
                    695: #ifdef TEMPLATES
1.154     vatton    696:   char     *max;
                    697:   int       maxVal, curVal;
                    698:   Element   child;
1.148     kia       699: 
1.104     kia       700:   max = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
1.105     vatton    701:   if (max)
1.104     kia       702:     {
1.154     vatton    703:       if (!strcmp(max, "*"))
                    704:         {
                    705:           TtaFreeMemory(max);
                    706:           return TRUE;
                    707:         }
                    708:       maxVal = atoi (max);
                    709:       TtaFreeMemory (max);
1.104     kia       710:       curVal = 0;
1.105     vatton    711:       for (child = TtaGetFirstChild(el); child; TtaNextSibling(&child))
1.104     kia       712:         curVal++;
1.154     vatton    713:       return (curVal < maxVal);
1.104     kia       714:     }
                    715:   else
1.98      kia       716:     return TRUE;
                    717: #endif /* TEMPLATES */
                    718:   return FALSE;
                    719: }
1.96      kia       720: 
1.89      kia       721: 
1.92      kia       722: #ifdef TEMPLATES
1.99      kia       723: /*----------------------------------------------------------------------
                    724:   QueryStringFromMenu
                    725:   Show a context menu to query a choice.
                    726:   @param items space-separated choice list string.
                    727:   @return The choosed item string or NULL if none.
                    728:   ----------------------------------------------------------------------*/
1.138     vatton    729: static char* QueryStringFromMenu (Document doc, char* items)
1.90      kia       730: {
                    731:   int nbitems, size;
                    732:   struct menuType *itemlist;
                    733:   char *menuString;
                    734:   char *result = NULL;
1.148     kia       735: 
1.138     vatton    736:   if (!TtaGetDocumentAccessMode (doc))
1.112     vatton    737:     return NULL;
                    738:   if (items == NULL)
1.110     kia       739:     return NULL;
1.138     vatton    740:   size = strlen (items);
                    741:   if (size == 0)
                    742:     return NULL;
1.90      kia       743:   giveItems (items, size, &itemlist, &nbitems);
                    744:   menuString = createMenuString (itemlist, nbitems);
                    745:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    746:                      nbitems, menuString , NULL, false, 'L');
                    747:   TtaFreeMemory (menuString);
                    748:   ReturnOption = -1;
                    749:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    750:   TtaWaitShowProcDialogue ();
                    751:   TtaDestroyDialogue (BaseDialog + OptionMenu);
1.148     kia       752: 
1.112     vatton    753:   if (ReturnOption!=-1)
1.90      kia       754:   {
                    755:     result = TtaStrdup(itemlist[ReturnOption].label);
                    756:   }
                    757:   
                    758:   TtaFreeMemory (itemlist);
                    759:   return result;
                    760: }
1.92      kia       761: #endif /* TEMPLATES */
1.90      kia       762: 
1.158     kia       763: #ifdef AMAYA_DEBUG
                    764: void FillInsertableElemList (Document doc, Element elem, DLList list);
                    765: #endif /* AMAYA_DEBUG */
1.162     vatton    766: /*----------------------------------------------------------------------
                    767:   ----------------------------------------------------------------------*/
                    768: char *Template_GetListTypes (XTigerTemplate t, Element el)
                    769: {
                    770: #ifdef TEMPLATES
                    771:   char  *listtypes = NULL, *types;
                    772: 
                    773:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
                    774:   if (types)
                    775:     listtypes = Template_ExpandTypes (t, types, el, FALSE);
                    776:   return listtypes;
                    777: #endif /* TEMPLATES */
                    778: }
1.158     kia       779: 
1.56      francesc  780: /*----------------------------------------------------------------------
1.138     vatton    781:   BagButtonClicked
                    782:   Called when a bag button is clicked.
                    783:   Can be called for useEl, useSimple or bag.
                    784:   If called for useEl or useSimple, the new element must be added after.
                    785:   If called for bag, the element must be added before all.
                    786:    
                    787:   Shows a menu with all the types that can be used in the bag.
                    788:   ----------------------------------------------------------------------*/
                    789: ThotBool BagButtonClicked (NotifyElement *event)
                    790: {
                    791: #ifdef TEMPLATES
                    792:   Document        doc = event->document;
                    793:   Element         el = event->element;
                    794:   ElementType     elType;
                    795:   XTigerTemplate  t;
                    796:   Declaration     decl;
                    797:   Element         bagEl = el;
                    798:   Element         firstEl;
                    799:   Element         newEl = NULL;
                    800:   View            view;
                    801:   SSchema         templateSSchema;
1.162     vatton    802:   char           *listtypes = NULL;
                    803:   char           *result = NULL;
1.138     vatton    804:   ThotBool        oldStructureChecking;
                    805:   DisplayMode     dispMode;
                    806: 
1.182   ! vatton    807:   if (!TtaGetDocumentAccessMode (doc))
1.138     vatton    808:     return TRUE;
1.182   ! vatton    809:   if (!IsTemplateInstanceDocument (doc))
        !           810:     return FALSE; /* let Thot perform normal operation */
1.148     kia       811: 
1.138     vatton    812:   TtaGetActiveView (&doc, &view);
                    813:   if (view != 1)
                    814:     return FALSE; /* let Thot perform normal operation */
                    815: 
                    816:   TtaCancelSelection (doc);
                    817:   templateSSchema = TtaGetSSchema ("Template", doc);  
1.171     kia       818:   t = GetXTigerDocTemplate(doc);
1.138     vatton    819:   elType = TtaGetElementType (el);
                    820:   while (bagEl &&
                    821:          (elType.ElSSchema != templateSSchema ||
                    822:           elType.ElTypeNum != Template_EL_bag))
                    823:     {
                    824:       bagEl = TtaGetParent (bagEl);
                    825:       elType = TtaGetElementType (bagEl);
                    826:     }
                    827: 
                    828:   if (bagEl)
                    829:   {
1.162     vatton    830:     listtypes = Template_GetListTypes (t, bagEl);
                    831:     if (listtypes)
1.138     vatton    832:       {
1.158     kia       833: #ifdef AMAYA_DEBUG
                    834:       printf("BagButtonClicked : \n  > %s\n", listtypes);
                    835: //      {
                    836: //        DLList list = DLList_Create();
                    837: //        FillInsertableElemList (doc, TtaGetFirstChild(bagEl), list);
                    838: //        DLListNode node;
                    839: //        ForwardIterator iter = DLList_GetForwardIterator(list);
                    840: //        ITERATOR_FOREACH(iter, DLListNode, node)
                    841: //        {
                    842: //          ElemListElement elem = (ElemListElement)node->elem;
                    843: //          printf("  + %s\n", ElemListElement_GetName(elem));
                    844: //        }
                    845: //        DLList_Destroy(list);
                    846: //      }
                    847: #endif /* AMAYA_DEBUG */
1.138     vatton    848:         result = QueryStringFromMenu (doc, listtypes);
                    849:         TtaFreeMemory (listtypes);
                    850:         if (result)
                    851:           {
                    852:             decl = Template_GetDeclaration (t, result);
                    853:             if (decl)
                    854:               {
                    855:                 dispMode = TtaGetDisplayMode (doc);
                    856:                 if (dispMode == DisplayImmediately)
                    857:                   /* don't set NoComputedDisplay
                    858:                      -> it breaks down views formatting when Enter generates new elements  */
1.139     vatton    859:                   TtaSetDisplayMode (doc, DeferredDisplay);
1.138     vatton    860: 
                    861:                 /* Prepare insertion.*/          
                    862:                 oldStructureChecking = TtaGetStructureChecking (doc);
                    863:                 TtaSetStructureChecking (FALSE, doc);
                    864:                 TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    865: 
                    866:                 /* Insert */
                    867:                 if (el == bagEl)
                    868:                   {
                    869:                     el = TtaGetFirstChild (el);
                    870:                     TtaSelectElement (doc, el);
                    871:                     TtaInsertAnyElement (doc, TRUE);
                    872:                   }
                    873:                 else
                    874:                   {
                    875:                     TtaSelectElement (doc, el);
                    876:                     TtaInsertAnyElement (doc, FALSE);
                    877:                   }
1.155     kia       878:                 newEl = Template_InsertBagChild (doc, bagEl, decl, FALSE);
1.138     vatton    879: 
                    880:                 /* Finish insertion.*/
                    881:                 TtaCloseUndoSequence (doc);
                    882:                 TtaSetDocumentModified (doc);
                    883:                 TtaSetStructureChecking (oldStructureChecking, doc);
                    884:                 // restore the display
                    885:                 TtaSetDisplayMode (doc, dispMode);
                    886:                 firstEl = GetFirstEditableElement (newEl);
                    887:                 if (firstEl)
                    888:                   {
                    889:                     TtaSelectElement (doc, firstEl);
                    890:                     TtaSetStatusSelectedElement (doc, view, firstEl);
                    891:                   }
                    892:                 else
                    893:                   {
                    894:                     TtaSelectElement (doc, newEl);
                    895:                     TtaSetStatusSelectedElement (doc, view, newEl);
                    896:                   }
                    897:               }
                    898:           }
                    899:       }
                    900:     TtaFreeMemory (result);
                    901:   }
                    902: #endif /* TEMPLATES */
                    903:   return TRUE; /* don't let Thot perform normal operation */
                    904: }
                    905: 
                    906: /*----------------------------------------------------------------------
1.79      quint     907:   RepeatButtonClicked
1.89      kia       908:   Called when a repeat button is clicked.
                    909:   Can be called for useEl, useSimple or repeat.
                    910:   If called for useEl or useSimple, the new element must be added after.
                    911:   If called for repeat, the element must be added before all.
                    912:    
1.56      francesc  913:   Shows a menu with all the types that can be used in a use element.
                    914:   ----------------------------------------------------------------------*/
1.79      quint     915: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  916: {
                    917: #ifdef TEMPLATES
1.89      kia       918:   Document        doc = event->document;
                    919:   Element         el = event->element;
                    920:   ElementType     elType;
1.90      kia       921:   XTigerTemplate  t;
                    922:   Declaration     decl;
                    923:   Element         repeatEl = el;
                    924:   Element         firstEl;
                    925:   Element         newEl = NULL;
1.95      kia       926:   View            view;
1.162     vatton    927:   char           *listtypes = NULL;
                    928:   char           *result = NULL;
1.138     vatton    929:   ThotBool        oldStructureChecking;
                    930:   DisplayMode     dispMode;
1.104     kia       931: 
1.112     vatton    932:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       933:     return TRUE;
1.182   ! vatton    934:   if (!IsTemplateInstanceDocument (doc))
        !           935:     return FALSE; /* let Thot perform normal operation */
1.148     kia       936: 
1.95      kia       937:   TtaGetActiveView (&doc, &view);
                    938:   if (view != 1)
                    939:     return FALSE; /* let Thot perform normal operation */
                    940: 
1.89      kia       941:   TtaCancelSelection(doc);
1.171     kia       942:   t = GetXTigerDocTemplate(doc);
1.89      kia       943:   elType = TtaGetElementType(el);
1.138     vatton    944:   while (elType.ElTypeNum != Template_EL_repeat)
1.179     kia       945:     {
                    946:       repeatEl = TtaGetParent(repeatEl);
                    947:       if (repeatEl == NULL)
                    948:         break;
                    949:       elType = TtaGetElementType(repeatEl);
                    950:     }
1.112     vatton    951:   if (repeatEl)
1.90      kia       952:     {
1.179     kia       953:       if (Template_CanInsertRepeatChild (repeatEl))
                    954:         {
                    955:           firstEl = TtaGetFirstChild (repeatEl);
                    956:           listtypes = Template_GetListTypes (t, firstEl);
                    957:           if (listtypes)
                    958:             {
1.158     kia       959: #ifdef AMAYA_DEBUG
1.179     kia       960:               printf("RepeatButtonClicked : \n  > %s\n", listtypes);
1.158     kia       961: #endif /* AMAYA_DEBUG */
1.179     kia       962: 
                    963:               result = QueryStringFromMenu (doc, listtypes);
                    964:               TtaFreeMemory (listtypes);
                    965:               if (result)
                    966:                 {
                    967:                   decl = Template_GetDeclaration (t, result);
                    968:                   if (decl)
                    969:                     {
                    970:                       dispMode = TtaGetDisplayMode (doc);
                    971:                       if (dispMode == DisplayImmediately)
                    972:                         /* don't set NoComputedDisplay
1.138     vatton    973:                  -> it breaks down views formatting when Enter generates new elements  */
1.179     kia       974:                         TtaSetDisplayMode (doc, DeferredDisplay);
                    975: 
                    976:                       /* Prepare insertion.*/          
                    977:                       oldStructureChecking = TtaGetStructureChecking (doc);
                    978:                       TtaSetStructureChecking (FALSE, doc);
                    979:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    980:                       /* Insert. */
                    981:                       if (el == repeatEl)
                    982:                         newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, NULL);
                    983:                       else
                    984:                         newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, el);
1.138     vatton    985: 
1.179     kia       986:                       /* Finish insertion.*/
                    987:                       TtaCloseUndoSequence(doc);
                    988:                       TtaSetDocumentModified (doc);
                    989:                       TtaSetStructureChecking (oldStructureChecking, doc);
                    990: 
                    991:                       // restore the display
                    992:                       TtaSetDisplayMode (doc, dispMode);
                    993:                       firstEl = GetFirstEditableElement (newEl);
                    994:                       if (firstEl)
                    995:                         {
                    996:                           TtaSelectElement (doc, firstEl);
                    997:                           TtaSetStatusSelectedElement (doc, view, firstEl);
                    998:                         }
                    999:                       else
                   1000:                         {
                   1001:                           TtaSelectElement (doc, newEl);
                   1002:                           TtaSetStatusSelectedElement (doc, view, newEl);
                   1003:                         }
                   1004:                     }
                   1005:                 }
1.104     kia      1006:             }
1.179     kia      1007:           TtaFreeMemory(result);
                   1008:           DumpSubtree(repeatEl, doc, 0);
                   1009: 
                   1010:         }
                   1011:       else /* if (Template_CanInsertRepeatChild(repeatEl)) */
                   1012:         {
                   1013:           TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.98      kia      1014:         }
                   1015:     }
1.77      vatton   1016:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc 1017: #endif /* TEMPLATES */
1.94      kia      1018:   return TRUE;
                   1019: }
                   1020: 
                   1021: /*----------------------------------------------------------------------
                   1022:   UseButtonClicked
                   1023:   Shows a menu with all the types that can be used in a use element.
                   1024:   ----------------------------------------------------------------------*/
                   1025: ThotBool UseButtonClicked (NotifyElement *event)
                   1026: {
                   1027: #ifdef TEMPLATES
                   1028:   Document        doc = event->document;
                   1029:   Element         el = event->element;
1.99      kia      1030:   Element         child;
1.94      kia      1031:   ElementType     elType;
                   1032:   XTigerTemplate  t;
                   1033:   Declaration     decl;
                   1034:   Element         firstEl;
                   1035:   Element         newEl = NULL;
                   1036:   char*           types;
                   1037:   ThotBool        oldStructureChecking;
1.95      kia      1038:   View            view;
1.133     vatton   1039:   char*           listtypes = NULL;
                   1040:   char*           result = NULL;
1.95      kia      1041: 
1.182   ! vatton   1042:   if (!TtaGetDocumentAccessMode (doc))
1.110     kia      1043:     return TRUE;
1.182   ! vatton   1044:   if (!IsTemplateInstanceDocument (doc))
        !          1045:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1046:     
1.95      kia      1047:   TtaGetActiveView (&doc, &view);
                   1048:   if (view != 1)
                   1049:     return FALSE; /* let Thot perform normal operation */
1.148     kia      1050: 
1.94      kia      1051:   TtaCancelSelection(doc);
1.171     kia      1052:   t = GetXTigerDocTemplate(doc);
1.94      kia      1053:   if (!t)
                   1054:     return FALSE; /* let Thot perform normal operation */
                   1055:   elType = TtaGetElementType(el);
                   1056: 
                   1057:   firstEl = TtaGetFirstChild(el);
1.112     vatton   1058:   if (firstEl)
1.94      kia      1059:   {
                   1060:     RepeatButtonClicked(event);
                   1061:   }
                   1062:   else
                   1063:   {
1.104     kia      1064:     types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.112     vatton   1065:     if (types)
1.94      kia      1066:     {
1.142     kia      1067:       listtypes = Template_ExpandTypes(t, types, NULL, FALSE);
1.158     kia      1068: #ifdef AMAYA_DEBUG
                   1069:       printf("UseButtonClicked : \n  > %s\n", listtypes);
                   1070: #endif /* AMAYA_DEBUG */
                   1071:       
1.104     kia      1072:       result = QueryStringFromMenu(doc, listtypes);
1.112     vatton   1073:       if (result)
1.94      kia      1074:       {
1.104     kia      1075:         decl = Template_GetDeclaration(t, result);
1.112     vatton   1076:         if (decl)
1.99      kia      1077:         {
1.104     kia      1078:           /* Prepare insertion.*/
                   1079:           oldStructureChecking = TtaGetStructureChecking (doc);
                   1080:           TtaSetStructureChecking (FALSE, doc);
1.138     vatton   1081:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104     kia      1082:           
                   1083:           /* Insert */
                   1084:           newEl = Template_InsertUseChildren(doc, el, decl);
                   1085:           for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
                   1086:           {
                   1087:             TtaRegisterElementCreate(child, doc);
                   1088:           }
                   1089:           
                   1090:           TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
                   1091:           TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
                   1092:           
1.117     kia      1093:           /* xt:currentType attribute.*/
                   1094:           SetAttributeStringValueWithUndo(el, Template_ATTR_currentType, result);
                   1095:           
1.104     kia      1096:           /* Finish insertion. */
                   1097:           TtaCloseUndoSequence(doc);
1.114     vatton   1098:           TtaSetDocumentModified (doc);
1.104     kia      1099:           TtaSetStructureChecking (oldStructureChecking, doc);
                   1100:           
                   1101:           firstEl = GetFirstEditableElement(newEl);
1.112     vatton   1102:           if (firstEl)
1.104     kia      1103:           {
                   1104:             TtaSelectElement (doc, firstEl);
                   1105:             TtaSetStatusSelectedElement(doc, view, firstEl);
                   1106:           }
                   1107:           else
                   1108:           {
                   1109:             TtaSelectElement (doc, newEl);
                   1110:             TtaSetStatusSelectedElement(doc, view, newEl);
                   1111:           }
1.98      kia      1112:         }
1.94      kia      1113:       }
                   1114:     }
1.104     kia      1115:     TtaFreeMemory(types);
1.94      kia      1116:     TtaFreeMemory(listtypes);
                   1117:     TtaFreeMemory(result);
                   1118:   }
1.148     kia      1119: 
1.94      kia      1120:   return TRUE;
                   1121: #endif /* TEMPLATES */
1.56      francesc 1122:        return TRUE;
                   1123: }
1.64      francesc 1124: 
1.89      kia      1125: 
1.103     kia      1126: /*----------------------------------------------------------------------
                   1127:   UseSimpleButtonClicked
                   1128:   ----------------------------------------------------------------------*/
                   1129: ThotBool UseSimpleButtonClicked (NotifyElement *event)
                   1130: {
                   1131: #ifdef TEMPLATES
1.181     vatton   1132:   ElementType     elType, parentType;
                   1133:   AttributeType   attrType;
                   1134:   Attribute       att;
                   1135: 
1.182   ! vatton   1136:   if (!TtaGetDocumentAccessMode (event->document))
1.110     kia      1137:     return TRUE;
1.182   ! vatton   1138:   if (!IsTemplateInstanceDocument (event->document))
        !          1139:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1140: 
1.181     vatton   1141:   elType = TtaGetElementType (event->element);
                   1142:   attrType.AttrSSchema = elType.ElSSchema;
                   1143:   attrType.AttrTypeNum = Template_ATTR_option;
                   1144:   att = TtaGetAttribute (event->element, attrType);
                   1145:   if (att)
                   1146:     return OptionButtonClicked (event);
                   1147:   
                   1148:   parentType = TtaGetElementType (TtaGetParent( event->element));
1.112     vatton   1149:   if (parentType.ElTypeNum == Template_EL_repeat)
1.138     vatton   1150:     return RepeatButtonClicked (event);
                   1151:   else if (parentType.ElTypeNum == Template_EL_bag)
                   1152:     return BagButtonClicked (event);
1.103     kia      1153: #endif /* TEMPLATES */  
                   1154:   return FALSE;
                   1155: }
1.94      kia      1156: 
                   1157: /*----------------------------------------------------------------------
                   1158:   OptionButtonClicked
                   1159:   ----------------------------------------------------------------------*/
                   1160: ThotBool OptionButtonClicked (NotifyElement *event)
                   1161: {
                   1162: #ifdef TEMPLATES
1.145     kia      1163:   Element         useEl, contentEl, next;
1.182   ! vatton   1164:   ElementType     useType;
1.94      kia      1165:   Document        doc;
                   1166:   XTigerTemplate  t;
                   1167:   View            view;
                   1168: 
1.182   ! vatton   1169:   if (!TtaGetDocumentAccessMode (event->document))
1.110     kia      1170:     return TRUE;
1.182   ! vatton   1171:   if (!IsTemplateInstanceDocument (event->document))
        !          1172:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1173: 
1.94      kia      1174:   TtaGetActiveView (&doc, &view);
                   1175:   if (view != 1)
                   1176:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1177: 
1.94      kia      1178:   doc = event->document;
1.182   ! vatton   1179:   useEl = event->element;
1.145     kia      1180:   if (!useEl)
1.94      kia      1181:     return FALSE; /* let Thot perform normal operation */
1.145     kia      1182:   useType = TtaGetElementType (useEl);
1.182   ! vatton   1183:   if (useType.ElTypeNum != Template_EL_useEl &&
        !          1184:       useType.ElTypeNum != Template_EL_useSimple)
1.94      kia      1185:     return FALSE;
                   1186: 
1.145     kia      1187:   TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
1.94      kia      1188:   TtaCancelSelection (doc);
1.145     kia      1189:   contentEl = TtaGetFirstChild (useEl);
                   1190:   if (!contentEl)
1.94      kia      1191:     /* the "use" element is empty. Instantiate it */
                   1192:     {
1.171     kia      1193:       t = GetXTigerDocTemplate (doc);
1.94      kia      1194:       if (!t)
                   1195:         return FALSE; // no template ?!?!
1.145     kia      1196:       InstantiateUse (t, useEl, doc, TRUE);
1.94      kia      1197:     }
                   1198:   else
                   1199:     /* remove the content of the "use" element */
                   1200:     {
                   1201:       do
                   1202:         {
1.145     kia      1203:           next = contentEl;
1.94      kia      1204:           TtaNextSibling (&next);
1.145     kia      1205:           TtaRegisterElementDelete(contentEl, doc);
                   1206:           TtaDeleteTree (contentEl, doc);
                   1207:           contentEl = next;
1.94      kia      1208:         }
                   1209:       while (next);
1.145     kia      1210:       if (NeedAMenu (useEl, doc))
                   1211:         {
                   1212:           TtaChangeTypeOfElement (useEl, doc, Template_EL_useEl);
                   1213:           TtaRegisterElementTypeChange(useEl, Template_EL_useSimple, doc);
                   1214:         }
1.94      kia      1215:     }
                   1216:   TtaSelectElement (doc, event->element);
1.145     kia      1217:   TtaCloseUndoSequence(doc);
1.94      kia      1218:   return TRUE; /* don't let Thot perform normal operation */
                   1219: #endif /* TEMPLATES */
                   1220:   return TRUE;
                   1221: }
                   1222: 
1.111     vatton   1223: /*----------------------------------------------------------------------
                   1224:   CheckTemplate checks if the template of the instance is loaded
1.126     vatton   1225:   Return TRUE if the template is loaded
1.111     vatton   1226:   ----------------------------------------------------------------------*/
                   1227: void CheckTemplate (Document doc)
                   1228: {
                   1229: #ifdef TEMPLATES
1.144     vatton   1230:   Element    root;
1.165     kia      1231:   
1.171     kia      1232:   if(IsTemplateInstanceDocument(doc))
1.111     vatton   1233:     {
1.144     vatton   1234:       XTigerTemplate   t;
                   1235: 
                   1236:       root = TtaGetRootElement (doc);
                   1237:       TtaSetAccessRight (root, ReadOnly, doc);
1.171     kia      1238:       t = GetXTigerDocTemplate (doc);
1.144     vatton   1239:       if (t == NULL)
                   1240:         {
                   1241:           // the template cannot be loaded
                   1242:           InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                   1243:           TtaSetDocumentAccessMode (doc, 0); // document readonly
                   1244:         }
                   1245:       else
                   1246:         {
                   1247:           // fix all access rights in the instance
1.171     kia      1248:           Template_PrepareTemplate(t);
1.166     kia      1249:           Template_FixAccessRight (t, root, doc);
1.144     vatton   1250:           TtaUpdateAccessRightInViews (doc, root);
                   1251:         }
1.111     vatton   1252:     }
                   1253: #endif /* TEMPLATES */
                   1254: }
1.94      kia      1255: 
1.66      vatton   1256: /*----------------------------------------------------------------------
1.108     vatton   1257:   OpeningInstance checks if it is a template instance needs.
                   1258:   If it's an instance and the template is not loaded, load it into a
                   1259:   temporary file
1.66      vatton   1260:   ----------------------------------------------------------------------*/
1.171     kia      1261: void OpeningInstance (char *localFileName, Document doc, char* docURL)
1.65      francesc 1262: {
                   1263: #ifdef TEMPLATES
1.76      vatton   1264:   XTigerTemplate   t;
1.103     kia      1265:   char            *content, *ptr, *begin;
1.76      vatton   1266:   gzFile           stream;
                   1267:   char             buffer[2000];
1.77      vatton   1268:   int              res;
1.171     kia      1269:   char            *template_version = NULL,
                   1270:                   *template_url = NULL;
1.65      francesc 1271: 
1.171     kia      1272:   stream = TtaGZOpen (localFileName);
1.76      vatton   1273:   if (stream != 0)
1.65      francesc 1274:     {
1.76      vatton   1275:       res = gzread (stream, buffer, 1999);
                   1276:       if (res >= 0)
1.65      francesc 1277:         {
1.81      vatton   1278:           buffer[res] = EOS;
1.103     kia      1279:           begin = strstr (buffer, "<?xtiger");
                   1280:           
1.112     vatton   1281:           if (begin)
1.103     kia      1282:           {
                   1283:             // Search for template version
                   1284:             ptr = strstr (begin, "templateVersion");
                   1285:             if (ptr)
                   1286:               ptr = strstr (ptr, "=");
                   1287:             if (ptr)
                   1288:               ptr = strstr (ptr, "\"");
                   1289:             if (ptr)
                   1290:               {
                   1291:                 // template URI
                   1292:                 content = &ptr[1];
                   1293:                 ptr = strstr (content, "\"");
                   1294:               }
                   1295:             if (ptr)
                   1296:               {
                   1297:                 *ptr = EOS;
                   1298:                 //Get now the template URI
1.171     kia      1299:                 template_version = TtaStrdup (content);
1.103     kia      1300:                 *ptr = '"';
                   1301:               }
                   1302:            
                   1303:             // Search for template uri
                   1304:             ptr = strstr (begin, "template");
                   1305:             if (ptr && ptr[8] != 'V')
                   1306:               ptr = strstr (ptr, "=");
                   1307:             if (ptr)
                   1308:               ptr = strstr (ptr, "\"");
                   1309:             if (ptr)
                   1310:               {
                   1311:                 // template URI
                   1312:                 content = &ptr[1];
                   1313:                 ptr = strstr (content, "\"");
                   1314:               }
                   1315:             if (ptr)
                   1316:               {
                   1317:                 *ptr = EOS;
                   1318:                 //Get now the template URI
1.171     kia      1319:                 template_url = TtaStrdup (content);
                   1320: 
                   1321:                 t = GetXTigerTemplate (template_url);
1.103     kia      1322:                 if (!t)
                   1323:                   {
1.171     kia      1324:                     LoadTemplate (doc, template_url);
                   1325:                     t = GetXTigerTemplate(template_url);
1.103     kia      1326:                   }
1.171     kia      1327:                 Template_PrepareInstance(docURL, doc, template_version, template_url);
                   1328:                 template_version = NULL;
                   1329:                 template_url     = NULL;
                   1330:                 Template_AddReference (t);
1.103     kia      1331:                 *ptr = '"';
                   1332:               }
                   1333:           }
1.65      francesc 1334:         }
                   1335:     }
1.171     kia      1336:   TtaFreeMemory(template_version);
                   1337:   TtaFreeMemory(template_url);
1.76      vatton   1338:   TtaGZClose (stream);
1.65      francesc 1339: #endif /* TEMPLATES */
                   1340: }
                   1341: 
1.64      francesc 1342: /*----------------------------------------------------------------------
1.178     kia      1343:   ClosingTemplateDocument
                   1344:   Callback called before closing a document which uses templates.
1.64      francesc 1345:   ----------------------------------------------------------------------*/
1.178     kia      1346: ThotBool ClosingTemplateDocument(NotifyDialog* dialog)
1.64      francesc 1347: {
1.65      francesc 1348: #ifdef TEMPLATES
1.171     kia      1349:   XTigerTemplate t = GetXTigerDocTemplate(dialog->document);
                   1350:   if(t)
1.178     kia      1351:       Template_RemoveReference(t);
1.65      francesc 1352: #endif /* TEMPLATES */
                   1353:   return FALSE;
1.64      francesc 1354: }
1.87      kia      1355: 
                   1356: /*----------------------------------------------------------------------
1.120     kia      1357:   IsTemplateElement
1.138     vatton   1358:   Test if an element is a template element.
1.87      kia      1359:   ----------------------------------------------------------------------*/
1.140     vatton   1360: ThotBool IsTemplateElement (Element elem)
1.87      kia      1361: {
                   1362: #ifdef TEMPLATES
1.138     vatton   1363:   ElementType     elType;
                   1364: 
                   1365:   elType = TtaGetElementType(elem);
                   1366:   if (elType.ElSSchema)
1.164     kia      1367:     return (strcmp(TtaGetSSchemaName(elType.ElSSchema) , "Template") == 0);
1.138     vatton   1368: #endif /* TEMPLATES */
1.87      kia      1369:   return FALSE;
                   1370: }
                   1371: 
                   1372: 
                   1373: /*----------------------------------------------------------------------
                   1374:   GetFirstTemplateParentElement
1.138     vatton   1375:   Return the first element which has "Template" as schema name or null.
1.87      kia      1376:   ----------------------------------------------------------------------*/
                   1377: Element GetFirstTemplateParentElement(Element elem)
                   1378: {
                   1379: #ifdef TEMPLATES
1.138     vatton   1380:   ElementType     elType;
                   1381: 
                   1382:   elem = TtaGetParent (elem);
                   1383:   elType = TtaGetElementType(elem);
                   1384:   while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
                   1385: {
                   1386:     elem = TtaGetParent (elem);
                   1387:     elType = TtaGetElementType(elem);
1.87      kia      1388:   }
                   1389:   return elem;
                   1390: #else
                   1391:   return NULL;
                   1392: #endif /* TEMPLATES */
                   1393: }
1.101     kia      1394: 
1.103     kia      1395: 
1.101     kia      1396: /*----------------------------------------------------------------------
1.102     vatton   1397:   TemplateElementWillBeCreated
1.101     kia      1398:   Processed when an element will be created in a template context.
                   1399:   ----------------------------------------------------------------------*/
1.102     vatton   1400: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101     kia      1401: {
1.103     kia      1402: #ifdef TEMPLATES
                   1403:   ElementType elType = event->elementType;
                   1404:   Element     parent = event->element;
                   1405:   ElementType parentType = TtaGetElementType(parent);
1.113     kia      1406:   Element     ancestor;
                   1407:   ElementType ancestorType;
                   1408:   SSchema     templateSSchema;
                   1409:   char*       types;
                   1410:   ThotBool    b;
1.101     kia      1411: 
1.115     kia      1412:   if(event->info==1)
                   1413:     return FALSE;
                   1414: 
1.112     vatton   1415:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1416:     return TRUE;
                   1417: 
1.138     vatton   1418:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.102     vatton   1419:   if (templateSSchema == NULL)
                   1420:     return FALSE; // let Thot do the job
1.115     kia      1421: 
1.113     kia      1422:   // Fisrt, test if in a xt:bag or in a base-element xt:use
1.147     vatton   1423:   if(parentType.ElSSchema == templateSSchema)
1.113     kia      1424:     ancestor = parent;
                   1425:   else
1.147     vatton   1426:     ancestor = GetFirstTemplateParentElement (parent);
1.113     kia      1427: 
1.147     vatton   1428:   if (ancestor)
1.113     kia      1429:   {
                   1430:     ancestorType = TtaGetElementType(ancestor);
1.147     vatton   1431:     if (ancestorType.ElTypeNum == Template_EL_bag)
1.113     kia      1432:     {
1.147     vatton   1433:       // only check the use child
                   1434:       if (ancestor != parent)
                   1435:         return  FALSE; // let Thot do the job
                   1436:       if (elType.ElSSchema == templateSSchema &&
                   1437:           (elType.ElTypeNum == Template_EL_useSimple ||
                   1438:            elType.ElTypeNum == Template_EL_useEl))
1.116     kia      1439:         return FALSE;
1.147     vatton   1440:       return !Template_CanInsertElementInBagElement (event->document, elType, ancestor);      
1.113     kia      1441:     }
1.147     vatton   1442:     else if(ancestorType.ElTypeNum == Template_EL_useSimple ||
                   1443:             ancestorType.ElTypeNum == Template_EL_useEl)
1.113     kia      1444:     {
1.147     vatton   1445:       // only check the bag child @@@ will be check exclude/include later
1.172     kia      1446:       if (ancestor != parent)
                   1447:         return  FALSE; // let Thot do the job
1.113     kia      1448:       types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
1.128     kia      1449:       b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position);
                   1450:       TtaFreeMemory(types);
1.115     kia      1451:       return !b;
1.113     kia      1452:     }
                   1453:   }
1.115     kia      1454:   
1.147     vatton   1455:   if (elType.ElSSchema == templateSSchema && elType.ElTypeNum == Template_EL_TEXT_UNIT)
1.115     kia      1456:   {
                   1457:     return FALSE;
                   1458:   }
                   1459:   
1.113     kia      1460:   // Can not insert.
                   1461:   return TRUE;
1.101     kia      1462: #endif /* TEMPLATES*/
1.102     vatton   1463:   return FALSE;
1.101     kia      1464: }
                   1465: 
                   1466: /*----------------------------------------------------------------------
                   1467:   TemplateElementWillBeDeleted
                   1468:   Processed when an element will be deleted in a template context.
                   1469:   ----------------------------------------------------------------------*/
                   1470: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1471: {
1.107     kia      1472: #ifdef TEMPLATES
                   1473:   Document       doc = event->document;
                   1474:   Element        elem = event->element;
1.163     vatton   1475:   Element        xtElem, parent = NULL, sibling;
1.117     kia      1476:   ElementType    xtType, elType;
1.107     kia      1477:   char*          type;
                   1478:   Declaration    dec;
1.115     kia      1479:   SSchema        templateSSchema;
1.107     kia      1480:   XTigerTemplate t;
1.156     vatton   1481:   ThotBool       selparent = FALSE;
1.107     kia      1482: 
1.115     kia      1483:   if(event->info==1)
                   1484:     return FALSE;
                   1485: 
1.112     vatton   1486:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1487:     return TRUE;
                   1488: 
1.138     vatton   1489:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.107     kia      1490:   if (templateSSchema == NULL)
                   1491:     return FALSE; // let Thot do the job
1.122     kia      1492: 
1.107     kia      1493:   xtElem = GetFirstTemplateParentElement(elem);
1.112     vatton   1494:   if (xtElem)
1.107     kia      1495:   {
                   1496:     xtType = TtaGetElementType(xtElem);
1.117     kia      1497:     
1.171     kia      1498:     t = GetXTigerDocTemplate(doc);
1.109     kia      1499: 
1.112     vatton   1500:     if (xtType.ElTypeNum==Template_EL_bag)
1.117     kia      1501:     {
                   1502:       elType = TtaGetElementType(elem);
                   1503:       if(elType.ElSSchema==templateSSchema &&
                   1504:         (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
                   1505:       {
                   1506:         // Remove element manually.
                   1507:         TtaOpenUndoSequence(doc, elem, elem, 0, 0);
                   1508:         TtaRegisterElementDelete(elem, doc);
                   1509:         TtaDeleteTree(elem, doc);
                   1510:         TtaCloseUndoSequence(doc);
                   1511:         return TRUE;
                   1512:       }
                   1513:       else
                   1514:         return FALSE; // xt:bag always allow remove children.
                   1515:     }
1.112     vatton   1516:     else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107     kia      1517:     {
1.109     kia      1518:       parent = TtaGetParent(elem);
1.117     kia      1519:       if (xtElem!=parent)
                   1520:       {
                   1521:         type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
                   1522:         dec = Template_GetDeclaration(t, type);
                   1523:         TtaFreeMemory(type);
                   1524:         
                   1525:         if (dec && dec->nature == XmlElementNat)
                   1526:           return FALSE; // Can remove element only if in xt:use current type is base language element. 
                   1527:         else
                   1528:           return TRUE;
1.107     kia      1529:       }
1.109     kia      1530:     }
1.112     vatton   1531:     else if (xtType.ElTypeNum==Template_EL_repeat)
1.109     kia      1532:     {
1.156     vatton   1533:       sibling = TtaGetSuccessor (elem);
                   1534:       if (sibling == NULL)
                   1535:         {
                   1536:           // there is no next element
                   1537:           sibling = TtaGetPredecessor (elem);
                   1538:           if (sibling == NULL)
                   1539:             selparent = TRUE;
                   1540:         }
1.109     kia      1541:       TtaRegisterElementDelete(elem, doc);
                   1542:       TtaDeleteTree(elem, doc);
                   1543:       InstantiateRepeat(t, xtElem, doc, TRUE);
1.156     vatton   1544:       if (selparent)
                   1545:         // look for the new sibling
                   1546:         sibling = TtaGetFirstChild (parent);
                   1547:       if (sibling)
                   1548:         TtaSelectElement(doc, sibling);
1.151     quint    1549:       else
1.156     vatton   1550:         TtaSelectElement(doc, parent);
1.109     kia      1551:       return TRUE;
1.107     kia      1552:     }
                   1553:   }
1.109     kia      1554:   
                   1555:   //TODO Test if current element is use or repeat.
                   1556:   // Because if an element is delete and it is the unique child of its parent,
                   1557:   // the parent intends to destroy itself. 
                   1558:   
1.107     kia      1559:   return TRUE;
                   1560: #else /* TEMPLATES */
1.101     kia      1561:   return FALSE;
1.107     kia      1562: #endif /* TEMPLATES */
1.101     kia      1563: }
                   1564: 
1.109     kia      1565: /*----------------------------------------------------------------------
                   1566:   CurrentTypeWillBeExported
                   1567:   Check if the xt:currentType attribute can be exported
                   1568:   ----------------------------------------------------------------------*/
                   1569: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
                   1570: {
                   1571: #ifdef TEMPLATES
1.110     kia      1572: 
1.112     vatton   1573:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1574:     return TRUE;
                   1575: 
1.112     vatton   1576:   if (IsTemplateDocument(event->document))
1.109     kia      1577:     return TRUE;
                   1578: #endif /* TEMPLATES */
                   1579:   return FALSE;
                   1580: }
1.127     kia      1581: 
                   1582: /*----------------------------------------------------------------------
                   1583:   TemplateAttrInMenu
                   1584:   Called by Thot when building the Attributes menu for template elements.
                   1585:   ----------------------------------------------------------------------*/
                   1586: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
                   1587: {
                   1588: #ifdef TEMPLATES
                   1589:   // Prevent from showing attributes for template instance but not templates.
                   1590:   if(IsTemplateInstanceDocument(event->document))
                   1591:     return TRUE;
                   1592:   else
                   1593: #endif /* TEMPLATES */
                   1594:     return FALSE;
                   1595: }
1.160     kia      1596: 
1.167     kia      1597: /*----------------------------------------------------------------------
1.168     kia      1598:   CreateTemplateFromDocument
                   1599:   Create a template from the current document.
1.167     kia      1600:   ----------------------------------------------------------------------*/
                   1601: void CreateTemplateFromDocument(Document doc, View view)
                   1602: {
1.171     kia      1603: #ifdef TEMPLATES
1.168     kia      1604:   char buffer[MAX_LENGTH];
                   1605:   strcpy(buffer, DocumentURLs[doc]);
                   1606:   strcat(buffer, ".xtd");
                   1607:   DontReplaceOldDoc = TRUE;
                   1608:   CreateTemplate(doc, buffer);
1.171     kia      1609: #endif /* TEMPLATES */
1.167     kia      1610: }
                   1611: 
1.169     kia      1612: /*----------------------------------------------------------------------
                   1613:   UpdateTemplateMenus
                   1614:   ----------------------------------------------------------------------*/
                   1615: void UpdateTemplateMenus (Document doc)
                   1616: {
1.180     kia      1617:   if(IsTemplateInstanceDocument(doc))
                   1618:     {
                   1619:       // Instance document
                   1620:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1621:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
                   1622:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
                   1623:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1624:     }
                   1625:   else if (IsTemplateDocument(doc))
                   1626:     {
                   1627:       // Template document
                   1628:       TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1629:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateTextBox);
                   1630:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateFreeBox);      
                   1631:       TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeat);
                   1632:     }
1.169     kia      1633:   else
1.180     kia      1634:     {
                   1635:       //Standard document
                   1636:       TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
                   1637:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
                   1638:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
                   1639:       TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
                   1640:     }
1.169     kia      1641: }
1.171     kia      1642: 
                   1643: /*----------------------------------------------------------------------
                   1644:   UninstanciateTemplateDocument
                   1645:   An instance of a template is tranformed into a template-less docuemnt.
                   1646:   Remove link between XTigerTemplate structure and document.
                   1647:   ----------------------------------------------------------------------*/
                   1648: void UninstanciateTemplateDocument(Document doc)
                   1649: {
                   1650: #ifdef TEMPLATES
                   1651:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1652:   if(t)
                   1653:     Template_Close(t);
                   1654: #endif /* TEMPLATES */  
                   1655: }
                   1656: 
                   1657: 
                   1658: /*----------------------------------------------------------------------
                   1659:   Template_PrepareInstance
                   1660:   Allocate XTigerTemplate structure for instance and initialize template
                   1661:   url and template version.
                   1662:   ----------------------------------------------------------------------*/
                   1663: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
                   1664: {
                   1665: #ifdef TEMPLATES
                   1666:   XTigerTemplate t = GetXTigerTemplate(fileName);
                   1667:   if(!t)
                   1668:     t = NewXTigerTemplate(fileName);
                   1669:   t->state           = templInstance;
                   1670:   t->templateVersion = template_version;
                   1671:   t->base_uri        = template_url;
                   1672:   t->doc             = doc;
1.178     kia      1673:   t->ref             = 1;
1.171     kia      1674:   
                   1675: #endif /* TEMPLATES */
                   1676: }
                   1677: 
                   1678: 
                   1679: /*----------------------------------------------------------------------
                   1680:   SetDocumentAsXTigerTemplate
                   1681:   Set the document template structure as template.
                   1682:   ----------------------------------------------------------------------*/
                   1683: void SetDocumentAsXTigerTemplate(Document doc)
                   1684: {
                   1685: #ifdef TEMPLATES
                   1686:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1687:   if(t)
                   1688:     t->state |= templTemplate;
                   1689: #endif /* TEMPLATES */  
                   1690: }
                   1691: 
                   1692: /*----------------------------------------------------------------------
                   1693:   SetDocumentAsXTigerLibrary
                   1694:   Set the document template structure as template library.
                   1695:   ----------------------------------------------------------------------*/
                   1696: void SetDocumentAsXTigerLibrary(Document doc)
                   1697: {
                   1698: #ifdef TEMPLATES
                   1699:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1700:   if(t)
                   1701:     t->state |= templLibrary;
                   1702: #endif /* TEMPLATES */  
                   1703: }
1.174     kia      1704: 
                   1705: 
                   1706: /*----------------------------------------------------------------------
                   1707:   TemplateCreateTextBox
                   1708:   Create a xt:use types="string" box around the selection.
                   1709:   ----------------------------------------------------------------------*/
                   1710: void TemplateCreateTextBox(Document doc, View view)
                   1711: {
                   1712: #ifdef TEMPLATES
                   1713:   Element     selElem;
                   1714:   ElementType selType;
                   1715:   int         firstChar, lastChar;
                   1716:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1717:   ElementType useType;
                   1718:   Element     use;
                   1719:   char        buffer[128];
                   1720:   
                   1721:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
                   1722:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING_LABEL);
                   1723: 
                   1724:   if (!TtaGetDocumentAccessMode(doc))
                   1725:     return;
                   1726:   
                   1727:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1728:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1729:     {
                   1730:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1731:       if(selElem)
                   1732:         {
                   1733:           selType =  TtaGetElementType(selElem);
                   1734:           if(!TtaIsLeaf(selType))
                   1735:             {
                   1736:               selElem = TtaGetFirstLeaf(selElem);
                   1737:               selType = TtaGetElementType(selElem);
                   1738:               firstChar = lastChar = 0;
                   1739:             }
                   1740:           
                   1741:           if(selType.ElTypeNum==1)
                   1742:             {
                   1743:               QueryStringFromUser(label, title, buffer, 127);
                   1744:               useType.ElSSchema = sstempl;
                   1745:               useType.ElTypeNum = Template_EL_useSimple;
                   1746: 
                   1747:               if(firstChar==0)
                   1748:                 {
                   1749:                   use = TtaNewElement(doc, useType);
                   1750:                   if(use)
                   1751:                     {
                   1752:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1753:                       TtaInsertSibling(use, selElem, FALSE, doc);
                   1754:                       TtaRegisterElementCreate(use, doc);
                   1755:                       TtaRegisterElementDelete (selElem, doc);
                   1756:                       TtaRemoveTree(selElem, doc);
                   1757:                       TtaInsertFirstChild(&selElem, use, doc);
                   1758:                       TtaRegisterElementDelete (selElem, doc);
                   1759:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1760:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);
                   1761:                       TtaCloseUndoSequence(doc);
                   1762:                       TtaSelectElement(doc, use);
                   1763:                     }
                   1764:                 }
                   1765:               else
                   1766:                 {
                   1767:                   GenerateInlineElement(Template_EL_useSimple, sstempl, 0, "", TRUE);
                   1768:                   TtaGiveFirstSelectedElement(doc, &use, &firstChar, &lastChar);
                   1769:                   selType =  TtaGetElementType(use);
                   1770:                   if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_useSimple)
                   1771:                     {
                   1772:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1773:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);                      
                   1774:                     }
                   1775:                 }
                   1776:             }
                   1777:           
                   1778:         }
                   1779:     }
                   1780: #endif /* TEMPLATES */
                   1781: }
                   1782: 
                   1783: 
                   1784: /*----------------------------------------------------------------------
                   1785:   TemplateCreateFreeBox
                   1786:   Create a xt:bag types="string" box around the selection.
                   1787:   ----------------------------------------------------------------------*/
                   1788: void TemplateCreateFreeBox(Document doc, View view)
                   1789: {
                   1790: #ifdef TEMPLATES
                   1791:   Element     selElem, selElem2;
                   1792:   ElementType selType, selType2;
                   1793:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1794:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1795: 
                   1796:   ElementType bagType;
                   1797:   Element     bag;
                   1798:   char        buffer[128];
                   1799: 
                   1800:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
                   1801:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY_LABEL);
                   1802: 
                   1803:   if (!TtaGetDocumentAccessMode(doc))
                   1804:     return;
                   1805:   
                   1806:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1807:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1808:     {
                   1809:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1810:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   1811:       
                   1812:       if(selElem && selElem2)
                   1813:         {
                   1814:           selType =  TtaGetElementType(selElem);
                   1815:           selType2 =  TtaGetElementType(selElem2);
                   1816: 
                   1817: //          printf("Selection %d %p %d %d\n", selType.ElTypeNum, selElem, firstChar, lastChar);
                   1818: //          printf("          %d %p %d %d\n", selType2.ElTypeNum, selElem2, firstChar2, lastChar2);
                   1819:           
                   1820:           QueryStringFromUser(label, title, buffer, 127);
                   1821:           bagType.ElSSchema = sstempl;
                   1822:           bagType.ElTypeNum = Template_EL_bag;
                   1823:           
                   1824:           if(selElem==selElem2)
                   1825:             {
                   1826:               if(firstChar==0)
                   1827:                 {
                   1828:                   ThotBool        oldStructureChecking;
                   1829:                   DisplayMode     dispMode;
                   1830:                   dispMode = TtaGetDisplayMode (doc);
                   1831:                   if (dispMode == DisplayImmediately)
                   1832:                     TtaSetDisplayMode (doc, DeferredDisplay);
                   1833:                   oldStructureChecking = TtaGetStructureChecking (doc);
                   1834:                   TtaSetStructureChecking (FALSE, doc);
                   1835:                   
                   1836:                   // Only one element fully selected
                   1837:                   bag = TtaNewElement(doc, bagType);
                   1838:                   
                   1839:                   TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1840:                   TtaInsertSibling(bag, selElem, FALSE, doc);
                   1841:                   TtaRegisterElementCreate(bag, doc);
                   1842:                   TtaRegisterElementDelete (selElem, doc);
                   1843:                   TtaRemoveTree(selElem, doc);
                   1844:                   TtaInsertFirstChild(&selElem, bag, doc);
                   1845:                   TtaRegisterElementDelete (selElem, doc);
                   1846:                   SetAttributeStringValue(bag, Template_ATTR_types, "any");
                   1847:                   SetAttributeStringValue(bag, Template_ATTR_title, buffer);
                   1848:                   TtaCloseUndoSequence(doc);
                   1849:                   TtaSelectElement(doc, bag);
                   1850:                   
                   1851:                   TtaSetStructureChecking (oldStructureChecking, doc);
                   1852:                   TtaSetDisplayMode (doc, dispMode);
                   1853: 
                   1854:                 }
                   1855:             }
                   1856:         }
                   1857:     }
                   1858: #endif /* TEMPLATES */
                   1859: }
                   1860: 
1.180     kia      1861: /*----------------------------------------------------------------------
                   1862:   TemplateCreateRepeat
                   1863:   Create a xt:repeat around the selection.
                   1864:   ----------------------------------------------------------------------*/
                   1865: void TemplateCreateRepeat(Document doc, View view)
                   1866: {
                   1867: #ifdef TEMPLATES
                   1868:   Element     selElem, selElem2;
                   1869:   ElementType selType, selType2;
                   1870:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1871:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1872: 
                   1873:   ElementType repType, compType;
                   1874:   Element     rep, comp;
                   1875:   char        buffer[128];
                   1876: 
                   1877:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATCOMP);
                   1878:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATCOMP_LABEL);
                   1879: 
                   1880:   if (!TtaGetDocumentAccessMode(doc))
                   1881:     return;
                   1882:   
                   1883:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1884:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1885:     {
                   1886:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1887:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   1888:       
                   1889:       if(selElem && selElem2)
                   1890:         {
                   1891:           selType =  TtaGetElementType(selElem);
                   1892:           selType2 =  TtaGetElementType(selElem2);
                   1893: 
                   1894:           QueryStringFromUser(label, title, buffer, 127);
                   1895:           repType.ElSSchema = sstempl;
                   1896:           repType.ElTypeNum = Template_EL_repeat;
                   1897:           compType.ElSSchema = sstempl;
                   1898:           compType.ElTypeNum = Template_EL_component;
                   1899:           
                   1900:           
                   1901:           if(selElem==selElem2)
                   1902:             {
                   1903:               if(firstChar==0)
                   1904:                 {
                   1905:                   ThotBool        oldStructureChecking;
                   1906:                   DisplayMode     dispMode;
                   1907:                   dispMode = TtaGetDisplayMode (doc);
                   1908:                   if (dispMode == DisplayImmediately)
                   1909:                     TtaSetDisplayMode (doc, DeferredDisplay);
                   1910:                   oldStructureChecking = TtaGetStructureChecking (doc);
                   1911:                   TtaSetStructureChecking (FALSE, doc);
                   1912:                   
                   1913:                   // Only one element fully selected
                   1914:                   
                   1915:                   TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1916:                   
                   1917:                   rep  = TtaNewElement(doc, repType);
                   1918:                   TtaInsertSibling(rep, selElem, FALSE, doc);
                   1919:                   TtaRegisterElementCreate(rep, doc);
                   1920: 
                   1921:                   comp = TtaNewElement(doc, compType);
                   1922:                   TtaInsertFirstChild(&comp, rep, doc);
                   1923:                   TtaRegisterElementCreate(comp, doc);
                   1924:                   
                   1925:                   TtaRegisterElementDelete (selElem, doc);
                   1926:                   TtaRemoveTree(selElem, doc);
                   1927:                   TtaInsertFirstChild(&selElem, comp, doc);
                   1928:                   TtaRegisterElementDelete (selElem, doc);
                   1929:                   SetAttributeStringValue(comp, Template_ATTR_name, buffer);
                   1930:                   TtaCloseUndoSequence(doc);
                   1931:                   TtaSelectElement(doc, rep);
                   1932:                   
                   1933:                   TtaSetStructureChecking (oldStructureChecking, doc);
                   1934:                   TtaSetDisplayMode (doc, dispMode);
                   1935: 
                   1936:                 }
                   1937:             }
                   1938:         }
                   1939:     }
                   1940: #endif /* TEMPLATES */
                   1941: }

Webmaster