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

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

Webmaster