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

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

Webmaster