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