Annotation of Amaya/amaya/HTMLbook.c, revision 1.164
1.1 cvs 1: /*
2: *
1.161 quint 3: * (c) COPYRIGHT INRIA and W3C, 1996-2009
1.1 cvs 4: * Please first read the full copyright statement in file COPYRIGHT.
5: *
6: */
1.161 quint 7:
1.1 cvs 8: /*
9: * Initialization functions and button functions of Amaya application.
10: *
11: * Authors: V. Quint, I. Vatton
1.85 cvs 12: * R. Guetari (W3C/INRIA) - Windows version.
1.1 cvs 13: */
14:
1.107 carcone 15: #ifdef _WX
1.135 vatton 16: #include "wx/wx.h"
1.107 carcone 17: #endif /* _WX */
1.1 cvs 18:
19: /* Included headerfiles */
1.101 gully 20: #define THOT_EXPORT extern
1.1 cvs 21: #include "amaya.h"
1.44 cvs 22: #include "AHTURLTools_f.h"
1.1 cvs 23: #include "print.h"
1.51 cvs 24: #include "css.h"
1.101 gully 25: #include "init_f.h"
1.1 cvs 26:
1.116 gully 27: #include "appdialogue_wx.h"
28: #include "message_wx.h"
29:
1.25 cvs 30: /* structure to register sub-documents in MakeBook function*/
31: typedef struct _SubDoc
1.135 vatton 32: {
33: struct _SubDoc *SDnext;
34: Element SDel;
35: char *SDname;
36: }SubDoc;
1.25 cvs 37:
1.29 cvs 38: /* the structure used for the GetIncludedDocuments_callback function */
1.58 cvs 39: typedef struct _IncludeCtxt
1.53 cvs 40: {
1.58 cvs 41: Element div; /* enclosing element for the search */
42: Element link; /* current processed link */
1.104 vatton 43: char *utf8path; /* called url */
1.85 cvs 44: char *name; /* the fragment name */
1.58 cvs 45: struct _IncludeCtxt *ctxt; /* the previous context */
46: } IncludeCtxt;
1.29 cvs 47:
1.55 cvs 48: /* shared with windialogapi.c */
49: ThotBool PrintURL;
50: ThotBool NumberLinks;
51: ThotBool WithToC;
52: ThotBool IgnoreCSS;
53:
1.139 vatton 54: static struct _SubDoc *SubDocs = NULL;
1.85 cvs 55: static char PSfile[MAX_PATH];
56: static char PPrinter[MAX_PATH];
57: static char *DocPrintURL;
1.161 quint 58: static Document DocPrint;
1.2 cvs 59: static int PaperPrint;
1.73 cvs 60: static int ManualFeed = PP_OFF;
1.71 cvs 61: static int Orientation;
1.2 cvs 62: static int PageSize;
1.71 cvs 63: static int PagePerSheet;
1.1 cvs 64:
1.153 carcone 65: #include "MENUconf.h"
1.114 vatton 66: #include "EDITORactions_f.h"
1.1 cvs 67: #include "init_f.h"
68: #include "HTMLactions_f.h"
69: #include "HTMLbook_f.h"
70: #include "HTMLedit_f.h"
1.25 cvs 71: #include "HTMLhistory_f.h"
1.51 cvs 72: #include "UIcss_f.h"
1.1 cvs 73:
1.107 carcone 74: #ifdef _WINGUI
1.37 cvs 75: #include "wininclude.h"
1.103 cvs 76: #endif /* _WINGUI */
1.31 cvs 77:
1.107 carcone 78: #ifdef _WX
1.135 vatton 79: #include "wxdialogapi_f.h"
1.107 carcone 80: #endif /* _WX */
81:
1.101 gully 82: static ThotBool GetIncludedDocuments (Element el, Element link,
1.135 vatton 83: Document doc, IncludeCtxt *prev);
1.1 cvs 84: /*----------------------------------------------------------------------
1.94 vatton 85: RedisplayDocument redisplays a view of a document
86: ----------------------------------------------------------------------*/
87: void RedisplayDocument(Document doc, View view)
88: {
1.95 vatton 89: Element el;
90: int position;
91: int distance;
92:
93: /* get the current position in the document */
94: position = RelativePosition (doc, &distance);
1.94 vatton 95: TtaSetDisplayMode (doc, NoComputedDisplay);
96: TtaSetDisplayMode (doc, DisplayImmediately);
1.95 vatton 97: /* show the document at the same position */
98: el = ElementAtPosition (doc, position);
99: TtaShowElement (doc, view, el, distance);
1.94 vatton 100: }
101:
102: /*----------------------------------------------------------------------
1.16 cvs 103: RegisterSubDoc adds a new entry in SubDoc table.
104: ----------------------------------------------------------------------*/
1.94 vatton 105: static void RegisterSubDoc (Element el, char *url)
1.16 cvs 106: {
107: struct _SubDoc *entry, *last;
108:
109: if (url == NULL || url[0] == EOS)
110: return;
111:
1.101 gully 112: entry = (struct _SubDoc *)TtaGetMemory (sizeof (struct _SubDoc));
1.16 cvs 113: entry->SDnext = NULL;
114: entry->SDel = el;
1.83 cvs 115: entry->SDname = TtaStrdup (url);
1.16 cvs 116:
117: if (SubDocs == NULL)
118: SubDocs = entry;
119: else
120: {
121: last = SubDocs;
122: while (last->SDnext != NULL)
1.135 vatton 123: last = last->SDnext;
1.16 cvs 124: last->SDnext = entry;
125: }
126: }
127:
128: /*----------------------------------------------------------------------
129: SearchSubDoc searches whether a document name is registered or not
130: within the SubDoc table.
131: Return the DIV element that correspond to the sub-document or NULL.
132: ----------------------------------------------------------------------*/
1.94 vatton 133: static Element SearchSubDoc (char *url)
1.16 cvs 134: {
135: Element el;
136: struct _SubDoc *entry;
1.47 cvs 137: ThotBool docFound;
1.16 cvs 138:
139: if (url == NULL || url[0] == EOS)
140: return (NULL);
141:
142: entry = SubDocs;
143: docFound = FALSE;
144: el = NULL;
145: while (!docFound && entry != NULL)
146: {
1.83 cvs 147: docFound = (strcmp (url, entry->SDname) == 0);
1.16 cvs 148: if (!docFound)
1.135 vatton 149: entry = entry->SDnext;
1.16 cvs 150: else
1.135 vatton 151: /* document found -> return the DIV element */
152: el = entry->SDel;
1.16 cvs 153: }
154: return (el);
155: }
156:
157: /*----------------------------------------------------------------------
158: FreeSubDocTable frees all entries in SubDoc table.
159: ----------------------------------------------------------------------*/
1.94 vatton 160: static void FreeSubDocTable ()
1.16 cvs 161: {
162: struct _SubDoc *entry, *last;
163:
164: entry = SubDocs;
165: while (entry != NULL)
166: {
167: last = entry;
168: entry = entry->SDnext;
169: TtaFreeMemory (last->SDname);
170: TtaFreeMemory (last);
171: }
172: SubDocs = NULL;
173: }
174:
175: /*----------------------------------------------------------------------
1.1 cvs 176: SetInternalLinks
1.8 cvs 177: Associate an InternalLink attribute with all anchor (A) elements of the
178: document which designate an element in the same document.
1.1 cvs 179: InternalLink is a Thot reference attribute that links a source and a
180: target anchor and that allows P schemas to display and print cross-references
181: ----------------------------------------------------------------------*/
1.94 vatton 182: void SetInternalLinks (Document document)
1.1 cvs 183: {
1.32 cvs 184: Element el, div, link, target, sibling;
185: ElementType elType, linkType;
1.16 cvs 186: Attribute HrefAttr, IntLinkAttr;
1.17 cvs 187: Attribute attr, ExtLinkAttr;
1.16 cvs 188: AttributeType attrType;
1.85 cvs 189: char *text, *ptr, *url;
1.83 cvs 190: char number[10];
191: char value[MAX_LENGTH];
1.25 cvs 192: int length, i, volume;
193: int status, position;
1.47 cvs 194: ThotBool split;
1.1 cvs 195:
1.16 cvs 196: /* Remember the current status of the document */
197: status = TtaIsDocumentModified (document);
1.32 cvs 198: el = TtaGetMainRoot (document);
199: volume = TtaGetElementVolume (el);
200: elType = TtaGetElementType (el);
201: elType.ElTypeNum = HTML_EL_AnyLink;
1.16 cvs 202: attrType.AttrSSchema = elType.ElSSchema;
1.32 cvs 203: /* looks for all links in the document */
1.16 cvs 204: link = el;
205: while (link != NULL)
206: {
1.25 cvs 207: /* display the progression of the work */
208: el = link;
209: position = 0;
210: while (el != NULL)
1.135 vatton 211: {
212: sibling = el;
213: do
214: {
215: /* add volume of each previous element */
216: TtaPreviousSibling (&sibling);
217: if (sibling != NULL)
218: position += TtaGetElementVolume (sibling);
219: }
220: while (sibling != NULL);
221: el = TtaGetParent (el);
222: }
1.83 cvs 223: sprintf (number, "%d", position*100/volume);
1.25 cvs 224: TtaSetStatus (document, 1, TtaGetMessage (AMAYA, AM_UPDATED_LINK), number);
225: TtaHandlePendingEvents ();
1.16 cvs 226: link = TtaSearchTypedElement (elType, SearchForward, link);
227: if (link != NULL)
1.135 vatton 228: /* a link has been found */
229: {
230: linkType = TtaGetElementType (link);
231: if (linkType.ElTypeNum == HTML_EL_Anchor)
232: attrType.AttrTypeNum = HTML_ATTR_HREF_;
233: else
234: attrType.AttrTypeNum = HTML_ATTR_cite;
235: HrefAttr = TtaGetAttribute (link, attrType);
236: attrType.AttrTypeNum = HTML_ATTR_InternalLink;
237: IntLinkAttr = TtaGetAttribute (link, attrType);
238: attrType.AttrTypeNum = HTML_ATTR_ExternalLink;
239: ExtLinkAttr = TtaGetAttribute (link, attrType);
240: if (HrefAttr == NULL)
241: /* this element is not a link (no href or cite attribute) */
242: /* remove attributes InternalLink and ExternalLink if they
243: are present */
244: {
245: if (IntLinkAttr != NULL)
246: TtaRemoveAttribute (link, IntLinkAttr, document);
247: if (ExtLinkAttr != NULL)
248: TtaRemoveAttribute (link, ExtLinkAttr, document);
249: }
250: else
251: /* this element has an HREF or cite attribute */
252: {
253: length = TtaGetTextAttributeLength (HrefAttr);
254: text = (char *)TtaGetMemory (length + 1);
255: TtaGiveTextAttributeValue (HrefAttr, text, &length);
256:
257: /* does an external link become an internal link ? */
258: if (document == DocBook && SubDocs != NULL)
259: {
260: ptr = strrchr (text, '#');
261: url = text;
262: split = FALSE;
263: if (ptr == text)
264: /* a local link */
265: url = NULL;
266: else if (ptr != NULL)
267: {
268: /* split url and name part */
269: ptr[0] = EOS;
270: split = TRUE;
271: }
272:
273: /* Is it a sub-document */
274: div = SearchSubDoc (url);
275: if (split)
276: /* retore the mark */
277: ptr[0] = '#';
278:
279: if (div == NULL)
280: {
281: /* it's not a sub-document */
282: if (url == NULL)
283: /* a local link */
284: ptr = &text[1];
285: else
286: /* still an externa; link */
287: ptr = NULL;
288: }
289: else
290: {
291: /* this link becomes internal */
292: if (ptr != NULL)
293: {
294: /* get the target name */
295: strcpy (value, ptr);
296: length = strlen (value);
297: /* check whether the name changed */
298: i = 0;
299: target = SearchNAMEattribute (document, &value[1], NULL, NULL);
300: while (target != NULL)
301: {
302: /* is it the right NAME */
303: if (TtaIsAncestor (target, div))
304: target = NULL;
305: else
306: {
307: /* continue the search */
308: i++;
309: sprintf (&value[length], "%d", i);
310: target = SearchNAMEattribute (document,
311: &value[1], NULL, NULL);
312: }
313: }
314: }
315: else
316: {
317: /* get the DIV name */
318: attrType.AttrTypeNum = HTML_ATTR_ID;
319: attr = TtaGetAttribute (div, attrType);
320: length = 200;
321: value[0] = '#';
322: TtaGiveTextAttributeValue (attr, &value[1], &length);
323: }
324: ptr = &value[1];
325: TtaSetAttributeText (HrefAttr, value, link, document);
326: }
327: }
328: else if (text[0] == '#')
329: ptr = &text[1];
330: else
331: ptr = NULL;
332:
333: if (ptr != NULL)
334: /* it's an internal link. Attach an attribute InternalLink */
335: /* to the link, if this attribute does not exist yet */
336: {
337: if (IntLinkAttr == NULL)
338: {
339: attrType.AttrTypeNum = HTML_ATTR_InternalLink;
340: IntLinkAttr = TtaNewAttribute (attrType);
341: TtaAttachAttribute (link, IntLinkAttr, document);
342: }
343: /* looks for the target element */
344: target = SearchNAMEattribute (document, ptr, NULL, NULL);
345: if (target != NULL)
346: /* set the Thot link */
347: TtaSetAttributeReference (IntLinkAttr, link, document,
348: target);
349: }
350: else
351: /* it's an external link */
352: {
353: /* Remove the InternalLink attribute if it is present */
354: if (IntLinkAttr != NULL)
355: TtaRemoveAttribute (link, IntLinkAttr, document);
356: /* create an ExternalLink attribute if there is none */
357: if (ExtLinkAttr == NULL)
358: {
359: attrType.AttrTypeNum = HTML_ATTR_ExternalLink;
360: ExtLinkAttr = TtaNewAttribute (attrType);
361: TtaAttachAttribute (link, ExtLinkAttr, document);
362: }
363: }
364: TtaFreeMemory (text);
365: }
366: }
1.16 cvs 367: }
368: /* Reset document status */
369: if (!status)
370: TtaSetDocumentUnmodified (document);
1.3 cvs 371: }
372:
373: /*----------------------------------------------------------------------
374: CheckPrintingDocument reinitialize printing parameters as soon as
375: the printing document changes.
376: ----------------------------------------------------------------------*/
1.89 vatton 377: static void CheckPrintingDocument (Document document)
1.3 cvs 378: {
1.83 cvs 379: char docName[MAX_LENGTH];
380: char *ptr;
381: char suffix[MAX_LENGTH];
1.81 cvs 382: int lg;
383:
384: if (DocPrint != document || DocPrintURL == NULL ||
1.83 cvs 385: strcmp(DocPrintURL, DocumentURLs[document]))
1.81 cvs 386: {
387: /* initialize print parameters */
388: TtaFreeMemory (DocPrintURL);
389: DocPrint = document;
390: DocPrintURL = TtaStrdup (DocumentURLs[document]);
391:
392: /* define the new default PS file */
393: ptr = TtaGetEnvString ("APP_TMPDIR");
394: if (ptr != NULL && TtaCheckDirectory (ptr))
1.135 vatton 395: strcpy (PSfile, ptr);
1.81 cvs 396: else
1.135 vatton 397: strcpy (PSfile, TtaGetDefEnvString ("APP_TMPDIR"));
1.83 cvs 398: lg = strlen (PSfile);
399: if (PSfile[lg - 1] == DIR_SEP)
1.135 vatton 400: PSfile[--lg] = EOS;
1.83 cvs 401: strcpy (docName, TtaGetDocumentName (document));
1.90 vatton 402: TtaExtractSuffix (docName, suffix);
1.83 cvs 403: sprintf (&PSfile[lg], "%c%s.ps", DIR_SEP, docName);
1.81 cvs 404: TtaSetPsFile (PSfile);
405: }
1.3 cvs 406: }
407:
408: /*----------------------------------------------------------------------
1.135 vatton 409: PrintDocument prints the document using predefined parameters.
410: ----------------------------------------------------------------------*/
1.91 vatton 411: static void PrintDocument (Document doc, View view)
1.3 cvs 412: {
1.126 cvs 413: #if defined(_WINDOWS) && defined(_WX) && defined(IV)
1.116 gully 414: /* On windows and with wxWidgets, disable printing for the moment */
415: wxMessageDialog messagedialog( NULL,
1.135 vatton 416: TtaConvMessageToWX("Not implemented yet"),
417: _T("Warning"),
418: (long) wxOK | wxICON_EXCLAMATION | wxSTAY_ON_TOP);
1.116 gully 419: messagedialog.ShowModal();
420: #else /* defined(_WINDOWS) && defined(_WX) */
1.34 cvs 421: AttributeType attrType;
1.84 cvs 422: ElementType elType;
1.34 cvs 423: Attribute attr;
1.84 cvs 424: Element el, docEl;
1.85 cvs 425: char *files, *dir;
1.84 cvs 426: char viewsToPrint[MAX_PATH];
1.47 cvs 427: ThotBool status, textFile;
1.3 cvs 428:
1.38 cvs 429: textFile = (DocumentTypes[doc] == docText ||
1.75 cvs 430: DocumentTypes[doc] == docSource ||
1.135 vatton 431: DocumentTypes[doc] == docCSS);
1.38 cvs 432:
1.81 cvs 433: /* initialize printing information */
1.75 cvs 434: CheckPrintingDocument (doc);
1.83 cvs 435: strcpy (viewsToPrint, "Formatted_view ");
1.75 cvs 436: if (DocumentTypes[doc] == docHTML && WithToC)
1.83 cvs 437: strcat (viewsToPrint, "Table_of_contents ");
1.75 cvs 438:
439: if (textFile)
440: {
441: if (PageSize == PP_A4)
1.135 vatton 442: {
443: if (Orientation == PP_Landscape)
444: TtaSetPrintSchema ("TextFilePL");
445: else
446: TtaSetPrintSchema ("TextFilePP");
447: }
1.75 cvs 448: else
1.135 vatton 449: {
450: if (Orientation == PP_Landscape)
451: TtaSetPrintSchema ("TextFileUSL");
452: else
453: TtaSetPrintSchema ("TextFilePPUS");
454: }
1.75 cvs 455: }
456: else if (DocumentTypes[doc] == docSVG)
1.88 vatton 457: TtaSetPrintSchema ("SVGP");
1.75 cvs 458: else if (DocumentTypes[doc] == docMath)
1.82 cvs 459: TtaSetPrintSchema ("MathMLP");
1.75 cvs 460: else if (DocumentTypes[doc] == docAnnot)
1.82 cvs 461: TtaSetPrintSchema ("AnnotP");
1.113 quint 462: else if (DocumentTypes[doc] == docHTML)
1.75 cvs 463: {
1.113 quint 464: if (NumberLinks)
1.135 vatton 465: /* display numbered links */
466: {
467: /* associate an attribute InternalLink with all anchors refering
468: a target in the same document. This allows P schemas to work
469: properly */
470: SetInternalLinks (DocPrint);
471: if (PageSize == PP_A4)
472: {
473: if (Orientation == PP_Landscape)
474: TtaSetPrintSchema ("HTMLPLL");
475: else
476: TtaSetPrintSchema ("HTMLPLP");
477: }
478: else
479: {
480: if (Orientation == PP_Landscape)
481: TtaSetPrintSchema ("HTMLUSLL");
482: else
483: TtaSetPrintSchema ("HTMLPLPUS");
484: }
485: strcat (viewsToPrint, "Links_view ");
486: }
1.113 quint 487: else if (PageSize == PP_A4)
1.135 vatton 488: {
489: if (Orientation == PP_Landscape)
490: TtaSetPrintSchema ("HTMLPL");
491: else
492: TtaSetPrintSchema ("HTMLPP");
493: }
1.75 cvs 494: else
1.135 vatton 495: {
496: if (Orientation == PP_Landscape)
497: TtaSetPrintSchema ("HTMLUSL");
498: else
499: TtaSetPrintSchema ("HTMLPPUS");
500: }
1.75 cvs 501: }
502: status = TtaIsDocumentModified (doc);
1.71 cvs 503:
1.75 cvs 504: if (textFile || DocumentTypes[doc] == docImage ||
505: DocumentTypes[doc] == docHTML)
506: {
507: /* post or remove the PrintURL attribute */
508: attrType.AttrSSchema = TtaGetDocumentSSchema (doc);
1.84 cvs 509: elType.ElSSchema = attrType.AttrSSchema;
1.75 cvs 510: if (textFile)
1.135 vatton 511: {
512: elType. ElTypeNum = TextFile_EL_TextFile;
513: attrType.AttrTypeNum = TextFile_ATTR_PrintURL;
514: }
1.75 cvs 515: else
1.135 vatton 516: {
517: elType. ElTypeNum = HTML_EL_HTML;
518: attrType.AttrTypeNum = HTML_ATTR_PrintURL;
519: }
1.84 cvs 520: docEl = TtaGetMainRoot (doc);
521: el = TtaSearchTypedElement (elType, SearchForward, docEl);
1.75 cvs 522: attr = TtaGetAttribute (el, attrType);
523: if (!attr && PrintURL)
1.135 vatton 524: {
525: attr = TtaNewAttribute (attrType);
526: TtaAttachAttribute (el, attr, doc);
527: }
1.75 cvs 528: if (attr && !PrintURL)
1.135 vatton 529: TtaRemoveAttribute (el, attr, doc);
1.75 cvs 530: }
531:
532: /* get the path dir where css files have to be stored */
1.113 quint 533: if ((DocumentTypes[doc] == docHTML || DocumentTypes[doc] == docSVG ||
534: DocumentTypes[doc] == docXml) &&
1.78 cvs 535: !IgnoreCSS)
1.75 cvs 536: {
537: TtaGetPrintNames (&files, &dir);
538: /* store css files and get the list of names */
539: files = CssToPrint (doc, dir);
540: }
541: else
542: files = NULL;
543: TtaPrint (DocPrint, viewsToPrint, files);
544: if (files)
545: TtaFreeMemory (files);
546: if (!status)
547: TtaSetDocumentUnmodified (doc);
1.116 gully 548: #endif /* defined(_WINDOWS) && defined(_WX) */
1.1 cvs 549: }
550:
1.45 cvs 551: /*----------------------------------------------------------------------
1.135 vatton 552: PrintAs prints the document using predefined parameters.
553: ----------------------------------------------------------------------*/
1.89 vatton 554: void PrintAs (Document doc, View view)
1.45 cvs 555: {
1.103 cvs 556: #ifdef _WINGUI
1.72 cvs 557: DocPrint = doc;
558: ReusePrinterDC ();
1.103 cvs 559: #else /* _WINGUI */
1.45 cvs 560: PrintDocument (doc, view);
1.103 cvs 561: #endif /* _WINGUI */
1.45 cvs 562: }
1.1 cvs 563:
564: /*----------------------------------------------------------------------
1.135 vatton 565: CallbackImage handle return of Print form.
1.1 cvs 566: ----------------------------------------------------------------------*/
1.85 cvs 567: void CallbackPrint (int ref, int typedata, char *data)
1.1 cvs 568: {
1.133 cvs 569: long int val;
1.1 cvs 570:
1.133 cvs 571: val = (long int) data;
1.53 cvs 572: switch (ref - BasePrint)
1.1 cvs 573: {
1.69 cvs 574: case FormPrint:
575: TtaDestroyDialogue (BasePrint + FormPrint);
1.1 cvs 576: switch (val)
1.135 vatton 577: {
578: case 1:
579: TtaSetPrintCommand (PPrinter);
580: TtaSetPsFile (PSfile);
581: /* update the environment variable */
582: TtaSetEnvString ("THOTPRINT", PPrinter, TRUE);
583: TtaSaveAppRegistry ();
584: PrintDocument (DocPrint, 1);
585: break;
586: default:
587: break;
588: }
1.1 cvs 589: break;
1.69 cvs 590: case PrintOptions:
1.1 cvs 591: switch (val)
1.135 vatton 592: {
593: case 0:
594: /* Manual feed option */
595: if (ManualFeed == PP_ON)
596: ManualFeed = PP_OFF;
597: else
598: ManualFeed = PP_ON;
599: TtaSetPrintParameter (PP_ManualFeed, ManualFeed);
600: break;
601: case 1:
602: /* Toc option */
603: WithToC = !WithToC;
604: break;
605: case 2:
606: /* NumberLinks option */
607: NumberLinks = !NumberLinks;
608: case 3:
609: /* URL option */
610: PrintURL = !PrintURL;
611: break;
612: case 4:
613: /* CSS option */
614: IgnoreCSS = !IgnoreCSS;
615: break;
616: }
1.1 cvs 617: break;
1.69 cvs 618: case PaperFormat:
1.1 cvs 619: /* page size submenu */
620: switch (val)
1.135 vatton 621: {
622: case 0:
623: PageSize = PP_A4;
624: break;
625: case 1:
626: PageSize = PP_US;
627: break;
628: }
1.81 cvs 629: TtaSetPrintParameter (PP_PaperSize, PageSize);
1.1 cvs 630: break;
1.71 cvs 631: case PaperOrientation:
632: /* orientation submenu */
633: Orientation = val;
1.81 cvs 634: TtaSetPrintParameter (PP_Orientation, Orientation);
1.71 cvs 635: break;
636: case PPagesPerSheet:
637: /* pages per sheet submenu */
638: switch (val)
1.135 vatton 639: {
640: case 0:
641: PagePerSheet = 1;
642: break;
643: case 1:
644: PagePerSheet = 2;
645: break;
646: case 2:
647: PagePerSheet = 4;
648: break;
649: }
1.81 cvs 650: TtaSetPrintParameter (PP_PagesPerSheet, PagePerSheet);
1.71 cvs 651: break;
1.69 cvs 652: case PrintSupport:
1.1 cvs 653: /* paper print/save PostScript submenu */
654: switch (val)
1.135 vatton 655: {
656: case 0:
657: if (PaperPrint == PP_PS)
658: {
659: PaperPrint = PP_PRINTER;
1.110 cvs 660: #ifdef _GTK
1.135 vatton 661: TtaSetTextForm (BasePrint + PPrinterName, PPrinter);
1.110 cvs 662: #endif /* _GTK */
1.135 vatton 663: TtaSetPrintParameter (PP_Destination, PaperPrint);
664: }
665: break;
666: case 1:
667: if (PaperPrint == PP_PRINTER)
668: {
669: PaperPrint = PP_PS;
1.110 cvs 670: #ifdef _GTK
1.135 vatton 671: TtaSetTextForm (BasePrint + PPrinterName, PSfile);
1.110 cvs 672: #endif /* _GTK */
1.135 vatton 673: TtaSetPrintParameter (PP_Destination, PaperPrint);
674: }
675: break;
676: }
1.1 cvs 677: break;
1.69 cvs 678: case PPrinterName:
1.131 vatton 679: if (data[0] != EOS)
1.135 vatton 680: {
681: if (PaperPrint == PP_PRINTER)
682: /* text capture zone for the printer name */
683: strncpy (PPrinter, data, MAX_PATH);
684: else
685: /* text capture zone for the name of the PostScript file */
686: strncpy (PSfile, data, MAX_PATH);
687: }
1.1 cvs 688: break;
689: }
690: }
691:
692: /*----------------------------------------------------------------------
693: ----------------------------------------------------------------------*/
1.94 vatton 694: void InitPrint (void)
1.1 cvs 695: {
1.83 cvs 696: char* ptr;
1.1 cvs 697:
1.135 vatton 698: BasePrint = TtaSetCallback ((Proc)CallbackPrint, PRINT_MAX_REF);
699: DocPrint = 0;
700: DocPrintURL = NULL;
701:
702: /* read default printer variable */
703: ptr = TtaGetEnvString ("THOTPRINT");
704: if (ptr == NULL)
705: strcpy (PPrinter, "");
706: else
707: strcpy (PPrinter, ptr);
708: TtaSetPrintCommand (PPrinter);
709: PaperPrint = PP_PRINTER;
710: TtaSetPrintParameter (PP_Destination, PaperPrint);
711:
712: /* define the new default PrintSchema */
713: NumberLinks = FALSE;
714: WithToC = FALSE;
715: IgnoreCSS = FALSE;
716: PrintURL = TRUE;
717: PageSize = TtaGetPrintParameter (PP_PaperSize);
718: TtaSetPrintSchema ("");
719: /* no manual feed */
720: ManualFeed = PP_OFF;
721: TtaSetPrintParameter (PP_ManualFeed, ManualFeed);
722: PagePerSheet = 1;
723: TtaSetPrintParameter (PP_PagesPerSheet, PagePerSheet);
1.1 cvs 724: }
725:
726: /*----------------------------------------------------------------------
1.3 cvs 727: SetupAndPrint sets printing parameters and starts the printing process
1.1 cvs 728: ----------------------------------------------------------------------*/
1.91 vatton 729: void SetupAndPrint (Document doc, View view)
1.1 cvs 730: {
1.107 carcone 731: #ifdef _GTK
1.83 cvs 732: char bufMenu[MAX_LENGTH];
1.91 vatton 733: int i;
1.107 carcone 734: #endif /* _GTK */
1.91 vatton 735: ThotBool textFile;
1.38 cvs 736:
1.81 cvs 737: textFile = (DocumentTypes[doc] == docText || DocumentTypes[doc] == docCSS);
738: /* Print form */
739: CheckPrintingDocument (doc);
1.27 cvs 740:
1.107 carcone 741: #ifdef _GTK
1.81 cvs 742: TtaNewSheet (BasePrint + FormPrint, TtaGetViewFrame (doc, view),
1.135 vatton 743: TtaGetMessage (LIB, TMSG_LIB_PRINT), 1,
744: TtaGetMessage (LIB, TMSG_BUTTON_PRINT), FALSE, 3, 'L', D_CANCEL);
1.81 cvs 745:
746: /* Paper format submenu */
747: i = 0;
748: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_A4));
1.83 cvs 749: i += strlen (&bufMenu[i]) + 1;
1.81 cvs 750: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_US));
751: TtaNewSubmenu (BasePrint + PaperFormat, BasePrint + FormPrint, 0,
1.135 vatton 752: TtaGetMessage (LIB, TMSG_PAPER_SIZE), 2, bufMenu, NULL, 0, TRUE);
1.81 cvs 753: if (PageSize == PP_US)
754: TtaSetMenuForm (BasePrint + PaperFormat, 1);
755: else
756: TtaSetMenuForm (BasePrint + PaperFormat, 0);
757:
758: /* Orientation submenu */
759: i = 0;
760: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (AMAYA, AM_PORTRAIT));
1.83 cvs 761: i += strlen (&bufMenu[i]) + 1;
1.81 cvs 762: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (AMAYA, AM_LANDSCAPE));
763: TtaNewSubmenu (BasePrint + PaperOrientation, BasePrint + FormPrint, 0,
1.135 vatton 764: TtaGetMessage (AMAYA, AM_ORIENTATION), 2, bufMenu, NULL, 0, TRUE);
1.81 cvs 765: if (Orientation == PP_Landscape)
766: TtaSetMenuForm (BasePrint + PaperOrientation, 1);
767: else
768: TtaSetMenuForm (BasePrint + PaperOrientation, 0);
769: /* Pages per sheet submenu */
770: i = 0;
771: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_1_PAGE_SHEET));
1.83 cvs 772: i += strlen (&bufMenu[i]) + 1;
1.81 cvs 773: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_2_PAGE_SHEET));
1.83 cvs 774: i += strlen (&bufMenu[i]) + 1;
1.81 cvs 775: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_4_PAGE_SHEET));
776: TtaNewSubmenu (BasePrint + PPagesPerSheet, BasePrint + FormPrint, 0,
1.135 vatton 777: TtaGetMessage (LIB, TMSG_REDUCTION), 3, bufMenu, NULL, 0, TRUE);
1.81 cvs 778: if (PagePerSheet == 1)
779: TtaSetMenuForm (BasePrint + PPagesPerSheet, 0);
780: else if (PagePerSheet == 2)
781: TtaSetMenuForm (BasePrint + PPagesPerSheet, 1);
782: else
783: TtaSetMenuForm (BasePrint + PPagesPerSheet, 2);
784:
785: /* Print to paper/ Print to file submenu */
786: i = 0;
787: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_PRINTER));
1.83 cvs 788: i += strlen (&bufMenu[i]) + 1;
1.81 cvs 789: sprintf (&bufMenu[i], "%s%s", "B", TtaGetMessage (LIB, TMSG_PS_FILE));
790: TtaNewSubmenu (BasePrint + PrintSupport, BasePrint + FormPrint, 0,
1.135 vatton 791: TtaGetMessage (LIB, TMSG_OUTPUT), 2, bufMenu, NULL, 0, TRUE);
1.81 cvs 792:
793: /* PaperPrint selector */
794: TtaNewTextForm (BasePrint + PPrinterName, BasePrint + FormPrint, NULL, 30, 1, TRUE);
795: if (PaperPrint == PP_PRINTER)
796: {
797: TtaSetMenuForm (BasePrint + PrintSupport, 0);
798: TtaSetTextForm (BasePrint + PPrinterName, PPrinter);
799: }
800: else
801: {
802: TtaSetMenuForm (BasePrint + PrintSupport, 1);
803: TtaSetTextForm (BasePrint + PPrinterName, PSfile);
804: }
1.1 cvs 805:
1.81 cvs 806: /* The toggle */
807: i = 0;
808: sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (LIB, TMSG_MANUAL_FEED));
1.83 cvs 809: i += strlen (&bufMenu[i]) + 1;
1.81 cvs 810: sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_PRINT_TOC));
1.83 cvs 811: i += strlen (&bufMenu[i]) + 1;
1.81 cvs 812: sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_NUMBERED_LINKS));
1.83 cvs 813: i += strlen (&bufMenu[i]) + 1;
1.81 cvs 814: sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_PRINT_URL));
1.83 cvs 815: i += strlen (&bufMenu[i]) + 1;
1.81 cvs 816: sprintf (&bufMenu[i], "%s%s", "T", TtaGetMessage (AMAYA, AM_WITH_CSS));
817: TtaNewToggleMenu (BasePrint + PrintOptions, BasePrint + FormPrint,
1.135 vatton 818: TtaGetMessage (LIB, TMSG_OPTIONS), 5, bufMenu, NULL, FALSE);
1.81 cvs 819: if (ManualFeed == PP_ON)
820: TtaSetToggleMenu (BasePrint + PrintOptions, 0, TRUE);
821: else
822: TtaSetToggleMenu (BasePrint + PrintOptions, 0, FALSE);
823: TtaSetToggleMenu (BasePrint + PrintOptions, 1, WithToC);
824: TtaSetToggleMenu (BasePrint + PrintOptions, 2, NumberLinks);
825: TtaSetToggleMenu (BasePrint + PrintOptions, 3, PrintURL);
826: TtaSetToggleMenu (BasePrint + PrintOptions, 4, IgnoreCSS);
827:
828: /* activates the Print form */
1.160 carcone 829: TtaShowDialogue (BasePrint+FormPrint, FALSE, TRUE);
1.81 cvs 830: if (textFile)
831: {
832: /* invalid dialogue entries */
1.102 gully 833: TtaRedrawMenuEntry (BasePrint + PrintOptions, 1, NULL, (ThotColor)-1, FALSE);
834: TtaRedrawMenuEntry (BasePrint + PrintOptions, 2, NULL, (ThotColor)-1, FALSE);
1.81 cvs 835: }
1.107 carcone 836: #endif /* _GTK */
837: #ifdef _WINGUI
1.81 cvs 838: CreatePrintDlgWindow (TtaGetViewFrame (doc, view), PSfile);
1.103 cvs 839: #endif /* _WINGUI */
1.107 carcone 840: #ifdef _WX
1.108 gully 841: {
1.125 gully 842: int paper_format;
843: int orientation;
844: int disposition;
845: int paper_print;
1.128 cvs 846: ThotBool manual_feed;
847: ThotBool with_toc;
848: ThotBool with_links;
849: ThotBool with_url;
850: ThotBool ignore_css;
1.125 gully 851:
852: /* Paper format submenu */
853: if (PageSize == PP_US)
854: paper_format = 1;
855: else
856: paper_format = 0;
857:
858: /* Orientation submenu */
859: if (Orientation == PP_Landscape)
860: orientation = 1;
861: else
862: orientation = 0;
863:
864: /* Pages per sheet submenu */
865: if (PagePerSheet == 1)
866: disposition = 0;
867: else if (PagePerSheet == 2)
868: disposition = 1;
869: else
870: disposition = 2;
871:
872: /* PaperPrint selector */
873: if (PaperPrint == PP_PRINTER)
874: paper_print = 0;
875: else
876: paper_print = 1;
877:
878: /* The toggle */
879: manual_feed = (ManualFeed == PP_ON);
880: with_toc = WithToC;
881: with_links = NumberLinks;
882: with_url = PrintURL;
883: ignore_css = IgnoreCSS;
884:
885: /* The dialog creation */
1.108 gully 886: ThotBool created;
1.125 gully 887: created = CreatePrintDlgWX (BasePrint + FormPrint, TtaGetViewFrame (doc, view),
1.135 vatton 888: PPrinter,
889: PSfile,
890: paper_format,
891: orientation,
892: disposition,
893: paper_print,
894: manual_feed,
895: with_toc,
896: with_links,
897: with_url,
898: ignore_css);
1.108 gully 899: if (created)
900: {
1.160 carcone 901: TtaShowDialogue (BasePrint+FormPrint, FALSE, TRUE);
1.108 gully 902: }
903: }
904:
1.107 carcone 905: #endif /* _WX */
1.1 cvs 906: }
907:
908: /*----------------------------------------------------------------------
909: UpdateURLsInSubtree
910: Update NAMEs and URLs in subtree of el element, to take into account
911: the move from one document to another.
912: If a NAME attribute already exists in the new document, it is changed
913: to avoid duplicate names.
914: Transform the HREF and SRC attribute to make them independent from their
915: former base.
916: ----------------------------------------------------------------------*/
1.99 cvs 917: void UpdateURLsInSubtree (NotifyElement *event, Element el)
1.1 cvs 918: {
1.77 cvs 919: Element nextEl, child;
920: ElementType elType;
921: SSchema HTMLschema;
1.1 cvs 922:
923: nextEl = TtaGetFirstChild (el);
1.82 cvs 924: HTMLschema = TtaGetSSchema ("HTML", event->document);
1.99 cvs 925: if (HTMLschema)
1.1 cvs 926: {
1.141 vatton 927: // first check the id of the enclosing division
928: MakeUniqueName (el, event->document, TRUE, TRUE);
1.99 cvs 929: elType.ElSSchema = HTMLschema;
1.136 vatton 930: while (nextEl)
1.135 vatton 931: {
1.136 vatton 932: CheckPastedElement (nextEl, event->document, 0, event->position, TRUE);
1.135 vatton 933: /* manage included links and anchors */
934: elType.ElTypeNum = HTML_EL_Anchor;
935: child = TtaSearchTypedElement (elType, SearchInTree, nextEl);
936: while (child)
937: {
1.136 vatton 938: CheckPastedElement (child, event->document, 0, event->position, TRUE);
1.135 vatton 939: child = TtaSearchTypedElementInTree (elType, SearchForward, nextEl, child);
940: }
1.99 cvs 941:
1.135 vatton 942: /* manage included links and anchors */
943: elType.ElTypeNum = HTML_EL_PICTURE_UNIT;
944: child = TtaSearchTypedElement (elType, SearchInTree, nextEl);
945: while (child)
946: {
1.136 vatton 947: CheckPastedElement (child, event->document, 0, event->position, TRUE);
1.135 vatton 948: child = TtaSearchTypedElementInTree (elType, SearchForward, nextEl, child);
949: }
950: TtaNextSibling (&nextEl);
951: }
1.1 cvs 952: }
953: }
954:
955:
956: /*----------------------------------------------------------------------
957: MoveDocumentBody
1.58 cvs 958: Copy the elements contained in the BODY of the document sourceDoc at the
959: position of the element el in the document destDoc.
960: Delete the element containing el and all its empty ancestors.
1.1 cvs 961: If deleteTree is TRUE, copied elements are deleted from the source
962: document.
1.58 cvs 963: Return the root element that delimits the new inserted part (a div).
1.1 cvs 964: ----------------------------------------------------------------------*/
1.80 cvs 965: static Element MoveDocumentBody (Element el, Document destDoc,
1.135 vatton 966: Document sourceDoc, char *target,
967: char *url, ThotBool deleteTree)
1.1 cvs 968: {
1.140 vatton 969: Element root, ancestor, elem, firstInserted, div;
1.12 cvs 970: Element lastInserted, srce, copy, old, parent, sibling;
1.140 vatton 971: ElementType elType;
972: SSchema nature;
1.12 cvs 973: NotifyElement event;
1.143 vatton 974: int i, j;
975: ThotBool checkingMode, isID;
1.1 cvs 976:
1.67 cvs 977: div = NULL;
1.140 vatton 978: if (target)
1.13 cvs 979: {
980: /* locate the target element within the source document */
1.98 vatton 981: root = SearchNAMEattribute (sourceDoc, target, NULL, NULL);
1.13 cvs 982: elType = TtaGetElementType (root);
1.84 cvs 983: isID = (elType.ElTypeNum != HTML_EL_Anchor &&
1.135 vatton 984: elType.ElTypeNum != HTML_EL_MAP);
1.13 cvs 985: }
986: else
987: {
988: isID = FALSE;
989: /* get the BODY element of source document */
990: root = TtaGetMainRoot (sourceDoc);
991: elType = TtaGetElementType (root);
992: elType.ElTypeNum = HTML_EL_BODY;
993: root = TtaSearchTypedElement (elType, SearchForward, root);
994: }
995:
1.140 vatton 996: if (root)
1.12 cvs 997: {
998: /* don't check the abstract tree against the structure schema */
999: checkingMode = TtaGetStructureChecking (destDoc);
1.124 vatton 1000: TtaSetStructureChecking (FALSE, destDoc);
1.58 cvs 1001: /* get elem, the ancestor of el which is a child of a DIV or BODY
1.135 vatton 1002: element in the destination document. The copied elements will be
1003: inserted just before this element. */
1.58 cvs 1004: elem = el;
1.12 cvs 1005: do
1.135 vatton 1006: {
1007: ancestor = TtaGetParent (elem);
1008: if (ancestor != NULL)
1009: {
1010: elType = TtaGetElementType (ancestor);
1011: if (elType.ElTypeNum == HTML_EL_BODY ||
1012: elType.ElTypeNum == HTML_EL_Division)
1013: ancestor = NULL;
1014: else
1015: elem = ancestor;
1016: }
1017: }
1.140 vatton 1018: while (ancestor);
1.12 cvs 1019: parent = TtaGetParent (elem);
1.14 cvs 1020:
1021: /* insert a DIV element */
1.15 cvs 1022: elType.ElTypeNum = HTML_EL_Division;
1.16 cvs 1023: lastInserted = TtaNewElement (destDoc, elType);
1024: TtaInsertSibling (lastInserted, elem, TRUE, destDoc);
1.58 cvs 1025: /* this delimits the new inserted part of the document */
1.16 cvs 1026: RegisterSubDoc (lastInserted, url);
1.164 ! vatton 1027: CreateTargetAnchor (destDoc, lastInserted, FALSE,TRUE, FALSE);
1.58 cvs 1028: div = lastInserted;
1.14 cvs 1029:
1.140 vatton 1030: // check if new natures are added
1031: if (DocumentMeta[destDoc] && !DocumentMeta[destDoc]->compound)
1032: {
1033: nature = NULL;
1034: TtaNextNature (sourceDoc, &nature);
1035: if (nature)
1036: DocumentMeta[destDoc]->compound = TRUE;
1037: }
1038:
1.12 cvs 1039: /* do copy */
1.16 cvs 1040: firstInserted = NULL;
1.17 cvs 1041: if (isID)
1.135 vatton 1042: srce = root;
1.17 cvs 1043: else
1.135 vatton 1044: srce = TtaGetFirstChild (root);
1.140 vatton 1045: while (srce)
1.135 vatton 1046: {
1047: copy = TtaCopyTree (srce, sourceDoc, destDoc, parent);
1048: if (copy != NULL)
1049: {
1050: if (firstInserted == NULL)
1051: /* this is the first copied element. Insert it before elem */
1052: {
1053: TtaInsertFirstChild (©, lastInserted, destDoc);
1054: firstInserted = copy;
1055: }
1056: else
1057: /* insert the new copied element after the element previously
1058: copied */
1059: TtaInsertSibling (copy, lastInserted, FALSE, destDoc);
1060: lastInserted = copy;
1061: /* update the NAMEs and URLs in the copied element */
1062: event.event = TteElemPaste;
1063: event.document = destDoc;
1064: event.element = copy;
1065: event.elementType = TtaGetElementType (copy);
1066: event.position = sourceDoc;
1067: event.info = 0;
1068: UpdateURLsInSubtree(&event, copy);
1069: }
1070: /* get the next element in the source document */
1071: old = srce;
1072: TtaNextSibling (&srce);
1073: if (deleteTree)
1.143 vatton 1074: {
1075: // remove the current selection if it concerns the removed subtree
1076: TtaGiveFirstSelectedElement (sourceDoc, &elem, &i, &j);
1077: if (elem)
1078: {
1079: if (TtaIsAncestor (elem, old))
1080: TtaCancelSelection (sourceDoc);
1081: else
1082: {
1083: TtaGiveLastSelectedElement (sourceDoc, &elem, &i, &j);
1084: if (elem &&TtaIsAncestor (elem, old))
1085: TtaCancelSelection (sourceDoc);
1086: }
1087: }
1088: TtaDeleteTree (old, sourceDoc);
1089: }
1.135 vatton 1090: /* Stop here if the target points to a specific element with an ID */
1091: if (isID)
1092: srce = NULL;
1093: }
1.12 cvs 1094:
1095: /* delete the element(s) containing the link to the copied document */
1.58 cvs 1096: /* delete the parent element of el and all empty ancestors */
1097: elem = TtaGetParent (el);
1.12 cvs 1098: do
1.135 vatton 1099: {
1100: sibling = elem;
1101: TtaNextSibling (&sibling);
1102: if (sibling == NULL)
1103: {
1104: sibling = elem;
1105: TtaPreviousSibling (&sibling);
1106: if (sibling == NULL)
1107: elem = TtaGetParent (elem);
1108: }
1109: }
1.12 cvs 1110: while (sibling == NULL);
1111: TtaDeleteTree (elem, destDoc);
1112: /* restore previous chacking mode */
1.124 vatton 1113: TtaSetStructureChecking (checkingMode, destDoc);
1.12 cvs 1114: }
1.58 cvs 1115: /* return the address of the new division */
1116: return (div);
1.1 cvs 1117: }
1118:
1.58 cvs 1119: /*----------------------------------------------------------------------
1120: CloseMakeBook
1121: ----------------------------------------------------------------------*/
1122: static void CloseMakeBook (Document document)
1123: {
1124: ResetStop (document);
1125: /* update internal links */
1126: SetInternalLinks (document);
1127: /* if the document changed force the browser mode */
1.139 vatton 1128: if (SubDocs)
1.128 cvs 1129: /* send a warning to the user to avoid to save this document */
1130: /* remove registered sub-documents */
1131: FreeSubDocTable ();
1.58 cvs 1132: DocBook = 0;
1133: TtaSetStatus (document, 1, TtaGetMessage (AMAYA, AM_DOCUMENT_LOADED), NULL);
1134: }
1135:
1136: /*----------------------------------------------------------------------
1137: GetIncludedDocuments_callback finishes the GetIncludedDocuments procedure
1138: ----------------------------------------------------------------------*/
1139: void GetIncludedDocuments_callback (int newdoc, int status,
1.135 vatton 1140: char *urlName,
1.145 cvs 1141: char *outputfile,
1142: char *proxyName,
1.135 vatton 1143: AHTHeaders *http_headers,
1144: void * context)
1.29 cvs 1145: {
1.58 cvs 1146: Element link, div;
1147: IncludeCtxt *ctx, *prev;
1.104 vatton 1148: char *utf8path, *ptr;
1.58 cvs 1149: ThotBool found = FALSE;
1.29 cvs 1150:
1151: /* restore GetIncludedDocuments's context */
1.58 cvs 1152: ctx = (IncludeCtxt *) context;
1.29 cvs 1153: if (!ctx)
1154: return;
1155:
1.58 cvs 1156: div = NULL;
1.29 cvs 1157: link = ctx->link;
1.58 cvs 1158: ptr = ctx->name;
1.104 vatton 1159: utf8path = ctx->utf8path;
1160: if (utf8path)
1.29 cvs 1161: {
1.58 cvs 1162: if (newdoc && newdoc != DocBook)
1.135 vatton 1163: {
1164: /* it's not the DocBook itself */
1165: /* copy the target document at the position of the link */
1166: TtaSetDocumentModified (DocBook);
1167: div = MoveDocumentBody (link, DocBook, newdoc, ptr, utf8path,
1168: (ThotBool)(newdoc == IncludedDocument));
1169: }
1.58 cvs 1170: /* global variables */
1171: FreeDocumentResource (IncludedDocument);
1172: TtaCloseDocument (IncludedDocument);
1173: IncludedDocument = 0;
1.29 cvs 1174: }
1.58 cvs 1175:
1.104 vatton 1176: if (div)
1.29 cvs 1177: {
1.58 cvs 1178: /* new starting point for the search */
1179: ctx->link = div;
1180: found = GetIncludedDocuments (div, div, DocBook, ctx);
1.29 cvs 1181: }
1.104 vatton 1182:
1.58 cvs 1183: while (!found && ctx)
1184: {
1185: /* this sub-document has no more inclusion, examine the caller */
1186: div = ctx->div;
1187: link = ctx->link;
1188: prev = ctx->ctxt;
1.104 vatton 1189: TtaFreeMemory (utf8path);
1190: utf8path = NULL;
1.58 cvs 1191: TtaFreeMemory (ctx);
1192: ctx = prev;
1193: found = GetIncludedDocuments (div, link, DocBook, ctx);
1194: }
1195: if (!found)
1196: /* all links are now managed */
1197: CloseMakeBook (DocBook);
1.29 cvs 1198: }
1199:
1.1 cvs 1200: /*----------------------------------------------------------------------
1201: GetIncludedDocuments
1.58 cvs 1202: Look forward within the element el, starting from element link, for a
1203: link (A) with attribute rel="chapter" or rel="subdocument" and replace
1204: that link by the contents of the target document.
1205: Return TRUE if one inclusion is launched.
1.1 cvs 1206: ----------------------------------------------------------------------*/
1.96 vatton 1207: static ThotBool GetIncludedDocuments (Element el, Element link,
1.135 vatton 1208: Document doc, IncludeCtxt *prev)
1.1 cvs 1209: {
1.58 cvs 1210: ElementType elType;
1211: Attribute attr;
1212: AttributeType attrType;
1213: Document newdoc;
1214: IncludeCtxt *ctx = NULL;
1.104 vatton 1215: char *utf8path, *ptr;
1.29 cvs 1216: int length;
1.58 cvs 1217: ThotBool found = FALSE;
1.29 cvs 1218:
1.58 cvs 1219: /* look for anchors with the attribute rel within the element el */
1220: attr = NULL;
1.96 vatton 1221: attrType.AttrSSchema = TtaGetSSchema ("HTML", doc);
1.58 cvs 1222: elType.ElSSchema = attrType.AttrSSchema;
1223: elType.ElTypeNum = HTML_EL_Anchor;
1.29 cvs 1224:
1.58 cvs 1225: /* Get only one included file each time */
1226: while (link && attr == NULL)
1.29 cvs 1227: {
1.58 cvs 1228: link = TtaSearchTypedElementInTree (elType, SearchForward, el, link);
1229: if (link)
1.135 vatton 1230: {
1231: attrType.AttrTypeNum = HTML_ATTR_REL;
1232: attr = TtaGetAttribute (link, attrType);
1233: }
1.58 cvs 1234: if (attr)
1.135 vatton 1235: {
1236: length = TtaGetTextAttributeLength (attr);
1237: utf8path = (char *)TtaGetMemory (length + 1);
1238: TtaGiveTextAttributeValue (attr, utf8path, &length);
1239: /* Valid rel values are rel="chapter" or rel="subdocument" */
1240: if (strcasecmp (utf8path, "chapter") &&
1241: strcasecmp (utf8path, "subdocument"))
1242: attr = NULL;
1243: TtaFreeMemory (utf8path);
1244: }
1.58 cvs 1245:
1246: if (attr)
1.135 vatton 1247: {
1248: /* a link with attribute rel="Chapter" has been found */
1249: attrType.AttrTypeNum = HTML_ATTR_HREF_;
1250: attr = TtaGetAttribute (link, attrType);
1251: }
1.58 cvs 1252: if (attr)
1.135 vatton 1253: /* this link has an attribute HREF */
1254: {
1255: length = TtaGetTextAttributeLength (attr);
1256: utf8path = (char *)TtaGetMemory (length + 1);
1257: TtaGiveTextAttributeValue (attr, utf8path, &length);
1.159 quint 1258: /* remove trailing spaces */
1259: while (length > 0 &&
1260: (utf8path[length - 1] == SPACE || utf8path[length - 1] == TAB))
1261: {
1262: utf8path[length-1] = EOS;
1263: length--;
1264: }
1.135 vatton 1265: ptr = strrchr (utf8path, '#');
1266: if (ptr)
1267: {
1268: /* link to a particular position within a document */
1269: if (ptr == utf8path)
1270: {
1271: /* local link */
1272: TtaFreeMemory (utf8path);
1273: utf8path = NULL;
1274: }
1275: else
1276: {
1277: ptr[0] = EOS;
1278: ptr = &ptr[1];
1279: }
1280: }
1.58 cvs 1281:
1.135 vatton 1282: if (utf8path)
1283: /* this link designates an external document */
1284: {
1285: /* create a new document and loads the target document */
1286: IncludedDocument = TtaNewDocument ("HTML", "tmp");
1287: if (IncludedDocument != 0)
1288: {
1289: TtaSetStatus (doc, 1, TtaGetMessage (AMAYA, AM_FETCHING), utf8path);
1290: ctx = (IncludeCtxt *)TtaGetMemory (sizeof (IncludeCtxt));
1291: ctx->div = el;
1292: ctx->link = link;
1293: ctx->utf8path = utf8path; /* the URL of the document */
1294: ctx->name = ptr;
1295: ctx->ctxt = prev; /* previous context */
1296: /* Get the reference of the calling document */
1297: SetStopButton (doc);
1298: newdoc = GetAmayaDoc (utf8path, NULL, IncludedDocument,
1299: doc, CE_MAKEBOOK, FALSE,
1.144 cvs 1300: (void (*)(int, int, char*, char*, char*, const AHTHeaders*, void*)) GetIncludedDocuments_callback,
1.135 vatton 1301: (void *) ctx);
1302: found = TRUE;
1303: }
1304: }
1305: else
1306: TtaFreeMemory (utf8path);
1307: }
1.29 cvs 1308: }
1.58 cvs 1309: return (found);
1.1 cvs 1310: }
1311:
1312: /*----------------------------------------------------------------------
1313: MakeBook
1314: Replace all links in a document which have an attribute REL="chapter"
1.5 cvs 1315: or REL="subdocument" by the corresponding target document.
1.1 cvs 1316: ----------------------------------------------------------------------*/
1.96 vatton 1317: void MakeBook (Document doc, View view)
1.1 cvs 1318: {
1.58 cvs 1319: Element root, body;
1320: ElementType elType;
1.1 cvs 1321:
1.139 vatton 1322: if (SubDocs)
1323: // another make_book is wi=orking
1324: return;
1.58 cvs 1325: /* stops all current transfers on this document */
1.96 vatton 1326: StopTransfer (doc, 1);
1.58 cvs 1327: /* simulate a transfert in the main document */
1.96 vatton 1328: DocBook = doc;
1.58 cvs 1329: IncludedDocument = 0;
1.96 vatton 1330: root = TtaGetMainRoot (doc);
1.58 cvs 1331: elType = TtaGetElementType (root);
1332: elType.ElTypeNum = HTML_EL_BODY;
1333: body = TtaSearchTypedElement (elType, SearchForward, root);
1334: if (body)
1.96 vatton 1335: GetIncludedDocuments (body, body, doc, NULL);
1.1 cvs 1336: }
1.105 vatton 1337:
1338: /*----------------------------------------------------------------------
1339: ----------------------------------------------------------------------*/
1340: void ReadAsUTF_8 (Document doc, View view)
1341: {
1342: ReparseAs (doc, view, FALSE, UTF_8);
1343: }
1344:
1345: /*----------------------------------------------------------------------
1346: ----------------------------------------------------------------------*/
1347: void ReadAsISO_8859_1 (Document doc, View view)
1348: {
1349: ReparseAs (doc, view, FALSE, ISO_8859_1);
1350: }
1351:
1352: /*----------------------------------------------------------------------
1353: ----------------------------------------------------------------------*/
1354: void ReadAsISO_8859_2 (Document doc, View view)
1355: {
1356: ReparseAs (doc, view, FALSE, ISO_8859_2);
1357: }
1358:
1359: /*----------------------------------------------------------------------
1360: ----------------------------------------------------------------------*/
1361: void ReadAsISO_8859_3 (Document doc, View view)
1362: {
1363: ReparseAs (doc, view, FALSE, ISO_8859_3);
1364: }
1365:
1366: /*----------------------------------------------------------------------
1367: ----------------------------------------------------------------------*/
1368: void ReadAsISO_8859_4 (Document doc, View view)
1369: {
1370: ReparseAs (doc, view, FALSE, ISO_8859_4);
1371: }
1372:
1373: /*----------------------------------------------------------------------
1374: ----------------------------------------------------------------------*/
1375: void ReadAsISO_8859_5 (Document doc, View view)
1376: {
1377: ReparseAs (doc, view, FALSE, ISO_8859_5);
1378: }
1379:
1380: /*----------------------------------------------------------------------
1381: ----------------------------------------------------------------------*/
1382: void ReadAsISO_8859_6 (Document doc, View view)
1383: {
1384: ReparseAs (doc, view, FALSE, ISO_8859_6);
1385: }
1386:
1387: /*----------------------------------------------------------------------
1388: ----------------------------------------------------------------------*/
1389: void ReadAsISO_8859_7 (Document doc, View view)
1390: {
1391: ReparseAs (doc, view, FALSE, ISO_8859_7);
1392: }
1393:
1394: /*----------------------------------------------------------------------
1395: ----------------------------------------------------------------------*/
1396: void ReadAsISO_8859_8 (Document doc, View view)
1397: {
1398: ReparseAs (doc, view, FALSE, ISO_8859_8);
1399: }
1400:
1401: /*----------------------------------------------------------------------
1402: ----------------------------------------------------------------------*/
1403: void ReadAsISO_8859_9 (Document doc, View view)
1404: {
1405: ReparseAs (doc, view, FALSE, ISO_8859_9);
1406: }
1407:
1408: /*----------------------------------------------------------------------
1409: ----------------------------------------------------------------------*/
1410: void ReadAsISO_8859_15 (Document doc, View view)
1411: {
1412: ReparseAs (doc, view, FALSE, ISO_8859_15);
1413: }
1414:
1415: /*----------------------------------------------------------------------
1416: ----------------------------------------------------------------------*/
1417: void ReadAsKOI8_R (Document doc, View view)
1418: {
1419: ReparseAs (doc, view, FALSE, KOI8_R);
1420: }
1421:
1422: /*----------------------------------------------------------------------
1423: ----------------------------------------------------------------------*/
1424: void ReadAsWINDOWS_1250 (Document doc, View view)
1425: {
1426: ReparseAs (doc, view, FALSE, WINDOWS_1250);
1427: }
1428:
1429: /*----------------------------------------------------------------------
1430: ----------------------------------------------------------------------*/
1431: void ReadAsWINDOWS_1251 (Document doc, View view)
1432: {
1433: ReparseAs (doc, view, FALSE, WINDOWS_1251);
1434: }
1435:
1436: /*----------------------------------------------------------------------
1437: ----------------------------------------------------------------------*/
1438: void ReadAsWINDOWS_1252 (Document doc, View view)
1439: {
1440: ReparseAs (doc, view, FALSE, WINDOWS_1252);
1441: }
1442:
1443: /*----------------------------------------------------------------------
1444: ----------------------------------------------------------------------*/
1445: void ReadAsWINDOWS_1253 (Document doc, View view)
1446: {
1447: ReparseAs (doc, view, FALSE, WINDOWS_1253);
1448: }
1449:
1450: /*----------------------------------------------------------------------
1451: ----------------------------------------------------------------------*/
1452: void ReadAsWINDOWS_1254 (Document doc, View view)
1453: {
1454: ReparseAs (doc, view, FALSE, WINDOWS_1254);
1455: }
1456:
1457: /*----------------------------------------------------------------------
1458: ----------------------------------------------------------------------*/
1459: void ReadAsWINDOWS_1255 (Document doc, View view)
1460: {
1461: ReparseAs (doc, view, FALSE, WINDOWS_1255);
1462: }
1463:
1464: /*----------------------------------------------------------------------
1465: ----------------------------------------------------------------------*/
1466: void ReadAsWINDOWS_1256 (Document doc, View view)
1467: {
1468: ReparseAs (doc, view, FALSE, WINDOWS_1256);
1469: }
1470:
1471: /*----------------------------------------------------------------------
1472: ----------------------------------------------------------------------*/
1473: void ReadAsWINDOWS_1257 (Document doc, View view)
1474: {
1475: ReparseAs (doc, view, FALSE, WINDOWS_1257);
1476: }
1477:
1478: /*----------------------------------------------------------------------
1479: ----------------------------------------------------------------------*/
1.134 vatton 1480: void ReadAsGB_2312 (Document doc, View view)
1481: {
1482: ReparseAs (doc, view, FALSE, GB_2312);
1483: }
1484:
1485: /*----------------------------------------------------------------------
1486: ----------------------------------------------------------------------*/
1.105 vatton 1487: void ReadAsISO_2022_JP (Document doc, View view)
1488: {
1489: ReparseAs (doc, view, FALSE, ISO_2022_JP);
1490: }
1491:
1492: /*----------------------------------------------------------------------
1493: ----------------------------------------------------------------------*/
1494: void ReadAsEUC_JP (Document doc, View view)
1495: {
1496: ReparseAs (doc, view, FALSE, EUC_JP);
1497: }
1498:
1499: /*----------------------------------------------------------------------
1500: ----------------------------------------------------------------------*/
1501: void ReadAsSHIFT_JIS (Document doc, View view)
1502: {
1503: ReparseAs (doc, view, FALSE, SHIFT_JIS);
1504: }
1.114 vatton 1505:
1506: /*----------------------------------------------------------------------
1.161 quint 1507: SectionNumbering generates numbers for all HTML H* elements after
1.119 vatton 1508: the current position.
1509: ----------------------------------------------------------------------*/
1510: void SectionNumbering (Document doc, View view)
1511: {
1.161 quint 1512: Element root, el, new_, child;
1.120 vatton 1513: DisplayMode dispMode;
1514: SSchema HTMLschema;
1515: ElementType elType, childType, searchedType1, searchedType2;
1516: ElementType searchedType3, searchedType4, searchedType5;
1517: Language lang;
1518: char s[MAX_LENGTH], n[20], *text;
1.161 quint 1519: int nH1, nH2, nH3, nH4, nH5, nH6, length, i;
1520: ThotBool closeUndo, manyH1, change = FALSE;
1.120 vatton 1521:
1522: /* check if there is any HTML element within the document */
1523: HTMLschema = TtaGetSSchema ("HTML", doc);
1524: if (HTMLschema == NULL)
1525: /* no HTML element */
1526: return;
1527:
1.161 quint 1528: dispMode = TtaGetDisplayMode (doc);
1.155 vatton 1529: if (TtaHasUndoSequence (doc))
1.135 vatton 1530: closeUndo = FALSE;
1.120 vatton 1531: else
1532: {
1533: closeUndo = TRUE;
1534: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1535: }
1536:
1.161 quint 1537: /* check if there are more than one HTML H1 element */
1538: manyH1 = FALSE;
1539: root = TtaGetMainRoot (doc);
1540: elType.ElSSchema = HTMLschema;
1541: elType.ElTypeNum = HTML_EL_H1;
1542: el = TtaSearchTypedElement (elType, SearchForward, root);
1543: if (el)
1544: {
1545: if (TtaSearchTypedElement (elType, SearchForward, el))
1546: /* there are at least 2 H1 elements in the document */
1547: manyH1 = TRUE;
1548: else
1549: /* there is 1 and only 1 H1 in the document */
1550: {
1551: /* remove its section number if it has one */
1552: /* look for the first leaf child */
1553: child = el;
1554: do
1555: {
1556: child = TtaGetFirstChild (child);
1557: if (child)
1558: childType = TtaGetElementType (child);
1559: }
1560: while (child && !TtaIsLeaf (childType) &&
1561: childType.ElSSchema == HTMLschema);
1562: if (child && childType.ElSSchema == HTMLschema &&
1563: childType.ElTypeNum == HTML_EL_TEXT_UNIT)
1564: /* the first leaf is a text unit */
1565: {
1566: /* check the text contents */
1567: length = TtaGetTextLength (child) + 1;
1568: text = (char *)TtaGetMemory (length);
1569: TtaGiveTextContent (child, (unsigned char *)text, &length, &lang);
1570: /* remove the old number */
1571: i = 0;
1572: while (isdigit (text[i]) || text[i] == '.')
1573: i++;
1574: // remove extra spaces
1575: while (text[i] == SPACE)
1576: i++;
1577: TtaRegisterElementReplace (child, doc);
1578: TtaSetTextContent (child, (unsigned char *)&text[i],
1579: Latin_Script, doc);
1580: TtaFreeMemory (text);
1581: change = TRUE;
1582: }
1583: }
1584: }
1585:
1.120 vatton 1586: searchedType1.ElSSchema = HTMLschema;
1587: searchedType2.ElSSchema = HTMLschema;
1588: searchedType3.ElSSchema = HTMLschema;
1589: searchedType4.ElSSchema = HTMLschema;
1590: searchedType5.ElSSchema = HTMLschema;
1.161 quint 1591: if (manyH1)
1592: {
1593: searchedType1.ElTypeNum = HTML_EL_H1;
1594: searchedType2.ElTypeNum = HTML_EL_H2;
1595: searchedType3.ElTypeNum = HTML_EL_H3;
1596: searchedType4.ElTypeNum = HTML_EL_H4;
1597: searchedType5.ElTypeNum = HTML_EL_H5;
1598: }
1599: else
1600: {
1601: searchedType1.ElTypeNum = HTML_EL_H2;
1602: searchedType2.ElTypeNum = HTML_EL_H3;
1603: searchedType3.ElTypeNum = HTML_EL_H4;
1604: searchedType4.ElTypeNum = HTML_EL_H5;
1605: searchedType5.ElTypeNum = HTML_EL_H6;
1606: }
1607: nH1 = nH2 = nH3 = nH4 = nH5 = nH6 = 0;
1608: el = root;
1.120 vatton 1609: while (el)
1610: {
1611: el = TtaSearchElementAmong5Types (searchedType1, searchedType2,
1.135 vatton 1612: searchedType3, searchedType4,
1613: searchedType5, SearchForward, el);
1.120 vatton 1614: if (el)
1.135 vatton 1615: {
1616: /* generate the text number */
1617: elType = TtaGetElementType (el);
1618: s[0] = EOS;
1619: switch (elType.ElTypeNum)
1620: {
1.161 quint 1621: case HTML_EL_H1:
1622: nH1++;
1623: nH2 = nH3 = nH4 = nH5 = nH6 = 0;
1624: sprintf (s, "%d.", nH1);
1625: break;
1.135 vatton 1626: case HTML_EL_H2:
1627: nH2++;
1628: nH3 = nH4 = nH5 = nH6 = 0;
1.161 quint 1629: if (manyH1)
1630: {
1631: if (nH1)
1632: sprintf (s, "%d.", nH1);
1633: sprintf (n, "%d.", nH2);
1634: strcat (s, n);
1635: }
1636: else
1637: sprintf (s, "%d.", nH2);
1.135 vatton 1638: break;
1639: case HTML_EL_H3:
1640: nH3++;
1641: nH4 = nH5 = nH6 = 0;
1.161 quint 1642: if (manyH1)
1643: {
1644: if (nH1)
1645: sprintf (s, "%d.", nH1);
1646: if (nH2)
1647: {
1648: sprintf (n, "%d.", nH2);
1649: strcat (s, n);
1650: }
1651: }
1652: else
1653: {
1654: if (nH2)
1655: sprintf (s, "%d.", nH2);
1656: }
1657: sprintf (n, "%d.", nH3);
1658: strcat (s, n);
1.135 vatton 1659: break;
1660: case HTML_EL_H4:
1661: nH4++;
1662: nH5 = nH6 = 0;
1.161 quint 1663: if (manyH1)
1664: {
1665: if (nH1)
1666: sprintf (s, "%d.", nH1);
1667: if (nH2)
1668: {
1669: sprintf (n, "%d.", nH2);
1670: strcat (s, n);
1671: }
1672: }
1673: else
1674: {
1675: if (nH2)
1676: sprintf (s, "%d.", nH2);
1677: }
1678: if (nH3)
1679: {
1680: sprintf (n, "%d.", nH3);
1681: strcat (s, n);
1682: }
1683: sprintf (n, "%d.", nH4);
1684: strcat (s, n);
1.135 vatton 1685: break;
1686: case HTML_EL_H5:
1687: nH5++;
1688: nH6 = 0;
1.161 quint 1689: if (manyH1)
1690: {
1691: if (nH1)
1692: sprintf (s, "%d.", nH1);
1693: if (nH2)
1694: {
1695: sprintf (n, "%d.", nH2);
1696: strcat (s, n);
1697: }
1698: }
1699: else
1700: {
1701: if (nH2)
1702: sprintf (s, "%d.", nH2);
1703: }
1704: if (nH3)
1705: {
1706: sprintf (n, "%d.", nH3);
1707: strcat (s, n);
1708: }
1709: if (nH4)
1710: {
1711: sprintf (n, "%d.", nH4);
1712: strcat (s, n);
1713: }
1714: sprintf (n, "%d.", nH5);
1715: strcat (s, n);
1.135 vatton 1716: break;
1717: case HTML_EL_H6:
1718: nH6++;
1719: if (nH2)
1720: sprintf (s, "%d.", nH2);
1721: if (nH3)
1722: {
1723: sprintf (n, "%d.", nH3);
1724: strcat (s, n);
1725: }
1726: if (nH4)
1727: {
1728: sprintf (n, "%d.", nH4);
1729: strcat (s, n);
1730: }
1731: if (nH5)
1732: {
1733: sprintf (n, "%d.", nH5);
1734: strcat (s, n);
1735: }
1736: sprintf (n, "%d.", nH6);
1737: strcat (s, n);
1738: break;
1739: default: break;
1740: }
1741: strcat (s, " ");
1742:
1743: /* look for the first leaf child */
1744: child = el;
1745: if (child)
1746: {
1747: do
1748: {
1749: child = TtaGetFirstChild (child);
1750: childType = TtaGetElementType (child);
1751: }
1.161 quint 1752: while (!TtaIsLeaf (childType) &&
1753: childType.ElSSchema == HTMLschema);
1.135 vatton 1754: }
1755: if (child && childType.ElSSchema == HTMLschema &&
1756: childType.ElTypeNum == HTML_EL_TEXT_UNIT)
1757: {
1758: /* the first leaf child is a text unit */
1759: new_ = NULL;
1760: /* check the text contents */
1761: length = TtaGetTextLength (child) + 1;
1762: text = (char *)TtaGetMemory (length);
1763: TtaGiveTextContent (child, (unsigned char *)text, &length, &lang);
1764: /* remove the old number */
1765: i = 0;
1766: while (isdigit (text[i]) || text[i] == '.')
1767: i++;
1.138 vatton 1768: // remove extra spaces
1769: while (text[i] == SPACE)
1770: i++;
1.135 vatton 1771: TtaRegisterElementReplace (child, doc);
1772: TtaSetTextContent (child, (unsigned char *)s, Latin_Script, doc);
1773: TtaAppendTextContent (child, (unsigned char *)&text[i], doc);
1774: TtaFreeMemory (text);
1775: change = TRUE;
1776: }
1777: else
1778: {
1779: /* add a new text element */
1780: elType.ElTypeNum = HTML_EL_TEXT_UNIT;
1781: new_ = TtaNewElement (doc, elType);
1782: if (child)
1783: /* insert before */
1784: TtaInsertSibling (new_, child, TRUE, doc);
1785: else
1786: TtaInsertFirstChild (&new_, el, doc);
1787: TtaSetTextContent (new_, (unsigned char *)s, Latin_Script, doc);
1788: TtaRegisterElementCreate (new_, doc);
1789: change = TRUE;
1790: }
1791: }
1.120 vatton 1792: }
1793:
1794: if (closeUndo)
1795: TtaCloseUndoSequence (doc);
1796: if (dispMode == DisplayImmediately)
1797: TtaSetDisplayMode (doc, dispMode);
1.123 vatton 1798: if (change)
1799: TtaSetDocumentModified (doc);
1.119 vatton 1800: }
1801:
1802: /*----------------------------------------------------------------------
1.114 vatton 1803: MakeToC generates a Table of Contents at the current position.
1.162 quint 1804: Looks for all HTML H* elements after the current position.
1.114 vatton 1805: ----------------------------------------------------------------------*/
1806: void MakeToc (Document doc, View view)
1807: {
1.146 quint 1808: Element el, new_, *list, parent, copy, srce, child, prev, ancest;
1.162 quint 1809: Element root, toc, lH1, lH2, lH3, lH4, lH5, lH6, item;
1.114 vatton 1810: ElementType elType, searchedType1, searchedType2;
1811: ElementType searchedType3, searchedType4, searchedType5;
1812: ElementType ulType, copyType;
1813: AttributeType attrType;
1814: Attribute attr;
1815: DisplayMode dispMode;
1.148 vatton 1816: char *s, *id, *value;
1.114 vatton 1817: int firstChar, i;
1.163 vatton 1818: ThotBool closeUndo, found, manyH1, extendUndo;
1.114 vatton 1819:
1820: dispMode = TtaGetDisplayMode (doc);
1821:
1822: /* get the insert point */
1823: TtaGiveFirstSelectedElement (doc, &el, &firstChar, &i);
1824: if (el == NULL || TtaIsReadOnly (el))
1.117 vatton 1825: {
1826: /* no selection */
1.154 vatton 1827: TtaDisplaySimpleMessage (CONFIRM, LIB, TMSG_EL_RO);
1.135 vatton 1828: return;
1.117 vatton 1829: }
1.137 vatton 1830:
1.114 vatton 1831: elType = TtaGetElementType (el);
1832: s = TtaGetSSchemaName (elType.ElSSchema);
1833: if (strcmp (s, "HTML"))
1834: /* not within HTML element */
1.146 quint 1835: {
1836: /* skip ancestors that are Template elements */
1837: ancest = el;
1838: while (ancest && !strcmp (s, "Template"))
1839: {
1840: ancest = TtaGetParent (ancest);
1841: if (ancest)
1842: {
1843: elType = TtaGetElementType (ancest);
1844: s = TtaGetSSchemaName (elType.ElSSchema);
1845: }
1846: }
1847: if (strcmp (s, "HTML"))
1848: return;
1849: }
1.163 vatton 1850: if (!TtaIsSelectionEmpty())
1851: {
1852: TtaInsertAnyElement (doc, TRUE);
1853: extendUndo = TRUE;
1854: }
1855: else
1856: extendUndo = FALSE;
1.114 vatton 1857: if (!HTMLelementAllowed (doc))
1858: /* the creation of an HTML element is not allowed here */
1859: return;
1860:
1.162 quint 1861: root = TtaGetMainRoot (doc);
1862: elType.ElTypeNum = HTML_EL_H1;
1863: manyH1 = FALSE;
1864: lH1 = TtaSearchTypedElement (elType, SearchForward, root);
1865: if (lH1)
1866: {
1867: if (TtaSearchTypedElement (elType, SearchForward, lH1))
1.163 vatton 1868: /* there are at least 2 H1 elements in the document */
1869: manyH1 = TRUE;
1.162 quint 1870: }
1871:
1.114 vatton 1872: attrType.AttrSSchema = elType.ElSSchema;
1873: ulType.ElSSchema = elType.ElSSchema;
1.115 vatton 1874: ulType.ElTypeNum = HTML_EL_Unnumbered_List;
1.162 quint 1875: toc = lH1 = lH2 = lH3 = lH4 = lH5 = lH6 = prev = NULL;
1.114 vatton 1876: list = NULL;
1.148 vatton 1877: /* check if the insert point is already within a table of contents */
1878: ancest = el;
1879: found = FALSE;
1880: while (ancest &&
1.152 vatton 1881: (elType.ElSSchema != ulType.ElSSchema ||
1882: elType.ElTypeNum != HTML_EL_Division))
1.148 vatton 1883: {
1884: if (!found && !strcmp (s, "Template"))
1885: // cross an enclosing XTiger element
1886: found = TRUE;
1887: ancest = TtaGetParent (ancest);
1888: if (ancest)
1889: {
1890: elType = TtaGetElementType (ancest);
1891: s = TtaGetSSchemaName (elType.ElSSchema);
1892: }
1893: }
1894: if (ancest)
1895: {
1896: // check if that division has the toc class
1897: attrType.AttrTypeNum = HTML_ATTR_Class;
1898: attr = TtaGetAttribute (ancest, attrType);
1899: if (attr)
1900: {
1901: i = TtaGetTextAttributeLength (attr);
1902: if (i == 3)
1903: {
1904: value = (char *)TtaGetMemory (i + 1);
1905: TtaGiveTextAttributeValue(attr, value, &i);
1906: if (strcmp (value, "toc"))
1907: attr = NULL;
1.150 vatton 1908: TtaFreeMemory (value);
1909: value = NULL;
1.148 vatton 1910: }
1911: else
1912: attr = NULL;
1913: }
1914: if (attr)
1915: {
1916: if (found)
1.149 vatton 1917: {
1918: TtaDisplaySimpleMessage (CONFIRM, LIB, TMSG_EL_RO);
1919: return;
1920: }
1.148 vatton 1921: else
1922: toc = ancest;
1923: }
1924: }
1925:
1926: if (dispMode == DisplayImmediately)
1927: TtaSetDisplayMode (doc, SuspendDisplay);
1928:
1.155 vatton 1929: if (TtaHasUndoSequence (doc))
1.148 vatton 1930: closeUndo = FALSE;
1931: else
1.114 vatton 1932: {
1.148 vatton 1933: closeUndo = TRUE;
1.163 vatton 1934: if (extendUndo)
1935: TtaExtendUndoSequence (doc);
1936: else
1937: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.148 vatton 1938: }
1939:
1940: if (toc)
1941: {
1942: // replace the old toc by the new one
1943: child = TtaGetFirstChild (toc);
1944: TtaRegisterElementDelete (child, doc);
1945: TtaRemoveTree (child, doc);
1.156 vatton 1946: TtaSetDocumentModified (doc);
1.148 vatton 1947: }
1948:
1.162 quint 1949: searchedType1.ElSSchema = elType.ElSSchema;
1950: searchedType2.ElSSchema = elType.ElSSchema;
1951: searchedType3.ElSSchema = elType.ElSSchema;
1952: searchedType4.ElSSchema = elType.ElSSchema;
1953: searchedType5.ElSSchema = elType.ElSSchema;
1954: if (manyH1)
1955: {
1956: searchedType1.ElTypeNum = HTML_EL_H1;
1957: searchedType2.ElTypeNum = HTML_EL_H2;
1958: searchedType3.ElTypeNum = HTML_EL_H3;
1959: searchedType4.ElTypeNum = HTML_EL_H4;
1960: searchedType5.ElTypeNum = HTML_EL_H5;
1961: }
1962: else
1963: {
1964: searchedType1.ElTypeNum = HTML_EL_H2;
1965: searchedType2.ElTypeNum = HTML_EL_H3;
1966: searchedType3.ElTypeNum = HTML_EL_H4;
1967: searchedType4.ElTypeNum = HTML_EL_H5;
1968: searchedType5.ElTypeNum = HTML_EL_H6;
1969: }
1970:
1.148 vatton 1971: // keep in memory the current selected element
1972: ancest = el;
1.162 quint 1973: el = root;
1.148 vatton 1974: while (el)
1975: {
1976: el = TtaSearchElementAmong5Types (searchedType1, searchedType2,
1977: searchedType3, searchedType4,
1978: searchedType5, SearchForward, el);
1979: if (el)
1.135 vatton 1980: {
1.148 vatton 1981: if (toc == NULL)
1.135 vatton 1982: {
1.151 quint 1983: /* generate the enclosing division */
1.148 vatton 1984: elType.ElTypeNum = HTML_EL_Division;
1.157 vatton 1985: if (TtaInsertElement (elType, doc))
1.135 vatton 1986: {
1.157 vatton 1987: TtaGiveFirstSelectedElement (doc, &child, &firstChar, &i);
1.148 vatton 1988: /* the div and its initial content is now created */
1.142 vatton 1989: toc = TtaGetTypedAncestor (child, elType);
1990: TtaRegisterElementDelete (child, doc);
1991: TtaRemoveTree (child, doc);
1.135 vatton 1992: }
1.142 vatton 1993: else
1.135 vatton 1994: {
1.157 vatton 1995: // the division is not created
1.148 vatton 1996: if (closeUndo)
1997: TtaCloseUndoSequence (doc);
1998: if (dispMode == DisplayImmediately)
1999: TtaSetDisplayMode (doc, dispMode);
1.149 vatton 2000: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_NOT_ALLOWED);
1.148 vatton 2001: return;
2002: }
1.157 vatton 2003:
1.148 vatton 2004: if (toc)
2005: {
2006: /* it's the last created element */
2007: attrType.AttrTypeNum = HTML_ATTR_Class;
2008: attr = TtaNewAttribute (attrType);
2009: TtaAttachAttribute (toc, attr, doc);
2010: TtaSetAttributeText (attr, "toc", toc, doc);
2011: TtaRegisterAttributeCreate (attr, toc, doc);
2012: }
2013: }
1.157 vatton 2014:
1.148 vatton 2015: if (toc == NULL)
2016: el = NULL;
2017: else
2018: {
2019: /* does the element have an ID attribute already? */
2020: attrType.AttrTypeNum = HTML_ATTR_ID;
2021: attr = TtaGetAttribute (el, attrType);
2022: if (!attr)
2023: {
2024: /* generate the ID if it does't exist */
1.164 ! vatton 2025: CreateTargetAnchor (doc, el, TRUE, TRUE, TRUE);
1.135 vatton 2026: attr = TtaGetAttribute (el, attrType);
1.148 vatton 2027: }
2028: i = TtaGetTextAttributeLength (attr) + 1;
2029: id = (char *)TtaGetMemory (i + 1);
2030: id[0] = '#';
2031: TtaGiveTextAttributeValue (attr, &id[1], &i);
1.114 vatton 2032:
1.148 vatton 2033: /* locate or generate the list */
2034: elType = TtaGetElementType (el);
1.162 quint 2035:
2036: if (elType.ElTypeNum == HTML_EL_H1)
1.148 vatton 2037: {
2038: parent = toc;
1.162 quint 2039: lH2 = lH3 = lH4 = lH5 = NULL;
2040: list = &lH1;
2041: }
2042: else if (elType.ElTypeNum == HTML_EL_H2)
2043: {
2044: if (manyH1)
2045: {
2046: if (lH1)
2047: parent = TtaGetLastChild (lH1);
2048: else
2049: parent = toc;
2050: lH3 = lH4 = lH5 = NULL;
2051: list = &lH2;
2052: }
2053: else
2054: {
2055: parent = toc;
2056: lH3 = lH4 = lH5 = lH6 = NULL;
2057: list = &lH2;
2058: }
1.148 vatton 2059: }
2060: else if (elType.ElTypeNum == HTML_EL_H3)
2061: {
1.162 quint 2062: if (manyH1)
2063: {
2064: if (lH2)
2065: parent = TtaGetLastChild (lH2);
2066: else if (lH3)
2067: parent = TtaGetLastChild (lH3);
2068: else
2069: parent = toc;
2070: lH4 = lH5 = NULL;
2071: list = &lH3;
2072: }
2073: else
2074: {
2075: if (lH2)
2076: parent = TtaGetLastChild (lH2);
2077: else
2078: parent = toc;
2079: lH4 = lH5 = lH6 = NULL;
2080: list = &lH3;
2081: }
1.148 vatton 2082: }
2083: else if (elType.ElTypeNum == HTML_EL_H4)
2084: {
1.162 quint 2085: if (manyH1)
2086: {
2087: if (lH3)
2088: parent = TtaGetLastChild (lH3);
2089: else if (lH2)
2090: parent = TtaGetLastChild (lH2);
2091: else if (lH1)
2092: parent = TtaGetLastChild (lH1);
2093: else
2094: parent = toc;
2095: lH5 = NULL;
2096: list = &lH4;
2097: }
2098: else
2099: {
2100: if (lH3)
2101: parent = TtaGetLastChild (lH3);
2102: else if (lH2)
2103: parent = TtaGetLastChild (lH2);
2104: else
2105: parent = toc;
2106: lH5 = lH6 = NULL;
2107: list = &lH4;
2108: }
1.148 vatton 2109: }
2110: else if (elType.ElTypeNum == HTML_EL_H5)
2111: {
1.162 quint 2112: if (manyH1)
2113: {
2114: if (lH4)
2115: parent = TtaGetLastChild (lH4);
2116: else if (lH3)
2117: parent = TtaGetLastChild (lH3);
2118: else if (lH2)
2119: parent = TtaGetLastChild (lH2);
2120: else if (lH1)
2121: parent = TtaGetLastChild (lH1);
2122: else
2123: parent = toc;
2124: list = &lH5;
2125: }
2126: else
2127: {
2128: if (lH4)
2129: parent = TtaGetLastChild (lH4);
2130: else if (lH3)
2131: parent = TtaGetLastChild (lH3);
2132: else if (lH2)
2133: parent = TtaGetLastChild (lH2);
2134: else
2135: parent = toc;
2136: lH6 = NULL;
2137: list = &lH5;
2138: }
1.148 vatton 2139: }
2140: else if (elType.ElTypeNum == HTML_EL_H6)
2141: {
1.162 quint 2142: if (lH5)
2143: parent = TtaGetLastChild (lH5);
2144: else if (lH4)
2145: parent = TtaGetLastChild (lH4);
2146: else if (lH3)
2147: parent = TtaGetLastChild (lH3);
2148: else if (lH2)
2149: parent = TtaGetLastChild (lH2);
2150: else
2151: parent = toc;
2152: list = &lH6;
1.148 vatton 2153: }
2154:
2155: if (*list == NULL)
2156: {
2157: /* generate the list */
2158: *list = TtaNewElement (doc, ulType);
2159: child = TtaGetLastChild (parent);
2160: if (child)
2161: TtaInsertSibling (*list, child, FALSE, doc);
2162: else
2163: TtaInsertFirstChild (list, parent, doc);
2164: TtaRegisterElementCreate (*list, doc);
2165: }
2166: /* generate the list item */
2167: elType.ElTypeNum = HTML_EL_List_Item;
2168: item = TtaNewElement (doc, elType);
2169: /* generate the HTML_EL_Pseudo_paragraph */
2170: elType.ElTypeNum = HTML_EL_Pseudo_paragraph;
2171: parent = TtaNewElement (doc, elType);
2172: TtaInsertFirstChild (&parent, item, doc);
2173: /* generate the link anchor */
2174: elType.ElTypeNum = HTML_EL_Anchor;
2175: new_ = TtaNewElement (doc, elType);
2176: TtaInsertFirstChild (&new_, parent, doc);
2177: attrType.AttrTypeNum = HTML_ATTR_HREF_;
2178: attr = TtaNewAttribute (attrType);
2179: TtaAttachAttribute (new_, attr, doc);
2180: TtaSetAttributeText (attr, id, new_, doc);
2181: TtaFreeMemory (id);
2182: id = NULL;
2183: /* get a copy of the Hi contents */
2184: srce = TtaGetFirstChild (el);
2185: prev = NULL;
2186: parent = NULL;
2187: while (srce)
2188: {
2189: copyType = TtaGetElementType (srce);
2190: s = TtaGetSSchemaName (copyType.ElSSchema);
2191: while (srce && !TtaIsLeaf (copyType) &&
2192: !strcmp (s, "Template"))
1.142 vatton 2193: {
1.148 vatton 2194: /* copy the anchor contents instead of the anchor */
2195: if (parent == NULL)
2196: parent = srce;
2197: srce = TtaGetFirstChild (srce);
2198: if (srce)
2199: {
2200: copyType = TtaGetElementType (srce);
2201: s = TtaGetSSchemaName (copyType.ElSSchema);
2202: }
1.142 vatton 2203: }
1.148 vatton 2204: if (srce &&
2205: copyType.ElTypeNum == HTML_EL_Anchor &&
2206: copyType.ElSSchema == elType.ElSSchema)
1.142 vatton 2207: {
1.148 vatton 2208: /* copy the anchor contents instead of the anchor */
2209: if (parent == NULL)
2210: parent = srce;
2211: srce = TtaGetFirstChild (srce);
1.142 vatton 2212: }
1.148 vatton 2213: if (srce)
1.142 vatton 2214: {
1.148 vatton 2215: /* copy children of the next source */
2216: copy = TtaCopyTree (srce, doc, doc, new_);
2217: if (copy)
2218: {
2219: if (prev == NULL)
2220: /* this is the first copied element. Insert it before elem */
2221: TtaInsertFirstChild (©, new_, doc);
2222: else
2223: /* insert the new copied element after the element previously
2224: copied */
2225: TtaInsertSibling (copy, prev, FALSE, doc);
2226: prev = copy;
2227: }
2228: TtaNextSibling (&srce);
1.142 vatton 2229: }
1.148 vatton 2230: if (srce == NULL && parent)
1.142 vatton 2231: {
1.148 vatton 2232: /* copy children of an anchor */
2233: srce = parent;
2234: parent = NULL;
1.135 vatton 2235: if (srce)
1.148 vatton 2236: TtaNextSibling (&srce);
1.135 vatton 2237: }
2238: }
1.148 vatton 2239: child = TtaGetLastChild (*list);
2240: if (child)
2241: TtaInsertSibling (item, child, FALSE, doc);
2242: else
2243: TtaInsertFirstChild (&item, *list, doc);
2244: TtaRegisterElementCreate (item, doc);
1.135 vatton 2245: }
2246: }
1.114 vatton 2247: }
1.148 vatton 2248:
1.114 vatton 2249: if (closeUndo)
2250: TtaCloseUndoSequence (doc);
1.137 vatton 2251:
2252: if (toc == NULL)
1.142 vatton 2253: {
2254: if (dispMode == DisplayImmediately)
2255: TtaSetDisplayMode (doc, dispMode);
2256: TtaDisplaySimpleMessage (CONFIRM, AMAYA, AM_NO_HEADING_FOUND);
2257: }
1.137 vatton 2258: else
1.115 vatton 2259: {
1.137 vatton 2260: /* force a complete redisplay to apply CSS */
2261: TtaSetDisplayMode (doc, NoComputedDisplay);
2262: if (dispMode == DisplayImmediately)
2263: TtaSetDisplayMode (doc, dispMode);
2264: /* select the end of the toc */
2265: if (prev)
1.135 vatton 2266: {
1.137 vatton 2267: child = prev;
2268: while (child)
2269: {
2270: child = TtaGetLastChild (prev);
2271: if (child)
2272: prev = child;
2273: }
2274: elType = TtaGetElementType (prev);
2275: if (elType.ElTypeNum == HTML_EL_TEXT_UNIT)
2276: {
2277: i = TtaGetElementVolume (prev);
2278: TtaSelectString (doc, prev, i+1, i);
2279: }
2280: else
2281: TtaSelectElement (doc, prev);
1.135 vatton 2282: }
1.115 vatton 2283: }
1.114 vatton 2284: }
Webmaster