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