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

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.175     vatton    477:       CreateInstance (templatename, s, docname, 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.134     kia       494:   XTigerTemplate t = GetXTigerTemplate (template_url);
1.112     vatton    495:   if (t)
1.107     kia       496:     t->users++;
                    497: #endif /* TEMPLATES */
                    498: }
                    499: 
                    500: /*----------------------------------------------------------------------
                    501:   AllowReloadingTemplate
                    502:   Allow reloading a template.
                    503:   You must call it after each PreventReloadingTemplate call.
                    504:   ----------------------------------------------------------------------*/
                    505: void AllowReloadingTemplate(char* template_url)
                    506: {
                    507: #ifdef TEMPLATES
1.134     kia       508:   XTigerTemplate t = GetXTigerTemplate (template_url);
1.112     vatton    509:   if (t)
1.107     kia       510:     t->users--;  
                    511: #endif /* TEMPLATES */  
                    512: }
                    513: 
                    514: 
1.137     vatton    515: /*----------------------------------------------------------------------
                    516:   ----------------------------------------------------------------------*/
1.134     kia       517: ThotBool isEOSorWhiteSpace (const char c)
                    518: {
1.137     vatton    519:   return c == SPACE || c == TAB || c ==  EOL || c ==__CR__ || c == EOS;
                    520: }
                    521: ThotBool isWhiteSpace (const char c)
                    522: {
                    523:   return c == SPACE || c == TAB || c == EOL || c ==__CR__;
1.134     kia       524: }
                    525: 
1.107     kia       526: /*----------------------------------------------------------------------
1.87      kia       527:   giveItems : Lists type items from string
                    528:   example : "one two three" is extracted to {one, two, three}
                    529:   note : item type are setted to SimpleTypeNat
                    530:   text : text from which list items
                    531:   size : size of text in characters
                    532:   items : address of exctracted item list
                    533:   nbitems : items number in items list
1.52      vatton    534:   ----------------------------------------------------------------------*/
1.76      vatton    535: void giveItems (char *text, int size, struct menuType **items, int *nbitems)
1.1       cvs       536: {
1.70      quint     537: #ifdef TEMPLATES
1.148     kia       538:   ThotBool         inElement = TRUE;
1.52      vatton    539:   struct menuType *menu;
                    540:   char            *iter;
1.148     kia       541:   char             temp[128];
1.52      vatton    542:   int              i;
1.148     kia       543:   int              labelSize;
1.28      tollenae  544: 
1.148     kia       545:   *nbitems = 1;
                    546:   for (i = 0; i < size; i++)
1.52      vatton    547:     {
                    548:       if (isEOSorWhiteSpace (text[i]))
                    549:         {
                    550:           if (inElement)
                    551:             inElement = FALSE;
                    552:         }
                    553:       else if (!inElement)
                    554:         {
                    555:           inElement = TRUE;
                    556:           (*nbitems)++;
                    557:         }
                    558:     }
1.51      francesc  559: 
1.148     kia       560:   menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
                    561:   iter = text;
                    562:   for (i = 0; i < *nbitems; i++)
                    563:     {
1.52      vatton    564:       labelSize = 0;
1.137     vatton    565:       while (isWhiteSpace (*iter))
1.52      vatton    566:         iter++;
1.137     vatton    567:       if (*iter != EOS)
                    568:         {
                    569:           while (!isEOSorWhiteSpace (*iter))
                    570:             {
                    571:               temp[labelSize++] = *iter;
                    572:               iter++;
                    573:             }
1.52      vatton    574: 
1.137     vatton    575:           temp[labelSize] = EOS;
                    576:           menu[i].label = (char *) TtaStrdup (temp);
                    577:           menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
1.52      vatton    578:         }
                    579:     }
1.137     vatton    580:   *items = menu;
1.70      quint     581: #endif /* TEMPLATES */
1.28      tollenae  582: }
1.37      tollenae  583: 
1.70      quint     584: #ifdef TEMPLATES
1.52      vatton    585: /*----------------------------------------------------------------------
                    586:   ----------------------------------------------------------------------*/
                    587: static char *createMenuString (const struct menuType* items, const int nbItems)
                    588: {
                    589:   char *result, *iter;
1.148     kia       590:   int   size = 0;
1.52      vatton    591:   int   i;
                    592: 
1.148     kia       593:   for (i=0; i < nbItems; i++)
                    594:     size += 2 + strlen (items[i].label);
1.52      vatton    595: 
1.148     kia       596:   result = (char *) TtaGetMemory (size);
                    597:   iter = result;
                    598:   for (i=0; i < nbItems; i++)
1.52      vatton    599:     {
                    600:       *iter = 'B';
                    601:       ++iter;
1.148     kia       602: 
1.52      vatton    603:       strcpy (iter, items[i].label);
                    604:       iter += strlen (items[i].label)+1;
                    605:     }
1.148     kia       606:   return result;
1.36      tollenae  607: }
1.71      quint     608: #endif /* TEMPLATES */
1.29      tollenae  609: 
1.71      quint     610: /*----------------------------------------------------------------------
                    611:   UseToBeCreated
                    612:   An new use element will be created by the user through some generic editing
                    613:   command
                    614:   -----------------------------------------------------------------------*/
                    615: ThotBool UseToBeCreated (NotifyElement *event)
                    616: {
                    617: #ifdef TEMPLATES
1.122     kia       618:   ElementType   parentType;
1.138     vatton    619:   SSchema       templateSSchema = TtaGetSSchema ("Template", event->document);  
1.130     vatton    620:   if (templateSSchema)
1.122     kia       621:   {
1.130     vatton    622:     parentType = TtaGetElementType (event->element);
                    623:     if (parentType.ElSSchema == templateSSchema &&
                    624:         parentType.ElTypeNum == Template_EL_repeat)
                    625:       return !Template_CanInsertRepeatChild (event->element);
                    626:     return TemplateElementWillBeCreated (event);
1.122     kia       627:   }
1.52      vatton    628: #endif /* TEMPLATES */
1.71      quint     629:   return FALSE; /* let Thot perform normal operation */
                    630: }
                    631: 
                    632: /*----------------------------------------------------------------------
                    633:   UseCreated
                    634:   A new "use" element has just been created by the user with a generic editing
                    635:   command.
                    636:   -----------------------------------------------------------------------*/
                    637: void UseCreated (NotifyElement *event)
                    638: {
                    639: #ifdef TEMPLATES
1.148     kia       640:   Document        doc = event->document;
                    641:   Element         el = event->element;
1.117     kia       642:   Element         parent;
                    643:   Element         first;
                    644:   ElementType     parentType;
                    645:   XTigerTemplate  t;
                    646:   SSchema         templateSSchema;
1.130     vatton    647:   char*           types, *text = NULL;
1.148     kia       648: 
1.112     vatton    649:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       650:     return;
                    651: 
1.72      quint     652:   if (TtaGetFirstChild (el))
                    653:     /* this Use element has already some content. It has already been
                    654:        instanciated */
                    655:     return;
1.117     kia       656: 
1.171     kia       657:   t = GetXTigerDocTemplate (doc);
1.71      quint     658:   if (!t)
                    659:     return; // no template ?!?!
1.101     kia       660: 
1.138     vatton    661:   templateSSchema = TtaGetSSchema ("Template", doc);
1.117     kia       662:   parent = TtaGetParent(el);
                    663:   parentType = TtaGetElementType(parent);
1.148     kia       664: 
1.130     vatton    665:   if (parentType.ElSSchema == templateSSchema &&
                    666:       parentType.ElTypeNum == Template_EL_repeat)
1.117     kia       667:   {
1.130     vatton    668:     first = TtaGetFirstChild (parent);
                    669:     if (first)
1.117     kia       670:     {
1.130     vatton    671:       types = GetAttributeStringValueFromNum (first, Template_ATTR_types, NULL);
                    672:       if (types)
1.117     kia       673:       {
1.130     vatton    674:         SetAttributeStringValueWithUndo (el, Template_ATTR_types, types);
                    675:         text = GetAttributeStringValueFromNum (el, Template_ATTR_title, NULL);
                    676:         SetAttributeStringValueWithUndo (first, Template_ATTR_title, text);
                    677:         TtaFreeMemory (text);
                    678:         text = GetAttributeStringValueFromNum (el, Template_ATTR_currentType, NULL);
                    679:         SetAttributeStringValueWithUndo (first, Template_ATTR_currentType, text);
                    680:         TtaFreeMemory (text);
                    681:         TtaFreeMemory (types);
1.117     kia       682:       }
                    683:     }
                    684:   }
                    685: 
1.76      vatton    686:   InstantiateUse (t, el, doc, TRUE);
1.71      quint     687: #endif /* TEMPLATES */
                    688: }
1.29      tollenae  689: 
1.89      kia       690: 
1.98      kia       691: /*----------------------------------------------------------------------
                    692:   Template_CanInsertRepeatChild
                    693:   Test if a xt:repeat child can be inserted (number between params min and max).
                    694:   @param el element (xt:repeat) to test
                    695:   @return True if an element can be inserted.
                    696:   ----------------------------------------------------------------------*/
                    697: ThotBool Template_CanInsertRepeatChild(Element el)
                    698: {
                    699: #ifdef TEMPLATES
1.154     vatton    700:   char     *max;
                    701:   int       maxVal, curVal;
                    702:   Element   child;
1.148     kia       703: 
1.104     kia       704:   max = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
1.105     vatton    705:   if (max)
1.104     kia       706:     {
1.154     vatton    707:       if (!strcmp(max, "*"))
                    708:         {
                    709:           TtaFreeMemory(max);
                    710:           return TRUE;
                    711:         }
                    712:       maxVal = atoi (max);
                    713:       TtaFreeMemory (max);
1.104     kia       714:       curVal = 0;
1.105     vatton    715:       for (child = TtaGetFirstChild(el); child; TtaNextSibling(&child))
1.104     kia       716:         curVal++;
1.154     vatton    717:       return (curVal < maxVal);
1.104     kia       718:     }
                    719:   else
1.98      kia       720:     return TRUE;
                    721: #endif /* TEMPLATES */
                    722:   return FALSE;
                    723: }
1.96      kia       724: 
1.89      kia       725: 
1.92      kia       726: #ifdef TEMPLATES
1.99      kia       727: /*----------------------------------------------------------------------
                    728:   QueryStringFromMenu
                    729:   Show a context menu to query a choice.
                    730:   @param items space-separated choice list string.
                    731:   @return The choosed item string or NULL if none.
                    732:   ----------------------------------------------------------------------*/
1.138     vatton    733: static char* QueryStringFromMenu (Document doc, char* items)
1.90      kia       734: {
                    735:   int nbitems, size;
                    736:   struct menuType *itemlist;
                    737:   char *menuString;
                    738:   char *result = NULL;
1.148     kia       739: 
1.138     vatton    740:   if (!TtaGetDocumentAccessMode (doc))
1.112     vatton    741:     return NULL;
                    742:   if (items == NULL)
1.110     kia       743:     return NULL;
1.138     vatton    744:   size = strlen (items);
                    745:   if (size == 0)
                    746:     return NULL;
1.90      kia       747:   giveItems (items, size, &itemlist, &nbitems);
                    748:   menuString = createMenuString (itemlist, nbitems);
                    749:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    750:                      nbitems, menuString , NULL, false, 'L');
                    751:   TtaFreeMemory (menuString);
                    752:   ReturnOption = -1;
                    753:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    754:   TtaWaitShowProcDialogue ();
                    755:   TtaDestroyDialogue (BaseDialog + OptionMenu);
1.148     kia       756: 
1.112     vatton    757:   if (ReturnOption!=-1)
1.90      kia       758:   {
                    759:     result = TtaStrdup(itemlist[ReturnOption].label);
                    760:   }
                    761:   
                    762:   TtaFreeMemory (itemlist);
                    763:   return result;
                    764: }
1.92      kia       765: #endif /* TEMPLATES */
1.90      kia       766: 
1.158     kia       767: #ifdef AMAYA_DEBUG
                    768: void FillInsertableElemList (Document doc, Element elem, DLList list);
                    769: #endif /* AMAYA_DEBUG */
1.162     vatton    770: /*----------------------------------------------------------------------
                    771:   ----------------------------------------------------------------------*/
                    772: char *Template_GetListTypes (XTigerTemplate t, Element el)
                    773: {
                    774: #ifdef TEMPLATES
                    775:   char  *listtypes = NULL, *types;
                    776: 
                    777:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
                    778:   if (types)
                    779:     listtypes = Template_ExpandTypes (t, types, el, FALSE);
                    780:   return listtypes;
                    781: #endif /* TEMPLATES */
                    782: }
1.158     kia       783: 
1.56      francesc  784: /*----------------------------------------------------------------------
1.138     vatton    785:   BagButtonClicked
                    786:   Called when a bag button is clicked.
                    787:   Can be called for useEl, useSimple or bag.
                    788:   If called for useEl or useSimple, the new element must be added after.
                    789:   If called for bag, the element must be added before all.
                    790:    
                    791:   Shows a menu with all the types that can be used in the bag.
                    792:   ----------------------------------------------------------------------*/
                    793: ThotBool BagButtonClicked (NotifyElement *event)
                    794: {
                    795: #ifdef TEMPLATES
                    796:   Document        doc = event->document;
                    797:   Element         el = event->element;
                    798:   ElementType     elType;
                    799:   XTigerTemplate  t;
                    800:   Declaration     decl;
                    801:   Element         bagEl = el;
                    802:   Element         firstEl;
                    803:   Element         newEl = NULL;
                    804:   View            view;
                    805:   SSchema         templateSSchema;
1.162     vatton    806:   char           *listtypes = NULL;
                    807:   char           *result = NULL;
1.138     vatton    808:   ThotBool        oldStructureChecking;
                    809:   DisplayMode     dispMode;
                    810: 
                    811:   if (!TtaGetDocumentAccessMode(doc))
                    812:     return TRUE;
1.148     kia       813: 
1.138     vatton    814:   TtaGetActiveView (&doc, &view);
                    815:   if (view != 1)
                    816:     return FALSE; /* let Thot perform normal operation */
                    817: 
                    818:   TtaCancelSelection (doc);
                    819:   templateSSchema = TtaGetSSchema ("Template", doc);  
1.171     kia       820:   t = GetXTigerDocTemplate(doc);
1.138     vatton    821:   elType = TtaGetElementType (el);
                    822:   while (bagEl &&
                    823:          (elType.ElSSchema != templateSSchema ||
                    824:           elType.ElTypeNum != Template_EL_bag))
                    825:     {
                    826:       bagEl = TtaGetParent (bagEl);
                    827:       elType = TtaGetElementType (bagEl);
                    828:     }
                    829: 
                    830:   if (bagEl)
                    831:   {
1.162     vatton    832:     listtypes = Template_GetListTypes (t, bagEl);
                    833:     if (listtypes)
1.138     vatton    834:       {
1.158     kia       835: #ifdef AMAYA_DEBUG
                    836:       printf("BagButtonClicked : \n  > %s\n", listtypes);
                    837: //      {
                    838: //        DLList list = DLList_Create();
                    839: //        FillInsertableElemList (doc, TtaGetFirstChild(bagEl), list);
                    840: //        DLListNode node;
                    841: //        ForwardIterator iter = DLList_GetForwardIterator(list);
                    842: //        ITERATOR_FOREACH(iter, DLListNode, node)
                    843: //        {
                    844: //          ElemListElement elem = (ElemListElement)node->elem;
                    845: //          printf("  + %s\n", ElemListElement_GetName(elem));
                    846: //        }
                    847: //        DLList_Destroy(list);
                    848: //      }
                    849: #endif /* AMAYA_DEBUG */
1.138     vatton    850:         result = QueryStringFromMenu (doc, listtypes);
                    851:         TtaFreeMemory (listtypes);
                    852:         if (result)
                    853:           {
                    854:             decl = Template_GetDeclaration (t, result);
                    855:             if (decl)
                    856:               {
                    857:                 dispMode = TtaGetDisplayMode (doc);
                    858:                 if (dispMode == DisplayImmediately)
                    859:                   /* don't set NoComputedDisplay
                    860:                      -> it breaks down views formatting when Enter generates new elements  */
1.139     vatton    861:                   TtaSetDisplayMode (doc, DeferredDisplay);
1.138     vatton    862: 
                    863:                 /* Prepare insertion.*/          
                    864:                 oldStructureChecking = TtaGetStructureChecking (doc);
                    865:                 TtaSetStructureChecking (FALSE, doc);
                    866:                 TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    867: 
                    868:                 /* Insert */
                    869:                 if (el == bagEl)
                    870:                   {
                    871:                     el = TtaGetFirstChild (el);
                    872:                     TtaSelectElement (doc, el);
                    873:                     TtaInsertAnyElement (doc, TRUE);
                    874:                   }
                    875:                 else
                    876:                   {
                    877:                     TtaSelectElement (doc, el);
                    878:                     TtaInsertAnyElement (doc, FALSE);
                    879:                   }
1.155     kia       880:                 newEl = Template_InsertBagChild (doc, bagEl, decl, FALSE);
1.138     vatton    881: 
                    882:                 /* Finish insertion.*/
                    883:                 TtaCloseUndoSequence (doc);
                    884:                 TtaSetDocumentModified (doc);
                    885:                 TtaSetStructureChecking (oldStructureChecking, doc);
                    886:                 // restore the display
                    887:                 TtaSetDisplayMode (doc, dispMode);
                    888:                 firstEl = GetFirstEditableElement (newEl);
                    889:                 if (firstEl)
                    890:                   {
                    891:                     TtaSelectElement (doc, firstEl);
                    892:                     TtaSetStatusSelectedElement (doc, view, firstEl);
                    893:                   }
                    894:                 else
                    895:                   {
                    896:                     TtaSelectElement (doc, newEl);
                    897:                     TtaSetStatusSelectedElement (doc, view, newEl);
                    898:                   }
                    899:               }
                    900:           }
                    901:       }
                    902:     TtaFreeMemory (result);
                    903:   }
                    904: #endif /* TEMPLATES */
                    905:   return TRUE; /* don't let Thot perform normal operation */
                    906: }
                    907: 
                    908: /*----------------------------------------------------------------------
1.79      quint     909:   RepeatButtonClicked
1.89      kia       910:   Called when a repeat button is clicked.
                    911:   Can be called for useEl, useSimple or repeat.
                    912:   If called for useEl or useSimple, the new element must be added after.
                    913:   If called for repeat, the element must be added before all.
                    914:    
1.56      francesc  915:   Shows a menu with all the types that can be used in a use element.
                    916:   ----------------------------------------------------------------------*/
1.79      quint     917: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  918: {
                    919: #ifdef TEMPLATES
1.89      kia       920:   Document        doc = event->document;
                    921:   Element         el = event->element;
                    922:   ElementType     elType;
1.90      kia       923:   XTigerTemplate  t;
                    924:   Declaration     decl;
                    925:   Element         repeatEl = el;
                    926:   Element         firstEl;
                    927:   Element         newEl = NULL;
1.95      kia       928:   View            view;
1.162     vatton    929:   char           *listtypes = NULL;
                    930:   char           *result = NULL;
1.138     vatton    931:   ThotBool        oldStructureChecking;
                    932:   DisplayMode     dispMode;
1.104     kia       933: 
1.112     vatton    934:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       935:     return TRUE;
1.148     kia       936: 
1.95      kia       937:   TtaGetActiveView (&doc, &view);
                    938:   if (view != 1)
                    939:     return FALSE; /* let Thot perform normal operation */
                    940: 
1.89      kia       941:   TtaCancelSelection(doc);
1.148     kia       942: 
1.171     kia       943:   t = GetXTigerDocTemplate(doc);
1.89      kia       944:   elType = TtaGetElementType(el);
1.138     vatton    945:   while (elType.ElTypeNum != Template_EL_repeat)
1.89      kia       946:   {
1.90      kia       947:     repeatEl = TtaGetParent(repeatEl);
1.138     vatton    948:     if (repeatEl == NULL)
1.89      kia       949:       break;
1.90      kia       950:     elType = TtaGetElementType(repeatEl);
1.89      kia       951:   }
1.112     vatton    952:   if (repeatEl)
1.89      kia       953:   {
1.138     vatton    954:     if (Template_CanInsertRepeatChild (repeatEl))
1.90      kia       955:     {
1.138     vatton    956:       firstEl = TtaGetFirstChild (repeatEl);
1.162     vatton    957:     listtypes = Template_GetListTypes (t, firstEl);
                    958:     if (listtypes)
1.90      kia       959:       {
1.158     kia       960: #ifdef AMAYA_DEBUG
                    961:       printf("RepeatButtonClicked : \n  > %s\n", listtypes);
                    962: #endif /* AMAYA_DEBUG */
                    963:         
1.138     vatton    964:         result = QueryStringFromMenu (doc, listtypes);
                    965:         TtaFreeMemory (listtypes);
1.112     vatton    966:         if (result)
1.98      kia       967:         {
1.138     vatton    968:           decl = Template_GetDeclaration (t, result);
1.112     vatton    969:           if (decl)
1.104     kia       970:           {
1.138     vatton    971:             dispMode = TtaGetDisplayMode (doc);
                    972:             if (dispMode == DisplayImmediately)
                    973:               /* don't set NoComputedDisplay
                    974:                  -> it breaks down views formatting when Enter generates new elements  */
1.139     vatton    975:               TtaSetDisplayMode (doc, DeferredDisplay);
1.138     vatton    976: 
1.104     kia       977:             /* Prepare insertion.*/          
                    978:             oldStructureChecking = TtaGetStructureChecking (doc);
                    979:             TtaSetStructureChecking (FALSE, doc);
1.138     vatton    980:             TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104     kia       981:             /* Insert. */
1.138     vatton    982:             if (el == repeatEl)
                    983:               newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, NULL);
1.104     kia       984:             else
1.138     vatton    985:               newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, el);
1.104     kia       986:               
                    987:             /* Finish insertion.*/
                    988:             TtaCloseUndoSequence(doc);
1.114     vatton    989:             TtaSetDocumentModified (doc);
1.104     kia       990:             TtaSetStructureChecking (oldStructureChecking, doc);
1.138     vatton    991: 
                    992:             // restore the display
                    993:             TtaSetDisplayMode (doc, dispMode);
                    994:             firstEl = GetFirstEditableElement (newEl);
1.112     vatton    995:             if (firstEl)
1.104     kia       996:             {
                    997:               TtaSelectElement (doc, firstEl);
1.138     vatton    998:               TtaSetStatusSelectedElement (doc, view, firstEl);
1.104     kia       999:             }
                   1000:             else
                   1001:             {
                   1002:               TtaSelectElement (doc, newEl);
1.138     vatton   1003:               TtaSetStatusSelectedElement (doc, view, newEl);
1.104     kia      1004:             }
1.98      kia      1005:           }
                   1006:         }
1.90      kia      1007:       }
1.98      kia      1008:       TtaFreeMemory(result);
                   1009:     }
1.112     vatton   1010:     else /* if (Template_CanInsertRepeatChild(repeatEl)) */
1.98      kia      1011:     {
                   1012:       TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.90      kia      1013:     }
1.89      kia      1014:   }
1.77      vatton   1015:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc 1016: #endif /* TEMPLATES */
1.94      kia      1017:   return TRUE;
                   1018: }
                   1019: 
                   1020: /*----------------------------------------------------------------------
                   1021:   UseButtonClicked
                   1022:   Shows a menu with all the types that can be used in a use element.
                   1023:   ----------------------------------------------------------------------*/
                   1024: ThotBool UseButtonClicked (NotifyElement *event)
                   1025: {
                   1026: #ifdef TEMPLATES
                   1027:   Document        doc = event->document;
                   1028:   Element         el = event->element;
1.99      kia      1029:   Element         child;
1.94      kia      1030:   ElementType     elType;
                   1031:   XTigerTemplate  t;
                   1032:   Declaration     decl;
                   1033:   Element         firstEl;
                   1034:   Element         newEl = NULL;
                   1035:   char*           types;
                   1036:   ThotBool        oldStructureChecking;
1.95      kia      1037:   View            view;
1.133     vatton   1038:   char*           listtypes = NULL;
                   1039:   char*           result = NULL;
1.95      kia      1040: 
1.112     vatton   1041:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia      1042:     return TRUE;
                   1043:     
1.95      kia      1044:   TtaGetActiveView (&doc, &view);
                   1045:   if (view != 1)
                   1046:     return FALSE; /* let Thot perform normal operation */
1.148     kia      1047: 
1.94      kia      1048:   TtaCancelSelection(doc);
                   1049:   
1.171     kia      1050:   t = GetXTigerDocTemplate(doc);
1.94      kia      1051:   if (!t)
                   1052:     return FALSE; /* let Thot perform normal operation */
                   1053:   elType = TtaGetElementType(el);
                   1054: 
                   1055:   firstEl = TtaGetFirstChild(el);
1.112     vatton   1056:   if (firstEl)
1.94      kia      1057:   {
                   1058:     RepeatButtonClicked(event);
                   1059:   }
                   1060:   else
                   1061:   {
1.104     kia      1062:     types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.112     vatton   1063:     if (types)
1.94      kia      1064:     {
1.142     kia      1065:       listtypes = Template_ExpandTypes(t, types, NULL, FALSE);
1.158     kia      1066: #ifdef AMAYA_DEBUG
                   1067:       printf("UseButtonClicked : \n  > %s\n", listtypes);
                   1068: #endif /* AMAYA_DEBUG */
                   1069:       
1.104     kia      1070:       result = QueryStringFromMenu(doc, listtypes);
1.112     vatton   1071:       if (result)
1.94      kia      1072:       {
1.104     kia      1073:         decl = Template_GetDeclaration(t, result);
1.112     vatton   1074:         if (decl)
1.99      kia      1075:         {
1.104     kia      1076:           /* Prepare insertion.*/
                   1077:           oldStructureChecking = TtaGetStructureChecking (doc);
                   1078:           TtaSetStructureChecking (FALSE, doc);
1.138     vatton   1079:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104     kia      1080:           
                   1081:           /* Insert */
                   1082:           newEl = Template_InsertUseChildren(doc, el, decl);
                   1083:           for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
                   1084:           {
                   1085:             TtaRegisterElementCreate(child, doc);
                   1086:           }
                   1087:           
                   1088:           TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
                   1089:           TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
                   1090:           
1.117     kia      1091:           /* xt:currentType attribute.*/
                   1092:           SetAttributeStringValueWithUndo(el, Template_ATTR_currentType, result);
                   1093:           
1.104     kia      1094:           /* Finish insertion. */
                   1095:           TtaCloseUndoSequence(doc);
1.114     vatton   1096:           TtaSetDocumentModified (doc);
1.104     kia      1097:           TtaSetStructureChecking (oldStructureChecking, doc);
                   1098:           
                   1099:           firstEl = GetFirstEditableElement(newEl);
1.112     vatton   1100:           if (firstEl)
1.104     kia      1101:           {
                   1102:             TtaSelectElement (doc, firstEl);
                   1103:             TtaSetStatusSelectedElement(doc, view, firstEl);
                   1104:           }
                   1105:           else
                   1106:           {
                   1107:             TtaSelectElement (doc, newEl);
                   1108:             TtaSetStatusSelectedElement(doc, view, newEl);
                   1109:           }
1.98      kia      1110:         }
1.94      kia      1111:       }
                   1112:     }
1.104     kia      1113:     TtaFreeMemory(types);
1.94      kia      1114:     TtaFreeMemory(listtypes);
                   1115:     TtaFreeMemory(result);
                   1116:   }
1.148     kia      1117: 
1.94      kia      1118:   return TRUE;
                   1119: #endif /* TEMPLATES */
1.56      francesc 1120:        return TRUE;
                   1121: }
1.64      francesc 1122: 
1.89      kia      1123: 
1.103     kia      1124: /*----------------------------------------------------------------------
                   1125:   UseSimpleButtonClicked
                   1126:   ----------------------------------------------------------------------*/
                   1127: ThotBool UseSimpleButtonClicked (NotifyElement *event)
                   1128: {
                   1129: #ifdef TEMPLATES
1.112     vatton   1130:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1131:     return TRUE;
                   1132: 
1.138     vatton   1133:   ElementType parentType = TtaGetElementType (TtaGetParent( event->element));
1.112     vatton   1134:   if (parentType.ElTypeNum == Template_EL_repeat)
1.138     vatton   1135:     return RepeatButtonClicked (event);
                   1136:   else if (parentType.ElTypeNum == Template_EL_bag)
                   1137:     return BagButtonClicked (event);
1.103     kia      1138: #endif /* TEMPLATES */  
                   1139:   return FALSE;
                   1140: }
1.94      kia      1141: 
                   1142: /*----------------------------------------------------------------------
                   1143:   OptionButtonClicked
                   1144:   ----------------------------------------------------------------------*/
                   1145: ThotBool OptionButtonClicked (NotifyElement *event)
                   1146: {
                   1147: #ifdef TEMPLATES
1.145     kia      1148:   Element         useEl, contentEl, next;
                   1149:   ElementType     useType, optType;
1.94      kia      1150:   Document        doc;
                   1151:   XTigerTemplate  t;
                   1152:   View            view;
                   1153: 
1.112     vatton   1154:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1155:     return TRUE;
                   1156: 
1.94      kia      1157:   TtaGetActiveView (&doc, &view);
                   1158:   if (view != 1)
                   1159:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1160: 
1.94      kia      1161:   doc = event->document;
1.145     kia      1162:   useEl = TtaGetFirstChild (event->element);
                   1163:   if (!useEl)
1.94      kia      1164:     return FALSE; /* let Thot perform normal operation */
1.145     kia      1165:   useType = TtaGetElementType (useEl);
                   1166:   optType = TtaGetElementType (event->element);
                   1167:   if ((useType.ElTypeNum != Template_EL_useEl &&
1.148     kia      1168:       useType.ElTypeNum != Template_EL_useSimple) ||
                   1169:       useType.ElSSchema != optType.ElSSchema)
1.94      kia      1170:     return FALSE;
                   1171: 
1.145     kia      1172:   TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
1.148     kia      1173: 
1.94      kia      1174:   TtaCancelSelection (doc);
1.148     kia      1175: 
1.145     kia      1176:   contentEl = TtaGetFirstChild (useEl);
                   1177:   if (!contentEl)
1.94      kia      1178:     /* the "use" element is empty. Instantiate it */
                   1179:     {
1.171     kia      1180:       t = GetXTigerDocTemplate (doc);
1.94      kia      1181:       if (!t)
                   1182:         return FALSE; // no template ?!?!
1.145     kia      1183:       InstantiateUse (t, useEl, doc, TRUE);
1.94      kia      1184:     }
                   1185:   else
                   1186:     /* remove the content of the "use" element */
                   1187:     {
                   1188:       do
                   1189:         {
1.145     kia      1190:           next = contentEl;
1.94      kia      1191:           TtaNextSibling (&next);
1.145     kia      1192:           TtaRegisterElementDelete(contentEl, doc);
                   1193:           TtaDeleteTree (contentEl, doc);
                   1194:           contentEl = next;
1.94      kia      1195:         }
                   1196:       while (next);
1.145     kia      1197:       if (NeedAMenu (useEl, doc))
                   1198:         {
                   1199:           TtaChangeTypeOfElement (useEl, doc, Template_EL_useEl);
                   1200:           TtaRegisterElementTypeChange(useEl, Template_EL_useSimple, doc);
                   1201:         }
1.94      kia      1202:     }
                   1203:   TtaSelectElement (doc, event->element);
1.145     kia      1204:   TtaCloseUndoSequence(doc);
1.94      kia      1205:   return TRUE; /* don't let Thot perform normal operation */
                   1206: #endif /* TEMPLATES */
                   1207:   return TRUE;
                   1208: }
                   1209: 
1.111     vatton   1210: /*----------------------------------------------------------------------
                   1211:   CheckTemplate checks if the template of the instance is loaded
1.126     vatton   1212:   Return TRUE if the template is loaded
1.111     vatton   1213:   ----------------------------------------------------------------------*/
                   1214: void CheckTemplate (Document doc)
                   1215: {
                   1216: #ifdef TEMPLATES
1.144     vatton   1217:   Element    root;
1.165     kia      1218:   
1.171     kia      1219:   if(IsTemplateInstanceDocument(doc))
1.111     vatton   1220:     {
1.144     vatton   1221:       XTigerTemplate   t;
                   1222: 
                   1223:       root = TtaGetRootElement (doc);
                   1224:       TtaSetAccessRight (root, ReadOnly, doc);
1.171     kia      1225:       t = GetXTigerDocTemplate (doc);
1.144     vatton   1226:       if (t == NULL)
                   1227:         {
                   1228:           // the template cannot be loaded
                   1229:           InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                   1230:           TtaSetDocumentAccessMode (doc, 0); // document readonly
                   1231:         }
                   1232:       else
                   1233:         {
                   1234:           // fix all access rights in the instance
1.171     kia      1235:           Template_PrepareTemplate(t);
1.166     kia      1236:           Template_FixAccessRight (t, root, doc);
1.144     vatton   1237:           TtaUpdateAccessRightInViews (doc, root);
                   1238:         }
1.111     vatton   1239:     }
                   1240: #endif /* TEMPLATES */
                   1241: }
1.94      kia      1242: 
1.66      vatton   1243: /*----------------------------------------------------------------------
1.108     vatton   1244:   OpeningInstance checks if it is a template instance needs.
                   1245:   If it's an instance and the template is not loaded, load it into a
                   1246:   temporary file
1.66      vatton   1247:   ----------------------------------------------------------------------*/
1.171     kia      1248: void OpeningInstance (char *localFileName, Document doc, char* docURL)
1.65      francesc 1249: {
                   1250: #ifdef TEMPLATES
1.76      vatton   1251:   XTigerTemplate   t;
1.103     kia      1252:   char            *content, *ptr, *begin;
1.76      vatton   1253:   gzFile           stream;
                   1254:   char             buffer[2000];
1.77      vatton   1255:   int              res;
1.171     kia      1256:   char            *template_version = NULL,
                   1257:                   *template_url = NULL;
1.65      francesc 1258: 
1.171     kia      1259:   stream = TtaGZOpen (localFileName);
1.76      vatton   1260:   if (stream != 0)
1.65      francesc 1261:     {
1.76      vatton   1262:       res = gzread (stream, buffer, 1999);
                   1263:       if (res >= 0)
1.65      francesc 1264:         {
1.81      vatton   1265:           buffer[res] = EOS;
1.103     kia      1266:           begin = strstr (buffer, "<?xtiger");
                   1267:           
1.112     vatton   1268:           if (begin)
1.103     kia      1269:           {
                   1270:             // Search for template version
                   1271:             ptr = strstr (begin, "templateVersion");
                   1272:             if (ptr)
                   1273:               ptr = strstr (ptr, "=");
                   1274:             if (ptr)
                   1275:               ptr = strstr (ptr, "\"");
                   1276:             if (ptr)
                   1277:               {
                   1278:                 // template URI
                   1279:                 content = &ptr[1];
                   1280:                 ptr = strstr (content, "\"");
                   1281:               }
                   1282:             if (ptr)
                   1283:               {
                   1284:                 *ptr = EOS;
                   1285:                 //Get now the template URI
1.171     kia      1286:                 template_version = TtaStrdup (content);
1.103     kia      1287:                 *ptr = '"';
                   1288:               }
                   1289:            
                   1290:             // Search for template uri
                   1291:             ptr = strstr (begin, "template");
                   1292:             if (ptr && ptr[8] != 'V')
                   1293:               ptr = strstr (ptr, "=");
                   1294:             if (ptr)
                   1295:               ptr = strstr (ptr, "\"");
                   1296:             if (ptr)
                   1297:               {
                   1298:                 // template URI
                   1299:                 content = &ptr[1];
                   1300:                 ptr = strstr (content, "\"");
                   1301:               }
                   1302:             if (ptr)
                   1303:               {
                   1304:                 *ptr = EOS;
                   1305:                 //Get now the template URI
1.171     kia      1306:                 template_url = TtaStrdup (content);
                   1307: 
                   1308:                 t = GetXTigerTemplate (template_url);
1.103     kia      1309:                 if (!t)
                   1310:                   {
1.171     kia      1311:                     LoadTemplate (doc, template_url);
                   1312:                     t = GetXTigerTemplate(template_url);
1.103     kia      1313:                   }
1.171     kia      1314:                 Template_PrepareInstance(docURL, doc, template_version, template_url);
                   1315:                 template_version = NULL;
                   1316:                 template_url     = NULL;
                   1317:                 Template_AddReference (t);
1.103     kia      1318:                 *ptr = '"';
                   1319:               }
                   1320:           }
1.65      francesc 1321:         }
                   1322:     }
1.171     kia      1323:   TtaFreeMemory(template_version);
                   1324:   TtaFreeMemory(template_url);
1.76      vatton   1325:   TtaGZClose (stream);
1.65      francesc 1326: #endif /* TEMPLATES */
                   1327: }
                   1328: 
1.64      francesc 1329: /*----------------------------------------------------------------------
1.65      francesc 1330:   ClosingInstance
1.64      francesc 1331:   Callback called before closing a document. Checks for unused templates.
                   1332:   ----------------------------------------------------------------------*/
1.65      francesc 1333: ThotBool ClosingInstance(NotifyDialog* dialog)
1.64      francesc 1334: {
1.65      francesc 1335: #ifdef TEMPLATES
1.171     kia      1336:   XTigerTemplate t = GetXTigerDocTemplate(dialog->document);
                   1337:   if(t)
                   1338:     Template_RemoveReference(t);
1.65      francesc 1339: #endif /* TEMPLATES */
                   1340:   return FALSE;
1.64      francesc 1341: }
1.87      kia      1342: 
                   1343: /*----------------------------------------------------------------------
1.120     kia      1344:   IsTemplateElement
1.138     vatton   1345:   Test if an element is a template element.
1.87      kia      1346:   ----------------------------------------------------------------------*/
1.140     vatton   1347: ThotBool IsTemplateElement (Element elem)
1.87      kia      1348: {
                   1349: #ifdef TEMPLATES
1.138     vatton   1350:   ElementType     elType;
                   1351: 
                   1352:   elType = TtaGetElementType(elem);
                   1353:   if (elType.ElSSchema)
1.164     kia      1354:     return (strcmp(TtaGetSSchemaName(elType.ElSSchema) , "Template") == 0);
1.138     vatton   1355: #endif /* TEMPLATES */
1.87      kia      1356:   return FALSE;
                   1357: }
                   1358: 
                   1359: 
                   1360: /*----------------------------------------------------------------------
                   1361:   GetFirstTemplateParentElement
1.138     vatton   1362:   Return the first element which has "Template" as schema name or null.
1.87      kia      1363:   ----------------------------------------------------------------------*/
                   1364: Element GetFirstTemplateParentElement(Element elem)
                   1365: {
                   1366: #ifdef TEMPLATES
1.138     vatton   1367:   ElementType     elType;
                   1368: 
                   1369:   elem = TtaGetParent (elem);
                   1370:   elType = TtaGetElementType(elem);
                   1371:   while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
                   1372: {
                   1373:     elem = TtaGetParent (elem);
                   1374:     elType = TtaGetElementType(elem);
1.87      kia      1375:   }
                   1376:   return elem;
                   1377: #else
                   1378:   return NULL;
                   1379: #endif /* TEMPLATES */
                   1380: }
1.101     kia      1381: 
1.103     kia      1382: 
1.101     kia      1383: /*----------------------------------------------------------------------
1.102     vatton   1384:   TemplateElementWillBeCreated
1.101     kia      1385:   Processed when an element will be created in a template context.
                   1386:   ----------------------------------------------------------------------*/
1.102     vatton   1387: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101     kia      1388: {
1.103     kia      1389: #ifdef TEMPLATES
                   1390:   ElementType elType = event->elementType;
                   1391:   Element     parent = event->element;
                   1392:   ElementType parentType = TtaGetElementType(parent);
1.113     kia      1393:   Element     ancestor;
                   1394:   ElementType ancestorType;
                   1395:   SSchema     templateSSchema;
                   1396:   char*       types;
                   1397:   ThotBool    b;
1.101     kia      1398: 
1.115     kia      1399:   if(event->info==1)
                   1400:     return FALSE;
                   1401: 
1.112     vatton   1402:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1403:     return TRUE;
                   1404: 
1.138     vatton   1405:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.102     vatton   1406:   if (templateSSchema == NULL)
                   1407:     return FALSE; // let Thot do the job
1.115     kia      1408: 
1.113     kia      1409:   // Fisrt, test if in a xt:bag or in a base-element xt:use
1.147     vatton   1410:   if(parentType.ElSSchema == templateSSchema)
1.113     kia      1411:     ancestor = parent;
                   1412:   else
1.147     vatton   1413:     ancestor = GetFirstTemplateParentElement (parent);
1.113     kia      1414: 
1.147     vatton   1415:   if (ancestor)
1.113     kia      1416:   {
                   1417:     ancestorType = TtaGetElementType(ancestor);
1.147     vatton   1418:     if (ancestorType.ElTypeNum == Template_EL_bag)
1.113     kia      1419:     {
1.147     vatton   1420:       // only check the use child
                   1421:       if (ancestor != parent)
                   1422:         return  FALSE; // let Thot do the job
                   1423:       if (elType.ElSSchema == templateSSchema &&
                   1424:           (elType.ElTypeNum == Template_EL_useSimple ||
                   1425:            elType.ElTypeNum == Template_EL_useEl))
1.116     kia      1426:         return FALSE;
1.147     vatton   1427:       return !Template_CanInsertElementInBagElement (event->document, elType, ancestor);      
1.113     kia      1428:     }
1.147     vatton   1429:     else if(ancestorType.ElTypeNum == Template_EL_useSimple ||
                   1430:             ancestorType.ElTypeNum == Template_EL_useEl)
1.113     kia      1431:     {
1.147     vatton   1432:       // only check the bag child @@@ will be check exclude/include later
1.172     kia      1433:       if (ancestor != parent)
                   1434:         return  FALSE; // let Thot do the job
1.113     kia      1435:       types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
1.128     kia      1436:       b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position);
                   1437:       TtaFreeMemory(types);
1.115     kia      1438:       return !b;
1.113     kia      1439:     }
                   1440:   }
1.115     kia      1441:   
1.147     vatton   1442:   if (elType.ElSSchema == templateSSchema && elType.ElTypeNum == Template_EL_TEXT_UNIT)
1.115     kia      1443:   {
                   1444:     return FALSE;
                   1445:   }
                   1446:   
1.113     kia      1447:   // Can not insert.
                   1448:   return TRUE;
1.101     kia      1449: #endif /* TEMPLATES*/
1.102     vatton   1450:   return FALSE;
1.101     kia      1451: }
                   1452: 
                   1453: /*----------------------------------------------------------------------
                   1454:   TemplateElementWillBeDeleted
                   1455:   Processed when an element will be deleted in a template context.
                   1456:   ----------------------------------------------------------------------*/
                   1457: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1458: {
1.107     kia      1459: #ifdef TEMPLATES
                   1460:   Document       doc = event->document;
                   1461:   Element        elem = event->element;
1.163     vatton   1462:   Element        xtElem, parent = NULL, sibling;
1.117     kia      1463:   ElementType    xtType, elType;
1.107     kia      1464:   char*          type;
                   1465:   Declaration    dec;
1.115     kia      1466:   SSchema        templateSSchema;
1.107     kia      1467:   XTigerTemplate t;
1.156     vatton   1468:   ThotBool       selparent = FALSE;
1.107     kia      1469: 
1.115     kia      1470:   if(event->info==1)
                   1471:     return FALSE;
                   1472: 
1.112     vatton   1473:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1474:     return TRUE;
                   1475: 
1.138     vatton   1476:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.107     kia      1477:   if (templateSSchema == NULL)
                   1478:     return FALSE; // let Thot do the job
1.122     kia      1479: 
1.107     kia      1480:   xtElem = GetFirstTemplateParentElement(elem);
1.112     vatton   1481:   if (xtElem)
1.107     kia      1482:   {
                   1483:     xtType = TtaGetElementType(xtElem);
1.117     kia      1484:     
1.171     kia      1485:     t = GetXTigerDocTemplate(doc);
1.109     kia      1486: 
1.112     vatton   1487:     if (xtType.ElTypeNum==Template_EL_bag)
1.117     kia      1488:     {
                   1489:       elType = TtaGetElementType(elem);
                   1490:       if(elType.ElSSchema==templateSSchema &&
                   1491:         (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
                   1492:       {
                   1493:         // Remove element manually.
                   1494:         TtaOpenUndoSequence(doc, elem, elem, 0, 0);
                   1495:         TtaRegisterElementDelete(elem, doc);
                   1496:         TtaDeleteTree(elem, doc);
                   1497:         TtaCloseUndoSequence(doc);
                   1498:         return TRUE;
                   1499:       }
                   1500:       else
                   1501:         return FALSE; // xt:bag always allow remove children.
                   1502:     }
1.112     vatton   1503:     else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107     kia      1504:     {
1.109     kia      1505:       parent = TtaGetParent(elem);
1.117     kia      1506:       if (xtElem!=parent)
                   1507:       {
                   1508:         type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
                   1509:         dec = Template_GetDeclaration(t, type);
                   1510:         TtaFreeMemory(type);
                   1511:         
                   1512:         if (dec && dec->nature == XmlElementNat)
                   1513:           return FALSE; // Can remove element only if in xt:use current type is base language element. 
                   1514:         else
                   1515:           return TRUE;
1.107     kia      1516:       }
1.109     kia      1517:     }
1.112     vatton   1518:     else if (xtType.ElTypeNum==Template_EL_repeat)
1.109     kia      1519:     {
1.156     vatton   1520:       sibling = TtaGetSuccessor (elem);
                   1521:       if (sibling == NULL)
                   1522:         {
                   1523:           // there is no next element
                   1524:           sibling = TtaGetPredecessor (elem);
                   1525:           if (sibling == NULL)
                   1526:             selparent = TRUE;
                   1527:         }
1.109     kia      1528:       TtaRegisterElementDelete(elem, doc);
                   1529:       TtaDeleteTree(elem, doc);
                   1530:       InstantiateRepeat(t, xtElem, doc, TRUE);
1.156     vatton   1531:       if (selparent)
                   1532:         // look for the new sibling
                   1533:         sibling = TtaGetFirstChild (parent);
                   1534:       if (sibling)
                   1535:         TtaSelectElement(doc, sibling);
1.151     quint    1536:       else
1.156     vatton   1537:         TtaSelectElement(doc, parent);
1.109     kia      1538:       return TRUE;
1.107     kia      1539:     }
                   1540:   }
1.109     kia      1541:   
                   1542:   //TODO Test if current element is use or repeat.
                   1543:   // Because if an element is delete and it is the unique child of its parent,
                   1544:   // the parent intends to destroy itself. 
                   1545:   
1.107     kia      1546:   return TRUE;
                   1547: #else /* TEMPLATES */
1.101     kia      1548:   return FALSE;
1.107     kia      1549: #endif /* TEMPLATES */
1.101     kia      1550: }
                   1551: 
1.109     kia      1552: /*----------------------------------------------------------------------
                   1553:   CurrentTypeWillBeExported
                   1554:   Check if the xt:currentType attribute can be exported
                   1555:   ----------------------------------------------------------------------*/
                   1556: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
                   1557: {
                   1558: #ifdef TEMPLATES
1.110     kia      1559: 
1.112     vatton   1560:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1561:     return TRUE;
                   1562: 
1.112     vatton   1563:   if (IsTemplateDocument(event->document))
1.109     kia      1564:     return TRUE;
                   1565: #endif /* TEMPLATES */
                   1566:   return FALSE;
                   1567: }
1.127     kia      1568: 
                   1569: /*----------------------------------------------------------------------
                   1570:   TemplateAttrInMenu
                   1571:   Called by Thot when building the Attributes menu for template elements.
                   1572:   ----------------------------------------------------------------------*/
                   1573: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
                   1574: {
                   1575: #ifdef TEMPLATES
                   1576:   // Prevent from showing attributes for template instance but not templates.
                   1577:   if(IsTemplateInstanceDocument(event->document))
                   1578:     return TRUE;
                   1579:   else
                   1580: #endif /* TEMPLATES */
                   1581:     return FALSE;
                   1582: }
1.160     kia      1583: 
1.167     kia      1584: /*----------------------------------------------------------------------
1.168     kia      1585:   CreateTemplateFromDocument
                   1586:   Create a template from the current document.
1.167     kia      1587:   ----------------------------------------------------------------------*/
                   1588: void CreateTemplateFromDocument(Document doc, View view)
                   1589: {
1.171     kia      1590: #ifdef TEMPLATES
1.168     kia      1591:   char buffer[MAX_LENGTH];
                   1592:   strcpy(buffer, DocumentURLs[doc]);
                   1593:   strcat(buffer, ".xtd");
                   1594:   DontReplaceOldDoc = TRUE;
                   1595:   CreateTemplate(doc, buffer);
1.171     kia      1596: #endif /* TEMPLATES */
1.167     kia      1597: }
                   1598: 
1.169     kia      1599: /*----------------------------------------------------------------------
                   1600:   UpdateTemplateMenus
                   1601:   ----------------------------------------------------------------------*/
                   1602: void UpdateTemplateMenus (Document doc)
                   1603: {
                   1604:   if(IsTemplateInstanceDocument(doc) || 
                   1605:       IsTemplateDocument(doc))
                   1606:     TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
                   1607:   else
                   1608:     TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
                   1609: }
1.171     kia      1610: 
                   1611: /*----------------------------------------------------------------------
                   1612:   UninstanciateTemplateDocument
                   1613:   An instance of a template is tranformed into a template-less docuemnt.
                   1614:   Remove link between XTigerTemplate structure and document.
                   1615:   ----------------------------------------------------------------------*/
                   1616: void UninstanciateTemplateDocument(Document doc)
                   1617: {
                   1618: #ifdef TEMPLATES
                   1619:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1620:   if(t)
                   1621:     Template_Close(t);
                   1622: #endif /* TEMPLATES */  
                   1623: }
                   1624: 
                   1625: 
                   1626: /*----------------------------------------------------------------------
                   1627:   Template_PrepareInstance
                   1628:   Allocate XTigerTemplate structure for instance and initialize template
                   1629:   url and template version.
                   1630:   ----------------------------------------------------------------------*/
                   1631: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
                   1632: {
                   1633: #ifdef TEMPLATES
                   1634:   XTigerTemplate t = GetXTigerTemplate(fileName);
                   1635:   if(!t)
                   1636:     t = NewXTigerTemplate(fileName);
                   1637:   t->state           = templInstance;
                   1638:   t->templateVersion = template_version;
                   1639:   t->base_uri        = template_url;
                   1640:   t->doc             = doc;
                   1641:   
                   1642: #endif /* TEMPLATES */
                   1643: }
                   1644: 
                   1645: 
                   1646: /*----------------------------------------------------------------------
                   1647:   SetDocumentAsXTigerTemplate
                   1648:   Set the document template structure as template.
                   1649:   ----------------------------------------------------------------------*/
                   1650: void SetDocumentAsXTigerTemplate(Document doc)
                   1651: {
                   1652: #ifdef TEMPLATES
                   1653:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1654:   if(t)
                   1655:     t->state |= templTemplate;
                   1656: #endif /* TEMPLATES */  
                   1657: }
                   1658: 
                   1659: /*----------------------------------------------------------------------
                   1660:   SetDocumentAsXTigerLibrary
                   1661:   Set the document template structure as template library.
                   1662:   ----------------------------------------------------------------------*/
                   1663: void SetDocumentAsXTigerLibrary(Document doc)
                   1664: {
                   1665: #ifdef TEMPLATES
                   1666:   XTigerTemplate t = GetXTigerDocTemplate(doc);
                   1667:   if(t)
                   1668:     t->state |= templLibrary;
                   1669: #endif /* TEMPLATES */  
                   1670: }
1.174     kia      1671: 
                   1672: 
                   1673: /*----------------------------------------------------------------------
                   1674:   TemplateCreateTextBox
                   1675:   Create a xt:use types="string" box around the selection.
                   1676:   ----------------------------------------------------------------------*/
                   1677: void TemplateCreateTextBox(Document doc, View view)
                   1678: {
                   1679: #ifdef TEMPLATES
                   1680:   Element     selElem;
                   1681:   ElementType selType;
                   1682:   int         firstChar, lastChar;
                   1683:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1684:   ElementType useType;
                   1685:   Element     use;
                   1686:   char        buffer[128];
                   1687:   
                   1688:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
                   1689:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING_LABEL);
                   1690: 
                   1691:   if (!TtaGetDocumentAccessMode(doc))
                   1692:     return;
                   1693:   
                   1694:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1695:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1696:     {
                   1697:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1698:       if(selElem)
                   1699:         {
                   1700:           selType =  TtaGetElementType(selElem);
                   1701:           if(!TtaIsLeaf(selType))
                   1702:             {
                   1703:               selElem = TtaGetFirstLeaf(selElem);
                   1704:               selType = TtaGetElementType(selElem);
                   1705:               firstChar = lastChar = 0;
                   1706:             }
                   1707:           
                   1708:           if(selType.ElTypeNum==1)
                   1709:             {
                   1710:               QueryStringFromUser(label, title, buffer, 127);
                   1711:               useType.ElSSchema = sstempl;
                   1712:               useType.ElTypeNum = Template_EL_useSimple;
                   1713: 
                   1714:               if(firstChar==0)
                   1715:                 {
                   1716:                   use = TtaNewElement(doc, useType);
                   1717:                   if(use)
                   1718:                     {
                   1719:                       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1720:                       TtaInsertSibling(use, selElem, FALSE, doc);
                   1721:                       TtaRegisterElementCreate(use, doc);
                   1722:                       TtaRegisterElementDelete (selElem, doc);
                   1723:                       TtaRemoveTree(selElem, doc);
                   1724:                       TtaInsertFirstChild(&selElem, use, doc);
                   1725:                       TtaRegisterElementDelete (selElem, doc);
                   1726:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1727:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);
                   1728:                       TtaCloseUndoSequence(doc);
                   1729:                       TtaSelectElement(doc, use);
                   1730:                     }
                   1731:                 }
                   1732:               else
                   1733:                 {
                   1734:                   GenerateInlineElement(Template_EL_useSimple, sstempl, 0, "", TRUE);
                   1735:                   TtaGiveFirstSelectedElement(doc, &use, &firstChar, &lastChar);
                   1736:                   selType =  TtaGetElementType(use);
                   1737:                   if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_useSimple)
                   1738:                     {
                   1739:                       SetAttributeStringValue(use, Template_ATTR_types, "string");
                   1740:                       SetAttributeStringValue(use, Template_ATTR_title, buffer);                      
                   1741:                     }
                   1742:                 }
                   1743:             }
                   1744:           
                   1745:         }
                   1746:     }
                   1747: #endif /* TEMPLATES */
                   1748: }
                   1749: 
                   1750: 
                   1751: /*----------------------------------------------------------------------
                   1752:   TemplateCreateFreeBox
                   1753:   Create a xt:bag types="string" box around the selection.
                   1754:   ----------------------------------------------------------------------*/
                   1755: void TemplateCreateFreeBox(Document doc, View view)
                   1756: {
                   1757: #ifdef TEMPLATES
                   1758:   Element     selElem, selElem2;
                   1759:   ElementType selType, selType2;
                   1760:   int         firstChar, lastChar, firstChar2, lastChar2;
                   1761:   SSchema     sstempl = TtaGetSSchema ("Template", doc);
                   1762: 
                   1763:   ElementType bagType;
                   1764:   Element     bag;
                   1765:   char        buffer[128];
                   1766: 
                   1767:   char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
                   1768:   char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY_LABEL);
                   1769: 
                   1770:   if (!TtaGetDocumentAccessMode(doc))
                   1771:     return;
                   1772:   
                   1773:   if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
                   1774:       IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
                   1775:     {
                   1776:       TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
                   1777:       TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
                   1778:       
                   1779:       if(selElem && selElem2)
                   1780:         {
                   1781:           selType =  TtaGetElementType(selElem);
                   1782:           selType2 =  TtaGetElementType(selElem2);
                   1783: 
                   1784: //          printf("Selection %d %p %d %d\n", selType.ElTypeNum, selElem, firstChar, lastChar);
                   1785: //          printf("          %d %p %d %d\n", selType2.ElTypeNum, selElem2, firstChar2, lastChar2);
                   1786:           
                   1787:           QueryStringFromUser(label, title, buffer, 127);
                   1788:           bagType.ElSSchema = sstempl;
                   1789:           bagType.ElTypeNum = Template_EL_bag;
                   1790:           
                   1791:           if(selElem==selElem2)
                   1792:             {
                   1793:               if(firstChar==0)
                   1794:                 {
                   1795:                   ThotBool        oldStructureChecking;
                   1796:                   DisplayMode     dispMode;
                   1797:                   dispMode = TtaGetDisplayMode (doc);
                   1798:                   if (dispMode == DisplayImmediately)
                   1799:                     TtaSetDisplayMode (doc, DeferredDisplay);
                   1800:                   oldStructureChecking = TtaGetStructureChecking (doc);
                   1801:                   TtaSetStructureChecking (FALSE, doc);
                   1802:                   
                   1803:                   // Only one element fully selected
                   1804:                   bag = TtaNewElement(doc, bagType);
                   1805:                   
                   1806:                   TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                   1807:                   TtaInsertSibling(bag, selElem, FALSE, doc);
                   1808:                   TtaRegisterElementCreate(bag, doc);
                   1809:                   TtaRegisterElementDelete (selElem, doc);
                   1810:                   TtaRemoveTree(selElem, doc);
                   1811:                   TtaInsertFirstChild(&selElem, bag, doc);
                   1812:                   TtaRegisterElementDelete (selElem, doc);
                   1813:                   SetAttributeStringValue(bag, Template_ATTR_types, "any");
                   1814:                   SetAttributeStringValue(bag, Template_ATTR_title, buffer);
                   1815:                   TtaCloseUndoSequence(doc);
                   1816:                   TtaSelectElement(doc, bag);
                   1817:                   
                   1818:                   TtaSetStructureChecking (oldStructureChecking, doc);
                   1819:                   TtaSetDisplayMode (doc, dispMode);
                   1820: 
                   1821:                 }
                   1822:             }
                   1823:         }
                   1824:     }
                   1825: #endif /* TEMPLATES */
                   1826: }
                   1827: 

Webmaster