grafthistory: comment about downloading the pack index
[elinks/elinks-j605.git] / src / bfu / style.c
blobf2c566c0df245d04eae1e611912f702febb6d880
1 /* BFU display helpers. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include "elinks.h"
9 #include "bfu/style.h"
10 #include "config/options.h"
11 #include "terminal/color.h"
12 #include "terminal/draw.h"
13 #include "terminal/terminal.h"
14 #include "util/color.h"
15 #include "util/hash.h"
18 struct bfu_color_entry {
19 /* Pointers to the options tree values. */
20 color_T *background;
21 color_T *foreground;
23 /* The copy of "text" and "background" colors. */
24 struct color_pair colors;
27 static struct hash *bfu_colors = NULL;
29 struct color_pair *
30 get_bfu_color(struct terminal *term, unsigned char *stylename)
32 static enum color_mode last_color_mode;
33 struct bfu_color_entry *entry;
34 int stylenamelen;
35 struct hash_item *item;
36 enum color_mode color_mode;
38 if (!term) return NULL;
40 color_mode = get_opt_int_tree(term->spec, "colors");
42 if (!bfu_colors) {
43 /* Initialize the style hash. */
44 bfu_colors = init_hash(8, &strhash);
45 if (!bfu_colors) return NULL;
47 last_color_mode = color_mode;
49 } else if (color_mode != last_color_mode) {
50 int i;
52 /* Change mode by emptying the cache so mono/color colors
53 * aren't mixed. */
54 foreach_hash_item (item, *bfu_colors, i) {
55 mem_free_if(item->value);
56 item = item->prev;
57 del_hash_item(bfu_colors, item->next);
60 last_color_mode = color_mode;
63 stylenamelen = strlen(stylename);
64 item = get_hash_item(bfu_colors, stylename, stylenamelen);
65 entry = item ? item->value : NULL;
67 if (!entry) {
68 struct option *opt;
70 /* Construct the color entry. */
71 opt = get_opt_rec_real(config_options, color_mode
72 ? "ui.colors.color" : "ui.colors.mono");
73 if (!opt) return NULL;
75 opt = get_opt_rec_real(opt, stylename);
76 if (!opt) return NULL;
78 entry = mem_calloc(1, sizeof(*entry));
79 if (!entry) return NULL;
81 item = add_hash_item(bfu_colors, stylename, stylenamelen, entry);
82 if (!item) {
83 mem_free(entry);
84 return NULL;
87 entry->foreground = &get_opt_color_tree(opt, "text");
88 entry->background = &get_opt_color_tree(opt, "background");
91 /* Always update the color pair. */
92 entry->colors.background = *entry->background;
93 entry->colors.foreground = *entry->foreground;
95 return &entry->colors;
98 void
99 done_bfu_colors(void)
101 struct hash_item *item;
102 int i;
104 if (!bfu_colors)
105 return;
107 foreach_hash_item (item, *bfu_colors, i) {
108 mem_free_if(item->value);
111 free_hash(bfu_colors);
112 bfu_colors = NULL;