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

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 */
                    101: };
                    102: 
                    103: 
                    104: #define PUTC(c) (*text->targetClass.put_character)(text->target, c)
                    105: #define PUTS(s) (*text->targetClass.put_string)(text->target, s)
                    106: 
                    107: /*     Boring static variable used for moving cursor across
                    108: */
                    109: 
                    110: #define SPACES(n) (&space_string[HTScreenWidth - (n)])
                    111:                                    /* String containing blank spaces only */
                    112: PRIVATE char * space_string;
                    113: 
                    114: PRIVATE HTStyle default_style =
                    115:        { 0,  "(Unstyled)", "",
                    116:        (HTFont)0, 1.0, HT_BLACK,               0, 0,
                    117:        0, 0, 0, HT_LEFT,               1, 0,   0, 
                    118:        NO, NO, 0, 0,                   0 };    
                    119: 
                    120: 
1.29      frystyk   121: PUBLIC void clear_screen (void);       /* Forward */
1.1       timbl     122: 
                    123: PRIVATE HTList * loaded_texts; /* A list of all those in memory */
                    124: 
                    125: /*                     Creation Method
                    126: **                     ---------------
1.3       timbl     127: **
                    128: **     Interactive version
                    129: **
1.1       timbl     130: */
1.31      frystyk   131: PUBLIC HText * HText_new (HTRequest * request, HTParentAnchor * anchor)
1.1       timbl     132: {
                    133:     HTLine * line;
1.34    ! frystyk   134:     HText * self;
        !           135:     if ((self = (HText  *) HT_MALLOC(sizeof(*self))) == NULL)
        !           136: /*        HT_OUTOFMEM("HText"); */
        !           137:        return self;
1.1       timbl     138:     
                    139:     if (!loaded_texts) loaded_texts = HTList_new();
                    140:     HTList_addObject(loaded_texts, self);
                    141:     if (HTList_count(loaded_texts) >= LOADED_LIMIT) {
1.20      frystyk   142:         if (CACHE_TRACE)
1.26      frystyk   143:            TTYPrint(TDEST, "MemoryCache. Freeing off cached doc.\n"); 
1.1       timbl     144:         HText_free((HText *)HTList_removeFirstObject(loaded_texts));
                    145:     }
                    146:     
1.34    ! frystyk   147:     if ((line = self->last_line = (HTLine *) HT_MALLOC(LINE_SIZE(MAX_LINE))) == NULL)
        !           148:        HT_OUTOFMEM("HText_New");
1.1       timbl     149:     line->next = line->prev = line;
                    150:     line->offset = line->size = 0;
                    151:     self->lines = self->chars = 0;
                    152:     self->title = 0;
                    153:     self->first_anchor = self->last_anchor = 0;
                    154:     self->style = &default_style;
                    155:     self->top_of_screen = 0;
                    156:     self->node_anchor = anchor;
                    157:     self->last_anchor_number = 0;      /* Numbering of them for references */
                    158:     self->stale = YES;
                    159:     
                    160:     self->target = NULL;
                    161:     
1.31      frystyk   162:     HTAnchor_setDocument(anchor, (void *) self);
1.1       timbl     163: 
                    164:     clear_screen();
                    165:     HTMainText = self;
                    166:     HTMainAnchor = anchor;
                    167:     self->display_on_the_fly = DISPLAY_LINES;
1.3       timbl     168:     self->all_pages = NO;      /* One page at a time on the fly */
1.1       timbl     169:     
                    170:     if (!space_string) {       /* Make a blank line */
                    171:         char *p;
1.34    ! frystyk   172:        if ((space_string = (char  *) HT_MALLOC(HTScreenWidth+1)) == NULL)
        !           173:            HT_OUTOFMEM("HText_New");
1.1       timbl     174:         for (p=space_string; p<space_string+HTScreenWidth; p++) 
                    175:             *p = ' ';          /* Used for printfs later */
                    176:         space_string[HTScreenWidth] = '\0'; 
                    177:     }
                    178:     
                    179:     return self;
                    180: }
                    181: 
                    182: 
                    183: /*                     Creation Method 2
                    184: **                     ---------------
                    185: **
1.4       timbl     186: **     Non-interative  OR interactive if stream is NULL
1.1       timbl     187: **     Stream is assumed open and left open.
                    188: */
1.31      frystyk   189: PUBLIC HText * HText_new2 (HTRequest *         request,
                    190:                            HTParentAnchor *    anchor,
                    191:                            HTStream *          stream)
1.1       timbl     192: {
1.31      frystyk   193:     HText * me = HText_new(request, anchor);
1.1       timbl     194:         
                    195:     if (stream) {
1.3       timbl     196:         me->target = stream;
                    197:        me->targetClass = *stream->isa; /* copy action procedures */
1.4       timbl     198:        me->all_pages = YES;    /* Display whole file on the fly */    
1.3       timbl     199:     }
                    200:     return me;
1.1       timbl     201: }
                    202: 
1.31      frystyk   203: /*     Free a data object
                    204: **     ------------------
                    205: **     Removes the data object from the anchor
1.1       timbl     206: */
1.29      frystyk   207: PUBLIC void hyperfree (HText *  self)
1.1       timbl     208: {
                    209:     while(YES) {               /* Free off line array */
                    210:         HTLine * l = self->last_line;
                    211:        l->next->prev = l->prev;
                    212:        l->prev->next = l->next;        /* Unlink l */
                    213:        self->last_line = l->prev;
1.34    ! frystyk   214:        HT_FREE(l);
1.1       timbl     215:        if (l == self->last_line) break;        /* empty */
                    216:     };
                    217:     
                    218:     while(self->first_anchor) {                /* Free off anchor array */
                    219:         TextAnchor * l = self->first_anchor;
                    220:        self->first_anchor = l->next;
1.34    ! frystyk   221:        HT_FREE(l);
1.1       timbl     222:     }
1.34    ! frystyk   223:     HT_FREE(self);
1.8       frystyk   224:     if (self == HTMainText)                              /* Henrik 24/02-94 */
                    225:        HTMainText = NULL;
1.18      frystyk   226: }
                    227: 
                    228: 
                    229: /*     Free Entire Text
                    230: **     ----------------
                    231: */
1.29      frystyk   232: PUBLIC void    HText_free (HText * self)
1.18      frystyk   233: {
1.31      frystyk   234:     HTAnchor_setDocument(self->node_anchor, NULL);
1.18      frystyk   235:     hyperfree(self);
1.1       timbl     236: }
                    237: 
                    238: 
                    239: /*             Display Methods
                    240: **             ---------------
                    241: */
1.22      frystyk   242: 
1.1       timbl     243: /*     Clear the screen (on screen-mode systems)
                    244: **     ----------------
                    245: */
1.29      frystyk   246: PUBLIC void clear_screen (void)
1.1       timbl     247: {
1.25      frystyk   248:     if (WWWTRACE)
1.22      frystyk   249:        return;                     /* in trace mode, don't clear trace away */
1.1       timbl     250: #ifdef CURSES
                    251:     if (w_text != NULL) {
1.22      frystyk   252:        wmove(w_text,0,0);
                    253:        wclear(w_text);
1.1       timbl     254:     }
1.22      frystyk   255: #endif /* Not CURSES */
1.1       timbl     256:     if (HTMainText) HTMainText->stale = YES;
                    257: }
                    258: 
                    259: 
                    260: /*     Output a line
                    261: **     -------------
                    262: */
1.29      frystyk   263: PRIVATE void display_line (HText * text, HTLine * line)
1.1       timbl     264: {
                    265: #ifdef CURSES
                    266:       int     y, x;
                    267: 
                    268:       waddstr(w_text, SPACES(line->offset));
                    269:       waddstr(w_text, line->data);
                    270:       getyx(w_text, y, x);
                    271:       if (y < DISPLAY_LINES-1) {
                    272:               wmove(w_text, ++y, 0);
                    273:       }
                    274: #else
                    275:    if (!text->target)
1.11      howcome   276:    {
                    277: #ifdef CYRILLIC
                    278:        /* HWL 18/7/94: applied patch from agl@glas2.glas.apc.org (Anton Tropashko) */
                    279:        a_print(SPACES(line->offset),H,stdout); 
                    280:        a_print(line->data,H,stdout);
                    281:        fputc('\n',stdout);
                    282: #else
1.26      frystyk   283:        TTYPrint(STDOUT, "%s%s\n", SPACES(line->offset), line->data);
1.11      howcome   284: #endif
                    285:    }
1.1       timbl     286:    else {
                    287:        PUTS(SPACES(line->offset));
                    288:        PUTS(line->data);
                    289:        PUTC('\n');
                    290:    }
                    291: #endif
                    292:    
                    293: }
                    294: 
                    295: /*     Output the title line
                    296: **     ---------------------
                    297: */
1.29      frystyk   298: PRIVATE void display_title (HText * text)
1.1       timbl     299: {
                    300:     CONST char * title = HTAnchor_title(text->node_anchor);
                    301:     char percent[20], format[20];
                    302:     if (text->lines > (DISPLAY_LINES-1)) {
                    303: #ifdef NOPE
                    304:        sprintf(percent, " (p%d of %d)",
                    305:            (text->top_of_screen/(DISPLAY_LINES-1)) + 1,
                    306:            (text->lines-1)/(DISPLAY_LINES-1) + 1);
                    307:        sprintf(percent, " (%d%%)",
                    308:            100*(text->top_of_screen+DISPLAY_LINES-1)/  /* Seen */
                    309:                (text->lines));                         /* Total */
                    310: #else
                    311:        sprintf(percent, " (%d/%d)",
                    312:            text->top_of_screen+DISPLAY_LINES-1,        /* Seen */
                    313:            text->lines);                               /* Total */
                    314: #endif
                    315:     } else {
                    316:        percent[0] = 0; /* Null string */
                    317:     }
                    318: 
                    319:     sprintf(format, "%%%d.%ds%%s\n",   /* Generate format string */
                    320:                    (int)(HTScreenWidth-strlen(percent)),
                    321:                    (int)(HTScreenWidth-strlen(percent)) );
                    322: #ifdef CURSES
                    323:     mvwprintw(w_top, 0, 0, format, title, percent);
                    324:     wrefresh(w_top);
                    325: #else
1.26      frystyk   326:     if (!text->target) TTYPrint(STDOUT, format, title, percent);
1.1       timbl     327:     else {
1.34    ! frystyk   328:        char * line;
        !           329:        if ((line = (char *) HT_MALLOC(HTScreenWidth+10)) == NULL)
        !           330:            HT_OUTOFMEM("display_titile");
1.1       timbl     331:         sprintf(line, format, title, percent);
                    332:        PUTS(line);
1.34    ! frystyk   333:        HT_FREE(line);
1.1       timbl     334:     }
                    335: #endif
                    336: }
                    337: 
                    338: 
                    339: /*     Fill the screen with blank after the file
                    340: **     --------------------------
                    341: */
1.29      frystyk   342: PRIVATE void fill_screen (HText *  text, int n)
1.1       timbl     343: {
                    344:     if (n<=0 || n>1000) return;                /* Large value means no pagination */
                    345:     if (text->target) return;
                    346: #ifdef CURSES
                    347:     waddstr(w_text, end_mark);
                    348:     wclrtobot(w_text);
                    349:     wrefresh(w_text);
                    350: #else
                    351: #ifndef VIOLA    
1.26      frystyk   352:     if (!text->target) TTYPrint(STDOUT, "%s\n", end_mark);
1.1       timbl     353:     else { PUTS(end_mark); PUTC('\n'); }
                    354:     n--;
                    355:     
                    356:     for (; n; n--) {
1.26      frystyk   357:         if (!text->target) TTYPrint(STDOUT, "\n");
1.1       timbl     358:        else PUTC('\n');
                    359:     }
                    360: #endif
                    361: #endif        /* Not CURSES */
                    362: }
                    363: 
                    364: 
                    365: /*     Output a page
                    366: **     -------------
                    367: */
1.29      frystyk   368: PRIVATE void display_page (HText * text, int line_number)
1.1       timbl     369: {
                    370:     HTLine * line = text->last_line->prev;
                    371:     int i;
                    372:     CONST char * title = HTAnchor_title(text->node_anchor);
                    373:     int lines_of_text = title ? (DISPLAY_LINES-TITLE_LINES) : DISPLAY_LINES;
                    374:     int last_screen = text->lines - lines_of_text;
                    375: 
                    376: /*     Constrain the line number to be within the document
                    377: */
                    378:     if (text->lines <= lines_of_text) line_number = 0;
                    379:     else if (line_number>last_screen) line_number = last_screen;
                    380:     else if (line_number < 0) line_number = 0;
                    381:     
                    382:     for(i=0,  line = text->last_line->next;            /* Find line */
                    383:        i<line_number && (line!=text->last_line);
                    384:       i++, line=line->next) /* Loop */ assert(line->next != NULL);
                    385: 
                    386:     while((line!=text->last_line) && (line->size==0)) {        /* Skip blank lines */
                    387:         assert(line->next != NULL);
                    388:       line = line->next;
                    389:         line_number ++;
                    390:     }
                    391: 
                    392: /*     Can we just scroll to it?
                    393: */ 
                    394: #ifndef VM                     /* No scrolling */
                    395:     if (!text->stale && (line_number>=text->top_of_screen) &&
                    396:        (line_number < text->top_of_screen + DISPLAY_LINES)) {  /* Yes */
                    397:        lines_of_text = line_number - text->top_of_screen;
                    398:        line = text->next_line;
                    399:         text->top_of_screen = line_number;
                    400: #ifdef CURSES
                    401:         scrollok(w_text, TRUE);
                    402:         for (i = 0; i < lines_of_text; i++) {
                    403:               scroll(w_text);
                    404:         }
                    405: #endif  
                    406:     } else
                    407: #endif
                    408:     {
                    409:        clear_screen();                                         /* No */
                    410:         text->top_of_screen = line_number;
                    411:        if (title) display_title(text);
                    412:     }
                    413:     
                    414: /* Bug: when we scroll to a part slightly futher down a page which previously
                    415:  fitted all on one screen including the [End], the code below will add an
                    416:  extra [End] to the screen, giving a total of two, one underneath the other.
                    417: */
                    418:     
                    419:  /*    print it
                    420:  */
                    421:     if (line) {
                    422:       for(i=0;
                    423:          (i< lines_of_text) && (line != text->last_line); i++)  {
                    424:       assert(line != NULL);
                    425:         display_line(text, line);
                    426:        line = line->next;
                    427:       }
                    428:       fill_screen(text, lines_of_text - i);
                    429:     }
                    430: 
                    431:     text->next_line = line;    /* Line after screen */
                    432:     text->stale = NO;          /* Display is up-to-date */
                    433: }
                    434: 
                    435: 
                    436: /*                     Object Building methods
                    437: **                     -----------------------
                    438: **
                    439: **     These are used by a parser to build the text in an object
                    440: */
1.29      frystyk   441: PUBLIC void HText_beginAppend (HText * text)
1.1       timbl     442: {
                    443:     text->permissible_split = 0;
                    444:     text->in_line_1 = YES;
                    445: }
                    446: 
                    447: 
                    448: /*     Add a new line of text
                    449: **     ----------------------
                    450: **
                    451: ** On entry,
                    452: **
                    453: **     split   is zero for newline function, else number of characters
                    454: **             before split.
                    455: **     text->display_on_the_fly
                    456: **             may be set to indicate direct output of the finished line.
1.3       timbl     457: **     text->all_pages
                    458: **             if set indicates all pages are to be done on the fly.
1.1       timbl     459: ** On exit,
                    460: **             A new line has been made, justified according to the
                    461: **             current style. Text after the split (if split nonzero)
                    462: **             is taken over onto the next line.
                    463: **
                    464: **             If display_on_the_fly is set, then it is decremented and
                    465: **             the finished line is displayed.
                    466: */
                    467: #define new_line(text) split_line(text, 0)
                    468: 
1.29      frystyk   469: PRIVATE void split_line (HText * text, int split)
1.1       timbl     470: {
                    471:     HTStyle * style = text->style;
                    472:     int spare;
1.14      frystyk   473:     int indent = (int) (text->in_line_1 ? text->style->indent1st
                    474:                        : text->style->leftIndent);
1.7       frystyk   475: 
1.1       timbl     476: /*     Make new line
                    477: */
                    478:     HTLine * previous = text->last_line;
1.34    ! frystyk   479:     HTLine * line;
        !           480:     if ((line = (HTLine  *) HT_MALLOC(LINE_SIZE(MAX_LINE))) == NULL)
        !           481:         HT_OUTOFMEM("split_line");
        !           482: 
1.1       timbl     483:     text->lines++;
                    484:     
                    485:     previous->next->prev = line;
                    486:     line->prev = previous;
                    487:     line->next = previous->next;
                    488:     previous->next = line;
                    489:     text->last_line = line;
                    490:     line->size = 0;
                    491:     line->offset = 0;
                    492: 
                    493: /*     Split at required point
                    494: */    
                    495:     if (split) {       /* Delete space at "split" splitting line */
                    496:        char * p;
                    497:        previous->data[previous->size] = 0;
                    498:        for (p = &previous->data[split]; *p; p++)
                    499:            if (*p != ' ') break;
                    500:        strcpy(line->data, p);
                    501:        line->size = strlen(line->data);
                    502:        previous->size = split;
                    503:     }
                    504:     
                    505: /*     Economise on space.
                    506: **     Not on the RS6000 due to a chaotic bug in realloc argument passing.
                    507: **     Same problem with Ultrix (4.2) : realloc() is not declared properly.
                    508: */
1.16      frystyk   509: #ifndef AIX
                    510: #ifndef ultrix
1.1       timbl     511:     while ((previous->size > 0) &&
                    512:        (previous->data[previous->size-1] == ' '))      /* Strip trailers */
                    513:         previous->size--;
1.7       frystyk   514: 
1.34    ! frystyk   515:     if ((previous = (HTLine  *) HT_REALLOC(previous, LINE_SIZE(previous->size))) == NULL)
        !           516:         HT_OUTOFMEM("split_line");
1.16      frystyk   517: #endif /* ultrix */
                    518: #endif /* AIX */
1.1       timbl     519: 
                    520:     previous->prev->next = previous;   /* Link in new line */
                    521:     previous->next->prev = previous;   /* Could be same node of course */
                    522: 
                    523: /*     Terminate finished line for printing
                    524: */
                    525:     previous->data[previous->size] = 0;
1.7       frystyk   526: 
1.1       timbl     527: /*     Align left, right or center
                    528: */
                    529: 
1.14      frystyk   530:     spare =  (int) (HTScreenWidth - style->rightIndent + style->leftIndent -
                    531:                    previous->size);                 /* @@ first line indent */
1.1       timbl     532:                
                    533:     switch (style->alignment) {
                    534:        case HT_CENTER :
                    535:            previous->offset = previous->offset + indent + spare/2;
                    536:            break;
                    537:        case HT_RIGHT :
                    538:            previous->offset = previous->offset + indent + spare;
                    539:            break;
                    540:        case HT_LEFT :
                    541:        case HT_JUSTIFY :               /* Not implemented */
                    542:        default:
                    543:            previous->offset = previous->offset + indent;
                    544:            break;
                    545:     } /* switch */
                    546: 
                    547:     text->chars = text->chars + previous->size + 1;    /* 1 for the line */
                    548:     text->in_line_1 = NO;              /* unless caller sets it otherwise */
                    549:     
                    550: /*     If displaying as we go, output it:
                    551: */
                    552:     if (text->display_on_the_fly) {
                    553:         if (text->display_on_the_fly == DISPLAY_LINES) { /* First line */
                    554:            if (previous->size == 0) {
                    555:                text->top_of_screen++;  /* Scroll white space off top */
                    556:                return;
                    557:            }
                    558:            if (HTAnchor_title(text->node_anchor)) { /* Title exists */
                    559:                display_title(text);
                    560:                text->display_on_the_fly--;
                    561:            }
                    562:        }
                    563:        display_line(text, previous);
                    564:        text->display_on_the_fly--;
1.3       timbl     565:        
                    566:        /* Loop to top of next page? */
                    567:        if (!text->display_on_the_fly && text->all_pages) {
                    568:            PUTS("\f\n"); /* Form feed on its own line a la rfc1111 */
                    569:            text->display_on_the_fly = DISPLAY_LINES;
                    570:        }
1.1       timbl     571:     }
                    572: } /* split_line */
                    573: 
                    574: 
                    575: /*     Allow vertical blank space
                    576: **     --------------------------
                    577: */
1.29      frystyk   578: PRIVATE void blank_lines (HText * text, int newlines)
1.1       timbl     579: {
                    580:     if (text->last_line->size == 0) {  /* No text on current line */
                    581:        HTLine * line = text->last_line->prev;
                    582:        while ((line!=text->last_line) && (line->size == 0)) {
                    583:            if (newlines==0) break;
                    584:            newlines--;         /* Don't bother: already blank */
                    585:            line = line->prev;
                    586:        }
                    587:     } else {
                    588:        newlines++;                     /* Need also to finish this line */
                    589:     }
                    590: 
                    591:     for(;newlines;newlines--) {
                    592:        new_line(text);
                    593:     }
                    594:     text->in_line_1 = YES;
                    595: }
                    596: 
                    597: 
                    598: /*     New paragraph in current style
                    599: **     ------------------------------
                    600: ** See also: setStyle.
                    601: */
                    602: 
1.29      frystyk   603: PUBLIC void HText_appendParagraph (HText * text)
1.1       timbl     604: {
1.14      frystyk   605:     int after = (int) text->style->spaceAfter;
                    606:     int before = (int) text->style->spaceBefore;
1.1       timbl     607:     blank_lines(text, after>before ? after : before);
                    608: }
                    609: 
                    610: 
                    611: /*     Set Style
                    612: **     ---------
                    613: **
                    614: **     Does not filter unnecessary style changes.
                    615: */
1.29      frystyk   616: PUBLIC void HText_setStyle (HText * text, HTStyle * style)
1.1       timbl     617: {
                    618:     int after, before;
                    619: 
                    620:     if (!style) return;                                /* Safety */
1.14      frystyk   621:     after = (int) text->style->spaceAfter;
                    622:     before = (int) style->spaceBefore;
1.20      frystyk   623:     if (SGML_TRACE)
1.26      frystyk   624:        TTYPrint(TDEST, "HTML: Change to style %s\n", style->name);
1.1       timbl     625:     blank_lines (text, after>before ? after : before);
                    626:     text->style = style;
                    627: }
                    628: 
                    629: 
                    630: /*     Append a character to the text object
                    631: **     -------------------------------------
                    632: */
1.29      frystyk   633: PUBLIC void HText_appendCharacter (HText * text, char ch)
1.1       timbl     634: {
                    635:     HTLine * line = text->last_line;
                    636:     HTStyle * style = text->style;
1.14      frystyk   637:     int indent = (int)(text->in_line_1 ? style->indent1st : style->leftIndent);
1.1       timbl     638:     
                    639: /*             New Line
                    640: */
                    641:     if (ch == '\n') {
                    642:            new_line(text);
                    643:            text->in_line_1 = YES;      /* First line of new paragraph */
                    644:            return;
                    645:     }
                    646: 
                    647: /*             Tabs
                    648: */
                    649: 
                    650:     if (ch == '\t') {
                    651:         HTTabStop * tab;
                    652:        int target;     /* Where to tab to */
                    653:        int here = line->size + line->offset +indent;
                    654:         if (style->tabs) {     /* Use tab table */
                    655:            for (tab = style->tabs;
                    656:                tab->position <= here;
                    657:                tab++)
                    658:                if (!tab->position) {
                    659:                    new_line(text);
                    660:                    return;
                    661:                }
1.14      frystyk   662:            target = (int) tab->position;
1.1       timbl     663:        } else if (text->in_line_1) {   /* Use 2nd indent */
                    664:            if (here >= style->leftIndent) {
                    665:                new_line(text); /* wrap */
                    666:                return;
                    667:            } else {
1.14      frystyk   668:                target = (int) style->leftIndent;
1.1       timbl     669:            }
                    670:        } else {                /* Default tabs align with left indent mod 8 */
                    671: #ifdef DEFAULT_TABS_8
                    672:            target = ((line->offset + line->size + 8) & (-8))
                    673:                        + style->leftIndent;
                    674: #else
                    675:            new_line(text);
                    676:            return;
                    677: #endif
                    678:        }
                    679:        if (target > HTScreenWidth - style->rightIndent) {
                    680:            new_line(text);
                    681:            return;
                    682:        } else {
                    683:             text->permissible_split = line->size;      /* Can split here */
                    684:            if (line->size == 0) line->offset = line->offset + target - here;
                    685:            else for(; here<target; here++) {
                    686:                 line->data[line->size++] = ' ';        /* Put character into line */
                    687:            }
                    688:            return;
                    689:        }
                    690:        /*NOTREACHED*/
                    691:     } /* if tab */ 
                    692: 
                    693:     
                    694:     if (ch==' ') {
                    695:         text->permissible_split = line->size;  /* Can split here */
                    696:     }
                    697: 
                    698: /*     Check for end of line
                    699: */    
                    700:     if (indent + line->offset + line->size + style->rightIndent
                    701:                >= HTScreenWidth) {
                    702:         if (style->wordWrap) {
1.7       frystyk   703:            if(text->permissible_split > line->size)    /* HENRIK 21/02-94 */
                    704:                text->permissible_split = line->size;
1.1       timbl     705:            split_line(text, text->permissible_split);
                    706:            if (ch==' ') return;        /* Ignore space causing split */
                    707:        } else new_line(text);
                    708:     }
                    709: 
                    710: /*     Insert normal characters
                    711: */
                    712:     if (ch == HT_NON_BREAK_SPACE) {
                    713:         ch = ' ';
                    714:     }
                    715:     {
                    716:         HTLine * line = text->last_line;       /* May have changed */
                    717:         HTFont font = style->font;
                    718:         line->data[line->size++] =     /* Put character into line */
                    719:            font & HT_CAPITALS ? TOUPPER(ch) : ch;
                    720:         if (font & HT_DOUBLE)          /* Do again if doubled */
                    721:             HText_appendCharacter(text, HT_NON_BREAK_SPACE);
                    722:            /* NOT a permissible split */ 
                    723:     }
                    724: }
                    725: 
                    726: /*             Anchor handling
                    727: **             ---------------
                    728: */
                    729: /*     Start an anchor field
                    730: */
1.29      frystyk   731: PUBLIC void HText_beginAnchor (HText * text, HTChildAnchor * anc)
1.1       timbl     732: {
1.2       timbl     733:     char marker[100];
1.34    ! frystyk   734:     TextAnchor * a;
        !           735:     if ((a = (TextAnchor  *) HT_MALLOC(sizeof(*a))) == NULL)
        !           736:         HT_OUTOFMEM("HText_beginAnchor");
1.1       timbl     737:     
                    738:     if (a == NULL) outofmem(__FILE__, "HText_beginAnchor");
                    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,
                    781:        CONST char *            alt,
                    782:        CONST char *            alignment,
                    783:        BOOL                    isMap)
1.5       timbl     784: {
1.9       frystyk   785:     HText_appendText(text, alt? alt : "[IMAGE]");
1.5       timbl     786: }
                    787:        
1.29      frystyk   788: PUBLIC void HText_appendText (HText * text, CONST char * str)
1.1       timbl     789: {
                    790:     CONST char * p;
                    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.26      frystyk   815:     TTYPrint(TDEST, "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.26      frystyk   923:        TTYPrint(TDEST, "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.26      frystyk   936:            TTYPrint(TDEST, "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.26      frystyk   948:            TTYPrint(TDEST,"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,
                   1000:        CONST char * aString, 
                   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.26      frystyk  1059:        TTYPrint(TDEST, "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.26      frystyk  1078:                TTYPrint(TDEST,"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) {
                   1082:                        if (WWWTRACE) TTYPrint(TDEST, "%s\n", notification);
                   1083:                    } else {
1.20      frystyk  1084:                        if (CACHE_TRACE)
1.26      frystyk  1085:                            TTYPrint(TDEST,
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