Merge branch '2621_path_encoding_and_extfs'
[midnight-commander.git] / lib / tty / color-ncurses.c
blobc8be60d6646cd167975e61aeaa14adda07806ea7
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.
5 Written by:
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
28 #include <config.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
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 /* --------------------------------------------------------------------------------------------- */
54 static inline void
55 mc_tty_color_attr_destroy_cb (gpointer data)
57 g_free (data);
60 /* --------------------------------------------------------------------------------------------- */
62 static void
63 mc_tty_color_save_attr (int color_pair, int color_attr)
65 int *attr, *key;
67 attr = g_try_new0 (int, 1);
68 if (attr == NULL)
69 return;
71 key = g_try_new (int, 1);
72 if (key == NULL)
74 g_free (attr);
75 return;
78 *key = color_pair;
79 *attr = color_attr;
81 g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
84 /* --------------------------------------------------------------------------------------------- */
86 static int
87 color_get_attr (int color_pair)
89 int *fnd = NULL;
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 /* --------------------------------------------------------------------------------------------- */
98 static void
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);
104 else
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 /* --------------------------------------------------------------------------------------------- */
113 void
114 tty_color_init_lib (gboolean disable, gboolean force)
116 (void) force;
118 if (has_colors () && !disable)
120 use_colors = TRUE;
121 start_color ();
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 /* --------------------------------------------------------------------------------------------- */
131 void
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 /* --------------------------------------------------------------------------------------------- */
140 void
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)
148 case SPEC_A_REVERSE:
149 mc_tty_color_pair_init_special (mc_color_pair,
150 COLOR_BLACK, COLOR_WHITE,
151 COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
152 break;
153 case SPEC_A_BOLD:
154 mc_tty_color_pair_init_special (mc_color_pair,
155 COLOR_WHITE, COLOR_BLACK,
156 COLOR_WHITE, COLOR_BLACK, A_BOLD);
157 break;
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);
162 break;
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);
167 break;
170 else
172 int ifg, ibg, attr;
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)
184 ifg &= 0x07;
185 attr |= A_BOLD;
188 if (ibg >= 8 && ibg < 16)
190 ibg &= 0x07;
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 /* --------------------------------------------------------------------------------------------- */
202 void
203 tty_setcolor (int color)
205 attrset (COLOR_PAIR (color) | color_get_attr (color));
208 /* --------------------------------------------------------------------------------------------- */
210 void
211 tty_lowlevel_setcolor (int color)
213 tty_setcolor (color);
216 /* --------------------------------------------------------------------------------------------- */
218 void
219 tty_set_normal_attrs (void)
221 standend ();
224 /* --------------------------------------------------------------------------------------------- */
226 gboolean
227 tty_use_256colors (void)
229 return (COLORS == 256);
232 /* --------------------------------------------------------------------------------------------- */