- Timestamp:
- Jan 20, 2007, 9:49:55 PM (18 years ago)
- Branches:
- master, barnowl_perlaim, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
- Children:
- 575877f
- Parents:
- 212764e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fmtext.c
ra0a5179 r8fa9562 11 11 f->textbuff=owl_strdup(""); 12 12 f->fmbuff=owl_malloc(5); 13 f->colorbuff=owl_malloc(5); 13 f->fgcolorbuff=owl_malloc(5); 14 f->bgcolorbuff=owl_malloc(5); 14 15 f->fmbuff[0]=OWL_FMTEXT_ATTR_NONE; 15 f->colorbuff[0]=OWL_COLOR_DEFAULT; 16 f->fgcolorbuff[0]=OWL_COLOR_DEFAULT; 17 f->bgcolorbuff[0]=OWL_COLOR_DEFAULT; 16 18 } 17 19 … … 41 43 * to index 'last 42 44 */ 43 void _owl_fmtext_set_ color(owl_fmtext *f, int color, int first, int last)45 void _owl_fmtext_set_fgcolor(owl_fmtext *f, int color, int first, int last) 44 46 { 45 47 int i; 46 48 for (i=first; i<=last; i++) { 47 f->colorbuff[i]=(unsigned char) color; 49 f->fgcolorbuff[i]=(unsigned char) color; 50 } 51 } 52 53 void _owl_fmtext_set_bgcolor(owl_fmtext *f, int color, int first, int last) 54 { 55 int i; 56 for (i=first; i<=last; i++) { 57 f->bgcolorbuff[i]=(unsigned char) color; 48 58 } 49 59 } … … 52 62 * 'color' 53 63 */ 54 void owl_fmtext_append_attr(owl_fmtext *f, char *text, int attr, int color)64 void owl_fmtext_append_attr(owl_fmtext *f, char *text, int attr, int fgcolor, int bgcolor) 55 65 { 56 66 int newlen; … … 59 69 f->textbuff=owl_realloc(f->textbuff, newlen+2); 60 70 f->fmbuff=owl_realloc(f->fmbuff, newlen+2); 61 f->colorbuff=owl_realloc(f->colorbuff, newlen+2); 71 f->fgcolorbuff=owl_realloc(f->fgcolorbuff, newlen+2); 72 f->bgcolorbuff=owl_realloc(f->bgcolorbuff, newlen+2); 62 73 63 74 strcat(f->textbuff, text); 64 75 _owl_fmtext_set_attr(f, attr, f->textlen, newlen); 65 _owl_fmtext_set_color(f, color, f->textlen, newlen); 76 _owl_fmtext_set_fgcolor(f, fgcolor, f->textlen, newlen); 77 _owl_fmtext_set_bgcolor(f, bgcolor, f->textlen, newlen); 66 78 f->textlen=newlen; 67 79 } … … 70 82 void owl_fmtext_append_normal(owl_fmtext *f, char *text) 71 83 { 72 owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT );84 owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); 73 85 } 74 86 75 87 /* Append normal text 'text' to 'f' with color 'color' */ 76 void owl_fmtext_append_normal_color(owl_fmtext *f, char *text, int color)77 { 78 owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, color);88 void owl_fmtext_append_normal_color(owl_fmtext *f, char *text, int fgcolor, int bgcolor) 89 { 90 owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, fgcolor, bgcolor); 79 91 } 80 92 … … 82 94 void owl_fmtext_append_bold(owl_fmtext *f, char *text) 83 95 { 84 owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT );96 owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); 85 97 } 86 98 … … 88 100 void owl_fmtext_append_reverse(owl_fmtext *f, char *text) 89 101 { 90 owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE, OWL_COLOR_DEFAULT );102 owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); 91 103 } 92 104 … … 94 106 void owl_fmtext_append_reversebold(owl_fmtext *f, char *text) 95 107 { 96 owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE | OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT );108 owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE | OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); 97 109 } 98 110 … … 114 126 void owl_fmtext_colorize(owl_fmtext *f, int color) 115 127 { 116 /* everywhere the color is OWL_COLOR_DEFAULT, change it to be 'color' */128 /* everywhere the fgcolor is OWL_COLOR_DEFAULT, change it to be 'color' */ 117 129 int i, j; 118 130 119 131 j=f->textlen; 120 132 for(i=0; i<j; i++) { 121 if (f->colorbuff[i]==OWL_COLOR_DEFAULT) f->colorbuff[i] = color; 133 if (f->fgcolorbuff[i]==OWL_COLOR_DEFAULT) f->fgcolorbuff[i] = color; 134 } 135 } 136 137 void owl_fmtext_colorizebg(owl_fmtext *f, int color) 138 { 139 /* everywhere the bgcolor is OWL_COLOR_DEFAULT, change it to be 'color' */ 140 int i, j; 141 142 j=f->textlen; 143 for(i=0; i<j; i++) { 144 if (f->bgcolorbuff[i]==OWL_COLOR_DEFAULT) f->bgcolorbuff[i] = color; 122 145 } 123 146 } … … 133 156 f->textbuff=owl_realloc(f->textbuff, newlen+1); 134 157 f->fmbuff=owl_realloc(f->fmbuff, newlen+1); 135 f->colorbuff=owl_realloc(f->colorbuff, newlen+1); 158 f->fgcolorbuff=owl_realloc(f->fgcolorbuff, newlen+1); 159 f->bgcolorbuff=owl_realloc(f->bgcolorbuff, newlen+1); 136 160 137 161 strncat(f->textbuff, in->textbuff+start, stop-start+1); … … 139 163 for (i=start; i<=stop; i++) { 140 164 f->fmbuff[f->textlen+(i-start)]=in->fmbuff[i]; 141 f->colorbuff[f->textlen+(i-start)]=in->colorbuff[i]; 165 f->fgcolorbuff[f->textlen+(i-start)]=in->fgcolorbuff[i]; 166 f->bgcolorbuff[f->textlen+(i-start)]=in->bgcolorbuff[i]; 142 167 } 143 168 f->textlen=newlen; … … 174 199 { 175 200 char *tmpbuff; 176 int position, trans1, trans2, len, lastsame;201 int position, trans1, trans2, trans3, len, lastsame; 177 202 178 203 if (w==NULL) { … … 188 213 /* find the last char with the current format and color */ 189 214 trans1=owl_util_find_trans(f->fmbuff+position, len-position); 190 trans2=owl_util_find_trans(f->colorbuff+position, len-position); 191 192 if (trans1<trans2) { 193 lastsame=position+trans1; 194 } else { 195 lastsame=position+trans2; 196 } 215 trans2=owl_util_find_trans(f->fgcolorbuff+position, len-position); 216 trans3=owl_util_find_trans(f->bgcolorbuff+position, len-position); 217 218 lastsame = (trans1 < trans2) ? trans1 : trans2; 219 lastsame = (lastsame < trans3) ? lastsame : trans3; 220 lastsame += position; 197 221 198 222 /* set the format */ … … 211 235 /* warning, this is sort of a hack */ 212 236 if (owl_global_get_hascolors(&g)) { 213 if (f->colorbuff[position]!=OWL_COLOR_DEFAULT) { 214 wattron(w, COLOR_PAIR(f->colorbuff[position])); 237 int pair, fg, bg; 238 fg = f->fgcolorbuff[position]; 239 bg = f->bgcolorbuff[position]; 240 owl_function_debugmsg("waddstr: fg(%i) bg(%i).", fg, bg); 241 242 pair = owl_fmtext_get_colorpair(fg, bg); 243 if (pair != -1) { 244 wattron(w, COLOR_PAIR(pair)); 215 245 } 216 246 } … … 355 385 dst->textbuff=owl_malloc(mallocsize); 356 386 dst->fmbuff=owl_malloc(mallocsize); 357 dst->colorbuff=owl_malloc(mallocsize); 387 dst->fgcolorbuff=owl_malloc(mallocsize); 388 dst->bgcolorbuff=owl_malloc(mallocsize); 358 389 memcpy(dst->textbuff, src->textbuff, src->textlen+1); 359 390 memcpy(dst->fmbuff, src->fmbuff, src->textlen); 360 memcpy(dst->colorbuff, src->colorbuff, src->textlen); 391 memcpy(dst->fgcolorbuff, src->fgcolorbuff, src->textlen); 392 memcpy(dst->bgcolorbuff, src->bgcolorbuff, src->textlen); 361 393 } 362 394 … … 415 447 if (!ptr) { 416 448 /* add all the rest of the text and exit */ 417 owl_fmtext_append_attr(f, txtptr, curattrs, curcolor );449 owl_fmtext_append_attr(f, txtptr, curattrs, curcolor, OWL_COLOR_DEFAULT); 418 450 return; 419 451 } else if (ptr[0]=='@') { … … 422 454 strncpy(buff, txtptr, ptr-txtptr); 423 455 buff[ptr-txtptr]='\0'; 424 owl_fmtext_append_attr(f, buff, curattrs, curcolor );456 owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); 425 457 owl_free(buff); 426 458 … … 432 464 /* if we've hit our max stack depth, print the @ and move on */ 433 465 if (stacksize==32) { 434 owl_fmtext_append_attr(f, "@", curattrs, curcolor );466 owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); 435 467 txtptr++; 436 468 continue; … … 439 471 /* if it's an @@, print an @ and continue */ 440 472 if (txtptr[1]=='@') { 441 owl_fmtext_append_attr(f, "@", curattrs, curcolor );473 owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); 442 474 txtptr+=2; 443 475 continue; … … 447 479 tmpptr=strpbrk(txtptr, "(<[{ "); 448 480 if (!tmpptr || tmpptr[0]==' ') { 449 owl_fmtext_append_attr(f, "@", curattrs, curcolor );481 owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); 450 482 txtptr++; 451 483 continue; … … 524 556 * but zwgc seems to be smarter about some screw cases than I am 525 557 */ 526 owl_fmtext_append_attr(f, "@", curattrs, curcolor );558 owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); 527 559 txtptr++; 528 560 continue; … … 534 566 strncpy(buff, txtptr, ptr-txtptr); 535 567 buff[ptr-txtptr]='\0'; 536 owl_fmtext_append_attr(f, buff, curattrs, curcolor );568 owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); 537 569 owl_free(buff); 538 570 … … 545 577 buff[0]=ptr[0]; 546 578 buff[1]='\0'; 547 owl_fmtext_append_attr(f, buff, curattrs, curcolor );579 owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); 548 580 owl_free(buff); 549 581 txtptr++; … … 570 602 buff[0]=ptr[0]; 571 603 buff[1]='\0'; 572 owl_fmtext_append_attr(f, buff, curattrs, curcolor );604 owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); 573 605 owl_free(buff); 574 606 txtptr++; … … 580 612 strncpy(buff, txtptr, ptr-txtptr+1); 581 613 buff[ptr-txtptr+1]='\0'; 582 owl_fmtext_append_attr(f, buff, curattrs, curcolor );614 owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); 583 615 owl_free(buff); 584 616 txtptr=ptr+1; … … 621 653 if (f->textbuff) owl_free(f->textbuff); 622 654 if (f->fmbuff) owl_free(f->fmbuff); 623 if (f->colorbuff) owl_free(f->colorbuff); 624 } 625 655 if (f->fgcolorbuff) owl_free(f->fgcolorbuff); 656 if (f->bgcolorbuff) owl_free(f->bgcolorbuff); 657 } 658 659 /*** Color Pair manager ***/ 660 void owl_fmtext_init_colorpair_mgr(owl_colorpair_mgr *cpmgr) 661 { 662 // This could be a bitarray if we wanted to save memory. 663 int i, j, colors; 664 cpmgr->used = owl_malloc(COLOR_PAIRS * sizeof(char)); 665 memset(cpmgr->used, 0, COLOR_PAIRS * sizeof(char)); 666 667 colors = COLORS + 1; // 1 to account for "default". 668 cpmgr->pairs = owl_malloc(colors * sizeof(int*)); 669 for(i = 0; i < colors; i++) { 670 // Because we're going to have a pair set for any fg and the 671 // default bg, we don't need to account for default here. 672 cpmgr->pairs[i] = owl_malloc(COLORS * sizeof(int)); 673 for(j = 0; j < COLORS; j++) { 674 cpmgr->pairs[i][j] = -1; 675 } 676 } 677 } 678 679 /* Reset used list */ 680 void owl_fmtext_reset_colorpairs() 681 { 682 memset(owl_global_get_colorpair_mgr(&g)->used, 0, COLOR_PAIRS * sizeof(char)); 683 } 684 685 /* Assign pairs by request */ 686 int owl_fmtext_get_colorpair(int fg, int bg) 687 { 688 owl_colorpair_mgr *cpmgr; 689 short pair, i; 690 if (fg == OWL_COLOR_DEFAULT) fg = -1; 691 if (bg == OWL_COLOR_DEFAULT) { 692 pair = fg; 693 } else { 694 cpmgr = owl_global_get_colorpair_mgr(&g); 695 pair = cpmgr->pairs[fg+1][bg]; 696 if (pair != -1) { 697 short oldfg, oldbg; 698 pair_content(pair, &oldfg, &oldbg); 699 if (cpmgr->pairs[oldfg+1][oldbg] == pair) { 700 cpmgr->pairs[oldfg+1][oldbg] = -1; 701 } 702 init_pair(pair, fg, bg); 703 cpmgr->pairs[fg+1][bg] = pair; 704 cpmgr->used[pair] = 1; 705 } else { 706 // Skip the first COLORS, sicne they're static. 707 for(i = COLORS; i < COLOR_PAIRS; i++) { 708 if (0 == cpmgr->used[i]) { 709 pair = i; 710 break; 711 } 712 } 713 if (pair != -1) { 714 short oldfg, oldbg; 715 pair_content(pair, &oldfg, &oldbg); 716 if (cpmgr->pairs[oldfg+1][oldbg] == pair) { 717 cpmgr->pairs[oldfg+1][oldbg] = -1; 718 } 719 init_pair(pair, fg, bg); 720 cpmgr->pairs[fg+1][bg] = pair; 721 cpmgr->used[pair] = 1; 722 } else { 723 // Fail to skipping background. 724 pair = fg; 725 } 726 } 727 } 728 return pair; 729 }
Note: See TracChangeset
for help on using the changeset viewer.