C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_ncurses_draw.c
Go to the documentation of this file.
2
3/** @file ui_ncurses_draw.c
4 @ingroup ui_ncurses
5 @brief Implementation of drawing functions for the ncurses UI backend.
6
7 This file contains the implementation of various drawing functions that
8 utilize the ncurses library to render text, lines, borders, and other
9 UI elements on the terminal. The functions take into account the styles
10 defined in UiStyle and apply them accordingly when drawing.
11 */
12
13/** @brief Convert an RGB value (0-255) to a 1000-based value for ncurses.
14 @ingroup ui_ncurses
15 Ncurses uses a 1000-based color system, so we need to convert our 0-255
16 RGB values to that scale.
17 @param v The RGB value (0-255).
18 @return The corresponding 1000-based value for ncurses.
19 */
20// static short clamp_rgb_1000(uint8_t v) {
21// return (short)((v * 1000) / 255);
22// }
23
24/** @brief Convert a UiStyle to an ncurses color pair index.
25 @ingroup ui_ncurses
26 This function maps the foreground and background colors from a UiStyle to
27 an ncurses color pair index. For simplicity, this example assumes a limited
28 set of colors and does not handle all possible combinations.
29 @param style The UiStyle containing the foreground and background colors.
30 @return The ncurses color pair index corresponding to the style.
31 */
32int ui_ncurses_color_pair_from_style(const UiStyle *style) {
33 (void)style;
34 return 0;
35}
36
37/** @brief Apply a UiStyle to an ncurses window.
38 @ingroup ui_ncurses
39 This function sets the appropriate attributes (bold, underline, reverse)
40 on the given ncurses window based on the properties of the UiStyle.
41 @param win The ncurses window to which the style should be applied.
42 @param style The UiStyle containing the attributes to apply.
43 @return 0 on success, -1 on failure (e.g., if win or style is NULL).
44 */
45int ui_ncurses_style_apply(WINDOW *win, const UiStyle *style) {
46 if (!win || !style)
47 return -1;
48 wchar_t wstr[2] = {L'\0', L'\0'};
49 cchar_t cc = {0};
50 attr_t attrs = 0;
51 uint32_t cpx = 0;
52 attrs |= style->bold ? WA_BOLD : 0;
53 attrs |= style->dim ? WA_DIM : 0;
54 attrs |= style->italic ? WA_ITALIC : 0;
55 attrs |= style->underline ? WA_UNDERLINE : 0;
56 attrs |= style->blink ? WA_BLINK : 0;
57 attrs |= style->reverse ? WA_REVERSE : 0;
58 attrs |= style->invis ? WA_INVIS : 0;
59 setcchar(&cc, wstr, attrs, cpx, nullptr);
60 return 0;
61}
62
63/** @brief Set the style for a UiSurface.
64 @ingroup ui_ncurses
65 This function applies the given UiStyle to the ncurses window associated
66 with the UiSurface. It uses the ui_ncurses_style_apply function to set
67 the appropriate attributes on the window.
68 @param s The UiSurface whose style is to be set.
69 @param style The UiStyle to apply to the surface.
70 @return 0 on success, -1 on failure (e.g., if s or style is NULL).
71 */
72int ui_surface_set_style(UiSurface *s, const UiStyle *style) {
73 if (!s || !style)
74 return -1;
76}
77
78/** @brief Set the base fill character and style for a UiSurface.
79 @ingroup ui_ncurses
80 This function sets the background fill character for the UiSurface's ncurses
81 window and applies the given UiStyle. If fill_ch is 0, it defaults to a
82 space character.
83 @param s The UiSurface to set the base for.
84 @param style The UiStyle to apply to the surface.
85 @param fill_ch The character to use for filling the background (0 for space).
86 @return 0 on success, -1 on failure (e.g., if s is NULL).
87 */
88int ui_surface_set_base(UiSurface *s, const UiStyle *style, uint32_t fill_ch) {
89 if (!s)
90 return -1;
91 if (style)
93 wbkgdset(s->win, (chtype)(fill_ch ? fill_ch : ' '));
94 return 0;
95}
96
97/** @brief Draw text on a UiSurface at a specified position with a given style.
98 @ingroup ui_ncurses
99 This function uses the ncurses mvwaddstr function to draw the provided text
100 at the specified (y, x) coordinates on the UiSurface's window. If a style
101 is provided, it applies that style before drawing the text.
102 @param s The UiSurface on which to draw the text.
103 @param y The y-coordinate (row) where the text should be drawn.
104 @param x The x-coordinate (column) where the text should be drawn.
105 @param style The UiStyle to apply to the text (can be NULL for default).
106 @param text The null-terminated string to draw.
107 @return 0 on success, -1 on failure (e.g., if s or text is NULL).
108 */
109int ui_draw_text(UiSurface *s, int y, int x, const UiStyle *style, const char *text) {
110 if (!s || !text)
111 return -1;
112 if (style)
114 mvwaddstr(s->win, y, x, text);
115 return 0;
116}
117
118/** @brief Draw a specified number of characters from a string on a UiSurface.
119 @ingroup ui_ncurses
120 This function is similar to ui_draw_text but allows specifying the number of
121 characters to draw from the provided string. It uses the ncurses mvwaddnstr
122 function to achieve this. If a style is provided, it applies that style before
123 drawing the text.
124 @param s The UiSurface on which to draw the text.
125 @param y The y-coordinate (row) where the text should be drawn.
126 @param x The x-coordinate (column) where the text should be drawn.
127 @param style The UiStyle to apply to the text (can be NULL for default).
128 @param text The string from which to draw characters (not necessarily null-terminated).
129 @param n The number of characters to draw from the string.
130 @return 0 on success, -1 on failure (e.g., if s or text is NULL).
131 */
132int ui_draw_text_n(UiSurface *s, int y, int x, const UiStyle *style, const char *text, size_t n) {
133 if (!s || !text)
134 return -1;
135 if (style)
137 mvwaddnstr(s->win, y, x, text, (int)n);
138 return 0;
139}
140
141/** @brief Draw a horizontal line on a UiSurface.
142 @ingroup ui_ncurses
143 This function uses the ncurses mvwhline function to draw a horizontal line of
144 a specified length at the given (y, x) coordinates on the UiSurface's window.
145 If a style is provided, it applies that style before drawing the line.
146 @param s The UiSurface on which to draw the horizontal line.
147 @param y The y-coordinate (row) where the line should be drawn.
148 @param x The x-coordinate (column) where the line should start.
149 @param len The length of the horizontal line to draw.
150 @param style The UiStyle to apply to the line (can be NULL for default).
151 @return 0 on success, -1 on failure (e.g., if s is NULL).
152 */
153int ui_draw_hline(UiSurface *s, int y, int x, int len, const UiStyle *style) {
154 if (!s)
155 return -1;
156 if (style)
158 mvwhline(s->win, y, x, 0, len);
159 return 0;
160}
161
162/** @brief Draw a vertical line on a UiSurface.
163 @ingroup ui_ncurses
164 This function uses the ncurses mvwvline function to draw a vertical line of
165 a specified length at the given (y, x) coordinates on the UiSurface's window.
166 If a style is provided, it applies that style before drawing the line.
167 @param s The UiSurface on which to draw the vertical line.
168 @param y The y-coordinate (row) where the line should start.
169 @param x The x-coordinate (column) where the line should be drawn.
170 @param len The length of the vertical line to draw.
171 @param style The UiStyle to apply to the line (can be NULL for default).
172 @return 0 on success, -1 on failure (e.g., if s is NULL).
173 */
174int ui_draw_vline(UiSurface *s, int y, int x, int len, const UiStyle *style) {
175 if (!s)
176 return -1;
177 if (style)
179 mvwvline(s->win, y, x, 0, len);
180 return 0;
181}
182
183/** @brief Draw a border around a UiSurface.
184 @ingroup ui_ncurses
185 This function draws a border around the UiSurface's window based on the
186 specified UiBorderKind. It uses the ncurses box function to draw the border
187 with the appropriate characters. If a style is provided, it applies that style
188 before drawing the border.
189 @param s The UiSurface on which to draw the border.
190 @param kind The kind of border to draw (e.g., none, ASCII, light, rounded).
191 @param style The UiStyle to apply to the border (can be NULL for default).
192 @return 0 on success, -1 on failure (e.g., if s is NULL).
193 */
194int ui_draw_border(UiSurface *s, UiBorderKind kind, const UiStyle *style) {
195 if (!s)
196 return -1;
197 if (style)
199
200 switch (kind) {
201 case UI_BORDER_NONE:
202 return 0;
203 case UI_BORDER_ASCII:
204 box(s->win, '|', '-');
205 return 0;
206 case UI_BORDER_LIGHT:
208 default:
209 box(s->win, 0, 0);
210 return 0;
211 }
212}
213
214/** @brief Draw a title on the top border of a UiSurface.
215 @ingroup ui_ncurses
216 This function draws the provided title string at the specified x-coordinate
217 on the top border of the UiSurface's window. It uses the ncurses mvwaddstr
218 function to place the title. If a style is provided, it applies that style
219 before drawing the title.
220 @param s The UiSurface on which to draw the box title.
221 @param x The x-coordinate (column) where the title should start.
222 @param style The UiStyle to apply to the title (can be NULL for default).
223 @param title The null-terminated string to use as the box title.
224 @return 0 on success, -1 on failure (e.g., if s or title is NULL).
225 */
226int ui_draw_box_title(UiSurface *s, int x, const UiStyle *style, const char *title) {
227 if (!s || !title)
228 return -1;
229 if (style)
231 mvwaddstr(s->win, 0, x, title);
232 return 0;
233}
@ UI_BORDER_ROUNDED
Definition ui_backend.h:119
@ UI_BORDER_ASCII
Definition ui_backend.h:117
@ UI_BORDER_LIGHT
Definition ui_backend.h:118
@ UI_BORDER_NONE
Definition ui_backend.h:116
int ui_draw_vline(UiSurface *s, int y, int x, int len, const UiStyle *style)
Draw a vertical line on a UiSurface.This function uses the ncurses mvwvline function to draw a vertic...
int ui_draw_border(UiSurface *s, UiBorderKind kind, const UiStyle *style)
Draw a border around a UiSurface.This function draws a border around the UiSurface's window based on ...
int ui_ncurses_style_apply(WINDOW *win, const UiStyle *style)
Apply a UiStyle to an ncurses window.This function sets the appropriate attributes (bold,...
int ui_surface_set_base(UiSurface *s, const UiStyle *style, uint32_t fill_ch)
Set the base fill character and style for a UiSurface.This function sets the background fill characte...
int ui_draw_box_title(UiSurface *s, int x, const UiStyle *style, const char *title)
Draw a title on the top border of a UiSurface.This function draws the provided title string at the sp...
int ui_draw_text_n(UiSurface *s, int y, int x, const UiStyle *style, const char *text, size_t n)
Draw a specified number of characters from a string on a UiSurface.This function is similar to ui_dra...
int ui_draw_text(UiSurface *s, int y, int x, const UiStyle *style, const char *text)
Draw text on a UiSurface at a specified position with a given style.This function uses the ncurses mv...
int ui_surface_set_style(UiSurface *s, const UiStyle *style)
Set the style for a UiSurface.This function applies the given UiStyle to the ncurses window associate...
int ui_ncurses_color_pair_from_style(const UiStyle *style)
Convert an RGB value (0-255) to a 1000-based value for ncurses.Ncurses uses a 1000-based color system...
int ui_draw_hline(UiSurface *s, int y, int x, int len, const UiStyle *style)
Draw a horizontal line on a UiSurface.This function uses the ncurses mvwhline function to draw a hori...
bool dim
Definition ui_backend.h:162
bool underline
Definition ui_backend.h:164
bool blink
Definition ui_backend.h:165
bool invis
Definition ui_backend.h:167
bool italic
Definition ui_backend.h:163
bool reverse
Definition ui_backend.h:166
bool bold
Definition ui_backend.h:161