C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_ncurses_draw.c
Go to the documentation of this file.
1/** @file ui_ncurses_draw.c
2 @ingroup ui_ncurses
3 @brief NCurses UI backend — drawing functions.
4
5 Implements the drawing operations declared in ui_backend.h using the
6 NCurses wide-character API.
7*/
8
9#define _XOPEN_SOURCE_EXTENDED 1
10
11#include "cm.h"
12#include "ui_backend.h"
14#include <stdlib.h>
15#include <string.h>
16#include <wchar.h>
17
18/* -------------------------------------------------------------------------
19 Surface style
20 ------------------------------------------------------------------------- */
21/** @brief Set the style of a surface window.
22 @param s The surface to modify.
23 @param w The window index within the surface.
24 @param style The style to apply (UiStyle).
25 @return 0 on success, -1 on error.
26*/
27int ui_wclrtobot(UiSurface *s, ss_t w) {
28 if (!s)
29 return -1;
30 wclrtobot(s->mwin[w]);
31 return 0;
32}
33/** @brief Clear to the end of the line in a surface window.
34 @param s The surface to modify.
35 @param w The window index within the surface.
36 @return 0 on success, -1 on error.
37*/
38int ui_wclrtoeol(UiSurface *s, ss_t w) {
39 if (!s)
40 return -1;
41 wclrtoeol(s->mwin[w]);
42 return 0;
43}
44/* -------------------------------------------------------------------------
45 Text with UiStyle
46 ------------------------------------------------------------------------- */
47/** @brief Draw a character in a surface window.
48 @param s The surface to modify.
49 @param w The window index within the surface.
50 @param c The character to draw.
51 @return 0 on success, -1 on error.
52*/
53int ui_draw_ch(UiSurface *s, ss_t w, const char c) {
54 if (!s)
55 return -1;
56 waddch(s->mwin[w], c);
57 return 0;
58}
59/** @brief Draw a character at a specific position in a surface window.
60 @param s The surface to modify.
61 @param w The window index within the surface.
62 @param y The y-coordinate (row) to draw the character.
63 @param x The x-coordinate (column) to draw the character.
64 @param c The character to draw.
65 @return 0 on success, -1 on error.
66*/
67int ui_draw_ch_yx(UiSurface *s, ss_t w, uint y, uint x, const char c) {
68 if (!s)
69 return -1;
70 mvwaddch(s->mwin[w], y, x, c);
71 return 0;
72}
73// text string
74int ui_draw_text(UiSurface *s, ss_t w, uint y, uint x, const char *text) {
75 if (!s || !text)
76 return -1;
77 mvwaddstr(s->mwin[w], y, x, text);
78 return 0;
79}
80/** @brief Draw a text string at a specific position in a surface window.
81 @param s The surface to modify.
82 @param w The window index within the surface.
83 @param y The y-coordinate (row) to draw the text.
84 @param x The x-coordinate (column) to draw the text.
85 @param text The text string to draw.
86 @return 0 on success, -1 on error.
87*/
88int ui_draw_text_n(UiSurface *s, ss_t w, uint y, uint x, const char *text, int n) {
89 if (!s || !text)
90 return -1;
91 mvwaddnstr(s->mwin[w], y, x, text, (int)n);
92 return 0;
93}
94/** @brief Draw a text string at a specific position in a surface window, filling the remaining space with spaces if the string is shorter than n.
95 @param s The surface to modify.
96 @param w The window index within the surface.
97 @param y The y-coordinate (row) to draw the text.
98 @param x The x-coordinate (column) to draw the text.
99 @param text The text string to draw.
100 @param n The number of characters to draw, padding with spaces if necessary.
101 @return 0 on success, -1 on error.
102*/
103int ui_draw_text_fill(UiSurface *s, ss_t w, uint y, uint x, const char *text, int n) {
104 if (!s || !text)
105 return -1;
106 uint l = strlen(text);
107 if (l < (uint)n) {
108 char *tmp_str = (char *)malloc(n + 1);
109 if (!tmp_str)
110 return -1;
111 strcpy(tmp_str, text);
112 for (int i = l; i < (int)n; i++) {
113 tmp_str[i] = ' ';
114 }
115 tmp_str[n] = '\0';
116 mvwaddstr(s->mwin[w], y, x, tmp_str);
117 free(tmp_str);
118 } else {
119 mvwaddnstr(s->mwin[w], y, x, text, (int)n);
120 }
121 return 0;
122}
123// -------------------------------------------------------------------------
124// Text Characters
125// -------------------------------------------------------------------------
126/** @brief Draw a character in a surface window.
127 @param s The surface to modify.
128 @param w The window index within the surface.
129 @param c The character to draw.
130 @return 0 on success, -1 on error.
131*/
132int ui_mvwaddch(UiSurface *s, ss_t w, uint y, uint x, const char c) {
133 if (!s)
134 return -1;
135 mvwaddch(s->mwin[w], y, x, c);
136 return 0;
137}
138// -------------------------------------------------------------------------
139// Text Strings
140// -------------------------------------------------------------------------
141/** @brief Draw a text string in a surface window.
142 @param s The surface to modify.
143 @param w The window index within the surface.
144 @param text The text string to draw.
145 @return 0 on success, -1 on error.
146*/
147int ui_waddstr(UiSurface *s, ss_t w, const char *text) {
148 if (!s || !text)
149 return -1;
150 waddstr(s->mwin[w], text);
151 return 0;
152}
153/** @brief Draw a text string in a surface window, with a maximum length.
154 @param s The surface to modify.
155 @param w The window index within the surface.
156 @param text The text string to draw.
157 @param m The maximum number of characters to draw from the text string.
158 @return 0 on success, -1 on error.
159*/
160int ui_waddnstr(UiSurface *s, ss_t w, const char *text, int m) {
161 if (!s || !text)
162 return -1;
163 waddnstr(s->mwin[w], text, m);
164 return 0;
165}
166/** @brief Draw a text string at a specific position in a surface window.
167 @param s The surface to modify.
168 @param w The window index within the surface.
169 @param y The y-coordinate (row) to draw the text.
170 @param x The x-coordinate (column) to draw the text.
171 @param text The text string to draw.
172 @return 0 on success, -1 on error.
173*/
174int ui_mvwaddstr(UiSurface *s, ss_t w, uint y, uint x, const char *text) {
175 if (!s || !text)
176 return -1;
177 mvwaddstr(s->mwin[w], y, x, text);
178 return 0;
179}
180/** @brief Draw a text string at a specific position in a surface window, with a maximum length.
181 @param s The surface to modify.
182 @param w The window index within the surface.
183 @param y The y-coordinate (row) to draw the text.
184 @param x The x-coordinate (column) to draw the text.
185 @param text The text string to draw.
186 @param m The maximum number of characters to draw from the text string.
187 @return 0 on success, -1 on error.
188*/
189int ui_mvwaddnstr(UiSurface *s, ss_t w, uint y, uint x, const char *text, int m) {
190 if (!s || !text)
191 return -1;
192 mvwaddnstr(s->mwin[w], y, x, text, m);
193 return 0;
194}
195/** @brief Draw a text string at a specific position in a surface window, filling the remaining space with spaces if the string is shorter than m.
196 @param s The surface to modify.
197 @param w The window index within the surface.
198 @param y The y-coordinate (row) to draw the text.
199 @param x The x-coordinate (column) to draw the text.
200 @param str The text string to draw.
201 @param m The number of characters to draw, padding with spaces if necessary.
202 @return 0 on success, -1 on error.
203*/
204int ui_mvwaddstr_fill(UiSurface *s, ss_t w, uint y, uint x, const char *str, int m) {
205 char *d, *e;
206 uint maxy, maxx;
207 char tmp_str[MAXLEN];
208 getmaxyx(s->mwin[w], maxy, maxx);
209 y = min(y, maxy);
210 m = min(m, maxx);
211 m = min(m, (uint)MAXLEN - 1);
212 e = d = tmp_str;
213 e += m;
214 while (d < e) {
215 if (*str == '\0' || *str == '\n')
216 *d++ = ' ';
217 else
218 *d++ = *str++;
219 }
220 *d = '\0';
221 m = strlen(tmp_str);
222 mvwaddnstr(s->mwin[w], y, x, tmp_str, m);
223 return 0;
224}
225// -------------------------------------------------------------------------
226// Wide Character Strings
227// ---------------------------------------------------------------------------
228/** @brief Draw a wide-character string in a surface window.
229 @param s The surface to modify.
230 @param w The window index within the surface.
231 @param wstr The wide-character string to draw.
232 @return 0 on success, -1 on error.
233*/
234int ui_waddwstr(UiSurface *s, ss_t w, const wchar_t *wstr) {
235 if (!s || !wstr)
236 return -1;
237 waddwstr(s->mwin[w], wstr);
238 return 0;
239}
240/** @brief Draw a wide-character string at a specific position in a surface window.
241 @param s The surface to modify.
242 @param w The window index within the surface.
243 @param y The y-coordinate (row) to draw the wide-character string.
244 @param x The x-coordinate (column) to draw the wide-character string.
245 @param wstr The wide-character string to draw.
246 @return 0 on success, -1 on error.
247*/
248int ui_mvwaddwstr(UiSurface *s, ss_t w, uint y, uint x, const wchar_t *wstr) {
249 if (!s || !wstr)
250 return -1;
251 mvwaddwstr(s->mwin[w], y, x, wstr);
252 return 0;
253}
254/** @brief Draw a wide-character string in a surface window, with a maximum length.
255 @param s The surface to modify.
256 @param w The window index within the surface.
257 @param wstr The wide-character string to draw.
258 @param m The maximum number of wide characters to draw from the wide-character string.
259 @return 0 on success, -1 on error.
260*/
261int ui_waddnwstr(UiSurface *s, ss_t w, const wchar_t *wstr, int m) {
262 if (!s || !wstr)
263 return -1;
264 waddnwstr(s->mwin[w], wstr, m);
265 return 0;
266}
267/** @brief Draw a wide-character string at a specific position in a surface window, with a maximum length.
268 @param s The surface to modify.
269 @param w The window index within the surface.
270 @param y The y-coordinate (row) to draw the wide-character string.
271 @param x The x-coordinate (column) to draw the wide-character string.
272 @param wstr The wide-character string to draw.
273 @param m The maximum number of wide characters to draw from the wide-character string.
274 @return 0 on success, -1 on error.
275*/
276int ui_mvwaddnwstr(UiSurface *s, ss_t w, uint y, uint x, const wchar_t *wstr, int m) {
277 if (!s || !wstr)
278 return -1;
279 mvwaddnwstr(s->mwin[w], y, x, wstr, m);
280 return 0;
281}
282// -------------------------------------------------------------------------
283// Complex Characters (cc)
284// ---------------------------------------------------------------------------
285/** @brief Draw a complex character in a surface window.
286 @param s The surface to modify.
287 @param w The window index within the surface.
288 @param cell The complex character (UiCell) to draw.
289 @return 0 on success, -1 on error.
290*/
291int ui_wadd_wch(UiSurface *s, ss_t w, const UiCell *cell) {
292 if (!s)
293 return -1;
294 wadd_wchnstr(s->mwin[w], cell, 1);
295 return 0;
296}
297/** @brief Draw a complex character at a specific position in a surface window.
298 @param s The surface to modify.
299 @param w The window index within the surface.
300 @param y The y-coordinate (row) to draw the complex character.
301 @param x The x-coordinate (column) to draw the complex character.
302 @param cell The complex character (UiCell) to draw.
303 @return 0 on success, -1 on error.
304*/
305int ui_mvwadd_wch(UiSurface *s, ss_t w, uint y, uint x, const UiCell *cell) {
306 if (!s)
307 return -1;
308 mvwadd_wch(s->mwin[w], y, x, cell);
309 return 0;
310}
311/** @brief Draw a complex character string in a surface window.
312 @param s The surface to modify.
313 @param w The window index within the surface.
314 @param cmplx_buf The complex character string (array of UiCell) to draw.
315 @return 0 on success, -1 on error.
316*/
317int ui_wadd_wchstr(UiSurface *s, ss_t w, const UiCell *cmplx_buf) {
318 if (!s || !cmplx_buf)
319 return -1;
320 wadd_wchstr(s->mwin[w], cmplx_buf);
321 return 0;
322}
323/** @brief Draw a complex character string at a specific position in a surface window.
324 @param s The surface to modify.
325 @param w The window index within the surface.
326 @param y The y-coordinate (row) to draw the complex character string.
327 @param x The x-coordinate (column) to draw the complex character string.
328 @param cmplx_buf The complex character string (array of UiCell) to draw.
329 @return 0 on success, -1 on error.
330 */
331int ui_mvwadd_wchstr(UiSurface *s, ss_t w, uint y, uint x, const UiCell *cmplx_buf) {
332 if (!s || !cmplx_buf)
333 return -1;
334 mvwadd_wchstr(s->mwin[w], y, x, cmplx_buf);
335 return 0;
336}
337/** @brief Draw a complex character string in a surface window, with a maximum length.
338 @param s The surface to modify.
339 @param w The window index within the surface.
340 @param cmplx_buf The complex character string (array of UiCell) to draw.
341 @param n The maximum number of complex characters to draw from the complex character string.
342 @return 0 on success, -1 on error.
343*/
344int ui_wadd_wchnstr(UiSurface *s, ss_t w, const UiCell *cmplx_buf, uint n) {
345 if (!s || !cmplx_buf)
346 return -1;
347 wadd_wchnstr(s->mwin[w], cmplx_buf, n);
348 return 0;
349}
350/** @brief Draw a complex character string at a specific position in a surface window, with a maximum length.
351 @param s The surface to modify.
352 @param w The window index within the surface.
353 @param y The y-coordinate (row) to draw the complex character string.
354 @param x The x-coordinate (column) to draw the complex character string.
355 @param cmplx_buf The complex character string (array of UiCell) to draw.
356 @param n The maximum number of complex characters to draw from the complex character string.
357 @return 0 on success, -1 on error.
358*/
359int ui_mvwadd_wchnstr(UiSurface *s, ss_t w, uint y, uint x, const UiCell *cmplx_buf, uint n) {
360 if (!s || !cmplx_buf)
361 return -1;
362 mvwadd_wchnstr(s->mwin[w], y, x, cmplx_buf, n);
363 return 0;
364}
365/* -------------------------------------------------------------------------
366 Screen management
367 ------------------------------------------------------------------------- */
368/** @brief Restore the state of all windows in the UI.
369 This function refreshes the standard screen and all surface windows, ensuring that their contents are displayed correctly.
370*/
372 touchwin(stdscr);
373 for (int s = 0; s <= sfc_ptr; s++) {
374 for (ss_t w = BOX; w < SUB_SFC_MAX; w++)
375 if (ui_surface[s]->mwin[w] != nullptr)
376 touchwin(ui_surface[s]->mwin[w]);
377 }
379}
#define min(x, y)
min macro evaluates two expressions, returning least result
Definition cm.h:104
int ui_draw_ch_yx(UiSurface *s, ss_t w, uint y, uint x, const char c)
Draw a character at a specific position in a surface window.
void ui_render()
Render the UI by updating panels and refreshing the display.
Definition ui_ncurses.c:884
int ui_draw_text_n(UiSurface *s, ss_t w, uint y, uint x, const char *text, int m)
Draw a text string at a specific position in a surface window.
int ui_draw_text_fill(UiSurface *s, ss_t w, uint y, uint x, const char *text, int m)
Draw a text string at a specific position in a surface window, filling the remaining space with space...
int ui_draw_text(UiSurface *s, ss_t w, uint y, uint x, const char *text)
on specified window, move to y, x, add string
@ SUB_SFC_MAX
Definition ui_backend.h:47
void ui_restore_wins()
Restore the state of all windows in the UI. This function refreshes the standard screen and all surfa...
int ui_draw_ch(UiSurface *s, ss_t w, const char c)
Draw a character in a surface window.
int sfc_ptr
Definition ui_ncurses.c:42
int ui_wclrtobot(UiSurface *s, ss_t w)
Set the style of a surface window.
int ui_wclrtoeol(UiSurface *s, ss_t w)
Clear to the end of the line in a surface window.
UiSurface * ui_surface[UI_SFC_MAX]
Definition ui_ncurses.c:40
int ui_mvwadd_wchnstr(UiSurface *s, ss_t w, uint y, uint x, const UiCell *cmplx_buf, uint n)
Draw a complex character string at a specific position in a surface window, with a maximum length.
int ui_waddstr(UiSurface *s, ss_t w, const char *text)
Draw a text string in a surface window.
int ui_mvwaddnwstr(UiSurface *s, ss_t w, uint y, uint x, const wchar_t *wstr, int m)
Draw a wide-character string at a specific position in a surface window, with a maximum length.
int ui_waddwstr(UiSurface *s, ss_t w, const wchar_t *wstr)
Draw a wide-character string in a surface window.
int ui_mvwaddch(UiSurface *s, ss_t w, uint y, uint x, const char c)
Draw a character in a surface window.
int ui_waddnstr(UiSurface *s, ss_t w, const char *text, int m)
Draw a text string in a surface window, with a maximum length.
int ui_mvwaddnstr(UiSurface *s, ss_t w, uint y, uint x, const char *text, int m)
Draw a text string at a specific position in a surface window, with a maximum length.
int ui_mvwadd_wchstr(UiSurface *s, ss_t w, uint y, uint x, const UiCell *cmplx_buf)
Draw a complex character string at a specific position in a surface window.
int ui_mvwaddwstr(UiSurface *s, ss_t w, uint y, uint x, const wchar_t *wstr)
Draw a wide-character string at a specific position in a surface window.
int ui_mvwaddstr(UiSurface *s, ss_t w, uint y, uint x, const char *text)
Draw a text string at a specific position in a surface window.
int ui_mvwadd_wch(UiSurface *s, ss_t w, uint y, uint x, const UiCell *cell)
Draw a complex character at a specific position in a surface window.
int ui_mvwaddstr_fill(UiSurface *s, ss_t w, uint y, uint x, const char *str, int m)
Draw a text string at a specific position in a surface window, filling the remaining space with space...
int ui_wadd_wch(UiSurface *s, ss_t w, const UiCell *cell)
Draw a complex character in a surface window.
int ui_waddnwstr(UiSurface *s, ss_t w, const wchar_t *wstr, int m)
Draw a wide-character string in a surface window, with a maximum length.
int ui_wadd_wchstr(UiSurface *s, ss_t w, const UiCell *cmplx_buf)
Draw a complex character string in a surface window.
int ui_wadd_wchnstr(UiSurface *s, ss_t w, const UiCell *cmplx_buf, uint n)
Draw a complex character string in a surface window, with a maximum length.
#define MAXLEN
Definition curskeys.c:15