2 Color setup for NCurses screen library
4 Copyright (C) 1994-2017
5 Free Software Foundation, Inc.
8 Andrew Borodin <aborodin@vmail.ru>, 2009
9 Slava Zanko <slavazanko@gmail.com>, 2010
10 Egmont Koblinger <egmont@gmail.com>, 2010
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file color-ncurses.c
29 * \brief Source: NCUrses-specific color setup
37 #include <sys/types.h> /* size_t */
39 #include "lib/global.h"
41 #include "tty-ncurses.h"
42 #include "color.h" /* variables */
43 #include "color-internal.h"
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 /*** file scope type declarations ****************************************************************/
51 /*** file scope variables ************************************************************************/
53 static GHashTable
*mc_tty_color_color_pair_attrs
= NULL
;
55 /*** file scope functions ************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
59 mc_tty_color_attr_destroy_cb (gpointer data
)
64 /* --------------------------------------------------------------------------------------------- */
67 mc_tty_color_save_attr (int color_pair
, int color_attr
)
71 attr
= g_try_new0 (int, 1);
75 key
= g_try_new (int, 1);
85 g_hash_table_replace (mc_tty_color_color_pair_attrs
, (gpointer
) key
, (gpointer
) attr
);
88 /* --------------------------------------------------------------------------------------------- */
91 color_get_attr (int color_pair
)
95 if (mc_tty_color_color_pair_attrs
!= NULL
)
96 fnd
= (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs
, (gpointer
) & color_pair
);
97 return (fnd
!= NULL
) ? *fnd
: 0;
100 /* --------------------------------------------------------------------------------------------- */
103 mc_tty_color_pair_init_special (tty_color_pair_t
* mc_color_pair
,
104 int fg1
, int bg1
, int fg2
, int bg2
, int attr
)
106 if (has_colors () && !mc_tty_color_disable
)
107 init_pair (mc_color_pair
->pair_index
, fg1
, bg1
);
109 init_pair (mc_color_pair
->pair_index
, fg2
, bg2
);
110 mc_tty_color_save_attr (mc_color_pair
->pair_index
, attr
);
113 /* --------------------------------------------------------------------------------------------- */
114 /*** public functions ****************************************************************************/
115 /* --------------------------------------------------------------------------------------------- */
118 tty_color_init_lib (gboolean disable
, gboolean force
)
122 if (has_colors () && !disable
)
126 use_default_colors ();
129 mc_tty_color_color_pair_attrs
= g_hash_table_new_full
130 (g_int_hash
, g_int_equal
, mc_tty_color_attr_destroy_cb
, mc_tty_color_attr_destroy_cb
);
133 /* --------------------------------------------------------------------------------------------- */
136 tty_color_deinit_lib (void)
138 g_hash_table_destroy (mc_tty_color_color_pair_attrs
);
139 mc_tty_color_color_pair_attrs
= NULL
;
142 /* --------------------------------------------------------------------------------------------- */
145 tty_color_try_alloc_pair_lib (tty_color_pair_t
* mc_color_pair
)
147 if (mc_color_pair
->ifg
<= (int) SPEC_A_REVERSE
)
149 switch (mc_color_pair
->ifg
)
152 mc_tty_color_pair_init_special (mc_color_pair
,
153 COLOR_BLACK
, COLOR_WHITE
,
154 COLOR_BLACK
, COLOR_WHITE
| A_BOLD
, A_REVERSE
);
157 mc_tty_color_pair_init_special (mc_color_pair
,
158 COLOR_WHITE
, COLOR_BLACK
,
159 COLOR_WHITE
, COLOR_BLACK
, A_BOLD
);
161 case SPEC_A_BOLD_REVERSE
:
162 mc_tty_color_pair_init_special (mc_color_pair
,
163 COLOR_WHITE
, COLOR_WHITE
,
164 COLOR_WHITE
, COLOR_WHITE
, A_BOLD
| A_REVERSE
);
166 case SPEC_A_UNDERLINE
:
167 mc_tty_color_pair_init_special (mc_color_pair
,
168 COLOR_WHITE
, COLOR_BLACK
,
169 COLOR_WHITE
, COLOR_BLACK
, A_UNDERLINE
);
179 ifg
= mc_color_pair
->ifg
;
180 ibg
= mc_color_pair
->ibg
;
181 attr
= mc_color_pair
->attr
;
183 /* In legacy color mode, change bright colors into bold */
184 if (!tty_use_256colors () && !tty_use_truecolors (NULL
))
186 if (ifg
>= 8 && ifg
< 16)
192 if (ibg
>= 8 && ibg
< 16)
195 /* attr | = A_BOLD | A_REVERSE ; */
199 init_pair (mc_color_pair
->pair_index
, ifg
, ibg
);
200 mc_tty_color_save_attr (mc_color_pair
->pair_index
, attr
);
204 /* --------------------------------------------------------------------------------------------- */
207 tty_setcolor (int color
)
209 attrset (COLOR_PAIR (color
) | color_get_attr (color
));
212 /* --------------------------------------------------------------------------------------------- */
215 tty_lowlevel_setcolor (int color
)
217 tty_setcolor (color
);
220 /* --------------------------------------------------------------------------------------------- */
223 tty_set_normal_attrs (void)
228 /* --------------------------------------------------------------------------------------------- */
231 tty_use_256colors (void)
233 return (COLORS
== 256);
236 /* --------------------------------------------------------------------------------------------- */
239 tty_use_truecolors (GError
** error
)
241 /* Not yet supported in ncurses */
242 g_set_error (error
, MC_ERROR
, -1, _("True color not supported with ncurses."));
246 /* --------------------------------------------------------------------------------------------- */