xref: /freebsd/contrib/bsddialog/lib/theme.c (revision 848ee2a3a8b47c9905fc51fefcf60eb371edbb98)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Alfonso Sabato Siciliano
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifdef PORTNCURSES
29 #include <ncurses/curses.h>
30 #else
31 #include <curses.h>
32 #endif
33 
34 #include "bsddialog.h"
35 #include "lib_util.h"
36 #include "bsddialog_theme.h"
37 
38 #define GET_COLOR(bg, fg) (COLOR_PAIR(bg * 8 + fg +1))
39 
40 struct bsddialog_theme t;
41 
42 static struct bsddialog_theme bsddialogtheme = {
43 #define bgwidget  COLOR_WHITE
44 #define bgcurr    COLOR_YELLOW
45 	.shadowcolor     = GET_COLOR(COLOR_BLACK, COLOR_BLACK),
46 	.shadowrows      = 1,
47 	.shadowcols      = 2,
48 
49 	.backgroundcolor = GET_COLOR(COLOR_BLACK, COLOR_CYAN),
50 	.surroundtitle   = true,
51 	.titlecolor      = GET_COLOR(COLOR_YELLOW, bgwidget),
52 	.lineraisecolor  = GET_COLOR(COLOR_BLACK,  bgwidget),
53 	.linelowercolor  = GET_COLOR(COLOR_BLACK,  bgwidget),
54 	.widgetcolor     = GET_COLOR(COLOR_BLACK,  bgwidget),
55 
56 	.texthmargin     = 1,
57 
58 	.curritemcolor   = GET_COLOR(COLOR_WHITE,  bgcurr),
59 	.itemcolor       = GET_COLOR(COLOR_BLACK,  bgwidget),
60 	.currtagcolor    = GET_COLOR(COLOR_BLACK,  bgcurr),
61 	.tagcolor        = GET_COLOR(COLOR_YELLOW, bgwidget),
62 	.namesepcolor    = GET_COLOR(COLOR_YELLOW, bgwidget),
63 	.descsepcolor    = GET_COLOR(COLOR_YELLOW, bgwidget),
64 
65 	.currfieldcolor  = GET_COLOR(COLOR_WHITE,  COLOR_BLUE),
66 	.fieldcolor      = GET_COLOR(COLOR_WHITE,  COLOR_CYAN),
67 	.fieldreadonlycolor = GET_COLOR(COLOR_CYAN,COLOR_WHITE),
68 
69 	.currbarcolor    = GET_COLOR(COLOR_WHITE, COLOR_BLUE),
70 	.barcolor        = GET_COLOR(COLOR_BLUE,  COLOR_WHITE),
71 
72 	.buttonspace     = 3,
73 	.buttleftch      = '[',
74 	.buttrightchar   = ']',
75 	.currbuttdelimcolor = GET_COLOR(COLOR_WHITE,  bgcurr),
76 	.buttdelimcolor     = GET_COLOR(COLOR_BLACK,  bgwidget),
77 	.currbuttoncolor    = GET_COLOR(COLOR_WHITE,  bgcurr)    | A_UNDERLINE,
78 	.buttoncolor        = GET_COLOR(COLOR_BLACK,  bgwidget)  | A_UNDERLINE,
79 	.currshortkeycolor  = GET_COLOR(COLOR_BLACK,  bgcurr)    | A_UNDERLINE,
80 	.shortkeycolor      = GET_COLOR(COLOR_YELLOW, bgwidget)  | A_UNDERLINE,
81 
82 	.bottomtitlecolor= GET_COLOR(COLOR_BLACK, bgwidget)
83 };
84 
85 static struct bsddialog_theme blackwhite = {
86 #define bk  COLOR_BLACK
87 #define fg  COLOR_WHITE
88 	.shadowcolor     = GET_COLOR(COLOR_BLACK, COLOR_BLACK),
89 	.shadowrows      = 1,
90 	.shadowcols      = 2,
91 
92 	.backgroundcolor = GET_COLOR(fg, bk),
93 	.surroundtitle   = true,
94 	.titlecolor      = GET_COLOR(fg, bk),
95 	.lineraisecolor  = GET_COLOR(fg, bk),
96 	.linelowercolor  = GET_COLOR(fg, bk),
97 	.widgetcolor     = GET_COLOR(fg, bk),
98 
99 	.texthmargin     = 1,
100 
101 	.curritemcolor   = GET_COLOR(fg, bk) | A_REVERSE,
102 	.itemcolor       = GET_COLOR(fg, bk),
103 	.currtagcolor    = GET_COLOR(fg, bk) | A_REVERSE,
104 	.tagcolor        = GET_COLOR(fg, bk),
105 	.namesepcolor    = GET_COLOR(fg, bk),
106 	.descsepcolor    = GET_COLOR(fg, bk),
107 
108 	.currfieldcolor  = GET_COLOR(fg, bk) | A_REVERSE,
109 	.fieldcolor      = GET_COLOR(fg, bk),
110 	.fieldreadonlycolor = GET_COLOR(fg, bk),
111 
112 	.currbarcolor    = GET_COLOR(fg, bk) | A_REVERSE,
113 	.barcolor        = GET_COLOR(fg, bk),
114 
115 	.buttonspace     = 3,
116 	.buttleftch      = '[',
117 	.buttrightchar   = ']',
118 	.currbuttdelimcolor = GET_COLOR(fg, bk),
119 	.buttdelimcolor     = GET_COLOR(fg, bk),
120 	.currbuttoncolor    = GET_COLOR(fg, bk) | A_UNDERLINE | A_REVERSE,
121 	.buttoncolor        = GET_COLOR(fg, bk) | A_UNDERLINE,
122 	.currshortkeycolor  = GET_COLOR(fg, bk) | A_UNDERLINE | A_REVERSE,
123 	.shortkeycolor      = GET_COLOR(fg, bk) | A_UNDERLINE,
124 
125 	.bottomtitlecolor= GET_COLOR(fg, bk)
126 };
127 
128 static struct bsddialog_theme dialogtheme = {
129 	.shadowcolor     = GET_COLOR(COLOR_BLACK, COLOR_BLACK),
130 	.shadowrows      = 1,
131 	.shadowcols      = 2,
132 
133 	.backgroundcolor = GET_COLOR(COLOR_CYAN,  COLOR_BLUE)  | A_BOLD,
134 	.surroundtitle   = false,
135 	.titlecolor      = GET_COLOR(COLOR_BLUE,  COLOR_WHITE) | A_BOLD,
136 	.lineraisecolor  = GET_COLOR(COLOR_WHITE, COLOR_WHITE) | A_BOLD,
137 	.linelowercolor  = GET_COLOR(COLOR_BLACK, COLOR_WHITE) | A_BOLD,
138 	.widgetcolor     = GET_COLOR(COLOR_BLACK, COLOR_WHITE),
139 
140 	.texthmargin     = 1,
141 
142 	.curritemcolor   = GET_COLOR(COLOR_WHITE, COLOR_BLUE)  | A_BOLD,
143 	.itemcolor       = GET_COLOR(COLOR_BLACK, COLOR_WHITE) | A_BOLD,
144 	.currtagcolor    = GET_COLOR(COLOR_YELLOW,COLOR_BLUE)  | A_BOLD,
145 	.tagcolor        = GET_COLOR(COLOR_BLUE,  COLOR_WHITE) | A_BOLD,
146 	.namesepcolor    = GET_COLOR(COLOR_RED,   COLOR_WHITE),
147 	.descsepcolor    = GET_COLOR(COLOR_RED,   COLOR_WHITE),
148 
149 	.currfieldcolor  = GET_COLOR(COLOR_WHITE,  COLOR_BLUE) | A_BOLD,
150 	.fieldcolor      = GET_COLOR(COLOR_WHITE,  COLOR_CYAN) | A_BOLD,
151 	.fieldreadonlycolor = GET_COLOR(COLOR_CYAN,COLOR_WHITE)| A_BOLD,
152 
153 	.currbarcolor    = GET_COLOR(COLOR_WHITE, COLOR_BLUE)  | A_BOLD,
154 	.barcolor        = GET_COLOR(COLOR_BLUE,  COLOR_WHITE) | A_BOLD,
155 
156 	.buttonspace     = 3,
157 	.buttleftch      = '<',
158 	.buttrightchar   = '>',
159 	.currbuttdelimcolor = GET_COLOR(COLOR_WHITE,  COLOR_BLUE)   | A_BOLD,
160 	.buttdelimcolor     = GET_COLOR(COLOR_BLACK,  COLOR_WHITE),
161 	.currbuttoncolor    = GET_COLOR(COLOR_YELLOW, COLOR_BLUE)   | A_BOLD,
162 	.buttoncolor        = GET_COLOR(COLOR_BLACK,  COLOR_WHITE),
163 	.currshortkeycolor  = GET_COLOR(COLOR_WHITE,  COLOR_BLUE)   | A_BOLD,
164 	.shortkeycolor      = GET_COLOR(COLOR_RED,    COLOR_WHITE)  | A_BOLD,
165 
166 	.bottomtitlecolor   = GET_COLOR(COLOR_BLACK,  COLOR_WHITE)  | A_BOLD
167 };
168 
169 static struct bsddialog_theme magentatheme = {
170 	.shadowcolor     = GET_COLOR(COLOR_BLACK, COLOR_BLACK),
171 	.shadowrows      = 1,
172 	.shadowcols      = 2,
173 
174 	.backgroundcolor = GET_COLOR(COLOR_WHITE,  COLOR_MAGENTA) | A_BOLD,
175 	.surroundtitle   = true,
176 	.titlecolor      = GET_COLOR(COLOR_RED,   COLOR_CYAN),
177 	.lineraisecolor  = GET_COLOR(COLOR_WHITE, COLOR_CYAN)     | A_BOLD,
178 	.linelowercolor  = GET_COLOR(COLOR_BLACK, COLOR_CYAN),
179 	.widgetcolor     = GET_COLOR(COLOR_BLACK, COLOR_CYAN),
180 
181 	.texthmargin     = 1,
182 
183 	.curritemcolor   = GET_COLOR(COLOR_WHITE, COLOR_BLUE) | A_BOLD,
184 	.itemcolor       = GET_COLOR(COLOR_BLACK, COLOR_CYAN) | A_BOLD,
185 	.currtagcolor    = GET_COLOR(COLOR_YELLOW,COLOR_BLUE) | A_BOLD,
186 	.tagcolor        = GET_COLOR(COLOR_BLUE,  COLOR_CYAN) | A_BOLD,
187 	.namesepcolor    = GET_COLOR(COLOR_RED,   COLOR_CYAN) | A_BOLD,
188 	.descsepcolor    = GET_COLOR(COLOR_BLACK, COLOR_CYAN) | A_BOLD,
189 
190 	.currfieldcolor  = GET_COLOR(COLOR_WHITE,  COLOR_BLUE) | A_BOLD,
191 	.fieldcolor      = GET_COLOR(COLOR_WHITE,  COLOR_CYAN) | A_BOLD,
192 	.fieldreadonlycolor = GET_COLOR(COLOR_CYAN,COLOR_WHITE)| A_BOLD,
193 
194 	.currbarcolor    = GET_COLOR(COLOR_WHITE, COLOR_BLUE)  | A_BOLD,
195 	.barcolor        = GET_COLOR(COLOR_BLUE,  COLOR_WHITE) | A_BOLD,
196 
197 	.buttonspace     = 3,
198 	.buttleftch      = '<',
199 	.buttrightchar   = '>',
200 	.currbuttdelimcolor = GET_COLOR(COLOR_WHITE, COLOR_RED)    | A_BOLD,
201 	.buttdelimcolor     = GET_COLOR(COLOR_BLACK, COLOR_CYAN),
202 	.currbuttoncolor    = GET_COLOR(COLOR_WHITE, COLOR_RED),
203 	.buttoncolor        = GET_COLOR(COLOR_BLACK,  COLOR_CYAN),
204 	.currshortkeycolor  = GET_COLOR(COLOR_WHITE,  COLOR_RED)   | A_BOLD,
205 	.shortkeycolor      = GET_COLOR(COLOR_BLACK,  COLOR_CYAN),
206 
207 	.bottomtitlecolor= GET_COLOR(COLOR_BLACK, COLOR_CYAN) | A_BOLD
208 };
209 
210 void bsddialog_set_theme(struct bsddialog_theme newtheme)
211 {
212 	t.shadowcolor     = newtheme.shadowcolor;
213 	t.shadowrows      = newtheme.shadowrows;
214 	t.shadowcols      = newtheme.shadowcols;
215 
216 	t.backgroundcolor = newtheme.backgroundcolor;
217 	t.surroundtitle   = newtheme.surroundtitle;
218 	t.titlecolor      = newtheme.titlecolor;
219 	t.lineraisecolor  = newtheme.lineraisecolor;
220 	t.linelowercolor  = newtheme.linelowercolor;
221 	t.widgetcolor     = newtheme.widgetcolor;
222 
223 	t.texthmargin     = newtheme.texthmargin;
224 
225 	t.curritemcolor   = newtheme.curritemcolor;
226 	t.itemcolor       = newtheme.itemcolor;
227 	t.currtagcolor    = newtheme.currtagcolor;
228 	t.tagcolor        = newtheme.tagcolor;
229 	t.namesepcolor    = newtheme.namesepcolor;
230 	t.descsepcolor    = newtheme.descsepcolor;
231 
232 	t.currfieldcolor  = newtheme.currfieldcolor;
233 	t.fieldcolor      = newtheme.fieldcolor;
234 	t.fieldreadonlycolor = newtheme.fieldreadonlycolor;
235 
236 	t.currbarcolor    = newtheme.currbarcolor;
237 	t.barcolor        = newtheme.barcolor;
238 
239 	t.buttonspace        = newtheme.buttonspace;
240 	t.buttleftch         = newtheme.buttleftch;
241 	t.buttrightchar      = newtheme.buttrightchar;
242 	t.currbuttdelimcolor = newtheme.currbuttdelimcolor;
243 	t.buttdelimcolor     = newtheme.buttdelimcolor;
244 	t.currbuttoncolor    = newtheme.currbuttoncolor;
245 	t.buttoncolor        = newtheme.buttoncolor;
246 	t.currshortkeycolor  = newtheme.currshortkeycolor;
247 	t.shortkeycolor      = newtheme.shortkeycolor;
248 
249 	t.bottomtitlecolor   = newtheme.bottomtitlecolor;
250 
251 	bkgd(t.backgroundcolor);
252 
253 	refresh();
254 }
255 
256 int bsddialog_set_default_theme(enum bsddialog_default_theme newtheme)
257 {
258 
259 	if (newtheme == BSDDIALOG_THEME_DEFAULT)
260 		bsddialog_set_theme(dialogtheme);
261 	else if (newtheme == BSDDIALOG_THEME_BSDDIALOG)
262 		bsddialog_set_theme(bsddialogtheme);
263 	else if (newtheme == BSDDIALOG_THEME_BLACKWHITE)
264 		bsddialog_set_theme(blackwhite);
265 	else if (newtheme == BSDDIALOG_THEME_DIALOG)
266 		bsddialog_set_theme(dialogtheme);
267 	else if (newtheme == BSDDIALOG_THEME_MAGENTA)
268 		bsddialog_set_theme(magentatheme);
269 	else
270 		RETURN_ERROR("Unknow default theme");
271 
272 	return 0;
273 }
274 
275 int
276 bsddialog_color(enum bsddialog_color background, enum bsddialog_color foreground)
277 {
278 
279 	return GET_COLOR(background, foreground);
280 }
281 
282 struct bsddialog_theme bsddialog_get_theme()
283 {
284 
285 	return t;
286 }
287