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

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

Webmaster