| 1 | #include "owl.h" |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <string.h> |
|---|
| 4 | |
|---|
| 5 | static const char fileIdent[] = "$Id$"; |
|---|
| 6 | |
|---|
| 7 | /* initialize an fmtext with no data */ |
|---|
| 8 | void owl_fmtext_init_null(owl_fmtext *f) |
|---|
| 9 | { |
|---|
| 10 | f->textlen = 0; |
|---|
| 11 | f->bufflen = 5; |
|---|
| 12 | f->textbuff = owl_malloc(5); |
|---|
| 13 | f->textbuff[0] = 0; |
|---|
| 14 | f->default_attrs = OWL_FMTEXT_ATTR_NONE; |
|---|
| 15 | f->default_fgcolor = OWL_COLOR_DEFAULT; |
|---|
| 16 | f->default_bgcolor = OWL_COLOR_DEFAULT; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | /* Clear the data from an fmtext, but don't deallocate memory. This |
|---|
| 20 | fmtext can then be appended to again. */ |
|---|
| 21 | void owl_fmtext_clear(owl_fmtext *f) |
|---|
| 22 | { |
|---|
| 23 | f->textlen = 0; |
|---|
| 24 | f->textbuff[0] = 0; |
|---|
| 25 | f->default_attrs = OWL_FMTEXT_ATTR_NONE; |
|---|
| 26 | f->default_fgcolor = OWL_COLOR_DEFAULT; |
|---|
| 27 | f->default_bgcolor = OWL_COLOR_DEFAULT; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | void _owl_fmtext_realloc(owl_fmtext *f, int newlen) /*noproto*/ |
|---|
| 31 | { |
|---|
| 32 | if(newlen + 1 > f->bufflen) { |
|---|
| 33 | f->textbuff = owl_realloc(f->textbuff, newlen + 1); |
|---|
| 34 | f->bufflen = newlen+1; |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | int owl_fmtext_is_format_char(gunichar c) |
|---|
| 39 | { |
|---|
| 40 | if ((c & ~OWL_FMTEXT_UC_ATTR_MASK) == OWL_FMTEXT_UC_ATTR) return 1; |
|---|
| 41 | if ((c & ~(OWL_FMTEXT_UC_ALLCOLOR_MASK)) == OWL_FMTEXT_UC_COLOR_BASE) return 1; |
|---|
| 42 | return 0; |
|---|
| 43 | } |
|---|
| 44 | /* append text to the end of 'f' with attribute 'attr' and color |
|---|
| 45 | * 'color' |
|---|
| 46 | */ |
|---|
| 47 | void owl_fmtext_append_attr(owl_fmtext *f, char *text, char attr, short fgcolor, short bgcolor) |
|---|
| 48 | { |
|---|
| 49 | char attrbuff[6]; |
|---|
| 50 | int newlen, a = 0, fg = 0, bg = 0; |
|---|
| 51 | |
|---|
| 52 | if (attr != OWL_FMTEXT_ATTR_NONE) a=1; |
|---|
| 53 | if (fgcolor != OWL_COLOR_DEFAULT) fg=1; |
|---|
| 54 | if (bgcolor != OWL_COLOR_DEFAULT) bg=1; |
|---|
| 55 | |
|---|
| 56 | /* Plane-16 characters in UTF-8 are 4 bytes long. */ |
|---|
| 57 | newlen = strlen(f->textbuff) + strlen(text) + (8 * (a + fg + bg)); |
|---|
| 58 | _owl_fmtext_realloc(f, newlen); |
|---|
| 59 | |
|---|
| 60 | /* Set attributes */ |
|---|
| 61 | if (a) { |
|---|
| 62 | memset(attrbuff,0,6); |
|---|
| 63 | g_unichar_to_utf8(OWL_FMTEXT_UC_ATTR | attr, attrbuff); |
|---|
| 64 | strcat(f->textbuff, attrbuff); |
|---|
| 65 | } |
|---|
| 66 | if (fg) { |
|---|
| 67 | memset(attrbuff,0,6); |
|---|
| 68 | g_unichar_to_utf8(OWL_FMTEXT_UC_FGCOLOR | fgcolor, attrbuff); |
|---|
| 69 | strcat(f->textbuff, attrbuff); |
|---|
| 70 | } |
|---|
| 71 | if (bg) { |
|---|
| 72 | memset(attrbuff,0,6); |
|---|
| 73 | g_unichar_to_utf8(OWL_FMTEXT_UC_BGCOLOR | bgcolor, attrbuff); |
|---|
| 74 | strcat(f->textbuff, attrbuff); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | strcat(f->textbuff, text); |
|---|
| 78 | |
|---|
| 79 | /* Reset attributes */ |
|---|
| 80 | if (bg) strcat(f->textbuff, OWL_FMTEXT_UTF8_BGDEFAULT); |
|---|
| 81 | if (fg) strcat(f->textbuff, OWL_FMTEXT_UTF8_FGDEFAULT); |
|---|
| 82 | if (a) strcat(f->textbuff, OWL_FMTEXT_UTF8_ATTR_NONE); |
|---|
| 83 | f->textlen=newlen; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | /* Append normal, uncolored text 'text' to 'f' */ |
|---|
| 87 | void owl_fmtext_append_normal(owl_fmtext *f, char *text) |
|---|
| 88 | { |
|---|
| 89 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /* Append normal, uncolored text specified by format string to 'f' */ |
|---|
| 93 | void owl_fmtext_appendf_normal(owl_fmtext *f, char *fmt, ...) |
|---|
| 94 | { |
|---|
| 95 | va_list ap; |
|---|
| 96 | char *buff; |
|---|
| 97 | |
|---|
| 98 | va_start(ap, fmt); |
|---|
| 99 | buff = g_strdup_vprintf(fmt, ap); |
|---|
| 100 | va_end(ap); |
|---|
| 101 | if (!buff) |
|---|
| 102 | return; |
|---|
| 103 | owl_fmtext_append_attr(f, buff, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | /* Append normal text 'text' to 'f' with color 'color' */ |
|---|
| 107 | void owl_fmtext_append_normal_color(owl_fmtext *f, char *text, int fgcolor, int bgcolor) |
|---|
| 108 | { |
|---|
| 109 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, fgcolor, bgcolor); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /* Append bold text 'text' to 'f' */ |
|---|
| 113 | void owl_fmtext_append_bold(owl_fmtext *f, char *text) |
|---|
| 114 | { |
|---|
| 115 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /* Append reverse video text 'text' to 'f' */ |
|---|
| 119 | void owl_fmtext_append_reverse(owl_fmtext *f, char *text) |
|---|
| 120 | { |
|---|
| 121 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /* Append reversed and bold, uncolored text 'text' to 'f' */ |
|---|
| 125 | void owl_fmtext_append_reversebold(owl_fmtext *f, char *text) |
|---|
| 126 | { |
|---|
| 127 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE | OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /* Add the attribute 'attr' to the default atts for the text in 'f' */ |
|---|
| 131 | void owl_fmtext_addattr(owl_fmtext *f, char attr) |
|---|
| 132 | { |
|---|
| 133 | /* add the attribute to all text */ |
|---|
| 134 | f->default_attrs |= attr; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /* Set the default foreground color for this fmtext to 'color'. |
|---|
| 138 | * Only affects text that is colored default. |
|---|
| 139 | */ |
|---|
| 140 | void owl_fmtext_colorize(owl_fmtext *f, int color) |
|---|
| 141 | { |
|---|
| 142 | f->default_fgcolor = color; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /* Set the default foreground color for this fmtext to 'color'. |
|---|
| 146 | * Only affects text that is colored default. |
|---|
| 147 | */ |
|---|
| 148 | void owl_fmtext_colorizebg(owl_fmtext *f, int color) |
|---|
| 149 | { |
|---|
| 150 | f->default_bgcolor = color; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /* Internal function. Parse attrbute character. */ |
|---|
| 154 | void _owl_fmtext_update_attributes(gunichar c, char *attr, short *fgcolor, short *bgcolor) /*noproto*/ |
|---|
| 155 | { |
|---|
| 156 | if ((c & OWL_FMTEXT_UC_ATTR) == OWL_FMTEXT_UC_ATTR) { |
|---|
| 157 | *attr = c & OWL_FMTEXT_UC_ATTR_MASK; |
|---|
| 158 | } |
|---|
| 159 | else if ((c & OWL_FMTEXT_UC_COLOR_BASE) == OWL_FMTEXT_UC_COLOR_BASE) { |
|---|
| 160 | if ((c & OWL_FMTEXT_UC_BGCOLOR) == OWL_FMTEXT_UC_BGCOLOR) { |
|---|
| 161 | *bgcolor = (c == OWL_FMTEXT_UC_BGDEFAULT |
|---|
| 162 | ? OWL_COLOR_DEFAULT |
|---|
| 163 | : c & OWL_FMTEXT_UC_COLOR_MASK); |
|---|
| 164 | } |
|---|
| 165 | else if ((c & OWL_FMTEXT_UC_FGCOLOR) == OWL_FMTEXT_UC_FGCOLOR) { |
|---|
| 166 | *fgcolor = (c == OWL_FMTEXT_UC_FGDEFAULT |
|---|
| 167 | ? OWL_COLOR_DEFAULT |
|---|
| 168 | : c & OWL_FMTEXT_UC_COLOR_MASK); |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /* Internal function. Scan for attribute characters. */ |
|---|
| 174 | void _owl_fmtext_scan_attributes(owl_fmtext *f, int start, char *attr, short *fgcolor, short *bgcolor) /*noproto*/ |
|---|
| 175 | { |
|---|
| 176 | char *p; |
|---|
| 177 | p = strchr(f->textbuff, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
|---|
| 178 | while (p && p < f->textbuff + start) { |
|---|
| 179 | _owl_fmtext_update_attributes(g_utf8_get_char(p), attr, fgcolor, bgcolor); |
|---|
| 180 | p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | /* Internal function. Append text from 'in' between index 'start' and |
|---|
| 185 | * 'stop', inclusive, to the end of 'f'. This function works with |
|---|
| 186 | * bytes. |
|---|
| 187 | */ |
|---|
| 188 | void _owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in, int start, int stop) /*noproto*/ |
|---|
| 189 | { |
|---|
| 190 | char attrbuff[6]; |
|---|
| 191 | int newlen, a = 0, fg = 0, bg = 0; |
|---|
| 192 | char attr = 0; |
|---|
| 193 | short fgcolor = OWL_COLOR_DEFAULT; |
|---|
| 194 | short bgcolor = OWL_COLOR_DEFAULT; |
|---|
| 195 | |
|---|
| 196 | _owl_fmtext_scan_attributes(in, start, &attr, &fgcolor, &bgcolor); |
|---|
| 197 | if (attr != OWL_FMTEXT_ATTR_NONE) a=1; |
|---|
| 198 | if (fgcolor != OWL_COLOR_DEFAULT) fg=1; |
|---|
| 199 | if (bgcolor != OWL_COLOR_DEFAULT) bg=1; |
|---|
| 200 | |
|---|
| 201 | /* We will reset to defaults after appending the text. We may need |
|---|
| 202 | to set initial attributes. */ |
|---|
| 203 | newlen=strlen(f->textbuff)+(stop-start+1) + (4 * (a + fg + bg)) + 12; |
|---|
| 204 | _owl_fmtext_realloc(f, newlen); |
|---|
| 205 | |
|---|
| 206 | if (a) { |
|---|
| 207 | memset(attrbuff,0,6); |
|---|
| 208 | g_unichar_to_utf8(OWL_FMTEXT_UC_ATTR | attr, attrbuff); |
|---|
| 209 | strcat(f->textbuff, attrbuff); |
|---|
| 210 | } |
|---|
| 211 | if (fg) { |
|---|
| 212 | memset(attrbuff,0,6); |
|---|
| 213 | g_unichar_to_utf8(OWL_FMTEXT_UC_FGCOLOR | fgcolor, attrbuff); |
|---|
| 214 | strcat(f->textbuff, attrbuff); |
|---|
| 215 | } |
|---|
| 216 | if (bg) { |
|---|
| 217 | memset(attrbuff,0,6); |
|---|
| 218 | g_unichar_to_utf8(OWL_FMTEXT_UC_BGCOLOR | bgcolor, attrbuff); |
|---|
| 219 | strcat(f->textbuff, attrbuff); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | strncat(f->textbuff, in->textbuff+start, stop-start+1); |
|---|
| 223 | |
|---|
| 224 | /* Reset attributes */ |
|---|
| 225 | strcat(f->textbuff, OWL_FMTEXT_UTF8_BGDEFAULT); |
|---|
| 226 | strcat(f->textbuff, OWL_FMTEXT_UTF8_FGDEFAULT); |
|---|
| 227 | strcat(f->textbuff, OWL_FMTEXT_UTF8_ATTR_NONE); |
|---|
| 228 | |
|---|
| 229 | f->textbuff[newlen]='\0'; |
|---|
| 230 | f->textlen=newlen; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /* append fmtext 'in' to 'f' */ |
|---|
| 234 | void owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in) |
|---|
| 235 | { |
|---|
| 236 | _owl_fmtext_append_fmtext(f, in, 0, in->textlen); |
|---|
| 237 | |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | /* Append 'nspaces' number of spaces to the end of 'f' */ |
|---|
| 241 | void owl_fmtext_append_spaces(owl_fmtext *f, int nspaces) |
|---|
| 242 | { |
|---|
| 243 | int i; |
|---|
| 244 | for (i=0; i<nspaces; i++) { |
|---|
| 245 | owl_fmtext_append_normal(f, " "); |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | /* Return a plain version of the fmtext. Caller is responsible for |
|---|
| 250 | * freeing the return |
|---|
| 251 | */ |
|---|
| 252 | char *owl_fmtext_print_plain(owl_fmtext *f) |
|---|
| 253 | { |
|---|
| 254 | return owl_strip_format_chars(f->textbuff); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | void _owl_fmtext_wattrset(WINDOW *w, int attrs) /*noproto*/ |
|---|
| 258 | { |
|---|
| 259 | wattrset(w, A_NORMAL); |
|---|
| 260 | if (attrs & OWL_FMTEXT_ATTR_BOLD) wattron(w, A_BOLD); |
|---|
| 261 | if (attrs & OWL_FMTEXT_ATTR_REVERSE) wattron(w, A_REVERSE); |
|---|
| 262 | if (attrs & OWL_FMTEXT_ATTR_UNDERLINE) wattron(w, A_UNDERLINE); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | void _owl_fmtext_update_colorpair(short fg, short bg, short *pair) /*noproto*/ |
|---|
| 266 | { |
|---|
| 267 | if (owl_global_get_hascolors(&g)) { |
|---|
| 268 | *pair = owl_fmtext_get_colorpair(fg, bg); |
|---|
| 269 | } |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | void _owl_fmtext_wcolor_set(WINDOW *w, short pair) /*noproto*/ |
|---|
| 273 | { |
|---|
| 274 | if (owl_global_get_hascolors(&g)) { |
|---|
| 275 | wcolor_set(w,pair,NULL); |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | /* add the formatted text to the curses window 'w'. The window 'w' |
|---|
| 280 | * must already be initiatlized with curses |
|---|
| 281 | */ |
|---|
| 282 | void _owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w, int do_search) /*noproto*/ |
|---|
| 283 | { |
|---|
| 284 | /* char *tmpbuff; */ |
|---|
| 285 | /* int position, trans1, trans2, trans3, len, lastsame; */ |
|---|
| 286 | char *s, *p; |
|---|
| 287 | char attr; |
|---|
| 288 | short fg, bg, pair; |
|---|
| 289 | int search_results, search_len; |
|---|
| 290 | |
|---|
| 291 | if (w==NULL) { |
|---|
| 292 | owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr."); |
|---|
| 293 | return; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | search_results = (do_search |
|---|
| 297 | ? owl_fmtext_search(f, owl_global_get_search_string(&g)) |
|---|
| 298 | : 0); |
|---|
| 299 | search_len = (search_results |
|---|
| 300 | ? strlen(owl_global_get_search_string(&g)) |
|---|
| 301 | : 0); |
|---|
| 302 | s = f->textbuff; |
|---|
| 303 | /* Set default attributes. */ |
|---|
| 304 | attr = f->default_attrs; |
|---|
| 305 | fg = f->default_fgcolor; |
|---|
| 306 | bg = f->default_bgcolor; |
|---|
| 307 | _owl_fmtext_wattrset(w, attr); |
|---|
| 308 | _owl_fmtext_update_colorpair(fg, bg, &pair); |
|---|
| 309 | _owl_fmtext_wcolor_set(w, pair); |
|---|
| 310 | |
|---|
| 311 | /* Find next possible format character. */ |
|---|
| 312 | p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
|---|
| 313 | while(p) { |
|---|
| 314 | if (owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
|---|
| 315 | /* Deal with all text from last insert to here. */ |
|---|
| 316 | char tmp; |
|---|
| 317 | |
|---|
| 318 | tmp = p[0]; |
|---|
| 319 | p[0] = '\0'; |
|---|
| 320 | if (search_results) { |
|---|
| 321 | /* Search is active, so highlight search results. */ |
|---|
| 322 | char tmp2, *ss; |
|---|
| 323 | ss = stristr(s, owl_global_get_search_string(&g)); |
|---|
| 324 | while (ss) { |
|---|
| 325 | /* Found search string, highlight it. */ |
|---|
| 326 | |
|---|
| 327 | tmp2 = ss[0]; |
|---|
| 328 | ss[0] = '\0'; |
|---|
| 329 | waddstr(w, s); |
|---|
| 330 | ss[0] = tmp2; |
|---|
| 331 | |
|---|
| 332 | _owl_fmtext_wattrset(w, attr ^ OWL_FMTEXT_ATTR_REVERSE); |
|---|
| 333 | _owl_fmtext_wcolor_set(w, pair); |
|---|
| 334 | |
|---|
| 335 | tmp2 = ss[search_len]; |
|---|
| 336 | ss[search_len] = '\0'; |
|---|
| 337 | waddstr(w, ss); |
|---|
| 338 | ss[search_len] = tmp2; |
|---|
| 339 | |
|---|
| 340 | _owl_fmtext_wattrset(w, attr); |
|---|
| 341 | _owl_fmtext_wcolor_set(w, pair); |
|---|
| 342 | |
|---|
| 343 | s = ss + search_len; |
|---|
| 344 | ss = stristr(s, owl_global_get_search_string(&g)); |
|---|
| 345 | } |
|---|
| 346 | } |
|---|
| 347 | /* Deal with remaining part of string. */ |
|---|
| 348 | waddstr(w, s); |
|---|
| 349 | p[0] = tmp; |
|---|
| 350 | |
|---|
| 351 | /* Deal with new attributes. Initialize to defaults, then |
|---|
| 352 | process all consecutive formatting characters. */ |
|---|
| 353 | attr = f->default_attrs; |
|---|
| 354 | fg = f->default_fgcolor; |
|---|
| 355 | bg = f->default_bgcolor; |
|---|
| 356 | while (p && owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
|---|
| 357 | _owl_fmtext_update_attributes(g_utf8_get_char(p), &attr, &fg, &bg); |
|---|
| 358 | p = g_utf8_next_char(p); |
|---|
| 359 | } |
|---|
| 360 | _owl_fmtext_wattrset(w, attr | f->default_attrs); |
|---|
| 361 | if (fg == OWL_COLOR_DEFAULT) fg = f->default_fgcolor; |
|---|
| 362 | if (bg == OWL_COLOR_DEFAULT) bg = f->default_bgcolor; |
|---|
| 363 | _owl_fmtext_update_colorpair(fg, bg, &pair); |
|---|
| 364 | _owl_fmtext_wcolor_set(w, pair); |
|---|
| 365 | |
|---|
| 366 | /* Advance to next non-formatting character. */ |
|---|
| 367 | s = p; |
|---|
| 368 | p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
|---|
| 369 | } |
|---|
| 370 | else { |
|---|
| 371 | p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
|---|
| 372 | } |
|---|
| 373 | } |
|---|
| 374 | if (s) { |
|---|
| 375 | waddstr(w, s); |
|---|
| 376 | } |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | void owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w) |
|---|
| 380 | { |
|---|
| 381 | _owl_fmtext_curs_waddstr(f, w, owl_global_is_search_active(&g)); |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | void owl_fmtext_curs_waddstr_without_search(owl_fmtext *f, WINDOW *w) |
|---|
| 385 | { |
|---|
| 386 | _owl_fmtext_curs_waddstr(f, w, 0); |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | /* start with line 'aline' (where the first line is 0) and print |
|---|
| 390 | * 'lines' number of lines into 'out' |
|---|
| 391 | */ |
|---|
| 392 | int owl_fmtext_truncate_lines(owl_fmtext *in, int aline, int lines, owl_fmtext *out) |
|---|
| 393 | { |
|---|
| 394 | char *ptr1, *ptr2; |
|---|
| 395 | int i, offset; |
|---|
| 396 | |
|---|
| 397 | /* find the starting line */ |
|---|
| 398 | ptr1 = in->textbuff; |
|---|
| 399 | for (i = 0; i < aline; i++) { |
|---|
| 400 | ptr1 = strchr(ptr1, '\n'); |
|---|
| 401 | if (!ptr1) return(-1); |
|---|
| 402 | ptr1++; |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | /* ptr1 now holds the starting point */ |
|---|
| 406 | |
|---|
| 407 | /* copy the default attributes */ |
|---|
| 408 | out->default_attrs = in->default_attrs; |
|---|
| 409 | out->default_fgcolor = in->default_fgcolor; |
|---|
| 410 | out->default_bgcolor = in->default_bgcolor; |
|---|
| 411 | |
|---|
| 412 | /* copy in the next 'lines' lines */ |
|---|
| 413 | if (lines < 1) return(-1); |
|---|
| 414 | |
|---|
| 415 | for (i = 0; i < lines; i++) { |
|---|
| 416 | offset = ptr1 - in->textbuff; |
|---|
| 417 | ptr2 = strchr(ptr1, '\n'); |
|---|
| 418 | if (!ptr2) { |
|---|
| 419 | _owl_fmtext_append_fmtext(out, in, offset, (in->textlen) - 1); |
|---|
| 420 | return(-1); |
|---|
| 421 | } |
|---|
| 422 | _owl_fmtext_append_fmtext(out, in, offset, (ptr2 - ptr1) + offset); |
|---|
| 423 | ptr1 = ptr2 + 1; |
|---|
| 424 | } |
|---|
| 425 | return(0); |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | /* Truncate the message so that each line begins at column 'acol' and |
|---|
| 429 | * ends at 'bcol' or sooner. The first column is number 0. The new |
|---|
| 430 | * message is placed in 'out'. The message is * expected to end in a |
|---|
| 431 | * new line for now. NOTE: This needs to be modified to deal with |
|---|
| 432 | * backing up if we find a SPACING COMBINING MARK at the end of a |
|---|
| 433 | * line. If that happens, we should back up to the last non-mark |
|---|
| 434 | * character and stop there. |
|---|
| 435 | */ |
|---|
| 436 | void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out) |
|---|
| 437 | { |
|---|
| 438 | char *ptr_s, *ptr_e, *ptr_c, *last; |
|---|
| 439 | int col, st, padding, chwidth; |
|---|
| 440 | |
|---|
| 441 | /* copy the default attributes */ |
|---|
| 442 | out->default_attrs = in->default_attrs; |
|---|
| 443 | out->default_fgcolor = in->default_fgcolor; |
|---|
| 444 | out->default_bgcolor = in->default_bgcolor; |
|---|
| 445 | |
|---|
| 446 | last=in->textbuff+in->textlen-1; |
|---|
| 447 | ptr_s=in->textbuff; |
|---|
| 448 | while (ptr_s <= last) { |
|---|
| 449 | ptr_e=strchr(ptr_s, '\n'); |
|---|
| 450 | if (!ptr_e) { |
|---|
| 451 | /* but this shouldn't happen if we end in a \n */ |
|---|
| 452 | break; |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | if (ptr_e == ptr_s) { |
|---|
| 456 | owl_fmtext_append_normal(out, "\n"); |
|---|
| 457 | ++ptr_s; |
|---|
| 458 | continue; |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | col = 0; |
|---|
| 462 | st = 0; |
|---|
| 463 | padding = 0; |
|---|
| 464 | chwidth = 0; |
|---|
| 465 | ptr_c = ptr_s; |
|---|
| 466 | while(ptr_c < ptr_e) { |
|---|
| 467 | gunichar c = g_utf8_get_char(ptr_c); |
|---|
| 468 | if (!owl_fmtext_is_format_char(c)) { |
|---|
| 469 | chwidth = mk_wcwidth(c); |
|---|
| 470 | if (col + chwidth > bcol) break; |
|---|
| 471 | |
|---|
| 472 | if (col >= acol) { |
|---|
| 473 | if (st == 0) { |
|---|
| 474 | ptr_s = ptr_c; |
|---|
| 475 | padding = col - acol; |
|---|
| 476 | ++st; |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | col += chwidth; |
|---|
| 480 | chwidth = 0; |
|---|
| 481 | } |
|---|
| 482 | ptr_c = g_utf8_next_char(ptr_c); |
|---|
| 483 | } |
|---|
| 484 | if (st) { |
|---|
| 485 | /* lead padding */ |
|---|
| 486 | owl_fmtext_append_spaces(out, padding); |
|---|
| 487 | if (ptr_c == ptr_e) { |
|---|
| 488 | /* We made it to the newline. */ |
|---|
| 489 | _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff); |
|---|
| 490 | } |
|---|
| 491 | else if (chwidth > 1) { |
|---|
| 492 | /* Last char is wide, truncate. */ |
|---|
| 493 | _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff - 1); |
|---|
| 494 | owl_fmtext_append_normal(out, "\n"); |
|---|
| 495 | } |
|---|
| 496 | else { |
|---|
| 497 | /* Last char fits perfectly, We skip to the next char and back |
|---|
| 498 | * up a byte to make sure we get it all. |
|---|
| 499 | */ |
|---|
| 500 | ptr_c = g_utf8_next_char(ptr_c); |
|---|
| 501 | _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff - 1); |
|---|
| 502 | } |
|---|
| 503 | } |
|---|
| 504 | else { |
|---|
| 505 | owl_fmtext_append_normal(out, "\n"); |
|---|
| 506 | } |
|---|
| 507 | ptr_s = g_utf8_next_char(ptr_e); |
|---|
| 508 | } |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | /* Return the number of lines in 'f' */ |
|---|
| 512 | int owl_fmtext_num_lines(owl_fmtext *f) |
|---|
| 513 | { |
|---|
| 514 | int lines, i; |
|---|
| 515 | |
|---|
| 516 | if (f->textlen==0) return(0); |
|---|
| 517 | |
|---|
| 518 | lines=0; |
|---|
| 519 | for (i=0; i<f->textlen; i++) { |
|---|
| 520 | if (f->textbuff[i]=='\n') lines++; |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | /* if the last char wasn't a \n there's one more line */ |
|---|
| 524 | if (f->textbuff[i-1]!='\n') lines++; |
|---|
| 525 | |
|---|
| 526 | return(lines); |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | char *owl_fmtext_get_text(owl_fmtext *f) |
|---|
| 530 | { |
|---|
| 531 | return(f->textbuff); |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | /* set the charater at 'index' to be 'char'. If index is out of |
|---|
| 535 | * bounds don't do anything. If c or char at index is not ASCII, don't |
|---|
| 536 | * do anything because it's not UTF-8 safe. */ |
|---|
| 537 | void owl_fmtext_set_char(owl_fmtext *f, int index, char ch) |
|---|
| 538 | { |
|---|
| 539 | if ((index < 0) || (index > f->textlen-1)) return; |
|---|
| 540 | /* NOT ASCII*/ |
|---|
| 541 | if (f->textbuff[index] & 0x80 || ch & 0x80) return; |
|---|
| 542 | f->textbuff[index]=ch; |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | /* Make a copy of the fmtext 'src' into 'dst' */ |
|---|
| 546 | void owl_fmtext_copy(owl_fmtext *dst, owl_fmtext *src) |
|---|
| 547 | { |
|---|
| 548 | int mallocsize; |
|---|
| 549 | |
|---|
| 550 | if (src->textlen==0) { |
|---|
| 551 | mallocsize=5; |
|---|
| 552 | } else { |
|---|
| 553 | mallocsize=src->textlen+2; |
|---|
| 554 | } |
|---|
| 555 | dst->textlen=src->textlen; |
|---|
| 556 | dst->bufflen=mallocsize; |
|---|
| 557 | dst->textbuff=owl_malloc(mallocsize); |
|---|
| 558 | memcpy(dst->textbuff, src->textbuff, src->textlen+1); |
|---|
| 559 | dst->default_attrs = src->default_attrs; |
|---|
| 560 | dst->default_fgcolor = src->default_fgcolor; |
|---|
| 561 | dst->default_bgcolor = src->default_bgcolor; |
|---|
| 562 | } |
|---|
| 563 | |
|---|
| 564 | /* return 1 if the string is found, 0 if not. This is a case |
|---|
| 565 | * insensitive search. |
|---|
| 566 | */ |
|---|
| 567 | int owl_fmtext_search(owl_fmtext *f, char *string) |
|---|
| 568 | { |
|---|
| 569 | if (stristr(f->textbuff, string)) return(1); |
|---|
| 570 | return(0); |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | /* Append the text 'text' to 'f' and interpret the zephyr style |
|---|
| 575 | * formatting syntax to set appropriate attributes. |
|---|
| 576 | */ |
|---|
| 577 | void owl_fmtext_append_ztext(owl_fmtext *f, char *text) |
|---|
| 578 | { |
|---|
| 579 | int stacksize, curattrs, curcolor; |
|---|
| 580 | char *ptr, *txtptr, *buff, *tmpptr; |
|---|
| 581 | int attrstack[32], chrstack[32], colorstack[32]; |
|---|
| 582 | |
|---|
| 583 | curattrs=OWL_FMTEXT_ATTR_NONE; |
|---|
| 584 | curcolor=OWL_COLOR_DEFAULT; |
|---|
| 585 | stacksize=0; |
|---|
| 586 | txtptr=text; |
|---|
| 587 | while (1) { |
|---|
| 588 | ptr=strpbrk(txtptr, "@{[<()>]}"); |
|---|
| 589 | if (!ptr) { |
|---|
| 590 | /* add all the rest of the text and exit */ |
|---|
| 591 | owl_fmtext_append_attr(f, txtptr, curattrs, curcolor, OWL_COLOR_DEFAULT); |
|---|
| 592 | return; |
|---|
| 593 | } else if (ptr[0]=='@') { |
|---|
| 594 | /* add the text up to this point then deal with the stack */ |
|---|
| 595 | buff=owl_malloc(ptr-txtptr+20); |
|---|
| 596 | strncpy(buff, txtptr, ptr-txtptr); |
|---|
| 597 | buff[ptr-txtptr]='\0'; |
|---|
| 598 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
|---|
| 599 | owl_free(buff); |
|---|
| 600 | |
|---|
| 601 | /* update pointer to point at the @ */ |
|---|
| 602 | txtptr=ptr; |
|---|
| 603 | |
|---|
| 604 | /* now the stack */ |
|---|
| 605 | |
|---|
| 606 | /* if we've hit our max stack depth, print the @ and move on */ |
|---|
| 607 | if (stacksize==32) { |
|---|
| 608 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
|---|
| 609 | txtptr++; |
|---|
| 610 | continue; |
|---|
| 611 | } |
|---|
| 612 | |
|---|
| 613 | /* if it's an @@, print an @ and continue */ |
|---|
| 614 | if (txtptr[1]=='@') { |
|---|
| 615 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
|---|
| 616 | txtptr+=2; |
|---|
| 617 | continue; |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | /* if there's no opener, print the @ and continue */ |
|---|
| 621 | tmpptr=strpbrk(txtptr, "(<[{ "); |
|---|
| 622 | if (!tmpptr || tmpptr[0]==' ') { |
|---|
| 623 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
|---|
| 624 | txtptr++; |
|---|
| 625 | continue; |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | /* check what command we've got, push it on the stack, start |
|---|
| 629 | using it, and continue ... unless it's a color command */ |
|---|
| 630 | buff=owl_malloc(tmpptr-ptr+20); |
|---|
| 631 | strncpy(buff, ptr, tmpptr-ptr); |
|---|
| 632 | buff[tmpptr-ptr]='\0'; |
|---|
| 633 | if (!strcasecmp(buff, "@bold")) { |
|---|
| 634 | attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD; |
|---|
| 635 | chrstack[stacksize]=tmpptr[0]; |
|---|
| 636 | colorstack[stacksize]=curcolor; |
|---|
| 637 | stacksize++; |
|---|
| 638 | curattrs|=OWL_FMTEXT_ATTR_BOLD; |
|---|
| 639 | txtptr+=6; |
|---|
| 640 | owl_free(buff); |
|---|
| 641 | continue; |
|---|
| 642 | } else if (!strcasecmp(buff, "@b")) { |
|---|
| 643 | attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD; |
|---|
| 644 | chrstack[stacksize]=tmpptr[0]; |
|---|
| 645 | colorstack[stacksize]=curcolor; |
|---|
| 646 | stacksize++; |
|---|
| 647 | curattrs|=OWL_FMTEXT_ATTR_BOLD; |
|---|
| 648 | txtptr+=3; |
|---|
| 649 | owl_free(buff); |
|---|
| 650 | continue; |
|---|
| 651 | } else if (!strcasecmp(buff, "@i")) { |
|---|
| 652 | attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE; |
|---|
| 653 | chrstack[stacksize]=tmpptr[0]; |
|---|
| 654 | colorstack[stacksize]=curcolor; |
|---|
| 655 | stacksize++; |
|---|
| 656 | curattrs|=OWL_FMTEXT_ATTR_UNDERLINE; |
|---|
| 657 | txtptr+=3; |
|---|
| 658 | owl_free(buff); |
|---|
| 659 | continue; |
|---|
| 660 | } else if (!strcasecmp(buff, "@italic")) { |
|---|
| 661 | attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE; |
|---|
| 662 | chrstack[stacksize]=tmpptr[0]; |
|---|
| 663 | colorstack[stacksize]=curcolor; |
|---|
| 664 | stacksize++; |
|---|
| 665 | curattrs|=OWL_FMTEXT_ATTR_UNDERLINE; |
|---|
| 666 | txtptr+=8; |
|---|
| 667 | owl_free(buff); |
|---|
| 668 | continue; |
|---|
| 669 | } else if (!strcasecmp(buff, "@")) { |
|---|
| 670 | attrstack[stacksize]=OWL_FMTEXT_ATTR_NONE; |
|---|
| 671 | chrstack[stacksize]=tmpptr[0]; |
|---|
| 672 | colorstack[stacksize]=curcolor; |
|---|
| 673 | stacksize++; |
|---|
| 674 | txtptr+=2; |
|---|
| 675 | owl_free(buff); |
|---|
| 676 | continue; |
|---|
| 677 | |
|---|
| 678 | /* if it's a color read the color, set the current color and |
|---|
| 679 | continue */ |
|---|
| 680 | } else if (!strcasecmp(buff, "@color") |
|---|
| 681 | && owl_global_get_hascolors(&g) |
|---|
| 682 | && owl_global_is_colorztext(&g)) { |
|---|
| 683 | owl_free(buff); |
|---|
| 684 | txtptr+=7; |
|---|
| 685 | tmpptr=strpbrk(txtptr, "@{[<()>]}"); |
|---|
| 686 | if (tmpptr && |
|---|
| 687 | ((txtptr[-1]=='(' && tmpptr[0]==')') || |
|---|
| 688 | (txtptr[-1]=='<' && tmpptr[0]=='>') || |
|---|
| 689 | (txtptr[-1]=='[' && tmpptr[0]==']') || |
|---|
| 690 | (txtptr[-1]=='{' && tmpptr[0]=='}'))) { |
|---|
| 691 | |
|---|
| 692 | /* grab the color name */ |
|---|
| 693 | buff=owl_malloc(tmpptr-txtptr+20); |
|---|
| 694 | strncpy(buff, txtptr, tmpptr-txtptr); |
|---|
| 695 | buff[tmpptr-txtptr]='\0'; |
|---|
| 696 | |
|---|
| 697 | /* set it as the current color */ |
|---|
| 698 | curcolor=owl_util_string_to_color(buff); |
|---|
| 699 | if (curcolor==-1) curcolor=OWL_COLOR_DEFAULT; |
|---|
| 700 | owl_free(buff); |
|---|
| 701 | txtptr=tmpptr+1; |
|---|
| 702 | continue; |
|---|
| 703 | |
|---|
| 704 | } else { |
|---|
| 705 | |
|---|
| 706 | } |
|---|
| 707 | |
|---|
| 708 | } else { |
|---|
| 709 | /* if we didn't understand it, we'll print it. This is different from zwgc |
|---|
| 710 | * but zwgc seems to be smarter about some screw cases than I am |
|---|
| 711 | */ |
|---|
| 712 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
|---|
| 713 | txtptr++; |
|---|
| 714 | continue; |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | } else if (ptr[0]=='}' || ptr[0]==']' || ptr[0]==')' || ptr[0]=='>') { |
|---|
| 718 | /* add the text up to this point first */ |
|---|
| 719 | buff=owl_malloc(ptr-txtptr+20); |
|---|
| 720 | strncpy(buff, txtptr, ptr-txtptr); |
|---|
| 721 | buff[ptr-txtptr]='\0'; |
|---|
| 722 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
|---|
| 723 | owl_free(buff); |
|---|
| 724 | |
|---|
| 725 | /* now deal with the closer */ |
|---|
| 726 | txtptr=ptr; |
|---|
| 727 | |
|---|
| 728 | /* first, if the stack is empty we must bail (just print and go) */ |
|---|
| 729 | if (stacksize==0) { |
|---|
| 730 | buff=owl_malloc(5); |
|---|
| 731 | buff[0]=ptr[0]; |
|---|
| 732 | buff[1]='\0'; |
|---|
| 733 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
|---|
| 734 | owl_free(buff); |
|---|
| 735 | txtptr++; |
|---|
| 736 | continue; |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | /* if the closing char is what's on the stack, turn off the |
|---|
| 740 | attribue and pop the stack */ |
|---|
| 741 | if ((ptr[0]==')' && chrstack[stacksize-1]=='(') || |
|---|
| 742 | (ptr[0]=='>' && chrstack[stacksize-1]=='<') || |
|---|
| 743 | (ptr[0]==']' && chrstack[stacksize-1]=='[') || |
|---|
| 744 | (ptr[0]=='}' && chrstack[stacksize-1]=='{')) { |
|---|
| 745 | int i; |
|---|
| 746 | stacksize--; |
|---|
| 747 | curattrs=OWL_FMTEXT_ATTR_NONE; |
|---|
| 748 | curcolor = colorstack[stacksize]; |
|---|
| 749 | for (i=0; i<stacksize; i++) { |
|---|
| 750 | curattrs|=attrstack[i]; |
|---|
| 751 | } |
|---|
| 752 | txtptr+=1; |
|---|
| 753 | continue; |
|---|
| 754 | } else { |
|---|
| 755 | /* otherwise print and continue */ |
|---|
| 756 | buff=owl_malloc(5); |
|---|
| 757 | buff[0]=ptr[0]; |
|---|
| 758 | buff[1]='\0'; |
|---|
| 759 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
|---|
| 760 | owl_free(buff); |
|---|
| 761 | txtptr++; |
|---|
| 762 | continue; |
|---|
| 763 | } |
|---|
| 764 | } else { |
|---|
| 765 | /* we've found an unattached opener, print everything and move on */ |
|---|
| 766 | buff=owl_malloc(ptr-txtptr+20); |
|---|
| 767 | strncpy(buff, txtptr, ptr-txtptr+1); |
|---|
| 768 | buff[ptr-txtptr+1]='\0'; |
|---|
| 769 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
|---|
| 770 | owl_free(buff); |
|---|
| 771 | txtptr=ptr+1; |
|---|
| 772 | continue; |
|---|
| 773 | } |
|---|
| 774 | } |
|---|
| 775 | } |
|---|
| 776 | |
|---|
| 777 | /* requires that the list values are strings or NULL. |
|---|
| 778 | * joins the elements together with join_with. |
|---|
| 779 | * If format_fn is specified, passes it the list element value |
|---|
| 780 | * and it will return a string which this needs to free. */ |
|---|
| 781 | void owl_fmtext_append_list(owl_fmtext *f, owl_list *l, char *join_with, char *(format_fn)(char *)) |
|---|
| 782 | { |
|---|
| 783 | int i, size; |
|---|
| 784 | char *elem; |
|---|
| 785 | char *text; |
|---|
| 786 | |
|---|
| 787 | size = owl_list_get_size(l); |
|---|
| 788 | for (i=0; i<size; i++) { |
|---|
| 789 | elem = (char*)owl_list_get_element(l,i); |
|---|
| 790 | if (elem && format_fn) { |
|---|
| 791 | text = format_fn(elem); |
|---|
| 792 | if (text) { |
|---|
| 793 | owl_fmtext_append_normal(f, text); |
|---|
| 794 | owl_free(text); |
|---|
| 795 | } |
|---|
| 796 | } else if (elem) { |
|---|
| 797 | owl_fmtext_append_normal(f, elem); |
|---|
| 798 | } |
|---|
| 799 | if ((i < size-1) && join_with) { |
|---|
| 800 | owl_fmtext_append_normal(f, join_with); |
|---|
| 801 | } |
|---|
| 802 | } |
|---|
| 803 | } |
|---|
| 804 | |
|---|
| 805 | /* Free all memory allocated by the object */ |
|---|
| 806 | void owl_fmtext_free(owl_fmtext *f) |
|---|
| 807 | { |
|---|
| 808 | if (f->textbuff) owl_free(f->textbuff); |
|---|
| 809 | } |
|---|
| 810 | |
|---|
| 811 | /*** Color Pair manager ***/ |
|---|
| 812 | void owl_fmtext_init_colorpair_mgr(owl_colorpair_mgr *cpmgr) |
|---|
| 813 | { |
|---|
| 814 | /* This could be a bitarray if we wanted to save memory. */ |
|---|
| 815 | short i, j; |
|---|
| 816 | cpmgr->next = 8; |
|---|
| 817 | |
|---|
| 818 | /* The test is <= because we allocate COLORS+1 entries. */ |
|---|
| 819 | cpmgr->pairs = owl_malloc((COLORS+1) * sizeof(short*)); |
|---|
| 820 | for(i = 0; i <= COLORS; i++) { |
|---|
| 821 | cpmgr->pairs[i] = owl_malloc((COLORS+1) * sizeof(short)); |
|---|
| 822 | for(j = 0; j <= COLORS; j++) { |
|---|
| 823 | cpmgr->pairs[i][j] = -1; |
|---|
| 824 | } |
|---|
| 825 | } |
|---|
| 826 | if (owl_global_get_hascolors(&g)) { |
|---|
| 827 | for(i = 0; i < 8; i++) { |
|---|
| 828 | short fg, bg; |
|---|
| 829 | if (i >= COLORS) continue; |
|---|
| 830 | pair_content(i, &fg, &bg); |
|---|
| 831 | cpmgr->pairs[fg+1][bg+1] = i; |
|---|
| 832 | } |
|---|
| 833 | } |
|---|
| 834 | } |
|---|
| 835 | |
|---|
| 836 | /* Reset used list */ |
|---|
| 837 | void owl_fmtext_reset_colorpairs() |
|---|
| 838 | { |
|---|
| 839 | if (owl_global_get_hascolors(&g)) { |
|---|
| 840 | short i, j; |
|---|
| 841 | owl_colorpair_mgr *cpmgr = owl_global_get_colorpair_mgr(&g); |
|---|
| 842 | cpmgr->next = 8; |
|---|
| 843 | |
|---|
| 844 | /* The test is <= because we allocated COLORS+1 entries. */ |
|---|
| 845 | for(i = 0; i <= COLORS; i++) { |
|---|
| 846 | for(j = 0; j <= COLORS; j++) { |
|---|
| 847 | cpmgr->pairs[i][j] = -1; |
|---|
| 848 | } |
|---|
| 849 | } |
|---|
| 850 | for(i = 0; i < 8; i++) { |
|---|
| 851 | short fg, bg; |
|---|
| 852 | if (i >= COLORS) continue; |
|---|
| 853 | pair_content(i, &fg, &bg); |
|---|
| 854 | cpmgr->pairs[fg+1][bg+1] = i; |
|---|
| 855 | } |
|---|
| 856 | } |
|---|
| 857 | } |
|---|
| 858 | |
|---|
| 859 | /* Assign pairs by request */ |
|---|
| 860 | short owl_fmtext_get_colorpair(int fg, int bg) |
|---|
| 861 | { |
|---|
| 862 | owl_colorpair_mgr *cpmgr; |
|---|
| 863 | short pair, default_bg; |
|---|
| 864 | |
|---|
| 865 | /* Sanity (Bounds) Check */ |
|---|
| 866 | if (fg > COLORS || fg < OWL_COLOR_DEFAULT) fg = OWL_COLOR_DEFAULT; |
|---|
| 867 | if (bg > COLORS || bg < OWL_COLOR_DEFAULT) bg = OWL_COLOR_DEFAULT; |
|---|
| 868 | |
|---|
| 869 | #ifdef HAVE_USE_DEFAULT_COLORS |
|---|
| 870 | if (fg == OWL_COLOR_DEFAULT) fg = -1; |
|---|
| 871 | default_bg = OWL_COLOR_DEFAULT; |
|---|
| 872 | #else |
|---|
| 873 | if (fg == OWL_COLOR_DEFAULT) fg = 0; |
|---|
| 874 | if (bg == OWL_COLOR_DEFAULT) bg = 0; |
|---|
| 875 | default_bg = COLOR_BLACK; |
|---|
| 876 | #endif |
|---|
| 877 | |
|---|
| 878 | /* looking for a pair we already set up for this draw. */ |
|---|
| 879 | cpmgr = owl_global_get_colorpair_mgr(&g); |
|---|
| 880 | pair = cpmgr->pairs[fg+1][bg+1]; |
|---|
| 881 | if (!(pair != -1 && pair < cpmgr->next)) { |
|---|
| 882 | /* If we didn't find a pair, search for a free one to assign. */ |
|---|
| 883 | pair = (cpmgr->next < COLOR_PAIRS) ? cpmgr->next : -1; |
|---|
| 884 | if (pair != -1) { |
|---|
| 885 | /* We found a free pair, initialize it. */ |
|---|
| 886 | init_pair(pair, fg, bg); |
|---|
| 887 | cpmgr->pairs[fg+1][bg+1] = pair; |
|---|
| 888 | cpmgr->next++; |
|---|
| 889 | } |
|---|
| 890 | else if (bg != OWL_COLOR_DEFAULT) { |
|---|
| 891 | /* We still don't have a pair, drop the background color. Too bad. */ |
|---|
| 892 | owl_function_debugmsg("colorpairs: color shortage - dropping background color."); |
|---|
| 893 | pair = owl_fmtext_get_colorpair(fg, OWL_COLOR_DEFAULT); |
|---|
| 894 | } |
|---|
| 895 | else { |
|---|
| 896 | /* We still don't have a pair, defaults all around. */ |
|---|
| 897 | owl_function_debugmsg("colorpairs: color shortage - dropping foreground and background color."); |
|---|
| 898 | pair = 0; |
|---|
| 899 | } |
|---|
| 900 | } |
|---|
| 901 | return pair; |
|---|
| 902 | } |
|---|