Annotation of Amaya/amaya/templates.c, revision 1.75
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 "templateInstanciation_f.h"
25: #include "templateDeclarations_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:
33: /*----------------------------------------------------------------------
1.51 francesc 34: NewTemplate: Create the "new document from template" dialog
1.1 cvs 35: ----------------------------------------------------------------------*/
1.18 cvs 36: void NewTemplate (Document doc, View view)
1.1 cvs 37: {
1.51 francesc 38: #ifdef TEMPLATES
1.52 vatton 39: char *templateDir = TtaGetEnvString ("TEMPLATES_DIRECTORY");
40: ThotBool created;
1.28 tollenae 41:
1.52 vatton 42: if (templates == NULL)
1.58 francesc 43: InitializeTemplateEnvironment();
1.52 vatton 44: created = CreateNewTemplateDocDlgWX(BaseDialog + OpenTemplate,
1.61 francesc 45: /*TtaGetViewFrame (doc, view)*/NULL, doc,
1.52 vatton 46: TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),templateDir);
1.51 francesc 47:
1.28 tollenae 48: if (created)
1.25 vatton 49: {
1.28 tollenae 50: TtaSetDialoguePosition ();
51: TtaShowDialogue (BaseDialog + OpenTemplate, TRUE);
1.25 vatton 52: }
1.51 francesc 53:
1.52 vatton 54: #endif /* TEMPLATES */
1.1 cvs 55: }
1.25 vatton 56:
1.53 vatton 57: /*----------------------------------------------------------------------
58: Load a template and create the instance file - update images and
59: stylesheets related to the template.
60: ----------------------------------------------------------------------*/
1.61 francesc 61: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
1.53 vatton 62: {
63: #ifdef TEMPLATES
64:
1.60 francesc 65: char *s;
1.65 francesc 66: ThotBool dontReplace = DontReplaceOldDoc;
1.60 francesc 67:
68: if (!IsW3Path (docname) && TtaFileExist (docname))
69: {
70: s = (char *)TtaGetMemory (strlen (docname) +
71: strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
72: sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
73: InitConfirm (0, 0, s);
1.61 francesc 74: TtaFreeMemory (s);
1.62 francesc 75: if (!UserAnswer)
76: return;
77: }
78:
1.65 francesc 79: LoadTemplate(0, templatename);
80: DontReplaceOldDoc = dontReplace;
1.62 francesc 81: CreateInstance(templatename, docname);
82:
1.53 vatton 83: #endif /* TEMPLATES */
84: }
85:
1.52 vatton 86: /*----------------------------------------------------------------------
87: ----------------------------------------------------------------------*/
1.70 quint 88: void giveItems(char *text, int size, struct menuType **items, int *nbitems)
1.1 cvs 89: {
1.70 quint 90: #ifdef TEMPLATES
1.52 vatton 91: ThotBool inElement = TRUE;
92: struct menuType *menu;
93: char *iter;
94: char temp[128];
95: int i;
96: int labelSize;
1.28 tollenae 97:
1.52 vatton 98: *nbitems = 1;
99: for (i = 0; i < size; i++)
100: {
101: if (isEOSorWhiteSpace (text[i]))
102: {
103: if (inElement)
104: inElement = FALSE;
105: }
106: else if (!inElement)
107: {
108: inElement = TRUE;
109: (*nbitems)++;
110: }
111: }
1.51 francesc 112:
1.52 vatton 113: menu = (struct menuType*) TtaGetMemory(sizeof(struct menuType)* *nbitems);
114: iter = text;
115: for (i = 0; i < *nbitems; i++)
116: {
117: labelSize = 0;
118: while (isEOSorWhiteSpace (*iter))
119: iter++;
120:
121: while (!isEOSorWhiteSpace (*iter))
122: {
123: temp[labelSize++] = *iter;
124: iter++;
125: }
126:
127: temp[labelSize] = EOS;
128: menu[i].label = (char *) TtaStrdup(temp);
1.68 quint 129: menu[i].type = SimpleTypeNat; /* @@@@@ ???? @@@@@ */
1.52 vatton 130: *items = menu;
131: }
1.70 quint 132: #endif /* TEMPLATES */
1.28 tollenae 133: }
1.37 tollenae 134:
1.70 quint 135: #ifdef TEMPLATES
1.52 vatton 136: /*----------------------------------------------------------------------
137: ----------------------------------------------------------------------*/
138: static char *createMenuString (const struct menuType* items, const int nbItems)
139: {
140: char *result, *iter;
141: int size = 0;
142: int i;
143:
144: for (i=0; i < nbItems; i++)
145: size += 2 + strlen (items[i].label);
146:
147: result = (char *) TtaGetMemory(size);
148: iter = result;
149: for (i=0; i < nbItems; i++)
150: {
151: *iter = 'B';
152: ++iter;
1.51 francesc 153:
1.52 vatton 154: strcpy (iter, items[i].label);
155: iter += strlen (items[i].label)+1;
156: }
1.51 francesc 157: return result;
1.36 tollenae 158: }
1.71 quint 159: #endif /* TEMPLATES */
1.29 tollenae 160:
1.71 quint 161: /*----------------------------------------------------------------------
162: UseToBeCreated
163: An new use element will be created by the user through some generic editing
164: command
165: -----------------------------------------------------------------------*/
166: ThotBool UseToBeCreated (NotifyElement *event)
167: {
168: #ifdef TEMPLATES
1.75 ! quint 169: Element el;
1.72 quint 170: Document doc;
171:
172: el = event->element;
173: doc = event->document;
174: /* is there a limit to the number of elements in the xt:repeat ? */
1.71 quint 175: /* @@@@@ */
1.52 vatton 176: #endif /* TEMPLATES */
1.71 quint 177: return FALSE; /* let Thot perform normal operation */
178: }
179:
180: /*----------------------------------------------------------------------
181: UseCreated
182: A new "use" element has just been created by the user with a generic editing
183: command.
184: -----------------------------------------------------------------------*/
185: void UseCreated (NotifyElement *event)
186: {
187: #ifdef TEMPLATES
188: Document doc;
189: Element el;
190: XTigerTemplate t;
191:
192: doc = event->document;
1.72 quint 193: el = event->element;
194: if (TtaGetFirstChild (el))
195: /* this Use element has already some content. It has already been
196: instanciated */
197: return;
1.71 quint 198: t = (XTigerTemplate) Get(templates, DocumentMeta[doc]->template_url);
199: if (!t)
200: return; // no template ?!?!
201: InstanciateUse (t, el, doc, TRUE);
202: #endif /* TEMPLATES */
203: }
1.29 tollenae 204:
1.46 vatton 205: /*----------------------------------------------------------------------
1.63 vatton 206: UseMenuClicked
1.51 francesc 207: Shows a menu with all the types that can be used in a use element.
1.46 vatton 208: ----------------------------------------------------------------------*/
1.56 francesc 209: ThotBool UseMenuClicked (NotifyElement *event)
1.39 tollenae 210: {
1.43 tollenae 211: #ifdef TEMPLATES
1.70 quint 212: Document doc;
213: Element el, comp;
214: ElementType elt;
1.52 vatton 215: Attribute at;
216: AttributeType att;
1.67 quint 217: XTigerTemplate t;
218: Declaration dec;
1.70 quint 219: Record rec, first;
1.52 vatton 220: int nbitems, size;
221: struct menuType *items;
222: char *types, *menuString;
1.70 quint 223:
224: doc = event->document;
225: el = event->element;
226: elt = TtaGetElementType(el);
1.67 quint 227: t = (XTigerTemplate) Get(templates, DocumentMeta[doc]->template_url);
228: if (!t)
229: return FALSE; // no template ?!?!
230:
1.51 francesc 231: att.AttrSSchema = elt.ElSSchema;
232: att.AttrTypeNum = Template_ATTR_types;
1.52 vatton 233: at = TtaGetAttribute (el, att);
1.51 francesc 234:
1.52 vatton 235: size = TtaGetTextAttributeLength (at);
236: types = (char *) TtaGetMemory (size+1);
237: TtaGiveTextAttributeValue (at, types, &size);
1.69 quint 238:
1.52 vatton 239: giveItems (types, size, &items, &nbitems);
1.70 quint 240: TtaFreeMemory (types);
241:
1.67 quint 242: if (nbitems == 1)
243: {
244: dec = GetDeclaration(t, items[0].label);
1.69 quint 245: /* if it's a union, display the menu of this union */
1.70 quint 246: if (dec)
247: switch(dec->nature)
248: {
249: case SimpleTypeNat :
250: nbitems = 0;
251: break;
252: case XmlElementNat :
253: nbitems = 0;
254: break;
255: case ComponentNat :
256: nbitems = 0;
257: break;
258: case UnionNat :
259: first = dec->unionType.include->first;
260: rec = first;
261: /* count the number of elements in the union */
262: nbitems = 0;
263: while (rec)
264: {
265: nbitems++;
266: rec = rec->next;
267: }
268: if (nbitems > 0)
269: {
270: items = (menuType*) TtaGetMemory(sizeof(struct menuType)* nbitems);
271: rec = first;
272: nbitems = 0;
273: while (rec)
274: {
275: items[nbitems].label = (char *) TtaStrdup(rec->key);
276: items[nbitems].type = SimpleTypeNat; /* @@@@@ ???? @@@@@ */
277: nbitems++;
278: rec = rec->next;
279: }
280: }
281: break;
282: default :
283: //Impossible
284: break;
285: }
286: }
287: if (nbitems > 0)
288: {
289: menuString = createMenuString (items, nbitems);
290: TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1),
291: NULL, nbitems, menuString , NULL, false, 'L');
292: TtaFreeMemory (menuString);
1.74 vatton 293: ReturnOption = -1; // no selection yet
1.70 quint 294: TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
295: TtaWaitShowProcDialogue();
296: TtaDestroyDialogue (BaseDialog + OptionMenu);
297: /* result: items[ReturnOption].label @@@@@ */
1.75 ! quint 298: if (ReturnOption != -1)
! 299: dec = GetDeclaration(t, items[ReturnOption].label);
1.70 quint 300: TtaFreeMemory (items);
1.75 ! quint 301: if (ReturnOption == -1)
! 302: return FALSE;
1.70 quint 303: if (dec)
1.67 quint 304: {
1.70 quint 305: switch(dec->nature)
306: {
307: case SimpleTypeNat :
308: /* @@@@@ */
309: break;
310: case XmlElementNat :
311: /* @@@@@ */
312: break;
313: case ComponentNat :
314: /* copy element dec->componentType.content */
315: comp = TtaCopyTree (dec->componentType.content, doc, doc, el);
316: TtaInsertFirstChild (&comp, el, doc);
317: /* @@@@@ */
318: break;
319: case UnionNat :
320: /* @@@@@ */
321: break;
322: default :
323: //Impossible
324: break;
325: }
1.67 quint 326: }
327: }
1.63 vatton 328: return FALSE;
329: #endif /* TEMPLATES */
330: return TRUE;
331: }
1.42 tollenae 332:
1.63 vatton 333: /*----------------------------------------------------------------------
334: OptionMenuClicked
335: ----------------------------------------------------------------------*/
336: ThotBool OptionMenuClicked (NotifyElement *event)
337: {
338: #ifdef TEMPLATES
1.75 ! quint 339: Element child, grandChild, next;
! 340: ElementType elType, elType1;
! 341: Document doc;
! 342: XTigerTemplate t;
! 343:
! 344: doc = event->document;
! 345: child = TtaGetFirstChild (event->element);
! 346: if (!child)
! 347: return FALSE;
! 348: elType = TtaGetElementType (child);
! 349: elType1 = TtaGetElementType (event->element);
! 350: if (elType.ElTypeNum != Template_EL_useEl ||
! 351: elType.ElSSchema != elType1.ElSSchema)
! 352: return FALSE;
! 353: grandChild = TtaGetFirstChild (child);
! 354: if (!grandChild)
! 355: /* the "use" element is empty. Instanciate it */
! 356: {
! 357: t = (XTigerTemplate) Get(templates, DocumentMeta[doc]->template_url);
! 358: if (!t)
! 359: return FALSE; // no template ?!?!
! 360: InstanciateUse (t, child, doc, TRUE);
! 361: }
! 362: else
! 363: /* remove the content of the "use" element */
! 364: {
! 365: do
! 366: {
! 367: next = grandChild;
! 368: TtaNextSibling (&next);
! 369: TtaDeleteTree (grandChild, doc);
! 370: grandChild = next;
! 371: }
! 372: while (next);
! 373: }
1.63 vatton 374: return FALSE;
1.57 francesc 375: #endif /* TEMPLATES */
1.52 vatton 376: return TRUE;
1.42 tollenae 377: }
1.51 francesc 378:
1.56 francesc 379: /*----------------------------------------------------------------------
1.63 vatton 380: RepeatMenuClicked
1.56 francesc 381: Shows a menu with all the types that can be used in a use element.
382: ----------------------------------------------------------------------*/
1.63 vatton 383: ThotBool RepeatMenuClicked (NotifyElement *event)
1.56 francesc 384: {
385: #ifdef TEMPLATES
1.70 quint 386: XTigerTemplate t;
387: Document doc;
388: Element el, child, newEl;
389: ElementType elt, elt1;
1.63 vatton 390: int nbitems, size;
391: struct menuType *items;
392: char *types, *menuString;
393:
1.70 quint 394: doc = event->document;
395: t = (XTigerTemplate) Get(templates, DocumentMeta[doc]->template_url);
396: if (!t)
397: return FALSE; // no template ?!?!
1.63 vatton 398: types = "top end";
399: size = strlen (types);
400: giveItems (types, size, &items, &nbitems);
401: menuString = createMenuString (items, nbitems);
402: TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL,
403: nbitems, menuString , NULL, false, 'L');
404: TtaFreeMemory (menuString);
1.74 vatton 405: ReturnOption = -1; // no selection yet
1.63 vatton 406: TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
407: TtaWaitShowProcDialogue();
408: TtaDestroyDialogue (BaseDialog + OptionMenu);
1.70 quint 409: if (ReturnOption == 0 || ReturnOption == 1)
410: {
411: el = event->element;
412: child = TtaGetFirstChild (el);
413: if (child)
414: {
415: elt = TtaGetElementType (el);
416: elt1 = TtaGetElementType (child);
417: if (elt.ElSSchema == elt1.ElSSchema &&
418: elt1.ElTypeNum == Template_EL_useEl)
419: {
420: newEl = InstanciateUse (t, child, doc, FALSE);
421: if (newEl)
422: {
423: if (ReturnOption == 0)
424: TtaInsertFirstChild (&newEl, el, doc);
425: else
426: {
427: child = TtaGetLastChild (el);
428: TtaInsertSibling (newEl, child, FALSE, doc);
429: }
430: }
431: }
432: }
433: }
1.63 vatton 434: return FALSE;
1.57 francesc 435: #endif /* TEMPLATES */
1.56 francesc 436: return TRUE;
437: }
1.64 francesc 438:
1.66 vatton 439: /*----------------------------------------------------------------------
440: ----------------------------------------------------------------------*/
1.65 francesc 441: void OpeningInstance(Document doc)
442: {
443: #ifdef TEMPLATES
444: char aux[MAX_LENGTH], content[MAX_LENGTH];
445: ElementType piType, lineType, textType;
446: Element root, pi, line, text;
447: Language language;
448: char *s;
449: int size;
450:
1.66 vatton 451: if (DocumentURLs[doc] == NULL)
452: return;
1.65 francesc 453: //If it is a template we must ignore it
1.66 vatton 454: strcpy (aux, DocumentURLs[doc]);
455: strcpy (content, &aux[strlen(aux)-4]);
1.73 vatton 456: if (strncasecmp (content, ".XTD", strlen(content))==0)
1.66 vatton 457: return;
1.65 francesc 458:
1.66 vatton 459: content[0] = EOS;
1.65 francesc 460: //Instanciate all elements
1.66 vatton 461: root = TtaGetMainRoot (doc);
1.65 francesc 462: //Look for PIs
463: /* check if the document has a DOCTYPE declaration */
464: #ifdef ANNOTATIONS
465: if (DocumentTypes[doc] == docAnnot)
466: piType = TtaGetElementType (root);
467: else
468: #endif /* ANNOTATIONS */
469: piType = TtaGetElementType (root);
470:
471: lineType.ElSSchema = piType.ElSSchema;
472: textType.ElSSchema = piType.ElSSchema;
473:
474: s = TtaGetSSchemaName (piType.ElSSchema);
475:
476: if (strcmp (s, "HTML") == 0)
477: {
478: piType.ElTypeNum = HTML_EL_XMLPI;
479: lineType.ElTypeNum = HTML_EL_PI_line;
480: textType.ElTypeNum = HTML_EL_TEXT_UNIT;
481: }
482: #ifdef _SVG
483: else if (strcmp (s, "SVG") == 0)
484: {
485: piType.ElTypeNum = SVG_EL_XMLPI;
486: lineType.ElTypeNum = SVG_EL_XMLPI_line;
487: textType.ElTypeNum = SVG_EL_TEXT_UNIT;
488: }
489: #endif /* _SVG */
490: else if (strcmp (s, "MathML") == 0)
491: {
492: piType.ElTypeNum = MathML_EL_XMLPI;
493: lineType.ElTypeNum = MathML_EL_XMLPI_line;
494: textType.ElTypeNum = MathML_EL_TEXT_UNIT;
495: }
496: else
497: {
498: piType.ElTypeNum = XML_EL_xmlpi;
499: lineType.ElTypeNum = XML_EL_xmlpi_line;
500: textType.ElTypeNum = XML_EL_TEXT_UNIT;
501: }
502:
503: pi = TtaSearchTypedElement (piType, SearchInTree, root);
504: while(pi!=NULL)
505: {
506: content[0] = '\0';
507: line = TtaSearchTypedElement (lineType, SearchInTree, pi);
508: while(line!=NULL)
509: {
510: text = TtaSearchTypedElement (textType, SearchInTree, line);
511: size = MAX_LENGTH;
512: TtaGiveTextContent(text, (unsigned char*)aux, &size, &language);
513: strcat(content, aux);
514:
515: //This is not an XTiger PI
1.73 vatton 516: if (!strstr(content,"xtiger")) break;
1.65 francesc 517:
518: line = TtaSearchTypedElement (lineType, SearchForward, line);
519: }
520: pi = TtaSearchTypedElement (piType, SearchForward, pi);
521: }
522:
523: DocumentMeta[doc]->template_url = NULL;
524:
1.73 vatton 525: if (content[0]=='\0')
1.65 francesc 526: return;
527:
528: char *pointer;
529:
530: //xtiger
531: strcpy(aux, content);
532: aux[6]='\0';
1.73 vatton 533: if (strcmp(aux,"xtiger")!=0)
1.65 francesc 534: return;
535:
536: //template
537: pointer = strstr(content, "template");
1.73 vatton 538: if (pointer==NULL)
1.65 francesc 539: return;
540:
541: //=
542: pointer = strstr(pointer, "=");
1.73 vatton 543: if (pointer==NULL)
1.65 francesc 544: return;
545:
546: //"
547: pointer = strstr(pointer, "\"");
1.73 vatton 548: if (pointer==NULL)
1.65 francesc 549: return;
550:
551: //content
552: strcpy(aux, pointer+1);
553: pointer = strstr(aux, "\"");
1.73 vatton 554: if (pointer==NULL)
1.65 francesc 555: return;
556: *pointer = '\0';
557:
558: //and finally
559: DocumentMeta[doc]->template_url = TtaStrdup(aux);
560:
1.73 vatton 561: if (!templates) InitializeTemplateEnvironment();
1.65 francesc 562:
563: XTigerTemplate t = (XTigerTemplate)Get(templates, aux);
564:
1.73 vatton 565: if (!t)
1.65 francesc 566: {
567: LoadTemplate(0, aux);
568: t = (XTigerTemplate)Get(templates, aux);
569: }
570: AddUser(t);
571:
572: #endif /* TEMPLATES */
573: }
574:
1.64 francesc 575: /*----------------------------------------------------------------------
1.65 francesc 576: ClosingInstance
1.64 francesc 577: Callback called before closing a document. Checks for unused templates.
578: ----------------------------------------------------------------------*/
1.65 francesc 579: ThotBool ClosingInstance(NotifyDialog* dialog)
1.64 francesc 580: {
1.65 francesc 581: #ifdef TEMPLATES
582: //If it is a template all has been already freed
1.73 vatton 583: if (DocumentMeta[dialog->document] == NULL) return FALSE;
1.65 francesc 584:
585: char *turl = DocumentMeta[dialog->document]->template_url;
1.73 vatton 586: if (turl)
1.65 francesc 587: {
588: XTigerTemplate t = (XTigerTemplate)Get(templates, turl);
1.73 vatton 589: if (t)
1.65 francesc 590: RemoveUser(t);
591: TtaFreeMemory(turl);
592: }
593: #endif /* TEMPLATES */
594: return FALSE;
1.64 francesc 595: }
Webmaster