Updated doc/NEWS file
[midnight-commander.git] / lib / tty / color-ncurses.c
bloba8d1aa5300993272d41195f707d11710602db35b
1 /*
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.
8 Written by:
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
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
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 /* --------------------------------------------------------------------------------------------- */
59 static inline void
60 mc_tty_color_attr_destroy_cb (gpointer data)
62 g_free (data);
65 /* --------------------------------------------------------------------------------------------- */
67 static void
68 mc_tty_color_save_attr (int color_pair, int color_attr)
70 int *attr, *key;
72 attr = g_try_new0 (int, 1);
73 if (attr == NULL)
74 return;
76 key = g_try_new (int, 1);
77 if (key == NULL)
79 g_free (attr);
80 return;
83 *key = color_pair;
84 *attr = color_attr;
86 g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
89 /* --------------------------------------------------------------------------------------------- */
91 static int
92 color_get_attr (int color_pair)
94 int *fnd = NULL;
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 /* --------------------------------------------------------------------------------------------- */
103 static void
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);
109 else
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 /* --------------------------------------------------------------------------------------------- */
118 void
119 tty_color_init_lib (gboolean disable, gboolean force)
121 (void) force;
123 if (has_colors () && !disable)
125 use_colors = TRUE;
126 start_color ();
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 /* --------------------------------------------------------------------------------------------- */
136 void
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 /* --------------------------------------------------------------------------------------------- */
145 void
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)
153 case SPEC_A_REVERSE:
154 mc_tty_color_pair_init_special (mc_color_pair,
155 COLOR_BLACK, COLOR_WHITE,
156 COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
157 break;
158 case SPEC_A_BOLD:
159 mc_tty_color_pair_init_special (mc_color_pair,
160 COLOR_WHITE, COLOR_BLACK,
161 COLOR_WHITE, COLOR_BLACK, A_BOLD);
162 break;
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);
167 break;
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);
172 break;
175 else
177 int ifg, ibg, attr;
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)
189 ifg &= 0x07;
190 attr |= A_BOLD;
193 if (ibg >= 8 && ibg < 16)
195 ibg &= 0x07;
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 /* --------------------------------------------------------------------------------------------- */
207 void
208 tty_setcolor (int color)
210 attrset (COLOR_PAIR (color) | color_get_attr (color));
213 /* --------------------------------------------------------------------------------------------- */
215 void
216 tty_lowlevel_setcolor (int color)
218 tty_setcolor (color);
221 /* --------------------------------------------------------------------------------------------- */
223 void
224 tty_set_normal_attrs (void)
226 standend ();
229 /* --------------------------------------------------------------------------------------------- */
231 gboolean
232 tty_use_256colors (void)
234 return (COLORS == 256);
237 /* --------------------------------------------------------------------------------------------- */