Changeset 8fa9562 for fmtext.c


Ignore:
Timestamp:
Jan 20, 2007, 9:49:55 PM (17 years ago)
Author:
Alejandro R. Sedeño <asedeno@mit.edu>
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
Message:
Added background color support to owl.

If we run out of color pairs during a painting of the screen, further
background colors will be silently dropped. I'm hoping this doesn't
happen often. The used pair list is reset with each draw, so only
what's actually on screen matters.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fmtext.c

    ra0a5179 r8fa9562  
    1111  f->textbuff=owl_strdup("");
    1212  f->fmbuff=owl_malloc(5);
    13   f->colorbuff=owl_malloc(5);
     13  f->fgcolorbuff=owl_malloc(5);
     14  f->bgcolorbuff=owl_malloc(5);
    1415  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;
    1618}
    1719
     
    4143 * to index 'last
    4244 */
    43 void _owl_fmtext_set_color(owl_fmtext *f, int color, int first, int last)
     45void _owl_fmtext_set_fgcolor(owl_fmtext *f, int color, int first, int last)
    4446{
    4547  int i;
    4648  for (i=first; i<=last; i++) {
    47     f->colorbuff[i]=(unsigned char) color;
     49    f->fgcolorbuff[i]=(unsigned char) color;
     50  }
     51}
     52
     53void _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;
    4858  }
    4959}
     
    5262 * 'color'
    5363 */
    54 void owl_fmtext_append_attr(owl_fmtext *f, char *text, int attr, int color)
     64void owl_fmtext_append_attr(owl_fmtext *f, char *text, int attr, int fgcolor, int bgcolor)
    5565{
    5666  int newlen;
     
    5969  f->textbuff=owl_realloc(f->textbuff, newlen+2);
    6070  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);
    6273
    6374  strcat(f->textbuff, text);
    6475  _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);
    6678  f->textlen=newlen;
    6779}
     
    7082void owl_fmtext_append_normal(owl_fmtext *f, char *text)
    7183{
    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);
    7385}
    7486
    7587/* 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);
     88void 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);
    7991}
    8092
     
    8294void owl_fmtext_append_bold(owl_fmtext *f, char *text)
    8395{
    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);
    8597}
    8698
     
    88100void owl_fmtext_append_reverse(owl_fmtext *f, char *text)
    89101{
    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);
    91103}
    92104
     
    94106void owl_fmtext_append_reversebold(owl_fmtext *f, char *text)
    95107{
    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);
    97109}
    98110
     
    114126void owl_fmtext_colorize(owl_fmtext *f, int color)
    115127{
    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' */
    117129  int i, j;
    118130
    119131  j=f->textlen;
    120132  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
     137void 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;
    122145  }
    123146}
     
    133156  f->textbuff=owl_realloc(f->textbuff, newlen+1);
    134157  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);
    136160
    137161  strncat(f->textbuff, in->textbuff+start, stop-start+1);
     
    139163  for (i=start; i<=stop; i++) {
    140164    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];
    142167  }
    143168  f->textlen=newlen;
     
    174199{
    175200  char *tmpbuff;
    176   int position, trans1, trans2, len, lastsame;
     201  int position, trans1, trans2, trans3, len, lastsame;
    177202
    178203  if (w==NULL) {
     
    188213    /* find the last char with the current format and color */
    189214    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;
    197221
    198222    /* set the format */
     
    211235    /* warning, this is sort of a hack */
    212236    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));
    215245      }
    216246    }
     
    355385  dst->textbuff=owl_malloc(mallocsize);
    356386  dst->fmbuff=owl_malloc(mallocsize);
    357   dst->colorbuff=owl_malloc(mallocsize);
     387  dst->fgcolorbuff=owl_malloc(mallocsize);
     388  dst->bgcolorbuff=owl_malloc(mallocsize);
    358389  memcpy(dst->textbuff, src->textbuff, src->textlen+1);
    359390  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);
    361393}
    362394
     
    415447    if (!ptr) {
    416448      /* 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);
    418450      return;
    419451    } else if (ptr[0]=='@') {
     
    422454      strncpy(buff, txtptr, ptr-txtptr);
    423455      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);
    425457      owl_free(buff);
    426458
     
    432464      /* if we've hit our max stack depth, print the @ and move on */
    433465      if (stacksize==32) {
    434         owl_fmtext_append_attr(f, "@", curattrs, curcolor);
     466        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
    435467        txtptr++;
    436468        continue;
     
    439471      /* if it's an @@, print an @ and continue */
    440472      if (txtptr[1]=='@') {
    441         owl_fmtext_append_attr(f, "@", curattrs, curcolor);
     473        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
    442474        txtptr+=2;
    443475        continue;
     
    447479      tmpptr=strpbrk(txtptr, "(<[{ ");
    448480      if (!tmpptr || tmpptr[0]==' ') {
    449         owl_fmtext_append_attr(f, "@", curattrs, curcolor);
     481        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
    450482        txtptr++;
    451483        continue;
     
    524556         * but zwgc seems to be smarter about some screw cases than I am
    525557         */
    526         owl_fmtext_append_attr(f, "@", curattrs, curcolor);
     558        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
    527559        txtptr++;
    528560        continue;
     
    534566      strncpy(buff, txtptr, ptr-txtptr);
    535567      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);
    537569      owl_free(buff);
    538570
     
    545577        buff[0]=ptr[0];
    546578        buff[1]='\0';
    547         owl_fmtext_append_attr(f, buff, curattrs, curcolor);
     579        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
    548580        owl_free(buff);
    549581        txtptr++;
     
    570602        buff[0]=ptr[0];
    571603        buff[1]='\0';
    572         owl_fmtext_append_attr(f, buff, curattrs, curcolor);
     604        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
    573605        owl_free(buff);
    574606        txtptr++;
     
    580612      strncpy(buff, txtptr, ptr-txtptr+1);
    581613      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);
    583615      owl_free(buff);
    584616      txtptr=ptr+1;
     
    621653  if (f->textbuff) owl_free(f->textbuff);
    622654  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 ***/
     660void 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 */
     680void 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 */
     686int 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.