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