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

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

Webmaster