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

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

Webmaster