2 Color setup for NCurses screen library
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2008, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
9 Andrew Borodin <aborodin@vmail.ru>, 2009
10 Slava Zanko <slavazanko@gmail.com>, 2010
11 Egmont Koblinger <egmont@gmail.com>, 2010
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file color-ncurses.c
30 * \brief Source: NCUrses-specific color setup
38 #include <sys/types.h> /* size_t */
40 #include "lib/global.h"
42 #include "tty-ncurses.h"
43 #include "color.h" /* variables */
44 #include "color-internal.h"
46 /*** global variables ****************************************************************************/
48 /*** file scope macro definitions ****************************************************************/
50 /*** file scope type declarations ****************************************************************/
52 /*** file scope variables ************************************************************************/
54 static GHashTable
*mc_tty_color_color_pair_attrs
= NULL
;
56 /*** file scope functions ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
60 mc_tty_color_attr_destroy_cb (gpointer data
)
65 /* --------------------------------------------------------------------------------------------- */
68 mc_tty_color_save_attr (int color_pair
, int color_attr
)
72 attr
= g_try_new0 (int, 1);
76 key
= g_try_new (int, 1);
86 g_hash_table_replace (mc_tty_color_color_pair_attrs
, (gpointer
) key
, (gpointer
) attr
);
89 /* --------------------------------------------------------------------------------------------- */
92 color_get_attr (int color_pair
)
96 if (mc_tty_color_color_pair_attrs
!= NULL
)
97 fnd
= (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs
, (gpointer
) & color_pair
);
98 return (fnd
!= NULL
) ? *fnd
: 0;
101 /* --------------------------------------------------------------------------------------------- */
104 mc_tty_color_pair_init_special (tty_color_pair_t
* mc_color_pair
,
105 int fg1
, int bg1
, int fg2
, int bg2
, int attr
)
107 if (has_colors () && !mc_tty_color_disable
)
108 init_pair (mc_color_pair
->pair_index
, fg1
, bg1
);
110 init_pair (mc_color_pair
->pair_index
, fg2
, bg2
);
111 mc_tty_color_save_attr (mc_color_pair
->pair_index
, attr
);
114 /* --------------------------------------------------------------------------------------------- */
115 /*** public functions ****************************************************************************/
116 /* --------------------------------------------------------------------------------------------- */
119 tty_color_init_lib (gboolean disable
, gboolean force
)
123 if (has_colors () && !disable
)
127 use_default_colors ();
130 mc_tty_color_color_pair_attrs
= g_hash_table_new_full
131 (g_int_hash
, g_int_equal
, mc_tty_color_attr_destroy_cb
, mc_tty_color_attr_destroy_cb
);
134 /* --------------------------------------------------------------------------------------------- */
137 tty_color_deinit_lib (void)
139 g_hash_table_destroy (mc_tty_color_color_pair_attrs
);
140 mc_tty_color_color_pair_attrs
= NULL
;
143 /* --------------------------------------------------------------------------------------------- */
146 tty_color_try_alloc_pair_lib (tty_color_pair_t
* mc_color_pair
)
149 if (mc_color_pair
->ifg
<= (int) SPEC_A_REVERSE
)
151 switch (mc_color_pair
->ifg
)
154 mc_tty_color_pair_init_special (mc_color_pair
,
155 COLOR_BLACK
, COLOR_WHITE
,
156 COLOR_BLACK
, COLOR_WHITE
| A_BOLD
, A_REVERSE
);
159 mc_tty_color_pair_init_special (mc_color_pair
,
160 COLOR_WHITE
, COLOR_BLACK
,
161 COLOR_WHITE
, COLOR_BLACK
, A_BOLD
);
163 case SPEC_A_BOLD_REVERSE
:
164 mc_tty_color_pair_init_special (mc_color_pair
,
165 COLOR_WHITE
, COLOR_WHITE
,
166 COLOR_WHITE
, COLOR_WHITE
, A_BOLD
| A_REVERSE
);
168 case SPEC_A_UNDERLINE
:
169 mc_tty_color_pair_init_special (mc_color_pair
,
170 COLOR_WHITE
, COLOR_BLACK
,
171 COLOR_WHITE
, COLOR_BLACK
, A_UNDERLINE
);
179 ifg
= mc_color_pair
->ifg
;
180 ibg
= mc_color_pair
->ibg
;
181 attr
= mc_color_pair
->attr
;
184 /* In non-256 color mode, change bright colors into bold */
185 if (!tty_use_256colors ())
187 if (ifg
>= 8 && ifg
< 16)
193 if (ibg
>= 8 && ibg
< 16)
196 /* attr | = A_BOLD | A_REVERSE ; */
200 init_pair (mc_color_pair
->pair_index
, ifg
, ibg
);
201 mc_tty_color_save_attr (mc_color_pair
->pair_index
, attr
);
205 /* --------------------------------------------------------------------------------------------- */
208 tty_setcolor (int color
)
210 attrset (COLOR_PAIR (color
) | color_get_attr (color
));
213 /* --------------------------------------------------------------------------------------------- */
216 tty_lowlevel_setcolor (int color
)
218 tty_setcolor (color
);
221 /* --------------------------------------------------------------------------------------------- */
224 tty_set_normal_attrs (void)
229 /* --------------------------------------------------------------------------------------------- */
232 tty_use_256colors (void)
234 return (COLORS
== 256);
237 /* --------------------------------------------------------------------------------------------- */