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

1.1       cvs         1: /*
                      2:  *
1.100     vatton      3:  *  COPYRIGHT INRIA and W3C, 1996-2007
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.1       cvs        10:  *
                     11:  */
                     12: 
                     13: #define THOT_EXPORT extern
                     14: #include "amaya.h"
                     15: #include "document.h"
1.99      kia        16: #include "undo.h"
1.90      kia        17: #include "containers.h"
                     18: #include "Elemlist.h"
1.92      kia        19: #include "templates.h"
                     20: 
1.90      kia        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.122     kia        27: #include "templates_f.h"
1.89      kia        28: #include "templateUtils_f.h"
1.67      quint      29: #include "templateLoad_f.h"
                     30: #include "templateDeclarations_f.h"
1.76      vatton     31: #include "templateInstantiate_f.h"
1.145     kia        32: #include "Templatebuilder_f.h"
1.28      tollenae   33: #include "appdialogue_wx.h"
1.29      tollenae   34: #include "init_f.h"
1.46      vatton     35: #include "wxdialogapi_f.h"
1.60      francesc   36: #include "AHTURLTools_f.h"
1.52      vatton     37: #endif /* TEMPLATES */
1.1       cvs        38: 
1.87      kia        39: #include "fetchXMLname_f.h"
1.83      kia        40: #include "MENUconf.h"
                     41: 
                     42: /* Paths from which looking for templates.*/
                     43: static Prop_Templates_Path *TemplateRepositoryPaths;
                     44: 
1.87      kia        45: /*----------------------------------------------------------------------
                     46:   IsTemplateInstanceDocument: Test if a document is a template instance
                     47:   doc : Document to test
                     48:   return : TRUE if the document is a template instance
                     49:   ----------------------------------------------------------------------*/
1.106     vatton     50: ThotBool IsTemplateInstanceDocument(Document doc)
                     51: {
1.87      kia        52: #ifdef TEMPLATES
1.138     vatton     53:   return (DocumentMeta[doc] != NULL) && (DocumentMeta[doc]->template_url != NULL);
1.87      kia        54: #else  /* TEMPLATES */
1.88      cvs        55:   return FALSE;
1.87      kia        56: #endif /* TEMPLATES */
                     57: }
                     58: 
1.83      kia        59: /*----------------------------------------------------------------------
1.109     kia        60:   IsTemplateDocument: Test if a document is a template (not an instance)
                     61:   doc : Document to test
                     62:   return : TRUE if the document is an instance
                     63:   ----------------------------------------------------------------------*/
                     64: ThotBool IsTemplateDocument(Document doc)
                     65: {
                     66: #ifdef TEMPLATES
1.143     kia        67:   return (DocumentMeta[doc] != NULL && DocumentMeta[doc]->isTemplate);
1.109     kia        68: #else  /* TEMPLATES */
                     69:   return FALSE;
                     70: #endif /* TEMPLATES */
                     71: }
                     72: 
1.146     vatton     73: /*----------------------------------------------------------------------
                     74:   CheckPromptIndicator checks if the element is a prompt text unit
                     75:   ----------------------------------------------------------------------*/
                     76: ThotBool CheckPromptIndicator (Element el, Document doc)
                     77: {
                     78: #ifdef TEMPLATES
                     79:   ElementType     elType;
                     80:   Element         parent;
                     81:   AttributeType   attrType;
                     82:   Attribute       att;
                     83:   SSchema         templateSSchema;
                     84: 
                     85:   elType = TtaGetElementType (el);
                     86:   templateSSchema = TtaGetSSchema ("Template", doc);
                     87:        if (elType.ElTypeNum == HTML_EL_TEXT_UNIT)
                     88:     {
                     89:       parent = TtaGetParent (el);
                     90:       elType = TtaGetElementType (parent);
                     91:       while (parent && elType.ElSSchema != templateSSchema)
                     92:         {
                     93:           parent = TtaGetParent (parent);
                     94:           elType = TtaGetElementType (parent);
                     95:         }
                     96:       if (parent &&
                     97:           (elType.ElTypeNum == Template_EL_useEl ||
                     98:            elType.ElTypeNum == Template_EL_useSimple))
                     99:         {
                    100:           // there is a parent template use
                    101:           attrType.AttrSSchema = elType.ElSSchema;
                    102:           attrType.AttrTypeNum = Template_ATTR_prompt;
                    103:           att = TtaGetAttribute (parent, attrType);
                    104:           if (att)
                    105:             TtaSelectElement (doc, el);
                    106:           return TRUE;
                    107:         }
                    108:     }
                    109: #endif /* TEMPLATES */
                    110:   return FALSE;
                    111: }
                    112: 
                    113: /*----------------------------------------------------------------------
                    114:   RemovePromptIndicator removes the enclosing prompt indicator
                    115:   ----------------------------------------------------------------------*/
                    116: void RemovePromptIndicator (Element el, Document doc)
                    117: {
                    118: #ifdef TEMPLATES
                    119:   ElementType     elType;
                    120:   Element         parent;
                    121:   AttributeType   attrType;
                    122:   Attribute       att;
                    123:   SSchema         templateSSchema;
                    124: 
                    125:   elType = TtaGetElementType (el);
                    126:   templateSSchema = TtaGetSSchema ("Template", doc);
                    127:        if (elType.ElTypeNum == HTML_EL_TEXT_UNIT)
                    128:     {
                    129:       parent = TtaGetParent (el);
                    130:       elType = TtaGetElementType (parent);
                    131:       while (parent && elType.ElSSchema != templateSSchema)
                    132:         {
                    133:           parent = TtaGetParent (parent);
                    134:           elType = TtaGetElementType (parent);
                    135:         }
                    136:       if (parent &&
                    137:           (elType.ElTypeNum == Template_EL_useEl ||
                    138:            elType.ElTypeNum == Template_EL_useSimple))
                    139:         {
                    140:           // there is a parent template use
                    141:           attrType.AttrSSchema = elType.ElSSchema;
                    142:           attrType.AttrTypeNum = Template_ATTR_prompt;
                    143:           att = TtaGetAttribute (parent, attrType);
                    144:           if (att)
                    145:             TtaRemoveAttribute (parent, att, doc);
                    146:         }
                    147:     }
                    148: #endif /* TEMPLATES */
                    149: }
                    150: 
1.109     kia       151: 
                    152: /*----------------------------------------------------------------------
1.108     vatton    153:   AllocTemplateRepositoryListElement: allocates an element for the list
                    154:   of template repositories.
1.83      kia       155:   path : path of the new element
                    156:   return : address of the new element
                    157:   ----------------------------------------------------------------------*/
                    158: void* AllocTemplateRepositoryListElement (const char* path, void* prevElement)
                    159: {
1.129     vatton    160:   Prop_Templates_Path *element;
                    161: 
                    162:   element  = (Prop_Templates_Path*)TtaGetMemory (sizeof(Prop_Templates_Path));
                    163:   memset (element, 0, sizeof(Prop_Templates_Path));
                    164:   strncpy (element->Path, path, MAX_LENGTH - 1);
1.83      kia       165:   if (prevElement)
1.129     vatton    166:     {
                    167:       element->NextPath = ((Prop_Templates_Path*)prevElement)->NextPath;
                    168:       ((Prop_Templates_Path*)prevElement)->NextPath = element;
                    169:     }
1.83      kia       170:   return element;
                    171: }
                    172: 
                    173: 
                    174: /*----------------------------------------------------------------------
                    175:   FreeTemplateRepositoryList: Free the list of template repositories.
                    176:   list : address of the list (address of the first element).
                    177:   ----------------------------------------------------------------------*/
                    178: void FreeTemplateRepositoryList (void* list)
                    179: {
1.131     vatton    180:   Prop_Templates_Path **l = (Prop_Templates_Path**) list;
                    181:   Prop_Templates_Path  *element = *l;
                    182: 
1.83      kia       183:   l = NULL;
                    184:   while (element)
                    185:   {
                    186:     Prop_Templates_Path* next = element->NextPath;
1.131     vatton    187:     TtaFreeMemory (element);
1.83      kia       188:     element = next;
                    189:   }
                    190: }
                    191: 
                    192: /*----------------------------------------------------------------------
                    193:   CopyTemplateRepositoryList: Copy a list of template repositories.
                    194:   src : address of the list (address of the first element).
                    195:   dst : address where copy the list
                    196:   ----------------------------------------------------------------------*/
1.91      vatton    197: static void CopyTemplateRepositoryList (const Prop_Templates_Path** src,
                    198:                                         Prop_Templates_Path** dst)
1.83      kia       199: {
                    200:   Prop_Templates_Path *element=NULL, *current=NULL;
                    201:   
1.131     vatton    202:   if (*src)
1.83      kia       203:   {
                    204:     *dst = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    205:     (*dst)->NextPath = NULL;
                    206:     strcpy((*dst)->Path, (*src)->Path);
                    207:     
                    208:     element = (*src)->NextPath;
                    209:     current = *dst;
                    210:   }
                    211: 
1.106     vatton    212:   while (element)
                    213:     {
1.83      kia       214:     current->NextPath = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    215:     current = current->NextPath; 
                    216:     current->NextPath = NULL;
                    217:     strcpy(current->Path, element->Path);
                    218:     element = element->NextPath;
1.106     vatton    219:     }
1.83      kia       220: }
                    221: 
                    222: /*----------------------------------------------------------------------
                    223:   LoadTemplateRepositoryList: Load the list of template repositories.
                    224:   list   : address of the list (address of the first element).
                    225:   return : the number of readed repository paths.
                    226:   ----------------------------------------------------------------------*/
                    227: static int LoadTemplateRepositoryList (Prop_Templates_Path** list)
                    228: {
                    229:   Prop_Templates_Path *element, *current = NULL;
                    230:   char *path, *homePath;
                    231:   unsigned char *c;
                    232:   int nb = 0;
                    233:   FILE *file;
                    234:   
1.131     vatton    235:   //clean up the curent list
                    236:   FreeTemplateRepositoryList (list);
                    237: 
                    238:   // open the file
1.83      kia       239:   path = (char *) TtaGetMemory (MAX_LENGTH);
1.86      vatton    240:   homePath       = TtaGetEnvString ("APP_HOME");
1.106     vatton    241:   sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83      kia       242:   file = TtaReadOpen ((char *)path);
1.84      kia       243:   if (!file)
                    244:   {
                    245:     /* The config file dont exist, create it. */
                    246:     file = TtaWriteOpen ((char *)path);
1.122     kia       247:     fprintf (file, "http://www.w3.org/Amaya/Templates/cv.xtd\n");
1.144     vatton    248:     fprintf (file, "http://www.w3.org/Amaya/Templates/slides.xtd\n");
                    249:     fprintf (file, "http://www.w3.org/Amaya/Templates/ACM-Proc-Article.xtd\n");
1.84      kia       250:     TtaWriteClose (file);
                    251:     /* Retry to open it.*/
                    252:     file = TtaReadOpen ((char *)path);
                    253:   }
                    254:   
1.83      kia       255:   if (file)
1.129     vatton    256:     {
1.131     vatton    257:       // read the file
1.129     vatton    258:       c = (unsigned char*)path;
                    259:       *c = EOS;
                    260:       while (TtaReadByte (file, c))
                    261:         {
                    262:           if (*c == 13 || *c == EOL)
                    263:             *c = EOS;
                    264:           if (*c == EOS && c != (unsigned char*)path )
                    265:             {
                    266:               element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    267:               element->NextPath = NULL;
                    268:               strcpy (element->Path, path);
1.148     kia       269: 
1.129     vatton    270:               if (*list == NULL)
                    271:                 *list = element; 
                    272:               else
                    273:                 current->NextPath = element;
                    274:               current = element;
                    275:               nb++;
1.83      kia       276: 
1.129     vatton    277:               c = (unsigned char*) path;
                    278:               *c = EOS;
                    279:             }
                    280:           else
                    281:             c++;
                    282:         }
                    283:       if (c != (unsigned char*)path && *path != EOS)
                    284:         {
                    285:           element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    286:           *(c+1) = EOS;
                    287:           strcpy (element->Path, path);
                    288:           element->NextPath = NULL;
1.148     kia       289: 
1.129     vatton    290:           if (*list == NULL)
                    291:             *list = element; 
                    292:           else
                    293:             current->NextPath = element;
                    294:           nb++;
                    295:         }
                    296:       TtaReadClose (file);
1.83      kia       297:     }
                    298:   TtaFreeMemory(path);
                    299:   return nb;
                    300: }
                    301: 
                    302: /*----------------------------------------------------------------------
                    303:   SaveTemplateRepositoryList: Save the list of template repositories.
                    304:   list   : address of the list (address of the first element).
                    305:   ----------------------------------------------------------------------*/
                    306: static void SaveTemplateRepositoryList (const Prop_Templates_Path** list)
                    307: {
                    308:   const Prop_Templates_Path *element;
                    309:   char *path, *homePath;
                    310:   unsigned char *c;
                    311:   FILE *file;
                    312: 
                    313:   path = (char *) TtaGetMemory (MAX_LENGTH);
                    314:   homePath       = TtaGetEnvString ("APP_HOME");
1.106     vatton    315:   sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83      kia       316: 
                    317:   file = TtaWriteOpen ((char *)path);
                    318:   c = (unsigned char*)path;
                    319:   *c = EOS;
                    320:   if (file)
                    321:   {
                    322:     element = *list;
                    323:     while (element)
                    324:     {
                    325:       fprintf(file, "%s\n", element->Path);
                    326:       element = element->NextPath;
                    327:     }
                    328:     TtaWriteClose (file);
                    329:   }
                    330: }
                    331: 
                    332: /*----------------------------------------------------------------------
                    333:   GetTemplateRepositoryList: Get the list of template repositories from template environment.
                    334:   list : address of the list (address of the first element).
                    335:   ----------------------------------------------------------------------*/
                    336: void GetTemplateRepositoryList (void* list)
                    337: {
                    338:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                    339:   CopyTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths, l);
                    340: }
                    341: 
                    342: /*----------------------------------------------------------------------
                    343:   SetTemplateRepositoryList: Set the list of template repositories environment.
                    344:   list : address of the list (address of the first element).
                    345:   ----------------------------------------------------------------------*/
                    346: void SetTemplateRepositoryList (const void* list)
                    347: {
                    348:   const Prop_Templates_Path** l = (const Prop_Templates_Path**) list;
                    349:   CopyTemplateRepositoryList((const Prop_Templates_Path**)l, &TemplateRepositoryPaths);
                    350:   SaveTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths);
                    351: }
                    352: 
                    353: /*-----------------------------------------------------------------------
                    354:    InitTemplates
                    355:    Initializes the annotation library
                    356:   -----------------------------------------------------------------------*/
                    357: void InitTemplates ()
                    358: {
                    359:   TtaSetEnvBoolean ("SHOW_TEMPLATES", TRUE, FALSE);
1.131     vatton    360:   LoadTemplateRepositoryList (&TemplateRepositoryPaths);
1.83      kia       361: }
                    362: 
                    363: 
1.1       cvs       364: /*----------------------------------------------------------------------
1.51      francesc  365:   NewTemplate: Create the "new document from template" dialog
1.1       cvs       366:   ----------------------------------------------------------------------*/
1.18      cvs       367: void NewTemplate (Document doc, View view)
1.1       cvs       368: {
1.51      francesc  369: #ifdef TEMPLATES
1.76      vatton    370:   ThotBool     created;
1.28      tollenae  371: 
1.134     kia       372:   if (Templates_Map == NULL)
1.76      vatton    373:     InitializeTemplateEnvironment ();
                    374:   created = CreateNewTemplateDocDlgWX (BaseDialog + OpenTemplate,
1.61      francesc  375:                                       /*TtaGetViewFrame (doc, view)*/NULL, doc,
1.131     vatton    376:                                       TtaGetMessage (AMAYA, AM_NEW_TEMPLATE));
1.28      tollenae  377:   if (created)
1.25      vatton    378:     {
1.28      tollenae  379:       TtaSetDialoguePosition ();
                    380:       TtaShowDialogue (BaseDialog + OpenTemplate, TRUE);
1.25      vatton    381:     }
1.52      vatton    382: #endif /* TEMPLATES */
1.1       cvs       383: }
1.25      vatton    384: 
1.108     vatton    385: /*----------------------------------------------------------------------
                    386:   Load a template and create the instance file - update images and 
                    387:   stylesheets related to the template.
                    388:   ----------------------------------------------------------------------*/
                    389: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
                    390: {
                    391: #ifdef TEMPLATES
                    392: 
                    393:   char *s;
                    394:   ThotBool dontReplace = DontReplaceOldDoc;
                    395: 
                    396:   if (!IsW3Path (docname) && TtaFileExist (docname))
                    397:     {
                    398:       s = (char *)TtaGetMemory (strlen (docname) +
                    399:                                 strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
                    400:       sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
                    401:       InitConfirm (0, 0, s);
                    402:       TtaFreeMemory (s);
                    403:       if (!UserAnswer)
                    404:         return;
                    405:     }
                    406: 
                    407:   LoadTemplate (0, templatename);
                    408:   DontReplaceOldDoc = dontReplace;
1.144     vatton    409:   CreateInstance (templatename, docname, doc);
1.108     vatton    410: #endif /* TEMPLATES */
                    411: }
                    412: 
1.53      vatton    413: 
1.109     kia       414: 
1.52      vatton    415: /*----------------------------------------------------------------------
1.107     kia       416:   PreventReloadingTemplate
                    417:   Prevent reloading a template.
                    418:   You must call AllowReloadingTemplate when finish.
                    419:   Usefull for reload an instance without reloading the template.
                    420:   ----------------------------------------------------------------------*/
                    421: void PreventReloadingTemplate(char* template_url)
                    422: {
                    423: #ifdef TEMPLATES
1.134     kia       424:   XTigerTemplate t = GetXTigerTemplate (template_url);
1.112     vatton    425:   if (t)
1.107     kia       426:     t->users++;
                    427: #endif /* TEMPLATES */
                    428: }
                    429: 
                    430: /*----------------------------------------------------------------------
                    431:   AllowReloadingTemplate
                    432:   Allow reloading a template.
                    433:   You must call it after each PreventReloadingTemplate call.
                    434:   ----------------------------------------------------------------------*/
                    435: void AllowReloadingTemplate(char* template_url)
                    436: {
                    437: #ifdef TEMPLATES
1.134     kia       438:   XTigerTemplate t = GetXTigerTemplate (template_url);
1.112     vatton    439:   if (t)
1.107     kia       440:     t->users--;  
                    441: #endif /* TEMPLATES */  
                    442: }
                    443: 
                    444: 
1.137     vatton    445: /*----------------------------------------------------------------------
                    446:   ----------------------------------------------------------------------*/
1.134     kia       447: ThotBool isEOSorWhiteSpace (const char c)
                    448: {
1.137     vatton    449:   return c == SPACE || c == TAB || c ==  EOL || c ==__CR__ || c == EOS;
                    450: }
                    451: ThotBool isWhiteSpace (const char c)
                    452: {
                    453:   return c == SPACE || c == TAB || c == EOL || c ==__CR__;
1.134     kia       454: }
                    455: 
1.107     kia       456: /*----------------------------------------------------------------------
1.87      kia       457:   giveItems : Lists type items from string
                    458:   example : "one two three" is extracted to {one, two, three}
                    459:   note : item type are setted to SimpleTypeNat
                    460:   text : text from which list items
                    461:   size : size of text in characters
                    462:   items : address of exctracted item list
                    463:   nbitems : items number in items list
1.52      vatton    464:   ----------------------------------------------------------------------*/
1.76      vatton    465: void giveItems (char *text, int size, struct menuType **items, int *nbitems)
1.1       cvs       466: {
1.70      quint     467: #ifdef TEMPLATES
1.148     kia       468:   ThotBool         inElement = TRUE;
1.52      vatton    469:   struct menuType *menu;
                    470:   char            *iter;
1.148     kia       471:   char             temp[128];
1.52      vatton    472:   int              i;
1.148     kia       473:   int              labelSize;
1.28      tollenae  474: 
1.148     kia       475:   *nbitems = 1;
                    476:   for (i = 0; i < size; i++)
1.52      vatton    477:     {
                    478:       if (isEOSorWhiteSpace (text[i]))
                    479:         {
                    480:           if (inElement)
                    481:             inElement = FALSE;
                    482:         }
                    483:       else if (!inElement)
                    484:         {
                    485:           inElement = TRUE;
                    486:           (*nbitems)++;
                    487:         }
                    488:     }
1.51      francesc  489: 
1.148     kia       490:   menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
                    491:   iter = text;
                    492:   for (i = 0; i < *nbitems; i++)
                    493:     {
1.52      vatton    494:       labelSize = 0;
1.137     vatton    495:       while (isWhiteSpace (*iter))
1.52      vatton    496:         iter++;
1.137     vatton    497:       if (*iter != EOS)
                    498:         {
                    499:           while (!isEOSorWhiteSpace (*iter))
                    500:             {
                    501:               temp[labelSize++] = *iter;
                    502:               iter++;
                    503:             }
1.52      vatton    504: 
1.137     vatton    505:           temp[labelSize] = EOS;
                    506:           menu[i].label = (char *) TtaStrdup (temp);
                    507:           menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
1.52      vatton    508:         }
                    509:     }
1.137     vatton    510:   *items = menu;
1.70      quint     511: #endif /* TEMPLATES */
1.28      tollenae  512: }
1.37      tollenae  513: 
1.70      quint     514: #ifdef TEMPLATES
1.52      vatton    515: /*----------------------------------------------------------------------
                    516:   ----------------------------------------------------------------------*/
                    517: static char *createMenuString (const struct menuType* items, const int nbItems)
                    518: {
                    519:   char *result, *iter;
1.148     kia       520:   int   size = 0;
1.52      vatton    521:   int   i;
                    522: 
1.148     kia       523:   for (i=0; i < nbItems; i++)
                    524:     size += 2 + strlen (items[i].label);
1.52      vatton    525: 
1.148     kia       526:   result = (char *) TtaGetMemory (size);
                    527:   iter = result;
                    528:   for (i=0; i < nbItems; i++)
1.52      vatton    529:     {
                    530:       *iter = 'B';
                    531:       ++iter;
1.148     kia       532: 
1.52      vatton    533:       strcpy (iter, items[i].label);
                    534:       iter += strlen (items[i].label)+1;
                    535:     }
1.148     kia       536:   return result;
1.36      tollenae  537: }
1.71      quint     538: #endif /* TEMPLATES */
1.29      tollenae  539: 
1.71      quint     540: /*----------------------------------------------------------------------
                    541:   UseToBeCreated
                    542:   An new use element will be created by the user through some generic editing
                    543:   command
                    544:   -----------------------------------------------------------------------*/
                    545: ThotBool UseToBeCreated (NotifyElement *event)
                    546: {
                    547: #ifdef TEMPLATES
1.122     kia       548:   ElementType   parentType;
1.138     vatton    549:   SSchema       templateSSchema = TtaGetSSchema ("Template", event->document);  
1.130     vatton    550:   if (templateSSchema)
1.122     kia       551:   {
1.130     vatton    552:     parentType = TtaGetElementType (event->element);
                    553:     if (parentType.ElSSchema == templateSSchema &&
                    554:         parentType.ElTypeNum == Template_EL_repeat)
                    555:       return !Template_CanInsertRepeatChild (event->element);
                    556:     return TemplateElementWillBeCreated (event);
1.122     kia       557:   }
1.52      vatton    558: #endif /* TEMPLATES */
1.71      quint     559:   return FALSE; /* let Thot perform normal operation */
                    560: }
                    561: 
                    562: /*----------------------------------------------------------------------
                    563:   UseCreated
                    564:   A new "use" element has just been created by the user with a generic editing
                    565:   command.
                    566:   -----------------------------------------------------------------------*/
                    567: void UseCreated (NotifyElement *event)
                    568: {
                    569: #ifdef TEMPLATES
1.148     kia       570:   Document        doc = event->document;
                    571:   Element         el = event->element;
1.117     kia       572:   Element         parent;
                    573:   Element         first;
                    574:   ElementType     parentType;
                    575:   XTigerTemplate  t;
                    576:   SSchema         templateSSchema;
1.130     vatton    577:   char*           types, *text = NULL;
1.148     kia       578: 
1.112     vatton    579:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       580:     return;
                    581: 
1.72      quint     582:   if (TtaGetFirstChild (el))
                    583:     /* this Use element has already some content. It has already been
                    584:        instanciated */
                    585:     return;
1.117     kia       586: 
1.134     kia       587:   t = GetXTigerTemplate (DocumentMeta[doc]->template_url);
1.71      quint     588:   if (!t)
                    589:     return; // no template ?!?!
1.101     kia       590: 
1.138     vatton    591:   templateSSchema = TtaGetSSchema ("Template", doc);
1.117     kia       592:   parent = TtaGetParent(el);
                    593:   parentType = TtaGetElementType(parent);
1.148     kia       594: 
1.130     vatton    595:   if (parentType.ElSSchema == templateSSchema &&
                    596:       parentType.ElTypeNum == Template_EL_repeat)
1.117     kia       597:   {
1.130     vatton    598:     first = TtaGetFirstChild (parent);
                    599:     if (first)
1.117     kia       600:     {
1.130     vatton    601:       types = GetAttributeStringValueFromNum (first, Template_ATTR_types, NULL);
                    602:       if (types)
1.117     kia       603:       {
1.130     vatton    604:         SetAttributeStringValueWithUndo (el, Template_ATTR_types, types);
                    605:         text = GetAttributeStringValueFromNum (el, Template_ATTR_title, NULL);
                    606:         SetAttributeStringValueWithUndo (first, Template_ATTR_title, text);
                    607:         TtaFreeMemory (text);
                    608:         text = GetAttributeStringValueFromNum (el, Template_ATTR_currentType, NULL);
                    609:         SetAttributeStringValueWithUndo (first, Template_ATTR_currentType, text);
                    610:         TtaFreeMemory (text);
                    611:         TtaFreeMemory (types);
1.117     kia       612:       }
                    613:     }
                    614:   }
                    615: 
1.76      vatton    616:   InstantiateUse (t, el, doc, TRUE);
1.71      quint     617: #endif /* TEMPLATES */
                    618: }
1.29      tollenae  619: 
1.98      kia       620: /*----------------------------------------------------------------------
                    621:   Template_IncrementRepeatOccurNumber
                    622:   Increment the number of occurs of a xt:repeat
                    623:   @param el element (xt:repeat)
                    624:   ----------------------------------------------------------------------*/
                    625: void Template_IncrementRepeatOccurNumber(Element el)
                    626: {
                    627: #ifdef TEMPLATES
                    628:   char* current;
                    629:   char  newVal[8];
                    630:   int curVal;
1.148     kia       631: 
1.104     kia       632:   current = GetAttributeStringValueFromNum(el, Template_ATTR_currentOccurs, NULL);
1.112     vatton    633:   if (current)
1.104     kia       634:   {
                    635:     curVal = atoi(current);
                    636:     curVal++;
                    637:     TtaFreeMemory(current);
                    638:     sprintf(newVal, "%d", curVal);
                    639:     SetAttributeStringValue(el, Template_ATTR_currentOccurs, newVal);
                    640:   }
1.98      kia       641: #endif /* TEMPLATES */
                    642: }
                    643: 
                    644: /*----------------------------------------------------------------------
                    645:   Template_DecrementRepeatOccurNumber
                    646:   Decrement the number of occurs of a xt:repeat
                    647:   @param el element (xt:repeat)
                    648:   ----------------------------------------------------------------------*/
                    649: void Template_DecrementRepeatOccurNumber(Element el)
                    650: {
                    651: #ifdef TEMPLATES
                    652:   char* current;
                    653:   char  newVal[8];
                    654:   int curVal;
1.148     kia       655: 
1.104     kia       656:   current = GetAttributeStringValueFromNum(el, Template_ATTR_currentOccurs, NULL);
1.112     vatton    657:   if (current)
1.104     kia       658:   {
                    659:     curVal = atoi(current);
                    660:     curVal--;
                    661:     TtaFreeMemory(current);
                    662:     sprintf(newVal, "%d", curVal);
                    663:     SetAttributeStringValue(el, Template_ATTR_currentOccurs, newVal);
                    664:   }
1.98      kia       665: #endif /* TEMPLATES */
                    666: }
                    667: 
1.89      kia       668: 
1.98      kia       669: /*----------------------------------------------------------------------
                    670:   Template_CanInsertRepeatChild
                    671:   Test if a xt:repeat child can be inserted (number between params min and max).
                    672:   @param el element (xt:repeat) to test
                    673:   @return True if an element can be inserted.
                    674:   ----------------------------------------------------------------------*/
                    675: ThotBool Template_CanInsertRepeatChild(Element el)
                    676: {
                    677: #ifdef TEMPLATES
                    678:   char* max;
                    679:   char* current;
                    680:   int maxVal, curVal;
1.104     kia       681:   Element child;
1.148     kia       682: 
1.104     kia       683:   max = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
1.105     vatton    684:   if (max)
1.104     kia       685:   {
1.112     vatton    686:     if (!strcmp(max, "*"))
1.105     vatton    687:       {
                    688:         TtaFreeMemory(max);
                    689:         return TRUE;
                    690:       }
                    691:     maxVal = atoi (max);
                    692:     TtaFreeMemory (max);
1.104     kia       693: 
                    694:     current = GetAttributeStringValueFromNum(el, Template_ATTR_currentOccurs, NULL);
1.105     vatton    695:     if (current)
1.104     kia       696:     {
1.105     vatton    697:       curVal = atoi (current);
                    698:       TtaFreeMemory (current);
1.104     kia       699:     }
                    700:     else
                    701:     {
                    702:       curVal = 0;
1.105     vatton    703:       for (child = TtaGetFirstChild(el); child; TtaNextSibling(&child))
1.104     kia       704:       {
                    705:         curVal++;
                    706:       }
                    707:     }
1.148     kia       708: 
1.104     kia       709:     return curVal<maxVal;
                    710:   }
                    711:   else
1.98      kia       712:     return TRUE;
                    713: #endif /* TEMPLATES */
                    714:   return FALSE;
                    715: }
1.96      kia       716: 
1.89      kia       717: 
1.92      kia       718: #ifdef TEMPLATES
1.99      kia       719: /*----------------------------------------------------------------------
                    720:   QueryStringFromMenu
                    721:   Show a context menu to query a choice.
                    722:   @param items space-separated choice list string.
                    723:   @return The choosed item string or NULL if none.
                    724:   ----------------------------------------------------------------------*/
1.138     vatton    725: static char* QueryStringFromMenu (Document doc, char* items)
1.90      kia       726: {
                    727:   int nbitems, size;
                    728:   struct menuType *itemlist;
                    729:   char *menuString;
                    730:   char *result = NULL;
1.148     kia       731: 
1.138     vatton    732:   if (!TtaGetDocumentAccessMode (doc))
1.112     vatton    733:     return NULL;
                    734:   if (items == NULL)
1.110     kia       735:     return NULL;
1.138     vatton    736:   size = strlen (items);
                    737:   if (size == 0)
                    738:     return NULL;
1.90      kia       739:   giveItems (items, size, &itemlist, &nbitems);
                    740:   menuString = createMenuString (itemlist, nbitems);
                    741:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    742:                      nbitems, menuString , NULL, false, 'L');
                    743:   TtaFreeMemory (menuString);
                    744:   ReturnOption = -1;
                    745:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    746:   TtaWaitShowProcDialogue ();
                    747:   TtaDestroyDialogue (BaseDialog + OptionMenu);
1.148     kia       748: 
1.112     vatton    749:   if (ReturnOption!=-1)
1.90      kia       750:   {
                    751:     result = TtaStrdup(itemlist[ReturnOption].label);
                    752:   }
                    753:   
                    754:   TtaFreeMemory (itemlist);
                    755:   return result;
                    756: }
1.92      kia       757: #endif /* TEMPLATES */
1.90      kia       758: 
1.56      francesc  759: /*----------------------------------------------------------------------
1.138     vatton    760:   BagButtonClicked
                    761:   Called when a bag button is clicked.
                    762:   Can be called for useEl, useSimple or bag.
                    763:   If called for useEl or useSimple, the new element must be added after.
                    764:   If called for bag, the element must be added before all.
                    765:    
                    766:   Shows a menu with all the types that can be used in the bag.
                    767:   ----------------------------------------------------------------------*/
                    768: ThotBool BagButtonClicked (NotifyElement *event)
                    769: {
                    770: #ifdef TEMPLATES
                    771:   Document        doc = event->document;
                    772:   Element         el = event->element;
                    773:   ElementType     elType;
                    774:   XTigerTemplate  t;
                    775:   Declaration     decl;
                    776:   Element         bagEl = el;
                    777:   Element         firstEl;
                    778:   Element         newEl = NULL;
                    779:   View            view;
                    780:   SSchema         templateSSchema;
                    781:   char*           types;
                    782:   char*           listtypes = NULL;
                    783:   char*           result = NULL;
                    784:   ThotBool        oldStructureChecking;
                    785:   DisplayMode     dispMode;
                    786: 
                    787:   if (!TtaGetDocumentAccessMode(doc))
                    788:     return TRUE;
1.148     kia       789: 
1.138     vatton    790:   TtaGetActiveView (&doc, &view);
                    791:   if (view != 1)
                    792:     return FALSE; /* let Thot perform normal operation */
                    793: 
                    794:   TtaCancelSelection (doc);
                    795:   templateSSchema = TtaGetSSchema ("Template", doc);  
                    796:   t = GetXTigerTemplate (DocumentMeta[doc]->template_url);
                    797:   elType = TtaGetElementType (el);
                    798:   while (bagEl &&
                    799:          (elType.ElSSchema != templateSSchema ||
                    800:           elType.ElTypeNum != Template_EL_bag))
                    801:     {
                    802:       bagEl = TtaGetParent (bagEl);
                    803:       elType = TtaGetElementType (bagEl);
                    804:     }
                    805: 
                    806:   if (bagEl)
                    807:   {
                    808:     types = GetAttributeStringValueFromNum (bagEl, Template_ATTR_types, NULL);
                    809:     if (types)
                    810:       {
1.142     kia       811:         listtypes = Template_ExpandTypes (t, types, bagEl, FALSE);
1.138     vatton    812:         result = QueryStringFromMenu (doc, listtypes);
                    813:         TtaFreeMemory (listtypes);
                    814:         if (result)
                    815:           {
                    816:             decl = Template_GetDeclaration (t, result);
                    817:             if (decl)
                    818:               {
                    819:                 dispMode = TtaGetDisplayMode (doc);
                    820:                 if (dispMode == DisplayImmediately)
                    821:                   /* don't set NoComputedDisplay
                    822:                      -> it breaks down views formatting when Enter generates new elements  */
1.139     vatton    823:                   TtaSetDisplayMode (doc, DeferredDisplay);
1.138     vatton    824: 
                    825:                 /* Prepare insertion.*/          
                    826:                 oldStructureChecking = TtaGetStructureChecking (doc);
                    827:                 TtaSetStructureChecking (FALSE, doc);
                    828:                 TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    829: 
                    830:                 /* Insert */
                    831:                 if (el == bagEl)
                    832:                   {
                    833:                     el = TtaGetFirstChild (el);
                    834:                     TtaSelectElement (doc, el);
                    835:                     TtaInsertAnyElement (doc, TRUE);
                    836:                   }
                    837:                 else
                    838:                   {
                    839:                     TtaSelectElement (doc, el);
                    840:                     TtaInsertAnyElement (doc, FALSE);
                    841:                   }
                    842:                 newEl = Template_InsertBagChild (doc, bagEl, decl);
                    843: 
                    844:                 /* Finish insertion.*/
                    845:                 TtaCloseUndoSequence (doc);
                    846:                 TtaSetDocumentModified (doc);
                    847:                 TtaSetStructureChecking (oldStructureChecking, doc);
                    848:                 // restore the display
                    849:                 TtaSetDisplayMode (doc, dispMode);
                    850:                 firstEl = GetFirstEditableElement (newEl);
                    851:                 if (firstEl)
                    852:                   {
                    853:                     TtaSelectElement (doc, firstEl);
                    854:                     TtaSetStatusSelectedElement (doc, view, firstEl);
                    855:                   }
                    856:                 else
                    857:                   {
                    858:                     TtaSelectElement (doc, newEl);
                    859:                     TtaSetStatusSelectedElement (doc, view, newEl);
                    860:                   }
                    861:               }
                    862:           }
                    863:       }
                    864:     TtaFreeMemory (types);
                    865:     TtaFreeMemory (result);
                    866:   }
                    867: #endif /* TEMPLATES */
                    868:   return TRUE; /* don't let Thot perform normal operation */
                    869: }
                    870: 
                    871: /*----------------------------------------------------------------------
1.79      quint     872:   RepeatButtonClicked
1.89      kia       873:   Called when a repeat button is clicked.
                    874:   Can be called for useEl, useSimple or repeat.
                    875:   If called for useEl or useSimple, the new element must be added after.
                    876:   If called for repeat, the element must be added before all.
                    877:    
1.56      francesc  878:   Shows a menu with all the types that can be used in a use element.
                    879:   ----------------------------------------------------------------------*/
1.79      quint     880: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  881: {
                    882: #ifdef TEMPLATES
1.89      kia       883:   Document        doc = event->document;
                    884:   Element         el = event->element;
                    885:   ElementType     elType;
1.90      kia       886:   XTigerTemplate  t;
                    887:   Declaration     decl;
                    888:   Element         repeatEl = el;
                    889:   Element         firstEl;
                    890:   Element         newEl = NULL;
1.95      kia       891:   View            view;
1.124     kia       892:   char*           listtypes = NULL;
                    893:   char*           result = NULL;
1.138     vatton    894:   char*           types;
                    895:   ThotBool        oldStructureChecking;
                    896:   DisplayMode     dispMode;
1.104     kia       897: 
1.112     vatton    898:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia       899:     return TRUE;
1.148     kia       900: 
1.95      kia       901:   TtaGetActiveView (&doc, &view);
                    902:   if (view != 1)
                    903:     return FALSE; /* let Thot perform normal operation */
                    904: 
1.89      kia       905:   TtaCancelSelection(doc);
1.148     kia       906: 
1.134     kia       907:   t = GetXTigerTemplate (DocumentMeta[doc]->template_url);
1.89      kia       908:   elType = TtaGetElementType(el);
1.138     vatton    909:   while (elType.ElTypeNum != Template_EL_repeat)
1.89      kia       910:   {
1.90      kia       911:     repeatEl = TtaGetParent(repeatEl);
1.138     vatton    912:     if (repeatEl == NULL)
1.89      kia       913:       break;
1.90      kia       914:     elType = TtaGetElementType(repeatEl);
1.89      kia       915:   }
1.112     vatton    916:   if (repeatEl)
1.89      kia       917:   {
1.138     vatton    918:     if (Template_CanInsertRepeatChild (repeatEl))
1.90      kia       919:     {
1.138     vatton    920:       firstEl = TtaGetFirstChild (repeatEl);
                    921:       types = GetAttributeStringValueFromNum (firstEl, Template_ATTR_types, NULL);
1.112     vatton    922:       if (types)
1.90      kia       923:       {
1.142     kia       924:         listtypes = Template_ExpandTypes (t, types, NULL, FALSE);
1.138     vatton    925:         result = QueryStringFromMenu (doc, listtypes);
                    926:         TtaFreeMemory (listtypes);
1.112     vatton    927:         if (result)
1.98      kia       928:         {
1.138     vatton    929:           decl = Template_GetDeclaration (t, result);
1.112     vatton    930:           if (decl)
1.104     kia       931:           {
1.138     vatton    932:             dispMode = TtaGetDisplayMode (doc);
                    933:             if (dispMode == DisplayImmediately)
                    934:               /* don't set NoComputedDisplay
                    935:                  -> it breaks down views formatting when Enter generates new elements  */
1.139     vatton    936:               TtaSetDisplayMode (doc, DeferredDisplay);
1.138     vatton    937: 
1.104     kia       938:             /* Prepare insertion.*/          
                    939:             oldStructureChecking = TtaGetStructureChecking (doc);
                    940:             TtaSetStructureChecking (FALSE, doc);
1.138     vatton    941:             TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104     kia       942:             /* Insert. */
1.138     vatton    943:             if (el == repeatEl)
                    944:               newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, NULL);
1.104     kia       945:             else
1.138     vatton    946:               newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, el);
1.104     kia       947:               
                    948:             /* Finish insertion.*/
                    949:             TtaCloseUndoSequence(doc);
1.114     vatton    950:             TtaSetDocumentModified (doc);
1.104     kia       951:             TtaSetStructureChecking (oldStructureChecking, doc);
1.138     vatton    952: 
                    953:             // restore the display
                    954:             TtaSetDisplayMode (doc, dispMode);
                    955:             firstEl = GetFirstEditableElement (newEl);
1.112     vatton    956:             if (firstEl)
1.104     kia       957:             {
                    958:               TtaSelectElement (doc, firstEl);
1.138     vatton    959:               TtaSetStatusSelectedElement (doc, view, firstEl);
1.104     kia       960:             }
                    961:             else
                    962:             {
                    963:               TtaSelectElement (doc, newEl);
1.138     vatton    964:               TtaSetStatusSelectedElement (doc, view, newEl);
1.104     kia       965:             }
1.98      kia       966:           }
                    967:         }
1.90      kia       968:       }
1.104     kia       969:       TtaFreeMemory(types);
1.98      kia       970:       TtaFreeMemory(result);
                    971:     }
1.112     vatton    972:     else /* if (Template_CanInsertRepeatChild(repeatEl)) */
1.98      kia       973:     {
                    974:       TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.90      kia       975:     }
1.89      kia       976:   }
1.77      vatton    977:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc  978: #endif /* TEMPLATES */
1.94      kia       979:   return TRUE;
                    980: }
                    981: 
                    982: /*----------------------------------------------------------------------
                    983:   UseButtonClicked
                    984:   Shows a menu with all the types that can be used in a use element.
                    985:   ----------------------------------------------------------------------*/
                    986: ThotBool UseButtonClicked (NotifyElement *event)
                    987: {
                    988: #ifdef TEMPLATES
                    989:   Document        doc = event->document;
                    990:   Element         el = event->element;
1.99      kia       991:   Element         child;
1.94      kia       992:   ElementType     elType;
                    993:   XTigerTemplate  t;
                    994:   Declaration     decl;
                    995:   Element         firstEl;
                    996:   Element         newEl = NULL;
                    997:   char*           types;
                    998:   ThotBool        oldStructureChecking;
1.95      kia       999:   View            view;
1.133     vatton   1000:   char*           listtypes = NULL;
                   1001:   char*           result = NULL;
1.95      kia      1002: 
1.112     vatton   1003:   if (!TtaGetDocumentAccessMode(doc))
1.110     kia      1004:     return TRUE;
                   1005:     
1.95      kia      1006:   TtaGetActiveView (&doc, &view);
                   1007:   if (view != 1)
                   1008:     return FALSE; /* let Thot perform normal operation */
1.148     kia      1009: 
1.94      kia      1010:   TtaCancelSelection(doc);
                   1011:   
1.134     kia      1012:   t = GetXTigerTemplate (DocumentMeta[doc]->template_url);
1.94      kia      1013:   if (!t)
                   1014:     return FALSE; /* let Thot perform normal operation */
                   1015:   elType = TtaGetElementType(el);
                   1016: 
                   1017:   firstEl = TtaGetFirstChild(el);
1.112     vatton   1018:   if (firstEl)
1.94      kia      1019:   {
                   1020:     RepeatButtonClicked(event);
                   1021:   }
                   1022:   else
                   1023:   {
1.104     kia      1024:     types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.112     vatton   1025:     if (types)
1.94      kia      1026:     {
1.142     kia      1027:       listtypes = Template_ExpandTypes(t, types, NULL, FALSE);
1.104     kia      1028:       result = QueryStringFromMenu(doc, listtypes);
1.112     vatton   1029:       if (result)
1.94      kia      1030:       {
1.104     kia      1031:         decl = Template_GetDeclaration(t, result);
1.112     vatton   1032:         if (decl)
1.99      kia      1033:         {
1.104     kia      1034:           /* Prepare insertion.*/
                   1035:           oldStructureChecking = TtaGetStructureChecking (doc);
                   1036:           TtaSetStructureChecking (FALSE, doc);
1.138     vatton   1037:           TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104     kia      1038:           
                   1039:           /* Insert */
                   1040:           newEl = Template_InsertUseChildren(doc, el, decl);
                   1041:           for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
                   1042:           {
                   1043:             TtaRegisterElementCreate(child, doc);
                   1044:           }
                   1045:           
                   1046:           TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
                   1047:           TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
                   1048:           
1.117     kia      1049:           /* xt:currentType attribute.*/
                   1050:           SetAttributeStringValueWithUndo(el, Template_ATTR_currentType, result);
                   1051:           
1.104     kia      1052:           /* Finish insertion. */
                   1053:           TtaCloseUndoSequence(doc);
1.114     vatton   1054:           TtaSetDocumentModified (doc);
1.104     kia      1055:           TtaSetStructureChecking (oldStructureChecking, doc);
                   1056:           
                   1057:           firstEl = GetFirstEditableElement(newEl);
1.112     vatton   1058:           if (firstEl)
1.104     kia      1059:           {
                   1060:             TtaSelectElement (doc, firstEl);
                   1061:             TtaSetStatusSelectedElement(doc, view, firstEl);
                   1062:           }
                   1063:           else
                   1064:           {
                   1065:             TtaSelectElement (doc, newEl);
                   1066:             TtaSetStatusSelectedElement(doc, view, newEl);
                   1067:           }
1.98      kia      1068:         }
1.94      kia      1069:       }
                   1070:     }
1.104     kia      1071:     TtaFreeMemory(types);
1.94      kia      1072:     TtaFreeMemory(listtypes);
                   1073:     TtaFreeMemory(result);
                   1074:   }
1.148     kia      1075: 
1.94      kia      1076:   return TRUE;
                   1077: #endif /* TEMPLATES */
1.56      francesc 1078:        return TRUE;
                   1079: }
1.64      francesc 1080: 
1.89      kia      1081: 
1.103     kia      1082: /*----------------------------------------------------------------------
                   1083:   UseSimpleButtonClicked
                   1084:   ----------------------------------------------------------------------*/
                   1085: ThotBool UseSimpleButtonClicked (NotifyElement *event)
                   1086: {
                   1087: #ifdef TEMPLATES
1.112     vatton   1088:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1089:     return TRUE;
                   1090: 
1.138     vatton   1091:   ElementType parentType = TtaGetElementType (TtaGetParent( event->element));
1.112     vatton   1092:   if (parentType.ElTypeNum == Template_EL_repeat)
1.138     vatton   1093:     return RepeatButtonClicked (event);
                   1094:   else if (parentType.ElTypeNum == Template_EL_bag)
                   1095:     return BagButtonClicked (event);
1.103     kia      1096: #endif /* TEMPLATES */  
                   1097:   return FALSE;
                   1098: }
1.94      kia      1099: 
                   1100: /*----------------------------------------------------------------------
                   1101:   OptionButtonClicked
                   1102:   ----------------------------------------------------------------------*/
                   1103: ThotBool OptionButtonClicked (NotifyElement *event)
                   1104: {
                   1105: #ifdef TEMPLATES
1.145     kia      1106:   Element         useEl, contentEl, next;
                   1107:   ElementType     useType, optType;
1.94      kia      1108:   Document        doc;
                   1109:   XTigerTemplate  t;
                   1110:   View            view;
                   1111: 
1.112     vatton   1112:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1113:     return TRUE;
                   1114: 
1.94      kia      1115:   TtaGetActiveView (&doc, &view);
                   1116:   if (view != 1)
                   1117:     return FALSE; /* let Thot perform normal operation */
1.110     kia      1118: 
1.94      kia      1119:   doc = event->document;
1.145     kia      1120:   useEl = TtaGetFirstChild (event->element);
                   1121:   if (!useEl)
1.94      kia      1122:     return FALSE; /* let Thot perform normal operation */
1.145     kia      1123:   useType = TtaGetElementType (useEl);
                   1124:   optType = TtaGetElementType (event->element);
                   1125:   if ((useType.ElTypeNum != Template_EL_useEl &&
1.148     kia      1126:       useType.ElTypeNum != Template_EL_useSimple) ||
                   1127:       useType.ElSSchema != optType.ElSSchema)
1.94      kia      1128:     return FALSE;
                   1129: 
1.145     kia      1130:   TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
1.148     kia      1131: 
1.94      kia      1132:   TtaCancelSelection (doc);
1.148     kia      1133: 
1.145     kia      1134:   contentEl = TtaGetFirstChild (useEl);
                   1135:   if (!contentEl)
1.94      kia      1136:     /* the "use" element is empty. Instantiate it */
                   1137:     {
1.134     kia      1138:       t = GetXTigerTemplate (DocumentMeta[doc]->template_url);
1.94      kia      1139:       if (!t)
                   1140:         return FALSE; // no template ?!?!
1.145     kia      1141:       InstantiateUse (t, useEl, doc, TRUE);
1.94      kia      1142:     }
                   1143:   else
                   1144:     /* remove the content of the "use" element */
                   1145:     {
                   1146:       do
                   1147:         {
1.145     kia      1148:           next = contentEl;
1.94      kia      1149:           TtaNextSibling (&next);
1.145     kia      1150:           TtaRegisterElementDelete(contentEl, doc);
                   1151:           TtaDeleteTree (contentEl, doc);
                   1152:           contentEl = next;
1.94      kia      1153:         }
                   1154:       while (next);
1.145     kia      1155:       if (NeedAMenu (useEl, doc))
                   1156:         {
                   1157:           TtaChangeTypeOfElement (useEl, doc, Template_EL_useEl);
                   1158:           TtaRegisterElementTypeChange(useEl, Template_EL_useSimple, doc);
                   1159:         }
1.94      kia      1160:     }
                   1161:   TtaSelectElement (doc, event->element);
1.145     kia      1162:   TtaCloseUndoSequence(doc);
1.94      kia      1163:   return TRUE; /* don't let Thot perform normal operation */
                   1164: #endif /* TEMPLATES */
                   1165:   return TRUE;
                   1166: }
                   1167: 
1.111     vatton   1168: /*----------------------------------------------------------------------
                   1169:   CheckTemplate checks if the template of the instance is loaded
1.126     vatton   1170:   Return TRUE if the template is loaded
1.111     vatton   1171:   ----------------------------------------------------------------------*/
                   1172: void CheckTemplate (Document doc)
                   1173: {
                   1174: #ifdef TEMPLATES
1.144     vatton   1175:   Element    root;
                   1176: 
                   1177:   if (DocumentMeta[doc] && DocumentMeta[doc]->template_url)
1.111     vatton   1178:     {
1.144     vatton   1179:       XTigerTemplate   t;
                   1180: 
                   1181:       root = TtaGetRootElement (doc);
                   1182:       TtaSetAccessRight (root, ReadOnly, doc);
                   1183:       t = GetXTigerTemplate (DocumentMeta[doc]->template_url);
                   1184:       if (t == NULL)
                   1185:         {
                   1186:           // the template cannot be loaded
                   1187:           InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                   1188:           TtaSetDocumentAccessMode (doc, 0); // document readonly
                   1189:         }
                   1190:       else
                   1191:         {
                   1192:           // fix all access rights in the instance
                   1193:           Template_FixAccessRight (t, root, doc);
                   1194:           TtaUpdateAccessRightInViews (doc, root);
                   1195:         }
1.111     vatton   1196:     }
                   1197: #endif /* TEMPLATES */
                   1198: }
1.94      kia      1199: 
1.66      vatton   1200: /*----------------------------------------------------------------------
1.108     vatton   1201:   OpeningInstance checks if it is a template instance needs.
                   1202:   If it's an instance and the template is not loaded, load it into a
                   1203:   temporary file
1.66      vatton   1204:   ----------------------------------------------------------------------*/
1.76      vatton   1205: void OpeningInstance (char *fileName, Document doc)
1.65      francesc 1206: {
                   1207: #ifdef TEMPLATES
1.76      vatton   1208:   XTigerTemplate   t;
1.103     kia      1209:   char            *content, *ptr, *begin;
1.76      vatton   1210:   gzFile           stream;
                   1211:   char             buffer[2000];
1.77      vatton   1212:   int              res;
1.65      francesc 1213: 
1.76      vatton   1214:   stream = TtaGZOpen (fileName);
                   1215:   if (stream != 0)
1.65      francesc 1216:     {
1.76      vatton   1217:       res = gzread (stream, buffer, 1999);
                   1218:       if (res >= 0)
1.65      francesc 1219:         {
1.81      vatton   1220:           buffer[res] = EOS;
1.103     kia      1221:           begin = strstr (buffer, "<?xtiger");
                   1222:           
1.112     vatton   1223:           if (begin)
1.103     kia      1224:           {
                   1225:             // Search for template version
                   1226:             ptr = strstr (begin, "templateVersion");
                   1227:             if (ptr)
                   1228:               ptr = strstr (ptr, "=");
                   1229:             if (ptr)
                   1230:               ptr = strstr (ptr, "\"");
                   1231:             if (ptr)
                   1232:               {
                   1233:                 // template URI
                   1234:                 content = &ptr[1];
                   1235:                 ptr = strstr (content, "\"");
                   1236:               }
                   1237:             if (ptr)
                   1238:               {
                   1239:                 *ptr = EOS;
                   1240:                 //Get now the template URI
                   1241:                 DocumentMeta[doc]->template_version = TtaStrdup (content);
                   1242:                 *ptr = '"';
                   1243:               }
                   1244:            
                   1245:             // Search for template uri
                   1246:             ptr = strstr (begin, "template");
                   1247:             if (ptr && ptr[8] != 'V')
                   1248:               ptr = strstr (ptr, "=");
                   1249:             if (ptr)
                   1250:               ptr = strstr (ptr, "\"");
                   1251:             if (ptr)
                   1252:               {
                   1253:                 // template URI
                   1254:                 content = &ptr[1];
                   1255:                 ptr = strstr (content, "\"");
                   1256:               }
                   1257:             if (ptr)
                   1258:               {
                   1259:                 *ptr = EOS;
                   1260:                 //Get now the template URI
                   1261:                 DocumentMeta[doc]->template_url = TtaStrdup (content);
1.134     kia      1262:                 if (Templates_Map == NULL)
1.103     kia      1263:                   InitializeTemplateEnvironment ();
1.134     kia      1264:                 t = GetXTigerTemplate (content);
1.103     kia      1265:                 if (!t)
                   1266:                   {
1.108     vatton   1267:                     LoadTemplate (doc, content);
1.134     kia      1268:                     t = GetXTigerTemplate(content);
1.103     kia      1269:                   }
                   1270:                 AddUser (t);
                   1271:                 *ptr = '"';
                   1272:               }
                   1273:           }
1.65      francesc 1274:         }
                   1275:     }
1.76      vatton   1276:   TtaGZClose (stream);
1.65      francesc 1277: #endif /* TEMPLATES */
                   1278: }
                   1279: 
1.64      francesc 1280: /*----------------------------------------------------------------------
1.65      francesc 1281:   ClosingInstance
1.64      francesc 1282:   Callback called before closing a document. Checks for unused templates.
                   1283:   ----------------------------------------------------------------------*/
1.65      francesc 1284: ThotBool ClosingInstance(NotifyDialog* dialog)
1.64      francesc 1285: {
1.65      francesc 1286: #ifdef TEMPLATES
                   1287:   //If it is a template all has been already freed
1.76      vatton   1288:   if (DocumentMeta[dialog->document] == NULL)
                   1289:     return FALSE;
1.65      francesc 1290: 
                   1291:   char *turl = DocumentMeta[dialog->document]->template_url;
1.73      vatton   1292:   if (turl)
1.104     kia      1293:   {
1.134     kia      1294:     XTigerTemplate t = GetXTigerTemplate(turl);
1.104     kia      1295:     if (t)
                   1296:       RemoveUser (t);
                   1297:     TtaFreeMemory (turl);
                   1298:     DocumentMeta[dialog->document]->template_url = NULL;
                   1299:   }
                   1300:   
1.112     vatton   1301:   if (DocumentMeta[dialog->document]->template_version)
1.104     kia      1302:   {
                   1303:     TtaFreeMemory(DocumentMeta[dialog->document]->template_version);
                   1304:     DocumentMeta[dialog->document]->template_version = NULL;
                   1305:   }
1.65      francesc 1306: #endif /* TEMPLATES */
                   1307:   return FALSE;
1.64      francesc 1308: }
1.87      kia      1309: 
                   1310: 
                   1311: /*----------------------------------------------------------------------
1.120     kia      1312:   IsTemplateElement
1.138     vatton   1313:   Test if an element is a template element.
1.87      kia      1314:   ----------------------------------------------------------------------*/
1.140     vatton   1315: ThotBool IsTemplateElement (Element elem)
1.87      kia      1316: {
                   1317: #ifdef TEMPLATES
1.138     vatton   1318:   ElementType     elType;
                   1319: 
                   1320:   elType = TtaGetElementType(elem);
                   1321:   if (elType.ElSSchema)
                   1322:     return (strcmp(TtaGetSSchemaName(elType.ElSSchema) , "Template") != 0);
                   1323: #endif /* TEMPLATES */
1.87      kia      1324:   return FALSE;
                   1325: }
                   1326: 
                   1327: 
                   1328: /*----------------------------------------------------------------------
                   1329:   GetFirstTemplateParentElement
1.138     vatton   1330:   Return the first element which has "Template" as schema name or null.
1.87      kia      1331:   ----------------------------------------------------------------------*/
                   1332: Element GetFirstTemplateParentElement(Element elem)
                   1333: {
                   1334: #ifdef TEMPLATES
1.138     vatton   1335:   ElementType     elType;
                   1336: 
                   1337:   elem = TtaGetParent (elem);
                   1338:   elType = TtaGetElementType(elem);
                   1339:   while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
                   1340: {
                   1341:     elem = TtaGetParent (elem);
                   1342:     elType = TtaGetElementType(elem);
1.87      kia      1343:   }
                   1344:   return elem;
                   1345: #else
                   1346:   return NULL;
                   1347: #endif /* TEMPLATES */
                   1348: }
1.101     kia      1349: 
1.103     kia      1350: 
1.101     kia      1351: /*----------------------------------------------------------------------
1.102     vatton   1352:   TemplateElementWillBeCreated
1.101     kia      1353:   Processed when an element will be created in a template context.
                   1354:   ----------------------------------------------------------------------*/
1.102     vatton   1355: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101     kia      1356: {
1.103     kia      1357: #ifdef TEMPLATES
                   1358:   ElementType elType = event->elementType;
                   1359:   Element     parent = event->element;
                   1360:   ElementType parentType = TtaGetElementType(parent);
1.113     kia      1361:   Element     ancestor;
                   1362:   ElementType ancestorType;
                   1363:   SSchema     templateSSchema;
                   1364:   char*       types;
                   1365:   ThotBool    b;
1.101     kia      1366: 
1.115     kia      1367:   if(event->info==1)
                   1368:     return FALSE;
                   1369: 
1.112     vatton   1370:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1371:     return TRUE;
                   1372: 
1.138     vatton   1373:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.102     vatton   1374:   if (templateSSchema == NULL)
                   1375:     return FALSE; // let Thot do the job
1.115     kia      1376: 
1.113     kia      1377:   // Fisrt, test if in a xt:bag or in a base-element xt:use
1.147     vatton   1378:   if(parentType.ElSSchema == templateSSchema)
1.113     kia      1379:     ancestor = parent;
                   1380:   else
1.147     vatton   1381:     ancestor = GetFirstTemplateParentElement (parent);
1.113     kia      1382: 
1.147     vatton   1383:   if (ancestor)
1.113     kia      1384:   {
                   1385:     ancestorType = TtaGetElementType(ancestor);
                   1386: 
1.147     vatton   1387:     if (ancestorType.ElTypeNum == Template_EL_bag)
1.113     kia      1388:     {
1.147     vatton   1389:       // only check the use child
                   1390:       if (ancestor != parent)
                   1391:         return  FALSE; // let Thot do the job
                   1392:       if (elType.ElSSchema == templateSSchema &&
                   1393:           (elType.ElTypeNum == Template_EL_useSimple ||
                   1394:            elType.ElTypeNum == Template_EL_useEl))
1.116     kia      1395:         return FALSE;
1.147     vatton   1396:       return !Template_CanInsertElementInBagElement (event->document, elType, ancestor);      
1.113     kia      1397:     }
1.147     vatton   1398:     else if(ancestorType.ElTypeNum == Template_EL_useSimple ||
                   1399:             ancestorType.ElTypeNum == Template_EL_useEl)
1.113     kia      1400:     {
1.147     vatton   1401:       // only check the bag child @@@ will be check exclude/include later
1.121     vatton   1402:       if (ancestor != parent)
                   1403:         return  FALSE; // let Thot do the job
1.113     kia      1404:       types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
1.128     kia      1405:       b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position);
                   1406:       TtaFreeMemory(types);
1.115     kia      1407:       return !b;
1.113     kia      1408:       
                   1409:     }
                   1410:   }
1.115     kia      1411:   
1.147     vatton   1412:   if (elType.ElSSchema == templateSSchema && elType.ElTypeNum == Template_EL_TEXT_UNIT)
1.115     kia      1413:   {
                   1414:     return FALSE;
                   1415:   }
                   1416:   
1.113     kia      1417:   // Can not insert.
                   1418:   return TRUE;
1.101     kia      1419: #endif /* TEMPLATES*/
1.102     vatton   1420:   return FALSE;
1.101     kia      1421: }
                   1422: 
                   1423: /*----------------------------------------------------------------------
                   1424:   TemplateElementWillBeDeleted
                   1425:   Processed when an element will be deleted in a template context.
                   1426:   ----------------------------------------------------------------------*/
                   1427: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
                   1428: {
1.107     kia      1429: #ifdef TEMPLATES
                   1430:   Document       doc = event->document;
                   1431:   Element        elem = event->element;
                   1432:   Element        xtElem, parent;
1.109     kia      1433:   Element        sibling;
1.117     kia      1434:   ElementType    xtType, elType;
1.107     kia      1435:   char*          type;
                   1436:   Declaration    dec;
1.115     kia      1437:   SSchema        templateSSchema;
1.107     kia      1438:   XTigerTemplate t;
                   1439: 
1.115     kia      1440:   if(event->info==1)
                   1441:     return FALSE;
                   1442: 
1.112     vatton   1443:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1444:     return TRUE;
                   1445: 
1.138     vatton   1446:   templateSSchema = TtaGetSSchema ("Template", event->document);
1.107     kia      1447:   if (templateSSchema == NULL)
                   1448:     return FALSE; // let Thot do the job
1.122     kia      1449: 
1.107     kia      1450:   xtElem = GetFirstTemplateParentElement(elem);
1.112     vatton   1451:   if (xtElem)
1.107     kia      1452:   {
                   1453:     xtType = TtaGetElementType(xtElem);
1.117     kia      1454:     
1.134     kia      1455:     t = GetXTigerTemplate(DocumentMeta[doc]->template_url);
1.109     kia      1456: 
1.112     vatton   1457:     if (xtType.ElTypeNum==Template_EL_bag)
1.117     kia      1458:     {
                   1459:       elType = TtaGetElementType(elem);
                   1460:       if(elType.ElSSchema==templateSSchema &&
                   1461:         (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
                   1462:       {
                   1463:         // Remove element manually.
                   1464:         TtaOpenUndoSequence(doc, elem, elem, 0, 0);
                   1465:         TtaRegisterElementDelete(elem, doc);
                   1466:         TtaDeleteTree(elem, doc);
                   1467:         TtaCloseUndoSequence(doc);
                   1468:         return TRUE;
                   1469:       }
                   1470:       else
                   1471:         return FALSE; // xt:bag always allow remove children.
                   1472:     }
1.112     vatton   1473:     else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107     kia      1474:     {
1.109     kia      1475:       parent = TtaGetParent(elem);
1.117     kia      1476:       if (xtElem!=parent)
                   1477:       {
                   1478:         type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
                   1479:         dec = Template_GetDeclaration(t, type);
                   1480:         TtaFreeMemory(type);
                   1481:         
                   1482:         if (dec && dec->nature == XmlElementNat)
                   1483:           return FALSE; // Can remove element only if in xt:use current type is base language element. 
                   1484:         else
                   1485:           return TRUE;
1.107     kia      1486:       }
1.109     kia      1487:     }
1.112     vatton   1488:     else if (xtType.ElTypeNum==Template_EL_repeat)
1.109     kia      1489:     {
                   1490:       sibling = TtaGetSuccessor(elem);
                   1491:       TtaRegisterElementDelete(elem, doc);
                   1492:       TtaDeleteTree(elem, doc);
1.123     kia      1493:       Template_DecrementRepeatOccurNumber(xtElem);
1.109     kia      1494:       InstantiateRepeat(t, xtElem, doc, TRUE);
                   1495:       TtaSelectElement(doc, sibling);
                   1496:       return TRUE;
1.107     kia      1497:     }
                   1498:   }
1.109     kia      1499:   
                   1500:   //TODO Test if current element is use or repeat.
                   1501:   // Because if an element is delete and it is the unique child of its parent,
                   1502:   // the parent intends to destroy itself. 
                   1503:   
1.107     kia      1504:   return TRUE;
                   1505: #else /* TEMPLATES */
1.101     kia      1506:   return FALSE;
1.107     kia      1507: #endif /* TEMPLATES */
1.101     kia      1508: }
                   1509: 
1.109     kia      1510: /*----------------------------------------------------------------------
                   1511:   CurrentTypeWillBeExported
                   1512:   Check if the xt:currentType attribute can be exported
                   1513:   ----------------------------------------------------------------------*/
                   1514: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
                   1515: {
                   1516: #ifdef TEMPLATES
1.110     kia      1517: 
1.112     vatton   1518:   if (!TtaGetDocumentAccessMode(event->document))
1.110     kia      1519:     return TRUE;
                   1520: 
1.112     vatton   1521:   if (IsTemplateDocument(event->document))
1.109     kia      1522:     return TRUE;
                   1523: #endif /* TEMPLATES */
                   1524:   return FALSE;
                   1525: }
1.127     kia      1526: 
                   1527: /*----------------------------------------------------------------------
                   1528:   TemplateAttrInMenu
                   1529:   Called by Thot when building the Attributes menu for template elements.
                   1530:   ----------------------------------------------------------------------*/
                   1531: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
                   1532: {
                   1533: #ifdef TEMPLATES
                   1534:   // Prevent from showing attributes for template instance but not templates.
                   1535:   if(IsTemplateInstanceDocument(event->document))
                   1536:     return TRUE;
                   1537:   else
                   1538: #endif /* TEMPLATES */
                   1539:     return FALSE;
                   1540: }

Webmaster