Annotation of Amaya/amaya/templates.c, revision 1.113
1.1 cvs 1: /*
2: *
1.100 vatton 3: * COPYRIGHT INRIA and W3C, 1996-2007
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.99 kia 17: #include "undo.h"
18:
1.90 kia 19: #include "containers.h"
20: #include "Elemlist.h"
1.92 kia 21: #include "templates.h"
22:
1.90 kia 23:
1.46 vatton 24: #ifdef TEMPLATES
25: #include "Template.h"
1.52 vatton 26: #include "templateDeclarations.h"
1.89 kia 27: #include "templateUtils_f.h"
1.52 vatton 28:
1.69 quint 29: #include "mydictionary_f.h"
1.67 quint 30: #include "templateLoad_f.h"
31: #include "templateDeclarations_f.h"
1.76 vatton 32: #include "templateInstantiate_f.h"
1.28 tollenae 33: #include "appdialogue_wx.h"
1.29 tollenae 34: #include "init_f.h"
1.46 vatton 35: #include "wxdialogapi_f.h"
1.60 francesc 36: #include "AHTURLTools_f.h"
37:
1.52 vatton 38: #endif /* TEMPLATES */
1.1 cvs 39:
1.87 kia 40:
41: #include "fetchXMLname_f.h"
1.83 kia 42: #include "MENUconf.h"
43:
1.87 kia 44:
1.83 kia 45: /* Paths from which looking for templates.*/
46: static Prop_Templates_Path *TemplateRepositoryPaths;
47:
1.87 kia 48:
49: /*----------------------------------------------------------------------
50: IsTemplateInstanceDocument: Test if a document is a template instance
51: doc : Document to test
52: return : TRUE if the document is a template instance
53: ----------------------------------------------------------------------*/
1.106 vatton 54: ThotBool IsTemplateInstanceDocument(Document doc)
55: {
1.87 kia 56: #ifdef TEMPLATES
57: return (DocumentMeta[doc]!=NULL) && (DocumentMeta[doc]->template_url!=NULL);
58: #else /* TEMPLATES */
1.88 cvs 59: return FALSE;
1.87 kia 60: #endif /* TEMPLATES */
61: }
62:
1.83 kia 63: /*----------------------------------------------------------------------
1.109 kia 64: IsTemplateDocument: Test if a document is a template (not an instance)
65: doc : Document to test
66: return : TRUE if the document is an instance
67: ----------------------------------------------------------------------*/
68: ThotBool IsTemplateDocument(Document doc)
69: {
70: #ifdef TEMPLATES
71: return (DocumentMeta[doc]!=NULL) && (DocumentMeta[doc]->template_url==NULL);
72: #else /* TEMPLATES */
73: return FALSE;
74: #endif /* TEMPLATES */
75: }
76:
77:
78: /*----------------------------------------------------------------------
1.108 vatton 79: AllocTemplateRepositoryListElement: allocates an element for the list
80: of template repositories.
1.83 kia 81: path : path of the new element
82: return : address of the new element
83: ----------------------------------------------------------------------*/
84: void* AllocTemplateRepositoryListElement (const char* path, void* prevElement)
85: {
86: Prop_Templates_Path *element = (Prop_Templates_Path*)TtaGetMemory (sizeof(Prop_Templates_Path));
87: element->NextPath = NULL;
88: strcpy (element->Path, path);
89: if (prevElement)
90: {
91: element->NextPath = ((Prop_Templates_Path*)prevElement)->NextPath;
92: ((Prop_Templates_Path*)prevElement)->NextPath = element;
93: }
94: return element;
95: }
96:
97:
98: /*----------------------------------------------------------------------
99: FreeTemplateRepositoryList: Free the list of template repositories.
100: list : address of the list (address of the first element).
101: ----------------------------------------------------------------------*/
102: void FreeTemplateRepositoryList (void* list)
103: {
104: Prop_Templates_Path** l = (Prop_Templates_Path**) list;
105:
106: Prop_Templates_Path* element = *l;
107: l = NULL;
108: while (element)
109: {
110: Prop_Templates_Path* next = element->NextPath;
111: TtaFreeMemory(element);
112: element = next;
113: }
114: }
115:
116: /*----------------------------------------------------------------------
117: CopyTemplateRepositoryList: Copy a list of template repositories.
118: src : address of the list (address of the first element).
119: dst : address where copy the list
120: ----------------------------------------------------------------------*/
1.91 vatton 121: static void CopyTemplateRepositoryList (const Prop_Templates_Path** src,
122: Prop_Templates_Path** dst)
1.83 kia 123: {
124: Prop_Templates_Path *element=NULL, *current=NULL;
125:
1.112 vatton 126: if (*src!=NULL)
1.83 kia 127: {
128: *dst = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
129: (*dst)->NextPath = NULL;
130: strcpy((*dst)->Path, (*src)->Path);
131:
132: element = (*src)->NextPath;
133: current = *dst;
134: }
135:
1.106 vatton 136: while (element)
137: {
1.83 kia 138: current->NextPath = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
139: current = current->NextPath;
140: current->NextPath = NULL;
141: strcpy(current->Path, element->Path);
142: element = element->NextPath;
1.106 vatton 143: }
1.83 kia 144: }
145:
146: /*----------------------------------------------------------------------
147: LoadTemplateRepositoryList: Load the list of template repositories.
148: list : address of the list (address of the first element).
149: return : the number of readed repository paths.
150: ----------------------------------------------------------------------*/
151: static int LoadTemplateRepositoryList (Prop_Templates_Path** list)
152: {
153: Prop_Templates_Path *element, *current = NULL;
154: char *path, *homePath;
155: unsigned char *c;
156: int nb = 0;
157: FILE *file;
158:
159: FreeTemplateRepositoryList(list);
160:
161: path = (char *) TtaGetMemory (MAX_LENGTH);
1.86 vatton 162: homePath = TtaGetEnvString ("APP_HOME");
1.106 vatton 163: sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83 kia 164:
165: file = TtaReadOpen ((char *)path);
1.84 kia 166: if (!file)
167: {
168: /* The config file dont exist, create it. */
169: file = TtaWriteOpen ((char *)path);
1.106 vatton 170: fprintf (file, "%s%ctemplate.xtd\n", homePath, DIR_SEP);
1.84 kia 171: TtaWriteClose (file);
172: /* Retry to open it.*/
173: file = TtaReadOpen ((char *)path);
174: }
175:
1.83 kia 176: if (file)
177: {
1.84 kia 178: c = (unsigned char*)path;
179: *c = EOS;
1.83 kia 180: while (TtaReadByte (file, c)){
181: if (*c==13 || *c==EOL)
182: *c = EOS;
183: if (*c==EOS && c!=(unsigned char*)path )
184: {
185: element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
186: element->NextPath = NULL;
187: strcpy (element->Path, path);
188:
189: if (*list == NULL)
190: *list = element;
191: else
192: current->NextPath = element;
193: current = element;
194: nb++;
195:
196: c = (unsigned char*) path;
197: *c = EOS;
198: }
199: else
200: c++;
201: }
202: if (c!=(unsigned char*)path && *path!=EOS)
203: {
204: element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
205: *(c+1) = EOS;
206: strcpy (element->Path, path);
207: element->NextPath = NULL;
208:
209: if (*list == NULL)
210: *list = element;
211: else
212: current->NextPath = element;
213: nb++;
214: }
215: TtaReadClose (file);
216: }
217: TtaFreeMemory(path);
218: return nb;
219: }
220:
221: /*----------------------------------------------------------------------
222: SaveTemplateRepositoryList: Save the list of template repositories.
223: list : address of the list (address of the first element).
224: ----------------------------------------------------------------------*/
225: static void SaveTemplateRepositoryList (const Prop_Templates_Path** list)
226: {
227: const Prop_Templates_Path *element;
228: char *path, *homePath;
229: unsigned char *c;
230: FILE *file;
231:
232: path = (char *) TtaGetMemory (MAX_LENGTH);
233: homePath = TtaGetEnvString ("APP_HOME");
1.106 vatton 234: sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83 kia 235:
236: file = TtaWriteOpen ((char *)path);
237: c = (unsigned char*)path;
238: *c = EOS;
239: if (file)
240: {
241: element = *list;
242: while (element)
243: {
244: fprintf(file, "%s\n", element->Path);
245: element = element->NextPath;
246: }
247: TtaWriteClose (file);
248: }
249: }
250:
251: /*----------------------------------------------------------------------
252: GetTemplateRepositoryList: Get the list of template repositories from template environment.
253: list : address of the list (address of the first element).
254: ----------------------------------------------------------------------*/
255: void GetTemplateRepositoryList (void* list)
256: {
257: Prop_Templates_Path** l = (Prop_Templates_Path**) list;
258: CopyTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths, l);
259: }
260:
261: /*----------------------------------------------------------------------
262: SetTemplateRepositoryList: Set the list of template repositories environment.
263: list : address of the list (address of the first element).
264: ----------------------------------------------------------------------*/
265: void SetTemplateRepositoryList (const void* list)
266: {
267: const Prop_Templates_Path** l = (const Prop_Templates_Path**) list;
268: CopyTemplateRepositoryList((const Prop_Templates_Path**)l, &TemplateRepositoryPaths);
269: SaveTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths);
270: }
271:
272: /*-----------------------------------------------------------------------
273: InitTemplates
274: Initializes the annotation library
275: -----------------------------------------------------------------------*/
276: void InitTemplates ()
277: {
278: TtaSetEnvBoolean ("SHOW_TEMPLATES", TRUE, FALSE);
279: LoadTemplateRepositoryList(&TemplateRepositoryPaths);
280: }
281:
282:
1.1 cvs 283: /*----------------------------------------------------------------------
1.51 francesc 284: NewTemplate: Create the "new document from template" dialog
1.1 cvs 285: ----------------------------------------------------------------------*/
1.18 cvs 286: void NewTemplate (Document doc, View view)
1.1 cvs 287: {
1.51 francesc 288: #ifdef TEMPLATES
1.76 vatton 289: char *templateDir = TtaGetEnvString ("TEMPLATES_DIRECTORY");
290: ThotBool created;
1.28 tollenae 291:
1.76 vatton 292: if (Templates_Dic == NULL)
293: InitializeTemplateEnvironment ();
294: created = CreateNewTemplateDocDlgWX (BaseDialog + OpenTemplate,
1.61 francesc 295: /*TtaGetViewFrame (doc, view)*/NULL, doc,
1.52 vatton 296: TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),templateDir);
1.51 francesc 297:
1.28 tollenae 298: if (created)
1.25 vatton 299: {
1.28 tollenae 300: TtaSetDialoguePosition ();
301: TtaShowDialogue (BaseDialog + OpenTemplate, TRUE);
1.25 vatton 302: }
1.51 francesc 303:
1.52 vatton 304: #endif /* TEMPLATES */
1.1 cvs 305: }
1.25 vatton 306:
1.108 vatton 307: /*----------------------------------------------------------------------
308: Load a template and create the instance file - update images and
309: stylesheets related to the template.
310: ----------------------------------------------------------------------*/
311: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
312: {
313: #ifdef TEMPLATES
314:
315: char *s;
316: ThotBool dontReplace = DontReplaceOldDoc;
317:
318: if (!IsW3Path (docname) && TtaFileExist (docname))
319: {
320: s = (char *)TtaGetMemory (strlen (docname) +
321: strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
322: sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
323: InitConfirm (0, 0, s);
324: TtaFreeMemory (s);
325: if (!UserAnswer)
326: return;
327: }
328:
329: LoadTemplate (0, templatename);
330: DontReplaceOldDoc = dontReplace;
331: CreateInstance (templatename, docname);
332:
333: #endif /* TEMPLATES */
334: }
335:
1.53 vatton 336:
1.109 kia 337:
1.52 vatton 338: /*----------------------------------------------------------------------
1.107 kia 339: PreventReloadingTemplate
340: Prevent reloading a template.
341: You must call AllowReloadingTemplate when finish.
342: Usefull for reload an instance without reloading the template.
343: ----------------------------------------------------------------------*/
344: void PreventReloadingTemplate(char* template_url)
345: {
346: #ifdef TEMPLATES
347: XTigerTemplate t = (XTigerTemplate) Dictionary_Get (Templates_Dic, template_url);
1.112 vatton 348: if (t)
1.107 kia 349: t->users++;
350: #endif /* TEMPLATES */
351: }
352:
353: /*----------------------------------------------------------------------
354: AllowReloadingTemplate
355: Allow reloading a template.
356: You must call it after each PreventReloadingTemplate call.
357: ----------------------------------------------------------------------*/
358: void AllowReloadingTemplate(char* template_url)
359: {
360: #ifdef TEMPLATES
361: XTigerTemplate t = (XTigerTemplate) Dictionary_Get (Templates_Dic, template_url);
1.112 vatton 362: if (t)
1.107 kia 363: t->users--;
364: #endif /* TEMPLATES */
365: }
366:
367:
368: /*----------------------------------------------------------------------
1.87 kia 369: giveItems : Lists type items from string
370: example : "one two three" is extracted to {one, two, three}
371: note : item type are setted to SimpleTypeNat
372: text : text from which list items
373: size : size of text in characters
374: items : address of exctracted item list
375: nbitems : items number in items list
1.52 vatton 376: ----------------------------------------------------------------------*/
1.76 vatton 377: void giveItems (char *text, int size, struct menuType **items, int *nbitems)
1.1 cvs 378: {
1.70 quint 379: #ifdef TEMPLATES
1.52 vatton 380: ThotBool inElement = TRUE;
381: struct menuType *menu;
382: char *iter;
383: char temp[128];
384: int i;
385: int labelSize;
1.28 tollenae 386:
1.52 vatton 387: *nbitems = 1;
388: for (i = 0; i < size; i++)
389: {
390: if (isEOSorWhiteSpace (text[i]))
391: {
392: if (inElement)
393: inElement = FALSE;
394: }
395: else if (!inElement)
396: {
397: inElement = TRUE;
398: (*nbitems)++;
399: }
400: }
1.51 francesc 401:
1.76 vatton 402: menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
1.52 vatton 403: iter = text;
404: for (i = 0; i < *nbitems; i++)
405: {
406: labelSize = 0;
407: while (isEOSorWhiteSpace (*iter))
408: iter++;
409:
410: while (!isEOSorWhiteSpace (*iter))
411: {
412: temp[labelSize++] = *iter;
413: iter++;
414: }
415:
416: temp[labelSize] = EOS;
1.76 vatton 417: menu[i].label = (char *) TtaStrdup (temp);
1.68 quint 418: menu[i].type = SimpleTypeNat; /* @@@@@ ???? @@@@@ */
1.52 vatton 419: *items = menu;
420: }
1.70 quint 421: #endif /* TEMPLATES */
1.28 tollenae 422: }
1.37 tollenae 423:
1.70 quint 424: #ifdef TEMPLATES
1.52 vatton 425: /*----------------------------------------------------------------------
426: ----------------------------------------------------------------------*/
427: static char *createMenuString (const struct menuType* items, const int nbItems)
428: {
429: char *result, *iter;
430: int size = 0;
431: int i;
432:
433: for (i=0; i < nbItems; i++)
434: size += 2 + strlen (items[i].label);
435:
1.76 vatton 436: result = (char *) TtaGetMemory (size);
1.52 vatton 437: iter = result;
438: for (i=0; i < nbItems; i++)
439: {
440: *iter = 'B';
441: ++iter;
1.51 francesc 442:
1.52 vatton 443: strcpy (iter, items[i].label);
444: iter += strlen (items[i].label)+1;
445: }
1.51 francesc 446: return result;
1.36 tollenae 447: }
1.71 quint 448: #endif /* TEMPLATES */
1.29 tollenae 449:
1.71 quint 450: /*----------------------------------------------------------------------
451: UseToBeCreated
452: An new use element will be created by the user through some generic editing
453: command
454: -----------------------------------------------------------------------*/
455: ThotBool UseToBeCreated (NotifyElement *event)
456: {
457: #ifdef TEMPLATES
1.75 quint 458: Element el;
1.72 quint 459: Document doc;
460:
461: el = event->element;
462: doc = event->document;
1.101 kia 463:
1.106 vatton 464: #ifdef AMAYA_DEBUG
1.101 kia 465: printf("UseToBeCreated\n");
1.106 vatton 466: #endif /* AMAYA_DEBUG */
1.101 kia 467:
1.72 quint 468: /* is there a limit to the number of elements in the xt:repeat ? */
1.71 quint 469: /* @@@@@ */
1.52 vatton 470: #endif /* TEMPLATES */
1.71 quint 471: return FALSE; /* let Thot perform normal operation */
472: }
473:
474: /*----------------------------------------------------------------------
475: UseCreated
476: A new "use" element has just been created by the user with a generic editing
477: command.
478: -----------------------------------------------------------------------*/
479: void UseCreated (NotifyElement *event)
480: {
481: #ifdef TEMPLATES
1.110 kia 482: Document doc = event->document;
483: Element el = event->element;
1.71 quint 484: XTigerTemplate t;
485:
1.112 vatton 486: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 487: return;
488:
1.72 quint 489: if (TtaGetFirstChild (el))
490: /* this Use element has already some content. It has already been
491: instanciated */
492: return;
1.87 kia 493: t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.71 quint 494: if (!t)
495: return; // no template ?!?!
1.101 kia 496:
1.76 vatton 497: InstantiateUse (t, el, doc, TRUE);
1.71 quint 498: #endif /* TEMPLATES */
499: }
1.29 tollenae 500:
1.98 kia 501: /*----------------------------------------------------------------------
502: Template_IncrementRepeatOccurNumber
503: Increment the number of occurs of a xt:repeat
504: @param el element (xt:repeat)
505: ----------------------------------------------------------------------*/
506: void Template_IncrementRepeatOccurNumber(Element el)
507: {
508: #ifdef TEMPLATES
509: char* current;
510: char newVal[8];
511: int curVal;
512:
1.104 kia 513: current = GetAttributeStringValueFromNum(el, Template_ATTR_currentOccurs, NULL);
1.112 vatton 514: if (current)
1.104 kia 515: {
516: curVal = atoi(current);
517: curVal++;
518: TtaFreeMemory(current);
519: sprintf(newVal, "%d", curVal);
520: SetAttributeStringValue(el, Template_ATTR_currentOccurs, newVal);
521: }
1.98 kia 522: #endif /* TEMPLATES */
523: }
524:
525: /*----------------------------------------------------------------------
526: Template_DecrementRepeatOccurNumber
527: Decrement the number of occurs of a xt:repeat
528: @param el element (xt:repeat)
529: ----------------------------------------------------------------------*/
530: void Template_DecrementRepeatOccurNumber(Element el)
531: {
532: #ifdef TEMPLATES
533: char* current;
534: char newVal[8];
535: int curVal;
536:
1.104 kia 537: current = GetAttributeStringValueFromNum(el, Template_ATTR_currentOccurs, NULL);
1.112 vatton 538: if (current)
1.104 kia 539: {
540: curVal = atoi(current);
541: curVal--;
542: TtaFreeMemory(current);
543: sprintf(newVal, "%d", curVal);
544: SetAttributeStringValue(el, Template_ATTR_currentOccurs, newVal);
545: }
1.98 kia 546: #endif /* TEMPLATES */
547: }
548:
1.89 kia 549:
1.98 kia 550: /*----------------------------------------------------------------------
551: Template_CanInsertRepeatChild
552: Test if a xt:repeat child can be inserted (number between params min and max).
553: @param el element (xt:repeat) to test
554: @return True if an element can be inserted.
555: ----------------------------------------------------------------------*/
556: ThotBool Template_CanInsertRepeatChild(Element el)
557: {
558: #ifdef TEMPLATES
559: char* max;
560: char* current;
561: int maxVal, curVal;
1.104 kia 562: Element child;
1.98 kia 563:
1.104 kia 564: max = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
1.105 vatton 565: if (max)
1.104 kia 566: {
1.112 vatton 567: if (!strcmp(max, "*"))
1.105 vatton 568: {
569: TtaFreeMemory(max);
570: return TRUE;
571: }
572: maxVal = atoi (max);
573: TtaFreeMemory (max);
1.104 kia 574:
575: current = GetAttributeStringValueFromNum(el, Template_ATTR_currentOccurs, NULL);
1.105 vatton 576: if (current)
1.104 kia 577: {
1.105 vatton 578: curVal = atoi (current);
579: TtaFreeMemory (current);
1.104 kia 580: }
581: else
582: {
583: curVal = 0;
1.105 vatton 584: for (child = TtaGetFirstChild(el); child; TtaNextSibling(&child))
1.104 kia 585: {
586: curVal++;
587: }
588: }
589:
590: return curVal<maxVal;
591: }
592: else
1.98 kia 593: return TRUE;
594: #endif /* TEMPLATES */
595: return FALSE;
596: }
1.96 kia 597:
1.89 kia 598:
599: /*----------------------------------------------------------------------
600: Template_InsertRepeatChildAfter
601: Insert a child to a xt:repeat
602: The decl parameter must be valid and will not be verified. It must be a
603: direct child element or the "use in the use" for union elements.
604: @param el element (xt:repeat) in which insert a new element
605: @param decl Template declaration of the element to insert
606: @param elPrev Element (xt:use) after which insert the new elem, NULL if first.
607: @return The inserted element
608: ----------------------------------------------------------------------*/
609: Element Template_InsertRepeatChildAfter(Document doc, Element el, Declaration decl, Element elPrev)
610: {
611: #ifdef TEMPLATES
612: Element useFirst; /* First xt:use of the repeat.*/
613: Element use; /* xt:use to insert.*/
614: ElementType useType; /* type of xt:use.*/
615:
1.112 vatton 616: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 617: return NULL;
618:
1.89 kia 619: /* Copy xt:use with xt:types param */
620: useFirst = TtaGetFirstChild(el);
621: useType = TtaGetElementType(useFirst);
622: use = TtaCopyElement(useFirst, doc, doc, el);
623:
624: Template_InsertUseChildren(doc, use, decl);
625:
626: /* insert it */
1.112 vatton 627: if (elPrev)
1.89 kia 628: {
629: TtaInsertSibling(use, elPrev, FALSE, doc);
630: }
631: else
632: {
633: TtaInsertSibling(use, useFirst, TRUE, doc);
634: }
1.99 kia 635:
636: TtaRegisterElementCreate(use, doc);
1.97 kia 637:
1.98 kia 638: Template_IncrementRepeatOccurNumber(el);
639:
1.89 kia 640: return use;
641:
1.93 cvs 642: #else /* TEMPLATES */
643: return NULL;
1.89 kia 644: #endif /* TEMPLATES */
645: }
646:
647: /*----------------------------------------------------------------------
648: Template_InsertRepeatChild
649: Insert a child to a xt:repeat
650: The decl parameter must be valid and will not be verified. It must be a
651: direct child element or the "use in the use" for union elements.
652: @param el element (repeat) in which insert a new element
653: @param decl Template declaration of the element to insert
654: @param pos Position of insertion (0 before all, 1 after first ... -1 after all)
655: @return The inserted element
656: ----------------------------------------------------------------------*/
657: Element Template_InsertRepeatChild(Document doc, Element el, Declaration decl, int pos)
658: {
1.112 vatton 659: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 660: return NULL;
661:
1.112 vatton 662: if (pos==0)
1.89 kia 663: {
664: return Template_InsertRepeatChildAfter(doc, el, decl, NULL);
665: }
1.112 vatton 666: else if (pos==-1)
1.89 kia 667: {
668: return Template_InsertRepeatChildAfter(doc, el, decl, TtaGetLastChild(el));
669: }
670: else
671: {
672: Element elem = TtaGetFirstChild(el);
673: pos--;
1.112 vatton 674: while (pos>0)
1.89 kia 675: {
676: TtaNextSibling(&elem);
677: pos--;
678: }
679: return Template_InsertRepeatChildAfter(doc, el, decl, elem);
680: }
681: }
682:
1.92 kia 683: #ifdef TEMPLATES
1.99 kia 684: /*----------------------------------------------------------------------
685: QueryMenu
686: Show a context menu to query a choice.
687: @param items space-separated choice list string.
688: @return The choosed item 0-based index or -1 if none.
689: ----------------------------------------------------------------------*/
1.89 kia 690: static int QueryMenu(Document doc, char* items)
691: {
692: int nbitems, size;
693: struct menuType *itemlist;
694: char *menuString;
695:
1.112 vatton 696: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 697: return -1;
698:
1.89 kia 699: size = strlen(items);
700: giveItems (items, size, &itemlist, &nbitems);
701: menuString = createMenuString (itemlist, nbitems);
702: TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL,
703: nbitems, menuString , NULL, false, 'L');
704: TtaFreeMemory (menuString);
705: ReturnOption = -1;
706: TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
707: TtaWaitShowProcDialogue ();
708: TtaDestroyDialogue (BaseDialog + OptionMenu);
709: TtaFreeMemory (itemlist);
710: return ReturnOption;
711: }
712:
1.99 kia 713: /*----------------------------------------------------------------------
714: QueryStringFromMenu
715: Show a context menu to query a choice.
716: @param items space-separated choice list string.
717: @return The choosed item string or NULL if none.
718: ----------------------------------------------------------------------*/
1.90 kia 719: static char* QueryStringFromMenu(Document doc, char* items)
720: {
721: int nbitems, size;
722: struct menuType *itemlist;
723: char *menuString;
724: char *result = NULL;
725:
1.112 vatton 726: if (!TtaGetDocumentAccessMode(doc))
727: return NULL;
728: if (items == NULL)
1.110 kia 729: return NULL;
1.90 kia 730: size = strlen(items);
731: giveItems (items, size, &itemlist, &nbitems);
732: menuString = createMenuString (itemlist, nbitems);
733: TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL,
734: nbitems, menuString , NULL, false, 'L');
735: TtaFreeMemory (menuString);
736: ReturnOption = -1;
737: TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
738: TtaWaitShowProcDialogue ();
739: TtaDestroyDialogue (BaseDialog + OptionMenu);
740:
1.112 vatton 741: if (ReturnOption!=-1)
1.90 kia 742: {
743: result = TtaStrdup(itemlist[ReturnOption].label);
744: }
745:
746: TtaFreeMemory (itemlist);
747: return result;
748: }
1.92 kia 749: #endif /* TEMPLATES */
1.90 kia 750:
1.56 francesc 751: /*----------------------------------------------------------------------
1.79 quint 752: RepeatButtonClicked
1.89 kia 753: Called when a repeat button is clicked.
754: Can be called for useEl, useSimple or repeat.
755: If called for useEl or useSimple, the new element must be added after.
756: If called for repeat, the element must be added before all.
757:
1.56 francesc 758: Shows a menu with all the types that can be used in a use element.
759: ----------------------------------------------------------------------*/
1.79 quint 760: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56 francesc 761: {
762: #ifdef TEMPLATES
1.89 kia 763: Document doc = event->document;
764: Element el = event->element;
765: ElementType elType;
1.90 kia 766: XTigerTemplate t;
767: Declaration decl;
768: Element repeatEl = el;
769: Element firstEl;
770: Element newEl = NULL;
1.89 kia 771: char* types;
1.90 kia 772: ThotBool oldStructureChecking;
1.95 kia 773: View view;
1.104 kia 774: char* listtypes;
775: char* result;
776:
1.112 vatton 777: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 778: return TRUE;
1.89 kia 779:
1.95 kia 780: TtaGetActiveView (&doc, &view);
781: if (view != 1)
782: return FALSE; /* let Thot perform normal operation */
783:
1.112 vatton 784: #ifdef AMAYA_DEBUG
1.109 kia 785: printf("Template url : %s\n", DocumentMeta[doc]->template_url);
1.112 vatton 786: #endif/* AMAYA_DEBUG */
1.109 kia 787:
1.89 kia 788: TtaCancelSelection(doc);
1.90 kia 789:
790: t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1.89 kia 791: elType = TtaGetElementType(el);
1.112 vatton 792: while (elType.ElTypeNum!=Template_EL_repeat)
1.89 kia 793: {
1.90 kia 794: repeatEl = TtaGetParent(repeatEl);
1.112 vatton 795: if (repeatEl==NULL)
1.89 kia 796: break;
1.90 kia 797: elType = TtaGetElementType(repeatEl);
1.89 kia 798: }
1.112 vatton 799: if (repeatEl)
1.89 kia 800: {
1.112 vatton 801: if (Template_CanInsertRepeatChild(repeatEl))
1.90 kia 802: {
1.98 kia 803: firstEl = TtaGetFirstChild(repeatEl);
1.104 kia 804: types = GetAttributeStringValueFromNum(firstEl, Template_ATTR_types, NULL);
1.112 vatton 805: if (types)
1.90 kia 806: {
1.104 kia 807: listtypes = Template_ExpandTypes(t, types);
808: result = QueryStringFromMenu(doc, listtypes);
1.112 vatton 809: if (result)
1.98 kia 810: {
1.104 kia 811: decl = Template_GetDeclaration(t, result);
1.112 vatton 812: if (decl)
1.104 kia 813: {
814: /* Prepare insertion.*/
815: oldStructureChecking = TtaGetStructureChecking (doc);
816: TtaSetStructureChecking (FALSE, doc);
817: TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
818:
819: /* Insert. */
1.112 vatton 820: if (el==repeatEl)
1.104 kia 821: newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, NULL);
822: else
823: newEl = Template_InsertRepeatChildAfter(doc, repeatEl, decl, el);
824:
825: /* Finish insertion.*/
826: TtaCloseUndoSequence(doc);
827: TtaSetStructureChecking (oldStructureChecking, doc);
1.99 kia 828:
1.104 kia 829: firstEl = GetFirstEditableElement(newEl);
1.112 vatton 830: if (firstEl)
1.104 kia 831: {
832: TtaSelectElement (doc, firstEl);
833: TtaSetStatusSelectedElement(doc, view, firstEl);
834: }
835: else
836: {
837: TtaSelectElement (doc, newEl);
838: TtaSetStatusSelectedElement(doc, view, newEl);
839: }
1.98 kia 840: }
841: }
1.90 kia 842: }
1.104 kia 843: TtaFreeMemory(types);
1.98 kia 844: TtaFreeMemory(listtypes);
845: TtaFreeMemory(result);
846: }
1.112 vatton 847: else /* if (Template_CanInsertRepeatChild(repeatEl)) */
1.98 kia 848: {
849: TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.90 kia 850: }
1.89 kia 851: }
1.77 vatton 852: return TRUE; /* don't let Thot perform normal operation */
1.57 francesc 853: #endif /* TEMPLATES */
1.94 kia 854: return TRUE;
855: }
856:
857: /*----------------------------------------------------------------------
858: UseButtonClicked
859: Shows a menu with all the types that can be used in a use element.
860: ----------------------------------------------------------------------*/
861: ThotBool UseButtonClicked (NotifyElement *event)
862: {
863: #ifdef TEMPLATES
864: Document doc = event->document;
865: Element el = event->element;
1.99 kia 866: Element child;
1.94 kia 867: ElementType elType;
868: XTigerTemplate t;
869: Declaration decl;
870: Element firstEl;
871: Element newEl = NULL;
872: char* types;
873: ThotBool oldStructureChecking;
1.95 kia 874: View view;
1.104 kia 875: char* listtypes;
876: char* result;
1.95 kia 877:
1.112 vatton 878: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 879: return TRUE;
880:
1.95 kia 881: TtaGetActiveView (&doc, &view);
882: if (view != 1)
883: return FALSE; /* let Thot perform normal operation */
1.94 kia 884:
885: TtaCancelSelection(doc);
886:
887: t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
888: if (!t)
889: return FALSE; /* let Thot perform normal operation */
890: elType = TtaGetElementType(el);
891:
892: firstEl = TtaGetFirstChild(el);
1.112 vatton 893: if (firstEl)
1.94 kia 894: {
895: RepeatButtonClicked(event);
896: }
897: else
898: {
1.104 kia 899: types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.112 vatton 900: if (types)
1.94 kia 901: {
1.104 kia 902: listtypes = Template_ExpandTypes(t, types);
903: result = QueryStringFromMenu(doc, listtypes);
1.112 vatton 904: if (result)
1.94 kia 905: {
1.104 kia 906: decl = Template_GetDeclaration(t, result);
1.112 vatton 907: if (decl)
1.99 kia 908: {
1.104 kia 909: /* Prepare insertion.*/
910: oldStructureChecking = TtaGetStructureChecking (doc);
911: TtaSetStructureChecking (FALSE, doc);
912: TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
913:
914: /* Insert */
915: newEl = Template_InsertUseChildren(doc, el, decl);
916:
917: for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
918: {
919: TtaRegisterElementCreate(child, doc);
920: }
921:
922: TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
923: TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
924:
925: /* Finish insertion. */
926: TtaCloseUndoSequence(doc);
927: TtaSetStructureChecking (oldStructureChecking, doc);
928:
929: firstEl = GetFirstEditableElement(newEl);
1.112 vatton 930: if (firstEl)
1.104 kia 931: {
932: TtaSelectElement (doc, firstEl);
933: TtaSetStatusSelectedElement(doc, view, firstEl);
934: }
935: else
936: {
937: TtaSelectElement (doc, newEl);
938: TtaSetStatusSelectedElement(doc, view, newEl);
939: }
1.98 kia 940: }
1.94 kia 941: }
942: }
1.104 kia 943: TtaFreeMemory(types);
1.94 kia 944: TtaFreeMemory(listtypes);
945: TtaFreeMemory(result);
946: }
947:
948: return TRUE;
949: #endif /* TEMPLATES */
1.56 francesc 950: return TRUE;
951: }
1.64 francesc 952:
1.89 kia 953:
1.103 kia 954: /*----------------------------------------------------------------------
955: UseSimpleButtonClicked
956: ----------------------------------------------------------------------*/
957: ThotBool UseSimpleButtonClicked (NotifyElement *event)
958: {
959: #ifdef TEMPLATES
1.112 vatton 960: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 961: return TRUE;
962:
1.103 kia 963: ElementType parentType = TtaGetElementType(TtaGetParent(event->element));
1.112 vatton 964: if (parentType.ElTypeNum == Template_EL_repeat)
1.103 kia 965: return RepeatButtonClicked(event);
966: #endif /* TEMPLATES */
967: return FALSE;
968: }
1.94 kia 969:
970: /*----------------------------------------------------------------------
971: OptionButtonClicked
972: ----------------------------------------------------------------------*/
973: ThotBool OptionButtonClicked (NotifyElement *event)
974: {
975: #ifdef TEMPLATES
976: Element child, grandChild, next;
977: ElementType elType, elType1;
978: Document doc;
979: XTigerTemplate t;
980: View view;
981:
1.112 vatton 982: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 983: return TRUE;
984:
1.94 kia 985: TtaGetActiveView (&doc, &view);
986: if (view != 1)
987: return FALSE; /* let Thot perform normal operation */
1.110 kia 988:
1.94 kia 989: doc = event->document;
990: child = TtaGetFirstChild (event->element);
991: if (!child)
992: return FALSE; /* let Thot perform normal operation */
993: elType = TtaGetElementType (child);
994: elType1 = TtaGetElementType (event->element);
995: if ((elType.ElTypeNum != Template_EL_useEl &&
996: elType.ElTypeNum != Template_EL_useSimple) ||
997: elType.ElSSchema != elType1.ElSSchema)
998: return FALSE;
999:
1000: TtaCancelSelection (doc);
1001: grandChild = TtaGetFirstChild (child);
1002: if (!grandChild)
1003: /* the "use" element is empty. Instantiate it */
1004: {
1005: t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1006: if (!t)
1007: return FALSE; // no template ?!?!
1008: InstantiateUse (t, child, doc, TRUE);
1009: }
1010: else
1011: /* remove the content of the "use" element */
1012: {
1013: do
1014: {
1015: next = grandChild;
1016: TtaNextSibling (&next);
1017: TtaDeleteTree (grandChild, doc);
1018: grandChild = next;
1019: }
1020: while (next);
1021: }
1022: TtaSelectElement (doc, event->element);
1023: return TRUE; /* don't let Thot perform normal operation */
1024: #endif /* TEMPLATES */
1025: return TRUE;
1026: }
1027:
1.111 vatton 1028: /*----------------------------------------------------------------------
1029: CheckTemplate checks if the template of the instance is loaded
1030: ----------------------------------------------------------------------*/
1031: void CheckTemplate (Document doc)
1032: {
1033: #ifdef TEMPLATES
1034: if (DocumentMeta[doc] && DocumentMeta[doc]->template_url &&
1035: !Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url))
1036: {
1037: // the template cannot be loaded
1.112 vatton 1038: InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
1.111 vatton 1039: TtaSetAccessRight (TtaGetRootElement (doc), ReadOnly, doc);
1.112 vatton 1040: TtaSetDocumentAccessMode (doc, 0); // document readonly
1.111 vatton 1041: }
1042: #endif /* TEMPLATES */
1043: }
1.94 kia 1044:
1.66 vatton 1045: /*----------------------------------------------------------------------
1.108 vatton 1046: OpeningInstance checks if it is a template instance needs.
1047: If it's an instance and the template is not loaded, load it into a
1048: temporary file
1.66 vatton 1049: ----------------------------------------------------------------------*/
1.76 vatton 1050: void OpeningInstance (char *fileName, Document doc)
1.65 francesc 1051: {
1052: #ifdef TEMPLATES
1.76 vatton 1053: XTigerTemplate t;
1.103 kia 1054: char *content, *ptr, *begin;
1.76 vatton 1055: gzFile stream;
1056: char buffer[2000];
1.77 vatton 1057: int res;
1.65 francesc 1058:
1.76 vatton 1059: stream = TtaGZOpen (fileName);
1060: if (stream != 0)
1.65 francesc 1061: {
1.76 vatton 1062: res = gzread (stream, buffer, 1999);
1063: if (res >= 0)
1.65 francesc 1064: {
1.81 vatton 1065: buffer[res] = EOS;
1.103 kia 1066: begin = strstr (buffer, "<?xtiger");
1067:
1.112 vatton 1068: if (begin)
1.103 kia 1069: {
1070: // Search for template version
1071: ptr = strstr (begin, "templateVersion");
1072: if (ptr)
1073: ptr = strstr (ptr, "=");
1074: if (ptr)
1075: ptr = strstr (ptr, "\"");
1076: if (ptr)
1077: {
1078: // template URI
1079: content = &ptr[1];
1080: ptr = strstr (content, "\"");
1081: }
1082: if (ptr)
1083: {
1084: *ptr = EOS;
1085: //Get now the template URI
1086: DocumentMeta[doc]->template_version = TtaStrdup (content);
1087: *ptr = '"';
1088: }
1089:
1090: // Search for template uri
1091: ptr = strstr (begin, "template");
1092: if (ptr && ptr[8] != 'V')
1093: ptr = strstr (ptr, "=");
1094: if (ptr)
1095: ptr = strstr (ptr, "\"");
1096: if (ptr)
1097: {
1098: // template URI
1099: content = &ptr[1];
1100: ptr = strstr (content, "\"");
1101: }
1102: if (ptr)
1103: {
1104: *ptr = EOS;
1105: //Get now the template URI
1106: DocumentMeta[doc]->template_url = TtaStrdup (content);
1107: if (Templates_Dic == NULL)
1108: InitializeTemplateEnvironment ();
1109: t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
1110: if (!t)
1111: {
1.108 vatton 1112: LoadTemplate (doc, content);
1.103 kia 1113: t = (XTigerTemplate) Dictionary_Get (Templates_Dic, content);
1114: }
1115: AddUser (t);
1116: *ptr = '"';
1117: }
1118: }
1.65 francesc 1119: }
1120: }
1.76 vatton 1121: TtaGZClose (stream);
1.65 francesc 1122: #endif /* TEMPLATES */
1123: }
1124:
1.64 francesc 1125: /*----------------------------------------------------------------------
1.65 francesc 1126: ClosingInstance
1.64 francesc 1127: Callback called before closing a document. Checks for unused templates.
1128: ----------------------------------------------------------------------*/
1.65 francesc 1129: ThotBool ClosingInstance(NotifyDialog* dialog)
1.64 francesc 1130: {
1.65 francesc 1131: #ifdef TEMPLATES
1132: //If it is a template all has been already freed
1.76 vatton 1133: if (DocumentMeta[dialog->document] == NULL)
1134: return FALSE;
1.65 francesc 1135:
1136: char *turl = DocumentMeta[dialog->document]->template_url;
1.73 vatton 1137: if (turl)
1.104 kia 1138: {
1139: XTigerTemplate t = (XTigerTemplate) Dictionary_Get (Templates_Dic, turl);
1140: if (t)
1141: RemoveUser (t);
1142: TtaFreeMemory (turl);
1143: DocumentMeta[dialog->document]->template_url = NULL;
1144: }
1145:
1.112 vatton 1146: if (DocumentMeta[dialog->document]->template_version)
1.104 kia 1147: {
1148: TtaFreeMemory(DocumentMeta[dialog->document]->template_version);
1149: DocumentMeta[dialog->document]->template_version = NULL;
1150: }
1.65 francesc 1151: #endif /* TEMPLATES */
1152: return FALSE;
1.64 francesc 1153: }
1.87 kia 1154:
1155:
1156: /*----------------------------------------------------------------------
1157: GetFirstTemplateParentElement
1158: Return the first element wich has "Template" as SShema name or null if none.
1159: ----------------------------------------------------------------------*/
1160: ThotBool IsTemplateElement(Element elem)
1161: {
1162: #ifdef TEMPLATES
1163: return strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
1164: , TEMPLATE_SSHEMA_NAME)==0;
1165: #else
1166: return FALSE;
1167: #endif /* TEMPLATES */
1168: }
1169:
1170:
1171: /*----------------------------------------------------------------------
1172: GetFirstTemplateParentElement
1173: Return the first element wich has "Template" as SShema name or null if none.
1174: ----------------------------------------------------------------------*/
1175: Element GetFirstTemplateParentElement(Element elem)
1176: {
1177: #ifdef TEMPLATES
1178: elem = TtaGetParent(elem);
1.112 vatton 1179: while (elem!=NULL && strcmp(TtaGetSSchemaName(TtaGetElementType(elem).ElSSchema)
1.87 kia 1180: , TEMPLATE_SSHEMA_NAME)!=0)
1181: {
1182: elem = TtaGetParent(elem);
1183: }
1184: return elem;
1185: #else
1186: return NULL;
1187: #endif /* TEMPLATES */
1188: }
1.101 kia 1189:
1.103 kia 1190:
1.101 kia 1191: /*----------------------------------------------------------------------
1.102 vatton 1192: TemplateElementWillBeCreated
1.101 kia 1193: Processed when an element will be created in a template context.
1194: ----------------------------------------------------------------------*/
1.102 vatton 1195: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101 kia 1196: {
1.103 kia 1197: #ifdef TEMPLATES
1198: ElementType elType = event->elementType;
1199: Element parent = event->element;
1200: ElementType parentType = TtaGetElementType(parent);
1.113 ! kia 1201: Element ancestor;
! 1202: ElementType ancestorType;
! 1203: SSchema templateSSchema;
! 1204: char* types;
! 1205: ThotBool b;
1.101 kia 1206:
1.112 vatton 1207: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1208: return TRUE;
1209:
1.113 ! kia 1210: templateSSchema = TtaGetSSchema ("Template", event->document);
1.102 vatton 1211: if (templateSSchema == NULL)
1212: return FALSE; // let Thot do the job
1.103 kia 1213:
1.106 vatton 1214: #ifdef AMAYA_DEBUG
1.113 ! kia 1215: printf("TemplateElementWillBeCreated %s:%s:%d\n", TtaGetSSchemaName(elType.ElSSchema), TtaGetElementTypeName(elType), elType.ElTypeNum);
1.103 kia 1216: printf(" ^^ %s:%s\n", TtaGetSSchemaName(parentType.ElSSchema), TtaGetElementTypeName(parentType));
1.106 vatton 1217: #endif /* AMAYA_DEBUG */
1.103 kia 1218:
1.113 ! kia 1219:
! 1220: // Fisrt, test if in a xt:bag or in a base-element xt:use
! 1221: if(parentType.ElSSchema==templateSSchema)
! 1222: ancestor = parent;
! 1223: else
! 1224: ancestor = GetFirstTemplateParentElement(parent);
! 1225:
! 1226: if(ancestor)
! 1227: {
! 1228: ancestorType = TtaGetElementType(ancestor);
! 1229:
! 1230: #ifdef AMAYA_DEBUG
! 1231: printf(" <> %s:%s\n", TtaGetSSchemaName(ancestorType.ElSSchema), TtaGetElementTypeName(ancestorType));
! 1232: #endif /* AMAYA_DEBUG */
! 1233:
! 1234: if(ancestorType.ElTypeNum==Template_EL_bag)
! 1235: {
! 1236: types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_types, NULL);
! 1237: b = Template_CanInsertElementInBag(event->document, elType, types);
! 1238: TtaFreeMemory(types);
! 1239: return !b;
! 1240: }
! 1241: else if(ancestorType.ElTypeNum==Template_EL_useSimple || ancestorType.ElTypeNum==Template_EL_useEl)
! 1242: {
! 1243: types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
! 1244: b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position);
! 1245: return !b;
! 1246:
! 1247: }
! 1248: }
! 1249: // Can not insert.
! 1250: return TRUE;
1.101 kia 1251: #endif /* TEMPLATES*/
1.102 vatton 1252: return FALSE;
1.101 kia 1253: }
1254:
1255: /*----------------------------------------------------------------------
1256: TemplateElementWillBeDeleted
1257: Processed when an element will be deleted in a template context.
1258: ----------------------------------------------------------------------*/
1259: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
1260: {
1.107 kia 1261: #ifdef TEMPLATES
1262: Document doc = event->document;
1263: Element elem = event->element;
1264: Element xtElem, parent;
1.109 kia 1265: Element sibling;
1.107 kia 1266: ElementType xtType;
1267: char* type;
1268: Declaration dec;
1269: SSchema templateSSchema = TtaGetSSchema ("Template", event->document);
1270: XTigerTemplate t;
1271:
1.112 vatton 1272: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1273: return TRUE;
1274:
1.107 kia 1275: if (templateSSchema == NULL)
1276: return FALSE; // let Thot do the job
1277:
1.103 kia 1278:
1.107 kia 1279: xtElem = GetFirstTemplateParentElement(elem);
1.112 vatton 1280: if (xtElem)
1.107 kia 1281: {
1282: xtType = TtaGetElementType(xtElem);
1.109 kia 1283: t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
1284:
1.112 vatton 1285: if (xtType.ElTypeNum==Template_EL_bag)
1.109 kia 1286: return FALSE; // xt:bag always allow remove children.
1.112 vatton 1287: else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107 kia 1288: {
1.109 kia 1289: parent = TtaGetParent(elem);
1.112 vatton 1290: if (xtElem!=parent){
1.109 kia 1291: type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
1292: dec = Template_GetDeclaration(t, type);
1293: TtaFreeMemory(type);
1.112 vatton 1294: if (dec->nature == XmlElementNat)
1.109 kia 1295: return FALSE; // Can remove element only if in xt:use current type is base language element.
1296: else
1297: return TRUE;
1.107 kia 1298: }
1.109 kia 1299: }
1.112 vatton 1300: else if (xtType.ElTypeNum==Template_EL_repeat)
1.109 kia 1301: {
1302: sibling = TtaGetSuccessor(elem);
1303: TtaRegisterElementDelete(elem, doc);
1304: TtaDeleteTree(elem, doc);
1305: InstantiateRepeat(t, xtElem, doc, TRUE);
1306: TtaSelectElement(doc, sibling);
1307: return TRUE;
1.107 kia 1308: }
1309: }
1.109 kia 1310:
1311: //TODO Test if current element is use or repeat.
1312: // Because if an element is delete and it is the unique child of its parent,
1313: // the parent intends to destroy itself.
1314:
1.107 kia 1315: return TRUE;
1316: #else /* TEMPLATES */
1.101 kia 1317: return FALSE;
1.107 kia 1318: #endif /* TEMPLATES */
1.101 kia 1319: }
1320:
1.109 kia 1321: /*----------------------------------------------------------------------
1322: CurrentTypeWillBeExported
1323: Check if the xt:currentType attribute can be exported
1324: ----------------------------------------------------------------------*/
1325: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
1326: {
1327: #ifdef TEMPLATES
1.110 kia 1328:
1.112 vatton 1329: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1330: return TRUE;
1331:
1.112 vatton 1332: if (IsTemplateDocument(event->document))
1.109 kia 1333: return TRUE;
1334: #endif /* TEMPLATES */
1335: return FALSE;
1336: }
Webmaster