comparison mcabber/src/screen.c @ 1489:e9c71ce96dca

Fixed the colorhandling for mucnicks and /color roster
author franky@veqlargh.fs
date Wed, 23 Apr 2008 20:34:56 +0200
parents b393b8cee171
children 50dbbca69b55
comparison
equal deleted inserted replaced
1488:b393b8cee171 1489:e9c71ce96dca
54 #include "histolog.h" 54 #include "histolog.h"
55 #include "settings.h" 55 #include "settings.h"
56 #include "utils.h" 56 #include "utils.h"
57 57
58 #define get_color(col) (COLOR_PAIR(col)|COLOR_ATTRIB[col]) 58 #define get_color(col) (COLOR_PAIR(col)|COLOR_ATTRIB[col])
59 #define compose_color(col) (COLOR_PAIR(col->color_pair)|col->color_attrib)
59 60
60 #define DEFAULT_LOG_WIN_HEIGHT (5+2) 61 #define DEFAULT_LOG_WIN_HEIGHT (5+2)
61 #define DEFAULT_ROSTER_WIDTH 24 62 #define DEFAULT_ROSTER_WIDTH 24
62 #define CHAT_WIN_HEIGHT (maxY-1-Log_Win_Height) 63 #define CHAT_WIN_HEIGHT (maxY-1-Log_Win_Height)
63 64
158 AspellConfig *spell_config; 159 AspellConfig *spell_config;
159 AspellSpeller *spell_checker; 160 AspellSpeller *spell_checker;
160 #endif 161 #endif
161 162
162 typedef struct { 163 typedef struct {
164 int color_pair;
165 int color_attrib;
166 } ccolor;
167
168 typedef struct {
163 char *status, *wildcard; 169 char *status, *wildcard;
164 int color; 170 ccolor * color;
165 GPatternSpec *compiled; 171 GPatternSpec *compiled;
166 } rostercolor; 172 } rostercolor;
167 173
168 static GSList *rostercolrules = NULL; 174 static GSList *rostercolrules = NULL;
169 175
170 static GHashTable *muccolors = NULL, *nickcolors = NULL; 176 static GHashTable *muccolors = NULL, *nickcolors = NULL;
171 177
172 typedef struct { 178 typedef struct {
173 bool manual; // Manually set? 179 bool manual; // Manually set?
174 int color; 180 ccolor * color;
175 } nickcolor; 181 } nickcolor;
176 182
177 static int nickcolcount = 0, *nickcols = NULL; 183 static int nickcolcount = 0;
184 static ccolor ** nickcols = NULL;
178 static muccoltype glob_muccol = MC_OFF; 185 static muccoltype glob_muccol = MC_OFF;
179 186
180 /* Functions */ 187 /* Functions */
181 188
182 static int color_conv_table[] = { 189 static int FindColor(const char *name)
183 COLOR_BLACK, 190 {
184 COLOR_RED, 191 int result;
185 COLOR_GREEN, 192
186 COLOR_YELLOW,
187 COLOR_BLUE,
188 COLOR_MAGENTA,
189 COLOR_CYAN,
190 COLOR_WHITE
191 };
192
193 static int color_conv_table_fg[] = {
194 COLOR_BLACK_FG,
195 COLOR_RED_FG,
196 COLOR_GREEN_FG,
197 COLOR_YELLOW_FG,
198 COLOR_BLUE_FG,
199 COLOR_MAGENTA_FG,
200 COLOR_CYAN_FG,
201 COLOR_WHITE_FG
202 };
203
204 static int color_to_color_fg(int color)
205 {
206 unsigned i = 0;
207 for ( ; i < sizeof color_conv_table / sizeof *color_conv_table; i++)
208 if (color == color_conv_table[i])
209 return color_conv_table_fg[i];
210 return -1;
211 }
212
213 static int color_fg_to_color(int color)
214 {
215 unsigned i = 0;
216 if (color >= COLOR_BLACK_BOLD_FG)
217 color -= COLOR_BLACK_BOLD_FG - COLOR_BLACK_FG;
218 for ( ; i < sizeof color_conv_table_fg / sizeof *color_conv_table_fg; i++)
219 if (color == color_conv_table_fg[i])
220 return color_conv_table[i];
221 return -1;
222 }
223
224 static int FindColorInternal(const char *name)
225 {
226 if (!strcmp(name, "default")) 193 if (!strcmp(name, "default"))
227 return -1; 194 return -1;
228 if (!strcmp(name, "black")) 195 if (!strcmp(name, "black"))
229 return COLOR_BLACK; 196 return COLOR_BLACK;
230 if (!strcmp(name, "red")) 197 if (!strcmp(name, "red"))
240 if (!strcmp(name, "cyan")) 207 if (!strcmp(name, "cyan"))
241 return COLOR_CYAN; 208 return COLOR_CYAN;
242 if (!strcmp(name, "white")) 209 if (!strcmp(name, "white"))
243 return COLOR_WHITE; 210 return COLOR_WHITE;
244 211
245 return -2;
246 }
247
248 static int FindColor(const char *name)
249 {
250 int result = FindColorInternal(name);
251 if (result != -2)
252 return result;
253
254 // Directly support 256-color values 212 // Directly support 256-color values
255 result = atoi(name); 213 result = atoi(name);
256 if (result > 0 && result < COLORS) 214 if (result > 0 && result < COLORS)
257 return result; 215 return result;
258 216
259 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Wrong color: %s", name); 217 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Wrong color: %s", name);
260 return -1; 218 return -1;
261 } 219 }
262 220
263 static int get_user_color(const char *color) 221 static ccolor * get_user_color(const char *color)
264 { 222 {
265 bool isbright = FALSE; 223 bool isbright = FALSE;
266 int cl; 224 int cl;
225 ccolor * ccol;
267 if (!strncmp(color, "bright", 6)) { 226 if (!strncmp(color, "bright", 6)) {
268 isbright = TRUE; 227 isbright = TRUE;
269 color += 6; 228 color += 6;
270 } 229 }
271 cl = color_to_color_fg(FindColorInternal(color)); 230 cl = FindColor(color);
272 if (cl < 0) 231 if (cl < 0)
273 return cl; 232 return NULL;
274 if (isbright) 233 ccol = g_new0(ccolor, 1);
275 cl += COLOR_BLACK_BOLD_FG - COLOR_BLACK_FG; 234 ccol->color_attrib = isbright ? A_BOLD : A_NORMAL;
276 return cl; 235 ccol->color_pair = cl + COLOR_max; //user colors come after the internal ones
236 return ccol;
277 } 237 }
278 238
279 static void ensure_string_htable(GHashTable **table, 239 static void ensure_string_htable(GHashTable **table,
280 GDestroyNotify value_destroy_func) 240 GDestroyNotify value_destroy_func)
281 { 241 {
347 } 307 }
348 g_free(snick);//They are not saved in the hash 308 g_free(snick);//They are not saved in the hash
349 g_free(mnick); 309 g_free(mnick);
350 need_update = TRUE; 310 need_update = TRUE;
351 } else { 311 } else {
352 int cl = get_user_color(color); 312 ccolor * cl = get_user_color(color);
353 if (cl < 0) { 313 if (!cl) {
354 scr_LogPrint(LPRINT_NORMAL, "No such color name"); 314 scr_LogPrint(LPRINT_NORMAL, "No such color name");
355 g_free(snick); 315 g_free(snick);
356 g_free(mnick); 316 g_free(mnick);
357 } else { 317 } else {
358 nickcolor *nc = g_new(nickcolor, 1); 318 nickcolor *nc = g_new(nickcolor, 1);
374 334
375 static void free_rostercolrule(rostercolor *col) 335 static void free_rostercolrule(rostercolor *col)
376 { 336 {
377 g_free(col->status); 337 g_free(col->status);
378 g_free(col->wildcard); 338 g_free(col->wildcard);
339 g_free(col->color);
379 g_pattern_spec_free(col->compiled); 340 g_pattern_spec_free(col->compiled);
380 g_free(col); 341 g_free(col);
381 } 342 }
382 343
383 // Removes all roster coloring rules 344 // Removes all roster coloring rules
418 } else { 379 } else {
419 scr_LogPrint(LPRINT_NORMAL, "No such color rule, nothing removed"); 380 scr_LogPrint(LPRINT_NORMAL, "No such color rule, nothing removed");
420 return FALSE; 381 return FALSE;
421 } 382 }
422 } else { 383 } else {
423 int cl = get_user_color(color); 384 ccolor * cl = get_user_color(color);
424 if (cl < 0 ) { 385 if (!cl) {
425 scr_LogPrint(LPRINT_NORMAL, "No such color name"); 386 scr_LogPrint(LPRINT_NORMAL, "No such color name");
426 return FALSE; 387 return FALSE;
427 } 388 }
428 if (found) { 389 if (found) {
429 rostercolor *rc = found->data; 390 rostercolor *rc = found->data;
391 g_free(rc->color);
430 rc->color = cl; 392 rc->color = cl;
431 } else { 393 } else {
432 rostercolor *rc = g_new(rostercolor, 1); 394 rostercolor *rc = g_new(rostercolor, 1);
433 rc->status = g_strdup(status); 395 rc->status = g_strdup(status);
434 rc->wildcard = g_strdup(wildcard); 396 rc->wildcard = g_strdup(wildcard);
531 init_pair(i+1, ((color) ? FindColor(color) : COLOR_WHITE), 493 init_pair(i+1, ((color) ? FindColor(color) : COLOR_WHITE),
532 FindColor(background)); 494 FindColor(background));
533 break; 495 break;
534 } 496 }
535 } 497 }
536 for (i = COLOR_BLACK_FG; i < COLOR_max; i++) { 498 for (i = COLOR_max; i < (COLOR_max + COLORS); i++)
537 init_pair(i, color_fg_to_color(i), FindColor(background)); 499 init_pair(i, i-COLOR_max, FindColor(background));
538 if (i >= COLOR_BLACK_BOLD_FG) 500
539 COLOR_ATTRIB[i] = A_BOLD;
540 }
541 if (!nickcols) { 501 if (!nickcols) {
542 char *ncolors = g_strdup(settings_opt_get("nick_colors")); 502 char *ncolors = g_strdup(settings_opt_get("nick_colors"));
543 if (ncolors) { 503 if (ncolors) {
544 char *ncolor_start, *ncolor_end; 504 char *ncolor_start, *ncolor_end;
545 ncolor_start = ncolor_end = ncolors; 505 ncolor_start = ncolor_end = ncolors;
550 while (ncolors < ncolor_end && *ncolors) { 510 while (ncolors < ncolor_end && *ncolors) {
551 if ((*ncolors == ' ') || (*ncolors == '\t')) { 511 if ((*ncolors == ' ') || (*ncolors == '\t')) {
552 ncolors++; 512 ncolors++;
553 } else { 513 } else {
554 char *end = ncolors; 514 char *end = ncolors;
555 int cl; 515 ccolor * cl;
556 while (*end && (*end != ' ') && (*end != '\t')) 516 while (*end && (*end != ' ') && (*end != '\t'))
557 end++; 517 end++;
558 *end = '\0'; 518 *end = '\0';
559 cl = get_user_color(ncolors); 519 cl = get_user_color(ncolors);
560 if (cl < 0) { 520 if (!cl) {
561 scr_LogPrint(LPRINT_NORMAL, "Unknown color %s", ncolors); 521 scr_LogPrint(LPRINT_NORMAL, "Unknown color %s", ncolors);
562 } else { 522 } else {
563 nickcols = g_realloc(nickcols, (++nickcolcount) * sizeof *nickcols); 523 nickcols = g_realloc(nickcols, (++nickcolcount) * sizeof *nickcols);
564 nickcols[nickcolcount-1] = cl; 524 nickcols[nickcolcount-1] = cl;
565 } 525 }
568 } 528 }
569 g_free(ncolor_start); 529 g_free(ncolor_start);
570 } 530 }
571 if (!nickcols) {//Fallback to have something 531 if (!nickcols) {//Fallback to have something
572 nickcolcount = 1; 532 nickcolcount = 1;
573 nickcols = g_new(int, 1); 533 nickcols = g_new(ccolor*, 1);
574 *nickcols = COLOR_GENERAL; 534 *nickcols = g_new(ccolor, 1);
535 (*nickcols)->color_pair = COLOR_GENERAL;
536 (*nickcols)->color_attrib = A_NORMAL;
575 } 537 }
576 } 538 }
577 } 539 }
578 540
579 static void init_keycodes(void) 541 static void init_keycodes(void)
1161 if (nickcolors) 1123 if (nickcolors)
1162 actual = g_hash_table_lookup(nickcolors, line->text); 1124 actual = g_hash_table_lookup(nickcolors, line->text);
1163 if (actual && ((type == MC_ALL) || (actual->manual)) 1125 if (actual && ((type == MC_ALL) || (actual->manual))
1164 && (line->flags & HBB_PREFIX_IN) && 1126 && (line->flags & HBB_PREFIX_IN) &&
1165 (!(line->flags & HBB_PREFIX_HLIGHT_OUT))) 1127 (!(line->flags & HBB_PREFIX_HLIGHT_OUT)))
1166 wattrset(win_entry->win, get_color(actual->color)); 1128 wattrset(win_entry->win, compose_color(actual->color));
1167 wprintw(win_entry->win, "%s", line->text); 1129 wprintw(win_entry->win, "%s", line->text);
1168 // Return the char 1130 // Return the char
1169 line->text[line->mucnicklen] = tmp; 1131 line->text[line->mucnicklen] = tmp;
1170 // Return the color back 1132 // Return the color back
1171 wattrset(win_entry->win, get_color(color)); 1133 wattrset(win_entry->win, get_color(color));
1899 const char *jid = buddy_getjid(BUDDATA(buddy)); 1861 const char *jid = buddy_getjid(BUDDATA(buddy));
1900 for (head = rostercolrules; head; head = g_slist_next(head)) { 1862 for (head = rostercolrules; head; head = g_slist_next(head)) {
1901 rostercolor *rc = head->data; 1863 rostercolor *rc = head->data;
1902 if (g_pattern_match_string(rc->compiled, jid) && 1864 if (g_pattern_match_string(rc->compiled, jid) &&
1903 (!strcmp("*", rc->status) || strchr(rc->status, status))) { 1865 (!strcmp("*", rc->status) || strchr(rc->status, status))) {
1904 color = get_color(rc->color); 1866 color = compose_color(rc->color);
1905 break; 1867 break;
1906 } 1868 }
1907 } 1869 }
1908 } 1870 }
1909 wattrset(rosterWnd, color); 1871 wattrset(rosterWnd, color);