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

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: 
1.92      kia       564: #ifdef TEMPLATES
1.89      kia       565: static int QueryMenu(Document doc, char* items)
                    566: {
                    567:   int nbitems, size;
                    568:   struct menuType *itemlist;
                    569:   char *menuString;
                    570:   
                    571:   size = strlen(items);
                    572:   giveItems (items, size, &itemlist, &nbitems);
                    573:   menuString = createMenuString (itemlist, nbitems);
                    574:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    575:                      nbitems, menuString , NULL, false, 'L');
                    576:   TtaFreeMemory (menuString);
                    577:   ReturnOption = -1;
                    578:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    579:   TtaWaitShowProcDialogue ();
                    580:   TtaDestroyDialogue (BaseDialog + OptionMenu);
                    581:   TtaFreeMemory (itemlist);
                    582:   return ReturnOption;
                    583: }
                    584: 
1.90      kia       585: static char* QueryStringFromMenu(Document doc, char* items)
                    586: {
                    587:   int nbitems, size;
                    588:   struct menuType *itemlist;
                    589:   char *menuString;
                    590:   char *result = NULL;
                    591:   
                    592:   size = strlen(items);
                    593:   giveItems (items, size, &itemlist, &nbitems);
                    594:   menuString = createMenuString (itemlist, nbitems);
                    595:   TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    596:                      nbitems, menuString , NULL, false, 'L');
                    597:   TtaFreeMemory (menuString);
                    598:   ReturnOption = -1;
                    599:   TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    600:   TtaWaitShowProcDialogue ();
                    601:   TtaDestroyDialogue (BaseDialog + OptionMenu);
                    602:   
                    603:   if(ReturnOption!=-1)
                    604:   {
                    605:     result = TtaStrdup(itemlist[ReturnOption].label);
                    606:   }
                    607:   
                    608:   TtaFreeMemory (itemlist);
                    609:   return result;
                    610: }
1.92      kia       611: #endif /* TEMPLATES */
1.90      kia       612: 
1.56      francesc  613: /*----------------------------------------------------------------------
1.79      quint     614:   RepeatButtonClicked
1.89      kia       615:   Called when a repeat button is clicked.
                    616:   Can be called for useEl, useSimple or repeat.
                    617:   If called for useEl or useSimple, the new element must be added after.
                    618:   If called for repeat, the element must be added before all.
                    619:    
1.56      francesc  620:   Shows a menu with all the types that can be used in a use element.
                    621:   ----------------------------------------------------------------------*/
1.79      quint     622: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  623: {
                    624: #ifdef TEMPLATES
1.89      kia       625:   Document        doc = event->document;
                    626:   Element         el = event->element;
                    627:   ElementType     elType;
1.90      kia       628:   XTigerTemplate  t;
                    629:   Declaration     decl;
                    630:   Element         repeatEl = el;
                    631:   Element         firstEl;
                    632:   Element         newEl = NULL;
1.89      kia       633:   char*           types;
1.90      kia       634:   ThotBool        oldStructureChecking;
1.89      kia       635:   
                    636:   TtaCancelSelection(doc);
1.90      kia       637:   
                    638:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.89      kia       639:   elType = TtaGetElementType(el);
                    640:   while(elType.ElTypeNum!=Template_EL_repeat)
                    641:   {
1.90      kia       642:     repeatEl = TtaGetParent(repeatEl);
                    643:     if(repeatEl==NULL)
1.89      kia       644:       break;
1.90      kia       645:     elType = TtaGetElementType(repeatEl);
1.89      kia       646:   }
1.90      kia       647:   if(repeatEl)
1.89      kia       648:   {
1.90      kia       649:     firstEl = TtaGetFirstChild(repeatEl);
                    650:     types = GetAttributeStringValue(firstEl, Template_ATTR_types);
                    651:     
                    652:     char* listtypes = Template_ExpandTypes(t, types);
                    653:     char* result = QueryStringFromMenu(doc, listtypes);
                    654:     if(result)
                    655:     {
                    656:       decl = GetDeclaration(t, result);
                    657:       if(decl)
                    658:       {
                    659:         oldStructureChecking = TtaGetStructureChecking (doc);
                    660:         TtaSetStructureChecking (FALSE, doc);
                    661:         
                    662:         if(el==repeatEl)
                    663:           newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, NULL);
                    664:         else
                    665:           newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, el);
                    666:         
                    667:         TtaSetStructureChecking (oldStructureChecking, doc);
                    668:         TtaSelectElement (doc, newEl);
                    669:       }
                    670:     }
                    671:     TtaFreeMemory(listtypes);
                    672:     TtaFreeMemory(result);
1.89      kia       673:   }
1.77      vatton    674:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc  675: #endif /* TEMPLATES */
1.94    ! kia       676:   return TRUE;
        !           677: }
        !           678: 
        !           679: /*----------------------------------------------------------------------
        !           680:   UseButtonClicked
        !           681:   Shows a menu with all the types that can be used in a use element.
        !           682:   ----------------------------------------------------------------------*/
        !           683: ThotBool UseButtonClicked (NotifyElement *event)
        !           684: {
        !           685: #ifdef TEMPLATES
        !           686:   Document        doc = event->document;
        !           687:   Element         el = event->element;
        !           688:   ElementType     elType;
        !           689:   XTigerTemplate  t;
        !           690:   Declaration     decl;
        !           691:   Element         useEl;
        !           692:   Element         repeatEl;
        !           693:   Element         firstEl;
        !           694:   Element         newEl = NULL;
        !           695:   char*           types;
        !           696:   ThotBool        oldStructureChecking;
        !           697:   
        !           698:   TtaCancelSelection(doc);
        !           699:   
        !           700:   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
        !           701:   if (!t)
        !           702:     return FALSE; /* let Thot perform normal operation */
        !           703:   elType = TtaGetElementType(el);
        !           704: 
        !           705:   firstEl = TtaGetFirstChild(el);
        !           706:   if(firstEl)
        !           707:   {
        !           708:     RepeatButtonClicked(event);
        !           709:   }
        !           710:   else
        !           711:   {
        !           712:     types = GetAttributeStringValue(el, Template_ATTR_types);
        !           713: 
        !           714:     char* listtypes = Template_ExpandTypes(t, types);
        !           715:     char* result = QueryStringFromMenu(doc, listtypes);
        !           716:     if(result)
        !           717:     {
        !           718:       decl = GetDeclaration(t, result);
        !           719:       if(decl)
        !           720:       {
        !           721:         oldStructureChecking = TtaGetStructureChecking (doc);
        !           722:         TtaSetStructureChecking (FALSE, doc);
        !           723:         
        !           724:         newEl = Template_InsertUseChildren(doc, el, decl);
        !           725:         
        !           726:         TtaSetStructureChecking (oldStructureChecking, doc);
        !           727:         TtaSelectElement (doc, newEl);
        !           728:       }
        !           729:     }
        !           730:     TtaFreeMemory(listtypes);
        !           731:     TtaFreeMemory(result);
        !           732:     printf("empty use\n");
        !           733:   }
        !           734:   
        !           735:   return TRUE;
        !           736: #endif /* TEMPLATES */
1.56      francesc  737:        return TRUE;
                    738: }
1.64      francesc  739: 
1.89      kia       740: 
                    741: 
1.94    ! kia       742: 
        !           743: 
        !           744: /*----------------------------------------------------------------------
        !           745:   OptionButtonClicked
        !           746:   ----------------------------------------------------------------------*/
        !           747: ThotBool OptionButtonClicked (NotifyElement *event)
        !           748: {
        !           749: #ifdef TEMPLATES
        !           750:   Element         child, grandChild, next;
        !           751:   ElementType     elType, elType1;
        !           752:   Document        doc;
        !           753:   XTigerTemplate  t;
        !           754:   View            view;
        !           755: 
        !           756:   TtaGetActiveView (&doc, &view);
        !           757:   if (view != 1)
        !           758:     return FALSE; /* let Thot perform normal operation */
        !           759:   doc = event->document;
        !           760:   child = TtaGetFirstChild (event->element);
        !           761:   if (!child)
        !           762:     return FALSE; /* let Thot perform normal operation */
        !           763:   elType = TtaGetElementType (child);
        !           764:   elType1 = TtaGetElementType (event->element);
        !           765:   if ((elType.ElTypeNum != Template_EL_useEl &&
        !           766:        elType.ElTypeNum != Template_EL_useSimple) ||
        !           767:       elType.ElSSchema != elType1.ElSSchema)
        !           768:     return FALSE;
        !           769: 
        !           770:   TtaCancelSelection (doc);
        !           771:   grandChild = TtaGetFirstChild (child);
        !           772:   if (!grandChild)
        !           773:     /* the "use" element is empty. Instantiate it */
        !           774:     {
        !           775:       t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
        !           776:       if (!t)
        !           777:         return FALSE; // no template ?!?!
        !           778:       InstantiateUse (t, child, doc, TRUE);
        !           779:     }
        !           780:   else
        !           781:     /* remove the content of the "use" element */
        !           782:     {
        !           783:       do
        !           784:         {
        !           785:           next = grandChild;
        !           786:           TtaNextSibling (&next);
        !           787:           TtaDeleteTree (grandChild, doc);
        !           788:           grandChild = next;
        !           789:         }
        !           790:       while (next);
        !           791:     }
        !           792:   TtaSelectElement (doc, event->element);
        !           793:   return TRUE; /* don't let Thot perform normal operation */
        !           794: #endif /* TEMPLATES */
        !           795:   return TRUE;
        !           796: }
        !           797: 
        !           798: 
        !           799: 
1.66      vatton    800: /*----------------------------------------------------------------------
                    801:   ----------------------------------------------------------------------*/
1.76      vatton    802: void OpeningInstance (char *fileName, Document doc)
1.65      francesc  803: {
                    804: #ifdef TEMPLATES
1.76      vatton    805:   XTigerTemplate   t;
                    806:   char            *content, *ptr;
                    807:   gzFile           stream;
                    808:   char             buffer[2000];
1.77      vatton    809:   int              res;
1.65      francesc  810: 
1.76      vatton    811:   stream = TtaGZOpen (fileName);
                    812:   if (stream != 0)
1.65      francesc  813:     {
1.76      vatton    814:       res = gzread (stream, buffer, 1999);
                    815:       if (res >= 0)
1.65      francesc  816:         {
1.81      vatton    817:           buffer[res] = EOS;
1.76      vatton    818:           ptr = strstr (buffer, "<?xtiger");
                    819:           if (ptr)
                    820:             ptr = strstr (ptr, "template");
                    821:           if (ptr)
                    822:             ptr = strstr (ptr, "=");
                    823:           if (ptr)
                    824:             ptr = strstr (ptr, "\"");
                    825:           if (ptr)
                    826:             {
                    827:               // template URI
                    828:               content = &ptr[1];
                    829:               ptr = strstr (content, "\"");
                    830:             }
                    831:           if (ptr)
                    832:             {
                    833:               *ptr = EOS;
                    834:               //Get now the template URI
                    835:               DocumentMeta[doc]->template_url = TtaStrdup (content);
                    836:               if (Templates_Dic == NULL)
                    837:                 InitializeTemplateEnvironment ();
1.87      kia       838:               t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
1.76      vatton    839:               if (!t)
                    840:                 {
                    841:                   LoadTemplate (0, content);
1.87      kia       842:                   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
1.76      vatton    843:                 }
                    844:               AddUser (t);
                    845:             }
1.65      francesc  846:         }
                    847:     }
1.76      vatton    848:   TtaGZClose (stream);
1.65      francesc  849: #endif /* TEMPLATES */
                    850: }
                    851: 
1.64      francesc  852: /*----------------------------------------------------------------------
1.65      francesc  853:   ClosingInstance
1.64      francesc  854:   Callback called before closing a document. Checks for unused templates.
                    855:   ----------------------------------------------------------------------*/
1.65      francesc  856: ThotBool ClosingInstance(NotifyDialog* dialog)
1.64      francesc  857: {
1.65      francesc  858: #ifdef TEMPLATES
                    859:   //If it is a template all has been already freed
1.76      vatton    860:   if (DocumentMeta[dialog->document] == NULL)
                    861:     return FALSE;
1.65      francesc  862: 
                    863:   char *turl = DocumentMeta[dialog->document]->template_url;
1.73      vatton    864:   if (turl)
1.65      francesc  865:     {
1.87      kia       866:       XTigerTemplate t = (XTigerTemplate) Dictionary_Get (Templates_Dic, turl);
1.73      vatton    867:       if (t)
1.76      vatton    868:         RemoveUser (t);
                    869:       TtaFreeMemory (turl);
1.65      francesc  870:     }
                    871: #endif /* TEMPLATES */
                    872:   return FALSE;
1.64      francesc  873: }
1.87      kia       874: 
                    875: 
                    876: /*----------------------------------------------------------------------
                    877:   GetFirstTemplateParentElement
                    878:   Return the first element wich has "Template" as SShema name or null if none.
                    879:   ----------------------------------------------------------------------*/
                    880: ThotBool IsTemplateElement(Element elem)
                    881: {
                    882: #ifdef TEMPLATES
                    883:   return strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
                    884:                                                     , TEMPLATE_SSHEMA_NAME)==0;
                    885: #else
                    886:   return FALSE;
                    887: #endif /* TEMPLATES */
                    888: }
                    889: 
                    890: 
                    891: /*----------------------------------------------------------------------
                    892:   GetFirstTemplateParentElement
                    893:   Return the first element wich has "Template" as SShema name or null if none.
                    894:   ----------------------------------------------------------------------*/
                    895: Element GetFirstTemplateParentElement(Element elem)
                    896: {
                    897: #ifdef TEMPLATES
                    898:   elem = TtaGetParent(elem);
                    899:   while(elem!=NULL && strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
                    900:                                                     , TEMPLATE_SSHEMA_NAME)!=0)
                    901:   {
                    902:     elem = TtaGetParent(elem);
                    903:   }
                    904:   return elem;
                    905: #else
                    906:   return NULL;
                    907: #endif /* TEMPLATES */
                    908: }

Webmaster