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

1.1       cvs         1: /*
                      2:  *
1.27      vatton      3:  *  COPYRIGHT INRIA and W3C, 1996-2005
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.52      vatton     16: 
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.89      kia        25: #include "templateUtils_f.h"
1.52      vatton     26: 
1.69      quint      27: #include "mydictionary_f.h"
1.67      quint      28: #include "templateLoad_f.h"
                     29: #include "templateDeclarations_f.h"
1.76      vatton     30: #include "templateInstantiate_f.h"
1.28      tollenae   31: #include "appdialogue_wx.h"
1.29      tollenae   32: #include "init_f.h"
1.46      vatton     33: #include "wxdialogapi_f.h"
1.60      francesc   34: #include "AHTURLTools_f.h"
                     35: 
1.52      vatton     36: #endif /* TEMPLATES */
1.1       cvs        37: 
1.87      kia        38: 
                     39: #include "fetchXMLname_f.h"
1.83      kia        40: #include "MENUconf.h"
                     41: 
1.87      kia        42: 
1.83      kia        43: /* Paths from which looking for templates.*/
                     44: static Prop_Templates_Path *TemplateRepositoryPaths;
                     45: 
1.87      kia        46: 
                     47: /*----------------------------------------------------------------------
                     48:   IsTemplateInstanceDocument: Test if a document is a template instance
                     49:   doc : Document to test
                     50:   return : TRUE if the document is a template instance
                     51:   ----------------------------------------------------------------------*/
                     52: ThotBool IsTemplateInstanceDocument(Document doc){
                     53: #ifdef TEMPLATES
                     54:   return (DocumentMeta[doc]!=NULL) && (DocumentMeta[doc]->template_url!=NULL);
                     55: #else  /* TEMPLATES */
1.88      cvs        56:   return FALSE;
1.87      kia        57: #endif /* TEMPLATES */
                     58: }
                     59: 
1.83      kia        60: /*----------------------------------------------------------------------
                     61:   AllocTemplateRepositoryListElement: alloc an element for the list of template repositories.
                     62:   path : path of the new element
                     63:   return : address of the new element
                     64:   ----------------------------------------------------------------------*/
                     65: void* AllocTemplateRepositoryListElement (const char* path, void* prevElement)
                     66: {
                     67:   Prop_Templates_Path *element = (Prop_Templates_Path*)TtaGetMemory (sizeof(Prop_Templates_Path));
                     68:   element->NextPath = NULL;
                     69:   strcpy (element->Path, path);
                     70:   if (prevElement)
                     71:   {
                     72:     element->NextPath = ((Prop_Templates_Path*)prevElement)->NextPath;
                     73:     ((Prop_Templates_Path*)prevElement)->NextPath = element;
                     74:   }
                     75:   return element;
                     76: }
                     77: 
                     78: 
                     79: /*----------------------------------------------------------------------
                     80:   FreeTemplateRepositoryList: Free the list of template repositories.
                     81:   list : address of the list (address of the first element).
                     82:   ----------------------------------------------------------------------*/
                     83: void FreeTemplateRepositoryList (void* list)
                     84: {
                     85:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                     86:   
                     87:   Prop_Templates_Path* element = *l;
                     88:   l = NULL;
                     89:   while (element)
                     90:   {
                     91:     Prop_Templates_Path* next = element->NextPath;
                     92:     TtaFreeMemory(element);
                     93:     element = next;
                     94:   }
                     95: }
                     96: 
                     97: /*----------------------------------------------------------------------
                     98:   CopyTemplateRepositoryList: Copy a list of template repositories.
                     99:   src : address of the list (address of the first element).
                    100:   dst : address where copy the list
                    101:   ----------------------------------------------------------------------*/
1.91      vatton    102: static void CopyTemplateRepositoryList (const Prop_Templates_Path** src,
                    103:                                         Prop_Templates_Path** dst)
1.83      kia       104: {
                    105:   Prop_Templates_Path *element=NULL, *current=NULL;
                    106:   
                    107:   if(*src!=NULL)
                    108:   {
                    109:     *dst = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    110:     (*dst)->NextPath = NULL;
                    111:     strcpy((*dst)->Path, (*src)->Path);
                    112:     
                    113:     element = (*src)->NextPath;
                    114:     current = *dst;
                    115:   }
                    116: 
                    117:   while (element){
                    118:     current->NextPath = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    119:     current = current->NextPath; 
                    120:     current->NextPath = NULL;
                    121:     strcpy(current->Path, element->Path);
                    122:     element = element->NextPath;
                    123:   }
                    124: }
                    125: 
                    126: /*----------------------------------------------------------------------
                    127:   LoadTemplateRepositoryList: Load the list of template repositories.
                    128:   list   : address of the list (address of the first element).
                    129:   return : the number of readed repository paths.
                    130:   ----------------------------------------------------------------------*/
                    131: static int LoadTemplateRepositoryList (Prop_Templates_Path** list)
                    132: {
                    133:   Prop_Templates_Path *element, *current = NULL;
                    134:   char *path, *homePath;
                    135:   unsigned char *c;
                    136:   int nb = 0;
                    137:   FILE *file;
                    138:   
                    139:   FreeTemplateRepositoryList(list);
                    140:   
                    141:   path = (char *) TtaGetMemory (MAX_LENGTH);
1.86      vatton    142:   homePath       = TtaGetEnvString ("APP_HOME");
1.83      kia       143:   sprintf (path, "%s%ctemplate-repositories.dat", homePath, DIR_SEP);
                    144:   
                    145:   file = TtaReadOpen ((char *)path);
1.84      kia       146:   if (!file)
                    147:   {
                    148:     /* The config file dont exist, create it. */
                    149:     file = TtaWriteOpen ((char *)path);
                    150:     fprintf(file, "%s%ctemplates%cen\n", homePath, DIR_SEP, DIR_SEP);
                    151:     TtaWriteClose (file);
                    152:     /* Retry to open it.*/
                    153:     file = TtaReadOpen ((char *)path);
                    154:   }
                    155:   
1.83      kia       156:   if (file)
                    157:   {
1.84      kia       158:     c = (unsigned char*)path;
                    159:     *c = EOS;
1.83      kia       160:     while (TtaReadByte (file, c)){
                    161:       if (*c==13 || *c==EOL)
                    162:         *c = EOS;
                    163:       if (*c==EOS && c!=(unsigned char*)path )
                    164:       {
                    165:         element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    166:         element->NextPath = NULL;
                    167:         strcpy (element->Path, path);
                    168:         
                    169:         if (*list == NULL)
                    170:           *list = element; 
                    171:         else
                    172:           current->NextPath = element;
                    173:         current = element;
                    174:         nb++;
                    175: 
                    176:         c = (unsigned char*) path;
                    177:         *c = EOS;
                    178:       }
                    179:       else
                    180:         c++;
                    181:     }
                    182:     if (c!=(unsigned char*)path && *path!=EOS)
                    183:     {
                    184:       element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    185:       *(c+1) = EOS;
                    186:       strcpy (element->Path, path);
                    187:       element->NextPath = NULL;
                    188:       
                    189:       if (*list == NULL)
                    190:         *list = element; 
                    191:       else
                    192:         current->NextPath = element;
                    193:       nb++;
                    194:     }
                    195:     TtaReadClose (file);
                    196:   }
                    197:   TtaFreeMemory(path);
                    198:   return nb;
                    199: }
                    200: 
                    201: /*----------------------------------------------------------------------
                    202:   SaveTemplateRepositoryList: Save the list of template repositories.
                    203:   list   : address of the list (address of the first element).
                    204:   ----------------------------------------------------------------------*/
                    205: static void SaveTemplateRepositoryList (const Prop_Templates_Path** list)
                    206: {
                    207:   const Prop_Templates_Path *element;
                    208:   char *path, *homePath;
                    209:   unsigned char *c;
                    210:   FILE *file;
                    211: 
                    212:   path = (char *) TtaGetMemory (MAX_LENGTH);
                    213:   homePath       = TtaGetEnvString ("APP_HOME");
                    214:   sprintf (path, "%s%ctemplate-repositories.dat", homePath, DIR_SEP);
                    215: 
                    216:   file = TtaWriteOpen ((char *)path);
                    217:   c = (unsigned char*)path;
                    218:   *c = EOS;
                    219:   if (file)
                    220:   {
                    221:     element = *list;
                    222:     while (element)
                    223:     {
                    224:       fprintf(file, "%s\n", element->Path);
                    225:       element = element->NextPath;
                    226:     }
                    227:     TtaWriteClose (file);
                    228:   }
                    229: }
                    230: 
                    231: /*----------------------------------------------------------------------
                    232:   GetTemplateRepositoryList: Get the list of template repositories from template environment.
                    233:   list : address of the list (address of the first element).
                    234:   ----------------------------------------------------------------------*/
                    235: void GetTemplateRepositoryList (void* list)
                    236: {
                    237:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                    238:   CopyTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths, l);
                    239: }
                    240: 
                    241: /*----------------------------------------------------------------------
                    242:   SetTemplateRepositoryList: Set the list of template repositories environment.
                    243:   list : address of the list (address of the first element).
                    244:   ----------------------------------------------------------------------*/
                    245: void SetTemplateRepositoryList (const void* list)
                    246: {
                    247:   const Prop_Templates_Path** l = (const Prop_Templates_Path**) list;
                    248:   CopyTemplateRepositoryList((const Prop_Templates_Path**)l, &TemplateRepositoryPaths);
                    249:   SaveTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths);
                    250: }
                    251: 
                    252: /*-----------------------------------------------------------------------
                    253:    InitTemplates
                    254:    Initializes the annotation library
                    255:   -----------------------------------------------------------------------*/
                    256: void InitTemplates ()
                    257: {
                    258:   TtaSetEnvBoolean ("SHOW_TEMPLATES", TRUE, FALSE);
                    259:   LoadTemplateRepositoryList(&TemplateRepositoryPaths);
                    260: }
                    261: 
                    262: 
                    263: 
1.87      kia       264: 
                    265: 
                    266: 
1.1       cvs       267: /*----------------------------------------------------------------------
1.51      francesc  268:   NewTemplate: Create the "new document from template" dialog
1.1       cvs       269:   ----------------------------------------------------------------------*/
1.18      cvs       270: void NewTemplate (Document doc, View view)
1.1       cvs       271: {
1.51      francesc  272: #ifdef TEMPLATES
1.76      vatton    273:   char        *templateDir = TtaGetEnvString ("TEMPLATES_DIRECTORY");
                    274:   ThotBool     created;
1.28      tollenae  275: 
1.76      vatton    276:   if (Templates_Dic == NULL)
                    277:     InitializeTemplateEnvironment ();
                    278:   created = CreateNewTemplateDocDlgWX (BaseDialog + OpenTemplate,
1.61      francesc  279:                                       /*TtaGetViewFrame (doc, view)*/NULL, doc,
1.52      vatton    280:                                       TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),templateDir);
1.51      francesc  281:   
1.28      tollenae  282:   if (created)
1.25      vatton    283:     {
1.28      tollenae  284:       TtaSetDialoguePosition ();
                    285:       TtaShowDialogue (BaseDialog + OpenTemplate, TRUE);
1.25      vatton    286:     }
1.51      francesc  287: 
1.52      vatton    288: #endif /* TEMPLATES */
1.1       cvs       289: }
1.25      vatton    290: 
1.53      vatton    291: /*----------------------------------------------------------------------
                    292:   Load a template and create the instance file - update images and 
                    293:   stylesheets related to the template.
                    294:   ----------------------------------------------------------------------*/
1.61      francesc  295: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
1.53      vatton    296: {
                    297: #ifdef TEMPLATES
                    298: 
1.60      francesc  299:   char *s;
1.65      francesc  300:   ThotBool dontReplace = DontReplaceOldDoc;
1.60      francesc  301: 
                    302:   if (!IsW3Path (docname) && TtaFileExist (docname))
                    303:     {
                    304:       s = (char *)TtaGetMemory (strlen (docname) +
                    305:                                 strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
                    306:       sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
                    307:       InitConfirm (0, 0, s);
1.61      francesc  308:       TtaFreeMemory (s);      
1.62      francesc  309:       if (!UserAnswer)
                    310:         return;
                    311:     }    
                    312: 
1.76      vatton    313:   LoadTemplate (0, templatename);
1.65      francesc  314:   DontReplaceOldDoc = dontReplace;
1.76      vatton    315:   CreateInstance (templatename, docname);
1.62      francesc  316:   
1.53      vatton    317: #endif /* TEMPLATES */
                    318: }
                    319: 
1.52      vatton    320: /*----------------------------------------------------------------------
1.87      kia       321:   giveItems : Lists type items from string
                    322:   example : "one two three" is extracted to {one, two, three}
                    323:   note : item type are setted to SimpleTypeNat
                    324:   text : text from which list items
                    325:   size : size of text in characters
                    326:   items : address of exctracted item list
                    327:   nbitems : items number in items list
1.52      vatton    328:   ----------------------------------------------------------------------*/
1.76      vatton    329: void giveItems (char *text, int size, struct menuType **items, int *nbitems)
1.1       cvs       330: {
1.70      quint     331: #ifdef TEMPLATES
1.52      vatton    332:        ThotBool         inElement = TRUE;
                    333:   struct menuType *menu;
                    334:   char            *iter;
                    335:        char             temp[128];
                    336:   int              i;
                    337:        int              labelSize;
1.28      tollenae  338: 
1.52      vatton    339:        *nbitems = 1;
                    340:        for (i = 0; i < size; i++)
                    341:     {
                    342:       if (isEOSorWhiteSpace (text[i]))
                    343:         {
                    344:           if (inElement)
                    345:             inElement = FALSE;
                    346:         }
                    347:       else if (!inElement)
                    348:         {
                    349:           inElement = TRUE;
                    350:           (*nbitems)++;
                    351:         }
                    352:     }
1.51      francesc  353: 
1.76      vatton    354:        menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
1.52      vatton    355:        iter = text;
                    356:        for (i = 0; i < *nbitems; i++)
                    357:     {          
                    358:       labelSize = 0;
                    359:       while (isEOSorWhiteSpace (*iter))
                    360:         iter++;
                    361: 
                    362:       while (!isEOSorWhiteSpace (*iter))
                    363:         {
                    364:           temp[labelSize++] = *iter;
                    365:           iter++;
                    366:         }
                    367: 
                    368:       temp[labelSize] = EOS;
1.76      vatton    369:       menu[i].label = (char *) TtaStrdup (temp);
1.68      quint     370:       menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
1.52      vatton    371:       *items = menu;
                    372:     }
1.70      quint     373: #endif /* TEMPLATES */
1.28      tollenae  374: }
1.37      tollenae  375: 
1.70      quint     376: #ifdef TEMPLATES
1.52      vatton    377: /*----------------------------------------------------------------------
                    378:   ----------------------------------------------------------------------*/
                    379: static char *createMenuString (const struct menuType* items, const int nbItems)
                    380: {
                    381:   char *result, *iter;
                    382:        int   size = 0;
                    383:   int   i;
                    384: 
                    385:        for (i=0; i < nbItems; i++)
                    386:                size += 2 + strlen (items[i].label);
                    387: 
1.76      vatton    388:        result = (char *) TtaGetMemory (size);
1.52      vatton    389:        iter = result;
                    390:        for (i=0; i < nbItems; i++)
                    391:     {
                    392:       *iter = 'B';
                    393:       ++iter;
1.51      francesc  394:                
1.52      vatton    395:       strcpy (iter, items[i].label);
                    396:       iter += strlen (items[i].label)+1;
                    397:     }
1.51      francesc  398:        return result;
1.36      tollenae  399: }
1.71      quint     400: #endif /* TEMPLATES */
1.29      tollenae  401: 
1.71      quint     402: /*----------------------------------------------------------------------
                    403:   UseToBeCreated
                    404:   An new use element will be created by the user through some generic editing
                    405:   command
                    406:   -----------------------------------------------------------------------*/
                    407: ThotBool UseToBeCreated (NotifyElement *event)
                    408: {
                    409: #ifdef TEMPLATES
1.75      quint     410:   Element        el;
1.72      quint     411:        Document       doc;
                    412: 
                    413:   el = event->element;
                    414:   doc = event->document;
                    415:   /* is there a limit to the number of elements in the xt:repeat ? */
1.71      quint     416:   /* @@@@@ */
1.52      vatton    417: #endif /* TEMPLATES */
1.71      quint     418:   return FALSE; /* let Thot perform normal operation */
                    419: }
                    420: 
                    421: /*----------------------------------------------------------------------
                    422:   UseCreated
                    423:   A new "use" element has just been created by the user with a generic editing
                    424:   command.
                    425:   -----------------------------------------------------------------------*/
                    426: void UseCreated (NotifyElement *event)
                    427: {
                    428: #ifdef TEMPLATES
                    429:        Document         doc;
                    430:        Element          el;
                    431:   XTigerTemplate   t;
                    432: 
                    433:        doc = event->document;
1.72      quint     434:   el = event->element;
                    435:   if (TtaGetFirstChild (el))
                    436:     /* this Use element has already some content. It has already been
                    437:        instanciated */
                    438:     return;
1.87      kia       439:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.71      quint     440:   if (!t)
                    441:     return; // no template ?!?!
1.76      vatton    442:   InstantiateUse (t, el, doc, TRUE);
1.71      quint     443: #endif /* TEMPLATES */
                    444: }
1.29      tollenae  445: 
1.89      kia       446: 
                    447: /*----------------------------------------------------------------------
                    448:   Template_InsertUseChildren
                    449:   Insert children to a xt:use
                    450:   The dec parameter must be valid and will not be verified. It must be a
                    451:     direct child element (for union elements).
                    452:   @param el element (xt:use) in which insert a new element
                    453:   @param dec Template declaration of the element to insert
                    454:   @return The inserted element
                    455:   ----------------------------------------------------------------------*/
                    456: Element Template_InsertUseChildren(Document doc, Element el, Declaration dec)
                    457: {
                    458: #ifdef TEMPLATES
                    459:   Element comp;
                    460: 
                    461:   switch (dec->nature)
                    462:   {
                    463:     case SimpleTypeNat :
                    464:       /* @@@@@ */
                    465:       break;
                    466:     case XmlElementNat :
                    467:       /* @@@@@ */
                    468:       break;
                    469:     case ComponentNat :
                    470:       /* copy element dec->componentType.content */
                    471:       comp = TtaCopyTree (dec->componentType.content, doc, doc, el);
                    472:       TtaInsertFirstChild (&comp, el, doc);
                    473:       return comp;
                    474:       /* @@@@@ */
                    475:       break;
                    476:     case UnionNat :
                    477:       /* @@@@@ */
                    478:       break;
                    479:     default :
                    480:       //Impossible
                    481:       break;   
                    482:   }
1.93    ! cvs       483: #else /* TEMPLATES */
        !           484:   return NULL;
1.89      kia       485: #endif /* TEMPLATES */
                    486: }
                    487: 
                    488: 
                    489: /*----------------------------------------------------------------------
                    490:   Template_InsertRepeatChildAfter
                    491:   Insert a child to a xt:repeat
                    492:   The decl parameter must be valid and will not be verified. It must be a
                    493:     direct child element or the "use in the use" for union elements.
                    494:   @param el element (xt:repeat) in which insert a new element
                    495:   @param decl Template declaration of the element to insert
                    496:   @param elPrev Element (xt:use) after which insert the new elem, NULL if first.
                    497:   @return The inserted element 
                    498:   ----------------------------------------------------------------------*/
                    499: Element Template_InsertRepeatChildAfter(Document doc, Element el, Declaration decl, Element elPrev)
                    500: {
                    501: #ifdef TEMPLATES
                    502:   Element useFirst; /* First xt:use of the repeat.*/
                    503:   Element use;      /* xt:use to insert.*/
                    504:   ElementType useType;  /* type of xt:use.*/
                    505:   Attribute useTypesAttr; /* xt:types attribute.*/
                    506:   AttributeType useTypesAttrType; /* Type of xt:types attribute.*/
                    507:   
                    508:   /* Copy xt:use with xt:types param */
                    509:   useFirst = TtaGetFirstChild(el);
                    510:   useType = TtaGetElementType(useFirst);
                    511:   use = TtaCopyElement(useFirst, doc, doc, el);
                    512: 
                    513:   Template_InsertUseChildren(doc, use, decl);
                    514: 
                    515:   /* insert it */
                    516:   if(elPrev)
                    517:   {
                    518:     TtaInsertSibling(use, elPrev, FALSE, doc);
                    519:   }
                    520:   else
                    521:   {
                    522:     TtaInsertSibling(use, useFirst, TRUE, doc);
                    523:   }
                    524:   return use;
                    525:   
1.93    ! cvs       526: #else /* TEMPLATES */
        !           527:   return NULL;
1.89      kia       528: #endif /* TEMPLATES */
                    529: }
                    530: 
                    531: /*----------------------------------------------------------------------
                    532:   Template_InsertRepeatChild
                    533:   Insert a child to a xt:repeat
                    534:   The decl parameter must be valid and will not be verified. It must be a
                    535:     direct child element or the "use in the use" for union elements.
                    536:   @param el element (repeat) in which insert a new element
                    537:   @param decl Template declaration of the element to insert
                    538:   @param pos Position of insertion (0 before all, 1 after first ... -1 after all)
                    539:   @return The inserted element
                    540:   ----------------------------------------------------------------------*/
                    541: Element Template_InsertRepeatChild(Document doc, Element el, Declaration decl, int pos)
                    542: {
                    543:   if(pos==0)
                    544:   {
                    545:     return Template_InsertRepeatChildAfter(doc, el, decl, NULL);
                    546:   }
                    547:   else if(pos==-1)
                    548:   {
                    549:     return Template_InsertRepeatChildAfter(doc, el, decl, TtaGetLastChild(el));
                    550:   }
                    551:   else
                    552:   {
                    553:     Element elem = TtaGetFirstChild(el);
                    554:     pos--;
                    555:     while(pos>0)
                    556:     {
                    557:       TtaNextSibling(&elem);
                    558:       pos--;
                    559:     }
                    560:     return Template_InsertRepeatChildAfter(doc, el, decl, elem);
                    561:   }
                    562: }
                    563: 
                    564: 
1.46      vatton    565: /*----------------------------------------------------------------------
1.79      quint     566:   UseButtonClicked
1.51      francesc  567:   Shows a menu with all the types that can be used in a use element.
1.46      vatton    568:   ----------------------------------------------------------------------*/
1.79      quint     569: ThotBool UseButtonClicked (NotifyElement *event)
1.39      tollenae  570: {
1.43      tollenae  571: #ifdef TEMPLATES
1.70      quint     572:        Document         doc;
                    573:        Element          el, comp;
1.76      vatton    574:        ElementType      elType;
                    575:        Attribute        att;
                    576:        AttributeType    attributeType;
1.67      quint     577:   XTigerTemplate   t;
                    578:   Declaration      dec;
1.70      quint     579:   Record           rec, first;
1.52      vatton    580:        int              nbitems, size;
                    581:        struct menuType *items;
                    582:   char            *types, *menuString;
1.77      vatton    583:   View            view;
                    584: 
1.89      kia       585: /*** EK ***/
                    586: /* Si l'englobant est un use alors menu sélection.
                    587:  * Sinon c'est un repeat.*/
                    588: 
1.77      vatton    589:   TtaGetActiveView (&doc, &view);
                    590:   if (view != 1)
                    591:     return FALSE; /* let Thot perform normal operation */
1.70      quint     592: 
                    593:        doc = event->document;
1.87      kia       594:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.67      quint     595:   if (!t)
1.77      vatton    596:     return FALSE; /* let Thot perform normal operation */
1.67      quint     597: 
1.79      quint     598:        el = event->element;
                    599:   if (TtaGetFirstChild (el))
                    600:     /* this Use element has already some content. Do not do anything */
                    601:     return FALSE; /* let Thot perform normal operation */
                    602: 
                    603:        elType = TtaGetElementType (el);
1.76      vatton    604:   // give the list of possible items
                    605:        attributeType.AttrSSchema = elType.ElSSchema;
                    606:        attributeType.AttrTypeNum = Template_ATTR_types;
                    607:        att = TtaGetAttribute (el, attributeType);
                    608:        size = TtaGetTextAttributeLength (att);
1.52      vatton    609:        types = (char *) TtaGetMemory (size+1); 
1.76      vatton    610:        TtaGiveTextAttributeValue (att, types, &size);
1.52      vatton    611:        giveItems (types, size, &items, &nbitems);
1.70      quint     612:        TtaFreeMemory (types);
                    613: 
1.67      quint     614:   if (nbitems == 1)
                    615:     {
1.76      vatton    616:       dec = GetDeclaration (t, items[0].label);
1.69      quint     617:       /* if it's a union, display the menu of this union */
1.70      quint     618:       if (dec)
1.76      vatton    619:         switch (dec->nature)
1.70      quint     620:           {
                    621:           case SimpleTypeNat :
                    622:             nbitems = 0;
                    623:             break;
                    624:           case XmlElementNat :
                    625:             nbitems = 0;
                    626:             break;
                    627:           case ComponentNat :
                    628:             nbitems = 0;
                    629:             break;
                    630:           case UnionNat :
                    631:             first = dec->unionType.include->first;
                    632:             rec = first;
                    633:             /* count the number of elements in the union */
                    634:             nbitems = 0;
                    635:             while (rec)
                    636:               {
                    637:                 nbitems++;
                    638:                 rec = rec->next;
                    639:               }
                    640:             if (nbitems > 0)
                    641:               {
1.76      vatton    642:                 items = (menuType*) TtaGetMemory (sizeof (struct menuType)* nbitems);
1.70      quint     643:                 rec = first;
                    644:                 nbitems = 0;
                    645:                 while (rec)
                    646:                   {
1.76      vatton    647:                     items[nbitems].label = (char *) TtaStrdup (rec->key);
1.70      quint     648:                     items[nbitems].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
                    649:                     nbitems++;
                    650:                     rec = rec->next;
                    651:                   }
                    652:               }
                    653:             break;
                    654:           default :
                    655:             //Impossible
                    656:             break;   
                    657:           }
                    658:     }
                    659:   if (nbitems > 0)
                    660:     {
1.80      vatton    661:       TtaCancelSelection (doc);
1.70      quint     662:       menuString = createMenuString (items, nbitems);
                    663:       TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1),
                    664:                          NULL, nbitems, menuString , NULL, false, 'L');
                    665:       TtaFreeMemory (menuString);
1.74      vatton    666:       ReturnOption = -1; // no selection yet
1.70      quint     667:       TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
1.76      vatton    668:       TtaWaitShowProcDialogue ();
1.70      quint     669:       TtaDestroyDialogue (BaseDialog + OptionMenu);
1.75      quint     670:       if (ReturnOption != -1)
1.76      vatton    671:         dec = GetDeclaration (t, items[ReturnOption].label);
1.70      quint     672:       TtaFreeMemory (items);
1.75      quint     673:       if (ReturnOption == -1)
                    674:         return FALSE;
1.70      quint     675:       if (dec)
1.67      quint     676:         {
1.89      kia       677:           Template_InsertUseChildren(doc, el, dec);
1.67      quint     678:         }
                    679:     }
1.80      vatton    680:   TtaSelectElement (doc, el);
1.77      vatton    681:   return TRUE;
1.63      vatton    682: #endif /* TEMPLATES */
                    683:        return TRUE;
                    684: }
1.42      tollenae  685: 
1.63      vatton    686: /*----------------------------------------------------------------------
1.79      quint     687:   OptionButtonClicked
1.63      vatton    688:   ----------------------------------------------------------------------*/
1.79      quint     689: ThotBool OptionButtonClicked (NotifyElement *event)
1.63      vatton    690: {
                    691: #ifdef TEMPLATES
1.75      quint     692:   Element         child, grandChild, next;
                    693:   ElementType     elType, elType1;
                    694:   Document        doc;
                    695:   XTigerTemplate  t;
1.77      vatton    696:   View            view;
1.75      quint     697: 
1.77      vatton    698:   TtaGetActiveView (&doc, &view);
                    699:   if (view != 1)
                    700:     return FALSE; /* let Thot perform normal operation */
1.75      quint     701:   doc = event->document;
                    702:   child = TtaGetFirstChild (event->element);
                    703:   if (!child)
1.77      vatton    704:     return FALSE; /* let Thot perform normal operation */
1.75      quint     705:   elType = TtaGetElementType (child);
                    706:   elType1 = TtaGetElementType (event->element);
1.76      vatton    707:   if ((elType.ElTypeNum != Template_EL_useEl &&
                    708:        elType.ElTypeNum != Template_EL_useSimple) ||
1.75      quint     709:       elType.ElSSchema != elType1.ElSSchema)
                    710:     return FALSE;
1.80      vatton    711: 
                    712:   TtaCancelSelection (doc);
1.75      quint     713:   grandChild = TtaGetFirstChild (child);
                    714:   if (!grandChild)
1.76      vatton    715:     /* the "use" element is empty. Instantiate it */
1.75      quint     716:     {
1.87      kia       717:       t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.75      quint     718:       if (!t)
                    719:         return FALSE; // no template ?!?!
1.76      vatton    720:       InstantiateUse (t, child, doc, TRUE);
1.75      quint     721:     }
                    722:   else
                    723:     /* remove the content of the "use" element */
                    724:     {
                    725:       do
                    726:         {
                    727:           next = grandChild;
                    728:           TtaNextSibling (&next);
                    729:           TtaDeleteTree (grandChild, doc);
                    730:           grandChild = next;
                    731:         }
                    732:       while (next);
                    733:     }
1.80      vatton    734:   TtaSelectElement (doc, event->element);
1.77      vatton    735:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc  736: #endif /* TEMPLATES */
1.52      vatton    737:        return TRUE;
1.42      tollenae  738: }
1.51      francesc  739: 
1.92      kia       740: #ifdef TEMPLATES
1.89      kia       741: static int QueryMenu(Document doc, char* items)
                    742: {
                    743:   int nbitems, size;
                    744:   struct menuType *itemlist;
                    745:   char *menuString;
                    746:   
                    747:   size = strlen(items);
                    748:   giveItems (items, size, &itemlist, &nbitems);
                    749:   menuString = createMenuString (itemlist, nbitems);
                    750:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    751:                      nbitems, menuString , NULL, false, 'L');
                    752:   TtaFreeMemory (menuString);
                    753:   ReturnOption = -1;
                    754:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    755:   TtaWaitShowProcDialogue ();
                    756:   TtaDestroyDialogue (BaseDialog + OptionMenu);
                    757:   TtaFreeMemory (itemlist);
                    758:   return ReturnOption;
                    759: }
                    760: 
1.90      kia       761: static char* QueryStringFromMenu(Document doc, char* items)
                    762: {
                    763:   int nbitems, size;
                    764:   struct menuType *itemlist;
                    765:   char *menuString;
                    766:   char *result = NULL;
                    767:   
                    768:   size = strlen(items);
                    769:   giveItems (items, size, &itemlist, &nbitems);
                    770:   menuString = createMenuString (itemlist, nbitems);
                    771:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    772:                      nbitems, menuString , NULL, false, 'L');
                    773:   TtaFreeMemory (menuString);
                    774:   ReturnOption = -1;
                    775:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    776:   TtaWaitShowProcDialogue ();
                    777:   TtaDestroyDialogue (BaseDialog + OptionMenu);
                    778:   
                    779:   if(ReturnOption!=-1)
                    780:   {
                    781:     result = TtaStrdup(itemlist[ReturnOption].label);
                    782:   }
                    783:   
                    784:   TtaFreeMemory (itemlist);
                    785:   return result;
                    786: }
1.92      kia       787: #endif /* TEMPLATES */
1.90      kia       788: 
1.56      francesc  789: /*----------------------------------------------------------------------
1.79      quint     790:   RepeatButtonClicked
1.89      kia       791:   Called when a repeat button is clicked.
                    792:   Can be called for useEl, useSimple or repeat.
                    793:   If called for useEl or useSimple, the new element must be added after.
                    794:   If called for repeat, the element must be added before all.
                    795:    
1.56      francesc  796:   Shows a menu with all the types that can be used in a use element.
                    797:   ----------------------------------------------------------------------*/
1.79      quint     798: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  799: {
                    800: #ifdef TEMPLATES
1.89      kia       801:   Document        doc = event->document;
                    802:   Element         el = event->element;
                    803:   ElementType     elType;
1.90      kia       804:   XTigerTemplate  t;
                    805:   Declaration     decl;
                    806:   Element         repeatEl = el;
                    807:   Element         firstEl;
                    808:   Element         newEl = NULL;
1.89      kia       809:   char*           types;
1.90      kia       810:   ThotBool        oldStructureChecking;
1.89      kia       811:   
                    812:   TtaCancelSelection(doc);
1.90      kia       813:   
                    814:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.89      kia       815:   elType = TtaGetElementType(el);
                    816:   while(elType.ElTypeNum!=Template_EL_repeat)
                    817:   {
1.90      kia       818:     repeatEl = TtaGetParent(repeatEl);
                    819:     if(repeatEl==NULL)
1.89      kia       820:       break;
1.90      kia       821:     elType = TtaGetElementType(repeatEl);
1.89      kia       822:   }
1.90      kia       823:   if(repeatEl)
1.89      kia       824:   {
1.90      kia       825:     firstEl = TtaGetFirstChild(repeatEl);
                    826:     types = GetAttributeStringValue(firstEl, Template_ATTR_types);
                    827:     
                    828:     char* listtypes = Template_ExpandTypes(t, types);
                    829:     char* result = QueryStringFromMenu(doc, listtypes);
                    830:     if(result)
                    831:     {
                    832:       decl = GetDeclaration(t, result);
                    833:       if(decl)
                    834:       {
                    835:         oldStructureChecking = TtaGetStructureChecking (doc);
                    836:         TtaSetStructureChecking (FALSE, doc);
                    837:         
                    838:         if(el==repeatEl)
                    839:           newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, NULL);
                    840:         else
                    841:           newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, el);
                    842:         
                    843:         TtaSetStructureChecking (oldStructureChecking, doc);
                    844:         TtaSelectElement (doc, newEl);
                    845:       }
                    846:     }
                    847:     TtaFreeMemory(listtypes);
                    848:     TtaFreeMemory(result);
1.89      kia       849:   }
1.77      vatton    850:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc  851: #endif /* TEMPLATES */
1.56      francesc  852:        return TRUE;
                    853: }
1.64      francesc  854: 
1.89      kia       855: 
                    856: 
1.66      vatton    857: /*----------------------------------------------------------------------
                    858:   ----------------------------------------------------------------------*/
1.76      vatton    859: void OpeningInstance (char *fileName, Document doc)
1.65      francesc  860: {
                    861: #ifdef TEMPLATES
1.76      vatton    862:   XTigerTemplate   t;
                    863:   char            *content, *ptr;
                    864:   gzFile           stream;
                    865:   char             buffer[2000];
1.77      vatton    866:   int              res;
1.65      francesc  867: 
1.76      vatton    868:   stream = TtaGZOpen (fileName);
                    869:   if (stream != 0)
1.65      francesc  870:     {
1.76      vatton    871:       res = gzread (stream, buffer, 1999);
                    872:       if (res >= 0)
1.65      francesc  873:         {
1.81      vatton    874:           buffer[res] = EOS;
1.76      vatton    875:           ptr = strstr (buffer, "<?xtiger");
                    876:           if (ptr)
                    877:             ptr = strstr (ptr, "template");
                    878:           if (ptr)
                    879:             ptr = strstr (ptr, "=");
                    880:           if (ptr)
                    881:             ptr = strstr (ptr, "\"");
                    882:           if (ptr)
                    883:             {
                    884:               // template URI
                    885:               content = &ptr[1];
                    886:               ptr = strstr (content, "\"");
                    887:             }
                    888:           if (ptr)
                    889:             {
                    890:               *ptr = EOS;
                    891:               //Get now the template URI
                    892:               DocumentMeta[doc]->template_url = TtaStrdup (content);
                    893:               if (Templates_Dic == NULL)
                    894:                 InitializeTemplateEnvironment ();
1.87      kia       895:               t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
1.76      vatton    896:               if (!t)
                    897:                 {
                    898:                   LoadTemplate (0, content);
1.87      kia       899:                   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
1.76      vatton    900:                 }
                    901:               AddUser (t);
                    902:             }
1.65      francesc  903:         }
                    904:     }
1.76      vatton    905:   TtaGZClose (stream);
1.65      francesc  906: #endif /* TEMPLATES */
                    907: }
                    908: 
1.64      francesc  909: /*----------------------------------------------------------------------
1.65      francesc  910:   ClosingInstance
1.64      francesc  911:   Callback called before closing a document. Checks for unused templates.
                    912:   ----------------------------------------------------------------------*/
1.65      francesc  913: ThotBool ClosingInstance(NotifyDialog* dialog)
1.64      francesc  914: {
1.65      francesc  915: #ifdef TEMPLATES
                    916:   //If it is a template all has been already freed
1.76      vatton    917:   if (DocumentMeta[dialog->document] == NULL)
                    918:     return FALSE;
1.65      francesc  919: 
                    920:   char *turl = DocumentMeta[dialog->document]->template_url;
1.73      vatton    921:   if (turl)
1.65      francesc  922:     {
1.87      kia       923:       XTigerTemplate t = (XTigerTemplate) Dictionary_Get (Templates_Dic, turl);
1.73      vatton    924:       if (t)
1.76      vatton    925:         RemoveUser (t);
                    926:       TtaFreeMemory (turl);
1.65      francesc  927:     }
                    928: #endif /* TEMPLATES */
                    929:   return FALSE;
1.64      francesc  930: }
1.87      kia       931: 
                    932: 
                    933: /*----------------------------------------------------------------------
                    934:   GetFirstTemplateParentElement
                    935:   Return the first element wich has "Template" as SShema name or null if none.
                    936:   ----------------------------------------------------------------------*/
                    937: ThotBool IsTemplateElement(Element elem)
                    938: {
                    939: #ifdef TEMPLATES
                    940:   return strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
                    941:                                                     , TEMPLATE_SSHEMA_NAME)==0;
                    942: #else
                    943:   return FALSE;
                    944: #endif /* TEMPLATES */
                    945: }
                    946: 
                    947: 
                    948: /*----------------------------------------------------------------------
                    949:   GetFirstTemplateParentElement
                    950:   Return the first element wich has "Template" as SShema name or null if none.
                    951:   ----------------------------------------------------------------------*/
                    952: Element GetFirstTemplateParentElement(Element elem)
                    953: {
                    954: #ifdef TEMPLATES
                    955:   elem = TtaGetParent(elem);
                    956:   while(elem!=NULL && strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
                    957:                                                     , TEMPLATE_SSHEMA_NAME)!=0)
                    958:   {
                    959:     elem = TtaGetParent(elem);
                    960:   }
                    961:   return elem;
                    962: #else
                    963:   return NULL;
                    964: #endif /* TEMPLATES */
                    965: }

Webmaster