1 /* Color setup for NCurses screen library
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 Andrew Borodin <aborodin@vmail.ru>, 2009
7 Slava Zanko <slavazanko@gmail.com>, 2010
8 Egmont Koblinger <egmont@gmail.com>, 2010
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
24 /** \file color-ncurses.c
25 * \brief Source: NCUrses-specific color setup
33 #include <sys/types.h> /* size_t */
35 #include "lib/global.h"
37 #include "tty-ncurses.h"
38 #include "color.h" /* variables */
39 #include "color-internal.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
49 static GHashTable
*mc_tty_color_color_pair_attrs
= NULL
;
51 /*** file scope functions ************************************************************************/
52 /* --------------------------------------------------------------------------------------------- */
55 mc_tty_color_attr_destroy_cb (gpointer data
)
60 /* --------------------------------------------------------------------------------------------- */
63 mc_tty_color_save_attr (int color_pair
, int color_attr
)
67 attr
= g_try_new0 (int, 1);
71 key
= g_try_new (int, 1);
81 g_hash_table_replace (mc_tty_color_color_pair_attrs
, (gpointer
) key
, (gpointer
) attr
);
84 /* --------------------------------------------------------------------------------------------- */
87 color_get_attr (int color_pair
)
91 if (mc_tty_color_color_pair_attrs
!= NULL
)
92 fnd
= (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs
, (gpointer
) & color_pair
);
93 return (fnd
!= NULL
) ? *fnd
: 0;
96 /* --------------------------------------------------------------------------------------------- */
99 mc_tty_color_pair_init_special (tty_color_pair_t
* mc_color_pair
,
100 int fg1
, int bg1
, int fg2
, int bg2
, int attr
)
102 if (has_colors () && !mc_tty_color_disable
)
103 init_pair (mc_color_pair
->pair_index
, fg1
, bg1
);
105 init_pair (mc_color_pair
->pair_index
, fg2
, bg2
);
106 mc_tty_color_save_attr (mc_color_pair
->pair_index
, attr
);
109 /* --------------------------------------------------------------------------------------------- */
110 /*** public functions ****************************************************************************/
111 /* --------------------------------------------------------------------------------------------- */
114 tty_color_init_lib (gboolean disable
, gboolean force
)
118 if (has_colors () && !disable
)
122 use_default_colors ();
125 mc_tty_color_color_pair_attrs
= g_hash_table_new_full
126 (g_int_hash
, g_int_equal
, mc_tty_color_attr_destroy_cb
, mc_tty_color_attr_destroy_cb
);
129 /* --------------------------------------------------------------------------------------------- */
132 tty_color_deinit_lib (void)
134 g_hash_table_destroy (mc_tty_color_color_pair_attrs
);
135 mc_tty_color_color_pair_attrs
= NULL
;
138 /* --------------------------------------------------------------------------------------------- */
141 tty_color_try_alloc_pair_lib (tty_color_pair_t
* mc_color_pair
)
144 if (mc_color_pair
->ifg
<= (int) SPEC_A_REVERSE
)
146 switch (mc_color_pair
->ifg
)
149 mc_tty_color_pair_init_special (mc_color_pair
,
150 COLOR_BLACK
, COLOR_WHITE
,
151 COLOR_BLACK
, COLOR_WHITE
| A_BOLD
, A_REVERSE
);
154 mc_tty_color_pair_init_special (mc_color_pair
,
155 COLOR_WHITE
, COLOR_BLACK
,
156 COLOR_WHITE
, COLOR_BLACK
, A_BOLD
);
158 case SPEC_A_BOLD_REVERSE
:
159 mc_tty_color_pair_init_special (mc_color_pair
,
160 COLOR_WHITE
, COLOR_WHITE
,
161 COLOR_WHITE
, COLOR_WHITE
, A_BOLD
| A_REVERSE
);
163 case SPEC_A_UNDERLINE
:
164 mc_tty_color_pair_init_special (mc_color_pair
,
165 COLOR_WHITE
, COLOR_BLACK
,
166 COLOR_WHITE
, COLOR_BLACK
, A_UNDERLINE
);
174 ifg
= mc_color_pair
->ifg
;
175 ibg
= mc_color_pair
->ibg
;
176 attr
= mc_color_pair
->attr
;
179 /* In non-256 color mode, change bright colors into bold */
180 if (!tty_use_256colors ())
182 if (ifg
>= 8 && ifg
< 16)
188 if (ibg
>= 8 && ibg
< 16)
191 /* attr | = A_BOLD | A_REVERSE ; */
195 init_pair (mc_color_pair
->pair_index
, ifg
, ibg
);
196 mc_tty_color_save_attr (mc_color_pair
->pair_index
, attr
);
200 /* --------------------------------------------------------------------------------------------- */
203 tty_setcolor (int color
)
205 attrset (COLOR_PAIR (color
) | color_get_attr (color
));
208 /* --------------------------------------------------------------------------------------------- */
211 tty_lowlevel_setcolor (int color
)
213 tty_setcolor (color
);
216 /* --------------------------------------------------------------------------------------------- */
219 tty_set_normal_attrs (void)
224 /* --------------------------------------------------------------------------------------------- */
227 tty_use_256colors (void)
229 return (COLORS
== 256);
232 /* --------------------------------------------------------------------------------------------- */