Annotation of libwww/LineMode/src/GridText.c, revision 1.38

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

Webmaster