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

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

Webmaster