Annotation of libwww/LineMode/src/GridText.c, revision 1.22
1.13 frystyk 1: /* GridText.c
2: ** CHARACTER GRID HYPERTEXT OBJECT
3: **
1.17 frystyk 4: ** (c) COPYRIGHT MIT 1995.
1.13 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
6: **
1.1 timbl 7: */
8:
1.16 frystyk 9: #include <assert.h>
10:
11: #include "WWWLib.h"
1.19 frystyk 12: #include "HTBrowse.h"
1.16 frystyk 13: #include "HTFont.h"
14: #include "GridStyle.h"
15: #include "GridText.h"
1.1 timbl 16:
1.11 howcome 17: /* HWL 18/7/94: applied patch from agl@glas2.glas.apc.org (Anton Tropashko) */
18: #ifdef CYRILLIC
19: #include "a_stdio.h"
20: #endif
21:
1.1 timbl 22: #define MAX_LINE HTScreenWidth /* No point in accumulating more */
1.16 frystyk 23: #define LOADED_LIMIT 6 /* For now, save last five texts */
1.1 timbl 24:
25: #ifdef CURSES
26: #define DISPLAY_LINES (HTScreenHeight)
27: #define TITLE_LINES 0
28: #else
1.19 frystyk 29: #define DISPLAY_LINES (HTScreenHeight - 2) /* Exclude prompt line */
1.1 timbl 30: #define TITLE_LINES 1
31: #endif
32:
1.19 frystyk 33: struct _HTStream { /* only know it as object */
34: CONST HTStreamClass * isa;
35: /* ... */
36: };
1.1 timbl 37:
38: /* From default style sheet:
39: */
40: extern HTStyleSheet * styleSheet; /* Default or overridden */
41:
42: /* Exports
43: */
44: PUBLIC HText * HTMainText = 0; /* Equivalent of main window */
45: PUBLIC HTParentAnchor * HTMainAnchor = 0; /* Anchor for HTMainText */
46:
47: typedef struct _line {
48: struct _line *next;
49: struct _line *prev;
50: short unsigned offset; /* Implicit initial spaces */
51: short unsigned size; /* Number of characters */
52: BOOL split_after; /* Can we split after? */
53: BOOL bullet; /* Do we bullet? */
54: char data[1]; /* Space for terminator at least! */
55: } HTLine;
56:
57: #define LINE_SIZE(l) (sizeof(HTLine)+(l)) /* allow for terminator */
58:
59: typedef struct _TextAnchor {
60: struct _TextAnchor * next;
61: int number; /* For user interface */
62: int start; /* Characters */
63: int extent; /* Characters */
64: HTChildAnchor * anchor;
65: } TextAnchor;
66:
67:
68: /* Notes on struct _Htext:
69: ** next_line is valid iff state is false.
70: ** top_of_screen line means the line at the top of the screen
71: ** or just under the title if there is one.
72: */
73: struct _HText {
74: HTParentAnchor * node_anchor;
75: char * title;
76: HTLine * last_line;
77: int lines; /* Number of them */
78: int chars; /* Number of them */
79: TextAnchor * first_anchor; /* Singly linked list */
80: TextAnchor * last_anchor;
81: int last_anchor_number; /* user number */
82: /* For Internal use: */
83: HTStyle * style; /* Current style */
84: int display_on_the_fly; /* Lines left */
1.3 timbl 85: BOOL all_pages; /* Loop on the fly */
1.1 timbl 86: int top_of_screen; /* Line number */
87: HTLine * top_of_screen_line; /* Top */
88: HTLine * next_line; /* Bottom + 1 */
89: int permissible_split; /* in last line */
90: BOOL in_line_1; /* of paragraph */
91: BOOL stale; /* Must refresh */
92:
93: HTStream* target; /* Output stream */
94: HTStreamClass targetClass; /* Output routines */
95: };
96:
97:
98: #define PUTC(c) (*text->targetClass.put_character)(text->target, c)
99: #define PUTS(s) (*text->targetClass.put_string)(text->target, s)
100:
101: /* Boring static variable used for moving cursor across
102: */
103:
104: #define SPACES(n) (&space_string[HTScreenWidth - (n)])
105: /* String containing blank spaces only */
106: PRIVATE char * space_string;
107:
108: PRIVATE HTStyle default_style =
109: { 0, "(Unstyled)", "",
110: (HTFont)0, 1.0, HT_BLACK, 0, 0,
111: 0, 0, 0, HT_LEFT, 1, 0, 0,
112: NO, NO, 0, 0, 0 };
113:
114:
115: PUBLIC void clear_screen NOPARAMS; /* Forward */
116:
117: PRIVATE HTList * loaded_texts; /* A list of all those in memory */
118:
119: /* Creation Method
120: ** ---------------
1.3 timbl 121: **
122: ** Interactive version
123: **
1.1 timbl 124: */
125: PUBLIC HText * HText_new ARGS1(HTParentAnchor *,anchor)
126: {
127: HTLine * line;
128: HText * self = (HText *) malloc(sizeof(*self));
129: if (!self) return self;
130:
131: if (!loaded_texts) loaded_texts = HTList_new();
132: HTList_addObject(loaded_texts, self);
133: if (HTList_count(loaded_texts) >= LOADED_LIMIT) {
1.20 frystyk 134: if (CACHE_TRACE)
135: fprintf(TDEST, "MemoryCache. Freeing off cached doc.\n");
1.1 timbl 136: HText_free((HText *)HTList_removeFirstObject(loaded_texts));
137: }
138:
139: line = self->last_line = (HTLine *)malloc(LINE_SIZE(MAX_LINE));
140: if (line == NULL) outofmem(__FILE__, "HText_New");
141: line->next = line->prev = line;
142: line->offset = line->size = 0;
143: self->lines = self->chars = 0;
144: self->title = 0;
145: self->first_anchor = self->last_anchor = 0;
146: self->style = &default_style;
147: self->top_of_screen = 0;
148: self->node_anchor = anchor;
149: self->last_anchor_number = 0; /* Numbering of them for references */
150: self->stale = YES;
151:
152: self->target = NULL;
153:
154: HTAnchor_setDocument(anchor, (HyperDoc *)self);
155:
156: clear_screen();
157: HTMainText = self;
158: HTMainAnchor = anchor;
159: self->display_on_the_fly = DISPLAY_LINES;
1.3 timbl 160: self->all_pages = NO; /* One page at a time on the fly */
1.1 timbl 161:
162: if (!space_string) { /* Make a blank line */
163: char *p;
164: space_string = (char *)malloc(HTScreenWidth+1);
165: if (space_string == NULL) outofmem(__FILE__, "HText_New");
166: for (p=space_string; p<space_string+HTScreenWidth; p++)
167: *p = ' '; /* Used for printfs later */
168: space_string[HTScreenWidth] = '\0';
169: }
170:
171: return self;
172: }
173:
174:
175: /* Creation Method 2
176: ** ---------------
177: **
1.4 timbl 178: ** Non-interative OR interactive if stream is NULL
1.1 timbl 179: ** Stream is assumed open and left open.
180: */
181: PUBLIC HText * HText_new2 ARGS2(
182: HTParentAnchor *, anchor,
183: HTStream*, stream)
184:
185: {
1.3 timbl 186: HText * me = HText_new(anchor);
1.1 timbl 187:
188: if (stream) {
1.3 timbl 189: me->target = stream;
190: me->targetClass = *stream->isa; /* copy action procedures */
1.4 timbl 191: me->all_pages = YES; /* Display whole file on the fly */
1.3 timbl 192: }
193: return me;
1.1 timbl 194: }
195:
1.18 frystyk 196: /* free a hyperdoc object
197: ** ---------------------
1.1 timbl 198: */
1.18 frystyk 199: PUBLIC void hyperfree ARGS1(HText *, self)
1.1 timbl 200: {
201: while(YES) { /* Free off line array */
202: HTLine * l = self->last_line;
203: l->next->prev = l->prev;
204: l->prev->next = l->next; /* Unlink l */
205: self->last_line = l->prev;
206: free(l);
207: if (l == self->last_line) break; /* empty */
208: };
209:
210: while(self->first_anchor) { /* Free off anchor array */
211: TextAnchor * l = self->first_anchor;
212: self->first_anchor = l->next;
213: free(l);
214: }
215: free(self);
1.8 frystyk 216: if (self == HTMainText) /* Henrik 24/02-94 */
217: HTMainText = NULL;
1.18 frystyk 218: }
219:
220:
221: /* Free Entire Text
222: ** ----------------
223: */
224: PUBLIC void HText_free ARGS1(HText *,self)
225: {
226: HTAnchor_setDocument(self->node_anchor, (HyperDoc *)0);
227: hyperfree(self);
1.1 timbl 228: }
229:
230:
231: /* Display Methods
232: ** ---------------
233: */
1.22 ! frystyk 234:
1.1 timbl 235: /* Clear the screen (on screen-mode systems)
236: ** ----------------
237: */
238: PUBLIC void clear_screen NOARGS
239: {
1.22 ! frystyk 240: if (TRACE)
! 241: return; /* in trace mode, don't clear trace away */
1.1 timbl 242: #ifdef CURSES
243: if (w_text != NULL) {
1.22 ! frystyk 244: wmove(w_text,0,0);
! 245: wclear(w_text);
1.1 timbl 246: }
1.22 ! frystyk 247: #endif /* Not CURSES */
1.1 timbl 248: if (HTMainText) HTMainText->stale = YES;
249: }
250:
251:
252: /* Output a line
253: ** -------------
254: */
255: PRIVATE void display_line ARGS2(HText *,text, HTLine *,line)
256: {
257: #ifdef CURSES
258: int y, x;
259:
260: waddstr(w_text, SPACES(line->offset));
261: waddstr(w_text, line->data);
262: getyx(w_text, y, x);
263: if (y < DISPLAY_LINES-1) {
264: wmove(w_text, ++y, 0);
265: }
266: #else
267: if (!text->target)
1.11 howcome 268: {
269: #ifdef CYRILLIC
270: /* HWL 18/7/94: applied patch from agl@glas2.glas.apc.org (Anton Tropashko) */
271: a_print(SPACES(line->offset),H,stdout);
272: a_print(line->data,H,stdout);
273: fputc('\n',stdout);
274: #else
1.1 timbl 275: printf("%s%s\n", SPACES(line->offset), line->data);
1.11 howcome 276: #endif
277: }
1.1 timbl 278: else {
279: PUTS(SPACES(line->offset));
280: PUTS(line->data);
281: PUTC('\n');
282: }
283: #endif
284:
285: }
286:
287: /* Output the title line
288: ** ---------------------
289: */
290: PRIVATE void display_title ARGS1(HText *,text)
291: {
292: CONST char * title = HTAnchor_title(text->node_anchor);
293: char percent[20], format[20];
294: if (text->lines > (DISPLAY_LINES-1)) {
295: #ifdef NOPE
296: sprintf(percent, " (p%d of %d)",
297: (text->top_of_screen/(DISPLAY_LINES-1)) + 1,
298: (text->lines-1)/(DISPLAY_LINES-1) + 1);
299: sprintf(percent, " (%d%%)",
300: 100*(text->top_of_screen+DISPLAY_LINES-1)/ /* Seen */
301: (text->lines)); /* Total */
302: #else
303: sprintf(percent, " (%d/%d)",
304: text->top_of_screen+DISPLAY_LINES-1, /* Seen */
305: text->lines); /* Total */
306: #endif
307: } else {
308: percent[0] = 0; /* Null string */
309: }
310:
311: sprintf(format, "%%%d.%ds%%s\n", /* Generate format string */
312: (int)(HTScreenWidth-strlen(percent)),
313: (int)(HTScreenWidth-strlen(percent)) );
314: #ifdef CURSES
315: mvwprintw(w_top, 0, 0, format, title, percent);
316: wrefresh(w_top);
317: #else
318: if (!text->target) printf(format, title, percent);
319: else {
320: char * line = (char*)malloc(HTScreenWidth+10);
321: sprintf(line, format, title, percent);
322: PUTS(line);
323: free(line);
324: }
325: #endif
326: }
327:
328:
329: /* Fill the screen with blank after the file
330: ** --------------------------
331: */
332: PRIVATE void fill_screen ARGS2(HText *, text, int,n)
333: {
334: if (n<=0 || n>1000) return; /* Large value means no pagination */
335: if (text->target) return;
336: #ifdef CURSES
337: waddstr(w_text, end_mark);
338: wclrtobot(w_text);
339: wrefresh(w_text);
340: #else
341: #ifndef VIOLA
342: if (!text->target) printf("%s\n", end_mark);
343: else { PUTS(end_mark); PUTC('\n'); }
344: n--;
345:
346: for (; n; n--) {
347: if (!text->target) printf("\n");
348: else PUTC('\n');
349: }
350: #endif
351: #endif /* Not CURSES */
352: }
353:
354:
355: /* Output a page
356: ** -------------
357: */
358: PRIVATE void display_page ARGS2(HText *,text, int,line_number)
359: {
360: HTLine * line = text->last_line->prev;
361: int i;
362: CONST char * title = HTAnchor_title(text->node_anchor);
363: int lines_of_text = title ? (DISPLAY_LINES-TITLE_LINES) : DISPLAY_LINES;
364: int last_screen = text->lines - lines_of_text;
365:
366: /* Constrain the line number to be within the document
367: */
368: if (text->lines <= lines_of_text) line_number = 0;
369: else if (line_number>last_screen) line_number = last_screen;
370: else if (line_number < 0) line_number = 0;
371:
372: for(i=0, line = text->last_line->next; /* Find line */
373: i<line_number && (line!=text->last_line);
374: i++, line=line->next) /* Loop */ assert(line->next != NULL);
375:
376: while((line!=text->last_line) && (line->size==0)) { /* Skip blank lines */
377: assert(line->next != NULL);
378: line = line->next;
379: line_number ++;
380: }
381:
382: /* Can we just scroll to it?
383: */
384: #ifndef VM /* No scrolling */
385: if (!text->stale && (line_number>=text->top_of_screen) &&
386: (line_number < text->top_of_screen + DISPLAY_LINES)) { /* Yes */
387: lines_of_text = line_number - text->top_of_screen;
388: line = text->next_line;
389: text->top_of_screen = line_number;
390: #ifdef CURSES
391: scrollok(w_text, TRUE);
392: for (i = 0; i < lines_of_text; i++) {
393: scroll(w_text);
394: }
395: #endif
396: } else
397: #endif
398: {
399: clear_screen(); /* No */
400: text->top_of_screen = line_number;
401: if (title) display_title(text);
402: }
403:
404: /* Bug: when we scroll to a part slightly futher down a page which previously
405: fitted all on one screen including the [End], the code below will add an
406: extra [End] to the screen, giving a total of two, one underneath the other.
407: */
408:
409: /* print it
410: */
411: if (line) {
412: for(i=0;
413: (i< lines_of_text) && (line != text->last_line); i++) {
414: assert(line != NULL);
415: display_line(text, line);
416: line = line->next;
417: }
418: fill_screen(text, lines_of_text - i);
419: }
420:
421: text->next_line = line; /* Line after screen */
422: text->stale = NO; /* Display is up-to-date */
423: }
424:
425:
426: /* Object Building methods
427: ** -----------------------
428: **
429: ** These are used by a parser to build the text in an object
430: */
431: PUBLIC void HText_beginAppend ARGS1(HText *,text)
432: {
433: text->permissible_split = 0;
434: text->in_line_1 = YES;
435: }
436:
437:
438: /* Add a new line of text
439: ** ----------------------
440: **
441: ** On entry,
442: **
443: ** split is zero for newline function, else number of characters
444: ** before split.
445: ** text->display_on_the_fly
446: ** may be set to indicate direct output of the finished line.
1.3 timbl 447: ** text->all_pages
448: ** if set indicates all pages are to be done on the fly.
1.1 timbl 449: ** On exit,
450: ** A new line has been made, justified according to the
451: ** current style. Text after the split (if split nonzero)
452: ** is taken over onto the next line.
453: **
454: ** If display_on_the_fly is set, then it is decremented and
455: ** the finished line is displayed.
456: */
457: #define new_line(text) split_line(text, 0)
458:
459: PRIVATE void split_line ARGS2(HText *,text, int,split)
460: {
461: HTStyle * style = text->style;
462: int spare;
1.14 frystyk 463: int indent = (int) (text->in_line_1 ? text->style->indent1st
464: : text->style->leftIndent);
1.7 frystyk 465:
1.1 timbl 466: /* Make new line
467: */
468: HTLine * previous = text->last_line;
469: HTLine * line = (HTLine *) malloc(LINE_SIZE(MAX_LINE));
470:
471: if (line == NULL) outofmem(__FILE__, "split_line");
472: text->lines++;
473:
474: previous->next->prev = line;
475: line->prev = previous;
476: line->next = previous->next;
477: previous->next = line;
478: text->last_line = line;
479: line->size = 0;
480: line->offset = 0;
481:
482: /* Split at required point
483: */
484: if (split) { /* Delete space at "split" splitting line */
485: char * p;
486: previous->data[previous->size] = 0;
487: for (p = &previous->data[split]; *p; p++)
488: if (*p != ' ') break;
489: strcpy(line->data, p);
490: line->size = strlen(line->data);
491: previous->size = split;
492: }
493:
494: /* Economise on space.
495: ** Not on the RS6000 due to a chaotic bug in realloc argument passing.
496: ** Same problem with Ultrix (4.2) : realloc() is not declared properly.
497: */
1.16 frystyk 498: #ifndef AIX
499: #ifndef ultrix
1.1 timbl 500: while ((previous->size > 0) &&
501: (previous->data[previous->size-1] == ' ')) /* Strip trailers */
502: previous->size--;
1.7 frystyk 503:
1.1 timbl 504: previous = (HTLine *) realloc (previous, LINE_SIZE(previous->size));
505: if (previous == NULL) outofmem(__FILE__, "split_line");
1.16 frystyk 506: #endif /* ultrix */
507: #endif /* AIX */
1.1 timbl 508:
509: previous->prev->next = previous; /* Link in new line */
510: previous->next->prev = previous; /* Could be same node of course */
511:
512: /* Terminate finished line for printing
513: */
514: previous->data[previous->size] = 0;
1.7 frystyk 515:
1.1 timbl 516: /* Align left, right or center
517: */
518:
1.14 frystyk 519: spare = (int) (HTScreenWidth - style->rightIndent + style->leftIndent -
520: previous->size); /* @@ first line indent */
1.1 timbl 521:
522: switch (style->alignment) {
523: case HT_CENTER :
524: previous->offset = previous->offset + indent + spare/2;
525: break;
526: case HT_RIGHT :
527: previous->offset = previous->offset + indent + spare;
528: break;
529: case HT_LEFT :
530: case HT_JUSTIFY : /* Not implemented */
531: default:
532: previous->offset = previous->offset + indent;
533: break;
534: } /* switch */
535:
536: text->chars = text->chars + previous->size + 1; /* 1 for the line */
537: text->in_line_1 = NO; /* unless caller sets it otherwise */
538:
539: /* If displaying as we go, output it:
540: */
541: if (text->display_on_the_fly) {
542: if (text->display_on_the_fly == DISPLAY_LINES) { /* First line */
543: if (previous->size == 0) {
544: text->top_of_screen++; /* Scroll white space off top */
545: return;
546: }
547: if (HTAnchor_title(text->node_anchor)) { /* Title exists */
548: display_title(text);
549: text->display_on_the_fly--;
550: }
551: }
552: display_line(text, previous);
553: text->display_on_the_fly--;
1.3 timbl 554:
555: /* Loop to top of next page? */
556: if (!text->display_on_the_fly && text->all_pages) {
557: PUTS("\f\n"); /* Form feed on its own line a la rfc1111 */
558: text->display_on_the_fly = DISPLAY_LINES;
559: }
1.1 timbl 560: }
561: } /* split_line */
562:
563:
564: /* Allow vertical blank space
565: ** --------------------------
566: */
567: PRIVATE void blank_lines ARGS2(HText *,text, int,newlines)
568: {
569: if (text->last_line->size == 0) { /* No text on current line */
570: HTLine * line = text->last_line->prev;
571: while ((line!=text->last_line) && (line->size == 0)) {
572: if (newlines==0) break;
573: newlines--; /* Don't bother: already blank */
574: line = line->prev;
575: }
576: } else {
577: newlines++; /* Need also to finish this line */
578: }
579:
580: for(;newlines;newlines--) {
581: new_line(text);
582: }
583: text->in_line_1 = YES;
584: }
585:
586:
587: /* New paragraph in current style
588: ** ------------------------------
589: ** See also: setStyle.
590: */
591:
592: PUBLIC void HText_appendParagraph ARGS1(HText *,text)
593: {
1.14 frystyk 594: int after = (int) text->style->spaceAfter;
595: int before = (int) text->style->spaceBefore;
1.1 timbl 596: blank_lines(text, after>before ? after : before);
597: }
598:
599:
600: /* Set Style
601: ** ---------
602: **
603: ** Does not filter unnecessary style changes.
604: */
605: PUBLIC void HText_setStyle ARGS2(HText *,text, HTStyle *,style)
606: {
607: int after, before;
608:
609: if (!style) return; /* Safety */
1.14 frystyk 610: after = (int) text->style->spaceAfter;
611: before = (int) style->spaceBefore;
1.20 frystyk 612: if (SGML_TRACE)
613: fprintf(TDEST, "HTML: Change to style %s\n", style->name);
1.1 timbl 614: blank_lines (text, after>before ? after : before);
615: text->style = style;
616: }
617:
618:
619: /* Append a character to the text object
620: ** -------------------------------------
621: */
622: PUBLIC void HText_appendCharacter ARGS2(HText *,text, char,ch)
623: {
624: HTLine * line = text->last_line;
625: HTStyle * style = text->style;
1.14 frystyk 626: int indent = (int)(text->in_line_1 ? style->indent1st : style->leftIndent);
1.1 timbl 627:
628: /* New Line
629: */
630: if (ch == '\n') {
631: new_line(text);
632: text->in_line_1 = YES; /* First line of new paragraph */
633: return;
634: }
635:
636: /* Tabs
637: */
638:
639: if (ch == '\t') {
640: HTTabStop * tab;
641: int target; /* Where to tab to */
642: int here = line->size + line->offset +indent;
643: if (style->tabs) { /* Use tab table */
644: for (tab = style->tabs;
645: tab->position <= here;
646: tab++)
647: if (!tab->position) {
648: new_line(text);
649: return;
650: }
1.14 frystyk 651: target = (int) tab->position;
1.1 timbl 652: } else if (text->in_line_1) { /* Use 2nd indent */
653: if (here >= style->leftIndent) {
654: new_line(text); /* wrap */
655: return;
656: } else {
1.14 frystyk 657: target = (int) style->leftIndent;
1.1 timbl 658: }
659: } else { /* Default tabs align with left indent mod 8 */
660: #ifdef DEFAULT_TABS_8
661: target = ((line->offset + line->size + 8) & (-8))
662: + style->leftIndent;
663: #else
664: new_line(text);
665: return;
666: #endif
667: }
668: if (target > HTScreenWidth - style->rightIndent) {
669: new_line(text);
670: return;
671: } else {
672: text->permissible_split = line->size; /* Can split here */
673: if (line->size == 0) line->offset = line->offset + target - here;
674: else for(; here<target; here++) {
675: line->data[line->size++] = ' '; /* Put character into line */
676: }
677: return;
678: }
679: /*NOTREACHED*/
680: } /* if tab */
681:
682:
683: if (ch==' ') {
684: text->permissible_split = line->size; /* Can split here */
685: }
686:
687: /* Check for end of line
688: */
689: if (indent + line->offset + line->size + style->rightIndent
690: >= HTScreenWidth) {
691: if (style->wordWrap) {
1.7 frystyk 692: if(text->permissible_split > line->size) /* HENRIK 21/02-94 */
693: text->permissible_split = line->size;
1.1 timbl 694: split_line(text, text->permissible_split);
695: if (ch==' ') return; /* Ignore space causing split */
696: } else new_line(text);
697: }
698:
699: /* Insert normal characters
700: */
701: if (ch == HT_NON_BREAK_SPACE) {
702: ch = ' ';
703: }
704: {
705: HTLine * line = text->last_line; /* May have changed */
706: HTFont font = style->font;
707: line->data[line->size++] = /* Put character into line */
708: font & HT_CAPITALS ? TOUPPER(ch) : ch;
709: if (font & HT_DOUBLE) /* Do again if doubled */
710: HText_appendCharacter(text, HT_NON_BREAK_SPACE);
711: /* NOT a permissible split */
712: }
713: }
714:
715: /* Anchor handling
716: ** ---------------
717: */
718: /* Start an anchor field
719: */
720: PUBLIC void HText_beginAnchor ARGS2(HText *,text, HTChildAnchor *,anc)
721: {
722: TextAnchor * a = (TextAnchor *) malloc(sizeof(*a));
1.2 timbl 723: char marker[100];
1.1 timbl 724:
725: if (a == NULL) outofmem(__FILE__, "HText_beginAnchor");
726: a->start = text->chars + text->last_line->size;
727: a->extent = 0;
728: if (text->last_anchor) {
729: text->last_anchor->next = a;
730: } else {
731: text->first_anchor = a;
732: }
733: a->next = 0;
734: a->anchor = anc;
735: text->last_anchor = a;
736:
737: if (HTAnchor_followMainLink((HTAnchor*)anc)) {
738: a->number = ++(text->last_anchor_number);
739: } else {
740: a->number = 0;
741: }
1.2 timbl 742:
743: if (start_reference && a->number && display_anchors) {
744: /* If it goes somewhere */
745: sprintf(marker, start_reference, a->number);
746: HText_appendText(text, marker);
747: }
1.1 timbl 748: }
749:
750:
751: PUBLIC void HText_endAnchor ARGS1(HText *,text)
752: {
753: TextAnchor * a = text->last_anchor;
754: char marker[100];
755: if (a->number && display_anchors) { /* If it goes somewhere */
1.2 timbl 756: sprintf(marker, end_reference, a->number);
1.1 timbl 757: HText_appendText(text, marker);
758: }
759: a->extent = text->chars + text->last_line->size - a->start;
760: }
761:
762:
1.5 timbl 763: /* IMAGES
764: */
765: PUBLIC void HText_appendImage ARGS5(
766: HText *, text,
767: HTChildAnchor *, anc,
768: CONST char *, alt,
1.9 frystyk 769: CONST char *, alignment,
1.5 timbl 770: BOOL, isMap)
771: {
1.9 frystyk 772: HText_appendText(text, alt? alt : "[IMAGE]");
1.5 timbl 773: }
774:
1.1 timbl 775: PUBLIC void HText_appendText ARGS2(HText *,text, CONST char *,str)
776: {
777: CONST char * p;
778: for(p=str; *p; p++) {
779: HText_appendCharacter(text, *p);
780: }
781: }
782:
783:
784: PUBLIC void HText_endAppend ARGS1(HText *,text)
785: {
786: new_line(text);
787:
788: if (text->display_on_the_fly) { /* Not finished? */
789: fill_screen(text, text->display_on_the_fly); /* Finish it */
790: text->display_on_the_fly = 0;
791: text->next_line = text->last_line; /* Bug fix after EvA 920117 */
792: text->stale = NO;
793: }
794: }
795:
796:
797:
1.20 frystyk 798: /* Dump diagnostics to TDEST
1.1 timbl 799: */
800: PUBLIC void HText_dump ARGS1(HText *,text)
801: {
1.20 frystyk 802: fprintf(TDEST, "HText: Dump called\n");
1.1 timbl 803: }
804:
805:
806: /* Return the anchor associated with this node
807: */
808: PUBLIC HTParentAnchor * HText_nodeAnchor ARGS1(HText *,text)
809: {
810: return text->node_anchor;
811: }
812:
813: /* GridText specials
814: ** =================
815: */
816: /* Return the anchor with index N
817: **
818: ** The index corresponds to the number we print in the anchor.
819: */
820: PUBLIC HTChildAnchor * HText_childNumber ARGS2(HText *,text, int,number)
821: {
822: TextAnchor * a;
823: for (a = text->first_anchor; a; a = a->next) {
824: if (a->number == number) return a->anchor;
825: }
826: return (HTChildAnchor *)0; /* Fail */
827: }
828:
829: PUBLIC void HText_setStale ARGS1(HText *,text)
830: {
1.16 frystyk 831: if (text)
832: text->stale = YES;
1.1 timbl 833: }
834:
835: PUBLIC void HText_refresh ARGS1(HText *,text)
836: {
1.16 frystyk 837: if (text && text->stale)
838: display_page(text, text->top_of_screen);
1.1 timbl 839: }
840:
841: PUBLIC int HText_sourceAnchors ARGS1(HText *,text)
842: {
1.16 frystyk 843: return (text ? text->last_anchor_number : -1);
1.1 timbl 844: }
845:
846: PUBLIC BOOL HText_canScrollUp ARGS1(HText *,text)
847: {
1.16 frystyk 848: return (text && text->top_of_screen != 0);
1.1 timbl 849: }
850:
851: PUBLIC BOOL HText_canScrollDown ARGS1(HText *,text)
852: {
1.16 frystyk 853: return (text && (text->top_of_screen + DISPLAY_LINES -
854: (text->title ? TITLE_LINES : 0)) < text->lines);
1.1 timbl 855: }
856:
857: /* Scroll actions
858: */
859: PUBLIC void HText_scrollTop ARGS1(HText *,text)
860: {
861: display_page(text, 0);
862: }
863:
864: PUBLIC void HText_scrollDown ARGS1(HText *,text)
865: {
866: display_page(text, text->top_of_screen + DISPLAY_LINES -1);
867: }
868:
869: PUBLIC void HText_scrollUp ARGS1(HText *,text)
870: {
871: display_page(text, text->top_of_screen - DISPLAY_LINES +1);
872: }
873:
874: PUBLIC void HText_scrollBottom ARGS1(HText *,text)
875: {
876: display_page(text, text->lines - DISPLAY_LINES +1);
877: }
878:
879:
880: /* Browsing functions
881: ** ==================
882: */
883:
884: /* Bring to front and highlight it
885: */
886:
887: PRIVATE int line_for_char ARGS2(HText *,text, int,char_num)
888: {
889: int line_number =0;
890: int characters = 0;
891: HTLine * line = text->last_line->next;
892: for(;;) {
893: if (line == text->last_line) return 0; /* Invalid */
894: characters = characters + line->size + 1;
895: if (characters > char_num) return line_number;
896: line_number ++;
897: line = line->next;
898: }
899: }
900:
901: PUBLIC BOOL HText_select ARGS1(HText *,text)
902: {
1.13 frystyk 903: if (text) {
1.1 timbl 904: HTMainText = text;
905: HTMainAnchor = text->node_anchor;
906: display_page(text, text->top_of_screen);
1.13 frystyk 907: return YES;
1.1 timbl 908: }
1.20 frystyk 909: if (SGML_TRACE)
910: fprintf(TDEST, "HText: Nothing to select!\n");
1.13 frystyk 911: return NO;
1.1 timbl 912: }
913:
914: PUBLIC BOOL HText_selectAnchor ARGS2(HText *,text, HTChildAnchor *,anchor)
915: {
916: TextAnchor * a;
917:
918: for(a=text->first_anchor; a; a=a->next) {
919: if (a->anchor == anchor) break;
920: }
921: if (!a) {
1.20 frystyk 922: if (SGML_TRACE)
923: fprintf(TDEST, "HText: No such anchor in this text!\n");
1.1 timbl 924: return NO;
925: }
926:
927: if (text != HTMainText) { /* Comment out by ??? */
928: HTMainText = text; /* Put back in by tbl 921208 */
929: HTMainAnchor = text->node_anchor;
930: }
931:
932: {
1.20 frystyk 933: int l = line_for_char(text, a->start);
934: if (SGML_TRACE)
935: fprintf(TDEST,"HText: Selecting anchor [%d] at char %d, line %d\n",
936: a->number, a->start, l);
1.1 timbl 937:
938: if ( !text->stale &&
1.20 frystyk 939: (l >= text->top_of_screen) &&
940: ( l < text->top_of_screen + DISPLAY_LINES-1))
941: return YES;
1.1 timbl 942: display_page(text, l - (DISPLAY_LINES/3)); /* Scroll to it */
943: }
944: return YES;
945: }
946:
947:
948: /* Editing functions - NOT IMPLEMENTED
949: ** =================
950: **
951: ** These are called from the application. There are many more functions
952: ** not included here from the orginal text object.
953: */
954:
955: /* Style handling:
956: */
957: /* Apply this style to the selection
958: */
959: PUBLIC void HText_applyStyle ARGS2(HText *, me, HTStyle *,style)
960: {
961:
962: }
963:
964:
965: /* Update all text with changed style.
966: */
967: PUBLIC void HText_updateStyle ARGS2(HText *, me, HTStyle *,style)
968: {
969:
970: }
971:
972:
973: /* Return style of selection
974: */
975: PUBLIC HTStyle * HText_selectionStyle ARGS2(
976: HText *,me,
977: HTStyleSheet *,sheet)
978: {
979: return 0;
980: }
981:
982:
983: /* Paste in styled text
984: */
985: PUBLIC void HText_replaceSel ARGS3(
986: HText *,me,
987: CONST char *,aString,
988: HTStyle *,aStyle)
989: {
990: }
991:
992:
993: /* Apply this style to the selection and all similarly formatted text
994: ** (style recovery only)
995: */
996: PUBLIC void HTextApplyToSimilar ARGS2(HText *,me, HTStyle *,style)
997: {
998:
999: }
1000:
1001:
1002: /* Select the first unstyled run.
1003: ** (style recovery only)
1004: */
1005: PUBLIC void HTextSelectUnstyled ARGS2(HText *,me, HTStyleSheet *,sheet)
1006: {
1007:
1008: }
1009:
1010:
1011: /* Anchor handling:
1012: */
1013: PUBLIC void HText_unlinkSelection ARGS1(HText *,me)
1014: {
1015:
1016: }
1017:
1018: PUBLIC HTAnchor * HText_referenceSelected ARGS1(HText *,me)
1019: {
1020: return 0;
1021: }
1022:
1023:
1024: #ifdef CURSES
1025: PUBLIC int HText_getTopOfScreen ARGS1(HText *,text)
1026: {
1027: return text->top_of_screen;
1028: }
1029:
1030: PUBLIC int HText_getLines ARGS1(HText *,text)
1031: {
1032: return text->lines;
1033: }
1034: #endif
1035: PUBLIC HTAnchor * HText_linkSelTo ARGS2(HText *,me, HTAnchor *,anchor)
1036: {
1037: return 0;
1038: }
1039:
1.20 frystyk 1040: /*
1041: ** Check if document is already loaded. As the application handles the
1042: ** memory cache, we call the application to ask. Also check if it has
1043: ** expired in which case we reload it (either from disk cache or remotely)
1044: */
1045: PUBLIC int HTMemoryCache ARGS3(HTRequest *, request, HTExpiresMode, mode,
1046: char *, notification)
1047: {
1048: HText *text;
1049: if (!request)
1050: return HT_ERROR;
1051: if ((text = (HText *) HTAnchor_document(request->anchor))) {
1052: if (request->reload != HT_MEM_REFRESH) {
1053: if (CACHE_TRACE)
1054: fprintf(TDEST,"HTMemCache.. Document already in memory\n");
1055: if (mode != HT_EXPIRES_IGNORE) {
1056: if (!HTCache_isValid(request->anchor)) {
1057: if (mode == HT_EXPIRES_NOTIFY)
1058: HTAlert(request, notification);
1059: else {
1060: if (CACHE_TRACE)
1061: fprintf(TDEST,
1062: "HTMemCache.. Expired - autoreload\n");
1063: request->RequestMask |= HT_IMS;
1064: #ifndef HT_SHARED_DISK_CACHE
1065: request->reload = HT_CACHE_REFRESH;
1066: #endif
1067: return HT_ERROR; /* Must go get it */
1068: }
1069: }
1070: }
1.1 timbl 1071:
1.20 frystyk 1072: /*
1073: ** If we can use object then select it and return
1074: ** This should be a callback to the app mem cache handler
1075: */
1076: if (request->childAnchor)
1077: HText_selectAnchor(text, request->childAnchor);
1078: else
1079: HText_select(text);
1080: return HT_LOADED;
1081: } else { /* If refresh version in memory */
1082: request->RequestMask |= HT_IMS;
1083: }
1084: } else { /* Don't reuse any old metainformation */
1085: HTAnchor_clearHeader(request->anchor);
1086: }
1087: return HT_ERROR;
1088: }
Webmaster