Annotation of Amaya/amaya/templates.c, revision 1.181
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.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.174 kia 27: #include "HTMLedit_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)
1.176 vatton 56: return (t->state & templInstance) != 0;
1.171 kia 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:
1.173 vatton 128: if (!IsTemplateInstanceDocument(doc))
129: /* let Thot perform normal operation */
130: return FALSE;
1.146 vatton 131: elType = TtaGetElementType (el);
132: templateSSchema = TtaGetSSchema ("Template", doc);
133: if (elType.ElTypeNum == HTML_EL_TEXT_UNIT)
134: {
135: parent = TtaGetParent (el);
136: elType = TtaGetElementType (parent);
137: while (parent && elType.ElSSchema != templateSSchema)
138: {
139: parent = TtaGetParent (parent);
140: elType = TtaGetElementType (parent);
141: }
142: if (parent &&
143: (elType.ElTypeNum == Template_EL_useEl ||
144: elType.ElTypeNum == Template_EL_useSimple))
145: {
146: // there is a parent template use
147: attrType.AttrSSchema = elType.ElSSchema;
148: attrType.AttrTypeNum = Template_ATTR_prompt;
149: att = TtaGetAttribute (parent, attrType);
150: if (att)
1.151 quint 151: {
152: TtaSelectElement (doc, el);
153: return TRUE;
154: }
1.146 vatton 155: }
156: }
157: #endif /* TEMPLATES */
1.173 vatton 158: /* let Thot perform normal operation */
1.146 vatton 159: return FALSE;
160: }
161:
162: /*----------------------------------------------------------------------
163: RemovePromptIndicator removes the enclosing prompt indicator
164: ----------------------------------------------------------------------*/
1.153 vatton 165: ThotBool RemovePromptIndicator (NotifyOnTarget *event)
1.146 vatton 166: {
167: #ifdef TEMPLATES
168: ElementType elType;
1.153 vatton 169: Element parent, el;
1.146 vatton 170: AttributeType attrType;
171: Attribute att;
1.153 vatton 172: Document doc;
1.146 vatton 173: SSchema templateSSchema;
174:
1.153 vatton 175: el = event->element;
176: doc = event->document;
1.146 vatton 177: elType = TtaGetElementType (el);
178: templateSSchema = TtaGetSSchema ("Template", doc);
1.153 vatton 179: parent = TtaGetParent (el);
180: elType = TtaGetElementType (parent);
181: while (parent && elType.ElSSchema != templateSSchema)
1.146 vatton 182: {
1.153 vatton 183: parent = TtaGetParent (parent);
1.146 vatton 184: elType = TtaGetElementType (parent);
1.153 vatton 185: }
186: if (parent &&
187: (elType.ElTypeNum == Template_EL_useEl ||
188: elType.ElTypeNum == Template_EL_useSimple))
189: {
190: // there is a parent template use
191: attrType.AttrSSchema = elType.ElSSchema;
192: attrType.AttrTypeNum = Template_ATTR_prompt;
193: att = TtaGetAttribute (parent, attrType);
194: if (att)
1.146 vatton 195: {
1.153 vatton 196: TtaRegisterAttributeDelete (att, parent, doc);
197: TtaRemoveAttribute (parent, att, doc);
1.146 vatton 198: }
199: }
200: #endif /* TEMPLATES */
1.153 vatton 201: return FALSE; /* let Thot perform normal operation */
1.146 vatton 202: }
203:
1.109 kia 204:
205: /*----------------------------------------------------------------------
1.108 vatton 206: AllocTemplateRepositoryListElement: allocates an element for the list
207: of template repositories.
1.83 kia 208: path : path of the new element
209: return : address of the new element
210: ----------------------------------------------------------------------*/
211: void* AllocTemplateRepositoryListElement (const char* path, void* prevElement)
212: {
1.129 vatton 213: Prop_Templates_Path *element;
214:
215: element = (Prop_Templates_Path*)TtaGetMemory (sizeof(Prop_Templates_Path));
216: memset (element, 0, sizeof(Prop_Templates_Path));
217: strncpy (element->Path, path, MAX_LENGTH - 1);
1.83 kia 218: if (prevElement)
1.129 vatton 219: {
220: element->NextPath = ((Prop_Templates_Path*)prevElement)->NextPath;
221: ((Prop_Templates_Path*)prevElement)->NextPath = element;
222: }
1.83 kia 223: return element;
224: }
225:
226:
227: /*----------------------------------------------------------------------
228: FreeTemplateRepositoryList: Free the list of template repositories.
229: list : address of the list (address of the first element).
230: ----------------------------------------------------------------------*/
231: void FreeTemplateRepositoryList (void* list)
232: {
1.131 vatton 233: Prop_Templates_Path **l = (Prop_Templates_Path**) list;
234: Prop_Templates_Path *element = *l;
235:
1.83 kia 236: l = NULL;
237: while (element)
238: {
239: Prop_Templates_Path* next = element->NextPath;
1.131 vatton 240: TtaFreeMemory (element);
1.83 kia 241: element = next;
242: }
243: }
244:
245: /*----------------------------------------------------------------------
246: CopyTemplateRepositoryList: Copy a list of template repositories.
247: src : address of the list (address of the first element).
248: dst : address where copy the list
249: ----------------------------------------------------------------------*/
1.91 vatton 250: static void CopyTemplateRepositoryList (const Prop_Templates_Path** src,
251: Prop_Templates_Path** dst)
1.83 kia 252: {
253: Prop_Templates_Path *element=NULL, *current=NULL;
254:
1.131 vatton 255: if (*src)
1.83 kia 256: {
257: *dst = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
258: (*dst)->NextPath = NULL;
259: strcpy((*dst)->Path, (*src)->Path);
260:
261: element = (*src)->NextPath;
262: current = *dst;
263: }
264:
1.106 vatton 265: while (element)
266: {
1.83 kia 267: current->NextPath = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
268: current = current->NextPath;
269: current->NextPath = NULL;
270: strcpy(current->Path, element->Path);
271: element = element->NextPath;
1.106 vatton 272: }
1.83 kia 273: }
274:
275: /*----------------------------------------------------------------------
276: LoadTemplateRepositoryList: Load the list of template repositories.
277: list : address of the list (address of the first element).
278: return : the number of readed repository paths.
279: ----------------------------------------------------------------------*/
280: static int LoadTemplateRepositoryList (Prop_Templates_Path** list)
281: {
282: Prop_Templates_Path *element, *current = NULL;
283: char *path, *homePath;
284: unsigned char *c;
285: int nb = 0;
286: FILE *file;
287:
1.131 vatton 288: //clean up the curent list
289: FreeTemplateRepositoryList (list);
290:
291: // open the file
1.83 kia 292: path = (char *) TtaGetMemory (MAX_LENGTH);
1.86 vatton 293: homePath = TtaGetEnvString ("APP_HOME");
1.106 vatton 294: sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83 kia 295: file = TtaReadOpen ((char *)path);
1.84 kia 296: if (!file)
297: {
298: /* The config file dont exist, create it. */
299: file = TtaWriteOpen ((char *)path);
1.122 kia 300: fprintf (file, "http://www.w3.org/Amaya/Templates/cv.xtd\n");
1.144 vatton 301: fprintf (file, "http://www.w3.org/Amaya/Templates/slides.xtd\n");
302: fprintf (file, "http://www.w3.org/Amaya/Templates/ACM-Proc-Article.xtd\n");
1.84 kia 303: TtaWriteClose (file);
304: /* Retry to open it.*/
305: file = TtaReadOpen ((char *)path);
306: }
307:
1.83 kia 308: if (file)
1.129 vatton 309: {
1.131 vatton 310: // read the file
1.129 vatton 311: c = (unsigned char*)path;
312: *c = EOS;
313: while (TtaReadByte (file, c))
314: {
315: if (*c == 13 || *c == EOL)
316: *c = EOS;
317: if (*c == EOS && c != (unsigned char*)path )
318: {
319: element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
320: element->NextPath = NULL;
321: strcpy (element->Path, path);
1.148 kia 322:
1.129 vatton 323: if (*list == NULL)
324: *list = element;
325: else
326: current->NextPath = element;
327: current = element;
328: nb++;
1.83 kia 329:
1.129 vatton 330: c = (unsigned char*) path;
331: *c = EOS;
332: }
333: else
334: c++;
335: }
336: if (c != (unsigned char*)path && *path != EOS)
337: {
338: element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
339: *(c+1) = EOS;
340: strcpy (element->Path, path);
341: element->NextPath = NULL;
1.148 kia 342:
1.129 vatton 343: if (*list == NULL)
344: *list = element;
345: else
346: current->NextPath = element;
347: nb++;
348: }
349: TtaReadClose (file);
1.83 kia 350: }
351: TtaFreeMemory(path);
352: return nb;
353: }
354:
355: /*----------------------------------------------------------------------
356: SaveTemplateRepositoryList: Save the list of template repositories.
357: list : address of the list (address of the first element).
358: ----------------------------------------------------------------------*/
359: static void SaveTemplateRepositoryList (const Prop_Templates_Path** list)
360: {
361: const Prop_Templates_Path *element;
362: char *path, *homePath;
363: unsigned char *c;
364: FILE *file;
365:
366: path = (char *) TtaGetMemory (MAX_LENGTH);
367: homePath = TtaGetEnvString ("APP_HOME");
1.106 vatton 368: sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83 kia 369:
370: file = TtaWriteOpen ((char *)path);
371: c = (unsigned char*)path;
372: *c = EOS;
373: if (file)
374: {
375: element = *list;
376: while (element)
377: {
378: fprintf(file, "%s\n", element->Path);
379: element = element->NextPath;
380: }
381: TtaWriteClose (file);
382: }
383: }
384:
385: /*----------------------------------------------------------------------
386: GetTemplateRepositoryList: Get the list of template repositories from template environment.
387: list : address of the list (address of the first element).
388: ----------------------------------------------------------------------*/
389: void GetTemplateRepositoryList (void* list)
390: {
391: Prop_Templates_Path** l = (Prop_Templates_Path**) list;
392: CopyTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths, l);
393: }
394:
395: /*----------------------------------------------------------------------
396: SetTemplateRepositoryList: Set the list of template repositories environment.
397: list : address of the list (address of the first element).
398: ----------------------------------------------------------------------*/
399: void SetTemplateRepositoryList (const void* list)
400: {
401: const Prop_Templates_Path** l = (const Prop_Templates_Path**) list;
402: CopyTemplateRepositoryList((const Prop_Templates_Path**)l, &TemplateRepositoryPaths);
403: SaveTemplateRepositoryList((const Prop_Templates_Path**)&TemplateRepositoryPaths);
404: }
405:
406: /*-----------------------------------------------------------------------
407: InitTemplates
408: Initializes the annotation library
409: -----------------------------------------------------------------------*/
410: void InitTemplates ()
411: {
412: TtaSetEnvBoolean ("SHOW_TEMPLATES", TRUE, FALSE);
1.131 vatton 413: LoadTemplateRepositoryList (&TemplateRepositoryPaths);
1.83 kia 414: }
415:
416:
1.1 cvs 417: /*----------------------------------------------------------------------
1.108 vatton 418: Load a template and create the instance file - update images and
419: stylesheets related to the template.
420: ----------------------------------------------------------------------*/
421: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
422: {
423: #ifdef TEMPLATES
1.170 vatton 424: DocumentType docType;
425: int len, i;
426: char *s;
427: char suffix[6];
428: ThotBool dontReplace = DontReplaceOldDoc;
1.108 vatton 429:
430: if (!IsW3Path (docname) && TtaFileExist (docname))
431: {
432: s = (char *)TtaGetMemory (strlen (docname) +
433: strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
434: sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
435: InitConfirm (0, 0, s);
436: TtaFreeMemory (s);
437: if (!UserAnswer)
438: return;
439: }
1.170 vatton 440: docType = LoadTemplate (0, templatename);
441: if (docType != docFree)
1.165 kia 442: {
1.170 vatton 443: /* check if the file suffix is conform to the document type */
444: s = (char *)TtaGetMemory (strlen (docname) + 10);
445: strcpy (s, docname);
446: if (!IsXMLName (docname))
447: {
448: // by default no suffix is added
449: suffix[0] = EOS;
450: if (IsMathMLName (docname) && docType != docMath)
451: strcpy (suffix, "mml");
452: else if (IsSVGName (docname) && docType != docSVG)
453: strcpy (suffix, "svg");
454: else if (IsHTMLName (docname) && docType != docHTML)
455: strcpy (suffix, "xml");
456: if (suffix[0] != EOS)
457: {
458: // change or update the suffix
459: len = strlen (s);
460: for (i = len-1; i > 0 && s[i] != '.'; i--);
461: if (s[i] != '.')
462: {
463: /* there is no suffix */
464: s[i++] = '.';
465: strcpy (&s[i], suffix);
466: }
467: else
468: {
469: /* there is a suffix */
470: i++;
471: strcpy (&s[i], suffix);
472: }
473: }
474: }
475: // now create the instance
1.165 kia 476: DontReplaceOldDoc = dontReplace;
1.177 vatton 477: CreateInstance (templatename, s, docname, docType, doc);
1.170 vatton 478: TtaFreeMemory (s);
1.165 kia 479: }
1.108 vatton 480: #endif /* TEMPLATES */
481: }
482:
1.53 vatton 483:
1.109 kia 484:
1.52 vatton 485: /*----------------------------------------------------------------------
1.107 kia 486: PreventReloadingTemplate
487: Prevent reloading a template.
488: You must call AllowReloadingTemplate when finish.
489: Usefull for reload an instance without reloading the template.
490: ----------------------------------------------------------------------*/
491: void PreventReloadingTemplate(char* template_url)
492: {
493: #ifdef TEMPLATES
1.178 kia 494: Template_AddReference(GetXTigerTemplate (template_url));
1.107 kia 495: #endif /* TEMPLATES */
496: }
497:
498: /*----------------------------------------------------------------------
499: AllowReloadingTemplate
500: Allow reloading a template.
501: You must call it after each PreventReloadingTemplate call.
502: ----------------------------------------------------------------------*/
503: void AllowReloadingTemplate(char* template_url)
504: {
505: #ifdef TEMPLATES
1.178 kia 506: Template_RemoveReference(GetXTigerTemplate (template_url));
1.107 kia 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.179 kia 942: {
943: repeatEl = TtaGetParent(repeatEl);
944: if (repeatEl == NULL)
945: break;
946: elType = TtaGetElementType(repeatEl);
947: }
1.112 vatton 948: if (repeatEl)
1.90 kia 949: {
1.179 kia 950: if (Template_CanInsertRepeatChild (repeatEl))
951: {
952: firstEl = TtaGetFirstChild (repeatEl);
953: listtypes = Template_GetListTypes (t, firstEl);
954: if (listtypes)
955: {
1.158 kia 956: #ifdef AMAYA_DEBUG
1.179 kia 957: printf("RepeatButtonClicked : \n > %s\n", listtypes);
1.158 kia 958: #endif /* AMAYA_DEBUG */
1.179 kia 959:
960: result = QueryStringFromMenu (doc, listtypes);
961: TtaFreeMemory (listtypes);
962: if (result)
963: {
964: decl = Template_GetDeclaration (t, result);
965: if (decl)
966: {
967: dispMode = TtaGetDisplayMode (doc);
968: if (dispMode == DisplayImmediately)
969: /* don't set NoComputedDisplay
1.138 vatton 970: -> it breaks down views formatting when Enter generates new elements */
1.179 kia 971: TtaSetDisplayMode (doc, DeferredDisplay);
972:
973: /* Prepare insertion.*/
974: oldStructureChecking = TtaGetStructureChecking (doc);
975: TtaSetStructureChecking (FALSE, doc);
976: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
977: /* Insert. */
978: if (el == repeatEl)
979: newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, NULL);
980: else
981: newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, el);
1.138 vatton 982:
1.179 kia 983: /* Finish insertion.*/
984: TtaCloseUndoSequence(doc);
985: TtaSetDocumentModified (doc);
986: TtaSetStructureChecking (oldStructureChecking, doc);
987:
988: // restore the display
989: TtaSetDisplayMode (doc, dispMode);
990: firstEl = GetFirstEditableElement (newEl);
991: if (firstEl)
992: {
993: TtaSelectElement (doc, firstEl);
994: TtaSetStatusSelectedElement (doc, view, firstEl);
995: }
996: else
997: {
998: TtaSelectElement (doc, newEl);
999: TtaSetStatusSelectedElement (doc, view, newEl);
1000: }
1001: }
1002: }
1.104 kia 1003: }
1.179 kia 1004: TtaFreeMemory(result);
1005: DumpSubtree(repeatEl, doc, 0);
1006:
1007: }
1008: else /* if (Template_CanInsertRepeatChild(repeatEl)) */
1009: {
1010: TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.98 kia 1011: }
1012: }
1.77 vatton 1013: return TRUE; /* don't let Thot perform normal operation */
1.57 francesc 1014: #endif /* TEMPLATES */
1.94 kia 1015: return TRUE;
1016: }
1017:
1018: /*----------------------------------------------------------------------
1019: UseButtonClicked
1020: Shows a menu with all the types that can be used in a use element.
1021: ----------------------------------------------------------------------*/
1022: ThotBool UseButtonClicked (NotifyElement *event)
1023: {
1024: #ifdef TEMPLATES
1025: Document doc = event->document;
1026: Element el = event->element;
1.99 kia 1027: Element child;
1.94 kia 1028: ElementType elType;
1029: XTigerTemplate t;
1030: Declaration decl;
1031: Element firstEl;
1032: Element newEl = NULL;
1033: char* types;
1034: ThotBool oldStructureChecking;
1.95 kia 1035: View view;
1.133 vatton 1036: char* listtypes = NULL;
1037: char* result = NULL;
1.95 kia 1038:
1.112 vatton 1039: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 1040: return TRUE;
1041:
1.95 kia 1042: TtaGetActiveView (&doc, &view);
1043: if (view != 1)
1044: return FALSE; /* let Thot perform normal operation */
1.148 kia 1045:
1.94 kia 1046: TtaCancelSelection(doc);
1047:
1.171 kia 1048: t = GetXTigerDocTemplate(doc);
1.94 kia 1049: if (!t)
1050: return FALSE; /* let Thot perform normal operation */
1051: elType = TtaGetElementType(el);
1052:
1053: firstEl = TtaGetFirstChild(el);
1.112 vatton 1054: if (firstEl)
1.94 kia 1055: {
1056: RepeatButtonClicked(event);
1057: }
1058: else
1059: {
1.104 kia 1060: types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.112 vatton 1061: if (types)
1.94 kia 1062: {
1.142 kia 1063: listtypes = Template_ExpandTypes(t, types, NULL, FALSE);
1.158 kia 1064: #ifdef AMAYA_DEBUG
1065: printf("UseButtonClicked : \n > %s\n", listtypes);
1066: #endif /* AMAYA_DEBUG */
1067:
1.104 kia 1068: result = QueryStringFromMenu(doc, listtypes);
1.112 vatton 1069: if (result)
1.94 kia 1070: {
1.104 kia 1071: decl = Template_GetDeclaration(t, result);
1.112 vatton 1072: if (decl)
1.99 kia 1073: {
1.104 kia 1074: /* Prepare insertion.*/
1075: oldStructureChecking = TtaGetStructureChecking (doc);
1076: TtaSetStructureChecking (FALSE, doc);
1.138 vatton 1077: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104 kia 1078:
1079: /* Insert */
1080: newEl = Template_InsertUseChildren(doc, el, decl);
1081: for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
1082: {
1083: TtaRegisterElementCreate(child, doc);
1084: }
1085:
1086: TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
1087: TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
1088:
1.117 kia 1089: /* xt:currentType attribute.*/
1090: SetAttributeStringValueWithUndo(el, Template_ATTR_currentType, result);
1091:
1.104 kia 1092: /* Finish insertion. */
1093: TtaCloseUndoSequence(doc);
1.114 vatton 1094: TtaSetDocumentModified (doc);
1.104 kia 1095: TtaSetStructureChecking (oldStructureChecking, doc);
1096:
1097: firstEl = GetFirstEditableElement(newEl);
1.112 vatton 1098: if (firstEl)
1.104 kia 1099: {
1100: TtaSelectElement (doc, firstEl);
1101: TtaSetStatusSelectedElement(doc, view, firstEl);
1102: }
1103: else
1104: {
1105: TtaSelectElement (doc, newEl);
1106: TtaSetStatusSelectedElement(doc, view, newEl);
1107: }
1.98 kia 1108: }
1.94 kia 1109: }
1110: }
1.104 kia 1111: TtaFreeMemory(types);
1.94 kia 1112: TtaFreeMemory(listtypes);
1113: TtaFreeMemory(result);
1114: }
1.148 kia 1115:
1.94 kia 1116: return TRUE;
1117: #endif /* TEMPLATES */
1.56 francesc 1118: return TRUE;
1119: }
1.64 francesc 1120:
1.89 kia 1121:
1.103 kia 1122: /*----------------------------------------------------------------------
1123: UseSimpleButtonClicked
1124: ----------------------------------------------------------------------*/
1125: ThotBool UseSimpleButtonClicked (NotifyElement *event)
1126: {
1127: #ifdef TEMPLATES
1.181 ! vatton 1128: ElementType elType, parentType;
! 1129: AttributeType attrType;
! 1130: Attribute att;
! 1131:
1.112 vatton 1132: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1133: return TRUE;
1134:
1.181 ! vatton 1135: elType = TtaGetElementType (event->element);
! 1136: attrType.AttrSSchema = elType.ElSSchema;
! 1137: attrType.AttrTypeNum = Template_ATTR_option;
! 1138: att = TtaGetAttribute (event->element, attrType);
! 1139: if (att)
! 1140: return OptionButtonClicked (event);
! 1141:
! 1142: parentType = TtaGetElementType (TtaGetParent( event->element));
1.112 vatton 1143: if (parentType.ElTypeNum == Template_EL_repeat)
1.138 vatton 1144: return RepeatButtonClicked (event);
1145: else if (parentType.ElTypeNum == Template_EL_bag)
1146: return BagButtonClicked (event);
1.103 kia 1147: #endif /* TEMPLATES */
1148: return FALSE;
1149: }
1.94 kia 1150:
1151: /*----------------------------------------------------------------------
1152: OptionButtonClicked
1153: ----------------------------------------------------------------------*/
1154: ThotBool OptionButtonClicked (NotifyElement *event)
1155: {
1156: #ifdef TEMPLATES
1.145 kia 1157: Element useEl, contentEl, next;
1158: ElementType useType, optType;
1.94 kia 1159: Document doc;
1160: XTigerTemplate t;
1161: View view;
1162:
1.112 vatton 1163: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1164: return TRUE;
1165:
1.94 kia 1166: TtaGetActiveView (&doc, &view);
1167: if (view != 1)
1168: return FALSE; /* let Thot perform normal operation */
1.110 kia 1169:
1.94 kia 1170: doc = event->document;
1.145 kia 1171: useEl = TtaGetFirstChild (event->element);
1172: if (!useEl)
1.94 kia 1173: return FALSE; /* let Thot perform normal operation */
1.145 kia 1174: useType = TtaGetElementType (useEl);
1175: optType = TtaGetElementType (event->element);
1176: if ((useType.ElTypeNum != Template_EL_useEl &&
1.148 kia 1177: useType.ElTypeNum != Template_EL_useSimple) ||
1178: useType.ElSSchema != optType.ElSSchema)
1.94 kia 1179: return FALSE;
1180:
1.145 kia 1181: TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
1.148 kia 1182:
1.94 kia 1183: TtaCancelSelection (doc);
1.148 kia 1184:
1.145 kia 1185: contentEl = TtaGetFirstChild (useEl);
1186: if (!contentEl)
1.94 kia 1187: /* the "use" element is empty. Instantiate it */
1188: {
1.171 kia 1189: t = GetXTigerDocTemplate (doc);
1.94 kia 1190: if (!t)
1191: return FALSE; // no template ?!?!
1.145 kia 1192: InstantiateUse (t, useEl, doc, TRUE);
1.94 kia 1193: }
1194: else
1195: /* remove the content of the "use" element */
1196: {
1197: do
1198: {
1.145 kia 1199: next = contentEl;
1.94 kia 1200: TtaNextSibling (&next);
1.145 kia 1201: TtaRegisterElementDelete(contentEl, doc);
1202: TtaDeleteTree (contentEl, doc);
1203: contentEl = next;
1.94 kia 1204: }
1205: while (next);
1.145 kia 1206: if (NeedAMenu (useEl, doc))
1207: {
1208: TtaChangeTypeOfElement (useEl, doc, Template_EL_useEl);
1209: TtaRegisterElementTypeChange(useEl, Template_EL_useSimple, doc);
1210: }
1.94 kia 1211: }
1212: TtaSelectElement (doc, event->element);
1.145 kia 1213: TtaCloseUndoSequence(doc);
1.94 kia 1214: return TRUE; /* don't let Thot perform normal operation */
1215: #endif /* TEMPLATES */
1216: return TRUE;
1217: }
1218:
1.111 vatton 1219: /*----------------------------------------------------------------------
1220: CheckTemplate checks if the template of the instance is loaded
1.126 vatton 1221: Return TRUE if the template is loaded
1.111 vatton 1222: ----------------------------------------------------------------------*/
1223: void CheckTemplate (Document doc)
1224: {
1225: #ifdef TEMPLATES
1.144 vatton 1226: Element root;
1.165 kia 1227:
1.171 kia 1228: if(IsTemplateInstanceDocument(doc))
1.111 vatton 1229: {
1.144 vatton 1230: XTigerTemplate t;
1231:
1232: root = TtaGetRootElement (doc);
1233: TtaSetAccessRight (root, ReadOnly, doc);
1.171 kia 1234: t = GetXTigerDocTemplate (doc);
1.144 vatton 1235: if (t == NULL)
1236: {
1237: // the template cannot be loaded
1238: InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
1239: TtaSetDocumentAccessMode (doc, 0); // document readonly
1240: }
1241: else
1242: {
1243: // fix all access rights in the instance
1.171 kia 1244: Template_PrepareTemplate(t);
1.166 kia 1245: Template_FixAccessRight (t, root, doc);
1.144 vatton 1246: TtaUpdateAccessRightInViews (doc, root);
1247: }
1.111 vatton 1248: }
1249: #endif /* TEMPLATES */
1250: }
1.94 kia 1251:
1.66 vatton 1252: /*----------------------------------------------------------------------
1.108 vatton 1253: OpeningInstance checks if it is a template instance needs.
1254: If it's an instance and the template is not loaded, load it into a
1255: temporary file
1.66 vatton 1256: ----------------------------------------------------------------------*/
1.171 kia 1257: void OpeningInstance (char *localFileName, Document doc, char* docURL)
1.65 francesc 1258: {
1259: #ifdef TEMPLATES
1.76 vatton 1260: XTigerTemplate t;
1.103 kia 1261: char *content, *ptr, *begin;
1.76 vatton 1262: gzFile stream;
1263: char buffer[2000];
1.77 vatton 1264: int res;
1.171 kia 1265: char *template_version = NULL,
1266: *template_url = NULL;
1.65 francesc 1267:
1.171 kia 1268: stream = TtaGZOpen (localFileName);
1.76 vatton 1269: if (stream != 0)
1.65 francesc 1270: {
1.76 vatton 1271: res = gzread (stream, buffer, 1999);
1272: if (res >= 0)
1.65 francesc 1273: {
1.81 vatton 1274: buffer[res] = EOS;
1.103 kia 1275: begin = strstr (buffer, "<?xtiger");
1276:
1.112 vatton 1277: if (begin)
1.103 kia 1278: {
1279: // Search for template version
1280: ptr = strstr (begin, "templateVersion");
1281: if (ptr)
1282: ptr = strstr (ptr, "=");
1283: if (ptr)
1284: ptr = strstr (ptr, "\"");
1285: if (ptr)
1286: {
1287: // template URI
1288: content = &ptr[1];
1289: ptr = strstr (content, "\"");
1290: }
1291: if (ptr)
1292: {
1293: *ptr = EOS;
1294: //Get now the template URI
1.171 kia 1295: template_version = TtaStrdup (content);
1.103 kia 1296: *ptr = '"';
1297: }
1298:
1299: // Search for template uri
1300: ptr = strstr (begin, "template");
1301: if (ptr && ptr[8] != 'V')
1302: ptr = strstr (ptr, "=");
1303: if (ptr)
1304: ptr = strstr (ptr, "\"");
1305: if (ptr)
1306: {
1307: // template URI
1308: content = &ptr[1];
1309: ptr = strstr (content, "\"");
1310: }
1311: if (ptr)
1312: {
1313: *ptr = EOS;
1314: //Get now the template URI
1.171 kia 1315: template_url = TtaStrdup (content);
1316:
1317: t = GetXTigerTemplate (template_url);
1.103 kia 1318: if (!t)
1319: {
1.171 kia 1320: LoadTemplate (doc, template_url);
1321: t = GetXTigerTemplate(template_url);
1.103 kia 1322: }
1.171 kia 1323: Template_PrepareInstance(docURL, doc, template_version, template_url);
1324: template_version = NULL;
1325: template_url = NULL;
1326: Template_AddReference (t);
1.103 kia 1327: *ptr = '"';
1328: }
1329: }
1.65 francesc 1330: }
1331: }
1.171 kia 1332: TtaFreeMemory(template_version);
1333: TtaFreeMemory(template_url);
1.76 vatton 1334: TtaGZClose (stream);
1.65 francesc 1335: #endif /* TEMPLATES */
1336: }
1337:
1.64 francesc 1338: /*----------------------------------------------------------------------
1.178 kia 1339: ClosingTemplateDocument
1340: Callback called before closing a document which uses templates.
1.64 francesc 1341: ----------------------------------------------------------------------*/
1.178 kia 1342: ThotBool ClosingTemplateDocument(NotifyDialog* dialog)
1.64 francesc 1343: {
1.65 francesc 1344: #ifdef TEMPLATES
1.171 kia 1345: XTigerTemplate t = GetXTigerDocTemplate(dialog->document);
1346: if(t)
1.178 kia 1347: Template_RemoveReference(t);
1.65 francesc 1348: #endif /* TEMPLATES */
1349: return FALSE;
1.64 francesc 1350: }
1.87 kia 1351:
1352: /*----------------------------------------------------------------------
1.120 kia 1353: IsTemplateElement
1.138 vatton 1354: Test if an element is a template element.
1.87 kia 1355: ----------------------------------------------------------------------*/
1.140 vatton 1356: ThotBool IsTemplateElement (Element elem)
1.87 kia 1357: {
1358: #ifdef TEMPLATES
1.138 vatton 1359: ElementType elType;
1360:
1361: elType = TtaGetElementType(elem);
1362: if (elType.ElSSchema)
1.164 kia 1363: return (strcmp(TtaGetSSchemaName(elType.ElSSchema) , "Template") == 0);
1.138 vatton 1364: #endif /* TEMPLATES */
1.87 kia 1365: return FALSE;
1366: }
1367:
1368:
1369: /*----------------------------------------------------------------------
1370: GetFirstTemplateParentElement
1.138 vatton 1371: Return the first element which has "Template" as schema name or null.
1.87 kia 1372: ----------------------------------------------------------------------*/
1373: Element GetFirstTemplateParentElement(Element elem)
1374: {
1375: #ifdef TEMPLATES
1.138 vatton 1376: ElementType elType;
1377:
1378: elem = TtaGetParent (elem);
1379: elType = TtaGetElementType(elem);
1380: while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
1381: {
1382: elem = TtaGetParent (elem);
1383: elType = TtaGetElementType(elem);
1.87 kia 1384: }
1385: return elem;
1386: #else
1387: return NULL;
1388: #endif /* TEMPLATES */
1389: }
1.101 kia 1390:
1.103 kia 1391:
1.101 kia 1392: /*----------------------------------------------------------------------
1.102 vatton 1393: TemplateElementWillBeCreated
1.101 kia 1394: Processed when an element will be created in a template context.
1395: ----------------------------------------------------------------------*/
1.102 vatton 1396: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101 kia 1397: {
1.103 kia 1398: #ifdef TEMPLATES
1399: ElementType elType = event->elementType;
1400: Element parent = event->element;
1401: ElementType parentType = TtaGetElementType(parent);
1.113 kia 1402: Element ancestor;
1403: ElementType ancestorType;
1404: SSchema templateSSchema;
1405: char* types;
1406: ThotBool b;
1.101 kia 1407:
1.115 kia 1408: if(event->info==1)
1409: return FALSE;
1410:
1.112 vatton 1411: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1412: return TRUE;
1413:
1.138 vatton 1414: templateSSchema = TtaGetSSchema ("Template", event->document);
1.102 vatton 1415: if (templateSSchema == NULL)
1416: return FALSE; // let Thot do the job
1.115 kia 1417:
1.113 kia 1418: // Fisrt, test if in a xt:bag or in a base-element xt:use
1.147 vatton 1419: if(parentType.ElSSchema == templateSSchema)
1.113 kia 1420: ancestor = parent;
1421: else
1.147 vatton 1422: ancestor = GetFirstTemplateParentElement (parent);
1.113 kia 1423:
1.147 vatton 1424: if (ancestor)
1.113 kia 1425: {
1426: ancestorType = TtaGetElementType(ancestor);
1.147 vatton 1427: if (ancestorType.ElTypeNum == Template_EL_bag)
1.113 kia 1428: {
1.147 vatton 1429: // only check the use child
1430: if (ancestor != parent)
1431: return FALSE; // let Thot do the job
1432: if (elType.ElSSchema == templateSSchema &&
1433: (elType.ElTypeNum == Template_EL_useSimple ||
1434: elType.ElTypeNum == Template_EL_useEl))
1.116 kia 1435: return FALSE;
1.147 vatton 1436: return !Template_CanInsertElementInBagElement (event->document, elType, ancestor);
1.113 kia 1437: }
1.147 vatton 1438: else if(ancestorType.ElTypeNum == Template_EL_useSimple ||
1439: ancestorType.ElTypeNum == Template_EL_useEl)
1.113 kia 1440: {
1.147 vatton 1441: // only check the bag child @@@ will be check exclude/include later
1.172 kia 1442: if (ancestor != parent)
1443: return FALSE; // let Thot do the job
1.113 kia 1444: types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
1.128 kia 1445: b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position);
1446: TtaFreeMemory(types);
1.115 kia 1447: return !b;
1.113 kia 1448: }
1449: }
1.115 kia 1450:
1.147 vatton 1451: if (elType.ElSSchema == templateSSchema && elType.ElTypeNum == Template_EL_TEXT_UNIT)
1.115 kia 1452: {
1453: return FALSE;
1454: }
1455:
1.113 kia 1456: // Can not insert.
1457: return TRUE;
1.101 kia 1458: #endif /* TEMPLATES*/
1.102 vatton 1459: return FALSE;
1.101 kia 1460: }
1461:
1462: /*----------------------------------------------------------------------
1463: TemplateElementWillBeDeleted
1464: Processed when an element will be deleted in a template context.
1465: ----------------------------------------------------------------------*/
1466: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
1467: {
1.107 kia 1468: #ifdef TEMPLATES
1469: Document doc = event->document;
1470: Element elem = event->element;
1.163 vatton 1471: Element xtElem, parent = NULL, sibling;
1.117 kia 1472: ElementType xtType, elType;
1.107 kia 1473: char* type;
1474: Declaration dec;
1.115 kia 1475: SSchema templateSSchema;
1.107 kia 1476: XTigerTemplate t;
1.156 vatton 1477: ThotBool selparent = FALSE;
1.107 kia 1478:
1.115 kia 1479: if(event->info==1)
1480: return FALSE;
1481:
1.112 vatton 1482: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1483: return TRUE;
1484:
1.138 vatton 1485: templateSSchema = TtaGetSSchema ("Template", event->document);
1.107 kia 1486: if (templateSSchema == NULL)
1487: return FALSE; // let Thot do the job
1.122 kia 1488:
1.107 kia 1489: xtElem = GetFirstTemplateParentElement(elem);
1.112 vatton 1490: if (xtElem)
1.107 kia 1491: {
1492: xtType = TtaGetElementType(xtElem);
1.117 kia 1493:
1.171 kia 1494: t = GetXTigerDocTemplate(doc);
1.109 kia 1495:
1.112 vatton 1496: if (xtType.ElTypeNum==Template_EL_bag)
1.117 kia 1497: {
1498: elType = TtaGetElementType(elem);
1499: if(elType.ElSSchema==templateSSchema &&
1500: (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
1501: {
1502: // Remove element manually.
1503: TtaOpenUndoSequence(doc, elem, elem, 0, 0);
1504: TtaRegisterElementDelete(elem, doc);
1505: TtaDeleteTree(elem, doc);
1506: TtaCloseUndoSequence(doc);
1507: return TRUE;
1508: }
1509: else
1510: return FALSE; // xt:bag always allow remove children.
1511: }
1.112 vatton 1512: else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107 kia 1513: {
1.109 kia 1514: parent = TtaGetParent(elem);
1.117 kia 1515: if (xtElem!=parent)
1516: {
1517: type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
1518: dec = Template_GetDeclaration(t, type);
1519: TtaFreeMemory(type);
1520:
1521: if (dec && dec->nature == XmlElementNat)
1522: return FALSE; // Can remove element only if in xt:use current type is base language element.
1523: else
1524: return TRUE;
1.107 kia 1525: }
1.109 kia 1526: }
1.112 vatton 1527: else if (xtType.ElTypeNum==Template_EL_repeat)
1.109 kia 1528: {
1.156 vatton 1529: sibling = TtaGetSuccessor (elem);
1530: if (sibling == NULL)
1531: {
1532: // there is no next element
1533: sibling = TtaGetPredecessor (elem);
1534: if (sibling == NULL)
1535: selparent = TRUE;
1536: }
1.109 kia 1537: TtaRegisterElementDelete(elem, doc);
1538: TtaDeleteTree(elem, doc);
1539: InstantiateRepeat(t, xtElem, doc, TRUE);
1.156 vatton 1540: if (selparent)
1541: // look for the new sibling
1542: sibling = TtaGetFirstChild (parent);
1543: if (sibling)
1544: TtaSelectElement(doc, sibling);
1.151 quint 1545: else
1.156 vatton 1546: TtaSelectElement(doc, parent);
1.109 kia 1547: return TRUE;
1.107 kia 1548: }
1549: }
1.109 kia 1550:
1551: //TODO Test if current element is use or repeat.
1552: // Because if an element is delete and it is the unique child of its parent,
1553: // the parent intends to destroy itself.
1554:
1.107 kia 1555: return TRUE;
1556: #else /* TEMPLATES */
1.101 kia 1557: return FALSE;
1.107 kia 1558: #endif /* TEMPLATES */
1.101 kia 1559: }
1560:
1.109 kia 1561: /*----------------------------------------------------------------------
1562: CurrentTypeWillBeExported
1563: Check if the xt:currentType attribute can be exported
1564: ----------------------------------------------------------------------*/
1565: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
1566: {
1567: #ifdef TEMPLATES
1.110 kia 1568:
1.112 vatton 1569: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1570: return TRUE;
1571:
1.112 vatton 1572: if (IsTemplateDocument(event->document))
1.109 kia 1573: return TRUE;
1574: #endif /* TEMPLATES */
1575: return FALSE;
1576: }
1.127 kia 1577:
1578: /*----------------------------------------------------------------------
1579: TemplateAttrInMenu
1580: Called by Thot when building the Attributes menu for template elements.
1581: ----------------------------------------------------------------------*/
1582: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
1583: {
1584: #ifdef TEMPLATES
1585: // Prevent from showing attributes for template instance but not templates.
1586: if(IsTemplateInstanceDocument(event->document))
1587: return TRUE;
1588: else
1589: #endif /* TEMPLATES */
1590: return FALSE;
1591: }
1.160 kia 1592:
1.167 kia 1593: /*----------------------------------------------------------------------
1.168 kia 1594: CreateTemplateFromDocument
1595: Create a template from the current document.
1.167 kia 1596: ----------------------------------------------------------------------*/
1597: void CreateTemplateFromDocument(Document doc, View view)
1598: {
1.171 kia 1599: #ifdef TEMPLATES
1.168 kia 1600: char buffer[MAX_LENGTH];
1601: strcpy(buffer, DocumentURLs[doc]);
1602: strcat(buffer, ".xtd");
1603: DontReplaceOldDoc = TRUE;
1604: CreateTemplate(doc, buffer);
1.171 kia 1605: #endif /* TEMPLATES */
1.167 kia 1606: }
1607:
1.169 kia 1608: /*----------------------------------------------------------------------
1609: UpdateTemplateMenus
1610: ----------------------------------------------------------------------*/
1611: void UpdateTemplateMenus (Document doc)
1612: {
1.180 kia 1613: if(IsTemplateInstanceDocument(doc))
1614: {
1615: // Instance document
1616: TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
1617: TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1618: TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1619: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
1620: }
1621: else if (IsTemplateDocument(doc))
1622: {
1623: // Template document
1624: TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
1625: TtaSetItemOn (doc, 1, Tools, BTemplateCreateTextBox);
1626: TtaSetItemOn (doc, 1, Tools, BTemplateCreateFreeBox);
1627: TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeat);
1628: }
1.169 kia 1629: else
1.180 kia 1630: {
1631: //Standard document
1632: TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
1633: TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1634: TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1635: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
1636: }
1.169 kia 1637: }
1.171 kia 1638:
1639: /*----------------------------------------------------------------------
1640: UninstanciateTemplateDocument
1641: An instance of a template is tranformed into a template-less docuemnt.
1642: Remove link between XTigerTemplate structure and document.
1643: ----------------------------------------------------------------------*/
1644: void UninstanciateTemplateDocument(Document doc)
1645: {
1646: #ifdef TEMPLATES
1647: XTigerTemplate t = GetXTigerDocTemplate(doc);
1648: if(t)
1649: Template_Close(t);
1650: #endif /* TEMPLATES */
1651: }
1652:
1653:
1654: /*----------------------------------------------------------------------
1655: Template_PrepareInstance
1656: Allocate XTigerTemplate structure for instance and initialize template
1657: url and template version.
1658: ----------------------------------------------------------------------*/
1659: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
1660: {
1661: #ifdef TEMPLATES
1662: XTigerTemplate t = GetXTigerTemplate(fileName);
1663: if(!t)
1664: t = NewXTigerTemplate(fileName);
1665: t->state = templInstance;
1666: t->templateVersion = template_version;
1667: t->base_uri = template_url;
1668: t->doc = doc;
1.178 kia 1669: t->ref = 1;
1.171 kia 1670:
1671: #endif /* TEMPLATES */
1672: }
1673:
1674:
1675: /*----------------------------------------------------------------------
1676: SetDocumentAsXTigerTemplate
1677: Set the document template structure as template.
1678: ----------------------------------------------------------------------*/
1679: void SetDocumentAsXTigerTemplate(Document doc)
1680: {
1681: #ifdef TEMPLATES
1682: XTigerTemplate t = GetXTigerDocTemplate(doc);
1683: if(t)
1684: t->state |= templTemplate;
1685: #endif /* TEMPLATES */
1686: }
1687:
1688: /*----------------------------------------------------------------------
1689: SetDocumentAsXTigerLibrary
1690: Set the document template structure as template library.
1691: ----------------------------------------------------------------------*/
1692: void SetDocumentAsXTigerLibrary(Document doc)
1693: {
1694: #ifdef TEMPLATES
1695: XTigerTemplate t = GetXTigerDocTemplate(doc);
1696: if(t)
1697: t->state |= templLibrary;
1698: #endif /* TEMPLATES */
1699: }
1.174 kia 1700:
1701:
1702: /*----------------------------------------------------------------------
1703: TemplateCreateTextBox
1704: Create a xt:use types="string" box around the selection.
1705: ----------------------------------------------------------------------*/
1706: void TemplateCreateTextBox(Document doc, View view)
1707: {
1708: #ifdef TEMPLATES
1709: Element selElem;
1710: ElementType selType;
1711: int firstChar, lastChar;
1712: SSchema sstempl = TtaGetSSchema ("Template", doc);
1713: ElementType useType;
1714: Element use;
1715: char buffer[128];
1716:
1717: char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
1718: char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING_LABEL);
1719:
1720: if (!TtaGetDocumentAccessMode(doc))
1721: return;
1722:
1723: if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1724: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
1725: {
1726: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1727: if(selElem)
1728: {
1729: selType = TtaGetElementType(selElem);
1730: if(!TtaIsLeaf(selType))
1731: {
1732: selElem = TtaGetFirstLeaf(selElem);
1733: selType = TtaGetElementType(selElem);
1734: firstChar = lastChar = 0;
1735: }
1736:
1737: if(selType.ElTypeNum==1)
1738: {
1739: QueryStringFromUser(label, title, buffer, 127);
1740: useType.ElSSchema = sstempl;
1741: useType.ElTypeNum = Template_EL_useSimple;
1742:
1743: if(firstChar==0)
1744: {
1745: use = TtaNewElement(doc, useType);
1746: if(use)
1747: {
1748: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1749: TtaInsertSibling(use, selElem, FALSE, doc);
1750: TtaRegisterElementCreate(use, doc);
1751: TtaRegisterElementDelete (selElem, doc);
1752: TtaRemoveTree(selElem, doc);
1753: TtaInsertFirstChild(&selElem, use, doc);
1754: TtaRegisterElementDelete (selElem, doc);
1755: SetAttributeStringValue(use, Template_ATTR_types, "string");
1756: SetAttributeStringValue(use, Template_ATTR_title, buffer);
1757: TtaCloseUndoSequence(doc);
1758: TtaSelectElement(doc, use);
1759: }
1760: }
1761: else
1762: {
1763: GenerateInlineElement(Template_EL_useSimple, sstempl, 0, "", TRUE);
1764: TtaGiveFirstSelectedElement(doc, &use, &firstChar, &lastChar);
1765: selType = TtaGetElementType(use);
1766: if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_useSimple)
1767: {
1768: SetAttributeStringValue(use, Template_ATTR_types, "string");
1769: SetAttributeStringValue(use, Template_ATTR_title, buffer);
1770: }
1771: }
1772: }
1773:
1774: }
1775: }
1776: #endif /* TEMPLATES */
1777: }
1778:
1779:
1780: /*----------------------------------------------------------------------
1781: TemplateCreateFreeBox
1782: Create a xt:bag types="string" box around the selection.
1783: ----------------------------------------------------------------------*/
1784: void TemplateCreateFreeBox(Document doc, View view)
1785: {
1786: #ifdef TEMPLATES
1787: Element selElem, selElem2;
1788: ElementType selType, selType2;
1789: int firstChar, lastChar, firstChar2, lastChar2;
1790: SSchema sstempl = TtaGetSSchema ("Template", doc);
1791:
1792: ElementType bagType;
1793: Element bag;
1794: char buffer[128];
1795:
1796: char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
1797: char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY_LABEL);
1798:
1799: if (!TtaGetDocumentAccessMode(doc))
1800: return;
1801:
1802: if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1803: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
1804: {
1805: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1806: TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1807:
1808: if(selElem && selElem2)
1809: {
1810: selType = TtaGetElementType(selElem);
1811: selType2 = TtaGetElementType(selElem2);
1812:
1813: // printf("Selection %d %p %d %d\n", selType.ElTypeNum, selElem, firstChar, lastChar);
1814: // printf(" %d %p %d %d\n", selType2.ElTypeNum, selElem2, firstChar2, lastChar2);
1815:
1816: QueryStringFromUser(label, title, buffer, 127);
1817: bagType.ElSSchema = sstempl;
1818: bagType.ElTypeNum = Template_EL_bag;
1819:
1820: if(selElem==selElem2)
1821: {
1822: if(firstChar==0)
1823: {
1824: ThotBool oldStructureChecking;
1825: DisplayMode dispMode;
1826: dispMode = TtaGetDisplayMode (doc);
1827: if (dispMode == DisplayImmediately)
1828: TtaSetDisplayMode (doc, DeferredDisplay);
1829: oldStructureChecking = TtaGetStructureChecking (doc);
1830: TtaSetStructureChecking (FALSE, doc);
1831:
1832: // Only one element fully selected
1833: bag = TtaNewElement(doc, bagType);
1834:
1835: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1836: TtaInsertSibling(bag, selElem, FALSE, doc);
1837: TtaRegisterElementCreate(bag, doc);
1838: TtaRegisterElementDelete (selElem, doc);
1839: TtaRemoveTree(selElem, doc);
1840: TtaInsertFirstChild(&selElem, bag, doc);
1841: TtaRegisterElementDelete (selElem, doc);
1842: SetAttributeStringValue(bag, Template_ATTR_types, "any");
1843: SetAttributeStringValue(bag, Template_ATTR_title, buffer);
1844: TtaCloseUndoSequence(doc);
1845: TtaSelectElement(doc, bag);
1846:
1847: TtaSetStructureChecking (oldStructureChecking, doc);
1848: TtaSetDisplayMode (doc, dispMode);
1849:
1850: }
1851: }
1852: }
1853: }
1854: #endif /* TEMPLATES */
1855: }
1856:
1.180 kia 1857: /*----------------------------------------------------------------------
1858: TemplateCreateRepeat
1859: Create a xt:repeat around the selection.
1860: ----------------------------------------------------------------------*/
1861: void TemplateCreateRepeat(Document doc, View view)
1862: {
1863: #ifdef TEMPLATES
1864: Element selElem, selElem2;
1865: ElementType selType, selType2;
1866: int firstChar, lastChar, firstChar2, lastChar2;
1867: SSchema sstempl = TtaGetSSchema ("Template", doc);
1868:
1869: ElementType repType, compType;
1870: Element rep, comp;
1871: char buffer[128];
1872:
1873: char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATCOMP);
1874: char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATCOMP_LABEL);
1875:
1876: if (!TtaGetDocumentAccessMode(doc))
1877: return;
1878:
1879: if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1880: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
1881: {
1882: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1883: TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1884:
1885: if(selElem && selElem2)
1886: {
1887: selType = TtaGetElementType(selElem);
1888: selType2 = TtaGetElementType(selElem2);
1889:
1890: QueryStringFromUser(label, title, buffer, 127);
1891: repType.ElSSchema = sstempl;
1892: repType.ElTypeNum = Template_EL_repeat;
1893: compType.ElSSchema = sstempl;
1894: compType.ElTypeNum = Template_EL_component;
1895:
1896:
1897: if(selElem==selElem2)
1898: {
1899: if(firstChar==0)
1900: {
1901: ThotBool oldStructureChecking;
1902: DisplayMode dispMode;
1903: dispMode = TtaGetDisplayMode (doc);
1904: if (dispMode == DisplayImmediately)
1905: TtaSetDisplayMode (doc, DeferredDisplay);
1906: oldStructureChecking = TtaGetStructureChecking (doc);
1907: TtaSetStructureChecking (FALSE, doc);
1908:
1909: // Only one element fully selected
1910:
1911: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1912:
1913: rep = TtaNewElement(doc, repType);
1914: TtaInsertSibling(rep, selElem, FALSE, doc);
1915: TtaRegisterElementCreate(rep, doc);
1916:
1917: comp = TtaNewElement(doc, compType);
1918: TtaInsertFirstChild(&comp, rep, doc);
1919: TtaRegisterElementCreate(comp, doc);
1920:
1921: TtaRegisterElementDelete (selElem, doc);
1922: TtaRemoveTree(selElem, doc);
1923: TtaInsertFirstChild(&selElem, comp, doc);
1924: TtaRegisterElementDelete (selElem, doc);
1925: SetAttributeStringValue(comp, Template_ATTR_name, buffer);
1926: TtaCloseUndoSequence(doc);
1927: TtaSelectElement(doc, rep);
1928:
1929: TtaSetStructureChecking (oldStructureChecking, doc);
1930: TtaSetDisplayMode (doc, dispMode);
1931:
1932: }
1933: }
1934: }
1935: }
1936: #endif /* TEMPLATES */
1937: }
Webmaster