C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_notcurses_draw.c
Go to the documentation of this file.
1/** @file ui_notcurses_draw.c
2 @ingroup ui_notcurses
3 @brief NotCurses UI backend — drawing functions.
4
5 Implements the drawing operations declared in ui_backend.h using the
6 NotCurses API.
7*/
8#define _GNU_SOURCE
9#define _XOPEN_SOURCE_EXTENDED 1
10
11#include "ui_backend.h"
13#include <notcurses/notcurses.h>
14#include <stddef.h>
15#include <stdlib.h>
16#include <string.h>
17
18/* -------------------------------------------------------------------------
19 Housekeeping functions
20 ------------------------------------------------------------------------- */
21
22int ui_wclrtoeol(UiSurface *s, uint w) {
23 if (!s)
24 return -1;
25 uint y, x, ylen, xlen, maxy, maxx;
26 ncplane_cursor_yx(s->mplane[w], &y, &x);
27 ncplane_dim_yx(s->mplane[w], &maxy, &maxx);
28 xlen = maxx - x;
29 ylen = 1;
30 ncplane_erase_region(s->mplane[w], y, x, ylen, xlen);
31 return 0;
32}
33
34int ui_wclrtobot(UiSurface *s, uint w) {
35 if (!s)
36 return -1;
37 uint y, x, ylen, xlen, maxy, maxx;
38 ncplane_cursor_yx(s->mplane[w], &y, &x);
39 ncplane_dim_yx(s->mplane[w], &maxy, &maxx);
40 xlen = maxx - x;
41 ylen = 1;
42 ncplane_erase_region(s->mplane[w], y, x, ylen, xlen);
43 ylen = maxy - y - 1;
44 xlen = maxx;
45 ncplane_erase_region(s->mplane[w], y + 1, x = 0, ylen, maxx);
46 return 0;
47}
48
49/* -------------------------------------------------------------------------
50 Text
51 ------------------------------------------------------------------------- */
52int mk_chimera(UiCell *cell, char c) {
53 cell->gcluster = c;
54 cell->gcluster_backstop = 0;
57 return 0;
58}
59int ui_draw_ch(UiSurface *s, uint w, char c) {
60 if (!s || !c)
61 return -1;
62 nccell cell = DEFAULT_INITIALIZER(c);
63 ncplane_putc_yx(s->mplane[w], -1, -1, &cell);
64 return 0;
65}
66int ui_draw_ch_yx(UiSurface *s, uint w, uint y, uint x, char c) {
67 if (!s || !c)
68 return -1;
69 nccell cell = DEFAULT_INITIALIZER(c);
70 ncplane_putc_yx(s->mplane[w], y, x, &cell);
71 return 0;
72}
73int ui_draw_text(UiSurface *s, uint w, uint y, uint x, const char *text) {
74 if (!s || !text)
75 return -1;
76 ncplane_putstr_yx(s->mplane[w], y, x, text);
77 return 0;
78}
79int ui_draw_text_n(UiSurface *s, uint w, uint y, uint x, const char *text, int m) {
80 if (!s || !text)
81 return -1;
82 ncplane_putnstr_yx(s->mplane[w], y, x, m, text);
83 return 0;
84}
85int ui_draw_text_fill(UiSurface *s, uint w, uint y, uint x, const char *text, int m) {
86 if (!s || !text)
87 return -1;
88 char tmp_str[MAXLEN];
89 strncpy(tmp_str, text, m);
90 int l = strlen(text);
91 for (int i = l; i < m; i++) {
92 tmp_str[i] = ' ';
93 }
94 tmp_str[m] = '\0';
95 ncplane_putnstr_yx(s->mplane[w], y, x, m, tmp_str);
97 return 0;
98}
99// -------------------------------------------------------------------------
100int ui_mvwaddch(UiSurface *s, uint w, uint y, uint x, const char c) {
101 if (!s || !c)
102 return -1;
103 ui_wmove(s, w, y, x);
104 nccell cell = DEFAULT_INITIALIZER(c);
105 ncplane_putc_yx(s->mplane[w], -1, -1, &cell);
106 return 0;
107}
108int ui_waddstr(UiSurface *s, uint w, const char *text) {
109 if (!s || !text)
110 return -1;
111 ncplane_putstr(s->mplane[w], text);
112 return 0;
113}
114int ui_waddnstr(UiSurface *s, uint w, const char *text, int m) {
115 if (!s || !text)
116 return -1;
117 ncplane_putnstr(s->mplane[w], m, text);
118 return 0;
119}
120int ui_mvwaddstr(UiSurface *s, uint w, uint y, uint x, const char *text) {
121 if (!s || !text)
122 return -1;
123 ncplane_putstr_yx(s->mplane[w], y, x, text);
124 return 0;
125}
126int ui_mvwaddnstr(UiSurface *s, uint w, uint y, uint x, const char *text, int m) {
127 if (!s || !text)
128 return -1;
129 ncplane_putnstr_yx(s->mplane[w], y, x, m, text);
130 return 0;
131}
132int ui_mvwaddstr_fill(UiSurface *s, uint w, uint y, uint x, const char *text, int m) {
133 if (!s || !text)
134 return -1;
135 int l = strlen(text);
136 if (l <= m) {
137 char *tmp_str = (char *)malloc(m + 1);
138 if (!tmp_str)
139 return -1;
140 strcpy(tmp_str, text);
141 for (int i = l; i < (int)m; i++) {
142 tmp_str[i] = ' ';
143 }
144 tmp_str[m] = '\0';
145 ncplane_putnstr_yx(s->mplane[w], y, x, m, tmp_str);
146 free(tmp_str);
147 }
148 return 0;
149}
150// ---------------------------------------------------------------------------
151// Wide Characters
152// ---------------------------------------------------------------------------
153int ui_waddwstr(UiSurface *s, uint w, const wchar_t *wstr) {
154 if (!s || !wstr)
155 return -1;
156 while (*wstr != L'\0')
157 ncplane_putwc_yx(s->mplane[w], -1, -1, *wstr++);
158 return 0;
159}
160int ui_mvwaddwstr(UiSurface *s, uint w, uint y, uint x, const wchar_t *wstr) {
161 if (!s || !wstr)
162 return -1;
163 while (*wstr != L'\0')
164 ncplane_putwc_yx(s->mplane[w], y, x++, *wstr++);
165 return 0;
166}
167int ui_waddnwstr(UiSurface *s, uint w, const wchar_t *wstr, int m) {
168 if (!s || !wstr)
169 return -1;
170 int cols = 0;
171 int width;
172 while (*wstr != L'\0') {
173 width = wcwidth(*wstr);
174 if (width < 0)
175 width = 0;
176 if (cols + width > m)
177 break;
178 ncplane_putwc_yx(s->mplane[w], -1, -1, *wstr++);
179 }
180 return 0;
181}
182int ui_mvwaddnwstr(UiSurface *s, uint w, uint y, uint x, const wchar_t *wstr, int m) {
183 if (!s || !wstr)
184 return -1;
185 int cols = 0;
186 int width;
187 while (*wstr != L'\0') {
188 width = wcwidth(*wstr);
189 if (width < 0)
190 width = 0;
191 if (cols + width > m)
192 break;
193 ncplane_putwc_yx(s->mplane[w], y, x++, *wstr++);
194 cols += wcwidth(*wstr);
195 }
196 return 0;
197}
198
199// ---------------------------------------------------------------------------
200// UiCells
201// ---------------------------------------------------------------------------
202int ui_wadd_wch(UiSurface *s, uint w, const UiCell *cell) {
203 if (!s)
204 return -1;
205 ncplane_putc(s->mplane[w], cell);
206 return 0;
207}
208int ui_mvwadd_wch(UiSurface *s, uint w, uint y, uint x, const UiCell *cell) {
209 if (!s)
210 return -1;
211 ncplane_putc_yx(s->mplane[w], y, x, cell);
212 return 0;
213}
214int ui_wadd_wchstr(UiSurface *s, uint w, const UiCell *cell) {
215 if (!s)
216 return -1;
217 while (cell->gcluster != 0)
218 ncplane_putc(s->mplane[w], cell++);
219 return 0;
220}
221int ui_mvwadd_wchstr(UiSurface *s, uint w, uint y, uint x, const UiCell *cell) {
222 if (!s)
223 return -1;
224 ui_wmove(s, w, y, x);
225 while (cell->gcluster != 0)
226 ncplane_putc(s->mplane[w], cell++);
227 return 0;
228}
229int ui_wadd_wchnstr(UiSurface *s, uint w, const UiCell *cell, uint m) {
230 if (!s)
231 return -1;
232 uint cols = 0;
233 while (cell->gcluster != 0 && cols < m) {
234 ncplane_putc(s->mplane[w], cell++);
235 cols += cell->width;
236 }
237 return 0;
238}
239
240int ui_mvwadd_wchnstr(UiSurface *s, uint w, uint y, uint x, const UiCell *cell, uint m) {
241 if (!s)
242 return -1;
243 ui_wmove(s, w, y, x);
244 uint cols = 0;
245 while (cell->gcluster != 0 && cols < m) {
246 ncplane_putc(s->mplane[w], cell++);
247 cols += cell->width;
248 }
249 return 0;
250}
251/* -------------------------------------------------------------------------
252 Screen managemen t
253 ------------------------------------------------------------------------- */
254
256 // for (int s = 0; s <= sfc_ptr; s++) {
257 // for (int w = 0; w < 8; w++)
258 // if (ui_surface[s]->mplane[w] != nullptr)
259 // // touchwin(ui_surface[s]->mplane[w]);
260 // }
261 // ui_render(ui);
262}
int ui_waddnstr(UiSurface *s, uint w, const char *text, int m)
void ui_render()
Definition ui_ncurses.c:800
int ui_wadd_wch(UiSurface *s, uint w, const UiCell *cell)
int ui_draw_ch_yx(UiSurface *s, uint w, uint y, uint x, const char c)
int ui_wclrtobot(UiSurface *s, uint w)
int ui_mvwadd_wchstr(UiSurface *s, uint w, uint y, uint x, const UiCell *cell)
int ui_mvwaddnstr(UiSurface *s, uint w, uint y, uint x, const char *text, int m)
int ui_mvwaddstr(UiSurface *s, uint w, uint y, uint x, const char *text)
int ui_wadd_wchnstr(UiSurface *s, uint w, const UiCell *cell, uint m)
int ui_mvwaddnwstr(UiSurface *s, uint w, uint y, uint x, const wchar_t *wstr, int m)
int ui_draw_text_n(UiSurface *s, uint w, uint y, uint x, const char *text, int m)
int ui_mvwadd_wchnstr(UiSurface *s, uint w, uint y, uint x, const UiCell *cell, uint m)
int ui_mvwaddch(UiSurface *s, uint w, uint y, uint x, const char c)
int ui_draw_text(UiSurface *s, uint w, uint y, uint x, const char *text)
int ui_waddnwstr(UiSurface *s, uint w, const wchar_t *wstr, int m)
void ui_restore_wins()
int ui_wmove(UiSurface *s, uint w, uint y, uint x)
Definition ui_ncurses.c:732
int ui_draw_text_fill(UiSurface *s, uint w, uint y, uint x, const char *text, int m)
int ui_waddstr(UiSurface *s, uint w, const char *text)
int ui_wclrtoeol(UiSurface *s, uint w)
struct nccell UiCell
Definition ui_backend.h:250
int ui_wadd_wchstr(UiSurface *s, uint w, const UiCell *cell)
#define DEFAULT_INITIALIZER(c)
Definition ui_backend.h:279
int ui_mvwadd_wch(UiSurface *s, uint w, uint y, uint x, const UiCell *cell)
int ui_mvwaddwstr(UiSurface *s, uint w, uint y, uint x, const wchar_t *wstr)
int ui_mvwaddstr_fill(UiSurface *s, uint w, uint y, uint x, const char *str, int m)
int ui_draw_ch(UiSurface *s, uint w, const char c)
int ui_waddwstr(UiSurface *s, uint w, const wchar_t *wstr)
UiCell bkgd_cell
int mk_chimera(UiCell *cell, char c)
#define MAXLEN
Definition curskeys.c:15
uint64_t channels
Definition nccell.h:8
uint16_t stylemask
Definition nccell.h:7
uint8_t gcluster_backstop
Definition nccell.h:5
uint8_t width
Definition nccell.h:6
uint32_t gcluster
Definition nccell.h:4