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

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.67      quint      22: #include "templateLoad_f.h"
                     23: #include "templateInstanciation_f.h"
                     24: #include "templateDeclarations_f.h"
1.28      tollenae   25: #include "appdialogue_wx.h"
1.29      tollenae   26: #include "init_f.h"
1.46      vatton     27: #include "wxdialogapi_f.h"
1.60      francesc   28: #include "AHTURLTools_f.h"
                     29: 
1.52      vatton     30: #endif /* TEMPLATES */
1.1       cvs        31: 
                     32: /*----------------------------------------------------------------------
1.51      francesc   33:   NewTemplate: Create the "new document from template" dialog
1.1       cvs        34:   ----------------------------------------------------------------------*/
1.18      cvs        35: void NewTemplate (Document doc, View view)
1.1       cvs        36: {
1.51      francesc   37: #ifdef TEMPLATES
1.52      vatton     38:   char *templateDir = TtaGetEnvString ("TEMPLATES_DIRECTORY");
                     39:   ThotBool created;
1.28      tollenae   40: 
1.52      vatton     41:   if (templates == NULL)
1.58      francesc   42:     InitializeTemplateEnvironment();
1.52      vatton     43:   created = CreateNewTemplateDocDlgWX(BaseDialog + OpenTemplate,
1.61      francesc   44:                                       /*TtaGetViewFrame (doc, view)*/NULL, doc,
1.52      vatton     45:                                       TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),templateDir);
1.51      francesc   46:   
1.28      tollenae   47:   if (created)
1.25      vatton     48:     {
1.28      tollenae   49:       TtaSetDialoguePosition ();
                     50:       TtaShowDialogue (BaseDialog + OpenTemplate, TRUE);
1.25      vatton     51:     }
1.51      francesc   52: 
1.52      vatton     53: #endif /* TEMPLATES */
1.1       cvs        54: }
1.25      vatton     55: 
1.53      vatton     56: /*----------------------------------------------------------------------
                     57:   Load a template and create the instance file - update images and 
                     58:   stylesheets related to the template.
                     59:   ----------------------------------------------------------------------*/
1.61      francesc   60: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
1.53      vatton     61: {
                     62: #ifdef TEMPLATES
                     63: 
1.60      francesc   64:   char *s;
1.65      francesc   65:   ThotBool dontReplace = DontReplaceOldDoc;
1.60      francesc   66: 
                     67:   if (!IsW3Path (docname) && TtaFileExist (docname))
                     68:     {
                     69:       s = (char *)TtaGetMemory (strlen (docname) +
                     70:                                 strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
                     71:       sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
                     72:       InitConfirm (0, 0, s);
1.61      francesc   73:       TtaFreeMemory (s);      
1.62      francesc   74:       if (!UserAnswer)
                     75:         return;
                     76:     }    
                     77: 
1.65      francesc   78:   LoadTemplate(0, templatename);
                     79:   DontReplaceOldDoc = dontReplace;
1.62      francesc   80:   CreateInstance(templatename, docname);
                     81:   
1.53      vatton     82: #endif /* TEMPLATES */
                     83: }
                     84: 
1.51      francesc   85: #ifdef TEMPLATES
1.52      vatton     86: /*----------------------------------------------------------------------
                     87:   ----------------------------------------------------------------------*/
                     88: static void giveItems(char *text, int size, struct menuType **items, int *nbitems)
1.1       cvs        89: {
1.52      vatton     90:        ThotBool         inElement = TRUE;
                     91:   struct menuType *menu;
                     92:   char            *iter;
                     93:        char             temp[128];
                     94:   int              i;
                     95:        int              labelSize;
1.28      tollenae   96: 
1.52      vatton     97:        *nbitems = 1;
                     98:        for (i = 0; i < size; i++)
                     99:     {
                    100:       if (isEOSorWhiteSpace (text[i]))
                    101:         {
                    102:           if (inElement)
                    103:             inElement = FALSE;
                    104:         }
                    105:       else if (!inElement)
                    106:         {
                    107:           inElement = TRUE;
                    108:           (*nbitems)++;
                    109:         }
                    110:     }
1.51      francesc  111: 
1.52      vatton    112:        menu = (struct menuType*) TtaGetMemory(sizeof(struct menuType)* *nbitems);
                    113:        iter = text;
                    114:        for (i = 0; i < *nbitems; i++)
                    115:     {          
                    116:       labelSize = 0;
                    117:       while (isEOSorWhiteSpace (*iter))
                    118:         iter++;
                    119: 
                    120:       while (!isEOSorWhiteSpace (*iter))
                    121:         {
                    122:           temp[labelSize++] = *iter;
                    123:           iter++;
                    124:         }
                    125: 
                    126:       temp[labelSize] = EOS;
                    127:       menu[i].label = (char *) TtaStrdup(temp);
1.68    ! quint     128:       menu[i].type = SimpleTypeNat;  /* @@@@@ ???? @@@@@ */
1.52      vatton    129:       *items = menu;
                    130:     }
1.28      tollenae  131: }
1.37      tollenae  132: 
1.52      vatton    133: /*----------------------------------------------------------------------
                    134:   ----------------------------------------------------------------------*/
                    135: static char *createMenuString (const struct menuType* items, const int nbItems)
                    136: {
                    137:   char *result, *iter;
                    138:        int   size = 0;
                    139:   int   i;
                    140: 
                    141:        for (i=0; i < nbItems; i++)
                    142:                size += 2 + strlen (items[i].label);
                    143: 
                    144:        result = (char *) TtaGetMemory(size);
                    145:        iter = result;
                    146:        for (i=0; i < nbItems; i++)
                    147:     {
                    148:       *iter = 'B';
                    149:       ++iter;
1.51      francesc  150:                
1.52      vatton    151:       strcpy (iter, items[i].label);
                    152:       iter += strlen (items[i].label)+1;
                    153:     }
1.51      francesc  154:        return result;
1.36      tollenae  155: }
1.29      tollenae  156: 
1.52      vatton    157: #endif /* TEMPLATES */
1.29      tollenae  158: 
1.46      vatton    159: /*----------------------------------------------------------------------
1.63      vatton    160:   UseMenuClicked
1.51      francesc  161:   Shows a menu with all the types that can be used in a use element.
1.46      vatton    162:   ----------------------------------------------------------------------*/
1.56      francesc  163: ThotBool UseMenuClicked (NotifyElement *event)
1.39      tollenae  164: {
1.43      tollenae  165: #ifdef TEMPLATES
1.52      vatton    166:        Document         doc = event->document;
1.63      vatton    167:        Element          el = event->element;
1.52      vatton    168:        ElementType      elt = TtaGetElementType(el);
                    169:        Attribute        at;
                    170:        AttributeType    att;
1.67      quint     171:   XTigerTemplate   t;
                    172:   Declaration      dec;
1.52      vatton    173:        int              nbitems, size;
                    174:        struct menuType *items;
                    175:   char            *types, *menuString;
1.51      francesc  176:        
1.67      quint     177:   t = (XTigerTemplate) Get(templates, DocumentMeta[doc]->template_url);
                    178:   if (!t)
                    179:     return FALSE; // no template ?!?!
                    180: 
1.51      francesc  181:        att.AttrSSchema = elt.ElSSchema;
                    182:        att.AttrTypeNum = Template_ATTR_types;
1.52      vatton    183:        at = TtaGetAttribute (el, att);
1.51      francesc  184: 
1.52      vatton    185:        size = TtaGetTextAttributeLength (at);
                    186:        types = (char *) TtaGetMemory (size+1); 
                    187:        TtaGiveTextAttributeValue (at, types, &size);
1.39      tollenae  188: 
1.52      vatton    189:        giveItems (types, size, &items, &nbitems);
1.67      quint     190:   if (nbitems == 1)
                    191:     {
                    192:       dec = GetDeclaration(t, items[0].label);
                    193:       if (dec)
                    194:         /* if it's a union, display the menu of this union */
                    195:         {
                    196:           /* @@@@@@@ */
                    197:         }
                    198:     }
1.52      vatton    199:        menuString = createMenuString (items, nbitems);
1.51      francesc  200:        TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
1.52      vatton    201:                      nbitems, menuString , NULL, false, 'L');
1.46      vatton    202: 
1.51      francesc  203:        TtaFreeMemory (menuString);
                    204:        TtaFreeMemory (types);
1.29      tollenae  205: 
1.51      francesc  206:        TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    207:        TtaWaitShowProcDialogue();
                    208:        TtaDestroyDialogue (BaseDialog + OptionMenu);
1.67      quint     209:   
                    210:   /* result: items[ReturnOption].label @@@@@ */
                    211:   dec = GetDeclaration(t, items[ReturnOption].label);
                    212:   if (dec)
                    213:     {
                    214:       size = 1; /* @@@@ */
                    215:     }
                    216:   TtaFreeMemory (items);
1.63      vatton    217:   return FALSE;
                    218: #endif /* TEMPLATES */
                    219:        //ReturnOption
                    220:        return TRUE;
                    221: }
1.42      tollenae  222: 
1.63      vatton    223: /*----------------------------------------------------------------------
                    224:   OptionMenuClicked
                    225:   ----------------------------------------------------------------------*/
                    226: ThotBool OptionMenuClicked (NotifyElement *event)
                    227: {
                    228: #ifdef TEMPLATES
                    229:   return FALSE;
1.57      francesc  230: #endif /* TEMPLATES */
1.52      vatton    231:        return TRUE;
1.42      tollenae  232: }
1.51      francesc  233: 
1.56      francesc  234: /*----------------------------------------------------------------------
1.63      vatton    235:   RepeatMenuClicked
1.56      francesc  236:   Shows a menu with all the types that can be used in a use element.
                    237:   ----------------------------------------------------------------------*/
1.63      vatton    238: ThotBool RepeatMenuClicked (NotifyElement *event)
1.56      francesc  239: {
                    240: #ifdef TEMPLATES
1.63      vatton    241:        Document         doc = event->document;
                    242:        int              nbitems, size;
                    243:        struct menuType *items;
                    244:   char            *types, *menuString;
                    245: 
                    246:        types = "top end";      
                    247:        size = strlen (types);
                    248:        giveItems (types, size, &items, &nbitems);
                    249:        menuString = createMenuString (items, nbitems);
                    250:        TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 
                    251:                      nbitems, menuString , NULL, false, 'L');
                    252:        TtaFreeMemory (menuString);
                    253:        TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
                    254:        TtaWaitShowProcDialogue();
                    255:        TtaDestroyDialogue (BaseDialog + OptionMenu);
                    256:   return FALSE;
1.57      francesc  257: #endif /* TEMPLATES */
1.56      francesc  258:        return TRUE;
                    259: }
1.64      francesc  260: 
1.66      vatton    261: /*----------------------------------------------------------------------
                    262:   ----------------------------------------------------------------------*/
1.65      francesc  263: void OpeningInstance(Document doc)
                    264: {
                    265: #ifdef TEMPLATES
                    266:   char            aux[MAX_LENGTH], content[MAX_LENGTH];
                    267:        ElementType               piType, lineType, textType;
                    268:        Element                     root, pi, line, text;
                    269:   Language        language;
                    270:        char                    *s;
                    271:   int             size;
                    272:   
1.66      vatton    273:   if (DocumentURLs[doc] == NULL)
                    274:     return;
1.65      francesc  275:   //If it is a template we must ignore it
1.66      vatton    276:   strcpy (aux, DocumentURLs[doc]);
                    277:   strcpy (content, &aux[strlen(aux)-4]);
                    278:   if(strncasecmp (content, ".XTD", strlen(content))==0)
                    279:     return;
1.65      francesc  280: 
1.66      vatton    281:   content[0] = EOS;
1.65      francesc  282:        //Instanciate all elements
1.66      vatton    283:        root =  TtaGetMainRoot (doc);
1.65      francesc  284:   //Look for PIs
                    285:   /* check if the document has a DOCTYPE declaration */
                    286: #ifdef ANNOTATIONS
                    287:   if (DocumentTypes[doc]  == docAnnot)
                    288:     piType = TtaGetElementType (root);
                    289:   else
                    290: #endif /* ANNOTATIONS */
                    291:     piType = TtaGetElementType (root);
                    292:   
                    293:   lineType.ElSSchema = piType.ElSSchema;
                    294:   textType.ElSSchema = piType.ElSSchema;
                    295:   
                    296:   s = TtaGetSSchemaName (piType.ElSSchema);
                    297:   
                    298:   if (strcmp (s, "HTML") == 0)
                    299:     {
                    300:       piType.ElTypeNum = HTML_EL_XMLPI;  
                    301:       lineType.ElTypeNum = HTML_EL_PI_line;
                    302:       textType.ElTypeNum = HTML_EL_TEXT_UNIT;
                    303:     }
                    304: #ifdef _SVG
                    305:   else if (strcmp (s, "SVG") == 0)    
                    306:     {
                    307:       piType.ElTypeNum = SVG_EL_XMLPI;  
                    308:       lineType.ElTypeNum = SVG_EL_XMLPI_line;
                    309:       textType.ElTypeNum = SVG_EL_TEXT_UNIT;
                    310:     }
                    311: #endif /* _SVG */
                    312:   else if (strcmp (s, "MathML") == 0)
                    313:     {
                    314:       piType.ElTypeNum = MathML_EL_XMLPI;  
                    315:       lineType.ElTypeNum = MathML_EL_XMLPI_line;
                    316:       textType.ElTypeNum = MathML_EL_TEXT_UNIT;
                    317:     }
                    318:   else
                    319:     {
                    320:       piType.ElTypeNum = XML_EL_xmlpi;
                    321:       lineType.ElTypeNum = XML_EL_xmlpi_line;
                    322:       textType.ElTypeNum = XML_EL_TEXT_UNIT;
                    323:     }
                    324:   
                    325:   pi = TtaSearchTypedElement (piType, SearchInTree, root);  
                    326:   while(pi!=NULL)
                    327:     {
                    328:       content[0] = '\0';
                    329:       line = TtaSearchTypedElement (lineType, SearchInTree, pi);
                    330:       while(line!=NULL)
                    331:         {
                    332:           text = TtaSearchTypedElement (textType, SearchInTree, line);
                    333:           size = MAX_LENGTH;
                    334:           TtaGiveTextContent(text, (unsigned char*)aux, &size, &language);
                    335:           strcat(content, aux);
                    336: 
                    337:           //This is not an XTiger PI
                    338:           if(!strstr(content,"xtiger")) break;            
                    339:  
                    340:           line = TtaSearchTypedElement (lineType, SearchForward, line);
                    341:         }
                    342:       pi = TtaSearchTypedElement (piType, SearchForward, pi);
                    343:     }
                    344: 
                    345:   DocumentMeta[doc]->template_url = NULL;
                    346: 
                    347:   if(content[0]=='\0')
                    348:     return;
                    349:     
                    350:   char *pointer;
                    351:   
                    352:   //xtiger
                    353:   strcpy(aux, content);
                    354:   aux[6]='\0';
                    355:   if(strcmp(aux,"xtiger")!=0)
                    356:     return;
                    357:   
                    358:   //template
                    359:   pointer = strstr(content, "template");
                    360:   if(pointer==NULL)
                    361:     return;
                    362: 
                    363:   //=
                    364:   pointer = strstr(pointer, "=");
                    365:   if(pointer==NULL)
                    366:     return;
                    367:   
                    368:   //"
                    369:   pointer = strstr(pointer, "\"");
                    370:   if(pointer==NULL)
                    371:     return;
                    372:   
                    373:   //content
                    374:   strcpy(aux, pointer+1);
                    375:   pointer = strstr(aux, "\"");
                    376:   if(pointer==NULL)
                    377:     return;
                    378:   *pointer = '\0';
                    379:   
                    380:   //and finally
                    381:   DocumentMeta[doc]->template_url = TtaStrdup(aux);
                    382: 
                    383:   if(!templates) InitializeTemplateEnvironment();
                    384: 
                    385:   XTigerTemplate t = (XTigerTemplate)Get(templates, aux);
                    386: 
                    387:   if(!t)
                    388:     {
                    389:       LoadTemplate(0, aux);
                    390:       t = (XTigerTemplate)Get(templates, aux);
                    391:     }
                    392:   AddUser(t);
                    393: 
                    394: #endif /* TEMPLATES */
                    395: }
                    396: 
1.64      francesc  397: /*----------------------------------------------------------------------
1.65      francesc  398:   ClosingInstance
1.64      francesc  399:   Callback called before closing a document. Checks for unused templates.
                    400:   ----------------------------------------------------------------------*/
1.65      francesc  401: ThotBool ClosingInstance(NotifyDialog* dialog)
1.64      francesc  402: {
1.65      francesc  403: #ifdef TEMPLATES
                    404:   //If it is a template all has been already freed
                    405:   if(DocumentMeta[dialog->document] == NULL) return FALSE;
                    406: 
                    407:   char *turl = DocumentMeta[dialog->document]->template_url;
                    408:   if(turl)
                    409:     {
                    410:       XTigerTemplate t = (XTigerTemplate)Get(templates, turl);
                    411:       if(t)
                    412:         RemoveUser(t);
                    413:       TtaFreeMemory(turl);
                    414:     }
                    415: #endif /* TEMPLATES */
                    416:   return FALSE;
1.64      francesc  417: }

Webmaster