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

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.46      vatton     17: #ifdef TEMPLATES
                     18: #include "Template.h"
1.67      quint      19: #include "templates.h"
1.52      vatton     20: #include "templateDeclarations.h"
                     21: 
1.69      quint      22: #include "mydictionary_f.h"
1.67      quint      23: #include "templateLoad_f.h"
                     24: #include "templateDeclarations_f.h"
1.76      vatton     25: #include "templateInstantiate_f.h"
1.28      tollenae   26: #include "appdialogue_wx.h"
1.29      tollenae   27: #include "init_f.h"
1.46      vatton     28: #include "wxdialogapi_f.h"
1.60      francesc   29: #include "AHTURLTools_f.h"
                     30: 
1.52      vatton     31: #endif /* TEMPLATES */
1.1       cvs        32: 
1.83      kia        33: #include "MENUconf.h"
                     34: 
                     35: /* Paths from which looking for templates.*/
                     36: static Prop_Templates_Path *TemplateRepositoryPaths;
                     37: 
                     38: /*----------------------------------------------------------------------
                     39:   AllocTemplateRepositoryListElement: alloc an element for the list of template repositories.
                     40:   path : path of the new element
                     41:   return : address of the new element
                     42:   ----------------------------------------------------------------------*/
                     43: void* AllocTemplateRepositoryListElement (const char* path, void* prevElement)
                     44: {
                     45:   Prop_Templates_Path *element = (Prop_Templates_Path*)TtaGetMemory (sizeof(Prop_Templates_Path));
                     46:   element->NextPath = NULL;
                     47:   strcpy (element->Path, path);
                     48:   if (prevElement)
                     49:   {
                     50:     element->NextPath = ((Prop_Templates_Path*)prevElement)->NextPath;
                     51:     ((Prop_Templates_Path*)prevElement)->NextPath = element;
                     52:   }
                     53:   return element;
                     54: }
                     55: 
                     56: 
                     57: /*----------------------------------------------------------------------
                     58:   FreeTemplateRepositoryList: Free the list of template repositories.
                     59:   list : address of the list (address of the first element).
                     60:   ----------------------------------------------------------------------*/
                     61: void FreeTemplateRepositoryList (void* list)
                     62: {
                     63:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                     64:   
                     65:   Prop_Templates_Path* element = *l;
                     66:   l = NULL;
                     67:   while (element)
                     68:   {
                     69:     Prop_Templates_Path* next = element->NextPath;
                     70:     TtaFreeMemory(element);
                     71:     element = next;
                     72:   }
                     73: }
                     74: 
                     75: /*----------------------------------------------------------------------
                     76:   CopyTemplateRepositoryList: Copy a list of template repositories.
                     77:   src : address of the list (address of the first element).
                     78:   dst : address where copy the list
                     79:   ----------------------------------------------------------------------*/
                     80: static void CopyTemplateRepositoryList (const Prop_Templates_Path** src, Prop_Templates_Path** dst)
                     81: {
                     82:   Prop_Templates_Path *element=NULL, *current=NULL;
                     83:   
                     84:   if(*src!=NULL)
                     85:   {
                     86:     *dst = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                     87:     (*dst)->NextPath = NULL;
                     88:     strcpy((*dst)->Path, (*src)->Path);
                     89:     
                     90:     element = (*src)->NextPath;
                     91:     current = *dst;
                     92:   }
                     93: 
                     94:   while (element){
                     95:     current->NextPath = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                     96:     current = current->NextPath; 
                     97:     current->NextPath = NULL;
                     98:     strcpy(current->Path, element->Path);
                     99:     element = element->NextPath;
                    100:   }
                    101: }
                    102: 
                    103: /*----------------------------------------------------------------------
                    104:   LoadTemplateRepositoryList: Load the list of template repositories.
                    105:   list   : address of the list (address of the first element).
                    106:   return : the number of readed repository paths.
                    107:   ----------------------------------------------------------------------*/
                    108: static int LoadTemplateRepositoryList (Prop_Templates_Path** list)
                    109: {
                    110:   Prop_Templates_Path *element, *current = NULL;
                    111:   char *path, *homePath;
                    112:   unsigned char *c;
                    113:   int nb = 0;
                    114:   FILE *file;
                    115:   
                    116:   FreeTemplateRepositoryList(list);
                    117:   
                    118:   path = (char *) TtaGetMemory (MAX_LENGTH);
                    119:   homePath       = TtaGetEnvString ("APP_HOME");
                    120:   sprintf (path, "%s%ctemplate-repositories.dat", homePath, DIR_SEP);
                    121:   
                    122:   file = TtaReadOpen ((char *)path);
1.84    ! kia       123:   if (!file)
        !           124:   {
        !           125:     /* The config file dont exist, create it. */
        !           126:     file = TtaWriteOpen ((char *)path);
        !           127:     fprintf(file, "%s%ctemplates%cen\n", homePath, DIR_SEP, DIR_SEP);
        !           128:     TtaWriteClose (file);
        !           129:     /* Retry to open it.*/
        !           130:     file = TtaReadOpen ((char *)path);
        !           131:   }
        !           132:   
1.83      kia       133:   if (file)
                    134:   {
1.84    ! kia       135:     c = (unsigned char*)path;
        !           136:     *c = EOS;
1.83      kia       137:     while (TtaReadByte (file, c)){
                    138:       if (*c==13 || *c==EOL)
                    139:         *c = EOS;
                    140:       if (*c==EOS && c!=(unsigned char*)path )
                    141:       {
                    142:         element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    143:         element->NextPath = NULL;
                    144:         strcpy (element->Path, path);
                    145:         
                    146:         if (*list == NULL)
                    147:           *list = element; 
                    148:         else
                    149:           current->NextPath = element;
                    150:         current = element;
                    151:         nb++;
                    152: 
                    153:         c = (unsigned char*) path;
                    154:         *c = EOS;
                    155:       }
                    156:       else
                    157:         c++;
                    158:     }
                    159:     if (c!=(unsigned char*)path && *path!=EOS)
                    160:     {
                    161:       element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
                    162:       *(c+1) = EOS;
                    163:       strcpy (element->Path, path);
                    164:       element->NextPath = NULL;
                    165:       
                    166:       if (*list == NULL)
                    167:         *list = element; 
                    168:       else
                    169:         current->NextPath = element;
                    170:       nb++;
                    171:     }
                    172:     TtaReadClose (file);
                    173:   }
                    174:   TtaFreeMemory(path);
                    175:   return nb;
                    176: }
                    177: 
                    178: /*----------------------------------------------------------------------
                    179:   SaveTemplateRepositoryList: Save the list of template repositories.
                    180:   list   : address of the list (address of the first element).
                    181:   ----------------------------------------------------------------------*/
                    182: static void SaveTemplateRepositoryList (const Prop_Templates_Path** list)
                    183: {
                    184:   const Prop_Templates_Path *element;
                    185:   char *path, *homePath;
                    186:   unsigned char *c;
                    187:   FILE *file;
                    188: 
                    189:   path = (char *) TtaGetMemory (MAX_LENGTH);
                    190:   homePath       = TtaGetEnvString ("APP_HOME");
                    191:   sprintf (path, "%s%ctemplate-repositories.dat", homePath, DIR_SEP);
                    192: 
                    193:   file = TtaWriteOpen ((char *)path);
                    194:   c = (unsigned char*)path;
                    195:   *c = EOS;
                    196:   if (file)
                    197:   {
                    198:     element = *list;
                    199:     while (element)
                    200:     {
                    201:       fprintf(file, "%s\n", element->Path);
                    202:       element = element->NextPath;
                    203:     }
                    204:     TtaWriteClose (file);
                    205:   }
                    206: }
                    207: 
                    208: /*----------------------------------------------------------------------
                    209:   GetTemplateRepositoryList: Get the list of template repositories from template environment.
                    210:   list : address of the list (address of the first element).
                    211:   ----------------------------------------------------------------------*/
                    212: void GetTemplateRepositoryList (void* list)
                    213: {
                    214:   Prop_Templates_Path** l = (Prop_Templates_Path**) list;
                    215:   CopyTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths, l);
                    216: }
                    217: 
                    218: /*----------------------------------------------------------------------
                    219:   SetTemplateRepositoryList: Set the list of template repositories environment.
                    220:   list : address of the list (address of the first element).
                    221:   ----------------------------------------------------------------------*/
                    222: void SetTemplateRepositoryList (const void* list)
                    223: {
                    224:   const Prop_Templates_Path** l = (const Prop_Templates_Path**) list;
                    225:   CopyTemplateRepositoryList((const Prop_Templates_Path**)l, &TemplateRepositoryPaths);
                    226:   SaveTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths);
                    227: }
                    228: 
                    229: /*-----------------------------------------------------------------------
                    230:    InitTemplates
                    231:    Initializes the annotation library
                    232:   -----------------------------------------------------------------------*/
                    233: void InitTemplates ()
                    234: {
                    235:   TtaSetEnvBoolean ("SHOW_TEMPLATES", TRUE, FALSE);
                    236:   LoadTemplateRepositoryList(&TemplateRepositoryPaths);
                    237: }
                    238: 
                    239: 
                    240: 
1.1       cvs       241: /*----------------------------------------------------------------------
1.51      francesc  242:   NewTemplate: Create the "new document from template" dialog
1.1       cvs       243:   ----------------------------------------------------------------------*/
1.18      cvs       244: void NewTemplate (Document doc, View view)
1.1       cvs       245: {
1.51      francesc  246: #ifdef TEMPLATES
1.76      vatton    247:   char        *templateDir = TtaGetEnvString ("TEMPLATES_DIRECTORY");
                    248:   ThotBool     created;
1.28      tollenae  249: 
1.76      vatton    250:   if (Templates_Dic == NULL)
                    251:     InitializeTemplateEnvironment ();
                    252:   created = CreateNewTemplateDocDlgWX (BaseDialog + OpenTemplate,
1.61      francesc  253:                                       /*TtaGetViewFrame (doc, view)*/NULL, doc,
1.52      vatton    254:                                       TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),templateDir);
1.51      francesc  255:   
1.28      tollenae  256:   if (created)
1.25      vatton    257:     {
1.28      tollenae  258:       TtaSetDialoguePosition ();
                    259:       TtaShowDialogue (BaseDialog + OpenTemplate, TRUE);
1.25      vatton    260:     }
1.51      francesc  261: 
1.52      vatton    262: #endif /* TEMPLATES */
1.1       cvs       263: }
1.25      vatton    264: 
1.53      vatton    265: /*----------------------------------------------------------------------
                    266:   Load a template and create the instance file - update images and 
                    267:   stylesheets related to the template.
                    268:   ----------------------------------------------------------------------*/
1.61      francesc  269: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
1.53      vatton    270: {
                    271: #ifdef TEMPLATES
                    272: 
1.60      francesc  273:   char *s;
1.65      francesc  274:   ThotBool dontReplace = DontReplaceOldDoc;
1.60      francesc  275: 
                    276:   if (!IsW3Path (docname) && TtaFileExist (docname))
                    277:     {
                    278:       s = (char *)TtaGetMemory (strlen (docname) +
                    279:                                 strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
                    280:       sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
                    281:       InitConfirm (0, 0, s);
1.61      francesc  282:       TtaFreeMemory (s);      
1.62      francesc  283:       if (!UserAnswer)
                    284:         return;
                    285:     }    
                    286: 
1.76      vatton    287:   LoadTemplate (0, templatename);
1.65      francesc  288:   DontReplaceOldDoc = dontReplace;
1.76      vatton    289:   CreateInstance (templatename, docname);
1.62      francesc  290:   
1.53      vatton    291: #endif /* TEMPLATES */
                    292: }
                    293: 
1.52      vatton    294: /*----------------------------------------------------------------------
                    295:   ----------------------------------------------------------------------*/
1.76      vatton    296: void giveItems (char *text, int size, struct menuType **items, int *nbitems)
1.1       cvs       297: {
1.70      quint     298: #ifdef TEMPLATES
1.52      vatton    299:        ThotBool         inElement = TRUE;
                    300:   struct menuType *menu;
                    301:   char            *iter;
                    302:        char             temp[128];
                    303:   int              i;
                    304:        int              labelSize;
1.28      tollenae  305: 
1.52      vatton    306:        *nbitems = 1;
                    307:        for (i = 0; i < size; i++)
                    308:     {
                    309:       if (isEOSorWhiteSpace (text[i]))
                    310:         {
                    311:           if (inElement)
                    312:             inElement = FALSE;
                    313:         }
                    314:       else if (!inElement)
                    315:         {
                    316:           inElement = TRUE;
                    317:           (*nbitems)++;
                    318:         }
                    319:     }
1.51      francesc  320: 
1.76      vatton    321:        menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
1.52      vatton    322:        iter = text;
                    323:        for (i = 0; i < *nbitems; i++)
                    324:     {          
                    325:       labelSize = 0;
                    326:       while (isEOSorWhiteSpace (*iter))
                    327:         iter++;
                    328: 
                    329:       while (!isEOSorWhiteSpace (*iter))
                    330:         {
                    331:           temp[labelSize++] = *iter;
                    332:           iter++;
                    333:         }
                    334: 
                    335:       temp[labelSize] = EOS;
1.76      vatton    336:       menu[i].label = (char *) TtaStrdup (temp);
1.68      quint     337:       menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
1.52      vatton    338:       *items = menu;
                    339:     }
1.70      quint     340: #endif /* TEMPLATES */
1.28      tollenae  341: }
1.37      tollenae  342: 
1.70      quint     343: #ifdef TEMPLATES
1.52      vatton    344: /*----------------------------------------------------------------------
                    345:   ----------------------------------------------------------------------*/
                    346: static char *createMenuString (const struct menuType* items, const int nbItems)
                    347: {
                    348:   char *result, *iter;
                    349:        int   size = 0;
                    350:   int   i;
                    351: 
                    352:        for (i=0; i < nbItems; i++)
                    353:                size += 2 + strlen (items[i].label);
                    354: 
1.76      vatton    355:        result = (char *) TtaGetMemory (size);
1.52      vatton    356:        iter = result;
                    357:        for (i=0; i < nbItems; i++)
                    358:     {
                    359:       *iter = 'B';
                    360:       ++iter;
1.51      francesc  361:                
1.52      vatton    362:       strcpy (iter, items[i].label);
                    363:       iter += strlen (items[i].label)+1;
                    364:     }
1.51      francesc  365:        return result;
1.36      tollenae  366: }
1.71      quint     367: #endif /* TEMPLATES */
1.29      tollenae  368: 
1.71      quint     369: /*----------------------------------------------------------------------
                    370:   UseToBeCreated
                    371:   An new use element will be created by the user through some generic editing
                    372:   command
                    373:   -----------------------------------------------------------------------*/
                    374: ThotBool UseToBeCreated (NotifyElement *event)
                    375: {
                    376: #ifdef TEMPLATES
1.75      quint     377:   Element        el;
1.72      quint     378:        Document       doc;
                    379: 
                    380:   el = event->element;
                    381:   doc = event->document;
                    382:   /* is there a limit to the number of elements in the xt:repeat ? */
1.71      quint     383:   /* @@@@@ */
1.52      vatton    384: #endif /* TEMPLATES */
1.71      quint     385:   return FALSE; /* let Thot perform normal operation */
                    386: }
                    387: 
                    388: /*----------------------------------------------------------------------
                    389:   UseCreated
                    390:   A new "use" element has just been created by the user with a generic editing
                    391:   command.
                    392:   -----------------------------------------------------------------------*/
                    393: void UseCreated (NotifyElement *event)
                    394: {
                    395: #ifdef TEMPLATES
                    396:        Document         doc;
                    397:        Element          el;
                    398:   XTigerTemplate   t;
                    399: 
                    400:        doc = event->document;
1.72      quint     401:   el = event->element;
                    402:   if (TtaGetFirstChild (el))
                    403:     /* this Use element has already some content. It has already been
                    404:        instanciated */
                    405:     return;
1.76      vatton    406:   t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.71      quint     407:   if (!t)
                    408:     return; // no template ?!?!
1.76      vatton    409:   InstantiateUse (t, el, doc, TRUE);
1.71      quint     410: #endif /* TEMPLATES */
                    411: }
1.29      tollenae  412: 
1.46      vatton    413: /*----------------------------------------------------------------------
1.79      quint     414:   UseButtonClicked
1.51      francesc  415:   Shows a menu with all the types that can be used in a use element.
1.46      vatton    416:   ----------------------------------------------------------------------*/
1.79      quint     417: ThotBool UseButtonClicked (NotifyElement *event)
1.39      tollenae  418: {
1.43      tollenae  419: #ifdef TEMPLATES
1.70      quint     420:        Document         doc;
                    421:        Element          el, comp;
1.76      vatton    422:        ElementType      elType;
                    423:        Attribute        att;
                    424:        AttributeType    attributeType;
1.67      quint     425:   XTigerTemplate   t;
                    426:   Declaration      dec;
1.70      quint     427:   Record           rec, first;
1.52      vatton    428:        int              nbitems, size;
                    429:        struct menuType *items;
                    430:   char            *types, *menuString;
1.77      vatton    431:   View            view;
                    432: 
                    433:   TtaGetActiveView (&doc, &view);
                    434:   if (view != 1)
                    435:     return FALSE; /* let Thot perform normal operation */
1.70      quint     436: 
                    437:        doc = event->document;
1.76      vatton    438:   t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.67      quint     439:   if (!t)
1.77      vatton    440:     return FALSE; /* let Thot perform normal operation */
1.67      quint     441: 
1.79      quint     442:        el = event->element;
                    443:   if (TtaGetFirstChild (el))
                    444:     /* this Use element has already some content. Do not do anything */
                    445:     return FALSE; /* let Thot perform normal operation */
                    446: 
                    447:        elType = TtaGetElementType (el);
1.76      vatton    448:   // give the list of possible items
                    449:        attributeType.AttrSSchema = elType.ElSSchema;
                    450:        attributeType.AttrTypeNum = Template_ATTR_types;
                    451:        att = TtaGetAttribute (el, attributeType);
                    452:        size = TtaGetTextAttributeLength (att);
1.52      vatton    453:        types = (char *) TtaGetMemory (size+1); 
1.76      vatton    454:        TtaGiveTextAttributeValue (att, types, &size);
1.52      vatton    455:        giveItems (types, size, &items, &nbitems);
1.70      quint     456:        TtaFreeMemory (types);
                    457: 
1.67      quint     458:   if (nbitems == 1)
                    459:     {
1.76      vatton    460:       dec = GetDeclaration (t, items[0].label);
1.69      quint     461:       /* if it's a union, display the menu of this union */
1.70      quint     462:       if (dec)
1.76      vatton    463:         switch (dec->nature)
1.70      quint     464:           {
                    465:           case SimpleTypeNat :
                    466:             nbitems = 0;
                    467:             break;
                    468:           case XmlElementNat :
                    469:             nbitems = 0;
                    470:             break;
                    471:           case ComponentNat :
                    472:             nbitems = 0;
                    473:             break;
                    474:           case UnionNat :
                    475:             first = dec->unionType.include->first;
                    476:             rec = first;
                    477:             /* count the number of elements in the union */
                    478:             nbitems = 0;
                    479:             while (rec)
                    480:               {
                    481:                 nbitems++;
                    482:                 rec = rec->next;
                    483:               }
                    484:             if (nbitems > 0)
                    485:               {
1.76      vatton    486:                 items = (menuType*) TtaGetMemory (sizeof (struct menuType)* nbitems);
1.70      quint     487:                 rec = first;
                    488:                 nbitems = 0;
                    489:                 while (rec)
                    490:                   {
1.76      vatton    491:                     items[nbitems].label = (char *) TtaStrdup (rec->key);
1.70      quint     492:                     items[nbitems].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
                    493:                     nbitems++;
                    494:                     rec = rec->next;
                    495:                   }
                    496:               }
                    497:             break;
                    498:           default :
                    499:             //Impossible
                    500:             break;   
                    501:           }
                    502:     }
                    503:   if (nbitems > 0)
                    504:     {
1.80      vatton    505:       TtaCancelSelection (doc);
1.70      quint     506:       menuString = createMenuString (items, nbitems);
                    507:       TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1),
                    508:                          NULL, nbitems, menuString , NULL, false, 'L');
                    509:       TtaFreeMemory (menuString);
1.74      vatton    510:       ReturnOption = -1; // no selection yet
1.70      quint     511:       TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
1.76      vatton    512:       TtaWaitShowProcDialogue ();
1.70      quint     513:       TtaDestroyDialogue (BaseDialog + OptionMenu);
1.75      quint     514:       if (ReturnOption != -1)
1.76      vatton    515:         dec = GetDeclaration (t, items[ReturnOption].label);
1.70      quint     516:       TtaFreeMemory (items);
1.75      quint     517:       if (ReturnOption == -1)
                    518:         return FALSE;
1.70      quint     519:       if (dec)
1.67      quint     520:         {
1.76      vatton    521:           switch (dec->nature)
1.70      quint     522:             {
                    523:             case SimpleTypeNat :
                    524:               /* @@@@@ */
                    525:               break;
                    526:             case XmlElementNat :
                    527:               /* @@@@@ */
                    528:               break;
                    529:             case ComponentNat :
                    530:               /* copy element dec->componentType.content */
                    531:               comp = TtaCopyTree (dec->componentType.content, doc, doc, el);
                    532:               TtaInsertFirstChild (&comp, el, doc);
1.80      vatton    533:               el = comp;
1.70      quint     534:               /* @@@@@ */
                    535:               break;
                    536:             case UnionNat :
                    537:               /* @@@@@ */
                    538:               break;
                    539:             default :
                    540:               //Impossible
                    541:               break;   
                    542:             }
1.67      quint     543:         }
                    544:     }
1.80      vatton    545:   TtaSelectElement (doc, el);
1.77      vatton    546:   return TRUE;
1.63      vatton    547: #endif /* TEMPLATES */
                    548:        return TRUE;
                    549: }
1.42      tollenae  550: 
1.63      vatton    551: /*----------------------------------------------------------------------
1.79      quint     552:   OptionButtonClicked
1.63      vatton    553:   ----------------------------------------------------------------------*/
1.79      quint     554: ThotBool OptionButtonClicked (NotifyElement *event)
1.63      vatton    555: {
                    556: #ifdef TEMPLATES
1.75      quint     557:   Element         child, grandChild, next;
                    558:   ElementType     elType, elType1;
                    559:   Document        doc;
                    560:   XTigerTemplate  t;
1.77      vatton    561:   View            view;
1.75      quint     562: 
1.77      vatton    563:   TtaGetActiveView (&doc, &view);
                    564:   if (view != 1)
                    565:     return FALSE; /* let Thot perform normal operation */
1.75      quint     566:   doc = event->document;
                    567:   child = TtaGetFirstChild (event->element);
                    568:   if (!child)
1.77      vatton    569:     return FALSE; /* let Thot perform normal operation */
1.75      quint     570:   elType = TtaGetElementType (child);
                    571:   elType1 = TtaGetElementType (event->element);
1.76      vatton    572:   if ((elType.ElTypeNum != Template_EL_useEl &&
                    573:        elType.ElTypeNum != Template_EL_useSimple) ||
1.75      quint     574:       elType.ElSSchema != elType1.ElSSchema)
                    575:     return FALSE;
1.80      vatton    576: 
                    577:   TtaCancelSelection (doc);
1.75      quint     578:   grandChild = TtaGetFirstChild (child);
                    579:   if (!grandChild)
1.76      vatton    580:     /* the "use" element is empty. Instantiate it */
1.75      quint     581:     {
1.76      vatton    582:       t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.75      quint     583:       if (!t)
                    584:         return FALSE; // no template ?!?!
1.76      vatton    585:       InstantiateUse (t, child, doc, TRUE);
1.75      quint     586:     }
                    587:   else
                    588:     /* remove the content of the "use" element */
                    589:     {
                    590:       do
                    591:         {
                    592:           next = grandChild;
                    593:           TtaNextSibling (&next);
                    594:           TtaDeleteTree (grandChild, doc);
                    595:           grandChild = next;
                    596:         }
                    597:       while (next);
                    598:     }
1.80      vatton    599:   TtaSelectElement (doc, event->element);
1.77      vatton    600:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc  601: #endif /* TEMPLATES */
1.52      vatton    602:        return TRUE;
1.42      tollenae  603: }
1.51      francesc  604: 
1.56      francesc  605: /*----------------------------------------------------------------------
1.79      quint     606:   RepeatButtonClicked
1.56      francesc  607:   Shows a menu with all the types that can be used in a use element.
                    608:   ----------------------------------------------------------------------*/
1.79      quint     609: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56      francesc  610: {
                    611: #ifdef TEMPLATES
1.70      quint     612:   XTigerTemplate   t;
                    613:        Document         doc;
                    614:   Element          el, child, newEl;
                    615:   ElementType      elt, elt1;
1.63      vatton    616:        int              nbitems, size;
                    617:        struct menuType *items;
                    618:   char            *types, *menuString;
1.78      quint     619:   ThotBool          oldStructureChecking;
1.77      vatton    620:   View            view;
                    621: 
                    622:   TtaGetActiveView (&doc, &view);
                    623:   if (view != 1)
                    624:     return FALSE; /* let Thot perform normal operation */
1.70      quint     625:   doc = event->document;
1.76      vatton    626:   t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.70      quint     627:   if (!t)
1.78      quint     628:     return FALSE; // no template ?!?!
1.80      vatton    629: 
                    630:   TtaCancelSelection (doc);
1.78      quint     631:        types = "begining end"; 
1.63      vatton    632:        size = strlen (types);
                    633:        giveItems (types, size, &items, &nbitems);
                    634:        menuString = createMenuString (items, nbitems);
                    635:        TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    636:                      nbitems, menuString , NULL, false, 'L');
                    637:        TtaFreeMemory (menuString);
1.74      vatton    638:   ReturnOption = -1; // no selection yet
1.63      vatton    639:        TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
1.76      vatton    640:        TtaWaitShowProcDialogue ();
1.63      vatton    641:        TtaDestroyDialogue (BaseDialog + OptionMenu);
1.82      vatton    642:   TtaFreeMemory (items);
1.80      vatton    643:   el = event->element;
1.70      quint     644:   if (ReturnOption == 0 || ReturnOption == 1)
                    645:     {
                    646:       child = TtaGetFirstChild (el);
                    647:       if (child)
                    648:         {
                    649:           elt = TtaGetElementType (el);
                    650:           elt1 = TtaGetElementType (child);
1.78      quint     651:           if (elt.ElSSchema == elt1.ElSSchema)
1.70      quint     652:             {
1.78      quint     653:               if (elt1.ElTypeNum == Template_EL_useEl ||
                    654:                   elt1.ElTypeNum == Template_EL_useSimple)
                    655:                 newEl = InstantiateUse (t, child, doc, FALSE);
                    656:               else if (elt1.ElTypeNum == Template_EL_folder)
                    657:                 newEl = TtaCopyTree (child, doc, doc, el);
                    658:               else
                    659:                 newEl = NULL;
1.70      quint     660:               if (newEl)
                    661:                 {
1.78      quint     662:                   oldStructureChecking = TtaGetStructureChecking (doc);
                    663:                   TtaSetStructureChecking (FALSE, doc);
1.70      quint     664:                   if (ReturnOption == 0)
                    665:                     TtaInsertFirstChild (&newEl, el, doc);
                    666:                   else
                    667:                     {
                    668:                       child = TtaGetLastChild (el);
                    669:                       TtaInsertSibling (newEl, child, FALSE, doc);
1.80      vatton    670:                       el = newEl;
1.70      quint     671:                     }
1.78      quint     672:                   TtaSetStructureChecking (oldStructureChecking, doc);
1.70      quint     673:                 }
                    674:             }
                    675:         }
                    676:     }
1.80      vatton    677:   TtaSelectElement (doc, el);
1.77      vatton    678:   return TRUE; /* don't let Thot perform normal operation */
1.57      francesc  679: #endif /* TEMPLATES */
1.56      francesc  680:        return TRUE;
                    681: }
1.64      francesc  682: 
1.66      vatton    683: /*----------------------------------------------------------------------
                    684:   ----------------------------------------------------------------------*/
1.76      vatton    685: void OpeningInstance (char *fileName, Document doc)
1.65      francesc  686: {
                    687: #ifdef TEMPLATES
1.76      vatton    688:   XTigerTemplate   t;
                    689:   char            *content, *ptr;
                    690:   gzFile           stream;
                    691:   char             buffer[2000];
1.77      vatton    692:   int              res;
1.65      francesc  693: 
1.76      vatton    694:   stream = TtaGZOpen (fileName);
                    695:   if (stream != 0)
1.65      francesc  696:     {
1.76      vatton    697:       res = gzread (stream, buffer, 1999);
                    698:       if (res >= 0)
1.65      francesc  699:         {
1.81      vatton    700:           buffer[res] = EOS;
1.76      vatton    701:           ptr = strstr (buffer, "<?xtiger");
                    702:           if (ptr)
                    703:             ptr = strstr (ptr, "template");
                    704:           if (ptr)
                    705:             ptr = strstr (ptr, "=");
                    706:           if (ptr)
                    707:             ptr = strstr (ptr, "\"");
                    708:           if (ptr)
                    709:             {
                    710:               // template URI
                    711:               content = &ptr[1];
                    712:               ptr = strstr (content, "\"");
                    713:             }
                    714:           if (ptr)
                    715:             {
                    716:               *ptr = EOS;
                    717:               //Get now the template URI
                    718:               DocumentMeta[doc]->template_url = TtaStrdup (content);
                    719:               if (Templates_Dic == NULL)
                    720:                 InitializeTemplateEnvironment ();
                    721:               t = (XTigerTemplate) Get (Templates_Dic, content);
                    722:               if (!t)
                    723:                 {
                    724:                   LoadTemplate (0, content);
                    725:                   t = (XTigerTemplate) Get (Templates_Dic, content);
                    726:                 }
                    727:               AddUser (t);
                    728:             }
1.65      francesc  729:         }
                    730:     }
1.76      vatton    731:   TtaGZClose (stream);
1.65      francesc  732: #endif /* TEMPLATES */
                    733: }
                    734: 
1.64      francesc  735: /*----------------------------------------------------------------------
1.65      francesc  736:   ClosingInstance
1.64      francesc  737:   Callback called before closing a document. Checks for unused templates.
                    738:   ----------------------------------------------------------------------*/
1.65      francesc  739: ThotBool ClosingInstance(NotifyDialog* dialog)
1.64      francesc  740: {
1.65      francesc  741: #ifdef TEMPLATES
                    742:   //If it is a template all has been already freed
1.76      vatton    743:   if (DocumentMeta[dialog->document] == NULL)
                    744:     return FALSE;
1.65      francesc  745: 
                    746:   char *turl = DocumentMeta[dialog->document]->template_url;
1.73      vatton    747:   if (turl)
1.65      francesc  748:     {
1.76      vatton    749:       XTigerTemplate t = (XTigerTemplate) Get (Templates_Dic, turl);
1.73      vatton    750:       if (t)
1.76      vatton    751:         RemoveUser (t);
                    752:       TtaFreeMemory (turl);
1.65      francesc  753:     }
                    754: #endif /* TEMPLATES */
                    755:   return FALSE;
1.64      francesc  756: }

Webmaster