xref: /freebsd/contrib/ncurses/ncurses/new_pair.h (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 /****************************************************************************
2  * Copyright 2018,2020 Thomas E. Dickey                                     *
3  * Copyright 2017 Free Software Foundation, Inc.                            *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Thomas E. Dickey                                                *
32  ****************************************************************************/
33 
34 /*
35  * Common type definitions and macros for new_pair.c, lib_color.c
36  *
37  * $Id: new_pair.h,v 1.11 2020/04/11 16:43:47 tom Exp $
38  */
39 
40 #ifndef NEW_PAIR_H
41 #define NEW_PAIR_H 1
42 /* *INDENT-OFF* */
43 
44 #define LIMIT_TYPED(n,t) \
45 	(t)(((n) > MAX_OF_TYPE(t)) \
46 	    ? MAX_OF_TYPE(t) \
47 	    : ((n) < -MAX_OF_TYPE(t)) \
48 	       ? -MAX_OF_TYPE(t) \
49 	       : (n))
50 
51 #define limit_COLOR(n) LIMIT_TYPED(n,NCURSES_COLOR_T)
52 #define limit_PAIRS(n) LIMIT_TYPED(n,NCURSES_PAIRS_T)
53 
54 #define MAX_XCURSES_PAIR MAX_OF_TYPE(NCURSES_PAIRS_T)
55 
56 #if NCURSES_EXT_COLORS
57 #define OPTIONAL_PAIR	GCC_UNUSED
58 #define get_extended_pair(opts, color_pair) \
59 	if ((opts) != NULL) { \
60 	    *(int*)(opts) = color_pair; \
61 	}
62 #define set_extended_pair(opts, color_pair) \
63 	if ((opts) != NULL) { \
64 	    color_pair = *(const int*)(opts); \
65 	}
66 #else
67 #define OPTIONAL_PAIR	/* nothing */
68 #define get_extended_pair(opts, color_pair) /* nothing */
69 #define set_extended_pair(opts, color_pair) \
70 	if ((opts) != NULL) { \
71 	    color_pair = -1; \
72 	}
73 #endif
74 
75 #ifdef NEW_PAIR_INTERNAL
76 
77 typedef enum {
78     cpKEEP = -1,		/* color pair 0 */
79     cpFREE = 0,			/* free for use */
80     cpINIT = 1			/* initialized */
81 } CPMODE;
82 
83 typedef struct _color_pairs
84 {
85     int fg;
86     int bg;
87 #if NCURSES_EXT_COLORS
88     int mode;			/* tells if the entry is allocated or free */
89     int prev;			/* index of previous item */
90     int next;			/* index of next item */
91 #endif
92 }
93 colorpair_t;
94 
95 #define MakeColorPair(target,f,b) target.fg = f, target.bg = b
96 #define isSamePair(a,b)		((a).fg == (b).fg && (a).bg == (b).bg)
97 #define FORE_OF(c)		(c).fg
98 #define BACK_OF(c)		(c).bg
99 
100 /*
101  * Ensure that we use color pairs only when colors have been started, and also
102  * that the index is within the limits of the table which we allocated.
103  */
104 #define ValidPair(sp,pair) \
105     ((sp != 0) && (pair >= 0) && (pair < sp->_pair_limit) && sp->_coloron)
106 
107 #if NCURSES_EXT_COLORS
108 extern NCURSES_EXPORT(void)     _nc_copy_pairs(SCREEN*, colorpair_t*, colorpair_t*, int);
109 extern NCURSES_EXPORT(void)     _nc_free_ordered_pairs(SCREEN*);
110 extern NCURSES_EXPORT(void)     _nc_reset_color_pair(SCREEN*, int, colorpair_t*);
111 extern NCURSES_EXPORT(void)     _nc_set_color_pair(SCREEN*, int, int);
112 #else
113 #define _nc_free_ordered_pairs(sp) /* nothing */
114 #define _nc_reset_color_pair(sp, pair, data) /* nothing */
115 #define _nc_set_color_pair(sp, pair, mode) /* nothing */
116 #endif
117 
118 #else
119 
120 typedef struct _color_pairs colorpair_t;
121 
122 #endif /* NEW_PAIR_INTERNAL */
123 
124 #if NO_LEAKS
125 extern NCURSES_EXPORT(void)     _nc_new_pair_leaks(SCREEN*);
126 #endif
127 
128 /* *INDENT-ON* */
129 
130 #endif /* NEW_PAIR_H */
131