Annotation of Amaya/amaya/templates.c, revision 1.243
1.1 cvs 1: /*
2: *
1.228 vatton 3: * COPYRIGHT INRIA and W3C, 1996-2009
1.1 cvs 4: * Please first read the full copyright statement in file COPYRIGHT.
5: *
6: */
1.206 kia 7:
1.1 cvs 8: /*
1.51 francesc 9: * Authors: Francesc Campoy Flores
1.210 vatton 10: * Emilien 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.*/
1.233 vatton 45: Prop_Templates_Path *TemplateRepositoryPaths = NULL;
1.209 vatton 46: // register the parent repeat of the new created use
1.233 vatton 47: static Element Creating_repeat = NULL;
1.209 vatton 48:
1.83 kia 49:
1.87 kia 50: /*----------------------------------------------------------------------
51: IsTemplateInstanceDocument: Test if a document is a template instance
52: doc : Document to test
53: return : TRUE if the document is a template instance
54: ----------------------------------------------------------------------*/
1.106 vatton 55: ThotBool IsTemplateInstanceDocument(Document doc)
56: {
1.87 kia 57: #ifdef TEMPLATES
1.213 vatton 58: // check first indicators
59: if (DocumentMeta[doc])
60: {
61: if (DocumentMeta[doc]->method == CE_INSTANCE)
62: return TRUE;
63: else if (DocumentMeta[doc]->method == CE_TEMPLATE)
64: return FALSE;
65: }
1.171 kia 66: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.209 vatton 67: if (t)
1.210 vatton 68: return ((t->state & templInstance) != 0);
1.171 kia 69: else
70: return FALSE;
71: #else /* TEMPLATES */
1.88 cvs 72: return FALSE;
1.206 kia 73: #endif /* TEMPLATES */
1.87 kia 74: }
75:
1.83 kia 76: /*----------------------------------------------------------------------
1.109 kia 77: IsTemplateDocument: Test if a document is a template (not an instance)
78: doc : Document to test
1.167 kia 79: return : TRUE if the document is a template
1.109 kia 80: ----------------------------------------------------------------------*/
1.183 vatton 81: ThotBool IsTemplateDocument (Document doc)
1.109 kia 82: {
83: #ifdef TEMPLATES
1.171 kia 84: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.232 vatton 85: if (DocumentMeta[doc])
86: {
87: if (DocumentMeta[doc]->method == CE_INSTANCE)
88: return FALSE;
89: else if (DocumentMeta[doc]->method == CE_TEMPLATE)
90: return TRUE;
91: }
1.183 vatton 92: if (t)
1.232 vatton 93: return ((t->state & templInstance) == 0);
1.171 kia 94: else
95: return FALSE;
1.206 kia 96: #endif /* TEMPLATES */
1.171 kia 97: return FALSE;
98: }
99:
100:
101: /*----------------------------------------------------------------------
1.210 vatton 102: GetUsedTypeName returns the name of the current used type or the first
103: name of the types attribute
104: The returned string must be freed
105: ----------------------------------------------------------------------*/
106: char *GetUsedTypeName (Element el)
107: {
108: char *name = NULL;
109: #ifdef TEMPLATES
110: char *ptr;
111:
112: if (IsTemplateElement (el))
113: {
114: name = GetAttributeStringValueFromNum (el, Template_ATTR_currentType, NULL);
115: if (name == NULL)
116: {
117: // use the first type
118: name = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
119: if (name)
120: {
121: ptr = strstr (name, " ");
122: if (ptr)
123: *ptr = EOS;
124: }
125: return name;
126: }
127: }
128: #endif /* TEMPLATES */
129: return name;
130: }
131:
132: /*----------------------------------------------------------------------
1.214 vatton 133: IsInLineTemplateElement returns TRUE if the template element can be
134: inserted into a paragraph
135: ----------------------------------------------------------------------*/
136: ThotBool IsInLineTemplateElement (Element el, Document doc)
137: {
138: #ifdef TEMPLATES
139: XTigerTemplate t;
140: ElementType elType;
141: Declaration dec;
142: char *name = NULL;
143:
144: elType = TtaGetElementType (el);
145: if (elType.ElTypeNum == Template_EL_useEl ||
146: elType.ElTypeNum == Template_EL_useSimple)
147: {
148: t = GetXTigerDocTemplate(doc);
149: if (t)
150: {
151: name = GetUsedTypeName (el);
152: dec = Template_GetDeclaration (t, name);
153: TtaFreeMemory (name);
154: if (dec)
155: return !dec->blockLevel;
156: else
157: return FALSE;
158: }
159: }
160: #endif /* TEMPLATES */
161: return FALSE;
162: }
163:
164: /*----------------------------------------------------------------------
1.231 vatton 165: ----------------------------------------------------------------------*/
166: Element GetParentLine (Element el, SSchema templateSSchema)
167: {
168: Element parent = TtaGetParent(el);
169: #ifdef TEMPLATES
170: ElementType parentType;
171: char *name;
172:
173: // look for the enclosing parent line element
174: parentType = TtaGetElementType(parent);
175: while (parent && parentType.ElSSchema == templateSSchema)
176: {
177: parent = TtaGetParent(parent);
178: parentType = TtaGetElementType(parent);
179: }
180: if (parent && parentType.ElSSchema)
181: {
182: name = TtaGetSSchemaName (parentType.ElSSchema);
183: if (name == NULL || strcmp (name, "HTML") ||
184: (parentType.ElTypeNum != HTML_EL_Pseudo_paragraph &&
185: parentType.ElTypeNum != HTML_EL_Paragraph))
186: parent = NULL;
187: }
188: #endif /* TEMPLATES */
189: return parent;
190: }
191:
192: /*----------------------------------------------------------------------
1.171 kia 193: Test if a document is an internal template.
194: (no instance is opened and it is not edited)
195: ----------------------------------------------------------------------*/
196: ThotBool IsInternalTemplateDocument(Document doc)
197: {
198: #ifdef TEMPLATES
199: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.209 vatton 200: if (t)
1.214 vatton 201: return (t->state & templInternal) != 0;
1.171 kia 202: else
203: return FALSE;
204: #else /* TEMPLATES */
1.109 kia 205: return FALSE;
1.206 kia 206: #endif /* TEMPLATES */
1.171 kia 207: }
208:
209: /*----------------------------------------------------------------------
210: Return the URL of an instance template.
211: ----------------------------------------------------------------------*/
212: char* GetDocumentInstanceTemplateUrl(Document doc)
213: {
214: #ifdef TEMPLATES
215: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.209 vatton 216: if (t)
1.171 kia 217: return t->base_uri;
218: else
219: return FALSE;
220: #else /* TEMPLATES */
221: return NULL;
1.206 kia 222: #endif /* TEMPLATES */
1.109 kia 223: }
224:
1.146 vatton 225: /*----------------------------------------------------------------------
226: CheckPromptIndicator checks if the element is a prompt text unit
227: ----------------------------------------------------------------------*/
228: ThotBool CheckPromptIndicator (Element el, Document doc)
229: {
230: #ifdef TEMPLATES
231: ElementType elType;
232: Element parent;
233: AttributeType attrType;
234: Attribute att;
235: SSchema templateSSchema;
236:
1.173 vatton 237: if (!IsTemplateInstanceDocument(doc))
238: /* let Thot perform normal operation */
239: return FALSE;
1.146 vatton 240: elType = TtaGetElementType (el);
241: templateSSchema = TtaGetSSchema ("Template", doc);
242: if (elType.ElTypeNum == HTML_EL_TEXT_UNIT)
243: {
244: parent = TtaGetParent (el);
245: elType = TtaGetElementType (parent);
246: while (parent && elType.ElSSchema != templateSSchema)
247: {
248: parent = TtaGetParent (parent);
249: elType = TtaGetElementType (parent);
250: }
251: if (parent &&
252: (elType.ElTypeNum == Template_EL_useEl ||
253: elType.ElTypeNum == Template_EL_useSimple))
254: {
255: // there is a parent template use
256: attrType.AttrSSchema = elType.ElSSchema;
257: attrType.AttrTypeNum = Template_ATTR_prompt;
258: att = TtaGetAttribute (parent, attrType);
259: if (att)
1.151 quint 260: {
261: TtaSelectElement (doc, el);
262: return TRUE;
263: }
1.146 vatton 264: }
265: }
266: #endif /* TEMPLATES */
1.173 vatton 267: /* let Thot perform normal operation */
1.146 vatton 268: return FALSE;
269: }
270:
271: /*----------------------------------------------------------------------
272: RemovePromptIndicator removes the enclosing prompt indicator
273: ----------------------------------------------------------------------*/
1.153 vatton 274: ThotBool RemovePromptIndicator (NotifyOnTarget *event)
1.146 vatton 275: {
276: #ifdef TEMPLATES
277: ElementType elType;
1.153 vatton 278: Element parent, el;
1.146 vatton 279: AttributeType attrType;
280: Attribute att;
1.153 vatton 281: Document doc;
1.146 vatton 282: SSchema templateSSchema;
283:
1.153 vatton 284: el = event->element;
285: doc = event->document;
1.146 vatton 286: elType = TtaGetElementType (el);
287: templateSSchema = TtaGetSSchema ("Template", doc);
1.153 vatton 288: parent = TtaGetParent (el);
289: elType = TtaGetElementType (parent);
290: while (parent && elType.ElSSchema != templateSSchema)
1.146 vatton 291: {
1.153 vatton 292: parent = TtaGetParent (parent);
1.146 vatton 293: elType = TtaGetElementType (parent);
1.153 vatton 294: }
295: if (parent &&
296: (elType.ElTypeNum == Template_EL_useEl ||
297: elType.ElTypeNum == Template_EL_useSimple))
298: {
299: // there is a parent template use
300: attrType.AttrSSchema = elType.ElSSchema;
301: attrType.AttrTypeNum = Template_ATTR_prompt;
302: att = TtaGetAttribute (parent, attrType);
303: if (att)
1.146 vatton 304: {
1.153 vatton 305: TtaRegisterAttributeDelete (att, parent, doc);
306: TtaRemoveAttribute (parent, att, doc);
1.146 vatton 307: }
308: }
309: #endif /* TEMPLATES */
1.153 vatton 310: return FALSE; /* let Thot perform normal operation */
1.146 vatton 311: }
312:
1.109 kia 313:
314: /*----------------------------------------------------------------------
1.108 vatton 315: AllocTemplateRepositoryListElement: allocates an element for the list
316: of template repositories.
1.83 kia 317: path : path of the new element
318: return : address of the new element
319: ----------------------------------------------------------------------*/
320: void* AllocTemplateRepositoryListElement (const char* path, void* prevElement)
321: {
1.129 vatton 322: Prop_Templates_Path *element;
323:
324: element = (Prop_Templates_Path*)TtaGetMemory (sizeof(Prop_Templates_Path));
325: memset (element, 0, sizeof(Prop_Templates_Path));
1.233 vatton 326: element->Path = TtaStrdup (path);
1.83 kia 327: if (prevElement)
1.129 vatton 328: {
329: element->NextPath = ((Prop_Templates_Path*)prevElement)->NextPath;
330: ((Prop_Templates_Path*)prevElement)->NextPath = element;
331: }
1.83 kia 332: return element;
333: }
334:
335:
336: /*----------------------------------------------------------------------
337: FreeTemplateRepositoryList: Free the list of template repositories.
338: ----------------------------------------------------------------------*/
1.233 vatton 339: void FreeTemplateRepositoryList ()
1.83 kia 340: {
1.233 vatton 341: Prop_Templates_Path *element = TemplateRepositoryPaths;
1.131 vatton 342:
1.83 kia 343: while (element)
1.209 vatton 344: {
345: Prop_Templates_Path* next = element->NextPath;
1.233 vatton 346: TtaFreeMemory (element->Path);
1.209 vatton 347: TtaFreeMemory (element);
348: element = next;
349: }
1.233 vatton 350: TemplateRepositoryPaths = NULL;
1.83 kia 351: }
352:
353: /*----------------------------------------------------------------------
1.233 vatton 354: SaveTemplateRepositoryList: Save the list of template repositories.
1.83 kia 355: ----------------------------------------------------------------------*/
1.233 vatton 356: void SaveTemplateRepositoryList ()
1.83 kia 357: {
1.233 vatton 358: const Prop_Templates_Path *element;
359: char *path, *homePath;
360: unsigned char *c;
361: FILE *file;
1.206 kia 362:
1.233 vatton 363: path = (char *) TtaGetMemory (MAX_LENGTH);
364: homePath = TtaGetEnvString ("APP_HOME");
365: sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.209 vatton 366:
1.233 vatton 367: file = TtaWriteOpen ((char *)path);
368: c = (unsigned char*)path;
369: *c = EOS;
370: if (file)
1.106 vatton 371: {
1.233 vatton 372: element = TemplateRepositoryPaths;
373: while (element)
374: {
375: fprintf(file, "%s\n", element->Path);
376: element = element->NextPath;
377: }
378: TtaWriteClose (file);
1.106 vatton 379: }
1.83 kia 380: }
381:
382: /*----------------------------------------------------------------------
383: LoadTemplateRepositoryList: Load the list of template repositories.
384: return : the number of readed repository paths.
385: ----------------------------------------------------------------------*/
1.233 vatton 386: static int LoadTemplateRepositoryList ()
1.83 kia 387: {
388: Prop_Templates_Path *element, *current = NULL;
1.233 vatton 389: char *path, *homePath;
390: unsigned char *c;
391: int nb = 0;
392: FILE *file;
1.131 vatton 393:
394: // open the file
1.83 kia 395: path = (char *) TtaGetMemory (MAX_LENGTH);
1.86 vatton 396: homePath = TtaGetEnvString ("APP_HOME");
1.106 vatton 397: sprintf (path, "%s%ctemplates.dat", homePath, DIR_SEP);
1.83 kia 398: file = TtaReadOpen ((char *)path);
1.84 kia 399: if (!file)
1.209 vatton 400: {
401: /* The config file dont exist, create it. */
402: file = TtaWriteOpen ((char *)path);
403: fprintf (file, "http://www.w3.org/Amaya/Templates/cv.xtd\n");
404: fprintf (file, "http://www.w3.org/Amaya/Templates/slides.xtd\n");
405: fprintf (file, "http://www.w3.org/Amaya/Templates/ACM-Proc-Article.xtd\n");
406: TtaWriteClose (file);
407: /* Retry to open it.*/
408: file = TtaReadOpen ((char *)path);
409: }
1.233 vatton 410: TemplateRepositoryPaths = NULL;
1.83 kia 411: if (file)
1.129 vatton 412: {
1.131 vatton 413: // read the file
1.129 vatton 414: c = (unsigned char*)path;
415: *c = EOS;
416: while (TtaReadByte (file, c))
417: {
418: if (*c == 13 || *c == EOL)
419: *c = EOS;
420: if (*c == EOS && c != (unsigned char*)path )
421: {
422: element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
423: element->NextPath = NULL;
1.233 vatton 424: element->Path = TtaStrdup (path);
425: if (TemplateRepositoryPaths == NULL)
426: TemplateRepositoryPaths = element;
1.129 vatton 427: else
428: current->NextPath = element;
429: current = element;
430: nb++;
1.83 kia 431:
1.129 vatton 432: c = (unsigned char*) path;
433: *c = EOS;
434: }
435: else
436: c++;
437: }
438: if (c != (unsigned char*)path && *path != EOS)
439: {
440: element = (Prop_Templates_Path*) TtaGetMemory (sizeof(Prop_Templates_Path));
441: *(c+1) = EOS;
1.233 vatton 442: element->Path = TtaStrdup (path);
1.129 vatton 443: element->NextPath = NULL;
1.148 kia 444:
1.233 vatton 445: if (TemplateRepositoryPaths == NULL)
446: TemplateRepositoryPaths = element;
1.129 vatton 447: else
448: current->NextPath = element;
449: nb++;
450: }
451: TtaReadClose (file);
1.83 kia 452: }
453: TtaFreeMemory(path);
454: return nb;
455: }
456:
457: /*-----------------------------------------------------------------------
1.209 vatton 458: InitTemplates
459: Initializes the annotation library
1.83 kia 460: -----------------------------------------------------------------------*/
461: void InitTemplates ()
462: {
463: TtaSetEnvBoolean ("SHOW_TEMPLATES", TRUE, FALSE);
1.233 vatton 464: LoadTemplateRepositoryList ();
1.83 kia 465: }
466:
467:
1.1 cvs 468: /*----------------------------------------------------------------------
1.206 kia 469: Load a template and create the instance file - update images and
1.108 vatton 470: stylesheets related to the template.
471: ----------------------------------------------------------------------*/
472: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname)
473: {
474: #ifdef TEMPLATES
1.170 vatton 475: DocumentType docType;
476: int len, i;
477: char *s;
478: char suffix[6];
479: ThotBool dontReplace = DontReplaceOldDoc;
1.108 vatton 480:
481: if (!IsW3Path (docname) && TtaFileExist (docname))
482: {
483: s = (char *)TtaGetMemory (strlen (docname) +
484: strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2);
485: sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname);
486: InitConfirm (0, 0, s);
487: TtaFreeMemory (s);
488: if (!UserAnswer)
489: return;
490: }
1.170 vatton 491: docType = LoadTemplate (0, templatename);
492: if (docType != docFree)
1.165 kia 493: {
1.170 vatton 494: /* check if the file suffix is conform to the document type */
495: s = (char *)TtaGetMemory (strlen (docname) + 10);
496: strcpy (s, docname);
497: if (!IsXMLName (docname))
498: {
499: // by default no suffix is added
500: suffix[0] = EOS;
501: if (IsMathMLName (docname) && docType != docMath)
502: strcpy (suffix, "mml");
503: else if (IsSVGName (docname) && docType != docSVG)
504: strcpy (suffix, "svg");
505: else if (IsHTMLName (docname) && docType != docHTML)
506: strcpy (suffix, "xml");
507: if (suffix[0] != EOS)
508: {
509: // change or update the suffix
510: len = strlen (s);
511: for (i = len-1; i > 0 && s[i] != '.'; i--);
512: if (s[i] != '.')
513: {
514: /* there is no suffix */
515: s[i++] = '.';
516: strcpy (&s[i], suffix);
517: }
518: else
519: {
520: /* there is a suffix */
521: i++;
522: strcpy (&s[i], suffix);
523: }
524: }
525: }
526: // now create the instance
1.165 kia 527: DontReplaceOldDoc = dontReplace;
1.177 vatton 528: CreateInstance (templatename, s, docname, docType, doc);
1.170 vatton 529: TtaFreeMemory (s);
1.165 kia 530: }
1.108 vatton 531: #endif /* TEMPLATES */
532: }
533:
1.53 vatton 534:
1.109 kia 535:
1.52 vatton 536: /*----------------------------------------------------------------------
1.107 kia 537: PreventReloadingTemplate
538: Prevent reloading a template.
539: You must call AllowReloadingTemplate when finish.
540: Usefull for reload an instance without reloading the template.
541: ----------------------------------------------------------------------*/
542: void PreventReloadingTemplate(char* template_url)
543: {
544: #ifdef TEMPLATES
1.178 kia 545: Template_AddReference(GetXTigerTemplate (template_url));
1.107 kia 546: #endif /* TEMPLATES */
547: }
548:
549: /*----------------------------------------------------------------------
550: AllowReloadingTemplate
551: Allow reloading a template.
552: You must call it after each PreventReloadingTemplate call.
553: ----------------------------------------------------------------------*/
554: void AllowReloadingTemplate(char* template_url)
555: {
556: #ifdef TEMPLATES
1.178 kia 557: Template_RemoveReference(GetXTigerTemplate (template_url));
1.206 kia 558: #endif /* TEMPLATES */
1.107 kia 559: }
560:
561:
1.137 vatton 562: /*----------------------------------------------------------------------
563: ----------------------------------------------------------------------*/
1.134 kia 564: ThotBool isEOSorWhiteSpace (const char c)
565: {
1.137 vatton 566: return c == SPACE || c == TAB || c == EOL || c ==__CR__ || c == EOS;
567: }
568: ThotBool isWhiteSpace (const char c)
569: {
570: return c == SPACE || c == TAB || c == EOL || c ==__CR__;
1.134 kia 571: }
572:
1.107 kia 573: /*----------------------------------------------------------------------
1.87 kia 574: giveItems : Lists type items from string
575: example : "one two three" is extracted to {one, two, three}
576: note : item type are setted to SimpleTypeNat
577: text : text from which list items
578: size : size of text in characters
579: items : address of exctracted item list
580: nbitems : items number in items list
1.52 vatton 581: ----------------------------------------------------------------------*/
1.76 vatton 582: void giveItems (char *text, int size, struct menuType **items, int *nbitems)
1.1 cvs 583: {
1.70 quint 584: #ifdef TEMPLATES
1.148 kia 585: ThotBool inElement = TRUE;
1.52 vatton 586: struct menuType *menu;
587: char *iter;
1.148 kia 588: char temp[128];
1.52 vatton 589: int i;
1.148 kia 590: int labelSize;
1.28 tollenae 591:
1.148 kia 592: *nbitems = 1;
593: for (i = 0; i < size; i++)
1.52 vatton 594: {
595: if (isEOSorWhiteSpace (text[i]))
596: {
597: if (inElement)
598: inElement = FALSE;
599: }
600: else if (!inElement)
601: {
602: inElement = TRUE;
603: (*nbitems)++;
604: }
605: }
1.51 francesc 606:
1.148 kia 607: menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems);
608: iter = text;
609: for (i = 0; i < *nbitems; i++)
610: {
1.52 vatton 611: labelSize = 0;
1.137 vatton 612: while (isWhiteSpace (*iter))
1.52 vatton 613: iter++;
1.137 vatton 614: if (*iter != EOS)
615: {
616: while (!isEOSorWhiteSpace (*iter))
617: {
618: temp[labelSize++] = *iter;
619: iter++;
620: }
1.52 vatton 621:
1.137 vatton 622: temp[labelSize] = EOS;
623: menu[i].label = (char *) TtaStrdup (temp);
624: menu[i].type = SimpleTypeNat; /* @@@@@ ???? @@@@@ */
1.52 vatton 625: }
626: }
1.137 vatton 627: *items = menu;
1.70 quint 628: #endif /* TEMPLATES */
1.28 tollenae 629: }
1.37 tollenae 630:
1.70 quint 631: #ifdef TEMPLATES
1.52 vatton 632: /*----------------------------------------------------------------------
1.235 vatton 633: The parameter option adds an empty string
1.52 vatton 634: ----------------------------------------------------------------------*/
1.235 vatton 635: static char *createMenuString (const struct menuType* items, const int nbItems,
636: ThotBool option)
1.52 vatton 637: {
638: char *result, *iter;
1.148 kia 639: int size = 0;
1.52 vatton 640: int i;
641:
1.235 vatton 642: if (option)
643: size += 3;
644: for (i = 0; i < nbItems; i++)
1.148 kia 645: size += 2 + strlen (items[i].label);
1.52 vatton 646:
1.148 kia 647: result = (char *) TtaGetMemory (size);
648: iter = result;
1.235 vatton 649: if (option)
650: {
651: strcpy (iter, "B ");
652: iter += 3;
653: }
654: for (i = 0; i < nbItems; i++)
1.52 vatton 655: {
656: *iter = 'B';
657: ++iter;
658: strcpy (iter, items[i].label);
659: iter += strlen (items[i].label)+1;
660: }
1.148 kia 661: return result;
1.36 tollenae 662: }
1.71 quint 663: #endif /* TEMPLATES */
1.29 tollenae 664:
1.185 kia 665:
666: /*----------------------------------------------------------------------
1.71 quint 667: UseToBeCreated
668: An new use element will be created by the user through some generic editing
669: command
670: -----------------------------------------------------------------------*/
671: ThotBool UseToBeCreated (NotifyElement *event)
672: {
673: #ifdef TEMPLATES
1.122 kia 674: ElementType parentType;
1.206 kia 675: SSchema templateSSchema = TtaGetSSchema ("Template", event->document);
1.130 vatton 676: if (templateSSchema)
1.209 vatton 677: {
678: parentType = TtaGetElementType (event->element);
679: if (parentType.ElSSchema == templateSSchema &&
680: parentType.ElTypeNum == Template_EL_repeat)
681: {
682: if (Template_CanInsertRepeatChild (event->element))
683: return TemplateElementWillBeCreated (event);
684: else
685: return TRUE; //don't let Thot do the job
686: }
687: else
688: return TemplateElementWillBeCreated (event);
689: }
1.52 vatton 690: #endif /* TEMPLATES */
1.71 quint 691: return FALSE; /* let Thot perform normal operation */
692: }
693:
694: /*----------------------------------------------------------------------
695: UseCreated
696: A new "use" element has just been created by the user with a generic editing
697: command.
698: -----------------------------------------------------------------------*/
699: void UseCreated (NotifyElement *event)
700: {
701: #ifdef TEMPLATES
1.148 kia 702: Document doc = event->document;
703: Element el = event->element;
1.117 kia 704: Element parent;
705: Element first;
706: ElementType parentType;
707: XTigerTemplate t;
708: SSchema templateSSchema;
1.130 vatton 709: char* types, *text = NULL;
1.148 kia 710:
1.112 vatton 711: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 712: return;
713:
1.72 quint 714: if (TtaGetFirstChild (el))
715: /* this Use element has already some content. It has already been
716: instanciated */
717: return;
1.117 kia 718:
1.171 kia 719: t = GetXTigerDocTemplate (doc);
1.71 quint 720: if (!t)
721: return; // no template ?!?!
1.101 kia 722:
1.138 vatton 723: templateSSchema = TtaGetSSchema ("Template", doc);
1.117 kia 724: parent = TtaGetParent(el);
725: parentType = TtaGetElementType(parent);
1.148 kia 726:
1.130 vatton 727: if (parentType.ElSSchema == templateSSchema &&
728: parentType.ElTypeNum == Template_EL_repeat)
1.117 kia 729: {
1.209 vatton 730: first = TtaGetFirstChild (parent);
731: if (first == el)
732: TtaNextSibling (&first);
733: if (first)
734: {
735: types = GetAttributeStringValueFromNum (first, Template_ATTR_types, NULL);
736: if (types)
737: {
738: SetAttributeStringValueWithUndo (el, Template_ATTR_types, types);
739: text = GetAttributeStringValueFromNum (el, Template_ATTR_title, NULL);
740: SetAttributeStringValueWithUndo (first, Template_ATTR_title, text);
741: TtaFreeMemory (text);
742: text = GetAttributeStringValueFromNum (el, Template_ATTR_currentType, NULL);
743: SetAttributeStringValueWithUndo (first, Template_ATTR_currentType, text);
744: TtaFreeMemory (text);
745: TtaFreeMemory (types);
746: }
747: }
1.117 kia 748: }
1.231 vatton 749: // look for the enclosing target element
750: parent = GetParentLine (parent, templateSSchema);
751: InstantiateUse (t, el, doc, parent, TRUE);
1.71 quint 752: #endif /* TEMPLATES */
753: }
1.29 tollenae 754:
1.89 kia 755:
1.98 kia 756: /*----------------------------------------------------------------------
757: Template_CanInsertRepeatChild
758: Test if a xt:repeat child can be inserted (number between params min and max).
759: @param el element (xt:repeat) to test
760: @return True if an element can be inserted.
761: ----------------------------------------------------------------------*/
762: ThotBool Template_CanInsertRepeatChild(Element el)
763: {
764: #ifdef TEMPLATES
1.154 vatton 765: char *max;
766: int maxVal, curVal;
767: Element child;
1.148 kia 768:
1.104 kia 769: max = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
1.191 kia 770: if (max && max[0]!=EOS)
1.104 kia 771: {
1.154 vatton 772: if (!strcmp(max, "*"))
773: {
774: TtaFreeMemory(max);
775: return TRUE;
776: }
777: maxVal = atoi (max);
778: TtaFreeMemory (max);
1.104 kia 779: curVal = 0;
1.105 vatton 780: for (child = TtaGetFirstChild(el); child; TtaNextSibling(&child))
1.104 kia 781: curVal++;
1.154 vatton 782: return (curVal < maxVal);
1.104 kia 783: }
784: else
1.98 kia 785: return TRUE;
786: #endif /* TEMPLATES */
787: return FALSE;
788: }
1.96 kia 789:
1.89 kia 790:
1.92 kia 791: #ifdef TEMPLATES
1.99 kia 792: /*----------------------------------------------------------------------
793: QueryStringFromMenu
794: Show a context menu to query a choice.
1.235 vatton 795: The parameter items is a space-separated choice list string.
796: The parameter option adds an empty string
797: Return The choosed item string or NULL if none.
1.99 kia 798: ----------------------------------------------------------------------*/
1.235 vatton 799: static char *QueryStringFromMenu (Document doc, char* items, ThotBool option)
1.90 kia 800: {
1.235 vatton 801: int nbitems, size;
1.90 kia 802: struct menuType *itemlist;
1.235 vatton 803: char *menuString;
804: char *result = NULL;
1.148 kia 805:
1.138 vatton 806: if (!TtaGetDocumentAccessMode (doc))
1.112 vatton 807: return NULL;
808: if (items == NULL)
1.110 kia 809: return NULL;
1.138 vatton 810: size = strlen (items);
811: if (size == 0)
812: return NULL;
1.90 kia 813: giveItems (items, size, &itemlist, &nbitems);
1.235 vatton 814: menuString = createMenuString (itemlist, nbitems, option);
1.206 kia 815: TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL,
1.90 kia 816: nbitems, menuString , NULL, false, 'L');
817: TtaFreeMemory (menuString);
818: ReturnOption = -1;
1.229 carcone 819: TtaShowDialogue (BaseDialog + OptionMenu, FALSE, TRUE);
1.90 kia 820: TtaWaitShowProcDialogue ();
821: TtaDestroyDialogue (BaseDialog + OptionMenu);
1.148 kia 822:
1.220 vatton 823: if (ReturnOption != -1)
1.235 vatton 824: {
825: if (option)
826: {
827: if (ReturnOption == 0)
828: result = TtaStrdup(" ");
829: else
830: result = TtaStrdup(itemlist[ReturnOption-1].label);
831: }
832: else
833: result = TtaStrdup(itemlist[ReturnOption].label);
834: }
1.206 kia 835:
1.90 kia 836: TtaFreeMemory (itemlist);
837: return result;
838: }
1.92 kia 839: #endif /* TEMPLATES */
1.90 kia 840:
1.223 vatton 841: #ifdef TEMPLATE_DEBUG
1.158 kia 842: void FillInsertableElemList (Document doc, Element elem, DLList list);
1.223 vatton 843: #endif /* TEMPLATE_DEBUG */
1.162 vatton 844: /*----------------------------------------------------------------------
845: ----------------------------------------------------------------------*/
846: char *Template_GetListTypes (XTigerTemplate t, Element el)
847: {
848: #ifdef TEMPLATES
849: char *listtypes = NULL, *types;
850:
851: types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
852: if (types)
1.220 vatton 853: {
854: listtypes = Template_ExpandTypes (t, types, el, FALSE);
855: TtaFreeMemory (types);
856: }
1.162 vatton 857: return listtypes;
858: #endif /* TEMPLATES */
859: }
1.158 kia 860:
1.56 francesc 861: /*----------------------------------------------------------------------
1.138 vatton 862: BagButtonClicked
863: Called when a bag button is clicked.
864: Can be called for useEl, useSimple or bag.
865: If called for useEl or useSimple, the new element must be added after.
866: If called for bag, the element must be added before all.
1.206 kia 867:
1.138 vatton 868: Shows a menu with all the types that can be used in the bag.
869: ----------------------------------------------------------------------*/
870: ThotBool BagButtonClicked (NotifyElement *event)
871: {
872: #ifdef TEMPLATES
873: Document doc = event->document;
874: Element el = event->element;
875: ElementType elType;
876: XTigerTemplate t;
877: Declaration decl;
878: Element bagEl = el;
879: Element firstEl;
880: Element newEl = NULL;
881: SSchema templateSSchema;
1.162 vatton 882: char *listtypes = NULL;
883: char *result = NULL;
1.138 vatton 884: ThotBool oldStructureChecking;
885: DisplayMode dispMode;
886:
1.182 vatton 887: if (!TtaGetDocumentAccessMode (doc))
1.138 vatton 888: return TRUE;
1.182 vatton 889: if (!IsTemplateInstanceDocument (doc))
890: return FALSE; /* let Thot perform normal operation */
1.148 kia 891:
1.238 vatton 892: #ifdef IV
893: View view;
1.138 vatton 894: TtaGetActiveView (&doc, &view);
1.238 vatton 895: if (view == 1)
1.138 vatton 896: return FALSE; /* let Thot perform normal operation */
1.238 vatton 897: #endif
1.138 vatton 898: TtaCancelSelection (doc);
1.206 kia 899: templateSSchema = TtaGetSSchema ("Template", doc);
1.171 kia 900: t = GetXTigerDocTemplate(doc);
1.138 vatton 901: elType = TtaGetElementType (el);
902: while (bagEl &&
903: (elType.ElSSchema != templateSSchema ||
904: elType.ElTypeNum != Template_EL_bag))
905: {
906: bagEl = TtaGetParent (bagEl);
907: elType = TtaGetElementType (bagEl);
908: }
909:
910: if (bagEl)
1.209 vatton 911: {
912: listtypes = Template_GetListTypes (t, bagEl);
913: if (listtypes)
914: {
1.235 vatton 915: result = QueryStringFromMenu (doc, listtypes, FALSE);
1.209 vatton 916: TtaFreeMemory (listtypes);
917: if (result)
918: {
919: decl = Template_GetDeclaration (t, result);
920: if (decl)
921: {
922: dispMode = TtaGetDisplayMode (doc);
923: if (dispMode == DisplayImmediately)
924: /* don't set NoComputedDisplay
925: -> it breaks down views formatting when Enter generates new elements */
926: TtaSetDisplayMode (doc, DeferredDisplay);
927:
928: /* Prepare insertion.*/
929: oldStructureChecking = TtaGetStructureChecking (doc);
930: TtaSetStructureChecking (FALSE, doc);
931:
932: /* Insert */
1.226 vatton 933: newEl = Template_InsertBagChild (doc, el, bagEl, decl, FALSE);
1.209 vatton 934:
935: /* Finish insertion.*/
936: TtaSetDocumentModified (doc);
937: TtaSetStructureChecking (oldStructureChecking, doc);
938: // restore the display
939: TtaSetDisplayMode (doc, dispMode);
940: firstEl = GetFirstEditableElement (newEl);
941: if (firstEl)
942: {
943: TtaSelectElement (doc, firstEl);
1.238 vatton 944: TtaSetStatusSelectedElement (doc, 1, firstEl);
1.209 vatton 945: }
946: else
947: {
948: TtaSelectElement (doc, newEl);
1.238 vatton 949: TtaSetStatusSelectedElement (doc, 1, newEl);
1.209 vatton 950: }
951: }
952: }
953: }
954: TtaFreeMemory (result);
955: }
1.138 vatton 956: #endif /* TEMPLATES */
957: return TRUE; /* don't let Thot perform normal operation */
958: }
959:
960: /*----------------------------------------------------------------------
1.209 vatton 961: DoReplicateUseElement insert a new element after the el child of
962: repeatEl or as the first child of repeatEl.
963: ----------------------------------------------------------------------*/
964: void DoReplicateUseElement (XTigerTemplate t, Document doc, int view,
965: Element el, Element repeatEl, char *name)
966: {
967: Declaration decl;
1.231 vatton 968: Element newEl, firstEl, prevRepeat, parentLine;
969: ElementType elType;
970: DisplayMode dispMode;
1.209 vatton 971: ThotBool oldStructureChecking;
972:
973: if (repeatEl == Creating_repeat)
974: return;
975: prevRepeat = Creating_repeat;
976: Creating_repeat = repeatEl;
977:
978: decl = Template_GetDeclaration (t, name);
1.231 vatton 979: elType = TtaGetElementType (el);
1.209 vatton 980: if (decl)
981: {
982: dispMode = TtaGetDisplayMode (doc);
983: if (dispMode == DisplayImmediately)
984: /* don't set NoComputedDisplay
985: -> it breaks down views formatting when Enter generates new elements */
986: TtaSetDisplayMode (doc, DeferredDisplay);
987: /* Prepare insertion.*/
988: oldStructureChecking = TtaGetStructureChecking (doc);
989: TtaSetStructureChecking (FALSE, doc);
990: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
991: /* Insert. */
992: if (el == repeatEl)
993: newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, NULL);
994: else
995: newEl = Template_InsertRepeatChildAfter (doc, repeatEl, decl, el);
1.231 vatton 996:
997: parentLine = GetParentLine (el, elType.ElSSchema);
998: if (parentLine)
999: // display the element in line
1000: Template_SetInline (el, elType.ElSSchema, doc, TRUE);
1.209 vatton 1001: /* Finish insertion.*/
1002: TtaCloseUndoSequence(doc);
1.224 vatton 1003:
1.209 vatton 1004: TtaSetDocumentModified (doc);
1005: TtaSetStructureChecking (oldStructureChecking, doc);
1006: // restore the display
1007: TtaSetDisplayMode (doc, dispMode);
1008: firstEl = GetFirstEditableElement (newEl);
1009: if (firstEl)
1010: {
1011: TtaSelectElement (doc, firstEl);
1012: TtaSetStatusSelectedElement (doc, view, firstEl);
1013: }
1014: else
1015: {
1016: TtaSelectElement (doc, newEl);
1017: TtaSetStatusSelectedElement (doc, view, newEl);
1018: }
1019: }
1020: Creating_repeat = prevRepeat;
1021: }
1022:
1023: /*----------------------------------------------------------------------
1.79 quint 1024: RepeatButtonClicked
1.89 kia 1025: Called when a repeat button is clicked.
1026: Can be called for useEl, useSimple or repeat.
1027: If called for useEl or useSimple, the new element must be added after.
1028: If called for repeat, the element must be added before all.
1.206 kia 1029:
1.56 francesc 1030: Shows a menu with all the types that can be used in a use element.
1031: ----------------------------------------------------------------------*/
1.79 quint 1032: ThotBool RepeatButtonClicked (NotifyElement *event)
1.56 francesc 1033: {
1034: #ifdef TEMPLATES
1.89 kia 1035: Document doc = event->document;
1036: Element el = event->element;
1037: ElementType elType;
1.90 kia 1038: XTigerTemplate t;
1039: Element repeatEl = el;
1040: Element firstEl;
1.95 kia 1041: View view;
1.162 vatton 1042: char *listtypes = NULL;
1043: char *result = NULL;
1.104 kia 1044:
1.112 vatton 1045: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 1046: return TRUE;
1.182 vatton 1047: if (!IsTemplateInstanceDocument (doc))
1048: return FALSE; /* let Thot perform normal operation */
1.148 kia 1049:
1.95 kia 1050: TtaGetActiveView (&doc, &view);
1.238 vatton 1051: #ifdef IV
1052: if (view == 1)
1.95 kia 1053: return FALSE; /* let Thot perform normal operation */
1.238 vatton 1054: #endif
1.89 kia 1055: TtaCancelSelection(doc);
1.171 kia 1056: t = GetXTigerDocTemplate(doc);
1.89 kia 1057: elType = TtaGetElementType(el);
1.138 vatton 1058: while (elType.ElTypeNum != Template_EL_repeat)
1.179 kia 1059: {
1060: repeatEl = TtaGetParent(repeatEl);
1061: if (repeatEl == NULL)
1062: break;
1063: elType = TtaGetElementType(repeatEl);
1064: }
1.112 vatton 1065: if (repeatEl)
1.90 kia 1066: {
1.179 kia 1067: if (Template_CanInsertRepeatChild (repeatEl))
1068: {
1069: firstEl = TtaGetFirstChild (repeatEl);
1070: listtypes = Template_GetListTypes (t, firstEl);
1071: if (listtypes)
1072: {
1.223 vatton 1073: #ifdef TEMPLATE_DEBUG
1.179 kia 1074: printf("RepeatButtonClicked : \n > %s\n", listtypes);
1.223 vatton 1075: #endif /* TEMPLATE_DEBUG */
1.235 vatton 1076: result = QueryStringFromMenu (doc, listtypes, FALSE);
1.179 kia 1077: TtaFreeMemory (listtypes);
1078: if (result)
1.238 vatton 1079: {
1080: if (event->position == 1)
1081: // force the insert before
1082: DoReplicateUseElement (t, doc, view, repeatEl, repeatEl, result);
1083: else
1084: DoReplicateUseElement (t, doc, view, el, repeatEl, result);
1085: }
1.104 kia 1086: }
1.222 vatton 1087: TtaFreeMemory (result);
1088: DumpSubtree (repeatEl, doc, 0);
1.179 kia 1089:
1090: }
1091: else /* if (Template_CanInsertRepeatChild(repeatEl)) */
1.226 vatton 1092: TtaSetStatus(doc, view, TtaGetMessage (AMAYA, AM_NUMBER_OCCUR_HAVE_MAX), NULL);
1.98 kia 1093: }
1.77 vatton 1094: return TRUE; /* don't let Thot perform normal operation */
1.57 francesc 1095: #endif /* TEMPLATES */
1.94 kia 1096: return TRUE;
1097: }
1098:
1099: /*----------------------------------------------------------------------
1.235 vatton 1100: ElementIsOptional
1101: Return TRUE if the element is optional
1102: ----------------------------------------------------------------------*/
1103: ThotBool ElementIsOptional (Element el)
1104: {
1105: ElementType elType;
1106: AttributeType attType;
1107: Attribute att;
1108:
1109: elType = TtaGetElementType (el);
1110: attType.AttrSSchema = elType.ElSSchema;
1.236 vatton 1111: attType.AttrTypeNum = Template_ATTR_option;
1.235 vatton 1112: att = TtaGetAttribute (el, attType);
1113: return (att != NULL);
1114: }
1115:
1116: /*----------------------------------------------------------------------
1.94 kia 1117: UseButtonClicked
1118: Shows a menu with all the types that can be used in a use element.
1119: ----------------------------------------------------------------------*/
1120: ThotBool UseButtonClicked (NotifyElement *event)
1121: {
1122: #ifdef TEMPLATES
1123: Document doc = event->document;
1124: Element el = event->element;
1.231 vatton 1125: Element child, parent;
1.234 vatton 1126: ElementType elType, parentType;
1.243 ! vatton 1127: View view = 1;
1.94 kia 1128: XTigerTemplate t;
1129: Declaration decl;
1.226 vatton 1130: Element firstEl, newEl = NULL;
1131: char *types, *listtypes = NULL, *result = NULL;
1.235 vatton 1132: ThotBool oldStructureChecking, option;
1.95 kia 1133:
1.182 vatton 1134: if (!TtaGetDocumentAccessMode (doc))
1.110 kia 1135: return TRUE;
1.182 vatton 1136: if (!IsTemplateInstanceDocument (doc))
1137: return FALSE; /* let Thot perform normal operation */
1.238 vatton 1138: #ifdef IV
1.95 kia 1139: TtaGetActiveView (&doc, &view);
1.238 vatton 1140: if (view == 1)
1.95 kia 1141: return FALSE; /* let Thot perform normal operation */
1.238 vatton 1142: #endif
1.94 kia 1143: TtaCancelSelection(doc);
1.171 kia 1144: t = GetXTigerDocTemplate(doc);
1.94 kia 1145: if (!t)
1146: return FALSE; /* let Thot perform normal operation */
1147:
1.214 vatton 1148: elType = TtaGetElementType (el);
1.234 vatton 1149: parent = TtaGetParent (el);
1150: parentType = TtaGetElementType (parent);
1151: if (parent &&
1.214 vatton 1152: // TemplateObject is just a place holder
1.234 vatton 1153: (parentType.ElSSchema == elType.ElSSchema ||
1154: parentType.ElTypeNum == Template_EL_useEl))
1.214 vatton 1155: RepeatButtonClicked(event);
1.94 kia 1156: else
1157: {
1.209 vatton 1158: types = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1159: if (types)
1160: {
1161: listtypes = Template_ExpandTypes(t, types, NULL, FALSE);
1.223 vatton 1162: #ifdef TEMPLATE_DEBUG
1.209 vatton 1163: printf("UseButtonClicked : \n > %s\n", listtypes);
1.223 vatton 1164: #endif /* TEMPLATE_DEBUG */
1.235 vatton 1165: option = ElementIsOptional(el);
1166: result = QueryStringFromMenu(doc, listtypes, option);
1.209 vatton 1167: if (result)
1168: {
1.236 vatton 1169: decl = Template_GetDeclaration (t, result);
1.235 vatton 1170: if (decl || !strcmp (result, " "))
1.209 vatton 1171: {
1172: /* Prepare insertion.*/
1173: oldStructureChecking = TtaGetStructureChecking (doc);
1174: TtaSetStructureChecking (FALSE, doc);
1175: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.234 vatton 1176: // clean up the current content
1177: firstEl = TtaGetFirstChild (el);
1178: while (firstEl)
1179: {
1180: TtaRegisterElementDelete(firstEl, doc);
1181: TtaDeleteTree (firstEl, doc);
1182: firstEl = TtaGetFirstChild (el);
1183: }
1184:
1.231 vatton 1185: // look for the enclosing target element
1186: parent = GetParentLine (el, elType.ElSSchema);
1.235 vatton 1187: if (decl)
1188: /* Insert */
1189: newEl = Template_InsertUseChildren(doc, el, decl, parent, TRUE);
1190: else
1191: {
1.236 vatton 1192: newEl = Template_GetNewSimpleTypeInstance(doc);
1193: newEl = InsertWithNotify (newEl, NULL, el, doc);
1.235 vatton 1194: }
1.209 vatton 1195: for (child = TtaGetFirstChild(newEl); child; TtaNextSibling(&child))
1.236 vatton 1196: TtaRegisterElementCreate (child, doc);
1.206 kia 1197:
1.209 vatton 1198: /* xt:currentType attribute.*/
1.236 vatton 1199: SetAttributeStringValueWithUndo (el, Template_ATTR_currentType, result);
1.209 vatton 1200: /* Finish insertion. */
1201: TtaCloseUndoSequence(doc);
1202: TtaSetDocumentModified (doc);
1203: TtaSetStructureChecking (oldStructureChecking, doc);
1.206 kia 1204:
1.213 vatton 1205: firstEl = GetFirstEditableElement (newEl);
1.209 vatton 1206: if (firstEl)
1207: {
1208: TtaSelectElement (doc, firstEl);
1.213 vatton 1209: TtaSetStatusSelectedElement (doc, view, firstEl);
1.209 vatton 1210: }
1211: else
1212: {
1213: TtaSelectElement (doc, newEl);
1214: TtaSetStatusSelectedElement(doc, view, newEl);
1215: }
1216: }
1217: }
1218: }
1.226 vatton 1219: TtaFreeMemory (types);
1220: TtaFreeMemory (listtypes);
1221: TtaFreeMemory (result);
1.209 vatton 1222: }
1.148 kia 1223:
1.94 kia 1224: return TRUE;
1225: #endif /* TEMPLATES */
1.56 francesc 1226: return TRUE;
1227: }
1.64 francesc 1228:
1.89 kia 1229:
1.103 kia 1230: /*----------------------------------------------------------------------
1231: UseSimpleButtonClicked
1232: ----------------------------------------------------------------------*/
1233: ThotBool UseSimpleButtonClicked (NotifyElement *event)
1234: {
1235: #ifdef TEMPLATES
1.181 vatton 1236: ElementType elType, parentType;
1237: AttributeType attrType;
1238: Attribute att;
1239:
1.182 vatton 1240: if (!TtaGetDocumentAccessMode (event->document))
1.110 kia 1241: return TRUE;
1.182 vatton 1242: if (!IsTemplateInstanceDocument (event->document))
1243: return FALSE; /* let Thot perform normal operation */
1.110 kia 1244:
1.238 vatton 1245: if (event->position == 0)
1246: return FALSE; /* let Thot perform normal operation */
1.181 vatton 1247: elType = TtaGetElementType (event->element);
1248: attrType.AttrSSchema = elType.ElSSchema;
1249: attrType.AttrTypeNum = Template_ATTR_option;
1250: att = TtaGetAttribute (event->element, attrType);
1.238 vatton 1251: if (att && event->position == 2)
1.181 vatton 1252: return OptionButtonClicked (event);
1.206 kia 1253:
1.181 vatton 1254: parentType = TtaGetElementType (TtaGetParent( event->element));
1.112 vatton 1255: if (parentType.ElTypeNum == Template_EL_repeat)
1.138 vatton 1256: return RepeatButtonClicked (event);
1257: else if (parentType.ElTypeNum == Template_EL_bag)
1258: return BagButtonClicked (event);
1.238 vatton 1259: else
1260: {
1261: // select to the whole element
1262: TtaSelectElement (event->document, event->element);
1263: return TRUE;
1264: }
1.206 kia 1265: #endif /* TEMPLATES */
1.103 kia 1266: return FALSE;
1267: }
1.94 kia 1268:
1269: /*----------------------------------------------------------------------
1270: OptionButtonClicked
1271: ----------------------------------------------------------------------*/
1272: ThotBool OptionButtonClicked (NotifyElement *event)
1273: {
1274: #ifdef TEMPLATES
1.236 vatton 1275: Element el, child, next, parent;
1276: ElementType useType, childType;
1.94 kia 1277: Document doc;
1278: XTigerTemplate t;
1279:
1.182 vatton 1280: if (!TtaGetDocumentAccessMode (event->document))
1.110 kia 1281: return TRUE;
1.182 vatton 1282: if (!IsTemplateInstanceDocument (event->document))
1283: return FALSE; /* let Thot perform normal operation */
1.238 vatton 1284: #ifdef IV
1285: View view;
1.94 kia 1286: TtaGetActiveView (&doc, &view);
1287: if (view != 1)
1288: return FALSE; /* let Thot perform normal operation */
1.238 vatton 1289: #endif
1.94 kia 1290: doc = event->document;
1.236 vatton 1291: el = event->element;
1292: if (!el)
1.94 kia 1293: return FALSE; /* let Thot perform normal operation */
1.236 vatton 1294: useType = TtaGetElementType (el);
1.182 vatton 1295: if (useType.ElTypeNum != Template_EL_useEl &&
1296: useType.ElTypeNum != Template_EL_useSimple)
1.94 kia 1297: return FALSE;
1298:
1.145 kia 1299: TtaOpenUndoSequence(doc, NULL, NULL, 0, 0);
1.94 kia 1300: TtaCancelSelection (doc);
1.236 vatton 1301: child = TtaGetFirstChild (el);
1302: childType = TtaGetElementType (child);
1303: if (child == NULL ||
1304: (childType.ElSSchema == useType.ElSSchema &&
1305: childType.ElTypeNum == Template_EL_TemplateObject))
1.94 kia 1306: /* the "use" element is empty. Instantiate it */
1307: {
1.236 vatton 1308: if (child)
1309: {
1310: TtaRegisterElementDelete (child, doc);
1311: TtaDeleteTree (child, doc);
1312: }
1.171 kia 1313: t = GetXTigerDocTemplate (doc);
1.94 kia 1314: if (!t)
1315: return FALSE; // no template ?!?!
1.231 vatton 1316:
1317: // look for the enclosing target element
1.236 vatton 1318: parent = GetParentLine (el, useType.ElSSchema);
1319: InstantiateUse (t, el, doc, parent, TRUE);
1320: SetAttributeIntValue (el, Template_ATTR_option,
1321: Template_ATTR_option_VAL_option_set, TRUE);
1.94 kia 1322: }
1323: else
1324: /* remove the content of the "use" element */
1325: {
1326: do
1327: {
1.236 vatton 1328: next = child;
1.94 kia 1329: TtaNextSibling (&next);
1.236 vatton 1330: TtaRegisterElementDelete(child, doc);
1331: TtaDeleteTree (child, doc);
1332: child = next;
1.94 kia 1333: }
1334: while (next);
1.236 vatton 1335: childType.ElSSchema = useType.ElSSchema;
1336: childType.ElTypeNum = Template_EL_TemplateObject;
1337: child = TtaNewElement (doc, childType);
1338: TtaInsertFirstChild (&child, el, doc);
1339: TtaRegisterElementCreate (child, doc);
1340: TtaSetAccessRight (child, ReadOnly, doc);
1341: SetAttributeIntValue (el, Template_ATTR_option,
1342: Template_ATTR_option_VAL_option_unset, TRUE);
1.94 kia 1343: }
1.236 vatton 1344: child = TtaGetFirstChild (el);
1345: TtaSelectElement (doc, child);
1.145 kia 1346: TtaCloseUndoSequence(doc);
1.236 vatton 1347: TtaSetDocumentModified (doc);
1.94 kia 1348: return TRUE; /* don't let Thot perform normal operation */
1349: #endif /* TEMPLATES */
1350: return TRUE;
1351: }
1352:
1.191 kia 1353:
1.111 vatton 1354: /*----------------------------------------------------------------------
1.191 kia 1355: Template_FillFromDocument
1356: Fill XTigerTemplate structure with the content of the document.
1357: Load dependencies if needed.
1.111 vatton 1358: ----------------------------------------------------------------------*/
1.191 kia 1359: void Template_FillFromDocument (Document doc)
1.111 vatton 1360: {
1361: #ifdef TEMPLATES
1.191 kia 1362: XTigerTemplate t = GetXTigerTemplate (DocumentURLs[doc]);
1363: Element root;
1.206 kia 1364:
1.193 vatton 1365: if (t)
1.111 vatton 1366: {
1.239 vatton 1367: SetTemplateDocument (t, doc);
1.223 vatton 1368: #ifdef TEMPLATE_DEBUG
1.239 vatton 1369: printf("Template_FillFromDocument state: %d ", t->doc);
1.193 vatton 1370: #endif
1.210 vatton 1371: Template_PrepareTemplate(t, doc);
1.220 vatton 1372: if (IsTemplateInstanceDocument (doc))
1.144 vatton 1373: {
1.223 vatton 1374: #ifdef TEMPLATE_DEBUG
1.239 vatton 1375: printf(" -> instance\n");
1.193 vatton 1376: #endif
1.144 vatton 1377: // fix all access rights in the instance
1.191 kia 1378: root = TtaGetRootElement (doc);
1379: TtaSetAccessRight (root, ReadOnly, doc);
1.166 kia 1380: Template_FixAccessRight (t, root, doc);
1.144 vatton 1381: TtaUpdateAccessRightInViews (doc, root);
1.239 vatton 1382: // Parse template to fill structure and remove extra data
1383: ParseTemplate (t, root, doc, NULL, FALSE);
1.144 vatton 1384: }
1.223 vatton 1385: #ifdef TEMPLATE_DEBUG
1.232 vatton 1386: else if (t->state & templLibraryFlag)
1.239 vatton 1387: printf(" -> library\n");
1.232 vatton 1388: else if (t->state & templTemplate)
1.239 vatton 1389: printf(" -> template\n");
1.206 kia 1390: #endif
1.191 kia 1391: // Mark loaded
1392: t->state |= templloaded;
1393: TtaSetDocumentUnmodified (doc);
1394: UpdateTemplateMenus (doc);
1.206 kia 1395:
1.223 vatton 1396: #ifdef TEMPLATE_DEBUG
1.191 kia 1397: DumpAllDeclarations();
1.223 vatton 1398: #endif /* TEMPLATE_DEBUG */
1.191 kia 1399: }
1400: #endif /* TEMPLATES */
1401: }
1402:
1403:
1404: /*----------------------------------------------------------------------
1405: Template_CheckAndPrepareTemplate checks if the document is a template
1406: or a library and prepare XTigerTemplate structure to use it.
1407: ----------------------------------------------------------------------*/
1.241 vatton 1408: ThotBool Template_CheckAndPrepareTemplate (char* docURL)
1.191 kia 1409: {
1410: #ifdef TEMPLATES
1411: XTigerTemplate t = NULL; //GetXTigerTemplate(docURL);
1.193 vatton 1412:
1.241 vatton 1413: if (IsXTiger (docURL))
1.212 vatton 1414: {
1.223 vatton 1415: #ifdef TEMPLATE_DEBUG
1.212 vatton 1416: printf("Template_CheckAndPrepareTemplate %s templTemplate\n", docURL);
1.193 vatton 1417: #endif
1.219 vatton 1418: t = LookForXTigerTemplate (docURL);
1.191 kia 1419: t->state |= templTemplate;
1.111 vatton 1420: }
1.241 vatton 1421: else if (IsXTigerLibrary (docURL))
1.191 kia 1422: {
1.223 vatton 1423: #ifdef TEMPLATE_DEBUG
1.212 vatton 1424: printf("Template_CheckAndPrepareTemplate %s templLibrary\n", docURL);
1425: #endif
1.219 vatton 1426: t = LookForXTigerLibrary (docURL);
1.191 kia 1427: t->state |= templLibrary;
1428: }
1.193 vatton 1429: return t != NULL;
1.111 vatton 1430: #endif /* TEMPLATES */
1.191 kia 1431: return FALSE;
1.111 vatton 1432: }
1.94 kia 1433:
1.191 kia 1434:
1.66 vatton 1435: /*----------------------------------------------------------------------
1.213 vatton 1436: Template_CheckAndPrepareInstance checks if it is a template instance.
1.108 vatton 1437: If it's an instance and the template is not loaded, load it into a
1438: temporary file
1.66 vatton 1439: ----------------------------------------------------------------------*/
1.213 vatton 1440: void Template_CheckAndPrepareInstance (char *localFileName, Document doc,
1441: char* docURL)
1.65 francesc 1442: {
1443: #ifdef TEMPLATES
1.76 vatton 1444: XTigerTemplate t;
1.103 kia 1445: char *content, *ptr, *begin;
1.76 vatton 1446: gzFile stream;
1447: char buffer[2000];
1.77 vatton 1448: int res;
1.213 vatton 1449: char *template_version = NULL, *template_url = NULL;
1.65 francesc 1450:
1.171 kia 1451: stream = TtaGZOpen (localFileName);
1.76 vatton 1452: if (stream != 0)
1.65 francesc 1453: {
1.76 vatton 1454: res = gzread (stream, buffer, 1999);
1455: if (res >= 0)
1.65 francesc 1456: {
1.81 vatton 1457: buffer[res] = EOS;
1.103 kia 1458: begin = strstr (buffer, "<?xtiger");
1.206 kia 1459:
1.112 vatton 1460: if (begin)
1.209 vatton 1461: {
1462: // Search for template version
1463: ptr = strstr (begin, "templateVersion");
1464: if (ptr)
1465: ptr = strstr (ptr, "=");
1466: if (ptr)
1467: ptr = strstr (ptr, "\"");
1468: if (ptr)
1469: {
1470: // template URI
1471: content = &ptr[1];
1472: ptr = strstr (content, "\"");
1473: }
1474: if (ptr)
1475: {
1476: *ptr = EOS;
1477: //Get now the template URI
1478: template_version = TtaStrdup (content);
1479: *ptr = '"';
1480: }
1481:
1482: // Search for template uri
1483: ptr = strstr (begin, "template");
1484: if (ptr && ptr[8] != 'V')
1485: ptr = strstr (ptr, "=");
1486: if (ptr)
1487: ptr = strstr (ptr, "\"");
1488: if (ptr)
1489: {
1490: // template URI
1491: content = &ptr[1];
1492: ptr = strstr (content, "\"");
1493: }
1494: if (ptr)
1495: {
1496: *ptr = EOS;
1497: //Get now the template URI
1498: template_url = TtaStrdup (content);
1499: t = GetXTigerTemplate (template_url);
1500: if (!t)
1501: {
1502: LoadTemplate (doc, template_url);
1.219 vatton 1503: t = GetXTigerTemplate (template_url);
1.209 vatton 1504: }
1.219 vatton 1505: Template_PrepareInstance (docURL, doc, template_version, template_url);
1.209 vatton 1506: Template_AddReference (t);
1507: *ptr = '"';
1508: }
1509: }
1.65 francesc 1510: }
1511: }
1.171 kia 1512: TtaFreeMemory(template_version);
1513: TtaFreeMemory(template_url);
1.76 vatton 1514: TtaGZClose (stream);
1.65 francesc 1515: #endif /* TEMPLATES */
1516: }
1517:
1.64 francesc 1518: /*----------------------------------------------------------------------
1.178 kia 1519: ClosingTemplateDocument
1520: Callback called before closing a document which uses templates.
1.64 francesc 1521: ----------------------------------------------------------------------*/
1.241 vatton 1522: ThotBool ClosingTemplateDocument (NotifyDialog* dialog)
1.64 francesc 1523: {
1.65 francesc 1524: #ifdef TEMPLATES
1.241 vatton 1525: XTigerTemplate t = GetXTigerDocTemplate (dialog->document);
1.209 vatton 1526: if (t)
1527: Template_RemoveReference(t);
1.65 francesc 1528: #endif /* TEMPLATES */
1529: return FALSE;
1.64 francesc 1530: }
1.87 kia 1531:
1532: /*----------------------------------------------------------------------
1.120 kia 1533: IsTemplateElement
1.138 vatton 1534: Test if an element is a template element.
1.87 kia 1535: ----------------------------------------------------------------------*/
1.140 vatton 1536: ThotBool IsTemplateElement (Element elem)
1.87 kia 1537: {
1538: #ifdef TEMPLATES
1.138 vatton 1539: ElementType elType;
1540:
1.241 vatton 1541: elType = TtaGetElementType (elem);
1.138 vatton 1542: if (elType.ElSSchema)
1.241 vatton 1543: return (strcmp (TtaGetSSchemaName(elType.ElSSchema) , "Template") == 0);
1.138 vatton 1544: #endif /* TEMPLATES */
1.87 kia 1545: return FALSE;
1546: }
1547:
1548:
1549: /*----------------------------------------------------------------------
1550: GetFirstTemplateParentElement
1.138 vatton 1551: Return the first element which has "Template" as schema name or null.
1.87 kia 1552: ----------------------------------------------------------------------*/
1.241 vatton 1553: Element GetFirstTemplateParentElement (Element elem)
1.87 kia 1554: {
1555: #ifdef TEMPLATES
1.138 vatton 1556: ElementType elType;
1557:
1558: elem = TtaGetParent (elem);
1559: elType = TtaGetElementType(elem);
1560: while (elem && strcmp(TtaGetSSchemaName(elType.ElSSchema), "Template"))
1.209 vatton 1561: {
1562: elem = TtaGetParent (elem);
1563: elType = TtaGetElementType(elem);
1564: }
1.87 kia 1565: return elem;
1566: #else
1567: return NULL;
1568: #endif /* TEMPLATES */
1569: }
1.101 kia 1570:
1.103 kia 1571:
1.101 kia 1572: /*----------------------------------------------------------------------
1.209 vatton 1573: IsBeginningSelected
1574: Returns TRUE if the selection is athe beginning of an element
1575: ----------------------------------------------------------------------*/
1576: ThotBool IsBeginningSelected (Element el, Document doc)
1577: {
1578: Element selElem, prev;
1579: int firstChar, lastChar;
1580:
1581: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1582: if (firstChar <= 1 && lastChar < firstChar)
1583: {
1584: while (selElem && selElem != el)
1585: {
1586: prev = selElem;
1587: TtaPreviousSibling (&prev);
1588: if (prev)
1589: return FALSE;
1590: selElem = TtaGetParent (selElem);
1591: }
1592: return TRUE;
1593: }
1594: return FALSE;
1595: }
1596:
1597: /*----------------------------------------------------------------------
1.102 vatton 1598: TemplateElementWillBeCreated
1.101 kia 1599: Processed when an element will be created in a template context.
1600: ----------------------------------------------------------------------*/
1.102 vatton 1601: ThotBool TemplateElementWillBeCreated (NotifyElement *event)
1.101 kia 1602: {
1.103 kia 1603: #ifdef TEMPLATES
1.209 vatton 1604: ElementType elType = event->elementType;
1605: Element parent = event->element;
1606: ElementType parentType = TtaGetElementType(parent);
1607: Element ancestor, el, next;
1608: ElementType ancestorType;
1609: SSchema templateSSchema;
1610: XTigerTemplate t;
1611: char *types, *ptr, *name = NULL;
1612: int len, view, i, doc = event->document;
1613: ThotBool b;
1.101 kia 1614:
1.209 vatton 1615: if (event->info == 1)
1.115 kia 1616: return FALSE;
1617:
1.223 vatton 1618: #ifdef TEMPLATE_DEBUG
1.185 kia 1619: printf("TemplateElementWillBeCreated\n");
1.193 vatton 1620: #endif
1.209 vatton 1621: if (!TtaGetDocumentAccessMode(doc))
1.110 kia 1622: return TRUE;
1623:
1.209 vatton 1624: templateSSchema = TtaGetSSchema ("Template", doc);
1.102 vatton 1625: if (templateSSchema == NULL)
1626: return FALSE; // let Thot do the job
1.222 vatton 1627: if (!IsTemplateInstanceDocument(doc))
1628: return FALSE; // let Thot do the job
1.115 kia 1629:
1.113 kia 1630: // Fisrt, test if in a xt:bag or in a base-element xt:use
1.209 vatton 1631: if (parentType.ElSSchema == templateSSchema)
1.113 kia 1632: ancestor = parent;
1633: else
1.147 vatton 1634: ancestor = GetFirstTemplateParentElement (parent);
1.113 kia 1635:
1.147 vatton 1636: if (ancestor)
1.209 vatton 1637: {
1638: ancestorType = TtaGetElementType(ancestor);
1639: if (ancestorType.ElTypeNum == Template_EL_bag)
1640: {
1641: // only check the use child
1642: if (ancestor != parent)
1643: return FALSE; // let Thot do the job
1644: if (elType.ElSSchema == templateSSchema &&
1645: (elType.ElTypeNum == Template_EL_useSimple ||
1646: elType.ElTypeNum == Template_EL_useEl))
1647: return FALSE;
1648: return !Template_CanInsertElementInBagElement (doc, elType, ancestor);
1649: }
1650: else if (ancestorType.ElTypeNum == Template_EL_repeat)
1651: {
1652: if (elType.ElSSchema != templateSSchema ||
1653: (elType.ElTypeNum != Template_EL_useSimple &&
1654: elType.ElTypeNum != Template_EL_useEl))
1655: return TRUE; // don't let Thot do the job
1.210 vatton 1656: t = GetXTigerDocTemplate (doc);
1.209 vatton 1657: el = NULL;
1658: i = 0;
1659: next = TtaGetFirstChild (ancestor);
1660: while (next && i < event->position)
1661: {
1662: el = next;
1663: TtaNextSibling (&next);
1664: i++;
1665: }
1666: if (el)
1667: next = el;
1.210 vatton 1668: name = GetUsedTypeName (next);
1.215 vatton 1669: DoReplicateUseElement (t, doc, 1, el, ancestor, name);
1.222 vatton 1670: TtaFreeMemory (name);
1.209 vatton 1671: return TRUE; // don't let Thot do the job
1672: }
1673: else if (ancestorType.ElTypeNum == Template_EL_useSimple ||
1674: ancestorType.ElTypeNum == Template_EL_useEl)
1675: {
1676: // only check the bag child @@@ will be check exclude/include later
1677: if (elType.ElSSchema == templateSSchema &&
1678: (elType.ElTypeNum == Template_EL_useSimple ||
1679: elType.ElTypeNum == Template_EL_useEl))
1680: return TRUE; // don't let Thot do the job
1681: if (ancestor != parent)
1.215 vatton 1682: return (TtaIsReadOnly (parent) != 0); // let or not Thot do the job
1.209 vatton 1683: types = GetAttributeStringValueFromNum (ancestor, Template_ATTR_currentType, NULL);
1684: b = Template_CanInsertElementInUse (doc, elType, types,
1685: parent, event->position);
1686: if (types && !b)
1687: {
1688: // check with equivalent component
1689: name = (char *)GetXMLElementName (elType, doc);
1690: if (name)
1691: {
1692: len = strlen (name);
1693: ptr = strstr (types, name);
1694: if (ptr && len &&
1695: (ptr == types || ptr[-1] == SPACE) &&
1696: (ptr[len] == EOS || ptr[len] == SPACE))
1697: {
1.222 vatton 1698: parent = TtaGetParent (ancestor);
1.209 vatton 1699: parentType = TtaGetElementType(parent);
1.222 vatton 1700: while (parentType.ElSSchema == templateSSchema &&
1701: parentType.ElTypeNum == Template_EL_useSimple)
1702: {
1703: // move up to the inclosing use
1704: ancestor = parent;
1705: parent = TtaGetParent (ancestor);
1706: parentType = TtaGetElementType(parent);
1707: name = NULL;
1708: }
1.209 vatton 1709: if (parentType.ElSSchema == templateSSchema &&
1710: parentType.ElTypeNum == Template_EL_repeat)
1711: {
1712: // duplicate the current use
1713: TtaGetActiveView (&doc, &view);
1714: t = GetXTigerDocTemplate(doc);
1715: if (IsBeginningSelected (ancestor, doc))
1716: TtaPreviousSibling (&ancestor);
1.222 vatton 1717: if (name == NULL)
1718: {
1719: name = GetUsedTypeName (ancestor);
1720: DoReplicateUseElement (t, doc, view, ancestor,
1721: parent, name);
1722: TtaFreeMemory (name);
1723: }
1724: else
1725: DoReplicateUseElement (t, doc, view, ancestor,
1726: parent, name);
1.209 vatton 1727: }
1728: }
1729: }
1730: }
1731: TtaFreeMemory(types);
1732: return !b;
1733: }
1734: else if (ancestorType.ElTypeNum == Template_EL_component)
1735: // allow all changes
1.116 kia 1736: return FALSE;
1.113 kia 1737: }
1.206 kia 1738:
1.210 vatton 1739: if (!IsTemplateInstanceDocument(doc) &&
1740: elType.ElSSchema == templateSSchema &&
1.209 vatton 1741: (elType.ElTypeNum == Template_EL_TEXT_UNIT ||
1742: elType.ElTypeNum == Template_EL_component))
1743: return FALSE;
1744: // Cannot insert.
1.113 kia 1745: return TRUE;
1.101 kia 1746: #endif /* TEMPLATES*/
1.102 vatton 1747: return FALSE;
1.101 kia 1748: }
1749:
1750: /*----------------------------------------------------------------------
1751: TemplateElementWillBeDeleted
1752: Processed when an element will be deleted in a template context.
1753: ----------------------------------------------------------------------*/
1754: ThotBool TemplateElementWillBeDeleted (NotifyElement *event)
1755: {
1.107 kia 1756: #ifdef TEMPLATES
1757: Document doc = event->document;
1758: Element elem = event->element;
1.163 vatton 1759: Element xtElem, parent = NULL, sibling;
1.117 kia 1760: ElementType xtType, elType;
1.231 vatton 1761: char *type;
1.107 kia 1762: Declaration dec;
1.115 kia 1763: SSchema templateSSchema;
1.107 kia 1764: XTigerTemplate t;
1.156 vatton 1765: ThotBool selparent = FALSE;
1.206 kia 1766:
1.209 vatton 1767: if (event->info==1)
1.115 kia 1768: return FALSE;
1769:
1.112 vatton 1770: if (!TtaGetDocumentAccessMode(event->document))
1.110 kia 1771: return TRUE;
1772:
1.138 vatton 1773: templateSSchema = TtaGetSSchema ("Template", event->document);
1.107 kia 1774: if (templateSSchema == NULL)
1775: return FALSE; // let Thot do the job
1.206 kia 1776:
1.192 kia 1777: t = GetXTigerDocTemplate(doc);
1.209 vatton 1778: if (Template_IsTemplate(t)||Template_IsLibrary(t))
1.192 kia 1779: return FALSE; // If template or library, pass to specialized functions.
1.122 kia 1780:
1.107 kia 1781: xtElem = GetFirstTemplateParentElement(elem);
1.112 vatton 1782: if (xtElem)
1.209 vatton 1783: {
1784: xtType = TtaGetElementType(xtElem);
1785: if (xtType.ElTypeNum==Template_EL_bag)
1786: {
1787: elType = TtaGetElementType(elem);
1788: if (elType.ElSSchema==templateSSchema &&
1.231 vatton 1789: (elType.ElTypeNum==Template_EL_useSimple ||
1790: elType.ElTypeNum==Template_EL_useEl))
1.209 vatton 1791: {
1792: // Remove element manually.
1793: TtaOpenUndoSequence(doc, elem, elem, 0, 0);
1794: TtaRegisterElementDelete(elem, doc);
1795: TtaDeleteTree(elem, doc);
1796: TtaCloseUndoSequence(doc);
1797: return TRUE;
1798: }
1799: else
1800: return FALSE; // xt:bag always allow remove children.
1801: }
1802: else if (xtType.ElTypeNum == Template_EL_useSimple ||
1803: xtType.ElTypeNum == Template_EL_useEl)
1804: {
1805: parent = TtaGetParent(elem);
1806: if (xtElem != parent)
1807: {
1808: type = GetAttributeStringValueFromNum (xtElem, Template_ATTR_currentType, NULL);
1809: dec = Template_GetDeclaration(t, type);
1810: TtaFreeMemory (type);
1.109 kia 1811:
1.209 vatton 1812: if (dec && dec->nature == XmlElementNat)
1.231 vatton 1813: // Can remove only if the current type is a target element.
1814: return FALSE;
1.209 vatton 1815: else
1816: return TRUE;
1817: }
1818: }
1819: else if (xtType.ElTypeNum == Template_EL_repeat)
1.156 vatton 1820: {
1.209 vatton 1821: sibling = TtaGetSuccessor (elem);
1.156 vatton 1822: if (sibling == NULL)
1.209 vatton 1823: {
1824: // there is no next element
1825: sibling = TtaGetPredecessor (elem);
1826: if (sibling == NULL)
1827: selparent = TRUE;
1828: }
1.231 vatton 1829: TtaRegisterElementDelete (elem, doc);
1830: TtaDeleteTree (elem, doc);
1831: parent = GetParentLine (xtElem, templateSSchema);
1832: InstantiateRepeat (t, xtElem, doc, parent, TRUE);
1.209 vatton 1833: if (selparent)
1834: // look for the new sibling
1835: sibling = TtaGetFirstChild (parent);
1836: if (sibling)
1837: TtaSelectElement(doc, sibling);
1838: else
1839: TtaSelectElement(doc, parent);
1840: return TRUE;
1.156 vatton 1841: }
1.107 kia 1842: }
1.206 kia 1843:
1.109 kia 1844: //TODO Test if current element is use or repeat.
1845: // Because if an element is delete and it is the unique child of its parent,
1.206 kia 1846: // the parent intends to destroy itself.
1847:
1.107 kia 1848: return TRUE;
1849: #else /* TEMPLATES */
1.101 kia 1850: return FALSE;
1.107 kia 1851: #endif /* TEMPLATES */
1.101 kia 1852: }
1853:
1.109 kia 1854: /*----------------------------------------------------------------------
1.127 kia 1855: TemplateAttrInMenu
1856: Called by Thot when building the Attributes menu for template elements.
1857: ----------------------------------------------------------------------*/
1858: ThotBool TemplateAttrInMenu (NotifyAttribute * event)
1859: {
1860: #ifdef TEMPLATES
1861: // Prevent from showing attributes for template instance but not templates.
1.209 vatton 1862: if (IsTemplateInstanceDocument(event->document))
1.127 kia 1863: return TRUE;
1864: else
1865: #endif /* TEMPLATES */
1866: return FALSE;
1867: }
1.160 kia 1868:
1.167 kia 1869: /*----------------------------------------------------------------------
1.168 kia 1870: CreateTemplateFromDocument
1871: Create a template from the current document.
1.167 kia 1872: ----------------------------------------------------------------------*/
1873: void CreateTemplateFromDocument(Document doc, View view)
1874: {
1.171 kia 1875: #ifdef TEMPLATES
1.205 vatton 1876: char buffer[MAX_LENGTH], suffix[10];
1.196 vatton 1877:
1.204 vatton 1878: if (IsW3Path (DocumentURLs[doc]) &&
1.205 vatton 1879: DocumentMeta[doc] && DocumentMeta[doc]->full_content_location)
1880: {
1.209 vatton 1881: // use the location instead of the current URI
1.206 kia 1882:
1.209 vatton 1883: strcpy (buffer, DocumentMeta[doc]->full_content_location);
1.205 vatton 1884: }
1.204 vatton 1885: else
1886: strcpy (buffer, DocumentURLs[doc]);
1887:
1888: // remove the current suffix
1889: TtaExtractSuffix (buffer, suffix);
1.196 vatton 1890: // the new suffix
1.168 kia 1891: strcat(buffer, ".xtd");
1892: DontReplaceOldDoc = TRUE;
1.221 vatton 1893: CreateTemplate (doc, buffer);
1.204 vatton 1894: // by default .xtd files are xml files
1895: TtaSetDocumentCharset (doc, UTF_8, FALSE);
1.171 kia 1896: #endif /* TEMPLATES */
1.167 kia 1897: }
1898:
1.169 kia 1899: /*----------------------------------------------------------------------
1900: UpdateTemplateMenus
1901: ----------------------------------------------------------------------*/
1902: void UpdateTemplateMenus (Document doc)
1903: {
1.196 vatton 1904: if (IsTemplateInstanceDocument(doc))
1.180 kia 1905: {
1906: // Instance document
1907: TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
1908: TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187 kia 1909: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195 kia 1910: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
1911: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
1912: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180 kia 1913: TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196 vatton 1914: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.241 vatton 1915: TtaSetItemOff (doc, 1, Tools, BTemplateCreateImport);
1.180 kia 1916: }
1.213 vatton 1917: else if (DocumentURLs[doc] && DocumentTypes[doc] != docSource &&
1.240 vatton 1918: (IsXTiger (DocumentURLs[doc]) || IsXTigerLibrary (DocumentURLs[doc])))
1.180 kia 1919: {
1920: // Template document
1921: TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
1922: TtaSetItemOn (doc, 1, Tools, BTemplateCreateTextBox);
1.187 kia 1923: TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseBox);
1.195 kia 1924: TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseCompBox);
1925: TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeat);
1926: TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeatComp);
1.206 kia 1927: TtaSetItemOn (doc, 1, Tools, BTemplateCreateFreeBox);
1.196 vatton 1928: TtaSetItemOn (doc, 1, Tools, BTemplateCreateUnion);
1.241 vatton 1929: TtaSetItemOn (doc, 1, Tools, BTemplateCreateImport);
1.180 kia 1930: }
1.169 kia 1931: else
1.180 kia 1932: {
1933: //Standard document
1934: TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
1935: TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187 kia 1936: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195 kia 1937: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
1938: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
1939: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180 kia 1940: TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196 vatton 1941: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.241 vatton 1942: TtaSetItemOff (doc, 1, Tools, BTemplateCreateImport);
1.180 kia 1943: }
1.169 kia 1944: }
1.171 kia 1945:
1946: /*----------------------------------------------------------------------
1947: UninstanciateTemplateDocument
1948: An instance of a template is tranformed into a template-less docuemnt.
1949: Remove link between XTigerTemplate structure and document.
1950: ----------------------------------------------------------------------*/
1951: void UninstanciateTemplateDocument(Document doc)
1952: {
1953: #ifdef TEMPLATES
1954: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.209 vatton 1955: if (t)
1.171 kia 1956: Template_Close(t);
1.206 kia 1957: #endif /* TEMPLATES */
1.171 kia 1958: }
1959:
1960:
1961: /*----------------------------------------------------------------------
1962: Template_PrepareInstance
1963: Allocate XTigerTemplate structure for instance and initialize template
1964: url and template version.
1965: ----------------------------------------------------------------------*/
1.241 vatton 1966: void Template_PrepareInstance (char *url, Document doc,
1967: char* template_version, char* template_url)
1.171 kia 1968: {
1969: #ifdef TEMPLATES
1.241 vatton 1970: XTigerTemplate t = GetXTigerTemplate (url);
1.209 vatton 1971: if (!t)
1.241 vatton 1972: t = NewXTigerTemplate (url);
1.171 kia 1973: t->state = templInstance;
1.213 vatton 1974: t->templateVersion = TtaStrdup (template_version);
1.237 vatton 1975: //t->uri = TtaStrdup (fileName); // already done
1.213 vatton 1976: t->base_uri = TtaStrdup (template_url);
1.171 kia 1977: t->doc = doc;
1.178 kia 1978: t->ref = 1;
1.241 vatton 1979: #endif /* TEMPLATES */
1980: }
1.206 kia 1981:
1.241 vatton 1982: /*----------------------------------------------------------------------
1983: Template_PrepareLibrary
1984: Allocate XTigerTemplate structure for a library and initialize template
1985: url and template version.
1986: ----------------------------------------------------------------------*/
1987: void Template_PrepareLibrary (char *url, Document doc, char* template_version)
1988: {
1989: #ifdef TEMPLATES
1990: XTigerTemplate t = GetXTigerTemplate(url);
1991: if (!t)
1992: t = NewXTigerTemplate (url);
1993: t->state = templLibrary;
1994: t->templateVersion = TtaStrdup (template_version);
1995: //t->uri = TtaStrdup (fileName); // already done
1996: t->doc = doc;
1997: t->ref = 1;
1.171 kia 1998: #endif /* TEMPLATES */
1999: }
2000:
2001:
2002: /*----------------------------------------------------------------------
2003: SetDocumentAsXTigerTemplate
2004: Set the document template structure as template.
2005: ----------------------------------------------------------------------*/
1.241 vatton 2006: void SetDocumentAsXTigerTemplate (Document doc)
1.171 kia 2007: {
2008: #ifdef TEMPLATES
1.241 vatton 2009: XTigerTemplate t = GetXTigerDocTemplate (doc);
1.209 vatton 2010: if (t)
1.171 kia 2011: t->state |= templTemplate;
1.206 kia 2012: #endif /* TEMPLATES */
1.171 kia 2013: }
2014:
2015: /*----------------------------------------------------------------------
2016: SetDocumentAsXTigerLibrary
2017: Set the document template structure as template library.
2018: ----------------------------------------------------------------------*/
1.218 vatton 2019: void SetDocumentAsXTigerLibrary (Document doc)
1.171 kia 2020: {
2021: #ifdef TEMPLATES
1.221 vatton 2022: XTigerTemplate t = GetXTigerDocTemplate (doc);
1.209 vatton 2023: if (t)
1.171 kia 2024: t->state |= templLibrary;
1.206 kia 2025: #endif /* TEMPLATES */
1.171 kia 2026: }
1.174 kia 2027:
2028: /*----------------------------------------------------------------------
2029: TemplateCreateTextBox
2030: Create a xt:use types="string" box around the selection.
2031: ----------------------------------------------------------------------*/
1.221 vatton 2032: void Template_CreateTextBox (Document doc, ThotBool createComp)
1.174 kia 2033: {
2034: #ifdef TEMPLATES
1.221 vatton 2035: Element selElem, comp, use, child, copy, head;
2036: ElementType selType, useType, compType;
2037: XTigerTemplate t;
1.174 kia 2038: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.221 vatton 2039: int firstChar, lastChar, sz = 128;
1.174 kia 2040: char buffer[128];
1.221 vatton 2041: const char *title, *label;
1.218 vatton 2042: ThotBool oldStructureChecking, open;
1.206 kia 2043:
1.174 kia 2044: if (!TtaGetDocumentAccessMode(doc))
2045: return;
1.206 kia 2046:
1.209 vatton 2047: if (doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1.174 kia 2048: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2049: {
2050: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.209 vatton 2051: if (selElem)
1.174 kia 2052: {
2053: selType = TtaGetElementType(selElem);
1.209 vatton 2054: if (!TtaIsLeaf(selType))
1.174 kia 2055: {
2056: selElem = TtaGetFirstLeaf(selElem);
2057: selType = TtaGetElementType(selElem);
2058: firstChar = lastChar = 0;
2059: }
1.207 vatton 2060: if (selType.ElTypeNum == 1)
1.174 kia 2061: {
1.207 vatton 2062: // request the element label
1.221 vatton 2063: title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
2064: if (createComp)
2065: label = TtaGetMessage (AMAYA, AM_NAME);
2066: else
2067: label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
2068: QueryStringFromUser(label, title, buffer, 32);
1.207 vatton 2069: if (buffer[0] == EOS)
1.218 vatton 2070: // stop the creation
1.207 vatton 2071: return;
2072:
1.218 vatton 2073: open = TtaHasUndoSequence (doc);
1.174 kia 2074: useType.ElSSchema = sstempl;
2075: useType.ElTypeNum = Template_EL_useSimple;
1.189 kia 2076: oldStructureChecking = TtaGetStructureChecking (doc);
2077: TtaSetStructureChecking (FALSE, doc);
1.218 vatton 2078: if (firstChar == 0)
1.174 kia 2079: {
1.218 vatton 2080: // the whole string is selected
1.174 kia 2081: use = TtaNewElement(doc, useType);
1.209 vatton 2082: if (use)
1.174 kia 2083: {
1.218 vatton 2084: if (!open)
2085: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.221 vatton 2086: // create the use element
2087: TtaInsertSibling (use, selElem, FALSE, doc);
2088: SetAttributeStringValue (use, Template_ATTR_types, "string");
2089: Template_SetLabel (doc, use, buffer, FALSE);
2090: TtaRegisterElementCreate (use, doc);
2091: // move the string
1.174 kia 2092: TtaRegisterElementDelete (selElem, doc);
1.218 vatton 2093: TtaRemoveTree (selElem, doc);
1.221 vatton 2094: TtaInsertFirstChild (&selElem, use, doc);
2095: TtaRegisterElementCreate (selElem, doc);
1.218 vatton 2096: if (!open)
2097: TtaCloseUndoSequence (doc);
2098: TtaSelectElement (doc, use);
1.174 kia 2099: }
2100: }
1.218 vatton 2101: else if (GenerateInlineElement (Template_EL_useSimple, sstempl, 0, "", TRUE))
1.174 kia 2102: {
1.218 vatton 2103: // only a piece of the string is selected
2104: TtaGiveFirstSelectedElement (doc, &use, &firstChar, &lastChar);
2105: selType = TtaGetElementType (use);
2106: while (use &&
2107: (selType.ElSSchema != sstempl ||
2108: selType.ElTypeNum != Template_EL_useSimple))
1.174 kia 2109: {
1.218 vatton 2110: // look for the enclosing use element
2111: use = TtaGetParent (use);
2112: selType = TtaGetElementType (use);
2113: }
2114: if (selType.ElSSchema == sstempl &&
2115: selType.ElTypeNum == Template_EL_useSimple)
2116: {
2117: if (!open)
2118: TtaExtendUndoSequence (doc);
1.221 vatton 2119: Template_SetLabel (doc, use, buffer, TRUE);
2120: if (createComp)
2121: {
2122: // Create the component
2123: head = TemplateFindHead (doc);
2124: child = TtaGetLastChild (head);
2125: compType.ElSSchema = sstempl;
2126: compType.ElTypeNum = Template_EL_component;
2127: comp = TtaNewElement(doc, compType);
2128: if (child)
2129: TtaInsertSibling(comp, child, FALSE, doc);
2130: else
2131: TtaInsertFirstChild (&comp, head, doc);
2132: child = TtaGetFirstChild (use);
2133: copy = TtaCopyTree (child, doc, doc, comp);
2134: TtaInsertFirstChild (©, comp, doc);
2135: // register and update the component name
2136: Template_SetName (doc, comp, buffer, FALSE);
2137: TtaRegisterElementCreate (comp, doc);
2138: GiveAttributeStringValueFromNum (comp, Template_ATTR_name, buffer, &sz);
2139: // Declare the new component
2140: t = GetXTigerDocTemplate(doc);
2141: Template_DeclareNewComponent (t, buffer, comp, 1);
2142: SetAttributeStringValueWithUndo (use, Template_ATTR_types, buffer);
2143: SetAttributeStringValueWithUndo (use, Template_ATTR_currentType, buffer);
2144: TtaSelectElement (doc, use);
2145: }
2146: else
2147: {
2148: SetAttributeStringValueWithUndo (use, Template_ATTR_types, "string");
2149: // update the status bar
2150: TtaSetStatusSelectedElement (doc, 1, use);
2151: }
1.218 vatton 2152: if (!open)
1.221 vatton 2153: TtaCloseUndoSequence (doc);
1.174 kia 2154: }
2155: }
1.189 kia 2156: TtaSetStructureChecking (oldStructureChecking, doc);
1.174 kia 2157: }
2158: }
2159: }
2160: #endif /* TEMPLATES */
2161: }
2162:
2163:
2164: /*----------------------------------------------------------------------
1.221 vatton 2165: TemplateCreateTextBox
2166: Create a xt:use types="string" box around the selection.
2167: ----------------------------------------------------------------------*/
2168: void TemplateCreateTextBox (Document doc, View view)
2169: {
2170: #ifdef TEMPLATES
2171: Template_CreateTextBox (doc, FALSE);
2172: #endif /* TEMPLATES */
2173: }
2174:
1.241 vatton 2175: /*----------------------------------------------------------------------
2176: TemplateCreateImport
2177: Create a xt:import
2178: ----------------------------------------------------------------------*/
2179: void TemplateCreateImport (Document doc, View view)
2180: {
2181: #ifdef TEMPLATES
1.242 vatton 2182: Element selElem, selElem2, parent;
2183: Element el, prev, next;
2184: ElementType elType;
2185: SSchema sstempl;
2186: XTigerTemplate t;
2187: int firstChar, lastChar, firstChar2, lastChar2;
2188:
2189: if (!TtaGetDocumentAccessMode(doc))
2190: return;
2191:
2192: sstempl = TtaGetSSchema ("Template", doc);
2193: if (sstempl && IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2194: {
2195: TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
2196: TtaGiveLastSelectedElement (doc, &selElem2, &firstChar2, &lastChar2);
2197: t = GetXTigerDocTemplate (doc);
2198: if (selElem == NULL || t == NULL || selElem != selElem2 || lastChar > firstChar)
2199: return;
2200:
2201: elType = TtaGetElementType(selElem);
2202: if (elType.ElSSchema == sstempl)
2203: {
2204: // locate the position of the new import
2205: prev = next = NULL;
2206: if (elType.ElSSchema == sstempl &&
2207: elType.ElTypeNum == Template_EL_component)
2208: next = selElem;
2209: else if (elType.ElSSchema == sstempl &&
2210: (elType.ElTypeNum == Template_EL_union ||
2211: elType.ElTypeNum == Template_EL_import))
2212: prev = selElem;
2213: else if (elType.ElSSchema == sstempl &&
2214: (elType.ElTypeNum == Template_EL_head ||
2215: elType.ElTypeNum == Template_EL_Template))
2216: parent = selElem;
2217: else
2218: {
2219: elType.ElSSchema = sstempl;
2220: elType.ElTypeNum = Template_EL_head;
2221: parent = TtaGetExactTypedAncestor (selElem, elType);
2222: if (parent == NULL)
2223: {
2224: elType.ElTypeNum = Template_EL_Template;
2225: parent = TtaGetExactTypedAncestor (selElem, elType);
2226: }
2227: if (parent == NULL)
2228: return;
2229: elType.ElTypeNum = Template_EL_component;
2230: prev = TtaGetExactTypedAncestor (selElem, elType);
2231: }
2232: // create the import
2233: elType.ElTypeNum = Template_EL_import;
2234: el = TtaNewElement (doc, elType);
2235: if (prev)
2236: TtaInsertSibling (el, prev, FALSE, doc);
2237: else if (next)
2238: TtaInsertSibling (el, next, TRUE, doc);
2239: else
2240: TtaInsertFirstChild (&el, parent, doc);
2241: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2242: TtaRegisterElementCreate (el, doc);
2243: TtaSelectElement (doc, el);
2244: LinkAsImport = TRUE;
2245: SelectDestination (doc, el, TRUE, FALSE);
2246: }
2247: }
1.241 vatton 2248: #endif /* TEMPLATES */
2249: }
2250:
1.221 vatton 2251:
2252: /*----------------------------------------------------------------------
1.174 kia 2253: TemplateCreateFreeBox
2254: Create a xt:bag types="string" box around the selection.
2255: ----------------------------------------------------------------------*/
1.218 vatton 2256: void TemplateCreateFreeBox (Document doc, View view)
1.174 kia 2257: {
2258: #ifdef TEMPLATES
1.218 vatton 2259: ThotBool oldStructureChecking;
2260: DisplayMode dispMode;
2261: Element selElem, selElem2, parent, parent2, current;
2262: Element bag, child = NULL;
2263: ElementType selType, selType2, bagType;
2264: SSchema sstempl, sshtml;
2265: int firstChar, lastChar, firstChar2, lastChar2;
2266: char buffer[128], *title, *label;
1.174 kia 2267:
2268: if (!TtaGetDocumentAccessMode(doc))
2269: return;
1.218 vatton 2270:
2271: sstempl = TtaGetSSchema ("Template", doc);
2272: if (doc && sstempl &&
1.174 kia 2273: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2274: {
2275: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.218 vatton 2276: if (selElem)
1.207 vatton 2277: {
1.218 vatton 2278: TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
2279: sshtml = TtaGetSSchema ("HTML", doc);
2280: selType = TtaGetElementType(selElem);
2281: selType2 = TtaGetElementType(selElem2);
2282: bagType.ElSSchema = sstempl;
2283: bagType.ElTypeNum = Template_EL_bag;
2284: parent = TtaGetParent(selElem);
2285: parent2 = TtaGetParent(selElem2);
2286: if (firstChar != 0 || firstChar2 != 0 || parent != parent2)
2287: {
2288: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
2289: return;
2290: }
2291: else if ((selType.ElSSchema == sshtml &&
2292: (selType.ElTypeNum == HTML_EL_List_Item ||
2293: selType.ElTypeNum == HTML_EL_Term ||
2294: selType.ElTypeNum == HTML_EL_Definition ||
2295: selType.ElTypeNum == HTML_EL_Option_item ||
2296: selType.ElTypeNum == HTML_EL_CAPTION ||
2297: selType.ElTypeNum == HTML_EL_Table_row ||
2298: selType.ElTypeNum == HTML_EL_thead ||
2299: selType.ElTypeNum == HTML_EL_tbody ||
2300: selType.ElTypeNum == HTML_EL_tfoot)) ||
2301: (selType2.ElSSchema == sshtml &&
2302: (selType2.ElTypeNum == HTML_EL_List_Item ||
2303: selType2.ElTypeNum == HTML_EL_Term ||
2304: selType2.ElTypeNum == HTML_EL_Definition ||
2305: selType2.ElTypeNum == HTML_EL_Option_item ||
2306: selType2.ElTypeNum == HTML_EL_CAPTION ||
2307: selType2.ElTypeNum == HTML_EL_Table_row ||
2308: selType2.ElTypeNum == HTML_EL_thead ||
2309: selType2.ElTypeNum == HTML_EL_tbody ||
2310: selType2.ElTypeNum == HTML_EL_tfoot)))
2311: {
2312: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
2313: return;
2314: }
2315: else if (selElem != selElem2 &&
2316: ((selType.ElSSchema == sshtml &&
2317: (selType.ElTypeNum == HTML_EL_Table_cell ||
2318: selType.ElTypeNum == HTML_EL_Data_cell ||
2319: selType.ElTypeNum == HTML_EL_Heading_cell)) ||
2320: (selType2.ElSSchema == sshtml &&
2321: (selType2.ElTypeNum == HTML_EL_Table_cell ||
2322: selType2.ElTypeNum == HTML_EL_Data_cell ||
2323: selType2.ElTypeNum == HTML_EL_Heading_cell))))
2324: {
2325: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
2326: return;
2327: }
2328:
1.207 vatton 2329: // request the element label
1.218 vatton 2330: title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
2331: label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.221 vatton 2332: QueryStringFromUser(label, title, buffer, 32);
1.207 vatton 2333: if (buffer[0] == EOS)
2334: return;
1.206 kia 2335:
1.218 vatton 2336: dispMode = TtaGetDisplayMode (doc);
2337: if (dispMode == DisplayImmediately)
2338: TtaSetDisplayMode (doc, DeferredDisplay);
2339: oldStructureChecking = TtaGetStructureChecking (doc);
2340: TtaSetStructureChecking (FALSE, doc);
1.206 kia 2341:
1.218 vatton 2342: // Create and insert xt:bag element
2343: bag = TtaNewElement(doc, bagType);
2344: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2345: if (selType.ElSSchema == sshtml &&
2346: (selType.ElTypeNum == HTML_EL_Table_cell ||
2347: selType.ElTypeNum == HTML_EL_Data_cell ||
2348: selType.ElTypeNum == HTML_EL_Heading_cell))
1.174 kia 2349: {
1.221 vatton 2350: // change the selection to children
1.218 vatton 2351: selElem = TtaGetFirstChild (selElem);
2352: selElem2 = TtaGetLastChild (selElem2);
2353: }
1.185 kia 2354:
1.218 vatton 2355: TtaInsertSibling (bag, selElem, TRUE, doc);
1.221 vatton 2356: SetAttributeStringValue (bag, Template_ATTR_types, "any");
2357: Template_SetLabel (doc, bag, buffer, FALSE);
1.218 vatton 2358: TtaRegisterElementCreate (bag, doc);
1.221 vatton 2359: // move the selection into the new bag
1.218 vatton 2360: TtaNextSibling (&selElem2);
2361: while (selElem != selElem2)
2362: {
2363: current = selElem;
2364: TtaNextSibling(&selElem);
2365: TtaRegisterElementDelete (current, doc);
2366: TtaRemoveTree (current, doc);
2367: if (child)
2368: TtaInsertSibling(current, child, FALSE, doc);
2369: else
2370: TtaInsertFirstChild(¤t, bag, doc);
2371: TtaRegisterElementDelete (current, doc);
2372: child = current;
2373: }
1.185 kia 2374:
1.218 vatton 2375: TtaSetDisplayMode (doc, dispMode);
2376: TtaCloseUndoSequence(doc);
2377: TtaSelectElement (doc, bag);
2378: // update the status bar
2379: TtaSetStatusSelectedElement (doc, 1, bag);
2380: TtaSetStructureChecking (oldStructureChecking, doc);
1.174 kia 2381: }
2382: }
2383: #endif /* TEMPLATES */
2384: }
2385:
1.180 kia 2386: /*----------------------------------------------------------------------
1.198 kia 2387: TemplateCreateUnion
2388: Create a xt:union around the selection.
2389: ----------------------------------------------------------------------*/
1.218 vatton 2390: void TemplateCreateUnion (Document doc, View view)
1.198 kia 2391: {
2392: #ifdef TEMPLATES
1.199 kia 2393: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.218 vatton 2394: Element head, sibling, unionEl, selElem;
2395: ElementType unionType, selType;
1.199 kia 2396: XTigerTemplate t = GetXTigerDocTemplate(doc);
2397: char *proposed, *name = NULL, *types=NULL;
1.218 vatton 2398: int firstChar, lastChar;
1.206 kia 2399:
1.209 vatton 2400: if (t && sstempl)
1.199 kia 2401: {
1.200 kia 2402: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
2403: selType = TtaGetElementType(selElem);
1.218 vatton 2404: if (selType.ElSSchema == sstempl && selType.ElTypeNum == Template_EL_union)
1.200 kia 2405: Template_ModifyUnionElement(doc, selElem);
2406: else
1.199 kia 2407: {
1.200 kia 2408: proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
1.209 vatton 2409: if (QueryUnionFromUser(proposed, NULL, &name, &types, TRUE))
1.200 kia 2410: {
2411: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.206 kia 2412:
1.200 kia 2413: head = TemplateFindHead(doc);
2414: sibling = TtaGetLastChild(head);
1.206 kia 2415:
1.200 kia 2416: unionType.ElSSchema = sstempl;
2417: unionType.ElTypeNum = Template_EL_union;
2418: unionEl = TtaNewElement(doc, unionType);
1.206 kia 2419:
1.209 vatton 2420: if (sibling)
1.200 kia 2421: TtaInsertSibling(unionEl, sibling, FALSE, doc);
2422: else
2423: {
2424: sibling = unionEl;
2425: TtaInsertFirstChild(&sibling, head, doc);
2426: }
1.221 vatton 2427: Template_SetName(doc, unionEl, name, FALSE);
1.200 kia 2428: SetAttributeStringValue(unionEl, Template_ATTR_includeAt, types);
2429: TtaSetAccessRight(unionEl, ReadOnly, doc);
2430: TtaRegisterElementCreate(unionEl, doc);
2431: TtaSelectElement(doc, unionEl);
2432: TtaCloseUndoSequence(doc);
2433: Template_DeclareNewUnion (t, name, types, "");
2434: TtaFreeMemory(proposed);
2435: TtaFreeMemory(name);
2436: TtaFreeMemory(types);
1.206 kia 2437: }
1.200 kia 2438: }
1.206 kia 2439: }
1.200 kia 2440: #endif /* TEMPLATES */
2441: }
1.199 kia 2442:
1.200 kia 2443: /*----------------------------------------------------------------------
2444: Template_ModifyUnionElement
2445: Query the user to modify an xt:union
2446: ----------------------------------------------------------------------*/
2447: void Template_ModifyUnionElement(Document doc, Element unionEl)
2448: {
2449: XTigerTemplate t = GetXTigerDocTemplate(doc);
2450: SSchema sstempl = TtaGetSSchema ("Template", doc);
2451: ElementType unionType;
2452: char *proposed, *checked, *name, *types=NULL;
1.206 kia 2453:
1.209 vatton 2454: if (doc && unionEl && t && sstempl)
1.200 kia 2455: {
2456: unionType = TtaGetElementType(unionEl);
1.218 vatton 2457: if (unionType.ElSSchema == sstempl && unionType.ElTypeNum == Template_EL_union)
1.200 kia 2458: {
2459: proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
1.218 vatton 2460: checked = GetAttributeStringValueFromNum(unionEl, Template_ATTR_includeAt, NULL);
2461: name = GetAttributeStringValueFromNum(unionEl, Template_ATTR_name, NULL);
1.209 vatton 2462: if (QueryUnionFromUser(proposed, checked, &name, &types, FALSE))
1.199 kia 2463: {
1.200 kia 2464: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2465: SetAttributeStringValueWithUndo(unionEl, Template_ATTR_includeAt, types);
2466: TtaCloseUndoSequence(doc);
1.199 kia 2467: }
1.200 kia 2468: TtaFreeMemory(proposed);
2469: TtaFreeMemory(checked);
2470: TtaFreeMemory(name);
2471: TtaFreeMemory(types);
1.199 kia 2472: }
1.200 kia 2473: }
1.198 kia 2474: }
2475:
2476: /*----------------------------------------------------------------------
1.180 kia 2477: TemplateCreateRepeat
2478: Create a xt:repeat around the selection.
2479: ----------------------------------------------------------------------*/
1.218 vatton 2480: void TemplateCreateRepeat (Document doc, View view)
1.180 kia 2481: {
1.218 vatton 2482: Template_CreateRepeatFromSelection (doc, view, FALSE);
1.198 kia 2483: }
2484:
2485: /*----------------------------------------------------------------------
2486: TemplateCreateRepeatComp
2487: Create a xt:component with the selection and use it in a new xt:repeat
2488: ----------------------------------------------------------------------*/
1.217 vatton 2489: void TemplateCreateRepeatComp (Document doc, View view)
1.198 kia 2490: {
1.218 vatton 2491: Template_CreateRepeatFromSelection (doc, view, TRUE);
1.198 kia 2492: }
2493:
2494: /*----------------------------------------------------------------------
2495: Template_CreateRepeatFromSelection
2496: Create a xt:repeat with the selection.
2497: If selection is empty, insert an inline xt:use.
1.206 kia 2498: If createComp is false, create a xt:use with types at the selected block.
1.198 kia 2499: If createComp is true, create a component with the selection.
2500: Return the xt:use element.
2501: ----------------------------------------------------------------------*/
1.218 vatton 2502: Element Template_CreateRepeatFromSelection (Document doc, int view,
2503: ThotBool createComp)
1.198 kia 2504: {
1.180 kia 2505: #ifdef TEMPLATES
1.187 kia 2506: ThotBool oldStructureChecking;
2507: DisplayMode dispMode;
1.220 vatton 2508: Element selElem, selElem2, parent, parent2, rep = NULL, use;
2509: ElementType selType, selType2, repType;
1.180 kia 2510: int firstChar, lastChar, firstChar2, lastChar2;
2511: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.220 vatton 2512: const char *title, *label;
2513: char buffer[128], *types;
1.180 kia 2514:
2515: if (!TtaGetDocumentAccessMode(doc))
1.198 kia 2516: return NULL;
1.206 kia 2517:
1.209 vatton 2518: if (doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1.180 kia 2519: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2520: {
2521: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.220 vatton 2522: selType = TtaGetElementType(selElem);
1.180 kia 2523: TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1.220 vatton 2524: selType2 = TtaGetElementType(selElem2);
2525: repType.ElSSchema = sstempl;
2526: repType.ElTypeNum = Template_EL_repeat;
2527: parent = TtaGetParent(selElem);
2528: parent2 = TtaGetParent(selElem2);
2529: if (selElem && selElem2 && firstChar == 0 && firstChar2 == 0 &&
2530: parent == parent2)
1.180 kia 2531: {
1.220 vatton 2532: oldStructureChecking = TtaGetStructureChecking (doc);
2533: if (selElem == selElem2 && selType.ElSSchema == sstempl &&
2534: (selType.ElTypeNum == Template_EL_useEl ||
2535: selType.ElTypeNum == Template_EL_useSimple))
2536: use = selElem;
2537: else
2538: // create first the use element
2539: use = Template_CreateUseFromSelection (doc, view, createComp);
2540: if (use)
2541: {
2542: // avoid to repeat a use string
2543: types = GetAttributeStringValueFromNum (use, Template_ATTR_types, NULL);
2544: if (types && strcmp (types, "string"))
1.187 kia 2545: {
1.220 vatton 2546: TtaFreeMemory (types);
2547: // request the element label
2548: title = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATCOMP);
2549: label = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATLABEL);
1.221 vatton 2550: QueryStringFromUser(label, title, buffer, 32);
1.220 vatton 2551: if (buffer[0] == EOS)
2552: return NULL;
2553: if (use == selElem)
2554: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2555: else
2556: TtaExtendUndoSequence(doc);
2557: dispMode = TtaGetDisplayMode (doc);
2558: if (dispMode == DisplayImmediately)
2559: TtaSetDisplayMode (doc, DeferredDisplay);
2560: TtaSetStructureChecking (FALSE, doc);
2561: // create the repeat element
1.187 kia 2562: rep = TtaNewElement(doc, repType);
2563: TtaInsertSibling(rep, use, FALSE, doc);
1.221 vatton 2564: Template_SetLabel (doc, rep, buffer, FALSE);
1.220 vatton 2565: TtaRegisterElementCreate (rep, doc);
1.221 vatton 2566: // move the use element inot the repeat
1.187 kia 2567: TtaRegisterElementDelete (use, doc);
2568: TtaRemoveTree(use, doc);
1.220 vatton 2569: TtaInsertFirstChild (&use, rep, doc);
2570: TtaRegisterElementCreate (use, doc);
2571: TtaSetDisplayMode (doc, dispMode);
2572: TtaSelectElement (doc, use);
2573: TtaCloseUndoSequence (doc);
2574: TtaSetStructureChecking (oldStructureChecking, doc);
2575: // register document modification
2576: TtaSetDocumentModified (doc);
1.187 kia 2577: }
2578: }
1.220 vatton 2579: return rep;
2580: }
2581: else
2582: {
2583: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
2584: return NULL;
1.187 kia 2585: }
2586: }
2587: #endif /* TEMPLATES */
1.198 kia 2588: return NULL;
1.195 kia 2589: }
2590:
1.187 kia 2591: /*----------------------------------------------------------------------
2592: TemplateCreateUseBox
2593: Create a xt:use around the selection.
2594: ----------------------------------------------------------------------*/
1.217 vatton 2595: void TemplateCreateUseBox (Document doc, View view)
1.187 kia 2596: {
2597: #ifdef TEMPLATES
1.218 vatton 2598: Template_CreateUseFromSelection (doc, view, FALSE);
1.187 kia 2599: #endif /* TEMPLATES */
2600: }
2601:
1.194 kia 2602: /*----------------------------------------------------------------------
1.195 kia 2603: TemplateCreateUseCompBox
2604: Create a xt:use around the selection.
2605: ----------------------------------------------------------------------*/
1.217 vatton 2606: void TemplateCreateUseCompBox (Document doc, View view)
1.195 kia 2607: {
2608: #ifdef TEMPLATES
1.218 vatton 2609: Template_CreateUseFromSelection (doc, view, TRUE);
1.195 kia 2610: #endif /* TEMPLATES */
2611: }
2612:
2613: /*----------------------------------------------------------------------
1.194 kia 2614: Template_CreateInlineUse
2615: Create an inline xt:use with the selection.
2616: ----------------------------------------------------------------------*/
1.221 vatton 2617: static Element Template_CreateInlineUse (Document doc)
1.194 kia 2618: {
2619: #ifdef TEMPLATES
2620: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.206 kia 2621: Element selElem, use = NULL;
1.221 vatton 2622: ElementType selType;
1.194 kia 2623: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.221 vatton 2624: Declaration dec;
1.194 kia 2625: char *proposed, *label = NULL, *types=NULL;
1.221 vatton 2626: int firstChar, lastChar;
1.206 kia 2627: ThotBool option;
2628:
1.209 vatton 2629: if (t && sstempl)
1.194 kia 2630: {
1.221 vatton 2631: TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
2632: if (selElem == NULL)
2633: return NULL;
1.194 kia 2634: proposed = Template_GetInlineLevelDeclarations(t, TRUE, TRUE);
1.221 vatton 2635: if (proposed &&
2636: QueryNewUseFromUser (proposed, &label, &types, &option))
1.194 kia 2637: {
1.221 vatton 2638: if (label && types)
2639: {
2640: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2641: if (strstr (types, " ") != NULL)
2642: GenerateInlineElement (Template_EL_useEl, sstempl, 0, NULL, TRUE);
2643: else
2644: {
2645: // check if it's an union
2646: dec = Template_GetUnionDeclaration (t, types);
2647: if (dec)
2648: GenerateInlineElement (Template_EL_useEl, sstempl, 0, NULL, TRUE);
2649: else
2650: GenerateInlineElement (Template_EL_useSimple, sstempl, 0, NULL, TRUE);
2651: }
1.197 kia 2652:
1.221 vatton 2653: // complete the creation of the use element
2654: TtaGiveFirstSelectedElement (doc, &use, &firstChar, &lastChar);
2655: selType = TtaGetElementType (use);
2656: while (use &&
2657: (selType.ElSSchema != sstempl ||
2658: selType.ElTypeNum != Template_EL_useSimple))
2659: {
2660: // look for the enclosing use element
2661: use = TtaGetParent (use);
2662: selType = TtaGetElementType (use);
2663: }
2664: SetAttributeStringValueWithUndo (use, Template_ATTR_types, types);
2665: Template_SetLabel (doc, use, label, TRUE);
2666: TtaSelectElement (doc, use);
2667: TtaCloseUndoSequence (doc);
2668: }
1.194 kia 2669: }
1.221 vatton 2670: TtaFreeMemory (proposed);
2671: TtaFreeMemory (label);
2672: TtaFreeMemory (types);
1.206 kia 2673: return use;
2674: }
1.194 kia 2675: #endif /* TEMPLATES */
1.221 vatton 2676: return NULL;
1.194 kia 2677: }
2678:
2679: /*----------------------------------------------------------------------
2680: Template_CreateEmptyBlockUse
2681: Create a block xt:use with the selection.
2682: ----------------------------------------------------------------------*/
1.221 vatton 2683: static Element Template_CreateEmptyBlockUse (Document doc)
1.194 kia 2684: {
2685: #ifdef TEMPLATES
2686: SSchema sstempl = TtaGetSSchema ("Template", doc);
2687: ElementType useType;
1.230 vatton 2688: Element selElem, use = NULL;
1.194 kia 2689: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.221 vatton 2690: Declaration dec;
1.194 kia 2691: char *proposed, *label = NULL, *types=NULL;
1.221 vatton 2692: int firstChar, lastChar;
2693: ThotBool option;
1.206 kia 2694:
1.209 vatton 2695: if (t && sstempl)
1.194 kia 2696: {
1.221 vatton 2697: TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
2698: if (selElem == NULL)
2699: return NULL;
2700:
1.194 kia 2701: proposed = Template_GetBlockLevelDeclarations(t, TRUE);
1.221 vatton 2702: if (proposed &&
2703: QueryNewUseFromUser(proposed, &label, &types, &option))
1.194 kia 2704: {
1.221 vatton 2705: if (label && types)
2706: {
2707: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2708: // create the use element
2709: useType.ElSSchema = sstempl;
2710: if (strstr (types, " ") != NULL)
2711: useType.ElTypeNum = Template_EL_useEl;
2712: else
2713: {
2714: // check if it's an union
2715: dec = Template_GetUnionDeclaration (t, types);
2716: if (dec)
2717: useType.ElTypeNum = Template_EL_useEl;
2718: else
2719: useType.ElTypeNum = Template_EL_useSimple;
2720: }
2721: use = TtaNewElement (doc, useType);
2722: TtaInsertSibling (use, selElem, FALSE, doc);
2723: SetAttributeStringValue (use, Template_ATTR_types, types);
2724: Template_SetLabel (doc, use, label, FALSE);
2725: TtaRegisterElementCreate (use, doc);
2726: // remove the current selection
2727: TtaDeleteTree (selElem, doc);
2728: TtaSelectElement (doc, use);
2729: TtaCloseUndoSequence (doc);
2730: }
1.194 kia 2731: }
1.221 vatton 2732: TtaFreeMemory (proposed);
2733: TtaFreeMemory (label);
2734: TtaFreeMemory (types);
1.206 kia 2735: return use;
2736: }
2737:
1.194 kia 2738: #endif /* TEMPLATES */
1.221 vatton 2739: return NULL;
1.194 kia 2740: }
1.187 kia 2741:
2742: /*----------------------------------------------------------------------
2743: Template_CreateUseFromSelection
2744: Create a xt:use with the selection.
1.197 kia 2745: If selection is empty, insert an inline xt:use.
1.206 kia 2746: If createComp is false, create a xt:use with types at the selected block.
1.197 kia 2747: If createComp is true, create a component with the selection.
2748: Return the xt:use element.
1.187 kia 2749: ----------------------------------------------------------------------*/
1.231 vatton 2750: Element Template_CreateUseFromSelection (Document doc, int view, ThotBool createComp)
1.187 kia 2751: {
2752: #ifdef TEMPLATES
1.217 vatton 2753: DisplayMode dispMode;
2754: Element selElem, selElem2, parent, parent2;
2755: Element use = NULL, comp, prev, next;
1.241 vatton 2756: ElementType selType, selType2, useType, compType;
1.217 vatton 2757: SSchema sstempl, sshtml;
1.206 kia 2758: XTigerTemplate t;
1.217 vatton 2759: int firstChar, lastChar, firstChar2, lastChar2;
2760: int sz = 128, option;
2761: char *proposed, *checked, *types = NULL;
2762: char buffer[128];
2763: const char *title, *label, *name = NULL;
2764: ThotBool oldStructureChecking, opt = FALSE, getContent = FALSE;
1.187 kia 2765:
2766: if (!TtaGetDocumentAccessMode(doc))
2767: return NULL;
1.206 kia 2768:
1.217 vatton 2769: sstempl = TtaGetSSchema ("Template", doc);
2770: sshtml = TtaGetSSchema ("HTML", doc);
1.209 vatton 2771: if (doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1.187 kia 2772: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2773: {
1.217 vatton 2774: TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
2775: TtaGiveLastSelectedElement (doc, &selElem2, &firstChar2, &lastChar2);
2776: t = GetXTigerDocTemplate (doc);
2777: if (selElem == NULL || t == NULL)
2778: return NULL;
1.187 kia 2779:
1.217 vatton 2780: selType = TtaGetElementType(selElem);
2781: selType2 = TtaGetElementType(selElem2);
2782: parent = TtaGetParent(selElem);
2783: parent2 = TtaGetParent(selElem2);
2784: dispMode = TtaGetDisplayMode (doc);
2785: oldStructureChecking = TtaGetStructureChecking (doc);
2786: TtaSetStructureChecking (FALSE, doc);
2787: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.206 kia 2788:
1.217 vatton 2789: if (firstChar == 0 && firstChar2 == 0 && parent == parent2)
1.187 kia 2790: {
1.241 vatton 2791: if (selType.ElSSchema == sstempl &&
2792: (selType.ElTypeNum == Template_EL_useEl ||
2793: selType.ElTypeNum == Template_EL_useSimple))
1.187 kia 2794: {
1.217 vatton 2795: // The selection is a template element.
1.241 vatton 2796: // TODO query to add/remove some types
2797: if (TtaGetElementVolume(selElem) == 0)
1.206 kia 2798: {
1.241 vatton 2799: proposed = Template_GetAllDeclarations(t, TRUE, TRUE, TRUE);
2800: checked = GetAttributeStringValueFromNum (selElem, Template_ATTR_types, NULL);
2801: option = GetAttributeIntValueFromNum (selElem, Template_ATTR_option);
2802: opt = (option == Template_ATTR_option_VAL_option_set);
2803: if (QueryUseFromUser (proposed, checked, buffer, 128, &types, &opt))
1.209 vatton 2804: {
1.241 vatton 2805: SetAttributeStringValueWithUndo(selElem, Template_ATTR_types, types);
2806: SetAttributeStringValueWithUndo(selElem, Template_ATTR_title, buffer);
2807: if (opt)
2808: SetAttributeIntValue (selElem, Template_ATTR_option, Template_ATTR_option_VAL_option_set, TRUE);
2809: else
2810: SetAttributeIntValue (selElem, Template_ATTR_option, Template_ATTR_option_VAL_option_unset, TRUE);
2811: // register document modification
2812: TtaSetDocumentModified (doc);
1.209 vatton 2813: }
1.241 vatton 2814: TtaFreeMemory (proposed);
2815: TtaFreeMemory (checked);
2816: TtaFreeMemory (types);
2817: createComp = FALSE;
1.206 kia 2818: }
1.217 vatton 2819: }
2820: else if (selElem != selElem2 &&
2821: ((selType.ElSSchema == sshtml &&
2822: (selType.ElTypeNum == HTML_EL_List_Item ||
2823: selType.ElTypeNum == HTML_EL_Term ||
2824: selType.ElTypeNum == HTML_EL_Definition ||
2825: selType.ElTypeNum == HTML_EL_Option_item ||
2826: selType.ElTypeNum == HTML_EL_CAPTION ||
2827: selType.ElTypeNum == HTML_EL_Table_row ||
2828: selType.ElTypeNum == HTML_EL_Table_cell ||
2829: selType.ElTypeNum == HTML_EL_Data_cell ||
2830: selType.ElTypeNum == HTML_EL_Heading_cell ||
2831: selType.ElTypeNum == HTML_EL_thead ||
2832: selType.ElTypeNum == HTML_EL_tbody ||
2833: selType.ElTypeNum == HTML_EL_tfoot)) ||
2834: (selType2.ElSSchema == sshtml &&
2835: (selType2.ElTypeNum == HTML_EL_List_Item ||
2836: selType2.ElTypeNum == HTML_EL_Term ||
2837: selType2.ElTypeNum == HTML_EL_Definition ||
2838: selType2.ElTypeNum == HTML_EL_Option_item ||
2839: selType2.ElTypeNum == HTML_EL_CAPTION ||
2840: selType2.ElTypeNum == HTML_EL_Table_row ||
2841: selType2.ElTypeNum == HTML_EL_Table_cell ||
2842: selType2.ElTypeNum == HTML_EL_Data_cell ||
2843: selType2.ElTypeNum == HTML_EL_Heading_cell ||
2844: selType2.ElTypeNum == HTML_EL_thead ||
2845: selType2.ElTypeNum == HTML_EL_tbody ||
2846: selType2.ElTypeNum == HTML_EL_tfoot))))
2847: {
2848: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
2849: return NULL;
2850: }
2851: else if (selType.ElSSchema == sshtml &&
2852: selElem == selElem2 &&
2853: (selType.ElTypeNum == HTML_EL_Element ||
2854: selType.ElTypeNum == HTML_EL_Basic_Elem ||
2855: (selType.ElTypeNum == HTML_EL_Paragraph &&
2856: TtaGetElementVolume(selElem) == 0)))
2857: // Create a block-level xt:use instead of selection.
2858: use = Template_CreateEmptyBlockUse (doc);
1.241 vatton 2859: else if (selType.ElSSchema == sstempl)
2860: {
1.242 vatton 2861: // locate the position of the new component
1.241 vatton 2862: prev = next = NULL;
2863: if (selType.ElSSchema == sstempl &&
2864: selType.ElTypeNum == Template_EL_component)
2865: next = selElem;
2866: else if (selType.ElSSchema == sstempl &&
1.242 vatton 2867: (selType.ElTypeNum == Template_EL_union ||
2868: selType.ElTypeNum == Template_EL_import))
1.241 vatton 2869: prev = selElem;
2870: else if (selType.ElSSchema == sstempl &&
2871: (selType.ElTypeNum == Template_EL_head ||
1.242 vatton 2872: selType.ElTypeNum == Template_EL_Template))
1.241 vatton 2873: parent = selElem;
2874: else
2875: {
2876: selType.ElSSchema = sstempl;
2877: selType.ElTypeNum = Template_EL_head;
2878: parent = TtaGetExactTypedAncestor (selElem, selType);
2879: if (parent == NULL)
2880: {
2881: selType.ElTypeNum = Template_EL_Template;
2882: parent = TtaGetExactTypedAncestor (selElem, selType);
2883: }
2884: if (parent == NULL)
2885: return NULL;
2886: selType.ElTypeNum = Template_EL_component;
2887: prev = TtaGetExactTypedAncestor (selElem, selType);
2888: }
2889:
2890: if (QueryComponentFromUser (buffer, 128))
2891: {
1.242 vatton 2892: // create the component
1.241 vatton 2893: compType.ElSSchema = sstempl;
2894: compType.ElTypeNum = Template_EL_component;
2895: comp = TtaNewElement (doc, compType);
2896: if (prev)
2897: TtaInsertSibling (comp, prev, FALSE, doc);
2898: else if (next)
2899: TtaInsertSibling (comp, next, TRUE, doc);
2900: else
2901: TtaInsertFirstChild (&comp, parent, doc);
2902: SetAttributeStringValue (comp, Template_ATTR_name, buffer);
2903: // generate a content
2904: selElem = TtaGetFirstChild (comp);
2905: if (selElem == NULL)
2906: {
2907: selType.ElSSchema = sstempl;
2908: selType.ElTypeNum = Template_EL_TemplateObject;
2909: selElem = TtaNewElement (doc, selType);
2910: TtaInsertFirstChild (&selElem, comp, doc);
2911: }
2912: TtaRegisterElementCreate (comp, doc);
2913: TtaSelectElement (doc, selElem);
2914: // register document modification
2915: TtaSetDocumentModified (doc);
2916: }
2917: }
1.217 vatton 2918: else
2919: {
1.218 vatton 2920: if (dispMode == DisplayImmediately)
2921: TtaSetDisplayMode (doc, DeferredDisplay);
1.217 vatton 2922: prev = selElem;
2923: TtaPreviousSibling(&prev);
2924: next = selElem2;
2925: TtaNextSibling (&next);
2926: if (selElem == selElem2 &&
2927: selType2.ElSSchema == sshtml &&
2928: (selType.ElTypeNum == HTML_EL_List_Item ||
2929: selType.ElTypeNum == HTML_EL_Term ||
2930: selType.ElTypeNum == HTML_EL_Definition ||
2931: selType.ElTypeNum == HTML_EL_Option_item ||
2932: selType.ElTypeNum == HTML_EL_CAPTION ||
2933: selType.ElTypeNum == HTML_EL_Table_row ||
2934: selType.ElTypeNum == HTML_EL_Table_cell ||
2935: selType.ElTypeNum == HTML_EL_Data_cell ||
2936: selType.ElTypeNum == HTML_EL_Heading_cell ||
2937: selType.ElTypeNum == HTML_EL_thead ||
2938: selType.ElTypeNum == HTML_EL_tbody ||
2939: selType.ElTypeNum == HTML_EL_tfoot))
1.206 kia 2940: {
1.217 vatton 2941: // special management
2942: createComp = FALSE;
1.218 vatton 2943: if (selType.ElTypeNum == HTML_EL_Table_cell ||
2944: selType.ElTypeNum == HTML_EL_Data_cell ||
2945: selType.ElTypeNum == HTML_EL_Heading_cell)
2946: // generate the use element around the first child
2947: // instead of the cell
2948: getContent = TRUE;
1.206 kia 2949: }
1.217 vatton 2950: if (createComp)
1.186 kia 2951: {
1.217 vatton 2952: // Create a component from selection and a use using it
1.241 vatton 2953: comp = Template_CreateComponentFromSelection (doc);
1.217 vatton 2954: if (comp)
1.197 kia 2955: {
1.217 vatton 2956: useType.ElSSchema = sstempl;
2957: useType.ElTypeNum = Template_EL_useSimple;
2958: use = TtaNewElement (doc, useType);
2959: if (prev)
2960: TtaInsertSibling (use, prev, FALSE, doc);
2961: else if (next)
2962: TtaInsertSibling (use, next, TRUE, doc);
2963: else
2964: TtaInsertFirstChild (&use, parent, doc);
2965: GiveAttributeStringValueFromNum (comp, Template_ATTR_name, buffer, &sz);
1.221 vatton 2966: // Declare the new component
2967: Template_DeclareNewComponent (t, buffer, comp, 2);
1.217 vatton 2968: SetAttributeStringValue (use, Template_ATTR_types, buffer);
2969: SetAttributeStringValue (use, Template_ATTR_currentType, buffer);
2970: SetAttributeStringValue (use, Template_ATTR_title, buffer);
2971: TtaRegisterElementCreate (use, doc);
2972: TtaSelectElement (doc, use);
2973: // register document modification
2974: TtaSetDocumentModified (doc);
1.197 kia 2975: }
1.217 vatton 2976: }
2977: else
2978: {
2979: title = TtaGetMessage (AMAYA, AM_TEMPLATE_USELABEL);
2980: label = TtaGetMessage (AMAYA, AM_NAME);
1.221 vatton 2981: QueryStringFromUser (label, title, buffer, 32);
1.220 vatton 2982: if (buffer[0] != EOS)
1.194 kia 2983: {
1.197 kia 2984: // Create a xt:use around the selection
1.194 kia 2985: useType.ElSSchema = sstempl;
1.197 kia 2986: useType.ElTypeNum = Template_EL_useSimple;
1.217 vatton 2987: use = TtaNewElement (doc, useType);
2988: if (getContent)
2989: {
2990: parent = selElem;
2991: selElem = TtaGetFirstChild (parent);
2992: selElem2 = TtaGetLastChild (parent);
2993: prev = next = NULL;
2994: }
2995: if (selElem)
2996: {
2997: name = TtaGetElementTypeName(TtaGetElementType(selElem));
2998: TtaRegisterElementDelete (selElem, doc);
2999: TtaRemoveTree(selElem, doc);
3000: TtaInsertFirstChild (&selElem, use, doc);
3001: }
3002: else
3003: {
3004: useType.ElTypeNum = Template_EL_TEXT_UNIT;
3005: selElem = TtaNewElement (doc, useType);
3006: TtaInsertFirstChild (&selElem, use, doc);
3007: }
1.209 vatton 3008: if (prev)
1.194 kia 3009: TtaInsertSibling(use, prev, FALSE, doc);
1.217 vatton 3010: else if (next)
3011: TtaInsertSibling(use, next,TRUE, doc);
1.194 kia 3012: else
3013: TtaInsertFirstChild(&use, parent, doc);
1.217 vatton 3014: if (name && strcmp (name, "???") &&
3015: strcmp (name, "Pseudo_paragraph"))
3016: {
3017: SetAttributeStringValue (use, Template_ATTR_types, name);
3018: SetAttributeStringValue (use, Template_ATTR_currentType, name);
3019: }
3020: else
3021: {
3022: SetAttributeStringValue (use, Template_ATTR_types, "string");
3023: SetAttributeStringValue (use, Template_ATTR_currentType, "string");
1.218 vatton 3024: }
1.221 vatton 3025: Template_SetLabel (doc, use, buffer, FALSE);
1.218 vatton 3026: TtaRegisterElementCreate (use, doc);
1.217 vatton 3027: TtaSelectElement (doc, use);
3028: // register document modification
3029: TtaSetDocumentModified (doc);
1.194 kia 3030: }
1.186 kia 3031: }
1.218 vatton 3032: if (dispMode == DisplayImmediately)
3033: TtaSetDisplayMode (doc, dispMode);
1.186 kia 3034: }
1.217 vatton 3035: }
3036: else if (lastChar < firstChar)
1.218 vatton 3037: use = Template_CreateInlineUse (doc);
3038: else
1.221 vatton 3039: Template_CreateTextBox (doc, createComp);
1.206 kia 3040:
1.217 vatton 3041: TtaSetStructureChecking (oldStructureChecking, doc);
3042: TtaCloseUndoSequence(doc);
1.186 kia 3043: }
1.187 kia 3044: return use;
3045: #else /* TEMPLATES */
3046: return NULL;
1.186 kia 3047: #endif /* TEMPLATES */
3048: }
3049:
3050: /*----------------------------------------------------------------------
1.187 kia 3051: Template_CreateComponentFromSelection
1.186 kia 3052: Create a xt:component with the selection and move it into the xt:head.
1.187 kia 3053: Return the xt:component element.
1.186 kia 3054: ----------------------------------------------------------------------*/
1.217 vatton 3055: Element Template_CreateComponentFromSelection (Document doc)
1.186 kia 3056: {
3057: #ifdef TEMPLATES
1.217 vatton 3058: ThotBool oldStructureChecking;
3059: DisplayMode dispMode;
3060: Element selElem, selElem2, parent, parent2, current, child, head;
3061: Element comp = NULL;
3062: ElementType selType, selType2, compType;
3063: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.192 kia 3064: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.217 vatton 3065: int firstChar, lastChar, firstChar2, lastChar2;
3066: char buffer[128];
3067: const char *title, *label;
3068: ThotBool closeUndo = FALSE;
1.186 kia 3069:
1.209 vatton 3070: if (doc && t && TtaGetDocumentAccessMode(doc) &&
1.186 kia 3071: TtaGetDocumentAccessMode(doc) && sstempl &&
3072: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
3073: {
1.217 vatton 3074: TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
3075: if (selElem == NULL)
3076: {
3077: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
3078: return NULL;
3079: }
3080: TtaGiveLastSelectedElement (doc, &selElem2, &firstChar2, &lastChar2);
3081: selType = TtaGetElementType(selElem);
3082: selType2 = TtaGetElementType(selElem2);
3083: parent = TtaGetParent(selElem);
3084: parent2 = TtaGetParent(selElem2);
3085: label = TtaGetMessage (AMAYA, AM_NAME);
3086: title = TtaGetMessage (AMAYA, AM_TEMPLATE_NEWCOMP);
1.221 vatton 3087: QueryStringFromUser(label, title, buffer, 32);
1.217 vatton 3088: if (buffer[0] != EOS)
1.186 kia 3089: {
1.221 vatton 3090: head = TemplateFindHead (doc);
1.217 vatton 3091: compType.ElSSchema = sstempl;
3092: compType.ElTypeNum = Template_EL_component;
3093: if (head && firstChar == 0 && firstChar2 == 0 && parent == parent2)
1.186 kia 3094: {
1.217 vatton 3095: dispMode = TtaGetDisplayMode (doc);
3096: oldStructureChecking = TtaGetStructureChecking (doc);
3097: TtaSetStructureChecking (FALSE, doc);
1.221 vatton 3098: child = TtaGetLastChild (head);
1.217 vatton 3099: if (TtaHasUndoSequence (doc))
3100: closeUndo = FALSE;
3101: else
3102: {
3103: closeUndo = TRUE;
3104: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
3105: }
1.206 kia 3106:
1.220 vatton 3107: if (dispMode == DisplayImmediately)
3108: TtaSetDisplayMode (doc, DeferredDisplay);
1.217 vatton 3109: // Create the component
3110: comp = TtaNewElement(doc, compType);
3111: if (child)
3112: TtaInsertSibling(comp, child, FALSE, doc);
3113: else
3114: TtaInsertFirstChild (&comp, head, doc);
3115: // register and update the component name
1.221 vatton 3116: Template_SetName (doc, comp, buffer, FALSE);
1.217 vatton 3117:
3118: TtaNextSibling (&selElem2);
3119: child = NULL;
3120: while (selElem != selElem2)
1.180 kia 3121: {
1.217 vatton 3122: current = selElem;
3123: TtaNextSibling (&selElem);
3124: TtaRegisterElementDelete (current, doc);
3125: TtaRemoveTree (current, doc);
1.209 vatton 3126: if (child)
1.217 vatton 3127: TtaInsertSibling (current, child, FALSE, doc);
1.185 kia 3128: else
1.217 vatton 3129: TtaInsertFirstChild (¤t, comp, doc);
3130: //TtaRegisterElementCreate (current, doc);
3131: child = current;
1.180 kia 3132: }
1.217 vatton 3133: TtaRegisterElementCreate (comp, doc);
3134: if (closeUndo)
3135: TtaCloseUndoSequence(doc);
3136: TtaSetStructureChecking (oldStructureChecking, doc);
3137: if (dispMode == DisplayImmediately)
3138: TtaSetDisplayMode (doc, dispMode);
1.180 kia 3139: }
3140: }
3141: }
1.186 kia 3142: return comp;
1.187 kia 3143: #else /* TEMPLATES */
3144: return NULL;
1.206 kia 3145: #endif /* TEMPLATES */
1.180 kia 3146: }
1.187 kia 3147:
1.192 kia 3148: /*----------------------------------------------------------------------
3149: TemplateComponentWillBeDeleted
3150: Processed when a component element will be deleted in a template context.
3151: ----------------------------------------------------------------------*/
3152: ThotBool TemplateComponentWillBeDeleted (NotifyElement *event)
3153: {
3154: #ifdef TEMPLATES
3155: XTigerTemplate t = GetXTigerDocTemplate(event->document);
1.220 vatton 3156: char *elemName;
1.206 kia 3157:
1.220 vatton 3158: elemName = GetAttributeStringValueFromNum(event->element, Template_ATTR_name, NULL);
3159: if (Template_IsUsedComponent (t, event->document, elemName))
1.192 kia 3160: {
3161: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_TEMPLATE_USEDCOMP_CANTREMOVE);
3162: return TRUE;
3163: }
1.206 kia 3164: #endif /* TEMPLATES */
1.192 kia 3165: return FALSE;
3166: }
1.187 kia 3167:
1.200 kia 3168: /*----------------------------------------------------------------------
3169: UnionClicked
1.206 kia 3170: Called when a xt:union is clicked
1.200 kia 3171: ----------------------------------------------------------------------*/
3172: void UnionClicked(NotifyElement* event)
3173: {
1.209 vatton 3174: if (event->document && event->element)
1.221 vatton 3175: TtaSelectElement (event->document, event->element);
1.200 kia 3176: }
3177:
3178: /*----------------------------------------------------------------------
3179: UnionDoubleClicked
1.206 kia 3180: Called when a xt:union is double clicked
1.200 kia 3181: ----------------------------------------------------------------------*/
3182: ThotBool UnionDoubleClicked(NotifyElement* event)
3183: {
3184: Template_ModifyUnionElement(event->document, event->element);
3185: TtaSelectElement(event->document, event->element);
3186: return TRUE;
3187: }
3188:
3189: /*----------------------------------------------------------------------
3190: TemplateNameAttributeDeleted
1.206 kia 3191: Called when a xt:name will be deleted
1.200 kia 3192: ----------------------------------------------------------------------*/
3193: ThotBool TemplateNameAttributeDeleted(NotifyAttribute* event)
3194: {
3195: // Prevent xt:name deletion
3196: return TRUE;
3197: }
3198:
3199: /*----------------------------------------------------------------------
3200: TemplateNameAttributeModified
1.206 kia 3201: Called when a xt:name will be modified
1.200 kia 3202: ----------------------------------------------------------------------*/
3203: ThotBool TemplateNameAttributeModified(NotifyAttribute* event)
3204: {
3205: // Prevent xt:name modification
3206: return TRUE;
3207: }
3208:
3209: /*----------------------------------------------------------------------
3210: TemplateNameAttributeCreated
1.206 kia 3211: Called when a xt:name have been created
1.200 kia 3212: ----------------------------------------------------------------------*/
1.203 vatton 3213: void TemplateNameAttributeCreated(NotifyAttribute* event)
1.200 kia 3214: {
1.202 kia 3215: MakeUniqueName(event->element, event->document, TRUE, FALSE);
1.200 kia 3216: }
3217:
3218:
3219: /*----------------------------------------------------------------------
3220: TemplateNameAttrInMenu
3221: Called by Thot when building the Attributes menu for template elements.
3222: ----------------------------------------------------------------------*/
3223: ThotBool TemplateNameAttrInMenu (NotifyAttribute * event)
3224: {
3225: #ifdef TEMPLATES
3226: ElementType type = TtaGetElementType(event->element);
1.209 vatton 3227: if (type.ElTypeNum==Template_EL_component ||
1.200 kia 3228: type.ElTypeNum==Template_EL_union)
3229: return TRUE;
3230: #endif /* TEMPLATES */
1.209 vatton 3231: return FALSE;
1.200 kia 3232: }
3233:
Webmaster