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