Annotation of Amaya/amaya/templates.c, revision 1.239
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.226 vatton 1127: View view;
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: ----------------------------------------------------------------------*/
1408: ThotBool Template_CheckAndPrepareTemplate(char* docURL)
1409: {
1410: #ifdef TEMPLATES
1411: XTigerTemplate t = NULL; //GetXTigerTemplate(docURL);
1.193 vatton 1412:
1.212 vatton 1413: if (IsXTiger(docURL))
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.193 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.178 kia 1522: ThotBool ClosingTemplateDocument(NotifyDialog* dialog)
1.64 francesc 1523: {
1.65 francesc 1524: #ifdef TEMPLATES
1.171 kia 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:
1541: elType = TtaGetElementType(elem);
1542: if (elType.ElSSchema)
1.164 kia 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: ----------------------------------------------------------------------*/
1553: Element GetFirstTemplateParentElement(Element elem)
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.180 kia 1915: }
1.213 vatton 1916: else if (DocumentURLs[doc] && DocumentTypes[doc] != docSource &&
1917: IsXTiger (DocumentURLs[doc]))
1.180 kia 1918: {
1919: // Template document
1920: TtaSetItemOff (doc, 1, Tools, BCreateTemplateFromDocument);
1921: TtaSetItemOn (doc, 1, Tools, BTemplateCreateTextBox);
1.187 kia 1922: TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseBox);
1.195 kia 1923: TtaSetItemOn (doc, 1, Tools, BTemplateCreateUseCompBox);
1924: TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeat);
1925: TtaSetItemOn (doc, 1, Tools, BTemplateCreateRepeatComp);
1.206 kia 1926: TtaSetItemOn (doc, 1, Tools, BTemplateCreateFreeBox);
1.196 vatton 1927: TtaSetItemOn (doc, 1, Tools, BTemplateCreateUnion);
1.180 kia 1928: }
1.169 kia 1929: else
1.180 kia 1930: {
1931: //Standard document
1932: TtaSetItemOn (doc, 1, Tools, BCreateTemplateFromDocument);
1933: TtaSetItemOff (doc, 1, Tools, BTemplateCreateTextBox);
1.187 kia 1934: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseBox);
1.195 kia 1935: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUseCompBox);
1936: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeat);
1937: TtaSetItemOff (doc, 1, Tools, BTemplateCreateRepeatComp);
1.180 kia 1938: TtaSetItemOff (doc, 1, Tools, BTemplateCreateFreeBox);
1.196 vatton 1939: TtaSetItemOff (doc, 1, Tools, BTemplateCreateUnion);
1.180 kia 1940: }
1.169 kia 1941: }
1.171 kia 1942:
1943: /*----------------------------------------------------------------------
1944: UninstanciateTemplateDocument
1945: An instance of a template is tranformed into a template-less docuemnt.
1946: Remove link between XTigerTemplate structure and document.
1947: ----------------------------------------------------------------------*/
1948: void UninstanciateTemplateDocument(Document doc)
1949: {
1950: #ifdef TEMPLATES
1951: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.209 vatton 1952: if (t)
1.171 kia 1953: Template_Close(t);
1.206 kia 1954: #endif /* TEMPLATES */
1.171 kia 1955: }
1956:
1957:
1958: /*----------------------------------------------------------------------
1959: Template_PrepareInstance
1960: Allocate XTigerTemplate structure for instance and initialize template
1961: url and template version.
1962: ----------------------------------------------------------------------*/
1963: void Template_PrepareInstance(char *fileName, Document doc, char* template_version, char* template_url)
1964: {
1965: #ifdef TEMPLATES
1966: XTigerTemplate t = GetXTigerTemplate(fileName);
1.209 vatton 1967: if (!t)
1.171 kia 1968: t = NewXTigerTemplate(fileName);
1969: t->state = templInstance;
1.213 vatton 1970: t->templateVersion = TtaStrdup (template_version);
1.237 vatton 1971: //t->uri = TtaStrdup (fileName); // already done
1.213 vatton 1972: t->base_uri = TtaStrdup (template_url);
1.171 kia 1973: t->doc = doc;
1.178 kia 1974: t->ref = 1;
1.206 kia 1975:
1.171 kia 1976: #endif /* TEMPLATES */
1977: }
1978:
1979:
1980: /*----------------------------------------------------------------------
1981: SetDocumentAsXTigerTemplate
1982: Set the document template structure as template.
1983: ----------------------------------------------------------------------*/
1984: void SetDocumentAsXTigerTemplate(Document doc)
1985: {
1986: #ifdef TEMPLATES
1987: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.209 vatton 1988: if (t)
1.171 kia 1989: t->state |= templTemplate;
1.206 kia 1990: #endif /* TEMPLATES */
1.171 kia 1991: }
1992:
1993: /*----------------------------------------------------------------------
1994: SetDocumentAsXTigerLibrary
1995: Set the document template structure as template library.
1996: ----------------------------------------------------------------------*/
1.218 vatton 1997: void SetDocumentAsXTigerLibrary (Document doc)
1.171 kia 1998: {
1999: #ifdef TEMPLATES
1.221 vatton 2000: XTigerTemplate t = GetXTigerDocTemplate (doc);
1.209 vatton 2001: if (t)
1.171 kia 2002: t->state |= templLibrary;
1.206 kia 2003: #endif /* TEMPLATES */
1.171 kia 2004: }
1.174 kia 2005:
2006: /*----------------------------------------------------------------------
2007: TemplateCreateTextBox
2008: Create a xt:use types="string" box around the selection.
2009: ----------------------------------------------------------------------*/
1.221 vatton 2010: void Template_CreateTextBox (Document doc, ThotBool createComp)
1.174 kia 2011: {
2012: #ifdef TEMPLATES
1.221 vatton 2013: Element selElem, comp, use, child, copy, head;
2014: ElementType selType, useType, compType;
2015: XTigerTemplate t;
1.174 kia 2016: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.221 vatton 2017: int firstChar, lastChar, sz = 128;
1.174 kia 2018: char buffer[128];
1.221 vatton 2019: const char *title, *label;
1.218 vatton 2020: ThotBool oldStructureChecking, open;
1.206 kia 2021:
1.174 kia 2022: if (!TtaGetDocumentAccessMode(doc))
2023: return;
1.206 kia 2024:
1.209 vatton 2025: if (doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1.174 kia 2026: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2027: {
2028: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.209 vatton 2029: if (selElem)
1.174 kia 2030: {
2031: selType = TtaGetElementType(selElem);
1.209 vatton 2032: if (!TtaIsLeaf(selType))
1.174 kia 2033: {
2034: selElem = TtaGetFirstLeaf(selElem);
2035: selType = TtaGetElementType(selElem);
2036: firstChar = lastChar = 0;
2037: }
1.207 vatton 2038: if (selType.ElTypeNum == 1)
1.174 kia 2039: {
1.207 vatton 2040: // request the element label
1.221 vatton 2041: title = TtaGetMessage (AMAYA, AM_TEMPLATE_USESTRING);
2042: if (createComp)
2043: label = TtaGetMessage (AMAYA, AM_NAME);
2044: else
2045: label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
2046: QueryStringFromUser(label, title, buffer, 32);
1.207 vatton 2047: if (buffer[0] == EOS)
1.218 vatton 2048: // stop the creation
1.207 vatton 2049: return;
2050:
1.218 vatton 2051: open = TtaHasUndoSequence (doc);
1.174 kia 2052: useType.ElSSchema = sstempl;
2053: useType.ElTypeNum = Template_EL_useSimple;
1.189 kia 2054: oldStructureChecking = TtaGetStructureChecking (doc);
2055: TtaSetStructureChecking (FALSE, doc);
1.218 vatton 2056: if (firstChar == 0)
1.174 kia 2057: {
1.218 vatton 2058: // the whole string is selected
1.174 kia 2059: use = TtaNewElement(doc, useType);
1.209 vatton 2060: if (use)
1.174 kia 2061: {
1.218 vatton 2062: if (!open)
2063: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.221 vatton 2064: // create the use element
2065: TtaInsertSibling (use, selElem, FALSE, doc);
2066: SetAttributeStringValue (use, Template_ATTR_types, "string");
2067: Template_SetLabel (doc, use, buffer, FALSE);
2068: TtaRegisterElementCreate (use, doc);
2069: // move the string
1.174 kia 2070: TtaRegisterElementDelete (selElem, doc);
1.218 vatton 2071: TtaRemoveTree (selElem, doc);
1.221 vatton 2072: TtaInsertFirstChild (&selElem, use, doc);
2073: TtaRegisterElementCreate (selElem, doc);
1.218 vatton 2074: if (!open)
2075: TtaCloseUndoSequence (doc);
2076: TtaSelectElement (doc, use);
1.174 kia 2077: }
2078: }
1.218 vatton 2079: else if (GenerateInlineElement (Template_EL_useSimple, sstempl, 0, "", TRUE))
1.174 kia 2080: {
1.218 vatton 2081: // only a piece of the string is selected
2082: TtaGiveFirstSelectedElement (doc, &use, &firstChar, &lastChar);
2083: selType = TtaGetElementType (use);
2084: while (use &&
2085: (selType.ElSSchema != sstempl ||
2086: selType.ElTypeNum != Template_EL_useSimple))
1.174 kia 2087: {
1.218 vatton 2088: // look for the enclosing use element
2089: use = TtaGetParent (use);
2090: selType = TtaGetElementType (use);
2091: }
2092: if (selType.ElSSchema == sstempl &&
2093: selType.ElTypeNum == Template_EL_useSimple)
2094: {
2095: if (!open)
2096: TtaExtendUndoSequence (doc);
1.221 vatton 2097: Template_SetLabel (doc, use, buffer, TRUE);
2098: if (createComp)
2099: {
2100: // Create the component
2101: head = TemplateFindHead (doc);
2102: child = TtaGetLastChild (head);
2103: compType.ElSSchema = sstempl;
2104: compType.ElTypeNum = Template_EL_component;
2105: comp = TtaNewElement(doc, compType);
2106: if (child)
2107: TtaInsertSibling(comp, child, FALSE, doc);
2108: else
2109: TtaInsertFirstChild (&comp, head, doc);
2110: child = TtaGetFirstChild (use);
2111: copy = TtaCopyTree (child, doc, doc, comp);
2112: TtaInsertFirstChild (©, comp, doc);
2113: // register and update the component name
2114: Template_SetName (doc, comp, buffer, FALSE);
2115: TtaRegisterElementCreate (comp, doc);
2116: GiveAttributeStringValueFromNum (comp, Template_ATTR_name, buffer, &sz);
2117: // Declare the new component
2118: t = GetXTigerDocTemplate(doc);
2119: Template_DeclareNewComponent (t, buffer, comp, 1);
2120: SetAttributeStringValueWithUndo (use, Template_ATTR_types, buffer);
2121: SetAttributeStringValueWithUndo (use, Template_ATTR_currentType, buffer);
2122: TtaSelectElement (doc, use);
2123: }
2124: else
2125: {
2126: SetAttributeStringValueWithUndo (use, Template_ATTR_types, "string");
2127: // update the status bar
2128: TtaSetStatusSelectedElement (doc, 1, use);
2129: }
1.218 vatton 2130: if (!open)
1.221 vatton 2131: TtaCloseUndoSequence (doc);
1.174 kia 2132: }
2133: }
1.189 kia 2134: TtaSetStructureChecking (oldStructureChecking, doc);
1.174 kia 2135: }
2136: }
2137: }
2138: #endif /* TEMPLATES */
2139: }
2140:
2141:
2142: /*----------------------------------------------------------------------
1.221 vatton 2143: TemplateCreateTextBox
2144: Create a xt:use types="string" box around the selection.
2145: ----------------------------------------------------------------------*/
2146: void TemplateCreateTextBox (Document doc, View view)
2147: {
2148: #ifdef TEMPLATES
2149: Template_CreateTextBox (doc, FALSE);
2150: #endif /* TEMPLATES */
2151: }
2152:
2153:
2154: /*----------------------------------------------------------------------
1.174 kia 2155: TemplateCreateFreeBox
2156: Create a xt:bag types="string" box around the selection.
2157: ----------------------------------------------------------------------*/
1.218 vatton 2158: void TemplateCreateFreeBox (Document doc, View view)
1.174 kia 2159: {
2160: #ifdef TEMPLATES
1.218 vatton 2161: ThotBool oldStructureChecking;
2162: DisplayMode dispMode;
2163: Element selElem, selElem2, parent, parent2, current;
2164: Element bag, child = NULL;
2165: ElementType selType, selType2, bagType;
2166: SSchema sstempl, sshtml;
2167: int firstChar, lastChar, firstChar2, lastChar2;
2168: char buffer[128], *title, *label;
1.174 kia 2169:
2170: if (!TtaGetDocumentAccessMode(doc))
2171: return;
1.218 vatton 2172:
2173: sstempl = TtaGetSSchema ("Template", doc);
2174: if (doc && sstempl &&
1.174 kia 2175: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2176: {
2177: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.218 vatton 2178: if (selElem)
1.207 vatton 2179: {
1.218 vatton 2180: TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
2181: sshtml = TtaGetSSchema ("HTML", doc);
2182: selType = TtaGetElementType(selElem);
2183: selType2 = TtaGetElementType(selElem2);
2184: bagType.ElSSchema = sstempl;
2185: bagType.ElTypeNum = Template_EL_bag;
2186: parent = TtaGetParent(selElem);
2187: parent2 = TtaGetParent(selElem2);
2188: if (firstChar != 0 || firstChar2 != 0 || parent != parent2)
2189: {
2190: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
2191: return;
2192: }
2193: else if ((selType.ElSSchema == sshtml &&
2194: (selType.ElTypeNum == HTML_EL_List_Item ||
2195: selType.ElTypeNum == HTML_EL_Term ||
2196: selType.ElTypeNum == HTML_EL_Definition ||
2197: selType.ElTypeNum == HTML_EL_Option_item ||
2198: selType.ElTypeNum == HTML_EL_CAPTION ||
2199: selType.ElTypeNum == HTML_EL_Table_row ||
2200: selType.ElTypeNum == HTML_EL_thead ||
2201: selType.ElTypeNum == HTML_EL_tbody ||
2202: selType.ElTypeNum == HTML_EL_tfoot)) ||
2203: (selType2.ElSSchema == sshtml &&
2204: (selType2.ElTypeNum == HTML_EL_List_Item ||
2205: selType2.ElTypeNum == HTML_EL_Term ||
2206: selType2.ElTypeNum == HTML_EL_Definition ||
2207: selType2.ElTypeNum == HTML_EL_Option_item ||
2208: selType2.ElTypeNum == HTML_EL_CAPTION ||
2209: selType2.ElTypeNum == HTML_EL_Table_row ||
2210: selType2.ElTypeNum == HTML_EL_thead ||
2211: selType2.ElTypeNum == HTML_EL_tbody ||
2212: selType2.ElTypeNum == HTML_EL_tfoot)))
2213: {
2214: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
2215: return;
2216: }
2217: else if (selElem != selElem2 &&
2218: ((selType.ElSSchema == sshtml &&
2219: (selType.ElTypeNum == HTML_EL_Table_cell ||
2220: selType.ElTypeNum == HTML_EL_Data_cell ||
2221: selType.ElTypeNum == HTML_EL_Heading_cell)) ||
2222: (selType2.ElSSchema == sshtml &&
2223: (selType2.ElTypeNum == HTML_EL_Table_cell ||
2224: selType2.ElTypeNum == HTML_EL_Data_cell ||
2225: selType2.ElTypeNum == HTML_EL_Heading_cell))))
2226: {
2227: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
2228: return;
2229: }
2230:
1.207 vatton 2231: // request the element label
1.218 vatton 2232: title = TtaGetMessage (AMAYA, AM_TEMPLATE_BAGANY);
2233: label = TtaGetMessage (AMAYA, AM_TEMPLATE_LABEL);
1.221 vatton 2234: QueryStringFromUser(label, title, buffer, 32);
1.207 vatton 2235: if (buffer[0] == EOS)
2236: return;
1.206 kia 2237:
1.218 vatton 2238: dispMode = TtaGetDisplayMode (doc);
2239: if (dispMode == DisplayImmediately)
2240: TtaSetDisplayMode (doc, DeferredDisplay);
2241: oldStructureChecking = TtaGetStructureChecking (doc);
2242: TtaSetStructureChecking (FALSE, doc);
1.206 kia 2243:
1.218 vatton 2244: // Create and insert xt:bag element
2245: bag = TtaNewElement(doc, bagType);
2246: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2247: if (selType.ElSSchema == sshtml &&
2248: (selType.ElTypeNum == HTML_EL_Table_cell ||
2249: selType.ElTypeNum == HTML_EL_Data_cell ||
2250: selType.ElTypeNum == HTML_EL_Heading_cell))
1.174 kia 2251: {
1.221 vatton 2252: // change the selection to children
1.218 vatton 2253: selElem = TtaGetFirstChild (selElem);
2254: selElem2 = TtaGetLastChild (selElem2);
2255: }
1.185 kia 2256:
1.218 vatton 2257: TtaInsertSibling (bag, selElem, TRUE, doc);
1.221 vatton 2258: SetAttributeStringValue (bag, Template_ATTR_types, "any");
2259: Template_SetLabel (doc, bag, buffer, FALSE);
1.218 vatton 2260: TtaRegisterElementCreate (bag, doc);
1.221 vatton 2261: // move the selection into the new bag
1.218 vatton 2262: TtaNextSibling (&selElem2);
2263: while (selElem != selElem2)
2264: {
2265: current = selElem;
2266: TtaNextSibling(&selElem);
2267: TtaRegisterElementDelete (current, doc);
2268: TtaRemoveTree (current, doc);
2269: if (child)
2270: TtaInsertSibling(current, child, FALSE, doc);
2271: else
2272: TtaInsertFirstChild(¤t, bag, doc);
2273: TtaRegisterElementDelete (current, doc);
2274: child = current;
2275: }
1.185 kia 2276:
1.218 vatton 2277: TtaSetDisplayMode (doc, dispMode);
2278: TtaCloseUndoSequence(doc);
2279: TtaSelectElement (doc, bag);
2280: // update the status bar
2281: TtaSetStatusSelectedElement (doc, 1, bag);
2282: TtaSetStructureChecking (oldStructureChecking, doc);
1.174 kia 2283: }
2284: }
2285: #endif /* TEMPLATES */
2286: }
2287:
1.180 kia 2288: /*----------------------------------------------------------------------
1.198 kia 2289: TemplateCreateUnion
2290: Create a xt:union around the selection.
2291: ----------------------------------------------------------------------*/
1.218 vatton 2292: void TemplateCreateUnion (Document doc, View view)
1.198 kia 2293: {
2294: #ifdef TEMPLATES
1.199 kia 2295: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.218 vatton 2296: Element head, sibling, unionEl, selElem;
2297: ElementType unionType, selType;
1.199 kia 2298: XTigerTemplate t = GetXTigerDocTemplate(doc);
2299: char *proposed, *name = NULL, *types=NULL;
1.218 vatton 2300: int firstChar, lastChar;
1.206 kia 2301:
1.209 vatton 2302: if (t && sstempl)
1.199 kia 2303: {
1.200 kia 2304: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
2305: selType = TtaGetElementType(selElem);
1.218 vatton 2306: if (selType.ElSSchema == sstempl && selType.ElTypeNum == Template_EL_union)
1.200 kia 2307: Template_ModifyUnionElement(doc, selElem);
2308: else
1.199 kia 2309: {
1.200 kia 2310: proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
1.209 vatton 2311: if (QueryUnionFromUser(proposed, NULL, &name, &types, TRUE))
1.200 kia 2312: {
2313: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.206 kia 2314:
1.200 kia 2315: head = TemplateFindHead(doc);
2316: sibling = TtaGetLastChild(head);
1.206 kia 2317:
1.200 kia 2318: unionType.ElSSchema = sstempl;
2319: unionType.ElTypeNum = Template_EL_union;
2320: unionEl = TtaNewElement(doc, unionType);
1.206 kia 2321:
1.209 vatton 2322: if (sibling)
1.200 kia 2323: TtaInsertSibling(unionEl, sibling, FALSE, doc);
2324: else
2325: {
2326: sibling = unionEl;
2327: TtaInsertFirstChild(&sibling, head, doc);
2328: }
1.221 vatton 2329: Template_SetName(doc, unionEl, name, FALSE);
1.200 kia 2330: SetAttributeStringValue(unionEl, Template_ATTR_includeAt, types);
2331: TtaSetAccessRight(unionEl, ReadOnly, doc);
2332: TtaRegisterElementCreate(unionEl, doc);
2333: TtaSelectElement(doc, unionEl);
2334: TtaCloseUndoSequence(doc);
2335: Template_DeclareNewUnion (t, name, types, "");
2336: TtaFreeMemory(proposed);
2337: TtaFreeMemory(name);
2338: TtaFreeMemory(types);
1.206 kia 2339: }
1.200 kia 2340: }
1.206 kia 2341: }
1.200 kia 2342: #endif /* TEMPLATES */
2343: }
1.199 kia 2344:
1.200 kia 2345: /*----------------------------------------------------------------------
2346: Template_ModifyUnionElement
2347: Query the user to modify an xt:union
2348: ----------------------------------------------------------------------*/
2349: void Template_ModifyUnionElement(Document doc, Element unionEl)
2350: {
2351: XTigerTemplate t = GetXTigerDocTemplate(doc);
2352: SSchema sstempl = TtaGetSSchema ("Template", doc);
2353: ElementType unionType;
2354: char *proposed, *checked, *name, *types=NULL;
1.206 kia 2355:
1.209 vatton 2356: if (doc && unionEl && t && sstempl)
1.200 kia 2357: {
2358: unionType = TtaGetElementType(unionEl);
1.218 vatton 2359: if (unionType.ElSSchema == sstempl && unionType.ElTypeNum == Template_EL_union)
1.200 kia 2360: {
2361: proposed = Template_GetAllDeclarations(t, TRUE, FALSE, TRUE);
1.218 vatton 2362: checked = GetAttributeStringValueFromNum(unionEl, Template_ATTR_includeAt, NULL);
2363: name = GetAttributeStringValueFromNum(unionEl, Template_ATTR_name, NULL);
1.209 vatton 2364: if (QueryUnionFromUser(proposed, checked, &name, &types, FALSE))
1.199 kia 2365: {
1.200 kia 2366: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2367: SetAttributeStringValueWithUndo(unionEl, Template_ATTR_includeAt, types);
2368: TtaCloseUndoSequence(doc);
1.199 kia 2369: }
1.200 kia 2370: TtaFreeMemory(proposed);
2371: TtaFreeMemory(checked);
2372: TtaFreeMemory(name);
2373: TtaFreeMemory(types);
1.199 kia 2374: }
1.200 kia 2375: }
1.198 kia 2376: }
2377:
2378: /*----------------------------------------------------------------------
1.180 kia 2379: TemplateCreateRepeat
2380: Create a xt:repeat around the selection.
2381: ----------------------------------------------------------------------*/
1.218 vatton 2382: void TemplateCreateRepeat (Document doc, View view)
1.180 kia 2383: {
1.218 vatton 2384: Template_CreateRepeatFromSelection (doc, view, FALSE);
1.198 kia 2385: }
2386:
2387: /*----------------------------------------------------------------------
2388: TemplateCreateRepeatComp
2389: Create a xt:component with the selection and use it in a new xt:repeat
2390: ----------------------------------------------------------------------*/
1.217 vatton 2391: void TemplateCreateRepeatComp (Document doc, View view)
1.198 kia 2392: {
1.218 vatton 2393: Template_CreateRepeatFromSelection (doc, view, TRUE);
1.198 kia 2394: }
2395:
2396: /*----------------------------------------------------------------------
2397: Template_CreateRepeatFromSelection
2398: Create a xt:repeat with the selection.
2399: If selection is empty, insert an inline xt:use.
1.206 kia 2400: If createComp is false, create a xt:use with types at the selected block.
1.198 kia 2401: If createComp is true, create a component with the selection.
2402: Return the xt:use element.
2403: ----------------------------------------------------------------------*/
1.218 vatton 2404: Element Template_CreateRepeatFromSelection (Document doc, int view,
2405: ThotBool createComp)
1.198 kia 2406: {
1.180 kia 2407: #ifdef TEMPLATES
1.187 kia 2408: ThotBool oldStructureChecking;
2409: DisplayMode dispMode;
1.220 vatton 2410: Element selElem, selElem2, parent, parent2, rep = NULL, use;
2411: ElementType selType, selType2, repType;
1.180 kia 2412: int firstChar, lastChar, firstChar2, lastChar2;
2413: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.220 vatton 2414: const char *title, *label;
2415: char buffer[128], *types;
1.180 kia 2416:
2417: if (!TtaGetDocumentAccessMode(doc))
1.198 kia 2418: return NULL;
1.206 kia 2419:
1.209 vatton 2420: if (doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1.180 kia 2421: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2422: {
2423: TtaGiveFirstSelectedElement(doc, &selElem, &firstChar, &lastChar);
1.220 vatton 2424: selType = TtaGetElementType(selElem);
1.180 kia 2425: TtaGiveLastSelectedElement(doc, &selElem2, &firstChar2, &lastChar2);
1.220 vatton 2426: selType2 = TtaGetElementType(selElem2);
2427: repType.ElSSchema = sstempl;
2428: repType.ElTypeNum = Template_EL_repeat;
2429: parent = TtaGetParent(selElem);
2430: parent2 = TtaGetParent(selElem2);
2431: if (selElem && selElem2 && firstChar == 0 && firstChar2 == 0 &&
2432: parent == parent2)
1.180 kia 2433: {
1.220 vatton 2434: oldStructureChecking = TtaGetStructureChecking (doc);
2435: if (selElem == selElem2 && selType.ElSSchema == sstempl &&
2436: (selType.ElTypeNum == Template_EL_useEl ||
2437: selType.ElTypeNum == Template_EL_useSimple))
2438: use = selElem;
2439: else
2440: // create first the use element
2441: use = Template_CreateUseFromSelection (doc, view, createComp);
2442: if (use)
2443: {
2444: // avoid to repeat a use string
2445: types = GetAttributeStringValueFromNum (use, Template_ATTR_types, NULL);
2446: if (types && strcmp (types, "string"))
1.187 kia 2447: {
1.220 vatton 2448: TtaFreeMemory (types);
2449: // request the element label
2450: title = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATCOMP);
2451: label = TtaGetMessage (AMAYA, AM_TEMPLATE_REPEATLABEL);
1.221 vatton 2452: QueryStringFromUser(label, title, buffer, 32);
1.220 vatton 2453: if (buffer[0] == EOS)
2454: return NULL;
2455: if (use == selElem)
2456: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2457: else
2458: TtaExtendUndoSequence(doc);
2459: dispMode = TtaGetDisplayMode (doc);
2460: if (dispMode == DisplayImmediately)
2461: TtaSetDisplayMode (doc, DeferredDisplay);
2462: TtaSetStructureChecking (FALSE, doc);
2463: // create the repeat element
1.187 kia 2464: rep = TtaNewElement(doc, repType);
2465: TtaInsertSibling(rep, use, FALSE, doc);
1.221 vatton 2466: Template_SetLabel (doc, rep, buffer, FALSE);
1.220 vatton 2467: TtaRegisterElementCreate (rep, doc);
1.221 vatton 2468: // move the use element inot the repeat
1.187 kia 2469: TtaRegisterElementDelete (use, doc);
2470: TtaRemoveTree(use, doc);
1.220 vatton 2471: TtaInsertFirstChild (&use, rep, doc);
2472: TtaRegisterElementCreate (use, doc);
2473: TtaSetDisplayMode (doc, dispMode);
2474: TtaSelectElement (doc, use);
2475: TtaCloseUndoSequence (doc);
2476: TtaSetStructureChecking (oldStructureChecking, doc);
2477: // register document modification
2478: TtaSetDocumentModified (doc);
1.187 kia 2479: }
2480: }
1.220 vatton 2481: return rep;
2482: }
2483: else
2484: {
2485: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
2486: return NULL;
1.187 kia 2487: }
2488: }
2489: #endif /* TEMPLATES */
1.198 kia 2490: return NULL;
1.195 kia 2491: }
2492:
1.187 kia 2493: /*----------------------------------------------------------------------
2494: TemplateCreateUseBox
2495: Create a xt:use around the selection.
2496: ----------------------------------------------------------------------*/
1.217 vatton 2497: void TemplateCreateUseBox (Document doc, View view)
1.187 kia 2498: {
2499: #ifdef TEMPLATES
1.218 vatton 2500: Template_CreateUseFromSelection (doc, view, FALSE);
1.187 kia 2501: #endif /* TEMPLATES */
2502: }
2503:
1.194 kia 2504: /*----------------------------------------------------------------------
1.195 kia 2505: TemplateCreateUseCompBox
2506: Create a xt:use around the selection.
2507: ----------------------------------------------------------------------*/
1.217 vatton 2508: void TemplateCreateUseCompBox (Document doc, View view)
1.195 kia 2509: {
2510: #ifdef TEMPLATES
1.218 vatton 2511: Template_CreateUseFromSelection (doc, view, TRUE);
1.195 kia 2512: #endif /* TEMPLATES */
2513: }
2514:
2515: /*----------------------------------------------------------------------
1.194 kia 2516: Template_CreateInlineUse
2517: Create an inline xt:use with the selection.
2518: ----------------------------------------------------------------------*/
1.221 vatton 2519: static Element Template_CreateInlineUse (Document doc)
1.194 kia 2520: {
2521: #ifdef TEMPLATES
2522: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.206 kia 2523: Element selElem, use = NULL;
1.221 vatton 2524: ElementType selType;
1.194 kia 2525: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.221 vatton 2526: Declaration dec;
1.194 kia 2527: char *proposed, *label = NULL, *types=NULL;
1.221 vatton 2528: int firstChar, lastChar;
1.206 kia 2529: ThotBool option;
2530:
1.209 vatton 2531: if (t && sstempl)
1.194 kia 2532: {
1.221 vatton 2533: TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
2534: if (selElem == NULL)
2535: return NULL;
1.194 kia 2536: proposed = Template_GetInlineLevelDeclarations(t, TRUE, TRUE);
1.221 vatton 2537: if (proposed &&
2538: QueryNewUseFromUser (proposed, &label, &types, &option))
1.194 kia 2539: {
1.221 vatton 2540: if (label && types)
2541: {
2542: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2543: if (strstr (types, " ") != NULL)
2544: GenerateInlineElement (Template_EL_useEl, sstempl, 0, NULL, TRUE);
2545: else
2546: {
2547: // check if it's an union
2548: dec = Template_GetUnionDeclaration (t, types);
2549: if (dec)
2550: GenerateInlineElement (Template_EL_useEl, sstempl, 0, NULL, TRUE);
2551: else
2552: GenerateInlineElement (Template_EL_useSimple, sstempl, 0, NULL, TRUE);
2553: }
1.197 kia 2554:
1.221 vatton 2555: // complete the creation of the use element
2556: TtaGiveFirstSelectedElement (doc, &use, &firstChar, &lastChar);
2557: selType = TtaGetElementType (use);
2558: while (use &&
2559: (selType.ElSSchema != sstempl ||
2560: selType.ElTypeNum != Template_EL_useSimple))
2561: {
2562: // look for the enclosing use element
2563: use = TtaGetParent (use);
2564: selType = TtaGetElementType (use);
2565: }
2566: SetAttributeStringValueWithUndo (use, Template_ATTR_types, types);
2567: Template_SetLabel (doc, use, label, TRUE);
2568: TtaSelectElement (doc, use);
2569: TtaCloseUndoSequence (doc);
2570: }
1.194 kia 2571: }
1.221 vatton 2572: TtaFreeMemory (proposed);
2573: TtaFreeMemory (label);
2574: TtaFreeMemory (types);
1.206 kia 2575: return use;
2576: }
1.194 kia 2577: #endif /* TEMPLATES */
1.221 vatton 2578: return NULL;
1.194 kia 2579: }
2580:
2581: /*----------------------------------------------------------------------
2582: Template_CreateEmptyBlockUse
2583: Create a block xt:use with the selection.
2584: ----------------------------------------------------------------------*/
1.221 vatton 2585: static Element Template_CreateEmptyBlockUse (Document doc)
1.194 kia 2586: {
2587: #ifdef TEMPLATES
2588: SSchema sstempl = TtaGetSSchema ("Template", doc);
2589: ElementType useType;
1.230 vatton 2590: Element selElem, use = NULL;
1.194 kia 2591: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.221 vatton 2592: Declaration dec;
1.194 kia 2593: char *proposed, *label = NULL, *types=NULL;
1.221 vatton 2594: int firstChar, lastChar;
2595: ThotBool option;
1.206 kia 2596:
1.209 vatton 2597: if (t && sstempl)
1.194 kia 2598: {
1.221 vatton 2599: TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
2600: if (selElem == NULL)
2601: return NULL;
2602:
1.194 kia 2603: proposed = Template_GetBlockLevelDeclarations(t, TRUE);
1.221 vatton 2604: if (proposed &&
2605: QueryNewUseFromUser(proposed, &label, &types, &option))
1.194 kia 2606: {
1.221 vatton 2607: if (label && types)
2608: {
2609: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2610: // create the use element
2611: useType.ElSSchema = sstempl;
2612: if (strstr (types, " ") != NULL)
2613: useType.ElTypeNum = Template_EL_useEl;
2614: else
2615: {
2616: // check if it's an union
2617: dec = Template_GetUnionDeclaration (t, types);
2618: if (dec)
2619: useType.ElTypeNum = Template_EL_useEl;
2620: else
2621: useType.ElTypeNum = Template_EL_useSimple;
2622: }
2623: use = TtaNewElement (doc, useType);
2624: TtaInsertSibling (use, selElem, FALSE, doc);
2625: SetAttributeStringValue (use, Template_ATTR_types, types);
2626: Template_SetLabel (doc, use, label, FALSE);
2627: TtaRegisterElementCreate (use, doc);
2628: // remove the current selection
2629: TtaDeleteTree (selElem, doc);
2630: TtaSelectElement (doc, use);
2631: TtaCloseUndoSequence (doc);
2632: }
1.194 kia 2633: }
1.221 vatton 2634: TtaFreeMemory (proposed);
2635: TtaFreeMemory (label);
2636: TtaFreeMemory (types);
1.206 kia 2637: return use;
2638: }
2639:
1.194 kia 2640: #endif /* TEMPLATES */
1.221 vatton 2641: return NULL;
1.194 kia 2642: }
1.187 kia 2643:
2644: /*----------------------------------------------------------------------
2645: Template_CreateUseFromSelection
2646: Create a xt:use with the selection.
1.197 kia 2647: If selection is empty, insert an inline xt:use.
1.206 kia 2648: If createComp is false, create a xt:use with types at the selected block.
1.197 kia 2649: If createComp is true, create a component with the selection.
2650: Return the xt:use element.
1.187 kia 2651: ----------------------------------------------------------------------*/
1.231 vatton 2652: Element Template_CreateUseFromSelection (Document doc, int view, ThotBool createComp)
1.187 kia 2653: {
2654: #ifdef TEMPLATES
1.217 vatton 2655: DisplayMode dispMode;
2656: Element selElem, selElem2, parent, parent2;
2657: Element use = NULL, comp, prev, next;
2658: ElementType selType, selType2, useType;
2659: SSchema sstempl, sshtml;
1.206 kia 2660: XTigerTemplate t;
1.217 vatton 2661: int firstChar, lastChar, firstChar2, lastChar2;
2662: int sz = 128, option;
2663: char *proposed, *checked, *types = NULL;
2664: char buffer[128];
2665: const char *title, *label, *name = NULL;
2666: ThotBool oldStructureChecking, opt = FALSE, getContent = FALSE;
1.187 kia 2667:
2668: if (!TtaGetDocumentAccessMode(doc))
2669: return NULL;
1.206 kia 2670:
1.217 vatton 2671: sstempl = TtaGetSSchema ("Template", doc);
2672: sshtml = TtaGetSSchema ("HTML", doc);
1.209 vatton 2673: if (doc && TtaGetDocumentAccessMode(doc) && sstempl &&
1.187 kia 2674: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2675: {
1.217 vatton 2676: TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
2677: TtaGiveLastSelectedElement (doc, &selElem2, &firstChar2, &lastChar2);
2678: t = GetXTigerDocTemplate (doc);
2679: if (selElem == NULL || t == NULL)
2680: return NULL;
1.187 kia 2681:
1.217 vatton 2682: selType = TtaGetElementType(selElem);
2683: selType2 = TtaGetElementType(selElem2);
2684: parent = TtaGetParent(selElem);
2685: parent2 = TtaGetParent(selElem2);
2686: dispMode = TtaGetDisplayMode (doc);
2687: oldStructureChecking = TtaGetStructureChecking (doc);
2688: TtaSetStructureChecking (FALSE, doc);
2689: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.206 kia 2690:
1.217 vatton 2691: if (firstChar == 0 && firstChar2 == 0 && parent == parent2)
1.187 kia 2692: {
1.217 vatton 2693: if (selType.ElSSchema == sstempl)
1.187 kia 2694: {
1.217 vatton 2695: // The selection is a template element.
2696: switch(selType.ElTypeNum)
1.206 kia 2697: {
1.217 vatton 2698: case Template_EL_useEl:
2699: case Template_EL_useSimple:
2700: // TODO query to add/remove some types
2701: if (TtaGetElementVolume(selElem) == 0)
1.209 vatton 2702: {
1.217 vatton 2703: proposed = Template_GetAllDeclarations(t, TRUE, TRUE, TRUE);
2704: checked = GetAttributeStringValueFromNum (selElem, Template_ATTR_types, NULL);
2705: option = GetAttributeIntValueFromNum (selElem, Template_ATTR_option);
2706: opt = (option == Template_ATTR_option_VAL_option_set);
2707: if (QueryUseFromUser (proposed, checked, buffer, 128, &types, &opt))
1.206 kia 2708: {
1.217 vatton 2709: SetAttributeStringValueWithUndo(selElem, Template_ATTR_types, types);
2710: SetAttributeStringValueWithUndo(selElem, Template_ATTR_title, buffer);
2711: if (opt)
2712: SetAttributeIntValue (selElem, Template_ATTR_option, Template_ATTR_option_VAL_option_set, TRUE);
2713: else
2714: SetAttributeIntValue (selElem, Template_ATTR_option, Template_ATTR_option_VAL_option_unset, TRUE);
2715: // register document modification
2716: TtaSetDocumentModified (doc);
1.206 kia 2717: }
1.217 vatton 2718: TtaFreeMemory (proposed);
2719: TtaFreeMemory (checked);
2720: TtaFreeMemory (types);
2721: createComp = FALSE;
1.209 vatton 2722: }
1.217 vatton 2723: break;
2724: default:
2725: // Do nothing
2726: break;
1.206 kia 2727: }
1.217 vatton 2728: }
2729: else if (selElem != selElem2 &&
2730: ((selType.ElSSchema == sshtml &&
2731: (selType.ElTypeNum == HTML_EL_List_Item ||
2732: selType.ElTypeNum == HTML_EL_Term ||
2733: selType.ElTypeNum == HTML_EL_Definition ||
2734: selType.ElTypeNum == HTML_EL_Option_item ||
2735: selType.ElTypeNum == HTML_EL_CAPTION ||
2736: selType.ElTypeNum == HTML_EL_Table_row ||
2737: selType.ElTypeNum == HTML_EL_Table_cell ||
2738: selType.ElTypeNum == HTML_EL_Data_cell ||
2739: selType.ElTypeNum == HTML_EL_Heading_cell ||
2740: selType.ElTypeNum == HTML_EL_thead ||
2741: selType.ElTypeNum == HTML_EL_tbody ||
2742: selType.ElTypeNum == HTML_EL_tfoot)) ||
2743: (selType2.ElSSchema == sshtml &&
2744: (selType2.ElTypeNum == HTML_EL_List_Item ||
2745: selType2.ElTypeNum == HTML_EL_Term ||
2746: selType2.ElTypeNum == HTML_EL_Definition ||
2747: selType2.ElTypeNum == HTML_EL_Option_item ||
2748: selType2.ElTypeNum == HTML_EL_CAPTION ||
2749: selType2.ElTypeNum == HTML_EL_Table_row ||
2750: selType2.ElTypeNum == HTML_EL_Table_cell ||
2751: selType2.ElTypeNum == HTML_EL_Data_cell ||
2752: selType2.ElTypeNum == HTML_EL_Heading_cell ||
2753: selType2.ElTypeNum == HTML_EL_thead ||
2754: selType2.ElTypeNum == HTML_EL_tbody ||
2755: selType2.ElTypeNum == HTML_EL_tfoot))))
2756: {
2757: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
2758: return NULL;
2759: }
2760: else if (selType.ElSSchema == sshtml &&
2761: selElem == selElem2 &&
2762: (selType.ElTypeNum == HTML_EL_Element ||
2763: selType.ElTypeNum == HTML_EL_Basic_Elem ||
2764: (selType.ElTypeNum == HTML_EL_Paragraph &&
2765: TtaGetElementVolume(selElem) == 0)))
2766: // Create a block-level xt:use instead of selection.
2767: use = Template_CreateEmptyBlockUse (doc);
2768: else
2769: {
1.218 vatton 2770: if (dispMode == DisplayImmediately)
2771: TtaSetDisplayMode (doc, DeferredDisplay);
1.217 vatton 2772: prev = selElem;
2773: TtaPreviousSibling(&prev);
2774: next = selElem2;
2775: TtaNextSibling (&next);
2776: if (selElem == selElem2 &&
2777: selType2.ElSSchema == sshtml &&
2778: (selType.ElTypeNum == HTML_EL_List_Item ||
2779: selType.ElTypeNum == HTML_EL_Term ||
2780: selType.ElTypeNum == HTML_EL_Definition ||
2781: selType.ElTypeNum == HTML_EL_Option_item ||
2782: selType.ElTypeNum == HTML_EL_CAPTION ||
2783: selType.ElTypeNum == HTML_EL_Table_row ||
2784: selType.ElTypeNum == HTML_EL_Table_cell ||
2785: selType.ElTypeNum == HTML_EL_Data_cell ||
2786: selType.ElTypeNum == HTML_EL_Heading_cell ||
2787: selType.ElTypeNum == HTML_EL_thead ||
2788: selType.ElTypeNum == HTML_EL_tbody ||
2789: selType.ElTypeNum == HTML_EL_tfoot))
1.206 kia 2790: {
1.217 vatton 2791: // special management
2792: createComp = FALSE;
1.218 vatton 2793: if (selType.ElTypeNum == HTML_EL_Table_cell ||
2794: selType.ElTypeNum == HTML_EL_Data_cell ||
2795: selType.ElTypeNum == HTML_EL_Heading_cell)
2796: // generate the use element around the first child
2797: // instead of the cell
2798: getContent = TRUE;
1.206 kia 2799: }
1.217 vatton 2800: if (createComp)
1.186 kia 2801: {
1.217 vatton 2802: // Create a component from selection and a use using it
2803: comp = Template_CreateComponentFromSelection(doc);
2804: if (comp)
1.197 kia 2805: {
1.217 vatton 2806: useType.ElSSchema = sstempl;
2807: useType.ElTypeNum = Template_EL_useSimple;
2808: use = TtaNewElement (doc, useType);
2809: if (prev)
2810: TtaInsertSibling (use, prev, FALSE, doc);
2811: else if (next)
2812: TtaInsertSibling (use, next, TRUE, doc);
2813: else
2814: TtaInsertFirstChild (&use, parent, doc);
2815: GiveAttributeStringValueFromNum (comp, Template_ATTR_name, buffer, &sz);
1.221 vatton 2816: // Declare the new component
2817: Template_DeclareNewComponent (t, buffer, comp, 2);
1.217 vatton 2818: SetAttributeStringValue (use, Template_ATTR_types, buffer);
2819: SetAttributeStringValue (use, Template_ATTR_currentType, buffer);
2820: SetAttributeStringValue (use, Template_ATTR_title, buffer);
2821: TtaRegisterElementCreate (use, doc);
2822: TtaSelectElement (doc, use);
2823: // register document modification
2824: TtaSetDocumentModified (doc);
1.197 kia 2825: }
1.217 vatton 2826: }
2827: else
2828: {
2829: title = TtaGetMessage (AMAYA, AM_TEMPLATE_USELABEL);
2830: label = TtaGetMessage (AMAYA, AM_NAME);
1.221 vatton 2831: QueryStringFromUser (label, title, buffer, 32);
1.220 vatton 2832: if (buffer[0] != EOS)
1.194 kia 2833: {
1.197 kia 2834: // Create a xt:use around the selection
1.194 kia 2835: useType.ElSSchema = sstempl;
1.197 kia 2836: useType.ElTypeNum = Template_EL_useSimple;
1.217 vatton 2837: use = TtaNewElement (doc, useType);
2838: if (getContent)
2839: {
2840: parent = selElem;
2841: selElem = TtaGetFirstChild (parent);
2842: selElem2 = TtaGetLastChild (parent);
2843: prev = next = NULL;
2844: }
2845: if (selElem)
2846: {
2847: name = TtaGetElementTypeName(TtaGetElementType(selElem));
2848: TtaRegisterElementDelete (selElem, doc);
2849: TtaRemoveTree(selElem, doc);
2850: TtaInsertFirstChild (&selElem, use, doc);
2851: }
2852: else
2853: {
2854: useType.ElTypeNum = Template_EL_TEXT_UNIT;
2855: selElem = TtaNewElement (doc, useType);
2856: TtaInsertFirstChild (&selElem, use, doc);
2857: }
1.209 vatton 2858: if (prev)
1.194 kia 2859: TtaInsertSibling(use, prev, FALSE, doc);
1.217 vatton 2860: else if (next)
2861: TtaInsertSibling(use, next,TRUE, doc);
1.194 kia 2862: else
2863: TtaInsertFirstChild(&use, parent, doc);
1.217 vatton 2864: if (name && strcmp (name, "???") &&
2865: strcmp (name, "Pseudo_paragraph"))
2866: {
2867: SetAttributeStringValue (use, Template_ATTR_types, name);
2868: SetAttributeStringValue (use, Template_ATTR_currentType, name);
2869: }
2870: else
2871: {
2872: SetAttributeStringValue (use, Template_ATTR_types, "string");
2873: SetAttributeStringValue (use, Template_ATTR_currentType, "string");
1.218 vatton 2874: }
1.221 vatton 2875: Template_SetLabel (doc, use, buffer, FALSE);
1.218 vatton 2876: TtaRegisterElementCreate (use, doc);
1.217 vatton 2877: TtaSelectElement (doc, use);
2878: // register document modification
2879: TtaSetDocumentModified (doc);
1.194 kia 2880: }
1.186 kia 2881: }
1.218 vatton 2882: if (dispMode == DisplayImmediately)
2883: TtaSetDisplayMode (doc, dispMode);
1.186 kia 2884: }
1.217 vatton 2885: }
2886: else if (lastChar < firstChar)
1.218 vatton 2887: use = Template_CreateInlineUse (doc);
2888: else
1.221 vatton 2889: Template_CreateTextBox (doc, createComp);
1.206 kia 2890:
1.217 vatton 2891: TtaSetStructureChecking (oldStructureChecking, doc);
2892: TtaCloseUndoSequence(doc);
1.186 kia 2893: }
1.187 kia 2894: return use;
2895: #else /* TEMPLATES */
2896: return NULL;
1.186 kia 2897: #endif /* TEMPLATES */
2898: }
2899:
2900: /*----------------------------------------------------------------------
1.187 kia 2901: Template_CreateComponentFromSelection
1.186 kia 2902: Create a xt:component with the selection and move it into the xt:head.
1.187 kia 2903: Return the xt:component element.
1.186 kia 2904: ----------------------------------------------------------------------*/
1.217 vatton 2905: Element Template_CreateComponentFromSelection (Document doc)
1.186 kia 2906: {
2907: #ifdef TEMPLATES
1.217 vatton 2908: ThotBool oldStructureChecking;
2909: DisplayMode dispMode;
2910: Element selElem, selElem2, parent, parent2, current, child, head;
2911: Element comp = NULL;
2912: ElementType selType, selType2, compType;
2913: SSchema sstempl = TtaGetSSchema ("Template", doc);
1.192 kia 2914: XTigerTemplate t = GetXTigerDocTemplate(doc);
1.217 vatton 2915: int firstChar, lastChar, firstChar2, lastChar2;
2916: char buffer[128];
2917: const char *title, *label;
2918: ThotBool closeUndo = FALSE;
1.186 kia 2919:
1.209 vatton 2920: if (doc && t && TtaGetDocumentAccessMode(doc) &&
1.186 kia 2921: TtaGetDocumentAccessMode(doc) && sstempl &&
2922: IsTemplateDocument(doc) && !IsTemplateInstanceDocument(doc))
2923: {
1.217 vatton 2924: TtaGiveFirstSelectedElement (doc, &selElem, &firstChar, &lastChar);
2925: if (selElem == NULL)
2926: {
2927: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_INVALID_SELECTION);
2928: return NULL;
2929: }
2930: TtaGiveLastSelectedElement (doc, &selElem2, &firstChar2, &lastChar2);
2931: selType = TtaGetElementType(selElem);
2932: selType2 = TtaGetElementType(selElem2);
2933: parent = TtaGetParent(selElem);
2934: parent2 = TtaGetParent(selElem2);
2935: label = TtaGetMessage (AMAYA, AM_NAME);
2936: title = TtaGetMessage (AMAYA, AM_TEMPLATE_NEWCOMP);
1.221 vatton 2937: QueryStringFromUser(label, title, buffer, 32);
1.217 vatton 2938: if (buffer[0] != EOS)
1.186 kia 2939: {
1.221 vatton 2940: head = TemplateFindHead (doc);
1.217 vatton 2941: compType.ElSSchema = sstempl;
2942: compType.ElTypeNum = Template_EL_component;
2943: if (head && firstChar == 0 && firstChar2 == 0 && parent == parent2)
1.186 kia 2944: {
1.217 vatton 2945: dispMode = TtaGetDisplayMode (doc);
2946: oldStructureChecking = TtaGetStructureChecking (doc);
2947: TtaSetStructureChecking (FALSE, doc);
1.221 vatton 2948: child = TtaGetLastChild (head);
1.217 vatton 2949: if (TtaHasUndoSequence (doc))
2950: closeUndo = FALSE;
2951: else
2952: {
2953: closeUndo = TRUE;
2954: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
2955: }
1.206 kia 2956:
1.220 vatton 2957: if (dispMode == DisplayImmediately)
2958: TtaSetDisplayMode (doc, DeferredDisplay);
1.217 vatton 2959: // Create the component
2960: comp = TtaNewElement(doc, compType);
2961: if (child)
2962: TtaInsertSibling(comp, child, FALSE, doc);
2963: else
2964: TtaInsertFirstChild (&comp, head, doc);
2965: // register and update the component name
1.221 vatton 2966: Template_SetName (doc, comp, buffer, FALSE);
1.217 vatton 2967:
2968: TtaNextSibling (&selElem2);
2969: child = NULL;
2970: while (selElem != selElem2)
1.180 kia 2971: {
1.217 vatton 2972: current = selElem;
2973: TtaNextSibling (&selElem);
2974: TtaRegisterElementDelete (current, doc);
2975: TtaRemoveTree (current, doc);
1.209 vatton 2976: if (child)
1.217 vatton 2977: TtaInsertSibling (current, child, FALSE, doc);
1.185 kia 2978: else
1.217 vatton 2979: TtaInsertFirstChild (¤t, comp, doc);
2980: //TtaRegisterElementCreate (current, doc);
2981: child = current;
1.180 kia 2982: }
1.217 vatton 2983: TtaRegisterElementCreate (comp, doc);
2984: if (closeUndo)
2985: TtaCloseUndoSequence(doc);
2986: TtaSetStructureChecking (oldStructureChecking, doc);
2987: if (dispMode == DisplayImmediately)
2988: TtaSetDisplayMode (doc, dispMode);
1.180 kia 2989: }
2990: }
2991: }
1.186 kia 2992: return comp;
1.187 kia 2993: #else /* TEMPLATES */
2994: return NULL;
1.206 kia 2995: #endif /* TEMPLATES */
1.180 kia 2996: }
1.187 kia 2997:
1.192 kia 2998: /*----------------------------------------------------------------------
2999: TemplateComponentWillBeDeleted
3000: Processed when a component element will be deleted in a template context.
3001: ----------------------------------------------------------------------*/
3002: ThotBool TemplateComponentWillBeDeleted (NotifyElement *event)
3003: {
3004: #ifdef TEMPLATES
3005: XTigerTemplate t = GetXTigerDocTemplate(event->document);
1.220 vatton 3006: char *elemName;
1.206 kia 3007:
1.220 vatton 3008: elemName = GetAttributeStringValueFromNum(event->element, Template_ATTR_name, NULL);
3009: if (Template_IsUsedComponent (t, event->document, elemName))
1.192 kia 3010: {
3011: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_TEMPLATE_USEDCOMP_CANTREMOVE);
3012: return TRUE;
3013: }
1.206 kia 3014: #endif /* TEMPLATES */
1.192 kia 3015: return FALSE;
3016: }
1.187 kia 3017:
1.200 kia 3018: /*----------------------------------------------------------------------
3019: UnionClicked
1.206 kia 3020: Called when a xt:union is clicked
1.200 kia 3021: ----------------------------------------------------------------------*/
3022: void UnionClicked(NotifyElement* event)
3023: {
1.209 vatton 3024: if (event->document && event->element)
1.221 vatton 3025: TtaSelectElement (event->document, event->element);
1.200 kia 3026: }
3027:
3028: /*----------------------------------------------------------------------
3029: UnionDoubleClicked
1.206 kia 3030: Called when a xt:union is double clicked
1.200 kia 3031: ----------------------------------------------------------------------*/
3032: ThotBool UnionDoubleClicked(NotifyElement* event)
3033: {
3034: Template_ModifyUnionElement(event->document, event->element);
3035: TtaSelectElement(event->document, event->element);
3036: return TRUE;
3037: }
3038:
3039: /*----------------------------------------------------------------------
3040: TemplateNameAttributeDeleted
1.206 kia 3041: Called when a xt:name will be deleted
1.200 kia 3042: ----------------------------------------------------------------------*/
3043: ThotBool TemplateNameAttributeDeleted(NotifyAttribute* event)
3044: {
3045: // Prevent xt:name deletion
3046: return TRUE;
3047: }
3048:
3049: /*----------------------------------------------------------------------
3050: TemplateNameAttributeModified
1.206 kia 3051: Called when a xt:name will be modified
1.200 kia 3052: ----------------------------------------------------------------------*/
3053: ThotBool TemplateNameAttributeModified(NotifyAttribute* event)
3054: {
3055: // Prevent xt:name modification
3056: return TRUE;
3057: }
3058:
3059: /*----------------------------------------------------------------------
3060: TemplateNameAttributeCreated
1.206 kia 3061: Called when a xt:name have been created
1.200 kia 3062: ----------------------------------------------------------------------*/
1.203 vatton 3063: void TemplateNameAttributeCreated(NotifyAttribute* event)
1.200 kia 3064: {
1.202 kia 3065: MakeUniqueName(event->element, event->document, TRUE, FALSE);
1.200 kia 3066: }
3067:
3068:
3069: /*----------------------------------------------------------------------
3070: TemplateNameAttrInMenu
3071: Called by Thot when building the Attributes menu for template elements.
3072: ----------------------------------------------------------------------*/
3073: ThotBool TemplateNameAttrInMenu (NotifyAttribute * event)
3074: {
3075: #ifdef TEMPLATES
3076: ElementType type = TtaGetElementType(event->element);
1.209 vatton 3077: if (type.ElTypeNum==Template_EL_component ||
1.200 kia 3078: type.ElTypeNum==Template_EL_union)
3079: return TRUE;
3080: #endif /* TEMPLATES */
1.209 vatton 3081: return FALSE;
1.200 kia 3082: }
3083:
Webmaster