]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/new_pair.h
f5153d5b947b9a76f318511fea92b37ea8348058
[ncurses.git] / ncurses / new_pair.h
1 /****************************************************************************
2  * Copyright (c) 2017 Free Software Foundation, Inc.                        *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Thomas E. Dickey                                                *
31  ****************************************************************************/
32
33 /*
34  * Common type definitions and macros for new_pair.c, lib_color.c
35  *
36  * $Id: new_pair.h,v 1.4 2017/03/31 11:32:07 tom Exp $
37  */
38
39 #ifndef NEW_PAIR_H
40 #define NEW_PAIR_H 1
41 /* *INDENT-OFF* */
42
43 #define USE_NEW_PAIR NCURSES_EXT_COLORS
44
45 #define MAX_OF_TYPE(t)   (int)(((unsigned t)(~0))>>1)
46 #define LIMIT_TYPED(n,t) (t)(((t)(n) < 0) ? MAX_OF_TYPE(t) : (n))
47
48 #define limit_COLOR(n) LIMIT_TYPED(n,NCURSES_COLOR_T)
49 #define limit_PAIRS(n) LIMIT_TYPED(n,NCURSES_PAIRS_T)
50
51 #define MAX_XCURSES_PAIR MAX_OF_TYPE(NCURSES_PAIRS_T)
52
53 #if USE_NEW_PAIR
54 #define OPTIONAL_PAIR   GCC_UNUSED
55 #define USE_NEW_PAIR NCURSES_EXT_COLORS
56 #define get_extended_pair(opts, color_pair) \
57         if ((opts) != NULL) { \
58             *(int*)(opts) = color_pair; \
59         }
60 #define set_extended_pair(opts, color_pair) \
61         if ((opts) != NULL) { \
62             color_pair = *(const int*)(opts); \
63         }
64 #else
65 #define OPTIONAL_PAIR   /* nothing */
66 #define USE_NEW_PAIR NCURSES_EXT_COLORS
67 #define get_extended_pair(opts, color_pair) /* nothing */
68 #define set_extended_pair(opts, color_pair) \
69         if ((opts) != NULL) { \
70             color_pair = -1; \
71         }
72 #endif
73
74 #ifdef NEW_PAIR_INTERNAL
75
76 typedef enum {
77     cpKEEP = -1,                /* color pair 0 */
78     cpFREE = 0,                 /* free for use */
79     cpINIT = 1,                 /* init_pair() */
80     cpAUTO = 1                  /* alloc_pair() */
81 } CPMODE;
82
83 typedef struct _color_pairs
84 {
85     int fg;
86     int bg;
87 #if USE_NEW_PAIR
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 USE_NEW_PAIR
108 extern NCURSES_EXPORT(void)     _nc_set_color_pair(SCREEN*, int, int);
109 extern NCURSES_EXPORT(void)     _nc_reset_color_pair(SCREEN*, int, colorpair_t*);
110 #else
111 #define _nc_set_color_pair(sp, pair, mode) /* nothing */
112 #define _nc_reset_color_pair(sp, pair, data) /* nothing */
113 #endif
114
115 #else
116
117 typedef struct _color_pairs colorpair_t;
118
119 #endif /* NEW_PAIR_INTERNAL */
120
121 #if NO_LEAKS
122 extern NCURSES_EXPORT(void)     _nc_new_pair_leaks(SCREEN*);
123 #endif
124
125 /* *INDENT-ON* */
126
127 #endif /* NEW_PAIR_H */