Annotation of Amaya/amaya/templates.c, revision 1.205
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.202 kia 10: * Émilien Kia
1.1 cvs 11: *
12: */
13:
1.194 kia 14:
1.1 cvs 15: #define THOT_EXPORT extern
16: #include "amaya.h"
17: #include "document.h"
1.99 kia 18: #include "undo.h"
1.90 kia 19: #include "containers.h"
20: #include "Elemlist.h"
1.92 kia 21: #include "templates.h"
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.174 kia 28: #include "HTMLedit_f.h"
1.122 kia 29: #include "templates_f.h"
1.89 kia 30: #include "templateUtils_f.h"
1.67 quint 31: #include "templateLoad_f.h"
32: #include "templateDeclarations_f.h"
1.76 vatton 33: #include "templateInstantiate_f.h"
1.145 kia 34: #include "Templatebuilder_f.h"
1.28 tollenae 35: #include "appdialogue_wx.h"
1.29 tollenae 36: #include "init_f.h"
1.46 vatton 37: #include "wxdialogapi_f.h"
1.60 francesc 38: #include "AHTURLTools_f.h"
1.52 vatton 39: #endif /* TEMPLATES */
1.1 cvs 40:
1.87 kia 41: #include "fetchXMLname_f.h"
1.83 kia 42: #include "MENUconf.h"
43:
44: /* Paths from which looking for templates.*/
45: static Prop_Templates_Path *TemplateRepositoryPaths;
46:
1.87 kia 47: /*----------------------------------------------------------------------
48: IsTemplateInstanceDocument: Test if a document is a template instance
49: doc : Document to test
50: return : TRUE if the document is a template instance
51: ----------------------------------------------------------------------*/
1.106 vatton 52: ThotBool IsTemplateInstanceDocument(Document doc)
53: {
1.87 kia 54: #ifdef TEMPLATES
1.171 kia 55: XTigerTemplate t = GetXTigerDocTemplate(doc);
56: if(t)
1.176 vatton 57: return (t->state & templInstance) != 0;
1.171 kia 58: else
59: return FALSE;
60: #else /* TEMPLATES */
1.88 cvs 61: return FALSE;
1.171 kia 62: #endif /* TEMPLATES */
1.87 kia 63: }
64:
1.83 kia 65: /*----------------------------------------------------------------------
1.109 kia 66: IsTemplateDocument: Test if a document is a template (not an instance)
67: doc : Document to test
1.167 kia 68: return : TRUE if the document is a template
1.109 kia 69: ----------------------------------------------------------------------*/
1.183 vatton 70: ThotBool IsTemplateDocument (Document doc)
1.109 kia 71: {
72: #ifdef TEMPLATES
1.171 kia 73: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.183 vatton 74: if (t)
75: return ((t->state & templTemplate) != 0);
1.171 kia 76: else
77: return FALSE;
1.183 vatton 78: #endif /* TEMPLATES */
1.171 kia 79: return FALSE;
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.185 kia 606:
607: /*----------------------------------------------------------------------
1.71 quint 608: UseToBeCreated
609: An new use element will be created by the user through some generic editing
610: command
611: -----------------------------------------------------------------------*/
612: ThotBool UseToBeCreated (NotifyElement *event)
613: {
614: #ifdef TEMPLATES
1.122 kia 615: ElementType parentType;
1.138 vatton 616: SSchema templateSSchema = TtaGetSSchema ("Template", event->document);
1.130 vatton 617: if (templateSSchema)
1.122 kia 618: {
1.130 vatton 619: parentType = TtaGetElementType (event->element);
620: if (parentType.ElSSchema == templateSSchema &&
621: parentType.ElTypeNum == Template_EL_repeat)
622: return !Template_CanInsertRepeatChild (event->element);
623: return TemplateElementWillBeCreated (event);
1.122 kia 624: }
1.52 vatton 625: #endif /* TEMPLATES */
1.71 quint 626: return FALSE; /* let Thot perform normal operation */
627: }
628:
629: /*----------------------------------------------------------------------
630: UseCreated
631: A new "use" element has just been created by the user with a generic editing
632: command.
633: -----------------------------------------------------------------------*/
634: void UseCreated (NotifyElement *event)
635: {
636: #ifdef TEMPLATES
1.148 kia 637: Document doc = event->document;
638: Element el = event->element;
1.117 kia 639: Element parent;
640: Element first;
641: ElementType parentType;
642: XTigerTemplate t;
643: SSchema templateSSchema;
1.130 vatton 644: char* types, *text = NULL;
1.148 kia 645:
1.112 vatton 646: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 647: return;
648:
1.72 quint 649: if (TtaGetFirstChild (el))
650: /* this Use element has already some content. It has already been
651: instanciated */
652: return;
1.117 kia 653:
1.171 kia 654: t = GetXTigerDocTemplate (doc);
1.71 quint 655: if (!t)
656: return; // no template ?!?!
1.101 kia 657:
1.138 vatton 658: templateSSchema = TtaGetSSchema ("Template", doc);
1.117 kia 659: parent = TtaGetParent(el);
660: parentType = TtaGetElementType(parent);
1.148 kia 661:
1.130 vatton 662: if (parentType.ElSSchema == templateSSchema &&
663: parentType.ElTypeNum == Template_EL_repeat)
1.117 kia 664: {
1.130 vatton 665: first = TtaGetFirstChild (parent);
666: if (first)
1.117 kia 667: {
1.130 vatton 668: types = GetAttributeStringValueFromNum (first, Template_ATTR_types, NULL);
669: if (types)
1.117 kia 670: {
1.130 vatton 671: SetAttributeStringValueWithUndo (el, Template_ATTR_types, types);
672: text = GetAttributeStringValueFromNum (el, Template_ATTR_title, NULL);
673: SetAttributeStringValueWithUndo (first, Template_ATTR_title, text);
674: TtaFreeMemory (text);
675: text = GetAttributeStringValueFromNum (el, Template_ATTR_currentType, NULL);
676: SetAttributeStringValueWithUndo (first, Template_ATTR_currentType, text);
677: TtaFreeMemory (text);
678: TtaFreeMemory (types);
1.117 kia 679: }
680: }
681: }
682:
1.76 vatton 683: InstantiateUse (t, el, doc, TRUE);
1.71 quint 684: #endif /* TEMPLATES */
685: }
1.29 tollenae 686:
1.89 kia 687:
1.98 kia 688: /*----------------------------------------------------------------------
689: Template_CanInsertRepeatChild
690: Test if a xt:repeat child can be inserted (number between params min and max).
691: @param el element (xt:repeat) to test
692: @return True if an element can be inserted.
693: ----------------------------------------------------------------------*/
694: ThotBool Template_CanInsertRepeatChild(Element el)
695: {
696: #ifdef TEMPLATES
1.154 vatton 697: char *max;
698: int maxVal, curVal;
699: Element child;
1.148 kia 700:
1.104 kia 701: max = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
1.191 kia 702: if (max && max[0]!=EOS)
1.104 kia 703: {
1.154 vatton 704: if (!strcmp(max, "*"))
705: {
706: TtaFreeMemory(max);
707: return TRUE;
708: }
709: maxVal = atoi (max);
710: TtaFreeMemory (max);
1.104 kia 711: curVal = 0;
1.105 vatton 712: for (child = TtaGetFirstChild(el); child; TtaNextSibling(&child))
1.104 kia 713: curVal++;
1.154 vatton 714: return (curVal < maxVal);
1.104 kia 715: }
716: else
1.98 kia 717: return TRUE;
718: #endif /* TEMPLATES */
719: return FALSE;
720: }
1.96 kia 721:
1.89 kia 722:
1.92 kia 723: #ifdef TEMPLATES
1.99 kia 724: /*----------------------------------------------------------------------
725: QueryStringFromMenu
726: Show a context menu to query a choice.
727: @param items space-separated choice list string.
728: @return The choosed item string or NULL if none.
729: ----------------------------------------------------------------------*/
1.138 vatton 730: static char* QueryStringFromMenu (Document doc, char* items)
1.90 kia 731: {
732: int nbitems, size;
733: struct menuType *itemlist;
734: char *menuString;
735: char *result = NULL;
1.148 kia 736:
1.138 vatton 737: if (!TtaGetDocumentAccessMode (doc))
1.112 vatton 738: return NULL;
739: if (items == NULL)
1.110 kia 740: return NULL;
1.138 vatton 741: size = strlen (items);
742: if (size == 0)
743: return NULL;
1.90 kia 744: giveItems (items, size, &itemlist, &nbitems);
745: menuString = createMenuString (itemlist, nbitems);
746: TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL,
747: nbitems, menuString , NULL, false, 'L');
748: TtaFreeMemory (menuString);
749: ReturnOption = -1;
750: TtaShowDialogue (BaseDialog + OptionMenu, FALSE);
751: TtaWaitShowProcDialogue ();
752: TtaDestroyDialogue (BaseDialog + OptionMenu);
1.148 kia 753:
1.112 vatton 754: if (ReturnOption!=-1)
1.90 kia 755: {
756: result = TtaStrdup(itemlist[ReturnOption].label);
757: }
758:
759: TtaFreeMemory (itemlist);
760: return result;
761: }
1.92 kia 762: #endif /* TEMPLATES */
1.90 kia 763:
1.158 kia 764: #ifdef AMAYA_DEBUG
765: void FillInsertableElemList (Document doc, Element elem, DLList list);
766: #endif /* AMAYA_DEBUG */
1.162 vatton 767: /*----------------------------------------------------------------------
768: ----------------------------------------------------------------------*/
769: char *Template_GetListTypes (XTigerTemplate t, Element el)
770: {
771: #ifdef TEMPLATES
772: char *listtypes = NULL, *types;
773:
774: types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
775: if (types)
776: listtypes = Template_ExpandTypes (t, types, el, FALSE);
777: return listtypes;
778: #endif /* TEMPLATES */
779: }
1.158 kia 780:
1.56 francesc 781: /*----------------------------------------------------------------------
1.138 vatton 782: BagButtonClicked
783: Called when a bag button is clicked.
784: Can be called for useEl, useSimple or bag.
785: If called for useEl or useSimple, the new element must be added after.
786: If called for bag, the element must be added before all.
787:
788: Shows a menu with all the types that can be used in the bag.
789: ----------------------------------------------------------------------*/
790: ThotBool BagButtonClicked (NotifyElement *event)
791: {
792: #ifdef TEMPLATES
793: Document doc = event->document;
794: Element el = event->element;
795: ElementType elType;
796: XTigerTemplate t;
797: Declaration decl;
798: Element bagEl = el;
799: Element firstEl;
800: Element newEl = NULL;
801: View view;
802: SSchema templateSSchema;
1.162 vatton 803: char *listtypes = NULL;
804: char *result = NULL;
1.138 vatton 805: ThotBool oldStructureChecking;
806: DisplayMode dispMode;
807:
1.182 vatton 808: if (!TtaGetDocumentAccessMode (doc))
1.138 vatton 809: return TRUE;
1.182 vatton 810: if (!IsTemplateInstanceDocument (doc))
811: return FALSE; /* let Thot perform normal operation */
1.148 kia 812:
1.138 vatton 813: TtaGetActiveView (&doc, &view);
814: if (view != 1)
815: return FALSE; /* let Thot perform normal operation */
816:
817: TtaCancelSelection (doc);
818: templateSSchema = TtaGetSSchema ("Template", doc);
1.171 kia 819: t = GetXTigerDocTemplate(doc);
1.138 vatton 820: elType = TtaGetElementType (el);
821: while (bagEl &&
822: (elType.ElSSchema != templateSSchema ||
823: elType.ElTypeNum != Template_EL_bag))
824: {
825: bagEl = TtaGetParent (bagEl);
826: elType = TtaGetElementType (bagEl);
827: }
828:
829: if (bagEl)
830: {
1.162 vatton 831: listtypes = Template_GetListTypes (t, bagEl);
832: if (listtypes)
1.138 vatton 833: {
1.158 kia 834: #ifdef AMAYA_DEBUG
835: printf("BagButtonClicked : \n > %s\n", listtypes);
836: // {
837: // DLList list = DLList_Create();
838: // FillInsertableElemList (doc, TtaGetFirstChild(bagEl), list);
839: // DLListNode node;
840: // ForwardIterator iter = DLList_GetForwardIterator(list);
841: // ITERATOR_FOREACH(iter, DLListNode, node)
842: // {
843: // ElemListElement elem = (ElemListElement)node->elem;
844: // printf(" + %s\n", ElemListElement_GetName(elem));
845: // }
846: // DLList_Destroy(list);
847: // }
848: #endif /* AMAYA_DEBUG */
1.138 vatton 849: result = QueryStringFromMenu (doc, listtypes);
850: TtaFreeMemory (listtypes);
851: if (result)
852: {
853: decl = Template_GetDeclaration (t, result);
854: if (decl)
855: {
856: dispMode = TtaGetDisplayMode (doc);
857: if (dispMode == DisplayImmediately)
858: /* don't set NoComputedDisplay
859: -> it breaks down views formatting when Enter generates new elements */
1.139 vatton 860: TtaSetDisplayMode (doc, DeferredDisplay);
1.138 vatton 861:
862: /* Prepare insertion.*/
863: oldStructureChecking = TtaGetStructureChecking (doc);
864: TtaSetStructureChecking (FALSE, doc);
865: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
866:
867: /* Insert */
868: if (el == bagEl)
869: {
870: el = TtaGetFirstChild (el);
871: TtaSelectElement (doc, el);
872: TtaInsertAnyElement (doc, TRUE);
873: }
874: else
875: {
876: TtaSelectElement (doc, el);
877: TtaInsertAnyElement (doc, FALSE);
878: }
1.155 kia 879: newEl = Template_InsertBagChild (doc, bagEl, decl, FALSE);
1.138 vatton 880:
881: /* Finish insertion.*/
882: TtaCloseUndoSequence (doc);
883: TtaSetDocumentModified (doc);
884: TtaSetStructureChecking (oldStructureChecking, doc);
885: // restore the display
886: TtaSetDisplayMode (doc, dispMode);
887: firstEl = GetFirstEditableElement (newEl);
888: if (firstEl)
889: {
890: TtaSelectElement (doc, firstEl);
891: TtaSetStatusSelectedElement (doc, view, firstEl);
892: }
893: else
894: {
895: TtaSelectElement (doc, newEl);
896: TtaSetStatusSelectedElement (doc, view, newEl);
897: }
898: }
899: }
900: }
901: TtaFreeMemory (result);
902: }
903: #endif /* TEMPLATES */
904: return TRUE; /* don't let Thot perform normal operation */
905: }
906:
907: /*----------------------------------------------------------------------
1.79 quint 908: RepeatButtonClicked
1.89 kia 909: Called when a repeat button is clicked.
910: Can be called for useEl, useSimple or repeat.
911: If called for useEl or useSimple, the new element must be added after.
912: If called for repeat, the element must be added before all.
913:
1.56 francesc 914: Shows a menu with all the types that can be used in a use element.
915: ----------------------------------------------------------------------*/
1.79 quint 916: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56 francesc 917: {
918: #ifdef TEMPLATES
1.89 kia 919: Document doc = event->document;
920: Element el = event->element;
921: ElementType elType;
1.90 kia 922: XTigerTemplate t;
923: Declaration decl;
924: Element repeatEl = el;
925: Element firstEl;
926: Element newEl = NULL;
1.95 kia 927: View view;
1.162 vatton 928: char *listtypes = NULL;
929: char *result = NULL;
1.138 vatton 930: ThotBool oldStructureChecking;
931: DisplayMode dispMode;
1.104 kia 932:
1.112 vatton 933: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 934: return TRUE;
1.182 vatton 935: if (!IsTemplateInstanceDocument (doc))
936: return FALSE; /* let Thot perform normal operation */
1.148 kia 937:
1.95 kia 938: TtaGetActiveView (&doc, &view);
939: if (view != 1)
940: return FALSE; /* let Thot perform normal operation */
941:
1.89 kia 942: TtaCancelSelection(doc);
1.171 kia 943: t = GetXTigerDocTemplate(doc);
1.89 kia 944: elType = TtaGetElementType(el);
1.138 vatton 945: while (elType.ElTypeNum != Template_EL_repeat)
1.179 kia 946: {
947: repeatEl = TtaGetParent(repeatEl);
948: if (repeatEl == NULL)
949: break;
950: elType = TtaGetElementType(repeatEl);
951: }
1.112 vatton 952: if (repeatEl)
1.90 kia 953: {
1.179 kia 954: if (Template_CanInsertRepeatChild (repeatEl))
955: {
956: firstEl = TtaGetFirstChild (repeatEl);
957: listtypes = Template_GetListTypes (t, firstEl);
958: if (listtypes)
959: {
1.158 kia 960: #ifdef AMAYA_DEBUG
1.179 kia 961: printf("RepeatButtonClicked : \n > %s\n", listtypes);
1.158 kia 962: #endif /* AMAYA_DEBUG */
1.179 kia 963:
964: result = QueryStringFromMenu (doc, listtypes);
965: TtaFreeMemory (listtypes);
966: if (result)
967: {
968: decl = Template_GetDeclaration (t, result);
969: if (decl)
970: {
971: dispMode = TtaGetDisplayMode (doc);
972: if (dispMode == DisplayImmediately)
973: /* don't set NoComputedDisplay
1.138 vatton 974: -> it breaks down views formatting when Enter generates new elements */
1.179 kia 975: TtaSetDisplayMode (doc, DeferredDisplay);
976:
977: /* Prepare insertion.*/
978: oldStructureChecking = TtaGetStructureChecking (doc);
979: TtaSetStructureChecking (FALSE, doc);
980: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
981: /* Insert. */
982: if (el == repeatEl)
983: newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, NULL);
984: else
985: newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, el);
1.138 vatton 986:
1.179 kia 987: /* Finish insertion.*/
988: TtaCloseUndoSequence(doc);
989: TtaSetDocumentModified (doc);
990: TtaSetStructureChecking (oldStructureChecking, doc);
991:
992: // restore the display
993: TtaSetDisplayMode (doc, dispMode);
994: firstEl = GetFirstEditableElement (newEl);
995: if (firstEl)
996: {
997: TtaSelectElement (doc, firstEl);
998: TtaSetStatusSelectedElement (doc, view, firstEl);
999: }
1000: else
1001: {
1002: TtaSelectElement (doc, newEl);
1003: TtaSetStatusSelectedElement (doc, view, newEl);
1004: }
1005: }
1006: }
1.104 kia 1007: }
1.179 kia 1008: TtaFreeMemory(result);
1009: DumpSubtree(repeatEl, doc, 0);
1010:
1011: }
1012: else /* if (Template_CanInsertRepeatChild(repeatEl)) */
1013: {
1014: TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.98 kia 1015: }
1016: }
1.77 vatton 1017: return TRUE; /* don't let Thot perform normal operation */
1.57 francesc 1018: #endif /* TEMPLATES */
1.94 kia 1019: return TRUE;
1020: }
1021:
1022: /*----------------------------------------------------------------------
1023: UseButtonClicked
1024: Shows a menu with all the types that can be used in a use element.
1025: ----------------------------------------------------------------------*/
1026: ThotBool UseButtonClicked (NotifyElement *event)
1027: {
1028: #ifdef TEMPLATES
1029: Document doc = event->document;
1030: Element el = event->element;
1.99 kia 1031: Element child;
1.94 kia 1032: ElementType elType;
1033: XTigerTemplate t;
1034: Declaration decl;
1035: Element firstEl;
1036: Element newEl = NULL;
1037: char* types;
1038: ThotBool oldStructureChecking;
1.95 kia 1039: View view;
1.133 vatton 1040: char* listtypes = NULL;
1041: char* result = NULL;
1.95 kia 1042:
1.182 vatton 1043: if (!TtaGetDocumentAccessMode (doc))
1.110 kia 1044: return TRUE;
1.182 vatton 1045: if (!IsTemplateInstanceDocument (doc))
1046: return FALSE; /* let Thot perform normal operation */
1.110 kia 1047:
1.95 kia 1048: TtaGetActiveView (&doc, &view);
1049: if (view != 1)
1050: return FALSE; /* let Thot perform normal operation */
1.148 kia 1051:
1.94 kia 1052: TtaCancelSelection(doc);
1.171 kia 1053: t = GetXTigerDocTemplate(doc);
1.94 kia 1054: if (!t)
1055: return FALSE; /* let Thot perform normal operation */
1056: elType = TtaGetElementType(el);
1057:
1058: firstEl = TtaGetFirstChild(el);
1.112 vatton 1059: if (firstEl)
1.94 kia 1060: {
1061: RepeatButtonClicked(event);
1062: }
1063: else
1064: {
1.104 kia 1065: types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.112 vatton 1066: if (types)
1.94 kia 1067: {
1.142 kia 1068: listtypes = Template_ExpandTypes(t, types, NULL, FALSE);
1.158 kia 1069: #ifdef AMAYA_DEBUG
1070: printf("UseButtonClicked : \n > %s\n", listtypes);
1071: #endif /* AMAYA_DEBUG */
1072:
1.104 kia 1073: result = QueryStringFromMenu(doc, listtypes);
1.112 vatton 1074: if (result)
1.94 kia 1075: {
1.104 kia 1076: decl = Template_GetDeclaration(t, result);
1.112 vatton 1077: if (decl)
1.99 kia 1078: {
1.104 kia 1079: /* Prepare insertion.*/
1080: oldStructureChecking = TtaGetStructureChecking (doc);
1081: TtaSetStructureChecking (FALSE, doc);
1.138 vatton 1082: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.104 kia 1083:
1084: /* Insert */
1085: newEl = Template_InsertUseChildren(doc, el, decl);
1086: for(child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
1087: {
1088: TtaRegisterElementCreate(child, doc);
1089: }
1090:
1091: TtaChangeTypeOfElement(el, doc, Template_EL_useSimple);
1092: TtaRegisterElementTypeChange(el, Template_EL_useEl, doc);
1093:
1.117 kia 1094: /* xt:currentType attribute.*/
1095: SetAttributeStringValueWithUndo(el, Template_ATTR_currentType, result);
1096:
1.104 kia 1097: /* Finish insertion. */
1098: TtaCloseUndoSequence(doc);
1.114 vatton 1099: TtaSetDocumentModified (doc);
1.104 kia 1100: TtaSetStructureChecking (oldStructureChecking, doc);
1101:
1102: firstEl = GetFirstEditableElement(newEl);
1.112 vatton 1103: if (firstEl)
1.104 kia 1104: {
1105: TtaSelectElement (doc, firstEl);
1106: TtaSetStatusSelectedElement(doc, view, firstEl);
1107: }
1108: else
1109: {
1110: TtaSelectElement (doc, newEl);
1111: TtaSetStatusSelectedElement(doc, view, newEl);
1112: }
1.98 kia 1113: }
1.94 kia 1114: }
1115: }
1.104 kia 1116: TtaFreeMemory(types);
1.94 kia 1117: TtaFreeMemory(listtypes);
1118: TtaFreeMemory(result);
1119: }
1.148 kia 1120:
1.94 kia 1121: return TRUE;
1122: #endif /* TEMPLATES */
1.56 francesc 1123: return TRUE;
1124: }
1.64 francesc 1125:
1.89 kia 1126:
1.103 kia 1127: /*----------------------------------------------------------------------
1128: UseSimpleButtonClicked
1129: ----------------------------------------------------------------------*/
1130: ThotBool UseSimpleButtonClicked (NotifyElement *event)
1131: {
1132: #ifdef TEMPLATES
1.181 vatton 1133: ElementType elType, parentType;
1134: AttributeType attrType;
1135: Attribute att;
1136:
1.182 vatton 1137: if (!TtaGetDocumentAccessMode (event->document))
1.110 kia 1138: return TRUE;
1.182 vatton 1139: if (!IsTemplateInstanceDocument (event->document))
1140: return FALSE; /* let Thot perform normal operation */
1.110 kia 1141:
1.181 vatton 1142: elType = TtaGetElementType (event->element);
1143: attrType.AttrSSchema = elType.ElSSchema;
1144: attrType.AttrTypeNum = Template_ATTR_option;
1145: att = TtaGetAttribute (event->element, attrType);
1146: if (att)
1147: return OptionButtonClicked (event);
1148:
1149: parentType = TtaGetElementType (TtaGetParent( event->element));
1.112 vatton 1150: if (parentType.ElTypeNum == Template_EL_repeat)
1.138 vatton 1151: return RepeatButtonClicked (event);
1152: else if (parentType.ElTypeNum == Template_EL_bag)
1153: return BagButtonClicked (event);
1.103 kia 1154: #endif /* TEMPLATES */
1155: return FALSE;
1156: }
1.94 kia 1157:
1158: /*----------------------------------------------------------------------
1159: OptionButtonClicked
1160: ----------------------------------------------------------------------*/
1161: ThotBool OptionButtonClicked (NotifyElement *event)
1162: {
1163: #ifdef TEMPLATES
1.145 kia 1164: Element useEl, contentEl, next;
1.182 vatton 1165: ElementType useType;
1.94 kia 1166: Document doc;
1167: XTigerTemplate t;
1168: View view;
1169:
1.182 vatton 1170: if (!TtaGetDocumentAccessMode (event->document))
1.110 kia 1171: return TRUE;
1.182 vatton 1172: if (!IsTemplateInstanceDocument (event->document))
1173: return FALSE; /* let Thot perform normal operation */
1.110 kia 1174:
1.94 kia 1175: TtaGetActiveView (&doc, &view);
1176: if (view != 1)
1177: return FALSE; /* let Thot perform normal operation */
1.110 kia 1178:
1.94 kia 1179: doc = event->document;
1.182 vatton 1180: useEl = event->element;
1.145 kia 1181: if (!useEl)
1.94 kia 1182: return FALSE; /* let Thot perform normal operation */
1.145 kia 1183: useType = TtaGetElementType (useEl);
1.182 vatton 1184: if (useType.ElTypeNum != Template_EL_useEl &&
1185: useType.ElTypeNum != Template_EL_useSimple)
1.94 kia 1186: return FALSE;
1187:
1.145 kia 1188: TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
1.94 kia 1189: TtaCancelSelection (doc);
1.145 kia 1190: contentEl = TtaGetFirstChild (useEl);
1191: if (!contentEl)
1.94 kia 1192: /* the "use" element is empty. Instantiate it */
1193: {
1.171 kia 1194: t = GetXTigerDocTemplate (doc);
1.94 kia 1195: if (!t)
1196: return FALSE; // no template ?!?!
1.145 kia 1197: InstantiateUse (t, useEl, doc, TRUE);
1.94 kia 1198: }
1199: else
1200: /* remove the content of the "use" element */
1201: {
1202: do
1203: {
1.145 kia 1204: next = contentEl;
1.94 kia 1205: TtaNextSibling (&next);
1.145 kia 1206: TtaRegisterElementDelete(contentEl, doc);
1207: TtaDeleteTree (contentEl, doc);
1208: contentEl = next;
1.94 kia 1209: }
1210: while (next);
1.145 kia 1211: if (NeedAMenu (useEl, doc))
1212: {
1213: TtaChangeTypeOfElement (useEl, doc, Template_EL_useEl);
1214: TtaRegisterElementTypeChange(useEl, Template_EL_useSimple, doc);
1215: }
1.94 kia 1216: }
1217: TtaSelectElement (doc, event->element);
1.145 kia 1218: TtaCloseUndoSequence(doc);
1.94 kia 1219: return TRUE; /* don't let Thot perform normal operation */
1220: #endif /* TEMPLATES */
1221: return TRUE;
1222: }
1223:
1.191 kia 1224:
1225:
1.111 vatton 1226: /*----------------------------------------------------------------------
1.191 kia 1227: Template_FillFromDocument
1228: Fill XTigerTemplate structure with the content of the document.
1229: Load dependencies if needed.
1.111 vatton 1230: ----------------------------------------------------------------------*/
1.191 kia 1231: void Template_FillFromDocument (Document doc)
1.111 vatton 1232: {
1233: #ifdef TEMPLATES
1.191 kia 1234: XTigerTemplate t = GetXTigerTemplate (DocumentURLs[doc]);
1235: Element root;
1.165 kia 1236:
1.193 vatton 1237: #ifdef AMAYA_DEBUG
1.191 kia 1238: printf("Template_FillFromDocument\n");
1.193 vatton 1239: #endif
1240: if (t)
1.111 vatton 1241: {
1.193 vatton 1242: #ifdef AMAYA_DEBUG
1.191 kia 1243: printf("plop : %d\n", t->state);
1.193 vatton 1244: #endif
1.191 kia 1245: SetTemplateDocument (t, doc);
1.144 vatton 1246:
1.191 kia 1247: Template_PrepareTemplate(t);
1248:
1249: if(IsTemplateInstanceDocument(doc))
1.144 vatton 1250: {
1.193 vatton 1251: #ifdef AMAYA_DEBUG
1.191 kia 1252: printf(" > instance\n");
1.193 vatton 1253: #endif
1.144 vatton 1254: // fix all access rights in the instance
1.191 kia 1255: root = TtaGetRootElement (doc);
1256: TtaSetAccessRight (root, ReadOnly, doc);
1.166 kia 1257: Template_FixAccessRight (t, root, doc);
1.144 vatton 1258: TtaUpdateAccessRightInViews (doc, root);
1259: }
1.193 vatton 1260: #ifdef AMAYA_DEBUG
1.191 kia 1261: else if(t->state&templLibraryFlag)
1262: printf(" > library\n");
1263: else if(t->state&templTemplate)
1264: printf(" > template\n");
1.193 vatton 1265: #endif
1.191 kia 1266: // Mark loaded
1267: t->state |= templloaded;
1268: TtaSetDocumentUnmodified (doc);
1269: UpdateTemplateMenus (doc);
1270:
1271: #ifdef AMAYA_DEBUG
1272: DumpAllDeclarations();
1273: #endif /* AMAYA_DEBUG */
1274: }
1275: #endif /* TEMPLATES */
1276: }
1277:
1278:
1279: /*----------------------------------------------------------------------
1280: Template_CheckAndPrepareTemplate checks if the document is a template
1281: or a library and prepare XTigerTemplate structure to use it.
1282: ----------------------------------------------------------------------*/
1283: ThotBool Template_CheckAndPrepareTemplate(char* docURL)
1284: {
1285: #ifdef TEMPLATES
1286: XTigerTemplate t = NULL; //GetXTigerTemplate(docURL);
1.193 vatton 1287:
1288: #ifdef AMAYA_DEBUG
1.191 kia 1289: printf("Template_CheckAndPrepareTemplate %s\n", docURL);
1.193 vatton 1290: #endif
1291: if (IsXTiger(docURL))
1.191 kia 1292: {
1293: t = LookForXTigerTemplate(docURL);
1294: t->state |= templTemplate;
1.111 vatton 1295: }
1.193 vatton 1296: else if (IsXTigerLibrary(docURL))
1.191 kia 1297: {
1298: t = LookForXTigerLibrary(docURL);
1299: t->state |= templLibrary;
1300: }
1.193 vatton 1301: return t != NULL;
1.111 vatton 1302: #endif /* TEMPLATES */
1.191 kia 1303: return FALSE;
1.111 vatton 1304: }
1.94 kia 1305:
1.191 kia 1306:
1.66 vatton 1307: /*----------------------------------------------------------------------
1.191 kia 1308: Template_CheckAndPrepareInstance checks if it is a template instance needs.
1.108 vatton 1309: If it's an instance and the template is not loaded, load it into a
1310: temporary file
1.66 vatton 1311: ----------------------------------------------------------------------*/
1.191 kia 1312: void Template_CheckAndPrepareInstance (char *localFileName, Document doc, char* docURL)
1.65 francesc 1313: {
1314: #ifdef TEMPLATES
1.76 vatton 1315: XTigerTemplate t;
1.103 kia 1316: char *content, *ptr, *begin;
1.76 vatton 1317: gzFile stream;
1318: char buffer[2000];
1.77 vatton 1319: int res;
1.171 kia 1320: char *template_version = NULL,
1321: *template_url = NULL;
1.65 francesc 1322:
1.171 kia 1323: stream = TtaGZOpen (localFileName);
1.76 vatton 1324: if (stream != 0)
1.65 francesc 1325: {
1.76 vatton 1326: res = gzread (stream, buffer, 1999);
1327: if (res >= 0)
1.65 francesc 1328: {
1.81 vatton 1329: buffer[res] = EOS;
1.103 kia 1330: begin = strstr (buffer, "<?xtiger");
1331:
1.112 vatton 1332: if (begin)
1.103 kia 1333: {
1334: // Search for template version
1335: ptr = strstr (begin, "templateVersion");
1336: if (ptr)
1337: ptr = strstr (ptr, "=");
1338: if (ptr)
1339: ptr = strstr (ptr, "\"");
1340: if (ptr)
1341: {
1342: // template URI
1343: content = &ptr[1];
1344: ptr = strstr (content, "\"");
1345: }
1346: if (ptr)
1347: {
1348: *ptr = EOS;
1349: //Get now the template URI
1.171 kia 1350: template_version = TtaStrdup (content);
1.103 kia 1351: *ptr = '"';
1352: }
1353:
1354: // Search for template uri
1355: ptr = strstr (begin, "template");
1356: if (ptr && ptr[8] != 'V')
1357: ptr = strstr (ptr, "=");
1358: if (ptr)
1359: ptr = strstr (ptr, "\"");
1360: if (ptr)
1361: {
1362: // template URI
1363: content = &ptr[1];
1364: ptr = strstr (content, "\"");
1365: }
1366: if (ptr)
1367: {
1368: *ptr = EOS;
1369: //Get now the template URI
1.171 kia 1370: template_url = TtaStrdup (content);
1371:
1372: t = GetXTigerTemplate (template_url);
1.103 kia 1373: if (!t)
1374: {
1.171 kia 1375: LoadTemplate (doc, template_url);
1376: t = GetXTigerTemplate(template_url);
1.103 kia 1377: }
1.171 kia 1378: Template_PrepareInstance(docURL, doc, template_version, template_url);
1379: template_version = NULL;
1380: template_url = NULL;
1381: Template_AddReference (t);
1.103 kia 1382: *ptr = '"';
1383: }
1384: }
1.65 francesc 1385: }
1386: }
1.171 kia 1387: TtaFreeMemory(template_version);
1388: TtaFreeMemory(template_url);
1.76 vatton 1389: TtaGZClose (stream);
1.65 francesc 1390: #endif /* TEMPLATES */
1391: }
1392:
1.64 francesc 1393: /*----------------------------------------------------------------------
1.178 kia 1394: ClosingTemplateDocument
1395: Callback called before closing a document which uses templates.
1.64 francesc 1396: ----------------------------------------------------------------------*/
1.178 kia 1397: ThotBool ClosingTemplateDocument(NotifyDialog* dialog)
1.64 francesc 1398: {
1.65 francesc 1399: #ifdef TEMPLATES
1.171 kia 1400: XTigerTemplate t = GetXTigerDocTemplate(dialog->document);
1401: if(t)
1.178 kia 1402: Template_RemoveReference(t);
1.65 francesc 1403: #endif /* TEMPLATES */
1404: return FALSE;
1.64 francesc 1405: }
1.87 kia 1406:
1407: /*----------------------------------------------------------------------
1.120 kia 1408: IsTemplateElement
1.138 vatton 1409: Test if an element is a template element.
1.87 kia 1410: ----------------------------------------------------------------------*/
1.140 vatton 1411: ThotBool IsTemplateElement (Element elem)
1.87 kia 1412: {
1413: #ifdef TEMPLATES
1.138 vatton 1414: ElementType elType;
1415:
1416: elType = TtaGetElementType(elem);
1417: if (elType.ElSSchema)
1.164 kia 1418: return (strcmp(TtaGetSSchemaName(elType.ElSSchema) , "Template") == 0);
1.138 vatton 1419: #endif /* TEMPLATES */
1.87 kia 1420: return FALSE;
1421: }
1422:
1423:
1424: /*----------------------------------------------------------------------
1425: GetFirstTemplateParentElement
1.138 vatton 1426: Return the first element which has "Template" as schema name or null.
1.87 kia 1427: ----------------------------------------------------------------------*/
1428: Element GetFirstTemplateParentElement(Element elem)
1429: {
1430: #ifdef TEMPLATES
1.138 vatton 1431: ElementType elType;
1432:
1433: elem = TtaGetParent (elem);
1434: elType = TtaGetElementType(elem);
1435: while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
1436: {
1437: elem = TtaGetParent (elem);
1438: elType = TtaGetElementType(elem);
1.87 kia 1439: }
1440: return elem;
1441: #else
1442: return NULL;
1443: #endif /* TEMPLATES */
1444: }
1.101 kia 1445:
1.103 kia 1446:
1.101 kia 1447: /*----------------------------------------------------------------------
1.102 vatton 1448: TemplateElementWillBeCreated
1.101 kia 1449: Processed when an element will be created in a template context.
1450: ----------------------------------------------------------------------*/
1.102 vatton 1451: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101 kia 1452: {
1.103 kia 1453: #ifdef TEMPLATES
1454: ElementType elType = event->elementType;
1455: Element parent = event->element;
1456: ElementType parentType = TtaGetElementType(parent);
1.113 kia 1457: Element ancestor;
1458: ElementType ancestorType;
1459: SSchema templateSSchema;
1460: char* types;
1461: ThotBool b;
1.101 kia 1462:
1.115 kia 1463: if(event->info==1)
1464: return FALSE;
1465:
1.193 vatton 1466: #ifdef AMAYA_DEBUG
1.185 kia 1467: printf("TemplateElementWillBeCreated\n");
1.193 vatton 1468: #endif
1.112 vatton 1469: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1470: return TRUE;
1471:
1.138 vatton 1472: templateSSchema = TtaGetSSchema ("Template", event->document);
1.102 vatton 1473: if (templateSSchema == NULL)
1474: return FALSE; // let Thot do the job
1.115 kia 1475:
1.113 kia 1476: // Fisrt, test if in a xt:bag or in a base-element xt:use
1.147 vatton 1477: if(parentType.ElSSchema == templateSSchema)
1.113 kia 1478: ancestor = parent;
1479: else
1.147 vatton 1480: ancestor = GetFirstTemplateParentElement (parent);
1.113 kia 1481:
1.147 vatton 1482: if (ancestor)
1.113 kia 1483: {
1484: ancestorType = TtaGetElementType(ancestor);
1.147 vatton 1485: if (ancestorType.ElTypeNum == Template_EL_bag)
1.113 kia 1486: {
1.147 vatton 1487: // only check the use child
1488: if (ancestor != parent)
1489: return FALSE; // let Thot do the job
1490: if (elType.ElSSchema == templateSSchema &&
1491: (elType.ElTypeNum == Template_EL_useSimple ||
1492: elType.ElTypeNum == Template_EL_useEl))
1.116 kia 1493: return FALSE;
1.147 vatton 1494: return !Template_CanInsertElementInBagElement (event->document, elType, ancestor);
1.113 kia 1495: }
1.147 vatton 1496: else if(ancestorType.ElTypeNum == Template_EL_useSimple ||
1497: ancestorType.ElTypeNum == Template_EL_useEl)
1.113 kia 1498: {
1.147 vatton 1499: // only check the bag child @@@ will be check exclude/include later
1.172 kia 1500: if (ancestor != parent)
1501: return FALSE; // let Thot do the job
1.113 kia 1502: types = GetAttributeStringValueFromNum(ancestor, Template_ATTR_currentType, NULL);
1.128 kia 1503: b = Template_CanInsertElementInUse(event->document, elType, types, parent, event->position);
1504: TtaFreeMemory(types);
1.115 kia 1505: return !b;
1.113 kia 1506: }
1507: }
1.115 kia 1508:
1.200 kia 1509: if (elType.ElSSchema == templateSSchema && (
1510: elType.ElTypeNum == Template_EL_TEXT_UNIT ||
1511: elType.ElTypeNum == Template_EL_component))
1.115 kia 1512: {
1513: return FALSE;
1514: }
1515:
1.113 kia 1516: // Can not insert.
1517: return TRUE;
1.101 kia 1518: #endif /* TEMPLATES*/
1.102 vatton 1519: return FALSE;
1.101 kia 1520: }
1521:
1522: /*----------------------------------------------------------------------
1523: TemplateElementWillBeDeleted
1524: Processed when an element will be deleted in a template context.
1525: ----------------------------------------------------------------------*/
1526: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
1527: {
1.107 kia 1528: #ifdef TEMPLATES
1529: Document doc = event->document;
1530: Element elem = event->element;
1.163 vatton 1531: Element xtElem, parent = NULL, sibling;
1.117 kia 1532: ElementType xtType, elType;
1.107 kia 1533: char* type;
1534: Declaration dec;
1.115 kia 1535: SSchema templateSSchema;
1.107 kia 1536: XTigerTemplate t;
1.156 vatton 1537: ThotBool selparent = FALSE;
1.192 kia 1538:
1.115 kia 1539: if(event->info==1)
1540: return FALSE;
1541:
1.112 vatton 1542: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1543: return TRUE;
1544:
1.138 vatton 1545: templateSSchema = TtaGetSSchema ("Template", event->document);
1.107 kia 1546: if (templateSSchema == NULL)
1547: return FALSE; // let Thot do the job
1.192 kia 1548:
1549: t = GetXTigerDocTemplate(doc);
1550: if(Template_IsTemplate(t)||Template_IsLibrary(t))
1551: return FALSE; // If template or library, pass to specialized functions.
1.122 kia 1552:
1.107 kia 1553: xtElem = GetFirstTemplateParentElement(elem);
1.112 vatton 1554: if (xtElem)
1.107 kia 1555: {
1556: xtType = TtaGetElementType(xtElem);
1.117 kia 1557:
1.192 kia 1558:
1.109 kia 1559:
1.112 vatton 1560: if (xtType.ElTypeNum==Template_EL_bag)
1.117 kia 1561: {
1562: elType = TtaGetElementType(elem);
1563: if(elType.ElSSchema==templateSSchema &&
1564: (elType.ElTypeNum==Template_EL_useSimple || elType.ElTypeNum==Template_EL_useEl))
1565: {
1566: // Remove element manually.
1567: TtaOpenUndoSequence(doc, elem, elem, 0, 0);
1568: TtaRegisterElementDelete(elem, doc);
1569: TtaDeleteTree(elem, doc);
1570: TtaCloseUndoSequence(doc);
1571: return TRUE;
1572: }
1573: else
1574: return FALSE; // xt:bag always allow remove children.
1575: }
1.112 vatton 1576: else if (xtType.ElTypeNum==Template_EL_useSimple || xtType.ElTypeNum==Template_EL_useEl)
1.107 kia 1577: {
1.109 kia 1578: parent = TtaGetParent(elem);
1.117 kia 1579: if (xtElem!=parent)
1580: {
1581: type = GetAttributeStringValueFromNum(xtElem, Template_ATTR_currentType, NULL);
1582: dec = Template_GetDeclaration(t, type);
1583: TtaFreeMemory(type);
1584:
1585: if (dec && dec->nature == XmlElementNat)
1586: return FALSE; // Can remove element only if in xt:use current type is base language element.
1587: else
1588: return TRUE;
1.107 kia 1589: }
1.109 kia 1590: }
1.112 vatton 1591: else if (xtType.ElTypeNum==Template_EL_repeat)
1.109 kia 1592: {
1.156 vatton 1593: sibling = TtaGetSuccessor (elem);
1594: if (sibling == NULL)
1595: {
1596: // there is no next element
1597: sibling = TtaGetPredecessor (elem);
1598: if (sibling == NULL)
1599: selparent = TRUE;
1600: }
1.109 kia 1601: TtaRegisterElementDelete(elem, doc);
1602: TtaDeleteTree(elem, doc);
1603: InstantiateRepeat(t, xtElem, doc, TRUE);
1.156 vatton 1604: if (selparent)
1605: // look for the new sibling
1606: sibling = TtaGetFirstChild (parent);
1607: if (sibling)
1608: TtaSelectElement(doc, sibling);
1.151 quint 1609: else
1.156 vatton 1610: TtaSelectElement(doc, parent);
1.109 kia 1611: return TRUE;
1.107 kia 1612: }
1613: }
1.109 kia 1614:
1615: //TODO Test if current element is use or repeat.
1616: // Because if an element is delete and it is the unique child of its parent,
1617: // the parent intends to destroy itself.
1618:
1.107 kia 1619: return TRUE;
1620: #else /* TEMPLATES */
1.101 kia 1621: return FALSE;
1.107 kia 1622: #endif /* TEMPLATES */
1.101 kia 1623: }
1624:
1.109 kia 1625: /*----------------------------------------------------------------------
1626: CurrentTypeWillBeExported
1627: Check if the xt:currentType attribute can be exported
1628: ----------------------------------------------------------------------*/
1629: ThotBool CurrentTypeWillBeExported (NotifyAttribute *event)
1630: {
1631: #ifdef TEMPLATES
1.110 kia 1632:
1.112 vatton 1633: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1634: return TRUE;
1635:
1.112 vatton 1636: if (IsTemplateDocument(event->document))
1.109 kia 1637: return TRUE;
1638: #endif /* TEMPLATES */
1639: return FALSE;
1640: }
1.127 kia 1641:
1642: /*----------------------------------------------------------------------
1643: TemplateAttrInMenu
1644: Called by Thot when building the Attributes menu for template elements.
1645: ----------------------------------------------------------------------*/
1646: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
1647: {
1648: #ifdef TEMPLATES
1649: // Prevent from showing attributes for template instance but not templates.
1650: if(IsTemplateInstanceDocument(event->document))
1651: return TRUE;
1652: else
1653: #endif /* TEMPLATES */
1654: return FALSE;
1655: }
1.160 kia 1656:
1.167 kia 1657: /*----------------------------------------------------------------------
1.168 kia 1658: CreateTemplateFromDocument
1659: Create a template from the current document.
1.167 kia 1660: ----------------------------------------------------------------------*/
1661: void CreateTemplateFromDocument(Document doc, View view)
1662: {
1.171 kia 1663: #ifdef TEMPLATES
1.205 ! vatton 1664: char buffer[MAX_LENGTH], suffix[10];
! 1665: ThotBool with_suffix;
1.196 vatton 1666:
1.204 vatton 1667: if (IsW3Path (DocumentURLs[doc]) &&
1.205 ! vatton 1668: DocumentMeta[doc] && DocumentMeta[doc]->full_content_location)
! 1669: {
1.196 vatton 1670: // use the location instead of the current URI
1.205 ! vatton 1671:
! 1672: strcpy (buffer, DocumentMeta[doc]->full_content_location);
! 1673: }
1.204 vatton 1674: else
1675: strcpy (buffer, DocumentURLs[doc]);
1676:
1677: // remove the current suffix
1678: TtaExtractSuffix (buffer, suffix);
1.196 vatton 1679: // the new suffix
1.168 kia 1680: strcat(buffer, ".xtd");
1681: DontReplaceOldDoc = TRUE;
1682: CreateTemplate(doc, buffer);
1.204 vatton 1683: // by default .xtd files are xml files
1684: TtaSetDocumentCharset (doc, UTF_8, FALSE);
1.171 kia 1685: #endif /* TEMPLATES */
1.167 kia 1686: }
1687:
1.169 kia 1688: /*----------------------------------------------------------------------
1689: UpdateTemplateMenus
1690: ----------------------------------------------------------------------*/
1691: void UpdateTemplateMenus (Document doc)
1692: {
1.196 vatton 1693: if (IsTemplateInstanceDocument(doc))
1.180 kia 1694: {
1695: // Instance document
1696: TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
1697: TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187 kia 1698: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195 kia 1699: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
1700: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
1701: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180 kia 1702: TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196 vatton 1703: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.180 kia 1704: }
1.183 vatton 1705: else if (DocumentURLs[doc] && IsXTiger (DocumentURLs[doc]))
1.180 kia 1706: {
1707: // Template document
1708: TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
1709: TtaSetItemOn (doc, 1, Tools, BTemplateCreateTextBox);
1.187 kia 1710: TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseBox);
1.195 kia 1711: TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseCompBox);
1712: TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeat);
1713: TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180 kia 1714: TtaSetItemOn (doc, 1, Tools, BTemplateCreateFreeBox);
1.196 vatton 1715: TtaSetItemOn (doc, 1, Tools, BTemplateCreateUnion);
1.180 kia 1716: }
1.169 kia 1717: else
1.180 kia 1718: {
1719: //Standard document
1720: TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
1721: TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187 kia 1722: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195 kia 1723: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
1724: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
1725: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180 kia 1726: TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196 vatton 1727: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.180 kia 1728: }
1.169 kia 1729: }
1.171 kia 1730:
1731: /*----------------------------------------------------------------------
1732: UninstanciateTemplateDocument
1733: An instance of a template is tranformed into a template-less docuemnt.
1734: Remove link between XTigerTemplate structure and document.
1735: ----------------------------------------------------------------------*/
1736: void UninstanciateTemplateDocument(Document doc)
1737: {
1738: #ifdef TEMPLATES
1739: XTigerTemplate t = GetXTigerDocTemplate(doc);
1740: if(t)
1741: Template_Close(t);
1742: #endif /* TEMPLATES */
1743: }
1744:
1745:
1746: /*----------------------------------------------------------------------
1747: Template_PrepareInstance
1748: Allocate XTigerTemplate structure for instance and initialize template
1749: url and template version.
1750: ----------------------------------------------------------------------*/
1751: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
1752: {
1753: #ifdef TEMPLATES
1754: XTigerTemplate t = GetXTigerTemplate(fileName);
1755: if(!t)
1756: t = NewXTigerTemplate(fileName);
1757: t->state = templInstance;
1758: t->templateVersion = template_version;
1759: t->base_uri = template_url;
1760: t->doc = doc;
1.178 kia 1761: t->ref = 1;
1.171 kia 1762:
1763: #endif /* TEMPLATES */
1764: }
1765:
1766:
1767: /*----------------------------------------------------------------------
1768: SetDocumentAsXTigerTemplate
1769: Set the document template structure as template.
1770: ----------------------------------------------------------------------*/
1771: void SetDocumentAsXTigerTemplate(Document doc)
1772: {
1773: #ifdef TEMPLATES
1774: XTigerTemplate t = GetXTigerDocTemplate(doc);
1775: if(t)
1776: t->state |= templTemplate;
1777: #endif /* TEMPLATES */
1778: }
1779:
1780: /*----------------------------------------------------------------------
1781: SetDocumentAsXTigerLibrary
1782: Set the document template structure as template library.
1783: ----------------------------------------------------------------------*/
1784: void SetDocumentAsXTigerLibrary(Document doc)
1785: {
1786: #ifdef TEMPLATES
1787: XTigerTemplate t = GetXTigerDocTemplate(doc);
1788: if(t)
1789: t->state |= templLibrary;
1790: #endif /* TEMPLATES */
1791: }
1.174 kia 1792:
1793:
1794: /*----------------------------------------------------------------------
1795: TemplateCreateTextBox
1796: Create a xt:use types="string" box around the selection.
1797: ----------------------------------------------------------------------*/
1798: void TemplateCreateTextBox(Document doc, View view)
1799: {
1800: #ifdef TEMPLATES
1801: Element selElem;
1802: ElementType selType;
1803: int firstChar, lastChar;
1804: SSchema sstempl = TtaGetSSchema ("Template", doc);
1805: ElementType useType;
1806: Element use;
1807: char buffer[128];
1.189 kia 1808: ThotBool oldStructureChecking;
1.174 kia 1809:
1810: char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
1.184 vatton 1811: char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174 kia 1812:
1813: if (!TtaGetDocumentAccessMode(doc))
1814: return;
1815:
1816: if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1817: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
1818: {
1819: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1820: if(selElem)
1821: {
1822: selType = TtaGetElementType(selElem);
1823: if(!TtaIsLeaf(selType))
1824: {
1825: selElem = TtaGetFirstLeaf(selElem);
1826: selType = TtaGetElementType(selElem);
1827: firstChar = lastChar = 0;
1828: }
1829:
1830: if(selType.ElTypeNum==1)
1831: {
1832: QueryStringFromUser(label, title, buffer, 127);
1833: useType.ElSSchema = sstempl;
1834: useType.ElTypeNum = Template_EL_useSimple;
1835:
1.189 kia 1836: oldStructureChecking = TtaGetStructureChecking (doc);
1837: TtaSetStructureChecking (FALSE, doc);
1838:
1.174 kia 1839: if(firstChar==0)
1840: {
1841: use = TtaNewElement(doc, useType);
1842: if(use)
1843: {
1844: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1845: TtaInsertSibling(use, selElem, FALSE, doc);
1846: TtaRegisterElementCreate(use, doc);
1847: TtaRegisterElementDelete (selElem, doc);
1848: TtaRemoveTree(selElem, doc);
1849: TtaInsertFirstChild(&selElem, use, doc);
1850: TtaRegisterElementDelete (selElem, doc);
1851: SetAttributeStringValue(use, Template_ATTR_types, "string");
1852: SetAttributeStringValue(use, Template_ATTR_title, buffer);
1853: TtaCloseUndoSequence(doc);
1854: TtaSelectElement(doc, use);
1855: }
1856: }
1857: else
1858: {
1859: GenerateInlineElement(Template_EL_useSimple, sstempl, 0, "", TRUE);
1860: TtaGiveFirstSelectedElement(doc, &use, &firstChar, &lastChar);
1861: selType = TtaGetElementType(use);
1862: if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_useSimple)
1863: {
1864: SetAttributeStringValue(use, Template_ATTR_types, "string");
1865: SetAttributeStringValue(use, Template_ATTR_title, buffer);
1866: }
1867: }
1.189 kia 1868:
1869: TtaSetStructureChecking (oldStructureChecking, doc);
1870:
1.174 kia 1871: }
1872:
1873: }
1874: }
1875: #endif /* TEMPLATES */
1876: }
1877:
1878:
1879: /*----------------------------------------------------------------------
1880: TemplateCreateFreeBox
1881: Create a xt:bag types="string" box around the selection.
1882: ----------------------------------------------------------------------*/
1883: void TemplateCreateFreeBox(Document doc, View view)
1884: {
1885: #ifdef TEMPLATES
1.185 kia 1886: Element selElem, selElem2, parent, parent2, current, child = NULL;
1.174 kia 1887: ElementType selType, selType2;
1888: int firstChar, lastChar, firstChar2, lastChar2;
1889: SSchema sstempl = TtaGetSSchema ("Template", doc);
1890:
1891: ElementType bagType;
1892: Element bag;
1893: char buffer[128];
1894:
1895: char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
1.184 vatton 1896: char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.174 kia 1897:
1898: if (!TtaGetDocumentAccessMode(doc))
1899: return;
1900:
1901: if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1902: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
1903: {
1904: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1905: TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1906:
1907: if(selElem && selElem2)
1908: {
1909: selType = TtaGetElementType(selElem);
1910: selType2 = TtaGetElementType(selElem2);
1911:
1912: QueryStringFromUser(label, title, buffer, 127);
1913: bagType.ElSSchema = sstempl;
1914: bagType.ElTypeNum = Template_EL_bag;
1915:
1.185 kia 1916: parent = TtaGetParent(selElem);
1917: parent2 = TtaGetParent(selElem2);
1918:
1919: if(firstChar==0 && firstChar2==0 && parent == parent2)
1.174 kia 1920: {
1.185 kia 1921: ThotBool oldStructureChecking;
1922: DisplayMode dispMode;
1923: dispMode = TtaGetDisplayMode (doc);
1924: if (dispMode == DisplayImmediately)
1925: TtaSetDisplayMode (doc, DeferredDisplay);
1926: oldStructureChecking = TtaGetStructureChecking (doc);
1927: TtaSetStructureChecking (FALSE, doc);
1928:
1929: // Create and insert xt:bag element
1930: bag = TtaNewElement(doc, bagType);
1931: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1932: TtaInsertSibling(bag, selElem, TRUE, doc);
1933: TtaRegisterElementCreate(bag, doc);
1934:
1935: TtaNextSibling(&selElem2);
1936: while(selElem!=selElem2)
1.174 kia 1937: {
1.185 kia 1938: current = selElem;
1939: TtaNextSibling(&selElem);
1.174 kia 1940:
1.185 kia 1941: TtaRegisterElementDelete (current, doc);
1942: TtaRemoveTree(current, doc);
1943: if(child)
1944: TtaInsertSibling(current, child, FALSE, doc);
1945: else
1946: TtaInsertFirstChild(¤t, bag, doc);
1947: TtaRegisterElementDelete (current, doc);
1948: child = current;
1.174 kia 1949: }
1.185 kia 1950:
1951: SetAttributeStringValue(bag, Template_ATTR_types, "any");
1952: SetAttributeStringValue(bag, Template_ATTR_title, buffer);
1953: TtaCloseUndoSequence(doc);
1954: TtaSelectElement(doc, bag);
1955:
1956: TtaSetStructureChecking (oldStructureChecking, doc);
1957: TtaSetDisplayMode (doc, dispMode);
1.174 kia 1958: }
1959: }
1960: }
1961: #endif /* TEMPLATES */
1962: }
1963:
1.180 kia 1964: /*----------------------------------------------------------------------
1.198 kia 1965: TemplateCreateUnion
1966: Create a xt:union around the selection.
1967: ----------------------------------------------------------------------*/
1968: void TemplateCreateUnion(Document doc, View view)
1969: {
1970: #ifdef TEMPLATES
1.199 kia 1971: SSchema sstempl = TtaGetSSchema ("Template", doc);
1972: Element head, sibling, unionEl;
1973: ElementType unionType;
1974: XTigerTemplate t = GetXTigerDocTemplate(doc);
1975: char *proposed, *name = NULL, *types=NULL;
1.200 kia 1976:
1977: Element selElem;
1978: ElementType selType;
1979: int firstChar, lastChar;
1980:
1.199 kia 1981:
1982: if(t && sstempl)
1983: {
1.200 kia 1984: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1985: selType = TtaGetElementType(selElem);
1986: if(selType.ElSSchema==sstempl && selType.ElTypeNum==Template_EL_union)
1987: Template_ModifyUnionElement(doc, selElem);
1988: else
1.199 kia 1989: {
1.200 kia 1990: proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
1991: if(QueryUnionFromUser(proposed, NULL, &name, &types, TRUE))
1992: {
1993: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1994:
1995: head = TemplateFindHead(doc);
1996: sibling = TtaGetLastChild(head);
1997:
1998: unionType.ElSSchema = sstempl;
1999: unionType.ElTypeNum = Template_EL_union;
2000: unionEl = TtaNewElement(doc, unionType);
2001:
2002: if(sibling)
2003: TtaInsertSibling(unionEl, sibling, FALSE, doc);
2004: else
2005: {
2006: sibling = unionEl;
2007: TtaInsertFirstChild(&sibling, head, doc);
2008: }
2009: Template_SetName(doc, unionEl, name, TRUE);
2010:
2011: SetAttributeStringValue(unionEl, Template_ATTR_includeAt, types);
2012: TtaSetAccessRight(unionEl, ReadOnly, doc);
2013:
2014: TtaRegisterElementCreate(unionEl, doc);
2015:
2016: TtaSelectElement(doc, unionEl);
2017: TtaCloseUndoSequence(doc);
2018:
2019: Template_DeclareNewUnion (t, name, types, "");
2020: TtaFreeMemory(proposed);
2021: TtaFreeMemory(name);
2022: TtaFreeMemory(types);
2023: }
2024: }
2025: }
1.199 kia 2026:
1.200 kia 2027: #endif /* TEMPLATES */
2028: }
1.199 kia 2029:
1.200 kia 2030: /*----------------------------------------------------------------------
2031: Template_ModifyUnionElement
2032: Query the user to modify an xt:union
2033: ----------------------------------------------------------------------*/
2034: void Template_ModifyUnionElement(Document doc, Element unionEl)
2035: {
2036: XTigerTemplate t = GetXTigerDocTemplate(doc);
2037: SSchema sstempl = TtaGetSSchema ("Template", doc);
2038: ElementType unionType;
2039: char *proposed, *checked, *name, *types=NULL;
2040:
2041: if(doc && unionEl && t && sstempl)
2042: {
2043: unionType = TtaGetElementType(unionEl);
2044: if(unionType.ElSSchema==sstempl && unionType.ElTypeNum==Template_EL_union)
2045: {
2046: proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
2047: checked = GetAttributeStringValueFromNum(unionEl, Template_ATTR_includeAt, NULL);
2048: name = GetAttributeStringValueFromNum(unionEl, Template_ATTR_name, NULL);
2049: if(QueryUnionFromUser(proposed, checked, &name, &types, FALSE))
1.199 kia 2050: {
1.200 kia 2051: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2052: SetAttributeStringValueWithUndo(unionEl, Template_ATTR_includeAt, types);
2053: TtaCloseUndoSequence(doc);
1.199 kia 2054: }
1.200 kia 2055: TtaFreeMemory(proposed);
2056: TtaFreeMemory(checked);
2057: TtaFreeMemory(name);
2058: TtaFreeMemory(types);
1.199 kia 2059: }
1.200 kia 2060: }
1.198 kia 2061: }
2062:
2063: /*----------------------------------------------------------------------
1.180 kia 2064: TemplateCreateRepeat
2065: Create a xt:repeat around the selection.
2066: ----------------------------------------------------------------------*/
2067: void TemplateCreateRepeat(Document doc, View view)
2068: {
1.198 kia 2069: Template_CreateRepeatFromSelection(doc, FALSE);
2070: }
2071:
2072: /*----------------------------------------------------------------------
2073: TemplateCreateRepeatComp
2074: Create a xt:component with the selection and use it in a new xt:repeat
2075: ----------------------------------------------------------------------*/
2076: void TemplateCreateRepeatComp(Document doc, View view)
2077: {
2078: Template_CreateRepeatFromSelection(doc, TRUE);
2079: }
2080:
2081: /*----------------------------------------------------------------------
2082: Template_CreateRepeatFromSelection
2083: Create a xt:repeat with the selection.
2084: If selection is empty, insert an inline xt:use.
2085: If createComp is false, create a xt:use with types at the selected block.
2086: If createComp is true, create a component with the selection.
2087: Return the xt:use element.
2088: ----------------------------------------------------------------------*/
2089: Element Template_CreateRepeatFromSelection(Document doc, ThotBool createComp)
2090: {
1.180 kia 2091: #ifdef TEMPLATES
1.187 kia 2092: ThotBool oldStructureChecking;
2093: DisplayMode dispMode;
2094: Element selElem, selElem2, parent, parent2;
2095: ElementType selType, selType2;
1.180 kia 2096: int firstChar, lastChar, firstChar2, lastChar2;
2097: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.197 kia 2098: const char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATCOMP);
2099: const char *label = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATLABEL);
2100: char buffer[128];
1.180 kia 2101:
1.186 kia 2102: ElementType repType;
1.187 kia 2103: Element rep, use;
1.180 kia 2104:
2105: if (!TtaGetDocumentAccessMode(doc))
1.198 kia 2106: return NULL;
1.180 kia 2107:
2108: if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
2109: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2110: {
2111: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
2112: TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1.185 kia 2113:
1.180 kia 2114: if(selElem && selElem2)
2115: {
2116: selType = TtaGetElementType(selElem);
2117: selType2 = TtaGetElementType(selElem2);
2118:
2119: repType.ElSSchema = sstempl;
2120: repType.ElTypeNum = Template_EL_repeat;
1.186 kia 2121:
1.185 kia 2122: parent = TtaGetParent(selElem);
2123: parent2 = TtaGetParent(selElem2);
2124:
1.197 kia 2125: QueryStringFromUser(label, title, buffer, 127);
2126: if(firstChar==0 && firstChar2==0 && parent == parent2 && buffer[0]!=0)
1.180 kia 2127: {
1.187 kia 2128: dispMode = TtaGetDisplayMode (doc);
2129: if (dispMode == DisplayImmediately)
2130: TtaSetDisplayMode (doc, DeferredDisplay);
2131: oldStructureChecking = TtaGetStructureChecking (doc);
2132: TtaSetStructureChecking (FALSE, doc);
2133:
2134: if(selElem==selElem2 && selType.ElSSchema==sstempl &&
2135: (selType.ElTypeNum==Template_EL_useEl ||
2136: selType.ElTypeNum==Template_EL_useSimple))
2137: use = selElem;
2138: else
1.198 kia 2139: use = Template_CreateUseFromSelection(doc, createComp);
1.187 kia 2140: if(use)
2141: {
2142: TtaExtendUndoSequence(doc);
2143:
2144: rep = TtaNewElement(doc, repType);
1.197 kia 2145:
1.187 kia 2146: TtaInsertSibling(rep, use, FALSE, doc);
1.197 kia 2147: SetAttributeStringValue(rep, Template_ATTR_title, buffer);
1.187 kia 2148: TtaRegisterElementCreate(rep, doc);
1.197 kia 2149:
1.187 kia 2150: TtaRegisterElementDelete (use, doc);
2151: TtaRemoveTree(use, doc);
2152:
2153: TtaInsertFirstChild(&use, rep, doc);
2154: TtaRegisterElementCreate(use, doc);
2155:
2156: TtaSelectElement(doc, use);
2157: TtaCloseUndoSequence(doc);
2158: }
2159:
2160: TtaSetStructureChecking (oldStructureChecking, doc);
2161: TtaSetDisplayMode (doc, dispMode);
1.198 kia 2162: return rep;
1.187 kia 2163: }
2164: }
2165: }
2166: #endif /* TEMPLATES */
1.198 kia 2167: return NULL;
1.195 kia 2168: }
2169:
1.187 kia 2170: /*----------------------------------------------------------------------
2171: TemplateCreateUseBox
2172: Create a xt:use around the selection.
2173: ----------------------------------------------------------------------*/
2174: void TemplateCreateUseBox(Document doc, View view)
2175: {
2176: #ifdef TEMPLATES
1.197 kia 2177: Template_CreateUseFromSelection(doc, FALSE);
1.187 kia 2178: #endif /* TEMPLATES */
2179: }
2180:
1.194 kia 2181: /*----------------------------------------------------------------------
1.195 kia 2182: TemplateCreateUseCompBox
2183: Create a xt:use around the selection.
2184: ----------------------------------------------------------------------*/
2185: void TemplateCreateUseCompBox(Document doc, View view)
2186: {
2187: #ifdef TEMPLATES
1.197 kia 2188: Template_CreateUseFromSelection(doc, TRUE);
1.195 kia 2189: #endif /* TEMPLATES */
2190: }
2191:
2192: /*----------------------------------------------------------------------
1.194 kia 2193: Template_CreateInlineUse
2194: Create an inline xt:use with the selection.
2195: ----------------------------------------------------------------------*/
2196: static ThotBool Template_CreateInlineUse(Document doc)
2197: {
2198: #ifdef TEMPLATES
2199: SSchema sstempl = TtaGetSSchema ("Template", doc);
2200: int firstChar, lastChar;
2201: Element selElem, use;
2202: XTigerTemplate t = GetXTigerDocTemplate(doc);
2203: char *proposed, *label = NULL, *types=NULL;
2204: ThotBool option, res = FALSE;
2205:
2206: if(t && sstempl)
2207: {
2208: proposed = Template_GetInlineLevelDeclarations(t, TRUE, TRUE);
2209: if(QueryNewUseFromUser(proposed, &label, &types, &option))
2210: {
2211: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2212:
2213: GenerateInlineElement(Template_EL_useEl, sstempl, 0, NULL, TRUE);
2214:
2215: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.197 kia 2216:
2217: use = selElem;
2218: TtaNextSibling(&use);
2219:
1.194 kia 2220: SetAttributeStringValue(use, Template_ATTR_title, label);
2221: SetAttributeStringValue(use, Template_ATTR_types, types);
2222: res = TRUE;
2223:
2224: TtaSelectElement(doc, use);
2225: TtaCloseUndoSequence(doc);
2226: }
2227:
2228: TtaFreeMemory(proposed);
2229: TtaFreeMemory(label);
2230: TtaFreeMemory(types);
2231: return res;
2232: }
2233:
2234: #endif /* TEMPLATES */
2235: return FALSE;
2236:
2237: }
2238:
2239: /*----------------------------------------------------------------------
2240: Template_CreateEmptyBlockUse
2241: Create a block xt:use with the selection.
2242: ----------------------------------------------------------------------*/
2243: static ThotBool Template_CreateEmptyBlockUse(Document doc)
2244: {
2245: #ifdef TEMPLATES
2246: SSchema sstempl = TtaGetSSchema ("Template", doc);
2247: ElementType useType;
2248: int firstChar, lastChar;
2249: Element selElem, use;
2250: XTigerTemplate t = GetXTigerDocTemplate(doc);
2251: char *proposed, *label = NULL, *types=NULL;
2252: ThotBool option, res = FALSE;
2253:
2254:
2255: if(t && sstempl)
2256: {
2257: proposed = Template_GetBlockLevelDeclarations(t, TRUE);
2258: if(QueryNewUseFromUser(proposed, &label, &types, &option))
2259: {
2260: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2261:
2262: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
2263: useType.ElSSchema = sstempl;
2264: useType.ElTypeNum = Template_EL_useEl;
2265: use = TtaNewElement(doc, useType);
2266:
2267: TtaInsertSibling(use, selElem, FALSE, doc);
2268: TtaDeleteTree(selElem, doc);
2269:
2270: SetAttributeStringValue(use, Template_ATTR_title, label);
2271: SetAttributeStringValue(use, Template_ATTR_types, types);
2272: TtaRegisterElementCreate(use, doc);
2273: res = TRUE;
2274:
2275: TtaSelectElement(doc, use);
2276: TtaCloseUndoSequence(doc);
2277:
2278: }
2279:
2280: TtaFreeMemory(proposed);
2281: TtaFreeMemory(label);
2282: TtaFreeMemory(types);
2283: return res;
2284: }
2285:
2286: #endif /* TEMPLATES */
2287: return FALSE;
2288: }
1.187 kia 2289:
2290: /*----------------------------------------------------------------------
2291: Template_CreateUseFromSelection
2292: Create a xt:use with the selection.
1.197 kia 2293: If selection is empty, insert an inline xt:use.
2294: If createComp is false, create a xt:use with types at the selected block.
2295: If createComp is true, create a component with the selection.
2296: Return the xt:use element.
1.187 kia 2297: ----------------------------------------------------------------------*/
1.197 kia 2298: Element Template_CreateUseFromSelection(Document doc, ThotBool createComp)
1.187 kia 2299: {
2300: #ifdef TEMPLATES
2301: ThotBool oldStructureChecking;
2302: DisplayMode dispMode;
2303: Element selElem, selElem2, parent, parent2;
2304: ElementType selType, selType2;
2305: int firstChar, lastChar, firstChar2, lastChar2;
1.194 kia 2306: SSchema sstempl = TtaGetSSchema ("Template", doc),
2307: sshtml = TtaGetSSchema ("HTML", doc);
1.187 kia 2308:
2309: ElementType useType;
2310: Element use = NULL, comp, prev;
2311: char buffer[128];
2312: int sz = 128;
2313:
2314: if (!TtaGetDocumentAccessMode(doc))
2315: return NULL;
2316:
2317: if(doc && TtaGetDocumentAccessMode(doc) && sstempl &&
2318: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2319: {
2320: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
2321: TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
2322:
2323: if(selElem && selElem2)
2324: {
2325: selType = TtaGetElementType(selElem);
2326: selType2 = TtaGetElementType(selElem2);
2327: parent = TtaGetParent(selElem);
2328: parent2 = TtaGetParent(selElem2);
2329:
1.194 kia 2330: dispMode = TtaGetDisplayMode (doc);
2331: if (dispMode == DisplayImmediately)
2332: TtaSetDisplayMode (doc, DeferredDisplay);
2333: oldStructureChecking = TtaGetStructureChecking (doc);
2334: TtaSetStructureChecking (FALSE, doc);
2335:
1.187 kia 2336: if(firstChar==0 && firstChar2==0 && parent == parent2)
2337: {
1.194 kia 2338: if(selType.ElSSchema==sshtml && selType.ElTypeNum==HTML_EL_Element)
2339: Template_CreateEmptyBlockUse(doc);
2340: else
1.186 kia 2341: {
1.194 kia 2342: prev = selElem;
2343: TtaPreviousSibling(&prev);
1.197 kia 2344:
2345: if(createComp)
2346: {
2347: // Create a xt:component from selection and create a xt:use using it
2348: comp = Template_CreateComponentFromSelection(doc);
2349: if(comp)
2350: {
2351: TtaExtendUndoSequence(doc);
2352:
2353: useType.ElSSchema = sstempl;
2354: useType.ElTypeNum = Template_EL_useEl;
2355: use = TtaNewElement(doc, useType);
2356: if(prev)
2357: TtaInsertSibling(use, prev, FALSE, doc);
2358: else
2359: TtaInsertFirstChild(&use, parent, doc);
2360:
2361: GiveAttributeStringValueFromNum(comp, Template_ATTR_name, buffer, &sz);
2362: SetAttributeStringValue(use, Template_ATTR_types, buffer);
2363: TtaRegisterElementCreate(use, doc);
2364:
2365: TtaSelectElement(doc, use);
2366: TtaCloseUndoSequence(doc);
2367: }
2368: }
2369: else
1.194 kia 2370: {
1.197 kia 2371: // Create a xt:use around the selection
2372: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.194 kia 2373:
1.197 kia 2374: TtaRegisterElementDelete (selElem, doc);
2375: TtaRemoveTree(selElem, doc);
2376:
1.194 kia 2377: useType.ElSSchema = sstempl;
1.197 kia 2378: useType.ElTypeNum = Template_EL_useSimple;
1.194 kia 2379: use = TtaNewElement(doc, useType);
2380: if(prev)
2381: TtaInsertSibling(use, prev, FALSE, doc);
2382: else
2383: TtaInsertFirstChild(&use, parent, doc);
2384:
1.197 kia 2385: TtaInsertFirstChild(&selElem, use, doc);
2386:
2387: SetAttributeStringValue(use, Template_ATTR_types, TtaGetElementTypeName(TtaGetElementType(selElem)));
2388:
1.194 kia 2389: TtaRegisterElementCreate(use, doc);
2390:
2391: TtaCloseUndoSequence(doc);
2392: }
1.186 kia 2393: }
2394: }
1.194 kia 2395: else if(lastChar<firstChar)
2396: Template_CreateInlineUse(doc);
2397:
2398: TtaSetStructureChecking (oldStructureChecking, doc);
2399: TtaSetDisplayMode (doc, dispMode);
2400:
1.186 kia 2401: }
2402: }
1.187 kia 2403: return use;
2404: #else /* TEMPLATES */
2405: return NULL;
1.186 kia 2406: #endif /* TEMPLATES */
2407: }
2408:
2409: /*----------------------------------------------------------------------
1.187 kia 2410: Template_CreateComponentFromSelection
1.186 kia 2411: Create a xt:component with the selection and move it into the xt:head.
1.187 kia 2412: Return the xt:component element.
1.186 kia 2413: ----------------------------------------------------------------------*/
1.187 kia 2414: Element Template_CreateComponentFromSelection(Document doc)
1.186 kia 2415: {
2416: #ifdef TEMPLATES
1.187 kia 2417: ThotBool oldStructureChecking;
2418: DisplayMode dispMode;
1.186 kia 2419: Element selElem, selElem2, parent, parent2, current, child, head;
2420: ElementType selType, selType2;
2421: int firstChar, lastChar, firstChar2, lastChar2;
2422: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.192 kia 2423: XTigerTemplate t = GetXTigerDocTemplate(doc);
2424:
1.186 kia 2425: ElementType compType;
2426: Element comp = NULL;
2427: char buffer[128];
1.200 kia 2428: int sz = 128;
1.185 kia 2429:
1.186 kia 2430: const char *title = TtaGetMessage (AMAYA, AM_TEMPLATE_NEWCOMP);
1.204 vatton 2431: const char *label = TtaGetMessage (AMAYA, AM_NAME);
1.186 kia 2432:
1.192 kia 2433: if(doc && t && TtaGetDocumentAccessMode(doc) &&
1.186 kia 2434: TtaGetDocumentAccessMode(doc) && sstempl &&
2435: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2436: {
2437: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
2438: TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
2439:
2440: if(selElem && selElem2)
2441: {
2442: selType = TtaGetElementType(selElem);
2443: selType2 = TtaGetElementType(selElem2);
2444:
2445: QueryStringFromUser(label, title, buffer, 127);
1.190 kia 2446: if(buffer[0]!=EOS)
1.186 kia 2447: {
1.190 kia 2448: head = TemplateFindHead(doc);
2449:
2450: compType.ElSSchema = sstempl;
2451: compType.ElTypeNum = Template_EL_component;
2452:
2453: parent = TtaGetParent(selElem);
2454: parent2 = TtaGetParent(selElem2);
1.185 kia 2455:
1.190 kia 2456: if(head && firstChar==0 && firstChar2==0 && parent == parent2)
1.180 kia 2457: {
1.190 kia 2458: dispMode = TtaGetDisplayMode (doc);
2459: if (dispMode == DisplayImmediately)
2460: TtaSetDisplayMode (doc, DeferredDisplay);
2461: oldStructureChecking = TtaGetStructureChecking (doc);
2462: TtaSetStructureChecking (FALSE, doc);
2463:
2464: child = TtaGetLastChild(head);
2465: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2466: comp = TtaNewElement(doc, compType);
1.185 kia 2467: if(child)
1.190 kia 2468: TtaInsertSibling(comp, child, FALSE, doc);
1.185 kia 2469: else
1.190 kia 2470: TtaInsertFirstChild(&comp, head, doc);
2471: TtaRegisterElementCreate(comp, doc);
2472:
2473: TtaNextSibling(&selElem2);
2474: child = NULL;
2475: while(selElem!=selElem2)
2476: {
2477: current = selElem;
2478: TtaNextSibling(&selElem);
2479:
2480: TtaRegisterElementDelete (current, doc);
2481: TtaRemoveTree(current, doc);
2482: if(child)
2483: TtaInsertSibling(current, child, FALSE, doc);
2484: else
2485: TtaInsertFirstChild(¤t, comp, doc);
2486: TtaRegisterElementDelete (current, doc);
2487: child = current;
2488: }
1.200 kia 2489: if(!Template_SetName(doc, comp, buffer, TRUE))
2490: GiveAttributeStringValueFromNum(comp, Template_ATTR_name, buffer, &sz);
1.190 kia 2491:
2492: TtaCloseUndoSequence(doc);
2493: TtaSelectElement(doc, comp);
2494:
1.192 kia 2495: Template_DeclareNewComponent(t, buffer, comp);
2496:
1.190 kia 2497: TtaSetStructureChecking (oldStructureChecking, doc);
2498: TtaSetDisplayMode (doc, dispMode);
1.180 kia 2499: }
2500: }
2501: }
2502: }
1.186 kia 2503: return comp;
1.187 kia 2504: #else /* TEMPLATES */
2505: return NULL;
2506: #endif /* TEMPLATES */
1.180 kia 2507: }
1.187 kia 2508:
1.192 kia 2509: /*----------------------------------------------------------------------
2510: TemplateComponentWillBeDeleted
2511: Processed when a component element will be deleted in a template context.
2512: ----------------------------------------------------------------------*/
2513: ThotBool TemplateComponentWillBeDeleted (NotifyElement *event)
2514: {
2515: #ifdef TEMPLATES
2516: XTigerTemplate t = GetXTigerDocTemplate(event->document);
2517: char* elemName = GetAttributeStringValueFromNum(event->element, Template_ATTR_name, NULL);
2518:
2519: if(Template_IsUsedComponent(t, event->document, elemName))
2520: {
2521: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_TEMPLATE_USEDCOMP_CANTREMOVE);
2522: return TRUE;
2523: }
2524: #endif /* TEMPLATES */
2525: return FALSE;
2526: }
1.187 kia 2527:
1.200 kia 2528: /*----------------------------------------------------------------------
2529: UnionClicked
2530: Called when a xt:union is clicked
2531: ----------------------------------------------------------------------*/
2532: void UnionClicked(NotifyElement* event)
2533: {
2534: if(event->document && event->element)
2535: TtaSelectElement(event->document, event->element);
2536: }
2537:
2538: /*----------------------------------------------------------------------
2539: UnionDoubleClicked
2540: Called when a xt:union is double clicked
2541: ----------------------------------------------------------------------*/
2542: ThotBool UnionDoubleClicked(NotifyElement* event)
2543: {
2544: Template_ModifyUnionElement(event->document, event->element);
2545: TtaSelectElement(event->document, event->element);
2546: return TRUE;
2547: }
2548:
2549: /*----------------------------------------------------------------------
2550: TemplateNameAttributeDeleted
2551: Called when a xt:name will be deleted
2552: ----------------------------------------------------------------------*/
2553: ThotBool TemplateNameAttributeDeleted(NotifyAttribute* event)
2554: {
2555: // Prevent xt:name deletion
2556: return TRUE;
2557: }
2558:
2559: /*----------------------------------------------------------------------
2560: TemplateNameAttributeModified
2561: Called when a xt:name will be modified
2562: ----------------------------------------------------------------------*/
2563: ThotBool TemplateNameAttributeModified(NotifyAttribute* event)
2564: {
2565: // Prevent xt:name modification
2566: return TRUE;
2567: }
2568:
2569: /*----------------------------------------------------------------------
2570: TemplateNameAttributeCreated
2571: Called when a xt:name have been created
2572: ----------------------------------------------------------------------*/
1.203 vatton 2573: void TemplateNameAttributeCreated(NotifyAttribute* event)
1.200 kia 2574: {
1.202 kia 2575: MakeUniqueName(event->element, event->document, TRUE, FALSE);
1.200 kia 2576: }
2577:
2578:
2579: /*----------------------------------------------------------------------
2580: TemplateNameAttrInMenu
2581: Called by Thot when building the Attributes menu for template elements.
2582: ----------------------------------------------------------------------*/
2583: ThotBool TemplateNameAttrInMenu (NotifyAttribute * event)
2584: {
2585: #ifdef TEMPLATES
2586: ElementType type = TtaGetElementType(event->element);
2587: if(type.ElTypeNum==Template_EL_component ||
2588: type.ElTypeNum==Template_EL_union)
2589: return TRUE;
2590: #endif /* TEMPLATES */
2591: return FALSE;
2592: }
2593:
Webmaster