1 /* Color setup for NCurses screen library
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2008, 2009 Free Software Foundation, Inc.
6 Andrew Borodin <aborodin@vmail.ru>, 2009.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 /** \file color-ncurses.c
23 * \brief Source: NCUrses-specific color setup
31 #include <sys/types.h> /* size_t */
33 #include "lib/global.h"
35 #include "tty-ncurses.h"
36 #include "color.h" /* variables */
37 #include "color-internal.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 /*** file scope type declarations ****************************************************************/
45 /*** file scope variables ************************************************************************/
47 static GHashTable
*mc_tty_color_color_pair_attrs
= NULL
;
49 /*** file scope functions ************************************************************************/
50 /* --------------------------------------------------------------------------------------------- */
53 mc_tty_color_attr_destroy_cb (gpointer data
)
58 /* --------------------------------------------------------------------------------------------- */
61 mc_tty_color_save_attr_lib (int color_pair
, int color_attr
)
65 attr
= g_try_new0 (int, 1);
69 key
= g_try_new (int, 1);
78 *attr
= color_attr
& (A_BOLD
| A_REVERSE
| A_UNDERLINE
);
79 g_hash_table_replace (mc_tty_color_color_pair_attrs
, (gpointer
) key
, (gpointer
) attr
);
80 return color_attr
& (~(*attr
));
83 /* --------------------------------------------------------------------------------------------- */
86 color_get_attr (int color_pair
)
90 if (mc_tty_color_color_pair_attrs
!= NULL
)
91 fnd
= (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs
, (gpointer
) & color_pair
);
92 return (fnd
!= NULL
) ? *fnd
: 0;
95 /* --------------------------------------------------------------------------------------------- */
98 mc_tty_color_pair_init_special (tty_color_pair_t
* mc_color_pair
,
99 int fg1
, int bg1
, int fg2
, int bg2
, int mask
)
101 if (has_colors () && !mc_tty_color_disable
)
102 init_pair (mc_color_pair
->pair_index
,
103 mc_tty_color_save_attr_lib (mc_color_pair
->pair_index
, fg1
| mask
), bg1
);
105 init_pair (mc_color_pair
->pair_index
,
106 mc_tty_color_save_attr_lib (mc_color_pair
->pair_index
, fg2
| mask
), bg2
);
109 /* --------------------------------------------------------------------------------------------- */
110 /*** public functions ****************************************************************************/
111 /* --------------------------------------------------------------------------------------------- */
114 tty_color_init_lib (gboolean disable
, gboolean force
)
118 if (has_colors () && !disable
) {
121 use_default_colors ();
124 mc_tty_color_color_pair_attrs
= g_hash_table_new_full
125 (g_int_hash
, g_int_equal
, mc_tty_color_attr_destroy_cb
, mc_tty_color_attr_destroy_cb
);
128 /* --------------------------------------------------------------------------------------------- */
131 tty_color_deinit_lib (void)
133 g_hash_table_destroy (mc_tty_color_color_pair_attrs
);
134 mc_tty_color_color_pair_attrs
= NULL
;
137 /* --------------------------------------------------------------------------------------------- */
140 tty_color_try_alloc_pair_lib (tty_color_pair_t
* mc_color_pair
)
142 if (mc_color_pair
->ifg
<= (int) SPEC_A_REVERSE
) {
143 switch (mc_color_pair
->ifg
) {
145 mc_tty_color_pair_init_special (mc_color_pair
,
146 COLOR_BLACK
, COLOR_WHITE
,
147 COLOR_BLACK
, COLOR_WHITE
| A_BOLD
, A_REVERSE
);
150 mc_tty_color_pair_init_special (mc_color_pair
,
151 COLOR_WHITE
, COLOR_BLACK
,
152 COLOR_WHITE
, COLOR_BLACK
, A_BOLD
);
154 case SPEC_A_BOLD_REVERSE
:
155 mc_tty_color_pair_init_special (mc_color_pair
,
156 COLOR_WHITE
, COLOR_WHITE
,
157 COLOR_WHITE
, COLOR_WHITE
, A_BOLD
| A_REVERSE
);
159 case SPEC_A_UNDERLINE
:
160 mc_tty_color_pair_init_special (mc_color_pair
,
161 COLOR_WHITE
, COLOR_BLACK
,
162 COLOR_WHITE
, COLOR_BLACK
, A_UNDERLINE
);
166 int mask_fg
= (mc_color_pair
->ifg
== -1) ? mc_color_pair
->ifg
: 0xff;
167 int mask_bg
= (mc_color_pair
->ibg
== -1) ? mc_color_pair
->ibg
: 0xff;
169 init_pair (mc_color_pair
->pair_index
,
170 mc_tty_color_save_attr_lib (mc_color_pair
->pair_index
,
171 mc_color_pair
->ifg
) & mask_fg
,
172 mc_color_pair
->ibg
& mask_bg
);
176 /* --------------------------------------------------------------------------------------------- */
179 tty_setcolor (int color
)
181 attrset (COLOR_PAIR (color
) | color_get_attr (color
));
184 /* --------------------------------------------------------------------------------------------- */
187 tty_lowlevel_setcolor (int color
)
189 attrset (COLOR_PAIR (color
) | color_get_attr (color
));
192 /* --------------------------------------------------------------------------------------------- */
195 tty_set_normal_attrs (void)
200 /* --------------------------------------------------------------------------------------------- */