X-Git-Url: http://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Fbase%2Fnew_pair.c;h=c47e61e7e8d7eeca4acadd9d7b2c78cecc4986f1;hp=8597fc4db20b53cc0bb4a1edbfe56b63634bf956;hb=bfe3845eb1a2ff02a740e917b537e939ec4e44cb;hpb=af3d0ee323cbb22d2a7596d564bf68f7307f5076 diff --git a/ncurses/base/new_pair.c b/ncurses/base/new_pair.c index 8597fc4d..c47e61e7 100644 --- a/ncurses/base/new_pair.c +++ b/ncurses/base/new_pair.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2017 Free Software Foundation, Inc. * + * Copyright (c) 2017,2018 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -48,7 +48,7 @@ #define MaxColors max_colors #endif -#if USE_NEW_PAIR +#if NCURSES_EXT_COLORS /* fix redefinition versys tic.h */ #undef entry @@ -60,9 +60,9 @@ #endif -MODULE_ID("$Id: new_pair.c,v 1.11 2017/06/17 18:43:16 tom Exp $") +MODULE_ID("$Id: new_pair.c,v 1.17 2018/12/29 21:27:21 tom Exp $") -#if USE_NEW_PAIR +#if NCURSES_EXT_COLORS #ifdef NEW_PAIR_DEBUG @@ -170,6 +170,20 @@ delink_color_pair(SCREEN *sp, int pair) } } +/* + * Discard all nodes in the fast-index. + */ +NCURSES_EXPORT(void) +_nc_free_ordered_pairs(SCREEN *sp) +{ + if (sp && sp->_ordered_pairs && sp->_pair_alloc) { + int n; + for (n = 0; n < sp->_pair_alloc; ++n) { + tdelete(&sp->_color_pairs[n], &sp->_ordered_pairs, compare_data); + } + } +} + /* * Use this call to update the fast-index when modifying an entry in the color * pair table. @@ -177,8 +191,10 @@ delink_color_pair(SCREEN *sp, int pair) NCURSES_EXPORT(void) _nc_reset_color_pair(SCREEN *sp, int pair, colorpair_t * next) { + colorpair_t *last; if (ValidPair(sp, pair)) { - colorpair_t *last = &(sp->_color_pairs[pair]); + ReservePairs(sp, pair); + last = &(sp->_color_pairs[pair]); delink_color_pair(sp, pair); if (last->mode > cpFREE && (last->fg != next->fg || last->bg != next->bg)) { @@ -216,6 +232,22 @@ _nc_set_color_pair(SCREEN *sp, int pair, int mode) } } +/* + * If we reallocate the color-pair array, we have to adjust the fast-index. + */ +NCURSES_EXPORT(void) +_nc_copy_pairs(SCREEN *sp, colorpair_t * target, colorpair_t * source, int length) +{ + int n; + for (n = 0; n < length; ++n) { + void *find = tfind(source + n, &sp->_ordered_pairs, compare_data); + if (find != 0) { + tdelete(source + n, &sp->_ordered_pairs, compare_data); + tsearch(target + n, &sp->_ordered_pairs, compare_data); + } + } +} + NCURSES_EXPORT(int) NCURSES_SP_NAME(alloc_pair) (NCURSES_SP_DCLx int fg, int bg) { @@ -237,13 +269,22 @@ NCURSES_SP_NAME(alloc_pair) (NCURSES_SP_DCLx int fg, int bg) * The linear search is done to allow mixing calls to init_pair() * and alloc_pair(). The former can make gaps... */ - for (pair = hint + 1; pair < SP_PARM->_pair_limit; pair++) { + for (pair = hint + 1; pair < SP_PARM->_pair_alloc; pair++) { if (SP_PARM->_color_pairs[pair].mode == cpFREE) { T(("found gap %d", pair)); found = TRUE; break; } } + if (!found && (SP_PARM->_pair_alloc < SP_PARM->_pair_limit)) { + pair = SP_PARM->_pair_alloc; + ReservePairs(SP_PARM, pair); + if (SP_PARM->_color_pairs == 0) { + pair = -1; + } else { + found = TRUE; + } + } if (!found) { for (pair = 1; pair <= hint; pair++) { if (SP_PARM->_color_pairs[pair].mode == cpFREE) { @@ -285,7 +326,7 @@ NCURSES_SP_NAME(free_pair) (NCURSES_SP_DCLx int pair) { int result = ERR; T((T_CALLED("free_pair(%d)"), pair)); - if (ValidPair(SP_PARM, pair)) { + if (ValidPair(SP_PARM, pair) && pair < SP_PARM->_pair_alloc) { colorpair_t *cp = &(SP_PARM->_color_pairs[pair]); if (pair != 0) { _nc_change_pair(SP_PARM, pair); @@ -337,4 +378,4 @@ void _nc_new_pair(void) { } -#endif /* USE_NEW_PAIR */ +#endif /* NCURSES_EXT_COLORS */