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