Fixed behavior after pressing PageDown at end of file
[midnight-commander.git] / lib / tty / color.c
blob21e958344ee4e881e6a6a84304caac3662bb541f
1 /* Color setup.
2 Interface functions.
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2008, 2009 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2009.
9 Slava Zanko <slavazanko@gmail.com>, 2009.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 /** \file color.c
26 * \brief Source: color setup
29 #include <config.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h> /* size_t */
36 #include "lib/global.h"
38 #include "tty.h"
39 #include "color.h"
41 #include "color-internal.h"
43 /*** global variables ****************************************************************************/
45 char *command_line_colors = NULL;
47 static char *tty_color_defaults__fg = NULL;
48 static char *tty_color_defaults__bg = NULL;
50 /* Set if we are actually using colors */
51 gboolean use_colors = FALSE;
53 /*** file scope macro definitions ****************************************************************/
55 /*** file scope type declarations ****************************************************************/
57 /*** file scope variables ************************************************************************/
59 static GHashTable *mc_tty_color__hashtable = NULL;
61 /*** file scope functions ************************************************************************/
62 /* --------------------------------------------------------------------------------------------- */
64 static inline void
65 color_hash_destroy_key (gpointer data)
67 g_free (data);
70 /* --------------------------------------------------------------------------------------------- */
72 static void
73 color_hash_destroy_value (gpointer data)
75 tty_color_pair_t *mc_color_pair = (tty_color_pair_t *) data;
76 g_free (mc_color_pair);
79 /* --------------------------------------------------------------------------------------------- */
81 static gboolean
82 tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
84 gboolean is_temp_color;
85 tty_color_pair_t *mc_color_pair;
86 (void) key;
88 is_temp_color = user_data != NULL;
89 mc_color_pair = (tty_color_pair_t *) value;
90 return (mc_color_pair->is_temp == is_temp_color);
93 /* --------------------------------------------------------------------------------------------- */
95 static void
96 tty_color_free_all (gboolean is_temp_color)
98 g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
99 (is_temp_color) ? (gpointer) 1 : NULL);
102 /* --------------------------------------------------------------------------------------------- */
104 static gboolean
105 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
107 size_t cp;
108 tty_color_pair_t *mc_color_pair;
109 (void) key;
111 cp = (size_t) user_data;
112 mc_color_pair = (tty_color_pair_t *) value;
114 if (cp == mc_color_pair->pair_index)
115 return TRUE;
117 return FALSE;
120 /* --------------------------------------------------------------------------------------------- */
122 static int
123 tty_color_get_next__color_pair_number ()
125 size_t cp_count = g_hash_table_size (mc_tty_color__hashtable);
126 size_t cp = 0;
128 for (cp = 0; cp < cp_count; cp++)
130 if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb, (gpointer) cp) ==
131 NULL)
132 return cp;
134 return cp;
137 /* --------------------------------------------------------------------------------------------- */
138 /*** public functions ****************************************************************************/
139 /* --------------------------------------------------------------------------------------------- */
141 void
142 tty_init_colors (gboolean disable, gboolean force)
144 tty_color_init_lib (disable, force);
145 mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal,
146 color_hash_destroy_key,
147 color_hash_destroy_value);
150 /* --------------------------------------------------------------------------------------------- */
152 void
153 tty_colors_done (void)
155 tty_color_deinit_lib ();
156 g_free (tty_color_defaults__fg);
157 g_free (tty_color_defaults__bg);
159 g_hash_table_destroy (mc_tty_color__hashtable);
162 /* --------------------------------------------------------------------------------------------- */
164 gboolean
165 tty_use_colors (void)
167 return use_colors;
170 /* --------------------------------------------------------------------------------------------- */
173 tty_try_alloc_color_pair2 (const char *fg, const char *bg, gboolean is_temp_color)
175 gchar *color_pair;
176 tty_color_pair_t *mc_color_pair;
177 const char *c_fg, *c_bg;
179 if (fg == NULL)
180 fg = tty_color_defaults__fg;
182 if (bg == NULL)
184 bg = tty_color_defaults__bg;
186 c_fg = tty_color_get_valid_name (fg);
187 c_bg = tty_color_get_valid_name (bg);
189 color_pair = g_strdup_printf ("%s.%s", c_fg, c_bg);
190 if (color_pair == NULL)
191 return 0;
193 mc_color_pair =
194 (tty_color_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable, (gpointer) color_pair);
196 if (mc_color_pair != NULL)
198 g_free (color_pair);
199 return mc_color_pair->pair_index;
202 mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
203 if (mc_color_pair == NULL)
205 g_free (color_pair);
206 return 0;
209 mc_color_pair->is_temp = is_temp_color;
210 mc_color_pair->cfg = c_fg;
211 mc_color_pair->cbg = c_bg;
212 mc_color_pair->ifg = tty_color_get_index_by_name (c_fg);
213 mc_color_pair->ibg = tty_color_get_index_by_name (c_bg);
214 mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
216 tty_color_try_alloc_pair_lib (mc_color_pair);
218 g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
220 return mc_color_pair->pair_index;
223 /* --------------------------------------------------------------------------------------------- */
226 tty_try_alloc_color_pair (const char *fg, const char *bg)
228 return tty_try_alloc_color_pair2 (fg, bg, TRUE);
231 /* --------------------------------------------------------------------------------------------- */
233 void
234 tty_color_free_all_tmp (void)
236 tty_color_free_all (TRUE);
239 /* --------------------------------------------------------------------------------------------- */
241 void
242 tty_color_free_all_non_tmp (void)
244 tty_color_free_all (FALSE);
247 /* --------------------------------------------------------------------------------------------- */
249 void
250 tty_color_set_defaults (const char *fgcolor, const char *bgcolor)
252 g_free (tty_color_defaults__fg);
253 g_free (tty_color_defaults__fg);
255 tty_color_defaults__fg = (fgcolor != NULL) ? g_strdup (fgcolor) : NULL;
256 tty_color_defaults__bg = (bgcolor != NULL) ? g_strdup (bgcolor) : NULL;
259 /* --------------------------------------------------------------------------------------------- */