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

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

Webmaster