Diff for /Amaya/amaya/templates.c between versions 1.75 and 1.76

version 1.75, 2006/09/15 16:14:07 version 1.76, 2006/09/28 07:50:59
Line 21 Line 21
   
 #include "mydictionary_f.h"  #include "mydictionary_f.h"
 #include "templateLoad_f.h"  #include "templateLoad_f.h"
 #include "templateInstanciation_f.h"  
 #include "templateDeclarations_f.h"  #include "templateDeclarations_f.h"
   #include "templateInstantiate_f.h"
 #include "appdialogue_wx.h"  #include "appdialogue_wx.h"
 #include "init_f.h"  #include "init_f.h"
 #include "wxdialogapi_f.h"  #include "wxdialogapi_f.h"
Line 36 Line 36
 void NewTemplate (Document doc, View view)  void NewTemplate (Document doc, View view)
 {  {
 #ifdef TEMPLATES  #ifdef TEMPLATES
   char *templateDir = TtaGetEnvString ("TEMPLATES_DIRECTORY");    char        *templateDir = TtaGetEnvString ("TEMPLATES_DIRECTORY");
   ThotBool created;    ThotBool     created;
   
   if (templates == NULL)    if (Templates_Dic == NULL)
     InitializeTemplateEnvironment();      InitializeTemplateEnvironment ();
   created = CreateNewTemplateDocDlgWX(BaseDialog + OpenTemplate,    created = CreateNewTemplateDocDlgWX (BaseDialog + OpenTemplate,
                                       /*TtaGetViewFrame (doc, view)*/NULL, doc,                                        /*TtaGetViewFrame (doc, view)*/NULL, doc,
                                       TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),templateDir);                                        TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),templateDir);
       
Line 76  void CreateInstanceOfTemplate (Document Line 76  void CreateInstanceOfTemplate (Document
         return;          return;
     }          }    
   
   LoadTemplate(0, templatename);    LoadTemplate (0, templatename);
   DontReplaceOldDoc = dontReplace;    DontReplaceOldDoc = dontReplace;
   CreateInstance(templatename, docname);    CreateInstance (templatename, docname);
       
 #endif /* TEMPLATES */  #endif /* TEMPLATES */
 }  }
   
 /*----------------------------------------------------------------------  /*----------------------------------------------------------------------
   ----------------------------------------------------------------------*/    ----------------------------------------------------------------------*/
 void giveItems(char *text, int size, struct menuType **items, int *nbitems)  void giveItems (char *text, int size, struct menuType **items, int *nbitems)
 {  {
 #ifdef TEMPLATES  #ifdef TEMPLATES
         ThotBool         inElement = TRUE;          ThotBool         inElement = TRUE;
Line 110  void giveItems(char *text, int size, str Line 110  void giveItems(char *text, int size, str
         }          }
     }      }
   
         menu = (struct menuType*) TtaGetMemory(sizeof(struct menuType)* *nbitems);          menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
         iter = text;          iter = text;
         for (i = 0; i < *nbitems; i++)          for (i = 0; i < *nbitems; i++)
     {                 {           
Line 125  void giveItems(char *text, int size, str Line 125  void giveItems(char *text, int size, str
         }          }
   
       temp[labelSize] = EOS;        temp[labelSize] = EOS;
       menu[i].label = (char *) TtaStrdup(temp);        menu[i].label = (char *) TtaStrdup (temp);
       menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */        menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
       *items = menu;        *items = menu;
     }      }
Line 144  static char *createMenuString (const str Line 144  static char *createMenuString (const str
         for (i=0; i < nbItems; i++)          for (i=0; i < nbItems; i++)
                 size += 2 + strlen (items[i].label);                  size += 2 + strlen (items[i].label);
   
         result = (char *) TtaGetMemory(size);          result = (char *) TtaGetMemory (size);
         iter = result;          iter = result;
         for (i=0; i < nbItems; i++)          for (i=0; i < nbItems; i++)
     {      {
Line 195  void UseCreated (NotifyElement *event) Line 195  void UseCreated (NotifyElement *event)
     /* this Use element has already some content. It has already been      /* this Use element has already some content. It has already been
        instanciated */         instanciated */
     return;      return;
   t = (XTigerTemplate) Get(templates, DocumentMeta[doc]->template_url);    t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url);
   if (!t)    if (!t)
     return; // no template ?!?!      return; // no template ?!?!
   InstanciateUse (t, el, doc, TRUE);    InstantiateUse (t, el, doc, TRUE);
 #endif /* TEMPLATES */  #endif /* TEMPLATES */
 }  }
   
Line 211  ThotBool UseMenuClicked (NotifyElement * Line 211  ThotBool UseMenuClicked (NotifyElement *
 #ifdef TEMPLATES  #ifdef TEMPLATES
         Document         doc;          Document         doc;
         Element          el, comp;          Element          el, comp;
         ElementType      elt;          ElementType      elType;
         Attribute        at;          Attribute        att;
         AttributeType    att;          AttributeType    attributeType;
   XTigerTemplate   t;    XTigerTemplate   t;
   Declaration      dec;    Declaration      dec;
   Record           rec, first;    Record           rec, first;
Line 223  ThotBool UseMenuClicked (NotifyElement * Line 223  ThotBool UseMenuClicked (NotifyElement *
   
         doc = event->document;          doc = event->document;
         el = event->element;          el = event->element;
         elt = TtaGetElementType(el);          elType = TtaGetElementType (el);
   t = (XTigerTemplate) Get(templates, DocumentMeta[doc]->template_url);    t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url);
   if (!t)    if (!t)
     return FALSE; // no template ?!?!      return FALSE; // no template ?!?!
   
         att.AttrSSchema = elt.ElSSchema;    // give the list of possible items
         att.AttrTypeNum = Template_ATTR_types;          attributeType.AttrSSchema = elType.ElSSchema;
         at = TtaGetAttribute (el, att);          attributeType.AttrTypeNum = Template_ATTR_types;
           att = TtaGetAttribute (el, attributeType);
         size = TtaGetTextAttributeLength (at);          size = TtaGetTextAttributeLength (att);
         types = (char *) TtaGetMemory (size+1);           types = (char *) TtaGetMemory (size+1); 
         TtaGiveTextAttributeValue (at, types, &size);          TtaGiveTextAttributeValue (att, types, &size);
     
         giveItems (types, size, &items, &nbitems);          giveItems (types, size, &items, &nbitems);
         TtaFreeMemory (types);          TtaFreeMemory (types);
   
   if (nbitems == 1)    if (nbitems == 1)
     {      {
       dec = GetDeclaration(t, items[0].label);        dec = GetDeclaration (t, items[0].label);
       /* if it's a union, display the menu of this union */        /* if it's a union, display the menu of this union */
       if (dec)        if (dec)
         switch(dec->nature)          switch (dec->nature)
           {            {
           case SimpleTypeNat :            case SimpleTypeNat :
             nbitems = 0;              nbitems = 0;
Line 267  ThotBool UseMenuClicked (NotifyElement * Line 266  ThotBool UseMenuClicked (NotifyElement *
               }                }
             if (nbitems > 0)              if (nbitems > 0)
               {                {
                 items = (menuType*) TtaGetMemory(sizeof(struct menuType)* nbitems);                  items = (menuType*) TtaGetMemory (sizeof (struct menuType)* nbitems);
                 rec = first;                  rec = first;
                 nbitems = 0;                  nbitems = 0;
                 while (rec)                  while (rec)
                   {                    {
                     items[nbitems].label = (char *) TtaStrdup(rec->key);                      items[nbitems].label = (char *) TtaStrdup (rec->key);
                     items[nbitems].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */                      items[nbitems].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
                     nbitems++;                      nbitems++;
                     rec = rec->next;                      rec = rec->next;
Line 292  ThotBool UseMenuClicked (NotifyElement * Line 291  ThotBool UseMenuClicked (NotifyElement *
       TtaFreeMemory (menuString);        TtaFreeMemory (menuString);
       ReturnOption = -1; // no selection yet        ReturnOption = -1; // no selection yet
       TtaShowDialogue (BaseDialog + OptionMenu, FALSE);        TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
       TtaWaitShowProcDialogue();        TtaWaitShowProcDialogue ();
       TtaDestroyDialogue (BaseDialog + OptionMenu);        TtaDestroyDialogue (BaseDialog + OptionMenu);
       /* result: items[ReturnOption].label @@@@@ */        /* result: items[ReturnOption].label @@@@@ */
       if (ReturnOption != -1)        if (ReturnOption != -1)
         dec = GetDeclaration(t, items[ReturnOption].label);          dec = GetDeclaration (t, items[ReturnOption].label);
       TtaFreeMemory (items);        TtaFreeMemory (items);
       if (ReturnOption == -1)        if (ReturnOption == -1)
         return FALSE;          return FALSE;
       if (dec)        if (dec)
         {          {
           switch(dec->nature)            switch (dec->nature)
             {              {
             case SimpleTypeNat :              case SimpleTypeNat :
               /* @@@@@ */                /* @@@@@ */
Line 347  ThotBool OptionMenuClicked (NotifyElemen Line 346  ThotBool OptionMenuClicked (NotifyElemen
     return FALSE;      return FALSE;
   elType = TtaGetElementType (child);    elType = TtaGetElementType (child);
   elType1 = TtaGetElementType (event->element);    elType1 = TtaGetElementType (event->element);
   if (elType.ElTypeNum != Template_EL_useEl ||    if ((elType.ElTypeNum != Template_EL_useEl &&
          elType.ElTypeNum != Template_EL_useSimple) ||
       elType.ElSSchema != elType1.ElSSchema)        elType.ElSSchema != elType1.ElSSchema)
     return FALSE;      return FALSE;
   grandChild = TtaGetFirstChild (child);    grandChild = TtaGetFirstChild (child);
   if (!grandChild)    if (!grandChild)
     /* the "use" element is empty. Instanciate it */      /* the "use" element is empty. Instantiate it */
     {      {
       t = (XTigerTemplate) Get(templates, DocumentMeta[doc]->template_url);        t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url);
       if (!t)        if (!t)
         return FALSE; // no template ?!?!          return FALSE; // no template ?!?!
       InstanciateUse (t, child, doc, TRUE);        InstantiateUse (t, child, doc, TRUE);
     }      }
   else    else
     /* remove the content of the "use" element */      /* remove the content of the "use" element */
Line 392  ThotBool RepeatMenuClicked (NotifyElemen Line 392  ThotBool RepeatMenuClicked (NotifyElemen
   char            *types, *menuString;    char            *types, *menuString;
   
   doc = event->document;    doc = event->document;
   t = (XTigerTemplate) Get(templates, DocumentMeta[doc]->template_url);    t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url);
   if (!t)    if (!t)
     return FALSE; // no template ?!?!      return FALSE; // no template ?!?!
         types = "top end";                types = "top end";      
Line 404  ThotBool RepeatMenuClicked (NotifyElemen Line 404  ThotBool RepeatMenuClicked (NotifyElemen
         TtaFreeMemory (menuString);          TtaFreeMemory (menuString);
   ReturnOption = -1; // no selection yet    ReturnOption = -1; // no selection yet
         TtaShowDialogue (BaseDialog + OptionMenu, FALSE);          TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
         TtaWaitShowProcDialogue();          TtaWaitShowProcDialogue ();
         TtaDestroyDialogue (BaseDialog + OptionMenu);          TtaDestroyDialogue (BaseDialog + OptionMenu);
   if (ReturnOption == 0 || ReturnOption == 1)    if (ReturnOption == 0 || ReturnOption == 1)
     {      {
Line 415  ThotBool RepeatMenuClicked (NotifyElemen Line 415  ThotBool RepeatMenuClicked (NotifyElemen
           elt = TtaGetElementType (el);            elt = TtaGetElementType (el);
           elt1 = TtaGetElementType (child);            elt1 = TtaGetElementType (child);
           if (elt.ElSSchema == elt1.ElSSchema &&            if (elt.ElSSchema == elt1.ElSSchema &&
               elt1.ElTypeNum == Template_EL_useEl)                (elt1.ElTypeNum == Template_EL_useEl ||
                  elt1.ElTypeNum == Template_EL_useSimple))
             {              {
               newEl = InstanciateUse (t, child, doc, FALSE);                newEl = InstantiateUse (t, child, doc, FALSE);
               if (newEl)                if (newEl)
                 {                  {
                   if (ReturnOption == 0)                    if (ReturnOption == 0)
Line 438  ThotBool RepeatMenuClicked (NotifyElemen Line 439  ThotBool RepeatMenuClicked (NotifyElemen
   
 /*----------------------------------------------------------------------  /*----------------------------------------------------------------------
   ----------------------------------------------------------------------*/    ----------------------------------------------------------------------*/
 void OpeningInstance(Document doc)  void OpeningInstance (char *fileName, Document doc)
 {  {
 #ifdef TEMPLATES  #ifdef TEMPLATES
   char            aux[MAX_LENGTH], content[MAX_LENGTH];    XTigerTemplate   t;
         ElementType               piType, lineType, textType;    char            *content, *ptr;
         Element                     root, pi, line, text;    gzFile           stream;
   Language        language;    char             buffer[2000];
         char                    *s;    int              res, size;
   int             size;  
     
   if (DocumentURLs[doc] == NULL)  
     return;  
   //If it is a template we must ignore it  
   strcpy (aux, DocumentURLs[doc]);  
   strcpy (content, &aux[strlen(aux)-4]);  
   if (strncasecmp (content, ".XTD", strlen(content))==0)  
     return;  
   
   content[0] = EOS;    stream = TtaGZOpen (fileName);
         //Instanciate all elements    if (stream != 0)
         root =  TtaGetMainRoot (doc);  
   //Look for PIs  
   /* check if the document has a DOCTYPE declaration */  
 #ifdef ANNOTATIONS  
   if (DocumentTypes[doc]  == docAnnot)  
     piType = TtaGetElementType (root);  
   else  
 #endif /* ANNOTATIONS */  
     piType = TtaGetElementType (root);  
     
   lineType.ElSSchema = piType.ElSSchema;  
   textType.ElSSchema = piType.ElSSchema;  
     
   s = TtaGetSSchemaName (piType.ElSSchema);  
     
   if (strcmp (s, "HTML") == 0)  
     {  
       piType.ElTypeNum = HTML_EL_XMLPI;    
       lineType.ElTypeNum = HTML_EL_PI_line;  
       textType.ElTypeNum = HTML_EL_TEXT_UNIT;  
     }  
 #ifdef _SVG  
   else if (strcmp (s, "SVG") == 0)      
     {      {
       piType.ElTypeNum = SVG_EL_XMLPI;          res = gzread (stream, buffer, 1999);
       lineType.ElTypeNum = SVG_EL_XMLPI_line;        if (res >= 0)
       textType.ElTypeNum = SVG_EL_TEXT_UNIT;  
     }  
 #endif /* _SVG */  
   else if (strcmp (s, "MathML") == 0)  
     {  
       piType.ElTypeNum = MathML_EL_XMLPI;    
       lineType.ElTypeNum = MathML_EL_XMLPI_line;  
       textType.ElTypeNum = MathML_EL_TEXT_UNIT;  
     }  
   else  
     {  
       piType.ElTypeNum = XML_EL_xmlpi;  
       lineType.ElTypeNum = XML_EL_xmlpi_line;  
       textType.ElTypeNum = XML_EL_TEXT_UNIT;  
     }  
     
   pi = TtaSearchTypedElement (piType, SearchInTree, root);    
   while(pi!=NULL)  
     {  
       content[0] = '\0';  
       line = TtaSearchTypedElement (lineType, SearchInTree, pi);  
       while(line!=NULL)  
         {          {
           text = TtaSearchTypedElement (textType, SearchInTree, line);            ptr = strstr (buffer, "<?xtiger");
           size = MAX_LENGTH;            if (ptr)
           TtaGiveTextContent(text, (unsigned char*)aux, &size, &language);              ptr = strstr (ptr, "template");
           strcat(content, aux);            if (ptr)
               ptr = strstr (ptr, "=");
           //This is not an XTiger PI            if (ptr)
           if (!strstr(content,"xtiger")) break;                          ptr = strstr (ptr, "\"");
              if (ptr)
           line = TtaSearchTypedElement (lineType, SearchForward, line);              {
                 // template URI
                 content = &ptr[1];
                 ptr = strstr (content, "\"");
               }
             if (ptr)
               {
                 *ptr = EOS;
                 //Get now the template URI
                 DocumentMeta[doc]->template_url = TtaStrdup (content);
                 if (Templates_Dic == NULL)
                   InitializeTemplateEnvironment ();
                 t = (XTigerTemplate) Get (Templates_Dic, content);
                 if (!t)
                   {
                     LoadTemplate (0, content);
                     t = (XTigerTemplate) Get (Templates_Dic, content);
                   }
                 AddUser (t);
               }
         }          }
       pi = TtaSearchTypedElement (piType, SearchForward, pi);  
     }  
   
   DocumentMeta[doc]->template_url = NULL;  
   
   if (content[0]=='\0')  
     return;  
       
   char *pointer;  
     
   //xtiger  
   strcpy(aux, content);  
   aux[6]='\0';  
   if (strcmp(aux,"xtiger")!=0)  
     return;  
     
   //template  
   pointer = strstr(content, "template");  
   if (pointer==NULL)  
     return;  
   
   //=  
   pointer = strstr(pointer, "=");  
   if (pointer==NULL)  
     return;  
     
   //"  
   pointer = strstr(pointer, "\"");  
   if (pointer==NULL)  
     return;  
     
   //content  
   strcpy(aux, pointer+1);  
   pointer = strstr(aux, "\"");  
   if (pointer==NULL)  
     return;  
   *pointer = '\0';  
     
   //and finally  
   DocumentMeta[doc]->template_url = TtaStrdup(aux);  
   
   if (!templates) InitializeTemplateEnvironment();  
   
   XTigerTemplate t = (XTigerTemplate)Get(templates, aux);  
   
   if (!t)  
     {  
       LoadTemplate(0, aux);  
       t = (XTigerTemplate)Get(templates, aux);  
     }      }
   AddUser(t);    TtaGZClose (stream);
   
 #endif /* TEMPLATES */  #endif /* TEMPLATES */
 }  }
   
Line 580  ThotBool ClosingInstance(NotifyDialog* d Line 496  ThotBool ClosingInstance(NotifyDialog* d
 {  {
 #ifdef TEMPLATES  #ifdef TEMPLATES
   //If it is a template all has been already freed    //If it is a template all has been already freed
   if (DocumentMeta[dialog->document] == NULL) return FALSE;    if (DocumentMeta[dialog->document] == NULL)
       return FALSE;
   
   char *turl = DocumentMeta[dialog->document]->template_url;    char *turl = DocumentMeta[dialog->document]->template_url;
   if (turl)    if (turl)
     {      {
       XTigerTemplate t = (XTigerTemplate)Get(templates, turl);        XTigerTemplate t = (XTigerTemplate) Get (Templates_Dic, turl);
       if (t)        if (t)
         RemoveUser(t);          RemoveUser (t);
       TtaFreeMemory(turl);        TtaFreeMemory (turl);
     }      }
 #endif /* TEMPLATES */  #endif /* TEMPLATES */
   return FALSE;    return FALSE;

Removed from v.1.75  
changed lines
  Added in v.1.76


Webmaster