Annotation of Amaya/amaya/templates.c, revision 1.45
1.1 cvs 1: /*
2: *
1.27 vatton 3: * COPYRIGHT INRIA and W3C, 1996-2005
1.1 cvs 4: * Please first read the full copyright statement in file COPYRIGHT.
5: *
6: */
1.14 cvs 7:
1.1 cvs 8: /*
9: * Amaya browser functions called form Thot and declared in HTML.A.
10: * These functions concern links and other HTML general features.
11: *
1.15 cvs 12: * Authors: I. Vatton, L. Bonameau, S. Bonhomme (INRIA)
1.1 cvs 13: *
14: */
15:
16: /* Included headerfiles */
1.29 tollenae 17:
18:
1.1 cvs 19: #define THOT_EXPORT extern
20: #include "amaya.h"
21: #include "css.h"
22: #include "document.h"
23: #include "view.h"
1.28 tollenae 24: #include "wxdialogapi_f.h"
25: #include "appdialogue_wx.h"
1.29 tollenae 26: #include "init_f.h"
1.32 tollenae 27: #include "parser.h"
1.29 tollenae 28: #include "tree.h"
29: #include "HTMLimage_f.h"
1.32 tollenae 30: #ifdef TEMPLATES
31: #include "Template.h"
32: #endif /* TEMPLATES */
33: #include "HTMLsave_f.h"
1.1 cvs 34:
35: /* content of the attr name of the meta tag defining the doc's destination URL */
1.18 cvs 36: #define META_TEMPLATE_NAME "AMAYA_TEMPLATE"
1.1 cvs 37: /* URL of the script providing templates (for reload) */
1.18 cvs 38: static char *script_URL;
1.3 cvs 39: #include "init_f.h"
1.43 tollenae 40: #include "css_f.h"
1.3 cvs 41:
42:
1.1 cvs 43: /*----------------------------------------------------------------------
1.34 tollenae 44: NewTemplate: Create the "new document from template's" dialog
1.1 cvs 45: ----------------------------------------------------------------------*/
1.18 cvs 46: void NewTemplate (Document doc, View view)
1.1 cvs 47: {
1.31 tollenae 48: #ifdef TEMPLATES
1.28 tollenae 49: char *templateDir ;
50:
51: templateDir = (char *) TtaGetMemory (MAX_LENGTH);
52:
53: // Amaya's templates directory (only french yet)
1.43 tollenae 54: sprintf ((char *)templateDir, "%s%ctemplates%cfr%c", TtaGetEnvString ("THOTDIR"),DIR_SEP,DIR_SEP,DIR_SEP);
1.28 tollenae 55:
56: char s [MAX_LENGTH];
57:
58: TtaExtractName (DocumentURLs[doc], s, DocumentName);
59: strcpy (DirectoryName, s);
60:
61: int window_id = TtaGetDocumentWindowId( doc, view );
62: ThotWindow p_window = (ThotWindow) TtaGetWindowFromId(window_id);
63:
64:
65: ThotBool created = CreateNewTemplateDocDlgWX(BaseDialog + OpenTemplate,
66: p_window,
67: doc,
1.31 tollenae 68: TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),
1.28 tollenae 69: templateDir,
70: s);
71:
72: if (created)
1.25 vatton 73: {
1.28 tollenae 74: TtaSetDialoguePosition ();
75: TtaShowDialogue (BaseDialog + OpenTemplate, TRUE);
1.25 vatton 76: }
1.28 tollenae 77:
1.31 tollenae 78: #endif // TEMPLATES
1.1 cvs 79: }
1.25 vatton 80:
1.28 tollenae 81:
82:
83:
1.1 cvs 84: /*----------------------------------------------------------------------
85: OpenTemplateDocument: Process the meta of a template document,
86: changes the URL, try to save it and warn the user if it cannot be
87: saved.
88: ----------------------------------------------------------------------*/
1.28 tollenae 89:
1.18 cvs 90: void OpenTemplateDocument (Document doc)
1.1 cvs 91: {
1.43 tollenae 92: #ifdef TEMPLATES
1.1 cvs 93: ElementType metaElType;
94: Element metaEl;
95: Attribute metaAttr;
96: AttributeType metaAttrType;
97: ThotBool found = FALSE;
98: int length;
1.24 vatton 99: char *utf8path, *path;
100: char *URLdir, *URLname;
1.1 cvs 101:
102: metaElType.ElSSchema = TtaGetDocumentSSchema (doc);
103: metaElType.ElTypeNum = HTML_EL_META;
104: metaAttrType.AttrSSchema = TtaGetDocumentSSchema (doc);
105: metaAttrType.AttrTypeNum = HTML_ATTR_meta_name;
106: /* search the meta element */
107: metaEl = TtaGetMainRoot (doc);
1.24 vatton 108: path = NULL;
1.1 cvs 109: do
110: {
111: metaEl = TtaSearchTypedElement (metaElType, SearchForward, metaEl);
112: if (metaEl != NULL)
113: {
114: metaAttr = TtaGetAttribute (metaEl, metaAttrType);
115: if (metaAttr != NULL)
116: {
1.24 vatton 117: length = TtaGetTextAttributeLength (metaAttr) + 1;
118: utf8path = (char *) TtaGetMemory (length);
119: TtaGiveTextAttributeValue (metaAttr, utf8path, &length);
120: path = (char *)TtaConvertMbsToByte ((unsigned char *)utf8path,
121: TtaGetDefaultCharset ());
122: TtaFreeMemory (utf8path);
123: found = !strcmp (path, META_TEMPLATE_NAME);
1.26 cvs 124: /* free previous path if necessary */
125: TtaFreeMemory (path);
126: path = NULL;
1.1 cvs 127: }
128: }
129: }
1.24 vatton 130: while (metaEl && !found);
131:
1.1 cvs 132: if (found)
133: {
134: /* get the url of the document */
135: metaAttrType.AttrTypeNum = HTML_ATTR_meta_content;
136: metaAttr = TtaGetAttribute (metaEl, metaAttrType);
137: if (metaAttr != NULL)
138: {
1.24 vatton 139: length = TtaGetTextAttributeLength (metaAttr) + 1;
140: utf8path = (char *) TtaGetMemory (length);
141: TtaGiveTextAttributeValue (metaAttr, utf8path, &length);
142: path = (char *)TtaConvertMbsToByte ((unsigned char *)utf8path,
143: TtaGetDefaultCharset ());
144: TtaFreeMemory (utf8path);
1.1 cvs 145: }
1.24 vatton 146:
1.1 cvs 147: /* Delete the meta element */
148: TtaDeleteTree (metaEl, doc);
1.24 vatton 149: if (script_URL == NULL)
150: script_URL = TtaStrdup (DocumentURLs[doc]);
151: TtaFreeMemory (DocumentURLs[doc]);
152: DocumentURLs[doc] = TtaStrdup (path);
153: DocumentMeta[doc]->method = CE_TEMPLATE;
154: AddURLInCombobox (DocumentURLs[doc], NULL, TRUE);
155: TtaSetTextZone (doc, 1, URL_list);
156: TtaSetDocumentUnmodified (doc);
157:
1.1 cvs 158: /* set the document name and dir */
1.24 vatton 159: URLname = strrchr (path, URL_SEP);
1.5 cvs 160: if (URLname)
161: {
162: URLname[0] = EOS;
163: URLname++;
1.24 vatton 164: URLdir = path;
1.5 cvs 165: TtaSetDocumentDirectory (doc, URLdir);
1.9 cvs 166: TtaSetDocumentName (doc, URLname);
1.5 cvs 167: /* SetBrowserEditor(doc); */
168: }
169: else
170: {
1.16 cvs 171: TtaSetDocumentDirectory (doc, "");
1.24 vatton 172: TtaSetDocumentName (doc, path);
1.5 cvs 173: }
1.9 cvs 174: SetWindowTitle (doc, doc, 0);
1.1 cvs 175: }
1.24 vatton 176: TtaFreeMemory (path);
1.43 tollenae 177: #endif /* TEMPLATES */
1.1 cvs 178: }
179:
180: /*----------------------------------------------------------------------
181: ReloadTemplateParams : restores the script URL and method into meta
182: to reload a template
183: ----------------------------------------------------------------------*/
1.18 cvs 184: void ReloadTemplateParams (char **docURL, ClickEvent *method)
1.1 cvs 185: {
186: *method = CE_FORM_GET;
1.5 cvs 187: TtaFreeMemory (*docURL);
1.17 cvs 188: *docURL = TtaStrdup (script_URL);
1.1 cvs 189: }
1.28 tollenae 190:
191:
192: /*------------------------------------------------------------------------
193: InitTemplateList : fills template list with HTML files in
194: the templates directory
195: ----------------------------------------------------------------------*/
196: void InitTemplateList ()
197: {
1.43 tollenae 198: #ifdef TEMPLATES
1.28 tollenae 199: int i, nb,len;
200: unsigned char *urlstring,c;
201: char *app_home;
202: FILE *file;
203: CHARSET encoding;
204:
205: TtaFreeMemory(Template_list);
206:
207: urlstring = (unsigned char *) TtaGetMemory (MAX_LENGTH);
208:
209: /* open the file list_url.dat into APP_HOME directory */
210: app_home = TtaGetEnvString ("APP_HOME");
211:
212: sprintf ((char *)urlstring, "%s%clist_url_utf8.dat", app_home, DIR_SEP);
213: encoding = UTF_8;
214:
215: file = TtaReadOpen ((char *)urlstring);
216:
217: *urlstring = EOS;
218: if (file)
219: {
220: /* get the size of the file */
221: fseek (file, 0L, 2);
222: /* end of the file */
1.30 gully 223: Template_list_len = (ftell (file) * 4) + GetMaxURLList() + 4;
1.28 tollenae 224: Template_list = (char *)TtaGetMemory (Template_list_len);
225: Template_list[0] = EOS;
226: fseek (file, 0L, 0); /* beginning of the file */
227: /* initialize the list by reading the file */
228: i = 0;
229: nb = 0;
230: while (TtaReadByte (file, &c))
1.43 tollenae 231: {
232: if (c == '"')
233: {
234: len = 0;
235: urlstring[len] = EOS;
236: while (len < MAX_LENGTH && TtaReadByte (file, &c) && c != EOL)
237: {
238: if (c == '"')
239: urlstring[len] = EOS;
240: else if (c == 13) /* Carriage return */
241: urlstring[len] = EOS;
242: else
243: urlstring[len++] = (char)c;
244: }
245: urlstring[len] = EOS;
246: if (i > 0 && len)
247: /* add an EOS between two urls */
248: URL_list[i++] = EOS;
249: if (len)
250: {
251: nb++;
252: strcpy ((char *)&Template_list[i], (char *)urlstring);
253: i += len;
254: }
255: }
256: }
1.28 tollenae 257: Template_list[i + 1] = EOS;
258: TtaReadClose (file);
259: }
260: TtaFreeMemory (urlstring);
1.43 tollenae 261: #endif /* TEMPLATES */
1.28 tollenae 262: }
1.37 tollenae 263:
1.36 tollenae 264: /*-------------------------------------------------
265: Insert the meta element identifying the template's
266: instance
267: ----------------------------------------------------*/
1.42 tollenae 268: void InsertInstanceMeta (Document newdoc)
1.36 tollenae 269: {
1.43 tollenae 270: #ifdef TEMPLATES
1.42 tollenae 271: Element head = TtaGetMainRoot (newdoc);
1.36 tollenae 272: Element meta;
273: ElementType elType;
274: AttributeType attrType;
275: Attribute attr;
1.42 tollenae 276: char *temp_id, *name;
277: int length;
278: ThotBool found = false;
1.36 tollenae 279:
280: elType.ElSSchema = TtaGetSSchema ("HTML", newdoc);
281: elType.ElTypeNum = HTML_EL_HEAD;
1.42 tollenae 282: head = TtaSearchTypedElement(elType, SearchInTree, head);
1.36 tollenae 283: if (head)
284: {
285: attrType.AttrTypeNum = HTML_ATTR_meta_name;
286: attrType.AttrSSchema = elType.ElSSchema;
1.42 tollenae 287:
288: elType.ElTypeNum = HTML_EL_META;
289: /* Search the template_id meta in the template document */
290: meta = TtaSearchTypedElement(elType, SearchInTree, head);
291: while (meta && !found)
292: {
293: attr = TtaGetAttribute (meta, attrType);
294: if (attr != NULL)
295: {
296: length = TtaGetTextAttributeLength (attr) + 1;
297: name = (char *) TtaGetMemory (length);
298: TtaGiveTextAttributeValue (attr, name, &length);
299: if (!strcmp (name, "template_id"))
300: {
301: /* The element is the template ID meta */
302: attrType.AttrTypeNum = HTML_ATTR_meta_content;
303: attr = TtaGetAttribute (meta, attrType);
304:
305: if (attr)
306: {
307: length = TtaGetTextAttributeLength (attr) + 1;
308: temp_id = (char *) TtaGetMemory (length);
309: TtaGiveTextAttributeValue (attr, temp_id, &length);
310: found = True;
311: }
312: TtaFreeMemory(name);
313: }
314: }
315: meta = TtaSearchTypedElement(elType, SearchForward, meta);
316: }
317: /* now "temp_id" should contain the content of template_id meta */
1.36 tollenae 318:
1.42 tollenae 319: if (found)
320: {
321: meta = TtaNewElement (newdoc, elType);
322:
323: /* Create the "name" attribute */
324:
325: attrType.AttrTypeNum = HTML_ATTR_meta_name;
326: attr = TtaNewAttribute (attrType);
327: TtaAttachAttribute (meta, attr, newdoc);
328: TtaSetAttributeText (attr, "template", meta, newdoc);
329:
330: /* Create the "content" attribute */
331: attrType.AttrTypeNum = HTML_ATTR_meta_content;
332: attr = TtaNewAttribute (attrType);
333: TtaAttachAttribute (meta, attr, newdoc);
334: TtaSetAttributeText (attr, temp_id, meta, newdoc);
335:
336: TtaInsertFirstChild (&meta, head, newdoc);
337: }
338: }
1.43 tollenae 339: #endif /*TEMPLATES*/
1.36 tollenae 340: }
1.29 tollenae 341:
1.32 tollenae 342: /*-----------------------------------------------
343: void UnlockSubtree
344: Set read/write access to an element and all his
345: children
346: -----------------------------------------------*/
1.29 tollenae 347:
1.32 tollenae 348: void UnlockSubtree (Document doc, Element el)
349: {
1.43 tollenae 350: #ifdef TEMPLATES
1.32 tollenae 351: TtaSetAccessRight (el, ReadWrite, doc);
352: TtaNextSibling (&el);
353: if (el != NULL)
354: /* The element has at least one sibling */
355: UnlockSubtree (doc, el);
1.43 tollenae 356: #endif /*TEMPLATES*/
1.32 tollenae 357: }
358:
1.39 tollenae 359: void UnlockContentElements (Document doc, Element el)
360: {
1.43 tollenae 361: #ifdef TEMPLATES
1.39 tollenae 362: ElementType elType;
363:
364: if (el != NULL)
365: {
366: elType = TtaGetElementType (el);
367: if (TtaIsLeaf (elType))
368: {
369: /* It's a content element */
370: TtaSetAccessRight(el, ReadWrite, doc);
371: }
372: else
373: {
374: TtaSetAccessRight(el, ReadOnly, doc);
375: UnlockContentElements(doc,TtaGetFirstChild(el));
376: }
377: TtaNextSibling(&el);
378: if (el != NULL)
379: {
380: UnlockContentElements(doc,el);
381: }
382: }
1.43 tollenae 383: #endif /*TEMPLATES*/
1.39 tollenae 384: }
385:
1.32 tollenae 386: /*-----------------------------------------------
387: void LockFixedAreas
388: Parse the subtree from el, set read-only access to
389: the element and his child when it's not a free_struct
390: element.
391: ------------------------------------------------*/
392:
393: void LockFixedAreas (Document doc, Element el)
394: {
395: #ifdef TEMPLATES
396: ElementType elType;
397: char *s;
398:
399: TtaSetAccessRight (el, ReadOnly, doc);
400: elType = TtaGetElementType(el);
401: s = TtaGetSSchemaName (elType.ElSSchema);
1.36 tollenae 402: if (TtaGetFirstChild(el)!=NULL)
1.32 tollenae 403: {
404: /* The element is not a leaf */
1.39 tollenae 405: if ((strcmp (s,"Template") == 0) &&
1.40 cvs 406: (elType.ElTypeNum == Template_EL_FREE_STRUCT))
1.39 tollenae 407: {
408: /* The element has a free structure */
1.45 ! tollenae 409: UnlockSubtree (doc, TtaGetFirstChild(el));
1.39 tollenae 410: }
411: else if ((strcmp (s,"Template") == 0) &&
1.40 cvs 412: (elType.ElTypeNum == Template_EL_FREE_CONTENT))
1.39 tollenae 413: {
414: /* The element has free content */
415: UnlockContentElements (doc, el);
416: }
417: else
418: {
419: /* The element has a fixed structure */
420: /* So we look for a free structure in
421: the subtree */
422: LockFixedAreas (doc, TtaGetFirstChild (el));
423: }
1.32 tollenae 424: }
425: TtaNextSibling (&el);
426: if (el != NULL)
427: /* The element has at least one sibling */
428: LockFixedAreas (doc, el);
429: #endif /* TEMPLATES */
430: }
1.29 tollenae 431:
432: /*---------------------------------------------------------------
1.32 tollenae 433: Load a template and create the instance file - update images and
1.29 tollenae 434: stylesheets related to the template.
435: ---------------------------------------------------------------*/
436:
437: int CreateInstanceOfTemplate (Document doc, char *templatename, char *docname,
1.32 tollenae 438: DocumentType docType)
1.29 tollenae 439: {
1.43 tollenae 440: #ifdef TEMPLATES
1.41 cvs 441: Element el;
1.29 tollenae 442: int newdoc, len;
443: ThotBool stopped_flag;
444:
445: W3Loading = doc;
446: BackupDocument = doc;
447: TtaExtractName (templatename, DirectoryName, DocumentName);
448: AddURLInCombobox (docname, NULL, TRUE);
1.42 tollenae 449: // doc = TtaInitDocument("HTML", templatename, 0);
1.29 tollenae 450: newdoc = InitDocAndView (doc,
1.33 tollenae 451: !DontReplaceOldDoc /* replaceOldDoc */,
1.42 tollenae 452: InNewWindow, /* inNewWindow */
1.29 tollenae 453: DocumentName, (DocumentType)docType, 0, FALSE,
1.42 tollenae 454: L_Other, (ClickEvent)CE_ABSOLUTE);
1.33 tollenae 455: if (newdoc != 0)
1.29 tollenae 456: {
457: LoadDocument (newdoc, templatename, NULL, NULL, CE_ABSOLUTE,
1.33 tollenae 458: "", DocumentName, NULL, FALSE, &DontReplaceOldDoc);
1.42 tollenae 459:
460: InsertInstanceMeta(newdoc);
1.33 tollenae 461:
1.32 tollenae 462: /* Update URLs of linked documents */
463: SetRelativeURLs (newdoc, docname);
1.33 tollenae 464:
1.29 tollenae 465: /* change its URL */
466: TtaFreeMemory (DocumentURLs[newdoc]);
467: len = strlen (docname) + 1;
468: DocumentURLs[newdoc] = TtaStrdup (docname);
469: DocumentSource[newdoc] = 0;
470: /* add the URI in the combobox string */
471: AddURLInCombobox (docname, NULL, FALSE);
472: TtaSetTextZone (newdoc, 1, URL_list);
473: /* change its directory name */
474: TtaSetDocumentDirectory (newdoc, DirectoryName);
475:
476: TtaSetDocumentModified (newdoc);
477: W3Loading = 0; /* loading is complete now */
478: DocNetworkStatus[newdoc] = AMAYA_NET_ACTIVE;
479: stopped_flag = FetchAndDisplayImages (newdoc, AMAYA_LOAD_IMAGE, NULL);
480: if (!stopped_flag)
1.32 tollenae 481: {
482: DocNetworkStatus[newdoc] = AMAYA_NET_INACTIVE;
483: /* almost one file is restored */
484: TtaSetStatus (newdoc, 1, TtaGetMessage (AMAYA, AM_DOCUMENT_LOADED),
485: NULL);
486: }
1.29 tollenae 487: /* check parsing errors */
488: CheckParsingErrors (newdoc);
489:
1.38 tollenae 490: /* Set elements access rights
491: according to free_* elements */
1.41 cvs 492: el = TtaGetMainRoot (newdoc);
1.38 tollenae 493: LockFixedAreas (newdoc, el);
494:
1.42 tollenae 495:
1.29 tollenae 496: }
1.33 tollenae 497: BackupDocument = 0;
498: return (newdoc);
1.43 tollenae 499: #endif /* TEMPLATES */
1.29 tollenae 500: }
501:
1.42 tollenae 502: /*---------------------------------------------------------------
503: Here we put all the actions to performs when we know a document
504: is an instance
505: ---------------------------------------------------------------*/
506:
1.37 tollenae 507: void LoadInstanceOfTemplate (Document doc)
508: {
1.43 tollenae 509: #ifdef TEMPLATES
510: ThotBool show;
511: Element el = TtaGetMainRoot (doc);
512: char *css_url;
513: PInfoPtr pInfo;
514: CSSInfoPtr css;
515:
1.37 tollenae 516: LockFixedAreas (doc, el);
1.43 tollenae 517: TtaGetEnvBoolean ("SHOW_TEMPLATES", &show);
518: css_url = (char *) TtaGetMemory (MAX_LENGTH);
519: sprintf (css_url, "%s%camaya%chide_template.css", TtaGetEnvString ("THOTDIR"),DIR_SEP,DIR_SEP);
520: if (!show)
521: {
522: /* We don't want to show the frames around template's elements,
523: so we load a stylesheet with border: none for all template's
524: elements */
525:
526: LoadStyleSheet (css_url, doc, NULL, NULL, NULL, CSS_ALL, FALSE);
527: }
528: else
529: {
530: css = SearchCSS (doc, css_url, NULL, &pInfo);
531: if (css)
532: RemoveStyle (css_url, doc, TRUE, TRUE, NULL, CSS_EXTERNAL_STYLE);
533: }
534: TtaFreeMemory(css_url);
535: #endif /* TEMPLATES */
1.37 tollenae 536: }
1.42 tollenae 537:
538:
539: /*---------------------------------------------------------------
540: ThotBool isTemplateInstance (Document doc)
541: Return true if the document is an instance
542: ---------------------------------------------------------------*/
543:
544: ThotBool IsTemplateInstance (Document doc)
545: {
1.43 tollenae 546: #ifdef TEMPLATES
1.42 tollenae 547: Element el = TtaGetMainRoot (doc);
548: Element meta;
549: ElementType metaType;
550: AttributeType attrType;
551: Attribute attr;
552: ThotBool found = false;
553: char* name,* version;
554: int length;
555:
556: /* metaType is first used to find a meta element */
557: metaType.ElSSchema = TtaGetSSchema ("HTML", doc);
558: metaType.ElTypeNum = HTML_EL_META;
559: meta = TtaSearchTypedElement(metaType, SearchInTree, el);
560:
561: /* attrType will be used to find the name attribute of a meta */
562: attrType.AttrSSchema = metaType.ElSSchema;
563: attrType.AttrTypeNum = HTML_ATTR_meta_name;
564: while (meta && !found)
565: {
566: attr = TtaGetAttribute (meta, attrType);
567: if (attr != NULL)
568: /* The element contains a name attribute */
569: {
570: length = TtaGetTextAttributeLength (attr) + 1;
571: name = (char *) TtaGetMemory (length);
572: TtaGiveTextAttributeValue (attr, name, &length);
573:
574: if (!strcmp(name, "template"))
575: {
576: /* The element is the template meta, the document is a template instance */
577: attrType.AttrTypeNum = HTML_ATTR_meta_content;
578: attr = TtaGetAttribute (meta, attrType);
579:
580: if (attr)
581: {
582: length = TtaGetTextAttributeLength (attr) + 1;
583: version = (char *) TtaGetMemory (length);
584: TtaGiveTextAttributeValue (attr, version, &length);
585:
586: DocumentMeta[doc]->template_version = version;
587: TtaFreeMemory (version);
588: found = True;
589: }
590: }
591: TtaFreeMemory(name);
592: }
593: meta = TtaSearchTypedElement(metaType, SearchForward, meta);
594: }
595:
596: return found;
1.43 tollenae 597: #else /* TEMPLATES */
1.44 cvs 598: return FALSE;
1.43 tollenae 599: #endif /* TEMPLATES */
1.42 tollenae 600: }
Webmaster