summaryrefslogtreecommitdiff
path: root/ncurses/new_pair.h
blob: 811abc11442b3395ee9928f1eb902fe6c2801ee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/****************************************************************************
 * Copyright 2018,2020 Thomas E. Dickey                                     *
 * Copyright 2017 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            *
 * "Software"), to deal in the Software without restriction, including      *
 * without limitation the rights to use, copy, modify, merge, publish,      *
 * distribute, distribute with modifications, sublicense, and/or sell       *
 * copies of the Software, and to permit persons to whom the Software is    *
 * furnished to do so, subject to the following conditions:                 *
 *                                                                          *
 * The above copyright notice and this permission notice shall be included  *
 * in all copies or substantial portions of the Software.                   *
 *                                                                          *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
 *                                                                          *
 * Except as contained in this notice, the name(s) of the above copyright   *
 * holders shall not be used in advertising or otherwise to promote the     *
 * sale, use or other dealings in this Software without prior written       *
 * authorization.                                                           *
 ****************************************************************************/

/****************************************************************************
 *  Author: Thomas E. Dickey                                                *
 ****************************************************************************/

/*
 * Common type definitions and macros for new_pair.c, lib_color.c
 *
 * $Id: new_pair.h,v 1.10 2020/02/02 23:34:34 tom Exp $
 */

#ifndef NEW_PAIR_H
#define NEW_PAIR_H 1
/* *INDENT-OFF* */

#define LIMIT_TYPED(n,t) \
	(t)(((n) > MAX_OF_TYPE(t)) \
	    ? MAX_OF_TYPE(t) \
	    : ((n) < -MAX_OF_TYPE(t)) \
	       ? -MAX_OF_TYPE(t) \
	       : (n))

#define limit_COLOR(n) LIMIT_TYPED(n,NCURSES_COLOR_T)
#define limit_PAIRS(n) LIMIT_TYPED(n,NCURSES_PAIRS_T)

#define MAX_XCURSES_PAIR MAX_OF_TYPE(NCURSES_PAIRS_T)

#if NCURSES_EXT_COLORS
#define OPTIONAL_PAIR	GCC_UNUSED
#define get_extended_pair(opts, color_pair) \
	if ((opts) != NULL) { \
	    *(int*)(opts) = color_pair; \
	}
#define set_extended_pair(opts, color_pair) \
	if ((opts) != NULL) { \
	    color_pair = *(const int*)(opts); \
	}
#else
#define OPTIONAL_PAIR	/* nothing */
#define get_extended_pair(opts, color_pair) /* nothing */
#define set_extended_pair(opts, color_pair) \
	if ((opts) != NULL) { \
	    color_pair = -1; \
	}
#endif

#ifdef NEW_PAIR_INTERNAL

typedef enum {
    cpKEEP = -1,		/* color pair 0 */
    cpFREE = 0,			/* free for use */
    cpINIT = 1,			/* init_pair() */
    cpAUTO = 1			/* alloc_pair() */
} CPMODE;

typedef struct _color_pairs
{
    int fg;
    int bg;
#if NCURSES_EXT_COLORS
    int mode;			/* tells if the entry is allocated or free */
    int prev;			/* index of previous item */
    int next;			/* index of next item */
#endif
}
colorpair_t;

#define MakeColorPair(target,f,b) target.fg = f, target.bg = b
#define isSamePair(a,b)		((a).fg == (b).fg && (a).bg == (b).bg)
#define FORE_OF(c)		(c).fg
#define BACK_OF(c)		(c).bg

/*
 * Ensure that we use color pairs only when colors have been started, and also
 * that the index is within the limits of the table which we allocated.
 */
#define ValidPair(sp,pair) \
    ((sp != 0) && (pair >= 0) && (pair < sp->_pair_limit) && sp->_coloron)

#if NCURSES_EXT_COLORS
extern NCURSES_EXPORT(void)     _nc_copy_pairs(SCREEN*, colorpair_t*, colorpair_t*, int);
extern NCURSES_EXPORT(void)     _nc_free_ordered_pairs(SCREEN*);
extern NCURSES_EXPORT(void)     _nc_reset_color_pair(SCREEN*, int, colorpair_t*);
extern NCURSES_EXPORT(void)     _nc_set_color_pair(SCREEN*, int, int);
#else
#define _nc_free_ordered_pairs(sp) /* nothing */
#define _nc_reset_color_pair(sp, pair, data) /* nothing */
#define _nc_set_color_pair(sp, pair, mode) /* nothing */
#endif

#else

typedef struct _color_pairs colorpair_t;

#endif /* NEW_PAIR_INTERNAL */

#if NO_LEAKS
extern NCURSES_EXPORT(void)     _nc_new_pair_leaks(SCREEN*);
#endif

/* *INDENT-ON* */

#endif /* NEW_PAIR_H */