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

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: 
                    807:   if (!TtaGetDocumentAccessMode(doc))
                    808:     return TRUE;
1.148     kia       809: 
1.138     vatton    810:   TtaGetActiveView (&doc, &view);
                    811:   if (view != 1)
                    812:     return FALSE; /* let Thot perform normal operation */
                    813: 
                    814:   TtaCancelSelection (doc);
                    815:   templateSSchema = TtaGetSSchema ("Template", doc);  
1.171     kia       816:   t = GetXTigerDocTemplate(doc);
1.138     vatton    817:   elType = TtaGetElementType (el);
                    818:   while (bagEl &&
                    819:          (elType.ElSSchema != templateSSchema ||
                    820:           elType.ElTypeNum != Template_EL_bag))
                    821:     {
                    822:       bagEl = TtaGetParent (bagEl);
                    823:       elType = TtaGetElementType (bagEl);
                    824:     }
                    825: 
                    826:   if (bagEl)
                    827:   {
1.162     vatton    828:     listtypes = Template_GetListTypes (t, bagEl);
                    829:     if (listtypes)
1.138     vatton    830:       {
1.158     kia       831: #ifdef AMAYA_DEBUG
                    832:       printf("BagButtonClicked : \n  > %s\n", listtypes);
                    833: //      {
                    834: //        DLList list = DLList_Create();
                    835: //        FillInsertableElemList (doc, TtaGetFirstChild(bagEl), list);
                    836: //        DLListNode node;
                    837: //        ForwardIterator iter = DLList_GetForwardIterator(list);
                    838: //        ITERATOR_FOREACH(iter, DLListNode, node)
                    839: //        {
                    840: //          ElemListElement elem = (ElemListElement)node->elem;
                    841: //          printf("  + %s\n", ElemListElement_GetName(elem));
                    842: //        }
                    843: //        DLList_Destroy(list);
                    844: //      }
                    845: #endif /* AMAYA_DEBUG */
1.138     vatton    846:         result = QueryStringFromMenu (doc, listtypes);
                    847:         TtaFreeMemory (listtypes);
                    848:         if (result)
                    849:           {
                    850:             decl = Template_GetDeclaration (t, result);
                    851:             if (decl)
                    852:               {
                    853:                 dispMode = TtaGetDisplayMode (doc);
                    854:                 if (dispMode == DisplayImmediately)
                    855:                   /* don't set NoComputedDisplay
                    856:                      -> it breaks down views formatting when Enter generates new elements  */
1.139     vatton    857:                   TtaSetDisplayMode (doc, DeferredDisplay);
1.138     vatton    858: 
                    859:                 /* Prepare insertion.*/          
                    860:                 oldStructureChecking = TtaGetStructureChecking (doc);
                    861:                 TtaSetStructureChecking (FALSE, doc);
                    862:                 TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    863: 
                    864:                 /* Insert */
                    865:                 if (el == bagEl)
                    866:                   {
                    867:                     el = TtaGetFirstChild (el);
                    868:                     TtaSelectElement (doc, el);
                    869:                     TtaInsertAnyElement (doc, TRUE);
                    870:                   }
                    871:                 else
                    872:                   {
                    873:                     TtaSelectElement (doc, el);
                    874:                     TtaInsertAnyElement (doc, FALSE);
                    875:                   }
1.155     kia       876:                 newEl = Template_InsertBagChild (doc, bagEl, decl, FALSE);
1.138     vatton    877: 
                    878:                 /* Finish insertion.*/
                    879:                 TtaCloseUndoSequence (doc);
                    880:                 TtaSetDocumentModified (doc);
                    881:                 TtaSetStructureChecking (oldStructureChecking, doc);
                    882:                 // restore the display
                    883:                 TtaSetDisplayMode (doc, dispMode);
                    884:                 firstEl = GetFirstEditableElement (newEl);
                    885:                 if (firstEl)
                    886:                   {
                    887:                     TtaSelectElement (doc, firstEl);
                    888:                     TtaSetStatusSelectedElement (doc, view, firstEl);
                    889:                   }
                    890:                 else
                    891:                   {
                    892:                     TtaSelectElement (doc, newEl);
                    893:                     TtaSetStatusSelectedElement (doc, view, newEl);
                    894:                   }
                    895:               }
                    896:           }
                    897:       }
                    898:     TtaFreeMemory (result);
                    899:   }
                    900: #endif /* TEMPLATES */
                    901:   return TRUE; /* don't let Thot perform normal operation */
                    902: }
                    903: 
                    904: /*----------------------------------------------------------------------
1.79      quint     905:   RepeatButtonClicked
1.89      kia       906:   Called when a repeat button is clicked.
                    907:   Can be called for useEl, useSimple or repeat.
                    908:   If called for useEl or useSimple, the new element must be added after.
                    909:   If called for repeat, the element must be added before all.
                    910:    
1.56      francesc  911:   Shows a menu with all the types that can be used in a use element.
                    912:   ----------------------------------------------------------------------*/
1.79      quint     913: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  914: {
                    915: #ifdef TEMPLATES
1.89      kia       916:   Document        doc = event->document;
                    917:   Element         el = event->element;
                    918:   ElementType     elType;
1.90      kia       919:   XTigerTemplate  t;
                    920:   Declaration     decl;
                    921:   Element         repeatEl = el;
                    922:   Element         firstEl;
                    923:   Element         newEl = NULL;
1.95      kia       924:   View            view;
1.162     vatton    925:   char           *listtypes = NULL;
                    926:   char           *result = NULL;
1.138     vatton    927:   ThotBool        oldStructureChecking;
                    928:   DisplayMode     dispMode;
1.104     kia       929: 
1.112     vatton    930:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       931:     return TRUE;
1.148     kia       932: 
1.95      kia       933:   TtaGetActiveView (&doc, &view);
                    934:   if (view != 1)
                    935:     return FALSE; /* let Thot perform normal operation */
                    936: 
1.89      kia       937:   TtaCancelSelection(doc);
1.148     kia       938: 
1.171     kia       939:   t = GetXTigerDocTemplate(doc);
1.89      kia       940:   elType = TtaGetElementType(el);
1.138     vatton    941:   while (elType.ElTypeNum != Template_EL_repeat)
1.89      kia       942:   {
1.90      kia       943:     repeatEl = TtaGetParent(repeatEl);
1.138     vatton    944:     if (repeatEl == NULL)
1.89      kia       945:       break;
1.90      kia       946:     elType = TtaGetElementType(repeatEl);
1.89      kia       947:   }
1.112     vatton    948:   if (repeatEl)
1.89      kia       949:   {
1.138     vatton    950:     if (Template_CanInsertRepeatChild (repeatEl))
1.90      kia       951:     {
1.138     vatton    952:       firstEl = TtaGetFirstChild (repeatEl);
1.162     vatton    953:     listtypes = Template_GetListTypes (t, firstEl);
                    954:     if (listtypes)
1.90      kia       955:       {
1.158     kia       956: #ifdef AMAYA_DEBUG
                    957:       printf("RepeatButtonClicked : \n  > %s\n", listtypes);
                    958: #endif /* AMAYA_DEBUG */
                    959:         
1.138     vatton    960:         result = QueryStringFromMenu (doc, listtypes);
                    961:         TtaFreeMemory (listtypes);
1.112     vatton    962:         if (result)
1.98      kia       963:         {
1.138     vatton    964:           decl = Template_GetDeclaration (t, result);
1.112     vatton    965:           if (decl)
1.104     kia       966:           {
1.138     vatton    967:             dispMode = TtaGetDisplayMode (doc);
                    968:             if (dispMode == DisplayImmediately)
                    969:               /* don't set NoComputedDisplay
                    970:                  -> it breaks down views formatting when Enter generates new elements  */
1.139     vatton    971:               TtaSetDisplayMode (doc, DeferredDisplay);
1.138     vatton    972: 
1.104     kia       973:             /* Prepare insertion.*/          
                    974:             oldStructureChecking = TtaGetStructureChecking (doc);
                    975:             TtaSetStructureChecking (FALSE, doc);
1.138     vatton    976:             TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104     kia       977:             /* Insert. */
1.138     vatton    978:             if (el == repeatEl)
                    979:               newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, NULL);
1.104     kia       980:             else
1.138     vatton    981:               newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, el);
1.104     kia       982:               
                    983:             /* Finish insertion.*/
                    984:             TtaCloseUndoSequence(doc);
1.114     vatton    985:             TtaSetDocumentModified (doc);
1.104     kia       986:             TtaSetStructureChecking (oldStructureChecking, doc);
1.138     vatton    987: 
                    988:             // restore the display
                    989:             TtaSetDisplayMode (doc, dispMode);
                    990:             firstEl = GetFirstEditableElement (newEl);
1.112     vatton    991:             if (firstEl)
1.104     kia       992:             {
                    993:               TtaSelectElement (doc, firstEl);
1.138     vatton    994:               TtaSetStatusSelectedElement (doc, view, firstEl);
1.104     kia       995:             }
                    996:             else
                    997:             {
                    998:               TtaSelectElement (doc, newEl);
1.138     vatton    999:               TtaSetStatusSelectedElement (doc, view, newEl);
1.104     kia      1000:             }
1.98      kia      1001:           }
                   1002:         }
1.90      kia      1003:       }
1.98      kia      1004:       TtaFreeMemory(result);
                   1005:     }
1.112     vatton   1006:     else /* if (Template_CanInsertRepeatChild(repeatEl)) */
1.98      kia      1007:     {
                   1008:       TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.90      kia      1009:     }
1.89      kia      1010:   }
1.77      vatton   1011:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc 1012: #endif /* TEMPLATES */
1.94      kia      1013:   return TRUE;
                   1014: }
                   1015: 
                   1016: /*----------------------------------------------------------------------
                   1017:   UseButtonClicked
                   1018:   Shows a menu with all the types that can be used in a use element.
                   1019:   ----------------------------------------------------------------------*/
                   1020: ThotBool UseButtonClicked (NotifyElement *event)
                   1021: {
                   1022: #ifdef TEMPLATES
                   1023:   Document        doc = event->document;
                   1024:   Element         el = event->element;
1.99      kia      1025:   Element         child;
1.94      kia      1026:   ElementType     elType;
                   1027:   XTigerTemplate  t;
                   1028:   Declaration     decl;
                   1029:   Element         firstEl;
                   1030:   Element         newEl = NULL;
                   1031:   char*           types;
                   1032:   ThotBool        oldStructureChecking;
1.95      kia      1033:   View            view;
1.133     vatton   1034:   char*           listtypes = NULL;
                   1035:   char*           result = NULL;
1.95      kia      1036: 
1.112     vatton   1037:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia      1038:     return TRUE;
                   1039:     
1.95      kia      1040:   TtaGetActiveView (&doc, &view);
                   1041:   if (view != 1)
                   1042:     return FALSE; /* let Thot perform normal operation */
1.148     kia      1043: 
1.94      kia      1044:   TtaCancelSelection(doc);
                   1045:   
1.171     kia      1046:   t = GetXTigerDocTemplate(doc);
1.94      kia      1047:   if (!t)
                   1048:     return FALSE; /* let Thot perform normal operation */
                   1049:   elType = TtaGetElementType(el);
                   1050: 
                   1051:   firstEl = TtaGetFirstChild(el);
1.112     vatton   1052:   if (firstEl)
1.94      kia      1053:   {
                   1054:     RepeatButtonClicked(event);
                   1055:   }
                   1056:   else
                   1057:   {
1.104     kia      1058:     types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.112     vatton   1059:     if (types)
1.94      kia      1060:     {
1.142     kia      1061:       listtypes = Template_ExpandTypes(t, types, NULL, FALSE);
1.158     kia      1062: #ifdef AMAYA_DEBUG
                   1063:       printf("UseButtonClicked : \n  > %s\n", listtypes);
                   1064: #endif /* AMAYA_DEBUG */
                   1065:       
1.104     kia      1066:       result = QueryStringFromMenu(doc, listtypes);
1.112     vatton   1067:       if (result)
1.94      kia      1068:       {
1.104     kia      1069:         decl = Template_GetDeclaration(t, result);
1.112     vatton   1070:         if (decl)
1.99      kia      1071:         {
1.104     kia      1072:           /* Prepare insertion.*/
                   1073:           oldStructureChecking = TtaGetStructureChecking (doc);
                   1074:           TtaSetStructureChecking (FALSE, doc);
1.138     vatton   1075:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104     kia      1076:           
                   1077:           /* Insert */
                   1078:           newEl = Template_InsertUseChildren(doc, el, decl);
                   1079:           for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
                   1080:           {
                   1081:             TtaRegisterElementCreate(child, doc);
                   1082:           }
                   1083:           
                   1084:           TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
                   1085:           TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
                   1086:           
1.117     kia      1087:           /* xt:currentType attribute.*/
                   1088:           SetAttributeStringValueWithUndo(el, Template_ATTR_currentType, result);
                   1089:           
1.104     kia      1090:           /* Finish insertion. */
                   1091:           TtaCloseUndoSequence(doc);
1.114     vatton   1092:           TtaSetDocumentModified (doc);
1.104     kia      1093:           TtaSetStructureChecking (oldStructureChecking, doc);
                   1094:           
                   1095:           firstEl = GetFirstEditableElement(newEl);
1.112     vatton   1096:           if (firstEl)
1.104     kia      1097:           {
                   1098:             TtaSelectElement (doc, firstEl);
                   1099:             TtaSetStatusSelectedElement(doc, view, firstEl);
                   1100:           }
                   1101:           else
                   1102:           {
                   1103:             TtaSelectElement (doc, newEl);
                   1104:             TtaSetStatusSelectedElement(doc, view, newEl);
                   1105:           }
1.98      kia      1106:         }
1.94      kia      1107:       }
                   1108:     }
1.104     kia      1109:     TtaFreeMemory(types);
1.94      kia      1110:     TtaFreeMemory(listtypes);
                   1111:     TtaFreeMemory(result);
                   1112:   }
1.148     kia      1113: 
1.94      kia      1114:   return TRUE;
                   1115: #endif /* TEMPLATES */
1.56      francesc 1116:        return TRUE;
                   1117: }
1.64      francesc 1118: 
1.89      kia      1119: 
1.103     kia      1120: /*----------------------------------------------------------------------
                   1121:   UseSimpleButtonClicked
                   1122:   ----------------------------------------------------------------------*/
                   1123: ThotBool UseSimpleButtonClicked (NotifyElement *event)
                   1124: {
                   1125: #ifdef TEMPLATES
1.112     vatton   1126:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1127:     return TRUE;
                   1128: 
1.138     vatton   1129:   ElementType parentType = TtaGetElementType (TtaGetParent( event->element));
1.112     vatton   1130:   if (parentType.ElTypeNum == Template_EL_repeat)
1.138     vatton   1131:     return RepeatButtonClicked (event);
                   1132:   else if (parentType.ElTypeNum == Template_EL_bag)
                   1133:     return BagButtonClicked (event);
1.103     kia      1134: #endif /* TEMPLATES */  
                   1135:   return FALSE;
                   1136: }
1.94      kia      1137: 
                   1138: /*----------------------------------------------------------------------
                   1139:   OptionButtonClicked
                   1140:   ----------------------------------------------------------------------*/
                   1141: ThotBool OptionButtonClicked (NotifyElement *event)
                   1142: {
                   1143: #ifdef TEMPLATES
1.145     kia      1144:   Element         useEl, contentEl, next;
                   1145:   ElementType     useType, optType;
1.94      kia      1146:   Document        doc;
                   1147:   XTigerTemplate  t;
                   1148:   View            view;
                   1149: 
1.112     vatton   1150:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1151:     return TRUE;
                   1152: 
1.94      kia      1153:   TtaGetActiveView (&doc, &view);
                   1154:   if (view != 1)
                   1155:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1156: 
1.94      kia      1157:   doc = event->document;
1.145     kia      1158:   useEl = TtaGetFirstChild (event->element);
                   1159:   if (!useEl)
1.94      kia      1160:     return FALSE; /* let Thot perform normal operation */
1.145     kia      1161:   useType = TtaGetElementType (useEl);
                   1162:   optType = TtaGetElementType (event->element);
                   1163:   if ((useType.ElTypeNum != Template_EL_useEl &&
1.148     kia      1164:       useType.ElTypeNum != Template_EL_useSimple) ||
                   1165:       useType.ElSSchema != optType.ElSSchema)
1.94      kia      1166:     return FALSE;
                   1167: 
1.145     kia      1168:   TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
1.148     kia      1169: 
1.94      kia      1170:   TtaCancelSelection (doc);
1.148     kia      1171: 
1.145     kia      1172:   contentEl = TtaGetFirstChild (useEl);
                   1173:   if (!contentEl)
1.94      kia      1174:     /* the "use" element is empty. Instantiate it */
                   1175:     {
1.171     kia      1176:       t = GetXTigerDocTemplate (doc);
1.94      kia      1177:       if (!t)
                   1178:         return FALSE; // no template ?!?!
1.145     kia      1179:       InstantiateUse (t, useEl, doc, TRUE);
1.94      kia      1180:     }
                   1181:   else
                   1182:     /* remove the content of the "use" element */
                   1183:     {
                   1184:       do
                   1185:         {
1.145     kia      1186:           next = contentEl;
1.94      kia      1187:           TtaNextSibling (&next);
1.145     kia      1188:           TtaRegisterElementDelete(contentEl, doc);
                   1189:           TtaDeleteTree (contentEl, doc);
                   1190:           contentEl = next;
1.94      kia      1191:         }
                   1192:       while (next);
1.145     kia      1193:       if (NeedAMenu (useEl, doc))
                   1194:         {
                   1195:           TtaChangeTypeOfElement (useEl, doc, Template_EL_useEl);
                   1196:           TtaRegisterElementTypeChange(useEl, Template_EL_useSimple, doc);
                   1197:         }
1.94      kia      1198:     }
                   1199:   TtaSelectElement (doc, event->element);
1.145     kia      1200:   TtaCloseUndoSequence(doc);
1.94      kia      1201:   return TRUE; /* don't let Thot perform normal operation */
                   1202: #endif /* TEMPLATES */
                   1203:   return TRUE;
                   1204: }
                   1205: 
1.111     vatton   1206: /*----------------------------------------------------------------------
                   1207:   CheckTemplate checks if the template of the instance is loaded
1.126     vatton   1208:   Return TRUE if the template is loaded
1.111     vatton   1209:   ----------------------------------------------------------------------*/
                   1210: void CheckTemplate (Document doc)
                   1211: {
                   1212: #ifdef TEMPLATES
1.144     vatton   1213:   Element    root;
1.165     kia      1214:   
1.171     kia      1215:   if(IsTemplateInstanceDocument(doc))
1.111     vatton   1216:     {
1.144     vatton   1217:       XTigerTemplate   t;
                   1218: 
                   1219:       root = TtaGetRootElement (doc);
                   1220:       TtaSetAccessRight (root, ReadOnly, doc);
1.171     kia      1221:       t = GetXTigerDocTemplate (doc);
1.144     vatton   1222:       if (t == NULL)
                   1223:         {
                   1224:           // the template cannot be loaded
                   1225:           InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                   1226:           TtaSetDocumentAccessMode (doc, 0); // document readonly
                   1227:         }
                   1228:       else
                   1229:         {
                   1230:           // fix all access rights in the instance
1.171     kia      1231:           Template_PrepareTemplate(t);
1.166     kia      1232:           Template_FixAccessRight (t, root, doc);
1.144     vatton   1233:           TtaUpdateAccessRightInViews (doc, root);
                   1234:         }
1.111     vatton   1235:     }
                   1236: #endif /* TEMPLATES */
                   1237: }
1.94      kia      1238: 
1.66      vatton   1239: /*----------------------------------------------------------------------
1.108     vatton   1240:   OpeningInstance checks if it is a template instance needs.
                   1241:   If it's an instance and the template is not loaded, load it into a
                   1242:   temporary file
1.66      vatton   1243:   ----------------------------------------------------------------------*/
1.171     kia      1244: void OpeningInstance (char *localFileName, Document doc, char* docURL)
1.65      francesc 1245: {
                   1246: #ifdef TEMPLATES
1.76      vatton   1247:   XTigerTemplate   t;
1.103     kia      1248:   char            *content, *ptr, *begin;
1.76      vatton   1249:   gzFile           stream;
                   1250:   char             buffer[2000];
1.77      vatton   1251:   int              res;
1.171     kia      1252:   char            *template_version = NULL,
                   1253:                   *template_url = NULL;
1.65      francesc 1254: 
1.171     kia      1255:   stream = TtaGZOpen (localFileName);
1.76      vatton   1256:   if (stream != 0)
1.65      francesc 1257:     {
1.76      vatton   1258:       res = gzread (stream, buffer, 1999);
                   1259:       if (res >= 0)
1.65      francesc 1260:         {
1.81      vatton   1261:           buffer[res] = EOS;
1.103     kia      1262:           begin = strstr (buffer, "<?xtiger");
                   1263:           
1.112     vatton   1264:           if (begin)
1.103     kia      1265:           {
                   1266:             // Search for template version
                   1267:             ptr = strstr (begin, "templateVersion");
                   1268:             if (ptr)
                   1269:               ptr = strstr (ptr, "=");
                   1270:             if (ptr)
                   1271:               ptr = strstr (ptr, "\"");
                   1272:             if (ptr)
                   1273:               {
                   1274:                 // template URI
                   1275:                 content = &ptr[1];
                   1276:                 ptr = strstr (content, "\"");
                   1277:               }
                   1278:             if (ptr)
                   1279:               {
                   1280:                 *ptr = EOS;
                   1281:                 //Get now the template URI
1.171     kia      1282:                 template_version = TtaStrdup (content);
1.103     kia      1283:                 *ptr = '"';
                   1284:               }
                   1285:            
                   1286:             // Search for template uri
                   1287:             ptr = strstr (begin, "template");
                   1288:             if (ptr && ptr[8] != 'V')
                   1289:               ptr = strstr (ptr, "=");
                   1290:             if (ptr)
                   1291:               ptr = strstr (ptr, "\"");
                   1292:             if (ptr)
                   1293:               {
                   1294:                 // template URI
                   1295:                 content = &ptr[1];
                   1296:                 ptr = strstr (content, "\"");
                   1297:               }
                   1298:             if (ptr)
                   1299:               {
                   1300:                 *ptr = EOS;
                   1301:                 //Get now the template URI
1.171     kia      1302:                 template_url = TtaStrdup (content);
                   1303: 
                   1304:                 t = GetXTigerTemplate (template_url);
1.103     kia      1305:                 if (!t)
                   1306:                   {
1.171     kia      1307:                     LoadTemplate (doc, template_url);
                   1308:                     t = GetXTigerTemplate(template_url);
1.103     kia      1309:                   }
1.171     kia      1310:                 Template_PrepareInstance(docURL, doc, template_version, template_url);
                   1311:                 template_version = NULL;
                   1312:                 template_url     = NULL;
                   1313:                 Template_AddReference (t);
1.103     kia      1314:                 *ptr = '"';
                   1315:               }
                   1316:           }
1.65      francesc 1317:         }
                   1318:     }
1.171     kia      1319:   TtaFreeMemory(template_version);
                   1320:   TtaFreeMemory(template_url);
1.76      vatton   1321:   TtaGZClose (stream);
1.65      francesc 1322: #endif /* TEMPLATES */
                   1323: }
                   1324: 
1.64      francesc 1325: /*----------------------------------------------------------------------
1.178   ! kia      1326:   ClosingTemplateDocument
        !          1327:   Callback called before closing a document which uses templates.
1.64      francesc 1328:   ----------------------------------------------------------------------*/
1.178   ! kia      1329: ThotBool ClosingTemplateDocument(NotifyDialog* dialog)
1.64      francesc 1330: {
1.65      francesc 1331: #ifdef TEMPLATES
1.171     kia      1332:   XTigerTemplate t = GetXTigerDocTemplate(dialog->document);
                   1333:   if(t)
1.178   ! kia      1334:       Template_RemoveReference(t);
1.65      francesc 1335: #endif /* TEMPLATES */
                   1336:   return FALSE;
1.64      francesc 1337: }
1.87      kia      1338: 
                   1339: /*----------------------------------------------------------------------
1.120     kia      1340:   IsTemplateElement
1.138     vatton   1341:   Test if an element is a template element.
1.87      kia      1342:   ----------------------------------------------------------------------*/
1.140     vatton   1343: ThotBool IsTemplateElement (Element elem)
1.87      kia      1344: {
                   1345: #ifdef TEMPLATES
1.138     vatton   1346:   ElementType     elType;
                   1347: 
                   1348:   elType = TtaGetElementType(elem);
                   1349:   if (elType.ElSSchema)
1.164     kia      1350:     return (strcmp(TtaGetSSchemaName(elType.ElSSchema) , "Template") == 0);
1.138     vatton   1351: #endif /* TEMPLATES */
1.87      kia      1352:   return FALSE;
                   1353: }
                   1354: 
                   1355: 
                   1356: /*----------------------------------------------------------------------
                   1357:   GetFirstTemplateParentElement
1.138     vatton   1358:   Return the first element which has "Template" as schema name or null.
1.87      kia      1359:   ----------------------------------------------------------------------*/
                   1360: Element GetFirstTemplateParentElement(Element elem)
                   1361: {
                   1362: #ifdef TEMPLATES
1.138     vatton   1363:   ElementType     elType;
                   1364: 
                   1365:   elem = TtaGetParent (elem);
                   1366:   elType = TtaGetElementType(elem);
                   1367:   while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
                   1368: {
                   1369:     elem = TtaGetParent (elem);
                   1370:     elType = TtaGetElementType(elem);
1.87      kia      1371:   }
                   1372:   return elem;
                   1373: #else
                   1374:   return NULL;
                   1375: #endif /* TEMPLATES */
                   1376: }
1.101     kia      1377: 
1.103     kia      1378: 
1.101     kia      1379: /*----------------------------------------------------------------------
1.102     vatton   1380:   TemplateElementWillBeCreated
1.101     kia      1381:   Processed when an element will be created in a template context.
                   1382:   ----------------------------------------------------------------------*/
1.102     vatton   1383: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101     kia      1384: {
1.103     kia      1385: #ifdef TEMPLATES
                   1386:   ElementType elType = event->elementType;
                   1387:   Element     parent = event->element;
                   1388:   ElementType parentType = TtaGetElementType(parent);
1.113     kia      1389:   Element     ancestor;
                   1390:   ElementType ancestorType;
                   1391:   SSchema     templateSSchema;
                   1392:   char*       types;
                   1393:   ThotBool    b;
1.101     kia      1394: 
1.115     kia      1395:   if(event->info==1)
                   1396:     return FALSE;
                   1397: 
1.112     vatton   1398:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1399:     return TRUE;
                   1400: 
1.138     vatton   1401:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.102     vatton   1402:   if (templateSSchema == NULL)
                   1403:     return FALSE; // let Thot do the job
1.115     kia      1404: 
1.113     kia      1405:   // Fisrt, test if in a xt:bag or in a base-element xt:use
1.147     vatton   1406:   if(parentType.ElSSchema == templateSSchema)
1.113     kia      1407:     ancestor = parent;
                   1408:   else
1.147     vatton   1409:     ancestor = GetFirstTemplateParentElement (parent);
1.113     kia      1410: 
1.147     vatton   1411:   if (ancestor)
1.113     kia      1412:   {
                   1413:     ancestorType = TtaGetElementType(ancestor);
1.147     vatton   1414:     if (ancestorType.ElTypeNum == Template_EL_bag)
1.113     kia      1415:     {
1.147     vatton   1416:       // only check the use child
                   1417:       if (ancestor != parent)
                   1418:         return  FALSE; // let Thot do the job
                   1419:       if (elType.ElSSchema == templateSSchema &&
                   1420:           (elType.ElTypeNum == Template_EL_useSimple ||
                   1421:            elType.ElTypeNum == Template_EL_useEl))
1.116     kia      1422:         return FALSE;
1.147     vatton   1423:       return !Template_CanInsertElementInBagElement (event->document, elType, ancestor);      
1.113     kia      1424:     }
1.147     vatton   1425:     else if(ancestorType.ElTypeNum == Template_EL_useSimple ||
                   1426:             ancestorType.ElTypeNum == Template_EL_useEl)
1.113     kia      1427:     {
1.147     vatton   1428:       // only check the bag child @@@ will be check exclude/include later
1.172     kia      1429:       if (ancestor != parent)
                   1430:         return  FALSE; // let Thot do the job
1.113     kia      1431:       types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
1.128     kia      1432:       b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position);
                   1433:       TtaFreeMemory(types);
1.115     kia      1434:       return !b;
1.113     kia      1435:     }
                   1436:   }
1.115     kia      1437:   
1.147     vatton   1438:   if (elType.ElSSchema == templateSSchema && elType.ElTypeNum == Template_EL_TEXT_UNIT)
1.115     kia      1439:   {
                   1440:     return FALSE;
                   1441:   }
                   1442:   
1.113     kia      1443:   // Can not insert.
                   1444:   return TRUE;
1.101     kia      1445: #endif /* TEMPLATES*/
1.102     vatton   1446:   return FALSE;
1.101     kia      1447: }
                   1448: 
                   1449: /*----------------------------------------------------------------------
                   1450:   TemplateElementWillBeDeleted
                   1451:   Processed when an element will be deleted in a template context.
                   1452:   ----------------------------------------------------------------------*/
                   1453: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1454: {
1.107     kia      1455: #ifdef TEMPLATES
                   1456:   Document       doc = event->document;
                   1457:   Element        elem = event->element;
1.163     vatton   1458:   Element        xtElem, parent = NULL, sibling;
1.117     kia      1459:   ElementType    xtType, elType;
1.107     kia      1460:   char*          type;
                   1461:   Declaration    dec;
1.115     kia      1462:   SSchema        templateSSchema;
1.107     kia      1463:   XTigerTemplate t;
1.156     vatton   1464:   ThotBool       selparent = FALSE;
1.107     kia      1465: 
1.115     kia      1466:   if(event->info==1)
                   1467:     return FALSE;
                   1468: 
1.112     vatton   1469:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1470:     return TRUE;
                   1471: 
1.138     vatton   1472:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.107     kia      1473:   if (templateSSchema == NULL)
                   1474:     return FALSE; // let Thot do the job
1.122     kia      1475: 
1.107     kia      1476:   xtElem = GetFirstTemplateParentElement(elem);
1.112     vatton   1477:   if (xtElem)
1.107     kia      1478:   {
                   1479:     xtType = TtaGetElementType(xtElem);
1.117     kia      1480:     
1.171     kia      1481:     t = GetXTigerDocTemplate(doc);
1.109     kia      1482: 
1.112     vatton   1483:     if (xtType.ElTypeNum==Template_EL_bag)
1.117     kia      1484:     {
                   1485:       elType = TtaGetElementType(elem);
                   1486:       if(elType.ElSSchema==templateSSchema &&
                   1487:         (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
                   1488:       {
                   1489:         // Remove element manually.
                   1490:         TtaOpenUndoSequence(doc, elem, elem, 0, 0);
                   1491:         TtaRegisterElementDelete(elem, doc);
                   1492:         TtaDeleteTree(elem, doc);
                   1493:         TtaCloseUndoSequence(doc);
                   1494:         return TRUE;
                   1495:       }
                   1496:       else
                   1497:         return FALSE; // xt:bag always allow remove children.
                   1498:     }
1.112     vatton   1499:     else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107     kia      1500:     {
1.109     kia      1501:       parent = TtaGetParent(elem);
1.117     kia      1502:       if (xtElem!=parent)
                   1503:       {
                   1504:         type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
                   1505:         dec = Template_GetDeclaration(t, type);
                   1506:         TtaFreeMemory(type);
                   1507:         
                   1508:         if (dec && dec->nature == XmlElementNat)
                   1509:           return FALSE; // Can remove element only if in xt:use current type is base language element. 
                   1510:         else
                   1511:           return TRUE;
1.107     kia      1512:       }
1.109     kia      1513:     }
1.112     vatton   1514:     else if (xtType.ElTypeNum==Template_EL_repeat)
1.109     kia      1515:     {
1.156     vatton   1516:       sibling = TtaGetSuccessor (elem);
                   1517:       if (sibling == NULL)
                   1518:         {
                   1519:           // there is no next element
                   1520:           sibling = TtaGetPredecessor (elem);
                   1521:           if (sibling == NULL)
                   1522:             selparent = TRUE;
                   1523:         }
1.109     kia      1524:       TtaRegisterElementDelete(elem, doc);
                   1525:       TtaDeleteTree(elem, doc);
                   1526:       InstantiateRepeat(t, xtElem, doc, TRUE);
1.156     vatton   1527:       if (selparent)
                   1528:         // look for the new sibling
                   1529:         sibling = TtaGetFirstChild (parent);
                   1530:       if (sibling)
                   1531:         TtaSelectElement(doc, sibling);
1.151     quint    1532:       else
1.156     vatton   1533:         TtaSelectElement(doc, parent);
1.109     kia      1534:       return TRUE;
1.107     kia      1535:     }
                   1536:   }
1.109     kia      1537:   
                   1538:   //TODO Test if current element is use or repeat.
                   1539:   // Because if an element is delete and it is the unique child of its parent,
                   1540:   // the parent intends to destroy itself. 
                   1541:   
1.107     kia      1542:   return TRUE;
                   1543: #else /* TEMPLATES */
1.101     kia      1544:   return FALSE;
1.107     kia      1545: #endif /* TEMPLATES */
1.101     kia      1546: }
                   1547: 
1.109     kia      1548: /*----------------------------------------------------------------------
                   1549:   CurrentTypeWillBeExported
                   1550:   Check if the xt:currentType attribute can be exported
                   1551:   ----------------------------------------------------------------------*/
                   1552: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
                   1553: {
                   1554: #ifdef TEMPLATES
1.110     kia      1555: 
1.112     vatton   1556:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1557:     return TRUE;
                   1558: 
1.112     vatton   1559:   if (IsTemplateDocument(event->document))
1.109     kia      1560:     return TRUE;
                   1561: #endif /* TEMPLATES */
                   1562:   return FALSE;
                   1563: }
1.127     kia      1564: 
                   1565: /*----------------------------------------------------------------------
                   1566:   TemplateAttrInMenu
                   1567:   Called by Thot when building the Attributes menu for template elements.
                   1568:   ----------------------------------------------------------------------*/
                   1569: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
                   1570: {
                   1571: #ifdef TEMPLATES
                   1572:   // Prevent from showing attributes for template instance but not templates.
                   1573:   if(IsTemplateInstanceDocument(event->document))
                   1574:     return TRUE;
                   1575:   else
                   1576: #endif /* TEMPLATES */
                   1577:     return FALSE;
                   1578: }
1.160     kia      1579: 
1.167     kia      1580: /*----------------------------------------------------------------------
1.168     kia      1581:   CreateTemplateFromDocument
                   1582:   Create a template from the current document.
1.167     kia      1583:   ----------------------------------------------------------------------*/
                   1584: void CreateTemplateFromDocument(Document doc, View view)
                   1585: {
1.171     kia      1586: #ifdef TEMPLATES
1.168     kia      1587:   char buffer[MAX_LENGTH];
                   1588:   strcpy(buffer, DocumentURLs[doc]);
                   1589:   strcat(buffer, ".xtd");
                   1590:   DontReplaceOldDoc = TRUE;
                   1591:   CreateTemplate(doc, buffer);
1.171     kia      1592: #endif /* TEMPLATES */
1.167     kia      1593: }
                   1594: 
1.169     kia      1595: /*----------------------------------------------------------------------
                   1596:   UpdateTemplateMenus
                   1597:   ----------------------------------------------------------------------*/
                   1598: void UpdateTemplateMenus (Document doc)
                   1599: {
                   1600:   if(IsTemplateInstanceDocument(doc) || 
                   1601:       IsTemplateDocument(doc))
                   1602:     TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1603:   else
                   1604:     TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
                   1605: }
1.171     kia      1606: 
                   1607: /*----------------------------------------------------------------------
                   1608:   UninstanciateTemplateDocument
                   1609:   An instance of a template is tranformed into a template-less docuemnt.
                   1610:   Remove link between XTigerTemplate structure and document.
                   1611:   ----------------------------------------------------------------------*/
                   1612: void UninstanciateTemplateDocument(Document doc)
                   1613: {
                   1614: #ifdef TEMPLATES
                   1615:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1616:   if(t)
                   1617:     Template_Close(t);
                   1618: #endif /* TEMPLATES */  
                   1619: }
                   1620: 
                   1621: 
                   1622: /*----------------------------------------------------------------------
                   1623:   Template_PrepareInstance
                   1624:   Allocate XTigerTemplate structure for instance and initialize template
                   1625:   url and template version.
                   1626:   ----------------------------------------------------------------------*/
                   1627: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
                   1628: {
                   1629: #ifdef TEMPLATES
                   1630:   XTigerTemplate t = GetXTigerTemplate(fileName);
                   1631:   if(!t)
                   1632:     t = NewXTigerTemplate(fileName);
                   1633:   t->state           = templInstance;
                   1634:   t->templateVersion = template_version;
                   1635:   t->base_uri        = template_url;
                   1636:   t->doc             = doc;
1.178   ! kia      1637:   t->ref             = 1;
1.171     kia      1638:   
                   1639: #endif /* TEMPLATES */
                   1640: }
                   1641: 
                   1642: 
                   1643: /*----------------------------------------------------------------------
                   1644:   SetDocumentAsXTigerTemplate
                   1645:   Set the document template structure as template.
                   1646:   ----------------------------------------------------------------------*/
                   1647: void SetDocumentAsXTigerTemplate(Document doc)
                   1648: {
                   1649: #ifdef TEMPLATES
                   1650:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1651:   if(t)
                   1652:     t->state |= templTemplate;
                   1653: #endif /* TEMPLATES */  
                   1654: }
                   1655: 
                   1656: /*----------------------------------------------------------------------
                   1657:   SetDocumentAsXTigerLibrary
                   1658:   Set the document template structure as template library.
                   1659:   ----------------------------------------------------------------------*/
                   1660: void SetDocumentAsXTigerLibrary(Document doc)
                   1661: {
                   1662: #ifdef TEMPLATES
                   1663:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1664:   if(t)
                   1665:     t->state |= templLibrary;
                   1666: #endif /* TEMPLATES */  
                   1667: }
1.174     kia      1668: 
                   1669: 
                   1670: /*----------------------------------------------------------------------
                   1671:   TemplateCreateTextBox
                   1672:   Create a xt:use types="string" box around the selection.
                   1673:   ----------------------------------------------------------------------*/
                   1674: void TemplateCreateTextBox(Document doc, View view)
                   1675: {
                   1676: #ifdef TEMPLATES
                   1677:   Element     selElem;
                   1678:   ElementType selType;
                   1679:   int         firstChar, lastChar;
                   1680:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1681:   ElementType useType;
                   1682:   Element     use;
                   1683:   char        buffer[128];
                   1684:   
                   1685:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
                   1686:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING_LABEL);
                   1687: 
                   1688:   if (!TtaGetDocumentAccessMode(doc))
                   1689:     return;
                   1690:   
                   1691:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1692:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1693:     {
                   1694:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1695:       if(selElem)
                   1696:         {
                   1697:           selType =  TtaGetElementType(selElem);
                   1698:           if(!TtaIsLeaf(selType))
                   1699:             {
                   1700:               selElem = TtaGetFirstLeaf(selElem);
                   1701:               selType = TtaGetElementType(selElem);
                   1702:               firstChar = lastChar = 0;
                   1703:             }
                   1704:           
                   1705:           if(selType.ElTypeNum==1)
                   1706:             {
                   1707:               QueryStringFromUser(label, title, buffer, 127);
                   1708:               useType.ElSSchema = sstempl;
                   1709:               useType.ElTypeNum = Template_EL_useSimple;
                   1710: 
                   1711:               if(firstChar==0)
                   1712:                 {
                   1713:                   use = TtaNewElement(doc, useType);
                   1714:                   if(use)
                   1715:                     {
                   1716:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1717:                       TtaInsertSibling(use, selElem, FALSE, doc);
                   1718:                       TtaRegisterElementCreate(use, doc);
                   1719:                       TtaRegisterElementDelete (selElem, doc);
                   1720:                       TtaRemoveTree(selElem, doc);
                   1721:                       TtaInsertFirstChild(&selElem, use, doc);
                   1722:                       TtaRegisterElementDelete (selElem, doc);
                   1723:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1724:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);
                   1725:                       TtaCloseUndoSequence(doc);
                   1726:                       TtaSelectElement(doc, use);
                   1727:                     }
                   1728:                 }
                   1729:               else
                   1730:                 {
                   1731:                   GenerateInlineElement(Template_EL_useSimple, sstempl, 0, "", TRUE);
                   1732:                   TtaGiveFirstSelectedElement(doc, &use, &firstChar, &lastChar);
                   1733:                   selType =  TtaGetElementType(use);
                   1734:                   if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_useSimple)
                   1735:                     {
                   1736:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1737:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);                      
                   1738:                     }
                   1739:                 }
                   1740:             }
                   1741:           
                   1742:         }
                   1743:     }
                   1744: #endif /* TEMPLATES */
                   1745: }
                   1746: 
                   1747: 
                   1748: /*----------------------------------------------------------------------
                   1749:   TemplateCreateFreeBox
                   1750:   Create a xt:bag types="string" box around the selection.
                   1751:   ----------------------------------------------------------------------*/
                   1752: void TemplateCreateFreeBox(Document doc, View view)
                   1753: {
                   1754: #ifdef TEMPLATES
                   1755:   Element     selElem, selElem2;
                   1756:   ElementType selType, selType2;
                   1757:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1758:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1759: 
                   1760:   ElementType bagType;
                   1761:   Element     bag;
                   1762:   char        buffer[128];
                   1763: 
                   1764:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
                   1765:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY_LABEL);
                   1766: 
                   1767:   if (!TtaGetDocumentAccessMode(doc))
                   1768:     return;
                   1769:   
                   1770:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1771:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1772:     {
                   1773:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1774:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   1775:       
                   1776:       if(selElem && selElem2)
                   1777:         {
                   1778:           selType =  TtaGetElementType(selElem);
                   1779:           selType2 =  TtaGetElementType(selElem2);
                   1780: 
                   1781: //          printf("Selection %d %p %d %d\n", selType.ElTypeNum, selElem, firstChar, lastChar);
                   1782: //          printf("          %d %p %d %d\n", selType2.ElTypeNum, selElem2, firstChar2, lastChar2);
                   1783:           
                   1784:           QueryStringFromUser(label, title, buffer, 127);
                   1785:           bagType.ElSSchema = sstempl;
                   1786:           bagType.ElTypeNum = Template_EL_bag;
                   1787:           
                   1788:           if(selElem==selElem2)
                   1789:             {
                   1790:               if(firstChar==0)
                   1791:                 {
                   1792:                   ThotBool        oldStructureChecking;
                   1793:                   DisplayMode     dispMode;
                   1794:                   dispMode = TtaGetDisplayMode (doc);
                   1795:                   if (dispMode == DisplayImmediately)
                   1796:                     TtaSetDisplayMode (doc, DeferredDisplay);
                   1797:                   oldStructureChecking = TtaGetStructureChecking (doc);
                   1798:                   TtaSetStructureChecking (FALSE, doc);
                   1799:                   
                   1800:                   // Only one element fully selected
                   1801:                   bag = TtaNewElement(doc, bagType);
                   1802:                   
                   1803:                   TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1804:                   TtaInsertSibling(bag, selElem, FALSE, doc);
                   1805:                   TtaRegisterElementCreate(bag, doc);
                   1806:                   TtaRegisterElementDelete (selElem, doc);
                   1807:                   TtaRemoveTree(selElem, doc);
                   1808:                   TtaInsertFirstChild(&selElem, bag, doc);
                   1809:                   TtaRegisterElementDelete (selElem, doc);
                   1810:                   SetAttributeStringValue(bag, Template_ATTR_types, "any");
                   1811:                   SetAttributeStringValue(bag, Template_ATTR_title, buffer);
                   1812:                   TtaCloseUndoSequence(doc);
                   1813:                   TtaSelectElement(doc, bag);
                   1814:                   
                   1815:                   TtaSetStructureChecking (oldStructureChecking, doc);
                   1816:                   TtaSetDisplayMode (doc, dispMode);
                   1817: 
                   1818:                 }
                   1819:             }
                   1820:         }
                   1821:     }
                   1822: #endif /* TEMPLATES */
                   1823: }
                   1824: 

Webmaster