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

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

Webmaster